bios.c 8.8 KB

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