lmb: Always compile arch_lmb_reserve() into U-Boot on arm
[platform/kernel/u-boot.git] / arch / arm / lib / stack.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Andreas Bießmann <andreas@biessmann.org>
4  *
5  * Copyright (c) 2011 The Chromium OS Authors.
6  * (C) Copyright 2002-2006
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * (C) Copyright 2002
10  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11  * Marius Groeger <mgroeger@sysgo.de>
12  */
13 #include <common.h>
14 #include <init.h>
15 #include <lmb.h>
16 #include <asm/global_data.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 int arch_reserve_stacks(void)
21 {
22 #ifdef CONFIG_SPL_BUILD
23         gd->start_addr_sp -= 128;       /* leave 32 words for abort-stack */
24         gd->irq_sp = gd->start_addr_sp;
25 #else
26         /* setup stack pointer for exceptions */
27         gd->irq_sp = gd->start_addr_sp;
28
29 # if !defined(CONFIG_ARM64)
30         /* leave 3 words for abort-stack, plus 1 for alignment */
31         gd->start_addr_sp -= 16;
32 # endif
33 #endif
34
35         return 0;
36 }
37
38 static ulong get_sp(void)
39 {
40         ulong ret;
41
42         asm("mov %0, sp" : "=r"(ret) : );
43         return ret;
44 }
45
46 void arch_lmb_reserve(struct lmb *lmb)
47 {
48         ulong sp, bank_end;
49         int bank;
50
51         /*
52          * Booting a (Linux) kernel image
53          *
54          * Allocate space for command line and board info - the
55          * address should be as high as possible within the reach of
56          * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
57          * memory, which means far enough below the current stack
58          * pointer.
59          */
60         sp = get_sp();
61         debug("## Current stack ends at 0x%08lx ", sp);
62
63         /* adjust sp by 4K to be safe */
64         sp -= 4096;
65         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
66                 if (!gd->bd->bi_dram[bank].size ||
67                     sp < gd->bd->bi_dram[bank].start)
68                         continue;
69                 /* Watch out for RAM at end of address space! */
70                 bank_end = gd->bd->bi_dram[bank].start +
71                         gd->bd->bi_dram[bank].size - 1;
72                 if (sp > bank_end)
73                         continue;
74                 if (bank_end > gd->ram_top)
75                         bank_end = gd->ram_top - 1;
76
77                 lmb_reserve(lmb, sp, bank_end - sp + 1);
78                 break;
79         }
80 }