refactor: striped more arch-related code from the kernel code base
[lunaix-os.git] / lunaix-os / hal / ioapic.c
1 #include <sys/interrupts.h>
2 #include <hal/acpi/acpi.h>
3 #include <hal/ioapic.h>
4 #include <lunaix/common.h>
5 #include <lunaix/mm/mmio.h>
6
7 #define IOAPIC_REG_SEL *((volatile u32_t*)(_ioapic_base + IOAPIC_IOREGSEL))
8 #define IOAPIC_REG_WIN *((volatile u32_t*)(_ioapic_base + IOAPIC_IOWIN))
9
10 static volatile ptr_t _ioapic_base;
11
12 void
13 ioapic_init()
14 {
15     // Remapping the IRQs
16
17     acpi_context* acpi_ctx = acpi_get_context();
18
19     _ioapic_base =
20       (ptr_t)ioremap(acpi_ctx->madt.ioapic->ioapic_addr & ~0xfff, 4096);
21 }
22
23 void
24 ioapic_write(u8_t sel, u32_t val)
25 {
26     IOAPIC_REG_SEL = sel;
27     IOAPIC_REG_WIN = val;
28 }
29
30 u32_t
31 ioapic_read(u8_t sel)
32 {
33     IOAPIC_REG_SEL = sel;
34     return IOAPIC_REG_WIN;
35 }
36
37 void
38 ioapic_redirect(u8_t irq, u8_t vector, u8_t dest, u32_t flags)
39 {
40     u8_t reg_sel = IOAPIC_IOREDTBL_BASE + irq * 2;
41
42     // Write low 32 bits
43     ioapic_write(reg_sel, (vector | flags) & 0x1FFFF);
44
45     // Write high 32 bits
46     ioapic_write(reg_sel + 1, (dest << 24));
47 }