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