2 * The PCI Library -- Direct Configuration access via i386 Ports
4 * Copyright (c) 1997--2006 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
16 #if defined(PCI_OS_LINUX)
17 #include "i386-io-linux.h"
18 #elif defined(PCI_OS_GNU)
19 #include "i386-io-hurd.h"
20 #elif defined(PCI_OS_SUNOS)
21 #include "i386-io-sunos.h"
22 #elif defined(PCI_OS_WINDOWS)
23 #include "i386-io-windows.h"
24 #elif defined(PCI_OS_CYGWIN)
25 #include "i386-io-cygwin.h"
26 #elif defined(PCI_OS_HAIKU)
27 #include "i386-io-haiku.h"
28 #elif defined(PCI_OS_BEOS)
29 #include "i386-io-beos.h"
30 #elif defined(PCI_OS_DJGPP)
31 #include "i386-io-djgpp.h"
33 #error Do not know how to access I/O ports on this OS.
36 static int conf12_io_enabled = -1; /* -1=haven't tried, 0=failed, 1=succeeded */
39 conf12_setup_io(struct pci_access *a)
41 if (conf12_io_enabled < 0)
42 conf12_io_enabled = intel_setup_io(a);
43 return conf12_io_enabled;
47 conf12_init(struct pci_access *a)
49 if (!conf12_setup_io(a))
50 a->error("No permission to access I/O ports (you probably have to be root).");
54 conf12_cleanup(struct pci_access *a)
56 if (conf12_io_enabled > 0)
59 conf12_io_enabled = -1;
64 * Before we decide to use direct hardware access mechanisms, we try to do some
65 * trivial checks to ensure it at least _seems_ to be working -- we just test
66 * whether bus 00 contains a host bridge (this is similar to checking
67 * techniques used in XFree86, but ours should be more reliable since we
68 * attempt to make use of direct access hints provided by the PCI BIOS).
70 * This should be close to trivial, but it isn't, because there are buggy
71 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
75 intel_sanity_check(struct pci_access *a, struct pci_methods *m)
79 memset(&d, 0, sizeof(d));
80 a->debug("...sanity check");
83 for (d.dev = 0; d.dev < 32; d.dev++)
86 if (m->read(&d, PCI_CLASS_DEVICE, (byte *) &class, sizeof(class)) &&
87 (class == cpu_to_le16(PCI_CLASS_BRIDGE_HOST) || class == cpu_to_le16(PCI_CLASS_DISPLAY_VGA)) ||
88 m->read(&d, PCI_VENDOR_ID, (byte *) &vendor, sizeof(vendor)) &&
89 (vendor == cpu_to_le16(PCI_VENDOR_ID_INTEL) || vendor == cpu_to_le16(PCI_VENDOR_ID_COMPAQ)))
91 a->debug("...outside the Asylum at 0/%02x/0", d.dev);
95 a->debug("...insane");
100 * Configuration type 1
103 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
106 conf1_detect(struct pci_access *a)
111 if (!conf12_setup_io(a))
113 a->debug("...no I/O permission");
120 outl (0x80000000, 0xCF8);
121 if (inl (0xCF8) == 0x80000000)
127 res = intel_sanity_check(a, &pm_intel_conf1);
132 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
134 int addr = 0xcfc + (pos&3);
137 if (d->domain || pos >= 256)
141 outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
149 ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
152 ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
155 res = pci_generic_block_read(d, pos, buf, len);
163 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
165 int addr = 0xcfc + (pos&3);
168 if (d->domain || pos >= 256)
172 outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
180 outw(le16_to_cpu(((u16 *) buf)[0]), addr);
183 outl(le32_to_cpu(((u32 *) buf)[0]), addr);
186 res = pci_generic_block_write(d, pos, buf, len);
193 * Configuration type 2. Obsolete and brain-damaged, but existing.
197 conf2_detect(struct pci_access *a)
201 if (!conf12_setup_io(a))
203 a->debug("...no I/O permission");
207 /* This is ugly and tends to produce false positives. Beware. */
213 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00)
214 res = intel_sanity_check(a, &pm_intel_conf2);
220 conf2_read(struct pci_dev *d, int pos, byte *buf, int len)
223 int addr = 0xc000 | (d->dev << 8) | pos;
225 if (d->domain || pos >= 256)
229 /* conf2 supports only 16 devices per bus */
233 outb((d->func << 1) | 0xf0, 0xcf8);
241 ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
244 ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
247 res = pci_generic_block_read(d, pos, buf, len);
255 conf2_write(struct pci_dev *d, int pos, byte *buf, int len)
258 int addr = 0xc000 | (d->dev << 8) | pos;
260 if (d->domain || pos >= 256)
264 /* conf2 supports only 16 devices per bus */
268 outb((d->func << 1) | 0xf0, 0xcf8);
276 outw(le16_to_cpu(* (u16 *) buf), addr);
279 outl(le32_to_cpu(* (u32 *) buf), addr);
282 res = pci_generic_block_write(d, pos, buf, len);
290 struct pci_methods pm_intel_conf1 = {
292 "Raw I/O port access using Intel conf1 interface",
298 pci_generic_fill_info,
303 NULL /* cleanup_dev */
306 struct pci_methods pm_intel_conf2 = {
308 "Raw I/O port access using Intel conf2 interface",
314 pci_generic_fill_info,
319 NULL /* cleanup_dev */