Merge tag 'arm64-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64...
[linux-2.6-microblaze.git] / arch / arm64 / kvm / hyp-init.S
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012,2013 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #include <linux/linkage.h>
8
9 #include <asm/alternative.h>
10 #include <asm/assembler.h>
11 #include <asm/kvm_arm.h>
12 #include <asm/kvm_mmu.h>
13 #include <asm/pgtable-hwdef.h>
14 #include <asm/sysreg.h>
15 #include <asm/virt.h>
16
17         .text
18         .pushsection    .hyp.idmap.text, "ax"
19
20         .align  11
21
22 SYM_CODE_START(__kvm_hyp_init)
23         ventry  __invalid               // Synchronous EL2t
24         ventry  __invalid               // IRQ EL2t
25         ventry  __invalid               // FIQ EL2t
26         ventry  __invalid               // Error EL2t
27
28         ventry  __invalid               // Synchronous EL2h
29         ventry  __invalid               // IRQ EL2h
30         ventry  __invalid               // FIQ EL2h
31         ventry  __invalid               // Error EL2h
32
33         ventry  __do_hyp_init           // Synchronous 64-bit EL1
34         ventry  __invalid               // IRQ 64-bit EL1
35         ventry  __invalid               // FIQ 64-bit EL1
36         ventry  __invalid               // Error 64-bit EL1
37
38         ventry  __invalid               // Synchronous 32-bit EL1
39         ventry  __invalid               // IRQ 32-bit EL1
40         ventry  __invalid               // FIQ 32-bit EL1
41         ventry  __invalid               // Error 32-bit EL1
42
43 __invalid:
44         b       .
45
46         /*
47          * x0: HYP pgd
48          * x1: HYP stack
49          * x2: HYP vectors
50          * x3: per-CPU offset
51          */
52 __do_hyp_init:
53         /* Check for a stub HVC call */
54         cmp     x0, #HVC_STUB_HCALL_NR
55         b.lo    __kvm_handle_stub_hvc
56
57         phys_to_ttbr x4, x0
58 alternative_if ARM64_HAS_CNP
59         orr     x4, x4, #TTBR_CNP_BIT
60 alternative_else_nop_endif
61         msr     ttbr0_el2, x4
62
63         mrs     x4, tcr_el1
64         mov_q   x5, TCR_EL2_MASK
65         and     x4, x4, x5
66         mov     x5, #TCR_EL2_RES1
67         orr     x4, x4, x5
68
69         /*
70          * The ID map may be configured to use an extended virtual address
71          * range. This is only the case if system RAM is out of range for the
72          * currently configured page size and VA_BITS, in which case we will
73          * also need the extended virtual range for the HYP ID map, or we won't
74          * be able to enable the EL2 MMU.
75          *
76          * However, at EL2, there is only one TTBR register, and we can't switch
77          * between translation tables *and* update TCR_EL2.T0SZ at the same
78          * time. Bottom line: we need to use the extended range with *both* our
79          * translation tables.
80          *
81          * So use the same T0SZ value we use for the ID map.
82          */
83         ldr_l   x5, idmap_t0sz
84         bfi     x4, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
85
86         /*
87          * Set the PS bits in TCR_EL2.
88          */
89         tcr_compute_pa_size x4, #TCR_EL2_PS_SHIFT, x5, x6
90
91         msr     tcr_el2, x4
92
93         mrs     x4, mair_el1
94         msr     mair_el2, x4
95         isb
96
97         /* Invalidate the stale TLBs from Bootloader */
98         tlbi    alle2
99         dsb     sy
100
101         /*
102          * Preserve all the RES1 bits while setting the default flags,
103          * as well as the EE bit on BE. Drop the A flag since the compiler
104          * is allowed to generate unaligned accesses.
105          */
106         mov_q   x4, (SCTLR_EL2_RES1 | (SCTLR_ELx_FLAGS & ~SCTLR_ELx_A))
107 CPU_BE( orr     x4, x4, #SCTLR_ELx_EE)
108         msr     sctlr_el2, x4
109         isb
110
111         /* Set the stack and new vectors */
112         kern_hyp_va     x1
113         mov     sp, x1
114         msr     vbar_el2, x2
115
116         /* Set tpidr_el2 for use by HYP */
117         msr     tpidr_el2, x3
118
119         /* Hello, World! */
120         eret
121 SYM_CODE_END(__kvm_hyp_init)
122
123 SYM_CODE_START(__kvm_handle_stub_hvc)
124         cmp     x0, #HVC_SOFT_RESTART
125         b.ne    1f
126
127         /* This is where we're about to jump, staying at EL2 */
128         msr     elr_el2, x1
129         mov     x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT | PSR_MODE_EL2h)
130         msr     spsr_el2, x0
131
132         /* Shuffle the arguments, and don't come back */
133         mov     x0, x2
134         mov     x1, x3
135         mov     x2, x4
136         b       reset
137
138 1:      cmp     x0, #HVC_RESET_VECTORS
139         b.ne    1f
140
141         /*
142          * Set the HVC_RESET_VECTORS return code before entering the common
143          * path so that we do not clobber x0-x2 in case we are coming via
144          * HVC_SOFT_RESTART.
145          */
146         mov     x0, xzr
147 reset:
148         /* Reset kvm back to the hyp stub. */
149         mrs     x5, sctlr_el2
150         mov_q   x6, SCTLR_ELx_FLAGS
151         bic     x5, x5, x6              // Clear SCTL_M and etc
152         pre_disable_mmu_workaround
153         msr     sctlr_el2, x5
154         isb
155
156         /* Install stub vectors */
157         adr_l   x5, __hyp_stub_vectors
158         msr     vbar_el2, x5
159         eret
160
161 1:      /* Bad stub call */
162         mov_q   x0, HVC_STUB_ERR
163         eret
164
165 SYM_CODE_END(__kvm_handle_stub_hvc)
166
167         .popsection