blob: 28d0088e71d5e99230d73d2c92d5d8cc057cb398 [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,
321 u32 *id_out, u8 type_mask,
322 int index)
323{
324 struct acpi_iort_node *parent;
325 struct acpi_iort_id_mapping *map;
326
327 if (!node->mapping_offset || !node->mapping_count ||
328 index >= node->mapping_count)
329 return NULL;
330
331 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000332 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000333
334 /* Firmware bug! */
335 if (!map->output_reference) {
336 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
337 node, node->type);
338 return NULL;
339 }
340
341 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
342 map->output_reference);
343
344 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
345 return NULL;
346
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000347 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000348 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
349 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000350 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000351 return parent;
352 }
353 }
354
355 return NULL;
356}
357
Hanjun Guo697f6092017-03-07 20:40:03 +0800358static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
359 u32 id_in, u32 *id_out,
360 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200361{
Hanjun Guo697f6092017-03-07 20:40:03 +0800362 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200363
364 /* Parse the ID mapping tree to find specified node type */
365 while (node) {
366 struct acpi_iort_id_mapping *map;
367 int i;
368
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000369 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800370 if (id_out)
371 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200372 return node;
373 }
374
375 if (!node->mapping_offset || !node->mapping_count)
376 goto fail_map;
377
378 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
379 node->mapping_offset);
380
381 /* Firmware bug! */
382 if (!map->output_reference) {
383 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
384 node, node->type);
385 goto fail_map;
386 }
387
Hanjun Guo697f6092017-03-07 20:40:03 +0800388 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200389 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800390 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200391 break;
392 }
393
394 if (i == node->mapping_count)
395 goto fail_map;
396
397 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
398 map->output_reference);
399 }
400
401fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800402 /* Map input ID to output ID unchanged on mapping failure */
403 if (id_out)
404 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200405
406 return NULL;
407}
408
409static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
410{
411 struct pci_bus *pbus;
412
413 if (!dev_is_pci(dev))
414 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
415 iort_match_node_callback, dev);
416
417 /* Find a PCI root bus */
418 pbus = to_pci_dev(dev)->bus;
419 while (!pci_is_root_bus(pbus))
420 pbus = pbus->parent;
421
422 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
423 iort_match_node_callback, &pbus->dev);
424}
425
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200426/**
427 * iort_msi_map_rid() - Map a MSI requester ID for a device
428 * @dev: The device for which the mapping is to be done.
429 * @req_id: The device requester ID.
430 *
431 * Returns: mapped MSI RID on success, input requester ID otherwise
432 */
433u32 iort_msi_map_rid(struct device *dev, u32 req_id)
434{
435 struct acpi_iort_node *node;
436 u32 dev_id;
437
438 node = iort_find_dev_node(dev);
439 if (!node)
440 return req_id;
441
Hanjun Guo697f6092017-03-07 20:40:03 +0800442 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200443 return dev_id;
444}
445
446/**
447 * iort_dev_find_its_id() - Find the ITS identifier for a device
448 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800449 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200450 * @idx: Index of the ITS identifier list.
451 * @its_id: ITS identifier.
452 *
453 * Returns: 0 on success, appropriate error value otherwise
454 */
455static int iort_dev_find_its_id(struct device *dev, u32 req_id,
456 unsigned int idx, int *its_id)
457{
458 struct acpi_iort_its_group *its;
459 struct acpi_iort_node *node;
460
461 node = iort_find_dev_node(dev);
462 if (!node)
463 return -ENXIO;
464
Hanjun Guo697f6092017-03-07 20:40:03 +0800465 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200466 if (!node)
467 return -ENXIO;
468
469 /* Move to ITS specific data */
470 its = (struct acpi_iort_its_group *)node->node_data;
471 if (idx > its->its_count) {
472 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
473 idx, its->its_count);
474 return -ENXIO;
475 }
476
477 *its_id = its->identifiers[idx];
478 return 0;
479}
480
481/**
482 * iort_get_device_domain() - Find MSI domain related to a device
483 * @dev: The device.
484 * @req_id: Requester ID for the device.
485 *
486 * Returns: the MSI domain for this device, NULL otherwise
487 */
488struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
489{
490 struct fwnode_handle *handle;
491 int its_id;
492
493 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
494 return NULL;
495
496 handle = iort_find_domain_token(its_id);
497 if (!handle)
498 return NULL;
499
500 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
501}
502
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000503static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data)
504{
505 u32 *rid = data;
506
507 *rid = alias;
508 return 0;
509}
510
511static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
512 struct fwnode_handle *fwnode,
513 const struct iommu_ops *ops)
514{
515 int ret = iommu_fwspec_init(dev, fwnode, ops);
516
517 if (!ret)
518 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
519
520 return ret;
521}
522
523static const struct iommu_ops *iort_iommu_xlate(struct device *dev,
524 struct acpi_iort_node *node,
525 u32 streamid)
526{
527 const struct iommu_ops *ops = NULL;
528 int ret = -ENODEV;
529 struct fwnode_handle *iort_fwnode;
530
531 if (node) {
532 iort_fwnode = iort_get_fwnode(node);
533 if (!iort_fwnode)
534 return NULL;
535
Joerg Roedel534766d2017-01-31 16:58:42 +0100536 ops = iommu_ops_from_fwnode(iort_fwnode);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000537 if (!ops)
538 return NULL;
539
540 ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
541 }
542
543 return ret ? NULL : ops;
544}
545
546/**
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000547 * iort_set_dma_mask - Set-up dma mask for a device.
548 *
549 * @dev: device to configure
550 */
551void iort_set_dma_mask(struct device *dev)
552{
553 /*
554 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
555 * setup the correct supported mask.
556 */
557 if (!dev->coherent_dma_mask)
558 dev->coherent_dma_mask = DMA_BIT_MASK(32);
559
560 /*
561 * Set it to coherent_dma_mask by default if the architecture
562 * code has not set it.
563 */
564 if (!dev->dma_mask)
565 dev->dma_mask = &dev->coherent_dma_mask;
566}
567
568/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000569 * iort_iommu_configure - Set-up IOMMU configuration for a device.
570 *
571 * @dev: device to configure
572 *
573 * Returns: iommu_ops pointer on configuration success
574 * NULL on configuration failure
575 */
576const struct iommu_ops *iort_iommu_configure(struct device *dev)
577{
578 struct acpi_iort_node *node, *parent;
579 const struct iommu_ops *ops = NULL;
580 u32 streamid = 0;
581
582 if (dev_is_pci(dev)) {
583 struct pci_bus *bus = to_pci_dev(dev)->bus;
584 u32 rid;
585
586 pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid,
587 &rid);
588
589 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
590 iort_match_node_callback, &bus->dev);
591 if (!node)
592 return NULL;
593
Hanjun Guo697f6092017-03-07 20:40:03 +0800594 parent = iort_node_map_id(node, rid, &streamid,
595 IORT_IOMMU_TYPE);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000596
597 ops = iort_iommu_xlate(dev, parent, streamid);
598
599 } else {
600 int i = 0;
601
602 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
603 iort_match_node_callback, dev);
604 if (!node)
605 return NULL;
606
607 parent = iort_node_get_id(node, &streamid,
608 IORT_IOMMU_TYPE, i++);
609
610 while (parent) {
611 ops = iort_iommu_xlate(dev, parent, streamid);
612
613 parent = iort_node_get_id(node, &streamid,
614 IORT_IOMMU_TYPE, i++);
615 }
616 }
617
618 return ops;
619}
620
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000621static void __init acpi_iort_register_irq(int hwirq, const char *name,
622 int trigger,
623 struct resource *res)
624{
625 int irq = acpi_register_gsi(NULL, hwirq, trigger,
626 ACPI_ACTIVE_HIGH);
627
628 if (irq <= 0) {
629 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
630 name);
631 return;
632 }
633
634 res->start = irq;
635 res->end = irq;
636 res->flags = IORESOURCE_IRQ;
637 res->name = name;
638}
639
640static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
641{
642 struct acpi_iort_smmu_v3 *smmu;
643 /* Always present mem resource */
644 int num_res = 1;
645
646 /* Retrieve SMMUv3 specific data */
647 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
648
649 if (smmu->event_gsiv)
650 num_res++;
651
652 if (smmu->pri_gsiv)
653 num_res++;
654
655 if (smmu->gerr_gsiv)
656 num_res++;
657
658 if (smmu->sync_gsiv)
659 num_res++;
660
661 return num_res;
662}
663
664static void __init arm_smmu_v3_init_resources(struct resource *res,
665 struct acpi_iort_node *node)
666{
667 struct acpi_iort_smmu_v3 *smmu;
668 int num_res = 0;
669
670 /* Retrieve SMMUv3 specific data */
671 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
672
673 res[num_res].start = smmu->base_address;
674 res[num_res].end = smmu->base_address + SZ_128K - 1;
675 res[num_res].flags = IORESOURCE_MEM;
676
677 num_res++;
678
679 if (smmu->event_gsiv)
680 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
681 ACPI_EDGE_SENSITIVE,
682 &res[num_res++]);
683
684 if (smmu->pri_gsiv)
685 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
686 ACPI_EDGE_SENSITIVE,
687 &res[num_res++]);
688
689 if (smmu->gerr_gsiv)
690 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
691 ACPI_EDGE_SENSITIVE,
692 &res[num_res++]);
693
694 if (smmu->sync_gsiv)
695 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
696 ACPI_EDGE_SENSITIVE,
697 &res[num_res++]);
698}
699
700static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
701{
702 struct acpi_iort_smmu_v3 *smmu;
703
704 /* Retrieve SMMUv3 specific data */
705 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
706
707 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
708}
709
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000710static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
711{
712 struct acpi_iort_smmu *smmu;
713
714 /* Retrieve SMMU specific data */
715 smmu = (struct acpi_iort_smmu *)node->node_data;
716
717 /*
718 * Only consider the global fault interrupt and ignore the
719 * configuration access interrupt.
720 *
721 * MMIO address and global fault interrupt resources are always
722 * present so add them to the context interrupt count as a static
723 * value.
724 */
725 return smmu->context_interrupt_count + 2;
726}
727
728static void __init arm_smmu_init_resources(struct resource *res,
729 struct acpi_iort_node *node)
730{
731 struct acpi_iort_smmu *smmu;
732 int i, hw_irq, trigger, num_res = 0;
733 u64 *ctx_irq, *glb_irq;
734
735 /* Retrieve SMMU specific data */
736 smmu = (struct acpi_iort_smmu *)node->node_data;
737
738 res[num_res].start = smmu->base_address;
739 res[num_res].end = smmu->base_address + smmu->span - 1;
740 res[num_res].flags = IORESOURCE_MEM;
741 num_res++;
742
743 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
744 /* Global IRQs */
745 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
746 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
747
748 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
749 &res[num_res++]);
750
751 /* Context IRQs */
752 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
753 for (i = 0; i < smmu->context_interrupt_count; i++) {
754 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
755 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
756
757 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
758 &res[num_res++]);
759 }
760}
761
762static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
763{
764 struct acpi_iort_smmu *smmu;
765
766 /* Retrieve SMMU specific data */
767 smmu = (struct acpi_iort_smmu *)node->node_data;
768
769 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
770}
771
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000772struct iort_iommu_config {
773 const char *name;
774 int (*iommu_init)(struct acpi_iort_node *node);
775 bool (*iommu_is_coherent)(struct acpi_iort_node *node);
776 int (*iommu_count_resources)(struct acpi_iort_node *node);
777 void (*iommu_init_resources)(struct resource *res,
778 struct acpi_iort_node *node);
779};
780
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000781static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = {
782 .name = "arm-smmu-v3",
783 .iommu_is_coherent = arm_smmu_v3_is_coherent,
784 .iommu_count_resources = arm_smmu_v3_count_resources,
785 .iommu_init_resources = arm_smmu_v3_init_resources
786};
787
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000788static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = {
789 .name = "arm-smmu",
790 .iommu_is_coherent = arm_smmu_is_coherent,
791 .iommu_count_resources = arm_smmu_count_resources,
792 .iommu_init_resources = arm_smmu_init_resources
793};
794
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000795static __init
796const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node)
797{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000798 switch (node->type) {
799 case ACPI_IORT_NODE_SMMU_V3:
800 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +0000801 case ACPI_IORT_NODE_SMMU:
802 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000803 default:
804 return NULL;
805 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000806}
807
808/**
809 * iort_add_smmu_platform_device() - Allocate a platform device for SMMU
810 * @node: Pointer to SMMU ACPI IORT node
811 *
812 * Returns: 0 on success, <0 failure
813 */
814static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node)
815{
816 struct fwnode_handle *fwnode;
817 struct platform_device *pdev;
818 struct resource *r;
819 enum dev_dma_attr attr;
820 int ret, count;
821 const struct iort_iommu_config *ops = iort_get_iommu_cfg(node);
822
823 if (!ops)
824 return -ENODEV;
825
826 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
827 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +0300828 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000829
830 count = ops->iommu_count_resources(node);
831
832 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
833 if (!r) {
834 ret = -ENOMEM;
835 goto dev_put;
836 }
837
838 ops->iommu_init_resources(r, node);
839
840 ret = platform_device_add_resources(pdev, r, count);
841 /*
842 * Resources are duplicated in platform_device_add_resources,
843 * free their allocated memory
844 */
845 kfree(r);
846
847 if (ret)
848 goto dev_put;
849
850 /*
851 * Add a copy of IORT node pointer to platform_data to
852 * be used to retrieve IORT data information.
853 */
854 ret = platform_device_add_data(pdev, &node, sizeof(node));
855 if (ret)
856 goto dev_put;
857
858 /*
859 * We expect the dma masks to be equivalent for
860 * all SMMUs set-ups
861 */
862 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
863
864 fwnode = iort_get_fwnode(node);
865
866 if (!fwnode) {
867 ret = -ENODEV;
868 goto dev_put;
869 }
870
871 pdev->dev.fwnode = fwnode;
872
873 attr = ops->iommu_is_coherent(node) ?
874 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
875
876 /* Configure DMA for the page table walker */
877 acpi_dma_configure(&pdev->dev, attr);
878
879 ret = platform_device_add(pdev);
880 if (ret)
881 goto dma_deconfigure;
882
883 return 0;
884
885dma_deconfigure:
886 acpi_dma_deconfigure(&pdev->dev);
887dev_put:
888 platform_device_put(pdev);
889
890 return ret;
891}
892
893static void __init iort_init_platform_devices(void)
894{
895 struct acpi_iort_node *iort_node, *iort_end;
896 struct acpi_table_iort *iort;
897 struct fwnode_handle *fwnode;
898 int i, ret;
899
900 /*
901 * iort_table and iort both point to the start of IORT table, but
902 * have different struct types
903 */
904 iort = (struct acpi_table_iort *)iort_table;
905
906 /* Get the first IORT node */
907 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
908 iort->node_offset);
909 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
910 iort_table->length);
911
912 for (i = 0; i < iort->node_count; i++) {
913 if (iort_node >= iort_end) {
914 pr_err("iort node pointer overflows, bad table\n");
915 return;
916 }
917
918 if ((iort_node->type == ACPI_IORT_NODE_SMMU) ||
919 (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) {
920
921 fwnode = acpi_alloc_fwnode_static();
922 if (!fwnode)
923 return;
924
925 iort_set_fwnode(iort_node, fwnode);
926
927 ret = iort_add_smmu_platform_device(iort_node);
928 if (ret) {
929 iort_delete_fwnode(iort_node);
930 acpi_free_fwnode_static(fwnode);
931 return;
932 }
933 }
934
935 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
936 iort_node->length);
937 }
938}
939
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200940void __init acpi_iort_init(void)
941{
942 acpi_status status;
943
944 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000945 if (ACPI_FAILURE(status)) {
946 if (status != AE_NOT_FOUND) {
947 const char *msg = acpi_format_exception(status);
948
949 pr_err("Failed to get table, %s\n", msg);
950 }
951
952 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200953 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000954
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +0000955 iort_init_platform_devices();
956
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000957 acpi_probe_device_table(iort);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200958}