acpi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 void
  43. align_resource(struct acpi_device *bridge, struct resource *res)
  44. {
  45. int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
  46. /*
  47. * Host bridge windows are not BARs, but the decoders on the PCI side
  48. * that claim this address space have starting alignment and length
  49. * constraints, so fix any obvious BIOS goofs.
  50. */
  51. if (!IS_ALIGNED(res->start, align)) {
  52. dev_printk(KERN_DEBUG, &bridge->dev,
  53. "host bridge window %pR invalid; "
  54. "aligning start to %d-byte boundary\n", res, align);
  55. res->start &= ~(align - 1);
  56. }
  57. if (!IS_ALIGNED(res->end + 1, align)) {
  58. dev_printk(KERN_DEBUG, &bridge->dev,
  59. "host bridge window %pR invalid; "
  60. "aligning end to %d-byte boundary\n", res, align);
  61. res->end = ALIGN(res->end, align) - 1;
  62. }
  63. }
  64. static acpi_status
  65. setup_resource(struct acpi_resource *acpi_res, void *data)
  66. {
  67. struct pci_root_info *info = data;
  68. struct resource *res;
  69. struct acpi_resource_address64 addr;
  70. acpi_status status;
  71. unsigned long flags;
  72. struct resource *root;
  73. u64 start, end;
  74. status = resource_to_addr(acpi_res, &addr);
  75. if (!ACPI_SUCCESS(status))
  76. return AE_OK;
  77. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  78. root = &iomem_resource;
  79. flags = IORESOURCE_MEM;
  80. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  81. flags |= IORESOURCE_PREFETCH;
  82. } else if (addr.resource_type == ACPI_IO_RANGE) {
  83. root = &ioport_resource;
  84. flags = IORESOURCE_IO;
  85. } else
  86. return AE_OK;
  87. start = addr.minimum + addr.translation_offset;
  88. end = start + addr.address_length - 1;
  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. align_resource(info->bridge, res);
  96. if (!(pci_probe & PCI_USE__CRS)) {
  97. dev_printk(KERN_DEBUG, &info->bridge->dev,
  98. "host bridge window %pR (ignored)\n", res);
  99. return AE_OK;
  100. }
  101. if (insert_resource(root, res)) {
  102. dev_err(&info->bridge->dev,
  103. "can't allocate host bridge window %pR\n", res);
  104. } else {
  105. pci_bus_add_resource(info->bus, res, 0);
  106. info->res_num++;
  107. if (addr.translation_offset)
  108. dev_info(&info->bridge->dev, "host bridge window %pR "
  109. "(PCI address [%#llx-%#llx])\n",
  110. res, res->start - addr.translation_offset,
  111. res->end - addr.translation_offset);
  112. else
  113. dev_info(&info->bridge->dev,
  114. "host bridge window %pR\n", res);
  115. }
  116. return AE_OK;
  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. if (pci_probe & PCI_USE__CRS)
  125. pci_bus_remove_resources(bus);
  126. else
  127. dev_info(&device->dev,
  128. "ignoring host bridge windows from ACPI; "
  129. "boot with \"pci=use_crs\" to use them\n");
  130. info.bridge = device;
  131. info.bus = bus;
  132. info.res_num = 0;
  133. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  134. &info);
  135. if (!info.res_num)
  136. return;
  137. size = sizeof(*info.res) * info.res_num;
  138. info.res = kmalloc(size, GFP_KERNEL);
  139. if (!info.res)
  140. goto res_alloc_fail;
  141. info.name = kmalloc(16, GFP_KERNEL);
  142. if (!info.name)
  143. goto name_alloc_fail;
  144. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  145. info.res_num = 0;
  146. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  147. &info);
  148. return;
  149. name_alloc_fail:
  150. kfree(info.res);
  151. res_alloc_fail:
  152. return;
  153. }
  154. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  155. {
  156. struct pci_bus *bus;
  157. struct pci_sysdata *sd;
  158. int node;
  159. #ifdef CONFIG_ACPI_NUMA
  160. int pxm;
  161. #endif
  162. if (domain && !pci_domains_supported) {
  163. printk(KERN_WARNING "pci_bus %04x:%02x: "
  164. "ignored (multiple domains not supported)\n",
  165. domain, busnum);
  166. return NULL;
  167. }
  168. node = -1;
  169. #ifdef CONFIG_ACPI_NUMA
  170. pxm = acpi_get_pxm(device->handle);
  171. if (pxm >= 0)
  172. node = pxm_to_node(pxm);
  173. if (node != -1)
  174. set_mp_bus_to_node(busnum, node);
  175. else
  176. #endif
  177. node = get_mp_bus_to_node(busnum);
  178. if (node != -1 && !node_online(node))
  179. node = -1;
  180. /* Allocate per-root-bus (not per bus) arch-specific data.
  181. * TODO: leak; this memory is never freed.
  182. * It's arguable whether it's worth the trouble to care.
  183. */
  184. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  185. if (!sd) {
  186. printk(KERN_WARNING "pci_bus %04x:%02x: "
  187. "ignored (out of memory)\n", domain, busnum);
  188. return NULL;
  189. }
  190. sd->domain = domain;
  191. sd->node = node;
  192. /*
  193. * Maybe the desired pci bus has been already scanned. In such case
  194. * it is unnecessary to scan the pci bus with the given domain,busnum.
  195. */
  196. bus = pci_find_bus(domain, busnum);
  197. if (bus) {
  198. /*
  199. * If the desired bus exits, the content of bus->sysdata will
  200. * be replaced by sd.
  201. */
  202. memcpy(bus->sysdata, sd, sizeof(*sd));
  203. kfree(sd);
  204. } else {
  205. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  206. if (bus) {
  207. get_current_resources(device, busnum, domain, bus);
  208. bus->subordinate = pci_scan_child_bus(bus);
  209. }
  210. }
  211. if (!bus)
  212. kfree(sd);
  213. if (bus && node != -1) {
  214. #ifdef CONFIG_ACPI_NUMA
  215. if (pxm >= 0)
  216. dev_printk(KERN_DEBUG, &bus->dev,
  217. "on NUMA node %d (pxm %d)\n", node, pxm);
  218. #else
  219. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  220. #endif
  221. }
  222. return bus;
  223. }
  224. int __init pci_acpi_init(void)
  225. {
  226. struct pci_dev *dev = NULL;
  227. if (pcibios_scanned)
  228. return 0;
  229. if (acpi_noirq)
  230. return 0;
  231. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  232. acpi_irq_penalty_init();
  233. pcibios_scanned++;
  234. pcibios_enable_irq = acpi_pci_irq_enable;
  235. pcibios_disable_irq = acpi_pci_irq_disable;
  236. if (pci_routeirq) {
  237. /*
  238. * PCI IRQ routing is set up by pci_enable_device(), but we
  239. * also do it here in case there are still broken drivers that
  240. * don't use pci_enable_device().
  241. */
  242. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  243. for_each_pci_dev(dev)
  244. acpi_pci_irq_enable(dev);
  245. }
  246. return 0;
  247. }