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