[4872160] | 1 | /*
|
---|
| 2 | * Copyright (c) 2005 Martin Decky
|
---|
| 3 | * Copyright (c) 2006 Jakub Jermar
|
---|
| 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 <arch/main.h>
|
---|
| 31 | #include <arch/arch.h>
|
---|
| 32 | #include <arch/asm.h>
|
---|
| 33 | #include <arch/ofw.h>
|
---|
| 34 | #include <arch/_components.h>
|
---|
| 35 | #include <genarch/ofw.h>
|
---|
| 36 | #include <genarch/ofw_tree.h>
|
---|
| 37 | #include <halt.h>
|
---|
| 38 | #include <printf.h>
|
---|
| 39 | #include <memstr.h>
|
---|
| 40 | #include <version.h>
|
---|
| 41 | #include <macros.h>
|
---|
| 42 | #include <align.h>
|
---|
| 43 | #include <str.h>
|
---|
| 44 | #include <errno.h>
|
---|
| 45 | #include <inflate.h>
|
---|
| 46 |
|
---|
| 47 | /* The lowest ID (read from the VER register) of some US3 CPU model */
|
---|
| 48 | #define FIRST_US3_CPU 0x14
|
---|
| 49 |
|
---|
| 50 | /* The greatest ID (read from the VER register) of some US3 CPU model */
|
---|
| 51 | #define LAST_US3_CPU 0x19
|
---|
| 52 |
|
---|
| 53 | /* UltraSPARC IIIi processor implementation code */
|
---|
| 54 | #define US_IIIi_CODE 0x15
|
---|
| 55 |
|
---|
| 56 | #define OBP_BIAS 0x400000
|
---|
| 57 |
|
---|
| 58 | #define BALLOC_MAX_SIZE 131072
|
---|
| 59 |
|
---|
| 60 | #define TOP2ADDR(top) (((void *) KERNEL_ADDRESS) + (top))
|
---|
| 61 |
|
---|
| 62 | /** UltraSPARC architecture */
|
---|
| 63 | static uint8_t arch;
|
---|
| 64 |
|
---|
| 65 | /** UltraSPARC subarchitecture */
|
---|
| 66 | static uint8_t subarch;
|
---|
| 67 |
|
---|
| 68 | /** Mask of the MID field inside the ICBUS_CONFIG register
|
---|
| 69 | *
|
---|
| 70 | * Shifted by MID_SHIFT bits to the right
|
---|
| 71 | *
|
---|
| 72 | */
|
---|
| 73 | static uint16_t mid_mask;
|
---|
| 74 |
|
---|
| 75 | static bootinfo_t bootinfo;
|
---|
| 76 |
|
---|
| 77 | /** Detect the UltraSPARC architecture
|
---|
| 78 | *
|
---|
| 79 | * Detection is done by inspecting the property called "compatible"
|
---|
| 80 | * in the OBP root node. Currently sun4u and sun4v are supported.
|
---|
| 81 | * Set global variable "arch" to the correct value.
|
---|
| 82 | *
|
---|
| 83 | */
|
---|
| 84 | static void arch_detect(void)
|
---|
| 85 | {
|
---|
| 86 | phandle root = ofw_find_device("/");
|
---|
| 87 | char compatible[OFW_TREE_PROPERTY_MAX_VALUELEN];
|
---|
| 88 |
|
---|
| 89 | if (ofw_get_property(root, "compatible", compatible,
|
---|
| 90 | OFW_TREE_PROPERTY_MAX_VALUELEN) <= 0) {
|
---|
| 91 | printf("Warning: Unable to determine architecture, assuming sun4u.\n");
|
---|
| 92 | arch = ARCH_SUN4U;
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if (str_cmp(compatible, "sun4v") != 0) {
|
---|
| 97 | /*
|
---|
| 98 | * As not all sun4u machines have "sun4u" in their "compatible"
|
---|
| 99 | * OBP property (e.g. Serengeti's OBP "compatible" property is
|
---|
| 100 | * "SUNW,Serengeti"), we will by default fallback to sun4u if
|
---|
| 101 | * an unknown value of the "compatible" property is encountered.
|
---|
| 102 | */
|
---|
[9644c69] | 103 | if (str_cmp(compatible, "sun4u") != 0)
|
---|
| 104 | printf("Warning: Unknown architecture, assuming sun4u.\n");
|
---|
[4872160] | 105 | arch = ARCH_SUN4U;
|
---|
| 106 | } else
|
---|
| 107 | arch = ARCH_SUN4V;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | /** Detect the subarchitecture (US, US3) of sun4u
|
---|
| 112 | *
|
---|
| 113 | * Set the global variables "subarch" and "mid_mask" to
|
---|
| 114 | * correct values.
|
---|
| 115 | *
|
---|
| 116 | */
|
---|
| 117 | static void sun4u_subarch_detect(void)
|
---|
| 118 | {
|
---|
| 119 | uint64_t ver;
|
---|
| 120 | asm volatile (
|
---|
| 121 | "rdpr %%ver, %[ver]\n"
|
---|
| 122 | : [ver] "=r" (ver)
|
---|
| 123 | );
|
---|
| 124 |
|
---|
| 125 | ver = (ver << 16) >> 48;
|
---|
| 126 |
|
---|
| 127 | if ((ver >= FIRST_US3_CPU) && (ver <= LAST_US3_CPU)) {
|
---|
| 128 | subarch = SUBARCH_US3;
|
---|
| 129 |
|
---|
| 130 | if (ver == US_IIIi_CODE)
|
---|
| 131 | mid_mask = (1 << 5) - 1;
|
---|
| 132 | else
|
---|
| 133 | mid_mask = (1 << 10) - 1;
|
---|
| 134 |
|
---|
| 135 | } else if (ver < FIRST_US3_CPU) {
|
---|
| 136 | subarch = SUBARCH_US;
|
---|
| 137 | mid_mask = (1 << 5) - 1;
|
---|
| 138 | } else {
|
---|
| 139 | printf("Warning: This CPU is not supported.");
|
---|
| 140 | subarch = SUBARCH_UNKNOWN;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Perform sun4u-specific SMP initialization.
|
---|
| 145 | *
|
---|
| 146 | */
|
---|
| 147 | static void sun4u_smp(void)
|
---|
| 148 | {
|
---|
| 149 | #ifdef CONFIG_AP
|
---|
| 150 | printf("Checking for secondary processors ...\n");
|
---|
| 151 | ofw_cpu(mid_mask, bootinfo.physmem_start);
|
---|
| 152 | #endif
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /** Perform sun4v-specific fixups.
|
---|
| 156 | *
|
---|
| 157 | */
|
---|
| 158 | static void sun4v_fixups(void)
|
---|
| 159 | {
|
---|
| 160 | /*
|
---|
| 161 | * When SILO booted, the OBP had established a virtual to physical
|
---|
| 162 | * memory mapping. This mapping is not an identity since the
|
---|
| 163 | * physical memory starts at non-zero address.
|
---|
| 164 | *
|
---|
| 165 | * However, the mapping even does not map virtual address 0
|
---|
| 166 | * onto the starting address of the physical memory, but onto an
|
---|
| 167 | * address which is 0x400000 (OBP_BIAS) bytes higher.
|
---|
| 168 | *
|
---|
| 169 | * The reason is that the OBP had already used the memory just
|
---|
| 170 | * at the beginning of the physical memory. Thus that memory cannot
|
---|
| 171 | * be used by SILO (nor the bootloader).
|
---|
| 172 | *
|
---|
| 173 | * So far, we solve it by a nasty workaround:
|
---|
| 174 | *
|
---|
| 175 | * We pretend that the physical memory starts 0x400000 (OBP_BIAS)
|
---|
| 176 | * bytes further than it actually does (and hence pretend that the
|
---|
| 177 | * physical memory is 0x400000 bytes smaller). Of course, the value
|
---|
| 178 | * 0x400000 will most probably depend on the machine and OBP version
|
---|
| 179 | * (the workaround works on Simics).
|
---|
| 180 | *
|
---|
| 181 | * A proper solution would be to inspect the "available" property
|
---|
| 182 | * of the "/memory" node to find out which parts of memory
|
---|
| 183 | * are used by OBP and redesign the algorithm of copying
|
---|
| 184 | * kernel/init tasks/ramdisk from the bootable image to memory
|
---|
| 185 | * (which we must do anyway because of issues with claiming the memory
|
---|
| 186 | * on Serengeti).
|
---|
| 187 | *
|
---|
| 188 | */
|
---|
| 189 | bootinfo.physmem_start += OBP_BIAS;
|
---|
| 190 | bootinfo.memmap.zones[0].start += OBP_BIAS;
|
---|
| 191 | bootinfo.memmap.zones[0].size -= OBP_BIAS;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void bootstrap(void)
|
---|
| 195 | {
|
---|
| 196 | version_print();
|
---|
| 197 |
|
---|
| 198 | arch_detect();
|
---|
| 199 | if (arch == ARCH_SUN4U)
|
---|
| 200 | sun4u_subarch_detect();
|
---|
| 201 | else
|
---|
| 202 | subarch = SUBARCH_UNKNOWN;
|
---|
| 203 |
|
---|
| 204 | bootinfo.physmem_start = ofw_get_physmem_start();
|
---|
| 205 | ofw_memmap(&bootinfo.memmap);
|
---|
| 206 |
|
---|
| 207 | void *bootinfo_pa = ofw_translate(&bootinfo);
|
---|
[b97b348] | 208 | void *kernel_address_pa = ofw_translate((void *) KERNEL_ADDRESS);
|
---|
[4872160] | 209 | void *loader_address_pa = ofw_translate((void *) LOADER_ADDRESS);
|
---|
| 210 |
|
---|
[7e752b2] | 211 | printf("\nMemory statistics (total %" PRIu64 " MB, starting at %p)\n",
|
---|
| 212 | bootinfo.memmap.total >> 20, (void *) bootinfo.physmem_start);
|
---|
| 213 | printf(" %p|%p: boot info structure\n", &bootinfo, (void *) bootinfo_pa);
|
---|
| 214 | printf(" %p|%p: kernel entry point\n",
|
---|
| 215 | (void *) KERNEL_ADDRESS, (void *) kernel_address_pa);
|
---|
| 216 | printf(" %p|%p: loader entry point\n",
|
---|
| 217 | (void *) LOADER_ADDRESS, (void *) loader_address_pa);
|
---|
[4872160] | 218 |
|
---|
| 219 | size_t i;
|
---|
| 220 | for (i = 0; i < COMPONENTS; i++)
|
---|
[7e752b2] | 221 | printf(" %p|%p: %s image (%zu/%zu bytes)\n", components[i].start,
|
---|
[4872160] | 222 | ofw_translate(components[i].start), components[i].name,
|
---|
| 223 | components[i].inflated, components[i].size);
|
---|
| 224 |
|
---|
| 225 | void *dest[COMPONENTS];
|
---|
| 226 | size_t top = KERNEL_ADDRESS;
|
---|
| 227 | size_t cnt = 0;
|
---|
| 228 | bootinfo.taskmap.cnt = 0;
|
---|
| 229 | for (i = 0; i < min(COMPONENTS, TASKMAP_MAX_RECORDS); i++) {
|
---|
| 230 | top = ALIGN_UP(top, PAGE_SIZE);
|
---|
| 231 |
|
---|
| 232 | if (i > 0) {
|
---|
| 233 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].addr =
|
---|
| 234 | (void *) top;
|
---|
| 235 | bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].size =
|
---|
| 236 | components[i].inflated;
|
---|
| 237 |
|
---|
| 238 | str_cpy(bootinfo.taskmap.tasks[bootinfo.taskmap.cnt].name,
|
---|
| 239 | BOOTINFO_TASK_NAME_BUFLEN, components[i].name);
|
---|
| 240 |
|
---|
| 241 | bootinfo.taskmap.cnt++;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | dest[i] = (void *) top;
|
---|
| 245 | top += components[i].inflated;
|
---|
| 246 | cnt++;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | printf("\nInflating components ... ");
|
---|
| 250 |
|
---|
| 251 | for (i = cnt; i > 0; i--) {
|
---|
| 252 | printf("%s ", components[i - 1].name);
|
---|
| 253 |
|
---|
| 254 | /*
|
---|
| 255 | * At this point, we claim the physical memory that we are
|
---|
| 256 | * going to use. We should be safe in case of the virtual
|
---|
| 257 | * address space because the OpenFirmware, according to its
|
---|
| 258 | * SPARC binding, should restrict its use of virtual memory
|
---|
| 259 | * to addresses from [0xffd00000; 0xffefffff] and
|
---|
| 260 | * [0xfe000000; 0xfeffffff].
|
---|
| 261 | *
|
---|
| 262 | * We don't map this piece of memory. We simply rely on
|
---|
| 263 | * SILO to have it done for us already in this case.
|
---|
| 264 | *
|
---|
| 265 | * XXX SILO only maps 8 MB for us here. We should improve
|
---|
| 266 | * this code to be totally independent on the behavior
|
---|
| 267 | * of SILO.
|
---|
| 268 | *
|
---|
| 269 | */
|
---|
| 270 | ofw_claim_phys(bootinfo.physmem_start + dest[i - 1],
|
---|
| 271 | ALIGN_UP(components[i - 1].inflated, PAGE_SIZE));
|
---|
| 272 |
|
---|
| 273 | int err = inflate(components[i - 1].start, components[i - 1].size,
|
---|
| 274 | dest[i - 1], components[i - 1].inflated);
|
---|
| 275 |
|
---|
| 276 | if (err != EOK) {
|
---|
| 277 | printf("\n%s: Inflating error %d, halting.\n",
|
---|
| 278 | components[i - 1].name, err);
|
---|
| 279 | halt();
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | printf(".\n");
|
---|
| 284 |
|
---|
| 285 | /*
|
---|
| 286 | * Claim and map the physical memory for the boot allocator.
|
---|
| 287 | * Initialize the boot allocator.
|
---|
| 288 | */
|
---|
| 289 | printf("Setting up boot allocator ...\n");
|
---|
| 290 | void *balloc_base = (void *) ALIGN_UP(top, PAGE_SIZE);
|
---|
| 291 | ofw_claim_phys(bootinfo.physmem_start + balloc_base, BALLOC_MAX_SIZE);
|
---|
| 292 | ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
|
---|
| 293 | BALLOC_MAX_SIZE, -1);
|
---|
| 294 | balloc_init(&bootinfo.ballocs, balloc_base, (uintptr_t) balloc_base,
|
---|
| 295 | BALLOC_MAX_SIZE);
|
---|
| 296 |
|
---|
| 297 | printf("Setting up screens ...\n");
|
---|
| 298 | ofw_setup_screens();
|
---|
| 299 |
|
---|
| 300 | printf("Canonizing OpenFirmware device tree ...\n");
|
---|
| 301 | bootinfo.ofw_root = ofw_tree_build();
|
---|
| 302 |
|
---|
| 303 | if (arch == ARCH_SUN4U)
|
---|
| 304 | sun4u_smp();
|
---|
| 305 |
|
---|
| 306 | if (arch == ARCH_SUN4V)
|
---|
| 307 | sun4v_fixups();
|
---|
| 308 |
|
---|
| 309 | printf("Booting the kernel ...\n");
|
---|
[b97b348] | 310 | jump_to_kernel(bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo, subarch,
|
---|
| 311 | (void *) KERNEL_ADDRESS);
|
---|
[4872160] | 312 | }
|
---|