vga_post.c revision 1.11
1/* $OpenBSD: vga_post.c,v 1.11 2021/01/03 02:29:28 jmatthew 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 = (vaddr_t)km_alloc(PAGE_SIZE, &kv_any, &kp_none,
129	    &kd_nowait);
130	if (sys_bios_data == 0)
131		return NULL;
132
133	sys_image = (vaddr_t)km_alloc(1024 * 1024, &kv_any, &kp_none,
134	    &kd_nowait);
135	if (sys_image == 0) {
136		km_free((void *)sys_bios_data, PAGE_SIZE, &kv_any, &kp_none);
137		return NULL;
138	}
139	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
140
141	TAILQ_INIT(&sc->ram_backing);
142	err = uvm_pglistalloc(BASE_MEMORY, 0, (paddr_t)-1, 0, 0,
143	    &sc->ram_backing, BASE_MEMORY/PAGE_SIZE, UVM_PLA_WAITOK);
144	if (err) {
145		km_free((void *)sc->sys_image, 1024 * 1024, &kv_any, &kp_none);
146		free(sc, M_DEVBUF, sizeof(*sc));
147		return NULL;
148	}
149
150	sc->sys_image = sys_image;
151	sc->emu.sys_private = sc;
152
153	pmap_kenter_pa(sys_bios_data, 0, PROT_READ);
154	pmap_update(pmap_kernel());
155	memcpy((void *)sc->bios_data, (void *)sys_bios_data, PAGE_SIZE);
156	pmap_kremove(sys_bios_data, PAGE_SIZE);
157	km_free((void *)sys_bios_data, PAGE_SIZE, &kv_any, &kp_none);
158
159	iter = 0;
160	TAILQ_FOREACH(pg, &sc->ram_backing, pageq) {
161		pmap_kenter_pa(sc->sys_image + iter, VM_PAGE_TO_PHYS(pg),
162		    PROT_READ | PROT_WRITE);
163		iter += PAGE_SIZE;
164	}
165	KASSERT(iter == BASE_MEMORY);
166
167	for (iter = 640 * 1024; iter < 1024 * 1024; iter += PAGE_SIZE)
168		pmap_kenter_pa(sc->sys_image + iter, iter,
169		    PROT_READ | PROT_WRITE);
170	pmap_update(pmap_kernel());
171
172	memset(&sc->emu, 0, sizeof(sc->emu));
173	x86emu_init_default(&sc->emu);
174	sc->emu.emu_inb = vm86_emu_inb;
175	sc->emu.emu_inw = vm86_emu_inw;
176	sc->emu.emu_inl = vm86_emu_inl;
177	sc->emu.emu_outb = vm86_emu_outb;
178	sc->emu.emu_outw = vm86_emu_outw;
179	sc->emu.emu_outl = vm86_emu_outl;
180
181	sc->emu.mem_base = (char *)sc->sys_image;
182	sc->emu.mem_size = 1024 * 1024;
183
184	sc->initial_eax = bus * 256 + device * 8 + function;
185#ifdef DDB
186	ddb_vgapostp = sc;
187#endif
188	return sc;
189}
190
191void
192vga_post_call(struct vga_post *sc)
193{
194	sc->emu.x86.R_EAX = sc->initial_eax;
195	sc->emu.x86.R_EDX = 0x00000080;
196	sc->emu.x86.R_DS = 0x0040;
197	sc->emu.x86.register_flags = 0x3200;
198
199	memcpy((void *)sc->sys_image, sc->bios_data, PAGE_SIZE);
200
201	/* stack is at the end of the first 64KB */
202	sc->emu.x86.R_SS = 0;
203	sc->emu.x86.R_ESP = 0;
204
205	/* Jump straight into the VGA BIOS POST code */
206	x86emu_exec_call(&sc->emu, 0xc000, 0x0003);
207}
208
209void
210vga_post_free(struct vga_post *sc)
211{
212	uvm_pglistfree(&sc->ram_backing);
213	pmap_kremove(sc->sys_image, 1024 * 1024);
214	km_free((void *)sc->sys_image, 1024 * 1024, &kv_any, &kp_none)
215	pmap_update(pmap_kernel());
216	free(sc, M_DEVBUF, sizeof(*sc));
217}
218
219#ifdef DDB
220void
221ddb_vgapost(void)
222{
223
224	if (ddb_vgapostp)
225		vga_post_call(ddb_vgapostp);
226	else
227		printf("ddb_vgapost: vga_post not initialized\n");
228}
229#endif
230