Subversion Repositories HelenOS

Rev

Rev 3743 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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