common: Drop bootstage.h from common header
[platform/kernel/u-boot.git] / arch / arm / lib / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4  *  - Added prep subcommand support
5  *  - Reorganized source - modeled after powerpc version
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Marius Groeger <mgroeger@sysgo.de>
10  *
11  * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
12  */
13
14 #include <common.h>
15 #include <bootstage.h>
16 #include <command.h>
17 #include <cpu_func.h>
18 #include <dm.h>
19 #include <hang.h>
20 #include <dm/root.h>
21 #include <env.h>
22 #include <image.h>
23 #include <u-boot/zlib.h>
24 #include <asm/byteorder.h>
25 #include <linux/libfdt.h>
26 #include <mapmem.h>
27 #include <fdt_support.h>
28 #include <asm/bootm.h>
29 #include <asm/secure.h>
30 #include <linux/compiler.h>
31 #include <bootm.h>
32 #include <vxworks.h>
33 #include <asm/cache.h>
34
35 #ifdef CONFIG_ARMV7_NONSEC
36 #include <asm/armv7.h>
37 #endif
38 #include <asm/setup.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 static struct tag *params;
43
44 static ulong get_sp(void)
45 {
46         ulong ret;
47
48         asm("mov %0, sp" : "=r"(ret) : );
49         return ret;
50 }
51
52 void arch_lmb_reserve(struct lmb *lmb)
53 {
54         ulong sp, bank_end;
55         int bank;
56
57         /*
58          * Booting a (Linux) kernel image
59          *
60          * Allocate space for command line and board info - the
61          * address should be as high as possible within the reach of
62          * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
63          * memory, which means far enough below the current stack
64          * pointer.
65          */
66         sp = get_sp();
67         debug("## Current stack ends at 0x%08lx ", sp);
68
69         /* adjust sp by 4K to be safe */
70         sp -= 4096;
71         for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
72                 if (!gd->bd->bi_dram[bank].size ||
73                     sp < gd->bd->bi_dram[bank].start)
74                         continue;
75                 /* Watch out for RAM at end of address space! */
76                 bank_end = gd->bd->bi_dram[bank].start +
77                         gd->bd->bi_dram[bank].size - 1;
78                 if (sp > bank_end)
79                         continue;
80                 if (bank_end > gd->ram_top)
81                         bank_end = gd->ram_top - 1;
82
83                 lmb_reserve(lmb, sp, bank_end - sp + 1);
84                 break;
85         }
86 }
87
88 __weak void board_quiesce_devices(void)
89 {
90 }
91
92 /**
93  * announce_and_cleanup() - Print message and prepare for kernel boot
94  *
95  * @fake: non-zero to do everything except actually boot
96  */
97 static void announce_and_cleanup(int fake)
98 {
99         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
100 #ifdef CONFIG_BOOTSTAGE_FDT
101         bootstage_fdt_add_report();
102 #endif
103 #ifdef CONFIG_BOOTSTAGE_REPORT
104         bootstage_report();
105 #endif
106
107 #ifdef CONFIG_USB_DEVICE
108         udc_disconnect();
109 #endif
110
111         board_quiesce_devices();
112
113         printf("\nStarting kernel ...%s\n\n", fake ?
114                 "(fake run for tracing)" : "");
115         /*
116          * Call remove function of all devices with a removal flag set.
117          * This may be useful for last-stage operations, like cancelling
118          * of DMA operation or releasing device internal buffers.
119          */
120         dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
121
122         cleanup_before_linux();
123 }
124
125 static void setup_start_tag (bd_t *bd)
126 {
127         params = (struct tag *)bd->bi_boot_params;
128
129         params->hdr.tag = ATAG_CORE;
130         params->hdr.size = tag_size (tag_core);
131
132         params->u.core.flags = 0;
133         params->u.core.pagesize = 0;
134         params->u.core.rootdev = 0;
135
136         params = tag_next (params);
137 }
138
139 static void setup_memory_tags(bd_t *bd)
140 {
141         int i;
142
143         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
144                 params->hdr.tag = ATAG_MEM;
145                 params->hdr.size = tag_size (tag_mem32);
146
147                 params->u.mem.start = bd->bi_dram[i].start;
148                 params->u.mem.size = bd->bi_dram[i].size;
149
150                 params = tag_next (params);
151         }
152 }
153
154 static void setup_commandline_tag(bd_t *bd, char *commandline)
155 {
156         char *p;
157
158         if (!commandline)
159                 return;
160
161         /* eat leading white space */
162         for (p = commandline; *p == ' '; p++);
163
164         /* skip non-existent command lines so the kernel will still
165          * use its default command line.
166          */
167         if (*p == '\0')
168                 return;
169
170         params->hdr.tag = ATAG_CMDLINE;
171         params->hdr.size =
172                 (sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
173
174         strcpy (params->u.cmdline.cmdline, p);
175
176         params = tag_next (params);
177 }
178
179 static void setup_initrd_tag(bd_t *bd, ulong initrd_start, ulong initrd_end)
180 {
181         /* an ATAG_INITRD node tells the kernel where the compressed
182          * ramdisk can be found. ATAG_RDIMG is a better name, actually.
183          */
184         params->hdr.tag = ATAG_INITRD2;
185         params->hdr.size = tag_size (tag_initrd);
186
187         params->u.initrd.start = initrd_start;
188         params->u.initrd.size = initrd_end - initrd_start;
189
190         params = tag_next (params);
191 }
192
193 static void setup_serial_tag(struct tag **tmp)
194 {
195         struct tag *params = *tmp;
196         struct tag_serialnr serialnr;
197
198         get_board_serial(&serialnr);
199         params->hdr.tag = ATAG_SERIAL;
200         params->hdr.size = tag_size (tag_serialnr);
201         params->u.serialnr.low = serialnr.low;
202         params->u.serialnr.high= serialnr.high;
203         params = tag_next (params);
204         *tmp = params;
205 }
206
207 static void setup_revision_tag(struct tag **in_params)
208 {
209         u32 rev = 0;
210
211         rev = get_board_rev();
212         params->hdr.tag = ATAG_REVISION;
213         params->hdr.size = tag_size (tag_revision);
214         params->u.revision.rev = rev;
215         params = tag_next (params);
216 }
217
218 static void setup_end_tag(bd_t *bd)
219 {
220         params->hdr.tag = ATAG_NONE;
221         params->hdr.size = 0;
222 }
223
224 __weak void setup_board_tags(struct tag **in_params) {}
225
226 #ifdef CONFIG_ARM64
227 static void do_nonsec_virt_switch(void)
228 {
229         smp_kick_all_cpus();
230         dcache_disable();       /* flush cache before swtiching to EL2 */
231 }
232 #endif
233
234 __weak void board_prep_linux(bootm_headers_t *images) { }
235
236 /* Subcommand: PREP */
237 static void boot_prep_linux(bootm_headers_t *images)
238 {
239         char *commandline = env_get("bootargs");
240
241         if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
242 #ifdef CONFIG_OF_LIBFDT
243                 debug("using: FDT\n");
244                 if (image_setup_linux(images)) {
245                         printf("FDT creation failed! hanging...");
246                         hang();
247                 }
248 #endif
249         } else if (BOOTM_ENABLE_TAGS) {
250                 debug("using: ATAGS\n");
251                 setup_start_tag(gd->bd);
252                 if (BOOTM_ENABLE_SERIAL_TAG)
253                         setup_serial_tag(&params);
254                 if (BOOTM_ENABLE_CMDLINE_TAG)
255                         setup_commandline_tag(gd->bd, commandline);
256                 if (BOOTM_ENABLE_REVISION_TAG)
257                         setup_revision_tag(&params);
258                 if (BOOTM_ENABLE_MEMORY_TAGS)
259                         setup_memory_tags(gd->bd);
260                 if (BOOTM_ENABLE_INITRD_TAG) {
261                         /*
262                          * In boot_ramdisk_high(), it may relocate ramdisk to
263                          * a specified location. And set images->initrd_start &
264                          * images->initrd_end to relocated ramdisk's start/end
265                          * addresses. So use them instead of images->rd_start &
266                          * images->rd_end when possible.
267                          */
268                         if (images->initrd_start && images->initrd_end) {
269                                 setup_initrd_tag(gd->bd, images->initrd_start,
270                                                  images->initrd_end);
271                         } else if (images->rd_start && images->rd_end) {
272                                 setup_initrd_tag(gd->bd, images->rd_start,
273                                                  images->rd_end);
274                         }
275                 }
276                 setup_board_tags(&params);
277                 setup_end_tag(gd->bd);
278         } else {
279                 printf("FDT and ATAGS support not compiled in - hanging\n");
280                 hang();
281         }
282
283         board_prep_linux(images);
284 }
285
286 __weak bool armv7_boot_nonsec_default(void)
287 {
288 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
289         return false;
290 #else
291         return true;
292 #endif
293 }
294
295 #ifdef CONFIG_ARMV7_NONSEC
296 bool armv7_boot_nonsec(void)
297 {
298         char *s = env_get("bootm_boot_mode");
299         bool nonsec = armv7_boot_nonsec_default();
300
301         if (s && !strcmp(s, "sec"))
302                 nonsec = false;
303
304         if (s && !strcmp(s, "nonsec"))
305                 nonsec = true;
306
307         return nonsec;
308 }
309 #endif
310
311 #ifdef CONFIG_ARM64
312 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
313 {
314 }
315
316 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
317 static void switch_to_el1(void)
318 {
319         if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
320             (images.os.arch == IH_ARCH_ARM))
321                 armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
322                                     (u64)images.ft_addr, 0,
323                                     (u64)images.ep,
324                                     ES_TO_AARCH32);
325         else
326                 armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
327                                     images.ep,
328                                     ES_TO_AARCH64);
329 }
330 #endif
331 #endif
332
333 /* Subcommand: GO */
334 static void boot_jump_linux(bootm_headers_t *images, int flag)
335 {
336 #ifdef CONFIG_ARM64
337         void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
338                         void *res2);
339         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
340
341         kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
342                                 void *res2))images->ep;
343
344         debug("## Transferring control to Linux (at address %lx)...\n",
345                 (ulong) kernel_entry);
346         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
347
348         announce_and_cleanup(fake);
349
350         if (!fake) {
351 #ifdef CONFIG_ARMV8_PSCI
352                 armv8_setup_psci();
353 #endif
354                 do_nonsec_virt_switch();
355
356                 update_os_arch_secondary_cores(images->os.arch);
357
358 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
359                 armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
360                                     (u64)switch_to_el1, ES_TO_AARCH64);
361 #else
362                 if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
363                     (images->os.arch == IH_ARCH_ARM))
364                         armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
365                                             (u64)images->ft_addr, 0,
366                                             (u64)images->ep,
367                                             ES_TO_AARCH32);
368                 else
369                         armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
370                                             images->ep,
371                                             ES_TO_AARCH64);
372 #endif
373         }
374 #else
375         unsigned long machid = gd->bd->bi_arch_number;
376         char *s;
377         void (*kernel_entry)(int zero, int arch, uint params);
378         unsigned long r2;
379         int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
380
381         kernel_entry = (void (*)(int, int, uint))images->ep;
382 #ifdef CONFIG_CPU_V7M
383         ulong addr = (ulong)kernel_entry | 1;
384         kernel_entry = (void *)addr;
385 #endif
386         s = env_get("machid");
387         if (s) {
388                 if (strict_strtoul(s, 16, &machid) < 0) {
389                         debug("strict_strtoul failed!\n");
390                         return;
391                 }
392                 printf("Using machid 0x%lx from environment\n", machid);
393         }
394
395         debug("## Transferring control to Linux (at address %08lx)" \
396                 "...\n", (ulong) kernel_entry);
397         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
398         announce_and_cleanup(fake);
399
400         if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
401                 r2 = (unsigned long)images->ft_addr;
402         else
403                 r2 = gd->bd->bi_boot_params;
404
405         if (!fake) {
406 #ifdef CONFIG_ARMV7_NONSEC
407                 if (armv7_boot_nonsec()) {
408                         armv7_init_nonsec();
409                         secure_ram_addr(_do_nonsec_entry)(kernel_entry,
410                                                           0, machid, r2);
411                 } else
412 #endif
413                         kernel_entry(0, machid, r2);
414         }
415 #endif
416 }
417
418 /* Main Entry point for arm bootm implementation
419  *
420  * Modeled after the powerpc implementation
421  * DIFFERENCE: Instead of calling prep and go at the end
422  * they are called if subcommand is equal 0.
423  */
424 int do_bootm_linux(int flag, int argc, char * const argv[],
425                    bootm_headers_t *images)
426 {
427         /* No need for those on ARM */
428         if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
429                 return -1;
430
431         if (flag & BOOTM_STATE_OS_PREP) {
432                 boot_prep_linux(images);
433                 return 0;
434         }
435
436         if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
437                 boot_jump_linux(images, flag);
438                 return 0;
439         }
440
441         boot_prep_linux(images);
442         boot_jump_linux(images, flag);
443         return 0;
444 }
445
446 #if defined(CONFIG_BOOTM_VXWORKS)
447 void boot_prep_vxworks(bootm_headers_t *images)
448 {
449 #if defined(CONFIG_OF_LIBFDT)
450         int off;
451
452         if (images->ft_addr) {
453                 off = fdt_path_offset(images->ft_addr, "/memory");
454                 if (off > 0) {
455                         if (arch_fixup_fdt(images->ft_addr))
456                                 puts("## WARNING: fixup memory failed!\n");
457                 }
458         }
459 #endif
460         cleanup_before_linux();
461 }
462 void boot_jump_vxworks(bootm_headers_t *images)
463 {
464 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
465         armv8_setup_psci();
466         smp_kick_all_cpus();
467 #endif
468
469         /* ARM VxWorks requires device tree physical address to be passed */
470         ((void (*)(void *))images->ep)(images->ft_addr);
471 }
472 #endif