blob: b39c5b9aff463d5efcf41dda4fe2474b27b97052 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Code to handle x86 style IRQs plus some generic interrupt stuff.
3 *
4 * Copyright (C) 1992 Linus Torvalds
5 * Copyright (C) 1994, 1995, 1996, 1997, 1998 Ralf Baechle
6 * Copyright (C) 1999 SuSE GmbH (Philipp Rumpf, prumpf@tux.org)
7 * Copyright (C) 1999-2000 Grant Grundler
8 * Copyright (c) 2005 Matthew Wilcox
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/kernel_stat.h>
29#include <linux/seq_file.h>
30#include <linux/spinlock.h>
31#include <linux/types.h>
James Bottomleyc2ab64d2005-11-17 16:28:37 -050032#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Kyle McMartin1d4c4522005-11-17 16:27:44 -050034#include <asm/smp.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#undef PARISC_IRQ_CR16_COUNTS
37
Matthew Wilcoxbe577a52006-10-06 20:47:23 -060038extern irqreturn_t timer_interrupt(int, void *);
39extern irqreturn_t ipi_interrupt(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define EIEM_MASK(irq) (1UL<<(CPU_IRQ_MAX - irq))
42
43/* Bits in EIEM correlate with cpu_irq_action[].
44** Numbered *Big Endian*! (ie bit 0 is MSB)
45*/
46static volatile unsigned long cpu_eiem = 0;
47
James Bottomley70856892006-09-09 12:36:25 -070048/*
49** ack bitmap ... habitually set to 1, but reset to zero
50** between ->ack() and ->end() of the interrupt to prevent
51** re-interruption of a processing interrupt.
52*/
53static volatile unsigned long global_ack_eiem = ~0UL;
54/*
55** Local bitmap, same as above but for per-cpu interrupts
56*/
57static DEFINE_PER_CPU(unsigned long, local_ack_eiem) = ~0UL;
58
James Bottomleyd911aed2005-11-17 16:27:02 -050059static void cpu_disable_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 unsigned long eirr_bit = EIEM_MASK(irq);
62
63 cpu_eiem &= ~eirr_bit;
James Bottomleyd911aed2005-11-17 16:27:02 -050064 /* Do nothing on the other CPUs. If they get this interrupt,
65 * The & cpu_eiem in the do_cpu_irq_mask() ensures they won't
66 * handle it, and the set_eiem() at the bottom will ensure it
67 * then gets disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static void cpu_enable_irq(unsigned int irq)
71{
72 unsigned long eirr_bit = EIEM_MASK(irq);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 cpu_eiem |= eirr_bit;
James Bottomleyd911aed2005-11-17 16:27:02 -050075
James Bottomleyd911aed2005-11-17 16:27:02 -050076 /* This is just a simple NOP IPI. But what it does is cause
77 * all the other CPUs to do a set_eiem(cpu_eiem) at the end
78 * of the interrupt handler */
79 smp_send_all_nop();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static unsigned int cpu_startup_irq(unsigned int irq)
83{
84 cpu_enable_irq(irq);
85 return 0;
86}
87
88void no_ack_irq(unsigned int irq) { }
89void no_end_irq(unsigned int irq) { }
90
James Bottomley70856892006-09-09 12:36:25 -070091void cpu_ack_irq(unsigned int irq)
92{
93 unsigned long mask = EIEM_MASK(irq);
94 int cpu = smp_processor_id();
95
96 /* Clear in EIEM so we can no longer process */
97 if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
98 per_cpu(local_ack_eiem, cpu) &= ~mask;
99 else
100 global_ack_eiem &= ~mask;
101
102 /* disable the interrupt */
103 set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
104 /* and now ack it */
105 mtctl(mask, 23);
106}
107
108void cpu_end_irq(unsigned int irq)
109{
110 unsigned long mask = EIEM_MASK(irq);
111 int cpu = smp_processor_id();
112
113 /* set it in the eiems---it's no longer in process */
114 if (CHECK_IRQ_PER_CPU(irq_desc[irq].status))
115 per_cpu(local_ack_eiem, cpu) |= mask;
116 else
117 global_ack_eiem |= mask;
118
119 /* enable the interrupt */
120 set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
121}
122
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500123#ifdef CONFIG_SMP
124int cpu_check_affinity(unsigned int irq, cpumask_t *dest)
125{
126 int cpu_dest;
127
128 /* timer and ipi have to always be received on all CPUs */
James Bottomley70856892006-09-09 12:36:25 -0700129 if (CHECK_IRQ_PER_CPU(irq)) {
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500130 /* Bad linux design decision. The mask has already
131 * been set; we must reset it */
Ingo Molnara53da522006-06-29 02:24:38 -0700132 irq_desc[irq].affinity = CPU_MASK_ALL;
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500133 return -EINVAL;
134 }
135
136 /* whatever mask they set, we just allow one CPU */
137 cpu_dest = first_cpu(*dest);
138 *dest = cpumask_of_cpu(cpu_dest);
139
140 return 0;
141}
142
143static void cpu_set_affinity_irq(unsigned int irq, cpumask_t dest)
144{
145 if (cpu_check_affinity(irq, &dest))
146 return;
147
Ingo Molnara53da522006-06-29 02:24:38 -0700148 irq_desc[irq].affinity = dest;
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500149}
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct hw_interrupt_type cpu_interrupt_type = {
153 .typename = "CPU",
154 .startup = cpu_startup_irq,
155 .shutdown = cpu_disable_irq,
156 .enable = cpu_enable_irq,
157 .disable = cpu_disable_irq,
James Bottomley70856892006-09-09 12:36:25 -0700158 .ack = cpu_ack_irq,
159 .end = cpu_end_irq,
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500160#ifdef CONFIG_SMP
161 .set_affinity = cpu_set_affinity_irq,
162#endif
Ingo Molnarc0ad90a2006-06-29 02:24:44 -0700163 /* XXX: Needs to be written. We managed without it so far, but
164 * we really ought to write it.
165 */
166 .retrigger = NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169int show_interrupts(struct seq_file *p, void *v)
170{
171 int i = *(loff_t *) v, j;
172 unsigned long flags;
173
174 if (i == 0) {
175 seq_puts(p, " ");
176 for_each_online_cpu(j)
177 seq_printf(p, " CPU%d", j);
178
179#ifdef PARISC_IRQ_CR16_COUNTS
180 seq_printf(p, " [min/avg/max] (CPU cycle counts)");
181#endif
182 seq_putc(p, '\n');
183 }
184
185 if (i < NR_IRQS) {
186 struct irqaction *action;
187
188 spin_lock_irqsave(&irq_desc[i].lock, flags);
189 action = irq_desc[i].action;
190 if (!action)
191 goto skip;
192 seq_printf(p, "%3d: ", i);
193#ifdef CONFIG_SMP
194 for_each_online_cpu(j)
195 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
196#else
197 seq_printf(p, "%10u ", kstat_irqs(i));
198#endif
199
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700200 seq_printf(p, " %14s", irq_desc[i].chip->typename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifndef PARISC_IRQ_CR16_COUNTS
202 seq_printf(p, " %s", action->name);
203
204 while ((action = action->next))
205 seq_printf(p, ", %s", action->name);
206#else
207 for ( ;action; action = action->next) {
208 unsigned int k, avg, min, max;
209
210 min = max = action->cr16_hist[0];
211
212 for (avg = k = 0; k < PARISC_CR16_HIST_SIZE; k++) {
213 int hist = action->cr16_hist[k];
214
215 if (hist) {
216 avg += hist;
217 } else
218 break;
219
220 if (hist > max) max = hist;
221 if (hist < min) min = hist;
222 }
223
224 avg /= k;
225 seq_printf(p, " %s[%d/%d/%d]", action->name,
226 min,avg,max);
227 }
228#endif
229
230 seq_putc(p, '\n');
231 skip:
232 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
233 }
234
235 return 0;
236}
237
238
239
240/*
241** The following form a "set": Virtual IRQ, Transaction Address, Trans Data.
242** Respectively, these map to IRQ region+EIRR, Processor HPA, EIRR bit.
243**
244** To use txn_XXX() interfaces, get a Virtual IRQ first.
245** Then use that to get the Transaction address and data.
246*/
247
Kyle McMartin5cfe87d2006-08-13 22:25:45 -0400248int cpu_claim_irq(unsigned int irq, struct irq_chip *type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 if (irq_desc[irq].action)
251 return -EBUSY;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700252 if (irq_desc[irq].chip != &cpu_interrupt_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return -EBUSY;
254
255 if (type) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700256 irq_desc[irq].chip = type;
257 irq_desc[irq].chip_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 cpu_interrupt_type.enable(irq);
259 }
260 return 0;
261}
262
263int txn_claim_irq(int irq)
264{
265 return cpu_claim_irq(irq, NULL, NULL) ? -1 : irq;
266}
267
268/*
269 * The bits_wide parameter accommodates the limitations of the HW/SW which
270 * use these bits:
271 * Legacy PA I/O (GSC/NIO): 5 bits (architected EIM register)
272 * V-class (EPIC): 6 bits
273 * N/L/A-class (iosapic): 8 bits
274 * PCI 2.2 MSI: 16 bits
275 * Some PCI devices: 32 bits (Symbios SCSI/ATM/HyperFabric)
276 *
277 * On the service provider side:
278 * o PA 1.1 (and PA2.0 narrow mode) 5-bits (width of EIR register)
279 * o PA 2.0 wide mode 6-bits (per processor)
280 * o IA64 8-bits (0-256 total)
281 *
282 * So a Legacy PA I/O device on a PA 2.0 box can't use all the bits supported
283 * by the processor...and the N/L-class I/O subsystem supports more bits than
284 * PA2.0 has. The first case is the problem.
285 */
286int txn_alloc_irq(unsigned int bits_wide)
287{
288 int irq;
289
290 /* never return irq 0 cause that's the interval timer */
291 for (irq = CPU_IRQ_BASE + 1; irq <= CPU_IRQ_MAX; irq++) {
292 if (cpu_claim_irq(irq, NULL, NULL) < 0)
293 continue;
294 if ((irq - CPU_IRQ_BASE) >= (1 << bits_wide))
295 continue;
296 return irq;
297 }
298
299 /* unlikely, but be prepared */
300 return -1;
301}
302
Grant Grundler03afe222005-11-17 16:29:16 -0500303
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500304unsigned long txn_affinity_addr(unsigned int irq, int cpu)
305{
Grant Grundler03afe222005-11-17 16:29:16 -0500306#ifdef CONFIG_SMP
Ingo Molnara53da522006-06-29 02:24:38 -0700307 irq_desc[irq].affinity = cpumask_of_cpu(cpu);
Grant Grundler03afe222005-11-17 16:29:16 -0500308#endif
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500309
310 return cpu_data[cpu].txn_addr;
311}
312
Grant Grundler03afe222005-11-17 16:29:16 -0500313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314unsigned long txn_alloc_addr(unsigned int virt_irq)
315{
316 static int next_cpu = -1;
317
318 next_cpu++; /* assign to "next" CPU we want this bugger on */
319
320 /* validate entry */
321 while ((next_cpu < NR_CPUS) && (!cpu_data[next_cpu].txn_addr ||
322 !cpu_online(next_cpu)))
323 next_cpu++;
324
325 if (next_cpu >= NR_CPUS)
326 next_cpu = 0; /* nothing else, assign monarch */
327
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500328 return txn_affinity_addr(virt_irq, next_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331
332unsigned int txn_alloc_data(unsigned int virt_irq)
333{
334 return virt_irq - CPU_IRQ_BASE;
335}
336
James Bottomley70856892006-09-09 12:36:25 -0700337static inline int eirr_to_irq(unsigned long eirr)
338{
339#ifdef CONFIG_64BIT
340 int bit = fls64(eirr);
341#else
342 int bit = fls(eirr);
343#endif
344 return (BITS_PER_LONG - bit) + TIMER_IRQ;
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347/* ONLY called from entry.S:intr_extint() */
348void do_cpu_irq_mask(struct pt_regs *regs)
349{
Matthew Wilcoxe11e30a2006-10-07 05:11:07 -0600350 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 unsigned long eirr_val;
James Bottomley70856892006-09-09 12:36:25 -0700352 int irq, cpu = smp_processor_id();
353#ifdef CONFIG_SMP
354 cpumask_t dest;
355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Matthew Wilcoxe11e30a2006-10-07 05:11:07 -0600357 old_regs = set_irq_regs(regs);
James Bottomley70856892006-09-09 12:36:25 -0700358 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 irq_enter();
360
James Bottomley70856892006-09-09 12:36:25 -0700361 eirr_val = mfctl(23) & cpu_eiem & global_ack_eiem &
362 per_cpu(local_ack_eiem, cpu);
363 if (!eirr_val)
364 goto set_out;
365 irq = eirr_to_irq(eirr_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Grant Grundler03afe222005-11-17 16:29:16 -0500367#ifdef CONFIG_SMP
James Bottomley70856892006-09-09 12:36:25 -0700368 dest = irq_desc[irq].affinity;
369 if (CHECK_IRQ_PER_CPU(irq_desc[irq].status) &&
370 !cpu_isset(smp_processor_id(), dest)) {
371 int cpu = first_cpu(dest);
James Bottomleyc2ab64d2005-11-17 16:28:37 -0500372
James Bottomley70856892006-09-09 12:36:25 -0700373 printk(KERN_DEBUG "redirecting irq %d from CPU %d to %d\n",
374 irq, smp_processor_id(), cpu);
375 gsc_writel(irq + CPU_IRQ_BASE,
376 cpu_data[cpu].hpa);
377 goto set_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
James Bottomley70856892006-09-09 12:36:25 -0700379#endif
Matthew Wilcoxbe577a52006-10-06 20:47:23 -0600380 __do_IRQ(irq);
Grant Grundler3f902882005-11-17 16:26:20 -0500381
James Bottomley70856892006-09-09 12:36:25 -0700382 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 irq_exit();
Matthew Wilcoxe11e30a2006-10-07 05:11:07 -0600384 set_irq_regs(old_regs);
James Bottomley70856892006-09-09 12:36:25 -0700385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
James Bottomley70856892006-09-09 12:36:25 -0700387 set_out:
388 set_eiem(cpu_eiem & global_ack_eiem & per_cpu(local_ack_eiem, cpu));
389 goto out;
390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392static struct irqaction timer_action = {
393 .handler = timer_interrupt,
394 .name = "timer",
James Bottomley70856892006-09-09 12:36:25 -0700395 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_PERCPU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
398#ifdef CONFIG_SMP
399static struct irqaction ipi_action = {
400 .handler = ipi_interrupt,
401 .name = "IPI",
James Bottomley70856892006-09-09 12:36:25 -0700402 .flags = IRQF_DISABLED | IRQF_PERCPU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404#endif
405
406static void claim_cpu_irqs(void)
407{
408 int i;
409 for (i = CPU_IRQ_BASE; i <= CPU_IRQ_MAX; i++) {
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700410 irq_desc[i].chip = &cpu_interrupt_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 irq_desc[TIMER_IRQ].action = &timer_action;
414 irq_desc[TIMER_IRQ].status |= IRQ_PER_CPU;
415#ifdef CONFIG_SMP
416 irq_desc[IPI_IRQ].action = &ipi_action;
417 irq_desc[IPI_IRQ].status = IRQ_PER_CPU;
418#endif
419}
420
421void __init init_IRQ(void)
422{
423 local_irq_disable(); /* PARANOID - should already be disabled */
424 mtctl(~0UL, 23); /* EIRR : clear all pending external intr */
425 claim_cpu_irqs();
426#ifdef CONFIG_SMP
427 if (!cpu_eiem)
428 cpu_eiem = EIEM_MASK(IPI_IRQ) | EIEM_MASK(TIMER_IRQ);
429#else
430 cpu_eiem = EIEM_MASK(TIMER_IRQ);
431#endif
432 set_eiem(cpu_eiem); /* EIEM : enable all external intr */
433
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436void ack_bad_irq(unsigned int irq)
437{
438 printk("unexpected IRQ %d\n", irq);
439}