acpi.c 6.4 KB

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