Subversion Repositories HelenOS

Rev

Rev 4337 | Rev 4342 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4337 Rev 4341
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
 
39
 
40
bootinfo_t bootinfo;
40
bootinfo_t bootinfo;
41
 
41
 
42
component_t components[COMPONENTS];
42
component_t components[COMPONENTS];
43
 
43
 
44
char *release = RELEASE;
44
char *release = RELEASE;
45
 
45
 
46
#ifdef REVISION
46
#ifdef REVISION
47
    char *revision = ", revision " REVISION;
47
    char *revision = ", revision " REVISION;
48
#else
48
#else
49
    char *revision = "";
49
    char *revision = "";
50
#endif
50
#endif
51
 
51
 
52
#ifdef TIMESTAMP
52
#ifdef TIMESTAMP
53
    char *timestamp = "\nBuilt on " TIMESTAMP;
53
    char *timestamp = "\nBuilt on " TIMESTAMP;
54
#else
54
#else
55
    char *timestamp = "";
55
    char *timestamp = "";
56
#endif
56
#endif
57
 
57
 
58
/** UltraSPARC subarchitecture - 1 for US, 3 for US3 */
58
/** UltraSPARC subarchitecture - 1 for US, 3 for US3 */
59
uint8_t subarchitecture;
59
uint8_t subarchitecture;
60
 
60
 
61
/**
61
/**
62
 * mask of the MID field inside the ICBUS_CONFIG register shifted by
62
 * mask of the MID field inside the ICBUS_CONFIG register shifted by
63
 * MID_SHIFT bits to the right
63
 * MID_SHIFT bits to the right
64
 */
64
 */
65
uint16_t mid_mask;
65
uint16_t mid_mask;
66
 
66
 
67
/** Print version information. */
67
/** Print version information. */
68
static void version_print(void)
68
static void version_print(void)
69
{
69
{
70
    printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
70
    printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
71
        "Copyright (c) 2006 HelenOS project\n",
71
        "Copyright (c) 2006 HelenOS project\n",
72
        release, revision, timestamp);
72
        release, revision, timestamp);
73
}
73
}
74
 
74
 
75
/* the lowest ID (read from the VER register) of some US3 CPU model */
75
/* the lowest ID (read from the VER register) of some US3 CPU model */
76
#define FIRST_US3_CPU   0x14
76
#define FIRST_US3_CPU   0x14
77
 
77
 
78
/* the greatest ID (read from the VER register) of some US3 CPU model */
78
/* the greatest ID (read from the VER register) of some US3 CPU model */
79
#define LAST_US3_CPU    0x19
79
#define LAST_US3_CPU    0x19
80
 
80
 
81
/* UltraSPARC IIIi processor implementation code */
81
/* UltraSPARC IIIi processor implementation code */
82
#define US_IIIi_CODE    0x15
82
#define US_IIIi_CODE    0x15
83
 
83
 
84
/**
84
/**
85
 * Sets the global variables "subarchitecture" and "mid_mask" to
85
 * Sets the global variables "subarchitecture" and "mid_mask" to
86
 * correct values.
86
 * correct values.
87
 */
87
 */
88
static void detect_subarchitecture(void)
88
static void detect_subarchitecture(void)
89
{
89
{
90
    uint64_t v;
90
    uint64_t v;
91
    asm volatile ("rdpr %%ver, %0\n" : "=r" (v));
91
    asm volatile ("rdpr %%ver, %0\n" : "=r" (v));
92
   
92
   
93
    v = (v << 16) >> 48;
93
    v = (v << 16) >> 48;
94
    if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
94
    if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
95
        subarchitecture = SUBARCH_US3;
95
        subarchitecture = SUBARCH_US3;
96
        if (v == US_IIIi_CODE)
96
        if (v == US_IIIi_CODE)
97
            mid_mask = (1 << 5) - 1;
97
            mid_mask = (1 << 5) - 1;
98
        else
98
        else
99
            mid_mask = (1 << 10) - 1;
99
            mid_mask = (1 << 10) - 1;
100
    } else if (v < FIRST_US3_CPU) {
100
    } else if (v < FIRST_US3_CPU) {
101
        subarchitecture = SUBARCH_US;
101
        subarchitecture = SUBARCH_US;
102
        mid_mask = (1 << 5) - 1;
102
        mid_mask = (1 << 5) - 1;
103
    } else {
103
    } else {
104
        printf("\nThis CPU is not supported by HelenOS.");
104
        printf("\nThis CPU is not supported by HelenOS.");
105
    }
105
    }
