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