host-bridge.c 2.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * host bridge related code
 */

#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/module.h>

#include "pci.h"

11
static struct pci_bus *find_pci_root_bus(struct pci_bus *bus)
12 13 14 15
{
	while (bus->parent)
		bus = bus->parent;

16 17 18
	return bus;
}

19
struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus)
20
{
21
	struct pci_bus *root_bus = find_pci_root_bus(bus);
22

23
	return to_pci_host_bridge(root_bus->bridge);
24 25
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39
struct device *pci_get_host_bridge_device(struct pci_dev *dev)
{
	struct pci_bus *root_bus = find_pci_root_bus(dev->bus);
	struct device *bridge = root_bus->bridge;

	kobject_get(&bridge->kobj);
	return bridge;
}

void  pci_put_host_bridge_device(struct device *dev)
{
	kobject_put(&dev->kobj);
}

40 41 42 43 44 45 46
void pci_set_host_bridge_release(struct pci_host_bridge *bridge,
				 void (*release_fn)(struct pci_host_bridge *),
				 void *release_data)
{
	bridge->release_fn = release_fn;
	bridge->release_data = release_data;
}
47
EXPORT_SYMBOL_GPL(pci_set_host_bridge_release);
48

49
void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
50 51
			     struct resource *res)
{
52
	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
53
	struct resource_entry *window;
54 55
	resource_size_t offset = 0;

56
	resource_list_for_each_entry(window, &bridge->windows) {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
		if (resource_contains(window->res, res)) {
			offset = window->offset;
			break;
		}
	}

	region->start = res->start - offset;
	region->end = res->end - offset;
}
EXPORT_SYMBOL(pcibios_resource_to_bus);

static bool region_contains(struct pci_bus_region *region1,
			    struct pci_bus_region *region2)
{
	return region1->start <= region2->start && region1->end >= region2->end;
}

74
void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
75 76
			     struct pci_bus_region *region)
{
77
	struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
78
	struct resource_entry *window;
79 80
	resource_size_t offset = 0;

81
	resource_list_for_each_entry(window, &bridge->windows) {
82 83
		struct pci_bus_region bus_region;

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		if (resource_type(res) != resource_type(window->res))
			continue;

		bus_region.start = window->res->start - window->offset;
		bus_region.end = window->res->end - window->offset;

		if (region_contains(&bus_region, region)) {
			offset = window->offset;
			break;
		}
	}

	res->start = region->start + offset;
	res->end = region->end + offset;
}
EXPORT_SYMBOL(pcibios_bus_to_resource);