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