blob: 045ab27d4271324fcebe9fcdc042db5c3958f055 [file] [log] [blame]
David S. Miller4a907de2007-06-13 00:01:04 -07001/* irq.c: UltraSparc IRQ handling/init/registry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller4a907de2007-06-13 00:01:04 -07003 * Copyright (C) 1997, 2007 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#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>
David S. Millerb5a37e92006-02-11 23:07:13 -080022#include <linux/bootmem.h>
David S. Millere18e2a02006-06-20 01:23:32 -070023#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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>
Sven Hartge2e457ef2005-10-08 21:12:04 -070030#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/sbus.h>
32#include <asm/iommu.h>
33#include <asm/upa.h>
34#include <asm/oplib.h>
David S. Miller25c75812006-06-22 20:21:22 -070035#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
David S. Miller63b61452005-06-27 17:04:45 -070042#include <asm/auxio.h>
David S. Miller92704a12006-02-26 23:27:19 -080043#include <asm/head.h>
David S. Miller4a907de2007-06-13 00:01:04 -070044#include <asm/hypervisor.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. Miller93b32382007-07-20 02:58:28 -070089static struct {
90 unsigned int irq;
91 unsigned int dev_handle;
92 unsigned int dev_ino;
93} virt_to_real_irq_table[NR_IRQS];
David S. Miller759f89e2007-10-11 03:16:13 -070094static DEFINE_SPINLOCK(virt_irq_alloc_lock);
David S. Miller8047e242006-06-20 01:22:35 -070095
David S. Miller759f89e2007-10-11 03:16:13 -070096unsigned char virt_irq_alloc(unsigned int real_irq)
David S. Miller8047e242006-06-20 01:22:35 -070097{
David S. Miller759f89e2007-10-11 03:16:13 -070098 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -070099 unsigned char ent;
100
101 BUILD_BUG_ON(NR_IRQS >= 256);
102
David S. Miller759f89e2007-10-11 03:16:13 -0700103 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
104
David S. Miller35a17eb2007-02-10 17:41:02 -0800105 for (ent = 1; ent < NR_IRQS; ent++) {
David S. Miller93b32382007-07-20 02:58:28 -0700106 if (!virt_to_real_irq_table[ent].irq)
David S. Miller35a17eb2007-02-10 17:41:02 -0800107 break;
108 }
David S. Miller8047e242006-06-20 01:22:35 -0700109 if (ent >= NR_IRQS) {
110 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
David S. Miller759f89e2007-10-11 03:16:13 -0700111 ent = 0;
112 } else {
113 virt_to_real_irq_table[ent].irq = real_irq;
David S. Miller8047e242006-06-20 01:22:35 -0700114 }
115
David S. Miller759f89e2007-10-11 03:16:13 -0700116 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700117
118 return ent;
119}
120
David S. Miller5746c992007-02-20 01:26:48 -0800121#ifdef CONFIG_PCI_MSI
David S. Miller759f89e2007-10-11 03:16:13 -0700122void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700123{
David S. Miller759f89e2007-10-11 03:16:13 -0700124 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700125
David S. Miller35a17eb2007-02-10 17:41:02 -0800126 if (virt_irq >= NR_IRQS)
127 return;
128
David S. Miller759f89e2007-10-11 03:16:13 -0700129 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
130
David S. Miller93b32382007-07-20 02:58:28 -0700131 virt_to_real_irq_table[virt_irq].irq = 0;
David S. Miller35a17eb2007-02-10 17:41:02 -0800132
David S. Miller759f89e2007-10-11 03:16:13 -0700133 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700134}
David S. Miller5746c992007-02-20 01:26:48 -0800135#endif
David S. Miller8047e242006-06-20 01:22:35 -0700136
137static unsigned int virt_to_real_irq(unsigned char virt_irq)
138{
David S. Miller93b32382007-07-20 02:58:28 -0700139 return virt_to_real_irq_table[virt_irq].irq;
David S. Miller8047e242006-06-20 01:22:35 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
David S. Millere18e2a02006-06-20 01:23:32 -0700143 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146int show_interrupts(struct seq_file *p, void *v)
147{
David S. Millere18e2a02006-06-20 01:23:32 -0700148 int i = *(loff_t *) v, j;
149 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David S. Millere18e2a02006-06-20 01:23:32 -0700152 if (i == 0) {
153 seq_printf(p, " ");
154 for_each_online_cpu(j)
155 seq_printf(p, "CPU%d ",j);
156 seq_putc(p, '\n');
157 }
158
159 if (i < NR_IRQS) {
160 spin_lock_irqsave(&irq_desc[i].lock, flags);
161 action = irq_desc[i].action;
162 if (!action)
163 goto skip;
164 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifndef CONFIG_SMP
166 seq_printf(p, "%10u ", kstat_irqs(i));
167#else
David S. Millere18e2a02006-06-20 01:23:32 -0700168 for_each_online_cpu(j)
169 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700171 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700172 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David S. Millere18e2a02006-06-20 01:23:32 -0700174 for (action=action->next; action; action = action->next)
175 seq_printf(p, ", %s", action->name);
176
177 seq_putc(p, '\n');
178skip:
179 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
183
David S. Millerebd8c562006-02-17 08:38:06 -0800184static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
185{
186 unsigned int tid;
187
188 if (this_is_starfire) {
189 tid = starfire_translate(imap, cpuid);
190 tid <<= IMAP_TID_SHIFT;
191 tid &= IMAP_TID_UPA;
192 } else {
193 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
194 unsigned long ver;
195
196 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
197 if ((ver >> 32UL) == __JALAPENO_ID ||
198 (ver >> 32UL) == __SERRANO_ID) {
199 tid = cpuid << IMAP_TID_SHIFT;
200 tid &= IMAP_TID_JBUS;
201 } else {
202 unsigned int a = cpuid & 0x1f;
203 unsigned int n = (cpuid >> 5) & 0x1f;
204
205 tid = ((a << IMAP_AID_SHIFT) |
206 (n << IMAP_NID_SHIFT));
207 tid &= (IMAP_AID_SAFARI |
208 IMAP_NID_SAFARI);;
209 }
210 } else {
211 tid = cpuid << IMAP_TID_SHIFT;
212 tid &= IMAP_TID_UPA;
213 }
214 }
215
216 return tid;
217}
218
David S. Millere18e2a02006-06-20 01:23:32 -0700219struct irq_handler_data {
220 unsigned long iclr;
221 unsigned long imap;
222
223 void (*pre_handler)(unsigned int, void *, void *);
224 void *pre_handler_arg1;
225 void *pre_handler_arg2;
226};
227
228static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
David S. Miller8047e242006-06-20 01:22:35 -0700230 unsigned int real_irq = virt_to_real_irq(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700231 struct ino_bucket *bucket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
David S. Millere18e2a02006-06-20 01:23:32 -0700233 if (likely(real_irq))
234 bucket = __bucket(real_irq);
David S. Miller8047e242006-06-20 01:22:35 -0700235
David S. Millere18e2a02006-06-20 01:23:32 -0700236 return bucket;
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
David S. Millere18e2a02006-06-20 01:23:32 -0700239#ifdef CONFIG_SMP
240static int irq_choose_cpu(unsigned int virt_irq)
241{
Ingo Molnara53da522006-06-29 02:24:38 -0700242 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700243 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
David S. Millere18e2a02006-06-20 01:23:32 -0700245 if (cpus_equal(mask, CPU_MASK_ALL)) {
246 static int irq_rover;
247 static DEFINE_SPINLOCK(irq_rover_lock);
248 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800249
David S. Millere18e2a02006-06-20 01:23:32 -0700250 /* Round-robin distribution... */
251 do_round_robin:
252 spin_lock_irqsave(&irq_rover_lock, flags);
253
254 while (!cpu_online(irq_rover)) {
255 if (++irq_rover >= NR_CPUS)
256 irq_rover = 0;
257 }
258 cpuid = irq_rover;
259 do {
260 if (++irq_rover >= NR_CPUS)
261 irq_rover = 0;
262 } while (!cpu_online(irq_rover));
263
264 spin_unlock_irqrestore(&irq_rover_lock, flags);
265 } else {
266 cpumask_t tmp;
267
268 cpus_and(tmp, cpu_online_map, mask);
269
270 if (cpus_empty(tmp))
271 goto do_round_robin;
272
273 cpuid = first_cpu(tmp);
274 }
275
276 return cpuid;
277}
278#else
279static int irq_choose_cpu(unsigned int virt_irq)
280{
281 return real_hard_smp_processor_id();
282}
283#endif
284
285static void sun4u_irq_enable(unsigned int virt_irq)
286{
David S. Miller68c92182007-01-29 12:12:28 -0800287 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700288
289 if (likely(data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700290 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700291 unsigned int tid;
292
293 cpuid = irq_choose_cpu(virt_irq);
294 imap = data->imap;
295
296 tid = sun4u_compute_tid(imap, cpuid);
297
David S. Miller861fe902007-05-02 17:31:36 -0700298 val = upa_readq(imap);
299 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
300 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
301 val |= tid | IMAP_VALID;
302 upa_writeq(val, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700303 }
304}
305
David S. Millerb53bcb62007-07-14 03:16:13 -0700306static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
307{
308 sun4u_irq_enable(virt_irq);
309}
310
David S. Millere18e2a02006-06-20 01:23:32 -0700311static void sun4u_irq_disable(unsigned int virt_irq)
312{
David S. Miller68c92182007-01-29 12:12:28 -0800313 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700314
315 if (likely(data)) {
316 unsigned long imap = data->imap;
David S. Miller6e69d602007-08-28 14:25:32 -0700317 unsigned long tmp = upa_readq(imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700318
319 tmp &= ~IMAP_VALID;
David S. Miller861fe902007-05-02 17:31:36 -0700320 upa_writeq(tmp, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700321 }
322}
323
324static void sun4u_irq_end(unsigned int virt_irq)
325{
David S. Miller68c92182007-01-29 12:12:28 -0800326 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700327 struct irq_desc *desc = irq_desc + virt_irq;
328
329 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
330 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700331
332 if (likely(data))
David S. Miller861fe902007-05-02 17:31:36 -0700333 upa_writeq(ICLR_IDLE, data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700334}
335
336static void sun4v_irq_enable(unsigned int virt_irq)
337{
338 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
339 unsigned int ino = bucket - &ivector_table[0];
340
341 if (likely(bucket)) {
342 unsigned long cpuid;
David S. Millerc4bea282006-02-13 22:56:27 -0800343 int err;
David S. Miller10951ee2006-02-13 18:22:57 -0800344
David S. Millere18e2a02006-06-20 01:23:32 -0700345 cpuid = irq_choose_cpu(virt_irq);
346
David S. Millerebd8c562006-02-17 08:38:06 -0800347 err = sun4v_intr_settarget(ino, cpuid);
David S. Millerc4bea282006-02-13 22:56:27 -0800348 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700349 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
350 "err(%d)\n", ino, cpuid, err);
David S. Millera357b8f2007-06-26 00:13:31 -0700351 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
352 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700353 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millera357b8f2007-06-26 00:13:31 -0700354 "err(%d)\n", ino, err);
David S. Millerabd92b22006-02-14 22:20:13 -0800355 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
David S. Millerc4bea282006-02-13 22:56:27 -0800356 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700357 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
David S. Millerc4bea282006-02-13 22:56:27 -0800358 ino, err);
David S. Millerd82ace72006-02-09 02:52:44 -0800359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
David S. Millerb53bcb62007-07-14 03:16:13 -0700362static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
363{
364 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
365 unsigned int ino = bucket - &ivector_table[0];
366
367 if (likely(bucket)) {
368 unsigned long cpuid;
369 int err;
370
371 cpuid = irq_choose_cpu(virt_irq);
372
373 err = sun4v_intr_settarget(ino, cpuid);
374 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700375 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
376 "err(%d)\n", ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700377 }
378}
379
David S. Millere18e2a02006-06-20 01:23:32 -0700380static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
David S. Millere18e2a02006-06-20 01:23:32 -0700382 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
383 unsigned int ino = bucket - &ivector_table[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David S. Millere18e2a02006-06-20 01:23:32 -0700385 if (likely(bucket)) {
David S. Miller8047e242006-06-20 01:22:35 -0700386 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David S. Miller8047e242006-06-20 01:22:35 -0700388 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
389 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700390 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
David S. Miller8047e242006-06-20 01:22:35 -0700391 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393}
394
David S. Millere18e2a02006-06-20 01:23:32 -0700395static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700396{
David S. Millere18e2a02006-06-20 01:23:32 -0700397 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
398 unsigned int ino = bucket - &ivector_table[0];
David S. Miller5a606b72007-07-09 22:40:36 -0700399 struct irq_desc *desc = irq_desc + virt_irq;
400
401 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
402 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700403
404 if (likely(bucket)) {
405 int err;
406
407 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
408 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700409 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millere18e2a02006-06-20 01:23:32 -0700410 "err(%d)\n", ino, err);
411 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700412}
413
David S. Miller4a907de2007-06-13 00:01:04 -0700414static void sun4v_virq_enable(unsigned int virt_irq)
415{
416 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700417
418 if (likely(bucket)) {
419 unsigned long cpuid, dev_handle, dev_ino;
420 int err;
421
422 cpuid = irq_choose_cpu(virt_irq);
423
David S. Miller93b32382007-07-20 02:58:28 -0700424 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
425 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700426
427 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
428 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700429 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Miller4a907de2007-06-13 00:01:04 -0700430 "err(%d)\n",
431 dev_handle, dev_ino, cpuid, err);
432 err = sun4v_vintr_set_state(dev_handle, dev_ino,
David S. Miller12450882007-06-26 00:13:09 -0700433 HV_INTR_STATE_IDLE);
434 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700435 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller12450882007-06-26 00:13:09 -0700436 "HV_INTR_STATE_IDLE): err(%d)\n",
437 dev_handle, dev_ino, err);
438 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700439 HV_INTR_ENABLED);
440 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700441 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700442 "HV_INTR_ENABLED): err(%d)\n",
443 dev_handle, dev_ino, err);
444 }
445}
446
David S. Millerb53bcb62007-07-14 03:16:13 -0700447static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
448{
449 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Millerb53bcb62007-07-14 03:16:13 -0700450
451 if (likely(bucket)) {
452 unsigned long cpuid, dev_handle, dev_ino;
453 int err;
454
455 cpuid = irq_choose_cpu(virt_irq);
456
David S. Miller93b32382007-07-20 02:58:28 -0700457 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
458 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millerb53bcb62007-07-14 03:16:13 -0700459
460 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
461 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700462 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Millerb53bcb62007-07-14 03:16:13 -0700463 "err(%d)\n",
464 dev_handle, dev_ino, cpuid, err);
465 }
466}
467
David S. Miller4a907de2007-06-13 00:01:04 -0700468static void sun4v_virq_disable(unsigned int virt_irq)
469{
470 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700471
472 if (likely(bucket)) {
473 unsigned long dev_handle, dev_ino;
474 int err;
475
David S. Miller93b32382007-07-20 02:58:28 -0700476 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
477 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700478
David S. Miller12450882007-06-26 00:13:09 -0700479 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700480 HV_INTR_DISABLED);
481 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700482 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700483 "HV_INTR_DISABLED): err(%d)\n",
484 dev_handle, dev_ino, err);
485 }
486}
487
488static void sun4v_virq_end(unsigned int virt_irq)
489{
490 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700491 struct irq_desc *desc = irq_desc + virt_irq;
492
493 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
494 return;
David S. Miller4a907de2007-06-13 00:01:04 -0700495
496 if (likely(bucket)) {
497 unsigned long dev_handle, dev_ino;
498 int err;
499
David S. Miller93b32382007-07-20 02:58:28 -0700500 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
501 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700502
503 err = sun4v_vintr_set_state(dev_handle, dev_ino,
504 HV_INTR_STATE_IDLE);
505 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700506 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700507 "HV_INTR_STATE_IDLE): err(%d)\n",
508 dev_handle, dev_ino, err);
509 }
510}
511
David S. Millere18e2a02006-06-20 01:23:32 -0700512static void run_pre_handler(unsigned int virt_irq)
513{
514 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800515 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700516
517 if (likely(data->pre_handler)) {
518 data->pre_handler(__irq_ino(__irq(bucket)),
519 data->pre_handler_arg1,
520 data->pre_handler_arg2);
521 }
522}
523
David S. Miller729e7d72006-12-12 00:59:12 -0800524static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700525 .typename = "sun4u",
526 .enable = sun4u_irq_enable,
527 .disable = sun4u_irq_disable,
528 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700529 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700530};
531
David S. Miller729e7d72006-12-12 00:59:12 -0800532static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700533 .typename = "sun4u+ack",
534 .enable = sun4u_irq_enable,
535 .disable = sun4u_irq_disable,
536 .ack = run_pre_handler,
537 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700538 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700539};
540
David S. Miller729e7d72006-12-12 00:59:12 -0800541static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700542 .typename = "sun4v",
543 .enable = sun4v_irq_enable,
544 .disable = sun4v_irq_disable,
545 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700546 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700547};
548
David S. Miller4a907de2007-06-13 00:01:04 -0700549static struct irq_chip sun4v_virq = {
550 .typename = "vsun4v",
551 .enable = sun4v_virq_enable,
552 .disable = sun4v_virq_disable,
553 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700554 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700555};
556
David S. Millere18e2a02006-06-20 01:23:32 -0700557void irq_install_pre_handler(int virt_irq,
558 void (*func)(unsigned int, void *, void *),
559 void *arg1, void *arg2)
560{
David S. Miller68c92182007-01-29 12:12:28 -0800561 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller759f89e2007-10-11 03:16:13 -0700562 struct irq_chip *chip = get_irq_chip(virt_irq);
563
564 if (WARN_ON(chip == &sun4v_irq || chip == &sun4v_virq)) {
565 printk(KERN_ERR "IRQ: Trying to install pre-handler on "
566 "sun4v irq %u\n", virt_irq);
567 return;
568 }
David S. Millere18e2a02006-06-20 01:23:32 -0700569
570 data->pre_handler = func;
571 data->pre_handler_arg1 = arg1;
572 data->pre_handler_arg2 = arg2;
573
David S. Miller759f89e2007-10-11 03:16:13 -0700574 if (chip == &sun4u_irq_ack)
David S. Miller24ac26d2006-06-29 14:38:21 -0700575 return;
576
David S. Miller759f89e2007-10-11 03:16:13 -0700577 set_irq_chip(virt_irq, &sun4u_irq_ack);
David S. Millere18e2a02006-06-20 01:23:32 -0700578}
579
580unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700583 struct irq_handler_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int ino;
585
David S. Miller10951ee2006-02-13 18:22:57 -0800586 BUG_ON(tlb_type == hypervisor);
587
David S. Miller861fe902007-05-02 17:31:36 -0700588 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700589 bucket = &ivector_table[ino];
David S. Millere18e2a02006-06-20 01:23:32 -0700590 if (!bucket->virt_irq) {
591 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800592 set_irq_chip(bucket->virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700593 }
594
David S. Miller68c92182007-01-29 12:12:28 -0800595 data = get_irq_chip_data(bucket->virt_irq);
596 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700597 goto out;
598
599 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
600 if (unlikely(!data)) {
601 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700602 prom_halt();
603 }
David S. Miller68c92182007-01-29 12:12:28 -0800604 set_irq_chip_data(bucket->virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700605
David S. Millere18e2a02006-06-20 01:23:32 -0700606 data->imap = imap;
607 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David S. Miller088dd1f2005-07-04 13:24:38 -0700609out:
David S. Miller8047e242006-06-20 01:22:35 -0700610 return bucket->virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
David S. Miller4a907de2007-06-13 00:01:04 -0700613static unsigned int sun4v_build_common(unsigned long sysino,
614 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800615{
616 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700617 struct irq_handler_data *data;
David S. Millere18e2a02006-06-20 01:23:32 -0700618
619 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800620
David S. Millere3999572006-02-13 18:16:10 -0800621 bucket = &ivector_table[sysino];
David S. Millere18e2a02006-06-20 01:23:32 -0700622 if (!bucket->virt_irq) {
623 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller4a907de2007-06-13 00:01:04 -0700624 set_irq_chip(bucket->virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700625 }
626
David S. Miller68c92182007-01-29 12:12:28 -0800627 data = get_irq_chip_data(bucket->virt_irq);
628 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700629 goto out;
630
631 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
632 if (unlikely(!data)) {
633 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
634 prom_halt();
635 }
David S. Miller68c92182007-01-29 12:12:28 -0800636 set_irq_chip_data(bucket->virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800637
638 /* Catch accidental accesses to these things. IMAP/ICLR handling
639 * is done by hypervisor calls on sun4v platforms, not by direct
640 * register accesses.
641 */
David S. Millere18e2a02006-06-20 01:23:32 -0700642 data->imap = ~0UL;
643 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800644
David S. Millere18e2a02006-06-20 01:23:32 -0700645out:
David S. Miller8047e242006-06-20 01:22:35 -0700646 return bucket->virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800647}
648
David S. Miller4a907de2007-06-13 00:01:04 -0700649unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
650{
651 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
652
653 return sun4v_build_common(sysino, &sun4v_irq);
654}
655
656unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
657{
658 unsigned long sysino, hv_err;
David S. Miller93b32382007-07-20 02:58:28 -0700659 unsigned int virq;
David S. Miller4a907de2007-06-13 00:01:04 -0700660
David S. Miller5f7426c2007-07-18 23:16:51 -0700661 BUG_ON(devhandle & devino);
David S. Miller4a907de2007-06-13 00:01:04 -0700662
663 sysino = devhandle | devino;
David S. Miller5f7426c2007-07-18 23:16:51 -0700664 BUG_ON(sysino & ~(IMAP_IGN | IMAP_INO));
David S. Miller4a907de2007-06-13 00:01:04 -0700665
666 hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
667 if (hv_err) {
668 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
669 "err=%lu\n", devhandle, devino, hv_err);
670 prom_halt();
671 }
672
David S. Miller93b32382007-07-20 02:58:28 -0700673 virq = sun4v_build_common(sysino, &sun4v_virq);
674
675 virt_to_real_irq_table[virq].dev_handle = devhandle;
676 virt_to_real_irq_table[virq].dev_ino = devino;
677
678 return virq;
David S. Miller4a907de2007-06-13 00:01:04 -0700679}
680
David S. Millere18e2a02006-06-20 01:23:32 -0700681void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700682{
David S. Millere18e2a02006-06-20 01:23:32 -0700683 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
684 unsigned int ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700685
David S. Millere18e2a02006-06-20 01:23:32 -0700686 if (bucket)
687 ino = bucket - &ivector_table[0];
David S. Miller088dd1f2005-07-04 13:24:38 -0700688
David S. Millere18e2a02006-06-20 01:23:32 -0700689 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
690 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693void handler_irq(int irq, struct pt_regs *regs)
694{
David S. Millere18e2a02006-06-20 01:23:32 -0700695 struct ino_bucket *bucket;
Al Viro6d24c8d2006-10-08 08:23:28 -0400696 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Al Viro6d24c8d2006-10-08 08:23:28 -0400700 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* Sliiiick... */
David S. Millere18e2a02006-06-20 01:23:32 -0700704 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
705 while (bucket) {
706 struct ino_bucket *next = __bucket(bucket->irq_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
David S. Millere18e2a02006-06-20 01:23:32 -0700708 bucket->irq_chain = 0;
Al Viro6d24c8d2006-10-08 08:23:28 -0400709 __do_IRQ(bucket->virt_irq);
David S. Millerfd0504c32006-06-20 01:20:00 -0700710
David S. Millere18e2a02006-06-20 01:23:32 -0700711 bucket = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
David S. Millere18e2a02006-06-20 01:23:32 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400715 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
David S. Millere02044092007-07-16 03:49:40 -0700718#ifdef CONFIG_HOTPLUG_CPU
719void fixup_irqs(void)
720{
721 unsigned int irq;
722
723 for (irq = 0; irq < NR_IRQS; irq++) {
724 unsigned long flags;
725
726 spin_lock_irqsave(&irq_desc[irq].lock, flags);
727 if (irq_desc[irq].action &&
728 !(irq_desc[irq].status & IRQ_PER_CPU)) {
729 if (irq_desc[irq].chip->set_affinity)
730 irq_desc[irq].chip->set_affinity(irq,
731 irq_desc[irq].affinity);
732 }
733 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
734 }
735}
736#endif
737
David S. Millercdd51862005-07-24 19:36:13 -0700738struct sun5_timer {
739 u64 count0;
740 u64 limit0;
741 u64 count1;
742 u64 limit1;
743};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David S. Millercdd51862005-07-24 19:36:13 -0700745static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static u64 prom_limit0, prom_limit1;
747
748static void map_prom_timers(void)
749{
David S. Miller25c75812006-06-22 20:21:22 -0700750 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700751 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700754 dp = of_find_node_by_path("/");
755 dp = dp->child;
756 while (dp) {
757 if (!strcmp(dp->name, "counter-timer"))
758 break;
759 dp = dp->sibling;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Assume if node is not present, PROM uses different tick mechanism
763 * which we should not care about.
764 */
David S. Miller25c75812006-06-22 20:21:22 -0700765 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 prom_timers = (struct sun5_timer *) 0;
767 return;
768 }
769
770 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700771 addr = of_get_property(dp, "address", NULL);
772 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 prom_printf("PROM does not have timer mapped, trying to continue.\n");
774 prom_timers = (struct sun5_timer *) 0;
775 return;
776 }
777 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
778}
779
780static void kill_prom_timer(void)
781{
782 if (!prom_timers)
783 return;
784
785 /* Save them away for later. */
786 prom_limit0 = prom_timers->limit0;
787 prom_limit1 = prom_timers->limit1;
788
789 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
790 * We turn both off here just to be paranoid.
791 */
792 prom_timers->limit0 = 0;
793 prom_timers->limit1 = 0;
794
795 /* Wheee, eat the interrupt packet too... */
796 __asm__ __volatile__(
797" mov 0x40, %%g2\n"
798" ldxa [%%g0] %0, %%g1\n"
799" ldxa [%%g2] %1, %%g1\n"
800" stxa %%g0, [%%g0] %0\n"
801" membar #Sync\n"
802 : /* no outputs */
803 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
804 : "g1", "g2");
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807void init_irqwork_curcpu(void)
808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int cpu = hard_smp_processor_id();
810
David S. Millerfd0504c32006-06-20 01:20:00 -0700811 trap_block[cpu].irq_worklist = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
David S. Miller5cbc3072007-05-25 15:49:59 -0700814/* Please be very careful with register_one_mondo() and
815 * sun4v_register_mondo_queues().
816 *
817 * On SMP this gets invoked from the CPU trampoline before
818 * the cpu has fully taken over the trap table from OBP,
819 * and it's kernel stack + %g6 thread register state is
820 * not fully cooked yet.
821 *
822 * Therefore you cannot make any OBP calls, not even prom_printf,
823 * from these two routines.
824 */
825static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800826{
David S. Miller5cbc3072007-05-25 15:49:59 -0700827 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800828 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800829
David S. Miller94f87622006-02-16 14:26:53 -0800830 status = sun4v_cpu_qconf(type, paddr, num_entries);
831 if (status != HV_EOK) {
832 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
833 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800834 prom_halt();
835 }
836}
837
David S. Millerb434e712007-08-08 17:32:33 -0700838void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c0572006-02-08 02:53:50 -0800839{
David S. Millerb5a37e92006-02-11 23:07:13 -0800840 struct trap_per_cpu *tb = &trap_block[this_cpu];
841
David S. Miller5cbc3072007-05-25 15:49:59 -0700842 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
843 tb->cpu_mondo_qmask);
844 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
845 tb->dev_mondo_qmask);
846 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
847 tb->resum_qmask);
848 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
849 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -0800850}
851
David S. Millerb434e712007-08-08 17:32:33 -0700852static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800853{
David S. Miller5cbc3072007-05-25 15:49:59 -0700854 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700855 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5cbc3072007-05-25 15:49:59 -0700856 if (!p) {
David S. Millerb5a37e92006-02-11 23:07:13 -0800857 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
858 prom_halt();
859 }
860
David S. Miller5cbc3072007-05-25 15:49:59 -0700861 *pa_ptr = __pa(p);
David S. Millerb5a37e92006-02-11 23:07:13 -0800862}
863
David S. Millerb434e712007-08-08 17:32:33 -0700864static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800865{
David S. Miller5cbc3072007-05-25 15:49:59 -0700866 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700867 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5b0c0572006-02-08 02:53:50 -0800868
David S. Miller5cbc3072007-05-25 15:49:59 -0700869 if (!p) {
David S. Miller5b0c0572006-02-08 02:53:50 -0800870 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
871 prom_halt();
872 }
873
David S. Miller5cbc3072007-05-25 15:49:59 -0700874 *pa_ptr = __pa(p);
David S. Miller5b0c0572006-02-08 02:53:50 -0800875}
876
David S. Millerb434e712007-08-08 17:32:33 -0700877static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800878{
879#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800880 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800881
882 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
883
David S. Millerb434e712007-08-08 17:32:33 -0700884 page = alloc_bootmem_low_pages(PAGE_SIZE);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800885 if (!page) {
886 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
887 prom_halt();
888 }
889
890 tb->cpu_mondo_block_pa = __pa(page);
891 tb->cpu_list_pa = __pa(page + 64);
892#endif
893}
894
David S. Millerb434e712007-08-08 17:32:33 -0700895/* Allocate mondo and error queues for all possible cpus. */
896static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -0800897{
David S. Millerb434e712007-08-08 17:32:33 -0700898 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -0800899
David S. Millerb434e712007-08-08 17:32:33 -0700900 for_each_possible_cpu(cpu) {
901 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -0800902
David S. Millerb434e712007-08-08 17:32:33 -0700903 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
904 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
905 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
906 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
907 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
908 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
909 tb->nonresum_qmask);
910
911 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -0800912 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800913
David S. Millerb434e712007-08-08 17:32:33 -0700914 /* Load up the boot cpu's entries. */
915 sun4v_register_mondo_queues(hard_smp_processor_id());
David S. Millerac29c112006-02-08 00:08:23 -0800916}
917
David S. Millere18e2a02006-06-20 01:23:32 -0700918static struct irqaction timer_irq_action = {
919 .name = "timer",
920};
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/* Only invoked on boot processor. */
923void __init init_IRQ(void)
924{
925 map_prom_timers();
926 kill_prom_timer();
927 memset(&ivector_table[0], 0, sizeof(ivector_table));
928
David S. Millerac29c112006-02-08 00:08:23 -0800929 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -0700930 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -0800931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* We need to clear any IRQ's pending in the soft interrupt
933 * registers, a spurious one could be left around from the
934 * PROM timer which we just disabled.
935 */
936 clear_softint(get_softint());
937
938 /* Now that ivector table is initialized, it is safe
939 * to receive IRQ vector traps. We will normally take
940 * one or two right now, in case some device PROM used
941 * to boot us wants to speak to us. We just ignore them.
942 */
943 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
944 "or %%g1, %0, %%g1\n\t"
945 "wrpr %%g1, 0x0, %%pstate"
946 : /* No outputs */
947 : "i" (PSTATE_IE)
948 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -0700949
950 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}