]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/acpi/arm64/iort.c
d048f72c23f84719558e2cda0c9a7d17062d6347
[karo-tx-linux.git] / drivers / acpi / arm64 / iort.c
1 /*
2  * Copyright (C) 2016, Semihalf
3  *      Author: Tomasz Nowicki <tn@semihalf.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * This file implements early detection/parsing of I/O mapping
15  * reported to OS through firmware via I/O Remapping Table (IORT)
16  * IORT document number: ARM DEN 0049A
17  */
18
19 #define pr_fmt(fmt)     "ACPI: IORT: " fmt
20
21 #include <linux/acpi_iort.h>
22 #include <linux/iommu.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28
29 #define IORT_TYPE_MASK(type)    (1 << (type))
30 #define IORT_MSI_TYPE           (1 << ACPI_IORT_NODE_ITS_GROUP)
31 #define IORT_IOMMU_TYPE         ((1 << ACPI_IORT_NODE_SMMU) |   \
32                                 (1 << ACPI_IORT_NODE_SMMU_V3))
33
34 struct iort_its_msi_chip {
35         struct list_head        list;
36         struct fwnode_handle    *fw_node;
37         u32                     translation_id;
38 };
39
40 struct iort_fwnode {
41         struct list_head list;
42         struct acpi_iort_node *iort_node;
43         struct fwnode_handle *fwnode;
44 };
45 static LIST_HEAD(iort_fwnode_list);
46 static DEFINE_SPINLOCK(iort_fwnode_lock);
47
48 /**
49  * iort_set_fwnode() - Create iort_fwnode and use it to register
50  *                     iommu data in the iort_fwnode_list
51  *
52  * @node: IORT table node associated with the IOMMU
53  * @fwnode: fwnode associated with the IORT node
54  *
55  * Returns: 0 on success
56  *          <0 on failure
57  */
58 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
59                                   struct fwnode_handle *fwnode)
60 {
61         struct iort_fwnode *np;
62
63         np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
64
65         if (WARN_ON(!np))
66                 return -ENOMEM;
67
68         INIT_LIST_HEAD(&np->list);
69         np->iort_node = iort_node;
70         np->fwnode = fwnode;
71
72         spin_lock(&iort_fwnode_lock);
73         list_add_tail(&np->list, &iort_fwnode_list);
74         spin_unlock(&iort_fwnode_lock);
75
76         return 0;
77 }
78
79 /**
80  * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
81  *
82  * @node: IORT table node to be looked-up
83  *
84  * Returns: fwnode_handle pointer on success, NULL on failure
85  */
86 static inline
87 struct fwnode_handle *iort_get_fwnode(struct acpi_iort_node *node)
88 {
89         struct iort_fwnode *curr;
90         struct fwnode_handle *fwnode = NULL;
91
92         spin_lock(&iort_fwnode_lock);
93         list_for_each_entry(curr, &iort_fwnode_list, list) {
94                 if (curr->iort_node == node) {
95                         fwnode = curr->fwnode;
96                         break;
97                 }
98         }
99         spin_unlock(&iort_fwnode_lock);
100
101         return fwnode;
102 }
103
104 /**
105  * iort_delete_fwnode() - Delete fwnode associated with an IORT node
106  *
107  * @node: IORT table node associated with fwnode to delete
108  */
109 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
110 {
111         struct iort_fwnode *curr, *tmp;
112
113         spin_lock(&iort_fwnode_lock);
114         list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
115                 if (curr->iort_node == node) {
116                         list_del(&curr->list);
117                         kfree(curr);
118                         break;
119                 }
120         }
121         spin_unlock(&iort_fwnode_lock);
122 }
123
124 typedef acpi_status (*iort_find_node_callback)
125         (struct acpi_iort_node *node, void *context);
126
127 /* Root pointer to the mapped IORT table */
128 static struct acpi_table_header *iort_table;
129
130 static LIST_HEAD(iort_msi_chip_list);
131 static DEFINE_SPINLOCK(iort_msi_chip_lock);
132
133 /**
134  * iort_register_domain_token() - register domain token and related ITS ID
135  * to the list from where we can get it back later on.
136  * @trans_id: ITS ID.
137  * @fw_node: Domain token.
138  *
139  * Returns: 0 on success, -ENOMEM if no memory when allocating list element
140  */
141 int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
142 {
143         struct iort_its_msi_chip *its_msi_chip;
144
145         its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
146         if (!its_msi_chip)
147                 return -ENOMEM;
148
149         its_msi_chip->fw_node = fw_node;
150         its_msi_chip->translation_id = trans_id;
151
152         spin_lock(&iort_msi_chip_lock);
153         list_add(&its_msi_chip->list, &iort_msi_chip_list);
154         spin_unlock(&iort_msi_chip_lock);
155
156         return 0;
157 }
158
159 /**
160  * iort_deregister_domain_token() - Deregister domain token based on ITS ID
161  * @trans_id: ITS ID.
162  *
163  * Returns: none.
164  */
165 void iort_deregister_domain_token(int trans_id)
166 {
167         struct iort_its_msi_chip *its_msi_chip, *t;
168
169         spin_lock(&iort_msi_chip_lock);
170         list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
171                 if (its_msi_chip->translation_id == trans_id) {
172                         list_del(&its_msi_chip->list);
173                         kfree(its_msi_chip);
174                         break;
175                 }
176         }
177         spin_unlock(&iort_msi_chip_lock);
178 }
179
180 /**
181  * iort_find_domain_token() - Find domain token based on given ITS ID
182  * @trans_id: ITS ID.
183  *
184  * Returns: domain token when find on the list, NULL otherwise
185  */
186 struct fwnode_handle *iort_find_domain_token(int trans_id)
187 {
188         struct fwnode_handle *fw_node = NULL;
189         struct iort_its_msi_chip *its_msi_chip;
190
191         spin_lock(&iort_msi_chip_lock);
192         list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
193                 if (its_msi_chip->translation_id == trans_id) {
194                         fw_node = its_msi_chip->fw_node;
195                         break;
196                 }
197         }
198         spin_unlock(&iort_msi_chip_lock);
199
200         return fw_node;
201 }
202
203 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
204                                              iort_find_node_callback callback,
205                                              void *context)
206 {
207         struct acpi_iort_node *iort_node, *iort_end;
208         struct acpi_table_iort *iort;
209         int i;
210
211         if (!iort_table)
212                 return NULL;
213
214         /* Get the first IORT node */
215         iort = (struct acpi_table_iort *)iort_table;
216         iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
217                                  iort->node_offset);
218         iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
219                                 iort_table->length);
220
221         for (i = 0; i < iort->node_count; i++) {
222                 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
223                                "IORT node pointer overflows, bad table!\n"))
224                         return NULL;
225
226                 if (iort_node->type == type &&
227                     ACPI_SUCCESS(callback(iort_node, context)))
228                         return iort_node;
229
230                 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
231                                          iort_node->length);
232         }
233
234         return NULL;
235 }
236
237 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
238                                             void *context)
239 {
240         struct device *dev = context;
241         acpi_status status = AE_NOT_FOUND;
242
243         if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
244                 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
245                 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
246                 struct acpi_iort_named_component *ncomp;
247
248                 if (!adev)
249                         goto out;
250
251                 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
252                 if (ACPI_FAILURE(status)) {
253                         dev_warn(dev, "Can't get device full path name\n");
254                         goto out;
255                 }
256
257                 ncomp = (struct acpi_iort_named_component *)node->node_data;
258                 status = !strcmp(ncomp->device_name, buf.pointer) ?
259                                                         AE_OK : AE_NOT_FOUND;
260                 acpi_os_free(buf.pointer);
261         } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
262                 struct acpi_iort_root_complex *pci_rc;
263                 struct pci_bus *bus;
264
265                 bus = to_pci_bus(dev);
266                 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
267
268                 /*
269                  * It is assumed that PCI segment numbers maps one-to-one
270                  * with root complexes. Each segment number can represent only
271                  * one root complex.
272                  */
273                 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
274                                                         AE_OK : AE_NOT_FOUND;
275         }
276 out:
277         return status;
278 }
279
280 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
281                        u32 *rid_out)
282 {
283         /* Single mapping does not care for input id */
284         if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
285                 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
286                     type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
287                         *rid_out = map->output_base;
288                         return 0;
289                 }
290
291                 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
292                         map, type);
293                 return -ENXIO;
294         }
295
296         if (rid_in < map->input_base ||
297             (rid_in >= map->input_base + map->id_count))
298                 return -ENXIO;
299
300         *rid_out = map->output_base + (rid_in - map->input_base);
301         return 0;
302 }
303
304 static
305 struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
306                                         u32 *id_out, int index)
307 {
308         struct acpi_iort_node *parent;
309         struct acpi_iort_id_mapping *map;
310
311         if (!node->mapping_offset || !node->mapping_count ||
312                                      index >= node->mapping_count)
313                 return NULL;
314
315         map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
316                            node->mapping_offset + index * sizeof(*map));
317
318         /* Firmware bug! */
319         if (!map->output_reference) {
320                 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
321                        node, node->type);
322                 return NULL;
323         }
324
325         parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
326                                map->output_reference);
327
328         if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
329                 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
330                     node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
331                         *id_out = map->output_base;
332                         return parent;
333                 }
334         }
335
336         return NULL;
337 }
338
339 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
340                                                u32 id_in, u32 *id_out,
341                                                u8 type_mask)
342 {
343         u32 id = id_in;
344
345         /* Parse the ID mapping tree to find specified node type */
346         while (node) {
347                 struct acpi_iort_id_mapping *map;
348                 int i;
349
350                 if (IORT_TYPE_MASK(node->type) & type_mask) {
351                         if (id_out)
352                                 *id_out = id;
353                         return node;
354                 }
355
356                 if (!node->mapping_offset || !node->mapping_count)
357                         goto fail_map;
358
359                 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
360                                    node->mapping_offset);
361
362                 /* Firmware bug! */
363                 if (!map->output_reference) {
364                         pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
365                                node, node->type);
366                         goto fail_map;
367                 }
368
369                 /* Do the ID translation */
370                 for (i = 0; i < node->mapping_count; i++, map++) {
371                         if (!iort_id_map(map, node->type, id, &id))
372                                 break;
373                 }
374
375                 if (i == node->mapping_count)
376                         goto fail_map;
377
378                 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
379                                     map->output_reference);
380         }
381
382 fail_map:
383         /* Map input ID to output ID unchanged on mapping failure */
384         if (id_out)
385                 *id_out = id_in;
386
387         return NULL;
388 }
389
390 static
391 struct acpi_iort_node *iort_node_map_platform_id(struct acpi_iort_node *node,
392                                                  u32 *id_out, u8 type_mask,
393                                                  int index)
394 {
395         struct acpi_iort_node *parent;
396         u32 id;
397
398         /* step 1: retrieve the initial dev id */
399         parent = iort_node_get_id(node, &id, index);
400         if (!parent)
401                 return NULL;
402
403         /*
404          * optional step 2: map the initial dev id if its parent is not
405          * the target type we want, map it again for the use cases such
406          * as NC (named component) -> SMMU -> ITS. If the type is matched,
407          * return the initial dev id and its parent pointer directly.
408          */
409         if (!(IORT_TYPE_MASK(parent->type) & type_mask))
410                 parent = iort_node_map_id(parent, id, id_out, type_mask);
411         else
412                 if (id_out)
413                         *id_out = id;
414
415         return parent;
416 }
417
418 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
419 {
420         struct pci_bus *pbus;
421
422         if (!dev_is_pci(dev))
423                 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
424                                       iort_match_node_callback, dev);
425
426         /* Find a PCI root bus */
427         pbus = to_pci_dev(dev)->bus;
428         while (!pci_is_root_bus(pbus))
429                 pbus = pbus->parent;
430
431         return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
432                               iort_match_node_callback, &pbus->dev);
433 }
434
435 /**
436  * iort_msi_map_rid() - Map a MSI requester ID for a device
437  * @dev: The device for which the mapping is to be done.
438  * @req_id: The device requester ID.
439  *
440  * Returns: mapped MSI RID on success, input requester ID otherwise
441  */
442 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
443 {
444         struct acpi_iort_node *node;
445         u32 dev_id;
446
447         node = iort_find_dev_node(dev);
448         if (!node)
449                 return req_id;
450
451         iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
452         return dev_id;
453 }
454
455 /**
456  * iort_pmsi_get_dev_id() - Get the device id for a device
457  * @dev: The device for which the mapping is to be done.
458  * @dev_id: The device ID found.
459  *
460  * Returns: 0 for successful find a dev id, -ENODEV on error
461  */
462 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
463 {
464         int i;
465         struct acpi_iort_node *node;
466
467         node = iort_find_dev_node(dev);
468         if (!node)
469                 return -ENODEV;
470
471         for (i = 0; i < node->mapping_count; i++) {
472                 if (iort_node_map_platform_id(node, dev_id, IORT_MSI_TYPE, i))
473                         return 0;
474         }
475
476         return -ENODEV;
477 }
478
479 /**
480  * iort_dev_find_its_id() - Find the ITS identifier for a device
481  * @dev: The device.
482  * @req_id: Device's requester ID
483  * @idx: Index of the ITS identifier list.
484  * @its_id: ITS identifier.
485  *
486  * Returns: 0 on success, appropriate error value otherwise
487  */
488 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
489                                 unsigned int idx, int *its_id)
490 {
491         struct acpi_iort_its_group *its;
492         struct acpi_iort_node *node;
493
494         node = iort_find_dev_node(dev);
495         if (!node)
496                 return -ENXIO;
497
498         node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
499         if (!node)
500                 return -ENXIO;
501
502         /* Move to ITS specific data */
503         its = (struct acpi_iort_its_group *)node->node_data;
504         if (idx > its->its_count) {
505                 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
506                         idx, its->its_count);
507                 return -ENXIO;
508         }
509
510         *its_id = its->identifiers[idx];
511         return 0;
512 }
513
514 /**
515  * iort_get_device_domain() - Find MSI domain related to a device
516  * @dev: The device.
517  * @req_id: Requester ID for the device.
518  *
519  * Returns: the MSI domain for this device, NULL otherwise
520  */
521 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
522 {
523         struct fwnode_handle *handle;
524         int its_id;
525
526         if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
527                 return NULL;
528
529         handle = iort_find_domain_token(its_id);
530         if (!handle)
531                 return NULL;
532
533         return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
534 }
535
536 /**
537  * iort_get_platform_device_domain() - Find MSI domain related to a
538  * platform device
539  * @dev: the dev pointer associated with the platform device
540  *
541  * Returns: the MSI domain for this device, NULL otherwise
542  */
543 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
544 {
545         struct acpi_iort_node *node, *msi_parent;
546         struct fwnode_handle *iort_fwnode;
547         struct acpi_iort_its_group *its;
548         int i;
549
550         /* find its associated iort node */
551         node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
552                               iort_match_node_callback, dev);
553         if (!node)
554                 return NULL;
555
556         /* then find its msi parent node */
557         for (i = 0; i < node->mapping_count; i++) {
558                 msi_parent = iort_node_map_platform_id(node, NULL,
559                                                        IORT_MSI_TYPE, i);
560                 if (msi_parent)
561                         break;
562         }
563
564         if (!msi_parent)
565                 return NULL;
566
567         /* Move to ITS specific data */
568         its = (struct acpi_iort_its_group *)msi_parent->node_data;
569
570         iort_fwnode = iort_find_domain_token(its->identifiers[0]);
571         if (!iort_fwnode)
572                 return NULL;
573
574         return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
575 }
576
577 void acpi_configure_pmsi_domain(struct device *dev)
578 {
579         struct irq_domain *msi_domain;
580
581         msi_domain = iort_get_platform_device_domain(dev);
582         if (msi_domain)
583                 dev_set_msi_domain(dev, msi_domain);
584 }
585
586 static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
587 {
588         u32 *rid = data;
589
590         *rid = alias;
591         return 0;
592 }
593
594 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
595                                struct fwnode_handle *fwnode,
596                                const struct iommu_ops *ops)
597 {
598         int ret = iommu_fwspec_init(dev, fwnode, ops);
599
600         if (!ret)
601                 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
602
603         return ret;
604 }
605
606 static inline bool iort_iommu_driver_enabled(u8 type)
607 {
608         switch (type) {
609         case ACPI_IORT_NODE_SMMU_V3:
610                 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
611         case ACPI_IORT_NODE_SMMU:
612                 return IS_BUILTIN(CONFIG_ARM_SMMU);
613         default:
614                 pr_warn("IORT node type %u does not describe an SMMU\n", type);
615                 return false;
616         }
617 }
618
619 #ifdef CONFIG_IOMMU_API
620 static inline
621 const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec)
622 {
623         return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
624 }
625
626 static inline
627 int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev)
628 {
629         int err = 0;
630
631         if (!IS_ERR_OR_NULL(ops) && ops->add_device && dev->bus &&
632             !dev->iommu_group)
633                 err = ops->add_device(dev);
634
635         return err;
636 }
637 #else
638 static inline
639 const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec)
640 { return NULL; }
641 static inline
642 int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev)
643 { return 0; }
644 #endif
645
646 static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
647                                         struct acpi_iort_node *node,
648                                         u32 streamid)
649 {
650         const struct iommu_ops *ops = NULL;
651         int ret = -ENODEV;
652         struct fwnode_handle *iort_fwnode;
653
654         if (node) {
655                 iort_fwnode = iort_get_fwnode(node);
656                 if (!iort_fwnode)
657                         return NULL;
658
659                 ops = iommu_ops_from_fwnode(iort_fwnode);
660                 /*
661                  * If the ops look-up fails, this means that either
662                  * the SMMU drivers have not been probed yet or that
663                  * the SMMU drivers are not built in the kernel;
664                  * Depending on whether the SMMU drivers are built-in
665                  * in the kernel or not, defer the IOMMU configuration
666                  * or just abort it.
667                  */
668                 if (!ops)
669                         return iort_iommu_driver_enabled(node->type) ?
670                                ERR_PTR(-EPROBE_DEFER) : NULL;
671
672                 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
673         }
674
675         return ret ? NULL : ops;
676 }
677
678 /**
679  * iort_set_dma_mask - Set-up dma mask for a device.
680  *
681  * @dev: device to configure
682  */
683 void iort_set_dma_mask(struct device *dev)
684 {
685         /*
686          * Set default coherent_dma_mask to 32 bit.  Drivers are expected to
687          * setup the correct supported mask.
688          */
689         if (!dev->coherent_dma_mask)
690                 dev->coherent_dma_mask = DMA_BIT_MASK(32);
691
692         /*
693          * Set it to coherent_dma_mask by default if the architecture
694          * code has not set it.
695          */
696         if (!dev->dma_mask)
697                 dev->dma_mask = &dev->coherent_dma_mask;
698 }
699
700 /**
701  * iort_iommu_configure - Set-up IOMMU configuration for a device.
702  *
703  * @dev: device to configure
704  *
705  * Returns: iommu_ops pointer on configuration success
706  *          NULL on configuration failure
707  */
708 const struct iommu_ops *iort_iommu_configure(struct device *dev)
709 {
710         struct acpi_iort_node *node, *parent;
711         const struct iommu_ops *ops = NULL;
712         u32 streamid = 0;
713         int err;
714
715         /*
716          * If we already translated the fwspec there
717          * is nothing left to do, return the iommu_ops.
718          */
719         ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
720         if (ops)
721                 return ops;
722
723         if (dev_is_pci(dev)) {
724                 struct pci_bus *bus = to_pci_dev(dev)->bus;
725                 u32 rid;
726
727                 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
728                                        &rid);
729
730                 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
731                                       iort_match_node_callback, &bus->dev);
732                 if (!node)
733                         return NULL;
734
735                 parent = iort_node_map_id(node, rid, &streamid,
736                                           IORT_IOMMU_TYPE);
737
738                 ops = iort_iommu_xlate(dev, parent, streamid);
739
740         } else {
741                 int i = 0;
742
743                 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
744                                       iort_match_node_callback, dev);
745                 if (!node)
746                         return NULL;
747
748                 parent = iort_node_map_platform_id(node, &streamid,
749                                                    IORT_IOMMU_TYPE, i++);
750
751                 while (parent) {
752                         ops = iort_iommu_xlate(dev, parent, streamid);
753                         if (IS_ERR_OR_NULL(ops))
754                                 return ops;
755
756                         parent = iort_node_map_platform_id(node, &streamid,
757                                                            IORT_IOMMU_TYPE,
758                                                            i++);
759                 }
760         }
761
762         /*
763          * If we have reason to believe the IOMMU driver missed the initial
764          * add_device callback for dev, replay it to get things in order.
765          */
766         err = iort_add_device_replay(ops, dev);
767         if (err)
768                 ops = ERR_PTR(err);
769
770         /* Ignore all other errors apart from EPROBE_DEFER */
771         if (IS_ERR(ops) && (PTR_ERR(ops) != -EPROBE_DEFER)) {
772                 dev_dbg(dev, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops));
773                 ops = NULL;
774         }
775
776         return ops;
777 }
778
779 static void __init acpi_iort_register_irq(int hwirq, const char *name,
780                                           int trigger,
781                                           struct resource *res)
782 {
783         int irq = acpi_register_gsi(NULL, hwirq, trigger,
784                                     ACPI_ACTIVE_HIGH);
785
786         if (irq <= 0) {
787                 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
788                                                                       name);
789                 return;
790         }
791
792         res->start = irq;
793         res->end = irq;
794         res->flags = IORESOURCE_IRQ;
795         res->name = name;
796 }
797
798 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
799 {
800         struct acpi_iort_smmu_v3 *smmu;
801         /* Always present mem resource */
802         int num_res = 1;
803
804         /* Retrieve SMMUv3 specific data */
805         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
806
807         if (smmu->event_gsiv)
808                 num_res++;
809
810         if (smmu->pri_gsiv)
811                 num_res++;
812
813         if (smmu->gerr_gsiv)
814                 num_res++;
815
816         if (smmu->sync_gsiv)
817                 num_res++;
818
819         return num_res;
820 }
821
822 static void __init arm_smmu_v3_init_resources(struct resource *res,
823                                               struct acpi_iort_node *node)
824 {
825         struct acpi_iort_smmu_v3 *smmu;
826         int num_res = 0;
827
828         /* Retrieve SMMUv3 specific data */
829         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
830
831         res[num_res].start = smmu->base_address;
832         res[num_res].end = smmu->base_address + SZ_128K - 1;
833         res[num_res].flags = IORESOURCE_MEM;
834
835         num_res++;
836
837         if (smmu->event_gsiv)
838                 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
839                                        ACPI_EDGE_SENSITIVE,
840                                        &res[num_res++]);
841
842         if (smmu->pri_gsiv)
843                 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
844                                        ACPI_EDGE_SENSITIVE,
845                                        &res[num_res++]);
846
847         if (smmu->gerr_gsiv)
848                 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
849                                        ACPI_EDGE_SENSITIVE,
850                                        &res[num_res++]);
851
852         if (smmu->sync_gsiv)
853                 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
854                                        ACPI_EDGE_SENSITIVE,
855                                        &res[num_res++]);
856 }
857
858 static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
859 {
860         struct acpi_iort_smmu_v3 *smmu;
861
862         /* Retrieve SMMUv3 specific data */
863         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
864
865         return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
866 }
867
868 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
869 {
870         struct acpi_iort_smmu *smmu;
871
872         /* Retrieve SMMU specific data */
873         smmu = (struct acpi_iort_smmu *)node->node_data;
874
875         /*
876          * Only consider the global fault interrupt and ignore the
877          * configuration access interrupt.
878          *
879          * MMIO address and global fault interrupt resources are always
880          * present so add them to the context interrupt count as a static
881          * value.
882          */
883         return smmu->context_interrupt_count + 2;
884 }
885
886 static void __init arm_smmu_init_resources(struct resource *res,
887                                            struct acpi_iort_node *node)
888 {
889         struct acpi_iort_smmu *smmu;
890         int i, hw_irq, trigger, num_res = 0;
891         u64 *ctx_irq, *glb_irq;
892
893         /* Retrieve SMMU specific data */
894         smmu = (struct acpi_iort_smmu *)node->node_data;
895
896         res[num_res].start = smmu->base_address;
897         res[num_res].end = smmu->base_address + smmu->span - 1;
898         res[num_res].flags = IORESOURCE_MEM;
899         num_res++;
900
901         glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
902         /* Global IRQs */
903         hw_irq = IORT_IRQ_MASK(glb_irq[0]);
904         trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
905
906         acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
907                                      &res[num_res++]);
908
909         /* Context IRQs */
910         ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
911         for (i = 0; i < smmu->context_interrupt_count; i++) {
912                 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
913                 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
914
915                 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
916                                        &res[num_res++]);
917         }
918 }
919
920 static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
921 {
922         struct acpi_iort_smmu *smmu;
923
924         /* Retrieve SMMU specific data */
925         smmu = (struct acpi_iort_smmu *)node->node_data;
926
927         return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
928 }
929
930 struct iort_iommu_config {
931         const char *name;
932         int (*iommu_init)(struct acpi_iort_node *node);
933         bool (*iommu_is_coherent)(struct acpi_iort_node *node);
934         int (*iommu_count_resources)(struct acpi_iort_node *node);
935         void (*iommu_init_resources)(struct resource *res,
936                                      struct acpi_iort_node *node);
937 };
938
939 static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
940         .name = "arm-smmu-v3",
941         .iommu_is_coherent = arm_smmu_v3_is_coherent,
942         .iommu_count_resources = arm_smmu_v3_count_resources,
943         .iommu_init_resources = arm_smmu_v3_init_resources
944 };
945
946 static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
947         .name = "arm-smmu",
948         .iommu_is_coherent = arm_smmu_is_coherent,
949         .iommu_count_resources = arm_smmu_count_resources,
950         .iommu_init_resources = arm_smmu_init_resources
951 };
952
953 static __init
954 const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
955 {
956         switch (node->type) {
957         case ACPI_IORT_NODE_SMMU_V3:
958                 return &iort_arm_smmu_v3_cfg;
959         case ACPI_IORT_NODE_SMMU:
960                 return &iort_arm_smmu_cfg;
961         default:
962                 return NULL;
963         }
964 }
965
966 /**
967  * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
968  * @node: Pointer to SMMU ACPI IORT node
969  *
970  * Returns: 0 on success, <0 failure
971  */
972 static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
973 {
974         struct fwnode_handle *fwnode;
975         struct platform_device *pdev;
976         struct resource *r;
977         enum dev_dma_attr attr;
978         int ret, count;
979         const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
980
981         if (!ops)
982                 return -ENODEV;
983
984         pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
985         if (!pdev)
986                 return -ENOMEM;
987
988         count = ops->iommu_count_resources(node);
989
990         r = kcalloc(count, sizeof(*r), GFP_KERNEL);
991         if (!r) {
992                 ret = -ENOMEM;
993                 goto dev_put;
994         }
995
996         ops->iommu_init_resources(r, node);
997
998         ret = platform_device_add_resources(pdev, r, count);
999         /*
1000          * Resources are duplicated in platform_device_add_resources,
1001          * free their allocated memory
1002          */
1003         kfree(r);
1004
1005         if (ret)
1006                 goto dev_put;
1007
1008         /*
1009          * Add a copy of IORT node pointer to platform_data to
1010          * be used to retrieve IORT data information.
1011          */
1012         ret = platform_device_add_data(pdev, &node, sizeof(node));
1013         if (ret)
1014                 goto dev_put;
1015
1016         /*
1017          * We expect the dma masks to be equivalent for
1018          * all SMMUs set-ups
1019          */
1020         pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1021
1022         fwnode = iort_get_fwnode(node);
1023
1024         if (!fwnode) {
1025                 ret = -ENODEV;
1026                 goto dev_put;
1027         }
1028
1029         pdev->dev.fwnode = fwnode;
1030
1031         attr = ops->iommu_is_coherent(node) ?
1032                              DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1033
1034         /* Configure DMA for the page table walker */
1035         acpi_dma_configure(&pdev->dev, attr);
1036
1037         ret = platform_device_add(pdev);
1038         if (ret)
1039                 goto dma_deconfigure;
1040
1041         return 0;
1042
1043 dma_deconfigure:
1044         acpi_dma_deconfigure(&pdev->dev);
1045 dev_put:
1046         platform_device_put(pdev);
1047
1048         return ret;
1049 }
1050
1051 static void __init iort_init_platform_devices(void)
1052 {
1053         struct acpi_iort_node *iort_node, *iort_end;
1054         struct acpi_table_iort *iort;
1055         struct fwnode_handle *fwnode;
1056         int i, ret;
1057
1058         /*
1059          * iort_table and iort both point to the start of IORT table, but
1060          * have different struct types
1061          */
1062         iort = (struct acpi_table_iort *)iort_table;
1063
1064         /* Get the first IORT node */
1065         iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1066                                  iort->node_offset);
1067         iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1068                                 iort_table->length);
1069
1070         for (i = 0; i < iort->node_count; i++) {
1071                 if (iort_node >= iort_end) {
1072                         pr_err("iort node pointer overflows, bad table\n");
1073                         return;
1074                 }
1075
1076                 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
1077                         (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
1078
1079                         fwnode = acpi_alloc_fwnode_static();
1080                         if (!fwnode)
1081                                 return;
1082
1083                         iort_set_fwnode(iort_node, fwnode);
1084
1085                         ret = iort_add_smmu_platform_device(iort_node);
1086                         if (ret) {
1087                                 iort_delete_fwnode(iort_node);
1088                                 acpi_free_fwnode_static(fwnode);
1089                                 return;
1090                         }
1091                 }
1092
1093                 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1094                                          iort_node->length);
1095         }
1096 }
1097
1098 void __init acpi_iort_init(void)
1099 {
1100         acpi_status status;
1101
1102         status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1103         if (ACPI_FAILURE(status)) {
1104                 if (status != AE_NOT_FOUND) {
1105                         const char *msg = acpi_format_exception(status);
1106
1107                         pr_err("Failed to get table, %s\n", msg);
1108                 }
1109
1110                 return;
1111         }
1112
1113         iort_init_platform_devices();
1114 }