xref: /linux/drivers/pci/host-bridge.c (revision ca64d84e93762f4e587e040a44ad9f6089afc777)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Host bridge related code
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 
10 #include "pci.h"
11 
12 static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
13 {
14 	while (bus->parent)
15 		bus = bus->parent;
16 
17 	return bus;
18 }
19 
20 struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus)
21 {
22 	struct pci_bus *root_bus = find_pci_root_bus(bus);
23 
24 	return to_pci_host_bridge(root_bus->bridge);
25 }
26 
27 struct device *pci_get_host_bridge_device(struct pci_dev *dev)
28 {
29 	struct pci_bus *root_bus = find_pci_root_bus(dev->bus);
30 	struct device *bridge = root_bus->bridge;
31 
32 	kobject_get(&bridge->kobj);
33 	return bridge;
34 }
35 
36 void  pci_put_host_bridge_device(struct device *dev)
37 {
38 	kobject_put(&dev->kobj);
39 }
40 
41 void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
42 				 void (*release_fn)(struct pci_host_bridge *),
43 				 void *release_data)
44 {
45 	bridge->release_fn = release_fn;
46 	bridge->release_data = release_data;
47 }
48 EXPORT_SYMBOL_GPL(pci_set_host_bridge_release);
49 
50 void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
51 			     struct resource *res)
52 {
53 	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
54 	struct resource_entry *window;
55 	resource_size_t offset = 0;
56 
57 	resource_list_for_each_entry(window, &bridge->windows) {
58 		if (resource_contains(window->res, res)) {
59 			offset = window->offset;
60 			break;
61 		}
62 	}
63 
64 	region->start = res->start - offset;
65 	region->end = res->end - offset;
66 }
67 EXPORT_SYMBOL(pcibios_resource_to_bus);
68 
69 static bool region_contains(struct pci_bus_region *region1,
70 			    struct pci_bus_region *region2)
71 {
72 	return region1->start <= region2->start && region1->end >= region2->end;
73 }
74 
75 void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
76 			     struct pci_bus_region *region)
77 {
78 	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
79 	struct resource_entry *window;
80 	resource_size_t offset = 0;
81 
82 	resource_list_for_each_entry(window, &bridge->windows) {
83 		struct pci_bus_region bus_region;
84 
85 		if (resource_type(res) != resource_type(window->res))
86 			continue;
87 
88 		bus_region.start = window->res->start - window->offset;
89 		bus_region.end = window->res->end - window->offset;
90 
91 		if (region_contains(&bus_region, region)) {
92 			offset = window->offset;
93 			break;
94 		}
95 	}
96 
97 	res->start = region->start + offset;
98 	res->end = region->end + offset;
99 }
100 EXPORT_SYMBOL(pcibios_bus_to_resource);
101