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 | |
| 12 | static LIST_HEAD(pci_host_bridges); |
| 13 | |
| 14 | void add_to_pci_host_bridges(struct pci_host_bridge *bridge) |
| 15 | { |
| 16 | list_add_tail(&bridge->list, &pci_host_bridges); |
| 17 | } |
| 18 | |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame^] | 19 | static struct pci_bus *find_pci_root_bus(struct pci_dev *dev) |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 20 | { |
| 21 | struct pci_bus *bus; |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 22 | |
| 23 | bus = dev->bus; |
| 24 | while (bus->parent) |
| 25 | bus = bus->parent; |
| 26 | |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame^] | 27 | return bus; |
| 28 | } |
| 29 | |
| 30 | static struct pci_host_bridge *find_pci_host_bridge(struct pci_dev *dev) |
| 31 | { |
| 32 | struct pci_bus *bus = find_pci_root_bus(dev); |
| 33 | struct pci_host_bridge *bridge; |
| 34 | |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 35 | list_for_each_entry(bridge, &pci_host_bridges, list) { |
| 36 | if (bridge->bus == bus) |
| 37 | return bridge; |
| 38 | } |
| 39 | |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | static bool resource_contains(struct resource *res1, struct resource *res2) |
| 44 | { |
| 45 | return res1->start <= res2->start && res1->end >= res2->end; |
| 46 | } |
| 47 | |
| 48 | void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region, |
| 49 | struct resource *res) |
| 50 | { |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame^] | 51 | struct pci_host_bridge *bridge = find_pci_host_bridge(dev); |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 52 | struct pci_host_bridge_window *window; |
| 53 | resource_size_t offset = 0; |
| 54 | |
| 55 | list_for_each_entry(window, &bridge->windows, list) { |
| 56 | if (resource_type(res) != resource_type(window->res)) |
| 57 | continue; |
| 58 | |
| 59 | if (resource_contains(window->res, res)) { |
| 60 | offset = window->offset; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | region->start = res->start - offset; |
| 66 | region->end = res->end - offset; |
| 67 | } |
| 68 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 69 | |
| 70 | static bool region_contains(struct pci_bus_region *region1, |
| 71 | struct pci_bus_region *region2) |
| 72 | { |
| 73 | return region1->start <= region2->start && region1->end >= region2->end; |
| 74 | } |
| 75 | |
| 76 | void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res, |
| 77 | struct pci_bus_region *region) |
| 78 | { |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame^] | 79 | struct pci_host_bridge *bridge = find_pci_host_bridge(dev); |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 80 | struct pci_host_bridge_window *window; |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 81 | resource_size_t offset = 0; |
| 82 | |
| 83 | list_for_each_entry(window, &bridge->windows, list) { |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame^] | 84 | struct pci_bus_region bus_region; |
| 85 | |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 86 | if (resource_type(res) != resource_type(window->res)) |
| 87 | continue; |
| 88 | |
| 89 | bus_region.start = window->res->start - window->offset; |
| 90 | bus_region.end = window->res->end - window->offset; |
| 91 | |
| 92 | if (region_contains(&bus_region, region)) { |
| 93 | offset = window->offset; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | res->start = region->start + offset; |
| 99 | res->end = region->end + offset; |
| 100 | } |
| 101 | EXPORT_SYMBOL(pcibios_bus_to_resource); |