blob: 2494138a6905ed105009894a8eb49ed2ee4ac4e6 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02002/*
3 * Copyright (C) 2016, Semihalf
4 * Author: Tomasz Nowicki <tn@semihalf.com>
5 *
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02006 * 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>
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +010014#include <linux/bitfield.h>
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +000015#include <linux/iommu.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020016#include <linux/kernel.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000017#include <linux/list.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020018#include <linux/pci.h>
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +000019#include <linux/platform_device.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000020#include <linux/slab.h>
Christoph Hellwig0a0f0d82020-09-22 15:31:03 +020021#include <linux/dma-map-ops.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020022
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000023#define IORT_TYPE_MASK(type) (1 << (type))
24#define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +000025#define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
26 (1 << ACPI_IORT_NODE_SMMU_V3))
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000027
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020028struct iort_its_msi_chip {
29 struct list_head list;
30 struct fwnode_handle *fw_node;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +000031 phys_addr_t base_addr;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020032 u32 translation_id;
33};
34
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000035struct iort_fwnode {
36 struct list_head list;
37 struct acpi_iort_node *iort_node;
38 struct fwnode_handle *fwnode;
39};
40static LIST_HEAD(iort_fwnode_list);
41static 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 *
Shiju Jose774c4a32020-10-14 10:31:39 +010047 * @iort_node: IORT table node associated with the IOMMU
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000048 * @fwnode: fwnode associated with the IORT node
49 *
50 * Returns: 0 on success
51 * <0 on failure
52 */
53static 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 */
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +010081static inline struct fwnode_handle *iort_get_fwnode(
82 struct acpi_iort_node *node)
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000083{
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 */
104static 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
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800119/**
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 */
126static 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
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200144typedef acpi_status (*iort_find_node_callback)
145 (struct acpi_iort_node *node, void *context);
146
147/* Root pointer to the mapped IORT table */
148static struct acpi_table_header *iort_table;
149
150static LIST_HEAD(iort_msi_chip_list);
151static DEFINE_SPINLOCK(iort_msi_chip_lock);
152
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200153/**
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000154 * 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.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200156 * @trans_id: ITS ID.
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000157 * @base: ITS base address.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200158 * @fw_node: Domain token.
159 *
160 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
161 */
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000162int iort_register_domain_token(int trans_id, phys_addr_t base,
163 struct fwnode_handle *fw_node)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200164{
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;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000173 its_msi_chip->base_addr = base;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200174
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 */
188void 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 */
209struct 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
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200226static 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)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800251 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200252
253 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
254 iort_node->length);
255 }
256
257 return NULL;
258}
259
260static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
261 void *context)
262{
263 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800264 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200265
266 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
267 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100268 struct acpi_device *adev;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200269 struct acpi_iort_named_component *ncomp;
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100270 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);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200286
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800287 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200288 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200289
290 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
291 if (ACPI_FAILURE(status)) {
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100292 dev_warn(nc_dev, "Can't get device full path name\n");
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200293 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;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200314 }
315out:
316 return status;
317}
318
319static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200320 u32 *rid_out, bool check_overlap)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200321{
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
Ard Biesheuvel6d3b29d2020-05-01 18:10:13 +0200335 if (rid_in < map->input_base ||
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200336 (rid_in > map->input_base + map->id_count))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200337 return -ENXIO;
338
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200339 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;
Hanjun Guo44cdc7b2020-05-08 11:56:38 +0800352
353 pr_err(FW_BUG "applying workaround.\n");
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200354 }
355
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200356 *rid_out = map->output_base + (rid_in - map->input_base);
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200357
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;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200366 return 0;
367}
368
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100369static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
370 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000371{
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,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000380 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000381
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
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000392 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000393 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800394 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
Neil Leeder24e51602019-03-26 15:17:50 +0000395 node->type == ACPI_IORT_NODE_SMMU_V3 ||
396 node->type == ACPI_IORT_NODE_PMCG) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000397 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000398 return parent;
399 }
400 }
401
402 return NULL;
403}
404
Hanjun Guo86456a32017-10-13 15:09:49 +0800405static int iort_get_id_mapping_index(struct acpi_iort_node *node)
406{
407 struct acpi_iort_smmu_v3 *smmu;
Tuan Phan50c8ab82020-05-20 10:13:07 -0700408 struct acpi_iort_pmcg *pmcg;
Hanjun Guo86456a32017-10-13 15:09:49 +0800409
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;
Neil Leeder24e51602019-03-26 15:17:50 +0000435 case ACPI_IORT_NODE_PMCG:
Tuan Phan50c8ab82020-05-20 10:13:07 -0700436 pmcg = (struct acpi_iort_pmcg *)node->node_data;
437 if (pmcg->overflow_gsiv || node->mapping_count == 0)
438 return -EINVAL;
439
Neil Leeder24e51602019-03-26 15:17:50 +0000440 return 0;
Hanjun Guo86456a32017-10-13 15:09:49 +0800441 default:
442 return -EINVAL;
443 }
444}
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800445
Hanjun Guo697f6092017-03-07 20:40:03 +0800446static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
447 u32 id_in, u32 *id_out,
448 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200449{
Hanjun Guo697f6092017-03-07 20:40:03 +0800450 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200451
452 /* Parse the ID mapping tree to find specified node type */
453 while (node) {
454 struct acpi_iort_id_mapping *map;
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200455 int i, index, rc = 0;
456 u32 out_ref = 0, map_id = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200457
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000458 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800459 if (id_out)
460 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200461 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
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800477 /*
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
Hanjun Guo697f6092017-03-07 20:40:03 +0800484 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200485 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800486 /* if it is special mapping index, skip it */
487 if (i == index)
488 continue;
489
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200490 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
491 if (!rc)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200492 break;
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200493 if (rc == -EAGAIN)
494 out_ref = map->output_reference;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200495 }
496
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200497 if (i == node->mapping_count && !out_ref)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200498 goto fail_map;
499
500 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200501 rc ? out_ref : map->output_reference);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200502 }
503
504fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800505 /* Map input ID to output ID unchanged on mapping failure */
506 if (id_out)
507 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200508
509 return NULL;
510}
511
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100512static struct acpi_iort_node *iort_node_map_platform_id(
513 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
514 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800515{
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
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200539static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
540{
541 struct pci_bus *pbus;
542
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800543 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;
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800554 /*
555 * if not, then it should be a platform device defined in
556 * DSDT/SSDT (with Named Component node in IORT)
557 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200558 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
559 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800560 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200561
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200562 pbus = to_pci_dev(dev)->bus;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200563
564 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
565 iort_match_node_callback, &pbus->dev);
566}
567
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200568/**
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100569 * iort_msi_map_id() - Map a MSI input ID for a device
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200570 * @dev: The device for which the mapping is to be done.
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100571 * @input_id: The device input ID.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200572 *
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100573 * Returns: mapped MSI ID on success, input ID otherwise
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200574 */
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100575u32 iort_msi_map_id(struct device *dev, u32 input_id)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200576{
577 struct acpi_iort_node *node;
578 u32 dev_id;
579
580 node = iort_find_dev_node(dev);
581 if (!node)
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100582 return input_id;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200583
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100584 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200585 return dev_id;
586}
587
588/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800589 * 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 */
595int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
596{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800597 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800598 struct acpi_iort_node *node;
599
600 node = iort_find_dev_node(dev);
601 if (!node)
602 return -ENODEV;
603
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800604 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))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800608 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800609 } 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 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800615 }
616
617 return -ENODEV;
618}
619
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000620static 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
Hanjun Guoae7c1832017-03-07 20:40:05 +0800638/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200639 * iort_dev_find_its_id() - Find the ITS identifier for a device
640 * @dev: The device.
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100641 * @id: Device's ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200642 * @idx: Index of the ITS identifier list.
643 * @its_id: ITS identifier.
644 *
645 * Returns: 0 on success, appropriate error value otherwise
646 */
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100647static int iort_dev_find_its_id(struct device *dev, u32 id,
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200648 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
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100657 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200658 if (!node)
659 return -ENXIO;
660
661 /* Move to ITS specific data */
662 its = (struct acpi_iort_its_group *)node->node_data;
Lorenzo Pieralisi5a46d3f2019-07-22 17:25:48 +0100663 if (idx >= its->its_count) {
664 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200665 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.
Shiju Jose774c4a32020-10-14 10:31:39 +0100676 * @id: Requester ID for the device.
677 * @bus_token: irq domain bus token.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200678 *
679 * Returns: the MSI domain for this device, NULL otherwise
680 */
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100681struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
682 enum irq_domain_bus_token bus_token)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200683{
684 struct fwnode_handle *handle;
685 int its_id;
686
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100687 if (iort_dev_find_its_id(dev, id, 0, &its_id))
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200688 return NULL;
689
690 handle = iort_find_domain_token(its_id);
691 if (!handle)
692 return NULL;
693
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100694 return irq_find_matching_fwnode(handle, bus_token);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200695}
696
Lorenzo Pieralisi65637902017-10-13 15:09:50 +0800697static 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
Hanjun Guod4f54a12017-03-07 20:40:06 +0800740/**
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 */
747static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
748{
Lorenzo Pieralisiea2412d2018-11-29 09:55:59 +0000749 struct acpi_iort_node *node, *msi_parent = NULL;
Hanjun Guod4f54a12017-03-07 20:40:06 +0800750 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
781void 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
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100790#ifdef CONFIG_IOMMU_API
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000791static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
792{
793 struct acpi_iort_node *iommu;
Joerg Roedel8097e532018-11-29 14:01:00 +0100794 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000795
796 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
797
798 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
799 struct acpi_iort_smmu_v3 *smmu;
800
801 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
802 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
803 return iommu;
804 }
805
806 return NULL;
807}
808
Joerg Roedel8097e532018-11-29 14:01:00 +0100809static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100810{
Joerg Roedel8097e532018-11-29 14:01:00 +0100811 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
812
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100813 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
814}
815
Zenghui Yu1ab64cf2020-08-18 14:36:24 +0800816static inline int iort_add_device_replay(struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100817{
818 int err = 0;
819
Joerg Roedeld2e1a002018-12-05 14:39:45 +0100820 if (dev->bus && !device_iommu_mapped(dev))
821 err = iommu_probe_device(dev);
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100822
823 return err;
824}
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000825
826/**
827 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
828 * @dev: Device from iommu_get_resv_regions()
829 * @head: Reserved region list from iommu_get_resv_regions()
830 *
831 * Returns: Number of msi reserved regions on success (0 if platform
832 * doesn't require the reservation or no associated msi regions),
833 * appropriate error value otherwise. The ITS interrupt translation
834 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
835 * are the msi reserved regions.
836 */
837int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
838{
Joerg Roedel8097e532018-11-29 14:01:00 +0100839 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000840 struct acpi_iort_its_group *its;
841 struct acpi_iort_node *iommu_node, *its_node = NULL;
842 int i, resv = 0;
843
844 iommu_node = iort_get_msi_resv_iommu(dev);
845 if (!iommu_node)
846 return 0;
847
848 /*
849 * Current logic to reserve ITS regions relies on HW topologies
850 * where a given PCI or named component maps its IDs to only one
851 * ITS group; if a PCI or named component can map its IDs to
852 * different ITS groups through IORT mappings this function has
853 * to be reworked to ensure we reserve regions for all ITS groups
854 * a given PCI or named component may map IDs to.
855 */
856
Joerg Roedel8097e532018-11-29 14:01:00 +0100857 for (i = 0; i < fwspec->num_ids; i++) {
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000858 its_node = iort_node_map_id(iommu_node,
Joerg Roedel8097e532018-11-29 14:01:00 +0100859 fwspec->ids[i],
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000860 NULL, IORT_MSI_TYPE);
861 if (its_node)
862 break;
863 }
864
865 if (!its_node)
866 return 0;
867
868 /* Move to ITS specific data */
869 its = (struct acpi_iort_its_group *)its_node->node_data;
870
871 for (i = 0; i < its->its_count; i++) {
872 phys_addr_t base;
873
874 if (!iort_find_its_base(its->identifiers[i], &base)) {
875 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
876 struct iommu_resv_region *region;
877
878 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
879 prot, IOMMU_RESV_MSI);
880 if (region) {
881 list_add_tail(&region->list, head);
882 resv++;
883 }
884 }
885 }
886
887 return (resv == its->its_count) ? resv : -ENODEV;
888}
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100889
890static inline bool iort_iommu_driver_enabled(u8 type)
891{
892 switch (type) {
893 case ACPI_IORT_NODE_SMMU_V3:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000894 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100895 case ACPI_IORT_NODE_SMMU:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000896 return IS_ENABLED(CONFIG_ARM_SMMU);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100897 default:
898 pr_warn("IORT node type %u does not describe an SMMU\n", type);
899 return false;
900 }
901}
902
903static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
904 struct fwnode_handle *fwnode,
905 const struct iommu_ops *ops)
906{
907 int ret = iommu_fwspec_init(dev, fwnode, ops);
908
909 if (!ret)
910 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
911
912 return ret;
913}
914
915static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
916{
917 struct acpi_iort_root_complex *pci_rc;
918
919 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
920 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
921}
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100922
Robin Murphybc8648d2017-08-04 17:42:06 +0100923static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
924 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000925{
Robin Murphybc8648d2017-08-04 17:42:06 +0100926 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000927 struct fwnode_handle *iort_fwnode;
928
Robin Murphybc8648d2017-08-04 17:42:06 +0100929 if (!node)
930 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000931
Robin Murphybc8648d2017-08-04 17:42:06 +0100932 iort_fwnode = iort_get_fwnode(node);
933 if (!iort_fwnode)
934 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000935
Robin Murphybc8648d2017-08-04 17:42:06 +0100936 /*
937 * If the ops look-up fails, this means that either
938 * the SMMU drivers have not been probed yet or that
939 * the SMMU drivers are not built in the kernel;
940 * Depending on whether the SMMU drivers are built-in
941 * in the kernel or not, defer the IOMMU configuration
942 * or just abort it.
943 */
944 ops = iommu_ops_from_fwnode(iort_fwnode);
945 if (!ops)
946 return iort_iommu_driver_enabled(node->type) ?
947 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000948
Robin Murphybc8648d2017-08-04 17:42:06 +0100949 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
950}
951
952struct iort_pci_alias_info {
953 struct device *dev;
954 struct acpi_iort_node *node;
955};
956
957static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
958{
959 struct iort_pci_alias_info *info = data;
960 struct acpi_iort_node *parent;
961 u32 streamid;
962
963 parent = iort_node_map_id(info->node, alias, &streamid,
964 IORT_IOMMU_TYPE);
965 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000966}
967
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +0100968static void iort_named_component_init(struct device *dev,
969 struct acpi_iort_node *node)
970{
971 struct acpi_iort_named_component *nc;
972 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
973
974 if (!fwspec)
975 return;
976
977 nc = (struct acpi_iort_named_component *)node->node_data;
978 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
979 nc->node_flags);
980}
981
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +0100982static int iort_nc_iommu_map(struct device *dev, struct acpi_iort_node *node)
983{
984 struct acpi_iort_node *parent;
985 int err = -ENODEV, i = 0;
986 u32 streamid = 0;
987
988 do {
989
990 parent = iort_node_map_platform_id(node, &streamid,
991 IORT_IOMMU_TYPE,
992 i++);
993
994 if (parent)
995 err = iort_iommu_xlate(dev, parent, streamid);
996 } while (parent && !err);
997
998 return err;
999}
1000
1001static int iort_nc_iommu_map_id(struct device *dev,
1002 struct acpi_iort_node *node,
1003 const u32 *in_id)
1004{
1005 struct acpi_iort_node *parent;
1006 u32 streamid;
1007
1008 parent = iort_node_map_id(node, *in_id, &streamid, IORT_IOMMU_TYPE);
1009 if (parent)
1010 return iort_iommu_xlate(dev, parent, streamid);
1011
1012 return -ENODEV;
1013}
1014
1015
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001016/**
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001017 * iort_iommu_configure_id - Set-up IOMMU configuration for a device.
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001018 *
1019 * @dev: device to configure
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001020 * @id_in: optional input id const value pointer
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001021 *
1022 * Returns: iommu_ops pointer on configuration success
1023 * NULL on configuration failure
1024 */
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001025const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1026 const u32 *id_in)
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001027{
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001028 struct acpi_iort_node *node;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001029 const struct iommu_ops *ops;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001030 int err = -ENODEV;
1031
1032 /*
1033 * If we already translated the fwspec there
1034 * is nothing left to do, return the iommu_ops.
1035 */
1036 ops = iort_fwspec_iommu_ops(dev);
1037 if (ops)
1038 return ops;
1039
1040 if (dev_is_pci(dev)) {
Joerg Roedel6990ec72020-03-26 16:08:27 +01001041 struct iommu_fwspec *fwspec;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001042 struct pci_bus *bus = to_pci_dev(dev)->bus;
1043 struct iort_pci_alias_info info = { .dev = dev };
1044
1045 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1046 iort_match_node_callback, &bus->dev);
1047 if (!node)
1048 return NULL;
1049
1050 info.node = node;
1051 err = pci_for_each_dma_alias(to_pci_dev(dev),
1052 iort_pci_iommu_init, &info);
1053
Joerg Roedel6990ec72020-03-26 16:08:27 +01001054 fwspec = dev_iommu_fwspec_get(dev);
1055 if (fwspec && iort_pci_rc_supports_ats(node))
1056 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001057 } else {
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001058 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1059 iort_match_node_callback, dev);
1060 if (!node)
1061 return NULL;
1062
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001063 err = id_in ? iort_nc_iommu_map_id(dev, node, id_in) :
1064 iort_nc_iommu_map(dev, node);
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +01001065
1066 if (!err)
1067 iort_named_component_init(dev, node);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001068 }
1069
1070 /*
1071 * If we have reason to believe the IOMMU driver missed the initial
1072 * add_device callback for dev, replay it to get things in order.
1073 */
1074 if (!err) {
1075 ops = iort_fwspec_iommu_ops(dev);
Zenghui Yu1ab64cf2020-08-18 14:36:24 +08001076 err = iort_add_device_replay(dev);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001077 }
1078
1079 /* Ignore all other errors apart from EPROBE_DEFER */
1080 if (err == -EPROBE_DEFER) {
1081 ops = ERR_PTR(err);
1082 } else if (err) {
1083 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1084 ops = NULL;
1085 }
1086
1087 return ops;
1088}
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001089
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001090#else
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001091int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1092{ return 0; }
Lorenzo Pieralisib8e069a2020-06-19 09:20:06 +01001093const struct iommu_ops *iort_iommu_configure_id(struct device *dev,
1094 const u32 *input_id)
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001095{ return NULL; }
1096#endif
1097
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001098static int nc_dma_get_range(struct device *dev, u64 *size)
1099{
1100 struct acpi_iort_node *node;
1101 struct acpi_iort_named_component *ncomp;
1102
1103 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1104 iort_match_node_callback, dev);
1105 if (!node)
1106 return -ENODEV;
1107
1108 ncomp = (struct acpi_iort_named_component *)node->node_data;
1109
Moritz Fischera1df8292021-01-21 17:24:19 -08001110 if (!ncomp->memory_address_limit) {
1111 pr_warn(FW_BUG "Named component missing memory address limit\n");
1112 return -EINVAL;
1113 }
1114
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001115 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1116 1ULL<<ncomp->memory_address_limit;
1117
1118 return 0;
1119}
1120
Robin Murphy5ac65e82018-07-23 23:16:06 +01001121static int rc_dma_get_range(struct device *dev, u64 *size)
1122{
1123 struct acpi_iort_node *node;
1124 struct acpi_iort_root_complex *rc;
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001125 struct pci_bus *pbus = to_pci_dev(dev)->bus;
Robin Murphy5ac65e82018-07-23 23:16:06 +01001126
1127 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001128 iort_match_node_callback, &pbus->dev);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001129 if (!node || node->revision < 1)
1130 return -ENODEV;
1131
1132 rc = (struct acpi_iort_root_complex *)node->node_data;
1133
Moritz Fischera1df8292021-01-21 17:24:19 -08001134 if (!rc->memory_address_limit) {
1135 pr_warn(FW_BUG "Root complex missing memory address limit\n");
1136 return -EINVAL;
1137 }
1138
Robin Murphy5ac65e82018-07-23 23:16:06 +01001139 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1140 1ULL<<rc->memory_address_limit;
1141
1142 return 0;
1143}
1144
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +00001145/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001146 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001147 *
1148 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001149 * @dma_addr: device DMA address result pointer
Shiju Jose774c4a32020-10-14 10:31:39 +01001150 * @dma_size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001151 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001152void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001153{
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001154 u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1155 int ret;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001156
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001157 /*
Robin Murphy6757cda2018-07-23 23:16:11 +01001158 * If @dev is expected to be DMA-capable then the bus code that created
1159 * it should have initialised its dma_mask pointer by this point. For
1160 * now, we'll continue the legacy behaviour of coercing it to the
1161 * coherent mask if not, but we'll no longer do so quietly.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001162 */
Robin Murphy6757cda2018-07-23 23:16:11 +01001163 if (!dev->dma_mask) {
1164 dev_warn(dev, "DMA mask not set\n");
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001165 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy6757cda2018-07-23 23:16:11 +01001166 }
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001167
Robin Murphy6757cda2018-07-23 23:16:11 +01001168 if (dev->coherent_dma_mask)
1169 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1170 else
1171 size = 1ULL << 32;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001172
Ard Biesheuvel7fb89e12020-04-20 11:27:53 +02001173 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1174 if (ret == -ENODEV)
1175 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1176 : nc_dma_get_range(dev, &size);
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001177
1178 if (!ret) {
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001179 /*
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001180 * Limit coherent and dma mask based on size retrieved from
1181 * firmware.
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001182 */
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001183 end = dmaaddr + size - 1;
1184 mask = DMA_BIT_MASK(ilog2(end) + 1);
1185 dev->bus_dma_limit = end;
Moritz Fischera1df8292021-01-21 17:24:19 -08001186 dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
1187 *dev->dma_mask = min(*dev->dma_mask, mask);
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001188 }
1189
1190 *dma_addr = dmaaddr;
1191 *dma_size = size;
1192
Jim Quinlane0d07272020-09-17 18:43:40 +02001193 ret = dma_direct_set_offset(dev, dmaaddr + offset, dmaaddr, size);
1194
1195 dev_dbg(dev, "dma_offset(%#08llx)%s\n", offset, ret ? " failed!" : "");
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001196}
1197
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001198static void __init acpi_iort_register_irq(int hwirq, const char *name,
1199 int trigger,
1200 struct resource *res)
1201{
1202 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1203 ACPI_ACTIVE_HIGH);
1204
1205 if (irq <= 0) {
1206 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1207 name);
1208 return;
1209 }
1210
1211 res->start = irq;
1212 res->end = irq;
1213 res->flags = IORESOURCE_IRQ;
1214 res->name = name;
1215}
1216
1217static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1218{
1219 struct acpi_iort_smmu_v3 *smmu;
1220 /* Always present mem resource */
1221 int num_res = 1;
1222
1223 /* Retrieve SMMUv3 specific data */
1224 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1225
1226 if (smmu->event_gsiv)
1227 num_res++;
1228
1229 if (smmu->pri_gsiv)
1230 num_res++;
1231
1232 if (smmu->gerr_gsiv)
1233 num_res++;
1234
1235 if (smmu->sync_gsiv)
1236 num_res++;
1237
1238 return num_res;
1239}
1240
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301241static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1242{
1243 /*
1244 * Cavium ThunderX2 implementation doesn't not support unique
1245 * irq line. Use single irq line for all the SMMUv3 interrupts.
1246 */
1247 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1248 return false;
1249
1250 /*
1251 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1252 * SPI numbers here.
1253 */
1254 return smmu->event_gsiv == smmu->pri_gsiv &&
1255 smmu->event_gsiv == smmu->gerr_gsiv &&
1256 smmu->event_gsiv == smmu->sync_gsiv;
1257}
1258
Linu Cherian403e8c72017-06-22 17:35:36 +05301259static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1260{
1261 /*
1262 * Override the size, for Cavium ThunderX2 implementation
1263 * which doesn't support the page 1 SMMU register space.
1264 */
1265 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1266 return SZ_64K;
1267
1268 return SZ_128K;
1269}
1270
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001271static void __init arm_smmu_v3_init_resources(struct resource *res,
1272 struct acpi_iort_node *node)
1273{
1274 struct acpi_iort_smmu_v3 *smmu;
1275 int num_res = 0;
1276
1277 /* Retrieve SMMUv3 specific data */
1278 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1279
1280 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301281 res[num_res].end = smmu->base_address +
1282 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001283 res[num_res].flags = IORESOURCE_MEM;
1284
1285 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301286 if (arm_smmu_v3_is_combined_irq(smmu)) {
1287 if (smmu->event_gsiv)
1288 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1289 ACPI_EDGE_SENSITIVE,
1290 &res[num_res++]);
1291 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001292
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301293 if (smmu->event_gsiv)
1294 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1295 ACPI_EDGE_SENSITIVE,
1296 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001297
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301298 if (smmu->pri_gsiv)
1299 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1300 ACPI_EDGE_SENSITIVE,
1301 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001302
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301303 if (smmu->gerr_gsiv)
1304 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1305 ACPI_EDGE_SENSITIVE,
1306 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001307
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301308 if (smmu->sync_gsiv)
1309 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1310 ACPI_EDGE_SENSITIVE,
1311 &res[num_res++]);
1312 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001313}
1314
Neil Leeder24e51602019-03-26 15:17:50 +00001315static void __init arm_smmu_v3_dma_configure(struct device *dev,
1316 struct acpi_iort_node *node)
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001317{
1318 struct acpi_iort_smmu_v3 *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001319 enum dev_dma_attr attr;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001320
1321 /* Retrieve SMMUv3 specific data */
1322 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1323
Neil Leeder24e51602019-03-26 15:17:50 +00001324 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1325 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1326
1327 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1328 dev->dma_mask = &dev->coherent_dma_mask;
1329
1330 /* Configure DMA for the page table walker */
1331 acpi_dma_configure(dev, attr);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001332}
1333
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001334#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001335/*
1336 * set numa proximity domain for smmuv3 device
1337 */
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001338static int __init arm_smmu_v3_set_proximity(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001339 struct acpi_iort_node *node)
1340{
1341 struct acpi_iort_smmu_v3 *smmu;
1342
1343 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1344 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
Jonathan Cameron01feba52020-08-18 22:24:26 +08001345 int dev_node = pxm_to_node(smmu->pxm);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001346
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001347 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001348 return -EINVAL;
1349
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001350 set_dev_node(dev, dev_node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001351 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1352 smmu->base_address,
1353 smmu->pxm);
1354 }
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001355 return 0;
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001356}
1357#else
1358#define arm_smmu_v3_set_proximity NULL
1359#endif
1360
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001361static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1362{
1363 struct acpi_iort_smmu *smmu;
1364
1365 /* Retrieve SMMU specific data */
1366 smmu = (struct acpi_iort_smmu *)node->node_data;
1367
1368 /*
1369 * Only consider the global fault interrupt and ignore the
1370 * configuration access interrupt.
1371 *
1372 * MMIO address and global fault interrupt resources are always
1373 * present so add them to the context interrupt count as a static
1374 * value.
1375 */
1376 return smmu->context_interrupt_count + 2;
1377}
1378
1379static void __init arm_smmu_init_resources(struct resource *res,
1380 struct acpi_iort_node *node)
1381{
1382 struct acpi_iort_smmu *smmu;
1383 int i, hw_irq, trigger, num_res = 0;
1384 u64 *ctx_irq, *glb_irq;
1385
1386 /* Retrieve SMMU specific data */
1387 smmu = (struct acpi_iort_smmu *)node->node_data;
1388
1389 res[num_res].start = smmu->base_address;
1390 res[num_res].end = smmu->base_address + smmu->span - 1;
1391 res[num_res].flags = IORESOURCE_MEM;
1392 num_res++;
1393
1394 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1395 /* Global IRQs */
1396 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1397 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1398
1399 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1400 &res[num_res++]);
1401
1402 /* Context IRQs */
1403 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1404 for (i = 0; i < smmu->context_interrupt_count; i++) {
1405 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1406 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1407
1408 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1409 &res[num_res++]);
1410 }
1411}
1412
Neil Leeder24e51602019-03-26 15:17:50 +00001413static void __init arm_smmu_dma_configure(struct device *dev,
1414 struct acpi_iort_node *node)
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001415{
1416 struct acpi_iort_smmu *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001417 enum dev_dma_attr attr;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001418
1419 /* Retrieve SMMU specific data */
1420 smmu = (struct acpi_iort_smmu *)node->node_data;
1421
Neil Leeder24e51602019-03-26 15:17:50 +00001422 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1423 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1424
1425 /* We expect the dma masks to be equivalent for SMMU set-ups */
1426 dev->dma_mask = &dev->coherent_dma_mask;
1427
1428 /* Configure DMA for the page table walker */
1429 acpi_dma_configure(dev, attr);
1430}
1431
1432static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1433{
1434 struct acpi_iort_pmcg *pmcg;
1435
1436 /* Retrieve PMCG specific data */
1437 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1438
1439 /*
1440 * There are always 2 memory resources.
1441 * If the overflow_gsiv is present then add that for a total of 3.
1442 */
1443 return pmcg->overflow_gsiv ? 3 : 2;
1444}
1445
1446static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1447 struct acpi_iort_node *node)
1448{
1449 struct acpi_iort_pmcg *pmcg;
1450
1451 /* Retrieve PMCG specific data */
1452 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1453
1454 res[0].start = pmcg->page0_base_address;
1455 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1456 res[0].flags = IORESOURCE_MEM;
1457 res[1].start = pmcg->page1_base_address;
1458 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1459 res[1].flags = IORESOURCE_MEM;
1460
1461 if (pmcg->overflow_gsiv)
1462 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1463 ACPI_EDGE_SENSITIVE, &res[2]);
1464}
1465
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001466static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1467 /* HiSilicon Hip08 Platform */
1468 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1469 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1470 { }
1471};
1472
Neil Leeder24e51602019-03-26 15:17:50 +00001473static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1474{
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001475 u32 model;
1476 int idx;
1477
1478 idx = acpi_match_platform_list(pmcg_plat_info);
1479 if (idx >= 0)
1480 model = pmcg_plat_info[idx].data;
1481 else
1482 model = IORT_SMMU_V3_PMCG_GENERIC;
Neil Leeder24e51602019-03-26 15:17:50 +00001483
1484 return platform_device_add_data(pdev, &model, sizeof(model));
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001485}
1486
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001487struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001488 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001489 int (*dev_init)(struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001490 void (*dev_dma_configure)(struct device *dev,
1491 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001492 int (*dev_count_resources)(struct acpi_iort_node *node);
1493 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001494 struct acpi_iort_node *node);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001495 int (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001496 struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001497 int (*dev_add_platdata)(struct platform_device *pdev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001498};
1499
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001500static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001501 .name = "arm-smmu-v3",
Neil Leeder24e51602019-03-26 15:17:50 +00001502 .dev_dma_configure = arm_smmu_v3_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001503 .dev_count_resources = arm_smmu_v3_count_resources,
1504 .dev_init_resources = arm_smmu_v3_init_resources,
1505 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001506};
1507
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001508static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001509 .name = "arm-smmu",
Neil Leeder24e51602019-03-26 15:17:50 +00001510 .dev_dma_configure = arm_smmu_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001511 .dev_count_resources = arm_smmu_count_resources,
Neil Leeder24e51602019-03-26 15:17:50 +00001512 .dev_init_resources = arm_smmu_init_resources,
1513};
1514
1515static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1516 .name = "arm-smmu-v3-pmcg",
1517 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1518 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1519 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001520};
1521
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001522static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001523 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001524{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001525 switch (node->type) {
1526 case ACPI_IORT_NODE_SMMU_V3:
1527 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001528 case ACPI_IORT_NODE_SMMU:
1529 return &iort_arm_smmu_cfg;
Neil Leeder24e51602019-03-26 15:17:50 +00001530 case ACPI_IORT_NODE_PMCG:
1531 return &iort_arm_smmu_v3_pmcg_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001532 default:
1533 return NULL;
1534 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001535}
1536
1537/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001538 * iort_add_platform_device() - Allocate a platform device for IORT node
1539 * @node: Pointer to device ACPI IORT node
Shiju Jose774c4a32020-10-14 10:31:39 +01001540 * @ops: Pointer to IORT device config struct
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001541 *
1542 * Returns: 0 on success, <0 failure
1543 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001544static int __init iort_add_platform_device(struct acpi_iort_node *node,
1545 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001546{
1547 struct fwnode_handle *fwnode;
1548 struct platform_device *pdev;
1549 struct resource *r;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001550 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001551
1552 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1553 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001554 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001555
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001556 if (ops->dev_set_proximity) {
1557 ret = ops->dev_set_proximity(&pdev->dev, node);
1558 if (ret)
1559 goto dev_put;
1560 }
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001561
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001562 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001563
1564 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1565 if (!r) {
1566 ret = -ENOMEM;
1567 goto dev_put;
1568 }
1569
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001570 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001571
1572 ret = platform_device_add_resources(pdev, r, count);
1573 /*
1574 * Resources are duplicated in platform_device_add_resources,
1575 * free their allocated memory
1576 */
1577 kfree(r);
1578
1579 if (ret)
1580 goto dev_put;
1581
1582 /*
Neil Leeder24e51602019-03-26 15:17:50 +00001583 * Platform devices based on PMCG nodes uses platform_data to
1584 * pass the hardware model info to the driver. For others, add
1585 * a copy of IORT node pointer to platform_data to be used to
1586 * retrieve IORT data information.
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001587 */
Neil Leeder24e51602019-03-26 15:17:50 +00001588 if (ops->dev_add_platdata)
1589 ret = ops->dev_add_platdata(pdev);
1590 else
1591 ret = platform_device_add_data(pdev, &node, sizeof(node));
1592
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001593 if (ret)
1594 goto dev_put;
1595
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001596 fwnode = iort_get_fwnode(node);
1597
1598 if (!fwnode) {
1599 ret = -ENODEV;
1600 goto dev_put;
1601 }
1602
1603 pdev->dev.fwnode = fwnode;
1604
Neil Leeder24e51602019-03-26 15:17:50 +00001605 if (ops->dev_dma_configure)
1606 ops->dev_dma_configure(&pdev->dev, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001607
Lorenzo Pieralisi65637902017-10-13 15:09:50 +08001608 iort_set_device_domain(&pdev->dev, node);
1609
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001610 ret = platform_device_add(pdev);
1611 if (ret)
1612 goto dma_deconfigure;
1613
1614 return 0;
1615
1616dma_deconfigure:
Christoph Hellwigdc3c0552018-08-24 10:28:18 +02001617 arch_teardown_dma_ops(&pdev->dev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001618dev_put:
1619 platform_device_put(pdev);
1620
1621 return ret;
1622}
1623
Sinan Kaya43554ce2018-12-19 22:46:58 +00001624#ifdef CONFIG_PCI
1625static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001626{
Sinan Kaya43554ce2018-12-19 22:46:58 +00001627 static bool acs_enabled __initdata;
1628
1629 if (acs_enabled)
1630 return;
1631
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001632 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1633 struct acpi_iort_node *parent;
1634 struct acpi_iort_id_mapping *map;
1635 int i;
1636
1637 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1638 iort_node->mapping_offset);
1639
1640 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1641 if (!map->output_reference)
1642 continue;
1643
1644 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1645 iort_table, map->output_reference);
1646 /*
1647 * If we detect a RC->SMMU mapping, make sure
1648 * we enable ACS on the system.
1649 */
1650 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1651 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1652 pci_request_acs();
Sinan Kaya43554ce2018-12-19 22:46:58 +00001653 acs_enabled = true;
1654 return;
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001655 }
1656 }
1657 }
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001658}
Sinan Kaya43554ce2018-12-19 22:46:58 +00001659#else
1660static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1661#endif
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001662
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001663static void __init iort_init_platform_devices(void)
1664{
1665 struct acpi_iort_node *iort_node, *iort_end;
1666 struct acpi_table_iort *iort;
1667 struct fwnode_handle *fwnode;
1668 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001669 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001670
1671 /*
1672 * iort_table and iort both point to the start of IORT table, but
1673 * have different struct types
1674 */
1675 iort = (struct acpi_table_iort *)iort_table;
1676
1677 /* Get the first IORT node */
1678 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1679 iort->node_offset);
1680 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1681 iort_table->length);
1682
1683 for (i = 0; i < iort->node_count; i++) {
1684 if (iort_node >= iort_end) {
1685 pr_err("iort node pointer overflows, bad table\n");
1686 return;
1687 }
1688
Sinan Kaya43554ce2018-12-19 22:46:58 +00001689 iort_enable_acs(iort_node);
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001690
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001691 ops = iort_get_dev_cfg(iort_node);
1692 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001693 fwnode = acpi_alloc_fwnode_static();
1694 if (!fwnode)
1695 return;
1696
1697 iort_set_fwnode(iort_node, fwnode);
1698
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001699 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001700 if (ret) {
1701 iort_delete_fwnode(iort_node);
1702 acpi_free_fwnode_static(fwnode);
1703 return;
1704 }
1705 }
1706
1707 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1708 iort_node->length);
1709 }
1710}
1711
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001712void __init acpi_iort_init(void)
1713{
1714 acpi_status status;
1715
Hanjun Guo701dafe2020-05-08 12:05:53 +08001716 /* iort_table will be used at runtime after the iort init,
1717 * so we don't need to call acpi_put_table() to release
1718 * the IORT table mapping.
1719 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001720 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001721 if (ACPI_FAILURE(status)) {
1722 if (status != AE_NOT_FOUND) {
1723 const char *msg = acpi_format_exception(status);
1724
1725 pr_err("Failed to get table, %s\n", msg);
1726 }
1727
1728 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001729 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001730
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001731 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001732}
Ard Biesheuvel2b865292020-11-19 18:53:58 +01001733
1734#ifdef CONFIG_ZONE_DMA
1735/*
1736 * Extract the highest CPU physical address accessible to all DMA masters in
1737 * the system. PHYS_ADDR_MAX is returned when no constrained device is found.
1738 */
1739phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
1740{
1741 phys_addr_t limit = PHYS_ADDR_MAX;
1742 struct acpi_iort_node *node, *end;
1743 struct acpi_table_iort *iort;
1744 acpi_status status;
1745 int i;
1746
1747 if (acpi_disabled)
1748 return limit;
1749
1750 status = acpi_get_table(ACPI_SIG_IORT, 0,
1751 (struct acpi_table_header **)&iort);
1752 if (ACPI_FAILURE(status))
1753 return limit;
1754
1755 node = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->node_offset);
1756 end = ACPI_ADD_PTR(struct acpi_iort_node, iort, iort->header.length);
1757
1758 for (i = 0; i < iort->node_count; i++) {
1759 if (node >= end)
1760 break;
1761
1762 switch (node->type) {
1763 struct acpi_iort_named_component *ncomp;
1764 struct acpi_iort_root_complex *rc;
1765 phys_addr_t local_limit;
1766
1767 case ACPI_IORT_NODE_NAMED_COMPONENT:
1768 ncomp = (struct acpi_iort_named_component *)node->node_data;
1769 local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
1770 limit = min_not_zero(limit, local_limit);
1771 break;
1772
1773 case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
1774 if (node->revision < 1)
1775 break;
1776
1777 rc = (struct acpi_iort_root_complex *)node->node_data;
1778 local_limit = DMA_BIT_MASK(rc->memory_address_limit);
1779 limit = min_not_zero(limit, local_limit);
1780 break;
1781 }
1782 node = ACPI_ADD_PTR(struct acpi_iort_node, node, node->length);
1783 }
1784 acpi_put_table(&iort->header);
1785 return limit;
1786}
1787#endif