From patchwork Sun Jul 21 07:05:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13737950 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D59D79CF for ; Sun, 21 Jul 2024 07:06:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545574; cv=none; b=YLoEPBuks0Ur2LLeBvbIzp8lNwj57cX4FMldyLpBAlxELMUeegsEbzJerHvU2wWxuQCwrps199IcXe6dJ/oIkhuk4lmpr3LbgyEtKpPsKytlPOULiW811uryG0HJzMoDhUHuq1Hj26jR8n+pqGcSozX7lB3tFAZGSQorsmGPJ+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545574; c=relaxed/simple; bh=Qogls816cd0Tv53VgYejA6+L3RDe17CCmGQ0+3vemKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pGIL1VJWkvzwHfCatWE2NUCaU3WAqpABEaYnWFtZ19Cx5yP1zK/4jljMGrUw2ReZfOrpTt+92QfmdxfBcbfnK/VJAHHrl5Ou80bNELU1uCSkRK8HQz2oOsQtAAcQMC77ogAXrx7Hv6cuI4CPfBC5JqBBZeHXZ2Vi1JN3bd0cTKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MB6w8jw0; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MB6w8jw0" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2cb576db1c5so1596767a91.1 for ; Sun, 21 Jul 2024 00:06:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721545571; x=1722150371; darn=vger.kernel.org; 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=joTVeUit/9PHPnwE5WAgfkXToAh6iydIyxEEP0jrhcY=; b=MB6w8jw0vXWOgcSCkEkcL9ebl4Djh8Xgby938FJXCdPrXpMbBfo1xUYL9ccoU7PKVY watZPBFf4WTwjXzo02mrwOp+YywL83kfnVfctgBcPGMrm2dUL8BhNTlQ90ZE7DYGQNP3 l0c440C6P1eRyQwlYPd3YCLlPW6zIfj3uuwihMv20/spH++Kw4n3y+r0nSp+yjsQKYaH fNSD6bUG3KSjlxBOYCB7Tzfji65/zhXRJHGG9KPBJRW5Nm+ibAMc3OiYGhUr+NFwoe3v KA+HvJ7VvZpGKmnJIwmHZUm5eC9bjVmagDa+XN4Byz6KdkcSnS2LseNm9YMXjsMl9XAv YaOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721545571; x=1722150371; 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=joTVeUit/9PHPnwE5WAgfkXToAh6iydIyxEEP0jrhcY=; b=g2B3oGHNjHIXwLnZOtpbZfkETH+Mqb4AP6GhvOHfVKH59e49TFgttVd2QtyuwbUYKb J4B85CXVXg3i8pUSSvMNzMk9tx/LBBssHQBG6JFh7+Mss1JX0w8lSntv3Zkk00Z1RljF pqy9btRJ6aj2qW+oUz7MM/QqUDZokOOXgM6PxTamrcrRDLD5+qWzMLJnJONCNpnii+fJ zpKuOWc5Qq8KLLZVgcwYIxOJKo73/ciWqoYLcRKjinvFzUoQkEGa/xAte7X7B8ZdXzI6 W5lypwq1MhorTaYIugjt9jvVjREjhjArqS+ksMdaNUZ/0+98gF3IXEmr8iABl1Yl47UF Mkdg== X-Gm-Message-State: AOJu0YxDqOa/Y3IbimX+/i6IoxeDFt6S6wKMiBhGcsF521VJqg51ToBB 8qRnBGC3yOWpLywQUIVddTy0FGY28VuHyac4+CsCVMgqfDPU73Sc6KnDLF9i X-Google-Smtp-Source: AGHT+IEqI9cCP4Pdc2FIOcmfu9rOjcP9n3Yr6xF4YtyKrxt83JH/A9bxv3jEpP6oIW60powen0eVng== X-Received: by 2002:a05:6a21:3a86:b0:1c2:8d16:c681 with SMTP id adf61e73a8af0-1c4285f2e45mr2064832637.34.1721545571314; Sun, 21 Jul 2024 00:06:11 -0700 (PDT) Received: from JRT-PC.. ([180.255.73.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cb77492bc6sm4891461a91.1.2024.07.21.00.06.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jul 2024 00:06:10 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v4 1/5] riscv: Extend exception handling support for interrupts Date: Sun, 21 Jul 2024 15:05:56 +0800 Message-ID: <20240721070601.88639-2-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240721070601.88639-1-jamestiotio@gmail.com> References: <20240721070601.88639-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Andrew Jones Add install_irq_handler() to enable tests to install interrupt handlers. Also add local_irq_enable() and local_irq_disable() to respectively enable and disable IRQs via the sstatus.SIE bit. Signed-off-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 2 ++ lib/riscv/asm/processor.h | 13 +++++++++++++ lib/riscv/processor.c | 27 +++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index 52608512..d6909d93 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -11,6 +11,8 @@ #define CSR_STVAL 0x143 #define CSR_SATP 0x180 +#define SR_SIE _AC(0x00000002, UL) + /* Exception cause high bit - is an interrupt if set */ #define CAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1)) diff --git a/lib/riscv/asm/processor.h b/lib/riscv/asm/processor.h index 32c499d0..6451adb5 100644 --- a/lib/riscv/asm/processor.h +++ b/lib/riscv/asm/processor.h @@ -5,6 +5,7 @@ #include #define EXCEPTION_CAUSE_MAX 16 +#define INTERRUPT_CAUSE_MAX 16 typedef void (*exception_fn)(struct pt_regs *); @@ -13,6 +14,7 @@ struct thread_info { unsigned long hartid; unsigned long isa[1]; exception_fn exception_handlers[EXCEPTION_CAUSE_MAX]; + exception_fn interrupt_handlers[INTERRUPT_CAUSE_MAX]; }; static inline struct thread_info *current_thread_info(void) @@ -20,7 +22,18 @@ static inline struct thread_info *current_thread_info(void) return (struct thread_info *)csr_read(CSR_SSCRATCH); } +static inline void local_irq_enable(void) +{ + csr_set(CSR_SSTATUS, SR_SIE); +} + +static inline void local_irq_disable(void) +{ + csr_clear(CSR_SSTATUS, SR_SIE); +} + void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *)); +void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *)); void do_handle_exception(struct pt_regs *regs); void thread_info_init(void); diff --git a/lib/riscv/processor.c b/lib/riscv/processor.c index ece7cbff..0dffadc7 100644 --- a/lib/riscv/processor.c +++ b/lib/riscv/processor.c @@ -36,10 +36,21 @@ void do_handle_exception(struct pt_regs *regs) { struct thread_info *info = current_thread_info(); - assert(regs->cause < EXCEPTION_CAUSE_MAX); - if (info->exception_handlers[regs->cause]) { - info->exception_handlers[regs->cause](regs); - return; + if (regs->cause & CAUSE_IRQ_FLAG) { + unsigned long irq_cause = regs->cause & ~CAUSE_IRQ_FLAG; + + assert(irq_cause < INTERRUPT_CAUSE_MAX); + if (info->interrupt_handlers[irq_cause]) { + info->interrupt_handlers[irq_cause](regs); + return; + } + } else { + assert(regs->cause < EXCEPTION_CAUSE_MAX); + + if (info->exception_handlers[regs->cause]) { + info->exception_handlers[regs->cause](regs); + return; + } } show_regs(regs); @@ -47,6 +58,14 @@ void do_handle_exception(struct pt_regs *regs) abort(); } +void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *)) +{ + struct thread_info *info = current_thread_info(); + + assert(cause < INTERRUPT_CAUSE_MAX); + info->interrupt_handlers[cause] = handler; +} + void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *)) { struct thread_info *info = current_thread_info(); From patchwork Sun Jul 21 07:05:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13737953 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97B9579DE for ; Sun, 21 Jul 2024 07:06:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545605; cv=none; b=ac59kjJMLtFNJ2PppOOWJssSnkoBT6r6kwoqvfwvjFHT4aSiL1DFTcVK0g93PcRVZgXK3F7rH9HNVqsaJ1sCk0Sli+5ul/CY13lOkvhOVXcoOuc1AW52qVxnGV509AjiucobMQpFk7Is+sHNfRWXPGxk3OrGtwGn9KBvpH1FPlo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545605; c=relaxed/simple; bh=+n3qbfDTrboEdcvpcY3rZAfV/jBQ7snQ9EH7EftH8lQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sn6LUj38owVQVw6M7AFvEkgxiktrRNmBHWFb3cVs8gRpfXlpym+8yKqQOX4CXZ+r3FglH5knQGFTQ8BVsmAir3iiWNkzLm18LsswANqE8TncKMHBQ2Wlt2DgRhtfC4my7igP+2iEIam+EBsuyG7dP+nzPzg7l2/ZQyp15RrpVaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=R7r5wanU; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R7r5wanU" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-70d162eef54so297855b3a.3 for ; Sun, 21 Jul 2024 00:06:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721545603; x=1722150403; darn=vger.kernel.org; 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=YOqEXJYpkIexGhOwSJXzU52SvIkIRl2QtZdZCBi6Nm8=; b=R7r5wanUgqgcUjBr6XxYRYFGuQ2v3TimpNoW5EkmHdyaBHupnNgmbXdYhzm0WwOBI2 K/G4QoivkM2CmoHYAkrq7ox3GhQDPfXUq/4HdGf74YXKhl8Wkn3peOtmiSZ06mnplHT7 2NtG1dGoluaLGmRW1Jwv8JAiB2/dUva5dP5bznU7zsze6PS2oCIP5ahG2PsUcbTdto/C C2ME9mhRXt6e+RpDcxJQxBVDf1lJVYDwR4iZC9KKA/lzxQF80H5X3nIQVEPcdgZe8aRd PI56K4ZpTVluEG+M18HHpIBu2VNmzu1giet3rTZEs/v/6oeXhZI+9xx2YPrm+d/f121Q WuPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721545603; x=1722150403; 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=YOqEXJYpkIexGhOwSJXzU52SvIkIRl2QtZdZCBi6Nm8=; b=qOAhXkH+/coJIaxjs09SO7A1xCRvgYsK/HTfZDc7lwhsG23A7e0G4va7X2WswMfTXy svAsYOhtCgWv+Uc3g7Zj0OdTOy4LHUmfTxUK1OKU1n5FQ7D/7bKU/jopu03wtWV4fedX itcZn1gwqtwFHPJAxC6b9CaRyWQpPl/YjUgKGHOz+ta4KBri03kxZe0BLUKttB7GZR0Y jkCzoDTYNBbj0rYYOj8oc/nqCsDNL/12vnLFXLi969s85Tyyz1rhApG8rXdsqK86b6tK G/PlyYkxq+fNsfINBvUa7wuW8/Ziih9EY40lKj4/NBsydPtSoqm2pXycskfheNlz47Ov EklQ== X-Gm-Message-State: AOJu0YyPBhkxxA5Agdhj7KJw9VLQQzfRaYG5ahyJNUUKlRyXy494BoU5 o/D2W2WxZLIsOn9IDBrcACdN0N+QngQOy0jOuGPxyzhlaq1dwABgU8j3obw5 X-Google-Smtp-Source: AGHT+IE3oS77IFsILowoEEOxHGX2hEBQPecGS32oyXyjCa+VZh+RcoTDPe3ttc2tgNOV3HU0a/svFA== X-Received: by 2002:a05:6a20:12cb:b0:1c3:b0d6:e496 with SMTP id adf61e73a8af0-1c4285ef1c1mr2301170637.45.1721545573585; Sun, 21 Jul 2024 00:06:13 -0700 (PDT) Received: from JRT-PC.. ([180.255.73.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cb77492bc6sm4891461a91.1.2024.07.21.00.06.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jul 2024 00:06:13 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v4 2/5] riscv: Update exception cause list Date: Sun, 21 Jul 2024 15:05:57 +0800 Message-ID: <20240721070601.88639-3-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240721070601.88639-1-jamestiotio@gmail.com> References: <20240721070601.88639-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Update the list of exception and interrupt causes to follow the latest RISC-V privileged ISA specification (version 20240411 section 18.6.1). Reviewed-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 10 ++++++++++ lib/riscv/asm/processor.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index d6909d93..ba810c9f 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -36,6 +36,16 @@ #define EXC_VIRTUAL_INST_FAULT 22 #define EXC_STORE_GUEST_PAGE_FAULT 23 +/* Interrupt causes */ +#define IRQ_S_SOFT 1 +#define IRQ_VS_SOFT 2 +#define IRQ_S_TIMER 5 +#define IRQ_VS_TIMER 6 +#define IRQ_S_EXT 9 +#define IRQ_VS_EXT 10 +#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13 + #ifndef __ASSEMBLY__ #define csr_swap(csr, val) \ diff --git a/lib/riscv/asm/processor.h b/lib/riscv/asm/processor.h index 6451adb5..4c9ad968 100644 --- a/lib/riscv/asm/processor.h +++ b/lib/riscv/asm/processor.h @@ -4,7 +4,7 @@ #include #include -#define EXCEPTION_CAUSE_MAX 16 +#define EXCEPTION_CAUSE_MAX 24 #define INTERRUPT_CAUSE_MAX 16 typedef void (*exception_fn)(struct pt_regs *); From patchwork Sun Jul 21 07:05:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13737954 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C03F88C0B for ; Sun, 21 Jul 2024 07:06:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545608; cv=none; b=fC5jneVnBChmmjnaxMr3alXtDZECP9t+S0dp9FfdCa4bwLwWlcUfdIRim5iwX2nD8ID9X6/lqfU9hwpDsKz6/1m1MtmOxtRHilIcguYWnCIbO07RMFUE7CUeDJEtFFu3yjTaV4lCCwhKZN0+xU/jV0AUFR0V3ECkd2mKixlmLcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545608; c=relaxed/simple; bh=XRJl/uPgbWvSt8YMeQOUC5JnzlykeY0IhpbgHZkzhCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cJR2zIhI1upkU9+mKjWpU4rM89bovAXXkSvARsELwsmeukZ+0qMk3y7l6UaYURIIe4ZmtdZ8WxSVUB4oBOTfG/3xJfYP61mRcYc1BNPCOE02Nbyj5DGH4DYiB5ChB+L0/g7xV7feFIMDjlaoa6TddcU/cVKgcNOTyXI4UlCGq1M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=H5Cgmpjg; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="H5Cgmpjg" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2cb566d528aso2028679a91.1 for ; Sun, 21 Jul 2024 00:06:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721545606; x=1722150406; darn=vger.kernel.org; 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=BEiXZ1Hftnd13V7cSXtmzxOOQFtbwN2az/SPk33hWyc=; b=H5CgmpjgeIJg7QBnUJMjPt+anfG363PYTnbysf4d+QFphVeWf8kz9jXaZYwQOFQwzJ 25MNY9SKkdLYSkYrdneXh8qWipCHCu24K+DfrZQ1u8ZrmqyYzgvhhLPFXjnfcgMc7EZ7 CgfXXSVLJTMdXfxBkqtn74nSs4DaXTLm9FzM46IFmXextzthx6I5J/VfyzlAFuGjTCtv nZF6CVgXD+6tHAY8seu7Uavs2fesu9p+lI/Ney3DWFCBY+0VXt9HnCYBALBewZIn8POv hwtxLTdG9x9Lrph61dA/vTAJtAj1IY5uBNR3htXcERXw1m5vm0gMoYqahK6Vw0/fVtDP X0jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721545606; x=1722150406; 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=BEiXZ1Hftnd13V7cSXtmzxOOQFtbwN2az/SPk33hWyc=; b=t3UTRiTQ8c1ehI1jiZ/8RjFYrAl+uQT+aWdLucUvmuYtcQqRPqSMIU8Dyphj60M665 5NdiggxeeK1jxf1KcdEcbdTv67t0PXiGmFOtJELkriIRxqm2afUs9/vdwl4MtvXnQcS7 m+BOEw7XlWPssOGk+QtEsiJQMgCVvOqNNSl5SI7q75KvqdFeEJdCMWAn7vBeykNL+0fT 2YqoJgO9kqLLOfSJ1XqbZv917lif64w8V/M6SauU8RDhKVSXcfxWmdSmwM9t2GNprxLl 8dk9qDHb5yJsYeg24hKQDY4crOpGlMhM83ouktVJQARneYLfm/kD354DjtM93QKAlbLR YYng== X-Gm-Message-State: AOJu0YxfEacCl8XbTBzzDm5WpqqzOQ3Kkbl3gcwiEuk6S7o35IGcR7f2 O88iBObNcxxvVz+7b+yiaGiifLyKEmcb2yBNBAdKNtQ4nM1vyki109KkW2Ko X-Google-Smtp-Source: AGHT+IGqLG6cChuadUwSJR6nMD/h/GchDo5AotWk4zMR+oxtwoOnJrNXEf93syvi6klE5/FW8cHLAA== X-Received: by 2002:a17:90b:1e44:b0:2c9:9fcd:aa51 with SMTP id 98e67ed59e1d1-2cd273d7d38mr2374397a91.5.1721545575837; Sun, 21 Jul 2024 00:06:15 -0700 (PDT) Received: from JRT-PC.. ([180.255.73.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cb77492bc6sm4891461a91.1.2024.07.21.00.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jul 2024 00:06:15 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v4 3/5] riscv: Add method to probe for SBI extensions Date: Sun, 21 Jul 2024 15:05:58 +0800 Message-ID: <20240721070601.88639-4-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240721070601.88639-1-jamestiotio@gmail.com> References: <20240721070601.88639-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a `sbi_probe` helper method that can be used by SBI extension tests to check if a given extension is available. Suggested-by: Andrew Jones Reviewed-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/sbi.h | 1 + lib/riscv/sbi.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/riscv/asm/sbi.h b/lib/riscv/asm/sbi.h index d82a384d..5e1a674a 100644 --- a/lib/riscv/asm/sbi.h +++ b/lib/riscv/asm/sbi.h @@ -49,6 +49,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, void sbi_shutdown(void); struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned long sp); +long sbi_probe(int ext); #endif /* !__ASSEMBLY__ */ #endif /* _ASMRISCV_SBI_H_ */ diff --git a/lib/riscv/sbi.c b/lib/riscv/sbi.c index f39134c4..3d4236e5 100644 --- a/lib/riscv/sbi.c +++ b/lib/riscv/sbi.c @@ -38,3 +38,16 @@ struct sbiret sbi_hart_start(unsigned long hartid, unsigned long entry, unsigned { return sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START, hartid, entry, sp, 0, 0, 0); } + +long sbi_probe(int ext) +{ + struct sbiret ret; + + ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_GET_SPEC_VERSION, 0, 0, 0, 0, 0, 0); + assert(!ret.error && ret.value >= 2); + + ret = sbi_ecall(SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, ext, 0, 0, 0, 0, 0); + assert(!ret.error); + + return ret.value; +} From patchwork Sun Jul 21 07:05:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13737951 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DDC89C8FF for ; Sun, 21 Jul 2024 07:06:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545581; cv=none; b=FkCHeHjfTlYJJx0MlvZuTfkhhUg8Z533DiA85XltAlUwQzJN7kmG0RIJhIbX9XmePjnqLuafLwtM5bx1knhcYFe6Ms61WjNDYFJiBPAqernFZ/89ZFh7U35hxFWKxGonWZxBz2As/Z3OBmrYb4neqtLkHOYLOdG1TdVdVhCxbuc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545581; c=relaxed/simple; bh=+agld4zlqbig1ojMy8W/4esTTSShlDdWhqY4xkhgWBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CrG+23xRseUNhMz2jz2oF4AvqzFZpxOyK9h+s4hdMc6V6skNvHtDHXbCcxza5zFE2SnBNC/8R9dWlsld/0a75Qv4XlEgDfgUE0T+dPaFI8s13vFAMvw2IMRltSvJ4ivCz4m1ozoEDsJpKhUGuG2b+MkXRm6CJsnjSiTi5xr9dq4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UWf2vXFj; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UWf2vXFj" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1fc66fc35f2so20524435ad.0 for ; Sun, 21 Jul 2024 00:06:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721545578; x=1722150378; darn=vger.kernel.org; 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=wwRnLnxZnIX7jpw6TVepgUUISRg5HGFY1cuRHhUH+u4=; b=UWf2vXFj0bowbDRVADYiQRj0/u+Wfm/BUhD4ZcT4VeyHAcaqQ5CgDM5lLJUWnjZaMw iLnFGj04TUQf7ifFDOvHRWBbfqkKpF7MA+wayjc2Z3OAS+5mwPg5SjPOgcCxsM0WyBV2 KiWKJcfICYFRjnh38K9HJjgWrlEyGR5PZW2WC5Jt0txOIxbWVIUT3mMiPt2O/Za6OcXf IsORUqk8rWB7D+ZudMD3/3JFhUG2hvDjpNg0S3rPDxx6h3X3yqd/W/iaUMMrmf4LIJkt K13W7+/YkuM7xqR5mkmbzvaUZ2p5V2ceSc8dVJQ0KsL/f7S2HZ3vDAUy7RHwGuPMmkwv nJVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721545578; x=1722150378; 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=wwRnLnxZnIX7jpw6TVepgUUISRg5HGFY1cuRHhUH+u4=; b=mN0sC7Oh0ZefKJ0KLFIpchSBpppmT7RiRd4h/erhxGTgGk82gPZjKQVi2rfZ9BHJsF x4RiQsX8h2SVyRWudMYjej8nzyUV7KklXEjzxm+bGS/UF1rdr214inPkjCGOofcxvO25 4Zy+Yepdq3eH3qucAUDjzLF6LrT+O22ad7RpfFNQTBkJ/bY6quuTWTX3eQ97p29xE7Jl DwZ5hKkhFIuOVnL5NAjjiIPLBY7yhQPcNNn4jHDIoq7PxB+e9gMQLpZxMedBjGhsCz4N ox4734mmVK58E715abRJMFt/2/xUUg2yHgUlsN/Qct6edybyARn5v3pZONNdWwbtaG0M 0+9Q== X-Gm-Message-State: AOJu0YypikqlWgHz3w1bPR+cc5uzR/1QUJbgpZyaDmHqgvRdg2UoUi3F 6mP1fzwQ8FRxUgxvPiZSnSm4b9terGLCRd1UVkzAZq/EDnXM0qYT5s4IkBJV X-Google-Smtp-Source: AGHT+IHgBFqJOIzhFEOebjR+115FyIhkPJsQ4RffTqIopgshibsvhDZ3nKoLtISm4iteJ5Ms2VJGPQ== X-Received: by 2002:a17:90b:3a85:b0:2c2:d590:808e with SMTP id 98e67ed59e1d1-2cd16d628fdmr6378942a91.13.1721545578121; Sun, 21 Jul 2024 00:06:18 -0700 (PDT) Received: from JRT-PC.. ([180.255.73.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cb77492bc6sm4891461a91.1.2024.07.21.00.06.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jul 2024 00:06:17 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v4 4/5] riscv: Add some delay and timer routines Date: Sun, 21 Jul 2024 15:05:59 +0800 Message-ID: <20240721070601.88639-5-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240721070601.88639-1-jamestiotio@gmail.com> References: <20240721070601.88639-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a delay method that would allow tests to wait for some specified number of cycles. Also add a conversion helper method between microseconds and cycles. This conversion is done by using the timebase frequency, which is obtained during setup via the device tree. Reviewed-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- riscv/Makefile | 2 ++ lib/riscv/asm/csr.h | 1 + lib/riscv/asm/delay.h | 16 ++++++++++++++++ lib/riscv/asm/setup.h | 1 + lib/riscv/asm/timer.h | 14 ++++++++++++++ lib/riscv/delay.c | 21 +++++++++++++++++++++ lib/riscv/setup.c | 4 ++++ lib/riscv/timer.c | 28 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+) create mode 100644 lib/riscv/asm/delay.h create mode 100644 lib/riscv/asm/timer.h create mode 100644 lib/riscv/delay.c create mode 100644 lib/riscv/timer.c diff --git a/riscv/Makefile b/riscv/Makefile index 919a3ebb..b0cd613f 100644 --- a/riscv/Makefile +++ b/riscv/Makefile @@ -30,6 +30,7 @@ cflatobjs += lib/memregions.o cflatobjs += lib/on-cpus.o cflatobjs += lib/vmalloc.o cflatobjs += lib/riscv/bitops.o +cflatobjs += lib/riscv/delay.o cflatobjs += lib/riscv/io.o cflatobjs += lib/riscv/isa.o cflatobjs += lib/riscv/mmu.o @@ -38,6 +39,7 @@ cflatobjs += lib/riscv/sbi.o cflatobjs += lib/riscv/setup.o cflatobjs += lib/riscv/smp.o cflatobjs += lib/riscv/stack.o +cflatobjs += lib/riscv/timer.o ifeq ($(ARCH),riscv32) cflatobjs += lib/ldiv32.o endif diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index ba810c9f..a9b1bd42 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -10,6 +10,7 @@ #define CSR_SCAUSE 0x142 #define CSR_STVAL 0x143 #define CSR_SATP 0x180 +#define CSR_TIME 0xc01 #define SR_SIE _AC(0x00000002, UL) diff --git a/lib/riscv/asm/delay.h b/lib/riscv/asm/delay.h new file mode 100644 index 00000000..31379eac --- /dev/null +++ b/lib/riscv/asm/delay.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_DELAY_H_ +#define _ASMRISCV_DELAY_H_ + +#include +#include + +extern void delay(uint64_t cycles); +extern void udelay(unsigned long usecs); + +static inline uint64_t usec_to_cycles(uint64_t usec) +{ + return (timebase_frequency * usec) / 1000000; +} + +#endif /* _ASMRISCV_DELAY_H_ */ diff --git a/lib/riscv/asm/setup.h b/lib/riscv/asm/setup.h index 7f81a705..a13159bf 100644 --- a/lib/riscv/asm/setup.h +++ b/lib/riscv/asm/setup.h @@ -7,6 +7,7 @@ #define NR_CPUS 16 extern struct thread_info cpus[NR_CPUS]; extern int nr_cpus; +extern uint64_t timebase_frequency; int hartid_to_cpu(unsigned long hartid); void io_init(void); diff --git a/lib/riscv/asm/timer.h b/lib/riscv/asm/timer.h new file mode 100644 index 00000000..f7504f84 --- /dev/null +++ b/lib/riscv/asm/timer.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_TIMER_H_ +#define _ASMRISCV_TIMER_H_ + +#include + +extern void timer_get_frequency(void); + +static inline uint64_t timer_get_cycles(void) +{ + return csr_read(CSR_TIME); +} + +#endif /* _ASMRISCV_TIMER_H_ */ diff --git a/lib/riscv/delay.c b/lib/riscv/delay.c new file mode 100644 index 00000000..d4f76c29 --- /dev/null +++ b/lib/riscv/delay.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024, James Raphael Tiovalen + */ +#include +#include +#include +#include + +void delay(uint64_t cycles) +{ + uint64_t start = timer_get_cycles(); + + while ((timer_get_cycles() - start) < cycles) + cpu_relax(); +} + +void udelay(unsigned long usecs) +{ + delay(usec_to_cycles((uint64_t)usecs)); +} diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c index 50ffb0d0..e0b5f6f7 100644 --- a/lib/riscv/setup.c +++ b/lib/riscv/setup.c @@ -20,6 +20,7 @@ #include #include #include +#include #define VA_BASE ((phys_addr_t)3 * SZ_1G) #if __riscv_xlen == 64 @@ -38,6 +39,7 @@ u32 initrd_size; struct thread_info cpus[NR_CPUS]; int nr_cpus; +uint64_t timebase_frequency; static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1]; @@ -199,6 +201,7 @@ void setup(const void *fdt, phys_addr_t freemem_start) mem_init(PAGE_ALIGN(__pa(freemem))); cpu_init(); + timer_get_frequency(); thread_info_init(); io_init(); @@ -264,6 +267,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) } cpu_init(); + timer_get_frequency(); thread_info_init(); io_init(); initrd_setup(); diff --git a/lib/riscv/timer.c b/lib/riscv/timer.c new file mode 100644 index 00000000..d78d254c --- /dev/null +++ b/lib/riscv/timer.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024, James Raphael Tiovalen + */ +#include +#include +#include +#include + +void timer_get_frequency(void) +{ + const struct fdt_property *prop; + u32 *data; + int cpus, len; + + assert_msg(dt_available(), "ACPI not yet supported"); + + const void *fdt = dt_fdt(); + + cpus = fdt_path_offset(fdt, "/cpus"); + assert(cpus >= 0); + + prop = fdt_get_property(fdt, cpus, "timebase-frequency", &len); + assert(prop != NULL && len == 4); + + data = (u32 *)prop->data; + timebase_frequency = fdt32_to_cpu(*data); +} From patchwork Sun Jul 21 07:06:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 13737952 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 501AD944E for ; Sun, 21 Jul 2024 07:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545583; cv=none; b=jRPdMtXMW0XjjGfFrFskQslC/T3vyTiTQnRbpxtdPV2PMl6aEExovOiJOOFPieaXJ+XcYzowTwDfl1/h59VSJ8XYcfD9ex+vfLblfv+9vY0oxKvbDTpCRHY2eYfxcfETpvfz4miEZ3jYlStYTKJD0X4xg0ylB6258YJqxBjxljE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721545583; c=relaxed/simple; bh=fL4r++2W3tafZl6nvytDiibtTrUAUThm1AYpEfEoKn8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gEHKt/P73/Tw3CaZcdo+JFhoztDPFmnqPrc+HYjM8aKdo9NLWE+k5Zp4g4jWLkioeirokSYP9XV5Wc9SshBcCaZbhas7g2ynGW+FeXr+em8ywwmWV2vdgG8WPC7Sl/3dNFsGqXhU5vrWjjZnEDAzejtaBWT6fMC0nQQrRU0b3Ko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cWWlUbaf; arc=none smtp.client-ip=209.85.210.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cWWlUbaf" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70b0e7f6f8bso1881074b3a.3 for ; Sun, 21 Jul 2024 00:06:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721545581; x=1722150381; darn=vger.kernel.org; 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=BBE6A/bNNTAy6FniqPADMXl7o6XMR7KLE4bnhlWkgK4=; b=cWWlUbafS/wNzU+gtwvfrYQ1yoLeUl4qg9Znrk1CwiZCNGbHSRzORI8xzxBzky2aJs rY+qtM4YQbLCQ4VKSpRrCmDrXUH4JWnDMaA7AL7woiyNC5Bm2vio7gRKL/UoT9lW8W88 ZOhE97U2QM6G/Ff8YvF0SomPHY7W6EypVOTWYs3EfncCDfxBVnlY3ImpMNotXygp+mSa AcwfmrgduWBfg6YyE/BDLewRwBvHRGyiGqIuMUtBxQ+KLWDPs/caOkdVCLynurmtOGND +Ntt1dyvbNiQArNqgysk/6JqE8iJm+pk6fUM4Bs6ROnJaKE8ZhcvUcgdXn+kNg6NYs6e 94FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721545581; x=1722150381; 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=BBE6A/bNNTAy6FniqPADMXl7o6XMR7KLE4bnhlWkgK4=; b=n2LPP2maMBhDXbxG++eXrSxBpuI1ZNKUavzKMcn/5fCkLJJNwPLx45IaObu5ZuogL4 0qroeXPlf89xYPf7mbX5X7d1sD7QW8nNcxdmUqpMH1Py9y/2KGzSz50fnXNHIwItzOQC l0ZYlBus6o4e3+srL5XQlzLmy1KpYRYUgy6/4RXS0pt48lBB/HHHFjJTW8sah5eV78MS noEkY2kkGJErT7nTpZLW1wo1wIy+jxWHP+C68CC5UOR32AA4vRqX+w4uskJYiNYiw5b5 nNGMsfr42mba1ca/ZeI7Ms4zYO+gvouHBhHvo+aBSngXSFQTn9TpDxP9Lb1oSOibrhaZ K+Qw== X-Gm-Message-State: AOJu0YyJtTj6tCOSlUdzZ1YFILdOGn3HTk6+6Ckv/46sBLYu9vsK2SKR ItCf/bzoN3glYY+WZTFA8epau0yzPgFFmky4cyHQTxAs0s1DQ3hiKeZp6Vt9 X-Google-Smtp-Source: AGHT+IFLtXv3i3p8jfevPC2wvq0SjkTMoC845xtDvLoXQgWuVrCSGCrbi5aw1SstRh3N/CYv3N0OfA== X-Received: by 2002:a05:6a21:c8a:b0:1c3:ce0f:bfb2 with SMTP id adf61e73a8af0-1c4285d55a7mr4598852637.23.1721545580432; Sun, 21 Jul 2024 00:06:20 -0700 (PDT) Received: from JRT-PC.. ([180.255.73.78]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cb77492bc6sm4891461a91.1.2024.07.21.00.06.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jul 2024 00:06:19 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v4 5/5] riscv: sbi: Add test for timer extension Date: Sun, 21 Jul 2024 15:06:00 +0800 Message-ID: <20240721070601.88639-6-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240721070601.88639-1-jamestiotio@gmail.com> References: <20240721070601.88639-1-jamestiotio@gmail.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a test for the set_timer function of the time extension. The test checks that: - The time extension is available - The installed timer interrupt handler is called - The timer interrupt is received within a reasonable time interval - The timer interrupt pending bit is cleared after the set_timer SBI call is made - The timer interrupt can be cleared either by requesting a timer interrupt infinitely far into the future or by masking the timer interrupt The timer interrupt delay can be set using the TIMER_DELAY environment variable in microseconds. The default delay value is 200 milliseconds. Since the interrupt can arrive a little later than the specified delay, allow some margin of error. This margin of error can be specified via the TIMER_MARGIN environment variable in microseconds. The default margin of error is 200 milliseconds. Signed-off-by: James Raphael Tiovalen --- lib/riscv/asm/csr.h | 8 +++ lib/riscv/asm/sbi.h | 5 ++ lib/riscv/asm/timer.h | 10 ++++ riscv/sbi.c | 132 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index a9b1bd42..24b333e0 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -4,11 +4,15 @@ #include #define CSR_SSTATUS 0x100 +#define CSR_SIE 0x104 #define CSR_STVEC 0x105 #define CSR_SSCRATCH 0x140 #define CSR_SEPC 0x141 #define CSR_SCAUSE 0x142 #define CSR_STVAL 0x143 +#define CSR_SIP 0x144 +#define CSR_STIMECMP 0x14d +#define CSR_STIMECMPH 0x15d #define CSR_SATP 0x180 #define CSR_TIME 0xc01 @@ -47,6 +51,10 @@ #define IRQ_S_GEXT 12 #define IRQ_PMU_OVF 13 +#define IE_TIE (_AC(0x1, UL) << IRQ_S_TIMER) + +#define IP_TIP IE_TIE + #ifndef __ASSEMBLY__ #define csr_swap(csr, val) \ diff --git a/lib/riscv/asm/sbi.h b/lib/riscv/asm/sbi.h index 5e1a674a..73ab5438 100644 --- a/lib/riscv/asm/sbi.h +++ b/lib/riscv/asm/sbi.h @@ -16,6 +16,7 @@ enum sbi_ext_id { SBI_EXT_BASE = 0x10, + SBI_EXT_TIME = 0x54494d45, SBI_EXT_HSM = 0x48534d, SBI_EXT_SRST = 0x53525354, }; @@ -37,6 +38,10 @@ enum sbi_ext_hsm_fid { SBI_EXT_HSM_HART_SUSPEND, }; +enum sbi_ext_time_fid { + SBI_EXT_TIME_SET_TIMER = 0, +}; + struct sbiret { long error; long value; diff --git a/lib/riscv/asm/timer.h b/lib/riscv/asm/timer.h index f7504f84..b3514d3f 100644 --- a/lib/riscv/asm/timer.h +++ b/lib/riscv/asm/timer.h @@ -11,4 +11,14 @@ static inline uint64_t timer_get_cycles(void) return csr_read(CSR_TIME); } +static inline void timer_irq_enable(void) +{ + csr_set(CSR_SIE, IE_TIE); +} + +static inline void timer_irq_disable(void) +{ + csr_clear(CSR_SIE, IE_TIE); +} + #endif /* _ASMRISCV_TIMER_H_ */ diff --git a/riscv/sbi.c b/riscv/sbi.c index 762e9711..fe2fe771 100644 --- a/riscv/sbi.c +++ b/riscv/sbi.c @@ -6,7 +6,21 @@ */ #include #include +#include +#include +#include +#include +#include +#include #include +#include +#include + +static bool timer_works; +static bool mask_timer_irq; +static bool timer_irq_set; +static bool timer_irq_cleared; +static unsigned long timer_irq_count; static void help(void) { @@ -19,6 +33,34 @@ static struct sbiret __base_sbi_ecall(int fid, unsigned long arg0) return sbi_ecall(SBI_EXT_BASE, fid, arg0, 0, 0, 0, 0, 0); } +static struct sbiret __time_sbi_ecall(unsigned long stime_value) +{ + return sbi_ecall(SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, stime_value, 0, 0, 0, 0, 0); +} + +static inline bool timer_irq_pending(void) +{ + return csr_read(CSR_SIP) & IP_TIP; +} + +static void timer_irq_handler(struct pt_regs *regs) +{ + if (timer_irq_count < ULONG_MAX) + ++timer_irq_count; + + timer_works = true; + if (timer_irq_pending()) + timer_irq_set = true; + + if (mask_timer_irq) { + timer_irq_disable(); + } else { + __time_sbi_ecall(ULONG_MAX); + if (!timer_irq_pending()) + timer_irq_cleared = true; + } +} + static bool env_or_skip(const char *env) { if (!getenv(env)) { @@ -112,6 +154,95 @@ static void check_base(void) report_prefix_pop(); } +static void check_time(void) +{ + struct sbiret ret; + unsigned long begin, end, duration; + unsigned long d = getenv("TIMER_DELAY") ? strtol(getenv("TIMER_DELAY"), NULL, 0) + : 200000; + unsigned long margin = getenv("TIMER_MARGIN") ? strtol(getenv("TIMER_MARGIN"), NULL, 0) + : 200000; + + d = usec_to_cycles(d); + margin = usec_to_cycles(margin); + + report_prefix_push("time"); + + if (!sbi_probe(SBI_EXT_TIME)) { + report_skip("time extension not available"); + report_prefix_pop(); + return; + } + + report_prefix_push("set_timer"); + + install_irq_handler(IRQ_S_TIMER, timer_irq_handler); + local_irq_enable(); + if (cpu_has_extension(smp_processor_id(), ISA_SSTC)) { + csr_write(CSR_STIMECMP, ULONG_MAX); +#if __riscv_xlen == 32 + csr_write(CSR_STIMECMPH, ULONG_MAX); +#endif + } + timer_irq_enable(); + + begin = timer_get_cycles(); + ret = __time_sbi_ecall(begin + d); + + report(!ret.error, "set timer"); + if (ret.error) + report_info("set timer failed with %ld\n", ret.error); + + report(!timer_irq_pending(), "pending timer interrupt bit cleared"); + + while ((end = timer_get_cycles()) <= (begin + d + margin) && !timer_works) + cpu_relax(); + + report(timer_works, "timer interrupt received"); + report(timer_irq_set, "pending timer interrupt bit set in irq handler"); + report(timer_irq_cleared, "pending timer interrupt bit cleared by setting timer to -1"); + + if (timer_works) { + duration = end - begin; + report(duration >= d && duration <= (d + margin), "timer delay honored"); + } + + if (timer_irq_count > 1) + report_fail("timer interrupt received multiple times"); + + timer_works = false; + timer_irq_set = false; + timer_irq_count = 0; + mask_timer_irq = true; + begin = timer_get_cycles(); + ret = __time_sbi_ecall(begin + d); + + report(!ret.error, "set timer for mask irq test"); + if (ret.error) + report_info("set timer for mask irq test failed with %ld\n", ret.error); + + while ((end = timer_get_cycles()) <= (begin + d + margin) && !timer_works) + cpu_relax(); + + report(timer_works, "timer interrupt received for mask irq test"); + report(timer_irq_set, "pending timer interrupt bit set in irq handler for mask irq test"); + + if (timer_works) { + duration = end - begin; + report(duration >= d && duration <= (d + margin), + "timer delay honored for mask irq test"); + } + + if (timer_irq_count > 1) + report_fail("timer interrupt received multiple times for mask irq test"); + + local_irq_disable(); + install_irq_handler(IRQ_S_TIMER, NULL); + + report_prefix_pop(); + report_prefix_pop(); +} + int main(int argc, char **argv) { @@ -122,6 +253,7 @@ int main(int argc, char **argv) report_prefix_push("sbi"); check_base(); + check_time(); return report_summary(); }