blob: 97936f547f1956d9249668100adc49ee0caf1967 [file] [log] [blame]
Arnd Bergmanncebf5892005-06-23 09:43:43 +10001/*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -05002 * Cell Internal Interrupt Controller
Arnd Bergmanncebf5892005-06-23 09:43:43 +10003 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Arnd Bergmanncebf5892005-06-23 09:43:43 +100023#include <linux/interrupt.h>
24#include <linux/irq.h>
Arnd Bergmann2fb9d202006-01-05 14:05:29 +000025#include <linux/module.h>
Arnd Bergmanncebf5892005-06-23 09:43:43 +100026#include <linux/percpu.h>
27#include <linux/types.h>
28
29#include <asm/io.h>
30#include <asm/pgtable.h>
31#include <asm/prom.h>
32#include <asm/ptrace.h>
33
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050034#include "interrupt.h"
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020035#include "cbe_regs.h"
Arnd Bergmanncebf5892005-06-23 09:43:43 +100036
37struct iic {
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020038 struct cbe_iic_thread_regs __iomem *regs;
Arnd Bergmann2fb9d202006-01-05 14:05:29 +000039 u8 target_id;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100040 u8 eoi_stack[16];
41 int eoi_ptr;
Arnd Bergmanncebf5892005-06-23 09:43:43 +100042};
43
44static DEFINE_PER_CPU(struct iic, iic);
45
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100046static void iic_mask(unsigned int irq)
47{
48}
49
50static void iic_unmask(unsigned int irq)
51{
52}
53
54static void iic_eoi(unsigned int irq)
Arnd Bergmanncebf5892005-06-23 09:43:43 +100055{
Arnd Bergmann55364082006-03-23 00:00:07 +010056 struct iic *iic = &__get_cpu_var(iic);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100057 out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
58 BUG_ON(iic->eoi_ptr < 0);
Arnd Bergmanncebf5892005-06-23 09:43:43 +100059}
60
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100061static struct irq_chip iic_chip = {
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050062 .typename = " CELL-IIC ",
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100063 .mask = iic_mask,
64 .unmask = iic_unmask,
65 .eoi = iic_eoi,
Arnd Bergmanncebf5892005-06-23 09:43:43 +100066};
67
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100068/* XXX All of this has to be reworked completely. We need to assign a real
69 * interrupt numbers to the external interrupts and remove all the hard coded
70 * interrupt maps (rely on the device-tree whenever possible).
71 *
72 * Basically, my scheme is to define the "pendings" bits to be the HW interrupt
73 * number (ignoring the data and flags here). That means we can sort-of split
74 * external sources based on priority, and we can use request_irq() on pretty
75 * much anything.
76 *
77 * For spider or axon, they have their own interrupt space. spider will just have
78 * local "hardward" interrupts 0...xx * node stride. The node stride is not
79 * necessary (separate interrupt chips will have separate HW number space), but
80 * will allow to be compatible with existing device-trees.
81 *
82 * All of thise little world will get a standard remapping scheme to map those HW
83 * numbers into the linux flat irq number space.
84*/
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020085static int iic_external_get_irq(struct cbe_iic_pending_bits pending)
Arnd Bergmanncebf5892005-06-23 09:43:43 +100086{
87 int irq;
88 unsigned char node, unit;
89
90 node = pending.source >> 4;
91 unit = pending.source & 0xf;
92 irq = -1;
93
94 /*
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050095 * This mapping is specific to the Cell Broadband
Arnd Bergmanncebf5892005-06-23 09:43:43 +100096 * Engine. We might need to get the numbers
97 * from the device tree to support future CPUs.
98 */
99 switch (unit) {
100 case 0x00:
101 case 0x0b:
102 /*
103 * One of these units can be connected
104 * to an external interrupt controller.
105 */
arnd@arndb.deb40feec2006-06-19 20:33:17 +0200106 if (pending.class != 2)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000107 break;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000108 /* TODO: We might want to silently ignore cascade interrupts
109 * when no cascade handler exist yet
110 */
111 irq = IIC_EXT_CASCADE + node * IIC_NODE_STRIDE;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000112 break;
113 case 0x01 ... 0x04:
114 case 0x07 ... 0x0a:
115 /*
116 * These units are connected to the SPEs
117 */
118 if (pending.class > 2)
119 break;
120 irq = IIC_SPE_OFFSET
121 + pending.class * IIC_CLASS_STRIDE
122 + node * IIC_NODE_STRIDE
123 + unit;
124 break;
125 }
126 if (irq == -1)
127 printk(KERN_WARNING "Unexpected interrupt class %02x, "
128 "source %02x, prio %02x, cpu %02x\n", pending.class,
129 pending.source, pending.prio, smp_processor_id());
130 return irq;
131}
132
133/* Get an IRQ number from the pending state register of the IIC */
134int iic_get_irq(struct pt_regs *regs)
135{
136 struct iic *iic;
137 int irq;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200138 struct cbe_iic_pending_bits pending;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000139
140 iic = &__get_cpu_var(iic);
141 *(unsigned long *) &pending =
142 in_be64((unsigned long __iomem *) &iic->regs->pending_destr);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000143 iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
144 BUG_ON(iic->eoi_ptr > 15);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000145
146 irq = -1;
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200147 if (pending.flags & CBE_IIC_IRQ_VALID) {
148 if (pending.flags & CBE_IIC_IRQ_IPI) {
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000149 irq = IIC_IPI_OFFSET + (pending.prio >> 4);
150/*
151 if (irq > 0x80)
152 printk(KERN_WARNING "Unexpected IPI prio %02x"
153 "on CPU %02x\n", pending.prio,
154 smp_processor_id());
155*/
156 } else {
157 irq = iic_external_get_irq(pending);
158 }
159 }
160 return irq;
161}
162
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100163/* hardcoded part to be compatible with older firmware */
164
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000165static int __init setup_iic_hardcoded(void)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000166{
167 struct device_node *np;
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100168 int nodeid, cpu;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000169 unsigned long regs;
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100170 struct iic *iic;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000171
Andrew Mortonfb1bb342006-06-25 05:46:43 -0700172 for_each_possible_cpu(cpu) {
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100173 iic = &per_cpu(iic, cpu);
174 nodeid = cpu/2;
175
176 for (np = of_find_node_by_type(NULL, "cpu");
177 np;
178 np = of_find_node_by_type(np, "cpu")) {
179 if (nodeid == *(int *)get_property(np, "node-id", NULL))
180 break;
181 }
182
183 if (!np) {
184 printk(KERN_WARNING "IIC: CPU %d not found\n", cpu);
185 iic->regs = NULL;
186 iic->target_id = 0xff;
187 return -ENODEV;
188 }
189
190 regs = *(long *)get_property(np, "iic", NULL);
191
192 /* hack until we have decided on the devtree info */
193 regs += 0x400;
194 if (cpu & 1)
195 regs += 0x20;
196
197 printk(KERN_INFO "IIC for CPU %d at %lx\n", cpu, regs);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200198 iic->regs = ioremap(regs, sizeof(struct cbe_iic_thread_regs));
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100199 iic->target_id = (nodeid << 4) + ((cpu & 1) ? 0xf : 0xe);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000200 iic->eoi_stack[0] = 0xff;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000201 }
202
Arnd Bergmann2fb9d202006-01-05 14:05:29 +0000203 return 0;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000204}
205
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000206static int __init setup_iic(void)
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100207{
208 struct device_node *dn;
209 unsigned long *regs;
210 char *compatible;
211 unsigned *np, found = 0;
212 struct iic *iic = NULL;
213
214 for (dn = NULL; (dn = of_find_node_by_name(dn, "interrupt-controller"));) {
215 compatible = (char *)get_property(dn, "compatible", NULL);
216
217 if (!compatible) {
218 printk(KERN_WARNING "no compatible property found !\n");
219 continue;
220 }
221
222 if (strstr(compatible, "IBM,CBEA-Internal-Interrupt-Controller"))
223 regs = (unsigned long *)get_property(dn,"reg", NULL);
224 else
225 continue;
226
227 if (!regs)
228 printk(KERN_WARNING "IIC: no reg property\n");
229
230 np = (unsigned int *)get_property(dn, "ibm,interrupt-server-ranges", NULL);
231
232 if (!np) {
233 printk(KERN_WARNING "IIC: CPU association not found\n");
234 iic->regs = NULL;
235 iic->target_id = 0xff;
236 return -ENODEV;
237 }
238
239 iic = &per_cpu(iic, np[0]);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200240 iic->regs = ioremap(regs[0], sizeof(struct cbe_iic_thread_regs));
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100241 iic->target_id = ((np[0] & 2) << 3) + ((np[0] & 1) ? 0xf : 0xe);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000242 iic->eoi_stack[0] = 0xff;
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100243 printk("IIC for CPU %d at %lx mapped to %p\n", np[0], regs[0], iic->regs);
244
245 iic = &per_cpu(iic, np[1]);
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +0200246 iic->regs = ioremap(regs[2], sizeof(struct cbe_iic_thread_regs));
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100247 iic->target_id = ((np[1] & 2) << 3) + ((np[1] & 1) ? 0xf : 0xe);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000248 iic->eoi_stack[0] = 0xff;
249
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100250 printk("IIC for CPU %d at %lx mapped to %p\n", np[1], regs[2], iic->regs);
251
252 found++;
253 }
254
255 if (found)
256 return 0;
257 else
258 return -ENODEV;
259}
260
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000261#ifdef CONFIG_SMP
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200262
263/* Use the highest interrupt priorities for IPI */
264static inline int iic_ipi_to_irq(int ipi)
265{
266 return IIC_IPI_OFFSET + IIC_NUM_IPIS - 1 - ipi;
267}
268
269static inline int iic_irq_to_ipi(int irq)
270{
271 return IIC_NUM_IPIS - 1 - (irq - IIC_IPI_OFFSET);
272}
273
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000274void iic_setup_cpu(void)
275{
276 out_be64(&__get_cpu_var(iic).regs->prio, 0xff);
277}
278
279void iic_cause_IPI(int cpu, int mesg)
280{
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200281 out_be64(&per_cpu(iic, cpu).regs->generate, (IIC_NUM_IPIS - 1 - mesg) << 4);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000282}
283
Arnd Bergmann2fb9d202006-01-05 14:05:29 +0000284u8 iic_get_target_id(int cpu)
285{
286 return per_cpu(iic, cpu).target_id;
287}
288EXPORT_SYMBOL_GPL(iic_get_target_id);
289
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000290static irqreturn_t iic_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
291{
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200292 smp_message_recv(iic_irq_to_ipi(irq), regs);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000293 return IRQ_HANDLED;
294}
295
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200296static void iic_request_ipi(int ipi, const char *name)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000297{
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200298 int irq;
299
300 irq = iic_ipi_to_irq(ipi);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000301
Thomas Gleixner67144652006-07-01 19:29:22 -0700302 /* IPIs are marked IRQF_DISABLED as they must run with irqs
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000303 * disabled */
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000304 set_irq_chip_and_handler(irq, &iic_chip, handle_percpu_irq);
Thomas Gleixner67144652006-07-01 19:29:22 -0700305 request_irq(irq, iic_ipi_action, IRQF_DISABLED, name, NULL);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000306}
307
308void iic_request_IPIs(void)
309{
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200310 iic_request_ipi(PPC_MSG_CALL_FUNCTION, "IPI-call");
311 iic_request_ipi(PPC_MSG_RESCHEDULE, "IPI-resched");
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000312#ifdef CONFIG_DEBUGGER
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200313 iic_request_ipi(PPC_MSG_DEBUGGER_BREAK, "IPI-debug");
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000314#endif /* CONFIG_DEBUGGER */
315}
316#endif /* CONFIG_SMP */
317
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000318static void __init iic_setup_builtin_handlers(void)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000319{
320 int be, isrc;
321
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000322 /* XXX FIXME: Assume two threads per BE are present */
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000323 for (be=0; be < num_present_cpus() / 2; be++) {
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000324 int irq;
325
326 /* setup SPE chip and handlers */
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000327 for (isrc = 0; isrc < IIC_CLASS_STRIDE * 3; isrc++) {
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000328 irq = IIC_NODE_STRIDE * be + IIC_SPE_OFFSET + isrc;
329 set_irq_chip_and_handler(irq, &iic_chip, handle_fasteoi_irq);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000330 }
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000331 /* setup cascade chip */
332 irq = IIC_EXT_CASCADE + be * IIC_NODE_STRIDE;
333 set_irq_chip_and_handler(irq, &iic_chip, handle_fasteoi_irq);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000334 }
335}
336
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000337void __init iic_init_IRQ(void)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000338{
339 int cpu, irq_offset;
340 struct iic *iic;
341
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100342 if (setup_iic() < 0)
343 setup_iic_hardcoded();
344
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000345 irq_offset = 0;
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800346 for_each_possible_cpu(cpu) {
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000347 iic = &per_cpu(iic, cpu);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000348 if (iic->regs)
349 out_be64(&iic->regs->prio, 0xff);
350 }
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000351 iic_setup_builtin_handlers();
352
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000353}