acpi.c 15 KB

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