quirks.c 12 KB

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