1 // SPDX-License-Identifier: GPL-2.0+
4 * - Added prep subcommand support
5 * - Reorganized source - modeled after powerpc version
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
16 #include <bootstage.h>
21 #include <asm/global_data.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 __weak void board_quiesce_devices(void)
51 * announce_and_cleanup() - Print message and prepare for kernel boot
53 * @fake: non-zero to do everything except actually boot
55 static void announce_and_cleanup(int fake)
57 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
58 #ifdef CONFIG_BOOTSTAGE_FDT
59 bootstage_fdt_add_report();
61 #ifdef CONFIG_BOOTSTAGE_REPORT
65 #ifdef CONFIG_USB_DEVICE
69 board_quiesce_devices();
71 printf("\nStarting kernel ...%s\n\n", fake ?
72 "(fake run for tracing)" : "");
74 * Call remove function of all devices with a removal flag set.
75 * This may be useful for last-stage operations, like cancelling
76 * of DMA operation or releasing device internal buffers.
78 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
80 /* Remove all active vital devices next */
81 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
83 cleanup_before_linux();
86 static void setup_start_tag (struct bd_info *bd)
88 params = (struct tag *)bd->bi_boot_params;
90 params->hdr.tag = ATAG_CORE;
91 params->hdr.size = tag_size (tag_core);
93 params->u.core.flags = 0;
94 params->u.core.pagesize = 0;
95 params->u.core.rootdev = 0;
97 params = tag_next (params);
100 static void setup_memory_tags(struct bd_info *bd)
104 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
105 params->hdr.tag = ATAG_MEM;
106 params->hdr.size = tag_size (tag_mem32);
108 params->u.mem.start = bd->bi_dram[i].start;
109 params->u.mem.size = bd->bi_dram[i].size;
111 params = tag_next (params);
115 static void setup_commandline_tag(struct bd_info *bd, char *commandline)
122 /* eat leading white space */
123 for (p = commandline; *p == ' '; p++);
125 /* skip non-existent command lines so the kernel will still
126 * use its default command line.
131 params->hdr.tag = ATAG_CMDLINE;
133 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
135 strcpy (params->u.cmdline.cmdline, p);
137 params = tag_next (params);
140 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
143 /* an ATAG_INITRD node tells the kernel where the compressed
144 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
146 params->hdr.tag = ATAG_INITRD2;
147 params->hdr.size = tag_size (tag_initrd);
149 params->u.initrd.start = initrd_start;
150 params->u.initrd.size = initrd_end - initrd_start;
152 params = tag_next (params);
155 static void setup_serial_tag(struct tag **tmp)
157 struct tag *params = *tmp;
158 struct tag_serialnr serialnr;
160 get_board_serial(&serialnr);
161 params->hdr.tag = ATAG_SERIAL;
162 params->hdr.size = tag_size (tag_serialnr);
163 params->u.serialnr.low = serialnr.low;
164 params->u.serialnr.high= serialnr.high;
165 params = tag_next (params);
169 static void setup_revision_tag(struct tag **in_params)
173 rev = get_board_rev();
174 params->hdr.tag = ATAG_REVISION;
175 params->hdr.size = tag_size (tag_revision);
176 params->u.revision.rev = rev;
177 params = tag_next (params);
180 static void setup_end_tag(struct bd_info *bd)
182 params->hdr.tag = ATAG_NONE;
183 params->hdr.size = 0;
186 __weak void setup_board_tags(struct tag **in_params) {}
189 static void do_nonsec_virt_switch(void)
192 dcache_disable(); /* flush cache before swtiching to EL2 */
196 __weak void board_prep_linux(struct bootm_headers *images) { }
198 /* Subcommand: PREP */
199 static void boot_prep_linux(struct bootm_headers *images)
201 char *commandline = env_get("bootargs");
203 if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
204 debug("using: FDT\n");
205 if (image_setup_linux(images)) {
206 panic("FDT creation failed!");
208 } else if (BOOTM_ENABLE_TAGS) {
209 debug("using: ATAGS\n");
210 setup_start_tag(gd->bd);
211 if (BOOTM_ENABLE_SERIAL_TAG)
212 setup_serial_tag(¶ms);
213 if (BOOTM_ENABLE_CMDLINE_TAG)
214 setup_commandline_tag(gd->bd, commandline);
215 if (BOOTM_ENABLE_REVISION_TAG)
216 setup_revision_tag(¶ms);
217 if (BOOTM_ENABLE_MEMORY_TAGS)
218 setup_memory_tags(gd->bd);
219 if (BOOTM_ENABLE_INITRD_TAG) {
221 * In boot_ramdisk_high(), it may relocate ramdisk to
222 * a specified location. And set images->initrd_start &
223 * images->initrd_end to relocated ramdisk's start/end
224 * addresses. So use them instead of images->rd_start &
225 * images->rd_end when possible.
227 if (images->initrd_start && images->initrd_end) {
228 setup_initrd_tag(gd->bd, images->initrd_start,
230 } else if (images->rd_start && images->rd_end) {
231 setup_initrd_tag(gd->bd, images->rd_start,
235 setup_board_tags(¶ms);
236 setup_end_tag(gd->bd);
238 panic("FDT and ATAGS support not compiled in\n");
241 board_prep_linux(images);
244 __weak bool armv7_boot_nonsec_default(void)
246 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
253 #ifdef CONFIG_ARMV7_NONSEC
254 bool armv7_boot_nonsec(void)
256 char *s = env_get("bootm_boot_mode");
257 bool nonsec = armv7_boot_nonsec_default();
259 if (s && !strcmp(s, "sec"))
262 if (s && !strcmp(s, "nonsec"))
270 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
274 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
275 static void switch_to_el1(void)
277 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
278 (images.os.arch == IH_ARCH_ARM))
279 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
280 (u64)images.ft_addr, 0,
284 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
292 static void boot_jump_linux(struct bootm_headers *images, int flag)
295 void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
297 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
299 kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
300 void *res2))images->ep;
302 debug("## Transferring control to Linux (at address %lx)...\n",
303 (ulong) kernel_entry);
304 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
306 announce_and_cleanup(fake);
309 #ifdef CONFIG_ARMV8_PSCI
312 do_nonsec_virt_switch();
314 update_os_arch_secondary_cores(images->os.arch);
316 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
317 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
318 (u64)switch_to_el1, ES_TO_AARCH64);
320 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
321 (images->os.arch == IH_ARCH_ARM))
322 armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
323 (u64)images->ft_addr, 0,
327 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
333 unsigned long machid = gd->bd->bi_arch_number;
335 void (*kernel_entry)(int zero, int arch, uint params);
337 int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
339 kernel_entry = (void (*)(int, int, uint))images->ep;
340 #ifdef CONFIG_CPU_V7M
341 ulong addr = (ulong)kernel_entry | 1;
342 kernel_entry = (void *)addr;
344 s = env_get("machid");
346 if (strict_strtoul(s, 16, &machid) < 0) {
347 debug("strict_strtoul failed!\n");
350 printf("Using machid 0x%lx from environment\n", machid);
353 debug("## Transferring control to Linux (at address %08lx)" \
354 "...\n", (ulong) kernel_entry);
355 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
356 announce_and_cleanup(fake);
358 if (CONFIG_IS_ENABLED(OF_LIBFDT) && images->ft_len)
359 r2 = (unsigned long)images->ft_addr;
361 r2 = gd->bd->bi_boot_params;
364 #ifdef CONFIG_ARMV7_NONSEC
365 if (armv7_boot_nonsec()) {
367 secure_ram_addr(_do_nonsec_entry)(kernel_entry,
371 kernel_entry(0, machid, r2);
376 /* Main Entry point for arm bootm implementation
378 * Modeled after the powerpc implementation
379 * DIFFERENCE: Instead of calling prep and go at the end
380 * they are called if subcommand is equal 0.
382 int do_bootm_linux(int flag, struct bootm_info *bmi)
384 struct bootm_headers *images = bmi->images;
386 /* No need for those on ARM */
387 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
390 if (flag & BOOTM_STATE_OS_PREP) {
391 boot_prep_linux(images);
395 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
396 boot_jump_linux(images, flag);
400 boot_prep_linux(images);
401 boot_jump_linux(images, flag);
405 #if defined(CONFIG_BOOTM_VXWORKS)
406 void boot_prep_vxworks(struct bootm_headers *images)
408 #if defined(CONFIG_OF_LIBFDT)
411 if (images->ft_addr) {
412 off = fdt_path_offset(images->ft_addr, "/memory");
414 if (arch_fixup_fdt(images->ft_addr))
415 puts("## WARNING: fixup memory failed!\n");
419 cleanup_before_linux();
422 void boot_jump_vxworks(struct bootm_headers *images)
424 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
429 /* ARM VxWorks requires device tree physical address to be passed */
430 ((void (*)(void *))images->ep)(images->ft_addr);