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