source: mainline/boot/arch/sparc64/loader/main.c@ 86018c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86018c1 was f238e86, checked in by Pavel Rimsky <pavel@…>, 16 years ago

Both sun4u and sun4v are compilable, sun4u feature-complete, sun4v reaches (at least) version_print.

  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[b7b5f83]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[e731b0d]3 * Copyright (c) 2006 Jakub Jermar
[b7b5f83]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
[e731b0d]30#include "main.h"
[b7b5f83]31#include <printf.h>
32#include "asm.h"
33#include "_components.h"
[61e90dd]34#include <balloc.h>
[822b64e]35#include <ofw.h>
[61e90dd]36#include <ofw_tree.h>
[b4fa652]37#include "ofwarch.h"
[63cda71]38#include <align.h>
[fa024ce]39#include <macros.h>
[dac629e]40#include <string.h>
[e731b0d]41#include <memstr.h>
[b7b5f83]42
[e731b0d]43static bootinfo_t bootinfo;
44static component_t components[COMPONENTS];
45static char *release = STRING(RELEASE);
[aca95f6b]46
47#ifdef REVISION
[e731b0d]48 static char *revision = ", revision " STRING(REVISION);
[aca95f6b]49#else
[e731b0d]50 static char *revision = "";
[aca95f6b]51#endif
52
53#ifdef TIMESTAMP
[e731b0d]54 static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
[aca95f6b]55#else
[e731b0d]56 static char *timestamp = "";
[aca95f6b]57#endif
58
[f238e86]59/** UltraSPARC subarchitecture - 1 for US, 3 for US3, 0 for other */
60static uint8_t subarchitecture = 0;
[965dc18]61
62/**
63 * mask of the MID field inside the ICBUS_CONFIG register shifted by
64 * MID_SHIFT bits to the right
65 */
[e731b0d]66static uint16_t mid_mask;
[965dc18]67
[aca95f6b]68/** Print version information. */
69static void version_print(void)
70{
[f238e86]71
[a9ddab2]72 printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
73 "Copyright (c) 2006 HelenOS project\n",
74 release, revision, timestamp);
[aca95f6b]75}
76
[965dc18]77/* the lowest ID (read from the VER register) of some US3 CPU model */
[e731b0d]78#define FIRST_US3_CPU 0x14
[965dc18]79
80/* the greatest ID (read from the VER register) of some US3 CPU model */
[e731b0d]81#define LAST_US3_CPU 0x19
[965dc18]82
83/* UltraSPARC IIIi processor implementation code */
[e731b0d]84#define US_IIIi_CODE 0x15
[965dc18]85
[f238e86]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 */
97static uint8_t architecture;
98
[965dc18]99/**
[f238e86]100 * Detects the UltraSPARC architecture (sun4u and sun4v currently supported)
101 * by inspecting the property called "compatible" in the OBP root node.
102 */
103static 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
[965dc18]132 * correct values.
133 */
134static void detect_subarchitecture(void)
135{
136 uint64_t v;
[e731b0d]137 asm volatile (
138 "rdpr %%ver, %0\n"
139 : "=r" (v)
140 );
[965dc18]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;
[e731b0d]152 } else
[965dc18]153 printf("\nThis CPU is not supported by HelenOS.");
154}
155
[f238e86]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 */
164static 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 * */
200static 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
[b7b5f83]230void bootstrap(void)
231{
[27518e4]232 void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
233 unsigned int top = 0;
[e731b0d]234 unsigned int i;
235 unsigned int j;
236
[f238e86]237 detect_architecture();
[b7b5f83]238 init_components(components);
[e731b0d]239
[f2ea5d8]240 if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
241 printf("Error: unable to get start of physical memory.\n");
242 halt();
243 }
[e731b0d]244
[63cda71]245 if (!ofw_memmap(&bootinfo.memmap)) {
246 printf("Error: unable to get memory map, halting.\n");
247 halt();
248 }
[e731b0d]249
[63cda71]250 if (bootinfo.memmap.total == 0) {
251 printf("Error: no memory detected, halting.\n");
252 halt();
253 }
[e731b0d]254
[a9ddab2]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;
[e731b0d]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),
[27518e4]267 silo_ramdisk_size, -1) != 0) {
[e731b0d]268 printf("Failed to map RAM disk.\n");
[27518e4]269 halt();
270 }
[a9ddab2]271 }
[63cda71]272
[e731b0d]273 printf("\nMemory statistics (total %d MB, starting at %P)\n",
[a9ddab2]274 bootinfo.memmap.total >> 20, bootinfo.physmem_start);
[e731b0d]275 printf(" %P: kernel entry point\n", KERNEL_VIRTUAL_ADDRESS);
[63cda71]276 printf(" %P: boot info structure\n", &bootinfo);
[b7b5f83]277
[27518e4]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++) {
[f2ea5d8]286 printf(" %P: %s image (size %d bytes)\n", components[i].start,
[95b47c82]287 components[i].name, components[i].size);
[27518e4]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 }
[e731b0d]294
[27518e4]295 bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
296 base + top;
297 bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
298 components[i].size;
[f7734012]299 strncpy(bootinfo.taskmap.tasks[
300 bootinfo.taskmap.count].name, components[i].name,
301 BOOTINFO_TASK_NAME_BUFLEN);
[27518e4]302 bootinfo.taskmap.count++;
303 }
304 top += components[i].size;
305 }
[e731b0d]306
[e0565005]307 printf("\n");
308
[e731b0d]309 /* Do not consider RAM disk */
310 j = bootinfo.taskmap.count - 1;
311
[27518e4]312 if (silo_ramdisk_image) {
[e731b0d]313 /* Treat the RAM disk as the last bootinfo task. */
[27518e4]314 if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
[e731b0d]315 printf("Skipping RAM disk.\n");
[27518e4]316 goto skip_ramdisk;
317 }
[e731b0d]318
[b7b5f83]319 top = ALIGN_UP(top, PAGE_SIZE);
[27518e4]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++;
[e0565005]325 printf("Copying RAM disk...");
[e731b0d]326
[27518e4]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);
[6196dae]332 (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
333 silo_ramdisk_size, -1);
[e731b0d]334 memmove(base + top, (void *) ((uintptr_t) silo_ramdisk_image),
[27518e4]335 silo_ramdisk_size);
[e731b0d]336
[27518e4]337 printf("done.\n");
338 top += silo_ramdisk_size;
339 }
340skip_ramdisk:
[e731b0d]341
[27518e4]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 */
[e0565005]347 printf("Copying tasks...");
[27518e4]348 for (i = COMPONENTS - 1; i > 0; i--, j--) {
[e731b0d]349 printf("%s ", components[i].name);
350
[95b47c82]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].
[27518e4]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.
[95b47c82]361 */
[27518e4]362 (void) ofw_claim_phys(bootinfo.physmem_start +
363 bootinfo.taskmap.tasks[j].addr,
[95b47c82]364 ALIGN_UP(components[i].size, PAGE_SIZE));
[e731b0d]365
366 memcpy((void *) bootinfo.taskmap.tasks[j].addr,
[27518e4]367 components[i].start, components[i].size);
[e731b0d]368
[b7b5f83]369 }
[e731b0d]370 printf(".\n");
371
[e0565005]372 printf("Copying kernel...");
[27518e4]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");
[e731b0d]377
[f238e86]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 }
[e731b0d]387
[e0565005]388 printf("Booting the kernel...\n");
[f2ea5d8]389 jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
[95b47c82]390 bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
[e731b0d]391 sizeof(bootinfo), subarchitecture);
[b7b5f83]392}
Note: See TracBrowser for help on using the repository browser.