mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Antony Pavlov <antonynpavlov@gmail.com>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] clocksource: timer-riscv: adapt riscv_timer_get_count_rdcycle() for RV32
Date: Tue, 30 Mar 2021 01:31:18 +0300	[thread overview]
Message-ID: <20210329223120.442256-2-antonynpavlov@gmail.com> (raw)
In-Reply-To: <20210329223120.442256-1-antonynpavlov@gmail.com>

On RV64 rdcycle instruction reads 64-bit counter which holds
a count of the number of clock cycles executed by the processor
core.
On RV32 rdcycle instruction reads only bits 31-0 of the same
counter; RDCYCLEH should be used to read bits 63–32.

The code of this patch is based on Figure 2.5: 'Sample code
for reading the 64-bit cycle counter in RV32' [1]:

    again:
      rdcycleh     x3
      rdcycle      x2
      rdcycleh     x4
      bne          x3, x4, again

  [1] The RISC-V Instruction Set Manual. Volume I:
        User-Level ISA, Document Version 2.2

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/clocksource/timer-riscv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/clocksource/timer-riscv.c b/drivers/clocksource/timer-riscv.c
index eb5ba2d8c2..ef67cff475 100644
--- a/drivers/clocksource/timer-riscv.c
+++ b/drivers/clocksource/timer-riscv.c
@@ -30,10 +30,17 @@ static u64 notrace riscv_timer_get_count_sbi(void)
 
 static u64 notrace riscv_timer_get_count_rdcycle(void)
 {
-	u64 ticks;
-	asm volatile("rdcycle %0" : "=r" (ticks));
+	__maybe_unused u32 hi, lo;
 
-	return ticks;
+	if (IS_ENABLED(CONFIG_64BIT))
+		return csr_read(CSR_CYCLE);
+
+	do {
+		hi = csr_read(CSR_CYCLEH);
+		lo = csr_read(CSR_CYCLE);
+	} while (hi != csr_read(CSR_CYCLEH));
+
+	return ((u64)hi << 32) | lo;
 }
 
 static u64 notrace riscv_timer_get_count(void)
-- 
2.30.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2021-03-30  1:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-29 22:31 [PATCH 0/3] RISC-V cycle timer fixes Antony Pavlov
2021-03-29 22:31 ` Antony Pavlov [this message]
2021-03-29 22:31 ` [PATCH 2/3] RISC-V: erizo.dtsi: set timebase-frequency = <24000000> Antony Pavlov
2021-03-29 22:31 ` [PATCH 3/3] RISC-V: drop old timer handling code Antony Pavlov
2021-03-30  5:36 ` [PATCH 0/3] RISC-V cycle timer fixes Sascha Hauer

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=20210329223120.442256-2-antonynpavlov@gmail.com \
    --to=antonynpavlov@gmail.com \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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