blob: 03f7360032c6d496c05640bfdd315c9ac81f3630 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glassb2978d32014-11-14 20:56:32 -07002/*
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
Simon Glassb2978d32014-11-14 20:56:32 -07007 */
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <compiler.h>
Simon Glassb2978d32014-11-14 20:56:32 -07009#include <bios_emul.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070010#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassec86bc62022-07-30 15:52:04 -060012#include <vesa.h>
Masahiro Yamadad0506f72014-12-03 17:36:57 +090013#include <linux/linkage.h>
Simon Glassb2978d32014-11-14 20:56:32 -070014#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
21/* Interrupt handlers for each interrupt the ROM can call */
22static int (*int_handler[256])(void);
23
24/* to have a common register file for interrupt handlers */
Simon Glassee95ec12023-07-15 21:38:58 -060025#if !CONFIG_IS_ENABLED(BIOSEMU)
Simon Glassb2978d32014-11-14 20:56:32 -070026X86EMU_sysEnv _X86EMU_env;
Bin Meng98857212021-07-07 15:36:26 +080027#endif
Simon Glassb2978d32014-11-14 20:56:32 -070028
29asmlinkage void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
30 u32 esi, u32 edi);
31
32asmlinkage void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
33 u32 edx, u32 esi, u32 edi);
34
35static void setup_realmode_code(void)
36{
37 memcpy((void *)REALMODE_BASE, &asm_realmode_code,
38 asm_realmode_code_size);
39
40 /* Ensure the global pointers are relocated properly. */
41 realmode_call = PTR_TO_REAL_MODE(asm_realmode_call);
42 realmode_interrupt = PTR_TO_REAL_MODE(__realmode_interrupt);
43
44 debug("Real mode stub @%x: %d bytes\n", REALMODE_BASE,
45 asm_realmode_code_size);
46}
47
48static void setup_rombios(void)
49{
50 const char date[] = "06/11/99";
51 memcpy((void *)0xffff5, &date, 8);
52
53 const char ident[] = "PCI_ISA";
54 memcpy((void *)0xfffd9, &ident, 7);
55
56 /* system model: IBM-AT */
57 writeb(0xfc, 0xffffe);
58}
59
60static int int_exception_handler(void)
61{
62 /* compatibility shim */
63 struct eregs reg_info = {
64 .eax = M.x86.R_EAX,
65 .ecx = M.x86.R_ECX,
66 .edx = M.x86.R_EDX,
67 .ebx = M.x86.R_EBX,
68 .esp = M.x86.R_ESP,
69 .ebp = M.x86.R_EBP,
70 .esi = M.x86.R_ESI,
71 .edi = M.x86.R_EDI,
72 .vector = M.x86.intno,
73 .error_code = 0,
74 .eip = M.x86.R_EIP,
75 .cs = M.x86.R_CS,
76 .eflags = M.x86.R_EFLG
77 };
78 struct eregs *regs = &reg_info;
79
Simon Glass13e3e3e2023-07-15 21:38:44 -060080 log_err("Exception %d while executing option rom\n", regs->vector);
Simon Glassb2978d32014-11-14 20:56:32 -070081 cpu_hlt();
82
83 return 0;
84}
85
86static int int_unknown_handler(void)
87{
88 debug("Unsupported software interrupt #0x%x eax 0x%x\n",
89 M.x86.intno, M.x86.R_EAX);
90
91 return -1;
92}
93
94/* setup interrupt handlers for mainboard */
95void bios_set_interrupt_handler(int intnum, int (*int_func)(void))
96{
97 int_handler[intnum] = int_func;
98}
99
100static void setup_interrupt_handlers(void)
101{
102 int i;
103
104 /*
105 * The first 16 int_handler functions are not BIOS services,
106 * but the CPU-generated exceptions ("hardware interrupts")
107 */
108 for (i = 0; i < 0x10; i++)
109 int_handler[i] = &int_exception_handler;
110
111 /* Mark all other int_handler calls as unknown first */
112 for (i = 0x10; i < 0x100; i++) {
113 /* Skip if bios_set_interrupt_handler() isn't called first */
114 if (int_handler[i])
115 continue;
116
117 /*
118 * Now set the default functions that are actually needed
119 * to initialize the option roms. The board may override
120 * these with bios_set_interrupt_handler()
121 */
122 switch (i) {
123 case 0x10:
124 int_handler[0x10] = &int10_handler;
125 break;
126 case 0x12:
127 int_handler[0x12] = &int12_handler;
128 break;
129 case 0x16:
130 int_handler[0x16] = &int16_handler;
131 break;
132 case 0x1a:
133 int_handler[0x1a] = &int1a_handler;
134 break;
135 default:
136 int_handler[i] = &int_unknown_handler;
137 break;
138 }
139 }
140}
141
142static void write_idt_stub(void *target, u8 intnum)
143{
144 unsigned char *codeptr;
145
146 codeptr = (unsigned char *)target;
147 memcpy(codeptr, &__idt_handler, __idt_handler_size);
148 codeptr[3] = intnum; /* modify int# in the code stub. */
149}
150
151static void setup_realmode_idt(void)
152{
153 struct realmode_idt *idts = NULL;
154 int i;
155
156 /*
157 * Copy IDT stub code for each interrupt. This might seem wasteful
158 * but it is really simple
159 */
160 for (i = 0; i < 256; i++) {
161 idts[i].cs = 0;
162 idts[i].offset = 0x1000 + (i * __idt_handler_size);
Simon Glassaa0ed9d2017-01-16 07:03:42 -0700163 write_idt_stub((void *)((ulong)idts[i].offset), i);
Simon Glassb2978d32014-11-14 20:56:32 -0700164 }
165
166 /*
167 * Many option ROMs use the hard coded interrupt entry points in the
168 * system bios. So install them at the known locations.
169 */
170
171 /* int42 is the relocated int10 */
172 write_idt_stub((void *)0xff065, 0x42);
173 /* BIOS Int 11 Handler F000:F84D */
174 write_idt_stub((void *)0xff84d, 0x11);
175 /* BIOS Int 12 Handler F000:F841 */
176 write_idt_stub((void *)0xff841, 0x12);
177 /* BIOS Int 13 Handler F000:EC59 */
178 write_idt_stub((void *)0xfec59, 0x13);
179 /* BIOS Int 14 Handler F000:E739 */
180 write_idt_stub((void *)0xfe739, 0x14);
181 /* BIOS Int 15 Handler F000:F859 */
182 write_idt_stub((void *)0xff859, 0x15);
183 /* BIOS Int 16 Handler F000:E82E */
184 write_idt_stub((void *)0xfe82e, 0x16);
185 /* BIOS Int 17 Handler F000:EFD2 */
186 write_idt_stub((void *)0xfefd2, 0x17);
187 /* ROM BIOS Int 1A Handler F000:FE6E */
188 write_idt_stub((void *)0xffe6e, 0x1a);
189}
190
Bin Mengc9dba412018-04-11 22:02:15 -0700191#ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
Simon Glass5b925202022-07-30 15:52:05 -0600192static u8 vbe_get_mode_info(struct vesa_state *mi)
Simon Glassb2978d32014-11-14 20:56:32 -0700193{
194 u16 buffer_seg;
195 u16 buffer_adr;
196 char *buffer;
197
198 debug("VBE: Getting information about VESA mode %04x\n",
199 mi->video_mode);
200 buffer = PTR_TO_REAL_MODE(asm_realmode_buffer);
201 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
202 buffer_adr = ((unsigned long)buffer) & 0xffff;
203
204 realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000, mi->video_mode,
205 0x0000, buffer_seg, buffer_adr);
Simon Glass58766fd2023-07-30 11:16:04 -0600206 memcpy(mi->mode_info_block, buffer, sizeof(struct vesa_mode_info));
Simon Glassb2978d32014-11-14 20:56:32 -0700207 mi->valid = true;
208
209 return 0;
210}
211
Simon Glass5b925202022-07-30 15:52:05 -0600212static u8 vbe_set_mode(struct vesa_state *mi)
Simon Glassb2978d32014-11-14 20:56:32 -0700213{
Simon Glass91ea0342015-01-01 16:18:04 -0700214 int video_mode = mi->video_mode;
215
216 debug("VBE: Setting VESA mode %#04x\n", video_mode);
Simon Glassb2978d32014-11-14 20:56:32 -0700217 /* request linear framebuffer mode */
Simon Glass91ea0342015-01-01 16:18:04 -0700218 video_mode |= (1 << 14);
Simon Glass6548ce92015-01-01 16:18:03 -0700219 /* don't clear the framebuffer, we do that later */
Simon Glass91ea0342015-01-01 16:18:04 -0700220 video_mode |= (1 << 15);
221 realmode_interrupt(0x10, VESA_SET_MODE, video_mode,
Simon Glassb2978d32014-11-14 20:56:32 -0700222 0x0000, 0x0000, 0x0000, 0x0000);
223
224 return 0;
225}
226
Simon Glass5b925202022-07-30 15:52:05 -0600227static void vbe_set_graphics(int vesa_mode, struct vesa_state *mode_info)
Simon Glassb2978d32014-11-14 20:56:32 -0700228{
229 unsigned char *framebuffer;
230
231 mode_info->video_mode = (1 << 14) | vesa_mode;
232 vbe_get_mode_info(mode_info);
233
Simon Glassaa0ed9d2017-01-16 07:03:42 -0700234 framebuffer = (unsigned char *)(ulong)mode_info->vesa.phys_base_ptr;
Simon Glassb2978d32014-11-14 20:56:32 -0700235 debug("VBE: resolution: %dx%d@%d\n",
236 le16_to_cpu(mode_info->vesa.x_resolution),
237 le16_to_cpu(mode_info->vesa.y_resolution),
238 mode_info->vesa.bits_per_pixel);
239 debug("VBE: framebuffer: %p\n", framebuffer);
240 if (!framebuffer) {
241 debug("VBE: Mode does not support linear framebuffer\n");
242 return;
243 }
244
Simon Glass91ea0342015-01-01 16:18:04 -0700245 mode_info->video_mode &= 0x3ff;
Simon Glassb2978d32014-11-14 20:56:32 -0700246 vbe_set_mode(mode_info);
247}
Bin Mengc9dba412018-04-11 22:02:15 -0700248#endif /* CONFIG_FRAMEBUFFER_SET_VESA_MODE */
Simon Glassb2978d32014-11-14 20:56:32 -0700249
Simon Glassa0630862015-11-29 13:17:58 -0700250void bios_run_on_x86(struct udevice *dev, unsigned long addr, int vesa_mode,
Simon Glass5b925202022-07-30 15:52:05 -0600251 struct vesa_state *mode_info)
Simon Glassb2978d32014-11-14 20:56:32 -0700252{
Simon Glassa0630862015-11-29 13:17:58 -0700253 pci_dev_t pcidev = dm_pci_get_bdf(dev);
Simon Glassb2978d32014-11-14 20:56:32 -0700254 u32 num_dev;
255
256 num_dev = PCI_BUS(pcidev) << 8 | PCI_DEV(pcidev) << 3 |
257 PCI_FUNC(pcidev);
258
259 /* Needed to avoid exceptions in some ROMs */
260 interrupt_init();
261
262 /* Set up some legacy information in the F segment */
263 setup_rombios();
264
265 /* Set up C interrupt handlers */
266 setup_interrupt_handlers();
267
268 /* Set up real-mode IDT */
269 setup_realmode_idt();
270
271 /* Make sure the code is placed. */
272 setup_realmode_code();
273
Simon Glassb2978d32014-11-14 20:56:32 -0700274 debug("Calling Option ROM at %lx, pci device %#x...", addr, num_dev);
275
276 /* Option ROM entry point is at OPROM start + 3 */
277 realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0,
278 0x0);
279 debug("done\n");
280
Bin Mengc9dba412018-04-11 22:02:15 -0700281#ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
Simon Glassb2978d32014-11-14 20:56:32 -0700282 if (vesa_mode != -1)
283 vbe_set_graphics(vesa_mode, mode_info);
Bin Mengc9dba412018-04-11 22:02:15 -0700284#endif
Simon Glassb2978d32014-11-14 20:56:32 -0700285}
286
287asmlinkage int interrupt_handler(u32 intnumber, u32 gsfs, u32 dses,
288 u32 edi, u32 esi, u32 ebp, u32 esp,
289 u32 ebx, u32 edx, u32 ecx, u32 eax,
290 u32 cs_ip, u16 stackflags)
291{
292 u32 ip;
293 u32 cs;
294 u32 flags;
295 int ret = 0;
296
297 ip = cs_ip & 0xffff;
298 cs = cs_ip >> 16;
299 flags = stackflags;
300
301#ifdef CONFIG_REALMODE_DEBUG
302 debug("oprom: INT# 0x%x\n", intnumber);
303 debug("oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
304 eax, ebx, ecx, edx);
305 debug("oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
306 ebp, esp, edi, esi);
307 debug("oprom: ip: %04x cs: %04x flags: %08x\n",
308 ip, cs, flags);
309 debug("oprom: stackflags = %04x\n", stackflags);
310#endif
311
312 /*
313 * Fetch arguments from the stack and put them to a place
314 * suitable for the interrupt handlers
315 */
316 M.x86.R_EAX = eax;
317 M.x86.R_ECX = ecx;
318 M.x86.R_EDX = edx;
319 M.x86.R_EBX = ebx;
320 M.x86.R_ESP = esp;
321 M.x86.R_EBP = ebp;
322 M.x86.R_ESI = esi;
323 M.x86.R_EDI = edi;
324 M.x86.intno = intnumber;
325 M.x86.R_EIP = ip;
326 M.x86.R_CS = cs;
327 M.x86.R_EFLG = flags;
328
329 /* Call the interrupt handler for this interrupt number */
330 ret = int_handler[intnumber]();
331
332 /*
333 * This code is quite strange...
334 *
335 * Put registers back on the stack. The assembler code will pop them
336 * later. We force (volatile!) changing the values of the parameters
337 * of this function. We know that they stay alive on the stack after
338 * we leave this function.
339 */
340 *(volatile u32 *)&eax = M.x86.R_EAX;
341 *(volatile u32 *)&ecx = M.x86.R_ECX;
342 *(volatile u32 *)&edx = M.x86.R_EDX;
343 *(volatile u32 *)&ebx = M.x86.R_EBX;
344 *(volatile u32 *)&esi = M.x86.R_ESI;
345 *(volatile u32 *)&edi = M.x86.R_EDI;
346 flags = M.x86.R_EFLG;
347
348 /* Pass success or error back to our caller via the CARRY flag */
349 if (ret) {
350 flags &= ~1; /* no error: clear carry */
351 } else {
352 debug("int%02x call returned error\n", intnumber);
353 flags |= 1; /* error: set carry */
354 }
355 *(volatile u16 *)&stackflags = flags;
356
357 return ret;
358}