Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / arch / i386 / kernel / pci-i386.c
blob53349bd0a665c002f8707e0e7d7b2ffaca047c80
1 /*
2 * Low-Level PCI Access for i386 machines
4 * Copyright 1993, 1994 Drew Eckhardt
5 * Visionary Computing
6 * (Unix and Linux consulting and custom programming)
7 * Drew@Colorado.EDU
8 * +1 (303) 786-7975
10 * Drew's work was sponsored by:
11 * iX Multiuser Multitasking Magazine
12 * Hannover, Germany
13 * hm@ix.de
15 * Copyright 1997--2000 Martin Mares <mj@suse.cz>
17 * For more information, please consult the following manuals (look at
18 * http://www.pcisig.com/ for how to get them):
20 * PCI BIOS Specification
21 * PCI Local Bus Specification
22 * PCI to PCI Bridge Specification
23 * PCI System Design Guide
26 * CHANGELOG :
27 * Jun 17, 1994 : Modified to accommodate the broken pre-PCI BIOS SPECIFICATION
28 * Revision 2.0 present on <thys@dennis.ee.up.ac.za>'s ASUS mainboard.
30 * Jan 5, 1995 : Modified to probe PCI hardware at boot time by Frederic
31 * Potter, potter@cao-vlsi.ibp.fr
33 * Jan 10, 1995 : Modified to store the information about configured pci
34 * devices into a list, which can be accessed via /proc/pci by
35 * Curtis Varner, cvarner@cs.ucr.edu
37 * Jan 12, 1995 : CPU-PCI bridge optimization support by Frederic Potter.
38 * Alpha version. Intel & UMC chipset support only.
40 * Apr 16, 1995 : Source merge with the DEC Alpha PCI support. Most of the code
41 * moved to drivers/pci/pci.c.
43 * Dec 7, 1996 : Added support for direct configuration access of boards
44 * with Intel compatible access schemes (tsbogend@alpha.franken.de)
46 * Feb 3, 1997 : Set internal functions to static, save/restore flags
47 * avoid dead locks reading broken PCI BIOS, werner@suse.de
49 * Apr 26, 1997 : Fixed case when there is BIOS32, but not PCI BIOS
50 * (mj@atrey.karlin.mff.cuni.cz)
52 * May 7, 1997 : Added some missing cli()'s. [mj]
54 * Jun 20, 1997 : Corrected problems in "conf1" type accesses.
55 * (paubert@iram.es)
57 * Aug 2, 1997 : Split to PCI BIOS handling and direct PCI access parts
58 * and cleaned it up... Martin Mares <mj@atrey.karlin.mff.cuni.cz>
60 * Feb 6, 1998 : No longer using BIOS to find devices and device classes. [mj]
62 * May 1, 1998 : Support for peer host bridges. [mj]
64 * Jun 19, 1998 : Changed to use spinlocks, so that PCI configuration space
65 * can be accessed from interrupts even on SMP systems. [mj]
67 * August 1998 : Better support for peer host bridges and more paranoid
68 * checks for direct hardware access. Ugh, this file starts to look as
69 * a large gallery of common hardware bug workarounds (watch the comments)
70 * -- the PCI specs themselves are sane, but most implementors should be
71 * hit hard with \hammer scaled \magstep5. [mj]
73 * Jan 23, 1999 : More improvements to peer host bridge logic. i450NX fixup. [mj]
75 * Feb 8, 1999 : Added UM8886BF I/O address fixup. [mj]
77 * August 1999 : New resource management and configuration access stuff. [mj]
79 * Sep 19, 1999 : Use PCI IRQ routing tables for detection of peer host bridges.
80 * Based on ideas by Chris Frantz and David Hinds. [mj]
82 * Sep 28, 1999 : Handle unreported/unassigned IRQs. Thanks to Shuu Yamaguchi
83 * for a lot of patience during testing. [mj]
85 * Oct 8, 1999 : Split to pci-i386.c, pci-pc.c and pci-visws.c. [mj]
88 #include <linux/types.h>
89 #include <linux/kernel.h>
90 #include <linux/pci.h>
91 #include <linux/init.h>
92 #include <linux/ioport.h>
93 #include <linux/errno.h>
95 #include "pci-i386.h"
97 void
98 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
99 struct resource *res, int resource)
101 u32 new, check;
102 int reg;
104 new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
105 if (resource < 6) {
106 reg = PCI_BASE_ADDRESS_0 + 4*resource;
107 } else if (resource == PCI_ROM_RESOURCE) {
108 res->flags |= PCI_ROM_ADDRESS_ENABLE;
109 reg = dev->rom_base_reg;
110 } else {
111 /* Somebody might have asked allocation of a non-standard resource */
112 return;
115 pci_write_config_dword(dev, reg, new);
116 pci_read_config_dword(dev, reg, &check);
117 if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
118 printk(KERN_ERR "PCI: Error while updating region "
119 "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
120 new, check);
125 * We need to avoid collisions with `mirrored' VGA ports
126 * and other strange ISA hardware, so we always want the
127 * addresses to be allocated in the 0x000-0x0ff region
128 * modulo 0x400.
130 * Why? Because some silly external IO cards only decode
131 * the low 10 bits of the IO address. The 0x00-0xff region
132 * is reserved for motherboard devices that decode all 16
133 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
134 * but we want to try to avoid allocating at 0x2900-0x2bff
135 * which might have be mirrored at 0x0100-0x03ff..
137 void
138 pcibios_align_resource(void *data, struct resource *res, unsigned long size)
140 struct pci_dev *dev = data;
142 if (res->flags & IORESOURCE_IO) {
143 unsigned long start = res->start;
145 if (size > 0x100) {
146 printk(KERN_ERR "PCI: I/O Region %s/%d too large"
147 " (%ld bytes)\n", dev->slot_name,
148 dev->resource - res, size);
151 if (start & 0x300) {
152 start = (start + 0x3ff) & ~0x3ff;
153 res->start = start;
160 * Handle resources of PCI devices. If the world were perfect, we could
161 * just allocate all the resource regions and do nothing more. It isn't.
162 * On the other hand, we cannot just re-allocate all devices, as it would
163 * require us to know lots of host bridge internals. So we attempt to
164 * keep as much of the original configuration as possible, but tweak it
165 * when it's found to be wrong.
167 * Known BIOS problems we have to work around:
168 * - I/O or memory regions not configured
169 * - regions configured, but not enabled in the command register
170 * - bogus I/O addresses above 64K used
171 * - expansion ROMs left enabled (this may sound harmless, but given
172 * the fact the PCI specs explicitly allow address decoders to be
173 * shared between expansion ROMs and other resource regions, it's
174 * at least dangerous)
176 * Our solution:
177 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
178 * This gives us fixed barriers on where we can allocate.
179 * (2) Allocate resources for all enabled devices. If there is
180 * a collision, just mark the resource as unallocated. Also
181 * disable expansion ROMs during this step.
182 * (3) Try to allocate resources for disabled devices. If the
183 * resources were assigned correctly, everything goes well,
184 * if they weren't, they won't disturb allocation of other
185 * resources.
186 * (4) Assign new addresses to resources which were either
187 * not configured at all or misconfigured. If explicitly
188 * requested by the user, configure expansion ROM address
189 * as well.
192 static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
194 struct list_head *ln;
195 struct pci_bus *bus;
196 struct pci_dev *dev;
197 int idx;
198 struct resource *r, *pr;
200 /* Depth-First Search on bus tree */
201 for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
202 bus = pci_bus_b(ln);
203 if ((dev = bus->self)) {
204 for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
205 r = &dev->resource[idx];
206 if (!r->start)
207 continue;
208 pr = pci_find_parent_resource(dev, r);
209 if (!pr || request_resource(pr, r) < 0)
210 printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, dev->slot_name);
213 pcibios_allocate_bus_resources(&bus->children);
217 static void __init pcibios_allocate_resources(int pass)
219 struct pci_dev *dev;
220 int idx, disabled;
221 u16 command;
222 struct resource *r, *pr;
224 pci_for_each_dev(dev) {
225 pci_read_config_word(dev, PCI_COMMAND, &command);
226 for(idx = 0; idx < 6; idx++) {
227 r = &dev->resource[idx];
228 if (r->parent) /* Already allocated */
229 continue;
230 if (!r->start) /* Address not assigned at all */
231 continue;
232 if (r->flags & IORESOURCE_IO)
233 disabled = !(command & PCI_COMMAND_IO);
234 else
235 disabled = !(command & PCI_COMMAND_MEMORY);
236 if (pass == disabled) {
237 DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
238 r->start, r->end, r->flags, disabled, pass);
239 pr = pci_find_parent_resource(dev, r);
240 if (!pr || request_resource(pr, r) < 0) {
241 printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, dev->slot_name);
242 /* We'll assign a new address later */
243 r->end -= r->start;
244 r->start = 0;
248 if (!pass) {
249 r = &dev->resource[PCI_ROM_RESOURCE];
250 if (r->flags & PCI_ROM_ADDRESS_ENABLE) {
251 /* Turn the ROM off, leave the resource region, but keep it unregistered. */
252 u32 reg;
253 DBG("PCI: Switching off ROM of %s\n", dev->slot_name);
254 r->flags &= ~PCI_ROM_ADDRESS_ENABLE;
255 pci_read_config_dword(dev, dev->rom_base_reg, &reg);
256 pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
262 static void __init pcibios_assign_resources(void)
264 struct pci_dev *dev;
265 int idx;
266 struct resource *r;
268 pci_for_each_dev(dev) {
269 int class = dev->class >> 8;
271 /* Don't touch classless devices and host bridges */
272 if (!class || class == PCI_CLASS_BRIDGE_HOST)
273 continue;
275 for(idx=0; idx<6; idx++) {
276 r = &dev->resource[idx];
279 * Don't touch IDE controllers and I/O ports of video cards!
281 if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
282 (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
283 continue;
286 * We shall assign a new address to this resource, either because
287 * the BIOS forgot to do so or because we have decided the old
288 * address was unusable for some reason.
290 if (!r->start && r->end)
291 pci_assign_resource(dev, idx);
294 if (pci_probe & PCI_ASSIGN_ROMS) {
295 r = &dev->resource[PCI_ROM_RESOURCE];
296 r->end -= r->start;
297 r->start = 0;
298 if (r->end)
299 pci_assign_resource(dev, PCI_ROM_RESOURCE);
304 void __init pcibios_resource_survey(void)
306 DBG("PCI: Allocating resources\n");
307 pcibios_allocate_bus_resources(&pci_root_buses);
308 pcibios_allocate_resources(0);
309 pcibios_allocate_resources(1);
310 pcibios_assign_resources();
313 int pcibios_enable_resources(struct pci_dev *dev)
315 u16 cmd, old_cmd;
316 int idx;
317 struct resource *r;
319 pci_read_config_word(dev, PCI_COMMAND, &cmd);
320 old_cmd = cmd;
321 for(idx=0; idx<6; idx++) {
322 r = &dev->resource[idx];
323 if (!r->start && r->end) {
324 printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", dev->slot_name);
325 return -EINVAL;
327 if (r->flags & IORESOURCE_IO)
328 cmd |= PCI_COMMAND_IO;
329 if (r->flags & IORESOURCE_MEM)
330 cmd |= PCI_COMMAND_MEMORY;
332 if (dev->resource[PCI_ROM_RESOURCE].start)
333 cmd |= PCI_COMMAND_MEMORY;
334 if (cmd != old_cmd) {
335 printk("PCI: Enabling device %s (%04x -> %04x)\n", dev->slot_name, old_cmd, cmd);
336 pci_write_config_word(dev, PCI_COMMAND, cmd);
338 return 0;
342 * If we set up a device for bus mastering, we need to check the latency
343 * timer as certain crappy BIOSes forget to set it properly.
345 unsigned int pcibios_max_latency = 255;
347 void pcibios_set_master(struct pci_dev *dev)
349 u8 lat;
350 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
351 if (lat < 16)
352 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
353 else if (lat > pcibios_max_latency)
354 lat = pcibios_max_latency;
355 else
356 return;
357 printk("PCI: Setting latency timer of device %s to %d\n", dev->slot_name, lat);
358 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);