1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2016, Semihalf
4 * Author: Tomasz Nowicki <tn@semihalf.com>
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 #include <linux/dma-map-ops.h>
22
23 #define IORT_TYPE_MASK(type) (1 << (type))
24 #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
25 #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
26 (1 << ACPI_IORT_NODE_SMMU_V3))
27
28 struct iort_its_msi_chip {
29 struct list_head list;
30 struct fwnode_handle *fw_node;
31 phys_addr_t base_addr;
32 u32 translation_id;
33 };
34
35 struct iort_fwnode {
36 struct list_head list;
37 struct acpi_iort_node *iort_node;
38 struct fwnode_handle *fwnode;
39 };
40 static LIST_HEAD(iort_fwnode_list);
41 static DEFINE_SPINLOCK(iort_fwnode_lock);
42
43 /**
44 * iort_set_fwnode() - Create iort_fwnode and use it to register
45 * iommu data in the iort_fwnode_list
46 *
47 * @iort_node: IORT table node associated with the IOMMU
48 * @fwnode: fwnode associated with the IORT node
49 *
50 * Returns: 0 on success
51 * <0 on failure
52 */
iort_set_fwnode(struct acpi_iort_node * iort_node,struct fwnode_handle * fwnode)53 static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
54 struct fwnode_handle *fwnode)
55 {
56 struct iort_fwnode *np;
57
58 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
59
60 if (WARN_ON(!np))
61 return -ENOMEM;
62
63 INIT_LIST_HEAD(&np->list);
64 np->iort_node = iort_node;
65 np->fwnode = fwnode;
66
67 spin_lock(&iort_fwnode_lock);
68 list_add_tail(&np->list, &iort_fwnode_list);
69 spin_unlock(&iort_fwnode_lock);
70
71 return 0;
72 }
73
74 /**
75 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
76 *
77 * @node: IORT table node to be looked-up
78 *
79 * Returns: fwnode_handle pointer on success, NULL on failure
80 */
iort_get_fwnode(struct acpi_iort_node * node)81 static inline struct fwnode_handle *iort_get_fwnode(
82 struct acpi_iort_node *node)
83 {
84 struct iort_fwnode *curr;
85 struct fwnode_handle *fwnode = NULL;
86
87 spin_lock(&iort_fwnode_lock);
88 list_for_each_entry(curr, &iort_fwnode_list, list) {
89 if (curr->iort_node == node) {
90 fwnode = curr->fwnode;
91 break;
92 }
93 }
94 spin_unlock(&iort_fwnode_lock);
95
96 return fwnode;
97 }
98
99 /**
100 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
101 *
102 * @node: IORT table node associated with fwnode to delete
103 */
iort_delete_fwnode(struct acpi_iort_node * node)104 static inline void iort_delete_fwnode(struct acpi_iort_node *node)
105 {
106 struct iort_fwnode *curr, *tmp;
107
108 spin_lock(&iort_fwnode_lock);
109 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
110 if (curr->iort_node == node) {
111 list_del(&curr->list);
112 kfree(curr);
113 break;
114 }
115 }
116 spin_unlock(&iort_fwnode_lock);
117 }
118
119 /**
120 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
121 *
122 * @fwnode: fwnode associated with device to be looked-up
123 *
124 * Returns: iort_node pointer on success, NULL on failure
125 */
iort_get_iort_node(struct fwnode_handle * fwnode)126 static inline struct acpi_iort_node *iort_get_iort_node(
127 struct fwnode_handle *fwnode)
128 {
129 struct iort_fwnode *curr;
130 struct acpi_iort_node *iort_node = NULL;
131
132 spin_lock(&iort_fwnode_lock);
133 list_for_each_entry(curr, &iort_fwnode_list, list) {
134 if (curr->fwnode == fwnode) {
135 iort_node = curr->iort_node;
136 break;
137 }
138 }
139 spin_unlock(&iort_fwnode_lock);
140
141 return iort_node;
142 }
143
144 typedef acpi_status (*iort_find_node_callback)
145 (struct acpi_iort_node *node, void *context);
146
147 /* Root pointer to the mapped IORT table */
148 static struct acpi_table_header *iort_table;
149
150 static LIST_HEAD(iort_msi_chip_list);
151 static DEFINE_SPINLOCK(iort_msi_chip_lock);
152
153 /**
154 * iort_register_domain_token() - register domain token along with related
155 * ITS ID and base address to the list from where we can get it back later on.
156 * @trans_id: ITS ID.
157 * @base: ITS base address.
158 * @fw_node: Domain token.
159 *
160 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
161 */
iort_register_domain_token(int trans_id,phys_addr_t base,struct fwnode_handle * fw_node)162 int iort_register_domain_token(int trans_id, phys_addr_t base,
163 struct fwnode_handle *fw_node)
164 {
165 struct iort_its_msi_chip *its_msi_chip;
166
167 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
168 if (!its_msi_chip)
169 return -ENOMEM;
170
171 its_msi_chip->fw_node = fw_node;
172 its_msi_chip->translation_id = trans_id;
173 its_msi_chip->base_addr = base;
174
175 spin_lock(&iort_msi_chip_lock);
176 list_add(&its_msi_chip->list, &iort_msi_chip_list);
177 spin_unlock(&iort_msi_chip_lock);
178
179 return 0;
180 }
181
182 /**
183 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
184 * @trans_id: ITS ID.
185 *
186 * Returns: none.
187 */
iort_deregister_domain_token(int trans_id)188 void iort_deregister_domain_token(int trans_id)
189 {
190 struct iort_its_msi_chip *its_msi_chip, *t;
191
192 spin_lock(&iort_msi_chip_lock);
193 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
194 if (its_msi_chip->translation_id == trans_id) {
195 list_del(&its_msi_chip->list);
196 kfree(its_msi_chip);
197 break;
198 }
199 }
200 spin_unlock(&iort_msi_chip_lock);
201 }
202
203 /**
204 * iort_find_domain_token() - Find domain token based on given ITS ID
205 * @trans_id: ITS ID.
206 *
207 * Returns: domain token when find on the list, NULL otherwise
208 */
iort_find_domain_token(int trans_id)209 struct fwnode_handle *iort_find_domain_token(int trans_id)
210 {
211 struct fwnode_handle *fw_node = NULL;
212 struct iort_its_msi_chip *its_msi_chip;
213
214 spin_lock(&iort_msi_chip_lock);
215 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
216 if (its_msi_chip->translation_id == trans_id) {
217 fw_node = its_msi_chip->fw_node;
218 break;
219 }
220 }
221 spin_unlock(&iort_msi_chip_lock);
222
223 return fw_node;
224 }
225
iort_scan_node(enum acpi_iort_node_type type,iort_find_node_callback callback,void * context)226 static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
227 iort_find_node_callback callback,
228 void *context)
229 {
230 struct acpi_iort_node *iort_node, *iort_end;
231 struct acpi_table_iort *iort;
232 int i;
233
234 if (!iort_table)
235 return NULL;
236
237 /* Get the first IORT node */
238 iort = (struct acpi_table_iort *)iort_table;
239 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
240 iort->node_offset);
241 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
242 iort_table->length);
243
244 for (i = 0; i < iort->node_count; i++) {
245 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
246 "IORT node pointer overflows, bad table!\n"))
247 return NULL;
248
249 if (iort_node->type == type &&
250 ACPI_SUCCESS(callback(iort_node, context)))
251 return iort_node;
252
253 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
254 iort_node->length);
255 }
256
257 return NULL;
258 }
259
iort_match_node_callback(struct acpi_iort_node * node,void * context)260 static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
261 void *context)
262 {
263 struct device *dev = context;
264 acpi_status status = AE_NOT_FOUND;
265
266 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
267 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
268 struct acpi_device *adev;
269 struct acpi_iort_named_component *ncomp;
270 struct device *nc_dev = dev;
271
272 /*
273 * Walk the device tree to find a device with an
274 * ACPI companion; there is no point in scanning
275 * IORT for a device matching a named component if
276 * the device does not have an ACPI companion to
277 * start with.
278 */
279 do {
280 adev = ACPI_COMPANION(nc_dev);
281 if (adev)
282 break;
283
284 nc_dev = nc_dev->parent;
285 } while (nc_dev);
286
287 if (!adev)
288 goto out;
289
290 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
291 if (ACPI_FAILURE(status)) {
292 dev_warn(nc_dev, "Can't get device full path name\n");
293 goto out;
294 }
295
296 ncomp = (struct acpi_iort_named_component *)node->node_data;
297 status = !strcmp(ncomp->device_name, buf.pointer) ?
298 AE_OK : AE_NOT_FOUND;
299 acpi_os_free(buf.pointer);
300 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
301 struct acpi_iort_root_complex *pci_rc;
302 struct pci_bus *bus;
303
304 bus = to_pci_bus(dev);
305 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
306
307 /*
308 * It is assumed that PCI segment numbers maps one-to-one
309 * with root complexes. Each segment number can represent only
310 * one root complex.
311 */
312 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
313 AE_OK : AE_NOT_FOUND;
314 }
315 out:
316 return status;
317 }
318
iort_id_map(struct acpi_iort_id_mapping * map,u8 type,u32 rid_in,u32 * rid_out,bool check_overlap)319 static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
320 u32 *rid_out, bool check_overlap)
321 {
322 /* Single mapping does not care for input id */
323 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
324 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
325 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
326 *rid_out = map->output_base;
327 return 0;
328 }
329
330 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
331 map, type);
332 return -ENXIO;
333 }
334
335 if (rid_in < map->input_base ||
336 (rid_in > map->input_base + map->id_count))
337 return -ENXIO;
338
339 if (check_overlap) {
340 /*
341 * We already found a mapping for this input ID at the end of
342 * another region. If it coincides with the start of this
343 * region, we assume the prior match was due to the off-by-1
344 * issue mentioned below, and allow it to be superseded.
345 * Otherwise, things are *really* broken, and we just disregard
346 * duplicate matches entirely to retain compatibility.
347 */
348 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
349 map, rid_in);
350 if (rid_in != map->input_base)
351 return -ENXIO;
352
353 pr_err(FW_BUG "applying workaround.\n");
354 }
355
356 *rid_out = map->output_base + (rid_in - map->input_base);
357
358 /*
359 * Due to confusion regarding the meaning of the id_count field (which
360 * carries the number of IDs *minus 1*), we may have to disregard this
361 * match if it is at the end of the range, and overlaps with the start
362 * of another one.
363 */
364 if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
365 return -EAGAIN;
366 return 0;
367 }
368
iort_node_get_id(struct acpi_iort_node * node,u32 * id_out,int index)369 static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
370 u32 *id_out, int index)
371 {
372 struct acpi_iort_node *parent;
373 struct acpi_iort_id_mapping *map;
374
375 if (!node->mapping_offset || !node->mapping_count ||
376 index >= node->mapping_count)
377 return NULL;
378
379 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
380 node->mapping_offset + index * sizeof(*map));
381
382 /* Firmware bug! */
383 if (!map->output_reference) {
384 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
385 node, node->type);
386 return NULL;
387 }
388
389 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
390 map->output_reference);
391
392 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
393 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
394 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
395 node->type == ACPI_IORT_NODE_SMMU_V3 ||
396 node->type == ACPI_IORT_NODE_PMCG) {
397 *id_out = map->output_base;
398 return parent;
399 }
400 }
401
402 return NULL;
403 }
404
iort_get_id_mapping_index(struct acpi_iort_node * node)405 static int iort_get_id_mapping_index(struct acpi_iort_node *node)
406 {
407 struct acpi_iort_smmu_v3 *smmu;
408 struct acpi_iort_pmcg *pmcg;
409
410 switch (node->type) {
411 case ACPI_IORT_NODE_SMMU_V3:
412 /*
413 * SMMUv3 dev ID mapping index was introduced in revision 1
414 * table, not available in revision 0
415 */
416 if (node->revision < 1)
417 return -EINVAL;
418
419 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
420 /*
421 * ID mapping index is only ignored if all interrupts are
422 * GSIV based
423 */
424 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
425 && smmu->sync_gsiv)
426 return -EINVAL;
427
428 if (smmu->id_mapping_index >= node->mapping_count) {
429 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
430 node, node->type);
431 return -EINVAL;
432 }
433
434 return smmu->id_mapping_index;
435 case ACPI_IORT_NODE_PMCG:
436 pmcg = (struct acpi_iort_pmcg *)node->node_data;
437 if (pmcg->overflow_gsiv || node->mapping_count == 0)
438 return -EINVAL;
439
440 return 0;
441 default:
442 return -EINVAL;
443 }
444 }
445
iort_node_map_id(struct acpi_iort_node * node,u32 id_in,u32 * id_out,u8 type_mask)446 static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
447 u32 id_in, u32 *id_out,
448 u8 type_mask)
449 {
450 u32 id = id_in;
451
452 /* Parse the ID mapping tree to find specified node type */
453 while (node) {
454 struct acpi_iort_id_mapping *map;
455 int i, index, rc = 0;
456 u32 out_ref = 0, map_id = id;
457
458 if (IORT_TYPE_MASK(node->type) & type_mask) {
459 if (id_out)
460 *id_out = id;
461 return node;
462 }
463
464 if (!node->mapping_offset || !node->mapping_count)
465 goto fail_map;
466
467 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
468 node->mapping_offset);
469
470 /* Firmware bug! */
471 if (!map->output_reference) {
472 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
473 node, node->type);
474 goto fail_map;
475 }
476
477 /*
478 * Get the special ID mapping index (if any) and skip its
479 * associated ID map to prevent erroneous multi-stage
480 * IORT ID translations.
481 */
482 index = iort_get_id_mapping_index(node);
483
484 /* Do the ID translation */
485 for (i = 0; i < node->mapping_count; i++, map++) {
486 /* if it is special mapping index, skip it */
487 if (i == index)
488 continue;
489
490 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
491 if (!rc)
492 break;
493 if (rc == -EAGAIN)
494 out_ref = map->output_reference;
495 }
496
497 if (i == node->mapping_count && !out_ref)
498 goto fail_map;
499
500 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
501 rc ? out_ref : map->output_reference);
502 }
503
504 fail_map:
505 /* Map input ID to output ID unchanged on mapping failure */
506 if (id_out)
507 *id_out = id_in;
508
509 return NULL;
510 }
511
iort_node_map_platform_id(struct acpi_iort_node * node,u32 * id_out,u8 type_mask,int index)512 static struct acpi_iort_node *iort_node_map_platform_id(
513 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
514 int index)
515 {
516 struct acpi_iort_node *parent;
517 u32 id;
518
519 /* step 1: retrieve the initial dev id */
520 parent = iort_node_get_id(node, &id, index);
521 if (!parent)
522 return NULL;
523
524 /*
525 * optional step 2: map the initial dev id if its parent is not
526 * the target type we want, map it again for the use cases such
527 * as NC (named component) -> SMMU -> ITS. If the type is matched,
528 * return the initial dev id and its parent pointer directly.
529 */
530 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
531 parent = iort_node_map_id(parent, id, id_out, type_mask);
532 else
533 if (id_out)
534 *id_out = id;
535
536 return parent;
537 }
538
iort_find_dev_node(struct device * dev)539 static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
540 {
541 struct pci_bus *pbus;
542
543 if (!dev_is_pci(dev)) {
544 struct acpi_iort_node *node;
545 /*
546 * scan iort_fwnode_list to see if it's an iort platform
547 * device (such as SMMU, PMCG),its iort node already cached
548 * and associated with fwnode when iort platform devices
549 * were initialized.
550 */
551 node = iort_get_iort_node(dev->fwnode);
552 if (node)
553 return node;
554 /*
555 * if not, then it should be a platform device defined in
556 * DSDT/SSDT (with Named Component node in IORT)
557 */
558 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
559 iort_match_node_callback, dev);
560 }
561
562 pbus = to_pci_dev(dev)->bus;
563
564 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
565 iort_match_node_callback, &pbus->dev);
566 }
567
568 /**
569 * iort_msi_map_id() - Map a MSI input ID for a device
570 * @dev: The device for which the mapping is to be done.
571 * @input_id: The device input ID.
572 *
573 * Returns: mapped MSI ID on success, input ID otherwise
574 */
iort_msi_map_id(struct device * dev,u32 input_id)575 u32 iort_msi_map_id(struct device *dev, u32 input_id)
576 {
577 struct acpi_iort_node *node;
578 u32 dev_id;
579
580 node = iort_find_dev_node(dev);
581 if (!node)
582 return input_id;
583
584 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
585 return dev_id;
586 }
587
588 /**
589 * iort_pmsi_get_dev_id() - Get the device id for a device
590 * @dev: The device for which the mapping is to be done.
591 * @dev_id: The device ID found.
592 *
593 * Returns: 0 for successful find a dev id, -ENODEV on error
594 */
iort_pmsi_get_dev_id(struct device * dev,u32 * dev_id)595 int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
596 {
597 int i, index;
598 struct acpi_iort_node *node;
599
600 node = iort_find_dev_node(dev);
601 if (!node)
602 return -ENODEV;
603
604 index = iort_get_id_mapping_index(node);
605 /* if there is a valid index, go get the dev_id directly */
606 if (index >= 0) {
607 if (iort_node_get_id(node, dev_id, index))
608 return 0;
609 } else {
610 for (i = 0; i < node->mapping_count; i++) {
611 if (iort_node_map_platform_id(node, dev_id,
612 IORT_MSI_TYPE, i))
613 return 0;
614 }
615 }
616
617 return -ENODEV;
618 }
619
iort_find_its_base(u32 its_id,phys_addr_t * base)620 static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
621 {
622 struct iort_its_msi_chip *its_msi_chip;
623 int ret = -ENODEV;
624
625 spin_lock(&iort_msi_chip_lock);
626 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
627 if (its_msi_chip->translation_id == its_id) {
628 *base = its_msi_chip->base_addr;
629 ret = 0;
630 break;
631 }
632 }
633 spin_unlock(&iort_msi_chip_lock);
634
635 return ret;
636 }
637
638 /**
639 * iort_dev_find_its_id() - Find the ITS identifier for a device
640 * @dev: The device.
641 * @id: Device's ID
642 * @idx: Index of the ITS identifier list.
643 * @its_id: ITS identifier.
644 *
645 * Returns: 0 on success, appropriate error value otherwise
646 */
iort_dev_find_its_id(struct device * dev,u32 id,unsigned int idx,int * its_id)647 static int iort_dev_find_its_id(struct device *dev, u32 id,
648 unsigned int idx, int *its_id)
649 {
650 struct acpi_iort_its_group *its;
651 struct acpi_iort_node *node;
652
653 node = iort_find_dev_node(dev);
654 if (!node)
655 return -ENXIO;
656
657 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
658 if (!node)
659 return -ENXIO;
660
661 /* Move to ITS specific data */
662 its = (struct acpi_iort_its_group *)node->node_data;
663 if (idx >= its->its_count) {
664 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
665 idx, its->its_count);
666 return -ENXIO;
667 }
668
669 *its_id = its->identifiers[idx];
670 return 0;
671 }
672
673 /**
674 * iort_get_device_domain() - Find MSI domain related to a device
675 * @dev: The device.
676 * @id: Requester ID for the device.
677 * @bus_token: irq domain bus token.
678 *
679 * Returns: the MSI domain for this device, NULL otherwise
680 */
iort_get_device_domain(struct device * dev,u32 id,enum irq_domain_bus_token bus_token)681 struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
682 enum irq_domain_bus_token bus_token)
683 {
684 struct fwnode_handle *handle;
685 int its_id;
686
687 if (iort_dev_find_its_id(dev, id, 0, &its_id))
688 return NULL;
689
690 handle = iort_find_domain_token(its_id);
691 if (!handle)
692 return NULL;
693
694 return irq_find_matching_fwnode(handle, bus_token);
695 }
696
iort_set_device_domain(struct device * dev,struct acpi_iort_node * node)697 static void iort_set_device_domain(struct device *dev,
698 struct acpi_iort_node *node)
699 {
700 struct acpi_iort_its_group *its;
701 struct acpi_iort_node *msi_parent;
702 struct acpi_iort_id_mapping *map;
703 struct fwnode_handle *iort_fwnode;
704 struct irq_domain *domain;
705 int index;
706
707 index = iort_get_id_mapping_index(node);
708 if (index < 0)
709 return;
710
711 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
712 node->mapping_offset + index * sizeof(*map));
713
714 /* Firmware bug! */
715 if (!map->output_reference ||
716 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
717 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
718 node, node->type);
719 return;
720 }
721
722 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
723 map->output_reference);
724
725 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
726 return;
727
728 /* Move to ITS specific data */
729 its = (struct acpi_iort_its_group *)msi_parent->node_data;
730
731 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
732 if (!iort_fwnode)
733 return;
734
735 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
736 if (domain)
737 dev_set_msi_domain(dev, domain);
738 }
739
740 /**
741 * iort_get_platform_device_domain() - Find MSI domain related to a
742 * platform device
743 * @dev: the dev pointer associated with the platform device
744 *
745 * Returns: the MSI domain for this device, NULL otherwise
746 */
iort_get_platform_device_domain(struct device * dev)747 static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
748 {
749 struct acpi_iort_node *node, *msi_parent = NULL;
750 struct fwnode_handle *iort_fwnode;
751 struct acpi_iort_its_group *its;
752 int i;
753
754 /* find its associated iort node */
755 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
756 iort_match_node_callback, dev);
757 if (!node)
758 return NULL;
759
760 /* then find its msi parent node */
761 for (i = 0; i < node->mapping_count; i++) {
762 msi_parent = iort_node_map_platform_id(node, NULL,
763 IORT_MSI_TYPE, i);
764 if (msi_parent)
765 break;
766 }
767
768 if (!msi_parent)
769 return NULL;
770
771 /* Move to ITS specific data */
772 its = (struct acpi_iort_its_group *)msi_parent->node_data;
773
774 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
775 if (!iort_fwnode)
776 return NULL;
777
778 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
779 }
780
acpi_configure_pmsi_domain(struct device * dev)781 void acpi_configure_pmsi_domain(struct device *dev)
782 {
783 struct irq_domain *msi_domain;
784
785 msi_domain = iort_get_platform_device_domain(dev);
786 if (msi_domain)
787 dev_set_msi_domain(dev, msi_domain);
788 }
789
790 #ifdef CONFIG_IOMMU_API
iort_rmr_free(struct device * dev,struct iommu_resv_region * region)791 static void iort_rmr_free(struct device *dev,
792 struct iommu_resv_region *region)
793 {
794 struct iommu_iort_rmr_data *rmr_data;
795
796 rmr_data = container_of(region, struct iommu_iort_rmr_data, rr);
797 kfree(rmr_data->sids);
798 kfree(rmr_data);
799 }
800
iort_rmr_alloc(struct acpi_iort_rmr_desc * rmr_desc,int prot,enum iommu_resv_type type,u32 * sids,u32 num_sids)801 static struct iommu_iort_rmr_data *iort_rmr_alloc(
802 struct acpi_iort_rmr_desc *rmr_desc,
803 int prot, enum iommu_resv_type type,
804 u32 *sids, u32 num_sids)
805 {
806 struct iommu_iort_rmr_data *rmr_data;
807 struct iommu_resv_region *region;
808 u32 *sids_copy;
809 u64 addr = rmr_desc->base_address, size = rmr_desc->length;
810
811 rmr_data = kmalloc(sizeof(*rmr_data), GFP_KERNEL);
812 if (!rmr_data)
813 return NULL;
814
815 /* Create a copy of SIDs array to associate with this rmr_data */
816 sids_copy = kmemdup(sids, num_sids * sizeof(*sids), GFP_KERNEL);
817 if (!sids_copy) {
818 kfree(rmr_data);
819 return NULL;
820 }
821 rmr_data->sids = sids_copy;
822 rmr_data->num_sids = num_sids;
823
824 if (!IS_ALIGNED(addr, SZ_64K) || !IS_ALIGNED(size, SZ_64K)) {
825 /* PAGE align base addr and size */
826 addr &= PAGE_MASK;
827 size = PAGE_ALIGN(size + offset_in_page(rmr_desc->base_address));
828
829 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] not aligned to 64K, continue with [0x%llx - 0x%llx]\n",
830 rmr_desc->base_address,
831 rmr_desc->base_address + rmr_desc->length - 1,
832 addr, addr + size - 1);
833 }
834
835 region = &rmr_data->rr;
836 INIT_LIST_HEAD(®ion->list);
837 region->start = addr;
838 region->length = size;
839 region->prot = prot;
840 region->type = type;
841 region->free = iort_rmr_free;
842
843 return rmr_data;
844 }
845
iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc * desc,u32 count)846 static void iort_rmr_desc_check_overlap(struct acpi_iort_rmr_desc *desc,
847 u32 count)
848 {
849 int i, j;
850
851 for (i = 0; i < count; i++) {
852 u64 end, start = desc[i].base_address, length = desc[i].length;
853
854 if (!length) {
855 pr_err(FW_BUG "RMR descriptor[0x%llx] with zero length, continue anyway\n",
856 start);
857 continue;
858 }
859
860 end = start + length - 1;
861
862 /* Check for address overlap */
863 for (j = i + 1; j < count; j++) {
864 u64 e_start = desc[j].base_address;
865 u64 e_end = e_start + desc[j].length - 1;
866
867 if (start <= e_end && end >= e_start)
868 pr_err(FW_BUG "RMR descriptor[0x%llx - 0x%llx] overlaps, continue anyway\n",
869 start, end);
870 }
871 }
872 }
873
874 /*
875 * Please note, we will keep the already allocated RMR reserve
876 * regions in case of a memory allocation failure.
877 */
iort_get_rmrs(struct acpi_iort_node * node,struct acpi_iort_node * smmu,u32 * sids,u32 num_sids,struct list_head * head)878 static void iort_get_rmrs(struct acpi_iort_node *node,
879 struct acpi_iort_node *smmu,
880 u32 *sids, u32 num_sids,
881 struct list_head *head)
882 {
883 struct acpi_iort_rmr *rmr = (struct acpi_iort_rmr *)node->node_data;
884 struct acpi_iort_rmr_desc *rmr_desc;
885 int i;
886
887 rmr_desc = ACPI_ADD_PTR(struct acpi_iort_rmr_desc, node,
888 rmr->rmr_offset);
889
890 iort_rmr_desc_check_overlap(rmr_desc, rmr->rmr_count);
891
892 for (i = 0; i < rmr->rmr_count; i++, rmr_desc++) {
893 struct iommu_iort_rmr_data *rmr_data;
894 enum iommu_resv_type type;
895 int prot = IOMMU_READ | IOMMU_WRITE;
896
897 if (rmr->flags & ACPI_IORT_RMR_REMAP_PERMITTED)
898 type = IOMMU_RESV_DIRECT_RELAXABLE;
899 else
900 type = IOMMU_RESV_DIRECT;
901
902 if (rmr->flags & ACPI_IORT_RMR_ACCESS_PRIVILEGE)
903 prot |= IOMMU_PRIV;
904
905 /* Attributes 0x00 - 0x03 represents device memory */
906 if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) <=
907 ACPI_IORT_RMR_ATTR_DEVICE_GRE)
908 prot |= IOMMU_MMIO;
909 else if (ACPI_IORT_RMR_ACCESS_ATTRIBUTES(rmr->flags) ==
910 ACPI_IORT_RMR_ATTR_NORMAL_IWB_OWB)
911 prot |= IOMMU_CACHE;
912
913 rmr_data = iort_rmr_alloc(rmr_desc, prot, type,
914 sids, num_sids);
915 if (!rmr_data)
916 return;
917
918 list_add_tail(&rmr_data->rr.list, head);
919 }
920 }
921
iort_rmr_alloc_sids(u32 * sids,u32 count,u32 id_start,u32 new_count)922 static u32 *iort_rmr_alloc_sids(u32 *sids, u32 count, u32 id_start,
923 u32 new_count)
924 {
925 u32 *new_sids;
926 u32 total_count = count + new_count;
927 int i;
928
929 new_sids = krealloc_array(sids, count + new_count,
930 sizeof(*new_sids), GFP_KERNEL);
931 if (!new_sids)
932 return NULL;
933
934 for (i = count; i < total_count; i++)
935 new_sids[i] = id_start++;
936
937 return new_sids;
938 }
939
iort_rmr_has_dev(struct device * dev,u32 id_start,u32 id_count)940 static bool iort_rmr_has_dev(struct device *dev, u32 id_start,
941 u32 id_count)
942 {
943 int i;
944 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
945
946 /*
947 * Make sure the kernel has preserved the boot firmware PCIe
948 * configuration. This is required to ensure that the RMR PCIe
949 * StreamIDs are still valid (Refer: ARM DEN 0049E.d Section 3.1.1.5).
950 */
951 if (dev_is_pci(dev)) {
952 struct pci_dev *pdev = to_pci_dev(dev);
953 struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
954
955 if (!host->preserve_config)
956 return false;
957 }
958
959 for (i = 0; i < fwspec->num_ids; i++) {
960 if (fwspec->ids[i] >= id_start &&
961 fwspec->ids[i] <= id_start + id_count)
962 return true;
963 }
964
965 return false;
966 }
967
iort_node_get_rmr_info(struct acpi_iort_node * node,struct acpi_iort_node * iommu,struct device * dev,struct list_head * head)968 static void iort_node_get_rmr_info(struct acpi_iort_node *node,
969 struct acpi_iort_node *iommu,
970 struct device *dev, struct list_head *head)
971 {
972 struct acpi_iort_node *smmu = NULL;
973 struct acpi_iort_rmr *rmr;
974 struct acpi_iort_id_mapping *map;
975 u32 *sids = NULL;
976 u32 num_sids = 0;
977 int i;
978
979 if (!node->mapping_offset || !node->mapping_count) {
980 pr_err(FW_BUG "Invalid ID mapping, skipping RMR node %p\n",
981 node);
982 return;
983 }
984
985 rmr = (struct acpi_iort_rmr *)node->node_data;
986 if (!rmr->rmr_offset || !rmr->rmr_count)
987 return;
988
989 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
990 node->mapping_offset);
991
992 /*
993 * Go through the ID mappings and see if we have a match for SMMU
994 * and dev(if !NULL). If found, get the sids for the Node.
995 * Please note, id_count is equal to the number of IDs in the
996 * range minus one.
997 */
998 for (i = 0; i < node->mapping_count; i++, map++) {
999 struct acpi_iort_node *parent;
1000
1001 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
1002 map->output_reference);
1003 if (parent != iommu)
1004 continue;
1005
1006 /* If dev is valid, check RMR node corresponds to the dev SID */
1007 if (dev && !iort_rmr_has_dev(dev, map->output_base,
1008 map->id_count))
1009 continue;
1010
1011 /* Retrieve SIDs associated with the Node. */
1012 sids = iort_rmr_alloc_sids(sids, num_sids, map->output_base,
1013 map->id_count + 1);
1014 if (!sids)
1015 return;
1016
1017 num_sids += map->id_count + 1;
1018 }
1019
1020 if (!sids)
1021 return;
1022
1023 iort_get_rmrs(node, smmu, sids, num_sids, head);
1024 kfree(sids);
1025 }
1026
iort_find_rmrs(struct acpi_iort_node * iommu,struct device * dev,struct list_head * head)1027 static void iort_find_rmrs(struct acpi_iort_node *iommu, struct device *dev,
1028 struct list_head *head)
1029 {
1030 struct acpi_table_iort *iort;
1031 struct acpi_iort_node *iort_node, *iort_end;
1032 int i;
1033
1034 /* Only supports ARM DEN 0049E.d onwards */
1035 if (iort_table->revision < 5)
1036 return;
1037
1038 iort = (struct acpi_table_iort *)iort_table;
1039
1040 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1041 iort->node_offset);
1042 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1043 iort_table->length);
1044
1045 for (i = 0; i < iort->node_count; i++) {
1046 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
1047 "IORT node pointer overflows, bad table!\n"))
1048 return;
1049
1050 if (iort_node->type == ACPI_IORT_NODE_RMR)
1051 iort_node_get_rmr_info(iort_node, iommu, dev, head);
1052
1053 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1054 iort_node->length);
1055 }
1056 }
1057
1058 /*
1059 * Populate the RMR list associated with a given IOMMU and dev(if provided).
1060 * If dev is NULL, the function populates all the RMRs associated with the
1061 * given IOMMU.
1062 */
iort_iommu_rmr_get_resv_regions(struct fwnode_handle * iommu_fwnode,struct device * dev,struct list_head * head)1063 static void iort_iommu_rmr_get_resv_regions(struct fwnode_handle *iommu_fwnode,
1064 struct device *dev,
1065 struct list_head *head)
1066 {
1067 struct acpi_iort_node *iommu;
1068
1069 iommu = iort_get_iort_node(iommu_fwnode);
1070 if (!iommu)
1071 return;
1072
1073 iort_find_rmrs(iommu, dev, head);
1074 }
1075
iort_get_msi_resv_iommu(struct device * dev)1076 static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
1077 {
1078 struct acpi_iort_node *iommu;
1079 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1080
1081 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
1082
1083 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
1084 struct acpi_iort_smmu_v3 *smmu;
1085
1086 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
1087 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
1088 return iommu;
1089 }
1090
1091 return NULL;
1092 }
1093
1094 /*
1095 * Retrieve platform specific HW MSI reserve regions.
1096 * The ITS interrupt translation spaces (ITS_base + SZ_64K, SZ_64K)
1097 * associated with the device are the HW MSI reserved regions.
1098 */
iort_iommu_msi_get_resv_regions(struct device * dev,struct list_head * head)1099 static void iort_iommu_msi_get_resv_regions(struct device *dev,
1100 struct list_head *head)
1101 {
1102 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1103 struct acpi_iort_its_group *its;
1104 struct acpi_iort_node *iommu_node, *its_node = NULL;
1105 int i;
1106
1107 iommu_node = iort_get_msi_resv_iommu(dev);
1108 if (!iommu_node)
1109 return;
1110
1111 /*
1112 * Current logic to reserve ITS regions relies on HW topologies
1113 * where a given PCI or named component maps its IDs to only one
1114 * ITS group; if a PCI or named component can map its IDs to
1115 * different ITS groups through IORT mappings this function has
1116 * to be reworked to ensure we reserve regions for all ITS groups
1117 * a given PCI or named component may map IDs to.
1118 */
1119
1120 for (i = 0; i < fwspec->num_ids; i++) {
1121 its_node = iort_node_map_id(iommu_node,
1122 fwspec->ids[i],
1123 NULL, IORT_MSI_TYPE);
1124 if (its_node)
1125 break;
1126 }
1127
1128 if (!its_node)
1129 return;
1130
1131 /* Move to ITS specific data */
1132 its = (struct acpi_iort_its_group *)its_node->node_data;
1133
1134 for (i = 0; i < its->its_count; i++) {
1135 phys_addr_t base;
1136
1137 if (!iort_find_its_base(its->identifiers[i], &base)) {
1138 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1139 struct iommu_resv_region *region;
1140
1141 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
1142 prot, IOMMU_RESV_MSI,
1143 GFP_KERNEL);
1144 if (region)
1145 list_add_tail(®ion->list, head);
1146 }
1147 }
1148 }
1149
1150 /**
1151 * iort_iommu_get_resv_regions - Generic helper to retrieve reserved regions.
1152 * @dev: Device from iommu_get_resv_regions()
1153 * @head: Reserved region list from iommu_get_resv_regions()
1154 */
iort_iommu_get_resv_regions(struct device * dev,struct list_head * head)1155 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
1156 {
1157 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1158
1159 iort_iommu_msi_get_resv_regions(dev, head);
1160 iort_iommu_rmr_get_resv_regions(fwspec->iommu_fwnode, dev, head);
1161 }
1162
1163 /**
1164 * iort_get_rmr_sids - Retrieve IORT RMR node reserved regions with
1165 * associated StreamIDs information.
1166 * @iommu_fwnode: fwnode associated with IOMMU
1167 * @head: Resereved region list
1168 */
iort_get_rmr_sids(struct fwnode_handle * iommu_fwnode,struct list_head * head)1169 void iort_get_rmr_sids(struct fwnode_handle *iommu_fwnode,
1170 struct list_head *head)
1171 {
1172 iort_iommu_rmr_get_resv_regions(iommu_fwnode, NULL, head);
1173 }
1174 EXPORT_SYMBOL_GPL(iort_get_rmr_sids);
1175
1176 /**
1177 * iort_put_rmr_sids - Free memory allocated for RMR reserved regions.
1178 * @iommu_fwnode: fwnode associated with IOMMU
1179 * @head: Resereved region list
1180 */
iort_put_rmr_sids(struct fwnode_handle * iommu_fwnode,struct list_head * head)1181 void iort_put_rmr_sids(struct fwnode_handle *iommu_fwnode,
1182 struct list_head *head)
1183 {
1184 struct iommu_resv_region *entry, *next;
1185
1186 list_for_each_entry_safe(entry, next, head, list)
1187 entry->free(NULL, entry);
1188 }
1189 EXPORT_SYMBOL_GPL(iort_put_rmr_sids);
1190
iort_iommu_driver_enabled(u8 type)1191 static inline bool iort_iommu_driver_enabled(u8 type)
1192 {
1193 switch (type) {
1194 case ACPI_IORT_NODE_SMMU_V3:
1195 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
1196 case ACPI_IORT_NODE_SMMU:
1197 return IS_ENABLED(CONFIG_ARM_SMMU);
1198 default:
1199 pr_warn("IORT node type %u does not describe an SMMU\n", type);
1200 return false;
1201 }
1202 }
1203
iort_pci_rc_supports_ats(struct acpi_iort_node * node)1204 static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
1205 {
1206 struct acpi_iort_root_complex *pci_rc;
1207
1208 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
1209 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
1210 }
1211
iort_iommu_xlate(struct device * dev,struct acpi_iort_node * node,u32 streamid)1212 static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
1213 u32 streamid)
1214 {
1215 const struct iommu_ops *ops;
1216 struct fwnode_handle *iort_fwnode;
1217
1218 if (!node)
1219 return -ENODEV;
1220
1221 iort_fwnode = iort_get_fwnode(node);
1222 if (!iort_fwnode)
1223 return -ENODEV;
1224
1225 /*
1226 * If the ops look-up fails, this means that either
1227 * the SMMU drivers have not been probed yet or that
1228 * the SMMU drivers are not built in the kernel;
1229 * Depending on whether the SMMU drivers are built-in
1230 * in the kernel or not, defer the IOMMU configuration
1231 * or just abort it.
1232 */
1233 ops = iommu_ops_from_fwnode(iort_fwnode);
1234 if (!ops)
1235 return iort_iommu_driver_enabled(node->type) ?
1236 -EPROBE_DEFER : -ENODEV;
1237
1238 return acpi_iommu_fwspec_init(dev, streamid, iort_fwnode, ops);
1239 }
1240
1241 struct iort_pci_alias_info {
1242 struct device *dev;
1243 struct acpi_iort_node *node;
1244 };
1245
iort_pci_iommu_init(struct pci_dev * pdev,u16 alias,void * data)1246 static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
1247 {
1248 struct iort_pci_alias_info *info = data;
1249 struct acpi_iort_node *parent;
1250 u32 streamid;
1251
1252 parent = iort_node_map_id(info->node, alias, &streamid,
1253 IORT_IOMMU_TYPE);
1254 return iort_iommu_xlate(info->dev, parent, streamid);
1255 }
1256
iort_named_component_init(struct device * dev,struct acpi_iort_node * node)1257 static void iort_named_component_init(struct device *dev,
1258 struct acpi_iort_node *node)
1259 {
1260 struct property_entry props[3] = {};
1261 struct acpi_iort_named_component *nc;
1262
1263 nc = (struct acpi_iort_named_component *)node->node_data;
1264 props[0] = PROPERTY_ENTRY_U32("pasid-num-bits",
1265 FIELD_GET(ACPI_IORT_NC_PASID_BITS,
1266 nc->node_flags));
1267 if (nc->node_flags & ACPI_IORT_NC_STALL_SUPPORTED)
1268 props[1] = PROPERTY_ENTRY_BOOL("dma-can-stall");
1269
1270 if (device_create_managed_software_node(dev, props, NULL))
1271 dev_warn(dev, "Could not add device properties\n");
1272 }
1273
iort_nc_iommu_map(struct device * dev,struct acpi_iort_node * node)1274 static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
1275 {
1276 struct acpi_iort_node *parent;
1277 int err = -ENODEV, i = 0;
1278 u32 streamid = 0;
1279
1280 do {
1281
1282 parent = iort_node_map_platform_id(node, &streamid,
1283 IORT_IOMMU_TYPE,
1284 i++);
1285
1286 if (parent)
1287 err = iort_iommu_xlate(dev, parent, streamid);
1288 } while (parent && !err);
1289
1290 return err;
1291 }
1292
iort_nc_iommu_map_id(struct device * dev,struct acpi_iort_node * node,const u32 * in_id)1293 static int iort_nc_iommu_map_id(struct device *dev,
1294 struct acpi_iort_node *node,
1295 const u32 *in_id)
1296 {
1297 struct acpi_iort_node *parent;
1298 u32 streamid;
1299
1300 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
1301 if (parent)
1302 return iort_iommu_xlate(dev, parent, streamid);
1303
1304 return -ENODEV;
1305 }
1306
1307
1308 /**
1309 * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
1310 *
1311 * @dev: device to configure
1312 * @id_in: optional input id const value pointer
1313 *
1314 * Returns: 0 on success, <0 on failure
1315 */
iort_iommu_configure_id(struct device * dev,const u32 * id_in)1316 int iort_iommu_configure_id(struct device *dev, const u32 *id_in)
1317 {
1318 struct acpi_iort_node *node;
1319 int err = -ENODEV;
1320
1321 if (dev_is_pci(dev)) {
1322 struct iommu_fwspec *fwspec;
1323 struct pci_bus *bus = to_pci_dev(dev)->bus;
1324 struct iort_pci_alias_info info = { .dev = dev };
1325
1326 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1327 iort_match_node_callback, &bus->dev);
1328 if (!node)
1329 return -ENODEV;
1330
1331 info.node = node;
1332 err = pci_for_each_dma_alias(to_pci_dev(dev),
1333 iort_pci_iommu_init, &info);
1334
1335 fwspec = dev_iommu_fwspec_get(dev);
1336 if (fwspec && iort_pci_rc_supports_ats(node))
1337 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
1338 } else {
1339 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1340 iort_match_node_callback, dev);
1341 if (!node)
1342 return -ENODEV;
1343
1344 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
1345 iort_nc_iommu_map(dev, node);
1346
1347 if (!err)
1348 iort_named_component_init(dev, node);
1349 }
1350
1351 return err;
1352 }
1353
1354 #else
iort_iommu_get_resv_regions(struct device * dev,struct list_head * head)1355 void iort_iommu_get_resv_regions(struct device *dev, struct list_head *head)
1356 { }
iort_iommu_configure_id(struct device * dev,const u32 * input_id)1357 int iort_iommu_configure_id(struct device *dev, const u32 *input_id)
1358 { return -ENODEV; }
1359 #endif
1360
nc_dma_get_range(struct device * dev,u64 * size)1361 static int nc_dma_get_range(struct device *dev, u64 *size)
1362 {
1363 struct acpi_iort_node *node;
1364 struct acpi_iort_named_component *ncomp;
1365
1366 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1367 iort_match_node_callback, dev);
1368 if (!node)
1369 return -ENODEV;
1370
1371 ncomp = (struct acpi_iort_named_component *)node->node_data;
1372
1373 if (!ncomp->memory_address_limit) {
1374 pr_warn(FW_BUG "Named component missing memory address limit\n");
1375 return -EINVAL;
1376 }
1377
1378 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1379 1ULL<<ncomp->memory_address_limit;
1380
1381 return 0;
1382 }
1383
rc_dma_get_range(struct device * dev,u64 * size)1384 static int rc_dma_get_range(struct device *dev, u64 *size)
1385 {
1386 struct acpi_iort_node *node;
1387 struct acpi_iort_root_complex *rc;
1388 struct pci_bus *pbus = to_pci_dev(dev)->bus;
1389
1390 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1391 iort_match_node_callback, &pbus->dev);
1392 if (!node || node->revision < 1)
1393 return -ENODEV;
1394
1395 rc = (struct acpi_iort_root_complex *)node->node_data;
1396
1397 if (!rc->memory_address_limit) {
1398 pr_warn(FW_BUG "Root complex missing memory address limit\n");
1399 return -EINVAL;
1400 }
1401
1402 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1403 1ULL<<rc->memory_address_limit;
1404
1405 return 0;
1406 }
1407
1408 /**
1409 * iort_dma_get_ranges() - Look up DMA addressing limit for the device
1410 * @dev: device to lookup
1411 * @size: DMA range size result pointer
1412 *
1413 * Return: 0 on success, an error otherwise.
1414 */
iort_dma_get_ranges(struct device * dev,u64 * size)1415 int iort_dma_get_ranges(struct device *dev, u64 *size)
1416 {
1417 if (dev_is_pci(dev))
1418 return rc_dma_get_range(dev, size);
1419 else
1420 return nc_dma_get_range(dev, size);
1421 }
1422
acpi_iort_register_irq(int hwirq,const char * name,int trigger,struct resource * res)1423 static void __init acpi_iort_register_irq(int hwirq, const char *name,
1424 int trigger,
1425 struct resource *res)
1426 {
1427 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1428 ACPI_ACTIVE_HIGH);
1429
1430 if (irq <= 0) {
1431 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1432 name);
1433 return;
1434 }
1435
1436 res->start = irq;
1437 res->end = irq;
1438 res->flags = IORESOURCE_IRQ;
1439 res->name = name;
1440 }
1441
arm_smmu_v3_count_resources(struct acpi_iort_node * node)1442 static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1443 {
1444 struct acpi_iort_smmu_v3 *smmu;
1445 /* Always present mem resource */
1446 int num_res = 1;
1447
1448 /* Retrieve SMMUv3 specific data */
1449 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1450
1451 if (smmu->event_gsiv)
1452 num_res++;
1453
1454 if (smmu->pri_gsiv)
1455 num_res++;
1456
1457 if (smmu->gerr_gsiv)
1458 num_res++;
1459
1460 if (smmu->sync_gsiv)
1461 num_res++;
1462
1463 return num_res;
1464 }
1465
arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 * smmu)1466 static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1467 {
1468 /*
1469 * Cavium ThunderX2 implementation doesn't not support unique
1470 * irq line. Use single irq line for all the SMMUv3 interrupts.
1471 */
1472 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1473 return false;
1474
1475 /*
1476 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1477 * SPI numbers here.
1478 */
1479 return smmu->event_gsiv == smmu->pri_gsiv &&
1480 smmu->event_gsiv == smmu->gerr_gsiv &&
1481 smmu->event_gsiv == smmu->sync_gsiv;
1482 }
1483
arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 * smmu)1484 static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1485 {
1486 /*
1487 * Override the size, for Cavium ThunderX2 implementation
1488 * which doesn't support the page 1 SMMU register space.
1489 */
1490 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1491 return SZ_64K;
1492
1493 return SZ_128K;
1494 }
1495
arm_smmu_v3_init_resources(struct resource * res,struct acpi_iort_node * node)1496 static void __init arm_smmu_v3_init_resources(struct resource *res,
1497 struct acpi_iort_node *node)
1498 {
1499 struct acpi_iort_smmu_v3 *smmu;
1500 int num_res = 0;
1501
1502 /* Retrieve SMMUv3 specific data */
1503 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1504
1505 res[num_res].start = smmu->base_address;
1506 res[num_res].end = smmu->base_address +
1507 arm_smmu_v3_resource_size(smmu) - 1;
1508 res[num_res].flags = IORESOURCE_MEM;
1509
1510 num_res++;
1511 if (arm_smmu_v3_is_combined_irq(smmu)) {
1512 if (smmu->event_gsiv)
1513 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1514 ACPI_EDGE_SENSITIVE,
1515 &res[num_res++]);
1516 } else {
1517
1518 if (smmu->event_gsiv)
1519 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1520 ACPI_EDGE_SENSITIVE,
1521 &res[num_res++]);
1522
1523 if (smmu->pri_gsiv)
1524 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1525 ACPI_EDGE_SENSITIVE,
1526 &res[num_res++]);
1527
1528 if (smmu->gerr_gsiv)
1529 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1530 ACPI_EDGE_SENSITIVE,
1531 &res[num_res++]);
1532
1533 if (smmu->sync_gsiv)
1534 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1535 ACPI_EDGE_SENSITIVE,
1536 &res[num_res++]);
1537 }
1538 }
1539
arm_smmu_v3_dma_configure(struct device * dev,struct acpi_iort_node * node)1540 static void __init arm_smmu_v3_dma_configure(struct device *dev,
1541 struct acpi_iort_node *node)
1542 {
1543 struct acpi_iort_smmu_v3 *smmu;
1544 enum dev_dma_attr attr;
1545
1546 /* Retrieve SMMUv3 specific data */
1547 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1548
1549 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1550 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1551
1552 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1553 dev->dma_mask = &dev->coherent_dma_mask;
1554
1555 /* Configure DMA for the page table walker */
1556 acpi_dma_configure(dev, attr);
1557 }
1558
1559 #if defined(CONFIG_ACPI_NUMA)
1560 /*
1561 * set numa proximity domain for smmuv3 device
1562 */
arm_smmu_v3_set_proximity(struct device * dev,struct acpi_iort_node * node)1563 static int __init arm_smmu_v3_set_proximity(struct device *dev,
1564 struct acpi_iort_node *node)
1565 {
1566 struct acpi_iort_smmu_v3 *smmu;
1567
1568 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1569 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1570 int dev_node = pxm_to_node(smmu->pxm);
1571
1572 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
1573 return -EINVAL;
1574
1575 set_dev_node(dev, dev_node);
1576 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1577 smmu->base_address,
1578 smmu->pxm);
1579 }
1580 return 0;
1581 }
1582 #else
1583 #define arm_smmu_v3_set_proximity NULL
1584 #endif
1585
arm_smmu_count_resources(struct acpi_iort_node * node)1586 static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1587 {
1588 struct acpi_iort_smmu *smmu;
1589
1590 /* Retrieve SMMU specific data */
1591 smmu = (struct acpi_iort_smmu *)node->node_data;
1592
1593 /*
1594 * Only consider the global fault interrupt and ignore the
1595 * configuration access interrupt.
1596 *
1597 * MMIO address and global fault interrupt resources are always
1598 * present so add them to the context interrupt count as a static
1599 * value.
1600 */
1601 return smmu->context_interrupt_count + 2;
1602 }
1603
arm_smmu_init_resources(struct resource * res,struct acpi_iort_node * node)1604 static void __init arm_smmu_init_resources(struct resource *res,
1605 struct acpi_iort_node *node)
1606 {
1607 struct acpi_iort_smmu *smmu;
1608 int i, hw_irq, trigger, num_res = 0;
1609 u64 *ctx_irq, *glb_irq;
1610
1611 /* Retrieve SMMU specific data */
1612 smmu = (struct acpi_iort_smmu *)node->node_data;
1613
1614 res[num_res].start = smmu->base_address;
1615 res[num_res].end = smmu->base_address + smmu->span - 1;
1616 res[num_res].flags = IORESOURCE_MEM;
1617 num_res++;
1618
1619 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1620 /* Global IRQs */
1621 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1622 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1623
1624 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1625 &res[num_res++]);
1626
1627 /* Context IRQs */
1628 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1629 for (i = 0; i < smmu->context_interrupt_count; i++) {
1630 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1631 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1632
1633 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1634 &res[num_res++]);
1635 }
1636 }
1637
arm_smmu_dma_configure(struct device * dev,struct acpi_iort_node * node)1638 static void __init arm_smmu_dma_configure(struct device *dev,
1639 struct acpi_iort_node *node)
1640 {
1641 struct acpi_iort_smmu *smmu;
1642 enum dev_dma_attr attr;
1643
1644 /* Retrieve SMMU specific data */
1645 smmu = (struct acpi_iort_smmu *)node->node_data;
1646
1647 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1648 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1649
1650 /* We expect the dma masks to be equivalent for SMMU set-ups */
1651 dev->dma_mask = &dev->coherent_dma_mask;
1652
1653 /* Configure DMA for the page table walker */
1654 acpi_dma_configure(dev, attr);
1655 }
1656
arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node * node)1657 static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1658 {
1659 struct acpi_iort_pmcg *pmcg;
1660
1661 /* Retrieve PMCG specific data */
1662 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1663
1664 /*
1665 * There are always 2 memory resources.
1666 * If the overflow_gsiv is present then add that for a total of 3.
1667 */
1668 return pmcg->overflow_gsiv ? 3 : 2;
1669 }
1670
arm_smmu_v3_pmcg_init_resources(struct resource * res,struct acpi_iort_node * node)1671 static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1672 struct acpi_iort_node *node)
1673 {
1674 struct acpi_iort_pmcg *pmcg;
1675
1676 /* Retrieve PMCG specific data */
1677 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1678
1679 res[0].start = pmcg->page0_base_address;
1680 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1681 res[0].flags = IORESOURCE_MEM;
1682 /*
1683 * The initial version in DEN0049C lacked a way to describe register
1684 * page 1, which makes it broken for most PMCG implementations; in
1685 * that case, just let the driver fail gracefully if it expects to
1686 * find a second memory resource.
1687 */
1688 if (node->revision > 0) {
1689 res[1].start = pmcg->page1_base_address;
1690 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1691 res[1].flags = IORESOURCE_MEM;
1692 }
1693
1694 if (pmcg->overflow_gsiv)
1695 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1696 ACPI_EDGE_SENSITIVE, &res[2]);
1697 }
1698
1699 static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1700 /* HiSilicon Hip08 Platform */
1701 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1702 "Erratum #162001800, Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP08},
1703 /* HiSilicon Hip09 Platform */
1704 {"HISI ", "HIP09 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1705 "Erratum #162001900", IORT_SMMU_V3_PMCG_HISI_HIP09},
1706 { }
1707 };
1708
arm_smmu_v3_pmcg_add_platdata(struct platform_device * pdev)1709 static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1710 {
1711 u32 model;
1712 int idx;
1713
1714 idx = acpi_match_platform_list(pmcg_plat_info);
1715 if (idx >= 0)
1716 model = pmcg_plat_info[idx].data;
1717 else
1718 model = IORT_SMMU_V3_PMCG_GENERIC;
1719
1720 return platform_device_add_data(pdev, &model, sizeof(model));
1721 }
1722
1723 struct iort_dev_config {
1724 const char *name;
1725 int (*dev_init)(struct acpi_iort_node *node);
1726 void (*dev_dma_configure)(struct device *dev,
1727 struct acpi_iort_node *node);
1728 int (*dev_count_resources)(struct acpi_iort_node *node);
1729 void (*dev_init_resources)(struct resource *res,
1730 struct acpi_iort_node *node);
1731 int (*dev_set_proximity)(struct device *dev,
1732 struct acpi_iort_node *node);
1733 int (*dev_add_platdata)(struct platform_device *pdev);
1734 };
1735
1736 static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
1737 .name = "arm-smmu-v3",
1738 .dev_dma_configure = arm_smmu_v3_dma_configure,
1739 .dev_count_resources = arm_smmu_v3_count_resources,
1740 .dev_init_resources = arm_smmu_v3_init_resources,
1741 .dev_set_proximity = arm_smmu_v3_set_proximity,
1742 };
1743
1744 static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
1745 .name = "arm-smmu",
1746 .dev_dma_configure = arm_smmu_dma_configure,
1747 .dev_count_resources = arm_smmu_count_resources,
1748 .dev_init_resources = arm_smmu_init_resources,
1749 };
1750
1751 static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1752 .name = "arm-smmu-v3-pmcg",
1753 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1754 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1755 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
1756 };
1757
iort_get_dev_cfg(struct acpi_iort_node * node)1758 static __init const struct iort_dev_config *iort_get_dev_cfg(
1759 struct acpi_iort_node *node)
1760 {
1761 switch (node->type) {
1762 case ACPI_IORT_NODE_SMMU_V3:
1763 return &iort_arm_smmu_v3_cfg;
1764 case ACPI_IORT_NODE_SMMU:
1765 return &iort_arm_smmu_cfg;
1766 case ACPI_IORT_NODE_PMCG:
1767 return &iort_arm_smmu_v3_pmcg_cfg;
1768 default:
1769 return NULL;
1770 }
1771 }
1772
1773 /**
1774 * iort_add_platform_device() - Allocate a platform device for IORT node
1775 * @node: Pointer to device ACPI IORT node
1776 * @ops: Pointer to IORT device config struct
1777 *
1778 * Returns: 0 on success, <0 failure
1779 */
iort_add_platform_device(struct acpi_iort_node * node,const struct iort_dev_config * ops)1780 static int __init iort_add_platform_device(struct acpi_iort_node *node,
1781 const struct iort_dev_config *ops)
1782 {
1783 struct fwnode_handle *fwnode;
1784 struct platform_device *pdev;
1785 struct resource *r;
1786 int ret, count;
1787
1788 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1789 if (!pdev)
1790 return -ENOMEM;
1791
1792 if (ops->dev_set_proximity) {
1793 ret = ops->dev_set_proximity(&pdev->dev, node);
1794 if (ret)
1795 goto dev_put;
1796 }
1797
1798 count = ops->dev_count_resources(node);
1799
1800 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1801 if (!r) {
1802 ret = -ENOMEM;
1803 goto dev_put;
1804 }
1805
1806 ops->dev_init_resources(r, node);
1807
1808 ret = platform_device_add_resources(pdev, r, count);
1809 /*
1810 * Resources are duplicated in platform_device_add_resources,
1811 * free their allocated memory
1812 */
1813 kfree(r);
1814
1815 if (ret)
1816 goto dev_put;
1817
1818 /*
1819 * Platform devices based on PMCG nodes uses platform_data to
1820 * pass the hardware model info to the driver. For others, add
1821 * a copy of IORT node pointer to platform_data to be used to
1822 * retrieve IORT data information.
1823 */
1824 if (ops->dev_add_platdata)
1825 ret = ops->dev_add_platdata(pdev);
1826 else
1827 ret = platform_device_add_data(pdev, &node, sizeof(node));
1828
1829 if (ret)
1830 goto dev_put;
1831
1832 fwnode = iort_get_fwnode(node);
1833
1834 if (!fwnode) {
1835 ret = -ENODEV;
1836 goto dev_put;
1837 }
1838
1839 pdev->dev.fwnode = fwnode;
1840
1841 if (ops->dev_dma_configure)
1842 ops->dev_dma_configure(&pdev->dev, node);
1843
1844 iort_set_device_domain(&pdev->dev, node);
1845
1846 ret = platform_device_add(pdev);
1847 if (ret)
1848 goto dma_deconfigure;
1849
1850 return 0;
1851
1852 dma_deconfigure:
1853 arch_teardown_dma_ops(&pdev->dev);
1854 dev_put:
1855 platform_device_put(pdev);
1856
1857 return ret;
1858 }
1859
1860 #ifdef CONFIG_PCI
iort_enable_acs(struct acpi_iort_node * iort_node)1861 static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
1862 {
1863 static bool acs_enabled __initdata;
1864
1865 if (acs_enabled)
1866 return;
1867
1868 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1869 struct acpi_iort_node *parent;
1870 struct acpi_iort_id_mapping *map;
1871 int i;
1872
1873 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1874 iort_node->mapping_offset);
1875
1876 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1877 if (!map->output_reference)
1878 continue;
1879
1880 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1881 iort_table, map->output_reference);
1882 /*
1883 * If we detect a RC->SMMU mapping, make sure
1884 * we enable ACS on the system.
1885 */
1886 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1887 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1888 pci_request_acs();
1889 acs_enabled = true;
1890 return;
1891 }
1892 }
1893 }
1894 }
1895 #else
iort_enable_acs(struct acpi_iort_node * iort_node)1896 static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1897 #endif
1898
iort_init_platform_devices(void)1899 static void __init iort_init_platform_devices(void)
1900 {
1901 struct acpi_iort_node *iort_node, *iort_end;
1902 struct acpi_table_iort *iort;
1903 struct fwnode_handle *fwnode;
1904 int i, ret;
1905 const struct iort_dev_config *ops;
1906
1907 /*
1908 * iort_table and iort both point to the start of IORT table, but
1909 * have different struct types
1910 */
1911 iort = (struct acpi_table_iort *)iort_table;
1912
1913 /* Get the first IORT node */
1914 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1915 iort->node_offset);
1916 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1917 iort_table->length);
1918
1919 for (i = 0; i < iort->node_count; i++) {
1920 if (iort_node >= iort_end) {
1921 pr_err("iort node pointer overflows, bad table\n");
1922 return;
1923 }
1924
1925 iort_enable_acs(iort_node);
1926
1927 ops = iort_get_dev_cfg(iort_node);
1928 if (ops) {
1929 fwnode = acpi_alloc_fwnode_static();
1930 if (!fwnode)
1931 return;
1932
1933 iort_set_fwnode(iort_node, fwnode);
1934
1935 ret = iort_add_platform_device(iort_node, ops);
1936 if (ret) {
1937 iort_delete_fwnode(iort_node);
1938 acpi_free_fwnode_static(fwnode);
1939 return;
1940 }
1941 }
1942
1943 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1944 iort_node->length);
1945 }
1946 }
1947
acpi_iort_init(void)1948 void __init acpi_iort_init(void)
1949 {
1950 acpi_status status;
1951
1952 /* iort_table will be used at runtime after the iort init,
1953 * so we don't need to call acpi_put_table() to release
1954 * the IORT table mapping.
1955 */
1956 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
1957 if (ACPI_FAILURE(status)) {
1958 if (status != AE_NOT_FOUND) {
1959 const char *msg = acpi_format_exception(status);
1960
1961 pr_err("Failed to get table, %s\n", msg);
1962 }
1963
1964 return;
1965 }
1966
1967 iort_init_platform_devices();
1968 }
1969
1970 #ifdef CONFIG_ZONE_DMA
1971 /*
1972 * Extract the highest CPU physical address accessible to all DMA masters in
1973 * the system. PHYS_ADDR_MAX is returned when no constrained device is found.
1974 */
acpi_iort_dma_get_max_cpu_address(void)1975 phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
1976 {
1977 phys_addr_t limit = PHYS_ADDR_MAX;
1978 struct acpi_iort_node *node, *end;
1979 struct acpi_table_iort *iort;
1980 acpi_status status;
1981 int i;
1982
1983 if (acpi_disabled)
1984 return limit;
1985
1986 status = acpi_get_table(ACPI_SIG_IORT, 0,
1987 (struct acpi_table_header **)&iort);
1988 if (ACPI_FAILURE(status))
1989 return limit;
1990
1991 node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset);
1992 end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length);
1993
1994 for (i = 0; i < iort->node_count; i++) {
1995 if (node >= end)
1996 break;
1997
1998 switch (node->type) {
1999 struct acpi_iort_named_component *ncomp;
2000 struct acpi_iort_root_complex *rc;
2001 phys_addr_t local_limit;
2002
2003 case ACPI_IORT_NODE_NAMED_COMPONENT:
2004 ncomp = (struct acpi_iort_named_component *)node->node_data;
2005 local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
2006 limit = min_not_zero(limit, local_limit);
2007 break;
2008
2009 case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
2010 if (node->revision < 1)
2011 break;
2012
2013 rc = (struct acpi_iort_root_complex *)node->node_data;
2014 local_limit = DMA_BIT_MASK(rc->memory_address_limit);
2015 limit = min_not_zero(limit, local_limit);
2016 break;
2017 }
2018 node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length);
2019 }
2020 acpi_put_table(&iort->header);
2021 return limit;
2022 }
2023 #endif
2024