blob: 3e568dcd907b8c82806ee4c2bf4f355e64cf7d51 [file] [log] [blame]
Marc Zyngier092bd142012-12-17 17:07:52 +00001/*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/linkage.h>
19
20#include <asm/assembler.h>
21#include <asm/kvm_arm.h>
22#include <asm/kvm_mmu.h>
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000023#include <asm/pgtable-hwdef.h>
Marc Zyngier092bd142012-12-17 17:07:52 +000024
25 .text
26 .pushsection .hyp.idmap.text, "ax"
27
28 .align 11
29
30ENTRY(__kvm_hyp_init)
31 ventry __invalid // Synchronous EL2t
32 ventry __invalid // IRQ EL2t
33 ventry __invalid // FIQ EL2t
34 ventry __invalid // Error EL2t
35
36 ventry __invalid // Synchronous EL2h
37 ventry __invalid // IRQ EL2h
38 ventry __invalid // FIQ EL2h
39 ventry __invalid // Error EL2h
40
41 ventry __do_hyp_init // Synchronous 64-bit EL1
42 ventry __invalid // IRQ 64-bit EL1
43 ventry __invalid // FIQ 64-bit EL1
44 ventry __invalid // Error 64-bit EL1
45
46 ventry __invalid // Synchronous 32-bit EL1
47 ventry __invalid // IRQ 32-bit EL1
48 ventry __invalid // FIQ 32-bit EL1
49 ventry __invalid // Error 32-bit EL1
50
51__invalid:
52 b .
53
54 /*
55 * x0: HYP boot pgd
56 * x1: HYP pgd
57 * x2: HYP stack
58 * x3: HYP vectors
59 */
60__do_hyp_init:
61
62 msr ttbr0_el2, x0
63
64 mrs x4, tcr_el1
65 ldr x5, =TCR_EL2_MASK
66 and x4, x4, x5
67 ldr x5, =TCR_EL2_FLAGS
68 orr x4, x4, x5
Ard Biesheuvele4c5a682015-03-19 16:42:28 +000069
70#ifndef CONFIG_ARM64_VA_BITS_48
71 /*
72 * If we are running with VA_BITS < 48, we may be running with an extra
73 * level of translation in the ID map. This is only the case if system
74 * RAM is out of range for the currently configured page size and number
75 * of translation levels, in which case we will also need the extra
76 * level for the HYP ID map, or we won't be able to enable the EL2 MMU.
77 *
78 * However, at EL2, there is only one TTBR register, and we can't switch
79 * between translation tables *and* update TCR_EL2.T0SZ at the same
80 * time. Bottom line: we need the extra level in *both* our translation
81 * tables.
82 *
83 * So use the same T0SZ value we use for the ID map.
84 */
85 ldr_l x5, idmap_t0sz
86 bfi x4, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
87#endif
Marc Zyngier092bd142012-12-17 17:07:52 +000088 msr tcr_el2, x4
89
90 ldr x4, =VTCR_EL2_FLAGS
Radha Mohan Chintakuntla87366d82014-03-07 08:49:25 +000091 /*
92 * Read the PARange bits from ID_AA64MMFR0_EL1 and set the PS bits in
93 * VTCR_EL2.
94 */
95 mrs x5, ID_AA64MMFR0_EL1
96 bfi x4, x5, #16, #3
Vladimir Murzin20475f72015-11-16 11:28:18 +000097 /*
98 * Read the VMIDBits bits from ID_AA64MMFR1_EL1 and set the VS bit in
99 * VTCR_EL2.
100 */
101 mrs x5, ID_AA64MMFR1_EL1
102 ubfx x5, x5, #5, #1
103 lsl x5, x5, #VTCR_EL2_VS
104 orr x4, x4, x5
105
Marc Zyngier092bd142012-12-17 17:07:52 +0000106 msr vtcr_el2, x4
107
108 mrs x4, mair_el1
109 msr mair_el2, x4
110 isb
111
Pranavkumar Sawargaonkarf6edbbf2014-07-31 12:23:23 +0530112 /* Invalidate the stale TLBs from Bootloader */
113 tlbi alle2
114 dsb sy
115
Marc Zyngier18ea3db2013-11-05 18:29:45 +0000116 mrs x4, sctlr_el2
117 and x4, x4, #SCTLR_EL2_EE // preserve endianness of EL2
118 ldr x5, =SCTLR_EL2_FLAGS
119 orr x4, x4, x5
Marc Zyngier092bd142012-12-17 17:07:52 +0000120 msr sctlr_el2, x4
121 isb
122
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000123 /* Skip the trampoline dance if we merged the boot and runtime PGDs */
124 cmp x0, x1
125 b.eq merged
126
Marc Zyngier092bd142012-12-17 17:07:52 +0000127 /* MMU is now enabled. Get ready for the trampoline dance */
128 ldr x4, =TRAMPOLINE_VA
129 adr x5, target
130 bfi x4, x5, #0, #PAGE_SHIFT
131 br x4
132
133target: /* We're now in the trampoline code, switch page tables */
134 msr ttbr0_el2, x1
135 isb
136
137 /* Invalidate the old TLBs */
138 tlbi alle2
139 dsb sy
140
Ard Biesheuvele4c5a682015-03-19 16:42:28 +0000141merged:
Marc Zyngier092bd142012-12-17 17:07:52 +0000142 /* Set the stack and new vectors */
143 kern_hyp_va x2
144 mov sp, x2
145 kern_hyp_va x3
146 msr vbar_el2, x3
147
148 /* Hello, World! */
149 eret
150ENDPROC(__kvm_hyp_init)
151
152 .ltorg
153
154 .popsection