bioscalls.c 15 KB

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