]> mj.ucw.cz Git - pciutils.git/blob - lib/i386-ports.c
cxl: Rename caps to be device caps
[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   intel_io_lock();
140   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
141
142   switch (len)
143     {
144     case 1:
145       buf[0] = inb(addr);
146       break;
147     case 2:
148       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
149       break;
150     case 4:
151       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
152       break;
153     default:
154       res = pci_generic_block_read(d, pos, buf, len);
155     }
156
157   intel_io_unlock();
158   return res;
159 }
160
161 static int
162 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
163 {
164   int addr = 0xcfc + (pos&3);
165   int res = 1;
166
167   if (d->domain || pos >= 256)
168     return 0;
169
170   intel_io_lock();
171   outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
172
173   switch (len)
174     {
175     case 1:
176       outb(buf[0], addr);
177       break;
178     case 2:
179       outw(le16_to_cpu(((u16 *) buf)[0]), addr);
180       break;
181     case 4:
182       outl(le32_to_cpu(((u32 *) buf)[0]), addr);
183       break;
184     default:
185       res = pci_generic_block_write(d, pos, buf, len);
186     }
187   intel_io_unlock();
188   return res;
189 }
190
191 /*
192  *      Configuration type 2. Obsolete and brain-damaged, but existing.
193  */
194
195 static int
196 conf2_detect(struct pci_access *a)
197 {
198   int res = 0;
199
200   if (!conf12_setup_io(a))
201     {
202       a->debug("...no I/O permission");
203       return 0;
204     }
205
206   /* This is ugly and tends to produce false positives. Beware. */
207
208   intel_io_lock();
209   outb(0x00, 0xCFB);
210   outb(0x00, 0xCF8);
211   outb(0x00, 0xCFA);
212   if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00)
213     res = intel_sanity_check(a, &pm_intel_conf2);
214   intel_io_unlock();
215   return res;
216 }
217
218 static int
219 conf2_read(struct pci_dev *d, int pos, byte *buf, int len)
220 {
221   int res = 1;
222   int addr = 0xc000 | (d->dev << 8) | pos;
223
224   if (d->domain || pos >= 256)
225     return 0;
226
227   if (d->dev >= 16)
228     /* conf2 supports only 16 devices per bus */
229     return 0;
230
231   intel_io_lock();
232   outb((d->func << 1) | 0xf0, 0xcf8);
233   outb(d->bus, 0xcfa);
234   switch (len)
235     {
236     case 1:
237       buf[0] = inb(addr);
238       break;
239     case 2:
240       ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
241       break;
242     case 4:
243       ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
244       break;
245     default:
246       res = pci_generic_block_read(d, pos, buf, len);
247     }
248   outb(0, 0xcf8);
249   intel_io_unlock();
250   return res;
251 }
252
253 static int
254 conf2_write(struct pci_dev *d, int pos, byte *buf, int len)
255 {
256   int res = 1;
257   int addr = 0xc000 | (d->dev << 8) | pos;
258
259   if (d->domain || pos >= 256)
260     return 0;
261
262   if (d->dev >= 16)
263     /* conf2 supports only 16 devices per bus */
264     return 0;
265
266   intel_io_lock();
267   outb((d->func << 1) | 0xf0, 0xcf8);
268   outb(d->bus, 0xcfa);
269   switch (len)
270     {
271     case 1:
272       outb(buf[0], addr);
273       break;
274     case 2:
275       outw(le16_to_cpu(* (u16 *) buf), addr);
276       break;
277     case 4:
278       outl(le32_to_cpu(* (u32 *) buf), addr);
279       break;
280     default:
281       res = pci_generic_block_write(d, pos, buf, len);
282     }
283
284   outb(0, 0xcf8);
285   intel_io_unlock();
286   return res;
287 }
288
289 struct pci_methods pm_intel_conf1 = {
290   "intel-conf1",
291   "Raw I/O port access using Intel conf1 interface",
292   NULL,                                 /* config */
293   conf1_detect,
294   conf12_init,
295   conf12_cleanup,
296   pci_generic_scan,
297   pci_generic_fill_info,
298   conf1_read,
299   conf1_write,
300   NULL,                                 /* read_vpd */
301   NULL,                                 /* init_dev */
302   NULL                                  /* cleanup_dev */
303 };
304
305 struct pci_methods pm_intel_conf2 = {
306   "intel-conf2",
307   "Raw I/O port access using Intel conf2 interface",
308   NULL,                                 /* config */
309   conf2_detect,
310   conf12_init,
311   conf12_cleanup,
312   pci_generic_scan,
313   pci_generic_fill_info,
314   conf2_read,
315   conf2_write,
316   NULL,                                 /* read_vpd */
317   NULL,                                 /* init_dev */
318   NULL                                  /* cleanup_dev */
319 };