xref: /freebsd/sys/dev/xen/control/control.c (revision 41716b8d5151d2bec72b6796ed58b6206cdb4779)
1ff662b5cSJustin T. Gibbs /*-
2ff662b5cSJustin T. Gibbs  * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation
3ff662b5cSJustin T. Gibbs  * All rights reserved.
4ff662b5cSJustin T. Gibbs  *
5ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
6ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
7ff662b5cSJustin T. Gibbs  * are met:
8ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
9ff662b5cSJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
10ff662b5cSJustin T. Gibbs  *    without modification.
11ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12ff662b5cSJustin T. Gibbs  *    substantially similar to the "NO WARRANTY" disclaimer below
13ff662b5cSJustin T. Gibbs  *    ("Disclaimer") and any redistribution must be conditioned upon
14ff662b5cSJustin T. Gibbs  *    including a substantially similar Disclaimer requirement for further
15ff662b5cSJustin T. Gibbs  *    binary redistribution.
16ff662b5cSJustin T. Gibbs  *
17ff662b5cSJustin T. Gibbs  * NO WARRANTY
18ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19ff662b5cSJustin T. Gibbs  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20ff662b5cSJustin T. Gibbs  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21ff662b5cSJustin T. Gibbs  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22ff662b5cSJustin T. Gibbs  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ff662b5cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ff662b5cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ff662b5cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26ff662b5cSJustin T. Gibbs  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27ff662b5cSJustin T. Gibbs  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28ff662b5cSJustin T. Gibbs  * POSSIBILITY OF SUCH DAMAGES.
29ff662b5cSJustin T. Gibbs  */
30ff662b5cSJustin T. Gibbs 
31ff662b5cSJustin T. Gibbs /*-
32ff662b5cSJustin T. Gibbs  * PV suspend/resume support:
33ff662b5cSJustin T. Gibbs  *
34ff662b5cSJustin T. Gibbs  * Copyright (c) 2004 Christian Limpach.
35ff662b5cSJustin T. Gibbs  * Copyright (c) 2004-2006,2008 Kip Macy
36ff662b5cSJustin T. Gibbs  * All rights reserved.
37ff662b5cSJustin T. Gibbs  *
38ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
39ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
40ff662b5cSJustin T. Gibbs  * are met:
41ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
42ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
43ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
44ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
45ff662b5cSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
46ff662b5cSJustin T. Gibbs  * 3. All advertising materials mentioning features or use of this software
47ff662b5cSJustin T. Gibbs  *    must display the following acknowledgement:
48ff662b5cSJustin T. Gibbs  *      This product includes software developed by Christian Limpach.
49ff662b5cSJustin T. Gibbs  * 4. The name of the author may not be used to endorse or promote products
50ff662b5cSJustin T. Gibbs  *    derived from this software without specific prior written permission.
51ff662b5cSJustin T. Gibbs  *
52ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53ff662b5cSJustin T. Gibbs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54ff662b5cSJustin T. Gibbs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55ff662b5cSJustin T. Gibbs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56ff662b5cSJustin T. Gibbs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57ff662b5cSJustin T. Gibbs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58ff662b5cSJustin T. Gibbs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59ff662b5cSJustin T. Gibbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60ff662b5cSJustin T. Gibbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61ff662b5cSJustin T. Gibbs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62ff662b5cSJustin T. Gibbs  */
63ff662b5cSJustin T. Gibbs 
64ff662b5cSJustin T. Gibbs /*-
65ff662b5cSJustin T. Gibbs  * HVM suspend/resume support:
66ff662b5cSJustin T. Gibbs  *
67ff662b5cSJustin T. Gibbs  * Copyright (c) 2008 Citrix Systems, Inc.
68ff662b5cSJustin T. Gibbs  * All rights reserved.
69ff662b5cSJustin T. Gibbs  *
70ff662b5cSJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
71ff662b5cSJustin T. Gibbs  * modification, are permitted provided that the following conditions
72ff662b5cSJustin T. Gibbs  * are met:
73ff662b5cSJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
74ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
75ff662b5cSJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
76ff662b5cSJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
77ff662b5cSJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
78ff662b5cSJustin T. Gibbs  *
79ff662b5cSJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
80ff662b5cSJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81ff662b5cSJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82ff662b5cSJustin T. Gibbs  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
83ff662b5cSJustin T. Gibbs  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84ff662b5cSJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85ff662b5cSJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86ff662b5cSJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87ff662b5cSJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88ff662b5cSJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ff662b5cSJustin T. Gibbs  * SUCH DAMAGE.
90ff662b5cSJustin T. Gibbs  */
91ff662b5cSJustin T. Gibbs #include <sys/cdefs.h>
92ff662b5cSJustin T. Gibbs __FBSDID("$FreeBSD$");
93ff662b5cSJustin T. Gibbs 
94ff662b5cSJustin T. Gibbs /**
95ff662b5cSJustin T. Gibbs  * \file control.c
96ff662b5cSJustin T. Gibbs  *
97ff662b5cSJustin T. Gibbs  * \brief Device driver to repond to control domain events that impact
98ff662b5cSJustin T. Gibbs  *        this VM.
99ff662b5cSJustin T. Gibbs  */
100ff662b5cSJustin T. Gibbs 
101ff662b5cSJustin T. Gibbs #include <sys/param.h>
102ff662b5cSJustin T. Gibbs #include <sys/systm.h>
103ff662b5cSJustin T. Gibbs #include <sys/kernel.h>
104ff662b5cSJustin T. Gibbs #include <sys/malloc.h>
105ff662b5cSJustin T. Gibbs 
106ff662b5cSJustin T. Gibbs #include <sys/bio.h>
107ff662b5cSJustin T. Gibbs #include <sys/bus.h>
108ff662b5cSJustin T. Gibbs #include <sys/conf.h>
109ff662b5cSJustin T. Gibbs #include <sys/disk.h>
110ff662b5cSJustin T. Gibbs #include <sys/fcntl.h>
111ff662b5cSJustin T. Gibbs #include <sys/filedesc.h>
112ff662b5cSJustin T. Gibbs #include <sys/kdb.h>
113ff662b5cSJustin T. Gibbs #include <sys/module.h>
114ff662b5cSJustin T. Gibbs #include <sys/namei.h>
115ff662b5cSJustin T. Gibbs #include <sys/proc.h>
116ff662b5cSJustin T. Gibbs #include <sys/reboot.h>
117ff662b5cSJustin T. Gibbs #include <sys/rman.h>
1182ca7463bSJustin T. Gibbs #include <sys/sched.h>
119ff662b5cSJustin T. Gibbs #include <sys/taskqueue.h>
120ff662b5cSJustin T. Gibbs #include <sys/types.h>
121ff662b5cSJustin T. Gibbs #include <sys/vnode.h>
122ff662b5cSJustin T. Gibbs #include <sys/sched.h>
123ff662b5cSJustin T. Gibbs #include <sys/smp.h>
124428b7ca2SJustin T. Gibbs #include <sys/eventhandler.h>
1256e2a4a5fSRoger Pau Monné #include <sys/timetc.h>
126ff662b5cSJustin T. Gibbs 
127ff662b5cSJustin T. Gibbs #include <geom/geom.h>
128ff662b5cSJustin T. Gibbs 
129ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h>
13076acc41fSJustin T. Gibbs #include <machine/intr_machdep.h>
131ff662b5cSJustin T. Gibbs 
13215226522SRoger Pau Monné #include <x86/apicvar.h>
13315226522SRoger Pau Monné 
134ff662b5cSJustin T. Gibbs #include <vm/vm.h>
135ff662b5cSJustin T. Gibbs #include <vm/vm_extern.h>
136ff662b5cSJustin T. Gibbs #include <vm/vm_kern.h>
137ff662b5cSJustin T. Gibbs 
13876acc41fSJustin T. Gibbs #include <xen/xen-os.h>
139ff662b5cSJustin T. Gibbs #include <xen/blkif.h>
140ff662b5cSJustin T. Gibbs #include <xen/evtchn.h>
141ff662b5cSJustin T. Gibbs #include <xen/gnttab.h>
142ff662b5cSJustin T. Gibbs #include <xen/xen_intr.h>
143ff662b5cSJustin T. Gibbs 
144428b7ca2SJustin T. Gibbs #include <xen/hvm.h>
145428b7ca2SJustin T. Gibbs 
146ff662b5cSJustin T. Gibbs #include <xen/interface/event_channel.h>
147ff662b5cSJustin T. Gibbs #include <xen/interface/grant_table.h>
148ff662b5cSJustin T. Gibbs 
149ff662b5cSJustin T. Gibbs #include <xen/xenbus/xenbusvar.h>
150ff662b5cSJustin T. Gibbs 
1518dee0e9bSRoger Pau Monné bool xen_suspend_cancelled;
152ff662b5cSJustin T. Gibbs /*--------------------------- Forward Declarations --------------------------*/
153ff662b5cSJustin T. Gibbs /** Function signature for shutdown event handlers. */
154ff662b5cSJustin T. Gibbs typedef	void (xctrl_shutdown_handler_t)(void);
155ff662b5cSJustin T. Gibbs 
156ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_poweroff;
157ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_reboot;
158ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_suspend;
159ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_crash;
160ff662b5cSJustin T. Gibbs 
161ff662b5cSJustin T. Gibbs /*-------------------------- Private Data Structures -------------------------*/
162ff662b5cSJustin T. Gibbs /** Element type for lookup table of event name to handler. */
163ff662b5cSJustin T. Gibbs struct xctrl_shutdown_reason {
164ff662b5cSJustin T. Gibbs 	const char		 *name;
165ff662b5cSJustin T. Gibbs 	xctrl_shutdown_handler_t *handler;
166ff662b5cSJustin T. Gibbs };
167ff662b5cSJustin T. Gibbs 
168ff662b5cSJustin T. Gibbs /** Lookup table for shutdown event name to handler. */
169dcceabd3SMarius Strobl static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = {
170ff662b5cSJustin T. Gibbs 	{ "poweroff", xctrl_poweroff },
171ff662b5cSJustin T. Gibbs 	{ "reboot",   xctrl_reboot   },
172ff662b5cSJustin T. Gibbs 	{ "suspend",  xctrl_suspend  },
173ff662b5cSJustin T. Gibbs 	{ "crash",    xctrl_crash    },
1744c85fcfaSJustin T. Gibbs 	{ "halt",     xctrl_poweroff },
175ff662b5cSJustin T. Gibbs };
176ff662b5cSJustin T. Gibbs 
177ff662b5cSJustin T. Gibbs struct xctrl_softc {
178ff662b5cSJustin T. Gibbs 	struct xs_watch    xctrl_watch;
179ff662b5cSJustin T. Gibbs };
180ff662b5cSJustin T. Gibbs 
181ff662b5cSJustin T. Gibbs /*------------------------------ Event Handlers ------------------------------*/
182ff662b5cSJustin T. Gibbs static void
183ff662b5cSJustin T. Gibbs xctrl_poweroff()
184ff662b5cSJustin T. Gibbs {
185ff662b5cSJustin T. Gibbs 	shutdown_nice(RB_POWEROFF|RB_HALT);
186ff662b5cSJustin T. Gibbs }
187ff662b5cSJustin T. Gibbs 
188ff662b5cSJustin T. Gibbs static void
189ff662b5cSJustin T. Gibbs xctrl_reboot()
190ff662b5cSJustin T. Gibbs {
191ff662b5cSJustin T. Gibbs 	shutdown_nice(0);
192ff662b5cSJustin T. Gibbs }
193ff662b5cSJustin T. Gibbs 
194ff662b5cSJustin T. Gibbs static void
195ff662b5cSJustin T. Gibbs xctrl_suspend()
196ff662b5cSJustin T. Gibbs {
197428b7ca2SJustin T. Gibbs #ifdef SMP
198428b7ca2SJustin T. Gibbs 	cpuset_t cpu_suspend_map;
199428b7ca2SJustin T. Gibbs #endif
200ff662b5cSJustin T. Gibbs 
2016e2a4a5fSRoger Pau Monné 	EVENTHANDLER_INVOKE(power_suspend_early);
202*41716b8dSRoger Pau Monné 	xs_lock();
2036e2a4a5fSRoger Pau Monné 	stop_all_proc();
204*41716b8dSRoger Pau Monné 	xs_unlock();
2052ca7463bSJustin T. Gibbs 	EVENTHANDLER_INVOKE(power_suspend);
2062ca7463bSJustin T. Gibbs 
207fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
208fdce57a0SJohn Baldwin 	MPASS(mp_ncpus == 1 || smp_started);
209fdce57a0SJohn Baldwin 	thread_lock(curthread);
210fdce57a0SJohn Baldwin 	sched_bind(curthread, 0);
211fdce57a0SJohn Baldwin 	thread_unlock(curthread);
212fdce57a0SJohn Baldwin #else
213428b7ca2SJustin T. Gibbs 	if (smp_started) {
214428b7ca2SJustin T. Gibbs 		thread_lock(curthread);
215428b7ca2SJustin T. Gibbs 		sched_bind(curthread, 0);
216428b7ca2SJustin T. Gibbs 		thread_unlock(curthread);
217428b7ca2SJustin T. Gibbs 	}
218fdce57a0SJohn Baldwin #endif
219428b7ca2SJustin T. Gibbs 	KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
220428b7ca2SJustin T. Gibbs 
221428b7ca2SJustin T. Gibbs 	/*
222428b7ca2SJustin T. Gibbs 	 * Clear our XenStore node so the toolstack knows we are
223428b7ca2SJustin T. Gibbs 	 * responding to the suspend request.
224428b7ca2SJustin T. Gibbs 	 */
225428b7ca2SJustin T. Gibbs 	xs_write(XST_NIL, "control", "shutdown", "");
226428b7ca2SJustin T. Gibbs 
2272ca7463bSJustin T. Gibbs 	/*
2282ca7463bSJustin T. Gibbs 	 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
2292ca7463bSJustin T. Gibbs 	 * drivers need this.
2302ca7463bSJustin T. Gibbs 	 */
2312ca7463bSJustin T. Gibbs 	mtx_lock(&Giant);
232dcceabd3SMarius Strobl 	if (DEVICE_SUSPEND(root_bus) != 0) {
2332ca7463bSJustin T. Gibbs 		mtx_unlock(&Giant);
234dcceabd3SMarius Strobl 		printf("%s: device_suspend failed\n", __func__);
235ff662b5cSJustin T. Gibbs 		return;
236ff662b5cSJustin T. Gibbs 	}
237ff662b5cSJustin T. Gibbs 
238428b7ca2SJustin T. Gibbs #ifdef SMP
239fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
240fdce57a0SJohn Baldwin 	/*
241fdce57a0SJohn Baldwin 	 * Suspend other CPUs. This prevents IPIs while we
242fdce57a0SJohn Baldwin 	 * are resuming, and will allow us to reset per-cpu
243fdce57a0SJohn Baldwin 	 * vcpu_info on resume.
244fdce57a0SJohn Baldwin 	 */
245fdce57a0SJohn Baldwin 	cpu_suspend_map = all_cpus;
246fdce57a0SJohn Baldwin 	CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
247fdce57a0SJohn Baldwin 	if (!CPU_EMPTY(&cpu_suspend_map))
248fdce57a0SJohn Baldwin 		suspend_cpus(cpu_suspend_map);
249fdce57a0SJohn Baldwin #else
250faf8ff5fSDimitry Andric 	CPU_ZERO(&cpu_suspend_map);	/* silence gcc */
251428b7ca2SJustin T. Gibbs 	if (smp_started) {
252428b7ca2SJustin T. Gibbs 		/*
253428b7ca2SJustin T. Gibbs 		 * Suspend other CPUs. This prevents IPIs while we
254428b7ca2SJustin T. Gibbs 		 * are resuming, and will allow us to reset per-cpu
255428b7ca2SJustin T. Gibbs 		 * vcpu_info on resume.
256428b7ca2SJustin T. Gibbs 		 */
257428b7ca2SJustin T. Gibbs 		cpu_suspend_map = all_cpus;
258428b7ca2SJustin T. Gibbs 		CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
259428b7ca2SJustin T. Gibbs 		if (!CPU_EMPTY(&cpu_suspend_map))
260428b7ca2SJustin T. Gibbs 			suspend_cpus(cpu_suspend_map);
261428b7ca2SJustin T. Gibbs 	}
262428b7ca2SJustin T. Gibbs #endif
263fdce57a0SJohn Baldwin #endif
264428b7ca2SJustin T. Gibbs 
265ff662b5cSJustin T. Gibbs 	/*
266ff662b5cSJustin T. Gibbs 	 * Prevent any races with evtchn_interrupt() handler.
267ff662b5cSJustin T. Gibbs 	 */
268ff662b5cSJustin T. Gibbs 	disable_intr();
26976acc41fSJustin T. Gibbs 	intr_suspend();
270428b7ca2SJustin T. Gibbs 	xen_hvm_suspend();
271ff662b5cSJustin T. Gibbs 
2728dee0e9bSRoger Pau Monné 	xen_suspend_cancelled = !!HYPERVISOR_suspend(0);
27376acc41fSJustin T. Gibbs 
2748dee0e9bSRoger Pau Monné 	if (!xen_suspend_cancelled) {
2758dee0e9bSRoger Pau Monné 		xen_hvm_resume(false);
2768dee0e9bSRoger Pau Monné 	}
2778dee0e9bSRoger Pau Monné 	intr_resume(xen_suspend_cancelled != 0);
278428b7ca2SJustin T. Gibbs 	enable_intr();
279ff662b5cSJustin T. Gibbs 
280ff662b5cSJustin T. Gibbs 	/*
281428b7ca2SJustin T. Gibbs 	 * Reset grant table info.
282ff662b5cSJustin T. Gibbs 	 */
2838dee0e9bSRoger Pau Monné 	if (!xen_suspend_cancelled) {
284a8f2f559SRoger Pau Monné 		gnttab_resume(NULL);
2858dee0e9bSRoger Pau Monné 	}
286428b7ca2SJustin T. Gibbs 
287428b7ca2SJustin T. Gibbs #ifdef SMP
288b406166fSJohn Baldwin 	if (!CPU_EMPTY(&cpu_suspend_map)) {
289428b7ca2SJustin T. Gibbs 		/*
290428b7ca2SJustin T. Gibbs 		 * Now that event channels have been initialized,
291428b7ca2SJustin T. Gibbs 		 * resume CPUs.
292428b7ca2SJustin T. Gibbs 		 */
293428b7ca2SJustin T. Gibbs 		resume_cpus(cpu_suspend_map);
2944ea0b4adSRoger Pau Monné 		/* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
2954ea0b4adSRoger Pau Monné 		lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
296428b7ca2SJustin T. Gibbs 	}
297428b7ca2SJustin T. Gibbs #endif
298ff662b5cSJustin T. Gibbs 
299ff662b5cSJustin T. Gibbs 	/*
300ff662b5cSJustin T. Gibbs 	 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
301ff662b5cSJustin T. Gibbs 	 * similar.
302ff662b5cSJustin T. Gibbs 	 */
303ff662b5cSJustin T. Gibbs 	DEVICE_RESUME(root_bus);
3042ca7463bSJustin T. Gibbs 	mtx_unlock(&Giant);
3052ca7463bSJustin T. Gibbs 
3066e2a4a5fSRoger Pau Monné 	/*
3076e2a4a5fSRoger Pau Monné 	 * Warm up timecounter again and reset system clock.
3086e2a4a5fSRoger Pau Monné 	 */
3096e2a4a5fSRoger Pau Monné 	timecounter->tc_get_timecount(timecounter);
3106e2a4a5fSRoger Pau Monné 	timecounter->tc_get_timecount(timecounter);
3116e2a4a5fSRoger Pau Monné 	inittodr(time_second);
3126e2a4a5fSRoger Pau Monné 
313fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
314fdce57a0SJohn Baldwin 	thread_lock(curthread);
315fdce57a0SJohn Baldwin 	sched_unbind(curthread);
316fdce57a0SJohn Baldwin 	thread_unlock(curthread);
317fdce57a0SJohn Baldwin #else
318428b7ca2SJustin T. Gibbs 	if (smp_started) {
319428b7ca2SJustin T. Gibbs 		thread_lock(curthread);
320428b7ca2SJustin T. Gibbs 		sched_unbind(curthread);
321428b7ca2SJustin T. Gibbs 		thread_unlock(curthread);
322428b7ca2SJustin T. Gibbs 	}
323fdce57a0SJohn Baldwin #endif
324428b7ca2SJustin T. Gibbs 
3256e2a4a5fSRoger Pau Monné 	resume_all_proc();
3266e2a4a5fSRoger Pau Monné 
3272ca7463bSJustin T. Gibbs 	EVENTHANDLER_INVOKE(power_resume);
328428b7ca2SJustin T. Gibbs 
329428b7ca2SJustin T. Gibbs 	if (bootverbose)
330428b7ca2SJustin T. Gibbs 		printf("System resumed after suspension\n");
331428b7ca2SJustin T. Gibbs 
332ff662b5cSJustin T. Gibbs }
333ff662b5cSJustin T. Gibbs 
334ff662b5cSJustin T. Gibbs static void
335ff662b5cSJustin T. Gibbs xctrl_crash()
336ff662b5cSJustin T. Gibbs {
337ff662b5cSJustin T. Gibbs 	panic("Xen directed crash");
338ff662b5cSJustin T. Gibbs }
339ff662b5cSJustin T. Gibbs 
340bd851e63SRoger Pau Monné static void
341bd851e63SRoger Pau Monné xen_pv_shutdown_final(void *arg, int howto)
342bd851e63SRoger Pau Monné {
343bd851e63SRoger Pau Monné 	/*
344bd851e63SRoger Pau Monné 	 * Inform the hypervisor that shutdown is complete.
345bd851e63SRoger Pau Monné 	 * This is not necessary in HVM domains since Xen
346bd851e63SRoger Pau Monné 	 * emulates ACPI in that mode and FreeBSD's ACPI
347bd851e63SRoger Pau Monné 	 * support will request this transition.
348bd851e63SRoger Pau Monné 	 */
349bd851e63SRoger Pau Monné 	if (howto & (RB_HALT | RB_POWEROFF))
350bd851e63SRoger Pau Monné 		HYPERVISOR_shutdown(SHUTDOWN_poweroff);
351bd851e63SRoger Pau Monné 	else
352bd851e63SRoger Pau Monné 		HYPERVISOR_shutdown(SHUTDOWN_reboot);
353bd851e63SRoger Pau Monné }
354bd851e63SRoger Pau Monné 
355ff662b5cSJustin T. Gibbs /*------------------------------ Event Reception -----------------------------*/
356ff662b5cSJustin T. Gibbs static void
357ff662b5cSJustin T. Gibbs xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
358ff662b5cSJustin T. Gibbs {
359dcceabd3SMarius Strobl 	const struct xctrl_shutdown_reason *reason;
360dcceabd3SMarius Strobl 	const struct xctrl_shutdown_reason *last_reason;
361ff662b5cSJustin T. Gibbs 	char *result;
362ff662b5cSJustin T. Gibbs 	int   error;
363ff662b5cSJustin T. Gibbs 	int   result_len;
364ff662b5cSJustin T. Gibbs 
365ff662b5cSJustin T. Gibbs 	error = xs_read(XST_NIL, "control", "shutdown",
366ff662b5cSJustin T. Gibbs 			&result_len, (void **)&result);
367ff662b5cSJustin T. Gibbs 	if (error != 0)
368ff662b5cSJustin T. Gibbs 		return;
369ff662b5cSJustin T. Gibbs 
370ff662b5cSJustin T. Gibbs 	reason = xctrl_shutdown_reasons;
371dcceabd3SMarius Strobl 	last_reason = reason + nitems(xctrl_shutdown_reasons);
372ff662b5cSJustin T. Gibbs 	while (reason < last_reason) {
373ff662b5cSJustin T. Gibbs 
374ff662b5cSJustin T. Gibbs 		if (!strcmp(result, reason->name)) {
375ff662b5cSJustin T. Gibbs 			reason->handler();
376ff662b5cSJustin T. Gibbs 			break;
377ff662b5cSJustin T. Gibbs 		}
378ff662b5cSJustin T. Gibbs 		reason++;
379ff662b5cSJustin T. Gibbs 	}
380ff662b5cSJustin T. Gibbs 
381ff662b5cSJustin T. Gibbs 	free(result, M_XENSTORE);
382ff662b5cSJustin T. Gibbs }
383ff662b5cSJustin T. Gibbs 
384ff662b5cSJustin T. Gibbs /*------------------ Private Device Attachment Functions  --------------------*/
385ff662b5cSJustin T. Gibbs /**
386ff662b5cSJustin T. Gibbs  * \brief Identify instances of this device type in the system.
387ff662b5cSJustin T. Gibbs  *
388ff662b5cSJustin T. Gibbs  * \param driver  The driver performing this identify action.
389ff662b5cSJustin T. Gibbs  * \param parent  The NewBus parent device for any devices this method adds.
390ff662b5cSJustin T. Gibbs  */
391ff662b5cSJustin T. Gibbs static void
392ff662b5cSJustin T. Gibbs xctrl_identify(driver_t *driver __unused, device_t parent)
393ff662b5cSJustin T. Gibbs {
394ff662b5cSJustin T. Gibbs 	/*
395ff662b5cSJustin T. Gibbs 	 * A single device instance for our driver is always present
396ff662b5cSJustin T. Gibbs 	 * in a system operating under Xen.
397ff662b5cSJustin T. Gibbs 	 */
398ff662b5cSJustin T. Gibbs 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
399ff662b5cSJustin T. Gibbs }
400ff662b5cSJustin T. Gibbs 
401ff662b5cSJustin T. Gibbs /**
402453130d9SPedro F. Giffuni  * \brief Probe for the existence of the Xen Control device
403ff662b5cSJustin T. Gibbs  *
404ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control instance.
405ff662b5cSJustin T. Gibbs  *
406ff662b5cSJustin T. Gibbs  * \return  Always returns 0 indicating success.
407ff662b5cSJustin T. Gibbs  */
408ff662b5cSJustin T. Gibbs static int
409ff662b5cSJustin T. Gibbs xctrl_probe(device_t dev)
410ff662b5cSJustin T. Gibbs {
411ff662b5cSJustin T. Gibbs 	device_set_desc(dev, "Xen Control Device");
412ff662b5cSJustin T. Gibbs 
4132f9ec994SRoger Pau Monné 	return (BUS_PROBE_NOWILDCARD);
414ff662b5cSJustin T. Gibbs }
415ff662b5cSJustin T. Gibbs 
416ff662b5cSJustin T. Gibbs /**
417ff662b5cSJustin T. Gibbs  * \brief Attach the Xen control device.
418ff662b5cSJustin T. Gibbs  *
419ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control instance.
420ff662b5cSJustin T. Gibbs  *
421ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
422ff662b5cSJustin T. Gibbs  *          type of failure.
423ff662b5cSJustin T. Gibbs  */
424ff662b5cSJustin T. Gibbs static int
425ff662b5cSJustin T. Gibbs xctrl_attach(device_t dev)
426ff662b5cSJustin T. Gibbs {
427ff662b5cSJustin T. Gibbs 	struct xctrl_softc *xctrl;
428ff662b5cSJustin T. Gibbs 
429ff662b5cSJustin T. Gibbs 	xctrl = device_get_softc(dev);
430ff662b5cSJustin T. Gibbs 
431ff662b5cSJustin T. Gibbs 	/* Activate watch */
432ff662b5cSJustin T. Gibbs 	xctrl->xctrl_watch.node = "control/shutdown";
433ff662b5cSJustin T. Gibbs 	xctrl->xctrl_watch.callback = xctrl_on_watch_event;
434283d6f72SJustin T. Gibbs 	xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
435ff662b5cSJustin T. Gibbs 	xs_register_watch(&xctrl->xctrl_watch);
436ff662b5cSJustin T. Gibbs 
437bd851e63SRoger Pau Monné 	if (xen_pv_domain())
438ff662b5cSJustin T. Gibbs 		EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
439ff662b5cSJustin T. Gibbs 		                      SHUTDOWN_PRI_LAST);
440ff662b5cSJustin T. Gibbs 
441ff662b5cSJustin T. Gibbs 	return (0);
442ff662b5cSJustin T. Gibbs }
443ff662b5cSJustin T. Gibbs 
444ff662b5cSJustin T. Gibbs /**
445ff662b5cSJustin T. Gibbs  * \brief Detach the Xen control device.
446ff662b5cSJustin T. Gibbs  *
447ff662b5cSJustin T. Gibbs  * \param dev  NewBus device_t for this Xen control device instance.
448ff662b5cSJustin T. Gibbs  *
449ff662b5cSJustin T. Gibbs  * \return  On success, 0. Otherwise an errno value indicating the
450ff662b5cSJustin T. Gibbs  *          type of failure.
451ff662b5cSJustin T. Gibbs  */
452ff662b5cSJustin T. Gibbs static int
453ff662b5cSJustin T. Gibbs xctrl_detach(device_t dev)
454ff662b5cSJustin T. Gibbs {
455ff662b5cSJustin T. Gibbs 	struct xctrl_softc *xctrl;
456ff662b5cSJustin T. Gibbs 
457ff662b5cSJustin T. Gibbs 	xctrl = device_get_softc(dev);
458ff662b5cSJustin T. Gibbs 
459ff662b5cSJustin T. Gibbs 	/* Release watch */
460ff662b5cSJustin T. Gibbs 	xs_unregister_watch(&xctrl->xctrl_watch);
461ff662b5cSJustin T. Gibbs 
462ff662b5cSJustin T. Gibbs 	return (0);
463ff662b5cSJustin T. Gibbs }
464ff662b5cSJustin T. Gibbs 
465ff662b5cSJustin T. Gibbs /*-------------------- Private Device Attachment Data  -----------------------*/
466ff662b5cSJustin T. Gibbs static device_method_t xctrl_methods[] = {
467ff662b5cSJustin T. Gibbs 	/* Device interface */
468ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_identify,	xctrl_identify),
469ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_probe,         xctrl_probe),
470ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_attach,        xctrl_attach),
471ff662b5cSJustin T. Gibbs 	DEVMETHOD(device_detach,        xctrl_detach),
472ff662b5cSJustin T. Gibbs 
473dcceabd3SMarius Strobl 	DEVMETHOD_END
474ff662b5cSJustin T. Gibbs };
475ff662b5cSJustin T. Gibbs 
476ff662b5cSJustin T. Gibbs DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
477ff662b5cSJustin T. Gibbs devclass_t xctrl_devclass;
478ff662b5cSJustin T. Gibbs 
479dcceabd3SMarius Strobl DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL);
480