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.
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.
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.
15 * Copyright IBM Corp. 2008
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
20 #ifndef __POWERPC_KVM_PPC_H__
21 #define __POWERPC_KVM_PPC_H__
23 /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
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>
35 #include <asm/kvm_booke.h>
37 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
42 * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
43 * for supporting software breakpoint.
45 #define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00
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 */
55 enum instruction_fetch_type {
57 INST_SC, /* system call */
61 XLATE_INST, /* translate instruction address */
62 XLATE_DATA /* translate data address */
65 enum xlate_readwrite {
66 XLATE_READ, /* check for read permissions */
67 XLATE_WRITE /* check for write permissions */
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);
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_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
82 unsigned int rt, unsigned int bytes,
83 int is_default_endian, int mmio_sign_extend);
84 extern int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
85 unsigned int rt, unsigned int bytes, int is_default_endian);
86 extern int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
87 unsigned int rs, unsigned int bytes, int is_default_endian);
88 extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
89 u64 val, unsigned int bytes,
90 int is_default_endian);
91 extern int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
92 int rs, unsigned int bytes,
93 int is_default_endian);
95 extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
96 enum instruction_fetch_type type, u32 *inst);
98 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
100 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
102 extern int kvmppc_emulate_instruction(struct kvm_run *run,
103 struct kvm_vcpu *vcpu);
104 extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
105 extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
106 extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
107 extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
108 extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
109 extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
110 extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
111 extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
113 /* Core-specific hooks */
115 extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
116 unsigned int gtlb_idx);
117 extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
118 extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
119 extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
120 extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
121 extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
122 extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
123 extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
125 extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
126 extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
127 extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
128 enum xlate_instdata xlid, enum xlate_readwrite xlrw,
129 struct kvmppc_pte *pte);
131 extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
133 extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
134 extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
135 extern int kvmppc_core_check_processor_compat(void);
136 extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
137 struct kvm_translation *tr);
139 extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
140 extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
142 extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
143 extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
144 extern void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu, ulong flags);
145 extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
146 extern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu);
147 extern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu);
148 extern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu);
149 extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
150 extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
151 extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
152 struct kvm_interrupt *irq);
153 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
154 extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
156 extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
159 extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
160 extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
162 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
163 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
165 extern int kvmppc_booke_init(void);
166 extern void kvmppc_booke_exit(void);
168 extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
169 extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
170 extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
172 extern int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order);
173 extern void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info);
174 extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order);
175 extern void kvmppc_free_hpt(struct kvm_hpt_info *info);
176 extern void kvmppc_rmap_reset(struct kvm *kvm);
177 extern long kvmppc_prepare_vrma(struct kvm *kvm,
178 struct kvm_userspace_memory_region *mem);
179 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
180 struct kvm_memory_slot *memslot, unsigned long porder);
181 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
182 extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
183 struct iommu_group *grp);
184 extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
185 struct iommu_group *grp);
186 extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
187 extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
188 extern void kvmppc_setup_partition_table(struct kvm *kvm);
190 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
191 struct kvm_create_spapr_tce_64 *args);
192 extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
193 struct kvm *kvm, unsigned long liobn);
194 #define kvmppc_ioba_validate(stt, ioba, npages) \
195 (iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
196 (stt)->size, (ioba), (npages)) ? \
197 H_PARAMETER : H_SUCCESS)
198 extern long kvmppc_tce_to_ua(struct kvm *kvm, unsigned long tce,
199 unsigned long *ua, unsigned long **prmap);
200 extern void kvmppc_tce_put(struct kvmppc_spapr_tce_table *tt,
201 unsigned long idx, unsigned long tce);
202 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
203 unsigned long ioba, unsigned long tce);
204 extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
205 unsigned long liobn, unsigned long ioba,
206 unsigned long tce_list, unsigned long npages);
207 extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
208 unsigned long liobn, unsigned long ioba,
209 unsigned long tce_value, unsigned long npages);
210 extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
212 extern struct page *kvm_alloc_hpt_cma(unsigned long nr_pages);
213 extern void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages);
214 extern int kvmppc_core_init_vm(struct kvm *kvm);
215 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
216 extern void kvmppc_core_free_memslot(struct kvm *kvm,
217 struct kvm_memory_slot *free,
218 struct kvm_memory_slot *dont);
219 extern int kvmppc_core_create_memslot(struct kvm *kvm,
220 struct kvm_memory_slot *slot,
221 unsigned long npages);
222 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
223 struct kvm_memory_slot *memslot,
224 const struct kvm_userspace_memory_region *mem);
225 extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
226 const struct kvm_userspace_memory_region *mem,
227 const struct kvm_memory_slot *old,
228 const struct kvm_memory_slot *new,
229 enum kvm_mr_change change);
230 extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
231 struct kvm_ppc_smmu_info *info);
232 extern void kvmppc_core_flush_memslot(struct kvm *kvm,
233 struct kvm_memory_slot *memslot);
235 extern int kvmppc_bookehv_init(void);
236 extern void kvmppc_bookehv_exit(void);
238 extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
240 extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
241 extern long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
242 struct kvm_ppc_resize_hpt *rhpt);
243 extern long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
244 struct kvm_ppc_resize_hpt *rhpt);
246 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
248 extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
249 extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
250 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
252 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
254 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
256 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
257 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
259 void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
260 void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
262 union kvmppc_one_reg {
277 struct module *owner;
278 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
279 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
280 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
281 union kvmppc_one_reg *val);
282 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
283 union kvmppc_one_reg *val);
284 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
285 void (*vcpu_put)(struct kvm_vcpu *vcpu);
286 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
287 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
288 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
289 void (*vcpu_free)(struct kvm_vcpu *vcpu);
290 int (*check_requests)(struct kvm_vcpu *vcpu);
291 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
292 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
293 int (*prepare_memory_region)(struct kvm *kvm,
294 struct kvm_memory_slot *memslot,
295 const struct kvm_userspace_memory_region *mem);
296 void (*commit_memory_region)(struct kvm *kvm,
297 const struct kvm_userspace_memory_region *mem,
298 const struct kvm_memory_slot *old,
299 const struct kvm_memory_slot *new,
300 enum kvm_mr_change change);
301 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
303 int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
304 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
305 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
306 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
307 void (*free_memslot)(struct kvm_memory_slot *free,
308 struct kvm_memory_slot *dont);
309 int (*create_memslot)(struct kvm_memory_slot *slot,
310 unsigned long npages);
311 int (*init_vm)(struct kvm *kvm);
312 void (*destroy_vm)(struct kvm *kvm);
313 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
314 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
315 unsigned int inst, int *advance);
316 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
317 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
318 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
319 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
321 int (*hcall_implemented)(unsigned long hcall);
322 int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
323 struct irq_bypass_producer *);
324 void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
325 struct irq_bypass_producer *);
326 int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
327 int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
328 int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
329 unsigned long flags);
330 void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
331 int (*enable_nested)(struct kvm *kvm);
332 int (*load_from_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
334 int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
338 extern struct kvmppc_ops *kvmppc_hv_ops;
339 extern struct kvmppc_ops *kvmppc_pr_ops;
341 static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
342 enum instruction_fetch_type type, u32 *inst)
344 int ret = EMULATE_DONE;
347 /* Load the instruction manually if it failed to do so in the
349 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
350 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
352 /* Write fetch_failed unswapped if the fetch failed */
353 if (ret == EMULATE_DONE)
354 fetched_inst = kvmppc_need_byteswap(vcpu) ?
355 swab32(vcpu->arch.last_inst) :
356 vcpu->arch.last_inst;
358 fetched_inst = vcpu->arch.last_inst;
360 *inst = fetched_inst;
364 static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
366 return kvm->arch.kvm_ops == kvmppc_hv_ops;
369 extern int kvmppc_hwrng_present(void);
372 * Cuts out inst bits with ordering according to spec.
373 * That means the leftmost bit is zero. All given bits are included.
375 static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
382 mask = (1 << (lsb - msb + 1)) - 1;
383 r = (inst >> (63 - lsb)) & mask;
389 * Replaces inst bits with ordering according to spec.
391 static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
398 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
399 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
404 #define one_reg_size(id) \
405 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
407 #define get_reg_val(id, reg) ({ \
408 union kvmppc_one_reg __u; \
409 switch (one_reg_size(id)) { \
410 case 4: __u.wval = (reg); break; \
411 case 8: __u.dval = (reg); break; \
418 #define set_reg_val(id, val) ({ \
420 switch (one_reg_size(id)) { \
421 case 4: __v = (val).wval; break; \
422 case 8: __v = (val).dval; break; \
428 int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
429 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
431 int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
432 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
434 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
435 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
436 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
437 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
439 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
443 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
444 extern void kvm_cma_reserve(void) __init;
445 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
447 paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
450 static inline void kvmppc_set_xive_tima(int cpu,
451 unsigned long phys_addr,
452 void __iomem *virt_addr)
454 paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
455 paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
458 static inline u32 kvmppc_get_xics_latch(void)
462 xirr = get_paca()->kvm_hstate.saved_xirr;
463 get_paca()->kvm_hstate.saved_xirr = 0;
467 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
469 paca_ptrs[cpu]->kvm_hstate.host_ipi = host_ipi;
472 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
474 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
477 extern void kvm_hv_vm_activated(void);
478 extern void kvm_hv_vm_deactivated(void);
479 extern bool kvm_hv_mode_active(void);
482 static inline void __init kvm_cma_reserve(void)
485 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
488 static inline void kvmppc_set_xive_tima(int cpu,
489 unsigned long phys_addr,
490 void __iomem *virt_addr)
493 static inline u32 kvmppc_get_xics_latch(void)
498 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
501 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
506 static inline bool kvm_hv_mode_active(void) { return false; }
510 #ifdef CONFIG_KVM_XICS
511 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
513 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
516 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
519 if (kvm && kvm_irq_bypass)
520 return kvm->arch.pimap;
524 extern void kvmppc_alloc_host_rm_ops(void);
525 extern void kvmppc_free_host_rm_ops(void);
526 extern void kvmppc_free_pimap(struct kvm *kvm);
527 extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
528 extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
529 extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
530 extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
531 extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
532 extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
533 struct kvm_vcpu *vcpu, u32 cpu);
534 extern void kvmppc_xics_ipi_action(void);
535 extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
536 unsigned long host_irq);
537 extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
538 unsigned long host_irq);
539 extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
540 struct kvmppc_irq_map *irq_map,
541 struct kvmppc_passthru_irqmap *pimap,
544 extern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
545 int level, bool line_status);
547 extern int h_ipi_redirect;
549 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
552 static inline void kvmppc_alloc_host_rm_ops(void) {};
553 static inline void kvmppc_free_host_rm_ops(void) {};
554 static inline void kvmppc_free_pimap(struct kvm *kvm) {};
555 static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
557 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
559 static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
560 static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
564 #ifdef CONFIG_KVM_XIVE
566 * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
567 * ie. P9 new interrupt controller, while the second "xive" is the legacy
568 * "eXternal Interrupt Vector Entry" which is the configuration of an
569 * interrupt on the "xics" interrupt controller on P8 and earlier. Those
570 * two function consume or produce a legacy "XIVE" state from the
571 * new "XIVE" interrupt controller.
573 extern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
575 extern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
577 extern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
578 extern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
579 extern void kvmppc_xive_init_module(void);
580 extern void kvmppc_xive_exit_module(void);
582 extern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
583 struct kvm_vcpu *vcpu, u32 cpu);
584 extern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
585 extern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
586 struct irq_desc *host_desc);
587 extern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
588 struct irq_desc *host_desc);
589 extern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
590 extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
592 extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
593 int level, bool line_status);
594 extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
596 static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
597 u32 priority) { return -1; }
598 static inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
599 u32 *priority) { return -1; }
600 static inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
601 static inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
602 static inline void kvmppc_xive_init_module(void) { }
603 static inline void kvmppc_xive_exit_module(void) { }
605 static inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
606 struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
607 static inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
608 static inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
609 struct irq_desc *host_desc) { return -ENODEV; }
610 static inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
611 struct irq_desc *host_desc) { return -ENODEV; }
612 static inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
613 static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
615 static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
616 int level, bool line_status) { return -ENODEV; }
617 static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
618 #endif /* CONFIG_KVM_XIVE */
621 * Prototypes for functions called only from assembler code.
622 * Having prototypes reduces sparse errors.
624 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
625 unsigned long ioba, unsigned long tce);
626 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
627 unsigned long liobn, unsigned long ioba,
628 unsigned long tce_list, unsigned long npages);
629 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
630 unsigned long liobn, unsigned long ioba,
631 unsigned long tce_value, unsigned long npages);
632 long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
633 unsigned int yield_count);
634 long kvmppc_h_random(struct kvm_vcpu *vcpu);
635 void kvmhv_commence_exit(int trap);
636 void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
637 void kvmppc_subcore_enter_guest(void);
638 void kvmppc_subcore_exit_guest(void);
639 long kvmppc_realmode_hmi_handler(void);
640 long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
641 long pte_index, unsigned long pteh, unsigned long ptel);
642 long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
643 unsigned long pte_index, unsigned long avpn);
644 long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
645 long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
646 unsigned long pte_index, unsigned long avpn,
648 long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
649 unsigned long pte_index);
650 long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
651 unsigned long pte_index);
652 long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
653 unsigned long pte_index);
654 long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
655 unsigned long slb_v, unsigned int status, bool data);
656 unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu);
657 unsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu);
658 unsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
659 int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
661 int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
662 int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
663 void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
666 * Host-side operations we want to set up while running in real
667 * mode in the guest operating on the xics.
668 * Currently only VCPU wakeup is supported.
671 union kvmppc_rm_state {
679 struct kvmppc_host_rm_core {
680 union kvmppc_rm_state rm_state;
685 struct kvmppc_host_rm_ops {
686 struct kvmppc_host_rm_core *rm_core;
687 void (*vcpu_kick)(struct kvm_vcpu *vcpu);
690 extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
692 static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
694 #ifdef CONFIG_KVM_BOOKE_HV
695 return mfspr(SPRN_GEPR);
696 #elif defined(CONFIG_BOOKE)
697 return vcpu->arch.epr;
703 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
705 #ifdef CONFIG_KVM_BOOKE_HV
706 mtspr(SPRN_GEPR, epr);
707 #elif defined(CONFIG_BOOKE)
708 vcpu->arch.epr = epr;
712 #ifdef CONFIG_KVM_MPIC
714 void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
715 int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
717 void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
721 static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
725 static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
726 struct kvm_vcpu *vcpu, u32 cpu)
731 static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
732 struct kvm_vcpu *vcpu)
736 #endif /* CONFIG_KVM_MPIC */
738 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
739 struct kvm_config_tlb *cfg);
740 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
741 struct kvm_dirty_tlb *cfg);
743 long kvmppc_alloc_lpid(void);
744 void kvmppc_claim_lpid(long lpid);
745 void kvmppc_free_lpid(long lpid);
746 void kvmppc_init_lpid(unsigned long nr_lpids);
748 static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
752 * We can only access pages that the kernel maps
753 * as memory. Bail out for unmapped ones.
758 /* Clear i-cache for new pages */
759 page = pfn_to_page(pfn);
760 if (!test_bit(PG_arch_1, &page->flags)) {
761 flush_dcache_icache_page(page);
762 set_bit(PG_arch_1, &page->flags);
767 * Shared struct helpers. The shared struct can be little or big endian,
768 * depending on the guest endianness. So expose helpers to all of them.
770 static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
772 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
773 /* Only Book3S_64 PR supports bi-endian for now */
774 return vcpu->arch.shared_big_endian;
775 #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
776 /* Book3s_64 HV on little endian is always little endian */
783 #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
784 static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
786 return mfspr(bookehv_spr); \
789 #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
790 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
792 mtspr(bookehv_spr, val); \
795 #define SHARED_WRAPPER_GET(reg, size) \
796 static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
798 if (kvmppc_shared_big_endian(vcpu)) \
799 return be##size##_to_cpu(vcpu->arch.shared->reg); \
801 return le##size##_to_cpu(vcpu->arch.shared->reg); \
804 #define SHARED_WRAPPER_SET(reg, size) \
805 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
807 if (kvmppc_shared_big_endian(vcpu)) \
808 vcpu->arch.shared->reg = cpu_to_be##size(val); \
810 vcpu->arch.shared->reg = cpu_to_le##size(val); \
813 #define SHARED_WRAPPER(reg, size) \
814 SHARED_WRAPPER_GET(reg, size) \
815 SHARED_WRAPPER_SET(reg, size) \
817 #define SPRNG_WRAPPER(reg, bookehv_spr) \
818 SPRNG_WRAPPER_GET(reg, bookehv_spr) \
819 SPRNG_WRAPPER_SET(reg, bookehv_spr) \
821 #ifdef CONFIG_KVM_BOOKE_HV
823 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
824 SPRNG_WRAPPER(reg, bookehv_spr) \
828 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
829 SHARED_WRAPPER(reg, size) \
833 SHARED_WRAPPER(critical, 64)
834 SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
835 SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
836 SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
837 SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
838 SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
839 SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
840 SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
841 SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
842 SHARED_WRAPPER_GET(msr, 64)
843 static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
845 if (kvmppc_shared_big_endian(vcpu))
846 vcpu->arch.shared->msr = cpu_to_be64(val);
848 vcpu->arch.shared->msr = cpu_to_le64(val);
850 SHARED_WRAPPER(dsisr, 32)
851 SHARED_WRAPPER(int_pending, 32)
852 SHARED_WRAPPER(sprg4, 64)
853 SHARED_WRAPPER(sprg5, 64)
854 SHARED_WRAPPER(sprg6, 64)
855 SHARED_WRAPPER(sprg7, 64)
857 static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
859 if (kvmppc_shared_big_endian(vcpu))
860 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
862 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
865 static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
867 if (kvmppc_shared_big_endian(vcpu))
868 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
870 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
874 * Please call after prepare_to_enter. This function puts the lazy ee and irq
875 * disabled tracking state back to normal mode, without actually enabling
878 static inline void kvmppc_fix_ee_before_entry(void)
884 * To avoid races, the caller must have gone directly from having
885 * interrupts fully-enabled to hard-disabled.
887 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
889 /* Only need to enable IRQs by hard enabling them after this */
890 local_paca->irq_happened = 0;
891 irq_soft_mask_set(IRQS_ENABLED);
895 static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
900 ea = kvmppc_get_gpr(vcpu, rb);
902 ea += kvmppc_get_gpr(vcpu, ra);
904 #if defined(CONFIG_PPC_BOOK3E_64)
906 #elif defined(CONFIG_PPC_BOOK3S_64)
910 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
916 extern void xics_wake_cpu(int cpu);
918 #endif /* __POWERPC_KVM_PPC_H__ */