acpi.c 9.2 KB

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