blob: fb725ee15f5515c9fb6333145f586a7079acae20 [file] [log] [blame]
Pavel Machek21fd5132008-05-21 11:44:02 +02001#include <linux/linkage.h>
Pavel Machek21fd5132008-05-21 11:44:02 +02002#include <linux/errno.h>
3#include <linux/signal.h>
4#include <linux/sched.h>
5#include <linux/ioport.h>
6#include <linux/interrupt.h>
Pavel Machek21fd5132008-05-21 11:44:02 +02007#include <linux/timex.h>
Pavel Machek21fd5132008-05-21 11:44:02 +02008#include <linux/slab.h>
9#include <linux/random.h>
10#include <linux/init.h>
11#include <linux/kernel_stat.h>
12#include <linux/sysdev.h>
13#include <linux/bitops.h>
Jaswinder Singh Rajput7bafaf32009-01-04 16:33:52 +053014#include <linux/acpi.h>
15#include <linux/io.h>
16#include <linux/delay.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020017
Pavel Machek21fd5132008-05-21 11:44:02 +020018#include <asm/atomic.h>
19#include <asm/system.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020020#include <asm/timer.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020021#include <asm/hw_irq.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020022#include <asm/pgtable.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020023#include <asm/desc.h>
24#include <asm/apic.h>
Pavel Machek21fd5132008-05-21 11:44:02 +020025#include <asm/i8259.h>
26
27/*
28 * This is the 'legacy' 8259A Programmable Interrupt Controller,
29 * present in the majority of PC/AT boxes.
30 * plus some generic x86 specific things if generic specifics makes
31 * any sense at all.
32 */
33
34static int i8259A_auto_eoi;
Thomas Gleixner5619c282009-07-25 18:35:11 +020035DEFINE_RAW_SPINLOCK(i8259A_lock);
Pavel Machek21fd5132008-05-21 11:44:02 +020036static void mask_and_ack_8259A(unsigned int);
Jacob Panb81bb372009-11-09 11:27:04 -080037static void mask_8259A(void);
38static void unmask_8259A(void);
39static void disable_8259A_irq(unsigned int irq);
40static void enable_8259A_irq(unsigned int irq);
41static void init_8259A(int auto_eoi);
42static int i8259A_irq_pending(unsigned int irq);
Pavel Machek21fd5132008-05-21 11:44:02 +020043
44struct irq_chip i8259A_chip = {
45 .name = "XT-PIC",
46 .mask = disable_8259A_irq,
47 .disable = disable_8259A_irq,
48 .unmask = enable_8259A_irq,
49 .mask_ack = mask_and_ack_8259A,
50};
51
52/*
53 * 8259A PIC functions to handle ISA devices:
54 */
55
56/*
57 * This contains the irq mask for both 8259A irq controllers,
58 */
59unsigned int cached_irq_mask = 0xffff;
60
61/*
62 * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
63 * boards the timer interrupt is not really connected to any IO-APIC pin,
64 * it's fed to the master 8259A's IR0 line only.
65 *
66 * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
67 * this 'mixed mode' IRQ handling costs nothing because it's only used
68 * at IRQ setup time.
69 */
70unsigned long io_apic_irqs;
71
Jacob Panb81bb372009-11-09 11:27:04 -080072static void disable_8259A_irq(unsigned int irq)
Pavel Machek21fd5132008-05-21 11:44:02 +020073{
74 unsigned int mask = 1 << irq;
75 unsigned long flags;
76
Thomas Gleixner5619c282009-07-25 18:35:11 +020077 raw_spin_lock_irqsave(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +020078 cached_irq_mask |= mask;
79 if (irq & 8)
80 outb(cached_slave_mask, PIC_SLAVE_IMR);
81 else
82 outb(cached_master_mask, PIC_MASTER_IMR);
Thomas Gleixner5619c282009-07-25 18:35:11 +020083 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +020084}
85
Jacob Panb81bb372009-11-09 11:27:04 -080086static void enable_8259A_irq(unsigned int irq)
Pavel Machek21fd5132008-05-21 11:44:02 +020087{
88 unsigned int mask = ~(1 << irq);
89 unsigned long flags;
90
Thomas Gleixner5619c282009-07-25 18:35:11 +020091 raw_spin_lock_irqsave(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +020092 cached_irq_mask &= mask;
93 if (irq & 8)
94 outb(cached_slave_mask, PIC_SLAVE_IMR);
95 else
96 outb(cached_master_mask, PIC_MASTER_IMR);
Thomas Gleixner5619c282009-07-25 18:35:11 +020097 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +020098}
99
Jacob Panb81bb372009-11-09 11:27:04 -0800100static int i8259A_irq_pending(unsigned int irq)
Pavel Machek21fd5132008-05-21 11:44:02 +0200101{
102 unsigned int mask = 1<<irq;
103 unsigned long flags;
104 int ret;
105
Thomas Gleixner5619c282009-07-25 18:35:11 +0200106 raw_spin_lock_irqsave(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200107 if (irq < 8)
108 ret = inb(PIC_MASTER_CMD) & mask;
109 else
110 ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
Thomas Gleixner5619c282009-07-25 18:35:11 +0200111 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200112
113 return ret;
114}
115
Jacob Panb81bb372009-11-09 11:27:04 -0800116static void make_8259A_irq(unsigned int irq)
Pavel Machek21fd5132008-05-21 11:44:02 +0200117{
118 disable_irq_nosync(irq);
119 io_apic_irqs &= ~(1<<irq);
120 set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
121 "XT");
122 enable_irq(irq);
123}
124
125/*
126 * This function assumes to be called rarely. Switching between
127 * 8259A registers is slow.
128 * This has to be protected by the irq controller spinlock
129 * before being called.
130 */
131static inline int i8259A_irq_real(unsigned int irq)
132{
133 int value;
134 int irqmask = 1<<irq;
135
136 if (irq < 8) {
Pavel Machek680afbf2008-05-21 11:57:52 +0200137 outb(0x0B, PIC_MASTER_CMD); /* ISR register */
Pavel Machek21fd5132008-05-21 11:44:02 +0200138 value = inb(PIC_MASTER_CMD) & irqmask;
Pavel Machek680afbf2008-05-21 11:57:52 +0200139 outb(0x0A, PIC_MASTER_CMD); /* back to the IRR register */
Pavel Machek21fd5132008-05-21 11:44:02 +0200140 return value;
141 }
Pavel Machek680afbf2008-05-21 11:57:52 +0200142 outb(0x0B, PIC_SLAVE_CMD); /* ISR register */
Pavel Machek21fd5132008-05-21 11:44:02 +0200143 value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
Pavel Machek680afbf2008-05-21 11:57:52 +0200144 outb(0x0A, PIC_SLAVE_CMD); /* back to the IRR register */
Pavel Machek21fd5132008-05-21 11:44:02 +0200145 return value;
146}
147
148/*
149 * Careful! The 8259A is a fragile beast, it pretty
150 * much _has_ to be done exactly like this (mask it
151 * first, _then_ send the EOI, and the order of EOI
152 * to the two 8259s is important!
153 */
154static void mask_and_ack_8259A(unsigned int irq)
155{
156 unsigned int irqmask = 1 << irq;
157 unsigned long flags;
158
Thomas Gleixner5619c282009-07-25 18:35:11 +0200159 raw_spin_lock_irqsave(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200160 /*
161 * Lightweight spurious IRQ detection. We do not want
162 * to overdo spurious IRQ handling - it's usually a sign
163 * of hardware problems, so we only do the checks we can
164 * do without slowing down good hardware unnecessarily.
165 *
166 * Note that IRQ7 and IRQ15 (the two spurious IRQs
167 * usually resulting from the 8259A-1|2 PICs) occur
168 * even if the IRQ is masked in the 8259A. Thus we
169 * can check spurious 8259A IRQs without doing the
170 * quite slow i8259A_irq_real() call for every IRQ.
171 * This does not cover 100% of spurious interrupts,
172 * but should be enough to warn the user that there
173 * is something bad going on ...
174 */
175 if (cached_irq_mask & irqmask)
176 goto spurious_8259A_irq;
177 cached_irq_mask |= irqmask;
178
179handle_real_irq:
180 if (irq & 8) {
181 inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
182 outb(cached_slave_mask, PIC_SLAVE_IMR);
Pavel Machek21fd5132008-05-21 11:44:02 +0200183 /* 'Specific EOI' to slave */
Pavel Machek3e8631d2008-05-21 11:52:52 +0200184 outb(0x60+(irq&7), PIC_SLAVE_CMD);
Pavel Machek21fd5132008-05-21 11:44:02 +0200185 /* 'Specific EOI' to master-IRQ2 */
Pavel Machek3e8631d2008-05-21 11:52:52 +0200186 outb(0x60+PIC_CASCADE_IR, PIC_MASTER_CMD);
Pavel Machek21fd5132008-05-21 11:44:02 +0200187 } else {
188 inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
189 outb(cached_master_mask, PIC_MASTER_IMR);
Pavel Machek3e8631d2008-05-21 11:52:52 +0200190 outb(0x60+irq, PIC_MASTER_CMD); /* 'Specific EOI to master */
Pavel Machek21fd5132008-05-21 11:44:02 +0200191 }
Thomas Gleixner5619c282009-07-25 18:35:11 +0200192 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200193 return;
194
195spurious_8259A_irq:
196 /*
197 * this is the slow path - should happen rarely.
198 */
199 if (i8259A_irq_real(irq))
200 /*
201 * oops, the IRQ _is_ in service according to the
202 * 8259A - not spurious, go handle it.
203 */
204 goto handle_real_irq;
205
206 {
207 static int spurious_irq_mask;
208 /*
209 * At this point we can be sure the IRQ is spurious,
210 * lets ACK and report it. [once per IRQ]
211 */
212 if (!(spurious_irq_mask & irqmask)) {
Pavel Machek21fd5132008-05-21 11:44:02 +0200213 printk(KERN_DEBUG
214 "spurious 8259A interrupt: IRQ%d.\n", irq);
Pavel Machek21fd5132008-05-21 11:44:02 +0200215 spurious_irq_mask |= irqmask;
216 }
217 atomic_inc(&irq_err_count);
218 /*
219 * Theoretically we do not have to handle this IRQ,
220 * but in Linux this does not cause problems and is
221 * simpler for us.
222 */
223 goto handle_real_irq;
224 }
225}
226
227static char irq_trigger[2];
228/**
229 * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
230 */
231static void restore_ELCR(char *trigger)
232{
233 outb(trigger[0], 0x4d0);
234 outb(trigger[1], 0x4d1);
235}
236
237static void save_ELCR(char *trigger)
238{
239 /* IRQ 0,1,2,8,13 are marked as reserved */
240 trigger[0] = inb(0x4d0) & 0xF8;
241 trigger[1] = inb(0x4d1) & 0xDE;
242}
243
244static int i8259A_resume(struct sys_device *dev)
245{
246 init_8259A(i8259A_auto_eoi);
247 restore_ELCR(irq_trigger);
248 return 0;
249}
250
251static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
252{
253 save_ELCR(irq_trigger);
254 return 0;
255}
256
257static int i8259A_shutdown(struct sys_device *dev)
258{
259 /* Put the i8259A into a quiescent state that
260 * the kernel initialization code can get it
261 * out of.
262 */
263 outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
264 outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
265 return 0;
266}
267
268static struct sysdev_class i8259_sysdev_class = {
269 .name = "i8259",
270 .suspend = i8259A_suspend,
271 .resume = i8259A_resume,
272 .shutdown = i8259A_shutdown,
273};
274
275static struct sys_device device_i8259A = {
276 .id = 0,
277 .cls = &i8259_sysdev_class,
278};
279
280static int __init i8259A_init_sysfs(void)
281{
282 int error = sysdev_class_register(&i8259_sysdev_class);
283 if (!error)
284 error = sysdev_register(&device_i8259A);
285 return error;
286}
287
288device_initcall(i8259A_init_sysfs);
289
Jacob Panb81bb372009-11-09 11:27:04 -0800290static void mask_8259A(void)
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700291{
292 unsigned long flags;
293
Thomas Gleixner5619c282009-07-25 18:35:11 +0200294 raw_spin_lock_irqsave(&i8259A_lock, flags);
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700295
296 outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
297 outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
298
Thomas Gleixner5619c282009-07-25 18:35:11 +0200299 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700300}
301
Jacob Panb81bb372009-11-09 11:27:04 -0800302static void unmask_8259A(void)
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700303{
304 unsigned long flags;
305
Thomas Gleixner5619c282009-07-25 18:35:11 +0200306 raw_spin_lock_irqsave(&i8259A_lock, flags);
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700307
308 outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
309 outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
310
Thomas Gleixner5619c282009-07-25 18:35:11 +0200311 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Suresh Siddhad94d93c2008-07-10 11:16:46 -0700312}
313
Jacob Panb81bb372009-11-09 11:27:04 -0800314static void init_8259A(int auto_eoi)
Pavel Machek21fd5132008-05-21 11:44:02 +0200315{
316 unsigned long flags;
317
318 i8259A_auto_eoi = auto_eoi;
319
Thomas Gleixner5619c282009-07-25 18:35:11 +0200320 raw_spin_lock_irqsave(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200321
322 outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
323 outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
324
325 /*
326 * outb_pic - this has to work on a wide range of PC hardware.
327 */
328 outb_pic(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
Pavel Machekc46e62f2008-05-28 12:42:57 +0200329
330 /* ICW2: 8259A-1 IR0-7 mapped to 0x30-0x37 on x86-64,
Jaswinder Singh Rajput7bafaf32009-01-04 16:33:52 +0530331 to 0x20-0x27 on i386 */
Pavel Machek21fd5132008-05-21 11:44:02 +0200332 outb_pic(IRQ0_VECTOR, PIC_MASTER_IMR);
Pavel Machekc46e62f2008-05-28 12:42:57 +0200333
Pavel Machek21fd5132008-05-21 11:44:02 +0200334 /* 8259A-1 (the master) has a slave on IR2 */
Pavel Machekc46e62f2008-05-28 12:42:57 +0200335 outb_pic(1U << PIC_CASCADE_IR, PIC_MASTER_IMR);
336
Pavel Machek21fd5132008-05-21 11:44:02 +0200337 if (auto_eoi) /* master does Auto EOI */
338 outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
339 else /* master expects normal EOI */
340 outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
341
342 outb_pic(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
Pavel Machekc46e62f2008-05-28 12:42:57 +0200343
344 /* ICW2: 8259A-2 IR0-7 mapped to IRQ8_VECTOR */
Pavel Machek21fd5132008-05-21 11:44:02 +0200345 outb_pic(IRQ8_VECTOR, PIC_SLAVE_IMR);
346 /* 8259A-2 is a slave on master's IR2 */
347 outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR);
348 /* (slave's support for AEOI in flat mode is to be investigated) */
349 outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
350
Pavel Machek21fd5132008-05-21 11:44:02 +0200351 if (auto_eoi)
352 /*
353 * In AEOI mode we just have to mask the interrupt
354 * when acking.
355 */
356 i8259A_chip.mask_ack = disable_8259A_irq;
357 else
358 i8259A_chip.mask_ack = mask_and_ack_8259A;
359
360 udelay(100); /* wait for 8259A to initialize */
361
362 outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
363 outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
364
Thomas Gleixner5619c282009-07-25 18:35:11 +0200365 raw_spin_unlock_irqrestore(&i8259A_lock, flags);
Pavel Machek21fd5132008-05-21 11:44:02 +0200366}
Jacob Panb81bb372009-11-09 11:27:04 -0800367
Jacob Panef354862009-11-09 11:24:14 -0800368/*
369 * make i8259 a driver so that we can select pic functions at run time. the goal
370 * is to make x86 binary compatible among pc compatible and non-pc compatible
371 * platforms, such as x86 MID.
372 */
373
Jacob Pan28a3c932010-02-23 02:03:31 -0800374static void legacy_pic_noop(void) { };
375static void legacy_pic_uint_noop(unsigned int unused) { };
376static void legacy_pic_int_noop(int unused) { };
Jacob Panef354862009-11-09 11:24:14 -0800377
378static struct irq_chip dummy_pic_chip = {
379 .name = "dummy pic",
380 .mask = legacy_pic_uint_noop,
381 .unmask = legacy_pic_uint_noop,
382 .disable = legacy_pic_uint_noop,
383 .mask_ack = legacy_pic_uint_noop,
384};
385static int legacy_pic_irq_pending_noop(unsigned int irq)
386{
387 return 0;
388}
389
390struct legacy_pic null_legacy_pic = {
391 .nr_legacy_irqs = 0,
392 .chip = &dummy_pic_chip,
393 .mask_all = legacy_pic_noop,
394 .restore_mask = legacy_pic_noop,
395 .init = legacy_pic_int_noop,
396 .irq_pending = legacy_pic_irq_pending_noop,
397 .make_irq = legacy_pic_uint_noop,
398};
399
400struct legacy_pic default_legacy_pic = {
401 .nr_legacy_irqs = NR_IRQS_LEGACY,
402 .chip = &i8259A_chip,
403 .mask_all = mask_8259A,
404 .restore_mask = unmask_8259A,
405 .init = init_8259A,
406 .irq_pending = i8259A_irq_pending,
407 .make_irq = make_8259A_irq,
408};
409
410struct legacy_pic *legacy_pic = &default_legacy_pic;