1 #include <hal/acpi/acpi.h>
2 #include <hal/ioapic.h>
4 #include <lunaix/isrm.h>
5 #include <lunaix/spike.h>
9 0~31: reserved for sys use (x32)
10 32~47: reserved for os use (x16)
11 48~ : free to allocate for external hardware use. (x208)
14 static char iv_bmp[(IV_MAX - IV_BASE) / 8];
15 static isr_cb handlers[IV_MAX];
18 intr_routine_fallback(const isr_param* param);
23 for (size_t i = 0; i < 256; i++) {
24 handlers[i] = intr_routine_fallback;
28 static inline uint32_t
29 __ivalloc_within(size_t a, size_t b, isr_cb handler)
31 a = (a - IV_BASE) / 8;
32 b = (b - IV_BASE) / 8;
33 for (size_t i = a; i < b; i++) {
34 char chunk = iv_bmp[i], j = 0;
37 while ((chunk & 0x1)) {
42 uint32_t iv = IV_BASE + i * 8 + j;
43 handlers[iv] = handler ? handler : intr_routine_fallback;
50 isrm_ivosalloc(isr_cb handler)
52 return __ivalloc_within(IV_BASE, IV_EX, handler);
56 isrm_ivexalloc(isr_cb handler)
58 return __ivalloc_within(IV_EX, IV_MAX, handler);
62 isrm_ivfree(uint32_t iv)
66 iv_bmp[(iv - IV_BASE) / 8] &= ~(1 << ((iv - IV_BASE) % 8));
68 handlers[iv] = intr_routine_fallback;
72 isrm_bindirq(uint32_t irq, isr_cb irq_handler)
75 if (!(iv = isrm_ivexalloc(irq_handler))) {
76 panickf("out of IV resource. (irq=%d)", irq);
77 return 0; // never reach
80 // PC_AT_IRQ_RTC -> RTC_TIMER_IV, fixed, edge trigged, polarity=high,
81 // physical, APIC ID 0
82 ioapic_redirect(acpi_gistranslate(irq), iv, 0, IOAPIC_DELMOD_FIXED);
87 isrm_bindiv(uint32_t iv, isr_cb handler)
91 iv_bmp[(iv - IV_BASE) / 8] |= 1 << ((iv - IV_BASE) % 8);
93 handlers[iv] = handler;