quirks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This file contains quirk handling code for PnP devices
  4. * Some devices do not report all their resources, and need to have extra
  5. * resources added. This is most easily accomplished at initialisation time
  6. * when building up the resource structure for the first time.
  7. *
  8. * Copyright (c) 2000 Peter Denison <[email protected]>
  9. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  10. * Bjorn Helgaas <[email protected]>
  11. *
  12. * Heavily based on PCI quirks handling which is
  13. *
  14. * Copyright (c) 1999 Martin Mares <[email protected]>
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/pnp.h>
  22. #include <linux/io.h>
  23. #include "base.h"
  24. static void quirk_awe32_add_ports(struct pnp_dev *dev,
  25. struct pnp_option *option,
  26. unsigned int offset)
  27. {
  28. struct pnp_option *new_option;
  29. new_option = kmalloc(sizeof(struct pnp_option), GFP_KERNEL);
  30. if (!new_option) {
  31. dev_err(&dev->dev, "couldn't add ioport region to option set "
  32. "%d\n", pnp_option_set(option));
  33. return;
  34. }
  35. *new_option = *option;
  36. new_option->u.port.min += offset;
  37. new_option->u.port.max += offset;
  38. list_add(&new_option->list, &option->list);
  39. dev_info(&dev->dev, "added ioport region %#llx-%#llx to set %d\n",
  40. (unsigned long long) new_option->u.port.min,
  41. (unsigned long long) new_option->u.port.max,
  42. pnp_option_set(option));
  43. }
  44. static void quirk_awe32_resources(struct pnp_dev *dev)
  45. {
  46. struct pnp_option *option;
  47. unsigned int set = ~0;
  48. /*
  49. * Add two extra ioport regions (at offset 0x400 and 0x800 from the
  50. * one given) to every dependent option set.
  51. */
  52. list_for_each_entry(option, &dev->options, list) {
  53. if (pnp_option_is_dependent(option) &&
  54. pnp_option_set(option) != set) {
  55. set = pnp_option_set(option);
  56. quirk_awe32_add_ports(dev, option, 0x800);
  57. quirk_awe32_add_ports(dev, option, 0x400);
  58. }
  59. }
  60. }
  61. static void quirk_cmi8330_resources(struct pnp_dev *dev)
  62. {
  63. struct pnp_option *option;
  64. struct pnp_irq *irq;
  65. struct pnp_dma *dma;
  66. list_for_each_entry(option, &dev->options, list) {
  67. if (!pnp_option_is_dependent(option))
  68. continue;
  69. if (option->type == IORESOURCE_IRQ) {
  70. irq = &option->u.irq;
  71. bitmap_zero(irq->map.bits, PNP_IRQ_NR);
  72. __set_bit(5, irq->map.bits);
  73. __set_bit(7, irq->map.bits);
  74. __set_bit(10, irq->map.bits);
  75. dev_info(&dev->dev, "set possible IRQs in "
  76. "option set %d to 5, 7, 10\n",
  77. pnp_option_set(option));
  78. } else if (option->type == IORESOURCE_DMA) {
  79. dma = &option->u.dma;
  80. if ((dma->flags & IORESOURCE_DMA_TYPE_MASK) ==
  81. IORESOURCE_DMA_8BIT &&
  82. dma->map != 0x0A) {
  83. dev_info(&dev->dev, "changing possible "
  84. "DMA channel mask in option set %d "
  85. "from %#02x to 0x0A (1, 3)\n",
  86. pnp_option_set(option), dma->map);
  87. dma->map = 0x0A;
  88. }
  89. }
  90. }
  91. }
  92. static void quirk_sb16audio_resources(struct pnp_dev *dev)
  93. {
  94. struct pnp_option *option;
  95. unsigned int prev_option_flags = ~0, n = 0;
  96. struct pnp_port *port;
  97. /*
  98. * The default range on the OPL port for these devices is 0x388-0x388.
  99. * Here we increase that range so that two such cards can be
  100. * auto-configured.
  101. */
  102. list_for_each_entry(option, &dev->options, list) {
  103. if (prev_option_flags != option->flags) {
  104. prev_option_flags = option->flags;
  105. n = 0;
  106. }
  107. if (pnp_option_is_dependent(option) &&
  108. option->type == IORESOURCE_IO) {
  109. n++;
  110. port = &option->u.port;
  111. if (n == 3 && port->min == port->max) {
  112. port->max += 0x70;
  113. dev_info(&dev->dev, "increased option port "
  114. "range from %#llx-%#llx to "
  115. "%#llx-%#llx\n",
  116. (unsigned long long) port->min,
  117. (unsigned long long) port->min,
  118. (unsigned long long) port->min,
  119. (unsigned long long) port->max);
  120. }
  121. }
  122. }
  123. }
  124. static struct pnp_option *pnp_clone_dependent_set(struct pnp_dev *dev,
  125. unsigned int set)
  126. {
  127. struct pnp_option *tail = NULL, *first_new_option = NULL;
  128. struct pnp_option *option, *new_option;
  129. unsigned int flags;
  130. list_for_each_entry(option, &dev->options, list) {
  131. if (pnp_option_is_dependent(option))
  132. tail = option;
  133. }
  134. if (!tail) {
  135. dev_err(&dev->dev, "no dependent option sets\n");
  136. return NULL;
  137. }
  138. flags = pnp_new_dependent_set(dev, PNP_RES_PRIORITY_FUNCTIONAL);
  139. list_for_each_entry(option, &dev->options, list) {
  140. if (pnp_option_is_dependent(option) &&
  141. pnp_option_set(option) == set) {
  142. new_option = kmalloc(sizeof(struct pnp_option),
  143. GFP_KERNEL);
  144. if (!new_option) {
  145. dev_err(&dev->dev, "couldn't clone dependent "
  146. "set %d\n", set);
  147. return NULL;
  148. }
  149. *new_option = *option;
  150. new_option->flags = flags;
  151. if (!first_new_option)
  152. first_new_option = new_option;
  153. list_add(&new_option->list, &tail->list);
  154. tail = new_option;
  155. }
  156. }
  157. return first_new_option;
  158. }
  159. static void quirk_add_irq_optional_dependent_sets(struct pnp_dev *dev)
  160. {
  161. struct pnp_option *new_option;
  162. unsigned int num_sets, i, set;
  163. struct pnp_irq *irq;
  164. num_sets = dev->num_dependent_sets;
  165. for (i = 0; i < num_sets; i++) {
  166. new_option = pnp_clone_dependent_set(dev, i);
  167. if (!new_option)
  168. return;
  169. set = pnp_option_set(new_option);
  170. while (new_option && pnp_option_set(new_option) == set) {
  171. if (new_option->type == IORESOURCE_IRQ) {
  172. irq = &new_option->u.irq;
  173. irq->flags |= IORESOURCE_IRQ_OPTIONAL;
  174. }
  175. dbg_pnp_show_option(dev, new_option);
  176. new_option = list_entry(new_option->list.next,
  177. struct pnp_option, list);
  178. }
  179. dev_info(&dev->dev, "added dependent option set %d (same as "
  180. "set %d except IRQ optional)\n", set, i);
  181. }
  182. }
  183. static void quirk_ad1815_mpu_resources(struct pnp_dev *dev)
  184. {
  185. struct pnp_option *option;
  186. struct pnp_irq *irq = NULL;
  187. unsigned int independent_irqs = 0;
  188. list_for_each_entry(option, &dev->options, list) {
  189. if (option->type == IORESOURCE_IRQ &&
  190. !pnp_option_is_dependent(option)) {
  191. independent_irqs++;
  192. irq = &option->u.irq;
  193. }
  194. }
  195. if (independent_irqs != 1)
  196. return;
  197. irq->flags |= IORESOURCE_IRQ_OPTIONAL;
  198. dev_info(&dev->dev, "made independent IRQ optional\n");
  199. }
  200. static void quirk_system_pci_resources(struct pnp_dev *dev)
  201. {
  202. struct pci_dev *pdev = NULL;
  203. struct resource *res;
  204. resource_size_t pnp_start, pnp_end, pci_start, pci_end;
  205. int i, j;
  206. /*
  207. * Some BIOSes have PNP motherboard devices with resources that
  208. * partially overlap PCI BARs. The PNP system driver claims these
  209. * motherboard resources, which prevents the normal PCI driver from
  210. * requesting them later.
  211. *
  212. * This patch disables the PNP resources that conflict with PCI BARs
  213. * so they won't be claimed by the PNP system driver.
  214. */
  215. for_each_pci_dev(pdev) {
  216. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  217. unsigned long flags, type;
  218. flags = pci_resource_flags(pdev, i);
  219. type = flags & (IORESOURCE_IO | IORESOURCE_MEM);
  220. if (!type || pci_resource_len(pdev, i) == 0)
  221. continue;
  222. if (flags & IORESOURCE_UNSET)
  223. continue;
  224. pci_start = pci_resource_start(pdev, i);
  225. pci_end = pci_resource_end(pdev, i);
  226. for (j = 0;
  227. (res = pnp_get_resource(dev, type, j)); j++) {
  228. if (res->start == 0 && res->end == 0)
  229. continue;
  230. pnp_start = res->start;
  231. pnp_end = res->end;
  232. /*
  233. * If the PNP region doesn't overlap the PCI
  234. * region at all, there's no problem.
  235. */
  236. if (pnp_end < pci_start || pnp_start > pci_end)
  237. continue;
  238. /*
  239. * If the PNP region completely encloses (or is
  240. * at least as large as) the PCI region, that's
  241. * also OK. For example, this happens when the
  242. * PNP device describes a bridge with PCI
  243. * behind it.
  244. */
  245. if (pnp_start <= pci_start &&
  246. pnp_end >= pci_end)
  247. continue;
  248. /*
  249. * Otherwise, the PNP region overlaps *part* of
  250. * the PCI region, and that might prevent a PCI
  251. * driver from requesting its resources.
  252. */
  253. dev_warn(&dev->dev,
  254. "disabling %pR because it overlaps "
  255. "%s BAR %d %pR\n", res,
  256. pci_name(pdev), i, &pdev->resource[i]);
  257. res->flags |= IORESOURCE_DISABLED;
  258. }
  259. }
  260. }
  261. }
  262. #ifdef CONFIG_AMD_NB
  263. #include <asm/amd_nb.h>
  264. static void quirk_amd_mmconfig_area(struct pnp_dev *dev)
  265. {
  266. resource_size_t start, end;
  267. struct pnp_resource *pnp_res;
  268. struct resource *res;
  269. struct resource mmconfig_res, *mmconfig;
  270. mmconfig = amd_get_mmconfig_range(&mmconfig_res);
  271. if (!mmconfig)
  272. return;
  273. list_for_each_entry(pnp_res, &dev->resources, list) {
  274. res = &pnp_res->res;
  275. if (res->end < mmconfig->start || res->start > mmconfig->end ||
  276. (res->start == mmconfig->start && res->end == mmconfig->end))
  277. continue;
  278. dev_info(&dev->dev, FW_BUG
  279. "%pR covers only part of AMD MMCONFIG area %pR; adding more reservations\n",
  280. res, mmconfig);
  281. if (mmconfig->start < res->start) {
  282. start = mmconfig->start;
  283. end = res->start - 1;
  284. pnp_add_mem_resource(dev, start, end, 0);
  285. }
  286. if (mmconfig->end > res->end) {
  287. start = res->end + 1;
  288. end = mmconfig->end;
  289. pnp_add_mem_resource(dev, start, end, 0);
  290. }
  291. break;
  292. }
  293. }
  294. #endif
  295. #ifdef CONFIG_PCI
  296. /* Device IDs of parts that have 32KB MCH space */
  297. static const unsigned int mch_quirk_devices[] = {
  298. 0x0154, /* Ivy Bridge */
  299. 0x0a04, /* Haswell-ULT */
  300. 0x0c00, /* Haswell */
  301. 0x1604, /* Broadwell */
  302. };
  303. static struct pci_dev *get_intel_host(void)
  304. {
  305. int i;
  306. struct pci_dev *host;
  307. for (i = 0; i < ARRAY_SIZE(mch_quirk_devices); i++) {
  308. host = pci_get_device(PCI_VENDOR_ID_INTEL, mch_quirk_devices[i],
  309. NULL);
  310. if (host)
  311. return host;
  312. }
  313. return NULL;
  314. }
  315. static void quirk_intel_mch(struct pnp_dev *dev)
  316. {
  317. struct pci_dev *host;
  318. u32 addr_lo, addr_hi;
  319. struct pci_bus_region region;
  320. struct resource mch;
  321. struct pnp_resource *pnp_res;
  322. struct resource *res;
  323. host = get_intel_host();
  324. if (!host)
  325. return;
  326. /*
  327. * MCHBAR is not an architected PCI BAR, so MCH space is usually
  328. * reported as a PNP0C02 resource. The MCH space was originally
  329. * 16KB, but is 32KB in newer parts. Some BIOSes still report a
  330. * PNP0C02 resource that is only 16KB, which means the rest of the
  331. * MCH space is consumed but unreported.
  332. */
  333. /*
  334. * Read MCHBAR for Host Member Mapped Register Range Base
  335. * https://www-ssl.intel.com/content/www/us/en/processors/core/4th-gen-core-family-desktop-vol-2-datasheet
  336. * Sec 3.1.12.
  337. */
  338. pci_read_config_dword(host, 0x48, &addr_lo);
  339. region.start = addr_lo & ~0x7fff;
  340. pci_read_config_dword(host, 0x4c, &addr_hi);
  341. region.start |= (u64) addr_hi << 32;
  342. region.end = region.start + 32*1024 - 1;
  343. memset(&mch, 0, sizeof(mch));
  344. mch.flags = IORESOURCE_MEM;
  345. pcibios_bus_to_resource(host->bus, &mch, &region);
  346. list_for_each_entry(pnp_res, &dev->resources, list) {
  347. res = &pnp_res->res;
  348. if (res->end < mch.start || res->start > mch.end)
  349. continue; /* no overlap */
  350. if (res->start == mch.start && res->end == mch.end)
  351. continue; /* exact match */
  352. dev_info(&dev->dev, FW_BUG "PNP resource %pR covers only part of %s Intel MCH; extending to %pR\n",
  353. res, pci_name(host), &mch);
  354. res->start = mch.start;
  355. res->end = mch.end;
  356. break;
  357. }
  358. pci_dev_put(host);
  359. }
  360. #endif
  361. /*
  362. * PnP Quirks
  363. * Cards or devices that need some tweaking due to incomplete resource info
  364. */
  365. static struct pnp_fixup pnp_fixups[] = {
  366. /* Soundblaster awe io port quirk */
  367. {"CTL0021", quirk_awe32_resources},
  368. {"CTL0022", quirk_awe32_resources},
  369. {"CTL0023", quirk_awe32_resources},
  370. /* CMI 8330 interrupt and dma fix */
  371. {"@X@0001", quirk_cmi8330_resources},
  372. /* Soundblaster audio device io port range quirk */
  373. {"CTL0001", quirk_sb16audio_resources},
  374. {"CTL0031", quirk_sb16audio_resources},
  375. {"CTL0041", quirk_sb16audio_resources},
  376. {"CTL0042", quirk_sb16audio_resources},
  377. {"CTL0043", quirk_sb16audio_resources},
  378. {"CTL0044", quirk_sb16audio_resources},
  379. {"CTL0045", quirk_sb16audio_resources},
  380. /* Add IRQ-optional MPU options */
  381. {"ADS7151", quirk_ad1815_mpu_resources},
  382. {"ADS7181", quirk_add_irq_optional_dependent_sets},
  383. {"AZT0002", quirk_add_irq_optional_dependent_sets},
  384. /* PnP resources that might overlap PCI BARs */
  385. {"PNP0c01", quirk_system_pci_resources},
  386. {"PNP0c02", quirk_system_pci_resources},
  387. #ifdef CONFIG_AMD_NB
  388. {"PNP0c01", quirk_amd_mmconfig_area},
  389. #endif
  390. #ifdef CONFIG_PCI
  391. {"PNP0c02", quirk_intel_mch},
  392. #endif
  393. {""}
  394. };
  395. void pnp_fixup_device(struct pnp_dev *dev)
  396. {
  397. struct pnp_fixup *f;
  398. for (f = pnp_fixups; *f->id; f++) {
  399. if (!compare_pnp_id(dev->id, f->id))
  400. continue;
  401. pnp_dbg(&dev->dev, "%s: calling %pS\n", f->id,
  402. f->quirk_function);
  403. f->quirk_function(dev);
  404. }
  405. }