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