Merge branch 'master' into signal-dev
[lunaix-os.git] / lunaix-os / includes / hal / apic.h
1 #ifndef __LUNAIX_APIC_H
2 #define __LUNAIX_APIC_H
3
4 #include <stdint.h>
5
6 #define APIC_BASE_VADDR 0x1000
7 #define __APIC_BASE_PADDR 0xFEE00000
8
9 #define IA32_MSR_APIC_BASE 0x1B
10 #define IA32_APIC_ENABLE   0x800
11
12 /*
13  *  Common APIC memory-mapped registers 
14  *              Ref: Intel Manual, Vol. 3A, Table 10-1
15  */
16
17 #define APIC_IDR        0x20        // ID Reg
18 #define APIC_VER        0x30        // Version Reg
19 #define APIC_TPR        0x80        // Task Priority
20 #define APIC_APR        0x90        // Arbitration Priority
21 #define APIC_PPR        0xA0        // Processor Priority
22 #define APIC_EOI        0xB0        // End-Of-Interrupt
23 #define APIC_RRD        0xC0        // Remote Read
24 #define APIC_LDR        0xD0        // Local Destination Reg
25 #define APIC_DFR        0xE0        // Destination Format Reg
26 #define APIC_SPIVR      0xF0        // Spurious Interrupt Vector Reg
27 #define APIC_ISR_BASE   0x100       // Base address for In-Service-Interrupt bitmap register (256bits)
28 #define APIC_TMR_BASE   0x180       // Base address for Trigger-Mode bitmap register (256bits)
29 #define APIC_IRR_BASE   0x200       // Base address for Interrupt-Request bitmap register (256bits)
30 #define APIC_ESR        0x280       // Error Status Reg
31 #define APIC_ICR_BASE   0x300       // Interrupt Command
32 #define APIC_LVT_LINT0  0x350
33 #define APIC_LVT_LINT1  0x360
34 #define APIC_LVT_ERROR  0x370
35
36 // APIC Timer specific
37 #define APIC_TIMER_LVT  0x320
38 #define APIC_TIMER_ICR  0x380       // Initial Count
39 #define APIC_TIMER_CCR  0x390       // Current Count
40 #define APIC_TIMER_DCR  0x3E0       // Divide Configuration
41
42 #define APIC_SPIV_FOCUS_DISABLE     0x200
43 #define APIC_SPIV_APIC_ENABLE       0x100
44 #define APIC_SPIV_EOI_BROADCAST     0x1000
45
46 #define LVT_DELIVERY_FIXED          0
47 #define LVT_DELIVERY_NMI            (0x4 << 8)
48 #define LVT_TRIGGER_EDGE            (0   << 15)
49 #define LVT_TRIGGER_LEVEL           (1   << 15)
50 #define LVT_MASKED                  (1   << 16)
51 #define LVT_TIMER_ONESHOT           (0   << 17)
52 #define LVT_TIMER_PERIODIC          (1   << 17)
53
54 // Dividers for timer. See Intel Manual Vol3A. 10-17 (pp. 3207), Figure 10-10
55 #define APIC_TIMER_DIV1             0b1011
56 #define APIC_TIMER_DIV2             0b0000
57 #define APIC_TIMER_DIV4             0b0001
58 #define APIC_TIMER_DIV8             0b0010
59 #define APIC_TIMER_DIV16            0b0011
60 #define APIC_TIMER_DIV32            0b1000
61 #define APIC_TIMER_DIV64            0b1001
62 #define APIC_TIMER_DIV128           0b1010
63
64 #define APIC_PRIORITY(cls, subcls)  (((cls) << 4) | (subcls))
65
66 #define apic_read_reg(reg)           (*(uint32_t*)(APIC_BASE_VADDR + (reg)))
67 #define apic_write_reg(reg, val)     (*(uint32_t*)(APIC_BASE_VADDR + (reg)) = (val))
68
69 void
70 apic_init();
71
72 /**
73  * @brief Tell the APIC that the handler for current interrupt is finished.
74  * This will issue a write action to EOI register.
75  * 
76  */
77 inline static void
78 apic_done_servicing() {
79     apic_write_reg(APIC_EOI, 0);
80 }
81
82 #endif /* __LUNAIX_APIC_H */