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

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


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
  • Date: Fri, 27 Nov 2026 10:24:49 +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=gwCj/mbhTs/20oQ1imTNvAl881TFCgkXgltm39M5MBE=; b=YIbwukEr9/1gPO9yp0nha8qHSiTaoAqKqeyE+kvVW+XvXw0l1Un22eYNjfSz02BJBE7zPnls3DAPcUgQvzH1vW8AyWWwZZo/RYI0DC1mr3N1Y2vTuVl9CE5Yozd5ebnz+jGDTCe5suLRBUmSpY9t+yOo+Ri6PbESFsRrnH/w/iLyiMAshDeGDoGtRRu9pys9jDu0iwsLDOAdA+L/IbKivOjnQZNCgj6GcUlEWDYf+9Z7d3BbFa0OI0eDfRKha+X+zrr9Hx+HZSLDFprWw25R0GIIi5NCRCcgVJYekEPRCIW0SlHkIYaYBlhA2TuLO06nYJ+aH2V0Tv5i8ukZOwAh6Q==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UGPivWpJMenRtb2IL/ETZs937WLBtXrG30puysGppTH98VY0CjDv+BF37SaAFh+m23CyK/kPpfptWTFqtvi97YGq4KWIL/rxX5jK+Tsfh1dFf+JEOsroXySAvTP3PbP6e2VjJKl4+Uy8cf6ygP/4T4Skhs09ZkkqgGIacI1uhDInfBx10pvg+s3XiUEEd5JQ+QQn6OrBPuNr1DOS722fukpRFJoQ/mWCUw7Ld8ovOHPLNlX9vPunpa3xQvvY+I5bEcT+aIh5P6BnQcytoWCE8yVUIgC4I7SHJIZXIcPFLILiDMRAisJX/DgKuruZFDwbVQSdDm7mnDoxUU1zQd1eWQ==
  • 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>
  • Delivery-date: Fri, 27 Nov 2026 10:25:27 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Msip_labels:
  • Thread-index: AQHdflmI5egzTrYqpUm/DU4ZVDt4Ag==
  • Thread-topic: [PATCH 1/2] 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().

Busy-wait on the time CSR instead, 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, so the
tick count is 0 and udelay() returns without waiting. This is also why
the loop doesn't use get_s_time(): it divides by cpu_khz, which is a
division by zero before then.

Assisted-by: Claude
Signed-off-by: Anandu Padinhare <Anandu.Padinhare@xxxxxxxxxxxxxx>
---
 xen/arch/riscv/Makefile |  1 +
 xen/arch/riscv/delay.c  | 17 +++++++++++++++++
 xen/arch/riscv/stubs.c  |  7 -------
 3 files changed, 18 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..232fe02fbf
--- /dev/null
+++ b/xen/arch/riscv/delay.c
@@ -0,0 +1,17 @@
+/* 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)
+{
+    /* cpu_khz is 0 until preinit_xen_time(), so this doesn't wait then. */
+    cycles_t ticks = DIV_ROUND_UP((cycles_t)usecs * cpu_khz, 1000);
+    cycles_t 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®.