refactor: one more step towards arch-agnostic design
[lunaix-os.git] / lunaix-os / arch / i386 / exceptions / i386_isrm.c
1 #include <lunaix/isrm.h>
2 #include <lunaix/spike.h>
3
4 #include <hal/intc.h>
5
6 /*
7     total: 256 ivs
8     0~31: reserved for sys use (x32)
9     32~47: reserved for os use (x16)
10     48~  : free to allocate for external hardware use. (x208)
11 */
12
13 static char iv_bmp[(IV_EX_END - IV_BASE_END) / 8];
14 static isr_cb handlers[TOTAL_IV];
15
16 extern void
17 intr_routine_fallback(const isr_param* param);
18
19 void
20 isrm_init()
21 {
22     for (size_t i = 0; i < TOTAL_IV; i++) {
23         handlers[i] = intr_routine_fallback;
24     }
25 }
26
27 static inline int
28 __ivalloc_within(size_t a, size_t b, isr_cb handler)
29 {
30     a = (a - IV_BASE_END);
31     b = (b - IV_BASE_END);
32     u8_t j = a % 8;
33     u8_t k = 0;
34
35     for (size_t i = a / 8; i < b / 8; i++, k += 8) {
36         u8_t chunk = iv_bmp[i];
37
38         if (chunk == 0xff)
39             continue;
40
41         chunk >>= j;
42         while ((chunk & 0x1) && k <= b) {
43             chunk >>= 1;
44             j++;
45             k++;
46         }
47
48         if (k > b) {
49             break;
50         }
51
52         iv_bmp[i] |= 1 << j;
53
54         int iv = IV_BASE_END + i * 8 + j;
55         handlers[iv] = handler ? handler : intr_routine_fallback;
56
57         return iv;
58     }
59
60     return 0;
61 }
62
63 int
64 isrm_ivosalloc(isr_cb handler)
65 {
66     return __ivalloc_within(IV_BASE_END, IV_EX_BEGIN, handler);
67 }
68
69 int
70 isrm_ivexalloc(isr_cb handler)
71 {
72     return __ivalloc_within(IV_EX_BEGIN, IV_EX_END, handler);
73 }
74
75 void
76 isrm_ivfree(int iv)
77 {
78     assert(iv < 256);
79
80     if (iv >= IV_BASE_END) {
81         iv_bmp[(iv - IV_BASE_END) / 8] &= ~(1 << ((iv - IV_BASE_END) % 8));
82     }
83
84     handlers[iv] = intr_routine_fallback;
85 }
86
87 int
88 isrm_bindirq(int irq, isr_cb irq_handler)
89 {
90     int iv;
91     if (!(iv = isrm_ivexalloc(irq_handler))) {
92         panickf("out of IV resource. (irq=%d)", irq);
93         return 0; // never reach
94     }
95
96     // fixed, edge trigged, polarity=high
97     intc_irq_attach(irq, iv, 0, IRQ_DEFAULT);
98
99     return iv;
100 }
101
102 void
103 isrm_bindiv(int iv, isr_cb handler)
104 {
105     assert(iv < 256);
106
107     if (iv >= IV_BASE_END) {
108         iv_bmp[(iv - IV_BASE_END) / 8] |= 1 << ((iv - IV_BASE_END) % 8);
109     }
110
111     handlers[iv] = handler;
112 }
113
114 isr_cb
115 isrm_get(int iv)
116 {
117     assert(iv < 256);
118
119     return handlers[iv];
120 }