]> Git Repo - linux.git/blob - drivers/acpi/arm64/iort.c
nfsd4: a client's own opens needn't prevent delegations
[linux.git] / drivers / acpi / arm64 / iort.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2016, Semihalf
4  *      Author: Tomasz Nowicki <[email protected]>
5  *
6  * This file implements early detection/parsing of I/O mapping
7  * reported to OS through firmware via I/O Remapping Table (IORT)
8  * IORT document number: ARM DEN 0049A
9  */
10
11 #define pr_fmt(fmt)     "ACPI: IORT: " fmt
12
13 #include <linux/acpi_iort.h>
14 #include <linux/bitfield.h>
15 #include <linux/iommu.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/pci.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21
22 #define IORT_TYPE_MASK(type)    (1 << (type))
23 #define IORT_MSI_TYPE           (1 << ACPI_IORT_NODE_ITS_GROUP)
24 #define IORT_IOMMU_TYPE         ((1 << ACPI_IORT_NODE_SMMU) |   \
25                                 (1 << ACPI_IORT_NODE_SMMU_V3))
26
27 struct iort_its_msi_chip {
28         struct list_head        list;
29         struct fwnode_handle    *fw_node;
30         phys_addr_t             base_addr;
31         u32                     translation_id;
32 };
33
34 struct iort_fwnode {
35         struct list_head list;
36         struct acpi_iort_node *iort_node;
37         struct fwnode_handle *fwnode;
38 };
39 static LIST_HEAD(iort_fwnode_list);
40 static DEFINE_SPINLOCK(iort_fwnode_lock);
41
42 /**
43  * iort_set_fwnode() - Create iort_fwnode and use it to register
44  *                     iommu data in the iort_fwnode_list
45  *
46  * @node: IORT table node associated with the IOMMU
47  * @fwnode: fwnode associated with the IORT node
48  *
49  * Returns: 0 on success
50  *          <0 on failure
51  */
52 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
53                                   struct fwnode_handle *fwnode)
54 {
55         struct iort_fwnode *np;
56
57         np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
58
59         if (WARN_ON(!np))
60                 return -ENOMEM;
61
62         INIT_LIST_HEAD(&np->list);
63         np->iort_node = iort_node;
64         np->fwnode = fwnode;
65
66         spin_lock(&iort_fwnode_lock);
67         list_add_tail(&np->list, &iort_fwnode_list);
68         spin_unlock(&iort_fwnode_lock);
69
70         return 0;
71 }
72
73 /**
74  * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
75  *
76  * @node: IORT table node to be looked-up
77  *
78  * Returns: fwnode_handle pointer on success, NULL on failure
79  */
80 static inline struct fwnode_handle *iort_get_fwnode(
81                         struct acpi_iort_node *node)
82 {
83         struct iort_fwnode *curr;
84         struct fwnode_handle *fwnode = NULL;
85
86         spin_lock(&iort_fwnode_lock);
87         list_for_each_entry(curr, &iort_fwnode_list, list) {
88                 if (curr->iort_node == node) {
89                         fwnode = curr->fwnode;
90                         break;
91                 }
92         }
93         spin_unlock(&iort_fwnode_lock);
94
95         return fwnode;
96 }
97
98 /**
99  * iort_delete_fwnode() - Delete fwnode associated with an IORT node
100  *
101  * @node: IORT table node associated with fwnode to delete
102  */
103 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
104 {
105         struct iort_fwnode *curr, *tmp;
106
107         spin_lock(&iort_fwnode_lock);
108         list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
109                 if (curr->iort_node == node) {
110                         list_del(&curr->list);
111                         kfree(curr);
112                         break;
113                 }
114         }
115         spin_unlock(&iort_fwnode_lock);
116 }
117
118 /**
119  * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
120  *
121  * @fwnode: fwnode associated with device to be looked-up
122  *
123  * Returns: iort_node pointer on success, NULL on failure
124  */
125 static inline struct acpi_iort_node *iort_get_iort_node(
126                         struct fwnode_handle *fwnode)
127 {
128         struct iort_fwnode *curr;
129         struct acpi_iort_node *iort_node = NULL;
130
131         spin_lock(&iort_fwnode_lock);
132         list_for_each_entry(curr, &iort_fwnode_list, list) {
133                 if (curr->fwnode == fwnode) {
134                         iort_node = curr->iort_node;
135                         break;
136                 }
137         }
138         spin_unlock(&iort_fwnode_lock);
139
140         return iort_node;
141 }
142
143 typedef acpi_status (*iort_find_node_callback)
144         (struct acpi_iort_node *node, void *context);
145
146 /* Root pointer to the mapped IORT table */
147 static struct acpi_table_header *iort_table;
148
149 static LIST_HEAD(iort_msi_chip_list);
150 static DEFINE_SPINLOCK(iort_msi_chip_lock);
151
152 /**
153  * iort_register_domain_token() - register domain token along with related
154  * ITS ID and base address to the list from where we can get it back later on.
155  * @trans_id: ITS ID.
156  * @base: ITS base address.
157  * @fw_node: Domain token.
158  *
159  * Returns: 0 on success, -ENOMEM if no memory when allocating list element
160  */
161 int iort_register_domain_token(int trans_id, phys_addr_t base,
162                                struct fwnode_handle *fw_node)
163 {
164         struct iort_its_msi_chip *its_msi_chip;
165
166         its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
167         if (!its_msi_chip)
168                 return -ENOMEM;
169
170         its_msi_chip->fw_node = fw_node;
171         its_msi_chip->translation_id = trans_id;
172         its_msi_chip->base_addr = base;
173
174         spin_lock(&iort_msi_chip_lock);
175         list_add(&its_msi_chip->list, &iort_msi_chip_list);
176         spin_unlock(&iort_msi_chip_lock);
177
178         return 0;
179 }
180
181 /**
182  * iort_deregister_domain_token() - Deregister domain token based on ITS ID
183  * @trans_id: ITS ID.
184  *
185  * Returns: none.
186  */
187 void iort_deregister_domain_token(int trans_id)
188 {
189         struct iort_its_msi_chip *its_msi_chip, *t;
190
191         spin_lock(&iort_msi_chip_lock);
192         list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
193                 if (its_msi_chip->translation_id == trans_id) {
194                         list_del(&its_msi_chip->list);
195                         kfree(its_msi_chip);
196                         break;
197                 }
198         }
199         spin_unlock(&iort_msi_chip_lock);
200 }
201
202 /**
203  * iort_find_domain_token() - Find domain token based on given ITS ID
204  * @trans_id: ITS ID.
205  *
206  * Returns: domain token when find on the list, NULL otherwise
207  */
208 struct fwnode_handle *iort_find_domain_token(int trans_id)
209 {
210         struct fwnode_handle *fw_node = NULL;
211         struct iort_its_msi_chip *its_msi_chip;
212
213         spin_lock(&iort_msi_chip_lock);
214         list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
215                 if (its_msi_chip->translation_id == trans_id) {
216                         fw_node = its_msi_chip->fw_node;
217                         break;
218                 }
219         }
220         spin_unlock(&iort_msi_chip_lock);
221
222         return fw_node;
223 }
224
225 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
226                                              iort_find_node_callback callback,
227                                              void *context)
228 {
229         struct acpi_iort_node *iort_node, *iort_end;
230         struct acpi_table_iort *iort;
231         int i;
232
233         if (!iort_table)
234                 return NULL;
235
236         /* Get the first IORT node */
237         iort = (struct acpi_table_iort *)iort_table;
238         iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
239                                  iort->node_offset);
240         iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
241                                 iort_table->length);
242
243         for (i = 0; i < iort->node_count; i++) {
244                 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
245                                "IORT node pointer overflows, bad table!\n"))
246                         return NULL;
247
248                 if (iort_node->type == type &&
249                     ACPI_SUCCESS(callback(iort_node, context)))
250                         return iort_node;
251
252                 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
253                                          iort_node->length);
254         }
255
256         return NULL;
257 }
258
259 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
260                                             void *context)
261 {
262         struct device *dev = context;
263         acpi_status status = AE_NOT_FOUND;
264
265         if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
266                 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
267                 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
268                 struct acpi_iort_named_component *ncomp;
269
270                 if (!adev)
271                         goto out;
272
273                 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
274                 if (ACPI_FAILURE(status)) {
275                         dev_warn(dev, "Can't get device full path name\n");
276                         goto out;
277                 }
278
279                 ncomp = (struct acpi_iort_named_component *)node->node_data;
280                 status = !strcmp(ncomp->device_name, buf.pointer) ?
281                                                         AE_OK : AE_NOT_FOUND;
282                 acpi_os_free(buf.pointer);
283         } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
284                 struct acpi_iort_root_complex *pci_rc;
285                 struct pci_bus *bus;
286
287                 bus = to_pci_bus(dev);
288                 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
289
290                 /*
291                  * It is assumed that PCI segment numbers maps one-to-one
292                  * with root complexes. Each segment number can represent only
293                  * one root complex.
294                  */
295                 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
296                                                         AE_OK : AE_NOT_FOUND;
297         }
298 out:
299         return status;
300 }
301
302 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
303                        u32 *rid_out, bool check_overlap)
304 {
305         /* Single mapping does not care for input id */
306         if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
307                 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
308                     type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
309                         *rid_out = map->output_base;
310                         return 0;
311                 }
312
313                 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
314                         map, type);
315                 return -ENXIO;
316         }
317
318         if (rid_in < map->input_base ||
319             (rid_in > map->input_base + map->id_count))
320                 return -ENXIO;
321
322         if (check_overlap) {
323                 /*
324                  * We already found a mapping for this input ID at the end of
325                  * another region. If it coincides with the start of this
326                  * region, we assume the prior match was due to the off-by-1
327                  * issue mentioned below, and allow it to be superseded.
328                  * Otherwise, things are *really* broken, and we just disregard
329                  * duplicate matches entirely to retain compatibility.
330                  */
331                 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
332                        map, rid_in);
333                 if (rid_in != map->input_base)
334                         return -ENXIO;
335
336                 pr_err(FW_BUG "applying workaround.\n");
337         }
338
339         *rid_out = map->output_base + (rid_in - map->input_base);
340
341         /*
342          * Due to confusion regarding the meaning of the id_count field (which
343          * carries the number of IDs *minus 1*), we may have to disregard this
344          * match if it is at the end of the range, and overlaps with the start
345          * of another one.
346          */
347         if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
348                 return -EAGAIN;
349         return 0;
350 }
351
352 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
353                                                u32 *id_out, int index)
354 {
355         struct acpi_iort_node *parent;
356         struct acpi_iort_id_mapping *map;
357
358         if (!node->mapping_offset || !node->mapping_count ||
359                                      index >= node->mapping_count)
360                 return NULL;
361
362         map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
363                            node->mapping_offset + index * sizeof(*map));
364
365         /* Firmware bug! */
366         if (!map->output_reference) {
367                 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
368                        node, node->type);
369                 return NULL;
370         }
371
372         parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
373                                map->output_reference);
374
375         if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
376                 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
377                     node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
378                     node->type == ACPI_IORT_NODE_SMMU_V3 ||
379                     node->type == ACPI_IORT_NODE_PMCG) {
380                         *id_out = map->output_base;
381                         return parent;
382                 }
383         }
384
385         return NULL;
386 }
387
388 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
389 {
390         struct acpi_iort_smmu_v3 *smmu;
391         struct acpi_iort_pmcg *pmcg;
392
393         switch (node->type) {
394         case ACPI_IORT_NODE_SMMU_V3:
395                 /*
396                  * SMMUv3 dev ID mapping index was introduced in revision 1
397                  * table, not available in revision 0
398                  */
399                 if (node->revision < 1)
400                         return -EINVAL;
401
402                 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
403                 /*
404                  * ID mapping index is only ignored if all interrupts are
405                  * GSIV based
406                  */
407                 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
408                     && smmu->sync_gsiv)
409                         return -EINVAL;
410
411                 if (smmu->id_mapping_index >= node->mapping_count) {
412                         pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
413                                node, node->type);
414                         return -EINVAL;
415                 }
416
417                 return smmu->id_mapping_index;
418         case ACPI_IORT_NODE_PMCG:
419                 pmcg = (struct acpi_iort_pmcg *)node->node_data;
420                 if (pmcg->overflow_gsiv || node->mapping_count == 0)
421                         return -EINVAL;
422
423                 return 0;
424         default:
425                 return -EINVAL;
426         }
427 }
428
429 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
430                                                u32 id_in, u32 *id_out,
431                                                u8 type_mask)
432 {
433         u32 id = id_in;
434
435         /* Parse the ID mapping tree to find specified node type */
436         while (node) {
437                 struct acpi_iort_id_mapping *map;
438                 int i, index, rc = 0;
439                 u32 out_ref = 0, map_id = id;
440
441                 if (IORT_TYPE_MASK(node->type) & type_mask) {
442                         if (id_out)
443                                 *id_out = id;
444                         return node;
445                 }
446
447                 if (!node->mapping_offset || !node->mapping_count)
448                         goto fail_map;
449
450                 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
451                                    node->mapping_offset);
452
453                 /* Firmware bug! */
454                 if (!map->output_reference) {
455                         pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
456                                node, node->type);
457                         goto fail_map;
458                 }
459
460                 /*
461                  * Get the special ID mapping index (if any) and skip its
462                  * associated ID map to prevent erroneous multi-stage
463                  * IORT ID translations.
464                  */
465                 index = iort_get_id_mapping_index(node);
466
467                 /* Do the ID translation */
468                 for (i = 0; i < node->mapping_count; i++, map++) {
469                         /* if it is special mapping index, skip it */
470                         if (i == index)
471                                 continue;
472
473                         rc = iort_id_map(map, node->type, map_id, &id, out_ref);
474                         if (!rc)
475                                 break;
476                         if (rc == -EAGAIN)
477                                 out_ref = map->output_reference;
478                 }
479
480                 if (i == node->mapping_count && !out_ref)
481                         goto fail_map;
482
483                 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
484                                     rc ? out_ref : map->output_reference);
485         }
486
487 fail_map:
488         /* Map input ID to output ID unchanged on mapping failure */
489         if (id_out)
490                 *id_out = id_in;
491
492         return NULL;
493 }
494
495 static struct acpi_iort_node *iort_node_map_platform_id(
496                 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
497                 int index)
498 {
499         struct acpi_iort_node *parent;
500         u32 id;
501
502         /* step 1: retrieve the initial dev id */
503         parent = iort_node_get_id(node, &id, index);
504         if (!parent)
505                 return NULL;
506
507         /*
508          * optional step 2: map the initial dev id if its parent is not
509          * the target type we want, map it again for the use cases such
510          * as NC (named component) -> SMMU -> ITS. If the type is matched,
511          * return the initial dev id and its parent pointer directly.
512          */
513         if (!(IORT_TYPE_MASK(parent->type) & type_mask))
514                 parent = iort_node_map_id(parent, id, id_out, type_mask);
515         else
516                 if (id_out)
517                         *id_out = id;
518
519         return parent;
520 }
521
522 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
523 {
524         struct pci_bus *pbus;
525
526         if (!dev_is_pci(dev)) {
527                 struct acpi_iort_node *node;
528                 /*
529                  * scan iort_fwnode_list to see if it's an iort platform
530                  * device (such as SMMU, PMCG),its iort node already cached
531                  * and associated with fwnode when iort platform devices
532                  * were initialized.
533                  */
534                 node = iort_get_iort_node(dev->fwnode);
535                 if (node)
536                         return node;
537
538                 /*
539                  * if not, then it should be a platform device defined in
540                  * DSDT/SSDT (with Named Component node in IORT)
541                  */
542                 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
543                                       iort_match_node_callback, dev);
544         }
545
546         /* Find a PCI root bus */
547         pbus = to_pci_dev(dev)->bus;
548         while (!pci_is_root_bus(pbus))
549                 pbus = pbus->parent;
550
551         return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
552                               iort_match_node_callback, &pbus->dev);
553 }
554
555 /**
556  * iort_msi_map_rid() - Map a MSI requester ID for a device
557  * @dev: The device for which the mapping is to be done.
558  * @req_id: The device requester ID.
559  *
560  * Returns: mapped MSI RID on success, input requester ID otherwise
561  */
562 u32 iort_msi_map_rid(struct device *dev, u32 req_id)
563 {
564         struct acpi_iort_node *node;
565         u32 dev_id;
566
567         node = iort_find_dev_node(dev);
568         if (!node)
569                 return req_id;
570
571         iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
572         return dev_id;
573 }
574
575 /**
576  * iort_pmsi_get_dev_id() - Get the device id for a device
577  * @dev: The device for which the mapping is to be done.
578  * @dev_id: The device ID found.
579  *
580  * Returns: 0 for successful find a dev id, -ENODEV on error
581  */
582 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
583 {
584         int i, index;
585         struct acpi_iort_node *node;
586
587         node = iort_find_dev_node(dev);
588         if (!node)
589                 return -ENODEV;
590
591         index = iort_get_id_mapping_index(node);
592         /* if there is a valid index, go get the dev_id directly */
593         if (index >= 0) {
594                 if (iort_node_get_id(node, dev_id, index))
595                         return 0;
596         } else {
597                 for (i = 0; i < node->mapping_count; i++) {
598                         if (iort_node_map_platform_id(node, dev_id,
599                                                       IORT_MSI_TYPE, i))
600                                 return 0;
601                 }
602         }
603
604         return -ENODEV;
605 }
606
607 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
608 {
609         struct iort_its_msi_chip *its_msi_chip;
610         int ret = -ENODEV;
611
612         spin_lock(&iort_msi_chip_lock);
613         list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
614                 if (its_msi_chip->translation_id == its_id) {
615                         *base = its_msi_chip->base_addr;
616                         ret = 0;
617                         break;
618                 }
619         }
620         spin_unlock(&iort_msi_chip_lock);
621
622         return ret;
623 }
624
625 /**
626  * iort_dev_find_its_id() - Find the ITS identifier for a device
627  * @dev: The device.
628  * @req_id: Device's requester ID
629  * @idx: Index of the ITS identifier list.
630  * @its_id: ITS identifier.
631  *
632  * Returns: 0 on success, appropriate error value otherwise
633  */
634 static int iort_dev_find_its_id(struct device *dev, u32 req_id,
635                                 unsigned int idx, int *its_id)
636 {
637         struct acpi_iort_its_group *its;
638         struct acpi_iort_node *node;
639
640         node = iort_find_dev_node(dev);
641         if (!node)
642                 return -ENXIO;
643
644         node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
645         if (!node)
646                 return -ENXIO;
647
648         /* Move to ITS specific data */
649         its = (struct acpi_iort_its_group *)node->node_data;
650         if (idx >= its->its_count) {
651                 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
652                         idx, its->its_count);
653                 return -ENXIO;
654         }
655
656         *its_id = its->identifiers[idx];
657         return 0;
658 }
659
660 /**
661  * iort_get_device_domain() - Find MSI domain related to a device
662  * @dev: The device.
663  * @req_id: Requester ID for the device.
664  *
665  * Returns: the MSI domain for this device, NULL otherwise
666  */
667 struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
668 {
669         struct fwnode_handle *handle;
670         int its_id;
671
672         if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
673                 return NULL;
674
675         handle = iort_find_domain_token(its_id);
676         if (!handle)
677                 return NULL;
678
679         return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
680 }
681
682 static void iort_set_device_domain(struct device *dev,
683                                    struct acpi_iort_node *node)
684 {
685         struct acpi_iort_its_group *its;
686         struct acpi_iort_node *msi_parent;
687         struct acpi_iort_id_mapping *map;
688         struct fwnode_handle *iort_fwnode;
689         struct irq_domain *domain;
690         int index;
691
692         index = iort_get_id_mapping_index(node);
693         if (index < 0)
694                 return;
695
696         map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
697                            node->mapping_offset + index * sizeof(*map));
698
699         /* Firmware bug! */
700         if (!map->output_reference ||
701             !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
702                 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
703                        node, node->type);
704                 return;
705         }
706
707         msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
708                                   map->output_reference);
709
710         if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
711                 return;
712
713         /* Move to ITS specific data */
714         its = (struct acpi_iort_its_group *)msi_parent->node_data;
715
716         iort_fwnode = iort_find_domain_token(its->identifiers[0]);
717         if (!iort_fwnode)
718                 return;
719
720         domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
721         if (domain)
722                 dev_set_msi_domain(dev, domain);
723 }
724
725 /**
726  * iort_get_platform_device_domain() - Find MSI domain related to a
727  * platform device
728  * @dev: the dev pointer associated with the platform device
729  *
730  * Returns: the MSI domain for this device, NULL otherwise
731  */
732 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
733 {
734         struct acpi_iort_node *node, *msi_parent = NULL;
735         struct fwnode_handle *iort_fwnode;
736         struct acpi_iort_its_group *its;
737         int i;
738
739         /* find its associated iort node */
740         node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
741                               iort_match_node_callback, dev);
742         if (!node)
743                 return NULL;
744
745         /* then find its msi parent node */
746         for (i = 0; i < node->mapping_count; i++) {
747                 msi_parent = iort_node_map_platform_id(node, NULL,
748                                                        IORT_MSI_TYPE, i);
749                 if (msi_parent)
750                         break;
751         }
752
753         if (!msi_parent)
754                 return NULL;
755
756         /* Move to ITS specific data */
757         its = (struct acpi_iort_its_group *)msi_parent->node_data;
758
759         iort_fwnode = iort_find_domain_token(its->identifiers[0]);
760         if (!iort_fwnode)
761                 return NULL;
762
763         return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
764 }
765
766 void acpi_configure_pmsi_domain(struct device *dev)
767 {
768         struct irq_domain *msi_domain;
769
770         msi_domain = iort_get_platform_device_domain(dev);
771         if (msi_domain)
772                 dev_set_msi_domain(dev, msi_domain);
773 }
774
775 #ifdef CONFIG_IOMMU_API
776 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
777 {
778         struct acpi_iort_node *iommu;
779         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
780
781         iommu = iort_get_iort_node(fwspec->iommu_fwnode);
782
783         if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
784                 struct acpi_iort_smmu_v3 *smmu;
785
786                 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
787                 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
788                         return iommu;
789         }
790
791         return NULL;
792 }
793
794 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
795 {
796         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
797
798         return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
799 }
800
801 static inline int iort_add_device_replay(const struct iommu_ops *ops,
802                                          struct device *dev)
803 {
804         int err = 0;
805
806         if (dev->bus && !device_iommu_mapped(dev))
807                 err = iommu_probe_device(dev);
808
809         return err;
810 }
811
812 /**
813  * iort_iommu_msi_get_resv_regions - Reserved region driver helper
814  * @dev: Device from iommu_get_resv_regions()
815  * @head: Reserved region list from iommu_get_resv_regions()
816  *
817  * Returns: Number of msi reserved regions on success (0 if platform
818  *          doesn't require the reservation or no associated msi regions),
819  *          appropriate error value otherwise. The ITS interrupt translation
820  *          spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
821  *          are the msi reserved regions.
822  */
823 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
824 {
825         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
826         struct acpi_iort_its_group *its;
827         struct acpi_iort_node *iommu_node, *its_node = NULL;
828         int i, resv = 0;
829
830         iommu_node = iort_get_msi_resv_iommu(dev);
831         if (!iommu_node)
832                 return 0;
833
834         /*
835          * Current logic to reserve ITS regions relies on HW topologies
836          * where a given PCI or named component maps its IDs to only one
837          * ITS group; if a PCI or named component can map its IDs to
838          * different ITS groups through IORT mappings this function has
839          * to be reworked to ensure we reserve regions for all ITS groups
840          * a given PCI or named component may map IDs to.
841          */
842
843         for (i = 0; i < fwspec->num_ids; i++) {
844                 its_node = iort_node_map_id(iommu_node,
845                                         fwspec->ids[i],
846                                         NULL, IORT_MSI_TYPE);
847                 if (its_node)
848                         break;
849         }
850
851         if (!its_node)
852                 return 0;
853
854         /* Move to ITS specific data */
855         its = (struct acpi_iort_its_group *)its_node->node_data;
856
857         for (i = 0; i < its->its_count; i++) {
858                 phys_addr_t base;
859
860                 if (!iort_find_its_base(its->identifiers[i], &base)) {
861                         int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
862                         struct iommu_resv_region *region;
863
864                         region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
865                                                          prot, IOMMU_RESV_MSI);
866                         if (region) {
867                                 list_add_tail(&region->list, head);
868                                 resv++;
869                         }
870                 }
871         }
872
873         return (resv == its->its_count) ? resv : -ENODEV;
874 }
875
876 static inline bool iort_iommu_driver_enabled(u8 type)
877 {
878         switch (type) {
879         case ACPI_IORT_NODE_SMMU_V3:
880                 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
881         case ACPI_IORT_NODE_SMMU:
882                 return IS_ENABLED(CONFIG_ARM_SMMU);
883         default:
884                 pr_warn("IORT node type %u does not describe an SMMU\n", type);
885                 return false;
886         }
887 }
888
889 static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
890                                struct fwnode_handle *fwnode,
891                                const struct iommu_ops *ops)
892 {
893         int ret = iommu_fwspec_init(dev, fwnode, ops);
894
895         if (!ret)
896                 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
897
898         return ret;
899 }
900
901 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
902 {
903         struct acpi_iort_root_complex *pci_rc;
904
905         pci_rc = (struct acpi_iort_root_complex *)node->node_data;
906         return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
907 }
908
909 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
910                             u32 streamid)
911 {
912         const struct iommu_ops *ops;
913         struct fwnode_handle *iort_fwnode;
914
915         if (!node)
916                 return -ENODEV;
917
918         iort_fwnode = iort_get_fwnode(node);
919         if (!iort_fwnode)
920                 return -ENODEV;
921
922         /*
923          * If the ops look-up fails, this means that either
924          * the SMMU drivers have not been probed yet or that
925          * the SMMU drivers are not built in the kernel;
926          * Depending on whether the SMMU drivers are built-in
927          * in the kernel or not, defer the IOMMU configuration
928          * or just abort it.
929          */
930         ops = iommu_ops_from_fwnode(iort_fwnode);
931         if (!ops)
932                 return iort_iommu_driver_enabled(node->type) ?
933                        -EPROBE_DEFER : -ENODEV;
934
935         return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
936 }
937
938 struct iort_pci_alias_info {
939         struct device *dev;
940         struct acpi_iort_node *node;
941 };
942
943 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
944 {
945         struct iort_pci_alias_info *info = data;
946         struct acpi_iort_node *parent;
947         u32 streamid;
948
949         parent = iort_node_map_id(info->node, alias, &streamid,
950                                   IORT_IOMMU_TYPE);
951         return iort_iommu_xlate(info->dev, parent, streamid);
952 }
953
954 static void iort_named_component_init(struct device *dev,
955                                       struct acpi_iort_node *node)
956 {
957         struct acpi_iort_named_component *nc;
958         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
959
960         if (!fwspec)
961                 return;
962
963         nc = (struct acpi_iort_named_component *)node->node_data;
964         fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
965                                            nc->node_flags);
966 }
967
968 /**
969  * iort_iommu_configure - Set-up IOMMU configuration for a device.
970  *
971  * @dev: device to configure
972  *
973  * Returns: iommu_ops pointer on configuration success
974  *          NULL on configuration failure
975  */
976 const struct iommu_ops *iort_iommu_configure(struct device *dev)
977 {
978         struct acpi_iort_node *node, *parent;
979         const struct iommu_ops *ops;
980         u32 streamid = 0;
981         int err = -ENODEV;
982
983         /*
984          * If we already translated the fwspec there
985          * is nothing left to do, return the iommu_ops.
986          */
987         ops = iort_fwspec_iommu_ops(dev);
988         if (ops)
989                 return ops;
990
991         if (dev_is_pci(dev)) {
992                 struct iommu_fwspec *fwspec;
993                 struct pci_bus *bus = to_pci_dev(dev)->bus;
994                 struct iort_pci_alias_info info = { .dev = dev };
995
996                 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
997                                       iort_match_node_callback, &bus->dev);
998                 if (!node)
999                         return NULL;
1000
1001                 info.node = node;
1002                 err = pci_for_each_dma_alias(to_pci_dev(dev),
1003                                              iort_pci_iommu_init, &info);
1004
1005                 fwspec = dev_iommu_fwspec_get(dev);
1006                 if (fwspec && iort_pci_rc_supports_ats(node))
1007                         fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1008         } else {
1009                 int i = 0;
1010
1011                 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1012                                       iort_match_node_callback, dev);
1013                 if (!node)
1014                         return NULL;
1015
1016                 do {
1017                         parent = iort_node_map_platform_id(node, &streamid,
1018                                                            IORT_IOMMU_TYPE,
1019                                                            i++);
1020
1021                         if (parent)
1022                                 err = iort_iommu_xlate(dev, parent, streamid);
1023                 } while (parent && !err);
1024
1025                 if (!err)
1026                         iort_named_component_init(dev, node);
1027         }
1028
1029         /*
1030          * If we have reason to believe the IOMMU driver missed the initial
1031          * add_device callback for dev, replay it to get things in order.
1032          */
1033         if (!err) {
1034                 ops = iort_fwspec_iommu_ops(dev);
1035                 err = iort_add_device_replay(ops, dev);
1036         }
1037
1038         /* Ignore all other errors apart from EPROBE_DEFER */
1039         if (err == -EPROBE_DEFER) {
1040                 ops = ERR_PTR(err);
1041         } else if (err) {
1042                 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1043                 ops = NULL;
1044         }
1045
1046         return ops;
1047 }
1048 #else
1049 static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1050 { return NULL; }
1051 static inline int iort_add_device_replay(const struct iommu_ops *ops,
1052                                          struct device *dev)
1053 { return 0; }
1054 int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1055 { return 0; }
1056 const struct iommu_ops *iort_iommu_configure(struct device *dev)
1057 { return NULL; }
1058 #endif
1059
1060 static int nc_dma_get_range(struct device *dev, u64 *size)
1061 {
1062         struct acpi_iort_node *node;
1063         struct acpi_iort_named_component *ncomp;
1064
1065         node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1066                               iort_match_node_callback, dev);
1067         if (!node)
1068                 return -ENODEV;
1069
1070         ncomp = (struct acpi_iort_named_component *)node->node_data;
1071
1072         *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1073                         1ULL<<ncomp->memory_address_limit;
1074
1075         return 0;
1076 }
1077
1078 static int rc_dma_get_range(struct device *dev, u64 *size)
1079 {
1080         struct acpi_iort_node *node;
1081         struct acpi_iort_root_complex *rc;
1082         struct pci_bus *pbus = to_pci_dev(dev)->bus;
1083
1084         node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1085                               iort_match_node_callback, &pbus->dev);
1086         if (!node || node->revision < 1)
1087                 return -ENODEV;
1088
1089         rc = (struct acpi_iort_root_complex *)node->node_data;
1090
1091         *size = rc->memory_address_limit >= 64 ? U64_MAX :
1092                         1ULL<<rc->memory_address_limit;
1093
1094         return 0;
1095 }
1096
1097 /**
1098  * iort_dma_setup() - Set-up device DMA parameters.
1099  *
1100  * @dev: device to configure
1101  * @dma_addr: device DMA address result pointer
1102  * @size: DMA range size result pointer
1103  */
1104 void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
1105 {
1106         u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1107         int ret;
1108
1109         /*
1110          * If @dev is expected to be DMA-capable then the bus code that created
1111          * it should have initialised its dma_mask pointer by this point. For
1112          * now, we'll continue the legacy behaviour of coercing it to the
1113          * coherent mask if not, but we'll no longer do so quietly.
1114          */
1115         if (!dev->dma_mask) {
1116                 dev_warn(dev, "DMA mask not set\n");
1117                 dev->dma_mask = &dev->coherent_dma_mask;
1118         }
1119
1120         if (dev->coherent_dma_mask)
1121                 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1122         else
1123                 size = 1ULL << 32;
1124
1125         ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1126         if (ret == -ENODEV)
1127                 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1128                                       : nc_dma_get_range(dev, &size);
1129
1130         if (!ret) {
1131                 /*
1132                  * Limit coherent and dma mask based on size retrieved from
1133                  * firmware.
1134                  */
1135                 end = dmaaddr + size - 1;
1136                 mask = DMA_BIT_MASK(ilog2(end) + 1);
1137                 dev->bus_dma_limit = end;
1138                 dev->coherent_dma_mask = mask;
1139                 *dev->dma_mask = mask;
1140         }
1141
1142         *dma_addr = dmaaddr;
1143         *dma_size = size;
1144
1145         dev->dma_pfn_offset = PFN_DOWN(offset);
1146         dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
1147 }
1148
1149 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1150                                           int trigger,
1151                                           struct resource *res)
1152 {
1153         int irq = acpi_register_gsi(NULL, hwirq, trigger,
1154                                     ACPI_ACTIVE_HIGH);
1155
1156         if (irq <= 0) {
1157                 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1158                                                                       name);
1159                 return;
1160         }
1161
1162         res->start = irq;
1163         res->end = irq;
1164         res->flags = IORESOURCE_IRQ;
1165         res->name = name;
1166 }
1167
1168 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1169 {
1170         struct acpi_iort_smmu_v3 *smmu;
1171         /* Always present mem resource */
1172         int num_res = 1;
1173
1174         /* Retrieve SMMUv3 specific data */
1175         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1176
1177         if (smmu->event_gsiv)
1178                 num_res++;
1179
1180         if (smmu->pri_gsiv)
1181                 num_res++;
1182
1183         if (smmu->gerr_gsiv)
1184                 num_res++;
1185
1186         if (smmu->sync_gsiv)
1187                 num_res++;
1188
1189         return num_res;
1190 }
1191
1192 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1193 {
1194         /*
1195          * Cavium ThunderX2 implementation doesn't not support unique
1196          * irq line. Use single irq line for all the SMMUv3 interrupts.
1197          */
1198         if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1199                 return false;
1200
1201         /*
1202          * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1203          * SPI numbers here.
1204          */
1205         return smmu->event_gsiv == smmu->pri_gsiv &&
1206                smmu->event_gsiv == smmu->gerr_gsiv &&
1207                smmu->event_gsiv == smmu->sync_gsiv;
1208 }
1209
1210 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1211 {
1212         /*
1213          * Override the size, for Cavium ThunderX2 implementation
1214          * which doesn't support the page 1 SMMU register space.
1215          */
1216         if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1217                 return SZ_64K;
1218
1219         return SZ_128K;
1220 }
1221
1222 static void __init arm_smmu_v3_init_resources(struct resource *res,
1223                                               struct acpi_iort_node *node)
1224 {
1225         struct acpi_iort_smmu_v3 *smmu;
1226         int num_res = 0;
1227
1228         /* Retrieve SMMUv3 specific data */
1229         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1230
1231         res[num_res].start = smmu->base_address;
1232         res[num_res].end = smmu->base_address +
1233                                 arm_smmu_v3_resource_size(smmu) - 1;
1234         res[num_res].flags = IORESOURCE_MEM;
1235
1236         num_res++;
1237         if (arm_smmu_v3_is_combined_irq(smmu)) {
1238                 if (smmu->event_gsiv)
1239                         acpi_iort_register_irq(smmu->event_gsiv, "combined",
1240                                                ACPI_EDGE_SENSITIVE,
1241                                                &res[num_res++]);
1242         } else {
1243
1244                 if (smmu->event_gsiv)
1245                         acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1246                                                ACPI_EDGE_SENSITIVE,
1247                                                &res[num_res++]);
1248
1249                 if (smmu->pri_gsiv)
1250                         acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1251                                                ACPI_EDGE_SENSITIVE,
1252                                                &res[num_res++]);
1253
1254                 if (smmu->gerr_gsiv)
1255                         acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1256                                                ACPI_EDGE_SENSITIVE,
1257                                                &res[num_res++]);
1258
1259                 if (smmu->sync_gsiv)
1260                         acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1261                                                ACPI_EDGE_SENSITIVE,
1262                                                &res[num_res++]);
1263         }
1264 }
1265
1266 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1267                                              struct acpi_iort_node *node)
1268 {
1269         struct acpi_iort_smmu_v3 *smmu;
1270         enum dev_dma_attr attr;
1271
1272         /* Retrieve SMMUv3 specific data */
1273         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1274
1275         attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1276                         DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1277
1278         /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1279         dev->dma_mask = &dev->coherent_dma_mask;
1280
1281         /* Configure DMA for the page table walker */
1282         acpi_dma_configure(dev, attr);
1283 }
1284
1285 #if defined(CONFIG_ACPI_NUMA)
1286 /*
1287  * set numa proximity domain for smmuv3 device
1288  */
1289 static int  __init arm_smmu_v3_set_proximity(struct device *dev,
1290                                               struct acpi_iort_node *node)
1291 {
1292         struct acpi_iort_smmu_v3 *smmu;
1293
1294         smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1295         if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1296                 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
1297
1298                 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1299                         return -EINVAL;
1300
1301                 set_dev_node(dev, dev_node);
1302                 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1303                         smmu->base_address,
1304                         smmu->pxm);
1305         }
1306         return 0;
1307 }
1308 #else
1309 #define arm_smmu_v3_set_proximity NULL
1310 #endif
1311
1312 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1313 {
1314         struct acpi_iort_smmu *smmu;
1315
1316         /* Retrieve SMMU specific data */
1317         smmu = (struct acpi_iort_smmu *)node->node_data;
1318
1319         /*
1320          * Only consider the global fault interrupt and ignore the
1321          * configuration access interrupt.
1322          *
1323          * MMIO address and global fault interrupt resources are always
1324          * present so add them to the context interrupt count as a static
1325          * value.
1326          */
1327         return smmu->context_interrupt_count + 2;
1328 }
1329
1330 static void __init arm_smmu_init_resources(struct resource *res,
1331                                            struct acpi_iort_node *node)
1332 {
1333         struct acpi_iort_smmu *smmu;
1334         int i, hw_irq, trigger, num_res = 0;
1335         u64 *ctx_irq, *glb_irq;
1336
1337         /* Retrieve SMMU specific data */
1338         smmu = (struct acpi_iort_smmu *)node->node_data;
1339
1340         res[num_res].start = smmu->base_address;
1341         res[num_res].end = smmu->base_address + smmu->span - 1;
1342         res[num_res].flags = IORESOURCE_MEM;
1343         num_res++;
1344
1345         glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1346         /* Global IRQs */
1347         hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1348         trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1349
1350         acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1351                                      &res[num_res++]);
1352
1353         /* Context IRQs */
1354         ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1355         for (i = 0; i < smmu->context_interrupt_count; i++) {
1356                 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1357                 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1358
1359                 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1360                                        &res[num_res++]);
1361         }
1362 }
1363
1364 static void __init arm_smmu_dma_configure(struct device *dev,
1365                                           struct acpi_iort_node *node)
1366 {
1367         struct acpi_iort_smmu *smmu;
1368         enum dev_dma_attr attr;
1369
1370         /* Retrieve SMMU specific data */
1371         smmu = (struct acpi_iort_smmu *)node->node_data;
1372
1373         attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1374                         DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1375
1376         /* We expect the dma masks to be equivalent for SMMU set-ups */
1377         dev->dma_mask = &dev->coherent_dma_mask;
1378
1379         /* Configure DMA for the page table walker */
1380         acpi_dma_configure(dev, attr);
1381 }
1382
1383 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1384 {
1385         struct acpi_iort_pmcg *pmcg;
1386
1387         /* Retrieve PMCG specific data */
1388         pmcg = (struct acpi_iort_pmcg *)node->node_data;
1389
1390         /*
1391          * There are always 2 memory resources.
1392          * If the overflow_gsiv is present then add that for a total of 3.
1393          */
1394         return pmcg->overflow_gsiv ? 3 : 2;
1395 }
1396
1397 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1398                                                    struct acpi_iort_node *node)
1399 {
1400         struct acpi_iort_pmcg *pmcg;
1401
1402         /* Retrieve PMCG specific data */
1403         pmcg = (struct acpi_iort_pmcg *)node->node_data;
1404
1405         res[0].start = pmcg->page0_base_address;
1406         res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1407         res[0].flags = IORESOURCE_MEM;
1408         res[1].start = pmcg->page1_base_address;
1409         res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1410         res[1].flags = IORESOURCE_MEM;
1411
1412         if (pmcg->overflow_gsiv)
1413                 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1414                                        ACPI_EDGE_SENSITIVE, &res[2]);
1415 }
1416
1417 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1418         /* HiSilicon Hip08 Platform */
1419         {"HISI  ", "HIP08   ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1420          "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1421         { }
1422 };
1423
1424 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1425 {
1426         u32 model;
1427         int idx;
1428
1429         idx = acpi_match_platform_list(pmcg_plat_info);
1430         if (idx >= 0)
1431                 model = pmcg_plat_info[idx].data;
1432         else
1433                 model = IORT_SMMU_V3_PMCG_GENERIC;
1434
1435         return platform_device_add_data(pdev, &model, sizeof(model));
1436 }
1437
1438 struct iort_dev_config {
1439         const char *name;
1440         int (*dev_init)(struct acpi_iort_node *node);
1441         void (*dev_dma_configure)(struct device *dev,
1442                                   struct acpi_iort_node *node);
1443         int (*dev_count_resources)(struct acpi_iort_node *node);
1444         void (*dev_init_resources)(struct resource *res,
1445                                      struct acpi_iort_node *node);
1446         int (*dev_set_proximity)(struct device *dev,
1447                                     struct acpi_iort_node *node);
1448         int (*dev_add_platdata)(struct platform_device *pdev);
1449 };
1450
1451 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1452         .name = "arm-smmu-v3",
1453         .dev_dma_configure = arm_smmu_v3_dma_configure,
1454         .dev_count_resources = arm_smmu_v3_count_resources,
1455         .dev_init_resources = arm_smmu_v3_init_resources,
1456         .dev_set_proximity = arm_smmu_v3_set_proximity,
1457 };
1458
1459 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1460         .name = "arm-smmu",
1461         .dev_dma_configure = arm_smmu_dma_configure,
1462         .dev_count_resources = arm_smmu_count_resources,
1463         .dev_init_resources = arm_smmu_init_resources,
1464 };
1465
1466 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1467         .name = "arm-smmu-v3-pmcg",
1468         .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1469         .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1470         .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1471 };
1472
1473 static __init const struct iort_dev_config *iort_get_dev_cfg(
1474                         struct acpi_iort_node *node)
1475 {
1476         switch (node->type) {
1477         case ACPI_IORT_NODE_SMMU_V3:
1478                 return &iort_arm_smmu_v3_cfg;
1479         case ACPI_IORT_NODE_SMMU:
1480                 return &iort_arm_smmu_cfg;
1481         case ACPI_IORT_NODE_PMCG:
1482                 return &iort_arm_smmu_v3_pmcg_cfg;
1483         default:
1484                 return NULL;
1485         }
1486 }
1487
1488 /**
1489  * iort_add_platform_device() - Allocate a platform device for IORT node
1490  * @node: Pointer to device ACPI IORT node
1491  *
1492  * Returns: 0 on success, <0 failure
1493  */
1494 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1495                                            const struct iort_dev_config *ops)
1496 {
1497         struct fwnode_handle *fwnode;
1498         struct platform_device *pdev;
1499         struct resource *r;
1500         int ret, count;
1501
1502         pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1503         if (!pdev)
1504                 return -ENOMEM;
1505
1506         if (ops->dev_set_proximity) {
1507                 ret = ops->dev_set_proximity(&pdev->dev, node);
1508                 if (ret)
1509                         goto dev_put;
1510         }
1511
1512         count = ops->dev_count_resources(node);
1513
1514         r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1515         if (!r) {
1516                 ret = -ENOMEM;
1517                 goto dev_put;
1518         }
1519
1520         ops->dev_init_resources(r, node);
1521
1522         ret = platform_device_add_resources(pdev, r, count);
1523         /*
1524          * Resources are duplicated in platform_device_add_resources,
1525          * free their allocated memory
1526          */
1527         kfree(r);
1528
1529         if (ret)
1530                 goto dev_put;
1531
1532         /*
1533          * Platform devices based on PMCG nodes uses platform_data to
1534          * pass the hardware model info to the driver. For others, add
1535          * a copy of IORT node pointer to platform_data to be used to
1536          * retrieve IORT data information.
1537          */
1538         if (ops->dev_add_platdata)
1539                 ret = ops->dev_add_platdata(pdev);
1540         else
1541                 ret = platform_device_add_data(pdev, &node, sizeof(node));
1542
1543         if (ret)
1544                 goto dev_put;
1545
1546         fwnode = iort_get_fwnode(node);
1547
1548         if (!fwnode) {
1549                 ret = -ENODEV;
1550                 goto dev_put;
1551         }
1552
1553         pdev->dev.fwnode = fwnode;
1554
1555         if (ops->dev_dma_configure)
1556                 ops->dev_dma_configure(&pdev->dev, node);
1557
1558         iort_set_device_domain(&pdev->dev, node);
1559
1560         ret = platform_device_add(pdev);
1561         if (ret)
1562                 goto dma_deconfigure;
1563
1564         return 0;
1565
1566 dma_deconfigure:
1567         arch_teardown_dma_ops(&pdev->dev);
1568 dev_put:
1569         platform_device_put(pdev);
1570
1571         return ret;
1572 }
1573
1574 #ifdef CONFIG_PCI
1575 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1576 {
1577         static bool acs_enabled __initdata;
1578
1579         if (acs_enabled)
1580                 return;
1581
1582         if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1583                 struct acpi_iort_node *parent;
1584                 struct acpi_iort_id_mapping *map;
1585                 int i;
1586
1587                 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1588                                    iort_node->mapping_offset);
1589
1590                 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1591                         if (!map->output_reference)
1592                                 continue;
1593
1594                         parent = ACPI_ADD_PTR(struct acpi_iort_node,
1595                                         iort_table,  map->output_reference);
1596                         /*
1597                          * If we detect a RC->SMMU mapping, make sure
1598                          * we enable ACS on the system.
1599                          */
1600                         if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1601                                 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1602                                 pci_request_acs();
1603                                 acs_enabled = true;
1604                                 return;
1605                         }
1606                 }
1607         }
1608 }
1609 #else
1610 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1611 #endif
1612
1613 static void __init iort_init_platform_devices(void)
1614 {
1615         struct acpi_iort_node *iort_node, *iort_end;
1616         struct acpi_table_iort *iort;
1617         struct fwnode_handle *fwnode;
1618         int i, ret;
1619         const struct iort_dev_config *ops;
1620
1621         /*
1622          * iort_table and iort both point to the start of IORT table, but
1623          * have different struct types
1624          */
1625         iort = (struct acpi_table_iort *)iort_table;
1626
1627         /* Get the first IORT node */
1628         iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1629                                  iort->node_offset);
1630         iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1631                                 iort_table->length);
1632
1633         for (i = 0; i < iort->node_count; i++) {
1634                 if (iort_node >= iort_end) {
1635                         pr_err("iort node pointer overflows, bad table\n");
1636                         return;
1637                 }
1638
1639                 iort_enable_acs(iort_node);
1640
1641                 ops = iort_get_dev_cfg(iort_node);
1642                 if (ops) {
1643                         fwnode = acpi_alloc_fwnode_static();
1644                         if (!fwnode)
1645                                 return;
1646
1647                         iort_set_fwnode(iort_node, fwnode);
1648
1649                         ret = iort_add_platform_device(iort_node, ops);
1650                         if (ret) {
1651                                 iort_delete_fwnode(iort_node);
1652                                 acpi_free_fwnode_static(fwnode);
1653                                 return;
1654                         }
1655                 }
1656
1657                 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1658                                          iort_node->length);
1659         }
1660 }
1661
1662 void __init acpi_iort_init(void)
1663 {
1664         acpi_status status;
1665
1666         /* iort_table will be used at runtime after the iort init,
1667          * so we don't need to call acpi_put_table() to release
1668          * the IORT table mapping.
1669          */
1670         status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1671         if (ACPI_FAILURE(status)) {
1672                 if (status != AE_NOT_FOUND) {
1673                         const char *msg = acpi_format_exception(status);
1674
1675                         pr_err("Failed to get table, %s\n", msg);
1676                 }
1677
1678                 return;
1679         }
1680
1681         iort_init_platform_devices();
1682 }
This page took 0.123528 seconds and 4 git commands to generate.