blob: 0659ced01185201bc4a6d323217a284cb153cc3a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * direct.c - Low-level direct PCI config space access
3 */
4
5#include <linux/pci.h>
6#include <linux/init.h>
Andi Kleenec0f08e2006-04-07 19:49:36 +02007#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "pci.h"
9
10/*
11 * Functions for accessing PCI configuration space with type 1 accesses
12 */
13
14#define PCI_CONF1_ADDRESS(bus, devfn, reg) \
15 (0x80000000 | (bus << 16) | (devfn << 8) | (reg & ~3))
16
Andi Kleen928cf8c2005-12-12 22:17:10 -080017int pci_conf1_read(unsigned int seg, unsigned int bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 unsigned int devfn, int reg, int len, u32 *value)
19{
20 unsigned long flags;
21
Andi Kleen49c93e82006-04-07 19:50:15 +020022 if (!value || (bus > 255) || (devfn > 255) || (reg > 255)) {
23 *value = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 return -EINVAL;
Andi Kleen49c93e82006-04-07 19:50:15 +020025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 spin_lock_irqsave(&pci_config_lock, flags);
28
29 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
30
31 switch (len) {
32 case 1:
33 *value = inb(0xCFC + (reg & 3));
34 break;
35 case 2:
36 *value = inw(0xCFC + (reg & 2));
37 break;
38 case 4:
39 *value = inl(0xCFC);
40 break;
41 }
42
43 spin_unlock_irqrestore(&pci_config_lock, flags);
44
45 return 0;
46}
47
Andi Kleen928cf8c2005-12-12 22:17:10 -080048int pci_conf1_write(unsigned int seg, unsigned int bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 unsigned int devfn, int reg, int len, u32 value)
50{
51 unsigned long flags;
52
53 if ((bus > 255) || (devfn > 255) || (reg > 255))
54 return -EINVAL;
55
56 spin_lock_irqsave(&pci_config_lock, flags);
57
58 outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8);
59
60 switch (len) {
61 case 1:
62 outb((u8)value, 0xCFC + (reg & 3));
63 break;
64 case 2:
65 outw((u16)value, 0xCFC + (reg & 2));
66 break;
67 case 4:
68 outl((u32)value, 0xCFC);
69 break;
70 }
71
72 spin_unlock_irqrestore(&pci_config_lock, flags);
73
74 return 0;
75}
76
77#undef PCI_CONF1_ADDRESS
78
79struct pci_raw_ops pci_direct_conf1 = {
80 .read = pci_conf1_read,
81 .write = pci_conf1_write,
82};
83
84
85/*
86 * Functions for accessing PCI configuration space with type 2 accesses
87 */
88
89#define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
90
91static int pci_conf2_read(unsigned int seg, unsigned int bus,
92 unsigned int devfn, int reg, int len, u32 *value)
93{
94 unsigned long flags;
95 int dev, fn;
96
97 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
98 return -EINVAL;
99
100 dev = PCI_SLOT(devfn);
101 fn = PCI_FUNC(devfn);
102
103 if (dev & 0x10)
104 return PCIBIOS_DEVICE_NOT_FOUND;
105
106 spin_lock_irqsave(&pci_config_lock, flags);
107
108 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
109 outb((u8)bus, 0xCFA);
110
111 switch (len) {
112 case 1:
113 *value = inb(PCI_CONF2_ADDRESS(dev, reg));
114 break;
115 case 2:
116 *value = inw(PCI_CONF2_ADDRESS(dev, reg));
117 break;
118 case 4:
119 *value = inl(PCI_CONF2_ADDRESS(dev, reg));
120 break;
121 }
122
123 outb(0, 0xCF8);
124
125 spin_unlock_irqrestore(&pci_config_lock, flags);
126
127 return 0;
128}
129
130static int pci_conf2_write(unsigned int seg, unsigned int bus,
131 unsigned int devfn, int reg, int len, u32 value)
132{
133 unsigned long flags;
134 int dev, fn;
135
136 if ((bus > 255) || (devfn > 255) || (reg > 255))
137 return -EINVAL;
138
139 dev = PCI_SLOT(devfn);
140 fn = PCI_FUNC(devfn);
141
142 if (dev & 0x10)
143 return PCIBIOS_DEVICE_NOT_FOUND;
144
145 spin_lock_irqsave(&pci_config_lock, flags);
146
147 outb((u8)(0xF0 | (fn << 1)), 0xCF8);
148 outb((u8)bus, 0xCFA);
149
150 switch (len) {
151 case 1:
152 outb((u8)value, PCI_CONF2_ADDRESS(dev, reg));
153 break;
154 case 2:
155 outw((u16)value, PCI_CONF2_ADDRESS(dev, reg));
156 break;
157 case 4:
158 outl((u32)value, PCI_CONF2_ADDRESS(dev, reg));
159 break;
160 }
161
162 outb(0, 0xCF8);
163
164 spin_unlock_irqrestore(&pci_config_lock, flags);
165
166 return 0;
167}
168
169#undef PCI_CONF2_ADDRESS
170
171static struct pci_raw_ops pci_direct_conf2 = {
172 .read = pci_conf2_read,
173 .write = pci_conf2_write,
174};
175
176
177/*
178 * Before we decide to use direct hardware access mechanisms, we try to do some
179 * trivial checks to ensure it at least _seems_ to be working -- we just test
180 * whether bus 00 contains a host bridge (this is similar to checking
181 * techniques used in XFree86, but ours should be more reliable since we
182 * attempt to make use of direct access hints provided by the PCI BIOS).
183 *
184 * This should be close to trivial, but it isn't, because there are buggy
185 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
186 */
187static int __init pci_sanity_check(struct pci_raw_ops *o)
188{
189 u32 x = 0;
190 int devfn;
191
192 if (pci_probe & PCI_NO_CHECKS)
193 return 1;
Andi Kleenec0f08e2006-04-07 19:49:36 +0200194 /* Assume Type 1 works for newer systems.
195 This handles machines that don't have anything on PCI Bus 0. */
196 if (dmi_get_year(DMI_BIOS_DATE) >= 2001)
197 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 for (devfn = 0; devfn < 0x100; devfn++) {
200 if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x))
201 continue;
202 if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)
203 return 1;
204
205 if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x))
206 continue;
207 if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)
208 return 1;
209 }
210
Daniel Marjamäkicac1a292005-11-23 15:45:09 -0800211 DBG(KERN_WARNING "PCI: Sanity check failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
213}
214
215static int __init pci_check_type1(void)
216{
217 unsigned long flags;
218 unsigned int tmp;
219 int works = 0;
220
221 local_irq_save(flags);
222
223 outb(0x01, 0xCFB);
224 tmp = inl(0xCF8);
225 outl(0x80000000, 0xCF8);
226 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) {
227 works = 1;
228 }
229 outl(tmp, 0xCF8);
230 local_irq_restore(flags);
231
232 return works;
233}
234
235static int __init pci_check_type2(void)
236{
237 unsigned long flags;
238 int works = 0;
239
240 local_irq_save(flags);
241
242 outb(0x00, 0xCFB);
243 outb(0x00, 0xCF8);
244 outb(0x00, 0xCFA);
245 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
246 pci_sanity_check(&pci_direct_conf2)) {
247 works = 1;
248 }
249
250 local_irq_restore(flags);
251
252 return works;
253}
254
Andi Kleen92c05fc2006-03-23 14:35:12 -0800255void __init pci_direct_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct resource *region, *region2;
258
259 if ((pci_probe & PCI_PROBE_CONF1) == 0)
260 goto type2;
261 region = request_region(0xCF8, 8, "PCI conf1");
262 if (!region)
263 goto type2;
264
265 if (pci_check_type1()) {
266 printk(KERN_INFO "PCI: Using configuration type 1\n");
267 raw_pci_ops = &pci_direct_conf1;
Andi Kleen92c05fc2006-03-23 14:35:12 -0800268 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 release_resource(region);
271
272 type2:
273 if ((pci_probe & PCI_PROBE_CONF2) == 0)
Andi Kleen92c05fc2006-03-23 14:35:12 -0800274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 region = request_region(0xCF8, 4, "PCI conf2");
276 if (!region)
Andi Kleen92c05fc2006-03-23 14:35:12 -0800277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 region2 = request_region(0xC000, 0x1000, "PCI conf2");
279 if (!region2)
280 goto fail2;
281
282 if (pci_check_type2()) {
283 printk(KERN_INFO "PCI: Using configuration type 2\n");
284 raw_pci_ops = &pci_direct_conf2;
Andi Kleen92c05fc2006-03-23 14:35:12 -0800285 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
288 release_resource(region2);
289 fail2:
290 release_resource(region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}