bioscalls.c 13 KB

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