1cef75c4bef9f08f55a4a406715e4feec3645289
[platform/kernel/u-boot.git] / arch / powerpc / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008 Semihalf
4  *
5  * (C) Copyright 2000-2006
6  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  */
8
9
10 #include <common.h>
11 #include <bootstage.h>
12 #include <cpu_func.h>
13 #include <env.h>
14 #include <init.h>
15 #include <watchdog.h>
16 #include <command.h>
17 #include <image.h>
18 #include <malloc.h>
19 #include <u-boot/zlib.h>
20 #include <bzlib.h>
21 #include <asm/byteorder.h>
22 #include <asm/mp.h>
23 #include <bootm.h>
24 #include <vxworks.h>
25
26 #if defined(CONFIG_OF_LIBFDT)
27 #include <linux/libfdt.h>
28 #include <fdt_support.h>
29 #endif
30
31 #ifdef CONFIG_SYS_INIT_RAM_LOCK
32 #include <asm/cache.h>
33 #endif
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 static ulong get_sp (void);
38 extern void ft_fixup_num_cores(void *blob);
39 static void set_clocks_in_mhz (bd_t *kbd);
40
41 #ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
42 #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE        (768*1024*1024)
43 #endif
44
45 static void boot_jump_linux(bootm_headers_t *images)
46 {
47         void    (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
48                           ulong r7, ulong r8, ulong r9);
49 #ifdef CONFIG_OF_LIBFDT
50         char *of_flat_tree = images->ft_addr;
51 #endif
52
53         kernel = (void (*)(bd_t *, ulong, ulong, ulong,
54                            ulong, ulong, ulong))images->ep;
55         debug ("## Transferring control to Linux (at address %08lx) ...\n",
56                 (ulong)kernel);
57
58         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
59
60 #ifdef CONFIG_BOOTSTAGE_FDT
61         bootstage_fdt_add_report();
62 #endif
63 #ifdef CONFIG_BOOTSTAGE_REPORT
64         bootstage_report();
65 #endif
66
67 #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
68         unlock_ram_in_cache();
69 #endif
70
71 #if defined(CONFIG_OF_LIBFDT)
72         if (of_flat_tree) {     /* device tree; boot new style */
73                 /*
74                  * Linux Kernel Parameters (passing device tree):
75                  *   r3: pointer to the fdt
76                  *   r4: 0
77                  *   r5: 0
78                  *   r6: epapr magic
79                  *   r7: size of IMA in bytes
80                  *   r8: 0
81                  *   r9: 0
82                  */
83                 debug ("   Booting using OF flat tree...\n");
84                 WATCHDOG_RESET ();
85                 (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
86                            env_get_bootm_mapsize(), 0, 0);
87                 /* does not return */
88         } else
89 #endif
90         {
91                 /*
92                  * Linux Kernel Parameters (passing board info data):
93                  *   r3: ptr to board info data
94                  *   r4: initrd_start or 0 if no initrd
95                  *   r5: initrd_end - unused if r4 is 0
96                  *   r6: Start of command line string
97                  *   r7: End   of command line string
98                  *   r8: 0
99                  *   r9: 0
100                  */
101                 ulong cmd_start = images->cmdline_start;
102                 ulong cmd_end = images->cmdline_end;
103                 ulong initrd_start = images->initrd_start;
104                 ulong initrd_end = images->initrd_end;
105                 bd_t *kbd = images->kbd;
106
107                 debug ("   Booting using board info...\n");
108                 WATCHDOG_RESET ();
109                 (*kernel) (kbd, initrd_start, initrd_end,
110                            cmd_start, cmd_end, 0, 0);
111                 /* does not return */
112         }
113         return ;
114 }
115
116 void arch_lmb_reserve(struct lmb *lmb)
117 {
118         phys_size_t bootm_size;
119         ulong size, sp, bootmap_base;
120
121         bootmap_base = env_get_bootm_low();
122         bootm_size = env_get_bootm_size();
123
124 #ifdef DEBUG
125         if (((u64)bootmap_base + bootm_size) >
126             (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
127                 puts("WARNING: bootm_low + bootm_size exceed total memory\n");
128         if ((bootmap_base + bootm_size) > get_effective_memsize())
129                 puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
130 #endif
131
132         size = min(bootm_size, get_effective_memsize());
133         size = min(size, (ulong)CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
134
135         if (size < bootm_size) {
136                 ulong base = bootmap_base + size;
137                 printf("WARNING: adjusting available memory to %lx\n", size);
138                 lmb_reserve(lmb, base, bootm_size - size);
139         }
140
141         /*
142          * Booting a (Linux) kernel image
143          *
144          * Allocate space for command line and board info - the
145          * address should be as high as possible within the reach of
146          * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
147          * memory, which means far enough below the current stack
148          * pointer.
149          */
150         sp = get_sp();
151         debug ("## Current stack ends at 0x%08lx\n", sp);
152
153         /* adjust sp by 4K to be safe */
154         sp -= 4096;
155         lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
156
157 #ifdef CONFIG_MP
158         cpu_mp_lmb_reserve(lmb);
159 #endif
160
161         return ;
162 }
163
164 static void boot_prep_linux(bootm_headers_t *images)
165 {
166 #ifdef CONFIG_MP
167         /*
168          * if we are MP make sure to flush the device tree so any changes are
169          * made visibile to all other cores.  In AMP boot scenarios the cores
170          * might not be HW cache coherent with each other.
171          */
172         flush_cache((unsigned long)images->ft_addr, images->ft_len);
173 #endif
174 }
175
176 static int boot_cmdline_linux(bootm_headers_t *images)
177 {
178         ulong of_size = images->ft_len;
179         struct lmb *lmb = &images->lmb;
180         ulong *cmd_start = &images->cmdline_start;
181         ulong *cmd_end = &images->cmdline_end;
182
183         int ret = 0;
184
185         if (!of_size) {
186                 /* allocate space and init command line */
187                 ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
188                 if (ret) {
189                         puts("ERROR with allocation of cmdline\n");
190                         return ret;
191                 }
192         }
193
194         return ret;
195 }
196
197 static int boot_bd_t_linux(bootm_headers_t *images)
198 {
199         ulong of_size = images->ft_len;
200         struct lmb *lmb = &images->lmb;
201         bd_t **kbd = &images->kbd;
202
203         int ret = 0;
204
205         if (!of_size) {
206                 /* allocate space for kernel copy of board info */
207                 ret = boot_get_kbd (lmb, kbd);
208                 if (ret) {
209                         puts("ERROR with allocation of kernel bd\n");
210                         return ret;
211                 }
212                 set_clocks_in_mhz(*kbd);
213         }
214
215         return ret;
216 }
217
218 static int boot_body_linux(bootm_headers_t *images)
219 {
220         int ret;
221
222         /* allocate space for kernel copy of board info */
223         ret = boot_bd_t_linux(images);
224         if (ret)
225                 return ret;
226
227         ret = image_setup_linux(images);
228         if (ret)
229                 return ret;
230
231         return 0;
232 }
233
234 noinline
235 int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
236 {
237         int     ret;
238
239         if (flag & BOOTM_STATE_OS_CMDLINE) {
240                 boot_cmdline_linux(images);
241                 return 0;
242         }
243
244         if (flag & BOOTM_STATE_OS_BD_T) {
245                 boot_bd_t_linux(images);
246                 return 0;
247         }
248
249         if (flag & BOOTM_STATE_OS_PREP) {
250                 boot_prep_linux(images);
251                 return 0;
252         }
253
254         boot_prep_linux(images);
255         ret = boot_body_linux(images);
256         if (ret)
257                 return ret;
258         boot_jump_linux(images);
259
260         return 0;
261 }
262
263 static ulong get_sp (void)
264 {
265         ulong sp;
266
267         asm( "mr %0,1": "=r"(sp) : );
268         return sp;
269 }
270
271 static void set_clocks_in_mhz (bd_t *kbd)
272 {
273         char    *s;
274
275         s = env_get("clocks_in_mhz");
276         if (s) {
277                 /* convert all clock information to MHz */
278                 kbd->bi_intfreq /= 1000000L;
279                 kbd->bi_busfreq /= 1000000L;
280 #if defined(CONFIG_CPM2)
281                 kbd->bi_cpmfreq /= 1000000L;
282                 kbd->bi_brgfreq /= 1000000L;
283                 kbd->bi_sccfreq /= 1000000L;
284                 kbd->bi_vco     /= 1000000L;
285 #endif
286         }
287 }
288
289 #if defined(CONFIG_BOOTM_VXWORKS)
290 void boot_prep_vxworks(bootm_headers_t *images)
291 {
292 #if defined(CONFIG_OF_LIBFDT)
293         int off;
294         u64 base, size;
295
296         if (!images->ft_addr)
297                 return;
298
299         base = (u64)gd->bd->bi_memstart;
300         size = (u64)gd->bd->bi_memsize;
301
302         off = fdt_path_offset(images->ft_addr, "/memory");
303         if (off < 0)
304                 fdt_fixup_memory(images->ft_addr, base, size);
305
306 #if defined(CONFIG_MP)
307 #if defined(CONFIG_MPC85xx)
308         ft_fixup_cpu(images->ft_addr, base + size);
309         ft_fixup_num_cores(images->ft_addr);
310 #elif defined(CONFIG_MPC86xx)
311         off = fdt_add_mem_rsv(images->ft_addr,
312                         determine_mp_bootpg(NULL), (u64)4096);
313         if (off < 0)
314                 printf("## WARNING %s: %s\n", __func__, fdt_strerror(off));
315         ft_fixup_num_cores(images->ft_addr);
316 #endif
317         flush_cache((unsigned long)images->ft_addr, images->ft_len);
318 #endif
319 #endif
320 }
321
322 void boot_jump_vxworks(bootm_headers_t *images)
323 {
324         /* PowerPC VxWorks boot interface conforms to the ePAPR standard
325          * general purpuse registers:
326          *
327          *      r3: Effective address of the device tree image
328          *      r4: 0
329          *      r5: 0
330          *      r6: ePAPR magic value
331          *      r7: shall be the size of the boot IMA in bytes
332          *      r8: 0
333          *      r9: 0
334          *      TCR: WRC = 0, no watchdog timer reset will occur
335          */
336         WATCHDOG_RESET();
337
338         ((void (*)(void *, ulong, ulong, ulong,
339                 ulong, ulong, ulong))images->ep)(images->ft_addr,
340                 0, 0, EPAPR_MAGIC, env_get_bootm_mapsize(), 0, 0);
341 }
342 #endif