[SPARC64]: Use KERN_ERR in IRQ manipulation error printks.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / sparc64 / kernel / irq.c
CommitLineData
4a907dec 1/* irq.c: UltraSparc IRQ handling/init/registry.
1da177e4 2 *
4a907dec 3 * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net)
1da177e4
LT
4 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
1da177e4
LT
8#include <linux/module.h>
9#include <linux/sched.h>
10#include <linux/ptrace.h>
11#include <linux/errno.h>
12#include <linux/kernel_stat.h>
13#include <linux/signal.h>
14#include <linux/mm.h>
15#include <linux/interrupt.h>
16#include <linux/slab.h>
17#include <linux/random.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
b5a37e96 22#include <linux/bootmem.h>
e18e2a00 23#include <linux/irq.h>
35a17eb6 24#include <linux/msi.h>
1da177e4
LT
25
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>
2e457ef6 31#include <asm/io.h>
1da177e4
LT
32#include <asm/sbus.h>
33#include <asm/iommu.h>
34#include <asm/upa.h>
35#include <asm/oplib.h>
25c7581b 36#include <asm/prom.h>
1da177e4
LT
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>
63b61452 43#include <asm/auxio.h>
92704a1c 44#include <asm/head.h>
4a907dec 45#include <asm/hypervisor.h>
1da177e4 46
1da177e4
LT
47/* UPA nodes send interrupt packet to UltraSparc with first data reg
48 * value low 5 (7 on Starfire) bits holding the IRQ identifier being
49 * delivered. We must translate this into a non-vector IRQ so we can
50 * set the softint on this cpu.
51 *
52 * To make processing these packets efficient and race free we use
53 * an array of irq buckets below. The interrupt vector handler in
54 * entry.S feeds incoming packets into per-cpu pil-indexed lists.
55 * The IVEC handler does not need to act atomically, the PIL dispatch
56 * code uses CAS to get an atomic snapshot of the list and clear it
57 * at the same time.
e18e2a00
DM
58 *
59 * If you make changes to ino_bucket, please update hand coded assembler
60 * of the vectored interrupt trap handler(s) in entry.S and sun4v_ivec.S
1da177e4 61 */
e18e2a00
DM
62struct ino_bucket {
63 /* Next handler in per-CPU IRQ worklist. We know that
64 * bucket pointers have the high 32-bits clear, so to
65 * save space we only store the bits we need.
66 */
67/*0x00*/unsigned int irq_chain;
1da177e4 68
e18e2a00
DM
69 /* Virtual interrupt number assigned to this INO. */
70/*0x04*/unsigned int virt_irq;
71};
72
73#define NUM_IVECS (IMAP_INR + 1)
1da177e4
LT
74struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
75
e18e2a00
DM
76#define __irq_ino(irq) \
77 (((struct ino_bucket *)(unsigned long)(irq)) - &ivector_table[0])
78#define __bucket(irq) ((struct ino_bucket *)(unsigned long)(irq))
79#define __irq(bucket) ((unsigned int)(unsigned long)(bucket))
80
1da177e4
LT
81/* This has to be in the main kernel image, it cannot be
82 * turned into per-cpu data. The reason is that the main
83 * kernel image is locked into the TLB and this structure
84 * is accessed from the vectored interrupt trap handler. If
85 * access to this structure takes a TLB miss it could cause
86 * the 5-level sparc v9 trap stack to overflow.
87 */
fd0504c3 88#define irq_work(__cpu) &(trap_block[(__cpu)].irq_worklist)
1da177e4 89
8047e247 90static unsigned int virt_to_real_irq_table[NR_IRQS];
8047e247
DM
91
92static unsigned char virt_irq_alloc(unsigned int real_irq)
93{
94 unsigned char ent;
95
96 BUILD_BUG_ON(NR_IRQS >= 256);
97
35a17eb6
DM
98 for (ent = 1; ent < NR_IRQS; ent++) {
99 if (!virt_to_real_irq_table[ent])
100 break;
101 }
8047e247
DM
102 if (ent >= NR_IRQS) {
103 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
104 return 0;
105 }
106
8047e247
DM
107 virt_to_real_irq_table[ent] = real_irq;
108
109 return ent;
110}
111
5746c99d 112#ifdef CONFIG_PCI_MSI
35a17eb6 113static void virt_irq_free(unsigned int virt_irq)
8047e247 114{
35a17eb6 115 unsigned int real_irq;
8047e247 116
35a17eb6
DM
117 if (virt_irq >= NR_IRQS)
118 return;
119
120 real_irq = virt_to_real_irq_table[virt_irq];
121 virt_to_real_irq_table[virt_irq] = 0;
122
123 __bucket(real_irq)->virt_irq = 0;
8047e247 124}
5746c99d 125#endif
8047e247
DM
126
127static unsigned int virt_to_real_irq(unsigned char virt_irq)
128{
129 return virt_to_real_irq_table[virt_irq];
130}
131
1da177e4 132/*
e18e2a00 133 * /proc/interrupts printing:
1da177e4 134 */
1da177e4
LT
135
136int show_interrupts(struct seq_file *p, void *v)
137{
e18e2a00
DM
138 int i = *(loff_t *) v, j;
139 struct irqaction * action;
1da177e4 140 unsigned long flags;
1da177e4 141
e18e2a00
DM
142 if (i == 0) {
143 seq_printf(p, " ");
144 for_each_online_cpu(j)
145 seq_printf(p, "CPU%d ",j);
146 seq_putc(p, '\n');
147 }
148
149 if (i < NR_IRQS) {
150 spin_lock_irqsave(&irq_desc[i].lock, flags);
151 action = irq_desc[i].action;
152 if (!action)
153 goto skip;
154 seq_printf(p, "%3d: ",i);
1da177e4
LT
155#ifndef CONFIG_SMP
156 seq_printf(p, "%10u ", kstat_irqs(i));
157#else
e18e2a00
DM
158 for_each_online_cpu(j)
159 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
1da177e4 160#endif
d1bef4ed 161 seq_printf(p, " %9s", irq_desc[i].chip->typename);
e18e2a00
DM
162 seq_printf(p, " %s", action->name);
163
164 for (action=action->next; action; action = action->next)
37cdcd9e 165 seq_printf(p, ", %s", action->name);
e18e2a00 166
1da177e4 167 seq_putc(p, '\n');
e18e2a00
DM
168skip:
169 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
1da177e4 170 }
1da177e4
LT
171 return 0;
172}
173
ebd8c56c
DM
174static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
175{
176 unsigned int tid;
177
178 if (this_is_starfire) {
179 tid = starfire_translate(imap, cpuid);
180 tid <<= IMAP_TID_SHIFT;
181 tid &= IMAP_TID_UPA;
182 } else {
183 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
184 unsigned long ver;
185
186 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
187 if ((ver >> 32UL) == __JALAPENO_ID ||
188 (ver >> 32UL) == __SERRANO_ID) {
189 tid = cpuid << IMAP_TID_SHIFT;
190 tid &= IMAP_TID_JBUS;
191 } else {
192 unsigned int a = cpuid & 0x1f;
193 unsigned int n = (cpuid >> 5) & 0x1f;
194
195 tid = ((a << IMAP_AID_SHIFT) |
196 (n << IMAP_NID_SHIFT));
197 tid &= (IMAP_AID_SAFARI |
198 IMAP_NID_SAFARI);;
199 }
200 } else {
201 tid = cpuid << IMAP_TID_SHIFT;
202 tid &= IMAP_TID_UPA;
203 }
204 }
205
206 return tid;
207}
208
e18e2a00
DM
209struct irq_handler_data {
210 unsigned long iclr;
211 unsigned long imap;
8047e247 212
e18e2a00
DM
213 void (*pre_handler)(unsigned int, void *, void *);
214 void *pre_handler_arg1;
215 void *pre_handler_arg2;
216};
1da177e4 217
e18e2a00 218static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
1da177e4 219{
8047e247 220 unsigned int real_irq = virt_to_real_irq(virt_irq);
e18e2a00 221 struct ino_bucket *bucket = NULL;
1da177e4 222
e18e2a00
DM
223 if (likely(real_irq))
224 bucket = __bucket(real_irq);
8047e247 225
e18e2a00 226 return bucket;
1da177e4
LT
227}
228
e18e2a00
DM
229#ifdef CONFIG_SMP
230static int irq_choose_cpu(unsigned int virt_irq)
088dd1f8 231{
a53da52f 232 cpumask_t mask = irq_desc[virt_irq].affinity;
e18e2a00 233 int cpuid;
088dd1f8 234
e18e2a00
DM
235 if (cpus_equal(mask, CPU_MASK_ALL)) {
236 static int irq_rover;
237 static DEFINE_SPINLOCK(irq_rover_lock);
238 unsigned long flags;
1da177e4 239
e18e2a00
DM
240 /* Round-robin distribution... */
241 do_round_robin:
242 spin_lock_irqsave(&irq_rover_lock, flags);
10951ee6 243
e18e2a00
DM
244 while (!cpu_online(irq_rover)) {
245 if (++irq_rover >= NR_CPUS)
246 irq_rover = 0;
247 }
248 cpuid = irq_rover;
249 do {
250 if (++irq_rover >= NR_CPUS)
251 irq_rover = 0;
252 } while (!cpu_online(irq_rover));
1da177e4 253
e18e2a00
DM
254 spin_unlock_irqrestore(&irq_rover_lock, flags);
255 } else {
256 cpumask_t tmp;
088dd1f8 257
e18e2a00 258 cpus_and(tmp, cpu_online_map, mask);
088dd1f8 259
e18e2a00
DM
260 if (cpus_empty(tmp))
261 goto do_round_robin;
088dd1f8 262
e18e2a00 263 cpuid = first_cpu(tmp);
1da177e4 264 }
088dd1f8 265
e18e2a00
DM
266 return cpuid;
267}
268#else
269static int irq_choose_cpu(unsigned int virt_irq)
270{
271 return real_hard_smp_processor_id();
1da177e4 272}
e18e2a00 273#endif
1da177e4 274
e18e2a00 275static void sun4u_irq_enable(unsigned int virt_irq)
e3999574 276{
68c92186 277 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
e3999574 278
e18e2a00 279 if (likely(data)) {
861fe906 280 unsigned long cpuid, imap, val;
e18e2a00 281 unsigned int tid;
e3999574 282
e18e2a00
DM
283 cpuid = irq_choose_cpu(virt_irq);
284 imap = data->imap;
e3999574 285
e18e2a00 286 tid = sun4u_compute_tid(imap, cpuid);
e3999574 287
861fe906
DM
288 val = upa_readq(imap);
289 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
290 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
291 val |= tid | IMAP_VALID;
292 upa_writeq(val, imap);
e3999574 293 }
e3999574
DM
294}
295
b53bcb67
DM
296static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
297{
298 sun4u_irq_enable(virt_irq);
299}
300
e18e2a00 301static void sun4u_irq_disable(unsigned int virt_irq)
1da177e4 302{
68c92186 303 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
1da177e4 304
e18e2a00
DM
305 if (likely(data)) {
306 unsigned long imap = data->imap;
861fe906 307 u32 tmp = upa_readq(imap);
1da177e4 308
e18e2a00 309 tmp &= ~IMAP_VALID;
861fe906 310 upa_writeq(tmp, imap);
088dd1f8 311 }
088dd1f8
DM
312}
313
e18e2a00 314static void sun4u_irq_end(unsigned int virt_irq)
088dd1f8 315{
68c92186 316 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
5a606b72
DM
317 struct irq_desc *desc = irq_desc + virt_irq;
318
319 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
320 return;
088dd1f8 321
e18e2a00 322 if (likely(data))
861fe906 323 upa_writeq(ICLR_IDLE, data->iclr);
088dd1f8
DM
324}
325
e18e2a00 326static void sun4v_irq_enable(unsigned int virt_irq)
088dd1f8 327{
e18e2a00
DM
328 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
329 unsigned int ino = bucket - &ivector_table[0];
088dd1f8 330
e18e2a00
DM
331 if (likely(bucket)) {
332 unsigned long cpuid;
333 int err;
088dd1f8 334
e18e2a00 335 cpuid = irq_choose_cpu(virt_irq);
088dd1f8 336
e18e2a00
DM
337 err = sun4v_intr_settarget(ino, cpuid);
338 if (err != HV_EOK)
e83fb17f
DM
339 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
340 "err(%d)\n", ino, cpuid, err);
a357b8f4
DM
341 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
342 if (err != HV_EOK)
e83fb17f 343 printk(KERN_ERR "sun4v_intr_setstate(%x): "
a357b8f4 344 "err(%d)\n", ino, err);
e18e2a00
DM
345 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
346 if (err != HV_EOK)
e83fb17f 347 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
e18e2a00 348 ino, err);
088dd1f8 349 }
088dd1f8
DM
350}
351
b53bcb67
DM
352static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
353{
354 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
355 unsigned int ino = bucket - &ivector_table[0];
356
357 if (likely(bucket)) {
358 unsigned long cpuid;
359 int err;
360
361 cpuid = irq_choose_cpu(virt_irq);
362
363 err = sun4v_intr_settarget(ino, cpuid);
364 if (err != HV_EOK)
e83fb17f
DM
365 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
366 "err(%d)\n", ino, cpuid, err);
b53bcb67
DM
367 }
368}
369
e18e2a00 370static void sun4v_irq_disable(unsigned int virt_irq)
1da177e4 371{
e18e2a00
DM
372 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
373 unsigned int ino = bucket - &ivector_table[0];
1da177e4 374
e18e2a00
DM
375 if (likely(bucket)) {
376 int err;
1da177e4 377
e18e2a00
DM
378 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
379 if (err != HV_EOK)
e83fb17f 380 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
e18e2a00 381 "err(%d)\n", ino, err);
1da177e4 382 }
e18e2a00 383}
1da177e4 384
35a17eb6
DM
385#ifdef CONFIG_PCI_MSI
386static void sun4v_msi_enable(unsigned int virt_irq)
387{
388 sun4v_irq_enable(virt_irq);
389 unmask_msi_irq(virt_irq);
390}
391
392static void sun4v_msi_disable(unsigned int virt_irq)
393{
394 mask_msi_irq(virt_irq);
395 sun4v_irq_disable(virt_irq);
396}
397#endif
398
e18e2a00
DM
399static void sun4v_irq_end(unsigned int virt_irq)
400{
401 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
402 unsigned int ino = bucket - &ivector_table[0];
5a606b72
DM
403 struct irq_desc *desc = irq_desc + virt_irq;
404
405 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
406 return;
1da177e4 407
e18e2a00
DM
408 if (likely(bucket)) {
409 int err;
1da177e4 410
e18e2a00
DM
411 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
412 if (err != HV_EOK)
e83fb17f 413 printk(KERN_ERR "sun4v_intr_setstate(%x): "
e18e2a00 414 "err(%d)\n", ino, err);
1da177e4 415 }
1da177e4
LT
416}
417
4a907dec
DM
418static void sun4v_virq_enable(unsigned int virt_irq)
419{
420 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
421 unsigned int ino = bucket - &ivector_table[0];
422
423 if (likely(bucket)) {
424 unsigned long cpuid, dev_handle, dev_ino;
425 int err;
426
427 cpuid = irq_choose_cpu(virt_irq);
428
429 dev_handle = ino & IMAP_IGN;
430 dev_ino = ino & IMAP_INO;
431
432 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
433 if (err != HV_EOK)
e83fb17f 434 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
4a907dec
DM
435 "err(%d)\n",
436 dev_handle, dev_ino, cpuid, err);
437 err = sun4v_vintr_set_state(dev_handle, dev_ino,
12450884
DM
438 HV_INTR_STATE_IDLE);
439 if (err != HV_EOK)
e83fb17f 440 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
12450884
DM
441 "HV_INTR_STATE_IDLE): err(%d)\n",
442 dev_handle, dev_ino, err);
443 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
4a907dec
DM
444 HV_INTR_ENABLED);
445 if (err != HV_EOK)
e83fb17f 446 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
4a907dec
DM
447 "HV_INTR_ENABLED): err(%d)\n",
448 dev_handle, dev_ino, err);
449 }
450}
451
b53bcb67
DM
452static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
453{
454 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
455 unsigned int ino = bucket - &ivector_table[0];
456
457 if (likely(bucket)) {
458 unsigned long cpuid, dev_handle, dev_ino;
459 int err;
460
461 cpuid = irq_choose_cpu(virt_irq);
462
463 dev_handle = ino & IMAP_IGN;
464 dev_ino = ino & IMAP_INO;
465
466 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
467 if (err != HV_EOK)
e83fb17f 468 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
b53bcb67
DM
469 "err(%d)\n",
470 dev_handle, dev_ino, cpuid, err);
471 }
472}
473
4a907dec
DM
474static void sun4v_virq_disable(unsigned int virt_irq)
475{
476 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
477 unsigned int ino = bucket - &ivector_table[0];
478
479 if (likely(bucket)) {
480 unsigned long dev_handle, dev_ino;
481 int err;
482
483 dev_handle = ino & IMAP_IGN;
484 dev_ino = ino & IMAP_INO;
485
12450884 486 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
4a907dec
DM
487 HV_INTR_DISABLED);
488 if (err != HV_EOK)
e83fb17f 489 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
4a907dec
DM
490 "HV_INTR_DISABLED): err(%d)\n",
491 dev_handle, dev_ino, err);
492 }
493}
494
495static void sun4v_virq_end(unsigned int virt_irq)
496{
497 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
498 unsigned int ino = bucket - &ivector_table[0];
5a606b72
DM
499 struct irq_desc *desc = irq_desc + virt_irq;
500
501 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
502 return;
4a907dec
DM
503
504 if (likely(bucket)) {
505 unsigned long dev_handle, dev_ino;
506 int err;
507
508 dev_handle = ino & IMAP_IGN;
509 dev_ino = ino & IMAP_INO;
510
511 err = sun4v_vintr_set_state(dev_handle, dev_ino,
512 HV_INTR_STATE_IDLE);
513 if (err != HV_EOK)
e83fb17f 514 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
4a907dec
DM
515 "HV_INTR_STATE_IDLE): err(%d)\n",
516 dev_handle, dev_ino, err);
517 }
518}
519
e18e2a00 520static void run_pre_handler(unsigned int virt_irq)
1da177e4 521{
e18e2a00 522 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
68c92186 523 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
1da177e4 524
e18e2a00
DM
525 if (likely(data->pre_handler)) {
526 data->pre_handler(__irq_ino(__irq(bucket)),
527 data->pre_handler_arg1,
528 data->pre_handler_arg2);
1da177e4 529 }
088dd1f8
DM
530}
531
729e7d7e 532static struct irq_chip sun4u_irq = {
e18e2a00
DM
533 .typename = "sun4u",
534 .enable = sun4u_irq_enable,
535 .disable = sun4u_irq_disable,
536 .end = sun4u_irq_end,
b53bcb67 537 .set_affinity = sun4u_set_affinity,
e18e2a00 538};
8047e247 539
729e7d7e 540static struct irq_chip sun4u_irq_ack = {
e18e2a00
DM
541 .typename = "sun4u+ack",
542 .enable = sun4u_irq_enable,
543 .disable = sun4u_irq_disable,
544 .ack = run_pre_handler,
545 .end = sun4u_irq_end,
b53bcb67 546 .set_affinity = sun4u_set_affinity,
e18e2a00 547};
088dd1f8 548
729e7d7e 549static struct irq_chip sun4v_irq = {
e18e2a00
DM
550 .typename = "sun4v",
551 .enable = sun4v_irq_enable,
552 .disable = sun4v_irq_disable,
553 .end = sun4v_irq_end,
b53bcb67 554 .set_affinity = sun4v_set_affinity,
e18e2a00 555};
1da177e4 556
729e7d7e 557static struct irq_chip sun4v_irq_ack = {
e18e2a00
DM
558 .typename = "sun4v+ack",
559 .enable = sun4v_irq_enable,
560 .disable = sun4v_irq_disable,
561 .ack = run_pre_handler,
562 .end = sun4v_irq_end,
b53bcb67 563 .set_affinity = sun4v_set_affinity,
e18e2a00 564};
1da177e4 565
35a17eb6
DM
566#ifdef CONFIG_PCI_MSI
567static struct irq_chip sun4v_msi = {
568 .typename = "sun4v+msi",
569 .mask = mask_msi_irq,
570 .unmask = unmask_msi_irq,
571 .enable = sun4v_msi_enable,
572 .disable = sun4v_msi_disable,
573 .ack = run_pre_handler,
574 .end = sun4v_irq_end,
b53bcb67 575 .set_affinity = sun4v_set_affinity,
35a17eb6
DM
576};
577#endif
578
4a907dec
DM
579static struct irq_chip sun4v_virq = {
580 .typename = "vsun4v",
581 .enable = sun4v_virq_enable,
582 .disable = sun4v_virq_disable,
583 .end = sun4v_virq_end,
b53bcb67 584 .set_affinity = sun4v_virt_set_affinity,
4a907dec
DM
585};
586
587static struct irq_chip sun4v_virq_ack = {
588 .typename = "vsun4v+ack",
589 .enable = sun4v_virq_enable,
590 .disable = sun4v_virq_disable,
591 .ack = run_pre_handler,
592 .end = sun4v_virq_end,
b53bcb67 593 .set_affinity = sun4v_virt_set_affinity,
4a907dec
DM
594};
595
e18e2a00
DM
596void irq_install_pre_handler(int virt_irq,
597 void (*func)(unsigned int, void *, void *),
598 void *arg1, void *arg2)
599{
68c92186
DM
600 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
601 struct irq_chip *chip;
088dd1f8 602
e18e2a00
DM
603 data->pre_handler = func;
604 data->pre_handler_arg1 = arg1;
605 data->pre_handler_arg2 = arg2;
1da177e4 606
68c92186
DM
607 chip = get_irq_chip(virt_irq);
608 if (chip == &sun4u_irq_ack ||
4a907dec
DM
609 chip == &sun4v_irq_ack ||
610 chip == &sun4v_virq_ack
35a17eb6
DM
611#ifdef CONFIG_PCI_MSI
612 || chip == &sun4v_msi
613#endif
614 )
24ac26d4
DM
615 return;
616
68c92186 617 chip = (chip == &sun4u_irq ?
4a907dec
DM
618 &sun4u_irq_ack :
619 (chip == &sun4v_irq ?
620 &sun4v_irq_ack : &sun4v_virq_ack));
68c92186 621 set_irq_chip(virt_irq, chip);
e18e2a00 622}
1da177e4 623
e18e2a00
DM
624unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
625{
626 struct ino_bucket *bucket;
627 struct irq_handler_data *data;
e18e2a00 628 int ino;
1da177e4 629
e18e2a00 630 BUG_ON(tlb_type == hypervisor);
088dd1f8 631
861fe906 632 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
e18e2a00
DM
633 bucket = &ivector_table[ino];
634 if (!bucket->virt_irq) {
635 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
68c92186 636 set_irq_chip(bucket->virt_irq, &sun4u_irq);
fd0504c3 637 }
1da177e4 638
68c92186
DM
639 data = get_irq_chip_data(bucket->virt_irq);
640 if (unlikely(data))
e18e2a00 641 goto out;
fd0504c3 642
e18e2a00
DM
643 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
644 if (unlikely(!data)) {
645 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
646 prom_halt();
1da177e4 647 }
68c92186 648 set_irq_chip_data(bucket->virt_irq, data);
1da177e4 649
e18e2a00
DM
650 data->imap = imap;
651 data->iclr = iclr;
1da177e4 652
e18e2a00
DM
653out:
654 return bucket->virt_irq;
655}
1da177e4 656
4a907dec
DM
657static unsigned int sun4v_build_common(unsigned long sysino,
658 struct irq_chip *chip)
1da177e4 659{
8047e247 660 struct ino_bucket *bucket;
e18e2a00 661 struct irq_handler_data *data;
8047e247 662
e18e2a00 663 BUG_ON(tlb_type != hypervisor);
1da177e4 664
e18e2a00
DM
665 bucket = &ivector_table[sysino];
666 if (!bucket->virt_irq) {
667 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
4a907dec 668 set_irq_chip(bucket->virt_irq, chip);
1da177e4 669 }
1da177e4 670
68c92186
DM
671 data = get_irq_chip_data(bucket->virt_irq);
672 if (unlikely(data))
1da177e4 673 goto out;
1da177e4 674
e18e2a00
DM
675 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
676 if (unlikely(!data)) {
677 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
678 prom_halt();
679 }
68c92186 680 set_irq_chip_data(bucket->virt_irq, data);
1da177e4 681
e18e2a00
DM
682 /* Catch accidental accesses to these things. IMAP/ICLR handling
683 * is done by hypervisor calls on sun4v platforms, not by direct
684 * register accesses.
685 */
686 data->imap = ~0UL;
687 data->iclr = ~0UL;
1da177e4 688
e18e2a00
DM
689out:
690 return bucket->virt_irq;
691}
1da177e4 692
4a907dec
DM
693unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
694{
695 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
696
697 return sun4v_build_common(sysino, &sun4v_irq);
698}
699
700unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
701{
702 unsigned long sysino, hv_err;
703
5f7426c0 704 BUG_ON(devhandle & devino);
4a907dec
DM
705
706 sysino = devhandle | devino;
5f7426c0 707 BUG_ON(sysino & ~(IMAP_IGN | IMAP_INO));
4a907dec
DM
708
709 hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
710 if (hv_err) {
711 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
712 "err=%lu\n", devhandle, devino, hv_err);
713 prom_halt();
714 }
715
716 return sun4v_build_common(sysino, &sun4v_virq);
717}
718
35a17eb6
DM
719#ifdef CONFIG_PCI_MSI
720unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
721 unsigned int msi_start, unsigned int msi_end)
722{
723 struct ino_bucket *bucket;
724 struct irq_handler_data *data;
725 unsigned long sysino;
726 unsigned int devino;
727
728 BUG_ON(tlb_type != hypervisor);
729
730 /* Find a free devino in the given range. */
731 for (devino = msi_start; devino < msi_end; devino++) {
732 sysino = sun4v_devino_to_sysino(devhandle, devino);
733 bucket = &ivector_table[sysino];
734 if (!bucket->virt_irq)
735 break;
736 }
737 if (devino >= msi_end)
738 return 0;
739
740 sysino = sun4v_devino_to_sysino(devhandle, devino);
741 bucket = &ivector_table[sysino];
742 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
743 *virt_irq_p = bucket->virt_irq;
744 set_irq_chip(bucket->virt_irq, &sun4v_msi);
745
746 data = get_irq_chip_data(bucket->virt_irq);
747 if (unlikely(data))
748 return devino;
749
750 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
751 if (unlikely(!data)) {
752 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
753 prom_halt();
754 }
755 set_irq_chip_data(bucket->virt_irq, data);
756
757 data->imap = ~0UL;
758 data->iclr = ~0UL;
759
760 return devino;
761}
762
763void sun4v_destroy_msi(unsigned int virt_irq)
764{
765 virt_irq_free(virt_irq);
766}
767#endif
768
e18e2a00
DM
769void ack_bad_irq(unsigned int virt_irq)
770{
771 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
772 unsigned int ino = 0xdeadbeef;
ab66a50e 773
e18e2a00
DM
774 if (bucket)
775 ino = bucket - &ivector_table[0];
6a76267f 776
e18e2a00
DM
777 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
778 ino, virt_irq);
1da177e4
LT
779}
780
1da177e4
LT
781void handler_irq(int irq, struct pt_regs *regs)
782{
e18e2a00 783 struct ino_bucket *bucket;
6d24c8dc 784 struct pt_regs *old_regs;
1da177e4 785
1da177e4 786 clear_softint(1 << irq);
1da177e4 787
6d24c8dc 788 old_regs = set_irq_regs(regs);
1da177e4 789 irq_enter();
1da177e4
LT
790
791 /* Sliiiick... */
e18e2a00
DM
792 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
793 while (bucket) {
794 struct ino_bucket *next = __bucket(bucket->irq_chain);
1da177e4 795
e18e2a00 796 bucket->irq_chain = 0;
6d24c8dc 797 __do_IRQ(bucket->virt_irq);
fd0504c3 798
e18e2a00 799 bucket = next;
1da177e4 800 }
e18e2a00 801
1da177e4 802 irq_exit();
6d24c8dc 803 set_irq_regs(old_regs);
1da177e4
LT
804}
805
e0204409
DM
806#ifdef CONFIG_HOTPLUG_CPU
807void fixup_irqs(void)
808{
809 unsigned int irq;
810
811 for (irq = 0; irq < NR_IRQS; irq++) {
812 unsigned long flags;
813
814 spin_lock_irqsave(&irq_desc[irq].lock, flags);
815 if (irq_desc[irq].action &&
816 !(irq_desc[irq].status & IRQ_PER_CPU)) {
817 if (irq_desc[irq].chip->set_affinity)
818 irq_desc[irq].chip->set_affinity(irq,
819 irq_desc[irq].affinity);
820 }
821 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
822 }
823}
824#endif
825
cdd5186f
DM
826struct sun5_timer {
827 u64 count0;
828 u64 limit0;
829 u64 count1;
830 u64 limit1;
831};
1da177e4 832
cdd5186f 833static struct sun5_timer *prom_timers;
1da177e4
LT
834static u64 prom_limit0, prom_limit1;
835
836static void map_prom_timers(void)
837{
25c7581b 838 struct device_node *dp;
6a23acf3 839 const unsigned int *addr;
1da177e4
LT
840
841 /* PROM timer node hangs out in the top level of device siblings... */
25c7581b
DM
842 dp = of_find_node_by_path("/");
843 dp = dp->child;
844 while (dp) {
845 if (!strcmp(dp->name, "counter-timer"))
846 break;
847 dp = dp->sibling;
848 }
1da177e4
LT
849
850 /* Assume if node is not present, PROM uses different tick mechanism
851 * which we should not care about.
852 */
25c7581b 853 if (!dp) {
1da177e4
LT
854 prom_timers = (struct sun5_timer *) 0;
855 return;
856 }
857
858 /* If PROM is really using this, it must be mapped by him. */
25c7581b
DM
859 addr = of_get_property(dp, "address", NULL);
860 if (!addr) {
1da177e4
LT
861 prom_printf("PROM does not have timer mapped, trying to continue.\n");
862 prom_timers = (struct sun5_timer *) 0;
863 return;
864 }
865 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
866}
867
868static void kill_prom_timer(void)
869{
870 if (!prom_timers)
871 return;
872
873 /* Save them away for later. */
874 prom_limit0 = prom_timers->limit0;
875 prom_limit1 = prom_timers->limit1;
876
877 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
878 * We turn both off here just to be paranoid.
879 */
880 prom_timers->limit0 = 0;
881 prom_timers->limit1 = 0;
882
883 /* Wheee, eat the interrupt packet too... */
884 __asm__ __volatile__(
885" mov 0x40, %%g2\n"
886" ldxa [%%g0] %0, %%g1\n"
887" ldxa [%%g2] %1, %%g1\n"
888" stxa %%g0, [%%g0] %0\n"
889" membar #Sync\n"
890 : /* no outputs */
891 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
892 : "g1", "g2");
893}
894
1da177e4
LT
895void init_irqwork_curcpu(void)
896{
1da177e4
LT
897 int cpu = hard_smp_processor_id();
898
fd0504c3 899 trap_block[cpu].irq_worklist = 0;
1da177e4
LT
900}
901
5cbc3073
DM
902/* Please be very careful with register_one_mondo() and
903 * sun4v_register_mondo_queues().
904 *
905 * On SMP this gets invoked from the CPU trampoline before
906 * the cpu has fully taken over the trap table from OBP,
907 * and it's kernel stack + %g6 thread register state is
908 * not fully cooked yet.
909 *
910 * Therefore you cannot make any OBP calls, not even prom_printf,
911 * from these two routines.
912 */
913static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
ac29c11d 914{
5cbc3073 915 unsigned long num_entries = (qmask + 1) / 64;
94f8762d
DM
916 unsigned long status;
917
918 status = sun4v_cpu_qconf(type, paddr, num_entries);
919 if (status != HV_EOK) {
920 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
921 "err %lu\n", type, paddr, num_entries, status);
ac29c11d
DM
922 prom_halt();
923 }
924}
925
b5a37e96 926static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
5b0c0572 927{
b5a37e96
DM
928 struct trap_per_cpu *tb = &trap_block[this_cpu];
929
5cbc3073
DM
930 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
931 tb->cpu_mondo_qmask);
932 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
933 tb->dev_mondo_qmask);
934 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
935 tb->resum_qmask);
936 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
937 tb->nonresum_qmask);
b5a37e96
DM
938}
939
5cbc3073 940static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask, int use_bootmem)
b5a37e96 941{
5cbc3073
DM
942 unsigned long size = PAGE_ALIGN(qmask + 1);
943 unsigned long order = get_order(size);
944 void *p = NULL;
b5a37e96 945
5cbc3073
DM
946 if (use_bootmem) {
947 p = __alloc_bootmem_low(size, size, 0);
948 } else {
949 struct page *page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, order);
950 if (page)
951 p = page_address(page);
952 }
b5a37e96 953
5cbc3073 954 if (!p) {
b5a37e96
DM
955 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
956 prom_halt();
957 }
958
5cbc3073 959 *pa_ptr = __pa(p);
b5a37e96
DM
960}
961
5cbc3073 962static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask, int use_bootmem)
b5a37e96 963{
5cbc3073
DM
964 unsigned long size = PAGE_ALIGN(qmask + 1);
965 unsigned long order = get_order(size);
966 void *p = NULL;
b5a37e96 967
5cbc3073
DM
968 if (use_bootmem) {
969 p = __alloc_bootmem_low(size, size, 0);
970 } else {
971 struct page *page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, order);
972 if (page)
973 p = page_address(page);
974 }
5b0c0572 975
5cbc3073 976 if (!p) {
5b0c0572
DM
977 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
978 prom_halt();
979 }
980
5cbc3073 981 *pa_ptr = __pa(p);
5b0c0572
DM
982}
983
b5a37e96 984static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
1d2f1f90
DM
985{
986#ifdef CONFIG_SMP
b5a37e96 987 void *page;
1d2f1f90
DM
988
989 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
990
b5a37e96
DM
991 if (use_bootmem)
992 page = alloc_bootmem_low_pages(PAGE_SIZE);
993 else
994 page = (void *) get_zeroed_page(GFP_ATOMIC);
995
1d2f1f90
DM
996 if (!page) {
997 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
998 prom_halt();
999 }
1000
1001 tb->cpu_mondo_block_pa = __pa(page);
1002 tb->cpu_list_pa = __pa(page + 64);
1003#endif
1004}
1005
b5a37e96 1006/* Allocate and register the mondo and error queues for this cpu. */
72aff53f 1007void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
ac29c11d 1008{
ac29c11d
DM
1009 struct trap_per_cpu *tb = &trap_block[cpu];
1010
72aff53f 1011 if (alloc) {
5cbc3073
DM
1012 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask, use_bootmem);
1013 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask, use_bootmem);
1014 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask, use_bootmem);
1015 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask, use_bootmem);
1016 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask, use_bootmem);
1017 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, tb->nonresum_qmask, use_bootmem);
1d2f1f90 1018
72aff53f
DM
1019 init_cpu_send_mondo_info(tb, use_bootmem);
1020 }
1d2f1f90 1021
72aff53f
DM
1022 if (load) {
1023 if (cpu != hard_smp_processor_id()) {
1024 prom_printf("SUN4V: init mondo on cpu %d not %d\n",
1025 cpu, hard_smp_processor_id());
1026 prom_halt();
1027 }
1028 sun4v_register_mondo_queues(cpu);
1029 }
ac29c11d
DM
1030}
1031
e18e2a00
DM
1032static struct irqaction timer_irq_action = {
1033 .name = "timer",
1034};
1035
1da177e4
LT
1036/* Only invoked on boot processor. */
1037void __init init_IRQ(void)
1038{
1039 map_prom_timers();
1040 kill_prom_timer();
1041 memset(&ivector_table[0], 0, sizeof(ivector_table));
1042
ac29c11d 1043 if (tlb_type == hypervisor)
72aff53f 1044 sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
ac29c11d 1045
1da177e4
LT
1046 /* We need to clear any IRQ's pending in the soft interrupt
1047 * registers, a spurious one could be left around from the
1048 * PROM timer which we just disabled.
1049 */
1050 clear_softint(get_softint());
1051
1052 /* Now that ivector table is initialized, it is safe
1053 * to receive IRQ vector traps. We will normally take
1054 * one or two right now, in case some device PROM used
1055 * to boot us wants to speak to us. We just ignore them.
1056 */
1057 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
1058 "or %%g1, %0, %%g1\n\t"
1059 "wrpr %%g1, 0x0, %%pstate"
1060 : /* No outputs */
1061 : "i" (PSTATE_IE)
1062 : "g1");
1da177e4 1063
e18e2a00 1064 irq_desc[0].action = &timer_irq_action;
1da177e4 1065}