blob: c98e3a12846876149214788ad58879032080d7e2 [file] [log] [blame]
David Gibson007e8f52005-10-28 15:35:50 +10001/*
2 * arch/powerpc/platforms/pseries/xics.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright 2000 IBM Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/threads.h>
14#include <linux/kernel.h>
15#include <linux/irq.h>
16#include <linux/smp.h>
17#include <linux/interrupt.h>
18#include <linux/signal.h>
19#include <linux/init.h>
20#include <linux/gfp.h>
21#include <linux/radix-tree.h>
22#include <linux/cpu.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100023
Michael Ellerman57cfb812006-03-21 20:45:59 +110024#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/prom.h>
26#include <asm/io.h>
27#include <asm/pgtable.h>
28#include <asm/smp.h>
29#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/hvcall.h>
31#include <asm/machdep.h>
Paul Mackerras22277182005-10-28 11:47:17 +100032#include <asm/i8259.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
David Gibson007e8f52005-10-28 15:35:50 +100034#include "xics.h"
Anton Blanchardb9377ff2006-07-19 08:01:28 +100035#include "plpar_wrappers.h"
David Gibson007e8f52005-10-28 15:35:50 +100036
Milton Miller0641cc92008-10-10 01:56:30 +000037static struct irq_host *xics_host;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define XICS_IPI 2
40#define XICS_IRQ_SPURIOUS 0
41
42/* Want a priority other than 0. Various HW issues require this. */
43#define DEFAULT_PRIORITY 5
44
David Gibson007e8f52005-10-28 15:35:50 +100045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Mark IPIs as higher priority so we can take them inside interrupts that
Thomas Gleixner67144652006-07-01 19:29:22 -070047 * arent marked IRQF_DISABLED
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49#define IPI_PRIORITY 4
50
Milton Miller0641cc92008-10-10 01:56:30 +000051static unsigned int default_server = 0xFF;
52static unsigned int default_distrib_server = 0;
53static unsigned int interrupt_server_size = 8;
54
55/* RTAS service tokens */
56static int ibm_get_xive;
57static int ibm_set_xive;
58static int ibm_int_on;
59static int ibm_int_off;
60
61
62/* Direct hardware low level accessors */
63
64/* The part of the interrupt presentation layer that we care about */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065struct xics_ipl {
66 union {
67 u32 word;
68 u8 bytes[4];
69 } xirr_poll;
70 union {
71 u32 word;
72 u8 bytes[4];
73 } xirr;
74 u32 dummy;
75 union {
76 u32 word;
77 u8 bytes[4];
78 } qirr;
79};
80
81static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
82
Milton Millerd7cf0ed2007-12-14 15:52:09 +110083static inline unsigned int direct_xirr_info_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Milton Millerd7cf0ed2007-12-14 15:52:09 +110085 int cpu = smp_processor_id();
86
87 return in_be32(&xics_per_cpu[cpu]->xirr.word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Milton Millerd7cf0ed2007-12-14 15:52:09 +110090static inline void direct_xirr_info_set(int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Milton Millerd7cf0ed2007-12-14 15:52:09 +110092 int cpu = smp_processor_id();
93
94 out_be32(&xics_per_cpu[cpu]->xirr.word, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Milton Millerd7cf0ed2007-12-14 15:52:09 +110097static inline void direct_cppr_info(u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Milton Millerd7cf0ed2007-12-14 15:52:09 +110099 int cpu = smp_processor_id();
100
101 out_8(&xics_per_cpu[cpu]->xirr.bytes[0], value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000104static inline void direct_qirr_info(int n_cpu, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000110/* LPAR low level accessors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100112static inline unsigned int lpar_xirr_info_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 unsigned long lpar_rc;
David Gibson007e8f52005-10-28 15:35:50 +1000115 unsigned long return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 lpar_rc = plpar_xirr(&return_value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200118 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000119 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000120 return (unsigned int)return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100123static inline void lpar_xirr_info_set(int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned long lpar_rc;
126 unsigned long val64 = value & 0xffffffff;
127
128 lpar_rc = plpar_eoi(val64);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200129 if (lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
David Gibson007e8f52005-10-28 15:35:50 +1000131 val64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100134static inline void lpar_cppr_info(u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long lpar_rc;
137
138 lpar_rc = plpar_cppr(value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200139 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000140 panic("bad return code cppr - rc = %lx\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000143static inline void lpar_qirr_info(int n_cpu , u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned long lpar_rc;
146
147 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200148 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000149 panic("bad return code qirr - rc = %lx\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Milton Miller0641cc92008-10-10 01:56:30 +0000153/* Interface to generic irq subsystem */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155#ifdef CONFIG_SMP
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000156static int get_irq_server(unsigned int virq, unsigned int strict_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000158 int server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* For the moment only implement delivery to all cpus or one cpu */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000160 cpumask_t cpumask = irq_desc[virq].affinity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 cpumask_t tmp = CPU_MASK_NONE;
162
163 if (!distribute_irqs)
164 return default_server;
165
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000166 if (!cpus_equal(cpumask, CPU_MASK_ALL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 cpus_and(tmp, cpu_online_map, cpumask);
168
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000169 server = first_cpu(tmp);
170
171 if (server < NR_CPUS)
172 return get_hard_smp_processor_id(server);
173
174 if (strict_check)
175 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000178 if (cpus_equal(cpu_online_map, cpu_present_map))
179 return default_distrib_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000181 return default_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183#else
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000184static int get_irq_server(unsigned int virq, unsigned int strict_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 return default_server;
187}
188#endif
189
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000190static void xics_unmask_irq(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned int irq;
193 int call_status;
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000194 int server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000196 pr_debug("xics: unmask virq %d\n", virq);
197
198 irq = (unsigned int)irq_map[virq].hwirq;
199 pr_debug(" -> map to hwirq 0x%x\n", irq);
200 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return;
202
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000203 server = get_irq_server(virq, 0);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
206 DEFAULT_PRIORITY);
207 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000208 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
209 "returned %d\n", irq, call_status);
210 printk("set_xive %x, server %x\n", ibm_set_xive, server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return;
212 }
213
214 /* Now unmask the interrupt (often a no-op) */
215 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
216 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000217 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
218 "returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return;
220 }
221}
222
Milton Miller0641cc92008-10-10 01:56:30 +0000223static unsigned int xics_startup(unsigned int virq)
224{
225 /* unmask it */
226 xics_unmask_irq(virq);
227 return 0;
228}
229
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000230static void xics_mask_real_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int call_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (irq == XICS_IPI)
235 return;
236
237 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
238 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000239 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
240 "ibm_int_off returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return;
242 }
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Have to set XIVE to 0xff to be able to remove a slot */
Michal Ostrowski673aeb72006-12-20 07:29:40 -0600245 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq,
246 default_server, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000248 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
249 " returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return;
251 }
252}
253
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000254static void xics_mask_irq(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 unsigned int irq;
257
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000258 pr_debug("xics: mask virq %d\n", virq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000260 irq = (unsigned int)irq_map[virq].hwirq;
261 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
262 return;
263 xics_mask_real_irq(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000264}
265
Milton Miller0641cc92008-10-10 01:56:30 +0000266static void xics_mask_unknown_vec(unsigned int vec)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000267{
Milton Miller0641cc92008-10-10 01:56:30 +0000268 printk(KERN_ERR "Interrupt %u (real) is invalid, disabling it.\n", vec);
269 xics_mask_real_irq(vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Milton Miller8767e9b2008-10-10 01:56:23 +0000272static inline unsigned int xics_xirr_vector(unsigned int xirr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Milton Miller8767e9b2008-10-10 01:56:23 +0000274 /*
275 * The top byte is the old cppr, to be restored on EOI.
276 * The remaining 24 bits are the vector.
277 */
278 return xirr & 0x00ffffff;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Olaf Hering35a84c22006-10-07 22:08:26 +1000281static unsigned int xics_get_irq_direct(void)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000282{
Milton Miller8767e9b2008-10-10 01:56:23 +0000283 unsigned int xirr = direct_xirr_info_get();
284 unsigned int vec = xics_xirr_vector(xirr);
285 unsigned int irq;
286
287 if (vec == XICS_IRQ_SPURIOUS)
288 return NO_IRQ;
289
290 irq = irq_radix_revmap_lookup(xics_host, vec);
291 if (likely(irq != NO_IRQ))
292 return irq;
293
294 /* We don't have a linux mapping, so have rtas mask it. */
295 xics_mask_unknown_vec(vec);
296
297 /* We might learn about it later, so EOI it */
298 direct_xirr_info_set(xirr);
299 return NO_IRQ;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000300}
301
Olaf Hering35a84c22006-10-07 22:08:26 +1000302static unsigned int xics_get_irq_lpar(void)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000303{
Milton Miller8767e9b2008-10-10 01:56:23 +0000304 unsigned int xirr = lpar_xirr_info_get();
305 unsigned int vec = xics_xirr_vector(xirr);
306 unsigned int irq;
307
308 if (vec == XICS_IRQ_SPURIOUS)
309 return NO_IRQ;
310
311 irq = irq_radix_revmap_lookup(xics_host, vec);
312 if (likely(irq != NO_IRQ))
313 return irq;
314
315 /* We don't have a linux mapping, so have RTAS mask it. */
316 xics_mask_unknown_vec(vec);
317
318 /* We might learn about it later, so EOI it */
319 lpar_xirr_info_set(xirr);
320 return NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Milton Miller0641cc92008-10-10 01:56:30 +0000323static void xics_eoi_direct(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Milton Miller0641cc92008-10-10 01:56:30 +0000325 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000327 iosync();
Milton Miller0641cc92008-10-10 01:56:30 +0000328 direct_xirr_info_set((0xff << 24) | irq);
329}
330
331static void xics_eoi_lpar(unsigned int virq)
332{
333 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
334
335 iosync();
336 lpar_xirr_info_set((0xff << 24) | irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000337}
338
339static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
340{
341 unsigned int irq;
342 int status;
343 int xics_status[2];
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000344 int irq_server;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000345
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000346 irq = (unsigned int)irq_map[virq].hwirq;
347 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000348 return;
349
350 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
351
352 if (status) {
353 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
354 "returns %d\n", irq, status);
355 return;
356 }
357
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000358 /*
359 * For the moment only implement delivery to all cpus or one cpu.
360 * Get current irq_server for the given irq
361 */
Anton Blancharde48395f2007-10-01 07:45:55 +1000362 irq_server = get_irq_server(virq, 1);
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000363 if (irq_server == -1) {
364 char cpulist[128];
365 cpumask_scnprintf(cpulist, sizeof(cpulist), cpumask);
366 printk(KERN_WARNING "xics_set_affinity: No online cpus in "
367 "the mask %s for irq %d\n", cpulist, virq);
368 return;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000369 }
370
371 status = rtas_call(ibm_set_xive, 3, 1, NULL,
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000372 irq, irq_server, xics_status[1]);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000373
374 if (status) {
375 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
376 "returns %d\n", irq, status);
377 return;
378 }
379}
380
381static struct irq_chip xics_pic_direct = {
382 .typename = " XICS ",
383 .startup = xics_startup,
384 .mask = xics_mask_irq,
385 .unmask = xics_unmask_irq,
386 .eoi = xics_eoi_direct,
387 .set_affinity = xics_set_affinity
388};
389
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000390static struct irq_chip xics_pic_lpar = {
391 .typename = " XICS ",
392 .startup = xics_startup,
393 .mask = xics_mask_irq,
394 .unmask = xics_unmask_irq,
395 .eoi = xics_eoi_lpar,
396 .set_affinity = xics_set_affinity
397};
398
Milton Miller0641cc92008-10-10 01:56:30 +0000399
400/* Interface to arch irq controller subsystem layer */
401
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100402/* Points to the irq_chip we're actually using */
403static struct irq_chip *xics_irq_chip;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000404
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000405static int xics_host_match(struct irq_host *h, struct device_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000407 /* IBM machines have interrupt parents of various funky types for things
408 * like vdevices, events, etc... The trick we use here is to match
409 * everything here except the legacy 8259 which is compatible "chrp,iic"
Paul Mackerras6c80a212005-05-06 16:28:56 +1000410 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000411 return !of_device_is_compatible(node, "chrp,iic");
Paul Mackerras6c80a212005-05-06 16:28:56 +1000412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100414static int xics_host_map(struct irq_host *h, unsigned int virq,
415 irq_hw_number_t hw)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000416{
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100417 pr_debug("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000418
Sebastien Dugue967e0122008-09-04 22:37:07 +1000419 /* Insert the interrupt mapping into the radix tree for fast lookup */
420 irq_radix_revmap_insert(xics_host, virq, hw);
421
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000422 get_irq_desc(virq)->status |= IRQ_LEVEL;
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100423 set_irq_chip_and_handler(virq, xics_irq_chip, handle_fasteoi_irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000424 return 0;
425}
426
427static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
428 u32 *intspec, unsigned int intsize,
429 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
430
431{
432 /* Current xics implementation translates everything
433 * to level. It is not technically right for MSIs but this
434 * is irrelevant at this point. We might get smarter in the future
435 */
436 *out_hwirq = intspec[0];
437 *out_flags = IRQ_TYPE_LEVEL_LOW;
438
439 return 0;
440}
441
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100442static struct irq_host_ops xics_host_ops = {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000443 .match = xics_host_match,
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100444 .map = xics_host_map,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000445 .xlate = xics_host_xlate,
446};
447
448static void __init xics_init_host(void)
449{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000450 if (firmware_has_feature(FW_FEATURE_LPAR))
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100451 xics_irq_chip = &xics_pic_lpar;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000452 else
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100453 xics_irq_chip = &xics_pic_direct;
454
455 xics_host = irq_alloc_host(NULL, IRQ_HOST_MAP_TREE, 0, &xics_host_ops,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000456 XICS_IRQ_SPURIOUS);
457 BUG_ON(xics_host == NULL);
458 irq_set_default_host(xics_host);
459}
460
Milton Miller0641cc92008-10-10 01:56:30 +0000461
462/* Inter-processor interrupt support */
463
464#ifdef CONFIG_SMP
465/*
466 * XICS only has a single IPI, so encode the messages per CPU
467 */
468struct xics_ipi_struct {
469 unsigned long value;
470 } ____cacheline_aligned;
471
472static struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
473
474static inline void smp_xics_do_message(int cpu, int msg)
475{
476 set_bit(msg, &xics_ipi_message[cpu].value);
477 mb();
478 if (firmware_has_feature(FW_FEATURE_LPAR))
479 lpar_qirr_info(cpu, IPI_PRIORITY);
480 else
481 direct_qirr_info(cpu, IPI_PRIORITY);
482}
483
484void smp_xics_message_pass(int target, int msg)
485{
486 unsigned int i;
487
488 if (target < NR_CPUS) {
489 smp_xics_do_message(target, msg);
490 } else {
491 for_each_online_cpu(i) {
492 if (target == MSG_ALL_BUT_SELF
493 && i == smp_processor_id())
494 continue;
495 smp_xics_do_message(i, msg);
496 }
497 }
498}
499
500static irqreturn_t xics_ipi_dispatch(int cpu)
501{
502 WARN_ON(cpu_is_offline(cpu));
503
504 while (xics_ipi_message[cpu].value) {
505 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
506 &xics_ipi_message[cpu].value)) {
507 mb();
508 smp_message_recv(PPC_MSG_CALL_FUNCTION);
509 }
510 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
511 &xics_ipi_message[cpu].value)) {
512 mb();
513 smp_message_recv(PPC_MSG_RESCHEDULE);
514 }
515 if (test_and_clear_bit(PPC_MSG_CALL_FUNC_SINGLE,
516 &xics_ipi_message[cpu].value)) {
517 mb();
518 smp_message_recv(PPC_MSG_CALL_FUNC_SINGLE);
519 }
520#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
521 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
522 &xics_ipi_message[cpu].value)) {
523 mb();
524 smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
525 }
526#endif
527 }
528 return IRQ_HANDLED;
529}
530
531static irqreturn_t xics_ipi_action_direct(int irq, void *dev_id)
532{
533 int cpu = smp_processor_id();
534
535 direct_qirr_info(cpu, 0xff);
536
537 return xics_ipi_dispatch(cpu);
538}
539
540static irqreturn_t xics_ipi_action_lpar(int irq, void *dev_id)
541{
542 int cpu = smp_processor_id();
543
544 lpar_qirr_info(cpu, 0xff);
545
546 return xics_ipi_dispatch(cpu);
547}
548
549static void xics_request_ipi(void)
550{
551 unsigned int ipi;
552 int rc;
553
554 ipi = irq_create_mapping(xics_host, XICS_IPI);
555 BUG_ON(ipi == NO_IRQ);
556
557 /*
558 * IPIs are marked IRQF_DISABLED as they must run with irqs
559 * disabled
560 */
561 set_irq_handler(ipi, handle_percpu_irq);
562 if (firmware_has_feature(FW_FEATURE_LPAR))
563 rc = request_irq(ipi, xics_ipi_action_lpar, IRQF_DISABLED,
564 "IPI", NULL);
565 else
566 rc = request_irq(ipi, xics_ipi_action_direct, IRQF_DISABLED,
567 "IPI", NULL);
568 BUG_ON(rc);
569}
570
571int __init smp_xics_probe(void)
572{
573 xics_request_ipi();
574
575 return cpus_weight(cpu_possible_map);
576}
577
578#endif /* CONFIG_SMP */
579
580
581/* Initialization */
582
583static void xics_update_irq_servers(void)
584{
585 int i, j;
586 struct device_node *np;
587 u32 ilen;
588 const u32 *ireg, *isize;
589 u32 hcpuid;
590
591 /* Find the server numbers for the boot cpu. */
592 np = of_get_cpu_node(boot_cpuid, NULL);
593 BUG_ON(!np);
594
595 ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
596 if (!ireg) {
597 of_node_put(np);
598 return;
599 }
600
601 i = ilen / sizeof(int);
602 hcpuid = get_hard_smp_processor_id(boot_cpuid);
603
604 /* Global interrupt distribution server is specified in the last
605 * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
606 * entry fom this property for current boot cpu id and use it as
607 * default distribution server
608 */
609 for (j = 0; j < i; j += 2) {
610 if (ireg[j] == hcpuid) {
611 default_server = hcpuid;
612 default_distrib_server = ireg[j+1];
613
614 isize = of_get_property(np,
615 "ibm,interrupt-server#-size", NULL);
616 if (isize)
617 interrupt_server_size = *isize;
618 }
619 }
620
621 of_node_put(np);
622}
623
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000624static void __init xics_map_one_cpu(int hw_id, unsigned long addr,
625 unsigned long size)
626{
627#ifdef CONFIG_SMP
628 int i;
629
630 /* This may look gross but it's good enough for now, we don't quite
631 * have a hard -> linux processor id matching.
632 */
633 for_each_possible_cpu(i) {
634 if (!cpu_present(i))
635 continue;
636 if (hw_id == get_hard_smp_processor_id(i)) {
637 xics_per_cpu[i] = ioremap(addr, size);
638 return;
639 }
640 }
641#else
642 if (hw_id != 0)
643 return;
644 xics_per_cpu[0] = ioremap(addr, size);
645#endif /* CONFIG_SMP */
646}
647
648static void __init xics_init_one_node(struct device_node *np,
649 unsigned int *indx)
650{
651 unsigned int ilen;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000652 const u32 *ireg;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000653
654 /* This code does the theorically broken assumption that the interrupt
655 * server numbers are the same as the hard CPU numbers.
656 * This happens to be the case so far but we are playing with fire...
657 * should be fixed one of these days. -BenH.
658 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000659 ireg = of_get_property(np, "ibm,interrupt-server-ranges", NULL);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000660
661 /* Do that ever happen ? we'll know soon enough... but even good'old
662 * f80 does have that property ..
663 */
664 WARN_ON(ireg == NULL);
665 if (ireg) {
666 /*
667 * set node starting index for this node
668 */
669 *indx = *ireg;
670 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000671 ireg = of_get_property(np, "reg", &ilen);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000672 if (!ireg)
673 panic("xics_init_IRQ: can't find interrupt reg property");
674
675 while (ilen >= (4 * sizeof(u32))) {
676 unsigned long addr, size;
677
678 /* XXX Use proper OF parsing code here !!! */
679 addr = (unsigned long)*ireg++ << 32;
680 ilen -= sizeof(u32);
681 addr |= *ireg++;
682 ilen -= sizeof(u32);
683 size = (unsigned long)*ireg++ << 32;
684 ilen -= sizeof(u32);
685 size |= *ireg++;
686 ilen -= sizeof(u32);
687 xics_map_one_cpu(*indx, addr, size);
688 (*indx)++;
689 }
690}
691
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000692void __init xics_init_IRQ(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct device_node *np;
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100695 u32 indx = 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000696 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 ppc64_boot_msg(0x20, "XICS Init");
699
700 ibm_get_xive = rtas_token("ibm,get-xive");
701 ibm_set_xive = rtas_token("ibm,set-xive");
702 ibm_int_on = rtas_token("ibm,int-on");
703 ibm_int_off = rtas_token("ibm,int-off");
704
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000705 for_each_node_by_type(np, "PowerPC-External-Interrupt-Presentation") {
706 found = 1;
707 if (firmware_has_feature(FW_FEATURE_LPAR))
708 break;
709 xics_init_one_node(np, &indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000711 if (found == 0)
712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100714 xics_update_irq_servers();
Milton Miller302905a2008-10-10 01:56:28 +0000715 xics_init_host();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000717 if (firmware_has_feature(FW_FEATURE_LPAR))
718 ppc_md.get_irq = xics_get_irq_lpar;
719 else
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000720 ppc_md.get_irq = xics_get_irq_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Paul Mackerras6c80a212005-05-06 16:28:56 +1000722 xics_setup_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 ppc64_boot_msg(0x21, "XICS Done");
725}
726
Milton Miller0641cc92008-10-10 01:56:30 +0000727/* Cpu startup, shutdown, and hotplug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Milton Miller0641cc92008-10-10 01:56:30 +0000729static void xics_set_cpu_priority(unsigned char cppr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Milton Miller0641cc92008-10-10 01:56:30 +0000731 if (firmware_has_feature(FW_FEATURE_LPAR))
732 lpar_cppr_info(cppr);
733 else
734 direct_cppr_info(cppr);
735 iosync();
736}
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000737
Milton Miller0641cc92008-10-10 01:56:30 +0000738
739void xics_setup_cpu(void)
740{
741 xics_set_cpu_priority(0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Thomas Gleixner67144652006-07-01 19:29:22 -0700743 /*
Milton Miller0641cc92008-10-10 01:56:30 +0000744 * Put the calling processor into the GIQ. This is really only
745 * necessary from a secondary thread as the OF start-cpu interface
746 * performs this function for us on primary threads.
747 *
748 * XXX: undo of teardown on kexec needs this too, as may hotplug
Thomas Gleixner67144652006-07-01 19:29:22 -0700749 */
Milton Miller0641cc92008-10-10 01:56:30 +0000750 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
751 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
Milton Millerd13f7202008-10-10 01:56:29 +0000753
Al Virof10095c2008-03-29 03:10:38 +0000754void xics_teardown_cpu(void)
R Sharadafce0d572005-06-25 14:58:10 -0700755{
756 int cpu = smp_processor_id();
R Sharadafce0d572005-06-25 14:58:10 -0700757
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100758 xics_set_cpu_priority(0);
Haren Myneni81bbbe92006-04-05 21:10:18 -0600759
760 /*
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700761 * Clear IPI
762 */
763 if (firmware_has_feature(FW_FEATURE_LPAR))
764 lpar_qirr_info(cpu, 0xff);
765 else
766 direct_qirr_info(cpu, 0xff);
Nathan Fontenotc3e85062008-02-07 07:37:31 +1100767}
768
769void xics_kexec_teardown_cpu(int secondary)
770{
771 unsigned int ipi;
772 struct irq_desc *desc;
773
774 xics_teardown_cpu();
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700775
776 /*
Nathan Fontenotc3e85062008-02-07 07:37:31 +1100777 * we need to EOI the IPI
Haren Myneni81bbbe92006-04-05 21:10:18 -0600778 *
779 * probably need to check all the other interrupts too
780 * should we be flagging idle loop instead?
781 * or creating some task to be scheduled?
782 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000783
784 ipi = irq_find_mapping(xics_host, XICS_IPI);
785 if (ipi == XICS_IRQ_SPURIOUS)
786 return;
787 desc = get_irq_desc(ipi);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000788 if (desc->chip && desc->chip->eoi)
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700789 desc->chip->eoi(ipi);
Haren Myneni81bbbe92006-04-05 21:10:18 -0600790
R Sharadafce0d572005-06-25 14:58:10 -0700791 /*
Paul Mackerras6d22d852005-08-04 12:53:37 -0700792 * Some machines need to have at least one cpu in the GIQ,
793 * so leave the master cpu in the group.
R Sharadafce0d572005-06-25 14:58:10 -0700794 */
Haren Myneni81bbbe92006-04-05 21:10:18 -0600795 if (secondary)
Haren Myneni81b73dd2006-07-27 14:29:00 -0700796 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000797 (1UL << interrupt_server_size) - 1 -
798 default_distrib_server, 0);
R Sharadafce0d572005-06-25 14:58:10 -0700799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#ifdef CONFIG_HOTPLUG_CPU
802
803/* Interrupts are disabled. */
804void xics_migrate_irqs_away(void)
805{
806 int status;
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100807 int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
808 unsigned int irq, virq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Milton Miller302905a2008-10-10 01:56:28 +0000810 /* If we used to be the default server, move to the new "boot_cpuid" */
811 if (hw_cpu == default_server)
812 xics_update_irq_servers();
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* Reject any interrupt that was queued to us... */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100815 xics_set_cpu_priority(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* remove ourselves from the global interrupt queue */
Haren Myneni81b73dd2006-07-27 14:29:00 -0700818 status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
820 WARN_ON(status < 0);
821
822 /* Allow IPIs again... */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100823 xics_set_cpu_priority(DEFAULT_PRIORITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 for_each_irq(virq) {
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000826 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int xics_status[2];
828 unsigned long flags;
829
830 /* We cant set affinity on ISA interrupts */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000831 if (virq < NUM_ISA_INTERRUPTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000833 if (irq_map[virq].host != xics_host)
834 continue;
835 irq = (unsigned int)irq_map[virq].hwirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* We need to get IPIs still. */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000837 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000839 desc = get_irq_desc(virq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* We only need to migrate enabled IRQS */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700842 if (desc == NULL || desc->chip == NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 || desc->action == NULL
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700844 || desc->chip->set_affinity == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 continue;
846
847 spin_lock_irqsave(&desc->lock, flags);
848
849 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
850 if (status) {
Anton Blanchard26370322005-09-12 13:12:11 +1000851 printk(KERN_ERR "migrate_irqs_away: irq=%u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 "ibm,get-xive returns %d\n",
853 virq, status);
854 goto unlock;
855 }
856
857 /*
858 * We only support delivery to all cpus or to one cpu.
859 * The irq has to be migrated only in the single cpu
860 * case.
861 */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100862 if (xics_status[0] != hw_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto unlock;
864
Anton Blanchard26370322005-09-12 13:12:11 +1000865 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 virq, cpu);
867
868 /* Reset affinity to all cpus */
Nathan Fontenota52572d2008-02-07 07:37:26 +1100869 irq_desc[virq].affinity = CPU_MASK_ALL;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700870 desc->chip->set_affinity(virq, CPU_MASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871unlock:
872 spin_unlock_irqrestore(&desc->lock, flags);
873 }
874}
875#endif