[v13,03/30] LoongArch: KVM: Implement kvm hardware enable, disable interface

Message ID 20230609090832.2131037-4-zhaotianrui@loongson.cn
State New
Headers
Series Add KVM LoongArch support |

Commit Message

zhaotianrui June 9, 2023, 9:08 a.m. UTC
  Implement kvm hardware enable, disable interface, setting
the guest config register to enable virtualization features
when called the interface.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/kvm/main.c | 64 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
  

Comments

bibo, mao June 13, 2023, 12:49 p.m. UTC | #1
在 2023/6/9 17:08, Tianrui Zhao 写道:
> Implement kvm hardware enable, disable interface, setting
> the guest config register to enable virtualization features
> when called the interface.
> 
> Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
> ---
>   arch/loongarch/kvm/main.c | 64 +++++++++++++++++++++++++++++++++++++++
>   1 file changed, 64 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index f98c1619725f..5ebae1ea7565 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -195,6 +195,70 @@ static void _kvm_init_gcsr_flag(void)
>   	set_gcsr_sw_flag(LOONGARCH_CSR_PERFCNTR3);
>   }
>   
> +void kvm_init_vmcs(struct kvm *kvm)
> +{
> +	kvm->arch.vmcs = vmcs;
> +}
> +
> +long kvm_arch_dev_ioctl(struct file *filp,
> +			unsigned int ioctl, unsigned long arg)
> +{
> +	return -ENOIOCTLCMD;
> +}
> +
> +#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
Can we remove macro CONFIG_KVM_GENERIC_HARDWARE_ENABLING here since it 
is always enabled? The other looks good to me.

Reviewed-by: Bibo Mao <maobibo@loongson.cn>

Regards
Bibo, Mao
> +int kvm_arch_hardware_enable(void)
> +{
> +	unsigned long env, gcfg = 0;
> +
> +	env = read_csr_gcfg();
> +	/* First init gtlbc, gcfg, gstat, gintc. All guest use the same config */
> +	clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
> +	write_csr_gcfg(0);
> +	write_csr_gstat(0);
> +	write_csr_gintc(0);
> +
> +	/*
> +	 * Enable virtualization features granting guest direct control of
> +	 * certain features:
> +	 * GCI=2:       Trap on init or unimplement cache instruction.
> +	 * TORU=0:      Trap on Root Unimplement.
> +	 * CACTRL=1:    Root control cache.
> +	 * TOP=0:       Trap on Previlege.
> +	 * TOE=0:       Trap on Exception.
> +	 * TIT=0:       Trap on Timer.
> +	 */
> +	if (env & CSR_GCFG_GCIP_ALL)
> +		gcfg |= CSR_GCFG_GCI_SECURE;
> +	if (env & CSR_GCFG_MATC_ROOT)
> +		gcfg |= CSR_GCFG_MATC_ROOT;
> +
> +	gcfg |= CSR_GCFG_TIT;
> +	write_csr_gcfg(gcfg);
> +
> +	kvm_flush_tlb_all();
> +
> +	/* Enable using TGID  */
> +	set_csr_gtlbc(CSR_GTLBC_USETGID);
> +	kvm_debug("gtlbc:%llx gintc:%llx gstat:%llx gcfg:%llx",
> +			read_csr_gtlbc(), read_csr_gintc(),
> +			read_csr_gstat(), read_csr_gcfg());
> +
> +	return 0;
> +}
> +
> +void kvm_arch_hardware_disable(void)
> +{
> +	clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
> +	write_csr_gcfg(0);
> +	write_csr_gstat(0);
> +	write_csr_gintc(0);
> +
> +	/* Flush any remaining guest TLB entries */
> +	kvm_flush_tlb_all();
> +}
> +#endif
> +
>   static int kvm_loongarch_env_init(void)
>   {
>   	struct kvm_context *context;
  
zhaotianrui June 19, 2023, 3:43 a.m. UTC | #2
在 2023/6/13 下午8:49, bibo, mao 写道:
>
>
> 在 2023/6/9 17:08, Tianrui Zhao 写道:
>> Implement kvm hardware enable, disable interface, setting
>> the guest config register to enable virtualization features
>> when called the interface.
>>
>> Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
>> ---
>>   arch/loongarch/kvm/main.c | 64 +++++++++++++++++++++++++++++++++++++++
>>   1 file changed, 64 insertions(+)
>>
>> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
>> index f98c1619725f..5ebae1ea7565 100644
>> --- a/arch/loongarch/kvm/main.c
>> +++ b/arch/loongarch/kvm/main.c
>> @@ -195,6 +195,70 @@ static void _kvm_init_gcsr_flag(void)
>>       set_gcsr_sw_flag(LOONGARCH_CSR_PERFCNTR3);
>>   }
>>   +void kvm_init_vmcs(struct kvm *kvm)
>> +{
>> +    kvm->arch.vmcs = vmcs;
>> +}
>> +
>> +long kvm_arch_dev_ioctl(struct file *filp,
>> +            unsigned int ioctl, unsigned long arg)
>> +{
>> +    return -ENOIOCTLCMD;
>> +}
>> +
>> +#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
> Can we remove macro CONFIG_KVM_GENERIC_HARDWARE_ENABLING here since it 
> is always enabled? The other looks good to me.
>
> Reviewed-by: Bibo Mao <maobibo@loongson.cn>
>
> Regards
> Bibo, Mao
Thanks, this CONFIG_KVM_GENERIC_HARDWARE_ENABLING is meaningless, and I 
will remove it.

