quirks.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * This file contains quirk handling code for PnP devices
  3. * Some devices do not report all their resources, and need to have extra
  4. * resources added. This is most easily accomplished at initialisation time
  5. * when building up the resource structure for the first time.
  6. *
  7. * Copyright (c) 2000 Peter Denison <peterd@pnd-pc.demon.co.uk>
  8. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  9. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  10. *
  11. * Heavily based on PCI quirks handling which is
  12. *
  13. * Copyright (c) 1999 Martin Mares <mj@ucw.cz>
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/pnp.h>
  20. #include <linux/io.h>
  21. #include <linux/kallsyms.h>
  22. #include "base.h"
  23. static void quirk_awe32_add_ports(struct pnp_dev *dev,
  24. struct pnp_option *option,
  25. unsigned int offset)
  26. {
  27. struct pnp_option *new_option;
  28. new_option = kmalloc(sizeof(struct pnp_option), GFP_KERNEL);
  29. if (!new_option) {
  30. dev_err(&dev->dev, "couldn't add ioport region to option set "
  31. "%d\n", pnp_option_set(option));
  32. return;
  33. }
  34. *new_option = *option;
  35. new_option->u.port.min += offset;
  36. new_option->u.port.max += offset;
  37. list_add(&new_option->list, &option->list);
  38. dev_info(&dev->dev, "added ioport region %#llx-%#llx to set %d\n",
  39. (unsigned long long) new_option->u.port.min,
  40. (unsigned long long) new_option->u.port.max,
  41. pnp_option_set(option));
  42. }
  43. static void quirk_awe32_resources(struct pnp_dev *dev)
  44. {
  45. struct pnp_option *option;
  46. unsigned int set = ~0;
  47. /*
  48. * Add two extra ioport regions (at offset 0x400 and 0x800 from the
  49. * one given) to every dependent option set.
  50. */
  51. list_for_each_entry(option, &dev->options, list) {
  52. if (pnp_option_is_dependent(option) &&
  53. pnp_option_set(option) != set) {
  54. set = pnp_option_set(option);
  55. quirk_awe32_add_ports(dev, option, 0x800);
  56. quirk_awe32_add_ports(dev, option, 0x400);
  57. }
  58. }
  59. }
  60. static void quirk_cmi8330_resources(struct pnp_dev *dev)
  61. {
  62. struct pnp_option *option;
  63. struct pnp_irq *irq;
  64. struct pnp_dma *dma;
  65. list_for_each_entry(option, &dev->options, list) {
  66. if (!pnp_option_is_dependent(option))
  67. continue;
  68. if (option->type == IORESOURCE_IRQ) {
  69. irq = &option->u.irq;
  70. bitmap_zero(irq->map.bits, PNP_IRQ_NR);
  71. __set_bit(5, irq->map.bits);
  72. __set_bit(7, irq->map.bits);
  73. __set_bit(10, irq->map.bits);
  74. dev_info(&dev->dev, "set possible IRQs in "
  75. "option set %d to 5, 7, 10\n",
  76. pnp_option_set(option));
  77. } else if (option->type == IORESOURCE_DMA) {
  78. dma = &option->u.dma;
  79. if ((dma->flags & IORESOURCE_DMA_TYPE_MASK) ==
  80. IORESOURCE_DMA_8BIT &&
  81. dma->map != 0x0A) {
  82. dev_info(&dev->dev, "changing possible "
  83. "DMA channel mask in option set %d "
  84. "from %#02x to 0x0A (1, 3)\n",
  85. pnp_option_set(option), dma->map);
  86. dma->map = 0x0A;
  87. }
  88. }
  89. }
  90. }
  91. static void quirk_sb16audio_resources(struct pnp_dev *dev)
  92. {
  93. struct pnp_option *option;
  94. unsigned int prev_option_flags = ~0, n = 0;
  95. struct pnp_port *port;
  96. /*
  97. * The default range on the OPL port for these devices is 0x388-0x388.
  98. * Here we increase that range so that two such cards can be
  99. * auto-configured.
  100. */
  101. list_for_each_entry(option, &dev->options, list) {
  102. if (prev_option_flags != option->flags) {
  103. prev_option_flags = option->flags;
  104. n = 0;
  105. }
  106. if (pnp_option_is_dependent(option) &&
  107. option->type == IORESOURCE_IO) {
  108. n++;
  109. port = &option->u.port;
  110. if (n == 3 && port->min == port->max) {
  111. port->max += 0x70;
  112. dev_info(&dev->dev, "increased option port "
  113. "range from %#llx-%#llx to "
  114. "%#llx-%#llx\n",
  115. (unsigned long long) port->min,
  116. (unsigned long long) port->min,
  117. (unsigned long long) port->min,
  118. (unsigned long long) port->max);
  119. }
  120. }
  121. }
  122. }
  123. static struct pnp_option *pnp_clone_dependent_set(struct pnp_dev *dev,
  124. unsigned int set)
  125. {
  126. struct pnp_option *tail = NULL, *first_new_option = NULL;
  127. struct pnp_option *option, *new_option;
  128. unsigned int flags;
  129. list_for_each_entry(option, &dev->options, list) {
  130. if (pnp_option_is_dependent(option))
  131. tail = option;
  132. }
  133. if (!tail) {
  134. dev_err(&dev->dev, "no dependent option sets\n");
  135. return NULL;
  136. }
  137. flags = pnp_new_dependent_set(dev, PNP_RES_PRIORITY_FUNCTIONAL);
  138. list_for_each_entry(option, &dev->options, list) {
  139. if (pnp_option_is_dependent(option) &&
  140. pnp_option_set(option) == set) {
  141. new_option = kmalloc(sizeof(struct pnp_option),
  142. GFP_KERNEL);
  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. #include <linux/pci.h>
  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 type;
  218. type = pci_resource_flags(pdev, i) &
  219. (IORESOURCE_IO | IORESOURCE_MEM);
  220. if (!type || pci_resource_len(pdev, i) == 0)
  221. continue;
  222. pci_start = pci_resource_start(pdev, i);
  223. pci_end = pci_resource_end(pdev, i);
  224. for (j = 0;
  225. (res = pnp_get_resource(dev, type, j)); j++) {
  226. if (res->start == 0 && res->end == 0)
  227. continue;
  228. pnp_start = res->start;
  229. pnp_end = res->end;
  230. /*
  231. * If the PNP region doesn't overlap the PCI
  232. * region at all, there's no problem.
  233. */
  234. if (pnp_end < pci_start || pnp_start > pci_end)
  235. continue;
  236. /*
  237. * If the PNP region completely encloses (or is
  238. * at least as large as) the PCI region, that's
  239. * also OK. For example, this happens when the
  240. * PNP device describes a bridge with PCI
  241. * behind it.
  242. */
  243. if (pnp_start <= pci_start &&
  244. pnp_end >= pci_end)
  245. continue;
  246. /*
  247. * Otherwise, the PNP region overlaps *part* of
  248. * the PCI region, and that might prevent a PCI
  249. * driver from requesting its resources.
  250. */
  251. dev_warn(&dev->dev,
  252. "disabling %pR because it overlaps "
  253. "%s BAR %d %pR\n", res,
  254. pci_name(pdev), i, &pdev->resource[i]);
  255. res->flags |= IORESOURCE_DISABLED;
  256. }
  257. }
  258. }
  259. }
  260. #ifdef CONFIG_AMD_NB
  261. #include <asm/amd_nb.h>
  262. static void quirk_amd_mmconfig_area(struct pnp_dev *dev)
  263. {
  264. resource_size_t start, end;
  265. struct pnp_resource *pnp_res;
  266. struct resource *res;
  267. struct resource mmconfig_res, *mmconfig;
  268. mmconfig = amd_get_mmconfig_range(&mmconfig_res);
  269. if (!mmconfig)
  270. return;
  271. list_for_each_entry(pnp_res, &dev->resources, list) {
  272. res = &pnp_res->res;
  273. if (res->end < mmconfig->start || res->start > mmconfig->end ||
  274. (res->start == mmconfig->start && res->end == mmconfig->end))
  275. continue;
  276. dev_info(&dev->dev, FW_BUG
  277. "%pR covers only part of AMD MMCONFIG area %pR; adding more reservations\n",
  278. res, mmconfig);
  279. if (mmconfig->start < res->start) {
  280. start = mmconfig->start;
  281. end = res->start - 1;
  282. pnp_add_mem_resource(dev, start, end, 0);
  283. }
  284. if (mmconfig->end > res->end) {
  285. start = res->end + 1;
  286. end = mmconfig->end;
  287. pnp_add_mem_resource(dev, start, end, 0);
  288. }
  289. break;
  290. }
  291. }
  292. #endif
  293. /*
  294. * PnP Quirks
  295. * Cards or devices that need some tweaking due to incomplete resource info
  296. */
  297. static struct pnp_fixup pnp_fixups[] = {
  298. /* Soundblaster awe io port quirk */
  299. {"CTL0021", quirk_awe32_resources},
  300. {"CTL0022", quirk_awe32_resources},
  301. {"CTL0023", quirk_awe32_resources},
  302. /* CMI 8330 interrupt and dma fix */
  303. {"@X@0001", quirk_cmi8330_resources},
  304. /* Soundblaster audio device io port range quirk */
  305. {"CTL0001", quirk_sb16audio_resources},
  306. {"CTL0031", quirk_sb16audio_resources},
  307. {"CTL0041", quirk_sb16audio_resources},
  308. {"CTL0042", quirk_sb16audio_resources},
  309. {"CTL0043", quirk_sb16audio_resources},
  310. {"CTL0044", quirk_sb16audio_resources},
  311. {"CTL0045", quirk_sb16audio_resources},
  312. /* Add IRQ-optional MPU options */
  313. {"ADS7151", quirk_ad1815_mpu_resources},
  314. {"ADS7181", quirk_add_irq_optional_dependent_sets},
  315. {"AZT0002", quirk_add_irq_optional_dependent_sets},
  316. /* PnP resources that might overlap PCI BARs */
  317. {"PNP0c01", quirk_system_pci_resources},
  318. {"PNP0c02", quirk_system_pci_resources},
  319. #ifdef CONFIG_AMD_NB
  320. {"PNP0c01", quirk_amd_mmconfig_area},
  321. #endif
  322. {""}
  323. };
  324. void pnp_fixup_device(struct pnp_dev *dev)
  325. {
  326. struct pnp_fixup *f;
  327. for (f = pnp_fixups; *f->id; f++) {
  328. if (!compare_pnp_id(dev->id, f->id))
  329. continue;
  330. pnp_dbg(&dev->dev, "%s: calling %pF\n", f->id,
  331. f->quirk_function);
  332. f->quirk_function(dev);
  333. }
  334. }