blob: 2be6bcbe50ca69532ce35b60a8e0a51d5d1299dc [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 __irq(bucket) ((unsigned long)(bucket))
David S. Millere18e2a02006-06-20 01:23:32 -0700126
David S. Millereb2d8d62007-10-13 21:42:46 -0700127#define irq_work_pa(__cpu) &(trap_block[(__cpu)].irq_worklist_pa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David S. Miller93b32382007-07-20 02:58:28 -0700129static struct {
David S. Millera650d382007-10-12 02:59:40 -0700130 unsigned long irq;
David S. Miller93b32382007-07-20 02:58:28 -0700131 unsigned int dev_handle;
132 unsigned int dev_ino;
133} virt_to_real_irq_table[NR_IRQS];
David S. Miller759f89e2007-10-11 03:16:13 -0700134static DEFINE_SPINLOCK(virt_irq_alloc_lock);
David S. Miller8047e242006-06-20 01:22:35 -0700135
David S. Millerbb74b732007-10-13 23:27:48 -0700136unsigned char virt_irq_alloc(unsigned long real_irq,
137 unsigned int dev_handle,
138 unsigned int dev_ino)
David S. Miller8047e242006-06-20 01:22:35 -0700139{
David S. Miller759f89e2007-10-11 03:16:13 -0700140 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700141 unsigned char ent;
142
143 BUILD_BUG_ON(NR_IRQS >= 256);
144
David S. Miller759f89e2007-10-11 03:16:13 -0700145 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
146
David S. Miller35a17eb2007-02-10 17:41:02 -0800147 for (ent = 1; ent < NR_IRQS; ent++) {
David S. Miller93b32382007-07-20 02:58:28 -0700148 if (!virt_to_real_irq_table[ent].irq)
David S. Miller35a17eb2007-02-10 17:41:02 -0800149 break;
150 }
David S. Miller8047e242006-06-20 01:22:35 -0700151 if (ent >= NR_IRQS) {
152 printk(KERN_ERR "IRQ: Out of virtual IRQs.\n");
David S. Miller759f89e2007-10-11 03:16:13 -0700153 ent = 0;
154 } else {
155 virt_to_real_irq_table[ent].irq = real_irq;
David S. Millerbb74b732007-10-13 23:27:48 -0700156 virt_to_real_irq_table[ent].dev_handle = dev_handle;
157 virt_to_real_irq_table[ent].dev_ino = dev_ino;
David S. Miller8047e242006-06-20 01:22:35 -0700158 }
159
David S. Miller759f89e2007-10-11 03:16:13 -0700160 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700161
162 return ent;
163}
164
David S. Miller5746c992007-02-20 01:26:48 -0800165#ifdef CONFIG_PCI_MSI
David S. Miller759f89e2007-10-11 03:16:13 -0700166void virt_irq_free(unsigned int virt_irq)
David S. Miller8047e242006-06-20 01:22:35 -0700167{
David S. Miller759f89e2007-10-11 03:16:13 -0700168 unsigned long flags;
David S. Miller8047e242006-06-20 01:22:35 -0700169
David S. Miller35a17eb2007-02-10 17:41:02 -0800170 if (virt_irq >= NR_IRQS)
171 return;
172
David S. Miller759f89e2007-10-11 03:16:13 -0700173 spin_lock_irqsave(&virt_irq_alloc_lock, flags);
174
David S. Miller93b32382007-07-20 02:58:28 -0700175 virt_to_real_irq_table[virt_irq].irq = 0;
David S. Miller35a17eb2007-02-10 17:41:02 -0800176
David S. Miller759f89e2007-10-11 03:16:13 -0700177 spin_unlock_irqrestore(&virt_irq_alloc_lock, flags);
David S. Miller8047e242006-06-20 01:22:35 -0700178}
David S. Miller5746c992007-02-20 01:26:48 -0800179#endif
David S. Miller8047e242006-06-20 01:22:35 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
David S. Millere18e2a02006-06-20 01:23:32 -0700182 * /proc/interrupts printing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185int show_interrupts(struct seq_file *p, void *v)
186{
David S. Millere18e2a02006-06-20 01:23:32 -0700187 int i = *(loff_t *) v, j;
188 struct irqaction * action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David S. Millere18e2a02006-06-20 01:23:32 -0700191 if (i == 0) {
192 seq_printf(p, " ");
193 for_each_online_cpu(j)
194 seq_printf(p, "CPU%d ",j);
195 seq_putc(p, '\n');
196 }
197
198 if (i < NR_IRQS) {
199 spin_lock_irqsave(&irq_desc[i].lock, flags);
200 action = irq_desc[i].action;
201 if (!action)
202 goto skip;
203 seq_printf(p, "%3d: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#ifndef CONFIG_SMP
205 seq_printf(p, "%10u ", kstat_irqs(i));
206#else
David S. Millere18e2a02006-06-20 01:23:32 -0700207 for_each_online_cpu(j)
208 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700210 seq_printf(p, " %9s", irq_desc[i].chip->typename);
David S. Millere18e2a02006-06-20 01:23:32 -0700211 seq_printf(p, " %s", action->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David S. Millere18e2a02006-06-20 01:23:32 -0700213 for (action=action->next; action; action = action->next)
214 seq_printf(p, ", %s", action->name);
215
216 seq_putc(p, '\n');
217skip:
218 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221}
222
David S. Millerebd8c562006-02-17 08:38:06 -0800223static unsigned int sun4u_compute_tid(unsigned long imap, unsigned long cpuid)
224{
225 unsigned int tid;
226
227 if (this_is_starfire) {
228 tid = starfire_translate(imap, cpuid);
229 tid <<= IMAP_TID_SHIFT;
230 tid &= IMAP_TID_UPA;
231 } else {
232 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
233 unsigned long ver;
234
235 __asm__ ("rdpr %%ver, %0" : "=r" (ver));
236 if ((ver >> 32UL) == __JALAPENO_ID ||
237 (ver >> 32UL) == __SERRANO_ID) {
238 tid = cpuid << IMAP_TID_SHIFT;
239 tid &= IMAP_TID_JBUS;
240 } else {
241 unsigned int a = cpuid & 0x1f;
242 unsigned int n = (cpuid >> 5) & 0x1f;
243
244 tid = ((a << IMAP_AID_SHIFT) |
245 (n << IMAP_NID_SHIFT));
246 tid &= (IMAP_AID_SAFARI |
247 IMAP_NID_SAFARI);;
248 }
249 } else {
250 tid = cpuid << IMAP_TID_SHIFT;
251 tid &= IMAP_TID_UPA;
252 }
253 }
254
255 return tid;
256}
257
David S. Millere18e2a02006-06-20 01:23:32 -0700258struct irq_handler_data {
259 unsigned long iclr;
260 unsigned long imap;
261
262 void (*pre_handler)(unsigned int, void *, void *);
263 void *pre_handler_arg1;
264 void *pre_handler_arg2;
265};
266
David S. Millere18e2a02006-06-20 01:23:32 -0700267#ifdef CONFIG_SMP
268static int irq_choose_cpu(unsigned int virt_irq)
269{
Ingo Molnara53da522006-06-29 02:24:38 -0700270 cpumask_t mask = irq_desc[virt_irq].affinity;
David S. Millere18e2a02006-06-20 01:23:32 -0700271 int cpuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David S. Millere18e2a02006-06-20 01:23:32 -0700273 if (cpus_equal(mask, CPU_MASK_ALL)) {
274 static int irq_rover;
275 static DEFINE_SPINLOCK(irq_rover_lock);
276 unsigned long flags;
David S. Millerebd8c562006-02-17 08:38:06 -0800277
David S. Millere18e2a02006-06-20 01:23:32 -0700278 /* Round-robin distribution... */
279 do_round_robin:
280 spin_lock_irqsave(&irq_rover_lock, flags);
281
282 while (!cpu_online(irq_rover)) {
283 if (++irq_rover >= NR_CPUS)
284 irq_rover = 0;
285 }
286 cpuid = irq_rover;
287 do {
288 if (++irq_rover >= NR_CPUS)
289 irq_rover = 0;
290 } while (!cpu_online(irq_rover));
291
292 spin_unlock_irqrestore(&irq_rover_lock, flags);
293 } else {
294 cpumask_t tmp;
295
296 cpus_and(tmp, cpu_online_map, mask);
297
298 if (cpus_empty(tmp))
299 goto do_round_robin;
300
301 cpuid = first_cpu(tmp);
302 }
303
304 return cpuid;
305}
306#else
307static int irq_choose_cpu(unsigned int virt_irq)
308{
309 return real_hard_smp_processor_id();
310}
311#endif
312
313static void sun4u_irq_enable(unsigned int virt_irq)
314{
David S. Miller68c92182007-01-29 12:12:28 -0800315 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700316
317 if (likely(data)) {
David S. Miller861fe902007-05-02 17:31:36 -0700318 unsigned long cpuid, imap, val;
David S. Millere18e2a02006-06-20 01:23:32 -0700319 unsigned int tid;
320
321 cpuid = irq_choose_cpu(virt_irq);
322 imap = data->imap;
323
324 tid = sun4u_compute_tid(imap, cpuid);
325
David S. Miller861fe902007-05-02 17:31:36 -0700326 val = upa_readq(imap);
327 val &= ~(IMAP_TID_UPA | IMAP_TID_JBUS |
328 IMAP_AID_SAFARI | IMAP_NID_SAFARI);
329 val |= tid | IMAP_VALID;
330 upa_writeq(val, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700331 }
332}
333
David S. Millerb53bcb62007-07-14 03:16:13 -0700334static void sun4u_set_affinity(unsigned int virt_irq, cpumask_t mask)
335{
336 sun4u_irq_enable(virt_irq);
337}
338
David S. Millere18e2a02006-06-20 01:23:32 -0700339static void sun4u_irq_disable(unsigned int virt_irq)
340{
David S. Miller68c92182007-01-29 12:12:28 -0800341 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millere18e2a02006-06-20 01:23:32 -0700342
343 if (likely(data)) {
344 unsigned long imap = data->imap;
David S. Miller6e69d602007-08-28 14:25:32 -0700345 unsigned long tmp = upa_readq(imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700346
347 tmp &= ~IMAP_VALID;
David S. Miller861fe902007-05-02 17:31:36 -0700348 upa_writeq(tmp, imap);
David S. Millere18e2a02006-06-20 01:23:32 -0700349 }
350}
351
352static void sun4u_irq_end(unsigned int virt_irq)
353{
David S. Miller68c92182007-01-29 12:12:28 -0800354 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller5a606b72007-07-09 22:40:36 -0700355 struct irq_desc *desc = irq_desc + virt_irq;
356
357 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
358 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700359
360 if (likely(data))
David S. Miller861fe902007-05-02 17:31:36 -0700361 upa_writeq(ICLR_IDLE, data->iclr);
David S. Millere18e2a02006-06-20 01:23:32 -0700362}
363
364static void sun4v_irq_enable(unsigned int virt_irq)
365{
David S. Miller771823002007-10-13 23:41:28 -0700366 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
367 unsigned long cpuid = irq_choose_cpu(virt_irq);
368 int err;
David S. Millere18e2a02006-06-20 01:23:32 -0700369
David S. Miller771823002007-10-13 23:41:28 -0700370 err = sun4v_intr_settarget(ino, cpuid);
371 if (err != HV_EOK)
372 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
373 "err(%d)\n", ino, cpuid, err);
374 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
375 if (err != HV_EOK)
376 printk(KERN_ERR "sun4v_intr_setstate(%x): "
377 "err(%d)\n", ino, err);
378 err = sun4v_intr_setenabled(ino, HV_INTR_ENABLED);
379 if (err != HV_EOK)
380 printk(KERN_ERR "sun4v_intr_setenabled(%x): err(%d)\n",
381 ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
David S. Millerb53bcb62007-07-14 03:16:13 -0700384static void sun4v_set_affinity(unsigned int virt_irq, cpumask_t mask)
385{
David S. Miller771823002007-10-13 23:41:28 -0700386 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
387 unsigned long cpuid = irq_choose_cpu(virt_irq);
388 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700389
David S. Miller771823002007-10-13 23:41:28 -0700390 err = sun4v_intr_settarget(ino, cpuid);
391 if (err != HV_EOK)
392 printk(KERN_ERR "sun4v_intr_settarget(%x,%lu): "
393 "err(%d)\n", ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700394}
395
David S. Millere18e2a02006-06-20 01:23:32 -0700396static void sun4v_irq_disable(unsigned int virt_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
David S. Miller771823002007-10-13 23:41:28 -0700398 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
399 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David S. Miller771823002007-10-13 23:41:28 -0700401 err = sun4v_intr_setenabled(ino, HV_INTR_DISABLED);
402 if (err != HV_EOK)
403 printk(KERN_ERR "sun4v_intr_setenabled(%x): "
404 "err(%d)\n", ino, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
David S. Millere18e2a02006-06-20 01:23:32 -0700407static void sun4v_irq_end(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700408{
David S. Miller771823002007-10-13 23:41:28 -0700409 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller5a606b72007-07-09 22:40:36 -0700410 struct irq_desc *desc = irq_desc + virt_irq;
David S. Miller771823002007-10-13 23:41:28 -0700411 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700412
413 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
414 return;
David S. Millere18e2a02006-06-20 01:23:32 -0700415
David S. Miller771823002007-10-13 23:41:28 -0700416 err = sun4v_intr_setstate(ino, HV_INTR_STATE_IDLE);
417 if (err != HV_EOK)
418 printk(KERN_ERR "sun4v_intr_setstate(%x): "
419 "err(%d)\n", ino, err);
David S. Miller088dd1f2005-07-04 13:24:38 -0700420}
421
David S. Miller4a907de2007-06-13 00:01:04 -0700422static void sun4v_virq_enable(unsigned int virt_irq)
423{
David S. Miller771823002007-10-13 23:41:28 -0700424 unsigned long cpuid, dev_handle, dev_ino;
425 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700426
David S. Miller771823002007-10-13 23:41:28 -0700427 cpuid = irq_choose_cpu(virt_irq);
David S. Miller4a907de2007-06-13 00:01:04 -0700428
David S. Miller771823002007-10-13 23:41:28 -0700429 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
430 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700431
David S. Miller771823002007-10-13 23:41:28 -0700432 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
433 if (err != HV_EOK)
434 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
435 "err(%d)\n",
436 dev_handle, dev_ino, cpuid, err);
437 err = sun4v_vintr_set_state(dev_handle, dev_ino,
438 HV_INTR_STATE_IDLE);
439 if (err != HV_EOK)
440 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
441 "HV_INTR_STATE_IDLE): err(%d)\n",
442 dev_handle, dev_ino, err);
443 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
444 HV_INTR_ENABLED);
445 if (err != HV_EOK)
446 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
447 "HV_INTR_ENABLED): err(%d)\n",
448 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700449}
450
David S. Millerb53bcb62007-07-14 03:16:13 -0700451static void sun4v_virt_set_affinity(unsigned int virt_irq, cpumask_t mask)
452{
David S. Miller771823002007-10-13 23:41:28 -0700453 unsigned long cpuid, dev_handle, dev_ino;
454 int err;
David S. Millerb53bcb62007-07-14 03:16:13 -0700455
David S. Miller771823002007-10-13 23:41:28 -0700456 cpuid = irq_choose_cpu(virt_irq);
David S. Millerb53bcb62007-07-14 03:16:13 -0700457
David S. Miller771823002007-10-13 23:41:28 -0700458 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
459 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millerb53bcb62007-07-14 03:16:13 -0700460
David S. Miller771823002007-10-13 23:41:28 -0700461 err = sun4v_vintr_set_target(dev_handle, dev_ino, cpuid);
462 if (err != HV_EOK)
463 printk(KERN_ERR "sun4v_vintr_set_target(%lx,%lx,%lu): "
464 "err(%d)\n",
465 dev_handle, dev_ino, cpuid, err);
David S. Millerb53bcb62007-07-14 03:16:13 -0700466}
467
David S. Miller4a907de2007-06-13 00:01:04 -0700468static void sun4v_virq_disable(unsigned int virt_irq)
469{
David S. Miller771823002007-10-13 23:41:28 -0700470 unsigned long dev_handle, dev_ino;
471 int err;
David S. Miller4a907de2007-06-13 00:01:04 -0700472
David S. Miller771823002007-10-13 23:41:28 -0700473 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
474 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700475
David S. Miller771823002007-10-13 23:41:28 -0700476 err = sun4v_vintr_set_valid(dev_handle, dev_ino,
477 HV_INTR_DISABLED);
478 if (err != HV_EOK)
479 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
480 "HV_INTR_DISABLED): err(%d)\n",
481 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700482}
483
484static void sun4v_virq_end(unsigned int virt_irq)
485{
David S. Miller5a606b72007-07-09 22:40:36 -0700486 struct irq_desc *desc = irq_desc + virt_irq;
David S. Miller771823002007-10-13 23:41:28 -0700487 unsigned long dev_handle, dev_ino;
488 int err;
David S. Miller5a606b72007-07-09 22:40:36 -0700489
490 if (unlikely(desc->status & (IRQ_DISABLED|IRQ_INPROGRESS)))
491 return;
David S. Miller4a907de2007-06-13 00:01:04 -0700492
David S. Miller771823002007-10-13 23:41:28 -0700493 dev_handle = virt_to_real_irq_table[virt_irq].dev_handle;
494 dev_ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller4a907de2007-06-13 00:01:04 -0700495
David S. Miller771823002007-10-13 23:41:28 -0700496 err = sun4v_vintr_set_state(dev_handle, dev_ino,
497 HV_INTR_STATE_IDLE);
498 if (err != HV_EOK)
499 printk(KERN_ERR "sun4v_vintr_set_state(%lx,%lx,"
500 "HV_INTR_STATE_IDLE): err(%d)\n",
501 dev_handle, dev_ino, err);
David S. Miller4a907de2007-06-13 00:01:04 -0700502}
503
David S. Millere18e2a02006-06-20 01:23:32 -0700504static void run_pre_handler(unsigned int virt_irq)
505{
David S. Miller68c92182007-01-29 12:12:28 -0800506 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Millerbb74b732007-10-13 23:27:48 -0700507 unsigned int ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700508
David S. Millerbb74b732007-10-13 23:27:48 -0700509 ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Millere18e2a02006-06-20 01:23:32 -0700510 if (likely(data->pre_handler)) {
David S. Millerbb74b732007-10-13 23:27:48 -0700511 data->pre_handler(ino,
David S. Millere18e2a02006-06-20 01:23:32 -0700512 data->pre_handler_arg1,
513 data->pre_handler_arg2);
514 }
515}
516
David S. Miller729e7d72006-12-12 00:59:12 -0800517static struct irq_chip sun4u_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700518 .typename = "sun4u",
519 .enable = sun4u_irq_enable,
520 .disable = sun4u_irq_disable,
521 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700522 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700523};
524
David S. Miller729e7d72006-12-12 00:59:12 -0800525static struct irq_chip sun4u_irq_ack = {
David S. Millere18e2a02006-06-20 01:23:32 -0700526 .typename = "sun4u+ack",
527 .enable = sun4u_irq_enable,
528 .disable = sun4u_irq_disable,
529 .ack = run_pre_handler,
530 .end = sun4u_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700531 .set_affinity = sun4u_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700532};
533
David S. Miller729e7d72006-12-12 00:59:12 -0800534static struct irq_chip sun4v_irq = {
David S. Millere18e2a02006-06-20 01:23:32 -0700535 .typename = "sun4v",
536 .enable = sun4v_irq_enable,
537 .disable = sun4v_irq_disable,
538 .end = sun4v_irq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700539 .set_affinity = sun4v_set_affinity,
David S. Millere18e2a02006-06-20 01:23:32 -0700540};
541
David S. Miller4a907de2007-06-13 00:01:04 -0700542static struct irq_chip sun4v_virq = {
543 .typename = "vsun4v",
544 .enable = sun4v_virq_enable,
545 .disable = sun4v_virq_disable,
546 .end = sun4v_virq_end,
David S. Millerb53bcb62007-07-14 03:16:13 -0700547 .set_affinity = sun4v_virt_set_affinity,
David S. Miller4a907de2007-06-13 00:01:04 -0700548};
549
David S. Millere18e2a02006-06-20 01:23:32 -0700550void irq_install_pre_handler(int virt_irq,
551 void (*func)(unsigned int, void *, void *),
552 void *arg1, void *arg2)
553{
David S. Miller68c92182007-01-29 12:12:28 -0800554 struct irq_handler_data *data = get_irq_chip_data(virt_irq);
David S. Miller759f89e2007-10-11 03:16:13 -0700555 struct irq_chip *chip = get_irq_chip(virt_irq);
556
557 if (WARN_ON(chip == &sun4v_irq || chip == &sun4v_virq)) {
558 printk(KERN_ERR "IRQ: Trying to install pre-handler on "
559 "sun4v irq %u\n", virt_irq);
560 return;
561 }
David S. Millere18e2a02006-06-20 01:23:32 -0700562
563 data->pre_handler = func;
564 data->pre_handler_arg1 = arg1;
565 data->pre_handler_arg2 = arg2;
566
David S. Miller759f89e2007-10-11 03:16:13 -0700567 if (chip == &sun4u_irq_ack)
David S. Miller24ac26d2006-06-29 14:38:21 -0700568 return;
569
David S. Miller759f89e2007-10-11 03:16:13 -0700570 set_irq_chip(virt_irq, &sun4u_irq_ack);
David S. Millere18e2a02006-06-20 01:23:32 -0700571}
572
573unsigned int build_irq(int inofixup, unsigned long iclr, unsigned long imap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700576 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700577 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int ino;
579
David S. Miller10951ee2006-02-13 18:22:57 -0800580 BUG_ON(tlb_type == hypervisor);
581
David S. Miller861fe902007-05-02 17:31:36 -0700582 ino = (upa_readq(imap) & (IMAP_IGN | IMAP_INO)) + inofixup;
David S. Miller088dd1f2005-07-04 13:24:38 -0700583 bucket = &ivector_table[ino];
David S. Miller42d5f992007-10-13 23:03:21 -0700584 virt_irq = bucket_get_virt_irq(__pa(bucket));
585 if (!virt_irq) {
David S. Millerbb74b732007-10-13 23:27:48 -0700586 virt_irq = virt_irq_alloc(__irq(bucket), 0, ino);
David S. Miller42d5f992007-10-13 23:03:21 -0700587 bucket_set_virt_irq(__pa(bucket), virt_irq);
588 set_irq_chip(virt_irq, &sun4u_irq);
David S. Miller088dd1f2005-07-04 13:24:38 -0700589 }
590
David S. Miller42d5f992007-10-13 23:03:21 -0700591 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800592 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700593 goto out;
594
595 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
596 if (unlikely(!data)) {
597 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
David S. Miller088dd1f2005-07-04 13:24:38 -0700598 prom_halt();
599 }
David S. Miller42d5f992007-10-13 23:03:21 -0700600 set_irq_chip_data(virt_irq, data);
David S. Miller088dd1f2005-07-04 13:24:38 -0700601
David S. Millere18e2a02006-06-20 01:23:32 -0700602 data->imap = imap;
603 data->iclr = iclr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
David S. Miller088dd1f2005-07-04 13:24:38 -0700605out:
David S. Miller42d5f992007-10-13 23:03:21 -0700606 return virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
David S. Miller4a907de2007-06-13 00:01:04 -0700609static unsigned int sun4v_build_common(unsigned long sysino,
610 struct irq_chip *chip)
David S. Millere3999572006-02-13 18:16:10 -0800611{
612 struct ino_bucket *bucket;
David S. Millere18e2a02006-06-20 01:23:32 -0700613 struct irq_handler_data *data;
David S. Miller42d5f992007-10-13 23:03:21 -0700614 unsigned int virt_irq;
David S. Millere18e2a02006-06-20 01:23:32 -0700615
616 BUG_ON(tlb_type != hypervisor);
David S. Millere3999572006-02-13 18:16:10 -0800617
David S. Millere3999572006-02-13 18:16:10 -0800618 bucket = &ivector_table[sysino];
David S. Miller42d5f992007-10-13 23:03:21 -0700619 virt_irq = bucket_get_virt_irq(__pa(bucket));
620 if (!virt_irq) {
David S. Millerbb74b732007-10-13 23:27:48 -0700621 virt_irq = virt_irq_alloc(__irq(bucket), 0, sysino);
David S. Miller42d5f992007-10-13 23:03:21 -0700622 bucket_set_virt_irq(__pa(bucket), virt_irq);
623 set_irq_chip(virt_irq, chip);
David S. Millere18e2a02006-06-20 01:23:32 -0700624 }
625
David S. Miller42d5f992007-10-13 23:03:21 -0700626 data = get_irq_chip_data(virt_irq);
David S. Miller68c92182007-01-29 12:12:28 -0800627 if (unlikely(data))
David S. Millere18e2a02006-06-20 01:23:32 -0700628 goto out;
629
630 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
631 if (unlikely(!data)) {
632 prom_printf("IRQ: kzalloc(irq_handler_data) failed.\n");
633 prom_halt();
634 }
David S. Miller42d5f992007-10-13 23:03:21 -0700635 set_irq_chip_data(virt_irq, data);
David S. Millere3999572006-02-13 18:16:10 -0800636
637 /* Catch accidental accesses to these things. IMAP/ICLR handling
638 * is done by hypervisor calls on sun4v platforms, not by direct
639 * register accesses.
640 */
David S. Millere18e2a02006-06-20 01:23:32 -0700641 data->imap = ~0UL;
642 data->iclr = ~0UL;
David S. Millere3999572006-02-13 18:16:10 -0800643
David S. Millere18e2a02006-06-20 01:23:32 -0700644out:
David S. Miller42d5f992007-10-13 23:03:21 -0700645 return virt_irq;
David S. Millere3999572006-02-13 18:16:10 -0800646}
647
David S. Miller4a907de2007-06-13 00:01:04 -0700648unsigned int sun4v_build_irq(u32 devhandle, unsigned int devino)
649{
650 unsigned long sysino = sun4v_devino_to_sysino(devhandle, devino);
651
652 return sun4v_build_common(sysino, &sun4v_irq);
653}
654
655unsigned int sun4v_build_virq(u32 devhandle, unsigned int devino)
656{
David S. Millerb80e6992007-10-13 21:51:37 -0700657 struct irq_handler_data *data;
658 struct ino_bucket *bucket;
659 unsigned long hv_err, cookie;
David S. Miller42d5f992007-10-13 23:03:21 -0700660 unsigned int virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700661
David S. Millerb80e6992007-10-13 21:51:37 -0700662 bucket = kzalloc(sizeof(struct ino_bucket), GFP_ATOMIC);
663 if (unlikely(!bucket))
664 return 0;
David S. Miller42d5f992007-10-13 23:03:21 -0700665 __flush_dcache_range((unsigned long) bucket,
666 ((unsigned long) bucket +
667 sizeof(struct ino_bucket)));
David S. Miller4a907de2007-06-13 00:01:04 -0700668
David S. Millerbb74b732007-10-13 23:27:48 -0700669 virt_irq = virt_irq_alloc(__irq(bucket), devhandle, devino);
David S. Miller42d5f992007-10-13 23:03:21 -0700670 bucket_set_virt_irq(__pa(bucket), virt_irq);
671 set_irq_chip(virt_irq, &sun4v_virq);
David S. Miller4a907de2007-06-13 00:01:04 -0700672
David S. Millerb80e6992007-10-13 21:51:37 -0700673 data = kzalloc(sizeof(struct irq_handler_data), GFP_ATOMIC);
674 if (unlikely(!data))
675 return 0;
676
David S. Miller42d5f992007-10-13 23:03:21 -0700677 set_irq_chip_data(virt_irq, data);
David S. Millerb80e6992007-10-13 21:51:37 -0700678
679 /* Catch accidental accesses to these things. IMAP/ICLR handling
680 * is done by hypervisor calls on sun4v platforms, not by direct
681 * register accesses.
682 */
683 data->imap = ~0UL;
684 data->iclr = ~0UL;
685
686 cookie = ~__pa(bucket);
687 hv_err = sun4v_vintr_set_cookie(devhandle, devino, cookie);
David S. Miller4a907de2007-06-13 00:01:04 -0700688 if (hv_err) {
689 prom_printf("IRQ: Fatal, cannot set cookie for [%x:%x] "
690 "err=%lu\n", devhandle, devino, hv_err);
691 prom_halt();
692 }
693
David S. Miller42d5f992007-10-13 23:03:21 -0700694 return virt_irq;
David S. Miller4a907de2007-06-13 00:01:04 -0700695}
696
David S. Millere18e2a02006-06-20 01:23:32 -0700697void ack_bad_irq(unsigned int virt_irq)
David S. Miller088dd1f2005-07-04 13:24:38 -0700698{
David S. Miller771823002007-10-13 23:41:28 -0700699 unsigned int ino = virt_to_real_irq_table[virt_irq].dev_ino;
David S. Miller088dd1f2005-07-04 13:24:38 -0700700
David S. Miller771823002007-10-13 23:41:28 -0700701 if (!ino)
702 ino = 0xdeadbeef;
David S. Miller088dd1f2005-07-04 13:24:38 -0700703
David S. Millere18e2a02006-06-20 01:23:32 -0700704 printk(KERN_CRIT "Unexpected IRQ from ino[%x] virt_irq[%u]\n",
705 ino, virt_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708void handler_irq(int irq, struct pt_regs *regs)
709{
David S. Millereb2d8d62007-10-13 21:42:46 -0700710 unsigned long pstate, bucket_pa;
Al Viro6d24c8d2006-10-08 08:23:28 -0400711 struct pt_regs *old_regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 clear_softint(1 << irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Al Viro6d24c8d2006-10-08 08:23:28 -0400715 old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 irq_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
David S. Millera650d382007-10-12 02:59:40 -0700718 /* Grab an atomic snapshot of the pending IVECs. */
719 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
720 "wrpr %0, %3, %%pstate\n\t"
721 "ldx [%2], %1\n\t"
722 "stx %%g0, [%2]\n\t"
723 "wrpr %0, 0x0, %%pstate\n\t"
David S. Millereb2d8d62007-10-13 21:42:46 -0700724 : "=&r" (pstate), "=&r" (bucket_pa)
725 : "r" (irq_work_pa(smp_processor_id())),
David S. Millera650d382007-10-12 02:59:40 -0700726 "i" (PSTATE_IE)
727 : "memory");
728
David S. Millereb2d8d62007-10-13 21:42:46 -0700729 while (bucket_pa) {
730 unsigned long next_pa;
731 unsigned int virt_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
David S. Miller42d5f992007-10-13 23:03:21 -0700733 next_pa = bucket_get_chain_pa(bucket_pa);
734 virt_irq = bucket_get_virt_irq(bucket_pa);
735 bucket_clear_chain_pa(bucket_pa);
David S. Millerfd0504c32006-06-20 01:20:00 -0700736
David S. Millereb2d8d62007-10-13 21:42:46 -0700737 __do_IRQ(virt_irq);
738
739 bucket_pa = next_pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
David S. Millere18e2a02006-06-20 01:23:32 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 irq_exit();
Al Viro6d24c8d2006-10-08 08:23:28 -0400743 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
David S. Millere02044092007-07-16 03:49:40 -0700746#ifdef CONFIG_HOTPLUG_CPU
747void fixup_irqs(void)
748{
749 unsigned int irq;
750
751 for (irq = 0; irq < NR_IRQS; irq++) {
752 unsigned long flags;
753
754 spin_lock_irqsave(&irq_desc[irq].lock, flags);
755 if (irq_desc[irq].action &&
756 !(irq_desc[irq].status & IRQ_PER_CPU)) {
757 if (irq_desc[irq].chip->set_affinity)
758 irq_desc[irq].chip->set_affinity(irq,
759 irq_desc[irq].affinity);
760 }
761 spin_unlock_irqrestore(&irq_desc[irq].lock, flags);
762 }
763}
764#endif
765
David S. Millercdd51862005-07-24 19:36:13 -0700766struct sun5_timer {
767 u64 count0;
768 u64 limit0;
769 u64 count1;
770 u64 limit1;
771};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
David S. Millercdd51862005-07-24 19:36:13 -0700773static struct sun5_timer *prom_timers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static u64 prom_limit0, prom_limit1;
775
776static void map_prom_timers(void)
777{
David S. Miller25c75812006-06-22 20:21:22 -0700778 struct device_node *dp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -0700779 const unsigned int *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* PROM timer node hangs out in the top level of device siblings... */
David S. Miller25c75812006-06-22 20:21:22 -0700782 dp = of_find_node_by_path("/");
783 dp = dp->child;
784 while (dp) {
785 if (!strcmp(dp->name, "counter-timer"))
786 break;
787 dp = dp->sibling;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Assume if node is not present, PROM uses different tick mechanism
791 * which we should not care about.
792 */
David S. Miller25c75812006-06-22 20:21:22 -0700793 if (!dp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 prom_timers = (struct sun5_timer *) 0;
795 return;
796 }
797
798 /* If PROM is really using this, it must be mapped by him. */
David S. Miller25c75812006-06-22 20:21:22 -0700799 addr = of_get_property(dp, "address", NULL);
800 if (!addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 prom_printf("PROM does not have timer mapped, trying to continue.\n");
802 prom_timers = (struct sun5_timer *) 0;
803 return;
804 }
805 prom_timers = (struct sun5_timer *) ((unsigned long)addr[0]);
806}
807
808static void kill_prom_timer(void)
809{
810 if (!prom_timers)
811 return;
812
813 /* Save them away for later. */
814 prom_limit0 = prom_timers->limit0;
815 prom_limit1 = prom_timers->limit1;
816
817 /* Just as in sun4c/sun4m PROM uses timer which ticks at IRQ 14.
818 * We turn both off here just to be paranoid.
819 */
820 prom_timers->limit0 = 0;
821 prom_timers->limit1 = 0;
822
823 /* Wheee, eat the interrupt packet too... */
824 __asm__ __volatile__(
825" mov 0x40, %%g2\n"
826" ldxa [%%g0] %0, %%g1\n"
827" ldxa [%%g2] %1, %%g1\n"
828" stxa %%g0, [%%g0] %0\n"
829" membar #Sync\n"
830 : /* no outputs */
831 : "i" (ASI_INTR_RECEIVE), "i" (ASI_INTR_R)
832 : "g1", "g2");
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835void init_irqwork_curcpu(void)
836{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int cpu = hard_smp_processor_id();
838
David S. Millereb2d8d62007-10-13 21:42:46 -0700839 trap_block[cpu].irq_worklist_pa = 0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
David S. Miller5cbc3072007-05-25 15:49:59 -0700842/* Please be very careful with register_one_mondo() and
843 * sun4v_register_mondo_queues().
844 *
845 * On SMP this gets invoked from the CPU trampoline before
846 * the cpu has fully taken over the trap table from OBP,
847 * and it's kernel stack + %g6 thread register state is
848 * not fully cooked yet.
849 *
850 * Therefore you cannot make any OBP calls, not even prom_printf,
851 * from these two routines.
852 */
853static void __cpuinit register_one_mondo(unsigned long paddr, unsigned long type, unsigned long qmask)
David S. Millerac29c112006-02-08 00:08:23 -0800854{
David S. Miller5cbc3072007-05-25 15:49:59 -0700855 unsigned long num_entries = (qmask + 1) / 64;
David S. Miller94f87622006-02-16 14:26:53 -0800856 unsigned long status;
David S. Millerac29c112006-02-08 00:08:23 -0800857
David S. Miller94f87622006-02-16 14:26:53 -0800858 status = sun4v_cpu_qconf(type, paddr, num_entries);
859 if (status != HV_EOK) {
860 prom_printf("SUN4V: sun4v_cpu_qconf(%lu:%lx:%lu) failed, "
861 "err %lu\n", type, paddr, num_entries, status);
David S. Millerac29c112006-02-08 00:08:23 -0800862 prom_halt();
863 }
864}
865
David S. Millerb434e712007-08-08 17:32:33 -0700866void __cpuinit sun4v_register_mondo_queues(int this_cpu)
David S. Miller5b0c05722006-02-08 02:53:50 -0800867{
David S. Millerb5a37e92006-02-11 23:07:13 -0800868 struct trap_per_cpu *tb = &trap_block[this_cpu];
869
David S. Miller5cbc3072007-05-25 15:49:59 -0700870 register_one_mondo(tb->cpu_mondo_pa, HV_CPU_QUEUE_CPU_MONDO,
871 tb->cpu_mondo_qmask);
872 register_one_mondo(tb->dev_mondo_pa, HV_CPU_QUEUE_DEVICE_MONDO,
873 tb->dev_mondo_qmask);
874 register_one_mondo(tb->resum_mondo_pa, HV_CPU_QUEUE_RES_ERROR,
875 tb->resum_qmask);
876 register_one_mondo(tb->nonresum_mondo_pa, HV_CPU_QUEUE_NONRES_ERROR,
877 tb->nonresum_qmask);
David S. Millerb5a37e92006-02-11 23:07:13 -0800878}
879
David S. Millerb434e712007-08-08 17:32:33 -0700880static void __init alloc_one_mondo(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800881{
David S. Miller5cbc3072007-05-25 15:49:59 -0700882 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700883 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5cbc3072007-05-25 15:49:59 -0700884 if (!p) {
David S. Millerb5a37e92006-02-11 23:07:13 -0800885 prom_printf("SUN4V: Error, cannot allocate mondo queue.\n");
886 prom_halt();
887 }
888
David S. Miller5cbc3072007-05-25 15:49:59 -0700889 *pa_ptr = __pa(p);
David S. Millerb5a37e92006-02-11 23:07:13 -0800890}
891
David S. Millerb434e712007-08-08 17:32:33 -0700892static void __init alloc_one_kbuf(unsigned long *pa_ptr, unsigned long qmask)
David S. Millerb5a37e92006-02-11 23:07:13 -0800893{
David S. Miller5cbc3072007-05-25 15:49:59 -0700894 unsigned long size = PAGE_ALIGN(qmask + 1);
David S. Millerb434e712007-08-08 17:32:33 -0700895 void *p = __alloc_bootmem_low(size, size, 0);
David S. Miller5b0c05722006-02-08 02:53:50 -0800896
David S. Miller5cbc3072007-05-25 15:49:59 -0700897 if (!p) {
David S. Miller5b0c05722006-02-08 02:53:50 -0800898 prom_printf("SUN4V: Error, cannot allocate kbuf page.\n");
899 prom_halt();
900 }
901
David S. Miller5cbc3072007-05-25 15:49:59 -0700902 *pa_ptr = __pa(p);
David S. Miller5b0c05722006-02-08 02:53:50 -0800903}
904
David S. Millerb434e712007-08-08 17:32:33 -0700905static void __init init_cpu_send_mondo_info(struct trap_per_cpu *tb)
David S. Miller1d2f1f92006-02-08 16:41:20 -0800906{
907#ifdef CONFIG_SMP
David S. Millerb5a37e92006-02-11 23:07:13 -0800908 void *page;
David S. Miller1d2f1f92006-02-08 16:41:20 -0800909
910 BUILD_BUG_ON((NR_CPUS * sizeof(u16)) > (PAGE_SIZE - 64));
911
David S. Millerb434e712007-08-08 17:32:33 -0700912 page = alloc_bootmem_low_pages(PAGE_SIZE);
David S. Miller1d2f1f92006-02-08 16:41:20 -0800913 if (!page) {
914 prom_printf("SUN4V: Error, cannot allocate cpu mondo page.\n");
915 prom_halt();
916 }
917
918 tb->cpu_mondo_block_pa = __pa(page);
919 tb->cpu_list_pa = __pa(page + 64);
920#endif
921}
922
David S. Millerb434e712007-08-08 17:32:33 -0700923/* Allocate mondo and error queues for all possible cpus. */
924static void __init sun4v_init_mondo_queues(void)
David S. Millerac29c112006-02-08 00:08:23 -0800925{
David S. Millerb434e712007-08-08 17:32:33 -0700926 int cpu;
David S. Millerac29c112006-02-08 00:08:23 -0800927
David S. Millerb434e712007-08-08 17:32:33 -0700928 for_each_possible_cpu(cpu) {
929 struct trap_per_cpu *tb = &trap_block[cpu];
David S. Miller1d2f1f92006-02-08 16:41:20 -0800930
David S. Millerb434e712007-08-08 17:32:33 -0700931 alloc_one_mondo(&tb->cpu_mondo_pa, tb->cpu_mondo_qmask);
932 alloc_one_mondo(&tb->dev_mondo_pa, tb->dev_mondo_qmask);
933 alloc_one_mondo(&tb->resum_mondo_pa, tb->resum_qmask);
934 alloc_one_kbuf(&tb->resum_kernel_buf_pa, tb->resum_qmask);
935 alloc_one_mondo(&tb->nonresum_mondo_pa, tb->nonresum_qmask);
936 alloc_one_kbuf(&tb->nonresum_kernel_buf_pa,
937 tb->nonresum_qmask);
938
939 init_cpu_send_mondo_info(tb);
David S. Miller72aff532006-02-17 01:29:17 -0800940 }
David S. Miller1d2f1f92006-02-08 16:41:20 -0800941
David S. Millerb434e712007-08-08 17:32:33 -0700942 /* Load up the boot cpu's entries. */
943 sun4v_register_mondo_queues(hard_smp_processor_id());
David S. Millerac29c112006-02-08 00:08:23 -0800944}
945
David S. Millere18e2a02006-06-20 01:23:32 -0700946static struct irqaction timer_irq_action = {
947 .name = "timer",
948};
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/* Only invoked on boot processor. */
951void __init init_IRQ(void)
952{
David S. Miller10397e42007-10-13 21:43:31 -0700953 unsigned long size;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 map_prom_timers();
956 kill_prom_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
David S. Miller10397e42007-10-13 21:43:31 -0700958 size = sizeof(struct ino_bucket) * NUM_IVECS;
959 ivector_table = alloc_bootmem_low(size);
960 if (!ivector_table) {
961 prom_printf("Fatal error, cannot allocate ivector_table\n");
962 prom_halt();
963 }
David S. Miller42d5f992007-10-13 23:03:21 -0700964 __flush_dcache_range((unsigned long) ivector_table,
965 ((unsigned long) ivector_table) + size);
David S. Miller10397e42007-10-13 21:43:31 -0700966
967 ivector_table_pa = __pa(ivector_table);
David S. Millereb2d8d62007-10-13 21:42:46 -0700968
David S. Millerac29c112006-02-08 00:08:23 -0800969 if (tlb_type == hypervisor)
David S. Millerb434e712007-08-08 17:32:33 -0700970 sun4v_init_mondo_queues();
David S. Millerac29c112006-02-08 00:08:23 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* We need to clear any IRQ's pending in the soft interrupt
973 * registers, a spurious one could be left around from the
974 * PROM timer which we just disabled.
975 */
976 clear_softint(get_softint());
977
978 /* Now that ivector table is initialized, it is safe
979 * to receive IRQ vector traps. We will normally take
980 * one or two right now, in case some device PROM used
981 * to boot us wants to speak to us. We just ignore them.
982 */
983 __asm__ __volatile__("rdpr %%pstate, %%g1\n\t"
984 "or %%g1, %0, %%g1\n\t"
985 "wrpr %%g1, 0x0, %%pstate"
986 : /* No outputs */
987 : "i" (PSTATE_IE)
988 : "g1");
David S. Millere18e2a02006-06-20 01:23:32 -0700989
990 irq_desc[0].action = &timer_irq_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}