aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ppc64/kernel/xics.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/ppc64/kernel/xics.c')
-rw-r--r--arch/ppc64/kernel/xics.c713
1 files changed, 713 insertions, 0 deletions
diff --git a/arch/ppc64/kernel/xics.c b/arch/ppc64/kernel/xics.c
new file mode 100644
index 000000000000..eedd1d3c2a10
--- /dev/null
+++ b/arch/ppc64/kernel/xics.c
@@ -0,0 +1,713 @@
1/*
2 * arch/ppc64/kernel/xics.c
3 *
4 * Copyright 2000 IBM Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11#include <linux/config.h>
12#include <linux/types.h>
13#include <linux/threads.h>
14#include <linux/kernel.h>
15#include <linux/irq.h>
16#include <linux/smp.h>
17#include <linux/interrupt.h>
18#include <linux/signal.h>
19#include <linux/init.h>
20#include <linux/gfp.h>
21#include <linux/radix-tree.h>
22#include <linux/cpu.h>
23#include <asm/prom.h>
24#include <asm/io.h>
25#include <asm/pgtable.h>
26#include <asm/smp.h>
27#include <asm/rtas.h>
28#include <asm/xics.h>
29#include <asm/hvcall.h>
30#include <asm/machdep.h>
31
32#include "i8259.h"
33
34static unsigned int xics_startup(unsigned int irq);
35static void xics_enable_irq(unsigned int irq);
36static void xics_disable_irq(unsigned int irq);
37static void xics_mask_and_ack_irq(unsigned int irq);
38static void xics_end_irq(unsigned int irq);
39static void xics_set_affinity(unsigned int irq_nr, cpumask_t cpumask);
40
41struct hw_interrupt_type xics_pic = {
42 .typename = " XICS ",
43 .startup = xics_startup,
44 .enable = xics_enable_irq,
45 .disable = xics_disable_irq,
46 .ack = xics_mask_and_ack_irq,
47 .end = xics_end_irq,
48 .set_affinity = xics_set_affinity
49};
50
51struct hw_interrupt_type xics_8259_pic = {
52 .typename = " XICS/8259",
53 .ack = xics_mask_and_ack_irq,
54};
55
56/* This is used to map real irq numbers to virtual */
57static struct radix_tree_root irq_map = RADIX_TREE_INIT(GFP_ATOMIC);
58
59#define XICS_IPI 2
60#define XICS_IRQ_SPURIOUS 0
61
62/* Want a priority other than 0. Various HW issues require this. */
63#define DEFAULT_PRIORITY 5
64
65/*
66 * Mark IPIs as higher priority so we can take them inside interrupts that
67 * arent marked SA_INTERRUPT
68 */
69#define IPI_PRIORITY 4
70
71struct xics_ipl {
72 union {
73 u32 word;
74 u8 bytes[4];
75 } xirr_poll;
76 union {
77 u32 word;
78 u8 bytes[4];
79 } xirr;
80 u32 dummy;
81 union {
82 u32 word;
83 u8 bytes[4];
84 } qirr;
85};
86
87static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
88
89static int xics_irq_8259_cascade = 0;
90static int xics_irq_8259_cascade_real = 0;
91static unsigned int default_server = 0xFF;
92/* also referenced in smp.c... */
93unsigned int default_distrib_server = 0;
94unsigned int interrupt_server_size = 8;
95
96/*
97 * XICS only has a single IPI, so encode the messages per CPU
98 */
99struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
100
101/* RTAS service tokens */
102int ibm_get_xive;
103int ibm_set_xive;
104int ibm_int_on;
105int ibm_int_off;
106
107typedef struct {
108 int (*xirr_info_get)(int cpu);
109 void (*xirr_info_set)(int cpu, int val);
110 void (*cppr_info)(int cpu, u8 val);
111 void (*qirr_info)(int cpu, u8 val);
112} xics_ops;
113
114
115/* SMP */
116
117static int pSeries_xirr_info_get(int n_cpu)
118{
119 return in_be32(&xics_per_cpu[n_cpu]->xirr.word);
120}
121
122static void pSeries_xirr_info_set(int n_cpu, int value)
123{
124 out_be32(&xics_per_cpu[n_cpu]->xirr.word, value);
125}
126
127static void pSeries_cppr_info(int n_cpu, u8 value)
128{
129 out_8(&xics_per_cpu[n_cpu]->xirr.bytes[0], value);
130}
131
132static void pSeries_qirr_info(int n_cpu, u8 value)
133{
134 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
135}
136
137static xics_ops pSeries_ops = {
138 pSeries_xirr_info_get,
139 pSeries_xirr_info_set,
140 pSeries_cppr_info,
141 pSeries_qirr_info
142};
143
144static xics_ops *ops = &pSeries_ops;
145
146
147/* LPAR */
148
149static inline long plpar_eoi(unsigned long xirr)
150{
151 return plpar_hcall_norets(H_EOI, xirr);
152}
153
154static inline long plpar_cppr(unsigned long cppr)
155{
156 return plpar_hcall_norets(H_CPPR, cppr);
157}
158
159static inline long plpar_ipi(unsigned long servernum, unsigned long mfrr)
160{
161 return plpar_hcall_norets(H_IPI, servernum, mfrr);
162}
163
164static inline long plpar_xirr(unsigned long *xirr_ret)
165{
166 unsigned long dummy;
167 return plpar_hcall(H_XIRR, 0, 0, 0, 0, xirr_ret, &dummy, &dummy);
168}
169
170static int pSeriesLP_xirr_info_get(int n_cpu)
171{
172 unsigned long lpar_rc;
173 unsigned long return_value;
174
175 lpar_rc = plpar_xirr(&return_value);
176 if (lpar_rc != H_Success)
177 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
178 return (int)return_value;
179}
180
181static void pSeriesLP_xirr_info_set(int n_cpu, int value)
182{
183 unsigned long lpar_rc;
184 unsigned long val64 = value & 0xffffffff;
185
186 lpar_rc = plpar_eoi(val64);
187 if (lpar_rc != H_Success)
188 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
189 val64);
190}
191
192void pSeriesLP_cppr_info(int n_cpu, u8 value)
193{
194 unsigned long lpar_rc;
195
196 lpar_rc = plpar_cppr(value);
197 if (lpar_rc != H_Success)
198 panic("bad return code cppr - rc = %lx\n", lpar_rc);
199}
200
201static void pSeriesLP_qirr_info(int n_cpu , u8 value)
202{
203 unsigned long lpar_rc;
204
205 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
206 if (lpar_rc != H_Success)
207 panic("bad return code qirr - rc = %lx\n", lpar_rc);
208}
209
210xics_ops pSeriesLP_ops = {
211 pSeriesLP_xirr_info_get,
212 pSeriesLP_xirr_info_set,
213 pSeriesLP_cppr_info,
214 pSeriesLP_qirr_info
215};
216
217static unsigned int xics_startup(unsigned int virq)
218{
219 unsigned int irq;
220
221 irq = irq_offset_down(virq);
222 if (radix_tree_insert(&irq_map, virt_irq_to_real(irq),
223 &virt_irq_to_real_map[irq]) == -ENOMEM)
224 printk(KERN_CRIT "Out of memory creating real -> virtual"
225 " IRQ mapping for irq %u (real 0x%x)\n",
226 virq, virt_irq_to_real(irq));
227 xics_enable_irq(virq);
228 return 0; /* return value is ignored */
229}
230
231static unsigned int real_irq_to_virt(unsigned int real_irq)
232{
233 unsigned int *ptr;
234
235 ptr = radix_tree_lookup(&irq_map, real_irq);
236 if (ptr == NULL)
237 return NO_IRQ;
238 return ptr - virt_irq_to_real_map;
239}
240
241#ifdef CONFIG_SMP
242static int get_irq_server(unsigned int irq)
243{
244 unsigned int server;
245 /* For the moment only implement delivery to all cpus or one cpu */
246 cpumask_t cpumask = irq_affinity[irq];
247 cpumask_t tmp = CPU_MASK_NONE;
248
249 if (!distribute_irqs)
250 return default_server;
251
252 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
253 server = default_distrib_server;
254 } else {
255 cpus_and(tmp, cpu_online_map, cpumask);
256
257 if (cpus_empty(tmp))
258 server = default_distrib_server;
259 else
260 server = get_hard_smp_processor_id(first_cpu(tmp));
261 }
262
263 return server;
264
265}
266#else
267static int get_irq_server(unsigned int irq)
268{
269 return default_server;
270}
271#endif
272
273static void xics_enable_irq(unsigned int virq)
274{
275 unsigned int irq;
276 int call_status;
277 unsigned int server;
278
279 irq = virt_irq_to_real(irq_offset_down(virq));
280 if (irq == XICS_IPI)
281 return;
282
283 server = get_irq_server(virq);
284 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
285 DEFAULT_PRIORITY);
286 if (call_status != 0) {
287 printk(KERN_ERR "xics_enable_irq: irq=%d: ibm_set_xive "
288 "returned %x\n", irq, call_status);
289 return;
290 }
291
292 /* Now unmask the interrupt (often a no-op) */
293 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
294 if (call_status != 0) {
295 printk(KERN_ERR "xics_enable_irq: irq=%d: ibm_int_on "
296 "returned %x\n", irq, call_status);
297 return;
298 }
299}
300
301static void xics_disable_real_irq(unsigned int irq)
302{
303 int call_status;
304 unsigned int server;
305
306 if (irq == XICS_IPI)
307 return;
308
309 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
310 if (call_status != 0) {
311 printk(KERN_ERR "xics_disable_real_irq: irq=%d: "
312 "ibm_int_off returned %x\n", irq, call_status);
313 return;
314 }
315
316 server = get_irq_server(irq);
317 /* Have to set XIVE to 0xff to be able to remove a slot */
318 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server, 0xff);
319 if (call_status != 0) {
320 printk(KERN_ERR "xics_disable_irq: irq=%d: ibm_set_xive(0xff)"
321 " returned %x\n", irq, call_status);
322 return;
323 }
324}
325
326static void xics_disable_irq(unsigned int virq)
327{
328 unsigned int irq;
329
330 irq = virt_irq_to_real(irq_offset_down(virq));
331 xics_disable_real_irq(irq);
332}
333
334static void xics_end_irq(unsigned int irq)
335{
336 int cpu = smp_processor_id();
337
338 iosync();
339 ops->xirr_info_set(cpu, ((0xff << 24) |
340 (virt_irq_to_real(irq_offset_down(irq)))));
341
342}
343
344static void xics_mask_and_ack_irq(unsigned int irq)
345{
346 int cpu = smp_processor_id();
347
348 if (irq < irq_offset_value()) {
349 i8259_pic.ack(irq);
350 iosync();
351 ops->xirr_info_set(cpu, ((0xff<<24) |
352 xics_irq_8259_cascade_real));
353 iosync();
354 }
355}
356
357int xics_get_irq(struct pt_regs *regs)
358{
359 unsigned int cpu = smp_processor_id();
360 unsigned int vec;
361 int irq;
362
363 vec = ops->xirr_info_get(cpu);
364 /* (vec >> 24) == old priority */
365 vec &= 0x00ffffff;
366
367 /* for sanity, this had better be < NR_IRQS - 16 */
368 if (vec == xics_irq_8259_cascade_real) {
369 irq = i8259_irq(cpu);
370 if (irq == -1) {
371 /* Spurious cascaded interrupt. Still must ack xics */
372 xics_end_irq(irq_offset_up(xics_irq_8259_cascade));
373
374 irq = -1;
375 }
376 } else if (vec == XICS_IRQ_SPURIOUS) {
377 irq = -1;
378 } else {
379 irq = real_irq_to_virt(vec);
380 if (irq == NO_IRQ)
381 irq = real_irq_to_virt_slowpath(vec);
382 if (irq == NO_IRQ) {
383 printk(KERN_ERR "Interrupt %d (real) is invalid,"
384 " disabling it.\n", vec);
385 xics_disable_real_irq(vec);
386 } else
387 irq = irq_offset_up(irq);
388 }
389 return irq;
390}
391
392#ifdef CONFIG_SMP
393
394irqreturn_t xics_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
395{
396 int cpu = smp_processor_id();
397
398 ops->qirr_info(cpu, 0xff);
399
400 WARN_ON(cpu_is_offline(cpu));
401
402 while (xics_ipi_message[cpu].value) {
403 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
404 &xics_ipi_message[cpu].value)) {
405 mb();
406 smp_message_recv(PPC_MSG_CALL_FUNCTION, regs);
407 }
408 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
409 &xics_ipi_message[cpu].value)) {
410 mb();
411 smp_message_recv(PPC_MSG_RESCHEDULE, regs);
412 }
413#if 0
414 if (test_and_clear_bit(PPC_MSG_MIGRATE_TASK,
415 &xics_ipi_message[cpu].value)) {
416 mb();
417 smp_message_recv(PPC_MSG_MIGRATE_TASK, regs);
418 }
419#endif
420#ifdef CONFIG_DEBUGGER
421 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
422 &xics_ipi_message[cpu].value)) {
423 mb();
424 smp_message_recv(PPC_MSG_DEBUGGER_BREAK, regs);
425 }
426#endif
427 }
428 return IRQ_HANDLED;
429}
430
431void xics_cause_IPI(int cpu)
432{
433 ops->qirr_info(cpu, IPI_PRIORITY);
434}
435
436void xics_setup_cpu(void)
437{
438 int cpu = smp_processor_id();
439
440 ops->cppr_info(cpu, 0xff);
441 iosync();
442}
443
444#endif /* CONFIG_SMP */
445
446void xics_init_IRQ(void)
447{
448 int i;
449 unsigned long intr_size = 0;
450 struct device_node *np;
451 uint *ireg, ilen, indx = 0;
452 unsigned long intr_base = 0;
453 struct xics_interrupt_node {
454 unsigned long addr;
455 unsigned long size;
456 } intnodes[NR_CPUS];
457
458 ppc64_boot_msg(0x20, "XICS Init");
459
460 ibm_get_xive = rtas_token("ibm,get-xive");
461 ibm_set_xive = rtas_token("ibm,set-xive");
462 ibm_int_on = rtas_token("ibm,int-on");
463 ibm_int_off = rtas_token("ibm,int-off");
464
465 np = of_find_node_by_type(NULL, "PowerPC-External-Interrupt-Presentation");
466 if (!np)
467 panic("xics_init_IRQ: can't find interrupt presentation");
468
469nextnode:
470 ireg = (uint *)get_property(np, "ibm,interrupt-server-ranges", NULL);
471 if (ireg) {
472 /*
473 * set node starting index for this node
474 */
475 indx = *ireg;
476 }
477
478 ireg = (uint *)get_property(np, "reg", &ilen);
479 if (!ireg)
480 panic("xics_init_IRQ: can't find interrupt reg property");
481
482 while (ilen) {
483 intnodes[indx].addr = (unsigned long)*ireg++ << 32;
484 ilen -= sizeof(uint);
485 intnodes[indx].addr |= *ireg++;
486 ilen -= sizeof(uint);
487 intnodes[indx].size = (unsigned long)*ireg++ << 32;
488 ilen -= sizeof(uint);
489 intnodes[indx].size |= *ireg++;
490 ilen -= sizeof(uint);
491 indx++;
492 if (indx >= NR_CPUS) break;
493 }
494
495 np = of_find_node_by_type(np, "PowerPC-External-Interrupt-Presentation");
496 if ((indx < NR_CPUS) && np) goto nextnode;
497
498 /* Find the server numbers for the boot cpu. */
499 for (np = of_find_node_by_type(NULL, "cpu");
500 np;
501 np = of_find_node_by_type(np, "cpu")) {
502 ireg = (uint *)get_property(np, "reg", &ilen);
503 if (ireg && ireg[0] == boot_cpuid_phys) {
504 ireg = (uint *)get_property(np, "ibm,ppc-interrupt-gserver#s",
505 &ilen);
506 i = ilen / sizeof(int);
507 if (ireg && i > 0) {
508 default_server = ireg[0];
509 default_distrib_server = ireg[i-1]; /* take last element */
510 }
511 ireg = (uint *)get_property(np,
512 "ibm,interrupt-server#-size", NULL);
513 if (ireg)
514 interrupt_server_size = *ireg;
515 break;
516 }
517 }
518 of_node_put(np);
519
520 intr_base = intnodes[0].addr;
521 intr_size = intnodes[0].size;
522
523 np = of_find_node_by_type(NULL, "interrupt-controller");
524 if (!np) {
525 printk(KERN_WARNING "xics: no ISA interrupt controller\n");
526 xics_irq_8259_cascade_real = -1;
527 xics_irq_8259_cascade = -1;
528 } else {
529 ireg = (uint *) get_property(np, "interrupts", NULL);
530 if (!ireg)
531 panic("xics_init_IRQ: can't find ISA interrupts property");
532
533 xics_irq_8259_cascade_real = *ireg;
534 xics_irq_8259_cascade
535 = virt_irq_create_mapping(xics_irq_8259_cascade_real);
536 of_node_put(np);
537 }
538
539 if (systemcfg->platform == PLATFORM_PSERIES) {
540#ifdef CONFIG_SMP
541 for_each_cpu(i) {
542 int hard_id;
543
544 /* FIXME: Do this dynamically! --RR */
545 if (!cpu_present(i))
546 continue;
547
548 hard_id = get_hard_smp_processor_id(i);
549 xics_per_cpu[i] = ioremap(intnodes[hard_id].addr,
550 intnodes[hard_id].size);
551 }
552#else
553 xics_per_cpu[0] = ioremap(intr_base, intr_size);
554#endif /* CONFIG_SMP */
555 } else if (systemcfg->platform == PLATFORM_PSERIES_LPAR) {
556 ops = &pSeriesLP_ops;
557 }
558
559 xics_8259_pic.enable = i8259_pic.enable;
560 xics_8259_pic.disable = i8259_pic.disable;
561 for (i = 0; i < 16; ++i)
562 get_irq_desc(i)->handler = &xics_8259_pic;
563 for (; i < NR_IRQS; ++i)
564 get_irq_desc(i)->handler = &xics_pic;
565
566 ops->cppr_info(boot_cpuid, 0xff);
567 iosync();
568
569 ppc64_boot_msg(0x21, "XICS Done");
570}
571
572/*
573 * We cant do this in init_IRQ because we need the memory subsystem up for
574 * request_irq()
575 */
576static int __init xics_setup_i8259(void)
577{
578 if (ppc64_interrupt_controller == IC_PPC_XIC &&
579 xics_irq_8259_cascade != -1) {
580 if (request_irq(irq_offset_up(xics_irq_8259_cascade),
581 no_action, 0, "8259 cascade", NULL))
582 printk(KERN_ERR "xics_setup_i8259: couldn't get 8259 "
583 "cascade\n");
584 i8259_init(0);
585 }
586 return 0;
587}
588arch_initcall(xics_setup_i8259);
589
590#ifdef CONFIG_SMP
591void xics_request_IPIs(void)
592{
593 virt_irq_to_real_map[XICS_IPI] = XICS_IPI;
594
595 /* IPIs are marked SA_INTERRUPT as they must run with irqs disabled */
596 request_irq(irq_offset_up(XICS_IPI), xics_ipi_action, SA_INTERRUPT,
597 "IPI", NULL);
598 get_irq_desc(irq_offset_up(XICS_IPI))->status |= IRQ_PER_CPU;
599}
600#endif
601
602static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
603{
604 unsigned int irq;
605 int status;
606 int xics_status[2];
607 unsigned long newmask;
608 cpumask_t tmp = CPU_MASK_NONE;
609
610 irq = virt_irq_to_real(irq_offset_down(virq));
611 if (irq == XICS_IPI || irq == NO_IRQ)
612 return;
613
614 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
615
616 if (status) {
617 printk(KERN_ERR "xics_set_affinity: irq=%d ibm,get-xive "
618 "returns %d\n", irq, status);
619 return;
620 }
621
622 /* For the moment only implement delivery to all cpus or one cpu */
623 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
624 newmask = default_distrib_server;
625 } else {
626 cpus_and(tmp, cpu_online_map, cpumask);
627 if (cpus_empty(tmp))
628 return;
629 newmask = get_hard_smp_processor_id(first_cpu(tmp));
630 }
631
632 status = rtas_call(ibm_set_xive, 3, 1, NULL,
633 irq, newmask, xics_status[1]);
634
635 if (status) {
636 printk(KERN_ERR "xics_set_affinity: irq=%d ibm,set-xive "
637 "returns %d\n", irq, status);
638 return;
639 }
640}
641
642#ifdef CONFIG_HOTPLUG_CPU
643
644/* Interrupts are disabled. */
645void xics_migrate_irqs_away(void)
646{
647 int status;
648 unsigned int irq, virq, cpu = smp_processor_id();
649
650 /* Reject any interrupt that was queued to us... */
651 ops->cppr_info(cpu, 0);
652 iosync();
653
654 /* remove ourselves from the global interrupt queue */
655 status = rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
656 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
657 WARN_ON(status < 0);
658
659 /* Allow IPIs again... */
660 ops->cppr_info(cpu, DEFAULT_PRIORITY);
661 iosync();
662
663 for_each_irq(virq) {
664 irq_desc_t *desc;
665 int xics_status[2];
666 unsigned long flags;
667
668 /* We cant set affinity on ISA interrupts */
669 if (virq < irq_offset_value())
670 continue;
671
672 desc = get_irq_desc(virq);
673 irq = virt_irq_to_real(irq_offset_down(virq));
674
675 /* We need to get IPIs still. */
676 if (irq == XICS_IPI || irq == NO_IRQ)
677 continue;
678
679 /* We only need to migrate enabled IRQS */
680 if (desc == NULL || desc->handler == NULL
681 || desc->action == NULL
682 || desc->handler->set_affinity == NULL)
683 continue;
684
685 spin_lock_irqsave(&desc->lock, flags);
686
687 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
688 if (status) {
689 printk(KERN_ERR "migrate_irqs_away: irq=%d "
690 "ibm,get-xive returns %d\n",
691 virq, status);
692 goto unlock;
693 }
694
695 /*
696 * We only support delivery to all cpus or to one cpu.
697 * The irq has to be migrated only in the single cpu
698 * case.
699 */
700 if (xics_status[0] != get_hard_smp_processor_id(cpu))
701 goto unlock;
702
703 printk(KERN_WARNING "IRQ %d affinity broken off cpu %u\n",
704 virq, cpu);
705
706 /* Reset affinity to all cpus */
707 desc->handler->set_affinity(virq, CPU_MASK_ALL);
708 irq_affinity[virq] = CPU_MASK_ALL;
709unlock:
710 spin_unlock_irqrestore(&desc->lock, flags);
711 }
712}
713#endif