Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016, Semihalf |
| 4 | * Author: Tomasz Nowicki <tn@semihalf.com> |
| 5 | * |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 6 | * This file implements early detection/parsing of I/O mapping |
| 7 | * reported to OS through firmware via I/O Remapping Table (IORT) |
| 8 | * IORT document number: ARM DEN 0049A |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) "ACPI: IORT: " fmt |
| 12 | |
| 13 | #include <linux/acpi_iort.h> |
Jean-Philippe Brucker | da22565 | 2020-01-15 13:52:30 +0100 | [diff] [blame^] | 14 | #include <linux/bitfield.h> |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 15 | #include <linux/iommu.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 17 | #include <linux/list.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 18 | #include <linux/pci.h> |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 20 | #include <linux/slab.h> |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 21 | |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 22 | #define IORT_TYPE_MASK(type) (1 << (type)) |
| 23 | #define IORT_MSI_TYPE (1 << ACPI_IORT_NODE_ITS_GROUP) |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 24 | #define IORT_IOMMU_TYPE ((1 << ACPI_IORT_NODE_SMMU) | \ |
| 25 | (1 << ACPI_IORT_NODE_SMMU_V3)) |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 26 | |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 27 | struct iort_its_msi_chip { |
| 28 | struct list_head list; |
| 29 | struct fwnode_handle *fw_node; |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 30 | phys_addr_t base_addr; |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 31 | u32 translation_id; |
| 32 | }; |
| 33 | |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 34 | struct iort_fwnode { |
| 35 | struct list_head list; |
| 36 | struct acpi_iort_node *iort_node; |
| 37 | struct fwnode_handle *fwnode; |
| 38 | }; |
| 39 | static LIST_HEAD(iort_fwnode_list); |
| 40 | static DEFINE_SPINLOCK(iort_fwnode_lock); |
| 41 | |
| 42 | /** |
| 43 | * iort_set_fwnode() - Create iort_fwnode and use it to register |
| 44 | * iommu data in the iort_fwnode_list |
| 45 | * |
| 46 | * @node: IORT table node associated with the IOMMU |
| 47 | * @fwnode: fwnode associated with the IORT node |
| 48 | * |
| 49 | * Returns: 0 on success |
| 50 | * <0 on failure |
| 51 | */ |
| 52 | static inline int iort_set_fwnode(struct acpi_iort_node *iort_node, |
| 53 | struct fwnode_handle *fwnode) |
| 54 | { |
| 55 | struct iort_fwnode *np; |
| 56 | |
| 57 | np = kzalloc(sizeof(struct iort_fwnode), GFP_ATOMIC); |
| 58 | |
| 59 | if (WARN_ON(!np)) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | INIT_LIST_HEAD(&np->list); |
| 63 | np->iort_node = iort_node; |
| 64 | np->fwnode = fwnode; |
| 65 | |
| 66 | spin_lock(&iort_fwnode_lock); |
| 67 | list_add_tail(&np->list, &iort_fwnode_list); |
| 68 | spin_unlock(&iort_fwnode_lock); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * iort_get_fwnode() - Retrieve fwnode associated with an IORT node |
| 75 | * |
| 76 | * @node: IORT table node to be looked-up |
| 77 | * |
| 78 | * Returns: fwnode_handle pointer on success, NULL on failure |
| 79 | */ |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 80 | static inline struct fwnode_handle *iort_get_fwnode( |
| 81 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | 7936df9 | 2016-11-21 10:01:35 +0000 | [diff] [blame] | 82 | { |
| 83 | struct iort_fwnode *curr; |
| 84 | struct fwnode_handle *fwnode = NULL; |
| 85 | |
| 86 | spin_lock(&iort_fwnode_lock); |
| 87 | list_for_each_entry(curr, &iort_fwnode_list, list) { |
| 88 | if (curr->iort_node == node) { |
| 89 | fwnode = curr->fwnode; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | spin_unlock(&iort_fwnode_lock); |
| 94 | |
| 95 | return fwnode; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * iort_delete_fwnode() - Delete fwnode associated with an IORT node |
| 100 | * |
| 101 | * @node: IORT table node associated with fwnode to delete |
| 102 | */ |
| 103 | static inline void iort_delete_fwnode(struct acpi_iort_node *node) |
| 104 | { |
| 105 | struct iort_fwnode *curr, *tmp; |
| 106 | |
| 107 | spin_lock(&iort_fwnode_lock); |
| 108 | list_for_each_entry_safe(curr, tmp, &iort_fwnode_list, list) { |
| 109 | if (curr->iort_node == node) { |
| 110 | list_del(&curr->list); |
| 111 | kfree(curr); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | spin_unlock(&iort_fwnode_lock); |
| 116 | } |
| 117 | |
Hanjun Guo | 0a71d8b | 2017-10-13 15:09:47 +0800 | [diff] [blame] | 118 | /** |
| 119 | * iort_get_iort_node() - Retrieve iort_node associated with an fwnode |
| 120 | * |
| 121 | * @fwnode: fwnode associated with device to be looked-up |
| 122 | * |
| 123 | * Returns: iort_node pointer on success, NULL on failure |
| 124 | */ |
| 125 | static inline struct acpi_iort_node *iort_get_iort_node( |
| 126 | struct fwnode_handle *fwnode) |
| 127 | { |
| 128 | struct iort_fwnode *curr; |
| 129 | struct acpi_iort_node *iort_node = NULL; |
| 130 | |
| 131 | spin_lock(&iort_fwnode_lock); |
| 132 | list_for_each_entry(curr, &iort_fwnode_list, list) { |
| 133 | if (curr->fwnode == fwnode) { |
| 134 | iort_node = curr->iort_node; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | spin_unlock(&iort_fwnode_lock); |
| 139 | |
| 140 | return iort_node; |
| 141 | } |
| 142 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 143 | typedef acpi_status (*iort_find_node_callback) |
| 144 | (struct acpi_iort_node *node, void *context); |
| 145 | |
| 146 | /* Root pointer to the mapped IORT table */ |
| 147 | static struct acpi_table_header *iort_table; |
| 148 | |
| 149 | static LIST_HEAD(iort_msi_chip_list); |
| 150 | static DEFINE_SPINLOCK(iort_msi_chip_lock); |
| 151 | |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 152 | /** |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 153 | * iort_register_domain_token() - register domain token along with related |
| 154 | * ITS ID and base address to the list from where we can get it back later on. |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 155 | * @trans_id: ITS ID. |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 156 | * @base: ITS base address. |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 157 | * @fw_node: Domain token. |
| 158 | * |
| 159 | * Returns: 0 on success, -ENOMEM if no memory when allocating list element |
| 160 | */ |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 161 | int iort_register_domain_token(int trans_id, phys_addr_t base, |
| 162 | struct fwnode_handle *fw_node) |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 163 | { |
| 164 | struct iort_its_msi_chip *its_msi_chip; |
| 165 | |
| 166 | its_msi_chip = kzalloc(sizeof(*its_msi_chip), GFP_KERNEL); |
| 167 | if (!its_msi_chip) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | its_msi_chip->fw_node = fw_node; |
| 171 | its_msi_chip->translation_id = trans_id; |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 172 | its_msi_chip->base_addr = base; |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 173 | |
| 174 | spin_lock(&iort_msi_chip_lock); |
| 175 | list_add(&its_msi_chip->list, &iort_msi_chip_list); |
| 176 | spin_unlock(&iort_msi_chip_lock); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * iort_deregister_domain_token() - Deregister domain token based on ITS ID |
| 183 | * @trans_id: ITS ID. |
| 184 | * |
| 185 | * Returns: none. |
| 186 | */ |
| 187 | void iort_deregister_domain_token(int trans_id) |
| 188 | { |
| 189 | struct iort_its_msi_chip *its_msi_chip, *t; |
| 190 | |
| 191 | spin_lock(&iort_msi_chip_lock); |
| 192 | list_for_each_entry_safe(its_msi_chip, t, &iort_msi_chip_list, list) { |
| 193 | if (its_msi_chip->translation_id == trans_id) { |
| 194 | list_del(&its_msi_chip->list); |
| 195 | kfree(its_msi_chip); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | spin_unlock(&iort_msi_chip_lock); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * iort_find_domain_token() - Find domain token based on given ITS ID |
| 204 | * @trans_id: ITS ID. |
| 205 | * |
| 206 | * Returns: domain token when find on the list, NULL otherwise |
| 207 | */ |
| 208 | struct fwnode_handle *iort_find_domain_token(int trans_id) |
| 209 | { |
| 210 | struct fwnode_handle *fw_node = NULL; |
| 211 | struct iort_its_msi_chip *its_msi_chip; |
| 212 | |
| 213 | spin_lock(&iort_msi_chip_lock); |
| 214 | list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { |
| 215 | if (its_msi_chip->translation_id == trans_id) { |
| 216 | fw_node = its_msi_chip->fw_node; |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | spin_unlock(&iort_msi_chip_lock); |
| 221 | |
| 222 | return fw_node; |
| 223 | } |
| 224 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 225 | static struct acpi_iort_node *iort_scan_node(enum acpi_iort_node_type type, |
| 226 | iort_find_node_callback callback, |
| 227 | void *context) |
| 228 | { |
| 229 | struct acpi_iort_node *iort_node, *iort_end; |
| 230 | struct acpi_table_iort *iort; |
| 231 | int i; |
| 232 | |
| 233 | if (!iort_table) |
| 234 | return NULL; |
| 235 | |
| 236 | /* Get the first IORT node */ |
| 237 | iort = (struct acpi_table_iort *)iort_table; |
| 238 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 239 | iort->node_offset); |
| 240 | iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 241 | iort_table->length); |
| 242 | |
| 243 | for (i = 0; i < iort->node_count; i++) { |
| 244 | if (WARN_TAINT(iort_node >= iort_end, TAINT_FIRMWARE_WORKAROUND, |
| 245 | "IORT node pointer overflows, bad table!\n")) |
| 246 | return NULL; |
| 247 | |
| 248 | if (iort_node->type == type && |
| 249 | ACPI_SUCCESS(callback(iort_node, context))) |
Hanjun Guo | d89cf2e | 2017-03-07 20:39:56 +0800 | [diff] [blame] | 250 | return iort_node; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 251 | |
| 252 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, |
| 253 | iort_node->length); |
| 254 | } |
| 255 | |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | static acpi_status iort_match_node_callback(struct acpi_iort_node *node, |
| 260 | void *context) |
| 261 | { |
| 262 | struct device *dev = context; |
Hanjun Guo | c92bdfe | 2017-03-07 20:39:58 +0800 | [diff] [blame] | 263 | acpi_status status = AE_NOT_FOUND; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 264 | |
| 265 | if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT) { |
| 266 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 267 | struct acpi_device *adev = to_acpi_device_node(dev->fwnode); |
| 268 | struct acpi_iort_named_component *ncomp; |
| 269 | |
Hanjun Guo | c92bdfe | 2017-03-07 20:39:58 +0800 | [diff] [blame] | 270 | if (!adev) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 271 | goto out; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 272 | |
| 273 | status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); |
| 274 | if (ACPI_FAILURE(status)) { |
| 275 | dev_warn(dev, "Can't get device full path name\n"); |
| 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | ncomp = (struct acpi_iort_named_component *)node->node_data; |
| 280 | status = !strcmp(ncomp->device_name, buf.pointer) ? |
| 281 | AE_OK : AE_NOT_FOUND; |
| 282 | acpi_os_free(buf.pointer); |
| 283 | } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 284 | struct acpi_iort_root_complex *pci_rc; |
| 285 | struct pci_bus *bus; |
| 286 | |
| 287 | bus = to_pci_bus(dev); |
| 288 | pci_rc = (struct acpi_iort_root_complex *)node->node_data; |
| 289 | |
| 290 | /* |
| 291 | * It is assumed that PCI segment numbers maps one-to-one |
| 292 | * with root complexes. Each segment number can represent only |
| 293 | * one root complex. |
| 294 | */ |
| 295 | status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? |
| 296 | AE_OK : AE_NOT_FOUND; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 297 | } |
| 298 | out: |
| 299 | return status; |
| 300 | } |
| 301 | |
| 302 | static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, |
| 303 | u32 *rid_out) |
| 304 | { |
| 305 | /* Single mapping does not care for input id */ |
| 306 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
| 307 | if (type == ACPI_IORT_NODE_NAMED_COMPONENT || |
| 308 | type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 309 | *rid_out = map->output_base; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", |
| 314 | map, type); |
| 315 | return -ENXIO; |
| 316 | } |
| 317 | |
| 318 | if (rid_in < map->input_base || |
| 319 | (rid_in >= map->input_base + map->id_count)) |
| 320 | return -ENXIO; |
| 321 | |
| 322 | *rid_out = map->output_base + (rid_in - map->input_base); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 326 | static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, |
| 327 | u32 *id_out, int index) |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 328 | { |
| 329 | struct acpi_iort_node *parent; |
| 330 | struct acpi_iort_id_mapping *map; |
| 331 | |
| 332 | if (!node->mapping_offset || !node->mapping_count || |
| 333 | index >= node->mapping_count) |
| 334 | return NULL; |
| 335 | |
| 336 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 337 | node->mapping_offset + index * sizeof(*map)); |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 338 | |
| 339 | /* Firmware bug! */ |
| 340 | if (!map->output_reference) { |
| 341 | pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", |
| 342 | node, node->type); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 347 | map->output_reference); |
| 348 | |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 349 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 350 | if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 351 | node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 352 | node->type == ACPI_IORT_NODE_SMMU_V3 || |
| 353 | node->type == ACPI_IORT_NODE_PMCG) { |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 354 | *id_out = map->output_base; |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 355 | return parent; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | return NULL; |
| 360 | } |
| 361 | |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 362 | static int iort_get_id_mapping_index(struct acpi_iort_node *node) |
| 363 | { |
| 364 | struct acpi_iort_smmu_v3 *smmu; |
| 365 | |
| 366 | switch (node->type) { |
| 367 | case ACPI_IORT_NODE_SMMU_V3: |
| 368 | /* |
| 369 | * SMMUv3 dev ID mapping index was introduced in revision 1 |
| 370 | * table, not available in revision 0 |
| 371 | */ |
| 372 | if (node->revision < 1) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 376 | /* |
| 377 | * ID mapping index is only ignored if all interrupts are |
| 378 | * GSIV based |
| 379 | */ |
| 380 | if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv |
| 381 | && smmu->sync_gsiv) |
| 382 | return -EINVAL; |
| 383 | |
| 384 | if (smmu->id_mapping_index >= node->mapping_count) { |
| 385 | pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", |
| 386 | node, node->type); |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | |
| 390 | return smmu->id_mapping_index; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 391 | case ACPI_IORT_NODE_PMCG: |
| 392 | return 0; |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 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 | |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 572 | static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) |
| 573 | { |
| 574 | struct iort_its_msi_chip *its_msi_chip; |
| 575 | int ret = -ENODEV; |
| 576 | |
| 577 | spin_lock(&iort_msi_chip_lock); |
| 578 | list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { |
| 579 | if (its_msi_chip->translation_id == its_id) { |
| 580 | *base = its_msi_chip->base_addr; |
| 581 | ret = 0; |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | spin_unlock(&iort_msi_chip_lock); |
| 586 | |
| 587 | return ret; |
| 588 | } |
| 589 | |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 590 | /** |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 591 | * iort_dev_find_its_id() - Find the ITS identifier for a device |
| 592 | * @dev: The device. |
Hanjun Guo | 6cb6bf5 | 2017-03-07 20:39:57 +0800 | [diff] [blame] | 593 | * @req_id: Device's requester ID |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 594 | * @idx: Index of the ITS identifier list. |
| 595 | * @its_id: ITS identifier. |
| 596 | * |
| 597 | * Returns: 0 on success, appropriate error value otherwise |
| 598 | */ |
| 599 | static int iort_dev_find_its_id(struct device *dev, u32 req_id, |
| 600 | unsigned int idx, int *its_id) |
| 601 | { |
| 602 | struct acpi_iort_its_group *its; |
| 603 | struct acpi_iort_node *node; |
| 604 | |
| 605 | node = iort_find_dev_node(dev); |
| 606 | if (!node) |
| 607 | return -ENXIO; |
| 608 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 609 | node = iort_node_map_id(node, req_id, NULL, IORT_MSI_TYPE); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 610 | if (!node) |
| 611 | return -ENXIO; |
| 612 | |
| 613 | /* Move to ITS specific data */ |
| 614 | its = (struct acpi_iort_its_group *)node->node_data; |
Lorenzo Pieralisi | 5a46d3f | 2019-07-22 17:25:48 +0100 | [diff] [blame] | 615 | if (idx >= its->its_count) { |
| 616 | dev_err(dev, "requested ITS ID index [%d] overruns ITS entries [%d]\n", |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 617 | idx, its->its_count); |
| 618 | return -ENXIO; |
| 619 | } |
| 620 | |
| 621 | *its_id = its->identifiers[idx]; |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * iort_get_device_domain() - Find MSI domain related to a device |
| 627 | * @dev: The device. |
| 628 | * @req_id: Requester ID for the device. |
| 629 | * |
| 630 | * Returns: the MSI domain for this device, NULL otherwise |
| 631 | */ |
| 632 | struct irq_domain *iort_get_device_domain(struct device *dev, u32 req_id) |
| 633 | { |
| 634 | struct fwnode_handle *handle; |
| 635 | int its_id; |
| 636 | |
| 637 | if (iort_dev_find_its_id(dev, req_id, 0, &its_id)) |
| 638 | return NULL; |
| 639 | |
| 640 | handle = iort_find_domain_token(its_id); |
| 641 | if (!handle) |
| 642 | return NULL; |
| 643 | |
| 644 | return irq_find_matching_fwnode(handle, DOMAIN_BUS_PCI_MSI); |
| 645 | } |
| 646 | |
Lorenzo Pieralisi | 6563790 | 2017-10-13 15:09:50 +0800 | [diff] [blame] | 647 | static void iort_set_device_domain(struct device *dev, |
| 648 | struct acpi_iort_node *node) |
| 649 | { |
| 650 | struct acpi_iort_its_group *its; |
| 651 | struct acpi_iort_node *msi_parent; |
| 652 | struct acpi_iort_id_mapping *map; |
| 653 | struct fwnode_handle *iort_fwnode; |
| 654 | struct irq_domain *domain; |
| 655 | int index; |
| 656 | |
| 657 | index = iort_get_id_mapping_index(node); |
| 658 | if (index < 0) |
| 659 | return; |
| 660 | |
| 661 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
| 662 | node->mapping_offset + index * sizeof(*map)); |
| 663 | |
| 664 | /* Firmware bug! */ |
| 665 | if (!map->output_reference || |
| 666 | !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { |
| 667 | pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", |
| 668 | node, node->type); |
| 669 | return; |
| 670 | } |
| 671 | |
| 672 | msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 673 | map->output_reference); |
| 674 | |
| 675 | if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) |
| 676 | return; |
| 677 | |
| 678 | /* Move to ITS specific data */ |
| 679 | its = (struct acpi_iort_its_group *)msi_parent->node_data; |
| 680 | |
| 681 | iort_fwnode = iort_find_domain_token(its->identifiers[0]); |
| 682 | if (!iort_fwnode) |
| 683 | return; |
| 684 | |
| 685 | domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); |
| 686 | if (domain) |
| 687 | dev_set_msi_domain(dev, domain); |
| 688 | } |
| 689 | |
Hanjun Guo | d4f54a1 | 2017-03-07 20:40:06 +0800 | [diff] [blame] | 690 | /** |
| 691 | * iort_get_platform_device_domain() - Find MSI domain related to a |
| 692 | * platform device |
| 693 | * @dev: the dev pointer associated with the platform device |
| 694 | * |
| 695 | * Returns: the MSI domain for this device, NULL otherwise |
| 696 | */ |
| 697 | static struct irq_domain *iort_get_platform_device_domain(struct device *dev) |
| 698 | { |
Lorenzo Pieralisi | ea2412d | 2018-11-29 09:55:59 +0000 | [diff] [blame] | 699 | struct acpi_iort_node *node, *msi_parent = NULL; |
Hanjun Guo | d4f54a1 | 2017-03-07 20:40:06 +0800 | [diff] [blame] | 700 | struct fwnode_handle *iort_fwnode; |
| 701 | struct acpi_iort_its_group *its; |
| 702 | int i; |
| 703 | |
| 704 | /* find its associated iort node */ |
| 705 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 706 | iort_match_node_callback, dev); |
| 707 | if (!node) |
| 708 | return NULL; |
| 709 | |
| 710 | /* then find its msi parent node */ |
| 711 | for (i = 0; i < node->mapping_count; i++) { |
| 712 | msi_parent = iort_node_map_platform_id(node, NULL, |
| 713 | IORT_MSI_TYPE, i); |
| 714 | if (msi_parent) |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | if (!msi_parent) |
| 719 | return NULL; |
| 720 | |
| 721 | /* Move to ITS specific data */ |
| 722 | its = (struct acpi_iort_its_group *)msi_parent->node_data; |
| 723 | |
| 724 | iort_fwnode = iort_find_domain_token(its->identifiers[0]); |
| 725 | if (!iort_fwnode) |
| 726 | return NULL; |
| 727 | |
| 728 | return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); |
| 729 | } |
| 730 | |
| 731 | void acpi_configure_pmsi_domain(struct device *dev) |
| 732 | { |
| 733 | struct irq_domain *msi_domain; |
| 734 | |
| 735 | msi_domain = iort_get_platform_device_domain(dev); |
| 736 | if (msi_domain) |
| 737 | dev_set_msi_domain(dev, msi_domain); |
| 738 | } |
| 739 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 740 | static int __maybe_unused __get_pci_rid(struct pci_dev *pdev, u16 alias, |
| 741 | void *data) |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 742 | { |
| 743 | u32 *rid = data; |
| 744 | |
| 745 | *rid = alias; |
| 746 | return 0; |
| 747 | } |
| 748 | |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 749 | #ifdef CONFIG_IOMMU_API |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 750 | static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) |
| 751 | { |
| 752 | struct acpi_iort_node *iommu; |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 753 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 754 | |
| 755 | iommu = iort_get_iort_node(fwspec->iommu_fwnode); |
| 756 | |
| 757 | if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { |
| 758 | struct acpi_iort_smmu_v3 *smmu; |
| 759 | |
| 760 | smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; |
| 761 | if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) |
| 762 | return iommu; |
| 763 | } |
| 764 | |
| 765 | return NULL; |
| 766 | } |
| 767 | |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 768 | static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev) |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 769 | { |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 770 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
| 771 | |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 772 | return (fwspec && fwspec->ops) ? fwspec->ops : NULL; |
| 773 | } |
| 774 | |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 775 | static inline int iort_add_device_replay(const struct iommu_ops *ops, |
| 776 | struct device *dev) |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 777 | { |
| 778 | int err = 0; |
| 779 | |
Joerg Roedel | d2e1a00 | 2018-12-05 14:39:45 +0100 | [diff] [blame] | 780 | if (dev->bus && !device_iommu_mapped(dev)) |
| 781 | err = iommu_probe_device(dev); |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 782 | |
| 783 | return err; |
| 784 | } |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 785 | |
| 786 | /** |
| 787 | * iort_iommu_msi_get_resv_regions - Reserved region driver helper |
| 788 | * @dev: Device from iommu_get_resv_regions() |
| 789 | * @head: Reserved region list from iommu_get_resv_regions() |
| 790 | * |
| 791 | * Returns: Number of msi reserved regions on success (0 if platform |
| 792 | * doesn't require the reservation or no associated msi regions), |
| 793 | * appropriate error value otherwise. The ITS interrupt translation |
| 794 | * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device |
| 795 | * are the msi reserved regions. |
| 796 | */ |
| 797 | int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) |
| 798 | { |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 799 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 800 | struct acpi_iort_its_group *its; |
| 801 | struct acpi_iort_node *iommu_node, *its_node = NULL; |
| 802 | int i, resv = 0; |
| 803 | |
| 804 | iommu_node = iort_get_msi_resv_iommu(dev); |
| 805 | if (!iommu_node) |
| 806 | return 0; |
| 807 | |
| 808 | /* |
| 809 | * Current logic to reserve ITS regions relies on HW topologies |
| 810 | * where a given PCI or named component maps its IDs to only one |
| 811 | * ITS group; if a PCI or named component can map its IDs to |
| 812 | * different ITS groups through IORT mappings this function has |
| 813 | * to be reworked to ensure we reserve regions for all ITS groups |
| 814 | * a given PCI or named component may map IDs to. |
| 815 | */ |
| 816 | |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 817 | for (i = 0; i < fwspec->num_ids; i++) { |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 818 | its_node = iort_node_map_id(iommu_node, |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 819 | fwspec->ids[i], |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 820 | NULL, IORT_MSI_TYPE); |
| 821 | if (its_node) |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | if (!its_node) |
| 826 | return 0; |
| 827 | |
| 828 | /* Move to ITS specific data */ |
| 829 | its = (struct acpi_iort_its_group *)its_node->node_data; |
| 830 | |
| 831 | for (i = 0; i < its->its_count; i++) { |
| 832 | phys_addr_t base; |
| 833 | |
| 834 | if (!iort_find_its_base(its->identifiers[i], &base)) { |
| 835 | int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; |
| 836 | struct iommu_resv_region *region; |
| 837 | |
| 838 | region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, |
| 839 | prot, IOMMU_RESV_MSI); |
| 840 | if (region) { |
| 841 | list_add_tail(®ion->list, head); |
| 842 | resv++; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | return (resv == its->its_count) ? resv : -ENODEV; |
| 848 | } |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 849 | |
| 850 | static inline bool iort_iommu_driver_enabled(u8 type) |
| 851 | { |
| 852 | switch (type) { |
| 853 | case ACPI_IORT_NODE_SMMU_V3: |
Ard Biesheuvel | d3daf66 | 2019-12-19 12:03:48 +0000 | [diff] [blame] | 854 | return IS_ENABLED(CONFIG_ARM_SMMU_V3); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 855 | case ACPI_IORT_NODE_SMMU: |
Ard Biesheuvel | d3daf66 | 2019-12-19 12:03:48 +0000 | [diff] [blame] | 856 | return IS_ENABLED(CONFIG_ARM_SMMU); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 857 | default: |
| 858 | pr_warn("IORT node type %u does not describe an SMMU\n", type); |
| 859 | return false; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | static int arm_smmu_iort_xlate(struct device *dev, u32 streamid, |
| 864 | struct fwnode_handle *fwnode, |
| 865 | const struct iommu_ops *ops) |
| 866 | { |
| 867 | int ret = iommu_fwspec_init(dev, fwnode, ops); |
| 868 | |
| 869 | if (!ret) |
| 870 | ret = iommu_fwspec_add_ids(dev, &streamid, 1); |
| 871 | |
| 872 | return ret; |
| 873 | } |
| 874 | |
| 875 | static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) |
| 876 | { |
| 877 | struct acpi_iort_root_complex *pci_rc; |
| 878 | |
| 879 | pci_rc = (struct acpi_iort_root_complex *)node->node_data; |
| 880 | return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; |
| 881 | } |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 882 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 883 | static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, |
| 884 | u32 streamid) |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 885 | { |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 886 | const struct iommu_ops *ops; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 887 | struct fwnode_handle *iort_fwnode; |
| 888 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 889 | if (!node) |
| 890 | return -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 891 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 892 | iort_fwnode = iort_get_fwnode(node); |
| 893 | if (!iort_fwnode) |
| 894 | return -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 895 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 896 | /* |
| 897 | * If the ops look-up fails, this means that either |
| 898 | * the SMMU drivers have not been probed yet or that |
| 899 | * the SMMU drivers are not built in the kernel; |
| 900 | * Depending on whether the SMMU drivers are built-in |
| 901 | * in the kernel or not, defer the IOMMU configuration |
| 902 | * or just abort it. |
| 903 | */ |
| 904 | ops = iommu_ops_from_fwnode(iort_fwnode); |
| 905 | if (!ops) |
| 906 | return iort_iommu_driver_enabled(node->type) ? |
| 907 | -EPROBE_DEFER : -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 908 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 909 | return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops); |
| 910 | } |
| 911 | |
| 912 | struct iort_pci_alias_info { |
| 913 | struct device *dev; |
| 914 | struct acpi_iort_node *node; |
| 915 | }; |
| 916 | |
| 917 | static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) |
| 918 | { |
| 919 | struct iort_pci_alias_info *info = data; |
| 920 | struct acpi_iort_node *parent; |
| 921 | u32 streamid; |
| 922 | |
| 923 | parent = iort_node_map_id(info->node, alias, &streamid, |
| 924 | IORT_IOMMU_TYPE); |
| 925 | return iort_iommu_xlate(info->dev, parent, streamid); |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Jean-Philippe Brucker | da22565 | 2020-01-15 13:52:30 +0100 | [diff] [blame^] | 928 | static void iort_named_component_init(struct device *dev, |
| 929 | struct acpi_iort_node *node) |
| 930 | { |
| 931 | struct acpi_iort_named_component *nc; |
| 932 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
| 933 | |
| 934 | if (!fwspec) |
| 935 | return; |
| 936 | |
| 937 | nc = (struct acpi_iort_named_component *)node->node_data; |
| 938 | fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS, |
| 939 | nc->node_flags); |
| 940 | } |
| 941 | |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 942 | /** |
| 943 | * iort_iommu_configure - Set-up IOMMU configuration for a device. |
| 944 | * |
| 945 | * @dev: device to configure |
| 946 | * |
| 947 | * Returns: iommu_ops pointer on configuration success |
| 948 | * NULL on configuration failure |
| 949 | */ |
| 950 | const struct iommu_ops *iort_iommu_configure(struct device *dev) |
| 951 | { |
| 952 | struct acpi_iort_node *node, *parent; |
| 953 | const struct iommu_ops *ops; |
| 954 | u32 streamid = 0; |
| 955 | int err = -ENODEV; |
| 956 | |
| 957 | /* |
| 958 | * If we already translated the fwspec there |
| 959 | * is nothing left to do, return the iommu_ops. |
| 960 | */ |
| 961 | ops = iort_fwspec_iommu_ops(dev); |
| 962 | if (ops) |
| 963 | return ops; |
| 964 | |
| 965 | if (dev_is_pci(dev)) { |
| 966 | struct pci_bus *bus = to_pci_dev(dev)->bus; |
| 967 | struct iort_pci_alias_info info = { .dev = dev }; |
| 968 | |
| 969 | node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
| 970 | iort_match_node_callback, &bus->dev); |
| 971 | if (!node) |
| 972 | return NULL; |
| 973 | |
| 974 | info.node = node; |
| 975 | err = pci_for_each_dma_alias(to_pci_dev(dev), |
| 976 | iort_pci_iommu_init, &info); |
| 977 | |
| 978 | if (!err && iort_pci_rc_supports_ats(node)) |
| 979 | dev->iommu_fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; |
| 980 | } else { |
| 981 | int i = 0; |
| 982 | |
| 983 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 984 | iort_match_node_callback, dev); |
| 985 | if (!node) |
| 986 | return NULL; |
| 987 | |
| 988 | do { |
| 989 | parent = iort_node_map_platform_id(node, &streamid, |
| 990 | IORT_IOMMU_TYPE, |
| 991 | i++); |
| 992 | |
| 993 | if (parent) |
| 994 | err = iort_iommu_xlate(dev, parent, streamid); |
| 995 | } while (parent && !err); |
Jean-Philippe Brucker | da22565 | 2020-01-15 13:52:30 +0100 | [diff] [blame^] | 996 | |
| 997 | if (!err) |
| 998 | iort_named_component_init(dev, node); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * If we have reason to believe the IOMMU driver missed the initial |
| 1003 | * add_device callback for dev, replay it to get things in order. |
| 1004 | */ |
| 1005 | if (!err) { |
| 1006 | ops = iort_fwspec_iommu_ops(dev); |
| 1007 | err = iort_add_device_replay(ops, dev); |
| 1008 | } |
| 1009 | |
| 1010 | /* Ignore all other errors apart from EPROBE_DEFER */ |
| 1011 | if (err == -EPROBE_DEFER) { |
| 1012 | ops = ERR_PTR(err); |
| 1013 | } else if (err) { |
| 1014 | dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); |
| 1015 | ops = NULL; |
| 1016 | } |
| 1017 | |
| 1018 | return ops; |
| 1019 | } |
| 1020 | #else |
| 1021 | static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev) |
| 1022 | { return NULL; } |
| 1023 | static inline int iort_add_device_replay(const struct iommu_ops *ops, |
| 1024 | struct device *dev) |
| 1025 | { return 0; } |
| 1026 | int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) |
| 1027 | { return 0; } |
| 1028 | const struct iommu_ops *iort_iommu_configure(struct device *dev) |
| 1029 | { return NULL; } |
| 1030 | #endif |
| 1031 | |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1032 | static int nc_dma_get_range(struct device *dev, u64 *size) |
| 1033 | { |
| 1034 | struct acpi_iort_node *node; |
| 1035 | struct acpi_iort_named_component *ncomp; |
| 1036 | |
| 1037 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 1038 | iort_match_node_callback, dev); |
| 1039 | if (!node) |
| 1040 | return -ENODEV; |
| 1041 | |
| 1042 | ncomp = (struct acpi_iort_named_component *)node->node_data; |
| 1043 | |
| 1044 | *size = ncomp->memory_address_limit >= 64 ? U64_MAX : |
| 1045 | 1ULL<<ncomp->memory_address_limit; |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1050 | static int rc_dma_get_range(struct device *dev, u64 *size) |
| 1051 | { |
| 1052 | struct acpi_iort_node *node; |
| 1053 | struct acpi_iort_root_complex *rc; |
Jean-Philippe Brucker | c777723 | 2019-01-10 18:41:51 +0000 | [diff] [blame] | 1054 | struct pci_bus *pbus = to_pci_dev(dev)->bus; |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1055 | |
| 1056 | node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
Jean-Philippe Brucker | c777723 | 2019-01-10 18:41:51 +0000 | [diff] [blame] | 1057 | iort_match_node_callback, &pbus->dev); |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1058 | if (!node || node->revision < 1) |
| 1059 | return -ENODEV; |
| 1060 | |
| 1061 | rc = (struct acpi_iort_root_complex *)node->node_data; |
| 1062 | |
| 1063 | *size = rc->memory_address_limit >= 64 ? U64_MAX : |
| 1064 | 1ULL<<rc->memory_address_limit; |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 1069 | /** |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1070 | * iort_dma_setup() - Set-up device DMA parameters. |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1071 | * |
| 1072 | * @dev: device to configure |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1073 | * @dma_addr: device DMA address result pointer |
| 1074 | * @size: DMA range size result pointer |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1075 | */ |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1076 | 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] | 1077 | { |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1078 | u64 end, mask, dmaaddr = 0, size = 0, offset = 0; |
| 1079 | int ret; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1080 | |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1081 | /* |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1082 | * If @dev is expected to be DMA-capable then the bus code that created |
| 1083 | * it should have initialised its dma_mask pointer by this point. For |
| 1084 | * now, we'll continue the legacy behaviour of coercing it to the |
| 1085 | * coherent mask if not, but we'll no longer do so quietly. |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1086 | */ |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1087 | if (!dev->dma_mask) { |
| 1088 | dev_warn(dev, "DMA mask not set\n"); |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1089 | dev->dma_mask = &dev->coherent_dma_mask; |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1090 | } |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1091 | |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1092 | if (dev->coherent_dma_mask) |
| 1093 | size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); |
| 1094 | else |
| 1095 | size = 1ULL << 32; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1096 | |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1097 | if (dev_is_pci(dev)) { |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1098 | ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size); |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1099 | if (ret == -ENODEV) |
| 1100 | ret = rc_dma_get_range(dev, &size); |
| 1101 | } else { |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1102 | ret = nc_dma_get_range(dev, &size); |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1103 | } |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1104 | |
| 1105 | if (!ret) { |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1106 | /* |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1107 | * Limit coherent and dma mask based on size retrieved from |
| 1108 | * firmware. |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1109 | */ |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1110 | end = dmaaddr + size - 1; |
| 1111 | mask = DMA_BIT_MASK(ilog2(end) + 1); |
| 1112 | dev->bus_dma_limit = end; |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1113 | dev->coherent_dma_mask = mask; |
| 1114 | *dev->dma_mask = mask; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | *dma_addr = dmaaddr; |
| 1118 | *dma_size = size; |
| 1119 | |
| 1120 | dev->dma_pfn_offset = PFN_DOWN(offset); |
| 1121 | dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset); |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1124 | static void __init acpi_iort_register_irq(int hwirq, const char *name, |
| 1125 | int trigger, |
| 1126 | struct resource *res) |
| 1127 | { |
| 1128 | int irq = acpi_register_gsi(NULL, hwirq, trigger, |
| 1129 | ACPI_ACTIVE_HIGH); |
| 1130 | |
| 1131 | if (irq <= 0) { |
| 1132 | pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, |
| 1133 | name); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | res->start = irq; |
| 1138 | res->end = irq; |
| 1139 | res->flags = IORESOURCE_IRQ; |
| 1140 | res->name = name; |
| 1141 | } |
| 1142 | |
| 1143 | static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) |
| 1144 | { |
| 1145 | struct acpi_iort_smmu_v3 *smmu; |
| 1146 | /* Always present mem resource */ |
| 1147 | int num_res = 1; |
| 1148 | |
| 1149 | /* Retrieve SMMUv3 specific data */ |
| 1150 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1151 | |
| 1152 | if (smmu->event_gsiv) |
| 1153 | num_res++; |
| 1154 | |
| 1155 | if (smmu->pri_gsiv) |
| 1156 | num_res++; |
| 1157 | |
| 1158 | if (smmu->gerr_gsiv) |
| 1159 | num_res++; |
| 1160 | |
| 1161 | if (smmu->sync_gsiv) |
| 1162 | num_res++; |
| 1163 | |
| 1164 | return num_res; |
| 1165 | } |
| 1166 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1167 | static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) |
| 1168 | { |
| 1169 | /* |
| 1170 | * Cavium ThunderX2 implementation doesn't not support unique |
| 1171 | * irq line. Use single irq line for all the SMMUv3 interrupts. |
| 1172 | */ |
| 1173 | if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 1174 | return false; |
| 1175 | |
| 1176 | /* |
| 1177 | * ThunderX2 doesn't support MSIs from the SMMU, so we're checking |
| 1178 | * SPI numbers here. |
| 1179 | */ |
| 1180 | return smmu->event_gsiv == smmu->pri_gsiv && |
| 1181 | smmu->event_gsiv == smmu->gerr_gsiv && |
| 1182 | smmu->event_gsiv == smmu->sync_gsiv; |
| 1183 | } |
| 1184 | |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 1185 | static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) |
| 1186 | { |
| 1187 | /* |
| 1188 | * Override the size, for Cavium ThunderX2 implementation |
| 1189 | * which doesn't support the page 1 SMMU register space. |
| 1190 | */ |
| 1191 | if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 1192 | return SZ_64K; |
| 1193 | |
| 1194 | return SZ_128K; |
| 1195 | } |
| 1196 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1197 | static void __init arm_smmu_v3_init_resources(struct resource *res, |
| 1198 | struct acpi_iort_node *node) |
| 1199 | { |
| 1200 | struct acpi_iort_smmu_v3 *smmu; |
| 1201 | int num_res = 0; |
| 1202 | |
| 1203 | /* Retrieve SMMUv3 specific data */ |
| 1204 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1205 | |
| 1206 | res[num_res].start = smmu->base_address; |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 1207 | res[num_res].end = smmu->base_address + |
| 1208 | arm_smmu_v3_resource_size(smmu) - 1; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1209 | res[num_res].flags = IORESOURCE_MEM; |
| 1210 | |
| 1211 | num_res++; |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1212 | if (arm_smmu_v3_is_combined_irq(smmu)) { |
| 1213 | if (smmu->event_gsiv) |
| 1214 | acpi_iort_register_irq(smmu->event_gsiv, "combined", |
| 1215 | ACPI_EDGE_SENSITIVE, |
| 1216 | &res[num_res++]); |
| 1217 | } else { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1218 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1219 | if (smmu->event_gsiv) |
| 1220 | acpi_iort_register_irq(smmu->event_gsiv, "eventq", |
| 1221 | ACPI_EDGE_SENSITIVE, |
| 1222 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1223 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1224 | if (smmu->pri_gsiv) |
| 1225 | acpi_iort_register_irq(smmu->pri_gsiv, "priq", |
| 1226 | ACPI_EDGE_SENSITIVE, |
| 1227 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1228 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1229 | if (smmu->gerr_gsiv) |
| 1230 | acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", |
| 1231 | ACPI_EDGE_SENSITIVE, |
| 1232 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1233 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1234 | if (smmu->sync_gsiv) |
| 1235 | acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", |
| 1236 | ACPI_EDGE_SENSITIVE, |
| 1237 | &res[num_res++]); |
| 1238 | } |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1241 | static void __init arm_smmu_v3_dma_configure(struct device *dev, |
| 1242 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1243 | { |
| 1244 | struct acpi_iort_smmu_v3 *smmu; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1245 | enum dev_dma_attr attr; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1246 | |
| 1247 | /* Retrieve SMMUv3 specific data */ |
| 1248 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1249 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1250 | attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? |
| 1251 | DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; |
| 1252 | |
| 1253 | /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ |
| 1254 | dev->dma_mask = &dev->coherent_dma_mask; |
| 1255 | |
| 1256 | /* Configure DMA for the page table walker */ |
| 1257 | acpi_dma_configure(dev, attr); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Lorenzo Pieralisi | 7580813 | 2017-09-28 13:57:10 +0100 | [diff] [blame] | 1260 | #if defined(CONFIG_ACPI_NUMA) |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1261 | /* |
| 1262 | * set numa proximity domain for smmuv3 device |
| 1263 | */ |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1264 | static int __init arm_smmu_v3_set_proximity(struct device *dev, |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1265 | struct acpi_iort_node *node) |
| 1266 | { |
| 1267 | struct acpi_iort_smmu_v3 *smmu; |
| 1268 | |
| 1269 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1270 | if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1271 | int dev_node = acpi_map_pxm_to_node(smmu->pxm); |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1272 | |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1273 | if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1274 | return -EINVAL; |
| 1275 | |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1276 | set_dev_node(dev, dev_node); |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1277 | pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", |
| 1278 | smmu->base_address, |
| 1279 | smmu->pxm); |
| 1280 | } |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1281 | return 0; |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1282 | } |
| 1283 | #else |
| 1284 | #define arm_smmu_v3_set_proximity NULL |
| 1285 | #endif |
| 1286 | |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1287 | static int __init arm_smmu_count_resources(struct acpi_iort_node *node) |
| 1288 | { |
| 1289 | struct acpi_iort_smmu *smmu; |
| 1290 | |
| 1291 | /* Retrieve SMMU specific data */ |
| 1292 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1293 | |
| 1294 | /* |
| 1295 | * Only consider the global fault interrupt and ignore the |
| 1296 | * configuration access interrupt. |
| 1297 | * |
| 1298 | * MMIO address and global fault interrupt resources are always |
| 1299 | * present so add them to the context interrupt count as a static |
| 1300 | * value. |
| 1301 | */ |
| 1302 | return smmu->context_interrupt_count + 2; |
| 1303 | } |
| 1304 | |
| 1305 | static void __init arm_smmu_init_resources(struct resource *res, |
| 1306 | struct acpi_iort_node *node) |
| 1307 | { |
| 1308 | struct acpi_iort_smmu *smmu; |
| 1309 | int i, hw_irq, trigger, num_res = 0; |
| 1310 | u64 *ctx_irq, *glb_irq; |
| 1311 | |
| 1312 | /* Retrieve SMMU specific data */ |
| 1313 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1314 | |
| 1315 | res[num_res].start = smmu->base_address; |
| 1316 | res[num_res].end = smmu->base_address + smmu->span - 1; |
| 1317 | res[num_res].flags = IORESOURCE_MEM; |
| 1318 | num_res++; |
| 1319 | |
| 1320 | glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); |
| 1321 | /* Global IRQs */ |
| 1322 | hw_irq = IORT_IRQ_MASK(glb_irq[0]); |
| 1323 | trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); |
| 1324 | |
| 1325 | acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, |
| 1326 | &res[num_res++]); |
| 1327 | |
| 1328 | /* Context IRQs */ |
| 1329 | ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); |
| 1330 | for (i = 0; i < smmu->context_interrupt_count; i++) { |
| 1331 | hw_irq = IORT_IRQ_MASK(ctx_irq[i]); |
| 1332 | trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); |
| 1333 | |
| 1334 | acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, |
| 1335 | &res[num_res++]); |
| 1336 | } |
| 1337 | } |
| 1338 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1339 | static void __init arm_smmu_dma_configure(struct device *dev, |
| 1340 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1341 | { |
| 1342 | struct acpi_iort_smmu *smmu; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1343 | enum dev_dma_attr attr; |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* Retrieve SMMU specific data */ |
| 1346 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1347 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1348 | attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? |
| 1349 | DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; |
| 1350 | |
| 1351 | /* We expect the dma masks to be equivalent for SMMU set-ups */ |
| 1352 | dev->dma_mask = &dev->coherent_dma_mask; |
| 1353 | |
| 1354 | /* Configure DMA for the page table walker */ |
| 1355 | acpi_dma_configure(dev, attr); |
| 1356 | } |
| 1357 | |
| 1358 | static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) |
| 1359 | { |
| 1360 | struct acpi_iort_pmcg *pmcg; |
| 1361 | |
| 1362 | /* Retrieve PMCG specific data */ |
| 1363 | pmcg = (struct acpi_iort_pmcg *)node->node_data; |
| 1364 | |
| 1365 | /* |
| 1366 | * There are always 2 memory resources. |
| 1367 | * If the overflow_gsiv is present then add that for a total of 3. |
| 1368 | */ |
| 1369 | return pmcg->overflow_gsiv ? 3 : 2; |
| 1370 | } |
| 1371 | |
| 1372 | static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, |
| 1373 | struct acpi_iort_node *node) |
| 1374 | { |
| 1375 | struct acpi_iort_pmcg *pmcg; |
| 1376 | |
| 1377 | /* Retrieve PMCG specific data */ |
| 1378 | pmcg = (struct acpi_iort_pmcg *)node->node_data; |
| 1379 | |
| 1380 | res[0].start = pmcg->page0_base_address; |
| 1381 | res[0].end = pmcg->page0_base_address + SZ_4K - 1; |
| 1382 | res[0].flags = IORESOURCE_MEM; |
| 1383 | res[1].start = pmcg->page1_base_address; |
| 1384 | res[1].end = pmcg->page1_base_address + SZ_4K - 1; |
| 1385 | res[1].flags = IORESOURCE_MEM; |
| 1386 | |
| 1387 | if (pmcg->overflow_gsiv) |
| 1388 | acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", |
| 1389 | ACPI_EDGE_SENSITIVE, &res[2]); |
| 1390 | } |
| 1391 | |
Shameer Kolothum | 24062fe | 2019-03-26 15:17:53 +0000 | [diff] [blame] | 1392 | static struct acpi_platform_list pmcg_plat_info[] __initdata = { |
| 1393 | /* HiSilicon Hip08 Platform */ |
| 1394 | {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, |
| 1395 | "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08}, |
| 1396 | { } |
| 1397 | }; |
| 1398 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1399 | static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) |
| 1400 | { |
Shameer Kolothum | 24062fe | 2019-03-26 15:17:53 +0000 | [diff] [blame] | 1401 | u32 model; |
| 1402 | int idx; |
| 1403 | |
| 1404 | idx = acpi_match_platform_list(pmcg_plat_info); |
| 1405 | if (idx >= 0) |
| 1406 | model = pmcg_plat_info[idx].data; |
| 1407 | else |
| 1408 | model = IORT_SMMU_V3_PMCG_GENERIC; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1409 | |
| 1410 | return platform_device_add_data(pdev, &model, sizeof(model)); |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1413 | struct iort_dev_config { |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1414 | const char *name; |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1415 | int (*dev_init)(struct acpi_iort_node *node); |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1416 | void (*dev_dma_configure)(struct device *dev, |
| 1417 | struct acpi_iort_node *node); |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1418 | int (*dev_count_resources)(struct acpi_iort_node *node); |
| 1419 | void (*dev_init_resources)(struct resource *res, |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1420 | struct acpi_iort_node *node); |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1421 | int (*dev_set_proximity)(struct device *dev, |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1422 | struct acpi_iort_node *node); |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1423 | int (*dev_add_platdata)(struct platform_device *pdev); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1424 | }; |
| 1425 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1426 | static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1427 | .name = "arm-smmu-v3", |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1428 | .dev_dma_configure = arm_smmu_v3_dma_configure, |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1429 | .dev_count_resources = arm_smmu_v3_count_resources, |
| 1430 | .dev_init_resources = arm_smmu_v3_init_resources, |
| 1431 | .dev_set_proximity = arm_smmu_v3_set_proximity, |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1432 | }; |
| 1433 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1434 | static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1435 | .name = "arm-smmu", |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1436 | .dev_dma_configure = arm_smmu_dma_configure, |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1437 | .dev_count_resources = arm_smmu_count_resources, |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1438 | .dev_init_resources = arm_smmu_init_resources, |
| 1439 | }; |
| 1440 | |
| 1441 | static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { |
| 1442 | .name = "arm-smmu-v3-pmcg", |
| 1443 | .dev_count_resources = arm_smmu_v3_pmcg_count_resources, |
| 1444 | .dev_init_resources = arm_smmu_v3_pmcg_init_resources, |
| 1445 | .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1446 | }; |
| 1447 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1448 | static __init const struct iort_dev_config *iort_get_dev_cfg( |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 1449 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1450 | { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1451 | switch (node->type) { |
| 1452 | case ACPI_IORT_NODE_SMMU_V3: |
| 1453 | return &iort_arm_smmu_v3_cfg; |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1454 | case ACPI_IORT_NODE_SMMU: |
| 1455 | return &iort_arm_smmu_cfg; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1456 | case ACPI_IORT_NODE_PMCG: |
| 1457 | return &iort_arm_smmu_v3_pmcg_cfg; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1458 | default: |
| 1459 | return NULL; |
| 1460 | } |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | /** |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1464 | * iort_add_platform_device() - Allocate a platform device for IORT node |
| 1465 | * @node: Pointer to device ACPI IORT node |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1466 | * |
| 1467 | * Returns: 0 on success, <0 failure |
| 1468 | */ |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1469 | static int __init iort_add_platform_device(struct acpi_iort_node *node, |
| 1470 | const struct iort_dev_config *ops) |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1471 | { |
| 1472 | struct fwnode_handle *fwnode; |
| 1473 | struct platform_device *pdev; |
| 1474 | struct resource *r; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1475 | int ret, count; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1476 | |
| 1477 | pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); |
| 1478 | if (!pdev) |
Dan Carpenter | 5e5afa6 | 2017-01-17 16:36:23 +0300 | [diff] [blame] | 1479 | return -ENOMEM; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1480 | |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1481 | if (ops->dev_set_proximity) { |
| 1482 | ret = ops->dev_set_proximity(&pdev->dev, node); |
| 1483 | if (ret) |
| 1484 | goto dev_put; |
| 1485 | } |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1486 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1487 | count = ops->dev_count_resources(node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1488 | |
| 1489 | r = kcalloc(count, sizeof(*r), GFP_KERNEL); |
| 1490 | if (!r) { |
| 1491 | ret = -ENOMEM; |
| 1492 | goto dev_put; |
| 1493 | } |
| 1494 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1495 | ops->dev_init_resources(r, node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1496 | |
| 1497 | ret = platform_device_add_resources(pdev, r, count); |
| 1498 | /* |
| 1499 | * Resources are duplicated in platform_device_add_resources, |
| 1500 | * free their allocated memory |
| 1501 | */ |
| 1502 | kfree(r); |
| 1503 | |
| 1504 | if (ret) |
| 1505 | goto dev_put; |
| 1506 | |
| 1507 | /* |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1508 | * Platform devices based on PMCG nodes uses platform_data to |
| 1509 | * pass the hardware model info to the driver. For others, add |
| 1510 | * a copy of IORT node pointer to platform_data to be used to |
| 1511 | * retrieve IORT data information. |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1512 | */ |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1513 | if (ops->dev_add_platdata) |
| 1514 | ret = ops->dev_add_platdata(pdev); |
| 1515 | else |
| 1516 | ret = platform_device_add_data(pdev, &node, sizeof(node)); |
| 1517 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1518 | if (ret) |
| 1519 | goto dev_put; |
| 1520 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1521 | fwnode = iort_get_fwnode(node); |
| 1522 | |
| 1523 | if (!fwnode) { |
| 1524 | ret = -ENODEV; |
| 1525 | goto dev_put; |
| 1526 | } |
| 1527 | |
| 1528 | pdev->dev.fwnode = fwnode; |
| 1529 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1530 | if (ops->dev_dma_configure) |
| 1531 | ops->dev_dma_configure(&pdev->dev, node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1532 | |
Lorenzo Pieralisi | 6563790 | 2017-10-13 15:09:50 +0800 | [diff] [blame] | 1533 | iort_set_device_domain(&pdev->dev, node); |
| 1534 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1535 | ret = platform_device_add(pdev); |
| 1536 | if (ret) |
| 1537 | goto dma_deconfigure; |
| 1538 | |
| 1539 | return 0; |
| 1540 | |
| 1541 | dma_deconfigure: |
Christoph Hellwig | dc3c055 | 2018-08-24 10:28:18 +0200 | [diff] [blame] | 1542 | arch_teardown_dma_ops(&pdev->dev); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1543 | dev_put: |
| 1544 | platform_device_put(pdev); |
| 1545 | |
| 1546 | return ret; |
| 1547 | } |
| 1548 | |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1549 | #ifdef CONFIG_PCI |
| 1550 | static void __init iort_enable_acs(struct acpi_iort_node *iort_node) |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1551 | { |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1552 | static bool acs_enabled __initdata; |
| 1553 | |
| 1554 | if (acs_enabled) |
| 1555 | return; |
| 1556 | |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1557 | if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 1558 | struct acpi_iort_node *parent; |
| 1559 | struct acpi_iort_id_mapping *map; |
| 1560 | int i; |
| 1561 | |
| 1562 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, |
| 1563 | iort_node->mapping_offset); |
| 1564 | |
| 1565 | for (i = 0; i < iort_node->mapping_count; i++, map++) { |
| 1566 | if (!map->output_reference) |
| 1567 | continue; |
| 1568 | |
| 1569 | parent = ACPI_ADD_PTR(struct acpi_iort_node, |
| 1570 | iort_table, map->output_reference); |
| 1571 | /* |
| 1572 | * If we detect a RC->SMMU mapping, make sure |
| 1573 | * we enable ACS on the system. |
| 1574 | */ |
| 1575 | if ((parent->type == ACPI_IORT_NODE_SMMU) || |
| 1576 | (parent->type == ACPI_IORT_NODE_SMMU_V3)) { |
| 1577 | pci_request_acs(); |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1578 | acs_enabled = true; |
| 1579 | return; |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | } |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1583 | } |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1584 | #else |
| 1585 | static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } |
| 1586 | #endif |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1587 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1588 | static void __init iort_init_platform_devices(void) |
| 1589 | { |
| 1590 | struct acpi_iort_node *iort_node, *iort_end; |
| 1591 | struct acpi_table_iort *iort; |
| 1592 | struct fwnode_handle *fwnode; |
| 1593 | int i, ret; |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1594 | const struct iort_dev_config *ops; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1595 | |
| 1596 | /* |
| 1597 | * iort_table and iort both point to the start of IORT table, but |
| 1598 | * have different struct types |
| 1599 | */ |
| 1600 | iort = (struct acpi_table_iort *)iort_table; |
| 1601 | |
| 1602 | /* Get the first IORT node */ |
| 1603 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1604 | iort->node_offset); |
| 1605 | iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1606 | iort_table->length); |
| 1607 | |
| 1608 | for (i = 0; i < iort->node_count; i++) { |
| 1609 | if (iort_node >= iort_end) { |
| 1610 | pr_err("iort node pointer overflows, bad table\n"); |
| 1611 | return; |
| 1612 | } |
| 1613 | |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1614 | iort_enable_acs(iort_node); |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1615 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1616 | ops = iort_get_dev_cfg(iort_node); |
| 1617 | if (ops) { |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1618 | fwnode = acpi_alloc_fwnode_static(); |
| 1619 | if (!fwnode) |
| 1620 | return; |
| 1621 | |
| 1622 | iort_set_fwnode(iort_node, fwnode); |
| 1623 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1624 | ret = iort_add_platform_device(iort_node, ops); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1625 | if (ret) { |
| 1626 | iort_delete_fwnode(iort_node); |
| 1627 | acpi_free_fwnode_static(fwnode); |
| 1628 | return; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, |
| 1633 | iort_node->length); |
| 1634 | } |
| 1635 | } |
| 1636 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1637 | void __init acpi_iort_init(void) |
| 1638 | { |
| 1639 | acpi_status status; |
| 1640 | |
| 1641 | status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1642 | if (ACPI_FAILURE(status)) { |
| 1643 | if (status != AE_NOT_FOUND) { |
| 1644 | const char *msg = acpi_format_exception(status); |
| 1645 | |
| 1646 | pr_err("Failed to get table, %s\n", msg); |
| 1647 | } |
| 1648 | |
| 1649 | return; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1650 | } |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1651 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1652 | iort_init_platform_devices(); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1653 | } |