1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/types.h>
3 #include <linux/kernel.h>
4 #include <linux/irq.h>
5 #include <linux/smp.h>
6 #include <linux/interrupt.h>
7 #include <linux/init.h>
8 #include <linux/cpu.h>
9 #include <linux/of.h>
10 #include <linux/spinlock.h>
11 #include <linux/msi.h>
12 
13 #include <asm/prom.h>
14 #include <asm/smp.h>
15 #include <asm/machdep.h>
16 #include <asm/irq.h>
17 #include <asm/errno.h>
18 #include <asm/xics.h>
19 #include <asm/rtas.h>
20 
21 /* RTAS service tokens */
22 static int ibm_get_xive;
23 static int ibm_set_xive;
24 static int ibm_int_on;
25 static int ibm_int_off;
26 
ics_rtas_unmask_irq(struct irq_data * d)27 static void ics_rtas_unmask_irq(struct irq_data *d)
28 {
29 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
30 	int call_status;
31 	int server;
32 
33 	pr_devel("xics: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq);
34 
35 	if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
36 		return;
37 
38 	server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0);
39 
40 	call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq,
41 					  server, DEFAULT_PRIORITY);
42 	if (call_status != 0) {
43 		printk(KERN_ERR
44 			"%s: ibm_set_xive irq %u server %x returned %d\n",
45 			__func__, hw_irq, server, call_status);
46 		return;
47 	}
48 
49 	/* Now unmask the interrupt (often a no-op) */
50 	call_status = rtas_call_reentrant(ibm_int_on, 1, 1, NULL, hw_irq);
51 	if (call_status != 0) {
52 		printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n",
53 			__func__, hw_irq, call_status);
54 		return;
55 	}
56 }
57 
ics_rtas_startup(struct irq_data * d)58 static unsigned int ics_rtas_startup(struct irq_data *d)
59 {
60 	/* unmask it */
61 	ics_rtas_unmask_irq(d);
62 	return 0;
63 }
64 
ics_rtas_mask_real_irq(unsigned int hw_irq)65 static void ics_rtas_mask_real_irq(unsigned int hw_irq)
66 {
67 	int call_status;
68 
69 	if (hw_irq == XICS_IPI)
70 		return;
71 
72 	call_status = rtas_call_reentrant(ibm_int_off, 1, 1, NULL, hw_irq);
73 	if (call_status != 0) {
74 		printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n",
75 			__func__, hw_irq, call_status);
76 		return;
77 	}
78 
79 	/* Have to set XIVE to 0xff to be able to remove a slot */
80 	call_status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL, hw_irq,
81 					  xics_default_server, 0xff);
82 	if (call_status != 0) {
83 		printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n",
84 			__func__, hw_irq, call_status);
85 		return;
86 	}
87 }
88 
ics_rtas_mask_irq(struct irq_data * d)89 static void ics_rtas_mask_irq(struct irq_data *d)
90 {
91 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
92 
93 	pr_devel("xics: mask virq %d [hw 0x%x]\n", d->irq, hw_irq);
94 
95 	if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
96 		return;
97 	ics_rtas_mask_real_irq(hw_irq);
98 }
99 
ics_rtas_set_affinity(struct irq_data * d,const struct cpumask * cpumask,bool force)100 static int ics_rtas_set_affinity(struct irq_data *d,
101 				 const struct cpumask *cpumask,
102 				 bool force)
103 {
104 	unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
105 	int status;
106 	int xics_status[2];
107 	int irq_server;
108 
109 	if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
110 		return -1;
111 
112 	status = rtas_call_reentrant(ibm_get_xive, 1, 3, xics_status, hw_irq);
113 
114 	if (status) {
115 		printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n",
116 			__func__, hw_irq, status);
117 		return -1;
118 	}
119 
120 	irq_server = xics_get_irq_server(d->irq, cpumask, 1);
121 	if (irq_server == -1) {
122 		pr_warn("%s: No online cpus in the mask %*pb for irq %d\n",
123 			__func__, cpumask_pr_args(cpumask), d->irq);
124 		return -1;
125 	}
126 
127 	pr_debug("%s: irq %d [hw 0x%x] server: 0x%x\n", __func__, d->irq,
128 		 hw_irq, irq_server);
129 
130 	status = rtas_call_reentrant(ibm_set_xive, 3, 1, NULL,
131 				     hw_irq, irq_server, xics_status[1]);
132 
133 	if (status) {
134 		printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n",
135 			__func__, hw_irq, status);
136 		return -1;
137 	}
138 
139 	return IRQ_SET_MASK_OK;
140 }
141 
142 static struct irq_chip ics_rtas_irq_chip = {
143 	.name = "XICS",
144 	.irq_startup = ics_rtas_startup,
145 	.irq_mask = ics_rtas_mask_irq,
146 	.irq_unmask = ics_rtas_unmask_irq,
147 	.irq_eoi = NULL, /* Patched at init time */
148 	.irq_set_affinity = ics_rtas_set_affinity,
149 	.irq_set_type = xics_set_irq_type,
150 	.irq_retrigger = xics_retrigger,
151 };
152 
ics_rtas_check(struct ics * ics,unsigned int hw_irq)153 static int ics_rtas_check(struct ics *ics, unsigned int hw_irq)
154 {
155 	int status[2];
156 	int rc;
157 
158 	if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
159 		return -EINVAL;
160 
161 	/* Check if RTAS knows about this interrupt */
162 	rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, hw_irq);
163 	if (rc)
164 		return -ENXIO;
165 
166 	return 0;
167 }
168 
ics_rtas_mask_unknown(struct ics * ics,unsigned long vec)169 static void ics_rtas_mask_unknown(struct ics *ics, unsigned long vec)
170 {
171 	ics_rtas_mask_real_irq(vec);
172 }
173 
ics_rtas_get_server(struct ics * ics,unsigned long vec)174 static long ics_rtas_get_server(struct ics *ics, unsigned long vec)
175 {
176 	int rc, status[2];
177 
178 	rc = rtas_call_reentrant(ibm_get_xive, 1, 3, status, vec);
179 	if (rc)
180 		return -1;
181 	return status[0];
182 }
183 
ics_rtas_host_match(struct ics * ics,struct device_node * node)184 static int ics_rtas_host_match(struct ics *ics, struct device_node *node)
185 {
186 	/* IBM machines have interrupt parents of various funky types for things
187 	 * like vdevices, events, etc... The trick we use here is to match
188 	 * everything here except the legacy 8259 which is compatible "chrp,iic"
189 	 */
190 	return !of_device_is_compatible(node, "chrp,iic");
191 }
192 
193 /* Only one global & state struct ics */
194 static struct ics ics_rtas = {
195 	.check		= ics_rtas_check,
196 	.mask_unknown	= ics_rtas_mask_unknown,
197 	.get_server	= ics_rtas_get_server,
198 	.host_match	= ics_rtas_host_match,
199 	.chip = &ics_rtas_irq_chip,
200 };
201 
ics_rtas_init(void)202 __init int ics_rtas_init(void)
203 {
204 	ibm_get_xive = rtas_token("ibm,get-xive");
205 	ibm_set_xive = rtas_token("ibm,set-xive");
206 	ibm_int_on  = rtas_token("ibm,int-on");
207 	ibm_int_off = rtas_token("ibm,int-off");
208 
209 	/* We enable the RTAS "ICS" if RTAS is present with the
210 	 * appropriate tokens
211 	 */
212 	if (ibm_get_xive == RTAS_UNKNOWN_SERVICE ||
213 	    ibm_set_xive == RTAS_UNKNOWN_SERVICE)
214 		return -ENODEV;
215 
216 	/* We need to patch our irq chip's EOI to point to the
217 	 * right ICP
218 	 */
219 	ics_rtas_irq_chip.irq_eoi = icp_ops->eoi;
220 
221 	/* Register ourselves */
222 	xics_register_ics(&ics_rtas);
223 
224 	return 0;
225 }
226 
227