[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3 1/3] xen/riscv: implement udelay()


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
  • Date: Fri, 4 Dec 2026 03:03:13 +0000
  • Accept-language: en-IN, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=electrifex.com; dmarc=pass action=none header.from=electrifex.com; dkim=pass header.d=electrifex.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yhFeCDFLeB11FwRM91gBKwsUHdyn7VPfudL/sghBSHU=; b=wc8PE2m0Cz3oC8SKxgFWudYQDayKqMkOhxLph9u+0obkaDztvSUMs3xuKct+zoEfE/uvpnoJFQe+VEkX2pf+cIYZd8Ytx+sljLhTmzD284IeBx7CRP0fac8lxIGElhhu5nuHnxiZWzaP1sP8BtKqZ/ZsV/aXTWGF8Y3c6z6W650m9FCwF5nOp6nXBYul5w3qqKEvj2WDR0RBV2s/gkMbe54lAfu20VW+WmjnHS55Mt3TA2AKUzuoqCiET7BtyDoOMz6F0MU6MWlygn5kQZPqPWD7rKrV/+Yp0g3gWecQlZHX2y0J4e7DQzbt1gX/eGV7tyl2aDpULD/zz78SxL+ByQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=s781SCt+Psfn2Fz7vwFQCDxsCvqoI/tXqxDBqSBtDqBmPNWbYKeIOIC7NGsrkNAF/ZPrAq2QAC8CM04GCUZ7fu9jui7i5PXn/efiM4Z5TAY/H6E5HMkzOOHX9ooC49ZnUBc8oGQPZKJqnvZMLWgkJhzvJxMkq+3df2r55rX4egCl8EoEm/tkZuD1u22njipKc59VwxhXtlFo1N7VjLEy7Mj7nANg9oiOw78GpXdnEcMwuguju7ScqV9ssJxTcl7dqsUOBf8YEl6w5v9RqDd8XPA/AKorFoCuz5YMGIyknMFY/Ja1N2DIYh/cY0MI1IQ+ixxtloqOOC5odaUhsulSRw==
  • Authentication-results: eu.smtp.expurgate.cloud; none
  • Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=electrifex.com;
  • Cc: Alistair Francis <alistair.francis@xxxxxxx>, Baptiste Le Duc <baptiste.le-duc@xxxxxxxxxx>, Connor Davis <connojdavis@xxxxxxxxx>, Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>, "andrew.cooper3@xxxxxxxxxx" <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, "roger@xxxxxxxxxxxxxx" <roger@xxxxxxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Doug Goldstein <cardoe@xxxxxxxxxx>
  • Delivery-date: Fri, 04 Dec 2026 03:03:30 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Msip_labels:
  • Thread-index: AQHdg5wAZEZ6JwOWHEy/X8GfOU18EA==
  • Thread-topic: [PATCH v3 1/3] xen/riscv: implement udelay()

udelay() is still a stub that hits BUG_ON(), so mdelay() can't be used
on RISC-V, for example to honour the delay in machine_restart().

Implement udelay() by busy-waiting on the time CSR, as Linux's RISC-V
__delay() does. Convert the delay to timer ticks once, multiplying
before dividing and rounding up so that short delays aren't cut to zero
ticks, then spin until that many ticks have passed.

cpu_khz is only set by preinit_xen_time(). Before that it is 0, and
udelay() returns without waiting.

Assisted-by: Claude
Signed-off-by: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
---
Changes in v3:
- Move udelay() into time.c (Baptiste).

Changes in v2:
- Reword the commit message and return early when cpu_khz is
  0 (Baptiste).

 xen/arch/riscv/stubs.c |  7 -------
 xen/arch/riscv/time.c  | 16 ++++++++++++++++
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/xen/arch/riscv/stubs.c b/xen/arch/riscv/stubs.c
index 3a7953593d..98231e39cc 100644
--- a/xen/arch/riscv/stubs.c
+++ b/xen/arch/riscv/stubs.c
@@ -240,13 +240,6 @@ int guest_physmap_remove_page(struct domain *d, gfn_t gfn, 
mfn_t mfn,
     BUG_ON("unimplemented");
 }
 
-/* delay.c */
-
-void udelay(unsigned long usecs)
-{
-    BUG_ON("unimplemented");
-}
-
 /* guest_access.h */
 
 static inline unsigned long raw_clear_guest(void *to, unsigned int len)
diff --git a/xen/arch/riscv/time.c b/xen/arch/riscv/time.c
index 97ebe402ae..252371ab8f 100644
--- a/xen/arch/riscv/time.c
+++ b/xen/arch/riscv/time.c
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 #include <xen/acpi.h>
+#include <xen/delay.h>
 #include <xen/device_tree.h>
 #include <xen/init.h>
 #include <xen/lib.h>
@@ -9,6 +10,7 @@
 
 #include <asm/cpufeature.h>
 #include <asm/csr.h>
+#include <asm/processor.h>
 
 unsigned long __ro_after_init cpu_khz; /* CPU clock frequency in kHz. */
 uint64_t __ro_after_init boot_clock_cycles;
@@ -29,6 +31,20 @@ s_time_t get_s_time(void)
     return ticks_to_ns(ticks);
 }
 
+void udelay(unsigned long usecs)
+{
+    cycles_t ticks, start;
+
+    if ( !cpu_khz )
+        return;
+
+    ticks = DIV_ROUND_UP((cycles_t)usecs * cpu_khz, 1000);
+    start = get_cycles();
+
+    while ( get_cycles() - start < ticks )
+        cpu_relax();
+}
+
 /* Set up the timer on the boot CPU (early init function) */
 static void __init preinit_dt_xen_time(void)
 {
-- 
2.53.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.