Commit | Line | Data |
---|---|---|
bbf45ba5 HB |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License, version 2, as | |
4 | * published by the Free Software Foundation. | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | * You should have received a copy of the GNU General Public License | |
12 | * along with this program; if not, write to the Free Software | |
13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
14 | * | |
15 | * Copyright IBM Corp. 2008 | |
16 | * | |
17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> | |
18 | */ | |
19 | ||
20 | #ifndef __POWERPC_KVM_PPC_H__ | |
21 | #define __POWERPC_KVM_PPC_H__ | |
22 | ||
23 | /* This file exists just so we can dereference kvm_vcpu, avoiding nested header | |
24 | * dependencies. */ | |
25 | ||
26 | #include <linux/mutex.h> | |
27 | #include <linux/timer.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/kvm_types.h> | |
30 | #include <linux/kvm_host.h> | |
a136a8bd | 31 | #include <linux/bug.h> |
1c0006d8 AG |
32 | #ifdef CONFIG_PPC_BOOK3S |
33 | #include <asm/kvm_book3s.h> | |
c7f38f46 AG |
34 | #else |
35 | #include <asm/kvm_booke.h> | |
1c0006d8 | 36 | #endif |
371fefd6 PM |
37 | #ifdef CONFIG_KVM_BOOK3S_64_HANDLER |
38 | #include <asm/paca.h> | |
39 | #endif | |
bbf45ba5 | 40 | |
a59c1d9e MS |
41 | /* |
42 | * KVMPPC_INST_SW_BREAKPOINT is debug Instruction | |
43 | * for supporting software breakpoint. | |
44 | */ | |
45 | #define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00 | |
46 | ||
bbf45ba5 HB |
47 | enum emulation_result { |
48 | EMULATE_DONE, /* no further processing */ | |
49 | EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */ | |
bbf45ba5 | 50 | EMULATE_FAIL, /* can't emulate this instruction */ |
37f5bca6 | 51 | EMULATE_AGAIN, /* something went wrong. go again */ |
c402a3f4 | 52 | EMULATE_EXIT_USER, /* emulation requires exit to user-space */ |
bbf45ba5 HB |
53 | }; |
54 | ||
51f04726 MC |
55 | enum instruction_type { |
56 | INST_GENERIC, | |
57 | INST_SC, /* system call */ | |
58 | }; | |
59 | ||
7d15c06f AG |
60 | enum xlate_instdata { |
61 | XLATE_INST, /* translate instruction address */ | |
62 | XLATE_DATA /* translate data address */ | |
63 | }; | |
64 | ||
65 | enum xlate_readwrite { | |
66 | XLATE_READ, /* check for read permissions */ | |
67 | XLATE_WRITE /* check for write permissions */ | |
68 | }; | |
69 | ||
df6909e5 | 70 | extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); |
bbf45ba5 | 71 | extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); |
29eb61bc | 72 | extern void kvmppc_handler_highmem(void); |
bbf45ba5 HB |
73 | |
74 | extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu); | |
75 | extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
76 | unsigned int rt, unsigned int bytes, | |
73601775 | 77 | int is_default_endian); |
3587d534 AG |
78 | extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, |
79 | unsigned int rt, unsigned int bytes, | |
73601775 | 80 | int is_default_endian); |
bbf45ba5 | 81 | extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
73601775 CLG |
82 | u64 val, unsigned int bytes, |
83 | int is_default_endian); | |
bbf45ba5 | 84 | |
51f04726 MC |
85 | extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, |
86 | enum instruction_type type, u32 *inst); | |
87 | ||
35c4a733 AG |
88 | extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
89 | bool data); | |
90 | extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, | |
91 | bool data); | |
bbf45ba5 HB |
92 | extern int kvmppc_emulate_instruction(struct kvm_run *run, |
93 | struct kvm_vcpu *vcpu); | |
d69614a2 | 94 | extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu); |
ce263d70 | 95 | extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu); |
75f74f0d | 96 | extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu); |
5ce941ee | 97 | extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb); |
d02d4d15 | 98 | extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu); |
af8f38b3 | 99 | extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu); |
f61c94bb BB |
100 | extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu); |
101 | extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu); | |
bbf45ba5 | 102 | |
ecc0981f HB |
103 | /* Core-specific hooks */ |
104 | ||
89168618 | 105 | extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr, |
7924bd41 | 106 | unsigned int gtlb_idx); |
bbf45ba5 | 107 | extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode); |
49dd2c49 | 108 | extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid); |
ecc0981f | 109 | extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu); |
9cc5e953 | 110 | extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu); |
fa86b8dd HB |
111 | extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr); |
112 | extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr); | |
be8d1cae HB |
113 | extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index, |
114 | gva_t eaddr); | |
b52a638c HB |
115 | extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu); |
116 | extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu); | |
7d15c06f AG |
117 | extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, |
118 | enum xlate_instdata xlid, enum xlate_readwrite xlrw, | |
119 | struct kvmppc_pte *pte); | |
9dd921cf | 120 | |
db93f574 HB |
121 | extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, |
122 | unsigned int id); | |
123 | extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu); | |
5cbb5106 | 124 | extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu); |
9dd921cf | 125 | extern int kvmppc_core_check_processor_compat(void); |
5cbb5106 HB |
126 | extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu, |
127 | struct kvm_translation *tr); | |
9dd921cf HB |
128 | |
129 | extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu); | |
130 | extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu); | |
131 | ||
a8e4ef84 | 132 | extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu); |
9dd921cf | 133 | extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu); |
25a8a02d | 134 | extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags); |
9dd921cf | 135 | extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu); |
7706664d | 136 | extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu); |
9dd921cf HB |
137 | extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, |
138 | struct kvm_interrupt *irq); | |
4fe27d2a | 139 | extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu); |
8de12015 AG |
140 | extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags, |
141 | ulong esr_flags); | |
142 | extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, | |
143 | ulong dear_flags, | |
144 | ulong esr_flags); | |
145 | extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu); | |
146 | extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, | |
147 | ulong esr_flags); | |
862d31f7 | 148 | extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu); |
7c973a2e | 149 | extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu); |
75f74f0d | 150 | |
db93f574 HB |
151 | extern int kvmppc_booke_init(void); |
152 | extern void kvmppc_booke_exit(void); | |
153 | ||
c30f8a6c | 154 | extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu); |
2a342ed5 | 155 | extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu); |
a4cd8b23 | 156 | extern void kvmppc_map_magic(struct kvm_vcpu *vcpu); |
c30f8a6c | 157 | |
32fad281 PM |
158 | extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp); |
159 | extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp); | |
de56a948 PM |
160 | extern void kvmppc_free_hpt(struct kvm *kvm); |
161 | extern long kvmppc_prepare_vrma(struct kvm *kvm, | |
162 | struct kvm_userspace_memory_region *mem); | |
c77162de | 163 | extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu, |
da9d1d7f | 164 | struct kvm_memory_slot *memslot, unsigned long porder); |
a8606e20 | 165 | extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu); |
bc5ad3f3 | 166 | |
54738c09 | 167 | extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, |
58ded420 | 168 | struct kvm_create_spapr_tce_64 *args); |
d3695aa4 AK |
169 | extern struct kvmppc_spapr_tce_table *kvmppc_find_table( |
170 | struct kvm_vcpu *vcpu, unsigned long liobn); | |
5ee7af18 AK |
171 | extern long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt, |
172 | unsigned long ioba, unsigned long npages); | |
173 | extern long kvmppc_tce_validate(struct kvmppc_spapr_tce_table *tt, | |
174 | unsigned long tce); | |
d3695aa4 AK |
175 | extern long kvmppc_gpa_to_ua(struct kvm *kvm, unsigned long gpa, |
176 | unsigned long *ua, unsigned long **prmap); | |
177 | extern void kvmppc_tce_put(struct kvmppc_spapr_tce_table *tt, | |
178 | unsigned long idx, unsigned long tce); | |
f31e65e1 BH |
179 | extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
180 | unsigned long ioba, unsigned long tce); | |
d3695aa4 AK |
181 | extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu, |
182 | unsigned long liobn, unsigned long ioba, | |
183 | unsigned long tce_list, unsigned long npages); | |
184 | extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu, | |
185 | unsigned long liobn, unsigned long ioba, | |
186 | unsigned long tce_value, unsigned long npages); | |
69e9fbb2 LD |
187 | extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
188 | unsigned long ioba); | |
fa61a4e3 AK |
189 | extern struct page *kvm_alloc_hpt(unsigned long nr_pages); |
190 | extern void kvm_release_hpt(struct page *page, unsigned long nr_pages); | |
f9e0554d PM |
191 | extern int kvmppc_core_init_vm(struct kvm *kvm); |
192 | extern void kvmppc_core_destroy_vm(struct kvm *kvm); | |
5587027c AK |
193 | extern void kvmppc_core_free_memslot(struct kvm *kvm, |
194 | struct kvm_memory_slot *free, | |
a66b48c3 | 195 | struct kvm_memory_slot *dont); |
5587027c AK |
196 | extern int kvmppc_core_create_memslot(struct kvm *kvm, |
197 | struct kvm_memory_slot *slot, | |
a66b48c3 | 198 | unsigned long npages); |
f9e0554d | 199 | extern int kvmppc_core_prepare_memory_region(struct kvm *kvm, |
a66b48c3 | 200 | struct kvm_memory_slot *memslot, |
09170a49 | 201 | const struct kvm_userspace_memory_region *mem); |
f9e0554d | 202 | extern void kvmppc_core_commit_memory_region(struct kvm *kvm, |
09170a49 | 203 | const struct kvm_userspace_memory_region *mem, |
f36f3f28 PB |
204 | const struct kvm_memory_slot *old, |
205 | const struct kvm_memory_slot *new); | |
5b74716e BH |
206 | extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, |
207 | struct kvm_ppc_smmu_info *info); | |
dfe49dbd PM |
208 | extern void kvmppc_core_flush_memslot(struct kvm *kvm, |
209 | struct kvm_memory_slot *memslot); | |
f9e0554d | 210 | |
d30f6e48 SW |
211 | extern int kvmppc_bookehv_init(void); |
212 | extern void kvmppc_bookehv_exit(void); | |
213 | ||
03d25c5b AG |
214 | extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu); |
215 | ||
a2932923 PM |
216 | extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *); |
217 | ||
5df554ad SW |
218 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq); |
219 | ||
8e591cb7 ME |
220 | extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp); |
221 | extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu); | |
222 | extern void kvmppc_rtas_tokens_free(struct kvm *kvm); | |
bc5ad3f3 BH |
223 | extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, |
224 | u32 priority); | |
225 | extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, | |
226 | u32 *priority); | |
d19bd862 PM |
227 | extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq); |
228 | extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq); | |
8e591cb7 | 229 | |
2f699a59 BB |
230 | void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu); |
231 | void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu); | |
232 | ||
3a167bea AK |
233 | union kvmppc_one_reg { |
234 | u32 wval; | |
235 | u64 dval; | |
236 | vector128 vval; | |
237 | u64 vsxval[2]; | |
238 | struct { | |
239 | u64 addr; | |
240 | u64 length; | |
241 | } vpaval; | |
242 | }; | |
243 | ||
244 | struct kvmppc_ops { | |
cbbc58d4 | 245 | struct module *owner; |
3a167bea AK |
246 | int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
247 | int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); | |
248 | int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id, | |
249 | union kvmppc_one_reg *val); | |
250 | int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id, | |
251 | union kvmppc_one_reg *val); | |
252 | void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu); | |
253 | void (*vcpu_put)(struct kvm_vcpu *vcpu); | |
254 | void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr); | |
255 | int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu); | |
256 | struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id); | |
257 | void (*vcpu_free)(struct kvm_vcpu *vcpu); | |
258 | int (*check_requests)(struct kvm_vcpu *vcpu); | |
259 | int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log); | |
260 | void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot); | |
261 | int (*prepare_memory_region)(struct kvm *kvm, | |
262 | struct kvm_memory_slot *memslot, | |
09170a49 | 263 | const struct kvm_userspace_memory_region *mem); |
3a167bea | 264 | void (*commit_memory_region)(struct kvm *kvm, |
09170a49 | 265 | const struct kvm_userspace_memory_region *mem, |
f36f3f28 PB |
266 | const struct kvm_memory_slot *old, |
267 | const struct kvm_memory_slot *new); | |
3a167bea AK |
268 | int (*unmap_hva)(struct kvm *kvm, unsigned long hva); |
269 | int (*unmap_hva_range)(struct kvm *kvm, unsigned long start, | |
270 | unsigned long end); | |
57128468 | 271 | int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end); |
3a167bea AK |
272 | int (*test_age_hva)(struct kvm *kvm, unsigned long hva); |
273 | void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte); | |
274 | void (*mmu_destroy)(struct kvm_vcpu *vcpu); | |
275 | void (*free_memslot)(struct kvm_memory_slot *free, | |
276 | struct kvm_memory_slot *dont); | |
277 | int (*create_memslot)(struct kvm_memory_slot *slot, | |
278 | unsigned long npages); | |
279 | int (*init_vm)(struct kvm *kvm); | |
280 | void (*destroy_vm)(struct kvm *kvm); | |
3a167bea AK |
281 | int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info); |
282 | int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
283 | unsigned int inst, int *advance); | |
284 | int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val); | |
285 | int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val); | |
286 | void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu); | |
287 | long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl, | |
288 | unsigned long arg); | |
ae2113a4 | 289 | int (*hcall_implemented)(unsigned long hcall); |
3a167bea AK |
290 | }; |
291 | ||
cbbc58d4 AK |
292 | extern struct kvmppc_ops *kvmppc_hv_ops; |
293 | extern struct kvmppc_ops *kvmppc_pr_ops; | |
3a167bea | 294 | |
51f04726 MC |
295 | static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu, |
296 | enum instruction_type type, u32 *inst) | |
297 | { | |
298 | int ret = EMULATE_DONE; | |
299 | u32 fetched_inst; | |
300 | ||
301 | /* Load the instruction manually if it failed to do so in the | |
302 | * exit path */ | |
303 | if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) | |
304 | ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst); | |
305 | ||
306 | /* Write fetch_failed unswapped if the fetch failed */ | |
307 | if (ret == EMULATE_DONE) | |
308 | fetched_inst = kvmppc_need_byteswap(vcpu) ? | |
309 | swab32(vcpu->arch.last_inst) : | |
310 | vcpu->arch.last_inst; | |
311 | else | |
312 | fetched_inst = vcpu->arch.last_inst; | |
313 | ||
314 | *inst = fetched_inst; | |
315 | return ret; | |
316 | } | |
317 | ||
a78b55d1 AK |
318 | static inline bool is_kvmppc_hv_enabled(struct kvm *kvm) |
319 | { | |
320 | return kvm->arch.kvm_ops == kvmppc_hv_ops; | |
321 | } | |
322 | ||
e928e9cb ME |
323 | extern int kvmppc_hwrng_present(void); |
324 | ||
0564ee8a AG |
325 | /* |
326 | * Cuts out inst bits with ordering according to spec. | |
327 | * That means the leftmost bit is zero. All given bits are included. | |
328 | */ | |
329 | static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb) | |
330 | { | |
331 | u32 r; | |
332 | u32 mask; | |
333 | ||
334 | BUG_ON(msb > lsb); | |
335 | ||
336 | mask = (1 << (lsb - msb + 1)) - 1; | |
337 | r = (inst >> (63 - lsb)) & mask; | |
338 | ||
339 | return r; | |
340 | } | |
341 | ||
342 | /* | |
343 | * Replaces inst bits with ordering according to spec. | |
344 | */ | |
345 | static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value) | |
346 | { | |
347 | u32 r; | |
348 | u32 mask; | |
349 | ||
350 | BUG_ON(msb > lsb); | |
351 | ||
352 | mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb); | |
353 | r = (inst & ~mask) | ((value << (63 - lsb)) & mask); | |
354 | ||
355 | return r; | |
356 | } | |
357 | ||
a136a8bd PM |
358 | #define one_reg_size(id) \ |
359 | (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT)) | |
360 | ||
361 | #define get_reg_val(id, reg) ({ \ | |
362 | union kvmppc_one_reg __u; \ | |
363 | switch (one_reg_size(id)) { \ | |
364 | case 4: __u.wval = (reg); break; \ | |
365 | case 8: __u.dval = (reg); break; \ | |
366 | default: BUG(); \ | |
367 | } \ | |
368 | __u; \ | |
369 | }) | |
370 | ||
371 | ||
372 | #define set_reg_val(id, val) ({ \ | |
373 | u64 __v; \ | |
374 | switch (one_reg_size(id)) { \ | |
375 | case 4: __v = (val).wval; break; \ | |
376 | case 8: __v = (val).dval; break; \ | |
377 | default: BUG(); \ | |
378 | } \ | |
379 | __v; \ | |
380 | }) | |
381 | ||
3a167bea | 382 | int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
5ce941ee SW |
383 | int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
384 | ||
3a167bea | 385 | int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
5ce941ee SW |
386 | int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
387 | ||
31f3438e PM |
388 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg); |
389 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg); | |
a136a8bd PM |
390 | int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *); |
391 | int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *); | |
31f3438e | 392 | |
5ce941ee SW |
393 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid); |
394 | ||
5df554ad | 395 | struct openpic; |
5df554ad | 396 | |
9975f5e3 | 397 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
fa61a4e3 | 398 | extern void kvm_cma_reserve(void) __init; |
371fefd6 PM |
399 | static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr) |
400 | { | |
401 | paca[cpu].kvm_hstate.xics_phys = addr; | |
402 | } | |
aa04b4cc | 403 | |
54695c30 BH |
404 | static inline u32 kvmppc_get_xics_latch(void) |
405 | { | |
699cc876 | 406 | u32 xirr; |
54695c30 | 407 | |
699cc876 | 408 | xirr = get_paca()->kvm_hstate.saved_xirr; |
54695c30 | 409 | get_paca()->kvm_hstate.saved_xirr = 0; |
54695c30 BH |
410 | return xirr; |
411 | } | |
412 | ||
413 | static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi) | |
414 | { | |
415 | paca[cpu].kvm_hstate.host_ipi = host_ipi; | |
416 | } | |
417 | ||
3a167bea AK |
418 | static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu) |
419 | { | |
cbbc58d4 | 420 | vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu); |
3a167bea | 421 | } |
aa04b4cc | 422 | |
441c19c8 ME |
423 | extern void kvm_hv_vm_activated(void); |
424 | extern void kvm_hv_vm_deactivated(void); | |
425 | extern bool kvm_hv_mode_active(void); | |
426 | ||
371fefd6 | 427 | #else |
fa61a4e3 AK |
428 | static inline void __init kvm_cma_reserve(void) |
429 | {} | |
430 | ||
371fefd6 PM |
431 | static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr) |
432 | {} | |
aa04b4cc | 433 | |
54695c30 BH |
434 | static inline u32 kvmppc_get_xics_latch(void) |
435 | { | |
436 | return 0; | |
437 | } | |
438 | ||
439 | static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi) | |
440 | {} | |
441 | ||
442 | static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu) | |
443 | { | |
444 | kvm_vcpu_kick(vcpu); | |
445 | } | |
441c19c8 ME |
446 | |
447 | static inline bool kvm_hv_mode_active(void) { return false; } | |
448 | ||
bc5ad3f3 BH |
449 | #endif |
450 | ||
451 | #ifdef CONFIG_KVM_XICS | |
452 | static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu) | |
453 | { | |
454 | return vcpu->arch.irq_type == KVMPPC_IRQ_XICS; | |
455 | } | |
79b6c247 SW |
456 | extern void kvmppc_alloc_host_rm_ops(void); |
457 | extern void kvmppc_free_host_rm_ops(void); | |
bc5ad3f3 BH |
458 | extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu); |
459 | extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server); | |
460 | extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args); | |
461 | extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd); | |
8b78645c PM |
462 | extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu); |
463 | extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval); | |
5975a2e0 PM |
464 | extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev, |
465 | struct kvm_vcpu *vcpu, u32 cpu); | |
0c2a6606 | 466 | extern void kvmppc_xics_ipi_action(void); |
520fe9c6 | 467 | extern int h_ipi_redirect; |
bc5ad3f3 | 468 | #else |
79b6c247 SW |
469 | static inline void kvmppc_alloc_host_rm_ops(void) {}; |
470 | static inline void kvmppc_free_host_rm_ops(void) {}; | |
bc5ad3f3 BH |
471 | static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu) |
472 | { return 0; } | |
473 | static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { } | |
474 | static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, | |
475 | unsigned long server) | |
476 | { return -EINVAL; } | |
477 | static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm, | |
478 | struct kvm_irq_level *args) | |
479 | { return -ENOTTY; } | |
480 | static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd) | |
481 | { return 0; } | |
371fefd6 PM |
482 | #endif |
483 | ||
79b6c247 SW |
484 | /* |
485 | * Host-side operations we want to set up while running in real | |
486 | * mode in the guest operating on the xics. | |
487 | * Currently only VCPU wakeup is supported. | |
488 | */ | |
489 | ||
490 | union kvmppc_rm_state { | |
491 | unsigned long raw; | |
492 | struct { | |
493 | u32 in_host; | |
494 | u32 rm_action; | |
495 | }; | |
496 | }; | |
497 | ||
498 | struct kvmppc_host_rm_core { | |
499 | union kvmppc_rm_state rm_state; | |
500 | void *rm_data; | |
501 | char pad[112]; | |
502 | }; | |
503 | ||
504 | struct kvmppc_host_rm_ops { | |
505 | struct kvmppc_host_rm_core *rm_core; | |
506 | void (*vcpu_kick)(struct kvm_vcpu *vcpu); | |
507 | }; | |
508 | ||
509 | extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv; | |
510 | ||
34f754b9 BB |
511 | static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu) |
512 | { | |
513 | #ifdef CONFIG_KVM_BOOKE_HV | |
514 | return mfspr(SPRN_GEPR); | |
515 | #elif defined(CONFIG_BOOKE) | |
516 | return vcpu->arch.epr; | |
517 | #else | |
518 | return 0; | |
519 | #endif | |
520 | } | |
521 | ||
1c810636 AG |
522 | static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr) |
523 | { | |
524 | #ifdef CONFIG_KVM_BOOKE_HV | |
525 | mtspr(SPRN_GEPR, epr); | |
526 | #elif defined(CONFIG_BOOKE) | |
527 | vcpu->arch.epr = epr; | |
528 | #endif | |
529 | } | |
530 | ||
5df554ad SW |
531 | #ifdef CONFIG_KVM_MPIC |
532 | ||
533 | void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu); | |
eb1e4f43 SW |
534 | int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu, |
535 | u32 cpu); | |
536 | void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu); | |
5df554ad SW |
537 | |
538 | #else | |
539 | ||
540 | static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu) | |
541 | { | |
542 | } | |
543 | ||
eb1e4f43 SW |
544 | static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, |
545 | struct kvm_vcpu *vcpu, u32 cpu) | |
546 | { | |
547 | return -EINVAL; | |
548 | } | |
549 | ||
550 | static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, | |
551 | struct kvm_vcpu *vcpu) | |
552 | { | |
553 | } | |
554 | ||
5df554ad SW |
555 | #endif /* CONFIG_KVM_MPIC */ |
556 | ||
dc83b8bc SW |
557 | int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu, |
558 | struct kvm_config_tlb *cfg); | |
559 | int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu, | |
560 | struct kvm_dirty_tlb *cfg); | |
561 | ||
043cc4d7 SW |
562 | long kvmppc_alloc_lpid(void); |
563 | void kvmppc_claim_lpid(long lpid); | |
564 | void kvmppc_free_lpid(long lpid); | |
565 | void kvmppc_init_lpid(unsigned long nr_lpids); | |
566 | ||
ba049e93 | 567 | static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn) |
249ba1ee | 568 | { |
249ba1ee | 569 | struct page *page; |
adccf65c BB |
570 | /* |
571 | * We can only access pages that the kernel maps | |
572 | * as memory. Bail out for unmapped ones. | |
573 | */ | |
574 | if (!pfn_valid(pfn)) | |
575 | return; | |
576 | ||
577 | /* Clear i-cache for new pages */ | |
249ba1ee AG |
578 | page = pfn_to_page(pfn); |
579 | if (!test_bit(PG_arch_1, &page->flags)) { | |
580 | flush_dcache_icache_page(page); | |
581 | set_bit(PG_arch_1, &page->flags); | |
582 | } | |
583 | } | |
584 | ||
5deb8e7a AG |
585 | /* |
586 | * Shared struct helpers. The shared struct can be little or big endian, | |
587 | * depending on the guest endianness. So expose helpers to all of them. | |
588 | */ | |
589 | static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu) | |
590 | { | |
591 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) | |
592 | /* Only Book3S_64 PR supports bi-endian for now */ | |
593 | return vcpu->arch.shared_big_endian; | |
594 | #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__) | |
595 | /* Book3s_64 HV on little endian is always little endian */ | |
596 | return false; | |
597 | #else | |
598 | return true; | |
599 | #endif | |
600 | } | |
601 | ||
5a484c7c | 602 | #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \ |
1dc0c5b8 BB |
603 | static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \ |
604 | { \ | |
5a484c7c | 605 | return mfspr(bookehv_spr); \ |
1dc0c5b8 BB |
606 | } \ |
607 | ||
5a484c7c | 608 | #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \ |
1dc0c5b8 BB |
609 | static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \ |
610 | { \ | |
5a484c7c | 611 | mtspr(bookehv_spr, val); \ |
1dc0c5b8 BB |
612 | } \ |
613 | ||
5deb8e7a | 614 | #define SHARED_WRAPPER_GET(reg, size) \ |
1dc0c5b8 | 615 | static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \ |
5deb8e7a AG |
616 | { \ |
617 | if (kvmppc_shared_big_endian(vcpu)) \ | |
618 | return be##size##_to_cpu(vcpu->arch.shared->reg); \ | |
619 | else \ | |
620 | return le##size##_to_cpu(vcpu->arch.shared->reg); \ | |
621 | } \ | |
622 | ||
623 | #define SHARED_WRAPPER_SET(reg, size) \ | |
624 | static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \ | |
625 | { \ | |
626 | if (kvmppc_shared_big_endian(vcpu)) \ | |
627 | vcpu->arch.shared->reg = cpu_to_be##size(val); \ | |
628 | else \ | |
629 | vcpu->arch.shared->reg = cpu_to_le##size(val); \ | |
630 | } \ | |
631 | ||
632 | #define SHARED_WRAPPER(reg, size) \ | |
633 | SHARED_WRAPPER_GET(reg, size) \ | |
634 | SHARED_WRAPPER_SET(reg, size) \ | |
635 | ||
5a484c7c BB |
636 | #define SPRNG_WRAPPER(reg, bookehv_spr) \ |
637 | SPRNG_WRAPPER_GET(reg, bookehv_spr) \ | |
638 | SPRNG_WRAPPER_SET(reg, bookehv_spr) \ | |
1dc0c5b8 BB |
639 | |
640 | #ifdef CONFIG_KVM_BOOKE_HV | |
641 | ||
5a484c7c BB |
642 | #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \ |
643 | SPRNG_WRAPPER(reg, bookehv_spr) \ | |
1dc0c5b8 BB |
644 | |
645 | #else | |
646 | ||
5a484c7c | 647 | #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \ |
1dc0c5b8 BB |
648 | SHARED_WRAPPER(reg, size) \ |
649 | ||
650 | #endif | |
651 | ||
5deb8e7a | 652 | SHARED_WRAPPER(critical, 64) |
1dc0c5b8 BB |
653 | SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0) |
654 | SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1) | |
655 | SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2) | |
656 | SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3) | |
657 | SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0) | |
658 | SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1) | |
659 | SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR) | |
dc168549 | 660 | SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR) |
5deb8e7a AG |
661 | SHARED_WRAPPER_GET(msr, 64) |
662 | static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val) | |
663 | { | |
664 | if (kvmppc_shared_big_endian(vcpu)) | |
665 | vcpu->arch.shared->msr = cpu_to_be64(val); | |
666 | else | |
667 | vcpu->arch.shared->msr = cpu_to_le64(val); | |
668 | } | |
669 | SHARED_WRAPPER(dsisr, 32) | |
670 | SHARED_WRAPPER(int_pending, 32) | |
671 | SHARED_WRAPPER(sprg4, 64) | |
672 | SHARED_WRAPPER(sprg5, 64) | |
673 | SHARED_WRAPPER(sprg6, 64) | |
674 | SHARED_WRAPPER(sprg7, 64) | |
675 | ||
676 | static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr) | |
677 | { | |
678 | if (kvmppc_shared_big_endian(vcpu)) | |
679 | return be32_to_cpu(vcpu->arch.shared->sr[nr]); | |
680 | else | |
681 | return le32_to_cpu(vcpu->arch.shared->sr[nr]); | |
682 | } | |
683 | ||
684 | static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val) | |
685 | { | |
686 | if (kvmppc_shared_big_endian(vcpu)) | |
687 | vcpu->arch.shared->sr[nr] = cpu_to_be32(val); | |
688 | else | |
689 | vcpu->arch.shared->sr[nr] = cpu_to_le32(val); | |
690 | } | |
691 | ||
5f1c248f SW |
692 | /* |
693 | * Please call after prepare_to_enter. This function puts the lazy ee and irq | |
694 | * disabled tracking state back to normal mode, without actually enabling | |
695 | * interrupts. | |
696 | */ | |
697 | static inline void kvmppc_fix_ee_before_entry(void) | |
bd2be683 | 698 | { |
5f1c248f SW |
699 | trace_hardirqs_on(); |
700 | ||
bd2be683 | 701 | #ifdef CONFIG_PPC64 |
6c85f52b SW |
702 | /* |
703 | * To avoid races, the caller must have gone directly from having | |
704 | * interrupts fully-enabled to hard-disabled. | |
705 | */ | |
706 | WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS); | |
707 | ||
bd2be683 AG |
708 | /* Only need to enable IRQs by hard enabling them after this */ |
709 | local_paca->irq_happened = 0; | |
710 | local_paca->soft_enabled = 1; | |
711 | #endif | |
712 | } | |
249ba1ee | 713 | |
7cdd7a95 MC |
714 | static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb) |
715 | { | |
716 | ulong ea; | |
8823a8fd | 717 | ulong msr_64bit = 0; |
7cdd7a95 MC |
718 | |
719 | ea = kvmppc_get_gpr(vcpu, rb); | |
720 | if (ra) | |
721 | ea += kvmppc_get_gpr(vcpu, ra); | |
722 | ||
8823a8fd MC |
723 | #if defined(CONFIG_PPC_BOOK3E_64) |
724 | msr_64bit = MSR_CM; | |
725 | #elif defined(CONFIG_PPC_BOOK3S_64) | |
726 | msr_64bit = MSR_SF; | |
727 | #endif | |
728 | ||
5deb8e7a | 729 | if (!(kvmppc_get_msr(vcpu) & msr_64bit)) |
8823a8fd MC |
730 | ea = (uint32_t)ea; |
731 | ||
7cdd7a95 MC |
732 | return ea; |
733 | } | |
734 | ||
54695c30 BH |
735 | extern void xics_wake_cpu(int cpu); |
736 | ||
bbf45ba5 | 737 | #endif /* __POWERPC_KVM_PPC_H__ */ |