2 * BIOS32 and PCI BIOS handling.
6 #include <linux/init.h>
8 #include "pci-functions.h"
11 /* BIOS32 signature: "_32_" */
12 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
14 /* PCI signature: "PCI " */
15 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
17 /* PCI service signature: "$PCI" */
18 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
20 /* PCI BIOS hardware mechanism flags */
21 #define PCIBIOS_HW_TYPE1 0x01
22 #define PCIBIOS_HW_TYPE2 0x02
23 #define PCIBIOS_HW_TYPE1_SPEC 0x10
24 #define PCIBIOS_HW_TYPE2_SPEC 0x20
27 * This is the standard structure used to identify the entry point
28 * to the BIOS32 Service Directory, as documented in
29 * Standard BIOS 32-bit Service Directory Proposal
30 * Revision 0.4 May 24, 1993
31 * Phoenix Technologies Ltd.
33 * and the PCI BIOS specification.
38 unsigned long signature
; /* _32_ */
39 unsigned long entry
; /* 32 bit physical address */
40 unsigned char revision
; /* Revision level, 0 */
41 unsigned char length
; /* Length in paragraphs should be 01 */
42 unsigned char checksum
; /* All bytes must add up to zero */
43 unsigned char reserved
[5]; /* Must be zero */
49 * Physical address of the service directory. I don't know if we're
50 * allowed to have more than one of these or not, so just in case
51 * we'll make pcibios_present() take a memory start parameter and store
56 unsigned long address
;
57 unsigned short segment
;
58 } bios32_indirect
= { 0, __KERNEL_CS
};
61 * Returns the entry point for the given service, NULL on error
64 static unsigned long bios32_service(unsigned long service
)
66 unsigned char return_code
; /* %al */
67 unsigned long address
; /* %ebx */
68 unsigned long length
; /* %ecx */
69 unsigned long entry
; /* %edx */
72 local_irq_save(flags
);
73 __asm__("lcall *(%%edi); cld"
80 "D" (&bios32_indirect
));
81 local_irq_restore(flags
);
83 switch (return_code
) {
85 return address
+ entry
;
86 case 0x80: /* Not present */
87 printk(KERN_WARNING
"bios32_service(0x%lx): not present\n", service
);
89 default: /* Shouldn't happen */
90 printk(KERN_WARNING
"bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
91 service
, return_code
);
97 unsigned long address
;
98 unsigned short segment
;
99 } pci_indirect
= { 0, __KERNEL_CS
};
101 static int pci_bios_present
;
103 static int __devinit
check_pcibios(void)
105 u32 signature
, eax
, ebx
, ecx
;
106 u8 status
, major_ver
, minor_ver
, hw_mech
;
107 unsigned long flags
, pcibios_entry
;
109 if ((pcibios_entry
= bios32_service(PCI_SERVICE
))) {
110 pci_indirect
.address
= pcibios_entry
+ PAGE_OFFSET
;
112 local_irq_save(flags
);
114 "lcall *(%%edi); cld\n\t"
122 : "1" (PCIBIOS_PCI_BIOS_PRESENT
),
125 local_irq_restore(flags
);
127 status
= (eax
>> 8) & 0xff;
128 hw_mech
= eax
& 0xff;
129 major_ver
= (ebx
>> 8) & 0xff;
130 minor_ver
= ebx
& 0xff;
131 if (pcibios_last_bus
< 0)
132 pcibios_last_bus
= ecx
& 0xff;
133 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
134 status
, hw_mech
, major_ver
, minor_ver
, pcibios_last_bus
);
135 if (status
|| signature
!= PCI_SIGNATURE
) {
136 printk (KERN_ERR
"PCI: BIOS BUG #%x[%08x] found\n",
140 printk(KERN_INFO
"PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
141 major_ver
, minor_ver
, pcibios_entry
, pcibios_last_bus
);
142 #ifdef CONFIG_PCI_DIRECT
143 if (!(hw_mech
& PCIBIOS_HW_TYPE1
))
144 pci_probe
&= ~PCI_PROBE_CONF1
;
145 if (!(hw_mech
& PCIBIOS_HW_TYPE2
))
146 pci_probe
&= ~PCI_PROBE_CONF2
;
153 static int __devinit
pci_bios_find_device (unsigned short vendor
, unsigned short device_id
,
154 unsigned short index
, unsigned char *bus
, unsigned char *device_fn
)
159 __asm__("lcall *(%%edi); cld\n\t"
165 : "1" (PCIBIOS_FIND_PCI_DEVICE
),
169 "D" (&pci_indirect
));
170 *bus
= (bx
>> 8) & 0xff;
171 *device_fn
= bx
& 0xff;
172 return (int) (ret
& 0xff00) >> 8;
175 static int pci_bios_read(unsigned int seg
, unsigned int bus
,
176 unsigned int devfn
, int reg
, int len
, u32
*value
)
178 unsigned long result
= 0;
180 unsigned long bx
= (bus
<< 8) | devfn
;
182 if (!value
|| (bus
> 255) || (devfn
> 255) || (reg
> 255))
185 spin_lock_irqsave(&pci_config_lock
, flags
);
189 __asm__("lcall *(%%esi); cld\n\t"
195 : "1" (PCIBIOS_READ_CONFIG_BYTE
),
198 "S" (&pci_indirect
));
201 __asm__("lcall *(%%esi); cld\n\t"
207 : "1" (PCIBIOS_READ_CONFIG_WORD
),
210 "S" (&pci_indirect
));
213 __asm__("lcall *(%%esi); cld\n\t"
219 : "1" (PCIBIOS_READ_CONFIG_DWORD
),
222 "S" (&pci_indirect
));
226 spin_unlock_irqrestore(&pci_config_lock
, flags
);
228 return (int)((result
& 0xff00) >> 8);
231 static int pci_bios_write(unsigned int seg
, unsigned int bus
,
232 unsigned int devfn
, int reg
, int len
, u32 value
)
234 unsigned long result
= 0;
236 unsigned long bx
= (bus
<< 8) | devfn
;
238 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
241 spin_lock_irqsave(&pci_config_lock
, flags
);
245 __asm__("lcall *(%%esi); cld\n\t"
250 : "0" (PCIBIOS_WRITE_CONFIG_BYTE
),
254 "S" (&pci_indirect
));
257 __asm__("lcall *(%%esi); cld\n\t"
262 : "0" (PCIBIOS_WRITE_CONFIG_WORD
),
266 "S" (&pci_indirect
));
269 __asm__("lcall *(%%esi); cld\n\t"
274 : "0" (PCIBIOS_WRITE_CONFIG_DWORD
),
278 "S" (&pci_indirect
));
282 spin_unlock_irqrestore(&pci_config_lock
, flags
);
284 return (int)((result
& 0xff00) >> 8);
289 * Function table for BIOS32 access
292 static struct pci_raw_ops pci_bios_access
= {
293 .read
= pci_bios_read
,
294 .write
= pci_bios_write
298 * Try to find PCI BIOS.
301 static struct pci_raw_ops
* __devinit
pci_find_bios(void)
308 * Follow the standard procedure for locating the BIOS32 Service
309 * directory by scanning the permissible address range from
310 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
313 for (check
= (union bios32
*) __va(0xe0000);
314 check
<= (union bios32
*) __va(0xffff0);
316 if (check
->fields
.signature
!= BIOS32_SIGNATURE
)
318 length
= check
->fields
.length
* 16;
322 for (i
= 0; i
< length
; ++i
)
323 sum
+= check
->chars
[i
];
326 if (check
->fields
.revision
!= 0) {
327 printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
328 check
->fields
.revision
, check
);
331 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check
);
332 if (check
->fields
.entry
>= 0x100000) {
333 printk("PCI: BIOS32 entry (0x%p) in high memory, cannot use.\n", check
);
336 unsigned long bios32_entry
= check
->fields
.entry
;
337 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n", bios32_entry
);
338 bios32_indirect
.address
= bios32_entry
+ PAGE_OFFSET
;
340 return &pci_bios_access
;
342 break; /* Hopefully more than one BIOS32 cannot happen... */
349 * Sort the device list according to PCI BIOS. Nasty hack, but since some
350 * fool forgot to define the `correct' device order in the PCI BIOS specs
351 * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
352 * which used BIOS ordering, we are bound to do this...
355 void __devinit
pcibios_sort(void)
357 LIST_HEAD(sorted_devices
);
358 struct list_head
*ln
;
359 struct pci_dev
*dev
, *d
;
361 unsigned char bus
, devfn
;
363 DBG("PCI: Sorting device list...\n");
364 while (!list_empty(&pci_devices
)) {
365 ln
= pci_devices
.next
;
368 while (pci_bios_find_device(dev
->vendor
, dev
->device
, idx
, &bus
, &devfn
) == PCIBIOS_SUCCESSFUL
) {
370 list_for_each(ln
, &pci_devices
) {
372 if (d
->bus
->number
== bus
&& d
->devfn
== devfn
) {
373 list_del(&d
->global_list
);
374 list_add_tail(&d
->global_list
, &sorted_devices
);
380 if (ln
== &pci_devices
) {
381 printk(KERN_WARNING
"PCI: BIOS reporting unknown device %02x:%02x\n", bus
, devfn
);
383 * We must not continue scanning as several buggy BIOSes
384 * return garbage after the last device. Grr.
390 printk(KERN_WARNING
"PCI: Device %s not found by BIOS\n",
392 list_del(&dev
->global_list
);
393 list_add_tail(&dev
->global_list
, &sorted_devices
);
396 list_splice(&sorted_devices
, &pci_devices
);
400 * BIOS Functions for IRQ Routing
403 struct irq_routing_options
{
405 struct irq_info
*table
;
407 } __attribute__((packed
));
409 struct irq_routing_table
* __devinit
pcibios_get_irq_routing_table(void)
411 struct irq_routing_options opt
;
412 struct irq_routing_table
*rt
= NULL
;
416 if (!pci_bios_present
)
418 page
= __get_free_page(GFP_KERNEL
);
421 opt
.table
= (struct irq_info
*) page
;
422 opt
.size
= PAGE_SIZE
;
423 opt
.segment
= __KERNEL_DS
;
425 DBG("PCI: Fetching IRQ routing table... ");
426 __asm__("push %%es\n\t"
429 "lcall *(%%esi); cld\n\t"
437 : "0" (PCIBIOS_GET_ROUTING_OPTIONS
),
443 DBG("OK ret=%d, size=%d, map=%x\n", ret
, opt
.size
, map
);
445 printk(KERN_ERR
"PCI: Error %02x when fetching IRQ routing table.\n", (ret
>> 8) & 0xff);
447 rt
= kmalloc(sizeof(struct irq_routing_table
) + opt
.size
, GFP_KERNEL
);
449 memset(rt
, 0, sizeof(struct irq_routing_table
));
450 rt
->size
= opt
.size
+ sizeof(struct irq_routing_table
);
451 rt
->exclusive_irqs
= map
;
452 memcpy(rt
->slots
, (void *) page
, opt
.size
);
453 printk(KERN_INFO
"PCI: Using BIOS Interrupt Routing Table\n");
461 int pcibios_set_irq_routing(struct pci_dev
*dev
, int pin
, int irq
)
465 __asm__("lcall *(%%esi); cld\n\t"
470 : "0" (PCIBIOS_SET_PCI_HW_INT
),
471 "b" ((dev
->bus
->number
<< 8) | dev
->devfn
),
472 "c" ((irq
<< 8) | (pin
+ 10)),
473 "S" (&pci_indirect
));
474 return !(ret
& 0xff00);
477 static int __init
pci_pcbios_init(void)
479 if ((pci_probe
& PCI_PROBE_BIOS
)
480 && ((raw_pci_ops
= pci_find_bios()))) {
481 pci_probe
|= PCI_BIOS_SORT
;
482 pci_bios_present
= 1;
487 arch_initcall(pci_pcbios_init
);