1#include <linux/config.h>
2#include <linux/ptrace.h>
3#include <linux/errno.h>
4#include <linux/signal.h>
5#include <linux/sched.h>
6#include <linux/ioport.h>
7#include <linux/interrupt.h>
8#include <linux/timex.h>
9#include <linux/slab.h>
10#include <linux/random.h>
11#include <linux/smp_lock.h>
12#include <linux/init.h>
13#include <linux/kernel_stat.h>
14
15#include <asm/atomic.h>
16#include <asm/system.h>
17#include <asm/io.h>
18#include <asm/irq.h>
19#include <asm/bitops.h>
20#include <asm/pgtable.h>
21#include <asm/delay.h>
22#include <asm/desc.h>
23#include <asm/apic.h>
24
25#include <linux/irq.h>
26
27
28
29
30
31
32
33
34
35
36
37
38BUILD_COMMON_IRQ()
39
40#define BI(x,y) \
41 BUILD_IRQ(x##y)
42
43#define BUILD_16_IRQS(x) \
44 BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
45 BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
46 BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
47 BI(x,c) BI(x,d) BI(x,e) BI(x,f)
48
49
50
51
52
53BUILD_16_IRQS(0x0)
54
55#ifdef CONFIG_X86_IO_APIC
56
57
58
59
60
61
62
63
64
65
66 BUILD_16_IRQS(0x1) BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
67BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
68BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
69BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd)
70#endif
71
72#undef BUILD_16_IRQS
73#undef BI
74
75
76
77
78
79
80
81#ifdef CONFIG_SMP
82BUILD_SMP_INTERRUPT(reschedule_interrupt,RESCHEDULE_VECTOR)
83BUILD_SMP_INTERRUPT(invalidate_interrupt,INVALIDATE_TLB_VECTOR)
84BUILD_SMP_INTERRUPT(call_function_interrupt,CALL_FUNCTION_VECTOR)
85#endif
86
87
88
89
90
91
92
93
94#ifdef CONFIG_X86_LOCAL_APIC
95BUILD_SMP_TIMER_INTERRUPT(apic_timer_interrupt,LOCAL_TIMER_VECTOR)
96BUILD_SMP_INTERRUPT(error_interrupt,ERROR_APIC_VECTOR)
97BUILD_SMP_INTERRUPT(spurious_interrupt,SPURIOUS_APIC_VECTOR)
98#endif
99
100#define IRQ(x,y) \
101 IRQ##x##y##_interrupt
102
103#define IRQLIST_16(x) \
104 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
105 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
106 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
107 IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
108
109void (*interrupt[NR_IRQS])(void) = {
110 IRQLIST_16(0x0),
111
112#ifdef CONFIG_X86_IO_APIC
113 IRQLIST_16(0x1), IRQLIST_16(0x2), IRQLIST_16(0x3),
114 IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
115 IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
116 IRQLIST_16(0xc), IRQLIST_16(0xd)
117#endif
118};
119
120#undef IRQ
121#undef IRQLIST_16
122
123
124
125
126
127
128
129
130
131
132spinlock_t i8259A_lock = SPIN_LOCK_UNLOCKED;
133
134static void end_8259A_irq (unsigned int irq)
135{
136 if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
137 enable_8259A_irq(irq);
138}
139
140#define shutdown_8259A_irq disable_8259A_irq
141
142void mask_and_ack_8259A(unsigned int);
143
144static unsigned int startup_8259A_irq(unsigned int irq)
145{
146 enable_8259A_irq(irq);
147 return 0;
148}
149
150static struct hw_interrupt_type i8259A_irq_type = {
151 "XT-PIC",
152 startup_8259A_irq,
153 shutdown_8259A_irq,
154 enable_8259A_irq,
155 disable_8259A_irq,
156 mask_and_ack_8259A,
157 end_8259A_irq,
158 NULL
159};
160
161
162
163
164
165
166
167
168static unsigned int cached_irq_mask = 0xffff;
169
170#define __byte(x,y) (((unsigned char *)&(y))[x])
171#define cached_21 (__byte(0,cached_irq_mask))
172#define cached_A1 (__byte(1,cached_irq_mask))
173
174
175
176
177
178
179
180
181
182
183unsigned long io_apic_irqs;
184
185void disable_8259A_irq(unsigned int irq)
186{
187 unsigned int mask = 1 << irq;
188 unsigned long flags;
189
190 spin_lock_irqsave(&i8259A_lock, flags);
191 cached_irq_mask |= mask;
192 if (irq & 8)
193 outb(cached_A1,0xA1);
194 else
195 outb(cached_21,0x21);
196 spin_unlock_irqrestore(&i8259A_lock, flags);
197}
198
199void enable_8259A_irq(unsigned int irq)
200{
201 unsigned int mask = ~(1 << irq);
202 unsigned long flags;
203
204 spin_lock_irqsave(&i8259A_lock, flags);
205 cached_irq_mask &= mask;
206 if (irq & 8)
207 outb(cached_A1,0xA1);
208 else
209 outb(cached_21,0x21);
210 spin_unlock_irqrestore(&i8259A_lock, flags);
211}
212
213int i8259A_irq_pending(unsigned int irq)
214{
215 unsigned int mask = 1<<irq;
216 unsigned long flags;
217 int ret;
218
219 spin_lock_irqsave(&i8259A_lock, flags);
220 if (irq < 8)
221 ret = inb(0x20) & mask;
222 else
223 ret = inb(0xA0) & (mask >> 8);
224 spin_unlock_irqrestore(&i8259A_lock, flags);
225
226 return ret;
227}
228
229void make_8259A_irq(unsigned int irq)
230{
231 disable_irq_nosync(irq);
232 io_apic_irqs &= ~(1<<irq);
233 irq_desc[irq].handler = &i8259A_irq_type;
234 enable_irq(irq);
235}
236
237
238
239
240
241
242
243static inline int i8259A_irq_real(unsigned int irq)
244{
245 int value;
246 int irqmask = 1<<irq;
247
248 if (irq < 8) {
249 outb(0x0B,0x20);
250 value = inb(0x20) & irqmask;
251 outb(0x0A,0x20);
252 return value;
253 }
254 outb(0x0B,0xA0);
255 value = inb(0xA0) & (irqmask >> 8);
256 outb(0x0A,0xA0);
257 return value;
258}
259
260
261
262
263
264
265
266void mask_and_ack_8259A(unsigned int irq)
267{
268 unsigned int irqmask = 1 << irq;
269 unsigned long flags;
270
271 spin_lock_irqsave(&i8259A_lock, flags);
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 if (cached_irq_mask & irqmask)
288 goto spurious_8259A_irq;
289 cached_irq_mask |= irqmask;
290
291handle_real_irq:
292 if (irq & 8) {
293 inb(0xA1);
294 outb(cached_A1,0xA1);
295 outb(0x60+(irq&7),0xA0);
296 outb(0x62,0x20);
297 } else {
298 inb(0x21);
299 outb(cached_21,0x21);
300 outb(0x60+irq,0x20);
301 }
302 spin_unlock_irqrestore(&i8259A_lock, flags);
303 return;
304
305spurious_8259A_irq:
306
307
308
309 if (i8259A_irq_real(irq))
310
311
312
313
314 goto handle_real_irq;
315
316 {
317 static int spurious_irq_mask;
318
319
320
321
322 if (!(spurious_irq_mask & irqmask)) {
323 printk("spurious 8259A interrupt: IRQ%d.\n", irq);
324 spurious_irq_mask |= irqmask;
325 }
326 atomic_inc(&irq_err_count);
327
328
329
330
331
332 goto handle_real_irq;
333 }
334}
335
336void __init init_8259A(int auto_eoi)
337{
338 unsigned long flags;
339
340 spin_lock_irqsave(&i8259A_lock, flags);
341
342 outb(0xff, 0x21);
343 outb(0xff, 0xA1);
344
345
346
347
348 outb_p(0x11, 0x20);
349 outb_p(0x20 + 0, 0x21);
350 outb_p(0x04, 0x21);
351 if (auto_eoi)
352 outb_p(0x03, 0x21);
353 else
354 outb_p(0x01, 0x21);
355
356 outb_p(0x11, 0xA0);
357 outb_p(0x20 + 8, 0xA1);
358 outb_p(0x02, 0xA1);
359 outb_p(0x01, 0xA1);
360
361
362 if (auto_eoi)
363
364
365
366
367 i8259A_irq_type.ack = disable_8259A_irq;
368 else
369 i8259A_irq_type.ack = mask_and_ack_8259A;
370
371 udelay(100);
372
373 outb(cached_21, 0x21);
374 outb(cached_A1, 0xA1);
375
376 spin_unlock_irqrestore(&i8259A_lock, flags);
377}
378
379
380
381
382
383
384
385
386
387
388
389
390
391static void math_error_irq(int cpl, void *dev_id, struct pt_regs *regs)
392{
393 extern void math_error(void *);
394 outb(0,0xF0);
395 if (ignore_irq13 || !boot_cpu_data.hard_math)
396 return;
397 math_error((void *)regs->eip);
398}
399
400
401
402
403
404static struct irqaction irq13 = { math_error_irq, 0, 0, "fpu", NULL, NULL };
405
406
407
408
409
410#ifndef CONFIG_VISWS
411static struct irqaction irq2 = { no_action, 0, 0, "cascade", NULL, NULL};
412#endif
413
414
415void __init init_ISA_irqs (void)
416{
417 int i;
418
419#ifdef CONFIG_X86_LOCAL_APIC
420 init_bsp_APIC();
421#endif
422 init_8259A(0);
423
424 for (i = 0; i < NR_IRQS; i++) {
425 irq_desc[i].status = IRQ_DISABLED;
426 irq_desc[i].action = 0;
427 irq_desc[i].depth = 1;
428
429 if (i < 16) {
430
431
432
433 irq_desc[i].handler = &i8259A_irq_type;
434 } else {
435
436
437
438 irq_desc[i].handler = &no_irq_type;
439 }
440 }
441}
442
443void __init init_IRQ(void)
444{
445 int i;
446
447#ifndef CONFIG_X86_VISWS_APIC
448 init_ISA_irqs();
449#else
450 init_VISWS_APIC_irqs();
451#endif
452
453
454
455
456
457 for (i = 0; i < NR_IRQS; i++) {
458 int vector = FIRST_EXTERNAL_VECTOR + i;
459 if (vector != SYSCALL_VECTOR)
460 set_intr_gate(vector, interrupt[i]);
461 }
462
463#ifdef CONFIG_SMP
464
465
466
467
468 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
469
470
471
472
473
474 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
475
476
477 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
478
479
480 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
481#endif
482
483#ifdef CONFIG_X86_LOCAL_APIC
484
485 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
486
487
488 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
489 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
490#endif
491
492
493
494
495
496 outb_p(0x34,0x43);
497 outb_p(LATCH & 0xff , 0x40);
498 outb(LATCH >> 8 , 0x40);
499
500#ifndef CONFIG_VISWS
501 setup_irq(2, &irq2);
502#endif
503
504
505
506
507
508 if (boot_cpu_data.hard_math && !cpu_has_fpu)
509 setup_irq(13, &irq13);
510}
511