blob: c443db1843719f9937463adea56b10ad6690f5ef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: irq.c,v 1.114 2002/01/11 08:45:38 davem Exp $
2 * irq.c: UltraSparc IRQ handling/init/registry.
3 *
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/sched.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>
David S. Millerb5a37e92006-02-11 23:07:13 -080023#include <linux/bootmem.h>
David S. Millere18e2a02006-06-20 01:23:32 -070024#include <linux/irq.h>
David S. Miller35a17eb2007-02-10 17:41:02 -080025#include <linux/msi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
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>
Sven Hartge2e457ef2005-10-08 21:12:04 -070032#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/sbus.h>
34#include <asm/iommu.h>
35#include <asm/upa.h>
36#include <asm/oplib.h>
David S. Miller25c75812006-06-22 20:21:22 -070037#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/timer.h>
39#include <asm/smp.h>
40#include <asm/starfire.h>
41#include <asm/uaccess.h>
42#include <asm/cache.h>
43#include <asm/cpudata.h>
David S. Miller63b61452005-06-27 17:04:45 -070044#include <asm/auxio.h>
David S. Miller92704a12006-02-26 23:27:19 -080045#include <asm/head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* 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.
David S. Millere18e2a02006-06-20 01:23:32 -070058 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
David S. Millere18e2a02006-06-20 01:23:32 -070062struct 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
David S. Millere18e2a02006-06-20 01:23:32 -070069 /* Virtual interrupt number assigned to this INO. */
70/*0x04*/unsigned int virt_irq;
71};
72
73#define NUM_IVECS (IMAP_INR + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
75
David S. Millere18e2a02006-06-20 01:23:32 -070076#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* 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 */
David S. Millerfd0504c32006-06-20 01:20:00 -070088#define irq_work(__cpu) &(trap_block[(__cpu)].irq_worklist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David S. Miller8047e242006-06-20 01:22:35 -070090static unsigned int virt_to_real_irq_table[NR_IRQS];
David S. Miller8047e242006-06-20 01:22:35 -070091
92static unsigned char virt_irq_alloc(unsigned int real_irq)
93{
94 unsigned char ent;
95
96 BUILD_BUG_ON(NR_IRQS >= 256);
97
David S. Miller35a17eb2007-02-10 17:41:02 -080098 for (ent = 1; ent < NR_IRQS; ent++) {
99 if (!virt_to_real_irq_table[ent])
100 break;
101 }
David S. Miller8047e242006-06-20 01:22:35 -0700102 if (ent >= NR_IRQS) {
103 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
104 return 0;
105 }
106
David S. Miller8047e242006-06-20 01:22:35 -0700107 virt_to_real_irq_table[ent] = real_irq;
108
109 return ent;
110}
111
David S. Miller5746c992007-02-20 01:26:48 -0800112#ifdef CONFIG_PCI_MSI
David S. Miller35a17eb2007-02-10 17:41:02 -0800113static void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700114{
David S. Miller35a17eb2007-02-10 17:41:02 -0800115 unsigned int real_irq;
David S. Miller8047e242006-06-20 01:22:35 -0700116
David S. Miller35a17eb2007-02-10 17:41:02 -0800117 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;
David S. Miller8047e242006-06-20 01:22:35 -0700124}
David S. Miller5746c992007-02-20 01:26:48 -0800125#endif
David S. Miller8047e242006-06-20 01:22:35 -0700126
127static unsigned int virt_to_real_irq(unsigned char virt_irq)
128{
129 return virt_to_real_irq_table[virt_irq];
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
David S. Millere18e2a02006-06-20 01:23:32 -0700133 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136int show_interrupts(struct seq_file *p, void *v)
137{
David S. Millere18e2a02006-06-20 01:23:32 -0700138 int i = *(loff_t *) v, j;
139 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
David S. Millere18e2a02006-06-20 01:23:32 -0700142 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifndef CONFIG_SMP
156 seq_printf(p, "%10u ", kstat_irqs(i));
157#else
David S. Millere18e2a02006-06-20 01:23:32 -0700158 for_each_online_cpu(j)
159 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700161 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700162 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David S. Millere18e2a02006-06-20 01:23:32 -0700164 for (action=action->next; action; action = action->next)
165 seq_printf(p, ", %s", action->name);
166
167 seq_putc(p, '\n');
168skip:
169 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172}
173
David S. Millerebd8c562006-02-17 08:38:06 -0800174extern unsigned long real_hard_smp_processor_id(void);
175
176static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
177{
178 unsigned int tid;
179
180 if (this_is_starfire) {
181 tid = starfire_translate(imap, cpuid);
182 tid <<= IMAP_TID_SHIFT;
183 tid &= IMAP_TID_UPA;
184 } else {
185 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
186 unsigned long ver;
187
188 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
189 if ((ver >> 32UL) == __JALAPENO_ID ||
190 (ver >> 32UL) == __SERRANO_ID) {
191 tid = cpuid << IMAP_TID_SHIFT;
192 tid &= IMAP_TID_JBUS;
193 } else {
194 unsigned int a = cpuid & 0x1f;
195 unsigned int n = (cpuid >> 5) & 0x1f;
196
197 tid = ((a << IMAP_AID_SHIFT) |
198 (n << IMAP_NID_SHIFT));
199 tid &= (IMAP_AID_SAFARI |
200 IMAP_NID_SAFARI);;
201 }
202 } else {
203 tid = cpuid << IMAP_TID_SHIFT;
204 tid &= IMAP_TID_UPA;
205 }
206 }
207
208 return tid;
209}
210
David S. Millere18e2a02006-06-20 01:23:32 -0700211struct irq_handler_data {
212 unsigned long iclr;
213 unsigned long imap;
214
215 void (*pre_handler)(unsigned int, void *, void *);
216 void *pre_handler_arg1;
217 void *pre_handler_arg2;
218};
219
220static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David S. Miller8047e242006-06-20 01:22:35 -0700222 unsigned int real_irq = virt_to_real_irq(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700223 struct ino_bucket *bucket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
David S. Millere18e2a02006-06-20 01:23:32 -0700225 if (likely(real_irq))
226 bucket = __bucket(real_irq);
David S. Miller8047e242006-06-20 01:22:35 -0700227
David S. Millere18e2a02006-06-20 01:23:32 -0700228 return bucket;
229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
David S. Millere18e2a02006-06-20 01:23:32 -0700231#ifdef CONFIG_SMP
232static int irq_choose_cpu(unsigned int virt_irq)
233{
Ingo Molnara53da522006-06-29 02:24:38 -0700234 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700235 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
David S. Millere18e2a02006-06-20 01:23:32 -0700237 if (cpus_equal(mask, CPU_MASK_ALL)) {
238 static int irq_rover;
239 static DEFINE_SPINLOCK(irq_rover_lock);
240 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800241
David S. Millere18e2a02006-06-20 01:23:32 -0700242 /* Round-robin distribution... */
243 do_round_robin:
244 spin_lock_irqsave(&irq_rover_lock, flags);
245
246 while (!cpu_online(irq_rover)) {
247 if (++irq_rover >= NR_CPUS)
248 irq_rover = 0;
249 }
250 cpuid = irq_rover;
251 do {
252 if (++irq_rover >= NR_CPUS)
253 irq_rover = 0;
254 } while (!cpu_online(irq_rover));
255
256 spin_unlock_irqrestore(&irq_rover_lock, flags);
257 } else {
258 cpumask_t tmp;
259
260 cpus_and(tmp, cpu_online_map, mask);
261
262 if (cpus_empty(tmp))
263 goto do_round_robin;
264
265 cpuid = first_cpu(tmp);
266 }
267
268 return cpuid;
269}
270#else
271static int irq_choose_cpu(unsigned int virt_irq)
272{
273 return real_hard_smp_processor_id();
274}
275#endif
276
277static void sun4u_irq_enable(unsigned int virt_irq)
278{
David S. Miller68c92182007-01-29 12:12:28 -0800279 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700280
281 if (likely(data)) {
282 unsigned long cpuid, imap;
283 unsigned int tid;
284
285 cpuid = irq_choose_cpu(virt_irq);
286 imap = data->imap;
287
288 tid = sun4u_compute_tid(imap, cpuid);
289
290 upa_writel(tid | IMAP_VALID, imap);
291 }
292}
293
294static void sun4u_irq_disable(unsigned int virt_irq)
295{
David S. Miller68c92182007-01-29 12:12:28 -0800296 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700297
298 if (likely(data)) {
299 unsigned long imap = data->imap;
300 u32 tmp = upa_readl(imap);
301
302 tmp &= ~IMAP_VALID;
303 upa_writel(tmp, imap);
304 }
305}
306
307static void sun4u_irq_end(unsigned int virt_irq)
308{
David S. Miller68c92182007-01-29 12:12:28 -0800309 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700310
311 if (likely(data))
312 upa_writel(ICLR_IDLE, data->iclr);
313}
314
315static void sun4v_irq_enable(unsigned int virt_irq)
316{
317 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
318 unsigned int ino = bucket - &ivector_table[0];
319
320 if (likely(bucket)) {
321 unsigned long cpuid;
David S. Millerc4bea282006-02-13 22:56:27 -0800322 int err;
David S. Miller10951ee2006-02-13 18:22:57 -0800323
David S. Millere18e2a02006-06-20 01:23:32 -0700324 cpuid = irq_choose_cpu(virt_irq);
325
David S. Millerebd8c562006-02-17 08:38:06 -0800326 err = sun4v_intr_settarget(ino, cpuid);
David S. Millerc4bea282006-02-13 22:56:27 -0800327 if (err != HV_EOK)
David S. Millerebd8c562006-02-17 08:38:06 -0800328 printk("sun4v_intr_settarget(%x,%lu): err(%d)\n",
329 ino, cpuid, err);
David S. Millerabd92b22006-02-14 22:20:13 -0800330 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
David S. Millerc4bea282006-02-13 22:56:27 -0800331 if (err != HV_EOK)
332 printk("sun4v_intr_setenabled(%x): err(%d)\n",
333 ino, err);
David S. Millerd82ace72006-02-09 02:52:44 -0800334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
David S. Millere18e2a02006-06-20 01:23:32 -0700337static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David S. Millere18e2a02006-06-20 01:23:32 -0700339 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
340 unsigned int ino = bucket - &ivector_table[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David S. Millere18e2a02006-06-20 01:23:32 -0700342 if (likely(bucket)) {
David S. Miller8047e242006-06-20 01:22:35 -0700343 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David S. Miller8047e242006-06-20 01:22:35 -0700345 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
346 if (err != HV_EOK)
347 printk("sun4v_intr_setenabled(%x): "
348 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350}
351
David S. Miller35a17eb2007-02-10 17:41:02 -0800352#ifdef CONFIG_PCI_MSI
353static void sun4v_msi_enable(unsigned int virt_irq)
354{
355 sun4v_irq_enable(virt_irq);
356 unmask_msi_irq(virt_irq);
357}
358
359static void sun4v_msi_disable(unsigned int virt_irq)
360{
361 mask_msi_irq(virt_irq);
362 sun4v_irq_disable(virt_irq);
363}
364#endif
365
David S. Millere18e2a02006-06-20 01:23:32 -0700366static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700367{
David S. Millere18e2a02006-06-20 01:23:32 -0700368 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
369 unsigned int ino = bucket - &ivector_table[0];
370
371 if (likely(bucket)) {
372 int err;
373
374 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
375 if (err != HV_EOK)
376 printk("sun4v_intr_setstate(%x): "
377 "err(%d)\n", ino, err);
378 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700379}
380
David S. Millere18e2a02006-06-20 01:23:32 -0700381static void run_pre_handler(unsigned int virt_irq)
382{
383 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800384 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700385
386 if (likely(data->pre_handler)) {
387 data->pre_handler(__irq_ino(__irq(bucket)),
388 data->pre_handler_arg1,
389 data->pre_handler_arg2);
390 }
391}
392
David S. Miller729e7d72006-12-12 00:59:12 -0800393static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700394 .typename = "sun4u",
395 .enable = sun4u_irq_enable,
396 .disable = sun4u_irq_disable,
397 .end = sun4u_irq_end,
398};
399
David S. Miller729e7d72006-12-12 00:59:12 -0800400static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700401 .typename = "sun4u+ack",
402 .enable = sun4u_irq_enable,
403 .disable = sun4u_irq_disable,
404 .ack = run_pre_handler,
405 .end = sun4u_irq_end,
406};
407
David S. Miller729e7d72006-12-12 00:59:12 -0800408static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700409 .typename = "sun4v",
410 .enable = sun4v_irq_enable,
411 .disable = sun4v_irq_disable,
412 .end = sun4v_irq_end,
413};
414
David S. Miller729e7d72006-12-12 00:59:12 -0800415static struct irq_chip sun4v_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700416 .typename = "sun4v+ack",
417 .enable = sun4v_irq_enable,
418 .disable = sun4v_irq_disable,
419 .ack = run_pre_handler,
420 .end = sun4v_irq_end,
421};
422
David S. Miller35a17eb2007-02-10 17:41:02 -0800423#ifdef CONFIG_PCI_MSI
424static struct irq_chip sun4v_msi = {
425 .typename = "sun4v+msi",
426 .mask = mask_msi_irq,
427 .unmask = unmask_msi_irq,
428 .enable = sun4v_msi_enable,
429 .disable = sun4v_msi_disable,
430 .ack = run_pre_handler,
431 .end = sun4v_irq_end,
432};
433#endif
434
David S. Millere18e2a02006-06-20 01:23:32 -0700435void irq_install_pre_handler(int virt_irq,
436 void (*func)(unsigned int, void *, void *),
437 void *arg1, void *arg2)
438{
David S. Miller68c92182007-01-29 12:12:28 -0800439 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
440 struct irq_chip *chip;
David S. Millere18e2a02006-06-20 01:23:32 -0700441
442 data->pre_handler = func;
443 data->pre_handler_arg1 = arg1;
444 data->pre_handler_arg2 = arg2;
445
David S. Miller68c92182007-01-29 12:12:28 -0800446 chip = get_irq_chip(virt_irq);
447 if (chip == &sun4u_irq_ack ||
David S. Miller35a17eb2007-02-10 17:41:02 -0800448 chip == &sun4v_irq_ack
449#ifdef CONFIG_PCI_MSI
450 || chip == &sun4v_msi
451#endif
452 )
David S. Miller24ac26d2006-06-29 14:38:21 -0700453 return;
454
David S. Miller68c92182007-01-29 12:12:28 -0800455 chip = (chip == &sun4u_irq ?
456 &sun4u_irq_ack : &sun4v_irq_ack);
457 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700458}
459
460unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700463 struct irq_handler_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int ino;
465
David S. Miller10951ee2006-02-13 18:22:57 -0800466 BUG_ON(tlb_type == hypervisor);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700469 bucket = &ivector_table[ino];
David S. Millere18e2a02006-06-20 01:23:32 -0700470 if (!bucket->virt_irq) {
471 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800472 set_irq_chip(bucket->virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700473 }
474
David S. Miller68c92182007-01-29 12:12:28 -0800475 data = get_irq_chip_data(bucket->virt_irq);
476 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700477 goto out;
478
479 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
480 if (unlikely(!data)) {
481 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700482 prom_halt();
483 }
David S. Miller68c92182007-01-29 12:12:28 -0800484 set_irq_chip_data(bucket->virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700485
David S. Millere18e2a02006-06-20 01:23:32 -0700486 data->imap = imap;
487 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
David S. Miller088dd1f2005-07-04 13:24:38 -0700489out:
David S. Miller8047e242006-06-20 01:22:35 -0700490 return bucket->virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
David S. Millere18e2a02006-06-20 01:23:32 -0700493unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
David S. Millere3999572006-02-13 18:16:10 -0800494{
495 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700496 struct irq_handler_data *data;
David S. Millere3999572006-02-13 18:16:10 -0800497 unsigned long sysino;
David S. Millere18e2a02006-06-20 01:23:32 -0700498
499 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800500
501 sysino = sun4v_devino_to_sysino(devhandle, devino);
David S. Millere3999572006-02-13 18:16:10 -0800502 bucket = &ivector_table[sysino];
David S. Millere18e2a02006-06-20 01:23:32 -0700503 if (!bucket->virt_irq) {
504 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800505 set_irq_chip(bucket->virt_irq, &sun4v_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700506 }
507
David S. Miller68c92182007-01-29 12:12:28 -0800508 data = get_irq_chip_data(bucket->virt_irq);
509 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700510 goto out;
511
512 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
513 if (unlikely(!data)) {
514 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
515 prom_halt();
516 }
David S. Miller68c92182007-01-29 12:12:28 -0800517 set_irq_chip_data(bucket->virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800518
519 /* Catch accidental accesses to these things. IMAP/ICLR handling
520 * is done by hypervisor calls on sun4v platforms, not by direct
521 * register accesses.
522 */
David S. Millere18e2a02006-06-20 01:23:32 -0700523 data->imap = ~0UL;
524 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800525
David S. Millere18e2a02006-06-20 01:23:32 -0700526out:
David S. Miller8047e242006-06-20 01:22:35 -0700527 return bucket->virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800528}
529
David S. Miller35a17eb2007-02-10 17:41:02 -0800530#ifdef CONFIG_PCI_MSI
531unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
532 unsigned int msi_start, unsigned int msi_end)
533{
534 struct ino_bucket *bucket;
535 struct irq_handler_data *data;
536 unsigned long sysino;
537 unsigned int devino;
538
539 BUG_ON(tlb_type != hypervisor);
540
541 /* Find a free devino in the given range. */
542 for (devino = msi_start; devino < msi_end; devino++) {
543 sysino = sun4v_devino_to_sysino(devhandle, devino);
544 bucket = &ivector_table[sysino];
545 if (!bucket->virt_irq)
546 break;
547 }
548 if (devino >= msi_end)
549 return 0;
550
551 sysino = sun4v_devino_to_sysino(devhandle, devino);
552 bucket = &ivector_table[sysino];
553 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
554 *virt_irq_p = bucket->virt_irq;
555 set_irq_chip(bucket->virt_irq, &sun4v_msi);
556
557 data = get_irq_chip_data(bucket->virt_irq);
558 if (unlikely(data))
559 return devino;
560
561 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
562 if (unlikely(!data)) {
563 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
564 prom_halt();
565 }
566 set_irq_chip_data(bucket->virt_irq, data);
567
568 data->imap = ~0UL;
569 data->iclr = ~0UL;
570
571 return devino;
572}
573
574void sun4v_destroy_msi(unsigned int virt_irq)
575{
576 virt_irq_free(virt_irq);
577}
578#endif
579
David S. Millere18e2a02006-06-20 01:23:32 -0700580void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700581{
David S. Millere18e2a02006-06-20 01:23:32 -0700582 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
583 unsigned int ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700584
David S. Millere18e2a02006-06-20 01:23:32 -0700585 if (bucket)
586 ino = bucket - &ivector_table[0];
David S. Miller088dd1f2005-07-04 13:24:38 -0700587
David S. Millere18e2a02006-06-20 01:23:32 -0700588 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
589 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
David S. Millerfd0504c32006-06-20 01:20:00 -0700592#ifndef CONFIG_SMP
Al Viro63540ba2006-10-09 11:51:14 +0100593extern irqreturn_t timer_interrupt(int, void *);
David S. Millerfd0504c32006-06-20 01:20:00 -0700594
595void timer_irq(int irq, struct pt_regs *regs)
596{
597 unsigned long clr_mask = 1 << irq;
598 unsigned long tick_mask = tick_ops->softint_mask;
Al Viro63540ba2006-10-09 11:51:14 +0100599 struct pt_regs *old_regs;
David S. Millerfd0504c32006-06-20 01:20:00 -0700600
601 if (get_softint() & tick_mask) {
602 irq = 0;
603 clr_mask = tick_mask;
604 }
605 clear_softint(clr_mask);
606
Al Viro63540ba2006-10-09 11:51:14 +0100607 old_regs = set_irq_regs(regs);
David S. Millerfd0504c32006-06-20 01:20:00 -0700608 irq_enter();
David S. Millere18e2a02006-06-20 01:23:32 -0700609
David S. Miller8047e242006-06-20 01:22:35 -0700610 kstat_this_cpu.irqs[0]++;
Al Viro63540ba2006-10-09 11:51:14 +0100611 timer_interrupt(irq, NULL);
David S. Millere18e2a02006-06-20 01:23:32 -0700612
David S. Millerfd0504c32006-06-20 01:20:00 -0700613 irq_exit();
Al Viro63540ba2006-10-09 11:51:14 +0100614 set_irq_regs(old_regs);
David S. Millerfd0504c32006-06-20 01:20:00 -0700615}
616#endif
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618void handler_irq(int irq, struct pt_regs *regs)
619{
David S. Millere18e2a02006-06-20 01:23:32 -0700620 struct ino_bucket *bucket;
Al Viro6d24c8d2006-10-08 08:23:28 -0400621 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Al Viro6d24c8d2006-10-08 08:23:28 -0400625 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Sliiiick... */
David S. Millere18e2a02006-06-20 01:23:32 -0700629 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
630 while (bucket) {
631 struct ino_bucket *next = __bucket(bucket->irq_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
David S. Millere18e2a02006-06-20 01:23:32 -0700633 bucket->irq_chain = 0;
Al Viro6d24c8d2006-10-08 08:23:28 -0400634 __do_IRQ(bucket->virt_irq);
David S. Millerfd0504c32006-06-20 01:20:00 -0700635
David S. Millere18e2a02006-06-20 01:23:32 -0700636 bucket = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
David S. Millere18e2a02006-06-20 01:23:32 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400640 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
David S. Millercdd51862005-07-24 19:36:13 -0700643struct sun5_timer {
644 u64 count0;
645 u64 limit0;
646 u64 count1;
647 u64 limit1;
648};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
David S. Millercdd51862005-07-24 19:36:13 -0700650static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static u64 prom_limit0, prom_limit1;
652
653static void map_prom_timers(void)
654{
David S. Miller25c75812006-06-22 20:21:22 -0700655 struct device_node *dp;
656 unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700659 dp = of_find_node_by_path("/");
660 dp = dp->child;
661 while (dp) {
662 if (!strcmp(dp->name, "counter-timer"))
663 break;
664 dp = dp->sibling;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Assume if node is not present, PROM uses different tick mechanism
668 * which we should not care about.
669 */
David S. Miller25c75812006-06-22 20:21:22 -0700670 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 prom_timers = (struct sun5_timer *) 0;
672 return;
673 }
674
675 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700676 addr = of_get_property(dp, "address", NULL);
677 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 prom_printf("PROM does not have timer mapped, trying to continue.\n");
679 prom_timers = (struct sun5_timer *) 0;
680 return;
681 }
682 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
683}
684
685static void kill_prom_timer(void)
686{
687 if (!prom_timers)
688 return;
689
690 /* Save them away for later. */
691 prom_limit0 = prom_timers->limit0;
692 prom_limit1 = prom_timers->limit1;
693
694 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
695 * We turn both off here just to be paranoid.
696 */
697 prom_timers->limit0 = 0;
698 prom_timers->limit1 = 0;
699
700 /* Wheee, eat the interrupt packet too... */
701 __asm__ __volatile__(
702" mov 0x40, %%g2\n"
703" ldxa [%%g0] %0, %%g1\n"
704" ldxa [%%g2] %1, %%g1\n"
705" stxa %%g0, [%%g0] %0\n"
706" membar #Sync\n"
707 : /* no outputs */
708 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
709 : "g1", "g2");
710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712void init_irqwork_curcpu(void)
713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int cpu = hard_smp_processor_id();
715
David S. Millerfd0504c32006-06-20 01:20:00 -0700716 trap_block[cpu].irq_worklist = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
David S. Millerb5a37e92006-02-11 23:07:13 -0800719static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
David S. Millerac29c112006-02-08 00:08:23 -0800720{
David S. Miller94f87622006-02-16 14:26:53 -0800721 unsigned long num_entries = 128;
722 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800723
David S. Miller94f87622006-02-16 14:26:53 -0800724 status = sun4v_cpu_qconf(type, paddr, num_entries);
725 if (status != HV_EOK) {
726 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
727 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800728 prom_halt();
729 }
730}
731
David S. Millerb5a37e92006-02-11 23:07:13 -0800732static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c0572006-02-08 02:53:50 -0800733{
David S. Millerb5a37e92006-02-11 23:07:13 -0800734 struct trap_per_cpu *tb = &trap_block[this_cpu];
735
736 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
737 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
738 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
739 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
740}
741
742static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
743{
744 void *page;
745
746 if (use_bootmem)
747 page = alloc_bootmem_low_pages(PAGE_SIZE);
748 else
749 page = (void *) get_zeroed_page(GFP_ATOMIC);
750
751 if (!page) {
752 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
753 prom_halt();
754 }
755
756 *pa_ptr = __pa(page);
757}
758
759static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
760{
761 void *page;
762
763 if (use_bootmem)
764 page = alloc_bootmem_low_pages(PAGE_SIZE);
765 else
766 page = (void *) get_zeroed_page(GFP_ATOMIC);
David S. Miller5b0c0572006-02-08 02:53:50 -0800767
768 if (!page) {
769 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
770 prom_halt();
771 }
772
773 *pa_ptr = __pa(page);
774}
775
David S. Millerb5a37e92006-02-11 23:07:13 -0800776static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800777{
778#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800779 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800780
781 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
782
David S. Millerb5a37e92006-02-11 23:07:13 -0800783 if (use_bootmem)
784 page = alloc_bootmem_low_pages(PAGE_SIZE);
785 else
786 page = (void *) get_zeroed_page(GFP_ATOMIC);
787
David S. Miller1d2f1f92006-02-08 16:41:20 -0800788 if (!page) {
789 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
790 prom_halt();
791 }
792
793 tb->cpu_mondo_block_pa = __pa(page);
794 tb->cpu_list_pa = __pa(page + 64);
795#endif
796}
797
David S. Millerb5a37e92006-02-11 23:07:13 -0800798/* Allocate and register the mondo and error queues for this cpu. */
David S. Miller72aff532006-02-17 01:29:17 -0800799void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
David S. Millerac29c112006-02-08 00:08:23 -0800800{
David S. Millerac29c112006-02-08 00:08:23 -0800801 struct trap_per_cpu *tb = &trap_block[cpu];
802
David S. Miller72aff532006-02-17 01:29:17 -0800803 if (alloc) {
804 alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
805 alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
806 alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
807 alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
808 alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
809 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800810
David S. Miller72aff532006-02-17 01:29:17 -0800811 init_cpu_send_mondo_info(tb, use_bootmem);
812 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800813
David S. Miller72aff532006-02-17 01:29:17 -0800814 if (load) {
815 if (cpu != hard_smp_processor_id()) {
816 prom_printf("SUN4V: init mondo on cpu %d not %d\n",
817 cpu, hard_smp_processor_id());
818 prom_halt();
819 }
820 sun4v_register_mondo_queues(cpu);
821 }
David S. Millerac29c112006-02-08 00:08:23 -0800822}
823
David S. Millere18e2a02006-06-20 01:23:32 -0700824static struct irqaction timer_irq_action = {
825 .name = "timer",
826};
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/* Only invoked on boot processor. */
829void __init init_IRQ(void)
830{
831 map_prom_timers();
832 kill_prom_timer();
833 memset(&ivector_table[0], 0, sizeof(ivector_table));
834
David S. Millerac29c112006-02-08 00:08:23 -0800835 if (tlb_type == hypervisor)
David S. Miller72aff532006-02-17 01:29:17 -0800836 sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
David S. Millerac29c112006-02-08 00:08:23 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* We need to clear any IRQ's pending in the soft interrupt
839 * registers, a spurious one could be left around from the
840 * PROM timer which we just disabled.
841 */
842 clear_softint(get_softint());
843
844 /* Now that ivector table is initialized, it is safe
845 * to receive IRQ vector traps. We will normally take
846 * one or two right now, in case some device PROM used
847 * to boot us wants to speak to us. We just ignore them.
848 */
849 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
850 "or %%g1, %0, %%g1\n\t"
851 "wrpr %%g1, 0x0, %%pstate"
852 : /* No outputs */
853 : "i" (PSTATE_IE)
854 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -0700855
856 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}