acpi.c 14 KB

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