]> mj.ucw.cz Git - pciutils.git/blob - lib/i386-ports.c
8b7ceb7f71c76f493606d256f4e4f6eca5e4ee15
[pciutils.git] / lib / i386-ports.c
1 /*
2  *      The PCI Library -- Direct Configuration access via i386 Ports
3  *
4  *      Copyright (c) 1997--2006 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #define _GNU_SOURCE
10
11 #include "internal.h"
12
13 #include <unistd.h>
14
15 #if defined(PCI_OS_LINUX)
16 #include "i386-io-linux.h"
17 #elif defined(PCI_OS_GNU)
18 #include "i386-io-hurd.h"
19 #elif defined(PCI_OS_SUNOS)
20 #include "i386-io-sunos.h"
21 #elif defined(PCI_OS_WINDOWS)
22 #include "i386-io-windows.h"
23 #elif defined(PCI_OS_CYGWIN)
24 #include "i386-io-cygwin.h"
25 #elif defined(PCI_OS_HAIKU)
26 #include "i386-io-haiku.h"
27 #elif defined(PCI_OS_BEOS)
28 #include "i386-io-beos.h"
29 #elif defined(PCI_OS_DJGPP)
30 #include "i386-io-djgpp.h"
31 #else
32 #error Do not know how to access I/O ports on this OS.
33 #endif
34
35 static int conf12_io_enabled = -1;              /* -1=haven't tried, 0=failed, 1=succeeded */
36
37 static int
38 conf12_setup_io(struct pci_access *a)
39 {
40   if (conf12_io_enabled < 0)
41     conf12_io_enabled = intel_setup_io(a);
42   return conf12_io_enabled;
43 }
44
45 static void
46 conf12_init(struct pci_access *a)
47 {
48   if (!conf12_setup_io(a))
49     a->error("No permission to access I/O ports (you probably have to be root).");
50 }
51
52 static void
53 conf12_cleanup(struct pci_access *a UNUSED)
54 {
55   if (conf12_io_enabled > 0)
56     {
57       intel_cleanup_io(a);
58       conf12_io_enabled = -1;
59     }
60 }
61
62 /*
63  * Before we decide to use direct hardware access mechanisms, we try to do some
64  * trivial checks to ensure it at least _seems_ to be working -- we just test
65  * whether bus 00 contains a host bridge (this is similar to checking
66  * techniques used in XFree86, but ours should be more reliable since we
67  * attempt to make use of direct access hints provided by the PCI BIOS).
68  *
69  * This should be close to trivial, but it isn't, because there are buggy
70  * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
71  */
72
73 static int
74 intel_sanity_check(struct pci_access *a, struct pci_methods *m)
75 {
76   struct pci_dev d;
77
78   a->debug("...sanity check");
79   d.bus = 0;
80   d.func = 0;
81   for (d.dev = 0; d.dev < 32; d.dev++)
82     {
83       u16 class, vendor;
84       if (m->read(&d, PCI_CLASS_DEVICE, (byte *) &class, sizeof(class)) &&
85           (class == cpu_to_le16(PCI_CLASS_BRIDGE_HOST) || class == cpu_to_le16(PCI_CLASS_DISPLAY_VGA)) ||
86           m->read(&d, PCI_VENDOR_ID, (byte *) &vendor, sizeof(vendor)) &&
87           (vendor == cpu_to_le16(PCI_VENDOR_ID_INTEL) || vendor == cpu_to_le16(PCI_VENDOR_ID_COMPAQ)))
88         {
89           a->debug("...outside the Asylum at 0/%02x/0", d.dev);
90           return 1;
91         }
92     }
93   a->debug("...insane");
94   return 0;
95 }
96
97 /*
98  *      Configuration type 1
99  */
100
101 #define CONFIG_CMD(bus, device_fn, where)   (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
102
103 static int
104 conf1_detect(struct pci_access *a)
105 {
106   unsigned int tmp;
107   int res = 0;
108
109   if (!conf12_setup_io(a))
110     {
111       a->debug("...no I/O permission");
112       return 0;
113     }
114
115   intel_io_lock();
116   outb (0x01, 0xCFB);
117   tmp = inl (0xCF8);
118   outl (0x80000000, 0xCF8);
119   if (inl (0xCF8) == 0x80000000)
120     res = 1;
121   outl (tmp, 0xCF8);
122   intel_io_unlock();
123
124   if (res)
125     res = intel_sanity_check(a, &pm_intel_conf1);
126   return res;
127 }
128
129 static int
130 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
131 {
132   int addr = 0xcfc + (pos&3);
133   int res = 1;
134
135   if (d->domain || pos >= 256)
136     return 0;
137
138   intel_io_lock();
139   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
140
141   switch (len)
142     {
143     case 1:
144       buf[0] = inb(addr);
145       break;
146     case 2:
147       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
148       break;
149     case 4:
150       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
151       break;
152     default:
153       res = pci_generic_block_read(d, pos, buf, len);
154     }
155
156   intel_io_unlock();
157   return res;
158 }
159
160 static int
161 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
162 {
163   int addr = 0xcfc + (pos&3);
164   int res = 1;
165
166   if (d->domain || pos >= 256)
167     return 0;
168
169   intel_io_lock();
170   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
171
172   switch (len)
173     {
174     case 1:
175       outb(buf[0], addr);
176       break;
177     case 2:
178       outw(le16_to_cpu(((u16 *) buf)[0]), addr);
179       break;
180     case 4:
181       outl(le32_to_cpu(((u32 *) buf)[0]), addr);
182       break;
183     default:
184       res = pci_generic_block_write(d, pos, buf, len);
185     }
186   intel_io_unlock();
187   return res;
188 }
189
190 /*
191  *      Configuration type 2. Obsolete and brain-damaged, but existing.
192  */
193
194 static int
195 conf2_detect(struct pci_access *a)
196 {
197   int res = 0;
198
199   if (!conf12_setup_io(a))
200     {
201       a->debug("...no I/O permission");
202       return 0;
203     }
204
205   /* This is ugly and tends to produce false positives. Beware. */
206
207   intel_io_lock();
208   outb(0x00, 0xCFB);
209   outb(0x00, 0xCF8);
210   outb(0x00, 0xCFA);
211   if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00)
212     res = intel_sanity_check(a, &pm_intel_conf2);
213   intel_io_unlock();
214   return res;
215 }
216
217 static int
218 conf2_read(struct pci_dev *d, int pos, byte *buf, int len)
219 {
220   int res = 1;
221   int addr = 0xc000 | (d->dev << 8) | pos;
222
223   if (d->domain || pos >= 256)
224     return 0;
225
226   if (d->dev >= 16)
227     /* conf2 supports only 16 devices per bus */
228     return 0;
229
230   intel_io_lock();
231   outb((d->func << 1) | 0xf0, 0xcf8);
232   outb(d->bus, 0xcfa);
233   switch (len)
234     {
235     case 1:
236       buf[0] = inb(addr);
237       break;
238     case 2:
239       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
240       break;
241     case 4:
242       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
243       break;
244     default:
245       res = pci_generic_block_read(d, pos, buf, len);
246     }
247   outb(0, 0xcf8);
248   intel_io_unlock();
249   return res;
250 }
251
252 static int
253 conf2_write(struct pci_dev *d, int pos, byte *buf, int len)
254 {
255   int res = 1;
256   int addr = 0xc000 | (d->dev << 8) | pos;
257
258   if (d->domain || pos >= 256)
259     return 0;
260
261   if (d->dev >= 16)
262     /* conf2 supports only 16 devices per bus */
263     return 0;
264
265   intel_io_lock();
266   outb((d->func << 1) | 0xf0, 0xcf8);
267   outb(d->bus, 0xcfa);
268   switch (len)
269     {
270     case 1:
271       outb(buf[0], addr);
272       break;
273     case 2:
274       outw(le16_to_cpu(* (u16 *) buf), addr);
275       break;
276     case 4:
277       outl(le32_to_cpu(* (u32 *) buf), addr);
278       break;
279     default:
280       res = pci_generic_block_write(d, pos, buf, len);
281     }
282
283   outb(0, 0xcf8);
284   intel_io_unlock();
285   return res;
286 }
287
288 struct pci_methods pm_intel_conf1 = {
289   "intel-conf1",
290   "Raw I/O port access using Intel conf1 interface",
291   NULL,                                 /* config */
292   conf1_detect,
293   conf12_init,
294   conf12_cleanup,
295   pci_generic_scan,
296   pci_generic_fill_info,
297   conf1_read,
298   conf1_write,
299   NULL,                                 /* read_vpd */
300   NULL,                                 /* init_dev */
301   NULL                                  /* cleanup_dev */
302 };
303
304 struct pci_methods pm_intel_conf2 = {
305   "intel-conf2",
306   "Raw I/O port access using Intel conf2 interface",
307   NULL,                                 /* config */
308   conf2_detect,
309   conf12_init,
310   conf12_cleanup,
311   pci_generic_scan,
312   pci_generic_fill_info,
313   conf2_read,
314   conf2_write,
315   NULL,                                 /* read_vpd */
316   NULL,                                 /* init_dev */
317   NULL                                  /* cleanup_dev */
318 };