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