acpi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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, *conflict;
  110. u64 start, end, max_len;
  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. max_len = addr.maximum - addr.minimum + 1;
  125. if (addr.address_length > max_len) {
  126. dev_printk(KERN_DEBUG, &info->bridge->dev,
  127. "host bridge window length %#llx doesn't fit in "
  128. "%#llx-%#llx, trimming\n",
  129. (unsigned long long) addr.address_length,
  130. (unsigned long long) addr.minimum,
  131. (unsigned long long) addr.maximum);
  132. addr.address_length = max_len;
  133. }
  134. start = addr.minimum + addr.translation_offset;
  135. end = start + addr.address_length - 1;
  136. res = &info->res[info->res_num];
  137. res->name = info->name;
  138. res->flags = flags;
  139. res->start = start;
  140. res->end = end;
  141. res->child = NULL;
  142. align_resource(info->bridge, res);
  143. if (!pci_use_crs) {
  144. dev_printk(KERN_DEBUG, &info->bridge->dev,
  145. "host bridge window %pR (ignored)\n", res);
  146. return AE_OK;
  147. }
  148. conflict = insert_resource_conflict(root, res);
  149. if (conflict) {
  150. dev_err(&info->bridge->dev,
  151. "address space collision: host bridge window %pR "
  152. "conflicts with %s %pR\n",
  153. res, conflict->name, conflict);
  154. } else {
  155. pci_bus_add_resource(info->bus, res, 0);
  156. info->res_num++;
  157. if (addr.translation_offset)
  158. dev_info(&info->bridge->dev, "host bridge window %pR "
  159. "(PCI address [%#llx-%#llx])\n",
  160. res, res->start - addr.translation_offset,
  161. res->end - addr.translation_offset);
  162. else
  163. dev_info(&info->bridge->dev,
  164. "host bridge window %pR\n", res);
  165. }
  166. return AE_OK;
  167. }
  168. static void
  169. get_current_resources(struct acpi_device *device, int busnum,
  170. int domain, struct pci_bus *bus)
  171. {
  172. struct pci_root_info info;
  173. size_t size;
  174. if (pci_use_crs)
  175. pci_bus_remove_resources(bus);
  176. info.bridge = device;
  177. info.bus = bus;
  178. info.res_num = 0;
  179. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  180. &info);
  181. if (!info.res_num)
  182. return;
  183. size = sizeof(*info.res) * info.res_num;
  184. info.res = kmalloc(size, GFP_KERNEL);
  185. if (!info.res)
  186. goto res_alloc_fail;
  187. info.name = kmalloc(16, GFP_KERNEL);
  188. if (!info.name)
  189. goto name_alloc_fail;
  190. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  191. info.res_num = 0;
  192. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  193. &info);
  194. return;
  195. name_alloc_fail:
  196. kfree(info.res);
  197. res_alloc_fail:
  198. return;
  199. }
  200. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  201. {
  202. struct pci_bus *bus;
  203. struct pci_sysdata *sd;
  204. int node;
  205. #ifdef CONFIG_ACPI_NUMA
  206. int pxm;
  207. #endif
  208. if (domain && !pci_domains_supported) {
  209. printk(KERN_WARNING "pci_bus %04x:%02x: "
  210. "ignored (multiple domains not supported)\n",
  211. domain, busnum);
  212. return NULL;
  213. }
  214. node = -1;
  215. #ifdef CONFIG_ACPI_NUMA
  216. pxm = acpi_get_pxm(device->handle);
  217. if (pxm >= 0)
  218. node = pxm_to_node(pxm);
  219. if (node != -1)
  220. set_mp_bus_to_node(busnum, node);
  221. else
  222. #endif
  223. node = get_mp_bus_to_node(busnum);
  224. if (node != -1 && !node_online(node))
  225. node = -1;
  226. /* Allocate per-root-bus (not per bus) arch-specific data.
  227. * TODO: leak; this memory is never freed.
  228. * It's arguable whether it's worth the trouble to care.
  229. */
  230. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  231. if (!sd) {
  232. printk(KERN_WARNING "pci_bus %04x:%02x: "
  233. "ignored (out of memory)\n", domain, busnum);
  234. return NULL;
  235. }
  236. sd->domain = domain;
  237. sd->node = node;
  238. /*
  239. * Maybe the desired pci bus has been already scanned. In such case
  240. * it is unnecessary to scan the pci bus with the given domain,busnum.
  241. */
  242. bus = pci_find_bus(domain, busnum);
  243. if (bus) {
  244. /*
  245. * If the desired bus exits, the content of bus->sysdata will
  246. * be replaced by sd.
  247. */
  248. memcpy(bus->sysdata, sd, sizeof(*sd));
  249. kfree(sd);
  250. } else {
  251. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  252. if (bus) {
  253. get_current_resources(device, busnum, domain, bus);
  254. bus->subordinate = pci_scan_child_bus(bus);
  255. }
  256. }
  257. if (!bus)
  258. kfree(sd);
  259. if (bus && node != -1) {
  260. #ifdef CONFIG_ACPI_NUMA
  261. if (pxm >= 0)
  262. dev_printk(KERN_DEBUG, &bus->dev,
  263. "on NUMA node %d (pxm %d)\n", node, pxm);
  264. #else
  265. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  266. #endif
  267. }
  268. return bus;
  269. }
  270. int __init pci_acpi_init(void)
  271. {
  272. struct pci_dev *dev = NULL;
  273. if (acpi_noirq)
  274. return -ENODEV;
  275. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  276. acpi_irq_penalty_init();
  277. pcibios_enable_irq = acpi_pci_irq_enable;
  278. pcibios_disable_irq = acpi_pci_irq_disable;
  279. x86_init.pci.init_irq = x86_init_noop;
  280. if (pci_routeirq) {
  281. /*
  282. * PCI IRQ routing is set up by pci_enable_device(), but we
  283. * also do it here in case there are still broken drivers that
  284. * don't use pci_enable_device().
  285. */
  286. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  287. for_each_pci_dev(dev)
  288. acpi_pci_irq_enable(dev);
  289. }
  290. return 0;
  291. }