Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1433065ybh; Fri, 13 Mar 2020 00:57:31 -0700 (PDT) X-Google-Smtp-Source: ADFU+vsnKSE1lmfvpYTRPch08Rzm1a9DiazAOfnGJrdp4gzxsU5u5Vm+2A+sEgM51rW3bhfA5vul X-Received: by 2002:a05:6830:1c7:: with SMTP id r7mr10082545ota.129.1584086251594; Fri, 13 Mar 2020 00:57:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1584086251; cv=pass; d=google.com; s=arc-20160816; b=AT56Px4Z0dFoTFuUoBlo1p0anz7F8AH4uVGotENuOKoSMxuH3Lrj1AD7/aE8yMmiBh xI9Zq4mAsA9XoWL//elgJW63B0agM5L7+j1fOHsbXDwhdIG8J6BopEnjZrtJTIQA5jMw WkhMJKGzPoAsnnpTy40YLZfrbPse+pCOzt4apga84vwBwDOhMhFZqPVQUsH4yd7f0WUg m1DL1JBRtdCAulNoL1mUBQSBB+MWeiudZ5satCCSSbI2ImdOS0v5wbPNP0oKh2IB+zZB StpFJrb5AXmzG0CeD8/Nfe7QJi41m/yXba1UVZzOCJ5mygDyN2Ytw9OFe4z8b56W2qVv P3eA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:wdcipoutbound:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :ironport-sdr:dkim-signature; bh=L7lE/atQcKIiCsruF/+taCR1HQgC5ppRlimIUjx5JN8=; b=wlpkRYAHM1UXEfJuMzs08mUnyMvfXiF6Qb2Pp935zWZjusxnJiZ4RVwGSJCWPfBtQS LVGN2sxY/siwf53QYC82GdMffLC+nEjvaqI6Q1EfAWbd6w+d69wASsQkx5K7SRm5o9Qb 53BWUCkV6omMSNdtqlZVjfchZ4hCB9xTR05Fl/k7G2CqmcUtq5fsGNuMnfx6Ms+T0IDS OjqcYeJ8mlOpzWnq17L7uMFzI3Kw23pSxyyZ3UkCUFaD6UDFlHPM1eKWxvbUkVn0/pf1 YYeAEcmBFWXC25tBwsTiPacbIbiOT9JRMVaMaCwhgSK0XGrGAliPeMtPwzLqKgycOP8a BWhA== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b="nEGkzK/6"; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=bkUF9U76; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p25si4216626oto.168.2020.03.13.00.57.19; Fri, 13 Mar 2020 00:57:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b="nEGkzK/6"; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=bkUF9U76; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726591AbgCMHzp (ORCPT + 99 others); Fri, 13 Mar 2020 03:55:45 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:9912 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726437AbgCMHzp (ORCPT ); Fri, 13 Mar 2020 03:55:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1584086144; x=1615622144; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=7hnf7789tM1CSG9CAl6JGzFMVd1cfw8Gy02HTUVLcQA=; b=nEGkzK/62n/C7MDKNgHeaqPj7QU/ElIokWFEe8mdvcqBqkQ7yqIVH6n1 3m23GWpWenSNvc1lgiP4djZ5Ka5oYlFPs+dtSBfrmXb56ZVIh5grtKUN2 6iFUM+A0udsRINekmqteCrM9w8+riTgUOqmPZgoTp4oLWBxjCyMUw+VZ3 id2bpFhY/9/TnqeFfqCOknB8K9Nure8ej/pf7hcro4IfcINYWXF02p+w6 YhH/At4chuVfkebIEO7S3anmCpx3Q0l874x+0HeBMwzfvcytZoZObiEd5 gha20d0E5BixGaQN5D6+JXPGGYtnJurVpFCuRtzOhbLcT4AfF3Lf5EhlD w==; IronPort-SDR: b4JiVQbhRSa6epkLzSXQ/zcUxhNqMeMPAyelgX0U5pXxhatyIBJHD4A/Q2+aY59LPGQoTU8fta EWKMVb59K5uyX7pwsnPuhHOwf8jQt13+z7fM8o/NRZxpav1FWQiVupG0JN01D+rB5RETO0JmpA ygnaUtsjQKzEw0Mg+cgRoY3OsdDBcE/TZ1hSPDskz/0jzNE3+peyWsxymh1wSkumCxLz4KodLg /xz1oBb8GdsS7JtfSw2gOBpd8enx1THfsmw7D1E+wom2Swsnvf/HKJbprE1b5jV7SGGuZM8xNE c7s= X-IronPort-AV: E=Sophos;i="5.70,547,1574092800"; d="scan'208";a="132374658" Received: from mail-sn1nam04lp2055.outbound.protection.outlook.com (HELO NAM04-SN1-obe.outbound.protection.outlook.com) ([104.47.44.55]) by ob1.hgst.iphmx.com with ESMTP; 13 Mar 2020 15:55:42 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RyHiydleYFU+N8ix+2M2OUImNL5hgeXamYK6tuxdaUV8oGMlL8SNEPrHTkYhzyvsBpswT91ThYIQDIoAbIe/hf3tr7+3Hi+FGDm6L3ZSWbXIdDEV8B0dJclS4qmHYjxNbqo5Nbr/ZHtwXorKSKnEJ4UOYcE9sLrEI/5eivvshQUb7bYsYVoUgvjDVmcVx7baC3y8JjPnoeqJsfOVLlunQrfsZt9aExz2mp+v988nQP7XLkh4VbUIBqOW3DNhKKPlxa3gZe9jOAK+zolzucu9VtiMIkJdbTKpIjXqwuIC5qxbkYCuTTRAbKE79IgizjNLNi7IIjk+/W/LIzubbTMS2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=L7lE/atQcKIiCsruF/+taCR1HQgC5ppRlimIUjx5JN8=; b=e2GOnWVxgRR5KoUxI9ymOBPOZgHpSORadWGifaalcNAdd6j1BnlD6TyRhC54yMrdRBTe8ZpX836BdC+bOh29ApZS2E9ORpBPDTIUltG7TVVTSfP6cr970RHWcpqhRP4oz7oRQFjneRyWNU7bkqA97JFmGmAKFM5Ce+55cMlr+vD9Tuj2Wjoca+aop5mnHxiiqdlW0td3Iq6pRmNq7vJo5CUw/GJLugbyFOEUDvYx534sH16YHbcT4xcVQjNdAx0acpa2UO7bvu9dXV8tcLrxooWD6wFOuqH81atBDb4QY1VgFu44liaSaNR0RGwtuz5SLdk9Hr9El2zrTYl7SliRHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=L7lE/atQcKIiCsruF/+taCR1HQgC5ppRlimIUjx5JN8=; b=bkUF9U76OY8NsvVK+sMWrdx/T5h2asxf6aaNoexAC0n1lZPEyR5WkfQvvoHy+FV54H6sfbQQ9kd1KJ4Cisku3Bxvx402UR8YmQ1luJJkUqJBbZjVW0qnJCKwoIcfVoLXdYGpIcWxQh77SdsdgZJr7VCa+Ui2mSYm9MoV+pDk6HA= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; Received: from MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) by MN2PR04MB6637.namprd04.prod.outlook.com (2603:10b6:208:1ef::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2793.16; Fri, 13 Mar 2020 07:55:42 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::159d:10c9:f6df:64c8]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::159d:10c9:f6df:64c8%6]) with mapi id 15.20.2814.018; Fri, 13 Mar 2020 07:55:42 +0000 From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Albert Ou , Paolo Bonzini , Radim K Cc: Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Christoph Hellwig , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Anup Patel Subject: [PATCH v11 07/20] RISC-V: KVM: Implement VCPU interrupts and requests handling Date: Fri, 13 Mar 2020 13:21:18 +0530 Message-Id: <20200313075131.69837-8-anup.patel@wdc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200313075131.69837-1-anup.patel@wdc.com> References: <20200313075131.69837-1-anup.patel@wdc.com> Content-Type: text/plain X-ClientProxiedBy: MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (1.39.129.91) by MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2814.14 via Frontend Transport; Fri, 13 Mar 2020 07:55:26 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [1.39.129.91] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 7fec1f65-c9a2-4eb2-03c2-08d7c723e952 X-MS-TrafficTypeDiagnostic: MN2PR04MB6637: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:1751; X-Forefront-PRVS: 034119E4F6 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(346002)(376002)(136003)(396003)(366004)(199004)(6666004)(1006002)(4326008)(8886007)(8676002)(81156014)(478600001)(7696005)(5660300002)(8936002)(36756003)(81166006)(52116002)(1076003)(44832011)(66476007)(26005)(316002)(66946007)(956004)(55016002)(186003)(2616005)(16526019)(54906003)(2906002)(7416002)(66556008)(86362001)(110136005)(36456003)(42976004);DIR:OUT;SFP:1102;SCL:1;SRVR:MN2PR04MB6637;H:MN2PR04MB6061.namprd04.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: y0n5iYCd9H18JYyiKoxihZ8VonOBMDaoZJAfAxCIU74wYDGMRFW7MIVduhfjqj1ALxOa4lXpQohh0TGkWEgDIGEgkuaYVoRC6ijWxIPS5bI2PL1oUzkIt0UTfySvoENMR4hjtYkbac2yimKLHdA8a9wPG9NvK2riWFdCQxPkum4FNO9p+m7a3KsA1plXzG9XLZi25wMx1zZd+mmpJHgebPZK85Amn2AkbZ555Awo5J5icxQOn9LjM4rclZ3np27CpFf6T/Lq/qGVqQubKH2X1piEsldgqmcoSef6/6f1bBkDEEyAkbcAR1giMjJ0g5ocB49Kf5W1+DoyO8BcFOVP3OThxPvGhHeBwKTfHZR3EQva8tzKH96Ycv6bgZJRDQ33GyzVY63qgY8DjJL9p0+mkjdHBvRgTRmAgouWpEkWjU8u1+JLQh2b/nwY91wlihrAIGblhFTaKCjhvewr6eCg3Ob21xPCCZw9Ww8dFQgb/ssV1VJH/xqIHjqOlsarOvbl X-MS-Exchange-AntiSpam-MessageData: vp9KTg0JN+NHEhnXc/YZnwp8trDz7oYIpvtXPRuGqbOZHm/YmDkgqO3CplZJtWNxyJtZWH+tmrLPaHnYulZkGy/9ZpCklvsOPkgw8+SRcLwVMkhiX2LgxAGFlW7yyiCLUHveHCgIcqHl6iEcpQjpTQ== X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7fec1f65-c9a2-4eb2-03c2-08d7c723e952 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2020 07:55:42.1351 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +04zLKEpHTPaz9YWHGHS8M3XZ8zFNKaVU+f8qKocCNlqKIhOZIGbgDlOOhYv2ZzYSa7u4cqsDrED1BU2hS4dxA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6637 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch implements VCPU interrupts and requests which are both asynchronous events. The VCPU interrupts can be set/unset using KVM_INTERRUPT ioctl from user-space. In future, the in-kernel IRQCHIP emulation will use kvm_riscv_vcpu_set_interrupt() and kvm_riscv_vcpu_unset_interrupt() functions to set/unset VCPU interrupts. Important VCPU requests implemented by this patch are: KVM_REQ_SLEEP - set whenever VCPU itself goes to sleep state KVM_REQ_VCPU_RESET - set whenever VCPU reset is requested The WFI trap-n-emulate (added later) will use KVM_REQ_SLEEP request and kvm_riscv_vcpu_has_interrupt() function. The KVM_REQ_VCPU_RESET request will be used by SBI emulation (added later) to power-up a VCPU in power-off state. The user-space can use the GET_MPSTATE/SET_MPSTATE ioctls to get/set power state of a VCPU. Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini Reviewed-by: Alexander Graf --- arch/riscv/include/asm/kvm_host.h | 23 ++++ arch/riscv/include/uapi/asm/kvm.h | 3 + arch/riscv/kvm/vcpu.c | 182 +++++++++++++++++++++++++++--- 3 files changed, 195 insertions(+), 13 deletions(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 297431660be6..94ae81220ac6 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -122,6 +122,21 @@ struct kvm_vcpu_arch { /* CPU CSR context upon Guest VCPU reset */ struct kvm_vcpu_csr guest_reset_csr; + /* + * VCPU interrupts + * + * We have a lockless approach for tracking pending VCPU interrupts + * implemented using atomic bitops. The irqs_pending bitmap represent + * pending interrupts whereas irqs_pending_mask represent bits changed + * in irqs_pending. Our approach is modeled around multiple producer + * and single consumer problem where the consumer is the VCPU itself. + */ + unsigned long irqs_pending; + unsigned long irqs_pending_mask; + + /* VCPU power-off state */ + bool power_off; + /* Don't run the VCPU (blocked) */ bool pause; @@ -147,4 +162,12 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, static inline void __kvm_riscv_switch_to(struct kvm_vcpu_arch *vcpu_arch) {} +int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq); +int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq); +void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu); +bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, unsigned long mask); +void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu); + #endif /* __RISCV_KVM_HOST_H__ */ diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index d15875818b6e..6dbc056d58ba 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -18,6 +18,9 @@ #define KVM_COALESCED_MMIO_PAGE_OFFSET 1 +#define KVM_INTERRUPT_SET -1U +#define KVM_INTERRUPT_UNSET -2U + /* for KVM_GET_REGS and KVM_SET_REGS */ struct kvm_regs { }; diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index f46d6461a174..2e14f8b8bdea 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,9 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) memcpy(csr, reset_csr, sizeof(*csr)); memcpy(cntx, reset_cntx, sizeof(*cntx)); + + WRITE_ONCE(vcpu->arch.irqs_pending, 0); + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); } int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) @@ -98,8 +102,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return kvm_riscv_vcpu_has_interrupts(vcpu, 1UL << IRQ_VS_TIMER); } void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) @@ -112,20 +115,18 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return (kvm_riscv_vcpu_has_interrupts(vcpu, -1UL) && + !vcpu->arch.power_off && !vcpu->arch.pause); } int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { - /* TODO: */ - return 0; + return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; } bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { - /* TODO: */ - return false; + return (vcpu->arch.guest_context.sstatus & SR_SPP) ? true : false; } bool kvm_arch_has_vcpu_debugfs(void) @@ -146,7 +147,21 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) long kvm_arch_vcpu_async_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { - /* TODO; */ + struct kvm_vcpu *vcpu = filp->private_data; + void __user *argp = (void __user *)arg; + + if (ioctl == KVM_INTERRUPT) { + struct kvm_interrupt irq; + + if (copy_from_user(&irq, argp, sizeof(irq))) + return -EFAULT; + + if (irq.irq == KVM_INTERRUPT_SET) + return kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_VS_EXT); + else + return kvm_riscv_vcpu_unset_interrupt(vcpu, IRQ_VS_EXT); + } + return -ENOIOCTLCMD; } @@ -195,18 +210,121 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) return -EINVAL; } +void kvm_riscv_vcpu_flush_interrupts(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + unsigned long mask, val; + + if (READ_ONCE(vcpu->arch.irqs_pending_mask)) { + mask = xchg_acquire(&vcpu->arch.irqs_pending_mask, 0); + val = READ_ONCE(vcpu->arch.irqs_pending) & mask; + + csr->hip &= ~mask; + csr->hip |= val; + } +} + +void kvm_riscv_vcpu_sync_interrupts(struct kvm_vcpu *vcpu) +{ + unsigned long hip; + struct kvm_vcpu_arch *v = &vcpu->arch; + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + /* Read current HIP and HIE CSRs */ + hip = csr_read(CSR_HIP); + csr->hie = csr_read(CSR_HIE); + + /* Sync-up HIP.VSSIP bit changes does by Guest */ + if ((csr->hip ^ hip) & (1UL << IRQ_VS_SOFT)) { + if (hip & (1UL << IRQ_VS_SOFT)) { + if (!test_and_set_bit(IRQ_VS_SOFT, + &v->irqs_pending_mask)) + set_bit(IRQ_VS_SOFT, &v->irqs_pending); + } else { + if (!test_and_set_bit(IRQ_VS_SOFT, + &v->irqs_pending_mask)) + clear_bit(IRQ_VS_SOFT, &v->irqs_pending); + } + } +} + +int kvm_riscv_vcpu_set_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) +{ + if (irq != IRQ_VS_SOFT && + irq != IRQ_VS_TIMER && + irq != IRQ_VS_EXT) + return -EINVAL; + + set_bit(irq, &vcpu->arch.irqs_pending); + smp_mb__before_atomic(); + set_bit(irq, &vcpu->arch.irqs_pending_mask); + + kvm_vcpu_kick(vcpu); + + return 0; +} + +int kvm_riscv_vcpu_unset_interrupt(struct kvm_vcpu *vcpu, unsigned int irq) +{ + if (irq != IRQ_VS_SOFT && + irq != IRQ_VS_TIMER && + irq != IRQ_VS_EXT) + return -EINVAL; + + clear_bit(irq, &vcpu->arch.irqs_pending); + smp_mb__before_atomic(); + set_bit(irq, &vcpu->arch.irqs_pending_mask); + + return 0; +} + +bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, unsigned long mask) +{ + return (READ_ONCE(vcpu->arch.irqs_pending) & + vcpu->arch.guest_csr.hie & mask) ? true : false; +} + +void kvm_riscv_vcpu_power_off(struct kvm_vcpu *vcpu) +{ + vcpu->arch.power_off = true; + kvm_make_request(KVM_REQ_SLEEP, vcpu); + kvm_vcpu_kick(vcpu); +} + +void kvm_riscv_vcpu_power_on(struct kvm_vcpu *vcpu) +{ + vcpu->arch.power_off = false; + kvm_vcpu_wake_up(vcpu); +} + int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - /* TODO: */ + if (vcpu->arch.power_off) + mp_state->mp_state = KVM_MP_STATE_STOPPED; + else + mp_state->mp_state = KVM_MP_STATE_RUNNABLE; + return 0; } int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, struct kvm_mp_state *mp_state) { - /* TODO: */ - return 0; + int ret = 0; + + switch (mp_state->mp_state) { + case KVM_MP_STATE_RUNNABLE: + vcpu->arch.power_off = false; + break; + case KVM_MP_STATE_STOPPED: + kvm_riscv_vcpu_power_off(vcpu); + break; + default: + ret = -EINVAL; + } + + return ret; } int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, @@ -230,7 +348,33 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu) { - /* TODO: */ + struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu); + + if (kvm_request_pending(vcpu)) { + if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) { + swait_event_interruptible_exclusive(*wq, + ((!vcpu->arch.power_off) && + (!vcpu->arch.pause))); + + if (vcpu->arch.power_off || vcpu->arch.pause) { + /* + * Awaken to handle a signal, request to + * sleep again later. + */ + kvm_make_request(KVM_REQ_SLEEP, vcpu); + } + } + + if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) + kvm_riscv_reset_vcpu(vcpu); + } +} + +static void kvm_riscv_update_hip(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + + csr_write(CSR_HIP, csr->hip); } int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) @@ -293,6 +437,15 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) srcu_read_unlock(&vcpu->kvm->srcu, vcpu->arch.srcu_idx); smp_mb__after_srcu_read_unlock(); + /* + * We might have got VCPU interrupts updated asynchronously + * so update it in HW. + */ + kvm_riscv_vcpu_flush_interrupts(vcpu); + + /* Update HIP CSR for current CPU */ + kvm_riscv_update_hip(vcpu); + if (ret <= 0 || kvm_request_pending(vcpu)) { vcpu->mode = OUTSIDE_GUEST_MODE; @@ -319,6 +472,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) htval = csr_read(CSR_HTVAL); htinst = csr_read(CSR_HTINST); + /* Syncup interrupts state with HW */ + kvm_riscv_vcpu_sync_interrupts(vcpu); + /* * We may have taken a host interrupt in VS/VU-mode (i.e. * while executing the guest). This interrupt is still -- 2.17.1