blob: 7baade61b073e39f792f93f4f869aa424fc339e3 [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 ARMv7 specific code required for leaving the
8 * secure mode 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>
Heinrich Schuchardtaa0b11b2019-01-08 18:13:06 +010014#include <asm/armv7.h>
15#include <asm/secure.h>
16#include <asm/setjmp.h>
17
18/**
19 * entry_non_secure() - entry point when switching to non-secure mode
20 *
21 * When switching to non-secure mode switch_to_non_secure_mode() calls this
22 * function passing a jump buffer. We use this jump buffer to restore the
23 * original stack and register state.
24 *
25 * @non_secure_jmp: jump buffer for restoring stack and registers
26 */
27static void entry_non_secure(struct jmp_buf_data *non_secure_jmp)
28{
29 dcache_enable();
30 debug("Reached non-secure mode\n");
31
32 /* Restore stack and registers saved in switch_to_non_secure_mode() */
33 longjmp(non_secure_jmp, 1);
34}
35
36/**
37 * switch_to_non_secure_mode() - switch to non-secure mode
38 *
39 * Operating systems may expect to run in non-secure mode. Here we check if
40 * we are running in secure mode and switch to non-secure mode if necessary.
41 */
42void switch_to_non_secure_mode(void)
43{
44 static bool is_nonsec;
45 struct jmp_buf_data non_secure_jmp;
46
47 if (armv7_boot_nonsec() && !is_nonsec) {
48 if (setjmp(&non_secure_jmp))
49 return;
50 dcache_disable(); /* flush cache before switch to HYP */
51 armv7_init_nonsec();
52 is_nonsec = true;
53 secure_ram_addr(_do_nonsec_entry)(entry_non_secure,
54 (uintptr_t)&non_secure_jmp,
55 0, 0);
56 }
57}