18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Host bridge related code 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/kernel.h> 78c2ecf20Sopenharmony_ci#include <linux/pci.h> 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include "pci.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistatic struct pci_bus *find_pci_root_bus(struct pci_bus *bus) 138c2ecf20Sopenharmony_ci{ 148c2ecf20Sopenharmony_ci while (bus->parent) 158c2ecf20Sopenharmony_ci bus = bus->parent; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci return bus; 188c2ecf20Sopenharmony_ci} 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct pci_host_bridge *pci_find_host_bridge(struct pci_bus *bus) 218c2ecf20Sopenharmony_ci{ 228c2ecf20Sopenharmony_ci struct pci_bus *root_bus = find_pci_root_bus(bus); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci return to_pci_host_bridge(root_bus->bridge); 258c2ecf20Sopenharmony_ci} 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct device *pci_get_host_bridge_device(struct pci_dev *dev) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct pci_bus *root_bus = find_pci_root_bus(dev->bus); 308c2ecf20Sopenharmony_ci struct device *bridge = root_bus->bridge; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci kobject_get(&bridge->kobj); 338c2ecf20Sopenharmony_ci return bridge; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_civoid pci_put_host_bridge_device(struct device *dev) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci kobject_put(&dev->kobj); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_civoid pci_set_host_bridge_release(struct pci_host_bridge *bridge, 428c2ecf20Sopenharmony_ci void (*release_fn)(struct pci_host_bridge *), 438c2ecf20Sopenharmony_ci void *release_data) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci bridge->release_fn = release_fn; 468c2ecf20Sopenharmony_ci bridge->release_data = release_data; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_set_host_bridge_release); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_civoid pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region, 518c2ecf20Sopenharmony_ci struct resource *res) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge = pci_find_host_bridge(bus); 548c2ecf20Sopenharmony_ci struct resource_entry *window; 558c2ecf20Sopenharmony_ci resource_size_t offset = 0; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci resource_list_for_each_entry(window, &bridge->windows) { 588c2ecf20Sopenharmony_ci if (resource_contains(window->res, res)) { 598c2ecf20Sopenharmony_ci offset = window->offset; 608c2ecf20Sopenharmony_ci break; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci region->start = res->start - offset; 658c2ecf20Sopenharmony_ci region->end = res->end - offset; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pcibios_resource_to_bus); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic bool region_contains(struct pci_bus_region *region1, 708c2ecf20Sopenharmony_ci struct pci_bus_region *region2) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci return region1->start <= region2->start && region1->end >= region2->end; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_civoid pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res, 768c2ecf20Sopenharmony_ci struct pci_bus_region *region) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge = pci_find_host_bridge(bus); 798c2ecf20Sopenharmony_ci struct resource_entry *window; 808c2ecf20Sopenharmony_ci resource_size_t offset = 0; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci resource_list_for_each_entry(window, &bridge->windows) { 838c2ecf20Sopenharmony_ci struct pci_bus_region bus_region; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (resource_type(res) != resource_type(window->res)) 868c2ecf20Sopenharmony_ci continue; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci bus_region.start = window->res->start - window->offset; 898c2ecf20Sopenharmony_ci bus_region.end = window->res->end - window->offset; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (region_contains(&bus_region, region)) { 928c2ecf20Sopenharmony_ci offset = window->offset; 938c2ecf20Sopenharmony_ci break; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci res->start = region->start + offset; 988c2ecf20Sopenharmony_ci res->end = region->end + offset; 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pcibios_bus_to_resource); 101