]> mj.ucw.cz Git - pciutils.git/blob - lib/i386-ports.c
Fix stripping in cross-compiling mode
[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 <string.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)
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   memset(&d, 0, sizeof(d));
79   a->debug("...sanity check");
80   d.bus = 0;
81   d.func = 0;
82   for (d.dev = 0; d.dev < 32; d.dev++)
83     {
84       u16 class, vendor;
85       if (m->read(&d, PCI_CLASS_DEVICE, (byte *) &class, sizeof(class)) &&
86           (class == cpu_to_le16(PCI_CLASS_BRIDGE_HOST) || class == cpu_to_le16(PCI_CLASS_DISPLAY_VGA)) ||
87           m->read(&d, PCI_VENDOR_ID, (byte *) &vendor, sizeof(vendor)) &&
88           (vendor == cpu_to_le16(PCI_VENDOR_ID_INTEL) || vendor == cpu_to_le16(PCI_VENDOR_ID_COMPAQ)))
89         {
90           a->debug("...outside the Asylum at 0/%02x/0", d.dev);
91           return 1;
92         }
93     }
94   a->debug("...insane");
95   return 0;
96 }
97
98 /*
99  *      Configuration type 1
100  */
101
102 #define CONFIG_CMD(bus, device_fn, where)   (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
103
104 static int
105 conf1_detect(struct pci_access *a)
106 {
107   unsigned int tmp;
108   int res = 0;
109
110   if (!conf12_setup_io(a))
111     {
112       a->debug("...no I/O permission");
113       return 0;
114     }
115
116   intel_io_lock();
117   outb (0x01, 0xCFB);
118   tmp = inl (0xCF8);
119   outl (0x80000000, 0xCF8);
120   if (inl (0xCF8) == 0x80000000)
121     res = 1;
122   outl (tmp, 0xCF8);
123   intel_io_unlock();
124
125   if (res)
126     res = intel_sanity_check(a, &pm_intel_conf1);
127   return res;
128 }
129
130 static int
131 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
132 {
133   int addr = 0xcfc + (pos&3);
134   int res = 1;
135
136   if (d->domain || pos >= 256)
137     return 0;
138
139   if (len != 1 && len != 2 && len != 4)
140     return pci_generic_block_read(d, pos, buf, len);
141
142   intel_io_lock();
143   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
144
145   switch (len)
146     {
147     case 1:
148       buf[0] = inb(addr);
149       break;
150     case 2:
151       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
152       break;
153     case 4:
154       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
155       break;
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   if (len != 1 && len != 2 && len != 4)
172     return pci_generic_block_write(d, pos, buf, len);
173
174   intel_io_lock();
175   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
176
177   switch (len)
178     {
179     case 1:
180       outb(buf[0], addr);
181       break;
182     case 2:
183       outw(le16_to_cpu(((u16 *) buf)[0]), addr);
184       break;
185     case 4:
186       outl(le32_to_cpu(((u32 *) buf)[0]), addr);
187       break;
188     }
189   intel_io_unlock();
190   return res;
191 }
192
193 /*
194  *      Configuration type 2. Obsolete and brain-damaged, but existing.
195  */
196
197 static int
198 conf2_detect(struct pci_access *a)
199 {
200   int res = 0;
201
202   if (!conf12_setup_io(a))
203     {
204       a->debug("...no I/O permission");
205       return 0;
206     }
207
208   /* This is ugly and tends to produce false positives. Beware. */
209
210   intel_io_lock();
211   outb(0x00, 0xCFB);
212   outb(0x00, 0xCF8);
213   outb(0x00, 0xCFA);
214   if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00)
215     res = intel_sanity_check(a, &pm_intel_conf2);
216   intel_io_unlock();
217   return res;
218 }
219
220 static int
221 conf2_read(struct pci_dev *d, int pos, byte *buf, int len)
222 {
223   int res = 1;
224   int addr = 0xc000 | (d->dev << 8) | pos;
225
226   if (d->domain || pos >= 256)
227     return 0;
228
229   if (d->dev >= 16)
230     /* conf2 supports only 16 devices per bus */
231     return 0;
232
233   if (len != 1 && len != 2 && len != 4)
234     return pci_generic_block_read(d, pos, buf, len);
235
236   intel_io_lock();
237   outb((d->func << 1) | 0xf0, 0xcf8);
238   outb(d->bus, 0xcfa);
239   switch (len)
240     {
241     case 1:
242       buf[0] = inb(addr);
243       break;
244     case 2:
245       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
246       break;
247     case 4:
248       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
249       break;
250     }
251   outb(0, 0xcf8);
252   intel_io_unlock();
253   return res;
254 }
255
256 static int
257 conf2_write(struct pci_dev *d, int pos, byte *buf, int len)
258 {
259   int res = 1;
260   int addr = 0xc000 | (d->dev << 8) | pos;
261
262   if (d->domain || pos >= 256)
263     return 0;
264
265   if (d->dev >= 16)
266     /* conf2 supports only 16 devices per bus */
267     return 0;
268
269   if (len != 1 && len != 2 && len != 4)
270     return pci_generic_block_write(d, pos, buf, len);
271
272   intel_io_lock();
273   outb((d->func << 1) | 0xf0, 0xcf8);
274   outb(d->bus, 0xcfa);
275   switch (len)
276     {
277     case 1:
278       outb(buf[0], addr);
279       break;
280     case 2:
281       outw(le16_to_cpu(* (u16 *) buf), addr);
282       break;
283     case 4:
284       outl(le32_to_cpu(* (u32 *) buf), addr);
285       break;
286     }
287
288   outb(0, 0xcf8);
289   intel_io_unlock();
290   return res;
291 }
292
293 struct pci_methods pm_intel_conf1 = {
294   "intel-conf1",
295   "Raw I/O port access using Intel conf1 interface",
296   NULL,                                 /* config */
297   conf1_detect,
298   conf12_init,
299   conf12_cleanup,
300   pci_generic_scan,
301   pci_generic_fill_info,
302   conf1_read,
303   conf1_write,
304   NULL,                                 /* read_vpd */
305   NULL,                                 /* init_dev */
306   NULL                                  /* cleanup_dev */
307 };
308
309 struct pci_methods pm_intel_conf2 = {
310   "intel-conf2",
311   "Raw I/O port access using Intel conf2 interface",
312   NULL,                                 /* config */
313   conf2_detect,
314   conf12_init,
315   conf12_cleanup,
316   pci_generic_scan,
317   pci_generic_fill_info,
318   conf2_read,
319   conf2_write,
320   NULL,                                 /* read_vpd */
321   NULL,                                 /* init_dev */
322   NULL                                  /* cleanup_dev */
323 };