Subversion Repositories HelenOS

Rev

Rev 4342 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4342 Rev 4345
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
3
 * Copyright (c) 2006 Jakub Jermar
3
 * Copyright (c) 2006 Jakub Jermar
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
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
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.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
#include "main.h" 
30
#include "main.h" 
31
#include <printf.h>
31
#include <printf.h>
32
#include "asm.h"
32
#include "asm.h"
33
#include "_components.h"
33
#include "_components.h"
34
#include <balloc.h>
34
#include <balloc.h>
35
#include <ofw.h>
35
#include <ofw.h>
36
#include <ofw_tree.h>
36
#include <ofw_tree.h>
37
#include "ofwarch.h"
37
#include "ofwarch.h"
38
#include <align.h>
38
#include <align.h>
39
#include <macros.h>
39
#include <macros.h>
40
#include <string.h>
40
#include <string.h>
41
 
41
 
42
bootinfo_t bootinfo;
42
bootinfo_t bootinfo;
43
 
43
 
44
component_t components[COMPONENTS];
44
component_t components[COMPONENTS];
45
 
45
 
46
char *release = STRING(RELEASE);
46
char *release = STRING(RELEASE);
47
 
47
 
48
#ifdef REVISION
48
#ifdef REVISION
49
    char *revision = ", revision " STRING(REVISION);
49
    char *revision = ", revision " STRING(REVISION);
50
#else
50
#else
51
    char *revision = "";
51
    char *revision = "";
52
#endif
52
#endif
53
 
53
 
54
#ifdef TIMESTAMP
54
#ifdef TIMESTAMP
55
    char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
55
    char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
56
#else
56
#else
57
    char *timestamp = "";
57
    char *timestamp = "";
58
#endif
58
#endif
59
 
59
 
60
/** UltraSPARC subarchitecture - 1 for US, 3 for US3 */
60
/** UltraSPARC subarchitecture - 1 for US, 3 for US3 */
61
uint8_t subarchitecture;
61
uint8_t subarchitecture;
62
 
62
 
63
/**
63
/**
64
 * mask of the MID field inside the ICBUS_CONFIG register shifted by
64
 * mask of the MID field inside the ICBUS_CONFIG register shifted by
65
 * MID_SHIFT bits to the right
65
 * MID_SHIFT bits to the right
66
 */
66
 */
67
uint16_t mid_mask;
67
uint16_t mid_mask;
68
 
68
 
69
/** Print version information. */
69
/** Print version information. */
70
static void version_print(void)
70
static void version_print(void)
71
{
71
{
72
    printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
72
    printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
73
        "Copyright (c) 2006 HelenOS project\n",
73
        "Copyright (c) 2006 HelenOS project\n",
74
        release, revision, timestamp);
74
        release, revision, timestamp);
75
}
75
}
76
 
76
 
77
/* the lowest ID (read from the VER register) of some US3 CPU model */
77
/* the lowest ID (read from the VER register) of some US3 CPU model */
78
#define FIRST_US3_CPU   0x14
78
#define FIRST_US3_CPU   0x14
79
 
79
 
80
/* the greatest ID (read from the VER register) of some US3 CPU model */
80
/* the greatest ID (read from the VER register) of some US3 CPU model */
81
#define LAST_US3_CPU    0x19
81
#define LAST_US3_CPU    0x19
82
 
82
 
83
/* UltraSPARC IIIi processor implementation code */
83
/* UltraSPARC IIIi processor implementation code */
84
#define US_IIIi_CODE    0x15
84
#define US_IIIi_CODE    0x15
85
 
85
 
86
/**
86
/**
87
 * Sets the global variables "subarchitecture" and "mid_mask" to
87
 * Sets the global variables "subarchitecture" and "mid_mask" to
88
 * correct values.
88
 * correct values.
89
 */
89
 */
90
static void detect_subarchitecture(void)
90
static void detect_subarchitecture(void)
91
{
91
{
92
    uint64_t v;
92
    uint64_t v;
93
    asm volatile ("rdpr %%ver, %0\n" : "=r" (v));
93
    asm volatile ("rdpr %%ver, %0\n" : "=r" (v));
94
   
94
   
95
    v = (v << 16) >> 48;
95
    v = (v << 16) >> 48;
96
    if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
96
    if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
97
        subarchitecture = SUBARCH_US3;
97
        subarchitecture = SUBARCH_US3;
98
        if (v == US_IIIi_CODE)
98
        if (v == US_IIIi_CODE)
99
            mid_mask = (1 << 5) - 1;
99
            mid_mask = (1 << 5) - 1;
100
        else
100
        else
101
            mid_mask = (1 << 10) - 1;
101
            mid_mask = (1 << 10) - 1;
102
    } else if (v < FIRST_US3_CPU) {
102
    } else if (v < FIRST_US3_CPU) {
103
        subarchitecture = SUBARCH_US;
103
        subarchitecture = SUBARCH_US;
104
        mid_mask = (1 << 5) - 1;
104
        mid_mask = (1 << 5) - 1;
105
    } else {
105
    } else {
106
        printf("\nThis CPU is not supported by HelenOS.");
106
        printf("\nThis CPU is not supported by HelenOS.");
107
    }
107
    }
