Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2010,2012 Freescale Semiconductor, Inc. All rights reserved.
0004  *
0005  * Author: Varun Sethi, <varun.sethi@freescale.com>
0006  *
0007  * Description:
0008  * This file is derived from arch/powerpc/kvm/e500.c,
0009  * by Yu Liu <yu.liu@freescale.com>.
0010  */
0011 
0012 #include <linux/kvm_host.h>
0013 #include <linux/slab.h>
0014 #include <linux/err.h>
0015 #include <linux/export.h>
0016 #include <linux/miscdevice.h>
0017 #include <linux/module.h>
0018 
0019 #include <asm/reg.h>
0020 #include <asm/cputable.h>
0021 #include <asm/kvm_ppc.h>
0022 #include <asm/dbell.h>
0023 
0024 #include "booke.h"
0025 #include "e500.h"
0026 
0027 void kvmppc_set_pending_interrupt(struct kvm_vcpu *vcpu, enum int_class type)
0028 {
0029     enum ppc_dbell dbell_type;
0030     unsigned long tag;
0031 
0032     switch (type) {
0033     case INT_CLASS_NONCRIT:
0034         dbell_type = PPC_G_DBELL;
0035         break;
0036     case INT_CLASS_CRIT:
0037         dbell_type = PPC_G_DBELL_CRIT;
0038         break;
0039     case INT_CLASS_MC:
0040         dbell_type = PPC_G_DBELL_MC;
0041         break;
0042     default:
0043         WARN_ONCE(1, "%s: unknown int type %d\n", __func__, type);
0044         return;
0045     }
0046 
0047     preempt_disable();
0048     tag = PPC_DBELL_LPID(get_lpid(vcpu)) | vcpu->vcpu_id;
0049     mb();
0050     ppc_msgsnd(dbell_type, 0, tag);
0051     preempt_enable();
0052 }
0053 
0054 /* gtlbe must not be mapped by more than one host tlb entry */
0055 void kvmppc_e500_tlbil_one(struct kvmppc_vcpu_e500 *vcpu_e500,
0056                struct kvm_book3e_206_tlb_entry *gtlbe)
0057 {
0058     unsigned int tid, ts;
0059     gva_t eaddr;
0060     u32 val;
0061     unsigned long flags;
0062 
0063     ts = get_tlb_ts(gtlbe);
0064     tid = get_tlb_tid(gtlbe);
0065 
0066     /* We search the host TLB to invalidate its shadow TLB entry */
0067     val = (tid << 16) | ts;
0068     eaddr = get_tlb_eaddr(gtlbe);
0069 
0070     local_irq_save(flags);
0071 
0072     mtspr(SPRN_MAS6, val);
0073     mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
0074 
0075     asm volatile("tlbsx 0, %[eaddr]\n" : : [eaddr] "r" (eaddr));
0076     val = mfspr(SPRN_MAS1);
0077     if (val & MAS1_VALID) {
0078         mtspr(SPRN_MAS1, val & ~MAS1_VALID);
0079         asm volatile("tlbwe");
0080     }
0081     mtspr(SPRN_MAS5, 0);
0082     /* NOTE: tlbsx also updates mas8, so clear it for host tlbwe */
0083     mtspr(SPRN_MAS8, 0);
0084     isync();
0085 
0086     local_irq_restore(flags);
0087 }
0088 
0089 void kvmppc_e500_tlbil_all(struct kvmppc_vcpu_e500 *vcpu_e500)
0090 {
0091     unsigned long flags;
0092 
0093     local_irq_save(flags);
0094     mtspr(SPRN_MAS5, MAS5_SGS | get_lpid(&vcpu_e500->vcpu));
0095     asm volatile("tlbilxlpid");
0096     mtspr(SPRN_MAS5, 0);
0097     local_irq_restore(flags);
0098 }
0099 
0100 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid)
0101 {
0102     vcpu->arch.pid = pid;
0103 }
0104 
0105 void kvmppc_mmu_msr_notify(struct kvm_vcpu *vcpu, u32 old_msr)
0106 {
0107 }
0108 
0109 /* We use two lpids per VM */
0110 static DEFINE_PER_CPU(struct kvm_vcpu *[KVMPPC_NR_LPIDS], last_vcpu_of_lpid);
0111 
0112 static void kvmppc_core_vcpu_load_e500mc(struct kvm_vcpu *vcpu, int cpu)
0113 {
0114     struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
0115 
0116     kvmppc_booke_vcpu_load(vcpu, cpu);
0117 
0118     mtspr(SPRN_LPID, get_lpid(vcpu));
0119     mtspr(SPRN_EPCR, vcpu->arch.shadow_epcr);
0120     mtspr(SPRN_GPIR, vcpu->vcpu_id);
0121     mtspr(SPRN_MSRP, vcpu->arch.shadow_msrp);
0122     vcpu->arch.eplc = EPC_EGS | (get_lpid(vcpu) << EPC_ELPID_SHIFT);
0123     vcpu->arch.epsc = vcpu->arch.eplc;
0124     mtspr(SPRN_EPLC, vcpu->arch.eplc);
0125     mtspr(SPRN_EPSC, vcpu->arch.epsc);
0126 
0127     mtspr(SPRN_GIVPR, vcpu->arch.ivpr);
0128     mtspr(SPRN_GIVOR2, vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]);
0129     mtspr(SPRN_GIVOR8, vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]);
0130     mtspr(SPRN_GSPRG0, (unsigned long)vcpu->arch.shared->sprg0);
0131     mtspr(SPRN_GSPRG1, (unsigned long)vcpu->arch.shared->sprg1);
0132     mtspr(SPRN_GSPRG2, (unsigned long)vcpu->arch.shared->sprg2);
0133     mtspr(SPRN_GSPRG3, (unsigned long)vcpu->arch.shared->sprg3);
0134 
0135     mtspr(SPRN_GSRR0, vcpu->arch.shared->srr0);
0136     mtspr(SPRN_GSRR1, vcpu->arch.shared->srr1);
0137 
0138     mtspr(SPRN_GEPR, vcpu->arch.epr);
0139     mtspr(SPRN_GDEAR, vcpu->arch.shared->dar);
0140     mtspr(SPRN_GESR, vcpu->arch.shared->esr);
0141 
0142     if (vcpu->arch.oldpir != mfspr(SPRN_PIR) ||
0143         __this_cpu_read(last_vcpu_of_lpid[get_lpid(vcpu)]) != vcpu) {
0144         kvmppc_e500_tlbil_all(vcpu_e500);
0145         __this_cpu_write(last_vcpu_of_lpid[get_lpid(vcpu)], vcpu);
0146     }
0147 }
0148 
0149 static void kvmppc_core_vcpu_put_e500mc(struct kvm_vcpu *vcpu)
0150 {
0151     vcpu->arch.eplc = mfspr(SPRN_EPLC);
0152     vcpu->arch.epsc = mfspr(SPRN_EPSC);
0153 
0154     vcpu->arch.shared->sprg0 = mfspr(SPRN_GSPRG0);
0155     vcpu->arch.shared->sprg1 = mfspr(SPRN_GSPRG1);
0156     vcpu->arch.shared->sprg2 = mfspr(SPRN_GSPRG2);
0157     vcpu->arch.shared->sprg3 = mfspr(SPRN_GSPRG3);
0158 
0159     vcpu->arch.shared->srr0 = mfspr(SPRN_GSRR0);
0160     vcpu->arch.shared->srr1 = mfspr(SPRN_GSRR1);
0161 
0162     vcpu->arch.epr = mfspr(SPRN_GEPR);
0163     vcpu->arch.shared->dar = mfspr(SPRN_GDEAR);
0164     vcpu->arch.shared->esr = mfspr(SPRN_GESR);
0165 
0166     vcpu->arch.oldpir = mfspr(SPRN_PIR);
0167 
0168     kvmppc_booke_vcpu_put(vcpu);
0169 }
0170 
0171 int kvmppc_core_check_processor_compat(void)
0172 {
0173     int r;
0174 
0175     if (strcmp(cur_cpu_spec->cpu_name, "e500mc") == 0)
0176         r = 0;
0177     else if (strcmp(cur_cpu_spec->cpu_name, "e5500") == 0)
0178         r = 0;
0179 #ifdef CONFIG_ALTIVEC
0180     /*
0181      * Since guests have the privilege to enable AltiVec, we need AltiVec
0182      * support in the host to save/restore their context.
0183      * Don't use CPU_FTR_ALTIVEC to identify cores with AltiVec unit
0184      * because it's cleared in the absence of CONFIG_ALTIVEC!
0185      */
0186     else if (strcmp(cur_cpu_spec->cpu_name, "e6500") == 0)
0187         r = 0;
0188 #endif
0189     else
0190         r = -ENOTSUPP;
0191 
0192     return r;
0193 }
0194 
0195 int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
0196 {
0197     struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
0198 
0199     vcpu->arch.shadow_epcr = SPRN_EPCR_DSIGS | SPRN_EPCR_DGTMI | \
0200                  SPRN_EPCR_DUVD;
0201 #ifdef CONFIG_64BIT
0202     vcpu->arch.shadow_epcr |= SPRN_EPCR_ICM;
0203 #endif
0204     vcpu->arch.shadow_msrp = MSRP_UCLEP | MSRP_PMMP;
0205 
0206     vcpu->arch.pvr = mfspr(SPRN_PVR);
0207     vcpu_e500->svr = mfspr(SPRN_SVR);
0208 
0209     vcpu->arch.cpu_type = KVM_CPU_E500MC;
0210 
0211     return 0;
0212 }
0213 
0214 static int kvmppc_core_get_sregs_e500mc(struct kvm_vcpu *vcpu,
0215                     struct kvm_sregs *sregs)
0216 {
0217     struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
0218 
0219     sregs->u.e.features |= KVM_SREGS_E_ARCH206_MMU | KVM_SREGS_E_PM |
0220                    KVM_SREGS_E_PC;
0221     sregs->u.e.impl_id = KVM_SREGS_E_IMPL_FSL;
0222 
0223     sregs->u.e.impl.fsl.features = 0;
0224     sregs->u.e.impl.fsl.svr = vcpu_e500->svr;
0225     sregs->u.e.impl.fsl.hid0 = vcpu_e500->hid0;
0226     sregs->u.e.impl.fsl.mcar = vcpu_e500->mcar;
0227 
0228     kvmppc_get_sregs_e500_tlb(vcpu, sregs);
0229 
0230     sregs->u.e.ivor_high[3] =
0231         vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR];
0232     sregs->u.e.ivor_high[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL];
0233     sregs->u.e.ivor_high[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT];
0234 
0235     return kvmppc_get_sregs_ivor(vcpu, sregs);
0236 }
0237 
0238 static int kvmppc_core_set_sregs_e500mc(struct kvm_vcpu *vcpu,
0239                     struct kvm_sregs *sregs)
0240 {
0241     struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
0242     int ret;
0243 
0244     if (sregs->u.e.impl_id == KVM_SREGS_E_IMPL_FSL) {
0245         vcpu_e500->svr = sregs->u.e.impl.fsl.svr;
0246         vcpu_e500->hid0 = sregs->u.e.impl.fsl.hid0;
0247         vcpu_e500->mcar = sregs->u.e.impl.fsl.mcar;
0248     }
0249 
0250     ret = kvmppc_set_sregs_e500_tlb(vcpu, sregs);
0251     if (ret < 0)
0252         return ret;
0253 
0254     if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
0255         return 0;
0256 
0257     if (sregs->u.e.features & KVM_SREGS_E_PM) {
0258         vcpu->arch.ivor[BOOKE_IRQPRIO_PERFORMANCE_MONITOR] =
0259             sregs->u.e.ivor_high[3];
0260     }
0261 
0262     if (sregs->u.e.features & KVM_SREGS_E_PC) {
0263         vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL] =
0264             sregs->u.e.ivor_high[4];
0265         vcpu->arch.ivor[BOOKE_IRQPRIO_DBELL_CRIT] =
0266             sregs->u.e.ivor_high[5];
0267     }
0268 
0269     return kvmppc_set_sregs_ivor(vcpu, sregs);
0270 }
0271 
0272 static int kvmppc_get_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
0273                   union kvmppc_one_reg *val)
0274 {
0275     int r = 0;
0276 
0277     switch (id) {
0278     case KVM_REG_PPC_SPRG9:
0279         *val = get_reg_val(id, vcpu->arch.sprg9);
0280         break;
0281     default:
0282         r = kvmppc_get_one_reg_e500_tlb(vcpu, id, val);
0283     }
0284 
0285     return r;
0286 }
0287 
0288 static int kvmppc_set_one_reg_e500mc(struct kvm_vcpu *vcpu, u64 id,
0289                   union kvmppc_one_reg *val)
0290 {
0291     int r = 0;
0292 
0293     switch (id) {
0294     case KVM_REG_PPC_SPRG9:
0295         vcpu->arch.sprg9 = set_reg_val(id, *val);
0296         break;
0297     default:
0298         r = kvmppc_set_one_reg_e500_tlb(vcpu, id, val);
0299     }
0300 
0301     return r;
0302 }
0303 
0304 static int kvmppc_core_vcpu_create_e500mc(struct kvm_vcpu *vcpu)
0305 {
0306     struct kvmppc_vcpu_e500 *vcpu_e500;
0307     int err;
0308 
0309     BUILD_BUG_ON(offsetof(struct kvmppc_vcpu_e500, vcpu) != 0);
0310     vcpu_e500 = to_e500(vcpu);
0311 
0312     /* Invalid PIR value -- this LPID doesn't have valid state on any cpu */
0313     vcpu->arch.oldpir = 0xffffffff;
0314 
0315     err = kvmppc_e500_tlb_init(vcpu_e500);
0316     if (err)
0317         return err;
0318 
0319     vcpu->arch.shared = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
0320     if (!vcpu->arch.shared) {
0321         err = -ENOMEM;
0322         goto uninit_tlb;
0323     }
0324 
0325     return 0;
0326 
0327 uninit_tlb:
0328     kvmppc_e500_tlb_uninit(vcpu_e500);
0329     return err;
0330 }
0331 
0332 static void kvmppc_core_vcpu_free_e500mc(struct kvm_vcpu *vcpu)
0333 {
0334     struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
0335 
0336     free_page((unsigned long)vcpu->arch.shared);
0337     kvmppc_e500_tlb_uninit(vcpu_e500);
0338 }
0339 
0340 static int kvmppc_core_init_vm_e500mc(struct kvm *kvm)
0341 {
0342     int lpid;
0343 
0344     lpid = kvmppc_alloc_lpid();
0345     if (lpid < 0)
0346         return lpid;
0347 
0348     /*
0349      * Use two lpids per VM on cores with two threads like e6500. Use
0350      * even numbers to speedup vcpu lpid computation with consecutive lpids
0351      * per VM. vm1 will use lpids 2 and 3, vm2 lpids 4 and 5, and so on.
0352      */
0353     if (threads_per_core == 2)
0354         lpid <<= 1;
0355 
0356     kvm->arch.lpid = lpid;
0357     return 0;
0358 }
0359 
0360 static void kvmppc_core_destroy_vm_e500mc(struct kvm *kvm)
0361 {
0362     int lpid = kvm->arch.lpid;
0363 
0364     if (threads_per_core == 2)
0365         lpid >>= 1;
0366 
0367     kvmppc_free_lpid(lpid);
0368 }
0369 
0370 static struct kvmppc_ops kvm_ops_e500mc = {
0371     .get_sregs = kvmppc_core_get_sregs_e500mc,
0372     .set_sregs = kvmppc_core_set_sregs_e500mc,
0373     .get_one_reg = kvmppc_get_one_reg_e500mc,
0374     .set_one_reg = kvmppc_set_one_reg_e500mc,
0375     .vcpu_load   = kvmppc_core_vcpu_load_e500mc,
0376     .vcpu_put    = kvmppc_core_vcpu_put_e500mc,
0377     .vcpu_create = kvmppc_core_vcpu_create_e500mc,
0378     .vcpu_free   = kvmppc_core_vcpu_free_e500mc,
0379     .init_vm = kvmppc_core_init_vm_e500mc,
0380     .destroy_vm = kvmppc_core_destroy_vm_e500mc,
0381     .emulate_op = kvmppc_core_emulate_op_e500,
0382     .emulate_mtspr = kvmppc_core_emulate_mtspr_e500,
0383     .emulate_mfspr = kvmppc_core_emulate_mfspr_e500,
0384     .create_vcpu_debugfs = kvmppc_create_vcpu_debugfs_e500,
0385 };
0386 
0387 static int __init kvmppc_e500mc_init(void)
0388 {
0389     int r;
0390 
0391     r = kvmppc_booke_init();
0392     if (r)
0393         goto err_out;
0394 
0395     /*
0396      * Use two lpids per VM on dual threaded processors like e6500
0397      * to workarround the lack of tlb write conditional instruction.
0398      * Expose half the number of available hardware lpids to the lpid
0399      * allocator.
0400      */
0401     kvmppc_init_lpid(KVMPPC_NR_LPIDS/threads_per_core);
0402 
0403     r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
0404     if (r)
0405         goto err_out;
0406     kvm_ops_e500mc.owner = THIS_MODULE;
0407     kvmppc_pr_ops = &kvm_ops_e500mc;
0408 
0409 err_out:
0410     return r;
0411 }
0412 
0413 static void __exit kvmppc_e500mc_exit(void)
0414 {
0415     kvmppc_pr_ops = NULL;
0416     kvmppc_booke_exit();
0417 }
0418 
0419 module_init(kvmppc_e500mc_init);
0420 module_exit(kvmppc_e500mc_exit);
0421 MODULE_ALIAS_MISCDEV(KVM_MINOR);
0422 MODULE_ALIAS("devname:kvm");