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.
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"
26 #error Do not know how to access I/O ports on this OS.
29 static int conf12_io_enabled = -1; /* -1=haven't tried, 0=failed, 1=succeeded */
32 conf12_setup_io(struct pci_access *a)
34 if (conf12_io_enabled < 0)
35 conf12_io_enabled = intel_setup_io(a);
36 return conf12_io_enabled;
40 conf12_init(struct pci_access *a)
42 if (!conf12_setup_io(a))
43 a->error("No permission to access I/O ports (you probably have to be root).");
47 conf12_cleanup(struct pci_access *a UNUSED)
49 if (conf12_io_enabled > 0)
50 conf12_io_enabled = intel_cleanup_io(a);
54 * Before we decide to use direct hardware access mechanisms, we try to do some
55 * trivial checks to ensure it at least _seems_ to be working -- we just test
56 * whether bus 00 contains a host bridge (this is similar to checking
57 * techniques used in XFree86, but ours should be more reliable since we
58 * attempt to make use of direct access hints provided by the PCI BIOS).
60 * This should be close to trivial, but it isn't, because there are buggy
61 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
65 intel_sanity_check(struct pci_access *a, struct pci_methods *m)
69 a->debug("...sanity check");
72 for(d.dev = 0; d.dev < 32; d.dev++)
75 if (m->read(&d, PCI_CLASS_DEVICE, (byte *) &class, sizeof(class)) &&
76 (class == cpu_to_le16(PCI_CLASS_BRIDGE_HOST) || class == cpu_to_le16(PCI_CLASS_DISPLAY_VGA)) ||
77 m->read(&d, PCI_VENDOR_ID, (byte *) &vendor, sizeof(vendor)) &&
78 (vendor == cpu_to_le16(PCI_VENDOR_ID_INTEL) || vendor == cpu_to_le16(PCI_VENDOR_ID_COMPAQ)))
80 a->debug("...outside the Asylum at 0/%02x/0", d.dev);
84 a->debug("...insane");
89 * Configuration type 1
92 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
95 conf1_detect(struct pci_access *a)
100 if (!conf12_setup_io(a))
102 a->debug("...no I/O permission");
107 outl (0x80000000, 0xCF8);
108 if (inl (0xCF8) == 0x80000000)
112 res = intel_sanity_check(a, &pm_intel_conf1);
117 conf1_read(struct pci_dev *d, int pos, byte *buf, int len)
119 int addr = 0xcfc + (pos&3);
124 outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
132 ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
135 ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
138 return pci_generic_block_read(d, pos, buf, len);
144 conf1_write(struct pci_dev *d, int pos, byte *buf, int len)
146 int addr = 0xcfc + (pos&3);
151 outl(0x80000000 | ((d->bus & 0xff) << 16) | (PCI_DEVFN(d->dev, d->func) << 8) | (pos&~3), 0xcf8);
159 outw(le16_to_cpu(((u16 *) buf)[0]), addr);
162 outl(le32_to_cpu(((u32 *) buf)[0]), addr);
165 return pci_generic_block_write(d, pos, buf, len);
171 * Configuration type 2. Obsolete and brain-damaged, but existing.
175 conf2_detect(struct pci_access *a)
177 if (!conf12_setup_io(a))
179 a->debug("...no I/O permission");
183 /* This is ugly and tends to produce false positives. Beware. */
188 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00)
189 return intel_sanity_check(a, &pm_intel_conf2);
195 conf2_read(struct pci_dev *d, int pos, byte *buf, int len)
197 int addr = 0xc000 | (d->dev << 8) | pos;
203 /* conf2 supports only 16 devices per bus */
205 outb((d->func << 1) | 0xf0, 0xcf8);
213 ((u16 *) buf)[0] = cpu_to_le16(inw(addr));
216 ((u32 *) buf)[0] = cpu_to_le32(inl(addr));
220 return pci_generic_block_read(d, pos, buf, len);
227 conf2_write(struct pci_dev *d, int pos, byte *buf, int len)
229 int addr = 0xc000 | (d->dev << 8) | pos;
235 d->access->error("conf2_write: only first 16 devices exist.");
236 outb((d->func << 1) | 0xf0, 0xcf8);
244 outw(le16_to_cpu(* (u16 *) buf), addr);
247 outl(le32_to_cpu(* (u32 *) buf), addr);
251 return pci_generic_block_write(d, pos, buf, len);
257 struct pci_methods pm_intel_conf1 = {
259 "Raw I/O port access using Intel conf1 interface",
265 pci_generic_fill_info,
269 NULL /* cleanup_dev */
272 struct pci_methods pm_intel_conf2 = {
274 "Raw I/O port access using Intel conf2 interface",
280 pci_generic_fill_info,
284 NULL /* cleanup_dev */