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