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