aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2022-04-17 13:40:56 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2022-04-17 13:40:56 +0300
commit1ceed7525272b82c1028ebd353e65c3a63bd2714 (patch)
tree828105db350e36df681bf79740434b47e6748e36
parent2a256aa4f1abb65afa6f3856998bfe00ac960d61 (diff)
downloadkmi-1ceed7525272b82c1028ebd353e65c3a63bd2714.tar.gz
kmi-1ceed7525272b82c1028ebd353e65c3a63bd2714.zip
expand sbi implementation a bit
-rw-r--r--arch/riscv64/include/sbi.h36
-rw-r--r--arch/riscv64/kernel/power.c31
-rw-r--r--arch/riscv64/kernel/sbi.c28
-rw-r--r--common/uapi/conf.c17
-rw-r--r--include/apos/power.h10
-rw-r--r--include/apos/types.h1
6 files changed, 120 insertions, 3 deletions
diff --git a/arch/riscv64/include/sbi.h b/arch/riscv64/include/sbi.h
index e6ceb92..b750475 100644
--- a/arch/riscv64/include/sbi.h
+++ b/arch/riscv64/include/sbi.h
@@ -1,13 +1,49 @@
#ifndef APOS_RISCV_SBI_H
+#define APOS_RISCV_SBI_H
+
+#include <apos/types.h>
struct sbiret {
long error;
long value;
};
+enum {
+ SBI_SUCCESS = 0,
+ SBI_ERR_FAILED = -1,
+ SBI_ERR_NOT_SUPPORTED = -2,
+ SBI_ERR_INVALID_PARAM = -3,
+ SBI_ERR_DENIED = -4,
+ SBI_ERR_INVALID_ADDRESS = -5,
+ SBI_ERR_ALREADY_AVAILABLE = -6,
+ SBI_ERR_ALREADY_STARTED = -7,
+ SBI_ERR_ALREADY_STOPEED = -8,
+};
+
+/* TODO: query which extensions are available */
struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
unsigned long arg1, unsigned long arg2,
unsigned long arg3, unsigned long arg4,
unsigned long arg5);
+#define EID_TIME 0x54494D45
+#define FID_SET_TIMER 0
+static inline struct sbiret sbi_set_timer(uint64_t stime_value)
+{
+ /* TODO: read timebase from fdt, seems to be clocks/sec for accurate
+ * timers */
+#if __riscv_xlen == 32
+ return sbi_ecall(EID_TIME, FID_SET_TIMER, stime_value, stime_value >> 32, 0, 0, 0, 0);
+#else
+ return sbi_ecall(EID_TIME, FID_SET_TIMER, stime_value, 0, 0, 0, 0, 0);
+#endif
+}
+
+#define EID_SRST 0x53525354
+#define FID_RESET 0
+static inline struct sbiret sbi_system_reset(uint32_t reset_type, uint32_t reset_reason)
+{
+ return sbi_ecall(EID_SRST, FID_RESET, reset_type, reset_reason, 0, 0, 0, 0);
+}
+
#endif /* APOS_RISCV_SBI_H */
diff --git a/arch/riscv64/kernel/power.c b/arch/riscv64/kernel/power.c
new file mode 100644
index 0000000..877c001
--- /dev/null
+++ b/arch/riscv64/kernel/power.c
@@ -0,0 +1,31 @@
+#include <apos/power.h>
+#include <sbi.h>
+
+/* shutdown reason */
+#define SBI_NO_REASON 0
+
+/* shutdown type */
+#define SBI_SHUTDOWN 0
+#define SBI_COLD_REBOOT 1
+#define SBI_WARM_REBOOT 2
+
+stat_t poweroff(enum poweroff_type type)
+{
+ /* TODO: this only shuts down the cpu itself, but may leave the SOC
+ * active. Should read from fdt poweroff and syscon-poweroff etc */
+ switch (type) {
+ case SHUTDOWN:
+ sbi_system_reset(SBI_SHUTDOWN, SBI_NO_REASON);
+ return ERR_MISC;
+
+ case COLD_REBOOT:
+ sbi_system_reset(SBI_COLD_REBOOT, SBI_NO_REASON);
+ return ERR_MISC;
+
+ case WARM_REBOOT:
+ sbi_system_reset(SBI_WARM_REBOOT, SBI_NO_REASON);
+ return ERR_MISC;
+ };
+
+ return ERR_INVAL;
+}
diff --git a/arch/riscv64/kernel/sbi.c b/arch/riscv64/kernel/sbi.c
new file mode 100644
index 0000000..cd3eb1b
--- /dev/null
+++ b/arch/riscv64/kernel/sbi.c
@@ -0,0 +1,28 @@
+#include <sbi.h>
+
+struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
+ unsigned long arg1, unsigned long arg2,
+ unsigned long arg3, unsigned long arg4,
+ unsigned long arg5)
+{
+ struct sbiret ret;
+
+ register uintptr_t a0 __asm__ ("a0") = (uintptr_t)(arg0);
+ register uintptr_t a1 __asm__ ("a1") = (uintptr_t)(arg1);
+ register uintptr_t a2 __asm__ ("a2") = (uintptr_t)(arg2);
+ register uintptr_t a3 __asm__ ("a3") = (uintptr_t)(arg3);
+ register uintptr_t a4 __asm__ ("a4") = (uintptr_t)(arg4);
+ register uintptr_t a5 __asm__ ("a5") = (uintptr_t)(arg5);
+ register uintptr_t a6 __asm__ ("a6") = (uintptr_t)(fid);
+ register uintptr_t a7 __asm__ ("a7") = (uintptr_t)(ext);
+
+ __asm__ volatile ("ecall"
+ : "+r" (a0), "+r" (a1)
+ : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
+ : "memory");
+
+ ret.error = a0;
+ ret.value = a1;
+
+ return ret;
+}
diff --git a/common/uapi/conf.c b/common/uapi/conf.c
index 56e07a1..7acb540 100644
--- a/common/uapi/conf.c
+++ b/common/uapi/conf.c
@@ -1,3 +1,4 @@
+#include <apos/power.h>
#include <apos/sizes.h>
#include <apos/uapi.h>
@@ -8,8 +9,12 @@ vm_t sys_conf(vm_t param, vm_t val, vm_t u0, vm_t u1)
{
UNUSED(u0);
UNUSED(u1);
+ UNUSED(param);
+ UNUSED(val);
+
/* no parameters supported atm */
- return 0;
+
+ return OK;
}
vm_t sys_poweroff(vm_t type, vm_t u0, vm_t u1, vm_t u2)
@@ -17,6 +22,12 @@ vm_t sys_poweroff(vm_t type, vm_t u0, vm_t u1, vm_t u2)
UNUSED(u0);
UNUSED(u1);
UNUSED(u2);
- /* powering off not supported yet, you're stuck here >:D */
- return 0;
+ switch (type) {
+ case SHUTDOWN:
+ case COLD_REBOOT:
+ case WARM_REBOOT:
+ return poweroff(type);
+ };
+
+ return ERR_INVAL;
}
diff --git a/include/apos/power.h b/include/apos/power.h
new file mode 100644
index 0000000..e0ca518
--- /dev/null
+++ b/include/apos/power.h
@@ -0,0 +1,10 @@
+#ifndef APOS_POWER_H
+#define APOS_POWER_H
+
+#include <apos/types.h>
+#include <apos/attrs.h>
+
+enum poweroff_type {SHUTDOWN, COLD_REBOOT, WARM_REBOOT};
+stat_t poweroff(enum poweroff_type type);
+
+#endif
diff --git a/include/apos/types.h b/include/apos/types.h
index 2f06ec4..01c5317 100644
--- a/include/apos/types.h
+++ b/include/apos/types.h
@@ -184,6 +184,7 @@ typedef uint_fast16_t vmflags_t;
/* negative error codes are reserved for general usage, positive error codes are
* allowed to be function-specific. */
enum {
+ ERR_MISC = -8, /* something */
ERR_NOINIT = -7, /* not initialized */
ERR_INVAL = -6, /* invalid value */
ERR_EXT = -5, /* already exists */