blob: e323ece0314deafacf30e7091fd51f7235d78829 [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)))
228 return iort_node;
229
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;
256 acpi_status status;
257
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
263 if (!adev) {
264 status = AE_NOT_FOUND;
265 goto out;
266 }
267
268 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
269 if (ACPI_FAILURE(status)) {
270 dev_warn(dev, "Can't get device full path name\n");
271 goto out;
272 }
273
274 ncomp = (struct acpi_iort_named_component *)node->node_data;
275 status = !strcmp(ncomp->device_name, buf.pointer) ?
276 AE_OK : AE_NOT_FOUND;
277 acpi_os_free(buf.pointer);
278 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
279 struct acpi_iort_root_complex *pci_rc;
280 struct pci_bus *bus;
281
282 bus = to_pci_bus(dev);
283 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
284
285 /*
286 * It is assumed that PCI segment numbers maps one-to-one
287 * with root complexes. Each segment number can represent only
288 * one root complex.
289 */
290 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
291 AE_OK : AE_NOT_FOUND;
292 } else {
293 status = AE_NOT_FOUND;
294 }
295out:
296 return status;
297}
298
299static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
300 u32 *rid_out)
301{
302 /* Single mapping does not care for input id */
303 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
304 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
305 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
306 *rid_out = map->output_base;
307 return 0;
308 }
309
310 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
311 map, type);
312 return -ENXIO;
313 }
314
315 if (rid_in < map->input_base ||
316 (rid_in >= map->input_base + map->id_count))
317 return -ENXIO;
318
319 *rid_out = map->output_base + (rid_in - map->input_base);
320 return 0;
321}
322
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000323static
324struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
325 u32 *id_out, u8 type_mask,
326 int index)
327{
328 struct acpi_iort_node *parent;
329 struct acpi_iort_id_mapping *map;
330
331 if (!node->mapping_offset || !node->mapping_count ||
332 index >= node->mapping_count)
333 return NULL;
334
335 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000336 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000337
338 /* Firmware bug! */
339 if (!map->output_reference) {
340 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
341 node, node->type);
342 return NULL;
343 }
344
345 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
346 map->output_reference);
347
348 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
349 return NULL;
350
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000351 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000352 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
353 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
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
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200362static struct acpi_iort_node *iort_node_map_rid(struct acpi_iort_node *node,
363 u32 rid_in, u32 *rid_out,
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000364 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200365{
366 u32 rid = rid_in;
367
368 /* Parse the ID mapping tree to find specified node type */
369 while (node) {
370 struct acpi_iort_id_mapping *map;
371 int i;
372
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000373 if (IORT_TYPE_MASK(node->type) & type_mask) {
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200374 if (rid_out)
375 *rid_out = rid;
376 return node;
377 }
378
379 if (!node->mapping_offset || !node->mapping_count)
380 goto fail_map;
381
382 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
383 node->mapping_offset);
384
385 /* Firmware bug! */
386 if (!map->output_reference) {
387 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
388 node, node->type);
389 goto fail_map;
390 }
391
392 /* Do the RID translation */
393 for (i = 0; i < node->mapping_count; i++, map++) {
394 if (!iort_id_map(map, node->type, rid, &rid))
395 break;
396 }
397
398 if (i == node->mapping_count)
399 goto fail_map;
400
401 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
402 map->output_reference);
403 }
404
405fail_map:
406 /* Map input RID to output RID unchanged on mapping failure*/
407 if (rid_out)
408 *rid_out = rid_in;
409
410 return NULL;
411}
412
413static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
414{
415 struct pci_bus *pbus;
416
417 if (!dev_is_pci(dev))
418 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
419 iort_match_node_callback, dev);
420
421 /* Find a PCI root bus */
422 pbus = to_pci_dev(dev)->bus;
423 while (!pci_is_root_bus(pbus))
424 pbus = pbus->parent;
425
426 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
427 iort_match_node_callback, &pbus->dev);
428}
429
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200430/**
431 * iort_msi_map_rid() - Map a MSI requester ID for a device
432 * @dev: The device for which the mapping is to be done.
433 * @req_id: The device requester ID.
434 *
435 * Returns: mapped MSI RID on success, input requester ID otherwise
436 */
437u32 iort_msi_map_rid(struct device *dev, u32 req_id)
438{
439 struct acpi_iort_node *node;
440 u32 dev_id;
441
442 node = iort_find_dev_node(dev);
443 if (!node)
444 return req_id;
445
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000446 iort_node_map_rid(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200447 return dev_id;
448}
449
450/**
451 * iort_dev_find_its_id() - Find the ITS identifier for a device
452 * @dev: The device.
453 * @idx: Index of the ITS identifier list.
454 * @its_id: ITS identifier.
455 *
456 * Returns: 0 on success, appropriate error value otherwise
457 */
458static int iort_dev_find_its_id(struct device *dev, u32 req_id,
459 unsigned int idx, int *its_id)
460{
461 struct acpi_iort_its_group *its;
462 struct acpi_iort_node *node;
463
464 node = iort_find_dev_node(dev);
465 if (!node)
466 return -ENXIO;
467
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000468 node = iort_node_map_rid(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200469 if (!node)
470 return -ENXIO;
471
472 /* Move to ITS specific data */
473 its = (struct acpi_iort_its_group *)node->node_data;
474 if (idx > its->its_count) {
475 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
476 idx, its->its_count);
477 return -ENXIO;
478 }
479
480 *its_id = its->identifiers[idx];
481 return 0;
482}
483
484/**
485 * iort_get_device_domain() - Find MSI domain related to a device
486 * @dev: The device.
487 * @req_id: Requester ID for the device.
488 *
489 * Returns: the MSI domain for this device, NULL otherwise
490 */
491struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
492{
493 struct fwnode_handle *handle;
494 int its_id;
495
496 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
497 return NULL;
498
499 handle = iort_find_domain_token(its_id);
500 if (!handle)
501 return NULL;
502
503 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
504}
505
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000506static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
507{
508 u32 *rid = data;
509
510 *rid = alias;
511 return 0;
512}
513
514static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
515 struct fwnode_handle *fwnode,
516 const struct iommu_ops *ops)
517{
518 int ret = iommu_fwspec_init(dev, fwnode, ops);
519
520 if (!ret)
521 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
522
523 return ret;
524}
525
Lorenzo Pieralisi1d9029d2017-04-10 16:50:59 +0530526static inline bool iort_iommu_driver_enabled(u8 type)
527{
528 switch (type) {
529 case ACPI_IORT_NODE_SMMU_V3:
530 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
531 case ACPI_IORT_NODE_SMMU:
532 return IS_BUILTIN(CONFIG_ARM_SMMU);
533 default:
534 pr_warn("IORT node type %u does not describe an SMMU\n", type);
535 return false;
536 }
537}
538
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000539static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
540 struct acpi_iort_node *node,
541 u32 streamid)
542{
543 const struct iommu_ops *ops = NULL;
544 int ret = -ENODEV;
545 struct fwnode_handle *iort_fwnode;
Sricharan R5a1bb632017-04-10 16:51:03 +0530546 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
547
548 /*
549 * If we already translated the fwspec there
550 * is nothing left to do, return the iommu_ops.
551 */
552 if (fwspec && fwspec->ops)
553 return fwspec->ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000554
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);
Sricharan R5a1bb632017-04-10 16:51:03 +0530561 /*
562 * If the ops look-up fails, this means that either
563 * the SMMU drivers have not been probed yet or that
564 * the SMMU drivers are not built in the kernel;
565 * Depending on whether the SMMU drivers are built-in
566 * in the kernel or not, defer the IOMMU configuration
567 * or just abort it.
568 */
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000569 if (!ops)
Sricharan R5a1bb632017-04-10 16:51:03 +0530570 return iort_iommu_driver_enabled(node->type) ?
571 ERR_PTR(-EPROBE_DEFER) : NULL;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000572
573 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
574 }
575
576 return ret ? NULL : ops;
577}
578
579/**
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000580 * iort_set_dma_mask - Set-up dma mask for a device.
581 *
582 * @dev: device to configure
583 */
584void iort_set_dma_mask(struct device *dev)
585{
586 /*
587 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
588 * setup the correct supported mask.
589 */
590 if (!dev->coherent_dma_mask)
591 dev->coherent_dma_mask = DMA_BIT_MASK(32);
592
593 /*
594 * Set it to coherent_dma_mask by default if the architecture
595 * code has not set it.
596 */
597 if (!dev->dma_mask)
598 dev->dma_mask = &dev->coherent_dma_mask;
599}
600
601/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000602 * iort_iommu_configure - Set-up IOMMU configuration for a device.
603 *
604 * @dev: device to configure
605 *
606 * Returns: iommu_ops pointer on configuration success
607 * NULL on configuration failure
608 */
609const struct iommu_ops *iort_iommu_configure(struct device *dev)
610{
611 struct acpi_iort_node *node, *parent;
612 const struct iommu_ops *ops = NULL;
613 u32 streamid = 0;
614
615 if (dev_is_pci(dev)) {
616 struct pci_bus *bus = to_pci_dev(dev)->bus;
617 u32 rid;
618
619 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
620 &rid);
621
622 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
623 iort_match_node_callback, &bus->dev);
624 if (!node)
625 return NULL;
626
627 parent = iort_node_map_rid(node, rid, &streamid,
628 IORT_IOMMU_TYPE);
629
630 ops = iort_iommu_xlate(dev, parent, streamid);
631
632 } else {
633 int i = 0;
634
635 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
636 iort_match_node_callback, dev);
637 if (!node)
638 return NULL;
639
640 parent = iort_node_get_id(node, &streamid,
641 IORT_IOMMU_TYPE, i++);
642
643 while (parent) {
644 ops = iort_iommu_xlate(dev, parent, streamid);
Sricharan R5a1bb632017-04-10 16:51:03 +0530645 if (IS_ERR_OR_NULL(ops))
646 return ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000647
648 parent = iort_node_get_id(node, &streamid,
649 IORT_IOMMU_TYPE, i++);
650 }
651 }
652
Sricharan R5a1bb632017-04-10 16:51:03 +0530653 /*
654 * If we have reason to believe the IOMMU driver missed the initial
655 * add_device callback for dev, replay it to get things in order.
656 */
657 if (!IS_ERR_OR_NULL(ops) && ops->add_device &&
658 dev->bus && !dev->iommu_group) {
659 int err = ops->add_device(dev);
660
661 if (err)
662 ops = ERR_PTR(err);
663 }
664
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000665 return ops;
666}
667
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000668static void __init acpi_iort_register_irq(int hwirq, const char *name,
669 int trigger,
670 struct resource *res)
671{
672 int irq = acpi_register_gsi(NULL, hwirq, trigger,
673 ACPI_ACTIVE_HIGH);
674
675 if (irq <= 0) {
676 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
677 name);
678 return;
679 }
680
681 res->start = irq;
682 res->end = irq;
683 res->flags = IORESOURCE_IRQ;
684 res->name = name;
685}
686
687static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
688{
689 struct acpi_iort_smmu_v3 *smmu;
690 /* Always present mem resource */
691 int num_res = 1;
692
693 /* Retrieve SMMUv3 specific data */
694 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
695
696 if (smmu->event_gsiv)
697 num_res++;
698
699 if (smmu->pri_gsiv)
700 num_res++;
701
702 if (smmu->gerr_gsiv)
703 num_res++;
704
705 if (smmu->sync_gsiv)
706 num_res++;
707
708 return num_res;
709}
710
711static void __init arm_smmu_v3_init_resources(struct resource *res,
712 struct acpi_iort_node *node)
713{
714 struct acpi_iort_smmu_v3 *smmu;
715 int num_res = 0;
716
717 /* Retrieve SMMUv3 specific data */
718 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
719
720 res[num_res].start = smmu->base_address;
721 res[num_res].end = smmu->base_address + SZ_128K - 1;
722 res[num_res].flags = IORESOURCE_MEM;
723
724 num_res++;
725
726 if (smmu->event_gsiv)
727 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
728 ACPI_EDGE_SENSITIVE,
729 &res[num_res++]);
730
731 if (smmu->pri_gsiv)
732 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
733 ACPI_EDGE_SENSITIVE,
734 &res[num_res++]);
735
736 if (smmu->gerr_gsiv)
737 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
738 ACPI_EDGE_SENSITIVE,
739 &res[num_res++]);
740
741 if (smmu->sync_gsiv)
742 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
743 ACPI_EDGE_SENSITIVE,
744 &res[num_res++]);
745}
746
747static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
748{
749 struct acpi_iort_smmu_v3 *smmu;
750
751 /* Retrieve SMMUv3 specific data */
752 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
753
754 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
755}
756
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000757static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
758{
759 struct acpi_iort_smmu *smmu;
760
761 /* Retrieve SMMU specific data */
762 smmu = (struct acpi_iort_smmu *)node->node_data;
763
764 /*
765 * Only consider the global fault interrupt and ignore the
766 * configuration access interrupt.
767 *
768 * MMIO address and global fault interrupt resources are always
769 * present so add them to the context interrupt count as a static
770 * value.
771 */
772 return smmu->context_interrupt_count + 2;
773}
774
775static void __init arm_smmu_init_resources(struct resource *res,
776 struct acpi_iort_node *node)
777{
778 struct acpi_iort_smmu *smmu;
779 int i, hw_irq, trigger, num_res = 0;
780 u64 *ctx_irq, *glb_irq;
781
782 /* Retrieve SMMU specific data */
783 smmu = (struct acpi_iort_smmu *)node->node_data;
784
785 res[num_res].start = smmu->base_address;
786 res[num_res].end = smmu->base_address + smmu->span - 1;
787 res[num_res].flags = IORESOURCE_MEM;
788 num_res++;
789
790 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
791 /* Global IRQs */
792 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
793 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
794
795 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
796 &res[num_res++]);
797
798 /* Context IRQs */
799 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
800 for (i = 0; i < smmu->context_interrupt_count; i++) {
801 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
802 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
803
804 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
805 &res[num_res++]);
806 }
807}
808
809static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
810{
811 struct acpi_iort_smmu *smmu;
812
813 /* Retrieve SMMU specific data */
814 smmu = (struct acpi_iort_smmu *)node->node_data;
815
816 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
817}
818
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000819struct iort_iommu_config {
820 const char *name;
821 int (*iommu_init)(struct acpi_iort_node *node);
822 bool (*iommu_is_coherent)(struct acpi_iort_node *node);
823 int (*iommu_count_resources)(struct acpi_iort_node *node);
824 void (*iommu_init_resources)(struct resource *res,
825 struct acpi_iort_node *node);
826};
827
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000828static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
829 .name = "arm-smmu-v3",
830 .iommu_is_coherent = arm_smmu_v3_is_coherent,
831 .iommu_count_resources = arm_smmu_v3_count_resources,
832 .iommu_init_resources = arm_smmu_v3_init_resources
833};
834
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000835static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
836 .name = "arm-smmu",
837 .iommu_is_coherent = arm_smmu_is_coherent,
838 .iommu_count_resources = arm_smmu_count_resources,
839 .iommu_init_resources = arm_smmu_init_resources
840};
841
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000842static __init
843const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
844{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000845 switch (node->type) {
846 case ACPI_IORT_NODE_SMMU_V3:
847 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000848 case ACPI_IORT_NODE_SMMU:
849 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000850 default:
851 return NULL;
852 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000853}
854
855/**
856 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
857 * @node: Pointer to SMMU ACPI IORT node
858 *
859 * Returns: 0 on success, <0 failure
860 */
861static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
862{
863 struct fwnode_handle *fwnode;
864 struct platform_device *pdev;
865 struct resource *r;
866 enum dev_dma_attr attr;
867 int ret, count;
868 const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
869
870 if (!ops)
871 return -ENODEV;
872
873 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
874 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +0300875 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000876
877 count = ops->iommu_count_resources(node);
878
879 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
880 if (!r) {
881 ret = -ENOMEM;
882 goto dev_put;
883 }
884
885 ops->iommu_init_resources(r, node);
886
887 ret = platform_device_add_resources(pdev, r, count);
888 /*
889 * Resources are duplicated in platform_device_add_resources,
890 * free their allocated memory
891 */
892 kfree(r);
893
894 if (ret)
895 goto dev_put;
896
897 /*
898 * Add a copy of IORT node pointer to platform_data to
899 * be used to retrieve IORT data information.
900 */
901 ret = platform_device_add_data(pdev, &node, sizeof(node));
902 if (ret)
903 goto dev_put;
904
905 /*
906 * We expect the dma masks to be equivalent for
907 * all SMMUs set-ups
908 */
909 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
910
911 fwnode = iort_get_fwnode(node);
912
913 if (!fwnode) {
914 ret = -ENODEV;
915 goto dev_put;
916 }
917
918 pdev->dev.fwnode = fwnode;
919
920 attr = ops->iommu_is_coherent(node) ?
921 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
922
923 /* Configure DMA for the page table walker */
924 acpi_dma_configure(&pdev->dev, attr);
925
926 ret = platform_device_add(pdev);
927 if (ret)
928 goto dma_deconfigure;
929
930 return 0;
931
932dma_deconfigure:
933 acpi_dma_deconfigure(&pdev->dev);
934dev_put:
935 platform_device_put(pdev);
936
937 return ret;
938}
939
940static void __init iort_init_platform_devices(void)
941{
942 struct acpi_iort_node *iort_node, *iort_end;
943 struct acpi_table_iort *iort;
944 struct fwnode_handle *fwnode;
945 int i, ret;
946
947 /*
948 * iort_table and iort both point to the start of IORT table, but
949 * have different struct types
950 */
951 iort = (struct acpi_table_iort *)iort_table;
952
953 /* Get the first IORT node */
954 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
955 iort->node_offset);
956 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
957 iort_table->length);
958
959 for (i = 0; i < iort->node_count; i++) {
960 if (iort_node >= iort_end) {
961 pr_err("iort node pointer overflows, bad table\n");
962 return;
963 }
964
965 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
966 (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
967
968 fwnode = acpi_alloc_fwnode_static();
969 if (!fwnode)
970 return;
971
972 iort_set_fwnode(iort_node, fwnode);
973
974 ret = iort_add_smmu_platform_device(iort_node);
975 if (ret) {
976 iort_delete_fwnode(iort_node);
977 acpi_free_fwnode_static(fwnode);
978 return;
979 }
980 }
981
982 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
983 iort_node->length);
984 }
985}
986
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200987void __init acpi_iort_init(void)
988{
989 acpi_status status;
990
991 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000992 if (ACPI_FAILURE(status)) {
993 if (status != AE_NOT_FOUND) {
994 const char *msg = acpi_format_exception(status);
995
996 pr_err("Failed to get table, %s\n", msg);
997 }
998
999 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001000 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001001
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001002 iort_init_platform_devices();
1003
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001004 acpi_probe_device_table(iort);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001005}