Blame view

arch/x86/kernel/i8259.c 10.1 KB
21fd5132b   Pavel Machek   x86: automatical ...
1
  #include <linux/linkage.h>
21fd5132b   Pavel Machek   x86: automatical ...
2
3
4
5
6
  #include <linux/errno.h>
  #include <linux/signal.h>
  #include <linux/sched.h>
  #include <linux/ioport.h>
  #include <linux/interrupt.h>
21fd5132b   Pavel Machek   x86: automatical ...
7
  #include <linux/timex.h>
21fd5132b   Pavel Machek   x86: automatical ...
8
9
10
  #include <linux/random.h>
  #include <linux/init.h>
  #include <linux/kernel_stat.h>
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
11
  #include <linux/syscore_ops.h>
21fd5132b   Pavel Machek   x86: automatical ...
12
  #include <linux/bitops.h>
7bafaf306   Jaswinder Singh Rajput   x86: i8259.c fix ...
13
14
15
  #include <linux/acpi.h>
  #include <linux/io.h>
  #include <linux/delay.h>
21fd5132b   Pavel Machek   x86: automatical ...
16

60063497a   Arun Sharma   atomic: use <linu...
17
  #include <linux/atomic.h>
21fd5132b   Pavel Machek   x86: automatical ...
18
  #include <asm/system.h>
21fd5132b   Pavel Machek   x86: automatical ...
19
  #include <asm/timer.h>
21fd5132b   Pavel Machek   x86: automatical ...
20
  #include <asm/hw_irq.h>
21fd5132b   Pavel Machek   x86: automatical ...
21
  #include <asm/pgtable.h>
21fd5132b   Pavel Machek   x86: automatical ...
22
23
  #include <asm/desc.h>
  #include <asm/apic.h>
21fd5132b   Pavel Machek   x86: automatical ...
24
25
26
27
28
29
30
31
  #include <asm/i8259.h>
  
  /*
   * This is the 'legacy' 8259A Programmable Interrupt Controller,
   * present in the majority of PC/AT boxes.
   * plus some generic x86 specific things if generic specifics makes
   * any sense at all.
   */
4305df947   Thomas Gleixner   x86: i8259: Conve...
32
  static void init_8259A(int auto_eoi);
21fd5132b   Pavel Machek   x86: automatical ...
33
34
  
  static int i8259A_auto_eoi;
5619c2806   Thomas Gleixner   x86: Convert i825...
35
  DEFINE_RAW_SPINLOCK(i8259A_lock);
21fd5132b   Pavel Machek   x86: automatical ...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  
  /*
   * 8259A PIC functions to handle ISA devices:
   */
  
  /*
   * This contains the irq mask for both 8259A irq controllers,
   */
  unsigned int cached_irq_mask = 0xffff;
  
  /*
   * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
   * boards the timer interrupt is not really connected to any IO-APIC pin,
   * it's fed to the master 8259A's IR0 line only.
   *
   * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
   * this 'mixed mode' IRQ handling costs nothing because it's only used
   * at IRQ setup time.
   */
  unsigned long io_apic_irqs;
4305df947   Thomas Gleixner   x86: i8259: Conve...
56
  static void mask_8259A_irq(unsigned int irq)
21fd5132b   Pavel Machek   x86: automatical ...
57
58
59
  {
  	unsigned int mask = 1 << irq;
  	unsigned long flags;
5619c2806   Thomas Gleixner   x86: Convert i825...
60
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
61
62
63
64
65
  	cached_irq_mask |= mask;
  	if (irq & 8)
  		outb(cached_slave_mask, PIC_SLAVE_IMR);
  	else
  		outb(cached_master_mask, PIC_MASTER_IMR);
5619c2806   Thomas Gleixner   x86: Convert i825...
66
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
67
  }
4305df947   Thomas Gleixner   x86: i8259: Conve...
68
69
70
71
72
73
  static void disable_8259A_irq(struct irq_data *data)
  {
  	mask_8259A_irq(data->irq);
  }
  
  static void unmask_8259A_irq(unsigned int irq)
