quirks.c 12 KB

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