bioscalls.c 14 KB

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