diff mbox series

clocksource/drivers/timer-riscv: Drop extra CSR write

Message ID 20240312193306.1814593-1-samuel.holland@sifive.com (mailing list archive)
State Rejected
Headers show
Series clocksource/drivers/timer-riscv: Drop extra CSR write | expand

Checks

Context Check Description
conchuod/vmtest-for-next-PR success PR summary
conchuod/patch-1-test-1 success .github/scripts/patches/tests/build_rv32_defconfig.sh
conchuod/patch-1-test-2 success .github/scripts/patches/tests/build_rv64_clang_allmodconfig.sh
conchuod/patch-1-test-3 success .github/scripts/patches/tests/build_rv64_gcc_allmodconfig.sh
conchuod/patch-1-test-4 success .github/scripts/patches/tests/build_rv64_nommu_k210_defconfig.sh
conchuod/patch-1-test-5 success .github/scripts/patches/tests/build_rv64_nommu_virt_defconfig.sh
conchuod/patch-1-test-6 success .github/scripts/patches/tests/checkpatch.sh
conchuod/patch-1-test-7 success .github/scripts/patches/tests/dtb_warn_rv64.sh
conchuod/patch-1-test-8 success .github/scripts/patches/tests/header_inline.sh
conchuod/patch-1-test-9 success .github/scripts/patches/tests/kdoc.sh
conchuod/patch-1-test-10 success .github/scripts/patches/tests/module_param.sh
conchuod/patch-1-test-11 success .github/scripts/patches/tests/verify_fixes.sh
conchuod/patch-1-test-12 success .github/scripts/patches/tests/verify_signedoff.sh

Commit Message

Samuel Holland March 12, 2024, 7:32 p.m. UTC
On riscv32, the time comparator value is split across two CSRs. We write
both when stopping the timer, but realistically the time is just as
unlikely to reach 0xffffffff00000000 as 0xffffffffffffffff, so there is
no need to write the low CSR.

Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---

 drivers/clocksource/timer-riscv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Anup Patel March 13, 2024, 4:56 p.m. UTC | #1
On Wed, Mar 13, 2024 at 1:03 AM Samuel Holland
<samuel.holland@sifive.com> wrote:
>
> On riscv32, the time comparator value is split across two CSRs. We write
> both when stopping the timer, but realistically the time is just as
> unlikely to reach 0xffffffff00000000 as 0xffffffffffffffff, so there is
> no need to write the low CSR.

Even though unlikely, there is still a theoretical possibility of
counter reaching value 0xffffffff00000000.

The good thing about value 0xffffffffffffffff is that the counter will
immediately wrap around after reaching it.

Regards,
Anup


