Subversion Repositories HelenOS

Rev

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

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