blob: 47aaf22d814e195f7d397237a59267334ccf5ca2 [file] [log] [blame]
Yinghai Lu610929e2012-04-02 18:31:53 -07001/*
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
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
Yinghai Lufc279852013-12-09 22:54:40 -080020static struct pci_host_bridge *find_pci_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
Yinghai Lu4fa26492012-04-02 18:31:53 -070027void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
28 void (*release_fn)(struct pci_host_bridge *),
29 void *release_data)
30{
31 bridge->release_fn = release_fn;
32 bridge->release_data = release_data;
33}
34
Yinghai Lufc279852013-12-09 22:54:40 -080035void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
Yinghai Lu610929e2012-04-02 18:31:53 -070036 struct resource *res)
37{
Yinghai Lufc279852013-12-09 22:54:40 -080038 struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
Yinghai Lu610929e2012-04-02 18:31:53 -070039 struct pci_host_bridge_window *window;
40 resource_size_t offset = 0;
41
42 list_for_each_entry(window, &bridge->windows, list) {
Yinghai Lu610929e2012-04-02 18:31:53 -070043 if (resource_contains(window->res, res)) {
44 offset = window->offset;
45 break;
46 }
47 }
48
49 region->start = res->start - offset;
50 region->end = res->end - offset;
51}
52EXPORT_SYMBOL(pcibios_resource_to_bus);
53
54static bool region_contains(struct pci_bus_region *region1,
55 struct pci_bus_region *region2)
56{
57 return region1->start <= region2->start && region1->end >= region2->end;
58}
59
Yinghai Lufc279852013-12-09 22:54:40 -080060void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
Yinghai Lu610929e2012-04-02 18:31:53 -070061 struct pci_bus_region *region)
62{
Yinghai Lufc279852013-12-09 22:54:40 -080063 struct pci_host_bridge *bridge = find_pci_host_bridge(bus);
Yinghai Lu610929e2012-04-02 18:31:53 -070064 struct pci_host_bridge_window *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070065 resource_size_t offset = 0;
66
67 list_for_each_entry(window, &bridge->windows, list) {
Yinghai Lu459f58c2012-04-02 18:31:53 -070068 struct pci_bus_region bus_region;
69
Yinghai Lu610929e2012-04-02 18:31:53 -070070 if (resource_type(res) != resource_type(window->res))
71 continue;
72
73 bus_region.start = window->res->start - window->offset;
74 bus_region.end = window->res->end - window->offset;
75
76 if (region_contains(&bus_region, region)) {
77 offset = window->offset;
78 break;
79 }
80 }
81
82 res->start = region->start + offset;
83 res->end = region->end + offset;
84}
85EXPORT_SYMBOL(pcibios_bus_to_resource);