1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * Copyright (c) 2006 Jakub Jermar
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include "main.h"
|
---|
31 | #include <printf.h>
|
---|
32 | #include "asm.h"
|
---|
33 | #include "_components.h"
|
---|
34 | #include <balloc.h>
|
---|
35 | #include <ofw.h>
|
---|
36 | #include <ofw_tree.h>
|
---|
37 | #include "ofwarch.h"
|
---|
38 | #include <align.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <string.h>
|
---|
41 | #include <memstr.h>
|
---|
42 |
|
---|
43 | static bootinfo_t bootinfo;
|
---|
44 | static component_t components[COMPONENTS];
|
---|
45 | static char *release = STRING(RELEASE);
|
---|
46 |
|
---|
47 | #ifdef REVISION
|
---|
48 | static char *revision = ", revision " STRING(REVISION);
|
---|
49 | #else
|
---|
50 | static char *revision = "";
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #ifdef TIMESTAMP
|
---|
54 | static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
|
---|
55 | #else
|
---|
56 | static char *timestamp = "";
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /** UltraSPARC subarchitecture - 1 for US, 3 for US3, 0 for other */
|
---|
60 | static uint8_t subarchitecture = 0;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * mask of the MID field inside the ICBUS_CONFIG register shifted by
|
---|
64 | * MID_SHIFT bits to the right
|
---|
65 | */
|
---|
66 | static uint16_t mid_mask;
|
---|
67 |
|
---|
68 | /** Print version information. */
|
---|
69 | static void version_print(void)
|
---|
70 | {
|
---|
71 |
|
---|
72 | printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
|
---|
73 | "Copyright (c) 2006 HelenOS project\n",
|
---|
74 | release, revision, timestamp);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* the lowest ID (read from the VER register) of some US3 CPU model */
|
---|
78 | #define FIRST_US3_CPU 0x14
|
---|
79 |
|
---|
80 | /* the greatest ID (read from the VER register) of some US3 CPU model */
|
---|
81 | #define LAST_US3_CPU 0x19
|
---|
82 |
|
---|
83 | /* UltraSPARC IIIi processor implementation code */
|
---|
84 | #define US_IIIi_CODE 0x15
|
---|
85 |
|
---|
86 | /* max. length of the "compatible" property of the root node */
|
---|
87 | #define COMPATIBLE_PROP_MAXLEN 64
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * HelenOS bootloader will use these constants to distinguish particular
|
---|
91 | * UltraSPARC architectures
|
---|
92 | */
|
---|
93 | #define COMPATIBLE_SUN4U 10
|
---|
94 | #define COMPATIBLE_SUN4V 20
|
---|
95 |
|
---|
96 | /** US architecture. COMPATIBLE_SUN4U for sun4v, COMPATIBLE_SUN4V for sun4u */
|
---|
97 | static uint8_t architecture;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Detects the UltraSPARC architecture (sun4u and sun4v currently supported)
|
---|
101 | * by inspecting the property called "compatible" in the OBP root node.
|
---|
102 | */
|
---|
103 | static void detect_architecture(void)
|
---|
104 | {
|
---|
105 | phandle root = ofw_find_device("/");
|
---|
106 | char compatible[COMPATIBLE_PROP_MAXLEN];
|
---|
107 |
|
---|
108 | if (ofw_get_property(root, "compatible", compatible,
|
---|
109 | COMPATIBLE_PROP_MAXLEN) <= 0) {
|
---|
110 | printf("Unable to determine architecture, default: sun4u.\n");
|
---|
111 | architecture = COMPATIBLE_SUN4U;
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (strcmp(compatible, "sun4v") == 0) {
|
---|
116 | architecture = COMPATIBLE_SUN4V;
|
---|
117 | } else {
|
---|
118 | /*
|
---|
119 | * As not all sun4u machines have "sun4u" in their "compatible"
|
---|
120 | * OBP property (e.g. Serengeti's OBP "compatible" property is
|
---|
121 | * "SUNW,Serengeti"), we will by default fallback to sun4u if
|
---|
122 | * an unknown value of the "compatible" property is encountered.
|
---|
123 | */
|
---|
124 | architecture = COMPATIBLE_SUN4U;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Detects the subarchitecture (US, US3) of the sun4u
|
---|
131 | * processor. Sets the global variables "subarchitecture" and "mid_mask" to
|
---|
132 | * correct values.
|
---|
133 | */
|
---|
134 | static void detect_subarchitecture(void)
|
---|
135 | {
|
---|
136 | uint64_t v;
|
---|
137 | asm volatile (
|
---|
138 | "rdpr %%ver, %0\n"
|
---|
139 | : "=r" (v)
|
---|
140 | );
|
---|
141 |
|
---|
142 | v = (v << 16) >> 48;
|
---|
143 | if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
|
---|
144 | subarchitecture = SUBARCH_US3;
|
---|
145 | if (v == US_IIIi_CODE)
|
---|
146 | mid_mask = (1 << 5) - 1;
|
---|
147 | else
|
---|
148 | mid_mask = (1 << 10) - 1;
|
---|
149 | } else if (v < FIRST_US3_CPU) {
|
---|
150 | subarchitecture = SUBARCH_US;
|
---|
151 | mid_mask = (1 << 5) - 1;
|
---|
152 | } else
|
---|
153 | printf("\nThis CPU is not supported by HelenOS.");
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Performs sun4u-specific initialization. The components are expected
|
---|
158 | * to be already copied and boot allocator initialized.
|
---|
159 | *
|
---|
160 | * @param base kernel base virtual address
|
---|
161 | * @param top virtual address above which the boot allocator
|
---|
162 | * can make allocations
|
---|
163 | */
|
---|
164 | static void bootstrap_sun4u(void *base, unsigned int top)
|
---|
165 | {
|
---|
166 | void *balloc_base;
|
---|
167 | /*
|
---|
168 | * Claim and map the physical memory for the boot allocator.
|
---|
169 | * Initialize the boot allocator.
|
---|
170 | */
|
---|
171 | balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
|
---|
172 | (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
|
---|
173 | BALLOC_MAX_SIZE);
|
---|
174 | (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
|
---|
175 | BALLOC_MAX_SIZE, -1);
|
---|
176 | balloc_init(&bootinfo.ballocs, (uintptr_t) balloc_base,
|
---|
177 | (uintptr_t) balloc_base);
|
---|
178 |
|
---|
179 | printf("Setting up screens...");
|
---|
180 | ofw_setup_screens();
|
---|
181 | printf("done.\n");
|
---|
182 |
|
---|
183 | printf("Canonizing OpenFirmware device tree...");
|
---|
184 | bootinfo.ofw_root = ofw_tree_build();
|
---|
185 | printf("done.\n");
|
---|
186 |
|
---|
187 | #ifdef CONFIG_AP
|
---|
188 | printf("Checking for secondary processors...");
|
---|
189 | if (!ofw_cpu(mid_mask, bootinfo.physmem_start))
|
---|
190 | printf("Error: unable to get CPU properties\n");
|
---|
191 | printf("done.\n");
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * * Performs sun4v-specific initialization. The components are expected
|
---|
198 | * * to be already copied and boot allocator initialized.
|
---|
199 | * */
|
---|
200 | static void bootstrap_sun4v(void)
|
---|
201 | {
|
---|
202 | /*
|
---|
203 | * When SILO booted, the OBP had established a virtual to physical
|
---|
204 | * memory mapping. This mapping is not an identity (because the
|
---|
205 | * physical memory starts on non-zero address) - this is not
|
---|
206 | * surprising. But! The mapping even does not map virtual address
|
---|
207 | * 0 onto the starting address of the physical memory, but onto an
|
---|
208 | * address which is 0x400000 bytes higher. The reason is that the
|
---|
209 | * OBP had already used the memory just at the beginning of the
|
---|
210 | * physical memory, so that memory cannot be used by SILO (nor
|
---|
211 | * bootloader). As for now, we solve it by a nasty workaround:
|
---|
212 | * we pretend that the physical memory starts 0x400000 bytes further
|
---|
213 | * than it actually does (and hence pretend that the physical memory
|
---|
214 | * is 0x400000 bytes smaller). Of course, the value 0x400000 will most
|
---|
215 | * probably depend on the machine and OBP version (the workaround now
|
---|
216 | * works on Simics). A solution would be to inspect the "available"
|
---|
217 | * property of the "/memory" node to find out which parts of memory
|
---|
218 | * are used by OBP and redesign the algorithm of copying
|
---|
219 | * kernel/init tasks/ramdisk from the bootable image to memory
|
---|
220 | * (which we must do anyway because of issues with claiming the memory
|
---|
221 | * on Serengeti).
|
---|
222 | */
|
---|
223 | bootinfo.physmem_start += 0x400000;
|
---|
224 | bootinfo.memmap.zones[0].start += 0x400000;
|
---|
225 | bootinfo.memmap.zones[0].size -= 0x400000;
|
---|
226 | printf("The sun4v init finished.");
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void bootstrap(void)
|
---|
231 | {
|
---|
232 | void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
|
---|
233 | unsigned int top = 0;
|
---|
234 | unsigned int i;
|
---|
235 | unsigned int j;
|
---|
236 |
|
---|
237 | detect_architecture();
|
---|
238 | init_components(components);
|
---|
239 |
|
---|
240 | if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
|
---|
241 | printf("Error: unable to get start of physical memory.\n");
|
---|
242 | halt();
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (!ofw_memmap(&bootinfo.memmap)) {
|
---|
246 | printf("Error: unable to get memory map, halting.\n");
|
---|
247 | halt();
|
---|
248 | }
|
---|
249 |
|
---|
250 | if (bootinfo.memmap.total == 0) {
|
---|
251 | printf("Error: no memory detected, halting.\n");
|
---|
252 | halt();
|
---|
253 | }
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * SILO for some reason adds 0x400000 and subtracts
|
---|
257 | * bootinfo.physmem_start to/from silo_ramdisk_image.
|
---|
258 | * We just need plain physical address so we fix it up.
|
---|
259 | */
|
---|
260 | if (silo_ramdisk_image) {
|
---|
261 | silo_ramdisk_image += bootinfo.physmem_start;
|
---|
262 | silo_ramdisk_image -= 0x400000;
|
---|
263 |
|
---|
264 | /* Install 1:1 mapping for the RAM disk. */
|
---|
265 | if (ofw_map((void *) ((uintptr_t) silo_ramdisk_image),
|
---|
266 | (void *) ((uintptr_t) silo_ramdisk_image),
|
---|
267 | silo_ramdisk_size, -1) != 0) {
|
---|
268 | printf("Failed to map RAM disk.\n");
|
---|
269 | halt();
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | printf("\nMemory statistics (total %d MB, starting at %P)\n",
|
---|
274 | bootinfo.memmap.total >> 20, bootinfo.physmem_start);
|
---|
275 | printf(" %P: kernel entry point\n", KERNEL_VIRTUAL_ADDRESS);
|
---|
276 | printf(" %P: boot info structure\n", &bootinfo);
|
---|
277 |
|
---|
278 | /*
|
---|
279 | * Figure out destination address for each component.
|
---|
280 | * In this phase, we don't copy the components yet because we want to
|
---|
281 | * to be careful not to overwrite anything, especially the components
|
---|
282 | * which haven't been copied yet.
|
---|
283 | */
|
---|
284 | bootinfo.taskmap.count = 0;
|
---|
285 | for (i = 0; i < COMPONENTS; i++) {
|
---|
286 | printf(" %P: %s image (size %d bytes)\n", components[i].start,
|
---|
287 | components[i].name, components[i].size);
|
---|
288 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
289 | if (i > 0) {
|
---|
290 | if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
|
---|
291 | printf("Skipping superfluous components.\n");
|
---|
292 | break;
|
---|
293 | }
|
---|
294 |
|
---|
295 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
|
---|
296 | base + top;
|
---|
297 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
|
---|
298 | components[i].size;
|
---|
299 | strncpy(bootinfo.taskmap.tasks[
|
---|
300 | bootinfo.taskmap.count].name, components[i].name,
|
---|
301 | BOOTINFO_TASK_NAME_BUFLEN);
|
---|
302 | bootinfo.taskmap.count++;
|
---|
303 | }
|
---|
304 | top += components[i].size;
|
---|
305 | }
|
---|
306 |
|
---|
307 | printf("\n");
|
---|
308 |
|
---|
309 | /* Do not consider RAM disk */
|
---|
310 | j = bootinfo.taskmap.count - 1;
|
---|
311 |
|
---|
312 | if (silo_ramdisk_image) {
|
---|
313 | /* Treat the RAM disk as the last bootinfo task. */
|
---|
314 | if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
|
---|
315 | printf("Skipping RAM disk.\n");
|
---|
316 | goto skip_ramdisk;
|
---|
317 | }
|
---|
318 |
|
---|
319 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
320 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
|
---|
321 | base + top;
|
---|
322 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
|
---|
323 | silo_ramdisk_size;
|
---|
324 | bootinfo.taskmap.count++;
|
---|
325 | printf("Copying RAM disk...");
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * Claim and map the whole ramdisk as it may exceed the area
|
---|
329 | * given to us by SILO.
|
---|
330 | */
|
---|
331 | (void) ofw_claim_phys(base + top, silo_ramdisk_size);
|
---|
332 | (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
|
---|
333 | silo_ramdisk_size, -1);
|
---|
334 | memmove(base + top, (void *) ((uintptr_t) silo_ramdisk_image),
|
---|
335 | silo_ramdisk_size);
|
---|
336 |
|
---|
337 | printf("done.\n");
|
---|
338 | top += silo_ramdisk_size;
|
---|
339 | }
|
---|
340 | skip_ramdisk:
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Now we can proceed to copy the components. We do it in reverse order
|
---|
344 | * so that we don't overwrite anything even if the components overlap
|
---|
345 | * with base.
|
---|
346 | */
|
---|
347 | printf("Copying tasks...");
|
---|
348 | for (i = COMPONENTS - 1; i > 0; i--, j--) {
|
---|
349 | printf("%s ", components[i].name);
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * At this point, we claim the physical memory that we are
|
---|
353 | * going to use. We should be safe in case of the virtual
|
---|
354 | * address space because the OpenFirmware, according to its
|
---|
355 | * SPARC binding, should restrict its use of virtual memory
|
---|
356 | * to addresses from [0xffd00000; 0xffefffff] and
|
---|
357 | * [0xfe000000; 0xfeffffff].
|
---|
358 | *
|
---|
359 | * XXX We don't map this piece of memory. We simply rely on
|
---|
360 | * SILO to have it done for us already in this case.
|
---|
361 | */
|
---|
362 | (void) ofw_claim_phys(bootinfo.physmem_start +
|
---|
363 | bootinfo.taskmap.tasks[j].addr,
|
---|
364 | ALIGN_UP(components[i].size, PAGE_SIZE));
|
---|
365 |
|
---|
366 | memcpy((void *) bootinfo.taskmap.tasks[j].addr,
|
---|
367 | components[i].start, components[i].size);
|
---|
368 |
|
---|
369 | }
|
---|
370 | printf(".\n");
|
---|
371 |
|
---|
372 | printf("Copying kernel...");
|
---|
373 | (void) ofw_claim_phys(bootinfo.physmem_start + base,
|
---|
374 | ALIGN_UP(components[0].size, PAGE_SIZE));
|
---|
375 | memcpy(base, components[0].start, components[0].size);
|
---|
376 | printf("done.\n");
|
---|
377 |
|
---|
378 | /* perform architecture-specific initialization */
|
---|
379 | if (architecture == COMPATIBLE_SUN4U) {
|
---|
380 | bootstrap_sun4u(base, top);
|
---|
381 | } else if (architecture == COMPATIBLE_SUN4V) {
|
---|
382 | bootstrap_sun4v();
|
---|
383 | } else {
|
---|
384 | printf("Unknown architecture.\n");
|
---|
385 | halt();
|
---|
386 | }
|
---|
387 |
|
---|
388 | printf("Booting the kernel...\n");
|
---|
389 | jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
|
---|
390 | bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
|
---|
391 | sizeof(bootinfo), subarchitecture);
|
---|
392 | }
|
---|