[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / arch / powerpc / platforms / pseries / xics.c
blobfd823c7c9ac88e6db77d9d912750819df682ccd3
1 /*
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/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/hvcall.h>
29 #include <asm/machdep.h>
30 #include <asm/i8259.h>
32 #include "xics.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 = {
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
51 /* This is used to map real irq numbers to virtual */
52 static struct radix_tree_root irq_map = RADIX_TREE_INIT(GFP_ATOMIC);
54 #define XICS_IPI 2
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
66 struct xics_ipl {
67 union {
68 u32 word;
69 u8 bytes[4];
70 } xirr_poll;
71 union {
72 u32 word;
73 u8 bytes[4];
74 } xirr;
75 u32 dummy;
76 union {
77 u32 word;
78 u8 bytes[4];
79 } qirr;
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;
101 typedef struct {
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);
106 } xics_ops;
109 /* SMP */
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,
134 pSeries_cppr_info,
135 pSeries_qirr_info
138 static xics_ops *ops = &pSeries_ops;
141 /* LPAR */
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)
160 unsigned long dummy;
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,
183 val64);
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,
207 pSeriesLP_cppr_info,
208 pSeriesLP_qirr_info
211 static unsigned int xics_startup(unsigned int virq)
213 unsigned int irq;
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)
227 unsigned int *ptr;
229 ptr = radix_tree_lookup(&irq_map, real_irq);
230 if (ptr == NULL)
231 return NO_IRQ;
232 return ptr - virt_irq_to_real_map;
235 #ifdef CONFIG_SMP
236 static int get_irq_server(unsigned int irq)
238 unsigned int server;
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;
248 } else {
249 cpus_and(tmp, cpu_online_map, cpumask);
251 if (cpus_empty(tmp))
252 server = default_distrib_server;
253 else
254 server = get_hard_smp_processor_id(first_cpu(tmp));
257 return server;
260 #else
261 static int get_irq_server(unsigned int irq)
263 return default_server;
265 #endif
267 static void xics_enable_irq(unsigned int virq)
269 unsigned int irq;
270 int call_status;
271 unsigned int server;
273 irq = virt_irq_to_real(irq_offset_down(virq));
274 if (irq == XICS_IPI)
275 return;
277 server = get_irq_server(virq);
278 call_status = rtas_call(ibm_set_xive, 3, 1, NULL, irq, server,
279 DEFAULT_PRIORITY);
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);
284 return;
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);
292 return;
296 static void xics_disable_real_irq(unsigned int irq)
298 int call_status;
299 unsigned int server;
301 if (irq == XICS_IPI)
302 return;
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);
308 return;
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);
317 return;
321 static void xics_disable_irq(unsigned int virq)
323 unsigned int irq;
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();
333 iosync();
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()) {
344 i8259_pic.ack(irq);
345 iosync();
346 ops->xirr_info_set(cpu, ((0xff<<24) |
347 xics_irq_8259_cascade_real));
348 iosync();
352 int xics_get_irq(struct pt_regs *regs)
354 unsigned int cpu = smp_processor_id();
355 unsigned int vec;
356 int irq;
358 vec = ops->xirr_info_get(cpu);
359 /* (vec >> 24) == old priority */
360 vec &= 0x00ffffff;
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) {
367 irq = -1;
368 } else {
369 irq = real_irq_to_virt(vec);
370 if (irq == NO_IRQ)
371 irq = real_irq_to_virt_slowpath(vec);
372 if (irq == NO_IRQ) {
373 printk(KERN_ERR "Interrupt %u (real) is invalid,"
374 " disabling it.\n", vec);
375 xics_disable_real_irq(vec);
376 } else
377 irq = irq_offset_up(irq);
379 return irq;
382 #ifdef CONFIG_SMP
384 static 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)) {
395 mb();
396 smp_message_recv(PPC_MSG_CALL_FUNCTION, regs);
398 if (test_and_clear_bit(PPC_MSG_RESCHEDULE,
399 &xics_ipi_message[cpu].value)) {
400 mb();
401 smp_message_recv(PPC_MSG_RESCHEDULE, regs);
403 #if 0
404 if (test_and_clear_bit(PPC_MSG_MIGRATE_TASK,
405 &xics_ipi_message[cpu].value)) {
406 mb();
407 smp_message_recv(PPC_MSG_MIGRATE_TASK, regs);
409 #endif
410 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
411 if (test_and_clear_bit(PPC_MSG_DEBUGGER_BREAK,
412 &xics_ipi_message[cpu].value)) {
413 mb();
414 smp_message_recv(PPC_MSG_DEBUGGER_BREAK, regs);
416 #endif
418 return IRQ_HANDLED;
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);
432 iosync();
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)
447 int i;
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 {
453 unsigned long addr;
454 unsigned long size;
455 } intnodes[NR_CPUS];
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");
465 if (!np)
466 panic("xics_init_IRQ: can't find interrupt presentation");
468 nextnode:
469 ireg = (uint *)get_property(np, "ibm,interrupt-server-ranges", NULL);
470 if (ireg) {
472 * set node starting index for this node
474 indx = *ireg;
477 ireg = (uint *)get_property(np, "reg", &ilen);
478 if (!ireg)
479 panic("xics_init_IRQ: can't find interrupt reg property");
481 while (ilen) {
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);
490 indx++;
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",
504 &ilen);
505 i = ilen / sizeof(int);
506 if (ireg && i > 0) {
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);
512 if (ireg)
513 interrupt_server_size = *ireg;
514 break;
517 of_node_put(np);
519 intr_base = intnodes[0].addr;
520 intr_size = intnodes[0].size;
522 np = of_find_node_by_type(NULL, "interrupt-controller");
523 if (!np) {
524 printk(KERN_WARNING "xics: no ISA interrupt controller\n");
525 xics_irq_8259_cascade_real = -1;
526 xics_irq_8259_cascade = -1;
527 } else {
528 ireg = (uint *) get_property(np, "interrupts", NULL);
529 if (!ireg)
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);
535 i8259_init(0, 0);
536 of_node_put(np);
539 if (platform_is_lpar())
540 ops = &pSeriesLP_ops;
541 else {
542 #ifdef CONFIG_SMP
543 for_each_cpu(i) {
544 int hard_id;
546 /* FIXME: Do this dynamically! --RR */
547 if (!cpu_present(i))
548 continue;
550 hard_id = get_hard_smp_processor_id(i);
551 xics_per_cpu[i] = ioremap(intnodes[hard_id].addr,
552 intnodes[hard_id].size);
554 #else
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;
562 xics_setup_cpu();
564 ppc64_boot_msg(0x21, "XICS Done");
568 * We cant do this in init_IRQ because we need the memory subsystem up for
569 * request_irq()
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 "
578 "cascade\n");
580 return 0;
582 arch_initcall(xics_setup_i8259);
584 #ifdef CONFIG_SMP
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,
591 "IPI", NULL);
592 get_irq_desc(irq_offset_up(XICS_IPI))->status |= IRQ_PER_CPU;
594 #endif
596 static void xics_set_affinity(unsigned int virq, cpumask_t cpumask)
598 unsigned int irq;
599 int status;
600 int xics_status[2];
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)
606 return;
608 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
610 if (status) {
611 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,get-xive "
612 "returns %d\n", irq, status);
613 return;
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;
619 } else {
620 cpus_and(tmp, cpu_online_map, cpumask);
621 if (cpus_empty(tmp))
622 return;
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]);
629 if (status) {
630 printk(KERN_ERR "xics_set_affinity: irq=%u ibm,set-xive "
631 "returns %d\n", irq, status);
632 return;
636 void xics_teardown_cpu(int secondary)
638 int cpu = smp_processor_id();
640 ops->cppr_info(cpu, 0x00);
641 iosync();
644 * Some machines need to have at least one cpu in the GIQ,
645 * so leave the master cpu in the group.
647 if (secondary) {
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)
667 int status;
668 unsigned int irq, virq, cpu = smp_processor_id();
670 /* Reject any interrupt that was queued to us... */
671 ops->cppr_info(cpu, 0);
672 iosync();
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);
677 WARN_ON(status < 0);
679 /* Allow IPIs again... */
680 ops->cppr_info(cpu, DEFAULT_PRIORITY);
681 iosync();
683 for_each_irq(virq) {
684 irq_desc_t *desc;
685 int xics_status[2];
686 unsigned long flags;
688 /* We cant set affinity on ISA interrupts */
689 if (virq < irq_offset_value())
690 continue;
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)
697 continue;
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)
703 continue;
705 spin_lock_irqsave(&desc->lock, flags);
707 status = rtas_call(ibm_get_xive, 1, 3, xics_status, irq);
708 if (status) {
709 printk(KERN_ERR "migrate_irqs_away: irq=%u "
710 "ibm,get-xive returns %d\n",
711 virq, status);
712 goto unlock;
716 * We only support delivery to all cpus or to one cpu.
717 * The irq has to be migrated only in the single cpu
718 * case.
720 if (xics_status[0] != get_hard_smp_processor_id(cpu))
721 goto unlock;
723 printk(KERN_WARNING "IRQ %u affinity broken off cpu %u\n",
724 virq, cpu);
726 /* Reset affinity to all cpus */
727 desc->handler->set_affinity(virq, CPU_MASK_ALL);
728 irq_affinity[virq] = CPU_MASK_ALL;
729 unlock:
730 spin_unlock_irqrestore(&desc->lock, flags);
733 #endif