blob: 85c78f55789dbd2cfb774eafef86527f25ef3bb1 [file] [log] [blame]
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Switch to non-secure mode
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt
6 *
7 * This module contains the ARMv8 specific code required to adjust the exception
8 * level before booting an operating system.
9 */
10
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +010011#include <bootm.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070012#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +010015#include <asm/setjmp.h>
16
17/**
18 * entry_non_secure() - entry point when switching to non-secure mode
19 *
20 * When switching to non-secure mode switch_to_non_secure_mode() calls this
21 * function passing a jump buffer. We use this jump buffer to restore the
22 * original stack and register state.
23 *
24 * @non_secure_jmp: jump buffer for restoring stack and registers
25 */
26static void entry_non_secure(struct jmp_buf_data *non_secure_jmp)
27{
28 dcache_enable();
29 debug("Reached non-secure mode\n");
30
31 /* Restore stack and registers saved in switch_to_non_secure_mode() */
32 longjmp(non_secure_jmp, 1);
33}
34
35/**
36 * switch_to_non_secure_mode() - switch to non-secure mode
37 *
38 * Exception level EL3 is meant to be used by the secure monitor only (ARM
39 * trusted firmware being one embodiment). The operating system shall be
40 * started at exception level EL2. So here we check the exception level
41 * and switch it if necessary.
42 */
43void switch_to_non_secure_mode(void)
44{
45 struct jmp_buf_data non_secure_jmp;
46
47 /* On AArch64 we need to make sure we call our payload in < EL3 */
48 if (current_el() == 3) {
49 if (setjmp(&non_secure_jmp))
50 return;
51 dcache_disable(); /* flush cache before switch to EL2 */
52
53 /* Move into EL2 and keep running there */
54 armv8_switch_to_el2((uintptr_t)&non_secure_jmp, 0, 0, 0,
55 (uintptr_t)entry_non_secure, ES_TO_AARCH64);
56 }
57}