106
}
106
}
107
 
107
 
108
void bootstrap(void)
108
void bootstrap(void)
109
{
109
{
110
    void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
110
    void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
111
    void *balloc_base;
111
    void *balloc_base;
112
    unsigned int top = 0;
112
    unsigned int top = 0;
113
    int i, j;
113
    int i, j;
114
 
114
 
115
    version_print();
115
    version_print();
116
   
116
   
117
    detect_subarchitecture();
117
    detect_subarchitecture();
118
    init_components(components);
118
    init_components(components);
119
 
119
 
120
    if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
120
    if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
121
        printf("Error: unable to get start of physical memory.\n");
121
        printf("Error: unable to get start of physical memory.\n");
122
        halt();
122
        halt();
123
    }
123
    }
124
 
124
 
125
    if (!ofw_memmap(&bootinfo.memmap)) {
125
    if (!ofw_memmap(&bootinfo.memmap)) {
126
        printf("Error: unable to get memory map, halting.\n");
126
        printf("Error: unable to get memory map, halting.\n");
127
        halt();
127
        halt();
128
    }
128
    }
129
 
129
 
130
    if (bootinfo.memmap.total == 0) {
130
    if (bootinfo.memmap.total == 0) {
131
        printf("Error: no memory detected, halting.\n");
131
        printf("Error: no memory detected, halting.\n");
132
        halt();
132
        halt();
133
    }
133
    }
134
 
134
 
135
    /*
135
    /*
136
     * SILO for some reason adds 0x400000 and subtracts
136
     * SILO for some reason adds 0x400000 and subtracts
137
     * bootinfo.physmem_start to/from silo_ramdisk_image.
137
     * bootinfo.physmem_start to/from silo_ramdisk_image.
138
     * We just need plain physical address so we fix it up.
138
     * We just need plain physical address so we fix it up.
139
     */
139
     */
140
    if (silo_ramdisk_image) {
140
    if (silo_ramdisk_image) {
141
        silo_ramdisk_image += bootinfo.physmem_start;
141
        silo_ramdisk_image += bootinfo.physmem_start;
142
        silo_ramdisk_image -= 0x400000;
142
        silo_ramdisk_image -= 0x400000;
143
        /* Install 1:1 mapping for the ramdisk. */
143
        /* Install 1:1 mapping for the ramdisk. */
144
        if (ofw_map((void *)((uintptr_t)silo_ramdisk_image),
144
        if (ofw_map((void *)((uintptr_t) silo_ramdisk_image),
145
            (void *)((uintptr_t)silo_ramdisk_image),
145
            (void *)((uintptr_t) silo_ramdisk_image),
146
            silo_ramdisk_size, -1) != 0) {
146
            silo_ramdisk_size, -1) != 0) {
147
            printf("Failed to map ramdisk.\n");
147
            printf("Failed to map ramdisk.\n");
148
            halt();
148
            halt();
149
        }
149
        }
150
    }
150
    }
151
   
151
   
152
    printf("\nSystem info\n");
152
    printf("\nSystem info\n");
153
    printf(" memory: %dM starting at %P\n",
153
    printf(" memory: %dM starting at %P\n",
154
        bootinfo.memmap.total >> 20, bootinfo.physmem_start);
154
        bootinfo.memmap.total >> 20, bootinfo.physmem_start);
155
 
155
 
156
    printf("\nMemory statistics\n");
156
    printf("\nMemory statistics\n");
157
    printf(" kernel entry point at %P\n", KERNEL_VIRTUAL_ADDRESS);
157
    printf(" kernel entry point at %P\n", KERNEL_VIRTUAL_ADDRESS);
158
    printf(" %P: boot info structure\n", &bootinfo);
158
    printf(" %P: boot info structure\n", &bootinfo);
159
   
159
   
160
    /*
160
    /*
161
     * Figure out destination address for each component.
161
     * Figure out destination address for each component.
162
     * In this phase, we don't copy the components yet because we want to
162
     * In this phase, we don't copy the components yet because we want to
163
     * to be careful not to overwrite anything, especially the components
163
     * to be careful not to overwrite anything, especially the components
164
     * which haven't been copied yet.
164
     * which haven't been copied yet.
165
     */
165
     */
