[PATCH v6 10/11] lib: utils/timer: Expose timer update function

Anup Patel anup at brainfault.org
Mon Sep 8 04:26:36 PDT 2025


On Fri, Sep 5, 2025 at 7:54 AM Nick Hu <nick.hu at sifive.com> wrote:
>
> Exposing the ACLINT timer update APIs so the user can update the mtimer
> after waking up from the non-retentive suspend.
>
> Signed-off-by: Nick Hu <nick.hu at sifive.com>
> Reviewed-by: Cyan Yang <cyan.yang at sifive.com>

LGTM.

Reviewed-by: Anup Patel <anup at brainfault.org>

Regards,
Anup

> ---
>  include/sbi_utils/timer/aclint_mtimer.h |  5 +++++
>  lib/utils/timer/aclint_mtimer.c         | 28 +++++++++++++++++++++-------
>  2 files changed, 26 insertions(+), 7 deletions(-)
>
> diff --git a/include/sbi_utils/timer/aclint_mtimer.h b/include/sbi_utils/timer/aclint_mtimer.h
> index a245e13e285f2ad658fce7db6366fe8b7bae8c2a..f8d2e5b4c210164c8a737e82643e5e49807753c4 100644
> --- a/include/sbi_utils/timer/aclint_mtimer.h
> +++ b/include/sbi_utils/timer/aclint_mtimer.h
> @@ -42,6 +42,11 @@ struct aclint_mtimer_data {
>         void (*time_wr)(bool timecmp, u64 value, volatile u64 *addr);
>  };
>
> +struct aclint_mtimer_data *aclint_get_mtimer_data(void);
> +
> +void aclint_mtimer_update(struct aclint_mtimer_data *mt,
> +                         struct aclint_mtimer_data *ref);
> +
>  void aclint_mtimer_sync(struct aclint_mtimer_data *mt);
>
>  void aclint_mtimer_set_reference(struct aclint_mtimer_data *mt,
> diff --git a/lib/utils/timer/aclint_mtimer.c b/lib/utils/timer/aclint_mtimer.c
> index 3db3c3be2575e4ddd0180c90f254f7733b7c1daa..fd6189aaa52ad92ca28330fcf0f701ac0b8c337f 100644
> --- a/lib/utils/timer/aclint_mtimer.c
> +++ b/lib/utils/timer/aclint_mtimer.c
> @@ -109,28 +109,42 @@ static struct sbi_timer_device mtimer = {
>         .timer_event_stop = mtimer_event_stop
>  };
>
> -void aclint_mtimer_sync(struct aclint_mtimer_data *mt)
> +struct aclint_mtimer_data *aclint_get_mtimer_data(void)
> +{
> +       return mtimer_get_hart_data_ptr(sbi_scratch_thishart_ptr());
> +}
> +
> +void aclint_mtimer_update(struct aclint_mtimer_data *mt,
> +                         struct aclint_mtimer_data *ref)
>  {
>         u64 v1, v2, mv, delta;
>         u64 *mt_time_val, *ref_time_val;
> -       struct aclint_mtimer_data *reference;
>
> -       /* Sync-up non-shared MTIME if reference is available */
> -       if (mt->has_shared_mtime || !mt->time_delta_reference)
> +       if (!mt || !ref || !mt->time_rd || !mt->time_wr || !ref->time_rd)
>                 return;
>
> -       reference = mt->time_delta_reference;
>         mt_time_val = (void *)mt->mtime_addr;
> -       ref_time_val = (void *)reference->mtime_addr;
> +       ref_time_val = (void *)ref->mtime_addr;
>         if (!atomic_raw_xchg_ulong(&mt->time_delta_computed, 1)) {
>                 v1 = mt->time_rd(mt_time_val);
> -               mv = reference->time_rd(ref_time_val);
> +               mv = ref->time_rd(ref_time_val);
>                 v2 = mt->time_rd(mt_time_val);
>                 delta = mv - ((v1 / 2) + (v2 / 2));
>                 mt->time_wr(false, mt->time_rd(mt_time_val) + delta,
>                             mt_time_val);
>         }
> +}
>
> +void aclint_mtimer_sync(struct aclint_mtimer_data *mt)
> +{
> +       struct aclint_mtimer_data *reference;
> +
> +       /* Sync-up non-shared MTIME if reference is available */
> +       if (mt->has_shared_mtime || !mt->time_delta_reference)
> +               return;
> +
> +       reference = mt->time_delta_reference;
> +       aclint_mtimer_update(mt, reference);
>  }
>
>  void aclint_mtimer_set_reference(struct aclint_mtimer_data *mt,
>
> --
> 2.34.1
>
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi



More information about the opensbi mailing list