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>
10 #include <device/pci.h>
11 #include <device/pci_ids.h>
12 #include <pc80/i8259.h>
13 #include <pc80/i8254.h>
17 #include <framebuffer_info.h>
19 /* we use x86emu's register file representation */
20 #include <x86emu/regs.h>
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
= {
93 .error_code
=0, // FIXME: fill in
103 struct eregs
*regs
= ®_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
);
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)
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.
150 intXX_handler
[0x10] = &int10_handler
;
153 intXX_handler
[0x12] = &int12_handler
;
156 intXX_handler
[0x16] = &int16_handler
;
159 intXX_handler
[0x1a] = &int1a_handler
;
162 intXX_handler
[i
] = &intXX_unknown_handler
;
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;
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
++) {
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
)
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 */
243 printk(BIOS_WARNING
, "Error from VGA BIOS in %s\n", __func__
);
246 memcpy(info
, buffer
, sizeof(vbe_info_block
));
250 static void vbe_oprom_list_supported_mode(uint16_t *video_mode_ptr
)
253 printk(BIOS_DEBUG
, "Supported Video Mode list for OpRom:\n");
255 mode
= *video_mode_ptr
++;
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
;
266 if (vbe_get_ctrl_info(&info
))
269 offset
= info
.video_mode_ptr
;
270 segment
= info
.video_mode_ptr
>> 16;
272 vbe_oprom_list_supported_mode((uint16_t *)((segment
<< 4) + offset
));
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
)
298 printk(BIOS_DEBUG
, "VBE: Function call failed!\n");
302 printk(BIOS_DEBUG
, "VBE: Function is not supported!\n");
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");
317 static u8
vbe_get_mode_info(vbe_mode_info_t
* mi
)
319 printk(BIOS_DEBUG
, "VBE: Getting information about VESA mode %04x\n",
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__
);
330 memcpy(mi
->mode_info_block
, buffer
, sizeof(mi
->mode_info_block
));
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__
);
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");
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
);
370 printk(BIOS_DEBUG
, "VBE: Mode does not support linear "
375 if (vbe_set_mode(&mode_info
)) {
376 printk(BIOS_WARNING
, "VBE Warning: Could not set VBE graphics mode.\n");
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)
402 if (mode_info
.vesa
.phys_base_ptr
) {
404 X86_EAX
= realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
405 0x0000, 0x0000, 0x0000);
406 if (!vbe_check_for_failure(X86_AH
))
411 printk(BIOS_WARNING
, "VBE Warning: Could not set VBE text mode.\n");
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.
427 /* Set up some legacy information in the F segment */
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
)
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
,
460 u32 cs_ip
, u16 stackflags
);
462 int asmlinkage
interrupt_handler(u32 intnumber
,
468 u32 cs_ip
, u16 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",
483 printk(BIOS_DEBUG
, "oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
485 printk(BIOS_DEBUG
, "oprom: ip: %04x cs: %04x flags: %08x\n",
489 // Fetch arguments from the stack and put them to a place
490 // suitable for the interrupt handlers
499 M
.x86
.intno
= intnumber
;
500 /* TODO: error_code must be stored somewhere */
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
;
522 /* Pass success or error back to our caller via the CARRY flag */
524 flags
&= ~1; // no error: clear carry
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 */