Linux-2.6.12-rc2
[linux-2.6/kvm.git] / arch / i386 / pci / direct.c
blob30b7e9b4f6a20368b27253a1cfb2db5edac50b7f
1 /*
2 * direct.c - Low-level direct PCI config space access
3 */
5 #include <linux/pci.h>
6 #include <linux/init.h>
7 #include "pci.h"
9 /*
10 * Functions for accessing PCI configuration space with type 1 accesses
13 #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
14 (0x80000000 | (bus << 16) | (devfn << 8) | (reg & ~3))
16 static int pci_conf1_read(unsigned int seg, unsigned int bus,
17 unsigned int devfn, int reg, int len, u32 *value)
19 unsigned long flags;
21 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
22 return -EINVAL;
24 spin_lock_irqsave(&pci_config_lock, flags);
26 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
28 switch (len) {
29 case 1:
30 *value = inb(0xCFC + (reg & 3));
31 break;
32 case 2:
33 *value = inw(0xCFC + (reg & 2));
34 break;
35 case 4:
36 *value = inl(0xCFC);
37 break;
40 spin_unlock_irqrestore(&pci_config_lock, flags);
42 return 0;
45 static int pci_conf1_write(unsigned int seg, unsigned int bus,
46 unsigned int devfn, int reg, int len, u32 value)
48 unsigned long flags;
50 if ((bus > 255) || (devfn > 255) || (reg > 255))
51 return -EINVAL;
53 spin_lock_irqsave(&pci_config_lock, flags);
55 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
57 switch (len) {
58 case 1:
59 outb((u8)value, 0xCFC + (reg & 3));
60 break;
61 case 2:
62 outw((u16)value, 0xCFC + (reg & 2));
63 break;
64 case 4:
65 outl((u32)value, 0xCFC);
66 break;
69 spin_unlock_irqrestore(&pci_config_lock, flags);
71 return 0;
74 #undef PCI_CONF1_ADDRESS
76 struct pci_raw_ops pci_direct_conf1 = {
77 .read = pci_conf1_read,
78 .write = pci_conf1_write,
83 * Functions for accessing PCI configuration space with type 2 accesses
86 #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
88 static int pci_conf2_read(unsigned int seg, unsigned int bus,
89 unsigned int devfn, int reg, int len, u32 *value)
91 unsigned long flags;
92 int dev, fn;
94 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
95 return -EINVAL;
97 dev = PCI_SLOT(devfn);
98 fn = PCI_FUNC(devfn);
100 if (dev & 0x10)
101 return PCIBIOS_DEVICE_NOT_FOUND;
103 spin_lock_irqsave(&pci_config_lock, flags);
105 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
106 outb((u8)bus, 0xCFA);
108 switch (len) {
109 case 1:
110 *value = inb(PCI_CONF2_ADDRESS(dev, reg));
111 break;
112 case 2:
113 *value = inw(PCI_CONF2_ADDRESS(dev, reg));
114 break;
115 case 4:
116 *value = inl(PCI_CONF2_ADDRESS(dev, reg));
117 break;
120 outb(0, 0xCF8);
122 spin_unlock_irqrestore(&pci_config_lock, flags);
124 return 0;
127 static int pci_conf2_write(unsigned int seg, unsigned int bus,
128 unsigned int devfn, int reg, int len, u32 value)
130 unsigned long flags;
131 int dev, fn;
133 if ((bus > 255) || (devfn > 255) || (reg > 255))
134 return -EINVAL;
136 dev = PCI_SLOT(devfn);
137 fn = PCI_FUNC(devfn);
139 if (dev & 0x10)
140 return PCIBIOS_DEVICE_NOT_FOUND;
142 spin_lock_irqsave(&pci_config_lock, flags);
144 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
145 outb((u8)bus, 0xCFA);
147 switch (len) {
148 case 1:
149 outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
150 break;
151 case 2:
152 outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
153 break;
154 case 4:
155 outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
156 break;
159 outb(0, 0xCF8);
161 spin_unlock_irqrestore(&pci_config_lock, flags);
163 return 0;
166 #undef PCI_CONF2_ADDRESS
168 static struct pci_raw_ops pci_direct_conf2 = {
169 .read = pci_conf2_read,
170 .write = pci_conf2_write,
175 * Before we decide to use direct hardware access mechanisms, we try to do some
176 * trivial checks to ensure it at least _seems_ to be working -- we just test
177 * whether bus 00 contains a host bridge (this is similar to checking
178 * techniques used in XFree86, but ours should be more reliable since we
179 * attempt to make use of direct access hints provided by the PCI BIOS).
181 * This should be close to trivial, but it isn't, because there are buggy
182 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
184 static int __init pci_sanity_check(struct pci_raw_ops *o)
186 u32 x = 0;
187 int devfn;
189 if (pci_probe & PCI_NO_CHECKS)
190 return 1;
192 for (devfn = 0; devfn < 0x100; devfn++) {
193 if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
194 continue;
195 if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
196 return 1;
198 if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
199 continue;
200 if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
201 return 1;
204 DBG("PCI: Sanity check failed\n");
205 return 0;
208 static int __init pci_check_type1(void)
210 unsigned long flags;
211 unsigned int tmp;
212 int works = 0;
214 local_irq_save(flags);
216 outb(0x01, 0xCFB);
217 tmp = inl(0xCF8);
218 outl(0x80000000, 0xCF8);
219 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
220 works = 1;
222 outl(tmp, 0xCF8);
223 local_irq_restore(flags);
225 return works;
228 static int __init pci_check_type2(void)
230 unsigned long flags;
231 int works = 0;
233 local_irq_save(flags);
235 outb(0x00, 0xCFB);
236 outb(0x00, 0xCF8);
237 outb(0x00, 0xCFA);
238 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
239 pci_sanity_check(&pci_direct_conf2)) {
240 works = 1;
243 local_irq_restore(flags);
245 return works;
248 static int __init pci_direct_init(void)
250 struct resource *region, *region2;
252 if ((pci_probe & PCI_PROBE_CONF1) == 0)
253 goto type2;
254 region = request_region(0xCF8, 8, "PCI conf1");
255 if (!region)
256 goto type2;
258 if (pci_check_type1()) {
259 printk(KERN_INFO "PCI: Using configuration type 1\n");
260 raw_pci_ops = &pci_direct_conf1;
261 return 0;
263 release_resource(region);
265 type2:
266 if ((pci_probe & PCI_PROBE_CONF2) == 0)
267 goto out;
268 region = request_region(0xCF8, 4, "PCI conf2");
269 if (!region)
270 goto out;
271 region2 = request_region(0xC000, 0x1000, "PCI conf2");
272 if (!region2)
273 goto fail2;
275 if (pci_check_type2()) {
276 printk(KERN_INFO "PCI: Using configuration type 2\n");
277 raw_pci_ops = &pci_direct_conf2;
278 return 0;
281 release_resource(region2);
282 fail2:
283 release_resource(region);
285 out:
286 return 0;
289 arch_initcall(pci_direct_init);