cleanup
[linux-2.4.21-pre4.git] / arch / ppc64 / kernel / irq.c
1 /*
2  *  arch/ppc/kernel/irq.c
3  *
4  *  Derived from arch/i386/kernel/irq.c
5  *    Copyright (C) 1992 Linus Torvalds
6  *  Adapted from arch/i386 by Gary Thomas
7  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
8  *  Updated and modified by Cort Dougan (cort@cs.nmt.edu)
9  *    Copyright (C) 1996 Cort Dougan
10  *  Adapted for Power Macintosh by Paul Mackerras
11  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
12  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  *
19  * This file contains the code used by various IRQ handling routines:
20  * asking for different IRQ's should be done through these routines
21  * instead of just grabbing them. Thus setups with different IRQ numbers
22  * shouldn't result in any weird surprises, and installing new handlers
23  * should be easier.
24  */
25
26 #include <linux/ptrace.h>
27 #include <linux/errno.h>
28 #include <linux/threads.h>
29 #include <linux/kernel_stat.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/ioport.h>
33 #include <linux/interrupt.h>
34 #include <linux/timex.h>
35 #include <linux/config.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/pci.h>
39 #include <linux/delay.h>
40 #include <linux/irq.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/random.h>
44
45 #include <asm/uaccess.h>
46 #include <asm/bitops.h>
47 #include <asm/system.h>
48 #include <asm/io.h>
49 #include <asm/pgtable.h>
50 #include <asm/irq.h>
51 #include <asm/cache.h>
52 #include <asm/prom.h>
53 #include <asm/ptrace.h>
54 #include <asm/iSeries/LparData.h>
55 #include <asm/machdep.h>
56 #include <asm/paca.h>
57 #include <asm/perfmon.h>
58
59 #include "local_irq.h"
60
61 atomic_t ipi_recv;
62 atomic_t ipi_sent;
63 void enable_irq(unsigned int irq_nr);
64 void disable_irq(unsigned int irq_nr);
65
66 #ifdef CONFIG_SMP
67 extern void iSeries_smp_message_recv( struct pt_regs * );
68 #endif
69
70 volatile unsigned char *chrp_int_ack_special;
71 static void register_irq_proc (unsigned int irq);
72
73 irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned =
74         { [0 ... NR_IRQS-1] = { 0, NULL, NULL, 0, SPIN_LOCK_UNLOCKED}};
75         
76 int ppc_spurious_interrupts = 0;
77 struct irqaction *ppc_irq_action[NR_IRQS];
78 unsigned long lpEvent_count = 0;
79 #ifdef CONFIG_XMON
80 extern void xmon(struct pt_regs *regs);
81 extern int xmon_bpt(struct pt_regs *regs);
82 extern int xmon_sstep(struct pt_regs *regs);
83 extern int xmon_iabr_match(struct pt_regs *regs);
84 extern int xmon_dabr_match(struct pt_regs *regs);
85 extern void (*xmon_fault_handler)(struct pt_regs *regs);
86 #endif
87 #ifdef CONFIG_XMON
88 extern void (*debugger)(struct pt_regs *regs);
89 extern int (*debugger_bpt)(struct pt_regs *regs);
90 extern int (*debugger_sstep)(struct pt_regs *regs);
91 extern int (*debugger_iabr_match)(struct pt_regs *regs);
92 extern int (*debugger_dabr_match)(struct pt_regs *regs);
93 extern void (*debugger_fault_handler)(struct pt_regs *regs);
94 #endif
95
96 /* nasty hack for shared irq's since we need to do kmalloc calls but
97  * can't very early in the boot when we need to do a request irq.
98  * this needs to be removed.
99  * -- Cort
100  */
101 #define IRQ_KMALLOC_ENTRIES 16
102 static int cache_bitmask = 0;
103 static struct irqaction malloc_cache[IRQ_KMALLOC_ENTRIES];
104 extern int mem_init_done;
105
106 void *irq_kmalloc(size_t size, int pri)
107 {
108         unsigned int i;
109         if ( mem_init_done )
110                 return kmalloc(size,pri);
111         for ( i = 0; i < IRQ_KMALLOC_ENTRIES ; i++ )
112                 if ( ! ( cache_bitmask & (1<<i) ) ) {
113                         cache_bitmask |= (1<<i);
114                         return (void *)(&malloc_cache[i]);
115                 }
116         return 0;
117 }
118
119 void irq_kfree(void *ptr)
120 {
121         unsigned int i;
122         for ( i = 0 ; i < IRQ_KMALLOC_ENTRIES ; i++ )
123                 if ( ptr == &malloc_cache[i] ) {
124                         cache_bitmask &= ~(1<<i);
125                         return;
126                 }
127         kfree(ptr);
128 }
129
130 int
131 setup_irq(unsigned int irq, struct irqaction * new)
132 {
133         int shared = 0;
134         unsigned long flags;
135         struct irqaction *old, **p;
136         irq_desc_t *desc = irq_desc + irq;
137
138         /*
139          * Some drivers like serial.c use request_irq() heavily,
140          * so we have to be careful not to interfere with a
141          * running system.
142          */
143         if (new->flags & SA_SAMPLE_RANDOM) {
144                 /*
145                  * This function might sleep, we want to call it first,
146                  * outside of the atomic block.
147                  * Yes, this might clear the entropy pool if the wrong
148                  * driver is attempted to be loaded, without actually
149                  * installing a new handler, but is this really a problem,
150                  * only the sysadmin is able to do this.
151                  */
152                 rand_initialize_irq(irq);
153         }
154
155         /*
156          * The following block of code has to be executed atomically
157          */
158         spin_lock_irqsave(&desc->lock,flags);
159         p = &desc->action;
160         if ((old = *p) != NULL) {
161                 /* Can't share interrupts unless both agree to */
162                 if (!(old->flags & new->flags & SA_SHIRQ)) {
163                         spin_unlock_irqrestore(&desc->lock,flags);
164                         return -EBUSY;
165                 }
166
167                 /* add new interrupt at end of irq queue */
168                 do {
169                         p = &old->next;
170                         old = *p;
171                 } while (old);
172                 shared = 1;
173         }
174
175         *p = new;
176
177         if (!shared) {
178                 desc->depth = 0;
179                 desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
180                 unmask_irq(irq);
181         }
182         spin_unlock_irqrestore(&desc->lock,flags);
183
184         register_irq_proc(irq);
185         return 0;
186 }
187
188 /* This could be promoted to a real free_irq() ... */
189 static int
190 do_free_irq(int irq, void* dev_id)
191 {
192         irq_desc_t *desc;
193         struct irqaction **p;
194         unsigned long flags;
195
196         desc = irq_desc + irq;
197         spin_lock_irqsave(&desc->lock,flags);
198         p = &desc->action;
199         for (;;) {
200                 struct irqaction * action = *p;
201                 if (action) {
202                         struct irqaction **pp = p;
203                         p = &action->next;
204                         if (action->dev_id != dev_id)
205                                 continue;
206
207                         /* Found it - now remove it from the list of entries */
208                         *pp = action->next;
209                         if (!desc->action) {
210                                 desc->status |= IRQ_DISABLED;
211                                 mask_irq(irq);
212                         }
213                         spin_unlock_irqrestore(&desc->lock,flags);
214
215 #ifdef CONFIG_SMP
216                         /* Wait to make sure it's not being used on another CPU */
217                         while (desc->status & IRQ_INPROGRESS)
218                                 barrier();
219 #endif
220                         irq_kfree(action);
221                         return 0;
222                 }
223                 printk("Trying to free free IRQ%d\n",irq);
224                 spin_unlock_irqrestore(&desc->lock,flags);
225                 break;
226         }
227         return -ENOENT;
228 }
229
230 int request_irq(unsigned int irq, void (*handler)(int, void *, struct pt_regs *),
231         unsigned long irqflags, const char * devname, void *dev_id)
232 {
233         struct irqaction *action;
234         int retval;
235
236         if (irq >= NR_IRQS)
237                 return -EINVAL;
238         if (!handler)
239                 /* We could implement really free_irq() instead of that... */
240                 return do_free_irq(irq, dev_id);
241         
242         action = (struct irqaction *)
243                 irq_kmalloc(sizeof(struct irqaction), GFP_KERNEL);
244         if (!action) {
245                 printk(KERN_ERR "irq_kmalloc() failed for irq %d !\n", irq);
246                 return -ENOMEM;
247         }
248         
249         action->handler = handler;
250         action->flags = irqflags;                                       
251         action->mask = 0;
252         action->name = devname;
253         action->dev_id = dev_id;
254         action->next = NULL;
255         
256         retval = setup_irq(irq, action);
257         if (retval)
258                 kfree(action);
259                 
260         return 0;
261 }
262
263 void free_irq(unsigned int irq, void *dev_id)
264 {
265         request_irq(irq, NULL, 0, NULL, dev_id);
266 }
267
268 /*
269  * Generic enable/disable code: this just calls
270  * down into the PIC-specific version for the actual
271  * hardware disable after having gotten the irq
272  * controller lock. 
273  */
274  
275 /**
276  *      disable_irq_nosync - disable an irq without waiting
277  *      @irq: Interrupt to disable
278  *
279  *      Disable the selected interrupt line. Disables of an interrupt
280  *      stack. Unlike disable_irq(), this function does not ensure existing
281  *      instances of the IRQ handler have completed before returning.
282  *
283  *      This function may be called from IRQ context.
284  */
285  
286  void disable_irq_nosync(unsigned int irq)
287 {
288         irq_desc_t *desc = irq_desc + irq;
289         unsigned long flags;
290
291         spin_lock_irqsave(&desc->lock, flags);
292         if (!desc->depth++) {
293                 if (!(desc->status & IRQ_PER_CPU))
294                         desc->status |= IRQ_DISABLED;
295                 mask_irq(irq);
296         }
297         spin_unlock_irqrestore(&desc->lock, flags);
298 }
299
300 /**
301  *      disable_irq - disable an irq and wait for completion
302  *      @irq: Interrupt to disable
303  *
304  *      Disable the selected interrupt line. Disables of an interrupt
305  *      stack. That is for two disables you need two enables. This
306  *      function waits for any pending IRQ handlers for this interrupt
307  *      to complete before returning. If you use this function while
308  *      holding a resource the IRQ handler may need you will deadlock.
309  *
310  *      This function may be called - with care - from IRQ context.
311  */
312  
313 void disable_irq(unsigned int irq)
314 {
315         disable_irq_nosync(irq);
316
317         if (!local_irq_count(smp_processor_id())) {
318                 do {
319                         barrier();
320                 } while (irq_desc[irq].status & IRQ_INPROGRESS);
321         }
322 }
323
324 /**
325  *      enable_irq - enable interrupt handling on an irq
326  *      @irq: Interrupt to enable
327  *
328  *      Re-enables the processing of interrupts on this IRQ line
329  *      providing no disable_irq calls are now in effect.
330  *
331  *      This function may be called from IRQ context.
332  */
333  
334 void enable_irq(unsigned int irq)
335 {
336         irq_desc_t *desc = irq_desc + irq;
337         unsigned long flags;
338
339         spin_lock_irqsave(&desc->lock, flags);
340         switch (desc->depth) {
341         case 1: {
342                 unsigned int status = desc->status & ~IRQ_DISABLED;
343                 desc->status = status;
344                 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
345                         desc->status = status | IRQ_REPLAY;
346                         hw_resend_irq(desc->handler,irq);
347                 }
348                 unmask_irq(irq);
349                 /* fall-through */
350         }
351         default:
352                 desc->depth--;
353                 break;
354         case 0:
355                 printk("enable_irq(%u) unbalanced\n", irq);
356         }
357         spin_unlock_irqrestore(&desc->lock, flags);
358 }
359
360 /* one would think this function has one foot in the grave */
361 int get_irq_list(char *buf)
362 {
363    int i, len = 0, j;
364    struct irqaction * action;
365
366    len += sprintf(buf+len, "           ");
367    for (j=0; j<smp_num_cpus; j++)
368        len += sprintf(buf+len, "CPU%d       ",j);
369    *(char *)(buf+len++) = '\n';
370
371    for (i = 0 ; i < NR_IRQS ; i++) {
372        action = irq_desc[i].action;
373        if ( !action || !action->handler )
374            continue;
375        len += sprintf(buf+len, "%3d: ", i);
376 #ifdef CONFIG_SMP
377        for (j = 0; j < smp_num_cpus; j++)
378            len += sprintf(buf+len, "%10u ",
379                kstat.irqs[cpu_logical_map(j)][i]);
380 #else
381        len += sprintf(buf+len, "%10u ", kstat_irqs(i));
382 #endif /* CONFIG_SMP */
383 if ( irq_desc[i].handler )
384 len += sprintf(buf+len, " %s ", irq_desc[i].handler->typename );
385 else
386 len += sprintf(buf+len, "  None      ");
387 len += sprintf(buf+len, "%s", (irq_desc[i].status & IRQ_LEVEL) ? "Level " : "Edge  ");
388 len += sprintf(buf+len, "    %s",action->name);
389 for (action=action->next; action; action = action->next) {
390 len += sprintf(buf+len, ", %s", action->name);
391 }
392 len += sprintf(buf+len, "\n");
393 }
394 #ifdef CONFIG_SMP
395 /* should this be per processor send/receive? */
396 len += sprintf(buf+len, "IPI (recv/sent): %10u/%u\n",
397 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
398 #endif
399 len += sprintf(buf+len, "BAD: %10u\n", ppc_spurious_interrupts);
400 return len;
401 }
402
403
404
405 int show_interrupts(struct seq_file *p, void *v)
406 {
407         int i, j;
408         struct irqaction * action;
409
410         seq_printf(p, "           ");
411         for (j=0; j<smp_num_cpus; j++)
412                 seq_printf(p, "CPU%d       ",j);
413         seq_putc(p, '\n');
414
415         for (i = 0 ; i < NR_IRQS ; i++) {
416                 action = irq_desc[i].action;
417                 if (!action || !action->handler)
418                         continue;
419                 seq_printf(p, "%3d: ", i);              
420 #ifdef CONFIG_SMP
421                 for (j = 0; j < smp_num_cpus; j++)
422                         seq_printf(p, "%10u ",
423                                 kstat.irqs[cpu_logical_map(j)][i]);
424 #else           
425                 seq_printf(p, "%10u ", kstat_irqs(i));
426 #endif /* CONFIG_SMP */
427                 if (irq_desc[i].handler)                
428                         seq_printf(p, " %s ", irq_desc[i].handler->typename );
429                 else
430                         seq_printf(p, "  None      ");
431                 seq_printf(p, "%s", (irq_desc[i].status & IRQ_LEVEL) ? "Level " : "Edge  ");
432                 seq_printf(p, "    %s",action->name);
433                 for (action=action->next; action; action = action->next)
434                         seq_printf(p, ", %s", action->name);
435                 seq_putc(p, '\n');
436         }
437 #ifdef CONFIG_SMP
438         /* should this be per processor send/receive? */
439         seq_printf(p, "IPI (recv/sent): %10u/%u\n",
440                        atomic_read(&ipi_recv), atomic_read(&ipi_sent));
441 #endif          
442         seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
443         return 0;
444 }
445
446 static inline void
447 handle_irq_event(int irq, struct pt_regs *regs, struct irqaction *action)
448 {
449         int status = 0;
450
451         if (!(action->flags & SA_INTERRUPT))
452                 __sti();
453
454         do {
455                 status |= action->flags;
456                 action->handler(irq, action->dev_id, regs);
457                 action = action->next;
458         } while (action);
459         if (status & SA_SAMPLE_RANDOM)
460                 add_interrupt_randomness(irq);
461         __cli();
462 }
463
464 /*
465  * Eventually, this should take an array of interrupts and an array size
466  * so it can dispatch multiple interrupts.
467  */
468 void ppc_irq_dispatch_handler(struct pt_regs *regs, int irq)
469 {
470         int status;
471         struct irqaction *action;
472         int cpu = smp_processor_id();
473         irq_desc_t *desc = irq_desc + irq;
474
475         kstat.irqs[cpu][irq]++;
476         spin_lock(&desc->lock);
477         ack_irq(irq);   
478         /*
479            REPLAY is when Linux resends an IRQ that was dropped earlier
480            WAITING is used by probe to mark irqs that are being tested
481            */
482         status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
483         if (!(status & IRQ_PER_CPU))
484                 status |= IRQ_PENDING; /* we _want_ to handle it */
485
486         /*
487          * If the IRQ is disabled for whatever reason, we cannot
488          * use the action we have.
489          */
490         action = NULL;
491         if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
492                 action = desc->action;
493                 if (!action || !action->handler) {
494                         ppc_spurious_interrupts++;
495                         printk(KERN_DEBUG "Unhandled interrupt %x, disabled\n", irq);
496                         /* We can't call disable_irq here, it would deadlock */
497                         if (!desc->depth)
498                                 desc->depth = 1;
499                         desc->status |= IRQ_DISABLED;
500                         /* This is not a real spurrious interrupt, we
501                          * have to eoi it, so we jump to out
502                          */
503                         mask_irq(irq);
504                         goto out;
505                 }
506                 status &= ~IRQ_PENDING; /* we commit to handling */
507                 if (!(status & IRQ_PER_CPU))
508                         status |= IRQ_INPROGRESS; /* we are handling it */
509         }
510         desc->status = status;
511
512         /*
513          * If there is no IRQ handler or it was disabled, exit early.
514            Since we set PENDING, if another processor is handling
515            a different instance of this same irq, the other processor
516            will take care of it.
517          */
518         if (!action)
519                 goto out;
520
521
522         /*
523          * Edge triggered interrupts need to remember
524          * pending events.
525          * This applies to any hw interrupts that allow a second
526          * instance of the same irq to arrive while we are in do_IRQ
527          * or in the handler. But the code here only handles the _second_
528          * instance of the irq, not the third or fourth. So it is mostly
529          * useful for irq hardware that does not mask cleanly in an
530          * SMP environment.
531          */
532         for (;;) {
533                 spin_unlock(&desc->lock);
534                 handle_irq_event(irq, regs, action);
535                 spin_lock(&desc->lock);
536                 
537                 if (!(desc->status & IRQ_PENDING))
538                         break;
539                 desc->status &= ~IRQ_PENDING;
540         }
541         desc->status &= ~IRQ_INPROGRESS;
542 out:
543         /*
544          * The ->end() handler has to deal with interrupts which got
545          * disabled while the handler was running.
546          */
547         if (irq_desc[irq].handler) {
548                 if (irq_desc[irq].handler->end)
549                         irq_desc[irq].handler->end(irq);
550                 else if (irq_desc[irq].handler->enable)
551                         irq_desc[irq].handler->enable(irq);
552         }
553         spin_unlock(&desc->lock);
554 }
555
556 int do_IRQ(struct pt_regs *regs)
557 {
558         int cpu = smp_processor_id();
559         int irq, first = 1;
560 #ifdef CONFIG_PPC_ISERIES
561         struct paca_struct *lpaca;
562         struct ItLpQueue *lpq;
563 #endif
564
565         irq_enter(cpu);
566
567 #ifdef CONFIG_PPC_ISERIES
568         lpaca = get_paca();
569 #ifdef CONFIG_SMP
570         if (lpaca->xLpPaca.xIntDword.xFields.xIpiCnt) {
571                 lpaca->xLpPaca.xIntDword.xFields.xIpiCnt = 0;
572                 iSeries_smp_message_recv(regs);
573         }
574 #endif /* CONFIG_SMP */
575         lpq = lpaca->lpQueuePtr;
576         if (lpq && ItLpQueue_isLpIntPending(lpq))
577                 lpEvent_count += ItLpQueue_process(lpq, regs);
578 #else
579         /*
580          * Every arch is required to implement ppc_md.get_irq.
581          * This function will either return an irq number or -1 to
582          * indicate there are no more pending.  But the first time
583          * through the loop this means there wasn't an IRQ pending.
584          * The value -2 is for buggy hardware and means that this IRQ
585          * has already been handled. -- Tom
586          */
587         while ((irq = ppc_md.get_irq(regs)) >= 0) {
588                 ppc_irq_dispatch_handler(regs, irq);
589                 first = 0;
590         }
591         if (irq != -2 && first)
592                 /* That's not SMP safe ... but who cares ? */
593                 ppc_spurious_interrupts++;
594 #endif
595
596         irq_exit(cpu);
597
598 #ifdef CONFIG_PPC_ISERIES
599         if (lpaca->xLpPaca.xIntDword.xFields.xDecrInt) {
600                 lpaca->xLpPaca.xIntDword.xFields.xDecrInt = 0;
601                 /* Signal a fake decrementer interrupt */
602                 timer_interrupt(regs);
603         }
604
605         if (lpaca->xLpPaca.xIntDword.xFields.xPdcInt) {
606                 lpaca->xLpPaca.xIntDword.xFields.xPdcInt = 0;
607                 /* Signal a fake PMC interrupt */
608                 PerformanceMonitorException();
609         }
610 #endif
611
612         if (softirq_pending(cpu))
613                 do_softirq();
614
615         return 1; /* lets ret_from_int know we can do checks */
616 }
617
618 unsigned long probe_irq_on (void)
619 {
620         return 0;
621 }
622
623 int probe_irq_off (unsigned long irqs)
624 {
625         return 0;
626 }
627
628 unsigned int probe_irq_mask(unsigned long irqs)
629 {
630         return 0;
631 }
632
633 void __init init_IRQ(void)
634 {
635         static int once = 0;
636
637         if ( once )
638                 return;
639         else
640                 once++;
641         
642         ppc_md.init_IRQ();
643         if(ppc_md.init_ras_IRQ) ppc_md.init_ras_IRQ(); 
644 }
645
646 #ifdef CONFIG_SMP
647 unsigned char global_irq_holder = NO_PROC_ID;
648
649 static void show(char * str)
650 {
651         int cpu = smp_processor_id();
652         int i;
653
654         printk("\n%s, CPU %d:\n", str, cpu);
655         printk("irq:  %d [ ", irqs_running());
656         for (i = 0; i < smp_num_cpus; i++)
657                 printk("%u ", __brlock_array[i][BR_GLOBALIRQ_LOCK]);
658         printk("]\nbh:   %d [ ",
659                 (spin_is_locked(&global_bh_lock) ? 1 : 0));
660         for (i = 0; i < smp_num_cpus; i++)
661                 printk("%u ", local_bh_count(i));
662         printk("]\n");
663 }
664
665 #define MAXCOUNT 10000000
666
667 void synchronize_irq(void)
668 {
669         if (irqs_running()) {
670                 cli();
671                 sti();
672         }
673 }
674
675 static inline void get_irqlock(int cpu)
676 {
677         int count;
678
679         if ((unsigned char)cpu == global_irq_holder)
680                 return;
681
682         count = MAXCOUNT;
683 again:
684         br_write_lock(BR_GLOBALIRQ_LOCK);
685         for (;;) {
686                 spinlock_t *lock;
687
688                 if (!irqs_running() &&
689                     (local_bh_count(smp_processor_id()) || !spin_is_locked(&global_bh_lock)))
690                         break;
691
692                 br_write_unlock(BR_GLOBALIRQ_LOCK);
693                 lock = &__br_write_locks[BR_GLOBALIRQ_LOCK].lock;
694                 while (irqs_running() ||
695                        spin_is_locked(lock) ||
696                        (!local_bh_count(smp_processor_id()) && spin_is_locked(&global_bh_lock))) {
697                         if (!--count) {
698                                 show("get_irqlock");
699                                 count = (~0 >> 1);
700                         }
701                         __sti();
702                         barrier();
703                         __cli();
704                 }
705                 goto again;
706         }
707
708         global_irq_holder = cpu;
709 }
710
711 /*
712  * A global "cli()" while in an interrupt context
713  * turns into just a local cli(). Interrupts
714  * should use spinlocks for the (very unlikely)
715  * case that they ever want to protect against
716  * each other.
717  *
718  * If we already have local interrupts disabled,
719  * this will not turn a local disable into a
720  * global one (problems with spinlocks: this makes
721  * save_flags+cli+sti usable inside a spinlock).
722  */
723 void __global_cli(void)
724 {
725         unsigned long flags;
726         
727         __save_flags(flags);
728         if (flags & (1UL << 15)) {
729                 int cpu = smp_processor_id();
730                 __cli();
731                 if (!local_irq_count(cpu))
732                         get_irqlock(cpu);
733         }
734 }
735
736 void __global_sti(void)
737 {
738         int cpu = smp_processor_id();
739
740         if (!local_irq_count(cpu))
741                 release_irqlock(cpu);
742         __sti();
743 }
744
745 /*
746  * SMP flags value to restore to:
747  * 0 - global cli
748  * 1 - global sti
749  * 2 - local cli
750  * 3 - local sti
751  */
752 unsigned long __global_save_flags(void)
753 {
754         int retval;
755         int local_enabled;
756         unsigned long flags;
757
758         __save_flags(flags);
759         local_enabled = (flags >> 15) & 1;
760         /* default to local */
761         retval = 2 + local_enabled;
762
763         /* check for global flags if we're not in an interrupt */
764         if (!local_irq_count(smp_processor_id())) {
765                 if (local_enabled)
766                         retval = 1;
767                 if (global_irq_holder == (unsigned char) smp_processor_id())
768                         retval = 0;
769         }
770         return retval;
771 }
772
773 void __global_restore_flags(unsigned long flags)
774 {
775         switch (flags) {
776         case 0:
777                 __global_cli();
778                 break;
779         case 1:
780                 __global_sti();
781                 break;
782         case 2:
783                 __cli();
784                 break;
785         case 3:
786                 __sti();
787                 break;
788         default:
789                 printk("global_restore_flags: %016lx caller %p\n",
790                         flags, __builtin_return_address(0));
791         }
792 }
793
794 #endif /* CONFIG_SMP */
795
796 static struct proc_dir_entry * root_irq_dir;
797 static struct proc_dir_entry * irq_dir [NR_IRQS];
798 static struct proc_dir_entry * smp_affinity_entry [NR_IRQS];
799
800 #ifdef CONFIG_IRQ_ALL_CPUS
801 unsigned int irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = 0xffffffff};
802 #else  /* CONFIG_IRQ_ALL_CPUS */
803 unsigned int irq_affinity [NR_IRQS] = { [0 ... NR_IRQS-1] = 0x00000000};
804 #endif /* CONFIG_IRQ_ALL_CPUS */
805
806 #define HEX_DIGITS 8
807
808 static int irq_affinity_read_proc (char *page, char **start, off_t off,
809                         int count, int *eof, void *data)
810 {
811         if (count < HEX_DIGITS+1)
812                 return -EINVAL;
813         return sprintf (page, "%08x\n", irq_affinity[(int)(long)data]);
814 }
815
816 static unsigned int parse_hex_value (const char *buffer,
817                 unsigned long count, unsigned long *ret)
818 {
819         unsigned char hexnum [HEX_DIGITS];
820         unsigned long value;
821         int i;
822
823         if (!count)
824                 return -EINVAL;
825         if (count > HEX_DIGITS)
826                 count = HEX_DIGITS;
827         if (copy_from_user(hexnum, buffer, count))
828                 return -EFAULT;
829
830         /*
831          * Parse the first 8 characters as a hex string, any non-hex char
832          * is end-of-string. '00e1', 'e1', '00E1', 'E1' are all the same.
833          */
834         value = 0;
835
836         for (i = 0; i < count; i++) {
837                 unsigned int c = hexnum[i];
838
839                 switch (c) {
840                         case '0' ... '9': c -= '0'; break;
841                         case 'a' ... 'f': c -= 'a'-10; break;
842                         case 'A' ... 'F': c -= 'A'-10; break;
843                 default:
844                         goto out;
845                 }
846                 value = (value << 4) | c;
847         }
848 out:
849         *ret = value;
850         return 0;
851 }
852
853 static int irq_affinity_write_proc (struct file *file, const char *buffer,
854                                         unsigned long count, void *data)
855 {
856         int irq = (int)(long) data, full_count = count, err;
857         unsigned long new_value;
858
859         if (!irq_desc[irq].handler->set_affinity)
860                 return -EIO;
861
862         err = parse_hex_value(buffer, count, &new_value);
863
864 /* Why is this disabled ? --BenH */
865 #if 0/*CONFIG_SMP*/
866         /*
867          * Do not allow disabling IRQs completely - it's a too easy
868          * way to make the system unusable accidentally :-) At least
869          * one online CPU still has to be targeted.
870          */
871         if (!(new_value & cpu_online_map))
872                 return -EINVAL;
873 #endif
874
875         irq_affinity[irq] = new_value;
876         irq_desc[irq].handler->set_affinity(irq, new_value);
877
878         return full_count;
879 }
880
881 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
882                         int count, int *eof, void *data)
883 {
884         unsigned long *mask = (unsigned long *) data;
885         if (count < HEX_DIGITS+1)
886                 return -EINVAL;
887         return sprintf (page, "%08lx\n", *mask);
888 }
889
890 static int prof_cpu_mask_write_proc (struct file *file, const char *buffer,
891                                         unsigned long count, void *data)
892 {
893         unsigned long *mask = (unsigned long *) data, full_count = count, err;
894         unsigned long new_value;
895
896         err = parse_hex_value(buffer, count, &new_value);
897         if (err)
898                 return err;
899
900         *mask = new_value;
901
902 #ifdef CONFIG_PPC_ISERIES
903         {
904                 unsigned i;
905                 for (i=0; i<MAX_PACAS; ++i) {
906                         if ( paca[i].prof_buffer && (new_value & 1) )
907                                 paca[i].prof_mode = PMC_STATE_DECR_PROFILE;
908                         else {
909                                 if(paca[i].prof_mode != PMC_STATE_INITIAL) 
910                                         paca[i].prof_mode = PMC_STATE_READY;
911                         }
912                         new_value >>= 1;
913                 }
914         }
915 #endif
916
917         return full_count;
918 }
919
920 #define MAX_NAMELEN 10
921
922 static void register_irq_proc (unsigned int irq)
923 {
924         struct proc_dir_entry *entry;
925         char name [MAX_NAMELEN];
926
927         if (!root_irq_dir || (irq_desc[irq].handler == NULL))
928                 return;
929
930         memset(name, 0, MAX_NAMELEN);
931         sprintf(name, "%d", irq);
932
933         /* create /proc/irq/1234 */
934         irq_dir[irq] = proc_mkdir(name, root_irq_dir);
935
936         /* create /proc/irq/1234/smp_affinity */
937         entry = create_proc_entry("smp_affinity", 0600, irq_dir[irq]);
938
939         entry->nlink = 1;
940         entry->data = (void *)(long)irq;
941         entry->read_proc = irq_affinity_read_proc;
942         entry->write_proc = irq_affinity_write_proc;
943
944         smp_affinity_entry[irq] = entry;
945 }
946
947 unsigned long prof_cpu_mask = -1;
948
949 void init_irq_proc (void)
950 {
951         struct proc_dir_entry *entry;
952         int i;
953
954         /* create /proc/irq */
955         root_irq_dir = proc_mkdir("irq", 0);
956
957         /* create /proc/irq/prof_cpu_mask */
958         entry = create_proc_entry("prof_cpu_mask", 0600, root_irq_dir);
959
960         entry->nlink = 1;
961         entry->data = (void *)&prof_cpu_mask;
962         entry->read_proc = prof_cpu_mask_read_proc;
963         entry->write_proc = prof_cpu_mask_write_proc;
964
965         /*
966          * Create entries for all existing IRQs.
967          */
968         for (i = 0; i < NR_IRQS; i++) {
969                 if (irq_desc[i].handler == NULL)
970                         continue;
971                 register_irq_proc(i);
972         }
973 }
974
975 void no_action(int irq, void *dev, struct pt_regs *regs)
976 {
977 }