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
40 #include <asm/cpu_has_feature.h>
44 * KVMPPC_INST_SW_BREAKPOINT is debug Instruction
45 * for supporting software breakpoint.
47 #define KVMPPC_INST_SW_BREAKPOINT 0x00dddd00
49 enum emulation_result {
50 EMULATE_DONE, /* no further processing */
51 EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
52 EMULATE_FAIL, /* can't emulate this instruction */
53 EMULATE_AGAIN, /* something went wrong. go again */
54 EMULATE_EXIT_USER, /* emulation requires exit to user-space */
57 enum instruction_fetch_type {
59 INST_SC, /* system call */
63 XLATE_INST, /* translate instruction address */
64 XLATE_DATA /* translate data address */
67 enum xlate_readwrite {
68 XLATE_READ, /* check for read permissions */
69 XLATE_WRITE /* check for write permissions */
72 extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
73 extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
74 extern void kvmppc_handler_highmem(void);
76 extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
77 extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
78 unsigned int rt, unsigned int bytes,
79 int is_default_endian);
80 extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
81 unsigned int rt, unsigned int bytes,
82 int is_default_endian);
83 extern int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
84 unsigned int rt, unsigned int bytes,
85 int is_default_endian, int mmio_sign_extend);
86 extern int kvmppc_handle_vmx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
87 unsigned int rt, unsigned int bytes, int is_default_endian);
88 extern int kvmppc_handle_vmx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
89 unsigned int rs, unsigned int bytes, int is_default_endian);
90 extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
91 u64 val, unsigned int bytes,
92 int is_default_endian);
93 extern int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
94 int rs, unsigned int bytes,
95 int is_default_endian);
97 extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
98 enum instruction_fetch_type type, u32 *inst);
100 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
102 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
104 extern int kvmppc_emulate_instruction(struct kvm_run *run,
105 struct kvm_vcpu *vcpu);
106 extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
107 extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
108 extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
109 extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
110 extern void kvmppc_decrementer_func(struct kvm_vcpu *vcpu);
111 extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
112 extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
113 extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
115 /* Core-specific hooks */
117 extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
118 unsigned int gtlb_idx);
119 extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
120 extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
121 extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
122 extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
123 extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
124 extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
125 extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
127 extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
128 extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
129 extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
130 enum xlate_instdata xlid, enum xlate_readwrite xlrw,
131 struct kvmppc_pte *pte);
133 extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
135 extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
136 extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
137 extern int kvmppc_core_check_processor_compat(void);
138 extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
139 struct kvm_translation *tr);
141 extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
142 extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
144 extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
145 extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
146 extern void kvmppc_core_queue_machine_check(struct kvm_vcpu *vcpu, ulong flags);
147 extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
148 extern void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu);
149 extern void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu);
150 extern void kvmppc_core_queue_vsx_unavail(struct kvm_vcpu *vcpu);
151 extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
152 extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
153 extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
154 struct kvm_interrupt *irq);
155 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
156 extern void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, ulong dear_flags,
158 extern void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
161 extern void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu);
162 extern void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
164 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
165 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
167 extern int kvmppc_booke_init(void);
168 extern void kvmppc_booke_exit(void);
170 extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
171 extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
172 extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
174 extern int kvmppc_allocate_hpt(struct kvm_hpt_info *info, u32 order);
175 extern void kvmppc_set_hpt(struct kvm *kvm, struct kvm_hpt_info *info);
176 extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, int order);
177 extern void kvmppc_free_hpt(struct kvm_hpt_info *info);
178 extern void kvmppc_rmap_reset(struct kvm *kvm);
179 extern long kvmppc_prepare_vrma(struct kvm *kvm,
180 struct kvm_userspace_memory_region *mem);
181 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
182 struct kvm_memory_slot *memslot, unsigned long porder);
183 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
184 extern long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
185 struct iommu_group *grp);
186 extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
187 struct iommu_group *grp);
188 extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
189 extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
190 extern void kvmppc_setup_partition_table(struct kvm *kvm);
192 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
193 struct kvm_create_spapr_tce_64 *args);
194 extern struct kvmppc_spapr_tce_table *kvmppc_find_table(
195 struct kvm *kvm, unsigned long liobn);
196 #define kvmppc_ioba_validate(stt, ioba, npages) \
197 (iommu_tce_check_ioba((stt)->page_shift, (stt)->offset, \
198 (stt)->size, (ioba), (npages)) ? \
199 H_PARAMETER : H_SUCCESS)
200 extern long kvmppc_tce_to_ua(struct kvm *kvm, unsigned long tce,
201 unsigned long *ua, unsigned long **prmap);
202 extern void kvmppc_tce_put(struct kvmppc_spapr_tce_table *tt,
203 unsigned long idx, unsigned long tce);
204 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
205 unsigned long ioba, unsigned long tce);
206 extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
207 unsigned long liobn, unsigned long ioba,
208 unsigned long tce_list, unsigned long npages);
209 extern long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
210 unsigned long liobn, unsigned long ioba,
211 unsigned long tce_value, unsigned long npages);
212 extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
214 extern struct page *kvm_alloc_hpt_cma(unsigned long nr_pages);
215 extern void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages);
216 extern int kvmppc_core_init_vm(struct kvm *kvm);
217 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
218 extern void kvmppc_core_free_memslot(struct kvm *kvm,
219 struct kvm_memory_slot *free,
220 struct kvm_memory_slot *dont);
221 extern int kvmppc_core_create_memslot(struct kvm *kvm,
222 struct kvm_memory_slot *slot,
223 unsigned long npages);
224 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
225 struct kvm_memory_slot *memslot,
226 const struct kvm_userspace_memory_region *mem);
227 extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
228 const struct kvm_userspace_memory_region *mem,
229 const struct kvm_memory_slot *old,
230 const struct kvm_memory_slot *new,
231 enum kvm_mr_change change);
232 extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
233 struct kvm_ppc_smmu_info *info);
234 extern void kvmppc_core_flush_memslot(struct kvm *kvm,
235 struct kvm_memory_slot *memslot);
237 extern int kvmppc_bookehv_init(void);
238 extern void kvmppc_bookehv_exit(void);
240 extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
242 extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
243 extern long kvm_vm_ioctl_resize_hpt_prepare(struct kvm *kvm,
244 struct kvm_ppc_resize_hpt *rhpt);
245 extern long kvm_vm_ioctl_resize_hpt_commit(struct kvm *kvm,
246 struct kvm_ppc_resize_hpt *rhpt);
248 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
250 extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
251 extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
252 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
254 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
256 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
258 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
259 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
261 void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu);
262 void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu);
264 union kvmppc_one_reg {
279 struct module *owner;
280 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
281 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
282 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
283 union kvmppc_one_reg *val);
284 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
285 union kvmppc_one_reg *val);
286 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
287 void (*vcpu_put)(struct kvm_vcpu *vcpu);
288 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
289 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
290 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
291 void (*vcpu_free)(struct kvm_vcpu *vcpu);
292 int (*check_requests)(struct kvm_vcpu *vcpu);
293 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
294 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
295 int (*prepare_memory_region)(struct kvm *kvm,
296 struct kvm_memory_slot *memslot,
297 const struct kvm_userspace_memory_region *mem);
298 void (*commit_memory_region)(struct kvm *kvm,
299 const struct kvm_userspace_memory_region *mem,
300 const struct kvm_memory_slot *old,
301 const struct kvm_memory_slot *new,
302 enum kvm_mr_change change);
303 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
305 int (*age_hva)(struct kvm *kvm, unsigned long start, unsigned long end);
306 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
307 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
308 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
309 void (*free_memslot)(struct kvm_memory_slot *free,
310 struct kvm_memory_slot *dont);
311 int (*create_memslot)(struct kvm_memory_slot *slot,
312 unsigned long npages);
313 int (*init_vm)(struct kvm *kvm);
314 void (*destroy_vm)(struct kvm *kvm);
315 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
316 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
317 unsigned int inst, int *advance);
318 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
319 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
320 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
321 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
323 int (*hcall_implemented)(unsigned long hcall);
324 int (*irq_bypass_add_producer)(struct irq_bypass_consumer *,
325 struct irq_bypass_producer *);
326 void (*irq_bypass_del_producer)(struct irq_bypass_consumer *,
327 struct irq_bypass_producer *);
328 int (*configure_mmu)(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg);
329 int (*get_rmmu_info)(struct kvm *kvm, struct kvm_ppc_rmmu_info *info);
330 int (*set_smt_mode)(struct kvm *kvm, unsigned long mode,
331 unsigned long flags);
332 void (*giveup_ext)(struct kvm_vcpu *vcpu, ulong msr);
333 int (*enable_nested)(struct kvm *kvm);
334 int (*load_from_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
336 int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
340 extern struct kvmppc_ops *kvmppc_hv_ops;
341 extern struct kvmppc_ops *kvmppc_pr_ops;
343 static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
344 enum instruction_fetch_type type, u32 *inst)
346 int ret = EMULATE_DONE;
349 /* Load the instruction manually if it failed to do so in the
351 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
352 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
354 /* Write fetch_failed unswapped if the fetch failed */
355 if (ret == EMULATE_DONE)
356 fetched_inst = kvmppc_need_byteswap(vcpu) ?
357 swab32(vcpu->arch.last_inst) :
358 vcpu->arch.last_inst;
360 fetched_inst = vcpu->arch.last_inst;
362 *inst = fetched_inst;
366 static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
368 return kvm->arch.kvm_ops == kvmppc_hv_ops;
371 extern int kvmppc_hwrng_present(void);
374 * Cuts out inst bits with ordering according to spec.
375 * That means the leftmost bit is zero. All given bits are included.
377 static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
384 mask = (1 << (lsb - msb + 1)) - 1;
385 r = (inst >> (63 - lsb)) & mask;
391 * Replaces inst bits with ordering according to spec.
393 static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
400 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
401 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
406 #define one_reg_size(id) \
407 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
409 #define get_reg_val(id, reg) ({ \
410 union kvmppc_one_reg __u; \
411 switch (one_reg_size(id)) { \
412 case 4: __u.wval = (reg); break; \
413 case 8: __u.dval = (reg); break; \
420 #define set_reg_val(id, val) ({ \
422 switch (one_reg_size(id)) { \
423 case 4: __v = (val).wval; break; \
424 case 8: __v = (val).dval; break; \
430 int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
431 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
433 int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
434 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
436 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
437 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
438 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
439 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
441 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
445 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
446 extern void kvm_cma_reserve(void) __init;
447 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
449 paca_ptrs[cpu]->kvm_hstate.xics_phys = (void __iomem *)addr;
452 static inline void kvmppc_set_xive_tima(int cpu,
453 unsigned long phys_addr,
454 void __iomem *virt_addr)
456 paca_ptrs[cpu]->kvm_hstate.xive_tima_phys = (void __iomem *)phys_addr;
457 paca_ptrs[cpu]->kvm_hstate.xive_tima_virt = virt_addr;
460 static inline u32 kvmppc_get_xics_latch(void)
464 xirr = get_paca()->kvm_hstate.saved_xirr;
465 get_paca()->kvm_hstate.saved_xirr = 0;
469 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
471 paca_ptrs[cpu]->kvm_hstate.host_ipi = host_ipi;
474 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
476 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
479 extern void kvm_hv_vm_activated(void);
480 extern void kvm_hv_vm_deactivated(void);
481 extern bool kvm_hv_mode_active(void);
484 static inline void __init kvm_cma_reserve(void)
487 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
490 static inline void kvmppc_set_xive_tima(int cpu,
491 unsigned long phys_addr,
492 void __iomem *virt_addr)
495 static inline u32 kvmppc_get_xics_latch(void)
500 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
503 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
508 static inline bool kvm_hv_mode_active(void) { return false; }
512 #ifdef CONFIG_KVM_XICS
513 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
515 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
518 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
521 if (kvm && kvm_irq_bypass)
522 return kvm->arch.pimap;
526 extern void kvmppc_alloc_host_rm_ops(void);
527 extern void kvmppc_free_host_rm_ops(void);
528 extern void kvmppc_free_pimap(struct kvm *kvm);
529 extern int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall);
530 extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
531 extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
532 extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
533 extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
534 extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
535 struct kvm_vcpu *vcpu, u32 cpu);
536 extern void kvmppc_xics_ipi_action(void);
537 extern void kvmppc_xics_set_mapped(struct kvm *kvm, unsigned long guest_irq,
538 unsigned long host_irq);
539 extern void kvmppc_xics_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
540 unsigned long host_irq);
541 extern long kvmppc_deliver_irq_passthru(struct kvm_vcpu *vcpu, __be32 xirr,
542 struct kvmppc_irq_map *irq_map,
543 struct kvmppc_passthru_irqmap *pimap,
546 extern int kvmppc_xics_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
547 int level, bool line_status);
549 extern int h_ipi_redirect;
551 static inline struct kvmppc_passthru_irqmap *kvmppc_get_passthru_irqmap(
554 static inline void kvmppc_alloc_host_rm_ops(void) {};
555 static inline void kvmppc_free_host_rm_ops(void) {};
556 static inline void kvmppc_free_pimap(struct kvm *kvm) {};
557 static inline int kvmppc_xics_rm_complete(struct kvm_vcpu *vcpu, u32 hcall)
559 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
561 static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
562 static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
566 #ifdef CONFIG_KVM_XIVE
568 * Below the first "xive" is the "eXternal Interrupt Virtualization Engine"
569 * ie. P9 new interrupt controller, while the second "xive" is the legacy
570 * "eXternal Interrupt Vector Entry" which is the configuration of an
571 * interrupt on the "xics" interrupt controller on P8 and earlier. Those
572 * two function consume or produce a legacy "XIVE" state from the
573 * new "XIVE" interrupt controller.
575 extern int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
577 extern int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
579 extern int kvmppc_xive_int_on(struct kvm *kvm, u32 irq);
580 extern int kvmppc_xive_int_off(struct kvm *kvm, u32 irq);
581 extern void kvmppc_xive_init_module(void);
582 extern void kvmppc_xive_exit_module(void);
584 extern int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
585 struct kvm_vcpu *vcpu, u32 cpu);
586 extern void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu);
587 extern int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
588 struct irq_desc *host_desc);
589 extern int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
590 struct irq_desc *host_desc);
591 extern u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu);
592 extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
594 extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
595 int level, bool line_status);
596 extern void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu);
598 static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
599 u32 priority) { return -1; }
600 static inline int kvmppc_xive_get_xive(struct kvm *kvm, u32 irq, u32 *server,
601 u32 *priority) { return -1; }
602 static inline int kvmppc_xive_int_on(struct kvm *kvm, u32 irq) { return -1; }
603 static inline int kvmppc_xive_int_off(struct kvm *kvm, u32 irq) { return -1; }
604 static inline void kvmppc_xive_init_module(void) { }
605 static inline void kvmppc_xive_exit_module(void) { }
607 static inline int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
608 struct kvm_vcpu *vcpu, u32 cpu) { return -EBUSY; }
609 static inline void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) { }
610 static inline int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,
611 struct irq_desc *host_desc) { return -ENODEV; }
612 static inline int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq,
613 struct irq_desc *host_desc) { return -ENODEV; }
614 static inline u64 kvmppc_xive_get_icp(struct kvm_vcpu *vcpu) { return 0; }
615 static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { return -ENOENT; }
617 static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq,
618 int level, bool line_status) { return -ENODEV; }
619 static inline void kvmppc_xive_push_vcpu(struct kvm_vcpu *vcpu) { }
620 #endif /* CONFIG_KVM_XIVE */
622 #if defined(CONFIG_PPC_POWERNV) && defined(CONFIG_KVM_BOOK3S_64_HANDLER)
623 static inline bool xics_on_xive(void)
625 return xive_enabled() && cpu_has_feature(CPU_FTR_HVMODE);
628 static inline bool xics_on_xive(void)
635 * Prototypes for functions called only from assembler code.
636 * Having prototypes reduces sparse errors.
638 long kvmppc_rm_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
639 unsigned long ioba, unsigned long tce);
640 long kvmppc_rm_h_put_tce_indirect(struct kvm_vcpu *vcpu,
641 unsigned long liobn, unsigned long ioba,
642 unsigned long tce_list, unsigned long npages);
643 long kvmppc_rm_h_stuff_tce(struct kvm_vcpu *vcpu,
644 unsigned long liobn, unsigned long ioba,
645 unsigned long tce_value, unsigned long npages);
646 long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
647 unsigned int yield_count);
648 long kvmppc_h_random(struct kvm_vcpu *vcpu);
649 void kvmhv_commence_exit(int trap);
650 void kvmppc_realmode_machine_check(struct kvm_vcpu *vcpu);
651 void kvmppc_subcore_enter_guest(void);
652 void kvmppc_subcore_exit_guest(void);
653 long kvmppc_realmode_hmi_handler(void);
654 long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
655 long pte_index, unsigned long pteh, unsigned long ptel);
656 long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
657 unsigned long pte_index, unsigned long avpn);
658 long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu);
659 long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
660 unsigned long pte_index, unsigned long avpn,
662 long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
663 unsigned long pte_index);
664 long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
665 unsigned long pte_index);
666 long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
667 unsigned long pte_index);
668 long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
669 unsigned long slb_v, unsigned int status, bool data);
670 unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu);
671 unsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu);
672 unsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server);
673 int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
675 int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr);
676 int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr);
677 void kvmppc_guest_entry_inject_int(struct kvm_vcpu *vcpu);
680 * Host-side operations we want to set up while running in real
681 * mode in the guest operating on the xics.
682 * Currently only VCPU wakeup is supported.
685 union kvmppc_rm_state {
693 struct kvmppc_host_rm_core {
694 union kvmppc_rm_state rm_state;
699 struct kvmppc_host_rm_ops {
700 struct kvmppc_host_rm_core *rm_core;
701 void (*vcpu_kick)(struct kvm_vcpu *vcpu);
704 extern struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
706 static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
708 #ifdef CONFIG_KVM_BOOKE_HV
709 return mfspr(SPRN_GEPR);
710 #elif defined(CONFIG_BOOKE)
711 return vcpu->arch.epr;
717 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
719 #ifdef CONFIG_KVM_BOOKE_HV
720 mtspr(SPRN_GEPR, epr);
721 #elif defined(CONFIG_BOOKE)
722 vcpu->arch.epr = epr;
726 #ifdef CONFIG_KVM_MPIC
728 void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
729 int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
731 void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
735 static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
739 static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
740 struct kvm_vcpu *vcpu, u32 cpu)
745 static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
746 struct kvm_vcpu *vcpu)
750 #endif /* CONFIG_KVM_MPIC */
752 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
753 struct kvm_config_tlb *cfg);
754 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
755 struct kvm_dirty_tlb *cfg);
757 long kvmppc_alloc_lpid(void);
758 void kvmppc_claim_lpid(long lpid);
759 void kvmppc_free_lpid(long lpid);
760 void kvmppc_init_lpid(unsigned long nr_lpids);
762 static inline void kvmppc_mmu_flush_icache(kvm_pfn_t pfn)
766 * We can only access pages that the kernel maps
767 * as memory. Bail out for unmapped ones.
772 /* Clear i-cache for new pages */
773 page = pfn_to_page(pfn);
774 if (!test_bit(PG_arch_1, &page->flags)) {
775 flush_dcache_icache_page(page);
776 set_bit(PG_arch_1, &page->flags);
781 * Shared struct helpers. The shared struct can be little or big endian,
782 * depending on the guest endianness. So expose helpers to all of them.
784 static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
786 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
787 /* Only Book3S_64 PR supports bi-endian for now */
788 return vcpu->arch.shared_big_endian;
789 #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
790 /* Book3s_64 HV on little endian is always little endian */
797 #define SPRNG_WRAPPER_GET(reg, bookehv_spr) \
798 static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
800 return mfspr(bookehv_spr); \
803 #define SPRNG_WRAPPER_SET(reg, bookehv_spr) \
804 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
806 mtspr(bookehv_spr, val); \
809 #define SHARED_WRAPPER_GET(reg, size) \
810 static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
812 if (kvmppc_shared_big_endian(vcpu)) \
813 return be##size##_to_cpu(vcpu->arch.shared->reg); \
815 return le##size##_to_cpu(vcpu->arch.shared->reg); \
818 #define SHARED_WRAPPER_SET(reg, size) \
819 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
821 if (kvmppc_shared_big_endian(vcpu)) \
822 vcpu->arch.shared->reg = cpu_to_be##size(val); \
824 vcpu->arch.shared->reg = cpu_to_le##size(val); \
827 #define SHARED_WRAPPER(reg, size) \
828 SHARED_WRAPPER_GET(reg, size) \
829 SHARED_WRAPPER_SET(reg, size) \
831 #define SPRNG_WRAPPER(reg, bookehv_spr) \
832 SPRNG_WRAPPER_GET(reg, bookehv_spr) \
833 SPRNG_WRAPPER_SET(reg, bookehv_spr) \
835 #ifdef CONFIG_KVM_BOOKE_HV
837 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
838 SPRNG_WRAPPER(reg, bookehv_spr) \
842 #define SHARED_SPRNG_WRAPPER(reg, size, bookehv_spr) \
843 SHARED_WRAPPER(reg, size) \
847 SHARED_WRAPPER(critical, 64)
848 SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
849 SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
850 SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
851 SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
852 SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
853 SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
854 SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
855 SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
856 SHARED_WRAPPER_GET(msr, 64)
857 static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
859 if (kvmppc_shared_big_endian(vcpu))
860 vcpu->arch.shared->msr = cpu_to_be64(val);
862 vcpu->arch.shared->msr = cpu_to_le64(val);
864 SHARED_WRAPPER(dsisr, 32)
865 SHARED_WRAPPER(int_pending, 32)
866 SHARED_WRAPPER(sprg4, 64)
867 SHARED_WRAPPER(sprg5, 64)
868 SHARED_WRAPPER(sprg6, 64)
869 SHARED_WRAPPER(sprg7, 64)
871 static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
873 if (kvmppc_shared_big_endian(vcpu))
874 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
876 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
879 static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
881 if (kvmppc_shared_big_endian(vcpu))
882 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
884 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
888 * Please call after prepare_to_enter. This function puts the lazy ee and irq
889 * disabled tracking state back to normal mode, without actually enabling
892 static inline void kvmppc_fix_ee_before_entry(void)
898 * To avoid races, the caller must have gone directly from having
899 * interrupts fully-enabled to hard-disabled.
901 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
903 /* Only need to enable IRQs by hard enabling them after this */
904 local_paca->irq_happened = 0;
905 irq_soft_mask_set(IRQS_ENABLED);
909 static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
914 ea = kvmppc_get_gpr(vcpu, rb);
916 ea += kvmppc_get_gpr(vcpu, ra);
918 #if defined(CONFIG_PPC_BOOK3E_64)
920 #elif defined(CONFIG_PPC_BOOK3S_64)
924 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
930 extern void xics_wake_cpu(int cpu);
932 #endif /* __POWERPC_KVM_PPC_H__ */