direct.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 ((bus > 255) || (devfn > 255) || (reg > 4095)) {
  21. *value = -1;
  22. return -EINVAL;
  23. }
  24. 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. 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 ((bus > 255) || (devfn > 255) || (reg > 4095))
  45. return -EINVAL;
  46. 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. spin_unlock_irqrestore(&pci_config_lock, flags);
  60. return 0;
  61. }
  62. #undef PCI_CONF1_ADDRESS
  63. 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. if ((bus > 255) || (devfn > 255) || (reg > 255)) {
  77. *value = -1;
  78. return -EINVAL;
  79. }
  80. dev = PCI_SLOT(devfn);
  81. fn = PCI_FUNC(devfn);
  82. if (dev & 0x10)
  83. return PCIBIOS_DEVICE_NOT_FOUND;
  84. spin_lock_irqsave(&pci_config_lock, flags);
  85. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  86. outb((u8)bus, 0xCFA);
  87. switch (len) {
  88. case 1:
  89. *value = inb(PCI_CONF2_ADDRESS(dev, reg));
  90. break;
  91. case 2:
  92. *value = inw(PCI_CONF2_ADDRESS(dev, reg));
  93. break;
  94. case 4:
  95. *value = inl(PCI_CONF2_ADDRESS(dev, reg));
  96. break;
  97. }
  98. outb(0, 0xCF8);
  99. spin_unlock_irqrestore(&pci_config_lock, flags);
  100. return 0;
  101. }
  102. static int pci_conf2_write(unsigned int seg, unsigned int bus,
  103. unsigned int devfn, int reg, int len, u32 value)
  104. {
  105. unsigned long flags;
  106. int dev, fn;
  107. if ((bus > 255) || (devfn > 255) || (reg > 255))
  108. return -EINVAL;
  109. dev = PCI_SLOT(devfn);
  110. fn = PCI_FUNC(devfn);
  111. if (dev & 0x10)
  112. return PCIBIOS_DEVICE_NOT_FOUND;
  113. spin_lock_irqsave(&pci_config_lock, flags);
  114. outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  115. outb((u8)bus, 0xCFA);
  116. switch (len) {
  117. case 1:
  118. outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
  119. break;
  120. case 2:
  121. outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
  122. break;
  123. case 4:
  124. outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
  125. break;
  126. }
  127. outb(0, 0xCF8);
  128. spin_unlock_irqrestore(&pci_config_lock, flags);
  129. return 0;
  130. }
  131. #undef PCI_CONF2_ADDRESS
  132. struct pci_raw_ops pci_direct_conf2 = {
  133. .read = pci_conf2_read,
  134. .write = pci_conf2_write,
  135. };
  136. /*
  137. * Before we decide to use direct hardware access mechanisms, we try to do some
  138. * trivial checks to ensure it at least _seems_ to be working -- we just test
  139. * whether bus 00 contains a host bridge (this is similar to checking
  140. * techniques used in XFree86, but ours should be more reliable since we
  141. * attempt to make use of direct access hints provided by the PCI BIOS).
  142. *
  143. * This should be close to trivial, but it isn't, because there are buggy
  144. * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  145. */
  146. static int __init pci_sanity_check(struct pci_raw_ops *o)
  147. {
  148. u32 x = 0;
  149. int devfn;
  150. if (pci_probe & PCI_NO_CHECKS)
  151. return 1;
  152. /* Assume Type 1 works for newer systems.
  153. This handles machines that don't have anything on PCI Bus 0. */
  154. if (dmi_get_year(DMI_BIOS_DATE) >= 2001)
  155. return 1;
  156. for (devfn = 0; devfn < 0x100; devfn++) {
  157. if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
  158. continue;
  159. if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
  160. return 1;
  161. if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
  162. continue;
  163. if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
  164. return 1;
  165. }
  166. DBG(KERN_WARNING "PCI: Sanity check failed\n");
  167. return 0;
  168. }
  169. static int __init pci_check_type1(void)
  170. {
  171. unsigned long flags;
  172. unsigned int tmp;
  173. int works = 0;
  174. local_irq_save(flags);
  175. outb(0x01, 0xCFB);
  176. tmp = inl(0xCF8);
  177. outl(0x80000000, 0xCF8);
  178. if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
  179. works = 1;
  180. }
  181. outl(tmp, 0xCF8);
  182. local_irq_restore(flags);
  183. return works;
  184. }
  185. static int __init pci_check_type2(void)
  186. {
  187. unsigned long flags;
  188. int works = 0;
  189. local_irq_save(flags);
  190. outb(0x00, 0xCFB);
  191. outb(0x00, 0xCF8);
  192. outb(0x00, 0xCFA);
  193. if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
  194. pci_sanity_check(&pci_direct_conf2)) {
  195. works = 1;
  196. }
  197. local_irq_restore(flags);
  198. return works;
  199. }
  200. void __init pci_direct_init(int type)
  201. {
  202. if (type == 0)
  203. return;
  204. printk(KERN_INFO "PCI: Using configuration type %d for base access\n",
  205. type);
  206. if (type == 1) {
  207. raw_pci_ops = &pci_direct_conf1;
  208. if (raw_pci_ext_ops)
  209. return;
  210. if (!(pci_probe & PCI_HAS_IO_ECS))
  211. return;
  212. printk(KERN_INFO "PCI: Using configuration type 1 "
  213. "for extended access\n");
  214. raw_pci_ext_ops = &pci_direct_conf1;
  215. return;
  216. }
  217. raw_pci_ops = &pci_direct_conf2;
  218. }
  219. int __init pci_direct_probe(void)
  220. {
  221. struct resource *region, *region2;
  222. if ((pci_probe & PCI_PROBE_CONF1) == 0)
  223. goto type2;
  224. region = request_region(0xCF8, 8, "PCI conf1");
  225. if (!region)
  226. goto type2;
  227. if (pci_check_type1()) {
  228. raw_pci_ops = &pci_direct_conf1;
  229. port_cf9_safe = true;
  230. return 1;
  231. }
  232. release_resource(region);
  233. type2:
  234. if ((pci_probe & PCI_PROBE_CONF2) == 0)
  235. return 0;
  236. region = request_region(0xCF8, 4, "PCI conf2");
  237. if (!region)
  238. return 0;
  239. region2 = request_region(0xC000, 0x1000, "PCI conf2");
  240. if (!region2)
  241. goto fail2;
  242. if (pci_check_type2()) {
  243. raw_pci_ops = &pci_direct_conf2;
  244. port_cf9_safe = true;
  245. return 2;
  246. }
  247. release_resource(region2);
  248. fail2:
  249. release_resource(region);
  250. return 0;
  251. }