bioscalls.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * bioscalls.c - the lowlevel layer of the PnPBIOS driver
  3. *
  4. */
  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/pnpbios.h>
  11. #include <linux/device.h>
  12. #include <linux/pnp.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/slab.h>
  16. #include <linux/kmod.h>
  17. #include <linux/completion.h>
  18. #include <linux/spinlock.h>
  19. #include <asm/page.h>
  20. #include <asm/desc.h>
  21. #include <asm/system.h>
  22. #include <asm/byteorder.h>
  23. #include "pnpbios.h"
  24. static struct {
  25. u16 offset;
  26. u16 segment;
  27. } pnp_bios_callpoint;
  28. /* The PnP BIOS entries in the GDT */
  29. #define PNP_GDT (GDT_ENTRY_PNPBIOS_BASE * 8)
  30. #define PNP_CS32 (PNP_GDT+0x00) /* segment for calling fn */
  31. #define PNP_CS16 (PNP_GDT+0x08) /* code segment for BIOS */
  32. #define PNP_DS (PNP_GDT+0x10) /* data segment for BIOS */
  33. #define PNP_TS1 (PNP_GDT+0x18) /* transfer data segment */
  34. #define PNP_TS2 (PNP_GDT+0x20) /* another data segment */
  35. /*
  36. * These are some opcodes for a "static asmlinkage"
  37. * As this code is *not* executed inside the linux kernel segment, but in a
  38. * alias at offset 0, we need a far return that can not be compiled by
  39. * default (please, prove me wrong! this is *really* ugly!)
  40. * This is the only way to get the bios to return into the kernel code,
  41. * because the bios code runs in 16 bit protected mode and therefore can only
  42. * return to the caller if the call is within the first 64kB, and the linux
  43. * kernel begins at offset 3GB...
  44. */
  45. asmlinkage void pnp_bios_callfunc(void);
  46. __asm__(
  47. ".text \n"
  48. __ALIGN_STR "\n"
  49. "pnp_bios_callfunc:\n"
  50. " pushl %edx \n"
  51. " pushl %ecx \n"
  52. " pushl %ebx \n"
  53. " pushl %eax \n"
  54. " lcallw *pnp_bios_callpoint\n"
  55. " addl $16, %esp \n"
  56. " lret \n"
  57. ".previous \n"
  58. );
  59. #define Q_SET_SEL(cpu, selname, address, size) \
  60. do { \
  61. struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
  62. set_base(gdt[(selname) >> 3], __va((u32)(address))); \
  63. set_limit(gdt[(selname) >> 3], size); \
  64. } while(0)
  65. #define Q2_SET_SEL(cpu, selname, address, size) \
  66. do { \
  67. struct desc_struct *gdt = get_cpu_gdt_table((cpu)); \
  68. set_base(gdt[(selname) >> 3], (u32)(address)); \
  69. set_limit(gdt[(selname) >> 3], size); \
  70. } while(0)
  71. static struct desc_struct bad_bios_desc = { 0, 0x00409200 };
  72. /*
  73. * At some point we want to use this stack frame pointer to unwind
  74. * after PnP BIOS oopses.
  75. */
  76. u32 pnp_bios_fault_esp;
  77. u32 pnp_bios_fault_eip;
  78. u32 pnp_bios_is_utter_crap = 0;
  79. static spinlock_t pnp_bios_lock;
  80. /*
  81. * Support Functions
  82. */
  83. static inline u16 call_pnp_bios(u16 func, u16 arg1, u16 arg2, u16 arg3,
  84. u16 arg4, u16 arg5, u16 arg6, u16 arg7,
  85. void *ts1_base, u32 ts1_size,
  86. void *ts2_base, u32 ts2_size)
  87. {
  88. unsigned long flags;
  89. u16 status;
  90. struct desc_struct save_desc_40;
  91. int cpu;
  92. /*
  93. * PnP BIOSes are generally not terribly re-entrant.
  94. * Also, don't rely on them to save everything correctly.
  95. */
  96. if(pnp_bios_is_utter_crap)
  97. return PNP_FUNCTION_NOT_SUPPORTED;
  98. cpu = get_cpu();
  99. save_desc_40 = get_cpu_gdt_table(cpu)[0x40 / 8];
  100. 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);
  103. /* The lock prevents us bouncing CPU here */
  104. if (ts1_size)
  105. Q2_SET_SEL(smp_processor_id(), PNP_TS1, ts1_base, ts1_size);
  106. if (ts2_size)
  107. Q2_SET_SEL(smp_processor_id(), PNP_TS2, ts2_base, ts2_size);
  108. __asm__ __volatile__(
  109. "pushl %%ebp\n\t"
  110. "pushl %%edi\n\t"
  111. "pushl %%esi\n\t"
  112. "pushl %%ds\n\t"
  113. "pushl %%es\n\t"
  114. "pushl %%fs\n\t"
  115. "pushl %%gs\n\t"
  116. "pushfl\n\t"
  117. "movl %%esp, pnp_bios_fault_esp\n\t"
  118. "movl $1f, pnp_bios_fault_eip\n\t"
  119. "lcall %5,%6\n\t"
  120. "1:popfl\n\t"
  121. "popl %%gs\n\t"
  122. "popl %%fs\n\t"
  123. "popl %%es\n\t"
  124. "popl %%ds\n\t"
  125. "popl %%esi\n\t"
  126. "popl %%edi\n\t"
  127. "popl %%ebp\n\t"
  128. : "=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),
  134. "i" (0)
  135. : "memory"
  136. );
  137. spin_unlock_irqrestore(&pnp_bios_lock, flags);
  138. get_cpu_gdt_table(cpu)[0x40 / 8] = save_desc_40;
  139. put_cpu();
  140. /* If we get here and this is set then the PnP BIOS faulted on us. */
  141. if(pnp_bios_is_utter_crap)
  142. {
  143. printk(KERN_ERR "PnPBIOS: Warning! Your PnP BIOS caused a fatal error. Attempting to continue\n");
  144. printk(KERN_ERR "PnPBIOS: You may need to reboot with the \"pnpbios=off\" option to operate stably\n");
  145. printk(KERN_ERR "PnPBIOS: Check with your vendor for an updated BIOS\n");
  146. }
  147. return status;
  148. }
  149. void pnpbios_print_status(const char * module, u16 status)
  150. {
  151. switch(status) {
  152. case PNP_SUCCESS:
  153. printk(KERN_ERR "PnPBIOS: %s: function successful\n", module);
  154. break;
  155. case PNP_NOT_SET_STATICALLY:
  156. printk(KERN_ERR "PnPBIOS: %s: unable to set static resources\n", module);
  157. break;
  158. case PNP_UNKNOWN_FUNCTION:
  159. printk(KERN_ERR "PnPBIOS: %s: invalid function number passed\n", module);
  160. break;
  161. case PNP_FUNCTION_NOT_SUPPORTED:
  162. printk(KERN_ERR "PnPBIOS: %s: function not supported on this system\n", module);
  163. break;
  164. case PNP_INVALID_HANDLE:
  165. printk(KERN_ERR "PnPBIOS: %s: invalid handle\n", module);
  166. break;
  167. case PNP_BAD_PARAMETER:
  168. printk(KERN_ERR "PnPBIOS: %s: invalid parameters were passed\n", module);
  169. break;
  170. case PNP_SET_FAILED:
  171. printk(KERN_ERR "PnPBIOS: %s: unable to set resources\n", module);
  172. break;
  173. case PNP_EVENTS_NOT_PENDING:
  174. printk(KERN_ERR "PnPBIOS: %s: no events are pending\n", module);
  175. break;
  176. case PNP_SYSTEM_NOT_DOCKED:
  177. printk(KERN_ERR "PnPBIOS: %s: the system is not docked\n", module);
  178. break;
  179. case PNP_NO_ISA_PNP_CARDS:
  180. printk(KERN_ERR "PnPBIOS: %s: no isapnp cards are installed on this system\n", module);
  181. break;
  182. case PNP_UNABLE_TO_DETERMINE_DOCK_CAPABILITIES:
  183. printk(KERN_ERR "PnPBIOS: %s: cannot determine the capabilities of the docking station\n", module);
  184. break;
  185. case PNP_CONFIG_CHANGE_FAILED_NO_BATTERY:
  186. printk(KERN_ERR "PnPBIOS: %s: unable to undock, the system does not have a battery\n", module);
  187. break;
  188. case PNP_CONFIG_CHANGE_FAILED_RESOURCE_CONFLICT:
  189. printk(KERN_ERR "PnPBIOS: %s: could not dock due to resource conflicts\n", module);
  190. break;
  191. case PNP_BUFFER_TOO_SMALL:
  192. printk(KERN_ERR "PnPBIOS: %s: the buffer passed is too small\n", module);
  193. break;
  194. case PNP_USE_ESCD_SUPPORT:
  195. printk(KERN_ERR "PnPBIOS: %s: use ESCD instead\n", module);
  196. break;
  197. case PNP_MESSAGE_NOT_SUPPORTED:
  198. printk(KERN_ERR "PnPBIOS: %s: the message is unsupported\n", module);
  199. break;
  200. case PNP_HARDWARE_ERROR:
  201. printk(KERN_ERR "PnPBIOS: %s: a hardware failure has occured\n", module);
  202. break;
  203. default:
  204. printk(KERN_ERR "PnPBIOS: %s: unexpected status 0x%x\n", module, status);
  205. break;
  206. }
  207. }
  208. /*
  209. * PnP BIOS Low Level Calls
  210. */
  211. #define PNP_GET_NUM_SYS_DEV_NODES 0x00
  212. #define PNP_GET_SYS_DEV_NODE 0x01
  213. #define PNP_SET_SYS_DEV_NODE 0x02
  214. #define PNP_GET_EVENT 0x03
  215. #define PNP_SEND_MESSAGE 0x04
  216. #define PNP_GET_DOCKING_STATION_INFORMATION 0x05
  217. #define PNP_SET_STATIC_ALLOCED_RES_INFO 0x09
  218. #define PNP_GET_STATIC_ALLOCED_RES_INFO 0x0a
  219. #define PNP_GET_APM_ID_TABLE 0x0b
  220. #define PNP_GET_PNP_ISA_CONFIG_STRUC 0x40
  221. #define PNP_GET_ESCD_INFO 0x41
  222. #define PNP_READ_ESCD 0x42
  223. #define PNP_WRITE_ESCD 0x43
  224. /*
  225. * Call PnP BIOS with function 0x00, "get number of system device nodes"
  226. */
  227. static int __pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
  228. {
  229. u16 status;
  230. if (!pnp_bios_present())
  231. return PNP_FUNCTION_NOT_SUPPORTED;
  232. status = call_pnp_bios(PNP_GET_NUM_SYS_DEV_NODES, 0, PNP_TS1, 2, PNP_TS1, PNP_DS, 0, 0,
  233. data, sizeof(struct pnp_dev_node_info), NULL, 0);
  234. data->no_nodes &= 0xff;
  235. return status;
  236. }
  237. int pnp_bios_dev_node_info(struct pnp_dev_node_info *data)
  238. {
  239. int status = __pnp_bios_dev_node_info( data );
  240. if ( status )
  241. pnpbios_print_status( "dev_node_info", status );
  242. return status;
  243. }
  244. /*
  245. * Note that some PnP BIOSes (e.g., on Sony Vaio laptops) die a horrible
  246. * death if they are asked to access the "current" configuration.
  247. * Therefore, if it's a matter of indifference, it's better to call
  248. * get_dev_node() and set_dev_node() with boot=1 rather than with boot=0.
  249. */
  250. /*
  251. * Call PnP BIOS with function 0x01, "get system device node"
  252. * Input: *nodenum = desired node,
  253. * boot = whether to get nonvolatile boot (!=0)
  254. * or volatile current (0) config
  255. * Output: *nodenum=next node or 0xff if no more nodes
  256. */
  257. static int __pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
  258. {
  259. u16 status;
  260. if (!pnp_bios_present())
  261. return PNP_FUNCTION_NOT_SUPPORTED;
  262. if ( !boot && pnpbios_dont_use_current_config )
  263. return PNP_FUNCTION_NOT_SUPPORTED;
  264. status = call_pnp_bios(PNP_GET_SYS_DEV_NODE, 0, PNP_TS1, 0, PNP_TS2, boot ? 2 : 1, PNP_DS, 0,
  265. nodenum, sizeof(char), data, 65536);
  266. return status;
  267. }
  268. int pnp_bios_get_dev_node(u8 *nodenum, char boot, struct pnp_bios_node *data)
  269. {
  270. int status;
  271. status = __pnp_bios_get_dev_node( nodenum, boot, data );
  272. if ( status )
  273. pnpbios_print_status( "get_dev_node", status );
  274. return status;
  275. }
  276. /*
  277. * Call PnP BIOS with function 0x02, "set system device node"
  278. * Input: *nodenum = desired node,
  279. * boot = whether to set nonvolatile boot (!=0)
  280. * or volatile current (0) config
  281. */
  282. static int __pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
  283. {
  284. u16 status;
  285. if (!pnp_bios_present())
  286. return PNP_FUNCTION_NOT_SUPPORTED;
  287. if ( !boot && pnpbios_dont_use_current_config )
  288. return PNP_FUNCTION_NOT_SUPPORTED;
  289. status = call_pnp_bios(PNP_SET_SYS_DEV_NODE, nodenum, 0, PNP_TS1, boot ? 2 : 1, PNP_DS, 0, 0,
  290. data, 65536, NULL, 0);
  291. return status;
  292. }
  293. int pnp_bios_set_dev_node(u8 nodenum, char boot, struct pnp_bios_node *data)
  294. {
  295. int status;
  296. status = __pnp_bios_set_dev_node( nodenum, boot, data );
  297. if ( status ) {
  298. pnpbios_print_status( "set_dev_node", status );
  299. return status;
  300. }
  301. if ( !boot ) { /* Update devlist */
  302. status = pnp_bios_get_dev_node( &nodenum, boot, data );
  303. if ( status )
  304. return status;
  305. }
  306. return status;
  307. }
  308. #if needed
  309. /*
  310. * Call PnP BIOS with function 0x03, "get event"
  311. */
  312. static int pnp_bios_get_event(u16 *event)
  313. {
  314. u16 status;
  315. if (!pnp_bios_present())
  316. return PNP_FUNCTION_NOT_SUPPORTED;
  317. status = call_pnp_bios(PNP_GET_EVENT, 0, PNP_TS1, PNP_DS, 0, 0 ,0 ,0,
  318. event, sizeof(u16), NULL, 0);
  319. return status;
  320. }
  321. #endif
  322. #if needed
  323. /*
  324. * Call PnP BIOS with function 0x04, "send message"
  325. */
  326. static int pnp_bios_send_message(u16 message)
  327. {
  328. u16 status;
  329. if (!pnp_bios_present())
  330. return PNP_FUNCTION_NOT_SUPPORTED;
  331. status = call_pnp_bios(PNP_SEND_MESSAGE, message, PNP_DS, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  332. return status;
  333. }
  334. #endif
  335. /*
  336. * Call PnP BIOS with function 0x05, "get docking station information"
  337. */
  338. int pnp_bios_dock_station_info(struct pnp_docking_station_info *data)
  339. {
  340. u16 status;
  341. if (!pnp_bios_present())
  342. return PNP_FUNCTION_NOT_SUPPORTED;
  343. status = call_pnp_bios(PNP_GET_DOCKING_STATION_INFORMATION, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
  344. data, sizeof(struct pnp_docking_station_info), NULL, 0);
  345. return status;
  346. }
  347. #if needed
  348. /*
  349. * Call PnP BIOS with function 0x09, "set statically allocated resource
  350. * information"
  351. */
  352. static int pnp_bios_set_stat_res(char *info)
  353. {
  354. u16 status;
  355. if (!pnp_bios_present())
  356. return PNP_FUNCTION_NOT_SUPPORTED;
  357. status = call_pnp_bios(PNP_SET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
  358. info, *((u16 *) info), 0, 0);
  359. return status;
  360. }
  361. #endif
  362. /*
  363. * Call PnP BIOS with function 0x0a, "get statically allocated resource
  364. * information"
  365. */
  366. static int __pnp_bios_get_stat_res(char *info)
  367. {
  368. u16 status;
  369. if (!pnp_bios_present())
  370. return PNP_FUNCTION_NOT_SUPPORTED;
  371. status = call_pnp_bios(PNP_GET_STATIC_ALLOCED_RES_INFO, 0, PNP_TS1, PNP_DS, 0, 0, 0, 0,
  372. info, 65536, NULL, 0);
  373. return status;
  374. }
  375. int pnp_bios_get_stat_res(char *info)
  376. {
  377. int status;
  378. status = __pnp_bios_get_stat_res( info );
  379. if ( status )
  380. pnpbios_print_status( "get_stat_res", status );
  381. return status;
  382. }
  383. #if needed
  384. /*
  385. * Call PnP BIOS with function 0x0b, "get APM id table"
  386. */
  387. static int pnp_bios_apm_id_table(char *table, u16 *size)
  388. {
  389. u16 status;
  390. if (!pnp_bios_present())
  391. return PNP_FUNCTION_NOT_SUPPORTED;
  392. status = call_pnp_bios(PNP_GET_APM_ID_TABLE, 0, PNP_TS2, 0, PNP_TS1, PNP_DS, 0, 0,
  393. table, *size, size, sizeof(u16));
  394. return status;
  395. }
  396. #endif
  397. /*
  398. * Call PnP BIOS with function 0x40, "get isa pnp configuration structure"
  399. */
  400. static int __pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
  401. {
  402. u16 status;
  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, 0, 0, 0, 0,
  406. data, sizeof(struct pnp_isa_config_struc), NULL, 0);
  407. return status;
  408. }
  409. int pnp_bios_isapnp_config(struct pnp_isa_config_struc *data)
  410. {
  411. int status;
  412. status = __pnp_bios_isapnp_config( data );
  413. if ( status )
  414. pnpbios_print_status( "isapnp_config", status );
  415. return status;
  416. }
  417. /*
  418. * Call PnP BIOS with function 0x41, "get ESCD info"
  419. */
  420. static int __pnp_bios_escd_info(struct escd_info_struc *data)
  421. {
  422. u16 status;
  423. if (!pnp_bios_present())
  424. return ESCD_FUNCTION_NOT_SUPPORTED;
  425. status = call_pnp_bios(PNP_GET_ESCD_INFO, 0, PNP_TS1, 2, PNP_TS1, 4, PNP_TS1, PNP_DS,
  426. data, sizeof(struct escd_info_struc), NULL, 0);
  427. return status;
  428. }
  429. int pnp_bios_escd_info(struct escd_info_struc *data)
  430. {
  431. int status;
  432. status = __pnp_bios_escd_info( data );
  433. if ( status )
  434. pnpbios_print_status( "escd_info", status );
  435. return status;
  436. }
  437. /*
  438. * Call PnP BIOS function 0x42, "read ESCD"
  439. * nvram_base is determined by calling escd_info
  440. */
  441. static int __pnp_bios_read_escd(char *data, u32 nvram_base)
  442. {
  443. u16 status;
  444. if (!pnp_bios_present())
  445. return ESCD_FUNCTION_NOT_SUPPORTED;
  446. status = call_pnp_bios(PNP_READ_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
  447. data, 65536, __va(nvram_base), 65536);
  448. return status;
  449. }
  450. int pnp_bios_read_escd(char *data, u32 nvram_base)
  451. {
  452. int status;
  453. status = __pnp_bios_read_escd( data, nvram_base );
  454. if ( status )
  455. pnpbios_print_status( "read_escd", status );
  456. return status;
  457. }
  458. #if needed
  459. /*
  460. * Call PnP BIOS function 0x43, "write ESCD"
  461. */
  462. static int pnp_bios_write_escd(char *data, u32 nvram_base)
  463. {
  464. u16 status;
  465. if (!pnp_bios_present())
  466. return ESCD_FUNCTION_NOT_SUPPORTED;
  467. status = call_pnp_bios(PNP_WRITE_ESCD, 0, PNP_TS1, PNP_TS2, PNP_DS, 0, 0, 0,
  468. data, 65536, __va(nvram_base), 65536);
  469. return status;
  470. }
  471. #endif
  472. /*
  473. * Initialization
  474. */
  475. void pnpbios_calls_init(union pnp_bios_install_struct *header)
  476. {
  477. int i;
  478. spin_lock_init(&pnp_bios_lock);
  479. pnp_bios_callpoint.offset = header->fields.pm16offset;
  480. pnp_bios_callpoint.segment = PNP_CS16;
  481. set_base(bad_bios_desc, __va((unsigned long)0x40 << 4));
  482. _set_limit((char *)&bad_bios_desc, 4095 - (0x40 << 4));
  483. for (i = 0; i < NR_CPUS; i++) {
  484. struct desc_struct *gdt = get_cpu_gdt_table(i);
  485. if (!gdt)
  486. continue;
  487. Q2_SET_SEL(i, PNP_CS32, &pnp_bios_callfunc, 64 * 1024);
  488. Q_SET_SEL(i, PNP_CS16, header->fields.pm16cseg, 64 * 1024);
  489. Q_SET_SEL(i, PNP_DS, header->fields.pm16dseg, 64 * 1024);
  490. }
  491. }