Xeon-SP boards: Factor out OCP VPD `get_cxl_mode()` impl
[coreboot2.git] / src / device / oprom / realmode / x86.c
blobc3adf401f42dbf59130091d2d7852d71eb5a4a6f
1 /* SPDX-License-Identifier: GPL-2.0-only */
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>
19 /* we use x86emu's register file representation */
20 #include <x86emu/regs.h>
22 #include "x86.h"
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;
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.
39 extern unsigned char __realmode_call, __realmode_interrupt;
40 extern unsigned char __realmode_buffer;
42 #define PTR_TO_REAL_MODE(sym)\
43 (void *)(REALMODE_BASE + ((char *)&(sym) - (char *)&__realmode_code))
45 /* to have a common register file for interrupt handlers */
46 X86EMU_sysEnv _X86EMU_env;
48 unsigned int (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
49 u32 esi, u32 edi) asmlinkage;
51 unsigned int (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
52 u32 edx, u32 esi, u32 edi) asmlinkage;
54 static void setup_realmode_code(void)
56 memcpy(REALMODE_BASE, &__realmode_code, __realmode_code_size);
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);
62 printk(BIOS_SPEW, "Real mode stub @%p: %d bytes\n", REALMODE_BASE,
63 __realmode_code_size);
66 static void setup_rombios(void)
68 const char date[] = "06/11/99";
69 memcpy((void *)0xffff5, &date, 8);
71 const char ident[] = "PCI_ISA";
72 memcpy((void *)0xfffd9, &ident, 7);
74 /* system model: IBM-AT */
75 write8((void *)0xffffe, 0xfc);
78 static int (*intXX_handler[256])(void) = { NULL };
80 static int intXX_exception_handler(void)
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
103 struct eregs *regs = &reg_info;
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
109 return 0; // Never really returns
112 static int intXX_unknown_handler(void)
114 printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
115 M.x86.intno, X86_EAX);
117 return -1;
120 /* setup interrupt handlers for mainboard */
121 void mainboard_interrupt_handlers(int intXX, int (*intXX_func)(void))
123 intXX_handler[intXX] = intXX_func;
126 static void setup_interrupt_handlers(void)
128 int i;
130 /* The first 16 intXX functions are not BIOS services,
131 * but the CPU-generated exceptions ("hardware interrupts")
133 for (i = 0; i < 0x10; i++)
134 intXX_handler[i] = &intXX_exception_handler;
136 /* Mark all other intXX calls as unknown first */
137 for (i = 0x10; i < 0x100; i++)
139 /* If the mainboard_interrupt_handler isn't called first.
141 if (!intXX_handler[i])
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.
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;
169 static void write_idt_stub(void *target, u8 intnum)
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. */
177 static void setup_realmode_idt(void)
179 struct realmode_idt *idts = (struct realmode_idt *) 0;
180 int i;
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();
186 /* Copy IDT stub code for each interrupt. This might seem wasteful
187 * but it is really simple
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);
195 null_breakpoint_init();
197 /* Many option ROMs use the hard coded interrupt entry points in the
198 * system bios. So install them at the known locations.
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);
221 #if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
222 static vbe_mode_info_t mode_info;
223 static int mode_info_valid;
225 const vbe_mode_info_t *vbe_mode_info(void)
227 if (!mode_info_valid || !mode_info.vesa.phys_base_ptr)
228 return NULL;
229 return &mode_info;
232 static int vbe_check_for_failure(int ah);
234 static u8 vbe_get_ctrl_info(vbe_info_block *info)
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;
246 memcpy(info, buffer, sizeof(vbe_info_block));
247 return 0;
250 static void vbe_oprom_list_supported_mode(uint16_t *video_mode_ptr)
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);
261 static u8 vbe_oprom_supported_mode_list(void)
263 uint16_t segment, offset;
264 vbe_info_block info;
266 if (vbe_get_ctrl_info(&info))
267 return 1;
269 offset = info.video_mode_ptr;
270 segment = info.video_mode_ptr >> 16;
272 vbe_oprom_list_supported_mode((uint16_t *)((segment << 4) + offset));
273 return 0;
276 * EAX register is used to indicate the completion status upon return from
277 * VBE function in real mode.
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:
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
287 * Return 0 on success else -1 for failure
289 static int vbe_check_for_failure(int ah)
291 int status;
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;
315 return status;
317 static u8 vbe_get_mode_info(vbe_mode_info_t * mi)
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;
330 memcpy(mi->mode_info_block, buffer, sizeof(mi->mode_info_block));
331 mode_info_valid = 1;
332 return 0;
335 static u8 vbe_set_mode(vbe_mode_info_t * mi)
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;
348 return 0;
351 /* These two functions could probably even be generic between
352 * yabel and x86 native. TBD later.
354 void vbe_set_graphics(void)
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;
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);
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;
375 if (vbe_set_mode(&mode_info)) {
376 printk(BIOS_WARNING, "VBE Warning: Could not set VBE graphics mode.\n");
377 return;
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,
396 fb_add_framebuffer_info_ex(&fb);
399 void vbe_textmode_console(void)
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;
410 if (retval)
411 printk(BIOS_WARNING, "VBE Warning: Could not set VBE text mode.\n");
414 #endif
416 void run_bios(struct device *dev, unsigned long addr)
418 u32 num_dev = (dev->upstream->secondary << 8) | dev->path.pci.devfn;
420 /* Setting up required hardware.
421 * Removing this will cause random illegal instruction exceptions
422 * in some option roms.
424 setup_i8259();
425 setup_i8254();
427 /* Set up some legacy information in the F segment */
428 setup_rombios();
430 /* Set up C interrupt handlers */
431 setup_interrupt_handlers();
433 /* Set up real-mode IDT */
434 setup_realmode_idt();
436 /* Make sure the code is placed. */
437 setup_realmode_code();
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");
445 #if CONFIG(FRAMEBUFFER_SET_VESA_MODE)
446 if ((dev->class >> 8)== PCI_CLASS_DISPLAY_VGA)
447 vbe_set_graphics();
448 #endif
451 /* interrupt_handler() is called from assembler code only,
452 * so there is no use in putting the prototype into a header file.
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);
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)
470 u32 ip;
471 u32 cs;
472 u32 flags;
473 int ret = 0;
475 ip = cs_ip & 0xffff;
476 cs = cs_ip >> 16;
477 flags = stackflags;
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
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;
505 // Call the interrupt handler for this int#
506 ret = intXX_handler[intnumber]();
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;
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
529 *(volatile u16 *)&stackflags = flags;
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;