blob: 5f4a2e04c8d7cf237535d0cafb0274ba4df904e5 [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
Aaron Lu3390e082015-03-25 14:32:25 +080019struct pci_host_bridge *pci_find_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
Murali Karicheri6675a602015-03-03 12:52:11 -050026struct 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
35void pci_put_host_bridge_device(struct device *dev)
36{
37 kobject_put(&dev->kobj);
38}
39
Yinghai Lu4fa26492012-04-02 18:31:53 -070040void 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}
47
Yinghai Lufc279852013-12-09 22:54:40 -080048void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
Yinghai Lu610929e2012-04-02 18:31:53 -070049 struct resource *res)
50{
Aaron Lu3390e082015-03-25 14:32:25 +080051 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080052 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070053 resource_size_t offset = 0;
54
Jiang Liu14d76b62015-02-05 13:44:44 +080055 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu610929e2012-04-02 18:31:53 -070056 if (resource_contains(window->res, res)) {
57 offset = window->offset;
58 break;
59 }
60 }
61
62 region->start = res->start - offset;
63 region->end = res->end - offset;
64}
65EXPORT_SYMBOL(pcibios_resource_to_bus);
66
67static bool region_contains(struct pci_bus_region *region1,
68 struct pci_bus_region *region2)
69{
70 return region1->start <= region2->start && region1->end >= region2->end;
71}
72
Yinghai Lufc279852013-12-09 22:54:40 -080073void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
Yinghai Lu610929e2012-04-02 18:31:53 -070074 struct pci_bus_region *region)
75{
Aaron Lu3390e082015-03-25 14:32:25 +080076 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080077 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070078 resource_size_t offset = 0;
79
Jiang Liu14d76b62015-02-05 13:44:44 +080080 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu459f58c2012-04-02 18:31:53 -070081 struct pci_bus_region bus_region;
82
Yinghai Lu610929e2012-04-02 18:31:53 -070083 if (resource_type(res) != resource_type(window->res))
84 continue;
85
86 bus_region.start = window->res->start - window->offset;
87 bus_region.end = window->res->end - window->offset;
88
89 if (region_contains(&bus_region, region)) {
90 offset = window->offset;
91 break;
92 }
93 }
94
95 res->start = region->start + offset;
96 res->end = region->end + offset;
97}
98EXPORT_SYMBOL(pcibios_bus_to_resource);