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