direct.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * direct.c - Low-level direct PCI config space access
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/init.h>
  7. #include <linux/dmi.h>
  8. #include <asm/pci_x86.h>
  9. /*
  10. * Functions for accessing PCI base (first 256 bytes) and extended
  11. * (4096 bytes per PCI function) configuration space with type 1
  12. * accesses.
  13. */
  14. #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
  15. (0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
  16. | (devfn << 8) | (reg & 0xFC))
  17. static int pci_conf1_read(unsigned int seg, unsigned int bus,
  18. unsigned int devfn, int reg, int len, u32 *value)
  19. {
  20. unsigned long flags;
  21. if (seg || (bus > 255) || (devfn > 255) || (reg > 4095)) {
  22. *value = -1;
  23. return -EINVAL;
  24. }
  25. raw_spin_lock_irqsave(&pci_config_lock, flags);
  26. outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
  27. switch (len) {
  28. case 1:
  29. *value = inb(0xCFC + (reg & 3));
  30. break;
  31. case 2:
  32. *value = inw(0xCFC + (reg & 2));
  33. break;
  34. case 4:
  35. *value = inl(0xCFC);
  36. break;
  37. }
  38. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  39. return 0;
  40. }
  41. static int pci_conf1_write(unsigned int seg, unsigned int bus,
  42. unsigned int devfn, int reg, int len, u32 value)
  43. {
  44. unsigned long flags;
  45. if (seg || (bus > 255) || (devfn > 255) || (reg > 4095))
  46. return -EINVAL;
  47. raw_spin_lock_irqsave(&pci_config_lock, flags);
  48. outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
  49. switch (len) {
  50. case 1:
  51. outb((u8)value, 0xCFC + (reg & 3));
  52. break;
  53. case 2:
  54. outw((u16)value, 0xCFC + (reg & 2));
  55. break;
  56. case 4:
  57. outl((u32)value, 0xCFC);
  58. break;
  59. }
  60. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  61. return 0;
  62. }
  63. #undef PCI_CONF1_ADDRESS
  64. const struct pci_raw_ops pci_direct_conf1 = {
  65. .read = pci_conf1_read,
  66. .write = pci_conf1_write,
  67. };
  68. /*
  69. * Functions for accessing PCI configuration space with type 2 accesses
  70. */
  71. #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
  72. static int pci_conf2_read(unsigned int seg, unsigned int bus,
  73. unsigned int devfn, int reg, int len, u32 *value)
  74. {
  75. unsigned long flags;
  76. int dev, fn;
  77. WARN_ON(seg);
  78. if ((bus > 255) || (devfn > 255) || (reg > 255)) {
  79. *value = -1;
  80. return -EINVAL;
  81. }
  82. dev = PCI_SLOT(devfn);
  83. fn = PCI_FUNC(devfn);
  84. if (dev & 0x10)
  85. return PCIBIOS_DEVICE_NOT_FOUND;
  86. raw_spin_lock_irqsave(&pci_config_lock, flags);
  87. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  88. outb((u8)bus, 0xCFA);
  89. switch (len) {
  90. case 1:
  91. *value = inb(PCI_CONF2_ADDRESS(dev, reg));
  92. break;
  93. case 2:
  94. *value = inw(PCI_CONF2_ADDRESS(dev, reg));
  95. break;
  96. case 4:
  97. *value = inl(PCI_CONF2_ADDRESS(dev, reg));
  98. break;
  99. }
  100. outb(0, 0xCF8);
  101. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  102. return 0;
  103. }
  104. static int pci_conf2_write(unsigned int seg, unsigned int bus,
  105. unsigned int devfn, int reg, int len, u32 value)
  106. {
  107. unsigned long flags;
  108. int dev, fn;
  109. WARN_ON(seg);
  110. if ((bus > 255) || (devfn > 255) || (reg > 255))
  111. return -EINVAL;
  112. dev = PCI_SLOT(devfn);
  113. fn = PCI_FUNC(devfn);
  114. if (dev & 0x10)
  115. return PCIBIOS_DEVICE_NOT_FOUND;
  116. raw_spin_lock_irqsave(&pci_config_lock, flags);
  117. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  118. outb((u8)bus, 0xCFA);
  119. switch (len) {
  120. case 1:
  121. outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
  122. break;
  123. case 2:
  124. outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
  125. break;
  126. case 4:
  127. outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
  128. break;
  129. }
  130. outb(0, 0xCF8);
  131. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  132. return 0;
  133. }
  134. #undef PCI_CONF2_ADDRESS
  135. static const struct pci_raw_ops pci_direct_conf2 = {
  136. .read = pci_conf2_read,
  137. .write = pci_conf2_write,
  138. };
  139. /*
  140. * Before we decide to use direct hardware access mechanisms, we try to do some
  141. * trivial checks to ensure it at least _seems_ to be working -- we just test
  142. * whether bus 00 contains a host bridge (this is similar to checking
  143. * techniques used in XFree86, but ours should be more reliable since we
  144. * attempt to make use of direct access hints provided by the PCI BIOS).
  145. *
  146. * This should be close to trivial, but it isn't, because there are buggy
  147. * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  148. */
  149. static int __init pci_sanity_check(const struct pci_raw_ops *o)
  150. {
  151. u32 x = 0;
  152. int devfn;
  153. if (pci_probe & PCI_NO_CHECKS)
  154. return 1;
  155. /* Assume Type 1 works for newer systems.
  156. This handles machines that don't have anything on PCI Bus 0. */
  157. if (dmi_get_bios_year() >= 2001)
  158. return 1;
  159. for (devfn = 0; devfn < 0x100; devfn++) {
  160. if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
  161. continue;
  162. if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
  163. return 1;
  164. if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
  165. continue;
  166. if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
  167. return 1;
  168. }
  169. DBG(KERN_WARNING "PCI: Sanity check failed\n");
  170. return 0;
  171. }
  172. static int __init pci_check_type1(void)
  173. {
  174. unsigned long flags;
  175. unsigned int tmp;
  176. int works = 0;
  177. local_irq_save(flags);
  178. outb(0x01, 0xCFB);
  179. tmp = inl(0xCF8);
  180. outl(0x80000000, 0xCF8);
  181. if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
  182. works = 1;
  183. }
  184. outl(tmp, 0xCF8);
  185. local_irq_restore(flags);
  186. return works;
  187. }
  188. static int __init pci_check_type2(void)
  189. {
  190. unsigned long flags;
  191. int works = 0;
  192. local_irq_save(flags);
  193. outb(0x00, 0xCFB);
  194. outb(0x00, 0xCF8);
  195. outb(0x00, 0xCFA);
  196. if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
  197. pci_sanity_check(&pci_direct_conf2)) {
  198. works = 1;
  199. }
  200. local_irq_restore(flags);
  201. return works;
  202. }
  203. void __init pci_direct_init(int type)
  204. {
  205. if (type == 0)
  206. return;
  207. printk(KERN_INFO "PCI: Using configuration type %d for base access\n",
  208. type);
  209. if (type == 1) {
  210. raw_pci_ops = &pci_direct_conf1;
  211. if (raw_pci_ext_ops)
  212. return;
  213. if (!(pci_probe & PCI_HAS_IO_ECS))
  214. return;
  215. printk(KERN_INFO "PCI: Using configuration type 1 "
  216. "for extended access\n");
  217. raw_pci_ext_ops = &pci_direct_conf1;
  218. return;
  219. }
  220. raw_pci_ops = &pci_direct_conf2;
  221. }
  222. int __init pci_direct_probe(void)
  223. {
  224. if ((pci_probe & PCI_PROBE_CONF1) == 0)
  225. goto type2;
  226. if (!request_region(0xCF8, 8, "PCI conf1"))
  227. goto type2;
  228. if (pci_check_type1()) {
  229. raw_pci_ops = &pci_direct_conf1;
  230. port_cf9_safe = true;
  231. return 1;
  232. }
  233. release_region(0xCF8, 8);
  234. type2:
  235. if ((pci_probe & PCI_PROBE_CONF2) == 0)
  236. return 0;
  237. if (!request_region(0xCF8, 4, "PCI conf2"))
  238. return 0;
  239. if (!request_region(0xC000, 0x1000, "PCI conf2"))
  240. goto fail2;
  241. if (pci_check_type2()) {
  242. raw_pci_ops = &pci_direct_conf2;
  243. port_cf9_safe = true;
  244. return 2;
  245. }
  246. release_region(0xC000, 0x1000);
  247. fail2:
  248. release_region(0xCF8, 4);
  249. return 0;
  250. }