acpi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. status = resource_to_addr(acpi_res, &addr);
  63. if (!ACPI_SUCCESS(status))
  64. return AE_OK;
  65. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  66. root = &iomem_resource;
  67. flags = IORESOURCE_MEM;
  68. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  69. flags |= IORESOURCE_PREFETCH;
  70. } else if (addr.resource_type == ACPI_IO_RANGE) {
  71. root = &ioport_resource;
  72. flags = IORESOURCE_IO;
  73. } else
  74. return AE_OK;
  75. res = &info->res[info->res_num];
  76. res->name = info->name;
  77. res->flags = flags;
  78. res->start = addr.minimum + addr.translation_offset;
  79. res->end = res->start + addr.address_length - 1;
  80. res->child = NULL;
  81. if (bus_has_transparent_bridge(info->bus))
  82. max_root_bus_resources -= 3;
  83. if (info->res_num >= max_root_bus_resources) {
  84. printk(KERN_WARNING "PCI: Failed to allocate 0x%lx-0x%lx "
  85. "from %s for %s due to _CRS returning more than "
  86. "%d resource descriptors\n", (unsigned long) res->start,
  87. (unsigned long) res->end, root->name, info->name,
  88. max_root_bus_resources);
  89. info->res_num++;
  90. return AE_OK;
  91. }
  92. if (insert_resource(root, res)) {
  93. printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
  94. "from %s for %s\n", (unsigned long) res->start,
  95. (unsigned long) res->end, root->name, info->name);
  96. } else {
  97. info->bus->resource[info->res_num] = res;
  98. info->res_num++;
  99. }
  100. return AE_OK;
  101. }
  102. static void
  103. adjust_transparent_bridge_resources(struct pci_bus *bus)
  104. {
  105. struct pci_dev *dev;
  106. list_for_each_entry(dev, &bus->devices, bus_list) {
  107. int i;
  108. u16 class = dev->class >> 8;
  109. if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
  110. for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
  111. dev->subordinate->resource[i] =
  112. dev->bus->resource[i - 3];
  113. }
  114. }
  115. }
  116. static void
  117. get_current_resources(struct acpi_device *device, int busnum,
  118. int domain, struct pci_bus *bus)
  119. {
  120. struct pci_root_info info;
  121. size_t size;
  122. info.bus = bus;
  123. info.res_num = 0;
  124. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  125. &info);
  126. if (!info.res_num)
  127. return;
  128. size = sizeof(*info.res) * info.res_num;
  129. info.res = kmalloc(size, GFP_KERNEL);
  130. if (!info.res)
  131. goto res_alloc_fail;
  132. info.name = kmalloc(16, GFP_KERNEL);
  133. if (!info.name)
  134. goto name_alloc_fail;
  135. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  136. info.res_num = 0;
  137. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  138. &info);
  139. if (info.res_num)
  140. adjust_transparent_bridge_resources(bus);
  141. return;
  142. name_alloc_fail:
  143. kfree(info.res);
  144. res_alloc_fail:
  145. return;
  146. }
  147. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  148. {
  149. struct pci_bus *bus;
  150. struct pci_sysdata *sd;
  151. int node;
  152. #ifdef CONFIG_ACPI_NUMA
  153. int pxm;
  154. #endif
  155. if (domain && !pci_domains_supported) {
  156. printk(KERN_WARNING "PCI: Multiple domains not supported "
  157. "(dom %d, bus %d)\n", domain, busnum);
  158. return NULL;
  159. }
  160. node = -1;
  161. #ifdef CONFIG_ACPI_NUMA
  162. pxm = acpi_get_pxm(device->handle);
  163. if (pxm >= 0)
  164. node = pxm_to_node(pxm);
  165. if (node != -1)
  166. set_mp_bus_to_node(busnum, node);
  167. else
  168. #endif
  169. node = get_mp_bus_to_node(busnum);
  170. if (node != -1 && !node_online(node))
  171. node = -1;
  172. /* Allocate per-root-bus (not per bus) arch-specific data.
  173. * TODO: leak; this memory is never freed.
  174. * It's arguable whether it's worth the trouble to care.
  175. */
  176. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  177. if (!sd) {
  178. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  179. return NULL;
  180. }
  181. sd->domain = domain;
  182. sd->node = node;
  183. /*
  184. * Maybe the desired pci bus has been already scanned. In such case
  185. * it is unnecessary to scan the pci bus with the given domain,busnum.
  186. */
  187. bus = pci_find_bus(domain, busnum);
  188. if (bus) {
  189. /*
  190. * If the desired bus exits, the content of bus->sysdata will
  191. * be replaced by sd.
  192. */
  193. memcpy(bus->sysdata, sd, sizeof(*sd));
  194. kfree(sd);
  195. } else
  196. bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  197. if (!bus)
  198. kfree(sd);
  199. if (bus && node != -1) {
  200. #ifdef CONFIG_ACPI_NUMA
  201. if (pxm >= 0)
  202. dev_printk(KERN_DEBUG, &bus->dev,
  203. "on NUMA node %d (pxm %d)\n", node, pxm);
  204. #else
  205. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  206. #endif
  207. }
  208. if (bus && (pci_probe & PCI_USE__CRS))
  209. get_current_resources(device, busnum, domain, bus);
  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. }