bus_numa.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/pci.h>
  4. #include <linux/range.h>
  5. #include "bus_numa.h"
  6. LIST_HEAD(pci_root_infos);
  7. static struct pci_root_info *x86_find_pci_root_info(int bus)
  8. {
  9. struct pci_root_info *info;
  10. list_for_each_entry(info, &pci_root_infos, list)
  11. if (info->busn.start == bus)
  12. return info;
  13. return NULL;
  14. }
  15. int x86_pci_root_bus_node(int bus)
  16. {
  17. struct pci_root_info *info = x86_find_pci_root_info(bus);
  18. if (!info)
  19. return NUMA_NO_NODE;
  20. return info->node;
  21. }
  22. void x86_pci_root_bus_resources(int bus, struct list_head *resources)
  23. {
  24. struct pci_root_info *info = x86_find_pci_root_info(bus);
  25. struct pci_root_res *root_res;
  26. struct resource_entry *window;
  27. bool found = false;
  28. if (!info)
  29. goto default_resources;
  30. printk(KERN_DEBUG "PCI: root bus %02x: hardware-probed resources\n",
  31. bus);
  32. /* already added by acpi ? */
  33. resource_list_for_each_entry(window, resources)
  34. if (window->res->flags & IORESOURCE_BUS) {
  35. found = true;
  36. break;
  37. }
  38. if (!found)
  39. pci_add_resource(resources, &info->busn);
  40. list_for_each_entry(root_res, &info->resources, list)
  41. pci_add_resource(resources, &root_res->res);
  42. return;
  43. default_resources:
  44. /*
  45. * We don't have any host bridge aperture information from the
  46. * "native host bridge drivers," e.g., amd_bus or broadcom_bus,
  47. * so fall back to the defaults historically used by pci_create_bus().
  48. */
  49. printk(KERN_DEBUG "PCI: root bus %02x: using default resources\n", bus);
  50. pci_add_resource(resources, &ioport_resource);
  51. pci_add_resource(resources, &iomem_resource);
  52. }
  53. struct pci_root_info __init *alloc_pci_root_info(int bus_min, int bus_max,
  54. int node, int link)
  55. {
  56. struct pci_root_info *info;
  57. info = kzalloc(sizeof(*info), GFP_KERNEL);
  58. if (!info)
  59. return info;
  60. sprintf(info->name, "PCI Bus #%02x", bus_min);
  61. INIT_LIST_HEAD(&info->resources);
  62. info->busn.name = info->name;
  63. info->busn.start = bus_min;
  64. info->busn.end = bus_max;
  65. info->busn.flags = IORESOURCE_BUS;
  66. info->node = node;
  67. info->link = link;
  68. list_add_tail(&info->list, &pci_root_infos);
  69. return info;
  70. }
  71. void update_res(struct pci_root_info *info, resource_size_t start,
  72. resource_size_t end, unsigned long flags, int merge)
  73. {
  74. struct resource *res;
  75. struct pci_root_res *root_res;
  76. if (start > end)
  77. return;
  78. if (start == MAX_RESOURCE)
  79. return;
  80. if (!merge)
  81. goto addit;
  82. /* try to merge it with old one */
  83. list_for_each_entry(root_res, &info->resources, list) {
  84. resource_size_t final_start, final_end;
  85. resource_size_t common_start, common_end;
  86. res = &root_res->res;
  87. if (res->flags != flags)
  88. continue;
  89. common_start = max(res->start, start);
  90. common_end = min(res->end, end);
  91. if (common_start > common_end + 1)
  92. continue;
  93. final_start = min(res->start, start);
  94. final_end = max(res->end, end);
  95. res->start = final_start;
  96. res->end = final_end;
  97. return;
  98. }
  99. addit:
  100. /* need to add that */
  101. root_res = kzalloc(sizeof(*root_res), GFP_KERNEL);
  102. if (!root_res)
  103. return;
  104. res = &root_res->res;
  105. res->name = info->name;
  106. res->flags = flags;
  107. res->start = start;
  108. res->end = end;
  109. list_add_tail(&root_res->list, &info->resources);
  110. }