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