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