]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/acpi/arm64/iort.c
Merge tag 'befs-v4.12-rc1' of git://github.com/luisbg/linux-befs
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / arm64 / iort.c
CommitLineData
88ef16d8
TN
1/*
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>
846f0e9e 22#include <linux/iommu.h>
88ef16d8 23#include <linux/kernel.h>
7936df92 24#include <linux/list.h>
88ef16d8 25#include <linux/pci.h>
846f0e9e 26#include <linux/platform_device.h>
7936df92 27#include <linux/slab.h>
88ef16d8 28
ea50b524
LP
29#define IORT_TYPE_MASK(type) (1 << (type))
30#define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP)
643b8e4d
LP
31#define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \
32 (1 << ACPI_IORT_NODE_SMMU_V3))
ea50b524 33
4bf2efd2
TN
34struct iort_its_msi_chip {
35 struct list_head list;
36 struct fwnode_handle *fw_node;
37 u32 translation_id;
38};
39
7936df92
LP
40struct 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
88ef16d8
TN
124typedef 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
4bf2efd2
TN
133/**
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
88ef16d8
TN
203static 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)))
d89cf2e4 228 return iort_node;
88ef16d8
TN
229
230 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
231 iort_node->length);
232 }
233
234 return NULL;
235}
236
bdca0c07
LP
237static 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
88ef16d8
TN
252static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
253 void *context)
254{
255 struct device *dev = context;
c92bdfe8 256 acpi_status status = AE_NOT_FOUND;
88ef16d8
TN
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
c92bdfe8 263 if (!adev)
88ef16d8 264 goto out;
88ef16d8
TN
265
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;
88ef16d8
TN
290 }
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
618f535a
LP
319static
320struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
8ca4f1d3 321 u32 *id_out, int index)
618f535a
LP
322{
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,
030abd8a 331 node->mapping_offset + index * sizeof(*map));
618f535a
LP
332
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
030abd8a 343 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
618f535a
LP
344 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
345 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
030abd8a 346 *id_out = map->output_base;
618f535a
LP
347 return parent;
348 }
349 }
350
351 return NULL;
352}
353
697f6093
HG
354static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
355 u32 id_in, u32 *id_out,
356 u8 type_mask)
88ef16d8 357{
697f6093 358 u32 id = id_in;
88ef16d8
TN
359
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
ea50b524 365 if (IORT_TYPE_MASK(node->type) & type_mask) {
697f6093
HG
366 if (id_out)
367 *id_out = id;
88ef16d8
TN
368 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
697f6093 384 /* Do the ID translation */
88ef16d8 385 for (i = 0; i < node->mapping_count; i++, map++) {
697f6093 386 if (!iort_id_map(map, node->type, id, &id))
88ef16d8
TN
387 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:
697f6093
HG
398 /* Map input ID to output ID unchanged on mapping failure */
399 if (id_out)
400 *id_out = id_in;
88ef16d8
TN
401
402 return NULL;
403}
404
8ca4f1d3
HG
405static
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
88ef16d8
TN
433static 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
4bf2efd2
TN
450/**
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
697f6093 466 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
4bf2efd2
TN
467 return dev_id;
468}
469
ae7c1838
HG
470/**
471 * iort_pmsi_get_dev_id() - Get the device id for a device
472 * @dev: The device for which the mapping is to be done.
473 * @dev_id: The device ID found.
474 *
475 * Returns: 0 for successful find a dev id, -ENODEV on error
476 */
477int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
478{
479 int i;
480 struct acpi_iort_node *node;
481
482 node = iort_find_dev_node(dev);
483 if (!node)
484 return -ENODEV;
485
486 for (i = 0; i < node->mapping_count; i++) {
487 if (iort_node_map_platform_id(node, dev_id, IORT_MSI_TYPE, i))
488 return 0;
489 }
490
491 return -ENODEV;
492}
493
4bf2efd2
TN
494/**
495 * iort_dev_find_its_id() - Find the ITS identifier for a device
496 * @dev: The device.
6cb6bf56 497 * @req_id: Device's requester ID
4bf2efd2
TN
498 * @idx: Index of the ITS identifier list.
499 * @its_id: ITS identifier.
500 *
501 * Returns: 0 on success, appropriate error value otherwise
502 */
503static int iort_dev_find_its_id(struct device *dev, u32 req_id,
504 unsigned int idx, int *its_id)
505{
506 struct acpi_iort_its_group *its;
507 struct acpi_iort_node *node;
508
509 node = iort_find_dev_node(dev);
510 if (!node)
511 return -ENXIO;
512
697f6093 513 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
4bf2efd2
TN
514 if (!node)
515 return -ENXIO;
516
517 /* Move to ITS specific data */
518 its = (struct acpi_iort_its_group *)node->node_data;
519 if (idx > its->its_count) {
520 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
521 idx, its->its_count);
522 return -ENXIO;
523 }
524
525 *its_id = its->identifiers[idx];
526 return 0;
527}
528
529/**
530 * iort_get_device_domain() - Find MSI domain related to a device
531 * @dev: The device.
532 * @req_id: Requester ID for the device.
533 *
534 * Returns: the MSI domain for this device, NULL otherwise
535 */
536struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
537{
538 struct fwnode_handle *handle;
539 int its_id;
540
541 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
542 return NULL;
543
544 handle = iort_find_domain_token(its_id);
545 if (!handle)
546 return NULL;
547
548 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
549}
550
d4f54a18
HG
551/**
552 * iort_get_platform_device_domain() - Find MSI domain related to a
553 * platform device
554 * @dev: the dev pointer associated with the platform device
555 *
556 * Returns: the MSI domain for this device, NULL otherwise
557 */
558static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
559{
560 struct acpi_iort_node *node, *msi_parent;
561 struct fwnode_handle *iort_fwnode;
562 struct acpi_iort_its_group *its;
563 int i;
564
565 /* find its associated iort node */
566 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
567 iort_match_node_callback, dev);
568 if (!node)
569 return NULL;
570
571 /* then find its msi parent node */
572 for (i = 0; i < node->mapping_count; i++) {
573 msi_parent = iort_node_map_platform_id(node, NULL,
574 IORT_MSI_TYPE, i);
575 if (msi_parent)
576 break;
577 }
578
579 if (!msi_parent)
580 return NULL;
581
582 /* Move to ITS specific data */
583 its = (struct acpi_iort_its_group *)msi_parent->node_data;
584
585 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
586 if (!iort_fwnode)
587 return NULL;
588
589 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
590}
591
592void acpi_configure_pmsi_domain(struct device *dev)
593{
594 struct irq_domain *msi_domain;
595
596 msi_domain = iort_get_platform_device_domain(dev);
597 if (msi_domain)
598 dev_set_msi_domain(dev, msi_domain);
599}
600
643b8e4d
LP
601static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
602{
603 u32 *rid = data;
604
605 *rid = alias;
606 return 0;
607}
608
609static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
610 struct fwnode_handle *fwnode,
611 const struct iommu_ops *ops)
612{
613 int ret = iommu_fwspec_init(dev, fwnode, ops);
614
615 if (!ret)
616 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
617
618 return ret;
619}
620
621static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
622 struct acpi_iort_node *node,
623 u32 streamid)
624{
625 const struct iommu_ops *ops = NULL;
626 int ret = -ENODEV;
627 struct fwnode_handle *iort_fwnode;
628
629 if (node) {
630 iort_fwnode = iort_get_fwnode(node);
631 if (!iort_fwnode)
632 return NULL;
633
534766df 634 ops = iommu_ops_from_fwnode(iort_fwnode);
643b8e4d
LP
635 if (!ops)
636 return NULL;
637
638 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
639 }
640
641 return ret ? NULL : ops;
642}
643
18b709be
LP
644/**
645 * iort_set_dma_mask - Set-up dma mask for a device.
646 *
647 * @dev: device to configure
648 */
649void iort_set_dma_mask(struct device *dev)
650{
651 /*
652 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
653 * setup the correct supported mask.
654 */
655 if (!dev->coherent_dma_mask)
656 dev->coherent_dma_mask = DMA_BIT_MASK(32);
657
658 /*
659 * Set it to coherent_dma_mask by default if the architecture
660 * code has not set it.
661 */
662 if (!dev->dma_mask)
663 dev->dma_mask = &dev->coherent_dma_mask;
664}
665
643b8e4d
LP
666/**
667 * iort_iommu_configure - Set-up IOMMU configuration for a device.
668 *
669 * @dev: device to configure
670 *
671 * Returns: iommu_ops pointer on configuration success
672 * NULL on configuration failure
673 */
674const struct iommu_ops *iort_iommu_configure(struct device *dev)
675{
676 struct acpi_iort_node *node, *parent;
677 const struct iommu_ops *ops = NULL;
678 u32 streamid = 0;
679
680 if (dev_is_pci(dev)) {
681 struct pci_bus *bus = to_pci_dev(dev)->bus;
682 u32 rid;
683
684 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
685 &rid);
686
687 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
688 iort_match_node_callback, &bus->dev);
689 if (!node)
690 return NULL;
691
697f6093
HG
692 parent = iort_node_map_id(node, rid, &streamid,
693 IORT_IOMMU_TYPE);
643b8e4d
LP
694
695 ops = iort_iommu_xlate(dev, parent, streamid);
696
697 } else {
698 int i = 0;
699
700 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
701 iort_match_node_callback, dev);
702 if (!node)
703 return NULL;
704
8ca4f1d3
HG
705 parent = iort_node_map_platform_id(node, &streamid,
706 IORT_IOMMU_TYPE, i++);
643b8e4d
LP
707
708 while (parent) {
709 ops = iort_iommu_xlate(dev, parent, streamid);
710
8ca4f1d3
HG
711 parent = iort_node_map_platform_id(node, &streamid,
712 IORT_IOMMU_TYPE,
713 i++);
643b8e4d
LP
714 }
715 }
716
717 return ops;
718}
719
e4dadfa8
LP
720static void __init acpi_iort_register_irq(int hwirq, const char *name,
721 int trigger,
722 struct resource *res)
723{
724 int irq = acpi_register_gsi(NULL, hwirq, trigger,
725 ACPI_ACTIVE_HIGH);
726
727 if (irq <= 0) {
728 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
729 name);
730 return;
731 }
732
733 res->start = irq;
734 res->end = irq;
735 res->flags = IORESOURCE_IRQ;
736 res->name = name;
737}
738
739static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
740{
741 struct acpi_iort_smmu_v3 *smmu;
742 /* Always present mem resource */
743 int num_res = 1;
744
745 /* Retrieve SMMUv3 specific data */
746 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
747
748 if (smmu->event_gsiv)
749 num_res++;
750
751 if (smmu->pri_gsiv)
752 num_res++;
753
754 if (smmu->gerr_gsiv)
755 num_res++;
756
757 if (smmu->sync_gsiv)
758 num_res++;
759
760 return num_res;
761}
762
763static void __init arm_smmu_v3_init_resources(struct resource *res,
764 struct acpi_iort_node *node)
765{
766 struct acpi_iort_smmu_v3 *smmu;
767 int num_res = 0;
768
769 /* Retrieve SMMUv3 specific data */
770 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
771
772 res[num_res].start = smmu->base_address;
773 res[num_res].end = smmu->base_address + SZ_128K - 1;
774 res[num_res].flags = IORESOURCE_MEM;
775
776 num_res++;
777
778 if (smmu->event_gsiv)
779 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
780 ACPI_EDGE_SENSITIVE,
781 &res[num_res++]);
782
783 if (smmu->pri_gsiv)
784 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
785 ACPI_EDGE_SENSITIVE,
786 &res[num_res++]);
787
788 if (smmu->gerr_gsiv)
789 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
790 ACPI_EDGE_SENSITIVE,
791 &res[num_res++]);
792
793 if (smmu->sync_gsiv)
794 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
795 ACPI_EDGE_SENSITIVE,
796 &res[num_res++]);
797}
798
799static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
800{
801 struct acpi_iort_smmu_v3 *smmu;
802
803 /* Retrieve SMMUv3 specific data */
804 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
805
806 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
807}
808
d6fcd3b1
LP
809static int __init arm_smmu_count_resources(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 /*
817 * Only consider the global fault interrupt and ignore the
818 * configuration access interrupt.
819 *
820 * MMIO address and global fault interrupt resources are always
821 * present so add them to the context interrupt count as a static
822 * value.
823 */
824 return smmu->context_interrupt_count + 2;
825}
826
827static void __init arm_smmu_init_resources(struct resource *res,
828 struct acpi_iort_node *node)
829{
830 struct acpi_iort_smmu *smmu;
831 int i, hw_irq, trigger, num_res = 0;
832 u64 *ctx_irq, *glb_irq;
833
834 /* Retrieve SMMU specific data */
835 smmu = (struct acpi_iort_smmu *)node->node_data;
836
837 res[num_res].start = smmu->base_address;
838 res[num_res].end = smmu->base_address + smmu->span - 1;
839 res[num_res].flags = IORESOURCE_MEM;
840 num_res++;
841
842 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
843 /* Global IRQs */
844 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
845 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
846
847 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
848 &res[num_res++]);
849
850 /* Context IRQs */
851 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
852 for (i = 0; i < smmu->context_interrupt_count; i++) {
853 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
854 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
855
856 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
857 &res[num_res++]);
858 }
859}
860
861static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
862{
863 struct acpi_iort_smmu *smmu;
864
865 /* Retrieve SMMU specific data */
866 smmu = (struct acpi_iort_smmu *)node->node_data;
867
868 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
869}
870
846f0e9e
LP
871struct iort_iommu_config {
872 const char *name;
873 int (*iommu_init)(struct acpi_iort_node *node);
874 bool (*iommu_is_coherent)(struct acpi_iort_node *node);
875 int (*iommu_count_resources)(struct acpi_iort_node *node);
876 void (*iommu_init_resources)(struct resource *res,
877 struct acpi_iort_node *node);
878};
879
e4dadfa8
LP
880static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
881 .name = "arm-smmu-v3",
882 .iommu_is_coherent = arm_smmu_v3_is_coherent,
883 .iommu_count_resources = arm_smmu_v3_count_resources,
884 .iommu_init_resources = arm_smmu_v3_init_resources
885};
886
d6fcd3b1
LP
887static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
888 .name = "arm-smmu",
889 .iommu_is_coherent = arm_smmu_is_coherent,
890 .iommu_count_resources = arm_smmu_count_resources,
891 .iommu_init_resources = arm_smmu_init_resources
892};
893
846f0e9e
LP
894static __init
895const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
896{
e4dadfa8
LP
897 switch (node->type) {
898 case ACPI_IORT_NODE_SMMU_V3:
899 return &iort_arm_smmu_v3_cfg;
d6fcd3b1
LP
900 case ACPI_IORT_NODE_SMMU:
901 return &iort_arm_smmu_cfg;
e4dadfa8
LP
902 default:
903 return NULL;
904 }
846f0e9e
LP
905}
906
907/**
908 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
909 * @node: Pointer to SMMU ACPI IORT node
910 *
911 * Returns: 0 on success, <0 failure
912 */
913static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
914{
915 struct fwnode_handle *fwnode;
916 struct platform_device *pdev;
917 struct resource *r;
918 enum dev_dma_attr attr;
919 int ret, count;
920 const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
921
922 if (!ops)
923 return -ENODEV;
924
925 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
926 if (!pdev)
5e5afa6c 927 return -ENOMEM;
846f0e9e
LP
928
929 count = ops->iommu_count_resources(node);
930
931 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
932 if (!r) {
933 ret = -ENOMEM;
934 goto dev_put;
935 }
936
937 ops->iommu_init_resources(r, node);
938
939 ret = platform_device_add_resources(pdev, r, count);
940 /*
941 * Resources are duplicated in platform_device_add_resources,
942 * free their allocated memory
943 */
944 kfree(r);
945
946 if (ret)
947 goto dev_put;
948
949 /*
950 * Add a copy of IORT node pointer to platform_data to
951 * be used to retrieve IORT data information.
952 */
953 ret = platform_device_add_data(pdev, &node, sizeof(node));
954 if (ret)
955 goto dev_put;
956
957 /*
958 * We expect the dma masks to be equivalent for
959 * all SMMUs set-ups
960 */
961 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
962
963 fwnode = iort_get_fwnode(node);
964
965 if (!fwnode) {
966 ret = -ENODEV;
967 goto dev_put;
968 }
969
970 pdev->dev.fwnode = fwnode;
971
972 attr = ops->iommu_is_coherent(node) ?
973 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
974
975 /* Configure DMA for the page table walker */
976 acpi_dma_configure(&pdev->dev, attr);
977
978 ret = platform_device_add(pdev);
979 if (ret)
980 goto dma_deconfigure;
981
982 return 0;
983
984dma_deconfigure:
985 acpi_dma_deconfigure(&pdev->dev);
986dev_put:
987 platform_device_put(pdev);
988
989 return ret;
990}
991
992static void __init iort_init_platform_devices(void)
993{
994 struct acpi_iort_node *iort_node, *iort_end;
995 struct acpi_table_iort *iort;
996 struct fwnode_handle *fwnode;
997 int i, ret;
998
999 /*
1000 * iort_table and iort both point to the start of IORT table, but
1001 * have different struct types
1002 */
1003 iort = (struct acpi_table_iort *)iort_table;
1004
1005 /* Get the first IORT node */
1006 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1007 iort->node_offset);
1008 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1009 iort_table->length);
1010
1011 for (i = 0; i < iort->node_count; i++) {
1012 if (iort_node >= iort_end) {
1013 pr_err("iort node pointer overflows, bad table\n");
1014 return;
1015 }
1016
1017 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
1018 (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
1019
1020 fwnode = acpi_alloc_fwnode_static();
1021 if (!fwnode)
1022 return;
1023
1024 iort_set_fwnode(iort_node, fwnode);
1025
1026 ret = iort_add_smmu_platform_device(iort_node);
1027 if (ret) {
1028 iort_delete_fwnode(iort_node);
1029 acpi_free_fwnode_static(fwnode);
1030 return;
1031 }
1032 }
1033
1034 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1035 iort_node->length);
1036 }
1037}
1038
88ef16d8
TN
1039void __init acpi_iort_init(void)
1040{
1041 acpi_status status;
1042
1043 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
34ceea27
LP
1044 if (ACPI_FAILURE(status)) {
1045 if (status != AE_NOT_FOUND) {
1046 const char *msg = acpi_format_exception(status);
1047
1048 pr_err("Failed to get table, %s\n", msg);
1049 }
1050
1051 return;
88ef16d8 1052 }
34ceea27 1053
846f0e9e
LP
1054 iort_init_platform_devices();
1055
34ceea27 1056 acpi_probe_device_table(iort);
88ef16d8 1057}