blob: acae635cf1953a79f15d744e033642be4dcff7dc [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
234 mode_info->video_mode = (1 << 14) | vesa_mode;
235 vbe_get_mode_info(mode_info);
236
Simon Glassaa0ed9d2017-01-16 07:03:42 -0700237 framebuffer = (unsigned char *)(ulong)mode_info->vesa.phys_base_ptr;
Simon Glassb2978d32014-11-14 20:56:32 -0700238 debug("VBE: resolution: %dx%d@%d\n",
239 le16_to_cpu(mode_info->vesa.x_resolution),
240 le16_to_cpu(mode_info->vesa.y_resolution),
241 mode_info->vesa.bits_per_pixel);
242 debug("VBE: framebuffer: %p\n", framebuffer);
243 if (!framebuffer) {
244 debug("VBE: Mode does not support linear framebuffer\n");
245 return;
246 }
247
Simon Glass91ea0342015-01-01 16:18:04 -0700248 mode_info->video_mode &= 0x3ff;
Simon Glassb2978d32014-11-14 20:56:32 -0700249 vbe_set_mode(mode_info);
250}
Bin Mengc9dba412018-04-11 22:02:15 -0700251#endif /* CONFIG_FRAMEBUFFER_SET_VESA_MODE */
Simon Glassb2978d32014-11-14 20:56:32 -0700252
Simon Glassa0630862015-11-29 13:17:58 -0700253void bios_run_on_x86(struct udevice *dev, unsigned long addr, int vesa_mode,
Simon Glass5b925202022-07-30 15:52:05 -0600254 struct vesa_state *mode_info)
Simon Glassb2978d32014-11-14 20:56:32 -0700255{
Simon Glassa0630862015-11-29 13:17:58 -0700256 pci_dev_t pcidev = dm_pci_get_bdf(dev);
Simon Glassb2978d32014-11-14 20:56:32 -0700257 u32 num_dev;
258
259 num_dev = PCI_BUS(pcidev) << 8 | PCI_DEV(pcidev) << 3 |
260 PCI_FUNC(pcidev);
261
262 /* Needed to avoid exceptions in some ROMs */
263 interrupt_init();
264
265 /* Set up some legacy information in the F segment */
266 setup_rombios();
267
268 /* Set up C interrupt handlers */
269 setup_interrupt_handlers();
270
271 /* Set up real-mode IDT */
272 setup_realmode_idt();
273
274 /* Make sure the code is placed. */
275 setup_realmode_code();
276
Simon Glassb2978d32014-11-14 20:56:32 -0700277 debug("Calling Option ROM at %lx, pci device %#x...", addr, num_dev);
278
279 /* Option ROM entry point is at OPROM start + 3 */
280 realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0,
281 0x0);
282 debug("done\n");
283
Bin Mengc9dba412018-04-11 22:02:15 -0700284#ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
Simon Glassb2978d32014-11-14 20:56:32 -0700285 if (vesa_mode != -1)
286 vbe_set_graphics(vesa_mode, mode_info);
Bin Mengc9dba412018-04-11 22:02:15 -0700287#endif
Simon Glassb2978d32014-11-14 20:56:32 -0700288}
289
290asmlinkage int interrupt_handler(u32 intnumber, u32 gsfs, u32 dses,
291 u32 edi, u32 esi, u32 ebp, u32 esp,
292 u32 ebx, u32 edx, u32 ecx, u32 eax,
293 u32 cs_ip, u16 stackflags)
294{
295 u32 ip;
296 u32 cs;
297 u32 flags;
298 int ret = 0;
299
300 ip = cs_ip & 0xffff;
301 cs = cs_ip >> 16;
302 flags = stackflags;
303
304#ifdef CONFIG_REALMODE_DEBUG
305 debug("oprom: INT# 0x%x\n", intnumber);
306 debug("oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
307 eax, ebx, ecx, edx);
308 debug("oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
309 ebp, esp, edi, esi);
310 debug("oprom: ip: %04x cs: %04x flags: %08x\n",
311 ip, cs, flags);
312 debug("oprom: stackflags = %04x\n", stackflags);
313#endif
314
315 /*
316 * Fetch arguments from the stack and put them to a place
317 * suitable for the interrupt handlers
318 */
319 M.x86.R_EAX = eax;
320 M.x86.R_ECX = ecx;
321 M.x86.R_EDX = edx;
322 M.x86.R_EBX = ebx;
323 M.x86.R_ESP = esp;
324 M.x86.R_EBP = ebp;
325 M.x86.R_ESI = esi;
326 M.x86.R_EDI = edi;
327 M.x86.intno = intnumber;
328 M.x86.R_EIP = ip;
329 M.x86.R_CS = cs;
330 M.x86.R_EFLG = flags;
331
332 /* Call the interrupt handler for this interrupt number */
333 ret = int_handler[intnumber]();
334
335 /*
336 * This code is quite strange...
337 *
338 * Put registers back on the stack. The assembler code will pop them
339 * later. We force (volatile!) changing the values of the parameters
340 * of this function. We know that they stay alive on the stack after
341 * we leave this function.
342 */
343 *(volatile u32 *)&eax = M.x86.R_EAX;
344 *(volatile u32 *)&ecx = M.x86.R_ECX;
345 *(volatile u32 *)&edx = M.x86.R_EDX;
346 *(volatile u32 *)&ebx = M.x86.R_EBX;
347 *(volatile u32 *)&esi = M.x86.R_ESI;
348 *(volatile u32 *)&edi = M.x86.R_EDI;
349 flags = M.x86.R_EFLG;
350
351 /* Pass success or error back to our caller via the CARRY flag */
352 if (ret) {
353 flags &= ~1; /* no error: clear carry */
354 } else {
355 debug("int%02x call returned error\n", intnumber);
356 flags |= 1; /* error: set carry */
357 }
358 *(volatile u16 *)&stackflags = flags;
359
360 return ret;
361}