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