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