acpi.c 15 KB

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