2 * BIOS32 and PCI BIOS handling.
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/uaccess.h>
11 #include <asm/pci_x86.h>
12 #include <asm/e820/types.h>
13 #include <asm/pci-functions.h>
14 #include <asm/cacheflush.h>
16 /* BIOS32 signature: "_32_" */
17 #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
19 /* PCI signature: "PCI " */
20 #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
22 /* PCI service signature: "$PCI" */
23 #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
25 /* PCI BIOS hardware mechanism flags */
26 #define PCIBIOS_HW_TYPE1 0x01
27 #define PCIBIOS_HW_TYPE2 0x02
28 #define PCIBIOS_HW_TYPE1_SPEC 0x10
29 #define PCIBIOS_HW_TYPE2_SPEC 0x20
33 /* According to the BIOS specification at:
34 * http://members.datafast.net.au/dft0802/specs/bios21.pdf, we could
35 * restrict the x zone to some pages and make it ro. But this may be
36 * broken on some bios, complex to handle with static_protections.
37 * We could make the 0xe0000-0x100000 range rox, but this can break
40 * So we let's an rw and x hole when pcibios is used. This shouldn't
41 * happen for modern system with mmconfig, and if you don't want it
42 * you could disable pcibios...
44 static inline void set_bios_x(void)
47 set_memory_x(PAGE_OFFSET + BIOS_BEGIN, (BIOS_END - BIOS_BEGIN) >> PAGE_SHIFT);
48 if (__supported_pte_mask & _PAGE_NX)
49 printk(KERN_INFO "PCI : PCI BIOS area is rw and x. Use pci=nobios if you want it NX.\n");
53 * This is the standard structure used to identify the entry point
54 * to the BIOS32 Service Directory, as documented in
55 * Standard BIOS 32-bit Service Directory Proposal
56 * Revision 0.4 May 24, 1993
57 * Phoenix Technologies Ltd.
59 * and the PCI BIOS specification.
64 unsigned long signature; /* _32_ */
65 unsigned long entry; /* 32 bit physical address */
66 unsigned char revision; /* Revision level, 0 */
67 unsigned char length; /* Length in paragraphs should be 01 */
68 unsigned char checksum; /* All bytes must add up to zero */
69 unsigned char reserved[5]; /* Must be zero */
75 * Physical address of the service directory. I don't know if we're
76 * allowed to have more than one of these or not, so just in case
77 * we'll make pcibios_present() take a memory start parameter and store
82 unsigned long address;
83 unsigned short segment;
84 } bios32_indirect __initdata = { 0, __KERNEL_CS };
87 * Returns the entry point for the given service, NULL on error
90 static unsigned long __init bios32_service(unsigned long service)
92 unsigned char return_code; /* %al */
93 unsigned long address; /* %ebx */
94 unsigned long length; /* %ecx */
95 unsigned long entry; /* %edx */
98 local_irq_save(flags);
99 __asm__("lcall *(%%edi); cld"
100 : "=a" (return_code),
106 "D" (&bios32_indirect));
107 local_irq_restore(flags);
109 switch (return_code) {
111 return address + entry;
112 case 0x80: /* Not present */
113 printk(KERN_WARNING "bios32_service(0x%lx): not present\n", service);
115 default: /* Shouldn't happen */
116 printk(KERN_WARNING "bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
117 service, return_code);
123 unsigned long address;
124 unsigned short segment;
125 } pci_indirect __ro_after_init = {
127 .segment = __KERNEL_CS,
130 static int pci_bios_present __ro_after_init;
132 static int __init check_pcibios(void)
134 u32 signature, eax, ebx, ecx;
135 u8 status, major_ver, minor_ver, hw_mech;
136 unsigned long flags, pcibios_entry;
138 if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
139 pci_indirect.address = pcibios_entry + PAGE_OFFSET;
141 local_irq_save(flags);
143 "lcall *(%%edi); cld\n\t"
151 : "1" (PCIBIOS_PCI_BIOS_PRESENT),
154 local_irq_restore(flags);
156 status = (eax >> 8) & 0xff;
157 hw_mech = eax & 0xff;
158 major_ver = (ebx >> 8) & 0xff;
159 minor_ver = ebx & 0xff;
160 if (pcibios_last_bus < 0)
161 pcibios_last_bus = ecx & 0xff;
162 DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
163 status, hw_mech, major_ver, minor_ver, pcibios_last_bus);
164 if (status || signature != PCI_SIGNATURE) {
165 printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found\n",
169 printk(KERN_INFO "PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
170 major_ver, minor_ver, pcibios_entry, pcibios_last_bus);
171 #ifdef CONFIG_PCI_DIRECT
172 if (!(hw_mech & PCIBIOS_HW_TYPE1))
173 pci_probe &= ~PCI_PROBE_CONF1;
174 if (!(hw_mech & PCIBIOS_HW_TYPE2))
175 pci_probe &= ~PCI_PROBE_CONF2;
182 static int pci_bios_read(unsigned int seg, unsigned int bus,
183 unsigned int devfn, int reg, int len, u32 *value)
185 unsigned long result = 0;
187 unsigned long bx = (bus << 8) | devfn;
188 u16 number = 0, mask = 0;
191 if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
194 raw_spin_lock_irqsave(&pci_config_lock, flags);
198 number = PCIBIOS_READ_CONFIG_BYTE;
202 number = PCIBIOS_READ_CONFIG_WORD;
206 number = PCIBIOS_READ_CONFIG_DWORD;
210 __asm__("lcall *(%%esi); cld\n\t"
219 "S" (&pci_indirect));
221 * Zero-extend the result beyond 8 or 16 bits, do not trust the
222 * BIOS having done it:
227 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
229 return (int)((result & 0xff00) >> 8);
232 static int pci_bios_write(unsigned int seg, unsigned int bus,
233 unsigned int devfn, int reg, int len, u32 value)
235 unsigned long result = 0;
237 unsigned long bx = (bus << 8) | devfn;
241 if ((bus > 255) || (devfn > 255) || (reg > 255))
244 raw_spin_lock_irqsave(&pci_config_lock, flags);
248 number = PCIBIOS_WRITE_CONFIG_BYTE;
251 number = PCIBIOS_WRITE_CONFIG_WORD;
254 number = PCIBIOS_WRITE_CONFIG_DWORD;
258 __asm__("lcall *(%%esi); cld\n\t"
267 "S" (&pci_indirect));
269 raw_spin_unlock_irqrestore(&pci_config_lock, flags);
271 return (int)((result & 0xff00) >> 8);
276 * Function table for BIOS32 access
279 static const struct pci_raw_ops pci_bios_access = {
280 .read = pci_bios_read,
281 .write = pci_bios_write
285 * Try to find PCI BIOS.
288 static const struct pci_raw_ops *__init pci_find_bios(void)
295 * Follow the standard procedure for locating the BIOS32 Service
296 * directory by scanning the permissible address range from
297 * 0xe0000 through 0xfffff for a valid BIOS32 structure.
300 for (check = (union bios32 *) __va(0xe0000);
301 check <= (union bios32 *) __va(0xffff0);
304 if (probe_kernel_address(&check->fields.signature, sig))
307 if (check->fields.signature != BIOS32_SIGNATURE)
309 length = check->fields.length * 16;
313 for (i = 0; i < length ; ++i)
314 sum += check->chars[i];
317 if (check->fields.revision != 0) {
318 printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
319 check->fields.revision, check);
322 DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
323 if (check->fields.entry >= 0x100000) {
324 printk("PCI: BIOS32 entry (0x%p) in high memory, "
325 "cannot use.\n", check);
328 unsigned long bios32_entry = check->fields.entry;
329 DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n",
331 bios32_indirect.address = bios32_entry + PAGE_OFFSET;
334 return &pci_bios_access;
336 break; /* Hopefully more than one BIOS32 cannot happen... */
343 * BIOS Functions for IRQ Routing
346 struct irq_routing_options {
348 struct irq_info *table;
350 } __attribute__((packed));
352 struct irq_routing_table * pcibios_get_irq_routing_table(void)
354 struct irq_routing_options opt;
355 struct irq_routing_table *rt = NULL;
359 if (!pci_bios_present)
361 page = __get_free_page(GFP_KERNEL);
364 opt.table = (struct irq_info *) page;
365 opt.size = PAGE_SIZE;
366 opt.segment = __KERNEL_DS;
368 DBG("PCI: Fetching IRQ routing table... ");
369 __asm__("push %%es\n\t"
372 "lcall *(%%esi); cld\n\t"
380 : "0" (PCIBIOS_GET_ROUTING_OPTIONS),
386 DBG("OK ret=%d, size=%d, map=%x\n", ret, opt.size, map);
388 printk(KERN_ERR "PCI: Error %02x when fetching IRQ routing table.\n", (ret >> 8) & 0xff);
390 rt = kmalloc(sizeof(struct irq_routing_table) + opt.size, GFP_KERNEL);
392 memset(rt, 0, sizeof(struct irq_routing_table));
393 rt->size = opt.size + sizeof(struct irq_routing_table);
394 rt->exclusive_irqs = map;
395 memcpy(rt->slots, (void *) page, opt.size);
396 printk(KERN_INFO "PCI: Using BIOS Interrupt Routing Table\n");
402 EXPORT_SYMBOL(pcibios_get_irq_routing_table);
404 int pcibios_set_irq_routing(struct pci_dev *dev, int pin, int irq)
408 __asm__("lcall *(%%esi); cld\n\t"
413 : "0" (PCIBIOS_SET_PCI_HW_INT),
414 "b" ((dev->bus->number << 8) | dev->devfn),
415 "c" ((irq << 8) | (pin + 10)),
416 "S" (&pci_indirect));
417 return !(ret & 0xff00);
419 EXPORT_SYMBOL(pcibios_set_irq_routing);
421 void __init pci_pcbios_init(void)
423 if ((pci_probe & PCI_PROBE_BIOS)
424 && ((raw_pci_ops = pci_find_bios()))) {
425 pci_bios_present = 1;