1 // SPDX-License-Identifier: GPL-2.0
3 * BIOS32 and PCI BIOS handling.
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/uaccess.h>
12 #include <asm/pci_x86.h>
13 #include <asm/e820/types.h>
14 #include <asm/pci-functions.h>
15 #include <asm/set_memory.h>
17 /* BIOS32 signature: "_32_" */
18 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
20 /* PCI signature: "PCI " */
21 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
23 /* PCI service signature: "$PCI" */
24 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
26 /* PCI BIOS hardware mechanism flags */
27 #define PCIBIOS_HW_TYPE1 0x01
28 #define PCIBIOS_HW_TYPE2 0x02
29 #define PCIBIOS_HW_TYPE1_SPEC 0x10
30 #define PCIBIOS_HW_TYPE2_SPEC 0x20
34 /* According to the BIOS specification at:
35 * http://members.datafast.net.au/dft0802/specs/bios21.pdf, we could
36 * restrict the x zone to some pages and make it ro. But this may be
37 * broken on some bios, complex to handle with static_protections.
38 * We could make the 0xe0000-0x100000 range rox, but this can break
41 * So we let's an rw and x hole when pcibios is used. This shouldn't
42 * happen for modern system with mmconfig, and if you don't want it
43 * you could disable pcibios...
45 static inline void set_bios_x(void)
48 set_memory_x(PAGE_OFFSET
+ BIOS_BEGIN
, (BIOS_END
- BIOS_BEGIN
) >> PAGE_SHIFT
);
49 if (__supported_pte_mask
& _PAGE_NX
)
50 printk(KERN_INFO
"PCI: PCI BIOS area is rw and x. Use pci=nobios if you want it NX.\n");
54 * This is the standard structure used to identify the entry point
55 * to the BIOS32 Service Directory, as documented in
56 * Standard BIOS 32-bit Service Directory Proposal
57 * Revision 0.4 May 24, 1993
58 * Phoenix Technologies Ltd.
60 * and the PCI BIOS specification.
65 unsigned long signature
; /* _32_ */
66 unsigned long entry
; /* 32 bit physical address */
67 unsigned char revision
; /* Revision level, 0 */
68 unsigned char length
; /* Length in paragraphs should be 01 */
69 unsigned char checksum
; /* All bytes must add up to zero */
70 unsigned char reserved
[5]; /* Must be zero */
76 * Physical address of the service directory. I don't know if we're
77 * allowed to have more than one of these or not, so just in case
78 * we'll make pcibios_present() take a memory start parameter and store
83 unsigned long address
;
84 unsigned short segment
;
85 } bios32_indirect __initdata
= { 0, __KERNEL_CS
};
88 * Returns the entry point for the given service, NULL on error
91 static unsigned long __init
bios32_service(unsigned long service
)
93 unsigned char return_code
; /* %al */
94 unsigned long address
; /* %ebx */
95 unsigned long length
; /* %ecx */
96 unsigned long entry
; /* %edx */
99 local_irq_save(flags
);
100 __asm__("lcall *(%%edi); cld"
101 : "=a" (return_code
),
107 "D" (&bios32_indirect
));
108 local_irq_restore(flags
);
110 switch (return_code
) {
112 return address
+ entry
;
113 case 0x80: /* Not present */
114 printk(KERN_WARNING
"bios32_service(0x%lx): not present\n", service
);
116 default: /* Shouldn't happen */
117 printk(KERN_WARNING
"bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
118 service
, return_code
);
124 unsigned long address
;
125 unsigned short segment
;
126 } pci_indirect __ro_after_init
= {
128 .segment
= __KERNEL_CS
,
131 static int pci_bios_present __ro_after_init
;
133 static int __init
check_pcibios(void)
135 u32 signature
, eax
, ebx
, ecx
;
136 u8 status
, major_ver
, minor_ver
, hw_mech
;
137 unsigned long flags
, pcibios_entry
;
139 if ((pcibios_entry
= bios32_service(PCI_SERVICE
))) {
140 pci_indirect
.address
= pcibios_entry
+ PAGE_OFFSET
;
142 local_irq_save(flags
);
144 "lcall *(%%edi); cld\n\t"
152 : "1" (PCIBIOS_PCI_BIOS_PRESENT
),
155 local_irq_restore(flags
);
157 status
= (eax
>> 8) & 0xff;
158 hw_mech
= eax
& 0xff;
159 major_ver
= (ebx
>> 8) & 0xff;
160 minor_ver
= ebx
& 0xff;
161 if (pcibios_last_bus
< 0)
162 pcibios_last_bus
= ecx
& 0xff;
163 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
164 status
, hw_mech
, major_ver
, minor_ver
, pcibios_last_bus
);
165 if (status
|| signature
!= PCI_SIGNATURE
) {
166 printk (KERN_ERR
"PCI: BIOS BUG #%x[%08x] found\n",
170 printk(KERN_INFO
"PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
171 major_ver
, minor_ver
, pcibios_entry
, pcibios_last_bus
);
172 #ifdef CONFIG_PCI_DIRECT
173 if (!(hw_mech
& PCIBIOS_HW_TYPE1
))
174 pci_probe
&= ~PCI_PROBE_CONF1
;
175 if (!(hw_mech
& PCIBIOS_HW_TYPE2
))
176 pci_probe
&= ~PCI_PROBE_CONF2
;
183 static int pci_bios_read(unsigned int seg
, unsigned int bus
,
184 unsigned int devfn
, int reg
, int len
, u32
*value
)
186 unsigned long result
= 0;
188 unsigned long bx
= (bus
<< 8) | devfn
;
189 u16 number
= 0, mask
= 0;
192 if (!value
|| (bus
> 255) || (devfn
> 255) || (reg
> 255))
195 raw_spin_lock_irqsave(&pci_config_lock
, flags
);
199 number
= PCIBIOS_READ_CONFIG_BYTE
;
203 number
= PCIBIOS_READ_CONFIG_WORD
;
207 number
= PCIBIOS_READ_CONFIG_DWORD
;
211 __asm__("lcall *(%%esi); cld\n\t"
220 "S" (&pci_indirect
));
222 * Zero-extend the result beyond 8 or 16 bits, do not trust the
223 * BIOS having done it:
228 raw_spin_unlock_irqrestore(&pci_config_lock
, flags
);
230 return (int)((result
& 0xff00) >> 8);
233 static int pci_bios_write(unsigned int seg
, unsigned int bus
,
234 unsigned int devfn
, int reg
, int len
, u32 value
)
236 unsigned long result
= 0;
238 unsigned long bx
= (bus
<< 8) | devfn
;
242 if ((bus
> 255) || (devfn
> 255) || (reg
> 255))
245 raw_spin_lock_irqsave(&pci_config_lock
, flags
);
249 number
= PCIBIOS_WRITE_CONFIG_BYTE
;
252 number
= PCIBIOS_WRITE_CONFIG_WORD
;
255 number
= PCIBIOS_WRITE_CONFIG_DWORD
;
259 __asm__("lcall *(%%esi); cld\n\t"
268 "S" (&pci_indirect
));
270 raw_spin_unlock_irqrestore(&pci_config_lock
, flags
);
272 return (int)((result
& 0xff00) >> 8);
277 * Function table for BIOS32 access
280 static const struct pci_raw_ops pci_bios_access
= {
281 .read
= pci_bios_read
,
282 .write
= pci_bios_write
286 * Try to find PCI BIOS.
289 static const struct pci_raw_ops
*__init
pci_find_bios(void)
296 * Follow the standard procedure for locating the BIOS32 Service
297 * directory by scanning the permissible address range from
298 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
301 for (check
= (union bios32
*) __va(0xe0000);
302 check
<= (union bios32
*) __va(0xffff0);
305 if (get_kernel_nofault(sig
, &check
->fields
.signature
))
308 if (check
->fields
.signature
!= BIOS32_SIGNATURE
)
310 length
= check
->fields
.length
* 16;
314 for (i
= 0; i
< length
; ++i
)
315 sum
+= check
->chars
[i
];
318 if (check
->fields
.revision
!= 0) {
319 printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
320 check
->fields
.revision
, check
);
323 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check
);
324 if (check
->fields
.entry
>= 0x100000) {
325 printk("PCI: BIOS32 entry (0x%p) in high memory, "
326 "cannot use.\n", check
);
329 unsigned long bios32_entry
= check
->fields
.entry
;
330 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n",
332 bios32_indirect
.address
= bios32_entry
+ PAGE_OFFSET
;
335 return &pci_bios_access
;
337 break; /* Hopefully more than one BIOS32 cannot happen... */
344 * BIOS Functions for IRQ Routing
347 struct irq_routing_options
{
349 struct irq_info
*table
;
351 } __attribute__((packed
));
353 struct irq_routing_table
* pcibios_get_irq_routing_table(void)
355 struct irq_routing_options opt
;
356 struct irq_routing_table
*rt
= NULL
;
360 if (!pci_bios_present
)
362 page
= __get_free_page(GFP_KERNEL
);
365 opt
.table
= (struct irq_info
*) page
;
366 opt
.size
= PAGE_SIZE
;
367 opt
.segment
= __KERNEL_DS
;
369 DBG("PCI: Fetching IRQ routing table... ");
370 __asm__("push %%es\n\t"
373 "lcall *(%%esi); cld\n\t"
381 : "0" (PCIBIOS_GET_ROUTING_OPTIONS
),
387 DBG("OK ret=%d, size=%d, map=%x\n", ret
, opt
.size
, map
);
389 printk(KERN_ERR
"PCI: Error %02x when fetching IRQ routing table.\n", (ret
>> 8) & 0xff);
391 rt
= kmalloc(sizeof(struct irq_routing_table
) + opt
.size
, GFP_KERNEL
);
393 memset(rt
, 0, sizeof(struct irq_routing_table
));
394 rt
->size
= opt
.size
+ sizeof(struct irq_routing_table
);
395 rt
->exclusive_irqs
= map
;
396 memcpy(rt
->slots
, (void *) page
, opt
.size
);
397 printk(KERN_INFO
"PCI: Using BIOS Interrupt Routing Table\n");
403 EXPORT_SYMBOL(pcibios_get_irq_routing_table
);
405 int pcibios_set_irq_routing(struct pci_dev
*dev
, int pin
, int irq
)
409 __asm__("lcall *(%%esi); cld\n\t"
414 : "0" (PCIBIOS_SET_PCI_HW_INT
),
415 "b" ((dev
->bus
->number
<< 8) | dev
->devfn
),
416 "c" ((irq
<< 8) | (pin
+ 10)),
417 "S" (&pci_indirect
));
418 return !(ret
& 0xff00);
420 EXPORT_SYMBOL(pcibios_set_irq_routing
);
422 void __init
pci_pcbios_init(void)
424 if ((pci_probe
& PCI_PROBE_BIOS
)
425 && ((raw_pci_ops
= pci_find_bios()))) {
426 pci_bios_present
= 1;