From: Hajime Tazaki <thehajime@gmail.com>
To: linux-um@lists.infradead.org
Cc: Octavian Purdila <tavi.purdila@gmail.com>,
Akira Moroo <retrage01@gmail.com>,
linux-kernel-library@freelists.org, linux-arch@vger.kernel.org,
Hajime Tazaki <thehajime@gmail.com>,
Michael Zimmermann <sigmaepsilon92@gmail.com>
Subject: [RFC v2 09/37] lkl: timers, time and delay support
Date: Fri, 8 Nov 2019 14:02:24 +0900 [thread overview]
Message-ID: <3414f0b2c46c2b65f53018fa06bb69b4c425d453.1573179553.git.thehajime@gmail.com> (raw)
In-Reply-To: <cover.1573179553.git.thehajime@gmail.com>
From: Octavian Purdila <tavi.purdila@gmail.com>
Clockevent driver based on host timer operations and clocksource
driver and udelay support based on host time operations.
Signed-off-by: Hajime Tazaki <thehajime@gmail.com>
Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
Signed-off-by: Octavian Purdila <tavi.purdila@gmail.com>
---
arch/um/lkl/include/uapi/asm/host_ops.h | 13 +++
arch/um/lkl/kernel/time.c | 145 ++++++++++++++++++++++++
2 files changed, 158 insertions(+)
create mode 100644 arch/um/lkl/kernel/time.c
diff --git a/arch/um/lkl/include/uapi/asm/host_ops.h b/arch/um/lkl/include/uapi/asm/host_ops.h
index 1c839d7139f8..c9f77dd7fbe7 100644
--- a/arch/um/lkl/include/uapi/asm/host_ops.h
+++ b/arch/um/lkl/include/uapi/asm/host_ops.h
@@ -48,6 +48,13 @@ struct lkl_jmp_buf {
* @mem_alloc - allocate memory
* @mem_free - free memory
*
+ * @timer_create - allocate a host timer that runs fn(arg) when the timer
+ * fires.
+ * @timer_free - disarms and free the timer
+ * @timer_set_oneshot - arm the timer to fire once, after delta ns.
+ * @timer_set_periodic - arm the timer to fire periodically, with a period of
+ * delta ns.
+ *
* @gettid - returns the host thread id of the caller, which need not
* be the same as the handle returned by thread_create
*
@@ -88,6 +95,12 @@ struct lkl_host_operations {
void *(*mem_alloc)(unsigned long mem);
void (*mem_free)(void *mem);
+ unsigned long long (*time)(void);
+
+ void *(*timer_alloc)(void (*fn)(void *), void *arg);
+ int (*timer_set_oneshot)(void *timer, unsigned long delta);
+ void (*timer_free)(void *timer);
+
long (*gettid)(void);
void (*jmp_buf_set)(struct lkl_jmp_buf *jmpb, void (*f)(void));
diff --git a/arch/um/lkl/kernel/time.c b/arch/um/lkl/kernel/time.c
new file mode 100644
index 000000000000..b8320e1bfa53
--- /dev/null
+++ b/arch/um/lkl/kernel/time.c
@@ -0,0 +1,145 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/clocksource.h>
+#include <linux/clockchips.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <asm/host_ops.h>
+
+static unsigned long long boot_time;
+
+void __ndelay(unsigned long nsecs)
+{
+ unsigned long long start = lkl_ops->time();
+
+ while (lkl_ops->time() < start + nsecs)
+ ;
+}
+
+void __udelay(unsigned long usecs)
+{
+ __ndelay(usecs * NSEC_PER_USEC);
+}
+
+void __const_udelay(unsigned long xloops)
+{
+ __udelay(xloops / 0x10c7ul);
+}
+
+void calibrate_delay(void)
+{
+}
+
+void read_persistent_clock(struct timespec *ts)
+{
+ *ts = ns_to_timespec(lkl_ops->time());
+}
+
+/*
+ * Scheduler clock - returns current time in nanosec units.
+ *
+ */
+unsigned long long sched_clock(void)
+{
+ if (!boot_time)
+ return 0;
+
+ return lkl_ops->time() - boot_time;
+}
+
+static u64 clock_read(struct clocksource *cs)
+{
+ return lkl_ops->time();
+}
+
+static struct clocksource clocksource = {
+ .name = "lkl",
+ .rating = 499,
+ .read = clock_read,
+ .flags = CLOCK_SOURCE_IS_CONTINUOUS,
+ .mask = CLOCKSOURCE_MASK(64),
+};
+
+static void *timer;
+
+static int timer_irq;
+
+static void timer_fn(void *arg)
+{
+ lkl_trigger_irq(timer_irq);
+}
+
+static int clockevent_set_state_shutdown(struct clock_event_device *evt)
+{
+ if (timer) {
+ lkl_ops->timer_free(timer);
+ timer = NULL;
+ }
+
+ return 0;
+}
+
+static int clockevent_set_state_oneshot(struct clock_event_device *evt)
+{
+ timer = lkl_ops->timer_alloc(timer_fn, NULL);
+ if (!timer)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static irqreturn_t timer_irq_handler(int irq, void *dev_id)
+{
+ struct clock_event_device *dev = (struct clock_event_device *)dev_id;
+
+ dev->event_handler(dev);
+
+ return IRQ_HANDLED;
+}
+
+static int clockevent_next_event(unsigned long ns,
+ struct clock_event_device *evt)
+{
+ return lkl_ops->timer_set_oneshot(timer, ns);
+}
+
+static struct clock_event_device clockevent = {
+ .name = "lkl",
+ .features = CLOCK_EVT_FEAT_ONESHOT,
+ .set_state_oneshot = clockevent_set_state_oneshot,
+ .set_next_event = clockevent_next_event,
+ .set_state_shutdown = clockevent_set_state_shutdown,
+};
+
+static struct irqaction irq0 = {
+ .handler = timer_irq_handler,
+ .flags = IRQF_NOBALANCING | IRQF_TIMER,
+ .dev_id = &clockevent,
+ .name = "timer"
+};
+
+void __init time_init(void)
+{
+ int ret;
+
+ if (!lkl_ops->timer_alloc || !lkl_ops->timer_free ||
+ !lkl_ops->timer_set_oneshot || !lkl_ops->time) {
+ pr_err("lkl: no time or timer support provided by host\n");
+ return;
+ }
+
+ timer_irq = lkl_get_free_irq("timer");
+ setup_irq(timer_irq, &irq0);
+
+ ret = clocksource_register_khz(&clocksource, 1000000);
+ if (ret)
+ pr_err("lkl: unable to register clocksource\n");
+
+ clockevents_config_and_register(&clockevent, NSEC_PER_SEC, 1,
+ ULONG_MAX);
+
+ boot_time = lkl_ops->time();
+ pr_info("lkl: time and timers initialized (irq%d)\n", timer_irq);
+}
--
2.20.1 (Apple Git-117)
next prev parent reply other threads:[~2019-11-08 5:03 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1571798507.git.thehajime@gmail.com>
2019-10-25 21:34 ` [RFC PATCH 00/47] Unifying LKL into UML Richard Weinberger
2019-10-27 2:34 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 00/37] " Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 01/37] asm-generic: atomic64: allow using generic atomic64 on 64bit platforms Hajime Tazaki
2019-11-25 22:02 ` Richard Weinberger
2019-11-26 14:02 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 02/37] arch: add __SYSCALL_DEFINE_ARCH Hajime Tazaki
2019-11-25 22:02 ` Richard Weinberger
2019-11-27 4:15 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 03/37] lkl: architecture skeleton for Linux kernel library Hajime Tazaki
2019-11-25 22:00 ` Richard Weinberger
2019-11-26 11:42 ` Octavian Purdila
2019-11-26 14:17 ` Hajime Tazaki
2019-11-26 16:02 ` Richard Weinberger
2020-02-05 7:37 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 04/37] lkl: host interface Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 05/37] lkl: memory handling Hajime Tazaki
2019-11-25 22:10 ` Richard Weinberger
2020-02-05 7:38 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 06/37] lkl: kernel threads support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 07/37] lkl: interrupt support Hajime Tazaki
2019-11-25 22:13 ` Richard Weinberger
2020-02-05 7:38 ` Hajime Tazaki
2020-02-05 10:49 ` Anton Ivanov
2020-02-05 14:24 ` Hajime Tazaki
2020-02-18 8:18 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 08/37] lkl: system call interface and application API Hajime Tazaki
2019-11-08 5:02 ` Hajime Tazaki [this message]
2019-11-08 5:02 ` [RFC v2 10/37] lkl: memory mapped I/O support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 11/37] lkl: basic kernel console support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 12/37] lkl: initialization and cleanup Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 13/37] lkl: plug in the build system Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 14/37] lkl tools: skeleton for host side library, tests and tools Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 15/37] lkl tools: host lib: add utilities functions Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 16/37] lkl tools: host lib: memory mapped I/O helpers Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 17/37] lkl tools: host lib: virtio devices Hajime Tazaki
2019-11-25 22:07 ` Richard Weinberger
2019-11-26 8:43 ` Johannes Berg
2019-11-26 8:50 ` Richard Weinberger
2019-11-26 8:52 ` Johannes Berg
2019-11-26 10:09 ` Richard Weinberger
2019-11-26 10:16 ` Johannes Berg
2019-11-26 10:42 ` Octavian Purdila
2019-11-26 10:49 ` Anton Ivanov
2019-11-27 4:06 ` Hajime Tazaki
2019-11-26 16:04 ` Richard Weinberger
2019-11-27 4:08 ` Hajime Tazaki
2019-11-27 14:28 ` Richard Weinberger
2019-11-28 9:53 ` Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 18/37] lkl tools: host lib: virtio block device Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 19/37] lkl tools: host lib: filesystem helpers Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 20/37] lkl tools: host lib: posix host operations Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 21/37] lkl tools: "boot" test Hajime Tazaki
2020-01-23 19:33 ` Brendan Higgins
2020-01-24 4:32 ` Hajime Tazaki
2020-03-02 19:51 ` Luis Chamberlain
2020-03-02 22:25 ` Brendan Higgins
2019-11-08 5:02 ` [RFC v2 22/37] lkl tools: tool that reads/writes to/from a filesystem image Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 23/37] lkl tools: tool that converts a filesystem image to tar Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 24/37] lkl tools: virtio: add network device support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 25/37] checkpatch: avoid showing BIT_ULL warnings for tools/ files Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 26/37] tools: Add the lkl host library to the common tools Makefile Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 27/37] lkl tools: add lklfuse Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 28/37] lkl: add system call hijack support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 29/37] lkl: add documentation Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 30/37] scripts: revert CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX patches Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 31/37] lkl: add support for Windows hosts Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 32/37] lkl tools: add support for Windows host Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 33/37] kallsyms: Add a config option to select section for kallsyms Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 34/37] lkl: Android ARM (arm/arm64) support Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 35/37] um lkl: add CI tests Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 36/37] um: use lkl virtio_net_tap device as UML device Hajime Tazaki
2019-11-08 5:02 ` [RFC v2 37/37] um: add lkl virtio-blk device Hajime Tazaki
2019-11-08 9:13 ` [RFC v2 00/37] Unifying LKL into UML Anton Ivanov
2019-11-08 11:17 ` Octavian Purdila
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=3414f0b2c46c2b65f53018fa06bb69b4c425d453.1573179553.git.thehajime@gmail.com \
--to=thehajime@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel-library@freelists.org \
--cc=linux-um@lists.infradead.org \
--cc=retrage01@gmail.com \
--cc=sigmaepsilon92@gmail.com \
--cc=tavi.purdila@gmail.com \
/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;
as well as URLs for NNTP newsgroup(s).