Merge tag 'arc-v3.11-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-2.6.git] / arch / x86 / pci / direct.c
blob15460590b8c551c023e7cd0eb1df768cdb572d45
1 /*
2 * direct.c - Low-level direct PCI config space access
3 */
5 #include <linux/pci.h>
6 #include <linux/init.h>
7 #include <linux/dmi.h>
8 #include <asm/pci_x86.h>
11 * Functions for accessing PCI base (first 256 bytes) and extended
12 * (4096 bytes per PCI function) configuration space with type 1
13 * accesses.
16 #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
17 (0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
18 | (devfn << 8) | (reg & 0xFC))
20 static int pci_conf1_read(unsigned int seg, unsigned int bus,
21 unsigned int devfn, int reg, int len, u32 *value)
23 unsigned long flags;
25 if (seg || (bus > 255) || (devfn > 255) || (reg > 4095)) {
26 *value = -1;
27 return -EINVAL;
30 raw_spin_lock_irqsave(&pci_config_lock, flags);
32 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
34 switch (len) {
35 case 1:
36 *value = inb(0xCFC + (reg & 3));
37 break;
38 case 2:
39 *value = inw(0xCFC + (reg & 2));
40 break;
41 case 4:
42 *value = inl(0xCFC);
43 break;
46 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
48 return 0;
51 static int pci_conf1_write(unsigned int seg, unsigned int bus,
52 unsigned int devfn, int reg, int len, u32 value)
54 unsigned long flags;
56 if (seg || (bus > 255) || (devfn > 255) || (reg > 4095))
57 return -EINVAL;
59 raw_spin_lock_irqsave(&pci_config_lock, flags);
61 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
63 switch (len) {
64 case 1:
65 outb((u8)value, 0xCFC + (reg & 3));
66 break;
67 case 2:
68 outw((u16)value, 0xCFC + (reg & 2));
69 break;
70 case 4:
71 outl((u32)value, 0xCFC);
72 break;
75 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
77 return 0;
80 #undef PCI_CONF1_ADDRESS
82 const struct pci_raw_ops pci_direct_conf1 = {
83 .read = pci_conf1_read,
84 .write = pci_conf1_write,
89 * Functions for accessing PCI configuration space with type 2 accesses
92 #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
94 static int pci_conf2_read(unsigned int seg, unsigned int bus,
95 unsigned int devfn, int reg, int len, u32 *value)
97 unsigned long flags;
98 int dev, fn;
100 WARN_ON(seg);
101 if ((bus > 255) || (devfn > 255) || (reg > 255)) {
102 *value = -1;
103 return -EINVAL;
106 dev = PCI_SLOT(devfn);
107 fn = PCI_FUNC(devfn);
109 if (dev & 0x10)
110 return PCIBIOS_DEVICE_NOT_FOUND;
112 raw_spin_lock_irqsave(&pci_config_lock, flags);
114 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
115 outb((u8)bus, 0xCFA);
117 switch (len) {
118 case 1:
119 *value = inb(PCI_CONF2_ADDRESS(dev, reg));
120 break;
121 case 2:
122 *value = inw(PCI_CONF2_ADDRESS(dev, reg));
123 break;
124 case 4:
125 *value = inl(PCI_CONF2_ADDRESS(dev, reg));
126 break;
129 outb(0, 0xCF8);
131 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
133 return 0;
136 static int pci_conf2_write(unsigned int seg, unsigned int bus,
137 unsigned int devfn, int reg, int len, u32 value)
139 unsigned long flags;
140 int dev, fn;
142 WARN_ON(seg);
143 if ((bus > 255) || (devfn > 255) || (reg > 255))
144 return -EINVAL;
146 dev = PCI_SLOT(devfn);
147 fn = PCI_FUNC(devfn);
149 if (dev & 0x10)
150 return PCIBIOS_DEVICE_NOT_FOUND;
152 raw_spin_lock_irqsave(&pci_config_lock, flags);
154 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
155 outb((u8)bus, 0xCFA);
157 switch (len) {
158 case 1:
159 outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
160 break;
161 case 2:
162 outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
163 break;
164 case 4:
165 outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
166 break;
169 outb(0, 0xCF8);
171 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
173 return 0;
176 #undef PCI_CONF2_ADDRESS
178 static const struct pci_raw_ops pci_direct_conf2 = {
179 .read = pci_conf2_read,
180 .write = pci_conf2_write,
185 * Before we decide to use direct hardware access mechanisms, we try to do some
186 * trivial checks to ensure it at least _seems_ to be working -- we just test
187 * whether bus 00 contains a host bridge (this is similar to checking
188 * techniques used in XFree86, but ours should be more reliable since we
189 * attempt to make use of direct access hints provided by the PCI BIOS).
191 * This should be close to trivial, but it isn't, because there are buggy
192 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
194 static int __init pci_sanity_check(const struct pci_raw_ops *o)
196 u32 x = 0;
197 int year, devfn;
199 if (pci_probe & PCI_NO_CHECKS)
200 return 1;
201 /* Assume Type 1 works for newer systems.
202 This handles machines that don't have anything on PCI Bus 0. */
203 dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL);
204 if (year >= 2001)
205 return 1;
207 for (devfn = 0; devfn < 0x100; devfn++) {
208 if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
209 continue;
210 if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
211 return 1;
213 if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
214 continue;
215 if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
216 return 1;
219 DBG(KERN_WARNING "PCI: Sanity check failed\n");
220 return 0;
223 static int __init pci_check_type1(void)
225 unsigned long flags;
226 unsigned int tmp;
227 int works = 0;
229 local_irq_save(flags);
231 outb(0x01, 0xCFB);
232 tmp = inl(0xCF8);
233 outl(0x80000000, 0xCF8);
234 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
235 works = 1;
237 outl(tmp, 0xCF8);
238 local_irq_restore(flags);
240 return works;
243 static int __init pci_check_type2(void)
245 unsigned long flags;
246 int works = 0;
248 local_irq_save(flags);
250 outb(0x00, 0xCFB);
251 outb(0x00, 0xCF8);
252 outb(0x00, 0xCFA);
253 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
254 pci_sanity_check(&pci_direct_conf2)) {
255 works = 1;
258 local_irq_restore(flags);
260 return works;
263 void __init pci_direct_init(int type)
265 if (type == 0)
266 return;
267 printk(KERN_INFO "PCI: Using configuration type %d for base access\n",
268 type);
269 if (type == 1) {
270 raw_pci_ops = &pci_direct_conf1;
271 if (raw_pci_ext_ops)
272 return;
273 if (!(pci_probe & PCI_HAS_IO_ECS))
274 return;
275 printk(KERN_INFO "PCI: Using configuration type 1 "
276 "for extended access\n");
277 raw_pci_ext_ops = &pci_direct_conf1;
278 return;
280 raw_pci_ops = &pci_direct_conf2;
283 int __init pci_direct_probe(void)
285 if ((pci_probe & PCI_PROBE_CONF1) == 0)
286 goto type2;
287 if (!request_region(0xCF8, 8, "PCI conf1"))
288 goto type2;
290 if (pci_check_type1()) {
291 raw_pci_ops = &pci_direct_conf1;
292 port_cf9_safe = true;
293 return 1;
295 release_region(0xCF8, 8);
297 type2:
298 if ((pci_probe & PCI_PROBE_CONF2) == 0)
299 return 0;
300 if (!request_region(0xCF8, 4, "PCI conf2"))
301 return 0;
302 if (!request_region(0xC000, 0x1000, "PCI conf2"))
303 goto fail2;
305 if (pci_check_type2()) {
306 raw_pci_ops = &pci_direct_conf2;
307 port_cf9_safe = true;
308 return 2;
311 release_region(0xC000, 0x1000);
312 fail2:
313 release_region(0xCF8, 4);
314 return 0;