blob: afa50b446567f77a1aacbf09ed5a0de9e8e81881 [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}
Rafael J. Wysocki59dc3322021-08-24 16:43:55 +020026EXPORT_SYMBOL_GPL(pci_find_host_bridge);
Yinghai Lu610929e2012-04-02 18:31:53 -070027
Murali Karicheri6675a602015-03-03 12:52:11 -050028struct device *pci_get_host_bridge_device(struct pci_dev *dev)
29{
30 struct pci_bus *root_bus = find_pci_root_bus(dev->bus);
31 struct device *bridge = root_bus->bridge;
32
33 kobject_get(&bridge->kobj);
34 return bridge;
35}
36
37void pci_put_host_bridge_device(struct device *dev)
38{
39 kobject_put(&dev->kobj);
40}
41
Yinghai Lu4fa26492012-04-02 18:31:53 -070042void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
43 void (*release_fn)(struct pci_host_bridge *),
44 void *release_data)
45{
46 bridge->release_fn = release_fn;
47 bridge->release_data = release_data;
48}
Andrew Donnellan6f38a8b2016-08-18 17:35:14 +100049EXPORT_SYMBOL_GPL(pci_set_host_bridge_release);
Yinghai Lu4fa26492012-04-02 18:31:53 -070050
Yinghai Lufc279852013-12-09 22:54:40 -080051void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
Yinghai Lu610929e2012-04-02 18:31:53 -070052 struct resource *res)
53{
Aaron Lu3390e082015-03-25 14:32:25 +080054 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080055 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070056 resource_size_t offset = 0;
57
Jiang Liu14d76b62015-02-05 13:44:44 +080058 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu610929e2012-04-02 18:31:53 -070059 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}
68EXPORT_SYMBOL(pcibios_resource_to_bus);
69
70static 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
Yinghai Lufc279852013-12-09 22:54:40 -080076void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
Yinghai Lu610929e2012-04-02 18:31:53 -070077 struct pci_bus_region *region)
78{
Aaron Lu3390e082015-03-25 14:32:25 +080079 struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
Jiang Liu14d76b62015-02-05 13:44:44 +080080 struct resource_entry *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070081 resource_size_t offset = 0;
82
Jiang Liu14d76b62015-02-05 13:44:44 +080083 resource_list_for_each_entry(window, &bridge->windows) {
Yinghai Lu459f58c2012-04-02 18:31:53 -070084 struct pci_bus_region bus_region;
85
Yinghai Lu610929e2012-04-02 18:31:53 -070086 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}
101EXPORT_SYMBOL(pcibios_bus_to_resource);