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