acpi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 bool pci_use_crs = true;
  17. static int __init set_use_crs(const struct dmi_system_id *id)
  18. {
  19. pci_use_crs = true;
  20. return 0;
  21. }
  22. static const struct dmi_system_id pci_use_crs_table[] __initconst = {
  23. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  24. {
  25. .callback = set_use_crs,
  26. .ident = "IBM System x3800",
  27. .matches = {
  28. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  29. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  30. },
  31. },
  32. {}
  33. };
  34. void __init pci_acpi_crs_quirks(void)
  35. {
  36. int year;
  37. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
  38. pci_use_crs = false;
  39. dmi_check_system(pci_use_crs_table);
  40. /*
  41. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  42. * takes precedence over anything we figured out above.
  43. */
  44. if (pci_probe & PCI_ROOT_NO_CRS)
  45. pci_use_crs = false;
  46. else if (pci_probe & PCI_USE__CRS)
  47. pci_use_crs = true;
  48. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  49. "if necessary, use \"pci=%s\" and report a bug\n",
  50. pci_use_crs ? "Using" : "Ignoring",
  51. pci_use_crs ? "nocrs" : "use_crs");
  52. }
  53. static acpi_status
  54. resource_to_addr(struct acpi_resource *resource,
  55. struct acpi_resource_address64 *addr)
  56. {
  57. acpi_status status;
  58. status = acpi_resource_to_address64(resource, addr);
  59. if (ACPI_SUCCESS(status) &&
  60. (addr->resource_type == ACPI_MEMORY_RANGE ||
  61. addr->resource_type == ACPI_IO_RANGE) &&
  62. addr->address_length > 0 &&
  63. addr->producer_consumer == ACPI_PRODUCER) {
  64. return AE_OK;
  65. }
  66. return AE_ERROR;
  67. }
  68. static acpi_status
  69. count_resource(struct acpi_resource *acpi_res, void *data)
  70. {
  71. struct pci_root_info *info = data;
  72. struct acpi_resource_address64 addr;
  73. acpi_status status;
  74. status = resource_to_addr(acpi_res, &addr);
  75. if (ACPI_SUCCESS(status))
  76. info->res_num++;
  77. return AE_OK;
  78. }
  79. static void
  80. align_resource(struct acpi_device *bridge, struct resource *res)
  81. {
  82. int align = (res->flags & IORESOURCE_MEM) ? 16 : 4;
  83. /*
  84. * Host bridge windows are not BARs, but the decoders on the PCI side
  85. * that claim this address space have starting alignment and length
  86. * constraints, so fix any obvious BIOS goofs.
  87. */
  88. if (!IS_ALIGNED(res->start, align)) {
  89. dev_printk(KERN_DEBUG, &bridge->dev,
  90. "host bridge window %pR invalid; "
  91. "aligning start to %d-byte boundary\n", res, align);
  92. res->start &= ~(align - 1);
  93. }
  94. if (!IS_ALIGNED(res->end + 1, align)) {
  95. dev_printk(KERN_DEBUG, &bridge->dev,
  96. "host bridge window %pR invalid; "
  97. "aligning end to %d-byte boundary\n", res, align);
  98. res->end = ALIGN(res->end, align) - 1;
  99. }
  100. }
  101. static acpi_status
  102. setup_resource(struct acpi_resource *acpi_res, void *data)
  103. {
  104. struct pci_root_info *info = data;
  105. struct resource *res;
  106. struct acpi_resource_address64 addr;
  107. acpi_status status;
  108. unsigned long flags;
  109. struct resource *root;
  110. u64 start, end;
  111. status = resource_to_addr(acpi_res, &addr);
  112. if (!ACPI_SUCCESS(status))
  113. return AE_OK;
  114. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  115. root = &iomem_resource;
  116. flags = IORESOURCE_MEM;
  117. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  118. flags |= IORESOURCE_PREFETCH;
  119. } else if (addr.resource_type == ACPI_IO_RANGE) {
  120. root = &ioport_resource;
  121. flags = IORESOURCE_IO;
  122. } else
  123. return AE_OK;
  124. start = addr.minimum + addr.translation_offset;
  125. end = start + addr.address_length - 1;
  126. res = &info->res[info->res_num];
  127. res->name = info->name;
  128. res->flags = flags;
  129. res->start = start;
  130. res->end = end;
  131. res->child = NULL;
  132. align_resource(info->bridge, res);
  133. if (!pci_use_crs) {
  134. dev_printk(KERN_DEBUG, &info->bridge->dev,
  135. "host bridge window %pR (ignored)\n", res);
  136. return AE_OK;
  137. }
  138. if (insert_resource(root, res)) {
  139. dev_err(&info->bridge->dev,
  140. "can't allocate host bridge window %pR\n", res);
  141. } else {
  142. pci_bus_add_resource(info->bus, res, 0);
  143. info->res_num++;
  144. if (addr.translation_offset)
  145. dev_info(&info->bridge->dev, "host bridge window %pR "
  146. "(PCI address [%#llx-%#llx])\n",
  147. res, res->start - addr.translation_offset,
  148. res->end - addr.translation_offset);
  149. else
  150. dev_info(&info->bridge->dev,
  151. "host bridge window %pR\n", res);
  152. }
  153. return AE_OK;
  154. }
  155. static void
  156. get_current_resources(struct acpi_device *device, int busnum,
  157. int domain, struct pci_bus *bus)
  158. {
  159. struct pci_root_info info;
  160. size_t size;
  161. if (pci_use_crs)
  162. pci_bus_remove_resources(bus);
  163. info.bridge = device;
  164. info.bus = bus;
  165. info.res_num = 0;
  166. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  167. &info);
  168. if (!info.res_num)
  169. return;
  170. size = sizeof(*info.res) * info.res_num;
  171. info.res = kmalloc(size, GFP_KERNEL);
  172. if (!info.res)
  173. goto res_alloc_fail;
  174. info.name = kmalloc(16, GFP_KERNEL);
  175. if (!info.name)
  176. goto name_alloc_fail;
  177. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  178. info.res_num = 0;
  179. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  180. &info);
  181. return;
  182. name_alloc_fail:
  183. kfree(info.res);
  184. res_alloc_fail:
  185. return;
  186. }
  187. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  188. {
  189. struct pci_bus *bus;
  190. struct pci_sysdata *sd;
  191. int node;
  192. #ifdef CONFIG_ACPI_NUMA
  193. int pxm;
  194. #endif
  195. if (domain && !pci_domains_supported) {
  196. printk(KERN_WARNING "pci_bus %04x:%02x: "
  197. "ignored (multiple domains not supported)\n",
  198. domain, busnum);
  199. return NULL;
  200. }
  201. node = -1;
  202. #ifdef CONFIG_ACPI_NUMA
  203. pxm = acpi_get_pxm(device->handle);
  204. if (pxm >= 0)
  205. node = pxm_to_node(pxm);
  206. if (node != -1)
  207. set_mp_bus_to_node(busnum, node);
  208. else
  209. #endif
  210. node = get_mp_bus_to_node(busnum);
  211. if (node != -1 && !node_online(node))
  212. node = -1;
  213. /* Allocate per-root-bus (not per bus) arch-specific data.
  214. * TODO: leak; this memory is never freed.
  215. * It's arguable whether it's worth the trouble to care.
  216. */
  217. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  218. if (!sd) {
  219. printk(KERN_WARNING "pci_bus %04x:%02x: "
  220. "ignored (out of memory)\n", domain, busnum);
  221. return NULL;
  222. }
  223. sd->domain = domain;
  224. sd->node = node;
  225. /*
  226. * Maybe the desired pci bus has been already scanned. In such case
  227. * it is unnecessary to scan the pci bus with the given domain,busnum.
  228. */
  229. bus = pci_find_bus(domain, busnum);
  230. if (bus) {
  231. /*
  232. * If the desired bus exits, the content of bus->sysdata will
  233. * be replaced by sd.
  234. */
  235. memcpy(bus->sysdata, sd, sizeof(*sd));
  236. kfree(sd);
  237. } else {
  238. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  239. if (bus) {
  240. get_current_resources(device, busnum, domain, bus);
  241. bus->subordinate = pci_scan_child_bus(bus);
  242. }
  243. }
  244. if (!bus)
  245. kfree(sd);
  246. if (bus && node != -1) {
  247. #ifdef CONFIG_ACPI_NUMA
  248. if (pxm >= 0)
  249. dev_printk(KERN_DEBUG, &bus->dev,
  250. "on NUMA node %d (pxm %d)\n", node, pxm);
  251. #else
  252. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  253. #endif
  254. }
  255. return bus;
  256. }
  257. int __init pci_acpi_init(void)
  258. {
  259. struct pci_dev *dev = NULL;
  260. if (acpi_noirq)
  261. return -ENODEV;
  262. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  263. acpi_irq_penalty_init();
  264. pcibios_enable_irq = acpi_pci_irq_enable;
  265. pcibios_disable_irq = acpi_pci_irq_disable;
  266. x86_init.pci.init_irq = x86_init_noop;
  267. if (pci_routeirq) {
  268. /*
  269. * PCI IRQ routing is set up by pci_enable_device(), but we
  270. * also do it here in case there are still broken drivers that
  271. * don't use pci_enable_device().
  272. */
  273. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  274. for_each_pci_dev(dev)
  275. acpi_pci_irq_enable(dev);
  276. }
  277. return 0;
  278. }