Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1403390rwl; Fri, 31 Mar 2023 10:33:48 -0700 (PDT) X-Google-Smtp-Source: AKy350YtT231qshrjo+DJlY3wXjc9Z2uhsGwCpZ1Se4Lu6RDB1QkasiabHYHeGZYv404JNozqlre X-Received: by 2002:a17:906:d3:b0:947:bff2:1c2b with SMTP id 19-20020a17090600d300b00947bff21c2bmr1991452eji.1.1680284027987; Fri, 31 Mar 2023 10:33:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680284027; cv=none; d=google.com; s=arc-20160816; b=gH7XEzuEyRf9wJf7Av/5ywIcymqRHi1vGdiF8c1JNpb2e5PlXxX824VWvk5s0DIsA2 0ofp06x5Nin20enwPekP3Ku0JDXq4xQViNgN8uWELakuhebkyG9MlP1BhRoG324Y8ADF aNlq2w7JUh4h++lbqfAe1jsTWnBscftKUsyonIq0cNOAqhs3iiZuGlw2ZrqFb8E90Jte KsUtcz9OWW9Gcsx2/I28umpjnCYc10AZ1izzcV4deqAia2NfebeJ3UAv5mXJIqA/w6+I MqtzA3p21D3Txu4H4vvb9ZymKI6CABH1soDg+H463Le86L+V/Xu+3bwH50hJqRrZlt30 HMpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=eAzw7TT/sjg4SXbGiPwMSsKqPPQHTtEY3GFboR2KSbo=; b=S6xh7Qo2A67KqCKI6XczWv0vfZYl5hCSJVZPjZHR2yStHlR8FeoWbrb5obanjTs8qq f71oTZdIUJPJMD887zL/fxX4ZHpZPJmkhIuJygZZab17syCwOLW+mQq2zfXXbj/DE+Iw N3/DR/5kdmJ/SG5v1m/cYDNDLbDI0HvVFZmeNYEpc9LRZ8bfD4HI/XHTmqc7WHjJRE+/ aSBLy35fY4Mwp5kePGzQFlI2g0JtbgRYEc4FdVE+1uaKN8ZPRHXLdNh1E0a873ZLcsSS 6QcFjL1kgit/l5mEU1VeLtY2s1RPuiR6OtmJ7JeHWKBpW7/5ChD8iLKXD6fXd3j4tMud llXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=gzeyNPl7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb14-20020a1709076d8e00b00947bae8c71bsi1956422ejc.916.2023.03.31.10.33.23; Fri, 31 Mar 2023 10:33:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@brainfault-org.20210112.gappssmtp.com header.s=20210112 header.b=gzeyNPl7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231929AbjCaRQD (ORCPT + 99 others); Fri, 31 Mar 2023 13:16:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbjCaRQB (ORCPT ); Fri, 31 Mar 2023 13:16:01 -0400 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 245E01BF7C for ; Fri, 31 Mar 2023 10:16:00 -0700 (PDT) Received: by mail-ed1-x52e.google.com with SMTP id w9so92398871edc.3 for ; Fri, 31 Mar 2023 10:16:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brainfault-org.20210112.gappssmtp.com; s=20210112; t=1680282958; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=eAzw7TT/sjg4SXbGiPwMSsKqPPQHTtEY3GFboR2KSbo=; b=gzeyNPl7aMroQwkhnuH9dGPyoW8udYaZzd0U7ML/vdC4+J7sclUNzM/jNdJZ1BGD55 M+UGvhyeguz14vqlRp4kqTNtrUdi+4pa7yVODVlapu245218Z9YiUeIgBi63e2M5/19v 1QKhUrIn/8qyjzomEKcIlz2/OLQ+X8c6xnsQIEY1hKzwmpRpaXx3clmtLzcB+fkZTjZ1 r56eqPJy4z4DrKCDQ/31TxX5pt9mhsbqCRZ53GUEbCS/plrOviyiXvmaL93zddm3vdAC N/jlIrYlDcCo3Ow/pExikuLl0zFRHD1+TktrgLgWY11XNPk1jBYZVn68wvFteMTU7F8w FJsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680282958; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eAzw7TT/sjg4SXbGiPwMSsKqPPQHTtEY3GFboR2KSbo=; b=CtHWBvoMmYJwcKjSLg+rhoncb84EJPulLAz2/WB91zj5MlvbC83d+WG474x/LE6hsx mlvCTNDQz5oDJDm0H+C10Ps6/ZfYC2btn0Mj9HOgOSI1JE8/0MLqUV2/tY2zkOS8KQ5y F+Io1Owvd4mbZ14u6DFWvBN5fnHR4p7toRqOXvEB0KLOnJ4WEylFgJ7shuo9X2Pmrf0O ixh6vC9171WG3NrsTGkgotQ0EarCGSWA038EFJNd6w8FHZnvn22UAhsy57MINx6vVgsS zGlAWBgAFIn20h39w50FxPzUwJ4hz6tEpSMRRQU7N943BOpI+qCs41wyIFhaZRr16ola /pYA== X-Gm-Message-State: AAQBX9dFTE8wPNqi2v177Asd6RxUu1xDjLmx9+oVctVrtpMRE5Yudey0 y4NA66VoRwngkBQObqAhY2FHYGDHFxKez8Ful5L+QQ== X-Received: by 2002:a17:907:7212:b0:946:f3f9:67b5 with SMTP id dr18-20020a170907721200b00946f3f967b5mr6589974ejc.6.1680282958562; Fri, 31 Mar 2023 10:15:58 -0700 (PDT) MIME-Version: 1.0 References: <20230128072737.2995881-1-apatel@ventanamicro.com> <20230128072737.2995881-6-apatel@ventanamicro.com> In-Reply-To: From: Anup Patel Date: Fri, 31 Mar 2023 22:45:45 +0530 Message-ID: Subject: Re: [PATCH v2 5/7] RISC-V: KVM: Add ONE_REG interface for AIA CSRs To: Atish Patra Cc: Anup Patel , Paolo Bonzini , Palmer Dabbelt , Paul Walmsley , Andrew Jones , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 8, 2023 at 5:34=E2=80=AFAM Atish Patra = wrote: > > On Fri, Jan 27, 2023 at 11:28 PM Anup Patel wro= te: > > > > We extend the CSR ONE_REG interface to access both general CSRs and > > AIA CSRs. To achieve this, we introduce "subtype" field in the ONE_REG > > id which can be used for grouping registers within a particular "type" > > of ONE_REG registers. > > > > Signed-off-by: Anup Patel > > --- > > arch/riscv/include/uapi/asm/kvm.h | 15 ++++- > > arch/riscv/kvm/vcpu.c | 96 ++++++++++++++++++++++++------- > > 2 files changed, 89 insertions(+), 22 deletions(-) > > > > diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uap= i/asm/kvm.h > > index 71992ff1f9dd..d0704eff0121 100644 > > --- a/arch/riscv/include/uapi/asm/kvm.h > > +++ b/arch/riscv/include/uapi/asm/kvm.h > > @@ -64,7 +64,7 @@ struct kvm_riscv_core { > > #define KVM_RISCV_MODE_S 1 > > #define KVM_RISCV_MODE_U 0 > > > > -/* CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ > > +/* General CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ > > struct kvm_riscv_csr { > > unsigned long sstatus; > > unsigned long sie; > > @@ -78,6 +78,10 @@ struct kvm_riscv_csr { > > unsigned long scounteren; > > }; > > > > +/* AIA CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ > > +struct kvm_riscv_aia_csr { > > +}; > > + > > /* TIMER registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ > > struct kvm_riscv_timer { > > __u64 frequency; > > @@ -105,6 +109,7 @@ enum KVM_RISCV_ISA_EXT_ID { > > KVM_RISCV_ISA_EXT_SVINVAL, > > KVM_RISCV_ISA_EXT_ZIHINTPAUSE, > > KVM_RISCV_ISA_EXT_ZICBOM, > > + KVM_RISCV_ISA_EXT_SSAIA, > > KVM_RISCV_ISA_EXT_MAX, > > }; > > > > @@ -134,6 +139,8 @@ enum KVM_RISCV_SBI_EXT_ID { > > /* If you need to interpret the index values, here is the key: */ > > #define KVM_REG_RISCV_TYPE_MASK 0x00000000FF000000 > > #define KVM_REG_RISCV_TYPE_SHIFT 24 > > +#define KVM_REG_RISCV_SUBTYPE_MASK 0x0000000000FF0000 > > +#define KVM_REG_RISCV_SUBTYPE_SHIFT 16 > > > > /* Config registers are mapped as type 1 */ > > #define KVM_REG_RISCV_CONFIG (0x01 << KVM_REG_RISCV_TYPE_SHI= FT) > > @@ -147,8 +154,12 @@ enum KVM_RISCV_SBI_EXT_ID { > > > > /* Control and status registers are mapped as type 3 */ > > #define KVM_REG_RISCV_CSR (0x03 << KVM_REG_RISCV_TYPE_SHI= FT) > > +#define KVM_REG_RISCV_CSR_GENERAL 0x0 > > +#define KVM_REG_RISCV_CSR_AIA 0x1 > > #define KVM_REG_RISCV_CSR_REG(name) \ > > - (offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned= long)) > > + (offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned long)) > > +#define KVM_REG_RISCV_CSR_AIA_REG(name) \ > > + (offsetof(struct kvm_riscv_aia_csr, name) / sizeof(unsigned lon= g)) > > > > /* Timer registers are mapped as type 4 */ > > #define KVM_REG_RISCV_TIMER (0x04 << KVM_REG_RISCV_TYPE_SHI= FT) > > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > > index 3cf50eadc8ce..37933ea20274 100644 > > --- a/arch/riscv/kvm/vcpu.c > > +++ b/arch/riscv/kvm/vcpu.c > > @@ -58,6 +58,7 @@ static const unsigned long kvm_isa_ext_arr[] =3D { > > [KVM_RISCV_ISA_EXT_I] =3D RISCV_ISA_EXT_i, > > [KVM_RISCV_ISA_EXT_M] =3D RISCV_ISA_EXT_m, > > > > + KVM_ISA_EXT_ARR(SSAIA), > > KVM_ISA_EXT_ARR(SSTC), > > KVM_ISA_EXT_ARR(SVINVAL), > > KVM_ISA_EXT_ARR(SVPBMT), > > @@ -96,6 +97,7 @@ static bool kvm_riscv_vcpu_isa_disable_allowed(unsign= ed long ext) > > case KVM_RISCV_ISA_EXT_C: > > case KVM_RISCV_ISA_EXT_I: > > case KVM_RISCV_ISA_EXT_M: > > + case KVM_RISCV_ISA_EXT_SSAIA: > > case KVM_RISCV_ISA_EXT_SSTC: > > case KVM_RISCV_ISA_EXT_SVINVAL: > > case KVM_RISCV_ISA_EXT_ZIHINTPAUSE: > > @@ -451,30 +453,79 @@ static int kvm_riscv_vcpu_set_reg_core(struct kvm= _vcpu *vcpu, > > return 0; > > } > > > > +static int kvm_riscv_vcpu_general_get_csr(struct kvm_vcpu *vcpu, > > + unsigned long reg_num, > > + unsigned long *out_val) > > +{ > > + struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; > > + > > + if (reg_num >=3D sizeof(struct kvm_riscv_csr) / sizeof(unsigned= long)) > > + return -EINVAL; > > + > > + if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) { > > + kvm_riscv_vcpu_flush_interrupts(vcpu); > > + *out_val =3D (csr->hvip >> VSIP_TO_HVIP_SHIFT) & VSIP_V= ALID_MASK; > > + } else > > + *out_val =3D ((unsigned long *)csr)[reg_num]; > > + > > + return 0; > > +} > > + > > static int kvm_riscv_vcpu_get_reg_csr(struct kvm_vcpu *vcpu, > > const struct kvm_one_reg *reg) > > { > > - struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; > > + int rc; > > unsigned long __user *uaddr =3D > > (unsigned long __user *)(unsigned long)reg->add= r; > > unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | > > KVM_REG_SIZE_MASK | > > KVM_REG_RISCV_CSR); > > - unsigned long reg_val; > > + unsigned long reg_val, reg_subtype; > > > > if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) > > return -EINVAL; > > + > > + reg_subtype =3D (reg_num & KVM_REG_RISCV_SUBTYPE_MASK) > > + >> KVM_REG_RISCV_SUBTYPE_SHIFT; > > + reg_num &=3D ~KVM_REG_RISCV_SUBTYPE_MASK; > > + switch (reg_subtype) { > > + case KVM_REG_RISCV_CSR_GENERAL: > > + rc =3D kvm_riscv_vcpu_general_get_csr(vcpu, reg_num, &r= eg_val); > > + break; > > + case KVM_REG_RISCV_CSR_AIA: > > + rc =3D kvm_riscv_vcpu_aia_get_csr(vcpu, reg_num, ®_v= al); > > + break; > > + default: > > + rc =3D -EINVAL; > > + break; > > + } > > + if (rc) > > + return rc; > > + > > + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) > > + return -EFAULT; > > + > > + return 0; > > +} > > + > > +static inline int kvm_riscv_vcpu_general_set_csr(struct kvm_vcpu *vcpu= , > > + unsigned long reg_num, > > + unsigned long reg_val) > > +{ > > + struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; > > + > > if (reg_num >=3D sizeof(struct kvm_riscv_csr) / sizeof(unsigned= long)) > > return -EINVAL; > > > > if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) { > > - kvm_riscv_vcpu_flush_interrupts(vcpu); > > - reg_val =3D (csr->hvip >> VSIP_TO_HVIP_SHIFT) & VSIP_VA= LID_MASK; > > - } else > > - reg_val =3D ((unsigned long *)csr)[reg_num]; > > + reg_val &=3D VSIP_VALID_MASK; > > + reg_val <<=3D VSIP_TO_HVIP_SHIFT; > > + } > > > > - if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) > > - return -EFAULT; > > + ((unsigned long *)csr)[reg_num] =3D reg_val; > > + > > + if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) > > + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > > > return 0; > > } > > @@ -482,31 +533,36 @@ static int kvm_riscv_vcpu_get_reg_csr(struct kvm_= vcpu *vcpu, > > static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu, > > const struct kvm_one_reg *reg) > > { > > - struct kvm_vcpu_csr *csr =3D &vcpu->arch.guest_csr; > > + int rc; > > unsigned long __user *uaddr =3D > > (unsigned long __user *)(unsigned long)reg->add= r; > > unsigned long reg_num =3D reg->id & ~(KVM_REG_ARCH_MASK | > > KVM_REG_SIZE_MASK | > > KVM_REG_RISCV_CSR); > > - unsigned long reg_val; > > + unsigned long reg_val, reg_subtype; > > > > if (KVM_REG_SIZE(reg->id) !=3D sizeof(unsigned long)) > > return -EINVAL; > > - if (reg_num >=3D sizeof(struct kvm_riscv_csr) / sizeof(unsigned= long)) > > - return -EINVAL; > > > > if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) > > return -EFAULT; > > > > - if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) { > > - reg_val &=3D VSIP_VALID_MASK; > > - reg_val <<=3D VSIP_TO_HVIP_SHIFT; > > + reg_subtype =3D (reg_num & KVM_REG_RISCV_SUBTYPE_MASK) > > + >> KVM_REG_RISCV_SUBTYPE_SHIFT; > > + reg_num &=3D ~KVM_REG_RISCV_SUBTYPE_MASK; > > + switch (reg_subtype) { > > + case KVM_REG_RISCV_CSR_GENERAL: > > + rc =3D kvm_riscv_vcpu_general_set_csr(vcpu, reg_num, re= g_val); > > + break; > > + case KVM_REG_RISCV_CSR_AIA: > > + rc =3D kvm_riscv_vcpu_aia_set_csr(vcpu, reg_num, reg_va= l); > > + break; > > + default: > > + rc =3D -EINVAL; > > + break; > > } > > - > > - ((unsigned long *)csr)[reg_num] =3D reg_val; > > - > > - if (reg_num =3D=3D KVM_REG_RISCV_CSR_REG(sip)) > > - WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); > > + if (rc) > > + return rc; > > > > return 0; > > } > > -- > > 2.34.1 > > > > This can be split into two patches. The first patch can just modify > the existing implementation > to parse subtypes for general CSRs. AIA support can be added in the next = one. Okay, I will split this into two patches. Regards, Anup