1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fb9aa6f1SThomas Gleixner /*
3fb9aa6f1SThomas Gleixner * BIOS32 and PCI BIOS handling.
4fb9aa6f1SThomas Gleixner */
5fb9aa6f1SThomas Gleixner
6fb9aa6f1SThomas Gleixner #include <linux/pci.h>
7fb9aa6f1SThomas Gleixner #include <linux/init.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
9fb9aa6f1SThomas Gleixner #include <linux/module.h>
10fb9aa6f1SThomas Gleixner #include <linux/uaccess.h>
115520b7e7SIngo Molnar
1282487711SJaswinder Singh Rajput #include <asm/pci_x86.h>
135520b7e7SIngo Molnar #include <asm/e820/types.h>
141164dd00SIngo Molnar #include <asm/pci-functions.h>
15d1163651SLaura Abbott #include <asm/set_memory.h>
16fb9aa6f1SThomas Gleixner
17fb9aa6f1SThomas Gleixner /* BIOS32 signature: "_32_" */
18fb9aa6f1SThomas Gleixner #define BIOS32_SIGNATURE (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
19fb9aa6f1SThomas Gleixner
20fb9aa6f1SThomas Gleixner /* PCI signature: "PCI " */
21fb9aa6f1SThomas Gleixner #define PCI_SIGNATURE (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
22fb9aa6f1SThomas Gleixner
23fb9aa6f1SThomas Gleixner /* PCI service signature: "$PCI" */
24fb9aa6f1SThomas Gleixner #define PCI_SERVICE (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
25fb9aa6f1SThomas Gleixner
26fb9aa6f1SThomas Gleixner /* PCI BIOS hardware mechanism flags */
27fb9aa6f1SThomas Gleixner #define PCIBIOS_HW_TYPE1 0x01
28fb9aa6f1SThomas Gleixner #define PCIBIOS_HW_TYPE2 0x02
29fb9aa6f1SThomas Gleixner #define PCIBIOS_HW_TYPE1_SPEC 0x10
30fb9aa6f1SThomas Gleixner #define PCIBIOS_HW_TYPE2_SPEC 0x20
31fb9aa6f1SThomas Gleixner
325bd5a452SMatthieu Castet int pcibios_enabled;
335bd5a452SMatthieu Castet
345bd5a452SMatthieu Castet /* According to the BIOS specification at:
355bd5a452SMatthieu Castet * http://members.datafast.net.au/dft0802/specs/bios21.pdf, we could
365bd5a452SMatthieu Castet * restrict the x zone to some pages and make it ro. But this may be
375bd5a452SMatthieu Castet * broken on some bios, complex to handle with static_protections.
385bd5a452SMatthieu Castet * We could make the 0xe0000-0x100000 range rox, but this can break
395bd5a452SMatthieu Castet * some ISA mapping.
405bd5a452SMatthieu Castet *
415bd5a452SMatthieu Castet * So we let's an rw and x hole when pcibios is used. This shouldn't
425bd5a452SMatthieu Castet * happen for modern system with mmconfig, and if you don't want it
435bd5a452SMatthieu Castet * you could disable pcibios...
445bd5a452SMatthieu Castet */
set_bios_x(void)455bd5a452SMatthieu Castet static inline void set_bios_x(void)
465bd5a452SMatthieu Castet {
475bd5a452SMatthieu Castet pcibios_enabled = 1;
485bd5a452SMatthieu Castet set_memory_x(PAGE_OFFSET + BIOS_BEGIN, (BIOS_END - BIOS_BEGIN) >> PAGE_SHIFT);
495bd5a452SMatthieu Castet if (__supported_pte_mask & _PAGE_NX)
50819a693bSWang YanQing printk(KERN_INFO "PCI: PCI BIOS area is rw and x. Use pci=nobios if you want it NX.\n");
515bd5a452SMatthieu Castet }
525bd5a452SMatthieu Castet
53fb9aa6f1SThomas Gleixner /*
54fb9aa6f1SThomas Gleixner * This is the standard structure used to identify the entry point
55fb9aa6f1SThomas Gleixner * to the BIOS32 Service Directory, as documented in
56fb9aa6f1SThomas Gleixner * Standard BIOS 32-bit Service Directory Proposal
57fb9aa6f1SThomas Gleixner * Revision 0.4 May 24, 1993
58fb9aa6f1SThomas Gleixner * Phoenix Technologies Ltd.
59fb9aa6f1SThomas Gleixner * Norwood, MA
60fb9aa6f1SThomas Gleixner * and the PCI BIOS specification.
61fb9aa6f1SThomas Gleixner */
62fb9aa6f1SThomas Gleixner
63fb9aa6f1SThomas Gleixner union bios32 {
64fb9aa6f1SThomas Gleixner struct {
65fb9aa6f1SThomas Gleixner unsigned long signature; /* _32_ */
66fb9aa6f1SThomas Gleixner unsigned long entry; /* 32 bit physical address */
67fb9aa6f1SThomas Gleixner unsigned char revision; /* Revision level, 0 */
68fb9aa6f1SThomas Gleixner unsigned char length; /* Length in paragraphs should be 01 */
69fb9aa6f1SThomas Gleixner unsigned char checksum; /* All bytes must add up to zero */
70fb9aa6f1SThomas Gleixner unsigned char reserved[5]; /* Must be zero */
71fb9aa6f1SThomas Gleixner } fields;
72fb9aa6f1SThomas Gleixner char chars[16];
73fb9aa6f1SThomas Gleixner };
74fb9aa6f1SThomas Gleixner
75fb9aa6f1SThomas Gleixner /*
76fb9aa6f1SThomas Gleixner * Physical address of the service directory. I don't know if we're
77fb9aa6f1SThomas Gleixner * allowed to have more than one of these or not, so just in case
78fb9aa6f1SThomas Gleixner * we'll make pcibios_present() take a memory start parameter and store
79fb9aa6f1SThomas Gleixner * the array there.
80fb9aa6f1SThomas Gleixner */
81fb9aa6f1SThomas Gleixner
82fb9aa6f1SThomas Gleixner static struct {
83fb9aa6f1SThomas Gleixner unsigned long address;
84fb9aa6f1SThomas Gleixner unsigned short segment;
85615f7751SMathias Krause } bios32_indirect __initdata = { 0, __KERNEL_CS };
86fb9aa6f1SThomas Gleixner
87fb9aa6f1SThomas Gleixner /*
88fb9aa6f1SThomas Gleixner * Returns the entry point for the given service, NULL on error
89fb9aa6f1SThomas Gleixner */
90fb9aa6f1SThomas Gleixner
bios32_service(unsigned long service)91615f7751SMathias Krause static unsigned long __init bios32_service(unsigned long service)
92fb9aa6f1SThomas Gleixner {
93fb9aa6f1SThomas Gleixner unsigned char return_code; /* %al */
94fb9aa6f1SThomas Gleixner unsigned long address; /* %ebx */
95fb9aa6f1SThomas Gleixner unsigned long length; /* %ecx */
96fb9aa6f1SThomas Gleixner unsigned long entry; /* %edx */
97fb9aa6f1SThomas Gleixner unsigned long flags;
98fb9aa6f1SThomas Gleixner
99fb9aa6f1SThomas Gleixner local_irq_save(flags);
100fb9aa6f1SThomas Gleixner __asm__("lcall *(%%edi); cld"
101fb9aa6f1SThomas Gleixner : "=a" (return_code),
102fb9aa6f1SThomas Gleixner "=b" (address),
103fb9aa6f1SThomas Gleixner "=c" (length),
104fb9aa6f1SThomas Gleixner "=d" (entry)
105fb9aa6f1SThomas Gleixner : "0" (service),
106fb9aa6f1SThomas Gleixner "1" (0),
107fb9aa6f1SThomas Gleixner "D" (&bios32_indirect));
108fb9aa6f1SThomas Gleixner local_irq_restore(flags);
109fb9aa6f1SThomas Gleixner
110fb9aa6f1SThomas Gleixner switch (return_code) {
111fb9aa6f1SThomas Gleixner case 0:
112fb9aa6f1SThomas Gleixner return address + entry;
113fb9aa6f1SThomas Gleixner case 0x80: /* Not present */
114fb9aa6f1SThomas Gleixner printk(KERN_WARNING "bios32_service(0x%lx): not present\n", service);
115fb9aa6f1SThomas Gleixner return 0;
116fb9aa6f1SThomas Gleixner default: /* Shouldn't happen */
117fb9aa6f1SThomas Gleixner printk(KERN_WARNING "bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
118fb9aa6f1SThomas Gleixner service, return_code);
119fb9aa6f1SThomas Gleixner return 0;
120fb9aa6f1SThomas Gleixner }
121fb9aa6f1SThomas Gleixner }
122fb9aa6f1SThomas Gleixner
123fb9aa6f1SThomas Gleixner static struct {
124fb9aa6f1SThomas Gleixner unsigned long address;
125fb9aa6f1SThomas Gleixner unsigned short segment;
126404f6aacSKees Cook } pci_indirect __ro_after_init = {
127404f6aacSKees Cook .address = 0,
128404f6aacSKees Cook .segment = __KERNEL_CS,
129404f6aacSKees Cook };
130fb9aa6f1SThomas Gleixner
131404f6aacSKees Cook static int pci_bios_present __ro_after_init;
132fb9aa6f1SThomas Gleixner
check_pcibios(void)133615f7751SMathias Krause static int __init check_pcibios(void)
134fb9aa6f1SThomas Gleixner {
135fb9aa6f1SThomas Gleixner u32 signature, eax, ebx, ecx;
136fb9aa6f1SThomas Gleixner u8 status, major_ver, minor_ver, hw_mech;
137fb9aa6f1SThomas Gleixner unsigned long flags, pcibios_entry;
138fb9aa6f1SThomas Gleixner
139fb9aa6f1SThomas Gleixner if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
140fb9aa6f1SThomas Gleixner pci_indirect.address = pcibios_entry + PAGE_OFFSET;
141fb9aa6f1SThomas Gleixner
142fb9aa6f1SThomas Gleixner local_irq_save(flags);
143fb9aa6f1SThomas Gleixner __asm__(
144fb9aa6f1SThomas Gleixner "lcall *(%%edi); cld\n\t"
145fb9aa6f1SThomas Gleixner "jc 1f\n\t"
146fb9aa6f1SThomas Gleixner "xor %%ah, %%ah\n"
147fb9aa6f1SThomas Gleixner "1:"
148fb9aa6f1SThomas Gleixner : "=d" (signature),
149fb9aa6f1SThomas Gleixner "=a" (eax),
150fb9aa6f1SThomas Gleixner "=b" (ebx),
151fb9aa6f1SThomas Gleixner "=c" (ecx)
152fb9aa6f1SThomas Gleixner : "1" (PCIBIOS_PCI_BIOS_PRESENT),
153fb9aa6f1SThomas Gleixner "D" (&pci_indirect)
154fb9aa6f1SThomas Gleixner : "memory");
155fb9aa6f1SThomas Gleixner local_irq_restore(flags);
156fb9aa6f1SThomas Gleixner
157fb9aa6f1SThomas Gleixner status = (eax >> 8) & 0xff;
158fb9aa6f1SThomas Gleixner hw_mech = eax & 0xff;
159fb9aa6f1SThomas Gleixner major_ver = (ebx >> 8) & 0xff;
160fb9aa6f1SThomas Gleixner minor_ver = ebx & 0xff;
161fb9aa6f1SThomas Gleixner if (pcibios_last_bus < 0)
162fb9aa6f1SThomas Gleixner pcibios_last_bus = ecx & 0xff;
163fb9aa6f1SThomas Gleixner DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
164fb9aa6f1SThomas Gleixner status, hw_mech, major_ver, minor_ver, pcibios_last_bus);
165fb9aa6f1SThomas Gleixner if (status || signature != PCI_SIGNATURE) {
166fb9aa6f1SThomas Gleixner printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found\n",
167fb9aa6f1SThomas Gleixner status, signature);
168fb9aa6f1SThomas Gleixner return 0;
169fb9aa6f1SThomas Gleixner }
170fb9aa6f1SThomas Gleixner printk(KERN_INFO "PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
171fb9aa6f1SThomas Gleixner major_ver, minor_ver, pcibios_entry, pcibios_last_bus);
172fb9aa6f1SThomas Gleixner #ifdef CONFIG_PCI_DIRECT
173fb9aa6f1SThomas Gleixner if (!(hw_mech & PCIBIOS_HW_TYPE1))
174fb9aa6f1SThomas Gleixner pci_probe &= ~PCI_PROBE_CONF1;
175fb9aa6f1SThomas Gleixner if (!(hw_mech & PCIBIOS_HW_TYPE2))
176fb9aa6f1SThomas Gleixner pci_probe &= ~PCI_PROBE_CONF2;
177fb9aa6f1SThomas Gleixner #endif
178fb9aa6f1SThomas Gleixner return 1;
179fb9aa6f1SThomas Gleixner }
180fb9aa6f1SThomas Gleixner return 0;
181fb9aa6f1SThomas Gleixner }
182fb9aa6f1SThomas Gleixner
pci_bios_read(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,u32 * value)183fb9aa6f1SThomas Gleixner static int pci_bios_read(unsigned int seg, unsigned int bus,
184fb9aa6f1SThomas Gleixner unsigned int devfn, int reg, int len, u32 *value)
185fb9aa6f1SThomas Gleixner {
186fb9aa6f1SThomas Gleixner unsigned long result = 0;
187fb9aa6f1SThomas Gleixner unsigned long flags;
188fb9aa6f1SThomas Gleixner unsigned long bx = (bus << 8) | devfn;
18996ae6469SGeliang Tang u16 number = 0, mask = 0;
190fb9aa6f1SThomas Gleixner
191db34a363SJan Beulich WARN_ON(seg);
192fb9aa6f1SThomas Gleixner if (!value || (bus > 255) || (devfn > 255) || (reg > 255))
193fb9aa6f1SThomas Gleixner return -EINVAL;
194fb9aa6f1SThomas Gleixner
195d19f61f0SThomas Gleixner raw_spin_lock_irqsave(&pci_config_lock, flags);
196fb9aa6f1SThomas Gleixner
197fb9aa6f1SThomas Gleixner switch (len) {
198fb9aa6f1SThomas Gleixner case 1:
19996ae6469SGeliang Tang number = PCIBIOS_READ_CONFIG_BYTE;
20096ae6469SGeliang Tang mask = 0xff;
201fb9aa6f1SThomas Gleixner break;
202fb9aa6f1SThomas Gleixner case 2:
20396ae6469SGeliang Tang number = PCIBIOS_READ_CONFIG_WORD;
20496ae6469SGeliang Tang mask = 0xffff;
20596ae6469SGeliang Tang break;
20696ae6469SGeliang Tang case 4:
20796ae6469SGeliang Tang number = PCIBIOS_READ_CONFIG_DWORD;
20896ae6469SGeliang Tang break;
20996ae6469SGeliang Tang }
21096ae6469SGeliang Tang
211fb9aa6f1SThomas Gleixner __asm__("lcall *(%%esi); cld\n\t"
212fb9aa6f1SThomas Gleixner "jc 1f\n\t"
213fb9aa6f1SThomas Gleixner "xor %%ah, %%ah\n"
214fb9aa6f1SThomas Gleixner "1:"
215fb9aa6f1SThomas Gleixner : "=c" (*value),
216fb9aa6f1SThomas Gleixner "=a" (result)
21796ae6469SGeliang Tang : "1" (number),
218fb9aa6f1SThomas Gleixner "b" (bx),
219fb9aa6f1SThomas Gleixner "D" ((long)reg),
220fb9aa6f1SThomas Gleixner "S" (&pci_indirect));
221f5dbb55bSIngo Molnar /*
22296ae6469SGeliang Tang * Zero-extend the result beyond 8 or 16 bits, do not trust the
223f5dbb55bSIngo Molnar * BIOS having done it:
224f5dbb55bSIngo Molnar */
22596ae6469SGeliang Tang if (mask)
22696ae6469SGeliang Tang *value &= mask;
227fb9aa6f1SThomas Gleixner
228d19f61f0SThomas Gleixner raw_spin_unlock_irqrestore(&pci_config_lock, flags);
229fb9aa6f1SThomas Gleixner
230fb9aa6f1SThomas Gleixner return (int)((result & 0xff00) >> 8);
231fb9aa6f1SThomas Gleixner }
232fb9aa6f1SThomas Gleixner
pci_bios_write(unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,u32 value)233fb9aa6f1SThomas Gleixner static int pci_bios_write(unsigned int seg, unsigned int bus,
234fb9aa6f1SThomas Gleixner unsigned int devfn, int reg, int len, u32 value)
235fb9aa6f1SThomas Gleixner {
236fb9aa6f1SThomas Gleixner unsigned long result = 0;
237fb9aa6f1SThomas Gleixner unsigned long flags;
238fb9aa6f1SThomas Gleixner unsigned long bx = (bus << 8) | devfn;
23996ae6469SGeliang Tang u16 number = 0;
240fb9aa6f1SThomas Gleixner
241db34a363SJan Beulich WARN_ON(seg);
242fb9aa6f1SThomas Gleixner if ((bus > 255) || (devfn > 255) || (reg > 255))
243fb9aa6f1SThomas Gleixner return -EINVAL;
244fb9aa6f1SThomas Gleixner
245d19f61f0SThomas Gleixner raw_spin_lock_irqsave(&pci_config_lock, flags);
246fb9aa6f1SThomas Gleixner
247fb9aa6f1SThomas Gleixner switch (len) {
248fb9aa6f1SThomas Gleixner case 1:
24996ae6469SGeliang Tang number = PCIBIOS_WRITE_CONFIG_BYTE;
250fb9aa6f1SThomas Gleixner break;
251fb9aa6f1SThomas Gleixner case 2:
25296ae6469SGeliang Tang number = PCIBIOS_WRITE_CONFIG_WORD;
253fb9aa6f1SThomas Gleixner break;
254fb9aa6f1SThomas Gleixner case 4:
25596ae6469SGeliang Tang number = PCIBIOS_WRITE_CONFIG_DWORD;
25696ae6469SGeliang Tang break;
25796ae6469SGeliang Tang }
25896ae6469SGeliang Tang
259fb9aa6f1SThomas Gleixner __asm__("lcall *(%%esi); cld\n\t"
260fb9aa6f1SThomas Gleixner "jc 1f\n\t"
261fb9aa6f1SThomas Gleixner "xor %%ah, %%ah\n"
262fb9aa6f1SThomas Gleixner "1:"
263fb9aa6f1SThomas Gleixner : "=a" (result)
26496ae6469SGeliang Tang : "0" (number),
265fb9aa6f1SThomas Gleixner "c" (value),
266fb9aa6f1SThomas Gleixner "b" (bx),
267fb9aa6f1SThomas Gleixner "D" ((long)reg),
268fb9aa6f1SThomas Gleixner "S" (&pci_indirect));
269fb9aa6f1SThomas Gleixner
270d19f61f0SThomas Gleixner raw_spin_unlock_irqrestore(&pci_config_lock, flags);
271fb9aa6f1SThomas Gleixner
272fb9aa6f1SThomas Gleixner return (int)((result & 0xff00) >> 8);
273fb9aa6f1SThomas Gleixner }
274fb9aa6f1SThomas Gleixner
275fb9aa6f1SThomas Gleixner
276fb9aa6f1SThomas Gleixner /*
277fb9aa6f1SThomas Gleixner * Function table for BIOS32 access
278fb9aa6f1SThomas Gleixner */
279fb9aa6f1SThomas Gleixner
28072da0b07SJan Beulich static const struct pci_raw_ops pci_bios_access = {
281fb9aa6f1SThomas Gleixner .read = pci_bios_read,
282fb9aa6f1SThomas Gleixner .write = pci_bios_write
283fb9aa6f1SThomas Gleixner };
284fb9aa6f1SThomas Gleixner
285fb9aa6f1SThomas Gleixner /*
286fb9aa6f1SThomas Gleixner * Try to find PCI BIOS.
287fb9aa6f1SThomas Gleixner */
288fb9aa6f1SThomas Gleixner
pci_find_bios(void)289615f7751SMathias Krause static const struct pci_raw_ops *__init pci_find_bios(void)
290fb9aa6f1SThomas Gleixner {
291fb9aa6f1SThomas Gleixner union bios32 *check;
292fb9aa6f1SThomas Gleixner unsigned char sum;
293fb9aa6f1SThomas Gleixner int i, length;
294fb9aa6f1SThomas Gleixner
295fb9aa6f1SThomas Gleixner /*
296fb9aa6f1SThomas Gleixner * Follow the standard procedure for locating the BIOS32 Service
297fb9aa6f1SThomas Gleixner * directory by scanning the permissible address range from
298fb9aa6f1SThomas Gleixner * 0xe0000 through 0xfffff for a valid BIOS32 structure.
299fb9aa6f1SThomas Gleixner */
300fb9aa6f1SThomas Gleixner
301fb9aa6f1SThomas Gleixner for (check = (union bios32 *) __va(0xe0000);
302fb9aa6f1SThomas Gleixner check <= (union bios32 *) __va(0xffff0);
303fb9aa6f1SThomas Gleixner ++check) {
304fb9aa6f1SThomas Gleixner long sig;
305*25f12ae4SChristoph Hellwig if (get_kernel_nofault(sig, &check->fields.signature))
306fb9aa6f1SThomas Gleixner continue;
307fb9aa6f1SThomas Gleixner
308fb9aa6f1SThomas Gleixner if (check->fields.signature != BIOS32_SIGNATURE)
309fb9aa6f1SThomas Gleixner continue;
310fb9aa6f1SThomas Gleixner length = check->fields.length * 16;
311fb9aa6f1SThomas Gleixner if (!length)
312fb9aa6f1SThomas Gleixner continue;
313fb9aa6f1SThomas Gleixner sum = 0;
314fb9aa6f1SThomas Gleixner for (i = 0; i < length ; ++i)
315fb9aa6f1SThomas Gleixner sum += check->chars[i];
316fb9aa6f1SThomas Gleixner if (sum != 0)
317fb9aa6f1SThomas Gleixner continue;
318fb9aa6f1SThomas Gleixner if (check->fields.revision != 0) {
319fb9aa6f1SThomas Gleixner printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
320fb9aa6f1SThomas Gleixner check->fields.revision, check);
321fb9aa6f1SThomas Gleixner continue;
322fb9aa6f1SThomas Gleixner }
323fb9aa6f1SThomas Gleixner DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
324fb9aa6f1SThomas Gleixner if (check->fields.entry >= 0x100000) {
325fb9aa6f1SThomas Gleixner printk("PCI: BIOS32 entry (0x%p) in high memory, "
326fb9aa6f1SThomas Gleixner "cannot use.\n", check);
327fb9aa6f1SThomas Gleixner return NULL;
328fb9aa6f1SThomas Gleixner } else {
329fb9aa6f1SThomas Gleixner unsigned long bios32_entry = check->fields.entry;
330fb9aa6f1SThomas Gleixner DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n",
331fb9aa6f1SThomas Gleixner bios32_entry);
332fb9aa6f1SThomas Gleixner bios32_indirect.address = bios32_entry + PAGE_OFFSET;
3335bd5a452SMatthieu Castet set_bios_x();
334fb9aa6f1SThomas Gleixner if (check_pcibios())
335fb9aa6f1SThomas Gleixner return &pci_bios_access;
336fb9aa6f1SThomas Gleixner }
337fb9aa6f1SThomas Gleixner break; /* Hopefully more than one BIOS32 cannot happen... */
338fb9aa6f1SThomas Gleixner }
339fb9aa6f1SThomas Gleixner
340fb9aa6f1SThomas Gleixner return NULL;
341fb9aa6f1SThomas Gleixner }
342fb9aa6f1SThomas Gleixner
343fb9aa6f1SThomas Gleixner /*
344fb9aa6f1SThomas Gleixner * BIOS Functions for IRQ Routing
345fb9aa6f1SThomas Gleixner */
346fb9aa6f1SThomas Gleixner
347fb9aa6f1SThomas Gleixner struct irq_routing_options {
348fb9aa6f1SThomas Gleixner u16 size;
349fb9aa6f1SThomas Gleixner struct irq_info *table;
350fb9aa6f1SThomas Gleixner u16 segment;
351fb9aa6f1SThomas Gleixner } __attribute__((packed));
352fb9aa6f1SThomas Gleixner
pcibios_get_irq_routing_table(void)353fb9aa6f1SThomas Gleixner struct irq_routing_table * pcibios_get_irq_routing_table(void)
354fb9aa6f1SThomas Gleixner {
355fb9aa6f1SThomas Gleixner struct irq_routing_options opt;
356fb9aa6f1SThomas Gleixner struct irq_routing_table *rt = NULL;
357fb9aa6f1SThomas Gleixner int ret, map;
358fb9aa6f1SThomas Gleixner unsigned long page;
359fb9aa6f1SThomas Gleixner
360fb9aa6f1SThomas Gleixner if (!pci_bios_present)
361fb9aa6f1SThomas Gleixner return NULL;
362fb9aa6f1SThomas Gleixner page = __get_free_page(GFP_KERNEL);
363fb9aa6f1SThomas Gleixner if (!page)
364fb9aa6f1SThomas Gleixner return NULL;
365fb9aa6f1SThomas Gleixner opt.table = (struct irq_info *) page;
366fb9aa6f1SThomas Gleixner opt.size = PAGE_SIZE;
367fb9aa6f1SThomas Gleixner opt.segment = __KERNEL_DS;
368fb9aa6f1SThomas Gleixner
369fb9aa6f1SThomas Gleixner DBG("PCI: Fetching IRQ routing table... ");
370fb9aa6f1SThomas Gleixner __asm__("push %%es\n\t"
371fb9aa6f1SThomas Gleixner "push %%ds\n\t"
372fb9aa6f1SThomas Gleixner "pop %%es\n\t"
373fb9aa6f1SThomas Gleixner "lcall *(%%esi); cld\n\t"
374fb9aa6f1SThomas Gleixner "pop %%es\n\t"
375fb9aa6f1SThomas Gleixner "jc 1f\n\t"
376fb9aa6f1SThomas Gleixner "xor %%ah, %%ah\n"
377fb9aa6f1SThomas Gleixner "1:"
378fb9aa6f1SThomas Gleixner : "=a" (ret),
379fb9aa6f1SThomas Gleixner "=b" (map),
380fb9aa6f1SThomas Gleixner "=m" (opt)
381fb9aa6f1SThomas Gleixner : "0" (PCIBIOS_GET_ROUTING_OPTIONS),
382fb9aa6f1SThomas Gleixner "1" (0),
383fb9aa6f1SThomas Gleixner "D" ((long) &opt),
384fb9aa6f1SThomas Gleixner "S" (&pci_indirect),
385fb9aa6f1SThomas Gleixner "m" (opt)
386fb9aa6f1SThomas Gleixner : "memory");
387fb9aa6f1SThomas Gleixner DBG("OK ret=%d, size=%d, map=%x\n", ret, opt.size, map);
388fb9aa6f1SThomas Gleixner if (ret & 0xff00)
389fb9aa6f1SThomas Gleixner printk(KERN_ERR "PCI: Error %02x when fetching IRQ routing table.\n", (ret >> 8) & 0xff);
390fb9aa6f1SThomas Gleixner else if (opt.size) {
391fb9aa6f1SThomas Gleixner rt = kmalloc(sizeof(struct irq_routing_table) + opt.size, GFP_KERNEL);
392fb9aa6f1SThomas Gleixner if (rt) {
393fb9aa6f1SThomas Gleixner memset(rt, 0, sizeof(struct irq_routing_table));
394fb9aa6f1SThomas Gleixner rt->size = opt.size + sizeof(struct irq_routing_table);
395fb9aa6f1SThomas Gleixner rt->exclusive_irqs = map;
396fb9aa6f1SThomas Gleixner memcpy(rt->slots, (void *) page, opt.size);
397fb9aa6f1SThomas Gleixner printk(KERN_INFO "PCI: Using BIOS Interrupt Routing Table\n");
398fb9aa6f1SThomas Gleixner }
399fb9aa6f1SThomas Gleixner }
400fb9aa6f1SThomas Gleixner free_page(page);
401fb9aa6f1SThomas Gleixner return rt;
402fb9aa6f1SThomas Gleixner }
403fb9aa6f1SThomas Gleixner EXPORT_SYMBOL(pcibios_get_irq_routing_table);
404fb9aa6f1SThomas Gleixner
pcibios_set_irq_routing(struct pci_dev * dev,int pin,int irq)405fb9aa6f1SThomas Gleixner int pcibios_set_irq_routing(struct pci_dev *dev, int pin, int irq)
406fb9aa6f1SThomas Gleixner {
407fb9aa6f1SThomas Gleixner int ret;
408fb9aa6f1SThomas Gleixner
409fb9aa6f1SThomas Gleixner __asm__("lcall *(%%esi); cld\n\t"
410fb9aa6f1SThomas Gleixner "jc 1f\n\t"
411fb9aa6f1SThomas Gleixner "xor %%ah, %%ah\n"
412fb9aa6f1SThomas Gleixner "1:"
413fb9aa6f1SThomas Gleixner : "=a" (ret)
414fb9aa6f1SThomas Gleixner : "0" (PCIBIOS_SET_PCI_HW_INT),
415fb9aa6f1SThomas Gleixner "b" ((dev->bus->number << 8) | dev->devfn),
416fb9aa6f1SThomas Gleixner "c" ((irq << 8) | (pin + 10)),
417fb9aa6f1SThomas Gleixner "S" (&pci_indirect));
418fb9aa6f1SThomas Gleixner return !(ret & 0xff00);
419fb9aa6f1SThomas Gleixner }
420fb9aa6f1SThomas Gleixner EXPORT_SYMBOL(pcibios_set_irq_routing);
421fb9aa6f1SThomas Gleixner
pci_pcbios_init(void)422fb9aa6f1SThomas Gleixner void __init pci_pcbios_init(void)
423fb9aa6f1SThomas Gleixner {
424fb9aa6f1SThomas Gleixner if ((pci_probe & PCI_PROBE_BIOS)
425fb9aa6f1SThomas Gleixner && ((raw_pci_ops = pci_find_bios()))) {
426fb9aa6f1SThomas Gleixner pci_bios_present = 1;
427fb9aa6f1SThomas Gleixner }
428fb9aa6f1SThomas Gleixner }
429fb9aa6f1SThomas Gleixner
430