acpi.c 8.1 KB

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