blob: 4ff57f2a0fd7e084d42938b265a723593b14c053 [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 ||
359 node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
Lorenzo Pieralisi030abd82017-01-05 17:32:16 +0000360 *id_out = map->output_base;
Lorenzo Pieralisi618f5352016-11-21 10:01:47 +0000361 return parent;
362 }
363 }
364
365 return NULL;
366}
367
Hanjun Guo697f6092017-03-07 20:40:03 +0800368static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node,
369 u32 id_in, u32 *id_out,
370 u8 type_mask)
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200371{
Hanjun Guo697f6092017-03-07 20:40:03 +0800372 u32 id = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200373
374 /* Parse the ID mapping tree to find specified node type */
375 while (node) {
376 struct acpi_iort_id_mapping *map;
377 int i;
378
Lorenzo Pieralisiea50b522016-11-21 10:01:46 +0000379 if (IORT_TYPE_MASK(node->type) & type_mask) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800380 if (id_out)
381 *id_out = id;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200382 return node;
383 }
384
385 if (!node->mapping_offset || !node->mapping_count)
386 goto fail_map;
387
388 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
389 node->mapping_offset);
390
391 /* Firmware bug! */
392 if (!map->output_reference) {
393 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
394 node, node->type);
395 goto fail_map;
396 }
397
Hanjun Guo697f6092017-03-07 20:40:03 +0800398 /* Do the ID translation */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200399 for (i = 0; i < node->mapping_count; i++, map++) {
Hanjun Guo697f6092017-03-07 20:40:03 +0800400 if (!iort_id_map(map, node->type, id, &id))
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200401 break;
402 }
403
404 if (i == node->mapping_count)
405 goto fail_map;
406
407 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
408 map->output_reference);
409 }
410
411fail_map:
Hanjun Guo697f6092017-03-07 20:40:03 +0800412 /* Map input ID to output ID unchanged on mapping failure */
413 if (id_out)
414 *id_out = id_in;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200415
416 return NULL;
417}
418
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100419static struct acpi_iort_node *iort_node_map_platform_id(
420 struct acpi_iort_node *node, u32 *id_out, u8 type_mask,
421 int index)
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800422{
423 struct acpi_iort_node *parent;
424 u32 id;
425
426 /* step 1: retrieve the initial dev id */
427 parent = iort_node_get_id(node, &id, index);
428 if (!parent)
429 return NULL;
430
431 /*
432 * optional step 2: map the initial dev id if its parent is not
433 * the target type we want, map it again for the use cases such
434 * as NC (named component) -> SMMU -> ITS. If the type is matched,
435 * return the initial dev id and its parent pointer directly.
436 */
437 if (!(IORT_TYPE_MASK(parent->type) & type_mask))
438 parent = iort_node_map_id(parent, id, id_out, type_mask);
439 else
440 if (id_out)
441 *id_out = id;
442
443 return parent;
444}
445
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200446static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
447{
448 struct pci_bus *pbus;
449
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800450 if (!dev_is_pci(dev)) {
451 struct acpi_iort_node *node;
452 /*
453 * scan iort_fwnode_list to see if it's an iort platform
454 * device (such as SMMU, PMCG),its iort node already cached
455 * and associated with fwnode when iort platform devices
456 * were initialized.
457 */
458 node = iort_get_iort_node(dev->fwnode);
459 if (node)
460 return node;
461
462 /*
463 * if not, then it should be a platform device defined in
464 * DSDT/SSDT (with Named Component node in IORT)
465 */
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200466 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
467 iort_match_node_callback, dev);
Hanjun Guo0a71d8b2017-10-13 15:09:47 +0800468 }
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200469
470 /* Find a PCI root bus */
471 pbus = to_pci_dev(dev)->bus;
472 while (!pci_is_root_bus(pbus))
473 pbus = pbus->parent;
474
475 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
476 iort_match_node_callback, &pbus->dev);
477}
478
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200479/**
480 * iort_msi_map_rid() - Map a MSI requester ID for a device
481 * @dev: The device for which the mapping is to be done.
482 * @req_id: The device requester ID.
483 *
484 * Returns: mapped MSI RID on success, input requester ID otherwise
485 */
486u32 iort_msi_map_rid(struct device *dev, u32 req_id)
487{
488 struct acpi_iort_node *node;
489 u32 dev_id;
490
491 node = iort_find_dev_node(dev);
492 if (!node)
493 return req_id;
494
Hanjun Guo697f6092017-03-07 20:40:03 +0800495 iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200496 return dev_id;
497}
498
499/**
Hanjun Guoae7c1832017-03-07 20:40:05 +0800500 * iort_pmsi_get_dev_id() - Get the device id for a device
501 * @dev: The device for which the mapping is to be done.
502 * @dev_id: The device ID found.
503 *
504 * Returns: 0 for successful find a dev id, -ENODEV on error
505 */
506int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
507{
508 int i;
509 struct acpi_iort_node *node;
510
511 node = iort_find_dev_node(dev);
512 if (!node)
513 return -ENODEV;
514
515 for (i = 0; i < node->mapping_count; i++) {
516 if (iort_node_map_platform_id(node, dev_id, IORT_MSI_TYPE, i))
517 return 0;
518 }
519
520 return -ENODEV;
521}
522
523/**
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200524 * iort_dev_find_its_id() - Find the ITS identifier for a device
525 * @dev: The device.
Hanjun Guo6cb6bf52017-03-07 20:39:57 +0800526 * @req_id: Device's requester ID
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200527 * @idx: Index of the ITS identifier list.
528 * @its_id: ITS identifier.
529 *
530 * Returns: 0 on success, appropriate error value otherwise
531 */
532static int iort_dev_find_its_id(struct device *dev, u32 req_id,
533 unsigned int idx, int *its_id)
534{
535 struct acpi_iort_its_group *its;
536 struct acpi_iort_node *node;
537
538 node = iort_find_dev_node(dev);
539 if (!node)
540 return -ENXIO;
541
Hanjun Guo697f6092017-03-07 20:40:03 +0800542 node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE);
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200543 if (!node)
544 return -ENXIO;
545
546 /* Move to ITS specific data */
547 its = (struct acpi_iort_its_group *)node->node_data;
548 if (idx > its->its_count) {
549 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
550 idx, its->its_count);
551 return -ENXIO;
552 }
553
554 *its_id = its->identifiers[idx];
555 return 0;
556}
557
558/**
559 * iort_get_device_domain() - Find MSI domain related to a device
560 * @dev: The device.
561 * @req_id: Requester ID for the device.
562 *
563 * Returns: the MSI domain for this device, NULL otherwise
564 */
565struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
566{
567 struct fwnode_handle *handle;
568 int its_id;
569
570 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
571 return NULL;
572
573 handle = iort_find_domain_token(its_id);
574 if (!handle)
575 return NULL;
576
577 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
578}
579
Hanjun Guod4f54a12017-03-07 20:40:06 +0800580/**
581 * iort_get_platform_device_domain() - Find MSI domain related to a
582 * platform device
583 * @dev: the dev pointer associated with the platform device
584 *
585 * Returns: the MSI domain for this device, NULL otherwise
586 */
587static struct irq_domain *iort_get_platform_device_domain(struct device *dev)
588{
589 struct acpi_iort_node *node, *msi_parent;
590 struct fwnode_handle *iort_fwnode;
591 struct acpi_iort_its_group *its;
592 int i;
593
594 /* find its associated iort node */
595 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
596 iort_match_node_callback, dev);
597 if (!node)
598 return NULL;
599
600 /* then find its msi parent node */
601 for (i = 0; i < node->mapping_count; i++) {
602 msi_parent = iort_node_map_platform_id(node, NULL,
603 IORT_MSI_TYPE, i);
604 if (msi_parent)
605 break;
606 }
607
608 if (!msi_parent)
609 return NULL;
610
611 /* Move to ITS specific data */
612 its = (struct acpi_iort_its_group *)msi_parent->node_data;
613
614 iort_fwnode = iort_find_domain_token(its->identifiers[0]);
615 if (!iort_fwnode)
616 return NULL;
617
618 return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI);
619}
620
621void acpi_configure_pmsi_domain(struct device *dev)
622{
623 struct irq_domain *msi_domain;
624
625 msi_domain = iort_get_platform_device_domain(dev);
626 if (msi_domain)
627 dev_set_msi_domain(dev, msi_domain);
628}
629
Robin Murphybc8648d2017-08-04 17:42:06 +0100630static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias,
631 void *data)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000632{
633 u32 *rid = data;
634
635 *rid = alias;
636 return 0;
637}
638
639static int arm_smmu_iort_xlate(struct device *dev, u32 streamid,
640 struct fwnode_handle *fwnode,
641 const struct iommu_ops *ops)
642{
643 int ret = iommu_fwspec_init(dev, fwnode, ops);
644
645 if (!ret)
646 ret = iommu_fwspec_add_ids(dev, &streamid, 1);
647
648 return ret;
649}
650
Lorenzo Pieralisi1d9029d2017-04-10 16:50:59 +0530651static inline bool iort_iommu_driver_enabled(u8 type)
652{
653 switch (type) {
654 case ACPI_IORT_NODE_SMMU_V3:
655 return IS_BUILTIN(CONFIG_ARM_SMMU_V3);
656 case ACPI_IORT_NODE_SMMU:
657 return IS_BUILTIN(CONFIG_ARM_SMMU);
658 default:
659 pr_warn("IORT node type %u does not describe an SMMU\n", type);
660 return false;
661 }
662}
663
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100664#ifdef CONFIG_IOMMU_API
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100665static inline const struct iommu_ops *iort_fwspec_iommu_ops(
666 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100667{
668 return (fwspec && fwspec->ops) ? fwspec->ops : NULL;
669}
670
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100671static inline int iort_add_device_replay(const struct iommu_ops *ops,
672 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100673{
674 int err = 0;
675
Robin Murphybc8648d2017-08-04 17:42:06 +0100676 if (ops->add_device && dev->bus && !dev->iommu_group)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100677 err = ops->add_device(dev);
678
679 return err;
680}
681#else
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100682static inline const struct iommu_ops *iort_fwspec_iommu_ops(
683 struct iommu_fwspec *fwspec)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100684{ return NULL; }
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +0100685static inline int iort_add_device_replay(const struct iommu_ops *ops,
686 struct device *dev)
Lorenzo Pieralisid49f2de2017-04-28 16:59:49 +0100687{ return 0; }
688#endif
689
Robin Murphybc8648d2017-08-04 17:42:06 +0100690static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node,
691 u32 streamid)
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000692{
Robin Murphybc8648d2017-08-04 17:42:06 +0100693 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000694 struct fwnode_handle *iort_fwnode;
695
Robin Murphybc8648d2017-08-04 17:42:06 +0100696 if (!node)
697 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000698
Robin Murphybc8648d2017-08-04 17:42:06 +0100699 iort_fwnode = iort_get_fwnode(node);
700 if (!iort_fwnode)
701 return -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000702
Robin Murphybc8648d2017-08-04 17:42:06 +0100703 /*
704 * If the ops look-up fails, this means that either
705 * the SMMU drivers have not been probed yet or that
706 * the SMMU drivers are not built in the kernel;
707 * Depending on whether the SMMU drivers are built-in
708 * in the kernel or not, defer the IOMMU configuration
709 * or just abort it.
710 */
711 ops = iommu_ops_from_fwnode(iort_fwnode);
712 if (!ops)
713 return iort_iommu_driver_enabled(node->type) ?
714 -EPROBE_DEFER : -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000715
Robin Murphybc8648d2017-08-04 17:42:06 +0100716 return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops);
717}
718
719struct iort_pci_alias_info {
720 struct device *dev;
721 struct acpi_iort_node *node;
722};
723
724static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
725{
726 struct iort_pci_alias_info *info = data;
727 struct acpi_iort_node *parent;
728 u32 streamid;
729
730 parent = iort_node_map_id(info->node, alias, &streamid,
731 IORT_IOMMU_TYPE);
732 return iort_iommu_xlate(info->dev, parent, streamid);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000733}
734
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100735static int nc_dma_get_range(struct device *dev, u64 *size)
736{
737 struct acpi_iort_node *node;
738 struct acpi_iort_named_component *ncomp;
739
740 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
741 iort_match_node_callback, dev);
742 if (!node)
743 return -ENODEV;
744
745 ncomp = (struct acpi_iort_named_component *)node->node_data;
746
747 *size = ncomp->memory_address_limit >= 64 ? U64_MAX :
748 1ULL<<ncomp->memory_address_limit;
749
750 return 0;
751}
752
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000753/**
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100754 * iort_dma_setup() - Set-up device DMA parameters.
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000755 *
756 * @dev: device to configure
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100757 * @dma_addr: device DMA address result pointer
758 * @size: DMA range size result pointer
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000759 */
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100760void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000761{
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100762 u64 mask, dmaaddr = 0, size = 0, offset = 0;
763 int ret, msb;
764
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000765 /*
766 * Set default coherent_dma_mask to 32 bit. Drivers are expected to
767 * setup the correct supported mask.
768 */
769 if (!dev->coherent_dma_mask)
770 dev->coherent_dma_mask = DMA_BIT_MASK(32);
771
772 /*
773 * Set it to coherent_dma_mask by default if the architecture
774 * code has not set it.
775 */
776 if (!dev->dma_mask)
777 dev->dma_mask = &dev->coherent_dma_mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100778
779 size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
780
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100781 if (dev_is_pci(dev))
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100782 ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
Lorenzo Pieralisi10d8ab22017-08-03 13:32:39 +0100783 else
784 ret = nc_dma_get_range(dev, &size);
785
786 if (!ret) {
787 msb = fls64(dmaaddr + size - 1);
788 /*
789 * Round-up to the power-of-two mask or set
790 * the mask to the whole 64-bit address space
791 * in case the DMA region covers the full
792 * memory window.
793 */
794 mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
795 /*
796 * Limit coherent and dma mask based on size
797 * retrieved from firmware.
798 */
799 dev->coherent_dma_mask = mask;
800 *dev->dma_mask = mask;
Lorenzo Pieralisi7ad42632017-08-07 11:29:49 +0100801 }
802
803 *dma_addr = dmaaddr;
804 *dma_size = size;
805
806 dev->dma_pfn_offset = PFN_DOWN(offset);
807 dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset);
Lorenzo Pieralisi18b709b2016-12-06 14:20:11 +0000808}
809
810/**
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000811 * iort_iommu_configure - Set-up IOMMU configuration for a device.
812 *
813 * @dev: device to configure
814 *
815 * Returns: iommu_ops pointer on configuration success
816 * NULL on configuration failure
817 */
818const struct iommu_ops *iort_iommu_configure(struct device *dev)
819{
820 struct acpi_iort_node *node, *parent;
Robin Murphybc8648d2017-08-04 17:42:06 +0100821 const struct iommu_ops *ops;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000822 u32 streamid = 0;
Robin Murphybc8648d2017-08-04 17:42:06 +0100823 int err = -ENODEV;
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000824
Lorenzo Pieralisi4dac3212017-05-27 19:17:44 +0530825 /*
826 * If we already translated the fwspec there
827 * is nothing left to do, return the iommu_ops.
828 */
829 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
830 if (ops)
831 return ops;
832
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000833 if (dev_is_pci(dev)) {
834 struct pci_bus *bus = to_pci_dev(dev)->bus;
Robin Murphybc8648d2017-08-04 17:42:06 +0100835 struct iort_pci_alias_info info = { .dev = dev };
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000836
837 node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
838 iort_match_node_callback, &bus->dev);
839 if (!node)
840 return NULL;
841
Robin Murphybc8648d2017-08-04 17:42:06 +0100842 info.node = node;
843 err = pci_for_each_dma_alias(to_pci_dev(dev),
844 iort_pci_iommu_init, &info);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000845 } else {
846 int i = 0;
847
848 node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
849 iort_match_node_callback, dev);
850 if (!node)
851 return NULL;
852
Robin Murphybc8648d2017-08-04 17:42:06 +0100853 do {
Hanjun Guo8ca4f1d2017-03-07 20:40:04 +0800854 parent = iort_node_map_platform_id(node, &streamid,
855 IORT_IOMMU_TYPE,
856 i++);
Robin Murphybc8648d2017-08-04 17:42:06 +0100857
858 if (parent)
859 err = iort_iommu_xlate(dev, parent, streamid);
860 } while (parent && !err);
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000861 }
862
Sricharan R5a1bb632017-04-10 16:51:03 +0530863 /*
864 * If we have reason to believe the IOMMU driver missed the initial
865 * add_device callback for dev, replay it to get things in order.
866 */
Robin Murphybc8648d2017-08-04 17:42:06 +0100867 if (!err) {
Arnd Bergmann4d360372017-08-10 17:45:22 +0100868 ops = iort_fwspec_iommu_ops(dev->iommu_fwspec);
Robin Murphybc8648d2017-08-04 17:42:06 +0100869 err = iort_add_device_replay(ops, dev);
870 }
Sricharan R5a1bb632017-04-10 16:51:03 +0530871
Sricharan R058f8c32017-05-27 19:17:42 +0530872 /* Ignore all other errors apart from EPROBE_DEFER */
Robin Murphybc8648d2017-08-04 17:42:06 +0100873 if (err == -EPROBE_DEFER) {
874 ops = ERR_PTR(err);
875 } else if (err) {
876 dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);
Sricharan R058f8c32017-05-27 19:17:42 +0530877 ops = NULL;
878 }
879
Lorenzo Pieralisi643b8e42016-11-21 10:01:48 +0000880 return ops;
881}
882
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000883static void __init acpi_iort_register_irq(int hwirq, const char *name,
884 int trigger,
885 struct resource *res)
886{
887 int irq = acpi_register_gsi(NULL, hwirq, trigger,
888 ACPI_ACTIVE_HIGH);
889
890 if (irq <= 0) {
891 pr_err("could not register gsi hwirq %d name [%s]\n", hwirq,
892 name);
893 return;
894 }
895
896 res->start = irq;
897 res->end = irq;
898 res->flags = IORESOURCE_IRQ;
899 res->name = name;
900}
901
902static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node)
903{
904 struct acpi_iort_smmu_v3 *smmu;
905 /* Always present mem resource */
906 int num_res = 1;
907
908 /* Retrieve SMMUv3 specific data */
909 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
910
911 if (smmu->event_gsiv)
912 num_res++;
913
914 if (smmu->pri_gsiv)
915 num_res++;
916
917 if (smmu->gerr_gsiv)
918 num_res++;
919
920 if (smmu->sync_gsiv)
921 num_res++;
922
923 return num_res;
924}
925
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530926static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu)
927{
928 /*
929 * Cavium ThunderX2 implementation doesn't not support unique
930 * irq line. Use single irq line for all the SMMUv3 interrupts.
931 */
932 if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
933 return false;
934
935 /*
936 * ThunderX2 doesn't support MSIs from the SMMU, so we're checking
937 * SPI numbers here.
938 */
939 return smmu->event_gsiv == smmu->pri_gsiv &&
940 smmu->event_gsiv == smmu->gerr_gsiv &&
941 smmu->event_gsiv == smmu->sync_gsiv;
942}
943
Linu Cherian403e8c72017-06-22 17:35:36 +0530944static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu)
945{
946 /*
947 * Override the size, for Cavium ThunderX2 implementation
948 * which doesn't support the page 1 SMMU register space.
949 */
950 if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX)
951 return SZ_64K;
952
953 return SZ_128K;
954}
955
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000956static void __init arm_smmu_v3_init_resources(struct resource *res,
957 struct acpi_iort_node *node)
958{
959 struct acpi_iort_smmu_v3 *smmu;
960 int num_res = 0;
961
962 /* Retrieve SMMUv3 specific data */
963 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
964
965 res[num_res].start = smmu->base_address;
Linu Cherian403e8c72017-06-22 17:35:36 +0530966 res[num_res].end = smmu->base_address +
967 arm_smmu_v3_resource_size(smmu) - 1;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000968 res[num_res].flags = IORESOURCE_MEM;
969
970 num_res++;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530971 if (arm_smmu_v3_is_combined_irq(smmu)) {
972 if (smmu->event_gsiv)
973 acpi_iort_register_irq(smmu->event_gsiv, "combined",
974 ACPI_EDGE_SENSITIVE,
975 &res[num_res++]);
976 } else {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000977
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530978 if (smmu->event_gsiv)
979 acpi_iort_register_irq(smmu->event_gsiv, "eventq",
980 ACPI_EDGE_SENSITIVE,
981 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000982
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530983 if (smmu->pri_gsiv)
984 acpi_iort_register_irq(smmu->pri_gsiv, "priq",
985 ACPI_EDGE_SENSITIVE,
986 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000987
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530988 if (smmu->gerr_gsiv)
989 acpi_iort_register_irq(smmu->gerr_gsiv, "gerror",
990 ACPI_EDGE_SENSITIVE,
991 &res[num_res++]);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000992
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530993 if (smmu->sync_gsiv)
994 acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync",
995 ACPI_EDGE_SENSITIVE,
996 &res[num_res++]);
997 }
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +0000998}
999
1000static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node)
1001{
1002 struct acpi_iort_smmu_v3 *smmu;
1003
1004 /* Retrieve SMMUv3 specific data */
1005 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1006
1007 return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE;
1008}
1009
Lorenzo Pieralisi75808132017-09-28 13:57:10 +01001010#if defined(CONFIG_ACPI_NUMA)
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001011/*
1012 * set numa proximity domain for smmuv3 device
1013 */
1014static void __init arm_smmu_v3_set_proximity(struct device *dev,
1015 struct acpi_iort_node *node)
1016{
1017 struct acpi_iort_smmu_v3 *smmu;
1018
1019 smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
1020 if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) {
1021 set_dev_node(dev, acpi_map_pxm_to_node(smmu->pxm));
1022 pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n",
1023 smmu->base_address,
1024 smmu->pxm);
1025 }
1026}
1027#else
1028#define arm_smmu_v3_set_proximity NULL
1029#endif
1030
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001031static int __init arm_smmu_count_resources(struct acpi_iort_node *node)
1032{
1033 struct acpi_iort_smmu *smmu;
1034
1035 /* Retrieve SMMU specific data */
1036 smmu = (struct acpi_iort_smmu *)node->node_data;
1037
1038 /*
1039 * Only consider the global fault interrupt and ignore the
1040 * configuration access interrupt.
1041 *
1042 * MMIO address and global fault interrupt resources are always
1043 * present so add them to the context interrupt count as a static
1044 * value.
1045 */
1046 return smmu->context_interrupt_count + 2;
1047}
1048
1049static void __init arm_smmu_init_resources(struct resource *res,
1050 struct acpi_iort_node *node)
1051{
1052 struct acpi_iort_smmu *smmu;
1053 int i, hw_irq, trigger, num_res = 0;
1054 u64 *ctx_irq, *glb_irq;
1055
1056 /* Retrieve SMMU specific data */
1057 smmu = (struct acpi_iort_smmu *)node->node_data;
1058
1059 res[num_res].start = smmu->base_address;
1060 res[num_res].end = smmu->base_address + smmu->span - 1;
1061 res[num_res].flags = IORESOURCE_MEM;
1062 num_res++;
1063
1064 glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset);
1065 /* Global IRQs */
1066 hw_irq = IORT_IRQ_MASK(glb_irq[0]);
1067 trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]);
1068
1069 acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger,
1070 &res[num_res++]);
1071
1072 /* Context IRQs */
1073 ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset);
1074 for (i = 0; i < smmu->context_interrupt_count; i++) {
1075 hw_irq = IORT_IRQ_MASK(ctx_irq[i]);
1076 trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]);
1077
1078 acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger,
1079 &res[num_res++]);
1080 }
1081}
1082
1083static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node)
1084{
1085 struct acpi_iort_smmu *smmu;
1086
1087 /* Retrieve SMMU specific data */
1088 smmu = (struct acpi_iort_smmu *)node->node_data;
1089
1090 return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK;
1091}
1092
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001093struct iort_dev_config {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001094 const char *name;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001095 int (*dev_init)(struct acpi_iort_node *node);
1096 bool (*dev_is_coherent)(struct acpi_iort_node *node);
1097 int (*dev_count_resources)(struct acpi_iort_node *node);
1098 void (*dev_init_resources)(struct resource *res,
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001099 struct acpi_iort_node *node);
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001100 void (*dev_set_proximity)(struct device *dev,
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001101 struct acpi_iort_node *node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001102};
1103
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001104static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = {
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001105 .name = "arm-smmu-v3",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001106 .dev_is_coherent = arm_smmu_v3_is_coherent,
1107 .dev_count_resources = arm_smmu_v3_count_resources,
1108 .dev_init_resources = arm_smmu_v3_init_resources,
1109 .dev_set_proximity = arm_smmu_v3_set_proximity,
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001110};
1111
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001112static const struct iort_dev_config iort_arm_smmu_cfg __initconst = {
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001113 .name = "arm-smmu",
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001114 .dev_is_coherent = arm_smmu_is_coherent,
1115 .dev_count_resources = arm_smmu_count_resources,
1116 .dev_init_resources = arm_smmu_init_resources
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001117};
1118
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001119static __init const struct iort_dev_config *iort_get_dev_cfg(
Lorenzo Pieralisie3d49392017-09-28 14:03:33 +01001120 struct acpi_iort_node *node)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001121{
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001122 switch (node->type) {
1123 case ACPI_IORT_NODE_SMMU_V3:
1124 return &iort_arm_smmu_v3_cfg;
Lorenzo Pieralisid6fcd3b2016-11-21 10:01:45 +00001125 case ACPI_IORT_NODE_SMMU:
1126 return &iort_arm_smmu_cfg;
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00001127 default:
1128 return NULL;
1129 }
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001130}
1131
1132/**
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001133 * iort_add_platform_device() - Allocate a platform device for IORT node
1134 * @node: Pointer to device ACPI IORT node
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001135 *
1136 * Returns: 0 on success, <0 failure
1137 */
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001138static int __init iort_add_platform_device(struct acpi_iort_node *node,
1139 const struct iort_dev_config *ops)
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001140{
1141 struct fwnode_handle *fwnode;
1142 struct platform_device *pdev;
1143 struct resource *r;
1144 enum dev_dma_attr attr;
1145 int ret, count;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001146
1147 pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO);
1148 if (!pdev)
Dan Carpenter5e5afa62017-01-17 16:36:23 +03001149 return -ENOMEM;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001150
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001151 if (ops->dev_set_proximity)
1152 ops->dev_set_proximity(&pdev->dev, node);
Ganapatrao Kulkarni5fe0ce32017-08-02 10:58:25 -07001153
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001154 count = ops->dev_count_resources(node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001155
1156 r = kcalloc(count, sizeof(*r), GFP_KERNEL);
1157 if (!r) {
1158 ret = -ENOMEM;
1159 goto dev_put;
1160 }
1161
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001162 ops->dev_init_resources(r, node);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001163
1164 ret = platform_device_add_resources(pdev, r, count);
1165 /*
1166 * Resources are duplicated in platform_device_add_resources,
1167 * free their allocated memory
1168 */
1169 kfree(r);
1170
1171 if (ret)
1172 goto dev_put;
1173
1174 /*
1175 * Add a copy of IORT node pointer to platform_data to
1176 * be used to retrieve IORT data information.
1177 */
1178 ret = platform_device_add_data(pdev, &node, sizeof(node));
1179 if (ret)
1180 goto dev_put;
1181
1182 /*
1183 * We expect the dma masks to be equivalent for
1184 * all SMMUs set-ups
1185 */
1186 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
1187
1188 fwnode = iort_get_fwnode(node);
1189
1190 if (!fwnode) {
1191 ret = -ENODEV;
1192 goto dev_put;
1193 }
1194
1195 pdev->dev.fwnode = fwnode;
1196
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001197 attr = ops->dev_is_coherent && ops->dev_is_coherent(node) ?
1198 DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001199
1200 /* Configure DMA for the page table walker */
1201 acpi_dma_configure(&pdev->dev, attr);
1202
1203 ret = platform_device_add(pdev);
1204 if (ret)
1205 goto dma_deconfigure;
1206
1207 return 0;
1208
1209dma_deconfigure:
1210 acpi_dma_deconfigure(&pdev->dev);
1211dev_put:
1212 platform_device_put(pdev);
1213
1214 return ret;
1215}
1216
1217static void __init iort_init_platform_devices(void)
1218{
1219 struct acpi_iort_node *iort_node, *iort_end;
1220 struct acpi_table_iort *iort;
1221 struct fwnode_handle *fwnode;
1222 int i, ret;
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001223 const struct iort_dev_config *ops;
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001224
1225 /*
1226 * iort_table and iort both point to the start of IORT table, but
1227 * have different struct types
1228 */
1229 iort = (struct acpi_table_iort *)iort_table;
1230
1231 /* Get the first IORT node */
1232 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1233 iort->node_offset);
1234 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort,
1235 iort_table->length);
1236
1237 for (i = 0; i < iort->node_count; i++) {
1238 if (iort_node >= iort_end) {
1239 pr_err("iort node pointer overflows, bad table\n");
1240 return;
1241 }
1242
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001243 ops = iort_get_dev_cfg(iort_node);
1244 if (ops) {
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001245 fwnode = acpi_alloc_fwnode_static();
1246 if (!fwnode)
1247 return;
1248
1249 iort_set_fwnode(iort_node, fwnode);
1250
Lorenzo Pieralisi896dd2c2017-09-20 17:03:58 +01001251 ret = iort_add_platform_device(iort_node, ops);
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001252 if (ret) {
1253 iort_delete_fwnode(iort_node);
1254 acpi_free_fwnode_static(fwnode);
1255 return;
1256 }
1257 }
1258
1259 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
1260 iort_node->length);
1261 }
1262}
1263
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001264void __init acpi_iort_init(void)
1265{
1266 acpi_status status;
1267
1268 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001269 if (ACPI_FAILURE(status)) {
1270 if (status != AE_NOT_FOUND) {
1271 const char *msg = acpi_format_exception(status);
1272
1273 pr_err("Failed to get table, %s\n", msg);
1274 }
1275
1276 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001277 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +00001278
Lorenzo Pieralisi846f0e92016-11-21 10:01:41 +00001279 iort_init_platform_devices();
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +02001280}