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