remove trailing whitespace
[coreboot.git] / src / devices / oprom / x86.c
index 2ce5b4534d8d9db11bb145e15a4af8b10bb5e55b..0c15b1560c538a485e3b44952e099eeb6f41e4f1 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * This file is part of the coreboot project.
  *
- * Copyright (C) 2009 coresystems GmbH
+ * Copyright (C) 2007 Advanced Micro Devices, Inc.
+ * Copyright (C) 2009-2010 coresystems GmbH
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <arch/registers.h>
 #include <console/console.h>
 #include <arch/interrupt.h>
+#include <cbfs.h>
+#include <delay.h>
+#include <pc80/i8259.h>
+#include "x86.h"
+#include "vbe.h"
+#include "../../src/lib/jpeg.h"
+
+void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
+               u32 esi, u32 edi) __attribute__((regparm(0))) =
+                                               (void *)&__realmode_call;
+
+void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx, u32 edx,
+               u32 esi, u32 edi) __attribute__((regparm(0))) =
+                                               (void *)&__realmode_interrupt;
+
+static void setup_bda(void)
+{
+       /* clear BIOS DATA AREA */
+       memset((void *)0x400, 0, 0x200);
 
-#define REALMODE_BASE ((void *)0x600)
+       write16(0x413, FAKE_MEMORY_SIZE / 1024);
+       write16(0x40e, INITIAL_EBDA_SEGMENT);
 
-struct realmode_idt {
-       u16 offset, cs;
-};
+       /* Set up EBDA */
+       memset((void *)(INITIAL_EBDA_SEGMENT << 4), 0, INITIAL_EBDA_SIZE);
+       write16((INITIAL_EBDA_SEGMENT << 4) + 0x0, INITIAL_EBDA_SIZE / 1024);
+}
 
-void x86_exception(struct eregs *info);
+static void setup_rombios(void)
+{
+       const char date[] = "06/11/99";
+       memcpy((void *)0xffff5, &date, 8);
 
-extern unsigned char __idt_handler, __idt_handler_size;
-extern unsigned char __realmode_code, __realmode_code_size;
-extern unsigned char __run_optionrom, __run_interrupt;
+       const char ident[] = "PCI_ISA";
+       memcpy((void *)0xfffd9, &ident, 7);
 
-void (*run_optionrom)(u32 devfn) __attribute__((regparm(0))) = (void *)&__run_optionrom;
-void (*vga_enable_console)(void) __attribute__((regparm(0))) = (void *)&__run_interrupt;
+       /* system model: IBM-AT */
+       write8(0xffffe, 0xfc);
+}
 
 int (*intXX_handler[256])(struct eregs *regs) = { NULL };
 
 static int intXX_exception_handler(struct eregs *regs)
 {
-       printk(BIOS_INFO, "Oops, exception %d while executing option rom\n", 
+       printk(BIOS_INFO, "Oops, exception %d while executing option rom\n",
                        regs->vector);
-       x86_exception(regs);    // Call coreboot exception handler 
+       x86_exception(regs);    // Call coreboot exception handler
 
-       return 0;               // Never returns?
+       return 0;               // Never really returns
 }
 
 static int intXX_unknown_handler(struct eregs *regs)
 {
-       printk(BIOS_INFO, "Unsupported software interrupt #0x%x\n", 
-                       regs->vector);
+       printk(BIOS_INFO, "Unsupported software interrupt #0x%x eax 0x%x\n",
+                       regs->vector, regs->eax);
 
        return -1;
 }
@@ -65,20 +90,16 @@ void mainboard_interrupt_handlers(int intXX, void *intXX_func)
        intXX_handler[intXX] = intXX_func;
 }
 
