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