direct.c 5.7 KB

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