2 * bios32.c - Low-Level PCI Access
4 * $Id: bios32.c,v 1.48 1998/09/26 08:06:55 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
173 #define PCI_NO_CHECKS 0x400
175 static unsigned int pci_probe
= PCI_PROBE_BIOS
| PCI_PROBE_CONF1
| PCI_PROBE_CONF2
;
178 * Direct access to PCI hardware...
181 #ifdef CONFIG_PCI_DIRECT
184 * Functions for accessing PCI configuration space with type 1 accesses
187 #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (bus << 16) | (device_fn << 8) | (where & ~3))
189 static int pci_conf1_read_config_byte(unsigned char bus
, unsigned char device_fn
,
190 unsigned char where
, unsigned char *value
)
192 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
193 *value
= inb(0xCFC + (where
&3));
194 return PCIBIOS_SUCCESSFUL
;
197 static int pci_conf1_read_config_word (unsigned char bus
,
198 unsigned char device_fn
, unsigned char where
, unsigned short *value
)
200 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
201 *value
= inw(0xCFC + (where
&2));
202 return PCIBIOS_SUCCESSFUL
;
205 static int pci_conf1_read_config_dword (unsigned char bus
, unsigned char device_fn
,
206 unsigned char where
, unsigned int *value
)
208 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
210 return PCIBIOS_SUCCESSFUL
;
213 static int pci_conf1_write_config_byte (unsigned char bus
, unsigned char device_fn
,
214 unsigned char where
, unsigned char value
)
216 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
217 outb(value
, 0xCFC + (where
&3));
218 return PCIBIOS_SUCCESSFUL
;
221 static int pci_conf1_write_config_word (unsigned char bus
, unsigned char device_fn
,
222 unsigned char where
, unsigned short value
)
224 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
225 outw(value
, 0xCFC + (where
&2));
226 return PCIBIOS_SUCCESSFUL
;
229 static int pci_conf1_write_config_dword (unsigned char bus
, unsigned char device_fn
,
230 unsigned char where
, unsigned int value
)
232 outl(CONFIG_CMD(bus
,device_fn
,where
), 0xCF8);
234 return PCIBIOS_SUCCESSFUL
;
239 static struct pci_access pci_direct_conf1
= {
241 pci_conf1_read_config_byte
,
242 pci_conf1_read_config_word
,
243 pci_conf1_read_config_dword
,
244 pci_conf1_write_config_byte
,
245 pci_conf1_write_config_word
,
246 pci_conf1_write_config_dword
250 * Functions for accessing PCI configuration space with type 2 accesses
253 #define IOADDR(devfn, where) ((0xC000 | ((devfn & 0x78) << 5)) + where)
254 #define FUNC(devfn) (((devfn & 7) << 1) | 0xf0)
256 static int pci_conf2_read_config_byte(unsigned char bus
, unsigned char device_fn
,
257 unsigned char where
, unsigned char *value
)
259 if (device_fn
& 0x80)
260 return PCIBIOS_DEVICE_NOT_FOUND
;
261 outb (FUNC(device_fn
), 0xCF8);
263 *value
= inb(IOADDR(device_fn
,where
));
265 return PCIBIOS_SUCCESSFUL
;
268 static int pci_conf2_read_config_word (unsigned char bus
, unsigned char device_fn
,
269 unsigned char where
, unsigned short *value
)
271 if (device_fn
& 0x80)
272 return PCIBIOS_DEVICE_NOT_FOUND
;
273 outb (FUNC(device_fn
), 0xCF8);
275 *value
= inw(IOADDR(device_fn
,where
));
277 return PCIBIOS_SUCCESSFUL
;
280 static int pci_conf2_read_config_dword (unsigned char bus
, unsigned char device_fn
,
281 unsigned char where
, unsigned int *value
)
283 if (device_fn
& 0x80)
284 return PCIBIOS_DEVICE_NOT_FOUND
;
285 outb (FUNC(device_fn
), 0xCF8);
287 *value
= inl (IOADDR(device_fn
,where
));
289 return PCIBIOS_SUCCESSFUL
;
292 static int pci_conf2_write_config_byte (unsigned char bus
, unsigned char device_fn
,
293 unsigned char where
, unsigned char value
)
295 if (device_fn
& 0x80)
296 return PCIBIOS_DEVICE_NOT_FOUND
;
297 outb (FUNC(device_fn
), 0xCF8);
299 outb (value
, IOADDR(device_fn
,where
));
301 return PCIBIOS_SUCCESSFUL
;
304 static int pci_conf2_write_config_word (unsigned char bus
, unsigned char device_fn
,
305 unsigned char where
, unsigned short value
)
307 if (device_fn
& 0x80)
308 return PCIBIOS_DEVICE_NOT_FOUND
;
309 outb (FUNC(device_fn
), 0xCF8);
311 outw (value
, IOADDR(device_fn
,where
));
313 return PCIBIOS_SUCCESSFUL
;
316 static int pci_conf2_write_config_dword (unsigned char bus
, unsigned char device_fn
,
317 unsigned char where
, unsigned int value
)
319 if (device_fn
& 0x80)
320 return PCIBIOS_DEVICE_NOT_FOUND
;
321 outb (FUNC(device_fn
), 0xCF8);
323 outl (value
, IOADDR(device_fn
,where
));
325 return PCIBIOS_SUCCESSFUL
;
331 static struct pci_access pci_direct_conf2
= {
333 pci_conf2_read_config_byte
,
334 pci_conf2_read_config_word
,
335 pci_conf2_read_config_dword
,
336 pci_conf2_write_config_byte
,
337 pci_conf2_write_config_word
,
338 pci_conf2_write_config_dword
342 * Before we decide to use direct hardware access mechanisms, we try to do some
343 * trivial checks to ensure it at least _seems_ to be working -- we just test
344 * whether bus 00 contains a host bridge (this is similar to checking
345 * techniques used in XFree86, but ours should be more reliable since we
346 * attempt to make use of direct access hints provided by the PCI BIOS).
348 * This should be close to trivial, but it isn't, because there are buggy
349 * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
351 __initfunc(int pci_sanity_check(struct pci_access
*a
))
356 return 1; /* Lithium PCI Bridges are non-standard */
359 if (pci_probe
& PCI_NO_CHECKS
)
361 for(dfn
=0; dfn
< 0x100; dfn
++)
362 if ((!a
->read_config_word(0, dfn
, PCI_CLASS_DEVICE
, &x
) &&
363 (x
== PCI_CLASS_BRIDGE_HOST
|| x
== PCI_CLASS_DISPLAY_VGA
)) ||
364 (!a
->read_config_word(0, dfn
, PCI_VENDOR_ID
, &x
) &&
365 (x
== PCI_VENDOR_ID_INTEL
|| x
== PCI_VENDOR_ID_COMPAQ
)))
367 DBG("PCI: Sanity check failed\n");
371 __initfunc(static struct pci_access
*pci_check_direct(void))
376 __save_flags(flags
); __cli();
379 * Check if configuration type 1 works.
381 if (pci_probe
& PCI_PROBE_CONF1
) {
384 outl (0x80000000, 0xCF8);
385 if (inl (0xCF8) == 0x80000000 &&
386 pci_sanity_check(&pci_direct_conf1
)) {
388 __restore_flags(flags
);
389 printk("PCI: Using configuration type 1\n");
390 return &pci_direct_conf1
;
396 * Check if configuration type 2 works.
398 if (pci_probe
& PCI_PROBE_CONF2
) {
402 if (inb (0xCF8) == 0x00 && inb (0xCFA) == 0x00 &&
403 pci_sanity_check(&pci_direct_conf2
)) {
404 __restore_flags(flags
);
405 printk("PCI: Using configuration type 2\n");
406 return &pci_direct_conf2
;
410 __restore_flags(flags
);
417 * BIOS32 and PCI BIOS handling.
420 #ifdef CONFIG_PCI_BIOS
422 #define PCIBIOS_PCI_FUNCTION_ID 0xb1XX
423 #define PCIBIOS_PCI_BIOS_PRESENT 0xb101
424 #define PCIBIOS_FIND_PCI_DEVICE 0xb102
425 #define PCIBIOS_FIND_PCI_CLASS_CODE 0xb103
426 #define PCIBIOS_GENERATE_SPECIAL_CYCLE 0xb106
427 #define PCIBIOS_READ_CONFIG_BYTE 0xb108
428 #define PCIBIOS_READ_CONFIG_WORD 0xb109
429 #define PCIBIOS_READ_CONFIG_DWORD 0xb10a
430 #define PCIBIOS_WRITE_CONFIG_BYTE 0xb10b
431 #define PCIBIOS_WRITE_CONFIG_WORD 0xb10c
432 #define PCIBIOS_WRITE_CONFIG_DWORD 0xb10d
434 /* BIOS32 signature: "_32_" */
435 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
437 /* PCI signature: "PCI " */
438 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
440 /* PCI service signature: "$PCI" */
441 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
443 /* PCI BIOS hardware mechanism flags */
444 #define PCIBIOS_HW_TYPE1 0x01
445 #define PCIBIOS_HW_TYPE2 0x02
446 #define PCIBIOS_HW_TYPE1_SPEC 0x10
447 #define PCIBIOS_HW_TYPE2_SPEC 0x20
450 * This is the standard structure used to identify the entry point
451 * to the BIOS32 Service Directory, as documented in
452 * Standard BIOS 32-bit Service Directory Proposal
453 * Revision 0.4 May 24, 1993
454 * Phoenix Technologies Ltd.
456 * and the PCI BIOS specification.
461 unsigned long signature
; /* _32_ */
462 unsigned long entry
; /* 32 bit physical address */
463 unsigned char revision
; /* Revision level, 0 */
464 unsigned char length
; /* Length in paragraphs should be 01 */
465 unsigned char checksum
; /* All bytes must add up to zero */
466 unsigned char reserved
[5]; /* Must be zero */
472 * Physical address of the service directory. I don't know if we're
473 * allowed to have more than one of these or not, so just in case
474 * we'll make pcibios_present() take a memory start parameter and store
479 unsigned long address
;
480 unsigned short segment
;
481 } bios32_indirect
= { 0, __KERNEL_CS
};
484 * Returns the entry point for the given service, NULL on error
487 static unsigned long bios32_service(unsigned long service
)
489 unsigned char return_code
; /* %al */
490 unsigned long address
; /* %ebx */
491 unsigned long length
; /* %ecx */
492 unsigned long entry
; /* %edx */
495 spin_lock_irqsave(&pci_lock
, flags
);
496 __asm__("lcall (%%edi)"
497 : "=a" (return_code
),
503 "D" (&bios32_indirect
));
504 spin_unlock_irqrestore(&pci_lock
, flags
);
506 switch (return_code
) {
508 return address
+ entry
;
509 case 0x80: /* Not present */
510 printk("bios32_service(0x%lx): not present\n", service
);
512 default: /* Shouldn't happen */
513 printk("bios32_service(0x%lx): returned 0x%x, report to <mj@ucw.cz>.\n",
514 service
, return_code
);
520 unsigned long address
;
521 unsigned short segment
;
522 } pci_indirect
= { 0, __KERNEL_CS
};
524 __initfunc(static int check_pcibios(void))
526 u32 signature
, eax
, ebx
, ecx
;
527 u8 status
, major_ver
, minor_ver
, hw_mech
, last_bus
;
528 unsigned long flags
, pcibios_entry
;
530 if ((pcibios_entry
= bios32_service(PCI_SERVICE
))) {
531 pci_indirect
.address
= pcibios_entry
+ PAGE_OFFSET
;
533 __save_flags(flags
); __cli();
543 : "1" (PCIBIOS_PCI_BIOS_PRESENT
),
546 __restore_flags(flags
);
548 status
= (eax
>> 8) & 0xff;
549 hw_mech
= eax
& 0xff;
550 major_ver
= (ebx
>> 8) & 0xff;
551 minor_ver
= ebx
& 0xff;
552 last_bus
= ecx
& 0xff;
553 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
554 status
, hw_mech
, major_ver
, minor_ver
, last_bus
);
555 if (status
|| signature
!= PCI_SIGNATURE
) {
556 printk (KERN_ERR
"PCI: BIOS BUG #%x[%08x] found, report to <mj@ucw.cz>\n",
560 printk("PCI: PCI BIOS revision %x.%02x entry at 0x%lx\n",
561 major_ver
, minor_ver
, pcibios_entry
);
562 #ifdef CONFIG_PCI_DIRECT
563 if (!(hw_mech
& PCIBIOS_HW_TYPE1
))
564 pci_probe
&= ~PCI_PROBE_CONF1
;
565 if (!(hw_mech
& PCIBIOS_HW_TYPE2
))
566 pci_probe
&= ~PCI_PROBE_CONF2
;
575 static int pci_bios_find_class (unsigned int class_code
, unsigned short index
,
576 unsigned char *bus
, unsigned char *device_fn
)
581 __asm__ ("lcall (%%edi)\n\t"
587 : "1" (PCIBIOS_FIND_PCI_CLASS_CODE
),
590 "D" (&pci_indirect
));
591 *bus
= (bx
>> 8) & 0xff;
592 *device_fn
= bx
& 0xff;
593 return (int) (ret
& 0xff00) >> 8;
598 __initfunc(static int pci_bios_find_device (unsigned short vendor
, unsigned short device_id
,
599 unsigned short index
, unsigned char *bus
, unsigned char *device_fn
))
604 __asm__("lcall (%%edi)\n\t"
610 : "1" (PCIBIOS_FIND_PCI_DEVICE
),
614 "D" (&pci_indirect
));
615 *bus
= (bx
>> 8) & 0xff;
616 *device_fn
= bx
& 0xff;
617 return (int) (ret
& 0xff00) >> 8;
620 static int pci_bios_read_config_byte(unsigned char bus
,
621 unsigned char device_fn
, unsigned char where
, unsigned char *value
)
624 unsigned long bx
= (bus
<< 8) | device_fn
;
626 __asm__("lcall (%%esi)\n\t"
632 : "1" (PCIBIOS_READ_CONFIG_BYTE
),
635 "S" (&pci_indirect
));
636 return (int) (ret
& 0xff00) >> 8;
639 static int pci_bios_read_config_word (unsigned char bus
,
640 unsigned char device_fn
, unsigned char where
, unsigned short *value
)
643 unsigned long bx
= (bus
<< 8) | device_fn
;
645 __asm__("lcall (%%esi)\n\t"
651 : "1" (PCIBIOS_READ_CONFIG_WORD
),
654 "S" (&pci_indirect
));
655 return (int) (ret
& 0xff00) >> 8;
658 static int pci_bios_read_config_dword (unsigned char bus
,
659 unsigned char device_fn
, unsigned char where
, unsigned int *value
)
662 unsigned long bx
= (bus
<< 8) | device_fn
;
664 __asm__("lcall (%%esi)\n\t"
670 : "1" (PCIBIOS_READ_CONFIG_DWORD
),
673 "S" (&pci_indirect
));
674 return (int) (ret
& 0xff00) >> 8;
677 static int pci_bios_write_config_byte (unsigned char bus
,
678 unsigned char device_fn
, unsigned char where
, unsigned char value
)
681 unsigned long bx
= (bus
<< 8) | device_fn
;
683 __asm__("lcall (%%esi)\n\t"
688 : "0" (PCIBIOS_WRITE_CONFIG_BYTE
),
692 "S" (&pci_indirect
));
693 return (int) (ret
& 0xff00) >> 8;
696 static int pci_bios_write_config_word (unsigned char bus
,
697 unsigned char device_fn
, unsigned char where
, unsigned short value
)
700 unsigned long bx
= (bus
<< 8) | device_fn
;
702 __asm__("lcall (%%esi)\n\t"
707 : "0" (PCIBIOS_WRITE_CONFIG_WORD
),
711 "S" (&pci_indirect
));
712 return (int) (ret
& 0xff00) >> 8;
715 static int pci_bios_write_config_dword (unsigned char bus
,
716 unsigned char device_fn
, unsigned char where
, unsigned int value
)
719 unsigned long bx
= (bus
<< 8) | device_fn
;
721 __asm__("lcall (%%esi)\n\t"
726 : "0" (PCIBIOS_WRITE_CONFIG_DWORD
),
730 "S" (&pci_indirect
));
731 return (int) (ret
& 0xff00) >> 8;
735 * Function table for BIOS32 access
738 static struct pci_access pci_bios_access
= {
740 pci_bios_read_config_byte
,
741 pci_bios_read_config_word
,
742 pci_bios_read_config_dword
,
743 pci_bios_write_config_byte
,
744 pci_bios_write_config_word
,
745 pci_bios_write_config_dword
749 * Try to find PCI BIOS.
752 __initfunc(static struct pci_access
*pci_find_bios(void))
759 * Follow the standard procedure for locating the BIOS32 Service
760 * directory by scanning the permissible address range from
761 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
764 for (check
= (union bios32
*) __va(0xe0000);
765 check
<= (union bios32
*) __va(0xffff0);
767 if (check
->fields
.signature
!= BIOS32_SIGNATURE
)
769 length
= check
->fields
.length
* 16;
773 for (i
= 0; i
< length
; ++i
)
774 sum
+= check
->chars
[i
];
777 if (check
->fields
.revision
!= 0) {
778 printk("PCI: unsupported BIOS32 revision %d at 0x%p, report to <mj@ucw.cz>\n",
779 check
->fields
.revision
, check
);
782 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check
);
783 if (check
->fields
.entry
>= 0x100000) {
784 printk("PCI: BIOS32 entry (0x%p) in high memory, cannot use.\n", check
);
787 unsigned long bios32_entry
= check
->fields
.entry
;
788 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n", bios32_entry
);
789 bios32_indirect
.address
= bios32_entry
+ PAGE_OFFSET
;
791 return &pci_bios_access
;
793 break; /* Hopefully more than one BIOS32 cannot happen... */
800 * Sort the device list according to PCI BIOS. Nasty hack, but since some
801 * fool forgot to define the `correct' device order in the PCI BIOS specs
802 * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
803 * which used BIOS ordering, we are bound to do this...
806 __initfunc(void pcibios_sort(void))
808 struct pci_dev
*dev
= pci_devices
;
809 struct pci_dev
**last
= &pci_devices
;
810 struct pci_dev
*d
, **dd
, *e
;
812 unsigned char bus
, devfn
;
814 DBG("PCI: Sorting device list...\n");
817 while (pci_bios_find_device(e
->vendor
, e
->device
, idx
, &bus
, &devfn
) == PCIBIOS_SUCCESSFUL
) {
819 for(dd
=&dev
; (d
= *dd
); dd
= &d
->next
) {
820 if (d
->bus
->number
== bus
&& d
->devfn
== devfn
) {
828 printk("PCI: BIOS reporting unknown device %02x:%02x\n", bus
, devfn
);
830 * We must not continue scanning as several buggy BIOSes
831 * return garbage after the last device. Grr.
837 printk("PCI: Device %02x:%02x not found by BIOS\n",
838 dev
->bus
->number
, dev
->devfn
);
851 * Several BIOS'es forget to assign addresses to I/O ranges.
852 * We try to fix it here, expecting there are free addresses
853 * starting with 0x5800. Ugly, but until we come with better
854 * resource management, it's the only simple solution.
857 static int pci_last_io_addr __initdata
= 0x5800;
859 __initfunc(void pcibios_fixup_io_addr(struct pci_dev
*dev
, int idx
))
862 unsigned int reg
= PCI_BASE_ADDRESS_0
+ 4*idx
;
863 unsigned int size
, addr
, try;
864 unsigned int bus
= dev
->bus
->number
;
865 unsigned int devfn
= dev
->devfn
;
867 if (!pci_last_io_addr
) {
868 printk("PCI: Unassigned I/O space for %02x:%02x\n", bus
, devfn
);
871 if ((dev
->class >> 8) == PCI_CLASS_STORAGE_IDE
&& idx
< 4) {
873 * In case the BIOS didn't assign an address 0--3 to an IDE
874 * controller, we don't try to fix it as it means "use default
875 * addresses" at least with several broken chips and the IDE
876 * driver needs the original settings to recognize which devices
877 * correspond to the primary controller.
881 pcibios_read_config_word(bus
, devfn
, PCI_COMMAND
, &cmd
);
882 pcibios_write_config_word(bus
, devfn
, PCI_COMMAND
, cmd
& ~PCI_COMMAND_IO
);
883 pcibios_write_config_dword(bus
, devfn
, reg
, ~0);
884 pcibios_read_config_dword(bus
, devfn
, reg
, &size
);
885 size
= (~(size
& PCI_BASE_ADDRESS_IO_MASK
) & 0xffff) + 1;
887 if (!size
|| size
> 0x100)
888 printk("PCI: Unable to handle I/O allocation for %02x:%02x (%04x), tell <mj@ucw.cz>\n", bus
, devfn
, size
);
891 addr
= (pci_last_io_addr
+ size
- 1) & ~(size
-1);
892 pci_last_io_addr
= addr
+ size
;
893 } while (check_region(addr
, size
));
894 printk("PCI: Assigning I/O space %04x-%04x to device %02x:%02x\n", addr
, addr
+size
-1, bus
, devfn
);
895 pcibios_write_config_dword(bus
, devfn
, reg
, addr
| PCI_BASE_ADDRESS_SPACE_IO
);
896 pcibios_read_config_dword(bus
, devfn
, reg
, &try);
897 if ((try & PCI_BASE_ADDRESS_IO_MASK
) != addr
) {
899 printk("PCI: Address setup failed, got %04x\n", try);
901 dev
->base_address
[idx
] = try;
904 pcibios_write_config_dword(bus
, devfn
, reg
, 0);
905 dev
->base_address
[idx
] = 0;
907 pcibios_write_config_word(bus
, devfn
, PCI_COMMAND
, cmd
);
911 * Several buggy motherboards address only 16 devices and mirror
912 * them to next 16 IDs. We try to detect this `feature' on all
913 * primary busses (those containing host bridges as they are
914 * expected to be unique) and remove the ghost devices.
917 __initfunc(void pcibios_fixup_ghosts(struct pci_bus
*b
))
919 struct pci_dev
*d
, *e
, **z
;
920 int mirror
= PCI_DEVFN(16,0);
921 int seen_host_bridge
= 0;
923 DBG("PCI: Scanning for ghost devices on bus %d\n", b
->number
);
924 for(d
=b
->devices
; d
&& d
->devfn
< mirror
; d
=d
->sibling
) {
925 if ((d
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
927 for(e
=d
->next
; e
; e
=e
->sibling
)
928 if (e
->devfn
== d
->devfn
+ mirror
&&
929 e
->vendor
== d
->vendor
&&
930 e
->device
== d
->device
&&
931 e
->class == d
->class &&
932 !memcmp(e
->base_address
, d
->base_address
, sizeof(e
->base_address
)))
937 if (!seen_host_bridge
)
939 printk("PCI: Ignoring ghost devices on bus %d\n", b
->number
);
940 for(e
=b
->devices
; e
->sibling
!= d
; e
=e
->sibling
);
942 for(z
=&pci_devices
; (d
=*z
);)
943 if (d
->bus
== b
&& d
->devfn
>= mirror
) {
945 kfree_s(d
, sizeof(*d
));
951 * In case there are peer host bridges, scan bus behind each of them.
952 * Although several sources claim that the host bridges should have
953 * header type 1 and be assigned a bus number as for PCI2PCI bridges,
954 * the reality doesn't pass this test and the bus number is usually
955 * set by BIOS to the first free value.
957 __initfunc(void pcibios_fixup_peer_bridges(void))
959 struct pci_bus
*b
= &pci_root
;
963 #ifdef CONFIG_PCI_DIRECT
965 * Don't search for peer host bridges if we use config type 2
966 * since it reads bogus values for non-existent busses and
967 * chipsets supporting multiple primary busses use conf1 anyway.
969 if (access_pci
== &pci_direct_conf2
)
972 for(d
=b
->devices
; d
; d
=d
->sibling
)
973 if ((d
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
975 n
= b
->subordinate
+ 1;
979 for(i
=0; i
<256; i
+= 8)
980 if (!pcibios_read_config_word(n
, i
, PCI_VENDOR_ID
, &l
) &&
981 l
!= 0x0000 && l
!= 0xffff) {
982 DBG("Found device at %02x:%02x\n", n
, i
);
984 if (!pcibios_read_config_word(n
, i
, PCI_CLASS_DEVICE
, &l
) &&
985 l
== PCI_CLASS_BRIDGE_HOST
)
991 printk("PCI: Discovered primary peer bus %02x\n", n
);
992 b
= kmalloc(sizeof(*b
), GFP_KERNEL
);
993 memset(b
, 0, sizeof(*b
));
994 b
->next
= pci_root
.next
;
996 b
->number
= b
->secondary
= n
;
997 b
->subordinate
= 0xff;
998 b
->subordinate
= pci_scan_bus(b
);
1006 * Fix base addresses, I/O and memory enables and IRQ's (mostly work-arounds
1007 * for buggy PCI BIOS'es :-[).
1010 __initfunc(void pcibios_fixup_devices(void))
1012 struct pci_dev
*dev
;
1013 int i
, has_io
, has_mem
;
1016 for(dev
= pci_devices
; dev
; dev
=dev
->next
) {
1018 * There are buggy BIOSes that forget to enable I/O and memory
1019 * access to PCI devices. We try to fix this, but we need to
1020 * be sure that the BIOS didn't forget to assign an address
1021 * to the device. [mj]
1023 has_io
= has_mem
= 0;
1024 for(i
=0; i
<6; i
++) {
1025 unsigned long a
= dev
->base_address
[i
];
1026 if (a
& PCI_BASE_ADDRESS_SPACE_IO
) {
1028 a
&= PCI_BASE_ADDRESS_IO_MASK
;
1029 if (!a
|| a
== PCI_BASE_ADDRESS_IO_MASK
)
1030 pcibios_fixup_io_addr(dev
, i
);
1031 } else if (a
& PCI_BASE_ADDRESS_MEM_MASK
)
1035 * Don't enable VGA-compatible cards since they have
1036 * fixed I/O and memory space.
1038 * Don't enabled disabled IDE interfaces either because
1039 * some BIOSes may reallocate the same address when they
1040 * find that no devices are attached.
1042 if (((dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
) &&
1043 ((dev
->class >> 8) != PCI_CLASS_STORAGE_IDE
)) {
1044 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
1045 if (has_io
&& !(cmd
& PCI_COMMAND_IO
)) {
1046 printk("PCI: Enabling I/O for device %02x:%02x\n",
1047 dev
->bus
->number
, dev
->devfn
);
1048 cmd
|= PCI_COMMAND_IO
;
1049 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
1051 if (has_mem
&& !(cmd
& PCI_COMMAND_MEMORY
)) {
1052 printk("PCI: Enabling memory for device %02x:%02x\n",
1053 dev
->bus
->number
, dev
->devfn
);
1054 cmd
|= PCI_COMMAND_MEMORY
;
1055 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
1058 #if defined(CONFIG_X86_IO_APIC)
1060 * Recalculate IRQ numbers if we use the I/O APIC
1066 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
1068 pin
--; /* interrupt pins are numbered starting from 1 */
1069 irq
= IO_APIC_get_PCI_irq_vector(dev
->bus
->number
, PCI_SLOT(dev
->devfn
), pin
);
1070 if (irq
< 0 && dev
->bus
->parent
) { /* go back to the bridge */
1071 struct pci_dev
* bridge
= dev
->bus
->self
;
1073 pin
= (pin
+ PCI_SLOT(dev
->devfn
)) % 4;
1074 irq
= IO_APIC_get_PCI_irq_vector(bridge
->bus
->number
,
1075 PCI_SLOT(bridge
->devfn
), pin
);
1077 printk(KERN_WARNING
"PCI: using PPB(B%d,I%d,P%d) to get irq %d\n",
1078 bridge
->bus
->number
, PCI_SLOT(bridge
->devfn
), pin
, irq
);
1081 printk("PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
1082 dev
->bus
->number
, PCI_SLOT(dev
->devfn
), pin
, irq
);
1089 * Fix out-of-range IRQ numbers
1091 if (dev
->irq
>= NR_IRQS
)
1097 * Arch-dependent fixups.
1100 __initfunc(void pcibios_fixup(void))
1102 pcibios_fixup_peer_bridges();
1103 pcibios_fixup_devices();
1105 #ifdef CONFIG_PCI_BIOS
1106 if ((pci_probe
& PCI_BIOS_SORT
) && !(pci_probe
& PCI_NO_SORT
))
1111 __initfunc(void pcibios_fixup_bus(struct pci_bus
*b
))
1113 pcibios_fixup_ghosts(b
);
1117 * Initialization. Try all known PCI access methods. Note that we support
1118 * using both PCI BIOS and direct access: in such cases, we use I/O ports
1119 * to access config space, but we still keep BIOS order of cards to be
1120 * compatible with 2.0.X. This should go away in 2.3.
1123 __initfunc(void pcibios_init(void))
1125 struct pci_access
*bios
= NULL
;
1126 struct pci_access
*dir
= NULL
;
1128 #ifdef CONFIG_PCI_BIOS
1129 if ((pci_probe
& PCI_PROBE_BIOS
) && ((bios
= pci_find_bios())))
1130 pci_probe
|= PCI_BIOS_SORT
;
1132 #ifdef CONFIG_PCI_DIRECT
1133 if (pci_probe
& (PCI_PROBE_CONF1
| PCI_PROBE_CONF2
))
1134 dir
= pci_check_direct();
1142 #if !defined(CONFIG_PCI_BIOS) && !defined(CONFIG_PCI_DIRECT)
1143 #error PCI configured with neither PCI BIOS or PCI direct access support.
1146 __initfunc(char *pcibios_setup(char *str
))
1148 if (!strcmp(str
, "off")) {
1151 } else if (!strncmp(str
, "io=", 3)) {
1153 unsigned int x
= simple_strtoul(str
+3, &p
, 16);
1156 pci_last_io_addr
= x
;
1159 #ifdef CONFIG_PCI_BIOS
1160 else if (!strcmp(str
, "bios")) {
1161 pci_probe
= PCI_PROBE_BIOS
;
1163 } else if (!strcmp(str
, "nobios")) {
1164 pci_probe
&= ~PCI_PROBE_BIOS
;
1166 } else if (!strcmp(str
, "nosort")) {
1167 pci_probe
|= PCI_NO_SORT
;
1171 #ifdef CONFIG_PCI_DIRECT
1172 else if (!strcmp(str
, "conf1")) {
1173 pci_probe
= PCI_PROBE_CONF1
| PCI_NO_CHECKS
;
1176 else if (!strcmp(str
, "conf2")) {
1177 pci_probe
= PCI_PROBE_CONF2
| PCI_NO_CHECKS
;