108
}
108
}
109
 
109
 
110
void bootstrap(void)
110
void bootstrap(void)
111
{
111
{
112
    void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
112
    void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
113
    void *balloc_base;
113
    void *balloc_base;
114
    unsigned int top = 0;
114
    unsigned int top = 0;
115
    int i, j;
115
    int i, j;
116
 
116
 
117
    version_print();
117
    version_print();
118
   
118
   
119
    detect_subarchitecture();
119
    detect_subarchitecture();
120
    init_components(components);
120
    init_components(components);
121
 
121
 
122
    if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
122
    if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
123
        printf("Error: unable to get start of physical memory.\n");
123
        printf("Error: unable to get start of physical memory.\n");
124
        halt();
124
        halt();
125
    }
125
    }
126
 
126
 
127
    if (!ofw_memmap(&bootinfo.memmap)) {
127
    if (!ofw_memmap(&bootinfo.memmap)) {
128
        printf("Error: unable to get memory map, halting.\n");
128
        printf("Error: unable to get memory map, halting.\n");
129
        halt();
129
        halt();
130
    }
130
    }
131
 
131
 
132
    if (bootinfo.memmap.total == 0) {
132
    if (bootinfo.memmap.total == 0) {
133
        printf("Error: no memory detected, halting.\n");
133
        printf("Error: no memory detected, halting.\n");
134
        halt();
134
        halt();
135
    }
135
    }
136
 
136
 
137
    /*
137
    /*
138
     * SILO for some reason adds 0x400000 and subtracts
138
     * SILO for some reason adds 0x400000 and subtracts
139
     * bootinfo.physmem_start to/from silo_ramdisk_image.
139
     * bootinfo.physmem_start to/from silo_ramdisk_image.
140
     * We just need plain physical address so we fix it up.
140
     * We just need plain physical address so we fix it up.
141
     */
141
     */
142
    if (silo_ramdisk_image) {
142
    if (silo_ramdisk_image) {
143
        silo_ramdisk_image += bootinfo.physmem_start;
143
        silo_ramdisk_image += bootinfo.physmem_start;
144
        silo_ramdisk_image -= 0x400000;
144
        silo_ramdisk_image -= 0x400000;
145
        /* Install 1:1 mapping for the ramdisk. */
145
        /* Install 1:1 mapping for the ramdisk. */
146
        if (ofw_map((void *)((uintptr_t) silo_ramdisk_image),
146
        if (ofw_map((void *)((uintptr_t) silo_ramdisk_image),
147
            (void *)((uintptr_t) silo_ramdisk_image),
147
            (void *)((uintptr_t) silo_ramdisk_image),
148
            silo_ramdisk_size, -1) != 0) {
148
            silo_ramdisk_size, -1) != 0) {
149
            printf("Failed to map ramdisk.\n");
149
            printf("Failed to map ramdisk.\n");
150
            halt();
150
            halt();
151
        }
151
        }
152
    }
152
    }
153
   
153
   
154
    printf("\nSystem info\n");
154
    printf("\nSystem info\n");
155
    printf(" memory: %dM starting at %P\n",
155
    printf(" memory: %dM starting at %P\n",
156
        bootinfo.memmap.total >> 20, bootinfo.physmem_start);
156
        bootinfo.memmap.total >> 20, bootinfo.physmem_start);
157
 
157
 
158
    printf("\nMemory statistics\n");
158
    printf("\nMemory statistics\n");
159
    printf(" kernel entry point at %P\n", KERNEL_VIRTUAL_ADDRESS);
159
    printf(" kernel entry point at %P\n", KERNEL_VIRTUAL_ADDRESS);
160
    printf(" %P: boot info structure\n", &bootinfo);
160
    printf(" %P: boot info structure\n", &bootinfo);
161
   
161
   
