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