1 /* $NetBSD: vga_post.c,v 1.18 2011/02/12 19:13:30 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: vga_post.c,v 1.18 2011/02/12 19:13:30 jmcneill Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/device.h> 37 #include <sys/kmem.h> 38 #include <uvm/uvm.h> 39 #include <uvm/uvm_page.h> 40 41 #include <machine/pio.h> 42 43 #include <x86/vga_post.h> 44 45 #include <x86emu/x86emu.h> 46 #include <x86emu/x86emu_i8254.h> 47 #include <x86emu/x86emu_regs.h> 48 49 #include "opt_ddb.h" 50 51 #define BASE_MEMORY 65536 /* How much memory to allocate in Real Mode */ 52 53 struct vga_post { 54 struct X86EMU emu; 55 vaddr_t sys_image; 56 uint32_t initial_eax; 57 struct x86emu_i8254 i8254; 58 uint8_t bios_data[PAGE_SIZE]; 59 struct pglist ram_backing; 60 }; 61 62 #ifdef DDB 63 static struct vga_post *ddb_vgapostp; 64 void ddb_vgapost(void); 65 #endif 66 67 static uint8_t 68 vm86_emu_inb(struct X86EMU *emu, uint16_t port) 69 { 70 struct vga_post *sc = emu->sys_private; 71 72 if (port == 0xb2) /* APM scratch register */ 73 return 0; 74 75 if (port >= 0x80 && port < 0x88) /* POST status register */ 76 return 0; 77 78 if (x86emu_i8254_claim_port(&sc->i8254, port) && 0) { 79 return x86emu_i8254_inb(&sc->i8254, port); 80 } else 81 return inb(port); 82 } 83 84 static uint16_t 85 vm86_emu_inw(struct X86EMU *emu, uint16_t port) 86 { 87 if (port >= 0x80 && port < 0x88) /* POST status register */ 88 return 0; 89 90 return inw(port); 91 } 92 93 static uint32_t 94 vm86_emu_inl(struct X86EMU *emu, uint16_t port) 95 { 96 if (port >= 0x80 && port < 0x88) /* POST status register */ 97 return 0; 98 99 return inl(port); 100 } 101 102 static void 103 vm86_emu_outb(struct X86EMU *emu, uint16_t port, uint8_t val) 104 { 105 struct vga_post *sc = emu->sys_private; 106 107 if (port == 0xb2) /* APM scratch register */ 108 return; 109 110 if (port >= 0x80 && port < 0x88) /* POST status register */ 111 return; 112 113 if (x86emu_i8254_claim_port(&sc->i8254, port) && 0) { 114 x86emu_i8254_outb(&sc->i8254, port, val); 115 } else 116 outb(port, val); 117 } 118 119 static void 120 vm86_emu_outw(struct X86EMU *emu, uint16_t port, uint16_t val) 121 { 122 if (port >= 0x80 && port < 0x88) /* POST status register */ 123 return; 124 125 outw(port, val); 126 } 127 128 static void 129 vm86_emu_outl(struct X86EMU *emu, uint16_t port, uint32_t val) 130 { 131 if (port >= 0x80 && port < 0x88) /* POST status register */ 132 return; 133 134 outl(port, val); 135 } 136 137 struct vga_post * 138 vga_post_init(int bus, int device, int function) 139 { 140 struct vga_post *sc; 141 vaddr_t iter; 142 struct vm_page *pg; 143 vaddr_t sys_image, sys_bios_data; 144 int err; 145 146 sys_image = uvm_km_alloc(kernel_map, 1024 * 1024, 0, UVM_KMF_VAONLY); 147 if (sys_image == 0) { 148 return NULL; 149 } 150 sc = kmem_alloc(sizeof(*sc), KM_SLEEP); 151 sc->sys_image = sys_image; 152 sc->emu.sys_private = sc; 153 154 err = uvm_pglistalloc(BASE_MEMORY, 0, (paddr_t)-1, 0, 0, 155 &sc->ram_backing, BASE_MEMORY/PAGE_SIZE, 1); 156 if (err) { 157 uvm_km_free(kernel_map, sc->sys_image, 158 1024 * 1024, UVM_KMF_VAONLY); 159 kmem_free(sc, sizeof(*sc)); 160 return NULL; 161 } 162 163 /* 164 * Map and copy BIOS data. 165 */ 166 sys_bios_data = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_VAONLY); 167 if (sys_bios_data == 0) { 168 return NULL; 169 } 170 pmap_kenter_pa(sys_bios_data, 0, VM_PROT_READ, 0); 171 pmap_update(pmap_kernel()); 172 173 memcpy((void *)sc->bios_data, (void *)sys_bios_data, PAGE_SIZE); 174 175 pmap_kremove(sys_bios_data, PAGE_SIZE); 176 pmap_update(pmap_kernel()); 177 uvm_km_free(kernel_map, sys_bios_data, PAGE_SIZE, UVM_KMF_VAONLY); 178 179 /* 180 * Map 0 .. 64KB and 640KB .. 1MB ranges. 181 */ 182 iter = 0; 183 TAILQ_FOREACH(pg, &sc->ram_backing, pageq.queue) { 184 pmap_kenter_pa(sc->sys_image + iter, VM_PAGE_TO_PHYS(pg), 185 VM_PROT_READ | VM_PROT_WRITE, 0); 186 iter += PAGE_SIZE; 187 } 188 KASSERT(iter == BASE_MEMORY); 189 190 for (iter = 640 * 1024; iter < 1024 * 1024; iter += PAGE_SIZE) { 191 pmap_kenter_pa(sc->sys_image + iter, iter, 192 VM_PROT_READ | VM_PROT_WRITE, 0); 193 } 194 pmap_update(pmap_kernel()); 195 196 memset(&sc->emu, 0, sizeof(sc->emu)); 197 X86EMU_init_default(&sc->emu); 198 sc->emu.emu_inb = vm86_emu_inb; 199 sc->emu.emu_inw = vm86_emu_inw; 200 sc->emu.emu_inl = vm86_emu_inl; 201 sc->emu.emu_outb = vm86_emu_outb; 202 sc->emu.emu_outw = vm86_emu_outw; 203 sc->emu.emu_outl = vm86_emu_outl; 204 205 sc->emu.mem_base = (char *)sc->sys_image; 206 sc->emu.mem_size = 1024 * 1024; 207 208 sc->initial_eax = bus * 256 + device * 8 + function; 209 #ifdef DDB 210 ddb_vgapostp = sc; 211 #endif 212 return sc; 213 } 214 215 void 216 vga_post_call(struct vga_post *sc) 217 { 218 sc->emu.x86.R_EAX = sc->initial_eax; 219 sc->emu.x86.R_EDX = 0x00000080; 220 sc->emu.x86.R_DS = 0x0040; 221 sc->emu.x86.register_flags = 0x3200; 222 223 memcpy((void *)sc->sys_image, sc->bios_data, PAGE_SIZE); 224 225 /* stack is at the end of the first 64KB */ 226 sc->emu.x86.R_SS = 0; 227 sc->emu.x86.R_ESP = 0; 228 229 x86emu_i8254_init(&sc->i8254, nanotime); 230 231 /* Jump straight into the VGA BIOS POST code */ 232 X86EMU_exec_call(&sc->emu, 0xc000, 0x0003); 233 } 234 235 void 236 vga_post_set_vbe(struct vga_post *sc, uint16_t vbemode) 237 { 238 sc->emu.x86.R_EAX = sc->initial_eax; 239 sc->emu.x86.R_EDX = 0x00000080; 240 sc->emu.x86.R_DS = 0x0040; 241 sc->emu.x86.register_flags = 0x3200; 242 243 memcpy((void *)sc->sys_image, sc->bios_data, PAGE_SIZE); 244 245 /* stack is at the end of the first 64KB */ 246 sc->emu.x86.R_SS = 0; 247 sc->emu.x86.R_ESP = 0; 248 249 x86emu_i8254_init(&sc->i8254, nanotime); 250 251 sc->emu.x86.R_EBX = vbemode | 0x4000; 252 sc->emu.x86.R_EAX = 0x4f02; 253 X86EMU_exec_intr(&sc->emu, 0x10); 254 } 255 256 void 257 vga_post_free(struct vga_post *sc) 258 { 259 260 uvm_pglistfree(&sc->ram_backing); 261 pmap_kremove(sc->sys_image, 1024 * 1024); 262 pmap_update(pmap_kernel()); 263 uvm_km_free(kernel_map, sc->sys_image, 1024 * 1024, UVM_KMF_VAONLY); 264 kmem_free(sc, sizeof(*sc)); 265 } 266 267 #ifdef DDB 268 void 269 ddb_vgapost(void) 270 { 271 272 if (ddb_vgapostp) 273 vga_post_call(ddb_vgapostp); 274 else 275 printf("ddb_vgapost: vga_post not initialized\n"); 276 } 277 #endif 278