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