bioscalls.c 13 KB

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