2 * BIOS32 and PCI BIOS handling.
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/uaccess.h>
10 #include "pci-functions.h"
13 /* BIOS32 signature: "_32_" */
14 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
16 /* PCI signature: "PCI " */
17 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
19 /* PCI service signature: "$PCI" */
20 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
22 /* PCI BIOS hardware mechanism flags */
23 #define PCIBIOS_HW_TYPE1 0x01
24 #define PCIBIOS_HW_TYPE2 0x02
25 #define PCIBIOS_HW_TYPE1_SPEC 0x10
26 #define PCIBIOS_HW_TYPE2_SPEC 0x20
29 * This is the standard structure used to identify the entry point
30 * to the BIOS32 Service Directory, as documented in
31 * Standard BIOS 32-bit Service Directory Proposal
32 * Revision 0.4 May 24, 1993
33 * Phoenix Technologies Ltd.
35 * and the PCI BIOS specification.
40 unsigned long signature
; /* _32_ */
41 unsigned long entry
; /* 32 bit physical address */
42 unsigned char revision
; /* Revision level, 0 */
43 unsigned char length
; /* Length in paragraphs should be 01 */
44 unsigned char checksum
; /* All bytes must add up to zero */
45 unsigned char reserved
[5]; /* Must be zero */
51 * Physical address of the service directory. I don't know if we're
52 * allowed to have more than one of these or not, so just in case
53 * we'll make pcibios_present() take a memory start parameter and store
58 unsigned long address
;
59 unsigned short segment
;
60 } bios32_indirect
= { 0, __KERNEL_CS
};
63 * Returns the entry point for the given service, NULL on error
66 static unsigned long bios32_service(unsigned long service
)
68 unsigned char return_code
; /* %al */
69 unsigned long address
; /* %ebx */
70 unsigned long length
; /* %ecx */
71 unsigned long entry
; /* %edx */
74 local_irq_save(flags
);
75 __asm__("lcall *(%%edi); cld"
82 "D" (&bios32_indirect
));
83 local_irq_restore(flags
);
85 switch (return_code
) {
87 return address
+ entry
;
88 case 0x80: /* Not present */
89 printk(KERN_WARNING
"bios32_service(0x%lx): not present\n", service
);
91 default: /* Shouldn't happen */
92 printk(KERN_WARNING
"bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
93 service
, return_code
);
99 unsigned long address
;
100 unsigned short segment
;
101 } pci_indirect
= { 0, __KERNEL_CS
};
103 static int pci_bios_present
;
105 static int __devinit
check_pcibios(void)
107 u32 signature
, eax
, ebx
, ecx
;
108 u8 status
, major_ver
, minor_ver
, hw_mech
;
109 unsigned long flags
, pcibios_entry
;
111 if ((pcibios_entry
= bios32_service(PCI_SERVICE
))) {
112 pci_indirect
.address
= pcibios_entry
+ PAGE_OFFSET
;
114 local_irq_save(flags
);
116 "lcall *(%%edi); cld\n\t"
124 : "1" (PCIBIOS_PCI_BIOS_PRESENT
),
127 local_irq_restore(flags
);
129 status
= (eax
>> 8) & 0xff;
130 hw_mech
= eax
& 0xff;
131 major_ver
= (ebx
>> 8) & 0xff;
132 minor_ver
= ebx
& 0xff;
133 if (pcibios_last_bus
< 0)
134 pcibios_last_bus
= ecx
& 0xff;
135 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
136 status
, hw_mech
, major_ver
, minor_ver
, pcibios_last_bus
);
137 if (status
|| signature
!= PCI_SIGNATURE
) {
138 printk (KERN_ERR
"PCI: BIOS BUG #%x[%08x] found\n",
142 printk(KERN_INFO
"PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
143 major_ver
, minor_ver
, pcibios_entry
, pcibios_last_bus
);
144 #ifdef CONFIG_PCI_DIRECT
145 if (!(hw_mech
& PCIBIOS_HW_TYPE1
))
146 pci_probe
&= ~PCI_PROBE_CONF1
;
147 if (!(hw_mech
& PCIBIOS_HW_TYPE2
))
148 pci_probe
&= ~PCI_PROBE_CONF2
;
155 static int __devinit
pci_bios_find_device (unsigned short vendor
, unsigned short device_id
,
156 unsigned short index
, unsigned char *bus
, unsigned char *device_fn
)
161 __asm__("lcall *(%%edi); cld\n\t"
167 : "1" (PCIBIOS_FIND_PCI_DEVICE
),
171 "D" (&pci_indirect
));
172 *bus
= (bx
>> 8) & 0xff;
173 *device_fn
= bx
& 0xff;
174 return (int) (ret
& 0xff00) >> 8;
177 static int pci_bios_read(unsigned int seg
, unsigned int bus
,
178 unsigned int devfn
, int reg
, int len
, u32
*value
)
180 unsigned long result
= 0;
182 unsigned long bx
= (bus
<< 8) | devfn
;
184 if (!value
|| (bus
> 255) || (devfn
> 255) || (reg
> 255))
187 spin_lock_irqsave(&pci_config_lock
, flags
);
191 __asm__("lcall *(%%esi); cld\n\t"
197 : "1" (PCIBIOS_READ_CONFIG_BYTE
),
200 "S" (&pci_indirect
));
201 <<<<<<< HEAD
:arch
/x86
/pci
/pcbios
.c
204 * Zero-extend the result beyond 8 bits, do not trust the
205 * BIOS having done it:
208 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/pci
/pcbios
.c
211 __asm__("lcall *(%%esi); cld\n\t"
217 : "1" (PCIBIOS_READ_CONFIG_WORD
),
220 "S" (&pci_indirect
));
221 <<<<<<< HEAD
:arch
/x86
/pci
/pcbios
.c
224 * Zero-extend the result beyond 16 bits, do not trust the
225 * BIOS having done it:
228 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/pci
/pcbios
.c
231 __asm__("lcall *(%%esi); cld\n\t"
237 : "1" (PCIBIOS_READ_CONFIG_DWORD
),
240 "S" (&pci_indirect
));
244 spin_unlock_irqrestore(&pci_config_lock
, flags
);
246 return (int)((result
& 0xff00) >> 8);
249 static int pci_bios_write(unsigned int seg
, unsigned int bus
,
250 unsigned int devfn
, int reg
, int len
, u32 value
)
252 unsigned long result
= 0;
254 unsigned long bx
= (bus
<< 8) | devfn
;
256 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
259 spin_lock_irqsave(&pci_config_lock
, flags
);
263 __asm__("lcall *(%%esi); cld\n\t"
268 : "0" (PCIBIOS_WRITE_CONFIG_BYTE
),
272 "S" (&pci_indirect
));
275 __asm__("lcall *(%%esi); cld\n\t"
280 : "0" (PCIBIOS_WRITE_CONFIG_WORD
),
284 "S" (&pci_indirect
));
287 __asm__("lcall *(%%esi); cld\n\t"
292 : "0" (PCIBIOS_WRITE_CONFIG_DWORD
),
296 "S" (&pci_indirect
));
300 spin_unlock_irqrestore(&pci_config_lock
, flags
);
302 return (int)((result
& 0xff00) >> 8);
307 * Function table for BIOS32 access
310 static struct pci_raw_ops pci_bios_access
= {
311 .read
= pci_bios_read
,
312 .write
= pci_bios_write
316 * Try to find PCI BIOS.
319 static struct pci_raw_ops
* __devinit
pci_find_bios(void)
326 * Follow the standard procedure for locating the BIOS32 Service
327 * directory by scanning the permissible address range from
328 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
331 for (check
= (union bios32
*) __va(0xe0000);
332 check
<= (union bios32
*) __va(0xffff0);
335 if (probe_kernel_address(&check
->fields
.signature
, sig
))
338 if (check
->fields
.signature
!= BIOS32_SIGNATURE
)
340 length
= check
->fields
.length
* 16;
344 for (i
= 0; i
< length
; ++i
)
345 sum
+= check
->chars
[i
];
348 if (check
->fields
.revision
!= 0) {
349 printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
350 check
->fields
.revision
, check
);
353 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check
);
354 if (check
->fields
.entry
>= 0x100000) {
355 printk("PCI: BIOS32 entry (0x%p) in high memory, "
356 "cannot use.\n", check
);
359 unsigned long bios32_entry
= check
->fields
.entry
;
360 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n",
362 bios32_indirect
.address
= bios32_entry
+ PAGE_OFFSET
;
364 return &pci_bios_access
;
366 break; /* Hopefully more than one BIOS32 cannot happen... */
373 * Sort the device list according to PCI BIOS. Nasty hack, but since some
374 * fool forgot to define the `correct' device order in the PCI BIOS specs
375 * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
376 * which used BIOS ordering, we are bound to do this...
379 void __devinit
pcibios_sort(void)
381 LIST_HEAD(sorted_devices
);
382 struct list_head
*ln
;
383 struct pci_dev
*dev
, *d
;
385 unsigned char bus
, devfn
;
387 DBG("PCI: Sorting device list...\n");
388 while (!list_empty(&pci_devices
)) {
389 ln
= pci_devices
.next
;
392 while (pci_bios_find_device(dev
->vendor
, dev
->device
, idx
, &bus
, &devfn
) == PCIBIOS_SUCCESSFUL
) {
394 list_for_each(ln
, &pci_devices
) {
396 if (d
->bus
->number
== bus
&& d
->devfn
== devfn
) {
397 list_move_tail(&d
->global_list
, &sorted_devices
);
403 if (ln
== &pci_devices
) {
404 printk(KERN_WARNING
"PCI: BIOS reporting unknown device %02x:%02x\n", bus
, devfn
);
406 * We must not continue scanning as several buggy BIOSes
407 * return garbage after the last device. Grr.
413 printk(KERN_WARNING
"PCI: Device %s not found by BIOS\n",
415 list_move_tail(&dev
->global_list
, &sorted_devices
);
418 list_splice(&sorted_devices
, &pci_devices
);
422 * BIOS Functions for IRQ Routing
425 struct irq_routing_options
{
427 struct irq_info
*table
;
429 } __attribute__((packed
));
431 struct irq_routing_table
* pcibios_get_irq_routing_table(void)
433 struct irq_routing_options opt
;
434 struct irq_routing_table
*rt
= NULL
;
438 if (!pci_bios_present
)
440 page
= __get_free_page(GFP_KERNEL
);
443 opt
.table
= (struct irq_info
*) page
;
444 opt
.size
= PAGE_SIZE
;
445 opt
.segment
= __KERNEL_DS
;
447 DBG("PCI: Fetching IRQ routing table... ");
448 __asm__("push %%es\n\t"
451 "lcall *(%%esi); cld\n\t"
459 : "0" (PCIBIOS_GET_ROUTING_OPTIONS
),
465 DBG("OK ret=%d, size=%d, map=%x\n", ret
, opt
.size
, map
);
467 printk(KERN_ERR
"PCI: Error %02x when fetching IRQ routing table.\n", (ret
>> 8) & 0xff);
469 rt
= kmalloc(sizeof(struct irq_routing_table
) + opt
.size
, GFP_KERNEL
);
471 memset(rt
, 0, sizeof(struct irq_routing_table
));
472 rt
->size
= opt
.size
+ sizeof(struct irq_routing_table
);
473 rt
->exclusive_irqs
= map
;
474 memcpy(rt
->slots
, (void *) page
, opt
.size
);
475 printk(KERN_INFO
"PCI: Using BIOS Interrupt Routing Table\n");
481 EXPORT_SYMBOL(pcibios_get_irq_routing_table
);
483 int pcibios_set_irq_routing(struct pci_dev
*dev
, int pin
, int irq
)
487 __asm__("lcall *(%%esi); cld\n\t"
492 : "0" (PCIBIOS_SET_PCI_HW_INT
),
493 "b" ((dev
->bus
->number
<< 8) | dev
->devfn
),
494 "c" ((irq
<< 8) | (pin
+ 10)),
495 "S" (&pci_indirect
));
496 return !(ret
& 0xff00);
498 EXPORT_SYMBOL(pcibios_set_irq_routing
);
500 void __init
pci_pcbios_init(void)
502 if ((pci_probe
& PCI_PROBE_BIOS
)
503 && ((raw_pci_ops
= pci_find_bios()))) {
504 pci_probe
|= PCI_BIOS_SORT
;
505 pci_bios_present
= 1;