1 #include <lunaix/spike.h>
2 #include <lunaix/owloysius.h>
3 #include <asm/x86_isrm.h>
6 #include "asm/soc/ioapic.h"
7 #include "asm/soc/apic.h"
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)
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];
20 static struct x86_intc arch_intc_ctx;
23 intr_routine_fallback(const struct hart_state* state);
28 for (size_t i = 0; i < TOTAL_IV; i++) {
29 handlers[i] = intr_routine_fallback;
34 __ivalloc_within(size_t a, size_t b, isr_cb handler)
36 a = (a - IV_BASE_END);
37 b = (b - IV_BASE_END);
41 for (size_t i = a / 8; i < b / 8; i++, k += 8) {
42 u8_t chunk = iv_bmp[i];
48 while ((chunk & 0x1) && k <= b) {
65 int iv = IV_BASE_END + i * 8 + j;
66 handlers[iv] = handler ? handler : intr_routine_fallback;
75 isrm_ivosalloc(isr_cb handler)
77 return __ivalloc_within(IV_BASE_END, IV_EX_BEGIN, handler);
81 isrm_ivexalloc(isr_cb handler)
83 return __ivalloc_within(IV_EX_BEGIN, IV_EX_END, handler);
91 if (iv >= IV_BASE_END) {
92 iv_bmp[(iv - IV_BASE_END) / 8] &= ~(1 << ((iv - IV_BASE_END) % 8));
95 handlers[iv] = intr_routine_fallback;
99 isrm_bindirq(int irq, isr_cb irq_handler)
102 if (!(iv = isrm_ivexalloc(irq_handler))) {
103 fail("out of IV resource.");
104 return 0; // never reach
107 // fixed, edge trigged, polarity=high
108 isrm_irq_attach(irq, iv, 0, IRQ_DEFAULT);
114 isrm_bindiv(int iv, isr_cb handler)
118 if (iv >= IV_BASE_END) {
119 iv_bmp[(iv - IV_BASE_END) / 8] |= 1 << ((iv - IV_BASE_END) % 8);
122 handlers[iv] = handler;
134 isrm_get_payload(const struct hart_state* state)
136 int iv = state->execp->vector;
139 return ivhand_payload[iv];
143 isrm_set_payload(int iv, ptr_t payload)
147 ivhand_payload[iv] = payload;
151 isrm_irq_attach(int irq, int iv, cpu_t dest, u32_t flags)
153 arch_intc_ctx.irq_attach(&arch_intc_ctx, irq, iv, dest, flags);
157 isrm_notify_eoi(cpu_t id, int iv)
159 arch_intc_ctx.notify_eoi(&arch_intc_ctx, id, iv);
163 isrm_notify_eos(cpu_t id)
165 isrm_notify_eoi(id, LUNAIX_SCHED);
169 isrm_msialloc(isr_cb handler)
171 unsigned int iv = isrm_ivexalloc(handler);
173 return (msi_vector_t){
174 .msi_addr = 0xfee00000,
181 isrm_bind_dtnode(struct dt_intr_node* node)
183 fail("not supported");
193 arch_intc_ctx.name = "i386_apic";
194 arch_intc_ctx.irq_attach = ioapic_irq_remap;
195 arch_intc_ctx.notify_eoi = apic_on_eoi;
197 owloysius_fetch_init(__intc_init, on_earlyboot);