2 * direct.c - Low-level direct PCI config space access
6 #include <linux/init.h>
8 #include <asm/pci_x86.h>
11 * Functions for accessing PCI base (first 256 bytes) and extended
12 * (4096 bytes per PCI function) configuration space with type 1
16 #define PCI_CONF1_ADDRESS(bus, devfn, reg) \
17 (0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \
18 | (devfn << 8) | (reg & 0xFC))
20 static int pci_conf1_read(unsigned int seg
, unsigned int bus
,
21 unsigned int devfn
, int reg
, int len
, u32
*value
)
25 if ((bus
> 255) || (devfn
> 255) || (reg
> 4095)) {
30 spin_lock_irqsave(&pci_config_lock
, flags
);
32 outl(PCI_CONF1_ADDRESS(bus
, devfn
, reg
), 0xCF8);
36 *value
= inb(0xCFC + (reg
& 3));
39 *value
= inw(0xCFC + (reg
& 2));
46 spin_unlock_irqrestore(&pci_config_lock
, flags
);
51 static int pci_conf1_write(unsigned int seg
, unsigned int bus
,
52 unsigned int devfn
, int reg
, int len
, u32 value
)
56 if ((bus
> 255) || (devfn
> 255) || (reg
> 4095))
59 spin_lock_irqsave(&pci_config_lock
, flags
);
61 outl(PCI_CONF1_ADDRESS(bus
, devfn
, reg
), 0xCF8);
65 outb((u8
)value
, 0xCFC + (reg
& 3));
68 outw((u16
)value
, 0xCFC + (reg
& 2));
71 outl((u32
)value
, 0xCFC);
75 spin_unlock_irqrestore(&pci_config_lock
, flags
);
80 #undef PCI_CONF1_ADDRESS
82 struct pci_raw_ops pci_direct_conf1
= {
83 .read
= pci_conf1_read
,
84 .write
= pci_conf1_write
,
89 * Functions for accessing PCI configuration space with type 2 accesses
92 #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg)
94 static int pci_conf2_read(unsigned int seg
, unsigned int bus
,
95 unsigned int devfn
, int reg
, int len
, u32
*value
)
100 if ((bus
> 255) || (devfn
> 255) || (reg
> 255)) {
105 dev
= PCI_SLOT(devfn
);
106 fn
= PCI_FUNC(devfn
);
109 return PCIBIOS_DEVICE_NOT_FOUND
;
111 spin_lock_irqsave(&pci_config_lock
, flags
);
113 outb((u8
)(0xF0 | (fn
<< 1)), 0xCF8);
114 outb((u8
)bus
, 0xCFA);
118 *value
= inb(PCI_CONF2_ADDRESS(dev
, reg
));
121 *value
= inw(PCI_CONF2_ADDRESS(dev
, reg
));
124 *value
= inl(PCI_CONF2_ADDRESS(dev
, reg
));
130 spin_unlock_irqrestore(&pci_config_lock
, flags
);
135 static int pci_conf2_write(unsigned int seg
, unsigned int bus
,
136 unsigned int devfn
, int reg
, int len
, u32 value
)
141 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
144 dev
= PCI_SLOT(devfn
);
145 fn
= PCI_FUNC(devfn
);
148 return PCIBIOS_DEVICE_NOT_FOUND
;
150 spin_lock_irqsave(&pci_config_lock
, flags
);
152 outb((u8
)(0xF0 | (fn
<< 1)), 0xCF8);
153 outb((u8
)bus
, 0xCFA);
157 outb((u8
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
160 outw((u16
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
163 outl((u32
)value
, PCI_CONF2_ADDRESS(dev
, reg
));
169 spin_unlock_irqrestore(&pci_config_lock
, flags
);
174 #undef PCI_CONF2_ADDRESS
176 struct pci_raw_ops pci_direct_conf2
= {
177 .read
= pci_conf2_read
,
178 .write
= pci_conf2_write
,
183 * Before we decide to use direct hardware access mechanisms, we try to do some
184 * trivial checks to ensure it at least _seems_ to be working -- we just test
185 * whether bus 00 contains a host bridge (this is similar to checking
186 * techniques used in XFree86, but ours should be more reliable since we
187 * attempt to make use of direct access hints provided by the PCI BIOS).
189 * This should be close to trivial, but it isn't, because there are buggy
190 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
192 static int __init
pci_sanity_check(struct pci_raw_ops
*o
)
197 if (pci_probe
& PCI_NO_CHECKS
)
199 /* Assume Type 1 works for newer systems.
200 This handles machines that don't have anything on PCI Bus 0. */
201 dmi_get_date(DMI_BIOS_DATE
, &year
, NULL
, NULL
);
205 for (devfn
= 0; devfn
< 0x100; devfn
++) {
206 if (o
->read(0, 0, devfn
, PCI_CLASS_DEVICE
, 2, &x
))
208 if (x
== PCI_CLASS_BRIDGE_HOST
|| x
== PCI_CLASS_DISPLAY_VGA
)
211 if (o
->read(0, 0, devfn
, PCI_VENDOR_ID
, 2, &x
))
213 if (x
== PCI_VENDOR_ID_INTEL
|| x
== PCI_VENDOR_ID_COMPAQ
)
217 DBG(KERN_WARNING
"PCI: Sanity check failed\n");
221 static int __init
pci_check_type1(void)
227 local_irq_save(flags
);
231 outl(0x80000000, 0xCF8);
232 if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1
)) {
236 local_irq_restore(flags
);
241 static int __init
pci_check_type2(void)
246 local_irq_save(flags
);
251 if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 &&
252 pci_sanity_check(&pci_direct_conf2
)) {
256 local_irq_restore(flags
);
261 void __init
pci_direct_init(int type
)
265 printk(KERN_INFO
"PCI: Using configuration type %d for base access\n",
268 raw_pci_ops
= &pci_direct_conf1
;
271 if (!(pci_probe
& PCI_HAS_IO_ECS
))
273 printk(KERN_INFO
"PCI: Using configuration type 1 "
274 "for extended access\n");
275 raw_pci_ext_ops
= &pci_direct_conf1
;
278 raw_pci_ops
= &pci_direct_conf2
;
281 int __init
pci_direct_probe(void)
283 struct resource
*region
, *region2
;
285 if ((pci_probe
& PCI_PROBE_CONF1
) == 0)
287 region
= request_region(0xCF8, 8, "PCI conf1");
291 if (pci_check_type1()) {
292 raw_pci_ops
= &pci_direct_conf1
;
293 port_cf9_safe
= true;
296 release_resource(region
);
299 if ((pci_probe
& PCI_PROBE_CONF2
) == 0)
301 region
= request_region(0xCF8, 4, "PCI conf2");
304 region2
= request_region(0xC000, 0x1000, "PCI conf2");
308 if (pci_check_type2()) {
309 raw_pci_ops
= &pci_direct_conf2
;
310 port_cf9_safe
= true;
314 release_resource(region2
);
316 release_resource(region
);