Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 1 | // Main VGA bios initialization |
| 2 | // |
| 3 | // Copyright (C) 2009-2013 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2001-2008 the LGPL VGABios developers Team |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 7 | |
| 8 | #include "biosvar.h" // SET_BDA |
| 9 | #include "bregs.h" // struct bregs |
| 10 | #include "hw/pci.h" // pci_config_readw |
| 11 | #include "hw/pci_regs.h" // PCI_VENDOR_ID |
Kevin O'Connor | 940fc1f | 2014-01-15 13:42:50 -0500 | [diff] [blame] | 12 | #include "hw/serialio.h" // serial_debug_preinit |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 13 | #include "output.h" // dprintf |
| 14 | #include "std/optionrom.h" // struct pci_data |
| 15 | #include "std/pmm.h" // struct pmmheader |
| 16 | #include "string.h" // checksum_far |
| 17 | #include "util.h" // VERSION |
Kevin O'Connor | 6397790 | 2014-10-23 16:24:36 -0400 | [diff] [blame] | 18 | #include "vgabios.h" // video_save_pointer_table |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 19 | #include "vgahw.h" // vgahw_setup |
| 20 | |
Kevin O'Connor | 6397790 | 2014-10-23 16:24:36 -0400 | [diff] [blame] | 21 | struct video_save_pointer_s video_save_pointer_table VAR16; |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 22 | |
Kevin O'Connor | 6397790 | 2014-10-23 16:24:36 -0400 | [diff] [blame] | 23 | struct video_param_s video_param_table[29] VAR16; |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 24 | |
Kevin O'Connor | fe2cbf6 | 2014-04-14 10:46:34 -0400 | [diff] [blame] | 25 | // Type of emulator platform - for dprintf with certain compile options. |
| 26 | int PlatformRunningOn VAR16; |
| 27 | |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 28 | |
| 29 | /**************************************************************** |
| 30 | * PCI Data |
| 31 | ****************************************************************/ |
| 32 | |
| 33 | struct pci_data rom_pci_data VAR16 VISIBLE16 = { |
| 34 | .signature = PCI_ROM_SIGNATURE, |
| 35 | .vendor = CONFIG_VGA_VID, |
| 36 | .device = CONFIG_VGA_DID, |
| 37 | .dlen = 0x18, |
| 38 | .class_hi = 0x300, |
| 39 | .irevision = 1, |
| 40 | .type = PCIROM_CODETYPE_X86, |
| 41 | .indicator = 0x80, |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | /**************************************************************** |
| 46 | * PMM call and extra stack setup |
| 47 | ****************************************************************/ |
| 48 | |
| 49 | u16 ExtraStackSeg VAR16 VISIBLE16; |
| 50 | |
| 51 | static void |
| 52 | allocate_extra_stack(void) |
| 53 | { |
| 54 | if (!CONFIG_VGA_ALLOCATE_EXTRA_STACK) |
| 55 | return; |
Kevin O'Connor | 8f82a4f | 2014-04-06 18:48:39 -0400 | [diff] [blame] | 56 | u32 pmmscan; |
| 57 | for (pmmscan=0; pmmscan < BUILD_BIOS_SIZE; pmmscan+=16) { |
| 58 | struct pmmheader *pmm = (void*)pmmscan; |
| 59 | if (GET_FARVAR(SEG_BIOS, pmm->signature) != PMM_SIGNATURE) |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 60 | continue; |
Kevin O'Connor | 8f82a4f | 2014-04-06 18:48:39 -0400 | [diff] [blame] | 61 | if (checksum_far(SEG_BIOS, pmm, GET_FARVAR(SEG_BIOS, pmm->length))) |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 62 | continue; |
Kevin O'Connor | 8f82a4f | 2014-04-06 18:48:39 -0400 | [diff] [blame] | 63 | struct segoff_s entry = GET_FARVAR(SEG_BIOS, pmm->entry); |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 64 | dprintf(1, "Attempting to allocate VGA stack via pmm call to %04x:%04x\n" |
| 65 | , entry.seg, entry.offset); |
| 66 | u16 res1, res2; |
| 67 | asm volatile( |
| 68 | "pushl %0\n" |
| 69 | "pushw $(8|1)\n" // Permanent low memory request |
| 70 | "pushl $0xffffffff\n" // Anonymous handle |
Kevin O'Connor | e8436b5 | 2014-02-18 13:48:09 -0500 | [diff] [blame] | 71 | "pushl $" __stringify(CONFIG_VGA_EXTRA_STACK_SIZE/16) "\n" |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 72 | "pushw $0x00\n" // PMM allocation request |
| 73 | "lcallw *12(%%esp)\n" |
| 74 | "addl $16, %%esp\n" |
| 75 | "cli\n" |
| 76 | "cld\n" |
| 77 | : "+r" (entry.segoff), "=a" (res1), "=d" (res2) : : "cc", "memory"); |
| 78 | u32 res = res1 | (res2 << 16); |
| 79 | if (!res || res == PMM_FUNCTION_NOT_SUPPORTED) |
| 80 | return; |
| 81 | dprintf(1, "VGA stack allocated at %x\n", res); |
| 82 | SET_VGA(ExtraStackSeg, res >> 4); |
| 83 | extern void entry_10_extrastack(void); |
| 84 | SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10_extrastack)); |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /**************************************************************** |
Kevin O'Connor | b4eb6fc | 2014-10-17 22:15:42 -0400 | [diff] [blame] | 91 | * Timer hook |
| 92 | ****************************************************************/ |
| 93 | |
| 94 | struct segoff_s Timer_Hook_Resume VAR16 VISIBLE16; |
| 95 | |
| 96 | void VISIBLE16 |
| 97 | handle_timer_hook(void) |
| 98 | { |
| 99 | if (!vga_emulate_text()) |
| 100 | return; |
| 101 | vgafb_set_swcursor(GET_BDA(timer_counter) % 18 < 9); |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | hook_timer_irq(void) |
| 106 | { |
| 107 | if (!CONFIG_VGA_EMULATE_TEXT) |
| 108 | return; |
| 109 | extern void entry_timer_hook(void); |
| 110 | extern void entry_timer_hook_extrastack(void); |
| 111 | struct segoff_s oldirq = GET_IVT(0x08); |
| 112 | struct segoff_s newirq = SEGOFF(get_global_seg(), (u32)entry_timer_hook); |
| 113 | if (CONFIG_VGA_ALLOCATE_EXTRA_STACK && GET_GLOBAL(ExtraStackSeg)) |
| 114 | newirq = SEGOFF(get_global_seg(), (u32)entry_timer_hook_extrastack); |
| 115 | dprintf(1, "Hooking hardware timer irq (old=%x new=%x)\n" |
| 116 | , oldirq.segoff, newirq.segoff); |
| 117 | SET_VGA(Timer_Hook_Resume, oldirq); |
| 118 | SET_IVT(0x08, newirq); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /**************************************************************** |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 123 | * VGA post |
| 124 | ****************************************************************/ |
| 125 | |
| 126 | static void |
| 127 | init_bios_area(void) |
| 128 | { |
| 129 | // init detected hardware BIOS Area |
| 130 | // set 80x25 color (not clear from RBIL but usual) |
| 131 | set_equipment_flags(0x30, 0x20); |
| 132 | |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 133 | // Set the basic modeset options |
| 134 | SET_BDA(modeset_ctl, 0x51); |
| 135 | |
Kevin O'Connor | c5acee4 | 2014-10-23 15:54:59 -0400 | [diff] [blame] | 136 | SET_BDA(dcc_index, CONFIG_VGA_STDVGA_PORTS ? 0x08 : 0xff); |
Kevin O'Connor | 5b89d95 | 2014-10-23 13:04:17 -0400 | [diff] [blame] | 137 | SET_BDA(video_savetable |
| 138 | , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table)); |
| 139 | |
| 140 | // FIXME |
| 141 | SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but... |
| 142 | SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but... |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int VgaBDF VAR16 = -1; |
| 146 | int HaveRunInit VAR16; |
| 147 | |
| 148 | void VISIBLE16 |
| 149 | vga_post(struct bregs *regs) |
| 150 | { |
Kevin O'Connor | 940fc1f | 2014-01-15 13:42:50 -0500 | [diff] [blame] | 151 | serial_debug_preinit(); |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 152 | dprintf(1, "Start SeaVGABIOS (version %s)\n", VERSION); |
Kevin O'Connor | efd70a5 | 2015-10-13 15:44:25 -0400 | [diff] [blame] | 153 | dprintf(1, "VGABUILD: %s\n", BUILDINFO); |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 154 | debug_enter(regs, DEBUG_VGA_POST); |
| 155 | |
| 156 | if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) { |
| 157 | u16 bdf = regs->ax; |
| 158 | if ((pci_config_readw(bdf, PCI_VENDOR_ID) |
| 159 | == GET_GLOBAL(rom_pci_data.vendor)) |
| 160 | && (pci_config_readw(bdf, PCI_DEVICE_ID) |
| 161 | == GET_GLOBAL(rom_pci_data.device))) |
| 162 | SET_VGA(VgaBDF, bdf); |
| 163 | } |
| 164 | |
| 165 | int ret = vgahw_setup(); |
| 166 | if (ret) { |
| 167 | dprintf(1, "Failed to initialize VGA hardware. Exiting.\n"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (GET_GLOBAL(HaveRunInit)) |
| 172 | return; |
| 173 | |
| 174 | init_bios_area(); |
| 175 | |
| 176 | SET_VGA(video_save_pointer_table.videoparam |
| 177 | , SEGOFF(get_global_seg(), (u32)video_param_table)); |
Kevin O'Connor | efbf4d6 | 2014-02-09 11:50:21 -0500 | [diff] [blame] | 178 | if (CONFIG_VGA_STDVGA_PORTS) |
| 179 | stdvga_build_video_param(); |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 180 | |
| 181 | extern void entry_10(void); |
| 182 | SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10)); |
| 183 | |
| 184 | allocate_extra_stack(); |
| 185 | |
Kevin O'Connor | b4eb6fc | 2014-10-17 22:15:42 -0400 | [diff] [blame] | 186 | hook_timer_irq(); |
| 187 | |
Kevin O'Connor | dab0a74 | 2013-12-03 11:50:49 -0500 | [diff] [blame] | 188 | SET_VGA(HaveRunInit, 1); |
| 189 | |
| 190 | // Fixup checksum |
| 191 | extern u8 _rom_header_size, _rom_header_checksum; |
| 192 | SET_VGA(_rom_header_checksum, 0); |
| 193 | u8 sum = -checksum_far(get_global_seg(), 0, |
| 194 | GET_GLOBAL(_rom_header_size) * 512); |
| 195 | SET_VGA(_rom_header_checksum, sum); |
| 196 | } |