| 1 |
|
| 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 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 54 |
static void setup_realmode_code(void) |
| 55 |
{ |
| 56 |
memcpy(REALMODE_BASE, &__realmode_code, __realmode_code_size); |
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 75 |
write8((void *)0xffffe, 0xfc); |
| 76 |
} |
| 77 |
|
| 78 |
static int (*intXX_handler[256])(void) = { NULL }; |
| 79 |
|
| 80 |
static int intXX_exception_handler(void) |
| 81 |
{ |
| 82 |
|
| 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, |
| 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 = ®_info; |
| 104 |
|
| 105 |
printk(BIOS_INFO, "Oops, exception %d while executing option rom\n", |
| 106 |
(uint32_t)regs->vector); |
| 107 |
x86_exception(regs); |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 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 |
|
| 121 |
void mainboard_interrupt_handlers(int intXX, int (*intXX_func)(void)) |
| 122 |
{ |
| 123 |
intXX_handler[intXX] = intXX_func; |
| 124 |
} |
| 125 |
|
| 126 |
static void setup_interrupt_handlers(void) |
| 127 |
{ |
| 128 |
int i; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
for (i = 0; i < 0x10; i++) |
| 134 |
intXX_handler[i] = &intXX_exception_handler; |
| 135 |
|
| 136 |
|
| 137 |
for (i = 0x10; i < 0x100; i++) |
| 138 |
{ |
| 139 |
|
| 140 |
|
| 141 |
if (!intXX_handler[i]) |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 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; |
| 175 |
} |
| 176 |
|
| 177 |
static void setup_realmode_idt(void) |
| 178 |
{ |
| 179 |
struct realmode_idt *idts = (struct realmode_idt *) 0; |
| 180 |
int i; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
null_breakpoint_remove(); |
| 185 |
|
| 186 |
|
| 187 |
|
| 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 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
write_idt_stub((void *)0xff065, 0x42); |
| 203 |
|
| 204 |
write_idt_stub((void *)0xff84d, 0x11); |
| 205 |
|
| 206 |
write_idt_stub((void *)0xff841, 0x12); |
| 207 |
|
| 208 |
write_idt_stub((void *)0xfec59, 0x13); |
| 209 |
|
| 210 |
write_idt_stub((void *)0xfe739, 0x14); |
| 211 |
|
| 212 |
write_idt_stub((void *)0xff859, 0x15); |
| 213 |
|
| 214 |
write_idt_stub((void *)0xfe82e, 0x16); |
| 215 |
|
| 216 |
write_idt_stub((void *)0xfefd2, 0x17); |
| 217 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 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 |
} |
| 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 |
|
| 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 |
|
| 339 |
mi->video_mode |= (1 << 14); |
| 340 |
|
| 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 |
|
| 352 |
|
| 353 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
setup_i8259(); |
| 425 |
setup_i8254(); |
| 426 |
|
| 427 |
|
| 428 |
setup_rombios(); |
| 429 |
|
| 430 |
|
| 431 |
setup_interrupt_handlers(); |
| 432 |
|
| 433 |
|
| 434 |
setup_realmode_idt(); |
| 435 |
|
| 436 |
|
| 437 |
setup_realmode_code(); |
| 438 |
|
| 439 |
printk(BIOS_DEBUG, "Calling Option ROM...\n"); |
| 440 |
|
| 441 |
|
| 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 |
|
| 452 |
|
| 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 |
|
| 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 |
|
| 490 |
|
| 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 |
|
| 501 |
X86_EIP = ip; |
| 502 |
X86_CS = cs; |
| 503 |
X86_EFLAGS = flags; |
| 504 |
|
| 505 |
|
| 506 |
ret = intXX_handler[intnumber](); |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 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 |
|
| 523 |
if (ret) { |
| 524 |
flags &= ~1; |
| 525 |
}else{ |
| 526 |
printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber); |
| 527 |
flags |= 1; |
| 528 |
} |
| 529 |
*(volatile u16 *)&stackflags = flags; |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
return ret; |
| 534 |
} |