/* SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #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);