blob: 23956096b3bf2f4044628eea93332251afe12adf [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>
David S. Miller35a17eb2007-02-10 17:41:02 -080024#include <linux/msi.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>
David S. Miller4a907de2007-06-13 00:01:04 -070045#include <asm/hypervisor.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. Miller93b32382007-07-20 02:58:28 -070090static struct {
91 unsigned int irq;
92 unsigned int dev_handle;
93 unsigned int dev_ino;
94} virt_to_real_irq_table[NR_IRQS];
David S. Miller8047e242006-06-20 01:22:35 -070095
96static unsigned char virt_irq_alloc(unsigned int real_irq)
97{
98 unsigned char ent;
99
100 BUILD_BUG_ON(NR_IRQS >= 256);
101
David S. Miller35a17eb2007-02-10 17:41:02 -0800102 for (ent = 1; ent < NR_IRQS; ent++) {
David S. Miller93b32382007-07-20 02:58:28 -0700103 if (!virt_to_real_irq_table[ent].irq)
David S. Miller35a17eb2007-02-10 17:41:02 -0800104 break;
105 }
David S. Miller8047e242006-06-20 01:22:35 -0700106 if (ent >= NR_IRQS) {
107 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
108 return 0;
109 }
110
David S. Miller93b32382007-07-20 02:58:28 -0700111 virt_to_real_irq_table[ent].irq = real_irq;
David S. Miller8047e242006-06-20 01:22:35 -0700112
113 return ent;
114}
115
David S. Miller5746c992007-02-20 01:26:48 -0800116#ifdef CONFIG_PCI_MSI
David S. Miller35a17eb2007-02-10 17:41:02 -0800117static void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700118{
David S. Miller35a17eb2007-02-10 17:41:02 -0800119 unsigned int real_irq;
David S. Miller8047e242006-06-20 01:22:35 -0700120
David S. Miller35a17eb2007-02-10 17:41:02 -0800121 if (virt_irq >= NR_IRQS)
122 return;
123
David S. Miller93b32382007-07-20 02:58:28 -0700124 real_irq = virt_to_real_irq_table[virt_irq].irq;
125 virt_to_real_irq_table[virt_irq].irq = 0;
David S. Miller35a17eb2007-02-10 17:41:02 -0800126
127 __bucket(real_irq)->virt_irq = 0;
David S. Miller8047e242006-06-20 01:22:35 -0700128}
David S. Miller5746c992007-02-20 01:26:48 -0800129#endif
David S. Miller8047e242006-06-20 01:22:35 -0700130
131static unsigned int virt_to_real_irq(unsigned char virt_irq)
132{
David S. Miller93b32382007-07-20 02:58:28 -0700133 return virt_to_real_irq_table[virt_irq].irq;
David S. Miller8047e242006-06-20 01:22:35 -0700134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
David S. Millere18e2a02006-06-20 01:23:32 -0700137 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140int show_interrupts(struct seq_file *p, void *v)
141{
David S. Millere18e2a02006-06-20 01:23:32 -0700142 int i = *(loff_t *) v, j;
143 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Millere18e2a02006-06-20 01:23:32 -0700146 if (i == 0) {
147 seq_printf(p, " ");
148 for_each_online_cpu(j)
149 seq_printf(p, "CPU%d ",j);
150 seq_putc(p, '\n');
151 }
152
153 if (i < NR_IRQS) {
154 spin_lock_irqsave(&irq_desc[i].lock, flags);
155 action = irq_desc[i].action;
156 if (!action)
157 goto skip;
158 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifndef CONFIG_SMP
160 seq_printf(p, "%10u ", kstat_irqs(i));
161#else
David S. Millere18e2a02006-06-20 01:23:32 -0700162 for_each_online_cpu(j)
163 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700165 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700166 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David S. Millere18e2a02006-06-20 01:23:32 -0700168 for (action=action->next; action; action = action->next)
169 seq_printf(p, ", %s", action->name);
170
171 seq_putc(p, '\n');
172skip:
173 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
177
David S. Millerebd8c562006-02-17 08:38:06 -0800178static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
179{
180 unsigned int tid;
181
182 if (this_is_starfire) {
183 tid = starfire_translate(imap, cpuid);
184 tid <<= IMAP_TID_SHIFT;
185 tid &= IMAP_TID_UPA;
186 } else {
187 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
188 unsigned long ver;
189
190 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
191 if ((ver >> 32UL) == __JALAPENO_ID ||
192 (ver >> 32UL) == __SERRANO_ID) {
193 tid = cpuid << IMAP_TID_SHIFT;
194 tid &= IMAP_TID_JBUS;
195 } else {
196 unsigned int a = cpuid & 0x1f;
197 unsigned int n = (cpuid >> 5) & 0x1f;
198
199 tid = ((a << IMAP_AID_SHIFT) |
200 (n << IMAP_NID_SHIFT));
201 tid &= (IMAP_AID_SAFARI |
202 IMAP_NID_SAFARI);;
203 }
204 } else {
205 tid = cpuid << IMAP_TID_SHIFT;
206 tid &= IMAP_TID_UPA;
207 }
208 }
209
210 return tid;
211}
212
David S. Millere18e2a02006-06-20 01:23:32 -0700213struct irq_handler_data {
214 unsigned long iclr;
215 unsigned long imap;
216
217 void (*pre_handler)(unsigned int, void *, void *);
218 void *pre_handler_arg1;
219 void *pre_handler_arg2;
David S. Miller5f92c322007-08-30 22:27:28 -0700220
221 u32 msi;
David S. Millere18e2a02006-06-20 01:23:32 -0700222};
223
David S. Miller5f92c322007-08-30 22:27:28 -0700224void sparc64_set_msi(unsigned int virt_irq, u32 msi)
225{
226 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
227
228 if (data)
229 data->msi = msi;
230}
231
232u32 sparc64_get_msi(unsigned int virt_irq)
233{
234 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
235
236 if (data)
237 return data->msi;
238 return 0xffffffff;
239}
240
David S. Millere18e2a02006-06-20 01:23:32 -0700241static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
David S. Miller8047e242006-06-20 01:22:35 -0700243 unsigned int real_irq = virt_to_real_irq(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700244 struct ino_bucket *bucket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David S. Millere18e2a02006-06-20 01:23:32 -0700246 if (likely(real_irq))
247 bucket = __bucket(real_irq);
David S. Miller8047e242006-06-20 01:22:35 -0700248
David S. Millere18e2a02006-06-20 01:23:32 -0700249 return bucket;
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David S. Millere18e2a02006-06-20 01:23:32 -0700252#ifdef CONFIG_SMP
253static int irq_choose_cpu(unsigned int virt_irq)
254{
Ingo Molnara53da522006-06-29 02:24:38 -0700255 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700256 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David S. Millere18e2a02006-06-20 01:23:32 -0700258 if (cpus_equal(mask, CPU_MASK_ALL)) {
259 static int irq_rover;
260 static DEFINE_SPINLOCK(irq_rover_lock);
261 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800262
David S. Millere18e2a02006-06-20 01:23:32 -0700263 /* Round-robin distribution... */
264 do_round_robin:
265 spin_lock_irqsave(&irq_rover_lock, flags);
266
267 while (!cpu_online(irq_rover)) {
268 if (++irq_rover >= NR_CPUS)
269 irq_rover = 0;
270 }
271 cpuid = irq_rover;
272 do {
273 if (++irq_rover >= NR_CPUS)
274 irq_rover = 0;
275 } while (!cpu_online(irq_rover));
276
277 spin_unlock_irqrestore(&irq_rover_lock, flags);
278 } else {
279 cpumask_t tmp;
280
281 cpus_and(tmp, cpu_online_map, mask);
282
283 if (cpus_empty(tmp))
284 goto do_round_robin;
285
286 cpuid = first_cpu(tmp);
287 }
288
289 return cpuid;
290}
291#else
292static int irq_choose_cpu(unsigned int virt_irq)
293{
294 return real_hard_smp_processor_id();
295}
296#endif
297
298static void sun4u_irq_enable(unsigned int virt_irq)
299{
David S. Miller68c92182007-01-29 12:12:28 -0800300 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700301
302 if (likely(data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700303 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700304 unsigned int tid;
305
306 cpuid = irq_choose_cpu(virt_irq);
307 imap = data->imap;
308
309 tid = sun4u_compute_tid(imap, cpuid);
310
David S. Miller861fe902007-05-02 17:31:36 -0700311 val = upa_readq(imap);
312 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
313 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
314 val |= tid | IMAP_VALID;
315 upa_writeq(val, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700316 }
317}
318
David S. Millerb53bcb62007-07-14 03:16:13 -0700319static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
320{
321 sun4u_irq_enable(virt_irq);
322}
323
David S. Millere18e2a02006-06-20 01:23:32 -0700324static void sun4u_irq_disable(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. Millere18e2a02006-06-20 01:23:32 -0700327
328 if (likely(data)) {
329 unsigned long imap = data->imap;
David S. Miller6e69d602007-08-28 14:25:32 -0700330 unsigned long tmp = upa_readq(imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700331
332 tmp &= ~IMAP_VALID;
David S. Miller861fe902007-05-02 17:31:36 -0700333 upa_writeq(tmp, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700334 }
335}
336
337static void sun4u_irq_end(unsigned int virt_irq)
338{
David S. Miller68c92182007-01-29 12:12:28 -0800339 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700340 struct irq_desc *desc = irq_desc + virt_irq;
341
342 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
343 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700344
345 if (likely(data))
David S. Miller861fe902007-05-02 17:31:36 -0700346 upa_writeq(ICLR_IDLE, data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700347}
348
349static void sun4v_irq_enable(unsigned int virt_irq)
350{
351 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
352 unsigned int ino = bucket - &ivector_table[0];
353
354 if (likely(bucket)) {
355 unsigned long cpuid;
David S. Millerc4bea282006-02-13 22:56:27 -0800356 int err;
David S. Miller10951ee2006-02-13 18:22:57 -0800357
David S. Millere18e2a02006-06-20 01:23:32 -0700358 cpuid = irq_choose_cpu(virt_irq);
359
David S. Millerebd8c562006-02-17 08:38:06 -0800360 err = sun4v_intr_settarget(ino, cpuid);
David S. Millerc4bea282006-02-13 22:56:27 -0800361 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700362 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
363 "err(%d)\n", ino, cpuid, err);
David S. Millera357b8f2007-06-26 00:13:31 -0700364 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
365 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700366 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millera357b8f2007-06-26 00:13:31 -0700367 "err(%d)\n", ino, err);
David S. Millerabd92b22006-02-14 22:20:13 -0800368 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
David S. Millerc4bea282006-02-13 22:56:27 -0800369 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700370 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
David S. Millerc4bea282006-02-13 22:56:27 -0800371 ino, err);
David S. Millerd82ace72006-02-09 02:52:44 -0800372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
David S. Millerb53bcb62007-07-14 03:16:13 -0700375static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
376{
377 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
378 unsigned int ino = bucket - &ivector_table[0];
379
380 if (likely(bucket)) {
381 unsigned long cpuid;
382 int err;
383
384 cpuid = irq_choose_cpu(virt_irq);
385
386 err = sun4v_intr_settarget(ino, cpuid);
387 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700388 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
389 "err(%d)\n", ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700390 }
391}
392
David S. Millere18e2a02006-06-20 01:23:32 -0700393static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
David S. Millere18e2a02006-06-20 01:23:32 -0700395 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
396 unsigned int ino = bucket - &ivector_table[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David S. Millere18e2a02006-06-20 01:23:32 -0700398 if (likely(bucket)) {
David S. Miller8047e242006-06-20 01:22:35 -0700399 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David S. Miller8047e242006-06-20 01:22:35 -0700401 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
402 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700403 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
David S. Miller8047e242006-06-20 01:22:35 -0700404 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406}
407
David S. Miller35a17eb2007-02-10 17:41:02 -0800408#ifdef CONFIG_PCI_MSI
409static void sun4v_msi_enable(unsigned int virt_irq)
410{
411 sun4v_irq_enable(virt_irq);
412 unmask_msi_irq(virt_irq);
413}
414
415static void sun4v_msi_disable(unsigned int virt_irq)
416{
417 mask_msi_irq(virt_irq);
418 sun4v_irq_disable(virt_irq);
419}
420#endif
421
David S. Millere18e2a02006-06-20 01:23:32 -0700422static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700423{
David S. Millere18e2a02006-06-20 01:23:32 -0700424 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
425 unsigned int ino = bucket - &ivector_table[0];
David S. Miller5a606b72007-07-09 22:40:36 -0700426 struct irq_desc *desc = irq_desc + virt_irq;
427
428 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
429 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700430
431 if (likely(bucket)) {
432 int err;
433
434 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
435 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700436 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millere18e2a02006-06-20 01:23:32 -0700437 "err(%d)\n", ino, err);
438 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700439}
440
David S. Miller4a907de2007-06-13 00:01:04 -0700441static void sun4v_virq_enable(unsigned int virt_irq)
442{
443 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700444
445 if (likely(bucket)) {
446 unsigned long cpuid, dev_handle, dev_ino;
447 int err;
448
449 cpuid = irq_choose_cpu(virt_irq);
450
David S. Miller93b32382007-07-20 02:58:28 -0700451 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
452 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700453
454 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
455 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700456 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Miller4a907de2007-06-13 00:01:04 -0700457 "err(%d)\n",
458 dev_handle, dev_ino, cpuid, err);
459 err = sun4v_vintr_set_state(dev_handle, dev_ino,
David S. Miller12450882007-06-26 00:13:09 -0700460 HV_INTR_STATE_IDLE);
461 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700462 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller12450882007-06-26 00:13:09 -0700463 "HV_INTR_STATE_IDLE): err(%d)\n",
464 dev_handle, dev_ino, err);
465 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700466 HV_INTR_ENABLED);
467 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700468 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700469 "HV_INTR_ENABLED): err(%d)\n",
470 dev_handle, dev_ino, err);
471 }
472}
473
David S. Millerb53bcb62007-07-14 03:16:13 -0700474static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
475{
476 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Millerb53bcb62007-07-14 03:16:13 -0700477
478 if (likely(bucket)) {
479 unsigned long cpuid, dev_handle, dev_ino;
480 int err;
481
482 cpuid = irq_choose_cpu(virt_irq);
483
David S. Miller93b32382007-07-20 02:58:28 -0700484 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
485 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millerb53bcb62007-07-14 03:16:13 -0700486
487 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
488 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700489 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Millerb53bcb62007-07-14 03:16:13 -0700490 "err(%d)\n",
491 dev_handle, dev_ino, cpuid, err);
492 }
493}
494
David S. Miller4a907de2007-06-13 00:01:04 -0700495static void sun4v_virq_disable(unsigned int virt_irq)
496{
497 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700498
499 if (likely(bucket)) {
500 unsigned long dev_handle, dev_ino;
501 int err;
502
David S. Miller93b32382007-07-20 02:58:28 -0700503 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
504 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700505
David S. Miller12450882007-06-26 00:13:09 -0700506 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700507 HV_INTR_DISABLED);
508 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700509 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700510 "HV_INTR_DISABLED): err(%d)\n",
511 dev_handle, dev_ino, err);
512 }
513}
514
515static void sun4v_virq_end(unsigned int virt_irq)
516{
517 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700518 struct irq_desc *desc = irq_desc + virt_irq;
519
520 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
521 return;
David S. Miller4a907de2007-06-13 00:01:04 -0700522
523 if (likely(bucket)) {
524 unsigned long dev_handle, dev_ino;
525 int err;
526
David S. Miller93b32382007-07-20 02:58:28 -0700527 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
528 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700529
530 err = sun4v_vintr_set_state(dev_handle, dev_ino,
531 HV_INTR_STATE_IDLE);
532 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700533 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700534 "HV_INTR_STATE_IDLE): err(%d)\n",
535 dev_handle, dev_ino, err);
536 }
537}
538
David S. Millere18e2a02006-06-20 01:23:32 -0700539static void run_pre_handler(unsigned int virt_irq)
540{
541 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800542 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700543
544 if (likely(data->pre_handler)) {
545 data->pre_handler(__irq_ino(__irq(bucket)),
546 data->pre_handler_arg1,
547 data->pre_handler_arg2);
548 }
549}
550
David S. Miller729e7d72006-12-12 00:59:12 -0800551static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700552 .typename = "sun4u",
553 .enable = sun4u_irq_enable,
554 .disable = sun4u_irq_disable,
555 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700556 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700557};
558
David S. Miller729e7d72006-12-12 00:59:12 -0800559static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700560 .typename = "sun4u+ack",
561 .enable = sun4u_irq_enable,
562 .disable = sun4u_irq_disable,
563 .ack = run_pre_handler,
564 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700565 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700566};
567
David S. Miller729e7d72006-12-12 00:59:12 -0800568static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700569 .typename = "sun4v",
570 .enable = sun4v_irq_enable,
571 .disable = sun4v_irq_disable,
572 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700573 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700574};
575
David S. Miller729e7d72006-12-12 00:59:12 -0800576static struct irq_chip sun4v_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700577 .typename = "sun4v+ack",
578 .enable = sun4v_irq_enable,
579 .disable = sun4v_irq_disable,
580 .ack = run_pre_handler,
581 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700582 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700583};
584
David S. Miller35a17eb2007-02-10 17:41:02 -0800585#ifdef CONFIG_PCI_MSI
586static struct irq_chip sun4v_msi = {
587 .typename = "sun4v+msi",
588 .mask = mask_msi_irq,
589 .unmask = unmask_msi_irq,
590 .enable = sun4v_msi_enable,
591 .disable = sun4v_msi_disable,
592 .ack = run_pre_handler,
593 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700594 .set_affinity = sun4v_set_affinity,
David S. Miller35a17eb2007-02-10 17:41:02 -0800595};
596#endif
597
David S. Miller4a907de2007-06-13 00:01:04 -0700598static struct irq_chip sun4v_virq = {
599 .typename = "vsun4v",
600 .enable = sun4v_virq_enable,
601 .disable = sun4v_virq_disable,
602 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700603 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700604};
605
606static struct irq_chip sun4v_virq_ack = {
607 .typename = "vsun4v+ack",
608 .enable = sun4v_virq_enable,
609 .disable = sun4v_virq_disable,
610 .ack = run_pre_handler,
611 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700612 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700613};
614
David S. Millere18e2a02006-06-20 01:23:32 -0700615void irq_install_pre_handler(int virt_irq,
616 void (*func)(unsigned int, void *, void *),
617 void *arg1, void *arg2)
618{
David S. Miller68c92182007-01-29 12:12:28 -0800619 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
620 struct irq_chip *chip;
David S. Millere18e2a02006-06-20 01:23:32 -0700621
622 data->pre_handler = func;
623 data->pre_handler_arg1 = arg1;
624 data->pre_handler_arg2 = arg2;
625
David S. Miller68c92182007-01-29 12:12:28 -0800626 chip = get_irq_chip(virt_irq);
627 if (chip == &sun4u_irq_ack ||
David S. Miller4a907de2007-06-13 00:01:04 -0700628 chip == &sun4v_irq_ack ||
629 chip == &sun4v_virq_ack
David S. Miller35a17eb2007-02-10 17:41:02 -0800630#ifdef CONFIG_PCI_MSI
631 || chip == &sun4v_msi
632#endif
633 )
David S. Miller24ac26d2006-06-29 14:38:21 -0700634 return;
635
David S. Miller68c92182007-01-29 12:12:28 -0800636 chip = (chip == &sun4u_irq ?
David S. Miller4a907de2007-06-13 00:01:04 -0700637 &sun4u_irq_ack :
638 (chip == &sun4v_irq ?
639 &sun4v_irq_ack : &sun4v_virq_ack));
David S. Miller68c92182007-01-29 12:12:28 -0800640 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700641}
642
643unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700646 struct irq_handler_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int ino;
648
David S. Miller10951ee2006-02-13 18:22:57 -0800649 BUG_ON(tlb_type == hypervisor);
650
David S. Miller861fe902007-05-02 17:31:36 -0700651 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700652 bucket = &ivector_table[ino];
David S. Millere18e2a02006-06-20 01:23:32 -0700653 if (!bucket->virt_irq) {
654 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller68c92182007-01-29 12:12:28 -0800655 set_irq_chip(bucket->virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700656 }
657
David S. Miller68c92182007-01-29 12:12:28 -0800658 data = get_irq_chip_data(bucket->virt_irq);
659 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700660 goto out;
661
662 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
663 if (unlikely(!data)) {
664 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700665 prom_halt();
666 }
David S. Miller68c92182007-01-29 12:12:28 -0800667 set_irq_chip_data(bucket->virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700668
David S. Millere18e2a02006-06-20 01:23:32 -0700669 data->imap = imap;
670 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
David S. Miller088dd1f2005-07-04 13:24:38 -0700672out:
David S. Miller8047e242006-06-20 01:22:35 -0700673 return bucket->virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
David S. Miller4a907de2007-06-13 00:01:04 -0700676static unsigned int sun4v_build_common(unsigned long sysino,
677 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800678{
679 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700680 struct irq_handler_data *data;
David S. Millere18e2a02006-06-20 01:23:32 -0700681
682 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800683
David S. Millere3999572006-02-13 18:16:10 -0800684 bucket = &ivector_table[sysino];
David S. Millere18e2a02006-06-20 01:23:32 -0700685 if (!bucket->virt_irq) {
686 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
David S. Miller4a907de2007-06-13 00:01:04 -0700687 set_irq_chip(bucket->virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700688 }
689
David S. Miller68c92182007-01-29 12:12:28 -0800690 data = get_irq_chip_data(bucket->virt_irq);
691 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700692 goto out;
693
694 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
695 if (unlikely(!data)) {
696 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
697 prom_halt();
698 }
David S. Miller68c92182007-01-29 12:12:28 -0800699 set_irq_chip_data(bucket->virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800700
701 /* Catch accidental accesses to these things. IMAP/ICLR handling
702 * is done by hypervisor calls on sun4v platforms, not by direct
703 * register accesses.
704 */
David S. Millere18e2a02006-06-20 01:23:32 -0700705 data->imap = ~0UL;
706 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800707
David S. Millere18e2a02006-06-20 01:23:32 -0700708out:
David S. Miller8047e242006-06-20 01:22:35 -0700709 return bucket->virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800710}
711
David S. Miller4a907de2007-06-13 00:01:04 -0700712unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
713{
714 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
715
716 return sun4v_build_common(sysino, &sun4v_irq);
717}
718
719unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
720{
721 unsigned long sysino, hv_err;
David S. Miller93b32382007-07-20 02:58:28 -0700722 unsigned int virq;
David S. Miller4a907de2007-06-13 00:01:04 -0700723
David S. Miller5f7426c2007-07-18 23:16:51 -0700724 BUG_ON(devhandle & devino);
David S. Miller4a907de2007-06-13 00:01:04 -0700725
726 sysino = devhandle | devino;
David S. Miller5f7426c2007-07-18 23:16:51 -0700727 BUG_ON(sysino & ~(IMAP_IGN | IMAP_INO));
David S. Miller4a907de2007-06-13 00:01:04 -0700728
729 hv_err = sun4v_vintr_set_cookie(devhandle, devino, sysino);
730 if (hv_err) {
731 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
732 "err=%lu\n", devhandle, devino, hv_err);
733 prom_halt();
734 }
735
David S. Miller93b32382007-07-20 02:58:28 -0700736 virq = sun4v_build_common(sysino, &sun4v_virq);
737
738 virt_to_real_irq_table[virq].dev_handle = devhandle;
739 virt_to_real_irq_table[virq].dev_ino = devino;
740
741 return virq;
David S. Miller4a907de2007-06-13 00:01:04 -0700742}
743
David S. Miller35a17eb2007-02-10 17:41:02 -0800744#ifdef CONFIG_PCI_MSI
745unsigned int sun4v_build_msi(u32 devhandle, unsigned int *virt_irq_p,
746 unsigned int msi_start, unsigned int msi_end)
747{
748 struct ino_bucket *bucket;
749 struct irq_handler_data *data;
750 unsigned long sysino;
751 unsigned int devino;
752
753 BUG_ON(tlb_type != hypervisor);
754
755 /* Find a free devino in the given range. */
756 for (devino = msi_start; devino < msi_end; devino++) {
757 sysino = sun4v_devino_to_sysino(devhandle, devino);
758 bucket = &ivector_table[sysino];
759 if (!bucket->virt_irq)
760 break;
761 }
762 if (devino >= msi_end)
David S. Miller5f92c322007-08-30 22:27:28 -0700763 return -ENOSPC;
David S. Miller35a17eb2007-02-10 17:41:02 -0800764
765 sysino = sun4v_devino_to_sysino(devhandle, devino);
766 bucket = &ivector_table[sysino];
767 bucket->virt_irq = virt_irq_alloc(__irq(bucket));
768 *virt_irq_p = bucket->virt_irq;
769 set_irq_chip(bucket->virt_irq, &sun4v_msi);
770
771 data = get_irq_chip_data(bucket->virt_irq);
772 if (unlikely(data))
773 return devino;
774
775 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
776 if (unlikely(!data)) {
David S. Miller5f92c322007-08-30 22:27:28 -0700777 virt_irq_free(*virt_irq_p);
778 return -ENOMEM;
David S. Miller35a17eb2007-02-10 17:41:02 -0800779 }
780 set_irq_chip_data(bucket->virt_irq, data);
781
782 data->imap = ~0UL;
783 data->iclr = ~0UL;
784
785 return devino;
786}
787
788void sun4v_destroy_msi(unsigned int virt_irq)
789{
790 virt_irq_free(virt_irq);
791}
792#endif
793
David S. Millere18e2a02006-06-20 01:23:32 -0700794void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700795{
David S. Millere18e2a02006-06-20 01:23:32 -0700796 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
797 unsigned int ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700798
David S. Millere18e2a02006-06-20 01:23:32 -0700799 if (bucket)
800 ino = bucket - &ivector_table[0];
David S. Miller088dd1f2005-07-04 13:24:38 -0700801
David S. Millere18e2a02006-06-20 01:23:32 -0700802 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
803 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806void handler_irq(int irq, struct pt_regs *regs)
807{
David S. Millere18e2a02006-06-20 01:23:32 -0700808 struct ino_bucket *bucket;
Al Viro6d24c8d2006-10-08 08:23:28 -0400809 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Al Viro6d24c8d2006-10-08 08:23:28 -0400813 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* Sliiiick... */
David S. Millere18e2a02006-06-20 01:23:32 -0700817 bucket = __bucket(xchg32(irq_work(smp_processor_id()), 0));
818 while (bucket) {
819 struct ino_bucket *next = __bucket(bucket->irq_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David S. Millere18e2a02006-06-20 01:23:32 -0700821 bucket->irq_chain = 0;
Al Viro6d24c8d2006-10-08 08:23:28 -0400822 __do_IRQ(bucket->virt_irq);
David S. Millerfd0504c32006-06-20 01:20:00 -0700823
David S. Millere18e2a02006-06-20 01:23:32 -0700824 bucket = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
David S. Millere18e2a02006-06-20 01:23:32 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400828 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
David S. Millere0204402007-07-16 03:49:40 -0700831#ifdef CONFIG_HOTPLUG_CPU
832void fixup_irqs(void)
833{
834 unsigned int irq;
835
836 for (irq = 0; irq < NR_IRQS; irq++) {
837 unsigned long flags;
838
839 spin_lock_irqsave(&irq_desc[irq].lock, flags);
840 if (irq_desc[irq].action &&
841 !(irq_desc[irq].status & IRQ_PER_CPU)) {
842 if (irq_desc[irq].chip->set_affinity)
843 irq_desc[irq].chip->set_affinity(irq,
844 irq_desc[irq].affinity);
845 }
846 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
847 }
848}
849#endif
850
David S. Millercdd51862005-07-24 19:36:13 -0700851struct sun5_timer {
852 u64 count0;
853 u64 limit0;
854 u64 count1;
855 u64 limit1;
856};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David S. Millercdd51862005-07-24 19:36:13 -0700858static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static u64 prom_limit0, prom_limit1;
860
861static void map_prom_timers(void)
862{
David S. Miller25c75812006-06-22 20:21:22 -0700863 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700864 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700867 dp = of_find_node_by_path("/");
868 dp = dp->child;
869 while (dp) {
870 if (!strcmp(dp->name, "counter-timer"))
871 break;
872 dp = dp->sibling;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* Assume if node is not present, PROM uses different tick mechanism
876 * which we should not care about.
877 */
David S. Miller25c75812006-06-22 20:21:22 -0700878 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 prom_timers = (struct sun5_timer *) 0;
880 return;
881 }
882
883 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700884 addr = of_get_property(dp, "address", NULL);
885 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 prom_printf("PROM does not have timer mapped, trying to continue.\n");
887 prom_timers = (struct sun5_timer *) 0;
888 return;
889 }
890 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
891}
892
893static void kill_prom_timer(void)
894{
895 if (!prom_timers)
896 return;
897
898 /* Save them away for later. */
899 prom_limit0 = prom_timers->limit0;
900 prom_limit1 = prom_timers->limit1;
901
902 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
903 * We turn both off here just to be paranoid.
904 */
905 prom_timers->limit0 = 0;
906 prom_timers->limit1 = 0;
907
908 /* Wheee, eat the interrupt packet too... */
909 __asm__ __volatile__(
910" mov 0x40, %%g2\n"
911" ldxa [%%g0] %0, %%g1\n"
912" ldxa [%%g2] %1, %%g1\n"
913" stxa %%g0, [%%g0] %0\n"
914" membar #Sync\n"
915 : /* no outputs */
916 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
917 : "g1", "g2");
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920void init_irqwork_curcpu(void)
921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int cpu = hard_smp_processor_id();
923
David S. Millerfd0504c32006-06-20 01:20:00 -0700924 trap_block[cpu].irq_worklist = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
David S. Miller5cbc3072007-05-25 15:49:59 -0700927/* Please be very careful with register_one_mondo() and
928 * sun4v_register_mondo_queues().
929 *
930 * On SMP this gets invoked from the CPU trampoline before
931 * the cpu has fully taken over the trap table from OBP,
932 * and it's kernel stack + %g6 thread register state is
933 * not fully cooked yet.
934 *
935 * Therefore you cannot make any OBP calls, not even prom_printf,
936 * from these two routines.
937 */
938static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800939{
David S. Miller5cbc3072007-05-25 15:49:59 -0700940 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800941 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800942
David S. Miller94f87622006-02-16 14:26:53 -0800943 status = sun4v_cpu_qconf(type, paddr, num_entries);
944 if (status != HV_EOK) {
945 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
946 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800947 prom_halt();
948 }
949}
950
David S. Millerb434e712007-08-08 17:32:33 -0700951void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c0572006-02-08 02:53:50 -0800952{
David S. Millerb5a37e92006-02-11 23:07:13 -0800953 struct trap_per_cpu *tb = &trap_block[this_cpu];
954
David S. Miller5cbc3072007-05-25 15:49:59 -0700955 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
956 tb->cpu_mondo_qmask);
957 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
958 tb->dev_mondo_qmask);
959 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
960 tb->resum_qmask);
961 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
962 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -0800963}
964
David S. Millerb434e712007-08-08 17:32:33 -0700965static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800966{
David S. Miller5cbc3072007-05-25 15:49:59 -0700967 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700968 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5cbc3072007-05-25 15:49:59 -0700969 if (!p) {
David S. Millerb5a37e92006-02-11 23:07:13 -0800970 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
971 prom_halt();
972 }
973
David S. Miller5cbc3072007-05-25 15:49:59 -0700974 *pa_ptr = __pa(p);
David S. Millerb5a37e92006-02-11 23:07:13 -0800975}
976
David S. Millerb434e712007-08-08 17:32:33 -0700977static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800978{
David S. Miller5cbc3072007-05-25 15:49:59 -0700979 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700980 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5b0c0572006-02-08 02:53:50 -0800981
David S. Miller5cbc3072007-05-25 15:49:59 -0700982 if (!p) {
David S. Miller5b0c0572006-02-08 02:53:50 -0800983 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
984 prom_halt();
985 }
986
David S. Miller5cbc3072007-05-25 15:49:59 -0700987 *pa_ptr = __pa(p);
David S. Miller5b0c0572006-02-08 02:53:50 -0800988}
989
David S. Millerb434e712007-08-08 17:32:33 -0700990static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800991{
992#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800993 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800994
995 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
996
David S. Millerb434e712007-08-08 17:32:33 -0700997 page = alloc_bootmem_low_pages(PAGE_SIZE);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800998 if (!page) {
999 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
1000 prom_halt();
1001 }
1002
1003 tb->cpu_mondo_block_pa = __pa(page);
1004 tb->cpu_list_pa = __pa(page + 64);
1005#endif
1006}
1007
David S. Millerb434e712007-08-08 17:32:33 -07001008/* Allocate mondo and error queues for all possible cpus. */
1009static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -08001010{
David S. Millerb434e712007-08-08 17:32:33 -07001011 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -08001012
David S. Millerb434e712007-08-08 17:32:33 -07001013 for_each_possible_cpu(cpu) {
1014 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -08001015
David S. Millerb434e712007-08-08 17:32:33 -07001016 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
1017 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
1018 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
1019 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
1020 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
1021 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
1022 tb->nonresum_qmask);
1023
1024 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -08001025 }
David S. Miller1d2f1f92006-02-08 16:41:20 -08001026
David S. Millerb434e712007-08-08 17:32:33 -07001027 /* Load up the boot cpu's entries. */
1028 sun4v_register_mondo_queues(hard_smp_processor_id());
David S. Millerac29c112006-02-08 00:08:23 -08001029}
1030
David S. Millere18e2a02006-06-20 01:23:32 -07001031static struct irqaction timer_irq_action = {
1032 .name = "timer",
1033};
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/* Only invoked on boot processor. */
1036void __init init_IRQ(void)
1037{
1038 map_prom_timers();
1039 kill_prom_timer();
1040 memset(&ivector_table[0], 0, sizeof(ivector_table));
1041
David S. Millerac29c112006-02-08 00:08:23 -08001042 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -07001043 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -08001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* We need to clear any IRQ's pending in the soft interrupt
1046 * registers, a spurious one could be left around from the
1047 * PROM timer which we just disabled.
1048 */
1049 clear_softint(get_softint());
1050
1051 /* Now that ivector table is initialized, it is safe
1052 * to receive IRQ vector traps. We will normally take
1053 * one or two right now, in case some device PROM used
1054 * to boot us wants to speak to us. We just ignore them.
1055 */
1056 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
1057 "or %%g1, %0, %%g1\n\t"
1058 "wrpr %%g1, 0x0, %%pstate"
1059 : /* No outputs */
1060 : "i" (PSTATE_IE)
1061 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -07001062
1063 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}