]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/sparc/kernel/irq_64.c
sparc: Cleanup direct irq_desc access
[karo-tx-linux.git] / arch / sparc / kernel / irq_64.c
1 /* irq.c: UltraSparc IRQ handling/init/registry.
2  *
3  * Copyright (C) 1997, 2007, 2008 David S. Miller (davem@davemloft.net)
4  * Copyright (C) 1998  Eddie C. Dost    (ecd@skynet.be)
5  * Copyright (C) 1998  Jakub Jelinek    (jj@ultra.linux.cz)
6  */
7
8 #include <linux/module.h>
9 #include <linux/sched.h>
10 #include <linux/linkage.h>
11 #include <linux/ptrace.h>
12 #include <linux/errno.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/signal.h>
15 #include <linux/mm.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/random.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/ftrace.h>
24 #include <linux/irq.h>
25 #include <linux/kmemleak.h>
26
27 #include <asm/ptrace.h>
28 #include <asm/processor.h>
29 #include <asm/atomic.h>
30 #include <asm/system.h>
31 #include <asm/irq.h>
32 #include <asm/io.h>
33 #include <asm/iommu.h>
34 #include <asm/upa.h>
35 #include <asm/oplib.h>
36 #include <asm/prom.h>
37 #include <asm/timer.h>
38 #include <asm/smp.h>
39 #include <asm/starfire.h>
40 #include <asm/uaccess.h>
41 #include <asm/cache.h>
42 #include <asm/cpudata.h>
43 #include <asm/auxio.h>
44 #include <asm/head.h>
45 #include <asm/hypervisor.h>
46 #include <asm/cacheflush.h>
47
48 #include "entry.h"
49 #include "cpumap.h"
50 #include "kstack.h"
51
52 #define NUM_IVECS       (IMAP_INR + 1)
53
54 struct ino_bucket *ivector_table;
55 unsigned long ivector_table_pa;
56
57 /* On several sun4u processors, it is illegal to mix bypass and
58  * non-bypass accesses.  Therefore we access all INO buckets
59  * using bypass accesses only.
60  */
61 static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
62 {
63         unsigned long ret;
64
65         __asm__ __volatile__("ldxa      [%1] %2, %0"
66                              : "=&r" (ret)
67                              : "r" (bucket_pa +
68                                     offsetof(struct ino_bucket,
69                                              __irq_chain_pa)),
70                                "i" (ASI_PHYS_USE_EC));
71
72         return ret;
73 }
74
75 static void bucket_clear_chain_pa(unsigned long bucket_pa)
76 {
77         __asm__ __volatile__("stxa      %%g0, [%0] %1"
78                              : /* no outputs */
79                              : "r" (bucket_pa +
80                                     offsetof(struct ino_bucket,
81                                              __irq_chain_pa)),
82                                "i" (ASI_PHYS_USE_EC));
83 }
84
85 static unsigned int bucket_get_irq(unsigned long bucket_pa)
86 {
87         unsigned int ret;
88
89         __asm__ __volatile__("lduwa     [%1] %2, %0"
90                              : "=&r" (ret)
91                              : "r" (bucket_pa +
92                                     offsetof(struct ino_bucket,
93                                              __irq)),
94                                "i" (ASI_PHYS_USE_EC));
95
96         return ret;
97 }
98
99 static void bucket_set_irq(unsigned long bucket_pa, unsigned int irq)
100 {
101         __asm__ __volatile__("stwa      %0, [%1] %2"
102                              : /* no outputs */
103                              : "r" (irq),
104                                "r" (bucket_pa +
105                                     offsetof(struct ino_bucket,
106                                              __irq)),
107                                "i" (ASI_PHYS_USE_EC));
108 }
109
110 #define irq_work_pa(__cpu)      &(trap_block[(__cpu)].irq_worklist_pa)
111
112 static struct {
113         unsigned int dev_handle;
114         unsigned int dev_ino;
115         unsigned int in_use;
116 } irq_table[NR_IRQS];
117 static DEFINE_SPINLOCK(irq_alloc_lock);
118
119 unsigned char irq_alloc(unsigned int dev_handle, unsigned int dev_ino)
120 {
121         unsigned long flags;
122         unsigned char ent;
123
124         BUILD_BUG_ON(NR_IRQS >= 256);
125
126         spin_lock_irqsave(&irq_alloc_lock, flags);
127
128         for (ent = 1; ent < NR_IRQS; ent++) {
129                 if (!irq_table[ent].in_use)
130                         break;
131         }
132         if (ent >= NR_IRQS) {
133                 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
134                 ent = 0;
135         } else {
136                 irq_table[ent].dev_handle = dev_handle;
137                 irq_table[ent].dev_ino = dev_ino;
138                 irq_table[ent].in_use = 1;
139         }
140
141         spin_unlock_irqrestore(&irq_alloc_lock, flags);
142
143         return ent;
144 }
145
146 #ifdef CONFIG_PCI_MSI
147 void irq_free(unsigned int irq)
148 {
149         unsigned long flags;
150
151         if (irq >= NR_IRQS)
152                 return;
153
154         spin_lock_irqsave(&irq_alloc_lock, flags);
155
156         irq_table[irq].in_use = 0;
157
158         spin_unlock_irqrestore(&irq_alloc_lock, flags);
159 }
160 #endif
161
162 /*
163  * /proc/interrupts printing:
164  */
165
166 int show_interrupts(struct seq_file *p, void *v)
167 {
168         int i = *(loff_t *) v, j;
169         struct irqaction * action;
170         unsigned long flags;
171
172         if (i == 0) {
173                 seq_printf(p, "           ");
174                 for_each_online_cpu(j)
175                         seq_printf(p, "CPU%d       ",j);
176                 seq_putc(p, '\n');
177         }
178
179         if (i < NR_IRQS) {
180                 raw_spin_lock_irqsave(&irq_desc[i].lock, flags);
181                 action = irq_desc[i].action;
182                 if (!action)
183                         goto skip;
184                 seq_printf(p, "%3d: ",i);
185 #ifndef CONFIG_SMP
186                 seq_printf(p, "%10u ", kstat_irqs(i));
187 #else
188                 for_each_online_cpu(j)
189                         seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
190 #endif
191                 seq_printf(p, " %9s", irq_desc[i].irq_data.chip->name);
192                 seq_printf(p, "  %s", action->name);
193
194                 for (action=action->next; action; action = action->next)
195                         seq_printf(p, ", %s", action->name);
196
197                 seq_putc(p, '\n');
198 skip:
199                 raw_spin_unlock_irqrestore(&irq_desc[i].lock, flags);
200         } else if (i == NR_IRQS) {
201                 seq_printf(p, "NMI: ");
202                 for_each_online_cpu(j)
203                         seq_printf(p, "%10u ", cpu_data(j).__nmi_count);
204                 seq_printf(p, "     Non-maskable interrupts\n");
205         }
206         return 0;
207 }
208
209 static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
210 {
211         unsigned int tid;
212
213         if (this_is_starfire) {
214                 tid = starfire_translate(imap, cpuid);
215                 tid <<= IMAP_TID_SHIFT;
216                 tid &= IMAP_TID_UPA;
217         } else {
218                 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
219                         unsigned long ver;
220
221                         __asm__ ("rdpr %%ver, %0" : "=r" (ver));
222                         if ((ver >> 32UL) == __JALAPENO_ID ||
223                             (ver >> 32UL) == __SERRANO_ID) {
224                                 tid = cpuid << IMAP_TID_SHIFT;
225                                 tid &= IMAP_TID_JBUS;
226                         } else {
227                                 unsigned int a = cpuid & 0x1f;
228                                 unsigned int n = (cpuid >> 5) & 0x1f;
229
230                                 tid = ((a << IMAP_AID_SHIFT) |
231                                        (n << IMAP_NID_SHIFT));
232                                 tid &= (IMAP_AID_SAFARI |
233                                         IMAP_NID_SAFARI);
234                         }
235                 } else {
236                         tid = cpuid << IMAP_TID_SHIFT;
237                         tid &= IMAP_TID_UPA;
238                 }
239         }
240
241         return tid;
242 }
243
244 struct irq_handler_data {
245         unsigned long   iclr;
246         unsigned long   imap;
247
248         void            (*pre_handler)(unsigned int, void *, void *);
249         void            *arg1;
250         void            *arg2;
251 };
252
253 #ifdef CONFIG_SMP
254 static int irq_choose_cpu(unsigned int irq, const struct cpumask *affinity)
255 {
256         cpumask_t mask;
257         int cpuid;
258
259         cpumask_copy(&mask, affinity);
260         if (cpus_equal(mask, cpu_online_map)) {
261                 cpuid = map_to_cpu(irq);
262         } else {
263                 cpumask_t tmp;
264
265                 cpus_and(tmp, cpu_online_map, mask);
266                 cpuid = cpus_empty(tmp) ? map_to_cpu(irq) : first_cpu(tmp);
267         }
268
269         return cpuid;
270 }
271 #else
272 #define irq_choose_cpu(irq, affinity)   \
273         real_hard_smp_processor_id()
274 #endif
275
276 static void sun4u_irq_enable(struct irq_data *data)
277 {
278         struct irq_handler_data *handler_data = data->handler_data;
279
280         if (likely(handler_data)) {
281                 unsigned long cpuid, imap, val;
282                 unsigned int tid;
283
284                 cpuid = irq_choose_cpu(data->irq, data->affinity);
285                 imap = handler_data->imap;
286
287                 tid = sun4u_compute_tid(imap, cpuid);
288
289                 val = upa_readq(imap);
290                 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
291                          IMAP_AID_SAFARI | IMAP_NID_SAFARI);
292                 val |= tid | IMAP_VALID;
293                 upa_writeq(val, imap);
294                 upa_writeq(ICLR_IDLE, handler_data->iclr);
295         }
296 }
297
298 static int sun4u_set_affinity(struct irq_data *data,
299                                const struct cpumask *mask, bool force)
300 {
301         struct irq_handler_data *handler_data = data->handler_data;
302
303         if (likely(handler_data)) {
304                 unsigned long cpuid, imap, val;
305                 unsigned int tid;
306
307                 cpuid = irq_choose_cpu(data->irq, mask);
308                 imap = handler_data->imap;
309
310                 tid = sun4u_compute_tid(imap, cpuid);
311
312                 val = upa_readq(imap);
313                 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
314                          IMAP_AID_SAFARI | IMAP_NID_SAFARI);
315                 val |= tid | IMAP_VALID;
316                 upa_writeq(val, imap);
317                 upa_writeq(ICLR_IDLE, handler_data->iclr);
318         }
319
320         return 0;
321 }
322
323 /* Don't do anything.  The desc->status check for IRQ_DISABLED in
324  * handler_irq() will skip the handler call and that will leave the
325  * interrupt in the sent state.  The next ->enable() call will hit the
326  * ICLR register to reset the state machine.
327  *
328  * This scheme is necessary, instead of clearing the Valid bit in the
329  * IMAP register, to handle the case of IMAP registers being shared by
330  * multiple INOs (and thus ICLR registers).  Since we use a different
331  * virtual IRQ for each shared IMAP instance, the generic code thinks
332  * there is only one user so it prematurely calls ->disable() on
333  * free_irq().
334  *
335  * We have to provide an explicit ->disable() method instead of using
336  * NULL to get the default.  The reason is that if the generic code
337  * sees that, it also hooks up a default ->shutdown method which
338  * invokes ->mask() which we do not want.  See irq_chip_set_defaults().
339  */
340 static void sun4u_irq_disable(struct irq_data *data)
341 {
342 }
343
344 static void sun4u_irq_eoi(struct irq_data *data)
345 {
346         struct irq_handler_data *handler_data = data->handler_data;
347
348         if (likely(handler_data))
349                 upa_writeq(ICLR_IDLE, handler_data->iclr);
350 }
351
352 static void sun4v_irq_enable(struct irq_data *data)
353 {
354         unsigned int ino = irq_table[data->irq].dev_ino;
355         unsigned long cpuid = irq_choose_cpu(data->irq, data->affinity);
356         int err;
357
358         err = sun4v_intr_settarget(ino, cpuid);
359         if (err != HV_EOK)
360                 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
361                        "err(%d)\n", ino, cpuid, err);
362         err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
363         if (err != HV_EOK)
364                 printk(KERN_ERR "sun4v_intr_setstate(%x): "
365                        "err(%d)\n", ino, err);
366         err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
367         if (err != HV_EOK)
368                 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
369                        ino, err);
370 }
371
372 static int sun4v_set_affinity(struct irq_data *data,
373                                const struct cpumask *mask, bool force)
374 {
375         unsigned int ino = irq_table[data->irq].dev_ino;
376         unsigned long cpuid = irq_choose_cpu(data->irq, mask);
377         int err;
378
379         err = sun4v_intr_settarget(ino, cpuid);
380         if (err != HV_EOK)
381                 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
382                        "err(%d)\n", ino, cpuid, err);
383
384         return 0;
385 }
386
387 static void sun4v_irq_disable(struct irq_data *data)
388 {
389         unsigned int ino = irq_table[data->irq].dev_ino;
390         int err;
391
392         err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
393         if (err != HV_EOK)
394                 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
395                        "err(%d)\n", ino, err);
396 }
397
398 static void sun4v_irq_eoi(struct irq_data *data)
399 {
400         unsigned int ino = irq_table[data->irq].dev_ino;
401         int err;
402
403         err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
404         if (err != HV_EOK)
405                 printk(KERN_ERR "sun4v_intr_setstate(%x): "
406                        "err(%d)\n", ino, err);
407 }
408
409 static void sun4v_virq_enable(struct irq_data *data)
410 {
411         unsigned long cpuid, dev_handle, dev_ino;
412         int err;
413
414         cpuid = irq_choose_cpu(data->irq, data->affinity);
415
416         dev_handle = irq_table[data->irq].dev_handle;
417         dev_ino = irq_table[data->irq].dev_ino;
418
419         err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
420         if (err != HV_EOK)
421                 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
422                        "err(%d)\n",
423                        dev_handle, dev_ino, cpuid, err);
424         err = sun4v_vintr_set_state(dev_handle, dev_ino,
425                                     HV_INTR_STATE_IDLE);
426         if (err != HV_EOK)
427                 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
428                        "HV_INTR_STATE_IDLE): err(%d)\n",
429                        dev_handle, dev_ino, err);
430         err = sun4v_vintr_set_valid(dev_handle, dev_ino,
431                                     HV_INTR_ENABLED);
432         if (err != HV_EOK)
433                 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
434                        "HV_INTR_ENABLED): err(%d)\n",
435                        dev_handle, dev_ino, err);
436 }
437
438 static int sun4v_virt_set_affinity(struct irq_data *data,
439                                     const struct cpumask *mask, bool force)
440 {
441         unsigned long cpuid, dev_handle, dev_ino;
442         int err;
443
444         cpuid = irq_choose_cpu(data->irq, mask);
445
446         dev_handle = irq_table[data->irq].dev_handle;
447         dev_ino = irq_table[data->irq].dev_ino;
448
449         err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
450         if (err != HV_EOK)
451                 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
452                        "err(%d)\n",
453                        dev_handle, dev_ino, cpuid, err);
454
455         return 0;
456 }
457
458 static void sun4v_virq_disable(struct irq_data *data)
459 {
460         unsigned long dev_handle, dev_ino;
461         int err;
462
463         dev_handle = irq_table[data->irq].dev_handle;
464         dev_ino = irq_table[data->irq].dev_ino;
465
466         err = sun4v_vintr_set_valid(dev_handle, dev_ino,
467                                     HV_INTR_DISABLED);
468         if (err != HV_EOK)
469                 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
470                        "HV_INTR_DISABLED): err(%d)\n",
471                        dev_handle, dev_ino, err);
472 }
473
474 static void sun4v_virq_eoi(struct irq_data *data)
475 {
476         unsigned long dev_handle, dev_ino;
477         int err;
478
479         dev_handle = irq_table[data->irq].dev_handle;
480         dev_ino = irq_table[data->irq].dev_ino;
481
482         err = sun4v_vintr_set_state(dev_handle, dev_ino,
483                                     HV_INTR_STATE_IDLE);
484         if (err != HV_EOK)
485                 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
486                        "HV_INTR_STATE_IDLE): err(%d)\n",
487                        dev_handle, dev_ino, err);
488 }
489
490 static struct irq_chip sun4u_irq = {
491         .name                   = "sun4u",
492         .irq_enable             = sun4u_irq_enable,
493         .irq_disable            = sun4u_irq_disable,
494         .irq_eoi                = sun4u_irq_eoi,
495         .irq_set_affinity       = sun4u_set_affinity,
496         .flags                  = IRQCHIP_EOI_IF_HANDLED,
497 };
498
499 static struct irq_chip sun4v_irq = {
500         .name                   = "sun4v",
501         .irq_enable             = sun4v_irq_enable,
502         .irq_disable            = sun4v_irq_disable,
503         .irq_eoi                = sun4v_irq_eoi,
504         .irq_set_affinity       = sun4v_set_affinity,
505         .flags                  = IRQCHIP_EOI_IF_HANDLED,
506 };
507
508 static struct irq_chip sun4v_virq = {
509         .name                   = "vsun4v",
510         .irq_enable             = sun4v_virq_enable,
511         .irq_disable            = sun4v_virq_disable,
512         .irq_eoi                = sun4v_virq_eoi,
513         .irq_set_affinity       = sun4v_virt_set_affinity,
514         .flags                  = IRQCHIP_EOI_IF_HANDLED,
515 };
516
517 static void pre_flow_handler(struct irq_data *d)
518 {
519         struct irq_handler_data *handler_data = irq_data_get_irq_handler_data(d);
520         unsigned int ino = irq_table[d->irq].dev_ino;
521
522         handler_data->pre_handler(ino, handler_data->arg1, handler_data->arg2);
523 }
524
525 void irq_install_pre_handler(int irq,
526                              void (*func)(unsigned int, void *, void *),
527                              void *arg1, void *arg2)
528 {
529         struct irq_handler_data *handler_data = get_irq_data(irq);
530
531         handler_data->pre_handler = func;
532         handler_data->arg1 = arg1;
533         handler_data->arg2 = arg2;
534
535         __irq_set_preflow_handler(irq, pre_flow_handler);
536 }
537
538 unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
539 {
540         struct ino_bucket *bucket;
541         struct irq_handler_data *handler_data;
542         unsigned int irq;
543         int ino;
544
545         BUG_ON(tlb_type == hypervisor);
546
547         ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
548         bucket = &ivector_table[ino];
549         irq = bucket_get_irq(__pa(bucket));
550         if (!irq) {
551                 irq = irq_alloc(0, ino);
552                 bucket_set_irq(__pa(bucket), irq);
553                 set_irq_chip_and_handler_name(irq,
554                                               &sun4u_irq,
555                                               handle_fasteoi_irq,
556                                               "IVEC");
557         }
558
559         handler_data = get_irq_data(irq);
560         if (unlikely(handler_data))
561                 goto out;
562
563         handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
564         if (unlikely(!handler_data)) {
565                 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
566                 prom_halt();
567         }
568         set_irq_data(irq, handler_data);
569
570         handler_data->imap  = imap;
571         handler_data->iclr  = iclr;
572
573 out:
574         return irq;
575 }
576
577 static unsigned int sun4v_build_common(unsigned long sysino,
578                                        struct irq_chip *chip)
579 {
580         struct ino_bucket *bucket;
581         struct irq_handler_data *handler_data;
582         unsigned int irq;
583
584         BUG_ON(tlb_type != hypervisor);
585
586         bucket = &ivector_table[sysino];
587         irq = bucket_get_irq(__pa(bucket));
588         if (!irq) {
589                 irq = irq_alloc(0, sysino);
590                 bucket_set_irq(__pa(bucket), irq);
591                 set_irq_chip_and_handler_name(irq, chip,
592                                               handle_fasteoi_irq,
593                                               "IVEC");
594         }
595
596         handler_data = get_irq_data(irq);
597         if (unlikely(handler_data))
598                 goto out;
599
600         handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
601         if (unlikely(!handler_data)) {
602                 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
603                 prom_halt();
604         }
605         set_irq_data(irq, handler_data);
606
607         /* Catch accidental accesses to these things.  IMAP/ICLR handling
608          * is done by hypervisor calls on sun4v platforms, not by direct
609          * register accesses.
610          */
611         handler_data->imap = ~0UL;
612         handler_data->iclr = ~0UL;
613
614 out:
615         return irq;
616 }
617
618 unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
619 {
620         unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
621
622         return sun4v_build_common(sysino, &sun4v_irq);
623 }
624
625 unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
626 {
627         struct irq_handler_data *handler_data;
628         unsigned long hv_err, cookie;
629         struct ino_bucket *bucket;
630         unsigned int irq;
631
632         bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
633         if (unlikely(!bucket))
634                 return 0;
635
636         /* The only reference we store to the IRQ bucket is
637          * by physical address which kmemleak can't see, tell
638          * it that this object explicitly is not a leak and
639          * should be scanned.
640          */
641         kmemleak_not_leak(bucket);
642
643         __flush_dcache_range((unsigned long) bucket,
644                              ((unsigned long) bucket +
645                               sizeof(struct ino_bucket)));
646
647         irq = irq_alloc(devhandle, devino);
648         bucket_set_irq(__pa(bucket), irq);
649
650         set_irq_chip_and_handler_name(irq, &sun4v_virq,
651                                       handle_fasteoi_irq,
652                                       "IVEC");
653
654         handler_data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
655         if (unlikely(!handler_data))
656                 return 0;
657
658         /* In order to make the LDC channel startup sequence easier,
659          * especially wrt. locking, we do not let request_irq() enable
660          * the interrupt.
661          */
662         irq_set_status_flags(irq, IRQ_NOAUTOEN);
663         set_irq_data(irq, handler_data);
664
665         /* Catch accidental accesses to these things.  IMAP/ICLR handling
666          * is done by hypervisor calls on sun4v platforms, not by direct
667          * register accesses.
668          */
669         handler_data->imap = ~0UL;
670         handler_data->iclr = ~0UL;
671
672         cookie = ~__pa(bucket);
673         hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
674         if (hv_err) {
675                 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
676                             "err=%lu\n", devhandle, devino, hv_err);
677                 prom_halt();
678         }
679
680         return irq;
681 }
682
683 void ack_bad_irq(unsigned int irq)
684 {
685         unsigned int ino = irq_table[irq].dev_ino;
686
687         if (!ino)
688                 ino = 0xdeadbeef;
689
690         printk(KERN_CRIT "Unexpected IRQ from ino[%x] irq[%u]\n",
691                ino, irq);
692 }
693
694 void *hardirq_stack[NR_CPUS];
695 void *softirq_stack[NR_CPUS];
696
697 void __irq_entry handler_irq(int pil, struct pt_regs *regs)
698 {
699         unsigned long pstate, bucket_pa;
700         struct pt_regs *old_regs;
701         void *orig_sp;
702
703         clear_softint(1 << pil);
704
705         old_regs = set_irq_regs(regs);
706         irq_enter();
707
708         /* Grab an atomic snapshot of the pending IVECs.  */
709         __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
710                              "wrpr      %0, %3, %%pstate\n\t"
711                              "ldx       [%2], %1\n\t"
712                              "stx       %%g0, [%2]\n\t"
713                              "wrpr      %0, 0x0, %%pstate\n\t"
714                              : "=&r" (pstate), "=&r" (bucket_pa)
715                              : "r" (irq_work_pa(smp_processor_id())),
716                                "i" (PSTATE_IE)
717                              : "memory");
718
719         orig_sp = set_hardirq_stack();
720
721         while (bucket_pa) {
722                 unsigned long next_pa;
723                 unsigned int irq;
724
725                 next_pa = bucket_get_chain_pa(bucket_pa);
726                 irq = bucket_get_irq(bucket_pa);
727                 bucket_clear_chain_pa(bucket_pa);
728
729                 generic_handle_irq(irq);
730
731                 bucket_pa = next_pa;
732         }
733
734         restore_hardirq_stack(orig_sp);
735
736         irq_exit();
737         set_irq_regs(old_regs);
738 }
739
740 void do_softirq(void)
741 {
742         unsigned long flags;
743
744         if (in_interrupt())
745                 return;
746
747         local_irq_save(flags);
748
749         if (local_softirq_pending()) {
750                 void *orig_sp, *sp = softirq_stack[smp_processor_id()];
751
752                 sp += THREAD_SIZE - 192 - STACK_BIAS;
753
754                 __asm__ __volatile__("mov %%sp, %0\n\t"
755                                      "mov %1, %%sp"
756                                      : "=&r" (orig_sp)
757                                      : "r" (sp));
758                 __do_softirq();
759                 __asm__ __volatile__("mov %0, %%sp"
760                                      : : "r" (orig_sp));
761         }
762
763         local_irq_restore(flags);
764 }
765
766 #ifdef CONFIG_HOTPLUG_CPU
767 void fixup_irqs(void)
768 {
769         unsigned int irq;
770
771         for (irq = 0; irq < NR_IRQS; irq++) {
772                 struct irq_desc *desc = irq_to_desc(irq);
773                 struct irq_data *data = irq_desc_get_irq_data(desc);
774                 unsigned long flags;
775
776                 raw_spin_lock_irqsave(&desc->lock, flags);
777                 if (desc->action && !irqd_is_per_cpu(data)) {
778                         if (data->chip->irq_set_affinity)
779                                 data->chip->irq_set_affinity(data,
780                                                              data->affinity,
781                                                              false);
782                 }
783                 raw_spin_unlock_irqrestore(&desc->lock, flags);
784         }
785
786         tick_ops->disable_irq();
787 }
788 #endif
789
790 struct sun5_timer {
791         u64     count0;
792         u64     limit0;
793         u64     count1;
794         u64     limit1;
795 };
796
797 static struct sun5_timer *prom_timers;
798 static u64 prom_limit0, prom_limit1;
799
800 static void map_prom_timers(void)
801 {
802         struct device_node *dp;
803         const unsigned int *addr;
804
805         /* PROM timer node hangs out in the top level of device siblings... */
806         dp = of_find_node_by_path("/");
807         dp = dp->child;
808         while (dp) {
809                 if (!strcmp(dp->name, "counter-timer"))
810                         break;
811                 dp = dp->sibling;
812         }
813
814         /* Assume if node is not present, PROM uses different tick mechanism
815          * which we should not care about.
816          */
817         if (!dp) {
818                 prom_timers = (struct sun5_timer *) 0;
819                 return;
820         }
821
822         /* If PROM is really using this, it must be mapped by him. */
823         addr = of_get_property(dp, "address", NULL);
824         if (!addr) {
825                 prom_printf("PROM does not have timer mapped, trying to continue.\n");
826                 prom_timers = (struct sun5_timer *) 0;
827                 return;
828         }
829         prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
830 }
831
832 static void kill_prom_timer(void)
833 {
834         if (!prom_timers)
835                 return;
836
837         /* Save them away for later. */
838         prom_limit0 = prom_timers->limit0;
839         prom_limit1 = prom_timers->limit1;
840
841         /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
842          * We turn both off here just to be paranoid.
843          */
844         prom_timers->limit0 = 0;
845         prom_timers->limit1 = 0;
846
847         /* Wheee, eat the interrupt packet too... */
848         __asm__ __volatile__(
849 "       mov     0x40, %%g2\n"
850 "       ldxa    [%%g0] %0, %%g1\n"
851 "       ldxa    [%%g2] %1, %%g1\n"
852 "       stxa    %%g0, [%%g0] %0\n"
853 "       membar  #Sync\n"
854         : /* no outputs */
855         : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
856         : "g1", "g2");
857 }
858
859 void notrace init_irqwork_curcpu(void)
860 {
861         int cpu = hard_smp_processor_id();
862
863         trap_block[cpu].irq_worklist_pa = 0UL;
864 }
865
866 /* Please be very careful with register_one_mondo() and
867  * sun4v_register_mondo_queues().
868  *
869  * On SMP this gets invoked from the CPU trampoline before
870  * the cpu has fully taken over the trap table from OBP,
871  * and it's kernel stack + %g6 thread register state is
872  * not fully cooked yet.
873  *
874  * Therefore you cannot make any OBP calls, not even prom_printf,
875  * from these two routines.
876  */
877 static void __cpuinit notrace register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
878 {
879         unsigned long num_entries = (qmask + 1) / 64;
880         unsigned long status;
881
882         status = sun4v_cpu_qconf(type, paddr, num_entries);
883         if (status != HV_EOK) {
884                 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
885                             "err %lu\n", type, paddr, num_entries, status);
886                 prom_halt();
887         }
888 }
889
890 void __cpuinit notrace sun4v_register_mondo_queues(int this_cpu)
891 {
892         struct trap_per_cpu *tb = &trap_block[this_cpu];
893
894         register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
895                            tb->cpu_mondo_qmask);
896         register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
897                            tb->dev_mondo_qmask);
898         register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
899                            tb->resum_qmask);
900         register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
901                            tb->nonresum_qmask);
902 }
903
904 /* Each queue region must be a power of 2 multiple of 64 bytes in
905  * size.  The base real address must be aligned to the size of the
906  * region.  Thus, an 8KB queue must be 8KB aligned, for example.
907  */
908 static void __init alloc_one_queue(unsigned long *pa_ptr, unsigned long qmask)
909 {
910         unsigned long size = PAGE_ALIGN(qmask + 1);
911         unsigned long order = get_order(size);
912         unsigned long p;
913
914         p = __get_free_pages(GFP_KERNEL, order);
915         if (!p) {
916                 prom_printf("SUN4V: Error, cannot allocate queue.\n");
917                 prom_halt();
918         }
919
920         *pa_ptr = __pa(p);
921 }
922
923 static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
924 {
925 #ifdef CONFIG_SMP
926         unsigned long page;
927
928         BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
929
930         page = get_zeroed_page(GFP_KERNEL);
931         if (!page) {
932                 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
933                 prom_halt();
934         }
935
936         tb->cpu_mondo_block_pa = __pa(page);
937         tb->cpu_list_pa = __pa(page + 64);
938 #endif
939 }
940
941 /* Allocate mondo and error queues for all possible cpus.  */
942 static void __init sun4v_init_mondo_queues(void)
943 {
944         int cpu;
945
946         for_each_possible_cpu(cpu) {
947                 struct trap_per_cpu *tb = &trap_block[cpu];
948
949                 alloc_one_queue(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
950                 alloc_one_queue(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
951                 alloc_one_queue(&tb->resum_mondo_pa, tb->resum_qmask);
952                 alloc_one_queue(&tb->resum_kernel_buf_pa, tb->resum_qmask);
953                 alloc_one_queue(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
954                 alloc_one_queue(&tb->nonresum_kernel_buf_pa,
955                                 tb->nonresum_qmask);
956         }
957 }
958
959 static void __init init_send_mondo_info(void)
960 {
961         int cpu;
962
963         for_each_possible_cpu(cpu) {
964                 struct trap_per_cpu *tb = &trap_block[cpu];
965
966                 init_cpu_send_mondo_info(tb);
967         }
968 }
969
970 static struct irqaction timer_irq_action = {
971         .name = "timer",
972 };
973
974 /* Only invoked on boot processor. */
975 void __init init_IRQ(void)
976 {
977         unsigned long size;
978
979         map_prom_timers();
980         kill_prom_timer();
981
982         size = sizeof(struct ino_bucket) * NUM_IVECS;
983         ivector_table = kzalloc(size, GFP_KERNEL);
984         if (!ivector_table) {
985                 prom_printf("Fatal error, cannot allocate ivector_table\n");
986                 prom_halt();
987         }
988         __flush_dcache_range((unsigned long) ivector_table,
989                              ((unsigned long) ivector_table) + size);
990
991         ivector_table_pa = __pa(ivector_table);
992
993         if (tlb_type == hypervisor)
994                 sun4v_init_mondo_queues();
995
996         init_send_mondo_info();
997
998         if (tlb_type == hypervisor) {
999                 /* Load up the boot cpu's entries.  */
1000                 sun4v_register_mondo_queues(hard_smp_processor_id());
1001         }
1002
1003         /* We need to clear any IRQ's pending in the soft interrupt
1004          * registers, a spurious one could be left around from the
1005          * PROM timer which we just disabled.
1006          */
1007         clear_softint(get_softint());
1008
1009         /* Now that ivector table is initialized, it is safe
1010          * to receive IRQ vector traps.  We will normally take
1011          * one or two right now, in case some device PROM used
1012          * to boot us wants to speak to us.  We just ignore them.
1013          */
1014         __asm__ __volatile__("rdpr      %%pstate, %%g1\n\t"
1015                              "or        %%g1, %0, %%g1\n\t"
1016                              "wrpr      %%g1, 0x0, %%pstate"
1017                              : /* No outputs */
1018                              : "i" (PSTATE_IE)
1019                              : "g1");
1020
1021         irq_to_desc(0)->action = &timer_irq_action;
1022 }