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