host-bridge.c 2.3 KB

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