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 }; |
Lorenzo Pieralisi | 07d2e59 | 2020-06-19 09:20:02 +0100 | [diff] [blame] | 267 | struct acpi_device *adev; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 268 | struct acpi_iort_named_component *ncomp; |
Lorenzo Pieralisi | 07d2e59 | 2020-06-19 09:20:02 +0100 | [diff] [blame] | 269 | struct device *nc_dev = dev; |
| 270 | |
| 271 | /* |
| 272 | * Walk the device tree to find a device with an |
| 273 | * ACPI companion; there is no point in scanning |
| 274 | * IORT for a device matching a named component if |
| 275 | * the device does not have an ACPI companion to |
| 276 | * start with. |
| 277 | */ |
| 278 | do { |
| 279 | adev = ACPI_COMPANION(nc_dev); |
| 280 | if (adev) |
| 281 | break; |
| 282 | |
| 283 | nc_dev = nc_dev->parent; |
| 284 | } while (nc_dev); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 285 | |
Hanjun Guo | c92bdfe | 2017-03-07 20:39:58 +0800 | [diff] [blame] | 286 | if (!adev) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 287 | goto out; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 288 | |
| 289 | status = acpi_get_name(adev->handle, ACPI_FULL_PATHNAME, &buf); |
| 290 | if (ACPI_FAILURE(status)) { |
Lorenzo Pieralisi | 07d2e59 | 2020-06-19 09:20:02 +0100 | [diff] [blame] | 291 | dev_warn(nc_dev, "Can't get device full path name\n"); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | ncomp = (struct acpi_iort_named_component *)node->node_data; |
| 296 | status = !strcmp(ncomp->device_name, buf.pointer) ? |
| 297 | AE_OK : AE_NOT_FOUND; |
| 298 | acpi_os_free(buf.pointer); |
| 299 | } else if (node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 300 | struct acpi_iort_root_complex *pci_rc; |
| 301 | struct pci_bus *bus; |
| 302 | |
| 303 | bus = to_pci_bus(dev); |
| 304 | pci_rc = (struct acpi_iort_root_complex *)node->node_data; |
| 305 | |
| 306 | /* |
| 307 | * It is assumed that PCI segment numbers maps one-to-one |
| 308 | * with root complexes. Each segment number can represent only |
| 309 | * one root complex. |
| 310 | */ |
| 311 | status = pci_rc->pci_segment_number == pci_domain_nr(bus) ? |
| 312 | AE_OK : AE_NOT_FOUND; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 313 | } |
| 314 | out: |
| 315 | return status; |
| 316 | } |
| 317 | |
| 318 | static int iort_id_map(struct acpi_iort_id_mapping *map, u8 type, u32 rid_in, |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 319 | u32 *rid_out, bool check_overlap) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 320 | { |
| 321 | /* Single mapping does not care for input id */ |
| 322 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
| 323 | if (type == ACPI_IORT_NODE_NAMED_COMPONENT || |
| 324 | type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 325 | *rid_out = map->output_base; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | pr_warn(FW_BUG "[map %p] SINGLE MAPPING flag not allowed for node type %d, skipping ID map\n", |
| 330 | map, type); |
| 331 | return -ENXIO; |
| 332 | } |
| 333 | |
Ard Biesheuvel | 6d3b29d | 2020-05-01 18:10:13 +0200 | [diff] [blame] | 334 | if (rid_in < map->input_base || |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 335 | (rid_in > map->input_base + map->id_count)) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 336 | return -ENXIO; |
| 337 | |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 338 | if (check_overlap) { |
| 339 | /* |
| 340 | * We already found a mapping for this input ID at the end of |
| 341 | * another region. If it coincides with the start of this |
| 342 | * region, we assume the prior match was due to the off-by-1 |
| 343 | * issue mentioned below, and allow it to be superseded. |
| 344 | * Otherwise, things are *really* broken, and we just disregard |
| 345 | * duplicate matches entirely to retain compatibility. |
| 346 | */ |
| 347 | pr_err(FW_BUG "[map %p] conflicting mapping for input ID 0x%x\n", |
| 348 | map, rid_in); |
| 349 | if (rid_in != map->input_base) |
| 350 | return -ENXIO; |
Hanjun Guo | 44cdc7b | 2020-05-08 11:56:38 +0800 | [diff] [blame] | 351 | |
| 352 | pr_err(FW_BUG "applying workaround.\n"); |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 355 | *rid_out = map->output_base + (rid_in - map->input_base); |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * Due to confusion regarding the meaning of the id_count field (which |
| 359 | * carries the number of IDs *minus 1*), we may have to disregard this |
| 360 | * match if it is at the end of the range, and overlaps with the start |
| 361 | * of another one. |
| 362 | */ |
| 363 | if (map->id_count > 0 && rid_in == map->input_base + map->id_count) |
| 364 | return -EAGAIN; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 368 | static struct acpi_iort_node *iort_node_get_id(struct acpi_iort_node *node, |
| 369 | u32 *id_out, int index) |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 370 | { |
| 371 | struct acpi_iort_node *parent; |
| 372 | struct acpi_iort_id_mapping *map; |
| 373 | |
| 374 | if (!node->mapping_offset || !node->mapping_count || |
| 375 | index >= node->mapping_count) |
| 376 | return NULL; |
| 377 | |
| 378 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 379 | node->mapping_offset + index * sizeof(*map)); |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 380 | |
| 381 | /* Firmware bug! */ |
| 382 | if (!map->output_reference) { |
| 383 | pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", |
| 384 | node, node->type); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 389 | map->output_reference); |
| 390 | |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 391 | if (map->flags & ACPI_IORT_ID_SINGLE_MAPPING) { |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 392 | if (node->type == ACPI_IORT_NODE_NAMED_COMPONENT || |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 393 | node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX || |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 394 | node->type == ACPI_IORT_NODE_SMMU_V3 || |
| 395 | node->type == ACPI_IORT_NODE_PMCG) { |
Lorenzo Pieralisi | 030abd8 | 2017-01-05 17:32:16 +0000 | [diff] [blame] | 396 | *id_out = map->output_base; |
Lorenzo Pieralisi | 618f535 | 2016-11-21 10:01:47 +0000 | [diff] [blame] | 397 | return parent; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return NULL; |
| 402 | } |
| 403 | |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 404 | static int iort_get_id_mapping_index(struct acpi_iort_node *node) |
| 405 | { |
| 406 | struct acpi_iort_smmu_v3 *smmu; |
Tuan Phan | 50c8ab8 | 2020-05-20 10:13:07 -0700 | [diff] [blame] | 407 | struct acpi_iort_pmcg *pmcg; |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 408 | |
| 409 | switch (node->type) { |
| 410 | case ACPI_IORT_NODE_SMMU_V3: |
| 411 | /* |
| 412 | * SMMUv3 dev ID mapping index was introduced in revision 1 |
| 413 | * table, not available in revision 0 |
| 414 | */ |
| 415 | if (node->revision < 1) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 419 | /* |
| 420 | * ID mapping index is only ignored if all interrupts are |
| 421 | * GSIV based |
| 422 | */ |
| 423 | if (smmu->event_gsiv && smmu->pri_gsiv && smmu->gerr_gsiv |
| 424 | && smmu->sync_gsiv) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | if (smmu->id_mapping_index >= node->mapping_count) { |
| 428 | pr_err(FW_BUG "[node %p type %d] ID mapping index overflows valid mappings\n", |
| 429 | node, node->type); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | return smmu->id_mapping_index; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 434 | case ACPI_IORT_NODE_PMCG: |
Tuan Phan | 50c8ab8 | 2020-05-20 10:13:07 -0700 | [diff] [blame] | 435 | pmcg = (struct acpi_iort_pmcg *)node->node_data; |
| 436 | if (pmcg->overflow_gsiv || node->mapping_count == 0) |
| 437 | return -EINVAL; |
| 438 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 439 | return 0; |
Hanjun Guo | 86456a3 | 2017-10-13 15:09:49 +0800 | [diff] [blame] | 440 | default: |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | } |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 444 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 445 | static struct acpi_iort_node *iort_node_map_id(struct acpi_iort_node *node, |
| 446 | u32 id_in, u32 *id_out, |
| 447 | u8 type_mask) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 448 | { |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 449 | u32 id = id_in; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 450 | |
| 451 | /* Parse the ID mapping tree to find specified node type */ |
| 452 | while (node) { |
| 453 | struct acpi_iort_id_mapping *map; |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 454 | int i, index, rc = 0; |
| 455 | u32 out_ref = 0, map_id = id; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 456 | |
Lorenzo Pieralisi | ea50b52 | 2016-11-21 10:01:46 +0000 | [diff] [blame] | 457 | if (IORT_TYPE_MASK(node->type) & type_mask) { |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 458 | if (id_out) |
| 459 | *id_out = id; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 460 | return node; |
| 461 | } |
| 462 | |
| 463 | if (!node->mapping_offset || !node->mapping_count) |
| 464 | goto fail_map; |
| 465 | |
| 466 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
| 467 | node->mapping_offset); |
| 468 | |
| 469 | /* Firmware bug! */ |
| 470 | if (!map->output_reference) { |
| 471 | pr_err(FW_BUG "[node %p type %d] ID map has NULL parent reference\n", |
| 472 | node, node->type); |
| 473 | goto fail_map; |
| 474 | } |
| 475 | |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 476 | /* |
| 477 | * Get the special ID mapping index (if any) and skip its |
| 478 | * associated ID map to prevent erroneous multi-stage |
| 479 | * IORT ID translations. |
| 480 | */ |
| 481 | index = iort_get_id_mapping_index(node); |
| 482 | |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 483 | /* Do the ID translation */ |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 484 | for (i = 0; i < node->mapping_count; i++, map++) { |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 485 | /* if it is special mapping index, skip it */ |
| 486 | if (i == index) |
| 487 | continue; |
| 488 | |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 489 | rc = iort_id_map(map, node->type, map_id, &id, out_ref); |
| 490 | if (!rc) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 491 | break; |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 492 | if (rc == -EAGAIN) |
| 493 | out_ref = map->output_reference; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 496 | if (i == node->mapping_count && !out_ref) |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 497 | goto fail_map; |
| 498 | |
| 499 | node = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
Ard Biesheuvel | 539979b | 2020-05-01 18:10:14 +0200 | [diff] [blame] | 500 | rc ? out_ref : map->output_reference); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | fail_map: |
Hanjun Guo | 697f609 | 2017-03-07 20:40:03 +0800 | [diff] [blame] | 504 | /* Map input ID to output ID unchanged on mapping failure */ |
| 505 | if (id_out) |
| 506 | *id_out = id_in; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 507 | |
| 508 | return NULL; |
| 509 | } |
| 510 | |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 511 | static struct acpi_iort_node *iort_node_map_platform_id( |
| 512 | struct acpi_iort_node *node, u32 *id_out, u8 type_mask, |
| 513 | int index) |
Hanjun Guo | 8ca4f1d | 2017-03-07 20:40:04 +0800 | [diff] [blame] | 514 | { |
| 515 | struct acpi_iort_node *parent; |
| 516 | u32 id; |
| 517 | |
| 518 | /* step 1: retrieve the initial dev id */ |
| 519 | parent = iort_node_get_id(node, &id, index); |
| 520 | if (!parent) |
| 521 | return NULL; |
| 522 | |
| 523 | /* |
| 524 | * optional step 2: map the initial dev id if its parent is not |
| 525 | * the target type we want, map it again for the use cases such |
| 526 | * as NC (named component) -> SMMU -> ITS. If the type is matched, |
| 527 | * return the initial dev id and its parent pointer directly. |
| 528 | */ |
| 529 | if (!(IORT_TYPE_MASK(parent->type) & type_mask)) |
| 530 | parent = iort_node_map_id(parent, id, id_out, type_mask); |
| 531 | else |
| 532 | if (id_out) |
| 533 | *id_out = id; |
| 534 | |
| 535 | return parent; |
| 536 | } |
| 537 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 538 | static struct acpi_iort_node *iort_find_dev_node(struct device *dev) |
| 539 | { |
| 540 | struct pci_bus *pbus; |
| 541 | |
Hanjun Guo | 0a71d8b | 2017-10-13 15:09:47 +0800 | [diff] [blame] | 542 | if (!dev_is_pci(dev)) { |
| 543 | struct acpi_iort_node *node; |
| 544 | /* |
| 545 | * scan iort_fwnode_list to see if it's an iort platform |
| 546 | * device (such as SMMU, PMCG),its iort node already cached |
| 547 | * and associated with fwnode when iort platform devices |
| 548 | * were initialized. |
| 549 | */ |
| 550 | node = iort_get_iort_node(dev->fwnode); |
| 551 | if (node) |
| 552 | return node; |
Hanjun Guo | 0a71d8b | 2017-10-13 15:09:47 +0800 | [diff] [blame] | 553 | /* |
| 554 | * if not, then it should be a platform device defined in |
| 555 | * DSDT/SSDT (with Named Component node in IORT) |
| 556 | */ |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 557 | return iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 558 | iort_match_node_callback, dev); |
Hanjun Guo | 0a71d8b | 2017-10-13 15:09:47 +0800 | [diff] [blame] | 559 | } |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 560 | |
| 561 | /* Find a PCI root bus */ |
| 562 | pbus = to_pci_dev(dev)->bus; |
| 563 | while (!pci_is_root_bus(pbus)) |
| 564 | pbus = pbus->parent; |
| 565 | |
| 566 | return iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
| 567 | iort_match_node_callback, &pbus->dev); |
| 568 | } |
| 569 | |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 570 | /** |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 571 | * iort_msi_map_id() - Map a MSI input ID for a device |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 572 | * @dev: The device for which the mapping is to be done. |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 573 | * @input_id: The device input ID. |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 574 | * |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 575 | * Returns: mapped MSI ID on success, input ID otherwise |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 576 | */ |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 577 | u32 iort_msi_map_id(struct device *dev, u32 input_id) |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 578 | { |
| 579 | struct acpi_iort_node *node; |
| 580 | u32 dev_id; |
| 581 | |
| 582 | node = iort_find_dev_node(dev); |
| 583 | if (!node) |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 584 | return input_id; |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 585 | |
Lorenzo Pieralisi | 39c3cf5 | 2020-06-19 09:20:04 +0100 | [diff] [blame^] | 586 | iort_node_map_id(node, input_id, &dev_id, IORT_MSI_TYPE); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 587 | return dev_id; |
| 588 | } |
| 589 | |
| 590 | /** |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 591 | * iort_pmsi_get_dev_id() - Get the device id for a device |
| 592 | * @dev: The device for which the mapping is to be done. |
| 593 | * @dev_id: The device ID found. |
| 594 | * |
| 595 | * Returns: 0 for successful find a dev id, -ENODEV on error |
| 596 | */ |
| 597 | int iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id) |
| 598 | { |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 599 | int i, index; |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 600 | struct acpi_iort_node *node; |
| 601 | |
| 602 | node = iort_find_dev_node(dev); |
| 603 | if (!node) |
| 604 | return -ENODEV; |
| 605 | |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 606 | index = iort_get_id_mapping_index(node); |
| 607 | /* if there is a valid index, go get the dev_id directly */ |
| 608 | if (index >= 0) { |
| 609 | if (iort_node_get_id(node, dev_id, index)) |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 610 | return 0; |
Hanjun Guo | 8c8df8d | 2017-10-13 15:09:48 +0800 | [diff] [blame] | 611 | } else { |
| 612 | for (i = 0; i < node->mapping_count; i++) { |
| 613 | if (iort_node_map_platform_id(node, dev_id, |
| 614 | IORT_MSI_TYPE, i)) |
| 615 | return 0; |
| 616 | } |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | return -ENODEV; |
| 620 | } |
| 621 | |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 622 | static int __maybe_unused iort_find_its_base(u32 its_id, phys_addr_t *base) |
| 623 | { |
| 624 | struct iort_its_msi_chip *its_msi_chip; |
| 625 | int ret = -ENODEV; |
| 626 | |
| 627 | spin_lock(&iort_msi_chip_lock); |
| 628 | list_for_each_entry(its_msi_chip, &iort_msi_chip_list, list) { |
| 629 | if (its_msi_chip->translation_id == its_id) { |
| 630 | *base = its_msi_chip->base_addr; |
| 631 | ret = 0; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | spin_unlock(&iort_msi_chip_lock); |
| 636 | |
| 637 | return ret; |
| 638 | } |
| 639 | |
Hanjun Guo | ae7c183 | 2017-03-07 20:40:05 +0800 | [diff] [blame] | 640 | /** |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 641 | * iort_dev_find_its_id() - Find the ITS identifier for a device |
| 642 | * @dev: The device. |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 643 | * @id: Device's ID |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 644 | * @idx: Index of the ITS identifier list. |
| 645 | * @its_id: ITS identifier. |
| 646 | * |
| 647 | * Returns: 0 on success, appropriate error value otherwise |
| 648 | */ |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 649 | static int iort_dev_find_its_id(struct device *dev, u32 id, |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 650 | unsigned int idx, int *its_id) |
| 651 | { |
| 652 | struct acpi_iort_its_group *its; |
| 653 | struct acpi_iort_node *node; |
| 654 | |
| 655 | node = iort_find_dev_node(dev); |
| 656 | if (!node) |
| 657 | return -ENXIO; |
| 658 | |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 659 | node = iort_node_map_id(node, id, NULL, IORT_MSI_TYPE); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 660 | if (!node) |
| 661 | return -ENXIO; |
| 662 | |
| 663 | /* Move to ITS specific data */ |
| 664 | its = (struct acpi_iort_its_group *)node->node_data; |
Lorenzo Pieralisi | 5a46d3f | 2019-07-22 17:25:48 +0100 | [diff] [blame] | 665 | if (idx >= its->its_count) { |
| 666 | 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] | 667 | idx, its->its_count); |
| 668 | return -ENXIO; |
| 669 | } |
| 670 | |
| 671 | *its_id = its->identifiers[idx]; |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * iort_get_device_domain() - Find MSI domain related to a device |
| 677 | * @dev: The device. |
| 678 | * @req_id: Requester ID for the device. |
| 679 | * |
| 680 | * Returns: the MSI domain for this device, NULL otherwise |
| 681 | */ |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 682 | struct irq_domain *iort_get_device_domain(struct device *dev, u32 id, |
| 683 | enum irq_domain_bus_token bus_token) |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 684 | { |
| 685 | struct fwnode_handle *handle; |
| 686 | int its_id; |
| 687 | |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 688 | if (iort_dev_find_its_id(dev, id, 0, &its_id)) |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 689 | return NULL; |
| 690 | |
| 691 | handle = iort_find_domain_token(its_id); |
| 692 | if (!handle) |
| 693 | return NULL; |
| 694 | |
Lorenzo Pieralisi | d1718a1b | 2020-06-19 09:20:03 +0100 | [diff] [blame] | 695 | return irq_find_matching_fwnode(handle, bus_token); |
Tomasz Nowicki | 4bf2efd | 2016-09-12 20:32:21 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Lorenzo Pieralisi | 6563790 | 2017-10-13 15:09:50 +0800 | [diff] [blame] | 698 | static void iort_set_device_domain(struct device *dev, |
| 699 | struct acpi_iort_node *node) |
| 700 | { |
| 701 | struct acpi_iort_its_group *its; |
| 702 | struct acpi_iort_node *msi_parent; |
| 703 | struct acpi_iort_id_mapping *map; |
| 704 | struct fwnode_handle *iort_fwnode; |
| 705 | struct irq_domain *domain; |
| 706 | int index; |
| 707 | |
| 708 | index = iort_get_id_mapping_index(node); |
| 709 | if (index < 0) |
| 710 | return; |
| 711 | |
| 712 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, node, |
| 713 | node->mapping_offset + index * sizeof(*map)); |
| 714 | |
| 715 | /* Firmware bug! */ |
| 716 | if (!map->output_reference || |
| 717 | !(map->flags & ACPI_IORT_ID_SINGLE_MAPPING)) { |
| 718 | pr_err(FW_BUG "[node %p type %d] Invalid MSI mapping\n", |
| 719 | node, node->type); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | msi_parent = ACPI_ADD_PTR(struct acpi_iort_node, iort_table, |
| 724 | map->output_reference); |
| 725 | |
| 726 | if (!msi_parent || msi_parent->type != ACPI_IORT_NODE_ITS_GROUP) |
| 727 | return; |
| 728 | |
| 729 | /* Move to ITS specific data */ |
| 730 | its = (struct acpi_iort_its_group *)msi_parent->node_data; |
| 731 | |
| 732 | iort_fwnode = iort_find_domain_token(its->identifiers[0]); |
| 733 | if (!iort_fwnode) |
| 734 | return; |
| 735 | |
| 736 | domain = irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); |
| 737 | if (domain) |
| 738 | dev_set_msi_domain(dev, domain); |
| 739 | } |
| 740 | |
Hanjun Guo | d4f54a1 | 2017-03-07 20:40:06 +0800 | [diff] [blame] | 741 | /** |
| 742 | * iort_get_platform_device_domain() - Find MSI domain related to a |
| 743 | * platform device |
| 744 | * @dev: the dev pointer associated with the platform device |
| 745 | * |
| 746 | * Returns: the MSI domain for this device, NULL otherwise |
| 747 | */ |
| 748 | static struct irq_domain *iort_get_platform_device_domain(struct device *dev) |
| 749 | { |
Lorenzo Pieralisi | ea2412d | 2018-11-29 09:55:59 +0000 | [diff] [blame] | 750 | struct acpi_iort_node *node, *msi_parent = NULL; |
Hanjun Guo | d4f54a1 | 2017-03-07 20:40:06 +0800 | [diff] [blame] | 751 | struct fwnode_handle *iort_fwnode; |
| 752 | struct acpi_iort_its_group *its; |
| 753 | int i; |
| 754 | |
| 755 | /* find its associated iort node */ |
| 756 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 757 | iort_match_node_callback, dev); |
| 758 | if (!node) |
| 759 | return NULL; |
| 760 | |
| 761 | /* then find its msi parent node */ |
| 762 | for (i = 0; i < node->mapping_count; i++) { |
| 763 | msi_parent = iort_node_map_platform_id(node, NULL, |
| 764 | IORT_MSI_TYPE, i); |
| 765 | if (msi_parent) |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | if (!msi_parent) |
| 770 | return NULL; |
| 771 | |
| 772 | /* Move to ITS specific data */ |
| 773 | its = (struct acpi_iort_its_group *)msi_parent->node_data; |
| 774 | |
| 775 | iort_fwnode = iort_find_domain_token(its->identifiers[0]); |
| 776 | if (!iort_fwnode) |
| 777 | return NULL; |
| 778 | |
| 779 | return irq_find_matching_fwnode(iort_fwnode, DOMAIN_BUS_PLATFORM_MSI); |
| 780 | } |
| 781 | |
| 782 | void acpi_configure_pmsi_domain(struct device *dev) |
| 783 | { |
| 784 | struct irq_domain *msi_domain; |
| 785 | |
| 786 | msi_domain = iort_get_platform_device_domain(dev); |
| 787 | if (msi_domain) |
| 788 | dev_set_msi_domain(dev, msi_domain); |
| 789 | } |
| 790 | |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 791 | #ifdef CONFIG_IOMMU_API |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 792 | static struct acpi_iort_node *iort_get_msi_resv_iommu(struct device *dev) |
| 793 | { |
| 794 | struct acpi_iort_node *iommu; |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 795 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 796 | |
| 797 | iommu = iort_get_iort_node(fwspec->iommu_fwnode); |
| 798 | |
| 799 | if (iommu && (iommu->type == ACPI_IORT_NODE_SMMU_V3)) { |
| 800 | struct acpi_iort_smmu_v3 *smmu; |
| 801 | |
| 802 | smmu = (struct acpi_iort_smmu_v3 *)iommu->node_data; |
| 803 | if (smmu->model == ACPI_IORT_SMMU_V3_HISILICON_HI161X) |
| 804 | return iommu; |
| 805 | } |
| 806 | |
| 807 | return NULL; |
| 808 | } |
| 809 | |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 810 | 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] | 811 | { |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 812 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
| 813 | |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 814 | return (fwspec && fwspec->ops) ? fwspec->ops : NULL; |
| 815 | } |
| 816 | |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 817 | static inline int iort_add_device_replay(const struct iommu_ops *ops, |
| 818 | struct device *dev) |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 819 | { |
| 820 | int err = 0; |
| 821 | |
Joerg Roedel | d2e1a00 | 2018-12-05 14:39:45 +0100 | [diff] [blame] | 822 | if (dev->bus && !device_iommu_mapped(dev)) |
| 823 | err = iommu_probe_device(dev); |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 824 | |
| 825 | return err; |
| 826 | } |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 827 | |
| 828 | /** |
| 829 | * iort_iommu_msi_get_resv_regions - Reserved region driver helper |
| 830 | * @dev: Device from iommu_get_resv_regions() |
| 831 | * @head: Reserved region list from iommu_get_resv_regions() |
| 832 | * |
| 833 | * Returns: Number of msi reserved regions on success (0 if platform |
| 834 | * doesn't require the reservation or no associated msi regions), |
| 835 | * appropriate error value otherwise. The ITS interrupt translation |
| 836 | * spaces (ITS_base + SZ_64K, SZ_64K) associated with the device |
| 837 | * are the msi reserved regions. |
| 838 | */ |
| 839 | int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) |
| 840 | { |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 841 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 842 | struct acpi_iort_its_group *its; |
| 843 | struct acpi_iort_node *iommu_node, *its_node = NULL; |
| 844 | int i, resv = 0; |
| 845 | |
| 846 | iommu_node = iort_get_msi_resv_iommu(dev); |
| 847 | if (!iommu_node) |
| 848 | return 0; |
| 849 | |
| 850 | /* |
| 851 | * Current logic to reserve ITS regions relies on HW topologies |
| 852 | * where a given PCI or named component maps its IDs to only one |
| 853 | * ITS group; if a PCI or named component can map its IDs to |
| 854 | * different ITS groups through IORT mappings this function has |
| 855 | * to be reworked to ensure we reserve regions for all ITS groups |
| 856 | * a given PCI or named component may map IDs to. |
| 857 | */ |
| 858 | |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 859 | for (i = 0; i < fwspec->num_ids; i++) { |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 860 | its_node = iort_node_map_id(iommu_node, |
Joerg Roedel | 8097e53 | 2018-11-29 14:01:00 +0100 | [diff] [blame] | 861 | fwspec->ids[i], |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 862 | NULL, IORT_MSI_TYPE); |
| 863 | if (its_node) |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | if (!its_node) |
| 868 | return 0; |
| 869 | |
| 870 | /* Move to ITS specific data */ |
| 871 | its = (struct acpi_iort_its_group *)its_node->node_data; |
| 872 | |
| 873 | for (i = 0; i < its->its_count; i++) { |
| 874 | phys_addr_t base; |
| 875 | |
| 876 | if (!iort_find_its_base(its->identifiers[i], &base)) { |
| 877 | int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO; |
| 878 | struct iommu_resv_region *region; |
| 879 | |
| 880 | region = iommu_alloc_resv_region(base + SZ_64K, SZ_64K, |
| 881 | prot, IOMMU_RESV_MSI); |
| 882 | if (region) { |
| 883 | list_add_tail(®ion->list, head); |
| 884 | resv++; |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | return (resv == its->its_count) ? resv : -ENODEV; |
| 890 | } |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 891 | |
| 892 | static inline bool iort_iommu_driver_enabled(u8 type) |
| 893 | { |
| 894 | switch (type) { |
| 895 | case ACPI_IORT_NODE_SMMU_V3: |
Ard Biesheuvel | d3daf66 | 2019-12-19 12:03:48 +0000 | [diff] [blame] | 896 | return IS_ENABLED(CONFIG_ARM_SMMU_V3); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 897 | case ACPI_IORT_NODE_SMMU: |
Ard Biesheuvel | d3daf66 | 2019-12-19 12:03:48 +0000 | [diff] [blame] | 898 | return IS_ENABLED(CONFIG_ARM_SMMU); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 899 | default: |
| 900 | pr_warn("IORT node type %u does not describe an SMMU\n", type); |
| 901 | return false; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | static int arm_smmu_iort_xlate(struct device *dev, u32 streamid, |
| 906 | struct fwnode_handle *fwnode, |
| 907 | const struct iommu_ops *ops) |
| 908 | { |
| 909 | int ret = iommu_fwspec_init(dev, fwnode, ops); |
| 910 | |
| 911 | if (!ret) |
| 912 | ret = iommu_fwspec_add_ids(dev, &streamid, 1); |
| 913 | |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | static bool iort_pci_rc_supports_ats(struct acpi_iort_node *node) |
| 918 | { |
| 919 | struct acpi_iort_root_complex *pci_rc; |
| 920 | |
| 921 | pci_rc = (struct acpi_iort_root_complex *)node->node_data; |
| 922 | return pci_rc->ats_attribute & ACPI_IORT_ATS_SUPPORTED; |
| 923 | } |
Lorenzo Pieralisi | d49f2de | 2017-04-28 16:59:49 +0100 | [diff] [blame] | 924 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 925 | static int iort_iommu_xlate(struct device *dev, struct acpi_iort_node *node, |
| 926 | u32 streamid) |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 927 | { |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 928 | const struct iommu_ops *ops; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 929 | struct fwnode_handle *iort_fwnode; |
| 930 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 931 | if (!node) |
| 932 | return -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 933 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 934 | iort_fwnode = iort_get_fwnode(node); |
| 935 | if (!iort_fwnode) |
| 936 | return -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 937 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 938 | /* |
| 939 | * If the ops look-up fails, this means that either |
| 940 | * the SMMU drivers have not been probed yet or that |
| 941 | * the SMMU drivers are not built in the kernel; |
| 942 | * Depending on whether the SMMU drivers are built-in |
| 943 | * in the kernel or not, defer the IOMMU configuration |
| 944 | * or just abort it. |
| 945 | */ |
| 946 | ops = iommu_ops_from_fwnode(iort_fwnode); |
| 947 | if (!ops) |
| 948 | return iort_iommu_driver_enabled(node->type) ? |
| 949 | -EPROBE_DEFER : -ENODEV; |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 950 | |
Robin Murphy | bc8648d | 2017-08-04 17:42:06 +0100 | [diff] [blame] | 951 | return arm_smmu_iort_xlate(dev, streamid, iort_fwnode, ops); |
| 952 | } |
| 953 | |
| 954 | struct iort_pci_alias_info { |
| 955 | struct device *dev; |
| 956 | struct acpi_iort_node *node; |
| 957 | }; |
| 958 | |
| 959 | static int iort_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) |
| 960 | { |
| 961 | struct iort_pci_alias_info *info = data; |
| 962 | struct acpi_iort_node *parent; |
| 963 | u32 streamid; |
| 964 | |
| 965 | parent = iort_node_map_id(info->node, alias, &streamid, |
| 966 | IORT_IOMMU_TYPE); |
| 967 | return iort_iommu_xlate(info->dev, parent, streamid); |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Jean-Philippe Brucker | da22565 | 2020-01-15 13:52:30 +0100 | [diff] [blame] | 970 | static void iort_named_component_init(struct device *dev, |
| 971 | struct acpi_iort_node *node) |
| 972 | { |
| 973 | struct acpi_iort_named_component *nc; |
| 974 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
| 975 | |
| 976 | if (!fwspec) |
| 977 | return; |
| 978 | |
| 979 | nc = (struct acpi_iort_named_component *)node->node_data; |
| 980 | fwspec->num_pasid_bits = FIELD_GET(ACPI_IORT_NC_PASID_BITS, |
| 981 | nc->node_flags); |
| 982 | } |
| 983 | |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 984 | /** |
| 985 | * iort_iommu_configure - Set-up IOMMU configuration for a device. |
| 986 | * |
| 987 | * @dev: device to configure |
| 988 | * |
| 989 | * Returns: iommu_ops pointer on configuration success |
| 990 | * NULL on configuration failure |
| 991 | */ |
| 992 | const struct iommu_ops *iort_iommu_configure(struct device *dev) |
| 993 | { |
| 994 | struct acpi_iort_node *node, *parent; |
| 995 | const struct iommu_ops *ops; |
| 996 | u32 streamid = 0; |
| 997 | int err = -ENODEV; |
| 998 | |
| 999 | /* |
| 1000 | * If we already translated the fwspec there |
| 1001 | * is nothing left to do, return the iommu_ops. |
| 1002 | */ |
| 1003 | ops = iort_fwspec_iommu_ops(dev); |
| 1004 | if (ops) |
| 1005 | return ops; |
| 1006 | |
| 1007 | if (dev_is_pci(dev)) { |
Joerg Roedel | 6990ec7 | 2020-03-26 16:08:27 +0100 | [diff] [blame] | 1008 | struct iommu_fwspec *fwspec; |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 1009 | struct pci_bus *bus = to_pci_dev(dev)->bus; |
| 1010 | struct iort_pci_alias_info info = { .dev = dev }; |
| 1011 | |
| 1012 | node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
| 1013 | iort_match_node_callback, &bus->dev); |
| 1014 | if (!node) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | info.node = node; |
| 1018 | err = pci_for_each_dma_alias(to_pci_dev(dev), |
| 1019 | iort_pci_iommu_init, &info); |
| 1020 | |
Joerg Roedel | 6990ec7 | 2020-03-26 16:08:27 +0100 | [diff] [blame] | 1021 | fwspec = dev_iommu_fwspec_get(dev); |
| 1022 | if (fwspec && iort_pci_rc_supports_ats(node)) |
| 1023 | fwspec->flags |= IOMMU_FWSPEC_PCI_RC_ATS; |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 1024 | } else { |
| 1025 | int i = 0; |
| 1026 | |
| 1027 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 1028 | iort_match_node_callback, dev); |
| 1029 | if (!node) |
| 1030 | return NULL; |
| 1031 | |
| 1032 | do { |
| 1033 | parent = iort_node_map_platform_id(node, &streamid, |
| 1034 | IORT_IOMMU_TYPE, |
| 1035 | i++); |
| 1036 | |
| 1037 | if (parent) |
| 1038 | err = iort_iommu_xlate(dev, parent, streamid); |
| 1039 | } while (parent && !err); |
Jean-Philippe Brucker | da22565 | 2020-01-15 13:52:30 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (!err) |
| 1042 | iort_named_component_init(dev, node); |
Lorenzo Pieralisi | 8212688 | 2019-05-16 16:52:58 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * If we have reason to believe the IOMMU driver missed the initial |
| 1047 | * add_device callback for dev, replay it to get things in order. |
| 1048 | */ |
| 1049 | if (!err) { |
| 1050 | ops = iort_fwspec_iommu_ops(dev); |
| 1051 | err = iort_add_device_replay(ops, dev); |
| 1052 | } |
| 1053 | |
| 1054 | /* Ignore all other errors apart from EPROBE_DEFER */ |
| 1055 | if (err == -EPROBE_DEFER) { |
| 1056 | ops = ERR_PTR(err); |
| 1057 | } else if (err) { |
| 1058 | dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); |
| 1059 | ops = NULL; |
| 1060 | } |
| 1061 | |
| 1062 | return ops; |
| 1063 | } |
| 1064 | #else |
| 1065 | static inline const struct iommu_ops *iort_fwspec_iommu_ops(struct device *dev) |
| 1066 | { return NULL; } |
| 1067 | static inline int iort_add_device_replay(const struct iommu_ops *ops, |
| 1068 | struct device *dev) |
| 1069 | { return 0; } |
| 1070 | int iort_iommu_msi_get_resv_regions(struct device *dev, struct list_head *head) |
| 1071 | { return 0; } |
| 1072 | const struct iommu_ops *iort_iommu_configure(struct device *dev) |
| 1073 | { return NULL; } |
| 1074 | #endif |
| 1075 | |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1076 | static int nc_dma_get_range(struct device *dev, u64 *size) |
| 1077 | { |
| 1078 | struct acpi_iort_node *node; |
| 1079 | struct acpi_iort_named_component *ncomp; |
| 1080 | |
| 1081 | node = iort_scan_node(ACPI_IORT_NODE_NAMED_COMPONENT, |
| 1082 | iort_match_node_callback, dev); |
| 1083 | if (!node) |
| 1084 | return -ENODEV; |
| 1085 | |
| 1086 | ncomp = (struct acpi_iort_named_component *)node->node_data; |
| 1087 | |
| 1088 | *size = ncomp->memory_address_limit >= 64 ? U64_MAX : |
| 1089 | 1ULL<<ncomp->memory_address_limit; |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1094 | static int rc_dma_get_range(struct device *dev, u64 *size) |
| 1095 | { |
| 1096 | struct acpi_iort_node *node; |
| 1097 | struct acpi_iort_root_complex *rc; |
Jean-Philippe Brucker | c777723 | 2019-01-10 18:41:51 +0000 | [diff] [blame] | 1098 | struct pci_bus *pbus = to_pci_dev(dev)->bus; |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1099 | |
| 1100 | node = iort_scan_node(ACPI_IORT_NODE_PCI_ROOT_COMPLEX, |
Jean-Philippe Brucker | c777723 | 2019-01-10 18:41:51 +0000 | [diff] [blame] | 1101 | iort_match_node_callback, &pbus->dev); |
Robin Murphy | 5ac65e8 | 2018-07-23 23:16:06 +0100 | [diff] [blame] | 1102 | if (!node || node->revision < 1) |
| 1103 | return -ENODEV; |
| 1104 | |
| 1105 | rc = (struct acpi_iort_root_complex *)node->node_data; |
| 1106 | |
| 1107 | *size = rc->memory_address_limit >= 64 ? U64_MAX : |
| 1108 | 1ULL<<rc->memory_address_limit; |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
Lorenzo Pieralisi | 643b8e4 | 2016-11-21 10:01:48 +0000 | [diff] [blame] | 1113 | /** |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1114 | * iort_dma_setup() - Set-up device DMA parameters. |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1115 | * |
| 1116 | * @dev: device to configure |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1117 | * @dma_addr: device DMA address result pointer |
| 1118 | * @size: DMA range size result pointer |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1119 | */ |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1120 | 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] | 1121 | { |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1122 | u64 end, mask, dmaaddr = 0, size = 0, offset = 0; |
| 1123 | int ret; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1124 | |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1125 | /* |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1126 | * If @dev is expected to be DMA-capable then the bus code that created |
| 1127 | * it should have initialised its dma_mask pointer by this point. For |
| 1128 | * now, we'll continue the legacy behaviour of coercing it to the |
| 1129 | * coherent mask if not, but we'll no longer do so quietly. |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1130 | */ |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1131 | if (!dev->dma_mask) { |
| 1132 | dev_warn(dev, "DMA mask not set\n"); |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1133 | dev->dma_mask = &dev->coherent_dma_mask; |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1134 | } |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1135 | |
Robin Murphy | 6757cda | 2018-07-23 23:16:11 +0100 | [diff] [blame] | 1136 | if (dev->coherent_dma_mask) |
| 1137 | size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); |
| 1138 | else |
| 1139 | size = 1ULL << 32; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1140 | |
Ard Biesheuvel | 7fb89e1 | 2020-04-20 11:27:53 +0200 | [diff] [blame] | 1141 | ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size); |
| 1142 | if (ret == -ENODEV) |
| 1143 | ret = dev_is_pci(dev) ? rc_dma_get_range(dev, &size) |
| 1144 | : nc_dma_get_range(dev, &size); |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1145 | |
| 1146 | if (!ret) { |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1147 | /* |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1148 | * Limit coherent and dma mask based on size retrieved from |
| 1149 | * firmware. |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1150 | */ |
Nicolas Saenz Julienne | a7ba70f | 2019-11-21 10:26:44 +0100 | [diff] [blame] | 1151 | end = dmaaddr + size - 1; |
| 1152 | mask = DMA_BIT_MASK(ilog2(end) + 1); |
| 1153 | dev->bus_dma_limit = end; |
Lorenzo Pieralisi | 10d8ab2 | 2017-08-03 13:32:39 +0100 | [diff] [blame] | 1154 | dev->coherent_dma_mask = mask; |
| 1155 | *dev->dma_mask = mask; |
Lorenzo Pieralisi | 7ad4263 | 2017-08-07 11:29:49 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | *dma_addr = dmaaddr; |
| 1159 | *dma_size = size; |
| 1160 | |
| 1161 | dev->dma_pfn_offset = PFN_DOWN(offset); |
| 1162 | dev_dbg(dev, "dma_pfn_offset(%#08llx)\n", offset); |
Lorenzo Pieralisi | 18b709b | 2016-12-06 14:20:11 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1165 | static void __init acpi_iort_register_irq(int hwirq, const char *name, |
| 1166 | int trigger, |
| 1167 | struct resource *res) |
| 1168 | { |
| 1169 | int irq = acpi_register_gsi(NULL, hwirq, trigger, |
| 1170 | ACPI_ACTIVE_HIGH); |
| 1171 | |
| 1172 | if (irq <= 0) { |
| 1173 | pr_err("could not register gsi hwirq %d name [%s]\n", hwirq, |
| 1174 | name); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | res->start = irq; |
| 1179 | res->end = irq; |
| 1180 | res->flags = IORESOURCE_IRQ; |
| 1181 | res->name = name; |
| 1182 | } |
| 1183 | |
| 1184 | static int __init arm_smmu_v3_count_resources(struct acpi_iort_node *node) |
| 1185 | { |
| 1186 | struct acpi_iort_smmu_v3 *smmu; |
| 1187 | /* Always present mem resource */ |
| 1188 | int num_res = 1; |
| 1189 | |
| 1190 | /* Retrieve SMMUv3 specific data */ |
| 1191 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1192 | |
| 1193 | if (smmu->event_gsiv) |
| 1194 | num_res++; |
| 1195 | |
| 1196 | if (smmu->pri_gsiv) |
| 1197 | num_res++; |
| 1198 | |
| 1199 | if (smmu->gerr_gsiv) |
| 1200 | num_res++; |
| 1201 | |
| 1202 | if (smmu->sync_gsiv) |
| 1203 | num_res++; |
| 1204 | |
| 1205 | return num_res; |
| 1206 | } |
| 1207 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1208 | static bool arm_smmu_v3_is_combined_irq(struct acpi_iort_smmu_v3 *smmu) |
| 1209 | { |
| 1210 | /* |
| 1211 | * Cavium ThunderX2 implementation doesn't not support unique |
| 1212 | * irq line. Use single irq line for all the SMMUv3 interrupts. |
| 1213 | */ |
| 1214 | if (smmu->model != ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 1215 | return false; |
| 1216 | |
| 1217 | /* |
| 1218 | * ThunderX2 doesn't support MSIs from the SMMU, so we're checking |
| 1219 | * SPI numbers here. |
| 1220 | */ |
| 1221 | return smmu->event_gsiv == smmu->pri_gsiv && |
| 1222 | smmu->event_gsiv == smmu->gerr_gsiv && |
| 1223 | smmu->event_gsiv == smmu->sync_gsiv; |
| 1224 | } |
| 1225 | |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 1226 | static unsigned long arm_smmu_v3_resource_size(struct acpi_iort_smmu_v3 *smmu) |
| 1227 | { |
| 1228 | /* |
| 1229 | * Override the size, for Cavium ThunderX2 implementation |
| 1230 | * which doesn't support the page 1 SMMU register space. |
| 1231 | */ |
| 1232 | if (smmu->model == ACPI_IORT_SMMU_V3_CAVIUM_CN99XX) |
| 1233 | return SZ_64K; |
| 1234 | |
| 1235 | return SZ_128K; |
| 1236 | } |
| 1237 | |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1238 | static void __init arm_smmu_v3_init_resources(struct resource *res, |
| 1239 | struct acpi_iort_node *node) |
| 1240 | { |
| 1241 | struct acpi_iort_smmu_v3 *smmu; |
| 1242 | int num_res = 0; |
| 1243 | |
| 1244 | /* Retrieve SMMUv3 specific data */ |
| 1245 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1246 | |
| 1247 | res[num_res].start = smmu->base_address; |
Linu Cherian | 403e8c7 | 2017-06-22 17:35:36 +0530 | [diff] [blame] | 1248 | res[num_res].end = smmu->base_address + |
| 1249 | arm_smmu_v3_resource_size(smmu) - 1; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1250 | res[num_res].flags = IORESOURCE_MEM; |
| 1251 | |
| 1252 | num_res++; |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1253 | if (arm_smmu_v3_is_combined_irq(smmu)) { |
| 1254 | if (smmu->event_gsiv) |
| 1255 | acpi_iort_register_irq(smmu->event_gsiv, "combined", |
| 1256 | ACPI_EDGE_SENSITIVE, |
| 1257 | &res[num_res++]); |
| 1258 | } else { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1259 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1260 | if (smmu->event_gsiv) |
| 1261 | acpi_iort_register_irq(smmu->event_gsiv, "eventq", |
| 1262 | ACPI_EDGE_SENSITIVE, |
| 1263 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1264 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1265 | if (smmu->pri_gsiv) |
| 1266 | acpi_iort_register_irq(smmu->pri_gsiv, "priq", |
| 1267 | ACPI_EDGE_SENSITIVE, |
| 1268 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1269 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1270 | if (smmu->gerr_gsiv) |
| 1271 | acpi_iort_register_irq(smmu->gerr_gsiv, "gerror", |
| 1272 | ACPI_EDGE_SENSITIVE, |
| 1273 | &res[num_res++]); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1274 | |
Geetha Sowjanya | f935448 | 2017-06-23 19:04:36 +0530 | [diff] [blame] | 1275 | if (smmu->sync_gsiv) |
| 1276 | acpi_iort_register_irq(smmu->sync_gsiv, "cmdq-sync", |
| 1277 | ACPI_EDGE_SENSITIVE, |
| 1278 | &res[num_res++]); |
| 1279 | } |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1282 | static void __init arm_smmu_v3_dma_configure(struct device *dev, |
| 1283 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1284 | { |
| 1285 | struct acpi_iort_smmu_v3 *smmu; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1286 | enum dev_dma_attr attr; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1287 | |
| 1288 | /* Retrieve SMMUv3 specific data */ |
| 1289 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1290 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1291 | attr = (smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE) ? |
| 1292 | DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; |
| 1293 | |
| 1294 | /* We expect the dma masks to be equivalent for all SMMUv3 set-ups */ |
| 1295 | dev->dma_mask = &dev->coherent_dma_mask; |
| 1296 | |
| 1297 | /* Configure DMA for the page table walker */ |
| 1298 | acpi_dma_configure(dev, attr); |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Lorenzo Pieralisi | 7580813 | 2017-09-28 13:57:10 +0100 | [diff] [blame] | 1301 | #if defined(CONFIG_ACPI_NUMA) |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1302 | /* |
| 1303 | * set numa proximity domain for smmuv3 device |
| 1304 | */ |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1305 | static int __init arm_smmu_v3_set_proximity(struct device *dev, |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1306 | struct acpi_iort_node *node) |
| 1307 | { |
| 1308 | struct acpi_iort_smmu_v3 *smmu; |
| 1309 | |
| 1310 | smmu = (struct acpi_iort_smmu_v3 *)node->node_data; |
| 1311 | if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1312 | int dev_node = acpi_map_pxm_to_node(smmu->pxm); |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1313 | |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1314 | if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1315 | return -EINVAL; |
| 1316 | |
Lorenzo Pieralisi | 3e77eeb | 2019-07-22 17:14:33 +0100 | [diff] [blame] | 1317 | set_dev_node(dev, dev_node); |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1318 | pr_info("SMMU-v3[%llx] Mapped to Proximity domain %d\n", |
| 1319 | smmu->base_address, |
| 1320 | smmu->pxm); |
| 1321 | } |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1322 | return 0; |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1323 | } |
| 1324 | #else |
| 1325 | #define arm_smmu_v3_set_proximity NULL |
| 1326 | #endif |
| 1327 | |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1328 | static int __init arm_smmu_count_resources(struct acpi_iort_node *node) |
| 1329 | { |
| 1330 | struct acpi_iort_smmu *smmu; |
| 1331 | |
| 1332 | /* Retrieve SMMU specific data */ |
| 1333 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1334 | |
| 1335 | /* |
| 1336 | * Only consider the global fault interrupt and ignore the |
| 1337 | * configuration access interrupt. |
| 1338 | * |
| 1339 | * MMIO address and global fault interrupt resources are always |
| 1340 | * present so add them to the context interrupt count as a static |
| 1341 | * value. |
| 1342 | */ |
| 1343 | return smmu->context_interrupt_count + 2; |
| 1344 | } |
| 1345 | |
| 1346 | static void __init arm_smmu_init_resources(struct resource *res, |
| 1347 | struct acpi_iort_node *node) |
| 1348 | { |
| 1349 | struct acpi_iort_smmu *smmu; |
| 1350 | int i, hw_irq, trigger, num_res = 0; |
| 1351 | u64 *ctx_irq, *glb_irq; |
| 1352 | |
| 1353 | /* Retrieve SMMU specific data */ |
| 1354 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1355 | |
| 1356 | res[num_res].start = smmu->base_address; |
| 1357 | res[num_res].end = smmu->base_address + smmu->span - 1; |
| 1358 | res[num_res].flags = IORESOURCE_MEM; |
| 1359 | num_res++; |
| 1360 | |
| 1361 | glb_irq = ACPI_ADD_PTR(u64, node, smmu->global_interrupt_offset); |
| 1362 | /* Global IRQs */ |
| 1363 | hw_irq = IORT_IRQ_MASK(glb_irq[0]); |
| 1364 | trigger = IORT_IRQ_TRIGGER_MASK(glb_irq[0]); |
| 1365 | |
| 1366 | acpi_iort_register_irq(hw_irq, "arm-smmu-global", trigger, |
| 1367 | &res[num_res++]); |
| 1368 | |
| 1369 | /* Context IRQs */ |
| 1370 | ctx_irq = ACPI_ADD_PTR(u64, node, smmu->context_interrupt_offset); |
| 1371 | for (i = 0; i < smmu->context_interrupt_count; i++) { |
| 1372 | hw_irq = IORT_IRQ_MASK(ctx_irq[i]); |
| 1373 | trigger = IORT_IRQ_TRIGGER_MASK(ctx_irq[i]); |
| 1374 | |
| 1375 | acpi_iort_register_irq(hw_irq, "arm-smmu-context", trigger, |
| 1376 | &res[num_res++]); |
| 1377 | } |
| 1378 | } |
| 1379 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1380 | static void __init arm_smmu_dma_configure(struct device *dev, |
| 1381 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1382 | { |
| 1383 | struct acpi_iort_smmu *smmu; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1384 | enum dev_dma_attr attr; |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1385 | |
| 1386 | /* Retrieve SMMU specific data */ |
| 1387 | smmu = (struct acpi_iort_smmu *)node->node_data; |
| 1388 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1389 | attr = (smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK) ? |
| 1390 | DEV_DMA_COHERENT : DEV_DMA_NON_COHERENT; |
| 1391 | |
| 1392 | /* We expect the dma masks to be equivalent for SMMU set-ups */ |
| 1393 | dev->dma_mask = &dev->coherent_dma_mask; |
| 1394 | |
| 1395 | /* Configure DMA for the page table walker */ |
| 1396 | acpi_dma_configure(dev, attr); |
| 1397 | } |
| 1398 | |
| 1399 | static int __init arm_smmu_v3_pmcg_count_resources(struct acpi_iort_node *node) |
| 1400 | { |
| 1401 | struct acpi_iort_pmcg *pmcg; |
| 1402 | |
| 1403 | /* Retrieve PMCG specific data */ |
| 1404 | pmcg = (struct acpi_iort_pmcg *)node->node_data; |
| 1405 | |
| 1406 | /* |
| 1407 | * There are always 2 memory resources. |
| 1408 | * If the overflow_gsiv is present then add that for a total of 3. |
| 1409 | */ |
| 1410 | return pmcg->overflow_gsiv ? 3 : 2; |
| 1411 | } |
| 1412 | |
| 1413 | static void __init arm_smmu_v3_pmcg_init_resources(struct resource *res, |
| 1414 | struct acpi_iort_node *node) |
| 1415 | { |
| 1416 | struct acpi_iort_pmcg *pmcg; |
| 1417 | |
| 1418 | /* Retrieve PMCG specific data */ |
| 1419 | pmcg = (struct acpi_iort_pmcg *)node->node_data; |
| 1420 | |
| 1421 | res[0].start = pmcg->page0_base_address; |
| 1422 | res[0].end = pmcg->page0_base_address + SZ_4K - 1; |
| 1423 | res[0].flags = IORESOURCE_MEM; |
| 1424 | res[1].start = pmcg->page1_base_address; |
| 1425 | res[1].end = pmcg->page1_base_address + SZ_4K - 1; |
| 1426 | res[1].flags = IORESOURCE_MEM; |
| 1427 | |
| 1428 | if (pmcg->overflow_gsiv) |
| 1429 | acpi_iort_register_irq(pmcg->overflow_gsiv, "overflow", |
| 1430 | ACPI_EDGE_SENSITIVE, &res[2]); |
| 1431 | } |
| 1432 | |
Shameer Kolothum | 24062fe | 2019-03-26 15:17:53 +0000 | [diff] [blame] | 1433 | static struct acpi_platform_list pmcg_plat_info[] __initdata = { |
| 1434 | /* HiSilicon Hip08 Platform */ |
| 1435 | {"HISI ", "HIP08 ", 0, ACPI_SIG_IORT, greater_than_or_equal, |
| 1436 | "Erratum #162001800", IORT_SMMU_V3_PMCG_HISI_HIP08}, |
| 1437 | { } |
| 1438 | }; |
| 1439 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1440 | static int __init arm_smmu_v3_pmcg_add_platdata(struct platform_device *pdev) |
| 1441 | { |
Shameer Kolothum | 24062fe | 2019-03-26 15:17:53 +0000 | [diff] [blame] | 1442 | u32 model; |
| 1443 | int idx; |
| 1444 | |
| 1445 | idx = acpi_match_platform_list(pmcg_plat_info); |
| 1446 | if (idx >= 0) |
| 1447 | model = pmcg_plat_info[idx].data; |
| 1448 | else |
| 1449 | model = IORT_SMMU_V3_PMCG_GENERIC; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1450 | |
| 1451 | return platform_device_add_data(pdev, &model, sizeof(model)); |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1454 | struct iort_dev_config { |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1455 | const char *name; |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1456 | int (*dev_init)(struct acpi_iort_node *node); |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1457 | void (*dev_dma_configure)(struct device *dev, |
| 1458 | struct acpi_iort_node *node); |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1459 | int (*dev_count_resources)(struct acpi_iort_node *node); |
| 1460 | void (*dev_init_resources)(struct resource *res, |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1461 | struct acpi_iort_node *node); |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1462 | int (*dev_set_proximity)(struct device *dev, |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1463 | struct acpi_iort_node *node); |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1464 | int (*dev_add_platdata)(struct platform_device *pdev); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1465 | }; |
| 1466 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1467 | static const struct iort_dev_config iort_arm_smmu_v3_cfg __initconst = { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1468 | .name = "arm-smmu-v3", |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1469 | .dev_dma_configure = arm_smmu_v3_dma_configure, |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1470 | .dev_count_resources = arm_smmu_v3_count_resources, |
| 1471 | .dev_init_resources = arm_smmu_v3_init_resources, |
| 1472 | .dev_set_proximity = arm_smmu_v3_set_proximity, |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1473 | }; |
| 1474 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1475 | static const struct iort_dev_config iort_arm_smmu_cfg __initconst = { |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1476 | .name = "arm-smmu", |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1477 | .dev_dma_configure = arm_smmu_dma_configure, |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1478 | .dev_count_resources = arm_smmu_count_resources, |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1479 | .dev_init_resources = arm_smmu_init_resources, |
| 1480 | }; |
| 1481 | |
| 1482 | static const struct iort_dev_config iort_arm_smmu_v3_pmcg_cfg __initconst = { |
| 1483 | .name = "arm-smmu-v3-pmcg", |
| 1484 | .dev_count_resources = arm_smmu_v3_pmcg_count_resources, |
| 1485 | .dev_init_resources = arm_smmu_v3_pmcg_init_resources, |
| 1486 | .dev_add_platdata = arm_smmu_v3_pmcg_add_platdata, |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1487 | }; |
| 1488 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1489 | static __init const struct iort_dev_config *iort_get_dev_cfg( |
Lorenzo Pieralisi | e3d4939 | 2017-09-28 14:03:33 +0100 | [diff] [blame] | 1490 | struct acpi_iort_node *node) |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1491 | { |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1492 | switch (node->type) { |
| 1493 | case ACPI_IORT_NODE_SMMU_V3: |
| 1494 | return &iort_arm_smmu_v3_cfg; |
Lorenzo Pieralisi | d6fcd3b | 2016-11-21 10:01:45 +0000 | [diff] [blame] | 1495 | case ACPI_IORT_NODE_SMMU: |
| 1496 | return &iort_arm_smmu_cfg; |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1497 | case ACPI_IORT_NODE_PMCG: |
| 1498 | return &iort_arm_smmu_v3_pmcg_cfg; |
Lorenzo Pieralisi | e4dadfa | 2016-11-21 10:01:43 +0000 | [diff] [blame] | 1499 | default: |
| 1500 | return NULL; |
| 1501 | } |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /** |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1505 | * iort_add_platform_device() - Allocate a platform device for IORT node |
| 1506 | * @node: Pointer to device ACPI IORT node |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1507 | * |
| 1508 | * Returns: 0 on success, <0 failure |
| 1509 | */ |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1510 | static int __init iort_add_platform_device(struct acpi_iort_node *node, |
| 1511 | const struct iort_dev_config *ops) |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1512 | { |
| 1513 | struct fwnode_handle *fwnode; |
| 1514 | struct platform_device *pdev; |
| 1515 | struct resource *r; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1516 | int ret, count; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1517 | |
| 1518 | pdev = platform_device_alloc(ops->name, PLATFORM_DEVID_AUTO); |
| 1519 | if (!pdev) |
Dan Carpenter | 5e5afa6 | 2017-01-17 16:36:23 +0300 | [diff] [blame] | 1520 | return -ENOMEM; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1521 | |
Kefeng Wang | 36a2ba0 | 2019-04-08 23:21:12 +0800 | [diff] [blame] | 1522 | if (ops->dev_set_proximity) { |
| 1523 | ret = ops->dev_set_proximity(&pdev->dev, node); |
| 1524 | if (ret) |
| 1525 | goto dev_put; |
| 1526 | } |
Ganapatrao Kulkarni | 5fe0ce3 | 2017-08-02 10:58:25 -0700 | [diff] [blame] | 1527 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1528 | count = ops->dev_count_resources(node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1529 | |
| 1530 | r = kcalloc(count, sizeof(*r), GFP_KERNEL); |
| 1531 | if (!r) { |
| 1532 | ret = -ENOMEM; |
| 1533 | goto dev_put; |
| 1534 | } |
| 1535 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1536 | ops->dev_init_resources(r, node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1537 | |
| 1538 | ret = platform_device_add_resources(pdev, r, count); |
| 1539 | /* |
| 1540 | * Resources are duplicated in platform_device_add_resources, |
| 1541 | * free their allocated memory |
| 1542 | */ |
| 1543 | kfree(r); |
| 1544 | |
| 1545 | if (ret) |
| 1546 | goto dev_put; |
| 1547 | |
| 1548 | /* |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1549 | * Platform devices based on PMCG nodes uses platform_data to |
| 1550 | * pass the hardware model info to the driver. For others, add |
| 1551 | * a copy of IORT node pointer to platform_data to be used to |
| 1552 | * retrieve IORT data information. |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1553 | */ |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1554 | if (ops->dev_add_platdata) |
| 1555 | ret = ops->dev_add_platdata(pdev); |
| 1556 | else |
| 1557 | ret = platform_device_add_data(pdev, &node, sizeof(node)); |
| 1558 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1559 | if (ret) |
| 1560 | goto dev_put; |
| 1561 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1562 | fwnode = iort_get_fwnode(node); |
| 1563 | |
| 1564 | if (!fwnode) { |
| 1565 | ret = -ENODEV; |
| 1566 | goto dev_put; |
| 1567 | } |
| 1568 | |
| 1569 | pdev->dev.fwnode = fwnode; |
| 1570 | |
Neil Leeder | 24e5160 | 2019-03-26 15:17:50 +0000 | [diff] [blame] | 1571 | if (ops->dev_dma_configure) |
| 1572 | ops->dev_dma_configure(&pdev->dev, node); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1573 | |
Lorenzo Pieralisi | 6563790 | 2017-10-13 15:09:50 +0800 | [diff] [blame] | 1574 | iort_set_device_domain(&pdev->dev, node); |
| 1575 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1576 | ret = platform_device_add(pdev); |
| 1577 | if (ret) |
| 1578 | goto dma_deconfigure; |
| 1579 | |
| 1580 | return 0; |
| 1581 | |
| 1582 | dma_deconfigure: |
Christoph Hellwig | dc3c055 | 2018-08-24 10:28:18 +0200 | [diff] [blame] | 1583 | arch_teardown_dma_ops(&pdev->dev); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1584 | dev_put: |
| 1585 | platform_device_put(pdev); |
| 1586 | |
| 1587 | return ret; |
| 1588 | } |
| 1589 | |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1590 | #ifdef CONFIG_PCI |
| 1591 | static void __init iort_enable_acs(struct acpi_iort_node *iort_node) |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1592 | { |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1593 | static bool acs_enabled __initdata; |
| 1594 | |
| 1595 | if (acs_enabled) |
| 1596 | return; |
| 1597 | |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1598 | if (iort_node->type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) { |
| 1599 | struct acpi_iort_node *parent; |
| 1600 | struct acpi_iort_id_mapping *map; |
| 1601 | int i; |
| 1602 | |
| 1603 | map = ACPI_ADD_PTR(struct acpi_iort_id_mapping, iort_node, |
| 1604 | iort_node->mapping_offset); |
| 1605 | |
| 1606 | for (i = 0; i < iort_node->mapping_count; i++, map++) { |
| 1607 | if (!map->output_reference) |
| 1608 | continue; |
| 1609 | |
| 1610 | parent = ACPI_ADD_PTR(struct acpi_iort_node, |
| 1611 | iort_table, map->output_reference); |
| 1612 | /* |
| 1613 | * If we detect a RC->SMMU mapping, make sure |
| 1614 | * we enable ACS on the system. |
| 1615 | */ |
| 1616 | if ((parent->type == ACPI_IORT_NODE_SMMU) || |
| 1617 | (parent->type == ACPI_IORT_NODE_SMMU_V3)) { |
| 1618 | pci_request_acs(); |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1619 | acs_enabled = true; |
| 1620 | return; |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | } |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1624 | } |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1625 | #else |
| 1626 | static inline void iort_enable_acs(struct acpi_iort_node *iort_node) { } |
| 1627 | #endif |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1628 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1629 | static void __init iort_init_platform_devices(void) |
| 1630 | { |
| 1631 | struct acpi_iort_node *iort_node, *iort_end; |
| 1632 | struct acpi_table_iort *iort; |
| 1633 | struct fwnode_handle *fwnode; |
| 1634 | int i, ret; |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1635 | const struct iort_dev_config *ops; |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * iort_table and iort both point to the start of IORT table, but |
| 1639 | * have different struct types |
| 1640 | */ |
| 1641 | iort = (struct acpi_table_iort *)iort_table; |
| 1642 | |
| 1643 | /* Get the first IORT node */ |
| 1644 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1645 | iort->node_offset); |
| 1646 | iort_end = ACPI_ADD_PTR(struct acpi_iort_node, iort, |
| 1647 | iort_table->length); |
| 1648 | |
| 1649 | for (i = 0; i < iort->node_count; i++) { |
| 1650 | if (iort_node >= iort_end) { |
| 1651 | pr_err("iort node pointer overflows, bad table\n"); |
| 1652 | return; |
| 1653 | } |
| 1654 | |
Sinan Kaya | 43554ce | 2018-12-19 22:46:58 +0000 | [diff] [blame] | 1655 | iort_enable_acs(iort_node); |
Lorenzo Pieralisi | 37f6b42 | 2017-10-02 18:28:44 +0100 | [diff] [blame] | 1656 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1657 | ops = iort_get_dev_cfg(iort_node); |
| 1658 | if (ops) { |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1659 | fwnode = acpi_alloc_fwnode_static(); |
| 1660 | if (!fwnode) |
| 1661 | return; |
| 1662 | |
| 1663 | iort_set_fwnode(iort_node, fwnode); |
| 1664 | |
Lorenzo Pieralisi | 896dd2c | 2017-09-20 17:03:58 +0100 | [diff] [blame] | 1665 | ret = iort_add_platform_device(iort_node, ops); |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1666 | if (ret) { |
| 1667 | iort_delete_fwnode(iort_node); |
| 1668 | acpi_free_fwnode_static(fwnode); |
| 1669 | return; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | iort_node = ACPI_ADD_PTR(struct acpi_iort_node, iort_node, |
| 1674 | iort_node->length); |
| 1675 | } |
| 1676 | } |
| 1677 | |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1678 | void __init acpi_iort_init(void) |
| 1679 | { |
| 1680 | acpi_status status; |
| 1681 | |
Hanjun Guo | 701dafe | 2020-05-08 12:05:53 +0800 | [diff] [blame] | 1682 | /* iort_table will be used at runtime after the iort init, |
| 1683 | * so we don't need to call acpi_put_table() to release |
| 1684 | * the IORT table mapping. |
| 1685 | */ |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1686 | status = acpi_get_table(ACPI_SIG_IORT, 0, &iort_table); |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1687 | if (ACPI_FAILURE(status)) { |
| 1688 | if (status != AE_NOT_FOUND) { |
| 1689 | const char *msg = acpi_format_exception(status); |
| 1690 | |
| 1691 | pr_err("Failed to get table, %s\n", msg); |
| 1692 | } |
| 1693 | |
| 1694 | return; |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1695 | } |
Lorenzo Pieralisi | 34ceea2 | 2016-11-21 10:01:34 +0000 | [diff] [blame] | 1696 | |
Lorenzo Pieralisi | 846f0e9 | 2016-11-21 10:01:41 +0000 | [diff] [blame] | 1697 | iort_init_platform_devices(); |
Tomasz Nowicki | 88ef16d | 2016-09-12 20:54:20 +0200 | [diff] [blame] | 1698 | } |