blob: b5390b4c9ade8431cee6e3cc483813f4e2be9e2a [file] [log] [blame]
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001/*
2 * Copyright (C) 2016, Semihalf
3 * Author: Tomasz Nowicki <tn@semihalf.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * This file implements early detection/parsing of I/O mapping
15 * reported to OS through firmware via I/O Remapping Table (IORT)
16 * IORT document number: ARM DEN 0049A
17 */
18
19#define pr_fmt(fmt) "ACPI: IORT: " fmt
20
21#include <linux/acpi_iort.h>
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +000022#include <linux/iommu.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020023#include <linux/kernel.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000024#include <linux/list.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020025#include <linux/pci.h>
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +000026#include <linux/platform_device.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000027#include <linux/slab.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020028
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000029#define IORT_TYPE_MASK(type) (1 << (type))
30#define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +000031#define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
32 (1 << ACPI_IORT_NODE_SMMU_V3))
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +000033
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020034struct iort_its_msi_chip {
35 struct list_head list;
36 struct fwnode_handle *fw_node;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +000037 phys_addr_t base_addr;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020038 u32 translation_id;
39};
40
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000041struct iort_fwnode {
42 struct list_head list;
43 struct acpi_iort_node *iort_node;
44 struct fwnode_handle *fwnode;
45};
46static LIST_HEAD(iort_fwnode_list);
47static DEFINE_SPINLOCK(iort_fwnode_lock);
48
49/**
50 * iort_set_fwnode() - Create iort_fwnode and use it to register
51 * iommu data in the iort_fwnode_list
52 *
53 * @node: IORT table node associated with the IOMMU
54 * @fwnode: fwnode associated with the IORT node
55 *
56 * Returns: 0 on success
57 * <0 on failure
58 */
59static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
60 struct fwnode_handle *fwnode)
61{
62 struct iort_fwnode *np;
63
64 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
65
66 if (WARN_ON(!np))
67 return -ENOMEM;
68
69 INIT_LIST_HEAD(&np->list);
70 np->iort_node = iort_node;
71 np->fwnode = fwnode;
72
73 spin_lock(&iort_fwnode_lock);
74 list_add_tail(&np->list, &iort_fwnode_list);
75 spin_unlock(&iort_fwnode_lock);
76
77 return 0;
78}
79
80/**
81 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
82 *
83 * @node: IORT table node to be looked-up
84 *
85 * Returns: fwnode_handle pointer on success, NULL on failure
86 */
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +010087static inline struct fwnode_handle *iort_get_fwnode(
88 struct acpi_iort_node *node)
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000089{
90 struct iort_fwnode *curr;
91 struct fwnode_handle *fwnode = NULL;
92
93 spin_lock(&iort_fwnode_lock);
94 list_for_each_entry(curr, &iort_fwnode_list, list) {
95 if (curr->iort_node == node) {
96 fwnode = curr->fwnode;
97 break;
98 }
99 }
100 spin_unlock(&iort_fwnode_lock);
101
102 return fwnode;
103}
104
105/**
106 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
107 *
108 * @node: IORT table node associated with fwnode to delete
109 */
110static inline void iort_delete_fwnode(struct acpi_iort_node *node)
111{
112 struct iort_fwnode *curr, *tmp;
113
114 spin_lock(&iort_fwnode_lock);
115 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
116 if (curr->iort_node == node) {
117 list_del(&curr->list);
118 kfree(curr);
119 break;
120 }
121 }
122 spin_unlock(&iort_fwnode_lock);
123}
124
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800125/**
126 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
127 *
128 * @fwnode: fwnode associated with device to be looked-up
129 *
130 * Returns: iort_node pointer on success, NULL on failure
131 */
132static inline struct acpi_iort_node *iort_get_iort_node(
133 struct fwnode_handle *fwnode)
134{
135 struct iort_fwnode *curr;
136 struct acpi_iort_node *iort_node = NULL;
137
138 spin_lock(&iort_fwnode_lock);
139 list_for_each_entry(curr, &iort_fwnode_list, list) {
140 if (curr->fwnode == fwnode) {
141 iort_node = curr->iort_node;
142 break;
143 }
144 }
145 spin_unlock(&iort_fwnode_lock);
146
147 return iort_node;
148}
149
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200150typedef acpi_status (*iort_find_node_callback)
151 (struct acpi_iort_node *node, void *context);
152
153/* Root pointer to the mapped IORT table */
154static struct acpi_table_header *iort_table;
155
156static LIST_HEAD(iort_msi_chip_list);
157static DEFINE_SPINLOCK(iort_msi_chip_lock);
158
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200159/**
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000160 * iort_register_domain_token() - register domain token along with related
161 * ITS ID and base address to the list from where we can get it back later on.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200162 * @trans_id: ITS ID.
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000163 * @base: ITS base address.
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200164 * @fw_node: Domain token.
165 *
166 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
167 */
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000168int iort_register_domain_token(int trans_id, phys_addr_t base,
169 struct fwnode_handle *fw_node)
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200170{
171 struct iort_its_msi_chip *its_msi_chip;
172
173 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
174 if (!its_msi_chip)
175 return -ENOMEM;
176
177 its_msi_chip->fw_node = fw_node;
178 its_msi_chip->translation_id = trans_id;
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000179 its_msi_chip->base_addr = base;
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200180
181 spin_lock(&iort_msi_chip_lock);
182 list_add(&its_msi_chip->list, &iort_msi_chip_list);
183 spin_unlock(&iort_msi_chip_lock);
184
185 return 0;
186}
187
188/**
189 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
190 * @trans_id: ITS ID.
191 *
192 * Returns: none.
193 */
194void iort_deregister_domain_token(int trans_id)
195{
196 struct iort_its_msi_chip *its_msi_chip, *t;
197
198 spin_lock(&iort_msi_chip_lock);
199 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
200 if (its_msi_chip->translation_id == trans_id) {
201 list_del(&its_msi_chip->list);
202 kfree(its_msi_chip);
203 break;
204 }
205 }
206 spin_unlock(&iort_msi_chip_lock);
207}
208
209/**
210 * iort_find_domain_token() - Find domain token based on given ITS ID
211 * @trans_id: ITS ID.
212 *
213 * Returns: domain token when find on the list, NULL otherwise
214 */
215struct fwnode_handle *iort_find_domain_token(int trans_id)
216{
217 struct fwnode_handle *fw_node = NULL;
218 struct iort_its_msi_chip *its_msi_chip;
219
220 spin_lock(&iort_msi_chip_lock);
221 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
222 if (its_msi_chip->translation_id == trans_id) {
223 fw_node = its_msi_chip->fw_node;
224 break;
225 }
226 }
227 spin_unlock(&iort_msi_chip_lock);
228
229 return fw_node;
230}
231
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200232static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
233 iort_find_node_callback callback,
234 void *context)
235{
236 struct acpi_iort_node *iort_node, *iort_end;
237 struct acpi_table_iort *iort;
238 int i;
239
240 if (!iort_table)
241 return NULL;
242
243 /* Get the first IORT node */
244 iort = (struct acpi_table_iort *)iort_table;
245 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
246 iort->node_offset);
247 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
248 iort_table->length);
249
250 for (i = 0; i < iort->node_count; i++) {
251 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
252 "IORT node pointer overflows, bad table!\n"))
253 return NULL;
254
255 if (iort_node->type == type &&
256 ACPI_SUCCESS(callback(iort_node, context)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800257 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200258
259 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
260 iort_node->length);
261 }
262
263 return NULL;
264}
265
266static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
267 void *context)
268{
269 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800270 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200271
272 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
273 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
274 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
275 struct acpi_iort_named_component *ncomp;
276
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800277 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200278 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200279
280 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
281 if (ACPI_FAILURE(status)) {
282 dev_warn(dev, "Can't get device full path name\n");
283 goto out;
284 }
285
286 ncomp = (struct acpi_iort_named_component *)node->node_data;
287 status = !strcmp(ncomp->device_name, buf.pointer) ?
288 AE_OK : AE_NOT_FOUND;
289 acpi_os_free(buf.pointer);
290 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
291 struct acpi_iort_root_complex *pci_rc;
292 struct pci_bus *bus;
293
294 bus = to_pci_bus(dev);
295 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
296
297 /*
298 * It is assumed that PCI segment numbers maps one-to-one
299 * with root complexes. Each segment number can represent only
300 * one root complex.
301 */
302 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
303 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200304 }
305out:
306 return status;
307}
308
309static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
310 u32 *rid_out)
311{
312 /* Single mapping does not care for input id */
313 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
314 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
315 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
316 *rid_out = map->output_base;
317 return 0;
318 }
319
320 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
321 map, type);
322 return -ENXIO;
323 }
324
325 if (rid_in < map->input_base ||
326 (rid_in >= map->input_base + map->id_count))
327 return -ENXIO;
328
329 *rid_out = map->output_base + (rid_in - map->input_base);
330 return 0;
331}
332
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100333static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
334 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000335{
336 struct acpi_iort_node *parent;
337 struct acpi_iort_id_mapping *map;
338
339 if (!node->mapping_offset || !node->mapping_count ||
340 index >= node->mapping_count)
341 return NULL;
342
343 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000344 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000345
346 /* Firmware bug! */
347 if (!map->output_reference) {
348 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
349 node, node->type);
350 return NULL;
351 }
352
353 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
354 map->output_reference);
355
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000356 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000357 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800358 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
Neil Leeder24e51602019-03-26 15:17:50 +0000359 node->type == ACPI_IORT_NODE_SMMU_V3 ||
360 node->type == ACPI_IORT_NODE_PMCG) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000361 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000362 return parent;
363 }
364 }
365
366 return NULL;
367}
368
Hanjun Guo86456a32017-10-13 15:09:49 +0800369static int iort_get_id_mapping_index(struct acpi_iort_node *node)
370{
371 struct acpi_iort_smmu_v3 *smmu;
372
373 switch (node->type) {
374 case ACPI_IORT_NODE_SMMU_V3:
375 /*
376 * SMMUv3 dev ID mapping index was introduced in revision 1
377 * table, not available in revision 0
378 */
379 if (node->revision < 1)
380 return -EINVAL;
381
382 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
383 /*
384 * ID mapping index is only ignored if all interrupts are
385 * GSIV based
386 */
387 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
388 && smmu->sync_gsiv)
389 return -EINVAL;
390
391 if (smmu->id_mapping_index >= node->mapping_count) {
392 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
393 node, node->type);
394 return -EINVAL;
395 }
396
397 return smmu->id_mapping_index;
Neil Leeder24e51602019-03-26 15:17:50 +0000398 case ACPI_IORT_NODE_PMCG:
399 return 0;
Hanjun Guo86456a32017-10-13 15:09:49 +0800400 default:
401 return -EINVAL;
402 }
403}
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800404
Hanjun Guo697f6092017-03-07 20:40:03 +0800405static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
406 u32 id_in, u32 *id_out,
407 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200408{
Hanjun Guo697f6092017-03-07 20:40:03 +0800409 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200410
411 /* Parse the ID mapping tree to find specified node type */
412 while (node) {
413 struct acpi_iort_id_mapping *map;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800414 int i, index;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200415
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000416 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800417 if (id_out)
418 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200419 return node;
420 }
421
422 if (!node->mapping_offset || !node->mapping_count)
423 goto fail_map;
424
425 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
426 node->mapping_offset);
427
428 /* Firmware bug! */
429 if (!map->output_reference) {
430 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
431 node, node->type);
432 goto fail_map;
433 }
434
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800435 /*
436 * Get the special ID mapping index (if any) and skip its
437 * associated ID map to prevent erroneous multi-stage
438 * IORT ID translations.
439 */
440 index = iort_get_id_mapping_index(node);
441
Hanjun Guo697f6092017-03-07 20:40:03 +0800442 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200443 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800444 /* if it is special mapping index, skip it */
445 if (i == index)
446 continue;
447
Hanjun Guo697f6092017-03-07 20:40:03 +0800448 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200449 break;
450 }
451
452 if (i == node->mapping_count)
453 goto fail_map;
454
455 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
456 map->output_reference);
457 }
458
459fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800460 /* Map input ID to output ID unchanged on mapping failure */
461 if (id_out)
462 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200463
464 return NULL;
465}
466
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100467static struct acpi_iort_node *iort_node_map_platform_id(
468 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
469 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800470{
471 struct acpi_iort_node *parent;
472 u32 id;
473
474 /* step 1: retrieve the initial dev id */
475 parent = iort_node_get_id(node, &id, index);
476 if (!parent)
477 return NULL;
478
479 /*
480 * optional step 2: map the initial dev id if its parent is not
481 * the target type we want, map it again for the use cases such
482 * as NC (named component) -> SMMU -> ITS. If the type is matched,
483 * return the initial dev id and its parent pointer directly.
484 */
485 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
486 parent = iort_node_map_id(parent, id, id_out, type_mask);
487 else
488 if (id_out)
489 *id_out = id;
490
491 return parent;
492}
493
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200494static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
495{
496 struct pci_bus *pbus;
497
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800498 if (!dev_is_pci(dev)) {
499 struct acpi_iort_node *node;
500 /*
501 * scan iort_fwnode_list to see if it's an iort platform
502 * device (such as SMMU, PMCG),its iort node already cached
503 * and associated with fwnode when iort platform devices
504 * were initialized.
505 */
506 node = iort_get_iort_node(dev->fwnode);
507 if (node)
508 return node;
509
510 /*
511 * if not, then it should be a platform device defined in
512 * DSDT/SSDT (with Named Component node in IORT)
513 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200514 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
515 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800516 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200517
518 /* Find a PCI root bus */
519 pbus = to_pci_dev(dev)->bus;
520 while (!pci_is_root_bus(pbus))
521 pbus = pbus->parent;
522
523 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
524 iort_match_node_callback, &pbus->dev);
525}
526
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200527/**
528 * iort_msi_map_rid() - Map a MSI requester ID for a device
529 * @dev: The device for which the mapping is to be done.
530 * @req_id: The device requester ID.
531 *
532 * Returns: mapped MSI RID on success, input requester ID otherwise
533 */
534u32 iort_msi_map_rid(struct device *dev, u32 req_id)
535{
536 struct acpi_iort_node *node;
537 u32 dev_id;
538
539 node = iort_find_dev_node(dev);
540 if (!node)
541 return req_id;
542
Hanjun Guo697f6092017-03-07 20:40:03 +0800543 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200544 return dev_id;
545}
546
547/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800548 * iort_pmsi_get_dev_id() - Get the device id for a device
549 * @dev: The device for which the mapping is to be done.
550 * @dev_id: The device ID found.
551 *
552 * Returns: 0 for successful find a dev id, -ENODEV on error
553 */
554int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
555{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800556 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800557 struct acpi_iort_node *node;
558
559 node = iort_find_dev_node(dev);
560 if (!node)
561 return -ENODEV;
562
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800563 index = iort_get_id_mapping_index(node);
564 /* if there is a valid index, go get the dev_id directly */
565 if (index >= 0) {
566 if (iort_node_get_id(node, dev_id, index))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800567 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800568 } else {
569 for (i = 0; i < node->mapping_count; i++) {
570 if (iort_node_map_platform_id(node, dev_id,
571 IORT_MSI_TYPE, i))
572 return 0;
573 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800574 }
575
576 return -ENODEV;
577}
578
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000579static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base)
580{
581 struct iort_its_msi_chip *its_msi_chip;
582 int ret = -ENODEV;
583
584 spin_lock(&iort_msi_chip_lock);
585 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
586 if (its_msi_chip->translation_id == its_id) {
587 *base = its_msi_chip->base_addr;
588 ret = 0;
589 break;
590 }
591 }
592 spin_unlock(&iort_msi_chip_lock);
593
594 return ret;
595}
596
Hanjun Guoae7c1832017-03-07 20:40:05 +0800597/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200598 * iort_dev_find_its_id() - Find the ITS identifier for a device
599 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800600 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200601 * @idx: Index of the ITS identifier list.
602 * @its_id: ITS identifier.
603 *
604 * Returns: 0 on success, appropriate error value otherwise
605 */
606static int iort_dev_find_its_id(struct device *dev, u32 req_id,
607 unsigned int idx, int *its_id)
608{
609 struct acpi_iort_its_group *its;
610 struct acpi_iort_node *node;
611
612 node = iort_find_dev_node(dev);
613 if (!node)
614 return -ENXIO;
615
Hanjun Guo697f6092017-03-07 20:40:03 +0800616 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200617 if (!node)
618 return -ENXIO;
619
620 /* Move to ITS specific data */
621 its = (struct acpi_iort_its_group *)node->node_data;
622 if (idx > its->its_count) {
623 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
624 idx, its->its_count);
625 return -ENXIO;
626 }
627
628 *its_id = its->identifiers[idx];
629 return 0;
630}
631
632/**
633 * iort_get_device_domain() - Find MSI domain related to a device
634 * @dev: The device.
635 * @req_id: Requester ID for the device.
636 *
637 * Returns: the MSI domain for this device, NULL otherwise
638 */
639struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
640{
641 struct fwnode_handle *handle;
642 int its_id;
643
644 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
645 return NULL;
646
647 handle = iort_find_domain_token(its_id);
648 if (!handle)
649 return NULL;
650
651 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
652}
653
Lorenzo Pieralisi65637902017-10-13 15:09:50 +0800654static void iort_set_device_domain(struct device *dev,
655 struct acpi_iort_node *node)
656{
657 struct acpi_iort_its_group *its;
658 struct acpi_iort_node *msi_parent;
659 struct acpi_iort_id_mapping *map;
660 struct fwnode_handle *iort_fwnode;
661 struct irq_domain *domain;
662 int index;
663
664 index = iort_get_id_mapping_index(node);
665 if (index < 0)
666 return;
667
668 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
669 node->mapping_offset + index * sizeof(*map));
670
671 /* Firmware bug! */
672 if (!map->output_reference ||
673 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
674 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
675 node, node->type);
676 return;
677 }
678
679 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
680 map->output_reference);
681
682 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
683 return;
684
685 /* Move to ITS specific data */
686 its = (struct acpi_iort_its_group *)msi_parent->node_data;
687
688 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
689 if (!iort_fwnode)
690 return;
691
692 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
693 if (domain)
694 dev_set_msi_domain(dev, domain);
695}
696
Hanjun Guod4f54a12017-03-07 20:40:06 +0800697/**
698 * iort_get_platform_device_domain() - Find MSI domain related to a
699 * platform device
700 * @dev: the dev pointer associated with the platform device
701 *
702 * Returns: the MSI domain for this device, NULL otherwise
703 */
704static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
705{
Lorenzo Pieralisiea2412d2018-11-29 09:55:59 +0000706 struct acpi_iort_node *node, *msi_parent = NULL;
Hanjun Guod4f54a12017-03-07 20:40:06 +0800707 struct fwnode_handle *iort_fwnode;
708 struct acpi_iort_its_group *its;
709 int i;
710
711 /* find its associated iort node */
712 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
713 iort_match_node_callback, dev);
714 if (!node)
715 return NULL;
716
717 /* then find its msi parent node */
718 for (i = 0; i < node->mapping_count; i++) {
719 msi_parent = iort_node_map_platform_id(node, NULL,
720 IORT_MSI_TYPE, i);
721 if (msi_parent)
722 break;
723 }
724
725 if (!msi_parent)
726 return NULL;
727
728 /* Move to ITS specific data */
729 its = (struct acpi_iort_its_group *)msi_parent->node_data;
730
731 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
732 if (!iort_fwnode)
733 return NULL;
734
735 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
736}
737
738void acpi_configure_pmsi_domain(struct device *dev)
739{
740 struct irq_domain *msi_domain;
741
742 msi_domain = iort_get_platform_device_domain(dev);
743 if (msi_domain)
744 dev_set_msi_domain(dev, msi_domain);
745}
746
Robin Murphybc8648d2017-08-04 17:42:06 +0100747static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
748 void *data)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000749{
750 u32 *rid = data;
751
752 *rid = alias;
753 return 0;
754}
755
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100756#ifdef CONFIG_IOMMU_API
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000757static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev)
758{
759 struct acpi_iort_node *iommu;
Joerg Roedel8097e532018-11-29 14:01:00 +0100760 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000761
762 iommu = iort_get_iort_node(fwspec->iommu_fwnode);
763
764 if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) {
765 struct acpi_iort_smmu_v3 *smmu;
766
767 smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data;
768 if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X)
769 return iommu;
770 }
771
772 return NULL;
773}
774
Joerg Roedel8097e532018-11-29 14:01:00 +0100775static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100776{
Joerg Roedel8097e532018-11-29 14:01:00 +0100777 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
778
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100779 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
780}
781
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100782static inline int iort_add_device_replay(const struct iommu_ops *ops,
783 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100784{
785 int err = 0;
786
Joerg Roedeld2e1a002018-12-05 14:39:45 +0100787 if (dev->bus && !device_iommu_mapped(dev))
788 err = iommu_probe_device(dev);
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100789
790 return err;
791}
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000792
793/**
794 * iort_iommu_msi_get_resv_regions - Reserved region driver helper
795 * @dev: Device from iommu_get_resv_regions()
796 * @head: Reserved region list from iommu_get_resv_regions()
797 *
798 * Returns: Number of msi reserved regions on success (0 if platform
799 * doesn't require the reservation or no associated msi regions),
800 * appropriate error value otherwise. The ITS interrupt translation
801 * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device
802 * are the msi reserved regions.
803 */
804int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
805{
Joerg Roedel8097e532018-11-29 14:01:00 +0100806 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000807 struct acpi_iort_its_group *its;
808 struct acpi_iort_node *iommu_node, *its_node = NULL;
809 int i, resv = 0;
810
811 iommu_node = iort_get_msi_resv_iommu(dev);
812 if (!iommu_node)
813 return 0;
814
815 /*
816 * Current logic to reserve ITS regions relies on HW topologies
817 * where a given PCI or named component maps its IDs to only one
818 * ITS group; if a PCI or named component can map its IDs to
819 * different ITS groups through IORT mappings this function has
820 * to be reworked to ensure we reserve regions for all ITS groups
821 * a given PCI or named component may map IDs to.
822 */
823
Joerg Roedel8097e532018-11-29 14:01:00 +0100824 for (i = 0; i < fwspec->num_ids; i++) {
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000825 its_node = iort_node_map_id(iommu_node,
Joerg Roedel8097e532018-11-29 14:01:00 +0100826 fwspec->ids[i],
Shameer Kolothum8b4282e2018-02-13 15:20:50 +0000827 NULL, IORT_MSI_TYPE);
828 if (its_node)
829 break;
830 }
831
832 if (!its_node)
833 return 0;
834
835 /* Move to ITS specific data */
836 its = (struct acpi_iort_its_group *)its_node->node_data;
837
838 for (i = 0; i < its->its_count; i++) {
839 phys_addr_t base;
840
841 if (!iort_find_its_base(its->identifiers[i], &base)) {
842 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
843 struct iommu_resv_region *region;
844
845 region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K,
846 prot, IOMMU_RESV_MSI);
847 if (region) {
848 list_add_tail(&region->list, head);
849 resv++;
850 }
851 }
852 }
853
854 return (resv == its->its_count) ? resv : -ENODEV;
855}
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100856
857static inline bool iort_iommu_driver_enabled(u8 type)
858{
859 switch (type) {
860 case ACPI_IORT_NODE_SMMU_V3:
861 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
862 case ACPI_IORT_NODE_SMMU:
863 return IS_BUILTIN(CONFIG_ARM_SMMU);
864 default:
865 pr_warn("IORT node type %u does not describe an SMMU\n", type);
866 return false;
867 }
868}
869
870static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
871 struct fwnode_handle *fwnode,
872 const struct iommu_ops *ops)
873{
874 int ret = iommu_fwspec_init(dev, fwnode, ops);
875
876 if (!ret)
877 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
878
879 return ret;
880}
881
882static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node)
883{
884 struct acpi_iort_root_complex *pci_rc;
885
886 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
887 return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED;
888}
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100889
Robin Murphybc8648d2017-08-04 17:42:06 +0100890static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
891 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000892{
Robin Murphybc8648d2017-08-04 17:42:06 +0100893 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000894 struct fwnode_handle *iort_fwnode;
895
Robin Murphybc8648d2017-08-04 17:42:06 +0100896 if (!node)
897 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000898
Robin Murphybc8648d2017-08-04 17:42:06 +0100899 iort_fwnode = iort_get_fwnode(node);
900 if (!iort_fwnode)
901 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000902
Robin Murphybc8648d2017-08-04 17:42:06 +0100903 /*
904 * If the ops look-up fails, this means that either
905 * the SMMU drivers have not been probed yet or that
906 * the SMMU drivers are not built in the kernel;
907 * Depending on whether the SMMU drivers are built-in
908 * in the kernel or not, defer the IOMMU configuration
909 * or just abort it.
910 */
911 ops = iommu_ops_from_fwnode(iort_fwnode);
912 if (!ops)
913 return iort_iommu_driver_enabled(node->type) ?
914 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000915
Robin Murphybc8648d2017-08-04 17:42:06 +0100916 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
917}
918
919struct iort_pci_alias_info {
920 struct device *dev;
921 struct acpi_iort_node *node;
922};
923
924static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
925{
926 struct iort_pci_alias_info *info = data;
927 struct acpi_iort_node *parent;
928 u32 streamid;
929
930 parent = iort_node_map_id(info->node, alias, &streamid,
931 IORT_IOMMU_TYPE);
932 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000933}
934
Lorenzo Pieralisi82126882019-05-16 16:52:58 +0100935/**
936 * iort_iommu_configure - Set-up IOMMU configuration for a device.
937 *
938 * @dev: device to configure
939 *
940 * Returns: iommu_ops pointer on configuration success
941 * NULL on configuration failure
942 */
943const struct iommu_ops *iort_iommu_configure(struct device *dev)
944{
945 struct acpi_iort_node *node, *parent;
946 const struct iommu_ops *ops;
947 u32 streamid = 0;
948 int err = -ENODEV;
949
950 /*
951 * If we already translated the fwspec there
952 * is nothing left to do, return the iommu_ops.
953 */
954 ops = iort_fwspec_iommu_ops(dev);
955 if (ops)
956 return ops;
957
958 if (dev_is_pci(dev)) {
959 struct pci_bus *bus = to_pci_dev(dev)->bus;
960 struct iort_pci_alias_info info = { .dev = dev };
961
962 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
963 iort_match_node_callback, &bus->dev);
964 if (!node)
965 return NULL;
966
967 info.node = node;
968 err = pci_for_each_dma_alias(to_pci_dev(dev),
969 iort_pci_iommu_init, &info);
970
971 if (!err && iort_pci_rc_supports_ats(node))
972 dev->iommu_fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS;
973 } else {
974 int i = 0;
975
976 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
977 iort_match_node_callback, dev);
978 if (!node)
979 return NULL;
980
981 do {
982 parent = iort_node_map_platform_id(node, &streamid,
983 IORT_IOMMU_TYPE,
984 i++);
985
986 if (parent)
987 err = iort_iommu_xlate(dev, parent, streamid);
988 } while (parent && !err);
989 }
990
991 /*
992 * If we have reason to believe the IOMMU driver missed the initial
993 * add_device callback for dev, replay it to get things in order.
994 */
995 if (!err) {
996 ops = iort_fwspec_iommu_ops(dev);
997 err = iort_add_device_replay(ops, dev);
998 }
999
1000 /* Ignore all other errors apart from EPROBE_DEFER */
1001 if (err == -EPROBE_DEFER) {
1002 ops = ERR_PTR(err);
1003 } else if (err) {
1004 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
1005 ops = NULL;
1006 }
1007
1008 return ops;
1009}
1010#else
1011static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev)
1012{ return NULL; }
1013static inline int iort_add_device_replay(const struct iommu_ops *ops,
1014 struct device *dev)
1015{ return 0; }
1016int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head)
1017{ return 0; }
1018const struct iommu_ops *iort_iommu_configure(struct device *dev)
1019{ return NULL; }
1020#endif
1021
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001022static int nc_dma_get_range(struct device *dev, u64 *size)
1023{
1024 struct acpi_iort_node *node;
1025 struct acpi_iort_named_component *ncomp;
1026
1027 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
1028 iort_match_node_callback, dev);
1029 if (!node)
1030 return -ENODEV;
1031
1032 ncomp = (struct acpi_iort_named_component *)node->node_data;
1033
1034 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
1035 1ULL<<ncomp->memory_address_limit;
1036
1037 return 0;
1038}
1039
Robin Murphy5ac65e82018-07-23 23:16:06 +01001040static int rc_dma_get_range(struct device *dev, u64 *size)
1041{
1042 struct acpi_iort_node *node;
1043 struct acpi_iort_root_complex *rc;
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001044 struct pci_bus *pbus = to_pci_dev(dev)->bus;
Robin Murphy5ac65e82018-07-23 23:16:06 +01001045
1046 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
Jean-Philippe Bruckerc7777232019-01-10 18:41:51 +00001047 iort_match_node_callback, &pbus->dev);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001048 if (!node || node->revision < 1)
1049 return -ENODEV;
1050
1051 rc = (struct acpi_iort_root_complex *)node->node_data;
1052
1053 *size = rc->memory_address_limit >= 64 ? U64_MAX :
1054 1ULL<<rc->memory_address_limit;
1055
1056 return 0;
1057}
1058
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +00001059/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001060 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001061 *
1062 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001063 * @dma_addr: device DMA address result pointer
1064 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001065 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001066void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001067{
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001068 u64 mask, dmaaddr = 0, size = 0, offset = 0;
1069 int ret, msb;
1070
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001071 /*
Robin Murphy6757cda2018-07-23 23:16:11 +01001072 * If @dev is expected to be DMA-capable then the bus code that created
1073 * it should have initialised its dma_mask pointer by this point. For
1074 * now, we'll continue the legacy behaviour of coercing it to the
1075 * coherent mask if not, but we'll no longer do so quietly.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001076 */
Robin Murphy6757cda2018-07-23 23:16:11 +01001077 if (!dev->dma_mask) {
1078 dev_warn(dev, "DMA mask not set\n");
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001079 dev->dma_mask = &dev->coherent_dma_mask;
Robin Murphy6757cda2018-07-23 23:16:11 +01001080 }
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001081
Robin Murphy6757cda2018-07-23 23:16:11 +01001082 if (dev->coherent_dma_mask)
1083 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
1084 else
1085 size = 1ULL << 32;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001086
Robin Murphy5ac65e82018-07-23 23:16:06 +01001087 if (dev_is_pci(dev)) {
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001088 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001089 if (ret == -ENODEV)
1090 ret = rc_dma_get_range(dev, &size);
1091 } else {
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001092 ret = nc_dma_get_range(dev, &size);
Robin Murphy5ac65e82018-07-23 23:16:06 +01001093 }
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001094
1095 if (!ret) {
1096 msb = fls64(dmaaddr + size - 1);
1097 /*
1098 * Round-up to the power-of-two mask or set
1099 * the mask to the whole 64-bit address space
1100 * in case the DMA region covers the full
1101 * memory window.
1102 */
1103 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
1104 /*
1105 * Limit coherent and dma mask based on size
1106 * retrieved from firmware.
1107 */
Robin Murphyd74ea712018-07-23 23:16:08 +01001108 dev->bus_dma_mask = mask;
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +01001109 dev->coherent_dma_mask = mask;
1110 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +01001111 }
1112
1113 *dma_addr = dmaaddr;
1114 *dma_size = size;
1115
1116 dev->dma_pfn_offset = PFN_DOWN(offset);
1117 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +00001118}
1119
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001120static void __init acpi_iort_register_irq(int hwirq, const char *name,
1121 int trigger,
1122 struct resource *res)
1123{
1124 int irq = acpi_register_gsi(NULL, hwirq, trigger,
1125 ACPI_ACTIVE_HIGH);
1126
1127 if (irq <= 0) {
1128 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
1129 name);
1130 return;
1131 }
1132
1133 res->start = irq;
1134 res->end = irq;
1135 res->flags = IORESOURCE_IRQ;
1136 res->name = name;
1137}
1138
1139static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
1140{
1141 struct acpi_iort_smmu_v3 *smmu;
1142 /* Always present mem resource */
1143 int num_res = 1;
1144
1145 /* Retrieve SMMUv3 specific data */
1146 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1147
1148 if (smmu->event_gsiv)
1149 num_res++;
1150
1151 if (smmu->pri_gsiv)
1152 num_res++;
1153
1154 if (smmu->gerr_gsiv)
1155 num_res++;
1156
1157 if (smmu->sync_gsiv)
1158 num_res++;
1159
1160 return num_res;
1161}
1162
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301163static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1164{
1165 /*
1166 * Cavium ThunderX2 implementation doesn't not support unique
1167 * irq line. Use single irq line for all the SMMUv3 interrupts.
1168 */
1169 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1170 return false;
1171
1172 /*
1173 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1174 * SPI numbers here.
1175 */
1176 return smmu->event_gsiv == smmu->pri_gsiv &&
1177 smmu->event_gsiv == smmu->gerr_gsiv &&
1178 smmu->event_gsiv == smmu->sync_gsiv;
1179}
1180
Linu Cherian403e8c72017-06-22 17:35:36 +05301181static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1182{
1183 /*
1184 * Override the size, for Cavium ThunderX2 implementation
1185 * which doesn't support the page 1 SMMU register space.
1186 */
1187 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1188 return SZ_64K;
1189
1190 return SZ_128K;
1191}
1192
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001193static void __init arm_smmu_v3_init_resources(struct resource *res,
1194 struct acpi_iort_node *node)
1195{
1196 struct acpi_iort_smmu_v3 *smmu;
1197 int num_res = 0;
1198
1199 /* Retrieve SMMUv3 specific data */
1200 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1201
1202 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301203 res[num_res].end = smmu->base_address +
1204 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001205 res[num_res].flags = IORESOURCE_MEM;
1206
1207 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301208 if (arm_smmu_v3_is_combined_irq(smmu)) {
1209 if (smmu->event_gsiv)
1210 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1211 ACPI_EDGE_SENSITIVE,
1212 &res[num_res++]);
1213 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001214
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301215 if (smmu->event_gsiv)
1216 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1217 ACPI_EDGE_SENSITIVE,
1218 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001219
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301220 if (smmu->pri_gsiv)
1221 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1222 ACPI_EDGE_SENSITIVE,
1223 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001224
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301225 if (smmu->gerr_gsiv)
1226 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1227 ACPI_EDGE_SENSITIVE,
1228 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001229
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301230 if (smmu->sync_gsiv)
1231 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1232 ACPI_EDGE_SENSITIVE,
1233 &res[num_res++]);
1234 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001235}
1236
Neil Leeder24e51602019-03-26 15:17:50 +00001237static void __init arm_smmu_v3_dma_configure(struct device *dev,
1238 struct acpi_iort_node *node)
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001239{
1240 struct acpi_iort_smmu_v3 *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001241 enum dev_dma_attr attr;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001242
1243 /* Retrieve SMMUv3 specific data */
1244 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1245
Neil Leeder24e51602019-03-26 15:17:50 +00001246 attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ?
1247 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1248
1249 /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */
1250 dev->dma_mask = &dev->coherent_dma_mask;
1251
1252 /* Configure DMA for the page table walker */
1253 acpi_dma_configure(dev, attr);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001254}
1255
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001256#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001257/*
1258 * set numa proximity domain for smmuv3 device
1259 */
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001260static int __init arm_smmu_v3_set_proximity(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001261 struct acpi_iort_node *node)
1262{
1263 struct acpi_iort_smmu_v3 *smmu;
1264
1265 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1266 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001267 int node = acpi_map_pxm_to_node(smmu->pxm);
1268
1269 if (node != NUMA_NO_NODE && !node_online(node))
1270 return -EINVAL;
1271
1272 set_dev_node(dev, node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001273 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1274 smmu->base_address,
1275 smmu->pxm);
1276 }
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001277 return 0;
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001278}
1279#else
1280#define arm_smmu_v3_set_proximity NULL
1281#endif
1282
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001283static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1284{
1285 struct acpi_iort_smmu *smmu;
1286
1287 /* Retrieve SMMU specific data */
1288 smmu = (struct acpi_iort_smmu *)node->node_data;
1289
1290 /*
1291 * Only consider the global fault interrupt and ignore the
1292 * configuration access interrupt.
1293 *
1294 * MMIO address and global fault interrupt resources are always
1295 * present so add them to the context interrupt count as a static
1296 * value.
1297 */
1298 return smmu->context_interrupt_count + 2;
1299}
1300
1301static void __init arm_smmu_init_resources(struct resource *res,
1302 struct acpi_iort_node *node)
1303{
1304 struct acpi_iort_smmu *smmu;
1305 int i, hw_irq, trigger, num_res = 0;
1306 u64 *ctx_irq, *glb_irq;
1307
1308 /* Retrieve SMMU specific data */
1309 smmu = (struct acpi_iort_smmu *)node->node_data;
1310
1311 res[num_res].start = smmu->base_address;
1312 res[num_res].end = smmu->base_address + smmu->span - 1;
1313 res[num_res].flags = IORESOURCE_MEM;
1314 num_res++;
1315
1316 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1317 /* Global IRQs */
1318 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1319 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1320
1321 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1322 &res[num_res++]);
1323
1324 /* Context IRQs */
1325 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1326 for (i = 0; i < smmu->context_interrupt_count; i++) {
1327 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1328 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1329
1330 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1331 &res[num_res++]);
1332 }
1333}
1334
Neil Leeder24e51602019-03-26 15:17:50 +00001335static void __init arm_smmu_dma_configure(struct device *dev,
1336 struct acpi_iort_node *node)
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001337{
1338 struct acpi_iort_smmu *smmu;
Neil Leeder24e51602019-03-26 15:17:50 +00001339 enum dev_dma_attr attr;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001340
1341 /* Retrieve SMMU specific data */
1342 smmu = (struct acpi_iort_smmu *)node->node_data;
1343
Neil Leeder24e51602019-03-26 15:17:50 +00001344 attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ?
1345 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
1346
1347 /* We expect the dma masks to be equivalent for SMMU set-ups */
1348 dev->dma_mask = &dev->coherent_dma_mask;
1349
1350 /* Configure DMA for the page table walker */
1351 acpi_dma_configure(dev, attr);
1352}
1353
1354static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node)
1355{
1356 struct acpi_iort_pmcg *pmcg;
1357
1358 /* Retrieve PMCG specific data */
1359 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1360
1361 /*
1362 * There are always 2 memory resources.
1363 * If the overflow_gsiv is present then add that for a total of 3.
1364 */
1365 return pmcg->overflow_gsiv ? 3 : 2;
1366}
1367
1368static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res,
1369 struct acpi_iort_node *node)
1370{
1371 struct acpi_iort_pmcg *pmcg;
1372
1373 /* Retrieve PMCG specific data */
1374 pmcg = (struct acpi_iort_pmcg *)node->node_data;
1375
1376 res[0].start = pmcg->page0_base_address;
1377 res[0].end = pmcg->page0_base_address + SZ_4K - 1;
1378 res[0].flags = IORESOURCE_MEM;
1379 res[1].start = pmcg->page1_base_address;
1380 res[1].end = pmcg->page1_base_address + SZ_4K - 1;
1381 res[1].flags = IORESOURCE_MEM;
1382
1383 if (pmcg->overflow_gsiv)
1384 acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow",
1385 ACPI_EDGE_SENSITIVE, &res[2]);
1386}
1387
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001388static struct acpi_platform_list pmcg_plat_info[] __initdata = {
1389 /* HiSilicon Hip08 Platform */
1390 {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal,
1391 "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08},
1392 { }
1393};
1394
Neil Leeder24e51602019-03-26 15:17:50 +00001395static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev)
1396{
Shameer Kolothum24062fe2019-03-26 15:17:53 +00001397 u32 model;
1398 int idx;
1399
1400 idx = acpi_match_platform_list(pmcg_plat_info);
1401 if (idx >= 0)
1402 model = pmcg_plat_info[idx].data;
1403 else
1404 model = IORT_SMMU_V3_PMCG_GENERIC;
Neil Leeder24e51602019-03-26 15:17:50 +00001405
1406 return platform_device_add_data(pdev, &model, sizeof(model));
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001407}
1408
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001409struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001410 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001411 int (*dev_init)(struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001412 void (*dev_dma_configure)(struct device *dev,
1413 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001414 int (*dev_count_resources)(struct acpi_iort_node *node);
1415 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001416 struct acpi_iort_node *node);
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001417 int (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001418 struct acpi_iort_node *node);
Neil Leeder24e51602019-03-26 15:17:50 +00001419 int (*dev_add_platdata)(struct platform_device *pdev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001420};
1421
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001422static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001423 .name = "arm-smmu-v3",
Neil Leeder24e51602019-03-26 15:17:50 +00001424 .dev_dma_configure = arm_smmu_v3_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001425 .dev_count_resources = arm_smmu_v3_count_resources,
1426 .dev_init_resources = arm_smmu_v3_init_resources,
1427 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001428};
1429
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001430static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001431 .name = "arm-smmu",
Neil Leeder24e51602019-03-26 15:17:50 +00001432 .dev_dma_configure = arm_smmu_dma_configure,
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001433 .dev_count_resources = arm_smmu_count_resources,
Neil Leeder24e51602019-03-26 15:17:50 +00001434 .dev_init_resources = arm_smmu_init_resources,
1435};
1436
1437static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = {
1438 .name = "arm-smmu-v3-pmcg",
1439 .dev_count_resources = arm_smmu_v3_pmcg_count_resources,
1440 .dev_init_resources = arm_smmu_v3_pmcg_init_resources,
1441 .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata,
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001442};
1443
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001444static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001445 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001446{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001447 switch (node->type) {
1448 case ACPI_IORT_NODE_SMMU_V3:
1449 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001450 case ACPI_IORT_NODE_SMMU:
1451 return &iort_arm_smmu_cfg;
Neil Leeder24e51602019-03-26 15:17:50 +00001452 case ACPI_IORT_NODE_PMCG:
1453 return &iort_arm_smmu_v3_pmcg_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001454 default:
1455 return NULL;
1456 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001457}
1458
1459/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001460 * iort_add_platform_device() - Allocate a platform device for IORT node
1461 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001462 *
1463 * Returns: 0 on success, <0 failure
1464 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001465static int __init iort_add_platform_device(struct acpi_iort_node *node,
1466 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001467{
1468 struct fwnode_handle *fwnode;
1469 struct platform_device *pdev;
1470 struct resource *r;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001471 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001472
1473 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1474 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001475 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001476
Kefeng Wang36a2ba02019-04-08 23:21:12 +08001477 if (ops->dev_set_proximity) {
1478 ret = ops->dev_set_proximity(&pdev->dev, node);
1479 if (ret)
1480 goto dev_put;
1481 }
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001482
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001483 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001484
1485 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1486 if (!r) {
1487 ret = -ENOMEM;
1488 goto dev_put;
1489 }
1490
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001491 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001492
1493 ret = platform_device_add_resources(pdev, r, count);
1494 /*
1495 * Resources are duplicated in platform_device_add_resources,
1496 * free their allocated memory
1497 */
1498 kfree(r);
1499
1500 if (ret)
1501 goto dev_put;
1502
1503 /*
Neil Leeder24e51602019-03-26 15:17:50 +00001504 * Platform devices based on PMCG nodes uses platform_data to
1505 * pass the hardware model info to the driver. For others, add
1506 * a copy of IORT node pointer to platform_data to be used to
1507 * retrieve IORT data information.
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001508 */
Neil Leeder24e51602019-03-26 15:17:50 +00001509 if (ops->dev_add_platdata)
1510 ret = ops->dev_add_platdata(pdev);
1511 else
1512 ret = platform_device_add_data(pdev, &node, sizeof(node));
1513
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001514 if (ret)
1515 goto dev_put;
1516
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001517 fwnode = iort_get_fwnode(node);
1518
1519 if (!fwnode) {
1520 ret = -ENODEV;
1521 goto dev_put;
1522 }
1523
1524 pdev->dev.fwnode = fwnode;
1525
Neil Leeder24e51602019-03-26 15:17:50 +00001526 if (ops->dev_dma_configure)
1527 ops->dev_dma_configure(&pdev->dev, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001528
Lorenzo Pieralisi65637902017-10-13 15:09:50 +08001529 iort_set_device_domain(&pdev->dev, node);
1530
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001531 ret = platform_device_add(pdev);
1532 if (ret)
1533 goto dma_deconfigure;
1534
1535 return 0;
1536
1537dma_deconfigure:
Christoph Hellwigdc3c0552018-08-24 10:28:18 +02001538 arch_teardown_dma_ops(&pdev->dev);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001539dev_put:
1540 platform_device_put(pdev);
1541
1542 return ret;
1543}
1544
Sinan Kaya43554ce2018-12-19 22:46:58 +00001545#ifdef CONFIG_PCI
1546static void __init iort_enable_acs(struct acpi_iort_node *iort_node)
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001547{
Sinan Kaya43554ce2018-12-19 22:46:58 +00001548 static bool acs_enabled __initdata;
1549
1550 if (acs_enabled)
1551 return;
1552
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001553 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1554 struct acpi_iort_node *parent;
1555 struct acpi_iort_id_mapping *map;
1556 int i;
1557
1558 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1559 iort_node->mapping_offset);
1560
1561 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1562 if (!map->output_reference)
1563 continue;
1564
1565 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1566 iort_table, map->output_reference);
1567 /*
1568 * If we detect a RC->SMMU mapping, make sure
1569 * we enable ACS on the system.
1570 */
1571 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1572 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1573 pci_request_acs();
Sinan Kaya43554ce2018-12-19 22:46:58 +00001574 acs_enabled = true;
1575 return;
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001576 }
1577 }
1578 }
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001579}
Sinan Kaya43554ce2018-12-19 22:46:58 +00001580#else
1581static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { }
1582#endif
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001583
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001584static void __init iort_init_platform_devices(void)
1585{
1586 struct acpi_iort_node *iort_node, *iort_end;
1587 struct acpi_table_iort *iort;
1588 struct fwnode_handle *fwnode;
1589 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001590 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001591
1592 /*
1593 * iort_table and iort both point to the start of IORT table, but
1594 * have different struct types
1595 */
1596 iort = (struct acpi_table_iort *)iort_table;
1597
1598 /* Get the first IORT node */
1599 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1600 iort->node_offset);
1601 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1602 iort_table->length);
1603
1604 for (i = 0; i < iort->node_count; i++) {
1605 if (iort_node >= iort_end) {
1606 pr_err("iort node pointer overflows, bad table\n");
1607 return;
1608 }
1609
Sinan Kaya43554ce2018-12-19 22:46:58 +00001610 iort_enable_acs(iort_node);
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001611
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001612 ops = iort_get_dev_cfg(iort_node);
1613 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001614 fwnode = acpi_alloc_fwnode_static();
1615 if (!fwnode)
1616 return;
1617
1618 iort_set_fwnode(iort_node, fwnode);
1619
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001620 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001621 if (ret) {
1622 iort_delete_fwnode(iort_node);
1623 acpi_free_fwnode_static(fwnode);
1624 return;
1625 }
1626 }
1627
1628 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1629 iort_node->length);
1630 }
1631}
1632
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001633void __init acpi_iort_init(void)
1634{
1635 acpi_status status;
1636
1637 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001638 if (ACPI_FAILURE(status)) {
1639 if (status != AE_NOT_FOUND) {
1640 const char *msg = acpi_format_exception(status);
1641
1642 pr_err("Failed to get table, %s\n", msg);
1643 }
1644
1645 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001646 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001647
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001648 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001649}