xref: /openbmc/u-boot/arch/arc/lib/bootm.c (revision baefb63a)
1 /*
2  * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 
9 DECLARE_GLOBAL_DATA_PTR;
10 
11 static ulong get_sp(void)
12 {
13 	ulong ret;
14 
15 	asm("mov %0, sp" : "=r"(ret) : );
16 	return ret;
17 }
18 
19 void arch_lmb_reserve(struct lmb *lmb)
20 {
21 	ulong sp;
22 
23 	/*
24 	 * Booting a (Linux) kernel image
25 	 *
26 	 * Allocate space for command line and board info - the
27 	 * address should be as high as possible within the reach of
28 	 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
29 	 * memory, which means far enough below the current stack
30 	 * pointer.
31 	 */
32 	sp = get_sp();
33 	debug("## Current stack ends at 0x%08lx ", sp);
34 
35 	/* adjust sp by 4K to be safe */
36 	sp -= 4096;
37 	lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp));
38 }
39 
40 int arch_fixup_fdt(void *blob)
41 {
42 	return 0;
43 }
44 
45 static int cleanup_before_linux(void)
46 {
47 	disable_interrupts();
48 	flush_dcache_all();
49 	invalidate_icache_all();
50 
51 	return 0;
52 }
53 
54 /* Subcommand: PREP */
55 static void boot_prep_linux(bootm_headers_t *images)
56 {
57 	if (image_setup_linux(images))
58 		hang();
59 }
60 
61 __weak void smp_set_core_boot_addr(unsigned long addr, int corenr) {}
62 __weak void smp_kick_all_cpus(void) {}
63 
64 /* Subcommand: GO */
65 static void boot_jump_linux(bootm_headers_t *images, int flag)
66 {
67 	void (*kernel_entry)(int zero, int arch, uint params);
68 	unsigned int r0, r2;
69 	int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
70 
71 	kernel_entry = (void (*)(int, int, uint))images->ep;
72 
73 	debug("## Transferring control to Linux (at address %08lx)...\n",
74 	      (ulong) kernel_entry);
75 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
76 
77 	printf("\nStarting kernel ...%s\n\n", fake ?
78 	       "(fake run for tracing)" : "");
79 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
80 
81 	cleanup_before_linux();
82 
83 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
84 		r0 = 2;
85 		r2 = (unsigned int)images->ft_addr;
86 	} else {
87 		r0 = 1;
88 		r2 = (unsigned int)env_get("bootargs");
89 	}
90 
91 	if (!fake) {
92 		smp_set_core_boot_addr((unsigned long)kernel_entry, -1);
93 		smp_kick_all_cpus();
94 		kernel_entry(r0, 0, r2);
95 	}
96 }
97 
98 int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
99 {
100 	/* No need for those on ARC */
101 	if ((flag & BOOTM_STATE_OS_BD_T) || (flag & BOOTM_STATE_OS_CMDLINE))
102 		return -1;
103 
104 	if (flag & BOOTM_STATE_OS_PREP) {
105 		boot_prep_linux(images);
106 		return 0;
107 	}
108 
109 	if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
110 		boot_jump_linux(images, flag);
111 		return 0;
112 	}
113 
114 	boot_prep_linux(images);
115 	boot_jump_linux(images, flag);
116 	return 0;
117 }
118