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>
25 #include <asm/pgtable.h>
28 #include <asm/hvcall.h>
29 #include <asm/machdep.h>
30 #include <asm/i8259.h>
34 static unsigned int xics_startup(unsigned int irq);
35 static void xics_enable_irq(unsigned int irq);
36 static void xics_disable_irq(unsigned int irq);
37 static void xics_mask_and_ack_irq(unsigned int irq);
38 static void xics_end_irq(unsigned int irq);
39 static void xics_set_affinity(unsigned int irq_nr, cpumask_t cpumask);
41 static struct hw_interrupt_type xics_pic = {
43 .startup = xics_startup,
44 .enable = xics_enable_irq,
45 .disable = xics_disable_irq,
46 .ack = xics_mask_and_ack_irq,
48 .set_affinity = xics_set_affinity
51 /* This is used to map real irq numbers to virtual */
52 static struct radix_tree_root irq_map = RADIX_TREE_INIT(GFP_ATOMIC);
55 #define XICS_IRQ_SPURIOUS 0
57 /* Want a priority other than 0. Various HW issues require this. */
58 #define DEFAULT_PRIORITY 5
61 * Mark IPIs as higher priority so we can take them inside interrupts that
62 * arent marked SA_INTERRUPT
64 #define IPI_PRIORITY 4
82 static struct xics_ipl __iomem *xics_per_cpu[NR_CPUS];
84 static int xics_irq_8259_cascade = 0;
85 static int xics_irq_8259_cascade_real = 0;
86 static unsigned int default_server = 0xFF;
87 static unsigned int default_distrib_server = 0;
88 static unsigned int interrupt_server_size = 8;
91 * XICS only has a single IPI, so encode the messages per CPU
93 struct xics_ipi_struct xics_ipi_message[NR_CPUS] __cacheline_aligned;
95 /* RTAS service tokens */
96 static int ibm_get_xive;
97 static int ibm_set_xive;
98 static int ibm_int_on;
99 static int ibm_int_off;
102 int (*xirr_info_get)(int cpu);
103 void (*xirr_info_set)(int cpu, int val);
104 void (*cppr_info)(int cpu, u8 val);
105 void (*qirr_info)(int cpu, u8 val);
111 static int pSeries_xirr_info_get(int n_cpu)
113 return in_be32(&xics_per_cpu[n_cpu]->xirr.word);
116 static void pSeries_xirr_info_set(int n_cpu, int value)
118 out_be32(&xics_per_cpu[n_cpu]->xirr.word, value);
121 static void pSeries_cppr_info(int n_cpu, u8 value)
123 out_8(&xics_per_cpu[n_cpu]->xirr.bytes[0], value);
126 static void pSeries_qirr_info(int n_cpu, u8 value)
128 out_8(&xics_per_cpu[n_cpu]->qirr.bytes[0], value);
131 static xics_ops pSeries_ops = {
132 pSeries_xirr_info_get,
133 pSeries_xirr_info_set,
138 static xics_ops *ops = &pSeries_ops;
143 static inline long plpar_eoi(unsigned long xirr)
145 return plpar_hcall_norets(H_EOI, xirr);
148 static inline long plpar_cppr(unsigned long cppr)
150 return plpar_hcall_norets(H_CPPR, cppr);
153 static inline long plpar_ipi(unsigned long servernum, unsigned long mfrr)
155 return plpar_hcall_norets(H_IPI, servernum, mfrr);
158 static inline long plpar_xirr(unsigned long *xirr_ret)
161 return plpar_hcall(H_XIRR, 0, 0, 0, 0, xirr_ret, &dummy, &dummy);
164 static int pSeriesLP_xirr_info_get(int n_cpu)
166 unsigned long lpar_rc;
167 unsigned long return_value;
169 lpar_rc = plpar_xirr(&return_value);
170 if (lpar_rc != H_Success)
171 panic(" bad return code xirr - rc = %lx \n", lpar_rc);
172 return (int)return_value;
175 static void pSeriesLP_xirr_info_set(int n_cpu, int value)
177 unsigned long lpar_rc;
178 unsigned long val64 = value & 0xffffffff;
180 lpar_rc = plpar_eoi(val64);
181 if (lpar_rc != H_Success)
182 panic("bad return code EOI - rc = %ld, value=%lx\n", lpar_rc,
186 void pSeriesLP_cppr_info(int n_cpu, u8 value)
188 unsigned long lpar_rc;
190 lpar_rc = plpar_cppr(value);
191 if (lpar_rc != H_Success)
192 panic("bad return code cppr - rc = %lx\n", lpar_rc);
195 static void pSeriesLP_qirr_info(int n_cpu , u8 value)
197 unsigned long lpar_rc;
199 lpar_rc = plpar_ipi(get_hard_smp_processor_id(n_cpu), value);
200 if (lpar_rc != H_Success)
201 panic("bad return code qirr - rc = %lx\n", lpar_rc);
204 xics_ops pSeriesLP_ops = {
205 pSeriesLP_xirr_info_get,
206 pSeriesLP_xirr_info_set,
211 static unsigned int xics_startup(unsigned int virq)
215 irq = irq_offset_down(virq);
216 if (radix_tree_insert(&irq_map, virt_irq_to_real(irq),
217 &virt_irq_to_real_map[irq]) == -ENOMEM)
218 printk(KERN_CRIT "Out of memory creating real -> virtual"
219 " IRQ mapping for irq %u (real 0x%x)\n",
220 virq, virt_irq_to_real(irq));
221 xics_enable_irq(virq);
222 return 0; /* return value is ignored */
225 static unsigned int real_irq_to_virt(unsigned int real_irq)
229 ptr = radix_tree_lookup(&irq_map, real_irq);
232 return ptr - virt_irq_to_real_map;
236 static int get_irq_server(unsigned int irq)
239 /* For the moment only implement delivery to all cpus or one cpu */
240 cpumask_t cpumask = irq_affinity[irq];
241 cpumask_t tmp = CPU_MASK_NONE;
243 if (!distribute_irqs)
244 return default_server;
246 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
247 server = default_distrib_server;
249 cpus_and(tmp, cpu_online_map, cpumask);
252 server = default_distrib_server;
254 server = get_hard_smp_processor_id(first_cpu(tmp));
261 static int get_irq_server(unsigned int irq)
263 return default_server;
267 static void xics_enable_irq(unsigned int virq)
273 irq = virt_irq_to_real(irq_offset_down(virq));
277 server = get_irq_server(virq);
278 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
280 if (call_status != 0) {
281 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_set_xive "
282 "returned %d\n", irq, call_status);
283 printk("set_xive %x, server %x\n", ibm_set_xive, server);
287 /* Now unmask the interrupt (often a no-op) */
288 call_status = rtas_call(ibm_int_on, 1, 1, NULL, irq);
289 if (call_status != 0) {
290 printk(KERN_ERR "xics_enable_irq: irq=%u: ibm_int_on "
291 "returned %d\n", irq, call_status);
296 static void xics_disable_real_irq(unsigned int irq)
304 call_status = rtas_call(ibm_int_off, 1, 1, NULL, irq);
305 if (call_status != 0) {
306 printk(KERN_ERR "xics_disable_real_irq: irq=%u: "
307 "ibm_int_off returned %d\n", irq, call_status);
311 server = get_irq_server(irq);
312 /* Have to set XIVE to 0xff to be able to remove a slot */
313 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server, 0xff);
314 if (call_status != 0) {
315 printk(KERN_ERR "xics_disable_irq: irq=%u: ibm_set_xive(0xff)"
316 " returned %d\n", irq, call_status);
321 static void xics_disable_irq(unsigned int virq)
325 irq = virt_irq_to_real(irq_offset_down(virq));
326 xics_disable_real_irq(irq);
329 static void xics_end_irq(unsigned int irq)
331 int cpu = smp_processor_id();
334 ops->xirr_info_set(cpu, ((0xff << 24) |
335 (virt_irq_to_real(irq_offset_down(irq)))));
339 static void xics_mask_and_ack_irq(unsigned int irq)
341 int cpu = smp_processor_id();
343 if (irq < irq_offset_value()) {
346 ops->xirr_info_set(cpu, ((0xff<<24) |
347 xics_irq_8259_cascade_real));
352 int xics_get_irq(struct pt_regs *regs)
354 unsigned int cpu = smp_processor_id();
358 vec = ops->xirr_info_get(cpu);
359 /* (vec >> 24) == old priority */
362 /* for sanity, this had better be < NR_IRQS - 16 */
363 if (vec == xics_irq_8259_cascade_real) {
364 irq = i8259_irq(regs);
365 xics_end_irq(irq_offset_up(xics_irq_8259_cascade));
366 } else if (vec == XICS_IRQ_SPURIOUS) {
369 irq = real_irq_to_virt(vec);
371 irq = real_irq_to_virt_slowpath(vec);
373 printk(KERN_ERR "Interrupt %u (real) is invalid,"
374 " disabling it.\n", vec);
375 xics_disable_real_irq(vec);
377 irq = irq_offset_up(irq);
384 irqreturn_t xics_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
386 int cpu = smp_processor_id();
388 ops->qirr_info(cpu, 0xff);
390 WARN_ON(cpu_is_offline(cpu));
392 while (xics_ipi_message[cpu].value) {
393 if (test_and_clear_bit(PPC_MSG_CALL_FUNCTION,
394 &xics_ipi_message[cpu].value)) {
396 smp_message_recv(PPC_MSG_CALL_FUNCTION, regs);
398 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
399 &xics_ipi_message[cpu].value)) {
401 smp_message_recv(PPC_MSG_RESCHEDULE, regs);
404 if (test_and_clear_bit(PPC_MSG_MIGRATE_TASK,
405 &xics_ipi_message[cpu].value)) {
407 smp_message_recv(PPC_MSG_MIGRATE_TASK, regs);
410 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
411 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
412 &xics_ipi_message[cpu].value)) {
414 smp_message_recv(PPC_MSG_DEBUGGER_BREAK, regs);
421 void xics_cause_IPI(int cpu)
423 ops->qirr_info(cpu, IPI_PRIORITY);
425 #endif /* CONFIG_SMP */
427 void xics_setup_cpu(void)
429 int cpu = smp_processor_id();
431 ops->cppr_info(cpu, 0xff);
435 * Put the calling processor into the GIQ. This is really only
436 * necessary from a secondary thread as the OF start-cpu interface
437 * performs this function for us on primary threads.
439 * XXX: undo of teardown on kexec needs this too, as may hotplug
441 rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
442 (1UL << interrupt_server_size) - 1 - default_distrib_server, 1);
445 void xics_init_IRQ(void)
448 unsigned long intr_size = 0;
449 struct device_node *np;
450 uint *ireg, ilen, indx = 0;
451 unsigned long intr_base = 0;
452 struct xics_interrupt_node {
457 ppc64_boot_msg(0x20, "XICS Init");
459 ibm_get_xive = rtas_token("ibm,get-xive");
460 ibm_set_xive = rtas_token("ibm,set-xive");
461 ibm_int_on = rtas_token("ibm,int-on");
462 ibm_int_off = rtas_token("ibm,int-off");
464 np = of_find_node_by_type(NULL, "PowerPC-External-Interrupt-Presentation");
466 panic("xics_init_IRQ: can't find interrupt presentation");
469 ireg = (uint *)get_property(np, "ibm,interrupt-server-ranges", NULL);
472 * set node starting index for this node
477 ireg = (uint *)get_property(np, "reg", &ilen);
479 panic("xics_init_IRQ: can't find interrupt reg property");
482 intnodes[indx].addr = (unsigned long)*ireg++ << 32;
483 ilen -= sizeof(uint);
484 intnodes[indx].addr |= *ireg++;
485 ilen -= sizeof(uint);
486 intnodes[indx].size = (unsigned long)*ireg++ << 32;
487 ilen -= sizeof(uint);
488 intnodes[indx].size |= *ireg++;
489 ilen -= sizeof(uint);
491 if (indx >= NR_CPUS) break;
494 np = of_find_node_by_type(np, "PowerPC-External-Interrupt-Presentation");
495 if ((indx < NR_CPUS) && np) goto nextnode;
497 /* Find the server numbers for the boot cpu. */
498 for (np = of_find_node_by_type(NULL, "cpu");
500 np = of_find_node_by_type(np, "cpu")) {
501 ireg = (uint *)get_property(np, "reg", &ilen);
502 if (ireg && ireg[0] == boot_cpuid_phys) {
503 ireg = (uint *)get_property(np, "ibm,ppc-interrupt-gserver#s",
505 i = ilen / sizeof(int);
507 default_server = ireg[0];
508 default_distrib_server = ireg[i-1]; /* take last element */
510 ireg = (uint *)get_property(np,
511 "ibm,interrupt-server#-size", NULL);
513 interrupt_server_size = *ireg;
519 intr_base = intnodes[0].addr;
520 intr_size = intnodes[0].size;
522 np = of_find_node_by_type(NULL, "interrupt-controller");
524 printk(KERN_WARNING "xics: no ISA interrupt controller\n");
525 xics_irq_8259_cascade_real = -1;
526 xics_irq_8259_cascade = -1;
528 ireg = (uint *) get_property(np, "interrupts", NULL);
530 panic("xics_init_IRQ: can't find ISA interrupts property");
532 xics_irq_8259_cascade_real = *ireg;
533 xics_irq_8259_cascade
534 = virt_irq_create_mapping(xics_irq_8259_cascade_real);
539 if (platform_is_lpar())
540 ops = &pSeriesLP_ops;
546 /* FIXME: Do this dynamically! --RR */
550 hard_id = get_hard_smp_processor_id(i);
551 xics_per_cpu[i] = ioremap(intnodes[hard_id].addr,
552 intnodes[hard_id].size);
555 xics_per_cpu[0] = ioremap(intr_base, intr_size);
556 #endif /* CONFIG_SMP */
559 for (i = irq_offset_value(); i < NR_IRQS; ++i)
560 get_irq_desc(i)->handler = &xics_pic;
564 ppc64_boot_msg(0x21, "XICS Done");
568 * We cant do this in init_IRQ because we need the memory subsystem up for
571 static int __init xics_setup_i8259(void)
573 if (ppc64_interrupt_controller == IC_PPC_XIC &&
574 xics_irq_8259_cascade != -1) {
575 if (request_irq(irq_offset_up(xics_irq_8259_cascade),
576 no_action, 0, "8259 cascade", NULL))
577 printk(KERN_ERR "xics_setup_i8259: couldn't get 8259 "
582 arch_initcall(xics_setup_i8259);
585 void xics_request_IPIs(void)
587 virt_irq_to_real_map[XICS_IPI] = XICS_IPI;
589 /* IPIs are marked SA_INTERRUPT as they must run with irqs disabled */
590 request_irq(irq_offset_up(XICS_IPI), xics_ipi_action, SA_INTERRUPT,
592 get_irq_desc(irq_offset_up(XICS_IPI))->status |= IRQ_PER_CPU;
596 static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
601 unsigned long newmask;
602 cpumask_t tmp = CPU_MASK_NONE;
604 irq = virt_irq_to_real(irq_offset_down(virq));
605 if (irq == XICS_IPI || irq == NO_IRQ)
608 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
611 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
612 "returns %d\n", irq, status);
616 /* For the moment only implement delivery to all cpus or one cpu */
617 if (cpus_equal(cpumask, CPU_MASK_ALL)) {
618 newmask = default_distrib_server;
620 cpus_and(tmp, cpu_online_map, cpumask);
623 newmask = get_hard_smp_processor_id(first_cpu(tmp));
626 status = rtas_call(ibm_set_xive, 3, 1, NULL,
627 irq, newmask, xics_status[1]);
630 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
631 "returns %d\n", irq, status);
636 void xics_teardown_cpu(int secondary)
638 int cpu = smp_processor_id();
640 ops->cppr_info(cpu, 0x00);
644 * Some machines need to have at least one cpu in the GIQ,
645 * so leave the master cpu in the group.
649 * we need to EOI the IPI if we got here from kexec down IPI
651 * probably need to check all the other interrupts too
652 * should we be flagging idle loop instead?
653 * or creating some task to be scheduled?
655 ops->xirr_info_set(cpu, XICS_IPI);
656 rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
657 (1UL << interrupt_server_size) - 1 -
658 default_distrib_server, 0);
662 #ifdef CONFIG_HOTPLUG_CPU
664 /* Interrupts are disabled. */
665 void xics_migrate_irqs_away(void)
668 unsigned int irq, virq, cpu = smp_processor_id();
670 /* Reject any interrupt that was queued to us... */
671 ops->cppr_info(cpu, 0);
674 /* remove ourselves from the global interrupt queue */
675 status = rtas_set_indicator(GLOBAL_INTERRUPT_QUEUE,
676 (1UL << interrupt_server_size) - 1 - default_distrib_server, 0);
679 /* Allow IPIs again... */
680 ops->cppr_info(cpu, DEFAULT_PRIORITY);
688 /* We cant set affinity on ISA interrupts */
689 if (virq < irq_offset_value())
692 desc = get_irq_desc(virq);
693 irq = virt_irq_to_real(irq_offset_down(virq));
695 /* We need to get IPIs still. */
696 if (irq == XICS_IPI || irq == NO_IRQ)
699 /* We only need to migrate enabled IRQS */
700 if (desc == NULL || desc->handler == NULL
701 || desc->action == NULL
702 || desc->handler->set_affinity == NULL)
705 spin_lock_irqsave(&desc->lock, flags);
707 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
709 printk(KERN_ERR "migrate_irqs_away: irq=%u "
710 "ibm,get-xive returns %d\n",
716 * We only support delivery to all cpus or to one cpu.
717 * The irq has to be migrated only in the single cpu
720 if (xics_status[0] != get_hard_smp_processor_id(cpu))
723 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
726 /* Reset affinity to all cpus */
727 desc->handler->set_affinity(virq, CPU_MASK_ALL);
728 irq_affinity[virq] = CPU_MASK_ALL;
730 spin_unlock_irqrestore(&desc->lock, flags);