OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: Anup Patel <anup.patel@oss.qualcomm.com>
Cc: Atish Patra <atish.patra@linux.dev>,
	 Andrew Jones <andrew.jones@oss.qualcomm.com>,
	Samuel Holland <samuel.holland@sifive.com>,
	 Anup Patel <anup@brainfault.org>,
	opensbi@lists.infradead.org
Subject: Re: [PATCH 2/3] lib: sbi_timer: Introduce per-HART timer state
Date: Thu, 23 Apr 2026 16:55:44 +1000	[thread overview]
Message-ID: <aenCUVNpjI_JJl9_@lima-default> (raw)
In-Reply-To: <20260415110002.2610929-3-anup.patel@oss.qualcomm.com>

On Wed, Apr 15, 2026 at 04:30:01PM +0530, Anup Patel wrote:
> Currently, only time_delta is per-HART so introduce per-HART timer
> state for having more per-HART timer information.
> 
> Signed-off-by: Anup Patel <anup.patel@oss.qualcomm.com>

Reviewed-by: Nicholas Piggin <npiggin@gmail.com>

> ---
>  lib/sbi/sbi_timer.c | 44 +++++++++++++++++++++++++-------------------
>  1 file changed, 25 insertions(+), 19 deletions(-)
> 
> diff --git a/lib/sbi/sbi_timer.c b/lib/sbi/sbi_timer.c
> index 4088a597..9806c033 100644
> --- a/lib/sbi/sbi_timer.c
> +++ b/lib/sbi/sbi_timer.c
> @@ -18,7 +18,11 @@
>  #include <sbi/sbi_scratch.h>
>  #include <sbi/sbi_timer.h>
>  
> -static unsigned long time_delta_off;
> +struct timer_state {
> +	u64 time_delta;
> +};
> +
> +static unsigned long timer_state_off;
>  static u64 (*get_time_val)(void);
>  static const struct sbi_timer_device *timer_dev = NULL;
>  
> @@ -98,35 +102,37 @@ u64 sbi_timer_value(void)
>  
>  u64 sbi_timer_virt_value(void)
>  {
> -	u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
> -						 time_delta_off);
> +	struct timer_state *tstate = sbi_scratch_thishart_offset_ptr(timer_state_off);
>  
> -	return sbi_timer_value() + *time_delta;
> +	return sbi_timer_value() + tstate->time_delta;
>  }
>  
>  u64 sbi_timer_get_delta(void)
>  {
> -	u64 *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
> -						 time_delta_off);
> +	struct timer_state *tstate = sbi_scratch_thishart_offset_ptr(timer_state_off);
>  
> -	return *time_delta;
> +	return tstate->time_delta;
>  }
>  
>  void sbi_timer_set_delta(ulong delta)
>  {
> -	ulong *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
> -						   time_delta_off);
> +	struct timer_state *tstate = sbi_scratch_thishart_offset_ptr(timer_state_off);
>  
> -	*time_delta = delta;
> +#if __riscv_xlen == 32
> +	tstate->time_delta &= ~0xffffffffUL;
> +	tstate->time_delta |= (u32)delta;
> +#else
> +	tstate->time_delta = delta;
> +#endif
>  }
>  
>  #if __riscv_xlen == 32
>  void sbi_timer_set_delta_upper(ulong delta_upper)
>  {
> -	ulong *time_delta = sbi_scratch_offset_ptr(sbi_scratch_thishart_ptr(),
> -						   time_delta_off);
> +	struct timer_state *tstate = sbi_scratch_thishart_offset_ptr(timer_state_off);
>  
> -	*(time_delta + 1) = delta_upper;
> +	tstate->time_delta &= 0xffffffffUL;
> +	tstate->time_delta |= (u64)delta << 32;
>  }
>  #endif
>  
> @@ -176,13 +182,13 @@ void sbi_timer_set_device(const struct sbi_timer_device *dev)
>  
>  int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
>  {
> -	u64 *time_delta;
>  	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> +	struct timer_state *tstate;
>  	int ret;
>  
>  	if (cold_boot) {
> -		time_delta_off = sbi_scratch_alloc_offset(sizeof(*time_delta));
> -		if (!time_delta_off)
> +		timer_state_off = sbi_scratch_alloc_offset(sizeof(*tstate));
> +		if (!timer_state_off)
>  			return SBI_ENOMEM;
>  
>  		if (sbi_hart_has_csr(scratch, SBI_HART_CSR_TIME))
> @@ -192,12 +198,12 @@ int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
>  		if (ret)
>  			return ret;
>  	} else {
> -		if (!time_delta_off)
> +		if (!timer_state_off)
>  			return SBI_ENOMEM;
>  	}
>  
> -	time_delta = sbi_scratch_offset_ptr(scratch, time_delta_off);
> -	*time_delta = 0;
> +	tstate = sbi_scratch_offset_ptr(scratch, timer_state_off);
> +	tstate->time_delta = 0;
>  
>  	if (timer_dev && timer_dev->warm_init) {
>  		ret = timer_dev->warm_init();
> -- 
> 2.43.0
> 
> 
> -- 
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi

-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

  reply	other threads:[~2026-04-23  6:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-15 10:59 [PATCH 0/3] Timer events for OpenSBI Anup Patel
2026-04-15 11:00 ` [PATCH 1/3] include: sbi: Add sbi_scratch_hartindex() macro Anup Patel
2026-04-23  6:55   ` Nicholas Piggin
2026-04-15 11:00 ` [PATCH 2/3] lib: sbi_timer: Introduce per-HART timer state Anup Patel
2026-04-23  6:55   ` Nicholas Piggin [this message]
2026-04-15 11:00 ` [PATCH 3/3] lib: sbi_timer: Add support for timer events Anup Patel
2026-04-23  6:54   ` Nicholas Piggin
2026-04-23  8:25     ` Anup Patel
2026-04-24  1:09       ` Nicholas Piggin
2026-04-23  6:57 ` [PATCH 0/3] Timer events for OpenSBI Nicholas Piggin
2026-04-23  8:08   ` Anup Patel
2026-04-24  3:56     ` Nicholas Piggin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aenCUVNpjI_JJl9_@lima-default \
    --to=npiggin@gmail.com \
    --cc=andrew.jones@oss.qualcomm.com \
    --cc=anup.patel@oss.qualcomm.com \
    --cc=anup@brainfault.org \
    --cc=atish.patra@linux.dev \
    --cc=opensbi@lists.infradead.org \
    --cc=samuel.holland@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox