acpi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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[16];
  12. struct pci_sysdata sd;
  13. #ifdef CONFIG_PCI_MMCONFIG
  14. bool mcfg_added;
  15. u16 segment;
  16. u8 start_bus;
  17. u8 end_bus;
  18. #endif
  19. };
  20. static bool pci_use_crs = true;
  21. static bool pci_ignore_seg = false;
  22. static int __init set_use_crs(const struct dmi_system_id *id)
  23. {
  24. pci_use_crs = true;
  25. return 0;
  26. }
  27. static int __init set_nouse_crs(const struct dmi_system_id *id)
  28. {
  29. pci_use_crs = false;
  30. return 0;
  31. }
  32. static int __init set_ignore_seg(const struct dmi_system_id *id)
  33. {
  34. printk(KERN_INFO "PCI: %s detected: ignoring ACPI _SEG\n", id->ident);
  35. pci_ignore_seg = true;
  36. return 0;
  37. }
  38. static const struct dmi_system_id pci_crs_quirks[] __initconst = {
  39. /* http://bugzilla.kernel.org/show_bug.cgi?id=14183 */
  40. {
  41. .callback = set_use_crs,
  42. .ident = "IBM System x3800",
  43. .matches = {
  44. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  45. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  46. },
  47. },
  48. /* https://bugzilla.kernel.org/show_bug.cgi?id=16007 */
  49. /* 2006 AMD HT/VIA system with two host bridges */
  50. {
  51. .callback = set_use_crs,
  52. .ident = "ASRock ALiveSATA2-GLAN",
  53. .matches = {
  54. DMI_MATCH(DMI_PRODUCT_NAME, "ALiveSATA2-GLAN"),
  55. },
  56. },
  57. /* https://bugzilla.kernel.org/show_bug.cgi?id=30552 */
  58. /* 2006 AMD HT/VIA system with two host bridges */
  59. {
  60. .callback = set_use_crs,
  61. .ident = "ASUS M2V-MX SE",
  62. .matches = {
  63. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  64. DMI_MATCH(DMI_BOARD_NAME, "M2V-MX SE"),
  65. DMI_MATCH(DMI_BIOS_VENDOR, "American Megatrends Inc."),
  66. },
  67. },
  68. /* https://bugzilla.kernel.org/show_bug.cgi?id=42619 */
  69. {
  70. .callback = set_use_crs,
  71. .ident = "MSI MS-7253",
  72. .matches = {
  73. DMI_MATCH(DMI_BOARD_VENDOR, "MICRO-STAR INTERNATIONAL CO., LTD"),
  74. DMI_MATCH(DMI_BOARD_NAME, "MS-7253"),
  75. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  76. },
  77. },
  78. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/931368 */
  79. /* https://bugs.launchpad.net/ubuntu/+source/alsa-driver/+bug/1033299 */
  80. {
  81. .callback = set_use_crs,
  82. .ident = "Foxconn K8M890-8237A",
  83. .matches = {
  84. DMI_MATCH(DMI_BOARD_VENDOR, "Foxconn"),
  85. DMI_MATCH(DMI_BOARD_NAME, "K8M890-8237A"),
  86. DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies, LTD"),
  87. },
  88. },
  89. /* Now for the blacklist.. */
  90. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  91. {
  92. .callback = set_nouse_crs,
  93. .ident = "Dell Studio 1557",
  94. .matches = {
  95. DMI_MATCH(DMI_BOARD_VENDOR, "Dell Inc."),
  96. DMI_MATCH(DMI_PRODUCT_NAME, "Studio 1557"),
  97. DMI_MATCH(DMI_BIOS_VERSION, "A09"),
  98. },
  99. },
  100. /* https://bugzilla.redhat.com/show_bug.cgi?id=769657 */
  101. {
  102. .callback = set_nouse_crs,
  103. .ident = "Thinkpad SL510",
  104. .matches = {
  105. DMI_MATCH(DMI_BOARD_VENDOR, "LENOVO"),
  106. DMI_MATCH(DMI_BOARD_NAME, "2847DFG"),
  107. DMI_MATCH(DMI_BIOS_VERSION, "6JET85WW (1.43 )"),
  108. },
  109. },
  110. /* https://bugzilla.kernel.org/show_bug.cgi?id=15362 */
  111. {
  112. .callback = set_ignore_seg,
  113. .ident = "HP xw9300",
  114. .matches = {
  115. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  116. DMI_MATCH(DMI_PRODUCT_NAME, "HP xw9300 Workstation"),
  117. },
  118. },
  119. {}
  120. };
  121. void __init pci_acpi_crs_quirks(void)
  122. {
  123. int year;
  124. if (dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL) && year < 2008) {
  125. if (iomem_resource.end <= 0xffffffff)
  126. pci_use_crs = false;
  127. }
  128. dmi_check_system(pci_crs_quirks);
  129. /*
  130. * If the user specifies "pci=use_crs" or "pci=nocrs" explicitly, that
  131. * takes precedence over anything we figured out above.
  132. */
  133. if (pci_probe & PCI_ROOT_NO_CRS)
  134. pci_use_crs = false;
  135. else if (pci_probe & PCI_USE__CRS)
  136. pci_use_crs = true;
  137. printk(KERN_INFO "PCI: %s host bridge windows from ACPI; "
  138. "if necessary, use \"pci=%s\" and report a bug\n",
  139. pci_use_crs ? "Using" : "Ignoring",
  140. pci_use_crs ? "nocrs" : "use_crs");
  141. }
  142. #ifdef CONFIG_PCI_MMCONFIG
  143. static int check_segment(u16 seg, struct device *dev, char *estr)
  144. {
  145. if (seg) {
  146. dev_err(dev,
  147. "%s can't access PCI configuration "
  148. "space under this host bridge.\n",
  149. estr);
  150. return -EIO;
  151. }
  152. /*
  153. * Failure in adding MMCFG information is not fatal,
  154. * just can't access extended configuration space of
  155. * devices under this host bridge.
  156. */
  157. dev_warn(dev,
  158. "%s can't access extended PCI configuration "
  159. "space under this bridge.\n",
  160. estr);
  161. return 0;
  162. }
  163. static int setup_mcfg_map(struct pci_root_info *info, u16 seg, u8 start,
  164. u8 end, phys_addr_t addr)
  165. {
  166. int result;
  167. struct device *dev = &info->bridge->dev;
  168. info->start_bus = start;
  169. info->end_bus = end;
  170. info->mcfg_added = false;
  171. /* return success if MMCFG is not in use */
  172. if (raw_pci_ext_ops && raw_pci_ext_ops != &pci_mmcfg)
  173. return 0;
  174. if (!(pci_probe & PCI_PROBE_MMCONF))
  175. return check_segment(seg, dev, "MMCONFIG is disabled,");
  176. result = pci_mmconfig_insert(dev, seg, start, end, addr);
  177. if (result == 0) {
  178. /* enable MMCFG if it hasn't been enabled yet */
  179. if (raw_pci_ext_ops == NULL)
  180. raw_pci_ext_ops = &pci_mmcfg;
  181. info->mcfg_added = true;
  182. } else if (result != -EEXIST)
  183. return check_segment(seg, dev,
  184. "fail to add MMCONFIG information,");
  185. return 0;
  186. }
  187. static void teardown_mcfg_map(struct pci_root_info *info)
  188. {
  189. if (info->mcfg_added) {
  190. pci_mmconfig_delete(info->segment, info->start_bus,
  191. info->end_bus);
  192. info->mcfg_added = false;
  193. }
  194. }
  195. #else
  196. static int setup_mcfg_map(struct pci_root_info *info,
  197. u16 seg, u8 start, u8 end,
  198. phys_addr_t addr)
  199. {
  200. return 0;
  201. }
  202. static void teardown_mcfg_map(struct pci_root_info *info)
  203. {
  204. }
  205. #endif
  206. static void validate_resources(struct device *dev, struct list_head *crs_res,
  207. unsigned long type)
  208. {
  209. LIST_HEAD(list);
  210. struct resource *res1, *res2, *root = NULL;
  211. struct resource_entry *tmp, *entry, *entry2;
  212. BUG_ON((type & (IORESOURCE_MEM | IORESOURCE_IO)) == 0);
  213. root = (type & IORESOURCE_MEM) ? &iomem_resource : &ioport_resource;
  214. list_splice_init(crs_res, &list);
  215. resource_list_for_each_entry_safe(entry, tmp, &list) {
  216. bool free = false;
  217. resource_size_t end;
  218. res1 = entry->res;
  219. if (!(res1->flags & type))
  220. goto next;
  221. /* Exclude non-addressable range or non-addressable portion */
  222. end = min(res1->end, root->end);
  223. if (end <= res1->start) {
  224. dev_info(dev, "host bridge window %pR (ignored, not CPU addressable)\n",
  225. res1);
  226. free = true;
  227. goto next;
  228. } else if (res1->end != end) {
  229. dev_info(dev, "host bridge window %pR ([%#llx-%#llx] ignored, not CPU addressable)\n",
  230. res1, (unsigned long long)end + 1,
  231. (unsigned long long)res1->end);
  232. res1->end = end;
  233. }
  234. resource_list_for_each_entry(entry2, crs_res) {
  235. res2 = entry2->res;
  236. if (!(res2->flags & type))
  237. continue;
  238. /*
  239. * I don't like throwing away windows because then
  240. * our resources no longer match the ACPI _CRS, but
  241. * the kernel resource tree doesn't allow overlaps.
  242. */
  243. if (resource_overlaps(res1, res2)) {
  244. res2->start = min(res1->start, res2->start);
  245. res2->end = max(res1->end, res2->end);
  246. dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
  247. res2, res1);
  248. free = true;
  249. goto next;
  250. }
  251. }
  252. next:
  253. resource_list_del(entry);
  254. if (free)
  255. resource_list_free_entry(entry);
  256. else
  257. resource_list_add_tail(entry, crs_res);
  258. }
  259. }
  260. static void add_resources(struct pci_root_info *info,
  261. struct list_head *resources,
  262. struct list_head *crs_res)
  263. {
  264. struct resource_entry *entry, *tmp;
  265. struct resource *res, *conflict, *root = NULL;
  266. validate_resources(&info->bridge->dev, crs_res, IORESOURCE_MEM);
  267. validate_resources(&info->bridge->dev, crs_res, IORESOURCE_IO);
  268. resource_list_for_each_entry_safe(entry, tmp, crs_res) {
  269. res = entry->res;
  270. if (res->flags & IORESOURCE_MEM)
  271. root = &iomem_resource;
  272. else if (res->flags & IORESOURCE_IO)
  273. root = &ioport_resource;
  274. else
  275. BUG_ON(res);
  276. conflict = insert_resource_conflict(root, res);
  277. if (conflict) {
  278. dev_info(&info->bridge->dev,
  279. "ignoring host bridge window %pR (conflicts with %s %pR)\n",
  280. res, conflict->name, conflict);
  281. resource_list_destroy_entry(entry);
  282. }
  283. }
  284. list_splice_tail(crs_res, resources);
  285. }
  286. static void release_pci_root_info(struct pci_host_bridge *bridge)
  287. {
  288. struct resource *res;
  289. struct resource_entry *entry;
  290. struct pci_root_info *info = bridge->release_data;
  291. resource_list_for_each_entry(entry, &bridge->windows) {
  292. res = entry->res;
  293. if (res->parent &&
  294. (res->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
  295. release_resource(res);
  296. }
  297. teardown_mcfg_map(info);
  298. kfree(info);
  299. }
  300. /*
  301. * An IO port or MMIO resource assigned to a PCI host bridge may be
  302. * consumed by the host bridge itself or available to its child
  303. * bus/devices. The ACPI specification defines a bit (Producer/Consumer)
  304. * to tell whether the resource is consumed by the host bridge itself,
  305. * but firmware hasn't used that bit consistently, so we can't rely on it.
  306. *
  307. * On x86 and IA64 platforms, all IO port and MMIO resources are assumed
  308. * to be available to child bus/devices except one special case:
  309. * IO port [0xCF8-0xCFF] is consumed by the host bridge itself
  310. * to access PCI configuration space.
  311. *
  312. * So explicitly filter out PCI CFG IO ports[0xCF8-0xCFF].
  313. */
  314. static bool resource_is_pcicfg_ioport(struct resource *res)
  315. {
  316. return (res->flags & IORESOURCE_IO) &&
  317. res->start == 0xCF8 && res->end == 0xCFF;
  318. }
  319. static void probe_pci_root_info(struct pci_root_info *info,
  320. struct acpi_device *device,
  321. int busnum, int domain,
  322. struct list_head *list)
  323. {
  324. int ret;
  325. struct resource_entry *entry, *tmp;
  326. sprintf(info->name, "PCI Bus %04x:%02x", domain, busnum);
  327. info->bridge = device;
  328. ret = acpi_dev_get_resources(device, list,
  329. acpi_dev_filter_resource_type_cb,
  330. (void *)(IORESOURCE_IO | IORESOURCE_MEM));
  331. if (ret < 0)
  332. dev_warn(&device->dev,
  333. "failed to parse _CRS method, error code %d\n", ret);
  334. else if (ret == 0)
  335. dev_dbg(&device->dev,
  336. "no IO and memory resources present in _CRS\n");
  337. else
  338. resource_list_for_each_entry_safe(entry, tmp, list) {
  339. if ((entry->res->flags & IORESOURCE_DISABLED) ||
  340. resource_is_pcicfg_ioport(entry->res))
  341. resource_list_destroy_entry(entry);
  342. else
  343. entry->res->name = info->name;
  344. }
  345. }
  346. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  347. {
  348. struct acpi_device *device = root->device;
  349. struct pci_root_info *info;
  350. int domain = root->segment;
  351. int busnum = root->secondary.start;
  352. struct resource_entry *res_entry;
  353. LIST_HEAD(crs_res);
  354. LIST_HEAD(resources);
  355. struct pci_bus *bus;
  356. struct pci_sysdata *sd;
  357. int node;
  358. if (pci_ignore_seg)
  359. domain = 0;
  360. if (domain && !pci_domains_supported) {
  361. printk(KERN_WARNING "pci_bus %04x:%02x: "
  362. "ignored (multiple domains not supported)\n",
  363. domain, busnum);
  364. return NULL;
  365. }
  366. node = acpi_get_node(device->handle);
  367. if (node == NUMA_NO_NODE) {
  368. node = x86_pci_root_bus_node(busnum);
  369. if (node != 0 && node != NUMA_NO_NODE)
  370. dev_info(&device->dev, FW_BUG "no _PXM; falling back to node %d from hardware (may be inconsistent with ACPI node numbers)\n",
  371. node);
  372. }
  373. if (node != NUMA_NO_NODE && !node_online(node))
  374. node = NUMA_NO_NODE;
  375. info = kzalloc_node(sizeof(*info), GFP_KERNEL, node);
  376. if (!info) {
  377. printk(KERN_WARNING "pci_bus %04x:%02x: "
  378. "ignored (out of memory)\n", domain, busnum);
  379. return NULL;
  380. }
  381. sd = &info->sd;
  382. sd->domain = domain;
  383. sd->node = node;
  384. sd->companion = device;
  385. bus = pci_find_bus(domain, busnum);
  386. if (bus) {
  387. /*
  388. * If the desired bus has been scanned already, replace
  389. * its bus->sysdata.
  390. */
  391. memcpy(bus->sysdata, sd, sizeof(*sd));
  392. kfree(info);
  393. } else {
  394. /* insert busn res at first */
  395. pci_add_resource(&resources, &root->secondary);
  396. /*
  397. * _CRS with no apertures is normal, so only fall back to
  398. * defaults or native bridge info if we're ignoring _CRS.
  399. */
  400. probe_pci_root_info(info, device, busnum, domain, &crs_res);
  401. if (pci_use_crs) {
  402. add_resources(info, &resources, &crs_res);
  403. } else {
  404. resource_list_for_each_entry(res_entry, &crs_res)
  405. dev_printk(KERN_DEBUG, &device->dev,
  406. "host bridge window %pR (ignored)\n",
  407. res_entry->res);
  408. resource_list_free(&crs_res);
  409. x86_pci_root_bus_resources(busnum, &resources);
  410. }
  411. if (!setup_mcfg_map(info, domain, (u8)root->secondary.start,
  412. (u8)root->secondary.end, root->mcfg_addr))
  413. bus = pci_create_root_bus(NULL, busnum, &pci_root_ops,
  414. sd, &resources);
  415. if (bus) {
  416. pci_scan_child_bus(bus);
  417. pci_set_host_bridge_release(
  418. to_pci_host_bridge(bus->bridge),
  419. release_pci_root_info, info);
  420. } else {
  421. resource_list_free(&resources);
  422. teardown_mcfg_map(info);
  423. kfree(info);
  424. }
  425. }
  426. /* After the PCI-E bus has been walked and all devices discovered,
  427. * configure any settings of the fabric that might be necessary.
  428. */
  429. if (bus) {
  430. struct pci_bus *child;
  431. list_for_each_entry(child, &bus->children, node)
  432. pcie_bus_configure_settings(child);
  433. }
  434. if (bus && node != NUMA_NO_NODE)
  435. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  436. return bus;
  437. }
  438. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  439. {
  440. /*
  441. * We pass NULL as parent to pci_create_root_bus(), so if it is not NULL
  442. * here, pci_create_root_bus() has been called by someone else and
  443. * sysdata is likely to be different from what we expect. Let it go in
  444. * that case.
  445. */
  446. if (!bridge->dev.parent) {
  447. struct pci_sysdata *sd = bridge->bus->sysdata;
  448. ACPI_COMPANION_SET(&bridge->dev, sd->companion);
  449. }
  450. return 0;
  451. }
  452. int __init pci_acpi_init(void)
  453. {
  454. struct pci_dev *dev = NULL;
  455. if (acpi_noirq)
  456. return -ENODEV;
  457. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  458. acpi_irq_penalty_init();
  459. pcibios_enable_irq = acpi_pci_irq_enable;
  460. pcibios_disable_irq = acpi_pci_irq_disable;
  461. x86_init.pci.init_irq = x86_init_noop;
  462. if (pci_routeirq) {
  463. /*
  464. * PCI IRQ routing is set up by pci_enable_device(), but we
  465. * also do it here in case there are still broken drivers that
  466. * don't use pci_enable_device().
  467. */
  468. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  469. for_each_pci_dev(dev)
  470. acpi_pci_irq_enable(dev);
  471. }
  472. return 0;
  473. }