39 lines
936 B
C
39 lines
936 B
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/requests.h>
|
|
#include <kernel/time.h>
|
|
#include <kernel/driver.h>
|
|
#include <kernel/common.h>
|
|
|
|
#define NSEC_PER_SEC 1000000000
|
|
|
|
struct {
|
|
u64 tsc_hz;
|
|
time_t boot_tsc;
|
|
time_t boot_unix_seconds;
|
|
} timekeeper = {0};
|
|
|
|
struct timespec get_realtime()
|
|
{
|
|
u64 elapsed = rdtsc() - timekeeper.boot_tsc;
|
|
u64 sec = elapsed / timekeeper.tsc_hz;
|
|
u64 r = elapsed % timekeeper.tsc_hz;
|
|
|
|
struct timespec ret = {.tv_sec = timekeeper.boot_unix_seconds + sec, .tv_nsec = (r * NSEC_PER_SEC) / timekeeper.tsc_hz};
|
|
return ret;
|
|
}
|
|
|
|
time_t unix_time()
|
|
{
|
|
u64 sec = (rdtsc() - timekeeper.boot_tsc) / timekeeper.tsc_hz;
|
|
return timekeeper.boot_unix_seconds + sec;
|
|
}
|
|
|
|
void time_init()
|
|
{
|
|
timekeeper.tsc_hz = tsc_calibrate();
|
|
timekeeper.boot_unix_seconds = arch_get_real_time();
|
|
timekeeper.boot_tsc = rdtsc();
|
|
|
|
krdy("time system ready. tsc_hz: %llu\n", timekeeper.tsc_hz);
|
|
}
|
|
DRIVER_ENTRY(time_init);
|