blob: 434fb934dd20afe7c6fcf206641f413b1b464606 [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 *
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10004 * Copyright (C) 2006 Benjamin Herrenschmidt (benh@kernel.crashing.org)
5 * IBM, Corp.
6 *
Arnd Bergmanncebf5892005-06-23 09:43:43 +10007 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
8 *
9 * Author: Arnd Bergmann <arndb@de.ibm.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100024 *
25 * TODO:
26 * - Fix various assumptions related to HW CPU numbers vs. linux CPU numbers
27 * vs node numbers in the setup code
28 * - Implement proper handling of maxcpus=1/2 (that is, routing of irqs from
29 * a non-active node to the active node)
Arnd Bergmanncebf5892005-06-23 09:43:43 +100030 */
31
Arnd Bergmanncebf5892005-06-23 09:43:43 +100032#include <linux/interrupt.h>
33#include <linux/irq.h>
Arnd Bergmann2fb9d202006-01-05 14:05:29 +000034#include <linux/module.h>
Arnd Bergmanncebf5892005-06-23 09:43:43 +100035#include <linux/percpu.h>
36#include <linux/types.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100037#include <linux/ioport.h>
Arnd Bergmanncebf5892005-06-23 09:43:43 +100038
39#include <asm/io.h>
40#include <asm/pgtable.h>
41#include <asm/prom.h>
42#include <asm/ptrace.h>
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100043#include <asm/machdep.h>
Arnd Bergmanncebf5892005-06-23 09:43:43 +100044
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050045#include "interrupt.h"
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020046#include "cbe_regs.h"
Arnd Bergmanncebf5892005-06-23 09:43:43 +100047
48struct iic {
Benjamin Herrenschmidtacf7d762006-06-19 20:33:16 +020049 struct cbe_iic_thread_regs __iomem *regs;
Arnd Bergmann2fb9d202006-01-05 14:05:29 +000050 u8 target_id;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100051 u8 eoi_stack[16];
52 int eoi_ptr;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100053 struct device_node *node;
Arnd Bergmanncebf5892005-06-23 09:43:43 +100054};
55
56static DEFINE_PER_CPU(struct iic, iic);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100057#define IIC_NODE_COUNT 2
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100058static struct irq_host *iic_host;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100059
60/* Convert between "pending" bits and hw irq number */
61static irq_hw_number_t iic_pending_to_hwnum(struct cbe_iic_pending_bits bits)
62{
63 unsigned char unit = bits.source & 0xf;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100064 unsigned char node = bits.source >> 4;
65 unsigned char class = bits.class & 3;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100066
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100067 /* Decode IPIs */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100068 if (bits.flags & CBE_IIC_IRQ_IPI)
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100069 return IIC_IRQ_TYPE_IPI | (bits.prio >> 4);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100070 else
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100071 return (node << IIC_IRQ_NODE_SHIFT) | (class << 4) | unit;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100072}
Arnd Bergmanncebf5892005-06-23 09:43:43 +100073
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100074static void iic_mask(unsigned int irq)
75{
76}
77
78static void iic_unmask(unsigned int irq)
79{
80}
81
82static void iic_eoi(unsigned int irq)
Arnd Bergmanncebf5892005-06-23 09:43:43 +100083{
Arnd Bergmann55364082006-03-23 00:00:07 +010084 struct iic *iic = &__get_cpu_var(iic);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100085 out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
86 BUG_ON(iic->eoi_ptr < 0);
Arnd Bergmanncebf5892005-06-23 09:43:43 +100087}
88
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100089static struct irq_chip iic_chip = {
Arnd Bergmannf3f66f52005-10-31 20:08:37 -050090 .typename = " CELL-IIC ",
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +100091 .mask = iic_mask,
92 .unmask = iic_unmask,
93 .eoi = iic_eoi,
Arnd Bergmanncebf5892005-06-23 09:43:43 +100094};
95
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +100096
97static void iic_ioexc_eoi(unsigned int irq)
98{
99}
100
101static void iic_ioexc_cascade(unsigned int irq, struct irq_desc *desc,
102 struct pt_regs *regs)
103{
Arnd Bergmann43b4f402006-10-04 17:26:24 +0200104 struct cbe_iic_regs __iomem *node_iic = (void __iomem *)desc->handler_data;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000105 unsigned int base = (irq & 0xffffff00) | IIC_IRQ_TYPE_IOEXC;
106 unsigned long bits, ack;
107 int cascade;
108
109 for (;;) {
110 bits = in_be64(&node_iic->iic_is);
111 if (bits == 0)
112 break;
113 /* pre-ack edge interrupts */
114 ack = bits & IIC_ISR_EDGE_MASK;
115 if (ack)
116 out_be64(&node_iic->iic_is, ack);
117 /* handle them */
118 for (cascade = 63; cascade >= 0; cascade--)
119 if (bits & (0x8000000000000000UL >> cascade)) {
120 unsigned int cirq =
121 irq_linear_revmap(iic_host,
122 base | cascade);
123 if (cirq != NO_IRQ)
124 generic_handle_irq(cirq, regs);
125 }
126 /* post-ack level interrupts */
127 ack = bits & ~IIC_ISR_EDGE_MASK;
128 if (ack)
129 out_be64(&node_iic->iic_is, ack);
130 }
131 desc->chip->eoi(irq);
132}
133
134
135static struct irq_chip iic_ioexc_chip = {
136 .typename = " CELL-IOEX",
137 .mask = iic_mask,
138 .unmask = iic_unmask,
139 .eoi = iic_ioexc_eoi,
140};
141
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000142/* Get an IRQ number from the pending state register of the IIC */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000143static unsigned int iic_get_irq(struct pt_regs *regs)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000144{
Geoff Levand9e6ee342006-08-09 15:28:13 -0700145 struct cbe_iic_pending_bits pending;
146 struct iic *iic;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000147 unsigned int virq;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000148
Geoff Levand9e6ee342006-08-09 15:28:13 -0700149 iic = &__get_cpu_var(iic);
150 *(unsigned long *) &pending =
151 in_be64((unsigned long __iomem *) &iic->regs->pending_destr);
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000152 if (!(pending.flags & CBE_IIC_IRQ_VALID))
153 return NO_IRQ;
154 virq = irq_linear_revmap(iic_host, iic_pending_to_hwnum(pending));
155 if (virq == NO_IRQ)
156 return NO_IRQ;
Geoff Levand9e6ee342006-08-09 15:28:13 -0700157 iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
158 BUG_ON(iic->eoi_ptr > 15);
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000159 return virq;
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100160}
161
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000162#ifdef CONFIG_SMP
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200163
164/* Use the highest interrupt priorities for IPI */
165static inline int iic_ipi_to_irq(int ipi)
166{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000167 return IIC_IRQ_TYPE_IPI + 0xf - ipi;
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200168}
169
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000170void iic_setup_cpu(void)
171{
172 out_be64(&__get_cpu_var(iic).regs->prio, 0xff);
173}
174
175void iic_cause_IPI(int cpu, int mesg)
176{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000177 out_be64(&per_cpu(iic, cpu).regs->generate, (0xf - mesg) << 4);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000178}
179
Arnd Bergmann2fb9d202006-01-05 14:05:29 +0000180u8 iic_get_target_id(int cpu)
181{
182 return per_cpu(iic, cpu).target_id;
183}
184EXPORT_SYMBOL_GPL(iic_get_target_id);
185
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000186struct irq_host *iic_get_irq_host(int node)
187{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000188 return iic_host;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000189}
190EXPORT_SYMBOL_GPL(iic_get_irq_host);
191
192
David Howells7d12e782006-10-05 14:55:46 +0100193static irqreturn_t iic_ipi_action(int irq, void *dev_id)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000194{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000195 int ipi = (int)(long)dev_id;
196
David Howells7d12e782006-10-05 14:55:46 +0100197 smp_message_recv(ipi);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000198
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000199 return IRQ_HANDLED;
200}
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200201static void iic_request_ipi(int ipi, const char *name)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000202{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000203 int virq;
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200204
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000205 virq = irq_create_mapping(iic_host, iic_ipi_to_irq(ipi));
206 if (virq == NO_IRQ) {
207 printk(KERN_ERR
208 "iic: failed to map IPI %s\n", name);
209 return;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000210 }
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000211 if (request_irq(virq, iic_ipi_action, IRQF_DISABLED, name,
212 (void *)(long)ipi))
213 printk(KERN_ERR
214 "iic: failed to request IPI %s\n", name);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000215}
216
217void iic_request_IPIs(void)
218{
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200219 iic_request_ipi(PPC_MSG_CALL_FUNCTION, "IPI-call");
220 iic_request_ipi(PPC_MSG_RESCHEDULE, "IPI-resched");
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000221#ifdef CONFIG_DEBUGGER
Arnd Bergmanna84195f2005-08-18 19:35:21 +0200222 iic_request_ipi(PPC_MSG_DEBUGGER_BREAK, "IPI-debug");
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000223#endif /* CONFIG_DEBUGGER */
224}
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000225
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000226#endif /* CONFIG_SMP */
227
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000228
229static int iic_host_match(struct irq_host *h, struct device_node *node)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000230{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000231 return device_is_compatible(node,
232 "IBM,CBEA-Internal-Interrupt-Controller");
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000233}
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000234
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000235static int iic_host_map(struct irq_host *h, unsigned int virq,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700236 irq_hw_number_t hw)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000237{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000238 switch (hw & IIC_IRQ_TYPE_MASK) {
239 case IIC_IRQ_TYPE_IPI:
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000240 set_irq_chip_and_handler(virq, &iic_chip, handle_percpu_irq);
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000241 break;
242 case IIC_IRQ_TYPE_IOEXC:
243 set_irq_chip_and_handler(virq, &iic_ioexc_chip,
244 handle_fasteoi_irq);
245 break;
246 default:
247 set_irq_chip_and_handler(virq, &iic_chip, handle_fasteoi_irq);
248 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000249 return 0;
250}
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000251
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000252static int iic_host_xlate(struct irq_host *h, struct device_node *ct,
253 u32 *intspec, unsigned int intsize,
254 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
255
256{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000257 unsigned int node, ext, unit, class;
258 const u32 *val;
259
260 if (!device_is_compatible(ct,
261 "IBM,CBEA-Internal-Interrupt-Controller"))
262 return -ENODEV;
263 if (intsize != 1)
264 return -ENODEV;
265 val = get_property(ct, "#interrupt-cells", NULL);
266 if (val == NULL || *val != 1)
267 return -ENODEV;
268
269 node = intspec[0] >> 24;
270 ext = (intspec[0] >> 16) & 0xff;
271 class = (intspec[0] >> 8) & 0xff;
272 unit = intspec[0] & 0xff;
273
274 /* Check if node is in supported range */
275 if (node > 1)
276 return -EINVAL;
277
278 /* Build up interrupt number, special case for IO exceptions */
279 *out_hwirq = (node << IIC_IRQ_NODE_SHIFT);
280 if (unit == IIC_UNIT_IIC && class == 1)
281 *out_hwirq |= IIC_IRQ_TYPE_IOEXC | ext;
282 else
283 *out_hwirq |= IIC_IRQ_TYPE_NORMAL |
284 (class << IIC_IRQ_CLASS_SHIFT) | unit;
285
286 /* Dummy flags, ignored by iic code */
287 *out_flags = IRQ_TYPE_EDGE_RISING;
288
289 return 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000290}
291
292static struct irq_host_ops iic_host_ops = {
293 .match = iic_host_match,
294 .map = iic_host_map,
295 .xlate = iic_host_xlate,
296};
297
298static void __init init_one_iic(unsigned int hw_cpu, unsigned long addr,
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000299 struct device_node *node)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000300{
301 /* XXX FIXME: should locate the linux CPU number from the HW cpu
302 * number properly. We are lucky for now
303 */
304 struct iic *iic = &per_cpu(iic, hw_cpu);
305
306 iic->regs = ioremap(addr, sizeof(struct cbe_iic_thread_regs));
307 BUG_ON(iic->regs == NULL);
308
309 iic->target_id = ((hw_cpu & 2) << 3) | ((hw_cpu & 1) ? 0xf : 0xe);
310 iic->eoi_stack[0] = 0xff;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000311 iic->node = of_node_get(node);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000312 out_be64(&iic->regs->prio, 0);
313
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000314 printk(KERN_INFO "IIC for CPU %d target id 0x%x : %s\n",
315 hw_cpu, iic->target_id, node->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000316}
317
318static int __init setup_iic(void)
319{
320 struct device_node *dn;
321 struct resource r0, r1;
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000322 unsigned int node, cascade, found = 0;
Arnd Bergmann43b4f402006-10-04 17:26:24 +0200323 struct cbe_iic_regs __iomem *node_iic;
Geoff Levand9e6ee342006-08-09 15:28:13 -0700324 const u32 *np;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000325
326 for (dn = NULL;
327 (dn = of_find_node_by_name(dn,"interrupt-controller")) != NULL;) {
328 if (!device_is_compatible(dn,
329 "IBM,CBEA-Internal-Interrupt-Controller"))
330 continue;
Jeremy Kerrc61c27d2006-07-12 15:39:54 +1000331 np = get_property(dn, "ibm,interrupt-server-ranges", NULL);
Geoff Levand9e6ee342006-08-09 15:28:13 -0700332 if (np == NULL) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000333 printk(KERN_WARNING "IIC: CPU association not found\n");
334 of_node_put(dn);
335 return -ENODEV;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000336 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000337 if (of_address_to_resource(dn, 0, &r0) ||
338 of_address_to_resource(dn, 1, &r1)) {
339 printk(KERN_WARNING "IIC: Can't resolve addresses\n");
340 of_node_put(dn);
341 return -ENODEV;
342 }
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000343 found++;
344 init_one_iic(np[0], r0.start, dn);
345 init_one_iic(np[1], r1.start, dn);
346
347 /* Setup cascade for IO exceptions. XXX cleanup tricks to get
348 * node vs CPU etc...
349 * Note that we configure the IIC_IRR here with a hard coded
350 * priority of 1. We might want to improve that later.
351 */
352 node = np[0] >> 1;
353 node_iic = cbe_get_cpu_iic_regs(np[0]);
354 cascade = node << IIC_IRQ_NODE_SHIFT;
355 cascade |= 1 << IIC_IRQ_CLASS_SHIFT;
356 cascade |= IIC_UNIT_IIC;
357 cascade = irq_create_mapping(iic_host, cascade);
358 if (cascade == NO_IRQ)
359 continue;
Arnd Bergmann43b4f402006-10-04 17:26:24 +0200360 /*
361 * irq_data is a generic pointer that gets passed back
362 * to us later, so the forced cast is fine.
363 */
364 set_irq_data(cascade, (void __force *)node_iic);
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000365 set_irq_chained_handler(cascade , iic_ioexc_cascade);
366 out_be64(&node_iic->iic_ir,
367 (1 << 12) /* priority */ |
368 (node << 4) /* dest node */ |
369 IIC_UNIT_THREAD_0 /* route them to thread 0 */);
370 /* Flush pending (make sure it triggers if there is
371 * anything pending
372 */
373 out_be64(&node_iic->iic_is, 0xfffffffffffffffful);
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000374 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000375
376 if (found)
377 return 0;
378 else
379 return -ENODEV;
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000380}
381
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000382void __init iic_init_IRQ(void)
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000383{
Benjamin Herrenschmidt2e194582006-09-29 15:00:29 +1000384 /* Setup an irq host data structure */
385 iic_host = irq_alloc_host(IRQ_HOST_MAP_LINEAR, IIC_SOURCE_COUNT,
386 &iic_host_ops, IIC_IRQ_INVALID);
387 BUG_ON(iic_host == NULL);
388 irq_set_default_host(iic_host);
389
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000390 /* Discover and initialize iics */
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100391 if (setup_iic() < 0)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000392 panic("IIC: Failed to initialize !\n");
Jens Osterkampd0e57c62006-03-23 00:00:06 +0100393
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000394 /* Set master interrupt handling function */
395 ppc_md.get_irq = iic_get_irq;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000396
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000397 /* Enable on current CPU */
398 iic_setup_cpu();
Arnd Bergmanncebf5892005-06-23 09:43:43 +1000399}