blob: 8c18097d0f4903a61c11e2d9f8975d47210cac90 [file] [log] [blame]
swissChili9b3584b2021-02-18 13:57:27 -08001#include "pic.h"
2#include "io.h"
swissChili825d46b2021-02-21 10:14:16 -08003#include "log.h"
swissChili9b3584b2021-02-18 13:57:27 -08004
swissChili825d46b2021-02-21 10:14:16 -08005#define ICW1_ICW4 0x01 /* ICW4 (not) needed */
6#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
7#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
8#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
9#define ICW1_INIT 0x10 /* Initialization - required! */
swissChilidefeb0d2021-02-18 15:28:36 -080010
swissChili825d46b2021-02-21 10:14:16 -080011#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
12#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
13#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
14#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
15#define ICW4_SFNM 0x10 /* Special fully nested (not) */
swissChilidefeb0d2021-02-18 15:28:36 -080016
swissChiliaed6ff32021-05-29 17:51:04 -070017typedef void (*interrupt_handler_t)(struct registers *);
18
19static interrupt_handler_t interrupt_handlers[256];
swissChili9b3584b2021-02-18 13:57:27 -080020
21void pic_send_eoi(uchar interrupt)
22{
swissChilidefeb0d2021-02-18 15:28:36 -080023 if (interrupt >= IRQ_TO_INT(8))
swissChili9b3584b2021-02-18 13:57:27 -080024 outb(PIC2_COMMAND, PIC_EOI);
swissChilidefeb0d2021-02-18 15:28:36 -080025
swissChili9b3584b2021-02-18 13:57:27 -080026 outb(PIC1_COMMAND, PIC_EOI);
27}
28
29void irq_handler(struct registers regs)
30{
31 pic_send_eoi(regs.interrupt_number);
32
33 if (interrupt_handlers[regs.interrupt_number])
swissChili825d46b2021-02-21 10:14:16 -080034 interrupt_handlers[regs.interrupt_number](&regs);
35 else
swissChilib58ab672022-01-17 21:18:01 -080036 kprintf(ERROR "Unhandled hardware interrupt: %d, called from %d\n",
37 regs.interrupt_number, regs.eip);
swissChili9b3584b2021-02-18 13:57:27 -080038}
39
swissChili825d46b2021-02-21 10:14:16 -080040void isr_handler(struct registers regs)
41{
42 if (interrupt_handlers[regs.interrupt_number])
43 interrupt_handlers[regs.interrupt_number](&regs);
44 else
swissChilib58ab672022-01-17 21:18:01 -080045 kprintf(ERROR "Unhandled interrupt: %d, called from %d\n",
46 regs.interrupt_number, regs.eip);
swissChili825d46b2021-02-21 10:14:16 -080047}
48
49void add_interrupt_handler(uchar interrupt, void (*handler)(struct registers *))
swissChili9b3584b2021-02-18 13:57:27 -080050{
51 interrupt_handlers[interrupt] = handler;
52}
swissChilidefeb0d2021-02-18 15:28:36 -080053
54void pic_remap()
55{
swissChilie5adca52021-06-16 21:00:31 -070056 // I don't remember what this does.
swissChili825d46b2021-02-21 10:14:16 -080057 outb(0x20, 0x11);
58 outb(0xA0, 0x11);
59 outb(0x21, 0x20);
60 outb(0xA1, 0x28);
61 outb(0x21, 0x04);
62 outb(0xA1, 0x02);
63 outb(0x21, 0x01);
64 outb(0xA1, 0x01);
65 outb(0x21, 0x0);
66 outb(0xA1, 0x0);
swissChilidefeb0d2021-02-18 15:28:36 -080067
swissChiliaed6ff32021-05-29 17:51:04 -070068 memset(interrupt_handlers, 0, sizeof(interrupt_handlers));
swissChilidefeb0d2021-02-18 15:28:36 -080069}