1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cell Internal Interrupt Controller
5 * Copyright (C) 2006 Benjamin Herrenschmidt (benh@kernel.crashing.org)
8 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
10 * Author: Arnd Bergmann <arndb@de.ibm.com>
13 * - Fix various assumptions related to HW CPU numbers vs. linux CPU numbers
14 * vs node numbers in the setup code
15 * - Implement proper handling of maxcpus=1/2 (that is, routing of irqs from
16 * a non-active node to the active node)
19 #include <linux/interrupt.h>
20 #include <linux/irq.h>
21 #include <linux/irqdomain.h>
22 #include <linux/export.h>
23 #include <linux/percpu.h>
24 #include <linux/types.h>
25 #include <linux/ioport.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/pgtable.h>
28 #include <linux/of_address.h>
31 #include <asm/ptrace.h>
32 #include <asm/machdep.h>
33 #include <asm/cell-regs.h>
35 #include "interrupt.h"
38 struct cbe_iic_thread_regs __iomem *regs;
42 struct device_node *node;
45 static DEFINE_PER_CPU(struct iic, cpu_iic);
46 #define IIC_NODE_COUNT 2
47 static struct irq_domain *iic_host;
49 /* Convert between "pending" bits and hw irq number */
50 static irq_hw_number_t iic_pending_to_hwnum(struct cbe_iic_pending_bits bits)
52 unsigned char unit = bits.source & 0xf;
53 unsigned char node = bits.source >> 4;
54 unsigned char class = bits.class & 3;
57 if (bits.flags & CBE_IIC_IRQ_IPI)
58 return IIC_IRQ_TYPE_IPI | (bits.prio >> 4);
60 return (node << IIC_IRQ_NODE_SHIFT) | (class << 4) | unit;
63 static void iic_mask(struct irq_data *d)
67 static void iic_unmask(struct irq_data *d)
71 static void iic_eoi(struct irq_data *d)
73 struct iic *iic = this_cpu_ptr(&cpu_iic);
74 out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
75 BUG_ON(iic->eoi_ptr < 0);
78 static struct irq_chip iic_chip = {
81 .irq_unmask = iic_unmask,
86 static void iic_ioexc_eoi(struct irq_data *d)
90 static void iic_ioexc_cascade(struct irq_desc *desc)
92 struct irq_chip *chip = irq_desc_get_chip(desc);
93 struct cbe_iic_regs __iomem *node_iic =
94 (void __iomem *)irq_desc_get_handler_data(desc);
95 unsigned int irq = irq_desc_get_irq(desc);
96 unsigned int base = (irq & 0xffffff00) | IIC_IRQ_TYPE_IOEXC;
97 unsigned long bits, ack;
101 bits = in_be64(&node_iic->iic_is);
104 /* pre-ack edge interrupts */
105 ack = bits & IIC_ISR_EDGE_MASK;
107 out_be64(&node_iic->iic_is, ack);
109 for (cascade = 63; cascade >= 0; cascade--)
110 if (bits & (0x8000000000000000UL >> cascade))
111 generic_handle_domain_irq(iic_host,
113 /* post-ack level interrupts */
114 ack = bits & ~IIC_ISR_EDGE_MASK;
116 out_be64(&node_iic->iic_is, ack);
118 chip->irq_eoi(&desc->irq_data);
122 static struct irq_chip iic_ioexc_chip = {
124 .irq_mask = iic_mask,
125 .irq_unmask = iic_unmask,
126 .irq_eoi = iic_ioexc_eoi,
129 /* Get an IRQ number from the pending state register of the IIC */
130 static unsigned int iic_get_irq(void)
132 struct cbe_iic_pending_bits pending;
136 iic = this_cpu_ptr(&cpu_iic);
137 *(unsigned long *) &pending =
138 in_be64((u64 __iomem *) &iic->regs->pending_destr);
139 if (!(pending.flags & CBE_IIC_IRQ_VALID))
141 virq = irq_linear_revmap(iic_host, iic_pending_to_hwnum(pending));
144 iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
145 BUG_ON(iic->eoi_ptr > 15);
149 void iic_setup_cpu(void)
151 out_be64(&this_cpu_ptr(&cpu_iic)->regs->prio, 0xff);
154 u8 iic_get_target_id(int cpu)
156 return per_cpu(cpu_iic, cpu).target_id;
159 EXPORT_SYMBOL_GPL(iic_get_target_id);
163 /* Use the highest interrupt priorities for IPI */
164 static inline int iic_msg_to_irq(int msg)
166 return IIC_IRQ_TYPE_IPI + 0xf - msg;
169 void iic_message_pass(int cpu, int msg)
171 out_be64(&per_cpu(cpu_iic, cpu).regs->generate, (0xf - msg) << 4);
174 static void iic_request_ipi(int msg)
178 virq = irq_create_mapping(iic_host, iic_msg_to_irq(msg));
181 "iic: failed to map IPI %s\n", smp_ipi_name[msg]);
186 * If smp_request_message_ipi encounters an error it will notify
187 * the error. If a message is not needed it will return non-zero.
189 if (smp_request_message_ipi(virq, msg))
190 irq_dispose_mapping(virq);
193 void iic_request_IPIs(void)
195 iic_request_ipi(PPC_MSG_CALL_FUNCTION);
196 iic_request_ipi(PPC_MSG_RESCHEDULE);
197 iic_request_ipi(PPC_MSG_TICK_BROADCAST);
198 iic_request_ipi(PPC_MSG_NMI_IPI);
201 #endif /* CONFIG_SMP */
204 static int iic_host_match(struct irq_domain *h, struct device_node *node,
205 enum irq_domain_bus_token bus_token)
207 return of_device_is_compatible(node,
208 "IBM,CBEA-Internal-Interrupt-Controller");
211 static int iic_host_map(struct irq_domain *h, unsigned int virq,
214 switch (hw & IIC_IRQ_TYPE_MASK) {
215 case IIC_IRQ_TYPE_IPI:
216 irq_set_chip_and_handler(virq, &iic_chip, handle_percpu_irq);
218 case IIC_IRQ_TYPE_IOEXC:
219 irq_set_chip_and_handler(virq, &iic_ioexc_chip,
220 handle_edge_eoi_irq);
223 irq_set_chip_and_handler(virq, &iic_chip, handle_edge_eoi_irq);
228 static int iic_host_xlate(struct irq_domain *h, struct device_node *ct,
229 const u32 *intspec, unsigned int intsize,
230 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
233 unsigned int node, ext, unit, class;
236 if (!of_device_is_compatible(ct,
237 "IBM,CBEA-Internal-Interrupt-Controller"))
241 val = of_get_property(ct, "#interrupt-cells", NULL);
242 if (val == NULL || *val != 1)
245 node = intspec[0] >> 24;
246 ext = (intspec[0] >> 16) & 0xff;
247 class = (intspec[0] >> 8) & 0xff;
248 unit = intspec[0] & 0xff;
250 /* Check if node is in supported range */
254 /* Build up interrupt number, special case for IO exceptions */
255 *out_hwirq = (node << IIC_IRQ_NODE_SHIFT);
256 if (unit == IIC_UNIT_IIC && class == 1)
257 *out_hwirq |= IIC_IRQ_TYPE_IOEXC | ext;
259 *out_hwirq |= IIC_IRQ_TYPE_NORMAL |
260 (class << IIC_IRQ_CLASS_SHIFT) | unit;
262 /* Dummy flags, ignored by iic code */
263 *out_flags = IRQ_TYPE_EDGE_RISING;
268 static const struct irq_domain_ops iic_host_ops = {
269 .match = iic_host_match,
271 .xlate = iic_host_xlate,
274 static void __init init_one_iic(unsigned int hw_cpu, unsigned long addr,
275 struct device_node *node)
277 /* XXX FIXME: should locate the linux CPU number from the HW cpu
278 * number properly. We are lucky for now
280 struct iic *iic = &per_cpu(cpu_iic, hw_cpu);
282 iic->regs = ioremap(addr, sizeof(struct cbe_iic_thread_regs));
283 BUG_ON(iic->regs == NULL);
285 iic->target_id = ((hw_cpu & 2) << 3) | ((hw_cpu & 1) ? 0xf : 0xe);
286 iic->eoi_stack[0] = 0xff;
287 iic->node = of_node_get(node);
288 out_be64(&iic->regs->prio, 0);
290 printk(KERN_INFO "IIC for CPU %d target id 0x%x : %pOF\n",
291 hw_cpu, iic->target_id, node);
294 static int __init setup_iic(void)
296 struct device_node *dn;
297 struct resource r0, r1;
298 unsigned int node, cascade, found = 0;
299 struct cbe_iic_regs __iomem *node_iic;
302 for_each_node_by_name(dn, "interrupt-controller") {
303 if (!of_device_is_compatible(dn,
304 "IBM,CBEA-Internal-Interrupt-Controller"))
306 np = of_get_property(dn, "ibm,interrupt-server-ranges", NULL);
308 printk(KERN_WARNING "IIC: CPU association not found\n");
312 if (of_address_to_resource(dn, 0, &r0) ||
313 of_address_to_resource(dn, 1, &r1)) {
314 printk(KERN_WARNING "IIC: Can't resolve addresses\n");
319 init_one_iic(np[0], r0.start, dn);
320 init_one_iic(np[1], r1.start, dn);
322 /* Setup cascade for IO exceptions. XXX cleanup tricks to get
324 * Note that we configure the IIC_IRR here with a hard coded
325 * priority of 1. We might want to improve that later.
328 node_iic = cbe_get_cpu_iic_regs(np[0]);
329 cascade = node << IIC_IRQ_NODE_SHIFT;
330 cascade |= 1 << IIC_IRQ_CLASS_SHIFT;
331 cascade |= IIC_UNIT_IIC;
332 cascade = irq_create_mapping(iic_host, cascade);
336 * irq_data is a generic pointer that gets passed back
337 * to us later, so the forced cast is fine.
339 irq_set_handler_data(cascade, (void __force *)node_iic);
340 irq_set_chained_handler(cascade, iic_ioexc_cascade);
341 out_be64(&node_iic->iic_ir,
342 (1 << 12) /* priority */ |
343 (node << 4) /* dest node */ |
344 IIC_UNIT_THREAD_0 /* route them to thread 0 */);
345 /* Flush pending (make sure it triggers if there is
348 out_be64(&node_iic->iic_is, 0xfffffffffffffffful);
357 void __init iic_init_IRQ(void)
359 /* Setup an irq host data structure */
360 iic_host = irq_domain_add_linear(NULL, IIC_SOURCE_COUNT, &iic_host_ops,
362 BUG_ON(iic_host == NULL);
363 irq_set_default_host(iic_host);
365 /* Discover and initialize iics */
367 panic("IIC: Failed to initialize !\n");
369 /* Set master interrupt handling function */
370 ppc_md.get_irq = iic_get_irq;
372 /* Enable on current CPU */
376 void iic_set_interrupt_routing(int cpu, int thread, int priority)
378 struct cbe_iic_regs __iomem *iic_regs = cbe_get_cpu_iic_regs(cpu);
382 /* Set which node and thread will handle the next interrupt */
383 iic_ir |= CBE_IIC_IR_PRIO(priority) |
384 CBE_IIC_IR_DEST_NODE(node);
386 iic_ir |= CBE_IIC_IR_DEST_UNIT(CBE_IIC_IR_PT_0);
388 iic_ir |= CBE_IIC_IR_DEST_UNIT(CBE_IIC_IR_PT_1);
389 out_be64(&iic_regs->iic_ir, iic_ir);