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>
35 * These are some opcodes for a "static asmlinkage"
36 * As this code is *not* executed inside the linux kernel segment, but in a
37 * alias at offset 0, we need a far return that can not be compiled by
38 * default (please, prove me wrong! this is *really* ugly!)
39 * This is the only way to get the bios to return into the kernel code,
40 * because the bios code runs in 16 bit protected mode and therefore can only
41 * return to the caller if the call is within the first 64kB, and the linux
42 * kernel begins at offset 3GB...
45 asmlinkage void pnp_bios_callfunc(void);
50 "pnp_bios_callfunc:\n"
55 " lcallw *pnp_bios_callpoint\n"
61 #define Q_SET_SEL(cpu, selname, address, size) \
63 struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
64 set_base(gdt[(selname) >> 3], __va((u32)(address))); \
65 set_limit(gdt[(selname) >> 3], size); \
68 #define Q2_SET_SEL(cpu, selname, address, size) \
70 struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
71 set_base(gdt[(selname) >> 3], (u32)(address)); \
72 set_limit(gdt[(selname) >> 3], size); \
75 static struct desc_struct bad_bios_desc = { 0, 0x00409200 };
78 * At some point we want to use this stack frame pointer to unwind
79 * after PnP BIOS oopses.
82 u32 pnp_bios_fault_esp;
83 u32 pnp_bios_fault_eip;
84 u32 pnp_bios_is_utter_crap = 0;
86 static spinlock_t pnp_bios_lock;
93 static inline u16 call_pnp_bios(u16 func, u16 arg1, u16 arg2, u16 arg3,
94 u16 arg4, u16 arg5, u16 arg6, u16 arg7,
95 void *ts1_base, u32 ts1_size,
96 void *ts2_base, u32 ts2_size)
100 struct desc_struct save_desc_40;
104 * PnP BIOSes are generally not terribly re-entrant.
105 * Also, don't rely on them to save everything correctly.
107 if(pnp_bios_is_utter_crap)
108 return PNP_FUNCTION_NOT_SUPPORTED;
111 save_desc_40 = get_cpu_gdt_table(cpu)[0x40 / 8];
112 get_cpu_gdt_table(cpu)[0x40 / 8] = bad_bios_desc;
114 /* On some boxes IRQ's during PnP BIOS calls are deadly. */
115 spin_lock_irqsave(&pnp_bios_lock, flags);
117 /* The lock prevents us bouncing CPU here */
119 Q2_SET_SEL(smp_processor_id(), PNP_TS1, ts1_base, ts1_size);
121 Q2_SET_SEL(smp_processor_id(), PNP_TS2, ts2_base, ts2_size);
123 __asm__ __volatile__(
132 "movl %%esp, pnp_bios_fault_esp\n\t"
133 "movl $1f, pnp_bios_fault_eip\n\t"
144 : "0" ((func) | (((u32)arg1) << 16)),
145 "b" ((arg2) | (((u32)arg3) << 16)),
146 "c" ((arg4) | (((u32)arg5) << 16)),
147 "d" ((arg6) | (((u32)arg7) << 16)),
152 spin_unlock_irqrestore(&pnp_bios_lock, flags);
154 get_cpu_gdt_table(cpu)[0x40 / 8] = save_desc_40;
157 /* If we get here and this is set then the PnP BIOS faulted on us. */
158 if(pnp_bios_is_utter_crap)
160 printk(KERN_ERR "PnPBIOS: Warning! Your PnP BIOS caused a fatal error. Attempting to continue\n");
161 printk(KERN_ERR "PnPBIOS: You may need to reboot with the \"pnpbios=off\" option to operate stably\n");
162 printk(KERN_ERR "PnPBIOS: Check with your vendor for an updated BIOS\n");
168 void pnpbios_print_status(const char * module, u16 status)
172 printk(KERN_ERR "PnPBIOS: %s: function successful\n", module);
174 case PNP_NOT_SET_STATICALLY:
175 printk(KERN_ERR "PnPBIOS: %s: unable to set static resources\n", module);
177 case PNP_UNKNOWN_FUNCTION:
178 printk(KERN_ERR "PnPBIOS: %s: invalid function number passed\n", module);
180 case PNP_FUNCTION_NOT_SUPPORTED:
181 printk(KERN_ERR "PnPBIOS: %s: function not supported on this system\n", module);
183 case PNP_INVALID_HANDLE:
184 printk(KERN_ERR "PnPBIOS: %s: invalid handle\n", module);
186 case PNP_BAD_PARAMETER:
187 printk(KERN_ERR "PnPBIOS: %s: invalid parameters were passed\n", module);
190 printk(KERN_ERR "PnPBIOS: %s: unable to set resources\n", module);
192 case PNP_EVENTS_NOT_PENDING:
193 printk(KERN_ERR "PnPBIOS: %s: no events are pending\n", module);
195 case PNP_SYSTEM_NOT_DOCKED:
196 printk(KERN_ERR "PnPBIOS: %s: the system is not docked\n", module);
198 case PNP_NO_ISA_PNP_CARDS:
199 printk(KERN_ERR "PnPBIOS: %s: no isapnp cards are installed on this system\n", module);
201 case PNP_UNABLE_TO_DETERMINE_DOCK_CAPABILITIES:
202 printk(KERN_ERR "PnPBIOS: %s: cannot determine the capabilities of the docking station\n", module);
204 case PNP_CONFIG_CHANGE_FAILED_NO_BATTERY:
205 printk(KERN_ERR "PnPBIOS: %s: unable to undock, the system does not have a battery\n", module);
207 case PNP_CONFIG_CHANGE_FAILED_RESOURCE_CONFLICT:
208 printk(KERN_ERR "PnPBIOS: %s: could not dock due to resource conflicts\n", module);
210 case PNP_BUFFER_TOO_SMALL:
211 printk(KERN_ERR "PnPBIOS: %s: the buffer passed is too small\n", module);
213 case PNP_USE_ESCD_SUPPORT:
214 printk(KERN_ERR "PnPBIOS: %s: use ESCD instead\n", module);
216 case PNP_MESSAGE_NOT_SUPPORTED:
217 printk(KERN_ERR "PnPBIOS: %s: the message is unsupported\n", module);
219 case PNP_HARDWARE_ERROR:
220 printk(KERN_ERR "PnPBIOS: %s: a hardware failure has occured\n", module);
223 printk(KERN_ERR "PnPBIOS: %s: unexpected status 0x%x\n", module, status);
230 * PnP BIOS Low Level Calls
233 #define PNP_GET_NUM_SYS_DEV_NODES 0x00
234 #define PNP_GET_SYS_DEV_NODE 0x01
235 #define PNP_SET_SYS_DEV_NODE 0x02
236 #define PNP_GET_EVENT 0x03
237 #define PNP_SEND_MESSAGE 0x04
238 #define PNP_GET_DOCKING_STATION_INFORMATION 0x05
239 #define PNP_SET_STATIC_ALLOCED_RES_INFO 0x09
240 #define PNP_GET_STATIC_ALLOCED_RES_INFO 0x0a
241 #define PNP_GET_APM_ID_TABLE 0x0b
242 #define PNP_GET_PNP_ISA_CONFIG_STRUC 0x40
243 #define PNP_GET_ESCD_INFO 0x41
244 #define PNP_READ_ESCD 0x42
245 #define PNP_WRITE_ESCD 0x43
248 * Call PnP BIOS with function 0x00, "get number of system device nodes"
250 static int __pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
253 if (!pnp_bios_present())
254 return PNP_FUNCTION_NOT_SUPPORTED;
255 status = call_pnp_bios(PNP_GET_NUM_SYS_DEV_NODES, 0, PNP_TS1, 2, PNP_TS1, PNP_DS, 0, 0,
256 data, sizeof(struct pnp_dev_node_info), NULL, 0);
257 data->no_nodes &= 0xff;
261 int pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
263 int status = __pnp_bios_dev_node_info( data );
265 pnpbios_print_status( "dev_node_info", status );
270 * Note that some PnP BIOSes (e.g., on Sony Vaio laptops) die a horrible
271 * death if they are asked to access the "current" configuration.
272 * Therefore, if it's a matter of indifference, it's better to call
273 * get_dev_node() and set_dev_node() with boot=1 rather than with boot=0.
277 * Call PnP BIOS with function 0x01, "get system device node"
278 * Input: *nodenum = desired node,
279 * boot = whether to get nonvolatile boot (!=0)
280 * or volatile current (0) config
281 * Output: *nodenum=next node or 0xff if no more nodes
283 static int __pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
287 if (!pnp_bios_present())
288 return PNP_FUNCTION_NOT_SUPPORTED;
289 if ( !boot && pnpbios_dont_use_current_config )
290 return PNP_FUNCTION_NOT_SUPPORTED;
291 tmp_nodenum = *nodenum;
292 status = call_pnp_bios(PNP_GET_SYS_DEV_NODE, 0, PNP_TS1, 0, PNP_TS2, boot ? 2 : 1, PNP_DS, 0,
293 &tmp_nodenum, sizeof(tmp_nodenum), data, 65536);
294 *nodenum = tmp_nodenum;
298 int pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
301 status = __pnp_bios_get_dev_node( nodenum, boot, data );
303 pnpbios_print_status( "get_dev_node", status );
309 * Call PnP BIOS with function 0x02, "set system device node"
310 * Input: *nodenum = desired node,
311 * boot = whether to set nonvolatile boot (!=0)
312 * or volatile current (0) config
314 static int __pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
317 if (!pnp_bios_present())
318 return PNP_FUNCTION_NOT_SUPPORTED;
319 if ( !boot && pnpbios_dont_use_current_config )
320 return PNP_FUNCTION_NOT_SUPPORTED;
321 status = call_pnp_bios(PNP_SET_SYS_DEV_NODE, nodenum, 0, PNP_TS1, boot ? 2 : 1, PNP_DS, 0, 0,
322 data, 65536, NULL, 0);
326 int pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
329 status = __pnp_bios_set_dev_node( nodenum, boot, data );
331 pnpbios_print_status( "set_dev_node", status );
334 if ( !boot ) { /* Update devlist */
335 status = pnp_bios_get_dev_node( &nodenum, boot, data );
344 * Call PnP BIOS with function 0x03, "get event"
346 static int pnp_bios_get_event(u16 *event)
349 if (!pnp_bios_present())
350 return PNP_FUNCTION_NOT_SUPPORTED;
351 status = call_pnp_bios(PNP_GET_EVENT, 0, PNP_TS1, PNP_DS, 0, 0 ,0 ,0,
352 event, sizeof(u16), NULL, 0);
359 * Call PnP BIOS with function 0x04, "send message"
361 static int pnp_bios_send_message(u16 message)
364 if (!pnp_bios_present())
365 return PNP_FUNCTION_NOT_SUPPORTED;
366 status = call_pnp_bios(PNP_SEND_MESSAGE, message, PNP_DS, 0, 0, 0, 0, 0, 0, 0, 0, 0);
372 * Call PnP BIOS with function 0x05, "get docking station information"
374 int pnp_bios_dock_station_info(struct pnp_docking_station_info *data)
377 if (!pnp_bios_present())
378 return PNP_FUNCTION_NOT_SUPPORTED;
379 status = call_pnp_bios(PNP_GET_DOCKING_STATION_INFORMATION, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
380 data, sizeof(struct pnp_docking_station_info), NULL, 0);
386 * Call PnP BIOS with function 0x09, "set statically allocated resource
389 static int pnp_bios_set_stat_res(char *info)
392 if (!pnp_bios_present())
393 return PNP_FUNCTION_NOT_SUPPORTED;
394 status = call_pnp_bios(PNP_SET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
395 info, *((u16 *) info), 0, 0);
401 * Call PnP BIOS with function 0x0a, "get statically allocated resource
404 static int __pnp_bios_get_stat_res(char *info)
407 if (!pnp_bios_present())
408 return PNP_FUNCTION_NOT_SUPPORTED;
409 status = call_pnp_bios(PNP_GET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
410 info, 65536, NULL, 0);
414 int pnp_bios_get_stat_res(char *info)
417 status = __pnp_bios_get_stat_res( info );
419 pnpbios_print_status( "get_stat_res", status );
425 * Call PnP BIOS with function 0x0b, "get APM id table"
427 static int pnp_bios_apm_id_table(char *table, u16 *size)
430 if (!pnp_bios_present())
431 return PNP_FUNCTION_NOT_SUPPORTED;
432 status = call_pnp_bios(PNP_GET_APM_ID_TABLE, 0, PNP_TS2, 0, PNP_TS1, PNP_DS, 0, 0,
433 table, *size, size, sizeof(u16));
439 * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
441 static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
444 if (!pnp_bios_present())
445 return PNP_FUNCTION_NOT_SUPPORTED;
446 status = call_pnp_bios(PNP_GET_PNP_ISA_CONFIG_STRUC, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
447 data, sizeof(struct pnp_isa_config_struc), NULL, 0);
451 int pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
454 status = __pnp_bios_isapnp_config( data );
456 pnpbios_print_status( "isapnp_config", status );
461 * Call PnP BIOS with function 0x41, "get ESCD info"
463 static int __pnp_bios_escd_info(struct escd_info_struc *data)
466 if (!pnp_bios_present())
467 return ESCD_FUNCTION_NOT_SUPPORTED;
468 status = call_pnp_bios(PNP_GET_ESCD_INFO, 0, PNP_TS1, 2, PNP_TS1, 4, PNP_TS1, PNP_DS,
469 data, sizeof(struct escd_info_struc), NULL, 0);
473 int pnp_bios_escd_info(struct escd_info_struc *data)
476 status = __pnp_bios_escd_info( data );
478 pnpbios_print_status( "escd_info", status );
483 * Call PnP BIOS function 0x42, "read ESCD"
484 * nvram_base is determined by calling escd_info
486 static int __pnp_bios_read_escd(char *data, u32 nvram_base)
489 if (!pnp_bios_present())
490 return ESCD_FUNCTION_NOT_SUPPORTED;
491 status = call_pnp_bios(PNP_READ_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
492 data, 65536, __va(nvram_base), 65536);
496 int pnp_bios_read_escd(char *data, u32 nvram_base)
499 status = __pnp_bios_read_escd( data, nvram_base );
501 pnpbios_print_status( "read_escd", status );
507 * Call PnP BIOS function 0x43, "write ESCD"
509 static int pnp_bios_write_escd(char *data, u32 nvram_base)
512 if (!pnp_bios_present())
513 return ESCD_FUNCTION_NOT_SUPPORTED;
514 status = call_pnp_bios(PNP_WRITE_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
515 data, 65536, __va(nvram_base), 65536);
525 void pnpbios_calls_init(union pnp_bios_install_struct *header)
528 spin_lock_init(&pnp_bios_lock);
529 pnp_bios_callpoint.offset = header->fields.pm16offset;
530 pnp_bios_callpoint.segment = PNP_CS16;
532 set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
533 _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
534 for (i = 0; i < NR_CPUS; i++) {
535 struct desc_struct *gdt = get_cpu_gdt_table(i);
538 Q2_SET_SEL(i, PNP_CS32, &pnp_bios_callfunc, 64 * 1024);
539 Q_SET_SEL(i, PNP_CS16, header->fields.pm16cseg, 64 * 1024);
540 Q_SET_SEL(i, PNP_DS, header->fields.pm16dseg, 64 * 1024);