37bacfcdbc5d4d6200e001885c9928a461c03fe1
[pandora-kernel.git] / drivers / pnp / pnpbios / bioscalls.c
1 /*
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
28 static struct {
29         u16     offset;
30         u16     segment;
31 } pnp_bios_callpoint;
32
33
34 /*
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
45 asmlinkage 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) \
62 do { \
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); \
66 } while(0)
67
68 #define Q2_SET_SEL(cpu, selname, address, size) \
69 do { \
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); \
73 } while(0)
74
75 static 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
82 u32 pnp_bios_fault_esp;
83 u32 pnp_bios_fault_eip;
84 u32 pnp_bios_is_utter_crap = 0;
85
86 static spinlock_t pnp_bios_lock;
87
88
89 /*
90  * Support Functions
91  */
92
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)
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();
111         save_desc_40 = get_cpu_gdt_table(cpu)[0x40 / 8];
112         get_cpu_gdt_table(cpu)[0x40 / 8] = bad_bios_desc;
113
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
154         get_cpu_gdt_table(cpu)[0x40 / 8] = save_desc_40;
155         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
168 void 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  */
250 static 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
261 int 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  */
283 static int __pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
284 {
285         u16 status;
286         if (!pnp_bios_present())
287                 return PNP_FUNCTION_NOT_SUPPORTED;
288         if ( !boot && pnpbios_dont_use_current_config )
289                 return PNP_FUNCTION_NOT_SUPPORTED;
290         status = call_pnp_bios(PNP_GET_SYS_DEV_NODE, 0, PNP_TS1, 0, PNP_TS2, boot ? 2 : 1, PNP_DS, 0,
291                                nodenum, sizeof(char), data, 65536);
292         return status;
293 }
294
295 int pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
296 {
297         int status;
298         status =  __pnp_bios_get_dev_node( nodenum, boot, data );
299         if ( status )
300                 pnpbios_print_status( "get_dev_node", status );
301         return status;
302 }
303
304
305 /*
306  * Call PnP BIOS with function 0x02, "set system device node"
307  * Input: *nodenum = desired node, 
308  *        boot = whether to set nonvolatile boot (!=0)
309  *               or volatile current (0) config
310  */
311 static int __pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
312 {
313         u16 status;
314         if (!pnp_bios_present())
315                 return PNP_FUNCTION_NOT_SUPPORTED;
316         if ( !boot && pnpbios_dont_use_current_config )
317                 return PNP_FUNCTION_NOT_SUPPORTED;
318         status = call_pnp_bios(PNP_SET_SYS_DEV_NODE, nodenum, 0, PNP_TS1, boot ? 2 : 1, PNP_DS, 0, 0,
319                                data, 65536, NULL, 0);
320         return status;
321 }
322
323 int pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
324 {
325         int status;
326         status =  __pnp_bios_set_dev_node( nodenum, boot, data );
327         if ( status ) {
328                 pnpbios_print_status( "set_dev_node", status );
329                 return status;
330         }
331         if ( !boot ) { /* Update devlist */
332                 status =  pnp_bios_get_dev_node( &nodenum, boot, data );
333                 if ( status )
334                         return status;
335         }
336         return status;
337 }
338
339 #if needed
340 /*
341  * Call PnP BIOS with function 0x03, "get event"
342  */
343 static int pnp_bios_get_event(u16 *event)
344 {
345         u16 status;
346         if (!pnp_bios_present())
347                 return PNP_FUNCTION_NOT_SUPPORTED;
348         status = call_pnp_bios(PNP_GET_EVENT, 0, PNP_TS1, PNP_DS, 0, 0 ,0 ,0,
349                                event, sizeof(u16), NULL, 0);
350         return status;
351 }
352 #endif
353
354 #if needed
355 /*
356  * Call PnP BIOS with function 0x04, "send message"
357  */
358 static int pnp_bios_send_message(u16 message)
359 {
360         u16 status;
361         if (!pnp_bios_present())
362                 return PNP_FUNCTION_NOT_SUPPORTED;
363         status = call_pnp_bios(PNP_SEND_MESSAGE, message, PNP_DS, 0, 0, 0, 0, 0, 0, 0, 0, 0);
364         return status;
365 }
366 #endif
367
368 /*
369  * Call PnP BIOS with function 0x05, "get docking station information"
370  */
371 int pnp_bios_dock_station_info(struct pnp_docking_station_info *data)
372 {
373         u16 status;
374         if (!pnp_bios_present())
375                 return PNP_FUNCTION_NOT_SUPPORTED;
376         status = call_pnp_bios(PNP_GET_DOCKING_STATION_INFORMATION, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
377                                data, sizeof(struct pnp_docking_station_info), NULL, 0);
378         return status;
379 }
380
381 #if needed
382 /*
383  * Call PnP BIOS with function 0x09, "set statically allocated resource
384  * information"
385  */
386 static int pnp_bios_set_stat_res(char *info)
387 {
388         u16 status;
389         if (!pnp_bios_present())
390                 return PNP_FUNCTION_NOT_SUPPORTED;
391         status = call_pnp_bios(PNP_SET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
392                                info, *((u16 *) info), 0, 0);
393         return status;
394 }
395 #endif
396
397 /*
398  * Call PnP BIOS with function 0x0a, "get statically allocated resource
399  * information"
400  */
401 static int __pnp_bios_get_stat_res(char *info)
402 {
403         u16 status;
404         if (!pnp_bios_present())
405                 return PNP_FUNCTION_NOT_SUPPORTED;
406         status = call_pnp_bios(PNP_GET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
407                                info, 65536, NULL, 0);
408         return status;
409 }
410
411 int pnp_bios_get_stat_res(char *info)
412 {
413         int status;
414         status = __pnp_bios_get_stat_res( info );
415         if ( status )
416                 pnpbios_print_status( "get_stat_res", status );
417         return status;
418 }
419
420 #if needed
421 /*
422  * Call PnP BIOS with function 0x0b, "get APM id table"
423  */
424 static int pnp_bios_apm_id_table(char *table, u16 *size)
425 {
426         u16 status;
427         if (!pnp_bios_present())
428                 return PNP_FUNCTION_NOT_SUPPORTED;
429         status = call_pnp_bios(PNP_GET_APM_ID_TABLE, 0, PNP_TS2, 0, PNP_TS1, PNP_DS, 0, 0,
430                                table, *size, size, sizeof(u16));
431         return status;
432 }
433 #endif
434
435 /*
436  * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
437  */
438 static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
439 {
440         u16 status;
441         if (!pnp_bios_present())
442                 return PNP_FUNCTION_NOT_SUPPORTED;
443         status = call_pnp_bios(PNP_GET_PNP_ISA_CONFIG_STRUC, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
444                                data, sizeof(struct pnp_isa_config_struc), NULL, 0);
445         return status;
446 }
447
448 int pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
449 {
450         int status;
451         status = __pnp_bios_isapnp_config( data );
452         if ( status )
453                 pnpbios_print_status( "isapnp_config", status );
454         return status;
455 }
456
457 /*
458  * Call PnP BIOS with function 0x41, "get ESCD info"
459  */
460 static int __pnp_bios_escd_info(struct escd_info_struc *data)
461 {
462         u16 status;
463         if (!pnp_bios_present())
464                 return ESCD_FUNCTION_NOT_SUPPORTED;
465         status = call_pnp_bios(PNP_GET_ESCD_INFO, 0, PNP_TS1, 2, PNP_TS1, 4, PNP_TS1, PNP_DS,
466                                data, sizeof(struct escd_info_struc), NULL, 0);
467         return status;
468 }
469
470 int pnp_bios_escd_info(struct escd_info_struc *data)
471 {
472         int status;
473         status = __pnp_bios_escd_info( data );
474         if ( status )
475                 pnpbios_print_status( "escd_info", status );
476         return status;
477 }
478
479 /*
480  * Call PnP BIOS function 0x42, "read ESCD"
481  * nvram_base is determined by calling escd_info
482  */
483 static int __pnp_bios_read_escd(char *data, u32 nvram_base)
484 {
485         u16 status;
486         if (!pnp_bios_present())
487                 return ESCD_FUNCTION_NOT_SUPPORTED;
488         status = call_pnp_bios(PNP_READ_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
489                                data, 65536, __va(nvram_base), 65536);
490         return status;
491 }
492
493 int pnp_bios_read_escd(char *data, u32 nvram_base)
494 {
495         int status;
496         status = __pnp_bios_read_escd( data, nvram_base );
497         if ( status )
498                 pnpbios_print_status( "read_escd", status );
499         return status;
500 }
501
502 #if needed
503 /*
504  * Call PnP BIOS function 0x43, "write ESCD"
505  */
506 static int pnp_bios_write_escd(char *data, u32 nvram_base)
507 {
508         u16 status;
509         if (!pnp_bios_present())
510                 return ESCD_FUNCTION_NOT_SUPPORTED;
511         status = call_pnp_bios(PNP_WRITE_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
512                                data, 65536, __va(nvram_base), 65536);
513         return status;
514 }
515 #endif
516
517
518 /*
519  * Initialization
520  */
521
522 void pnpbios_calls_init(union pnp_bios_install_struct *header)
523 {
524         int i;
525         spin_lock_init(&pnp_bios_lock);
526         pnp_bios_callpoint.offset = header->fields.pm16offset;
527         pnp_bios_callpoint.segment = PNP_CS16;
528
529         set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
530         _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
531         for (i = 0; i < NR_CPUS; i++) {
532                 struct desc_struct *gdt = get_cpu_gdt_table(i);
533                 if (!gdt)
534                         continue;
535                 Q2_SET_SEL(i, PNP_CS32, &pnp_bios_callfunc, 64 * 1024);
536                 Q_SET_SEL(i, PNP_CS16, header->fields.pm16cseg, 64 * 1024);
537                 Q_SET_SEL(i, PNP_DS, header->fields.pm16dseg, 64 * 1024);
538         }
539 }