Subversion Repositories HelenOS

Rev

Rev 3742 | 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.
155
 */
156
static void bootstrap_sun4u(void)
157
{
158
    printf("\nCanonizing OpenFirmware device tree...");
159
    bootinfo.ofw_root = ofw_tree_build();
160
    printf("done.\n");
161
 
162
    detect_subarchitecture();
163
 
164
#ifdef CONFIG_SMP
165
    printf("\nChecking for secondary processors...");
166
    if (!ofw_cpu())
167
        printf("Error: unable to get CPU properties\n");
168
    printf("done.\n");
169
#endif
170
 
171
    setup_palette();
172
}
173
 
174
/**
175
 * Performs sun4v-specific initialization. The components are expected
176
 * to be already copied and boot allocator initialized.
177
 */
178
static void bootstrap_sun4v(void)
179
{
180
}
181
 
1018 decky 182
void bootstrap(void)
183
{
3492 rimsky 184
    void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
185
    void *balloc_base;
186
    unsigned int top = 0;
187
    int i, j;
188
 
3743 rimsky 189
    detect_architecture();
1685 decky 190
    init_components(components);
1782 jermar 191
 
1978 jermar 192
    if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
193
        printf("Error: unable to get start of physical memory.\n");
194
        halt();
195
    }
196
 
1789 jermar 197
    if (!ofw_memmap(&bootinfo.memmap)) {
198
        printf("Error: unable to get memory map, halting.\n");
199
        halt();
200
    }
3502 rimsky 201
 
1789 jermar 202
    if (bootinfo.memmap.total == 0) {
203
        printf("Error: no memory detected, halting.\n");
204
        halt();
205
    }
3397 rimsky 206
 
207
    /*
208
     * SILO for some reason adds 0x400000 and subtracts
209
     * bootinfo.physmem_start to/from silo_ramdisk_image.
210
     * We just need plain physical address so we fix it up.
211
     */
212
    if (silo_ramdisk_image) {
213
        silo_ramdisk_image += bootinfo.physmem_start;
214
        silo_ramdisk_image -= 0x400000;
3492 rimsky 215
        /* Install 1:1 mapping for the ramdisk. */
216
        if (ofw_map((void *)((uintptr_t)silo_ramdisk_image),
217
            (void *)((uintptr_t)silo_ramdisk_image),
218
            silo_ramdisk_size, -1) != 0) {
219
            printf("Failed to map ramdisk.\n");
220
            halt();
221
        }
3397 rimsky 222
    }
1789 jermar 223
 
1899 jermar 224
    printf("\nSystem info\n");
1978 jermar 225
    printf(" memory: %dM starting at %P\n",
3397 rimsky 226
        bootinfo.memmap.total >> 20, bootinfo.physmem_start);
1789 jermar 227
 
1685 decky 228
    printf("\nMemory statistics\n");
1789 jermar 229
    printf(" kernel entry point at %P\n", KERNEL_VIRTUAL_ADDRESS);
230
    printf(" %P: boot info structure\n", &bootinfo);
1685 decky 231
 
3492 rimsky 232
    /*
233
     * Figure out destination address for each component.
234
     * In this phase, we don't copy the components yet because we want to
235
     * to be careful not to overwrite anything, especially the components
236
     * which haven't been copied yet.
237
     */
238
    bootinfo.taskmap.count = 0;
239
    for (i = 0; i < COMPONENTS; i++) {
1978 jermar 240
        printf(" %P: %s image (size %d bytes)\n", components[i].start,
2250 jermar 241
            components[i].name, components[i].size);
3492 rimsky 242
        top = ALIGN_UP(top, PAGE_SIZE);
243
        if (i > 0) {
244
            if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
245
                printf("Skipping superfluous components.\n");
246
                break;
247
            }
248
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
249
                base + top;
250
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
251
                components[i].size;
252
            bootinfo.taskmap.count++;
253
        }
254
        top += components[i].size;
255
    }
1782 jermar 256
 
3492 rimsky 257
    j = bootinfo.taskmap.count - 1; /* do not consider ramdisk */
