blob: 9e702bc4960f0b3e279574e388027e5528b68034 [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 */
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +010086static inline struct fwnode_handle *iort_get_fwnode(
87 struct acpi_iort_node *node)
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000088{
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
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800124/**
125 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
126 *
127 * @fwnode: fwnode associated with device to be looked-up
128 *
129 * Returns: iort_node pointer on success, NULL on failure
130 */
131static inline struct acpi_iort_node *iort_get_iort_node(
132 struct fwnode_handle *fwnode)
133{
134 struct iort_fwnode *curr;
135 struct acpi_iort_node *iort_node = NULL;
136
137 spin_lock(&iort_fwnode_lock);
138 list_for_each_entry(curr, &iort_fwnode_list, list) {
139 if (curr->fwnode == fwnode) {
140 iort_node = curr->iort_node;
141 break;
142 }
143 }
144 spin_unlock(&iort_fwnode_lock);
145
146 return iort_node;
147}
148
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200149typedef acpi_status (*iort_find_node_callback)
150 (struct acpi_iort_node *node, void *context);
151
152/* Root pointer to the mapped IORT table */
153static struct acpi_table_header *iort_table;
154
155static LIST_HEAD(iort_msi_chip_list);
156static DEFINE_SPINLOCK(iort_msi_chip_lock);
157
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200158/**
159 * iort_register_domain_token() - register domain token and related ITS ID
160 * to the list from where we can get it back later on.
161 * @trans_id: ITS ID.
162 * @fw_node: Domain token.
163 *
164 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
165 */
166int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
167{
168 struct iort_its_msi_chip *its_msi_chip;
169
170 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
171 if (!its_msi_chip)
172 return -ENOMEM;
173
174 its_msi_chip->fw_node = fw_node;
175 its_msi_chip->translation_id = trans_id;
176
177 spin_lock(&iort_msi_chip_lock);
178 list_add(&its_msi_chip->list, &iort_msi_chip_list);
179 spin_unlock(&iort_msi_chip_lock);
180
181 return 0;
182}
183
184/**
185 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
186 * @trans_id: ITS ID.
187 *
188 * Returns: none.
189 */
190void iort_deregister_domain_token(int trans_id)
191{
192 struct iort_its_msi_chip *its_msi_chip, *t;
193
194 spin_lock(&iort_msi_chip_lock);
195 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
196 if (its_msi_chip->translation_id == trans_id) {
197 list_del(&its_msi_chip->list);
198 kfree(its_msi_chip);
199 break;
200 }
201 }
202 spin_unlock(&iort_msi_chip_lock);
203}
204
205/**
206 * iort_find_domain_token() - Find domain token based on given ITS ID
207 * @trans_id: ITS ID.
208 *
209 * Returns: domain token when find on the list, NULL otherwise
210 */
211struct fwnode_handle *iort_find_domain_token(int trans_id)
212{
213 struct fwnode_handle *fw_node = NULL;
214 struct iort_its_msi_chip *its_msi_chip;
215
216 spin_lock(&iort_msi_chip_lock);
217 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
218 if (its_msi_chip->translation_id == trans_id) {
219 fw_node = its_msi_chip->fw_node;
220 break;
221 }
222 }
223 spin_unlock(&iort_msi_chip_lock);
224
225 return fw_node;
226}
227
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200228static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
229 iort_find_node_callback callback,
230 void *context)
231{
232 struct acpi_iort_node *iort_node, *iort_end;
233 struct acpi_table_iort *iort;
234 int i;
235
236 if (!iort_table)
237 return NULL;
238
239 /* Get the first IORT node */
240 iort = (struct acpi_table_iort *)iort_table;
241 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
242 iort->node_offset);
243 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
244 iort_table->length);
245
246 for (i = 0; i < iort->node_count; i++) {
247 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
248 "IORT node pointer overflows, bad table!\n"))
249 return NULL;
250
251 if (iort_node->type == type &&
252 ACPI_SUCCESS(callback(iort_node, context)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800253 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200254
255 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
256 iort_node->length);
257 }
258
259 return NULL;
260}
261
262static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
263 void *context)
264{
265 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800266 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200267
268 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
269 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
270 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
271 struct acpi_iort_named_component *ncomp;
272
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800273 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200274 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200275
276 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
277 if (ACPI_FAILURE(status)) {
278 dev_warn(dev, "Can't get device full path name\n");
279 goto out;
280 }
281
282 ncomp = (struct acpi_iort_named_component *)node->node_data;
283 status = !strcmp(ncomp->device_name, buf.pointer) ?
284 AE_OK : AE_NOT_FOUND;
285 acpi_os_free(buf.pointer);
286 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
287 struct acpi_iort_root_complex *pci_rc;
288 struct pci_bus *bus;
289
290 bus = to_pci_bus(dev);
291 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
292
293 /*
294 * It is assumed that PCI segment numbers maps one-to-one
295 * with root complexes. Each segment number can represent only
296 * one root complex.
297 */
298 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
299 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200300 }
301out:
302 return status;
303}
304
305static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
306 u32 *rid_out)
307{
308 /* Single mapping does not care for input id */
309 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
310 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
311 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
312 *rid_out = map->output_base;
313 return 0;
314 }
315
316 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
317 map, type);
318 return -ENXIO;
319 }
320
321 if (rid_in < map->input_base ||
322 (rid_in >= map->input_base + map->id_count))
323 return -ENXIO;
324
325 *rid_out = map->output_base + (rid_in - map->input_base);
326 return 0;
327}
328
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100329static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
330 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000331{
332 struct acpi_iort_node *parent;
333 struct acpi_iort_id_mapping *map;
334
335 if (!node->mapping_offset || !node->mapping_count ||
336 index >= node->mapping_count)
337 return NULL;
338
339 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000340 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000341
342 /* Firmware bug! */
343 if (!map->output_reference) {
344 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
345 node, node->type);
346 return NULL;
347 }
348
349 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
350 map->output_reference);
351
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000352 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000353 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800354 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
355 node->type == ACPI_IORT_NODE_SMMU_V3) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000356 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000357 return parent;
358 }
359 }
360
361 return NULL;
362}
363
Hanjun Guo86456a32017-10-13 15:09:49 +0800364static int iort_get_id_mapping_index(struct acpi_iort_node *node)
365{
366 struct acpi_iort_smmu_v3 *smmu;
367
368 switch (node->type) {
369 case ACPI_IORT_NODE_SMMU_V3:
370 /*
371 * SMMUv3 dev ID mapping index was introduced in revision 1
372 * table, not available in revision 0
373 */
374 if (node->revision < 1)
375 return -EINVAL;
376
377 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
378 /*
379 * ID mapping index is only ignored if all interrupts are
380 * GSIV based
381 */
382 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
383 && smmu->sync_gsiv)
384 return -EINVAL;
385
386 if (smmu->id_mapping_index >= node->mapping_count) {
387 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
388 node, node->type);
389 return -EINVAL;
390 }
391
392 return smmu->id_mapping_index;
393 default:
394 return -EINVAL;
395 }
396}
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800397
Hanjun Guo697f6092017-03-07 20:40:03 +0800398static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
399 u32 id_in, u32 *id_out,
400 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200401{
Hanjun Guo697f6092017-03-07 20:40:03 +0800402 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200403
404 /* Parse the ID mapping tree to find specified node type */
405 while (node) {
406 struct acpi_iort_id_mapping *map;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800407 int i, index;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200408
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000409 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800410 if (id_out)
411 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200412 return node;
413 }
414
415 if (!node->mapping_offset || !node->mapping_count)
416 goto fail_map;
417
418 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
419 node->mapping_offset);
420
421 /* Firmware bug! */
422 if (!map->output_reference) {
423 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
424 node, node->type);
425 goto fail_map;
426 }
427
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800428 /*
429 * Get the special ID mapping index (if any) and skip its
430 * associated ID map to prevent erroneous multi-stage
431 * IORT ID translations.
432 */
433 index = iort_get_id_mapping_index(node);
434
Hanjun Guo697f6092017-03-07 20:40:03 +0800435 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200436 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800437 /* if it is special mapping index, skip it */
438 if (i == index)
439 continue;
440
Hanjun Guo697f6092017-03-07 20:40:03 +0800441 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200442 break;
443 }
444
445 if (i == node->mapping_count)
446 goto fail_map;
447
448 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
449 map->output_reference);
450 }
451
452fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800453 /* Map input ID to output ID unchanged on mapping failure */
454 if (id_out)
455 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200456
457 return NULL;
458}
459
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100460static struct acpi_iort_node *iort_node_map_platform_id(
461 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
462 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800463{
464 struct acpi_iort_node *parent;
465 u32 id;
466
467 /* step 1: retrieve the initial dev id */
468 parent = iort_node_get_id(node, &id, index);
469 if (!parent)
470 return NULL;
471
472 /*
473 * optional step 2: map the initial dev id if its parent is not
474 * the target type we want, map it again for the use cases such
475 * as NC (named component) -> SMMU -> ITS. If the type is matched,
476 * return the initial dev id and its parent pointer directly.
477 */
478 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
479 parent = iort_node_map_id(parent, id, id_out, type_mask);
480 else
481 if (id_out)
482 *id_out = id;
483
484 return parent;
485}
486
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200487static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
488{
489 struct pci_bus *pbus;
490
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800491 if (!dev_is_pci(dev)) {
492 struct acpi_iort_node *node;
493 /*
494 * scan iort_fwnode_list to see if it's an iort platform
495 * device (such as SMMU, PMCG),its iort node already cached
496 * and associated with fwnode when iort platform devices
497 * were initialized.
498 */
499 node = iort_get_iort_node(dev->fwnode);
500 if (node)
501 return node;
502
503 /*
504 * if not, then it should be a platform device defined in
505 * DSDT/SSDT (with Named Component node in IORT)
506 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200507 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
508 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800509 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200510
511 /* Find a PCI root bus */
512 pbus = to_pci_dev(dev)->bus;
513 while (!pci_is_root_bus(pbus))
514 pbus = pbus->parent;
515
516 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
517 iort_match_node_callback, &pbus->dev);
518}
519
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200520/**
521 * iort_msi_map_rid() - Map a MSI requester ID for a device
522 * @dev: The device for which the mapping is to be done.
523 * @req_id: The device requester ID.
524 *
525 * Returns: mapped MSI RID on success, input requester ID otherwise
526 */
527u32 iort_msi_map_rid(struct device *dev, u32 req_id)
528{
529 struct acpi_iort_node *node;
530 u32 dev_id;
531
532 node = iort_find_dev_node(dev);
533 if (!node)
534 return req_id;
535
Hanjun Guo697f6092017-03-07 20:40:03 +0800536 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200537 return dev_id;
538}
539
540/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800541 * iort_pmsi_get_dev_id() - Get the device id for a device
542 * @dev: The device for which the mapping is to be done.
543 * @dev_id: The device ID found.
544 *
545 * Returns: 0 for successful find a dev id, -ENODEV on error
546 */
547int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
548{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800549 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800550 struct acpi_iort_node *node;
551
552 node = iort_find_dev_node(dev);
553 if (!node)
554 return -ENODEV;
555
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800556 index = iort_get_id_mapping_index(node);
557 /* if there is a valid index, go get the dev_id directly */
558 if (index >= 0) {
559 if (iort_node_get_id(node, dev_id, index))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800560 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800561 } else {
562 for (i = 0; i < node->mapping_count; i++) {
563 if (iort_node_map_platform_id(node, dev_id,
564 IORT_MSI_TYPE, i))
565 return 0;
566 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800567 }
568
569 return -ENODEV;
570}
571
572/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200573 * iort_dev_find_its_id() - Find the ITS identifier for a device
574 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800575 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200576 * @idx: Index of the ITS identifier list.
577 * @its_id: ITS identifier.
578 *
579 * Returns: 0 on success, appropriate error value otherwise
580 */
581static int iort_dev_find_its_id(struct device *dev, u32 req_id,
582 unsigned int idx, int *its_id)
583{
584 struct acpi_iort_its_group *its;
585 struct acpi_iort_node *node;
586
587 node = iort_find_dev_node(dev);
588 if (!node)
589 return -ENXIO;
590
Hanjun Guo697f6092017-03-07 20:40:03 +0800591 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200592 if (!node)
593 return -ENXIO;
594
595 /* Move to ITS specific data */
596 its = (struct acpi_iort_its_group *)node->node_data;
597 if (idx > its->its_count) {
598 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
599 idx, its->its_count);
600 return -ENXIO;
601 }
602
603 *its_id = its->identifiers[idx];
604 return 0;
605}
606
607/**
608 * iort_get_device_domain() - Find MSI domain related to a device
609 * @dev: The device.
610 * @req_id: Requester ID for the device.
611 *
612 * Returns: the MSI domain for this device, NULL otherwise
613 */
614struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
615{
616 struct fwnode_handle *handle;
617 int its_id;
618
619 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
620 return NULL;
621
622 handle = iort_find_domain_token(its_id);
623 if (!handle)
624 return NULL;
625
626 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
627}
628
Lorenzo Pieralisi65637902017-10-13 15:09:50 +0800629static void iort_set_device_domain(struct device *dev,
630 struct acpi_iort_node *node)
631{
632 struct acpi_iort_its_group *its;
633 struct acpi_iort_node *msi_parent;
634 struct acpi_iort_id_mapping *map;
635 struct fwnode_handle *iort_fwnode;
636 struct irq_domain *domain;
637 int index;
638
639 index = iort_get_id_mapping_index(node);
640 if (index < 0)
641 return;
642
643 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
644 node->mapping_offset + index * sizeof(*map));
645
646 /* Firmware bug! */
647 if (!map->output_reference ||
648 !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) {
649 pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n",
650 node, node->type);
651 return;
652 }
653
654 msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
655 map->output_reference);
656
657 if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP)
658 return;
659
660 /* Move to ITS specific data */
661 its = (struct acpi_iort_its_group *)msi_parent->node_data;
662
663 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
664 if (!iort_fwnode)
665 return;
666
667 domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
668 if (domain)
669 dev_set_msi_domain(dev, domain);
670}
671
Hanjun Guod4f54a12017-03-07 20:40:06 +0800672/**
673 * iort_get_platform_device_domain() - Find MSI domain related to a
674 * platform device
675 * @dev: the dev pointer associated with the platform device
676 *
677 * Returns: the MSI domain for this device, NULL otherwise
678 */
679static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
680{
681 struct acpi_iort_node *node, *msi_parent;
682 struct fwnode_handle *iort_fwnode;
683 struct acpi_iort_its_group *its;
684 int i;
685
686 /* find its associated iort node */
687 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
688 iort_match_node_callback, dev);
689 if (!node)
690 return NULL;
691
692 /* then find its msi parent node */
693 for (i = 0; i < node->mapping_count; i++) {
694 msi_parent = iort_node_map_platform_id(node, NULL,
695 IORT_MSI_TYPE, i);
696 if (msi_parent)
697 break;
698 }
699
700 if (!msi_parent)
701 return NULL;
702
703 /* Move to ITS specific data */
704 its = (struct acpi_iort_its_group *)msi_parent->node_data;
705
706 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
707 if (!iort_fwnode)
708 return NULL;
709
710 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
711}
712
713void acpi_configure_pmsi_domain(struct device *dev)
714{
715 struct irq_domain *msi_domain;
716
717 msi_domain = iort_get_platform_device_domain(dev);
718 if (msi_domain)
719 dev_set_msi_domain(dev, msi_domain);
720}
721
Robin Murphybc8648d2017-08-04 17:42:06 +0100722static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
723 void *data)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000724{
725 u32 *rid = data;
726
727 *rid = alias;
728 return 0;
729}
730
731static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
732 struct fwnode_handle *fwnode,
733 const struct iommu_ops *ops)
734{
735 int ret = iommu_fwspec_init(dev, fwnode, ops);
736
737 if (!ret)
738 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
739
740 return ret;
741}
742
Lorenzo Pieralisi1d9029d2017-04-10 16:50:59 +0530743static inline bool iort_iommu_driver_enabled(u8 type)
744{
745 switch (type) {
746 case ACPI_IORT_NODE_SMMU_V3:
747 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
748 case ACPI_IORT_NODE_SMMU:
749 return IS_BUILTIN(CONFIG_ARM_SMMU);
750 default:
751 pr_warn("IORT node type %u does not describe an SMMU\n", type);
752 return false;
753 }
754}
755
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100756#ifdef CONFIG_IOMMU_API
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100757static inline const struct iommu_ops *iort_fwspec_iommu_ops(
758 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100759{
760 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
761}
762
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100763static inline int iort_add_device_replay(const struct iommu_ops *ops,
764 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100765{
766 int err = 0;
767
Robin Murphybc8648d2017-08-04 17:42:06 +0100768 if (ops->add_device && dev->bus && !dev->iommu_group)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100769 err = ops->add_device(dev);
770
771 return err;
772}
773#else
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100774static inline const struct iommu_ops *iort_fwspec_iommu_ops(
775 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100776{ return NULL; }
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100777static inline int iort_add_device_replay(const struct iommu_ops *ops,
778 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100779{ return 0; }
780#endif
781
Robin Murphybc8648d2017-08-04 17:42:06 +0100782static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
783 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000784{
Robin Murphybc8648d2017-08-04 17:42:06 +0100785 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000786 struct fwnode_handle *iort_fwnode;
787
Robin Murphybc8648d2017-08-04 17:42:06 +0100788 if (!node)
789 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000790
Robin Murphybc8648d2017-08-04 17:42:06 +0100791 iort_fwnode = iort_get_fwnode(node);
792 if (!iort_fwnode)
793 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000794
Robin Murphybc8648d2017-08-04 17:42:06 +0100795 /*
796 * If the ops look-up fails, this means that either
797 * the SMMU drivers have not been probed yet or that
798 * the SMMU drivers are not built in the kernel;
799 * Depending on whether the SMMU drivers are built-in
800 * in the kernel or not, defer the IOMMU configuration
801 * or just abort it.
802 */
803 ops = iommu_ops_from_fwnode(iort_fwnode);
804 if (!ops)
805 return iort_iommu_driver_enabled(node->type) ?
806 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000807
Robin Murphybc8648d2017-08-04 17:42:06 +0100808 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
809}
810
811struct iort_pci_alias_info {
812 struct device *dev;
813 struct acpi_iort_node *node;
814};
815
816static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
817{
818 struct iort_pci_alias_info *info = data;
819 struct acpi_iort_node *parent;
820 u32 streamid;
821
822 parent = iort_node_map_id(info->node, alias, &streamid,
823 IORT_IOMMU_TYPE);
824 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000825}
826
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100827static int nc_dma_get_range(struct device *dev, u64 *size)
828{
829 struct acpi_iort_node *node;
830 struct acpi_iort_named_component *ncomp;
831
832 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
833 iort_match_node_callback, dev);
834 if (!node)
835 return -ENODEV;
836
837 ncomp = (struct acpi_iort_named_component *)node->node_data;
838
839 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
840 1ULL<<ncomp->memory_address_limit;
841
842 return 0;
843}
844
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000845/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100846 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000847 *
848 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100849 * @dma_addr: device DMA address result pointer
850 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000851 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100852void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000853{
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100854 u64 mask, dmaaddr = 0, size = 0, offset = 0;
855 int ret, msb;
856
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000857 /*
858 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
859 * setup the correct supported mask.
860 */
861 if (!dev->coherent_dma_mask)
862 dev->coherent_dma_mask = DMA_BIT_MASK(32);
863
864 /*
865 * Set it to coherent_dma_mask by default if the architecture
866 * code has not set it.
867 */
868 if (!dev->dma_mask)
869 dev->dma_mask = &dev->coherent_dma_mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100870
871 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
872
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100873 if (dev_is_pci(dev))
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100874 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100875 else
876 ret = nc_dma_get_range(dev, &size);
877
878 if (!ret) {
879 msb = fls64(dmaaddr + size - 1);
880 /*
881 * Round-up to the power-of-two mask or set
882 * the mask to the whole 64-bit address space
883 * in case the DMA region covers the full
884 * memory window.
885 */
886 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
887 /*
888 * Limit coherent and dma mask based on size
889 * retrieved from firmware.
890 */
891 dev->coherent_dma_mask = mask;
892 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100893 }
894
895 *dma_addr = dmaaddr;
896 *dma_size = size;
897
898 dev->dma_pfn_offset = PFN_DOWN(offset);
899 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000900}
901
902/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000903 * iort_iommu_configure - Set-up IOMMU configuration for a device.
904 *
905 * @dev: device to configure
906 *
907 * Returns: iommu_ops pointer on configuration success
908 * NULL on configuration failure
909 */
910const struct iommu_ops *iort_iommu_configure(struct device *dev)
911{
912 struct acpi_iort_node *node, *parent;
Robin Murphybc8648d2017-08-04 17:42:06 +0100913 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000914 u32 streamid = 0;
Robin Murphybc8648d2017-08-04 17:42:06 +0100915 int err = -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000916
Lorenzo Pieralisi4dac3212017-05-27 19:17:44 +0530917 /*
918 * If we already translated the fwspec there
919 * is nothing left to do, return the iommu_ops.
920 */
921 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
922 if (ops)
923 return ops;
924
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000925 if (dev_is_pci(dev)) {
926 struct pci_bus *bus = to_pci_dev(dev)->bus;
Robin Murphybc8648d2017-08-04 17:42:06 +0100927 struct iort_pci_alias_info info = { .dev = dev };
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000928
929 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
930 iort_match_node_callback, &bus->dev);
931 if (!node)
932 return NULL;
933
Robin Murphybc8648d2017-08-04 17:42:06 +0100934 info.node = node;
935 err = pci_for_each_dma_alias(to_pci_dev(dev),
936 iort_pci_iommu_init, &info);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000937 } else {
938 int i = 0;
939
940 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
941 iort_match_node_callback, dev);
942 if (!node)
943 return NULL;
944
Robin Murphybc8648d2017-08-04 17:42:06 +0100945 do {
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800946 parent = iort_node_map_platform_id(node, &streamid,
947 IORT_IOMMU_TYPE,
948 i++);
Robin Murphybc8648d2017-08-04 17:42:06 +0100949
950 if (parent)
951 err = iort_iommu_xlate(dev, parent, streamid);
952 } while (parent && !err);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000953 }
954
Sricharan R5a1bb632017-04-10 16:51:03 +0530955 /*
956 * If we have reason to believe the IOMMU driver missed the initial
957 * add_device callback for dev, replay it to get things in order.
958 */
Robin Murphybc8648d2017-08-04 17:42:06 +0100959 if (!err) {
Arnd Bergmann4d360372017-08-10 17:45:22 +0100960 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
Robin Murphybc8648d2017-08-04 17:42:06 +0100961 err = iort_add_device_replay(ops, dev);
962 }
Sricharan R5a1bb632017-04-10 16:51:03 +0530963
Sricharan R058f8c32017-05-27 19:17:42 +0530964 /* Ignore all other errors apart from EPROBE_DEFER */
Robin Murphybc8648d2017-08-04 17:42:06 +0100965 if (err == -EPROBE_DEFER) {
966 ops = ERR_PTR(err);
967 } else if (err) {
968 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
Sricharan R058f8c32017-05-27 19:17:42 +0530969 ops = NULL;
970 }
971
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000972 return ops;
973}
974
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000975static void __init acpi_iort_register_irq(int hwirq, const char *name,
976 int trigger,
977 struct resource *res)
978{
979 int irq = acpi_register_gsi(NULL, hwirq, trigger,
980 ACPI_ACTIVE_HIGH);
981
982 if (irq <= 0) {
983 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
984 name);
985 return;
986 }
987
988 res->start = irq;
989 res->end = irq;
990 res->flags = IORESOURCE_IRQ;
991 res->name = name;
992}
993
994static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
995{
996 struct acpi_iort_smmu_v3 *smmu;
997 /* Always present mem resource */
998 int num_res = 1;
999
1000 /* Retrieve SMMUv3 specific data */
1001 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1002
1003 if (smmu->event_gsiv)
1004 num_res++;
1005
1006 if (smmu->pri_gsiv)
1007 num_res++;
1008
1009 if (smmu->gerr_gsiv)
1010 num_res++;
1011
1012 if (smmu->sync_gsiv)
1013 num_res++;
1014
1015 return num_res;
1016}
1017
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301018static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
1019{
1020 /*
1021 * Cavium ThunderX2 implementation doesn't not support unique
1022 * irq line. Use single irq line for all the SMMUv3 interrupts.
1023 */
1024 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1025 return false;
1026
1027 /*
1028 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
1029 * SPI numbers here.
1030 */
1031 return smmu->event_gsiv == smmu->pri_gsiv &&
1032 smmu->event_gsiv == smmu->gerr_gsiv &&
1033 smmu->event_gsiv == smmu->sync_gsiv;
1034}
1035
Linu Cherian403e8c72017-06-22 17:35:36 +05301036static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1037{
1038 /*
1039 * Override the size, for Cavium ThunderX2 implementation
1040 * which doesn't support the page 1 SMMU register space.
1041 */
1042 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1043 return SZ_64K;
1044
1045 return SZ_128K;
1046}
1047
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001048static void __init arm_smmu_v3_init_resources(struct resource *res,
1049 struct acpi_iort_node *node)
1050{
1051 struct acpi_iort_smmu_v3 *smmu;
1052 int num_res = 0;
1053
1054 /* Retrieve SMMUv3 specific data */
1055 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1056
1057 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301058 res[num_res].end = smmu->base_address +
1059 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001060 res[num_res].flags = IORESOURCE_MEM;
1061
1062 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301063 if (arm_smmu_v3_is_combined_irq(smmu)) {
1064 if (smmu->event_gsiv)
1065 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1066 ACPI_EDGE_SENSITIVE,
1067 &res[num_res++]);
1068 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001069
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301070 if (smmu->event_gsiv)
1071 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1072 ACPI_EDGE_SENSITIVE,
1073 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001074
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301075 if (smmu->pri_gsiv)
1076 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1077 ACPI_EDGE_SENSITIVE,
1078 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001079
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301080 if (smmu->gerr_gsiv)
1081 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1082 ACPI_EDGE_SENSITIVE,
1083 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001084
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301085 if (smmu->sync_gsiv)
1086 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1087 ACPI_EDGE_SENSITIVE,
1088 &res[num_res++]);
1089 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001090}
1091
1092static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
1093{
1094 struct acpi_iort_smmu_v3 *smmu;
1095
1096 /* Retrieve SMMUv3 specific data */
1097 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1098
1099 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
1100}
1101
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001102#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001103/*
1104 * set numa proximity domain for smmuv3 device
1105 */
1106static void __init arm_smmu_v3_set_proximity(struct device *dev,
1107 struct acpi_iort_node *node)
1108{
1109 struct acpi_iort_smmu_v3 *smmu;
1110
1111 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1112 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1113 set_dev_node(dev, acpi_map_pxm_to_node(smmu->pxm));
1114 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1115 smmu->base_address,
1116 smmu->pxm);
1117 }
1118}
1119#else
1120#define arm_smmu_v3_set_proximity NULL
1121#endif
1122
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001123static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1124{
1125 struct acpi_iort_smmu *smmu;
1126
1127 /* Retrieve SMMU specific data */
1128 smmu = (struct acpi_iort_smmu *)node->node_data;
1129
1130 /*
1131 * Only consider the global fault interrupt and ignore the
1132 * configuration access interrupt.
1133 *
1134 * MMIO address and global fault interrupt resources are always
1135 * present so add them to the context interrupt count as a static
1136 * value.
1137 */
1138 return smmu->context_interrupt_count + 2;
1139}
1140
1141static void __init arm_smmu_init_resources(struct resource *res,
1142 struct acpi_iort_node *node)
1143{
1144 struct acpi_iort_smmu *smmu;
1145 int i, hw_irq, trigger, num_res = 0;
1146 u64 *ctx_irq, *glb_irq;
1147
1148 /* Retrieve SMMU specific data */
1149 smmu = (struct acpi_iort_smmu *)node->node_data;
1150
1151 res[num_res].start = smmu->base_address;
1152 res[num_res].end = smmu->base_address + smmu->span - 1;
1153 res[num_res].flags = IORESOURCE_MEM;
1154 num_res++;
1155
1156 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1157 /* Global IRQs */
1158 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1159 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1160
1161 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1162 &res[num_res++]);
1163
1164 /* Context IRQs */
1165 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1166 for (i = 0; i < smmu->context_interrupt_count; i++) {
1167 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1168 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1169
1170 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1171 &res[num_res++]);
1172 }
1173}
1174
1175static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
1176{
1177 struct acpi_iort_smmu *smmu;
1178
1179 /* Retrieve SMMU specific data */
1180 smmu = (struct acpi_iort_smmu *)node->node_data;
1181
1182 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
1183}
1184
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001185struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001186 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001187 int (*dev_init)(struct acpi_iort_node *node);
1188 bool (*dev_is_coherent)(struct acpi_iort_node *node);
1189 int (*dev_count_resources)(struct acpi_iort_node *node);
1190 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001191 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001192 void (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001193 struct acpi_iort_node *node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001194};
1195
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001196static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001197 .name = "arm-smmu-v3",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001198 .dev_is_coherent = arm_smmu_v3_is_coherent,
1199 .dev_count_resources = arm_smmu_v3_count_resources,
1200 .dev_init_resources = arm_smmu_v3_init_resources,
1201 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001202};
1203
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001204static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001205 .name = "arm-smmu",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001206 .dev_is_coherent = arm_smmu_is_coherent,
1207 .dev_count_resources = arm_smmu_count_resources,
1208 .dev_init_resources = arm_smmu_init_resources
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001209};
1210
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001211static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001212 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001213{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001214 switch (node->type) {
1215 case ACPI_IORT_NODE_SMMU_V3:
1216 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001217 case ACPI_IORT_NODE_SMMU:
1218 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001219 default:
1220 return NULL;
1221 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001222}
1223
1224/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001225 * iort_add_platform_device() - Allocate a platform device for IORT node
1226 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001227 *
1228 * Returns: 0 on success, <0 failure
1229 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001230static int __init iort_add_platform_device(struct acpi_iort_node *node,
1231 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001232{
1233 struct fwnode_handle *fwnode;
1234 struct platform_device *pdev;
1235 struct resource *r;
1236 enum dev_dma_attr attr;
1237 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001238
1239 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1240 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001241 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001242
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001243 if (ops->dev_set_proximity)
1244 ops->dev_set_proximity(&pdev->dev, node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001245
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001246 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001247
1248 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1249 if (!r) {
1250 ret = -ENOMEM;
1251 goto dev_put;
1252 }
1253
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001254 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001255
1256 ret = platform_device_add_resources(pdev, r, count);
1257 /*
1258 * Resources are duplicated in platform_device_add_resources,
1259 * free their allocated memory
1260 */
1261 kfree(r);
1262
1263 if (ret)
1264 goto dev_put;
1265
1266 /*
1267 * Add a copy of IORT node pointer to platform_data to
1268 * be used to retrieve IORT data information.
1269 */
1270 ret = platform_device_add_data(pdev, &node, sizeof(node));
1271 if (ret)
1272 goto dev_put;
1273
1274 /*
1275 * We expect the dma masks to be equivalent for
1276 * all SMMUs set-ups
1277 */
1278 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1279
1280 fwnode = iort_get_fwnode(node);
1281
1282 if (!fwnode) {
1283 ret = -ENODEV;
1284 goto dev_put;
1285 }
1286
1287 pdev->dev.fwnode = fwnode;
1288
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001289 attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
1290 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001291
1292 /* Configure DMA for the page table walker */
1293 acpi_dma_configure(&pdev->dev, attr);
1294
Lorenzo Pieralisi65637902017-10-13 15:09:50 +08001295 iort_set_device_domain(&pdev->dev, node);
1296
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001297 ret = platform_device_add(pdev);
1298 if (ret)
1299 goto dma_deconfigure;
1300
1301 return 0;
1302
1303dma_deconfigure:
1304 acpi_dma_deconfigure(&pdev->dev);
1305dev_put:
1306 platform_device_put(pdev);
1307
1308 return ret;
1309}
1310
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001311static bool __init iort_enable_acs(struct acpi_iort_node *iort_node)
1312{
1313 if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
1314 struct acpi_iort_node *parent;
1315 struct acpi_iort_id_mapping *map;
1316 int i;
1317
1318 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node,
1319 iort_node->mapping_offset);
1320
1321 for (i = 0; i < iort_node->mapping_count; i++, map++) {
1322 if (!map->output_reference)
1323 continue;
1324
1325 parent = ACPI_ADD_PTR(struct acpi_iort_node,
1326 iort_table, map->output_reference);
1327 /*
1328 * If we detect a RC->SMMU mapping, make sure
1329 * we enable ACS on the system.
1330 */
1331 if ((parent->type == ACPI_IORT_NODE_SMMU) ||
1332 (parent->type == ACPI_IORT_NODE_SMMU_V3)) {
1333 pci_request_acs();
1334 return true;
1335 }
1336 }
1337 }
1338
1339 return false;
1340}
1341
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001342static void __init iort_init_platform_devices(void)
1343{
1344 struct acpi_iort_node *iort_node, *iort_end;
1345 struct acpi_table_iort *iort;
1346 struct fwnode_handle *fwnode;
1347 int i, ret;
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001348 bool acs_enabled = false;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001349 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001350
1351 /*
1352 * iort_table and iort both point to the start of IORT table, but
1353 * have different struct types
1354 */
1355 iort = (struct acpi_table_iort *)iort_table;
1356
1357 /* Get the first IORT node */
1358 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1359 iort->node_offset);
1360 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1361 iort_table->length);
1362
1363 for (i = 0; i < iort->node_count; i++) {
1364 if (iort_node >= iort_end) {
1365 pr_err("iort node pointer overflows, bad table\n");
1366 return;
1367 }
1368
Lorenzo Pieralisi37f6b422017-10-02 18:28:44 +01001369 if (!acs_enabled)
1370 acs_enabled = iort_enable_acs(iort_node);
1371
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001372 ops = iort_get_dev_cfg(iort_node);
1373 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001374 fwnode = acpi_alloc_fwnode_static();
1375 if (!fwnode)
1376 return;
1377
1378 iort_set_fwnode(iort_node, fwnode);
1379
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001380 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001381 if (ret) {
1382 iort_delete_fwnode(iort_node);
1383 acpi_free_fwnode_static(fwnode);
1384 return;
1385 }
1386 }
1387
1388 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1389 iort_node->length);
1390 }
1391}
1392
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001393void __init acpi_iort_init(void)
1394{
1395 acpi_status status;
1396
1397 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001398 if (ACPI_FAILURE(status)) {
1399 if (status != AE_NOT_FOUND) {
1400 const char *msg = acpi_format_exception(status);
1401
1402 pr_err("Failed to get table, %s\n", msg);
1403 }
1404
1405 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001406 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001407
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001408 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001409}