Line data Source code
1 : /* $OpenBSD: vga_post.c,v 1.10 2015/08/28 00:03:53 deraadt 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 :
46 : struct 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
55 : static struct vga_post *ddb_vgapostp;
56 : void ddb_vgapost(void);
57 : #endif
58 :
59 : static uint8_t
60 0 : vm86_emu_inb(struct x86emu *emu, uint16_t port)
61 : {
62 0 : if (port == 0xb2) /* APM scratch register */
63 0 : return 0;
64 :
65 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
66 0 : return 0;
67 :
68 0 : return inb(port);
69 0 : }
70 :
71 : static uint16_t
72 0 : vm86_emu_inw(struct x86emu *emu, uint16_t port)
73 : {
74 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
75 0 : return 0;
76 :
77 0 : return inw(port);
78 0 : }
79 :
80 : static uint32_t
81 0 : vm86_emu_inl(struct x86emu *emu, uint16_t port)
82 : {
83 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
84 0 : return 0;
85 :
86 0 : return inl(port);
87 0 : }
88 :
89 : static void
90 0 : vm86_emu_outb(struct x86emu *emu, uint16_t port, uint8_t val)
91 : {
92 0 : if (port == 0xb2) /* APM scratch register */
93 : return;
94 :
95 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
96 : return;
97 :
98 0 : outb(port, val);
99 0 : }
100 :
101 : static void
102 0 : vm86_emu_outw(struct x86emu *emu, uint16_t port, uint16_t val)
103 : {
104 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
105 : return;
106 :
107 0 : outw(port, val);
108 0 : }
109 :
110 : static void
111 0 : vm86_emu_outl(struct x86emu *emu, uint16_t port, uint32_t val)
112 : {
113 0 : if (port >= 0x80 && port < 0x88) /* POST status register */
114 : return;
115 :
116 0 : outl(port, val);
117 0 : }
118 :
119 : struct vga_post *
120 0 : vga_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 0 : sys_bios_data = uvm_km_valloc(kernel_map, PAGE_SIZE);
129 0 : if (sys_bios_data == 0)
130 0 : return NULL;
131 :
132 0 : sys_image = uvm_km_valloc(kernel_map, 1024 * 1024);
133 0 : if (sys_image == 0) {
134 0 : uvm_km_free(kernel_map, sys_bios_data, PAGE_SIZE);
135 0 : return NULL;
136 : }
137 0 : sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
138 :
139 0 : TAILQ_INIT(&sc->ram_backing);
140 0 : err = uvm_pglistalloc(BASE_MEMORY, 0, (paddr_t)-1, 0, 0,
141 : &sc->ram_backing, BASE_MEMORY/PAGE_SIZE, UVM_PLA_WAITOK);
142 0 : if (err) {
143 0 : uvm_km_free(kernel_map, sc->sys_image, 1024 * 1024);
144 0 : free(sc, M_DEVBUF, sizeof(*sc));
145 0 : return NULL;
146 : }
147 :
148 0 : sc->sys_image = sys_image;
149 0 : sc->emu.sys_private = sc;
150 :
151 0 : pmap_kenter_pa(sys_bios_data, 0, PROT_READ);
152 : pmap_update(pmap_kernel());
153 0 : memcpy((void *)sc->bios_data, (void *)sys_bios_data, PAGE_SIZE);
154 0 : pmap_kremove(sys_bios_data, PAGE_SIZE);
155 0 : uvm_km_free(kernel_map, sys_bios_data, PAGE_SIZE);
156 :
157 : iter = 0;
158 0 : TAILQ_FOREACH(pg, &sc->ram_backing, pageq) {
159 0 : pmap_kenter_pa(sc->sys_image + iter, VM_PAGE_TO_PHYS(pg),
160 : PROT_READ | PROT_WRITE);
161 0 : iter += PAGE_SIZE;
162 : }
163 0 : KASSERT(iter == BASE_MEMORY);
164 :
165 0 : for (iter = 640 * 1024; iter < 1024 * 1024; iter += PAGE_SIZE)
166 0 : pmap_kenter_pa(sc->sys_image + iter, iter,
167 : PROT_READ | PROT_WRITE);
168 : pmap_update(pmap_kernel());
169 :
170 0 : memset(&sc->emu, 0, sizeof(sc->emu));
171 0 : x86emu_init_default(&sc->emu);
172 0 : sc->emu.emu_inb = vm86_emu_inb;
173 0 : sc->emu.emu_inw = vm86_emu_inw;
174 0 : sc->emu.emu_inl = vm86_emu_inl;
175 0 : sc->emu.emu_outb = vm86_emu_outb;
176 0 : sc->emu.emu_outw = vm86_emu_outw;
177 0 : sc->emu.emu_outl = vm86_emu_outl;
178 :
179 0 : sc->emu.mem_base = (char *)sc->sys_image;
180 0 : sc->emu.mem_size = 1024 * 1024;
181 :
182 0 : sc->initial_eax = bus * 256 + device * 8 + function;
183 : #ifdef DDB
184 0 : ddb_vgapostp = sc;
185 : #endif
186 0 : return sc;
187 0 : }
188 :
189 : void
190 0 : vga_post_call(struct vga_post *sc)
191 : {
192 0 : sc->emu.x86.R_EAX = sc->initial_eax;
193 0 : sc->emu.x86.R_EDX = 0x00000080;
194 0 : sc->emu.x86.R_DS = 0x0040;
195 0 : sc->emu.x86.register_flags = 0x3200;
196 :
197 0 : memcpy((void *)sc->sys_image, sc->bios_data, PAGE_SIZE);
198 :
199 : /* stack is at the end of the first 64KB */
200 0 : sc->emu.x86.R_SS = 0;
201 0 : sc->emu.x86.R_ESP = 0;
202 :
203 : /* Jump straight into the VGA BIOS POST code */
204 0 : x86emu_exec_call(&sc->emu, 0xc000, 0x0003);
205 0 : }
206 :
207 : void
208 0 : vga_post_free(struct vga_post *sc)
209 : {
210 0 : uvm_pglistfree(&sc->ram_backing);
211 0 : pmap_kremove(sc->sys_image, 1024 * 1024);
212 0 : uvm_km_free(kernel_map, sc->sys_image, 1024 * 1024);
213 : pmap_update(pmap_kernel());
214 0 : free(sc, M_DEVBUF, sizeof(*sc));
215 0 : }
216 :
217 : #ifdef DDB
218 : void
219 0 : ddb_vgapost(void)
220 : {
221 :
222 0 : if (ddb_vgapostp)
223 0 : vga_post_call(ddb_vgapostp);
224 : else
225 0 : printf("ddb_vgapost: vga_post not initialized\n");
226 0 : }
227 : #endif
|