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