acpi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <linux/pci.h>
  2. #include <linux/acpi.h>
  3. #include <linux/init.h>
  4. #include <linux/irq.h>
  5. #include <linux/dmi.h>
  6. #include <asm/numa.h>
  7. #include <asm/pci_x86.h>
  8. struct pci_root_info {
  9. struct acpi_device *bridge;
  10. char *name;
  11. unsigned int res_num;
  12. struct resource *res;
  13. struct pci_bus *bus;
  14. int busnum;
  15. };
  16. static acpi_status
  17. resource_to_addr(struct acpi_resource *resource,
  18. struct acpi_resource_address64 *addr)
  19. {
  20. acpi_status status;
  21. status = acpi_resource_to_address64(resource, addr);
  22. if (ACPI_SUCCESS(status) &&
  23. (addr->resource_type == ACPI_MEMORY_RANGE ||
  24. addr->resource_type == ACPI_IO_RANGE) &&
  25. addr->address_length > 0 &&
  26. addr->producer_consumer == ACPI_PRODUCER) {
  27. return AE_OK;
  28. }
  29. return AE_ERROR;
  30. }
  31. static acpi_status
  32. count_resource(struct acpi_resource *acpi_res, void *data)
  33. {
  34. struct pci_root_info *info = data;
  35. struct acpi_resource_address64 addr;
  36. acpi_status status;
  37. status = resource_to_addr(acpi_res, &addr);
  38. if (ACPI_SUCCESS(status))
  39. info->res_num++;
  40. return AE_OK;
  41. }
  42. static int
  43. bus_has_transparent_bridge(struct pci_bus *bus)
  44. {
  45. struct pci_dev *dev;
  46. list_for_each_entry(dev, &bus->devices, bus_list) {
  47. u16 class = dev->class >> 8;
  48. if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent)
  49. return true;
  50. }
  51. return false;
  52. }
  53. static acpi_status
  54. setup_resource(struct acpi_resource *acpi_res, void *data)
  55. {
  56. struct pci_root_info *info = data;
  57. struct resource *res;
  58. struct acpi_resource_address64 addr;
  59. acpi_status status;
  60. unsigned long flags;
  61. struct resource *root;
  62. int max_root_bus_resources = PCI_BUS_NUM_RESOURCES;
  63. u64 start, end;
  64. if (bus_has_transparent_bridge(info->bus))
  65. max_root_bus_resources -= 3;
  66. status = resource_to_addr(acpi_res, &addr);
  67. if (!ACPI_SUCCESS(status))
  68. return AE_OK;
  69. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  70. root = &iomem_resource;
  71. flags = IORESOURCE_MEM;
  72. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  73. flags |= IORESOURCE_PREFETCH;
  74. } else if (addr.resource_type == ACPI_IO_RANGE) {
  75. root = &ioport_resource;
  76. flags = IORESOURCE_IO;
  77. } else
  78. return AE_OK;
  79. start = addr.minimum + addr.translation_offset;
  80. end = start + addr.address_length - 1;
  81. if (info->res_num >= max_root_bus_resources) {
  82. printk(KERN_WARNING "PCI: Failed to allocate 0x%lx-0x%lx "
  83. "from %s for %s due to _CRS returning more than "
  84. "%d resource descriptors\n", (unsigned long) start,
  85. (unsigned long) end, root->name, info->name,
  86. max_root_bus_resources);
  87. return AE_OK;
  88. }
  89. res = &info->res[info->res_num];
  90. res->name = info->name;
  91. res->flags = flags;
  92. res->start = start;
  93. res->end = end;
  94. res->child = NULL;
  95. if (insert_resource(root, res)) {
  96. dev_err(&info->bridge->dev, "can't allocate %pRt\n", res);
  97. } else {
  98. info->bus->resource[info->res_num] = res;
  99. info->res_num++;
  100. if (addr.translation_offset)
  101. dev_info(&info->bridge->dev, "host bridge window: %pRt "
  102. "(PCI address [%#llx-%#llx])\n",
  103. res, res->start - addr.translation_offset,
  104. res->end - addr.translation_offset);
  105. else
  106. dev_info(&info->bridge->dev,
  107. "host bridge window: %pRt\n", res);
  108. }
  109. return AE_OK;
  110. }
  111. static void
  112. get_current_resources(struct acpi_device *device, int busnum,
  113. int domain, struct pci_bus *bus)
  114. {
  115. struct pci_root_info info;
  116. size_t size;
  117. info.bridge = device;
  118. info.bus = bus;
  119. info.res_num = 0;
  120. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  121. &info);
  122. if (!info.res_num)
  123. return;
  124. size = sizeof(*info.res) * info.res_num;
  125. info.res = kmalloc(size, GFP_KERNEL);
  126. if (!info.res)
  127. goto res_alloc_fail;
  128. info.name = kmalloc(16, GFP_KERNEL);
  129. if (!info.name)
  130. goto name_alloc_fail;
  131. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  132. info.res_num = 0;
  133. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  134. &info);
  135. return;
  136. name_alloc_fail:
  137. kfree(info.res);
  138. res_alloc_fail:
  139. return;
  140. }
  141. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  142. {
  143. struct pci_bus *bus;
  144. struct pci_sysdata *sd;
  145. int node;
  146. #ifdef CONFIG_ACPI_NUMA
  147. int pxm;
  148. #endif
  149. if (domain && !pci_domains_supported) {
  150. printk(KERN_WARNING "PCI: Multiple domains not supported "
  151. "(dom %d, bus %d)\n", domain, busnum);
  152. return NULL;
  153. }
  154. node = -1;
  155. #ifdef CONFIG_ACPI_NUMA
  156. pxm = acpi_get_pxm(device->handle);
  157. if (pxm >= 0)
  158. node = pxm_to_node(pxm);
  159. if (node != -1)
  160. set_mp_bus_to_node(busnum, node);
  161. else
  162. #endif
  163. node = get_mp_bus_to_node(busnum);
  164. if (node != -1 && !node_online(node))
  165. node = -1;
  166. /* Allocate per-root-bus (not per bus) arch-specific data.
  167. * TODO: leak; this memory is never freed.
  168. * It's arguable whether it's worth the trouble to care.
  169. */
  170. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  171. if (!sd) {
  172. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  173. return NULL;
  174. }
  175. sd->domain = domain;
  176. sd->node = node;
  177. /*
  178. * Maybe the desired pci bus has been already scanned. In such case
  179. * it is unnecessary to scan the pci bus with the given domain,busnum.
  180. */
  181. bus = pci_find_bus(domain, busnum);
  182. if (bus) {
  183. /*
  184. * If the desired bus exits, the content of bus->sysdata will
  185. * be replaced by sd.
  186. */
  187. memcpy(bus->sysdata, sd, sizeof(*sd));
  188. kfree(sd);
  189. } else {
  190. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  191. if (bus) {
  192. if (pci_probe & PCI_USE__CRS)
  193. get_current_resources(device, busnum, domain,
  194. bus);
  195. bus->subordinate = pci_scan_child_bus(bus);
  196. }
  197. }
  198. if (!bus)
  199. kfree(sd);
  200. if (bus && node != -1) {
  201. #ifdef CONFIG_ACPI_NUMA
  202. if (pxm >= 0)
  203. dev_printk(KERN_DEBUG, &bus->dev,
  204. "on NUMA node %d (pxm %d)\n", node, pxm);
  205. #else
  206. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  207. #endif
  208. }
  209. return bus;
  210. }
  211. int __init pci_acpi_init(void)
  212. {
  213. struct pci_dev *dev = NULL;
  214. if (pcibios_scanned)
  215. return 0;
  216. if (acpi_noirq)
  217. return 0;
  218. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  219. acpi_irq_penalty_init();
  220. pcibios_scanned++;
  221. pcibios_enable_irq = acpi_pci_irq_enable;
  222. pcibios_disable_irq = acpi_pci_irq_disable;
  223. if (pci_routeirq) {
  224. /*
  225. * PCI IRQ routing is set up by pci_enable_device(), but we
  226. * also do it here in case there are still broken drivers that
  227. * don't use pci_enable_device().
  228. */
  229. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  230. for_each_pci_dev(dev)
  231. acpi_pci_irq_enable(dev);
  232. }
  233. return 0;
  234. }