blob: f8f704524ed95ccf3411986212314adcd61883b3 [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>
David S. Miller42d5f992007-10-13 23:03:21 -070045#include <asm/cacheflush.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.
David S. Millere18e2a02006-06-20 01:23:32 -070055 *
56 * If you make changes to ino_bucket, please update hand coded assembler
57 * of the vectored interrupt trap handler(s) in entry.S and sun4v_ivec.S
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
David S. Millere18e2a02006-06-20 01:23:32 -070059struct ino_bucket {
David S. Miller42d5f992007-10-13 23:03:21 -070060/*0x00*/unsigned long __irq_chain_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David S. Millere18e2a02006-06-20 01:23:32 -070062 /* Virtual interrupt number assigned to this INO. */
David S. Miller42d5f992007-10-13 23:03:21 -070063/*0x08*/unsigned int __virt_irq;
David S. Millera650d382007-10-12 02:59:40 -070064/*0x0c*/unsigned int __pad;
David S. Millere18e2a02006-06-20 01:23:32 -070065};
66
67#define NUM_IVECS (IMAP_INR + 1)
David S. Miller10397e42007-10-13 21:43:31 -070068struct ino_bucket *ivector_table;
David S. Millereb2d8d62007-10-13 21:42:46 -070069unsigned long ivector_table_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David S. Miller42d5f992007-10-13 23:03:21 -070071/* On several sun4u processors, it is illegal to mix bypass and
72 * non-bypass accesses. Therefore we access all INO buckets
73 * using bypass accesses only.
74 */
75static unsigned long bucket_get_chain_pa(unsigned long bucket_pa)
76{
77 unsigned long ret;
78
79 __asm__ __volatile__("ldxa [%1] %2, %0"
80 : "=&r" (ret)
81 : "r" (bucket_pa +
82 offsetof(struct ino_bucket,
83 __irq_chain_pa)),
84 "i" (ASI_PHYS_USE_EC));
85
86 return ret;
87}
88
89static void bucket_clear_chain_pa(unsigned long bucket_pa)
90{
91 __asm__ __volatile__("stxa %%g0, [%0] %1"
92 : /* no outputs */
93 : "r" (bucket_pa +
94 offsetof(struct ino_bucket,
95 __irq_chain_pa)),
96 "i" (ASI_PHYS_USE_EC));
97}
98
99static unsigned int bucket_get_virt_irq(unsigned long bucket_pa)
100{
101 unsigned int ret;
102
103 __asm__ __volatile__("lduwa [%1] %2, %0"
104 : "=&r" (ret)
105 : "r" (bucket_pa +
106 offsetof(struct ino_bucket,
107 __virt_irq)),
108 "i" (ASI_PHYS_USE_EC));
109
110 return ret;
111}
112
113static void bucket_set_virt_irq(unsigned long bucket_pa,
114 unsigned int virt_irq)
115{
116 __asm__ __volatile__("stwa %0, [%1] %2"
117 : /* no outputs */
118 : "r" (virt_irq),
119 "r" (bucket_pa +
120 offsetof(struct ino_bucket,
121 __virt_irq)),
122 "i" (ASI_PHYS_USE_EC));
123}
124
David S. Millera650d382007-10-12 02:59:40 -0700125#define __bucket(irq) ((struct ino_bucket *)(irq))
126#define __irq(bucket) ((unsigned long)(bucket))
David S. Millere18e2a02006-06-20 01:23:32 -0700127
David S. Millereb2d8d62007-10-13 21:42:46 -0700128#define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David S. Miller93b32382007-07-20 02:58:28 -0700130static struct {
David S. Millera650d382007-10-12 02:59:40 -0700131 unsigned long irq;
David S. Miller93b32382007-07-20 02:58:28 -0700132 unsigned int dev_handle;
133 unsigned int dev_ino;
134} virt_to_real_irq_table[NR_IRQS];
David S. Miller759f89e2007-10-11 03:16:13 -0700135static DEFINE_SPINLOCK(virt_irq_alloc_lock);
David S. Miller8047e242006-06-20 01:22:35 -0700136
David S. Millerbb74b732007-10-13 23:27:48 -0700137unsigned char virt_irq_alloc(unsigned long real_irq,
138 unsigned int dev_handle,
139 unsigned int dev_ino)
David S. Miller8047e242006-06-20 01:22:35 -0700140{
David S. Miller759f89e2007-10-11 03:16:13 -0700141 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700142 unsigned char ent;
143
144 BUILD_BUG_ON(NR_IRQS >= 256);
145
David S. Miller759f89e2007-10-11 03:16:13 -0700146 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
147
David S. Miller35a17eb2007-02-10 17:41:02 -0800148 for (ent = 1; ent < NR_IRQS; ent++) {
David S. Miller93b32382007-07-20 02:58:28 -0700149 if (!virt_to_real_irq_table[ent].irq)
David S. Miller35a17eb2007-02-10 17:41:02 -0800150 break;
151 }
David S. Miller8047e242006-06-20 01:22:35 -0700152 if (ent >= NR_IRQS) {
153 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
David S. Miller759f89e2007-10-11 03:16:13 -0700154 ent = 0;
155 } else {
156 virt_to_real_irq_table[ent].irq = real_irq;
David S. Millerbb74b732007-10-13 23:27:48 -0700157 virt_to_real_irq_table[ent].dev_handle = dev_handle;
158 virt_to_real_irq_table[ent].dev_ino = dev_ino;
David S. Miller8047e242006-06-20 01:22:35 -0700159 }
160
David S. Miller759f89e2007-10-11 03:16:13 -0700161 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700162
163 return ent;
164}
165
David S. Miller5746c992007-02-20 01:26:48 -0800166#ifdef CONFIG_PCI_MSI
David S. Miller759f89e2007-10-11 03:16:13 -0700167void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700168{
David S. Miller759f89e2007-10-11 03:16:13 -0700169 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700170
David S. Miller35a17eb2007-02-10 17:41:02 -0800171 if (virt_irq >= NR_IRQS)
172 return;
173
David S. Miller759f89e2007-10-11 03:16:13 -0700174 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
175
David S. Miller93b32382007-07-20 02:58:28 -0700176 virt_to_real_irq_table[virt_irq].irq = 0;
David S. Miller35a17eb2007-02-10 17:41:02 -0800177
David S. Miller759f89e2007-10-11 03:16:13 -0700178 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700179}
David S. Miller5746c992007-02-20 01:26:48 -0800180#endif
David S. Miller8047e242006-06-20 01:22:35 -0700181
David S. Millera650d382007-10-12 02:59:40 -0700182static unsigned long virt_to_real_irq(unsigned char virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700183{
David S. Miller93b32382007-07-20 02:58:28 -0700184 return virt_to_real_irq_table[virt_irq].irq;
David S. Miller8047e242006-06-20 01:22:35 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
David S. Millere18e2a02006-06-20 01:23:32 -0700188 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191int show_interrupts(struct seq_file *p, void *v)
192{
David S. Millere18e2a02006-06-20 01:23:32 -0700193 int i = *(loff_t *) v, j;
194 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
David S. Millere18e2a02006-06-20 01:23:32 -0700197 if (i == 0) {
198 seq_printf(p, " ");
199 for_each_online_cpu(j)
200 seq_printf(p, "CPU%d ",j);
201 seq_putc(p, '\n');
202 }
203
204 if (i < NR_IRQS) {
205 spin_lock_irqsave(&irq_desc[i].lock, flags);
206 action = irq_desc[i].action;
207 if (!action)
208 goto skip;
209 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifndef CONFIG_SMP
211 seq_printf(p, "%10u ", kstat_irqs(i));
212#else
David S. Millere18e2a02006-06-20 01:23:32 -0700213 for_each_online_cpu(j)
214 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700216 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700217 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David S. Millere18e2a02006-06-20 01:23:32 -0700219 for (action=action->next; action; action = action->next)
220 seq_printf(p, ", %s", action->name);
221
222 seq_putc(p, '\n');
223skip:
224 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227}
228
David S. Millerebd8c562006-02-17 08:38:06 -0800229static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
230{
231 unsigned int tid;
232
233 if (this_is_starfire) {
234 tid = starfire_translate(imap, cpuid);
235 tid <<= IMAP_TID_SHIFT;
236 tid &= IMAP_TID_UPA;
237 } else {
238 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
239 unsigned long ver;
240
241 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
242 if ((ver >> 32UL) == __JALAPENO_ID ||
243 (ver >> 32UL) == __SERRANO_ID) {
244 tid = cpuid << IMAP_TID_SHIFT;
245 tid &= IMAP_TID_JBUS;
246 } else {
247 unsigned int a = cpuid & 0x1f;
248 unsigned int n = (cpuid >> 5) & 0x1f;
249
250 tid = ((a << IMAP_AID_SHIFT) |
251 (n << IMAP_NID_SHIFT));
252 tid &= (IMAP_AID_SAFARI |
253 IMAP_NID_SAFARI);;
254 }
255 } else {
256 tid = cpuid << IMAP_TID_SHIFT;
257 tid &= IMAP_TID_UPA;
258 }
259 }
260
261 return tid;
262}
263
David S. Millere18e2a02006-06-20 01:23:32 -0700264struct irq_handler_data {
265 unsigned long iclr;
266 unsigned long imap;
267
268 void (*pre_handler)(unsigned int, void *, void *);
269 void *pre_handler_arg1;
270 void *pre_handler_arg2;
271};
272
273static inline struct ino_bucket *virt_irq_to_bucket(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
David S. Millera650d382007-10-12 02:59:40 -0700275 unsigned long real_irq = virt_to_real_irq(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700276 struct ino_bucket *bucket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David S. Millere18e2a02006-06-20 01:23:32 -0700278 if (likely(real_irq))
279 bucket = __bucket(real_irq);
David S. Miller8047e242006-06-20 01:22:35 -0700280
David S. Millere18e2a02006-06-20 01:23:32 -0700281 return bucket;
282}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
David S. Millere18e2a02006-06-20 01:23:32 -0700284#ifdef CONFIG_SMP
285static int irq_choose_cpu(unsigned int virt_irq)
286{
Ingo Molnara53da522006-06-29 02:24:38 -0700287 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700288 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David S. Millere18e2a02006-06-20 01:23:32 -0700290 if (cpus_equal(mask, CPU_MASK_ALL)) {
291 static int irq_rover;
292 static DEFINE_SPINLOCK(irq_rover_lock);
293 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800294
David S. Millere18e2a02006-06-20 01:23:32 -0700295 /* Round-robin distribution... */
296 do_round_robin:
297 spin_lock_irqsave(&irq_rover_lock, flags);
298
299 while (!cpu_online(irq_rover)) {
300 if (++irq_rover >= NR_CPUS)
301 irq_rover = 0;
302 }
303 cpuid = irq_rover;
304 do {
305 if (++irq_rover >= NR_CPUS)
306 irq_rover = 0;
307 } while (!cpu_online(irq_rover));
308
309 spin_unlock_irqrestore(&irq_rover_lock, flags);
310 } else {
311 cpumask_t tmp;
312
313 cpus_and(tmp, cpu_online_map, mask);
314
315 if (cpus_empty(tmp))
316 goto do_round_robin;
317
318 cpuid = first_cpu(tmp);
319 }
320
321 return cpuid;
322}
323#else
324static int irq_choose_cpu(unsigned int virt_irq)
325{
326 return real_hard_smp_processor_id();
327}
328#endif
329
330static void sun4u_irq_enable(unsigned int virt_irq)
331{
David S. Miller68c92182007-01-29 12:12:28 -0800332 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700333
334 if (likely(data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700335 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700336 unsigned int tid;
337
338 cpuid = irq_choose_cpu(virt_irq);
339 imap = data->imap;
340
341 tid = sun4u_compute_tid(imap, cpuid);
342
David S. Miller861fe902007-05-02 17:31:36 -0700343 val = upa_readq(imap);
344 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
345 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
346 val |= tid | IMAP_VALID;
347 upa_writeq(val, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700348 }
349}
350
David S. Millerb53bcb62007-07-14 03:16:13 -0700351static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
352{
353 sun4u_irq_enable(virt_irq);
354}
355
David S. Millere18e2a02006-06-20 01:23:32 -0700356static void sun4u_irq_disable(unsigned int virt_irq)
357{
David S. Miller68c92182007-01-29 12:12:28 -0800358 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700359
360 if (likely(data)) {
361 unsigned long imap = data->imap;
David S. Miller6e69d602007-08-28 14:25:32 -0700362 unsigned long tmp = upa_readq(imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700363
364 tmp &= ~IMAP_VALID;
David S. Miller861fe902007-05-02 17:31:36 -0700365 upa_writeq(tmp, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700366 }
367}
368
369static void sun4u_irq_end(unsigned int virt_irq)
370{
David S. Miller68c92182007-01-29 12:12:28 -0800371 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700372 struct irq_desc *desc = irq_desc + virt_irq;
373
374 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
375 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700376
377 if (likely(data))
David S. Miller861fe902007-05-02 17:31:36 -0700378 upa_writeq(ICLR_IDLE, data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700379}
380
381static void sun4v_irq_enable(unsigned int virt_irq)
382{
383 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
384 unsigned int ino = bucket - &ivector_table[0];
385
386 if (likely(bucket)) {
387 unsigned long cpuid;
David S. Millerc4bea282006-02-13 22:56:27 -0800388 int err;
David S. Miller10951ee2006-02-13 18:22:57 -0800389
David S. Millere18e2a02006-06-20 01:23:32 -0700390 cpuid = irq_choose_cpu(virt_irq);
391
David S. Millerebd8c562006-02-17 08:38:06 -0800392 err = sun4v_intr_settarget(ino, cpuid);
David S. Millerc4bea282006-02-13 22:56:27 -0800393 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700394 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
395 "err(%d)\n", ino, cpuid, err);
David S. Millera357b8f2007-06-26 00:13:31 -0700396 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
397 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700398 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millera357b8f2007-06-26 00:13:31 -0700399 "err(%d)\n", ino, err);
David S. Millerabd92b22006-02-14 22:20:13 -0800400 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
David S. Millerc4bea282006-02-13 22:56:27 -0800401 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700402 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
David S. Millerc4bea282006-02-13 22:56:27 -0800403 ino, err);
David S. Millerd82ace72006-02-09 02:52:44 -0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
David S. Millerb53bcb62007-07-14 03:16:13 -0700407static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
408{
409 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
410 unsigned int ino = bucket - &ivector_table[0];
411
412 if (likely(bucket)) {
413 unsigned long cpuid;
414 int err;
415
416 cpuid = irq_choose_cpu(virt_irq);
417
418 err = sun4v_intr_settarget(ino, cpuid);
419 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700420 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
421 "err(%d)\n", ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700422 }
423}
424
David S. Millere18e2a02006-06-20 01:23:32 -0700425static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
David S. Millere18e2a02006-06-20 01:23:32 -0700427 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
428 unsigned int ino = bucket - &ivector_table[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
David S. Millere18e2a02006-06-20 01:23:32 -0700430 if (likely(bucket)) {
David S. Miller8047e242006-06-20 01:22:35 -0700431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
David S. Miller8047e242006-06-20 01:22:35 -0700433 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
434 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700435 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
David S. Miller8047e242006-06-20 01:22:35 -0700436 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438}
439
David S. Millere18e2a02006-06-20 01:23:32 -0700440static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700441{
David S. Millere18e2a02006-06-20 01:23:32 -0700442 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
443 unsigned int ino = bucket - &ivector_table[0];
David S. Miller5a606b72007-07-09 22:40:36 -0700444 struct irq_desc *desc = irq_desc + virt_irq;
445
446 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
447 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700448
449 if (likely(bucket)) {
450 int err;
451
452 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
453 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700454 printk(KERN_ERR "sun4v_intr_setstate(%x): "
David S. Millere18e2a02006-06-20 01:23:32 -0700455 "err(%d)\n", ino, err);
456 }
David S. Miller088dd1f2005-07-04 13:24:38 -0700457}
458
David S. Miller4a907de2007-06-13 00:01:04 -0700459static void sun4v_virq_enable(unsigned int virt_irq)
460{
461 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700462
463 if (likely(bucket)) {
464 unsigned long cpuid, dev_handle, dev_ino;
465 int err;
466
467 cpuid = irq_choose_cpu(virt_irq);
468
David S. Miller93b32382007-07-20 02:58:28 -0700469 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
470 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700471
472 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
473 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700474 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Miller4a907de2007-06-13 00:01:04 -0700475 "err(%d)\n",
476 dev_handle, dev_ino, cpuid, err);
477 err = sun4v_vintr_set_state(dev_handle, dev_ino,
David S. Miller12450882007-06-26 00:13:09 -0700478 HV_INTR_STATE_IDLE);
479 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700480 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller12450882007-06-26 00:13:09 -0700481 "HV_INTR_STATE_IDLE): err(%d)\n",
482 dev_handle, dev_ino, err);
483 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700484 HV_INTR_ENABLED);
485 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700486 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700487 "HV_INTR_ENABLED): err(%d)\n",
488 dev_handle, dev_ino, err);
489 }
490}
491
David S. Millerb53bcb62007-07-14 03:16:13 -0700492static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
493{
494 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Millerb53bcb62007-07-14 03:16:13 -0700495
496 if (likely(bucket)) {
497 unsigned long cpuid, dev_handle, dev_ino;
498 int err;
499
500 cpuid = irq_choose_cpu(virt_irq);
501
David S. Miller93b32382007-07-20 02:58:28 -0700502 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
503 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millerb53bcb62007-07-14 03:16:13 -0700504
505 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
506 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700507 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
David S. Millerb53bcb62007-07-14 03:16:13 -0700508 "err(%d)\n",
509 dev_handle, dev_ino, cpuid, err);
510 }
511}
512
David S. Miller4a907de2007-06-13 00:01:04 -0700513static void sun4v_virq_disable(unsigned int virt_irq)
514{
515 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700516
517 if (likely(bucket)) {
518 unsigned long dev_handle, dev_ino;
519 int err;
520
David S. Miller93b32382007-07-20 02:58:28 -0700521 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
522 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700523
David S. Miller12450882007-06-26 00:13:09 -0700524 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
David S. Miller4a907de2007-06-13 00:01:04 -0700525 HV_INTR_DISABLED);
526 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700527 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700528 "HV_INTR_DISABLED): err(%d)\n",
529 dev_handle, dev_ino, err);
530 }
531}
532
533static void sun4v_virq_end(unsigned int virt_irq)
534{
535 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700536 struct irq_desc *desc = irq_desc + virt_irq;
537
538 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
539 return;
David S. Miller4a907de2007-06-13 00:01:04 -0700540
541 if (likely(bucket)) {
542 unsigned long dev_handle, dev_ino;
543 int err;
544
David S. Miller93b32382007-07-20 02:58:28 -0700545 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
546 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700547
548 err = sun4v_vintr_set_state(dev_handle, dev_ino,
549 HV_INTR_STATE_IDLE);
550 if (err != HV_EOK)
David S. Millere83fb172007-07-20 02:39:04 -0700551 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
David S. Miller4a907de2007-06-13 00:01:04 -0700552 "HV_INTR_STATE_IDLE): err(%d)\n",
553 dev_handle, dev_ino, err);
554 }
555}
556
David S. Millere18e2a02006-06-20 01:23:32 -0700557static void run_pre_handler(unsigned int virt_irq)
558{
David S. Miller68c92182007-01-29 12:12:28 -0800559 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millerbb74b732007-10-13 23:27:48 -0700560 unsigned int ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700561
David S. Millerbb74b732007-10-13 23:27:48 -0700562 ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700563 if (likely(data->pre_handler)) {
David S. Millerbb74b732007-10-13 23:27:48 -0700564 data->pre_handler(ino,
David S. Millere18e2a02006-06-20 01:23:32 -0700565 data->pre_handler_arg1,
566 data->pre_handler_arg2);
567 }
568}
569
David S. Miller729e7d72006-12-12 00:59:12 -0800570static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700571 .typename = "sun4u",
572 .enable = sun4u_irq_enable,
573 .disable = sun4u_irq_disable,
574 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700575 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700576};
577
David S. Miller729e7d72006-12-12 00:59:12 -0800578static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700579 .typename = "sun4u+ack",
580 .enable = sun4u_irq_enable,
581 .disable = sun4u_irq_disable,
582 .ack = run_pre_handler,
583 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700584 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700585};
586
David S. Miller729e7d72006-12-12 00:59:12 -0800587static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700588 .typename = "sun4v",
589 .enable = sun4v_irq_enable,
590 .disable = sun4v_irq_disable,
591 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700592 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700593};
594
David S. Miller4a907de2007-06-13 00:01:04 -0700595static struct irq_chip sun4v_virq = {
596 .typename = "vsun4v",
597 .enable = sun4v_virq_enable,
598 .disable = sun4v_virq_disable,
599 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700600 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700601};
602
David S. Millere18e2a02006-06-20 01:23:32 -0700603void irq_install_pre_handler(int virt_irq,
604 void (*func)(unsigned int, void *, void *),
605 void *arg1, void *arg2)
606{
David S. Miller68c92182007-01-29 12:12:28 -0800607 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller759f89e2007-10-11 03:16:13 -0700608 struct irq_chip *chip = get_irq_chip(virt_irq);
609
610 if (WARN_ON(chip == &sun4v_irq || chip == &sun4v_virq)) {
611 printk(KERN_ERR "IRQ: Trying to install pre-handler on "
612 "sun4v irq %u\n", virt_irq);
613 return;
614 }
David S. Millere18e2a02006-06-20 01:23:32 -0700615
616 data->pre_handler = func;
617 data->pre_handler_arg1 = arg1;
618 data->pre_handler_arg2 = arg2;
619
David S. Miller759f89e2007-10-11 03:16:13 -0700620 if (chip == &sun4u_irq_ack)
David S. Miller24ac26d2006-06-29 14:38:21 -0700621 return;
622
David S. Miller759f89e2007-10-11 03:16:13 -0700623 set_irq_chip(virt_irq, &sun4u_irq_ack);
David S. Millere18e2a02006-06-20 01:23:32 -0700624}
625
626unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700629 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700630 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int ino;
632
David S. Miller10951ee2006-02-13 18:22:57 -0800633 BUG_ON(tlb_type == hypervisor);
634
David S. Miller861fe902007-05-02 17:31:36 -0700635 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700636 bucket = &ivector_table[ino];
David S. Miller42d5f992007-10-13 23:03:21 -0700637 virt_irq = bucket_get_virt_irq(__pa(bucket));
638 if (!virt_irq) {
David S. Millerbb74b732007-10-13 23:27:48 -0700639 virt_irq = virt_irq_alloc(__irq(bucket), 0, ino);
David S. Miller42d5f992007-10-13 23:03:21 -0700640 bucket_set_virt_irq(__pa(bucket), virt_irq);
641 set_irq_chip(virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700642 }
643
David S. Miller42d5f992007-10-13 23:03:21 -0700644 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800645 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700646 goto out;
647
648 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
649 if (unlikely(!data)) {
650 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700651 prom_halt();
652 }
David S. Miller42d5f992007-10-13 23:03:21 -0700653 set_irq_chip_data(virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700654
David S. Millere18e2a02006-06-20 01:23:32 -0700655 data->imap = imap;
656 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
David S. Miller088dd1f2005-07-04 13:24:38 -0700658out:
David S. Miller42d5f992007-10-13 23:03:21 -0700659 return virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
David S. Miller4a907de2007-06-13 00:01:04 -0700662static unsigned int sun4v_build_common(unsigned long sysino,
663 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800664{
665 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700666 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700667 unsigned int virt_irq;
David S. Millere18e2a02006-06-20 01:23:32 -0700668
669 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800670
David S. Millere3999572006-02-13 18:16:10 -0800671 bucket = &ivector_table[sysino];
David S. Miller42d5f992007-10-13 23:03:21 -0700672 virt_irq = bucket_get_virt_irq(__pa(bucket));
673 if (!virt_irq) {
David S. Millerbb74b732007-10-13 23:27:48 -0700674 virt_irq = virt_irq_alloc(__irq(bucket), 0, sysino);
David S. Miller42d5f992007-10-13 23:03:21 -0700675 bucket_set_virt_irq(__pa(bucket), virt_irq);
676 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700677 }
678
David S. Miller42d5f992007-10-13 23:03:21 -0700679 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800680 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700681 goto out;
682
683 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
684 if (unlikely(!data)) {
685 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
686 prom_halt();
687 }
David S. Miller42d5f992007-10-13 23:03:21 -0700688 set_irq_chip_data(virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800689
690 /* Catch accidental accesses to these things. IMAP/ICLR handling
691 * is done by hypervisor calls on sun4v platforms, not by direct
692 * register accesses.
693 */
David S. Millere18e2a02006-06-20 01:23:32 -0700694 data->imap = ~0UL;
695 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800696
David S. Millere18e2a02006-06-20 01:23:32 -0700697out:
David S. Miller42d5f992007-10-13 23:03:21 -0700698 return virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800699}
700
David S. Miller4a907de2007-06-13 00:01:04 -0700701unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
702{
703 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
704
705 return sun4v_build_common(sysino, &sun4v_irq);
706}
707
708unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
709{
David S. Millerb80e6992007-10-13 21:51:37 -0700710 struct irq_handler_data *data;
711 struct ino_bucket *bucket;
712 unsigned long hv_err, cookie;
David S. Miller42d5f992007-10-13 23:03:21 -0700713 unsigned int virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700714
David S. Millerb80e6992007-10-13 21:51:37 -0700715 bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
716 if (unlikely(!bucket))
717 return 0;
David S. Miller42d5f992007-10-13 23:03:21 -0700718 __flush_dcache_range((unsigned long) bucket,
719 ((unsigned long) bucket +
720 sizeof(struct ino_bucket)));
David S. Miller4a907de2007-06-13 00:01:04 -0700721
David S. Millerbb74b732007-10-13 23:27:48 -0700722 virt_irq = virt_irq_alloc(__irq(bucket), devhandle, devino);
David S. Miller42d5f992007-10-13 23:03:21 -0700723 bucket_set_virt_irq(__pa(bucket), virt_irq);
724 set_irq_chip(virt_irq, &sun4v_virq);
David S. Miller4a907de2007-06-13 00:01:04 -0700725
David S. Millerb80e6992007-10-13 21:51:37 -0700726 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
727 if (unlikely(!data))
728 return 0;
729
David S. Miller42d5f992007-10-13 23:03:21 -0700730 set_irq_chip_data(virt_irq, data);
David S. Millerb80e6992007-10-13 21:51:37 -0700731
732 /* Catch accidental accesses to these things. IMAP/ICLR handling
733 * is done by hypervisor calls on sun4v platforms, not by direct
734 * register accesses.
735 */
736 data->imap = ~0UL;
737 data->iclr = ~0UL;
738
739 cookie = ~__pa(bucket);
740 hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
David S. Miller4a907de2007-06-13 00:01:04 -0700741 if (hv_err) {
742 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
743 "err=%lu\n", devhandle, devino, hv_err);
744 prom_halt();
745 }
746
David S. Miller42d5f992007-10-13 23:03:21 -0700747 return virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700748}
749
David S. Millere18e2a02006-06-20 01:23:32 -0700750void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700751{
David S. Millere18e2a02006-06-20 01:23:32 -0700752 struct ino_bucket *bucket = virt_irq_to_bucket(virt_irq);
753 unsigned int ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700754
David S. Millere18e2a02006-06-20 01:23:32 -0700755 if (bucket)
756 ino = bucket - &ivector_table[0];
David S. Miller088dd1f2005-07-04 13:24:38 -0700757
David S. Millere18e2a02006-06-20 01:23:32 -0700758 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
759 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762void handler_irq(int irq, struct pt_regs *regs)
763{
David S. Millereb2d8d62007-10-13 21:42:46 -0700764 unsigned long pstate, bucket_pa;
Al Viro6d24c8d2006-10-08 08:23:28 -0400765 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Al Viro6d24c8d2006-10-08 08:23:28 -0400769 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
David S. Millera650d382007-10-12 02:59:40 -0700772 /* Grab an atomic snapshot of the pending IVECs. */
773 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
774 "wrpr %0, %3, %%pstate\n\t"
775 "ldx [%2], %1\n\t"
776 "stx %%g0, [%2]\n\t"
777 "wrpr %0, 0x0, %%pstate\n\t"
David S. Millereb2d8d62007-10-13 21:42:46 -0700778 : "=&r" (pstate), "=&r" (bucket_pa)
779 : "r" (irq_work_pa(smp_processor_id())),
David S. Millera650d382007-10-12 02:59:40 -0700780 "i" (PSTATE_IE)
781 : "memory");
782
David S. Millereb2d8d62007-10-13 21:42:46 -0700783 while (bucket_pa) {
784 unsigned long next_pa;
785 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
David S. Miller42d5f992007-10-13 23:03:21 -0700787 next_pa = bucket_get_chain_pa(bucket_pa);
788 virt_irq = bucket_get_virt_irq(bucket_pa);
789 bucket_clear_chain_pa(bucket_pa);
David S. Millerfd0504c32006-06-20 01:20:00 -0700790
David S. Millereb2d8d62007-10-13 21:42:46 -0700791 __do_IRQ(virt_irq);
792
793 bucket_pa = next_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
David S. Millere18e2a02006-06-20 01:23:32 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400797 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
David S. Millere0204402007-07-16 03:49:40 -0700800#ifdef CONFIG_HOTPLUG_CPU
801void fixup_irqs(void)
802{
803 unsigned int irq;
804
805 for (irq = 0; irq < NR_IRQS; irq++) {
806 unsigned long flags;
807
808 spin_lock_irqsave(&irq_desc[irq].lock, flags);
809 if (irq_desc[irq].action &&
810 !(irq_desc[irq].status & IRQ_PER_CPU)) {
811 if (irq_desc[irq].chip->set_affinity)
812 irq_desc[irq].chip->set_affinity(irq,
813 irq_desc[irq].affinity);
814 }
815 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
816 }
817}
818#endif
819
David S. Millercdd51862005-07-24 19:36:13 -0700820struct sun5_timer {
821 u64 count0;
822 u64 limit0;
823 u64 count1;
824 u64 limit1;
825};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
David S. Millercdd51862005-07-24 19:36:13 -0700827static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static u64 prom_limit0, prom_limit1;
829
830static void map_prom_timers(void)
831{
David S. Miller25c75812006-06-22 20:21:22 -0700832 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700833 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700836 dp = of_find_node_by_path("/");
837 dp = dp->child;
838 while (dp) {
839 if (!strcmp(dp->name, "counter-timer"))
840 break;
841 dp = dp->sibling;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Assume if node is not present, PROM uses different tick mechanism
845 * which we should not care about.
846 */
David S. Miller25c75812006-06-22 20:21:22 -0700847 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 prom_timers = (struct sun5_timer *) 0;
849 return;
850 }
851
852 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700853 addr = of_get_property(dp, "address", NULL);
854 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 prom_printf("PROM does not have timer mapped, trying to continue.\n");
856 prom_timers = (struct sun5_timer *) 0;
857 return;
858 }
859 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
860}
861
862static void kill_prom_timer(void)
863{
864 if (!prom_timers)
865 return;
866
867 /* Save them away for later. */
868 prom_limit0 = prom_timers->limit0;
869 prom_limit1 = prom_timers->limit1;
870
871 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
872 * We turn both off here just to be paranoid.
873 */
874 prom_timers->limit0 = 0;
875 prom_timers->limit1 = 0;
876
877 /* Wheee, eat the interrupt packet too... */
878 __asm__ __volatile__(
879" mov 0x40, %%g2\n"
880" ldxa [%%g0] %0, %%g1\n"
881" ldxa [%%g2] %1, %%g1\n"
882" stxa %%g0, [%%g0] %0\n"
883" membar #Sync\n"
884 : /* no outputs */
885 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
886 : "g1", "g2");
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889void init_irqwork_curcpu(void)
890{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int cpu = hard_smp_processor_id();
892
David S. Millereb2d8d62007-10-13 21:42:46 -0700893 trap_block[cpu].irq_worklist_pa = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
David S. Miller5cbc3072007-05-25 15:49:59 -0700896/* Please be very careful with register_one_mondo() and
897 * sun4v_register_mondo_queues().
898 *
899 * On SMP this gets invoked from the CPU trampoline before
900 * the cpu has fully taken over the trap table from OBP,
901 * and it's kernel stack + %g6 thread register state is
902 * not fully cooked yet.
903 *
904 * Therefore you cannot make any OBP calls, not even prom_printf,
905 * from these two routines.
906 */
907static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800908{
David S. Miller5cbc3072007-05-25 15:49:59 -0700909 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800910 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800911
David S. Miller94f87622006-02-16 14:26:53 -0800912 status = sun4v_cpu_qconf(type, paddr, num_entries);
913 if (status != HV_EOK) {
914 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
915 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800916 prom_halt();
917 }
918}
919
David S. Millerb434e712007-08-08 17:32:33 -0700920void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c0572006-02-08 02:53:50 -0800921{
David S. Millerb5a37e92006-02-11 23:07:13 -0800922 struct trap_per_cpu *tb = &trap_block[this_cpu];
923
David S. Miller5cbc3072007-05-25 15:49:59 -0700924 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
925 tb->cpu_mondo_qmask);
926 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
927 tb->dev_mondo_qmask);
928 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
929 tb->resum_qmask);
930 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
931 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -0800932}
933
David S. Millerb434e712007-08-08 17:32:33 -0700934static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800935{
David S. Miller5cbc3072007-05-25 15:49:59 -0700936 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700937 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5cbc3072007-05-25 15:49:59 -0700938 if (!p) {
David S. Millerb5a37e92006-02-11 23:07:13 -0800939 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
940 prom_halt();
941 }
942
David S. Miller5cbc3072007-05-25 15:49:59 -0700943 *pa_ptr = __pa(p);
David S. Millerb5a37e92006-02-11 23:07:13 -0800944}
945
David S. Millerb434e712007-08-08 17:32:33 -0700946static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800947{
David S. Miller5cbc3072007-05-25 15:49:59 -0700948 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700949 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5b0c0572006-02-08 02:53:50 -0800950
David S. Miller5cbc3072007-05-25 15:49:59 -0700951 if (!p) {
David S. Miller5b0c0572006-02-08 02:53:50 -0800952 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
953 prom_halt();
954 }
955
David S. Miller5cbc3072007-05-25 15:49:59 -0700956 *pa_ptr = __pa(p);
David S. Miller5b0c0572006-02-08 02:53:50 -0800957}
958
David S. Millerb434e712007-08-08 17:32:33 -0700959static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800960{
961#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800962 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800963
964 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
965
David S. Millerb434e712007-08-08 17:32:33 -0700966 page = alloc_bootmem_low_pages(PAGE_SIZE);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800967 if (!page) {
968 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
969 prom_halt();
970 }
971
972 tb->cpu_mondo_block_pa = __pa(page);
973 tb->cpu_list_pa = __pa(page + 64);
974#endif
975}
976
David S. Millerb434e712007-08-08 17:32:33 -0700977/* Allocate mondo and error queues for all possible cpus. */
978static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -0800979{
David S. Millerb434e712007-08-08 17:32:33 -0700980 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -0800981
David S. Millerb434e712007-08-08 17:32:33 -0700982 for_each_possible_cpu(cpu) {
983 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -0800984
David S. Millerb434e712007-08-08 17:32:33 -0700985 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
986 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
987 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
988 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
989 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
990 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
991 tb->nonresum_qmask);
992
993 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -0800994 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800995
David S. Millerb434e712007-08-08 17:32:33 -0700996 /* Load up the boot cpu's entries. */
997 sun4v_register_mondo_queues(hard_smp_processor_id());
David S. Millerac29c112006-02-08 00:08:23 -0800998}
999
David S. Millere18e2a02006-06-20 01:23:32 -07001000static struct irqaction timer_irq_action = {
1001 .name = "timer",
1002};
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/* Only invoked on boot processor. */
1005void __init init_IRQ(void)
1006{
David S. Miller10397e42007-10-13 21:43:31 -07001007 unsigned long size;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 map_prom_timers();
1010 kill_prom_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
David S. Miller10397e42007-10-13 21:43:31 -07001012 size = sizeof(struct ino_bucket) * NUM_IVECS;
1013 ivector_table = alloc_bootmem_low(size);
1014 if (!ivector_table) {
1015 prom_printf("Fatal error, cannot allocate ivector_table\n");
1016 prom_halt();
1017 }
David S. Miller42d5f992007-10-13 23:03:21 -07001018 __flush_dcache_range((unsigned long) ivector_table,
1019 ((unsigned long) ivector_table) + size);
David S. Miller10397e42007-10-13 21:43:31 -07001020
1021 ivector_table_pa = __pa(ivector_table);
David S. Millereb2d8d62007-10-13 21:42:46 -07001022
David S. Millerac29c112006-02-08 00:08:23 -08001023 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -07001024 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -08001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* We need to clear any IRQ's pending in the soft interrupt
1027 * registers, a spurious one could be left around from the
1028 * PROM timer which we just disabled.
1029 */
1030 clear_softint(get_softint());
1031
1032 /* Now that ivector table is initialized, it is safe
1033 * to receive IRQ vector traps. We will normally take
1034 * one or two right now, in case some device PROM used
1035 * to boot us wants to speak to us. We just ignore them.
1036 */
1037 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
1038 "or %%g1, %0, %%g1\n\t"
1039 "wrpr %%g1, 0x0, %%pstate"
1040 : /* No outputs */
1041 : "i" (PSTATE_IE)
1042 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -07001043
1044 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}