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.
13 #include <linux/types.h>
14 #include <linux/threads.h>
15 #include <linux/kernel.h>
16 #include <linux/irq.h>
17 #include <linux/smp.h>
18 #include <linux/interrupt.h>
19 #include <linux/signal.h>
20 #include <linux/init.h>
21 #include <linux/gfp.h>
22 #include <linux/radix-tree.h>
23 #include <linux/cpu.h>
25 #include <asm/firmware.h>
28 #include <asm/pgtable.h>
31 #include <asm/hvcall.h>
32 #include <asm/machdep.h>
33 #include <asm/i8259.h>
36 #include "plpar_wrappers.h"
39 #define XICS_IRQ_SPURIOUS 0
41 /* Want a priority other than 0. Various HW issues require this. */
42 #define DEFAULT_PRIORITY 5
45 * Mark IPIs as higher priority so we can take them inside interrupts that
46 * arent marked IRQF_DISABLED
48 #define IPI_PRIORITY 4
66 static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
68 static unsigned int default_server = 0xFF;
69 static unsigned int default_distrib_server = 0;
70 static unsigned int interrupt_server_size = 8;
72 static struct irq_host *xics_host;
75 * XICS only has a single IPI, so encode the messages per CPU
77 struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
79 /* RTAS service tokens */
80 static int ibm_get_xive;
81 static int ibm_set_xive;
82 static int ibm_int_on;
83 static int ibm_int_off;
86 /* Direct HW low level accessors */
89 static inline unsigned int direct_xirr_info_get(void)
91 int cpu = smp_processor_id();
93 return in_be32(&xics_per_cpu[cpu]->xirr.word);
96 static inline void direct_xirr_info_set(int value)
98 int cpu = smp_processor_id();
100 out_be32(&xics_per_cpu[cpu]->xirr.word, value);
103 static inline void direct_cppr_info(u8 value)
105 int cpu = smp_processor_id();
107 out_8(&xics_per_cpu[cpu]->xirr.bytes[0], value);
110 static inline void direct_qirr_info(int n_cpu, u8 value)
112 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
116 /* LPAR low level accessors */
119 static inline unsigned int lpar_xirr_info_get(void)
121 unsigned long lpar_rc;
122 unsigned long return_value;
124 lpar_rc = plpar_xirr(&return_value);
125 if (lpar_rc != H_SUCCESS)
126 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
127 return (unsigned int)return_value;
130 static inline void lpar_xirr_info_set(int value)
132 unsigned long lpar_rc;
133 unsigned long val64 = value & 0xffffffff;
135 lpar_rc = plpar_eoi(val64);
136 if (lpar_rc != H_SUCCESS)
137 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
141 static inline void lpar_cppr_info(u8 value)
143 unsigned long lpar_rc;
145 lpar_rc = plpar_cppr(value);
146 if (lpar_rc != H_SUCCESS)
147 panic("bad return code cppr - rc = %lx\n", lpar_rc);
150 static inline void lpar_qirr_info(int n_cpu , u8 value)
152 unsigned long lpar_rc;
154 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
155 if (lpar_rc != H_SUCCESS)
156 panic("bad return code qirr - rc = %lx\n", lpar_rc);
160 /* High level handlers and init code */
162 static void xics_update_irq_servers(void)
165 struct device_node *np;
167 const u32 *ireg, *isize;
170 /* Find the server numbers for the boot cpu. */
171 np = of_get_cpu_node(boot_cpuid, NULL);
174 ireg = of_get_property(np, "ibm,ppc-interrupt-gserver#s", &ilen);
180 i = ilen / sizeof(int);
181 hcpuid = get_hard_smp_processor_id(boot_cpuid);
183 /* Global interrupt distribution server is specified in the last
184 * entry of "ibm,ppc-interrupt-gserver#s" property. Get the last
185 * entry fom this property for current boot cpu id and use it as
186 * default distribution server
188 for (j = 0; j < i; j += 2) {
189 if (ireg[j] == hcpuid) {
190 default_server = hcpuid;
191 default_distrib_server = ireg[j+1];
193 isize = of_get_property(np,
194 "ibm,interrupt-server#-size", NULL);
196 interrupt_server_size = *isize;
204 static int get_irq_server(unsigned int virq, unsigned int strict_check)
207 /* For the moment only implement delivery to all cpus or one cpu */
208 cpumask_t cpumask = irq_desc[virq].affinity;
209 cpumask_t tmp = CPU_MASK_NONE;
211 if (! cpu_isset(default_server, cpu_online_map))
212 xics_update_irq_servers();
214 if (!distribute_irqs)
215 return default_server;
217 if (!cpus_equal(cpumask, CPU_MASK_ALL)) {
218 cpus_and(tmp, cpu_online_map, cpumask);
220 server = first_cpu(tmp);
222 if (server < NR_CPUS)
223 return get_hard_smp_processor_id(server);
229 if (cpus_equal(cpu_online_map, cpu_present_map))
230 return default_distrib_server;
232 return default_server;
235 static int get_irq_server(unsigned int virq, unsigned int strict_check)
237 return default_server;
242 static void xics_unmask_irq(unsigned int virq)
248 pr_debug("xics: unmask virq %d\n", virq);
250 irq = (unsigned int)irq_map[virq].hwirq;
251 pr_debug(" -> map to hwirq 0x%x\n", irq);
252 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
255 server = get_irq_server(virq, 0);
257 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
259 if (call_status != 0) {
260 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
261 "returned %d\n", irq, call_status);
262 printk("set_xive %x, server %x\n", ibm_set_xive, server);
266 /* Now unmask the interrupt (often a no-op) */
267 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
268 if (call_status != 0) {
269 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
270 "returned %d\n", irq, call_status);
275 static void xics_mask_real_irq(unsigned int irq)
282 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
283 if (call_status != 0) {
284 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
285 "ibm_int_off returned %d\n", irq, call_status);
289 /* Have to set XIVE to 0xff to be able to remove a slot */
290 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq,
291 default_server, 0xff);
292 if (call_status != 0) {
293 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
294 " returned %d\n", irq, call_status);
299 static void xics_mask_irq(unsigned int virq)
303 pr_debug("xics: mask virq %d\n", virq);
305 irq = (unsigned int)irq_map[virq].hwirq;
306 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
308 xics_mask_real_irq(irq);
311 static unsigned int xics_startup(unsigned int virq)
314 xics_unmask_irq(virq);
318 static void xics_eoi_direct(unsigned int virq)
320 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
323 direct_xirr_info_set((0xff << 24) | irq);
327 static void xics_eoi_lpar(unsigned int virq)
329 unsigned int irq = (unsigned int)irq_map[virq].hwirq;
332 lpar_xirr_info_set((0xff << 24) | irq);
335 static inline unsigned int xics_xirr_vector(unsigned int xirr)
338 * The top byte is the old cppr, to be restored on EOI.
339 * The remaining 24 bits are the vector.
341 return xirr & 0x00ffffff;
344 static void xics_mask_unknown_vec(unsigned int vec)
346 printk(KERN_ERR "Interrupt %u (real) is invalid, disabling it.\n", vec);
347 xics_mask_real_irq(vec);
350 static unsigned int xics_get_irq_direct(void)
352 unsigned int xirr = direct_xirr_info_get();
353 unsigned int vec = xics_xirr_vector(xirr);
356 if (vec == XICS_IRQ_SPURIOUS)
359 irq = irq_radix_revmap_lookup(xics_host, vec);
360 if (likely(irq != NO_IRQ))
363 /* We don't have a linux mapping, so have rtas mask it. */
364 xics_mask_unknown_vec(vec);
366 /* We might learn about it later, so EOI it */
367 direct_xirr_info_set(xirr);
371 static unsigned int xics_get_irq_lpar(void)
373 unsigned int xirr = lpar_xirr_info_get();
374 unsigned int vec = xics_xirr_vector(xirr);
377 if (vec == XICS_IRQ_SPURIOUS)
380 irq = irq_radix_revmap_lookup(xics_host, vec);
381 if (likely(irq != NO_IRQ))
384 /* We don't have a linux mapping, so have RTAS mask it. */
385 xics_mask_unknown_vec(vec);
387 /* We might learn about it later, so EOI it */
388 lpar_xirr_info_set(xirr);
394 static irqreturn_t xics_ipi_dispatch(int cpu)
396 WARN_ON(cpu_is_offline(cpu));
398 while (xics_ipi_message[cpu].value) {
399 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
400 &xics_ipi_message[cpu].value)) {
402 smp_message_recv(PPC_MSG_CALL_FUNCTION);
404 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
405 &xics_ipi_message[cpu].value)) {
407 smp_message_recv(PPC_MSG_RESCHEDULE);
409 if (test_and_clear_bit(PPC_MSG_CALL_FUNC_SINGLE,
410 &xics_ipi_message[cpu].value)) {
412 smp_message_recv(PPC_MSG_CALL_FUNC_SINGLE);
414 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
415 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
416 &xics_ipi_message[cpu].value)) {
418 smp_message_recv(PPC_MSG_DEBUGGER_BREAK);
425 static irqreturn_t xics_ipi_action_direct(int irq, void *dev_id)
427 int cpu = smp_processor_id();
429 direct_qirr_info(cpu, 0xff);
431 return xics_ipi_dispatch(cpu);
434 static irqreturn_t xics_ipi_action_lpar(int irq, void *dev_id)
436 int cpu = smp_processor_id();
438 lpar_qirr_info(cpu, 0xff);
440 return xics_ipi_dispatch(cpu);
443 void xics_cause_IPI(int cpu)
445 if (firmware_has_feature(FW_FEATURE_LPAR))
446 lpar_qirr_info(cpu, IPI_PRIORITY);
448 direct_qirr_info(cpu, IPI_PRIORITY);
451 #endif /* CONFIG_SMP */
453 static void xics_set_cpu_priority(unsigned char cppr)
455 if (firmware_has_feature(FW_FEATURE_LPAR))
456 lpar_cppr_info(cppr);
458 direct_cppr_info(cppr);
462 static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
469 irq = (unsigned int)irq_map[virq].hwirq;
470 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
473 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
476 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
477 "returns %d\n", irq, status);
482 * For the moment only implement delivery to all cpus or one cpu.
483 * Get current irq_server for the given irq
485 irq_server = get_irq_server(virq, 1);
486 if (irq_server == -1) {
488 cpumask_scnprintf(cpulist, sizeof(cpulist), cpumask);
489 printk(KERN_WARNING "xics_set_affinity: No online cpus in "
490 "the mask %s for irq %d\n", cpulist, virq);
494 status = rtas_call(ibm_set_xive, 3, 1, NULL,
495 irq, irq_server, xics_status[1]);
498 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
499 "returns %d\n", irq, status);
504 void xics_setup_cpu(void)
506 xics_set_cpu_priority(0xff);
509 * Put the calling processor into the GIQ. This is really only
510 * necessary from a secondary thread as the OF start-cpu interface
511 * performs this function for us on primary threads.
513 * XXX: undo of teardown on kexec needs this too, as may hotplug
515 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
516 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
520 static struct irq_chip xics_pic_direct = {
521 .typename = " XICS ",
522 .startup = xics_startup,
523 .mask = xics_mask_irq,
524 .unmask = xics_unmask_irq,
525 .eoi = xics_eoi_direct,
526 .set_affinity = xics_set_affinity
530 static struct irq_chip xics_pic_lpar = {
531 .typename = " XICS ",
532 .startup = xics_startup,
533 .mask = xics_mask_irq,
534 .unmask = xics_unmask_irq,
535 .eoi = xics_eoi_lpar,
536 .set_affinity = xics_set_affinity
539 /* Points to the irq_chip we're actually using */
540 static struct irq_chip *xics_irq_chip;
542 static int xics_host_match(struct irq_host *h, struct device_node *node)
544 /* IBM machines have interrupt parents of various funky types for things
545 * like vdevices, events, etc... The trick we use here is to match
546 * everything here except the legacy 8259 which is compatible "chrp,iic"
548 return !of_device_is_compatible(node, "chrp,iic");
551 static int xics_host_map(struct irq_host *h, unsigned int virq,
554 pr_debug("xics: map virq %d, hwirq 0x%lx\n", virq, hw);
556 /* Insert the interrupt mapping into the radix tree for fast lookup */
557 irq_radix_revmap_insert(xics_host, virq, hw);
559 get_irq_desc(virq)->status |= IRQ_LEVEL;
560 set_irq_chip_and_handler(virq, xics_irq_chip, handle_fasteoi_irq);
564 static int xics_host_xlate(struct irq_host *h, struct device_node *ct,
565 u32 *intspec, unsigned int intsize,
566 irq_hw_number_t *out_hwirq, unsigned int *out_flags)
569 /* Current xics implementation translates everything
570 * to level. It is not technically right for MSIs but this
571 * is irrelevant at this point. We might get smarter in the future
573 *out_hwirq = intspec[0];
574 *out_flags = IRQ_TYPE_LEVEL_LOW;
579 static struct irq_host_ops xics_host_ops = {
580 .match = xics_host_match,
581 .map = xics_host_map,
582 .xlate = xics_host_xlate,
585 static void __init xics_init_host(void)
587 if (firmware_has_feature(FW_FEATURE_LPAR))
588 xics_irq_chip = &xics_pic_lpar;
590 xics_irq_chip = &xics_pic_direct;
592 xics_host = irq_alloc_host(NULL, IRQ_HOST_MAP_TREE, 0, &xics_host_ops,
594 BUG_ON(xics_host == NULL);
595 irq_set_default_host(xics_host);
598 static void __init xics_map_one_cpu(int hw_id, unsigned long addr,
604 /* This may look gross but it's good enough for now, we don't quite
605 * have a hard -> linux processor id matching.
607 for_each_possible_cpu(i) {
610 if (hw_id == get_hard_smp_processor_id(i)) {
611 xics_per_cpu[i] = ioremap(addr, size);
618 xics_per_cpu[0] = ioremap(addr, size);
619 #endif /* CONFIG_SMP */
622 static void __init xics_init_one_node(struct device_node *np,
628 /* This code does the theorically broken assumption that the interrupt
629 * server numbers are the same as the hard CPU numbers.
630 * This happens to be the case so far but we are playing with fire...
631 * should be fixed one of these days. -BenH.
633 ireg = of_get_property(np, "ibm,interrupt-server-ranges", NULL);
635 /* Do that ever happen ? we'll know soon enough... but even good'old
636 * f80 does have that property ..
638 WARN_ON(ireg == NULL);
641 * set node starting index for this node
645 ireg = of_get_property(np, "reg", &ilen);
647 panic("xics_init_IRQ: can't find interrupt reg property");
649 while (ilen >= (4 * sizeof(u32))) {
650 unsigned long addr, size;
652 /* XXX Use proper OF parsing code here !!! */
653 addr = (unsigned long)*ireg++ << 32;
657 size = (unsigned long)*ireg++ << 32;
661 xics_map_one_cpu(*indx, addr, size);
666 void __init xics_init_IRQ(void)
668 struct device_node *np;
672 ppc64_boot_msg(0x20, "XICS Init");
674 ibm_get_xive = rtas_token("ibm,get-xive");
675 ibm_set_xive = rtas_token("ibm,set-xive");
676 ibm_int_on = rtas_token("ibm,int-on");
677 ibm_int_off = rtas_token("ibm,int-off");
679 for_each_node_by_type(np, "PowerPC-External-Interrupt-Presentation") {
681 if (firmware_has_feature(FW_FEATURE_LPAR))
683 xics_init_one_node(np, &indx);
689 xics_update_irq_servers();
691 if (firmware_has_feature(FW_FEATURE_LPAR))
692 ppc_md.get_irq = xics_get_irq_lpar;
694 ppc_md.get_irq = xics_get_irq_direct;
698 ppc64_boot_msg(0x21, "XICS Done");
703 void xics_request_IPIs(void)
708 ipi = irq_create_mapping(xics_host, XICS_IPI);
709 BUG_ON(ipi == NO_IRQ);
712 * IPIs are marked IRQF_DISABLED as they must run with irqs
715 set_irq_handler(ipi, handle_percpu_irq);
716 if (firmware_has_feature(FW_FEATURE_LPAR))
717 rc = request_irq(ipi, xics_ipi_action_lpar, IRQF_DISABLED,
720 rc = request_irq(ipi, xics_ipi_action_direct, IRQF_DISABLED,
724 #endif /* CONFIG_SMP */
726 void xics_teardown_cpu(void)
728 int cpu = smp_processor_id();
730 xics_set_cpu_priority(0);
735 if (firmware_has_feature(FW_FEATURE_LPAR))
736 lpar_qirr_info(cpu, 0xff);
738 direct_qirr_info(cpu, 0xff);
741 void xics_kexec_teardown_cpu(int secondary)
744 struct irq_desc *desc;
749 * we need to EOI the IPI
751 * probably need to check all the other interrupts too
752 * should we be flagging idle loop instead?
753 * or creating some task to be scheduled?
756 ipi = irq_find_mapping(xics_host, XICS_IPI);
757 if (ipi == XICS_IRQ_SPURIOUS)
759 desc = get_irq_desc(ipi);
760 if (desc->chip && desc->chip->eoi)
761 desc->chip->eoi(ipi);
764 * Some machines need to have at least one cpu in the GIQ,
765 * so leave the master cpu in the group.
768 rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
769 (1UL << interrupt_server_size) - 1 -
770 default_distrib_server, 0);
773 #ifdef CONFIG_HOTPLUG_CPU
775 /* Interrupts are disabled. */
776 void xics_migrate_irqs_away(void)
779 int cpu = smp_processor_id(), hw_cpu = hard_smp_processor_id();
780 unsigned int irq, virq;
782 /* Reject any interrupt that was queued to us... */
783 xics_set_cpu_priority(0);
785 /* remove ourselves from the global interrupt queue */
786 status = rtas_set_indicator_fast(GLOBAL_INTERRUPT_QUEUE,
787 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
790 /* Allow IPIs again... */
791 xics_set_cpu_priority(DEFAULT_PRIORITY);
794 struct irq_desc *desc;
798 /* We cant set affinity on ISA interrupts */
799 if (virq < NUM_ISA_INTERRUPTS)
801 if (irq_map[virq].host != xics_host)
803 irq = (unsigned int)irq_map[virq].hwirq;
804 /* We need to get IPIs still. */
805 if (irq == XICS_IPI || irq == XICS_IRQ_SPURIOUS)
807 desc = get_irq_desc(virq);
809 /* We only need to migrate enabled IRQS */
810 if (desc == NULL || desc->chip == NULL
811 || desc->action == NULL
812 || desc->chip->set_affinity == NULL)
815 spin_lock_irqsave(&desc->lock, flags);
817 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
819 printk(KERN_ERR "migrate_irqs_away: irq=%u "
820 "ibm,get-xive returns %d\n",
826 * We only support delivery to all cpus or to one cpu.
827 * The irq has to be migrated only in the single cpu
830 if (xics_status[0] != hw_cpu)
833 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
836 /* Reset affinity to all cpus */
837 irq_desc[virq].affinity = CPU_MASK_ALL;
838 desc->chip->set_affinity(virq, CPU_MASK_ALL);
840 spin_unlock_irqrestore(&desc->lock, flags);