blob: 9edc01142097db058dc73b7a14a6e56c9aacb5b0 [file] [log] [blame]
Furquan Shaikh668316b2014-08-30 21:59:11 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Furquan Shaikh668316b2014-08-30 21:59:11 -070014 */
15
Julius Werner7dcf9d52015-10-16 13:10:02 -070016#include <arch/cache.h>
Furquan Shaikh668316b2014-08-30 21:59:11 -070017#include <arch/lib_helpers.h>
18#include <arch/transition.h>
Julius Werner7dcf9d52015-10-16 13:10:02 -070019#include <assert.h>
Furquan Shaikh668316b2014-08-30 21:59:11 -070020#include <console/console.h>
21
Furquan Shaikh668316b2014-08-30 21:59:11 -070022/* Litte-endian, No XN-forced, Instr cache disabled,
23 * Stack alignment disabled, Data and unified cache
24 * disabled, Alignment check disabled, MMU disabled
25 */
26#define SCTLR_MASK (SCTLR_MMU_DISABLE | SCTLR_ACE_DISABLE | \
27 SCTLR_CACHE_DISABLE | SCTLR_SAE_DISABLE | SCTLR_RES1 | \
28 SCTLR_ICE_DISABLE | SCTLR_WXN_DISABLE | SCTLR_LITTLE_END)
29
30void __attribute__((weak)) exc_dispatch(struct exc_state *exc_state, uint64_t id)
31{
32 /* Default weak implementation does nothing. */
33}
34
35void exc_entry(struct exc_state *exc_state, uint64_t id)
36{
37 struct elx_state *elx = &exc_state->elx;
38 struct regs *regs = &exc_state->regs;
39 uint8_t elx_mode, elx_el;
40
41 elx->spsr = raw_read_spsr_current();
42 elx_mode = get_mode_from_spsr(elx->spsr);
43 elx_el = get_el_from_spsr(elx->spsr);
44
45 if (elx_mode == SPSR_USE_H) {
46 if (elx_el == get_current_el())
47 regs->sp = (uint64_t)&exc_state[1];
48 else
49 regs->sp = raw_read_sp_elx(elx_el);
50 } else {
51 regs->sp = raw_read_sp_el0();
52 }
53
54 elx->elr = raw_read_elr_current();
55
56 exc_dispatch(exc_state, id);
57}
58
59void transition_with_entry(void *entry, void *arg, struct exc_state *exc_state)
60{
61 /* Argument to entry point goes into X0 */
62 exc_state->regs.x[X0_INDEX] = (uint64_t)arg;
63 /* Entry point goes into ELR */
64 exc_state->elx.elr = (uint64_t)entry;
65
66 transition(exc_state);
67}
68
69void transition(struct exc_state *exc_state)
70{
Furquan Shaikh668316b2014-08-30 21:59:11 -070071 uint64_t sctlr;
72 uint32_t current_el = get_current_el();
73
74 struct elx_state *elx = &exc_state->elx;
75 struct regs *regs = &exc_state->regs;
76
77 uint8_t elx_el = get_el_from_spsr(elx->spsr);
78
79 /*
80 * Policies enforced:
81 * 1. We support only elx --> (elx - 1) transitions
82 * 2. We support transitions to Aarch64 mode only
83 *
84 * If any of the above conditions holds false, then we need a proper way
85 * to update SCR/HCR before removing the checks below
86 */
87 if ((current_el - elx_el) != 1)
88 die("ARM64 Error: Do not support transition\n");
89
90 if (elx->spsr & SPSR_ERET_32)
91 die("ARM64 Error: Do not support eret to Aarch32\n");
Furquan Shaikh668316b2014-08-30 21:59:11 -070092
Julius Werner7dcf9d52015-10-16 13:10:02 -070093 /* Most parts of coreboot currently don't support EL2 anyway. */
94 assert(current_el == EL3);
95
96 /* Initialize SCR with defaults for running without secure monitor. */
97 raw_write_scr_el3(SCR_TWE_DISABLE | /* don't trap WFE */
98 SCR_TWI_DISABLE | /* don't trap WFI */
99 SCR_ST_ENABLE | /* allow secure timer access */
100 SCR_LOWER_AARCH64 | /* lower level is AArch64 */
101 SCR_SIF_DISABLE | /* disable secure ins. fetch */
102 SCR_HVC_ENABLE | /* allow HVC instruction */
103 SCR_SMD_ENABLE | /* disable SMC instruction */
104 SCR_RES1 | /* reserved-1 bits */
105 SCR_EA_DISABLE | /* disable ext. abort trap */
106 SCR_FIQ_DISABLE | /* disable FIQ trap to EL3 */
107 SCR_IRQ_DISABLE | /* disable IRQ trap to EL3 */
108 SCR_NS_ENABLE); /* lower level is non-secure */
109
110 /* Initialize CPTR to not trap anything to EL3. */
111 raw_write_cptr_el3(CPTR_EL3_TCPAC_DISABLE | CPTR_EL3_TTA_DISABLE |
112 CPTR_EL3_TFP_DISABLE);
Furquan Shaikh668316b2014-08-30 21:59:11 -0700113
Julius Wernerda3a1462015-05-13 11:19:33 -0700114 /* ELR/SPSR: Write entry point and processor state of program */
Furquan Shaikh668316b2014-08-30 21:59:11 -0700115 raw_write_elr_current(elx->elr);
Julius Wernerda3a1462015-05-13 11:19:33 -0700116 raw_write_spsr_current(elx->spsr);
Furquan Shaikh668316b2014-08-30 21:59:11 -0700117
118 /* SCTLR: Initialize EL with selected properties */
119 sctlr = raw_read_sctlr(elx_el);
120 sctlr &= SCTLR_MASK;
121 raw_write_sctlr(sctlr, elx_el);
122
123 /* SP_ELx: Initialize stack pointer */
124 raw_write_sp_elx(elx->sp_elx, elx_el);
Julius Werner7dcf9d52015-10-16 13:10:02 -0700125 isb();
Furquan Shaikh668316b2014-08-30 21:59:11 -0700126
127 /* Eret to the entry point */
128 trans_switch(regs);
129}