blob: 3d7e3cd2eae8f62b999d0ca804526c0a714b6aa6 [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
Robert Richter12275bf2017-06-22 21:20:54 +020034/* Until ACPICA headers cover IORT rev. C */
35#ifndef ACPI_IORT_SMMU_V3_CAVIUM_CN99XX
36#define ACPI_IORT_SMMU_V3_CAVIUM_CN99XX 0x2
37#endif
38
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020039struct iort_its_msi_chip {
40 struct list_head list;
41 struct fwnode_handle *fw_node;
42 u32 translation_id;
43};
44
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000045struct iort_fwnode {
46 struct list_head list;
47 struct acpi_iort_node *iort_node;
48 struct fwnode_handle *fwnode;
49};
50static LIST_HEAD(iort_fwnode_list);
51static DEFINE_SPINLOCK(iort_fwnode_lock);
52
53/**
54 * iort_set_fwnode() - Create iort_fwnode and use it to register
55 * iommu data in the iort_fwnode_list
56 *
57 * @node: IORT table node associated with the IOMMU
58 * @fwnode: fwnode associated with the IORT node
59 *
60 * Returns: 0 on success
61 * <0 on failure
62 */
63static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
64 struct fwnode_handle *fwnode)
65{
66 struct iort_fwnode *np;
67
68 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
69
70 if (WARN_ON(!np))
71 return -ENOMEM;
72
73 INIT_LIST_HEAD(&np->list);
74 np->iort_node = iort_node;
75 np->fwnode = fwnode;
76
77 spin_lock(&iort_fwnode_lock);
78 list_add_tail(&np->list, &iort_fwnode_list);
79 spin_unlock(&iort_fwnode_lock);
80
81 return 0;
82}
83
84/**
85 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
86 *
87 * @node: IORT table node to be looked-up
88 *
89 * Returns: fwnode_handle pointer on success, NULL on failure
90 */
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +010091static inline struct fwnode_handle *iort_get_fwnode(
92 struct acpi_iort_node *node)
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000093{
94 struct iort_fwnode *curr;
95 struct fwnode_handle *fwnode = NULL;
96
97 spin_lock(&iort_fwnode_lock);
98 list_for_each_entry(curr, &iort_fwnode_list, list) {
99 if (curr->iort_node == node) {
100 fwnode = curr->fwnode;
101 break;
102 }
103 }
104 spin_unlock(&iort_fwnode_lock);
105
106 return fwnode;
107}
108
109/**
110 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
111 *
112 * @node: IORT table node associated with fwnode to delete
113 */
114static inline void iort_delete_fwnode(struct acpi_iort_node *node)
115{
116 struct iort_fwnode *curr, *tmp;
117
118 spin_lock(&iort_fwnode_lock);
119 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
120 if (curr->iort_node == node) {
121 list_del(&curr->list);
122 kfree(curr);
123 break;
124 }
125 }
126 spin_unlock(&iort_fwnode_lock);
127}
128
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800129/**
130 * iort_get_iort_node() - Retrieve iort_node associated with an fwnode
131 *
132 * @fwnode: fwnode associated with device to be looked-up
133 *
134 * Returns: iort_node pointer on success, NULL on failure
135 */
136static inline struct acpi_iort_node *iort_get_iort_node(
137 struct fwnode_handle *fwnode)
138{
139 struct iort_fwnode *curr;
140 struct acpi_iort_node *iort_node = NULL;
141
142 spin_lock(&iort_fwnode_lock);
143 list_for_each_entry(curr, &iort_fwnode_list, list) {
144 if (curr->fwnode == fwnode) {
145 iort_node = curr->iort_node;
146 break;
147 }
148 }
149 spin_unlock(&iort_fwnode_lock);
150
151 return iort_node;
152}
153
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200154typedef acpi_status (*iort_find_node_callback)
155 (struct acpi_iort_node *node, void *context);
156
157/* Root pointer to the mapped IORT table */
158static struct acpi_table_header *iort_table;
159
160static LIST_HEAD(iort_msi_chip_list);
161static DEFINE_SPINLOCK(iort_msi_chip_lock);
162
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200163/**
164 * iort_register_domain_token() - register domain token and related ITS ID
165 * to the list from where we can get it back later on.
166 * @trans_id: ITS ID.
167 * @fw_node: Domain token.
168 *
169 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
170 */
171int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
172{
173 struct iort_its_msi_chip *its_msi_chip;
174
175 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
176 if (!its_msi_chip)
177 return -ENOMEM;
178
179 its_msi_chip->fw_node = fw_node;
180 its_msi_chip->translation_id = trans_id;
181
182 spin_lock(&iort_msi_chip_lock);
183 list_add(&its_msi_chip->list, &iort_msi_chip_list);
184 spin_unlock(&iort_msi_chip_lock);
185
186 return 0;
187}
188
189/**
190 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
191 * @trans_id: ITS ID.
192 *
193 * Returns: none.
194 */
195void iort_deregister_domain_token(int trans_id)
196{
197 struct iort_its_msi_chip *its_msi_chip, *t;
198
199 spin_lock(&iort_msi_chip_lock);
200 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
201 if (its_msi_chip->translation_id == trans_id) {
202 list_del(&its_msi_chip->list);
203 kfree(its_msi_chip);
204 break;
205 }
206 }
207 spin_unlock(&iort_msi_chip_lock);
208}
209
210/**
211 * iort_find_domain_token() - Find domain token based on given ITS ID
212 * @trans_id: ITS ID.
213 *
214 * Returns: domain token when find on the list, NULL otherwise
215 */
216struct fwnode_handle *iort_find_domain_token(int trans_id)
217{
218 struct fwnode_handle *fw_node = NULL;
219 struct iort_its_msi_chip *its_msi_chip;
220
221 spin_lock(&iort_msi_chip_lock);
222 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
223 if (its_msi_chip->translation_id == trans_id) {
224 fw_node = its_msi_chip->fw_node;
225 break;
226 }
227 }
228 spin_unlock(&iort_msi_chip_lock);
229
230 return fw_node;
231}
232
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200233static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
234 iort_find_node_callback callback,
235 void *context)
236{
237 struct acpi_iort_node *iort_node, *iort_end;
238 struct acpi_table_iort *iort;
239 int i;
240
241 if (!iort_table)
242 return NULL;
243
244 /* Get the first IORT node */
245 iort = (struct acpi_table_iort *)iort_table;
246 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
247 iort->node_offset);
248 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
249 iort_table->length);
250
251 for (i = 0; i < iort->node_count; i++) {
252 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
253 "IORT node pointer overflows, bad table!\n"))
254 return NULL;
255
256 if (iort_node->type == type &&
257 ACPI_SUCCESS(callback(iort_node, context)))
Hanjun Guod89cf2e2017-03-07 20:39:56 +0800258 return iort_node;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200259
260 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
261 iort_node->length);
262 }
263
264 return NULL;
265}
266
267static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
268 void *context)
269{
270 struct device *dev = context;
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800271 acpi_status status = AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200272
273 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
274 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
275 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
276 struct acpi_iort_named_component *ncomp;
277
Hanjun Guoc92bdfe2017-03-07 20:39:58 +0800278 if (!adev)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200279 goto out;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200280
281 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
282 if (ACPI_FAILURE(status)) {
283 dev_warn(dev, "Can't get device full path name\n");
284 goto out;
285 }
286
287 ncomp = (struct acpi_iort_named_component *)node->node_data;
288 status = !strcmp(ncomp->device_name, buf.pointer) ?
289 AE_OK : AE_NOT_FOUND;
290 acpi_os_free(buf.pointer);
291 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
292 struct acpi_iort_root_complex *pci_rc;
293 struct pci_bus *bus;
294
295 bus = to_pci_bus(dev);
296 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
297
298 /*
299 * It is assumed that PCI segment numbers maps one-to-one
300 * with root complexes. Each segment number can represent only
301 * one root complex.
302 */
303 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
304 AE_OK : AE_NOT_FOUND;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200305 }
306out:
307 return status;
308}
309
310static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
311 u32 *rid_out)
312{
313 /* Single mapping does not care for input id */
314 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
315 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
316 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
317 *rid_out = map->output_base;
318 return 0;
319 }
320
321 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
322 map, type);
323 return -ENXIO;
324 }
325
326 if (rid_in < map->input_base ||
327 (rid_in >= map->input_base + map->id_count))
328 return -ENXIO;
329
330 *rid_out = map->output_base + (rid_in - map->input_base);
331 return 0;
332}
333
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100334static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node,
335 u32 *id_out, int index)
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000336{
337 struct acpi_iort_node *parent;
338 struct acpi_iort_id_mapping *map;
339
340 if (!node->mapping_offset || !node->mapping_count ||
341 index >= node->mapping_count)
342 return NULL;
343
344 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000345 node->mapping_offset + index * sizeof(*map));
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000346
347 /* Firmware bug! */
348 if (!map->output_reference) {
349 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
350 node, node->type);
351 return NULL;
352 }
353
354 parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
355 map->output_reference);
356
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000357 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000358 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT ||
Hanjun Guo86456a32017-10-13 15:09:49 +0800359 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX ||
360 node->type == ACPI_IORT_NODE_SMMU_V3) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000361 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000362 return parent;
363 }
364 }
365
366 return NULL;
367}
368
Hanjun Guo86456a32017-10-13 15:09:49 +0800369#if (ACPI_CA_VERSION > 0x20170929)
370static int iort_get_id_mapping_index(struct acpi_iort_node *node)
371{
372 struct acpi_iort_smmu_v3 *smmu;
373
374 switch (node->type) {
375 case ACPI_IORT_NODE_SMMU_V3:
376 /*
377 * SMMUv3 dev ID mapping index was introduced in revision 1
378 * table, not available in revision 0
379 */
380 if (node->revision < 1)
381 return -EINVAL;
382
383 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
384 /*
385 * ID mapping index is only ignored if all interrupts are
386 * GSIV based
387 */
388 if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv
389 && smmu->sync_gsiv)
390 return -EINVAL;
391
392 if (smmu->id_mapping_index >= node->mapping_count) {
393 pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n",
394 node, node->type);
395 return -EINVAL;
396 }
397
398 return smmu->id_mapping_index;
399 default:
400 return -EINVAL;
401 }
402}
403#else
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800404static inline int iort_get_id_mapping_index(struct acpi_iort_node *node)
405{
406 return -EINVAL;
407}
Hanjun Guo86456a32017-10-13 15:09:49 +0800408#endif
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800409
Hanjun Guo697f6092017-03-07 20:40:03 +0800410static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
411 u32 id_in, u32 *id_out,
412 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200413{
Hanjun Guo697f6092017-03-07 20:40:03 +0800414 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200415
416 /* Parse the ID mapping tree to find specified node type */
417 while (node) {
418 struct acpi_iort_id_mapping *map;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800419 int i, index;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200420
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000421 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800422 if (id_out)
423 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200424 return node;
425 }
426
427 if (!node->mapping_offset || !node->mapping_count)
428 goto fail_map;
429
430 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
431 node->mapping_offset);
432
433 /* Firmware bug! */
434 if (!map->output_reference) {
435 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
436 node, node->type);
437 goto fail_map;
438 }
439
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800440 /*
441 * Get the special ID mapping index (if any) and skip its
442 * associated ID map to prevent erroneous multi-stage
443 * IORT ID translations.
444 */
445 index = iort_get_id_mapping_index(node);
446
Hanjun Guo697f6092017-03-07 20:40:03 +0800447 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200448 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800449 /* if it is special mapping index, skip it */
450 if (i == index)
451 continue;
452
Hanjun Guo697f6092017-03-07 20:40:03 +0800453 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200454 break;
455 }
456
457 if (i == node->mapping_count)
458 goto fail_map;
459
460 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
461 map->output_reference);
462 }
463
464fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800465 /* Map input ID to output ID unchanged on mapping failure */
466 if (id_out)
467 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200468
469 return NULL;
470}
471
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100472static struct acpi_iort_node *iort_node_map_platform_id(
473 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
474 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800475{
476 struct acpi_iort_node *parent;
477 u32 id;
478
479 /* step 1: retrieve the initial dev id */
480 parent = iort_node_get_id(node, &id, index);
481 if (!parent)
482 return NULL;
483
484 /*
485 * optional step 2: map the initial dev id if its parent is not
486 * the target type we want, map it again for the use cases such
487 * as NC (named component) -> SMMU -> ITS. If the type is matched,
488 * return the initial dev id and its parent pointer directly.
489 */
490 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
491 parent = iort_node_map_id(parent, id, id_out, type_mask);
492 else
493 if (id_out)
494 *id_out = id;
495
496 return parent;
497}
498
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200499static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
500{
501 struct pci_bus *pbus;
502
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800503 if (!dev_is_pci(dev)) {
504 struct acpi_iort_node *node;
505 /*
506 * scan iort_fwnode_list to see if it's an iort platform
507 * device (such as SMMU, PMCG),its iort node already cached
508 * and associated with fwnode when iort platform devices
509 * were initialized.
510 */
511 node = iort_get_iort_node(dev->fwnode);
512 if (node)
513 return node;
514
515 /*
516 * if not, then it should be a platform device defined in
517 * DSDT/SSDT (with Named Component node in IORT)
518 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200519 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
520 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800521 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200522
523 /* Find a PCI root bus */
524 pbus = to_pci_dev(dev)->bus;
525 while (!pci_is_root_bus(pbus))
526 pbus = pbus->parent;
527
528 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
529 iort_match_node_callback, &pbus->dev);
530}
531
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200532/**
533 * iort_msi_map_rid() - Map a MSI requester ID for a device
534 * @dev: The device for which the mapping is to be done.
535 * @req_id: The device requester ID.
536 *
537 * Returns: mapped MSI RID on success, input requester ID otherwise
538 */
539u32 iort_msi_map_rid(struct device *dev, u32 req_id)
540{
541 struct acpi_iort_node *node;
542 u32 dev_id;
543
544 node = iort_find_dev_node(dev);
545 if (!node)
546 return req_id;
547
Hanjun Guo697f6092017-03-07 20:40:03 +0800548 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200549 return dev_id;
550}
551
552/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800553 * iort_pmsi_get_dev_id() - Get the device id for a device
554 * @dev: The device for which the mapping is to be done.
555 * @dev_id: The device ID found.
556 *
557 * Returns: 0 for successful find a dev id, -ENODEV on error
558 */
559int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
560{
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800561 int i, index;
Hanjun Guoae7c1832017-03-07 20:40:05 +0800562 struct acpi_iort_node *node;
563
564 node = iort_find_dev_node(dev);
565 if (!node)
566 return -ENODEV;
567
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800568 index = iort_get_id_mapping_index(node);
569 /* if there is a valid index, go get the dev_id directly */
570 if (index >= 0) {
571 if (iort_node_get_id(node, dev_id, index))
Hanjun Guoae7c1832017-03-07 20:40:05 +0800572 return 0;
Hanjun Guo8c8df8d2017-10-13 15:09:48 +0800573 } else {
574 for (i = 0; i < node->mapping_count; i++) {
575 if (iort_node_map_platform_id(node, dev_id,
576 IORT_MSI_TYPE, i))
577 return 0;
578 }
Hanjun Guoae7c1832017-03-07 20:40:05 +0800579 }
580
581 return -ENODEV;
582}
583
584/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200585 * iort_dev_find_its_id() - Find the ITS identifier for a device
586 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800587 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200588 * @idx: Index of the ITS identifier list.
589 * @its_id: ITS identifier.
590 *
591 * Returns: 0 on success, appropriate error value otherwise
592 */
593static int iort_dev_find_its_id(struct device *dev, u32 req_id,
594 unsigned int idx, int *its_id)
595{
596 struct acpi_iort_its_group *its;
597 struct acpi_iort_node *node;
598
599 node = iort_find_dev_node(dev);
600 if (!node)
601 return -ENXIO;
602
Hanjun Guo697f6092017-03-07 20:40:03 +0800603 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200604 if (!node)
605 return -ENXIO;
606
607 /* Move to ITS specific data */
608 its = (struct acpi_iort_its_group *)node->node_data;
609 if (idx > its->its_count) {
610 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
611 idx, its->its_count);
612 return -ENXIO;
613 }
614
615 *its_id = its->identifiers[idx];
616 return 0;
617}
618
619/**
620 * iort_get_device_domain() - Find MSI domain related to a device
621 * @dev: The device.
622 * @req_id: Requester ID for the device.
623 *
624 * Returns: the MSI domain for this device, NULL otherwise
625 */
626struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
627{
628 struct fwnode_handle *handle;
629 int its_id;
630
631 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
632 return NULL;
633
634 handle = iort_find_domain_token(its_id);
635 if (!handle)
636 return NULL;
637
638 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
639}
640
Hanjun Guod4f54a12017-03-07 20:40:06 +0800641/**
642 * iort_get_platform_device_domain() - Find MSI domain related to a
643 * platform device
644 * @dev: the dev pointer associated with the platform device
645 *
646 * Returns: the MSI domain for this device, NULL otherwise
647 */
648static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
649{
650 struct acpi_iort_node *node, *msi_parent;
651 struct fwnode_handle *iort_fwnode;
652 struct acpi_iort_its_group *its;
653 int i;
654
655 /* find its associated iort node */
656 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
657 iort_match_node_callback, dev);
658 if (!node)
659 return NULL;
660
661 /* then find its msi parent node */
662 for (i = 0; i < node->mapping_count; i++) {
663 msi_parent = iort_node_map_platform_id(node, NULL,
664 IORT_MSI_TYPE, i);
665 if (msi_parent)
666 break;
667 }
668
669 if (!msi_parent)
670 return NULL;
671
672 /* Move to ITS specific data */
673 its = (struct acpi_iort_its_group *)msi_parent->node_data;
674
675 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
676 if (!iort_fwnode)
677 return NULL;
678
679 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
680}
681
682void acpi_configure_pmsi_domain(struct device *dev)
683{
684 struct irq_domain *msi_domain;
685
686 msi_domain = iort_get_platform_device_domain(dev);
687 if (msi_domain)
688 dev_set_msi_domain(dev, msi_domain);
689}
690
Robin Murphybc8648d2017-08-04 17:42:06 +0100691static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
692 void *data)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000693{
694 u32 *rid = data;
695
696 *rid = alias;
697 return 0;
698}
699
700static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
701 struct fwnode_handle *fwnode,
702 const struct iommu_ops *ops)
703{
704 int ret = iommu_fwspec_init(dev, fwnode, ops);
705
706 if (!ret)
707 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
708
709 return ret;
710}
711
Lorenzo Pieralisi1d9029d2017-04-10 16:50:59 +0530712static inline bool iort_iommu_driver_enabled(u8 type)
713{
714 switch (type) {
715 case ACPI_IORT_NODE_SMMU_V3:
716 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
717 case ACPI_IORT_NODE_SMMU:
718 return IS_BUILTIN(CONFIG_ARM_SMMU);
719 default:
720 pr_warn("IORT node type %u does not describe an SMMU\n", type);
721 return false;
722 }
723}
724
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100725#ifdef CONFIG_IOMMU_API
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100726static inline const struct iommu_ops *iort_fwspec_iommu_ops(
727 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100728{
729 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
730}
731
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100732static inline int iort_add_device_replay(const struct iommu_ops *ops,
733 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100734{
735 int err = 0;
736
Robin Murphybc8648d2017-08-04 17:42:06 +0100737 if (ops->add_device && dev->bus && !dev->iommu_group)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100738 err = ops->add_device(dev);
739
740 return err;
741}
742#else
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100743static inline const struct iommu_ops *iort_fwspec_iommu_ops(
744 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100745{ return NULL; }
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100746static inline int iort_add_device_replay(const struct iommu_ops *ops,
747 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100748{ return 0; }
749#endif
750
Robin Murphybc8648d2017-08-04 17:42:06 +0100751static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
752 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000753{
Robin Murphybc8648d2017-08-04 17:42:06 +0100754 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000755 struct fwnode_handle *iort_fwnode;
756
Robin Murphybc8648d2017-08-04 17:42:06 +0100757 if (!node)
758 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000759
Robin Murphybc8648d2017-08-04 17:42:06 +0100760 iort_fwnode = iort_get_fwnode(node);
761 if (!iort_fwnode)
762 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000763
Robin Murphybc8648d2017-08-04 17:42:06 +0100764 /*
765 * If the ops look-up fails, this means that either
766 * the SMMU drivers have not been probed yet or that
767 * the SMMU drivers are not built in the kernel;
768 * Depending on whether the SMMU drivers are built-in
769 * in the kernel or not, defer the IOMMU configuration
770 * or just abort it.
771 */
772 ops = iommu_ops_from_fwnode(iort_fwnode);
773 if (!ops)
774 return iort_iommu_driver_enabled(node->type) ?
775 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000776
Robin Murphybc8648d2017-08-04 17:42:06 +0100777 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
778}
779
780struct iort_pci_alias_info {
781 struct device *dev;
782 struct acpi_iort_node *node;
783};
784
785static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
786{
787 struct iort_pci_alias_info *info = data;
788 struct acpi_iort_node *parent;
789 u32 streamid;
790
791 parent = iort_node_map_id(info->node, alias, &streamid,
792 IORT_IOMMU_TYPE);
793 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000794}
795
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100796static int nc_dma_get_range(struct device *dev, u64 *size)
797{
798 struct acpi_iort_node *node;
799 struct acpi_iort_named_component *ncomp;
800
801 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
802 iort_match_node_callback, dev);
803 if (!node)
804 return -ENODEV;
805
806 ncomp = (struct acpi_iort_named_component *)node->node_data;
807
808 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
809 1ULL<<ncomp->memory_address_limit;
810
811 return 0;
812}
813
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000814/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100815 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000816 *
817 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100818 * @dma_addr: device DMA address result pointer
819 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000820 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100821void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000822{
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100823 u64 mask, dmaaddr = 0, size = 0, offset = 0;
824 int ret, msb;
825
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000826 /*
827 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
828 * setup the correct supported mask.
829 */
830 if (!dev->coherent_dma_mask)
831 dev->coherent_dma_mask = DMA_BIT_MASK(32);
832
833 /*
834 * Set it to coherent_dma_mask by default if the architecture
835 * code has not set it.
836 */
837 if (!dev->dma_mask)
838 dev->dma_mask = &dev->coherent_dma_mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100839
840 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
841
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100842 if (dev_is_pci(dev))
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100843 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100844 else
845 ret = nc_dma_get_range(dev, &size);
846
847 if (!ret) {
848 msb = fls64(dmaaddr + size - 1);
849 /*
850 * Round-up to the power-of-two mask or set
851 * the mask to the whole 64-bit address space
852 * in case the DMA region covers the full
853 * memory window.
854 */
855 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
856 /*
857 * Limit coherent and dma mask based on size
858 * retrieved from firmware.
859 */
860 dev->coherent_dma_mask = mask;
861 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100862 }
863
864 *dma_addr = dmaaddr;
865 *dma_size = size;
866
867 dev->dma_pfn_offset = PFN_DOWN(offset);
868 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000869}
870
871/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000872 * iort_iommu_configure - Set-up IOMMU configuration for a device.
873 *
874 * @dev: device to configure
875 *
876 * Returns: iommu_ops pointer on configuration success
877 * NULL on configuration failure
878 */
879const struct iommu_ops *iort_iommu_configure(struct device *dev)
880{
881 struct acpi_iort_node *node, *parent;
Robin Murphybc8648d2017-08-04 17:42:06 +0100882 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000883 u32 streamid = 0;
Robin Murphybc8648d2017-08-04 17:42:06 +0100884 int err = -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000885
Lorenzo Pieralisi4dac3212017-05-27 19:17:44 +0530886 /*
887 * If we already translated the fwspec there
888 * is nothing left to do, return the iommu_ops.
889 */
890 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
891 if (ops)
892 return ops;
893
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000894 if (dev_is_pci(dev)) {
895 struct pci_bus *bus = to_pci_dev(dev)->bus;
Robin Murphybc8648d2017-08-04 17:42:06 +0100896 struct iort_pci_alias_info info = { .dev = dev };
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000897
898 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
899 iort_match_node_callback, &bus->dev);
900 if (!node)
901 return NULL;
902
Robin Murphybc8648d2017-08-04 17:42:06 +0100903 info.node = node;
904 err = pci_for_each_dma_alias(to_pci_dev(dev),
905 iort_pci_iommu_init, &info);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000906 } else {
907 int i = 0;
908
909 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
910 iort_match_node_callback, dev);
911 if (!node)
912 return NULL;
913
Robin Murphybc8648d2017-08-04 17:42:06 +0100914 do {
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800915 parent = iort_node_map_platform_id(node, &streamid,
916 IORT_IOMMU_TYPE,
917 i++);
Robin Murphybc8648d2017-08-04 17:42:06 +0100918
919 if (parent)
920 err = iort_iommu_xlate(dev, parent, streamid);
921 } while (parent && !err);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000922 }
923
Sricharan R5a1bb632017-04-10 16:51:03 +0530924 /*
925 * If we have reason to believe the IOMMU driver missed the initial
926 * add_device callback for dev, replay it to get things in order.
927 */
Robin Murphybc8648d2017-08-04 17:42:06 +0100928 if (!err) {
Arnd Bergmann4d360372017-08-10 17:45:22 +0100929 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
Robin Murphybc8648d2017-08-04 17:42:06 +0100930 err = iort_add_device_replay(ops, dev);
931 }
Sricharan R5a1bb632017-04-10 16:51:03 +0530932
Sricharan R058f8c32017-05-27 19:17:42 +0530933 /* Ignore all other errors apart from EPROBE_DEFER */
Robin Murphybc8648d2017-08-04 17:42:06 +0100934 if (err == -EPROBE_DEFER) {
935 ops = ERR_PTR(err);
936 } else if (err) {
937 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
Sricharan R058f8c32017-05-27 19:17:42 +0530938 ops = NULL;
939 }
940
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000941 return ops;
942}
943
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000944static void __init acpi_iort_register_irq(int hwirq, const char *name,
945 int trigger,
946 struct resource *res)
947{
948 int irq = acpi_register_gsi(NULL, hwirq, trigger,
949 ACPI_ACTIVE_HIGH);
950
951 if (irq <= 0) {
952 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
953 name);
954 return;
955 }
956
957 res->start = irq;
958 res->end = irq;
959 res->flags = IORESOURCE_IRQ;
960 res->name = name;
961}
962
963static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
964{
965 struct acpi_iort_smmu_v3 *smmu;
966 /* Always present mem resource */
967 int num_res = 1;
968
969 /* Retrieve SMMUv3 specific data */
970 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
971
972 if (smmu->event_gsiv)
973 num_res++;
974
975 if (smmu->pri_gsiv)
976 num_res++;
977
978 if (smmu->gerr_gsiv)
979 num_res++;
980
981 if (smmu->sync_gsiv)
982 num_res++;
983
984 return num_res;
985}
986
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530987static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
988{
989 /*
990 * Cavium ThunderX2 implementation doesn't not support unique
991 * irq line. Use single irq line for all the SMMUv3 interrupts.
992 */
993 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
994 return false;
995
996 /*
997 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
998 * SPI numbers here.
999 */
1000 return smmu->event_gsiv == smmu->pri_gsiv &&
1001 smmu->event_gsiv == smmu->gerr_gsiv &&
1002 smmu->event_gsiv == smmu->sync_gsiv;
1003}
1004
Linu Cherian403e8c72017-06-22 17:35:36 +05301005static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
1006{
1007 /*
1008 * Override the size, for Cavium ThunderX2 implementation
1009 * which doesn't support the page 1 SMMU register space.
1010 */
1011 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
1012 return SZ_64K;
1013
1014 return SZ_128K;
1015}
1016
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001017static void __init arm_smmu_v3_init_resources(struct resource *res,
1018 struct acpi_iort_node *node)
1019{
1020 struct acpi_iort_smmu_v3 *smmu;
1021 int num_res = 0;
1022
1023 /* Retrieve SMMUv3 specific data */
1024 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1025
1026 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +05301027 res[num_res].end = smmu->base_address +
1028 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001029 res[num_res].flags = IORESOURCE_MEM;
1030
1031 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301032 if (arm_smmu_v3_is_combined_irq(smmu)) {
1033 if (smmu->event_gsiv)
1034 acpi_iort_register_irq(smmu->event_gsiv, "combined",
1035 ACPI_EDGE_SENSITIVE,
1036 &res[num_res++]);
1037 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001038
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301039 if (smmu->event_gsiv)
1040 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
1041 ACPI_EDGE_SENSITIVE,
1042 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001043
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301044 if (smmu->pri_gsiv)
1045 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
1046 ACPI_EDGE_SENSITIVE,
1047 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001048
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301049 if (smmu->gerr_gsiv)
1050 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
1051 ACPI_EDGE_SENSITIVE,
1052 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001053
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301054 if (smmu->sync_gsiv)
1055 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
1056 ACPI_EDGE_SENSITIVE,
1057 &res[num_res++]);
1058 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001059}
1060
1061static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
1062{
1063 struct acpi_iort_smmu_v3 *smmu;
1064
1065 /* Retrieve SMMUv3 specific data */
1066 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1067
1068 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
1069}
1070
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001071#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001072/*
1073 * set numa proximity domain for smmuv3 device
1074 */
1075static void __init arm_smmu_v3_set_proximity(struct device *dev,
1076 struct acpi_iort_node *node)
1077{
1078 struct acpi_iort_smmu_v3 *smmu;
1079
1080 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1081 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1082 set_dev_node(dev, acpi_map_pxm_to_node(smmu->pxm));
1083 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1084 smmu->base_address,
1085 smmu->pxm);
1086 }
1087}
1088#else
1089#define arm_smmu_v3_set_proximity NULL
1090#endif
1091
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001092static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1093{
1094 struct acpi_iort_smmu *smmu;
1095
1096 /* Retrieve SMMU specific data */
1097 smmu = (struct acpi_iort_smmu *)node->node_data;
1098
1099 /*
1100 * Only consider the global fault interrupt and ignore the
1101 * configuration access interrupt.
1102 *
1103 * MMIO address and global fault interrupt resources are always
1104 * present so add them to the context interrupt count as a static
1105 * value.
1106 */
1107 return smmu->context_interrupt_count + 2;
1108}
1109
1110static void __init arm_smmu_init_resources(struct resource *res,
1111 struct acpi_iort_node *node)
1112{
1113 struct acpi_iort_smmu *smmu;
1114 int i, hw_irq, trigger, num_res = 0;
1115 u64 *ctx_irq, *glb_irq;
1116
1117 /* Retrieve SMMU specific data */
1118 smmu = (struct acpi_iort_smmu *)node->node_data;
1119
1120 res[num_res].start = smmu->base_address;
1121 res[num_res].end = smmu->base_address + smmu->span - 1;
1122 res[num_res].flags = IORESOURCE_MEM;
1123 num_res++;
1124
1125 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1126 /* Global IRQs */
1127 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1128 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1129
1130 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1131 &res[num_res++]);
1132
1133 /* Context IRQs */
1134 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1135 for (i = 0; i < smmu->context_interrupt_count; i++) {
1136 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1137 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1138
1139 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1140 &res[num_res++]);
1141 }
1142}
1143
1144static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
1145{
1146 struct acpi_iort_smmu *smmu;
1147
1148 /* Retrieve SMMU specific data */
1149 smmu = (struct acpi_iort_smmu *)node->node_data;
1150
1151 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
1152}
1153
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001154struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001155 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001156 int (*dev_init)(struct acpi_iort_node *node);
1157 bool (*dev_is_coherent)(struct acpi_iort_node *node);
1158 int (*dev_count_resources)(struct acpi_iort_node *node);
1159 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001160 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001161 void (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001162 struct acpi_iort_node *node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001163};
1164
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001165static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001166 .name = "arm-smmu-v3",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001167 .dev_is_coherent = arm_smmu_v3_is_coherent,
1168 .dev_count_resources = arm_smmu_v3_count_resources,
1169 .dev_init_resources = arm_smmu_v3_init_resources,
1170 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001171};
1172
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001173static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001174 .name = "arm-smmu",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001175 .dev_is_coherent = arm_smmu_is_coherent,
1176 .dev_count_resources = arm_smmu_count_resources,
1177 .dev_init_resources = arm_smmu_init_resources
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001178};
1179
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001180static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001181 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001182{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001183 switch (node->type) {
1184 case ACPI_IORT_NODE_SMMU_V3:
1185 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001186 case ACPI_IORT_NODE_SMMU:
1187 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001188 default:
1189 return NULL;
1190 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001191}
1192
1193/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001194 * iort_add_platform_device() - Allocate a platform device for IORT node
1195 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001196 *
1197 * Returns: 0 on success, <0 failure
1198 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001199static int __init iort_add_platform_device(struct acpi_iort_node *node,
1200 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001201{
1202 struct fwnode_handle *fwnode;
1203 struct platform_device *pdev;
1204 struct resource *r;
1205 enum dev_dma_attr attr;
1206 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001207
1208 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1209 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001210 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001211
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001212 if (ops->dev_set_proximity)
1213 ops->dev_set_proximity(&pdev->dev, node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001214
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001215 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001216
1217 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1218 if (!r) {
1219 ret = -ENOMEM;
1220 goto dev_put;
1221 }
1222
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001223 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001224
1225 ret = platform_device_add_resources(pdev, r, count);
1226 /*
1227 * Resources are duplicated in platform_device_add_resources,
1228 * free their allocated memory
1229 */
1230 kfree(r);
1231
1232 if (ret)
1233 goto dev_put;
1234
1235 /*
1236 * Add a copy of IORT node pointer to platform_data to
1237 * be used to retrieve IORT data information.
1238 */
1239 ret = platform_device_add_data(pdev, &node, sizeof(node));
1240 if (ret)
1241 goto dev_put;
1242
1243 /*
1244 * We expect the dma masks to be equivalent for
1245 * all SMMUs set-ups
1246 */
1247 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1248
1249 fwnode = iort_get_fwnode(node);
1250
1251 if (!fwnode) {
1252 ret = -ENODEV;
1253 goto dev_put;
1254 }
1255
1256 pdev->dev.fwnode = fwnode;
1257
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001258 attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
1259 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001260
1261 /* Configure DMA for the page table walker */
1262 acpi_dma_configure(&pdev->dev, attr);
1263
1264 ret = platform_device_add(pdev);
1265 if (ret)
1266 goto dma_deconfigure;
1267
1268 return 0;
1269
1270dma_deconfigure:
1271 acpi_dma_deconfigure(&pdev->dev);
1272dev_put:
1273 platform_device_put(pdev);
1274
1275 return ret;
1276}
1277
1278static void __init iort_init_platform_devices(void)
1279{
1280 struct acpi_iort_node *iort_node, *iort_end;
1281 struct acpi_table_iort *iort;
1282 struct fwnode_handle *fwnode;
1283 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001284 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001285
1286 /*
1287 * iort_table and iort both point to the start of IORT table, but
1288 * have different struct types
1289 */
1290 iort = (struct acpi_table_iort *)iort_table;
1291
1292 /* Get the first IORT node */
1293 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1294 iort->node_offset);
1295 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1296 iort_table->length);
1297
1298 for (i = 0; i < iort->node_count; i++) {
1299 if (iort_node >= iort_end) {
1300 pr_err("iort node pointer overflows, bad table\n");
1301 return;
1302 }
1303
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001304 ops = iort_get_dev_cfg(iort_node);
1305 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001306 fwnode = acpi_alloc_fwnode_static();
1307 if (!fwnode)
1308 return;
1309
1310 iort_set_fwnode(iort_node, fwnode);
1311
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001312 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001313 if (ret) {
1314 iort_delete_fwnode(iort_node);
1315 acpi_free_fwnode_static(fwnode);
1316 return;
1317 }
1318 }
1319
1320 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1321 iort_node->length);
1322 }
1323}
1324
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001325void __init acpi_iort_init(void)
1326{
1327 acpi_status status;
1328
1329 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001330 if (ACPI_FAILURE(status)) {
1331 if (status != AE_NOT_FOUND) {
1332 const char *msg = acpi_format_exception(status);
1333
1334 pr_err("Failed to get table, %s\n", msg);
1335 }
1336
1337 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001338 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001339
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001340 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001341}