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