From patchwork Wed May 17 10:51:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 95171 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1044681vqo; Wed, 17 May 2023 04:06:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4v4YGKLfFVTiZFhqiKIL/khGHS+UFQlU29hymZm7sdASKzxMeKJC11ILq+ldAUTfb+31n4 X-Received: by 2002:a17:902:c113:b0:1ae:56ff:758 with SMTP id 19-20020a170902c11300b001ae56ff0758mr992474pli.19.1684321604126; Wed, 17 May 2023 04:06:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684321604; cv=none; d=google.com; s=arc-20160816; b=yEMRhhy9M8IunWjq2pIDahu0RulqVfJNsb0vvRyMYcL5z7DmuN54ZAS1w71vic+CIE cNWupe340yhiVB41V/Mr1gIS2vYX6BFxiXKTzddMJKi9ZhPpG4cYWho/OWa+aGe5BR95 JXyV5OaAw0L15Gx6d1FC7KrVRfsE5ACn8vKfVp4lRJeThpmeN6pZuRkDknttedY0YBSe I5Pje19M9powWW7GjZ/I4OWHlBW8iH6fE6uX0LCRQ7NjQ7tqex7gXVQdK0FUuN6u+TMg QMD1J5UYigiIaMM1XlnqM7kqYU+Gyrqp2JF7Ona4FJUZUSJglCRGkKPYAvsTZvjK0czN XLCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kUsqFBRSizZOBbNz09TlCKcbFhTjGXkGkHhT7XXk9dc=; b=SEOYaEhYwuF/nOTlX0vsQfLCEhOEGR7PSTpnd5fxSLpnXL/Qh+V25aldNlA/v3Iie/ CxSAFU33qNnDnj1RbGUP2rajVHhXW3a+zVt9fZud9p6KDIv8YH184G59qfUx8cOjV6Gi L4bvLrTeUr2onjy++k5Uoc3ICC4FG6emsLGo+kyyETuzVVmEcGMv4xSeBz2LJTYiUQQZ +nQQuc7Jv+LsCCxRCUxLTwWFz4TYWAjjat6PsrSbIsquXhfeJVDLSs6pqOljdlluRuHw oElWPAIS3N1TokB5qX61dESvVtFrlmaPp+WNHYD7RDiROTj8kwXPk86LXXNEQCB4qCv/ EE3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ventanamicro.com header.s=google header.b="UbgZS/Tz"; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 13-20020a170902c20d00b001ab259969e7si20459477pll.31.2023.05.17.04.06.28; Wed, 17 May 2023 04:06:44 -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=@ventanamicro.com header.s=google header.b="UbgZS/Tz"; 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 S230129AbjEQKxQ (ORCPT + 99 others); Wed, 17 May 2023 06:53:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230472AbjEQKxM (ORCPT ); Wed, 17 May 2023 06:53:12 -0400 Received: from mail-ot1-x32f.google.com (mail-ot1-x32f.google.com [IPv6:2607:f8b0:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 616916E8E for ; Wed, 17 May 2023 03:52:44 -0700 (PDT) Received: by mail-ot1-x32f.google.com with SMTP id 46e09a7af769-6aaf9092c52so266384a34.1 for ; Wed, 17 May 2023 03:52:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1684320747; x=1686912747; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kUsqFBRSizZOBbNz09TlCKcbFhTjGXkGkHhT7XXk9dc=; b=UbgZS/Tz5hnb69o2g+omOdrV7tfYg+sEssYVWHXO/bJ+7Blf4jWHzp7/ULMtpNODbx ewic8n6XLnfOl2E9hv0RmHdpHNd7uFFnl+LYnKVUnqmOry4HBNI71om8UMKpRSss7/nc a1FEZ2C0SEs9Q129ddmu1hKZVe4YlPc31Bzvwnsb2WCTgHlk5QlsR86abLM+AjoNcniH Bv2xJUPQXogOqUkidVXnHfidhDGrHvW/s/iZ06W/7zChZry7rtJsQa6mL8A2QFj/JL/O zJA3+GU6TC3ke6HHZk+rgcErvZh2GpTsjVra2i0Km4eQNZ4tnLL6hzfedT2xPEKGLpr3 cWQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684320747; x=1686912747; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kUsqFBRSizZOBbNz09TlCKcbFhTjGXkGkHhT7XXk9dc=; b=MnmyTQ33KewhHBOA/JftfCQBPDAjiDRlz/JON4a8Q6YK9xFd23cXNaquVsrfRrJsET 6BtetV/vv/m3LluV0zBlbFSRVV+3R2lW55fgj5nM8Db1lN6dgykhk015my/zlsMNuE7H ViVvvIWbwX3Ro6FpUL9GQc8nf+O7v4f7l2dmvL/XDZ1CJN4MRFR3ptboBWoVnsVz7DDT 3EtOZaWHVHsHOdeQdKDVeN7SzaBvYC34h2YdLtDkaG7ckrox4aImGNU4tz+DRPQ3vgIG +rFzRa6rXTKY6qG1l0NTwwTD/IbG1DQFd5t1Vwb3kaTfQYBcS3CzFDKMB6FTvs3tHsU2 Q1Zg== X-Gm-Message-State: AC+VfDy/cWfDZTb/Hk4Q2Er0jgtny/jfFDfEM7cxt/iU0UUVfnBF6s5i aeysdd/0gcPB78KSeTOXO85CyQ== X-Received: by 2002:a05:6830:55:b0:6ad:8322:7460 with SMTP id d21-20020a056830005500b006ad83227460mr6048951otp.9.1684320747057; Wed, 17 May 2023 03:52:27 -0700 (PDT) Received: from anup-ubuntu-vm.localdomain ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id w1-20020a9d77c1000000b006ade3815527sm2279896otl.22.2023.05.17.03.52.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 03:52:26 -0700 (PDT) From: Anup Patel To: Paolo Bonzini , Atish Patra Cc: 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, Anup Patel Subject: [PATCH 08/10] RISC-V: KVM: Expose APLIC registers as attributes of AIA irqchip Date: Wed, 17 May 2023 16:21:33 +0530 Message-Id: <20230517105135.1871868-9-apatel@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230517105135.1871868-1-apatel@ventanamicro.com> References: <20230517105135.1871868-1-apatel@ventanamicro.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766139210442461878?= X-GMAIL-MSGID: =?utf-8?q?1766139210442461878?= We expose APLIC registers as KVM device attributes of the in-kernel AIA irqchip device. This will allow KVM user-space to save/restore APLIC state using KVM device ioctls(). Signed-off-by: Anup Patel --- arch/riscv/include/asm/kvm_aia.h | 3 +++ arch/riscv/include/uapi/asm/kvm.h | 2 ++ arch/riscv/kvm/aia_aplic.c | 43 +++++++++++++++++++++++++++++++ arch/riscv/kvm/aia_device.c | 25 ++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/arch/riscv/include/asm/kvm_aia.h b/arch/riscv/include/asm/kvm_aia.h index f6bd8523395f..ba939c0054aa 100644 --- a/arch/riscv/include/asm/kvm_aia.h +++ b/arch/riscv/include/asm/kvm_aia.h @@ -129,6 +129,9 @@ static inline void kvm_riscv_vcpu_aia_imsic_cleanup(struct kvm_vcpu *vcpu) { } +int kvm_riscv_aia_aplic_set_attr(struct kvm *kvm, unsigned long type, u32 v); +int kvm_riscv_aia_aplic_get_attr(struct kvm *kvm, unsigned long type, u32 *v); +int kvm_riscv_aia_aplic_has_attr(struct kvm *kvm, unsigned long type); int kvm_riscv_aia_aplic_inject(struct kvm *kvm, u32 source, bool level); int kvm_riscv_aia_aplic_init(struct kvm *kvm); void kvm_riscv_aia_aplic_cleanup(struct kvm *kvm); diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index 57f8d8bb498e..e80210c2220b 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -240,6 +240,8 @@ enum KVM_RISCV_SBI_EXT_ID { #define KVM_DEV_RISCV_AIA_GRP_CTRL 2 #define KVM_DEV_RISCV_AIA_CTRL_INIT 0 +#define KVM_DEV_RISCV_AIA_GRP_APLIC 3 + /* One single KVM irqchip, ie. the AIA */ #define KVM_NR_IRQCHIPS 1 diff --git a/arch/riscv/kvm/aia_aplic.c b/arch/riscv/kvm/aia_aplic.c index 1b0a4df64815..ed9102dfba77 100644 --- a/arch/riscv/kvm/aia_aplic.c +++ b/arch/riscv/kvm/aia_aplic.c @@ -499,6 +499,49 @@ static struct kvm_io_device_ops aplic_iodoev_ops = { .write = aplic_mmio_write, }; +int kvm_riscv_aia_aplic_set_attr(struct kvm *kvm, unsigned long type, u32 v) +{ + int rc; + + if (!kvm->arch.aia.aplic_state) + return -ENODEV; + + rc = aplic_mmio_write_offset(kvm, type, v); + if (rc) + return rc; + + return 0; +} + +int kvm_riscv_aia_aplic_get_attr(struct kvm *kvm, unsigned long type, u32 *v) +{ + int rc; + + if (!kvm->arch.aia.aplic_state) + return -ENODEV; + + rc = aplic_mmio_read_offset(kvm, type, v); + if (rc) + return rc; + + return 0; +} + +int kvm_riscv_aia_aplic_has_attr(struct kvm *kvm, unsigned long type) +{ + int rc; + u32 val; + + if (!kvm->arch.aia.aplic_state) + return -ENODEV; + + rc = aplic_mmio_read_offset(kvm, type, &val); + if (rc) + return rc; + + return 0; +} + int kvm_riscv_aia_aplic_init(struct kvm *kvm) { int i, ret = 0; diff --git a/arch/riscv/kvm/aia_device.c b/arch/riscv/kvm/aia_device.c index a151fb357887..17dba92a90e1 100644 --- a/arch/riscv/kvm/aia_device.c +++ b/arch/riscv/kvm/aia_device.c @@ -364,6 +364,15 @@ static int aia_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) break; } + break; + case KVM_DEV_RISCV_AIA_GRP_APLIC: + if (copy_from_user(&nr, uaddr, sizeof(nr))) + return -EFAULT; + + mutex_lock(&dev->kvm->lock); + r = kvm_riscv_aia_aplic_set_attr(dev->kvm, type, nr); + mutex_unlock(&dev->kvm->lock); + break; } @@ -411,6 +420,20 @@ static int aia_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) if (copy_to_user(uaddr, &addr, sizeof(addr))) return -EFAULT; + break; + case KVM_DEV_RISCV_AIA_GRP_APLIC: + if (copy_from_user(&nr, uaddr, sizeof(nr))) + return -EFAULT; + + mutex_lock(&dev->kvm->lock); + r = kvm_riscv_aia_aplic_get_attr(dev->kvm, type, &nr); + mutex_unlock(&dev->kvm->lock); + if (r) + return r; + + if (copy_to_user(uaddr, &nr, sizeof(nr))) + return -EFAULT; + break; } @@ -447,6 +470,8 @@ static int aia_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr) return 0; } break; + case KVM_DEV_RISCV_AIA_GRP_APLIC: + return kvm_riscv_aia_aplic_has_attr(dev->kvm, attr->attr); } return -ENXIO;