]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/acpi/arm64/iort.c
47bace8eafb657129466bcab600785709630d85e
[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
238 iort_match_type_callback(struct acpi_iort_node *node, void *context)
239 {
240         return AE_OK;
241 }
242
243 bool iort_node_match(u8 type)
244 {
245         struct acpi_iort_node *node;
246
247         node = iort_scan_node(type, iort_match_type_callback, NULL);
248
249         return node != NULL;
250 }
251
252 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
253                                             void *context)
254 {
255         struct device *dev = context;
256         acpi_status status;
257
258         if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
259                 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
260                 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
261                 struct acpi_iort_named_component *ncomp;
262
263                 if (!adev) {
264                         status = AE_NOT_FOUND;
265                         goto out;
266                 }
267
268                 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
269                 if (ACPI_FAILURE(status)) {
270                         dev_warn(dev, "Can't get device full path name\n");
271                         goto out;
272                 }
273
274                 ncomp = (struct acpi_iort_named_component *)node->node_data;
275                 status = !strcmp(ncomp->device_name, buf.pointer) ?
276                                                         AE_OK : AE_NOT_FOUND;
277                 acpi_os_free(buf.pointer);
278         } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
279                 struct acpi_iort_root_complex *pci_rc;
280                 struct pci_bus *bus;
281
282                 bus = to_pci_bus(dev);
283                 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
284
285                 /*
286                  * It is assumed that PCI segment numbers maps one-to-one
287                  * with root complexes. Each segment number can represent only
288                  * one root complex.
289                  */
290                 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
291                                                         AE_OK : AE_NOT_FOUND;
292         } else {
293                 status = AE_NOT_FOUND;
294         }
295 out:
296         return status;
297 }
298
299 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
300                        u32 *rid_out)
301 {
302         /* Single mapping does not care for input id */
303         if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
304                 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
305                     type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
306                         *rid_out = map->output_base;
307                         return 0;
308                 }
309
310                 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
311                         map, type);
312                 return -ENXIO;
313         }
314
315         if (rid_in < map->input_base ||
316             (rid_in >= map->input_base + map->id_count))
317                 return -ENXIO;
318
319         *rid_out = map->output_base + (rid_in - map->input_base);
320         return 0;
321 }
322
323 static
324 struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
325                                         u32 *id_out, u8 type_mask,
326                                         int index)
327 {
328         struct acpi_iort_node *parent;
329         struct acpi_iort_id_mapping *map;
330
331         if (!node->mapping_offset || !node->mapping_count ||
332                                      index >= node->mapping_count)
333                 return NULL;
334
335         map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
336                            node->mapping_offset);
337
338         /* Firmware bug! */
339         if (!map->output_reference) {
340                 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
341                        node, node->type);
342                 return NULL;
343         }
344
345         parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
346                                map->output_reference);
347
348         if (!(IORT_TYPE_MASK(parent->type) & type_mask))
349                 return NULL;
350
351         if (map[index].flags & ACPI_IORT_ID_SINGLE_MAPPING) {
352                 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
353                     node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
354                         *id_out = map[index].output_base;
355                         return parent;
356                 }
357         }
358
359         return NULL;
360 }
361
362 static struct acpi_iort_node *iort_node_map_rid(struct acpi_iort_node *node,
363                                                 u32 rid_in, u32 *rid_out,
364                                                 u8 type_mask)
365 {
366         u32 rid = rid_in;
367
368         /* Parse the ID mapping tree to find specified node type */
369         while (node) {
370                 struct acpi_iort_id_mapping *map;
371                 int i;
372
373                 if (IORT_TYPE_MASK(node->type) & type_mask) {
374                         if (rid_out)
375                                 *rid_out = rid;
376                         return node;
377                 }
378
379                 if (!node->mapping_offset || !node->mapping_count)
380                         goto fail_map;
381
382                 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
383                                    node->mapping_offset);
384
385                 /* Firmware bug! */
386                 if (!map->output_reference) {
387                         pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
388                                node, node->type);
389                         goto fail_map;
390                 }
391
392                 /* Do the RID translation */
393                 for (i = 0; i < node->mapping_count; i++, map++) {
394                         if (!iort_id_map(map, node->type, rid, &rid))
395                                 break;
396                 }
397
398                 if (i == node->mapping_count)
399                         goto fail_map;
400
401                 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
402                                     map->output_reference);
403         }
404
405 fail_map:
406         /* Map input RID to output RID unchanged on mapping failure*/
407         if (rid_out)
408                 *rid_out = rid_in;
409
410         return NULL;
411 }
412
413 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
414 {
415         struct pci_bus *pbus;
416
417         if (!dev_is_pci(dev))
418                 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
419                                       iort_match_node_callback, dev);
420
421         /* Find a PCI root bus */
422         pbus = to_pci_dev(dev)->bus;
423         while (!pci_is_root_bus(pbus))
424                 pbus = pbus->parent;
425
426         return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
427                               iort_match_node_callback, &pbus->dev);
428 }
429
430 /**
431  * iort_msi_map_rid() - Map a MSI requester ID for a device
432  * @dev: The device for which the mapping is to be done.
433  * @req_id: The device requester ID.
434  *
435  * Returns: mapped MSI RID on success, input requester ID otherwise
436  */
437 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
438 {
439         struct acpi_iort_node *node;
440         u32 dev_id;
441
442         node = iort_find_dev_node(dev);
443         if (!node)
444                 return req_id;
445
446         iort_node_map_rid(node, req_id, &dev_id, IORT_MSI_TYPE);
447         return dev_id;
448 }
449
450 /**
451  * iort_dev_find_its_id() - Find the ITS identifier for a device
452  * @dev: The device.
453  * @idx: Index of the ITS identifier list.
454  * @its_id: ITS identifier.
455  *
456  * Returns: 0 on success, appropriate error value otherwise
457  */
458 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
459                                 unsigned int idx, int *its_id)
460 {
461         struct acpi_iort_its_group *its;
462         struct acpi_iort_node *node;
463
464         node = iort_find_dev_node(dev);
465         if (!node)
466                 return -ENXIO;
467
468         node = iort_node_map_rid(node, req_id, NULL, IORT_MSI_TYPE);
469         if (!node)
470                 return -ENXIO;
471
472         /* Move to ITS specific data */
473         its = (struct acpi_iort_its_group *)node->node_data;
474         if (idx > its->its_count) {
475                 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
476                         idx, its->its_count);
477                 return -ENXIO;
478         }
479
480         *its_id = its->identifiers[idx];
481         return 0;
482 }
483
484 /**
485  * iort_get_device_domain() - Find MSI domain related to a device
486  * @dev: The device.
487  * @req_id: Requester ID for the device.
488  *
489  * Returns: the MSI domain for this device, NULL otherwise
490  */
491 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
492 {
493         struct fwnode_handle *handle;
494         int its_id;
495
496         if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
497                 return NULL;
498
499         handle = iort_find_domain_token(its_id);
500         if (!handle)
501                 return NULL;
502
503         return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
504 }
505
506 static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
507 {
508         u32 *rid = data;
509
510         *rid = alias;
511         return 0;
512 }
513
514 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
515                                struct fwnode_handle *fwnode,
516                                const struct iommu_ops *ops)
517 {
518         int ret = iommu_fwspec_init(dev, fwnode, ops);
519
520         if (!ret)
521                 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
522
523         return ret;
524 }
525
526 static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
527                                         struct acpi_iort_node *node,
528                                         u32 streamid)
529 {
530         const struct iommu_ops *ops = NULL;
531         int ret = -ENODEV;
532         struct fwnode_handle *iort_fwnode;
533
534         if (node) {
535                 iort_fwnode = iort_get_fwnode(node);
536                 if (!iort_fwnode)
537                         return NULL;
538
539                 ops = iommu_get_instance(iort_fwnode);
540                 if (!ops)
541                         return NULL;
542
543                 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
544         }
545
546         return ret ? NULL : ops;
547 }
548
549 /**
550  * iort_iommu_configure - Set-up IOMMU configuration for a device.
551  *
552  * @dev: device to configure
553  *
554  * Returns: iommu_ops pointer on configuration success
555  *          NULL on configuration failure
556  */
557 const struct iommu_ops *iort_iommu_configure(struct device *dev)
558 {
559         struct acpi_iort_node *node, *parent;
560         const struct iommu_ops *ops = NULL;
561         u32 streamid = 0;
562
563         if (dev_is_pci(dev)) {
564                 struct pci_bus *bus = to_pci_dev(dev)->bus;
565                 u32 rid;
566
567                 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
568                                        &rid);
569
570                 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
571                                       iort_match_node_callback, &bus->dev);
572                 if (!node)
573                         return NULL;
574
575                 parent = iort_node_map_rid(node, rid, &streamid,
576                                            IORT_IOMMU_TYPE);
577
578                 ops = iort_iommu_xlate(dev, parent, streamid);
579
580         } else {
581                 int i = 0;
582
583                 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
584                                       iort_match_node_callback, dev);
585                 if (!node)
586                         return NULL;
587
588                 parent = iort_node_get_id(node, &streamid,
589                                           IORT_IOMMU_TYPE, i++);
590
591                 while (parent) {
592                         ops = iort_iommu_xlate(dev, parent, streamid);
593
594                         parent = iort_node_get_id(node, &streamid,
595                                                   IORT_IOMMU_TYPE, i++);
596                 }
597         }
598
599         return ops;
600 }
601
602 static void __init acpi_iort_register_irq(int hwirq, const char *name,
603                                           int trigger,
604                                           struct resource *res)
605 {
606         int irq = acpi_register_gsi(NULL, hwirq, trigger,
607                                     ACPI_ACTIVE_HIGH);
608
609         if (irq <= 0) {
610                 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
611                                                                       name);
612                 return;
613         }
614
615         res->start = irq;
616         res->end = irq;
617         res->flags = IORESOURCE_IRQ;
618         res->name = name;
619 }
620
621 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
622 {
623         struct acpi_iort_smmu_v3 *smmu;
624         /* Always present mem resource */
625         int num_res = 1;
626
627         /* Retrieve SMMUv3 specific data */
628         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
629
630         if (smmu->event_gsiv)
631                 num_res++;
632
633         if (smmu->pri_gsiv)
634                 num_res++;
635
636         if (smmu->gerr_gsiv)
637                 num_res++;
638
639         if (smmu->sync_gsiv)
640                 num_res++;
641
642         return num_res;
643 }
644
645 static void __init arm_smmu_v3_init_resources(struct resource *res,
646                                               struct acpi_iort_node *node)
647 {
648         struct acpi_iort_smmu_v3 *smmu;
649         int num_res = 0;
650
651         /* Retrieve SMMUv3 specific data */
652         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
653
654         res[num_res].start = smmu->base_address;
655         res[num_res].end = smmu->base_address + SZ_128K - 1;
656         res[num_res].flags = IORESOURCE_MEM;
657
658         num_res++;
659
660         if (smmu->event_gsiv)
661                 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
662                                        ACPI_EDGE_SENSITIVE,
663                                        &res[num_res++]);
664
665         if (smmu->pri_gsiv)
666                 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
667                                        ACPI_EDGE_SENSITIVE,
668                                        &res[num_res++]);
669
670         if (smmu->gerr_gsiv)
671                 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
672                                        ACPI_EDGE_SENSITIVE,
673                                        &res[num_res++]);
674
675         if (smmu->sync_gsiv)
676                 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
677                                        ACPI_EDGE_SENSITIVE,
678                                        &res[num_res++]);
679 }
680
681 static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
682 {
683         struct acpi_iort_smmu_v3 *smmu;
684
685         /* Retrieve SMMUv3 specific data */
686         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
687
688         return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
689 }
690
691 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
692 {
693         struct acpi_iort_smmu *smmu;
694
695         /* Retrieve SMMU specific data */
696         smmu = (struct acpi_iort_smmu *)node->node_data;
697
698         /*
699          * Only consider the global fault interrupt and ignore the
700          * configuration access interrupt.
701          *
702          * MMIO address and global fault interrupt resources are always
703          * present so add them to the context interrupt count as a static
704          * value.
705          */
706         return smmu->context_interrupt_count + 2;
707 }
708
709 static void __init arm_smmu_init_resources(struct resource *res,
710                                            struct acpi_iort_node *node)
711 {
712         struct acpi_iort_smmu *smmu;
713         int i, hw_irq, trigger, num_res = 0;
714         u64 *ctx_irq, *glb_irq;
715
716         /* Retrieve SMMU specific data */
717         smmu = (struct acpi_iort_smmu *)node->node_data;
718
719         res[num_res].start = smmu->base_address;
720         res[num_res].end = smmu->base_address + smmu->span - 1;
721         res[num_res].flags = IORESOURCE_MEM;
722         num_res++;
723
724         glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
725         /* Global IRQs */
726         hw_irq = IORT_IRQ_MASK(glb_irq[0]);
727         trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
728
729         acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
730                                      &res[num_res++]);
731
732         /* Context IRQs */
733         ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
734         for (i = 0; i < smmu->context_interrupt_count; i++) {
735                 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
736                 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
737
738                 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
739                                        &res[num_res++]);
740         }
741 }
742
743 static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
744 {
745         struct acpi_iort_smmu *smmu;
746
747         /* Retrieve SMMU specific data */
748         smmu = (struct acpi_iort_smmu *)node->node_data;
749
750         return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
751 }
752
753 struct iort_iommu_config {
754         const char *name;
755         int (*iommu_init)(struct acpi_iort_node *node);
756         bool (*iommu_is_coherent)(struct acpi_iort_node *node);
757         int (*iommu_count_resources)(struct acpi_iort_node *node);
758         void (*iommu_init_resources)(struct resource *res,
759                                      struct acpi_iort_node *node);
760 };
761
762 static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
763         .name = "arm-smmu-v3",
764         .iommu_is_coherent = arm_smmu_v3_is_coherent,
765         .iommu_count_resources = arm_smmu_v3_count_resources,
766         .iommu_init_resources = arm_smmu_v3_init_resources
767 };
768
769 static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
770         .name = "arm-smmu",
771         .iommu_is_coherent = arm_smmu_is_coherent,
772         .iommu_count_resources = arm_smmu_count_resources,
773         .iommu_init_resources = arm_smmu_init_resources
774 };
775
776 static __init
777 const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
778 {
779         switch (node->type) {
780         case ACPI_IORT_NODE_SMMU_V3:
781                 return &iort_arm_smmu_v3_cfg;
782         case ACPI_IORT_NODE_SMMU:
783                 return &iort_arm_smmu_cfg;
784         default:
785                 return NULL;
786         }
787 }
788
789 /**
790  * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
791  * @node: Pointer to SMMU ACPI IORT node
792  *
793  * Returns: 0 on success, <0 failure
794  */
795 static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
796 {
797         struct fwnode_handle *fwnode;
798         struct platform_device *pdev;
799         struct resource *r;
800         enum dev_dma_attr attr;
801         int ret, count;
802         const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
803
804         if (!ops)
805                 return -ENODEV;
806
807         pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
808         if (!pdev)
809                 return PTR_ERR(pdev);
810
811         count = ops->iommu_count_resources(node);
812
813         r = kcalloc(count, sizeof(*r), GFP_KERNEL);
814         if (!r) {
815                 ret = -ENOMEM;
816                 goto dev_put;
817         }
818
819         ops->iommu_init_resources(r, node);
820
821         ret = platform_device_add_resources(pdev, r, count);
822         /*
823          * Resources are duplicated in platform_device_add_resources,
824          * free their allocated memory
825          */
826         kfree(r);
827
828         if (ret)
829                 goto dev_put;
830
831         /*
832          * Add a copy of IORT node pointer to platform_data to
833          * be used to retrieve IORT data information.
834          */
835         ret = platform_device_add_data(pdev, &node, sizeof(node));
836         if (ret)
837                 goto dev_put;
838
839         /*
840          * We expect the dma masks to be equivalent for
841          * all SMMUs set-ups
842          */
843         pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
844
845         fwnode = iort_get_fwnode(node);
846
847         if (!fwnode) {
848                 ret = -ENODEV;
849                 goto dev_put;
850         }
851
852         pdev->dev.fwnode = fwnode;
853
854         attr = ops->iommu_is_coherent(node) ?
855                              DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
856
857         /* Configure DMA for the page table walker */
858         acpi_dma_configure(&pdev->dev, attr);
859
860         ret = platform_device_add(pdev);
861         if (ret)
862                 goto dma_deconfigure;
863
864         return 0;
865
866 dma_deconfigure:
867         acpi_dma_deconfigure(&pdev->dev);
868 dev_put:
869         platform_device_put(pdev);
870
871         return ret;
872 }
873
874 static void __init iort_init_platform_devices(void)
875 {
876         struct acpi_iort_node *iort_node, *iort_end;
877         struct acpi_table_iort *iort;
878         struct fwnode_handle *fwnode;
879         int i, ret;
880
881         /*
882          * iort_table and iort both point to the start of IORT table, but
883          * have different struct types
884          */
885         iort = (struct acpi_table_iort *)iort_table;
886
887         /* Get the first IORT node */
888         iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
889                                  iort->node_offset);
890         iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
891                                 iort_table->length);
892
893         for (i = 0; i < iort->node_count; i++) {
894                 if (iort_node >= iort_end) {
895                         pr_err("iort node pointer overflows, bad table\n");
896                         return;
897                 }
898
899                 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
900                         (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
901
902                         fwnode = acpi_alloc_fwnode_static();
903                         if (!fwnode)
904                                 return;
905
906                         iort_set_fwnode(iort_node, fwnode);
907
908                         ret = iort_add_smmu_platform_device(iort_node);
909                         if (ret) {
910                                 iort_delete_fwnode(iort_node);
911                                 acpi_free_fwnode_static(fwnode);
912                                 return;
913                         }
914                 }
915
916                 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
917                                          iort_node->length);
918         }
919 }
920
921 void __init acpi_iort_init(void)
922 {
923         acpi_status status;
924
925         status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
926         if (ACPI_FAILURE(status)) {
927                 if (status != AE_NOT_FOUND) {
928                         const char *msg = acpi_format_exception(status);
929
930                         pr_err("Failed to get table, %s\n", msg);
931                 }
932
933                 return;
934         }
935
936         iort_init_platform_devices();
937
938         acpi_probe_device_table(iort);
939 }