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