source: rtems/c/src/lib/libbsp/sparc64/shared/helenos/boot/sparc64/loader/main.c @ 9379c70

5
Last change on this file since 9379c70 was 9379c70, checked in by Sebastian Huber <sebastian.huber@…>, on 07/28/17 at 11:40:45

bsps/sparc64: Fix warning

  • Property mode set to 100644
File size: 12.7 KB
Line 
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/*
31 * Modifications are made to switch to using printk rather than printf,
32 * and to remove portions of the HelenOS bootstrap process that are
33 * unnecessary on RTEMS.  The removed code is elided with #if 0 ... #endif
34 * blocks.
35 *
36 * Removes some header files. Adds back some missing defines.
37 */
38
39#define RTEMS
40
41#include <bsp.h>
42#include <rtems/bspIo.h>
43#include <inttypes.h>
44
45#include <boot/main.h>
46#include <boot/balloc.h>
47#include <boot/ofw.h>
48#include <boot/ofw_tree.h>
49#include <boot/ofwarch.h>
50#include <boot/align.h>
51
52#if 0
53#include "asm.h"
54#include <printf.h>
55#include "_components.h"
56#include <macros.h>
57#include <string.h>
58#include <memstr.h>
59#endif
60
61#include <asm.h>
62
63#if 0
64#define PAGE_WIDTH  14
65#define PAGE_SIZE   (1 << PAGE_WIDTH)
66#endif
67
68static bootinfo_t bootinfo;
69#if 0
70static component_t components[COMPONENTS];
71static char *release = STRING(RELEASE);
72
73#ifdef REVISION
74        static char *revision = ", revision " STRING(REVISION);
75#else
76        static char *revision = "";
77#endif
78
79#ifdef TIMESTAMP
80        static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
81#else
82        static char *timestamp = "";
83#endif
84#endif
85
86#if 0
87/** UltraSPARC subarchitecture - 1 for US, 3 for US3, 0 for other */
88static uint8_t subarchitecture = 0;
89#endif
90
91#if 0
92/**
93 * mask of the MID field inside the ICBUS_CONFIG register shifted by
94 * MID_SHIFT bits to the right
95 */
96static uint16_t mid_mask;
97#endif
98
99#if 0
100/** Print version information. */
101static void version_print(void)
102{
103        printk("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
104            "Copyright (c) 2006 HelenOS project\n",
105            release, revision, timestamp);
106}
107#endif
108
109/* the lowest ID (read from the VER register) of some US3 CPU model */
110#define FIRST_US3_CPU  0x14
111
112/* the greatest ID (read from the VER register) of some US3 CPU model */
113#define LAST_US3_CPU   0x19
114
115/* UltraSPARC IIIi processor implementation code */
116#define US_IIIi_CODE   0x15
117
118/* max. length of the "compatible" property of the root node */
119#define COMPATIBLE_PROP_MAXLEN  64
120
121/*
122 * HelenOS bootloader will use these constants to distinguish particular
123 * UltraSPARC architectures
124 */
125#define COMPATIBLE_SUN4U        10
126#define COMPATIBLE_SUN4V        20
127
128/** US architecture. COMPATIBLE_SUN4U for sun4v, COMPATIBLE_SUN4V for sun4u */
129static uint8_t architecture;
130
131/**
132 * Detects the UltraSPARC architecture (sun4u and sun4v currently supported)
133 * by inspecting the property called "compatible" in the OBP root node.
134 */
135static void detect_architecture(void)
136{
137        phandle root = ofw_find_device("/");
138        char compatible[COMPATIBLE_PROP_MAXLEN];
139
140        if (ofw_get_property(root, "compatible", compatible,
141                        COMPATIBLE_PROP_MAXLEN) <= 0) {
142                printk("Unable to determine architecture, default: sun4u.\n");
143                architecture = COMPATIBLE_SUN4U;
144                return;
145        }
146
147        if (strcmp(compatible, "sun4v") == 0) {
148                architecture = COMPATIBLE_SUN4V;
149        } else {
150                /*
151                 * As not all sun4u machines have "sun4u" in their "compatible"
152                 * OBP property (e.g. Serengeti's OBP "compatible" property is
153                 * "SUNW,Serengeti"), we will by default fallback to sun4u if
154                 * an unknown value of the "compatible" property is encountered.
155                 */
156                architecture = COMPATIBLE_SUN4U;
157        }
158}
159
160#if 0
161/**
162 * Detects the subarchitecture (US, US3) of the sun4u
163 * processor. Sets the global variables "subarchitecture" and "mid_mask" to
164 * correct values.
165 */
166static void detect_subarchitecture(void)
167{
168        uint64_t v;
169        asm volatile (
170                "rdpr %%ver, %0\n"
171                : "=r" (v)
172        );
173       
174        v = (v << 16) >> 48;
175        if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
176                subarchitecture = SUBARCH_US3;
177                if (v == US_IIIi_CODE)
178                        mid_mask = (1 << 5) - 1;
179                else
180                        mid_mask = (1 << 10) - 1;
181        } else if (v < FIRST_US3_CPU) {
182                subarchitecture = SUBARCH_US;
183                mid_mask = (1 << 5) - 1;
184        } else
185                printk("\nThis CPU is not supported by HelenOS.");
186}
187#endif
188
189#if 0
190/**
191 * Performs sun4u-specific initialization. The components are expected
192 * to be already copied and boot allocator initialized.
193 *
194 * @param base  kernel base virtual address
195 * @param top   virtual address above which the boot allocator
196 *              can make allocations
197 */
198static void bootstrap_sun4u(void *base, unsigned int top)
199{
200        void *balloc_base;
201        /*
202         * Claim and map the physical memory for the boot allocator.
203         * Initialize the boot allocator.
204         */
205        balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
206        (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
207            BALLOC_MAX_SIZE);
208        (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
209            BALLOC_MAX_SIZE, -1);
210        balloc_init(&bootinfo.ballocs, (uintptr_t) balloc_base,
211            (uintptr_t) balloc_base);
212#if 0   
213        printf("Setting up screens...");
214        ofw_setup_screens();
215        printf("done.\n");
216#endif
217#if 0
218        printf("Canonizing OpenFirmware device tree...");
219#endif
220        bootinfo.ofw_root = ofw_tree_build();
221#if 0
222        printf("done.\n");
223#endif
224#if 0
225#ifdef CONFIG_AP
226        printf("Checking for secondary processors...");
227        if (!ofw_cpu(mid_mask, bootinfo.physmem_start))
228                printf("Error: unable to get CPU properties\n");
229        printf("done.\n");
230#endif
231#endif
232}
233#endif
234
235#if 0
236/**
237 *  * Performs sun4v-specific initialization. The components are expected
238 *   * to be already copied and boot allocator initialized.
239 *    */
240static void bootstrap_sun4v(void)
241{
242        /*
243         * When SILO booted, the OBP had established a virtual to physical
244         * memory mapping. This mapping is not an identity (because the
245         * physical memory starts on non-zero address) - this is not
246         * surprising. But! The mapping even does not map virtual address
247         * 0 onto the starting address of the physical memory, but onto an
248         * address which is 0x400000 bytes higher. The reason is that the
249         * OBP had already used the memory just at the beginning of the
250         * physical memory, so that memory cannot be used by SILO (nor
251         * bootloader). As for now, we solve it by a nasty workaround:
252         * we pretend that the physical memory starts 0x400000 bytes further
253         * than it actually does (and hence pretend that the physical memory
254         * is 0x400000 bytes smaller). Of course, the value 0x400000 will most
255         * probably depend on the machine and OBP version (the workaround now
256         * works on Simics). A solution would be to inspect the "available"
257         * property of the "/memory" node to find out which parts of memory
258         * are used by OBP and redesign the algorithm of copying
259         * kernel/init tasks/ramdisk from the bootable image to memory
260         * (which we must do anyway because of issues with claiming the memory
261         * on Serengeti).
262         */
263        bootinfo.physmem_start += 0x400000;
264        bootinfo.memmap.zones[0].start += 0x400000;
265        bootinfo.memmap.zones[0].size -= 0x400000;
266#if 0
267        printf("The sun4v init finished.");
268#endif
269}
270#endif
271
272void bootstrap(void)
273{
274#if 0
275        void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
276        unsigned int top = 0;
277        unsigned int i;
278        unsigned int j;
279#endif
280
281  detect_architecture();
282#if 0
283        init_components(components);
284#endif
285       
286        if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
287                printk("Error: unable to get start of physical memory.\n");
288                halt();
289        }
290       
291        if (!ofw_memmap(&bootinfo.memmap)) {
292                printk("Error: unable to get memory map, halting.\n");
293                halt();
294        }
295       
296        if (bootinfo.memmap.total == 0) {
297                printk("Error: no memory detected, halting.\n");
298                halt();
299        }
300       
301        /*
302         * SILO for some reason adds 0x400000 and subtracts
303         * bootinfo.physmem_start to/from silo_ramdisk_image.
304         * We just need plain physical address so we fix it up.
305         */
306        if (silo_ramdisk_image) {
307                silo_ramdisk_image += bootinfo.physmem_start;
308                silo_ramdisk_image -= 0x400000;
309               
310                /* Install 1:1 mapping for the RAM disk. */
311                if (ofw_map((void *) ((uintptr_t) silo_ramdisk_image),
312                    (void *) ((uintptr_t) silo_ramdisk_image),
313                    silo_ramdisk_size, -1) != 0) {
314                        printk("Failed to map RAM disk.\n");
315                        halt();
316                }
317        }
318       
319  printk("\nMemory statistics (total %d MB, starting at %" PRIxPTR ")\n",
320            bootinfo.memmap.total >> 20, bootinfo.physmem_start);
321        printk(" %x: kernel entry point\n", KERNEL_VIRTUAL_ADDRESS);
322        printk(" %p: boot info structure\n", &bootinfo);
323
324#if 0
325        /*
326         * Figure out destination address for each component.
327         * In this phase, we don't copy the components yet because we want to
328         * to be careful not to overwrite anything, especially the components
329         * which haven't been copied yet.
330         */
331        bootinfo.taskmap.count = 0;
332        for (i = 0; i < COMPONENTS; i++) {
333                printf(" %P: %s image (size %d bytes)\n", components[i].start,
334                    components[i].name, components[i].size);
335                top = ALIGN_UP(top, PAGE_SIZE);
336                if (i > 0) {
337                        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
338                                printf("Skipping superfluous components.\n");
339                                break;
340                        }
341                       
342                        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
343                            base + top;
344                        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
345                            components[i].size;
346                        strncpy(bootinfo.taskmap.tasks[
347                            bootinfo.taskmap.count].name, components[i].name,
348                            BOOTINFO_TASK_NAME_BUFLEN);
349                        bootinfo.taskmap.count++;
350                }
351                top += components[i].size;
352        }
353       
354        printf("\n");
355
356        /* Do not consider RAM disk */
357        j = bootinfo.taskmap.count - 1;
358       
359        if (silo_ramdisk_image) {
360                /* Treat the RAM disk as the last bootinfo task. */
361                if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
362                        printf("Skipping RAM disk.\n");
363                        goto skip_ramdisk;
364                }
365               
366                top = ALIGN_UP(top, PAGE_SIZE);
367                bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
368                    base + top;
369                bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
370                    silo_ramdisk_size;
371                bootinfo.taskmap.count++;
372                printf("Copying RAM disk...");
373               
374                /*
375                 * Claim and map the whole ramdisk as it may exceed the area
376                 * given to us by SILO.
377                 */
378                (void) ofw_claim_phys(base + top, silo_ramdisk_size);
379                (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
380                    silo_ramdisk_size, -1);
381                memmove(base + top, (void *) ((uintptr_t) silo_ramdisk_image),
382                    silo_ramdisk_size);
383               
384                printf("done.\n");
385                top += silo_ramdisk_size;
386        }
387skip_ramdisk:
388       
389        /*
390         * Now we can proceed to copy the components. We do it in reverse order
391         * so that we don't overwrite anything even if the components overlap
392         * with base.
393         */
394        printf("Copying tasks...");
395        for (i = COMPONENTS - 1; i > 0; i--, j--) {
396                printf("%s ", components[i].name);
397               
398                /*
399                 * At this point, we claim the physical memory that we are
400                 * going to use. We should be safe in case of the virtual
401                 * address space because the OpenFirmware, according to its
402                 * SPARC binding, should restrict its use of virtual memory
403                 * to addresses from [0xffd00000; 0xffefffff] and
404                 * [0xfe000000; 0xfeffffff].
405                 *
406                 * XXX We don't map this piece of memory. We simply rely on
407                 *     SILO to have it done for us already in this case.
408                 */
409                (void) ofw_claim_phys(bootinfo.physmem_start +
410                    bootinfo.taskmap.tasks[j].addr,
411                    ALIGN_UP(components[i].size, PAGE_SIZE));
412               
413                memcpy((void *) bootinfo.taskmap.tasks[j].addr,
414                    components[i].start, components[i].size);
415               
416        }
417        printf(".\n");
418       
419        printf("Copying kernel...");
420        (void) ofw_claim_phys(bootinfo.physmem_start + base,
421            ALIGN_UP(components[0].size, PAGE_SIZE));
422        memcpy(base, components[0].start, components[0].size);
423        printf("done.\n");
424       
425        /* perform architecture-specific initialization */
426        if (architecture == COMPATIBLE_SUN4U) {
427                bootstrap_sun4u(base, top);
428        } else if (architecture == COMPATIBLE_SUN4V) {
429                bootstrap_sun4v();
430        } else {
431                printf("Unknown architecture.\n");
432                halt();
433        }
434       
435        printf("Booting the kernel...\n");
436        jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
437            bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
438            sizeof(bootinfo), subarchitecture);
439#endif
440}
Note: See TracBrowser for help on using the repository browser.