Kernelite 0.1.0
Kernelite is a minimalist, educational operating system kernel built from scratch. The project aims to recreate core components of the Linux architecture to explore the fundamentals of operating system design and hardware-software interaction.
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions | Variables
arm_timer.c File Reference

Implementation of the ARM generic system timer driver (CNTP). More...

#include "arm_timer.h"
#include "asm/asm_helper.h"
#include "fdt/fdt.h"
#include "utils/kprintf.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for arm_timer.c:

Data Structures

union  cntp_ctl_el0_t
 CNTP_CTL_EL0 (EL1 Physical Timer Control Register) layout. More...
 

Macros

#define MAX_TIMER_INTERRUPTS   4
 Maximum number of "interrupts" entries the timer node may have.
 
#define NS_PHYSICAL_TIMER_INTR_INDEX   1
 Index of the non-secure EL1 physical timer in the ARM generic timer's standard 4-entry "interrupts" property (secure, non-secure, virtual, hypervisor). This is the PPI fired by CNTP_*.
 
#define MILLISECONDS_PER_SECOND   1000U
 Number of milliseconds in a second, used to derive CNTP_TVAL_EL0 ticks from a millisecond period and CNTFRQ_EL0.
 

Typedefs

typedef union cntp_ctl_el0_t cntp_ctl_el0_t
 CNTP_CTL_EL0 (EL1 Physical Timer Control Register) layout.
 

Functions

static void timer_isr (void *priv)
 ISR for the EL1 physical timer interrupt.
 
static bool set_period (uint64_t period_ms)
 Compute and store the CNTP_TVAL_EL0 tick count for a tick period.
 
static bool setup_isr (uint32_t int_id, handler_data_t handler)
 Register the tick handler and enable the EL1 physical timer.
 
bool setup_arm64_sys_timer (const void *fdt, int node_offset, uint64_t milliseconds, handler_data_t handler)
 Configure the system timer's tick period and interrupt handler.
 

Variables

static uint64_t period_ticks
 Number of ticks CNTP_TVAL_EL0 must be loaded with to fire once per configured tick period; set by set_period() and consumed by timer_isr() to re-arm the timer after each interrupt.
 
static handler_data_t tick_handler
 Handler registered by the caller of setup_arm64_sys_timer(), invoked from timer_isr() on every tick.
 

Detailed Description

Implementation of the ARM generic system timer driver (CNTP).

Author
Abhin Parekadan Jose
Date
2026-06-28

Function Documentation

◆ set_period()

static bool set_period ( uint64_t  period_ms)
static

Compute and store the CNTP_TVAL_EL0 tick count for a tick period.

Parameters
period_msDesired tick period in milliseconds.
Returns
true if the period fits in CNTP_TVAL_EL0, false otherwise.

◆ setup_arm64_sys_timer()

bool setup_arm64_sys_timer ( const void *  fdt,
int  node_offset,
uint64_t  milliseconds,
handler_data_t  handler 
)

Configure the system timer's tick period and interrupt handler.

Parameters
fdtPointer to the Flattened Device Tree (FDT) blob, which may be used to discover timer properties and configuration.
node_offsetOffset of the timer node in the FDT.
millisecondsDesired tick period in milliseconds.
handlerHandler function and private data to invoke when the timer interrupt fires.
Returns
true if the timer was configured successfully, false otherwise.

◆ setup_isr()

static bool setup_isr ( uint32_t  int_id,
handler_data_t  handler 
)
static

Register the tick handler and enable the EL1 physical timer.

Parameters
int_idGIC INTID of the EL1 physical timer interrupt.
handlerHandler function and private data to invoke on each tick.
Returns
true if the ISR was registered and the timer enabled successfully, false otherwise.

◆ timer_isr()

static void timer_isr ( void *  priv)
static

ISR for the EL1 physical timer interrupt.

Masks the timer, invokes the registered tick handler if the timer condition was actually met, reloads CNTP_TVAL_EL0 for the next period, and unmasks/re-enables the timer.

Parameters
privUnused; registered with NULL private data via icu_register_irq().