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