bios.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * From Coreboot file device/oprom/realmode/x86.c
  4. *
  5. * Copyright (C) 2007 Advanced Micro Devices, Inc.
  6. * Copyright (C) 2009-2010 coresystems GmbH
  7. */
  8. #include <common.h>
  9. #include <bios_emul.h>
  10. #include <vbe.h>
  11. #include <linux/linkage.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 *)((ulong)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. #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
  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 *)(ulong)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. #endif /* CONFIG_FRAMEBUFFER_SET_VESA_MODE */
  208. void bios_run_on_x86(struct udevice *dev, unsigned long addr, int vesa_mode,
  209. struct vbe_mode_info *mode_info)
  210. {
  211. pci_dev_t pcidev = dm_pci_get_bdf(dev);
  212. u32 num_dev;
  213. num_dev = PCI_BUS(pcidev) << 8 | PCI_DEV(pcidev) << 3 |
  214. PCI_FUNC(pcidev);
  215. /* Needed to avoid exceptions in some ROMs */
  216. interrupt_init();
  217. /* Set up some legacy information in the F segment */
  218. setup_rombios();
  219. /* Set up C interrupt handlers */
  220. setup_interrupt_handlers();
  221. /* Set up real-mode IDT */
  222. setup_realmode_idt();
  223. /* Make sure the code is placed. */
  224. setup_realmode_code();
  225. debug("Calling Option ROM at %lx, pci device %#x...", addr, num_dev);
  226. /* Option ROM entry point is at OPROM start + 3 */
  227. realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0,
  228. 0x0);
  229. debug("done\n");
  230. #ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
  231. if (vesa_mode != -1)
  232. vbe_set_graphics(vesa_mode, mode_info);
  233. #endif
  234. }
  235. asmlinkage int interrupt_handler(u32 intnumber, u32 gsfs, u32 dses,
  236. u32 edi, u32 esi, u32 ebp, u32 esp,
  237. u32 ebx, u32 edx, u32 ecx, u32 eax,
  238. u32 cs_ip, u16 stackflags)
  239. {
  240. u32 ip;
  241. u32 cs;
  242. u32 flags;
  243. int ret = 0;
  244. ip = cs_ip & 0xffff;
  245. cs = cs_ip >> 16;
  246. flags = stackflags;
  247. #ifdef CONFIG_REALMODE_DEBUG
  248. debug("oprom: INT# 0x%x\n", intnumber);
  249. debug("oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
  250. eax, ebx, ecx, edx);
  251. debug("oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
  252. ebp, esp, edi, esi);
  253. debug("oprom: ip: %04x cs: %04x flags: %08x\n",
  254. ip, cs, flags);
  255. debug("oprom: stackflags = %04x\n", stackflags);
  256. #endif
  257. /*
  258. * Fetch arguments from the stack and put them to a place
  259. * suitable for the interrupt handlers
  260. */
  261. M.x86.R_EAX = eax;
  262. M.x86.R_ECX = ecx;
  263. M.x86.R_EDX = edx;
  264. M.x86.R_EBX = ebx;
  265. M.x86.R_ESP = esp;
  266. M.x86.R_EBP = ebp;
  267. M.x86.R_ESI = esi;
  268. M.x86.R_EDI = edi;
  269. M.x86.intno = intnumber;
  270. M.x86.R_EIP = ip;
  271. M.x86.R_CS = cs;
  272. M.x86.R_EFLG = flags;
  273. /* Call the interrupt handler for this interrupt number */
  274. ret = int_handler[intnumber]();
  275. /*
  276. * This code is quite strange...
  277. *
  278. * Put registers back on the stack. The assembler code will pop them
  279. * later. We force (volatile!) changing the values of the parameters
  280. * of this function. We know that they stay alive on the stack after
  281. * we leave this function.
  282. */
  283. *(volatile u32 *)&eax = M.x86.R_EAX;
  284. *(volatile u32 *)&ecx = M.x86.R_ECX;
  285. *(volatile u32 *)&edx = M.x86.R_EDX;
  286. *(volatile u32 *)&ebx = M.x86.R_EBX;
  287. *(volatile u32 *)&esi = M.x86.R_ESI;
  288. *(volatile u32 *)&edi = M.x86.R_EDI;
  289. flags = M.x86.R_EFLG;
  290. /* Pass success or error back to our caller via the CARRY flag */
  291. if (ret) {
  292. flags &= ~1; /* no error: clear carry */
  293. } else {
  294. debug("int%02x call returned error\n", intnumber);
  295. flags |= 1; /* error: set carry */
  296. }
  297. *(volatile u16 *)&stackflags = flags;
  298. return ret;
  299. }