xref: /freebsd/sys/dev/xen/control/control.c (revision f929eb1ed50f079f1638cdabe511ac2e347f541a)
1ff662b5cSJustin T. Gibbs /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
3718cf2ccSPedro F. Giffuni  *
4ff662b5cSJustin T. Gibbs  * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation
5ff662b5cSJustin T. Gibbs  * All rights reserved.
6ff662b5cSJustin T. Gibbs  *
7ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
8ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
9ff662b5cSJustin T. Gibbs  * are met:
10ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
11ff662b5cSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
12ff662b5cSJustin T. Gibbs  *    without modification.
13ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14ff662b5cSJustin T. Gibbs  *    substantially similar to the "NO WARRANTY" disclaimer below
15ff662b5cSJustin T. Gibbs  *    ("Disclaimer") and any redistribution must be conditioned upon
16ff662b5cSJustin T. Gibbs  *    including a substantially similar Disclaimer requirement for further
17ff662b5cSJustin T. Gibbs  *    binary redistribution.
18ff662b5cSJustin T. Gibbs  *
19ff662b5cSJustin T. Gibbs  * NO WARRANTY
20ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ff662b5cSJustin T. Gibbs  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ff662b5cSJustin T. Gibbs  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23ff662b5cSJustin T. Gibbs  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ff662b5cSJustin T. Gibbs  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ff662b5cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ff662b5cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ff662b5cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28ff662b5cSJustin T. Gibbs  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29ff662b5cSJustin T. Gibbs  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30ff662b5cSJustin T. Gibbs  * POSSIBILITY OF SUCH DAMAGES.
31ff662b5cSJustin T. Gibbs  */
32ff662b5cSJustin T. Gibbs 
33ff662b5cSJustin T. Gibbs /*-
34ff662b5cSJustin T. Gibbs  * PV suspend/resume support:
35ff662b5cSJustin T. Gibbs  *
36ff662b5cSJustin T. Gibbs  * Copyright (c) 2004 Christian Limpach.
37ff662b5cSJustin T. Gibbs  * Copyright (c) 2004-2006,2008 Kip Macy
38ff662b5cSJustin T. Gibbs  * All rights reserved.
39ff662b5cSJustin T. Gibbs  *
40ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
41ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
42ff662b5cSJustin T. Gibbs  * are met:
43ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
44ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
45ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
46ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
47ff662b5cSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
48ff662b5cSJustin T. Gibbs  * 3. All advertising materials mentioning features or use of this software
49ff662b5cSJustin T. Gibbs  *    must display the following acknowledgement:
50ff662b5cSJustin T. Gibbs  *      This product includes software developed by Christian Limpach.
51ff662b5cSJustin T. Gibbs  * 4. The name of the author may not be used to endorse or promote products
52ff662b5cSJustin T. Gibbs  *    derived from this software without specific prior written permission.
53ff662b5cSJustin T. Gibbs  *
54ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55ff662b5cSJustin T. Gibbs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56ff662b5cSJustin T. Gibbs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57ff662b5cSJustin T. Gibbs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58ff662b5cSJustin T. Gibbs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59ff662b5cSJustin T. Gibbs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60ff662b5cSJustin T. Gibbs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61ff662b5cSJustin T. Gibbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62ff662b5cSJustin T. Gibbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63ff662b5cSJustin T. Gibbs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64ff662b5cSJustin T. Gibbs  */
65ff662b5cSJustin T. Gibbs 
66ff662b5cSJustin T. Gibbs /*-
67ff662b5cSJustin T. Gibbs  * HVM suspend/resume support:
68ff662b5cSJustin T. Gibbs  *
69ff662b5cSJustin T. Gibbs  * Copyright (c) 2008 Citrix Systems, Inc.
70ff662b5cSJustin T. Gibbs  * All rights reserved.
71ff662b5cSJustin T. Gibbs  *
72ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
73ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
74ff662b5cSJustin T. Gibbs  * are met:
75ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
76ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
77ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
78ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
79ff662b5cSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
80ff662b5cSJustin T. Gibbs  *
81ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
82ff662b5cSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83ff662b5cSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84ff662b5cSJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
85ff662b5cSJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86ff662b5cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87ff662b5cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88ff662b5cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89ff662b5cSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90ff662b5cSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91ff662b5cSJustin T. Gibbs  * SUCH DAMAGE.
92ff662b5cSJustin T. Gibbs  */
93ff662b5cSJustin T. Gibbs #include <sys/cdefs.h>
94ff662b5cSJustin T. Gibbs __FBSDID("$FreeBSD$");
95ff662b5cSJustin T. Gibbs 
96ff662b5cSJustin T. Gibbs /**
97ff662b5cSJustin T. Gibbs  * \file control.c
98ff662b5cSJustin T. Gibbs  *
99ff662b5cSJustin T. Gibbs  * \brief Device driver to repond to control domain events that impact
100ff662b5cSJustin T. Gibbs  *        this VM.
101ff662b5cSJustin T. Gibbs  */
102ff662b5cSJustin T. Gibbs 
103ff662b5cSJustin T. Gibbs #include <sys/param.h>
104ff662b5cSJustin T. Gibbs #include <sys/systm.h>
105ff662b5cSJustin T. Gibbs #include <sys/kernel.h>
106ff662b5cSJustin T. Gibbs #include <sys/malloc.h>
107ff662b5cSJustin T. Gibbs 
108ff662b5cSJustin T. Gibbs #include <sys/bio.h>
109ff662b5cSJustin T. Gibbs #include <sys/bus.h>
110ff662b5cSJustin T. Gibbs #include <sys/conf.h>
111ff662b5cSJustin T. Gibbs #include <sys/disk.h>
112ff662b5cSJustin T. Gibbs #include <sys/fcntl.h>
113ff662b5cSJustin T. Gibbs #include <sys/filedesc.h>
114ff662b5cSJustin T. Gibbs #include <sys/kdb.h>
115ff662b5cSJustin T. Gibbs #include <sys/module.h>
116f1084587SKonstantin Belousov #include <sys/mount.h>
117ff662b5cSJustin T. Gibbs #include <sys/namei.h>
118ff662b5cSJustin T. Gibbs #include <sys/proc.h>
119ff662b5cSJustin T. Gibbs #include <sys/reboot.h>
120ff662b5cSJustin T. Gibbs #include <sys/rman.h>
1212ca7463bSJustin T. Gibbs #include <sys/sched.h>
122ff662b5cSJustin T. Gibbs #include <sys/taskqueue.h>
123ff662b5cSJustin T. Gibbs #include <sys/types.h>
124ff662b5cSJustin T. Gibbs #include <sys/vnode.h>
125ff662b5cSJustin T. Gibbs #include <sys/sched.h>
126ff662b5cSJustin T. Gibbs #include <sys/smp.h>
127428b7ca2SJustin T. Gibbs #include <sys/eventhandler.h>
1286e2a4a5fSRoger Pau Monné #include <sys/timetc.h>
129ff662b5cSJustin T. Gibbs 
130ff662b5cSJustin T. Gibbs #include <geom/geom.h>
131ff662b5cSJustin T. Gibbs 
132ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h>
13346c46eddSJulien Grall #if defined(__amd64__) || defined(__i386__)
13476acc41fSJustin T. Gibbs #include <machine/intr_machdep.h>
135ff662b5cSJustin T. Gibbs 
13615226522SRoger Pau Monné #include <x86/apicvar.h>
137d3705b5aSElliott Mitchell #endif
13815226522SRoger Pau Monné 
139ff662b5cSJustin T. Gibbs #include <vm/vm.h>
140ff662b5cSJustin T. Gibbs #include <vm/vm_extern.h>
141ff662b5cSJustin T. Gibbs #include <vm/vm_kern.h>
142ff662b5cSJustin T. Gibbs 
14376acc41fSJustin T. Gibbs #include <xen/xen-os.h>
144ff662b5cSJustin T. Gibbs #include <xen/blkif.h>
145ff662b5cSJustin T. Gibbs #include <xen/evtchn.h>
146ff662b5cSJustin T. Gibbs #include <xen/gnttab.h>
147ff662b5cSJustin T. Gibbs #include <xen/xen_intr.h>
148ff662b5cSJustin T. Gibbs 
149428b7ca2SJustin T. Gibbs #include <xen/hvm.h>
150428b7ca2SJustin T. Gibbs 
151ad7dd514SElliott Mitchell #include <contrib/xen/event_channel.h>
152ad7dd514SElliott Mitchell #include <contrib/xen/grant_table.h>
153ff662b5cSJustin T. Gibbs 
154ff662b5cSJustin T. Gibbs #include <xen/xenbus/xenbusvar.h>
155ff662b5cSJustin T. Gibbs 
1568dee0e9bSRoger Pau Monné bool xen_suspend_cancelled;
157ff662b5cSJustin T. Gibbs /*--------------------------- Forward Declarations --------------------------*/
158ff662b5cSJustin T. Gibbs /** Function signature for shutdown event handlers. */
159ff662b5cSJustin T. Gibbs typedef	void (xctrl_shutdown_handler_t)(void);
160ff662b5cSJustin T. Gibbs 
161ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_poweroff;
162ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_reboot;
163ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_suspend;
164ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_crash;
165ff662b5cSJustin T. Gibbs 
166ff662b5cSJustin T. Gibbs /*-------------------------- Private Data Structures -------------------------*/
167ff662b5cSJustin T. Gibbs /** Element type for lookup table of event name to handler. */
168ff662b5cSJustin T. Gibbs struct xctrl_shutdown_reason {
169ff662b5cSJustin T. Gibbs 	const char		 *name;
170ff662b5cSJustin T. Gibbs 	xctrl_shutdown_handler_t *handler;
171ff662b5cSJustin T. Gibbs };
172ff662b5cSJustin T. Gibbs 
173ff662b5cSJustin T. Gibbs /** Lookup table for shutdown event name to handler. */
174dcceabd3SMarius Strobl static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = {
175ff662b5cSJustin T. Gibbs 	{ "poweroff", xctrl_poweroff },
176ff662b5cSJustin T. Gibbs 	{ "reboot",   xctrl_reboot   },
177ff662b5cSJustin T. Gibbs 	{ "suspend",  xctrl_suspend  },
178ff662b5cSJustin T. Gibbs 	{ "crash",    xctrl_crash    },
1794c85fcfaSJustin T. Gibbs 	{ "halt",     xctrl_poweroff },
180ff662b5cSJustin T. Gibbs };
181ff662b5cSJustin T. Gibbs 
182ff662b5cSJustin T. Gibbs struct xctrl_softc {
183ff662b5cSJustin T. Gibbs 	struct xs_watch    xctrl_watch;
184ff662b5cSJustin T. Gibbs };
185ff662b5cSJustin T. Gibbs 
186ff662b5cSJustin T. Gibbs /*------------------------------ Event Handlers ------------------------------*/
187ff662b5cSJustin T. Gibbs static void
188ff662b5cSJustin T. Gibbs xctrl_poweroff()
189ff662b5cSJustin T. Gibbs {
190ff662b5cSJustin T. Gibbs 	shutdown_nice(RB_POWEROFF|RB_HALT);
191ff662b5cSJustin T. Gibbs }
192ff662b5cSJustin T. Gibbs 
193ff662b5cSJustin T. Gibbs static void
194ff662b5cSJustin T. Gibbs xctrl_reboot()
195ff662b5cSJustin T. Gibbs {
196ff662b5cSJustin T. Gibbs 	shutdown_nice(0);
197ff662b5cSJustin T. Gibbs }
198ff662b5cSJustin T. Gibbs 
19946c46eddSJulien Grall #if !defined(__amd64__) && !defined(__i386__)
20046c46eddSJulien Grall static void
20146c46eddSJulien Grall xctrl_suspend()
20246c46eddSJulien Grall {
20346c46eddSJulien Grall 	printf("WARNING: xen/control: Suspend not supported!\n");
20446c46eddSJulien Grall }
20546c46eddSJulien Grall #else /* __amd64__ || __i386__ */
206ff662b5cSJustin T. Gibbs static void
207ff662b5cSJustin T. Gibbs xctrl_suspend()
208ff662b5cSJustin T. Gibbs {
209428b7ca2SJustin T. Gibbs #ifdef SMP
210428b7ca2SJustin T. Gibbs 	cpuset_t cpu_suspend_map;
211428b7ca2SJustin T. Gibbs #endif
212ff662b5cSJustin T. Gibbs 
2136e2a4a5fSRoger Pau Monné 	EVENTHANDLER_INVOKE(power_suspend_early);
21441716b8dSRoger Pau Monné 	xs_lock();
2156e2a4a5fSRoger Pau Monné 	stop_all_proc();
21641716b8dSRoger Pau Monné 	xs_unlock();
217f1084587SKonstantin Belousov 	suspend_all_fs();
2182ca7463bSJustin T. Gibbs 	EVENTHANDLER_INVOKE(power_suspend);
2192ca7463bSJustin T. Gibbs 
220fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
221fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
222fdce57a0SJohn Baldwin 	thread_lock(curthread);
223fdce57a0SJohn Baldwin 	sched_bind(curthread, 0);
224fdce57a0SJohn Baldwin 	thread_unlock(curthread);
225fdce57a0SJohn Baldwin #else
226428b7ca2SJustin T. Gibbs 	if (smp_started) {
227428b7ca2SJustin T. Gibbs 		thread_lock(curthread);
228428b7ca2SJustin T. Gibbs 		sched_bind(curthread, 0);
229428b7ca2SJustin T. Gibbs 		thread_unlock(curthread);
230428b7ca2SJustin T. Gibbs 	}
231fdce57a0SJohn Baldwin #endif
232428b7ca2SJustin T. Gibbs 	KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
233428b7ca2SJustin T. Gibbs 
234428b7ca2SJustin T. Gibbs 	/*
235c6df6f53SWarner Losh 	 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME.
2362ca7463bSJustin T. Gibbs 	 */
237c6df6f53SWarner Losh 	bus_topo_lock();
238dcceabd3SMarius Strobl 	if (DEVICE_SUSPEND(root_bus) != 0) {
239c6df6f53SWarner Losh 		bus_topo_unlock();
240dcceabd3SMarius Strobl 		printf("%s: device_suspend failed\n", __func__);
241ff662b5cSJustin T. Gibbs 		return;
242ff662b5cSJustin T. Gibbs 	}
243ff662b5cSJustin T. Gibbs 
244428b7ca2SJustin T. Gibbs #ifdef SMP
245fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
246fdce57a0SJohn Baldwin 	/*
247fdce57a0SJohn Baldwin 	 * Suspend other CPUs. This prevents IPIs while we
248fdce57a0SJohn Baldwin 	 * are resuming, and will allow us to reset per-cpu
249fdce57a0SJohn Baldwin 	 * vcpu_info on resume.
250fdce57a0SJohn Baldwin 	 */
251fdce57a0SJohn Baldwin 	cpu_suspend_map = all_cpus;
252fdce57a0SJohn Baldwin 	CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
253fdce57a0SJohn Baldwin 	if (!CPU_EMPTY(&cpu_suspend_map))
254fdce57a0SJohn Baldwin 		suspend_cpus(cpu_suspend_map);
255fdce57a0SJohn Baldwin #else
256faf8ff5fSDimitry Andric 	CPU_ZERO(&cpu_suspend_map);	/* silence gcc */
257428b7ca2SJustin T. Gibbs 	if (smp_started) {
258428b7ca2SJustin T. Gibbs 		/*
259428b7ca2SJustin T. Gibbs 		 * Suspend other CPUs. This prevents IPIs while we
260428b7ca2SJustin T. Gibbs 		 * are resuming, and will allow us to reset per-cpu
261428b7ca2SJustin T. Gibbs 		 * vcpu_info on resume.
262428b7ca2SJustin T. Gibbs 		 */
263428b7ca2SJustin T. Gibbs 		cpu_suspend_map = all_cpus;
264428b7ca2SJustin T. Gibbs 		CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
265428b7ca2SJustin T. Gibbs 		if (!CPU_EMPTY(&cpu_suspend_map))
266428b7ca2SJustin T. Gibbs 			suspend_cpus(cpu_suspend_map);
267428b7ca2SJustin T. Gibbs 	}
268428b7ca2SJustin T. Gibbs #endif
269fdce57a0SJohn Baldwin #endif
270428b7ca2SJustin T. Gibbs 
271ff662b5cSJustin T. Gibbs 	/*
272ff662b5cSJustin T. Gibbs 	 * Prevent any races with evtchn_interrupt() handler.
273ff662b5cSJustin T. Gibbs 	 */
274ff662b5cSJustin T. Gibbs 	disable_intr();
27576acc41fSJustin T. Gibbs 	intr_suspend();
276428b7ca2SJustin T. Gibbs 	xen_hvm_suspend();
277ff662b5cSJustin T. Gibbs 
2788dee0e9bSRoger Pau Monné 	xen_suspend_cancelled = !!HYPERVISOR_suspend(0);
27976acc41fSJustin T. Gibbs 
2808dee0e9bSRoger Pau Monné 	if (!xen_suspend_cancelled) {
2818dee0e9bSRoger Pau Monné 		xen_hvm_resume(false);
2828dee0e9bSRoger Pau Monné 	}
2838dee0e9bSRoger Pau Monné 	intr_resume(xen_suspend_cancelled != 0);
284428b7ca2SJustin T. Gibbs 	enable_intr();
285ff662b5cSJustin T. Gibbs 
286ff662b5cSJustin T. Gibbs 	/*
287428b7ca2SJustin T. Gibbs 	 * Reset grant table info.
288ff662b5cSJustin T. Gibbs 	 */
2898dee0e9bSRoger Pau Monné 	if (!xen_suspend_cancelled) {
290a8f2f559SRoger Pau Monné 		gnttab_resume(NULL);
2918dee0e9bSRoger Pau Monné 	}
292428b7ca2SJustin T. Gibbs 
293428b7ca2SJustin T. Gibbs #ifdef SMP
294b406166fSJohn Baldwin 	if (!CPU_EMPTY(&cpu_suspend_map)) {
295428b7ca2SJustin T. Gibbs 		/*
296428b7ca2SJustin T. Gibbs 		 * Now that event channels have been initialized,
297428b7ca2SJustin T. Gibbs 		 * resume CPUs.
298428b7ca2SJustin T. Gibbs 		 */
299428b7ca2SJustin T. Gibbs 		resume_cpus(cpu_suspend_map);
300d3705b5aSElliott Mitchell #if defined(__amd64__) || defined(__i386__)
3014ea0b4adSRoger Pau Monné 		/* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
3024ea0b4adSRoger Pau Monné 		lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
303d3705b5aSElliott Mitchell #endif
304428b7ca2SJustin T. Gibbs 	}
305428b7ca2SJustin T. Gibbs #endif
306ff662b5cSJustin T. Gibbs 
307ff662b5cSJustin T. Gibbs 	/*
308ff662b5cSJustin T. Gibbs 	 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
309ff662b5cSJustin T. Gibbs 	 * similar.
310ff662b5cSJustin T. Gibbs 	 */
311ff662b5cSJustin T. Gibbs 	DEVICE_RESUME(root_bus);
312c6df6f53SWarner Losh 	bus_topo_unlock();
3132ca7463bSJustin T. Gibbs 
3146e2a4a5fSRoger Pau Monné 	/*
3156e2a4a5fSRoger Pau Monné 	 * Warm up timecounter again and reset system clock.
3166e2a4a5fSRoger Pau Monné 	 */
3176e2a4a5fSRoger Pau Monné 	timecounter->tc_get_timecount(timecounter);
3186e2a4a5fSRoger Pau Monné 	inittodr(time_second);
3196e2a4a5fSRoger Pau Monné 
320fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
321fdce57a0SJohn Baldwin 	thread_lock(curthread);
322fdce57a0SJohn Baldwin 	sched_unbind(curthread);
323fdce57a0SJohn Baldwin 	thread_unlock(curthread);
324fdce57a0SJohn Baldwin #else
325428b7ca2SJustin T. Gibbs 	if (smp_started) {
326428b7ca2SJustin T. Gibbs 		thread_lock(curthread);
327428b7ca2SJustin T. Gibbs 		sched_unbind(curthread);
328428b7ca2SJustin T. Gibbs 		thread_unlock(curthread);
329428b7ca2SJustin T. Gibbs 	}
330fdce57a0SJohn Baldwin #endif
331428b7ca2SJustin T. Gibbs 
332f1084587SKonstantin Belousov 	resume_all_fs();
3336e2a4a5fSRoger Pau Monné 	resume_all_proc();
3346e2a4a5fSRoger Pau Monné 
3352ca7463bSJustin T. Gibbs 	EVENTHANDLER_INVOKE(power_resume);
336428b7ca2SJustin T. Gibbs 
337428b7ca2SJustin T. Gibbs 	if (bootverbose)
338428b7ca2SJustin T. Gibbs 		printf("System resumed after suspension\n");
339428b7ca2SJustin T. Gibbs 
340ff662b5cSJustin T. Gibbs }
34146c46eddSJulien Grall #endif /* __amd64__ || __i386__ */
342ff662b5cSJustin T. Gibbs 
343ff662b5cSJustin T. Gibbs static void
344ff662b5cSJustin T. Gibbs xctrl_crash()
345ff662b5cSJustin T. Gibbs {
346ff662b5cSJustin T. Gibbs 	panic("Xen directed crash");
347ff662b5cSJustin T. Gibbs }
348ff662b5cSJustin T. Gibbs 
349bd851e63SRoger Pau Monné static void
350ca46f328SRoger Pau Monné shutdown_final(void *arg, int howto)
351bd851e63SRoger Pau Monné {
352ca46f328SRoger Pau Monné 	/* Inform the hypervisor that shutdown is complete. */
353ca46f328SRoger Pau Monné 	if (howto & RB_POWEROFF)
354bd851e63SRoger Pau Monné 		HYPERVISOR_shutdown(SHUTDOWN_poweroff);
355ca46f328SRoger Pau Monné 	else if (howto & RB_POWERCYCLE)
356bd851e63SRoger Pau Monné 		HYPERVISOR_shutdown(SHUTDOWN_reboot);
357bd851e63SRoger Pau Monné }
358bd851e63SRoger Pau Monné 
359ff662b5cSJustin T. Gibbs /*------------------------------ Event Reception -----------------------------*/
360ff662b5cSJustin T. Gibbs static void
361ff662b5cSJustin T. Gibbs xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
362ff662b5cSJustin T. Gibbs {
363dcceabd3SMarius Strobl 	const struct xctrl_shutdown_reason *reason;
364dcceabd3SMarius Strobl 	const struct xctrl_shutdown_reason *last_reason;
365ff662b5cSJustin T. Gibbs 	char *result;
366ff662b5cSJustin T. Gibbs 	int   error;
367ff662b5cSJustin T. Gibbs 	int   result_len;
368ff662b5cSJustin T. Gibbs 
369ff662b5cSJustin T. Gibbs 	error = xs_read(XST_NIL, "control", "shutdown",
370ff662b5cSJustin T. Gibbs 			&result_len, (void **)&result);
37106592d60SRoger Pau Monné 	if (error != 0 || result_len == 0)
372ff662b5cSJustin T. Gibbs 		return;
373ff662b5cSJustin T. Gibbs 
37406798cf5SRoger Pau Monné 	/* Acknowledge the request by writing back an empty string. */
37506798cf5SRoger Pau Monné 	error = xs_write(XST_NIL, "control", "shutdown", "");
37606798cf5SRoger Pau Monné 	if (error != 0)
37706798cf5SRoger Pau Monné 		printf("unable to ack shutdown request, proceeding anyway\n");
37806798cf5SRoger Pau Monné 
379ff662b5cSJustin T. Gibbs 	reason = xctrl_shutdown_reasons;
380dcceabd3SMarius Strobl 	last_reason = reason + nitems(xctrl_shutdown_reasons);
381ff662b5cSJustin T. Gibbs 	while (reason < last_reason) {
382ff662b5cSJustin T. Gibbs 		if (!strcmp(result, reason->name)) {
383ff662b5cSJustin T. Gibbs 			reason->handler();
384ff662b5cSJustin T. Gibbs 			break;
385ff662b5cSJustin T. Gibbs 		}
386ff662b5cSJustin T. Gibbs 		reason++;
387ff662b5cSJustin T. Gibbs 	}
388ff662b5cSJustin T. Gibbs 
389ff662b5cSJustin T. Gibbs 	free(result, M_XENSTORE);
390ff662b5cSJustin T. Gibbs }
391ff662b5cSJustin T. Gibbs 
392ff662b5cSJustin T. Gibbs /*------------------ Private Device Attachment Functions  --------------------*/
393ff662b5cSJustin T. Gibbs /**
394ff662b5cSJustin T. Gibbs  * \brief Identify instances of this device type in the system.
395ff662b5cSJustin T. Gibbs  *
396ff662b5cSJustin T. Gibbs  * \param driver  The driver performing this identify action.
397ff662b5cSJustin T. Gibbs  * \param parent  The NewBus parent device for any devices this method adds.
398ff662b5cSJustin T. Gibbs  */
399ff662b5cSJustin T. Gibbs static void
400ff662b5cSJustin T. Gibbs xctrl_identify(driver_t *driver __unused, device_t parent)
401ff662b5cSJustin T. Gibbs {
402ff662b5cSJustin T. Gibbs 	/*
403ff662b5cSJustin T. Gibbs 	 * A single device instance for our driver is always present
404ff662b5cSJustin T. Gibbs 	 * in a system operating under Xen.
405ff662b5cSJustin T. Gibbs 	 */
406ff662b5cSJustin T. Gibbs 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
407ff662b5cSJustin T. Gibbs }
408ff662b5cSJustin T. Gibbs 
409ff662b5cSJustin T. Gibbs /**
410453130d9SPedro F. Giffuni  * \brief Probe for the existence of the Xen Control device
411ff662b5cSJustin T. Gibbs  *
412ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control instance.
413ff662b5cSJustin T. Gibbs  *
414ff662b5cSJustin T. Gibbs  * \return  Always returns 0 indicating success.
415ff662b5cSJustin T. Gibbs  */
416ff662b5cSJustin T. Gibbs static int
417ff662b5cSJustin T. Gibbs xctrl_probe(device_t dev)
418ff662b5cSJustin T. Gibbs {
419ff662b5cSJustin T. Gibbs 	device_set_desc(dev, "Xen Control Device");
420ff662b5cSJustin T. Gibbs 
4212f9ec994SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
422ff662b5cSJustin T. Gibbs }
423ff662b5cSJustin T. Gibbs 
424ff662b5cSJustin T. Gibbs /**
425ff662b5cSJustin T. Gibbs  * \brief Attach the Xen control device.
426ff662b5cSJustin T. Gibbs  *
427ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control instance.
428ff662b5cSJustin T. Gibbs  *
429ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
430ff662b5cSJustin T. Gibbs  *          type of failure.
431ff662b5cSJustin T. Gibbs  */
432ff662b5cSJustin T. Gibbs static int
433ff662b5cSJustin T. Gibbs xctrl_attach(device_t dev)
434ff662b5cSJustin T. Gibbs {
435ff662b5cSJustin T. Gibbs 	struct xctrl_softc *xctrl;
436ff662b5cSJustin T. Gibbs 
437ff662b5cSJustin T. Gibbs 	xctrl = device_get_softc(dev);
438ff662b5cSJustin T. Gibbs 
439ff662b5cSJustin T. Gibbs 	/* Activate watch */
440ff662b5cSJustin T. Gibbs 	xctrl->xctrl_watch.node = "control/shutdown";
441ff662b5cSJustin T. Gibbs 	xctrl->xctrl_watch.callback = xctrl_on_watch_event;
442283d6f72SJustin T. Gibbs 	xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
4434e4e43dcSRoger Pau Monné 	/*
4444e4e43dcSRoger Pau Monné 	 * We don't care about the path updated, just about the value changes
4454e4e43dcSRoger Pau Monné 	 * on that single node, hence there's no need to queue more that one
4464e4e43dcSRoger Pau Monné 	 * event.
4474e4e43dcSRoger Pau Monné 	 */
4484e4e43dcSRoger Pau Monné 	xctrl->xctrl_watch.max_pending = 1;
449ff662b5cSJustin T. Gibbs 	xs_register_watch(&xctrl->xctrl_watch);
450ff662b5cSJustin T. Gibbs 
451ca46f328SRoger Pau Monné 	EVENTHANDLER_REGISTER(shutdown_final, shutdown_final, NULL,
452ff662b5cSJustin T. Gibbs 	    SHUTDOWN_PRI_LAST);
453ff662b5cSJustin T. Gibbs 
454ff662b5cSJustin T. Gibbs 	return (0);
455ff662b5cSJustin T. Gibbs }
456ff662b5cSJustin T. Gibbs 
457ff662b5cSJustin T. Gibbs /**
458ff662b5cSJustin T. Gibbs  * \brief Detach the Xen control device.
459ff662b5cSJustin T. Gibbs  *
460ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control device instance.
461ff662b5cSJustin T. Gibbs  *
462ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
463ff662b5cSJustin T. Gibbs  *          type of failure.
464ff662b5cSJustin T. Gibbs  */
465ff662b5cSJustin T. Gibbs static int
466ff662b5cSJustin T. Gibbs xctrl_detach(device_t dev)
467ff662b5cSJustin T. Gibbs {
468ff662b5cSJustin T. Gibbs 	struct xctrl_softc *xctrl;
469ff662b5cSJustin T. Gibbs 
470ff662b5cSJustin T. Gibbs 	xctrl = device_get_softc(dev);
471ff662b5cSJustin T. Gibbs 
472ff662b5cSJustin T. Gibbs 	/* Release watch */
473ff662b5cSJustin T. Gibbs 	xs_unregister_watch(&xctrl->xctrl_watch);
474ff662b5cSJustin T. Gibbs 
475ff662b5cSJustin T. Gibbs 	return (0);
476ff662b5cSJustin T. Gibbs }
477ff662b5cSJustin T. Gibbs 
478ff662b5cSJustin T. Gibbs /*-------------------- Private Device Attachment Data  -----------------------*/
479ff662b5cSJustin T. Gibbs static device_method_t xctrl_methods[] = {
480ff662b5cSJustin T. Gibbs 	/* Device interface */
481ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_identify,	xctrl_identify),
482ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_probe,         xctrl_probe),
483ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_attach,        xctrl_attach),
484ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_detach,        xctrl_detach),
485ff662b5cSJustin T. Gibbs 
486dcceabd3SMarius Strobl 	DEVMETHOD_END
487ff662b5cSJustin T. Gibbs };
488ff662b5cSJustin T. Gibbs 
489ff662b5cSJustin T. Gibbs DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
490ff662b5cSJustin T. Gibbs 
491*f929eb1eSJohn Baldwin DRIVER_MODULE(xctrl, xenstore, xctrl_driver, NULL, NULL);
492