166
    bootinfo.taskmap.count = 0;
166
    bootinfo.taskmap.count = 0;
167
    for (i = 0; i < COMPONENTS; i++) {
167
    for (i = 0; i < COMPONENTS; i++) {
168
        printf(" %P: %s image (size %d bytes)\n", components[i].start,
168
        printf(" %P: %s image (size %d bytes)\n", components[i].start,
169
            components[i].name, components[i].size);
169
            components[i].name, components[i].size);
170
        top = ALIGN_UP(top, PAGE_SIZE);
170
        top = ALIGN_UP(top, PAGE_SIZE);
171
        if (i > 0) {
171
        if (i > 0) {
172
            if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
172
            if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
173
                printf("Skipping superfluous components.\n");
173
                printf("Skipping superfluous components.\n");
174
                break;
174
                break;
175
            }
175
            }
176
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
176
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
177
                base + top;
177
                base + top;
178
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
178
            bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
179
                components[i].size;
179
                components[i].size;
180
            bootinfo.taskmap.count++;
180
            bootinfo.taskmap.count++;
181
        }
181
        }
182
        top += components[i].size;
182
        top += components[i].size;
183
    }
183
    }
184
 
184
 
185
    j = bootinfo.taskmap.count - 1; /* do not consider ramdisk */
185
    j = bootinfo.taskmap.count - 1; /* do not consider ramdisk */
186
 
186
 
187
    if (silo_ramdisk_image) {
187
    if (silo_ramdisk_image) {
188
        /* Treat the ramdisk as the last bootinfo task. */
188
        /* Treat the ramdisk as the last bootinfo task. */
189
        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
189
        if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
190
            printf("Skipping ramdisk.\n");
190
            printf("Skipping ramdisk.\n");
191
            goto skip_ramdisk;
191
            goto skip_ramdisk;
192
        }
192
        }
193
        top = ALIGN_UP(top, PAGE_SIZE);
193
        top = ALIGN_UP(top, PAGE_SIZE);
194
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
194
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
195
            base + top;
195
            base + top;
196
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
196
        bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
197
            silo_ramdisk_size;
197
            silo_ramdisk_size;
198
        bootinfo.taskmap.count++;
198
        bootinfo.taskmap.count++;
199
        printf("\nCopying ramdisk...");
199
        printf("\nCopying ramdisk...");
200
        /*
200
        /*
201
         * Claim and map the whole ramdisk as it may exceed the area
201
         * Claim and map the whole ramdisk as it may exceed the area
202
         * given to us by SILO.
202
         * given to us by SILO.
203
         */
203
         */
204
        (void) ofw_claim_phys(base + top, silo_ramdisk_size);
204
        (void) ofw_claim_phys(base + top, silo_ramdisk_size);
205
        (void) ofw_map(base + top, base + top, silo_ramdisk_size, -1);
205
        (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
-
 
206
            silo_ramdisk_size, -1);
206
        /*
207
        /*
207
         * FIXME If the source and destination overlap, it may be
208
         * FIXME If the source and destination overlap, it may be
208
         * desirable to copy in reverse order, depending on how the two
209
         * desirable to copy in reverse order, depending on how the two
209
         * regions overlap.
210
         * regions overlap.
210
         */
211
         */
211
        memcpy(base + top, (void *)((uintptr_t)silo_ramdisk_image),
212
        memcpy(base + top, (void *)((uintptr_t)silo_ramdisk_image),
212
            silo_ramdisk_size);
213
            silo_ramdisk_size);
213
        printf("done.\n");
214
        printf("done.\n");
214
        top += silo_ramdisk_size;
215
        top += silo_ramdisk_size;
215
    }
216
    }
216
skip_ramdisk:
217
skip_ramdisk:
217
 
218
 
218
    /*
219
    /*
219
     * Now we can proceed to copy the components. We do it in reverse order
220
     * Now we can proceed to copy the components. We do it in reverse order
220
     * so that we don't overwrite anything even if the components overlap
221
     * so that we don't overwrite anything even if the components overlap
221
     * with base.
222
     * with base.
222
     */
223
     */
223
    printf("\nCopying bootinfo tasks\n");
224
    printf("\nCopying bootinfo tasks\n");
