bios.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * From Coreboot file device/oprom/realmode/x86.c
  3. *
  4. * Copyright (C) 2007 Advanced Micro Devices, Inc.
  5. * Copyright (C) 2009-2010 coresystems GmbH
  6. *
  7. * SPDX-License-Identifier: GPL-2.0
  8. */
  9. #include <common.h>
  10. #include <bios_emul.h>
  11. #include <vbe.h>
  12. #include <asm/cache.h>
  13. #include <asm/processor.h>
  14. #include <asm/i8259.h>
  15. #include <asm/io.h>
  16. #include <asm/post.h>
  17. #include "bios.h"
  18. /* Interrupt handlers for each interrupt the ROM can call */
  19. static int (*int_handler[256])(void);
  20. /* to have a common register file for interrupt handlers */
  21. X86EMU_sysEnv _X86EMU_env;
  22. asmlinkage void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
  23. u32 esi, u32 edi);
  24. asmlinkage void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
  25. u32 edx, u32 esi, u32 edi);
  26. static void setup_realmode_code(void)
  27. {
  28. memcpy((void *)REALMODE_BASE, &asm_realmode_code,
  29. asm_realmode_code_size);
  30. /* Ensure the global pointers are relocated properly. */
  31. realmode_call = PTR_TO_REAL_MODE(asm_realmode_call);
  32. realmode_interrupt = PTR_TO_REAL_MODE(__realmode_interrupt);
  33. debug("Real mode stub @%x: %d bytes\n", REALMODE_BASE,
  34. asm_realmode_code_size);
  35. }
  36. static void setup_rombios(void)
  37. {
  38. const char date[] = "06/11/99";
  39. memcpy((void *)0xffff5, &date, 8);
  40. const char ident[] = "PCI_ISA";
  41. memcpy((void *)0xfffd9, &ident, 7);
  42. /* system model: IBM-AT */
  43. writeb(0xfc, 0xffffe);
  44. }
  45. static int int_exception_handler(void)
  46. {
  47. /* compatibility shim */
  48. struct eregs reg_info = {
  49. .eax = M.x86.R_EAX,
  50. .ecx = M.x86.R_ECX,
  51. .edx = M.x86.R_EDX,
  52. .ebx = M.x86.R_EBX,
  53. .esp = M.x86.R_ESP,
  54. .ebp = M.x86.R_EBP,
  55. .esi = M.x86.R_ESI,
  56. .edi = M.x86.R_EDI,
  57. .vector = M.x86.intno,
  58. .error_code = 0,
  59. .eip = M.x86.R_EIP,
  60. .cs = M.x86.R_CS,
  61. .eflags = M.x86.R_EFLG
  62. };
  63. struct eregs *regs = &reg_info;
  64. debug("Oops, exception %d while executing option rom\n", regs->vector);
  65. cpu_hlt();
  66. return 0;
  67. }
  68. static int int_unknown_handler(void)
  69. {
  70. debug("Unsupported software interrupt #0x%x eax 0x%x\n",
  71. M.x86.intno, M.x86.R_EAX);
  72. return -1;
  73. }
  74. /* setup interrupt handlers for mainboard */
  75. void bios_set_interrupt_handler(int intnum, int (*int_func)(void))
  76. {
  77. int_handler[intnum] = int_func;
  78. }
  79. static void setup_interrupt_handlers(void)
  80. {
  81. int i;
  82. /*
  83. * The first 16 int_handler functions are not BIOS services,
  84. * but the CPU-generated exceptions ("hardware interrupts")
  85. */
  86. for (i = 0; i < 0x10; i++)
  87. int_handler[i] = &int_exception_handler;
  88. /* Mark all other int_handler calls as unknown first */
  89. for (i = 0x10; i < 0x100; i++) {
  90. /* Skip if bios_set_interrupt_handler() isn't called first */
  91. if (int_handler[i])
  92. continue;
  93. /*
  94. * Now set the default functions that are actually needed
  95. * to initialize the option roms. The board may override
  96. * these with bios_set_interrupt_handler()
  97. */
  98. switch (i) {
  99. case 0x10:
  100. int_handler[0x10] = &int10_handler;
  101. break;
  102. case 0x12:
  103. int_handler[0x12] = &int12_handler;
  104. break;
  105. case 0x16:
  106. int_handler[0x16] = &int16_handler;
  107. break;
  108. case 0x1a:
  109. int_handler[0x1a] = &int1a_handler;
  110. break;
  111. default:
  112. int_handler[i] = &int_unknown_handler;
  113. break;
  114. }
  115. }
  116. }
  117. static void write_idt_stub(void *target, u8 intnum)
  118. {
  119. unsigned char *codeptr;
  120. codeptr = (unsigned char *)target;
  121. memcpy(codeptr, &__idt_handler, __idt_handler_size);
  122. codeptr[3] = intnum; /* modify int# in the code stub. */
  123. }
  124. static void setup_realmode_idt(void)
  125. {
  126. struct realmode_idt *idts = NULL;
  127. int i;
  128. /*
  129. * Copy IDT stub code for each interrupt. This might seem wasteful
  130. * but it is really simple
  131. */
  132. for (i = 0; i < 256; i++) {
  133. idts[i].cs = 0;
  134. idts[i].offset = 0x1000 + (i * __idt_handler_size);
  135. write_idt_stub((void *)((u32)idts[i].offset), i);
  136. }
  137. /*
  138. * Many option ROMs use the hard coded interrupt entry points in the
  139. * system bios. So install them at the known locations.
  140. */
  141. /* int42 is the relocated int10 */
  142. write_idt_stub((void *)0xff065, 0x42);
  143. /* BIOS Int 11 Handler F000:F84D */
  144. write_idt_stub((void *)0xff84d, 0x11);
  145. /* BIOS Int 12 Handler F000:F841 */
  146. write_idt_stub((void *)0xff841, 0x12);
  147. /* BIOS Int 13 Handler F000:EC59 */
  148. write_idt_stub((void *)0xfec59, 0x13);
  149. /* BIOS Int 14 Handler F000:E739 */
  150. write_idt_stub((void *)0xfe739, 0x14);
  151. /* BIOS Int 15 Handler F000:F859 */
  152. write_idt_stub((void *)0xff859, 0x15);
  153. /* BIOS Int 16 Handler F000:E82E */
  154. write_idt_stub((void *)0xfe82e, 0x16);
  155. /* BIOS Int 17 Handler F000:EFD2 */
  156. write_idt_stub((void *)0xfefd2, 0x17);
  157. /* ROM BIOS Int 1A Handler F000:FE6E */
  158. write_idt_stub((void *)0xffe6e, 0x1a);
  159. }
  160. static u8 vbe_get_mode_info(struct vbe_mode_info *mi)
  161. {
  162. u16 buffer_seg;
  163. u16 buffer_adr;
  164. char *buffer;
  165. debug("VBE: Getting information about VESA mode %04x\n",
  166. mi->video_mode);
  167. buffer = PTR_TO_REAL_MODE(asm_realmode_buffer);
  168. buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
  169. buffer_adr = ((unsigned long)buffer) & 0xffff;
  170. realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000, mi->video_mode,
  171. 0x0000, buffer_seg, buffer_adr);
  172. memcpy(mi->mode_info_block, buffer, sizeof(struct vbe_mode_info));
  173. mi->valid = true;
  174. return 0;
  175. }
  176. static u8 vbe_set_mode(struct vbe_mode_info *mi)
  177. {
  178. debug("VBE: Setting VESA mode %#04x\n", mi->video_mode);
  179. /* request linear framebuffer mode */
  180. mi->video_mode |= (1 << 14);
  181. /* request clearing of framebuffer */
  182. mi->video_mode &= ~(1 << 15);
  183. realmode_interrupt(0x10, VESA_SET_MODE, mi->video_mode,
  184. 0x0000, 0x0000, 0x0000, 0x0000);
  185. return 0;
  186. }
  187. static void vbe_set_graphics(int vesa_mode, struct vbe_mode_info *mode_info)
  188. {
  189. unsigned char *framebuffer;
  190. mode_info->video_mode = (1 << 14) | vesa_mode;
  191. vbe_get_mode_info(mode_info);
  192. framebuffer = (unsigned char *)mode_info->vesa.phys_base_ptr;
  193. debug("VBE: resolution: %dx%d@%d\n",
  194. le16_to_cpu(mode_info->vesa.x_resolution),
  195. le16_to_cpu(mode_info->vesa.y_resolution),
  196. mode_info->vesa.bits_per_pixel);
  197. debug("VBE: framebuffer: %p\n", framebuffer);
  198. if (!framebuffer) {
  199. debug("VBE: Mode does not support linear framebuffer\n");
  200. return;
  201. }
  202. vbe_set_mode(mode_info);
  203. }
  204. void bios_run_on_x86(pci_dev_t pcidev, unsigned long addr, int vesa_mode,
  205. struct vbe_mode_info *mode_info)
  206. {
  207. u32 num_dev;
  208. num_dev = PCI_BUS(pcidev) << 8 | PCI_DEV(pcidev) << 3 |
  209. PCI_FUNC(pcidev);
  210. /* Needed to avoid exceptions in some ROMs */
  211. interrupt_init();
  212. /* Set up some legacy information in the F segment */
  213. setup_rombios();
  214. /* Set up C interrupt handlers */
  215. setup_interrupt_handlers();
  216. /* Set up real-mode IDT */
  217. setup_realmode_idt();
  218. /* Make sure the code is placed. */
  219. setup_realmode_code();
  220. disable_caches();
  221. debug("Calling Option ROM at %lx, pci device %#x...", addr, num_dev);
  222. /* Option ROM entry point is at OPROM start + 3 */
  223. realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0,
  224. 0x0);
  225. debug("done\n");
  226. if (vesa_mode != -1)
  227. vbe_set_graphics(vesa_mode, mode_info);
  228. }
  229. asmlinkage int interrupt_handler(u32 intnumber, u32 gsfs, u32 dses,
  230. u32 edi, u32 esi, u32 ebp, u32 esp,
  231. u32 ebx, u32 edx, u32 ecx, u32 eax,
  232. u32 cs_ip, u16 stackflags)
  233. {
  234. u32 ip;
  235. u32 cs;
  236. u32 flags;
  237. int ret = 0;
  238. ip = cs_ip & 0xffff;
  239. cs = cs_ip >> 16;
  240. flags = stackflags;
  241. #ifdef CONFIG_REALMODE_DEBUG
  242. debug("oprom: INT# 0x%x\n", intnumber);
  243. debug("oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
  244. eax, ebx, ecx, edx);
  245. debug("oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
  246. ebp, esp, edi, esi);
  247. debug("oprom: ip: %04x cs: %04x flags: %08x\n",
  248. ip, cs, flags);
  249. debug("oprom: stackflags = %04x\n", stackflags);
  250. #endif
  251. /*
  252. * Fetch arguments from the stack and put them to a place
  253. * suitable for the interrupt handlers
  254. */
  255. M.x86.R_EAX = eax;
  256. M.x86.R_ECX = ecx;
  257. M.x86.R_EDX = edx;
  258. M.x86.R_EBX = ebx;
  259. M.x86.R_ESP = esp;
  260. M.x86.R_EBP = ebp;
  261. M.x86.R_ESI = esi;
  262. M.x86.R_EDI = edi;
  263. M.x86.intno = intnumber;
  264. M.x86.R_EIP = ip;
  265. M.x86.R_CS = cs;
  266. M.x86.R_EFLG = flags;
  267. /* Call the interrupt handler for this interrupt number */
  268. ret = int_handler[intnumber]();
  269. /*
  270. * This code is quite strange...
  271. *
  272. * Put registers back on the stack. The assembler code will pop them
  273. * later. We force (volatile!) changing the values of the parameters
  274. * of this function. We know that they stay alive on the stack after
  275. * we leave this function.
  276. */
  277. *(volatile u32 *)&eax = M.x86.R_EAX;
  278. *(volatile u32 *)&ecx = M.x86.R_ECX;
  279. *(volatile u32 *)&edx = M.x86.R_EDX;
  280. *(volatile u32 *)&ebx = M.x86.R_EBX;
  281. *(volatile u32 *)&esi = M.x86.R_ESI;
  282. *(volatile u32 *)&edi = M.x86.R_EDI;
  283. flags = M.x86.R_EFLG;
  284. /* Pass success or error back to our caller via the CARRY flag */
  285. if (ret) {
  286. flags &= ~1; /* no error: clear carry */
  287. } else {
  288. debug("int%02x call returned error\n", intnumber);
  289. flags |= 1; /* error: set carry */
  290. }
  291. *(volatile u16 *)&stackflags = flags;
  292. return ret;
  293. }