2 * arch/powerpc/platforms/pseries/xics.c
4 * Copyright 2000 IBM Corporation.
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.
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/firmware.h>
26 #include <asm/pgtable.h>
29 #include <asm/hvcall.h>
30 #include <asm/machdep.h>
31 #include <asm/i8259.h>
35 static unsigned int xics_startup(unsigned int irq);
36 static void xics_enable_irq(unsigned int irq);
37 static void xics_disable_irq(unsigned int irq);
38 static void xics_mask_and_ack_irq(unsigned int irq);
39 static void xics_end_irq(unsigned int irq);
40 static void xics_set_affinity(unsigned int irq_nr, cpumask_t cpumask);
42 static struct hw_interrupt_type xics_pic = {
44 .startup = xics_startup,
45 .enable = xics_enable_irq,
46 .disable = xics_disable_irq,
47 .ack = xics_mask_and_ack_irq,
49 .set_affinity = xics_set_affinity
52 /* This is used to map real irq numbers to virtual */
53 static struct radix_tree_root irq_map = RADIX_TREE_INIT(GFP_ATOMIC);
56 #define XICS_IRQ_SPURIOUS 0
58 /* Want a priority other than 0. Various HW issues require this. */
59 #define DEFAULT_PRIORITY 5
62 * Mark IPIs as higher priority so we can take them inside interrupts that
63 * arent marked SA_INTERRUPT
65 #define IPI_PRIORITY 4
83 static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
85 static int xics_irq_8259_cascade = 0;
86 static int xics_irq_8259_cascade_real = 0;
87 static unsigned int default_server = 0xFF;
88 static unsigned int default_distrib_server = 0;
89 static unsigned int interrupt_server_size = 8;
92 * XICS only has a single IPI, so encode the messages per CPU
94 struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
96 /* RTAS service tokens */
97 static int ibm_get_xive;
98 static int ibm_set_xive;
99 static int ibm_int_on;
100 static int ibm_int_off;
103 int (*xirr_info_get)(int cpu);
104 void (*xirr_info_set)(int cpu, int val);
105 void (*cppr_info)(int cpu, u8 val);
106 void (*qirr_info)(int cpu, u8 val);
112 static int pSeries_xirr_info_get(int n_cpu)
114 return in_be32(&xics_per_cpu[n_cpu]->xirr.word);
117 static void pSeries_xirr_info_set(int n_cpu, int value)
119 out_be32(&xics_per_cpu[n_cpu]->xirr.word, value);
122 static void pSeries_cppr_info(int n_cpu, u8 value)
124 out_8(&xics_per_cpu[n_cpu]->xirr.bytes[0], value);
127 static void pSeries_qirr_info(int n_cpu, u8 value)
129 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
132 static xics_ops pSeries_ops = {
133 pSeries_xirr_info_get,
134 pSeries_xirr_info_set,
139 static xics_ops *ops = &pSeries_ops;
144 static inline long plpar_eoi(unsigned long xirr)
146 return plpar_hcall_norets(H_EOI, xirr);
149 static inline long plpar_cppr(unsigned long cppr)
151 return plpar_hcall_norets(H_CPPR, cppr);
154 static inline long plpar_ipi(unsigned long servernum, unsigned long mfrr)
156 return plpar_hcall_norets(H_IPI, servernum, mfrr);
159 static inline long plpar_xirr(unsigned long *xirr_ret)
162 return plpar_hcall(H_XIRR, 0, 0, 0, 0, xirr_ret, &dummy, &dummy);
165 static int pSeriesLP_xirr_info_get(int n_cpu)
167 unsigned long lpar_rc;
168 unsigned long return_value;
170 lpar_rc = plpar_xirr(&return_value);
171 if (lpar_rc != H_Success)
172 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
173 return (int)return_value;
176 static void pSeriesLP_xirr_info_set(int n_cpu, int value)
178 unsigned long lpar_rc;
179 unsigned long val64 = value & 0xffffffff;
181 lpar_rc = plpar_eoi(val64);
182 if (lpar_rc != H_Success)
183 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
187 void pSeriesLP_cppr_info(int n_cpu, u8 value)
189 unsigned long lpar_rc;
191 lpar_rc = plpar_cppr(value);
192 if (lpar_rc != H_Success)
193 panic("bad return code cppr - rc = %lx\n", lpar_rc);
196 static void pSeriesLP_qirr_info(int n_cpu , u8 value)
198 unsigned long lpar_rc;
200 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
201 if (lpar_rc != H_Success)
202 panic("bad return code qirr - rc = %lx\n", lpar_rc);
205 xics_ops pSeriesLP_ops = {
206 pSeriesLP_xirr_info_get,
207 pSeriesLP_xirr_info_set,
212 static unsigned int xics_startup(unsigned int virq)
216 irq = irq_offset_down(virq);
217 if (radix_tree_insert(&irq_map, virt_irq_to_real(irq),
218 &virt_irq_to_real_map[irq]) == -ENOMEM)
219 printk(KERN_CRIT "Out of memory creating real -> virtual"
220 " IRQ mapping for irq %u (real 0x%x)\n",
221 virq, virt_irq_to_real(irq));
222 xics_enable_irq(virq);
223 return 0; /* return value is ignored */
226 static unsigned int real_irq_to_virt(unsigned int real_irq)
230 ptr = radix_tree_lookup(&irq_map, real_irq);
233 return ptr - virt_irq_to_real_map;
237 static int get_irq_server(unsigned int irq)
240 /* For the moment only implement delivery to all cpus or one cpu */
241 cpumask_t cpumask = irq_affinity[irq];
242 cpumask_t tmp = CPU_MASK_NONE;
244 if (!distribute_irqs)
245 return default_server;
247 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
248 server = default_distrib_server;
250 cpus_and(tmp, cpu_online_map, cpumask);
253 server = default_distrib_server;
255 server = get_hard_smp_processor_id(first_cpu(tmp));
262 static int get_irq_server(unsigned int irq)
264 return default_server;
268 static void xics_enable_irq(unsigned int virq)
274 irq = virt_irq_to_real(irq_offset_down(virq));
278 server = get_irq_server(virq);
279 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
281 if (call_status != 0) {
282 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
283 "returned %d\n", irq, call_status);
284 printk("set_xive %x, server %x\n", ibm_set_xive, server);
288 /* Now unmask the interrupt (often a no-op) */
289 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
290 if (call_status != 0) {
291 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
292 "returned %d\n", irq, call_status);
297 static void xics_disable_real_irq(unsigned int irq)
305 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
306 if (call_status != 0) {
307 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
308 "ibm_int_off returned %d\n", irq, call_status);
312 server = get_irq_server(irq);
313 /* Have to set XIVE to 0xff to be able to remove a slot */
314 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server, 0xff);
315 if (call_status != 0) {
316 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
317 " returned %d\n", irq, call_status);
322 static void xics_disable_irq(unsigned int virq)
326 irq = virt_irq_to_real(irq_offset_down(virq));
327 xics_disable_real_irq(irq);
330 static void xics_end_irq(unsigned int irq)
332 int cpu = smp_processor_id();
335 ops->xirr_info_set(cpu, ((0xff << 24) |
336 (virt_irq_to_real(irq_offset_down(irq)))));
340 static void xics_mask_and_ack_irq(unsigned int irq)
342 int cpu = smp_processor_id();
344 if (irq < irq_offset_value()) {
347 ops->xirr_info_set(cpu, ((0xff<<24) |
348 xics_irq_8259_cascade_real));
353 int xics_get_irq(struct pt_regs *regs)
355 unsigned int cpu = smp_processor_id();
359 vec = ops->xirr_info_get(cpu);
360 /* (vec >> 24) == old priority */
363 /* for sanity, this had better be < NR_IRQS - 16 */
364 if (vec == xics_irq_8259_cascade_real) {
365 irq = i8259_irq(regs);
366 xics_end_irq(irq_offset_up(xics_irq_8259_cascade));
367 } else if (vec == XICS_IRQ_SPURIOUS) {
370 irq = real_irq_to_virt(vec);
372 irq = real_irq_to_virt_slowpath(vec);
374 printk(KERN_ERR "Interrupt %u (real) is invalid,"
375 " disabling it.\n", vec);
376 xics_disable_real_irq(vec);
378 irq = irq_offset_up(irq);
385 static irqreturn_t xics_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
387 int cpu = smp_processor_id();
389 ops->qirr_info(cpu, 0xff);
391 WARN_ON(cpu_is_offline(cpu));
393 while (xics_ipi_message[cpu].value) {
394 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
395 &xics_ipi_message[cpu].value)) {
397 smp_message_recv(PPC_MSG_CALL_FUNCTION, regs);
399 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
400 &xics_ipi_message[cpu].value)) {
402 smp_message_recv(PPC_MSG_RESCHEDULE, regs);
405 if (test_and_clear_bit(PPC_MSG_MIGRATE_TASK,
406 &xics_ipi_message[cpu].value)) {
408 smp_message_recv(PPC_MSG_MIGRATE_TASK, regs);
411 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
412 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
413 &xics_ipi_message[cpu].value)) {
415 smp_message_recv(PPC_MSG_DEBUGGER_BREAK, regs);
422 void xics_cause_IPI(int cpu)
424 ops->qirr_info(cpu, IPI_PRIORITY);
426 #endif /* CONFIG_SMP */
428 void xics_setup_cpu(void)
430 int cpu = smp_processor_id();
432 ops->cppr_info(cpu, 0xff);
436 * Put the calling processor into the GIQ. This is really only
437 * necessary from a secondary thread as the OF start-cpu interface
438 * performs this function for us on primary threads.
440 * XXX: undo of teardown on kexec needs this too, as may hotplug
442 rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
443 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
446 void xics_init_IRQ(void)
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 {
458 ppc64_boot_msg(0x20, "XICS Init");
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");
465 np = of_find_node_by_type(NULL, "PowerPC-External-Interrupt-Presentation");
467 panic("xics_init_IRQ: can't find interrupt presentation");
470 ireg = (uint *)get_property(np, "ibm,interrupt-server-ranges", NULL);
473 * set node starting index for this node
478 ireg = (uint *)get_property(np, "reg", &ilen);
480 panic("xics_init_IRQ: can't find interrupt reg property");
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);
492 if (indx >= NR_CPUS) break;
495 np = of_find_node_by_type(np, "PowerPC-External-Interrupt-Presentation");
496 if ((indx < NR_CPUS) && np) goto nextnode;
498 /* Find the server numbers for the boot cpu. */
499 for (np = of_find_node_by_type(NULL, "cpu");
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",
506 i = ilen / sizeof(int);
508 default_server = ireg[0];
509 default_distrib_server = ireg[i-1]; /* take last element */
511 ireg = (uint *)get_property(np,
512 "ibm,interrupt-server#-size", NULL);
514 interrupt_server_size = *ireg;
520 intr_base = intnodes[0].addr;
521 intr_size = intnodes[0].size;
523 np = of_find_node_by_type(NULL, "interrupt-controller");
525 printk(KERN_WARNING "xics: no ISA interrupt controller\n");
526 xics_irq_8259_cascade_real = -1;
527 xics_irq_8259_cascade = -1;
529 ireg = (uint *) get_property(np, "interrupts", NULL);
531 panic("xics_init_IRQ: can't find ISA interrupts property");
533 xics_irq_8259_cascade_real = *ireg;
534 xics_irq_8259_cascade
535 = virt_irq_create_mapping(xics_irq_8259_cascade_real);
540 if (firmware_has_feature(FW_FEATURE_LPAR))
541 ops = &pSeriesLP_ops;
547 /* FIXME: Do this dynamically! --RR */
551 hard_id = get_hard_smp_processor_id(i);
552 xics_per_cpu[i] = ioremap(intnodes[hard_id].addr,
553 intnodes[hard_id].size);
556 xics_per_cpu[0] = ioremap(intr_base, intr_size);
557 #endif /* CONFIG_SMP */
560 for (i = irq_offset_value(); i < NR_IRQS; ++i)
561 get_irq_desc(i)->handler = &xics_pic;
565 ppc64_boot_msg(0x21, "XICS Done");
569 * We cant do this in init_IRQ because we need the memory subsystem up for
572 static int __init xics_setup_i8259(void)
574 if (ppc64_interrupt_controller == IC_PPC_XIC &&
575 xics_irq_8259_cascade != -1) {
576 if (request_irq(irq_offset_up(xics_irq_8259_cascade),
577 no_action, 0, "8259 cascade", NULL))
578 printk(KERN_ERR "xics_setup_i8259: couldn't get 8259 "
583 arch_initcall(xics_setup_i8259);
586 void xics_request_IPIs(void)
588 virt_irq_to_real_map[XICS_IPI] = XICS_IPI;
590 /* IPIs are marked SA_INTERRUPT as they must run with irqs disabled */
591 request_irq(irq_offset_up(XICS_IPI), xics_ipi_action, SA_INTERRUPT,
593 get_irq_desc(irq_offset_up(XICS_IPI))->status |= IRQ_PER_CPU;
597 static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
602 unsigned long newmask;
603 cpumask_t tmp = CPU_MASK_NONE;
605 irq = virt_irq_to_real(irq_offset_down(virq));
606 if (irq == XICS_IPI || irq == NO_IRQ)
609 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
612 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
613 "returns %d\n", irq, status);
617 /* For the moment only implement delivery to all cpus or one cpu */
618 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
619 newmask = default_distrib_server;
621 cpus_and(tmp, cpu_online_map, cpumask);
624 newmask = get_hard_smp_processor_id(first_cpu(tmp));
627 status = rtas_call(ibm_set_xive, 3, 1, NULL,
628 irq, newmask, xics_status[1]);
631 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
632 "returns %d\n", irq, status);
637 void xics_teardown_cpu(int secondary)
639 int cpu = smp_processor_id();
641 ops->cppr_info(cpu, 0x00);
645 * Some machines need to have at least one cpu in the GIQ,
646 * so leave the master cpu in the group.
650 * we need to EOI the IPI if we got here from kexec down IPI
652 * probably need to check all the other interrupts too
653 * should we be flagging idle loop instead?
654 * or creating some task to be scheduled?
656 ops->xirr_info_set(cpu, XICS_IPI);
657 rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
658 (1UL << interrupt_server_size) - 1 -
659 default_distrib_server, 0);
663 #ifdef CONFIG_HOTPLUG_CPU
665 /* Interrupts are disabled. */
666 void xics_migrate_irqs_away(void)
669 unsigned int irq, virq, cpu = smp_processor_id();
671 /* Reject any interrupt that was queued to us... */
672 ops->cppr_info(cpu, 0);
675 /* remove ourselves from the global interrupt queue */
676 status = rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
677 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
680 /* Allow IPIs again... */
681 ops->cppr_info(cpu, DEFAULT_PRIORITY);
689 /* We cant set affinity on ISA interrupts */
690 if (virq < irq_offset_value())
693 desc = get_irq_desc(virq);
694 irq = virt_irq_to_real(irq_offset_down(virq));
696 /* We need to get IPIs still. */
697 if (irq == XICS_IPI || irq == NO_IRQ)
700 /* We only need to migrate enabled IRQS */
701 if (desc == NULL || desc->handler == NULL
702 || desc->action == NULL
703 || desc->handler->set_affinity == NULL)
706 spin_lock_irqsave(&desc->lock, flags);
708 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
710 printk(KERN_ERR "migrate_irqs_away: irq=%u "
711 "ibm,get-xive returns %d\n",
717 * We only support delivery to all cpus or to one cpu.
718 * The irq has to be migrated only in the single cpu
721 if (xics_status[0] != get_hard_smp_processor_id(cpu))
724 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
727 /* Reset affinity to all cpus */
728 desc->handler->set_affinity(virq, CPU_MASK_ALL);
729 irq_affinity[virq] = CPU_MASK_ALL;
731 spin_unlock_irqrestore(&desc->lock, flags);