aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / arch / timer.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* timer.c -- EL1 physical generic timer (CNTP, PPI INTID 30) and the |
| 18 | * monotonic time services. Tick semantics match the Rust kernel: the IRQ |
| 19 | * re-arms CNTP_CVAL 10 ms after "now", then records the interval since the |
| 20 | * previous tick (the first interval is measured from arming). |
| 21 | * |
| 22 | * Both ends of an interval are the counter value read at IRQ ENTRY, the same |
| 23 | * value the next deadline is built from. An interval is therefore never |
| 24 | * shorter than the period, whatever the host does: the old code read the |
| 25 | * counter again AFTER re-arming, so a host stall between the two reads (QEMU |
| 26 | * TCG on a loaded machine) made one interval long and the next one short, and |
| 27 | * the M1 "no interval shorter than half a period" check failed (an interrupt |
| 28 | * storm or a missing EOI still shows: the next IRQ arrives at once). */ |
| 29 | #include "arch.h" |
| 30 | #include "ck_internal.h" |
| 31 | |
| 32 | static volatile uint64_t ticks, last, tmin, tmax; |
| 33 | |
| 34 | #define CK_TIMER_MIN_TICKS 5 /* the M1 gate wants at least five ticks */ |
| 35 | #define CK_TIMER_GRACE_MS 5000 /* longest extra wait for them */ |
| 36 | |
| 37 | static uint64_t counter(void) |
| 38 | { |
| 39 | ck_isb(); |
| 40 | return ck_rd(cntpct_el0); |
| 41 | } |
| 42 | |
| 43 | uint64_t ck_counter_to_us(uint64_t c, uint64_t f) |
| 44 | { |
| 45 | if (!f) |
| 46 | return 0; |
| 47 | /* floor(c * 1e6 / f) without 128-bit division. */ |
| 48 | return (c / f) * 1000000u + ((c % f) * 1000000u) / f; |
| 49 | } |
| 50 | |
| 51 | uint64_t ck_time_us(void) |
| 52 | { |
| 53 | return ck_counter_to_us(counter(), ck_rd(cntfrq_el0)); |
| 54 | } |
| 55 | |
| 56 | void ck_udelay(uint32_t us) |
| 57 | { |
| 58 | uint64_t f = ck_rd(cntfrq_el0), start = counter(); |
| 59 | uint64_t need = (uint64_t)us * (f / 1000000u) + ((uint64_t)us * (f % 1000000u)) / 1000000u; |
| 60 | while (counter() - start < need) |
| 61 | ; |
| 62 | } |
| 63 | |
| 64 | void ck_timer_tick(void) |
| 65 | { |
| 66 | uint64_t hz = ck_rd(cntfrq_el0); |
| 67 | uint64_t now = counter(); |
| 68 | ck_wr(cntp_cval_el0, now + (hz ? hz / 100 : 625000)); |
| 69 | ck_wr(cntp_ctl_el0, 1); |
| 70 | ck_isb(); |
| 71 | ticks++; |
| 72 | uint64_t interval = now - last; |
| 73 | last = now; |
| 74 | if (interval < tmin) |
| 75 | tmin = interval; |
| 76 | if (interval > tmax) |
| 77 | tmax = interval; |
| 78 | } |
| 79 | |
| 80 | int ck_timer_window(uint32_t ms, struct ck_timer_window *w) |
| 81 | { |
| 82 | uint64_t hz = ck_rd(cntfrq_el0); |
| 83 | if (!hz) |
| 84 | return -1; |
| 85 | uint64_t start = counter(); |
| 86 | ticks = 0; |
| 87 | last = start; |
| 88 | tmin = ~0ull; |
| 89 | tmax = 0; |
| 90 | ck_wr(cntp_cval_el0, start + hz / 100); |
| 91 | ck_wr(cntp_ctl_el0, 1); /* ENABLE, IMASK clear */ |
| 92 | ck_isb(); |
| 93 | ck_irq_on(); |
| 94 | ck_isb(); |
| 95 | /* The window is wall-clock. A starved vCPU (QEMU TCG on a loaded host) may |
| 96 | * see fewer than CK_TIMER_MIN_TICKS ticks in it, which says nothing about |
| 97 | * the timer. Keep counting past the window until that many ticks arrived, |
| 98 | * giving up after CK_TIMER_GRACE_MS: a dead timer still reports 0 ticks. */ |
| 99 | uint64_t window = hz / 1000 * ms; |
| 100 | uint64_t grace = hz / 1000 * CK_TIMER_GRACE_MS; |
| 101 | uint64_t t; |
| 102 | while ((t = counter()) - start < window || (ticks < CK_TIMER_MIN_TICKS && t - start < grace)) |
| 103 | ; |
| 104 | ck_irq_off(); |
| 105 | ck_isb(); |
| 106 | ck_wr(cntp_ctl_el0, 0); |
| 107 | ck_isb(); |
| 108 | w->ticks = ticks; |
| 109 | w->ms = (t - start) / (hz / 1000); |
| 110 | w->freq_hz = hz; |
| 111 | w->min_interval = ticks ? tmin : 0; |
| 112 | w->max_interval = tmax; |
| 113 | w->span = ticks ? last - start : 0; |
| 114 | return 0; |
| 115 | } |