xref: /freebsd/sys/powerpc/pseries/xics.c (revision b37f6c9805edb4b89f0a8c2b78f78a3dcfc0647b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2011 Nathan Whitehorn
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_platform.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/smp.h>
41 
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
48 #include <machine/rtas.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 
53 #ifdef POWERNV
54 #include <powerpc/powernv/opal.h>
55 #endif
56 
57 #include "phyp-hvcall.h"
58 #include "pic_if.h"
59 
60 #define XICP_PRIORITY	5	/* Random non-zero number */
61 #define XICP_IPI	2
62 #define MAX_XICP_IRQS	(1<<24)	/* 24-bit XIRR field */
63 
64 static int	xicp_probe(device_t);
65 static int	xicp_attach(device_t);
66 static int	xics_probe(device_t);
67 static int	xics_attach(device_t);
68 
69 static void	xicp_bind(device_t dev, u_int irq, cpuset_t cpumask);
70 static void	xicp_dispatch(device_t, struct trapframe *);
71 static void	xicp_enable(device_t, u_int, u_int);
72 static void	xicp_eoi(device_t, u_int);
73 static void	xicp_ipi(device_t, u_int);
74 static void	xicp_mask(device_t, u_int);
75 static void	xicp_unmask(device_t, u_int);
76 
77 static device_method_t  xicp_methods[] = {
78 	/* Device interface */
79 	DEVMETHOD(device_probe,		xicp_probe),
80 	DEVMETHOD(device_attach,	xicp_attach),
81 
82 	/* PIC interface */
83 	DEVMETHOD(pic_bind,		xicp_bind),
84 	DEVMETHOD(pic_dispatch,		xicp_dispatch),
85 	DEVMETHOD(pic_enable,		xicp_enable),
86 	DEVMETHOD(pic_eoi,		xicp_eoi),
87 	DEVMETHOD(pic_ipi,		xicp_ipi),
88 	DEVMETHOD(pic_mask,		xicp_mask),
89 	DEVMETHOD(pic_unmask,		xicp_unmask),
90 
91 	DEVMETHOD_END
92 };
93 
94 static device_method_t  xics_methods[] = {
95 	/* Device interface */
96 	DEVMETHOD(device_probe,		xics_probe),
97 	DEVMETHOD(device_attach,	xics_attach),
98 
99 	DEVMETHOD_END
100 };
101 
102 struct xicp_softc {
103 	struct mtx sc_mtx;
104 	struct resource *mem[MAXCPU];
105 
106 	int cpu_range[2];
107 
108 	int ibm_int_on;
109 	int ibm_int_off;
110 	int ibm_get_xive;
111 	int ibm_set_xive;
112 
113 	/* XXX: inefficient -- hash table? tree? */
114 	struct {
115 		int irq;
116 		int vector;
117 		int cpu;
118 	} intvecs[256];
119 	int nintvecs;
120 };
121 
122 static driver_t xicp_driver = {
123 	"xicp",
124 	xicp_methods,
125 	sizeof(struct xicp_softc)
126 };
127 
128 static driver_t xics_driver = {
129 	"xics",
130 	xics_methods,
131 	0
132 };
133 
134 static devclass_t xicp_devclass;
135 static devclass_t xics_devclass;
136 
137 EARLY_DRIVER_MODULE(xicp, ofwbus, xicp_driver, xicp_devclass, 0, 0,
138     BUS_PASS_INTERRUPT-1);
139 EARLY_DRIVER_MODULE(xics, ofwbus, xics_driver, xics_devclass, 0, 0,
140     BUS_PASS_INTERRUPT);
141 
142 #ifdef POWERNV
143 static struct resource *
144 xicp_mem_for_cpu(int cpu)
145 {
146 	device_t dev;
147 	struct xicp_softc *sc;
148 	int i;
149 
150 	for (i = 0; (dev = devclass_get_device(xicp_devclass, i)) != NULL; i++){
151 		sc = device_get_softc(dev);
152 		if (cpu >= sc->cpu_range[0] && cpu < sc->cpu_range[1])
153 			return (sc->mem[cpu - sc->cpu_range[0]]);
154 	}
155 
156 	return (NULL);
157 }
158 #endif
159 
160 static int
161 xicp_probe(device_t dev)
162 {
163 
164 	if (!ofw_bus_is_compatible(dev, "ibm,ppc-xicp"))
165 		return (ENXIO);
166 
167 	device_set_desc(dev, "External Interrupt Presentation Controller");
168 	return (BUS_PROBE_GENERIC);
169 }
170 
171 static int
172 xics_probe(device_t dev)
173 {
174 
175 	if (!ofw_bus_is_compatible(dev, "ibm,ppc-xics"))
176 		return (ENXIO);
177 
178 	device_set_desc(dev, "External Interrupt Source Controller");
179 	return (BUS_PROBE_GENERIC);
180 }
181 
182 static int
183 xicp_attach(device_t dev)
184 {
185 	struct xicp_softc *sc = device_get_softc(dev);
186 	phandle_t phandle = ofw_bus_get_node(dev);
187 
188 	if (rtas_exists()) {
189 		sc->ibm_int_on = rtas_token_lookup("ibm,int-on");
190 		sc->ibm_int_off = rtas_token_lookup("ibm,int-off");
191 		sc->ibm_set_xive = rtas_token_lookup("ibm,set-xive");
192 		sc->ibm_get_xive = rtas_token_lookup("ibm,get-xive");
193 #ifdef POWERNV
194 	} else if (opal_check() == 0) {
195 		/* No init needed */
196 #endif
197 	} else {
198 		device_printf(dev, "Cannot attach without RTAS or OPAL\n");
199 		return (ENXIO);
200 	}
201 
202 	if (OF_hasprop(phandle, "ibm,interrupt-server-ranges")) {
203 		OF_getencprop(phandle, "ibm,interrupt-server-ranges",
204 		    sc->cpu_range, sizeof(sc->cpu_range));
205 		sc->cpu_range[1] += sc->cpu_range[0];
206 		device_printf(dev, "Handling CPUs %d-%d\n", sc->cpu_range[0],
207 		    sc->cpu_range[1]-1);
208 	} else {
209 		sc->cpu_range[0] = 0;
210 		sc->cpu_range[1] = mp_ncpus;
211 	}
212 
213 #ifdef POWERNV
214 	if (mfmsr() & PSL_HV) {
215 		int i;
216 
217 		for (i = 0; i < sc->cpu_range[1] - sc->cpu_range[0]; i++) {
218 			sc->mem[i] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
219 			    &i, RF_ACTIVE);
220 			if (sc->mem[i] == NULL) {
221 				device_printf(dev, "Could not alloc mem "
222 				    "resource %d\n", i);
223 				return (ENXIO);
224 			}
225 
226 			/* Unmask interrupts on all cores */
227 			bus_write_1(sc->mem[i], 4, 0xff);
228 			bus_write_1(sc->mem[i], 12, 0xff);
229 		}
230 	}
231 #endif
232 
233 	mtx_init(&sc->sc_mtx, "XICP", NULL, MTX_DEF);
234 	sc->nintvecs = 0;
235 
236 	powerpc_register_pic(dev, OF_xref_from_node(phandle), MAX_XICP_IRQS,
237 	    1 /* Number of IPIs */, FALSE);
238 	root_pic = dev;
239 
240 	return (0);
241 }
242 
243 static int
244 xics_attach(device_t dev)
245 {
246 	phandle_t phandle = ofw_bus_get_node(dev);
247 
248 	/* The XICP (root PIC) will handle all our interrupts */
249 	powerpc_register_pic(root_pic, OF_xref_from_node(phandle),
250 	    MAX_XICP_IRQS, 1 /* Number of IPIs */, FALSE);
251 
252 	return (0);
253 }
254 
255 /*
256  * PIC I/F methods.
257  */
258 
259 static void
260 xicp_bind(device_t dev, u_int irq, cpuset_t cpumask)
261 {
262 	struct xicp_softc *sc = device_get_softc(dev);
263 	cell_t status, cpu;
264 	int ncpus, i, error;
265 
266 	/*
267 	 * This doesn't appear to actually support affinity groups, so pick a
268 	 * random CPU.
269 	 */
270 	ncpus = 0;
271 	CPU_FOREACH(cpu)
272 		if (CPU_ISSET(cpu, &cpumask)) ncpus++;
273 
274 	i = mftb() % ncpus;
275 	ncpus = 0;
276 	CPU_FOREACH(cpu) {
277 		if (!CPU_ISSET(cpu, &cpumask))
278 			continue;
279 		if (ncpus == i)
280 			break;
281 		ncpus++;
282 	}
283 
284 	/* XXX: super inefficient */
285 	for (i = 0; i < sc->nintvecs; i++) {
286 		if (sc->intvecs[i].irq == irq) {
287 			sc->intvecs[i].cpu = cpu;
288 			break;
289 		}
290 	}
291 	KASSERT(i < sc->nintvecs, ("Binding non-configured interrupt"));
292 
293 	if (rtas_exists())
294 		error = rtas_call_method(sc->ibm_set_xive, 3, 1, irq, cpu,
295 		    XICP_PRIORITY, &status);
296 #ifdef POWERNV
297 	else
298 		error = opal_call(OPAL_SET_XIVE, irq, cpu << 2, XICP_PRIORITY);
299 #endif
300 
301 	if (error < 0)
302 		panic("Cannot bind interrupt %d to CPU %d", irq, cpu);
303 }
304 
305 static void
306 xicp_dispatch(device_t dev, struct trapframe *tf)
307 {
308 	struct xicp_softc *sc;
309 	struct resource *regs = NULL;
310 	uint64_t xirr, junk;
311 	int i;
312 
313 #ifdef POWERNV
314 	if (mfmsr() & PSL_HV) {
315 		regs = xicp_mem_for_cpu(PCPU_GET(cpuid));
316 		KASSERT(regs != NULL,
317 		    ("Can't find regs for CPU %d", PCPU_GET(cpuid)));
318 	}
319 #endif
320 
321 	sc = device_get_softc(dev);
322 	for (;;) {
323 		/* Return value in R4, use the PFT call */
324 		if (regs) {
325 			xirr = bus_read_4(regs, 4);
326 		} else {
327 			/* Return value in R4, use the PFT call */
328 			phyp_pft_hcall(H_XIRR, 0, 0, 0, 0, &xirr, &junk, &junk);
329 		}
330 		xirr &= 0x00ffffff;
331 
332 		if (xirr == 0) { /* No more pending interrupts? */
333 			if (regs)
334 				bus_write_1(regs, 4, 0xff);
335 			else
336 				phyp_hcall(H_CPPR, (uint64_t)0xff);
337 			break;
338 		}
339 		if (xirr == XICP_IPI) {		/* Magic number for IPIs */
340 			xirr = MAX_XICP_IRQS;	/* Map to FreeBSD magic */
341 
342 			/* Clear IPI */
343 			if (regs)
344 				bus_write_1(regs, 12, 0xff);
345 			else
346 				phyp_hcall(H_IPI, (uint64_t)(PCPU_GET(cpuid)),
347 				    0xff);
348 		}
349 
350 		/* XXX: super inefficient */
351 		for (i = 0; i < sc->nintvecs; i++) {
352 			if (sc->intvecs[i].irq == xirr)
353 				break;
354 		}
355 
356 		KASSERT(i < sc->nintvecs, ("Unmapped XIRR"));
357 		powerpc_dispatch_intr(sc->intvecs[i].vector, tf);
358 	}
359 }
360 
361 static void
362 xicp_enable(device_t dev, u_int irq, u_int vector)
363 {
364 	struct xicp_softc *sc;
365 	cell_t status, cpu;
366 
367 	sc = device_get_softc(dev);
368 
369 	KASSERT(sc->nintvecs + 1 < nitems(sc->intvecs),
370 		("Too many XICP interrupts"));
371 
372 	/* Bind to this CPU to start: distrib. ID is last entry in gserver# */
373 	cpu = PCPU_GET(cpuid);
374 
375 	mtx_lock(&sc->sc_mtx);
376 	sc->intvecs[sc->nintvecs].irq = irq;
377 	sc->intvecs[sc->nintvecs].vector = vector;
378 	sc->intvecs[sc->nintvecs].cpu = cpu;
379 	mb();
380 	sc->nintvecs++;
381 	mtx_unlock(&sc->sc_mtx);
382 
383 	/* IPIs are also enabled */
384 	if (irq == MAX_XICP_IRQS)
385 		return;
386 
387 	if (rtas_exists()) {
388 		rtas_call_method(sc->ibm_set_xive, 3, 1, irq, cpu,
389 		    XICP_PRIORITY, &status);
390 		xicp_unmask(dev, irq);
391 #ifdef POWERNV
392 	} else {
393 		status = opal_call(OPAL_SET_XIVE, irq, cpu << 2, XICP_PRIORITY);
394 		/* Unmask implicit for OPAL */
395 
396 		if (status != 0)
397 			panic("OPAL_SET_XIVE IRQ %d -> cpu %d failed: %d", irq,
398 			    cpu, status);
399 #endif
400 	}
401 }
402 
403 static void
404 xicp_eoi(device_t dev, u_int irq)
405 {
406 	uint64_t xirr;
407 
408 	if (irq == MAX_XICP_IRQS) /* Remap IPI interrupt to internal value */
409 		irq = XICP_IPI;
410 	xirr = irq | (XICP_PRIORITY << 24);
411 
412 #ifdef POWERNV
413 	if (mfmsr() & PSL_HV)
414 		bus_write_4(xicp_mem_for_cpu(PCPU_GET(cpuid)), 4, xirr);
415 	else
416 #endif
417 		phyp_hcall(H_EOI, xirr);
418 }
419 
420 static void
421 xicp_ipi(device_t dev, u_int cpu)
422 {
423 
424 #ifdef POWERNV
425 	if (mfmsr() & PSL_HV)
426 		bus_write_1(xicp_mem_for_cpu(cpu), 12, XICP_PRIORITY);
427 	else
428 #endif
429 		phyp_hcall(H_IPI, (uint64_t)cpu, XICP_PRIORITY);
430 }
431 
432 static void
433 xicp_mask(device_t dev, u_int irq)
434 {
435 	struct xicp_softc *sc = device_get_softc(dev);
436 	cell_t status;
437 
438 	if (irq == MAX_XICP_IRQS)
439 		return;
440 
441 	if (rtas_exists()) {
442 		rtas_call_method(sc->ibm_int_off, 1, 1, irq, &status);
443 #ifdef POWERNV
444 	} else {
445 		int i;
446 
447 		for (i = 0; i < sc->nintvecs; i++) {
448 			if (sc->intvecs[i].irq == irq) {
449 				break;
450 			}
451 		}
452 		KASSERT(i < sc->nintvecs, ("Masking unconfigured interrupt"));
453 		opal_call(OPAL_SET_XIVE, irq, sc->intvecs[i].cpu << 2, 0xff);
454 #endif
455 	}
456 }
457 
458 static void
459 xicp_unmask(device_t dev, u_int irq)
460 {
461 	struct xicp_softc *sc = device_get_softc(dev);
462 	cell_t status;
463 
464 	if (irq == MAX_XICP_IRQS)
465 		return;
466 
467 	if (rtas_exists()) {
468 		rtas_call_method(sc->ibm_int_on, 1, 1, irq, &status);
469 #ifdef POWERNV
470 	} else {
471 		int i;
472 
473 		for (i = 0; i < sc->nintvecs; i++) {
474 			if (sc->intvecs[i].irq == irq) {
475 				break;
476 			}
477 		}
478 		KASSERT(i < sc->nintvecs, ("Unmasking unconfigured interrupt"));
479 		opal_call(OPAL_SET_XIVE, irq, sc->intvecs[i].cpu << 2,
480 		    XICP_PRIORITY);
481 #endif
482 	}
483 }
484 
485