From patchwork Tue Jul 30 06:18:16 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: 13746715 Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (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 BE4E018D4AA for ; Tue, 30 Jul 2024 06:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320312; cv=none; b=LIiuYDTURrjC+fW9VtXmRrDie5csaBWjK/v5EseOhxX44xY88zRwPZVdFJ6n8N4ail6ViLjd1+gESWnGPn32L2MCvVaDbXR0+9+qJ//EdvLewbl0QdMm9zaeiy7O5rCiY8EIyV2zPU2TJ8QELwI7ojvSrsgtfmmQF9JX8qTE0gA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320312; c=relaxed/simple; bh=Qogls816cd0Tv53VgYejA6+L3RDe17CCmGQ0+3vemKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ig7KZcpXKEplXGVWDC0PrkTb684FSJnN+L2ZPeJNQw18NQl7mBwJlEo26wdt2F3uEUQnBgOqZc9jqvrh04+FC2Vp9X7e7Am0lQVCwMJirhI4S3RY2562NauUN2pSHkiOvsrKoLkgFsKU0T6yBbVce+z5sWhIyLeg2wLsW3U/8Qw= 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=nbVlL/xj; arc=none smtp.client-ip=209.85.160.52 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="nbVlL/xj" Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-260f863108fso2531355fac.1 for ; Mon, 29 Jul 2024 23:18:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320309; x=1722925109; 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=nbVlL/xjzs6PPk2sl+ChyMCHZNnq7oB4BVoFC5VJAx4OAONSUidv7Bz1b97op0nNR4 +GBNF+1eASHj9JEUC3FzzElJ6rlbDfL628jleiYYxbDdp469Rp8PU+0E0peW/Jc29lDV mU/PiggNbZFvrCPwZXOl8oet7LlPjlmVUiBkWfI0BpSlIyyYql3qODsGiX245vmX40G3 5Ne5rN25lPyEzsSFwsO7GWaf1X2TAfUxpFdHvv3PCZaBV/ygi8RGLPkoDl2Um44z66O8 puYjRVhPmZN8S6WBk3UyHfdiV4ntRtAkE4nYxb0iL/ln7REBpx2iEgVCTd/YejU19i50 ojiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320309; x=1722925109; 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=NCPWVJjeEeww2Dh9cBgSV3sbO62zRlerVYz/4f6U+AjXR6JKzMHcoMpr/oEECfJ3Ux XCcCu6LTzNE7gJfUjJUQkV6l5JvSGiyLf6Nve7lyEvUON1/s65qYNNvhGXoRhFedeVeQ CA81nnVVSbPFt+zyjHsoihs7JEaEeBAc4mCNKHMVP2Qer5Lx+B2XQZXhgwKA0j/BBPFN BC597uTbzeO845vs76ZqSbEOElQi2P1RFsa723WYCl3hWcBoc4crAj/o6dvSkA7bMQpI ZrWVwwj3eLeDsD5WxRh6eV4CrV8WySm15B5h5WynN0rt1EPLzC9IGP/NNt8g3vQ/MzF6 5pnQ== X-Gm-Message-State: AOJu0YxbRZprck76EzcYknCkw22+uZ80kNU2xVVstS2OjVytrh+z8JvY u3ZyUQZfTP1ZE6KI1qRMdf8IvvqzFe+Uz3KgOtAIcQNIaRcMvoGatLTD+qVsK0k= X-Google-Smtp-Source: AGHT+IFZ3x92GfozCY0gffggNOeUFWskApsLodKhUoHGWGIw8sMwo0kCANfV3FpZC5rDkPZNY+qAPg== X-Received: by 2002:a05:6870:7192:b0:254:7211:424b with SMTP id 586e51a60fabf-267d4ccc224mr12102963fac.6.1722320309253; Mon, 29 Jul 2024 23:18:29 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:28 -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 v6 1/5] riscv: Extend exception handling support for interrupts Date: Tue, 30 Jul 2024 14:18:16 +0800 Message-ID: <20240730061821.43811-2-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-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 Tue Jul 30 06:18:17 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: 13746716 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 2B02E18E776 for ; Tue, 30 Jul 2024 06:18:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320314; cv=none; b=Eus5igskBrif4zPMnUeHXyHG+juDFUoMrUg1xM5EfoMB5XYsyudAxsPmXd98uDaOFe0nslCgyCMMzQtM+9jSRyWEZNgB1iAxwBjoYCHWTdIFGci9xPxVjUHL4DF0pwSmNh7YjCXUVK+4wmZCf2c4c+gV9LHwFhsHiDyxJ496Hno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320314; c=relaxed/simple; bh=+n3qbfDTrboEdcvpcY3rZAfV/jBQ7snQ9EH7EftH8lQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eCIplHifVgpbEvUeF+nj7FOR+4HPmX3AgqE/H4tvubmjQghqbh87oM2dPe/ry6caLHLR3St9ODph33YjZuAEOGA8hBOwa7tSJxd45qP3cyu8SsFh+JAE0KGX1J2ZHS9hWasFt9xnIl5tXpXbf3KnOHpyrwTXVw84Z2jNaAXz7ik= 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=noy18f/3; arc=none smtp.client-ip=209.85.210.174 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="noy18f/3" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-70d1c8d7d95so2532721b3a.2 for ; Mon, 29 Jul 2024 23:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320312; x=1722925112; 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=noy18f/3UvPO7AyjOEVcqlWoQnPA78468gD2rp011xBHk7oJi9gDdgy6Ub+MXElEAy wxEjcQ0EuWypBiwMLzdujU2nJKuBrBicwGI5KiXbUJptAhwdtX0dtUxgiBlmJe0SvQgs MsOOYUlGZwLD13Qwy6acnxClLaT5wVMLlRNwvXPnlNq1JJOX2vTvmzbrGTuR7w7y1UCu 0wJFXQJlkZt1q/JQRvkSBHeVY3CUS/XtJA750Fk8s+2y/jopLAy7F30t2TBqWncAcNYw AyFP5dntuNppsUmuzy5LqoIFSaGOyFwZ7mmJEK/UR2oxHskkp4vIyEvlo04AhFjVh9fr xolQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320312; x=1722925112; 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=WPDXEO27TVAVy9N1oqMmI0g5Vqd32+uxAgSm9qoiggGbmyrC7iDLaoPQYHviueKeCT wVVHPg/Z38UfqB26vfGBqrt/wJ0ba3LuLfXmFIx3UuG4L1/QTZDSFsMhWME1pS2x92f7 gyRyJC3LYTTL2Z813SlreHAGvWG/G4MZmBaIDP5foTq8vYfqejKIBREtHUg4ZmYbeVGd /gAao+2LXISAuXEIfcp+IiZOOL70Bb7GRjuO3f6IqgnZo375AKlrYwit7N4pfEKiuFyy 4wV+wKcFzvlcHU8oMONTjazm64sSjTKFlumrxjCf5zg5pP2+ol42SKOQ8wI32SzFz8CE ZfZg== X-Gm-Message-State: AOJu0YzG01UVnjCm6dhOZXWSDvVV+wBLpwOzwmRrzRLFYJzIBvx0lMQq rVpHcVlFeLdf3QKdqKRVRjP3edX19uVAOhEhJHJz8JBls93au1yPS70LgxX3IZg= X-Google-Smtp-Source: AGHT+IGueUZNpYqBiwQ62P95Q5NRqthBhu516As+jDWGSNvqn1Hw5DPqrLuTQBmoOvfL7J5BmHJ/oA== X-Received: by 2002:aa7:88c5:0:b0:70d:262e:7279 with SMTP id d2e1a72fcca58-70ecea01412mr10223964b3a.3.1722320311581; Mon, 29 Jul 2024 23:18:31 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:31 -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 v6 2/5] riscv: Update exception cause list Date: Tue, 30 Jul 2024 14:18:17 +0800 Message-ID: <20240730061821.43811-3-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-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 Tue Jul 30 06:18:18 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: 13746717 Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 4092818F2E7 for ; Tue, 30 Jul 2024 06:18:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320316; cv=none; b=s+0Uo4uLG20rg5Az4ShakCixOI3JBGfuFfe5DeambCKfEfGv3A+Hd4ssUV/hzLNq7rj5ZIZmLCTljJlUzn1Mrh3IamnrBNp268v1V+FNYqfwX6S1aLIscSp6RjNQ4/gNlPirBgXWhV/bkyC+qe874IgY+lqNTj5zmbO3e3PwUX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320316; c=relaxed/simple; bh=XRJl/uPgbWvSt8YMeQOUC5JnzlykeY0IhpbgHZkzhCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KZMWqxChd3adwjwrN3T9sqflKQwknIWSy/K29sNwmmbnvRFWIpcTHcXrNo5pzqlMwNLA2c2gh/U0c/X+YUwWsL54Yjsl8yf5UyN4c/v01PHvAd+EnTs8GC7+u7ricf6BfkoLZ3aLjZnmi3x9IzUEA2TzWZoLk9PJtdYuDqVU1yc= 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=eGv/K86z; arc=none smtp.client-ip=209.85.210.176 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="eGv/K86z" Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-70b703eda27so2484400b3a.3 for ; Mon, 29 Jul 2024 23:18:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320314; x=1722925114; 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=eGv/K86zMAGvjKGim+LWTPb9kxOHTDJuqjrjyn1R63qts4Bs0ghWmzvVzsgJrURRM9 kNhdEInZ1xVhkO89IuVBfy+2zb4inG1UIBGHdsqoXD4qLl2uPfQ+u/wzSp3ofdiwpJjx 7GTfruKjawDWvUBxLF/00WtYstXHJvr0aXu1ZlLS+juUicENMAMHZ9yzVgGksGDK25Ed o7WU3OdoN2zzgxByKw39autvgQ2wAlnRLl4OEy55TDqB0hWVEEpDIs1w1yq00zYoUGqt 2Qdmi5yGxYofnnRmlOLMeYBMB1KydM/HOqhj8hVTgtrd+dcffopA0z3TUuOa4/08sX0z u1eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320314; x=1722925114; 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=IMOB6qoPLKnqW+1XQvyFYKhRWXeYZUR58KETLjhJsaiRswaog6Vd6nPOO/R8PX1MR5 w2Vlqnm3tt93iLxXuLJNaoKb3b/7O4MBoBAHH7ZAkPnCuRGbovH58B7TGIlnySq+O0cz cqHlcwDII0vPEJck9Jdg5aHHQSLWWiUy6OVdTYxcPvDJA6OwbPX58Ffeq/X5rv26zR1a zTNkru+M73R7o/LFGg0FLnt0ws86HenXKVb6OHhZcsTj3zWx29QX9AKdaYV1+oESq9YH t1R33/IFffPtHyINft5RFqHJFy27xd9rsIr2JBLhxQYkPSlt2ZqQzoX3FckxHKyFFX3p r2Vw== X-Gm-Message-State: AOJu0Yy0BK9+j90tW3krjb3wKYVmEHlJz1bVTSilmWRt+TSuZdsybj+y Zn8PXAvHBbiktfMSuLkLk5MC5CmiGs0ZRDU8C/30ltWUtTd7T/Wtpkn/pskp65Q= X-Google-Smtp-Source: AGHT+IHEOGQoMzw6uwNYgIfG5QWUNtOH7qO1sK3jwBwHoTREqCpBgVGPtSWJwiAOfdutPlMvqnSdKw== X-Received: by 2002:a05:6a20:9e4b:b0:1be:c2f7:275 with SMTP id adf61e73a8af0-1c4a153355fmr8735392637.50.1722320313931; Mon, 29 Jul 2024 23:18:33 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:33 -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 v6 3/5] riscv: Add method to probe for SBI extensions Date: Tue, 30 Jul 2024 14:18:18 +0800 Message-ID: <20240730061821.43811-4-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-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 Tue Jul 30 06:18:19 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: 13746718 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 8976416B75D for ; Tue, 30 Jul 2024 06:18:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320319; cv=none; b=IIG+Yb5r+yoRQzhpsjJFsQ9dWIU/AI1uEfNawM3nOaUN2MZ9n5PlyXwuuRpqh2ZSLHzN72r/DQG7cVLYZHwDuZtUp4EuyWYhqjZUGAUTUmJI3wrpNL/cJaqc5yzq3vdFs4GKLEI57+g4ZtIBdZC4n6CtAJYyDRA2lNGqT3eFjn8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320319; c=relaxed/simple; bh=+agld4zlqbig1ojMy8W/4esTTSShlDdWhqY4xkhgWBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NNAltyDfqrv+gGfYGvMJHcMsuKqAw5bbpdscrpt1pDkyFsyzay0rO2eRemruNFX51PS8RrYIVoEi4FX83/wrM1bg3VCtZ4PzgllPVicZ9yY8DNKn72YchfKDp0hT6Tjv97tONw4KSdZ3Abj0HBFlZU5EW/lGRTWWwZBtRtfViOI= 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=j9Uv0Zyg; arc=none smtp.client-ip=209.85.210.172 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="j9Uv0Zyg" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-70d19c525b5so2656526b3a.2 for ; Mon, 29 Jul 2024 23:18:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320316; x=1722925116; 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=j9Uv0Zyg0LYZLh0cB7CXhvozlO1+k3U/sDksdtOqutOB0joa259i6RggO0gKSKLI85 qd6l3yp/IZORxD0WwgbQn3p9PVUHIwcL/a08iFNQGhWzMAJQdWJraeSB5NvjGkyM8J/W 5vJL+Ql0FrvF9OMGJht+uQO5n03XrJsDVRk++a27dWKoqb3mAa8On8Qieq/zwb1rZdjn 1LjNklDNJxX+oCQM+EQijSIz1ib4KuqKv4P7DYV1lFIf6o0zchZzid214FH1klHoBq04 VXaIKTwvzFUHOTa1ZL6T2AXXeRhNlMvwUzkgDOJQi9Jp9b/uQNOxhwW/WDkA7qZQVzbI Xrug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320316; x=1722925116; 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=tGQsRkHCOVTQwD1hrJEjw52b1aXb6I3DaEuswMrzeGbrLs1pB9ZR3YVer3PYHUsN8x tkBj0DppQLGZ4JulNS0DGQF+bNW91SzMsEVaJxIPSkMHmEOnjryyBDabJrAn6kJTPOpB CyHO9ISiWWvXIMtiyT6gTqOJoBqcdDUGV6BK7QeJxH0se33Ul7/dR6izqeW+7GqPpf/z +GSEirLD1hRIJvM0F4FYIv7vBPkDeQn1j3mode9ycozAFUUKNSESXyrDkvPTLhB3ROBF h/K+9Uj7DLEPXWSHn0FcSjoyEQYM/2Oz7dCy07+tAZluF8b33mYe1BlsiOMkwMTMaoFr LCMw== X-Gm-Message-State: AOJu0Yw8WXgsRBPB9+hKIyfdkCNOtmBIIlZCydOrImubsKein6Rd5F8/ 2f1pEAk+AfU7v2NXBHxL+u5Efr38lmqjTSXCwOIo6EZ9CPGekExY9WROF1Z+XWU= X-Google-Smtp-Source: AGHT+IEOe21GzPpIFL9RJlKO9WKXPkc5wU/dA9Yimqtc6/HmISTe4cOgbk2Kf7vwcHA2NB7i87ed8A== X-Received: by 2002:a05:6a21:789f:b0:1bd:7559:1710 with SMTP id adf61e73a8af0-1c4a12a2e50mr9193595637.14.1722320316245; Mon, 29 Jul 2024 23:18:36 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:35 -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 v6 4/5] riscv: Add some delay and timer routines Date: Tue, 30 Jul 2024 14:18:19 +0800 Message-ID: <20240730061821.43811-5-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-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 Tue Jul 30 06:18:20 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: 13746719 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 DA4E518FC85 for ; Tue, 30 Jul 2024 06:18:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320321; cv=none; b=DnEa0uE0SuEcSC1YVQtudRV9p/xHv0Vgh8JSqs9v8cXIDWqCKqHudtbJ3q/3RIZxEQ7dLIxV5ql7VBoNZ13uUBqzi6Uhx9Mi5ndDpJZniqEFNAfMe8fxYYBSwtiG8tJwVUDjWb1ar18P9ZMpXP8eDHsKZdsQHinsSMVSbJxhfP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722320321; c=relaxed/simple; bh=1i7CBN3jOe218/OcDlClAJcZNUqGkBYiPKY7XxKDLZo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ctLVK0F3gyDA5mq/cRfRZ/IsXhxYVDYPLCyNaVUr8cuSnHGr0lsd/ufps4Z+bEvCy1Usw8WaWl7a+c2B838pLIz42FICJS8+f5w9h7BZBIDrFBHsEypjGajhceQKdv6RoPCIhOlDtKCj9vxFJDas+zRmIRq0eiMRE9uXJC/83oM= 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=N3AwMcEX; arc=none smtp.client-ip=209.85.210.174 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="N3AwMcEX" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-70d18112b60so2943801b3a.1 for ; Mon, 29 Jul 2024 23:18:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320319; x=1722925119; 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=b9aR+63uT7v78VjbRv6DJCJHpLF+0X46I4y0U041o2c=; b=N3AwMcEX/pj4M8q2J2dsvuT81ezVtP606OwGF1+e8nGdaNVDO7sWwbHfjzPlJxWofR fVZ1RpXTaUFe9yfpZQle7AVxZDNaObRpuamukz/lIP19yq3YweDpH5GqPYzsNf9zOFdB m1sRPTZJUuLEMPJ/FoTWnfsq9g1SVLj2fAHDqWNTL6kPFCVoGxmgzT/LWHZazzpgrQLa DgkVE4eNqM2U+Jd7uJlLAJy9z13ZXtvO1CVZkbj2XUTr1UkNoe1RXGqsj8FvN84Eok0g 4+Sn37KoaBLxUmIanuTqr1UfjHcwo9sNxEMsTCkKgukC3mN9hafN8MLTgnuts2nGKnbG x2pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320319; x=1722925119; 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=b9aR+63uT7v78VjbRv6DJCJHpLF+0X46I4y0U041o2c=; b=k1/joeWs5sEIaPgpnXIEbwLopg5s1Bx2KkftURhqZMcfitKMYk3GOm6JSn0ukVnaVH gpmjGWIISEYXUxk7WE8h95rPaF2oNSn5prZYKc4PQnea1PePnqcYR11tDiW41ojEd4hT LrO5Kosdx/i6y4xUZZvsT6YwAGTa48AsDr8JgjU8Kccex22T/vMWbayjXHDpjAGEDcco hQVKSSlsvqAIJL0TWzHzyfb13Ash//NGm3pdSCWUlyNMWPSltW8VZke0GK+JQA1iK5HI KCRQqPGHiMnsGDKYknM9XCMUtIUDOzGF2bgoLWB+S3eTi0r6L6lAUHaSp7FXVwTLMWZA VRog== X-Gm-Message-State: AOJu0Ywl6Wtnz8mWFFqa3YTEAP8nAMGZA+YETT6RCUqUEcfjQTkikrdw RRaMZCEVcgOJbqeduQCNi6Erv4oivrtMf7SifTtLXQaQvHPUGX5Ljj5dyd3Qbxw= X-Google-Smtp-Source: AGHT+IEAIFl8b07iaWFTucQrr8o5u8AfL9aHdpepBAosSPFLueK4FRXeZIiIZifwd3D2tPdwPtlgDA== X-Received: by 2002:aa7:8257:0:b0:70e:ce95:b87 with SMTP id d2e1a72fcca58-70efe2a2430mr1637242b3a.0.1722320318624; Mon, 29 Jul 2024 23:18:38 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:38 -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 v6 5/5] riscv: sbi: Add test for timer extension Date: Tue, 30 Jul 2024 14:18:20 +0800 Message-ID: <20240730061821.43811-6-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-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. Reviewed-by: Andrew Jones 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 | 127 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 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..9fc099df 100644 --- a/riscv/sbi.c +++ b/riscv/sbi.c @@ -6,7 +6,25 @@ */ #include #include +#include +#include +#include +#include +#include +#include #include +#include +#include + +struct timer_info { + bool timer_works; + bool mask_timer_irq; + bool timer_irq_set; + bool timer_irq_cleared; + unsigned long timer_irq_count; +}; + +static struct timer_info timer_info; static void help(void) { @@ -19,6 +37,78 @@ 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 bool timer_irq_pending(void) +{ + return csr_read(CSR_SIP) & IP_TIP; +} + +static void timer_irq_handler(struct pt_regs *regs) +{ + if (timer_info.timer_irq_count < ULONG_MAX) + ++timer_info.timer_irq_count; + + timer_info.timer_works = true; + if (timer_irq_pending()) + timer_info.timer_irq_set = true; + + if (timer_info.mask_timer_irq) { + timer_irq_disable(); + __time_sbi_ecall(0); + } else { + __time_sbi_ecall(ULONG_MAX); + } + + if (!timer_irq_pending()) + timer_info.timer_irq_cleared = true; +} + +static void execute_set_timer_test(bool mask_timer_irq) +{ + struct sbiret ret; + unsigned long begin, end, duration; + const char *mask_test_str = mask_timer_irq ? " for mask irq test" : ""; + const char *clear_irq_bit_str = mask_timer_irq ? "masking timer irq" + : "setting timer to -1"; + 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); + + timer_info = (struct timer_info){ .mask_timer_irq = mask_timer_irq }; + begin = timer_get_cycles(); + ret = __time_sbi_ecall(begin + d); + + report(!ret.error, "set timer%s", mask_test_str); + if (ret.error) + report_info("set timer%s failed with %ld\n", mask_test_str, ret.error); + + while ((end = timer_get_cycles()) <= (begin + d + margin) && !timer_info.timer_works) + cpu_relax(); + + report(timer_info.timer_works, "timer interrupt received%s", mask_test_str); + report(timer_info.timer_irq_set, "pending timer interrupt bit set in irq handler%s", + mask_test_str); + report(timer_info.timer_irq_set && timer_info.timer_irq_cleared, + "pending timer interrupt bit cleared by %s", clear_irq_bit_str); + + if (timer_info.timer_works) { + duration = end - begin; + report(duration >= d && duration <= (d + margin), "timer delay honored%s", + mask_test_str); + } + + if (timer_info.timer_irq_count > 1) + report_fail("timer interrupt received multiple times%s", mask_test_str); +} + static bool env_or_skip(const char *env) { if (!getenv(env)) { @@ -112,6 +202,42 @@ static void check_base(void) report_prefix_pop(); } +static void check_time(void) +{ + 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(); + + execute_set_timer_test(false); + + if (csr_read(CSR_SIE) & IE_TIE) + execute_set_timer_test(true); + else + report_skip("timer irq enable bit is not writable, skipping 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 +248,7 @@ int main(int argc, char **argv) report_prefix_push("sbi"); check_base(); + check_time(); return report_summary(); }