2 * bioscalls.c - the lowlevel layer of the PnPBIOS driver
6 #include <linux/types.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/linkage.h>
10 #include <linux/kernel.h>
11 #include <linux/pnpbios.h>
12 #include <linux/device.h>
13 #include <linux/pnp.h>
15 #include <linux/smp.h>
16 #include <linux/slab.h>
17 #include <linux/kmod.h>
18 #include <linux/completion.h>
19 #include <linux/spinlock.h>
23 #include <asm/system.h>
24 #include <asm/byteorder.h>
34 /* The PnP BIOS entries in the GDT */
35 #define PNP_GDT (GDT_ENTRY_PNPBIOS_BASE * 8)
37 #define PNP_CS32 (PNP_GDT+0x00) /* segment for calling fn */
38 #define PNP_CS16 (PNP_GDT+0x08) /* code segment for BIOS */
39 #define PNP_DS (PNP_GDT+0x10) /* data segment for BIOS */
40 #define PNP_TS1 (PNP_GDT+0x18) /* transfer data segment */
41 #define PNP_TS2 (PNP_GDT+0x20) /* another data segment */
44 * These are some opcodes for a "static asmlinkage"
45 * As this code is *not* executed inside the linux kernel segment, but in a
46 * alias at offset 0, we need a far return that can not be compiled by
47 * default (please, prove me wrong! this is *really* ugly!)
48 * This is the only way to get the bios to return into the kernel code,
49 * because the bios code runs in 16 bit protected mode and therefore can only
50 * return to the caller if the call is within the first 64kB, and the linux
51 * kernel begins at offset 3GB...
54 asmlinkage
void pnp_bios_callfunc(void);
59 "pnp_bios_callfunc:\n"
64 " lcallw *pnp_bios_callpoint\n"
70 #define Q_SET_SEL(cpu, selname, address, size) \
72 set_base(per_cpu(cpu_gdt_table,cpu)[(selname) >> 3], __va((u32)(address))); \
73 set_limit(per_cpu(cpu_gdt_table,cpu)[(selname) >> 3], size); \
76 #define Q2_SET_SEL(cpu, selname, address, size) \
78 set_base(per_cpu(cpu_gdt_table,cpu)[(selname) >> 3], (u32)(address)); \
79 set_limit(per_cpu(cpu_gdt_table,cpu)[(selname) >> 3], size); \
82 static struct desc_struct bad_bios_desc
= { 0, 0x00409200 };
85 * At some point we want to use this stack frame pointer to unwind
86 * after PnP BIOS oopses.
89 u32 pnp_bios_fault_esp
;
90 u32 pnp_bios_fault_eip
;
91 u32 pnp_bios_is_utter_crap
= 0;
93 static spinlock_t pnp_bios_lock
;
100 static inline u16
call_pnp_bios(u16 func
, u16 arg1
, u16 arg2
, u16 arg3
,
101 u16 arg4
, u16 arg5
, u16 arg6
, u16 arg7
,
102 void *ts1_base
, u32 ts1_size
,
103 void *ts2_base
, u32 ts2_size
)
107 struct desc_struct save_desc_40
;
111 * PnP BIOSes are generally not terribly re-entrant.
112 * Also, don't rely on them to save everything correctly.
114 if(pnp_bios_is_utter_crap
)
115 return PNP_FUNCTION_NOT_SUPPORTED
;
118 save_desc_40
= per_cpu(cpu_gdt_table
,cpu
)[0x40 / 8];
119 per_cpu(cpu_gdt_table
,cpu
)[0x40 / 8] = bad_bios_desc
;
121 /* On some boxes IRQ's during PnP BIOS calls are deadly. */
122 spin_lock_irqsave(&pnp_bios_lock
, flags
);
124 /* The lock prevents us bouncing CPU here */
126 Q2_SET_SEL(smp_processor_id(), PNP_TS1
, ts1_base
, ts1_size
);
128 Q2_SET_SEL(smp_processor_id(), PNP_TS2
, ts2_base
, ts2_size
);
130 __asm__
__volatile__(
139 "movl %%esp, pnp_bios_fault_esp\n\t"
140 "movl $1f, pnp_bios_fault_eip\n\t"
151 : "0" ((func
) | (((u32
)arg1
) << 16)),
152 "b" ((arg2
) | (((u32
)arg3
) << 16)),
153 "c" ((arg4
) | (((u32
)arg5
) << 16)),
154 "d" ((arg6
) | (((u32
)arg7
) << 16)),
159 spin_unlock_irqrestore(&pnp_bios_lock
, flags
);
161 per_cpu(cpu_gdt_table
,cpu
)[0x40 / 8] = save_desc_40
;
164 /* If we get here and this is set then the PnP BIOS faulted on us. */
165 if(pnp_bios_is_utter_crap
)
167 printk(KERN_ERR
"PnPBIOS: Warning! Your PnP BIOS caused a fatal error. Attempting to continue\n");
168 printk(KERN_ERR
"PnPBIOS: You may need to reboot with the \"pnpbios=off\" option to operate stably\n");
169 printk(KERN_ERR
"PnPBIOS: Check with your vendor for an updated BIOS\n");
175 void pnpbios_print_status(const char * module
, u16 status
)
179 printk(KERN_ERR
"PnPBIOS: %s: function successful\n", module
);
181 case PNP_NOT_SET_STATICALLY
:
182 printk(KERN_ERR
"PnPBIOS: %s: unable to set static resources\n", module
);
184 case PNP_UNKNOWN_FUNCTION
:
185 printk(KERN_ERR
"PnPBIOS: %s: invalid function number passed\n", module
);
187 case PNP_FUNCTION_NOT_SUPPORTED
:
188 printk(KERN_ERR
"PnPBIOS: %s: function not supported on this system\n", module
);
190 case PNP_INVALID_HANDLE
:
191 printk(KERN_ERR
"PnPBIOS: %s: invalid handle\n", module
);
193 case PNP_BAD_PARAMETER
:
194 printk(KERN_ERR
"PnPBIOS: %s: invalid parameters were passed\n", module
);
197 printk(KERN_ERR
"PnPBIOS: %s: unable to set resources\n", module
);
199 case PNP_EVENTS_NOT_PENDING
:
200 printk(KERN_ERR
"PnPBIOS: %s: no events are pending\n", module
);
202 case PNP_SYSTEM_NOT_DOCKED
:
203 printk(KERN_ERR
"PnPBIOS: %s: the system is not docked\n", module
);
205 case PNP_NO_ISA_PNP_CARDS
:
206 printk(KERN_ERR
"PnPBIOS: %s: no isapnp cards are installed on this system\n", module
);
208 case PNP_UNABLE_TO_DETERMINE_DOCK_CAPABILITIES
:
209 printk(KERN_ERR
"PnPBIOS: %s: cannot determine the capabilities of the docking station\n", module
);
211 case PNP_CONFIG_CHANGE_FAILED_NO_BATTERY
:
212 printk(KERN_ERR
"PnPBIOS: %s: unable to undock, the system does not have a battery\n", module
);
214 case PNP_CONFIG_CHANGE_FAILED_RESOURCE_CONFLICT
:
215 printk(KERN_ERR
"PnPBIOS: %s: could not dock due to resource conflicts\n", module
);
217 case PNP_BUFFER_TOO_SMALL
:
218 printk(KERN_ERR
"PnPBIOS: %s: the buffer passed is too small\n", module
);
220 case PNP_USE_ESCD_SUPPORT
:
221 printk(KERN_ERR
"PnPBIOS: %s: use ESCD instead\n", module
);
223 case PNP_MESSAGE_NOT_SUPPORTED
:
224 printk(KERN_ERR
"PnPBIOS: %s: the message is unsupported\n", module
);
226 case PNP_HARDWARE_ERROR
:
227 printk(KERN_ERR
"PnPBIOS: %s: a hardware failure has occured\n", module
);
230 printk(KERN_ERR
"PnPBIOS: %s: unexpected status 0x%x\n", module
, status
);
237 * PnP BIOS Low Level Calls
240 #define PNP_GET_NUM_SYS_DEV_NODES 0x00
241 #define PNP_GET_SYS_DEV_NODE 0x01
242 #define PNP_SET_SYS_DEV_NODE 0x02
243 #define PNP_GET_EVENT 0x03
244 #define PNP_SEND_MESSAGE 0x04
245 #define PNP_GET_DOCKING_STATION_INFORMATION 0x05
246 #define PNP_SET_STATIC_ALLOCED_RES_INFO 0x09
247 #define PNP_GET_STATIC_ALLOCED_RES_INFO 0x0a
248 #define PNP_GET_APM_ID_TABLE 0x0b
249 #define PNP_GET_PNP_ISA_CONFIG_STRUC 0x40
250 #define PNP_GET_ESCD_INFO 0x41
251 #define PNP_READ_ESCD 0x42
252 #define PNP_WRITE_ESCD 0x43
255 * Call PnP BIOS with function 0x00, "get number of system device nodes"
257 static int __pnp_bios_dev_node_info(struct pnp_dev_node_info
*data
)
260 if (!pnp_bios_present())
261 return PNP_FUNCTION_NOT_SUPPORTED
;
262 status
= call_pnp_bios(PNP_GET_NUM_SYS_DEV_NODES
, 0, PNP_TS1
, 2, PNP_TS1
, PNP_DS
, 0, 0,
263 data
, sizeof(struct pnp_dev_node_info
), NULL
, 0);
264 data
->no_nodes
&= 0xff;
268 int pnp_bios_dev_node_info(struct pnp_dev_node_info
*data
)
270 int status
= __pnp_bios_dev_node_info( data
);
272 pnpbios_print_status( "dev_node_info", status
);
277 * Note that some PnP BIOSes (e.g., on Sony Vaio laptops) die a horrible
278 * death if they are asked to access the "current" configuration.
279 * Therefore, if it's a matter of indifference, it's better to call
280 * get_dev_node() and set_dev_node() with boot=1 rather than with boot=0.
284 * Call PnP BIOS with function 0x01, "get system device node"
285 * Input: *nodenum = desired node,
286 * boot = whether to get nonvolatile boot (!=0)
287 * or volatile current (0) config
288 * Output: *nodenum=next node or 0xff if no more nodes
290 static int __pnp_bios_get_dev_node(u8
*nodenum
, char boot
, struct pnp_bios_node
*data
)
293 if (!pnp_bios_present())
294 return PNP_FUNCTION_NOT_SUPPORTED
;
295 if ( !boot
&& pnpbios_dont_use_current_config
)
296 return PNP_FUNCTION_NOT_SUPPORTED
;
297 status
= call_pnp_bios(PNP_GET_SYS_DEV_NODE
, 0, PNP_TS1
, 0, PNP_TS2
, boot
? 2 : 1, PNP_DS
, 0,
298 nodenum
, sizeof(char), data
, 65536);
302 int pnp_bios_get_dev_node(u8
*nodenum
, char boot
, struct pnp_bios_node
*data
)
305 status
= __pnp_bios_get_dev_node( nodenum
, boot
, data
);
307 pnpbios_print_status( "get_dev_node", status
);
313 * Call PnP BIOS with function 0x02, "set system device node"
314 * Input: *nodenum = desired node,
315 * boot = whether to set nonvolatile boot (!=0)
316 * or volatile current (0) config
318 static int __pnp_bios_set_dev_node(u8 nodenum
, char boot
, struct pnp_bios_node
*data
)
321 if (!pnp_bios_present())
322 return PNP_FUNCTION_NOT_SUPPORTED
;
323 if ( !boot
&& pnpbios_dont_use_current_config
)
324 return PNP_FUNCTION_NOT_SUPPORTED
;
325 status
= call_pnp_bios(PNP_SET_SYS_DEV_NODE
, nodenum
, 0, PNP_TS1
, boot
? 2 : 1, PNP_DS
, 0, 0,
326 data
, 65536, NULL
, 0);
330 int pnp_bios_set_dev_node(u8 nodenum
, char boot
, struct pnp_bios_node
*data
)
333 status
= __pnp_bios_set_dev_node( nodenum
, boot
, data
);
335 pnpbios_print_status( "set_dev_node", status
);
338 if ( !boot
) { /* Update devlist */
339 status
= pnp_bios_get_dev_node( &nodenum
, boot
, data
);
348 * Call PnP BIOS with function 0x03, "get event"
350 static int pnp_bios_get_event(u16
*event
)
353 if (!pnp_bios_present())
354 return PNP_FUNCTION_NOT_SUPPORTED
;
355 status
= call_pnp_bios(PNP_GET_EVENT
, 0, PNP_TS1
, PNP_DS
, 0, 0 ,0 ,0,
356 event
, sizeof(u16
), NULL
, 0);
363 * Call PnP BIOS with function 0x04, "send message"
365 static int pnp_bios_send_message(u16 message
)
368 if (!pnp_bios_present())
369 return PNP_FUNCTION_NOT_SUPPORTED
;
370 status
= call_pnp_bios(PNP_SEND_MESSAGE
, message
, PNP_DS
, 0, 0, 0, 0, 0, 0, 0, 0, 0);
376 * Call PnP BIOS with function 0x05, "get docking station information"
378 int pnp_bios_dock_station_info(struct pnp_docking_station_info
*data
)
381 if (!pnp_bios_present())
382 return PNP_FUNCTION_NOT_SUPPORTED
;
383 status
= call_pnp_bios(PNP_GET_DOCKING_STATION_INFORMATION
, 0, PNP_TS1
, PNP_DS
, 0, 0, 0, 0,
384 data
, sizeof(struct pnp_docking_station_info
), NULL
, 0);
390 * Call PnP BIOS with function 0x09, "set statically allocated resource
393 static int pnp_bios_set_stat_res(char *info
)
396 if (!pnp_bios_present())
397 return PNP_FUNCTION_NOT_SUPPORTED
;
398 status
= call_pnp_bios(PNP_SET_STATIC_ALLOCED_RES_INFO
, 0, PNP_TS1
, PNP_DS
, 0, 0, 0, 0,
399 info
, *((u16
*) info
), 0, 0);
405 * Call PnP BIOS with function 0x0a, "get statically allocated resource
408 static int __pnp_bios_get_stat_res(char *info
)
411 if (!pnp_bios_present())
412 return PNP_FUNCTION_NOT_SUPPORTED
;
413 status
= call_pnp_bios(PNP_GET_STATIC_ALLOCED_RES_INFO
, 0, PNP_TS1
, PNP_DS
, 0, 0, 0, 0,
414 info
, 65536, NULL
, 0);
418 int pnp_bios_get_stat_res(char *info
)
421 status
= __pnp_bios_get_stat_res( info
);
423 pnpbios_print_status( "get_stat_res", status
);
429 * Call PnP BIOS with function 0x0b, "get APM id table"
431 static int pnp_bios_apm_id_table(char *table
, u16
*size
)
434 if (!pnp_bios_present())
435 return PNP_FUNCTION_NOT_SUPPORTED
;
436 status
= call_pnp_bios(PNP_GET_APM_ID_TABLE
, 0, PNP_TS2
, 0, PNP_TS1
, PNP_DS
, 0, 0,
437 table
, *size
, size
, sizeof(u16
));
443 * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
445 static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc
*data
)
448 if (!pnp_bios_present())
449 return PNP_FUNCTION_NOT_SUPPORTED
;
450 status
= call_pnp_bios(PNP_GET_PNP_ISA_CONFIG_STRUC
, 0, PNP_TS1
, PNP_DS
, 0, 0, 0, 0,
451 data
, sizeof(struct pnp_isa_config_struc
), NULL
, 0);
455 int pnp_bios_isapnp_config(struct pnp_isa_config_struc
*data
)
458 status
= __pnp_bios_isapnp_config( data
);
460 pnpbios_print_status( "isapnp_config", status
);
465 * Call PnP BIOS with function 0x41, "get ESCD info"
467 static int __pnp_bios_escd_info(struct escd_info_struc
*data
)
470 if (!pnp_bios_present())
471 return ESCD_FUNCTION_NOT_SUPPORTED
;
472 status
= call_pnp_bios(PNP_GET_ESCD_INFO
, 0, PNP_TS1
, 2, PNP_TS1
, 4, PNP_TS1
, PNP_DS
,
473 data
, sizeof(struct escd_info_struc
), NULL
, 0);
477 int pnp_bios_escd_info(struct escd_info_struc
*data
)
480 status
= __pnp_bios_escd_info( data
);
482 pnpbios_print_status( "escd_info", status
);
487 * Call PnP BIOS function 0x42, "read ESCD"
488 * nvram_base is determined by calling escd_info
490 static int __pnp_bios_read_escd(char *data
, u32 nvram_base
)
493 if (!pnp_bios_present())
494 return ESCD_FUNCTION_NOT_SUPPORTED
;
495 status
= call_pnp_bios(PNP_READ_ESCD
, 0, PNP_TS1
, PNP_TS2
, PNP_DS
, 0, 0, 0,
496 data
, 65536, __va(nvram_base
), 65536);
500 int pnp_bios_read_escd(char *data
, u32 nvram_base
)
503 status
= __pnp_bios_read_escd( data
, nvram_base
);
505 pnpbios_print_status( "read_escd", status
);
511 * Call PnP BIOS function 0x43, "write ESCD"
513 static int pnp_bios_write_escd(char *data
, u32 nvram_base
)
516 if (!pnp_bios_present())
517 return ESCD_FUNCTION_NOT_SUPPORTED
;
518 status
= call_pnp_bios(PNP_WRITE_ESCD
, 0, PNP_TS1
, PNP_TS2
, PNP_DS
, 0, 0, 0,
519 data
, 65536, __va(nvram_base
), 65536);
529 void pnpbios_calls_init(union pnp_bios_install_struct
*header
)
532 spin_lock_init(&pnp_bios_lock
);
533 pnp_bios_callpoint
.offset
= header
->fields
.pm16offset
;
534 pnp_bios_callpoint
.segment
= PNP_CS16
;
536 set_base(bad_bios_desc
, __va((unsigned long)0x40 << 4));
537 _set_limit((char *)&bad_bios_desc
, 4095 - (0x40 << 4));
538 for(i
=0; i
< NR_CPUS
; i
++)
540 Q2_SET_SEL(i
, PNP_CS32
, &pnp_bios_callfunc
, 64 * 1024);
541 Q_SET_SEL(i
, PNP_CS16
, header
->fields
.pm16cseg
, 64 * 1024);
542 Q_SET_SEL(i
, PNP_DS
, header
->fields
.pm16dseg
, 64 * 1024);