blob: e01d53f5b32f6f99cda5d3753cbb3d3fba53fe03 [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Yinghai Lu610929e2012-04-02 18:31:53 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * Host bridge related code
Yinghai Lu610929e2012-04-02 18:31:53 -07004 */
5
6#include <linux/kernel.h>
Yinghai Lu610929e2012-04-02 18:31:53 -07007#include <linux/pci.h>
8#include <linux/module.h>
9
10#include "pci.h"
11
Yinghai Lufc279852013-12-09 22:54:40 -080012static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
Yinghai Lu610929e2012-04-02 18:31:53 -070013{
Yinghai Lu610929e2012-04-02 18:31:53 -070014 while (bus->parent)
15 bus = bus->parent;
16
Yinghai Lu459f58c2012-04-02 18:31:53 -070017 return bus;
18}
19
Aaron Lu3390e082015-03-25 14:32:25 +080020struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus)
Yinghai Lu459f58c2012-04-02 18:31:53 -070021{
Yinghai Lufc279852013-12-09 22:54:40 -080022 struct pci_bus *root_bus = find_pci_root_bus(bus);
Yinghai Lu459f58c2012-04-02 18:31:53 -070023
Yinghai Lufc279852013-12-09 22:54:40 -080024 return to_pci_host_bridge(root_bus->bridge);
Yinghai Lu610929e2012-04-02 18:31:53 -070025}
26
Murali Karicheri6675a602015-03-03 12:52:11 -050027struct device *pci_get_host_bridge_device(struct pci_dev *dev)
28{
29 struct pci_bus *root_bus = find_pci_root_bus(dev->bus);
30 struct device *bridge = root_bus->bridge;
31
32 kobject_get(&bridge->kobj);
33 return bridge;
34}
35
36void pci_put_host_bridge_device(struct device *dev)
37{
38 kobject_put(&dev->kobj);
39}
40
Yinghai Lu4fa26492012-04-02 18:31:53 -070041void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
42 void (*release_fn)(struct pci_host_bridge *),
43 void *release_data)
44{
45 bridge->release_fn = release_fn;
46 bridge->release_data = release_data;
47}
Andrew Donnellan6f38a8b2016-08-18 17:35:14 +100048EXPORT_SYMBOL_GPL(pci_set_host_bridge_release);
Yinghai Lu4fa26492012-04-02 18:31:53 -070049
Yinghai Lufc279852013-12-09 22:54:40 -080050void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
Yinghai Lu610929e2012-04-02 18:31:53 -070051 struct resource *res)
52{
Aaron Lu3390e082015-03-25 14:32:25 +080053 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080054 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070055 resource_size_t offset = 0;
56
Jiang Liu14d76b62015-02-05 13:44:44 +080057 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu610929e2012-04-02 18:31:53 -070058 if (resource_contains(window->res, res)) {
59 offset = window->offset;
60 break;
61 }
62 }
63
64 region->start = res->start - offset;
65 region->end = res->end - offset;
66}
67EXPORT_SYMBOL(pcibios_resource_to_bus);
68
69static bool region_contains(struct pci_bus_region *region1,
70 struct pci_bus_region *region2)
71{
72 return region1->start <= region2->start && region1->end >= region2->end;
73}
74
Yinghai Lufc279852013-12-09 22:54:40 -080075void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
Yinghai Lu610929e2012-04-02 18:31:53 -070076 struct pci_bus_region *region)
77{
Aaron Lu3390e082015-03-25 14:32:25 +080078 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080079 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070080 resource_size_t offset = 0;
81
Jiang Liu14d76b62015-02-05 13:44:44 +080082 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu459f58c2012-04-02 18:31:53 -070083 struct pci_bus_region bus_region;
84
Yinghai Lu610929e2012-04-02 18:31:53 -070085 if (resource_type(res) != resource_type(window->res))
86 continue;
87
88 bus_region.start = window->res->start - window->offset;
89 bus_region.end = window->res->end - window->offset;
90
91 if (region_contains(&bus_region, region)) {
92 offset = window->offset;
93 break;
94 }
95 }
96
97 res->start = region->start + offset;
98 res->end = region->end + offset;
99}
100EXPORT_SYMBOL(pcibios_bus_to_resource);