blob: 909306ca04f41de7df5491bdfc59f8e1174037f8 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
2 * arch/powerpc/kernel/mpic.c
3 *
4 * Driver for interrupt controllers following the OpenPIC standard, the
5 * common implementation beeing IBM's MPIC. This driver also can deal
6 * with various broken implementations of this HW.
7 *
8 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
9 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
12 * for more details.
13 */
14
15#undef DEBUG
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +110016#undef DEBUG_IPI
17#undef DEBUG_IRQ
18#undef DEBUG_LOW
Paul Mackerras14cf11a2005-09-26 16:04:21 +100019
Paul Mackerras14cf11a2005-09-26 16:04:21 +100020#include <linux/types.h>
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/irq.h>
24#include <linux/smp.h>
25#include <linux/interrupt.h>
26#include <linux/bootmem.h>
27#include <linux/spinlock.h>
28#include <linux/pci.h>
29
30#include <asm/ptrace.h>
31#include <asm/signal.h>
32#include <asm/io.h>
33#include <asm/pgtable.h>
34#include <asm/irq.h>
35#include <asm/machdep.h>
36#include <asm/mpic.h>
37#include <asm/smp.h>
38
39#ifdef DEBUG
40#define DBG(fmt...) printk(fmt)
41#else
42#define DBG(fmt...)
43#endif
44
45static struct mpic *mpics;
46static struct mpic *mpic_primary;
47static DEFINE_SPINLOCK(mpic_lock);
48
Paul Mackerrasc0c0d992005-10-01 13:49:08 +100049#ifdef CONFIG_PPC32 /* XXX for now */
Andy Whitcrofte40c7f02005-11-29 19:25:54 +000050#ifdef CONFIG_IRQ_ALL_CPUS
51#define distribute_irqs (1)
52#else
53#define distribute_irqs (0)
54#endif
Paul Mackerrasc0c0d992005-10-01 13:49:08 +100055#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100056
Zang Roy-r6191172335932006-08-25 14:16:30 +100057#ifdef CONFIG_MPIC_WEIRD
58static u32 mpic_infos[][MPIC_IDX_END] = {
59 [0] = { /* Original OpenPIC compatible MPIC */
60 MPIC_GREG_BASE,
61 MPIC_GREG_FEATURE_0,
62 MPIC_GREG_GLOBAL_CONF_0,
63 MPIC_GREG_VENDOR_ID,
64 MPIC_GREG_IPI_VECTOR_PRI_0,
65 MPIC_GREG_IPI_STRIDE,
66 MPIC_GREG_SPURIOUS,
67 MPIC_GREG_TIMER_FREQ,
68
69 MPIC_TIMER_BASE,
70 MPIC_TIMER_STRIDE,
71 MPIC_TIMER_CURRENT_CNT,
72 MPIC_TIMER_BASE_CNT,
73 MPIC_TIMER_VECTOR_PRI,
74 MPIC_TIMER_DESTINATION,
75
76 MPIC_CPU_BASE,
77 MPIC_CPU_STRIDE,
78 MPIC_CPU_IPI_DISPATCH_0,
79 MPIC_CPU_IPI_DISPATCH_STRIDE,
80 MPIC_CPU_CURRENT_TASK_PRI,
81 MPIC_CPU_WHOAMI,
82 MPIC_CPU_INTACK,
83 MPIC_CPU_EOI,
84
85 MPIC_IRQ_BASE,
86 MPIC_IRQ_STRIDE,
87 MPIC_IRQ_VECTOR_PRI,
88 MPIC_VECPRI_VECTOR_MASK,
89 MPIC_VECPRI_POLARITY_POSITIVE,
90 MPIC_VECPRI_POLARITY_NEGATIVE,
91 MPIC_VECPRI_SENSE_LEVEL,
92 MPIC_VECPRI_SENSE_EDGE,
93 MPIC_VECPRI_POLARITY_MASK,
94 MPIC_VECPRI_SENSE_MASK,
95 MPIC_IRQ_DESTINATION
96 },
97 [1] = { /* Tsi108/109 PIC */
98 TSI108_GREG_BASE,
99 TSI108_GREG_FEATURE_0,
100 TSI108_GREG_GLOBAL_CONF_0,
101 TSI108_GREG_VENDOR_ID,
102 TSI108_GREG_IPI_VECTOR_PRI_0,
103 TSI108_GREG_IPI_STRIDE,
104 TSI108_GREG_SPURIOUS,
105 TSI108_GREG_TIMER_FREQ,
106
107 TSI108_TIMER_BASE,
108 TSI108_TIMER_STRIDE,
109 TSI108_TIMER_CURRENT_CNT,
110 TSI108_TIMER_BASE_CNT,
111 TSI108_TIMER_VECTOR_PRI,
112 TSI108_TIMER_DESTINATION,
113
114 TSI108_CPU_BASE,
115 TSI108_CPU_STRIDE,
116 TSI108_CPU_IPI_DISPATCH_0,
117 TSI108_CPU_IPI_DISPATCH_STRIDE,
118 TSI108_CPU_CURRENT_TASK_PRI,
119 TSI108_CPU_WHOAMI,
120 TSI108_CPU_INTACK,
121 TSI108_CPU_EOI,
122
123 TSI108_IRQ_BASE,
124 TSI108_IRQ_STRIDE,
125 TSI108_IRQ_VECTOR_PRI,
126 TSI108_VECPRI_VECTOR_MASK,
127 TSI108_VECPRI_POLARITY_POSITIVE,
128 TSI108_VECPRI_POLARITY_NEGATIVE,
129 TSI108_VECPRI_SENSE_LEVEL,
130 TSI108_VECPRI_SENSE_EDGE,
131 TSI108_VECPRI_POLARITY_MASK,
132 TSI108_VECPRI_SENSE_MASK,
133 TSI108_IRQ_DESTINATION
134 },
135};
136
137#define MPIC_INFO(name) mpic->hw_set[MPIC_IDX_##name]
138
139#else /* CONFIG_MPIC_WEIRD */
140
141#define MPIC_INFO(name) MPIC_##name
142
143#endif /* CONFIG_MPIC_WEIRD */
144
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145/*
146 * Register accessor functions
147 */
148
149
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100150static inline u32 _mpic_read(enum mpic_reg_type type,
151 struct mpic_reg_bank *rb,
152 unsigned int reg)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000153{
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100154 switch(type) {
155#ifdef CONFIG_PPC_DCR
156 case mpic_access_dcr:
157 return dcr_read(rb->dhost,
158 rb->dbase + reg + rb->doff);
159#endif
160 case mpic_access_mmio_be:
161 return in_be32(rb->base + (reg >> 2));
162 case mpic_access_mmio_le:
163 default:
164 return in_le32(rb->base + (reg >> 2));
165 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000166}
167
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100168static inline void _mpic_write(enum mpic_reg_type type,
169 struct mpic_reg_bank *rb,
170 unsigned int reg, u32 value)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000171{
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100172 switch(type) {
173#ifdef CONFIG_PPC_DCR
174 case mpic_access_dcr:
175 return dcr_write(rb->dhost,
176 rb->dbase + reg + rb->doff, value);
177#endif
178 case mpic_access_mmio_be:
179 return out_be32(rb->base + (reg >> 2), value);
180 case mpic_access_mmio_le:
181 default:
182 return out_le32(rb->base + (reg >> 2), value);
183 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000184}
185
186static inline u32 _mpic_ipi_read(struct mpic *mpic, unsigned int ipi)
187{
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100188 enum mpic_reg_type type = mpic->reg_type;
Zang Roy-r6191172335932006-08-25 14:16:30 +1000189 unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
190 (ipi * MPIC_INFO(GREG_IPI_STRIDE));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000191
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100192 if ((mpic->flags & MPIC_BROKEN_IPI) && type == mpic_access_mmio_le)
193 type = mpic_access_mmio_be;
194 return _mpic_read(type, &mpic->gregs, offset);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000195}
196
197static inline void _mpic_ipi_write(struct mpic *mpic, unsigned int ipi, u32 value)
198{
Zang Roy-r6191172335932006-08-25 14:16:30 +1000199 unsigned int offset = MPIC_INFO(GREG_IPI_VECTOR_PRI_0) +
200 (ipi * MPIC_INFO(GREG_IPI_STRIDE));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000201
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100202 _mpic_write(mpic->reg_type, &mpic->gregs, offset, value);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000203}
204
205static inline u32 _mpic_cpu_read(struct mpic *mpic, unsigned int reg)
206{
207 unsigned int cpu = 0;
208
209 if (mpic->flags & MPIC_PRIMARY)
210 cpu = hard_smp_processor_id();
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100211 return _mpic_read(mpic->reg_type, &mpic->cpuregs[cpu], reg);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212}
213
214static inline void _mpic_cpu_write(struct mpic *mpic, unsigned int reg, u32 value)
215{
216 unsigned int cpu = 0;
217
218 if (mpic->flags & MPIC_PRIMARY)
219 cpu = hard_smp_processor_id();
220
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100221 _mpic_write(mpic->reg_type, &mpic->cpuregs[cpu], reg, value);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000222}
223
224static inline u32 _mpic_irq_read(struct mpic *mpic, unsigned int src_no, unsigned int reg)
225{
226 unsigned int isu = src_no >> mpic->isu_shift;
227 unsigned int idx = src_no & mpic->isu_mask;
228
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100229 return _mpic_read(mpic->reg_type, &mpic->isus[isu],
Zang Roy-r6191172335932006-08-25 14:16:30 +1000230 reg + (idx * MPIC_INFO(IRQ_STRIDE)));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231}
232
233static inline void _mpic_irq_write(struct mpic *mpic, unsigned int src_no,
234 unsigned int reg, u32 value)
235{
236 unsigned int isu = src_no >> mpic->isu_shift;
237 unsigned int idx = src_no & mpic->isu_mask;
238
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100239 _mpic_write(mpic->reg_type, &mpic->isus[isu],
Zang Roy-r6191172335932006-08-25 14:16:30 +1000240 reg + (idx * MPIC_INFO(IRQ_STRIDE)), value);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000241}
242
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100243#define mpic_read(b,r) _mpic_read(mpic->reg_type,&(b),(r))
244#define mpic_write(b,r,v) _mpic_write(mpic->reg_type,&(b),(r),(v))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000245#define mpic_ipi_read(i) _mpic_ipi_read(mpic,(i))
246#define mpic_ipi_write(i,v) _mpic_ipi_write(mpic,(i),(v))
247#define mpic_cpu_read(i) _mpic_cpu_read(mpic,(i))
248#define mpic_cpu_write(i,v) _mpic_cpu_write(mpic,(i),(v))
249#define mpic_irq_read(s,r) _mpic_irq_read(mpic,(s),(r))
250#define mpic_irq_write(s,r,v) _mpic_irq_write(mpic,(s),(r),(v))
251
252
253/*
254 * Low level utility functions
255 */
256
257
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100258static void _mpic_map_mmio(struct mpic *mpic, unsigned long phys_addr,
259 struct mpic_reg_bank *rb, unsigned int offset,
260 unsigned int size)
261{
262 rb->base = ioremap(phys_addr + offset, size);
263 BUG_ON(rb->base == NULL);
264}
265
266#ifdef CONFIG_PPC_DCR
267static void _mpic_map_dcr(struct mpic *mpic, struct mpic_reg_bank *rb,
268 unsigned int offset, unsigned int size)
269{
270 rb->dbase = mpic->dcr_base;
271 rb->doff = offset;
272 rb->dhost = dcr_map(mpic->of_node, rb->dbase + rb->doff, size);
273 BUG_ON(!DCR_MAP_OK(rb->dhost));
274}
275
276static inline void mpic_map(struct mpic *mpic, unsigned long phys_addr,
277 struct mpic_reg_bank *rb, unsigned int offset,
278 unsigned int size)
279{
280 if (mpic->flags & MPIC_USES_DCR)
281 _mpic_map_dcr(mpic, rb, offset, size);
282 else
283 _mpic_map_mmio(mpic, phys_addr, rb, offset, size);
284}
285#else /* CONFIG_PPC_DCR */
286#define mpic_map(m,p,b,o,s) _mpic_map_mmio(m,p,b,o,s)
287#endif /* !CONFIG_PPC_DCR */
288
289
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000290
291/* Check if we have one of those nice broken MPICs with a flipped endian on
292 * reads from IPI registers
293 */
294static void __init mpic_test_broken_ipi(struct mpic *mpic)
295{
296 u32 r;
297
Zang Roy-r6191172335932006-08-25 14:16:30 +1000298 mpic_write(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0), MPIC_VECPRI_MASK);
299 r = mpic_read(mpic->gregs, MPIC_INFO(GREG_IPI_VECTOR_PRI_0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000300
301 if (r == le32_to_cpu(MPIC_VECPRI_MASK)) {
302 printk(KERN_INFO "mpic: Detected reversed IPI registers\n");
303 mpic->flags |= MPIC_BROKEN_IPI;
304 }
305}
306
307#ifdef CONFIG_MPIC_BROKEN_U3
308
309/* Test if an interrupt is sourced from HyperTransport (used on broken U3s)
310 * to force the edge setting on the MPIC and do the ack workaround.
311 */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100312static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000313{
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100314 if (source >= 128 || !mpic->fixups)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000315 return 0;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100316 return mpic->fixups[source].base != NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000317}
318
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100319
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100320static inline void mpic_ht_end_irq(struct mpic *mpic, unsigned int source)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000321{
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100322 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000323
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100324 if (fixup->applebase) {
325 unsigned int soff = (fixup->index >> 3) & ~3;
326 unsigned int mask = 1U << (fixup->index & 0x1f);
327 writel(mask, fixup->applebase + soff);
328 } else {
329 spin_lock(&mpic->fixup_lock);
330 writeb(0x11 + 2 * fixup->index, fixup->base + 2);
331 writel(fixup->data, fixup->base + 4);
332 spin_unlock(&mpic->fixup_lock);
333 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000334}
335
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100336static void mpic_startup_ht_interrupt(struct mpic *mpic, unsigned int source,
337 unsigned int irqflags)
338{
339 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
340 unsigned long flags;
341 u32 tmp;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000342
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100343 if (fixup->base == NULL)
344 return;
345
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700346 DBG("startup_ht_interrupt(0x%x, 0x%x) index: %d\n",
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100347 source, irqflags, fixup->index);
348 spin_lock_irqsave(&mpic->fixup_lock, flags);
349 /* Enable and configure */
350 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
351 tmp = readl(fixup->base + 4);
352 tmp &= ~(0x23U);
353 if (irqflags & IRQ_LEVEL)
354 tmp |= 0x22;
355 writel(tmp, fixup->base + 4);
356 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
357}
358
359static void mpic_shutdown_ht_interrupt(struct mpic *mpic, unsigned int source,
360 unsigned int irqflags)
361{
362 struct mpic_irq_fixup *fixup = &mpic->fixups[source];
363 unsigned long flags;
364 u32 tmp;
365
366 if (fixup->base == NULL)
367 return;
368
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700369 DBG("shutdown_ht_interrupt(0x%x, 0x%x)\n", source, irqflags);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100370
371 /* Disable */
372 spin_lock_irqsave(&mpic->fixup_lock, flags);
373 writeb(0x10 + 2 * fixup->index, fixup->base + 2);
374 tmp = readl(fixup->base + 4);
Segher Boessenkool72b13812006-02-17 11:25:42 +0100375 tmp |= 1;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100376 writel(tmp, fixup->base + 4);
377 spin_unlock_irqrestore(&mpic->fixup_lock, flags);
378}
379
380static void __init mpic_scan_ht_pic(struct mpic *mpic, u8 __iomem *devbase,
381 unsigned int devfn, u32 vdid)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000382{
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100383 int i, irq, n;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100384 u8 __iomem *base;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000385 u32 tmp;
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100386 u8 pos;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000387
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100388 for (pos = readb(devbase + PCI_CAPABILITY_LIST); pos != 0;
389 pos = readb(devbase + pos + PCI_CAP_LIST_NEXT)) {
390 u8 id = readb(devbase + pos + PCI_CAP_LIST_ID);
Brice Goglin46ff3462006-08-31 01:55:24 -0400391 if (id == PCI_CAP_ID_HT) {
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100392 id = readb(devbase + pos + 3);
Eric W. Biedermane78d0162006-10-04 02:16:54 -0700393 if (id == HT_CAPTYPE_IRQ)
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100394 break;
395 }
396 }
397 if (pos == 0)
398 return;
399
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100400 base = devbase + pos;
401 writeb(0x01, base + 2);
402 n = (readl(base + 4) >> 16) & 0xff;
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100403
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100404 printk(KERN_INFO "mpic: - HT:%02x.%x [0x%02x] vendor %04x device %04x"
405 " has %d irqs\n",
406 devfn >> 3, devfn & 0x7, pos, vdid & 0xffff, vdid >> 16, n + 1);
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100407
408 for (i = 0; i <= n; i++) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100409 writeb(0x10 + 2 * i, base + 2);
410 tmp = readl(base + 4);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000411 irq = (tmp >> 16) & 0xff;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100412 DBG("HT PIC index 0x%x, irq 0x%x, tmp: %08x\n", i, irq, tmp);
413 /* mask it , will be unmasked later */
414 tmp |= 0x1;
415 writel(tmp, base + 4);
416 mpic->fixups[irq].index = i;
417 mpic->fixups[irq].base = base;
418 /* Apple HT PIC has a non-standard way of doing EOIs */
419 if ((vdid & 0xffff) == 0x106b)
420 mpic->fixups[irq].applebase = devbase + 0x60;
421 else
422 mpic->fixups[irq].applebase = NULL;
423 writeb(0x11 + 2 * i, base + 2);
424 mpic->fixups[irq].data = readl(base + 4) | 0x80000000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000425 }
426}
427
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000428
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100429static void __init mpic_scan_ht_pics(struct mpic *mpic)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000430{
431 unsigned int devfn;
432 u8 __iomem *cfgspace;
433
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100434 printk(KERN_INFO "mpic: Setting up HT PICs workarounds for U3/U4\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000435
436 /* Allocate fixups array */
437 mpic->fixups = alloc_bootmem(128 * sizeof(struct mpic_irq_fixup));
438 BUG_ON(mpic->fixups == NULL);
439 memset(mpic->fixups, 0, 128 * sizeof(struct mpic_irq_fixup));
440
441 /* Init spinlock */
442 spin_lock_init(&mpic->fixup_lock);
443
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100444 /* Map U3 config space. We assume all IO-APICs are on the primary bus
445 * so we only need to map 64kB.
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000446 */
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100447 cfgspace = ioremap(0xf2000000, 0x10000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000448 BUG_ON(cfgspace == NULL);
449
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100450 /* Now we scan all slots. We do a very quick scan, we read the header
451 * type, vendor ID and device ID only, that's plenty enough
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000452 */
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100453 for (devfn = 0; devfn < 0x100; devfn++) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000454 u8 __iomem *devbase = cfgspace + (devfn << 8);
455 u8 hdr_type = readb(devbase + PCI_HEADER_TYPE);
456 u32 l = readl(devbase + PCI_VENDOR_ID);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100457 u16 s;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000458
459 DBG("devfn %x, l: %x\n", devfn, l);
460
461 /* If no device, skip */
462 if (l == 0xffffffff || l == 0x00000000 ||
463 l == 0x0000ffff || l == 0xffff0000)
464 goto next;
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100465 /* Check if is supports capability lists */
466 s = readw(devbase + PCI_STATUS);
467 if (!(s & PCI_STATUS_CAP_LIST))
468 goto next;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000469
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100470 mpic_scan_ht_pic(mpic, devbase, devfn, l);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000471
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000472 next:
473 /* next device, if function 0 */
Segher Boessenkoolc4b22f22005-12-13 18:04:29 +1100474 if (PCI_FUNC(devfn) == 0 && (hdr_type & 0x80) == 0)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000475 devfn += 7;
476 }
477}
478
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700479#else /* CONFIG_MPIC_BROKEN_U3 */
480
481static inline int mpic_is_ht_interrupt(struct mpic *mpic, unsigned int source)
482{
483 return 0;
484}
485
486static void __init mpic_scan_ht_pics(struct mpic *mpic)
487{
488}
489
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000490#endif /* CONFIG_MPIC_BROKEN_U3 */
491
492
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000493#define mpic_irq_to_hw(virq) ((unsigned int)irq_map[virq].hwirq)
494
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000495/* Find an mpic associated with a given linux interrupt */
496static struct mpic *mpic_find(unsigned int irq, unsigned int *is_ipi)
497{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000498 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000499
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000500 if (irq < NUM_ISA_INTERRUPTS)
501 return NULL;
502 if (is_ipi)
503 *is_ipi = (src >= MPIC_VEC_IPI_0 && src <= MPIC_VEC_IPI_3);
504
505 return irq_desc[irq].chip_data;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000506}
507
508/* Convert a cpu mask from logical to physical cpu numbers. */
509static inline u32 mpic_physmask(u32 cpumask)
510{
511 int i;
512 u32 mask = 0;
513
514 for (i = 0; i < NR_CPUS; ++i, cpumask >>= 1)
515 mask |= (cpumask & 1) << get_hard_smp_processor_id(i);
516 return mask;
517}
518
519#ifdef CONFIG_SMP
520/* Get the mpic structure from the IPI number */
521static inline struct mpic * mpic_from_ipi(unsigned int ipi)
522{
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000523 return irq_desc[ipi].chip_data;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000524}
525#endif
526
527/* Get the mpic structure from the irq number */
528static inline struct mpic * mpic_from_irq(unsigned int irq)
529{
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000530 return irq_desc[irq].chip_data;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000531}
532
533/* Send an EOI */
534static inline void mpic_eoi(struct mpic *mpic)
535{
Zang Roy-r6191172335932006-08-25 14:16:30 +1000536 mpic_cpu_write(MPIC_INFO(CPU_EOI), 0);
537 (void)mpic_cpu_read(MPIC_INFO(CPU_WHOAMI));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000538}
539
540#ifdef CONFIG_SMP
David Howells7d12e782006-10-05 14:55:46 +0100541static irqreturn_t mpic_ipi_action(int irq, void *dev_id)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000542{
David Howells7d12e782006-10-05 14:55:46 +0100543 smp_message_recv(mpic_irq_to_hw(irq) - MPIC_VEC_IPI_0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000544 return IRQ_HANDLED;
545}
546#endif /* CONFIG_SMP */
547
548/*
549 * Linux descriptor level callbacks
550 */
551
552
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000553static void mpic_unmask_irq(unsigned int irq)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000554{
555 unsigned int loops = 100000;
556 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000557 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000558
Paul Mackerrasbd561c72005-10-26 21:55:33 +1000559 DBG("%p: %s: enable_irq: %d (src %d)\n", mpic, mpic->name, irq, src);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000560
Zang Roy-r6191172335932006-08-25 14:16:30 +1000561 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
562 mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) &
Benjamin Herrenschmidte5356642005-11-18 17:18:15 +1100563 ~MPIC_VECPRI_MASK);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000564 /* make sure mask gets to controller before we return to user */
565 do {
566 if (!loops--) {
567 printk(KERN_ERR "mpic_enable_irq timeout\n");
568 break;
569 }
Zang Roy-r6191172335932006-08-25 14:16:30 +1000570 } while(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100571}
572
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000573static void mpic_mask_irq(unsigned int irq)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000574{
575 unsigned int loops = 100000;
576 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000577 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000578
579 DBG("%s: disable_irq: %d (src %d)\n", mpic->name, irq, src);
580
Zang Roy-r6191172335932006-08-25 14:16:30 +1000581 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
582 mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) |
Benjamin Herrenschmidte5356642005-11-18 17:18:15 +1100583 MPIC_VECPRI_MASK);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000584
585 /* make sure mask gets to controller before we return to user */
586 do {
587 if (!loops--) {
588 printk(KERN_ERR "mpic_enable_irq timeout\n");
589 break;
590 }
Zang Roy-r6191172335932006-08-25 14:16:30 +1000591 } while(!(mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI)) & MPIC_VECPRI_MASK));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000592}
593
594static void mpic_end_irq(unsigned int irq)
595{
596 struct mpic *mpic = mpic_from_irq(irq);
597
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100598#ifdef DEBUG_IRQ
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000599 DBG("%s: end_irq: %d\n", mpic->name, irq);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100600#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000601 /* We always EOI on end_irq() even for edge interrupts since that
602 * should only lower the priority, the MPIC should have properly
603 * latched another edge interrupt coming in anyway
604 */
605
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000606 mpic_eoi(mpic);
607}
608
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000609#ifdef CONFIG_MPIC_BROKEN_U3
610
611static void mpic_unmask_ht_irq(unsigned int irq)
612{
613 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000614 unsigned int src = mpic_irq_to_hw(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000615
616 mpic_unmask_irq(irq);
617
618 if (irq_desc[irq].status & IRQ_LEVEL)
619 mpic_ht_end_irq(mpic, src);
620}
621
622static unsigned int mpic_startup_ht_irq(unsigned int irq)
623{
624 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000625 unsigned int src = mpic_irq_to_hw(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000626
627 mpic_unmask_irq(irq);
628 mpic_startup_ht_interrupt(mpic, src, irq_desc[irq].status);
629
630 return 0;
631}
632
633static void mpic_shutdown_ht_irq(unsigned int irq)
634{
635 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000636 unsigned int src = mpic_irq_to_hw(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000637
638 mpic_shutdown_ht_interrupt(mpic, src, irq_desc[irq].status);
639 mpic_mask_irq(irq);
640}
641
642static void mpic_end_ht_irq(unsigned int irq)
643{
644 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000645 unsigned int src = mpic_irq_to_hw(irq);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000646
647#ifdef DEBUG_IRQ
648 DBG("%s: end_irq: %d\n", mpic->name, irq);
649#endif
650 /* We always EOI on end_irq() even for edge interrupts since that
651 * should only lower the priority, the MPIC should have properly
652 * latched another edge interrupt coming in anyway
653 */
654
655 if (irq_desc[irq].status & IRQ_LEVEL)
656 mpic_ht_end_irq(mpic, src);
657 mpic_eoi(mpic);
658}
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700659#endif /* !CONFIG_MPIC_BROKEN_U3 */
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000660
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000661#ifdef CONFIG_SMP
662
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000663static void mpic_unmask_ipi(unsigned int irq)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000664{
665 struct mpic *mpic = mpic_from_ipi(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000666 unsigned int src = mpic_irq_to_hw(irq) - MPIC_VEC_IPI_0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000667
668 DBG("%s: enable_ipi: %d (ipi %d)\n", mpic->name, irq, src);
669 mpic_ipi_write(src, mpic_ipi_read(src) & ~MPIC_VECPRI_MASK);
670}
671
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000672static void mpic_mask_ipi(unsigned int irq)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000673{
674 /* NEVER disable an IPI... that's just plain wrong! */
675}
676
677static void mpic_end_ipi(unsigned int irq)
678{
679 struct mpic *mpic = mpic_from_ipi(irq);
680
681 /*
682 * IPIs are marked IRQ_PER_CPU. This has the side effect of
683 * preventing the IRQ_PENDING/IRQ_INPROGRESS logic from
684 * applying to them. We EOI them late to avoid re-entering.
Thomas Gleixner67144652006-07-01 19:29:22 -0700685 * We mark IPI's with IRQF_DISABLED as they must run with
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000686 * irqs disabled.
687 */
688 mpic_eoi(mpic);
689}
690
691#endif /* CONFIG_SMP */
692
693static void mpic_set_affinity(unsigned int irq, cpumask_t cpumask)
694{
695 struct mpic *mpic = mpic_from_irq(irq);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000696 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000697
698 cpumask_t tmp;
699
700 cpus_and(tmp, cpumask, cpu_online_map);
701
Zang Roy-r6191172335932006-08-25 14:16:30 +1000702 mpic_irq_write(src, MPIC_INFO(IRQ_DESTINATION),
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000703 mpic_physmask(cpus_addr(tmp)[0]));
704}
705
Zang Roy-r6191172335932006-08-25 14:16:30 +1000706static unsigned int mpic_type_to_vecpri(struct mpic *mpic, unsigned int type)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000707{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000708 /* Now convert sense value */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700709 switch(type & IRQ_TYPE_SENSE_MASK) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000710 case IRQ_TYPE_EDGE_RISING:
Zang Roy-r6191172335932006-08-25 14:16:30 +1000711 return MPIC_INFO(VECPRI_SENSE_EDGE) |
712 MPIC_INFO(VECPRI_POLARITY_POSITIVE);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000713 case IRQ_TYPE_EDGE_FALLING:
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700714 case IRQ_TYPE_EDGE_BOTH:
Zang Roy-r6191172335932006-08-25 14:16:30 +1000715 return MPIC_INFO(VECPRI_SENSE_EDGE) |
716 MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000717 case IRQ_TYPE_LEVEL_HIGH:
Zang Roy-r6191172335932006-08-25 14:16:30 +1000718 return MPIC_INFO(VECPRI_SENSE_LEVEL) |
719 MPIC_INFO(VECPRI_POLARITY_POSITIVE);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000720 case IRQ_TYPE_LEVEL_LOW:
721 default:
Zang Roy-r6191172335932006-08-25 14:16:30 +1000722 return MPIC_INFO(VECPRI_SENSE_LEVEL) |
723 MPIC_INFO(VECPRI_POLARITY_NEGATIVE);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000724 }
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700725}
726
727static int mpic_set_irq_type(unsigned int virq, unsigned int flow_type)
728{
729 struct mpic *mpic = mpic_from_irq(virq);
730 unsigned int src = mpic_irq_to_hw(virq);
731 struct irq_desc *desc = get_irq_desc(virq);
732 unsigned int vecpri, vold, vnew;
733
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700734 DBG("mpic: set_irq_type(mpic:@%p,virq:%d,src:0x%x,type:0x%x)\n",
735 mpic, virq, src, flow_type);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700736
737 if (src >= mpic->irq_count)
738 return -EINVAL;
739
740 if (flow_type == IRQ_TYPE_NONE)
741 if (mpic->senses && src < mpic->senses_count)
742 flow_type = mpic->senses[src];
743 if (flow_type == IRQ_TYPE_NONE)
744 flow_type = IRQ_TYPE_LEVEL_LOW;
745
746 desc->status &= ~(IRQ_TYPE_SENSE_MASK | IRQ_LEVEL);
747 desc->status |= flow_type & IRQ_TYPE_SENSE_MASK;
748 if (flow_type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW))
749 desc->status |= IRQ_LEVEL;
750
751 if (mpic_is_ht_interrupt(mpic, src))
752 vecpri = MPIC_VECPRI_POLARITY_POSITIVE |
753 MPIC_VECPRI_SENSE_EDGE;
754 else
Zang Roy-r6191172335932006-08-25 14:16:30 +1000755 vecpri = mpic_type_to_vecpri(mpic, flow_type);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700756
Zang Roy-r6191172335932006-08-25 14:16:30 +1000757 vold = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
758 vnew = vold & ~(MPIC_INFO(VECPRI_POLARITY_MASK) |
759 MPIC_INFO(VECPRI_SENSE_MASK));
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700760 vnew |= vecpri;
761 if (vold != vnew)
Zang Roy-r6191172335932006-08-25 14:16:30 +1000762 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI), vnew);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700763
764 return 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000765}
766
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000767static struct irq_chip mpic_irq_chip = {
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700768 .mask = mpic_mask_irq,
769 .unmask = mpic_unmask_irq,
770 .eoi = mpic_end_irq,
771 .set_type = mpic_set_irq_type,
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000772};
773
774#ifdef CONFIG_SMP
775static struct irq_chip mpic_ipi_chip = {
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700776 .mask = mpic_mask_ipi,
777 .unmask = mpic_unmask_ipi,
778 .eoi = mpic_end_ipi,
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000779};
780#endif /* CONFIG_SMP */
781
782#ifdef CONFIG_MPIC_BROKEN_U3
783static struct irq_chip mpic_irq_ht_chip = {
784 .startup = mpic_startup_ht_irq,
785 .shutdown = mpic_shutdown_ht_irq,
786 .mask = mpic_mask_irq,
787 .unmask = mpic_unmask_ht_irq,
788 .eoi = mpic_end_ht_irq,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700789 .set_type = mpic_set_irq_type,
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000790};
791#endif /* CONFIG_MPIC_BROKEN_U3 */
792
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000793
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000794static int mpic_host_match(struct irq_host *h, struct device_node *node)
795{
796 struct mpic *mpic = h->host_data;
797
798 /* Exact match, unless mpic node is NULL */
799 return mpic->of_node == NULL || mpic->of_node == node;
800}
801
802static int mpic_host_map(struct irq_host *h, unsigned int virq,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700803 irq_hw_number_t hw)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000804{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000805 struct mpic *mpic = h->host_data;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700806 struct irq_chip *chip;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000807
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700808 DBG("mpic: map virq %d, hwirq 0x%lx\n", virq, hw);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000809
810 if (hw == MPIC_VEC_SPURRIOUS)
811 return -EINVAL;
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700812
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000813#ifdef CONFIG_SMP
814 else if (hw >= MPIC_VEC_IPI_0) {
815 WARN_ON(!(mpic->flags & MPIC_PRIMARY));
816
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700817 DBG("mpic: mapping as IPI\n");
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000818 set_irq_chip_data(virq, mpic);
819 set_irq_chip_and_handler(virq, &mpic->hc_ipi,
820 handle_percpu_irq);
821 return 0;
822 }
823#endif /* CONFIG_SMP */
824
825 if (hw >= mpic->irq_count)
826 return -EINVAL;
827
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700828 /* Default chip */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000829 chip = &mpic->hc_irq;
830
831#ifdef CONFIG_MPIC_BROKEN_U3
832 /* Check for HT interrupts, override vecpri */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700833 if (mpic_is_ht_interrupt(mpic, hw))
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000834 chip = &mpic->hc_ht_irq;
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700835#endif /* CONFIG_MPIC_BROKEN_U3 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000836
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700837 DBG("mpic: mapping to irq chip @%p\n", chip);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000838
839 set_irq_chip_data(virq, mpic);
840 set_irq_chip_and_handler(virq, chip, handle_fasteoi_irq);
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -0700841
842 /* Set default irq type */
843 set_irq_type(virq, IRQ_TYPE_NONE);
844
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000845 return 0;
846}
847
848static int mpic_host_xlate(struct irq_host *h, struct device_node *ct,
849 u32 *intspec, unsigned int intsize,
850 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
851
852{
853 static unsigned char map_mpic_senses[4] = {
854 IRQ_TYPE_EDGE_RISING,
855 IRQ_TYPE_LEVEL_LOW,
856 IRQ_TYPE_LEVEL_HIGH,
857 IRQ_TYPE_EDGE_FALLING,
858 };
859
860 *out_hwirq = intspec[0];
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700861 if (intsize > 1) {
862 u32 mask = 0x3;
863
864 /* Apple invented a new race of encoding on machines with
865 * an HT APIC. They encode, among others, the index within
866 * the HT APIC. We don't care about it here since thankfully,
867 * it appears that they have the APIC already properly
868 * configured, and thus our current fixup code that reads the
869 * APIC config works fine. However, we still need to mask out
870 * bits in the specifier to make sure we only get bit 0 which
871 * is the level/edge bit (the only sense bit exposed by Apple),
872 * as their bit 1 means something else.
873 */
874 if (machine_is(powermac))
875 mask = 0x1;
876 *out_flags = map_mpic_senses[intspec[1] & mask];
877 } else
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000878 *out_flags = IRQ_TYPE_NONE;
879
Benjamin Herrenschmidt06fe98e2006-07-10 04:44:43 -0700880 DBG("mpic: xlate (%d cells: 0x%08x 0x%08x) to line 0x%lx sense 0x%x\n",
881 intsize, intspec[0], intspec[1], *out_hwirq, *out_flags);
882
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000883 return 0;
884}
885
886static struct irq_host_ops mpic_host_ops = {
887 .match = mpic_host_match,
888 .map = mpic_host_map,
889 .xlate = mpic_host_xlate,
890};
891
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000892/*
893 * Exported functions
894 */
895
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000896struct mpic * __init mpic_alloc(struct device_node *node,
897 unsigned long phys_addr,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000898 unsigned int flags,
899 unsigned int isu_size,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000900 unsigned int irq_count,
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000901 const char *name)
902{
903 struct mpic *mpic;
904 u32 reg;
905 const char *vers;
906 int i;
907
908 mpic = alloc_bootmem(sizeof(struct mpic));
909 if (mpic == NULL)
910 return NULL;
911
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000912 memset(mpic, 0, sizeof(struct mpic));
913 mpic->name = name;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000914 mpic->of_node = node ? of_node_get(node) : NULL;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000915
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000916 mpic->irqhost = irq_alloc_host(IRQ_HOST_MAP_LINEAR, 256,
917 &mpic_host_ops,
918 MPIC_VEC_SPURRIOUS);
919 if (mpic->irqhost == NULL) {
920 of_node_put(node);
921 return NULL;
922 }
923
924 mpic->irqhost->host_data = mpic;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000925 mpic->hc_irq = mpic_irq_chip;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000926 mpic->hc_irq.typename = name;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000927 if (flags & MPIC_PRIMARY)
928 mpic->hc_irq.set_affinity = mpic_set_affinity;
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000929#ifdef CONFIG_MPIC_BROKEN_U3
930 mpic->hc_ht_irq = mpic_irq_ht_chip;
931 mpic->hc_ht_irq.typename = name;
932 if (flags & MPIC_PRIMARY)
933 mpic->hc_ht_irq.set_affinity = mpic_set_affinity;
934#endif /* CONFIG_MPIC_BROKEN_U3 */
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100935
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000936#ifdef CONFIG_SMP
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000937 mpic->hc_ipi = mpic_ipi_chip;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000938 mpic->hc_ipi.typename = name;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000939#endif /* CONFIG_SMP */
940
941 mpic->flags = flags;
942 mpic->isu_size = isu_size;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000943 mpic->irq_count = irq_count;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000944 mpic->num_sources = 0; /* so far */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000945
Zang Roy-r6191172335932006-08-25 14:16:30 +1000946#ifdef CONFIG_MPIC_WEIRD
947 mpic->hw_set = mpic_infos[MPIC_GET_REGSET(flags)];
948#endif
949
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100950 /* default register type */
951 mpic->reg_type = (flags & MPIC_BIG_ENDIAN) ?
952 mpic_access_mmio_be : mpic_access_mmio_le;
953
954#ifdef CONFIG_PPC_DCR
955 if (mpic->flags & MPIC_USES_DCR) {
956 const u32 *dbasep;
957 BUG_ON(mpic->of_node == NULL);
958 dbasep = get_property(mpic->of_node, "dcr-reg", NULL);
959 BUG_ON(dbasep == NULL);
960 mpic->dcr_base = *dbasep;
961 mpic->reg_type = mpic_access_dcr;
962 }
963#else
964 BUG_ON (mpic->flags & MPIC_USES_DCR);
965#endif /* CONFIG_PPC_DCR */
966
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000967 /* Map the global registers */
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100968 mpic_map(mpic, phys_addr, &mpic->gregs, MPIC_INFO(GREG_BASE), 0x1000);
969 mpic_map(mpic, phys_addr, &mpic->tmregs, MPIC_INFO(TIMER_BASE), 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000970
971 /* Reset */
972 if (flags & MPIC_WANTS_RESET) {
Zang Roy-r6191172335932006-08-25 14:16:30 +1000973 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
974 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000975 | MPIC_GREG_GCONF_RESET);
Zang Roy-r6191172335932006-08-25 14:16:30 +1000976 while( mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000977 & MPIC_GREG_GCONF_RESET)
978 mb();
979 }
980
981 /* Read feature register, calculate num CPUs and, for non-ISU
982 * MPICs, num sources as well. On ISU MPICs, sources are counted
983 * as ISUs are added
984 */
Zang Roy-r6191172335932006-08-25 14:16:30 +1000985 reg = mpic_read(mpic->gregs, MPIC_INFO(GREG_FEATURE_0));
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000986 mpic->num_cpus = ((reg & MPIC_GREG_FEATURE_LAST_CPU_MASK)
987 >> MPIC_GREG_FEATURE_LAST_CPU_SHIFT) + 1;
988 if (isu_size == 0)
989 mpic->num_sources = ((reg & MPIC_GREG_FEATURE_LAST_SRC_MASK)
990 >> MPIC_GREG_FEATURE_LAST_SRC_SHIFT) + 1;
991
992 /* Map the per-CPU registers */
993 for (i = 0; i < mpic->num_cpus; i++) {
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +1100994 mpic_map(mpic, phys_addr, &mpic->cpuregs[i],
995 MPIC_INFO(CPU_BASE) + i * MPIC_INFO(CPU_STRIDE),
996 0x1000);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000997 }
998
999 /* Initialize main ISU if none provided */
1000 if (mpic->isu_size == 0) {
1001 mpic->isu_size = mpic->num_sources;
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +11001002 mpic_map(mpic, phys_addr, &mpic->isus[0],
1003 MPIC_INFO(IRQ_BASE), MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001004 }
1005 mpic->isu_shift = 1 + __ilog2(mpic->isu_size - 1);
1006 mpic->isu_mask = (1 << mpic->isu_shift) - 1;
1007
1008 /* Display version */
1009 switch (reg & MPIC_GREG_FEATURE_VERSION_MASK) {
1010 case 1:
1011 vers = "1.0";
1012 break;
1013 case 2:
1014 vers = "1.2";
1015 break;
1016 case 3:
1017 vers = "1.3";
1018 break;
1019 default:
1020 vers = "<unknown>";
1021 break;
1022 }
1023 printk(KERN_INFO "mpic: Setting up MPIC \"%s\" version %s at %lx, max %d CPUs\n",
1024 name, vers, phys_addr, mpic->num_cpus);
1025 printk(KERN_INFO "mpic: ISU size: %d, shift: %d, mask: %x\n", mpic->isu_size,
1026 mpic->isu_shift, mpic->isu_mask);
1027
1028 mpic->next = mpics;
1029 mpics = mpic;
1030
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001031 if (flags & MPIC_PRIMARY) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001032 mpic_primary = mpic;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001033 irq_set_default_host(mpic->irqhost);
1034 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001035
1036 return mpic;
1037}
1038
1039void __init mpic_assign_isu(struct mpic *mpic, unsigned int isu_num,
1040 unsigned long phys_addr)
1041{
1042 unsigned int isu_first = isu_num * mpic->isu_size;
1043
1044 BUG_ON(isu_num >= MPIC_MAX_ISU);
1045
Benjamin Herrenschmidtfbf02742006-11-11 17:24:55 +11001046 mpic_map(mpic, phys_addr, &mpic->isus[isu_num], 0,
1047 MPIC_INFO(IRQ_STRIDE) * mpic->isu_size);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001048 if ((isu_first + mpic->isu_size) > mpic->num_sources)
1049 mpic->num_sources = isu_first + mpic->isu_size;
1050}
1051
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001052void __init mpic_set_default_senses(struct mpic *mpic, u8 *senses, int count)
1053{
1054 mpic->senses = senses;
1055 mpic->senses_count = count;
1056}
1057
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001058void __init mpic_init(struct mpic *mpic)
1059{
1060 int i;
1061
1062 BUG_ON(mpic->num_sources == 0);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001063 WARN_ON(mpic->num_sources > MPIC_VEC_IPI_0);
1064
1065 /* Sanitize source count */
1066 if (mpic->num_sources > MPIC_VEC_IPI_0)
1067 mpic->num_sources = MPIC_VEC_IPI_0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001068
1069 printk(KERN_INFO "mpic: Initializing for %d sources\n", mpic->num_sources);
1070
1071 /* Set current processor priority to max */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001072 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001073
1074 /* Initialize timers: just disable them all */
1075 for (i = 0; i < 4; i++) {
1076 mpic_write(mpic->tmregs,
Zang Roy-r6191172335932006-08-25 14:16:30 +10001077 i * MPIC_INFO(TIMER_STRIDE) +
1078 MPIC_INFO(TIMER_DESTINATION), 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001079 mpic_write(mpic->tmregs,
Zang Roy-r6191172335932006-08-25 14:16:30 +10001080 i * MPIC_INFO(TIMER_STRIDE) +
1081 MPIC_INFO(TIMER_VECTOR_PRI),
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001082 MPIC_VECPRI_MASK |
1083 (MPIC_VEC_TIMER_0 + i));
1084 }
1085
1086 /* Initialize IPIs to our reserved vectors and mark them disabled for now */
1087 mpic_test_broken_ipi(mpic);
1088 for (i = 0; i < 4; i++) {
1089 mpic_ipi_write(i,
1090 MPIC_VECPRI_MASK |
1091 (10 << MPIC_VECPRI_PRIORITY_SHIFT) |
1092 (MPIC_VEC_IPI_0 + i));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001093 }
1094
1095 /* Initialize interrupt sources */
1096 if (mpic->irq_count == 0)
1097 mpic->irq_count = mpic->num_sources;
1098
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001099 /* Do the HT PIC fixups on U3 broken mpic */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001100 DBG("MPIC flags: %x\n", mpic->flags);
1101 if ((mpic->flags & MPIC_BROKEN_U3) && (mpic->flags & MPIC_PRIMARY))
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +10001102 mpic_scan_ht_pics(mpic);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001103
1104 for (i = 0; i < mpic->num_sources; i++) {
1105 /* start with vector = source number, and masked */
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -07001106 u32 vecpri = MPIC_VECPRI_MASK | i |
1107 (8 << MPIC_VECPRI_PRIORITY_SHIFT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001108
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001109 /* init hw */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001110 mpic_irq_write(i, MPIC_INFO(IRQ_VECTOR_PRI), vecpri);
1111 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001112 1 << hard_smp_processor_id());
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001113 }
1114
1115 /* Init spurrious vector */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001116 mpic_write(mpic->gregs, MPIC_INFO(GREG_SPURIOUS), MPIC_VEC_SPURRIOUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001117
Zang Roy-r6191172335932006-08-25 14:16:30 +10001118 /* Disable 8259 passthrough, if supported */
1119 if (!(mpic->flags & MPIC_NO_PTHROU_DIS))
1120 mpic_write(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0),
1121 mpic_read(mpic->gregs, MPIC_INFO(GREG_GLOBAL_CONF_0))
1122 | MPIC_GREG_GCONF_8259_PTHROU_DIS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001123
1124 /* Set current processor priority to 0 */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001125 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001126}
1127
Mark A. Greer868ea0c2006-06-20 14:15:36 -07001128void __init mpic_set_clk_ratio(struct mpic *mpic, u32 clock_ratio)
1129{
1130 u32 v;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001131
Mark A. Greer868ea0c2006-06-20 14:15:36 -07001132 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1133 v &= ~MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO_MASK;
1134 v |= MPIC_GREG_GLOBAL_CONF_1_CLK_RATIO(clock_ratio);
1135 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
1136}
1137
1138void __init mpic_set_serial_int(struct mpic *mpic, int enable)
1139{
Benjamin Herrenschmidtba1826e2006-07-05 15:36:15 +10001140 unsigned long flags;
Mark A. Greer868ea0c2006-06-20 14:15:36 -07001141 u32 v;
1142
Benjamin Herrenschmidtba1826e2006-07-05 15:36:15 +10001143 spin_lock_irqsave(&mpic_lock, flags);
Mark A. Greer868ea0c2006-06-20 14:15:36 -07001144 v = mpic_read(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1);
1145 if (enable)
1146 v |= MPIC_GREG_GLOBAL_CONF_1_SIE;
1147 else
1148 v &= ~MPIC_GREG_GLOBAL_CONF_1_SIE;
1149 mpic_write(mpic->gregs, MPIC_GREG_GLOBAL_CONF_1, v);
Benjamin Herrenschmidtba1826e2006-07-05 15:36:15 +10001150 spin_unlock_irqrestore(&mpic_lock, flags);
Mark A. Greer868ea0c2006-06-20 14:15:36 -07001151}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001152
1153void mpic_irq_set_priority(unsigned int irq, unsigned int pri)
1154{
1155 int is_ipi;
1156 struct mpic *mpic = mpic_find(irq, &is_ipi);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001157 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001158 unsigned long flags;
1159 u32 reg;
1160
1161 spin_lock_irqsave(&mpic_lock, flags);
1162 if (is_ipi) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001163 reg = mpic_ipi_read(src - MPIC_VEC_IPI_0) &
Benjamin Herrenschmidte5356642005-11-18 17:18:15 +11001164 ~MPIC_VECPRI_PRIORITY_MASK;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001165 mpic_ipi_write(src - MPIC_VEC_IPI_0,
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001166 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1167 } else {
Zang Roy-r6191172335932006-08-25 14:16:30 +10001168 reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI))
Benjamin Herrenschmidte5356642005-11-18 17:18:15 +11001169 & ~MPIC_VECPRI_PRIORITY_MASK;
Zang Roy-r6191172335932006-08-25 14:16:30 +10001170 mpic_irq_write(src, MPIC_INFO(IRQ_VECTOR_PRI),
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001171 reg | (pri << MPIC_VECPRI_PRIORITY_SHIFT));
1172 }
1173 spin_unlock_irqrestore(&mpic_lock, flags);
1174}
1175
1176unsigned int mpic_irq_get_priority(unsigned int irq)
1177{
1178 int is_ipi;
1179 struct mpic *mpic = mpic_find(irq, &is_ipi);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001180 unsigned int src = mpic_irq_to_hw(irq);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001181 unsigned long flags;
1182 u32 reg;
1183
1184 spin_lock_irqsave(&mpic_lock, flags);
1185 if (is_ipi)
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001186 reg = mpic_ipi_read(src = MPIC_VEC_IPI_0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001187 else
Zang Roy-r6191172335932006-08-25 14:16:30 +10001188 reg = mpic_irq_read(src, MPIC_INFO(IRQ_VECTOR_PRI));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001189 spin_unlock_irqrestore(&mpic_lock, flags);
1190 return (reg & MPIC_VECPRI_PRIORITY_MASK) >> MPIC_VECPRI_PRIORITY_SHIFT;
1191}
1192
1193void mpic_setup_this_cpu(void)
1194{
1195#ifdef CONFIG_SMP
1196 struct mpic *mpic = mpic_primary;
1197 unsigned long flags;
1198 u32 msk = 1 << hard_smp_processor_id();
1199 unsigned int i;
1200
1201 BUG_ON(mpic == NULL);
1202
1203 DBG("%s: setup_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1204
1205 spin_lock_irqsave(&mpic_lock, flags);
1206
1207 /* let the mpic know we want intrs. default affinity is 0xffffffff
1208 * until changed via /proc. That's how it's done on x86. If we want
1209 * it differently, then we should make sure we also change the default
Ingo Molnara53da522006-06-29 02:24:38 -07001210 * values of irq_desc[].affinity in irq.c.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001211 */
1212 if (distribute_irqs) {
1213 for (i = 0; i < mpic->num_sources ; i++)
Zang Roy-r6191172335932006-08-25 14:16:30 +10001214 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1215 mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) | msk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001216 }
1217
1218 /* Set current processor priority to 0 */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001219 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001220
1221 spin_unlock_irqrestore(&mpic_lock, flags);
1222#endif /* CONFIG_SMP */
1223}
1224
1225int mpic_cpu_get_priority(void)
1226{
1227 struct mpic *mpic = mpic_primary;
1228
Zang Roy-r6191172335932006-08-25 14:16:30 +10001229 return mpic_cpu_read(MPIC_INFO(CPU_CURRENT_TASK_PRI));
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001230}
1231
1232void mpic_cpu_set_priority(int prio)
1233{
1234 struct mpic *mpic = mpic_primary;
1235
1236 prio &= MPIC_CPU_TASKPRI_MASK;
Zang Roy-r6191172335932006-08-25 14:16:30 +10001237 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), prio);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001238}
1239
1240/*
1241 * XXX: someone who knows mpic should check this.
1242 * do we need to eoi the ipi including for kexec cpu here (see xics comments)?
1243 * or can we reset the mpic in the new kernel?
1244 */
1245void mpic_teardown_this_cpu(int secondary)
1246{
1247 struct mpic *mpic = mpic_primary;
1248 unsigned long flags;
1249 u32 msk = 1 << hard_smp_processor_id();
1250 unsigned int i;
1251
1252 BUG_ON(mpic == NULL);
1253
1254 DBG("%s: teardown_this_cpu(%d)\n", mpic->name, hard_smp_processor_id());
1255 spin_lock_irqsave(&mpic_lock, flags);
1256
1257 /* let the mpic know we don't want intrs. */
1258 for (i = 0; i < mpic->num_sources ; i++)
Zang Roy-r6191172335932006-08-25 14:16:30 +10001259 mpic_irq_write(i, MPIC_INFO(IRQ_DESTINATION),
1260 mpic_irq_read(i, MPIC_INFO(IRQ_DESTINATION)) & ~msk);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001261
1262 /* Set current processor priority to max */
Zang Roy-r6191172335932006-08-25 14:16:30 +10001263 mpic_cpu_write(MPIC_INFO(CPU_CURRENT_TASK_PRI), 0xf);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001264
1265 spin_unlock_irqrestore(&mpic_lock, flags);
1266}
1267
1268
1269void mpic_send_ipi(unsigned int ipi_no, unsigned int cpu_mask)
1270{
1271 struct mpic *mpic = mpic_primary;
1272
1273 BUG_ON(mpic == NULL);
1274
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001275#ifdef DEBUG_IPI
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001276 DBG("%s: send_ipi(ipi_no: %d)\n", mpic->name, ipi_no);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001277#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001278
Zang Roy-r6191172335932006-08-25 14:16:30 +10001279 mpic_cpu_write(MPIC_INFO(CPU_IPI_DISPATCH_0) +
1280 ipi_no * MPIC_INFO(CPU_IPI_DISPATCH_STRIDE),
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001281 mpic_physmask(cpu_mask & cpus_addr(cpu_online_map)[0]));
1282}
1283
Olaf Hering35a84c22006-10-07 22:08:26 +10001284unsigned int mpic_get_one_irq(struct mpic *mpic)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001285{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001286 u32 src;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001287
Zang Roy-r6191172335932006-08-25 14:16:30 +10001288 src = mpic_cpu_read(MPIC_INFO(CPU_INTACK)) & MPIC_INFO(VECPRI_VECTOR_MASK);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001289#ifdef DEBUG_LOW
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001290 DBG("%s: get_one_irq(): %d\n", mpic->name, src);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +11001291#endif
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001292 if (unlikely(src == MPIC_VEC_SPURRIOUS))
1293 return NO_IRQ;
1294 return irq_linear_revmap(mpic->irqhost, src);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001295}
1296
Olaf Hering35a84c22006-10-07 22:08:26 +10001297unsigned int mpic_get_irq(void)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001298{
1299 struct mpic *mpic = mpic_primary;
1300
1301 BUG_ON(mpic == NULL);
1302
Olaf Hering35a84c22006-10-07 22:08:26 +10001303 return mpic_get_one_irq(mpic);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001304}
1305
1306
1307#ifdef CONFIG_SMP
1308void mpic_request_ipis(void)
1309{
1310 struct mpic *mpic = mpic_primary;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001311 int i;
1312 static char *ipi_names[] = {
1313 "IPI0 (call function)",
1314 "IPI1 (reschedule)",
1315 "IPI2 (unused)",
1316 "IPI3 (debugger break)",
1317 };
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001318 BUG_ON(mpic == NULL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001319
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001320 printk(KERN_INFO "mpic: requesting IPIs ... \n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001321
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001322 for (i = 0; i < 4; i++) {
1323 unsigned int vipi = irq_create_mapping(mpic->irqhost,
Benjamin Herrenschmidt6e99e452006-07-10 04:44:42 -07001324 MPIC_VEC_IPI_0 + i);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +10001325 if (vipi == NO_IRQ) {
1326 printk(KERN_ERR "Failed to map IPI %d\n", i);
1327 break;
1328 }
1329 request_irq(vipi, mpic_ipi_action, IRQF_DISABLED,
1330 ipi_names[i], mpic);
1331 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001332}
Paul Mackerrasa9c59262005-10-20 17:09:51 +10001333
1334void smp_mpic_message_pass(int target, int msg)
1335{
1336 /* make sure we're sending something that translates to an IPI */
1337 if ((unsigned int)msg > 3) {
1338 printk("SMP %d: smp_message_pass: unknown msg %d\n",
1339 smp_processor_id(), msg);
1340 return;
1341 }
1342 switch (target) {
1343 case MSG_ALL:
1344 mpic_send_ipi(msg, 0xffffffff);
1345 break;
1346 case MSG_ALL_BUT_SELF:
1347 mpic_send_ipi(msg, 0xffffffff & ~(1 << smp_processor_id()));
1348 break;
1349 default:
1350 mpic_send_ipi(msg, 1 << target);
1351 break;
1352 }
1353}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001354#endif /* CONFIG_SMP */