blob: 83cd59dcb8b3b750a7ebaf8fe4b32a6d3f99cc9b [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;
37 u32 translation_id;
38};
39
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000040struct iort_fwnode {
41 struct list_head list;
42 struct acpi_iort_node *iort_node;
43 struct fwnode_handle *fwnode;
44};
45static LIST_HEAD(iort_fwnode_list);
46static DEFINE_SPINLOCK(iort_fwnode_lock);
47
48/**
49 * iort_set_fwnode() - Create iort_fwnode and use it to register
50 * iommu data in the iort_fwnode_list
51 *
52 * @node: IORT table node associated with the IOMMU
53 * @fwnode: fwnode associated with the IORT node
54 *
55 * Returns: 0 on success
56 * <0 on failure
57 */
58static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
59 struct fwnode_handle *fwnode)
60{
61 struct iort_fwnode *np;
62
63 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
64
65 if (WARN_ON(!np))
66 return -ENOMEM;
67
68 INIT_LIST_HEAD(&np->list);
69 np->iort_node = iort_node;
70 np->fwnode = fwnode;
71
72 spin_lock(&iort_fwnode_lock);
73 list_add_tail(&np->list, &iort_fwnode_list);
74 spin_unlock(&iort_fwnode_lock);
75
76 return 0;
77}
78
79/**
80 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
81 *
82 * @node: IORT table node to be looked-up
83 *
84 * Returns: fwnode_handle pointer on success, NULL on failure
85 */
86static inline
87struct fwnode_handle *iort_get_fwnode(struct acpi_iort_node *node)
88{
89 struct iort_fwnode *curr;
90 struct fwnode_handle *fwnode = NULL;
91
92 spin_lock(&iort_fwnode_lock);
93 list_for_each_entry(curr, &iort_fwnode_list, list) {
94 if (curr->iort_node == node) {
95 fwnode = curr->fwnode;
96 break;
97 }
98 }
99 spin_unlock(&iort_fwnode_lock);
100
101 return fwnode;
102}
103
104/**
105 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
106 *
107 * @node: IORT table node associated with fwnode to delete
108 */
109static inline void iort_delete_fwnode(struct acpi_iort_node *node)
110{
111 struct iort_fwnode *curr, *tmp;
112
113 spin_lock(&iort_fwnode_lock);
114 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
115 if (curr->iort_node == node) {
116 list_del(&curr->list);
117 kfree(curr);
118 break;
119 }
120 }
121 spin_unlock(&iort_fwnode_lock);
122}
123
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200124typedef acpi_status (*iort_find_node_callback)
125 (struct acpi_iort_node *node, void *context);
126
127/* Root pointer to the mapped IORT table */
128static struct acpi_table_header *iort_table;
129
130static LIST_HEAD(iort_msi_chip_list);
131static DEFINE_SPINLOCK(iort_msi_chip_lock);
132
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200133/**
134 * iort_register_domain_token() - register domain token and related ITS ID
135 * to the list from where we can get it back later on.
136 * @trans_id: ITS ID.
137 * @fw_node: Domain token.
138 *
139 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
140 */
141int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
142{
143 struct iort_its_msi_chip *its_msi_chip;
144
145 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
146 if (!its_msi_chip)
147 return -ENOMEM;
148
149 its_msi_chip->fw_node = fw_node;
150 its_msi_chip->translation_id = trans_id;
151
152 spin_lock(&iort_msi_chip_lock);
153 list_add(&its_msi_chip->list, &iort_msi_chip_list);
154 spin_unlock(&iort_msi_chip_lock);
155
156 return 0;
157}
158
159/**
160 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
161 * @trans_id: ITS ID.
162 *
163 * Returns: none.
164 */
165void iort_deregister_domain_token(int trans_id)
166{
167 struct iort_its_msi_chip *its_msi_chip, *t;
168
169 spin_lock(&iort_msi_chip_lock);
170 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
171 if (its_msi_chip->translation_id == trans_id) {
172 list_del(&its_msi_chip->list);
173 kfree(its_msi_chip);
174 break;
175 }
176 }
177 spin_unlock(&iort_msi_chip_lock);
178}
179
180/**
181 * iort_find_domain_token() - Find domain token based on given ITS ID
182 * @trans_id: ITS ID.
183 *
184 * Returns: domain token when find on the list, NULL otherwise
185 */
186struct fwnode_handle *iort_find_domain_token(int trans_id)
187{
188 struct fwnode_handle *fw_node = NULL;
189 struct iort_its_msi_chip *its_msi_chip;
190
191 spin_lock(&iort_msi_chip_lock);
192 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
193 if (its_msi_chip->translation_id == trans_id) {
194 fw_node = its_msi_chip->fw_node;
195 break;
196 }
197 }
198 spin_unlock(&iort_msi_chip_lock);
199
200 return fw_node;
201}
202
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200203static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
204 iort_find_node_callback callback,
205 void *context)
206{
207 struct acpi_iort_node *iort_node, *iort_end;
208 struct acpi_table_iort *iort;
209 int i;
210
211 if (!iort_table)
212 return NULL;
213
214 /* Get the first IORT node */
215 iort = (struct acpi_table_iort *)iort_table;
216 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
217 iort->node_offset);
218 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
219 iort_table->length);
220
221 for (i = 0; i < iort->node_count; i++) {
222 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
223 "IORT node pointer overflows, bad table!\n"))
224 return NULL;
225
226 if (iort_node->type == type &&
227 ACPI_SUCCESS(callback(iort_node, context)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800228 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200229
230 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
231 iort_node->length);
232 }
233
234 return NULL;
235}
236
Lorenzo Pieralisibdca0c02016-11-21 10:01:40 +0000237static acpi_status
238iort_match_type_callback(struct acpi_iort_node *node, void *context)
239{
240 return AE_OK;
241}
242
243bool iort_node_match(u8 type)
244{
245 struct acpi_iort_node *node;
246
247 node = iort_scan_node(type, iort_match_type_callback, NULL);
248
249 return node != NULL;
250}
251
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200252static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
253 void *context)
254{
255 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800256 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200257
258 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
259 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
260 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
261 struct acpi_iort_named_component *ncomp;
262
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800263 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200264 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200265
266 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
267 if (ACPI_FAILURE(status)) {
268 dev_warn(dev, "Can't get device full path name\n");
269 goto out;
270 }
271
272 ncomp = (struct acpi_iort_named_component *)node->node_data;
273 status = !strcmp(ncomp->device_name, buf.pointer) ?
274 AE_OK : AE_NOT_FOUND;
275 acpi_os_free(buf.pointer);
276 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
277 struct acpi_iort_root_complex *pci_rc;
278 struct pci_bus *bus;
279
280 bus = to_pci_bus(dev);
281 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
282
283 /*
284 * It is assumed that PCI segment numbers maps one-to-one
285 * with root complexes. Each segment number can represent only
286 * one root complex.
287 */
288 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
289 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200290 }
291out:
292 return status;
293}
294
295static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
296 u32 *rid_out)
297{
298 /* Single mapping does not care for input id */
299 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
300 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
301 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
302 *rid_out = map->output_base;
303 return 0;
304 }
305
306 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
307 map, type);
308 return -ENXIO;
309 }
310
311 if (rid_in < map->input_base ||
312 (rid_in >= map->input_base + map->id_count))
313 return -ENXIO;
314
315 *rid_out = map->output_base + (rid_in - map->input_base);
316 return 0;
317}
318
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000319static
320struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800321 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000322{
323 struct acpi_iort_node *parent;
324 struct acpi_iort_id_mapping *map;
325
326 if (!node->mapping_offset || !node->mapping_count ||
327 index >= node->mapping_count)
328 return NULL;
329
330 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000331 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000332
333 /* Firmware bug! */
334 if (!map->output_reference) {
335 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
336 node, node->type);
337 return NULL;
338 }
339
340 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
341 map->output_reference);
342
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000343 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000344 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
345 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000346 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000347 return parent;
348 }
349 }
350
351 return NULL;
352}
353
Hanjun Guo697f6092017-03-07 20:40:03 +0800354static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
355 u32 id_in, u32 *id_out,
356 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200357{
Hanjun Guo697f6092017-03-07 20:40:03 +0800358 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200359
360 /* Parse the ID mapping tree to find specified node type */
361 while (node) {
362 struct acpi_iort_id_mapping *map;
363 int i;
364
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000365 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800366 if (id_out)
367 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200368 return node;
369 }
370
371 if (!node->mapping_offset || !node->mapping_count)
372 goto fail_map;
373
374 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
375 node->mapping_offset);
376
377 /* Firmware bug! */
378 if (!map->output_reference) {
379 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
380 node, node->type);
381 goto fail_map;
382 }
383
Hanjun Guo697f6092017-03-07 20:40:03 +0800384 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200385 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800386 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200387 break;
388 }
389
390 if (i == node->mapping_count)
391 goto fail_map;
392
393 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
394 map->output_reference);
395 }
396
397fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800398 /* Map input ID to output ID unchanged on mapping failure */
399 if (id_out)
400 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200401
402 return NULL;
403}
404
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800405static
406struct acpi_iort_node *iort_node_map_platform_id(struct acpi_iort_node *node,
407 u32 *id_out, u8 type_mask,
408 int index)
409{
410 struct acpi_iort_node *parent;
411 u32 id;
412
413 /* step 1: retrieve the initial dev id */
414 parent = iort_node_get_id(node, &id, index);
415 if (!parent)
416 return NULL;
417
418 /*
419 * optional step 2: map the initial dev id if its parent is not
420 * the target type we want, map it again for the use cases such
421 * as NC (named component) -> SMMU -> ITS. If the type is matched,
422 * return the initial dev id and its parent pointer directly.
423 */
424 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
425 parent = iort_node_map_id(parent, id, id_out, type_mask);
426 else
427 if (id_out)
428 *id_out = id;
429
430 return parent;
431}
432
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200433static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
434{
435 struct pci_bus *pbus;
436
437 if (!dev_is_pci(dev))
438 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
439 iort_match_node_callback, dev);
440
441 /* Find a PCI root bus */
442 pbus = to_pci_dev(dev)->bus;
443 while (!pci_is_root_bus(pbus))
444 pbus = pbus->parent;
445
446 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
447 iort_match_node_callback, &pbus->dev);
448}
449
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200450/**
451 * iort_msi_map_rid() - Map a MSI requester ID for a device
452 * @dev: The device for which the mapping is to be done.
453 * @req_id: The device requester ID.
454 *
455 * Returns: mapped MSI RID on success, input requester ID otherwise
456 */
457u32 iort_msi_map_rid(struct device *dev, u32 req_id)
458{
459 struct acpi_iort_node *node;
460 u32 dev_id;
461
462 node = iort_find_dev_node(dev);
463 if (!node)
464 return req_id;
465
Hanjun Guo697f6092017-03-07 20:40:03 +0800466 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200467 return dev_id;
468}
469
470/**
471 * iort_dev_find_its_id() - Find the ITS identifier for a device
472 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800473 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200474 * @idx: Index of the ITS identifier list.
475 * @its_id: ITS identifier.
476 *
477 * Returns: 0 on success, appropriate error value otherwise
478 */
479static int iort_dev_find_its_id(struct device *dev, u32 req_id,
480 unsigned int idx, int *its_id)
481{
482 struct acpi_iort_its_group *its;
483 struct acpi_iort_node *node;
484
485 node = iort_find_dev_node(dev);
486 if (!node)
487 return -ENXIO;
488
Hanjun Guo697f6092017-03-07 20:40:03 +0800489 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200490 if (!node)
491 return -ENXIO;
492
493 /* Move to ITS specific data */
494 its = (struct acpi_iort_its_group *)node->node_data;
495 if (idx > its->its_count) {
496 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
497 idx, its->its_count);
498 return -ENXIO;
499 }
500
501 *its_id = its->identifiers[idx];
502 return 0;
503}
504
505/**
506 * iort_get_device_domain() - Find MSI domain related to a device
507 * @dev: The device.
508 * @req_id: Requester ID for the device.
509 *
510 * Returns: the MSI domain for this device, NULL otherwise
511 */
512struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
513{
514 struct fwnode_handle *handle;
515 int its_id;
516
517 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
518 return NULL;
519
520 handle = iort_find_domain_token(its_id);
521 if (!handle)
522 return NULL;
523
524 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
525}
526
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000527static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
528{
529 u32 *rid = data;
530
531 *rid = alias;
532 return 0;
533}
534
535static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
536 struct fwnode_handle *fwnode,
537 const struct iommu_ops *ops)
538{
539 int ret = iommu_fwspec_init(dev, fwnode, ops);
540
541 if (!ret)
542 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
543
544 return ret;
545}
546
547static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
548 struct acpi_iort_node *node,
549 u32 streamid)
550{
551 const struct iommu_ops *ops = NULL;
552 int ret = -ENODEV;
553 struct fwnode_handle *iort_fwnode;
554
555 if (node) {
556 iort_fwnode = iort_get_fwnode(node);
557 if (!iort_fwnode)
558 return NULL;
559
Joerg Roedel534766d2017-01-31 16:58:42 +0100560 ops = iommu_ops_from_fwnode(iort_fwnode);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000561 if (!ops)
562 return NULL;
563
564 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
565 }
566
567 return ret ? NULL : ops;
568}
569
570/**
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000571 * iort_set_dma_mask - Set-up dma mask for a device.
572 *
573 * @dev: device to configure
574 */
575void iort_set_dma_mask(struct device *dev)
576{
577 /*
578 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
579 * setup the correct supported mask.
580 */
581 if (!dev->coherent_dma_mask)
582 dev->coherent_dma_mask = DMA_BIT_MASK(32);
583
584 /*
585 * Set it to coherent_dma_mask by default if the architecture
586 * code has not set it.
587 */
588 if (!dev->dma_mask)
589 dev->dma_mask = &dev->coherent_dma_mask;
590}
591
592/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000593 * iort_iommu_configure - Set-up IOMMU configuration for a device.
594 *
595 * @dev: device to configure
596 *
597 * Returns: iommu_ops pointer on configuration success
598 * NULL on configuration failure
599 */
600const struct iommu_ops *iort_iommu_configure(struct device *dev)
601{
602 struct acpi_iort_node *node, *parent;
603 const struct iommu_ops *ops = NULL;
604 u32 streamid = 0;
605
606 if (dev_is_pci(dev)) {
607 struct pci_bus *bus = to_pci_dev(dev)->bus;
608 u32 rid;
609
610 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
611 &rid);
612
613 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
614 iort_match_node_callback, &bus->dev);
615 if (!node)
616 return NULL;
617
Hanjun Guo697f6092017-03-07 20:40:03 +0800618 parent = iort_node_map_id(node, rid, &streamid,
619 IORT_IOMMU_TYPE);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000620
621 ops = iort_iommu_xlate(dev, parent, streamid);
622
623 } else {
624 int i = 0;
625
626 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
627 iort_match_node_callback, dev);
628 if (!node)
629 return NULL;
630
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800631 parent = iort_node_map_platform_id(node, &streamid,
632 IORT_IOMMU_TYPE, i++);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000633
634 while (parent) {
635 ops = iort_iommu_xlate(dev, parent, streamid);
636
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800637 parent = iort_node_map_platform_id(node, &streamid,
638 IORT_IOMMU_TYPE,
639 i++);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000640 }
641 }
642
643 return ops;
644}
645
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000646static void __init acpi_iort_register_irq(int hwirq, const char *name,
647 int trigger,
648 struct resource *res)
649{
650 int irq = acpi_register_gsi(NULL, hwirq, trigger,
651 ACPI_ACTIVE_HIGH);
652
653 if (irq <= 0) {
654 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
655 name);
656 return;
657 }
658
659 res->start = irq;
660 res->end = irq;
661 res->flags = IORESOURCE_IRQ;
662 res->name = name;
663}
664
665static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
666{
667 struct acpi_iort_smmu_v3 *smmu;
668 /* Always present mem resource */
669 int num_res = 1;
670
671 /* Retrieve SMMUv3 specific data */
672 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
673
674 if (smmu->event_gsiv)
675 num_res++;
676
677 if (smmu->pri_gsiv)
678 num_res++;
679
680 if (smmu->gerr_gsiv)
681 num_res++;
682
683 if (smmu->sync_gsiv)
684 num_res++;
685
686 return num_res;
687}
688
689static void __init arm_smmu_v3_init_resources(struct resource *res,
690 struct acpi_iort_node *node)
691{
692 struct acpi_iort_smmu_v3 *smmu;
693 int num_res = 0;
694
695 /* Retrieve SMMUv3 specific data */
696 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
697
698 res[num_res].start = smmu->base_address;
699 res[num_res].end = smmu->base_address + SZ_128K - 1;
700 res[num_res].flags = IORESOURCE_MEM;
701
702 num_res++;
703
704 if (smmu->event_gsiv)
705 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
706 ACPI_EDGE_SENSITIVE,
707 &res[num_res++]);
708
709 if (smmu->pri_gsiv)
710 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
711 ACPI_EDGE_SENSITIVE,
712 &res[num_res++]);
713
714 if (smmu->gerr_gsiv)
715 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
716 ACPI_EDGE_SENSITIVE,
717 &res[num_res++]);
718
719 if (smmu->sync_gsiv)
720 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
721 ACPI_EDGE_SENSITIVE,
722 &res[num_res++]);
723}
724
725static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
726{
727 struct acpi_iort_smmu_v3 *smmu;
728
729 /* Retrieve SMMUv3 specific data */
730 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
731
732 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
733}
734
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000735static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
736{
737 struct acpi_iort_smmu *smmu;
738
739 /* Retrieve SMMU specific data */
740 smmu = (struct acpi_iort_smmu *)node->node_data;
741
742 /*
743 * Only consider the global fault interrupt and ignore the
744 * configuration access interrupt.
745 *
746 * MMIO address and global fault interrupt resources are always
747 * present so add them to the context interrupt count as a static
748 * value.
749 */
750 return smmu->context_interrupt_count + 2;
751}
752
753static void __init arm_smmu_init_resources(struct resource *res,
754 struct acpi_iort_node *node)
755{
756 struct acpi_iort_smmu *smmu;
757 int i, hw_irq, trigger, num_res = 0;
758 u64 *ctx_irq, *glb_irq;
759
760 /* Retrieve SMMU specific data */
761 smmu = (struct acpi_iort_smmu *)node->node_data;
762
763 res[num_res].start = smmu->base_address;
764 res[num_res].end = smmu->base_address + smmu->span - 1;
765 res[num_res].flags = IORESOURCE_MEM;
766 num_res++;
767
768 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
769 /* Global IRQs */
770 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
771 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
772
773 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
774 &res[num_res++]);
775
776 /* Context IRQs */
777 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
778 for (i = 0; i < smmu->context_interrupt_count; i++) {
779 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
780 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
781
782 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
783 &res[num_res++]);
784 }
785}
786
787static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
788{
789 struct acpi_iort_smmu *smmu;
790
791 /* Retrieve SMMU specific data */
792 smmu = (struct acpi_iort_smmu *)node->node_data;
793
794 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
795}
796
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000797struct iort_iommu_config {
798 const char *name;
799 int (*iommu_init)(struct acpi_iort_node *node);
800 bool (*iommu_is_coherent)(struct acpi_iort_node *node);
801 int (*iommu_count_resources)(struct acpi_iort_node *node);
802 void (*iommu_init_resources)(struct resource *res,
803 struct acpi_iort_node *node);
804};
805
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000806static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
807 .name = "arm-smmu-v3",
808 .iommu_is_coherent = arm_smmu_v3_is_coherent,
809 .iommu_count_resources = arm_smmu_v3_count_resources,
810 .iommu_init_resources = arm_smmu_v3_init_resources
811};
812
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000813static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
814 .name = "arm-smmu",
815 .iommu_is_coherent = arm_smmu_is_coherent,
816 .iommu_count_resources = arm_smmu_count_resources,
817 .iommu_init_resources = arm_smmu_init_resources
818};
819
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000820static __init
821const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
822{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000823 switch (node->type) {
824 case ACPI_IORT_NODE_SMMU_V3:
825 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000826 case ACPI_IORT_NODE_SMMU:
827 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000828 default:
829 return NULL;
830 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000831}
832
833/**
834 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
835 * @node: Pointer to SMMU ACPI IORT node
836 *
837 * Returns: 0 on success, <0 failure
838 */
839static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
840{
841 struct fwnode_handle *fwnode;
842 struct platform_device *pdev;
843 struct resource *r;
844 enum dev_dma_attr attr;
845 int ret, count;
846 const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
847
848 if (!ops)
849 return -ENODEV;
850
851 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
852 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +0300853 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000854
855 count = ops->iommu_count_resources(node);
856
857 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
858 if (!r) {
859 ret = -ENOMEM;
860 goto dev_put;
861 }
862
863 ops->iommu_init_resources(r, node);
864
865 ret = platform_device_add_resources(pdev, r, count);
866 /*
867 * Resources are duplicated in platform_device_add_resources,
868 * free their allocated memory
869 */
870 kfree(r);
871
872 if (ret)
873 goto dev_put;
874
875 /*
876 * Add a copy of IORT node pointer to platform_data to
877 * be used to retrieve IORT data information.
878 */
879 ret = platform_device_add_data(pdev, &node, sizeof(node));
880 if (ret)
881 goto dev_put;
882
883 /*
884 * We expect the dma masks to be equivalent for
885 * all SMMUs set-ups
886 */
887 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
888
889 fwnode = iort_get_fwnode(node);
890
891 if (!fwnode) {
892 ret = -ENODEV;
893 goto dev_put;
894 }
895
896 pdev->dev.fwnode = fwnode;
897
898 attr = ops->iommu_is_coherent(node) ?
899 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
900
901 /* Configure DMA for the page table walker */
902 acpi_dma_configure(&pdev->dev, attr);
903
904 ret = platform_device_add(pdev);
905 if (ret)
906 goto dma_deconfigure;
907
908 return 0;
909
910dma_deconfigure:
911 acpi_dma_deconfigure(&pdev->dev);
912dev_put:
913 platform_device_put(pdev);
914
915 return ret;
916}
917
918static void __init iort_init_platform_devices(void)
919{
920 struct acpi_iort_node *iort_node, *iort_end;
921 struct acpi_table_iort *iort;
922 struct fwnode_handle *fwnode;
923 int i, ret;
924
925 /*
926 * iort_table and iort both point to the start of IORT table, but
927 * have different struct types
928 */
929 iort = (struct acpi_table_iort *)iort_table;
930
931 /* Get the first IORT node */
932 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
933 iort->node_offset);
934 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
935 iort_table->length);
936
937 for (i = 0; i < iort->node_count; i++) {
938 if (iort_node >= iort_end) {
939 pr_err("iort node pointer overflows, bad table\n");
940 return;
941 }
942
943 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
944 (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
945
946 fwnode = acpi_alloc_fwnode_static();
947 if (!fwnode)
948 return;
949
950 iort_set_fwnode(iort_node, fwnode);
951
952 ret = iort_add_smmu_platform_device(iort_node);
953 if (ret) {
954 iort_delete_fwnode(iort_node);
955 acpi_free_fwnode_static(fwnode);
956 return;
957 }
958 }
959
960 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
961 iort_node->length);
962 }
963}
964
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200965void __init acpi_iort_init(void)
966{
967 acpi_status status;
968
969 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000970 if (ACPI_FAILURE(status)) {
971 if (status != AE_NOT_FOUND) {
972 const char *msg = acpi_format_exception(status);
973
974 pr_err("Failed to get table, %s\n", msg);
975 }
976
977 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200978 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000979
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000980 iort_init_platform_devices();
981
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000982 acpi_probe_device_table(iort);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200983}