blob: 53f9ef5150896f2c17138a23786b3e5ca8dfec79 [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>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020021
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000022#define IORT_TYPE_MASK(type) (1 << (type))
23#define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +000024#define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
25 (1 << ACPI_IORT_NODE_SMMU_V3))
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000026
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020027struct iort_its_msi_chip {
28 struct list_head list;
29 struct fwnode_handle *fw_node;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +000030 phys_addr_t base_addr;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020031 u32 translation_id;
32};
33
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000034struct iort_fwnode {
35 struct list_head list;
36 struct acpi_iort_node *iort_node;
37 struct fwnode_handle *fwnode;
38};
39static LIST_HEAD(iort_fwnode_list);
40static DEFINE_SPINLOCK(iort_fwnode_lock);
41
42/**
43 * iort_set_fwnode() - Create iort_fwnode and use it to register
44 * iommu data in the iort_fwnode_list
45 *
46 * @node: IORT table node associated with the IOMMU
47 * @fwnode: fwnode associated with the IORT node
48 *
49 * Returns: 0 on success
50 * <0 on failure
51 */
52static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
53 struct fwnode_handle *fwnode)
54{
55 struct iort_fwnode *np;
56
57 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
58
59 if (WARN_ON(!np))
60 return -ENOMEM;
61
62 INIT_LIST_HEAD(&np->list);
63 np->iort_node = iort_node;
64 np->fwnode = fwnode;
65
66 spin_lock(&iort_fwnode_lock);
67 list_add_tail(&np->list, &iort_fwnode_list);
68 spin_unlock(&iort_fwnode_lock);
69
70 return 0;
71}
72
73/**
74 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
75 *
76 * @node: IORT table node to be looked-up
77 *
78 * Returns: fwnode_handle pointer on success, NULL on failure
79 */
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +010080static inline struct fwnode_handle *iort_get_fwnode(
81 struct acpi_iort_node *node)
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000082{
83 struct iort_fwnode *curr;
84 struct fwnode_handle *fwnode = NULL;
85
86 spin_lock(&iort_fwnode_lock);
87 list_for_each_entry(curr, &iort_fwnode_list, list) {
88 if (curr->iort_node == node) {
89 fwnode = curr->fwnode;
90 break;
91 }
92 }
93 spin_unlock(&iort_fwnode_lock);
94
95 return fwnode;
96}
97
98/**
99 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
100 *
101 * @node: IORT table node associated with fwnode to delete
102 */
103static inline void iort_delete_fwnode(struct acpi_iort_node *node)
104{
105 struct iort_fwnode *curr, *tmp;
106
107 spin_lock(&iort_fwnode_lock);
108 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
109 if (curr->iort_node == node) {
110 list_del(&curr->list);
111 kfree(curr);
112 break;
113 }
114 }
115 spin_unlock(&iort_fwnode_lock);
116}
117
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800118/**
119 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
120 *
121 * @fwnode: fwnode associated with device to be looked-up
122 *
123 * Returns: iort_node pointer on success, NULL on failure
124 */
125static inline struct acpi_iort_node *iort_get_iort_node(
126 struct fwnode_handle *fwnode)
127{
128 struct iort_fwnode *curr;
129 struct acpi_iort_node *iort_node = NULL;
130
131 spin_lock(&iort_fwnode_lock);
132 list_for_each_entry(curr, &iort_fwnode_list, list) {
133 if (curr->fwnode == fwnode) {
134 iort_node = curr->iort_node;
135 break;
136 }
137 }
138 spin_unlock(&iort_fwnode_lock);
139
140 return iort_node;
141}
142
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200143typedef acpi_status (*iort_find_node_callback)
144 (struct acpi_iort_node *node, void *context);
145
146/* Root pointer to the mapped IORT table */
147static struct acpi_table_header *iort_table;
148
149static LIST_HEAD(iort_msi_chip_list);
150static DEFINE_SPINLOCK(iort_msi_chip_lock);
151
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200152/**
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000153 * iort_register_domain_token() - register domain token along with related
154 * ITS ID and base address to the list from where we can get it back later on.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200155 * @trans_id: ITS ID.
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000156 * @base: ITS base address.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200157 * @fw_node: Domain token.
158 *
159 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
160 */
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000161int iort_register_domain_token(int trans_id, phys_addr_t base,
162 struct fwnode_handle *fw_node)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200163{
164 struct iort_its_msi_chip *its_msi_chip;
165
166 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
167 if (!its_msi_chip)
168 return -ENOMEM;
169
170 its_msi_chip->fw_node = fw_node;
171 its_msi_chip->translation_id = trans_id;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000172 its_msi_chip->base_addr = base;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200173
174 spin_lock(&iort_msi_chip_lock);
175 list_add(&its_msi_chip->list, &iort_msi_chip_list);
176 spin_unlock(&iort_msi_chip_lock);
177
178 return 0;
179}
180
181/**
182 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
183 * @trans_id: ITS ID.
184 *
185 * Returns: none.
186 */
187void iort_deregister_domain_token(int trans_id)
188{
189 struct iort_its_msi_chip *its_msi_chip, *t;
190
191 spin_lock(&iort_msi_chip_lock);
192 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
193 if (its_msi_chip->translation_id == trans_id) {
194 list_del(&its_msi_chip->list);
195 kfree(its_msi_chip);
196 break;
197 }
198 }
199 spin_unlock(&iort_msi_chip_lock);
200}
201
202/**
203 * iort_find_domain_token() - Find domain token based on given ITS ID
204 * @trans_id: ITS ID.
205 *
206 * Returns: domain token when find on the list, NULL otherwise
207 */
208struct fwnode_handle *iort_find_domain_token(int trans_id)
209{
210 struct fwnode_handle *fw_node = NULL;
211 struct iort_its_msi_chip *its_msi_chip;
212
213 spin_lock(&iort_msi_chip_lock);
214 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
215 if (its_msi_chip->translation_id == trans_id) {
216 fw_node = its_msi_chip->fw_node;
217 break;
218 }
219 }
220 spin_unlock(&iort_msi_chip_lock);
221
222 return fw_node;
223}
224
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200225static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
226 iort_find_node_callback callback,
227 void *context)
228{
229 struct acpi_iort_node *iort_node, *iort_end;
230 struct acpi_table_iort *iort;
231 int i;
232
233 if (!iort_table)
234 return NULL;
235
236 /* Get the first IORT node */
237 iort = (struct acpi_table_iort *)iort_table;
238 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
239 iort->node_offset);
240 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
241 iort_table->length);
242
243 for (i = 0; i < iort->node_count; i++) {
244 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
245 "IORT node pointer overflows, bad table!\n"))
246 return NULL;
247
248 if (iort_node->type == type &&
249 ACPI_SUCCESS(callback(iort_node, context)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800250 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200251
252 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
253 iort_node->length);
254 }
255
256 return NULL;
257}
258
259static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
260 void *context)
261{
262 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800263 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200264
265 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
266 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100267 struct acpi_device *adev;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200268 struct acpi_iort_named_component *ncomp;
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100269 struct device *nc_dev = dev;
270
271 /*
272 * Walk the device tree to find a device with an
273 * ACPI companion; there is no point in scanning
274 * IORT for a device matching a named component if
275 * the device does not have an ACPI companion to
276 * start with.
277 */
278 do {
279 adev = ACPI_COMPANION(nc_dev);
280 if (adev)
281 break;
282
283 nc_dev = nc_dev->parent;
284 } while (nc_dev);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200285
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800286 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200287 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200288
289 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
290 if (ACPI_FAILURE(status)) {
Lorenzo Pieralisi07d2e592020-06-19 09:20:02 +0100291 dev_warn(nc_dev, "Can't get device full path name\n");
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200292 goto out;
293 }
294
295 ncomp = (struct acpi_iort_named_component *)node->node_data;
296 status = !strcmp(ncomp->device_name, buf.pointer) ?
297 AE_OK : AE_NOT_FOUND;
298 acpi_os_free(buf.pointer);
299 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
300 struct acpi_iort_root_complex *pci_rc;
301 struct pci_bus *bus;
302
303 bus = to_pci_bus(dev);
304 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
305
306 /*
307 * It is assumed that PCI segment numbers maps one-to-one
308 * with root complexes. Each segment number can represent only
309 * one root complex.
310 */
311 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
312 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200313 }
314out:
315 return status;
316}
317
318static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200319 u32 *rid_out, bool check_overlap)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200320{
321 /* Single mapping does not care for input id */
322 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
323 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
324 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
325 *rid_out = map->output_base;
326 return 0;
327 }
328
329 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
330 map, type);
331 return -ENXIO;
332 }
333
Ard Biesheuvel6d3b29d2020-05-01 18:10:13 +0200334 if (rid_in < map->input_base ||
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200335 (rid_in > map->input_base + map->id_count))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200336 return -ENXIO;
337
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200338 if (check_overlap) {
339 /*
340 * We already found a mapping for this input ID at the end of
341 * another region. If it coincides with the start of this
342 * region, we assume the prior match was due to the off-by-1
343 * issue mentioned below, and allow it to be superseded.
344 * Otherwise, things are *really* broken, and we just disregard
345 * duplicate matches entirely to retain compatibility.
346 */
347 pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n",
348 map, rid_in);
349 if (rid_in != map->input_base)
350 return -ENXIO;
Hanjun Guo44cdc7b2020-05-08 11:56:38 +0800351
352 pr_err(FW_BUG "applying workaround.\n");
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200353 }
354
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200355 *rid_out = map->output_base + (rid_in - map->input_base);
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200356
357 /*
358 * Due to confusion regarding the meaning of the id_count field (which
359 * carries the number of IDs *minus 1*), we may have to disregard this
360 * match if it is at the end of the range, and overlaps with the start
361 * of another one.
362 */
363 if (map->id_count > 0 && rid_in == map->input_base + map->id_count)
364 return -EAGAIN;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200365 return 0;
366}
367
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100368static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
369 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000370{
371 struct acpi_iort_node *parent;
372 struct acpi_iort_id_mapping *map;
373
374 if (!node->mapping_offset || !node->mapping_count ||
375 index >= node->mapping_count)
376 return NULL;
377
378 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000379 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000380
381 /* Firmware bug! */
382 if (!map->output_reference) {
383 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
384 node, node->type);
385 return NULL;
386 }
387
388 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
389 map->output_reference);
390
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000391 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000392 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800393 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
Neil Leeder24e51602019-03-26 15:17:50 +0000394 node->type == ACPI_IORT_NODE_SMMU_V3 ||
395 node->type == ACPI_IORT_NODE_PMCG) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000396 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000397 return parent;
398 }
399 }
400
401 return NULL;
402}
403
Hanjun Guo86456a32017-10-13 15:09:49 +0800404static int iort_get_id_mapping_index(struct acpi_iort_node *node)
405{
406 struct acpi_iort_smmu_v3 *smmu;
Tuan Phan50c8ab82020-05-20 10:13:07 -0700407 struct acpi_iort_pmcg *pmcg;
Hanjun Guo86456a32017-10-13 15:09:49 +0800408
409 switch (node->type) {
410 case ACPI_IORT_NODE_SMMU_V3:
411 /*
412 * SMMUv3 dev ID mapping index was introduced in revision 1
413 * table, not available in revision 0
414 */
415 if (node->revision < 1)
416 return -EINVAL;
417
418 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
419 /*
420 * ID mapping index is only ignored if all interrupts are
421 * GSIV based
422 */
423 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
424 && smmu->sync_gsiv)
425 return -EINVAL;
426
427 if (smmu->id_mapping_index >= node->mapping_count) {
428 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
429 node, node->type);
430 return -EINVAL;
431 }
432
433 return smmu->id_mapping_index;
Neil Leeder24e51602019-03-26 15:17:50 +0000434 case ACPI_IORT_NODE_PMCG:
Tuan Phan50c8ab82020-05-20 10:13:07 -0700435 pmcg = (struct acpi_iort_pmcg *)node->node_data;
436 if (pmcg->overflow_gsiv || node->mapping_count == 0)
437 return -EINVAL;
438
Neil Leeder24e51602019-03-26 15:17:50 +0000439 return 0;
Hanjun Guo86456a32017-10-13 15:09:49 +0800440 default:
441 return -EINVAL;
442 }
443}
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800444
Hanjun Guo697f6092017-03-07 20:40:03 +0800445static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
446 u32 id_in, u32 *id_out,
447 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200448{
Hanjun Guo697f6092017-03-07 20:40:03 +0800449 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200450
451 /* Parse the ID mapping tree to find specified node type */
452 while (node) {
453 struct acpi_iort_id_mapping *map;
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200454 int i, index, rc = 0;
455 u32 out_ref = 0, map_id = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200456
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000457 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800458 if (id_out)
459 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200460 return node;
461 }
462
463 if (!node->mapping_offset || !node->mapping_count)
464 goto fail_map;
465
466 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
467 node->mapping_offset);
468
469 /* Firmware bug! */
470 if (!map->output_reference) {
471 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
472 node, node->type);
473 goto fail_map;
474 }
475
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800476 /*
477 * Get the special ID mapping index (if any) and skip its
478 * associated ID map to prevent erroneous multi-stage
479 * IORT ID translations.
480 */
481 index = iort_get_id_mapping_index(node);
482
Hanjun Guo697f6092017-03-07 20:40:03 +0800483 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200484 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800485 /* if it is special mapping index, skip it */
486 if (i == index)
487 continue;
488
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200489 rc = iort_id_map(map, node->type, map_id, &id, out_ref);
490 if (!rc)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200491 break;
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200492 if (rc == -EAGAIN)
493 out_ref = map->output_reference;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200494 }
495
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200496 if (i == node->mapping_count && !out_ref)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200497 goto fail_map;
498
499 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
Ard Biesheuvel539979b2020-05-01 18:10:14 +0200500 rc ? out_ref : map->output_reference);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200501 }
502
503fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800504 /* Map input ID to output ID unchanged on mapping failure */
505 if (id_out)
506 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200507
508 return NULL;
509}
510
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100511static struct acpi_iort_node *iort_node_map_platform_id(
512 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
513 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800514{
515 struct acpi_iort_node *parent;
516 u32 id;
517
518 /* step 1: retrieve the initial dev id */
519 parent = iort_node_get_id(node, &id, index);
520 if (!parent)
521 return NULL;
522
523 /*
524 * optional step 2: map the initial dev id if its parent is not
525 * the target type we want, map it again for the use cases such
526 * as NC (named component) -> SMMU -> ITS. If the type is matched,
527 * return the initial dev id and its parent pointer directly.
528 */
529 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
530 parent = iort_node_map_id(parent, id, id_out, type_mask);
531 else
532 if (id_out)
533 *id_out = id;
534
535 return parent;
536}
537
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200538static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
539{
540 struct pci_bus *pbus;
541
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800542 if (!dev_is_pci(dev)) {
543 struct acpi_iort_node *node;
544 /*
545 * scan iort_fwnode_list to see if it's an iort platform
546 * device (such as SMMU, PMCG),its iort node already cached
547 * and associated with fwnode when iort platform devices
548 * were initialized.
549 */
550 node = iort_get_iort_node(dev->fwnode);
551 if (node)
552 return node;
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800553 /*
554 * if not, then it should be a platform device defined in
555 * DSDT/SSDT (with Named Component node in IORT)
556 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200557 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
558 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800559 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200560
561 /* Find a PCI root bus */
562 pbus = to_pci_dev(dev)->bus;
563 while (!pci_is_root_bus(pbus))
564 pbus = pbus->parent;
565
566 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
567 iort_match_node_callback, &pbus->dev);
568}
569
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200570/**
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100571 * iort_msi_map_id() - Map a MSI input ID for a device
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200572 * @dev: The device for which the mapping is to be done.
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100573 * @input_id: The device input ID.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200574 *
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100575 * Returns: mapped MSI ID on success, input ID otherwise
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200576 */
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100577u32 iort_msi_map_id(struct device *dev, u32 input_id)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200578{
579 struct acpi_iort_node *node;
580 u32 dev_id;
581
582 node = iort_find_dev_node(dev);
583 if (!node)
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100584 return input_id;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200585
Lorenzo Pieralisi39c3cf52020-06-19 09:20:04 +0100586 iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200587 return dev_id;
588}
589
590/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800591 * iort_pmsi_get_dev_id() - Get the device id for a device
592 * @dev: The device for which the mapping is to be done.
593 * @dev_id: The device ID found.
594 *
595 * Returns: 0 for successful find a dev id, -ENODEV on error
596 */
597int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
598{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800599 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800600 struct acpi_iort_node *node;
601
602 node = iort_find_dev_node(dev);
603 if (!node)
604 return -ENODEV;
605
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800606 index = iort_get_id_mapping_index(node);
607 /* if there is a valid index, go get the dev_id directly */
608 if (index >= 0) {
609 if (iort_node_get_id(node, dev_id, index))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800610 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800611 } else {
612 for (i = 0; i < node->mapping_count; i++) {
613 if (iort_node_map_platform_id(node, dev_id,
614 IORT_MSI_TYPE, i))
615 return 0;
616 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800617 }
618
619 return -ENODEV;
620}
621
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000622static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
623{
624 struct iort_its_msi_chip *its_msi_chip;
625 int ret = -ENODEV;
626
627 spin_lock(&iort_msi_chip_lock);
628 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
629 if (its_msi_chip->translation_id == its_id) {
630 *base = its_msi_chip->base_addr;
631 ret = 0;
632 break;
633 }
634 }
635 spin_unlock(&iort_msi_chip_lock);
636
637 return ret;
638}
639
Hanjun Guoae7c1832017-03-07 20:40:05 +0800640/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200641 * iort_dev_find_its_id() - Find the ITS identifier for a device
642 * @dev: The device.
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100643 * @id: Device's ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200644 * @idx: Index of the ITS identifier list.
645 * @its_id: ITS identifier.
646 *
647 * Returns: 0 on success, appropriate error value otherwise
648 */
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100649static int iort_dev_find_its_id(struct device *dev, u32 id,
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200650 unsigned int idx, int *its_id)
651{
652 struct acpi_iort_its_group *its;
653 struct acpi_iort_node *node;
654
655 node = iort_find_dev_node(dev);
656 if (!node)
657 return -ENXIO;
658
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100659 node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200660 if (!node)
661 return -ENXIO;
662
663 /* Move to ITS specific data */
664 its = (struct acpi_iort_its_group *)node->node_data;
Lorenzo Pieralisi5a46d3f2019-07-22 17:25:48 +0100665 if (idx >= its->its_count) {
666 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200667 idx, its->its_count);
668 return -ENXIO;
669 }
670
671 *its_id = its->identifiers[idx];
672 return 0;
673}
674
675/**
676 * iort_get_device_domain() - Find MSI domain related to a device
677 * @dev: The device.
678 * @req_id: Requester ID for the device.
679 *
680 * Returns: the MSI domain for this device, NULL otherwise
681 */
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100682struct irq_domain *iort_get_device_domain(struct device *dev, u32 id,
683 enum irq_domain_bus_token bus_token)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200684{
685 struct fwnode_handle *handle;
686 int its_id;
687
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100688 if (iort_dev_find_its_id(dev, id, 0, &its_id))
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200689 return NULL;
690
691 handle = iort_find_domain_token(its_id);
692 if (!handle)
693 return NULL;
694
Lorenzo Pieralisid1718a1b2020-06-19 09:20:03 +0100695 return irq_find_matching_fwnode(handle, bus_token);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200696}
697
Lorenzo Pieralisi65637902017-10-13 15:09:50 +0800698static void iort_set_device_domain(struct device *dev,
699 struct acpi_iort_node *node)
700{
701 struct acpi_iort_its_group *its;
702 struct acpi_iort_node *msi_parent;
703 struct acpi_iort_id_mapping *map;
704 struct fwnode_handle *iort_fwnode;
705 struct irq_domain *domain;
706 int index;
707
708 index = iort_get_id_mapping_index(node);
709 if (index < 0)
710 return;
711
712 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
713 node->mapping_offset + index * sizeof(*map));
714
715 /* Firmware bug! */
716 if (!map->output_reference ||
717 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
718 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
719 node, node->type);
720 return;
721 }
722
723 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
724 map->output_reference);
725
726 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
727 return;
728
729 /* Move to ITS specific data */
730 its = (struct acpi_iort_its_group *)msi_parent->node_data;
731
732 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
733 if (!iort_fwnode)
734 return;
735
736 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
737 if (domain)
738 dev_set_msi_domain(dev, domain);
739}
740
Hanjun Guod4f54a12017-03-07 20:40:06 +0800741/**
742 * iort_get_platform_device_domain() - Find MSI domain related to a
743 * platform device
744 * @dev: the dev pointer associated with the platform device
745 *
746 * Returns: the MSI domain for this device, NULL otherwise
747 */
748static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
749{
Lorenzo Pieralisiea2412d2018-11-29 09:55:59 +0000750 struct acpi_iort_node *node, *msi_parent = NULL;
Hanjun Guod4f54a12017-03-07 20:40:06 +0800751 struct fwnode_handle *iort_fwnode;
752 struct acpi_iort_its_group *its;
753 int i;
754
755 /* find its associated iort node */
756 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
757 iort_match_node_callback, dev);
758 if (!node)
759 return NULL;
760
761 /* then find its msi parent node */
762 for (i = 0; i < node->mapping_count; i++) {
763 msi_parent = iort_node_map_platform_id(node, NULL,
764 IORT_MSI_TYPE, i);
765 if (msi_parent)
766 break;
767 }
768
769 if (!msi_parent)
770 return NULL;
771
772 /* Move to ITS specific data */
773 its = (struct acpi_iort_its_group *)msi_parent->node_data;
774
775 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
776 if (!iort_fwnode)
777 return NULL;
778
779 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
780}
781
782void acpi_configure_pmsi_domain(struct device *dev)
783{
784 struct irq_domain *msi_domain;
785
786 msi_domain = iort_get_platform_device_domain(dev);
787 if (msi_domain)
788 dev_set_msi_domain(dev, msi_domain);
789}
790
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100791#ifdef CONFIG_IOMMU_API
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000792static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
793{
794 struct acpi_iort_node *iommu;
Joerg Roedel8097e532018-11-29 14:01:00 +0100795 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000796
797 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
798
799 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
800 struct acpi_iort_smmu_v3 *smmu;
801
802 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
803 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
804 return iommu;
805 }
806
807 return NULL;
808}
809
Joerg Roedel8097e532018-11-29 14:01:00 +0100810static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100811{
Joerg Roedel8097e532018-11-29 14:01:00 +0100812 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
813
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100814 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
815}
816
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100817static inline int iort_add_device_replay(const struct iommu_ops *ops,
818 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100819{
820 int err = 0;
821
Joerg Roedeld2e1a002018-12-05 14:39:45 +0100822 if (dev->bus && !device_iommu_mapped(dev))
823 err = iommu_probe_device(dev);
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100824
825 return err;
826}
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000827
828/**
829 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
830 * @dev: Device from iommu_get_resv_regions()
831 * @head: Reserved region list from iommu_get_resv_regions()
832 *
833 * Returns: Number of msi reserved regions on success (0 if platform
834 * doesn't require the reservation or no associated msi regions),
835 * appropriate error value otherwise. The ITS interrupt translation
836 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
837 * are the msi reserved regions.
838 */
839int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
840{
Joerg Roedel8097e532018-11-29 14:01:00 +0100841 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000842 struct acpi_iort_its_group *its;
843 struct acpi_iort_node *iommu_node, *its_node = NULL;
844 int i, resv = 0;
845
846 iommu_node = iort_get_msi_resv_iommu(dev);
847 if (!iommu_node)
848 return 0;
849
850 /*
851 * Current logic to reserve ITS regions relies on HW topologies
852 * where a given PCI or named component maps its IDs to only one
853 * ITS group; if a PCI or named component can map its IDs to
854 * different ITS groups through IORT mappings this function has
855 * to be reworked to ensure we reserve regions for all ITS groups
856 * a given PCI or named component may map IDs to.
857 */
858
Joerg Roedel8097e532018-11-29 14:01:00 +0100859 for (i = 0; i < fwspec->num_ids; i++) {
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000860 its_node = iort_node_map_id(iommu_node,
Joerg Roedel8097e532018-11-29 14:01:00 +0100861 fwspec->ids[i],
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000862 NULL, IORT_MSI_TYPE);
863 if (its_node)
864 break;
865 }
866
867 if (!its_node)
868 return 0;
869
870 /* Move to ITS specific data */
871 its = (struct acpi_iort_its_group *)its_node->node_data;
872
873 for (i = 0; i < its->its_count; i++) {
874 phys_addr_t base;
875
876 if (!iort_find_its_base(its->identifiers[i], &base)) {
877 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
878 struct iommu_resv_region *region;
879
880 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
881 prot, IOMMU_RESV_MSI);
882 if (region) {
883 list_add_tail(&region->list, head);
884 resv++;
885 }
886 }
887 }
888
889 return (resv == its->its_count) ? resv : -ENODEV;
890}
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100891
892static inline bool iort_iommu_driver_enabled(u8 type)
893{
894 switch (type) {
895 case ACPI_IORT_NODE_SMMU_V3:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000896 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100897 case ACPI_IORT_NODE_SMMU:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000898 return IS_ENABLED(CONFIG_ARM_SMMU);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100899 default:
900 pr_warn("IORT node type %u does not describe an SMMU\n", type);
901 return false;
902 }
903}
904
905static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
906 struct fwnode_handle *fwnode,
907 const struct iommu_ops *ops)
908{
909 int ret = iommu_fwspec_init(dev, fwnode, ops);
910
911 if (!ret)
912 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
913
914 return ret;
915}
916
917static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
918{
919 struct acpi_iort_root_complex *pci_rc;
920
921 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
922 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
923}
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100924
Robin Murphybc8648d2017-08-04 17:42:06 +0100925static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
926 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000927{
Robin Murphybc8648d2017-08-04 17:42:06 +0100928 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000929 struct fwnode_handle *iort_fwnode;
930
Robin Murphybc8648d2017-08-04 17:42:06 +0100931 if (!node)
932 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000933
Robin Murphybc8648d2017-08-04 17:42:06 +0100934 iort_fwnode = iort_get_fwnode(node);
935 if (!iort_fwnode)
936 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000937
Robin Murphybc8648d2017-08-04 17:42:06 +0100938 /*
939 * If the ops look-up fails, this means that either
940 * the SMMU drivers have not been probed yet or that
941 * the SMMU drivers are not built in the kernel;
942 * Depending on whether the SMMU drivers are built-in
943 * in the kernel or not, defer the IOMMU configuration
944 * or just abort it.
945 */
946 ops = iommu_ops_from_fwnode(iort_fwnode);
947 if (!ops)
948 return iort_iommu_driver_enabled(node->type) ?
949 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000950
Robin Murphybc8648d2017-08-04 17:42:06 +0100951 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
952}
953
954struct iort_pci_alias_info {
955 struct device *dev;
956 struct acpi_iort_node *node;
957};
958
959static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
960{
961 struct iort_pci_alias_info *info = data;
962 struct acpi_iort_node *parent;
963 u32 streamid;
964
965 parent = iort_node_map_id(info->node, alias, &streamid,
966 IORT_IOMMU_TYPE);
967 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000968}
969
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +0100970static void iort_named_component_init(struct device *dev,
971 struct acpi_iort_node *node)
972{
973 struct acpi_iort_named_component *nc;
974 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
975
976 if (!fwspec)
977 return;
978
979 nc = (struct acpi_iort_named_component *)node->node_data;
980 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
981 nc->node_flags);
982}
983
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100984/**
985 * iort_iommu_configure - Set-up IOMMU configuration for a device.
986 *
987 * @dev: device to configure
988 *
989 * Returns: iommu_ops pointer on configuration success
990 * NULL on configuration failure
991 */
992const struct iommu_ops *iort_iommu_configure(struct device *dev)
993{
994 struct acpi_iort_node *node, *parent;
995 const struct iommu_ops *ops;
996 u32 streamid = 0;
997 int err = -ENODEV;
998
999 /*
1000 * If we already translated the fwspec there
1001 * is nothing left to do, return the iommu_ops.
1002 */
1003 ops = iort_fwspec_iommu_ops(dev);
1004 if (ops)
1005 return ops;
1006
1007 if (dev_is_pci(dev)) {
Joerg Roedel6990ec72020-03-26 16:08:27 +01001008 struct iommu_fwspec *fwspec;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001009 struct pci_bus *bus = to_pci_dev(dev)->bus;
1010 struct iort_pci_alias_info info = { .dev = dev };
1011
1012 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
1013 iort_match_node_callback, &bus->dev);
1014 if (!node)
1015 return NULL;
1016
1017 info.node = node;
1018 err = pci_for_each_dma_alias(to_pci_dev(dev),
1019 iort_pci_iommu_init, &info);
1020
Joerg Roedel6990ec72020-03-26 16:08:27 +01001021 fwspec = dev_iommu_fwspec_get(dev);
1022 if (fwspec && iort_pci_rc_supports_ats(node))
1023 fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001024 } else {
1025 int i = 0;
1026
1027 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1028 iort_match_node_callback, dev);
1029 if (!node)
1030 return NULL;
1031
1032 do {
1033 parent = iort_node_map_platform_id(node, &streamid,
1034 IORT_IOMMU_TYPE,
1035 i++);
1036
1037 if (parent)
1038 err = iort_iommu_xlate(dev, parent, streamid);
1039 } while (parent && !err);
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +01001040
1041 if (!err)
1042 iort_named_component_init(dev, node);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +01001043 }
1044
1045 /*
1046 * If we have reason to believe the IOMMU driver missed the initial
1047 * add_device callback for dev, replay it to get things in order.
1048 */
1049 if (!err) {
1050 ops = iort_fwspec_iommu_ops(dev);
1051 err = iort_add_device_replay(ops, dev);
1052 }
1053
1054 /* Ignore all other errors apart from EPROBE_DEFER */
1055 if (err == -EPROBE_DEFER) {
1056 ops = ERR_PTR(err);
1057 } else if (err) {
1058 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1059 ops = NULL;
1060 }
1061
1062 return ops;
1063}
1064#else
1065static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1066{ return NULL; }
1067static inline int iort_add_device_replay(const struct iommu_ops *ops,
1068 struct device *dev)
1069{ return 0; }
1070int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1071{ return 0; }
1072const struct iommu_ops *iort_iommu_configure(struct device *dev)
1073{ return NULL; }
1074#endif
1075
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001076static int nc_dma_get_range(struct device *dev, u64 *size)
1077{
1078 struct acpi_iort_node *node;
1079 struct acpi_iort_named_component *ncomp;
1080
1081 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1082 iort_match_node_callback, dev);
1083 if (!node)
1084 return -ENODEV;
1085
1086 ncomp = (struct acpi_iort_named_component *)node->node_data;
1087
1088 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1089 1ULL<<ncomp->memory_address_limit;
1090
1091 return 0;
1092}
1093
Robin Murphy5ac65e82018-07-23 23:16:06 +01001094static int rc_dma_get_range(struct device *dev, u64 *size)
1095{
1096 struct acpi_iort_node *node;
1097 struct acpi_iort_root_complex *rc;
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001098 struct pci_bus *pbus = to_pci_dev(dev)->bus;
Robin Murphy5ac65e82018-07-23 23:16:06 +01001099
1100 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001101 iort_match_node_callback, &pbus->dev);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001102 if (!node || node->revision < 1)
1103 return -ENODEV;
1104
1105 rc = (struct acpi_iort_root_complex *)node->node_data;
1106
1107 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1108 1ULL<<rc->memory_address_limit;
1109
1110 return 0;
1111}
1112
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +00001113/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001114 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001115 *
1116 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001117 * @dma_addr: device DMA address result pointer
1118 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001119 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001120void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001121{
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001122 u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1123 int ret;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001124
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001125 /*
Robin Murphy6757cda2018-07-23 23:16:11 +01001126 * If @dev is expected to be DMA-capable then the bus code that created
1127 * it should have initialised its dma_mask pointer by this point. For
1128 * now, we'll continue the legacy behaviour of coercing it to the
1129 * coherent mask if not, but we'll no longer do so quietly.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001130 */
Robin Murphy6757cda2018-07-23 23:16:11 +01001131 if (!dev->dma_mask) {
1132 dev_warn(dev, "DMA mask not set\n");
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001133 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy6757cda2018-07-23 23:16:11 +01001134 }
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001135
Robin Murphy6757cda2018-07-23 23:16:11 +01001136 if (dev->coherent_dma_mask)
1137 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1138 else
1139 size = 1ULL << 32;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001140
Ard Biesheuvel7fb89e12020-04-20 11:27:53 +02001141 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
1142 if (ret == -ENODEV)
1143 ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size)
1144 : nc_dma_get_range(dev, &size);
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001145
1146 if (!ret) {
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001147 /*
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001148 * Limit coherent and dma mask based on size retrieved from
1149 * firmware.
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001150 */
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001151 end = dmaaddr + size - 1;
1152 mask = DMA_BIT_MASK(ilog2(end) + 1);
1153 dev->bus_dma_limit = end;
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001154 dev->coherent_dma_mask = mask;
1155 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001156 }
1157
1158 *dma_addr = dmaaddr;
1159 *dma_size = size;
1160
1161 dev->dma_pfn_offset = PFN_DOWN(offset);
1162 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001163}
1164
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001165static void __init acpi_iort_register_irq(int hwirq, const char *name,
1166 int trigger,
1167 struct resource *res)
1168{
1169 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1170 ACPI_ACTIVE_HIGH);
1171
1172 if (irq <= 0) {
1173 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1174 name);
1175 return;
1176 }
1177
1178 res->start = irq;
1179 res->end = irq;
1180 res->flags = IORESOURCE_IRQ;
1181 res->name = name;
1182}
1183
1184static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1185{
1186 struct acpi_iort_smmu_v3 *smmu;
1187 /* Always present mem resource */
1188 int num_res = 1;
1189
1190 /* Retrieve SMMUv3 specific data */
1191 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1192
1193 if (smmu->event_gsiv)
1194 num_res++;
1195
1196 if (smmu->pri_gsiv)
1197 num_res++;
1198
1199 if (smmu->gerr_gsiv)
1200 num_res++;
1201
1202 if (smmu->sync_gsiv)
1203 num_res++;
1204
1205 return num_res;
1206}
1207
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301208static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1209{
1210 /*
1211 * Cavium ThunderX2 implementation doesn't not support unique
1212 * irq line. Use single irq line for all the SMMUv3 interrupts.
1213 */
1214 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1215 return false;
1216
1217 /*
1218 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1219 * SPI numbers here.
1220 */
1221 return smmu->event_gsiv == smmu->pri_gsiv &&
1222 smmu->event_gsiv == smmu->gerr_gsiv &&
1223 smmu->event_gsiv == smmu->sync_gsiv;
1224}
1225
Linu Cherian403e8c72017-06-22 17:35:36 +05301226static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1227{
1228 /*
1229 * Override the size, for Cavium ThunderX2 implementation
1230 * which doesn't support the page 1 SMMU register space.
1231 */
1232 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1233 return SZ_64K;
1234
1235 return SZ_128K;
1236}
1237
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001238static void __init arm_smmu_v3_init_resources(struct resource *res,
1239 struct acpi_iort_node *node)
1240{
1241 struct acpi_iort_smmu_v3 *smmu;
1242 int num_res = 0;
1243
1244 /* Retrieve SMMUv3 specific data */
1245 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1246
1247 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301248 res[num_res].end = smmu->base_address +
1249 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001250 res[num_res].flags = IORESOURCE_MEM;
1251
1252 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301253 if (arm_smmu_v3_is_combined_irq(smmu)) {
1254 if (smmu->event_gsiv)
1255 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1256 ACPI_EDGE_SENSITIVE,
1257 &res[num_res++]);
1258 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001259
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301260 if (smmu->event_gsiv)
1261 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1262 ACPI_EDGE_SENSITIVE,
1263 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001264
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301265 if (smmu->pri_gsiv)
1266 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1267 ACPI_EDGE_SENSITIVE,
1268 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001269
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301270 if (smmu->gerr_gsiv)
1271 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1272 ACPI_EDGE_SENSITIVE,
1273 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001274
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301275 if (smmu->sync_gsiv)
1276 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1277 ACPI_EDGE_SENSITIVE,
1278 &res[num_res++]);
1279 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001280}
1281
Neil Leeder24e51602019-03-26 15:17:50 +00001282static void __init arm_smmu_v3_dma_configure(struct device *dev,
1283 struct acpi_iort_node *node)
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001284{
1285 struct acpi_iort_smmu_v3 *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001286 enum dev_dma_attr attr;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001287
1288 /* Retrieve SMMUv3 specific data */
1289 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1290
Neil Leeder24e51602019-03-26 15:17:50 +00001291 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1292 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1293
1294 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1295 dev->dma_mask = &dev->coherent_dma_mask;
1296
1297 /* Configure DMA for the page table walker */
1298 acpi_dma_configure(dev, attr);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001299}
1300
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001301#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001302/*
1303 * set numa proximity domain for smmuv3 device
1304 */
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001305static int __init arm_smmu_v3_set_proximity(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001306 struct acpi_iort_node *node)
1307{
1308 struct acpi_iort_smmu_v3 *smmu;
1309
1310 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1311 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001312 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001313
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001314 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001315 return -EINVAL;
1316
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001317 set_dev_node(dev, dev_node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001318 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1319 smmu->base_address,
1320 smmu->pxm);
1321 }
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001322 return 0;
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001323}
1324#else
1325#define arm_smmu_v3_set_proximity NULL
1326#endif
1327
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001328static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1329{
1330 struct acpi_iort_smmu *smmu;
1331
1332 /* Retrieve SMMU specific data */
1333 smmu = (struct acpi_iort_smmu *)node->node_data;
1334
1335 /*
1336 * Only consider the global fault interrupt and ignore the
1337 * configuration access interrupt.
1338 *
1339 * MMIO address and global fault interrupt resources are always
1340 * present so add them to the context interrupt count as a static
1341 * value.
1342 */
1343 return smmu->context_interrupt_count + 2;
1344}
1345
1346static void __init arm_smmu_init_resources(struct resource *res,
1347 struct acpi_iort_node *node)
1348{
1349 struct acpi_iort_smmu *smmu;
1350 int i, hw_irq, trigger, num_res = 0;
1351 u64 *ctx_irq, *glb_irq;
1352
1353 /* Retrieve SMMU specific data */
1354 smmu = (struct acpi_iort_smmu *)node->node_data;
1355
1356 res[num_res].start = smmu->base_address;
1357 res[num_res].end = smmu->base_address + smmu->span - 1;
1358 res[num_res].flags = IORESOURCE_MEM;
1359 num_res++;
1360
1361 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1362 /* Global IRQs */
1363 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1364 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1365
1366 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1367 &res[num_res++]);
1368
1369 /* Context IRQs */
1370 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1371 for (i = 0; i < smmu->context_interrupt_count; i++) {
1372 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1373 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1374
1375 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1376 &res[num_res++]);
1377 }
1378}
1379
Neil Leeder24e51602019-03-26 15:17:50 +00001380static void __init arm_smmu_dma_configure(struct device *dev,
1381 struct acpi_iort_node *node)
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001382{
1383 struct acpi_iort_smmu *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001384 enum dev_dma_attr attr;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001385
1386 /* Retrieve SMMU specific data */
1387 smmu = (struct acpi_iort_smmu *)node->node_data;
1388
Neil Leeder24e51602019-03-26 15:17:50 +00001389 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1390 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1391
1392 /* We expect the dma masks to be equivalent for SMMU set-ups */
1393 dev->dma_mask = &dev->coherent_dma_mask;
1394
1395 /* Configure DMA for the page table walker */
1396 acpi_dma_configure(dev, attr);
1397}
1398
1399static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1400{
1401 struct acpi_iort_pmcg *pmcg;
1402
1403 /* Retrieve PMCG specific data */
1404 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1405
1406 /*
1407 * There are always 2 memory resources.
1408 * If the overflow_gsiv is present then add that for a total of 3.
1409 */
1410 return pmcg->overflow_gsiv ? 3 : 2;
1411}
1412
1413static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1414 struct acpi_iort_node *node)
1415{
1416 struct acpi_iort_pmcg *pmcg;
1417
1418 /* Retrieve PMCG specific data */
1419 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1420
1421 res[0].start = pmcg->page0_base_address;
1422 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1423 res[0].flags = IORESOURCE_MEM;
1424 res[1].start = pmcg->page1_base_address;
1425 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1426 res[1].flags = IORESOURCE_MEM;
1427
1428 if (pmcg->overflow_gsiv)
1429 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1430 ACPI_EDGE_SENSITIVE, &res[2]);
1431}
1432
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001433static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1434 /* HiSilicon Hip08 Platform */
1435 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1436 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1437 { }
1438};
1439
Neil Leeder24e51602019-03-26 15:17:50 +00001440static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1441{
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001442 u32 model;
1443 int idx;
1444
1445 idx = acpi_match_platform_list(pmcg_plat_info);
1446 if (idx >= 0)
1447 model = pmcg_plat_info[idx].data;
1448 else
1449 model = IORT_SMMU_V3_PMCG_GENERIC;
Neil Leeder24e51602019-03-26 15:17:50 +00001450
1451 return platform_device_add_data(pdev, &model, sizeof(model));
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001452}
1453
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001454struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001455 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001456 int (*dev_init)(struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001457 void (*dev_dma_configure)(struct device *dev,
1458 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001459 int (*dev_count_resources)(struct acpi_iort_node *node);
1460 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001461 struct acpi_iort_node *node);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001462 int (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001463 struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001464 int (*dev_add_platdata)(struct platform_device *pdev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001465};
1466
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001467static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001468 .name = "arm-smmu-v3",
Neil Leeder24e51602019-03-26 15:17:50 +00001469 .dev_dma_configure = arm_smmu_v3_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001470 .dev_count_resources = arm_smmu_v3_count_resources,
1471 .dev_init_resources = arm_smmu_v3_init_resources,
1472 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001473};
1474
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001475static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001476 .name = "arm-smmu",
Neil Leeder24e51602019-03-26 15:17:50 +00001477 .dev_dma_configure = arm_smmu_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001478 .dev_count_resources = arm_smmu_count_resources,
Neil Leeder24e51602019-03-26 15:17:50 +00001479 .dev_init_resources = arm_smmu_init_resources,
1480};
1481
1482static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1483 .name = "arm-smmu-v3-pmcg",
1484 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1485 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1486 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001487};
1488
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001489static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001490 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001491{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001492 switch (node->type) {
1493 case ACPI_IORT_NODE_SMMU_V3:
1494 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001495 case ACPI_IORT_NODE_SMMU:
1496 return &iort_arm_smmu_cfg;
Neil Leeder24e51602019-03-26 15:17:50 +00001497 case ACPI_IORT_NODE_PMCG:
1498 return &iort_arm_smmu_v3_pmcg_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001499 default:
1500 return NULL;
1501 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001502}
1503
1504/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001505 * iort_add_platform_device() - Allocate a platform device for IORT node
1506 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001507 *
1508 * Returns: 0 on success, <0 failure
1509 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001510static int __init iort_add_platform_device(struct acpi_iort_node *node,
1511 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001512{
1513 struct fwnode_handle *fwnode;
1514 struct platform_device *pdev;
1515 struct resource *r;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001516 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001517
1518 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1519 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001520 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001521
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001522 if (ops->dev_set_proximity) {
1523 ret = ops->dev_set_proximity(&pdev->dev, node);
1524 if (ret)
1525 goto dev_put;
1526 }
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001527
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001528 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001529
1530 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1531 if (!r) {
1532 ret = -ENOMEM;
1533 goto dev_put;
1534 }
1535
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001536 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001537
1538 ret = platform_device_add_resources(pdev, r, count);
1539 /*
1540 * Resources are duplicated in platform_device_add_resources,
1541 * free their allocated memory
1542 */
1543 kfree(r);
1544
1545 if (ret)
1546 goto dev_put;
1547
1548 /*
Neil Leeder24e51602019-03-26 15:17:50 +00001549 * Platform devices based on PMCG nodes uses platform_data to
1550 * pass the hardware model info to the driver. For others, add
1551 * a copy of IORT node pointer to platform_data to be used to
1552 * retrieve IORT data information.
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001553 */
Neil Leeder24e51602019-03-26 15:17:50 +00001554 if (ops->dev_add_platdata)
1555 ret = ops->dev_add_platdata(pdev);
1556 else
1557 ret = platform_device_add_data(pdev, &node, sizeof(node));
1558
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001559 if (ret)
1560 goto dev_put;
1561
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001562 fwnode = iort_get_fwnode(node);
1563
1564 if (!fwnode) {
1565 ret = -ENODEV;
1566 goto dev_put;
1567 }
1568
1569 pdev->dev.fwnode = fwnode;
1570
Neil Leeder24e51602019-03-26 15:17:50 +00001571 if (ops->dev_dma_configure)
1572 ops->dev_dma_configure(&pdev->dev, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001573
Lorenzo Pieralisi65637902017-10-13 15:09:50 +08001574 iort_set_device_domain(&pdev->dev, node);
1575
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001576 ret = platform_device_add(pdev);
1577 if (ret)
1578 goto dma_deconfigure;
1579
1580 return 0;
1581
1582dma_deconfigure:
Christoph Hellwigdc3c0552018-08-24 10:28:18 +02001583 arch_teardown_dma_ops(&pdev->dev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001584dev_put:
1585 platform_device_put(pdev);
1586
1587 return ret;
1588}
1589
Sinan Kaya43554ce2018-12-19 22:46:58 +00001590#ifdef CONFIG_PCI
1591static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001592{
Sinan Kaya43554ce2018-12-19 22:46:58 +00001593 static bool acs_enabled __initdata;
1594
1595 if (acs_enabled)
1596 return;
1597
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001598 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1599 struct acpi_iort_node *parent;
1600 struct acpi_iort_id_mapping *map;
1601 int i;
1602
1603 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1604 iort_node->mapping_offset);
1605
1606 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1607 if (!map->output_reference)
1608 continue;
1609
1610 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1611 iort_table, map->output_reference);
1612 /*
1613 * If we detect a RC->SMMU mapping, make sure
1614 * we enable ACS on the system.
1615 */
1616 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1617 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1618 pci_request_acs();
Sinan Kaya43554ce2018-12-19 22:46:58 +00001619 acs_enabled = true;
1620 return;
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001621 }
1622 }
1623 }
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001624}
Sinan Kaya43554ce2018-12-19 22:46:58 +00001625#else
1626static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1627#endif
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001628
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001629static void __init iort_init_platform_devices(void)
1630{
1631 struct acpi_iort_node *iort_node, *iort_end;
1632 struct acpi_table_iort *iort;
1633 struct fwnode_handle *fwnode;
1634 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001635 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001636
1637 /*
1638 * iort_table and iort both point to the start of IORT table, but
1639 * have different struct types
1640 */
1641 iort = (struct acpi_table_iort *)iort_table;
1642
1643 /* Get the first IORT node */
1644 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1645 iort->node_offset);
1646 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1647 iort_table->length);
1648
1649 for (i = 0; i < iort->node_count; i++) {
1650 if (iort_node >= iort_end) {
1651 pr_err("iort node pointer overflows, bad table\n");
1652 return;
1653 }
1654
Sinan Kaya43554ce2018-12-19 22:46:58 +00001655 iort_enable_acs(iort_node);
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001656
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001657 ops = iort_get_dev_cfg(iort_node);
1658 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001659 fwnode = acpi_alloc_fwnode_static();
1660 if (!fwnode)
1661 return;
1662
1663 iort_set_fwnode(iort_node, fwnode);
1664
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001665 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001666 if (ret) {
1667 iort_delete_fwnode(iort_node);
1668 acpi_free_fwnode_static(fwnode);
1669 return;
1670 }
1671 }
1672
1673 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1674 iort_node->length);
1675 }
1676}
1677
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001678void __init acpi_iort_init(void)
1679{
1680 acpi_status status;
1681
Hanjun Guo701dafe2020-05-08 12:05:53 +08001682 /* iort_table will be used at runtime after the iort init,
1683 * so we don't need to call acpi_put_table() to release
1684 * the IORT table mapping.
1685 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001686 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001687 if (ACPI_FAILURE(status)) {
1688 if (status != AE_NOT_FOUND) {
1689 const char *msg = acpi_format_exception(status);
1690
1691 pr_err("Failed to get table, %s\n", msg);
1692 }
1693
1694 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001695 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001696
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001697 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001698}