1894 jermar 258
 
3492 rimsky 259
    if (silo_ramdisk_image) {
260
        /* Treat the ramdisk as the last bootinfo task. */
261
        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
262
            printf("Skipping ramdisk.\n");
263
            goto skip_ramdisk;
264
        }
1685 decky 265
        top = ALIGN_UP(top, PAGE_SIZE);
3492 rimsky 266
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
267
            base + top;
268
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
269
            silo_ramdisk_size;
270
        bootinfo.taskmap.count++;
271
        printf("\nCopying ramdisk...");
272
        /*
273
         * Claim and map the whole ramdisk as it may exceed the area
274
         * given to us by SILO.
275
         */
276
        (void) ofw_claim_phys(base + top, silo_ramdisk_size);
277
        (void) ofw_map(base + top, base + top, silo_ramdisk_size, -1);
278
        memmove(base + top, (void *)((uintptr_t)silo_ramdisk_image),
279
            silo_ramdisk_size);
280
        printf("done.\n");
281
        top += silo_ramdisk_size;
282
    }
283
skip_ramdisk:
2250 jermar 284
 
3492 rimsky 285
    /*
286
     * Now we can proceed to copy the components. We do it in reverse order
287
     * so that we don't overwrite anything even if the components overlap
288
     * with base.
289
     */
290
    printf("\nCopying bootinfo tasks\n");
291
    for (i = COMPONENTS - 1; i > 0; i--, j--) {
292
        printf(" %s...", components[i].name);
293
 
2250 jermar 294
        /*
295
         * At this point, we claim the physical memory that we are
296
         * going to use. We should be safe in case of the virtual
297
         * address space because the OpenFirmware, according to its
298
         * SPARC binding, should restrict its use of virtual memory
299
         * to addresses from [0xffd00000; 0xffefffff] and
300
         * [0xfe000000; 0xfeffffff].
3492 rimsky 301
         *
302
         * XXX We don't map this piece of memory. We simply rely on
303
         *     SILO to have it done for us already in this case.
2250 jermar 304
         */
3492 rimsky 305
        (void) ofw_claim_phys(bootinfo.physmem_start +
306
            bootinfo.taskmap.tasks[j].addr,
2250 jermar 307
            ALIGN_UP(components[i].size, PAGE_SIZE));
308
 
3492 rimsky 309
        memcpy((void *)bootinfo.taskmap.tasks[j].addr,
310
            components[i].start, components[i].size);
1685 decky 311
        printf("done.\n");
1018 decky 312
    }
1782 jermar 313
 
3492 rimsky 314
    printf("\nCopying kernel...");
315
    (void) ofw_claim_phys(bootinfo.physmem_start + base,
316
        ALIGN_UP(components[0].size, PAGE_SIZE));
317
    memcpy(base, components[0].start, components[0].size);
318
    printf("done.\n");
319
 
2250 jermar 320
    /*
3492 rimsky 321
     * Claim and map the physical memory for the boot allocator.
2250 jermar 322
     * Initialize the boot allocator.
323
     */
3492 rimsky 324
    balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
325
    (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
326
        BALLOC_MAX_SIZE);
327
    (void) ofw_map(balloc_base, balloc_base, BALLOC_MAX_SIZE, -1);
328
    balloc_init(&bootinfo.ballocs, (uintptr_t)balloc_base);
1894 jermar 329
 
3743 rimsky 330
    /* perform architecture-specific initialization */
331
    if (architecture == COMPATIBLE_SUN4U) {
332
        bootstrap_sun4u();
333
    } else if (architecture == COMPATIBLE_SUN4V) {
334
        bootstrap_sun4v();
335
    } else {
336
        printf("Unknown architecture.\n");
337
        halt();
338
    }
1894 jermar 339
 
1018 decky 340
    printf("\nBooting the kernel...\n");
1978 jermar 341
    jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
2250 jermar 342
        bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
343
        sizeof(bootinfo));
1018 decky 344
}
2250 jermar 345