toaruos/kernel/cpu/irq.c

160 lines
3.5 KiB
C
Raw Normal View History

2014-06-08 10:51:01 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2011-2014 Kevin Lange
*
* Interrupt Requests
*
*/
2011-01-16 07:17:42 +03:00
#include <system.h>
2011-12-27 05:23:58 +04:00
#include <logging.h>
2011-01-16 07:17:42 +03:00
2013-06-06 10:10:36 +04:00
extern void _irq0(void);
extern void _irq1(void);
extern void _irq2(void);
extern void _irq3(void);
extern void _irq4(void);
extern void _irq5(void);
extern void _irq6(void);
extern void _irq7(void);
extern void _irq8(void);
extern void _irq9(void);
extern void _irq10(void);
extern void _irq11(void);
extern void _irq12(void);
extern void _irq13(void);
extern void _irq14(void);
extern void _irq15(void);
2011-01-16 07:17:42 +03:00
static irq_handler_chain_t irq_routines[16] = { NULL };
static irq_handler_chain_t irq_routines_a[16] = { NULL };
static irq_handler_chain_t irq_routines_b[16] = { NULL };
static irq_handler_chain_t irq_routines_c[16] = { NULL };
2011-01-16 07:17:42 +03:00
2011-03-05 06:05:03 +03:00
/*
* Install an interupt handler for a hardware device.
*/
void irq_install_handler(size_t irq, irq_handler_chain_t handler) {
if (irq_routines[irq]) {
if (irq_routines_a[irq]) {
if (irq_routines_b[irq]) {
irq_routines_c[irq] = handler;
return;
}
irq_routines_b[irq] = handler;
return;
}
irq_routines_a[irq] = handler;
return;
}
2011-01-16 07:17:42 +03:00
irq_routines[irq] = handler;
}
2011-03-05 06:05:03 +03:00
/*
* Remove an interrupt handler for a hardware device.
*/
2013-06-06 10:10:36 +04:00
void irq_uninstall_handler(size_t irq) {
irq_routines[irq] = NULL;
irq_routines_a[irq] = NULL;
irq_routines_b[irq] = NULL;
irq_routines_c[irq] = NULL;
}
2011-03-05 06:05:03 +03:00
/*
* Remap interrupt handlers
*/
2013-06-06 10:10:36 +04:00
void irq_remap(void) {
2011-01-16 07:17:42 +03:00
outportb(0x20, 0x11);
outportb(0xA0, 0x11);
outportb(0x21, 0x20);
outportb(0xA1, 0x28);
outportb(0x21, 0x04);
outportb(0xA1, 0x02);
outportb(0x21, 0x01);
outportb(0xA1, 0x01);
outportb(0x21, 0x0);
outportb(0xA1, 0x0);
}
2013-06-06 10:10:36 +04:00
void irq_gates(void) {
idt_set_gate(32, _irq0, 0x08, 0x8E);
idt_set_gate(33, _irq1, 0x08, 0x8E);
idt_set_gate(34, _irq2, 0x08, 0x8E);
idt_set_gate(35, _irq3, 0x08, 0x8E);
idt_set_gate(36, _irq4, 0x08, 0x8E);
idt_set_gate(37, _irq5, 0x08, 0x8E);
idt_set_gate(38, _irq6, 0x08, 0x8E);
idt_set_gate(39, _irq7, 0x08, 0x8E);
idt_set_gate(40, _irq8, 0x08, 0x8E);
idt_set_gate(41, _irq9, 0x08, 0x8E);
idt_set_gate(42, _irq10, 0x08, 0x8E);
idt_set_gate(43, _irq11, 0x08, 0x8E);
idt_set_gate(44, _irq12, 0x08, 0x8E);
idt_set_gate(45, _irq13, 0x08, 0x8E);
idt_set_gate(46, _irq14, 0x08, 0x8E);
idt_set_gate(47, _irq15, 0x08, 0x8E);
2011-05-07 10:55:49 +04:00
}
/*
* Set up interrupt handler for hardware devices.
*/
2013-06-06 10:10:36 +04:00
void irq_install(void) {
2011-05-07 10:55:49 +04:00
irq_remap();
irq_gates();
IRQ_RES;
2011-01-16 07:17:42 +03:00
}
2013-06-06 10:10:36 +04:00
void irq_ack(size_t irq_no) {
2014-05-07 10:27:58 +04:00
if (irq_no >= 8) {
outportb(0xA0, 0x20);
}
outportb(0x20, 0x20);
}
2013-06-06 10:10:36 +04:00
void irq_handler(struct regs *r) {
IRQ_OFF;
2012-02-17 00:31:40 +04:00
if (r->int_no > 47 || r->int_no < 32) {
IRQ_RES;
return;
}
if (irq_routines [r->int_no - 32] && irq_routines [r->int_no - 32](r)) goto _done;
if (irq_routines_a[r->int_no - 32] && irq_routines_a[r->int_no - 32](r)) goto _done;
if (irq_routines_b[r->int_no - 32] && irq_routines_b[r->int_no - 32](r)) goto _done;
if (irq_routines_c[r->int_no - 32] && irq_routines_c[r->int_no - 32](r)) goto _done;
irq_ack(r->int_no - 32);
_done:
IRQ_RES;
2011-01-16 07:17:42 +03:00
}
static int _irq_sem = 0;
void irq_off(void) {
uint32_t eflags;
asm volatile(
"pushf\n"
"pop %0\n"
"cli"
: "=r" (eflags));
if (eflags & (1 << 9)) {
_irq_sem = 1;
} else {
_irq_sem++;
}
}
void irq_res(void) {
if (_irq_sem == 0) {
asm volatile ("sti");
return;
}
_irq_sem--;
if (_irq_sem == 0) {
asm volatile ("sti");
}
}
void irq_on(void) {
_irq_sem = 0;
asm volatile ("sti");
}