hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/pci/of.c
....@@ -15,32 +15,47 @@
1515 #include <linux/of_pci.h>
1616 #include "pci.h"
1717
18
+#ifdef CONFIG_PCI
1819 void pci_set_of_node(struct pci_dev *dev)
1920 {
2021 if (!dev->bus->dev.of_node)
2122 return;
2223 dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
2324 dev->devfn);
25
+ if (dev->dev.of_node)
26
+ dev->dev.fwnode = &dev->dev.of_node->fwnode;
2427 }
2528
2629 void pci_release_of_node(struct pci_dev *dev)
2730 {
2831 of_node_put(dev->dev.of_node);
2932 dev->dev.of_node = NULL;
33
+ dev->dev.fwnode = NULL;
3034 }
3135
3236 void pci_set_bus_of_node(struct pci_bus *bus)
3337 {
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;
3852 }
3953
4054 void pci_release_bus_of_node(struct pci_bus *bus)
4155 {
4256 of_node_put(bus->dev.of_node);
4357 bus->dev.of_node = NULL;
58
+ bus->dev.fwnode = NULL;
4459 }
4560
4661 struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
....@@ -113,7 +128,7 @@
113128 * a fake root for all functions of a multi-function
114129 * device we go down them as well.
115130 */
116
- if (!strcmp(node->name, "multifunc-device")) {
131
+ if (of_node_name_eq(node, "multifunc-device")) {
117132 for_each_child_of_node(node, node2) {
118133 if (__of_pci_pci_compare(node2, devfn)) {
119134 of_node_put(node);
....@@ -197,27 +212,6 @@
197212 EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
198213
199214 /**
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
-/**
221215 * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
222216 * is present and valid
223217 */
....@@ -242,7 +236,6 @@
242236 }
243237 EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
244238
245
-#if defined(CONFIG_OF_ADDRESS)
246239 /**
247240 * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI
248241 * host bridge resources from DT
....@@ -250,6 +243,8 @@
250243 * @busno: bus number associated with the bridge root bus
251244 * @bus_max: maximum number of buses for this bridge
252245 * @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
253248 * @io_base: pointer to a variable that will contain on return the physical
254249 * address for the start of the I/O range. Can be NULL if the caller doesn't
255250 * expect I/O ranges to be present in the device tree.
....@@ -261,16 +256,18 @@
261256 * It returns zero if the range parsing has been successful or a standard error
262257 * value if it failed.
263258 */
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,
265260 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)
267264 {
268265 struct device_node *dev_node = dev->of_node;
269266 struct resource *res, tmp_res;
270267 struct resource *bus_range;
271268 struct of_pci_range range;
272269 struct of_pci_range_parser parser;
273
- char range_type[4];
270
+ const char *range_type;
274271 int err;
275272
276273 if (io_base)
....@@ -298,18 +295,18 @@
298295 /* Check for ranges property */
299296 err = of_pci_range_parser_init(&parser, dev_node);
300297 if (err)
301
- goto failed;
298
+ return 0;
302299
303300 dev_dbg(dev, "Parsing ranges property...\n");
304301 for_each_of_pci_range(&parser, &range) {
305302 /* Read next ranges element */
306303 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
307
- snprintf(range_type, 4, " IO");
304
+ range_type = "IO";
308305 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
309
- snprintf(range_type, 4, "MEM");
306
+ range_type = "MEM";
310307 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",
313310 range_type, range.cpu_addr,
314311 range.cpu_addr + range.size - 1, range.pci_addr);
315312
....@@ -346,121 +343,60 @@
346343 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
347344 }
348345
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
+
349388 return 0;
350389
351390 failed:
352391 pci_free_resource_list(resources);
353392 return err;
354393 }
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
-}
458394
459395 #if IS_ENABLED(CONFIG_OF_IRQ)
460396 /**
461397 * of_irq_parse_pci - Resolve the interrupt for a PCI device
462398 * @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
464400 *
465401 * This function resolves the PCI interrupt for a given PCI device. If a
466402 * device-node exists for a given pci_dev, it will use normal OF tree
....@@ -587,25 +523,26 @@
587523 EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
588524 #endif /* CONFIG_OF_IRQ */
589525
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)
593528 {
594529 int err, res_valid = 0;
595530 resource_size_t iobase;
596531 struct resource_entry *win, *tmp;
597532
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);
601538 if (err)
602539 return err;
603540
604
- err = devm_request_pci_bus_resources(dev, resources);
541
+ err = devm_request_pci_bus_resources(dev, &bridge->windows);
605542 if (err)
606
- goto out_release_res;
543
+ return err;
607544
608
- resource_list_for_each_entry_safe(win, tmp, resources) {
545
+ resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
609546 struct resource *res = win->res;
610547
611548 switch (resource_type(res)) {
....@@ -620,21 +557,45 @@
620557 case IORESOURCE_MEM:
621558 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
622559 break;
623
- case IORESOURCE_BUS:
624
- if (bus_range)
625
- *bus_range = res;
626
- break;
627560 }
628561 }
629562
630
- if (res_valid)
631
- return 0;
563
+ if (!res_valid)
564
+ dev_warn(dev, "non-prefetchable memory resource required\n");
632565
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;
639567 }
640568
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);