1 // SPDX-License-Identifier: GPL-2.0+
3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4 * - Added prep subcommand support
5 * - Reorganized source - modeled after powerpc version
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Marius Groeger <mgroeger@sysgo.de>
11 * Copyright (C) 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
15 #include <bootstage.h>
23 #include <u-boot/zlib.h>
24 #include <asm/byteorder.h>
25 #include <linux/libfdt.h>
27 #include <fdt_support.h>
28 #include <asm/bootm.h>
29 #include <asm/secure.h>
30 #include <linux/compiler.h>
33 #include <asm/cache.h>
35 #ifdef CONFIG_ARMV7_NONSEC
36 #include <asm/armv7.h>
38 #include <asm/setup.h>
40 DECLARE_GLOBAL_DATA_PTR;
42 static struct tag *params;
44 static ulong get_sp(void)
48 asm("mov %0, sp" : "=r"(ret) : );
52 void arch_lmb_reserve(struct lmb *lmb)
58 * Booting a (Linux) kernel image
60 * Allocate space for command line and board info - the
61 * address should be as high as possible within the reach of
62 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
63 * memory, which means far enough below the current stack
67 debug("## Current stack ends at 0x%08lx ", sp);
69 /* adjust sp by 4K to be safe */
71 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
72 if (!gd->bd->bi_dram[bank].size ||
73 sp < gd->bd->bi_dram[bank].start)
75 /* Watch out for RAM at end of address space! */
76 bank_end = gd->bd->bi_dram[bank].start +
77 gd->bd->bi_dram[bank].size - 1;
80 if (bank_end > gd->ram_top)
81 bank_end = gd->ram_top - 1;
83 lmb_reserve(lmb, sp, bank_end - sp + 1);
88 __weak void board_quiesce_devices(void)
93 * announce_and_cleanup() - Print message and prepare for kernel boot
95 * @fake: non-zero to do everything except actually boot
97 static void announce_and_cleanup(int fake)
99 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
100 #ifdef CONFIG_BOOTSTAGE_FDT
101 bootstage_fdt_add_report();
103 #ifdef CONFIG_BOOTSTAGE_REPORT
107 #ifdef CONFIG_USB_DEVICE
111 board_quiesce_devices();
113 printf("\nStarting kernel ...%s\n\n", fake ?
114 "(fake run for tracing)" : "");
116 * Call remove function of all devices with a removal flag set.
117 * This may be useful for last-stage operations, like cancelling
118 * of DMA operation or releasing device internal buffers.
120 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
122 cleanup_before_linux();
125 static void setup_start_tag (bd_t *bd)
127 params = (struct tag *)bd->bi_boot_params;
129 params->hdr.tag = ATAG_CORE;
130 params->hdr.size = tag_size (tag_core);
132 params->u.core.flags = 0;
133 params->u.core.pagesize = 0;
134 params->u.core.rootdev = 0;
136 params = tag_next (params);
139 static void setup_memory_tags(bd_t *bd)
143 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
144 params->hdr.tag = ATAG_MEM;
145 params->hdr.size = tag_size (tag_mem32);
147 params->u.mem.start = bd->bi_dram[i].start;
148 params->u.mem.size = bd->bi_dram[i].size;
150 params = tag_next (params);
154 static void setup_commandline_tag(bd_t *bd, char *commandline)
161 /* eat leading white space */
162 for (p = commandline; *p == ' '; p++);
164 /* skip non-existent command lines so the kernel will still
165 * use its default command line.
170 params->hdr.tag = ATAG_CMDLINE;
172 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
174 strcpy (params->u.cmdline.cmdline, p);
176 params = tag_next (params);
179 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
181 /* an ATAG_INITRD node tells the kernel where the compressed
182 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
184 params->hdr.tag = ATAG_INITRD2;
185 params->hdr.size = tag_size (tag_initrd);
187 params->u.initrd.start = initrd_start;
188 params->u.initrd.size = initrd_end - initrd_start;
190 params = tag_next (params);
193 static void setup_serial_tag(struct tag **tmp)
195 struct tag *params = *tmp;
196 struct tag_serialnr serialnr;
198 get_board_serial(&serialnr);
199 params->hdr.tag = ATAG_SERIAL;
200 params->hdr.size = tag_size (tag_serialnr);
201 params->u.serialnr.low = serialnr.low;
202 params->u.serialnr.high= serialnr.high;
203 params = tag_next (params);
207 static void setup_revision_tag(struct tag **in_params)
211 rev = get_board_rev();
212 params->hdr.tag = ATAG_REVISION;
213 params->hdr.size = tag_size (tag_revision);
214 params->u.revision.rev = rev;
215 params = tag_next (params);
218 static void setup_end_tag(bd_t *bd)
220 params->hdr.tag = ATAG_NONE;
221 params->hdr.size = 0;
224 __weak void setup_board_tags(struct tag **in_params) {}
227 static void do_nonsec_virt_switch(void)
230 dcache_disable(); /* flush cache before swtiching to EL2 */
234 __weak void board_prep_linux(bootm_headers_t *images) { }
236 /* Subcommand: PREP */
237 static void boot_prep_linux(bootm_headers_t *images)
239 char *commandline = env_get("bootargs");
241 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
242 #ifdef CONFIG_OF_LIBFDT
243 debug("using: FDT\n");
244 if (image_setup_linux(images)) {
245 printf("FDT creation failed! hanging...");
249 } else if (BOOTM_ENABLE_TAGS) {
250 debug("using: ATAGS\n");
251 setup_start_tag(gd->bd);
252 if (BOOTM_ENABLE_SERIAL_TAG)
253 setup_serial_tag(¶ms);
254 if (BOOTM_ENABLE_CMDLINE_TAG)
255 setup_commandline_tag(gd->bd, commandline);
256 if (BOOTM_ENABLE_REVISION_TAG)
257 setup_revision_tag(¶ms);
258 if (BOOTM_ENABLE_MEMORY_TAGS)
259 setup_memory_tags(gd->bd);
260 if (BOOTM_ENABLE_INITRD_TAG) {
262 * In boot_ramdisk_high(), it may relocate ramdisk to
263 * a specified location. And set images->initrd_start &
264 * images->initrd_end to relocated ramdisk's start/end
265 * addresses. So use them instead of images->rd_start &
266 * images->rd_end when possible.
268 if (images->initrd_start && images->initrd_end) {
269 setup_initrd_tag(gd->bd, images->initrd_start,
271 } else if (images->rd_start && images->rd_end) {
272 setup_initrd_tag(gd->bd, images->rd_start,
276 setup_board_tags(¶ms);
277 setup_end_tag(gd->bd);
279 printf("FDT and ATAGS support not compiled in - hanging\n");
283 board_prep_linux(images);
286 __weak bool armv7_boot_nonsec_default(void)
288 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
295 #ifdef CONFIG_ARMV7_NONSEC
296 bool armv7_boot_nonsec(void)
298 char *s = env_get("bootm_boot_mode");
299 bool nonsec = armv7_boot_nonsec_default();
301 if (s && !strcmp(s, "sec"))
304 if (s && !strcmp(s, "nonsec"))
312 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
316 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
317 static void switch_to_el1(void)
319 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
320 (images.os.arch == IH_ARCH_ARM))
321 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
322 (u64)images.ft_addr, 0,
326 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
334 static void boot_jump_linux(bootm_headers_t *images, int flag)
337 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
339 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
341 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
342 void *res2))images->ep;
344 debug("## Transferring control to Linux (at address %lx)...\n",
345 (ulong) kernel_entry);
346 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
348 announce_and_cleanup(fake);
351 #ifdef CONFIG_ARMV8_PSCI
354 do_nonsec_virt_switch();
356 update_os_arch_secondary_cores(images->os.arch);
358 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
359 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
360 (u64)switch_to_el1, ES_TO_AARCH64);
362 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
363 (images->os.arch == IH_ARCH_ARM))
364 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
365 (u64)images->ft_addr, 0,
369 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
375 unsigned long machid = gd->bd->bi_arch_number;
377 void (*kernel_entry)(int zero, int arch, uint params);
379 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
381 kernel_entry = (void (*)(int, int, uint))images->ep;
382 #ifdef CONFIG_CPU_V7M
383 ulong addr = (ulong)kernel_entry | 1;
384 kernel_entry = (void *)addr;
386 s = env_get("machid");
388 if (strict_strtoul(s, 16, &machid) < 0) {
389 debug("strict_strtoul failed!\n");
392 printf("Using machid 0x%lx from environment\n", machid);
395 debug("## Transferring control to Linux (at address %08lx)" \
396 "...\n", (ulong) kernel_entry);
397 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
398 announce_and_cleanup(fake);
400 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
401 r2 = (unsigned long)images->ft_addr;
403 r2 = gd->bd->bi_boot_params;
406 #ifdef CONFIG_ARMV7_NONSEC
407 if (armv7_boot_nonsec()) {
409 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
413 kernel_entry(0, machid, r2);
418 /* Main Entry point for arm bootm implementation
420 * Modeled after the powerpc implementation
421 * DIFFERENCE: Instead of calling prep and go at the end
422 * they are called if subcommand is equal 0.
424 int do_bootm_linux(int flag, int argc, char * const argv[],
425 bootm_headers_t *images)
427 /* No need for those on ARM */
428 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
431 if (flag & BOOTM_STATE_OS_PREP) {
432 boot_prep_linux(images);
436 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
437 boot_jump_linux(images, flag);
441 boot_prep_linux(images);
442 boot_jump_linux(images, flag);
446 #if defined(CONFIG_BOOTM_VXWORKS)
447 void boot_prep_vxworks(bootm_headers_t *images)
449 #if defined(CONFIG_OF_LIBFDT)
452 if (images->ft_addr) {
453 off = fdt_path_offset(images->ft_addr, "/memory");
455 if (arch_fixup_fdt(images->ft_addr))
456 puts("## WARNING: fixup memory failed!\n");
460 cleanup_before_linux();
462 void boot_jump_vxworks(bootm_headers_t *images)
464 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
469 /* ARM VxWorks requires device tree physical address to be passed */
470 ((void (*)(void *))images->ep)(images->ft_addr);