wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Daniel Engström, Omicron Ceti AB, daniel@omicron.se. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 25 | #include <malloc.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/i8259.h> |
| 28 | #include <asm/ibmpc.h> |
Graeme Russ | 0c8c62e | 2008-12-07 10:29:01 +1100 | [diff] [blame] | 29 | #include <asm/interrupt.h> |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 30 | |
| 31 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 32 | struct idt_entry { |
| 33 | u16 base_low; |
| 34 | u16 selector; |
| 35 | u8 res; |
| 36 | u8 access; |
| 37 | u16 base_high; |
| 38 | } __attribute__ ((packed)); |
| 39 | |
| 40 | |
| 41 | struct idt_entry idt[256]; |
| 42 | |
| 43 | |
| 44 | #define MAX_IRQ 16 |
| 45 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 46 | typedef struct irq_handler { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 47 | struct irq_handler *next; |
| 48 | interrupt_handler_t* isr_func; |
| 49 | void *isr_data; |
| 50 | } irq_handler_t; |
| 51 | |
| 52 | #define IRQ_DISABLED 1 |
| 53 | |
| 54 | typedef struct { |
| 55 | irq_handler_t *handler; |
| 56 | unsigned long status; |
| 57 | } irq_desc_t; |
| 58 | |
| 59 | static irq_desc_t irq_table[MAX_IRQ]; |
| 60 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 61 | asm ("irq_return:\n" |
| 62 | " addl $4, %esp\n" |
| 63 | " popa\n" |
| 64 | " iret\n"); |
| 65 | |
| 66 | asm ("exp_return:\n" |
| 67 | " addl $12, %esp\n" |
| 68 | " pop %esp\n" |
| 69 | " popa\n" |
| 70 | " iret\n"); |
| 71 | |
| 72 | char exception_stack[4096]; |
| 73 | |
| 74 | #define DECLARE_INTERRUPT(x) \ |
| 75 | asm(".globl irq_"#x"\n" \ |
| 76 | "irq_"#x":\n" \ |
| 77 | "pusha \n" \ |
| 78 | "pushl $"#x"\n" \ |
| 79 | "pushl $irq_return\n" \ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 80 | "jmp do_irq\n"); \ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 81 | void __attribute__ ((regparm(0))) irq_##x(void) |
| 82 | |
| 83 | #define DECLARE_EXCEPTION(x, f) \ |
| 84 | asm(".globl exp_"#x"\n" \ |
| 85 | "exp_"#x":\n" \ |
| 86 | "pusha \n" \ |
| 87 | "movl %esp, %ebx\n" \ |
| 88 | "movl $exception_stack, %eax\n" \ |
| 89 | "movl %eax, %esp \n" \ |
| 90 | "pushl %ebx\n" \ |
| 91 | "movl 32(%esp), %ebx\n" \ |
| 92 | "xorl %edx, %edx\n" \ |
| 93 | "movw 36(%esp), %dx\n" \ |
| 94 | "pushl %edx\n" \ |
| 95 | "pushl %ebx\n" \ |
| 96 | "pushl $"#x"\n" \ |
| 97 | "pushl $exp_return\n" \ |
| 98 | "jmp "#f"\n"); \ |
| 99 | void __attribute__ ((regparm(0))) exp_##x(void) |
| 100 | |
| 101 | DECLARE_EXCEPTION(0, divide_exception_entry); /* Divide exception */ |
| 102 | DECLARE_EXCEPTION(1, debug_exception_entry); /* Debug exception */ |
| 103 | DECLARE_EXCEPTION(2, nmi_entry); /* NMI */ |
| 104 | DECLARE_EXCEPTION(3, unknown_exception_entry); /* Breakpoint/Coprocessor Error */ |
| 105 | DECLARE_EXCEPTION(4, unknown_exception_entry); /* Overflow */ |
| 106 | DECLARE_EXCEPTION(5, unknown_exception_entry); /* Bounds */ |
| 107 | DECLARE_EXCEPTION(6, invalid_instruction_entry); /* Invalid instruction */ |
| 108 | DECLARE_EXCEPTION(7, unknown_exception_entry); /* Device not present */ |
| 109 | DECLARE_EXCEPTION(8, double_fault_entry); /* Double fault */ |
| 110 | DECLARE_EXCEPTION(9, unknown_exception_entry); /* Co-processor segment overrun */ |
| 111 | DECLARE_EXCEPTION(10, invalid_tss_exception_entry);/* Invalid TSS */ |
| 112 | DECLARE_EXCEPTION(11, seg_fault_entry); /* Segment not present */ |
| 113 | DECLARE_EXCEPTION(12, stack_fault_entry); /* Stack overflow */ |
| 114 | DECLARE_EXCEPTION(13, gpf_entry); /* GPF */ |
| 115 | DECLARE_EXCEPTION(14, page_fault_entry); /* PF */ |
| 116 | DECLARE_EXCEPTION(15, unknown_exception_entry); /* Reserved */ |
| 117 | DECLARE_EXCEPTION(16, fp_exception_entry); /* Floating point */ |
| 118 | DECLARE_EXCEPTION(17, alignment_check_entry); /* alignment check */ |
| 119 | DECLARE_EXCEPTION(18, machine_check_entry); /* machine check */ |
| 120 | DECLARE_EXCEPTION(19, unknown_exception_entry); /* Reserved */ |
| 121 | DECLARE_EXCEPTION(20, unknown_exception_entry); /* Reserved */ |
| 122 | DECLARE_EXCEPTION(21, unknown_exception_entry); /* Reserved */ |
| 123 | DECLARE_EXCEPTION(22, unknown_exception_entry); /* Reserved */ |
| 124 | DECLARE_EXCEPTION(23, unknown_exception_entry); /* Reserved */ |
| 125 | DECLARE_EXCEPTION(24, unknown_exception_entry); /* Reserved */ |
| 126 | DECLARE_EXCEPTION(25, unknown_exception_entry); /* Reserved */ |
| 127 | DECLARE_EXCEPTION(26, unknown_exception_entry); /* Reserved */ |
| 128 | DECLARE_EXCEPTION(27, unknown_exception_entry); /* Reserved */ |
| 129 | DECLARE_EXCEPTION(28, unknown_exception_entry); /* Reserved */ |
| 130 | DECLARE_EXCEPTION(29, unknown_exception_entry); /* Reserved */ |
| 131 | DECLARE_EXCEPTION(30, unknown_exception_entry); /* Reserved */ |
| 132 | DECLARE_EXCEPTION(31, unknown_exception_entry); /* Reserved */ |
| 133 | |
| 134 | DECLARE_INTERRUPT(0); |
| 135 | DECLARE_INTERRUPT(1); |
| 136 | DECLARE_INTERRUPT(3); |
| 137 | DECLARE_INTERRUPT(4); |
| 138 | DECLARE_INTERRUPT(5); |
| 139 | DECLARE_INTERRUPT(6); |
| 140 | DECLARE_INTERRUPT(7); |
| 141 | DECLARE_INTERRUPT(8); |
| 142 | DECLARE_INTERRUPT(9); |
| 143 | DECLARE_INTERRUPT(10); |
| 144 | DECLARE_INTERRUPT(11); |
| 145 | DECLARE_INTERRUPT(12); |
| 146 | DECLARE_INTERRUPT(13); |
| 147 | DECLARE_INTERRUPT(14); |
| 148 | DECLARE_INTERRUPT(15); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 149 | |
| 150 | void __attribute__ ((regparm(0))) default_isr(void); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 151 | asm ("default_isr: iret\n"); |
| 152 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 153 | void disable_irq(int irq) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 154 | { |
| 155 | if (irq >= MAX_IRQ) { |
| 156 | return; |
| 157 | } |
| 158 | irq_table[irq].status |= IRQ_DISABLED; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 160 | } |
| 161 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 162 | void enable_irq(int irq) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 163 | { |
| 164 | if (irq >= MAX_IRQ) { |
| 165 | return; |
| 166 | } |
| 167 | irq_table[irq].status &= ~IRQ_DISABLED; |
| 168 | } |
| 169 | |
| 170 | /* masks one specific IRQ in the PIC */ |
| 171 | static void unmask_irq(int irq) |
| 172 | { |
| 173 | int imr_port; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 175 | if (irq >= MAX_IRQ) { |
| 176 | return; |
| 177 | } |
| 178 | if (irq > 7) { |
| 179 | imr_port = SLAVE_PIC + IMR; |
| 180 | } else { |
| 181 | imr_port = MASTER_PIC + IMR; |
| 182 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 183 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 184 | outb(inb(imr_port)&~(1<<(irq&7)), imr_port); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /* unmasks one specific IRQ in the PIC */ |
| 189 | static void mask_irq(int irq) |
| 190 | { |
| 191 | int imr_port; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 192 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 193 | if (irq >= MAX_IRQ) { |
| 194 | return; |
| 195 | } |
| 196 | if (irq > 7) { |
| 197 | imr_port = SLAVE_PIC + IMR; |
| 198 | } else { |
| 199 | imr_port = MASTER_PIC + IMR; |
| 200 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | |
| 202 | outb(inb(imr_port)|(1<<(irq&7)), imr_port); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | /* issue a Specific End Of Interrupt instruciton */ |
| 207 | static void specific_eoi(int irq) |
| 208 | { |
| 209 | /* If it is on the slave PIC this have to be performed on |
| 210 | * both the master and the slave PICs */ |
| 211 | if (irq > 7) { |
| 212 | outb(OCW2_SEOI|(irq&7), SLAVE_PIC + OCW2); |
| 213 | irq = SEOI_IR2; /* also do IR2 on master */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 214 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 215 | outb(OCW2_SEOI|irq, MASTER_PIC + OCW2); |
| 216 | } |
| 217 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 218 | void __attribute__ ((regparm(0))) do_irq(int irq) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 219 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 220 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 221 | mask_irq(irq); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 222 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 223 | if (irq_table[irq].status & IRQ_DISABLED) { |
| 224 | unmask_irq(irq); |
| 225 | specific_eoi(irq); |
| 226 | return; |
| 227 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 228 | |
| 229 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 230 | if (NULL != irq_table[irq].handler) { |
| 231 | irq_handler_t *handler; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 232 | for (handler = irq_table[irq].handler; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 233 | NULL!= handler; handler = handler->next) { |
| 234 | handler->isr_func(handler->isr_data); |
| 235 | } |
| 236 | } else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 237 | if ((irq & 7) != 7) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 238 | printf("Spurious irq %d\n", irq); |
| 239 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 240 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 241 | unmask_irq(irq); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 242 | specific_eoi(irq); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 246 | void __attribute__ ((regparm(0))) unknown_exception_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 247 | { |
| 248 | printf("Unknown Exception %d at %04x:%08x\n", cause, seg, ip); |
| 249 | } |
| 250 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 251 | void __attribute__ ((regparm(0))) divide_exception_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 252 | { |
| 253 | printf("Divide Error (Division by zero) at %04x:%08x\n", seg, ip); |
| 254 | while(1); |
| 255 | } |
| 256 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 257 | void __attribute__ ((regparm(0))) debug_exception_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 258 | { |
| 259 | printf("Debug Interrupt (Single step) at %04x:%08x\n", seg, ip); |
| 260 | } |
| 261 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 262 | void __attribute__ ((regparm(0))) nmi_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 263 | { |
| 264 | printf("NMI Interrupt at %04x:%08x\n", seg, ip); |
| 265 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 266 | |
| 267 | void __attribute__ ((regparm(0))) invalid_instruction_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 268 | { |
| 269 | printf("Invalid Instruction at %04x:%08x\n", seg, ip); |
| 270 | while(1); |
| 271 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 272 | |
| 273 | void __attribute__ ((regparm(0))) double_fault_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 274 | { |
| 275 | printf("Double fault at %04x:%08x\n", seg, ip); |
| 276 | while(1); |
| 277 | } |
| 278 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 279 | void __attribute__ ((regparm(0))) invalid_tss_exception_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 280 | { |
| 281 | printf("Invalid TSS at %04x:%08x\n", seg, ip); |
| 282 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 283 | |
| 284 | void __attribute__ ((regparm(0))) seg_fault_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 285 | { |
| 286 | printf("Segmentation fault at %04x:%08x\n", seg, ip); |
| 287 | while(1); |
| 288 | } |
| 289 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 290 | void __attribute__ ((regparm(0))) stack_fault_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 291 | { |
| 292 | printf("Stack fault at %04x:%08x\n", seg, ip); |
| 293 | while(1); |
| 294 | } |
| 295 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 296 | void __attribute__ ((regparm(0))) gpf_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 297 | { |
| 298 | printf("General protection fault at %04x:%08x\n", seg, ip); |
| 299 | } |
| 300 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 301 | void __attribute__ ((regparm(0))) page_fault_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 302 | { |
| 303 | printf("Page fault at %04x:%08x\n", seg, ip); |
| 304 | while(1); |
| 305 | } |
| 306 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 307 | void __attribute__ ((regparm(0))) fp_exception_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 308 | { |
| 309 | printf("Floating point exception at %04x:%08x\n", seg, ip); |
| 310 | } |
| 311 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 312 | void __attribute__ ((regparm(0))) alignment_check_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 313 | { |
| 314 | printf("Alignment check at %04x:%08x\n", seg, ip); |
| 315 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 316 | |
| 317 | void __attribute__ ((regparm(0))) machine_check_entry(int cause, int ip, int seg) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 318 | { |
| 319 | printf("Machine check exception at %04x:%08x\n", seg, ip); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void irq_install_handler(int ino, interrupt_handler_t *func, void *pdata) |
| 324 | { |
| 325 | int status; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 326 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 327 | if (ino>MAX_IRQ) { |
| 328 | return; |
| 329 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 330 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 331 | if (NULL != irq_table[ino].handler) { |
| 332 | return; |
| 333 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 334 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 335 | status = disable_interrupts(); |
| 336 | irq_table[ino].handler = malloc(sizeof(irq_handler_t)); |
| 337 | if (NULL == irq_table[ino].handler) { |
| 338 | return; |
| 339 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 340 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 341 | memset(irq_table[ino].handler, 0, sizeof(irq_handler_t)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 342 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 343 | irq_table[ino].handler->isr_func = func; |
| 344 | irq_table[ino].handler->isr_data = pdata; |
| 345 | if (status) { |
| 346 | enable_interrupts(); |
| 347 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 348 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 349 | unmask_irq(ino); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 350 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | void irq_free_handler(int ino) |
| 355 | { |
| 356 | int status; |
| 357 | if (ino>MAX_IRQ) { |
| 358 | return; |
| 359 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 360 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 361 | status = disable_interrupts(); |
| 362 | mask_irq(ino); |
| 363 | if (NULL == irq_table[ino].handler) { |
| 364 | return; |
| 365 | } |
| 366 | free(irq_table[ino].handler); |
| 367 | irq_table[ino].handler=NULL; |
| 368 | if (status) { |
| 369 | enable_interrupts(); |
| 370 | } |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | asm ("idt_ptr:\n" |
| 376 | ".word 0x800\n" /* size of the table 8*256 bytes */ |
| 377 | ".long idt\n" /* offset */ |
| 378 | ".word 0x18\n");/* data segment */ |
| 379 | |
Graeme Russ | 0c8c62e | 2008-12-07 10:29:01 +1100 | [diff] [blame] | 380 | void set_vector(int intnum, void *routine) |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 381 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 382 | idt[intnum].base_high = (u16)((u32)(routine)>>16); |
| 383 | idt[intnum].base_low = (u16)((u32)(routine)&0xffff); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
| 387 | int interrupt_init(void) |
| 388 | { |
| 389 | int i; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 390 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 391 | /* Just in case... */ |
| 392 | disable_interrupts(); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 393 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 394 | /* Initialize the IDT and stuff */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 395 | |
| 396 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 397 | memset(irq_table, 0, sizeof(irq_table)); |
| 398 | |
| 399 | /* Setup the IDT */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 400 | for (i=0;i<256;i++) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 401 | idt[i].access = 0x8e; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 402 | idt[i].res = 0; |
| 403 | idt[i].selector = 0x10; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 404 | set_vector(i, default_isr); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 405 | } |
| 406 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 407 | asm ("cs lidt idt_ptr\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 408 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 409 | /* Setup exceptions */ |
| 410 | set_vector(0x00, exp_0); |
| 411 | set_vector(0x01, exp_1); |
| 412 | set_vector(0x02, exp_2); |
| 413 | set_vector(0x03, exp_3); |
| 414 | set_vector(0x04, exp_4); |
| 415 | set_vector(0x05, exp_5); |
| 416 | set_vector(0x06, exp_6); |
| 417 | set_vector(0x07, exp_7); |
| 418 | set_vector(0x08, exp_8); |
| 419 | set_vector(0x09, exp_9); |
| 420 | set_vector(0x0a, exp_10); |
| 421 | set_vector(0x0b, exp_11); |
| 422 | set_vector(0x0c, exp_12); |
| 423 | set_vector(0x0d, exp_13); |
| 424 | set_vector(0x0e, exp_14); |
| 425 | set_vector(0x0f, exp_15); |
| 426 | set_vector(0x10, exp_16); |
| 427 | set_vector(0x11, exp_17); |
| 428 | set_vector(0x12, exp_18); |
| 429 | set_vector(0x13, exp_19); |
| 430 | set_vector(0x14, exp_20); |
| 431 | set_vector(0x15, exp_21); |
| 432 | set_vector(0x16, exp_22); |
| 433 | set_vector(0x17, exp_23); |
| 434 | set_vector(0x18, exp_24); |
| 435 | set_vector(0x19, exp_25); |
| 436 | set_vector(0x1a, exp_26); |
| 437 | set_vector(0x1b, exp_27); |
| 438 | set_vector(0x1c, exp_28); |
| 439 | set_vector(0x1d, exp_29); |
| 440 | set_vector(0x1e, exp_30); |
| 441 | set_vector(0x1f, exp_31); |
| 442 | |
| 443 | |
| 444 | /* Setup interrupts */ |
| 445 | set_vector(0x20, irq_0); |
| 446 | set_vector(0x21, irq_1); |
| 447 | set_vector(0x23, irq_3); |
| 448 | set_vector(0x24, irq_4); |
| 449 | set_vector(0x25, irq_5); |
| 450 | set_vector(0x26, irq_6); |
| 451 | set_vector(0x27, irq_7); |
| 452 | set_vector(0x28, irq_8); |
| 453 | set_vector(0x29, irq_9); |
| 454 | set_vector(0x2a, irq_10); |
| 455 | set_vector(0x2b, irq_11); |
| 456 | set_vector(0x2c, irq_12); |
| 457 | set_vector(0x2d, irq_13); |
| 458 | set_vector(0x2e, irq_14); |
| 459 | set_vector(0x2f, irq_15); |
| 460 | /* vectors 0x30-0x3f are reserved for irq 16-31 */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 461 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 462 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 463 | /* Mask all interrupts */ |
| 464 | outb(0xff, MASTER_PIC + IMR); |
| 465 | outb(0xff, SLAVE_PIC + IMR); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 466 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 467 | /* Master PIC */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 468 | outb(ICW1_SEL|ICW1_EICW4, MASTER_PIC + ICW1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 469 | outb(0x20, MASTER_PIC + ICW2); /* Place master PIC interrupts at INT20 */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 470 | outb(IR2, MASTER_PIC + ICW3); /* ICW3, One slevc PIC is present */ |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 471 | outb(ICW4_PM, MASTER_PIC + ICW4); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 472 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 473 | for (i=0;i<8;i++) { |
| 474 | outb(OCW2_SEOI|i, MASTER_PIC + OCW2); |
| 475 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 476 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 477 | /* Slave PIC */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 478 | outb(ICW1_SEL|ICW1_EICW4, SLAVE_PIC + ICW1); |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 479 | outb(0x28, SLAVE_PIC + ICW2); /* Place slave PIC interrupts at INT28 */ |
| 480 | outb(0x02, SLAVE_PIC + ICW3); /* Slave ID */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 481 | outb(ICW4_PM, SLAVE_PIC + ICW4); |
| 482 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 483 | for (i=0;i<8;i++) { |
| 484 | outb(OCW2_SEOI|i, SLAVE_PIC + OCW2); |
| 485 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 486 | |
| 487 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 488 | /* enable cascade interrerupt */ |
| 489 | outb(0xfb, MASTER_PIC + IMR); |
| 490 | outb(0xff, SLAVE_PIC + IMR); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 491 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 492 | /* It is now safe to enable interrupts */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 493 | enable_interrupts(); |
| 494 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | void enable_interrupts(void) |
| 499 | { |
| 500 | asm("sti\n"); |
| 501 | } |
| 502 | |
| 503 | int disable_interrupts(void) |
| 504 | { |
| 505 | long flags; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 506 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 507 | asm volatile ("pushfl ; popl %0 ; cli\n" : "=g" (flags) : ); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 508 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 509 | return (flags&0x200); /* IE flags is bit 9 */ |
| 510 | } |