blob: 0e5f3c95af5bd75d288d2067b79aacc6abd65a94 [file] [log] [blame]
Yinghai Lu610929e2012-04-02 18:31:53 -07001/*
2 * host bridge related code
3 */
4
5#include <linux/kernel.h>
Yinghai Lu610929e2012-04-02 18:31:53 -07006#include <linux/pci.h>
7#include <linux/module.h>
8
9#include "pci.h"
10
Yinghai Lufc279852013-12-09 22:54:40 -080011static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
Yinghai Lu610929e2012-04-02 18:31:53 -070012{
Yinghai Lu610929e2012-04-02 18:31:53 -070013 while (bus->parent)
14 bus = bus->parent;
15
Yinghai Lu459f58c2012-04-02 18:31:53 -070016 return bus;
17}
18
Yinghai Lufc279852013-12-09 22:54:40 -080019static struct pci_host_bridge *find_pci_host_bridge(struct pci_bus *bus)
Yinghai Lu459f58c2012-04-02 18:31:53 -070020{
Yinghai Lufc279852013-12-09 22:54:40 -080021 struct pci_bus *root_bus = find_pci_root_bus(bus);
Yinghai Lu459f58c2012-04-02 18:31:53 -070022
Yinghai Lufc279852013-12-09 22:54:40 -080023 return to_pci_host_bridge(root_bus->bridge);
Yinghai Lu610929e2012-04-02 18:31:53 -070024}
25
Yinghai Lu4fa26492012-04-02 18:31:53 -070026void 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
Yinghai Lufc279852013-12-09 22:54:40 -080034void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
Yinghai Lu610929e2012-04-02 18:31:53 -070035 struct resource *res)
36{
Yinghai Lufc279852013-12-09 22:54:40 -080037 struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
Yinghai Lu610929e2012-04-02 18:31:53 -070038 struct pci_host_bridge_window *window;
39 resource_size_t offset = 0;
40
41 list_for_each_entry(window, &bridge->windows, list) {
Yinghai Lu610929e2012-04-02 18:31:53 -070042 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}
51EXPORT_SYMBOL(pcibios_resource_to_bus);
52
53static 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
Yinghai Lufc279852013-12-09 22:54:40 -080059void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
Yinghai Lu610929e2012-04-02 18:31:53 -070060 struct pci_bus_region *region)
61{
Yinghai Lufc279852013-12-09 22:54:40 -080062 struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
Yinghai Lu610929e2012-04-02 18:31:53 -070063 struct pci_host_bridge_window *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070064 resource_size_t offset = 0;
65
66 list_for_each_entry(window, &bridge->windows, list) {
Yinghai Lu459f58c2012-04-02 18:31:53 -070067 struct pci_bus_region bus_region;
68
Yinghai Lu610929e2012-04-02 18:31:53 -070069 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}
84EXPORT_SYMBOL(pcibios_bus_to_resource);