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