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. 2007
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/vmalloc.h>
25 #include <linux/hrtimer.h>
27 #include <linux/slab.h>
28 #include <asm/cputable.h>
29 #include <asm/uaccess.h>
30 #include <asm/kvm_ppc.h>
31 #include <asm/tlbflush.h>
32 #include <asm/cputhreads.h>
33 #include <asm/irqflags.h>
35 #include "../mm/mmu_decl.h"
37 #define CREATE_TRACE_POINTS
40 int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
42 return !!(v->arch.pending_exceptions) ||
46 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
51 #ifndef CONFIG_KVM_BOOK3S_64_HV
53 * Common checks before entering the guest world. Call with interrupts
56 * returns !0 if a signal is pending and check_signal is true
58 int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
62 WARN_ON_ONCE(!irqs_disabled());
71 if (signal_pending(current)) {
78 /* Make sure we process requests preemptable */
80 trace_kvm_check_requests(vcpu);
81 kvmppc_core_check_requests(vcpu);
86 if (kvmppc_core_prepare_to_enter(vcpu)) {
87 /* interrupts got enabled in between, so we
88 are back at square 1 */
92 if (vcpu->mode == EXITING_GUEST_MODE) {
100 if (lazy_irq_pending()) {
101 /* Got an interrupt in between, try again */
113 /* Going into guest context! Yay! */
114 vcpu->mode = IN_GUEST_MODE;
122 #endif /* CONFIG_KVM_BOOK3S_64_HV */
124 int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
126 int nr = kvmppc_get_gpr(vcpu, 11);
128 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
129 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
130 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
131 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
132 unsigned long r2 = 0;
134 if (!(vcpu->arch.shared->msr & MSR_SF)) {
136 param1 &= 0xffffffff;
137 param2 &= 0xffffffff;
138 param3 &= 0xffffffff;
139 param4 &= 0xffffffff;
143 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
145 vcpu->arch.magic_page_pa = param1;
146 vcpu->arch.magic_page_ea = param2;
148 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
153 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
155 #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
156 /* XXX Missing magic page on 44x */
157 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
160 /* Second return value is in r4 */
162 case EV_HCALL_TOKEN(EV_IDLE):
164 kvm_vcpu_block(vcpu);
165 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
168 r = EV_UNIMPLEMENTED;
172 kvmppc_set_gpr(vcpu, 4, r2);
177 int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
181 /* We have to know what CPU to virtualize */
185 /* PAPR only works with book3s_64 */
186 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
189 #ifdef CONFIG_KVM_BOOK3S_64_HV
190 /* HV KVM can only do PAPR mode for now */
191 if (!vcpu->arch.papr_enabled)
195 #ifdef CONFIG_KVM_BOOKE_HV
196 if (!cpu_has_feature(CPU_FTR_EMB_HV))
204 return r ? 0 : -EINVAL;
207 int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
209 enum emulation_result er;
212 er = kvmppc_emulate_instruction(run, vcpu);
215 /* Future optimization: only reload non-volatiles if they were
216 * actually modified. */
219 case EMULATE_DO_MMIO:
220 run->exit_reason = KVM_EXIT_MMIO;
221 /* We must reload nonvolatiles because "update" load/store
222 * instructions modify register state. */
223 /* Future optimization: only reload non-volatiles if they were
224 * actually modified. */
228 /* XXX Deliver Program interrupt to guest. */
229 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
230 kvmppc_get_last_inst(vcpu));
240 int kvm_arch_hardware_enable(void *garbage)
245 void kvm_arch_hardware_disable(void *garbage)
249 int kvm_arch_hardware_setup(void)
254 void kvm_arch_hardware_unsetup(void)
258 void kvm_arch_check_processor_compat(void *rtn)
260 *(int *)rtn = kvmppc_core_check_processor_compat();
263 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
268 return kvmppc_core_init_vm(kvm);
271 void kvm_arch_destroy_vm(struct kvm *kvm)
274 struct kvm_vcpu *vcpu;
276 kvm_for_each_vcpu(i, vcpu, kvm)
277 kvm_arch_vcpu_free(vcpu);
279 mutex_lock(&kvm->lock);
280 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
281 kvm->vcpus[i] = NULL;
283 atomic_set(&kvm->online_vcpus, 0);
285 kvmppc_core_destroy_vm(kvm);
287 mutex_unlock(&kvm->lock);
290 void kvm_arch_sync_events(struct kvm *kvm)
294 int kvm_dev_ioctl_check_extension(long ext)
300 case KVM_CAP_PPC_BOOKE_SREGS:
302 case KVM_CAP_PPC_SEGSTATE:
303 case KVM_CAP_PPC_HIOR:
304 case KVM_CAP_PPC_PAPR:
306 case KVM_CAP_PPC_UNSET_IRQ:
307 case KVM_CAP_PPC_IRQ_LEVEL:
308 case KVM_CAP_ENABLE_CAP:
309 case KVM_CAP_ONE_REG:
312 #ifndef CONFIG_KVM_BOOK3S_64_HV
313 case KVM_CAP_PPC_PAIRED_SINGLES:
314 case KVM_CAP_PPC_OSI:
315 case KVM_CAP_PPC_GET_PVINFO:
316 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
321 case KVM_CAP_COALESCED_MMIO:
322 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
325 #ifdef CONFIG_PPC_BOOK3S_64
326 case KVM_CAP_SPAPR_TCE:
327 case KVM_CAP_PPC_ALLOC_HTAB:
330 #endif /* CONFIG_PPC_BOOK3S_64 */
331 #ifdef CONFIG_KVM_BOOK3S_64_HV
332 case KVM_CAP_PPC_SMT:
333 r = threads_per_core;
335 case KVM_CAP_PPC_RMA:
337 /* PPC970 requires an RMA */
338 if (cpu_has_feature(CPU_FTR_ARCH_201))
342 case KVM_CAP_SYNC_MMU:
343 #ifdef CONFIG_KVM_BOOK3S_64_HV
344 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
345 #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
351 case KVM_CAP_NR_VCPUS:
353 * Recommending a number of CPUs is somewhat arbitrary; we
354 * return the number of present CPUs for -HV (since a host
355 * will have secondary threads "offline"), and for other KVM
356 * implementations just count online CPUs.
358 #ifdef CONFIG_KVM_BOOK3S_64_HV
359 r = num_present_cpus();
361 r = num_online_cpus();
364 case KVM_CAP_MAX_VCPUS:
367 #ifdef CONFIG_PPC_BOOK3S_64
368 case KVM_CAP_PPC_GET_SMMU_INFO:
380 long kvm_arch_dev_ioctl(struct file *filp,
381 unsigned int ioctl, unsigned long arg)
386 void kvm_arch_free_memslot(struct kvm_memory_slot *free,
387 struct kvm_memory_slot *dont)
389 if (!dont || free->arch.rmap != dont->arch.rmap) {
390 vfree(free->arch.rmap);
391 free->arch.rmap = NULL;
395 int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
397 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
398 if (!slot->arch.rmap)
404 int kvm_arch_prepare_memory_region(struct kvm *kvm,
405 struct kvm_memory_slot *memslot,
406 struct kvm_memory_slot old,
407 struct kvm_userspace_memory_region *mem,
410 return kvmppc_core_prepare_memory_region(kvm, mem);
413 void kvm_arch_commit_memory_region(struct kvm *kvm,
414 struct kvm_userspace_memory_region *mem,
415 struct kvm_memory_slot old,
418 kvmppc_core_commit_memory_region(kvm, mem);
421 void kvm_arch_flush_shadow_all(struct kvm *kvm)
425 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
426 struct kvm_memory_slot *slot)
430 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
432 struct kvm_vcpu *vcpu;
433 vcpu = kvmppc_core_vcpu_create(kvm, id);
435 vcpu->arch.wqp = &vcpu->wq;
436 kvmppc_create_vcpu_debugfs(vcpu, id);
441 void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
443 /* Make sure we're not using the vcpu anymore */
444 hrtimer_cancel(&vcpu->arch.dec_timer);
445 tasklet_kill(&vcpu->arch.tasklet);
447 kvmppc_remove_vcpu_debugfs(vcpu);
448 kvmppc_core_vcpu_free(vcpu);
451 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
453 kvm_arch_vcpu_free(vcpu);
456 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
458 return kvmppc_core_pending_dec(vcpu);
462 * low level hrtimer wake routine. Because this runs in hardirq context
463 * we schedule a tasklet to do the real work.
465 enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
467 struct kvm_vcpu *vcpu;
469 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
470 tasklet_schedule(&vcpu->arch.tasklet);
472 return HRTIMER_NORESTART;
475 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
477 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
478 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
479 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
480 vcpu->arch.dec_expires = ~(u64)0;
482 #ifdef CONFIG_KVM_EXIT_TIMING
483 mutex_init(&vcpu->arch.exit_timing_lock);
489 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
491 kvmppc_mmu_destroy(vcpu);
494 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
498 * vrsave (formerly usprg0) isn't used by Linux, but may
499 * be used by the guest.
501 * On non-booke this is associated with Altivec and
502 * is handled by code in book3s.c.
504 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
506 kvmppc_core_vcpu_load(vcpu, cpu);
507 vcpu->cpu = smp_processor_id();
510 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
512 kvmppc_core_vcpu_put(vcpu);
514 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
519 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
520 struct kvm_guest_debug *dbg)
525 static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
528 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
531 static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
534 u64 uninitialized_var(gpr);
536 if (run->mmio.len > sizeof(gpr)) {
537 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
541 if (vcpu->arch.mmio_is_bigendian) {
542 switch (run->mmio.len) {
543 case 8: gpr = *(u64 *)run->mmio.data; break;
544 case 4: gpr = *(u32 *)run->mmio.data; break;
545 case 2: gpr = *(u16 *)run->mmio.data; break;
546 case 1: gpr = *(u8 *)run->mmio.data; break;
549 /* Convert BE data from userland back to LE. */
550 switch (run->mmio.len) {
551 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
552 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
553 case 1: gpr = *(u8 *)run->mmio.data; break;
557 if (vcpu->arch.mmio_sign_extend) {
558 switch (run->mmio.len) {
573 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
575 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
576 case KVM_MMIO_REG_GPR:
577 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
579 case KVM_MMIO_REG_FPR:
580 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
582 #ifdef CONFIG_PPC_BOOK3S
583 case KVM_MMIO_REG_QPR:
584 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
586 case KVM_MMIO_REG_FQPR:
587 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
588 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
596 int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
597 unsigned int rt, unsigned int bytes, int is_bigendian)
599 if (bytes > sizeof(run->mmio.data)) {
600 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
604 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
605 run->mmio.len = bytes;
606 run->mmio.is_write = 0;
608 vcpu->arch.io_gpr = rt;
609 vcpu->arch.mmio_is_bigendian = is_bigendian;
610 vcpu->mmio_needed = 1;
611 vcpu->mmio_is_write = 0;
612 vcpu->arch.mmio_sign_extend = 0;
614 return EMULATE_DO_MMIO;
617 /* Same as above, but sign extends */
618 int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
619 unsigned int rt, unsigned int bytes, int is_bigendian)
623 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
624 vcpu->arch.mmio_sign_extend = 1;
629 int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
630 u64 val, unsigned int bytes, int is_bigendian)
632 void *data = run->mmio.data;
634 if (bytes > sizeof(run->mmio.data)) {
635 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
639 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
640 run->mmio.len = bytes;
641 run->mmio.is_write = 1;
642 vcpu->mmio_needed = 1;
643 vcpu->mmio_is_write = 1;
645 /* Store the value at the lowest bytes in 'data'. */
648 case 8: *(u64 *)data = val; break;
649 case 4: *(u32 *)data = val; break;
650 case 2: *(u16 *)data = val; break;
651 case 1: *(u8 *)data = val; break;
654 /* Store LE value into 'data'. */
656 case 4: st_le32(data, val); break;
657 case 2: st_le16(data, val); break;
658 case 1: *(u8 *)data = val; break;
662 return EMULATE_DO_MMIO;
665 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
670 if (vcpu->sigset_active)
671 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
673 if (vcpu->mmio_needed) {
674 if (!vcpu->mmio_is_write)
675 kvmppc_complete_mmio_load(vcpu, run);
676 vcpu->mmio_needed = 0;
677 } else if (vcpu->arch.dcr_needed) {
678 if (!vcpu->arch.dcr_is_write)
679 kvmppc_complete_dcr_load(vcpu, run);
680 vcpu->arch.dcr_needed = 0;
681 } else if (vcpu->arch.osi_needed) {
682 u64 *gprs = run->osi.gprs;
685 for (i = 0; i < 32; i++)
686 kvmppc_set_gpr(vcpu, i, gprs[i]);
687 vcpu->arch.osi_needed = 0;
688 } else if (vcpu->arch.hcall_needed) {
691 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
692 for (i = 0; i < 9; ++i)
693 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
694 vcpu->arch.hcall_needed = 0;
697 r = kvmppc_vcpu_run(run, vcpu);
699 if (vcpu->sigset_active)
700 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
705 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
707 if (irq->irq == KVM_INTERRUPT_UNSET) {
708 kvmppc_core_dequeue_external(vcpu, irq);
712 kvmppc_core_queue_external(vcpu, irq);
719 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
720 struct kvm_enable_cap *cap)
728 case KVM_CAP_PPC_OSI:
730 vcpu->arch.osi_enabled = true;
732 case KVM_CAP_PPC_PAPR:
734 vcpu->arch.papr_enabled = true;
736 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
737 case KVM_CAP_SW_TLB: {
738 struct kvm_config_tlb cfg;
739 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
742 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
745 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
755 r = kvmppc_sanity_check(vcpu);
760 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
761 struct kvm_mp_state *mp_state)
766 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
767 struct kvm_mp_state *mp_state)
772 long kvm_arch_vcpu_ioctl(struct file *filp,
773 unsigned int ioctl, unsigned long arg)
775 struct kvm_vcpu *vcpu = filp->private_data;
776 void __user *argp = (void __user *)arg;
780 case KVM_INTERRUPT: {
781 struct kvm_interrupt irq;
783 if (copy_from_user(&irq, argp, sizeof(irq)))
785 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
791 struct kvm_enable_cap cap;
793 if (copy_from_user(&cap, argp, sizeof(cap)))
795 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
799 case KVM_SET_ONE_REG:
800 case KVM_GET_ONE_REG:
802 struct kvm_one_reg reg;
804 if (copy_from_user(®, argp, sizeof(reg)))
806 if (ioctl == KVM_SET_ONE_REG)
807 r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®);
809 r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®);
813 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
814 case KVM_DIRTY_TLB: {
815 struct kvm_dirty_tlb dirty;
817 if (copy_from_user(&dirty, argp, sizeof(dirty)))
819 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
831 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
833 return VM_FAULT_SIGBUS;
836 static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
838 u32 inst_nop = 0x60000000;
839 #ifdef CONFIG_KVM_BOOKE_HV
840 u32 inst_sc1 = 0x44000022;
841 pvinfo->hcall[0] = inst_sc1;
842 pvinfo->hcall[1] = inst_nop;
843 pvinfo->hcall[2] = inst_nop;
844 pvinfo->hcall[3] = inst_nop;
846 u32 inst_lis = 0x3c000000;
847 u32 inst_ori = 0x60000000;
848 u32 inst_sc = 0x44000002;
849 u32 inst_imm_mask = 0xffff;
852 * The hypercall to get into KVM from within guest context is as
855 * lis r0, r0, KVM_SC_MAGIC_R0@h
856 * ori r0, KVM_SC_MAGIC_R0@l
860 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
861 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
862 pvinfo->hcall[2] = inst_sc;
863 pvinfo->hcall[3] = inst_nop;
866 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
871 long kvm_arch_vm_ioctl(struct file *filp,
872 unsigned int ioctl, unsigned long arg)
874 void __user *argp = (void __user *)arg;
878 case KVM_PPC_GET_PVINFO: {
879 struct kvm_ppc_pvinfo pvinfo;
880 memset(&pvinfo, 0, sizeof(pvinfo));
881 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
882 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
889 #ifdef CONFIG_PPC_BOOK3S_64
890 case KVM_CREATE_SPAPR_TCE: {
891 struct kvm_create_spapr_tce create_tce;
892 struct kvm *kvm = filp->private_data;
895 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
897 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
900 #endif /* CONFIG_PPC_BOOK3S_64 */
902 #ifdef CONFIG_KVM_BOOK3S_64_HV
903 case KVM_ALLOCATE_RMA: {
904 struct kvm *kvm = filp->private_data;
905 struct kvm_allocate_rma rma;
907 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
908 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
913 case KVM_PPC_ALLOCATE_HTAB: {
914 struct kvm *kvm = filp->private_data;
918 if (get_user(htab_order, (u32 __user *)argp))
920 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
924 if (put_user(htab_order, (u32 __user *)argp))
929 #endif /* CONFIG_KVM_BOOK3S_64_HV */
931 #ifdef CONFIG_PPC_BOOK3S_64
932 case KVM_PPC_GET_SMMU_INFO: {
933 struct kvm *kvm = filp->private_data;
934 struct kvm_ppc_smmu_info info;
936 memset(&info, 0, sizeof(info));
937 r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
938 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
942 #endif /* CONFIG_PPC_BOOK3S_64 */
951 static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
952 static unsigned long nr_lpids;
954 long kvmppc_alloc_lpid(void)
959 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
960 if (lpid >= nr_lpids) {
961 pr_err("%s: No LPIDs free\n", __func__);
964 } while (test_and_set_bit(lpid, lpid_inuse));
969 void kvmppc_claim_lpid(long lpid)
971 set_bit(lpid, lpid_inuse);
974 void kvmppc_free_lpid(long lpid)
976 clear_bit(lpid, lpid_inuse);
979 void kvmppc_init_lpid(unsigned long nr_lpids_param)
981 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
982 memset(lpid_inuse, 0, sizeof(lpid_inuse));
985 int kvm_arch_init(void *opaque)
990 void kvm_arch_exit(void)