-int int12_handler(struct eregs *regs);
-int int15_handler(struct eregs *regs);
-int int1a_handler(struct eregs *regs);
-
 static void setup_interrupt_handlers(void)
 {
        int i;
 
-       /* The first 16 intXX functions are not BIOS services, 
+       /* The first 16 intXX functions are not BIOS services,
         * but the CPU-generated exceptions ("hardware interrupts")
         */
        for (i = 0; i < 0x10; i++)
                intXX_handler[i] = &intXX_exception_handler;
-       
+
        /* Mark all other intXX calls as unknown first */
        for (i = 0x10; i < 0x100; i++)
        {
@@ -87,16 +108,19 @@ static void setup_interrupt_handlers(void)
                if(!intXX_handler[i])
                {
                        /* Now set the default functions that are actually
-                        * needed to initialize the option roms. This is very
-                        * slick, as it allows us to implement mainboard specific
-                        * interrupt handlers, such as the int15
+                        * needed to initialize the option roms. This is
+                        * very slick, as it allows us to implement mainboard
+                        * specific interrupt handlers, such as the int15.
                         */
                        switch (i) {
+                       case 0x10:
+                               intXX_handler[0x10] = &int10_handler;
+                               break;
                        case 0x12:
                                intXX_handler[0x12] = &int12_handler;
                                break;
-                       case 0x15:
-                               intXX_handler[0x15] = &int15_handler;
+                       case 0x16:
+                               intXX_handler[0x16] = &int16_handler;
                                break;
                        case 0x1a:
                                intXX_handler[0x1a] = &int1a_handler;
@@ -132,23 +156,132 @@ static void setup_realmode_idt(void)
        }
 
        /* Many option ROMs use the hard coded interrupt entry points in the
-        * system bios. So install them at the known locations. 
-        * Only need int10 so far.
+        * system bios. So install them at the known locations.
         */
-       
+
        /* int42 is the relocated int10 */
-       write_idt_stub((void *)0xff065, 0x42); 
+       write_idt_stub((void *)0xff065, 0x42);
+       /* BIOS Int 11 Handler F000:F84D */
+       write_idt_stub((void *)0xff84d, 0x11);
+       /* BIOS Int 12 Handler F000:F841 */
+       write_idt_stub((void *)0xff841, 0x12);
+       /* BIOS Int 13 Handler F000:EC59 */
+       write_idt_stub((void *)0xfec59, 0x13);
+       /* BIOS Int 14 Handler F000:E739 */
+       write_idt_stub((void *)0xfe739, 0x14);
+       /* BIOS Int 15 Handler F000:F859 */
+       write_idt_stub((void *)0xff859, 0x15);
+       /* BIOS Int 16 Handler F000:E82E */
+       write_idt_stub((void *)0xfe82e, 0x16);
+       /* BIOS Int 17 Handler F000:EFD2 */
+       write_idt_stub((void *)0xfefd2, 0x17);
+       /* ROM BIOS Int 1A Handler F000:FE6E */
+       write_idt_stub((void *)0xffe6e, 0x1a);
 }
 
+#if CONFIG_FRAMEBUFFER_SET_VESA_MODE
+static u8 vbe_get_mode_info(vbe_mode_info_t * mode_info)
+{
+       printk(BIOS_DEBUG, "Getting information about VESA mode %04x\n",
+               mode_info->video_mode);
+       char *buffer = (char *)&__buffer;
+       u16 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
+       u16 buffer_adr = ((unsigned long)buffer) & 0xffff;
+       realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000,
+                       mode_info->video_mode, 0x0000, buffer_seg, buffer_adr);
+       memcpy(mode_info->mode_info_block, buffer, sizeof(vbe_mode_info_t));
+       return 0;
+}
+
+static u8 vbe_set_mode(vbe_mode_info_t * mode_info)
+{
+       printk(BIOS_DEBUG, "Setting VESA mode %04x\n", mode_info->video_mode);
+       // request linear framebuffer mode
+       mode_info->video_mode |= (1 << 14);
+       // request clearing of framebuffer
+       mode_info->video_mode &= ~(1 << 15);
+       realmode_interrupt(0x10, VESA_SET_MODE, mode_info->video_mode,
+                       0x0000, 0x0000, 0x0000, 0x0000);
+       return 0;
+}
+
+vbe_mode_info_t mode_info;
+
+/* These two functions could probably even be generic between
+ * yabel and x86 native. TBD later.
+ */
+void vbe_set_graphics(void)
+{
+       mode_info.video_mode = (1 << 14) | CONFIG_FRAMEBUFFER_VESA_MODE;
+       vbe_get_mode_info(&mode_info);
+       unsigned char *framebuffer =
+               (unsigned char *)mode_info.vesa.phys_base_ptr;
+       printk(BIOS_DEBUG, "framebuffer: %p\n", framebuffer);
+       vbe_set_mode(&mode_info);
+#if CONFIG_BOOTSPLASH
+       struct jpeg_decdata *decdata;
+       decdata = malloc(sizeof(*decdata));
+       unsigned char *jpeg = cbfs_find_file("bootsplash.jpg",
+                                               CBFS_TYPE_BOOTSPLASH);
+       if (!jpeg) {
+               return;
+       }
+       int ret = 0;
+       ret = jpeg_decode(jpeg, framebuffer, 1024, 768, 16, decdata);
+#endif
+}
+
+void vbe_textmode_console(void)
+{
+       delay(2);
+       realmode_interrupt(0x10, 0x0003, 0x0000, 0x0000,
+                               0x0000, 0x0000, 0x0000);
+}
+
+void fill_lb_framebuffer(struct lb_framebuffer *framebuffer)
+{
+       framebuffer->physical_address = mode_info.vesa.phys_base_ptr;
+
+       framebuffer->x_resolution = le16_to_cpu(mode_info.vesa.x_resolution);
+       framebuffer->y_resolution = le16_to_cpu(mode_info.vesa.y_resolution);
+       framebuffer->bytes_per_line =
+                               le16_to_cpu(mode_info.vesa.bytes_per_scanline);
+       framebuffer->bits_per_pixel = mode_info.vesa.bits_per_pixel;
+
+       framebuffer->red_mask_pos = mode_info.vesa.red_mask_pos;
+       framebuffer->red_mask_size = mode_info.vesa.red_mask_size;
+
+       framebuffer->green_mask_pos = mode_info.vesa.green_mask_pos;
+       framebuffer->green_mask_size = mode_info.vesa.green_mask_size;
+
+       framebuffer->blue_mask_pos = mode_info.vesa.blue_mask_pos;
+       framebuffer->blue_mask_size = mode_info.vesa.blue_mask_size;
+
+       framebuffer->reserved_mask_pos = mode_info.vesa.reserved_mask_pos;
+       framebuffer->reserved_mask_size = mode_info.vesa.reserved_mask_size;
+}
+#endif
+
 void run_bios(struct device *dev, unsigned long addr)
 {
-       /* clear vga bios data area */
-       memset((void *)0x400, 0, 0x200);
+       u32 num_dev = (dev->bus->secondary << 8) | dev->path.pci.devfn;
+
+       /* Setting up required hardware.
+        * Removing this will cause random illegal instruction exceptions
+        * in some option roms.
+        */
+       setup_i8259();
+
+       /* Set up BIOS Data Area */
+       setup_bda();
+
+       /* Set up some legacy information in the F segment */
+       setup_rombios();
 
        /* Set up C interrupt handlers */
        setup_interrupt_handlers();
 
-       /* Setting up realmode IDT */
+       /* Set up real-mode IDT */
        setup_realmode_idt();
 
        memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
@@ -156,10 +289,98 @@ void run_bios(struct device *dev, unsigned long addr)
                        (u32)&__realmode_code_size);
 
        printk(BIOS_DEBUG, "Calling Option ROM...\n");
-       run_optionrom((dev->bus->secondary << 8) | dev->path.pci.devfn);
+       /* TODO ES:DI Pointer to System BIOS PnP Installation Check Structure */
+       /* Option ROM entry point is at OPROM start + 3 */
+       realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0, 0x0);
        printk(BIOS_DEBUG, "... Option ROM returned.\n");
