blob: 14469cf9df68dc8497408a8953cbbbcf7b360ea1 [file] [log] [blame]
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +10001/*
2 * Copyright 2011 IBM Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 */
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/irq.h>
13#include <linux/smp.h>
14#include <linux/interrupt.h>
15#include <linux/init.h>
16#include <linux/cpu.h>
17#include <linux/of.h>
18
19#include <asm/smp.h>
20#include <asm/irq.h>
21#include <asm/errno.h>
22#include <asm/xics.h>
23#include <asm/io.h>
24#include <asm/hvcall.h>
25
26static inline unsigned int icp_hv_get_xirr(unsigned char cppr)
27{
28 unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
29 long rc;
Anton Blanchard3ce21cd2011-11-24 19:39:36 +000030 unsigned int ret = XICS_IRQ_SPURIOUS;
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100031
32 rc = plpar_hcall(H_XIRR, retbuf, cppr);
Anton Blanchard3ce21cd2011-11-24 19:39:36 +000033 if (rc == H_SUCCESS) {
34 ret = (unsigned int)retbuf[0];
35 } else {
36 pr_err("%s: bad return code xirr cppr=0x%x returned %ld\n",
37 __func__, cppr, rc);
38 WARN_ON_ONCE(1);
39 }
40
41 return ret;
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100042}
43
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100044static inline void icp_hv_set_cppr(u8 value)
45{
46 long rc = plpar_hcall_norets(H_CPPR, value);
Anton Blanchard3ce21cd2011-11-24 19:39:36 +000047 if (rc != H_SUCCESS) {
48 pr_err("%s: bad return code cppr cppr=0x%x returned %ld\n",
49 __func__, value, rc);
50 WARN_ON_ONCE(1);
51 }
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100052}
53
Anton Blanchard90e8f57c2011-11-30 00:23:15 +000054static inline void icp_hv_set_xirr(unsigned int value)
55{
56 long rc = plpar_hcall_norets(H_EOI, value);
57 if (rc != H_SUCCESS) {
58 pr_err("%s: bad return code eoi xirr=0x%x returned %ld\n",
59 __func__, value, rc);
60 WARN_ON_ONCE(1);
61 icp_hv_set_cppr(value >> 24);
62 }
63}
64
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100065static inline void icp_hv_set_qirr(int n_cpu , u8 value)
66{
Anton Blanchard3ce21cd2011-11-24 19:39:36 +000067 int hw_cpu = get_hard_smp_processor_id(n_cpu);
68 long rc = plpar_hcall_norets(H_IPI, hw_cpu, value);
69 if (rc != H_SUCCESS) {
70 pr_err("%s: bad return code qirr cpu=%d hw_cpu=%d mfrr=0x%x "
71 "returned %ld\n", __func__, n_cpu, hw_cpu, value, rc);
72 WARN_ON_ONCE(1);
73 }
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100074}
75
76static void icp_hv_eoi(struct irq_data *d)
77{
Grant Likely476eb492011-05-04 15:02:15 +100078 unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100079
80 iosync();
81 icp_hv_set_xirr((xics_pop_cppr() << 24) | hw_irq);
82}
83
84static void icp_hv_teardown_cpu(void)
85{
86 int cpu = smp_processor_id();
87
88 /* Clear any pending IPI */
89 icp_hv_set_qirr(cpu, 0xff);
90}
91
92static void icp_hv_flush_ipi(void)
93{
94 /* We take the ipi irq but and never return so we
95 * need to EOI the IPI, but want to leave our priority 0
96 *
97 * should we check all the other interrupts too?
98 * should we be flagging idle loop instead?
99 * or creating some task to be scheduled?
100 */
101
102 icp_hv_set_xirr((0x00 << 24) | XICS_IPI);
103}
104
105static unsigned int icp_hv_get_irq(void)
106{
107 unsigned int xirr = icp_hv_get_xirr(xics_cppr_top());
108 unsigned int vec = xirr & 0x00ffffff;
109 unsigned int irq;
110
111 if (vec == XICS_IRQ_SPURIOUS)
112 return NO_IRQ;
113
Grant Likelyd6b0d1f2012-06-03 22:04:37 -0700114 irq = irq_find_mapping(xics_host, vec);
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000115 if (likely(irq != NO_IRQ)) {
116 xics_push_cppr(vec);
117 return irq;
118 }
119
120 /* We don't have a linux mapping, so have rtas mask it. */
121 xics_mask_unknown_vec(vec);
122
123 /* We might learn about it later, so EOI it */
124 icp_hv_set_xirr(xirr);
125
126 return NO_IRQ;
127}
128
129static void icp_hv_set_cpu_priority(unsigned char cppr)
130{
131 xics_set_base_cppr(cppr);
132 icp_hv_set_cppr(cppr);
133 iosync();
134}
135
136#ifdef CONFIG_SMP
137
Milton Miller23d72bf2011-05-10 19:29:39 +0000138static void icp_hv_cause_ipi(int cpu, unsigned long data)
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000139{
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000140 icp_hv_set_qirr(cpu, IPI_PRIORITY);
141}
142
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000143static irqreturn_t icp_hv_ipi_action(int irq, void *dev_id)
144{
145 int cpu = smp_processor_id();
146
147 icp_hv_set_qirr(cpu, 0xff);
148
Milton Miller23d72bf2011-05-10 19:29:39 +0000149 return smp_ipi_demux();
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000150}
151
152#endif /* CONFIG_SMP */
153
154static const struct icp_ops icp_hv_ops = {
155 .get_irq = icp_hv_get_irq,
156 .eoi = icp_hv_eoi,
157 .set_priority = icp_hv_set_cpu_priority,
158 .teardown_cpu = icp_hv_teardown_cpu,
159 .flush_ipi = icp_hv_flush_ipi,
160#ifdef CONFIG_SMP
161 .ipi_action = icp_hv_ipi_action,
Milton Miller23d72bf2011-05-10 19:29:39 +0000162 .cause_ipi = icp_hv_cause_ipi,
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +1000163#endif
164};
165
166int icp_hv_init(void)
167{
168 struct device_node *np;
169
170 np = of_find_compatible_node(NULL, NULL, "ibm,ppc-xicp");
171 if (!np)
172 np = of_find_node_by_type(NULL,
173 "PowerPC-External-Interrupt-Presentation");
174 if (!np)
175 return -ENODEV;
176
177 icp_ops = &icp_hv_ops;
178
179 return 0;
180}
181