refactor: organize all arch related files together.
[lunaix-os.git] / lunaix-os / hal / apic.c
1 /**
2  * @file apic.c
3  * @author Lunaixsky
4  * @brief Abstraction for Advanced Programmable Interrupts Controller (APIC)
5  * @version 0.1
6  * @date 2022-03-06
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11 #include <hal/apic.h>
12 #include <hal/cpu.h>
13 #include <hal/pic.h>
14 #include <hal/rtc.h>
15
16 #include <arch/i386/interrupts.h>
17
18 #include <lunaix/mm/mmio.h>
19 #include <lunaix/spike.h>
20 #include <lunaix/syslog.h>
21
22 LOG_MODULE("APIC")
23
24 static volatile uintptr_t _apic_base;
25
26 void
27 apic_setup_lvts();
28
29 void
30 apic_init()
31 {
32     // ensure that external interrupt is disabled
33     cpu_disable_interrupt();
34
35     // Make sure the APIC is there
36     //  FUTURE: Use 8259 as fallback
37
38     // FIXME apic abstraction as local interrupt controller
39     // assert_msg(cpu_has_apic(), "No APIC detected!");
40
41     // As we are going to use APIC, disable the old 8259 PIC
42     pic_disable();
43
44     _apic_base = (ptr_t)ioremap(__APIC_BASE_PADDR, 4096);
45
46     // Hardware enable the APIC
47     // By setting bit 11 of IA32_APIC_BASE register
48     // Note: After this point, you can't disable then re-enable it until a
49     // reset (i.e., reboot)
50     asm volatile("movl %0, %%ecx\n"
51                  "rdmsr\n"
52                  "orl %1, %%eax\n"
53                  "wrmsr\n" ::"i"(IA32_MSR_APIC_BASE),
54                  "i"(IA32_APIC_ENABLE)
55                  : "eax", "ecx", "edx");
56
57     // Print the basic information of our current local APIC
58     u32_t apic_id = apic_read_reg(APIC_IDR) >> 24;
59     u32_t apic_ver = apic_read_reg(APIC_VER);
60
61     kprintf(KINFO "ID: %x, Version: %x, Max LVT: %u\n",
62             apic_id,
63             apic_ver & 0xff,
64             (apic_ver >> 16) & 0xff);
65
66     // initialize the local vector table (LVT)
67     apic_setup_lvts();
68
69     // initialize priority registers
70
71     // set the task priority to the lowest possible, so all external interrupts
72     // are acceptable
73     //   Note, the lowest possible priority class is 2, not 0, 1, as they are
74     //   reserved for internal interrupts (vector 0-31, and each p-class
75     //   resposible for 16 vectors). See Intel Manual Vol. 3A, 10-29
76     apic_write_reg(APIC_TPR, APIC_PRIORITY(2, 0));
77
78     // enable APIC
79     u32_t spiv = apic_read_reg(APIC_SPIVR);
80
81     // install our handler for spurious interrupt.
82     spiv = (spiv & ~0xff) | APIC_SPIV_APIC_ENABLE | APIC_SPIV_IV;
83     apic_write_reg(APIC_SPIVR, spiv);
84 }
85
86 #define LVT_ENTRY_LINT0(vector) (LVT_DELIVERY_FIXED | vector)
87
88 // Pin LINT#1 is configured for relaying NMI, but we masked it here as I think
89 //  it is too early for that
90 // LINT#1 *must* be edge trigged (Intel manual vol3. 10-14)
91 #define LVT_ENTRY_LINT1 (LVT_DELIVERY_NMI | LVT_MASKED | LVT_TRIGGER_EDGE)
92 #define LVT_ENTRY_ERROR(vector) (LVT_DELIVERY_FIXED | vector)
93
94 void
95 apic_setup_lvts()
96 {
97     apic_write_reg(APIC_LVT_LINT0, LVT_ENTRY_LINT0(APIC_LINT0_IV));
98     apic_write_reg(APIC_LVT_LINT1, LVT_ENTRY_LINT1);
99     apic_write_reg(APIC_LVT_ERROR, LVT_ENTRY_ERROR(APIC_ERROR_IV));
100 }
101
102 void
103 apic_done_servicing()
104 {
105     *(unsigned int*)(_apic_base + APIC_EOI) = 0;
106 }
107
108 unsigned int
109 apic_read_reg(unsigned int reg)
110 {
111     return *(unsigned int*)(_apic_base + (reg));
112 }
113
114 void
115 apic_write_reg(unsigned int reg, unsigned int val)
116 {
117     *(unsigned int*)(_apic_base + reg) = val;
118 }