blob: a72126180e9736080c7f6c8b7cece88ff57f48ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * bioscalls.c - the lowlevel layer of the PnPBIOS driver
3 *
4 */
5
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>
14#include <linux/mm.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>
20
21#include <asm/page.h>
22#include <asm/desc.h>
23#include <asm/system.h>
24#include <asm/byteorder.h>
25
26#include "pnpbios.h"
27
28static struct {
29 u16 offset;
30 u16 segment;
31} pnp_bios_callpoint;
32
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
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...
43 */
44
45asmlinkage void pnp_bios_callfunc(void);
46
47__asm__(
48 ".text \n"
49 __ALIGN_STR "\n"
50 "pnp_bios_callfunc:\n"
51 " pushl %edx \n"
52 " pushl %ecx \n"
53 " pushl %ebx \n"
54 " pushl %eax \n"
55 " lcallw *pnp_bios_callpoint\n"
56 " addl $16, %esp \n"
57 " lret \n"
58 ".previous \n"
59);
60
61#define Q_SET_SEL(cpu, selname, address, size) \
62do { \
Zachary Amsden7c4cb602006-01-06 00:11:47 -080063struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
64set_base(gdt[(selname) >> 3], __va((u32)(address))); \
65set_limit(gdt[(selname) >> 3], size); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070066} while(0)
67
68#define Q2_SET_SEL(cpu, selname, address, size) \
69do { \
Zachary Amsden7c4cb602006-01-06 00:11:47 -080070struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
71set_base(gdt[(selname) >> 3], (u32)(address)); \
72set_limit(gdt[(selname) >> 3], size); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070073} while(0)
74
75static struct desc_struct bad_bios_desc = { 0, 0x00409200 };
76
77/*
78 * At some point we want to use this stack frame pointer to unwind
79 * after PnP BIOS oopses.
80 */
81
82u32 pnp_bios_fault_esp;
83u32 pnp_bios_fault_eip;
84u32 pnp_bios_is_utter_crap = 0;
85
86static spinlock_t pnp_bios_lock;
87
88
89/*
90 * Support Functions
91 */
92
93static 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)
97{
98 unsigned long flags;
99 u16 status;
100 struct desc_struct save_desc_40;
101 int cpu;
102
103 /*
104 * PnP BIOSes are generally not terribly re-entrant.
105 * Also, don't rely on them to save everything correctly.
106 */
107 if(pnp_bios_is_utter_crap)
108 return PNP_FUNCTION_NOT_SUPPORTED;
109
110 cpu = get_cpu();
Zachary Amsden7c4cb602006-01-06 00:11:47 -0800111 save_desc_40 = get_cpu_gdt_table(cpu)[0x40 / 8];
112 get_cpu_gdt_table(cpu)[0x40 / 8] = bad_bios_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /* On some boxes IRQ's during PnP BIOS calls are deadly. */
115 spin_lock_irqsave(&pnp_bios_lock, flags);
116
117 /* The lock prevents us bouncing CPU here */
118 if (ts1_size)
119 Q2_SET_SEL(smp_processor_id(), PNP_TS1, ts1_base, ts1_size);
120 if (ts2_size)
121 Q2_SET_SEL(smp_processor_id(), PNP_TS2, ts2_base, ts2_size);
122
123 __asm__ __volatile__(
124 "pushl %%ebp\n\t"
125 "pushl %%edi\n\t"
126 "pushl %%esi\n\t"
127 "pushl %%ds\n\t"
128 "pushl %%es\n\t"
129 "pushl %%fs\n\t"
130 "pushl %%gs\n\t"
131 "pushfl\n\t"
132 "movl %%esp, pnp_bios_fault_esp\n\t"
133 "movl $1f, pnp_bios_fault_eip\n\t"
134 "lcall %5,%6\n\t"
135 "1:popfl\n\t"
136 "popl %%gs\n\t"
137 "popl %%fs\n\t"
138 "popl %%es\n\t"
139 "popl %%ds\n\t"
140 "popl %%esi\n\t"
141 "popl %%edi\n\t"
142 "popl %%ebp\n\t"
143 : "=a" (status)
144 : "0" ((func) | (((u32)arg1) << 16)),
145 "b" ((arg2) | (((u32)arg3) << 16)),
146 "c" ((arg4) | (((u32)arg5) << 16)),
147 "d" ((arg6) | (((u32)arg7) << 16)),
148 "i" (PNP_CS32),
149 "i" (0)
150 : "memory"
151 );
152 spin_unlock_irqrestore(&pnp_bios_lock, flags);
153
Zachary Amsden7c4cb602006-01-06 00:11:47 -0800154 get_cpu_gdt_table(cpu)[0x40 / 8] = save_desc_40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 put_cpu();
156
157 /* If we get here and this is set then the PnP BIOS faulted on us. */
158 if(pnp_bios_is_utter_crap)
159 {
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");
163 }
164
165 return status;
166}
167
168void pnpbios_print_status(const char * module, u16 status)
169{
170 switch(status) {
171 case PNP_SUCCESS:
172 printk(KERN_ERR "PnPBIOS: %s: function successful\n", module);
173 break;
174 case PNP_NOT_SET_STATICALLY:
175 printk(KERN_ERR "PnPBIOS: %s: unable to set static resources\n", module);
176 break;
177 case PNP_UNKNOWN_FUNCTION:
178 printk(KERN_ERR "PnPBIOS: %s: invalid function number passed\n", module);
179 break;
180 case PNP_FUNCTION_NOT_SUPPORTED:
181 printk(KERN_ERR "PnPBIOS: %s: function not supported on this system\n", module);
182 break;
183 case PNP_INVALID_HANDLE:
184 printk(KERN_ERR "PnPBIOS: %s: invalid handle\n", module);
185 break;
186 case PNP_BAD_PARAMETER:
187 printk(KERN_ERR "PnPBIOS: %s: invalid parameters were passed\n", module);
188 break;
189 case PNP_SET_FAILED:
190 printk(KERN_ERR "PnPBIOS: %s: unable to set resources\n", module);
191 break;
192 case PNP_EVENTS_NOT_PENDING:
193 printk(KERN_ERR "PnPBIOS: %s: no events are pending\n", module);
194 break;
195 case PNP_SYSTEM_NOT_DOCKED:
196 printk(KERN_ERR "PnPBIOS: %s: the system is not docked\n", module);
197 break;
198 case PNP_NO_ISA_PNP_CARDS:
199 printk(KERN_ERR "PnPBIOS: %s: no isapnp cards are installed on this system\n", module);
200 break;
201 case PNP_UNABLE_TO_DETERMINE_DOCK_CAPABILITIES:
202 printk(KERN_ERR "PnPBIOS: %s: cannot determine the capabilities of the docking station\n", module);
203 break;
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);
206 break;
207 case PNP_CONFIG_CHANGE_FAILED_RESOURCE_CONFLICT:
208 printk(KERN_ERR "PnPBIOS: %s: could not dock due to resource conflicts\n", module);
209 break;
210 case PNP_BUFFER_TOO_SMALL:
211 printk(KERN_ERR "PnPBIOS: %s: the buffer passed is too small\n", module);
212 break;
213 case PNP_USE_ESCD_SUPPORT:
214 printk(KERN_ERR "PnPBIOS: %s: use ESCD instead\n", module);
215 break;
216 case PNP_MESSAGE_NOT_SUPPORTED:
217 printk(KERN_ERR "PnPBIOS: %s: the message is unsupported\n", module);
218 break;
219 case PNP_HARDWARE_ERROR:
220 printk(KERN_ERR "PnPBIOS: %s: a hardware failure has occured\n", module);
221 break;
222 default:
223 printk(KERN_ERR "PnPBIOS: %s: unexpected status 0x%x\n", module, status);
224 break;
225 }
226}
227
228
229/*
230 * PnP BIOS Low Level Calls
231 */
232
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
246
247/*
248 * Call PnP BIOS with function 0x00, "get number of system device nodes"
249 */
250static int __pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
251{
252 u16 status;
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;
258 return status;
259}
260
261int pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
262{
263 int status = __pnp_bios_dev_node_info( data );
264 if ( status )
265 pnpbios_print_status( "dev_node_info", status );
266 return status;
267}
268
269/*
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.
274 */
275
276/*
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
282 */
283static int __pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
284{
285 u16 status;
Zachary Amsden5fe9fe3c2006-01-06 00:11:55 -0800286 u16 tmp_nodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!pnp_bios_present())
288 return PNP_FUNCTION_NOT_SUPPORTED;
289 if ( !boot && pnpbios_dont_use_current_config )
290 return PNP_FUNCTION_NOT_SUPPORTED;
Zachary Amsden5fe9fe3c2006-01-06 00:11:55 -0800291 tmp_nodenum = *nodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 status = call_pnp_bios(PNP_GET_SYS_DEV_NODE, 0, PNP_TS1, 0, PNP_TS2, boot ? 2 : 1, PNP_DS, 0,
Zachary Amsden5fe9fe3c2006-01-06 00:11:55 -0800293 &tmp_nodenum, sizeof(tmp_nodenum), data, 65536);
294 *nodenum = tmp_nodenum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return status;
296}
297
298int pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
299{
300 int status;
301 status = __pnp_bios_get_dev_node( nodenum, boot, data );
302 if ( status )
303 pnpbios_print_status( "get_dev_node", status );
304 return status;
305}
306
307
308/*
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
313 */
314static int __pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
315{
316 u16 status;
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);
323 return status;
324}
325
326int pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
327{
328 int status;
329 status = __pnp_bios_set_dev_node( nodenum, boot, data );
330 if ( status ) {
331 pnpbios_print_status( "set_dev_node", status );
332 return status;
333 }
334 if ( !boot ) { /* Update devlist */
335 status = pnp_bios_get_dev_node( &nodenum, boot, data );
336 if ( status )
337 return status;
338 }
339 return status;
340}
341
342#if needed
343/*
344 * Call PnP BIOS with function 0x03, "get event"
345 */
346static int pnp_bios_get_event(u16 *event)
347{
348 u16 status;
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);
353 return status;
354}
355#endif
356
357#if needed
358/*
359 * Call PnP BIOS with function 0x04, "send message"
360 */
361static int pnp_bios_send_message(u16 message)
362{
363 u16 status;
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);
367 return status;
368}
369#endif
370
371/*
372 * Call PnP BIOS with function 0x05, "get docking station information"
373 */
374int pnp_bios_dock_station_info(struct pnp_docking_station_info *data)
375{
376 u16 status;
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);
381 return status;
382}
383
384#if needed
385/*
386 * Call PnP BIOS with function 0x09, "set statically allocated resource
387 * information"
388 */
389static int pnp_bios_set_stat_res(char *info)
390{
391 u16 status;
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);
396 return status;
397}
398#endif
399
400/*
401 * Call PnP BIOS with function 0x0a, "get statically allocated resource
402 * information"
403 */
404static int __pnp_bios_get_stat_res(char *info)
405{
406 u16 status;
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);
411 return status;
412}
413
414int pnp_bios_get_stat_res(char *info)
415{
416 int status;
417 status = __pnp_bios_get_stat_res( info );
418 if ( status )
419 pnpbios_print_status( "get_stat_res", status );
420 return status;
421}
422
423#if needed
424/*
425 * Call PnP BIOS with function 0x0b, "get APM id table"
426 */
427static int pnp_bios_apm_id_table(char *table, u16 *size)
428{
429 u16 status;
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));
434 return status;
435}
436#endif
437
438/*
439 * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
440 */
441static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
442{
443 u16 status;
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);
448 return status;
449}
450
451int pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
452{
453 int status;
454 status = __pnp_bios_isapnp_config( data );
455 if ( status )
456 pnpbios_print_status( "isapnp_config", status );
457 return status;
458}
459
460/*
461 * Call PnP BIOS with function 0x41, "get ESCD info"
462 */
463static int __pnp_bios_escd_info(struct escd_info_struc *data)
464{
465 u16 status;
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);
470 return status;
471}
472
473int pnp_bios_escd_info(struct escd_info_struc *data)
474{
475 int status;
476 status = __pnp_bios_escd_info( data );
477 if ( status )
478 pnpbios_print_status( "escd_info", status );
479 return status;
480}
481
482/*
483 * Call PnP BIOS function 0x42, "read ESCD"
484 * nvram_base is determined by calling escd_info
485 */
486static int __pnp_bios_read_escd(char *data, u32 nvram_base)
487{
488 u16 status;
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);
493 return status;
494}
495
496int pnp_bios_read_escd(char *data, u32 nvram_base)
497{
498 int status;
499 status = __pnp_bios_read_escd( data, nvram_base );
500 if ( status )
501 pnpbios_print_status( "read_escd", status );
502 return status;
503}
504
505#if needed
506/*
507 * Call PnP BIOS function 0x43, "write ESCD"
508 */
509static int pnp_bios_write_escd(char *data, u32 nvram_base)
510{
511 u16 status;
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);
516 return status;
517}
518#endif
519
520
521/*
522 * Initialization
523 */
524
525void pnpbios_calls_init(union pnp_bios_install_struct *header)
526{
527 int i;
528 spin_lock_init(&pnp_bios_lock);
529 pnp_bios_callpoint.offset = header->fields.pm16offset;
530 pnp_bios_callpoint.segment = PNP_CS16;
531
532 set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
533 _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
Zachary Amsden7c4cb602006-01-06 00:11:47 -0800534 for (i = 0; i < NR_CPUS; i++) {
535 struct desc_struct *gdt = get_cpu_gdt_table(i);
536 if (!gdt)
537 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 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);
541 }
542}