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>
25 #include <u-boot/zlib.h>
26 #include <asm/byteorder.h>
27 #include <linux/libfdt.h>
29 #include <fdt_support.h>
30 #include <asm/bootm.h>
31 #include <asm/secure.h>
32 #include <linux/compiler.h>
35 #include <asm/cache.h>
37 #ifdef CONFIG_ARMV7_NONSEC
38 #include <asm/armv7.h>
40 #include <asm/setup.h>
42 DECLARE_GLOBAL_DATA_PTR;
44 static struct tag *params;
46 static ulong get_sp(void)
50 asm("mov %0, sp" : "=r"(ret) : );
54 void arch_lmb_reserve(struct lmb *lmb)
60 * Booting a (Linux) kernel image
62 * Allocate space for command line and board info - the
63 * address should be as high as possible within the reach of
64 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
65 * memory, which means far enough below the current stack
69 debug("## Current stack ends at 0x%08lx ", sp);
71 /* adjust sp by 4K to be safe */
73 for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
74 if (!gd->bd->bi_dram[bank].size ||
75 sp < gd->bd->bi_dram[bank].start)
77 /* Watch out for RAM at end of address space! */
78 bank_end = gd->bd->bi_dram[bank].start +
79 gd->bd->bi_dram[bank].size - 1;
82 if (bank_end > gd->ram_top)
83 bank_end = gd->ram_top - 1;
85 lmb_reserve(lmb, sp, bank_end - sp + 1);
90 __weak void board_quiesce_devices(void)
95 * announce_and_cleanup() - Print message and prepare for kernel boot
97 * @fake: non-zero to do everything except actually boot
99 static void announce_and_cleanup(int fake)
101 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
102 #ifdef CONFIG_BOOTSTAGE_FDT
103 bootstage_fdt_add_report();
105 #ifdef CONFIG_BOOTSTAGE_REPORT
109 #ifdef CONFIG_USB_DEVICE
113 board_quiesce_devices();
115 printf("\nStarting kernel ...%s\n\n", fake ?
116 "(fake run for tracing)" : "");
118 * Call remove function of all devices with a removal flag set.
119 * This may be useful for last-stage operations, like cancelling
120 * of DMA operation or releasing device internal buffers.
122 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
124 cleanup_before_linux();
127 static void setup_start_tag (bd_t *bd)
129 params = (struct tag *)bd->bi_boot_params;
131 params->hdr.tag = ATAG_CORE;
132 params->hdr.size = tag_size (tag_core);
134 params->u.core.flags = 0;
135 params->u.core.pagesize = 0;
136 params->u.core.rootdev = 0;
138 params = tag_next (params);
141 static void setup_memory_tags(bd_t *bd)
145 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
146 params->hdr.tag = ATAG_MEM;
147 params->hdr.size = tag_size (tag_mem32);
149 params->u.mem.start = bd->bi_dram[i].start;
150 params->u.mem.size = bd->bi_dram[i].size;
152 params = tag_next (params);
156 static void setup_commandline_tag(bd_t *bd, char *commandline)
163 /* eat leading white space */
164 for (p = commandline; *p == ' '; p++);
166 /* skip non-existent command lines so the kernel will still
167 * use its default command line.
172 params->hdr.tag = ATAG_CMDLINE;
174 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
176 strcpy (params->u.cmdline.cmdline, p);
178 params = tag_next (params);
181 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
183 /* an ATAG_INITRD node tells the kernel where the compressed
184 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
186 params->hdr.tag = ATAG_INITRD2;
187 params->hdr.size = tag_size (tag_initrd);
189 params->u.initrd.start = initrd_start;
190 params->u.initrd.size = initrd_end - initrd_start;
192 params = tag_next (params);
195 static void setup_serial_tag(struct tag **tmp)
197 struct tag *params = *tmp;
198 struct tag_serialnr serialnr;
200 get_board_serial(&serialnr);
201 params->hdr.tag = ATAG_SERIAL;
202 params->hdr.size = tag_size (tag_serialnr);
203 params->u.serialnr.low = serialnr.low;
204 params->u.serialnr.high= serialnr.high;
205 params = tag_next (params);
209 static void setup_revision_tag(struct tag **in_params)
213 rev = get_board_rev();
214 params->hdr.tag = ATAG_REVISION;
215 params->hdr.size = tag_size (tag_revision);
216 params->u.revision.rev = rev;
217 params = tag_next (params);
220 static void setup_end_tag(bd_t *bd)
222 params->hdr.tag = ATAG_NONE;
223 params->hdr.size = 0;
226 __weak void setup_board_tags(struct tag **in_params) {}
229 static void do_nonsec_virt_switch(void)
232 dcache_disable(); /* flush cache before swtiching to EL2 */
236 __weak void board_prep_linux(bootm_headers_t *images) { }
238 /* Subcommand: PREP */
239 static void boot_prep_linux(bootm_headers_t *images)
241 char *commandline = env_get("bootargs");
243 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
244 #ifdef CONFIG_OF_LIBFDT
245 debug("using: FDT\n");
246 if (image_setup_linux(images)) {
247 printf("FDT creation failed! hanging...");
251 } else if (BOOTM_ENABLE_TAGS) {
252 debug("using: ATAGS\n");
253 setup_start_tag(gd->bd);
254 if (BOOTM_ENABLE_SERIAL_TAG)
255 setup_serial_tag(¶ms);
256 if (BOOTM_ENABLE_CMDLINE_TAG)
257 setup_commandline_tag(gd->bd, commandline);
258 if (BOOTM_ENABLE_REVISION_TAG)
259 setup_revision_tag(¶ms);
260 if (BOOTM_ENABLE_MEMORY_TAGS)
261 setup_memory_tags(gd->bd);
262 if (BOOTM_ENABLE_INITRD_TAG) {
264 * In boot_ramdisk_high(), it may relocate ramdisk to
265 * a specified location. And set images->initrd_start &
266 * images->initrd_end to relocated ramdisk's start/end
267 * addresses. So use them instead of images->rd_start &
268 * images->rd_end when possible.
270 if (images->initrd_start && images->initrd_end) {
271 setup_initrd_tag(gd->bd, images->initrd_start,
273 } else if (images->rd_start && images->rd_end) {
274 setup_initrd_tag(gd->bd, images->rd_start,
278 setup_board_tags(¶ms);
279 setup_end_tag(gd->bd);
281 printf("FDT and ATAGS support not compiled in - hanging\n");
285 board_prep_linux(images);
288 __weak bool armv7_boot_nonsec_default(void)
290 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
297 #ifdef CONFIG_ARMV7_NONSEC
298 bool armv7_boot_nonsec(void)
300 char *s = env_get("bootm_boot_mode");
301 bool nonsec = armv7_boot_nonsec_default();
303 if (s && !strcmp(s, "sec"))
306 if (s && !strcmp(s, "nonsec"))
314 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
318 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
319 static void switch_to_el1(void)
321 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
322 (images.os.arch == IH_ARCH_ARM))
323 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
324 (u64)images.ft_addr, 0,
328 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
336 static void boot_jump_linux(bootm_headers_t *images, int flag)
339 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
341 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
343 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
344 void *res2))images->ep;
346 debug("## Transferring control to Linux (at address %lx)...\n",
347 (ulong) kernel_entry);
348 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
350 announce_and_cleanup(fake);
353 #ifdef CONFIG_ARMV8_PSCI
356 do_nonsec_virt_switch();
358 update_os_arch_secondary_cores(images->os.arch);
360 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
361 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
362 (u64)switch_to_el1, ES_TO_AARCH64);
364 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
365 (images->os.arch == IH_ARCH_ARM))
366 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
367 (u64)images->ft_addr, 0,
371 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
377 unsigned long machid = gd->bd->bi_arch_number;
379 void (*kernel_entry)(int zero, int arch, uint params);
381 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
383 kernel_entry = (void (*)(int, int, uint))images->ep;
384 #ifdef CONFIG_CPU_V7M
385 ulong addr = (ulong)kernel_entry | 1;
386 kernel_entry = (void *)addr;
388 s = env_get("machid");
390 if (strict_strtoul(s, 16, &machid) < 0) {
391 debug("strict_strtoul failed!\n");
394 printf("Using machid 0x%lx from environment\n", machid);
397 debug("## Transferring control to Linux (at address %08lx)" \
398 "...\n", (ulong) kernel_entry);
399 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
400 announce_and_cleanup(fake);
402 if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
403 r2 = (unsigned long)images->ft_addr;
405 r2 = gd->bd->bi_boot_params;
408 #ifdef CONFIG_ARMV7_NONSEC
409 if (armv7_boot_nonsec()) {
411 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
415 kernel_entry(0, machid, r2);
420 /* Main Entry point for arm bootm implementation
422 * Modeled after the powerpc implementation
423 * DIFFERENCE: Instead of calling prep and go at the end
424 * they are called if subcommand is equal 0.
426 int do_bootm_linux(int flag, int argc, char *const argv[],
427 bootm_headers_t *images)
429 /* No need for those on ARM */
430 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
433 if (flag & BOOTM_STATE_OS_PREP) {
434 boot_prep_linux(images);
438 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
439 boot_jump_linux(images, flag);
443 boot_prep_linux(images);
444 boot_jump_linux(images, flag);
448 #if defined(CONFIG_BOOTM_VXWORKS)
449 void boot_prep_vxworks(bootm_headers_t *images)
451 #if defined(CONFIG_OF_LIBFDT)
454 if (images->ft_addr) {
455 off = fdt_path_offset(images->ft_addr, "/memory");
457 if (arch_fixup_fdt(images->ft_addr))
458 puts("## WARNING: fixup memory failed!\n");
462 cleanup_before_linux();
464 void boot_jump_vxworks(bootm_headers_t *images)
466 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
471 /* ARM VxWorks requires device tree physical address to be passed */
472 ((void (*)(void *))images->ep)(images->ft_addr);