Merge branch 'master' of github.com:Minep/lunaix-os
[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/x86/interrupts.h>
17
18 #include <lunaix/spike.h>
19 #include <lunaix/syslog.h>
20
21 LOG_MODULE("APIC")
22
23 void
24 apic_setup_lvts();
25
26 void
27 apic_init()
28 {
29     // ensure that external interrupt is disabled
30     cpu_disable_interrupt();
31
32     // Make sure the APIC is there
33     //  FUTURE: Use 8259 as fallback
34     assert_msg(cpu_has_apic(), "No APIC detected!");
35
36     // As we are going to use APIC, disable the old 8259 PIC
37     pic_disable();
38
39     // Hardware enable the APIC
40     // By setting bit 11 of IA32_APIC_BASE register
41     // Note: After this point, you can't disable then re-enable it until a reset
42     // (i.e., reboot)
43     asm volatile("movl %0, %%ecx\n"
44                  "rdmsr\n"
45                  "orl %1, %%eax\n"
46                  "wrmsr\n" ::"i"(IA32_MSR_APIC_BASE),
47                  "i"(IA32_APIC_ENABLE)
48                  : "eax", "ecx", "edx");
49
50     // Print the basic information of our current local APIC
51     uint32_t apic_id = apic_read_reg(APIC_IDR) >> 24;
52     uint32_t apic_ver = apic_read_reg(APIC_VER);
53
54     kprintf(KINFO "ID: %x, Version: %x, Max LVT: %u\n",
55             apic_id,
56             apic_ver & 0xff,
57             (apic_ver >> 16) & 0xff);
58
59     // initialize the local vector table (LVT)
60     apic_setup_lvts();
61
62     // initialize priority registers
63
64     // set the task priority to the lowest possible, so all external interrupts
65     // are acceptable
66     //   Note, the lowest possible priority class is 2, not 0, 1, as they are
67     //   reserved for internal interrupts (vector 0-31, and each p-class
68     //   resposible for 16 vectors). See Intel Manual Vol. 3A, 10-29
69     apic_write_reg(APIC_TPR, APIC_PRIORITY(2, 0));
70
71     // enable APIC
72     uint32_t spiv = apic_read_reg(APIC_SPIVR);
73
74     // install our handler for spurious interrupt.
75     spiv = (spiv & ~0xff) | APIC_SPIV_APIC_ENABLE | APIC_SPIV_IV;
76     apic_write_reg(APIC_SPIVR, spiv);
77 }
78
79 #define LVT_ENTRY_LINT0(vector) (LVT_DELIVERY_FIXED | vector)
80
81 // Pin LINT#1 is configured for relaying NMI, but we masked it here as I think
82 //  it is too early for that
83 // LINT#1 *must* be edge trigged (Intel manual vol3. 10-14)
84 #define LVT_ENTRY_LINT1 (LVT_DELIVERY_NMI | LVT_MASKED | LVT_TRIGGER_EDGE)
85 #define LVT_ENTRY_ERROR(vector) (LVT_DELIVERY_FIXED | vector)
86
87 void
88 apic_setup_lvts()
89 {
90     apic_write_reg(APIC_LVT_LINT0, LVT_ENTRY_LINT0(APIC_LINT0_IV));
91     apic_write_reg(APIC_LVT_LINT1, LVT_ENTRY_LINT1);
92     apic_write_reg(APIC_LVT_ERROR, LVT_ENTRY_ERROR(APIC_ERROR_IV));
93 }