blob: 9f0a9f9339f9e617ed1cdc5eadfa1cfc96715047 [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 };
267 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
268 struct acpi_iort_named_component *ncomp;
269
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800270 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200271 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200272
273 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
274 if (ACPI_FAILURE(status)) {
275 dev_warn(dev, "Can't get device full path name\n");
276 goto out;
277 }
278
279 ncomp = (struct acpi_iort_named_component *)node->node_data;
280 status = !strcmp(ncomp->device_name, buf.pointer) ?
281 AE_OK : AE_NOT_FOUND;
282 acpi_os_free(buf.pointer);
283 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
284 struct acpi_iort_root_complex *pci_rc;
285 struct pci_bus *bus;
286
287 bus = to_pci_bus(dev);
288 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
289
290 /*
291 * It is assumed that PCI segment numbers maps one-to-one
292 * with root complexes. Each segment number can represent only
293 * one root complex.
294 */
295 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
296 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200297 }
298out:
299 return status;
300}
301
302static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
303 u32 *rid_out)
304{
305 /* Single mapping does not care for input id */
306 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
307 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
308 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
309 *rid_out = map->output_base;
310 return 0;
311 }
312
313 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
314 map, type);
315 return -ENXIO;
316 }
317
318 if (rid_in < map->input_base ||
319 (rid_in >= map->input_base + map->id_count))
320 return -ENXIO;
321
322 *rid_out = map->output_base + (rid_in - map->input_base);
323 return 0;
324}
325
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100326static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
327 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000328{
329 struct acpi_iort_node *parent;
330 struct acpi_iort_id_mapping *map;
331
332 if (!node->mapping_offset || !node->mapping_count ||
333 index >= node->mapping_count)
334 return NULL;
335
336 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000337 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000338
339 /* Firmware bug! */
340 if (!map->output_reference) {
341 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
342 node, node->type);
343 return NULL;
344 }
345
346 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
347 map->output_reference);
348
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000349 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000350 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800351 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
Neil Leeder24e51602019-03-26 15:17:50 +0000352 node->type == ACPI_IORT_NODE_SMMU_V3 ||
353 node->type == ACPI_IORT_NODE_PMCG) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000354 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000355 return parent;
356 }
357 }
358
359 return NULL;
360}
361
Hanjun Guo86456a32017-10-13 15:09:49 +0800362static int iort_get_id_mapping_index(struct acpi_iort_node *node)
363{
364 struct acpi_iort_smmu_v3 *smmu;
365
366 switch (node->type) {
367 case ACPI_IORT_NODE_SMMU_V3:
368 /*
369 * SMMUv3 dev ID mapping index was introduced in revision 1
370 * table, not available in revision 0
371 */
372 if (node->revision < 1)
373 return -EINVAL;
374
375 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
376 /*
377 * ID mapping index is only ignored if all interrupts are
378 * GSIV based
379 */
380 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
381 && smmu->sync_gsiv)
382 return -EINVAL;
383
384 if (smmu->id_mapping_index >= node->mapping_count) {
385 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
386 node, node->type);
387 return -EINVAL;
388 }
389
390 return smmu->id_mapping_index;
Neil Leeder24e51602019-03-26 15:17:50 +0000391 case ACPI_IORT_NODE_PMCG:
392 return 0;
Hanjun Guo86456a32017-10-13 15:09:49 +0800393 default:
394 return -EINVAL;
395 }
396}
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800397
Hanjun Guo697f6092017-03-07 20:40:03 +0800398static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
399 u32 id_in, u32 *id_out,
400 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200401{
Hanjun Guo697f6092017-03-07 20:40:03 +0800402 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200403
404 /* Parse the ID mapping tree to find specified node type */
405 while (node) {
406 struct acpi_iort_id_mapping *map;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800407 int i, index;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200408
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000409 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800410 if (id_out)
411 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200412 return node;
413 }
414
415 if (!node->mapping_offset || !node->mapping_count)
416 goto fail_map;
417
418 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
419 node->mapping_offset);
420
421 /* Firmware bug! */
422 if (!map->output_reference) {
423 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
424 node, node->type);
425 goto fail_map;
426 }
427
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800428 /*
429 * Get the special ID mapping index (if any) and skip its
430 * associated ID map to prevent erroneous multi-stage
431 * IORT ID translations.
432 */
433 index = iort_get_id_mapping_index(node);
434
Hanjun Guo697f6092017-03-07 20:40:03 +0800435 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200436 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800437 /* if it is special mapping index, skip it */
438 if (i == index)
439 continue;
440
Hanjun Guo697f6092017-03-07 20:40:03 +0800441 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200442 break;
443 }
444
445 if (i == node->mapping_count)
446 goto fail_map;
447
448 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
449 map->output_reference);
450 }
451
452fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800453 /* Map input ID to output ID unchanged on mapping failure */
454 if (id_out)
455 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200456
457 return NULL;
458}
459
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100460static struct acpi_iort_node *iort_node_map_platform_id(
461 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
462 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800463{
464 struct acpi_iort_node *parent;
465 u32 id;
466
467 /* step 1: retrieve the initial dev id */
468 parent = iort_node_get_id(node, &id, index);
469 if (!parent)
470 return NULL;
471
472 /*
473 * optional step 2: map the initial dev id if its parent is not
474 * the target type we want, map it again for the use cases such
475 * as NC (named component) -> SMMU -> ITS. If the type is matched,
476 * return the initial dev id and its parent pointer directly.
477 */
478 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
479 parent = iort_node_map_id(parent, id, id_out, type_mask);
480 else
481 if (id_out)
482 *id_out = id;
483
484 return parent;
485}
486
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200487static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
488{
489 struct pci_bus *pbus;
490
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800491 if (!dev_is_pci(dev)) {
492 struct acpi_iort_node *node;
493 /*
494 * scan iort_fwnode_list to see if it's an iort platform
495 * device (such as SMMU, PMCG),its iort node already cached
496 * and associated with fwnode when iort platform devices
497 * were initialized.
498 */
499 node = iort_get_iort_node(dev->fwnode);
500 if (node)
501 return node;
502
503 /*
504 * if not, then it should be a platform device defined in
505 * DSDT/SSDT (with Named Component node in IORT)
506 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200507 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
508 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800509 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200510
511 /* Find a PCI root bus */
512 pbus = to_pci_dev(dev)->bus;
513 while (!pci_is_root_bus(pbus))
514 pbus = pbus->parent;
515
516 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
517 iort_match_node_callback, &pbus->dev);
518}
519
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200520/**
521 * iort_msi_map_rid() - Map a MSI requester ID for a device
522 * @dev: The device for which the mapping is to be done.
523 * @req_id: The device requester ID.
524 *
525 * Returns: mapped MSI RID on success, input requester ID otherwise
526 */
527u32 iort_msi_map_rid(struct device *dev, u32 req_id)
528{
529 struct acpi_iort_node *node;
530 u32 dev_id;
531
532 node = iort_find_dev_node(dev);
533 if (!node)
534 return req_id;
535
Hanjun Guo697f6092017-03-07 20:40:03 +0800536 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200537 return dev_id;
538}
539
540/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800541 * iort_pmsi_get_dev_id() - Get the device id for a device
542 * @dev: The device for which the mapping is to be done.
543 * @dev_id: The device ID found.
544 *
545 * Returns: 0 for successful find a dev id, -ENODEV on error
546 */
547int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
548{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800549 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800550 struct acpi_iort_node *node;
551
552 node = iort_find_dev_node(dev);
553 if (!node)
554 return -ENODEV;
555
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800556 index = iort_get_id_mapping_index(node);
557 /* if there is a valid index, go get the dev_id directly */
558 if (index >= 0) {
559 if (iort_node_get_id(node, dev_id, index))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800560 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800561 } else {
562 for (i = 0; i < node->mapping_count; i++) {
563 if (iort_node_map_platform_id(node, dev_id,
564 IORT_MSI_TYPE, i))
565 return 0;
566 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800567 }
568
569 return -ENODEV;
570}
571
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000572static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
573{
574 struct iort_its_msi_chip *its_msi_chip;
575 int ret = -ENODEV;
576
577 spin_lock(&iort_msi_chip_lock);
578 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
579 if (its_msi_chip->translation_id == its_id) {
580 *base = its_msi_chip->base_addr;
581 ret = 0;
582 break;
583 }
584 }
585 spin_unlock(&iort_msi_chip_lock);
586
587 return ret;
588}
589
Hanjun Guoae7c1832017-03-07 20:40:05 +0800590/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200591 * iort_dev_find_its_id() - Find the ITS identifier for a device
592 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800593 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200594 * @idx: Index of the ITS identifier list.
595 * @its_id: ITS identifier.
596 *
597 * Returns: 0 on success, appropriate error value otherwise
598 */
599static int iort_dev_find_its_id(struct device *dev, u32 req_id,
600 unsigned int idx, int *its_id)
601{
602 struct acpi_iort_its_group *its;
603 struct acpi_iort_node *node;
604
605 node = iort_find_dev_node(dev);
606 if (!node)
607 return -ENXIO;
608
Hanjun Guo697f6092017-03-07 20:40:03 +0800609 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200610 if (!node)
611 return -ENXIO;
612
613 /* Move to ITS specific data */
614 its = (struct acpi_iort_its_group *)node->node_data;
Lorenzo Pieralisi5a46d3f2019-07-22 17:25:48 +0100615 if (idx >= its->its_count) {
616 dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n",
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200617 idx, its->its_count);
618 return -ENXIO;
619 }
620
621 *its_id = its->identifiers[idx];
622 return 0;
623}
624
625/**
626 * iort_get_device_domain() - Find MSI domain related to a device
627 * @dev: The device.
628 * @req_id: Requester ID for the device.
629 *
630 * Returns: the MSI domain for this device, NULL otherwise
631 */
632struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
633{
634 struct fwnode_handle *handle;
635 int its_id;
636
637 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
638 return NULL;
639
640 handle = iort_find_domain_token(its_id);
641 if (!handle)
642 return NULL;
643
644 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
645}
646
Lorenzo Pieralisi65637902017-10-13 15:09:50 +0800647static void iort_set_device_domain(struct device *dev,
648 struct acpi_iort_node *node)
649{
650 struct acpi_iort_its_group *its;
651 struct acpi_iort_node *msi_parent;
652 struct acpi_iort_id_mapping *map;
653 struct fwnode_handle *iort_fwnode;
654 struct irq_domain *domain;
655 int index;
656
657 index = iort_get_id_mapping_index(node);
658 if (index < 0)
659 return;
660
661 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
662 node->mapping_offset + index * sizeof(*map));
663
664 /* Firmware bug! */
665 if (!map->output_reference ||
666 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
667 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
668 node, node->type);
669 return;
670 }
671
672 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
673 map->output_reference);
674
675 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
676 return;
677
678 /* Move to ITS specific data */
679 its = (struct acpi_iort_its_group *)msi_parent->node_data;
680
681 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
682 if (!iort_fwnode)
683 return;
684
685 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
686 if (domain)
687 dev_set_msi_domain(dev, domain);
688}
689
Hanjun Guod4f54a12017-03-07 20:40:06 +0800690/**
691 * iort_get_platform_device_domain() - Find MSI domain related to a
692 * platform device
693 * @dev: the dev pointer associated with the platform device
694 *
695 * Returns: the MSI domain for this device, NULL otherwise
696 */
697static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
698{
Lorenzo Pieralisiea2412d2018-11-29 09:55:59 +0000699 struct acpi_iort_node *node, *msi_parent = NULL;
Hanjun Guod4f54a12017-03-07 20:40:06 +0800700 struct fwnode_handle *iort_fwnode;
701 struct acpi_iort_its_group *its;
702 int i;
703
704 /* find its associated iort node */
705 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
706 iort_match_node_callback, dev);
707 if (!node)
708 return NULL;
709
710 /* then find its msi parent node */
711 for (i = 0; i < node->mapping_count; i++) {
712 msi_parent = iort_node_map_platform_id(node, NULL,
713 IORT_MSI_TYPE, i);
714 if (msi_parent)
715 break;
716 }
717
718 if (!msi_parent)
719 return NULL;
720
721 /* Move to ITS specific data */
722 its = (struct acpi_iort_its_group *)msi_parent->node_data;
723
724 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
725 if (!iort_fwnode)
726 return NULL;
727
728 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
729}
730
731void acpi_configure_pmsi_domain(struct device *dev)
732{
733 struct irq_domain *msi_domain;
734
735 msi_domain = iort_get_platform_device_domain(dev);
736 if (msi_domain)
737 dev_set_msi_domain(dev, msi_domain);
738}
739
Robin Murphybc8648d2017-08-04 17:42:06 +0100740static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
741 void *data)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000742{
743 u32 *rid = data;
744
745 *rid = alias;
746 return 0;
747}
748
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100749#ifdef CONFIG_IOMMU_API
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000750static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
751{
752 struct acpi_iort_node *iommu;
Joerg Roedel8097e532018-11-29 14:01:00 +0100753 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000754
755 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
756
757 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
758 struct acpi_iort_smmu_v3 *smmu;
759
760 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
761 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
762 return iommu;
763 }
764
765 return NULL;
766}
767
Joerg Roedel8097e532018-11-29 14:01:00 +0100768static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100769{
Joerg Roedel8097e532018-11-29 14:01:00 +0100770 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
771
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100772 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
773}
774
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100775static inline int iort_add_device_replay(const struct iommu_ops *ops,
776 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100777{
778 int err = 0;
779
Joerg Roedeld2e1a002018-12-05 14:39:45 +0100780 if (dev->bus && !device_iommu_mapped(dev))
781 err = iommu_probe_device(dev);
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100782
783 return err;
784}
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000785
786/**
787 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
788 * @dev: Device from iommu_get_resv_regions()
789 * @head: Reserved region list from iommu_get_resv_regions()
790 *
791 * Returns: Number of msi reserved regions on success (0 if platform
792 * doesn't require the reservation or no associated msi regions),
793 * appropriate error value otherwise. The ITS interrupt translation
794 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
795 * are the msi reserved regions.
796 */
797int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
798{
Joerg Roedel8097e532018-11-29 14:01:00 +0100799 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000800 struct acpi_iort_its_group *its;
801 struct acpi_iort_node *iommu_node, *its_node = NULL;
802 int i, resv = 0;
803
804 iommu_node = iort_get_msi_resv_iommu(dev);
805 if (!iommu_node)
806 return 0;
807
808 /*
809 * Current logic to reserve ITS regions relies on HW topologies
810 * where a given PCI or named component maps its IDs to only one
811 * ITS group; if a PCI or named component can map its IDs to
812 * different ITS groups through IORT mappings this function has
813 * to be reworked to ensure we reserve regions for all ITS groups
814 * a given PCI or named component may map IDs to.
815 */
816
Joerg Roedel8097e532018-11-29 14:01:00 +0100817 for (i = 0; i < fwspec->num_ids; i++) {
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000818 its_node = iort_node_map_id(iommu_node,
Joerg Roedel8097e532018-11-29 14:01:00 +0100819 fwspec->ids[i],
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000820 NULL, IORT_MSI_TYPE);
821 if (its_node)
822 break;
823 }
824
825 if (!its_node)
826 return 0;
827
828 /* Move to ITS specific data */
829 its = (struct acpi_iort_its_group *)its_node->node_data;
830
831 for (i = 0; i < its->its_count; i++) {
832 phys_addr_t base;
833
834 if (!iort_find_its_base(its->identifiers[i], &base)) {
835 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
836 struct iommu_resv_region *region;
837
838 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
839 prot, IOMMU_RESV_MSI);
840 if (region) {
841 list_add_tail(&region->list, head);
842 resv++;
843 }
844 }
845 }
846
847 return (resv == its->its_count) ? resv : -ENODEV;
848}
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100849
850static inline bool iort_iommu_driver_enabled(u8 type)
851{
852 switch (type) {
853 case ACPI_IORT_NODE_SMMU_V3:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000854 return IS_ENABLED(CONFIG_ARM_SMMU_V3);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100855 case ACPI_IORT_NODE_SMMU:
Ard Biesheuveld3daf662019-12-19 12:03:48 +0000856 return IS_ENABLED(CONFIG_ARM_SMMU);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100857 default:
858 pr_warn("IORT node type %u does not describe an SMMU\n", type);
859 return false;
860 }
861}
862
863static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
864 struct fwnode_handle *fwnode,
865 const struct iommu_ops *ops)
866{
867 int ret = iommu_fwspec_init(dev, fwnode, ops);
868
869 if (!ret)
870 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
871
872 return ret;
873}
874
875static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
876{
877 struct acpi_iort_root_complex *pci_rc;
878
879 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
880 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
881}
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100882
Robin Murphybc8648d2017-08-04 17:42:06 +0100883static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
884 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000885{
Robin Murphybc8648d2017-08-04 17:42:06 +0100886 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000887 struct fwnode_handle *iort_fwnode;
888
Robin Murphybc8648d2017-08-04 17:42:06 +0100889 if (!node)
890 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000891
Robin Murphybc8648d2017-08-04 17:42:06 +0100892 iort_fwnode = iort_get_fwnode(node);
893 if (!iort_fwnode)
894 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000895
Robin Murphybc8648d2017-08-04 17:42:06 +0100896 /*
897 * If the ops look-up fails, this means that either
898 * the SMMU drivers have not been probed yet or that
899 * the SMMU drivers are not built in the kernel;
900 * Depending on whether the SMMU drivers are built-in
901 * in the kernel or not, defer the IOMMU configuration
902 * or just abort it.
903 */
904 ops = iommu_ops_from_fwnode(iort_fwnode);
905 if (!ops)
906 return iort_iommu_driver_enabled(node->type) ?
907 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000908
Robin Murphybc8648d2017-08-04 17:42:06 +0100909 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
910}
911
912struct iort_pci_alias_info {
913 struct device *dev;
914 struct acpi_iort_node *node;
915};
916
917static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
918{
919 struct iort_pci_alias_info *info = data;
920 struct acpi_iort_node *parent;
921 u32 streamid;
922
923 parent = iort_node_map_id(info->node, alias, &streamid,
924 IORT_IOMMU_TYPE);
925 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000926}
927
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +0100928static void iort_named_component_init(struct device *dev,
929 struct acpi_iort_node *node)
930{
931 struct acpi_iort_named_component *nc;
932 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
933
934 if (!fwspec)
935 return;
936
937 nc = (struct acpi_iort_named_component *)node->node_data;
938 fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS,
939 nc->node_flags);
940}
941
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100942/**
943 * iort_iommu_configure - Set-up IOMMU configuration for a device.
944 *
945 * @dev: device to configure
946 *
947 * Returns: iommu_ops pointer on configuration success
948 * NULL on configuration failure
949 */
950const struct iommu_ops *iort_iommu_configure(struct device *dev)
951{
952 struct acpi_iort_node *node, *parent;
953 const struct iommu_ops *ops;
954 u32 streamid = 0;
955 int err = -ENODEV;
956
957 /*
958 * If we already translated the fwspec there
959 * is nothing left to do, return the iommu_ops.
960 */
961 ops = iort_fwspec_iommu_ops(dev);
962 if (ops)
963 return ops;
964
965 if (dev_is_pci(dev)) {
966 struct pci_bus *bus = to_pci_dev(dev)->bus;
967 struct iort_pci_alias_info info = { .dev = dev };
968
969 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
970 iort_match_node_callback, &bus->dev);
971 if (!node)
972 return NULL;
973
974 info.node = node;
975 err = pci_for_each_dma_alias(to_pci_dev(dev),
976 iort_pci_iommu_init, &info);
977
978 if (!err && iort_pci_rc_supports_ats(node))
979 dev->iommu_fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
980 } else {
981 int i = 0;
982
983 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
984 iort_match_node_callback, dev);
985 if (!node)
986 return NULL;
987
988 do {
989 parent = iort_node_map_platform_id(node, &streamid,
990 IORT_IOMMU_TYPE,
991 i++);
992
993 if (parent)
994 err = iort_iommu_xlate(dev, parent, streamid);
995 } while (parent && !err);
Jean-Philippe Bruckerda225652020-01-15 13:52:30 +0100996
997 if (!err)
998 iort_named_component_init(dev, node);
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100999 }
1000
1001 /*
1002 * If we have reason to believe the IOMMU driver missed the initial
1003 * add_device callback for dev, replay it to get things in order.
1004 */
1005 if (!err) {
1006 ops = iort_fwspec_iommu_ops(dev);
1007 err = iort_add_device_replay(ops, dev);
1008 }
1009
1010 /* Ignore all other errors apart from EPROBE_DEFER */
1011 if (err == -EPROBE_DEFER) {
1012 ops = ERR_PTR(err);
1013 } else if (err) {
1014 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1015 ops = NULL;
1016 }
1017
1018 return ops;
1019}
1020#else
1021static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1022{ return NULL; }
1023static inline int iort_add_device_replay(const struct iommu_ops *ops,
1024 struct device *dev)
1025{ return 0; }
1026int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1027{ return 0; }
1028const struct iommu_ops *iort_iommu_configure(struct device *dev)
1029{ return NULL; }
1030#endif
1031
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001032static int nc_dma_get_range(struct device *dev, u64 *size)
1033{
1034 struct acpi_iort_node *node;
1035 struct acpi_iort_named_component *ncomp;
1036
1037 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1038 iort_match_node_callback, dev);
1039 if (!node)
1040 return -ENODEV;
1041
1042 ncomp = (struct acpi_iort_named_component *)node->node_data;
1043
1044 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1045 1ULL<<ncomp->memory_address_limit;
1046
1047 return 0;
1048}
1049
Robin Murphy5ac65e82018-07-23 23:16:06 +01001050static int rc_dma_get_range(struct device *dev, u64 *size)
1051{
1052 struct acpi_iort_node *node;
1053 struct acpi_iort_root_complex *rc;
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001054 struct pci_bus *pbus = to_pci_dev(dev)->bus;
Robin Murphy5ac65e82018-07-23 23:16:06 +01001055
1056 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001057 iort_match_node_callback, &pbus->dev);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001058 if (!node || node->revision < 1)
1059 return -ENODEV;
1060
1061 rc = (struct acpi_iort_root_complex *)node->node_data;
1062
1063 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1064 1ULL<<rc->memory_address_limit;
1065
1066 return 0;
1067}
1068
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +00001069/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001070 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001071 *
1072 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001073 * @dma_addr: device DMA address result pointer
1074 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001075 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001076void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001077{
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001078 u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1079 int ret;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001080
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001081 /*
Robin Murphy6757cda2018-07-23 23:16:11 +01001082 * If @dev is expected to be DMA-capable then the bus code that created
1083 * it should have initialised its dma_mask pointer by this point. For
1084 * now, we'll continue the legacy behaviour of coercing it to the
1085 * coherent mask if not, but we'll no longer do so quietly.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001086 */
Robin Murphy6757cda2018-07-23 23:16:11 +01001087 if (!dev->dma_mask) {
1088 dev_warn(dev, "DMA mask not set\n");
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001089 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy6757cda2018-07-23 23:16:11 +01001090 }
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001091
Robin Murphy6757cda2018-07-23 23:16:11 +01001092 if (dev->coherent_dma_mask)
1093 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1094 else
1095 size = 1ULL << 32;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001096
Robin Murphy5ac65e82018-07-23 23:16:06 +01001097 if (dev_is_pci(dev)) {
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001098 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001099 if (ret == -ENODEV)
1100 ret = rc_dma_get_range(dev, &size);
1101 } else {
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001102 ret = nc_dma_get_range(dev, &size);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001103 }
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001104
1105 if (!ret) {
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001106 /*
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001107 * Limit coherent and dma mask based on size retrieved from
1108 * firmware.
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001109 */
Nicolas Saenz Juliennea7ba70f2019-11-21 10:26:44 +01001110 end = dmaaddr + size - 1;
1111 mask = DMA_BIT_MASK(ilog2(end) + 1);
1112 dev->bus_dma_limit = end;
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001113 dev->coherent_dma_mask = mask;
1114 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001115 }
1116
1117 *dma_addr = dmaaddr;
1118 *dma_size = size;
1119
1120 dev->dma_pfn_offset = PFN_DOWN(offset);
1121 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001122}
1123
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001124static void __init acpi_iort_register_irq(int hwirq, const char *name,
1125 int trigger,
1126 struct resource *res)
1127{
1128 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1129 ACPI_ACTIVE_HIGH);
1130
1131 if (irq <= 0) {
1132 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1133 name);
1134 return;
1135 }
1136
1137 res->start = irq;
1138 res->end = irq;
1139 res->flags = IORESOURCE_IRQ;
1140 res->name = name;
1141}
1142
1143static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1144{
1145 struct acpi_iort_smmu_v3 *smmu;
1146 /* Always present mem resource */
1147 int num_res = 1;
1148
1149 /* Retrieve SMMUv3 specific data */
1150 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1151
1152 if (smmu->event_gsiv)
1153 num_res++;
1154
1155 if (smmu->pri_gsiv)
1156 num_res++;
1157
1158 if (smmu->gerr_gsiv)
1159 num_res++;
1160
1161 if (smmu->sync_gsiv)
1162 num_res++;
1163
1164 return num_res;
1165}
1166
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301167static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1168{
1169 /*
1170 * Cavium ThunderX2 implementation doesn't not support unique
1171 * irq line. Use single irq line for all the SMMUv3 interrupts.
1172 */
1173 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1174 return false;
1175
1176 /*
1177 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1178 * SPI numbers here.
1179 */
1180 return smmu->event_gsiv == smmu->pri_gsiv &&
1181 smmu->event_gsiv == smmu->gerr_gsiv &&
1182 smmu->event_gsiv == smmu->sync_gsiv;
1183}
1184
Linu Cherian403e8c72017-06-22 17:35:36 +05301185static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1186{
1187 /*
1188 * Override the size, for Cavium ThunderX2 implementation
1189 * which doesn't support the page 1 SMMU register space.
1190 */
1191 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1192 return SZ_64K;
1193
1194 return SZ_128K;
1195}
1196
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001197static void __init arm_smmu_v3_init_resources(struct resource *res,
1198 struct acpi_iort_node *node)
1199{
1200 struct acpi_iort_smmu_v3 *smmu;
1201 int num_res = 0;
1202
1203 /* Retrieve SMMUv3 specific data */
1204 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1205
1206 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301207 res[num_res].end = smmu->base_address +
1208 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001209 res[num_res].flags = IORESOURCE_MEM;
1210
1211 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301212 if (arm_smmu_v3_is_combined_irq(smmu)) {
1213 if (smmu->event_gsiv)
1214 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1215 ACPI_EDGE_SENSITIVE,
1216 &res[num_res++]);
1217 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001218
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301219 if (smmu->event_gsiv)
1220 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1221 ACPI_EDGE_SENSITIVE,
1222 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001223
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301224 if (smmu->pri_gsiv)
1225 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1226 ACPI_EDGE_SENSITIVE,
1227 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001228
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301229 if (smmu->gerr_gsiv)
1230 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1231 ACPI_EDGE_SENSITIVE,
1232 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001233
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301234 if (smmu->sync_gsiv)
1235 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1236 ACPI_EDGE_SENSITIVE,
1237 &res[num_res++]);
1238 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001239}
1240
Neil Leeder24e51602019-03-26 15:17:50 +00001241static void __init arm_smmu_v3_dma_configure(struct device *dev,
1242 struct acpi_iort_node *node)
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001243{
1244 struct acpi_iort_smmu_v3 *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001245 enum dev_dma_attr attr;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001246
1247 /* Retrieve SMMUv3 specific data */
1248 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1249
Neil Leeder24e51602019-03-26 15:17:50 +00001250 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1251 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1252
1253 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1254 dev->dma_mask = &dev->coherent_dma_mask;
1255
1256 /* Configure DMA for the page table walker */
1257 acpi_dma_configure(dev, attr);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001258}
1259
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001260#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001261/*
1262 * set numa proximity domain for smmuv3 device
1263 */
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001264static int __init arm_smmu_v3_set_proximity(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001265 struct acpi_iort_node *node)
1266{
1267 struct acpi_iort_smmu_v3 *smmu;
1268
1269 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1270 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001271 int dev_node = acpi_map_pxm_to_node(smmu->pxm);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001272
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001273 if (dev_node != NUMA_NO_NODE && !node_online(dev_node))
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001274 return -EINVAL;
1275
Lorenzo Pieralisi3e77eeb2019-07-22 17:14:33 +01001276 set_dev_node(dev, dev_node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001277 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1278 smmu->base_address,
1279 smmu->pxm);
1280 }
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001281 return 0;
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001282}
1283#else
1284#define arm_smmu_v3_set_proximity NULL
1285#endif
1286
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001287static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1288{
1289 struct acpi_iort_smmu *smmu;
1290
1291 /* Retrieve SMMU specific data */
1292 smmu = (struct acpi_iort_smmu *)node->node_data;
1293
1294 /*
1295 * Only consider the global fault interrupt and ignore the
1296 * configuration access interrupt.
1297 *
1298 * MMIO address and global fault interrupt resources are always
1299 * present so add them to the context interrupt count as a static
1300 * value.
1301 */
1302 return smmu->context_interrupt_count + 2;
1303}
1304
1305static void __init arm_smmu_init_resources(struct resource *res,
1306 struct acpi_iort_node *node)
1307{
1308 struct acpi_iort_smmu *smmu;
1309 int i, hw_irq, trigger, num_res = 0;
1310 u64 *ctx_irq, *glb_irq;
1311
1312 /* Retrieve SMMU specific data */
1313 smmu = (struct acpi_iort_smmu *)node->node_data;
1314
1315 res[num_res].start = smmu->base_address;
1316 res[num_res].end = smmu->base_address + smmu->span - 1;
1317 res[num_res].flags = IORESOURCE_MEM;
1318 num_res++;
1319
1320 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1321 /* Global IRQs */
1322 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1323 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1324
1325 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1326 &res[num_res++]);
1327
1328 /* Context IRQs */
1329 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1330 for (i = 0; i < smmu->context_interrupt_count; i++) {
1331 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1332 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1333
1334 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1335 &res[num_res++]);
1336 }
1337}
1338
Neil Leeder24e51602019-03-26 15:17:50 +00001339static void __init arm_smmu_dma_configure(struct device *dev,
1340 struct acpi_iort_node *node)
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001341{
1342 struct acpi_iort_smmu *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001343 enum dev_dma_attr attr;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001344
1345 /* Retrieve SMMU specific data */
1346 smmu = (struct acpi_iort_smmu *)node->node_data;
1347
Neil Leeder24e51602019-03-26 15:17:50 +00001348 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1349 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1350
1351 /* We expect the dma masks to be equivalent for SMMU set-ups */
1352 dev->dma_mask = &dev->coherent_dma_mask;
1353
1354 /* Configure DMA for the page table walker */
1355 acpi_dma_configure(dev, attr);
1356}
1357
1358static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1359{
1360 struct acpi_iort_pmcg *pmcg;
1361
1362 /* Retrieve PMCG specific data */
1363 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1364
1365 /*
1366 * There are always 2 memory resources.
1367 * If the overflow_gsiv is present then add that for a total of 3.
1368 */
1369 return pmcg->overflow_gsiv ? 3 : 2;
1370}
1371
1372static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1373 struct acpi_iort_node *node)
1374{
1375 struct acpi_iort_pmcg *pmcg;
1376
1377 /* Retrieve PMCG specific data */
1378 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1379
1380 res[0].start = pmcg->page0_base_address;
1381 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1382 res[0].flags = IORESOURCE_MEM;
1383 res[1].start = pmcg->page1_base_address;
1384 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1385 res[1].flags = IORESOURCE_MEM;
1386
1387 if (pmcg->overflow_gsiv)
1388 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1389 ACPI_EDGE_SENSITIVE, &res[2]);
1390}
1391
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001392static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1393 /* HiSilicon Hip08 Platform */
1394 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1395 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1396 { }
1397};
1398
Neil Leeder24e51602019-03-26 15:17:50 +00001399static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1400{
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001401 u32 model;
1402 int idx;
1403
1404 idx = acpi_match_platform_list(pmcg_plat_info);
1405 if (idx >= 0)
1406 model = pmcg_plat_info[idx].data;
1407 else
1408 model = IORT_SMMU_V3_PMCG_GENERIC;
Neil Leeder24e51602019-03-26 15:17:50 +00001409
1410 return platform_device_add_data(pdev, &model, sizeof(model));
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001411}
1412
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001413struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001414 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001415 int (*dev_init)(struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001416 void (*dev_dma_configure)(struct device *dev,
1417 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001418 int (*dev_count_resources)(struct acpi_iort_node *node);
1419 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001420 struct acpi_iort_node *node);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001421 int (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001422 struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001423 int (*dev_add_platdata)(struct platform_device *pdev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001424};
1425
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001426static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001427 .name = "arm-smmu-v3",
Neil Leeder24e51602019-03-26 15:17:50 +00001428 .dev_dma_configure = arm_smmu_v3_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001429 .dev_count_resources = arm_smmu_v3_count_resources,
1430 .dev_init_resources = arm_smmu_v3_init_resources,
1431 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001432};
1433
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001434static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001435 .name = "arm-smmu",
Neil Leeder24e51602019-03-26 15:17:50 +00001436 .dev_dma_configure = arm_smmu_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001437 .dev_count_resources = arm_smmu_count_resources,
Neil Leeder24e51602019-03-26 15:17:50 +00001438 .dev_init_resources = arm_smmu_init_resources,
1439};
1440
1441static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1442 .name = "arm-smmu-v3-pmcg",
1443 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1444 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1445 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001446};
1447
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001448static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001449 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001450{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001451 switch (node->type) {
1452 case ACPI_IORT_NODE_SMMU_V3:
1453 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001454 case ACPI_IORT_NODE_SMMU:
1455 return &iort_arm_smmu_cfg;
Neil Leeder24e51602019-03-26 15:17:50 +00001456 case ACPI_IORT_NODE_PMCG:
1457 return &iort_arm_smmu_v3_pmcg_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001458 default:
1459 return NULL;
1460 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001461}
1462
1463/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001464 * iort_add_platform_device() - Allocate a platform device for IORT node
1465 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001466 *
1467 * Returns: 0 on success, <0 failure
1468 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001469static int __init iort_add_platform_device(struct acpi_iort_node *node,
1470 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001471{
1472 struct fwnode_handle *fwnode;
1473 struct platform_device *pdev;
1474 struct resource *r;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001475 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001476
1477 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1478 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001479 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001480
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001481 if (ops->dev_set_proximity) {
1482 ret = ops->dev_set_proximity(&pdev->dev, node);
1483 if (ret)
1484 goto dev_put;
1485 }
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001486
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001487 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001488
1489 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1490 if (!r) {
1491 ret = -ENOMEM;
1492 goto dev_put;
1493 }
1494
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001495 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001496
1497 ret = platform_device_add_resources(pdev, r, count);
1498 /*
1499 * Resources are duplicated in platform_device_add_resources,
1500 * free their allocated memory
1501 */
1502 kfree(r);
1503
1504 if (ret)
1505 goto dev_put;
1506
1507 /*
Neil Leeder24e51602019-03-26 15:17:50 +00001508 * Platform devices based on PMCG nodes uses platform_data to
1509 * pass the hardware model info to the driver. For others, add
1510 * a copy of IORT node pointer to platform_data to be used to
1511 * retrieve IORT data information.
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001512 */
Neil Leeder24e51602019-03-26 15:17:50 +00001513 if (ops->dev_add_platdata)
1514 ret = ops->dev_add_platdata(pdev);
1515 else
1516 ret = platform_device_add_data(pdev, &node, sizeof(node));
1517
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001518 if (ret)
1519 goto dev_put;
1520
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001521 fwnode = iort_get_fwnode(node);
1522
1523 if (!fwnode) {
1524 ret = -ENODEV;
1525 goto dev_put;
1526 }
1527
1528 pdev->dev.fwnode = fwnode;
1529
Neil Leeder24e51602019-03-26 15:17:50 +00001530 if (ops->dev_dma_configure)
1531 ops->dev_dma_configure(&pdev->dev, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001532
Lorenzo Pieralisi65637902017-10-13 15:09:50 +08001533 iort_set_device_domain(&pdev->dev, node);
1534
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001535 ret = platform_device_add(pdev);
1536 if (ret)
1537 goto dma_deconfigure;
1538
1539 return 0;
1540
1541dma_deconfigure:
Christoph Hellwigdc3c0552018-08-24 10:28:18 +02001542 arch_teardown_dma_ops(&pdev->dev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001543dev_put:
1544 platform_device_put(pdev);
1545
1546 return ret;
1547}
1548
Sinan Kaya43554ce2018-12-19 22:46:58 +00001549#ifdef CONFIG_PCI
1550static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001551{
Sinan Kaya43554ce2018-12-19 22:46:58 +00001552 static bool acs_enabled __initdata;
1553
1554 if (acs_enabled)
1555 return;
1556
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001557 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1558 struct acpi_iort_node *parent;
1559 struct acpi_iort_id_mapping *map;
1560 int i;
1561
1562 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1563 iort_node->mapping_offset);
1564
1565 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1566 if (!map->output_reference)
1567 continue;
1568
1569 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1570 iort_table, map->output_reference);
1571 /*
1572 * If we detect a RC->SMMU mapping, make sure
1573 * we enable ACS on the system.
1574 */
1575 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1576 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1577 pci_request_acs();
Sinan Kaya43554ce2018-12-19 22:46:58 +00001578 acs_enabled = true;
1579 return;
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001580 }
1581 }
1582 }
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001583}
Sinan Kaya43554ce2018-12-19 22:46:58 +00001584#else
1585static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1586#endif
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001587
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001588static void __init iort_init_platform_devices(void)
1589{
1590 struct acpi_iort_node *iort_node, *iort_end;
1591 struct acpi_table_iort *iort;
1592 struct fwnode_handle *fwnode;
1593 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001594 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001595
1596 /*
1597 * iort_table and iort both point to the start of IORT table, but
1598 * have different struct types
1599 */
1600 iort = (struct acpi_table_iort *)iort_table;
1601
1602 /* Get the first IORT node */
1603 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1604 iort->node_offset);
1605 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1606 iort_table->length);
1607
1608 for (i = 0; i < iort->node_count; i++) {
1609 if (iort_node >= iort_end) {
1610 pr_err("iort node pointer overflows, bad table\n");
1611 return;
1612 }
1613
Sinan Kaya43554ce2018-12-19 22:46:58 +00001614 iort_enable_acs(iort_node);
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001615
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001616 ops = iort_get_dev_cfg(iort_node);
1617 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001618 fwnode = acpi_alloc_fwnode_static();
1619 if (!fwnode)
1620 return;
1621
1622 iort_set_fwnode(iort_node, fwnode);
1623
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001624 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001625 if (ret) {
1626 iort_delete_fwnode(iort_node);
1627 acpi_free_fwnode_static(fwnode);
1628 return;
1629 }
1630 }
1631
1632 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1633 iort_node->length);
1634 }
1635}
1636
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001637void __init acpi_iort_init(void)
1638{
1639 acpi_status status;
1640
1641 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001642 if (ACPI_FAILURE(status)) {
1643 if (status != AE_NOT_FOUND) {
1644 const char *msg = acpi_format_exception(status);
1645
1646 pr_err("Failed to get table, %s\n", msg);
1647 }
1648
1649 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001650 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001651
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001652 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001653}