Tianrui Zhao
>> +int kvm_arch_hardware_enable(void)
>> +{
>> +    unsigned long env, gcfg = 0;
>> +
>> +    env = read_csr_gcfg();
>> +    /* First init gtlbc, gcfg, gstat, gintc. All guest use the same 
>> config */
>> +    clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
>> +    write_csr_gcfg(0);
>> +    write_csr_gstat(0);
>> +    write_csr_gintc(0);
>> +
>> +    /*
>> +     * Enable virtualization features granting guest direct control of
>> +     * certain features:
>> +     * GCI=2:       Trap on init or unimplement cache instruction.
>> +     * TORU=0:      Trap on Root Unimplement.
>> +     * CACTRL=1:    Root control cache.
>> +     * TOP=0:       Trap on Previlege.
>> +     * TOE=0:       Trap on Exception.
>> +     * TIT=0:       Trap on Timer.
>> +     */
>> +    if (env & CSR_GCFG_GCIP_ALL)
>> +        gcfg |= CSR_GCFG_GCI_SECURE;
>> +    if (env & CSR_GCFG_MATC_ROOT)
>> +        gcfg |= CSR_GCFG_MATC_ROOT;
>> +
>> +    gcfg |= CSR_GCFG_TIT;
>> +    write_csr_gcfg(gcfg);
>> +
>> +    kvm_flush_tlb_all();
>> +
>> +    /* Enable using TGID  */
>> +    set_csr_gtlbc(CSR_GTLBC_USETGID);
>> +    kvm_debug("gtlbc:%llx gintc:%llx gstat:%llx gcfg:%llx",
>> +            read_csr_gtlbc(), read_csr_gintc(),
>> +            read_csr_gstat(), read_csr_gcfg());
>> +
>> +    return 0;
>> +}
>> +
>> +void kvm_arch_hardware_disable(void)
>> +{
>> +    clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
>> +    write_csr_gcfg(0);
>> +    write_csr_gstat(0);
>> +    write_csr_gintc(0);
>> +
>> +    /* Flush any remaining guest TLB entries */
>> +    kvm_flush_tlb_all();
>> +}
>> +#endif
>> +
>>   static int kvm_loongarch_env_init(void)
>>   {
>>       struct kvm_context *context;
  

Patch

diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
index f98c1619725f..5ebae1ea7565 100644
--- a/arch/loongarch/kvm/main.c
+++ b/arch/loongarch/kvm/main.c
@@ -195,6 +195,70 @@  static void _kvm_init_gcsr_flag(void)
 	set_gcsr_sw_flag(LOONGARCH_CSR_PERFCNTR3);
 }
 
+void kvm_init_vmcs(struct kvm *kvm)
+{
+	kvm->arch.vmcs = vmcs;
+}
+
+long kvm_arch_dev_ioctl(struct file *filp,
+			unsigned int ioctl, unsigned long arg)
+{
+	return -ENOIOCTLCMD;
+}
+
+#ifdef CONFIG_KVM_GENERIC_HARDWARE_ENABLING
+int kvm_arch_hardware_enable(void)
+{
+	unsigned long env, gcfg = 0;
+
+	env = read_csr_gcfg();
+	/* First init gtlbc, gcfg, gstat, gintc. All guest use the same config */
+	clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
+	write_csr_gcfg(0);
+	write_csr_gstat(0);
+	write_csr_gintc(0);
+
+	/*
+	 * Enable virtualization features granting guest direct control of
+	 * certain features:
+	 * GCI=2:       Trap on init or unimplement cache instruction.
+	 * TORU=0:      Trap on Root Unimplement.
+	 * CACTRL=1:    Root control cache.
+	 * TOP=0:       Trap on Previlege.
+	 * TOE=0:       Trap on Exception.
+	 * TIT=0:       Trap on Timer.
+	 */
+	if (env & CSR_GCFG_GCIP_ALL)
+		gcfg |= CSR_GCFG_GCI_SECURE;
+	if (env & CSR_GCFG_MATC_ROOT)
+		gcfg |= CSR_GCFG_MATC_ROOT;
+
+	gcfg |= CSR_GCFG_TIT;
+	write_csr_gcfg(gcfg);
+
+	kvm_flush_tlb_all();
+
+	/* Enable using TGID  */
+	set_csr_gtlbc(CSR_GTLBC_USETGID);
+	kvm_debug("gtlbc:%llx gintc:%llx gstat:%llx gcfg:%llx",
+			read_csr_gtlbc(), read_csr_gintc(),
+			read_csr_gstat(), read_csr_gcfg());
+
+	return 0;
+}
+
+void kvm_arch_hardware_disable(void)
+{
+	clear_csr_gtlbc(CSR_GTLBC_USETGID | CSR_GTLBC_TOTI);
+	write_csr_gcfg(0);
+	write_csr_gstat(0);
+	write_csr_gintc(0);
+
+	/* Flush any remaining guest TLB entries */
+	kvm_flush_tlb_all();
+}
+#endif
+
 static int kvm_loongarch_env_init(void)
 {
 	struct kvm_context *context;