162
    /*
162
    /*
163
     * Figure out destination address for each component.
163
     * Figure out destination address for each component.
164
     * In this phase, we don't copy the components yet because we want to
164
     * In this phase, we don't copy the components yet because we want to
165
     * to be careful not to overwrite anything, especially the components
165
     * to be careful not to overwrite anything, especially the components
166
     * which haven't been copied yet.
166
     * which haven't been copied yet.
167
     */
167
     */
168
    bootinfo.taskmap.count = 0;
168
    bootinfo.taskmap.count = 0;
169
    for (i = 0; i < COMPONENTS; i++) {
169
    for (i = 0; i < COMPONENTS; i++) {
170
        printf(" %P: %s image (size %d bytes)\n", components[i].start,
170
        printf(" %P: %s image (size %d bytes)\n", components[i].start,
171
            components[i].name, components[i].size);
171
            components[i].name, components[i].size);
172
        top = ALIGN_UP(top, PAGE_SIZE);
172
        top = ALIGN_UP(top, PAGE_SIZE);
173
        if (i > 0) {
173
        if (i > 0) {
174
            if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
174
            if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
175
                printf("Skipping superfluous components.\n");
175
                printf("Skipping superfluous components.\n");
176
                break;
176
                break;
177
            }
177
            }
178
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
178
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
179
                base + top;
179
                base + top;
180
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
180
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
181
                components[i].size;
181
                components[i].size;
-
 
182
            strncpy(bootinfo.taskmap.tasks[
-
 
183
                bootinfo.taskmap.count].name, components[i].name,
-
 
184
                BOOTINFO_TASK_NAME_BUFLEN);
182
            bootinfo.taskmap.count++;
185
            bootinfo.taskmap.count++;
183
        }
186
        }
184
        top += components[i].size;
187
        top += components[i].size;
185
    }
188
    }
186
 
189
 
187
    j = bootinfo.taskmap.count - 1; /* do not consider ramdisk */
190
    j = bootinfo.taskmap.count - 1; /* do not consider ramdisk */
188
 
191
 
189
    if (silo_ramdisk_image) {
192
    if (silo_ramdisk_image) {
190
        /* Treat the ramdisk as the last bootinfo task. */
193
        /* Treat the ramdisk as the last bootinfo task. */
191
        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
194
        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
192
            printf("Skipping ramdisk.\n");
195
            printf("Skipping ramdisk.\n");
193
            goto skip_ramdisk;
196
            goto skip_ramdisk;
194
        }
197
        }
195
        top = ALIGN_UP(top, PAGE_SIZE);
198
        top = ALIGN_UP(top, PAGE_SIZE);
196
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
199
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
197
            base + top;
200
            base + top;
198
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
201
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
199
            silo_ramdisk_size;
202
            silo_ramdisk_size;
200
        bootinfo.taskmap.count++;
203
        bootinfo.taskmap.count++;
201
        printf("\nCopying ramdisk...");
204
        printf("\nCopying ramdisk...");
202
        /*
205
        /*
203
         * Claim and map the whole ramdisk as it may exceed the area
206
         * Claim and map the whole ramdisk as it may exceed the area
204
         * given to us by SILO.
207
         * given to us by SILO.
205
         */
208
         */
206
        (void) ofw_claim_phys(base + top, silo_ramdisk_size);
209
        (void) ofw_claim_phys(base + top, silo_ramdisk_size);
207
        (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
210
        (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
208
            silo_ramdisk_size, -1);
211
            silo_ramdisk_size, -1);
209
        /*
212
        /*
210
         * FIXME If the source and destination overlap, it may be
213
         * FIXME If the source and destination overlap, it may be
211
         * desirable to copy in reverse order, depending on how the two
214
         * desirable to copy in reverse order, depending on how the two
212
         * regions overlap.
215
         * regions overlap.
213
         */
216
         */
214
        memcpy(base + top, (void *)((uintptr_t)silo_ramdisk_image),
217
        memcpy(base + top, (void *)((uintptr_t)silo_ramdisk_image),
215
            silo_ramdisk_size);
218
            silo_ramdisk_size);
216
        printf("done.\n");
219
        printf("done.\n");
217
        top += silo_ramdisk_size;
220
        top += silo_ramdisk_size;
218
    }
221
    }
219
skip_ramdisk:
222
skip_ramdisk:
220
 
223
 
221
    /*
224
    /*
222
     * Now we can proceed to copy the components. We do it in reverse order
225
     * Now we can proceed to copy the components. We do it in reverse order
223
     * so that we don't overwrite anything even if the components overlap
226
     * so that we don't overwrite anything even if the components overlap
224
     * with base.
227
     * with base.
225
     */
228
     */
226
    printf("\nCopying bootinfo tasks\n");
