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

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


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
  • Date: Wed, 2 Dec 2026 08:05:18 +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=pqv1mzchhTa77ibMEhLPLlDPrqNFmXjt152lO3oChGM=; b=sCC0nCahyopYo9qHwuu1kKwNv2QXQIMEUVpEd4uUqPqI0wQCDoLe0hb/pa+OpG8vwRcWydsQ5H628dKQoIV7b6JfH7fwYoXt/cOQx+TQICbA1X1AmsEyK0OOmu/2uZ/j4IACUAg6v+6/6j16yNDvy5dmYobuurKHBiv1MtcdzfFMcbXV6Y2GMl1QgeQ8y4b+zxMVvlb2ZDZbR72sfarLMd/bWyDY/XPE4UZXJdyWw6UNrg3vkPSvyiopg8OI8XxDlfO1EupWN+Vg1REdWsxrixZYl+pacBGN+q5yuGplXPGmNXHpZ9EJk9j/nTnmIPXQamSW35qTGyI4HgUSFhl40Q==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=I025sxMEzVJIAFTsgdBp7ZQ7dxIKun9E1x+hEPxpid43aetBtHxP4Ltju/7lGGI4pXg0OWB2FVTqZ/ZJccByHxwBkqBd8A99t0PU4qqXxbqdXuqWGbJHi9qzyHGC84/y6Kw97htUxrE4jA9LX7n7Vx7oYaVYsGxpBMDG1Ct6hPckCwbuMpVlkAPeldbGRpbAjdDxQiMq9TpCG+4V7f8WKeElgRdtCDNF9WG7rzQZpO2tqqfN+Qf0Y6Qh4y62LdbwdVXKmYlMcI5qP0IzHGQeHLzrOEtNBhezNXKvd4uXOprXLyY4ub5KuvT+0LypFAkBQPIkQ05LRHa8TOK4a+TEOQ==
  • 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: Wed, 02 Dec 2026 08:05:35 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Msip_labels:
  • Thread-index: AQHdgjPfhR+iYjBthUC39lT4lb4ZsQ==
  • Thread-topic: [PATCH v2 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 v2:
- Reword the commit message and return early when cpu_khz is
  0 (Baptiste).

 xen/arch/riscv/Makefile |  1 +
 xen/arch/riscv/delay.c  | 21 +++++++++++++++++++++
 xen/arch/riscv/stubs.c  |  7 -------
 3 files changed, 22 insertions(+), 7 deletions(-)
 create mode 100644 xen/arch/riscv/delay.c

diff --git a/xen/arch/riscv/Makefile b/xen/arch/riscv/Makefile
index 665ec26933..a9aa7d1b2f 100644
--- a/xen/arch/riscv/Makefile
+++ b/xen/arch/riscv/Makefile
@@ -1,6 +1,7 @@
 obj-y += aia.o
 obj-y += aplic.o
 obj-y += cpufeature.o
+obj-y += delay.o
 obj-y += device.init.o
 obj-y += domain.o
 obj-y += domain-build.init.o
diff --git a/xen/arch/riscv/delay.c b/xen/arch/riscv/delay.c
new file mode 100644
index 0000000000..e6ba9b3a5a
--- /dev/null
+++ b/xen/arch/riscv/delay.c
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <xen/delay.h>
+#include <xen/macros.h>
+#include <xen/time.h>
+
+#include <asm/processor.h>
+
+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();
+}
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)
-- 
2.53.0




 


Rackspace

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