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>
20 #include <asm/global_data.h>
24 #include <u-boot/zlib.h>
25 #include <asm/byteorder.h>
26 #include <linux/libfdt.h>
28 #include <fdt_support.h>
29 #include <asm/bootm.h>
30 #include <asm/secure.h>
31 #include <linux/compiler.h>
34 #include <asm/cache.h>
36 #ifdef CONFIG_ARMV7_NONSEC
37 #include <asm/armv7.h>
39 #include <asm/setup.h>
41 DECLARE_GLOBAL_DATA_PTR;
43 static struct tag *params;
45 __weak void board_quiesce_devices(void)
50 * announce_and_cleanup() - Print message and prepare for kernel boot
52 * @fake: non-zero to do everything except actually boot
54 static void announce_and_cleanup(int fake)
56 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
57 #ifdef CONFIG_BOOTSTAGE_FDT
58 bootstage_fdt_add_report();
60 #ifdef CONFIG_BOOTSTAGE_REPORT
64 #ifdef CONFIG_USB_DEVICE
68 board_quiesce_devices();
70 printf("\nStarting kernel ...%s\n\n", fake ?
71 "(fake run for tracing)" : "");
73 * Call remove function of all devices with a removal flag set.
74 * This may be useful for last-stage operations, like cancelling
75 * of DMA operation or releasing device internal buffers.
77 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
79 /* Remove all active vital devices next */
80 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
82 cleanup_before_linux();
85 static void setup_start_tag (struct bd_info *bd)
87 params = (struct tag *)bd->bi_boot_params;
89 params->hdr.tag = ATAG_CORE;
90 params->hdr.size = tag_size (tag_core);
92 params->u.core.flags = 0;
93 params->u.core.pagesize = 0;
94 params->u.core.rootdev = 0;
96 params = tag_next (params);
99 static void setup_memory_tags(struct bd_info *bd)
103 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
104 params->hdr.tag = ATAG_MEM;
105 params->hdr.size = tag_size (tag_mem32);
107 params->u.mem.start = bd->bi_dram[i].start;
108 params->u.mem.size = bd->bi_dram[i].size;
110 params = tag_next (params);
114 static void setup_commandline_tag(struct bd_info *bd, char *commandline)
121 /* eat leading white space */
122 for (p = commandline; *p == ' '; p++);
124 /* skip non-existent command lines so the kernel will still
125 * use its default command line.
130 params->hdr.tag = ATAG_CMDLINE;
132 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
134 strcpy (params->u.cmdline.cmdline, p);
136 params = tag_next (params);
139 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
142 /* an ATAG_INITRD node tells the kernel where the compressed
143 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
145 params->hdr.tag = ATAG_INITRD2;
146 params->hdr.size = tag_size (tag_initrd);
148 params->u.initrd.start = initrd_start;
149 params->u.initrd.size = initrd_end - initrd_start;
151 params = tag_next (params);
154 static void setup_serial_tag(struct tag **tmp)
156 struct tag *params = *tmp;
157 struct tag_serialnr serialnr;
159 get_board_serial(&serialnr);
160 params->hdr.tag = ATAG_SERIAL;
161 params->hdr.size = tag_size (tag_serialnr);
162 params->u.serialnr.low = serialnr.low;
163 params->u.serialnr.high= serialnr.high;
164 params = tag_next (params);
168 static void setup_revision_tag(struct tag **in_params)
172 rev = get_board_rev();
173 params->hdr.tag = ATAG_REVISION;
174 params->hdr.size = tag_size (tag_revision);
175 params->u.revision.rev = rev;
176 params = tag_next (params);
179 static void setup_end_tag(struct bd_info *bd)
181 params->hdr.tag = ATAG_NONE;
182 params->hdr.size = 0;
185 __weak void setup_board_tags(struct tag **in_params) {}
188 static void do_nonsec_virt_switch(void)
191 dcache_disable(); /* flush cache before swtiching to EL2 */
195 __weak void board_prep_linux(struct bootm_headers *images) { }
197 /* Subcommand: PREP */
198 static void boot_prep_linux(struct bootm_headers *images)
200 char *commandline = env_get("bootargs");
202 if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
203 debug("using: FDT\n");
204 if (image_setup_linux(images)) {
205 panic("FDT creation failed!");
207 } else if (BOOTM_ENABLE_TAGS) {
208 debug("using: ATAGS\n");
209 setup_start_tag(gd->bd);
210 if (BOOTM_ENABLE_SERIAL_TAG)
211 setup_serial_tag(¶ms);
212 if (BOOTM_ENABLE_CMDLINE_TAG)
213 setup_commandline_tag(gd->bd, commandline);
214 if (BOOTM_ENABLE_REVISION_TAG)
215 setup_revision_tag(¶ms);
216 if (BOOTM_ENABLE_MEMORY_TAGS)
217 setup_memory_tags(gd->bd);
218 if (BOOTM_ENABLE_INITRD_TAG) {
220 * In boot_ramdisk_high(), it may relocate ramdisk to
221 * a specified location. And set images->initrd_start &
222 * images->initrd_end to relocated ramdisk's start/end
223 * addresses. So use them instead of images->rd_start &
224 * images->rd_end when possible.
226 if (images->initrd_start && images->initrd_end) {
227 setup_initrd_tag(gd->bd, images->initrd_start,
229 } else if (images->rd_start && images->rd_end) {
230 setup_initrd_tag(gd->bd, images->rd_start,
234 setup_board_tags(¶ms);
235 setup_end_tag(gd->bd);
237 panic("FDT and ATAGS support not compiled in\n");
240 board_prep_linux(images);
243 __weak bool armv7_boot_nonsec_default(void)
245 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
252 #ifdef CONFIG_ARMV7_NONSEC
253 bool armv7_boot_nonsec(void)
255 char *s = env_get("bootm_boot_mode");
256 bool nonsec = armv7_boot_nonsec_default();
258 if (s && !strcmp(s, "sec"))
261 if (s && !strcmp(s, "nonsec"))
269 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
273 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
274 static void switch_to_el1(void)
276 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
277 (images.os.arch == IH_ARCH_ARM))
278 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
279 (u64)images.ft_addr, 0,
283 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
291 static void boot_jump_linux(struct bootm_headers *images, int flag)
294 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
296 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
298 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
299 void *res2))images->ep;
301 debug("## Transferring control to Linux (at address %lx)...\n",
302 (ulong) kernel_entry);
303 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
305 announce_and_cleanup(fake);
308 #ifdef CONFIG_ARMV8_PSCI
311 do_nonsec_virt_switch();
313 update_os_arch_secondary_cores(images->os.arch);
315 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
316 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
317 (u64)switch_to_el1, ES_TO_AARCH64);
319 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
320 (images->os.arch == IH_ARCH_ARM))
321 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
322 (u64)images->ft_addr, 0,
326 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
332 unsigned long machid = gd->bd->bi_arch_number;
334 void (*kernel_entry)(int zero, int arch, uint params);
336 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
338 kernel_entry = (void (*)(int, int, uint))images->ep;
339 #ifdef CONFIG_CPU_V7M
340 ulong addr = (ulong)kernel_entry | 1;
341 kernel_entry = (void *)addr;
343 s = env_get("machid");
345 if (strict_strtoul(s, 16, &machid) < 0) {
346 debug("strict_strtoul failed!\n");
349 printf("Using machid 0x%lx from environment\n", machid);
352 debug("## Transferring control to Linux (at address %08lx)" \
353 "...\n", (ulong) kernel_entry);
354 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
355 announce_and_cleanup(fake);
357 if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len)
358 r2 = (unsigned long)images->ft_addr;
360 r2 = gd->bd->bi_boot_params;
363 #ifdef CONFIG_ARMV7_NONSEC
364 if (armv7_boot_nonsec()) {
366 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
370 kernel_entry(0, machid, r2);
375 /* Main Entry point for arm bootm implementation
377 * Modeled after the powerpc implementation
378 * DIFFERENCE: Instead of calling prep and go at the end
379 * they are called if subcommand is equal 0.
381 int do_bootm_linux(int flag, struct bootm_info *bmi)
383 struct bootm_headers *images = bmi->images;
385 /* No need for those on ARM */
386 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
389 if (flag & BOOTM_STATE_OS_PREP) {
390 boot_prep_linux(images);
394 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
395 boot_jump_linux(images, flag);
399 boot_prep_linux(images);
400 boot_jump_linux(images, flag);
404 #if defined(CONFIG_BOOTM_VXWORKS)
405 void boot_prep_vxworks(struct bootm_headers *images)
407 #if defined(CONFIG_OF_LIBFDT)
410 if (images->ft_addr) {
411 off = fdt_path_offset(images->ft_addr, "/memory");
413 if (arch_fixup_fdt(images->ft_addr))
414 puts("## WARNING: fixup memory failed!\n");
418 cleanup_before_linux();
421 void boot_jump_vxworks(struct bootm_headers *images)
423 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
428 /* ARM VxWorks requires device tree physical address to be passed */
429 ((void (*)(void *))images->ep)(images->ft_addr);