• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/mmio.h>
4 #include <arch/interrupt.h>
5 #include <arch/null_breakpoint.h>
6 #include <arch/registers.h>
7 #include <boot/coreboot_tables.h>
8 #include <console/console.h>
9 #include <delay.h>
10 #include <device/pci.h>
11 #include <device/pci_ids.h>
12 #include <pc80/i8259.h>
13 #include <pc80/i8254.h>
14 #include <stdint.h>
15 #include <string.h>
16 #include <vbe.h>
17 #include <framebuffer_info.h>
18 
19 /* we use x86emu's register file representation */
20 #include <x86emu/regs.h>
21 
22 #include "x86.h"
23 
24 typedef struct {
25 	char signature[4];
26 	u16 version;
27 	u8 *oem_string_ptr;
28 	u32 capabilities;
29 	u32 video_mode_ptr;
30 	u16 total_memory;
31 	char reserved[236];
32 } __packed vbe_info_block;
33 
34 /* The following symbols cannot be used directly. They need to be fixed up
35  * to point to the correct address location after the code has been copied
36  * to REALMODE_BASE. Absolute symbols are not used because those symbols are
37  * relocated when a relocatable ramstage is enabled.
38  */
39 extern unsigned char __realmode_call, __realmode_interrupt;
40 extern unsigned char __realmode_buffer;
41 
42 #define PTR_TO_REAL_MODE(sym)\
43 	(void *)(REALMODE_BASE + ((char *)&(sym) - (char *)&__realmode_code))
44 
45 /* to have a common register file for interrupt handlers */
46 X86EMU_sysEnv _X86EMU_env;
47 
48 unsigned int (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
49 		u32 esi, u32 edi) asmlinkage;
50 
51 unsigned int (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
52 		u32 edx, u32 esi, u32 edi) asmlinkage;
53 
setup_realmode_code(void)54 static void setup_realmode_code(void)
55 {
56 	memcpy(REALMODE_BASE, &__realmode_code, __realmode_code_size);
57 
58 	/* Ensure the global pointers are relocated properly. */
59 	realmode_call = PTR_TO_REAL_MODE(__realmode_call);
60 	realmode_interrupt = PTR_TO_REAL_MODE(__realmode_interrupt);
61 
62 	printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
63 			__realmode_code_size);
64 }
65 
setup_rombios(void)66 static void setup_rombios(void)
67 {
68 	const char date[] = "06/11/99";
69 	memcpy((void *)0xffff5, &date, 8);
70 
71 	const char ident[] = "PCI_ISA";
72 	memcpy((void *)0xfffd9, &ident, 7);
73 
74 	/* system model: IBM-AT */
75 	write8((void *)0xffffe, 0xfc);
76 }
77 
78 static int (*intXX_handler[256])(void) = { NULL };
79 
intXX_exception_handler(void)80 static int intXX_exception_handler(void)
81 {
82 	/* compatibility shim */
83 	struct eregs reg_info = {
84 		.eax=X86_EAX,
85 		.ecx=X86_ECX,
86 		.edx=X86_EDX,
87 		.ebx=X86_EBX,
88 		.esp=X86_ESP,
89 		.ebp=X86_EBP,
90 		.esi=X86_ESI,
91 		.edi=X86_EDI,
92 		.vector=M.x86.intno,
93 		.error_code=0, // FIXME: fill in
94 		.cs=X86_CS,
95 #if ENV_X86_64
96 		.rip=X86_EIP,
97 		.rflags=X86_EFLAGS
98 #else
99 		.eip=X86_EIP,
100 		.eflags=X86_EFLAGS
101 #endif
102 	};
103 	struct eregs *regs = &reg_info;
104 
105 	printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
106 	       (uint32_t)regs->vector);
107 	x86_exception(regs);	// Call coreboot exception handler
108 
109 	return 0;		// Never really returns
110 }
111 
intXX_unknown_handler(void)112 static int intXX_unknown_handler(void)
113 {
114 	printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
115 			M.x86.intno, X86_EAX);
116 
117 	return -1;
118 }
119 
120 /* setup interrupt handlers for mainboard */
mainboard_interrupt_handlers(int intXX,int (* intXX_func)(void))121 void mainboard_interrupt_handlers(int intXX, int (*intXX_func)(void))
122 {
123 	intXX_handler[intXX] = intXX_func;
124 }
125 
setup_interrupt_handlers(void)126 static void setup_interrupt_handlers(void)
127 {
128 	int i;
129 
130 	/* The first 16 intXX functions are not BIOS services,
131 	 * but the CPU-generated exceptions ("hardware interrupts")
132 	 */
133 	for (i = 0; i < 0x10; i++)
134 		intXX_handler[i] = &intXX_exception_handler;
135 
136 	/* Mark all other intXX calls as unknown first */
137 	for (i = 0x10; i < 0x100; i++)
138 	{
139 		/* If the mainboard_interrupt_handler isn't called first.
140 		 */
141 		if (!intXX_handler[i])
142 		{
143 			/* Now set the default functions that are actually
144 			 * needed to initialize the option roms. This is
145 			 * very slick, as it allows us to implement mainboard
146 			 * specific interrupt handlers, such as the int15.
147 			 */
148 			switch (i) {
149 			case 0x10:
150 				intXX_handler[0x10] = &int10_handler;
151 				break;
152 			case 0x12:
153 				intXX_handler[0x12] = &int12_handler;
154 				break;
155 			case 0x16:
156 				intXX_handler[0x16] = &int16_handler;
157 				break;
158 			case 0x1a:
159 				intXX_handler[0x1a] = &int1a_handler;
160 				break;
161 			default:
162 				intXX_handler[i] = &intXX_unknown_handler;
163 				break;
164 			}
165 		}
166 	}
167 }
168 
write_idt_stub(void * target,u8 intnum)169 static void write_idt_stub(void *target, u8 intnum)
170 {
171 	unsigned char *codeptr;
172 	codeptr = (unsigned char *) target;
173 	memcpy(codeptr, &__idt_handler, __idt_handler_size);
174 	codeptr[3] = intnum; /* modify int# in the code stub. */
175 }
176 
setup_realmode_idt(void)177 static void setup_realmode_idt(void)
178 {
179 	struct realmode_idt *idts = (struct realmode_idt *) 0;
180 	int i;
181 
182 	/* It's expected that we write to the NULL page in the first two iterations of the
183 	   following loop, so temporarily disable the NULL breakpoint. */
184 	null_breakpoint_disable();
185 
186 	/* Copy IDT stub code for each interrupt. This might seem wasteful
187 	 * but it is really simple
188 	 */
189 	for (i = 0; i < 256; i++) {
190 		idts[i].cs = 0;
191 		idts[i].offset = 0x1000 + (i * __idt_handler_size);
192 		write_idt_stub((void *)((uintptr_t)idts[i].offset), i);
193 	}
194 
195 	null_breakpoint_init();
196 
197 	/* Many option ROMs use the hard coded interrupt entry points in the
198 	 * system bios. So install them at the known locations.
199 	 */
200 
201 	/* int42 is the relocated int10 */
202 	write_idt_stub((void *)0xff065, 0x42);
203 	/* BIOS Int 11 Handler F000:F84D */
204 	write_idt_stub((void *)0xff84d, 0x11);
205 	/* BIOS Int 12 Handler F000:F841 */
206 	write_idt_stub((void *)0xff841, 0x12);
207 	/* BIOS Int 13 Handler F000:EC59 */
208 	write_idt_stub((void *)0xfec59, 0x13);
209 	/* BIOS Int 14 Handler F000:E739 */
210 	write_idt_stub((void *)0xfe739, 0x14);
211 	/* BIOS Int 15 Handler F000:F859 */
212 	write_idt_stub((void *)0xff859, 0x15);
213 	/* BIOS Int 16 Handler F000:E82E */
214 	write_idt_stub((void *)0xfe82e, 0x16);
215 	/* BIOS Int 17 Handler F000:EFD2 */
216 	write_idt_stub((void *)0xfefd2, 0x17);
217 	/* ROM BIOS Int 1A Handler F000:FE6E */
218 	write_idt_stub((void *)0xffe6e, 0x1a);
219 }
220 
221 #if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
222 static vbe_mode_info_t mode_info;
223 static int mode_info_valid;
224 
vbe_mode_info(void)225 const vbe_mode_info_t *vbe_mode_info(void)
226 {
227 	if (!mode_info_valid || !mode_info.vesa.phys_base_ptr)
228 		return NULL;
229 	return &mode_info;
230 }
231 
232 static int vbe_check_for_failure(int ah);
233 
vbe_get_ctrl_info(vbe_info_block * info)234 static u8 vbe_get_ctrl_info(vbe_info_block *info)
235 {
236 	char *buffer = PTR_TO_REAL_MODE(__realmode_buffer);
237 	u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
238 	u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
239 	X86_EAX = realmode_interrupt(0x10, VESA_GET_INFO, 0x0000, 0x0000,
240 			0x0000, buffer_seg, buffer_adr);
241 	/* If the VBE function completed successfully, 0x0 is returned in AH */
242 	if (X86_AH) {
243 		printk(BIOS_WARNING, "Error from VGA BIOS in %s\n", __func__);
244 		return 1;
245 	}
246 	memcpy(info, buffer, sizeof(vbe_info_block));
247 	return 0;
248 }
249 
vbe_oprom_list_supported_mode(uint16_t * video_mode_ptr)250 static void vbe_oprom_list_supported_mode(uint16_t *video_mode_ptr)
251 {
252 	uint16_t mode;
253 	printk(BIOS_DEBUG, "Supported Video Mode list for OpRom:\n");
254 	do {
255 		mode = *video_mode_ptr++;
256 		if (mode != 0xffff)
257 			printk(BIOS_DEBUG, "%x\n", mode);
258 	} while (mode != 0xffff);
259 }
260 
vbe_oprom_supported_mode_list(void)261 static u8 vbe_oprom_supported_mode_list(void)
262 {
263 	uint16_t segment, offset;
264 	vbe_info_block info;
265 
266 	if (vbe_get_ctrl_info(&info))
267 		return 1;
268 
269 	offset = info.video_mode_ptr;
270 	segment = info.video_mode_ptr >> 16;
271 
272 	vbe_oprom_list_supported_mode((uint16_t *)((segment << 4) + offset));
273 	return 0;
274 }
275 /*
276  * EAX register is used to indicate the completion status upon return from
277  * VBE function in real mode.
278  *
279  * If the VBE function completed successfully then 0x0 is returned in the AH
280  * register. Otherwise the AH register is set with the nature of the failure:
281  *
282  * AH == 0x00: Function call successful
283  * AH == 0x01: Function call failed
284  * AH == 0x02: Function is not supported in the current HW configuration
285  * AH == 0x03: Function call invalid in current video mode
286  *
287  * Return 0 on success else -1 for failure
288  */
vbe_check_for_failure(int ah)289 static int vbe_check_for_failure(int ah)
290 {
291 	int status;
292 
293 	switch (ah) {
294 	case 0x0:
295 		status = 0;
296 		break;
297 	case 1:
298 		printk(BIOS_DEBUG, "VBE: Function call failed!\n");
299 		status = -1;
300 		break;
301 	case 2:
302 		printk(BIOS_DEBUG, "VBE: Function is not supported!\n");
303 		status = -1;
304 		break;
305 	case 3:
306 	default:
307 		printk(BIOS_DEBUG, "VBE: Unsupported video mode %x!\n",
308 			CONFIG_FRAMEBUFFER_VESA_MODE);
309 		if (vbe_oprom_supported_mode_list())
310 			printk(BIOS_WARNING, "VBE Warning: Could not get VBE mode list.\n");
311 		status = -1;
312 		break;
313 	}
314 
315 	return status;
316 }
vbe_get_mode_info(vbe_mode_info_t * mi)317 static u8 vbe_get_mode_info(vbe_mode_info_t * mi)
318 {
319 	printk(BIOS_DEBUG, "VBE: Getting information about VESA mode %04x\n",
320 		mi->video_mode);
321 	char *buffer = PTR_TO_REAL_MODE(__realmode_buffer);
322 	u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
323 	u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
324 	X86_EAX = realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000,
325 			mi->video_mode, 0x0000, buffer_seg, buffer_adr);
326 	if (vbe_check_for_failure(X86_AH)) {
327 		printk(BIOS_WARNING, "VBE Warning: Error from VGA BIOS in %s\n", __func__);
328 		return 1;
329 	}
330 	memcpy(mi->mode_info_block, buffer, sizeof(mi->mode_info_block));
331 	mode_info_valid = 1;
332 	return 0;
333 }
334 
vbe_set_mode(vbe_mode_info_t * mi)335 static u8 vbe_set_mode(vbe_mode_info_t * mi)
336 {
337 	printk(BIOS_DEBUG, "VBE: Setting VESA mode %04x\n", mi->video_mode);
338 	// request linear framebuffer mode
339 	mi->video_mode |= (1 << 14);
340 	// request clearing of framebuffer
341 	mi->video_mode &= ~(1 << 15);
342 	X86_EAX = realmode_interrupt(0x10, VESA_SET_MODE, mi->video_mode,
343 			0x0000, 0x0000, 0x0000, 0x0000);
344 	if (vbe_check_for_failure(X86_AH)) {
345 		printk(BIOS_WARNING, "VBE Warning: Error from VGA BIOS in %s\n", __func__);
346 		return 1;
347 	}
348 	return 0;
349 }
350 
351 /* These two functions could probably even be generic between
352  * yabel and x86 native. TBD later.
353  */
vbe_set_graphics(void)354 void vbe_set_graphics(void)
355 {
356 	mode_info.video_mode = (1 << 14) | CONFIG_FRAMEBUFFER_VESA_MODE;
357 	if (vbe_get_mode_info(&mode_info)) {
358 		printk(BIOS_WARNING, "VBE Warning: Could not get VBE graphics mode info.\n");
359 		return;
360 	}
361 	unsigned char *framebuffer =
362 		(unsigned char *)mode_info.vesa.phys_base_ptr;
363 	printk(BIOS_DEBUG, "VBE: resolution:  %dx%d@%d\n",
364 		le16_to_cpu(mode_info.vesa.x_resolution),
365 		le16_to_cpu(mode_info.vesa.y_resolution),
366 		mode_info.vesa.bits_per_pixel);
367 
368 	printk(BIOS_DEBUG, "VBE: framebuffer: %p\n", framebuffer);
369 	if (!framebuffer) {
370 		printk(BIOS_DEBUG, "VBE: Mode does not support linear "
371 			"framebuffer\n");
372 		return;
373 	}
374 
375 	if (vbe_set_mode(&mode_info)) {
376 		printk(BIOS_WARNING, "VBE Warning: Could not set VBE graphics mode.\n");
377 		return;
378 	}
379 	const struct lb_framebuffer fb = {
380 		.physical_address    = mode_info.vesa.phys_base_ptr,
381 		.x_resolution        = le16_to_cpu(mode_info.vesa.x_resolution),
382 		.y_resolution        = le16_to_cpu(mode_info.vesa.y_resolution),
383 		.bytes_per_line      = le16_to_cpu(mode_info.vesa.bytes_per_scanline),
384 		.bits_per_pixel      = mode_info.vesa.bits_per_pixel,
385 		.red_mask_pos        = mode_info.vesa.red_mask_pos,
386 		.red_mask_size       = mode_info.vesa.red_mask_size,
387 		.green_mask_pos      = mode_info.vesa.green_mask_pos,
388 		.green_mask_size     = mode_info.vesa.green_mask_size,
389 		.blue_mask_pos       = mode_info.vesa.blue_mask_pos,
390 		.blue_mask_size      = mode_info.vesa.blue_mask_size,
391 		.reserved_mask_pos   = mode_info.vesa.reserved_mask_pos,
392 		.reserved_mask_size  = mode_info.vesa.reserved_mask_size,
393 		.orientation         = LB_FB_ORIENTATION_NORMAL,
394 	};
395 
396 	fb_add_framebuffer_info_ex(&fb);
397 }
398 
vbe_textmode_console(void)399 void vbe_textmode_console(void)
400 {
401 	u8 retval = 1;
402 	if (mode_info.vesa.phys_base_ptr) {
403 		delay(2);
404 		X86_EAX = realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
405 					0x0000, 0x0000, 0x0000);
406 		if (!vbe_check_for_failure(X86_AH))
407 			retval = 0;
408 	}
409 
410 	if (retval)
411 		printk(BIOS_WARNING, "VBE Warning: Could not set VBE text mode.\n");
412 }
413 
414 #endif
415 
run_bios(struct device * dev,unsigned long addr)416 void run_bios(struct device *dev, unsigned long addr)
417 {
418 	u32 num_dev = (dev->upstream->secondary << 8) | dev->path.pci.devfn;
419 
420 	/* Setting up required hardware.
421 	 * Removing this will cause random illegal instruction exceptions
422 	 * in some option roms.
423 	 */
424 	setup_i8259();
425 	setup_i8254();
426 
427 	/* Set up some legacy information in the F segment */
428 	setup_rombios();
429 
430 	/* Set up C interrupt handlers */
431 	setup_interrupt_handlers();
432 
433 	/* Set up real-mode IDT */
434 	setup_realmode_idt();
435 
436 	/* Make sure the code is placed. */
437 	setup_realmode_code();
438 
439 	printk(BIOS_DEBUG, "Calling Option ROM...\n");
440 	/* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
441 	/* Option ROM entry point is at OPROM start + 3 */
442 	realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
443 	printk(BIOS_DEBUG, "... Option ROM returned.\n");
444 
445 #if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
446 	if ((dev->class >> 8)== PCI_CLASS_DISPLAY_VGA)
447 		vbe_set_graphics();
448 #endif
449 }
450 
451 /* interrupt_handler() is called from assembler code only,
452  * so there is no use in putting the prototype into a header file.
453  */
454 int asmlinkage interrupt_handler(u32 intnumber,
455 	    u32 gsfs, u32 dses,
456 	    u32 edi, u32 esi,
457 	    u32 ebp, u32 esp,
458 	    u32 ebx, u32 edx,
459 	    u32 ecx, u32 eax,
460 	    u32 cs_ip, u16 stackflags);
461 
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)462 int asmlinkage interrupt_handler(u32 intnumber,
463 	    u32 gsfs, u32 dses,
464 	    u32 edi, u32 esi,
465 	    u32 ebp, u32 esp,
466 	    u32 ebx, u32 edx,
467 	    u32 ecx, u32 eax,
468 	    u32 cs_ip, u16 stackflags)
469 {
470 	u32 ip;
471 	u32 cs;
472 	u32 flags;
473 	int ret = 0;
474 
475 	ip = cs_ip & 0xffff;
476 	cs = cs_ip >> 16;
477 	flags = stackflags;
478 
479 #if CONFIG(REALMODE_DEBUG)
480 	printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
481 	printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
482 		      eax, ebx, ecx, edx);
483 	printk(BIOS_DEBUG, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
484 		     ebp, esp, edi, esi);
485 	printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
486 		     ip, cs, flags);
487 #endif
488 
489 	// Fetch arguments from the stack and put them to a place
490 	// suitable for the interrupt handlers
491 	X86_EAX = eax;
492 	X86_ECX = ecx;
493 	X86_EDX = edx;
494 	X86_EBX = ebx;
495 	X86_ESP = esp;
496 	X86_EBP = ebp;
497 	X86_ESI = esi;
498 	X86_EDI = edi;
499 	M.x86.intno = intnumber;
500 	/* TODO: error_code must be stored somewhere */
501 	X86_EIP = ip;
502 	X86_CS = cs;
503 	X86_EFLAGS = flags;
504 
505 	// Call the interrupt handler for this int#
506 	ret = intXX_handler[intnumber]();
507 
508 	// Put registers back on the stack. The assembler code
509 	// will later pop them.
510 	// What happens here is that we force (volatile!) changing
511 	// the values of the parameters of this function. We do this
512 	// because we know that they stay alive on the stack after
513 	// we leave this function. Don't say this is bollocks.
514 	*(volatile u32 *)&eax = X86_EAX;
515 	*(volatile u32 *)&ecx = X86_ECX;
516 	*(volatile u32 *)&edx = X86_EDX;
517 	*(volatile u32 *)&ebx = X86_EBX;
518 	*(volatile u32 *)&esi = X86_ESI;
519 	*(volatile u32 *)&edi = X86_EDI;
520 	flags = X86_EFLAGS;
521 
522 	/* Pass success or error back to our caller via the CARRY flag */
523 	if (ret) {
524 		flags &= ~1; // no error: clear carry
525 	}else{
526 		printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
527 		flags |= 1;  // error: set carry
528 	}
529 	*(volatile u16 *)&stackflags = flags;
530 
531 	/* The assembler code doesn't actually care for the return value,
532 	 * but keep it around so its expectations are met */
533 	return ret;
534 }
535