xref: /freebsd/sys/x86/xen/xen_apic.c (revision 15c433351f54e7cd5bec8d36c8e89e6a7fa55b26)
1 /*
2  * Copyright (c) 2014 Roger Pau Monné <roger.pau@citrix.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/proc.h>
35 #include <sys/smp.h>
36 #include <sys/systm.h>
37 
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 
41 #include <machine/cpufunc.h>
42 #include <machine/cpu.h>
43 #include <machine/intr_machdep.h>
44 #include <machine/smp.h>
45 
46 #include <x86/apicreg.h>
47 #include <x86/apicvar.h>
48 
49 #include <xen/xen-os.h>
50 #include <xen/features.h>
51 #include <xen/gnttab.h>
52 #include <xen/hypervisor.h>
53 #include <xen/hvm.h>
54 #include <xen/xen_intr.h>
55 
56 #include <xen/interface/vcpu.h>
57 
58 /*--------------------------------- Macros -----------------------------------*/
59 
60 #define XEN_APIC_UNSUPPORTED \
61 	panic("%s: not available in Xen PV port.", __func__)
62 
63 
64 /*--------------------------- Forward Declarations ---------------------------*/
65 #ifdef SMP
66 static driver_filter_t xen_smp_rendezvous_action;
67 static driver_filter_t xen_invltlb;
68 static driver_filter_t xen_invlpg;
69 static driver_filter_t xen_invlrng;
70 static driver_filter_t xen_invlcache;
71 static driver_filter_t xen_ipi_bitmap_handler;
72 static driver_filter_t xen_cpustop_handler;
73 static driver_filter_t xen_cpususpend_handler;
74 static driver_filter_t xen_cpustophard_handler;
75 #endif
76 
77 /*---------------------------------- Macros ----------------------------------*/
78 #define	IPI_TO_IDX(ipi) ((ipi) - APIC_IPI_INTS)
79 
80 /*--------------------------------- Xen IPIs ---------------------------------*/
81 #ifdef SMP
82 struct xen_ipi_handler
83 {
84 	driver_filter_t	*filter;
85 	const char	*description;
86 };
87 
88 static struct xen_ipi_handler xen_ipis[] =
89 {
90 	[IPI_TO_IDX(IPI_RENDEZVOUS)]	= { xen_smp_rendezvous_action,	"r"   },
91 	[IPI_TO_IDX(IPI_INVLTLB)]	= { xen_invltlb,		"itlb"},
92 	[IPI_TO_IDX(IPI_INVLPG)]	= { xen_invlpg,			"ipg" },
93 	[IPI_TO_IDX(IPI_INVLRNG)]	= { xen_invlrng,		"irg" },
94 	[IPI_TO_IDX(IPI_INVLCACHE)]	= { xen_invlcache,		"ic"  },
95 	[IPI_TO_IDX(IPI_BITMAP_VECTOR)] = { xen_ipi_bitmap_handler,	"b"   },
96 	[IPI_TO_IDX(IPI_STOP)]		= { xen_cpustop_handler,	"st"  },
97 	[IPI_TO_IDX(IPI_SUSPEND)]	= { xen_cpususpend_handler,	"sp"  },
98 	[IPI_TO_IDX(IPI_STOP_HARD)]	= { xen_cpustophard_handler,	"sth" },
99 };
100 #endif
101 
102 /*------------------------------- Per-CPU Data -------------------------------*/
103 #ifdef SMP
104 DPCPU_DEFINE(xen_intr_handle_t, ipi_handle[nitems(xen_ipis)]);
105 #endif
106 
107 /*------------------------------- Xen PV APIC --------------------------------*/
108 
109 static void
110 xen_pv_lapic_create(u_int apic_id, int boot_cpu)
111 {
112 #ifdef SMP
113 	cpu_add(apic_id, boot_cpu);
114 #endif
115 }
116 
117 static void
118 xen_pv_lapic_init(vm_paddr_t addr)
119 {
120 
121 }
122 
123 static void
124 xen_pv_lapic_setup(int boot)
125 {
126 
127 }
128 
129 static void
130 xen_pv_lapic_dump(const char *str)
131 {
132 
133 	printf("cpu%d %s XEN PV LAPIC\n", PCPU_GET(cpuid), str);
134 }
135 
136 static void
137 xen_pv_lapic_disable(void)
138 {
139 
140 }
141 
142 static void
143 xen_pv_lapic_eoi(void)
144 {
145 
146 	XEN_APIC_UNSUPPORTED;
147 }
148 
149 static int
150 xen_pv_lapic_id(void)
151 {
152 
153 	return (PCPU_GET(apic_id));
154 }
155 
156 static int
157 xen_pv_lapic_intr_pending(u_int vector)
158 {
159 
160 	XEN_APIC_UNSUPPORTED;
161 	return (0);
162 }
163 
164 static u_int
165 xen_pv_apic_cpuid(u_int apic_id)
166 {
167 #ifdef SMP
168 	return (apic_cpuids[apic_id]);
169 #else
170 	return (0);
171 #endif
172 }
173 
174 static u_int
175 xen_pv_apic_alloc_vector(u_int apic_id, u_int irq)
176 {
177 
178 	XEN_APIC_UNSUPPORTED;
179 	return (0);
180 }
181 
182 static u_int
183 xen_pv_apic_alloc_vectors(u_int apic_id, u_int *irqs, u_int count, u_int align)
184 {
185 
186 	XEN_APIC_UNSUPPORTED;
187 	return (0);
188 }
189 
190 static void
191 xen_pv_apic_disable_vector(u_int apic_id, u_int vector)
192 {
193 
194 	XEN_APIC_UNSUPPORTED;
195 }
196 
197 static void
198 xen_pv_apic_enable_vector(u_int apic_id, u_int vector)
199 {
200 
201 	XEN_APIC_UNSUPPORTED;
202 }
203 
204 static void
205 xen_pv_apic_free_vector(u_int apic_id, u_int vector, u_int irq)
206 {
207 
208 	XEN_APIC_UNSUPPORTED;
209 }
210 
211 static void
212 xen_pv_lapic_set_logical_id(u_int apic_id, u_int cluster, u_int cluster_id)
213 {
214 
215 	XEN_APIC_UNSUPPORTED;
216 }
217 
218 static int
219 xen_pv_lapic_enable_pmc(void)
220 {
221 
222 	XEN_APIC_UNSUPPORTED;
223 	return (0);
224 }
225 
226 static void
227 xen_pv_lapic_disable_pmc(void)
228 {
229 
230 	XEN_APIC_UNSUPPORTED;
231 }
232 
233 static void
234 xen_pv_lapic_reenable_pmc(void)
235 {
236 
237 	XEN_APIC_UNSUPPORTED;
238 }
239 
240 static void
241 xen_pv_lapic_enable_cmc(void)
242 {
243 
244 }
245 
246 #ifdef SMP
247 static void
248 xen_pv_lapic_ipi_raw(register_t icrlo, u_int dest)
249 {
250 
251 	XEN_APIC_UNSUPPORTED;
252 }
253 
254 static void
255 xen_pv_lapic_ipi_vectored(u_int vector, int dest)
256 {
257 	xen_intr_handle_t *ipi_handle;
258 	int ipi_idx, to_cpu, self;
259 
260 	ipi_idx = IPI_TO_IDX(vector);
261 	if (ipi_idx >= nitems(xen_ipis))
262 		panic("IPI out of range");
263 
264 	switch(dest) {
265 	case APIC_IPI_DEST_SELF:
266 		ipi_handle = DPCPU_GET(ipi_handle);
267 		xen_intr_signal(ipi_handle[ipi_idx]);
268 		break;
269 	case APIC_IPI_DEST_ALL:
270 		CPU_FOREACH(to_cpu) {
271 			ipi_handle = DPCPU_ID_GET(to_cpu, ipi_handle);
272 			xen_intr_signal(ipi_handle[ipi_idx]);
273 		}
274 		break;
275 	case APIC_IPI_DEST_OTHERS:
276 		self = PCPU_GET(cpuid);
277 		CPU_FOREACH(to_cpu) {
278 			if (to_cpu != self) {
279 				ipi_handle = DPCPU_ID_GET(to_cpu, ipi_handle);
280 				xen_intr_signal(ipi_handle[ipi_idx]);
281 			}
282 		}
283 		break;
284 	default:
285 		to_cpu = apic_cpuid(dest);
286 		ipi_handle = DPCPU_ID_GET(to_cpu, ipi_handle);
287 		xen_intr_signal(ipi_handle[ipi_idx]);
288 		break;
289 	}
290 }
291 
292 static int
293 xen_pv_lapic_ipi_wait(int delay)
294 {
295 
296 	XEN_APIC_UNSUPPORTED;
297 	return (0);
298 }
299 
300 static int
301 xen_pv_lapic_ipi_alloc(inthand_t *ipifunc)
302 {
303 
304 	XEN_APIC_UNSUPPORTED;
305 	return (-1);
306 }
307 
308 static void
309 xen_pv_lapic_ipi_free(int vector)
310 {
311 
312 	XEN_APIC_UNSUPPORTED;
313 }
314 #endif	/* SMP */
315 
316 static int
317 xen_pv_lapic_set_lvt_mask(u_int apic_id, u_int lvt, u_char masked)
318 {
319 
320 	XEN_APIC_UNSUPPORTED;
321 	return (0);
322 }
323 
324 static int
325 xen_pv_lapic_set_lvt_mode(u_int apic_id, u_int lvt, uint32_t mode)
326 {
327 
328 	XEN_APIC_UNSUPPORTED;
329 	return (0);
330 }
331 
332 static int
333 xen_pv_lapic_set_lvt_polarity(u_int apic_id, u_int lvt, enum intr_polarity pol)
334 {
335 
336 	XEN_APIC_UNSUPPORTED;
337 	return (0);
338 }
339 
340 static int
341 xen_pv_lapic_set_lvt_triggermode(u_int apic_id, u_int lvt,
342     enum intr_trigger trigger)
343 {
344 
345 	XEN_APIC_UNSUPPORTED;
346 	return (0);
347 }
348 
349 /* Xen apic_ops implementation */
350 struct apic_ops xen_apic_ops = {
351 	.create			= xen_pv_lapic_create,
352 	.init			= xen_pv_lapic_init,
353 	.xapic_mode		= xen_pv_lapic_disable,
354 	.setup			= xen_pv_lapic_setup,
355 	.dump			= xen_pv_lapic_dump,
356 	.disable		= xen_pv_lapic_disable,
357 	.eoi			= xen_pv_lapic_eoi,
358 	.id			= xen_pv_lapic_id,
359 	.intr_pending		= xen_pv_lapic_intr_pending,
360 	.set_logical_id		= xen_pv_lapic_set_logical_id,
361 	.cpuid			= xen_pv_apic_cpuid,
362 	.alloc_vector		= xen_pv_apic_alloc_vector,
363 	.alloc_vectors		= xen_pv_apic_alloc_vectors,
364 	.enable_vector		= xen_pv_apic_enable_vector,
365 	.disable_vector		= xen_pv_apic_disable_vector,
366 	.free_vector		= xen_pv_apic_free_vector,
367 	.enable_pmc		= xen_pv_lapic_enable_pmc,
368 	.disable_pmc		= xen_pv_lapic_disable_pmc,
369 	.reenable_pmc		= xen_pv_lapic_reenable_pmc,
370 	.enable_cmc		= xen_pv_lapic_enable_cmc,
371 #ifdef SMP
372 	.ipi_raw		= xen_pv_lapic_ipi_raw,
373 	.ipi_vectored		= xen_pv_lapic_ipi_vectored,
374 	.ipi_wait		= xen_pv_lapic_ipi_wait,
375 	.ipi_alloc		= xen_pv_lapic_ipi_alloc,
376 	.ipi_free		= xen_pv_lapic_ipi_free,
377 #endif
378 	.set_lvt_mask		= xen_pv_lapic_set_lvt_mask,
379 	.set_lvt_mode		= xen_pv_lapic_set_lvt_mode,
380 	.set_lvt_polarity	= xen_pv_lapic_set_lvt_polarity,
381 	.set_lvt_triggermode	= xen_pv_lapic_set_lvt_triggermode,
382 };
383 
384 #ifdef SMP
385 /*---------------------------- XEN PV IPI Handlers ---------------------------*/
386 /*
387  * These are C clones of the ASM functions found in apic_vector.
388  */
389 static int
390 xen_ipi_bitmap_handler(void *arg)
391 {
392 	struct trapframe *frame;
393 
394 	frame = arg;
395 	ipi_bitmap_handler(*frame);
396 	return (FILTER_HANDLED);
397 }
398 
399 static int
400 xen_smp_rendezvous_action(void *arg)
401 {
402 #ifdef COUNT_IPIS
403 	(*ipi_rendezvous_counts[PCPU_GET(cpuid)])++;
404 #endif /* COUNT_IPIS */
405 
406 	smp_rendezvous_action();
407 	return (FILTER_HANDLED);
408 }
409 
410 static int
411 xen_invltlb(void *arg)
412 {
413 
414 	invltlb_handler();
415 	return (FILTER_HANDLED);
416 }
417 
418 #ifdef __amd64__
419 static int
420 xen_invltlb_invpcid(void *arg)
421 {
422 
423 	invltlb_invpcid_handler();
424 	return (FILTER_HANDLED);
425 }
426 
427 static int
428 xen_invltlb_pcid(void *arg)
429 {
430 
431 	invltlb_pcid_handler();
432 	return (FILTER_HANDLED);
433 }
434 #endif
435 
436 static int
437 xen_invlpg(void *arg)
438 {
439 
440 	invlpg_handler();
441 	return (FILTER_HANDLED);
442 }
443 
444 static int
445 xen_invlrng(void *arg)
446 {
447 
448 	invlrng_handler();
449 	return (FILTER_HANDLED);
450 }
451 
452 static int
453 xen_invlcache(void *arg)
454 {
455 
456 	invlcache_handler();
457 	return (FILTER_HANDLED);
458 }
459 
460 static int
461 xen_cpustop_handler(void *arg)
462 {
463 
464 	cpustop_handler();
465 	return (FILTER_HANDLED);
466 }
467 
468 static int
469 xen_cpususpend_handler(void *arg)
470 {
471 
472 	cpususpend_handler();
473 	return (FILTER_HANDLED);
474 }
475 
476 static int
477 xen_cpustophard_handler(void *arg)
478 {
479 
480 	ipi_nmi_handler();
481 	return (FILTER_HANDLED);
482 }
483 
484 /*----------------------------- XEN PV IPI setup -----------------------------*/
485 /*
486  * Those functions are provided outside of the Xen PV APIC implementation
487  * so PVHVM guests can also use PV IPIs without having an actual Xen PV APIC,
488  * because on PVHVM there's an emulated LAPIC provided by Xen.
489  */
490 static void
491 xen_cpu_ipi_init(int cpu)
492 {
493 	xen_intr_handle_t *ipi_handle;
494 	const struct xen_ipi_handler *ipi;
495 	device_t dev;
496 	int idx, rc;
497 
498 	ipi_handle = DPCPU_ID_GET(cpu, ipi_handle);
499 	dev = pcpu_find(cpu)->pc_device;
500 	KASSERT((dev != NULL), ("NULL pcpu device_t"));
501 
502 	for (ipi = xen_ipis, idx = 0; idx < nitems(xen_ipis); ipi++, idx++) {
503 
504 		if (ipi->filter == NULL) {
505 			ipi_handle[idx] = NULL;
506 			continue;
507 		}
508 
509 		rc = xen_intr_alloc_and_bind_ipi(dev, cpu, ipi->filter,
510 		    INTR_TYPE_TTY, &ipi_handle[idx]);
511 		if (rc != 0)
512 			panic("Unable to allocate a XEN IPI port");
513 		xen_intr_describe(ipi_handle[idx], "%s", ipi->description);
514 	}
515 }
516 
517 static void
518 xen_setup_cpus(void)
519 {
520 	int i;
521 
522 	if (!xen_vector_callback_enabled)
523 		return;
524 
525 #ifdef __amd64__
526 	if (pmap_pcid_enabled) {
527 		xen_ipis[IPI_TO_IDX(IPI_INVLTLB)].filter = invpcid_works ?
528 		    xen_invltlb_invpcid : xen_invltlb_pcid;
529 	}
530 #endif
531 	CPU_FOREACH(i)
532 		xen_cpu_ipi_init(i);
533 
534 	/* Set the xen pv ipi ops to replace the native ones */
535 	if (xen_hvm_domain())
536 		apic_ops.ipi_vectored = xen_pv_lapic_ipi_vectored;
537 }
538 
539 /* We need to setup IPIs before APs are started */
540 SYSINIT(xen_setup_cpus, SI_SUB_SMP-1, SI_ORDER_FIRST, xen_setup_cpus, NULL);
541 #endif /* SMP */
542