blob: 4bb6acbf881f8009ddb6b435359de095f60e3266 [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>
22#include <linux/kernel.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000023#include <linux/list.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020024#include <linux/pci.h>
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000025#include <linux/slab.h>
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +020026
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +020027struct iort_its_msi_chip {
28 struct list_head list;
29 struct fwnode_handle *fw_node;
30 u32 translation_id;
31};
32
Lorenzo Pieralisi7936df92016-11-21 10:01:35 +000033struct iort_fwnode {
34 struct list_head list;
35 struct acpi_iort_node *iort_node;
36 struct fwnode_handle *fwnode;
37};
38static LIST_HEAD(iort_fwnode_list);
39static DEFINE_SPINLOCK(iort_fwnode_lock);
40
41/**
42 * iort_set_fwnode() - Create iort_fwnode and use it to register
43 * iommu data in the iort_fwnode_list
44 *
45 * @node: IORT table node associated with the IOMMU
46 * @fwnode: fwnode associated with the IORT node
47 *
48 * Returns: 0 on success
49 * <0 on failure
50 */
51static inline int iort_set_fwnode(struct acpi_iort_node *iort_node,
52 struct fwnode_handle *fwnode)
53{
54 struct iort_fwnode *np;
55
56 np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC);
57
58 if (WARN_ON(!np))
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&np->list);
62 np->iort_node = iort_node;
63 np->fwnode = fwnode;
64
65 spin_lock(&iort_fwnode_lock);
66 list_add_tail(&np->list, &iort_fwnode_list);
67 spin_unlock(&iort_fwnode_lock);
68
69 return 0;
70}
71
72/**
73 * iort_get_fwnode() - Retrieve fwnode associated with an IORT node
74 *
75 * @node: IORT table node to be looked-up
76 *
77 * Returns: fwnode_handle pointer on success, NULL on failure
78 */
79static inline
80struct fwnode_handle *iort_get_fwnode(struct acpi_iort_node *node)
81{
82 struct iort_fwnode *curr;
83 struct fwnode_handle *fwnode = NULL;
84
85 spin_lock(&iort_fwnode_lock);
86 list_for_each_entry(curr, &iort_fwnode_list, list) {
87 if (curr->iort_node == node) {
88 fwnode = curr->fwnode;
89 break;
90 }
91 }
92 spin_unlock(&iort_fwnode_lock);
93
94 return fwnode;
95}
96
97/**
98 * iort_delete_fwnode() - Delete fwnode associated with an IORT node
99 *
100 * @node: IORT table node associated with fwnode to delete
101 */
102static inline void iort_delete_fwnode(struct acpi_iort_node *node)
103{
104 struct iort_fwnode *curr, *tmp;
105
106 spin_lock(&iort_fwnode_lock);
107 list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) {
108 if (curr->iort_node == node) {
109 list_del(&curr->list);
110 kfree(curr);
111 break;
112 }
113 }
114 spin_unlock(&iort_fwnode_lock);
115}
116
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200117typedef acpi_status (*iort_find_node_callback)
118 (struct acpi_iort_node *node, void *context);
119
120/* Root pointer to the mapped IORT table */
121static struct acpi_table_header *iort_table;
122
123static LIST_HEAD(iort_msi_chip_list);
124static DEFINE_SPINLOCK(iort_msi_chip_lock);
125
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200126/**
127 * iort_register_domain_token() - register domain token and related ITS ID
128 * to the list from where we can get it back later on.
129 * @trans_id: ITS ID.
130 * @fw_node: Domain token.
131 *
132 * Returns: 0 on success, -ENOMEM if no memory when allocating list element
133 */
134int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node)
135{
136 struct iort_its_msi_chip *its_msi_chip;
137
138 its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL);
139 if (!its_msi_chip)
140 return -ENOMEM;
141
142 its_msi_chip->fw_node = fw_node;
143 its_msi_chip->translation_id = trans_id;
144
145 spin_lock(&iort_msi_chip_lock);
146 list_add(&its_msi_chip->list, &iort_msi_chip_list);
147 spin_unlock(&iort_msi_chip_lock);
148
149 return 0;
150}
151
152/**
153 * iort_deregister_domain_token() - Deregister domain token based on ITS ID
154 * @trans_id: ITS ID.
155 *
156 * Returns: none.
157 */
158void iort_deregister_domain_token(int trans_id)
159{
160 struct iort_its_msi_chip *its_msi_chip, *t;
161
162 spin_lock(&iort_msi_chip_lock);
163 list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) {
164 if (its_msi_chip->translation_id == trans_id) {
165 list_del(&its_msi_chip->list);
166 kfree(its_msi_chip);
167 break;
168 }
169 }
170 spin_unlock(&iort_msi_chip_lock);
171}
172
173/**
174 * iort_find_domain_token() - Find domain token based on given ITS ID
175 * @trans_id: ITS ID.
176 *
177 * Returns: domain token when find on the list, NULL otherwise
178 */
179struct fwnode_handle *iort_find_domain_token(int trans_id)
180{
181 struct fwnode_handle *fw_node = NULL;
182 struct iort_its_msi_chip *its_msi_chip;
183
184 spin_lock(&iort_msi_chip_lock);
185 list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) {
186 if (its_msi_chip->translation_id == trans_id) {
187 fw_node = its_msi_chip->fw_node;
188 break;
189 }
190 }
191 spin_unlock(&iort_msi_chip_lock);
192
193 return fw_node;
194}
195
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200196static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type,
197 iort_find_node_callback callback,
198 void *context)
199{
200 struct acpi_iort_node *iort_node, *iort_end;
201 struct acpi_table_iort *iort;
202 int i;
203
204 if (!iort_table)
205 return NULL;
206
207 /* Get the first IORT node */
208 iort = (struct acpi_table_iort *)iort_table;
209 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort,
210 iort->node_offset);
211 iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
212 iort_table->length);
213
214 for (i = 0; i < iort->node_count; i++) {
215 if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND,
216 "IORT node pointer overflows, bad table!\n"))
217 return NULL;
218
219 if (iort_node->type == type &&
220 ACPI_SUCCESS(callback(iort_node, context)))
221 return iort_node;
222
223 iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node,
224 iort_node->length);
225 }
226
227 return NULL;
228}
229
Lorenzo Pieralisibdca0c02016-11-21 10:01:40 +0000230static acpi_status
231iort_match_type_callback(struct acpi_iort_node *node, void *context)
232{
233 return AE_OK;
234}
235
236bool iort_node_match(u8 type)
237{
238 struct acpi_iort_node *node;
239
240 node = iort_scan_node(type, iort_match_type_callback, NULL);
241
242 return node != NULL;
243}
244
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200245static acpi_status iort_match_node_callback(struct acpi_iort_node *node,
246 void *context)
247{
248 struct device *dev = context;
249 acpi_status status;
250
251 if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) {
252 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
253 struct acpi_device *adev = to_acpi_device_node(dev->fwnode);
254 struct acpi_iort_named_component *ncomp;
255
256 if (!adev) {
257 status = AE_NOT_FOUND;
258 goto out;
259 }
260
261 status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf);
262 if (ACPI_FAILURE(status)) {
263 dev_warn(dev, "Can't get device full path name\n");
264 goto out;
265 }
266
267 ncomp = (struct acpi_iort_named_component *)node->node_data;
268 status = !strcmp(ncomp->device_name, buf.pointer) ?
269 AE_OK : AE_NOT_FOUND;
270 acpi_os_free(buf.pointer);
271 } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
272 struct acpi_iort_root_complex *pci_rc;
273 struct pci_bus *bus;
274
275 bus = to_pci_bus(dev);
276 pci_rc = (struct acpi_iort_root_complex *)node->node_data;
277
278 /*
279 * It is assumed that PCI segment numbers maps one-to-one
280 * with root complexes. Each segment number can represent only
281 * one root complex.
282 */
283 status = pci_rc->pci_segment_number == pci_domain_nr(bus) ?
284 AE_OK : AE_NOT_FOUND;
285 } else {
286 status = AE_NOT_FOUND;
287 }
288out:
289 return status;
290}
291
292static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in,
293 u32 *rid_out)
294{
295 /* Single mapping does not care for input id */
296 if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) {
297 if (type == ACPI_IORT_NODE_NAMED_COMPONENT ||
298 type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
299 *rid_out = map->output_base;
300 return 0;
301 }
302
303 pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n",
304 map, type);
305 return -ENXIO;
306 }
307
308 if (rid_in < map->input_base ||
309 (rid_in >= map->input_base + map->id_count))
310 return -ENXIO;
311
312 *rid_out = map->output_base + (rid_in - map->input_base);
313 return 0;
314}
315
316static struct acpi_iort_node *iort_node_map_rid(struct acpi_iort_node *node,
317 u32 rid_in, u32 *rid_out,
318 u8 type)
319{
320 u32 rid = rid_in;
321
322 /* Parse the ID mapping tree to find specified node type */
323 while (node) {
324 struct acpi_iort_id_mapping *map;
325 int i;
326
327 if (node->type == type) {
328 if (rid_out)
329 *rid_out = rid;
330 return node;
331 }
332
333 if (!node->mapping_offset || !node->mapping_count)
334 goto fail_map;
335
336 map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node,
337 node->mapping_offset);
338
339 /* Firmware bug! */
340 if (!map->output_reference) {
341 pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n",
342 node, node->type);
343 goto fail_map;
344 }
345
346 /* Do the RID translation */
347 for (i = 0; i < node->mapping_count; i++, map++) {
348 if (!iort_id_map(map, node->type, rid, &rid))
349 break;
350 }
351
352 if (i == node->mapping_count)
353 goto fail_map;
354
355 node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table,
356 map->output_reference);
357 }
358
359fail_map:
360 /* Map input RID to output RID unchanged on mapping failure*/
361 if (rid_out)
362 *rid_out = rid_in;
363
364 return NULL;
365}
366
367static struct acpi_iort_node *iort_find_dev_node(struct device *dev)
368{
369 struct pci_bus *pbus;
370
371 if (!dev_is_pci(dev))
372 return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT,
373 iort_match_node_callback, dev);
374
375 /* Find a PCI root bus */
376 pbus = to_pci_dev(dev)->bus;
377 while (!pci_is_root_bus(pbus))
378 pbus = pbus->parent;
379
380 return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX,
381 iort_match_node_callback, &pbus->dev);
382}
383
Tomasz Nowicki4bf2efd2016-09-12 20:32:21 +0200384/**
385 * iort_msi_map_rid() - Map a MSI requester ID for a device
386 * @dev: The device for which the mapping is to be done.
387 * @req_id: The device requester ID.
388 *
389 * Returns: mapped MSI RID on success, input requester ID otherwise
390 */
391u32 iort_msi_map_rid(struct device *dev, u32 req_id)
392{
393 struct acpi_iort_node *node;
394 u32 dev_id;
395
396 node = iort_find_dev_node(dev);
397 if (!node)
398 return req_id;
399
400 iort_node_map_rid(node, req_id, &dev_id, ACPI_IORT_NODE_ITS_GROUP);
401 return dev_id;
402}
403
404/**
405 * iort_dev_find_its_id() - Find the ITS identifier for a device
406 * @dev: The device.
407 * @idx: Index of the ITS identifier list.
408 * @its_id: ITS identifier.
409 *
410 * Returns: 0 on success, appropriate error value otherwise
411 */
412static int iort_dev_find_its_id(struct device *dev, u32 req_id,
413 unsigned int idx, int *its_id)
414{
415 struct acpi_iort_its_group *its;
416 struct acpi_iort_node *node;
417
418 node = iort_find_dev_node(dev);
419 if (!node)
420 return -ENXIO;
421
422 node = iort_node_map_rid(node, req_id, NULL, ACPI_IORT_NODE_ITS_GROUP);
423 if (!node)
424 return -ENXIO;
425
426 /* Move to ITS specific data */
427 its = (struct acpi_iort_its_group *)node->node_data;
428 if (idx > its->its_count) {
429 dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n",
430 idx, its->its_count);
431 return -ENXIO;
432 }
433
434 *its_id = its->identifiers[idx];
435 return 0;
436}
437
438/**
439 * iort_get_device_domain() - Find MSI domain related to a device
440 * @dev: The device.
441 * @req_id: Requester ID for the device.
442 *
443 * Returns: the MSI domain for this device, NULL otherwise
444 */
445struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id)
446{
447 struct fwnode_handle *handle;
448 int its_id;
449
450 if (iort_dev_find_its_id(dev, req_id, 0, &its_id))
451 return NULL;
452
453 handle = iort_find_domain_token(its_id);
454 if (!handle)
455 return NULL;
456
457 return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI);
458}
459
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200460void __init acpi_iort_init(void)
461{
462 acpi_status status;
463
464 status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table);
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000465 if (ACPI_FAILURE(status)) {
466 if (status != AE_NOT_FOUND) {
467 const char *msg = acpi_format_exception(status);
468
469 pr_err("Failed to get table, %s\n", msg);
470 }
471
472 return;
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200473 }
Lorenzo Pieralisi34ceea22016-11-21 10:01:34 +0000474
475 acpi_probe_device_table(iort);
Tomasz Nowicki88ef16d2016-09-12 20:54:20 +0200476}