224
    for (i = COMPONENTS - 1; i > 0; i--, j--) {
225
    for (i = COMPONENTS - 1; i > 0; i--, j--) {
225
        printf(" %s...", components[i].name);
226
        printf(" %s...", components[i].name);
226
 
227
 
227
        /*
228
        /*
228
         * At this point, we claim the physical memory that we are
229
         * At this point, we claim the physical memory that we are
229
         * going to use. We should be safe in case of the virtual
230
         * going to use. We should be safe in case of the virtual
230
         * address space because the OpenFirmware, according to its
231
         * address space because the OpenFirmware, according to its
231
         * SPARC binding, should restrict its use of virtual memory
232
         * SPARC binding, should restrict its use of virtual memory
232
         * to addresses from [0xffd00000; 0xffefffff] and
233
         * to addresses from [0xffd00000; 0xffefffff] and
233
         * [0xfe000000; 0xfeffffff].
234
         * [0xfe000000; 0xfeffffff].
234
         *
235
         *
235
         * XXX We don't map this piece of memory. We simply rely on
236
         * XXX We don't map this piece of memory. We simply rely on
236
         *     SILO to have it done for us already in this case.
237
         *     SILO to have it done for us already in this case.
237
         */
238
         */
238
        (void) ofw_claim_phys(bootinfo.physmem_start +
239
        (void) ofw_claim_phys(bootinfo.physmem_start +
239
            bootinfo.taskmap.tasks[j].addr,
240
            bootinfo.taskmap.tasks[j].addr,
240
            ALIGN_UP(components[i].size, PAGE_SIZE));
241
            ALIGN_UP(components[i].size, PAGE_SIZE));
241
           
242
           
242
        memcpy((void *)bootinfo.taskmap.tasks[j].addr,
243
        memcpy((void *)bootinfo.taskmap.tasks[j].addr,
243
            components[i].start, components[i].size);
244
            components[i].start, components[i].size);
244
        printf("done.\n");
245
        printf("done.\n");
245
    }
246
    }
246
 
247
 
247
    printf("\nCopying kernel...");
248
    printf("\nCopying kernel...");
248
    (void) ofw_claim_phys(bootinfo.physmem_start + base,
249
    (void) ofw_claim_phys(bootinfo.physmem_start + base,
249
        ALIGN_UP(components[0].size, PAGE_SIZE));
250
        ALIGN_UP(components[0].size, PAGE_SIZE));
250
    memcpy(base, components[0].start, components[0].size);
251
    memcpy(base, components[0].start, components[0].size);
251
    printf("done.\n");
252
    printf("done.\n");
252
 
253
 
253
    /*
254
    /*
254
     * Claim and map the physical memory for the boot allocator.
255
     * Claim and map the physical memory for the boot allocator.
255
     * Initialize the boot allocator.
256
     * Initialize the boot allocator.
256
     */
257
     */
257
    balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
258
    balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
258
    (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
259
    (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
259
        BALLOC_MAX_SIZE);
260
        BALLOC_MAX_SIZE);
260
    (void) ofw_map(balloc_base, balloc_base, BALLOC_MAX_SIZE, -1);
261
    (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
-
 
262
        BALLOC_MAX_SIZE, -1);
261
    balloc_init(&bootinfo.ballocs, (uintptr_t)balloc_base);
263
    balloc_init(&bootinfo.ballocs, (uintptr_t)balloc_base);
262
 
264
 
263
    printf("\nCanonizing OpenFirmware device tree...");
265
    printf("\nCanonizing OpenFirmware device tree...");
264
    bootinfo.ofw_root = ofw_tree_build();
266
    bootinfo.ofw_root = ofw_tree_build();
265
    printf("done.\n");
267
    printf("done.\n");
266
 
268
 
267
#ifdef CONFIG_SMP
269
#ifdef CONFIG_AP
268
    printf("\nChecking for secondary processors...");
270
    printf("\nChecking for secondary processors...");
269
    if (!ofw_cpu())
271
    if (!ofw_cpu())
270
        printf("Error: unable to get CPU properties\n");
272
        printf("Error: unable to get CPU properties\n");
271
    printf("done.\n");
273
    printf("done.\n");
272
#endif
274
#endif
273
 
275
 
274
    setup_palette();
276
    setup_palette();
275
 
277
 
276
    printf("\nBooting the kernel...\n");
278
    printf("\nBooting the kernel...\n");
277
    jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
279
    jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
278
        bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
280
        bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
279
        sizeof(bootinfo));
281
        sizeof(bootinfo));
280
}
282
}
281
 
-
 
282
 
283