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