PatchworkOS  321f6ec
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 <kernel/defs.h>
12#include <stdint.h>
13
33
34static void apic_timer_set(irq_virt_t virt, clock_t uptime, clock_t timeout)
35{
36 (void)uptime;
37
39 if (lapic->ticksPerMs == 0)
40 {
42 }
43
46
47 if (timeout == CLOCKS_NEVER)
48 {
49 return;
50 }
51
52 uint64_t ticks = (timeout * lapic->ticksPerMs) / (CLOCKS_PER_SEC / 1000);
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(cpu_t* cpu)
68{
69 (void)cpu;
70
72}
73
74/**
75 * According to https://telematics.tm.kit.edu/publications/Files/61/walter_ibm_linux_challenge.pdf, the APIC timer has a
76 * precision of 1 microsecond.
77 */
79 .name = "APIC Timer",
80 .precision = 1000, // 1 microsecond
81 .set = apic_timer_set,
82 .ack = NULL,
83 .eoi = apic_timer_eoi,
84};
85
87{
89 {
90 LOG_ERR("failed to register apic timer source\n");
91 return ERR;
92 }
93
94 return 0;
95}
static timer_source_t apicTimer
Definition apic_timer.c:78
static void apic_timer_eoi(cpu_t *cpu)
Definition apic_timer.c:67
static void apic_timer_set(irq_virt_t virt, clock_t uptime, clock_t timeout)
Definition apic_timer.c:34
static uint64_t apic_timer_ticks_per_ms(void)
Definition apic_timer.c:14
#define CLOCKS_NEVER
Definition clock_t.h:16
#define CLOCKS_PER_SEC
Definition clock_t.h:15
void interrupt_disable(void)
Disable interrupts and increment the disableDepth.
Definition interrupt.c:25
void interrupt_enable(void)
Decrement the CLI depth and enable interrupts if depth reaches zero and interrupts were previously en...
Definition interrupt.c:37
uint8_t irq_virt_t
Virtual IRQ numbers.
Definition irq.h:57
static cpuid_t cpu_get_id_unsafe(void)
Gets the current CPU ID.
Definition cpu.h:316
#define LOG_ERR(format,...)
Definition log.h:108
void clock_wait(clock_t nanoseconds)
Wait for a specified number of nanoseconds.
Definition clock.c:130
uint64_t timer_source_register(const timer_source_t *source)
Register a timer source.
Definition timer.c:49
clock_t uptime(void)
System call for retreving the time since boot.
Definition uptime.c:6
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
lapic_t * lapic_get(uint32_t cpuId)
Get the lapic data for the specified CPU.
Definition lapic.c:49
uint32_t lapic_read(uint32_t reg)
Read from a local apic register.
Definition lapic.c:18
void lapic_write(uint32_t reg, uint32_t value)
Write to a local apic register.
Definition lapic.c:23
@ LAPIC_REG_TIMER_CURRENT_COUNT
Definition lapic.h:74
@ LAPIC_REG_TIMER_INITIAL_COUNT
Definition lapic.h:73
@ LAPIC_REG_TIMER_DIVIDER
Definition lapic.h:75
@ LAPIC_REG_EOI
Definition lapic.h:55
@ LAPIC_REG_LVT_TIMER
Definition lapic.h:67
uint64_t apic_timer_init(void)
Initialize the APIC timer subsystem.
Definition apic_timer.c:86
@ 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
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define UINT32_MAX
Definition stdint.h:70
CPU structure.
Definition cpu.h:122
Local APIC Structure.
Definition lapic.h:123
uint64_t ticksPerMs
Initialized to 0, set on first use of the APIC timer on the CPU.
Definition lapic.h:124
Timer source structure.
Definition timer.h:67
const char * name
Definition timer.h:68