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