bios.c 9.0 KB

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