Architectural Support: x86_64 (#37)
[lunaix-os.git] / lunaix-os / arch / x86 / exceptions / isrm.c
1 #include <lunaix/generic/isrm.h>
2 #include <lunaix/spike.h>
3 #include <lunaix/owloysius.h>
4
5 #include "sys/x86_isa.h"
6 #include "sys/ioapic.h"
7 #include "sys/apic.h"
8
9 /*
10     total: 256 ivs
11     0~31: reserved for sys use (x32)
12     32~47: reserved for os use (x16)
13     48~  : free to allocate for external hardware use. (x208)
14 */
15
16 static char iv_bmp[(IV_EX_END - IV_BASE_END) / 8];
17 static isr_cb handlers[TOTAL_IV];
18 static ptr_t ivhand_payload[TOTAL_IV];
19
20 static struct x86_intc arch_intc_ctx;
21
22 extern void
23 intr_routine_fallback(const struct hart_state* state);
24
25 void
26 isrm_init()
27 {
28     for (size_t i = 0; i < TOTAL_IV; i++) {
29         handlers[i] = intr_routine_fallback;
30     }
31 }
32
33 static inline int
34 __ivalloc_within(size_t a, size_t b, isr_cb handler)
35 {
36     a = (a - IV_BASE_END);
37     b = (b - IV_BASE_END);
38     u8_t j = a % 8;
39     u8_t k = 0;
40
41     for (size_t i = a / 8; i < b / 8; i++, k += 8) {
42         u8_t chunk = iv_bmp[i];
43
44         if (chunk == 0xff)
45             continue;
46
47         chunk >>= j;
48         while ((chunk & 0x1) && k <= b) {
49             chunk >>= 1;
50             j++;
51             k++;
52         }
53
54         if (k > b) {
55             break;
56         }
57
58         iv_bmp[i] |= 1 << j;
59
60         int iv = IV_BASE_END + i * 8 + j;
61         handlers[iv] = handler ? handler : intr_routine_fallback;
62
63         return iv;
64     }
65
66     return 0;
67 }
68
69 int
70 isrm_ivosalloc(isr_cb handler)
71 {
72     return __ivalloc_within(IV_BASE_END, IV_EX_BEGIN, handler);
73 }
74
75 int
76 isrm_ivexalloc(isr_cb handler)
77 {
78     return __ivalloc_within(IV_EX_BEGIN, IV_EX_END, handler);
79 }
80
81 void
82 isrm_ivfree(int iv)
83 {
84     assert(iv < 256);
85
86     if (iv >= IV_BASE_END) {
87         iv_bmp[(iv - IV_BASE_END) / 8] &= ~(1 << ((iv - IV_BASE_END) % 8));
88     }
89
90     handlers[iv] = intr_routine_fallback;
91 }
92
93 int
94 isrm_bindirq(int irq, isr_cb irq_handler)
95 {
96     int iv;
97     if (!(iv = isrm_ivexalloc(irq_handler))) {
98         fail("out of IV resource.");
99         return 0; // never reach
100     }
101
102     // fixed, edge trigged, polarity=high
103     isrm_irq_attach(irq, iv, 0, IRQ_DEFAULT);
104
105     return iv;
106 }
107
108 void
109 isrm_bindiv(int iv, isr_cb handler)
110 {
111     assert(iv < 256);
112
113     if (iv >= IV_BASE_END) {
114         iv_bmp[(iv - IV_BASE_END) / 8] |= 1 << ((iv - IV_BASE_END) % 8);
115     }
116
117     handlers[iv] = handler;
118 }
119
120 isr_cb
121 isrm_get(int iv)
122 {
123     assert(iv < 256);
124
125     return handlers[iv];
126 }
127
128 ptr_t
129 isrm_get_payload(const struct hart_state* state)
130 {
131     int iv = state->execp->vector;
132     assert(iv < 256);
133
134     return ivhand_payload[iv];
135 }
136
137 void
138 isrm_set_payload(int iv, ptr_t payload)
139 {
140     assert(iv < 256);
141
142     ivhand_payload[iv] = payload;
143 }
144
145 void
146 isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags)
147 {
148     arch_intc_ctx.irq_attach(&arch_intc_ctx, irq, iv, dest, flags);
149 }
150
151 void
152 isrm_notify_eoi(cpu_t id, int iv)
153 {
154     arch_intc_ctx.notify_eoi(&arch_intc_ctx, id, iv);
155 }
156
157 void
158 isrm_notify_eos(cpu_t id)
159 {
160     isrm_notify_eoi(id, LUNAIX_SCHED);
161 }
162
163
164 static void
165 __intc_init()
166 {
167     apic_init();
168     ioapic_init();
169
170     arch_intc_ctx.name = "i386_apic";
171     arch_intc_ctx.irq_attach = ioapic_irq_remap;
172     arch_intc_ctx.notify_eoi = apic_on_eoi;
173 }
174 owloysius_fetch_init(__intc_init, on_earlyboot);