summaryrefslogtreecommitdiff
path: root/sys/arch/i386
diff options
context:
space:
mode:
authorPaul Irofti <pirofti@cvs.openbsd.org>2009-06-06 00:35:01 +0000
committerPaul Irofti <pirofti@cvs.openbsd.org>2009-06-06 00:35:01 +0000
commitae8d51383026ad58c6f4a80c429d1ea6baeef3f2 (patch)
tree30925450336d18c4ea9ec48e94ea312a02616b1b /sys/arch/i386
parent0beb67fb1755f6972960b884f8640f623226401c (diff)
Add vga bios repost support. Fetched from the NetBSD tree mostly.
Tested on multiple i386 and it works, amd64 works also with a few exceptions that will get fixed. The initial effort of importing was done by oga@, thanks! Lots of testing and debugging by mlarkin@ and me. Okay deraadt@, oga@, mlarkin@.
Diffstat (limited to 'sys/arch/i386')
-rw-r--r--sys/arch/i386/conf/GENERIC3
-rw-r--r--sys/arch/i386/conf/files.i3863
-rw-r--r--sys/arch/i386/include/vga_post.h43
-rw-r--r--sys/arch/i386/pci/vga_post.c231
4 files changed, 278 insertions, 2 deletions
diff --git a/sys/arch/i386/conf/GENERIC b/sys/arch/i386/conf/GENERIC
index 58c27ebc64b..8d2c87edc0b 100644
--- a/sys/arch/i386/conf/GENERIC
+++ b/sys/arch/i386/conf/GENERIC
@@ -1,4 +1,4 @@
-# $OpenBSD: GENERIC,v 1.662 2009/06/03 07:13:48 pirofti Exp $
+# $OpenBSD: GENERIC,v 1.663 2009/06/06 00:35:00 pirofti Exp $
#
# For further information on compiling OpenBSD kernels, see the config(8)
# man page.
@@ -340,6 +340,7 @@ pckbd* at pckbc? # PC keyboard
pms* at pckbc? # PS/2 mouse for wsmouse
pmsi* at pckbc? # PS/2 "Intelli"mouse for wsmouse
vga0 at isa?
+option VGA_POST # we can re-POST video cards
vga* at pci?
pcdisplay0 at isa? # CGA, MDA, EGA, HGA
wsdisplay* at vga?
diff --git a/sys/arch/i386/conf/files.i386 b/sys/arch/i386/conf/files.i386
index 354a4f0083c..ffe4ccef9e1 100644
--- a/sys/arch/i386/conf/files.i386
+++ b/sys/arch/i386/conf/files.i386
@@ -1,4 +1,4 @@
-# $OpenBSD: files.i386,v 1.190 2009/05/12 19:49:36 mglocker Exp $
+# $OpenBSD: files.i386,v 1.191 2009/06/06 00:35:00 pirofti Exp $
#
# new style config file for i386 architecture
#
@@ -103,6 +103,7 @@ include "../../../dev/pci/files.pci"
file arch/i386/pci/pci_machdep.c pci
file arch/i386/pci/pciide_machdep.c pciide
file arch/i386/pci/pcic_pci_machdep.c pcic_pci
+file arch/i386/pci/vga_post.c vga_pci & vga_post
# PCI-Host bridge chipsets
device pchb: pcibus, agpbus
diff --git a/sys/arch/i386/include/vga_post.h b/sys/arch/i386/include/vga_post.h
new file mode 100644
index 00000000000..36a9a2d4680
--- /dev/null
+++ b/sys/arch/i386/include/vga_post.h
@@ -0,0 +1,43 @@
+/* $NetBSD: vga_post.h,v 1.2 2008/03/29 17:40:22 jmcneill Exp $ */
+/* $OpenBSD: vga_post.h,v 1.1 2009/06/06 00:35:00 pirofti Exp $ */
+
+/*-
+ * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _X86_VGA_POST_H_
+#define _X86_VGA_POST_H_
+
+#ifdef _KERNEL
+struct vga_post;
+
+struct vga_post *vga_post_init(int, int, int);
+void vga_post_free(struct vga_post *);
+void vga_post_call(struct vga_post *);
+#endif
+#endif
diff --git a/sys/arch/i386/pci/vga_post.c b/sys/arch/i386/pci/vga_post.c
new file mode 100644
index 00000000000..43f0a86cf47
--- /dev/null
+++ b/sys/arch/i386/pci/vga_post.c
@@ -0,0 +1,231 @@
+/* $NetBSD: vga_post.c,v 1.12 2009/03/15 21:32:36 cegger Exp $ */
+/* $OpenBSD: vga_post.c,v 1.1 2009/06/06 00:35:00 pirofti Exp $ */
+
+/*-
+ * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+#include <sys/param.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+#include <uvm/uvm_extern.h>
+#include <uvm/uvm_page.h>
+
+#include <machine/pio.h>
+
+#include <machine/vga_post.h>
+
+#include <lib/libkern/x86emu.h>
+#include <lib/libkern/x86emu_regs.h>
+
+#define BASE_MEMORY 65536 /* How much memory to allocate in Real Mode */
+
+struct vga_post {
+ struct X86EMU emu;
+ vaddr_t sys_image;
+ uint32_t initial_eax;
+ uint8_t bios_data[PAGE_SIZE];
+ struct pglist ram_backing;
+};
+
+#ifdef DDB
+struct vga_post *ddb_vgapostp;
+void ddb_vgapost(void);
+#endif
+
+static uint8_t
+vm86_emu_inb(struct X86EMU *emu, uint16_t port)
+{
+ if (port == 0xb2) /* APM scratch register */
+ return 0;
+
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return 0;
+
+ return inb(port);
+}
+
+static uint16_t
+vm86_emu_inw(struct X86EMU *emu, uint16_t port)
+{
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return 0;
+
+ return inw(port);
+}
+
+static uint32_t
+vm86_emu_inl(struct X86EMU *emu, uint16_t port)
+{
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return 0;
+
+ return inl(port);
+}
+
+static void
+vm86_emu_outb(struct X86EMU *emu, uint16_t port, uint8_t val)
+{
+ if (port == 0xb2) /* APM scratch register */
+ return;
+
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return;
+
+ outb(port, val);
+}
+
+static void
+vm86_emu_outw(struct X86EMU *emu, uint16_t port, uint16_t val)
+{
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return;
+
+ outw(port, val);
+}
+
+static void
+vm86_emu_outl(struct X86EMU *emu, uint16_t port, uint32_t val)
+{
+ if (port >= 0x80 && port < 0x88) /* POST status register */
+ return;
+
+ outl(port, val);
+}
+
+struct vga_post *
+vga_post_init(int bus, int device, int function)
+{
+ struct vga_post *sc;
+ vaddr_t iter;
+ struct vm_page *pg;
+ vaddr_t sys_image, sys_bios_data;
+ int err;
+
+ sys_bios_data = uvm_km_valloc(kernel_map, PAGE_SIZE);
+ if (sys_bios_data == 0)
+ return NULL;
+
+ sys_image = uvm_km_valloc(kernel_map, 1024 * 1024);
+ if (sys_image == 0) {
+ uvm_km_free(kernel_map, sys_bios_data, PAGE_SIZE);
+ return NULL;
+ }
+ sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
+
+ err = uvm_pglistalloc(BASE_MEMORY, 0, (paddr_t)-1, 0, 0,
+ &sc->ram_backing, BASE_MEMORY/PAGE_SIZE, UVM_PLA_WAITOK);
+ if (err) {
+ uvm_km_free(kernel_map, sc->sys_image, 1024 * 1024);
+ free(sc, M_DEVBUF);
+ return NULL;
+ }
+
+ sc->sys_image = sys_image;
+ sc->emu.sys_private = sc;
+
+ pmap_kenter_pa(sys_bios_data, 0, VM_PROT_READ);
+ pmap_update(pmap_kernel());
+ memcpy((void *)sc->bios_data, (void *)sys_bios_data, PAGE_SIZE);
+ pmap_kremove(sys_bios_data, PAGE_SIZE);
+ uvm_km_free(kernel_map, sys_bios_data, PAGE_SIZE);
+
+ iter = 0;
+ TAILQ_FOREACH(pg, &sc->ram_backing, pageq) {
+ pmap_kenter_pa(sc->sys_image + iter, VM_PAGE_TO_PHYS(pg),
+ VM_PROT_READ | VM_PROT_WRITE);
+ iter += PAGE_SIZE;
+ }
+ KASSERT(iter == 65536);
+
+ for (iter = 640 * 1024; iter < 1024 * 1024; iter += PAGE_SIZE)
+ pmap_kenter_pa(sc->sys_image + iter, iter,
+ VM_PROT_READ | VM_PROT_WRITE);
+ pmap_update(pmap_kernel());
+
+ memset(&sc->emu, 0, sizeof(sc->emu));
+ X86EMU_init_default(&sc->emu);
+ sc->emu.emu_inb = vm86_emu_inb;
+ sc->emu.emu_inw = vm86_emu_inw;
+ sc->emu.emu_inl = vm86_emu_inl;
+ sc->emu.emu_outb = vm86_emu_outb;
+ sc->emu.emu_outw = vm86_emu_outw;
+ sc->emu.emu_outl = vm86_emu_outl;
+
+ sc->emu.mem_base = (char *)sc->sys_image;
+ sc->emu.mem_size = 1024 * 1024;
+
+ sc->initial_eax = bus * 256 + device * 8 + function;
+#ifdef DDB
+ ddb_vgapostp = sc;
+#endif
+ return sc;
+}
+
+void
+vga_post_call(struct vga_post *sc)
+{
+ sc->emu.x86.R_EAX = sc->initial_eax;
+ sc->emu.x86.R_EDX = 0x00000080;
+ sc->emu.x86.R_DS = 0x0040;
+ sc->emu.x86.register_flags = 0x3200;
+
+ memcpy((void *)sc->sys_image, sc->bios_data, PAGE_SIZE);
+
+ /* stack is at the end of the first 64KB */
+ sc->emu.x86.R_SS = 0;
+ sc->emu.x86.R_ESP = 0;
+
+ /* Jump straight into the VGA BIOS POST code */
+ X86EMU_exec_call(&sc->emu, 0xc000, 0x0003);
+}
+
+void
+vga_post_free(struct vga_post *sc)
+{
+ uvm_pglistfree(&sc->ram_backing);
+ pmap_kremove(sc->sys_image, 1024 * 1024);
+
+ uvm_km_free(kernel_map, sc->sys_image, 1024 * 1024);
+ pmap_update(pmap_kernel());
+ free(sc, M_DEVBUF);
+}
+
+#ifdef DDB
+void
+ddb_vgapost(void)
+{
+
+ if (ddb_vgapostp)
+ vga_post_call(ddb_vgapostp);
+ else
+ printf("ddb_vgapost: vga_post not initialized\n");
+}
+#endif