blob: 695b9e40cd802cb1bd016cb0fdb6ff04e025d7ab [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. Millereb2d8d62007-10-13 21:42:46 -0700125#define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David S. Miller93b32382007-07-20 02:58:28 -0700127static struct {
David S. Miller93b32382007-07-20 02:58:28 -0700128 unsigned int dev_handle;
129 unsigned int dev_ino;
David S. Miller256c1df2007-10-13 23:50:38 -0700130 unsigned int in_use;
David S. Miller93b32382007-07-20 02:58:28 -0700131} virt_to_real_irq_table[NR_IRQS];
David S. Miller759f89e2007-10-11 03:16:13 -0700132static DEFINE_SPINLOCK(virt_irq_alloc_lock);
David S. Miller8047e242006-06-20 01:22:35 -0700133
David S. Miller256c1df2007-10-13 23:50:38 -0700134unsigned char virt_irq_alloc(unsigned int dev_handle,
David S. Millerbb74b732007-10-13 23:27:48 -0700135 unsigned int dev_ino)
David S. Miller8047e242006-06-20 01:22:35 -0700136{
David S. Miller759f89e2007-10-11 03:16:13 -0700137 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700138 unsigned char ent;
139
140 BUILD_BUG_ON(NR_IRQS >= 256);
141
David S. Miller759f89e2007-10-11 03:16:13 -0700142 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
143
David S. Miller35a17eb2007-02-10 17:41:02 -0800144 for (ent = 1; ent < NR_IRQS; ent++) {
David S. Miller256c1df2007-10-13 23:50:38 -0700145 if (!virt_to_real_irq_table[ent].in_use)
David S. Miller35a17eb2007-02-10 17:41:02 -0800146 break;
147 }
David S. Miller8047e242006-06-20 01:22:35 -0700148 if (ent >= NR_IRQS) {
149 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
David S. Miller759f89e2007-10-11 03:16:13 -0700150 ent = 0;
151 } else {
David S. Millerbb74b732007-10-13 23:27:48 -0700152 virt_to_real_irq_table[ent].dev_handle = dev_handle;
153 virt_to_real_irq_table[ent].dev_ino = dev_ino;
David S. Miller256c1df2007-10-13 23:50:38 -0700154 virt_to_real_irq_table[ent].in_use = 1;
David S. Miller8047e242006-06-20 01:22:35 -0700155 }
156
David S. Miller759f89e2007-10-11 03:16:13 -0700157 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700158
159 return ent;
160}
161
David S. Miller5746c992007-02-20 01:26:48 -0800162#ifdef CONFIG_PCI_MSI
David S. Miller759f89e2007-10-11 03:16:13 -0700163void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700164{
David S. Miller759f89e2007-10-11 03:16:13 -0700165 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700166
David S. Miller35a17eb2007-02-10 17:41:02 -0800167 if (virt_irq >= NR_IRQS)
168 return;
169
David S. Miller759f89e2007-10-11 03:16:13 -0700170 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
171
David S. Miller256c1df2007-10-13 23:50:38 -0700172 virt_to_real_irq_table[virt_irq].in_use = 0;
David S. Miller35a17eb2007-02-10 17:41:02 -0800173
David S. Miller759f89e2007-10-11 03:16:13 -0700174 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700175}
David S. Miller5746c992007-02-20 01:26:48 -0800176#endif
David S. Miller8047e242006-06-20 01:22:35 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
David S. Millere18e2a02006-06-20 01:23:32 -0700179 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182int show_interrupts(struct seq_file *p, void *v)
183{
David S. Millere18e2a02006-06-20 01:23:32 -0700184 int i = *(loff_t *) v, j;
185 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David S. Millere18e2a02006-06-20 01:23:32 -0700188 if (i == 0) {
189 seq_printf(p, " ");
190 for_each_online_cpu(j)
191 seq_printf(p, "CPU%d ",j);
192 seq_putc(p, '\n');
193 }
194
195 if (i < NR_IRQS) {
196 spin_lock_irqsave(&irq_desc[i].lock, flags);
197 action = irq_desc[i].action;
198 if (!action)
199 goto skip;
200 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#ifndef CONFIG_SMP
202 seq_printf(p, "%10u ", kstat_irqs(i));
203#else
David S. Millere18e2a02006-06-20 01:23:32 -0700204 for_each_online_cpu(j)
205 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700207 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700208 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David S. Millere18e2a02006-06-20 01:23:32 -0700210 for (action=action->next; action; action = action->next)
211 seq_printf(p, ", %s", action->name);
212
213 seq_putc(p, '\n');
214skip:
215 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
David S. Millerebd8c562006-02-17 08:38:06 -0800220static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
221{
222 unsigned int tid;
223
224 if (this_is_starfire) {
225 tid = starfire_translate(imap, cpuid);
226 tid <<= IMAP_TID_SHIFT;
227 tid &= IMAP_TID_UPA;
228 } else {
229 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
230 unsigned long ver;
231
232 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
233 if ((ver >> 32UL) == __JALAPENO_ID ||
234 (ver >> 32UL) == __SERRANO_ID) {
235 tid = cpuid << IMAP_TID_SHIFT;
236 tid &= IMAP_TID_JBUS;
237 } else {
238 unsigned int a = cpuid & 0x1f;
239 unsigned int n = (cpuid >> 5) & 0x1f;
240
241 tid = ((a << IMAP_AID_SHIFT) |
242 (n << IMAP_NID_SHIFT));
243 tid &= (IMAP_AID_SAFARI |
244 IMAP_NID_SAFARI);;
245 }
246 } else {
247 tid = cpuid << IMAP_TID_SHIFT;
248 tid &= IMAP_TID_UPA;
249 }
250 }
251
252 return tid;
253}
254
David S. Millere18e2a02006-06-20 01:23:32 -0700255struct irq_handler_data {
256 unsigned long iclr;
257 unsigned long imap;
258
259 void (*pre_handler)(unsigned int, void *, void *);
260 void *pre_handler_arg1;
261 void *pre_handler_arg2;
262};
263
David S. Millere18e2a02006-06-20 01:23:32 -0700264#ifdef CONFIG_SMP
265static int irq_choose_cpu(unsigned int virt_irq)
266{
Ingo Molnara53da522006-06-29 02:24:38 -0700267 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700268 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
David S. Millere18e2a02006-06-20 01:23:32 -0700270 if (cpus_equal(mask, CPU_MASK_ALL)) {
271 static int irq_rover;
272 static DEFINE_SPINLOCK(irq_rover_lock);
273 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800274
David S. Millere18e2a02006-06-20 01:23:32 -0700275 /* Round-robin distribution... */
276 do_round_robin:
277 spin_lock_irqsave(&irq_rover_lock, flags);
278
279 while (!cpu_online(irq_rover)) {
280 if (++irq_rover >= NR_CPUS)
281 irq_rover = 0;
282 }
283 cpuid = irq_rover;
284 do {
285 if (++irq_rover >= NR_CPUS)
286 irq_rover = 0;
287 } while (!cpu_online(irq_rover));
288
289 spin_unlock_irqrestore(&irq_rover_lock, flags);
290 } else {
291 cpumask_t tmp;
292
293 cpus_and(tmp, cpu_online_map, mask);
294
295 if (cpus_empty(tmp))
296 goto do_round_robin;
297
298 cpuid = first_cpu(tmp);
299 }
300
301 return cpuid;
302}
303#else
304static int irq_choose_cpu(unsigned int virt_irq)
305{
306 return real_hard_smp_processor_id();
307}
308#endif
309
310static void sun4u_irq_enable(unsigned int virt_irq)
311{
David S. Miller68c92182007-01-29 12:12:28 -0800312 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700313
314 if (likely(data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700315 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700316 unsigned int tid;
317
318 cpuid = irq_choose_cpu(virt_irq);
319 imap = data->imap;
320
321 tid = sun4u_compute_tid(imap, cpuid);
322
David S. Miller861fe902007-05-02 17:31:36 -0700323 val = upa_readq(imap);
324 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
325 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
326 val |= tid | IMAP_VALID;
327 upa_writeq(val, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700328 }
329}
330
David S. Millerb53bcb62007-07-14 03:16:13 -0700331static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
332{
333 sun4u_irq_enable(virt_irq);
334}
335
David S. Millere18e2a02006-06-20 01:23:32 -0700336static void sun4u_irq_disable(unsigned int virt_irq)
337{
David S. Miller68c92182007-01-29 12:12:28 -0800338 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700339
340 if (likely(data)) {
341 unsigned long imap = data->imap;
David S. Miller6e69d602007-08-28 14:25:32 -0700342 unsigned long tmp = upa_readq(imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700343
344 tmp &= ~IMAP_VALID;
David S. Miller861fe902007-05-02 17:31:36 -0700345 upa_writeq(tmp, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700346 }
347}
348
349static void sun4u_irq_end(unsigned int virt_irq)
350{
David S. Miller68c92182007-01-29 12:12:28 -0800351 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700352 struct irq_desc *desc = irq_desc + virt_irq;
353
354 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
355 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700356
357 if (likely(data))
David S. Miller861fe902007-05-02 17:31:36 -0700358 upa_writeq(ICLR_IDLE, data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700359}
360
361static void sun4v_irq_enable(unsigned int virt_irq)
362{
David S. Miller771823002007-10-13 23:41:28 -0700363 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
364 unsigned long cpuid = irq_choose_cpu(virt_irq);
365 int err;
David S. Millere18e2a02006-06-20 01:23:32 -0700366
David S. Miller771823002007-10-13 23:41:28 -0700367 err = sun4v_intr_settarget(ino, cpuid);
368 if (err != HV_EOK)
369 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
370 "err(%d)\n", ino, cpuid, err);
371 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
372 if (err != HV_EOK)
373 printk(KERN_ERR "sun4v_intr_setstate(%x): "
374 "err(%d)\n", ino, err);
375 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
376 if (err != HV_EOK)
377 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
378 ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
David S. Millerb53bcb62007-07-14 03:16:13 -0700381static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
382{
David S. Miller771823002007-10-13 23:41:28 -0700383 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
384 unsigned long cpuid = irq_choose_cpu(virt_irq);
385 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700386
David S. Miller771823002007-10-13 23:41:28 -0700387 err = sun4v_intr_settarget(ino, cpuid);
388 if (err != HV_EOK)
389 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
390 "err(%d)\n", ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700391}
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. Miller771823002007-10-13 23:41:28 -0700395 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
396 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David S. Miller771823002007-10-13 23:41:28 -0700398 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
399 if (err != HV_EOK)
400 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
401 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
David S. Millere18e2a02006-06-20 01:23:32 -0700404static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700405{
David S. Miller771823002007-10-13 23:41:28 -0700406 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller5a606b72007-07-09 22:40:36 -0700407 struct irq_desc *desc = irq_desc + virt_irq;
David S. Miller771823002007-10-13 23:41:28 -0700408 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700409
410 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
411 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700412
David S. Miller771823002007-10-13 23:41:28 -0700413 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
414 if (err != HV_EOK)
415 printk(KERN_ERR "sun4v_intr_setstate(%x): "
416 "err(%d)\n", ino, err);
David S. Miller088dd1f2005-07-04 13:24:38 -0700417}
418
David S. Miller4a907de2007-06-13 00:01:04 -0700419static void sun4v_virq_enable(unsigned int virt_irq)
420{
David S. Miller771823002007-10-13 23:41:28 -0700421 unsigned long cpuid, dev_handle, dev_ino;
422 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700423
David S. Miller771823002007-10-13 23:41:28 -0700424 cpuid = irq_choose_cpu(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700425
David S. Miller771823002007-10-13 23:41:28 -0700426 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
427 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700428
David S. Miller771823002007-10-13 23:41:28 -0700429 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
430 if (err != HV_EOK)
431 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
432 "err(%d)\n",
433 dev_handle, dev_ino, cpuid, err);
434 err = sun4v_vintr_set_state(dev_handle, dev_ino,
435 HV_INTR_STATE_IDLE);
436 if (err != HV_EOK)
437 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
438 "HV_INTR_STATE_IDLE): err(%d)\n",
439 dev_handle, dev_ino, err);
440 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
441 HV_INTR_ENABLED);
442 if (err != HV_EOK)
443 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
444 "HV_INTR_ENABLED): err(%d)\n",
445 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700446}
447
David S. Millerb53bcb62007-07-14 03:16:13 -0700448static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
449{
David S. Miller771823002007-10-13 23:41:28 -0700450 unsigned long cpuid, dev_handle, dev_ino;
451 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700452
David S. Miller771823002007-10-13 23:41:28 -0700453 cpuid = irq_choose_cpu(virt_irq);
David S. Millerb53bcb62007-07-14 03:16:13 -0700454
David S. Miller771823002007-10-13 23:41:28 -0700455 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
456 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millerb53bcb62007-07-14 03:16:13 -0700457
David S. Miller771823002007-10-13 23:41:28 -0700458 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
459 if (err != HV_EOK)
460 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
461 "err(%d)\n",
462 dev_handle, dev_ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700463}
464
David S. Miller4a907de2007-06-13 00:01:04 -0700465static void sun4v_virq_disable(unsigned int virt_irq)
466{
David S. Miller771823002007-10-13 23:41:28 -0700467 unsigned long dev_handle, dev_ino;
468 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700469
David S. Miller771823002007-10-13 23:41:28 -0700470 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
471 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700472
David S. Miller771823002007-10-13 23:41:28 -0700473 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
474 HV_INTR_DISABLED);
475 if (err != HV_EOK)
476 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
477 "HV_INTR_DISABLED): err(%d)\n",
478 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700479}
480
481static void sun4v_virq_end(unsigned int virt_irq)
482{
David S. Miller5a606b72007-07-09 22:40:36 -0700483 struct irq_desc *desc = irq_desc + virt_irq;
David S. Miller771823002007-10-13 23:41:28 -0700484 unsigned long dev_handle, dev_ino;
485 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700486
487 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
488 return;
David S. Miller4a907de2007-06-13 00:01:04 -0700489
David S. Miller771823002007-10-13 23:41:28 -0700490 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
491 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700492
David S. Miller771823002007-10-13 23:41:28 -0700493 err = sun4v_vintr_set_state(dev_handle, dev_ino,
494 HV_INTR_STATE_IDLE);
495 if (err != HV_EOK)
496 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
497 "HV_INTR_STATE_IDLE): err(%d)\n",
498 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700499}
500
David S. Millere18e2a02006-06-20 01:23:32 -0700501static void run_pre_handler(unsigned int virt_irq)
502{
David S. Miller68c92182007-01-29 12:12:28 -0800503 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millerbb74b732007-10-13 23:27:48 -0700504 unsigned int ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700505
David S. Millerbb74b732007-10-13 23:27:48 -0700506 ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700507 if (likely(data->pre_handler)) {
David S. Millerbb74b732007-10-13 23:27:48 -0700508 data->pre_handler(ino,
David S. Millere18e2a02006-06-20 01:23:32 -0700509 data->pre_handler_arg1,
510 data->pre_handler_arg2);
511 }
512}
513
David S. Miller729e7d72006-12-12 00:59:12 -0800514static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700515 .typename = "sun4u",
516 .enable = sun4u_irq_enable,
517 .disable = sun4u_irq_disable,
518 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700519 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700520};
521
David S. Miller729e7d72006-12-12 00:59:12 -0800522static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700523 .typename = "sun4u+ack",
524 .enable = sun4u_irq_enable,
525 .disable = sun4u_irq_disable,
526 .ack = run_pre_handler,
527 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700528 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700529};
530
David S. Miller729e7d72006-12-12 00:59:12 -0800531static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700532 .typename = "sun4v",
533 .enable = sun4v_irq_enable,
534 .disable = sun4v_irq_disable,
535 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700536 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700537};
538
David S. Miller4a907de2007-06-13 00:01:04 -0700539static struct irq_chip sun4v_virq = {
540 .typename = "vsun4v",
541 .enable = sun4v_virq_enable,
542 .disable = sun4v_virq_disable,
543 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700544 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700545};
546
David S. Millere18e2a02006-06-20 01:23:32 -0700547void irq_install_pre_handler(int virt_irq,
548 void (*func)(unsigned int, void *, void *),
549 void *arg1, void *arg2)
550{
David S. Miller68c92182007-01-29 12:12:28 -0800551 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller759f89e2007-10-11 03:16:13 -0700552 struct irq_chip *chip = get_irq_chip(virt_irq);
553
554 if (WARN_ON(chip == &sun4v_irq || chip == &sun4v_virq)) {
555 printk(KERN_ERR "IRQ: Trying to install pre-handler on "
556 "sun4v irq %u\n", virt_irq);
557 return;
558 }
David S. Millere18e2a02006-06-20 01:23:32 -0700559
560 data->pre_handler = func;
561 data->pre_handler_arg1 = arg1;
562 data->pre_handler_arg2 = arg2;
563
David S. Miller759f89e2007-10-11 03:16:13 -0700564 if (chip == &sun4u_irq_ack)
David S. Miller24ac26d2006-06-29 14:38:21 -0700565 return;
566
David S. Miller759f89e2007-10-11 03:16:13 -0700567 set_irq_chip(virt_irq, &sun4u_irq_ack);
David S. Millere18e2a02006-06-20 01:23:32 -0700568}
569
570unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700573 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700574 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int ino;
576
David S. Miller10951ee2006-02-13 18:22:57 -0800577 BUG_ON(tlb_type == hypervisor);
578
David S. Miller861fe902007-05-02 17:31:36 -0700579 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700580 bucket = &ivector_table[ino];
David S. Miller42d5f992007-10-13 23:03:21 -0700581 virt_irq = bucket_get_virt_irq(__pa(bucket));
582 if (!virt_irq) {
David S. Miller256c1df2007-10-13 23:50:38 -0700583 virt_irq = virt_irq_alloc(0, ino);
David S. Miller42d5f992007-10-13 23:03:21 -0700584 bucket_set_virt_irq(__pa(bucket), virt_irq);
585 set_irq_chip(virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700586 }
587
David S. Miller42d5f992007-10-13 23:03:21 -0700588 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800589 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700590 goto out;
591
592 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
593 if (unlikely(!data)) {
594 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700595 prom_halt();
596 }
David S. Miller42d5f992007-10-13 23:03:21 -0700597 set_irq_chip_data(virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700598
David S. Millere18e2a02006-06-20 01:23:32 -0700599 data->imap = imap;
600 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
David S. Miller088dd1f2005-07-04 13:24:38 -0700602out:
David S. Miller42d5f992007-10-13 23:03:21 -0700603 return virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
David S. Miller4a907de2007-06-13 00:01:04 -0700606static unsigned int sun4v_build_common(unsigned long sysino,
607 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800608{
609 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700610 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700611 unsigned int virt_irq;
David S. Millere18e2a02006-06-20 01:23:32 -0700612
613 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800614
David S. Millere3999572006-02-13 18:16:10 -0800615 bucket = &ivector_table[sysino];
David S. Miller42d5f992007-10-13 23:03:21 -0700616 virt_irq = bucket_get_virt_irq(__pa(bucket));
617 if (!virt_irq) {
David S. Miller256c1df2007-10-13 23:50:38 -0700618 virt_irq = virt_irq_alloc(0, sysino);
David S. Miller42d5f992007-10-13 23:03:21 -0700619 bucket_set_virt_irq(__pa(bucket), virt_irq);
620 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700621 }
622
David S. Miller42d5f992007-10-13 23:03:21 -0700623 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800624 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700625 goto out;
626
627 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
628 if (unlikely(!data)) {
629 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
630 prom_halt();
631 }
David S. Miller42d5f992007-10-13 23:03:21 -0700632 set_irq_chip_data(virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800633
634 /* Catch accidental accesses to these things. IMAP/ICLR handling
635 * is done by hypervisor calls on sun4v platforms, not by direct
636 * register accesses.
637 */
David S. Millere18e2a02006-06-20 01:23:32 -0700638 data->imap = ~0UL;
639 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800640
David S. Millere18e2a02006-06-20 01:23:32 -0700641out:
David S. Miller42d5f992007-10-13 23:03:21 -0700642 return virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800643}
644
David S. Miller4a907de2007-06-13 00:01:04 -0700645unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
646{
647 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
648
649 return sun4v_build_common(sysino, &sun4v_irq);
650}
651
652unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
653{
David S. Millerb80e6992007-10-13 21:51:37 -0700654 struct irq_handler_data *data;
655 struct ino_bucket *bucket;
656 unsigned long hv_err, cookie;
David S. Miller42d5f992007-10-13 23:03:21 -0700657 unsigned int virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700658
David S. Millerb80e6992007-10-13 21:51:37 -0700659 bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
660 if (unlikely(!bucket))
661 return 0;
David S. Miller42d5f992007-10-13 23:03:21 -0700662 __flush_dcache_range((unsigned long) bucket,
663 ((unsigned long) bucket +
664 sizeof(struct ino_bucket)));
David S. Miller4a907de2007-06-13 00:01:04 -0700665
David S. Miller256c1df2007-10-13 23:50:38 -0700666 virt_irq = virt_irq_alloc(devhandle, devino);
David S. Miller42d5f992007-10-13 23:03:21 -0700667 bucket_set_virt_irq(__pa(bucket), virt_irq);
668 set_irq_chip(virt_irq, &sun4v_virq);
David S. Miller4a907de2007-06-13 00:01:04 -0700669
David S. Millerb80e6992007-10-13 21:51:37 -0700670 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
671 if (unlikely(!data))
672 return 0;
673
David S. Miller42d5f992007-10-13 23:03:21 -0700674 set_irq_chip_data(virt_irq, data);
David S. Millerb80e6992007-10-13 21:51:37 -0700675
676 /* Catch accidental accesses to these things. IMAP/ICLR handling
677 * is done by hypervisor calls on sun4v platforms, not by direct
678 * register accesses.
679 */
680 data->imap = ~0UL;
681 data->iclr = ~0UL;
682
683 cookie = ~__pa(bucket);
684 hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
David S. Miller4a907de2007-06-13 00:01:04 -0700685 if (hv_err) {
686 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
687 "err=%lu\n", devhandle, devino, hv_err);
688 prom_halt();
689 }
690
David S. Miller42d5f992007-10-13 23:03:21 -0700691 return virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700692}
693
David S. Millere18e2a02006-06-20 01:23:32 -0700694void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700695{
David S. Miller771823002007-10-13 23:41:28 -0700696 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller088dd1f2005-07-04 13:24:38 -0700697
David S. Miller771823002007-10-13 23:41:28 -0700698 if (!ino)
699 ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700700
David S. Millere18e2a02006-06-20 01:23:32 -0700701 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
702 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void handler_irq(int irq, struct pt_regs *regs)
706{
David S. Millereb2d8d62007-10-13 21:42:46 -0700707 unsigned long pstate, bucket_pa;
Al Viro6d24c8d2006-10-08 08:23:28 -0400708 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Al Viro6d24c8d2006-10-08 08:23:28 -0400712 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David S. Millera650d382007-10-12 02:59:40 -0700715 /* Grab an atomic snapshot of the pending IVECs. */
716 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
717 "wrpr %0, %3, %%pstate\n\t"
718 "ldx [%2], %1\n\t"
719 "stx %%g0, [%2]\n\t"
720 "wrpr %0, 0x0, %%pstate\n\t"
David S. Millereb2d8d62007-10-13 21:42:46 -0700721 : "=&r" (pstate), "=&r" (bucket_pa)
722 : "r" (irq_work_pa(smp_processor_id())),
David S. Millera650d382007-10-12 02:59:40 -0700723 "i" (PSTATE_IE)
724 : "memory");
725
David S. Millereb2d8d62007-10-13 21:42:46 -0700726 while (bucket_pa) {
727 unsigned long next_pa;
728 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
David S. Miller42d5f992007-10-13 23:03:21 -0700730 next_pa = bucket_get_chain_pa(bucket_pa);
731 virt_irq = bucket_get_virt_irq(bucket_pa);
732 bucket_clear_chain_pa(bucket_pa);
David S. Millerfd0504c32006-06-20 01:20:00 -0700733
David S. Millereb2d8d62007-10-13 21:42:46 -0700734 __do_IRQ(virt_irq);
735
736 bucket_pa = next_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
David S. Millere18e2a02006-06-20 01:23:32 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400740 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
David S. Millere02044092007-07-16 03:49:40 -0700743#ifdef CONFIG_HOTPLUG_CPU
744void fixup_irqs(void)
745{
746 unsigned int irq;
747
748 for (irq = 0; irq < NR_IRQS; irq++) {
749 unsigned long flags;
750
751 spin_lock_irqsave(&irq_desc[irq].lock, flags);
752 if (irq_desc[irq].action &&
753 !(irq_desc[irq].status & IRQ_PER_CPU)) {
754 if (irq_desc[irq].chip->set_affinity)
755 irq_desc[irq].chip->set_affinity(irq,
756 irq_desc[irq].affinity);
757 }
758 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
759 }
760}
761#endif
762
David S. Millercdd51862005-07-24 19:36:13 -0700763struct sun5_timer {
764 u64 count0;
765 u64 limit0;
766 u64 count1;
767 u64 limit1;
768};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
David S. Millercdd51862005-07-24 19:36:13 -0700770static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static u64 prom_limit0, prom_limit1;
772
773static void map_prom_timers(void)
774{
David S. Miller25c75812006-06-22 20:21:22 -0700775 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700776 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700779 dp = of_find_node_by_path("/");
780 dp = dp->child;
781 while (dp) {
782 if (!strcmp(dp->name, "counter-timer"))
783 break;
784 dp = dp->sibling;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* Assume if node is not present, PROM uses different tick mechanism
788 * which we should not care about.
789 */
David S. Miller25c75812006-06-22 20:21:22 -0700790 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 prom_timers = (struct sun5_timer *) 0;
792 return;
793 }
794
795 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700796 addr = of_get_property(dp, "address", NULL);
797 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 prom_printf("PROM does not have timer mapped, trying to continue.\n");
799 prom_timers = (struct sun5_timer *) 0;
800 return;
801 }
802 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
803}
804
805static void kill_prom_timer(void)
806{
807 if (!prom_timers)
808 return;
809
810 /* Save them away for later. */
811 prom_limit0 = prom_timers->limit0;
812 prom_limit1 = prom_timers->limit1;
813
814 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
815 * We turn both off here just to be paranoid.
816 */
817 prom_timers->limit0 = 0;
818 prom_timers->limit1 = 0;
819
820 /* Wheee, eat the interrupt packet too... */
821 __asm__ __volatile__(
822" mov 0x40, %%g2\n"
823" ldxa [%%g0] %0, %%g1\n"
824" ldxa [%%g2] %1, %%g1\n"
825" stxa %%g0, [%%g0] %0\n"
826" membar #Sync\n"
827 : /* no outputs */
828 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
829 : "g1", "g2");
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832void init_irqwork_curcpu(void)
833{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 int cpu = hard_smp_processor_id();
835
David S. Millereb2d8d62007-10-13 21:42:46 -0700836 trap_block[cpu].irq_worklist_pa = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
David S. Miller5cbc3072007-05-25 15:49:59 -0700839/* Please be very careful with register_one_mondo() and
840 * sun4v_register_mondo_queues().
841 *
842 * On SMP this gets invoked from the CPU trampoline before
843 * the cpu has fully taken over the trap table from OBP,
844 * and it's kernel stack + %g6 thread register state is
845 * not fully cooked yet.
846 *
847 * Therefore you cannot make any OBP calls, not even prom_printf,
848 * from these two routines.
849 */
850static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800851{
David S. Miller5cbc3072007-05-25 15:49:59 -0700852 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800853 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800854
David S. Miller94f87622006-02-16 14:26:53 -0800855 status = sun4v_cpu_qconf(type, paddr, num_entries);
856 if (status != HV_EOK) {
857 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
858 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800859 prom_halt();
860 }
861}
862
David S. Millerb434e712007-08-08 17:32:33 -0700863void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c05722006-02-08 02:53:50 -0800864{
David S. Millerb5a37e92006-02-11 23:07:13 -0800865 struct trap_per_cpu *tb = &trap_block[this_cpu];
866
David S. Miller5cbc3072007-05-25 15:49:59 -0700867 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
868 tb->cpu_mondo_qmask);
869 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
870 tb->dev_mondo_qmask);
871 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
872 tb->resum_qmask);
873 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
874 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -0800875}
876
David S. Millerb434e712007-08-08 17:32:33 -0700877static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800878{
David S. Miller5cbc3072007-05-25 15:49:59 -0700879 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700880 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5cbc3072007-05-25 15:49:59 -0700881 if (!p) {
David S. Millerb5a37e92006-02-11 23:07:13 -0800882 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
883 prom_halt();
884 }
885
David S. Miller5cbc3072007-05-25 15:49:59 -0700886 *pa_ptr = __pa(p);
David S. Millerb5a37e92006-02-11 23:07:13 -0800887}
888
David S. Millerb434e712007-08-08 17:32:33 -0700889static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800890{
David S. Miller5cbc3072007-05-25 15:49:59 -0700891 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700892 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5b0c05722006-02-08 02:53:50 -0800893
David S. Miller5cbc3072007-05-25 15:49:59 -0700894 if (!p) {
David S. Miller5b0c05722006-02-08 02:53:50 -0800895 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
896 prom_halt();
897 }
898
David S. Miller5cbc3072007-05-25 15:49:59 -0700899 *pa_ptr = __pa(p);
David S. Miller5b0c05722006-02-08 02:53:50 -0800900}
901
David S. Millerb434e712007-08-08 17:32:33 -0700902static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800903{
904#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800905 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800906
907 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
908
David S. Millerb434e712007-08-08 17:32:33 -0700909 page = alloc_bootmem_low_pages(PAGE_SIZE);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800910 if (!page) {
911 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
912 prom_halt();
913 }
914
915 tb->cpu_mondo_block_pa = __pa(page);
916 tb->cpu_list_pa = __pa(page + 64);
917#endif
918}
919
David S. Millerb434e712007-08-08 17:32:33 -0700920/* Allocate mondo and error queues for all possible cpus. */
921static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -0800922{
David S. Millerb434e712007-08-08 17:32:33 -0700923 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -0800924
David S. Millerb434e712007-08-08 17:32:33 -0700925 for_each_possible_cpu(cpu) {
926 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -0800927
David S. Millerb434e712007-08-08 17:32:33 -0700928 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
929 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
930 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
931 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
932 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
933 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
934 tb->nonresum_qmask);
935
936 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -0800937 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800938
David S. Millerb434e712007-08-08 17:32:33 -0700939 /* Load up the boot cpu's entries. */
940 sun4v_register_mondo_queues(hard_smp_processor_id());
David S. Millerac29c112006-02-08 00:08:23 -0800941}
942
David S. Millere18e2a02006-06-20 01:23:32 -0700943static struct irqaction timer_irq_action = {
944 .name = "timer",
945};
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/* Only invoked on boot processor. */
948void __init init_IRQ(void)
949{
David S. Miller10397e42007-10-13 21:43:31 -0700950 unsigned long size;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 map_prom_timers();
953 kill_prom_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
David S. Miller10397e42007-10-13 21:43:31 -0700955 size = sizeof(struct ino_bucket) * NUM_IVECS;
956 ivector_table = alloc_bootmem_low(size);
957 if (!ivector_table) {
958 prom_printf("Fatal error, cannot allocate ivector_table\n");
959 prom_halt();
960 }
David S. Miller42d5f992007-10-13 23:03:21 -0700961 __flush_dcache_range((unsigned long) ivector_table,
962 ((unsigned long) ivector_table) + size);
David S. Miller10397e42007-10-13 21:43:31 -0700963
964 ivector_table_pa = __pa(ivector_table);
David S. Millereb2d8d62007-10-13 21:42:46 -0700965
David S. Millerac29c112006-02-08 00:08:23 -0800966 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -0700967 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -0800968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* We need to clear any IRQ's pending in the soft interrupt
970 * registers, a spurious one could be left around from the
971 * PROM timer which we just disabled.
972 */
973 clear_softint(get_softint());
974
975 /* Now that ivector table is initialized, it is safe
976 * to receive IRQ vector traps. We will normally take
977 * one or two right now, in case some device PROM used
978 * to boot us wants to speak to us. We just ignore them.
979 */
980 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
981 "or %%g1, %0, %%g1\n\t"
982 "wrpr %%g1, 0x0, %%pstate"
983 : /* No outputs */
984 : "i" (PSTATE_IE)
985 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -0700986
987 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}