blob: 2873835e2626af60633c6d9a116c32d901c760df [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
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>
Sven Hartge2e457ef2005-10-08 21:12:04 -070031#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/sbus.h>
33#include <asm/iommu.h>
34#include <asm/upa.h>
35#include <asm/oplib.h>
David S. Miller25c75812006-06-22 20:21:22 -070036#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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>
David S. Miller63b61452005-06-27 17:04:45 -070043#include <asm/auxio.h>
David S. Miller92704a12006-02-26 23:27:19 -080044#include <asm/head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* 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.
54 * The IVEC handler does not need to act atomically, the PIL dispatch
55 * code uses CAS to get an atomic snapshot of the list and clear it
56 * at the same time.
David S. Millere18e2a02006-06-20 01:23:32 -070057 *
58 * If you make changes to ino_bucket, please update hand coded assembler
59 * of the vectored interrupt trap handler(s) in entry.S and sun4v_ivec.S
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
David S. Millere18e2a02006-06-20 01:23:32 -070061struct ino_bucket {
62 /* Next handler in per-CPU IRQ worklist. We know that
63 * bucket pointers have the high 32-bits clear, so to
64 * save space we only store the bits we need.
65 */
66/*0x00*/unsigned int irq_chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David S. Millere18e2a02006-06-20 01:23:32 -070068 /* Virtual interrupt number assigned to this INO. */
69/*0x04*/unsigned int virt_irq;
70};
71
72#define NUM_IVECS (IMAP_INR + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct ino_bucket ivector_table[NUM_IVECS] __attribute__ ((aligned (SMP_CACHE_BYTES)));
74
David S. Millere18e2a02006-06-20 01:23:32 -070075#define __irq_ino(irq) \
76 (((struct ino_bucket *)(unsigned long)(irq)) - &ivector_table[0])
77#define __bucket(irq) ((struct ino_bucket *)(unsigned long)(irq))
78#define __irq(bucket) ((unsigned int)(unsigned long)(bucket))
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* This has to be in the main kernel image, it cannot be
81 * turned into per-cpu data. The reason is that the main
82 * kernel image is locked into the TLB and this structure
83 * is accessed from the vectored interrupt trap handler. If
84 * access to this structure takes a TLB miss it could cause
85 * the 5-level sparc v9 trap stack to overflow.
86 */
David S. Millerfd0504c32006-06-20 01:20:00 -070087#define irq_work(__cpu) &(trap_block[(__cpu)].irq_worklist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David S. Miller8047e242006-06-20 01:22:35 -070089static unsigned int virt_to_real_irq_table[NR_IRQS];
90static unsigned char virt_irq_cur = 1;
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
98 ent = virt_irq_cur;
99 if (ent >= NR_IRQS) {
100 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
101 return 0;
102 }
103
104 virt_irq_cur = ent + 1;
105 virt_to_real_irq_table[ent] = real_irq;
106
107 return ent;
108}
109
110#if 0 /* Currently unused. */
111static unsigned char real_to_virt_irq(unsigned int real_irq)
112{
113 struct ino_bucket *bucket = __bucket(real_irq);
114
115 return bucket->virt_irq;
116}
117#endif
118
119static unsigned int virt_to_real_irq(unsigned char virt_irq)
120{
121 return virt_to_real_irq_table[virt_irq];
122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
David S. Millere18e2a02006-06-20 01:23:32 -0700125 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128int show_interrupts(struct seq_file *p, void *v)
129{
David S. Millere18e2a02006-06-20 01:23:32 -0700130 int i = *(loff_t *) v, j;
131 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David S. Millere18e2a02006-06-20 01:23:32 -0700134 if (i == 0) {
135 seq_printf(p, " ");
136 for_each_online_cpu(j)
137 seq_printf(p, "CPU%d ",j);
138 seq_putc(p, '\n');
139 }
140
141 if (i < NR_IRQS) {
142 spin_lock_irqsave(&irq_desc[i].lock, flags);
143 action = irq_desc[i].action;
144 if (!action)
145 goto skip;
146 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifndef CONFIG_SMP
148 seq_printf(p, "%10u ", kstat_irqs(i));
149#else
David S. Millere18e2a02006-06-20 01:23:32 -0700150 for_each_online_cpu(j)
151 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700153 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700154 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
David S. Millere18e2a02006-06-20 01:23:32 -0700156 for (action=action->next; action; action = action->next)
157 seq_printf(p, ", %s", action->name);
158
159 seq_putc(p, '\n');
160skip:
161 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
164}
165
David S. Millerebd8c562006-02-17 08:38:06 -0800166extern unsigned long real_hard_smp_processor_id(void);
167
168static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
169{
170 unsigned int tid;
171
172 if (this_is_starfire) {
173 tid = starfire_translate(imap, cpuid);
174 tid <<= IMAP_TID_SHIFT;
175 tid &= IMAP_TID_UPA;
176 } else {
177 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
178 unsigned long ver;
179
180 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
181 if ((ver >> 32UL) == __JALAPENO_ID ||
182 (ver >> 32UL) == __SERRANO_ID) {
183 tid = cpuid << IMAP_TID_SHIFT;
184 tid &= IMAP_TID_JBUS;
185 } else {
186 unsigned int a = cpuid & 0x1f;
187 unsigned int n = (cpuid >> 5) & 0x1f;
188
189 tid = ((a << IMAP_AID_SHIFT) |
190 (n << IMAP_NID_SHIFT));
191 tid &= (IMAP_AID_SAFARI |
192 IMAP_NID_SAFARI);;
193 }
194 } else {
195 tid = cpuid << IMAP_TID_SHIFT;
196 tid &= IMAP_TID_UPA;
197 }
198 }
199
200 return tid;
201}
202
David S. Millere18e2a02006-06-20 01:23:32 -0700203struct irq_handler_data {
204 unsigned long iclr;
205 unsigned long imap;
206
207 void (*pre_handler)(unsigned int, void *, void *);
208 void *pre_handler_arg1;
209 void *pre_handler_arg2;
210};
211
212static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David S. Miller8047e242006-06-20 01:22:35 -0700214 unsigned int real_irq = virt_to_real_irq(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700215 struct ino_bucket *bucket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
David S. Millere18e2a02006-06-20 01:23:32 -0700217 if (likely(real_irq))
218 bucket = __bucket(real_irq);
David S. Miller8047e242006-06-20 01:22:35 -0700219
David S. Millere18e2a02006-06-20 01:23:32 -0700220 return bucket;
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David S. Millere18e2a02006-06-20 01:23:32 -0700223#ifdef CONFIG_SMP
224static int irq_choose_cpu(unsigned int virt_irq)
225{
Ingo Molnara53da522006-06-29 02:24:38 -0700226 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700227 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
David S. Millere18e2a02006-06-20 01:23:32 -0700229 if (cpus_equal(mask, CPU_MASK_ALL)) {
230 static int irq_rover;
231 static DEFINE_SPINLOCK(irq_rover_lock);
232 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800233
David S. Millere18e2a02006-06-20 01:23:32 -0700234 /* Round-robin distribution... */
235 do_round_robin:
236 spin_lock_irqsave(&irq_rover_lock, flags);
237
238 while (!cpu_online(irq_rover)) {
239 if (++irq_rover >= NR_CPUS)
240 irq_rover = 0;
241 }
242 cpuid = irq_rover;
243 do {
244 if (++irq_rover >= NR_CPUS)
245 irq_rover = 0;
246 } while (!cpu_online(irq_rover));
247
248 spin_unlock_irqrestore(&irq_rover_lock, flags);
249 } else {
250 cpumask_t tmp;
251
252 cpus_and(tmp, cpu_online_map, mask);
253
254 if (cpus_empty(tmp))
255 goto do_round_robin;
256
257 cpuid = first_cpu(tmp);
258 }
259
260 return cpuid;
261}
262#else
263static int irq_choose_cpu(unsigned int virt_irq)
264{
265 return real_hard_smp_processor_id();
266}
267#endif
268
269static void sun4u_irq_enable(unsigned int virt_irq)
270{
David S. Miller68c92182007-01-29 12:12:28 -0800271 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700272
273 if (likely(data)) {
274 unsigned long cpuid, imap;
275 unsigned int tid;
276
277 cpuid = irq_choose_cpu(virt_irq);
278 imap = data->imap;
279
280 tid = sun4u_compute_tid(imap, cpuid);
281
282 upa_writel(tid | IMAP_VALID, imap);
283 }
284}
285
286static void sun4u_irq_disable(unsigned int virt_irq)
287{
David S. Miller68c92182007-01-29 12:12:28 -0800288 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700289
290 if (likely(data)) {
291 unsigned long imap = data->imap;
292 u32 tmp = upa_readl(imap);
293
294 tmp &= ~IMAP_VALID;
295 upa_writel(tmp, imap);
296 }
297}
298
299static void sun4u_irq_end(unsigned int virt_irq)
300{
David S. Miller68c92182007-01-29 12:12:28 -0800301 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700302
303 if (likely(data))
304 upa_writel(ICLR_IDLE, data->iclr);
305}
306
307static void sun4v_irq_enable(unsigned int virt_irq)
308{
309 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
310 unsigned int ino = bucket - &ivector_table[0];
311
312 if (likely(bucket)) {
313 unsigned long cpuid;
David S. Millerc4bea282006-02-13 22:56:27 -0800314 int err;
David S. Miller10951ee2006-02-13 18:22:57 -0800315
David S. Millere18e2a02006-06-20 01:23:32 -0700316 cpuid = irq_choose_cpu(virt_irq);
317
David S. Millerebd8c562006-02-17 08:38:06 -0800318 err = sun4v_intr_settarget(ino, cpuid);
David S. Millerc4bea282006-02-13 22:56:27 -0800319 if (err != HV_EOK)
David S. Millerebd8c562006-02-17 08:38:06 -0800320 printk("sun4v_intr_settarget(%x,%lu): err(%d)\n",
321 ino, cpuid, err);
David S. Millerabd92b22006-02-14 22:20:13 -0800322 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
David S. Millerc4bea282006-02-13 22:56:27 -0800323 if (err != HV_EOK)
324 printk("sun4v_intr_setenabled(%x): err(%d)\n",
325 ino, err);
David S. Millerd82ace72006-02-09 02:52:44 -0800326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
David S. Millere18e2a02006-06-20 01:23:32 -0700329static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
David S. Millere18e2a02006-06-20 01:23:32 -0700331 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
332 unsigned int ino = bucket - &ivector_table[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
David S. Millere18e2a02006-06-20 01:23:32 -0700334 if (likely(bucket)) {
David S. Miller8047e242006-06-20 01:22:35 -0700335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David S. Miller8047e242006-06-20 01:22:35 -0700337 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
338 if (err != HV_EOK)
339 printk("sun4v_intr_setenabled(%x): "
340 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342}
343
David S. Millere18e2a02006-06-20 01:23:32 -0700344static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700345{
David S. Millere18e2a02006-06-20 01:23:32 -0700346 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
347 unsigned int ino = bucket - &ivector_table[0];
348
349 if (likely(bucket)) {
350 int err;
351
352 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
353 if (err != HV_EOK)
354 printk("sun4v_intr_setstate(%x): "
355 "err(%d)\n", ino, err);
356 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700357}
358
David S. Millere18e2a02006-06-20 01:23:32 -0700359static void run_pre_handler(unsigned int virt_irq)
360{
361 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800362 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700363
364 if (likely(data->pre_handler)) {
365 data->pre_handler(__irq_ino(__irq(bucket)),
366 data->pre_handler_arg1,
367 data->pre_handler_arg2);
368 }
369}
370
David S. Miller729e7d72006-12-12 00:59:12 -0800371static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700372 .typename = "sun4u",
373 .enable = sun4u_irq_enable,
374 .disable = sun4u_irq_disable,
375 .end = sun4u_irq_end,
376};
377
David S. Miller729e7d72006-12-12 00:59:12 -0800378static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700379 .typename = "sun4u+ack",
380 .enable = sun4u_irq_enable,
381 .disable = sun4u_irq_disable,
382 .ack = run_pre_handler,
383 .end = sun4u_irq_end,
384};
385
David S. Miller729e7d72006-12-12 00:59:12 -0800386static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700387 .typename = "sun4v",
388 .enable = sun4v_irq_enable,
389 .disable = sun4v_irq_disable,
390 .end = sun4v_irq_end,
391};
392
David S. Miller729e7d72006-12-12 00:59:12 -0800393static struct irq_chip sun4v_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700394 .typename = "sun4v+ack",
395 .enable = sun4v_irq_enable,
396 .disable = sun4v_irq_disable,
397 .ack = run_pre_handler,
398 .end = sun4v_irq_end,
399};
400
401void irq_install_pre_handler(int virt_irq,
402 void (*func)(unsigned int, void *, void *),
403 void *arg1, void *arg2)
404{
David S. Miller68c92182007-01-29 12:12:28 -0800405 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
406 struct irq_chip *chip;
David S. Millere18e2a02006-06-20 01:23:32 -0700407
408 data->pre_handler = func;
409 data->pre_handler_arg1 = arg1;
410 data->pre_handler_arg2 = arg2;
411
David S. Miller68c92182007-01-29 12:12:28 -0800412 chip = get_irq_chip(virt_irq);
413 if (chip == &sun4u_irq_ack ||
414 chip == &sun4v_irq_ack)
David S. Miller24ac26d2006-06-29 14:38:21 -0700415 return;
416
David S. Miller68c92182007-01-29 12:12:28 -0800417 chip = (chip == &sun4u_irq ?
418 &sun4u_irq_ack : &sun4v_irq_ack);
419 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700420}
421
422unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700425 struct irq_handler_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int ino;
427
David S. Miller10951ee2006-02-13 18:22:57 -0800428 BUG_ON(tlb_type == hypervisor);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ino = (upa_readl(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700431 bucket = &ivector_table[ino];
David S. Millere18e2a02006-06-20 01:23:32 -0700432 if (!bucket->virt_irq) {
433 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800434 set_irq_chip(bucket->virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700435 }
436
David S. Miller68c92182007-01-29 12:12:28 -0800437 data = get_irq_chip_data(bucket->virt_irq);
438 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700439 goto out;
440
441 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
442 if (unlikely(!data)) {
443 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700444 prom_halt();
445 }
David S. Miller68c92182007-01-29 12:12:28 -0800446 set_irq_chip_data(bucket->virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700447
David S. Millere18e2a02006-06-20 01:23:32 -0700448 data->imap = imap;
449 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David S. Miller088dd1f2005-07-04 13:24:38 -0700451out:
David S. Miller8047e242006-06-20 01:22:35 -0700452 return bucket->virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
David S. Millere18e2a02006-06-20 01:23:32 -0700455unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
David S. Millere3999572006-02-13 18:16:10 -0800456{
457 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700458 struct irq_handler_data *data;
David S. Millere3999572006-02-13 18:16:10 -0800459 unsigned long sysino;
David S. Millere18e2a02006-06-20 01:23:32 -0700460
461 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800462
463 sysino = sun4v_devino_to_sysino(devhandle, devino);
David S. Millere3999572006-02-13 18:16:10 -0800464 bucket = &ivector_table[sysino];
David S. Millere18e2a02006-06-20 01:23:32 -0700465 if (!bucket->virt_irq) {
466 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800467 set_irq_chip(bucket->virt_irq, &sun4v_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700468 }
469
David S. Miller68c92182007-01-29 12:12:28 -0800470 data = get_irq_chip_data(bucket->virt_irq);
471 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700472 goto out;
473
474 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
475 if (unlikely(!data)) {
476 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
477 prom_halt();
478 }
David S. Miller68c92182007-01-29 12:12:28 -0800479 set_irq_chip_data(bucket->virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800480
481 /* Catch accidental accesses to these things. IMAP/ICLR handling
482 * is done by hypervisor calls on sun4v platforms, not by direct
483 * register accesses.
484 */
David S. Millere18e2a02006-06-20 01:23:32 -0700485 data->imap = ~0UL;
486 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800487
David S. Millere18e2a02006-06-20 01:23:32 -0700488out:
David S. Miller8047e242006-06-20 01:22:35 -0700489 return bucket->virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800490}
491
David S. Millere18e2a02006-06-20 01:23:32 -0700492void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700493{
David S. Millere18e2a02006-06-20 01:23:32 -0700494 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
495 unsigned int ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700496
David S. Millere18e2a02006-06-20 01:23:32 -0700497 if (bucket)
498 ino = bucket - &ivector_table[0];
David S. Miller088dd1f2005-07-04 13:24:38 -0700499
David S. Millere18e2a02006-06-20 01:23:32 -0700500 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
501 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
David S. Millerfd0504c32006-06-20 01:20:00 -0700504#ifndef CONFIG_SMP
Al Viro63540ba2006-10-09 11:51:14 +0100505extern irqreturn_t timer_interrupt(int, void *);
David S. Millerfd0504c32006-06-20 01:20:00 -0700506
507void timer_irq(int irq, struct pt_regs *regs)
508{
509 unsigned long clr_mask = 1 << irq;
510 unsigned long tick_mask = tick_ops->softint_mask;
Al Viro63540ba2006-10-09 11:51:14 +0100511 struct pt_regs *old_regs;
David S. Millerfd0504c32006-06-20 01:20:00 -0700512
513 if (get_softint() & tick_mask) {
514 irq = 0;
515 clr_mask = tick_mask;
516 }
517 clear_softint(clr_mask);
518
Al Viro63540ba2006-10-09 11:51:14 +0100519 old_regs = set_irq_regs(regs);
David S. Millerfd0504c32006-06-20 01:20:00 -0700520 irq_enter();
David S. Millere18e2a02006-06-20 01:23:32 -0700521
David S. Miller8047e242006-06-20 01:22:35 -0700522 kstat_this_cpu.irqs[0]++;
Al Viro63540ba2006-10-09 11:51:14 +0100523 timer_interrupt(irq, NULL);
David S. Millere18e2a02006-06-20 01:23:32 -0700524
David S. Millerfd0504c32006-06-20 01:20:00 -0700525 irq_exit();
Al Viro63540ba2006-10-09 11:51:14 +0100526 set_irq_regs(old_regs);
David S. Millerfd0504c32006-06-20 01:20:00 -0700527}
528#endif
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530void handler_irq(int irq, struct pt_regs *regs)
531{
David S. Millere18e2a02006-06-20 01:23:32 -0700532 struct ino_bucket *bucket;
Al Viro6d24c8d2006-10-08 08:23:28 -0400533 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Al Viro6d24c8d2006-10-08 08:23:28 -0400537 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Sliiiick... */
David S. Millere18e2a02006-06-20 01:23:32 -0700541 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
542 while (bucket) {
543 struct ino_bucket *next = __bucket(bucket->irq_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David S. Millere18e2a02006-06-20 01:23:32 -0700545 bucket->irq_chain = 0;
Al Viro6d24c8d2006-10-08 08:23:28 -0400546 __do_IRQ(bucket->virt_irq);
David S. Millerfd0504c32006-06-20 01:20:00 -0700547
David S. Millere18e2a02006-06-20 01:23:32 -0700548 bucket = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
David S. Millere18e2a02006-06-20 01:23:32 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400552 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
David S. Millercdd51862005-07-24 19:36:13 -0700555struct sun5_timer {
556 u64 count0;
557 u64 limit0;
558 u64 count1;
559 u64 limit1;
560};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
David S. Millercdd51862005-07-24 19:36:13 -0700562static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static u64 prom_limit0, prom_limit1;
564
565static void map_prom_timers(void)
566{
David S. Miller25c75812006-06-22 20:21:22 -0700567 struct device_node *dp;
568 unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700571 dp = of_find_node_by_path("/");
572 dp = dp->child;
573 while (dp) {
574 if (!strcmp(dp->name, "counter-timer"))
575 break;
576 dp = dp->sibling;
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* Assume if node is not present, PROM uses different tick mechanism
580 * which we should not care about.
581 */
David S. Miller25c75812006-06-22 20:21:22 -0700582 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 prom_timers = (struct sun5_timer *) 0;
584 return;
585 }
586
587 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700588 addr = of_get_property(dp, "address", NULL);
589 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 prom_printf("PROM does not have timer mapped, trying to continue.\n");
591 prom_timers = (struct sun5_timer *) 0;
592 return;
593 }
594 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
595}
596
597static void kill_prom_timer(void)
598{
599 if (!prom_timers)
600 return;
601
602 /* Save them away for later. */
603 prom_limit0 = prom_timers->limit0;
604 prom_limit1 = prom_timers->limit1;
605
606 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
607 * We turn both off here just to be paranoid.
608 */
609 prom_timers->limit0 = 0;
610 prom_timers->limit1 = 0;
611
612 /* Wheee, eat the interrupt packet too... */
613 __asm__ __volatile__(
614" mov 0x40, %%g2\n"
615" ldxa [%%g0] %0, %%g1\n"
616" ldxa [%%g2] %1, %%g1\n"
617" stxa %%g0, [%%g0] %0\n"
618" membar #Sync\n"
619 : /* no outputs */
620 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
621 : "g1", "g2");
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624void init_irqwork_curcpu(void)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int cpu = hard_smp_processor_id();
627
David S. Millerfd0504c32006-06-20 01:20:00 -0700628 trap_block[cpu].irq_worklist = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
David S. Millerb5a37e92006-02-11 23:07:13 -0800631static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type)
David S. Millerac29c112006-02-08 00:08:23 -0800632{
David S. Miller94f87622006-02-16 14:26:53 -0800633 unsigned long num_entries = 128;
634 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800635
David S. Miller94f87622006-02-16 14:26:53 -0800636 status = sun4v_cpu_qconf(type, paddr, num_entries);
637 if (status != HV_EOK) {
638 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
639 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800640 prom_halt();
641 }
642}
643
David S. Millerb5a37e92006-02-11 23:07:13 -0800644static void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c0572006-02-08 02:53:50 -0800645{
David S. Millerb5a37e92006-02-11 23:07:13 -0800646 struct trap_per_cpu *tb = &trap_block[this_cpu];
647
648 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO);
649 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO);
650 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR);
651 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR);
652}
653
654static void __cpuinit alloc_one_mondo(unsigned long *pa_ptr, int use_bootmem)
655{
656 void *page;
657
658 if (use_bootmem)
659 page = alloc_bootmem_low_pages(PAGE_SIZE);
660 else
661 page = (void *) get_zeroed_page(GFP_ATOMIC);
662
663 if (!page) {
664 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
665 prom_halt();
666 }
667
668 *pa_ptr = __pa(page);
669}
670
671static void __cpuinit alloc_one_kbuf(unsigned long *pa_ptr, int use_bootmem)
672{
673 void *page;
674
675 if (use_bootmem)
676 page = alloc_bootmem_low_pages(PAGE_SIZE);
677 else
678 page = (void *) get_zeroed_page(GFP_ATOMIC);
David S. Miller5b0c0572006-02-08 02:53:50 -0800679
680 if (!page) {
681 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
682 prom_halt();
683 }
684
685 *pa_ptr = __pa(page);
686}
687
David S. Millerb5a37e92006-02-11 23:07:13 -0800688static void __cpuinit init_cpu_send_mondo_info(struct trap_per_cpu *tb, int use_bootmem)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800689{
690#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800691 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800692
693 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
694
David S. Millerb5a37e92006-02-11 23:07:13 -0800695 if (use_bootmem)
696 page = alloc_bootmem_low_pages(PAGE_SIZE);
697 else
698 page = (void *) get_zeroed_page(GFP_ATOMIC);
699
David S. Miller1d2f1f92006-02-08 16:41:20 -0800700 if (!page) {
701 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
702 prom_halt();
703 }
704
705 tb->cpu_mondo_block_pa = __pa(page);
706 tb->cpu_list_pa = __pa(page + 64);
707#endif
708}
709
David S. Millerb5a37e92006-02-11 23:07:13 -0800710/* Allocate and register the mondo and error queues for this cpu. */
David S. Miller72aff532006-02-17 01:29:17 -0800711void __cpuinit sun4v_init_mondo_queues(int use_bootmem, int cpu, int alloc, int load)
David S. Millerac29c112006-02-08 00:08:23 -0800712{
David S. Millerac29c112006-02-08 00:08:23 -0800713 struct trap_per_cpu *tb = &trap_block[cpu];
714
David S. Miller72aff532006-02-17 01:29:17 -0800715 if (alloc) {
716 alloc_one_mondo(&tb->cpu_mondo_pa, use_bootmem);
717 alloc_one_mondo(&tb->dev_mondo_pa, use_bootmem);
718 alloc_one_mondo(&tb->resum_mondo_pa, use_bootmem);
719 alloc_one_kbuf(&tb->resum_kernel_buf_pa, use_bootmem);
720 alloc_one_mondo(&tb->nonresum_mondo_pa, use_bootmem);
721 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa, use_bootmem);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800722
David S. Miller72aff532006-02-17 01:29:17 -0800723 init_cpu_send_mondo_info(tb, use_bootmem);
724 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800725
David S. Miller72aff532006-02-17 01:29:17 -0800726 if (load) {
727 if (cpu != hard_smp_processor_id()) {
728 prom_printf("SUN4V: init mondo on cpu %d not %d\n",
729 cpu, hard_smp_processor_id());
730 prom_halt();
731 }
732 sun4v_register_mondo_queues(cpu);
733 }
David S. Millerac29c112006-02-08 00:08:23 -0800734}
735
David S. Millere18e2a02006-06-20 01:23:32 -0700736static struct irqaction timer_irq_action = {
737 .name = "timer",
738};
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* Only invoked on boot processor. */
741void __init init_IRQ(void)
742{
743 map_prom_timers();
744 kill_prom_timer();
745 memset(&ivector_table[0], 0, sizeof(ivector_table));
746
David S. Millerac29c112006-02-08 00:08:23 -0800747 if (tlb_type == hypervisor)
David S. Miller72aff532006-02-17 01:29:17 -0800748 sun4v_init_mondo_queues(1, hard_smp_processor_id(), 1, 1);
David S. Millerac29c112006-02-08 00:08:23 -0800749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* We need to clear any IRQ's pending in the soft interrupt
751 * registers, a spurious one could be left around from the
752 * PROM timer which we just disabled.
753 */
754 clear_softint(get_softint());
755
756 /* Now that ivector table is initialized, it is safe
757 * to receive IRQ vector traps. We will normally take
758 * one or two right now, in case some device PROM used
759 * to boot us wants to speak to us. We just ignore them.
760 */
761 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
762 "or %%g1, %0, %%g1\n\t"
763 "wrpr %%g1, 0x0, %%pstate"
764 : /* No outputs */
765 : "i" (PSTATE_IE)
766 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -0700767
768 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}