1 // SPDX-License-Identifier: GPL-2.0
3 * Host bridge related code
6 #include <linux/kernel.h>
8 #include <linux/module.h>
12 static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
20 struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus)
22 struct pci_bus *root_bus = find_pci_root_bus(bus);
24 return to_pci_host_bridge(root_bus->bridge);
26 EXPORT_SYMBOL_GPL(pci_find_host_bridge);
28 struct device *pci_get_host_bridge_device(struct pci_dev *dev)
30 struct pci_bus *root_bus = find_pci_root_bus(dev->bus);
31 struct device *bridge = root_bus->bridge;
33 kobject_get(&bridge->kobj);
37 void pci_put_host_bridge_device(struct device *dev)
39 kobject_put(&dev->kobj);
42 void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
43 void (*release_fn)(struct pci_host_bridge *),
46 bridge->release_fn = release_fn;
47 bridge->release_data = release_data;
49 EXPORT_SYMBOL_GPL(pci_set_host_bridge_release);
51 void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
54 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
55 struct resource_entry *window;
56 resource_size_t offset = 0;
58 resource_list_for_each_entry(window, &bridge->windows) {
59 if (resource_contains(window->res, res)) {
60 offset = window->offset;
65 region->start = res->start - offset;
66 region->end = res->end - offset;
68 EXPORT_SYMBOL(pcibios_resource_to_bus);
70 static bool region_contains(struct pci_bus_region *region1,
71 struct pci_bus_region *region2)
73 return region1->start <= region2->start && region1->end >= region2->end;
76 void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
77 struct pci_bus_region *region)
79 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
80 struct resource_entry *window;
81 resource_size_t offset = 0;
83 resource_list_for_each_entry(window, &bridge->windows) {
84 struct pci_bus_region bus_region;
86 if (resource_type(res) != resource_type(window->res))
89 bus_region.start = window->res->start - window->offset;
90 bus_region.end = window->res->end - window->offset;
92 if (region_contains(&bus_region, region)) {
93 offset = window->offset;
98 res->start = region->start + offset;
99 res->end = region->end + offset;
101 EXPORT_SYMBOL(pcibios_bus_to_resource);