acpi.c 7.4 KB

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