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