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