summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-06-04 15:49:22 -0400
committerIan Moffett <ian@osmora.org>2024-06-04 15:49:41 -0400
commitd6f85b7c3f4c4d28d1a80799618535786af24b8f (patch)
tree5bc2ffa4a73efe51133a581a59a20e10456eb9be /sys
parent0263d78126287fdc81998ab2278379af9e9fab6f (diff)
kernel: Add timer abstraction
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys')
-rw-r--r--sys/dev/timer.c112
-rw-r--r--sys/include/dev/timer.h87
2 files changed, 199 insertions, 0 deletions
diff --git a/sys/dev/timer.c b/sys/dev/timer.c
new file mode 100644
index 0000000..4293508
--- /dev/null
+++ b/sys/dev/timer.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <dev/timer.h>
+
+/*
+ * When a timer on the machine has been registered
+ * to the Hyra kernel, they'll be added to the Hyra timer
+ * registry.
+ */
+static const struct timer *tmr_registry[TIMER_ID_COUNT] = { 0 };
+
+/*
+ * Returns true if the timer ID given
+ * is valid.
+ *
+ * @id: ID to verify.
+ */
+static inline bool
+is_timer_id_valid(timer_id_t id)
+{
+ return id < TIMER_ID_COUNT;
+}
+
+/*
+ * Adds timer on the machine to the timer registry. To be specific,
+ * this function writes information about the specific timer to the
+ * timer registry. However, it will not overwrite an entry. To do this
+ * you must use tmr_registry_overwrite(), of course with caution.
+ *
+ * @id: ID of timer to register.
+ * @tmr: Timer descriptor to register.
+ */
+tmrr_status_t
+register_timer(timer_id_t id, const struct timer *tmr)
+{
+ if (!is_timer_id_valid(id))
+ return TMRR_INVALID_TYPE;
+
+ if (tmr_registry[id] != NULL)
+ return TMRR_HAS_ENTRY;
+
+ tmr_registry[id] = tmr;
+ return TMRR_SUCCESS;
+}
+
+/*
+ * Overwrites an entry within the timer registery.
+ * Use with caution.
+ *
+ * @id: ID of entry to overwrite.
+ * @tmr: Timer descriptor to write.
+ */
+tmrr_status_t
+tmr_registry_overwrite(timer_id_t id, const struct timer *tmr)
+{
+ if (!is_timer_id_valid(id))
+ return TMRR_INVALID_TYPE;
+
+ tmr_registry[id] = tmr;
+ return TMRR_SUCCESS;
+}
+
+/*
+ * Requests a specific timer descriptor
+ * with a specific ID.
+ *
+ * @id: ID to request.
+ * @tmr_out: Pointer to memory that'll hold the
+ * requested descriptor.
+ */
+tmrr_status_t
+req_timer(timer_id_t id, struct timer *tmr_out)
+{
+ if (!is_timer_id_valid(id))
+ return TMRR_INVALID_TYPE;
+
+ if (tmr_registry[id] == NULL)
+ return TMRR_EMPTY_ENTRY;
+
+ if (tmr_out == NULL)
+ return TMRR_INVALID_ARG;
+
+ *tmr_out = *tmr_registry[id];
+ return TMRR_SUCCESS;
+}
diff --git a/sys/include/dev/timer.h b/sys/include/dev/timer.h
new file mode 100644
index 0000000..813f2aa
--- /dev/null
+++ b/sys/include/dev/timer.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2023-2024 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _DEV_TIMER_H_
+#define _DEV_TIMER_H_
+
+#include <sys/types.h>
+
+/* Timer IDs */
+#define TIMER_SCHED 0x00000000U /* Scheduler reserved timer */
+#define TIMER_GP 0x00000001U /* General purpose timer */
+
+/* Number of timer IDs, adjust when adding timer IDs */
+#define TIMER_ID_COUNT 2
+
+/* Timer registry status */
+#define TMRR_SUCCESS 0x00000000U /* Successful */
+#define TMRR_HAS_ENTRY 0x00000001U /* Already has an entry */
+#define TMRR_INVALID_TYPE 0x00000002U /* Invalid timer id */
+#define TMRR_EMPTY_ENTRY 0x00000003U /* Entry is empty */
+#define TMRR_INVALID_ARG 0x00000004U /* Invalid iface arg */
+
+/* See timer ID defines */
+typedef uint8_t timer_id_t;
+
+/* See timer registry status */
+typedef int tmrr_status_t;
+
+/*
+ * Represents a timer, pointer fields
+ * are optional and may be set to NULL, therefore
+ * it is paramount to verify any function or general
+ * pointer field within this struct is checked for
+ * a NULL value. Fields should be NULL if the timer
+ * driver implementation doesn't implement support
+ * for a functionality.
+ *
+ * XXX: The msleep, usleep, ... functions must return
+ * either EXIT_SUCCESS and EXIT_FAILURE from sys/errno.h
+ * ONLY.
+ */
+struct timer {
+ const char *name; /* e.g "HPET" */
+ size_t(*calibrate)(void); /* Returns frequency, 0 for unspecified */
+ size_t(*get_time_usec)(void); /* Time since init (microseconds) */
+ size_t(*get_time_sec)(void); /* Time since init (seconds) */
+ int(*msleep)(size_t ms);
+ int(*usleep)(size_t us);
+ int(*nsleep)(size_t ns);
+ void(*periodic_ms)(size_t ms);
+ void(*periodic_us)(size_t ms);
+ void(*oneshot_ms)(size_t ms);
+ void(*oneshot_us)(size_t ms);
+ void(*stop)(void);
+};
+
+tmrr_status_t register_timer(timer_id_t id, const struct timer *tmr);
+tmrr_status_t tmr_registry_overwrite(timer_id_t, const struct timer *tmr);
+tmrr_status_t req_timer(timer_id_t id, struct timer *tmr_out);
+
+#endif /* !_DEV_TIMER_H_ */