xref: /openbmc/linux/drivers/pci/host-bridge.c (revision 62e7ca52)
1 /*
2  * host bridge related code
3  */
4 
5 #include <linux/kernel.h>
6 #include <linux/pci.h>
7 #include <linux/module.h>
8 
9 #include "pci.h"
10 
11 static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
12 {
13 	while (bus->parent)
14 		bus = bus->parent;
15 
16 	return bus;
17 }
18 
19 static struct pci_host_bridge *find_pci_host_bridge(struct pci_bus *bus)
20 {
21 	struct pci_bus *root_bus = find_pci_root_bus(bus);
22 
23 	return to_pci_host_bridge(root_bus->bridge);
24 }
25 
26 void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
27 				 void (*release_fn)(struct pci_host_bridge *),
28 				 void *release_data)
29 {
30 	bridge->release_fn = release_fn;
31 	bridge->release_data = release_data;
32 }
33 
34 void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
35 			     struct resource *res)
36 {
37 	struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
38 	struct pci_host_bridge_window *window;
39 	resource_size_t offset = 0;
40 
41 	list_for_each_entry(window, &bridge->windows, list) {
42 		if (resource_contains(window->res, res)) {
43 			offset = window->offset;
44 			break;
45 		}
46 	}
47 
48 	region->start = res->start - offset;
49 	region->end = res->end - offset;
50 }
51 EXPORT_SYMBOL(pcibios_resource_to_bus);
52 
53 static bool region_contains(struct pci_bus_region *region1,
54 			    struct pci_bus_region *region2)
55 {
56 	return region1->start <= region2->start && region1->end >= region2->end;
57 }
58 
59 void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
60 			     struct pci_bus_region *region)
61 {
62 	struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
63 	struct pci_host_bridge_window *window;
64 	resource_size_t offset = 0;
65 
66 	list_for_each_entry(window, &bridge->windows, list) {
67 		struct pci_bus_region bus_region;
68 
69 		if (resource_type(res) != resource_type(window->res))
70 			continue;
71 
72 		bus_region.start = window->res->start - window->offset;
73 		bus_region.end = window->res->end - window->offset;
74 
75 		if (region_contains(&bus_region, region)) {
76 			offset = window->offset;
77 			break;
78 		}
79 	}
80 
81 	res->start = region->start + offset;
82 	res->end = region->end + offset;
83 }
84 EXPORT_SYMBOL(pcibios_bus_to_resource);
85