1#include <acpi/hpet.h>
2#include <asm.h>
3#include <console/printf.h>
4#include <sch/sched.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <time/time.h>
8
9#define TIME_REFRESH_CYCLES (smp_core()->tsc_hz / 100)
10#define CMOS_ADDRESS 0x70
11#define CMOS_DATA 0x71
12
13void time_print_unix(uint32_t timestamp) {
14 uint32_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
15
16 uint32_t year = 1970;
17 while (timestamp >= 31536000) {
18 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
19 if (timestamp >= 31622400) {
20 timestamp -= 31622400;
21 } else {
22 break;
23 }
24 } else {
25 timestamp -= 31536000;
26 }
27 year++;
28 }
29
30 uint32_t month = 0;
31 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
32 days_in_month[1] = 29;
33 }
34
35 for (month = 0; month < 12; month++) {
36 if (timestamp < days_in_month[month] * 86400) {
37 break;
38 }
39 timestamp -= days_in_month[month] * 86400;
40 }
41
42 uint32_t day = timestamp / 86400 + 1;
43 timestamp %= 86400;
44
45 uint32_t hour = timestamp / 3600;
46 timestamp %= 3600;
47 uint32_t minute = timestamp / 60;
48 uint32_t second = timestamp % 60;
49
50 printf(format: "%04d-%02d-%02d %02d:%02d:%02d", year, month + 1, day, hour, minute,
51 second);
52}
53void time_print_current() {
54 time_print_unix(timestamp: time_get_unix());
55}
56
57static uint32_t is_leap(int year) {
58 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
59}
60
61static uint32_t days_in_month(int year, int month) {
62 if (month == 2) {
63 return is_leap(year) ? 29 : 28;
64 }
65 return (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 31;
66}
67
68static uint32_t datetime_to_unix(int year, int month, int day, int hour,
69 int minute, int second) {
70 unsigned int timestamp = 0;
71
72 for (int y = 1970; y < year; y++) {
73 timestamp += is_leap(year: y) ? 366 * 24 * 3600 : 365 * 24 * 3600;
74 }
75
76 for (int m = 1; m < month; m++) {
77 timestamp += days_in_month(year, month: m) * 24 * 3600;
78 }
79
80 timestamp += (day - 1) * 24 * 3600;
81
82 timestamp += hour * 3600;
83 timestamp += minute * 60;
84 timestamp += second;
85
86 return timestamp;
87}
88
89static inline uint8_t cmos_read(uint8_t reg) {
90 outb(CMOS_ADDRESS, value: reg);
91 return inb(CMOS_DATA);
92}
93
94static bool is_updating() {
95 outb(CMOS_ADDRESS, value: 0x0A);
96 return inb(CMOS_DATA) & 0x80;
97}
98
99static uint8_t bcd_to_bin(uint8_t bcd) {
100 return ((bcd / 16) * 10) + (bcd & 0x0F);
101}
102
103#define GET_TIME_UNIT(unit, cmos_addr) \
104 uint8_t time_get_##unit() { \
105 uint8_t unit = 0; \
106 while (is_updating()) \
107 ; \
108 unit = cmos_read(cmos_addr); \
109 uint8_t status_b = cmos_read(0x0B); \
110 bool bcd = !(status_b & 0x04); \
111 if (bcd) { \
112 unit = bcd_to_bin(unit); \
113 } \
114 return unit; \
115 }
116
117GET_TIME_UNIT(second, 0x00)
118GET_TIME_UNIT(minute, 0x02)
119GET_TIME_UNIT(hour, 0x04)
120GET_TIME_UNIT(day, 0x07)
121GET_TIME_UNIT(month, 0x08)
122GET_TIME_UNIT(year, 0x09)
123GET_TIME_UNIT(century, 0x32)
124
125uint32_t time_get_unix() {
126 return datetime_to_unix(year: time_get_century() * 100 + time_get_year(),
127 month: time_get_month(), day: time_get_day(), hour: time_get_hour(),
128 minute: time_get_minute(), second: time_get_second());
129}
130
131uint64_t time_get_ns() {
132 return hpet_timestamp_ns();
133}
134
135uint64_t time_get_ms(void) {
136 return time_get_us() / 1000;
137}
138
139uint64_t time_get_us(void) {
140 if (global.current_bootstage < BOOTSTAGE_MID_MP)
141 return hpet_timestamp_us();
142
143 enum irql irql = irql_raise(new_level: IRQL_HIGH_LEVEL);
144
145 uint64_t now_tsc = rdtsc();
146 uint64_t delta = now_tsc - smp_core()->last_tsc;
147
148 if (delta < TIME_REFRESH_CYCLES && smp_core()->last_us != 0) {
149 uint64_t elapsed_us = (delta * 1000000ULL) / smp_core()->tsc_hz;
150 uint64_t ret = smp_core()->last_us + elapsed_us;
151 irql_lower(old_level: irql);
152 return ret;
153 }
154
155 uint64_t now_us = hpet_timestamp_us();
156 smp_core()->last_us = now_us;
157 smp_core()->last_tsc = now_tsc;
158 irql_lower(old_level: irql);
159 return now_us;
160}
161
162uint64_t tsc_calibrate(void) {
163 uint64_t start_tsc = rdtsc();
164 uint64_t start_us = hpet_timestamp_us();
165
166 uint64_t target_us = start_us + MS_TO_US(50);
167
168 while (hpet_timestamp_us() < target_us)
169 ;
170
171 uint64_t end_tsc = rdtsc();
172 uint64_t end_us = hpet_timestamp_us();
173
174 uint64_t delta_tsc = end_tsc - start_tsc;
175 uint64_t delta_us = end_us - start_us;
176
177 uint64_t tsc_hz = (delta_tsc * 1000000ULL) / delta_us;
178 return tsc_hz;
179}
180