+
+#if CONFIG_FRAMEBUFFER_SET_VESA_MODE
+       vbe_set_graphics();
+#endif
+}
+
+#if CONFIG_GEODE_VSA
+#include <cpu/amd/lxdef.h>
+#include <cpu/amd/vr.h>
+#include <cbfs.h>
+
+#define VSA2_BUFFER            0x60000
+#define VSA2_ENTRY_POINT       0x60020
+
+// TODO move to a header file.
+void do_vsmbios(void);
+
+/* VSA virtual register helper */
+static u32 VSA_vrRead(u16 classIndex)
+{
+       u32 eax, ebx, ecx, edx;
+       asm volatile (
+               "movw   $0x0AC1C, %%dx\n"
+               "orl    $0x0FC530000, %%eax\n"
+               "outl   %%eax, %%dx\n"
+               "addb   $2, %%dl\n"
+               "inw    %%dx, %%ax\n"
+               : "=a" (eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
+               : "a"(classIndex)
+       );
+
+       return eax;
+}
+
+void do_vsmbios(void)
+{
+       printk(BIOS_DEBUG, "Preparing for VSA...\n");
+
+       /* Set up C interrupt handlers */
+       setup_interrupt_handlers();
+
+       /* Setting up realmode IDT */
+       setup_realmode_idt();
+
+       memcpy(REALMODE_BASE, &__realmode_code, (size_t)&__realmode_code_size);
+       printk(BIOS_SPEW, "VSA: Real mode stub @%p: %d bytes\n", REALMODE_BASE,
+                       (u32)&__realmode_code_size);
+
+       if ((unsigned int)cbfs_load_stage("vsa") != VSA2_ENTRY_POINT) {
+               printk(BIOS_ERR, "Failed to load VSA.\n");
+               return;
+       }
+
+       unsigned char *buf = (unsigned char *)VSA2_BUFFER;
+       printk(BIOS_DEBUG, "VSA: Buffer @%p *[0k]=%02x\n", buf, buf[0]);
+       printk(BIOS_DEBUG, "VSA: Signature *[0x20-0x23] is %02x:%02x:%02x:%02x\n",
+                    buf[0x20], buf[0x21], buf[0x22], buf[0x23]);
+
+       /* Check for code to emit POST code at start of VSA. */
+       if ((buf[0x20] != 0xb0) || (buf[0x21] != 0x10) ||
+           (buf[0x22] != 0xe6) || (buf[0x23] != 0x80)) {
+               printk(BIOS_WARNING, "VSA: Signature incorrect. Install failed.\n");
+               return;
+       }
+
+       printk(BIOS_DEBUG, "Calling VSA module...\n");
+
+       /* ECX gets SMM, EDX gets SYSMEM */
+       realmode_call(VSA2_ENTRY_POINT, 0x0, 0x0, MSR_GLIU0_SMM,
+                       MSR_GLIU0_SYSMEM, 0x0, 0x0);
+
+       printk(BIOS_DEBUG, "... VSA module returned.\n");
+
+       /* Restart timer 1 */
+       outb(0x56, 0x43);
+       outb(0x12, 0x41);
+
+       /* Check that VSA is running OK */
+       if (VSA_vrRead(SIGNATURE) == VSA2_SIGNATURE)
+               printk(BIOS_DEBUG, "VSM: VSA2 VR signature verified.\n");
+       else
+               printk(BIOS_ERR, "VSM: VSA2 VR signature not valid. Install failed.\n");
 }
+#endif
 
+/* interrupt_handler() is called from assembler code only,
+ * so there is no use in putting the prototype into a header file.
+ */
 int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
            u32 gsfs, u32 dses,
            u32 edi, u32 esi,
@@ -186,6 +407,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
        cs = cs_ip >> 16;
        flags = stackflags;
 
+#if CONFIG_REALMODE_DEBUG
        printk(BIOS_DEBUG, "oprom: INT# 0x%x\n", intnumber);
        printk(BIOS_DEBUG, "oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
                      eax, ebx, ecx, edx);
@@ -193,6 +415,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
                     ebp, esp, edi, esi);
        printk(BIOS_DEBUG, "oprom:  ip: %04x      cs: %04x   flags: %08x\n",
                     ip, cs, flags);
+#endif
 
        // Fetch arguments from the stack and put them into
        // a structure that we want to pass on to our sub interrupt
@@ -220,7 +443,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
        // will later pop them.
        // What happens here is that we force (volatile!) changing
        // the values of the parameters of this function. We do this
-       // because we know that they stay alive on the stack after 
+       // because we know that they stay alive on the stack after
        // we leave this function. Don't say this is bollocks.
        *(volatile u32 *)&eax = reg_info.eax;
        *(volatile u32 *)&ecx = reg_info.ecx;
@@ -232,7 +455,7 @@ int __attribute__((regparm(0))) interrupt_handler(u32 intnumber,
 
        /* Pass errors back to our caller via the CARRY flag */
        if (ret) {
-               printk(BIOS_DEBUG,"error!\n");
+               printk(BIOS_DEBUG,"int%02x call returned error.\n", intnumber);
                flags |= 1;  // error: set carry
        }else{
                flags &= ~1; // no error: clear carry