Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0 2 : /* 3 : * Host bridge related code 4 : */ 5 : 6 : #include <linux/kernel.h> 7 : #include <linux/pci.h> 8 : #include <linux/module.h> 9 : 10 : #include "pci.h" 11 : 12 : static struct pci_bus *find_pci_root_bus(struct pci_bus *bus) 13 : { 14 0 : while (bus->parent) 15 : bus = bus->parent; 16 : 17 : return bus; 18 : } 19 : 20 0 : struct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus) 21 : { 22 0 : struct pci_bus *root_bus = find_pci_root_bus(bus); 23 : 24 0 : return to_pci_host_bridge(root_bus->bridge); 25 : } 26 : EXPORT_SYMBOL_GPL(pci_find_host_bridge); 27 : 28 0 : struct device *pci_get_host_bridge_device(struct pci_dev *dev) 29 : { 30 0 : struct pci_bus *root_bus = find_pci_root_bus(dev->bus); 31 0 : struct device *bridge = root_bus->bridge; 32 : 33 0 : kobject_get(&bridge->kobj); 34 0 : return bridge; 35 : } 36 : 37 0 : void pci_put_host_bridge_device(struct device *dev) 38 : { 39 0 : kobject_put(&dev->kobj); 40 0 : } 41 : 42 0 : void pci_set_host_bridge_release(struct pci_host_bridge *bridge, 43 : void (*release_fn)(struct pci_host_bridge *), 44 : void *release_data) 45 : { 46 0 : bridge->release_fn = release_fn; 47 0 : bridge->release_data = release_data; 48 0 : } 49 : EXPORT_SYMBOL_GPL(pci_set_host_bridge_release); 50 : 51 0 : void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region, 52 : struct resource *res) 53 : { 54 0 : struct pci_host_bridge *bridge = pci_find_host_bridge(bus); 55 : struct resource_entry *window; 56 0 : resource_size_t offset = 0; 57 : 58 0 : resource_list_for_each_entry(window, &bridge->windows) { 59 0 : if (resource_contains(window->res, res)) { 60 0 : offset = window->offset; 61 0 : break; 62 : } 63 : } 64 : 65 0 : region->start = res->start - offset; 66 0 : region->end = res->end - offset; 67 0 : } 68 : EXPORT_SYMBOL(pcibios_resource_to_bus); 69 : 70 : static bool region_contains(struct pci_bus_region *region1, 71 : struct pci_bus_region *region2) 72 : { 73 0 : return region1->start <= region2->start && region1->end >= region2->end; 74 : } 75 : 76 0 : void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res, 77 : struct pci_bus_region *region) 78 : { 79 0 : struct pci_host_bridge *bridge = pci_find_host_bridge(bus); 80 : struct resource_entry *window; 81 0 : resource_size_t offset = 0; 82 : 83 0 : resource_list_for_each_entry(window, &bridge->windows) { 84 : struct pci_bus_region bus_region; 85 : 86 0 : if (resource_type(res) != resource_type(window->res)) 87 0 : continue; 88 : 89 0 : bus_region.start = window->res->start - window->offset; 90 0 : bus_region.end = window->res->end - window->offset; 91 : 92 0 : if (region_contains(&bus_region, region)) { 93 0 : offset = window->offset; 94 : break; 95 : } 96 : } 97 : 98 0 : res->start = region->start + offset; 99 0 : res->end = region->end + offset; 100 0 : } 101 : EXPORT_SYMBOL(pcibios_bus_to_resource);