blob: d80cfb1925f123d2173048a88acdc852c0205c34 [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
Graeme Russd11b0852009-11-24 20:04:18 +11002 * (C) Copyright 2008
3 * Graeme Russ, graeme.russ@gmail.com.
4 *
wdenk591dda52002-11-18 00:14:45 +00005 * (C) Copyright 2002
6 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
Graeme Russ0c8c62e2008-12-07 10:29:01 +110028#include <asm/interrupt.h>
wdenk591dda52002-11-18 00:14:45 +000029
Graeme Russd11b0852009-11-24 20:04:18 +110030#define DECLARE_INTERRUPT(x) \
31 ".globl irq_"#x"\n" \
32 "irq_"#x":\n" \
33 "pushl %ebp\n" \
34 "movl %esp,%ebp\n" \
35 "pusha\n" \
36 "pushl $"#x"\n" \
37 "jmp irq_common_entry\n"
wdenk591dda52002-11-18 00:14:45 +000038
wdenk591dda52002-11-18 00:14:45 +000039struct idt_entry {
40 u16 base_low;
41 u16 selector;
42 u8 res;
43 u8 access;
44 u16 base_high;
45} __attribute__ ((packed));
46
Graeme Russd11b0852009-11-24 20:04:18 +110047struct desc_ptr {
48 unsigned short size;
49 unsigned long address;
50 unsigned short segment;
51} __attribute__((packed));
wdenk591dda52002-11-18 00:14:45 +000052
53struct idt_entry idt[256];
54
Graeme Russd11b0852009-11-24 20:04:18 +110055struct desc_ptr idt_ptr;
wdenk591dda52002-11-18 00:14:45 +000056
Graeme Russd11b0852009-11-24 20:04:18 +110057static inline void load_idt(const struct desc_ptr *dtr)
58{
59 asm volatile("cs lidt %0"::"m" (*dtr));
60}
wdenk591dda52002-11-18 00:14:45 +000061
Graeme Russ77290ee2009-02-24 21:13:40 +110062void set_vector(u8 intnum, void *routine)
wdenk591dda52002-11-18 00:14:45 +000063{
Graeme Russ84959642009-02-24 21:14:56 +110064 idt[intnum].base_high = (u16)((u32)(routine + gd->reloc_off) >> 16);
65 idt[intnum].base_low = (u16)((u32)(routine + gd->reloc_off) & 0xffff);
wdenk591dda52002-11-18 00:14:45 +000066}
67
Graeme Russd11b0852009-11-24 20:04:18 +110068void irq_0(void);
69void irq_1(void);
wdenk591dda52002-11-18 00:14:45 +000070
Graeme Russ77290ee2009-02-24 21:13:40 +110071int cpu_init_interrupts(void)
wdenk591dda52002-11-18 00:14:45 +000072{
73 int i;
wdenk57b2d802003-06-27 21:31:46 +000074
Graeme Russd11b0852009-11-24 20:04:18 +110075 int irq_entry_size = irq_1 - irq_0;
76 void *irq_entry = (void *)irq_0;
77
wdenk591dda52002-11-18 00:14:45 +000078 /* Just in case... */
79 disable_interrupts();
wdenk57b2d802003-06-27 21:31:46 +000080
wdenk591dda52002-11-18 00:14:45 +000081 /* Setup the IDT */
wdenk57b2d802003-06-27 21:31:46 +000082 for (i=0;i<256;i++) {
wdenk591dda52002-11-18 00:14:45 +000083 idt[i].access = 0x8e;
wdenk57b2d802003-06-27 21:31:46 +000084 idt[i].res = 0;
85 idt[i].selector = 0x10;
Graeme Russd11b0852009-11-24 20:04:18 +110086 set_vector(i, irq_entry);
87 irq_entry += irq_entry_size;
wdenk57b2d802003-06-27 21:31:46 +000088 }
89
Graeme Russd11b0852009-11-24 20:04:18 +110090 idt_ptr.size = 256 * 8;
91 idt_ptr.address = (unsigned long) idt;
92 idt_ptr.segment = 0x18;
93
94 load_idt(&idt_ptr);
wdenk57b2d802003-06-27 21:31:46 +000095
wdenk591dda52002-11-18 00:14:45 +000096 /* It is now safe to enable interrupts */
wdenk57b2d802003-06-27 21:31:46 +000097 enable_interrupts();
98
wdenk591dda52002-11-18 00:14:45 +000099 return 0;
100}
101
Graeme Russd11b0852009-11-24 20:04:18 +1100102void __do_irq(int irq)
103{
104 printf("Unhandled IRQ : %d\n", irq);
105}
106void do_irq(int irq) __attribute__((weak, alias("__do_irq")));
107
wdenk591dda52002-11-18 00:14:45 +0000108void enable_interrupts(void)
109{
110 asm("sti\n");
111}
112
113int disable_interrupts(void)
114{
115 long flags;
wdenk57b2d802003-06-27 21:31:46 +0000116
wdenk591dda52002-11-18 00:14:45 +0000117 asm volatile ("pushfl ; popl %0 ; cli\n" : "=g" (flags) : );
wdenk57b2d802003-06-27 21:31:46 +0000118
wdenk591dda52002-11-18 00:14:45 +0000119 return (flags&0x200); /* IE flags is bit 9 */
120}
Graeme Russd11b0852009-11-24 20:04:18 +1100121
122/* IRQ Low-Level Service Routine */
123__isr__ irq_llsr(int ip, int seg, int irq)
124{
125 /*
126 * For detailed description of each exception, refer to:
127 * Intel® 64 and IA-32 Architectures Software Developer's Manual
128 * Volume 1: Basic Architecture
129 * Order Number: 253665-029US, November 2008
130 * Table 6-1. Exceptions and Interrupts
131 */
132 switch (irq) {
133 case 0x00:
134 printf("Divide Error (Division by zero) at %04x:%08x\n", seg, ip);
135 while(1);
136 break;
137 case 0x01:
138 printf("Debug Interrupt (Single step) at %04x:%08x\n", seg, ip);
139 break;
140 case 0x02:
141 printf("NMI Interrupt at %04x:%08x\n", seg, ip);
142 break;
143 case 0x03:
144 printf("Breakpoint at %04x:%08x\n", seg, ip);
145 break;
146 case 0x04:
147 printf("Overflow at %04x:%08x\n", seg, ip);
148 while(1);
149 break;
150 case 0x05:
151 printf("BOUND Range Exceeded at %04x:%08x\n", seg, ip);
152 while(1);
153 break;
154 case 0x06:
155 printf("Invalid Opcode (UnDefined Opcode) at %04x:%08x\n", seg, ip);
156 while(1);
157 break;
158 case 0x07:
159 printf("Device Not Available (No Math Coprocessor) at %04x:%08x\n", seg, ip);
160 while(1);
161 break;
162 case 0x08:
163 printf("Double fault at %04x:%08x\n", seg, ip);
164 while(1);
165 break;
166 case 0x09:
167 printf("Co-processor segment overrun at %04x:%08x\n", seg, ip);
168 while(1);
169 break;
170 case 0x0a:
171 printf("Invalid TSS at %04x:%08x\n", seg, ip);
172 break;
173 case 0x0b:
174 printf("Segment Not Present at %04x:%08x\n", seg, ip);
175 while(1);
176 break;
177 case 0x0c:
178 printf("Stack Segment Fault at %04x:%08x\n", seg, ip);
179 while(1);
180 break;
181 case 0x0d:
182 printf("General Protection at %04x:%08x\n", seg, ip);
183 break;
184 case 0x0e:
185 printf("Page fault at %04x:%08x\n", seg, ip);
186 while(1);
187 break;
188 case 0x0f:
189 printf("Floating-Point Error (Math Fault) at %04x:%08x\n", seg, ip);
190 break;
191 case 0x10:
192 printf("Alignment check at %04x:%08x\n", seg, ip);
193 break;
194 case 0x11:
195 printf("Machine Check at %04x:%08x\n", seg, ip);
196 break;
197 case 0x12:
198 printf("SIMD Floating-Point Exception at %04x:%08x\n", seg, ip);
199 break;
200 case 0x13:
201 case 0x14:
202 case 0x15:
203 case 0x16:
204 case 0x17:
205 case 0x18:
206 case 0x19:
207 case 0x1a:
208 case 0x1b:
209 case 0x1c:
210 case 0x1d:
211 case 0x1e:
212 case 0x1f:
213 printf("Reserved Exception %d at %04x:%08x\n", irq, seg, ip);
214 break;
215
216 default:
217 /* Hardware or User IRQ */
218 do_irq(irq);
219 }
220}
221
222/*
223 * OK - This looks really horrible, but it serves a purpose - It helps create
224 * fully relocatable code.
225 * - The call to irq_llsr will be a relative jump
226 * - The IRQ entries will be guaranteed to be in order
227 * It's a bit annoying that we need to waste 3 bytes per interrupt entry
228 * (total of 768 code bytes), but we MUST create a Stack Frame and this is
229 * the easiest way I could do it. Maybe it can be made better later.
230 */
231asm(".globl irq_common_entry\n" \
232 "irq_common_entry:\n" \
233 "pushl $0\n" \
234 "pushl $0\n" \
235 "call irq_llsr\n" \
236 "popl %eax\n" \
237 "popl %eax\n" \
238 "popl %eax\n" \
239 "popa\n" \
240 "leave\n"\
241 "iret\n" \
242 DECLARE_INTERRUPT(0) \
243 DECLARE_INTERRUPT(1) \
244 DECLARE_INTERRUPT(2) \
245 DECLARE_INTERRUPT(3) \
246 DECLARE_INTERRUPT(4) \
247 DECLARE_INTERRUPT(5) \
248 DECLARE_INTERRUPT(6) \
249 DECLARE_INTERRUPT(7) \
250 DECLARE_INTERRUPT(8) \
251 DECLARE_INTERRUPT(9) \
252 DECLARE_INTERRUPT(10) \
253 DECLARE_INTERRUPT(11) \
254 DECLARE_INTERRUPT(12) \
255 DECLARE_INTERRUPT(13) \
256 DECLARE_INTERRUPT(14) \
257 DECLARE_INTERRUPT(15) \
258 DECLARE_INTERRUPT(16) \
259 DECLARE_INTERRUPT(17) \
260 DECLARE_INTERRUPT(18) \
261 DECLARE_INTERRUPT(19) \
262 DECLARE_INTERRUPT(20) \
263 DECLARE_INTERRUPT(21) \
264 DECLARE_INTERRUPT(22) \
265 DECLARE_INTERRUPT(23) \
266 DECLARE_INTERRUPT(24) \
267 DECLARE_INTERRUPT(25) \
268 DECLARE_INTERRUPT(26) \
269 DECLARE_INTERRUPT(27) \
270 DECLARE_INTERRUPT(28) \
271 DECLARE_INTERRUPT(29) \
272 DECLARE_INTERRUPT(30) \
273 DECLARE_INTERRUPT(31) \
274 DECLARE_INTERRUPT(32) \
275 DECLARE_INTERRUPT(33) \
276 DECLARE_INTERRUPT(34) \
277 DECLARE_INTERRUPT(35) \
278 DECLARE_INTERRUPT(36) \
279 DECLARE_INTERRUPT(37) \
280 DECLARE_INTERRUPT(38) \
281 DECLARE_INTERRUPT(39) \
282 DECLARE_INTERRUPT(40) \
283 DECLARE_INTERRUPT(41) \
284 DECLARE_INTERRUPT(42) \
285 DECLARE_INTERRUPT(43) \
286 DECLARE_INTERRUPT(44) \
287 DECLARE_INTERRUPT(45) \
288 DECLARE_INTERRUPT(46) \
289 DECLARE_INTERRUPT(47) \
290 DECLARE_INTERRUPT(48) \
291 DECLARE_INTERRUPT(49) \
292 DECLARE_INTERRUPT(50) \
293 DECLARE_INTERRUPT(51) \
294 DECLARE_INTERRUPT(52) \
295 DECLARE_INTERRUPT(53) \
296 DECLARE_INTERRUPT(54) \
297 DECLARE_INTERRUPT(55) \
298 DECLARE_INTERRUPT(56) \
299 DECLARE_INTERRUPT(57) \
300 DECLARE_INTERRUPT(58) \
301 DECLARE_INTERRUPT(59) \
302 DECLARE_INTERRUPT(60) \
303 DECLARE_INTERRUPT(61) \
304 DECLARE_INTERRUPT(62) \
305 DECLARE_INTERRUPT(63) \
306 DECLARE_INTERRUPT(64) \
307 DECLARE_INTERRUPT(65) \
308 DECLARE_INTERRUPT(66) \
309 DECLARE_INTERRUPT(67) \
310 DECLARE_INTERRUPT(68) \
311 DECLARE_INTERRUPT(69) \
312 DECLARE_INTERRUPT(70) \
313 DECLARE_INTERRUPT(71) \
314 DECLARE_INTERRUPT(72) \
315 DECLARE_INTERRUPT(73) \
316 DECLARE_INTERRUPT(74) \
317 DECLARE_INTERRUPT(75) \
318 DECLARE_INTERRUPT(76) \
319 DECLARE_INTERRUPT(77) \
320 DECLARE_INTERRUPT(78) \
321 DECLARE_INTERRUPT(79) \
322 DECLARE_INTERRUPT(80) \
323 DECLARE_INTERRUPT(81) \
324 DECLARE_INTERRUPT(82) \
325 DECLARE_INTERRUPT(83) \
326 DECLARE_INTERRUPT(84) \
327 DECLARE_INTERRUPT(85) \
328 DECLARE_INTERRUPT(86) \
329 DECLARE_INTERRUPT(87) \
330 DECLARE_INTERRUPT(88) \
331 DECLARE_INTERRUPT(89) \
332 DECLARE_INTERRUPT(90) \
333 DECLARE_INTERRUPT(91) \
334 DECLARE_INTERRUPT(92) \
335 DECLARE_INTERRUPT(93) \
336 DECLARE_INTERRUPT(94) \
337 DECLARE_INTERRUPT(95) \
338 DECLARE_INTERRUPT(97) \
339 DECLARE_INTERRUPT(96) \
340 DECLARE_INTERRUPT(98) \
341 DECLARE_INTERRUPT(99) \
342 DECLARE_INTERRUPT(100) \
343 DECLARE_INTERRUPT(101) \
344 DECLARE_INTERRUPT(102) \
345 DECLARE_INTERRUPT(103) \
346 DECLARE_INTERRUPT(104) \
347 DECLARE_INTERRUPT(105) \
348 DECLARE_INTERRUPT(106) \
349 DECLARE_INTERRUPT(107) \
350 DECLARE_INTERRUPT(108) \
351 DECLARE_INTERRUPT(109) \
352 DECLARE_INTERRUPT(110) \
353 DECLARE_INTERRUPT(111) \
354 DECLARE_INTERRUPT(112) \
355 DECLARE_INTERRUPT(113) \
356 DECLARE_INTERRUPT(114) \
357 DECLARE_INTERRUPT(115) \
358 DECLARE_INTERRUPT(116) \
359 DECLARE_INTERRUPT(117) \
360 DECLARE_INTERRUPT(118) \
361 DECLARE_INTERRUPT(119) \
362 DECLARE_INTERRUPT(120) \
363 DECLARE_INTERRUPT(121) \
364 DECLARE_INTERRUPT(122) \
365 DECLARE_INTERRUPT(123) \
366 DECLARE_INTERRUPT(124) \
367 DECLARE_INTERRUPT(125) \
368 DECLARE_INTERRUPT(126) \
369 DECLARE_INTERRUPT(127) \
370 DECLARE_INTERRUPT(128) \
371 DECLARE_INTERRUPT(129) \
372 DECLARE_INTERRUPT(130) \
373 DECLARE_INTERRUPT(131) \
374 DECLARE_INTERRUPT(132) \
375 DECLARE_INTERRUPT(133) \
376 DECLARE_INTERRUPT(134) \
377 DECLARE_INTERRUPT(135) \
378 DECLARE_INTERRUPT(136) \
379 DECLARE_INTERRUPT(137) \
380 DECLARE_INTERRUPT(138) \
381 DECLARE_INTERRUPT(139) \
382 DECLARE_INTERRUPT(140) \
383 DECLARE_INTERRUPT(141) \
384 DECLARE_INTERRUPT(142) \
385 DECLARE_INTERRUPT(143) \
386 DECLARE_INTERRUPT(144) \
387 DECLARE_INTERRUPT(145) \
388 DECLARE_INTERRUPT(146) \
389 DECLARE_INTERRUPT(147) \
390 DECLARE_INTERRUPT(148) \
391 DECLARE_INTERRUPT(149) \
392 DECLARE_INTERRUPT(150) \
393 DECLARE_INTERRUPT(151) \
394 DECLARE_INTERRUPT(152) \
395 DECLARE_INTERRUPT(153) \
396 DECLARE_INTERRUPT(154) \
397 DECLARE_INTERRUPT(155) \
398 DECLARE_INTERRUPT(156) \
399 DECLARE_INTERRUPT(157) \
400 DECLARE_INTERRUPT(158) \
401 DECLARE_INTERRUPT(159) \
402 DECLARE_INTERRUPT(160) \
403 DECLARE_INTERRUPT(161) \
404 DECLARE_INTERRUPT(162) \
405 DECLARE_INTERRUPT(163) \
406 DECLARE_INTERRUPT(164) \
407 DECLARE_INTERRUPT(165) \
408 DECLARE_INTERRUPT(166) \
409 DECLARE_INTERRUPT(167) \
410 DECLARE_INTERRUPT(168) \
411 DECLARE_INTERRUPT(169) \
412 DECLARE_INTERRUPT(170) \
413 DECLARE_INTERRUPT(171) \
414 DECLARE_INTERRUPT(172) \
415 DECLARE_INTERRUPT(173) \
416 DECLARE_INTERRUPT(174) \
417 DECLARE_INTERRUPT(175) \
418 DECLARE_INTERRUPT(176) \
419 DECLARE_INTERRUPT(177) \
420 DECLARE_INTERRUPT(178) \
421 DECLARE_INTERRUPT(179) \
422 DECLARE_INTERRUPT(180) \
423 DECLARE_INTERRUPT(181) \
424 DECLARE_INTERRUPT(182) \
425 DECLARE_INTERRUPT(183) \
426 DECLARE_INTERRUPT(184) \
427 DECLARE_INTERRUPT(185) \
428 DECLARE_INTERRUPT(186) \
429 DECLARE_INTERRUPT(187) \
430 DECLARE_INTERRUPT(188) \
431 DECLARE_INTERRUPT(189) \
432 DECLARE_INTERRUPT(190) \
433 DECLARE_INTERRUPT(191) \
434 DECLARE_INTERRUPT(192) \
435 DECLARE_INTERRUPT(193) \
436 DECLARE_INTERRUPT(194) \
437 DECLARE_INTERRUPT(195) \
438 DECLARE_INTERRUPT(196) \
439 DECLARE_INTERRUPT(197) \
440 DECLARE_INTERRUPT(198) \
441 DECLARE_INTERRUPT(199) \
442 DECLARE_INTERRUPT(200) \
443 DECLARE_INTERRUPT(201) \
444 DECLARE_INTERRUPT(202) \
445 DECLARE_INTERRUPT(203) \
446 DECLARE_INTERRUPT(204) \
447 DECLARE_INTERRUPT(205) \
448 DECLARE_INTERRUPT(206) \
449 DECLARE_INTERRUPT(207) \
450 DECLARE_INTERRUPT(208) \
451 DECLARE_INTERRUPT(209) \
452 DECLARE_INTERRUPT(210) \
453 DECLARE_INTERRUPT(211) \
454 DECLARE_INTERRUPT(212) \
455 DECLARE_INTERRUPT(213) \
456 DECLARE_INTERRUPT(214) \
457 DECLARE_INTERRUPT(215) \
458 DECLARE_INTERRUPT(216) \
459 DECLARE_INTERRUPT(217) \
460 DECLARE_INTERRUPT(218) \
461 DECLARE_INTERRUPT(219) \
462 DECLARE_INTERRUPT(220) \
463 DECLARE_INTERRUPT(221) \
464 DECLARE_INTERRUPT(222) \
465 DECLARE_INTERRUPT(223) \
466 DECLARE_INTERRUPT(224) \
467 DECLARE_INTERRUPT(225) \
468 DECLARE_INTERRUPT(226) \
469 DECLARE_INTERRUPT(227) \
470 DECLARE_INTERRUPT(228) \
471 DECLARE_INTERRUPT(229) \
472 DECLARE_INTERRUPT(230) \
473 DECLARE_INTERRUPT(231) \
474 DECLARE_INTERRUPT(232) \
475 DECLARE_INTERRUPT(233) \
476 DECLARE_INTERRUPT(234) \
477 DECLARE_INTERRUPT(235) \
478 DECLARE_INTERRUPT(236) \
479 DECLARE_INTERRUPT(237) \
480 DECLARE_INTERRUPT(238) \
481 DECLARE_INTERRUPT(239) \
482 DECLARE_INTERRUPT(240) \
483 DECLARE_INTERRUPT(241) \
484 DECLARE_INTERRUPT(242) \
485 DECLARE_INTERRUPT(243) \
486 DECLARE_INTERRUPT(244) \
487 DECLARE_INTERRUPT(245) \
488 DECLARE_INTERRUPT(246) \
489 DECLARE_INTERRUPT(247) \
490 DECLARE_INTERRUPT(248) \
491 DECLARE_INTERRUPT(249) \
492 DECLARE_INTERRUPT(250) \
493 DECLARE_INTERRUPT(251) \
494 DECLARE_INTERRUPT(252) \
495 DECLARE_INTERRUPT(253) \
496 DECLARE_INTERRUPT(254) \
497 DECLARE_INTERRUPT(255));