acpi.c 8.4 KB

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