Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1 | /* |
| 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 Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 22 | #include <linux/iommu.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 24 | #include <linux/list.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 25 | #include <linux/pci.h> |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 27 | #include <linux/slab.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 28 | |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 29 | #define IORT_TYPE_MASK(type) (1 << (type)) |
| 30 | #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 31 | #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ |
| 32 | (1 << ACPI_IORT_NODE_SMMU_V3)) |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 33 | |
Robert Richter | 12275bf | 2017-06-22 21:20:54 +0200 | [diff] [blame] | 34 | /* 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 Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 39 | struct iort_its_msi_chip { |
| 40 | struct list_head list; |
| 41 | struct fwnode_handle *fw_node; |
| 42 | u32 translation_id; |
| 43 | }; |
| 44 | |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 45 | struct iort_fwnode { |
| 46 | struct list_head list; |
| 47 | struct acpi_iort_node *iort_node; |
| 48 | struct fwnode_handle *fwnode; |
| 49 | }; |
| 50 | static LIST_HEAD(iort_fwnode_list); |
| 51 | static 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 | */ |
| 63 | static 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 | */ |
| 91 | static inline |
| 92 | struct fwnode_handle *iort_get_fwnode(struct acpi_iort_node *node) |
| 93 | { |
| 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 | */ |
| 114 | static 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 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 129 | typedef acpi_status (*iort_find_node_callback) |
| 130 | (struct acpi_iort_node *node, void *context); |
| 131 | |
| 132 | /* Root pointer to the mapped IORT table */ |
| 133 | static struct acpi_table_header *iort_table; |
| 134 | |
| 135 | static LIST_HEAD(iort_msi_chip_list); |
| 136 | static DEFINE_SPINLOCK(iort_msi_chip_lock); |
| 137 | |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 138 | /** |
| 139 | * iort_register_domain_token() - register domain token and related ITS ID |
| 140 | * to the list from where we can get it back later on. |
| 141 | * @trans_id: ITS ID. |
| 142 | * @fw_node: Domain token. |
| 143 | * |
| 144 | * Returns: 0 on success, -ENOMEM if no memory when allocating list element |
| 145 | */ |
| 146 | int iort_register_domain_token(int trans_id, struct fwnode_handle *fw_node) |
| 147 | { |
| 148 | struct iort_its_msi_chip *its_msi_chip; |
| 149 | |
| 150 | its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL); |
| 151 | if (!its_msi_chip) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | its_msi_chip->fw_node = fw_node; |
| 155 | its_msi_chip->translation_id = trans_id; |
| 156 | |
| 157 | spin_lock(&iort_msi_chip_lock); |
| 158 | list_add(&its_msi_chip->list, &iort_msi_chip_list); |
| 159 | spin_unlock(&iort_msi_chip_lock); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * iort_deregister_domain_token() - Deregister domain token based on ITS ID |
| 166 | * @trans_id: ITS ID. |
| 167 | * |
| 168 | * Returns: none. |
| 169 | */ |
| 170 | void iort_deregister_domain_token(int trans_id) |
| 171 | { |
| 172 | struct iort_its_msi_chip *its_msi_chip, *t; |
| 173 | |
| 174 | spin_lock(&iort_msi_chip_lock); |
| 175 | list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { |
| 176 | if (its_msi_chip->translation_id == trans_id) { |
| 177 | list_del(&its_msi_chip->list); |
| 178 | kfree(its_msi_chip); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | spin_unlock(&iort_msi_chip_lock); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * iort_find_domain_token() - Find domain token based on given ITS ID |
| 187 | * @trans_id: ITS ID. |
| 188 | * |
| 189 | * Returns: domain token when find on the list, NULL otherwise |
| 190 | */ |
| 191 | struct fwnode_handle *iort_find_domain_token(int trans_id) |
| 192 | { |
| 193 | struct fwnode_handle *fw_node = NULL; |
| 194 | struct iort_its_msi_chip *its_msi_chip; |
| 195 | |
| 196 | spin_lock(&iort_msi_chip_lock); |
| 197 | list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { |
| 198 | if (its_msi_chip->translation_id == trans_id) { |
| 199 | fw_node = its_msi_chip->fw_node; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | spin_unlock(&iort_msi_chip_lock); |
| 204 | |
| 205 | return fw_node; |
| 206 | } |
| 207 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 208 | static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, |
| 209 | iort_find_node_callback callback, |
| 210 | void *context) |
| 211 | { |
| 212 | struct acpi_iort_node *iort_node, *iort_end; |
| 213 | struct acpi_table_iort *iort; |
| 214 | int i; |
| 215 | |
| 216 | if (!iort_table) |
| 217 | return NULL; |
| 218 | |
| 219 | /* Get the first IORT node */ |
| 220 | iort = (struct acpi_table_iort *)iort_table; |
| 221 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 222 | iort->node_offset); |
| 223 | iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 224 | iort_table->length); |
| 225 | |
| 226 | for (i = 0; i < iort->node_count; i++) { |
| 227 | if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, |
| 228 | "IORT node pointer overflows, bad table!\n")) |
| 229 | return NULL; |
| 230 | |
| 231 | if (iort_node->type == type && |
| 232 | ACPI_SUCCESS(callback(iort_node, context))) |
Hanjun Guo | d89cf2e | 2017-03-07 20:39:56 +0800 | [diff] [blame] | 233 | return iort_node; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 234 | |
| 235 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, |
| 236 | iort_node->length); |
| 237 | } |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | static acpi_status iort_match_node_callback(struct acpi_iort_node *node, |
| 243 | void *context) |
| 244 | { |
| 245 | struct device *dev = context; |
Hanjun Guo | c92bdfe | 2017-03-07 20:39:58 +0800 | [diff] [blame] | 246 | acpi_status status = AE_NOT_FOUND; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 247 | |
| 248 | if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { |
| 249 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 250 | struct acpi_device *adev = to_acpi_device_node(dev->fwnode); |
| 251 | struct acpi_iort_named_component *ncomp; |
| 252 | |
Hanjun Guo | c92bdfe | 2017-03-07 20:39:58 +0800 | [diff] [blame] | 253 | if (!adev) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 254 | goto out; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 255 | |
| 256 | status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); |
| 257 | if (ACPI_FAILURE(status)) { |
| 258 | dev_warn(dev, "Can't get device full path name\n"); |
| 259 | goto out; |
| 260 | } |
| 261 | |
| 262 | ncomp = (struct acpi_iort_named_component *)node->node_data; |
| 263 | status = !strcmp(ncomp->device_name, buf.pointer) ? |
| 264 | AE_OK : AE_NOT_FOUND; |
| 265 | acpi_os_free(buf.pointer); |
| 266 | } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 267 | struct acpi_iort_root_complex *pci_rc; |
| 268 | struct pci_bus *bus; |
| 269 | |
| 270 | bus = to_pci_bus(dev); |
| 271 | pci_rc = (struct acpi_iort_root_complex *)node->node_data; |
| 272 | |
| 273 | /* |
| 274 | * It is assumed that PCI segment numbers maps one-to-one |
| 275 | * with root complexes. Each segment number can represent only |
| 276 | * one root complex. |
| 277 | */ |
| 278 | status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? |
| 279 | AE_OK : AE_NOT_FOUND; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 280 | } |
| 281 | out: |
| 282 | return status; |
| 283 | } |
| 284 | |
| 285 | static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, |
| 286 | u32 *rid_out) |
| 287 | { |
| 288 | /* Single mapping does not care for input id */ |
| 289 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
| 290 | if (type == ACPI_IORT_NODE_NAMED_COMPONENT || |
| 291 | type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 292 | *rid_out = map->output_base; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", |
| 297 | map, type); |
| 298 | return -ENXIO; |
| 299 | } |
| 300 | |
| 301 | if (rid_in < map->input_base || |
| 302 | (rid_in >= map->input_base + map->id_count)) |
| 303 | return -ENXIO; |
| 304 | |
| 305 | *rid_out = map->output_base + (rid_in - map->input_base); |
| 306 | return 0; |
| 307 | } |
| 308 | |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 309 | static |
| 310 | struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, |
Hanjun Guo | 8ca4f1d | 2017-03-07 20:40:04 +0800 | [diff] [blame] | 311 | u32 *id_out, int index) |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 312 | { |
| 313 | struct acpi_iort_node *parent; |
| 314 | struct acpi_iort_id_mapping *map; |
| 315 | |
| 316 | if (!node->mapping_offset || !node->mapping_count || |
| 317 | index >= node->mapping_count) |
| 318 | return NULL; |
| 319 | |
| 320 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 321 | node->mapping_offset + index * sizeof(*map)); |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 322 | |
| 323 | /* Firmware bug! */ |
| 324 | if (!map->output_reference) { |
| 325 | pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", |
| 326 | node, node->type); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 331 | map->output_reference); |
| 332 | |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 333 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 334 | if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || |
| 335 | node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 336 | *id_out = map->output_base; |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 337 | return parent; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 344 | static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, |
| 345 | u32 id_in, u32 *id_out, |
| 346 | u8 type_mask) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 347 | { |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 348 | u32 id = id_in; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 349 | |
| 350 | /* Parse the ID mapping tree to find specified node type */ |
| 351 | while (node) { |
| 352 | struct acpi_iort_id_mapping *map; |
| 353 | int i; |
| 354 | |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 355 | if (IORT_TYPE_MASK(node->type) & type_mask) { |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 356 | if (id_out) |
| 357 | *id_out = id; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 358 | return node; |
| 359 | } |
| 360 | |
| 361 | if (!node->mapping_offset || !node->mapping_count) |
| 362 | goto fail_map; |
| 363 | |
| 364 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
| 365 | node->mapping_offset); |
| 366 | |
| 367 | /* Firmware bug! */ |
| 368 | if (!map->output_reference) { |
| 369 | pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", |
| 370 | node, node->type); |
| 371 | goto fail_map; |
| 372 | } |
| 373 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 374 | /* Do the ID translation */ |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 375 | for (i = 0; i < node->mapping_count; i++, map++) { |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 376 | if (!iort_id_map(map, node->type, id, &id)) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
| 380 | if (i == node->mapping_count) |
| 381 | goto fail_map; |
| 382 | |
| 383 | node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 384 | map->output_reference); |
| 385 | } |
| 386 | |
| 387 | fail_map: |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 388 | /* Map input ID to output ID unchanged on mapping failure */ |
| 389 | if (id_out) |
| 390 | *id_out = id_in; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 391 | |
| 392 | return NULL; |
| 393 | } |
| 394 | |
Hanjun Guo | 8ca4f1d | 2017-03-07 20:40:04 +0800 | [diff] [blame] | 395 | static |
| 396 | struct acpi_iort_node *iort_node_map_platform_id(struct acpi_iort_node *node, |
| 397 | u32 *id_out, u8 type_mask, |
| 398 | int index) |
| 399 | { |
| 400 | struct acpi_iort_node *parent; |
| 401 | u32 id; |
| 402 | |
| 403 | /* step 1: retrieve the initial dev id */ |
| 404 | parent = iort_node_get_id(node, &id, index); |
| 405 | if (!parent) |
| 406 | return NULL; |
| 407 | |
| 408 | /* |
| 409 | * optional step 2: map the initial dev id if its parent is not |
| 410 | * the target type we want, map it again for the use cases such |
| 411 | * as NC (named component) -> SMMU -> ITS. If the type is matched, |
| 412 | * return the initial dev id and its parent pointer directly. |
| 413 | */ |
| 414 | if (!(IORT_TYPE_MASK(parent->type) & type_mask)) |
| 415 | parent = iort_node_map_id(parent, id, id_out, type_mask); |
| 416 | else |
| 417 | if (id_out) |
| 418 | *id_out = id; |
| 419 | |
| 420 | return parent; |
| 421 | } |
| 422 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 423 | static struct acpi_iort_node *iort_find_dev_node(struct device *dev) |
| 424 | { |
| 425 | struct pci_bus *pbus; |
| 426 | |
| 427 | if (!dev_is_pci(dev)) |
| 428 | return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 429 | iort_match_node_callback, dev); |
| 430 | |
| 431 | /* Find a PCI root bus */ |
| 432 | pbus = to_pci_dev(dev)->bus; |
| 433 | while (!pci_is_root_bus(pbus)) |
| 434 | pbus = pbus->parent; |
| 435 | |
| 436 | return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
| 437 | iort_match_node_callback, &pbus->dev); |
| 438 | } |
| 439 | |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 440 | /** |
| 441 | * iort_msi_map_rid() - Map a MSI requester ID for a device |
| 442 | * @dev: The device for which the mapping is to be done. |
| 443 | * @req_id: The device requester ID. |
| 444 | * |
| 445 | * Returns: mapped MSI RID on success, input requester ID otherwise |
| 446 | */ |
| 447 | u32 iort_msi_map_rid(struct device *dev, u32 req_id) |
| 448 | { |
| 449 | struct acpi_iort_node *node; |
| 450 | u32 dev_id; |
| 451 | |
| 452 | node = iort_find_dev_node(dev); |
| 453 | if (!node) |
| 454 | return req_id; |
| 455 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 456 | iort_node_map_id(node, req_id, &dev_id, IORT_MSI_TYPE); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 457 | return dev_id; |
| 458 | } |
| 459 | |
| 460 | /** |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 461 | * iort_pmsi_get_dev_id() - Get the device id for a device |
| 462 | * @dev: The device for which the mapping is to be done. |
| 463 | * @dev_id: The device ID found. |
| 464 | * |
| 465 | * Returns: 0 for successful find a dev id, -ENODEV on error |
| 466 | */ |
| 467 | int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) |
| 468 | { |
| 469 | int i; |
| 470 | struct acpi_iort_node *node; |
| 471 | |
| 472 | node = iort_find_dev_node(dev); |
| 473 | if (!node) |
| 474 | return -ENODEV; |
| 475 | |
| 476 | for (i = 0; i < node->mapping_count; i++) { |
| 477 | if (iort_node_map_platform_id(node, dev_id, IORT_MSI_TYPE, i)) |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | return -ENODEV; |
| 482 | } |
| 483 | |
| 484 | /** |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 485 | * iort_dev_find_its_id() - Find the ITS identifier for a device |
| 486 | * @dev: The device. |
Hanjun Guo | 6cb6bf5 | 2017-03-07 20:39:57 +0800 | [diff] [blame] | 487 | * @req_id: Device's requester ID |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 488 | * @idx: Index of the ITS identifier list. |
| 489 | * @its_id: ITS identifier. |
| 490 | * |
| 491 | * Returns: 0 on success, appropriate error value otherwise |
| 492 | */ |
| 493 | static int iort_dev_find_its_id(struct device *dev, u32 req_id, |
| 494 | unsigned int idx, int *its_id) |
| 495 | { |
| 496 | struct acpi_iort_its_group *its; |
| 497 | struct acpi_iort_node *node; |
| 498 | |
| 499 | node = iort_find_dev_node(dev); |
| 500 | if (!node) |
| 501 | return -ENXIO; |
| 502 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 503 | node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 504 | if (!node) |
| 505 | return -ENXIO; |
| 506 | |
| 507 | /* Move to ITS specific data */ |
| 508 | its = (struct acpi_iort_its_group *)node->node_data; |
| 509 | if (idx > its->its_count) { |
| 510 | dev_err(dev, "requested ITS ID index [%d] is greater than available [%d]\n", |
| 511 | idx, its->its_count); |
| 512 | return -ENXIO; |
| 513 | } |
| 514 | |
| 515 | *its_id = its->identifiers[idx]; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * iort_get_device_domain() - Find MSI domain related to a device |
| 521 | * @dev: The device. |
| 522 | * @req_id: Requester ID for the device. |
| 523 | * |
| 524 | * Returns: the MSI domain for this device, NULL otherwise |
| 525 | */ |
| 526 | struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id) |
| 527 | { |
| 528 | struct fwnode_handle *handle; |
| 529 | int its_id; |
| 530 | |
| 531 | if (iort_dev_find_its_id(dev, req_id, 0, &its_id)) |
| 532 | return NULL; |
| 533 | |
| 534 | handle = iort_find_domain_token(its_id); |
| 535 | if (!handle) |
| 536 | return NULL; |
| 537 | |
| 538 | return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI); |
| 539 | } |
| 540 | |
Hanjun Guo | d4f54a1 | 2017-03-07 20:40:06 +0800 | [diff] [blame] | 541 | /** |
| 542 | * iort_get_platform_device_domain() - Find MSI domain related to a |
| 543 | * platform device |
| 544 | * @dev: the dev pointer associated with the platform device |
| 545 | * |
| 546 | * Returns: the MSI domain for this device, NULL otherwise |
| 547 | */ |
| 548 | static struct irq_domain *iort_get_platform_device_domain(struct device *dev) |
| 549 | { |
| 550 | struct acpi_iort_node *node, *msi_parent; |
| 551 | struct fwnode_handle *iort_fwnode; |
| 552 | struct acpi_iort_its_group *its; |
| 553 | int i; |
| 554 | |
| 555 | /* find its associated iort node */ |
| 556 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 557 | iort_match_node_callback, dev); |
| 558 | if (!node) |
| 559 | return NULL; |
| 560 | |
| 561 | /* then find its msi parent node */ |
| 562 | for (i = 0; i < node->mapping_count; i++) { |
| 563 | msi_parent = iort_node_map_platform_id(node, NULL, |
| 564 | IORT_MSI_TYPE, i); |
| 565 | if (msi_parent) |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | if (!msi_parent) |
| 570 | return NULL; |
| 571 | |
| 572 | /* Move to ITS specific data */ |
| 573 | its = (struct acpi_iort_its_group *)msi_parent->node_data; |
| 574 | |
| 575 | iort_fwnode = iort_find_domain_token(its->identifiers[0]); |
| 576 | if (!iort_fwnode) |
| 577 | return NULL; |
| 578 | |
| 579 | return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); |
| 580 | } |
| 581 | |
| 582 | void acpi_configure_pmsi_domain(struct device *dev) |
| 583 | { |
| 584 | struct irq_domain *msi_domain; |
| 585 | |
| 586 | msi_domain = iort_get_platform_device_domain(dev); |
| 587 | if (msi_domain) |
| 588 | dev_set_msi_domain(dev, msi_domain); |
| 589 | } |
| 590 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 591 | static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data) |
| 592 | { |
| 593 | u32 *rid = data; |
| 594 | |
| 595 | *rid = alias; |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int arm_smmu_iort_xlate(struct device *dev, u32 streamid, |
| 600 | struct fwnode_handle *fwnode, |
| 601 | const struct iommu_ops *ops) |
| 602 | { |
| 603 | int ret = iommu_fwspec_init(dev, fwnode, ops); |
| 604 | |
| 605 | if (!ret) |
| 606 | ret = iommu_fwspec_add_ids(dev, &streamid, 1); |
| 607 | |
| 608 | return ret; |
| 609 | } |
| 610 | |
Lorenzo Pieralisi | 1d9029d | 2017-04-10 16:50:59 +0530 | [diff] [blame] | 611 | static inline bool iort_iommu_driver_enabled(u8 type) |
| 612 | { |
| 613 | switch (type) { |
| 614 | case ACPI_IORT_NODE_SMMU_V3: |
| 615 | return IS_BUILTIN(CONFIG_ARM_SMMU_V3); |
| 616 | case ACPI_IORT_NODE_SMMU: |
| 617 | return IS_BUILTIN(CONFIG_ARM_SMMU); |
| 618 | default: |
| 619 | pr_warn("IORT node type %u does not describe an SMMU\n", type); |
| 620 | return false; |
| 621 | } |
| 622 | } |
| 623 | |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 624 | #ifdef CONFIG_IOMMU_API |
| 625 | static inline |
| 626 | const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec) |
| 627 | { |
| 628 | return (fwspec && fwspec->ops) ? fwspec->ops : NULL; |
| 629 | } |
| 630 | |
| 631 | static inline |
| 632 | int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev) |
| 633 | { |
| 634 | int err = 0; |
| 635 | |
| 636 | if (!IS_ERR_OR_NULL(ops) && ops->add_device && dev->bus && |
| 637 | !dev->iommu_group) |
| 638 | err = ops->add_device(dev); |
| 639 | |
| 640 | return err; |
| 641 | } |
| 642 | #else |
| 643 | static inline |
| 644 | const struct iommu_ops *iort_fwspec_iommu_ops(struct iommu_fwspec *fwspec) |
| 645 | { return NULL; } |
| 646 | static inline |
| 647 | int iort_add_device_replay(const struct iommu_ops *ops, struct device *dev) |
| 648 | { return 0; } |
| 649 | #endif |
| 650 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 651 | static const struct iommu_ops *iort_iommu_xlate(struct device *dev, |
| 652 | struct acpi_iort_node *node, |
| 653 | u32 streamid) |
| 654 | { |
| 655 | const struct iommu_ops *ops = NULL; |
| 656 | int ret = -ENODEV; |
| 657 | struct fwnode_handle *iort_fwnode; |
| 658 | |
| 659 | if (node) { |
| 660 | iort_fwnode = iort_get_fwnode(node); |
| 661 | if (!iort_fwnode) |
| 662 | return NULL; |
| 663 | |
Joerg Roedel | 534766d | 2017-01-31 16:58:42 +0100 | [diff] [blame] | 664 | ops = iommu_ops_from_fwnode(iort_fwnode); |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 665 | /* |
| 666 | * If the ops look-up fails, this means that either |
| 667 | * the SMMU drivers have not been probed yet or that |
| 668 | * the SMMU drivers are not built in the kernel; |
| 669 | * Depending on whether the SMMU drivers are built-in |
| 670 | * in the kernel or not, defer the IOMMU configuration |
| 671 | * or just abort it. |
| 672 | */ |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 673 | if (!ops) |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 674 | return iort_iommu_driver_enabled(node->type) ? |
| 675 | ERR_PTR(-EPROBE_DEFER) : NULL; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 676 | |
| 677 | ret = arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops); |
| 678 | } |
| 679 | |
| 680 | return ret ? NULL : ops; |
| 681 | } |
| 682 | |
| 683 | /** |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame^] | 684 | * iort_dma_setup() - Set-up device DMA parameters. |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 685 | * |
| 686 | * @dev: device to configure |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame^] | 687 | * @dma_addr: device DMA address result pointer |
| 688 | * @size: DMA range size result pointer |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 689 | */ |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame^] | 690 | void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size) |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 691 | { |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame^] | 692 | u64 mask, dmaaddr = 0, size = 0, offset = 0; |
| 693 | int ret, msb; |
| 694 | |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 695 | /* |
| 696 | * Set default coherent_dma_mask to 32 bit. Drivers are expected to |
| 697 | * setup the correct supported mask. |
| 698 | */ |
| 699 | if (!dev->coherent_dma_mask) |
| 700 | dev->coherent_dma_mask = DMA_BIT_MASK(32); |
| 701 | |
| 702 | /* |
| 703 | * Set it to coherent_dma_mask by default if the architecture |
| 704 | * code has not set it. |
| 705 | */ |
| 706 | if (!dev->dma_mask) |
| 707 | dev->dma_mask = &dev->coherent_dma_mask; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame^] | 708 | |
| 709 | size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); |
| 710 | |
| 711 | if (dev_is_pci(dev)) { |
| 712 | ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size); |
| 713 | if (!ret) { |
| 714 | msb = fls64(dmaaddr + size - 1); |
| 715 | /* |
| 716 | * Round-up to the power-of-two mask or set |
| 717 | * the mask to the whole 64-bit address space |
| 718 | * in case the DMA region covers the full |
| 719 | * memory window. |
| 720 | */ |
| 721 | mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1; |
| 722 | /* |
| 723 | * Limit coherent and dma mask based on size |
| 724 | * retrieved from firmware. |
| 725 | */ |
| 726 | dev->coherent_dma_mask = mask; |
| 727 | *dev->dma_mask = mask; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | *dma_addr = dmaaddr; |
| 732 | *dma_size = size; |
| 733 | |
| 734 | dev->dma_pfn_offset = PFN_DOWN(offset); |
| 735 | dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset); |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 739 | * iort_iommu_configure - Set-up IOMMU configuration for a device. |
| 740 | * |
| 741 | * @dev: device to configure |
| 742 | * |
| 743 | * Returns: iommu_ops pointer on configuration success |
| 744 | * NULL on configuration failure |
| 745 | */ |
| 746 | const struct iommu_ops *iort_iommu_configure(struct device *dev) |
| 747 | { |
| 748 | struct acpi_iort_node *node, *parent; |
| 749 | const struct iommu_ops *ops = NULL; |
| 750 | u32 streamid = 0; |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 751 | int err; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 752 | |
Lorenzo Pieralisi | 4dac321 | 2017-05-27 19:17:44 +0530 | [diff] [blame] | 753 | /* |
| 754 | * If we already translated the fwspec there |
| 755 | * is nothing left to do, return the iommu_ops. |
| 756 | */ |
| 757 | ops = iort_fwspec_iommu_ops(dev->iommu_fwspec); |
| 758 | if (ops) |
| 759 | return ops; |
| 760 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 761 | if (dev_is_pci(dev)) { |
| 762 | struct pci_bus *bus = to_pci_dev(dev)->bus; |
| 763 | u32 rid; |
| 764 | |
| 765 | pci_for_each_dma_alias(to_pci_dev(dev), __get_pci_rid, |
| 766 | &rid); |
| 767 | |
| 768 | node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
| 769 | iort_match_node_callback, &bus->dev); |
| 770 | if (!node) |
| 771 | return NULL; |
| 772 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 773 | parent = iort_node_map_id(node, rid, &streamid, |
| 774 | IORT_IOMMU_TYPE); |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 775 | |
| 776 | ops = iort_iommu_xlate(dev, parent, streamid); |
| 777 | |
| 778 | } else { |
| 779 | int i = 0; |
| 780 | |
| 781 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 782 | iort_match_node_callback, dev); |
| 783 | if (!node) |
| 784 | return NULL; |
| 785 | |
Hanjun Guo | 8ca4f1d | 2017-03-07 20:40:04 +0800 | [diff] [blame] | 786 | parent = iort_node_map_platform_id(node, &streamid, |
| 787 | IORT_IOMMU_TYPE, i++); |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 788 | |
| 789 | while (parent) { |
| 790 | ops = iort_iommu_xlate(dev, parent, streamid); |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 791 | if (IS_ERR_OR_NULL(ops)) |
| 792 | return ops; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 793 | |
Hanjun Guo | 8ca4f1d | 2017-03-07 20:40:04 +0800 | [diff] [blame] | 794 | parent = iort_node_map_platform_id(node, &streamid, |
| 795 | IORT_IOMMU_TYPE, |
| 796 | i++); |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 800 | /* |
| 801 | * If we have reason to believe the IOMMU driver missed the initial |
| 802 | * add_device callback for dev, replay it to get things in order. |
| 803 | */ |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 804 | err = iort_add_device_replay(ops, dev); |
| 805 | if (err) |
| 806 | ops = ERR_PTR(err); |
Sricharan R | 5a1bb63 | 2017-04-10 16:51:03 +0530 | [diff] [blame] | 807 | |
Sricharan R | 058f8c3 | 2017-05-27 19:17:42 +0530 | [diff] [blame] | 808 | /* Ignore all other errors apart from EPROBE_DEFER */ |
| 809 | if (IS_ERR(ops) && (PTR_ERR(ops) != -EPROBE_DEFER)) { |
| 810 | dev_dbg(dev, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops)); |
| 811 | ops = NULL; |
| 812 | } |
| 813 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 814 | return ops; |
| 815 | } |
| 816 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 817 | static void __init acpi_iort_register_irq(int hwirq, const char *name, |
| 818 | int trigger, |
| 819 | struct resource *res) |
| 820 | { |
| 821 | int irq = acpi_register_gsi(NULL, hwirq, trigger, |
| 822 | ACPI_ACTIVE_HIGH); |
| 823 | |
| 824 | if (irq <= 0) { |
| 825 | pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, |
| 826 | name); |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | res->start = irq; |
| 831 | res->end = irq; |
| 832 | res->flags = IORESOURCE_IRQ; |
| 833 | res->name = name; |
| 834 | } |
| 835 | |
| 836 | static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) |
| 837 | { |
| 838 | struct acpi_iort_smmu_v3 *smmu; |
| 839 | /* Always present mem resource */ |
| 840 | int num_res = 1; |
| 841 | |
| 842 | /* Retrieve SMMUv3 specific data */ |
| 843 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 844 | |
| 845 | if (smmu->event_gsiv) |
| 846 | num_res++; |
| 847 | |
| 848 | if (smmu->pri_gsiv) |
| 849 | num_res++; |
| 850 | |
| 851 | if (smmu->gerr_gsiv) |
| 852 | num_res++; |
| 853 | |
| 854 | if (smmu->sync_gsiv) |
| 855 | num_res++; |
| 856 | |
| 857 | return num_res; |
| 858 | } |
| 859 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 860 | static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) |
| 861 | { |
| 862 | /* |
| 863 | * Cavium ThunderX2 implementation doesn't not support unique |
| 864 | * irq line. Use single irq line for all the SMMUv3 interrupts. |
| 865 | */ |
| 866 | if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 867 | return false; |
| 868 | |
| 869 | /* |
| 870 | * ThunderX2 doesn't support MSIs from the SMMU, so we're checking |
| 871 | * SPI numbers here. |
| 872 | */ |
| 873 | return smmu->event_gsiv == smmu->pri_gsiv && |
| 874 | smmu->event_gsiv == smmu->gerr_gsiv && |
| 875 | smmu->event_gsiv == smmu->sync_gsiv; |
| 876 | } |
| 877 | |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 878 | static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) |
| 879 | { |
| 880 | /* |
| 881 | * Override the size, for Cavium ThunderX2 implementation |
| 882 | * which doesn't support the page 1 SMMU register space. |
| 883 | */ |
| 884 | if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 885 | return SZ_64K; |
| 886 | |
| 887 | return SZ_128K; |
| 888 | } |
| 889 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 890 | static void __init arm_smmu_v3_init_resources(struct resource *res, |
| 891 | struct acpi_iort_node *node) |
| 892 | { |
| 893 | struct acpi_iort_smmu_v3 *smmu; |
| 894 | int num_res = 0; |
| 895 | |
| 896 | /* Retrieve SMMUv3 specific data */ |
| 897 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 898 | |
| 899 | res[num_res].start = smmu->base_address; |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 900 | res[num_res].end = smmu->base_address + |
| 901 | arm_smmu_v3_resource_size(smmu) - 1; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 902 | res[num_res].flags = IORESOURCE_MEM; |
| 903 | |
| 904 | num_res++; |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 905 | if (arm_smmu_v3_is_combined_irq(smmu)) { |
| 906 | if (smmu->event_gsiv) |
| 907 | acpi_iort_register_irq(smmu->event_gsiv, "combined", |
| 908 | ACPI_EDGE_SENSITIVE, |
| 909 | &res[num_res++]); |
| 910 | } else { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 911 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 912 | if (smmu->event_gsiv) |
| 913 | acpi_iort_register_irq(smmu->event_gsiv, "eventq", |
| 914 | ACPI_EDGE_SENSITIVE, |
| 915 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 916 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 917 | if (smmu->pri_gsiv) |
| 918 | acpi_iort_register_irq(smmu->pri_gsiv, "priq", |
| 919 | ACPI_EDGE_SENSITIVE, |
| 920 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 921 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 922 | if (smmu->gerr_gsiv) |
| 923 | acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", |
| 924 | ACPI_EDGE_SENSITIVE, |
| 925 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 926 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 927 | if (smmu->sync_gsiv) |
| 928 | acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", |
| 929 | ACPI_EDGE_SENSITIVE, |
| 930 | &res[num_res++]); |
| 931 | } |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static bool __init arm_smmu_v3_is_coherent(struct acpi_iort_node *node) |
| 935 | { |
| 936 | struct acpi_iort_smmu_v3 *smmu; |
| 937 | |
| 938 | /* Retrieve SMMUv3 specific data */ |
| 939 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 940 | |
| 941 | return smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE; |
| 942 | } |
| 943 | |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 944 | static int __init arm_smmu_count_resources(struct acpi_iort_node *node) |
| 945 | { |
| 946 | struct acpi_iort_smmu *smmu; |
| 947 | |
| 948 | /* Retrieve SMMU specific data */ |
| 949 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 950 | |
| 951 | /* |
| 952 | * Only consider the global fault interrupt and ignore the |
| 953 | * configuration access interrupt. |
| 954 | * |
| 955 | * MMIO address and global fault interrupt resources are always |
| 956 | * present so add them to the context interrupt count as a static |
| 957 | * value. |
| 958 | */ |
| 959 | return smmu->context_interrupt_count + 2; |
| 960 | } |
| 961 | |
| 962 | static void __init arm_smmu_init_resources(struct resource *res, |
| 963 | struct acpi_iort_node *node) |
| 964 | { |
| 965 | struct acpi_iort_smmu *smmu; |
| 966 | int i, hw_irq, trigger, num_res = 0; |
| 967 | u64 *ctx_irq, *glb_irq; |
| 968 | |
| 969 | /* Retrieve SMMU specific data */ |
| 970 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 971 | |
| 972 | res[num_res].start = smmu->base_address; |
| 973 | res[num_res].end = smmu->base_address + smmu->span - 1; |
| 974 | res[num_res].flags = IORESOURCE_MEM; |
| 975 | num_res++; |
| 976 | |
| 977 | glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); |
| 978 | /* Global IRQs */ |
| 979 | hw_irq = IORT_IRQ_MASK(glb_irq[0]); |
| 980 | trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); |
| 981 | |
| 982 | acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, |
| 983 | &res[num_res++]); |
| 984 | |
| 985 | /* Context IRQs */ |
| 986 | ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); |
| 987 | for (i = 0; i < smmu->context_interrupt_count; i++) { |
| 988 | hw_irq = IORT_IRQ_MASK(ctx_irq[i]); |
| 989 | trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); |
| 990 | |
| 991 | acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, |
| 992 | &res[num_res++]); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | static bool __init arm_smmu_is_coherent(struct acpi_iort_node *node) |
| 997 | { |
| 998 | struct acpi_iort_smmu *smmu; |
| 999 | |
| 1000 | /* Retrieve SMMU specific data */ |
| 1001 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1002 | |
| 1003 | return smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK; |
| 1004 | } |
| 1005 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1006 | struct iort_iommu_config { |
| 1007 | const char *name; |
| 1008 | int (*iommu_init)(struct acpi_iort_node *node); |
| 1009 | bool (*iommu_is_coherent)(struct acpi_iort_node *node); |
| 1010 | int (*iommu_count_resources)(struct acpi_iort_node *node); |
| 1011 | void (*iommu_init_resources)(struct resource *res, |
| 1012 | struct acpi_iort_node *node); |
| 1013 | }; |
| 1014 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1015 | static const struct iort_iommu_config iort_arm_smmu_v3_cfg __initconst = { |
| 1016 | .name = "arm-smmu-v3", |
| 1017 | .iommu_is_coherent = arm_smmu_v3_is_coherent, |
| 1018 | .iommu_count_resources = arm_smmu_v3_count_resources, |
| 1019 | .iommu_init_resources = arm_smmu_v3_init_resources |
| 1020 | }; |
| 1021 | |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1022 | static const struct iort_iommu_config iort_arm_smmu_cfg __initconst = { |
| 1023 | .name = "arm-smmu", |
| 1024 | .iommu_is_coherent = arm_smmu_is_coherent, |
| 1025 | .iommu_count_resources = arm_smmu_count_resources, |
| 1026 | .iommu_init_resources = arm_smmu_init_resources |
| 1027 | }; |
| 1028 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1029 | static __init |
| 1030 | const struct iort_iommu_config *iort_get_iommu_cfg(struct acpi_iort_node *node) |
| 1031 | { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1032 | switch (node->type) { |
| 1033 | case ACPI_IORT_NODE_SMMU_V3: |
| 1034 | return &iort_arm_smmu_v3_cfg; |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1035 | case ACPI_IORT_NODE_SMMU: |
| 1036 | return &iort_arm_smmu_cfg; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1037 | default: |
| 1038 | return NULL; |
| 1039 | } |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * iort_add_smmu_platform_device() - Allocate a platform device for SMMU |
| 1044 | * @node: Pointer to SMMU ACPI IORT node |
| 1045 | * |
| 1046 | * Returns: 0 on success, <0 failure |
| 1047 | */ |
| 1048 | static int __init iort_add_smmu_platform_device(struct acpi_iort_node *node) |
| 1049 | { |
| 1050 | struct fwnode_handle *fwnode; |
| 1051 | struct platform_device *pdev; |
| 1052 | struct resource *r; |
| 1053 | enum dev_dma_attr attr; |
| 1054 | int ret, count; |
| 1055 | const struct iort_iommu_config *ops = iort_get_iommu_cfg(node); |
| 1056 | |
| 1057 | if (!ops) |
| 1058 | return -ENODEV; |
| 1059 | |
| 1060 | pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); |
| 1061 | if (!pdev) |
Dan Carpenter | 5e5afa6 | 2017-01-17 16:36:23 +0300 | [diff] [blame] | 1062 | return -ENOMEM; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1063 | |
| 1064 | count = ops->iommu_count_resources(node); |
| 1065 | |
| 1066 | r = kcalloc(count, sizeof(*r), GFP_KERNEL); |
| 1067 | if (!r) { |
| 1068 | ret = -ENOMEM; |
| 1069 | goto dev_put; |
| 1070 | } |
| 1071 | |
| 1072 | ops->iommu_init_resources(r, node); |
| 1073 | |
| 1074 | ret = platform_device_add_resources(pdev, r, count); |
| 1075 | /* |
| 1076 | * Resources are duplicated in platform_device_add_resources, |
| 1077 | * free their allocated memory |
| 1078 | */ |
| 1079 | kfree(r); |
| 1080 | |
| 1081 | if (ret) |
| 1082 | goto dev_put; |
| 1083 | |
| 1084 | /* |
| 1085 | * Add a copy of IORT node pointer to platform_data to |
| 1086 | * be used to retrieve IORT data information. |
| 1087 | */ |
| 1088 | ret = platform_device_add_data(pdev, &node, sizeof(node)); |
| 1089 | if (ret) |
| 1090 | goto dev_put; |
| 1091 | |
| 1092 | /* |
| 1093 | * We expect the dma masks to be equivalent for |
| 1094 | * all SMMUs set-ups |
| 1095 | */ |
| 1096 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 1097 | |
| 1098 | fwnode = iort_get_fwnode(node); |
| 1099 | |
| 1100 | if (!fwnode) { |
| 1101 | ret = -ENODEV; |
| 1102 | goto dev_put; |
| 1103 | } |
| 1104 | |
| 1105 | pdev->dev.fwnode = fwnode; |
| 1106 | |
| 1107 | attr = ops->iommu_is_coherent(node) ? |
| 1108 | DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; |
| 1109 | |
| 1110 | /* Configure DMA for the page table walker */ |
| 1111 | acpi_dma_configure(&pdev->dev, attr); |
| 1112 | |
| 1113 | ret = platform_device_add(pdev); |
| 1114 | if (ret) |
| 1115 | goto dma_deconfigure; |
| 1116 | |
| 1117 | return 0; |
| 1118 | |
| 1119 | dma_deconfigure: |
| 1120 | acpi_dma_deconfigure(&pdev->dev); |
| 1121 | dev_put: |
| 1122 | platform_device_put(pdev); |
| 1123 | |
| 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | static void __init iort_init_platform_devices(void) |
| 1128 | { |
| 1129 | struct acpi_iort_node *iort_node, *iort_end; |
| 1130 | struct acpi_table_iort *iort; |
| 1131 | struct fwnode_handle *fwnode; |
| 1132 | int i, ret; |
| 1133 | |
| 1134 | /* |
| 1135 | * iort_table and iort both point to the start of IORT table, but |
| 1136 | * have different struct types |
| 1137 | */ |
| 1138 | iort = (struct acpi_table_iort *)iort_table; |
| 1139 | |
| 1140 | /* Get the first IORT node */ |
| 1141 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1142 | iort->node_offset); |
| 1143 | iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1144 | iort_table->length); |
| 1145 | |
| 1146 | for (i = 0; i < iort->node_count; i++) { |
| 1147 | if (iort_node >= iort_end) { |
| 1148 | pr_err("iort node pointer overflows, bad table\n"); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | if ((iort_node->type == ACPI_IORT_NODE_SMMU) || |
| 1153 | (iort_node->type == ACPI_IORT_NODE_SMMU_V3)) { |
| 1154 | |
| 1155 | fwnode = acpi_alloc_fwnode_static(); |
| 1156 | if (!fwnode) |
| 1157 | return; |
| 1158 | |
| 1159 | iort_set_fwnode(iort_node, fwnode); |
| 1160 | |
| 1161 | ret = iort_add_smmu_platform_device(iort_node); |
| 1162 | if (ret) { |
| 1163 | iort_delete_fwnode(iort_node); |
| 1164 | acpi_free_fwnode_static(fwnode); |
| 1165 | return; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, |
| 1170 | iort_node->length); |
| 1171 | } |
| 1172 | } |
| 1173 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1174 | void __init acpi_iort_init(void) |
| 1175 | { |
| 1176 | acpi_status status; |
| 1177 | |
| 1178 | status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1179 | if (ACPI_FAILURE(status)) { |
| 1180 | if (status != AE_NOT_FOUND) { |
| 1181 | const char *msg = acpi_format_exception(status); |
| 1182 | |
| 1183 | pr_err("Failed to get table, %s\n", msg); |
| 1184 | } |
| 1185 | |
| 1186 | return; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1187 | } |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1188 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1189 | iort_init_platform_devices(); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1190 | } |