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