]>
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 DG |
167 | extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, |
168 | struct kvm_create_spapr_tce *args); | |
f31e65e1 BH |
169 | extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
170 | unsigned long ioba, unsigned long tce); | |
69e9fbb2 LD |
171 | extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn, |
172 | unsigned long ioba); | |
fa61a4e3 AK |
173 | extern struct page *kvm_alloc_hpt(unsigned long nr_pages); |
174 | extern void kvm_release_hpt(struct page *page, unsigned long nr_pages); | |
f9e0554d PM |
175 | extern int kvmppc_core_init_vm(struct kvm *kvm); |
176 | extern void kvmppc_core_destroy_vm(struct kvm *kvm); | |
5587027c AK |
177 | extern void kvmppc_core_free_memslot(struct kvm *kvm, |
178 | struct kvm_memory_slot *free, | |
a66b48c3 | 179 | struct kvm_memory_slot *dont); |
5587027c AK |
180 | extern int kvmppc_core_create_memslot(struct kvm *kvm, |
181 | struct kvm_memory_slot *slot, | |
a66b48c3 | 182 | unsigned long npages); |
f9e0554d | 183 | extern int kvmppc_core_prepare_memory_region(struct kvm *kvm, |
a66b48c3 | 184 | struct kvm_memory_slot *memslot, |
f9e0554d PM |
185 | struct kvm_userspace_memory_region *mem); |
186 | extern void kvmppc_core_commit_memory_region(struct kvm *kvm, | |
dfe49dbd | 187 | struct kvm_userspace_memory_region *mem, |
8482644a | 188 | const struct kvm_memory_slot *old); |
5b74716e BH |
189 | extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, |
190 | struct kvm_ppc_smmu_info *info); | |
dfe49dbd PM |
191 | extern void kvmppc_core_flush_memslot(struct kvm *kvm, |
192 | struct kvm_memory_slot *memslot); | |
f9e0554d | 193 | |
d30f6e48 SW |
194 | extern int kvmppc_bookehv_init(void); |
195 | extern void kvmppc_bookehv_exit(void); | |
196 | ||
03d25c5b AG |
197 | extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu); |
198 | ||
a2932923 PM |
199 | extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *); |
200 | ||
5df554ad SW |
201 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq); |
202 | ||
8e591cb7 ME |
203 | extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp); |
204 | extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu); | |
205 | extern void kvmppc_rtas_tokens_free(struct kvm *kvm); | |
bc5ad3f3 BH |
206 | extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server, |
207 | u32 priority); | |
208 | extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server, | |
209 | u32 *priority); | |
d19bd862 PM |
210 | extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq); |
211 | extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq); | |
8e591cb7 | 212 | |
2f699a59 BB |
213 | void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu); |
214 | void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu); | |
215 | ||
3a167bea AK |
216 | union kvmppc_one_reg { |
217 | u32 wval; | |
218 | u64 dval; | |
219 | vector128 vval; | |
220 | u64 vsxval[2]; | |
221 | struct { | |
222 | u64 addr; | |
223 | u64 length; | |
224 | } vpaval; | |
225 | }; | |
226 | ||
227 | struct kvmppc_ops { | |
cbbc58d4 | 228 | struct module *owner; |
3a167bea AK |
229 | int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
230 | int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); | |
231 | int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id, | |
232 | union kvmppc_one_reg *val); | |
233 | int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id, | |
234 | union kvmppc_one_reg *val); | |
235 | void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu); | |
236 | void (*vcpu_put)(struct kvm_vcpu *vcpu); | |
237 | void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr); | |
238 | int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu); | |
239 | struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id); | |
240 | void (*vcpu_free)(struct kvm_vcpu *vcpu); | |
241 | int (*check_requests)(struct kvm_vcpu *vcpu); | |
242 | int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log); | |
243 | void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot); | |
244 | int (*prepare_memory_region)(struct kvm *kvm, | |
245 | struct kvm_memory_slot *memslot, | |
246 | struct kvm_userspace_memory_region *mem); | |
247 | void (*commit_memory_region)(struct kvm *kvm, | |
248 | struct kvm_userspace_memory_region *mem, | |
249 | const struct kvm_memory_slot *old); | |
250 | int (*unmap_hva)(struct kvm *kvm, unsigned long hva); | |
251 | int (*unmap_hva_range)(struct kvm *kvm, unsigned long start, | |
252 | unsigned long end); | |
57128468 | 253 | int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end); |
3a167bea AK |
254 | int (*test_age_hva)(struct kvm *kvm, unsigned long hva); |
255 | void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte); | |
256 | void (*mmu_destroy)(struct kvm_vcpu *vcpu); | |
257 | void (*free_memslot)(struct kvm_memory_slot *free, | |
258 | struct kvm_memory_slot *dont); | |
259 | int (*create_memslot)(struct kvm_memory_slot *slot, | |
260 | unsigned long npages); | |
261 | int (*init_vm)(struct kvm *kvm); | |
262 | void (*destroy_vm)(struct kvm *kvm); | |
3a167bea AK |
263 | int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info); |
264 | int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
265 | unsigned int inst, int *advance); | |
266 | int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val); | |
267 | int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val); | |
268 | void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu); | |
269 | long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl, | |
270 | unsigned long arg); | |
ae2113a4 | 271 | int (*hcall_implemented)(unsigned long hcall); |
3a167bea AK |
272 | }; |
273 | ||
cbbc58d4 AK |
274 | extern struct kvmppc_ops *kvmppc_hv_ops; |
275 | extern struct kvmppc_ops *kvmppc_pr_ops; | |
3a167bea | 276 | |
51f04726 MC |
277 | static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu, |
278 | enum instruction_type type, u32 *inst) | |
279 | { | |
280 | int ret = EMULATE_DONE; | |
281 | u32 fetched_inst; | |
282 | ||
283 | /* Load the instruction manually if it failed to do so in the | |
284 | * exit path */ | |
285 | if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) | |
286 | ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst); | |
287 | ||
288 | /* Write fetch_failed unswapped if the fetch failed */ | |
289 | if (ret == EMULATE_DONE) | |
290 | fetched_inst = kvmppc_need_byteswap(vcpu) ? | |
291 | swab32(vcpu->arch.last_inst) : | |
292 | vcpu->arch.last_inst; | |
293 | else | |
294 | fetched_inst = vcpu->arch.last_inst; | |
295 | ||
296 | *inst = fetched_inst; | |
297 | return ret; | |
298 | } | |
299 | ||
a78b55d1 AK |
300 | static inline bool is_kvmppc_hv_enabled(struct kvm *kvm) |
301 | { | |
302 | return kvm->arch.kvm_ops == kvmppc_hv_ops; | |
303 | } | |
304 | ||
e928e9cb ME |
305 | extern int kvmppc_hwrng_present(void); |
306 | ||
0564ee8a AG |
307 | /* |
308 | * Cuts out inst bits with ordering according to spec. | |
309 | * That means the leftmost bit is zero. All given bits are included. | |
310 | */ | |
311 | static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb) | |
312 | { | |
313 | u32 r; | |
314 | u32 mask; | |
315 | ||
316 | BUG_ON(msb > lsb); | |
317 | ||
318 | mask = (1 << (lsb - msb + 1)) - 1; | |
319 | r = (inst >> (63 - lsb)) & mask; | |
320 | ||
321 | return r; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Replaces inst bits with ordering according to spec. | |
326 | */ | |
327 | static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value) | |
328 | { | |
329 | u32 r; | |
330 | u32 mask; | |
331 | ||
332 | BUG_ON(msb > lsb); | |
333 | ||
334 | mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb); | |
335 | r = (inst & ~mask) | ((value << (63 - lsb)) & mask); | |
336 | ||
337 | return r; | |
338 | } | |
339 | ||
a136a8bd PM |
340 | #define one_reg_size(id) \ |
341 | (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT)) | |
342 | ||
343 | #define get_reg_val(id, reg) ({ \ | |
344 | union kvmppc_one_reg __u; \ | |
345 | switch (one_reg_size(id)) { \ | |
346 | case 4: __u.wval = (reg); break; \ | |
347 | case 8: __u.dval = (reg); break; \ | |
348 | default: BUG(); \ | |
349 | } \ | |
350 | __u; \ | |
351 | }) | |
352 | ||
353 | ||
354 | #define set_reg_val(id, val) ({ \ | |
355 | u64 __v; \ | |
356 | switch (one_reg_size(id)) { \ | |
357 | case 4: __v = (val).wval; break; \ | |
358 | case 8: __v = (val).dval; break; \ | |
359 | default: BUG(); \ | |
360 | } \ | |
361 | __v; \ | |
362 | }) | |
363 | ||
3a167bea | 364 | int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
5ce941ee SW |
365 | int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
366 | ||
3a167bea | 367 | int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
5ce941ee SW |
368 | int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs); |
369 | ||
31f3438e PM |
370 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg); |
371 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg); | |
a136a8bd PM |
372 | int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *); |
373 | int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *); | |
31f3438e | 374 | |
5ce941ee SW |
375 | void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid); |
376 | ||
5df554ad | 377 | struct openpic; |
5df554ad | 378 | |
9975f5e3 | 379 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
fa61a4e3 | 380 | extern void kvm_cma_reserve(void) __init; |
371fefd6 PM |
381 | static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr) |
382 | { | |
383 | paca[cpu].kvm_hstate.xics_phys = addr; | |
384 | } | |
aa04b4cc | 385 | |
54695c30 BH |
386 | static inline u32 kvmppc_get_xics_latch(void) |
387 | { | |
699cc876 | 388 | u32 xirr; |
54695c30 | 389 | |
699cc876 | 390 | xirr = get_paca()->kvm_hstate.saved_xirr; |
54695c30 | 391 | get_paca()->kvm_hstate.saved_xirr = 0; |
54695c30 BH |
392 | return xirr; |
393 | } | |
394 | ||
395 | static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi) | |
396 | { | |
397 | paca[cpu].kvm_hstate.host_ipi = host_ipi; | |
398 | } | |
399 | ||
3a167bea AK |
400 | static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu) |
401 | { | |
cbbc58d4 | 402 | vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu); |
3a167bea | 403 | } |
aa04b4cc | 404 | |
441c19c8 ME |
405 | extern void kvm_hv_vm_activated(void); |
406 | extern void kvm_hv_vm_deactivated(void); | |
407 | extern bool kvm_hv_mode_active(void); | |
408 | ||
371fefd6 | 409 | #else |
fa61a4e3 AK |
410 | static inline void __init kvm_cma_reserve(void) |
411 | {} | |
412 | ||
371fefd6 PM |
413 | static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr) |
414 | {} | |
aa04b4cc | 415 | |
54695c30 BH |
416 | static inline u32 kvmppc_get_xics_latch(void) |
417 | { | |
418 | return 0; | |
419 | } | |
420 | ||
421 | static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi) | |
422 | {} | |
423 | ||
424 | static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu) | |
425 | { | |
426 | kvm_vcpu_kick(vcpu); | |
427 | } | |
441c19c8 ME |
428 | |
429 | static inline bool kvm_hv_mode_active(void) { return false; } | |
430 | ||
bc5ad3f3 BH |
431 | #endif |
432 | ||
433 | #ifdef CONFIG_KVM_XICS | |
434 | static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu) | |
435 | { | |
436 | return vcpu->arch.irq_type == KVMPPC_IRQ_XICS; | |
437 | } | |
438 | extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu); | |
439 | extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server); | |
440 | extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args); | |
441 | extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd); | |
8b78645c PM |
442 | extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu); |
443 | extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval); | |
5975a2e0 PM |
444 | extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev, |
445 | struct kvm_vcpu *vcpu, u32 cpu); | |
bc5ad3f3 BH |
446 | #else |
447 | static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu) | |
448 | { return 0; } | |
449 | static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { } | |
450 | static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, | |
451 | unsigned long server) | |
452 | { return -EINVAL; } | |
453 | static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm, | |
454 | struct kvm_irq_level *args) | |
455 | { return -ENOTTY; } | |
456 | static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd) | |
457 | { return 0; } | |
371fefd6 PM |
458 | #endif |
459 | ||
34f754b9 BB |
460 | static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu) |
461 | { | |
462 | #ifdef CONFIG_KVM_BOOKE_HV | |
463 | return mfspr(SPRN_GEPR); | |
464 | #elif defined(CONFIG_BOOKE) | |
465 | return vcpu->arch.epr; | |
466 | #else | |
467 | return 0; | |
468 | #endif | |
469 | } | |
470 | ||
1c810636 AG |
471 | static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr) |
472 | { | |
473 | #ifdef CONFIG_KVM_BOOKE_HV | |
474 | mtspr(SPRN_GEPR, epr); | |
475 | #elif defined(CONFIG_BOOKE) | |
476 | vcpu->arch.epr = epr; | |
477 | #endif | |
478 | } | |
479 | ||
5df554ad SW |
480 | #ifdef CONFIG_KVM_MPIC |
481 | ||
482 | void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu); | |
eb1e4f43 SW |
483 | int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu, |
484 | u32 cpu); | |
485 | void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu); | |
5df554ad SW |
486 | |
487 | #else | |
488 | ||
489 | static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu) | |
490 | { | |
491 | } | |
492 | ||
eb1e4f43 SW |
493 | static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, |
494 | struct kvm_vcpu *vcpu, u32 cpu) | |
495 | { | |
496 | return -EINVAL; | |
497 | } | |
498 | ||
499 | static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, | |
500 | struct kvm_vcpu *vcpu) | |
501 | { | |
502 | } | |
503 | ||
5df554ad SW |
504 | #endif /* CONFIG_KVM_MPIC */ |
505 | ||
dc83b8bc SW |
506 | int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu, |
507 | struct kvm_config_tlb *cfg); | |
508 | int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu, | |
509 | struct kvm_dirty_tlb *cfg); | |
510 | ||
043cc4d7 SW |
511 | long kvmppc_alloc_lpid(void); |
512 | void kvmppc_claim_lpid(long lpid); | |
513 | void kvmppc_free_lpid(long lpid); | |
514 | void kvmppc_init_lpid(unsigned long nr_lpids); | |
515 | ||
249ba1ee AG |
516 | static inline void kvmppc_mmu_flush_icache(pfn_t pfn) |
517 | { | |
249ba1ee | 518 | struct page *page; |
adccf65c BB |
519 | /* |
520 | * We can only access pages that the kernel maps | |
521 | * as memory. Bail out for unmapped ones. | |
522 | */ | |
523 | if (!pfn_valid(pfn)) | |
524 | return; | |
525 | ||
526 | /* Clear i-cache for new pages */ | |
249ba1ee AG |
527 | page = pfn_to_page(pfn); |
528 | if (!test_bit(PG_arch_1, &page->flags)) { | |
529 | flush_dcache_icache_page(page); | |
530 | set_bit(PG_arch_1, &page->flags); | |
531 | } | |
532 | } | |
533 | ||
5deb8e7a AG |
534 | /* |
535 | * Shared struct helpers. The shared struct can be little or big endian, | |
536 | * depending on the guest endianness. So expose helpers to all of them. | |
537 | */ | |
538 | static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu) | |
539 | { | |
540 | #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE) | |
541 | /* Only Book3S_64 PR supports bi-endian for now */ | |
542 | return vcpu->arch.shared_big_endian; | |
543 | #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__) | |
544 | /* Book3s_64 HV on little endian is always little endian */ | |
545 | return false; | |
546 | #else | |
547 | return true; | |
548 | #endif | |
549 | } | |
550 | ||
5a484c7c | 551 | #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \ |
1dc0c5b8 BB |
552 | static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \ |
553 | { \ | |
5a484c7c | 554 | return mfspr(bookehv_spr); \ |
1dc0c5b8 BB |
555 | } \ |
556 | ||
5a484c7c | 557 | #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \ |
1dc0c5b8 BB |
558 | static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \ |
559 | { \ | |
5a484c7c | 560 | mtspr(bookehv_spr, val); \ |
1dc0c5b8 BB |
561 | } \ |
562 | ||
5deb8e7a | 563 | #define SHARED_WRAPPER_GET(reg, size) \ |
1dc0c5b8 | 564 | static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \ |
5deb8e7a AG |
565 | { \ |
566 | if (kvmppc_shared_big_endian(vcpu)) \ | |
567 | return be##size##_to_cpu(vcpu->arch.shared->reg); \ | |
568 | else \ | |
569 | return le##size##_to_cpu(vcpu->arch.shared->reg); \ | |
570 | } \ | |
571 | ||
572 | #define SHARED_WRAPPER_SET(reg, size) \ | |
573 | static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \ | |
574 | { \ | |
575 | if (kvmppc_shared_big_endian(vcpu)) \ | |
576 | vcpu->arch.shared->reg = cpu_to_be##size(val); \ | |
577 | else \ | |
578 | vcpu->arch.shared->reg = cpu_to_le##size(val); \ | |
579 | } \ | |
580 | ||
581 | #define SHARED_WRAPPER(reg, size) \ | |
582 | SHARED_WRAPPER_GET(reg, size) \ | |
583 | SHARED_WRAPPER_SET(reg, size) \ | |
584 | ||
5a484c7c BB |
585 | #define SPRNG_WRAPPER(reg, bookehv_spr) \ |
586 | SPRNG_WRAPPER_GET(reg, bookehv_spr) \ | |
587 | SPRNG_WRAPPER_SET(reg, bookehv_spr) \ | |
1dc0c5b8 BB |
588 | |
589 | #ifdef CONFIG_KVM_BOOKE_HV | |
590 | ||
5a484c7c BB |
591 | #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \ |
592 | SPRNG_WRAPPER(reg, bookehv_spr) \ | |
1dc0c5b8 BB |
593 | |
594 | #else | |
595 | ||
5a484c7c | 596 | #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \ |
1dc0c5b8 BB |
597 | SHARED_WRAPPER(reg, size) \ |
598 | ||
599 | #endif | |
600 | ||
5deb8e7a | 601 | SHARED_WRAPPER(critical, 64) |
1dc0c5b8 BB |
602 | SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0) |
603 | SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1) | |
604 | SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2) | |
605 | SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3) | |
606 | SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0) | |
607 | SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1) | |
608 | SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR) | |
dc168549 | 609 | SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR) |
5deb8e7a AG |
610 | SHARED_WRAPPER_GET(msr, 64) |
611 | static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val) | |
612 | { | |
613 | if (kvmppc_shared_big_endian(vcpu)) | |
614 | vcpu->arch.shared->msr = cpu_to_be64(val); | |
615 | else | |
616 | vcpu->arch.shared->msr = cpu_to_le64(val); | |
617 | } | |
618 | SHARED_WRAPPER(dsisr, 32) | |
619 | SHARED_WRAPPER(int_pending, 32) | |
620 | SHARED_WRAPPER(sprg4, 64) | |
621 | SHARED_WRAPPER(sprg5, 64) | |
622 | SHARED_WRAPPER(sprg6, 64) | |
623 | SHARED_WRAPPER(sprg7, 64) | |
624 | ||
625 | static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr) | |
626 | { | |
627 | if (kvmppc_shared_big_endian(vcpu)) | |
628 | return be32_to_cpu(vcpu->arch.shared->sr[nr]); | |
629 | else | |
630 | return le32_to_cpu(vcpu->arch.shared->sr[nr]); | |
631 | } | |
632 | ||
633 | static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val) | |
634 | { | |
635 | if (kvmppc_shared_big_endian(vcpu)) | |
636 | vcpu->arch.shared->sr[nr] = cpu_to_be32(val); | |
637 | else | |
638 | vcpu->arch.shared->sr[nr] = cpu_to_le32(val); | |
639 | } | |
640 | ||
5f1c248f SW |
641 | /* |
642 | * Please call after prepare_to_enter. This function puts the lazy ee and irq | |
643 | * disabled tracking state back to normal mode, without actually enabling | |
644 | * interrupts. | |
645 | */ | |
646 | static inline void kvmppc_fix_ee_before_entry(void) | |
bd2be683 | 647 | { |
5f1c248f SW |
648 | trace_hardirqs_on(); |
649 | ||
bd2be683 | 650 | #ifdef CONFIG_PPC64 |
6c85f52b SW |
651 | /* |
652 | * To avoid races, the caller must have gone directly from having | |
653 | * interrupts fully-enabled to hard-disabled. | |
654 | */ | |
655 | WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS); | |
656 | ||
bd2be683 AG |
657 | /* Only need to enable IRQs by hard enabling them after this */ |
658 | local_paca->irq_happened = 0; | |
659 | local_paca->soft_enabled = 1; | |
660 | #endif | |
661 | } | |
249ba1ee | 662 | |
7cdd7a95 MC |
663 | static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb) |
664 | { | |
665 | ulong ea; | |
8823a8fd | 666 | ulong msr_64bit = 0; |
7cdd7a95 MC |
667 | |
668 | ea = kvmppc_get_gpr(vcpu, rb); | |
669 | if (ra) | |
670 | ea += kvmppc_get_gpr(vcpu, ra); | |
671 | ||
8823a8fd MC |
672 | #if defined(CONFIG_PPC_BOOK3E_64) |
673 | msr_64bit = MSR_CM; | |
674 | #elif defined(CONFIG_PPC_BOOK3S_64) | |
675 | msr_64bit = MSR_SF; | |
676 | #endif | |
677 | ||
5deb8e7a | 678 | if (!(kvmppc_get_msr(vcpu) & msr_64bit)) |
8823a8fd MC |
679 | ea = (uint32_t)ea; |
680 | ||
7cdd7a95 MC |
681 | return ea; |
682 | } | |
683 | ||
54695c30 BH |
684 | extern void xics_wake_cpu(int cpu); |
685 | ||
bbf45ba5 | 686 | #endif /* __POWERPC_KVM_PPC_H__ */ |