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
8 * (Unix and Linux consulting and custom programming)
12 * Drew's work was sponsored by:
13 * iX Multiuser Multitasking Magazine
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
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.
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>
86 #include <asm/segment.h>
87 #include <asm/system.h>
90 #include <asm/spinlock.h>
97 #define DBG(x...) printk(x)
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.
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 */
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) \
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); \
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);
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);
233 return PCIBIOS_SUCCESSFUL
;
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);
262 *value
= inb(IOADDR(device_fn
,where
));
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);
274 *value
= inw(IOADDR(device_fn
,where
));
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);
286 *value
= inl (IOADDR(device_fn
,where
));
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);
298 outb (value
, IOADDR(device_fn
,where
));
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);
310 outw (value
, IOADDR(device_fn
,where
));
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);
322 outl (value
, IOADDR(device_fn
,where
));
324 return PCIBIOS_SUCCESSFUL
;
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
))
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
)
355 DBG("PCI: Sanity check failed\n");
359 __initfunc(static struct pci_access
*pci_check_direct(void))
364 __save_flags(flags
); __cli();
367 * Check if configuration type 1 works.
369 if (pci_probe
& PCI_PROBE_CONF1
) {
372 outl (0x80000000, 0xCF8);
373 if (inl (0xCF8) == 0x80000000 &&
374 pci_sanity_check(&pci_direct_conf1
)) {
376 __restore_flags(flags
);
377 printk("PCI: Using configuration type 1\n");
378 return &pci_direct_conf1
;
384 * Check if configuration type 2 works.
386 if (pci_probe
& PCI_PROBE_CONF2
) {
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
);
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.
444 * and the PCI BIOS specification.
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 */
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
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 */
483 spin_lock_irqsave(&pci_lock
, flags
);
484 __asm__("lcall (%%edi)"
485 : "=a" (return_code
),
491 "D" (&bios32_indirect
));
492 spin_unlock_irqrestore(&pci_lock
, flags
);
494 switch (return_code
) {
496 return address
+ entry
;
497 case 0x80: /* Not present */
498 printk("bios32_service(0x%lx): not present\n", service
);
500 default: /* Shouldn't happen */
501 printk("bios32_service(0x%lx): returned 0x%x, report to <mj@ucw.cz>.\n",
502 service
, return_code
);
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();
531 : "1" (PCIBIOS_PCI_BIOS_PRESENT
),
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",
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
;
563 static int pci_bios_find_class (unsigned int class_code
, unsigned short index
,
564 unsigned char *bus
, unsigned char *device_fn
)
569 __asm__ ("lcall (%%edi)\n\t"
575 : "1" (PCIBIOS_FIND_PCI_CLASS_CODE
),
578 "D" (&pci_indirect
));
579 *bus
= (bx
>> 8) & 0xff;
580 *device_fn
= bx
& 0xff;
581 return (int) (ret
& 0xff00) >> 8;
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
))
592 __asm__("lcall (%%edi)\n\t"
598 : "1" (PCIBIOS_FIND_PCI_DEVICE
),
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
)
612 unsigned long bx
= (bus
<< 8) | device_fn
;
614 __asm__("lcall (%%esi)\n\t"
620 : "1" (PCIBIOS_READ_CONFIG_BYTE
),
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
)
631 unsigned long bx
= (bus
<< 8) | device_fn
;
633 __asm__("lcall (%%esi)\n\t"
639 : "1" (PCIBIOS_READ_CONFIG_WORD
),
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
)
650 unsigned long bx
= (bus
<< 8) | device_fn
;
652 __asm__("lcall (%%esi)\n\t"
658 : "1" (PCIBIOS_READ_CONFIG_DWORD
),
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
)
669 unsigned long bx
= (bus
<< 8) | device_fn
;
671 __asm__("lcall (%%esi)\n\t"
676 : "0" (PCIBIOS_WRITE_CONFIG_BYTE
),
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
)
688 unsigned long bx
= (bus
<< 8) | device_fn
;
690 __asm__("lcall (%%esi)\n\t"
695 : "0" (PCIBIOS_WRITE_CONFIG_WORD
),
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
)
707 unsigned long bx
= (bus
<< 8) | device_fn
;
709 __asm__("lcall (%%esi)\n\t"
714 : "0" (PCIBIOS_WRITE_CONFIG_DWORD
),
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))
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);
755 if (check
->fields
.signature
!= BIOS32_SIGNATURE
)
757 length
= check
->fields
.length
* 16;
761 for (i
= 0; i
< length
; ++i
)
762 sum
+= check
->chars
[i
];
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
);
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
);
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
;
779 return &pci_bios_access
;
781 break; /* Hopefully more than one BIOS32 cannot happen... */
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
;
800 unsigned char bus
, devfn
;
802 DBG("PCI: Sorting device list...\n");
805 while (pci_bios_find_device(e
->vendor
, e
->device
, idx
, &bus
, &devfn
) == PCIBIOS_SUCCESSFUL
) {
807 for(dd
=&dev
; (d
= *dd
); dd
= &d
->next
) {
808 if (d
->bus
->number
== bus
&& d
->devfn
== devfn
) {
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.
825 printk("PCI: Device %02x:%02x not found by BIOS\n",
826 dev
->bus
->number
, dev
->devfn
);
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
))
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
);
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.
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;
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
);
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
) {
887 printk("PCI: Address setup failed, got %04x\n", try);
889 dev
->base_address
[idx
] = try;
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
)
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
)))
925 if (!seen_host_bridge
)
927 printk("PCI: Ignoring ghost devices on bus %d\n", b
->number
);
928 for(e
=b
->devices
; e
->sibling
!= d
; e
=e
->sibling
);
930 for(z
=&pci_devices
; (d
=*z
);)
931 if (d
->bus
== b
&& d
->devfn
>= mirror
) {
933 kfree_s(d
, sizeof(*d
));
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
;
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
)
960 for(d
=b
->devices
; d
; d
=d
->sibling
)
961 if ((d
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
964 int n
= b
->subordinate
+1;
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
);
972 if (!pcibios_read_config_word(n
, i
, PCI_CLASS_DEVICE
, &l
) &&
973 l
== PCI_CLASS_BRIDGE_HOST
)
976 if (found
&& cnt
> 0) {
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
;
983 b
->number
= b
->secondary
= n
;
984 b
->subordinate
= 0xff;
985 b
->subordinate
= pci_scan_bus(b
);
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))
999 int i
, has_io
, has_mem
;
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
) {
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
)
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
);
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
)) {
1056 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &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
);
1061 printk("PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
1062 dev
->bus
->number
, PCI_SLOT(dev
->devfn
), pin
, irq
);
1069 * Fix out-of-range IRQ numbers
1071 if (dev
->irq
>= NR_IRQS
)
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
))
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
;
1112 #ifdef CONFIG_PCI_DIRECT
1113 if (pci_probe
& (PCI_PROBE_CONF1
| PCI_PROBE_CONF2
))
1114 dir
= pci_check_direct();
1122 #if !defined(CONFIG_PCI_BIOS) && !defined(CONFIG_PCI_DIRECT)
1123 #error PCI configured with neither PCI BIOS or PCI direct access support.
1126 __initfunc(char *pcibios_setup(char *str
))
1128 if (!strcmp(str
, "off")) {
1131 } else if (!strncmp(str
, "io=", 3)) {
1133 unsigned int x
= simple_strtoul(str
+3, &p
, 16);
1136 pci_last_io_addr
= x
;
1139 #ifdef CONFIG_PCI_BIOS
1140 else if (!strcmp(str
, "bios")) {
1141 pci_probe
= PCI_PROBE_BIOS
;
1143 } else if (!strcmp(str
, "nobios")) {
1144 pci_probe
&= ~PCI_PROBE_BIOS
;
1146 } else if (!strcmp(str
, "nosort")) {
1147 pci_probe
|= PCI_NO_SORT
;
1151 #ifdef CONFIG_PCI_DIRECT
1152 else if (!strcmp(str
, "conf1")) {
1153 pci_probe
= PCI_PROBE_CONF1
;
1156 else if (!strcmp(str
, "conf2")) {
1157 pci_probe
= PCI_PROBE_CONF2
;