acpi.c 14 KB

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