xref: /netbsd-src/sys/arch/hpc/stand/hpcboot/arch.cpp (revision 17dd36da8292193180754d5047c0926dbb56818c)
1 /* -*-C++-*-	$NetBSD: arch.cpp,v 1.2 2001/03/15 17:24:47 uch Exp $	 */
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <hpcboot.h>
40 #include <hpcmenu.h>
41 
42 #include <console.h>
43 #include <memory.h>
44 #include <load.h>
45 #include <arch.h>
46 #include <framebuffer.h>
47 
48 Architecture::Architecture(Console *&cons, MemoryManager *&mem)
49 	:_cons(cons), _mem(mem)
50 {
51 	_loader_addr = 0;
52 	_debug = FALSE;
53 	_dll = 0;
54 }
55 
56 Architecture::~Architecture(void)
57 {
58 	if (_dll)
59 		FreeLibrary(_dll);
60 }
61 
62 BOOL
63 Architecture::allocateMemory(size_t sz)
64 {
65 	//binary image.
66 	sz = _mem->estimateTaggedPageSize(sz);
67 	//pvec + BootArgs + 2 nd bootloader + bootloader stack.
68 	sz += _mem->getPageSize() * 4;
69 	sz = _mem->roundRegion(sz);
70 	return _mem->reservePage(sz);
71 }
72 
73 paddr_t
74 Architecture::setupBootInfo(Loader &loader)
75 {
76 	HpcMenuInterface &menu = HpcMenuInterface::Instance();
77 	vaddr_t v;
78 	paddr_t p;
79 
80 	_mem->getPage(v, p);
81 	_boot_arg = reinterpret_cast <struct BootArgs *>(v);
82 
83 	_boot_arg->argc = menu.setup_kernel_args(v + sizeof(struct BootArgs),
84 					    p + sizeof(struct BootArgs));
85 	_boot_arg->argv = ptokv(p + sizeof(struct BootArgs));
86 	menu.setup_bootinfo(_boot_arg->bi);
87 	_boot_arg->bi.bi_cnuse = _cons->getBootConsole();
88 
89 	_boot_arg->bootinfo = ptokv(p + offsetof(struct BootArgs, bi));
90 	_boot_arg->kernel_entry = loader.jumpAddr();
91 
92 	struct bootinfo &bi = _boot_arg->bi;
93 	DPRINTF((TEXT("framebuffer: %dx%d type=%d linebytes=%d addr=0x%08x\n"),
94 		 bi.fb_width, bi.fb_height, bi.fb_type, bi.fb_line_bytes,
95 		 bi.fb_addr));
96 	DPRINTF((TEXT("console = %d\n"), bi.bi_cnuse));
97 
98 	return p;
99 }
100 
101 void *
102 Architecture::_load_func(const TCHAR * name)
103 {
104 	if (_dll == 0)
105 		_dll = LoadLibrary(TEXT("coredll.dll"));
106 
107 	return _dll
108 		? reinterpret_cast <void *>(GetProcAddress(_dll, name))
109 		: 0;
110 }
111 
112 void
113 Architecture::systemInfo(void)
114 {
115 	int(*func)(HDC, int, int, LPCSTR, int, LPSTR);
116 	u_int32_t val = 0;
117 	int ret;
118 	HDC hdc;
119 
120 	// inquire default setting.
121 	FrameBufferInfo fb(0, 0);
122 	DPRINTF((TEXT("[DISPLAY] %dx%d %dbpp\n"), fb.width(), fb.height(),
123 		 fb.bpp()));
124 
125 	func = reinterpret_cast <int(*)(HDC, int, int, LPCSTR, int, LPSTR)>
126 		(_load_func(TEXT("ExtEscape")));
127 	if (func == 0) {
128 		DPRINTF((TEXT("ExtEscape not found.\n")));
129 		return;
130 	}
131 	hdc = GetDC(0);
132 	ret = func(hdc, GETVFRAMEPHYSICAL, 0, 0, sizeof(u_int32_t),
133 		   reinterpret_cast <char *>(&val));
134 	if (ret == 0)
135 		DPRINTF((TEXT("ExtEscape(GETVFRAMEPHYSICAL) not implemented.\n")));
136 	else if (ret < 0)
137 		DPRINTF((TEXT("ExtEscape(GETVFRAMEPHYSICAL) failure.\n")));
138 	else
139 		DPRINTF((TEXT("frame buffer physical address: 0x%08x\n"),
140 			 val));
141 
142 	ret = func(hdc, GETVFRAMELEN, 0, 0, sizeof(u_int32_t),
143 		   reinterpret_cast <char *>(&val));
144 
145 	if (ret == 0)
146 		DPRINTF((TEXT("ExtEscape(GETVFRAMELEN) not implemented.\n")));
147 	else if (ret < 0)
148 		DPRINTF((TEXT("ExtEscape(GETVFRAMELEN) failure.\n")));
149 	else
150 		DPRINTF((TEXT("frame buffer length: 0x%08x\n"), val));
151 
152 	ReleaseDC(0, hdc);
153 
154 }
155 
156 BOOL(*Architecture::_load_LockPages(void))(LPVOID, DWORD, PDWORD, int)
157 {
158 	return reinterpret_cast <BOOL(*)(LPVOID, DWORD, PDWORD, int)>
159 		(_load_func(TEXT("LockPages")));
160 }
161 
162 BOOL(*Architecture::_load_UnlockPages(void))(LPVOID, DWORD)
163 {
164 	return reinterpret_cast <BOOL(*)(LPVOID, DWORD)>
165 		(_load_func(TEXT("UnlockPages")));
166 }
167 
168 //
169 // Debug support.
170 //
171 void
172 Architecture::_bitdisp(u_int32_t a, int s, int e, int m, int c)
173 {
174 	u_int32_t j, j1;
175 	int i, n;
176 
177 	n = 31;	// 32bit only.
178 	j1 = 1 << n;
179 	e = e ? e : n;
180 	for (j = j1, i = n; j > 0; j >>=1, i--) {
181 		if (i > e || i < s) {
182 			DPRINTF((TEXT("%c"), a & j ? '+' : '-'));
183 		} else {
184 			DPRINTF((TEXT("%c"), a & j ? '|' : '.'));
185 		}
186 	}
187 	if (m) {
188 		DPRINTF((TEXT("[%s]"),(char*)m));
189 	}
190 	if (c) {
191 		for (j = j1, i = n; j > 0; j >>=1, i--) {
192 			if (!(i > e || i < s) &&(a & j)) {
193 				DPRINTF((TEXT(" %d"), i));
194 			}
195 		}
196 	}
197 	DPRINTF((TEXT(" [0x%08x] %d"), a, a));
198 	DPRINTF((TEXT("\n")));
199 }
200 
201 void
202 Architecture::_dbg_bit_print(u_int32_t reg, u_int32_t mask, const char *name)
203 {
204 	static const char onoff[3] = "_x";
205 	DPRINTF((TEXT("%S[%c] "), name, onoff[reg & mask ? 1 : 0]));
206 }
207