direct.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * direct.c - Low-level direct PCI config space access
  3. */
  4. #include <linux/pci.h>
  5. #include <linux/init.h>
  6. #include <linux/dmi.h>
  7. #include <asm/pci_x86.h>
  8. /*
  9. * Functions for accessing PCI base (first 256 bytes) and extended
  10. * (4096 bytes per PCI function) configuration space with type 1
  11. * accesses.
  12. */
  13. #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
  14. (0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
  15. | (devfn << 8) | (reg & 0xFC))
  16. static int pci_conf1_read(unsigned int seg, unsigned int bus,
  17. unsigned int devfn, int reg, int len, u32 *value)
  18. {
  19. unsigned long flags;
  20. if (seg || (bus > 255) || (devfn > 255) || (reg > 4095)) {
  21. *value = -1;
  22. return -EINVAL;
  23. }
  24. raw_spin_lock_irqsave(&pci_config_lock, flags);
  25. outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
  26. switch (len) {
  27. case 1:
  28. *value = inb(0xCFC + (reg & 3));
  29. break;
  30. case 2:
  31. *value = inw(0xCFC + (reg & 2));
  32. break;
  33. case 4:
  34. *value = inl(0xCFC);
  35. break;
  36. }
  37. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  38. return 0;
  39. }
  40. static int pci_conf1_write(unsigned int seg, unsigned int bus,
  41. unsigned int devfn, int reg, int len, u32 value)
  42. {
  43. unsigned long flags;
  44. if (seg || (bus > 255) || (devfn > 255) || (reg > 4095))
  45. return -EINVAL;
  46. raw_spin_lock_irqsave(&pci_config_lock, flags);
  47. outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
  48. switch (len) {
  49. case 1:
  50. outb((u8)value, 0xCFC + (reg & 3));
  51. break;
  52. case 2:
  53. outw((u16)value, 0xCFC + (reg & 2));
  54. break;
  55. case 4:
  56. outl((u32)value, 0xCFC);
  57. break;
  58. }
  59. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  60. return 0;
  61. }
  62. #undef PCI_CONF1_ADDRESS
  63. const struct pci_raw_ops pci_direct_conf1 = {
  64. .read = pci_conf1_read,
  65. .write = pci_conf1_write,
  66. };
  67. /*
  68. * Functions for accessing PCI configuration space with type 2 accesses
  69. */
  70. #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
  71. static int pci_conf2_read(unsigned int seg, unsigned int bus,
  72. unsigned int devfn, int reg, int len, u32 *value)
  73. {
  74. unsigned long flags;
  75. int dev, fn;
  76. WARN_ON(seg);
  77. if ((bus > 255) || (devfn > 255) || (reg > 255)) {
  78. *value = -1;
  79. return -EINVAL;
  80. }
  81. dev = PCI_SLOT(devfn);
  82. fn = PCI_FUNC(devfn);
  83. if (dev & 0x10)
  84. return PCIBIOS_DEVICE_NOT_FOUND;
  85. raw_spin_lock_irqsave(&pci_config_lock, flags);
  86. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  87. outb((u8)bus, 0xCFA);
  88. switch (len) {
  89. case 1:
  90. *value = inb(PCI_CONF2_ADDRESS(dev, reg));
  91. break;
  92. case 2:
  93. *value = inw(PCI_CONF2_ADDRESS(dev, reg));
  94. break;
  95. case 4:
  96. *value = inl(PCI_CONF2_ADDRESS(dev, reg));
  97. break;
  98. }
  99. outb(0, 0xCF8);
  100. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  101. return 0;
  102. }
  103. static int pci_conf2_write(unsigned int seg, unsigned int bus,
  104. unsigned int devfn, int reg, int len, u32 value)
  105. {
  106. unsigned long flags;
  107. int dev, fn;
  108. WARN_ON(seg);
  109. if ((bus > 255) || (devfn > 255) || (reg > 255))
  110. return -EINVAL;
  111. dev = PCI_SLOT(devfn);
  112. fn = PCI_FUNC(devfn);
  113. if (dev & 0x10)
  114. return PCIBIOS_DEVICE_NOT_FOUND;
  115. raw_spin_lock_irqsave(&pci_config_lock, flags);
  116. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  117. outb((u8)bus, 0xCFA);
  118. switch (len) {
  119. case 1:
  120. outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
  121. break;
  122. case 2:
  123. outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
  124. break;
  125. case 4:
  126. outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
  127. break;
  128. }
  129. outb(0, 0xCF8);
  130. raw_spin_unlock_irqrestore(&pci_config_lock, flags);
  131. return 0;
  132. }
  133. #undef PCI_CONF2_ADDRESS
  134. static const struct pci_raw_ops pci_direct_conf2 = {
  135. .read = pci_conf2_read,
  136. .write = pci_conf2_write,
  137. };
  138. /*
  139. * Before we decide to use direct hardware access mechanisms, we try to do some
  140. * trivial checks to ensure it at least _seems_ to be working -- we just test
  141. * whether bus 00 contains a host bridge (this is similar to checking
  142. * techniques used in XFree86, but ours should be more reliable since we
  143. * attempt to make use of direct access hints provided by the PCI BIOS).
  144. *
  145. * This should be close to trivial, but it isn't, because there are buggy
  146. * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  147. */
  148. static int __init pci_sanity_check(const struct pci_raw_ops *o)
  149. {
  150. u32 x = 0;
  151. int year, devfn;
  152. if (pci_probe & PCI_NO_CHECKS)
  153. return 1;
  154. /* Assume Type 1 works for newer systems.
  155. This handles machines that don't have anything on PCI Bus 0. */
  156. dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
  157. if (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. }