PCI 16x50 UART Controller, O2 Enablement (#39)
[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 (j == 8) {
55             j = 0;
56             continue;
57         }
58
59         if (k > b) {
60             break;
61         }
62
63         iv_bmp[i] |= 1 << j;
64
65         int iv = IV_BASE_END + i * 8 + j;
66         handlers[iv] = handler ? handler : intr_routine_fallback;
67
68         return iv;
69     }
70
71     return 0;
72 }
73
74 int
75 isrm_ivosalloc(isr_cb handler)
76 {
77     return __ivalloc_within(IV_BASE_END, IV_EX_BEGIN, handler);
78 }
79
80 int
81 isrm_ivexalloc(isr_cb handler)
82 {
83     return __ivalloc_within(IV_EX_BEGIN, IV_EX_END, handler);
84 }
85
86 void
87 isrm_ivfree(int iv)
88 {
89     assert(iv < 256);
90
91     if (iv >= IV_BASE_END) {
92         iv_bmp[(iv - IV_BASE_END) / 8] &= ~(1 << ((iv - IV_BASE_END) % 8));
93     }
94
95     handlers[iv] = intr_routine_fallback;
96 }
97
98 int
99 isrm_bindirq(int irq, isr_cb irq_handler)
100 {
101     int iv;
102     if (!(iv = isrm_ivexalloc(irq_handler))) {
103         fail("out of IV resource.");
104         return 0; // never reach
105     }
106
107     // fixed, edge trigged, polarity=high
108     isrm_irq_attach(irq, iv, 0, IRQ_DEFAULT);
109
110     return iv;
111 }
112
113 void
114 isrm_bindiv(int iv, isr_cb handler)
115 {
116     assert(iv < 256);
117
118     if (iv >= IV_BASE_END) {
119         iv_bmp[(iv - IV_BASE_END) / 8] |= 1 << ((iv - IV_BASE_END) % 8);
120     }
121
122     handlers[iv] = handler;
123 }
124
125 isr_cb
126 isrm_get(int iv)
127 {
128     assert(iv < 256);
129
130     return handlers[iv];
131 }
132
133 ptr_t
134 isrm_get_payload(const struct hart_state* state)
135 {
136     int iv = state->execp->vector;
137     assert(iv < 256);
138
139     return ivhand_payload[iv];
140 }
141
142 void
143 isrm_set_payload(int iv, ptr_t payload)
144 {
145     assert(iv < 256);
146
147     ivhand_payload[iv] = payload;
148 }
149
150 void
151 isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags)
152 {
153     arch_intc_ctx.irq_attach(&arch_intc_ctx, irq, iv, dest, flags);
154 }
155
156 void
157 isrm_notify_eoi(cpu_t id, int iv)
158 {
159     arch_intc_ctx.notify_eoi(&arch_intc_ctx, id, iv);
160 }
161
162 void
163 isrm_notify_eos(cpu_t id)
164 {
165     isrm_notify_eoi(id, LUNAIX_SCHED);
166 }
167
168
169 static void
170 __intc_init()
171 {
172     apic_init();
173     ioapic_init();
174
175     arch_intc_ctx.name = "i386_apic";
176     arch_intc_ctx.irq_attach = ioapic_irq_remap;
177     arch_intc_ctx.notify_eoi = apic_on_eoi;
178 }
179 owloysius_fetch_init(__intc_init, on_earlyboot);