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> |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 6 | #include <linux/pci.h> |
| 7 | #include <linux/module.h> |
| 8 | |
| 9 | #include "pci.h" |
| 10 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 11 | static struct pci_bus *find_pci_root_bus(struct pci_bus *bus) |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 12 | { |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 13 | while (bus->parent) |
| 14 | bus = bus->parent; |
| 15 | |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 16 | return bus; |
| 17 | } |
| 18 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 19 | 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] | 20 | { |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 21 | struct pci_bus *root_bus = find_pci_root_bus(bus); |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 22 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 23 | return to_pci_host_bridge(root_bus->bridge); |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Yinghai Lu | 4fa2649 | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 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 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 34 | 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] | 35 | struct resource *res) |
| 36 | { |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 37 | struct pci_host_bridge *bridge = find_pci_host_bridge(bus); |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 38 | struct pci_host_bridge_window *window; |
| 39 | resource_size_t offset = 0; |
| 40 | |
| 41 | list_for_each_entry(window, &bridge->windows, list) { |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 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 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 59 | void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res, |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 60 | struct pci_bus_region *region) |
| 61 | { |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 62 | struct pci_host_bridge *bridge = find_pci_host_bridge(bus); |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 63 | struct pci_host_bridge_window *window; |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 64 | resource_size_t offset = 0; |
| 65 | |
| 66 | list_for_each_entry(window, &bridge->windows, list) { |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 67 | struct pci_bus_region bus_region; |
| 68 | |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 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); |