.. | .. |
---|
15 | 15 | #include <linux/of_pci.h> |
---|
16 | 16 | #include "pci.h" |
---|
17 | 17 | |
---|
| 18 | +#ifdef CONFIG_PCI |
---|
18 | 19 | void pci_set_of_node(struct pci_dev *dev) |
---|
19 | 20 | { |
---|
20 | 21 | if (!dev->bus->dev.of_node) |
---|
21 | 22 | return; |
---|
22 | 23 | dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node, |
---|
23 | 24 | dev->devfn); |
---|
| 25 | + if (dev->dev.of_node) |
---|
| 26 | + dev->dev.fwnode = &dev->dev.of_node->fwnode; |
---|
24 | 27 | } |
---|
25 | 28 | |
---|
26 | 29 | void pci_release_of_node(struct pci_dev *dev) |
---|
27 | 30 | { |
---|
28 | 31 | of_node_put(dev->dev.of_node); |
---|
29 | 32 | dev->dev.of_node = NULL; |
---|
| 33 | + dev->dev.fwnode = NULL; |
---|
30 | 34 | } |
---|
31 | 35 | |
---|
32 | 36 | void pci_set_bus_of_node(struct pci_bus *bus) |
---|
33 | 37 | { |
---|
34 | | - if (bus->self == NULL) |
---|
35 | | - bus->dev.of_node = pcibios_get_phb_of_node(bus); |
---|
36 | | - else |
---|
37 | | - bus->dev.of_node = of_node_get(bus->self->dev.of_node); |
---|
| 38 | + struct device_node *node; |
---|
| 39 | + |
---|
| 40 | + if (bus->self == NULL) { |
---|
| 41 | + node = pcibios_get_phb_of_node(bus); |
---|
| 42 | + } else { |
---|
| 43 | + node = of_node_get(bus->self->dev.of_node); |
---|
| 44 | + if (node && of_property_read_bool(node, "external-facing")) |
---|
| 45 | + bus->self->external_facing = true; |
---|
| 46 | + } |
---|
| 47 | + |
---|
| 48 | + bus->dev.of_node = node; |
---|
| 49 | + |
---|
| 50 | + if (bus->dev.of_node) |
---|
| 51 | + bus->dev.fwnode = &bus->dev.of_node->fwnode; |
---|
38 | 52 | } |
---|
39 | 53 | |
---|
40 | 54 | void pci_release_bus_of_node(struct pci_bus *bus) |
---|
41 | 55 | { |
---|
42 | 56 | of_node_put(bus->dev.of_node); |
---|
43 | 57 | bus->dev.of_node = NULL; |
---|
| 58 | + bus->dev.fwnode = NULL; |
---|
44 | 59 | } |
---|
45 | 60 | |
---|
46 | 61 | struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus) |
---|
.. | .. |
---|
113 | 128 | * a fake root for all functions of a multi-function |
---|
114 | 129 | * device we go down them as well. |
---|
115 | 130 | */ |
---|
116 | | - if (!strcmp(node->name, "multifunc-device")) { |
---|
| 131 | + if (of_node_name_eq(node, "multifunc-device")) { |
---|
117 | 132 | for_each_child_of_node(node, node2) { |
---|
118 | 133 | if (__of_pci_pci_compare(node2, devfn)) { |
---|
119 | 134 | of_node_put(node); |
---|
.. | .. |
---|
197 | 212 | EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); |
---|
198 | 213 | |
---|
199 | 214 | /** |
---|
200 | | - * This function will try to find the limitation of link speed by finding |
---|
201 | | - * a property called "max-link-speed" of the given device node. |
---|
202 | | - * |
---|
203 | | - * @node: device tree node with the max link speed information |
---|
204 | | - * |
---|
205 | | - * Returns the associated max link speed from DT, or a negative value if the |
---|
206 | | - * required property is not found or is invalid. |
---|
207 | | - */ |
---|
208 | | -int of_pci_get_max_link_speed(struct device_node *node) |
---|
209 | | -{ |
---|
210 | | - u32 max_link_speed; |
---|
211 | | - |
---|
212 | | - if (of_property_read_u32(node, "max-link-speed", &max_link_speed) || |
---|
213 | | - max_link_speed > 4) |
---|
214 | | - return -EINVAL; |
---|
215 | | - |
---|
216 | | - return max_link_speed; |
---|
217 | | -} |
---|
218 | | -EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); |
---|
219 | | - |
---|
220 | | -/** |
---|
221 | 215 | * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only |
---|
222 | 216 | * is present and valid |
---|
223 | 217 | */ |
---|
.. | .. |
---|
242 | 236 | } |
---|
243 | 237 | EXPORT_SYMBOL_GPL(of_pci_check_probe_only); |
---|
244 | 238 | |
---|
245 | | -#if defined(CONFIG_OF_ADDRESS) |
---|
246 | 239 | /** |
---|
247 | 240 | * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI |
---|
248 | 241 | * host bridge resources from DT |
---|
.. | .. |
---|
250 | 243 | * @busno: bus number associated with the bridge root bus |
---|
251 | 244 | * @bus_max: maximum number of buses for this bridge |
---|
252 | 245 | * @resources: list where the range of resources will be added after DT parsing |
---|
| 246 | + * @ib_resources: list where the range of inbound resources (with addresses |
---|
| 247 | + * from 'dma-ranges') will be added after DT parsing |
---|
253 | 248 | * @io_base: pointer to a variable that will contain on return the physical |
---|
254 | 249 | * address for the start of the I/O range. Can be NULL if the caller doesn't |
---|
255 | 250 | * expect I/O ranges to be present in the device tree. |
---|
.. | .. |
---|
261 | 256 | * It returns zero if the range parsing has been successful or a standard error |
---|
262 | 257 | * value if it failed. |
---|
263 | 258 | */ |
---|
264 | | -int devm_of_pci_get_host_bridge_resources(struct device *dev, |
---|
| 259 | +static int devm_of_pci_get_host_bridge_resources(struct device *dev, |
---|
265 | 260 | unsigned char busno, unsigned char bus_max, |
---|
266 | | - struct list_head *resources, resource_size_t *io_base) |
---|
| 261 | + struct list_head *resources, |
---|
| 262 | + struct list_head *ib_resources, |
---|
| 263 | + resource_size_t *io_base) |
---|
267 | 264 | { |
---|
268 | 265 | struct device_node *dev_node = dev->of_node; |
---|
269 | 266 | struct resource *res, tmp_res; |
---|
270 | 267 | struct resource *bus_range; |
---|
271 | 268 | struct of_pci_range range; |
---|
272 | 269 | struct of_pci_range_parser parser; |
---|
273 | | - char range_type[4]; |
---|
| 270 | + const char *range_type; |
---|
274 | 271 | int err; |
---|
275 | 272 | |
---|
276 | 273 | if (io_base) |
---|
.. | .. |
---|
298 | 295 | /* Check for ranges property */ |
---|
299 | 296 | err = of_pci_range_parser_init(&parser, dev_node); |
---|
300 | 297 | if (err) |
---|
301 | | - goto failed; |
---|
| 298 | + return 0; |
---|
302 | 299 | |
---|
303 | 300 | dev_dbg(dev, "Parsing ranges property...\n"); |
---|
304 | 301 | for_each_of_pci_range(&parser, &range) { |
---|
305 | 302 | /* Read next ranges element */ |
---|
306 | 303 | if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) |
---|
307 | | - snprintf(range_type, 4, " IO"); |
---|
| 304 | + range_type = "IO"; |
---|
308 | 305 | else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) |
---|
309 | | - snprintf(range_type, 4, "MEM"); |
---|
| 306 | + range_type = "MEM"; |
---|
310 | 307 | else |
---|
311 | | - snprintf(range_type, 4, "err"); |
---|
312 | | - dev_info(dev, " %s %#010llx..%#010llx -> %#010llx\n", |
---|
| 308 | + range_type = "err"; |
---|
| 309 | + dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n", |
---|
313 | 310 | range_type, range.cpu_addr, |
---|
314 | 311 | range.cpu_addr + range.size - 1, range.pci_addr); |
---|
315 | 312 | |
---|
.. | .. |
---|
346 | 343 | pci_add_resource_offset(resources, res, res->start - range.pci_addr); |
---|
347 | 344 | } |
---|
348 | 345 | |
---|
| 346 | + /* Check for dma-ranges property */ |
---|
| 347 | + if (!ib_resources) |
---|
| 348 | + return 0; |
---|
| 349 | + err = of_pci_dma_range_parser_init(&parser, dev_node); |
---|
| 350 | + if (err) |
---|
| 351 | + return 0; |
---|
| 352 | + |
---|
| 353 | + dev_dbg(dev, "Parsing dma-ranges property...\n"); |
---|
| 354 | + for_each_of_pci_range(&parser, &range) { |
---|
| 355 | + struct resource_entry *entry; |
---|
| 356 | + /* |
---|
| 357 | + * If we failed translation or got a zero-sized region |
---|
| 358 | + * then skip this range |
---|
| 359 | + */ |
---|
| 360 | + if (((range.flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM) || |
---|
| 361 | + range.cpu_addr == OF_BAD_ADDR || range.size == 0) |
---|
| 362 | + continue; |
---|
| 363 | + |
---|
| 364 | + dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n", |
---|
| 365 | + "IB MEM", range.cpu_addr, |
---|
| 366 | + range.cpu_addr + range.size - 1, range.pci_addr); |
---|
| 367 | + |
---|
| 368 | + |
---|
| 369 | + err = of_pci_range_to_resource(&range, dev_node, &tmp_res); |
---|
| 370 | + if (err) |
---|
| 371 | + continue; |
---|
| 372 | + |
---|
| 373 | + res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL); |
---|
| 374 | + if (!res) { |
---|
| 375 | + err = -ENOMEM; |
---|
| 376 | + goto failed; |
---|
| 377 | + } |
---|
| 378 | + |
---|
| 379 | + /* Keep the resource list sorted */ |
---|
| 380 | + resource_list_for_each_entry(entry, ib_resources) |
---|
| 381 | + if (entry->res->start > res->start) |
---|
| 382 | + break; |
---|
| 383 | + |
---|
| 384 | + pci_add_resource_offset(&entry->node, res, |
---|
| 385 | + res->start - range.pci_addr); |
---|
| 386 | + } |
---|
| 387 | + |
---|
349 | 388 | return 0; |
---|
350 | 389 | |
---|
351 | 390 | failed: |
---|
352 | 391 | pci_free_resource_list(resources); |
---|
353 | 392 | return err; |
---|
354 | 393 | } |
---|
355 | | -EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources); |
---|
356 | | -#endif /* CONFIG_OF_ADDRESS */ |
---|
357 | | - |
---|
358 | | -/** |
---|
359 | | - * of_pci_map_rid - Translate a requester ID through a downstream mapping. |
---|
360 | | - * @np: root complex device node. |
---|
361 | | - * @rid: PCI requester ID to map. |
---|
362 | | - * @map_name: property name of the map to use. |
---|
363 | | - * @map_mask_name: optional property name of the mask to use. |
---|
364 | | - * @target: optional pointer to a target device node. |
---|
365 | | - * @id_out: optional pointer to receive the translated ID. |
---|
366 | | - * |
---|
367 | | - * Given a PCI requester ID, look up the appropriate implementation-defined |
---|
368 | | - * platform ID and/or the target device which receives transactions on that |
---|
369 | | - * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or |
---|
370 | | - * @id_out may be NULL if only the other is required. If @target points to |
---|
371 | | - * a non-NULL device node pointer, only entries targeting that node will be |
---|
372 | | - * matched; if it points to a NULL value, it will receive the device node of |
---|
373 | | - * the first matching target phandle, with a reference held. |
---|
374 | | - * |
---|
375 | | - * Return: 0 on success or a standard error code on failure. |
---|
376 | | - */ |
---|
377 | | -int of_pci_map_rid(struct device_node *np, u32 rid, |
---|
378 | | - const char *map_name, const char *map_mask_name, |
---|
379 | | - struct device_node **target, u32 *id_out) |
---|
380 | | -{ |
---|
381 | | - u32 map_mask, masked_rid; |
---|
382 | | - int map_len; |
---|
383 | | - const __be32 *map = NULL; |
---|
384 | | - |
---|
385 | | - if (!np || !map_name || (!target && !id_out)) |
---|
386 | | - return -EINVAL; |
---|
387 | | - |
---|
388 | | - map = of_get_property(np, map_name, &map_len); |
---|
389 | | - if (!map) { |
---|
390 | | - if (target) |
---|
391 | | - return -ENODEV; |
---|
392 | | - /* Otherwise, no map implies no translation */ |
---|
393 | | - *id_out = rid; |
---|
394 | | - return 0; |
---|
395 | | - } |
---|
396 | | - |
---|
397 | | - if (!map_len || map_len % (4 * sizeof(*map))) { |
---|
398 | | - pr_err("%pOF: Error: Bad %s length: %d\n", np, |
---|
399 | | - map_name, map_len); |
---|
400 | | - return -EINVAL; |
---|
401 | | - } |
---|
402 | | - |
---|
403 | | - /* The default is to select all bits. */ |
---|
404 | | - map_mask = 0xffffffff; |
---|
405 | | - |
---|
406 | | - /* |
---|
407 | | - * Can be overridden by "{iommu,msi}-map-mask" property. |
---|
408 | | - * If of_property_read_u32() fails, the default is used. |
---|
409 | | - */ |
---|
410 | | - if (map_mask_name) |
---|
411 | | - of_property_read_u32(np, map_mask_name, &map_mask); |
---|
412 | | - |
---|
413 | | - masked_rid = map_mask & rid; |
---|
414 | | - for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) { |
---|
415 | | - struct device_node *phandle_node; |
---|
416 | | - u32 rid_base = be32_to_cpup(map + 0); |
---|
417 | | - u32 phandle = be32_to_cpup(map + 1); |
---|
418 | | - u32 out_base = be32_to_cpup(map + 2); |
---|
419 | | - u32 rid_len = be32_to_cpup(map + 3); |
---|
420 | | - |
---|
421 | | - if (rid_base & ~map_mask) { |
---|
422 | | - pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n", |
---|
423 | | - np, map_name, map_name, |
---|
424 | | - map_mask, rid_base); |
---|
425 | | - return -EFAULT; |
---|
426 | | - } |
---|
427 | | - |
---|
428 | | - if (masked_rid < rid_base || masked_rid >= rid_base + rid_len) |
---|
429 | | - continue; |
---|
430 | | - |
---|
431 | | - phandle_node = of_find_node_by_phandle(phandle); |
---|
432 | | - if (!phandle_node) |
---|
433 | | - return -ENODEV; |
---|
434 | | - |
---|
435 | | - if (target) { |
---|
436 | | - if (*target) |
---|
437 | | - of_node_put(phandle_node); |
---|
438 | | - else |
---|
439 | | - *target = phandle_node; |
---|
440 | | - |
---|
441 | | - if (*target != phandle_node) |
---|
442 | | - continue; |
---|
443 | | - } |
---|
444 | | - |
---|
445 | | - if (id_out) |
---|
446 | | - *id_out = masked_rid - rid_base + out_base; |
---|
447 | | - |
---|
448 | | - pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n", |
---|
449 | | - np, map_name, map_mask, rid_base, out_base, |
---|
450 | | - rid_len, rid, masked_rid - rid_base + out_base); |
---|
451 | | - return 0; |
---|
452 | | - } |
---|
453 | | - |
---|
454 | | - pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n", |
---|
455 | | - np, map_name, rid, target && *target ? *target : NULL); |
---|
456 | | - return -EFAULT; |
---|
457 | | -} |
---|
458 | 394 | |
---|
459 | 395 | #if IS_ENABLED(CONFIG_OF_IRQ) |
---|
460 | 396 | /** |
---|
461 | 397 | * of_irq_parse_pci - Resolve the interrupt for a PCI device |
---|
462 | 398 | * @pdev: the device whose interrupt is to be resolved |
---|
463 | | - * @out_irq: structure of_irq filled by this function |
---|
| 399 | + * @out_irq: structure of_phandle_args filled by this function |
---|
464 | 400 | * |
---|
465 | 401 | * This function resolves the PCI interrupt for a given PCI device. If a |
---|
466 | 402 | * device-node exists for a given pci_dev, it will use normal OF tree |
---|
.. | .. |
---|
587 | 523 | EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci); |
---|
588 | 524 | #endif /* CONFIG_OF_IRQ */ |
---|
589 | 525 | |
---|
590 | | -int pci_parse_request_of_pci_ranges(struct device *dev, |
---|
591 | | - struct list_head *resources, |
---|
592 | | - struct resource **bus_range) |
---|
| 526 | +static int pci_parse_request_of_pci_ranges(struct device *dev, |
---|
| 527 | + struct pci_host_bridge *bridge) |
---|
593 | 528 | { |
---|
594 | 529 | int err, res_valid = 0; |
---|
595 | 530 | resource_size_t iobase; |
---|
596 | 531 | struct resource_entry *win, *tmp; |
---|
597 | 532 | |
---|
598 | | - INIT_LIST_HEAD(resources); |
---|
599 | | - err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, resources, |
---|
600 | | - &iobase); |
---|
| 533 | + INIT_LIST_HEAD(&bridge->windows); |
---|
| 534 | + INIT_LIST_HEAD(&bridge->dma_ranges); |
---|
| 535 | + |
---|
| 536 | + err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &bridge->windows, |
---|
| 537 | + &bridge->dma_ranges, &iobase); |
---|
601 | 538 | if (err) |
---|
602 | 539 | return err; |
---|
603 | 540 | |
---|
604 | | - err = devm_request_pci_bus_resources(dev, resources); |
---|
| 541 | + err = devm_request_pci_bus_resources(dev, &bridge->windows); |
---|
605 | 542 | if (err) |
---|
606 | | - goto out_release_res; |
---|
| 543 | + return err; |
---|
607 | 544 | |
---|
608 | | - resource_list_for_each_entry_safe(win, tmp, resources) { |
---|
| 545 | + resource_list_for_each_entry_safe(win, tmp, &bridge->windows) { |
---|
609 | 546 | struct resource *res = win->res; |
---|
610 | 547 | |
---|
611 | 548 | switch (resource_type(res)) { |
---|
.. | .. |
---|
620 | 557 | case IORESOURCE_MEM: |
---|
621 | 558 | res_valid |= !(res->flags & IORESOURCE_PREFETCH); |
---|
622 | 559 | break; |
---|
623 | | - case IORESOURCE_BUS: |
---|
624 | | - if (bus_range) |
---|
625 | | - *bus_range = res; |
---|
626 | | - break; |
---|
627 | 560 | } |
---|
628 | 561 | } |
---|
629 | 562 | |
---|
630 | | - if (res_valid) |
---|
631 | | - return 0; |
---|
| 563 | + if (!res_valid) |
---|
| 564 | + dev_warn(dev, "non-prefetchable memory resource required\n"); |
---|
632 | 565 | |
---|
633 | | - dev_err(dev, "non-prefetchable memory resource required\n"); |
---|
634 | | - err = -EINVAL; |
---|
635 | | - |
---|
636 | | - out_release_res: |
---|
637 | | - pci_free_resource_list(resources); |
---|
638 | | - return err; |
---|
| 566 | + return 0; |
---|
639 | 567 | } |
---|
640 | 568 | |
---|
| 569 | +int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge) |
---|
| 570 | +{ |
---|
| 571 | + if (!dev->of_node) |
---|
| 572 | + return 0; |
---|
| 573 | + |
---|
| 574 | + bridge->swizzle_irq = pci_common_swizzle; |
---|
| 575 | + bridge->map_irq = of_irq_parse_and_map_pci; |
---|
| 576 | + |
---|
| 577 | + return pci_parse_request_of_pci_ranges(dev, bridge); |
---|
| 578 | +} |
---|
| 579 | + |
---|
| 580 | +#endif /* CONFIG_PCI */ |
---|
| 581 | + |
---|
| 582 | +/** |
---|
| 583 | + * This function will try to find the limitation of link speed by finding |
---|
| 584 | + * a property called "max-link-speed" of the given device node. |
---|
| 585 | + * |
---|
| 586 | + * @node: device tree node with the max link speed information |
---|
| 587 | + * |
---|
| 588 | + * Returns the associated max link speed from DT, or a negative value if the |
---|
| 589 | + * required property is not found or is invalid. |
---|
| 590 | + */ |
---|
| 591 | +int of_pci_get_max_link_speed(struct device_node *node) |
---|
| 592 | +{ |
---|
| 593 | + u32 max_link_speed; |
---|
| 594 | + |
---|
| 595 | + if (of_property_read_u32(node, "max-link-speed", &max_link_speed) || |
---|
| 596 | + max_link_speed == 0 || max_link_speed > 4) |
---|
| 597 | + return -EINVAL; |
---|
| 598 | + |
---|
| 599 | + return max_link_speed; |
---|
| 600 | +} |
---|
| 601 | +EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed); |
---|