2 * bioscalls.c - the lowlevel layer of the PnPBIOS driver
5 #include <linux/types.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/linkage.h>
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/pnp.h>
13 #include <linux/smp.h>
14 #include <linux/slab.h>
15 #include <linux/kmod.h>
16 #include <linux/completion.h>
17 #include <linux/spinlock.h>
21 #include <asm/system.h>
22 #include <asm/byteorder.h>
32 * These are some opcodes for a "static asmlinkage"
33 * As this code is *not* executed inside the linux kernel segment, but in a
34 * alias at offset 0, we need a far return that can not be compiled by
35 * default (please, prove me wrong! this is *really* ugly!)
36 * This is the only way to get the bios to return into the kernel code,
37 * because the bios code runs in 16 bit protected mode and therefore can only
38 * return to the caller if the call is within the first 64kB, and the linux
39 * kernel begins at offset 3GB...
42 asmlinkage void pnp_bios_callfunc(void);
46 "pnp_bios_callfunc:\n"
51 " lcallw *pnp_bios_callpoint\n"
56 #define Q2_SET_SEL(cpu, selname, address, size) \
58 struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
59 set_base(gdt[(selname) >> 3], (u32)(address)); \
60 set_limit(gdt[(selname) >> 3], size); \
63 static struct desc_struct bad_bios_desc;
66 * At some point we want to use this stack frame pointer to unwind
67 * after PnP BIOS oopses.
70 u32 pnp_bios_fault_esp;
71 u32 pnp_bios_fault_eip;
72 u32 pnp_bios_is_utter_crap = 0;
74 static spinlock_t pnp_bios_lock;
80 static inline u16 call_pnp_bios(u16 func, u16 arg1, u16 arg2, u16 arg3,
81 u16 arg4, u16 arg5, u16 arg6, u16 arg7,
82 void *ts1_base, u32 ts1_size,
83 void *ts2_base, u32 ts2_size)
87 struct desc_struct save_desc_40;
91 * PnP BIOSes are generally not terribly re-entrant.
92 * Also, don't rely on them to save everything correctly.
94 if (pnp_bios_is_utter_crap)
95 return PNP_FUNCTION_NOT_SUPPORTED;
98 save_desc_40 = get_cpu_gdt_table(cpu)[0x40 / 8];
99 get_cpu_gdt_table(cpu)[0x40 / 8] = bad_bios_desc;
101 /* On some boxes IRQ's during PnP BIOS calls are deadly. */
102 spin_lock_irqsave(&pnp_bios_lock, flags);
104 /* The lock prevents us bouncing CPU here */
106 Q2_SET_SEL(smp_processor_id(), PNP_TS1, ts1_base, ts1_size);
108 Q2_SET_SEL(smp_processor_id(), PNP_TS2, ts2_base, ts2_size);
110 __asm__ __volatile__("pushl %%ebp\n\t"
118 "movl %%esp, pnp_bios_fault_esp\n\t"
119 "movl $1f, pnp_bios_fault_eip\n\t"
128 "popl %%ebp\n\t":"=a"(status)
129 :"0"((func) | (((u32) arg1) << 16)),
130 "b"((arg2) | (((u32) arg3) << 16)),
131 "c"((arg4) | (((u32) arg5) << 16)),
132 "d"((arg6) | (((u32) arg7) << 16)),
133 "i"(PNP_CS32), "i"(0)
135 spin_unlock_irqrestore(&pnp_bios_lock, flags);
137 get_cpu_gdt_table(cpu)[0x40 / 8] = save_desc_40;
140 /* If we get here and this is set then the PnP BIOS faulted on us. */
141 if (pnp_bios_is_utter_crap) {
143 "PnPBIOS: Warning! Your PnP BIOS caused a fatal error. Attempting to continue\n");
145 "PnPBIOS: You may need to reboot with the \"pnpbios=off\" option to operate stably\n");
147 "PnPBIOS: Check with your vendor for an updated BIOS\n");
153 void pnpbios_print_status(const char *module, u16 status)
157 printk(KERN_ERR "PnPBIOS: %s: function successful\n", module);
159 case PNP_NOT_SET_STATICALLY:
160 printk(KERN_ERR "PnPBIOS: %s: unable to set static resources\n",
163 case PNP_UNKNOWN_FUNCTION:
164 printk(KERN_ERR "PnPBIOS: %s: invalid function number passed\n",
167 case PNP_FUNCTION_NOT_SUPPORTED:
169 "PnPBIOS: %s: function not supported on this system\n",
172 case PNP_INVALID_HANDLE:
173 printk(KERN_ERR "PnPBIOS: %s: invalid handle\n", module);
175 case PNP_BAD_PARAMETER:
176 printk(KERN_ERR "PnPBIOS: %s: invalid parameters were passed\n",
180 printk(KERN_ERR "PnPBIOS: %s: unable to set resources\n",
183 case PNP_EVENTS_NOT_PENDING:
184 printk(KERN_ERR "PnPBIOS: %s: no events are pending\n", module);
186 case PNP_SYSTEM_NOT_DOCKED:
187 printk(KERN_ERR "PnPBIOS: %s: the system is not docked\n",
190 case PNP_NO_ISA_PNP_CARDS:
192 "PnPBIOS: %s: no isapnp cards are installed on this system\n",
195 case PNP_UNABLE_TO_DETERMINE_DOCK_CAPABILITIES:
197 "PnPBIOS: %s: cannot determine the capabilities of the docking station\n",
200 case PNP_CONFIG_CHANGE_FAILED_NO_BATTERY:
202 "PnPBIOS: %s: unable to undock, the system does not have a battery\n",
205 case PNP_CONFIG_CHANGE_FAILED_RESOURCE_CONFLICT:
207 "PnPBIOS: %s: could not dock due to resource conflicts\n",
210 case PNP_BUFFER_TOO_SMALL:
211 printk(KERN_ERR "PnPBIOS: %s: the buffer passed is too small\n",
214 case PNP_USE_ESCD_SUPPORT:
215 printk(KERN_ERR "PnPBIOS: %s: use ESCD instead\n", module);
217 case PNP_MESSAGE_NOT_SUPPORTED:
218 printk(KERN_ERR "PnPBIOS: %s: the message is unsupported\n",
221 case PNP_HARDWARE_ERROR:
222 printk(KERN_ERR "PnPBIOS: %s: a hardware failure has occured\n",
226 printk(KERN_ERR "PnPBIOS: %s: unexpected status 0x%x\n", module,
233 * PnP BIOS Low Level Calls
236 #define PNP_GET_NUM_SYS_DEV_NODES 0x00
237 #define PNP_GET_SYS_DEV_NODE 0x01
238 #define PNP_SET_SYS_DEV_NODE 0x02
239 #define PNP_GET_EVENT 0x03
240 #define PNP_SEND_MESSAGE 0x04
241 #define PNP_GET_DOCKING_STATION_INFORMATION 0x05
242 #define PNP_SET_STATIC_ALLOCED_RES_INFO 0x09
243 #define PNP_GET_STATIC_ALLOCED_RES_INFO 0x0a
244 #define PNP_GET_APM_ID_TABLE 0x0b
245 #define PNP_GET_PNP_ISA_CONFIG_STRUC 0x40
246 #define PNP_GET_ESCD_INFO 0x41
247 #define PNP_READ_ESCD 0x42
248 #define PNP_WRITE_ESCD 0x43
251 * Call PnP BIOS with function 0x00, "get number of system device nodes"
253 static int __pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
257 if (!pnp_bios_present())
258 return PNP_FUNCTION_NOT_SUPPORTED;
259 status = call_pnp_bios(PNP_GET_NUM_SYS_DEV_NODES, 0, PNP_TS1, 2,
260 PNP_TS1, PNP_DS, 0, 0, data,
261 sizeof(struct pnp_dev_node_info), NULL, 0);
262 data->no_nodes &= 0xff;
266 int pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
268 int status = __pnp_bios_dev_node_info(data);
271 pnpbios_print_status("dev_node_info", status);
276 * Note that some PnP BIOSes (e.g., on Sony Vaio laptops) die a horrible
277 * death if they are asked to access the "current" configuration.
278 * Therefore, if it's a matter of indifference, it's better to call
279 * get_dev_node() and set_dev_node() with boot=1 rather than with boot=0.
283 * Call PnP BIOS with function 0x01, "get system device node"
284 * Input: *nodenum = desired node,
285 * boot = whether to get nonvolatile boot (!=0)
286 * or volatile current (0) config
287 * Output: *nodenum=next node or 0xff if no more nodes
289 static int __pnp_bios_get_dev_node(u8 *nodenum, char boot,
290 struct pnp_bios_node *data)
295 if (!pnp_bios_present())
296 return PNP_FUNCTION_NOT_SUPPORTED;
297 if (!boot && pnpbios_dont_use_current_config)
298 return PNP_FUNCTION_NOT_SUPPORTED;
299 tmp_nodenum = *nodenum;
300 status = call_pnp_bios(PNP_GET_SYS_DEV_NODE, 0, PNP_TS1, 0, PNP_TS2,
301 boot ? 2 : 1, PNP_DS, 0, &tmp_nodenum,
302 sizeof(tmp_nodenum), data, 65536);
303 *nodenum = tmp_nodenum;
307 int pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
311 status = __pnp_bios_get_dev_node(nodenum, boot, data);
313 pnpbios_print_status("get_dev_node", status);
318 * Call PnP BIOS with function 0x02, "set system device node"
319 * Input: *nodenum = desired node,
320 * boot = whether to set nonvolatile boot (!=0)
321 * or volatile current (0) config
323 static int __pnp_bios_set_dev_node(u8 nodenum, char boot,
324 struct pnp_bios_node *data)
328 if (!pnp_bios_present())
329 return PNP_FUNCTION_NOT_SUPPORTED;
330 if (!boot && pnpbios_dont_use_current_config)
331 return PNP_FUNCTION_NOT_SUPPORTED;
332 status = call_pnp_bios(PNP_SET_SYS_DEV_NODE, nodenum, 0, PNP_TS1,
333 boot ? 2 : 1, PNP_DS, 0, 0, data, 65536, NULL,
338 int pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
342 status = __pnp_bios_set_dev_node(nodenum, boot, data);
344 pnpbios_print_status("set_dev_node", status);
347 if (!boot) { /* Update devlist */
348 status = pnp_bios_get_dev_node(&nodenum, boot, data);
356 * Call PnP BIOS with function 0x05, "get docking station information"
358 int pnp_bios_dock_station_info(struct pnp_docking_station_info *data)
362 if (!pnp_bios_present())
363 return PNP_FUNCTION_NOT_SUPPORTED;
364 status = call_pnp_bios(PNP_GET_DOCKING_STATION_INFORMATION, 0, PNP_TS1,
365 PNP_DS, 0, 0, 0, 0, data,
366 sizeof(struct pnp_docking_station_info), NULL,
372 * Call PnP BIOS with function 0x0a, "get statically allocated resource
375 static int __pnp_bios_get_stat_res(char *info)
379 if (!pnp_bios_present())
380 return PNP_FUNCTION_NOT_SUPPORTED;
381 status = call_pnp_bios(PNP_GET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1,
382 PNP_DS, 0, 0, 0, 0, info, 65536, NULL, 0);
386 int pnp_bios_get_stat_res(char *info)
390 status = __pnp_bios_get_stat_res(info);
392 pnpbios_print_status("get_stat_res", status);
397 * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
399 static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
403 if (!pnp_bios_present())
404 return PNP_FUNCTION_NOT_SUPPORTED;
405 status = call_pnp_bios(PNP_GET_PNP_ISA_CONFIG_STRUC, 0, PNP_TS1, PNP_DS,
407 sizeof(struct pnp_isa_config_struc), NULL, 0);
411 int pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
415 status = __pnp_bios_isapnp_config(data);
417 pnpbios_print_status("isapnp_config", status);
422 * Call PnP BIOS with function 0x41, "get ESCD info"
424 static int __pnp_bios_escd_info(struct escd_info_struc *data)
428 if (!pnp_bios_present())
429 return ESCD_FUNCTION_NOT_SUPPORTED;
430 status = call_pnp_bios(PNP_GET_ESCD_INFO, 0, PNP_TS1, 2, PNP_TS1, 4,
431 PNP_TS1, PNP_DS, data,
432 sizeof(struct escd_info_struc), NULL, 0);
436 int pnp_bios_escd_info(struct escd_info_struc *data)
440 status = __pnp_bios_escd_info(data);
442 pnpbios_print_status("escd_info", status);
447 * Call PnP BIOS function 0x42, "read ESCD"
448 * nvram_base is determined by calling escd_info
450 static int __pnp_bios_read_escd(char *data, u32 nvram_base)
454 if (!pnp_bios_present())
455 return ESCD_FUNCTION_NOT_SUPPORTED;
456 status = call_pnp_bios(PNP_READ_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0,
457 0, data, 65536, __va(nvram_base), 65536);
461 int pnp_bios_read_escd(char *data, u32 nvram_base)
465 status = __pnp_bios_read_escd(data, nvram_base);
467 pnpbios_print_status("read_escd", status);
471 void pnpbios_calls_init(union pnp_bios_install_struct *header)
475 spin_lock_init(&pnp_bios_lock);
476 pnp_bios_callpoint.offset = header->fields.pm16offset;
477 pnp_bios_callpoint.segment = PNP_CS16;
480 bad_bios_desc.b = 0x00409200;
482 set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
483 _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
484 for_each_possible_cpu(i) {
485 struct desc_struct *gdt = get_cpu_gdt_table(i);
488 set_base(gdt[GDT_ENTRY_PNPBIOS_CS32], &pnp_bios_callfunc);
489 set_base(gdt[GDT_ENTRY_PNPBIOS_CS16],
490 __va(header->fields.pm16cseg));
491 set_base(gdt[GDT_ENTRY_PNPBIOS_DS],
492 __va(header->fields.pm16dseg));