Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 1 | #define pr_fmt(fmt) "OF: PCI: " fmt |
| 2 | |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 3 | #include <linux/kernel.h> |
Paul Gortmaker | 9775913 | 2011-05-27 17:06:52 -0400 | [diff] [blame] | 4 | #include <linux/export.h> |
Grant Likely | a642285 | 2011-07-23 23:52:48 -0600 | [diff] [blame] | 5 | #include <linux/of.h> |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 6 | #include <linux/of_address.h> |
Murali Karicheri | c49b8fc | 2015-03-03 12:52:12 -0500 | [diff] [blame] | 7 | #include <linux/of_device.h> |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 8 | #include <linux/of_pci.h> |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 9 | #include <linux/slab.h> |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 10 | |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 11 | static inline int __of_pci_pci_compare(struct device_node *node, |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 12 | unsigned int data) |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 13 | { |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 14 | int devfn; |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 15 | |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 16 | devfn = of_pci_get_devfn(node); |
| 17 | if (devfn < 0) |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 18 | return 0; |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 19 | |
| 20 | return devfn == data; |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 21 | } |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 22 | |
| 23 | struct device_node *of_pci_find_child_device(struct device_node *parent, |
| 24 | unsigned int devfn) |
| 25 | { |
| 26 | struct device_node *node, *node2; |
| 27 | |
| 28 | for_each_child_of_node(parent, node) { |
| 29 | if (__of_pci_pci_compare(node, devfn)) |
| 30 | return node; |
| 31 | /* |
| 32 | * Some OFs create a parent node "multifunc-device" as |
| 33 | * a fake root for all functions of a multi-function |
| 34 | * device we go down them as well. |
| 35 | */ |
| 36 | if (!strcmp(node->name, "multifunc-device")) { |
| 37 | for_each_child_of_node(node, node2) { |
| 38 | if (__of_pci_pci_compare(node2, devfn)) { |
| 39 | of_node_put(node); |
| 40 | return node2; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(of_pci_find_child_device); |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * of_pci_get_devfn() - Get device and function numbers for a device node |
| 51 | * @np: device node |
| 52 | * |
| 53 | * Parses a standard 5-cell PCI resource and returns an 8-bit value that can |
| 54 | * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device |
| 55 | * and function numbers respectively. On error a negative error code is |
| 56 | * returned. |
| 57 | */ |
| 58 | int of_pci_get_devfn(struct device_node *np) |
| 59 | { |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 60 | u32 reg[5]; |
| 61 | int error; |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 62 | |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 63 | error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg)); |
| 64 | if (error) |
| 65 | return error; |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 66 | |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 67 | return (reg[0] >> 8) & 0xff; |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 68 | } |
| 69 | EXPORT_SYMBOL_GPL(of_pci_get_devfn); |
Thierry Reding | 4e23d3f | 2013-05-16 17:55:19 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * of_pci_parse_bus_range() - parse the bus-range property of a PCI device |
| 73 | * @node: device node |
| 74 | * @res: address to a struct resource to return the bus-range |
| 75 | * |
| 76 | * Returns 0 on success or a negative error-code on failure. |
| 77 | */ |
| 78 | int of_pci_parse_bus_range(struct device_node *node, struct resource *res) |
| 79 | { |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 80 | u32 bus_range[2]; |
| 81 | int error; |
Thierry Reding | 4e23d3f | 2013-05-16 17:55:19 +0200 | [diff] [blame] | 82 | |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 83 | error = of_property_read_u32_array(node, "bus-range", bus_range, |
| 84 | ARRAY_SIZE(bus_range)); |
| 85 | if (error) |
| 86 | return error; |
Thierry Reding | 4e23d3f | 2013-05-16 17:55:19 +0200 | [diff] [blame] | 87 | |
| 88 | res->name = node->name; |
Sergei Shtylyov | e2af1f5 | 2017-07-23 22:41:46 +0300 | [diff] [blame] | 89 | res->start = bus_range[0]; |
| 90 | res->end = bus_range[1]; |
Thierry Reding | 4e23d3f | 2013-05-16 17:55:19 +0200 | [diff] [blame] | 91 | res->flags = IORESOURCE_BUS; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 96 | |
Liviu Dudau | 41e5c0f | 2014-09-29 15:29:27 +0100 | [diff] [blame] | 97 | /** |
| 98 | * This function will try to obtain the host bridge domain number by |
| 99 | * finding a property called "linux,pci-domain" of the given device node. |
| 100 | * |
| 101 | * @node: device tree node with the domain information |
| 102 | * |
| 103 | * Returns the associated domain number from DT in the range [0-0xffff], or |
| 104 | * a negative value if the required property is not found. |
| 105 | */ |
| 106 | int of_get_pci_domain_nr(struct device_node *node) |
| 107 | { |
Sergei Shtylyov | 56134e3 | 2017-07-23 22:41:45 +0300 | [diff] [blame] | 108 | u32 domain; |
| 109 | int error; |
Liviu Dudau | 41e5c0f | 2014-09-29 15:29:27 +0100 | [diff] [blame] | 110 | |
Sergei Shtylyov | 56134e3 | 2017-07-23 22:41:45 +0300 | [diff] [blame] | 111 | error = of_property_read_u32(node, "linux,pci-domain", &domain); |
| 112 | if (error) |
| 113 | return error; |
Liviu Dudau | 41e5c0f | 2014-09-29 15:29:27 +0100 | [diff] [blame] | 114 | |
Sergei Shtylyov | 56134e3 | 2017-07-23 22:41:45 +0300 | [diff] [blame] | 115 | return (u16)domain; |
Liviu Dudau | 41e5c0f | 2014-09-29 15:29:27 +0100 | [diff] [blame] | 116 | } |
| 117 | EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); |
| 118 | |
Murali Karicheri | c49b8fc | 2015-03-03 12:52:12 -0500 | [diff] [blame] | 119 | /** |
Shawn Lin | 9a1dc38 | 2016-11-14 15:21:14 -0600 | [diff] [blame] | 120 | * This function will try to find the limitation of link speed by finding |
| 121 | * a property called "max-link-speed" of the given device node. |
| 122 | * |
| 123 | * @node: device tree node with the max link speed information |
| 124 | * |
| 125 | * Returns the associated max link speed from DT, or a negative value if the |
| 126 | * required property is not found or is invalid. |
| 127 | */ |
| 128 | int of_pci_get_max_link_speed(struct device_node *node) |
| 129 | { |
| 130 | u32 max_link_speed; |
| 131 | |
| 132 | if (of_property_read_u32(node, "max-link-speed", &max_link_speed) || |
| 133 | max_link_speed > 4) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | return max_link_speed; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); |
| 139 | |
| 140 | /** |
Marc Zyngier | f81c11a | 2015-09-04 17:50:08 +0100 | [diff] [blame] | 141 | * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only |
| 142 | * is present and valid |
| 143 | */ |
| 144 | void of_pci_check_probe_only(void) |
| 145 | { |
| 146 | u32 val; |
| 147 | int ret; |
| 148 | |
| 149 | ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val); |
| 150 | if (ret) { |
| 151 | if (ret == -ENODATA || ret == -EOVERFLOW) |
| 152 | pr_warn("linux,pci-probe-only without valid value, ignoring\n"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (val) |
| 157 | pci_add_flags(PCI_PROBE_ONLY); |
| 158 | else |
| 159 | pci_clear_flags(PCI_PROBE_ONLY); |
| 160 | |
Rob Herring | 606ad42 | 2016-06-15 08:32:18 -0500 | [diff] [blame] | 161 | pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis"); |
Marc Zyngier | f81c11a | 2015-09-04 17:50:08 +0100 | [diff] [blame] | 162 | } |
| 163 | EXPORT_SYMBOL_GPL(of_pci_check_probe_only); |
| 164 | |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 165 | #if defined(CONFIG_OF_ADDRESS) |
| 166 | /** |
| 167 | * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT |
| 168 | * @dev: device node of the host bridge having the range property |
| 169 | * @busno: bus number associated with the bridge root bus |
| 170 | * @bus_max: maximum number of buses for this bridge |
| 171 | * @resources: list where the range of resources will be added after DT parsing |
| 172 | * @io_base: pointer to a variable that will contain on return the physical |
| 173 | * address for the start of the I/O range. Can be NULL if the caller doesn't |
| 174 | * expect IO ranges to be present in the device tree. |
| 175 | * |
| 176 | * It is the caller's job to free the @resources list. |
| 177 | * |
| 178 | * This function will parse the "ranges" property of a PCI host bridge device |
| 179 | * node and setup the resource mapping based on its content. It is expected |
| 180 | * that the property conforms with the Power ePAPR document. |
| 181 | * |
| 182 | * It returns zero if the range parsing has been successful or a standard error |
| 183 | * value if it failed. |
| 184 | */ |
| 185 | int of_pci_get_host_bridge_resources(struct device_node *dev, |
| 186 | unsigned char busno, unsigned char bus_max, |
| 187 | struct list_head *resources, resource_size_t *io_base) |
| 188 | { |
Rafael J. Wysocki | 5c493df | 2015-02-09 15:40:30 +0100 | [diff] [blame] | 189 | struct resource_entry *window; |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 190 | struct resource *res; |
| 191 | struct resource *bus_range; |
| 192 | struct of_pci_range range; |
| 193 | struct of_pci_range_parser parser; |
| 194 | char range_type[4]; |
| 195 | int err; |
| 196 | |
| 197 | if (io_base) |
| 198 | *io_base = (resource_size_t)OF_BAD_ADDR; |
| 199 | |
| 200 | bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); |
| 201 | if (!bus_range) |
| 202 | return -ENOMEM; |
| 203 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 204 | pr_info("host bridge %pOF ranges:\n", dev); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 205 | |
| 206 | err = of_pci_parse_bus_range(dev, bus_range); |
| 207 | if (err) { |
| 208 | bus_range->start = busno; |
| 209 | bus_range->end = bus_max; |
| 210 | bus_range->flags = IORESOURCE_BUS; |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 211 | pr_info(" No bus range found for %pOF, using %pR\n", |
| 212 | dev, bus_range); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 213 | } else { |
| 214 | if (bus_range->end > bus_range->start + bus_max) |
| 215 | bus_range->end = bus_range->start + bus_max; |
| 216 | } |
| 217 | pci_add_resource(resources, bus_range); |
| 218 | |
| 219 | /* Check for ranges property */ |
| 220 | err = of_pci_range_parser_init(&parser, dev); |
| 221 | if (err) |
| 222 | goto parse_failed; |
| 223 | |
| 224 | pr_debug("Parsing ranges property...\n"); |
| 225 | for_each_of_pci_range(&parser, &range) { |
| 226 | /* Read next ranges element */ |
| 227 | if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) |
| 228 | snprintf(range_type, 4, " IO"); |
| 229 | else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) |
| 230 | snprintf(range_type, 4, "MEM"); |
| 231 | else |
| 232 | snprintf(range_type, 4, "err"); |
| 233 | pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type, |
| 234 | range.cpu_addr, range.cpu_addr + range.size - 1, |
| 235 | range.pci_addr); |
| 236 | |
| 237 | /* |
| 238 | * If we failed translation or got a zero-sized region |
| 239 | * then skip this range |
| 240 | */ |
| 241 | if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) |
| 242 | continue; |
| 243 | |
| 244 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 245 | if (!res) { |
| 246 | err = -ENOMEM; |
| 247 | goto parse_failed; |
| 248 | } |
| 249 | |
| 250 | err = of_pci_range_to_resource(&range, dev, res); |
Pavel Fedin | f134f25 | 2015-10-08 10:24:26 +0300 | [diff] [blame] | 251 | if (err) { |
| 252 | kfree(res); |
| 253 | continue; |
| 254 | } |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 255 | |
| 256 | if (resource_type(res) == IORESOURCE_IO) { |
| 257 | if (!io_base) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 258 | pr_err("I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n", |
| 259 | dev); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 260 | err = -EINVAL; |
| 261 | goto conversion_failed; |
| 262 | } |
| 263 | if (*io_base != (resource_size_t)OF_BAD_ADDR) |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 264 | pr_warn("More than one I/O resource converted for %pOF. CPU base address for old range lost!\n", |
| 265 | dev); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 266 | *io_base = range.cpu_addr; |
| 267 | } |
| 268 | |
| 269 | pci_add_resource_offset(resources, res, res->start - range.pci_addr); |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | |
| 274 | conversion_failed: |
| 275 | kfree(res); |
| 276 | parse_failed: |
Rafael J. Wysocki | 5c493df | 2015-02-09 15:40:30 +0100 | [diff] [blame] | 277 | resource_list_for_each_entry(window, resources) |
Lorenzo Pieralisi | d2be00c | 2015-01-27 18:01:45 +0000 | [diff] [blame] | 278 | kfree(window->res); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 279 | pci_free_resource_list(resources); |
| 280 | return err; |
| 281 | } |
| 282 | EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); |
| 283 | #endif /* CONFIG_OF_ADDRESS */ |
| 284 | |
Robin Murphy | 987068f | 2016-09-12 17:13:40 +0100 | [diff] [blame] | 285 | /** |
| 286 | * of_pci_map_rid - Translate a requester ID through a downstream mapping. |
| 287 | * @np: root complex device node. |
| 288 | * @rid: PCI requester ID to map. |
| 289 | * @map_name: property name of the map to use. |
| 290 | * @map_mask_name: optional property name of the mask to use. |
| 291 | * @target: optional pointer to a target device node. |
| 292 | * @id_out: optional pointer to receive the translated ID. |
| 293 | * |
| 294 | * Given a PCI requester ID, look up the appropriate implementation-defined |
| 295 | * platform ID and/or the target device which receives transactions on that |
| 296 | * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or |
| 297 | * @id_out may be NULL if only the other is required. If @target points to |
| 298 | * a non-NULL device node pointer, only entries targeting that node will be |
| 299 | * matched; if it points to a NULL value, it will receive the device node of |
| 300 | * the first matching target phandle, with a reference held. |
| 301 | * |
| 302 | * Return: 0 on success or a standard error code on failure. |
| 303 | */ |
| 304 | int of_pci_map_rid(struct device_node *np, u32 rid, |
| 305 | const char *map_name, const char *map_mask_name, |
| 306 | struct device_node **target, u32 *id_out) |
| 307 | { |
| 308 | u32 map_mask, masked_rid; |
| 309 | int map_len; |
| 310 | const __be32 *map = NULL; |
| 311 | |
| 312 | if (!np || !map_name || (!target && !id_out)) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | map = of_get_property(np, map_name, &map_len); |
| 316 | if (!map) { |
| 317 | if (target) |
| 318 | return -ENODEV; |
| 319 | /* Otherwise, no map implies no translation */ |
| 320 | *id_out = rid; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | if (!map_len || map_len % (4 * sizeof(*map))) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 325 | pr_err("%pOF: Error: Bad %s length: %d\n", np, |
Robin Murphy | 987068f | 2016-09-12 17:13:40 +0100 | [diff] [blame] | 326 | map_name, map_len); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | |
| 330 | /* The default is to select all bits. */ |
| 331 | map_mask = 0xffffffff; |
| 332 | |
| 333 | /* |
| 334 | * Can be overridden by "{iommu,msi}-map-mask" property. |
| 335 | * If of_property_read_u32() fails, the default is used. |
| 336 | */ |
| 337 | if (map_mask_name) |
| 338 | of_property_read_u32(np, map_mask_name, &map_mask); |
| 339 | |
| 340 | masked_rid = map_mask & rid; |
| 341 | for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { |
| 342 | struct device_node *phandle_node; |
| 343 | u32 rid_base = be32_to_cpup(map + 0); |
| 344 | u32 phandle = be32_to_cpup(map + 1); |
| 345 | u32 out_base = be32_to_cpup(map + 2); |
| 346 | u32 rid_len = be32_to_cpup(map + 3); |
| 347 | |
| 348 | if (rid_base & ~map_mask) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 349 | pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", |
| 350 | np, map_name, map_name, |
Robin Murphy | 987068f | 2016-09-12 17:13:40 +0100 | [diff] [blame] | 351 | map_mask, rid_base); |
| 352 | return -EFAULT; |
| 353 | } |
| 354 | |
| 355 | if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) |
| 356 | continue; |
| 357 | |
| 358 | phandle_node = of_find_node_by_phandle(phandle); |
| 359 | if (!phandle_node) |
| 360 | return -ENODEV; |
| 361 | |
| 362 | if (target) { |
| 363 | if (*target) |
| 364 | of_node_put(phandle_node); |
| 365 | else |
| 366 | *target = phandle_node; |
| 367 | |
| 368 | if (*target != phandle_node) |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if (id_out) |
| 373 | *id_out = masked_rid - rid_base + out_base; |
| 374 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 375 | pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", |
| 376 | np, map_name, map_mask, rid_base, out_base, |
Robin Murphy | c545f63 | 2017-11-15 12:50:06 +0000 | [diff] [blame] | 377 | rid_len, rid, masked_rid - rid_base + out_base); |
Robin Murphy | 987068f | 2016-09-12 17:13:40 +0100 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 381 | pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", |
| 382 | np, map_name, rid, target && *target ? *target : NULL); |
Robin Murphy | 987068f | 2016-09-12 17:13:40 +0100 | [diff] [blame] | 383 | return -EFAULT; |
| 384 | } |