1#include <linux/errno.h>
2#include <linux/signal.h>
3#include <linux/sched.h>
4#include <linux/ioport.h>
5#include <linux/interrupt.h>
6#include <linux/slab.h>
7#include <linux/random.h>
8#include <linux/init.h>
9#include <linux/kernel_stat.h>
10#include <linux/sysdev.h>
11#include <linux/bitops.h>
12
13#include <asm/8253pit.h>
14#include <asm/atomic.h>
15#include <asm/system.h>
16#include <asm/io.h>
17#include <asm/timer.h>
18#include <asm/pgtable.h>
19#include <asm/delay.h>
20#include <asm/desc.h>
21#include <asm/apic.h>
22#include <asm/arch_hooks.h>
23#include <asm/i8259.h>
24
25#include <io_ports.h>
26
27
28
29
30
31
32
33
34
35
36static int i8259A_auto_eoi;
37DEFINE_SPINLOCK(i8259A_lock);
38static void mask_and_ack_8259A(unsigned int);
39
40static struct irq_chip i8259A_chip = {
41 .name = "XT-PIC",
42 .mask = disable_8259A_irq,
43 .disable = disable_8259A_irq,
44 .unmask = enable_8259A_irq,
45 .mask_ack = mask_and_ack_8259A,
46};
47
48
49
50
51
52
53
54
55unsigned int cached_irq_mask = 0xffff;
56
57
58
59
60
61
62
63
64
65
66unsigned long io_apic_irqs;
67
68void disable_8259A_irq(unsigned int irq)
69{
70 unsigned int mask = 1 << irq;
71 unsigned long flags;
72
73 spin_lock_irqsave(&i8259A_lock, flags);
74 cached_irq_mask |= mask;
75 if (irq & 8)
76 outb(cached_slave_mask, PIC_SLAVE_IMR);
77 else
78 outb(cached_master_mask, PIC_MASTER_IMR);
79 spin_unlock_irqrestore(&i8259A_lock, flags);
80}
81
82void enable_8259A_irq(unsigned int irq)
83{
84 unsigned int mask = ~(1 << irq);
85 unsigned long flags;
86
87 spin_lock_irqsave(&i8259A_lock, flags);
88 cached_irq_mask &= mask;
89 if (irq & 8)
90 outb(cached_slave_mask, PIC_SLAVE_IMR);
91 else
92 outb(cached_master_mask, PIC_MASTER_IMR);
93 spin_unlock_irqrestore(&i8259A_lock, flags);
94}
95
96int i8259A_irq_pending(unsigned int irq)
97{
98 unsigned int mask = 1<<irq;
99 unsigned long flags;
100 int ret;
101
102 spin_lock_irqsave(&i8259A_lock, flags);
103 if (irq < 8)
104 ret = inb(PIC_MASTER_CMD) & mask;
105 else
106 ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
107 spin_unlock_irqrestore(&i8259A_lock, flags);
108
109 return ret;
110}
111
112void make_8259A_irq(unsigned int irq)
113{
114 disable_irq_nosync(irq);
115 io_apic_irqs &= ~(1<<irq);
116 set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
117 "XT");
118 enable_irq(irq);
119}
120
121
122
123
124
125
126
127static inline int i8259A_irq_real(unsigned int irq)
128{
129 int value;
130 int irqmask = 1<<irq;
131
132 if (irq < 8) {
133 outb(0x0B,PIC_MASTER_CMD);
134 value = inb(PIC_MASTER_CMD) & irqmask;
135 outb(0x0A,PIC_MASTER_CMD);
136 return value;
137 }
138 outb(0x0B,PIC_SLAVE_CMD);
139 value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
140 outb(0x0A,PIC_SLAVE_CMD);
141 return value;
142}
143
144
145
146
147
148
149
150static void mask_and_ack_8259A(unsigned int irq)
151{
152 unsigned int irqmask = 1 << irq;
153 unsigned long flags;
154
155 spin_lock_irqsave(&i8259A_lock, flags);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 if (cached_irq_mask & irqmask)
172 goto spurious_8259A_irq;
173 cached_irq_mask |= irqmask;
174
175handle_real_irq:
176 if (irq & 8) {
177 inb(PIC_SLAVE_IMR);
178 outb(cached_slave_mask, PIC_SLAVE_IMR);
179 outb(0x60+(irq&7),PIC_SLAVE_CMD);
180 outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD);
181 } else {
182 inb(PIC_MASTER_IMR);
183 outb(cached_master_mask, PIC_MASTER_IMR);
184 outb(0x60+irq,PIC_MASTER_CMD);
185 }
186 spin_unlock_irqrestore(&i8259A_lock, flags);
187 return;
188
189spurious_8259A_irq:
190
191
192
193 if (i8259A_irq_real(irq))
194
195
196
197
198 goto handle_real_irq;
199
200 {
201 static int spurious_irq_mask;
202
203
204
205
206 if (!(spurious_irq_mask & irqmask)) {
207 printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
208 spurious_irq_mask |= irqmask;
209 }
210 atomic_inc(&irq_err_count);
211
212
213
214
215
216 goto handle_real_irq;
217 }
218}
219
220static char irq_trigger[2];
221
222
223
224static void restore_ELCR(char *trigger)
225{
226 outb(trigger[0], 0x4d0);
227 outb(trigger[1], 0x4d1);
228}
229
230static void save_ELCR(char *trigger)
231{
232
233 trigger[0] = inb(0x4d0) & 0xF8;
234 trigger[1] = inb(0x4d1) & 0xDE;
235}
236
237static int i8259A_resume(struct sys_device *dev)
238{
239 init_8259A(i8259A_auto_eoi);
240 restore_ELCR(irq_trigger);
241 return 0;
242}
243
244static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
245{
246 save_ELCR(irq_trigger);
247 return 0;
248}
249
250static int i8259A_shutdown(struct sys_device *dev)
251{
252
253
254
255
256 outb(0xff, PIC_MASTER_IMR);
257 outb(0xff, PIC_SLAVE_IMR);
258 return 0;
259}
260
261static struct sysdev_class i8259_sysdev_class = {
262 set_kset_name("i8259"),
263 .suspend = i8259A_suspend,
264 .resume = i8259A_resume,
265 .shutdown = i8259A_shutdown,
266};
267
268static struct sys_device device_i8259A = {
269 .id = 0,
270 .cls = &i8259_sysdev_class,
271};
272
273static int __init i8259A_init_sysfs(void)
274{
275 int error = sysdev_class_register(&i8259_sysdev_class);
276 if (!error)
277 error = sysdev_register(&device_i8259A);
278 return error;
279}
280
281device_initcall(i8259A_init_sysfs);
282
283void init_8259A(int auto_eoi)
284{
285 unsigned long flags;
286
287 i8259A_auto_eoi = auto_eoi;
288
289 spin_lock_irqsave(&i8259A_lock, flags);
290
291 outb(0xff, PIC_MASTER_IMR);
292 outb(0xff, PIC_SLAVE_IMR);
293
294
295
296
297 outb_p(0x11, PIC_MASTER_CMD);
298 outb_p(0x20 + 0, PIC_MASTER_IMR);
299 outb_p(1U << PIC_CASCADE_IR, PIC_MASTER_IMR);
300 if (auto_eoi)
301 outb_p(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
302 else
303 outb_p(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
304
305 outb_p(0x11, PIC_SLAVE_CMD);
306 outb_p(0x20 + 8, PIC_SLAVE_IMR);
307 outb_p(PIC_CASCADE_IR, PIC_SLAVE_IMR);
308 outb_p(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
309 if (auto_eoi)
310
311
312
313
314 i8259A_chip.mask_ack = disable_8259A_irq;
315 else
316 i8259A_chip.mask_ack = mask_and_ack_8259A;
317
318 udelay(100);
319
320 outb(cached_master_mask, PIC_MASTER_IMR);
321 outb(cached_slave_mask, PIC_SLAVE_IMR);
322
323 spin_unlock_irqrestore(&i8259A_lock, flags);
324}
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339static irqreturn_t math_error_irq(int cpl, void *dev_id)
340{
341 extern void math_error(void __user *);
342 outb(0,0xF0);
343 if (ignore_fpu_irq || !boot_cpu_data.hard_math)
344 return IRQ_NONE;
345 math_error((void __user *)get_irq_regs()->eip);
346 return IRQ_HANDLED;
347}
348
349
350
351
352
353static struct irqaction fpu_irq = { math_error_irq, 0, CPU_MASK_NONE, "fpu", NULL, NULL };
354
355void __init init_ISA_irqs (void)
356{
357 int i;
358
359#ifdef CONFIG_X86_LOCAL_APIC
360 init_bsp_APIC();
361#endif
362 init_8259A(0);
363
364 for (i = 0; i < NR_IRQS; i++) {
365 irq_desc[i].status = IRQ_DISABLED;
366 irq_desc[i].action = NULL;
367 irq_desc[i].depth = 1;
368
369 if (i < 16) {
370
371
372
373 set_irq_chip_and_handler_name(i, &i8259A_chip,
374 handle_level_irq, "XT");
375 } else {
376
377
378
379 irq_desc[i].chip = &no_irq_chip;
380 }
381 }
382}
383
384
385void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
386
387void __init native_init_IRQ(void)
388{
389 int i;
390
391
392 pre_intr_init_hook();
393
394
395
396
397
398
399 for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
400 int vector = FIRST_EXTERNAL_VECTOR + i;
401 if (i >= NR_IRQS)
402 break;
403 if (vector != SYSCALL_VECTOR)
404 set_intr_gate(vector, interrupt[i]);
405 }
406
407
408
409
410 intr_init_hook();
411
412
413
414
415
416 if (boot_cpu_data.hard_math && !cpu_has_fpu)
417 setup_irq(FPU_IRQ, &fpu_irq);
418
419 irq_ctx_init(smp_processor_id());
420}
421