acpi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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,
  97. "can't allocate host bridge window %pR\n", res);
  98. } else {
  99. info->bus->resource[info->res_num] = res;
  100. info->res_num++;
  101. if (addr.translation_offset)
  102. dev_info(&info->bridge->dev, "host bridge window %pR "
  103. "(PCI address [%#llx-%#llx])\n",
  104. res, res->start - addr.translation_offset,
  105. res->end - addr.translation_offset);
  106. else
  107. dev_info(&info->bridge->dev,
  108. "host bridge window %pR\n", res);
  109. }
  110. return AE_OK;
  111. }
  112. static void
  113. get_current_resources(struct acpi_device *device, int busnum,
  114. int domain, struct pci_bus *bus)
  115. {
  116. struct pci_root_info info;
  117. size_t size;
  118. info.bridge = device;
  119. info.bus = bus;
  120. info.res_num = 0;
  121. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  122. &info);
  123. if (!info.res_num)
  124. return;
  125. size = sizeof(*info.res) * info.res_num;
  126. info.res = kmalloc(size, GFP_KERNEL);
  127. if (!info.res)
  128. goto res_alloc_fail;
  129. info.name = kmalloc(16, GFP_KERNEL);
  130. if (!info.name)
  131. goto name_alloc_fail;
  132. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  133. info.res_num = 0;
  134. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  135. &info);
  136. return;
  137. name_alloc_fail:
  138. kfree(info.res);
  139. res_alloc_fail:
  140. return;
  141. }
  142. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  143. {
  144. struct pci_bus *bus;
  145. struct pci_sysdata *sd;
  146. int node;
  147. #ifdef CONFIG_ACPI_NUMA
  148. int pxm;
  149. #endif
  150. if (domain && !pci_domains_supported) {
  151. printk(KERN_WARNING "PCI: Multiple domains not supported "
  152. "(dom %d, bus %d)\n", domain, busnum);
  153. return NULL;
  154. }
  155. node = -1;
  156. #ifdef CONFIG_ACPI_NUMA
  157. pxm = acpi_get_pxm(device->handle);
  158. if (pxm >= 0)
  159. node = pxm_to_node(pxm);
  160. if (node != -1)
  161. set_mp_bus_to_node(busnum, node);
  162. else
  163. #endif
  164. node = get_mp_bus_to_node(busnum);
  165. if (node != -1 && !node_online(node))
  166. node = -1;
  167. /* Allocate per-root-bus (not per bus) arch-specific data.
  168. * TODO: leak; this memory is never freed.
  169. * It's arguable whether it's worth the trouble to care.
  170. */
  171. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  172. if (!sd) {
  173. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  174. return NULL;
  175. }
  176. sd->domain = domain;
  177. sd->node = node;
  178. /*
  179. * Maybe the desired pci bus has been already scanned. In such case
  180. * it is unnecessary to scan the pci bus with the given domain,busnum.
  181. */
  182. bus = pci_find_bus(domain, busnum);
  183. if (bus) {
  184. /*
  185. * If the desired bus exits, the content of bus->sysdata will
  186. * be replaced by sd.
  187. */
  188. memcpy(bus->sysdata, sd, sizeof(*sd));
  189. kfree(sd);
  190. } else {
  191. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  192. if (bus) {
  193. if (pci_probe & PCI_USE__CRS)
  194. get_current_resources(device, busnum, domain,
  195. bus);
  196. bus->subordinate = pci_scan_child_bus(bus);
  197. }
  198. }
  199. if (!bus)
  200. kfree(sd);
  201. if (bus && node != -1) {
  202. #ifdef CONFIG_ACPI_NUMA
  203. if (pxm >= 0)
  204. dev_printk(KERN_DEBUG, &bus->dev,
  205. "on NUMA node %d (pxm %d)\n", node, pxm);
  206. #else
  207. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  208. #endif
  209. }
  210. return bus;
  211. }
  212. int __init pci_acpi_init(void)
  213. {
  214. struct pci_dev *dev = NULL;
  215. if (pcibios_scanned)
  216. return 0;
  217. if (acpi_noirq)
  218. return 0;
  219. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  220. acpi_irq_penalty_init();
  221. pcibios_scanned++;
  222. pcibios_enable_irq = acpi_pci_irq_enable;
  223. pcibios_disable_irq = acpi_pci_irq_disable;
  224. if (pci_routeirq) {
  225. /*
  226. * PCI IRQ routing is set up by pci_enable_device(), but we
  227. * also do it here in case there are still broken drivers that
  228. * don't use pci_enable_device().
  229. */
  230. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  231. for_each_pci_dev(dev)
  232. acpi_pci_irq_enable(dev);
  233. }
  234. return 0;
  235. }