blob: 0fc830f576f5532870157af81cdaf4c65625b67d [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
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/threads.h>
15#include <linux/kernel.h>
16#include <linux/irq.h>
17#include <linux/smp.h>
18#include <linux/interrupt.h>
19#include <linux/signal.h>
20#include <linux/init.h>
21#include <linux/gfp.h>
22#include <linux/radix-tree.h>
23#include <linux/cpu.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100024
Michael Ellerman57cfb812006-03-21 20:45:59 +110025#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/prom.h>
27#include <asm/io.h>
28#include <asm/pgtable.h>
29#include <asm/smp.h>
30#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/hvcall.h>
32#include <asm/machdep.h>
Paul Mackerras22277182005-10-28 11:47:17 +100033#include <asm/i8259.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David Gibson007e8f52005-10-28 15:35:50 +100035#include "xics.h"
Anton Blanchardb9377ff2006-07-19 08:01:28 +100036#include "plpar_wrappers.h"
David Gibson007e8f52005-10-28 15:35:50 +100037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define XICS_IPI 2
39#define XICS_IRQ_SPURIOUS 0
40
41/* Want a priority other than 0. Various HW issues require this. */
42#define DEFAULT_PRIORITY 5
43
David Gibson007e8f52005-10-28 15:35:50 +100044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Mark IPIs as higher priority so we can take them inside interrupts that
Thomas Gleixner67144652006-07-01 19:29:22 -070046 * arent marked IRQF_DISABLED
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
48#define IPI_PRIORITY 4
49
50struct xics_ipl {
51 union {
52 u32 word;
53 u8 bytes[4];
54 } xirr_poll;
55 union {
56 u32 word;
57 u8 bytes[4];
58 } xirr;
59 u32 dummy;
60 union {
61 u32 word;
62 u8 bytes[4];
63 } qirr;
64};
65
66static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int default_server = 0xFF;
Anton Blanchard26370322005-09-12 13:12:11 +100069static unsigned int default_distrib_server = 0;
70static unsigned int interrupt_server_size = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100072static struct irq_host *xics_host;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * XICS only has a single IPI, so encode the messages per CPU
76 */
77struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
78
79/* RTAS service tokens */
Anton Blanchard26370322005-09-12 13:12:11 +100080static int ibm_get_xive;
81static int ibm_set_xive;
82static int ibm_int_on;
83static int ibm_int_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100085
86/* Direct HW low level accessors */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88
Milton Millerd7cf0ed2007-12-14 15:52:09 +110089static inline unsigned int direct_xirr_info_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Milton Millerd7cf0ed2007-12-14 15:52:09 +110091 int cpu = smp_processor_id();
92
93 return in_be32(&xics_per_cpu[cpu]->xirr.word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Milton Millerd7cf0ed2007-12-14 15:52:09 +110096static inline void direct_xirr_info_set(int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Milton Millerd7cf0ed2007-12-14 15:52:09 +110098 int cpu = smp_processor_id();
99
100 out_be32(&xics_per_cpu[cpu]->xirr.word, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100103static inline void direct_cppr_info(u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100105 int cpu = smp_processor_id();
106
107 out_8(&xics_per_cpu[cpu]->xirr.bytes[0], value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000110static inline void direct_qirr_info(int n_cpu, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000116/* LPAR low level accessors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100119static inline unsigned int lpar_xirr_info_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long lpar_rc;
David Gibson007e8f52005-10-28 15:35:50 +1000122 unsigned long return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 lpar_rc = plpar_xirr(&return_value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200125 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000126 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000127 return (unsigned int)return_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100130static inline void lpar_xirr_info_set(int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned long lpar_rc;
133 unsigned long val64 = value & 0xffffffff;
134
135 lpar_rc = plpar_eoi(val64);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200136 if (lpar_rc != H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
David Gibson007e8f52005-10-28 15:35:50 +1000138 val64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100141static inline void lpar_cppr_info(u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 unsigned long lpar_rc;
144
145 lpar_rc = plpar_cppr(value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200146 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000147 panic("bad return code cppr - rc = %lx\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000150static inline void lpar_qirr_info(int n_cpu , u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 unsigned long lpar_rc;
153
154 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200155 if (lpar_rc != H_SUCCESS)
David Gibson007e8f52005-10-28 15:35:50 +1000156 panic("bad return code qirr - rc = %lx\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000160/* High level handlers and init code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100162static void xics_update_irq_servers(void)
163{
164 int i, j;
165 struct device_node *np;
166 u32 ilen;
167 const u32 *ireg, *isize;
168 u32 hcpuid;
169
170 /* Find the server numbers for the boot cpu. */
171 np = of_get_cpu_node(boot_cpuid, NULL);
172 BUG_ON(!np);
173
174 ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
175 if (!ireg) {
176 of_node_put(np);
177 return;
178 }
179
180 i = ilen / sizeof(int);
181 hcpuid = get_hard_smp_processor_id(boot_cpuid);
182
183 /* Global interrupt distribution server is specified in the last
184 * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
185 * entry fom this property for current boot cpu id and use it as
186 * default distribution server
187 */
188 for (j = 0; j < i; j += 2) {
189 if (ireg[j] == hcpuid) {
190 default_server = hcpuid;
191 default_distrib_server = ireg[j+1];
192
193 isize = of_get_property(np,
194 "ibm,interrupt-server#-size", NULL);
195 if (isize)
196 interrupt_server_size = *isize;
197 }
198 }
199
200 of_node_put(np);
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#ifdef CONFIG_SMP
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000204static int get_irq_server(unsigned int virq, unsigned int strict_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000206 int server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* For the moment only implement delivery to all cpus or one cpu */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000208 cpumask_t cpumask = irq_desc[virq].affinity;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 cpumask_t tmp = CPU_MASK_NONE;
210
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100211 if (! cpu_isset(default_server, cpu_online_map))
212 xics_update_irq_servers();
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!distribute_irqs)
215 return default_server;
216
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000217 if (!cpus_equal(cpumask, CPU_MASK_ALL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 cpus_and(tmp, cpu_online_map, cpumask);
219
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000220 server = first_cpu(tmp);
221
222 if (server < NR_CPUS)
223 return get_hard_smp_processor_id(server);
224
225 if (strict_check)
226 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000229 if (cpus_equal(cpu_online_map, cpu_present_map))
230 return default_distrib_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000232 return default_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234#else
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000235static int get_irq_server(unsigned int virq, unsigned int strict_check)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 return default_server;
238}
239#endif
240
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000241
242static void xics_unmask_irq(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 unsigned int irq;
245 int call_status;
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000246 int server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000248 pr_debug("xics: unmask virq %d\n", virq);
249
250 irq = (unsigned int)irq_map[virq].hwirq;
251 pr_debug(" -> map to hwirq 0x%x\n", irq);
252 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return;
254
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000255 server = get_irq_server(virq, 0);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
258 DEFAULT_PRIORITY);
259 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000260 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
261 "returned %d\n", irq, call_status);
262 printk("set_xive %x, server %x\n", ibm_set_xive, server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
264 }
265
266 /* Now unmask the interrupt (often a no-op) */
267 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
268 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000269 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
270 "returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return;
272 }
273}
274
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000275static void xics_mask_real_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int call_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (irq == XICS_IPI)
280 return;
281
282 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
283 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000284 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
285 "ibm_int_off returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Have to set XIVE to 0xff to be able to remove a slot */
Michal Ostrowski673aeb72006-12-20 07:29:40 -0600290 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq,
291 default_server, 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (call_status != 0) {
Anton Blanchard26370322005-09-12 13:12:11 +1000293 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
294 " returned %d\n", irq, call_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return;
296 }
297}
298
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000299static void xics_mask_irq(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 unsigned int irq;
302
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000303 pr_debug("xics: mask virq %d\n", virq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000305 irq = (unsigned int)irq_map[virq].hwirq;
306 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
307 return;
308 xics_mask_real_irq(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000309}
310
311static unsigned int xics_startup(unsigned int virq)
312{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000313 unsigned int irq;
314
315 /* force a reverse mapping of the interrupt so it gets in the cache */
316 irq = (unsigned int)irq_map[virq].hwirq;
317 irq_radix_revmap(xics_host, irq);
318
319 /* unmask it */
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000320 xics_unmask_irq(virq);
321 return 0;
322}
323
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000324static void xics_eoi_direct(unsigned int virq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000326 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 iosync();
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100329 direct_xirr_info_set((0xff << 24) | irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000330}
331
332
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000333static void xics_eoi_lpar(unsigned int virq)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000334{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000335 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000336
337 iosync();
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100338 lpar_xirr_info_set((0xff << 24) | irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000341static inline unsigned int xics_remap_irq(unsigned int vec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000343 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 vec &= 0x00ffffff;
346
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000347 if (vec == XICS_IRQ_SPURIOUS)
348 return NO_IRQ;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000349 irq = irq_radix_revmap(xics_host, vec);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000350 if (likely(irq != NO_IRQ))
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000351 return irq;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000352
353 printk(KERN_ERR "Interrupt %u (real) is invalid,"
354 " disabling it.\n", vec);
355 xics_mask_real_irq(vec);
356 return NO_IRQ;
357}
358
Olaf Hering35a84c22006-10-07 22:08:26 +1000359static unsigned int xics_get_irq_direct(void)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000360{
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100361 return xics_remap_irq(direct_xirr_info_get());
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000362}
363
Olaf Hering35a84c22006-10-07 22:08:26 +1000364static unsigned int xics_get_irq_lpar(void)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000365{
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100366 return xics_remap_irq(lpar_xirr_info_get());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369#ifdef CONFIG_SMP
370
David Howells7d12e782006-10-05 14:55:46 +0100371static irqreturn_t xics_ipi_dispatch(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 WARN_ON(cpu_is_offline(cpu));
374
375 while (xics_ipi_message[cpu].value) {
376 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
377 &xics_ipi_message[cpu].value)) {
378 mb();
David Howells7d12e782006-10-05 14:55:46 +0100379 smp_message_recv(PPC_MSG_CALL_FUNCTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
382 &xics_ipi_message[cpu].value)) {
383 mb();
David Howells7d12e782006-10-05 14:55:46 +0100384 smp_message_recv(PPC_MSG_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jens Axboeb7d7a242008-06-26 11:22:13 +0200386 if (test_and_clear_bit(PPC_MSG_CALL_FUNC_SINGLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 &xics_ipi_message[cpu].value)) {
388 mb();
Jens Axboeb7d7a242008-06-26 11:22:13 +0200389 smp_message_recv(PPC_MSG_CALL_FUNC_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Michael Ellermancc532912005-12-04 18:39:43 +1100391#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
393 &xics_ipi_message[cpu].value)) {
394 mb();
David Howells7d12e782006-10-05 14:55:46 +0100395 smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397#endif
398 }
399 return IRQ_HANDLED;
400}
401
David Howells7d12e782006-10-05 14:55:46 +0100402static irqreturn_t xics_ipi_action_direct(int irq, void *dev_id)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000403{
404 int cpu = smp_processor_id();
405
406 direct_qirr_info(cpu, 0xff);
407
David Howells7d12e782006-10-05 14:55:46 +0100408 return xics_ipi_dispatch(cpu);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000409}
410
David Howells7d12e782006-10-05 14:55:46 +0100411static irqreturn_t xics_ipi_action_lpar(int irq, void *dev_id)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000412{
413 int cpu = smp_processor_id();
414
415 lpar_qirr_info(cpu, 0xff);
416
David Howells7d12e782006-10-05 14:55:46 +0100417 return xics_ipi_dispatch(cpu);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420void xics_cause_IPI(int cpu)
421{
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000422 if (firmware_has_feature(FW_FEATURE_LPAR))
423 lpar_qirr_info(cpu, IPI_PRIORITY);
424 else
425 direct_qirr_info(cpu, IPI_PRIORITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000427
Paul Mackerras6c80a212005-05-06 16:28:56 +1000428#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100430static void xics_set_cpu_priority(unsigned char cppr)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000431{
432 if (firmware_has_feature(FW_FEATURE_LPAR))
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100433 lpar_cppr_info(cppr);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000434 else
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100435 direct_cppr_info(cppr);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000436 iosync();
437}
438
439static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
440{
441 unsigned int irq;
442 int status;
443 int xics_status[2];
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000444 int irq_server;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000445
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000446 irq = (unsigned int)irq_map[virq].hwirq;
447 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000448 return;
449
450 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
451
452 if (status) {
453 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
454 "returns %d\n", irq, status);
455 return;
456 }
457
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000458 /*
459 * For the moment only implement delivery to all cpus or one cpu.
460 * Get current irq_server for the given irq
461 */
Anton Blancharde48395f2007-10-01 07:45:55 +1000462 irq_server = get_irq_server(virq, 1);
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000463 if (irq_server == -1) {
464 char cpulist[128];
465 cpumask_scnprintf(cpulist, sizeof(cpulist), cpumask);
466 printk(KERN_WARNING "xics_set_affinity: No online cpus in "
467 "the mask %s for irq %d\n", cpulist, virq);
468 return;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000469 }
470
471 status = rtas_call(ibm_set_xive, 3, 1, NULL,
Mohan Kumar M7ccb4a62007-06-13 00:51:57 +1000472 irq, irq_server, xics_status[1]);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000473
474 if (status) {
475 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
476 "returns %d\n", irq, status);
477 return;
478 }
479}
480
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000481void xics_setup_cpu(void)
482{
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100483 xics_set_cpu_priority(0xff);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000484
485 /*
486 * Put the calling processor into the GIQ. This is really only
487 * necessary from a secondary thread as the OF start-cpu interface
488 * performs this function for us on primary threads.
489 *
490 * XXX: undo of teardown on kexec needs this too, as may hotplug
491 */
Haren Myneni81b73dd2006-07-27 14:29:00 -0700492 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000493 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
494}
495
496
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000497static struct irq_chip xics_pic_direct = {
498 .typename = " XICS ",
499 .startup = xics_startup,
500 .mask = xics_mask_irq,
501 .unmask = xics_unmask_irq,
502 .eoi = xics_eoi_direct,
503 .set_affinity = xics_set_affinity
504};
505
506
507static struct irq_chip xics_pic_lpar = {
508 .typename = " XICS ",
509 .startup = xics_startup,
510 .mask = xics_mask_irq,
511 .unmask = xics_unmask_irq,
512 .eoi = xics_eoi_lpar,
513 .set_affinity = xics_set_affinity
514};
515
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100516/* Points to the irq_chip we're actually using */
517static struct irq_chip *xics_irq_chip;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000518
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000519static int xics_host_match(struct irq_host *h, struct device_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000521 /* IBM machines have interrupt parents of various funky types for things
522 * like vdevices, events, etc... The trick we use here is to match
523 * everything here except the legacy 8259 which is compatible "chrp,iic"
Paul Mackerras6c80a212005-05-06 16:28:56 +1000524 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000525 return !of_device_is_compatible(node, "chrp,iic");
Paul Mackerras6c80a212005-05-06 16:28:56 +1000526}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100528static int xics_host_map(struct irq_host *h, unsigned int virq,
529 irq_hw_number_t hw)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000530{
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100531 pr_debug("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000532
533 get_irq_desc(virq)->status |= IRQ_LEVEL;
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100534 set_irq_chip_and_handler(virq, xics_irq_chip, handle_fasteoi_irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000535 return 0;
536}
537
538static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
539 u32 *intspec, unsigned int intsize,
540 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
541
542{
543 /* Current xics implementation translates everything
544 * to level. It is not technically right for MSIs but this
545 * is irrelevant at this point. We might get smarter in the future
546 */
547 *out_hwirq = intspec[0];
548 *out_flags = IRQ_TYPE_LEVEL_LOW;
549
550 return 0;
551}
552
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100553static struct irq_host_ops xics_host_ops = {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000554 .match = xics_host_match,
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100555 .map = xics_host_map,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000556 .xlate = xics_host_xlate,
557};
558
559static void __init xics_init_host(void)
560{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000561 if (firmware_has_feature(FW_FEATURE_LPAR))
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100562 xics_irq_chip = &xics_pic_lpar;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000563 else
Michael Ellerman1af9fa82008-04-01 17:42:27 +1100564 xics_irq_chip = &xics_pic_direct;
565
566 xics_host = irq_alloc_host(NULL, IRQ_HOST_MAP_TREE, 0, &xics_host_ops,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000567 XICS_IRQ_SPURIOUS);
568 BUG_ON(xics_host == NULL);
569 irq_set_default_host(xics_host);
570}
571
572static void __init xics_map_one_cpu(int hw_id, unsigned long addr,
573 unsigned long size)
574{
575#ifdef CONFIG_SMP
576 int i;
577
578 /* This may look gross but it's good enough for now, we don't quite
579 * have a hard -> linux processor id matching.
580 */
581 for_each_possible_cpu(i) {
582 if (!cpu_present(i))
583 continue;
584 if (hw_id == get_hard_smp_processor_id(i)) {
585 xics_per_cpu[i] = ioremap(addr, size);
586 return;
587 }
588 }
589#else
590 if (hw_id != 0)
591 return;
592 xics_per_cpu[0] = ioremap(addr, size);
593#endif /* CONFIG_SMP */
594}
595
596static void __init xics_init_one_node(struct device_node *np,
597 unsigned int *indx)
598{
599 unsigned int ilen;
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000600 const u32 *ireg;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000601
602 /* This code does the theorically broken assumption that the interrupt
603 * server numbers are the same as the hard CPU numbers.
604 * This happens to be the case so far but we are playing with fire...
605 * should be fixed one of these days. -BenH.
606 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000607 ireg = of_get_property(np, "ibm,interrupt-server-ranges", NULL);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000608
609 /* Do that ever happen ? we'll know soon enough... but even good'old
610 * f80 does have that property ..
611 */
612 WARN_ON(ireg == NULL);
613 if (ireg) {
614 /*
615 * set node starting index for this node
616 */
617 *indx = *ireg;
618 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000619 ireg = of_get_property(np, "reg", &ilen);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000620 if (!ireg)
621 panic("xics_init_IRQ: can't find interrupt reg property");
622
623 while (ilen >= (4 * sizeof(u32))) {
624 unsigned long addr, size;
625
626 /* XXX Use proper OF parsing code here !!! */
627 addr = (unsigned long)*ireg++ << 32;
628 ilen -= sizeof(u32);
629 addr |= *ireg++;
630 ilen -= sizeof(u32);
631 size = (unsigned long)*ireg++ << 32;
632 ilen -= sizeof(u32);
633 size |= *ireg++;
634 ilen -= sizeof(u32);
635 xics_map_one_cpu(*indx, addr, size);
636 (*indx)++;
637 }
638}
639
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000640void __init xics_init_IRQ(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct device_node *np;
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100643 u32 indx = 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000644 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 ppc64_boot_msg(0x20, "XICS Init");
647
648 ibm_get_xive = rtas_token("ibm,get-xive");
649 ibm_set_xive = rtas_token("ibm,set-xive");
650 ibm_int_on = rtas_token("ibm,int-on");
651 ibm_int_off = rtas_token("ibm,int-off");
652
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000653 for_each_node_by_type(np, "PowerPC-External-Interrupt-Presentation") {
654 found = 1;
655 if (firmware_has_feature(FW_FEATURE_LPAR))
656 break;
657 xics_init_one_node(np, &indx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000659 if (found == 0)
660 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000662 xics_init_host();
Nathan Fontenotde0723d2008-02-07 07:37:40 +1100663 xics_update_irq_servers();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000665 if (firmware_has_feature(FW_FEATURE_LPAR))
666 ppc_md.get_irq = xics_get_irq_lpar;
667 else
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000668 ppc_md.get_irq = xics_get_irq_direct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Paul Mackerras6c80a212005-05-06 16:28:56 +1000670 xics_setup_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 ppc64_boot_msg(0x21, "XICS Done");
673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676#ifdef CONFIG_SMP
677void xics_request_IPIs(void)
678{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000679 unsigned int ipi;
Michael Neuling66b30922007-05-29 17:01:52 +1000680 int rc;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000681
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700682 ipi = irq_create_mapping(xics_host, XICS_IPI);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000683 BUG_ON(ipi == NO_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Thomas Gleixner67144652006-07-01 19:29:22 -0700685 /*
686 * IPIs are marked IRQF_DISABLED as they must run with irqs
687 * disabled
688 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000689 set_irq_handler(ipi, handle_percpu_irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000690 if (firmware_has_feature(FW_FEATURE_LPAR))
Michael Neuling66b30922007-05-29 17:01:52 +1000691 rc = request_irq(ipi, xics_ipi_action_lpar, IRQF_DISABLED,
692 "IPI", NULL);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000693 else
Michael Neuling66b30922007-05-29 17:01:52 +1000694 rc = request_irq(ipi, xics_ipi_action_direct, IRQF_DISABLED,
695 "IPI", NULL);
696 BUG_ON(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000698#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Al Virof10095c2008-03-29 03:10:38 +0000700void xics_teardown_cpu(void)
R Sharadafce0d572005-06-25 14:58:10 -0700701{
702 int cpu = smp_processor_id();
R Sharadafce0d572005-06-25 14:58:10 -0700703
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100704 xics_set_cpu_priority(0);
Haren Myneni81bbbe92006-04-05 21:10:18 -0600705
706 /*
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700707 * Clear IPI
708 */
709 if (firmware_has_feature(FW_FEATURE_LPAR))
710 lpar_qirr_info(cpu, 0xff);
711 else
712 direct_qirr_info(cpu, 0xff);
Nathan Fontenotc3e85062008-02-07 07:37:31 +1100713}
714
715void xics_kexec_teardown_cpu(int secondary)
716{
717 unsigned int ipi;
718 struct irq_desc *desc;
719
720 xics_teardown_cpu();
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700721
722 /*
Nathan Fontenotc3e85062008-02-07 07:37:31 +1100723 * we need to EOI the IPI
Haren Myneni81bbbe92006-04-05 21:10:18 -0600724 *
725 * probably need to check all the other interrupts too
726 * should we be flagging idle loop instead?
727 * or creating some task to be scheduled?
728 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000729
730 ipi = irq_find_mapping(xics_host, XICS_IPI);
731 if (ipi == XICS_IRQ_SPURIOUS)
732 return;
733 desc = get_irq_desc(ipi);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000734 if (desc->chip && desc->chip->eoi)
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700735 desc->chip->eoi(ipi);
Haren Myneni81bbbe92006-04-05 21:10:18 -0600736
R Sharadafce0d572005-06-25 14:58:10 -0700737 /*
Paul Mackerras6d22d852005-08-04 12:53:37 -0700738 * Some machines need to have at least one cpu in the GIQ,
739 * so leave the master cpu in the group.
R Sharadafce0d572005-06-25 14:58:10 -0700740 */
Haren Myneni81bbbe92006-04-05 21:10:18 -0600741 if (secondary)
Haren Myneni81b73dd2006-07-27 14:29:00 -0700742 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000743 (1UL << interrupt_server_size) - 1 -
744 default_distrib_server, 0);
R Sharadafce0d572005-06-25 14:58:10 -0700745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#ifdef CONFIG_HOTPLUG_CPU
748
749/* Interrupts are disabled. */
750void xics_migrate_irqs_away(void)
751{
752 int status;
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100753 int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
754 unsigned int irq, virq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* Reject any interrupt that was queued to us... */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100757 xics_set_cpu_priority(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* remove ourselves from the global interrupt queue */
Haren Myneni81b73dd2006-07-27 14:29:00 -0700760 status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
762 WARN_ON(status < 0);
763
764 /* Allow IPIs again... */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100765 xics_set_cpu_priority(DEFAULT_PRIORITY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 for_each_irq(virq) {
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000768 struct irq_desc *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int xics_status[2];
770 unsigned long flags;
771
772 /* We cant set affinity on ISA interrupts */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000773 if (virq < NUM_ISA_INTERRUPTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000775 if (irq_map[virq].host != xics_host)
776 continue;
777 irq = (unsigned int)irq_map[virq].hwirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* We need to get IPIs still. */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000779 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 continue;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000781 desc = get_irq_desc(virq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* We only need to migrate enabled IRQS */
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700784 if (desc == NULL || desc->chip == NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 || desc->action == NULL
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700786 || desc->chip->set_affinity == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 continue;
788
789 spin_lock_irqsave(&desc->lock, flags);
790
791 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
792 if (status) {
Anton Blanchard26370322005-09-12 13:12:11 +1000793 printk(KERN_ERR "migrate_irqs_away: irq=%u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 "ibm,get-xive returns %d\n",
795 virq, status);
796 goto unlock;
797 }
798
799 /*
800 * We only support delivery to all cpus or to one cpu.
801 * The irq has to be migrated only in the single cpu
802 * case.
803 */
Milton Millerd7cf0ed2007-12-14 15:52:09 +1100804 if (xics_status[0] != hw_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto unlock;
806
Anton Blanchard26370322005-09-12 13:12:11 +1000807 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 virq, cpu);
809
810 /* Reset affinity to all cpus */
Nathan Fontenota52572d2008-02-07 07:37:26 +1100811 irq_desc[virq].affinity = CPU_MASK_ALL;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700812 desc->chip->set_affinity(virq, CPU_MASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813unlock:
814 spin_unlock_irqrestore(&desc->lock, flags);
815 }
816}
817#endif