21fd5132b   Pavel Machek   x86: automatical ...
74
75
76
  {
  	unsigned int mask = ~(1 << irq);
  	unsigned long flags;
5619c2806   Thomas Gleixner   x86: Convert i825...
77
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
78
79
80
81
82
  	cached_irq_mask &= mask;
  	if (irq & 8)
  		outb(cached_slave_mask, PIC_SLAVE_IMR);
  	else
  		outb(cached_master_mask, PIC_MASTER_IMR);
5619c2806   Thomas Gleixner   x86: Convert i825...
83
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
84
  }
4305df947   Thomas Gleixner   x86: i8259: Conve...
85
86
87
88
  static void enable_8259A_irq(struct irq_data *data)
  {
  	unmask_8259A_irq(data->irq);
  }
b81bb373a   Jacob Pan   x86, pic: Make us...
89
  static int i8259A_irq_pending(unsigned int irq)
21fd5132b   Pavel Machek   x86: automatical ...
90
91
92
93
  {
  	unsigned int mask = 1<<irq;
  	unsigned long flags;
  	int ret;
5619c2806   Thomas Gleixner   x86: Convert i825...
94
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
95
96
97
98
  	if (irq < 8)
  		ret = inb(PIC_MASTER_CMD) & mask;
  	else
  		ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
5619c2806   Thomas Gleixner   x86: Convert i825...
99
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
100
101
102
  
  	return ret;
  }
b81bb373a   Jacob Pan   x86, pic: Make us...
103
  static void make_8259A_irq(unsigned int irq)
21fd5132b   Pavel Machek   x86: automatical ...
104
105
106
  {
  	disable_irq_nosync(irq);
  	io_apic_irqs &= ~(1<<irq);
2c778651f   Thomas Gleixner   x86: Cleanup the ...
107
  	irq_set_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
4305df947   Thomas Gleixner   x86: i8259: Conve...
108
  				      i8259A_chip.name);
21fd5132b   Pavel Machek   x86: automatical ...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  	enable_irq(irq);
  }
  
  /*
   * This function assumes to be called rarely. Switching between
   * 8259A registers is slow.
   * This has to be protected by the irq controller spinlock
   * before being called.
   */
  static inline int i8259A_irq_real(unsigned int irq)
  {
  	int value;
  	int irqmask = 1<<irq;
  
  	if (irq < 8) {
680afbf98   Pavel Machek   x86: i8259: clean...
124
  		outb(0x0B, PIC_MASTER_CMD);	/* ISR register */
21fd5132b   Pavel Machek   x86: automatical ...
125
  		value = inb(PIC_MASTER_CMD) & irqmask;
680afbf98   Pavel Machek   x86: i8259: clean...
126
  		outb(0x0A, PIC_MASTER_CMD);	/* back to the IRR register */
21fd5132b   Pavel Machek   x86: automatical ...
127
128
  		return value;
  	}
680afbf98   Pavel Machek   x86: i8259: clean...
129
  	outb(0x0B, PIC_SLAVE_CMD);	/* ISR register */
21fd5132b   Pavel Machek   x86: automatical ...
130
  	value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
680afbf98   Pavel Machek   x86: i8259: clean...
131
  	outb(0x0A, PIC_SLAVE_CMD);	/* back to the IRR register */
21fd5132b   Pavel Machek   x86: automatical ...
132
133
134
135
136
137
138
139
140
  	return value;
  }
  
  /*
   * Careful! The 8259A is a fragile beast, it pretty
   * much _has_ to be done exactly like this (mask it
   * first, _then_ send the EOI, and the order of EOI
   * to the two 8259s is important!
   */
4305df947   Thomas Gleixner   x86: i8259: Conve...
141
  static void mask_and_ack_8259A(struct irq_data *data)
