quirks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 "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, *r;
  204. int i, j;
  205. /*
  206. * Some BIOSes have PNP motherboard devices with resources that
  207. * partially overlap PCI BARs. The PNP system driver claims these
  208. * motherboard resources, which prevents the normal PCI driver from
  209. * requesting them later.
  210. *
  211. * This patch disables the PNP resources that conflict with PCI BARs
  212. * so they won't be claimed by the PNP system driver.
  213. */
  214. for_each_pci_dev(pdev) {
  215. pci_dev_for_each_resource(pdev, r, i) {
  216. unsigned long type = resource_type(r);
  217. if (!(type == IORESOURCE_IO || type == IORESOURCE_MEM) ||
  218. resource_size(r) == 0)
  219. continue;
  220. if (r->flags & IORESOURCE_UNSET)
  221. continue;
  222. for (j = 0;
  223. (res = pnp_get_resource(dev, type, j)); j++) {
  224. if (res->start == 0 && res->end == 0)
  225. continue;
  226. /*
  227. * If the PNP region doesn't overlap the PCI
  228. * region at all, there's no problem.
  229. */
  230. if (!resource_overlaps(res, r))
  231. continue;
  232. /*
  233. * If the PNP region completely encloses (or is
  234. * at least as large as) the PCI region, that's
  235. * also OK. For example, this happens when the
  236. * PNP device describes a bridge with PCI
  237. * behind it.
  238. */
  239. if (res->start <= r->start && res->end >= r->end)
  240. continue;
  241. /*
  242. * Otherwise, the PNP region overlaps *part* of
  243. * the PCI region, and that might prevent a PCI
  244. * driver from requesting its resources.
  245. */
  246. dev_warn(&dev->dev,
  247. "disabling %pR because it overlaps %s BAR %d %pR\n",
  248. res, pci_name(pdev), i, r);
  249. res->flags |= IORESOURCE_DISABLED;
  250. }
  251. }
  252. }
  253. }
  254. #ifdef CONFIG_AMD_NB
  255. #include <asm/amd_nb.h>
  256. static void quirk_amd_mmconfig_area(struct pnp_dev *dev)
  257. {
  258. resource_size_t start, end;
  259. struct pnp_resource *pnp_res;
  260. struct resource *res;
  261. struct resource mmconfig_res, *mmconfig;
  262. mmconfig = amd_get_mmconfig_range(&mmconfig_res);
  263. if (!mmconfig)
  264. return;
  265. list_for_each_entry(pnp_res, &dev->resources, list) {
  266. res = &pnp_res->res;
  267. if (res->end < mmconfig->start || res->start > mmconfig->end ||
  268. (res->start == mmconfig->start && res->end == mmconfig->end))
  269. continue;
  270. dev_info(&dev->dev, FW_BUG
  271. "%pR covers only part of AMD MMCONFIG area %pR; adding more reservations\n",
  272. res, mmconfig);
  273. if (mmconfig->start < res->start) {
  274. start = mmconfig->start;
  275. end = res->start - 1;
  276. pnp_add_mem_resource(dev, start, end, 0);
  277. }
  278. if (mmconfig->end > res->end) {
  279. start = res->end + 1;
  280. end = mmconfig->end;
  281. pnp_add_mem_resource(dev, start, end, 0);
  282. }
  283. break;
  284. }
  285. }
  286. #endif
  287. #ifdef CONFIG_PCI
  288. /* Device IDs of parts that have 32KB MCH space */
  289. static const unsigned int mch_quirk_devices[] = {
  290. 0x0154, /* Ivy Bridge */
  291. 0x0a04, /* Haswell-ULT */
  292. 0x0c00, /* Haswell */
  293. 0x1604, /* Broadwell */
  294. };
  295. static struct pci_dev *get_intel_host(void)
  296. {
  297. int i;
  298. struct pci_dev *host;
  299. for (i = 0; i < ARRAY_SIZE(mch_quirk_devices); i++) {
  300. host = pci_get_device(PCI_VENDOR_ID_INTEL, mch_quirk_devices[i],
  301. NULL);
  302. if (host)
  303. return host;
  304. }
  305. return NULL;
  306. }
  307. static void quirk_intel_mch(struct pnp_dev *dev)
  308. {
  309. struct pci_dev *host;
  310. u32 addr_lo, addr_hi;
  311. struct pci_bus_region region;
  312. struct resource mch;
  313. struct pnp_resource *pnp_res;
  314. struct resource *res;
  315. host = get_intel_host();
  316. if (!host)
  317. return;
  318. /*
  319. * MCHBAR is not an architected PCI BAR, so MCH space is usually
  320. * reported as a PNP0C02 resource. The MCH space was originally
  321. * 16KB, but is 32KB in newer parts. Some BIOSes still report a
  322. * PNP0C02 resource that is only 16KB, which means the rest of the
  323. * MCH space is consumed but unreported.
  324. */
  325. /*
  326. * Read MCHBAR for Host Member Mapped Register Range Base
  327. * https://www-ssl.intel.com/content/www/us/en/processors/core/4th-gen-core-family-desktop-vol-2-datasheet
  328. * Sec 3.1.12.
  329. */
  330. pci_read_config_dword(host, 0x48, &addr_lo);
  331. region.start = addr_lo & ~0x7fff;
  332. pci_read_config_dword(host, 0x4c, &addr_hi);
  333. region.start |= (u64) addr_hi << 32;
  334. region.end = region.start + 32*1024 - 1;
  335. memset(&mch, 0, sizeof(mch));
  336. mch.flags = IORESOURCE_MEM;
  337. pcibios_bus_to_resource(host->bus, &mch, &region);
  338. list_for_each_entry(pnp_res, &dev->resources, list) {
  339. res = &pnp_res->res;
  340. if (res->end < mch.start || res->start > mch.end)
  341. continue; /* no overlap */
  342. if (res->start == mch.start && res->end == mch.end)
  343. continue; /* exact match */
  344. dev_info(&dev->dev, FW_BUG "PNP resource %pR covers only part of %s Intel MCH; extending to %pR\n",
  345. res, pci_name(host), &mch);
  346. res->start = mch.start;
  347. res->end = mch.end;
  348. break;
  349. }
  350. pci_dev_put(host);
  351. }
  352. #endif
  353. /*
  354. * PnP Quirks
  355. * Cards or devices that need some tweaking due to incomplete resource info
  356. */
  357. static struct pnp_fixup pnp_fixups[] = {
  358. /* Soundblaster awe io port quirk */
  359. {"CTL0021", quirk_awe32_resources},
  360. {"CTL0022", quirk_awe32_resources},
  361. {"CTL0023", quirk_awe32_resources},
  362. /* CMI 8330 interrupt and dma fix */
  363. {"@X@0001", quirk_cmi8330_resources},
  364. /* Soundblaster audio device io port range quirk */
  365. {"CTL0001", quirk_sb16audio_resources},
  366. {"CTL0031", quirk_sb16audio_resources},
  367. {"CTL0041", quirk_sb16audio_resources},
  368. {"CTL0042", quirk_sb16audio_resources},
  369. {"CTL0043", quirk_sb16audio_resources},
  370. {"CTL0044", quirk_sb16audio_resources},
  371. {"CTL0045", quirk_sb16audio_resources},
  372. /* Add IRQ-optional MPU options */
  373. {"ADS7151", quirk_ad1815_mpu_resources},
  374. {"ADS7181", quirk_add_irq_optional_dependent_sets},
  375. {"AZT0002", quirk_add_irq_optional_dependent_sets},
  376. /* PnP resources that might overlap PCI BARs */
  377. {"PNP0c01", quirk_system_pci_resources},
  378. {"PNP0c02", quirk_system_pci_resources},
  379. #ifdef CONFIG_AMD_NB
  380. {"PNP0c01", quirk_amd_mmconfig_area},
  381. #endif
  382. #ifdef CONFIG_PCI
  383. {"PNP0c02", quirk_intel_mch},
  384. #endif
  385. {""}
  386. };
  387. void pnp_fixup_device(struct pnp_dev *dev)
  388. {
  389. struct pnp_fixup *f;
  390. for (f = pnp_fixups; *f->id; f++) {
  391. if (!compare_pnp_id(dev->id, f->id))
  392. continue;
  393. pnp_dbg(&dev->dev, "%s: calling %pS\n", f->id,
  394. f->quirk_function);
  395. f->quirk_function(dev);
  396. }
  397. }