Import 2.1.118
[davej-history.git] / arch / i386 / kernel / bios32.c
blob0aa1f455379c71b3fec83e4f3a7e7c58f052822e
1 /*
2 * bios32.c - Low-Level PCI Access
4 * $Id: bios32.c,v 1.45 1998/08/15 10:41:04 mj Exp $
6 * Copyright 1993, 1994 Drew Eckhardt
7 * Visionary Computing
8 * (Unix and Linux consulting and custom programming)
9 * Drew@Colorado.EDU
10 * +1 (303) 786-7975
12 * Drew's work was sponsored by:
13 * iX Multiuser Multitasking Magazine
14 * Hannover, Germany
15 * hm@ix.de
17 * Copyright 1997, 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
19 * For more information, please consult the following manuals (look at
20 * http://www.pcisig.com/ for how to get them):
22 * PCI BIOS Specification
23 * PCI Local Bus Specification
24 * PCI to PCI Bridge Specification
25 * PCI System Design Guide
28 * CHANGELOG :
29 * Jun 17, 1994 : Modified to accommodate the broken pre-PCI BIOS SPECIFICATION
30 * Revision 2.0 present on <thys@dennis.ee.up.ac.za>'s ASUS mainboard.
32 * Jan 5, 1995 : Modified to probe PCI hardware at boot time by Frederic
33 * Potter, potter@cao-vlsi.ibp.fr
35 * Jan 10, 1995 : Modified to store the information about configured pci
36 * devices into a list, which can be accessed via /proc/pci by
37 * Curtis Varner, cvarner@cs.ucr.edu
39 * Jan 12, 1995 : CPU-PCI bridge optimization support by Frederic Potter.
40 * Alpha version. Intel & UMC chipset support only.
42 * Apr 16, 1995 : Source merge with the DEC Alpha PCI support. Most of the code
43 * moved to drivers/pci/pci.c.
45 * Dec 7, 1996 : Added support for direct configuration access of boards
46 * with Intel compatible access schemes (tsbogend@alpha.franken.de)
48 * Feb 3, 1997 : Set internal functions to static, save/restore flags
49 * avoid dead locks reading broken PCI BIOS, werner@suse.de
51 * Apr 26, 1997 : Fixed case when there is BIOS32, but not PCI BIOS
52 * (mj@atrey.karlin.mff.cuni.cz)
54 * May 7, 1997 : Added some missing cli()'s. [mj]
56 * Jun 20, 1997 : Corrected problems in "conf1" type accesses.
57 * (paubert@iram.es)
59 * Aug 2, 1997 : Split to PCI BIOS handling and direct PCI access parts
60 * and cleaned it up... Martin Mares <mj@atrey.karlin.mff.cuni.cz>
62 * Feb 6, 1998 : No longer using BIOS to find devices and device classes. [mj]
64 * May 1, 1998 : Support for peer host bridges. [mj]
66 * Jun 19, 1998 : Changed to use spinlocks, so that PCI configuration space
67 * can be accessed from interrupts even on SMP systems. [mj]
69 * August 1998 : Better support for peer host bridges and more paranoid
70 * checks for direct hardware access. Ugh, this file starts to look as
71 * a large gallery of common hardware bug workarounds (watch the comments)
72 * -- the PCI specs themselves are sane, but most implementors should be
73 * hit hard with \hammer scaled \magstep5. [mj]
76 #include <linux/config.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/pci.h>
80 #include <linux/init.h>
81 #include <linux/ioport.h>
82 #include <linux/malloc.h>
83 #include <linux/smp_lock.h>
85 #include <asm/page.h>
86 #include <asm/segment.h>
87 #include <asm/system.h>
88 #include <asm/io.h>
89 #include <asm/smp.h>
90 #include <asm/spinlock.h>
92 #include "irq.h"
94 #undef DEBUG
96 #ifdef DEBUG
97 #define DBG(x...) printk(x)
98 #else
99 #define DBG(x...)
100 #endif
103 * This interrupt-safe spinlock protects all accesses to PCI
104 * configuration space.
107 spinlock_t pci_lock = SPIN_LOCK_UNLOCKED;
110 * Generic PCI access -- indirect calls according to detected HW.
113 struct pci_access {
114 int pci_present;
115 int (*read_config_byte)(unsigned char, unsigned char, unsigned char, unsigned char *);
116 int (*read_config_word)(unsigned char, unsigned char, unsigned char, unsigned short *);
117 int (*read_config_dword)(unsigned char, unsigned char, unsigned char, unsigned int *);
118 int (*write_config_byte)(unsigned char, unsigned char, unsigned char, unsigned char);
119 int (*write_config_word)(unsigned char, unsigned char, unsigned char, unsigned short);
120 int (*write_config_dword)(unsigned char, unsigned char, unsigned char, unsigned int);
123 static int pci_stub(void)
125 return PCIBIOS_FUNC_NOT_SUPPORTED;
128 static struct pci_access pci_access_none = {
129 0, /* No PCI present */
130 (void *) pci_stub,
131 (void *) pci_stub,
132 (void *) pci_stub,
133 (void *) pci_stub,
134 (void *) pci_stub,
135 (void *) pci_stub
138 static struct pci_access *access_pci = &pci_access_none;
140 int pcibios_present(void)
142 return access_pci->pci_present;
145 #define PCI_byte_BAD 0
146 #define PCI_word_BAD (pos & 1)
147 #define PCI_dword_BAD (pos & 3)
149 #define PCI_STUB(rw,size,type) \
150 int pcibios_##rw##_config_##size (u8 bus, u8 dfn, u8 pos, type value) \
152 int res; \
153 unsigned long flags; \
154 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
155 spin_lock_irqsave(&pci_lock, flags); \
156 res = access_pci->rw##_config_##size(bus, dfn, pos, value); \
157 spin_unlock_irqrestore(&pci_lock, flags); \
158 return res; \
161 PCI_STUB(read, byte, u8 *)
162 PCI_STUB(read, word, u16 *)
163 PCI_STUB(read, dword, u32 *)
164 PCI_STUB(write, byte, u8)
165 PCI_STUB(write, word, u16)
166 PCI_STUB(write, dword, u32)
168 #define PCI_PROBE_BIOS 1
169 #define PCI_PROBE_CONF1 2
170 #define PCI_PROBE_CONF2 4
171 #define PCI_NO_SORT 0x100
172 #define PCI_BIOS_SORT 0x200
174 static unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2;
177 * Direct access to PCI hardware...
180 #ifdef CONFIG_PCI_DIRECT
183 * Functions for accessing PCI configuration space with type 1 accesses
186 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
188 static int pci_conf1_read_config_byte(unsigned char bus, unsigned char device_fn,
189 unsigned char where, unsigned char *value)
191 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
192 *value = inb(0xCFC + (where&3));
193 return PCIBIOS_SUCCESSFUL;
196 static int pci_conf1_read_config_word (unsigned char bus,
197 unsigned char device_fn, unsigned char where, unsigned short *value)
199 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
200 *value = inw(0xCFC + (where&2));
201 return PCIBIOS_SUCCESSFUL;
204 static int pci_conf1_read_config_dword (unsigned char bus, unsigned char device_fn,
205 unsigned char where, unsigned int *value)
207 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
208 *value = inl(0xCFC);
209 return PCIBIOS_SUCCESSFUL;
212 static int pci_conf1_write_config_byte (unsigned char bus, unsigned char device_fn,
213 unsigned char where, unsigned char value)
215 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
216 outb(value, 0xCFC + (where&3));
217 return PCIBIOS_SUCCESSFUL;
220 static int pci_conf1_write_config_word (unsigned char bus, unsigned char device_fn,
221 unsigned char where, unsigned short value)
223 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
224 outw(value, 0xCFC + (where&2));
225 return PCIBIOS_SUCCESSFUL;
228 static int pci_conf1_write_config_dword (unsigned char bus, unsigned char device_fn,
229 unsigned char where, unsigned int value)
231 outl(CONFIG_CMD(bus,device_fn,where), 0xCF8);
232 outl(value, 0xCFC);
233 return PCIBIOS_SUCCESSFUL;
236 #undef CONFIG_CMD
238 static struct pci_access pci_direct_conf1 = {
240 pci_conf1_read_config_byte,
241 pci_conf1_read_config_word,
242 pci_conf1_read_config_dword,
243 pci_conf1_write_config_byte,
244 pci_conf1_write_config_word,
245 pci_conf1_write_config_dword
249 * Functions for accessing PCI configuration space with type 2 accesses
252 #define IOADDR(devfn, where) ((0xC000 | ((devfn & 0x78) << 5)) + where)
253 #define FUNC(devfn) (((devfn & 7) << 1) | 0xf0)
255 static int pci_conf2_read_config_byte(unsigned char bus, unsigned char device_fn,
256 unsigned char where, unsigned char *value)
258 if (device_fn & 0x80)
259 return PCIBIOS_DEVICE_NOT_FOUND;
260 outb (FUNC(device_fn), 0xCF8);
261 outb (bus, 0xCFA);
262 *value = inb(IOADDR(device_fn,where));
263 outb (0, 0xCF8);
264 return PCIBIOS_SUCCESSFUL;
267 static int pci_conf2_read_config_word (unsigned char bus, unsigned char device_fn,
268 unsigned char where, unsigned short *value)
270 if (device_fn & 0x80)
271 return PCIBIOS_DEVICE_NOT_FOUND;
272 outb (FUNC(device_fn), 0xCF8);
273 outb (bus, 0xCFA);
274 *value = inw(IOADDR(device_fn,where));
275 outb (0, 0xCF8);
276 return PCIBIOS_SUCCESSFUL;
279 static int pci_conf2_read_config_dword (unsigned char bus, unsigned char device_fn,
280 unsigned char where, unsigned int *value)
282 if (device_fn & 0x80)
283 return PCIBIOS_DEVICE_NOT_FOUND;
284 outb (FUNC(device_fn), 0xCF8);
285 outb (bus, 0xCFA);
286 *value = inl (IOADDR(device_fn,where));
287 outb (0, 0xCF8);
288 return PCIBIOS_SUCCESSFUL;
291 static int pci_conf2_write_config_byte (unsigned char bus, unsigned char device_fn,
292 unsigned char where, unsigned char value)
294 if (device_fn & 0x80)
295 return PCIBIOS_DEVICE_NOT_FOUND;
296 outb (FUNC(device_fn), 0xCF8);
297 outb (bus, 0xCFA);
298 outb (value, IOADDR(device_fn,where));
299 outb (0, 0xCF8);
300 return PCIBIOS_SUCCESSFUL;
303 static int pci_conf2_write_config_word (unsigned char bus, unsigned char device_fn,
304 unsigned char where, unsigned short value)
306 if (device_fn & 0x80)
307 return PCIBIOS_DEVICE_NOT_FOUND;
308 outb (FUNC(device_fn), 0xCF8);
309 outb (bus, 0xCFA);
310 outw (value, IOADDR(device_fn,where));
311 outb (0, 0xCF8);
312 return PCIBIOS_SUCCESSFUL;
315 static int pci_conf2_write_config_dword (unsigned char bus, unsigned char device_fn,
316 unsigned char where, unsigned int value)
318 if (device_fn & 0x80)
319 return PCIBIOS_DEVICE_NOT_FOUND;
320 outb (FUNC(device_fn), 0xCF8);
321 outb (bus, 0xCFA);
322 outl (value, IOADDR(device_fn,where));
323 outb (0, 0xCF8);
324 return PCIBIOS_SUCCESSFUL;
327 #undef IOADDR
328 #undef FUNC
330 static struct pci_access pci_direct_conf2 = {
332 pci_conf2_read_config_byte,
333 pci_conf2_read_config_word,
334 pci_conf2_read_config_dword,
335 pci_conf2_write_config_byte,
336 pci_conf2_write_config_word,
337 pci_conf2_write_config_dword
341 * Before we decide to use direct hardware access mechanisms, we try to do some
342 * trivial checks to ensure it at least _seems_ to be working -- we just test
343 * whether bus 00 contains a host bridge (this is similar to checking
344 * techniques used in XFree86, but ours should be more reliable since we
345 * attempt to make use of direct access hints provided by the PCI BIOS).
347 __initfunc(int pci_sanity_check(struct pci_access *a))
349 u16 dfn, class;
351 for(dfn=0; dfn < 0x100; dfn++)
352 if (!a->read_config_word(0, dfn, PCI_CLASS_DEVICE, &class) &&
353 class == PCI_CLASS_BRIDGE_HOST)
354 return 1;
355 DBG("PCI: Sanity check failed\n");
356 return 0;
359 __initfunc(static struct pci_access *pci_check_direct(void))
361 unsigned int tmp;
362 unsigned long flags;
364 __save_flags(flags); __cli();
367 * Check if configuration type 1 works.
369 if (pci_probe & PCI_PROBE_CONF1) {
370 outb (0x01, 0xCFB);
371 tmp = inl (0xCF8);
372 outl (0x80000000, 0xCF8);
373 if (inl (0xCF8) == 0x80000000 &&
374 pci_sanity_check(&pci_direct_conf1)) {
375 outl (tmp, 0xCF8);
376 __restore_flags(flags);
377 printk("PCI: Using configuration type 1\n");
378 return &pci_direct_conf1;
380 outl (tmp, 0xCF8);
384 * Check if configuration type 2 works.
386 if (pci_probe & PCI_PROBE_CONF2) {
387 outb (0x00, 0xCFB);
388 outb (0x00, 0xCF8);
389 outb (0x00, 0xCFA);
390 if (inb (0xCF8) == 0x00 && inb (0xCFA) == 0x00 &&
391 pci_sanity_check(&pci_direct_conf2)) {
392 __restore_flags(flags);
393 printk("PCI: Using configuration type 2\n");
394 return &pci_direct_conf2;
398 __restore_flags(flags);
399 return NULL;
402 #endif
405 * BIOS32 and PCI BIOS handling.
408 #ifdef CONFIG_PCI_BIOS
410 #define PCIBIOS_PCI_FUNCTION_ID 0xb1XX
411 #define PCIBIOS_PCI_BIOS_PRESENT 0xb101
412 #define PCIBIOS_FIND_PCI_DEVICE 0xb102
413 #define PCIBIOS_FIND_PCI_CLASS_CODE 0xb103
414 #define PCIBIOS_GENERATE_SPECIAL_CYCLE 0xb106
415 #define PCIBIOS_READ_CONFIG_BYTE 0xb108
416 #define PCIBIOS_READ_CONFIG_WORD 0xb109
417 #define PCIBIOS_READ_CONFIG_DWORD 0xb10a
418 #define PCIBIOS_WRITE_CONFIG_BYTE 0xb10b
419 #define PCIBIOS_WRITE_CONFIG_WORD 0xb10c
420 #define PCIBIOS_WRITE_CONFIG_DWORD 0xb10d
422 /* BIOS32 signature: "_32_" */
423 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
425 /* PCI signature: "PCI " */
426 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
428 /* PCI service signature: "$PCI" */
429 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
431 /* PCI BIOS hardware mechanism flags */
432 #define PCIBIOS_HW_TYPE1 0x01
433 #define PCIBIOS_HW_TYPE2 0x02
434 #define PCIBIOS_HW_TYPE1_SPEC 0x10
435 #define PCIBIOS_HW_TYPE2_SPEC 0x20
438 * This is the standard structure used to identify the entry point
439 * to the BIOS32 Service Directory, as documented in
440 * Standard BIOS 32-bit Service Directory Proposal
441 * Revision 0.4 May 24, 1993
442 * Phoenix Technologies Ltd.
443 * Norwood, MA
444 * and the PCI BIOS specification.
447 union bios32 {
448 struct {
449 unsigned long signature; /* _32_ */
450 unsigned long entry; /* 32 bit physical address */
451 unsigned char revision; /* Revision level, 0 */
452 unsigned char length; /* Length in paragraphs should be 01 */
453 unsigned char checksum; /* All bytes must add up to zero */
454 unsigned char reserved[5]; /* Must be zero */
455 } fields;
456 char chars[16];
460 * Physical address of the service directory. I don't know if we're
461 * allowed to have more than one of these or not, so just in case
462 * we'll make pcibios_present() take a memory start parameter and store
463 * the array there.
466 static struct {
467 unsigned long address;
468 unsigned short segment;
469 } bios32_indirect = { 0, __KERNEL_CS };
472 * Returns the entry point for the given service, NULL on error
475 static unsigned long bios32_service(unsigned long service)
477 unsigned char return_code; /* %al */
478 unsigned long address; /* %ebx */
479 unsigned long length; /* %ecx */
480 unsigned long entry; /* %edx */
481 unsigned long flags;
483 spin_lock_irqsave(&pci_lock, flags);
484 __asm__("lcall (%%edi)"
485 : "=a" (return_code),
486 "=b" (address),
487 "=c" (length),
488 "=d" (entry)
489 : "0" (service),
490 "1" (0),
491 "D" (&bios32_indirect));
492 spin_unlock_irqrestore(&pci_lock, flags);
494 switch (return_code) {
495 case 0:
496 return address + entry;
497 case 0x80: /* Not present */
498 printk("bios32_service(0x%lx): not present\n", service);
499 return 0;
500 default: /* Shouldn't happen */
501 printk("bios32_service(0x%lx): returned 0x%x, report to <mj@ucw.cz>.\n",
502 service, return_code);
503 return 0;
507 static struct {
508 unsigned long address;
509 unsigned short segment;
510 } pci_indirect = { 0, __KERNEL_CS };
512 __initfunc(static int check_pcibios(void))
514 u32 signature, eax, ebx, ecx;
515 u8 status, major_ver, minor_ver, hw_mech, last_bus;
516 unsigned long flags, pcibios_entry;
518 if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
519 pci_indirect.address = pcibios_entry + PAGE_OFFSET;
521 __save_flags(flags); __cli();
522 __asm__(
523 "lcall (%%edi)\n\t"
524 "jc 1f\n\t"
525 "xor %%ah, %%ah\n"
526 "1:"
527 : "=d" (signature),
528 "=a" (eax),
529 "=b" (ebx),
530 "=c" (ecx)
531 : "1" (PCIBIOS_PCI_BIOS_PRESENT),
532 "D" (&pci_indirect)
533 : "memory");
534 __restore_flags(flags);
536 status = (eax >> 8) & 0xff;
537 hw_mech = eax & 0xff;
538 major_ver = (ebx >> 8) & 0xff;
539 minor_ver = ebx & 0xff;
540 last_bus = ecx & 0xff;
541 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
542 status, hw_mech, major_ver, minor_ver, last_bus);
543 if (status || signature != PCI_SIGNATURE) {
544 printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found, report to <mj@ucw.cz>\n",
545 status, signature);
546 return 0;
548 printk("PCI: PCI BIOS revision %x.%02x entry at 0x%lx\n",
549 major_ver, minor_ver, pcibios_entry);
550 #ifdef CONFIG_PCI_DIRECT
551 if (!(hw_mech & PCIBIOS_HW_TYPE1))
552 pci_probe &= ~PCI_PROBE_CONF1;
553 if (!(hw_mech & PCIBIOS_HW_TYPE2))
554 pci_probe &= ~PCI_PROBE_CONF2;
555 #endif
556 return 1;
558 return 0;
561 #if 0 /* Not used */
563 static int pci_bios_find_class (unsigned int class_code, unsigned short index,
564 unsigned char *bus, unsigned char *device_fn)
566 unsigned long bx;
567 unsigned long ret;
569 __asm__ ("lcall (%%edi)\n\t"
570 "jc 1f\n\t"
571 "xor %%ah, %%ah\n"
572 "1:"
573 : "=b" (bx),
574 "=a" (ret)
575 : "1" (PCIBIOS_FIND_PCI_CLASS_CODE),
576 "c" (class_code),
577 "S" ((int) index),
578 "D" (&pci_indirect));
579 *bus = (bx >> 8) & 0xff;
580 *device_fn = bx & 0xff;
581 return (int) (ret & 0xff00) >> 8;
584 #endif
586 __initfunc(static int pci_bios_find_device (unsigned short vendor, unsigned short device_id,
587 unsigned short index, unsigned char *bus, unsigned char *device_fn))
589 unsigned short bx;
590 unsigned short ret;
592 __asm__("lcall (%%edi)\n\t"
593 "jc 1f\n\t"
594 "xor %%ah, %%ah\n"
595 "1:"
596 : "=b" (bx),
597 "=a" (ret)
598 : "1" (PCIBIOS_FIND_PCI_DEVICE),
599 "c" (device_id),
600 "d" (vendor),
601 "S" ((int) index),
602 "D" (&pci_indirect));
603 *bus = (bx >> 8) & 0xff;
604 *device_fn = bx & 0xff;
605 return (int) (ret & 0xff00) >> 8;
608 static int pci_bios_read_config_byte(unsigned char bus,
609 unsigned char device_fn, unsigned char where, unsigned char *value)
611 unsigned long ret;
612 unsigned long bx = (bus << 8) | device_fn;
614 __asm__("lcall (%%esi)\n\t"
615 "jc 1f\n\t"
616 "xor %%ah, %%ah\n"
617 "1:"
618 : "=c" (*value),
619 "=a" (ret)
620 : "1" (PCIBIOS_READ_CONFIG_BYTE),
621 "b" (bx),
622 "D" ((long) where),
623 "S" (&pci_indirect));
624 return (int) (ret & 0xff00) >> 8;
627 static int pci_bios_read_config_word (unsigned char bus,
628 unsigned char device_fn, unsigned char where, unsigned short *value)
630 unsigned long ret;
631 unsigned long bx = (bus << 8) | device_fn;
633 __asm__("lcall (%%esi)\n\t"
634 "jc 1f\n\t"
635 "xor %%ah, %%ah\n"
636 "1:"
637 : "=c" (*value),
638 "=a" (ret)
639 : "1" (PCIBIOS_READ_CONFIG_WORD),
640 "b" (bx),
641 "D" ((long) where),
642 "S" (&pci_indirect));
643 return (int) (ret & 0xff00) >> 8;
646 static int pci_bios_read_config_dword (unsigned char bus,
647 unsigned char device_fn, unsigned char where, unsigned int *value)
649 unsigned long ret;
650 unsigned long bx = (bus << 8) | device_fn;
652 __asm__("lcall (%%esi)\n\t"
653 "jc 1f\n\t"
654 "xor %%ah, %%ah\n"
655 "1:"
656 : "=c" (*value),
657 "=a" (ret)
658 : "1" (PCIBIOS_READ_CONFIG_DWORD),
659 "b" (bx),
660 "D" ((long) where),
661 "S" (&pci_indirect));
662 return (int) (ret & 0xff00) >> 8;
665 static int pci_bios_write_config_byte (unsigned char bus,
666 unsigned char device_fn, unsigned char where, unsigned char value)
668 unsigned long ret;
669 unsigned long bx = (bus << 8) | device_fn;
671 __asm__("lcall (%%esi)\n\t"
672 "jc 1f\n\t"
673 "xor %%ah, %%ah\n"
674 "1:"
675 : "=a" (ret)
676 : "0" (PCIBIOS_WRITE_CONFIG_BYTE),
677 "c" (value),
678 "b" (bx),
679 "D" ((long) where),
680 "S" (&pci_indirect));
681 return (int) (ret & 0xff00) >> 8;
684 static int pci_bios_write_config_word (unsigned char bus,
685 unsigned char device_fn, unsigned char where, unsigned short value)
687 unsigned long ret;
688 unsigned long bx = (bus << 8) | device_fn;
690 __asm__("lcall (%%esi)\n\t"
691 "jc 1f\n\t"
692 "xor %%ah, %%ah\n"
693 "1:"
694 : "=a" (ret)
695 : "0" (PCIBIOS_WRITE_CONFIG_WORD),
696 "c" (value),
697 "b" (bx),
698 "D" ((long) where),
699 "S" (&pci_indirect));
700 return (int) (ret & 0xff00) >> 8;
703 static int pci_bios_write_config_dword (unsigned char bus,
704 unsigned char device_fn, unsigned char where, unsigned int value)
706 unsigned long ret;
707 unsigned long bx = (bus << 8) | device_fn;
709 __asm__("lcall (%%esi)\n\t"
710 "jc 1f\n\t"
711 "xor %%ah, %%ah\n"
712 "1:"
713 : "=a" (ret)
714 : "0" (PCIBIOS_WRITE_CONFIG_DWORD),
715 "c" (value),
716 "b" (bx),
717 "D" ((long) where),
718 "S" (&pci_indirect));
719 return (int) (ret & 0xff00) >> 8;
723 * Function table for BIOS32 access
726 static struct pci_access pci_bios_access = {
728 pci_bios_read_config_byte,
729 pci_bios_read_config_word,
730 pci_bios_read_config_dword,
731 pci_bios_write_config_byte,
732 pci_bios_write_config_word,
733 pci_bios_write_config_dword
737 * Try to find PCI BIOS.
740 __initfunc(static struct pci_access *pci_find_bios(void))
742 union bios32 *check;
743 unsigned char sum;
744 int i, length;
747 * Follow the standard procedure for locating the BIOS32 Service
748 * directory by scanning the permissible address range from
749 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
752 for (check = (union bios32 *) __va(0xe0000);
753 check <= (union bios32 *) __va(0xffff0);
754 ++check) {
755 if (check->fields.signature != BIOS32_SIGNATURE)
756 continue;
757 length = check->fields.length * 16;
758 if (!length)
759 continue;
760 sum = 0;
761 for (i = 0; i < length ; ++i)
762 sum += check->chars[i];
763 if (sum != 0)
764 continue;
765 if (check->fields.revision != 0) {
766 printk("PCI: unsupported BIOS32 revision %d at 0x%p, report to <mj@ucw.cz>\n",
767 check->fields.revision, check);
768 continue;
770 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
771 if (check->fields.entry >= 0x100000) {
772 printk("PCI: BIOS32 entry (0x%p) in high memory, cannot use.\n", check);
773 return NULL;
774 } else {
775 unsigned long bios32_entry = check->fields.entry;
776 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n", bios32_entry);
777 bios32_indirect.address = bios32_entry + PAGE_OFFSET;
778 if (check_pcibios())
779 return &pci_bios_access;
781 break; /* Hopefully more than one BIOS32 cannot happen... */
784 return NULL;
788 * Sort the device list according to PCI BIOS. Nasty hack, but since some
789 * fool forgot to define the `correct' device order in the PCI BIOS specs
790 * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
791 * which used BIOS ordering, we are bound to do this...
794 __initfunc(void pcibios_sort(void))
796 struct pci_dev *dev = pci_devices;
797 struct pci_dev **last = &pci_devices;
798 struct pci_dev *d, **dd, *e;
799 int idx;
800 unsigned char bus, devfn;
802 DBG("PCI: Sorting device list...\n");
803 while ((e = dev)) {
804 idx = 0;
805 while (pci_bios_find_device(e->vendor, e->device, idx, &bus, &devfn) == PCIBIOS_SUCCESSFUL) {
806 idx++;
807 for(dd=&dev; (d = *dd); dd = &d->next) {
808 if (d->bus->number == bus && d->devfn == devfn) {
809 *dd = d->next;
810 *last = d;
811 last = &d->next;
812 break;
815 if (!d) {
816 printk("PCI: BIOS reporting unknown device %02x:%02x\n", bus, devfn);
818 * We must not continue scanning as several buggy BIOSes
819 * return garbage after the last device. Grr.
821 break;
824 if (e == dev) {
825 printk("PCI: Device %02x:%02x not found by BIOS\n",
826 dev->bus->number, dev->devfn);
827 d = dev;
828 dev = dev->next;
829 *last = d;
830 last = &d->next;
833 *last = NULL;
836 #endif
839 * Several BIOS'es forget to assign addresses to I/O ranges.
840 * We try to fix it here, expecting there are free addresses
841 * starting with 0x5800. Ugly, but until we come with better
842 * resource management, it's the only simple solution.
845 static int pci_last_io_addr __initdata = 0x5800;
847 __initfunc(void pcibios_fixup_io_addr(struct pci_dev *dev, int idx))
849 unsigned short cmd;
850 unsigned int reg = PCI_BASE_ADDRESS_0 + 4*idx;
851 unsigned int size, addr, try;
852 unsigned int bus = dev->bus->number;
853 unsigned int devfn = dev->devfn;
855 if (!pci_last_io_addr) {
856 printk("PCI: Unassigned I/O space for %02x:%02x\n", bus, devfn);
857 return;
859 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && idx < 4) {
861 * In case the BIOS didn't assign an address 0--3 to an IDE
862 * controller, we don't try to fix it as it means "use default
863 * addresses" at least with several broken chips and the IDE
864 * driver needs the original settings to recognize which devices
865 * correspond to the primary controller.
867 return;
869 pcibios_read_config_word(bus, devfn, PCI_COMMAND, &cmd);
870 pcibios_write_config_word(bus, devfn, PCI_COMMAND, cmd & ~PCI_COMMAND_IO);
871 pcibios_write_config_dword(bus, devfn, reg, ~0);
872 pcibios_read_config_dword(bus, devfn, reg, &size);
873 size = (~(size & PCI_BASE_ADDRESS_IO_MASK) & 0xffff) + 1;
874 addr = 0;
875 if (!size || size > 0x100)
876 printk("PCI: Unable to handle I/O allocation for %02x:%02x (%04x), tell <mj@ucw.cz>\n", bus, devfn, size);
877 else {
878 do {
879 addr = (pci_last_io_addr + size - 1) & ~(size-1);
880 pci_last_io_addr = addr + size;
881 } while (check_region(addr, size));
882 printk("PCI: Assigning I/O space %04x-%04x to device %02x:%02x\n", addr, addr+size-1, bus, devfn);
883 pcibios_write_config_dword(bus, devfn, reg, addr | PCI_BASE_ADDRESS_SPACE_IO);
884 pcibios_read_config_dword(bus, devfn, reg, &try);
885 if ((try & PCI_BASE_ADDRESS_IO_MASK) != addr) {
886 addr = 0;
887 printk("PCI: Address setup failed, got %04x\n", try);
888 } else
889 dev->base_address[idx] = try;
891 if (!addr) {
892 pcibios_write_config_dword(bus, devfn, reg, 0);
893 dev->base_address[idx] = 0;
895 pcibios_write_config_word(bus, devfn, PCI_COMMAND, cmd);
899 * Several buggy motherboards address only 16 devices and mirror
900 * them to next 16 IDs. We try to detect this `feature' on all
901 * primary busses (those containing host bridges as they are
902 * expected to be unique) and remove the ghost devices.
905 __initfunc(void pcibios_fixup_ghosts(struct pci_bus *b))
907 struct pci_dev *d, *e, **z;
908 int mirror = PCI_DEVFN(16,0);
909 int seen_host_bridge = 0;
911 DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
912 for(d=b->devices; d && d->devfn < mirror; d=d->sibling) {
913 if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
914 seen_host_bridge++;
915 for(e=d->next; e; e=e->sibling)
916 if (e->devfn == d->devfn + mirror &&
917 e->vendor == d->vendor &&
918 e->device == d->device &&
919 e->class == d->class &&
920 !memcmp(e->base_address, d->base_address, sizeof(e->base_address)))
921 break;
922 if (!e)
923 return;
925 if (!seen_host_bridge)
926 return;
927 printk("PCI: Ignoring ghost devices on bus %d\n", b->number);
928 for(e=b->devices; e->sibling != d; e=e->sibling);
929 e->sibling = NULL;
930 for(z=&pci_devices; (d=*z);)
931 if (d->bus == b && d->devfn >= mirror) {
932 *z = d->next;
933 kfree_s(d, sizeof(*d));
934 } else
935 z = &d->next;
939 * In case there are peer host bridges, scan bus behind each of them.
940 * Although several sources claim that the host bridges should have
941 * header type 1 and be assigned a bus number as for PCI2PCI bridges,
942 * the reality doesn't pass this test and the bus number is usually
943 * set by BIOS to the first free value.
945 __initfunc(void pcibios_fixup_peer_bridges(void))
947 struct pci_bus *b = &pci_root;
948 int i, cnt=-1;
949 struct pci_dev *d;
951 #ifdef CONFIG_PCI_DIRECT
953 * Don't search for peer host bridges if we use config type 2
954 * since it reads bogus values for non-existent busses and
955 * chipsets supporting multiple primary busses use conf1 anyway.
957 if (access_pci == &pci_direct_conf2)
958 return;
959 #endif
960 for(d=b->devices; d; d=d->sibling)
961 if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
962 cnt++;
963 do {
964 int n = b->subordinate+1;
965 int found = 0;
966 u16 l;
967 for(i=0; i<256; i += 8)
968 if (!pcibios_read_config_word(n, i, PCI_VENDOR_ID, &l) &&
969 l != 0x0000 && l != 0xffff) {
970 DBG("Found device at %02x:%02x\n", n, i);
971 found++;
972 if (!pcibios_read_config_word(n, i, PCI_CLASS_DEVICE, &l) &&
973 l == PCI_CLASS_BRIDGE_HOST)
974 cnt++;
976 if (found && cnt > 0) {
977 cnt--;
978 printk("PCI: Discovered primary peer bus %02x\n", n);
979 b = kmalloc(sizeof(*b), GFP_KERNEL);
980 memset(b, 0, sizeof(*b));
981 b->next = pci_root.next;
982 pci_root.next = b;
983 b->number = b->secondary = n;
984 b->subordinate = 0xff;
985 b->subordinate = pci_scan_bus(b);
986 break;
988 } while (i < 256);
992 * Fix base addresses, I/O and memory enables and IRQ's (mostly work-arounds
993 * for buggy PCI BIOS'es :-[).
996 __initfunc(void pcibios_fixup_devices(void))
998 struct pci_dev *dev;
999 int i, has_io, has_mem;
1000 unsigned short cmd;
1002 for(dev = pci_devices; dev; dev=dev->next) {
1004 * There are buggy BIOSes that forget to enable I/O and memory
1005 * access to PCI devices. We try to fix this, but we need to
1006 * be sure that the BIOS didn't forget to assign an address
1007 * to the device. [mj]
1009 has_io = has_mem = 0;
1010 for(i=0; i<6; i++) {
1011 unsigned long a = dev->base_address[i];
1012 if (a & PCI_BASE_ADDRESS_SPACE_IO) {
1013 has_io = 1;
1014 a &= PCI_BASE_ADDRESS_IO_MASK;
1015 if (!a || a == PCI_BASE_ADDRESS_IO_MASK)
1016 pcibios_fixup_io_addr(dev, i);
1017 } else if (a & PCI_BASE_ADDRESS_MEM_MASK)
1018 has_mem = 1;
1021 * Don't enable VGA-compatible cards since they have
1022 * fixed I/O and memory space.
1024 * Don't enabled disabled IDE interfaces either because
1025 * some BIOSes may reallocate the same address when they
1026 * find that no devices are attached.
1028 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) &&
1029 ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)) {
1030 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1031 if (has_io && !(cmd & PCI_COMMAND_IO)) {
1032 printk("PCI: Enabling I/O for device %02x:%02x\n",
1033 dev->bus->number, dev->devfn);
1034 cmd |= PCI_COMMAND_IO;
1035 pci_write_config_word(dev, PCI_COMMAND, cmd);
1037 if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
1038 printk("PCI: Enabling memory for device %02x:%02x\n",
1039 dev->bus->number, dev->devfn);
1040 cmd |= PCI_COMMAND_MEMORY;
1041 pci_write_config_word(dev, PCI_COMMAND, cmd);
1044 #ifdef __SMP__
1046 * Recalculate IRQ numbers if we use the I/O APIC
1048 * NOTE! If the "original" interrupt is marked as an old-fashioned
1049 * irq, we have to keep it old-fashioned even if it's a PCI device
1050 * and we could have found it in the MP-table transform.
1052 if (IO_APIC_IRQ(dev->irq)) {
1053 int irq;
1054 unsigned char pin;
1056 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1057 if (pin) {
1058 pin--; /* interrupt pins are numbered starting from 1 */
1059 irq = IO_APIC_get_PCI_irq_vector (dev->bus->number, PCI_SLOT(dev->devfn), pin);
1060 if (irq >= 0) {
1061 printk("PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
1062 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
1063 dev->irq = irq;
1067 #endif
1069 * Fix out-of-range IRQ numbers
1071 if (dev->irq >= NR_IRQS)
1072 dev->irq = 0;
1077 * Arch-dependent fixups.
1080 __initfunc(void pcibios_fixup(void))
1082 pcibios_fixup_peer_bridges();
1083 pcibios_fixup_devices();
1085 #ifdef CONFIG_PCI_BIOS
1086 if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
1087 pcibios_sort();
1088 #endif
1091 __initfunc(void pcibios_fixup_bus(struct pci_bus *b))
1093 pcibios_fixup_ghosts(b);
1097 * Initialization. Try all known PCI access methods. Note that we support
1098 * using both PCI BIOS and direct access: in such cases, we use I/O ports
1099 * to access config space, but we still keep BIOS order of cards to be
1100 * compatible with 2.0.X. This should go away in 2.3.
1103 __initfunc(void pcibios_init(void))
1105 struct pci_access *bios = NULL;
1106 struct pci_access *dir = NULL;
1108 #ifdef CONFIG_PCI_BIOS
1109 if ((pci_probe & PCI_PROBE_BIOS) && ((bios = pci_find_bios())))
1110 pci_probe |= PCI_BIOS_SORT;
1111 #endif
1112 #ifdef CONFIG_PCI_DIRECT
1113 if (pci_probe & (PCI_PROBE_CONF1 | PCI_PROBE_CONF2))
1114 dir = pci_check_direct();
1115 #endif
1116 if (dir)
1117 access_pci = dir;
1118 else if (bios)
1119 access_pci = bios;
1122 #if !defined(CONFIG_PCI_BIOS) && !defined(CONFIG_PCI_DIRECT)
1123 #error PCI configured with neither PCI BIOS or PCI direct access support.
1124 #endif
1126 __initfunc(char *pcibios_setup(char *str))
1128 if (!strcmp(str, "off")) {
1129 pci_probe = 0;
1130 return NULL;
1131 } else if (!strncmp(str, "io=", 3)) {
1132 char *p;
1133 unsigned int x = simple_strtoul(str+3, &p, 16);
1134 if (p && *p)
1135 return str;
1136 pci_last_io_addr = x;
1137 return NULL;
1139 #ifdef CONFIG_PCI_BIOS
1140 else if (!strcmp(str, "bios")) {
1141 pci_probe = PCI_PROBE_BIOS;
1142 return NULL;
1143 } else if (!strcmp(str, "nobios")) {
1144 pci_probe &= ~PCI_PROBE_BIOS;
1145 return NULL;
1146 } else if (!strcmp(str, "nosort")) {
1147 pci_probe |= PCI_NO_SORT;
1148 return NULL;
1150 #endif
1151 #ifdef CONFIG_PCI_DIRECT
1152 else if (!strcmp(str, "conf1")) {
1153 pci_probe = PCI_PROBE_CONF1;
1154 return NULL;
1156 else if (!strcmp(str, "conf2")) {
1157 pci_probe = PCI_PROBE_CONF2;
1158 return NULL;
1160 #endif
1161 return str;