>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> ---
>
>  drivers/clocksource/timer-riscv.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c
> index e66dcbd66566..eaaf01f3c34b 100644
> --- a/drivers/clocksource/timer-riscv.c
> +++ b/drivers/clocksource/timer-riscv.c
> @@ -35,9 +35,10 @@ static bool riscv_timer_cannot_wake_cpu;
>  static void riscv_clock_event_stop(void)
>  {
>         if (static_branch_likely(&riscv_sstc_available)) {
> -               csr_write(CSR_STIMECMP, ULONG_MAX);
>                 if (IS_ENABLED(CONFIG_32BIT))
>                         csr_write(CSR_STIMECMPH, ULONG_MAX);
> +               else
> +                       csr_write(CSR_STIMECMP, ULONG_MAX);
>         } else {
>                 sbi_set_timer(U64_MAX);
>         }
> --
> 2.43.1
>
>
Palmer Dabbelt April 17, 2024, 1:49 a.m. UTC | #2
On Wed, 13 Mar 2024 09:56:34 PDT (-0700), apatel@ventanamicro.com wrote:
> On Wed, Mar 13, 2024 at 1:03 AM Samuel Holland
> <samuel.holland@sifive.com> wrote:
>>
>> On riscv32, the time comparator value is split across two CSRs. We write
>> both when stopping the timer, but realistically the time is just as
>> unlikely to reach 0xffffffff00000000 as 0xffffffffffffffff, so there is
>> no need to write the low CSR.
>
> Even though unlikely, there is still a theoretical possibility of
> counter reaching value 0xffffffff00000000.

I guess that depends on the timebase frequency, but if my math is right 
then we've got some timers that will overflow a 32-bit counter in 10 
minutes -- take that with a grain of salt as they're all 64-bit systems 
(we don't have any 32-bit DTs upstream?), but it still seems plausible 
for 32-bit overflows to happen here on real systems.

> The good thing about value 0xffffffffffffffff is that the counter will
> immediately wrap around after reaching it.

I'm not sure how that's good?  Luckily we've got ~100,000 years to 
figure out, even on these systems with pretty fast timers ;)

> Regards,
> Anup
>
>
>>
>> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
>> ---
>>
>>  drivers/clocksource/timer-riscv.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c
>> index e66dcbd66566..eaaf01f3c34b 100644
>> --- a/drivers/clocksource/timer-riscv.c
>> +++ b/drivers/clocksource/timer-riscv.c
>> @@ -35,9 +35,10 @@ static bool riscv_timer_cannot_wake_cpu;
>>  static void riscv_clock_event_stop(void)
>>  {
>>         if (static_branch_likely(&riscv_sstc_available)) {
>> -               csr_write(CSR_STIMECMP, ULONG_MAX);
>>                 if (IS_ENABLED(CONFIG_32BIT))
>>                         csr_write(CSR_STIMECMPH, ULONG_MAX);
>> +               else
>> +                       csr_write(CSR_STIMECMP, ULONG_MAX);
>>         } else {
>>                 sbi_set_timer(U64_MAX);
>>         }
>> --
>> 2.43.1
>>
>>
Anup Patel April 17, 2024, 6:04 a.m. UTC | #3
On Wed, Apr 17, 2024 at 7:20 AM Palmer Dabbelt <palmer@dabbelt.com> wrote:
>
> On Wed, 13 Mar 2024 09:56:34 PDT (-0700), apatel@ventanamicro.com wrote:
> > On Wed, Mar 13, 2024 at 1:03 AM Samuel Holland
> > <samuel.holland@sifive.com> wrote:
> >>
> >> On riscv32, the time comparator value is split across two CSRs. We write
> >> both when stopping the timer, but realistically the time is just as
> >> unlikely to reach 0xffffffff00000000 as 0xffffffffffffffff, so there is
> >> no need to write the low CSR.
> >
> > Even though unlikely, there is still a theoretical possibility of
> > counter reaching value 0xffffffff00000000.
>
> I guess that depends on the timebase frequency, but if my math is right
> then we've got some timers that will overflow a 32-bit counter in 10
> minutes -- take that with a grain of salt as they're all 64-bit systems
> (we don't have any 32-bit DTs upstream?), but it still seems plausible
> for 32-bit overflows to happen here on real systems.
>
> > The good thing about value 0xffffffffffffffff is that the counter will
> > immediately wrap around after reaching it.
>
> I'm not sure how that's good?  Luckily we've got ~100,000 years to
> figure out, even on these systems with pretty fast timers ;)

The RISC-V server soc spec mandates timer frequency to be at least
100 MHz so we can certainly have 100MHz as the timer frequency
and based on this time CSR will overflow in roughly 5840 years. Also,
nothing stops a SoC vendor from having a GHz timer frequency.

In addition to timer frequency, changing the initial value of time CSR
can also impact when the time CSR overflows. This particularly applies
to virtualization because KVM RISC-V allows the KVM user space tool
to set the desired initial value of time CSR visible to the Guest/VM.

For example, if the timer frequency is 10MHz and KVM user space
tool sets initial time CSR value to 0xffffffff_00000000 then Guest/VM
time CSR will overflow in roughly 7 mins.

Regards,
Anup

>
> > Regards,
> > Anup
> >
> >
> >>
> >> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> >> ---
> >>
> >>  drivers/clocksource/timer-riscv.c | 3 ++-
> >>  1 file changed, 2 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c
> >> index e66dcbd66566..eaaf01f3c34b 100644
> >> --- a/drivers/clocksource/timer-riscv.c
> >> +++ b/drivers/clocksource/timer-riscv.c
> >> @@ -35,9 +35,10 @@ static bool riscv_timer_cannot_wake_cpu;
> >>  static void riscv_clock_event_stop(void)
> >>  {
> >>         if (static_branch_likely(&riscv_sstc_available)) {
> >> -               csr_write(CSR_STIMECMP, ULONG_MAX);
> >>                 if (IS_ENABLED(CONFIG_32BIT))
> >>                         csr_write(CSR_STIMECMPH, ULONG_MAX);
> >> +               else
> >> +                       csr_write(CSR_STIMECMP, ULONG_MAX);
> >>         } else {
> >>                 sbi_set_timer(U64_MAX);
> >>         }
> >> --
> >> 2.43.1
> >>
> >>
>
diff mbox series

Patch

diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c
index e66dcbd66566..eaaf01f3c34b 100644
--- a/drivers/clocksource/timer-riscv.c
+++ b/drivers/clocksource/timer-riscv.c
@@ -35,9 +35,10 @@  static bool riscv_timer_cannot_wake_cpu;
 static void riscv_clock_event_stop(void)
 {
 	if (static_branch_likely(&riscv_sstc_available)) {
-		csr_write(CSR_STIMECMP, ULONG_MAX);
 		if (IS_ENABLED(CONFIG_32BIT))
 			csr_write(CSR_STIMECMPH, ULONG_MAX);
+		else
+			csr_write(CSR_STIMECMP, ULONG_MAX);
 	} else {
 		sbi_set_timer(U64_MAX);
 	}