blob: 51f65054bf18fb794d2920f62556fa9fdda6e7fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
2 * irq.c: UltraSparc IRQ handling/init/registry.
3 *
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
7 */
8
9#include <linux/config.h>
10#include <linux/module.h>
11#include <linux/sched.h>
12#include <linux/ptrace.h>
13#include <linux/errno.h>
14#include <linux/kernel_stat.h>
15#include <linux/signal.h>
16#include <linux/mm.h>
17#include <linux/interrupt.h>
18#include <linux/slab.h>
19#include <linux/random.h>
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/proc_fs.h>
23#include <linux/seq_file.h>
David S. Millerb5a37e92006-02-11 23:07:13 -080024#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/ptrace.h>
27#include <asm/processor.h>
28#include <asm/atomic.h>
29#include <asm/system.h>
30#include <asm/irq.h>
Sven Hartge2e457ef2005-10-08 21:12:04 -070031#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/sbus.h>
33#include <asm/iommu.h>
34#include <asm/upa.h>
35#include <asm/oplib.h>
36#include <asm/timer.h>
37#include <asm/smp.h>
38#include <asm/starfire.h>
39#include <asm/uaccess.h>
40#include <asm/cache.h>
41#include <asm/cpudata.h>
David S. Miller63b61452005-06-27 17:04:45 -070042#include <asm/auxio.h>
David S. Miller92704a12006-02-26 23:27:19 -080043#include <asm/head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#ifdef CONFIG_SMP
46static void distribute_irqs(void);
47#endif
48
49/* UPA nodes send interrupt packet to UltraSparc with first data reg
50 * value low 5 (7 on Starfire) bits holding the IRQ identifier being
51 * delivered. We must translate this into a non-vector IRQ so we can
52 * set the softint on this cpu.
53 *
54 * To make processing these packets efficient and race free we use
55 * an array of irq buckets below. The interrupt vector handler in
56 * entry.S feeds incoming packets into per-cpu pil-indexed lists.
57 * The IVEC handler does not need to act atomically, the PIL dispatch
58 * code uses CAS to get an atomic snapshot of the list and clear it
59 * at the same time.
60 */
61
62struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
63
64/* This has to be in the main kernel image, it cannot be
65 * turned into per-cpu data. The reason is that the main
66 * kernel image is locked into the TLB and this structure
67 * is accessed from the vectored interrupt trap handler. If
68 * access to this structure takes a TLB miss it could cause
69 * the 5-level sparc v9 trap stack to overflow.
70 */
71struct irq_work_struct {
72 unsigned int irq_worklists[16];
73};
74struct irq_work_struct __irq_work[NR_CPUS];
75#define irq_work(__cpu, __pil) &(__irq_work[(__cpu)].irq_worklists[(__pil)])
76
David S. Miller088dd1f2005-07-04 13:24:38 -070077static struct irqaction *irq_action[NR_IRQS+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* This only synchronizes entities which modify IRQ handler
80 * state and some selected user-level spots that want to
81 * read things in the table. IRQ handler processing orders
82 * its' accesses such that no locking is needed.
83 */
84static DEFINE_SPINLOCK(irq_action_lock);
85
86static void register_irq_proc (unsigned int irq);
87
88/*
89 * Upper 2b of irqaction->flags holds the ino.
90 * irqaction->mask holds the smp affinity information.
91 */
92#define put_ino_in_irqaction(action, irq) \
93 action->flags &= 0xffffffffffffUL; \
94 if (__bucket(irq) == &pil0_dummy_bucket) \
95 action->flags |= 0xdeadUL << 48; \
96 else \
97 action->flags |= __irq_ino(irq) << 48;
98#define get_ino_in_irqaction(action) (action->flags >> 48)
99
100#define put_smpaff_in_irqaction(action, smpaff) (action)->mask = (smpaff)
101#define get_smpaff_in_irqaction(action) ((action)->mask)
102
103int show_interrupts(struct seq_file *p, void *v)
104{
105 unsigned long flags;
106 int i = *(loff_t *) v;
107 struct irqaction *action;
108#ifdef CONFIG_SMP
109 int j;
110#endif
111
112 spin_lock_irqsave(&irq_action_lock, flags);
113 if (i <= NR_IRQS) {
114 if (!(action = *(i + irq_action)))
115 goto out_unlock;
116 seq_printf(p, "%3d: ", i);
117#ifndef CONFIG_SMP
118 seq_printf(p, "%10u ", kstat_irqs(i));
119#else
120 for (j = 0; j < NR_CPUS; j++) {
121 if (!cpu_online(j))
122 continue;
123 seq_printf(p, "%10u ",
124 kstat_cpu(j).irqs[i]);
125 }
126#endif
127 seq_printf(p, " %s:%lx", action->name,
128 get_ino_in_irqaction(action));
129 for (action = action->next; action; action = action->next) {
130 seq_printf(p, ", %s:%lx", action->name,
131 get_ino_in_irqaction(action));
132 }
133 seq_putc(p, '\n');
134 }
135out_unlock:
136 spin_unlock_irqrestore(&irq_action_lock, flags);
137
138 return 0;
139}
140
141/* Now these are always passed a true fully specified sun4u INO. */
142void enable_irq(unsigned int irq)
143{
144 struct ino_bucket *bucket = __bucket(irq);
145 unsigned long imap;
146 unsigned long tid;
147
148 imap = bucket->imap;
149 if (imap == 0UL)
150 return;
151
152 preempt_disable();
153
David S. Millerd82ace72006-02-09 02:52:44 -0800154 if (tlb_type == hypervisor) {
155 /* XXX SUN4V: implement me... XXX */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } else {
David S. Millerd82ace72006-02-09 02:52:44 -0800157 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
158 unsigned long ver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David S. Millerd82ace72006-02-09 02:52:44 -0800160 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
161 if ((ver >> 32) == __JALAPENO_ID ||
162 (ver >> 32) == __SERRANO_ID) {
163 /* We set it to our JBUS ID. */
164 __asm__ __volatile__("ldxa [%%g0] %1, %0"
165 : "=r" (tid)
166 : "i" (ASI_JBUS_CONFIG));
167 tid = ((tid & (0x1fUL<<17)) << 9);
168 tid &= IMAP_TID_JBUS;
169 } else {
170 /* We set it to our Safari AID. */
171 __asm__ __volatile__("ldxa [%%g0] %1, %0"
172 : "=r" (tid)
173 : "i"(ASI_SAFARI_CONFIG));
174 tid = ((tid & (0x3ffUL<<17)) << 9);
175 tid &= IMAP_AID_SAFARI;
176 }
177 } else if (this_is_starfire == 0) {
178 /* We set it to our UPA MID. */
179 __asm__ __volatile__("ldxa [%%g0] %1, %0"
180 : "=r" (tid)
181 : "i" (ASI_UPA_CONFIG));
182 tid = ((tid & UPA_CONFIG_MID) << 9);
183 tid &= IMAP_TID_UPA;
184 } else {
185 tid = (starfire_translate(imap,
186 smp_processor_id()) << 26);
187 tid &= IMAP_TID_UPA;
188 }
189
190 /* NOTE NOTE NOTE, IGN and INO are read-only, IGN is a product
191 * of this SYSIO's preconfigured IGN in the SYSIO Control
192 * Register, the hardware just mirrors that value here.
193 * However for Graphics and UPA Slave devices the full
194 * IMAP_INR field can be set by the programmer here.
195 *
196 * Things like FFB can now be handled via the new IRQ
197 * mechanism.
198 */
199 upa_writel(tid | IMAP_VALID, imap);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 preempt_enable();
203}
204
205/* This now gets passed true ino's as well. */
206void disable_irq(unsigned int irq)
207{
208 struct ino_bucket *bucket = __bucket(irq);
209 unsigned long imap;
210
211 imap = bucket->imap;
212 if (imap != 0UL) {
213 u32 tmp;
214
215 /* NOTE: We do not want to futz with the IRQ clear registers
216 * and move the state to IDLE, the SCSI code does call
217 * disable_irq() to assure atomicity in the queue cmd
218 * SCSI adapter driver code. Thus we'd lose interrupts.
219 */
220 tmp = upa_readl(imap);
221 tmp &= ~IMAP_VALID;
222 upa_writel(tmp, imap);
223 }
224}
225
226/* The timer is the one "weird" interrupt which is generated by
227 * the CPU %tick register and not by some normal vectored interrupt
228 * source. To handle this special case, we use this dummy INO bucket.
229 */
David S. Miller088dd1f2005-07-04 13:24:38 -0700230static struct irq_desc pil0_dummy_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static struct ino_bucket pil0_dummy_bucket = {
David S. Miller088dd1f2005-07-04 13:24:38 -0700232 .irq_info = &pil0_dummy_desc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
David S. Miller088dd1f2005-07-04 13:24:38 -0700235static void build_irq_error(const char *msg, unsigned int ino, int pil, int inofixup,
236 unsigned long iclr, unsigned long imap,
237 struct ino_bucket *bucket)
238{
239 prom_printf("IRQ: INO %04x (%d:%016lx:%016lx) --> "
240 "(%d:%d:%016lx:%016lx), halting...\n",
241 ino, bucket->pil, bucket->iclr, bucket->imap,
242 pil, inofixup, iclr, imap);
243 prom_halt();
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246unsigned int build_irq(int pil, int inofixup, unsigned long iclr, unsigned long imap)
247{
248 struct ino_bucket *bucket;
249 int ino;
250
251 if (pil == 0) {
252 if (iclr != 0UL || imap != 0UL) {
253 prom_printf("Invalid dummy bucket for PIL0 (%lx:%lx)\n",
254 iclr, imap);
255 prom_halt();
256 }
257 return __irq(&pil0_dummy_bucket);
258 }
259
260 /* RULE: Both must be specified in all other cases. */
261 if (iclr == 0UL || imap == 0UL) {
262 prom_printf("Invalid build_irq %d %d %016lx %016lx\n",
263 pil, inofixup, iclr, imap);
264 prom_halt();
265 }
266
267 ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
268 if (ino > NUM_IVECS) {
269 prom_printf("Invalid INO %04x (%d:%d:%016lx:%016lx)\n",
270 ino, pil, inofixup, iclr, imap);
271 prom_halt();
272 }
273
David S. Miller088dd1f2005-07-04 13:24:38 -0700274 bucket = &ivector_table[ino];
275 if (bucket->flags & IBF_ACTIVE)
276 build_irq_error("IRQ: Trying to build active INO bucket.\n",
277 ino, pil, inofixup, iclr, imap, bucket);
278
279 if (bucket->irq_info) {
280 if (bucket->imap != imap || bucket->iclr != iclr)
281 build_irq_error("IRQ: Trying to reinit INO bucket.\n",
282 ino, pil, inofixup, iclr, imap, bucket);
283
284 goto out;
285 }
286
287 bucket->irq_info = kmalloc(sizeof(struct irq_desc), GFP_ATOMIC);
288 if (!bucket->irq_info) {
289 prom_printf("IRQ: Error, kmalloc(irq_desc) failed.\n");
290 prom_halt();
291 }
292 memset(bucket->irq_info, 0, sizeof(struct irq_desc));
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* Ok, looks good, set it up. Don't touch the irq_chain or
295 * the pending flag.
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 bucket->imap = imap;
298 bucket->iclr = iclr;
299 bucket->pil = pil;
300 bucket->flags = 0;
301
David S. Miller088dd1f2005-07-04 13:24:38 -0700302out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return __irq(bucket);
304}
305
306static void atomic_bucket_insert(struct ino_bucket *bucket)
307{
308 unsigned long pstate;
309 unsigned int *ent;
310
311 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
312 __asm__ __volatile__("wrpr %0, %1, %%pstate"
313 : : "r" (pstate), "i" (PSTATE_IE));
314 ent = irq_work(smp_processor_id(), bucket->pil);
315 bucket->irq_chain = *ent;
316 *ent = __irq(bucket);
317 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
318}
319
David S. Miller088dd1f2005-07-04 13:24:38 -0700320static int check_irq_sharing(int pil, unsigned long irqflags)
321{
322 struct irqaction *action, *tmp;
323
324 action = *(irq_action + pil);
325 if (action) {
326 if ((action->flags & SA_SHIRQ) && (irqflags & SA_SHIRQ)) {
327 for (tmp = action; tmp->next; tmp = tmp->next)
328 ;
329 } else {
330 return -EBUSY;
331 }
332 }
333 return 0;
334}
335
336static void append_irq_action(int pil, struct irqaction *action)
337{
338 struct irqaction **pp = irq_action + pil;
339
340 while (*pp)
341 pp = &((*pp)->next);
342 *pp = action;
343}
344
345static struct irqaction *get_action_slot(struct ino_bucket *bucket)
346{
347 struct irq_desc *desc = bucket->irq_info;
348 int max_irq, i;
349
350 max_irq = 1;
351 if (bucket->flags & IBF_PCI)
352 max_irq = MAX_IRQ_DESC_ACTION;
353 for (i = 0; i < max_irq; i++) {
354 struct irqaction *p = &desc->action[i];
355 u32 mask = (1 << i);
356
357 if (desc->action_active_mask & mask)
358 continue;
359
360 desc->action_active_mask |= mask;
361 return p;
362 }
363 return NULL;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
367 unsigned long irqflags, const char *name, void *dev_id)
368{
David S. Miller088dd1f2005-07-04 13:24:38 -0700369 struct irqaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct ino_bucket *bucket = __bucket(irq);
371 unsigned long flags;
372 int pending = 0;
373
David S. Miller088dd1f2005-07-04 13:24:38 -0700374 if (unlikely(!handler))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
David S. Miller088dd1f2005-07-04 13:24:38 -0700376
377 if (unlikely(!bucket->irq_info))
378 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if ((bucket != &pil0_dummy_bucket) && (irqflags & SA_SAMPLE_RANDOM)) {
381 /*
382 * This function might sleep, we want to call it first,
383 * outside of the atomic block. In SA_STATIC_ALLOC case,
384 * random driver's kmalloc will fail, but it is safe.
385 * If already initialized, random driver will not reinit.
386 * Yes, this might clear the entropy pool if the wrong
387 * driver is attempted to be loaded, without actually
388 * installing a new handler, but is this really a problem,
389 * only the sysadmin is able to do this.
390 */
391 rand_initialize_irq(irq);
392 }
393
394 spin_lock_irqsave(&irq_action_lock, flags);
395
David S. Miller088dd1f2005-07-04 13:24:38 -0700396 if (check_irq_sharing(bucket->pil, irqflags)) {
397 spin_unlock_irqrestore(&irq_action_lock, flags);
398 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
David S. Miller088dd1f2005-07-04 13:24:38 -0700401 action = get_action_slot(bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!action) {
403 spin_unlock_irqrestore(&irq_action_lock, flags);
404 return -ENOMEM;
405 }
406
David S. Miller088dd1f2005-07-04 13:24:38 -0700407 bucket->flags |= IBF_ACTIVE;
408 pending = 0;
409 if (bucket != &pil0_dummy_bucket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 pending = bucket->pending;
411 if (pending)
412 bucket->pending = 0;
413 }
414
415 action->handler = handler;
416 action->flags = irqflags;
417 action->name = name;
418 action->next = NULL;
419 action->dev_id = dev_id;
420 put_ino_in_irqaction(action, irq);
421 put_smpaff_in_irqaction(action, CPU_MASK_NONE);
422
David S. Miller088dd1f2005-07-04 13:24:38 -0700423 append_irq_action(bucket->pil, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 enable_irq(irq);
426
427 /* We ate the IVEC already, this makes sure it does not get lost. */
428 if (pending) {
429 atomic_bucket_insert(bucket);
430 set_softint(1 << bucket->pil);
431 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock_irqrestore(&irq_action_lock, flags);
David S. Miller088dd1f2005-07-04 13:24:38 -0700434
435 if (bucket != &pil0_dummy_bucket)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 register_irq_proc(__irq_ino(irq));
437
438#ifdef CONFIG_SMP
439 distribute_irqs();
440#endif
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444EXPORT_SYMBOL(request_irq);
445
David S. Miller088dd1f2005-07-04 13:24:38 -0700446static struct irqaction *unlink_irq_action(unsigned int irq, void *dev_id)
447{
448 struct ino_bucket *bucket = __bucket(irq);
449 struct irqaction *action, **pp;
450
451 pp = irq_action + bucket->pil;
452 action = *pp;
453 if (unlikely(!action))
454 return NULL;
455
456 if (unlikely(!action->handler)) {
457 printk("Freeing free IRQ %d\n", bucket->pil);
458 return NULL;
459 }
460
461 while (action && action->dev_id != dev_id) {
462 pp = &action->next;
463 action = *pp;
464 }
465
466 if (likely(action))
467 *pp = action->next;
468
469 return action;
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472void free_irq(unsigned int irq, void *dev_id)
473{
474 struct irqaction *action;
David S. Miller088dd1f2005-07-04 13:24:38 -0700475 struct ino_bucket *bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 spin_lock_irqsave(&irq_action_lock, flags);
479
David S. Miller088dd1f2005-07-04 13:24:38 -0700480 action = unlink_irq_action(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 spin_unlock_irqrestore(&irq_action_lock, flags);
483
David S. Miller088dd1f2005-07-04 13:24:38 -0700484 if (unlikely(!action))
485 return;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 synchronize_irq(irq);
488
489 spin_lock_irqsave(&irq_action_lock, flags);
490
David S. Miller088dd1f2005-07-04 13:24:38 -0700491 bucket = __bucket(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (bucket != &pil0_dummy_bucket) {
David S. Miller088dd1f2005-07-04 13:24:38 -0700493 struct irq_desc *desc = bucket->irq_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 unsigned long imap = bucket->imap;
David S. Miller088dd1f2005-07-04 13:24:38 -0700495 int ent, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David S. Miller088dd1f2005-07-04 13:24:38 -0700497 for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
498 struct irqaction *p = &desc->action[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David S. Miller088dd1f2005-07-04 13:24:38 -0700500 if (p == action) {
501 desc->action_active_mask &= ~(1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
David S. Miller088dd1f2005-07-04 13:24:38 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
David S. Miller088dd1f2005-07-04 13:24:38 -0700506 if (!desc->action_active_mask) {
507 /* This unique interrupt source is now inactive. */
508 bucket->flags &= ~IBF_ACTIVE;
509
510 /* See if any other buckets share this bucket's IMAP
511 * and are still active.
512 */
513 for (ent = 0; ent < NUM_IVECS; ent++) {
514 struct ino_bucket *bp = &ivector_table[ent];
515 if (bp != bucket &&
516 bp->imap == imap &&
517 (bp->flags & IBF_ACTIVE) != 0)
518 break;
519 }
520
521 /* Only disable when no other sub-irq levels of
522 * the same IMAP are active.
523 */
524 if (ent == NUM_IVECS)
525 disable_irq(irq);
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 spin_unlock_irqrestore(&irq_action_lock, flags);
530}
531
532EXPORT_SYMBOL(free_irq);
533
534#ifdef CONFIG_SMP
535void synchronize_irq(unsigned int irq)
536{
537 struct ino_bucket *bucket = __bucket(irq);
538
539#if 0
540 /* The following is how I wish I could implement this.
541 * Unfortunately the ICLR registers are read-only, you can
542 * only write ICLR_foo values to them. To get the current
543 * IRQ status you would need to get at the IRQ diag registers
544 * in the PCI/SBUS controller and the layout of those vary
545 * from one controller to the next, sigh... -DaveM
546 */
547 unsigned long iclr = bucket->iclr;
548
549 while (1) {
550 u32 tmp = upa_readl(iclr);
551
552 if (tmp == ICLR_TRANSMIT ||
553 tmp == ICLR_PENDING) {
554 cpu_relax();
555 continue;
556 }
557 break;
558 }
559#else
560 /* So we have to do this with a INPROGRESS bit just like x86. */
561 while (bucket->flags & IBF_INPROGRESS)
562 cpu_relax();
563#endif
564}
565#endif /* CONFIG_SMP */
566
David S. Miller088dd1f2005-07-04 13:24:38 -0700567static void process_bucket(int irq, struct ino_bucket *bp, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
David S. Miller088dd1f2005-07-04 13:24:38 -0700569 struct irq_desc *desc = bp->irq_info;
570 unsigned char flags = bp->flags;
571 u32 action_mask, i;
572 int random;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
David S. Miller088dd1f2005-07-04 13:24:38 -0700574 bp->flags |= IBF_INPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
David S. Miller088dd1f2005-07-04 13:24:38 -0700576 if (unlikely(!(flags & IBF_ACTIVE))) {
577 bp->pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
David S. Miller088dd1f2005-07-04 13:24:38 -0700581 if (desc->pre_handler)
582 desc->pre_handler(bp,
583 desc->pre_handler_arg1,
584 desc->pre_handler_arg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
David S. Miller088dd1f2005-07-04 13:24:38 -0700586 action_mask = desc->action_active_mask;
587 random = 0;
588 for (i = 0; i < MAX_IRQ_DESC_ACTION; i++) {
589 struct irqaction *p = &desc->action[i];
590 u32 mask = (1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
David S. Miller088dd1f2005-07-04 13:24:38 -0700592 if (!(action_mask & mask))
593 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
David S. Miller088dd1f2005-07-04 13:24:38 -0700595 action_mask &= ~mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
David S. Miller088dd1f2005-07-04 13:24:38 -0700597 if (p->handler(__irq(bp), p->dev_id, regs) == IRQ_HANDLED)
598 random |= p->flags;
599
600 if (!action_mask)
601 break;
602 }
603 if (bp->pil != 0) {
604 upa_writel(ICLR_IDLE, bp->iclr);
605 /* Test and add entropy */
606 if (random & SA_SAMPLE_RANDOM)
607 add_interrupt_randomness(irq);
608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609out:
David S. Miller088dd1f2005-07-04 13:24:38 -0700610 bp->flags &= ~IBF_INPROGRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613void handler_irq(int irq, struct pt_regs *regs)
614{
David S. Miller088dd1f2005-07-04 13:24:38 -0700615 struct ino_bucket *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int cpu = smp_processor_id();
617
618#ifndef CONFIG_SMP
619 /*
620 * Check for TICK_INT on level 14 softint.
621 */
622 {
623 unsigned long clr_mask = 1 << irq;
624 unsigned long tick_mask = tick_ops->softint_mask;
625
626 if ((irq == 14) && (get_softint() & tick_mask)) {
627 irq = 0;
628 clr_mask = tick_mask;
629 }
630 clear_softint(clr_mask);
631 }
632#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 clear_softint(1 << irq);
634#endif
635
636 irq_enter();
637 kstat_this_cpu.irqs[irq]++;
638
639 /* Sliiiick... */
640#ifndef CONFIG_SMP
641 bp = ((irq != 0) ?
642 __bucket(xchg32(irq_work(cpu, irq), 0)) :
643 &pil0_dummy_bucket);
644#else
645 bp = __bucket(xchg32(irq_work(cpu, irq), 0));
646#endif
David S. Miller088dd1f2005-07-04 13:24:38 -0700647 while (bp) {
648 struct ino_bucket *nbp = __bucket(bp->irq_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 bp->irq_chain = 0;
David S. Miller088dd1f2005-07-04 13:24:38 -0700651 process_bucket(irq, bp, regs);
652 bp = nbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 irq_exit();
655}
656
657#ifdef CONFIG_BLK_DEV_FD
David S. Miller63b61452005-06-27 17:04:45 -0700658extern irqreturn_t floppy_interrupt(int, void *, struct pt_regs *);;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
David S. Miller63b61452005-06-27 17:04:45 -0700660/* XXX No easy way to include asm/floppy.h XXX */
661extern unsigned char *pdma_vaddr;
662extern unsigned long pdma_size;
663extern volatile int doing_pdma;
664extern unsigned long fdc_status;
665
666irqreturn_t sparc_floppy_irq(int irq, void *dev_cookie, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
David S. Miller63b61452005-06-27 17:04:45 -0700668 if (likely(doing_pdma)) {
669 void __iomem *stat = (void __iomem *) fdc_status;
670 unsigned char *vaddr = pdma_vaddr;
671 unsigned long size = pdma_size;
672 u8 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
David S. Miller63b61452005-06-27 17:04:45 -0700674 while (size) {
675 val = readb(stat);
676 if (unlikely(!(val & 0x80))) {
677 pdma_vaddr = vaddr;
678 pdma_size = size;
679 return IRQ_HANDLED;
680 }
681 if (unlikely(!(val & 0x20))) {
682 pdma_vaddr = vaddr;
683 pdma_size = size;
684 doing_pdma = 0;
685 goto main_interrupt;
686 }
687 if (val & 0x40) {
688 /* read */
689 *vaddr++ = readb(stat + 1);
690 } else {
691 unsigned char data = *vaddr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
David S. Miller63b61452005-06-27 17:04:45 -0700693 /* write */
694 writeb(data, stat + 1);
695 }
696 size--;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
David S. Miller63b61452005-06-27 17:04:45 -0700699 pdma_vaddr = vaddr;
700 pdma_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David S. Miller63b61452005-06-27 17:04:45 -0700702 /* Send Terminal Count pulse to floppy controller. */
703 val = readb(auxio_register);
704 val |= AUXIO_AUX1_FTCNT;
705 writeb(val, auxio_register);
Bernhard R Link94bbc172006-03-10 01:23:13 -0800706 val &= ~AUXIO_AUX1_FTCNT;
David S. Miller63b61452005-06-27 17:04:45 -0700707 writeb(val, auxio_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
David S. Miller63b61452005-06-27 17:04:45 -0700709 doing_pdma = 0;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
David S. Miller63b61452005-06-27 17:04:45 -0700712main_interrupt:
713 return floppy_interrupt(irq, dev_cookie, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
David S. Miller63b61452005-06-27 17:04:45 -0700715EXPORT_SYMBOL(sparc_floppy_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716#endif
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/* We really don't need these at all on the Sparc. We only have
719 * stubs here because they are exported to modules.
720 */
721unsigned long probe_irq_on(void)
722{
723 return 0;
724}
725
726EXPORT_SYMBOL(probe_irq_on);
727
728int probe_irq_off(unsigned long mask)
729{
730 return 0;
731}
732
733EXPORT_SYMBOL(probe_irq_off);
734
735#ifdef CONFIG_SMP
736static int retarget_one_irq(struct irqaction *p, int goal_cpu)
737{
738 struct ino_bucket *bucket = get_ino_in_irqaction(p) + ivector_table;
739 unsigned long imap = bucket->imap;
740 unsigned int tid;
741
742 while (!cpu_online(goal_cpu)) {
743 if (++goal_cpu >= NR_CPUS)
744 goal_cpu = 0;
745 }
746
747 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
748 tid = goal_cpu << 26;
749 tid &= IMAP_AID_SAFARI;
750 } else if (this_is_starfire == 0) {
751 tid = goal_cpu << 26;
752 tid &= IMAP_TID_UPA;
753 } else {
754 tid = (starfire_translate(imap, goal_cpu) << 26);
755 tid &= IMAP_TID_UPA;
756 }
757 upa_writel(tid | IMAP_VALID, imap);
758
David S. Millercee28242005-05-03 22:04:36 -0700759 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (++goal_cpu >= NR_CPUS)
761 goal_cpu = 0;
David S. Millercee28242005-05-03 22:04:36 -0700762 } while (!cpu_online(goal_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 return goal_cpu;
765}
766
767/* Called from request_irq. */
768static void distribute_irqs(void)
769{
770 unsigned long flags;
771 int cpu, level;
772
773 spin_lock_irqsave(&irq_action_lock, flags);
774 cpu = 0;
775
776 /*
777 * Skip the timer at [0], and very rare error/power intrs at [15].
778 * Also level [12], it causes problems on Ex000 systems.
779 */
780 for (level = 1; level < NR_IRQS; level++) {
781 struct irqaction *p = irq_action[level];
David S. Miller088dd1f2005-07-04 13:24:38 -0700782
783 if (level == 12)
784 continue;
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 while(p) {
787 cpu = retarget_one_irq(p, cpu);
788 p = p->next;
789 }
790 }
791 spin_unlock_irqrestore(&irq_action_lock, flags);
792}
793#endif
794
David S. Millercdd51862005-07-24 19:36:13 -0700795struct sun5_timer {
796 u64 count0;
797 u64 limit0;
798 u64 count1;
799 u64 limit1;
800};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
David S. Millercdd51862005-07-24 19:36:13 -0700802static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803static u64 prom_limit0, prom_limit1;
804
805static void map_prom_timers(void)
806{
807 unsigned int addr[3];
808 int tnode, err;
809
810 /* PROM timer node hangs out in the top level of device siblings... */
811 tnode = prom_finddevice("/counter-timer");
812
813 /* Assume if node is not present, PROM uses different tick mechanism
814 * which we should not care about.
815 */
816 if (tnode == 0 || tnode == -1) {
817 prom_timers = (struct sun5_timer *) 0;
818 return;
819 }
820
821 /* If PROM is really using this, it must be mapped by him. */
822 err = prom_getproperty(tnode, "address", (char *)addr, sizeof(addr));
823 if (err == -1) {
824 prom_printf("PROM does not have timer mapped, trying to continue.\n");
825 prom_timers = (struct sun5_timer *) 0;
826 return;
827 }
828 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
829}
830
831static void kill_prom_timer(void)
832{
833 if (!prom_timers)
834 return;
835
836 /* Save them away for later. */
837 prom_limit0 = prom_timers->limit0;
838 prom_limit1 = prom_timers->limit1;
839
840 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
841 * We turn both off here just to be paranoid.
842 */
843 prom_timers->limit0 = 0;
844 prom_timers->limit1 = 0;
845
846 /* Wheee, eat the interrupt packet too... */
847 __asm__ __volatile__(
848" mov 0x40, %%g2\n"
849" ldxa [%%g0] %0, %%g1\n"
850" ldxa [%%g2] %1, %%g1\n"
851" stxa %%g0, [%%g0] %0\n"
852" membar #Sync\n"
853 : /* no outputs */
854 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
855 : "g1", "g2");
856}
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858void init_irqwork_curcpu(void)
859{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int cpu = hard_smp_processor_id();
861
David S. Miller56fb4df2006-02-26 23:24:22 -0800862 memset(__irq_work + cpu, 0, sizeof(struct irq_work_struct));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
David S. Millerb5a37e92006-02-11 23:07:13 -0800865static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
David S. Millerac29c112006-02-08 00:08:23 -0800866{
David S. Miller164c2202006-02-09 22:57:21 -0800867 register unsigned long func __asm__("%o5");
868 register unsigned long arg0 __asm__("%o0");
869 register unsigned long arg1 __asm__("%o1");
870 register unsigned long arg2 __asm__("%o2");
David S. Millerac29c112006-02-08 00:08:23 -0800871
872 func = HV_FAST_CPU_QCONF;
873 arg0 = type;
David S. Millerb5a37e92006-02-11 23:07:13 -0800874 arg1 = paddr;
David S. Millerac29c112006-02-08 00:08:23 -0800875 arg2 = 128; /* XXX Implied by Niagara queue offsets. XXX */
876 __asm__ __volatile__("ta %8"
877 : "=&r" (func), "=&r" (arg0),
878 "=&r" (arg1), "=&r" (arg2)
879 : "0" (func), "1" (arg0),
880 "2" (arg1), "3" (arg2),
881 "i" (HV_FAST_TRAP));
882
David S. Millerb5a37e92006-02-11 23:07:13 -0800883 if (arg0 != HV_EOK) {
David S. Millerac29c112006-02-08 00:08:23 -0800884 prom_printf("SUN4V: cpu_qconf(%lu) failed with error %lu\n",
885 type, func);
886 prom_halt();
887 }
888}
889
David S. Millerb5a37e92006-02-11 23:07:13 -0800890static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c05722006-02-08 02:53:50 -0800891{
David S. Millerb5a37e92006-02-11 23:07:13 -0800892 struct trap_per_cpu *tb = &trap_block[this_cpu];
893
894 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
895 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
896 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
897 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
898}
899
900static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
901{
902 void *page;
903
904 if (use_bootmem)
905 page = alloc_bootmem_low_pages(PAGE_SIZE);
906 else
907 page = (void *) get_zeroed_page(GFP_ATOMIC);
908
909 if (!page) {
910 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
911 prom_halt();
912 }
913
914 *pa_ptr = __pa(page);
915}
916
917static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
918{
919 void *page;
920
921 if (use_bootmem)
922 page = alloc_bootmem_low_pages(PAGE_SIZE);
923 else
924 page = (void *) get_zeroed_page(GFP_ATOMIC);
David S. Miller5b0c05722006-02-08 02:53:50 -0800925
926 if (!page) {
927 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
928 prom_halt();
929 }
930
931 *pa_ptr = __pa(page);
932}
933
David S. Millerb5a37e92006-02-11 23:07:13 -0800934static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800935{
936#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800937 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800938
939 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
940
David S. Millerb5a37e92006-02-11 23:07:13 -0800941 if (use_bootmem)
942 page = alloc_bootmem_low_pages(PAGE_SIZE);
943 else
944 page = (void *) get_zeroed_page(GFP_ATOMIC);
945
David S. Miller1d2f1f92006-02-08 16:41:20 -0800946 if (!page) {
947 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
948 prom_halt();
949 }
950
951 tb->cpu_mondo_block_pa = __pa(page);
952 tb->cpu_list_pa = __pa(page + 64);
953#endif
954}
955
David S. Millerb5a37e92006-02-11 23:07:13 -0800956/* Allocate and register the mondo and error queues for this cpu. */
957void __cpuinit sun4v_init_mondo_queues(int use_bootmem)
David S. Millerac29c112006-02-08 00:08:23 -0800958{
959 int cpu = hard_smp_processor_id();
960 struct trap_per_cpu *tb = &trap_block[cpu];
961
David S. Millerb5a37e92006-02-11 23:07:13 -0800962 alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
963 alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
964 alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
965 alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
966 alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
967 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800968
David S. Millerb5a37e92006-02-11 23:07:13 -0800969 init_cpu_send_mondo_info(tb, use_bootmem);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800970
David S. Millerb5a37e92006-02-11 23:07:13 -0800971 sun4v_register_mondo_queues(cpu);
David S. Millerac29c112006-02-08 00:08:23 -0800972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974/* Only invoked on boot processor. */
975void __init init_IRQ(void)
976{
977 map_prom_timers();
978 kill_prom_timer();
979 memset(&ivector_table[0], 0, sizeof(ivector_table));
980
David S. Millerac29c112006-02-08 00:08:23 -0800981 if (tlb_type == hypervisor)
David S. Millerb5a37e92006-02-11 23:07:13 -0800982 sun4v_init_mondo_queues(1);
David S. Millerac29c112006-02-08 00:08:23 -0800983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* We need to clear any IRQ's pending in the soft interrupt
985 * registers, a spurious one could be left around from the
986 * PROM timer which we just disabled.
987 */
988 clear_softint(get_softint());
989
990 /* Now that ivector table is initialized, it is safe
991 * to receive IRQ vector traps. We will normally take
992 * one or two right now, in case some device PROM used
993 * to boot us wants to speak to us. We just ignore them.
994 */
995 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
996 "or %%g1, %0, %%g1\n\t"
997 "wrpr %%g1, 0x0, %%pstate"
998 : /* No outputs */
999 : "i" (PSTATE_IE)
1000 : "g1");
1001}
1002
1003static struct proc_dir_entry * root_irq_dir;
1004static struct proc_dir_entry * irq_dir [NUM_IVECS];
1005
1006#ifdef CONFIG_SMP
1007
1008static int irq_affinity_read_proc (char *page, char **start, off_t off,
1009 int count, int *eof, void *data)
1010{
1011 struct ino_bucket *bp = ivector_table + (long)data;
Eddie C. Dost12cf6492005-07-06 15:40:21 -07001012 struct irq_desc *desc = bp->irq_info;
1013 struct irqaction *ap = desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 cpumask_t mask;
1015 int len;
1016
1017 mask = get_smpaff_in_irqaction(ap);
1018 if (cpus_empty(mask))
1019 mask = cpu_online_map;
1020
1021 len = cpumask_scnprintf(page, count, mask);
1022 if (count - len < 2)
1023 return -EINVAL;
1024 len += sprintf(page + len, "\n");
1025 return len;
1026}
1027
1028static inline void set_intr_affinity(int irq, cpumask_t hw_aff)
1029{
1030 struct ino_bucket *bp = ivector_table + irq;
Eddie C. Dost12cf6492005-07-06 15:40:21 -07001031 struct irq_desc *desc = bp->irq_info;
1032 struct irqaction *ap = desc->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /* Users specify affinity in terms of hw cpu ids.
1035 * As soon as we do this, handler_irq() might see and take action.
1036 */
Eddie C. Dost12cf6492005-07-06 15:40:21 -07001037 put_smpaff_in_irqaction(ap, hw_aff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 /* Migration is simply done by the next cpu to service this
1040 * interrupt.
1041 */
1042}
1043
1044static int irq_affinity_write_proc (struct file *file, const char __user *buffer,
1045 unsigned long count, void *data)
1046{
1047 int irq = (long) data, full_count = count, err;
1048 cpumask_t new_value;
1049
1050 err = cpumask_parse(buffer, count, new_value);
1051
1052 /*
1053 * Do not allow disabling IRQs completely - it's a too easy
1054 * way to make the system unusable accidentally :-) At least
1055 * one online CPU still has to be targeted.
1056 */
1057 cpus_and(new_value, new_value, cpu_online_map);
1058 if (cpus_empty(new_value))
1059 return -EINVAL;
1060
1061 set_intr_affinity(irq, new_value);
1062
1063 return full_count;
1064}
1065
1066#endif
1067
1068#define MAX_NAMELEN 10
1069
1070static void register_irq_proc (unsigned int irq)
1071{
1072 char name [MAX_NAMELEN];
1073
1074 if (!root_irq_dir || irq_dir[irq])
1075 return;
1076
1077 memset(name, 0, MAX_NAMELEN);
1078 sprintf(name, "%x", irq);
1079
1080 /* create /proc/irq/1234 */
1081 irq_dir[irq] = proc_mkdir(name, root_irq_dir);
1082
1083#ifdef CONFIG_SMP
1084 /* XXX SMP affinity not supported on starfire yet. */
1085 if (this_is_starfire == 0) {
1086 struct proc_dir_entry *entry;
1087
1088 /* create /proc/irq/1234/smp_affinity */
1089 entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
1090
1091 if (entry) {
1092 entry->nlink = 1;
1093 entry->data = (void *)(long)irq;
1094 entry->read_proc = irq_affinity_read_proc;
1095 entry->write_proc = irq_affinity_write_proc;
1096 }
1097 }
1098#endif
1099}
1100
1101void init_irq_proc (void)
1102{
1103 /* create /proc/irq */
1104 root_irq_dir = proc_mkdir("irq", NULL);
1105}
1106