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