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.
14 #include <linux/types.h>
15 #include <linux/threads.h>
16 #include <linux/kernel.h>
17 #include <linux/irq.h>
18 #include <linux/smp.h>
19 #include <linux/interrupt.h>
20 #include <linux/signal.h>
21 #include <linux/init.h>
22 #include <linux/gfp.h>
23 #include <linux/radix-tree.h>
24 #include <linux/cpu.h>
26 #include <asm/firmware.h>
29 #include <asm/pgtable.h>
32 #include <asm/hvcall.h>
33 #include <asm/machdep.h>
34 #include <asm/i8259.h>
37 #include "plpar_wrappers.h"
40 #define XICS_IRQ_SPURIOUS 0
42 /* Want a priority other than 0. Various HW issues require this. */
43 #define DEFAULT_PRIORITY 5
46 * Mark IPIs as higher priority so we can take them inside interrupts that
47 * arent marked IRQF_DISABLED
49 #define IPI_PRIORITY 4
67 static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
69 static unsigned int default_server = 0xFF;
70 static unsigned int default_distrib_server = 0;
71 static unsigned int interrupt_server_size = 8;
73 static struct irq_host *xics_host;
76 * XICS only has a single IPI, so encode the messages per CPU
78 struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
80 /* RTAS service tokens */
81 static int ibm_get_xive;
82 static int ibm_set_xive;
83 static int ibm_int_on;
84 static int ibm_int_off;
87 /* Direct HW low level accessors */
90 static inline unsigned int direct_xirr_info_get(int n_cpu)
92 return in_be32(&xics_per_cpu[n_cpu]->xirr.word);
95 static inline void direct_xirr_info_set(int n_cpu, int value)
97 out_be32(&xics_per_cpu[n_cpu]->xirr.word, value);
100 static inline void direct_cppr_info(int n_cpu, u8 value)
102 out_8(&xics_per_cpu[n_cpu]->xirr.bytes[0], value);
105 static inline void direct_qirr_info(int n_cpu, u8 value)
107 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
111 /* LPAR low level accessors */
114 static inline unsigned int lpar_xirr_info_get(int n_cpu)
116 unsigned long lpar_rc;
117 unsigned long return_value;
119 lpar_rc = plpar_xirr(&return_value);
120 if (lpar_rc != H_SUCCESS)
121 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
122 return (unsigned int)return_value;
125 static inline void lpar_xirr_info_set(int n_cpu, int value)
127 unsigned long lpar_rc;
128 unsigned long val64 = value & 0xffffffff;
130 lpar_rc = plpar_eoi(val64);
131 if (lpar_rc != H_SUCCESS)
132 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
136 static inline void lpar_cppr_info(int n_cpu, u8 value)
138 unsigned long lpar_rc;
140 lpar_rc = plpar_cppr(value);
141 if (lpar_rc != H_SUCCESS)
142 panic("bad return code cppr - rc = %lx\n", lpar_rc);
145 static inline void lpar_qirr_info(int n_cpu , u8 value)
147 unsigned long lpar_rc;
149 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
150 if (lpar_rc != H_SUCCESS)
151 panic("bad return code qirr - rc = %lx\n", lpar_rc);
155 /* High level handlers and init code */
159 static int get_irq_server(unsigned int virq)
162 /* For the moment only implement delivery to all cpus or one cpu */
163 cpumask_t cpumask = irq_desc[virq].affinity;
164 cpumask_t tmp = CPU_MASK_NONE;
166 if (!distribute_irqs)
167 return default_server;
169 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
170 server = default_distrib_server;
172 cpus_and(tmp, cpu_online_map, cpumask);
175 server = default_distrib_server;
177 server = get_hard_smp_processor_id(first_cpu(tmp));
184 static int get_irq_server(unsigned int virq)
186 return default_server;
191 static void xics_unmask_irq(unsigned int virq)
197 pr_debug("xics: unmask virq %d\n", virq);
199 irq = (unsigned int)irq_map[virq].hwirq;
200 pr_debug(" -> map to hwirq 0x%x\n", irq);
201 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
204 server = get_irq_server(virq);
206 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
208 if (call_status != 0) {
209 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
210 "returned %d\n", irq, call_status);
211 printk("set_xive %x, server %x\n", ibm_set_xive, server);
215 /* Now unmask the interrupt (often a no-op) */
216 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
217 if (call_status != 0) {
218 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
219 "returned %d\n", irq, call_status);
224 static void xics_mask_real_irq(unsigned int irq)
231 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
232 if (call_status != 0) {
233 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
234 "ibm_int_off returned %d\n", irq, call_status);
238 /* Have to set XIVE to 0xff to be able to remove a slot */
239 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq,
240 default_server, 0xff);
241 if (call_status != 0) {
242 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
243 " returned %d\n", irq, call_status);
248 static void xics_mask_irq(unsigned int virq)
252 pr_debug("xics: mask virq %d\n", virq);
254 irq = (unsigned int)irq_map[virq].hwirq;
255 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
257 xics_mask_real_irq(irq);
260 static unsigned int xics_startup(unsigned int virq)
264 /* force a reverse mapping of the interrupt so it gets in the cache */
265 irq = (unsigned int)irq_map[virq].hwirq;
266 irq_radix_revmap(xics_host, irq);
269 xics_unmask_irq(virq);
273 static void xics_eoi_direct(unsigned int virq)
275 int cpu = smp_processor_id();
276 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
279 direct_xirr_info_set(cpu, (0xff << 24) | irq);
283 static void xics_eoi_lpar(unsigned int virq)
285 int cpu = smp_processor_id();
286 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
289 lpar_xirr_info_set(cpu, (0xff << 24) | irq);
292 static inline unsigned int xics_remap_irq(unsigned int vec)
298 if (vec == XICS_IRQ_SPURIOUS)
300 irq = irq_radix_revmap(xics_host, vec);
301 if (likely(irq != NO_IRQ))
304 printk(KERN_ERR "Interrupt %u (real) is invalid,"
305 " disabling it.\n", vec);
306 xics_mask_real_irq(vec);
310 static unsigned int xics_get_irq_direct(void)
312 unsigned int cpu = smp_processor_id();
314 return xics_remap_irq(direct_xirr_info_get(cpu));
317 static unsigned int xics_get_irq_lpar(void)
319 unsigned int cpu = smp_processor_id();
321 return xics_remap_irq(lpar_xirr_info_get(cpu));
326 static irqreturn_t xics_ipi_dispatch(int cpu)
328 WARN_ON(cpu_is_offline(cpu));
330 while (xics_ipi_message[cpu].value) {
331 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
332 &xics_ipi_message[cpu].value)) {
334 smp_message_recv(PPC_MSG_CALL_FUNCTION);
336 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
337 &xics_ipi_message[cpu].value)) {
339 smp_message_recv(PPC_MSG_RESCHEDULE);
342 if (test_and_clear_bit(PPC_MSG_MIGRATE_TASK,
343 &xics_ipi_message[cpu].value)) {
345 smp_message_recv(PPC_MSG_MIGRATE_TASK);
348 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
349 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
350 &xics_ipi_message[cpu].value)) {
352 smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
359 static irqreturn_t xics_ipi_action_direct(int irq, void *dev_id)
361 int cpu = smp_processor_id();
363 direct_qirr_info(cpu, 0xff);
365 return xics_ipi_dispatch(cpu);
368 static irqreturn_t xics_ipi_action_lpar(int irq, void *dev_id)
370 int cpu = smp_processor_id();
372 lpar_qirr_info(cpu, 0xff);
374 return xics_ipi_dispatch(cpu);
377 void xics_cause_IPI(int cpu)
379 if (firmware_has_feature(FW_FEATURE_LPAR))
380 lpar_qirr_info(cpu, IPI_PRIORITY);
382 direct_qirr_info(cpu, IPI_PRIORITY);
385 #endif /* CONFIG_SMP */
387 static void xics_set_cpu_priority(int cpu, unsigned char cppr)
389 if (firmware_has_feature(FW_FEATURE_LPAR))
390 lpar_cppr_info(cpu, cppr);
392 direct_cppr_info(cpu, cppr);
396 static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
401 unsigned long newmask;
402 cpumask_t tmp = CPU_MASK_NONE;
404 irq = (unsigned int)irq_map[virq].hwirq;
405 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
408 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
411 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
412 "returns %d\n", irq, status);
416 /* For the moment only implement delivery to all cpus or one cpu */
417 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
418 newmask = default_distrib_server;
420 cpus_and(tmp, cpu_online_map, cpumask);
423 newmask = get_hard_smp_processor_id(first_cpu(tmp));
426 status = rtas_call(ibm_set_xive, 3, 1, NULL,
427 irq, newmask, xics_status[1]);
430 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
431 "returns %d\n", irq, status);
436 void xics_setup_cpu(void)
438 int cpu = smp_processor_id();
440 xics_set_cpu_priority(cpu, 0xff);
443 * Put the calling processor into the GIQ. This is really only
444 * necessary from a secondary thread as the OF start-cpu interface
445 * performs this function for us on primary threads.
447 * XXX: undo of teardown on kexec needs this too, as may hotplug
449 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
450 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
454 static struct irq_chip xics_pic_direct = {
455 .typename = " XICS ",
456 .startup = xics_startup,
457 .mask = xics_mask_irq,
458 .unmask = xics_unmask_irq,
459 .eoi = xics_eoi_direct,
460 .set_affinity = xics_set_affinity
464 static struct irq_chip xics_pic_lpar = {
465 .typename = " XICS ",
466 .startup = xics_startup,
467 .mask = xics_mask_irq,
468 .unmask = xics_unmask_irq,
469 .eoi = xics_eoi_lpar,
470 .set_affinity = xics_set_affinity
474 static int xics_host_match(struct irq_host *h, struct device_node *node)
476 /* IBM machines have interrupt parents of various funky types for things
477 * like vdevices, events, etc... The trick we use here is to match
478 * everything here except the legacy 8259 which is compatible "chrp,iic"
480 return !of_device_is_compatible(node, "chrp,iic");
483 static int xics_host_map_direct(struct irq_host *h, unsigned int virq,
486 pr_debug("xics: map_direct virq %d, hwirq 0x%lx\n", virq, hw);
488 get_irq_desc(virq)->status |= IRQ_LEVEL;
489 set_irq_chip_and_handler(virq, &xics_pic_direct, handle_fasteoi_irq);
493 static int xics_host_map_lpar(struct irq_host *h, unsigned int virq,
496 pr_debug("xics: map_direct virq %d, hwirq 0x%lx\n", virq, hw);
498 get_irq_desc(virq)->status |= IRQ_LEVEL;
499 set_irq_chip_and_handler(virq, &xics_pic_lpar, handle_fasteoi_irq);
503 static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
504 u32 *intspec, unsigned int intsize,
505 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
508 /* Current xics implementation translates everything
509 * to level. It is not technically right for MSIs but this
510 * is irrelevant at this point. We might get smarter in the future
512 *out_hwirq = intspec[0];
513 *out_flags = IRQ_TYPE_LEVEL_LOW;
518 static struct irq_host_ops xics_host_direct_ops = {
519 .match = xics_host_match,
520 .map = xics_host_map_direct,
521 .xlate = xics_host_xlate,
524 static struct irq_host_ops xics_host_lpar_ops = {
525 .match = xics_host_match,
526 .map = xics_host_map_lpar,
527 .xlate = xics_host_xlate,
530 static void __init xics_init_host(void)
532 struct irq_host_ops *ops;
534 if (firmware_has_feature(FW_FEATURE_LPAR))
535 ops = &xics_host_lpar_ops;
537 ops = &xics_host_direct_ops;
538 xics_host = irq_alloc_host(IRQ_HOST_MAP_TREE, 0, ops,
540 BUG_ON(xics_host == NULL);
541 irq_set_default_host(xics_host);
544 static void __init xics_map_one_cpu(int hw_id, unsigned long addr,
550 /* This may look gross but it's good enough for now, we don't quite
551 * have a hard -> linux processor id matching.
553 for_each_possible_cpu(i) {
556 if (hw_id == get_hard_smp_processor_id(i)) {
557 xics_per_cpu[i] = ioremap(addr, size);
564 xics_per_cpu[0] = ioremap(addr, size);
565 #endif /* CONFIG_SMP */
568 static void __init xics_init_one_node(struct device_node *np,
574 /* This code does the theorically broken assumption that the interrupt
575 * server numbers are the same as the hard CPU numbers.
576 * This happens to be the case so far but we are playing with fire...
577 * should be fixed one of these days. -BenH.
579 ireg = of_get_property(np, "ibm,interrupt-server-ranges", NULL);
581 /* Do that ever happen ? we'll know soon enough... but even good'old
582 * f80 does have that property ..
584 WARN_ON(ireg == NULL);
587 * set node starting index for this node
591 ireg = of_get_property(np, "reg", &ilen);
593 panic("xics_init_IRQ: can't find interrupt reg property");
595 while (ilen >= (4 * sizeof(u32))) {
596 unsigned long addr, size;
598 /* XXX Use proper OF parsing code here !!! */
599 addr = (unsigned long)*ireg++ << 32;
603 size = (unsigned long)*ireg++ << 32;
607 xics_map_one_cpu(*indx, addr, size);
613 static void __init xics_setup_8259_cascade(void)
615 struct device_node *np, *old, *found = NULL;
618 unsigned long intack = 0;
620 for_each_node_by_type(np, "interrupt-controller")
621 if (of_device_is_compatible(np, "chrp,iic")) {
626 printk(KERN_DEBUG "xics: no ISA interrupt controller\n");
629 cascade = irq_of_parse_and_map(found, 0);
630 if (cascade == NO_IRQ) {
631 printk(KERN_ERR "xics: failed to map cascade interrupt");
634 pr_debug("xics: cascade mapped to irq %d\n", cascade);
636 for (old = of_node_get(found); old != NULL ; old = np) {
637 np = of_get_parent(old);
641 if (strcmp(np->name, "pci") != 0)
643 addrp = of_get_property(np, "8259-interrupt-acknowledge", NULL);
646 naddr = of_n_addr_cells(np);
647 intack = addrp[naddr-1];
649 intack |= ((unsigned long)addrp[naddr-2]) << 32;
652 printk(KERN_DEBUG "xics: PCI 8259 intack at 0x%016lx\n", intack);
653 i8259_init(found, intack);
655 set_irq_chained_handler(cascade, pseries_8259_cascade);
658 static struct device_node *cpuid_to_of_node(int cpu)
660 struct device_node *np;
661 u32 hcpuid = get_hard_smp_processor_id(cpu);
663 for_each_node_by_type(np, "cpu") {
667 intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
671 intserv = of_get_property(np, "reg", &len);
673 i = len / sizeof(u32);
676 if (intserv[i] == hcpuid)
683 void __init xics_init_IRQ(void)
686 struct device_node *np;
688 const u32 *ireg, *isize;
692 ppc64_boot_msg(0x20, "XICS Init");
694 ibm_get_xive = rtas_token("ibm,get-xive");
695 ibm_set_xive = rtas_token("ibm,set-xive");
696 ibm_int_on = rtas_token("ibm,int-on");
697 ibm_int_off = rtas_token("ibm,int-off");
699 for_each_node_by_type(np, "PowerPC-External-Interrupt-Presentation") {
701 if (firmware_has_feature(FW_FEATURE_LPAR))
703 xics_init_one_node(np, &indx);
710 /* Find the server numbers for the boot cpu. */
711 np = cpuid_to_of_node(boot_cpuid);
713 ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
715 goto skip_gserver_check;
716 i = ilen / sizeof(int);
717 hcpuid = get_hard_smp_processor_id(boot_cpuid);
719 /* Global interrupt distribution server is specified in the last
720 * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
721 * entry fom this property for current boot cpu id and use it as
722 * default distribution server
724 for (j = 0; j < i; j += 2) {
725 if (ireg[j] == hcpuid) {
726 default_server = hcpuid;
727 default_distrib_server = ireg[j+1];
729 isize = of_get_property(np,
730 "ibm,interrupt-server#-size", NULL);
732 interrupt_server_size = *isize;
738 if (firmware_has_feature(FW_FEATURE_LPAR))
739 ppc_md.get_irq = xics_get_irq_lpar;
741 ppc_md.get_irq = xics_get_irq_direct;
745 xics_setup_8259_cascade();
747 ppc64_boot_msg(0x21, "XICS Done");
752 void xics_request_IPIs(void)
757 ipi = irq_create_mapping(xics_host, XICS_IPI);
758 BUG_ON(ipi == NO_IRQ);
761 * IPIs are marked IRQF_DISABLED as they must run with irqs
764 set_irq_handler(ipi, handle_percpu_irq);
765 if (firmware_has_feature(FW_FEATURE_LPAR))
766 rc = request_irq(ipi, xics_ipi_action_lpar, IRQF_DISABLED,
769 rc = request_irq(ipi, xics_ipi_action_direct, IRQF_DISABLED,
773 #endif /* CONFIG_SMP */
775 void xics_teardown_cpu(int secondary)
777 int cpu = smp_processor_id();
779 struct irq_desc *desc;
781 xics_set_cpu_priority(cpu, 0);
786 if (firmware_has_feature(FW_FEATURE_LPAR))
787 lpar_qirr_info(cpu, 0xff);
789 direct_qirr_info(cpu, 0xff);
792 * we need to EOI the IPI if we got here from kexec down IPI
794 * probably need to check all the other interrupts too
795 * should we be flagging idle loop instead?
796 * or creating some task to be scheduled?
799 ipi = irq_find_mapping(xics_host, XICS_IPI);
800 if (ipi == XICS_IRQ_SPURIOUS)
802 desc = get_irq_desc(ipi);
803 if (desc->chip && desc->chip->eoi)
804 desc->chip->eoi(ipi);
807 * Some machines need to have at least one cpu in the GIQ,
808 * so leave the master cpu in the group.
811 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
812 (1UL << interrupt_server_size) - 1 -
813 default_distrib_server, 0);
816 #ifdef CONFIG_HOTPLUG_CPU
818 /* Interrupts are disabled. */
819 void xics_migrate_irqs_away(void)
822 unsigned int irq, virq, cpu = smp_processor_id();
824 /* Reject any interrupt that was queued to us... */
825 xics_set_cpu_priority(cpu, 0);
827 /* remove ourselves from the global interrupt queue */
828 status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
829 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
832 /* Allow IPIs again... */
833 xics_set_cpu_priority(cpu, DEFAULT_PRIORITY);
836 struct irq_desc *desc;
840 /* We cant set affinity on ISA interrupts */
841 if (virq < NUM_ISA_INTERRUPTS)
843 if (irq_map[virq].host != xics_host)
845 irq = (unsigned int)irq_map[virq].hwirq;
846 /* We need to get IPIs still. */
847 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
849 desc = get_irq_desc(virq);
851 /* We only need to migrate enabled IRQS */
852 if (desc == NULL || desc->chip == NULL
853 || desc->action == NULL
854 || desc->chip->set_affinity == NULL)
857 spin_lock_irqsave(&desc->lock, flags);
859 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
861 printk(KERN_ERR "migrate_irqs_away: irq=%u "
862 "ibm,get-xive returns %d\n",
868 * We only support delivery to all cpus or to one cpu.
869 * The irq has to be migrated only in the single cpu
872 if (xics_status[0] != get_hard_smp_processor_id(cpu))
875 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
878 /* Reset affinity to all cpus */
879 desc->chip->set_affinity(virq, CPU_MASK_ALL);
880 irq_desc[irq].affinity = CPU_MASK_ALL;
882 spin_unlock_irqrestore(&desc->lock, flags);