21fd5132b   Pavel Machek   x86: automatical ...
142
  {
4305df947   Thomas Gleixner   x86: i8259: Conve...
143
  	unsigned int irq = data->irq;
21fd5132b   Pavel Machek   x86: automatical ...
144
145
  	unsigned int irqmask = 1 << irq;
  	unsigned long flags;
5619c2806   Thomas Gleixner   x86: Convert i825...
146
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  	/*
  	 * Lightweight spurious IRQ detection. We do not want
  	 * to overdo spurious IRQ handling - it's usually a sign
  	 * of hardware problems, so we only do the checks we can
  	 * do without slowing down good hardware unnecessarily.
  	 *
  	 * Note that IRQ7 and IRQ15 (the two spurious IRQs
  	 * usually resulting from the 8259A-1|2 PICs) occur
  	 * even if the IRQ is masked in the 8259A. Thus we
  	 * can check spurious 8259A IRQs without doing the
  	 * quite slow i8259A_irq_real() call for every IRQ.
  	 * This does not cover 100% of spurious interrupts,
  	 * but should be enough to warn the user that there
  	 * is something bad going on ...
  	 */
  	if (cached_irq_mask & irqmask)
  		goto spurious_8259A_irq;
  	cached_irq_mask |= irqmask;
  
  handle_real_irq:
  	if (irq & 8) {
  		inb(PIC_SLAVE_IMR);	/* DUMMY - (do we need this?) */
  		outb(cached_slave_mask, PIC_SLAVE_IMR);
21fd5132b   Pavel Machek   x86: automatical ...
170
  		/* 'Specific EOI' to slave */
3e8631d27   Pavel Machek   x86: i8259.c: rem...
171
  		outb(0x60+(irq&7), PIC_SLAVE_CMD);
21fd5132b   Pavel Machek   x86: automatical ...
172
  		 /* 'Specific EOI' to master-IRQ2 */
3e8631d27   Pavel Machek   x86: i8259.c: rem...
173
  		outb(0x60+PIC_CASCADE_IR, PIC_MASTER_CMD);
21fd5132b   Pavel Machek   x86: automatical ...
174
175
176
  	} else {
  		inb(PIC_MASTER_IMR);	/* DUMMY - (do we need this?) */
  		outb(cached_master_mask, PIC_MASTER_IMR);
3e8631d27   Pavel Machek   x86: i8259.c: rem...
177
  		outb(0x60+irq, PIC_MASTER_CMD);	/* 'Specific EOI to master */
21fd5132b   Pavel Machek   x86: automatical ...
178
  	}
5619c2806   Thomas Gleixner   x86: Convert i825...
179
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  	return;
  
  spurious_8259A_irq:
  	/*
  	 * this is the slow path - should happen rarely.
  	 */
  	if (i8259A_irq_real(irq))
  		/*
  		 * oops, the IRQ _is_ in service according to the
  		 * 8259A - not spurious, go handle it.
  		 */
  		goto handle_real_irq;
  
  	{
  		static int spurious_irq_mask;
  		/*
  		 * At this point we can be sure the IRQ is spurious,
  		 * lets ACK and report it. [once per IRQ]
  		 */
  		if (!(spurious_irq_mask & irqmask)) {
21fd5132b   Pavel Machek   x86: automatical ...
200
201
202
  			printk(KERN_DEBUG
  			       "spurious 8259A interrupt: IRQ%d.
  ", irq);
21fd5132b   Pavel Machek   x86: automatical ...
203
204
205
206
207
208
209
210
211
212
213
  			spurious_irq_mask |= irqmask;
  		}
  		atomic_inc(&irq_err_count);
  		/*
  		 * Theoretically we do not have to handle this IRQ,
  		 * but in Linux this does not cause problems and is
  		 * simpler for us.
  		 */
  		goto handle_real_irq;
  	}
  }
4305df947   Thomas Gleixner   x86: i8259: Conve...
214
215
216
217
218
219
220
  struct irq_chip i8259A_chip = {
  	.name		= "XT-PIC",
  	.irq_mask	= disable_8259A_irq,
  	.irq_disable	= disable_8259A_irq,
  	.irq_unmask	= enable_8259A_irq,
  	.irq_mask_ack	= mask_and_ack_8259A,
  };
21fd5132b   Pavel Machek   x86: automatical ...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  static char irq_trigger[2];
  /**
   * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
   */
  static void restore_ELCR(char *trigger)
  {
  	outb(trigger[0], 0x4d0);
  	outb(trigger[1], 0x4d1);
  }
  
  static void save_ELCR(char *trigger)
  {
  	/* IRQ 0,1,2,8,13 are marked as reserved */
  	trigger[0] = inb(0x4d0) & 0xF8;
  	trigger[1] = inb(0x4d1) & 0xDE;
  }
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
237
  static void i8259A_resume(void)
21fd5132b   Pavel Machek   x86: automatical ...
238
239
240
  {
  	init_8259A(i8259A_auto_eoi);
  	restore_ELCR(irq_trigger);
21fd5132b   Pavel Machek   x86: automatical ...
241
  }
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
242
  static int i8259A_suspend(void)
21fd5132b   Pavel Machek   x86: automatical ...
243
244
245
246
  {
  	save_ELCR(irq_trigger);
  	return 0;
  }
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
247
  static void i8259A_shutdown(void)
21fd5132b   Pavel Machek   x86: automatical ...
248
249
250
251
252
253
254
  {
  	/* Put the i8259A into a quiescent state that
  	 * the kernel initialization code can get it
  	 * out of.
  	 */
  	outb(0xff, PIC_MASTER_IMR);	/* mask all of 8259A-1 */
  	outb(0xff, PIC_SLAVE_IMR);	/* mask all of 8259A-1 */
21fd5132b   Pavel Machek   x86: automatical ...
255
  }
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
256
  static struct syscore_ops i8259_syscore_ops = {
21fd5132b   Pavel Machek   x86: automatical ...
257
258
259
260
  	.suspend = i8259A_suspend,
  	.resume = i8259A_resume,
  	.shutdown = i8259A_shutdown,
  };
b81bb373a   Jacob Pan   x86, pic: Make us...
261
  static void mask_8259A(void)
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
262
263
  {
  	unsigned long flags;
5619c2806   Thomas Gleixner   x86: Convert i825...
264
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
265
266
267
  
  	outb(0xff, PIC_MASTER_IMR);	/* mask all of 8259A-1 */
  	outb(0xff, PIC_SLAVE_IMR);	/* mask all of 8259A-2 */
5619c2806   Thomas Gleixner   x86: Convert i825...
268
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
269
  }
b81bb373a   Jacob Pan   x86, pic: Make us...
270
  static void unmask_8259A(void)
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
271
272
  {
  	unsigned long flags;
5619c2806   Thomas Gleixner   x86: Convert i825...
273
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
274
275
276
  
  	outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  	outb(cached_slave_mask, PIC_SLAVE_IMR);	  /* restore slave IRQ mask */
5619c2806   Thomas Gleixner   x86: Convert i825...
277
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
d94d93ca5   Suresh Siddha   x64, x2apic/intr-...
278
  }
b81bb373a   Jacob Pan   x86, pic: Make us...
279
  static void init_8259A(int auto_eoi)
21fd5132b   Pavel Machek   x86: automatical ...
280
281
282
283
  {
  	unsigned long flags;
  
  	i8259A_auto_eoi = auto_eoi;
5619c2806   Thomas Gleixner   x86: Convert i825...
284
  	raw_spin_lock_irqsave(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
285
286
287
288
289
290
291
292
  
  	outb(0xff, PIC_MASTER_IMR);	/* mask all of 8259A-1 */
  	outb(0xff, PIC_SLAVE_IMR);	/* mask all of 8259A-2 */
  
  	/*
  	 * outb_pic - this has to work on a wide range of PC hardware.
  	 */
  	outb_pic(0x11, PIC_MASTER_CMD);	/* ICW1: select 8259A-1 init */
c46e62f73   Pavel Machek   i8259: fix final ...
293
294
  
  	/* ICW2: 8259A-1 IR0-7 mapped to 0x30-0x37 on x86-64,
7bafaf306   Jaswinder Singh Rajput   x86: i8259.c fix ...
295
  	   to 0x20-0x27 on i386 */
21fd5132b   Pavel Machek   x86: automatical ...
296
  	outb_pic(IRQ0_VECTOR, PIC_MASTER_IMR);
c46e62f73   Pavel Machek   i8259: fix final ...
297

21fd5132b   Pavel Machek   x86: automatical ...
298
  	/* 8259A-1 (the master) has a slave on IR2 */
c46e62f73   Pavel Machek   i8259: fix final ...
299
  	outb_pic(1U << PIC_CASCADE_IR, PIC_MASTER_IMR);
21fd5132b   Pavel Machek   x86: automatical ...
300
301
302
303
304
305
  	if (auto_eoi)	/* master does Auto EOI */
  		outb_pic(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  	else		/* master expects normal EOI */
  		outb_pic(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  
  	outb_pic(0x11, PIC_SLAVE_CMD);	/* ICW1: select 8259A-2 init */
c46e62f73   Pavel Machek   i8259: fix final ...
306
307
  
  	/* ICW2: 8259A-2 IR0-7 mapped to IRQ8_VECTOR */
21fd5132b   Pavel Machek   x86: automatical ...
308
309
310
311
312
  	outb_pic(IRQ8_VECTOR, PIC_SLAVE_IMR);
  	/* 8259A-2 is a slave on master's IR2 */
  	outb_pic(PIC_CASCADE_IR, PIC_SLAVE_IMR);
  	/* (slave's support for AEOI in flat mode is to be investigated) */
  	outb_pic(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR);
21fd5132b   Pavel Machek   x86: automatical ...
313
314
315
316
317
  	if (auto_eoi)
  		/*
  		 * In AEOI mode we just have to mask the interrupt
  		 * when acking.
  		 */
4305df947   Thomas Gleixner   x86: i8259: Conve...
318
  		i8259A_chip.irq_mask_ack = disable_8259A_irq;
21fd5132b   Pavel Machek   x86: automatical ...
319
  	else
4305df947   Thomas Gleixner   x86: i8259: Conve...
320
  		i8259A_chip.irq_mask_ack = mask_and_ack_8259A;
21fd5132b   Pavel Machek   x86: automatical ...
321
322
323
324
325
  
  	udelay(100);		/* wait for 8259A to initialize */
  
  	outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  	outb(cached_slave_mask, PIC_SLAVE_IMR);	  /* restore slave IRQ mask */
5619c2806   Thomas Gleixner   x86: Convert i825...
326
  	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
21fd5132b   Pavel Machek   x86: automatical ...
327
  }
b81bb373a   Jacob Pan   x86, pic: Make us...
328

ef3548668   Jacob Pan   x86, pic: Introdu...
329
330
331
332
333
  /*
   * make i8259 a driver so that we can select pic functions at run time. the goal
   * is to make x86 binary compatible among pc compatible and non-pc compatible
   * platforms, such as x86 MID.
   */
28a3c93d1   Jacob Pan   x86, pic: Fix sec...
334
335
336
  static void legacy_pic_noop(void) { };
  static void legacy_pic_uint_noop(unsigned int unused) { };
  static void legacy_pic_int_noop(int unused) { };
ef3548668   Jacob Pan   x86, pic: Introdu...
337
338
339
340
341
342
343
  static int legacy_pic_irq_pending_noop(unsigned int irq)
  {
  	return 0;
  }
  
  struct legacy_pic null_legacy_pic = {
  	.nr_legacy_irqs = 0,
4305df947   Thomas Gleixner   x86: i8259: Conve...
344
345
346
  	.chip = &dummy_irq_chip,
  	.mask = legacy_pic_uint_noop,
  	.unmask = legacy_pic_uint_noop,
ef3548668   Jacob Pan   x86, pic: Introdu...
347
348
349
350
351
352
353
354
355
356
  	.mask_all = legacy_pic_noop,
  	.restore_mask = legacy_pic_noop,
  	.init = legacy_pic_int_noop,
  	.irq_pending = legacy_pic_irq_pending_noop,
  	.make_irq = legacy_pic_uint_noop,
  };
  
  struct legacy_pic default_legacy_pic = {
  	.nr_legacy_irqs = NR_IRQS_LEGACY,
  	.chip  = &i8259A_chip,
4305df947   Thomas Gleixner   x86: i8259: Conve...
357
358
359
  	.mask = mask_8259A_irq,
  	.unmask = unmask_8259A_irq,
  	.mask_all = mask_8259A,
ef3548668   Jacob Pan   x86, pic: Introdu...
360
361
362
363
364
365
366
  	.restore_mask = unmask_8259A,
  	.init = init_8259A,
  	.irq_pending = i8259A_irq_pending,
  	.make_irq = make_8259A_irq,
  };
  
  struct legacy_pic *legacy_pic = &default_legacy_pic;
087b255a2   Adam Lackorzynski   x86, i8259: Only ...
367

f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
368
  static int __init i8259A_init_ops(void)
087b255a2   Adam Lackorzynski   x86, i8259: Only ...
369
  {
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
370
371
  	if (legacy_pic == &default_legacy_pic)
  		register_syscore_ops(&i8259_syscore_ops);
087b255a2   Adam Lackorzynski   x86, i8259: Only ...
372

f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
373
  	return 0;
087b255a2   Adam Lackorzynski   x86, i8259: Only ...
374
  }
f3c6ea1b0   Rafael J. Wysocki   x86: Use syscore_...
375
  device_initcall(i8259A_init_ops);