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 | |
Aaron Lu | 3390e08 | 2015-03-25 14:32:25 +0800 | [diff] [blame] | 19 | struct pci_host_bridge *pci_find_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 | |
Murali Karicheri | 6675a60 | 2015-03-03 12:52:11 -0500 | [diff] [blame] | 26 | struct device *pci_get_host_bridge_device(struct pci_dev *dev) |
| 27 | { |
| 28 | struct pci_bus *root_bus = find_pci_root_bus(dev->bus); |
| 29 | struct device *bridge = root_bus->bridge; |
| 30 | |
| 31 | kobject_get(&bridge->kobj); |
| 32 | return bridge; |
| 33 | } |
| 34 | |
| 35 | void pci_put_host_bridge_device(struct device *dev) |
| 36 | { |
| 37 | kobject_put(&dev->kobj); |
| 38 | } |
| 39 | |
Yinghai Lu | 4fa2649 | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 40 | void pci_set_host_bridge_release(struct pci_host_bridge *bridge, |
| 41 | void (*release_fn)(struct pci_host_bridge *), |
| 42 | void *release_data) |
| 43 | { |
| 44 | bridge->release_fn = release_fn; |
| 45 | bridge->release_data = release_data; |
| 46 | } |
Andrew Donnellan | 6f38a8b | 2016-08-18 17:35:14 +1000 | [diff] [blame] | 47 | EXPORT_SYMBOL_GPL(pci_set_host_bridge_release); |
Yinghai Lu | 4fa2649 | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 48 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 49 | 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] | 50 | struct resource *res) |
| 51 | { |
Aaron Lu | 3390e08 | 2015-03-25 14:32:25 +0800 | [diff] [blame] | 52 | struct pci_host_bridge *bridge = pci_find_host_bridge(bus); |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 53 | struct resource_entry *window; |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 54 | resource_size_t offset = 0; |
| 55 | |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 56 | resource_list_for_each_entry(window, &bridge->windows) { |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 57 | if (resource_contains(window->res, res)) { |
| 58 | offset = window->offset; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | region->start = res->start - offset; |
| 64 | region->end = res->end - offset; |
| 65 | } |
| 66 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
| 67 | |
| 68 | static bool region_contains(struct pci_bus_region *region1, |
| 69 | struct pci_bus_region *region2) |
| 70 | { |
| 71 | return region1->start <= region2->start && region1->end >= region2->end; |
| 72 | } |
| 73 | |
Yinghai Lu | fc27985 | 2013-12-09 22:54:40 -0800 | [diff] [blame] | 74 | void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res, |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 75 | struct pci_bus_region *region) |
| 76 | { |
Aaron Lu | 3390e08 | 2015-03-25 14:32:25 +0800 | [diff] [blame] | 77 | struct pci_host_bridge *bridge = pci_find_host_bridge(bus); |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 78 | struct resource_entry *window; |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 79 | resource_size_t offset = 0; |
| 80 | |
Jiang Liu | 14d76b6 | 2015-02-05 13:44:44 +0800 | [diff] [blame] | 81 | resource_list_for_each_entry(window, &bridge->windows) { |
Yinghai Lu | 459f58c | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 82 | struct pci_bus_region bus_region; |
| 83 | |
Yinghai Lu | 610929e | 2012-04-02 18:31:53 -0700 | [diff] [blame] | 84 | if (resource_type(res) != resource_type(window->res)) |
| 85 | continue; |
| 86 | |
| 87 | bus_region.start = window->res->start - window->offset; |
| 88 | bus_region.end = window->res->end - window->offset; |
| 89 | |
| 90 | if (region_contains(&bus_region, region)) { |
| 91 | offset = window->offset; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | res->start = region->start + offset; |
| 97 | res->end = region->end + offset; |
| 98 | } |
| 99 | EXPORT_SYMBOL(pcibios_bus_to_resource); |