blob: c49a1c482cfb100dfe91f1d1bba173c842803646 [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
12static LIST_HEAD(pci_host_bridges);
13
14void add_to_pci_host_bridges(struct pci_host_bridge *bridge)
15{
16 list_add_tail(&bridge->list, &pci_host_bridges);
17}
18
Yinghai Lu459f58c2012-04-02 18:31:53 -070019static struct pci_bus *find_pci_root_bus(struct pci_dev *dev)
Yinghai Lu610929e2012-04-02 18:31:53 -070020{
21 struct pci_bus *bus;
Yinghai Lu610929e2012-04-02 18:31:53 -070022
23 bus = dev->bus;
24 while (bus->parent)
25 bus = bus->parent;
26
Yinghai Lu459f58c2012-04-02 18:31:53 -070027 return bus;
28}
29
30static struct pci_host_bridge *find_pci_host_bridge(struct pci_dev *dev)
31{
32 struct pci_bus *bus = find_pci_root_bus(dev);
33 struct pci_host_bridge *bridge;
34
Yinghai Lu610929e2012-04-02 18:31:53 -070035 list_for_each_entry(bridge, &pci_host_bridges, list) {
36 if (bridge->bus == bus)
37 return bridge;
38 }
39
40 return NULL;
41}
42
43static bool resource_contains(struct resource *res1, struct resource *res2)
44{
45 return res1->start <= res2->start && res1->end >= res2->end;
46}
47
48void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
49 struct resource *res)
50{
Yinghai Lu459f58c2012-04-02 18:31:53 -070051 struct pci_host_bridge *bridge = find_pci_host_bridge(dev);
Yinghai Lu610929e2012-04-02 18:31:53 -070052 struct pci_host_bridge_window *window;
53 resource_size_t offset = 0;
54
55 list_for_each_entry(window, &bridge->windows, list) {
56 if (resource_type(res) != resource_type(window->res))
57 continue;
58
59 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
76void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
77 struct pci_bus_region *region)
78{
Yinghai Lu459f58c2012-04-02 18:31:53 -070079 struct pci_host_bridge *bridge = find_pci_host_bridge(dev);
Yinghai Lu610929e2012-04-02 18:31:53 -070080 struct pci_host_bridge_window *window;
Yinghai Lu610929e2012-04-02 18:31:53 -070081 resource_size_t offset = 0;
82
83 list_for_each_entry(window, &bridge->windows, list) {
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);