4 * @brief Abstraction for Advanced Programmable Interrupts Controller (APIC)
8 * @copyright Copyright (c) 2022
16 #include <arch/x86/interrupts.h>
18 #include <lunaix/spike.h>
19 #include <lunaix/syslog.h>
32 // ensure that external interrupt is disabled
33 cpu_disable_interrupt();
35 // Make sure the APIC is there
36 // FUTURE: Use 8259 as fallback
37 assert_msg(cpu_has_apic(), "No APIC detected!");
39 // As we are going to use APIC, disable the old 8259 PIC
42 // Hardware enable the APIC
43 // By setting bit 11 of IA32_APIC_BASE register
44 // Note: After this point, you can't disable then re-enable it until a reset (i.e., reboot)
50 ::"i"(IA32_APIC_BASE_MSR), "i"(IA32_APIC_ENABLE)
54 // Print the basic information of our current local APIC
55 uint32_t apic_id = apic_read_reg(APIC_IDR) >> 24;
56 uint32_t apic_ver = apic_read_reg(APIC_VER);
58 kprintf(KINFO "ID: %x, Version: %x, Max LVT: %u\n",
61 (apic_ver >> 16) & 0xff);
63 // initialize the local vector table (LVT)
66 // initialize priority registers
68 // set the task priority to the lowest possible, so all external interrupts are acceptable
69 // Note, the lowest possible priority class is 2, not 0, 1, as they are reserved for
70 // internal interrupts (vector 0-31, and each p-class resposible for 16 vectors).
71 // See Intel Manual Vol. 3A, 10-29
72 apic_write_reg(APIC_TPR, APIC_PRIORITY(2, 0));
75 uint32_t spiv = apic_read_reg(APIC_SPIVR);
77 // install our handler for spurious interrupt.
78 spiv = (spiv & ~0xff) | APIC_SPIV_APIC_ENABLE | APIC_SPIV_IV;
79 apic_write_reg(APIC_SPIVR, spiv);
82 #define LVT_ENTRY_LINT0(vector) (LVT_DELIVERY_FIXED | vector)
84 // Pin LINT#1 is configured for relaying NMI, but we masked it here as I think
85 // it is too early for that
86 // LINT#1 *must* be edge trigged (Intel manual vol3. 10-14)
87 #define LVT_ENTRY_LINT1 (LVT_DELIVERY_NMI | LVT_MASKED | LVT_TRIGGER_EDGE)
88 #define LVT_ENTRY_ERROR(vector) (LVT_DELIVERY_FIXED | vector)
93 apic_write_reg(APIC_LVT_LINT0, LVT_ENTRY_LINT0(APIC_LINT0_IV));
94 apic_write_reg(APIC_LVT_LINT1, LVT_ENTRY_LINT1);
95 apic_write_reg(APIC_LVT_ERROR, LVT_ENTRY_ERROR(APIC_ERROR_IV));