PatchworkOS  dbbdc99
A non-POSIX operating system.
Loading...
Searching...
No Matches
apic_timer.c
Go to the documentation of this file.
3
4#include <kernel/cpu/cpu.h>
5#include <kernel/cpu/irq.h>
6#include <kernel/log/log.h>
10
11#include <stdint.h>
12#include <sys/defs.h>
13
32
33static void apic_timer_set(irq_virt_t virt, clock_t uptime, clock_t timeout)
34{
36
37 CLI_SCOPE();
38
39 if (_pcpu_lapic->ticksPerMs == 0)
40 {
42 }
43
46
47 if (timeout == CLOCKS_NEVER)
48 {
49 return;
50 }
51
52 uint64_t ticks = (timeout * _pcpu_lapic->ticksPerMs) / (CLOCKS_PER_MS);
53 if (ticks == 0)
54 {
55 ticks = 1;
56 }
57 if (ticks > UINT32_MAX)
58 {
59 ticks = UINT32_MAX;
60 }
61
65}
66
67static void apic_timer_eoi(void)
68{
70}
71
72/**
73 * According to https://telematics.tm.kit.edu/publications/Files/61/walter_ibm_linux_challenge.pdf, the APIC timer has a
74 * precision of 1 microsecond.
75 */
77 .name = "APIC Timer",
78 .precision = 1000, // 1 microsecond
79 .set = apic_timer_set,
80 .ack = NULL,
81 .eoi = apic_timer_eoi,
82};
83
85{
87 {
88 LOG_ERR("failed to register apic timer source\n");
89 return ERR;
90 }
91
92 return 0;
93}
static void apic_timer_eoi(void)
Definition apic_timer.c:67
static timer_source_t apicTimer
Definition apic_timer.c:76
static void apic_timer_set(irq_virt_t virt, clock_t uptime, clock_t timeout)
Definition apic_timer.c:33
static uint64_t apic_timer_ticks_per_ms(void)
Definition apic_timer.c:14
#define CLOCKS_NEVER
Definition clock_t.h:18
#define CLOCKS_PER_MS
Definition clock_t.h:16
#define CLI_SCOPE()
Macro to increment CLI depth for the duration of the current scope.
Definition cli.h:56
uint8_t irq_virt_t
Virtual IRQ numbers.
Definition irq.h:57
uint32_t lapic_read(uint32_t reg)
Read from a local apic register.
Definition lapic.c:37
lapic_t PERCPU _pcpu_lapic
The per-CPU local APIC structure.
void lapic_write(uint32_t reg, uint32_t value)
Write to a local apic register.
Definition lapic.c:42
@ LAPIC_REG_TIMER_CURRENT_COUNT
Definition lapic.h:76
@ LAPIC_REG_TIMER_INITIAL_COUNT
Definition lapic.h:75
@ LAPIC_REG_TIMER_DIVIDER
Definition lapic.h:77
@ LAPIC_REG_EOI
Definition lapic.h:57
@ LAPIC_REG_LVT_TIMER
Definition lapic.h:69
uint64_t apic_timer_init(void)
Initialize the APIC timer.
Definition apic_timer.c:84
@ APIC_TIMER_ONE_SHOT
Definition apic_timer.h:26
@ APIC_TIMER_MASKED
Timer is masked (disabled)
Definition apic_timer.h:24
@ APIC_TIMER_DIV_DEFAULT
Definition apic_timer.h:39
#define LOG_ERR(format,...)
Definition log.h:93
void clock_wait(clock_t nanoseconds)
Wait for a specified number of nanoseconds.
Definition clock.c:121
uint64_t timer_source_register(const timer_source_t *source)
Register a timer source.
Definition timer.c:54
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:96
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
#define NULL
Pointer error value.
Definition NULL.h:25
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define UINT32_MAX
Definition stdint.h:70
uint64_t ticksPerMs
Initialized to 0, set on first use of the APIC timer on the CPU.
Definition lapic.h:126
Timer source structure.
Definition timer.h:52
const char * name
Definition timer.h:53