blob: d6a297e331a3031e9a2e8be6dac97bd17defdf0c [file] [log] [blame]
Kevin O'Connordab0a742013-12-03 11:50:49 -05001// 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'Connor940fc1f2014-01-15 13:42:50 -050012#include "hw/serialio.h" // serial_debug_preinit
Kevin O'Connordab0a742013-12-03 11:50:49 -050013#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
Kevin O'Connor2f2ec112016-08-05 11:14:58 -040017#include "vgabios.h" // SET_VGA
Kevin O'Connordab0a742013-12-03 11:50:49 -050018#include "vgahw.h" // vgahw_setup
Kevin O'Connor2f2ec112016-08-05 11:14:58 -040019#include "vgautil.h" // swcursor_check_event
Kevin O'Connordab0a742013-12-03 11:50:49 -050020
Kevin O'Connorfe2cbf62014-04-14 10:46:34 -040021// Type of emulator platform - for dprintf with certain compile options.
22int PlatformRunningOn VAR16;
23
Kevin O'Connordab0a742013-12-03 11:50:49 -050024
25/****************************************************************
26 * PCI Data
27 ****************************************************************/
28
29struct pci_data rom_pci_data VAR16 VISIBLE16 = {
30 .signature = PCI_ROM_SIGNATURE,
31 .vendor = CONFIG_VGA_VID,
32 .device = CONFIG_VGA_DID,
33 .dlen = 0x18,
34 .class_hi = 0x300,
35 .irevision = 1,
36 .type = PCIROM_CODETYPE_X86,
37 .indicator = 0x80,
38};
39
40
41/****************************************************************
42 * PMM call and extra stack setup
43 ****************************************************************/
44
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020045u32
46allocate_pmm(u32 size, int highmem, int aligned)
Kevin O'Connordab0a742013-12-03 11:50:49 -050047{
Kevin O'Connor8f82a4f2014-04-06 18:48:39 -040048 u32 pmmscan;
49 for (pmmscan=0; pmmscan < BUILD_BIOS_SIZE; pmmscan+=16) {
50 struct pmmheader *pmm = (void*)pmmscan;
51 if (GET_FARVAR(SEG_BIOS, pmm->signature) != PMM_SIGNATURE)
Kevin O'Connordab0a742013-12-03 11:50:49 -050052 continue;
Kevin O'Connor8f82a4f2014-04-06 18:48:39 -040053 if (checksum_far(SEG_BIOS, pmm, GET_FARVAR(SEG_BIOS, pmm->length)))
Kevin O'Connordab0a742013-12-03 11:50:49 -050054 continue;
Kevin O'Connor8f82a4f2014-04-06 18:48:39 -040055 struct segoff_s entry = GET_FARVAR(SEG_BIOS, pmm->entry);
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020056 dprintf(1, "Attempting to allocate %u bytes %s via pmm call to %04x:%04x\n"
57 , size, highmem ? "highmem" : "lowmem"
Kevin O'Connordab0a742013-12-03 11:50:49 -050058 , entry.seg, entry.offset);
59 u16 res1, res2;
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020060 u16 flags = 8 |
61 ( highmem ? 2 : 1 )|
62 ( aligned ? 4 : 0 );
63 size >>= 4;
Kevin O'Connordab0a742013-12-03 11:50:49 -050064 asm volatile(
65 "pushl %0\n"
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020066 "pushw %2\n" // flags
Kevin O'Connordab0a742013-12-03 11:50:49 -050067 "pushl $0xffffffff\n" // Anonymous handle
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020068 "pushl %1\n" // size
Kevin O'Connordab0a742013-12-03 11:50:49 -050069 "pushw $0x00\n" // PMM allocation request
70 "lcallw *12(%%esp)\n"
71 "addl $16, %%esp\n"
72 "cli\n"
73 "cld\n"
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020074 : "+r" (entry.segoff), "+r" (size), "+r" (flags),
75 "=a" (res1), "=d" (res2) : : "cc", "memory");
Kevin O'Connordab0a742013-12-03 11:50:49 -050076 u32 res = res1 | (res2 << 16);
77 if (!res || res == PMM_FUNCTION_NOT_SUPPORTED)
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020078 return 0;
79 return res;
Kevin O'Connordab0a742013-12-03 11:50:49 -050080 }
Gerd Hoffmanne1a95792018-06-15 07:55:47 +020081 return 0;
82}
83
84u16 ExtraStackSeg VAR16 VISIBLE16;
85
86static void
87allocate_extra_stack(void)
88{
89 if (!CONFIG_VGA_ALLOCATE_EXTRA_STACK)
90 return;
91 u32 res = allocate_pmm(CONFIG_VGA_EXTRA_STACK_SIZE, 0, 0);
92 if (!res)
93 return;
94 dprintf(1, "VGA stack allocated at %x\n", res);
95 SET_VGA(ExtraStackSeg, res >> 4);
96 extern void entry_10_extrastack(void);
97 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10_extrastack));
98 return;
Kevin O'Connordab0a742013-12-03 11:50:49 -050099}
100
101
102/****************************************************************
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400103 * Timer hook
104 ****************************************************************/
105
106struct segoff_s Timer_Hook_Resume VAR16 VISIBLE16;
107
108void VISIBLE16
109handle_timer_hook(void)
110{
Kevin O'Connor774f5cd2016-08-04 17:02:16 -0400111 swcursor_check_event();
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400112}
113
114static void
115hook_timer_irq(void)
116{
117 if (!CONFIG_VGA_EMULATE_TEXT)
118 return;
119 extern void entry_timer_hook(void);
120 extern void entry_timer_hook_extrastack(void);
121 struct segoff_s oldirq = GET_IVT(0x08);
122 struct segoff_s newirq = SEGOFF(get_global_seg(), (u32)entry_timer_hook);
123 if (CONFIG_VGA_ALLOCATE_EXTRA_STACK && GET_GLOBAL(ExtraStackSeg))
124 newirq = SEGOFF(get_global_seg(), (u32)entry_timer_hook_extrastack);
125 dprintf(1, "Hooking hardware timer irq (old=%x new=%x)\n"
126 , oldirq.segoff, newirq.segoff);
127 SET_VGA(Timer_Hook_Resume, oldirq);
128 SET_IVT(0x08, newirq);
129}
130
131
132/****************************************************************
Kevin O'Connordab0a742013-12-03 11:50:49 -0500133 * VGA post
134 ****************************************************************/
135
136static void
137init_bios_area(void)
138{
139 // init detected hardware BIOS Area
140 // set 80x25 color (not clear from RBIL but usual)
141 set_equipment_flags(0x30, 0x20);
142
Kevin O'Connordab0a742013-12-03 11:50:49 -0500143 // Set the basic modeset options
144 SET_BDA(modeset_ctl, 0x51);
145
Kevin O'Connorc5acee42014-10-23 15:54:59 -0400146 SET_BDA(dcc_index, CONFIG_VGA_STDVGA_PORTS ? 0x08 : 0xff);
Kevin O'Connor5b89d952014-10-23 13:04:17 -0400147
148 // FIXME
149 SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
150 SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
Kevin O'Connordab0a742013-12-03 11:50:49 -0500151}
152
153int VgaBDF VAR16 = -1;
154int HaveRunInit VAR16;
155
156void VISIBLE16
157vga_post(struct bregs *regs)
158{
Kevin O'Connor940fc1f2014-01-15 13:42:50 -0500159 serial_debug_preinit();
Kevin O'Connordab0a742013-12-03 11:50:49 -0500160 dprintf(1, "Start SeaVGABIOS (version %s)\n", VERSION);
Kevin O'Connorefd70a52015-10-13 15:44:25 -0400161 dprintf(1, "VGABUILD: %s\n", BUILDINFO);
Kevin O'Connordab0a742013-12-03 11:50:49 -0500162 debug_enter(regs, DEBUG_VGA_POST);
163
164 if (CONFIG_VGA_PCI && !GET_GLOBAL(HaveRunInit)) {
165 u16 bdf = regs->ax;
166 if ((pci_config_readw(bdf, PCI_VENDOR_ID)
167 == GET_GLOBAL(rom_pci_data.vendor))
168 && (pci_config_readw(bdf, PCI_DEVICE_ID)
169 == GET_GLOBAL(rom_pci_data.device)))
170 SET_VGA(VgaBDF, bdf);
171 }
172
173 int ret = vgahw_setup();
174 if (ret) {
175 dprintf(1, "Failed to initialize VGA hardware. Exiting.\n");
176 return;
177 }
178
179 if (GET_GLOBAL(HaveRunInit))
180 return;
181
182 init_bios_area();
183
Kevin O'Connorefbf4d62014-02-09 11:50:21 -0500184 if (CONFIG_VGA_STDVGA_PORTS)
185 stdvga_build_video_param();
Kevin O'Connordab0a742013-12-03 11:50:49 -0500186
187 extern void entry_10(void);
188 SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
189
190 allocate_extra_stack();
191
Kevin O'Connorb4eb6fc2014-10-17 22:15:42 -0400192 hook_timer_irq();
193
Kevin O'Connordab0a742013-12-03 11:50:49 -0500194 SET_VGA(HaveRunInit, 1);
195
196 // Fixup checksum
197 extern u8 _rom_header_size, _rom_header_checksum;
198 SET_VGA(_rom_header_checksum, 0);
199 u8 sum = -checksum_far(get_global_seg(), 0,
200 GET_GLOBAL(_rom_header_size) * 512);
201 SET_VGA(_rom_header_checksum, sum);
202}