229
    printf("\nCopying bootinfo tasks\n");
227
    for (i = COMPONENTS - 1; i > 0; i--, j--) {
230
    for (i = COMPONENTS - 1; i > 0; i--, j--) {
228
        printf(" %s...", components[i].name);
231
        printf(" %s...", components[i].name);
229
 
232
 
230
        /*
233
        /*
231
         * At this point, we claim the physical memory that we are
234
         * At this point, we claim the physical memory that we are
232
         * going to use. We should be safe in case of the virtual
235
         * going to use. We should be safe in case of the virtual
233
         * address space because the OpenFirmware, according to its
236
         * address space because the OpenFirmware, according to its
234
         * SPARC binding, should restrict its use of virtual memory
237
         * SPARC binding, should restrict its use of virtual memory
235
         * to addresses from [0xffd00000; 0xffefffff] and
238
         * to addresses from [0xffd00000; 0xffefffff] and
236
         * [0xfe000000; 0xfeffffff].
239
         * [0xfe000000; 0xfeffffff].
237
         *
240
         *
238
         * XXX We don't map this piece of memory. We simply rely on
241
         * XXX We don't map this piece of memory. We simply rely on
239
         *     SILO to have it done for us already in this case.
242
         *     SILO to have it done for us already in this case.
240
         */
243
         */
241
        (void) ofw_claim_phys(bootinfo.physmem_start +
244
        (void) ofw_claim_phys(bootinfo.physmem_start +
242
            bootinfo.taskmap.tasks[j].addr,
245
            bootinfo.taskmap.tasks[j].addr,
243
            ALIGN_UP(components[i].size, PAGE_SIZE));
246
            ALIGN_UP(components[i].size, PAGE_SIZE));
244
           
247
           
245
        memcpy((void *)bootinfo.taskmap.tasks[j].addr,
248
        memcpy((void *)bootinfo.taskmap.tasks[j].addr,
246
            components[i].start, components[i].size);
249
            components[i].start, components[i].size);
247
        printf("done.\n");
250
        printf("done.\n");
248
    }
251
    }
249
 
252
 
250
    printf("\nCopying kernel...");
253
    printf("\nCopying kernel...");
251
    (void) ofw_claim_phys(bootinfo.physmem_start + base,
254
    (void) ofw_claim_phys(bootinfo.physmem_start + base,
252
        ALIGN_UP(components[0].size, PAGE_SIZE));
255
        ALIGN_UP(components[0].size, PAGE_SIZE));
253
    memcpy(base, components[0].start, components[0].size);
256
    memcpy(base, components[0].start, components[0].size);
254
    printf("done.\n");
257
    printf("done.\n");
255
 
258
 
256
    /*
259
    /*
257
     * Claim and map the physical memory for the boot allocator.
260
     * Claim and map the physical memory for the boot allocator.
258
     * Initialize the boot allocator.
261
     * Initialize the boot allocator.
259
     */
262
     */
260
    balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
263
    balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
261
    (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
264
    (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
262
        BALLOC_MAX_SIZE);
265
        BALLOC_MAX_SIZE);
263
    (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
266
    (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
264
        BALLOC_MAX_SIZE, -1);
267
        BALLOC_MAX_SIZE, -1);
265
    balloc_init(&bootinfo.ballocs, (uintptr_t)balloc_base);
268
    balloc_init(&bootinfo.ballocs, (uintptr_t)balloc_base);
266
 
269
 
267
    printf("\nCanonizing OpenFirmware device tree...");
270
    printf("\nCanonizing OpenFirmware device tree...");
268
    bootinfo.ofw_root = ofw_tree_build();
271
    bootinfo.ofw_root = ofw_tree_build();
269
    printf("done.\n");
272
    printf("done.\n");
270
 
273
 
271
#ifdef CONFIG_AP
274
#ifdef CONFIG_AP
272
    printf("\nChecking for secondary processors...");
275
    printf("\nChecking for secondary processors...");
273
    if (!ofw_cpu())
276
    if (!ofw_cpu())
274
        printf("Error: unable to get CPU properties\n");
277
        printf("Error: unable to get CPU properties\n");
275
    printf("done.\n");
278
    printf("done.\n");
276
#endif
279
#endif
277
 
280
 
278
    ofw_setup_palette();
281
    ofw_setup_palette();
279
 
282
 
280
    printf("\nBooting the kernel...\n");
283
    printf("\nBooting the kernel...\n");
281
    jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
284
    jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
282
        bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
285
        bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
283
        sizeof(bootinfo));
286
        sizeof(bootinfo));
284
}
287
}
285
 
288