host-bridge.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void pcibios_resource_to_bus(struct pci_bus *bus, struct pci_bus_region *region,
  38. struct resource *res)
  39. {
  40. struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
  41. struct resource_entry *window;
  42. resource_size_t offset = 0;
  43. resource_list_for_each_entry(window, &bridge->windows) {
  44. if (resource_contains(window->res, res)) {
  45. offset = window->offset;
  46. break;
  47. }
  48. }
  49. region->start = res->start - offset;
  50. region->end = res->end - offset;
  51. }
  52. EXPORT_SYMBOL(pcibios_resource_to_bus);
  53. static bool region_contains(struct pci_bus_region *region1,
  54. struct pci_bus_region *region2)
  55. {
  56. return region1->start <= region2->start && region1->end >= region2->end;
  57. }
  58. void pcibios_bus_to_resource(struct pci_bus *bus, struct resource *res,
  59. struct pci_bus_region *region)
  60. {
  61. struct pci_host_bridge *bridge = pci_find_host_bridge(bus);
  62. struct resource_entry *window;
  63. resource_size_t offset = 0;
  64. resource_list_for_each_entry(window, &bridge->windows) {
  65. struct pci_bus_region bus_region;
  66. if (resource_type(res) != resource_type(window->res))
  67. continue;
  68. bus_region.start = window->res->start - window->offset;
  69. bus_region.end = window->res->end - window->offset;
  70. if (region_contains(&bus_region, region)) {
  71. offset = window->offset;
  72. break;
  73. }
  74. }
  75. res->start = region->start + offset;
  76. res->end = region->end + offset;
  77. }
  78. EXPORT_SYMBOL(pcibios_bus_to_resource);