ZXFoundation™ 26h2
Loading...
Searching...
No Matches
core.cxxm
1/// SPDX-License-Identifier: Apache-2.0
2/// @file zxfoundation/sys/timer.cxxm
3/// @brief Generic kernel timer subsystem — per-CPU min-heap with clock comparator arming.
4
5export module zxfoundation.sys.timer.core;
6import zxfoundation.sys.timer.types;
7import zxfoundation.base.types;
8import arch.s390x.cpu.irq;
9import lib.error;
10import std;
11
12export {
13
14namespace zxfoundation::sys::timer {
15
16 /// @brief Convert nanoseconds to TOD clock units.
17 [[nodiscard]] constexpr auto ns_to_tod(u64 ns) noexcept -> u64 {
18 return static_cast<u64>(
19 (static_cast<u128>(ns) * 4096ULL) / 1000ULL);
20 }
21
22 /// @brief Convert TOD clock units to nanoseconds.
23 [[nodiscard]] constexpr auto tod_to_ns(u64 tod) noexcept -> u64 {
24 return static_cast<u64>(
25 (static_cast<u128>(tod) * 1000ULL) / 4096ULL);
26 }
27
28 /// @brief Initialize the timer subsystem on an application processor.
29 constexpr auto init_ap(u16 cpu_id) noexcept -> void { discard_value cpu_id; }
30
31 /// @brief Add a timer to the current CPU's heap and arm the clock comparator.
32 [[nodiscard]] auto timer_add(kernel_timer& t) noexcept
33 -> std::expected<void, lib::kernel_error>;
34
35 /// @brief Cancel an armed timer.
36 [[nodiscard]] auto timer_cancel(kernel_timer& t) noexcept
37 -> std::expected<void, lib::kernel_error>;
38
39 /// @brief Clock comparator interrupt handler.
40 auto timer_interrupt() noexcept -> void;
41
42 /// @brief Initialize the timer subsystem on the BSP (CPU 0).
43 auto init() noexcept -> void;
44
45} // namespace zxfoundation::sys::timer
46
47} // end export