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