acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 int __init set_nouse_crs(const struct dmi_system_id *id)
  24. {
  25. pci_use_crs = false;
  26. return 0;
  27. }
  28. static const struct dmi_system_id pci_use_crs_table[] __initconst = {
  29. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  30. {
  31. .callback = set_use_crs,
  32. .ident = "IBM System x3800",
  33. .matches = {
  34. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  35. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  36. },
  37. },
  38. /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
  39. /* 2006 AMD HT/VIA system with two host bridges */
  40. {
  41. .callback = set_use_crs,
  42. .ident = "ASRock ALiveSATA2-GLAN",
  43. .matches = {
  44. DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
  45. },
  46. },
  47. /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
  48. /* 2006 AMD HT/VIA system with two host bridges */
  49. {
  50. .callback = set_use_crs,
  51. .ident = "ASUS M2V-MX SE",
  52. .matches = {
  53. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  54. DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
  55. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  56. },
  57. },
  58. /* Now for the blacklist.. */
  59. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  60. {
  61. .callback = set_nouse_crs,
  62. .ident = "Dell Studio 1557",
  63. .matches = {
  64. DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
  65. DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
  66. DMI_MATCH(DMI_BIOS_VERSION, "A09"),
  67. },
  68. },
  69. {}
  70. };
  71. void __init pci_acpi_crs_quirks(void)
  72. {
  73. int year;
  74. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008)
  75. pci_use_crs = false;
  76. dmi_check_system(pci_use_crs_table);
  77. /*
  78. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  79. * takes precedence over anything we figured out above.
  80. */
  81. if (pci_probe & PCI_ROOT_NO_CRS)
  82. pci_use_crs = false;
  83. else if (pci_probe & PCI_USE__CRS)
  84. pci_use_crs = true;
  85. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  86. "if necessary, use \"pci=%s\" and report a bug\n",
  87. pci_use_crs ? "Using" : "Ignoring",
  88. pci_use_crs ? "nocrs" : "use_crs");
  89. }
  90. static acpi_status
  91. resource_to_addr(struct acpi_resource *resource,
  92. struct acpi_resource_address64 *addr)
  93. {
  94. acpi_status status;
  95. struct acpi_resource_memory24 *memory24;
  96. struct acpi_resource_memory32 *memory32;
  97. struct acpi_resource_fixed_memory32 *fixed_memory32;
  98. memset(addr, 0, sizeof(*addr));
  99. switch (resource->type) {
  100. case ACPI_RESOURCE_TYPE_MEMORY24:
  101. memory24 = &resource->data.memory24;
  102. addr->resource_type = ACPI_MEMORY_RANGE;
  103. addr->minimum = memory24->minimum;
  104. addr->address_length = memory24->address_length;
  105. addr->maximum = addr->minimum + addr->address_length - 1;
  106. return AE_OK;
  107. case ACPI_RESOURCE_TYPE_MEMORY32:
  108. memory32 = &resource->data.memory32;
  109. addr->resource_type = ACPI_MEMORY_RANGE;
  110. addr->minimum = memory32->minimum;
  111. addr->address_length = memory32->address_length;
  112. addr->maximum = addr->minimum + addr->address_length - 1;
  113. return AE_OK;
  114. case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
  115. fixed_memory32 = &resource->data.fixed_memory32;
  116. addr->resource_type = ACPI_MEMORY_RANGE;
  117. addr->minimum = fixed_memory32->address;
  118. addr->address_length = fixed_memory32->address_length;
  119. addr->maximum = addr->minimum + addr->address_length - 1;
  120. return AE_OK;
  121. case ACPI_RESOURCE_TYPE_ADDRESS16:
  122. case ACPI_RESOURCE_TYPE_ADDRESS32:
  123. case ACPI_RESOURCE_TYPE_ADDRESS64:
  124. status = acpi_resource_to_address64(resource, addr);
  125. if (ACPI_SUCCESS(status) &&
  126. (addr->resource_type == ACPI_MEMORY_RANGE ||
  127. addr->resource_type == ACPI_IO_RANGE) &&
  128. addr->address_length > 0) {
  129. return AE_OK;
  130. }
  131. break;
  132. }
  133. return AE_ERROR;
  134. }
  135. static acpi_status
  136. count_resource(struct acpi_resource *acpi_res, void *data)
  137. {
  138. struct pci_root_info *info = data;
  139. struct acpi_resource_address64 addr;
  140. acpi_status status;
  141. status = resource_to_addr(acpi_res, &addr);
  142. if (ACPI_SUCCESS(status))
  143. info->res_num++;
  144. return AE_OK;
  145. }
  146. static acpi_status
  147. setup_resource(struct acpi_resource *acpi_res, void *data)
  148. {
  149. struct pci_root_info *info = data;
  150. struct resource *res;
  151. struct acpi_resource_address64 addr;
  152. acpi_status status;
  153. unsigned long flags;
  154. u64 start, end;
  155. status = resource_to_addr(acpi_res, &addr);
  156. if (!ACPI_SUCCESS(status))
  157. return AE_OK;
  158. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  159. flags = IORESOURCE_MEM;
  160. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  161. flags |= IORESOURCE_PREFETCH;
  162. } else if (addr.resource_type == ACPI_IO_RANGE) {
  163. flags = IORESOURCE_IO;
  164. } else
  165. return AE_OK;
  166. start = addr.minimum + addr.translation_offset;
  167. end = addr.maximum + addr.translation_offset;
  168. res = &info->res[info->res_num];
  169. res->name = info->name;
  170. res->flags = flags;
  171. res->start = start;
  172. res->end = end;
  173. res->child = NULL;
  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. info->res_num++;
  180. if (addr.translation_offset)
  181. dev_info(&info->bridge->dev, "host bridge window %pR "
  182. "(PCI address [%#llx-%#llx])\n",
  183. res, res->start - addr.translation_offset,
  184. res->end - addr.translation_offset);
  185. else
  186. dev_info(&info->bridge->dev, "host bridge window %pR\n", res);
  187. return AE_OK;
  188. }
  189. static bool resource_contains(struct resource *res, resource_size_t point)
  190. {
  191. if (res->start <= point && point <= res->end)
  192. return true;
  193. return false;
  194. }
  195. static void coalesce_windows(struct pci_root_info *info, unsigned long type)
  196. {
  197. int i, j;
  198. struct resource *res1, *res2;
  199. for (i = 0; i < info->res_num; i++) {
  200. res1 = &info->res[i];
  201. if (!(res1->flags & type))
  202. continue;
  203. for (j = i + 1; j < info->res_num; j++) {
  204. res2 = &info->res[j];
  205. if (!(res2->flags & type))
  206. continue;
  207. /*
  208. * I don't like throwing away windows because then
  209. * our resources no longer match the ACPI _CRS, but
  210. * the kernel resource tree doesn't allow overlaps.
  211. */
  212. if (resource_contains(res1, res2->start) ||
  213. resource_contains(res1, res2->end) ||
  214. resource_contains(res2, res1->start) ||
  215. resource_contains(res2, res1->end)) {
  216. res1->start = min(res1->start, res2->start);
  217. res1->end = max(res1->end, res2->end);
  218. dev_info(&info->bridge->dev,
  219. "host bridge window expanded to %pR; %pR ignored\n",
  220. res1, res2);
  221. res2->flags = 0;
  222. }
  223. }
  224. }
  225. }
  226. static void add_resources(struct pci_root_info *info)
  227. {
  228. int i;
  229. struct resource *res, *root, *conflict;
  230. if (!pci_use_crs)
  231. return;
  232. coalesce_windows(info, IORESOURCE_MEM);
  233. coalesce_windows(info, IORESOURCE_IO);
  234. for (i = 0; i < info->res_num; i++) {
  235. res = &info->res[i];
  236. if (res->flags & IORESOURCE_MEM)
  237. root = &iomem_resource;
  238. else if (res->flags & IORESOURCE_IO)
  239. root = &ioport_resource;
  240. else
  241. continue;
  242. conflict = insert_resource_conflict(root, res);
  243. if (conflict)
  244. dev_info(&info->bridge->dev,
  245. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  246. res, conflict->name, conflict);
  247. else
  248. pci_bus_add_resource(info->bus, res, 0);
  249. }
  250. }
  251. static void
  252. get_current_resources(struct acpi_device *device, int busnum,
  253. int domain, struct pci_bus *bus)
  254. {
  255. struct pci_root_info info;
  256. size_t size;
  257. if (pci_use_crs)
  258. pci_bus_remove_resources(bus);
  259. info.bridge = device;
  260. info.bus = bus;
  261. info.res_num = 0;
  262. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  263. &info);
  264. if (!info.res_num)
  265. return;
  266. size = sizeof(*info.res) * info.res_num;
  267. info.res = kmalloc(size, GFP_KERNEL);
  268. if (!info.res)
  269. goto res_alloc_fail;
  270. info.name = kasprintf(GFP_KERNEL, "PCI Bus %04x:%02x", domain, busnum);
  271. if (!info.name)
  272. goto name_alloc_fail;
  273. info.res_num = 0;
  274. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  275. &info);
  276. add_resources(&info);
  277. return;
  278. name_alloc_fail:
  279. kfree(info.res);
  280. res_alloc_fail:
  281. return;
  282. }
  283. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_pci_root *root)
  284. {
  285. struct acpi_device *device = root->device;
  286. int domain = root->segment;
  287. int busnum = root->secondary.start;
  288. struct pci_bus *bus;
  289. struct pci_sysdata *sd;
  290. int node;
  291. #ifdef CONFIG_ACPI_NUMA
  292. int pxm;
  293. #endif
  294. if (domain && !pci_domains_supported) {
  295. printk(KERN_WARNING "pci_bus %04x:%02x: "
  296. "ignored (multiple domains not supported)\n",
  297. domain, busnum);
  298. return NULL;
  299. }
  300. node = -1;
  301. #ifdef CONFIG_ACPI_NUMA
  302. pxm = acpi_get_pxm(device->handle);
  303. if (pxm >= 0)
  304. node = pxm_to_node(pxm);
  305. if (node != -1)
  306. set_mp_bus_to_node(busnum, node);
  307. else
  308. #endif
  309. node = get_mp_bus_to_node(busnum);
  310. if (node != -1 && !node_online(node))
  311. node = -1;
  312. /* Allocate per-root-bus (not per bus) arch-specific data.
  313. * TODO: leak; this memory is never freed.
  314. * It's arguable whether it's worth the trouble to care.
  315. */
  316. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  317. if (!sd) {
  318. printk(KERN_WARNING "pci_bus %04x:%02x: "
  319. "ignored (out of memory)\n", domain, busnum);
  320. return NULL;
  321. }
  322. sd->domain = domain;
  323. sd->node = node;
  324. /*
  325. * Maybe the desired pci bus has been already scanned. In such case
  326. * it is unnecessary to scan the pci bus with the given domain,busnum.
  327. */
  328. bus = pci_find_bus(domain, busnum);
  329. if (bus) {
  330. /*
  331. * If the desired bus exits, the content of bus->sysdata will
  332. * be replaced by sd.
  333. */
  334. memcpy(bus->sysdata, sd, sizeof(*sd));
  335. kfree(sd);
  336. } else {
  337. bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd);
  338. if (bus) {
  339. get_current_resources(device, busnum, domain, bus);
  340. bus->subordinate = pci_scan_child_bus(bus);
  341. }
  342. }
  343. /* After the PCI-E bus has been walked and all devices discovered,
  344. * configure any settings of the fabric that might be necessary.
  345. */
  346. if (bus) {
  347. struct pci_bus *child;
  348. list_for_each_entry(child, &bus->children, node) {
  349. struct pci_dev *self = child->self;
  350. if (!self)
  351. continue;
  352. pcie_bus_configure_settings(child, self->pcie_mpss);
  353. }
  354. }
  355. if (!bus)
  356. kfree(sd);
  357. if (bus && node != -1) {
  358. #ifdef CONFIG_ACPI_NUMA
  359. if (pxm >= 0)
  360. dev_printk(KERN_DEBUG, &bus->dev,
  361. "on NUMA node %d (pxm %d)\n", node, pxm);
  362. #else
  363. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  364. #endif
  365. }
  366. return bus;
  367. }
  368. int __init pci_acpi_init(void)
  369. {
  370. struct pci_dev *dev = NULL;
  371. if (acpi_noirq)
  372. return -ENODEV;
  373. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  374. acpi_irq_penalty_init();
  375. pcibios_enable_irq = acpi_pci_irq_enable;
  376. pcibios_disable_irq = acpi_pci_irq_disable;
  377. x86_init.pci.init_irq = x86_init_noop;
  378. if (pci_routeirq) {
  379. /*
  380. * PCI IRQ routing is set up by pci_enable_device(), but we
  381. * also do it here in case there are still broken drivers that
  382. * don't use pci_enable_device().
  383. */
  384. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  385. for_each_pci_dev(dev)
  386. acpi_pci_irq_enable(dev);
  387. }
  388. return 0;
  389. }