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> 125ff662b5cSJustin T. Gibbs 126ff662b5cSJustin T. Gibbs #include <geom/geom.h> 127ff662b5cSJustin T. Gibbs 128ff662b5cSJustin T. Gibbs #include <machine/_inttypes.h> 12976acc41fSJustin T. Gibbs #include <machine/intr_machdep.h> 130ff662b5cSJustin T. Gibbs 131ff662b5cSJustin T. Gibbs #include <vm/vm.h> 132ff662b5cSJustin T. Gibbs #include <vm/vm_extern.h> 133ff662b5cSJustin T. Gibbs #include <vm/vm_kern.h> 134ff662b5cSJustin T. Gibbs 13576acc41fSJustin T. Gibbs #include <xen/xen-os.h> 136ff662b5cSJustin T. Gibbs #include <xen/blkif.h> 137ff662b5cSJustin T. Gibbs #include <xen/evtchn.h> 138ff662b5cSJustin T. Gibbs #include <xen/gnttab.h> 139ff662b5cSJustin T. Gibbs #include <xen/xen_intr.h> 140ff662b5cSJustin T. Gibbs 141428b7ca2SJustin T. Gibbs #ifdef XENHVM 142428b7ca2SJustin T. Gibbs #include <xen/hvm.h> 143428b7ca2SJustin T. Gibbs #endif 144428b7ca2SJustin T. Gibbs 145ff662b5cSJustin T. Gibbs #include <xen/interface/event_channel.h> 146ff662b5cSJustin T. Gibbs #include <xen/interface/grant_table.h> 147ff662b5cSJustin T. Gibbs 148ff662b5cSJustin T. Gibbs #include <xen/xenbus/xenbusvar.h> 149ff662b5cSJustin T. Gibbs 15076acc41fSJustin T. Gibbs #include <machine/xen/xenvar.h> 15176acc41fSJustin T. Gibbs #include <machine/xen/xenfunc.h> 15276acc41fSJustin T. Gibbs 153ff662b5cSJustin T. Gibbs /*--------------------------- Forward Declarations --------------------------*/ 154ff662b5cSJustin T. Gibbs /** Function signature for shutdown event handlers. */ 155ff662b5cSJustin T. Gibbs typedef void (xctrl_shutdown_handler_t)(void); 156ff662b5cSJustin T. Gibbs 157ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_poweroff; 158ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_reboot; 159ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_suspend; 160ff662b5cSJustin T. Gibbs static xctrl_shutdown_handler_t xctrl_crash; 161ff662b5cSJustin T. Gibbs 162ff662b5cSJustin T. Gibbs /*-------------------------- Private Data Structures -------------------------*/ 163ff662b5cSJustin T. Gibbs /** Element type for lookup table of event name to handler. */ 164ff662b5cSJustin T. Gibbs struct xctrl_shutdown_reason { 165ff662b5cSJustin T. Gibbs const char *name; 166ff662b5cSJustin T. Gibbs xctrl_shutdown_handler_t *handler; 167ff662b5cSJustin T. Gibbs }; 168ff662b5cSJustin T. Gibbs 169ff662b5cSJustin T. Gibbs /** Lookup table for shutdown event name to handler. */ 170dcceabd3SMarius Strobl static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = { 171ff662b5cSJustin T. Gibbs { "poweroff", xctrl_poweroff }, 172ff662b5cSJustin T. Gibbs { "reboot", xctrl_reboot }, 173ff662b5cSJustin T. Gibbs { "suspend", xctrl_suspend }, 174ff662b5cSJustin T. Gibbs { "crash", xctrl_crash }, 1754c85fcfaSJustin T. Gibbs { "halt", xctrl_poweroff }, 176ff662b5cSJustin T. Gibbs }; 177ff662b5cSJustin T. Gibbs 178ff662b5cSJustin T. Gibbs struct xctrl_softc { 179ff662b5cSJustin T. Gibbs struct xs_watch xctrl_watch; 180ff662b5cSJustin T. Gibbs }; 181ff662b5cSJustin T. Gibbs 182ff662b5cSJustin T. Gibbs /*------------------------------ Event Handlers ------------------------------*/ 183ff662b5cSJustin T. Gibbs static void 184ff662b5cSJustin T. Gibbs xctrl_poweroff() 185ff662b5cSJustin T. Gibbs { 186ff662b5cSJustin T. Gibbs shutdown_nice(RB_POWEROFF|RB_HALT); 187ff662b5cSJustin T. Gibbs } 188ff662b5cSJustin T. Gibbs 189ff662b5cSJustin T. Gibbs static void 190ff662b5cSJustin T. Gibbs xctrl_reboot() 191ff662b5cSJustin T. Gibbs { 192ff662b5cSJustin T. Gibbs shutdown_nice(0); 193ff662b5cSJustin T. Gibbs } 194ff662b5cSJustin T. Gibbs 195ff662b5cSJustin T. Gibbs #ifndef XENHVM 196ff662b5cSJustin T. Gibbs extern void xencons_suspend(void); 197ff662b5cSJustin T. Gibbs extern void xencons_resume(void); 198ff662b5cSJustin T. Gibbs 199ff662b5cSJustin T. Gibbs /* Full PV mode suspension. */ 200ff662b5cSJustin T. Gibbs static void 201ff662b5cSJustin T. Gibbs xctrl_suspend() 202ff662b5cSJustin T. Gibbs { 203428b7ca2SJustin T. Gibbs int i, j, k, fpp, suspend_cancelled; 204ff662b5cSJustin T. Gibbs unsigned long max_pfn, start_info_mfn; 205ff662b5cSJustin T. Gibbs 2062ca7463bSJustin T. Gibbs EVENTHANDLER_INVOKE(power_suspend); 2072ca7463bSJustin T. Gibbs 208ff662b5cSJustin T. Gibbs #ifdef SMP 20971a19bdcSAttilio Rao struct thread *td; 21071a19bdcSAttilio Rao cpuset_t map; 211dcceabd3SMarius Strobl u_int cpuid; 212dcceabd3SMarius Strobl 213ff662b5cSJustin T. Gibbs /* 214ff662b5cSJustin T. Gibbs * Bind us to CPU 0 and stop any other VCPUs. 215ff662b5cSJustin T. Gibbs */ 21671a19bdcSAttilio Rao td = curthread; 21771a19bdcSAttilio Rao thread_lock(td); 21871a19bdcSAttilio Rao sched_bind(td, 0); 21971a19bdcSAttilio Rao thread_unlock(td); 220a38f1f26SAttilio Rao cpuid = PCPU_GET(cpuid); 221a38f1f26SAttilio Rao KASSERT(cpuid == 0, ("xen_suspend: not running on cpu 0")); 222ff662b5cSJustin T. Gibbs 223a38f1f26SAttilio Rao map = all_cpus; 224a38f1f26SAttilio Rao CPU_CLR(cpuid, &map); 22571a19bdcSAttilio Rao CPU_NAND(&map, &stopped_cpus); 22671a19bdcSAttilio Rao if (!CPU_EMPTY(&map)) 227ff662b5cSJustin T. Gibbs stop_cpus(map); 228ff662b5cSJustin T. Gibbs #endif 229ff662b5cSJustin T. Gibbs 2302ca7463bSJustin T. Gibbs /* 2312ca7463bSJustin T. Gibbs * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 2322ca7463bSJustin T. Gibbs * drivers need this. 2332ca7463bSJustin T. Gibbs */ 2342ca7463bSJustin T. Gibbs mtx_lock(&Giant); 235ff662b5cSJustin T. Gibbs if (DEVICE_SUSPEND(root_bus) != 0) { 2362ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 237dcceabd3SMarius Strobl printf("%s: device_suspend failed\n", __func__); 238ff662b5cSJustin T. Gibbs #ifdef SMP 23971a19bdcSAttilio Rao if (!CPU_EMPTY(&map)) 240ff662b5cSJustin T. Gibbs restart_cpus(map); 241ff662b5cSJustin T. Gibbs #endif 242ff662b5cSJustin T. Gibbs return; 243ff662b5cSJustin T. Gibbs } 2442ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 245ff662b5cSJustin T. Gibbs 246ff662b5cSJustin T. Gibbs local_irq_disable(); 247ff662b5cSJustin T. Gibbs 248ff662b5cSJustin T. Gibbs xencons_suspend(); 249ff662b5cSJustin T. Gibbs gnttab_suspend(); 25076acc41fSJustin T. Gibbs intr_suspend(); 251ff662b5cSJustin T. Gibbs 252ff662b5cSJustin T. Gibbs max_pfn = HYPERVISOR_shared_info->arch.max_pfn; 253ff662b5cSJustin T. Gibbs 254ff662b5cSJustin T. Gibbs void *shared_info = HYPERVISOR_shared_info; 255ff662b5cSJustin T. Gibbs HYPERVISOR_shared_info = NULL; 256ff662b5cSJustin T. Gibbs pmap_kremove((vm_offset_t) shared_info); 257ff662b5cSJustin T. Gibbs PT_UPDATES_FLUSH(); 258ff662b5cSJustin T. Gibbs 259ff662b5cSJustin T. Gibbs xen_start_info->store_mfn = MFNTOPFN(xen_start_info->store_mfn); 260ff662b5cSJustin T. Gibbs xen_start_info->console.domU.mfn = MFNTOPFN(xen_start_info->console.domU.mfn); 261ff662b5cSJustin T. Gibbs 262ff662b5cSJustin T. Gibbs /* 263ff662b5cSJustin T. Gibbs * We'll stop somewhere inside this hypercall. When it returns, 264ff662b5cSJustin T. Gibbs * we'll start resuming after the restore. 265ff662b5cSJustin T. Gibbs */ 266ff662b5cSJustin T. Gibbs start_info_mfn = VTOMFN(xen_start_info); 267ff662b5cSJustin T. Gibbs pmap_suspend(); 268428b7ca2SJustin T. Gibbs suspend_cancelled = HYPERVISOR_suspend(start_info_mfn); 269ff662b5cSJustin T. Gibbs pmap_resume(); 270ff662b5cSJustin T. Gibbs 271ff662b5cSJustin T. Gibbs pmap_kenter_ma((vm_offset_t) shared_info, xen_start_info->shared_info); 272ff662b5cSJustin T. Gibbs HYPERVISOR_shared_info = shared_info; 273ff662b5cSJustin T. Gibbs 274ff662b5cSJustin T. Gibbs HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list = 275ff662b5cSJustin T. Gibbs VTOMFN(xen_pfn_to_mfn_frame_list_list); 276ff662b5cSJustin T. Gibbs 277ff662b5cSJustin T. Gibbs fpp = PAGE_SIZE/sizeof(unsigned long); 278ff662b5cSJustin T. Gibbs for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) { 279ff662b5cSJustin T. Gibbs if ((j % fpp) == 0) { 280ff662b5cSJustin T. Gibbs k++; 281ff662b5cSJustin T. Gibbs xen_pfn_to_mfn_frame_list_list[k] = 282ff662b5cSJustin T. Gibbs VTOMFN(xen_pfn_to_mfn_frame_list[k]); 283ff662b5cSJustin T. Gibbs j = 0; 284ff662b5cSJustin T. Gibbs } 285ff662b5cSJustin T. Gibbs xen_pfn_to_mfn_frame_list[k][j] = 286ff662b5cSJustin T. Gibbs VTOMFN(&xen_phys_machine[i]); 287ff662b5cSJustin T. Gibbs } 288ff662b5cSJustin T. Gibbs HYPERVISOR_shared_info->arch.max_pfn = max_pfn; 289ff662b5cSJustin T. Gibbs 290ff662b5cSJustin T. Gibbs gnttab_resume(); 291428b7ca2SJustin T. Gibbs intr_resume(suspend_cancelled != 0); 292ff662b5cSJustin T. Gibbs local_irq_enable(); 293ff662b5cSJustin T. Gibbs xencons_resume(); 294ff662b5cSJustin T. Gibbs 295ff662b5cSJustin T. Gibbs #ifdef CONFIG_SMP 296ff662b5cSJustin T. Gibbs for_each_cpu(i) 297ff662b5cSJustin T. Gibbs vcpu_prepare(i); 298ff662b5cSJustin T. Gibbs 299ff662b5cSJustin T. Gibbs #endif 3002ca7463bSJustin T. Gibbs 301ff662b5cSJustin T. Gibbs /* 302ff662b5cSJustin T. Gibbs * Only resume xenbus /after/ we've prepared our VCPUs; otherwise 303ff662b5cSJustin T. Gibbs * the VCPU hotplug callback can race with our vcpu_prepare 304ff662b5cSJustin T. Gibbs */ 3052ca7463bSJustin T. Gibbs mtx_lock(&Giant); 306ff662b5cSJustin T. Gibbs DEVICE_RESUME(root_bus); 3072ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 308ff662b5cSJustin T. Gibbs 309ff662b5cSJustin T. Gibbs #ifdef SMP 310ff662b5cSJustin T. Gibbs thread_lock(curthread); 311ff662b5cSJustin T. Gibbs sched_unbind(curthread); 312ff662b5cSJustin T. Gibbs thread_unlock(curthread); 31371a19bdcSAttilio Rao if (!CPU_EMPTY(&map)) 314ff662b5cSJustin T. Gibbs restart_cpus(map); 315ff662b5cSJustin T. Gibbs #endif 3162ca7463bSJustin T. Gibbs EVENTHANDLER_INVOKE(power_resume); 317ff662b5cSJustin T. Gibbs } 318ff662b5cSJustin T. Gibbs 319ff662b5cSJustin T. Gibbs #else 320ff662b5cSJustin T. Gibbs 321ff662b5cSJustin T. Gibbs /* HVM mode suspension. */ 322ff662b5cSJustin T. Gibbs static void 323ff662b5cSJustin T. Gibbs xctrl_suspend() 324ff662b5cSJustin T. Gibbs { 325428b7ca2SJustin T. Gibbs #ifdef SMP 326428b7ca2SJustin T. Gibbs cpuset_t cpu_suspend_map; 327428b7ca2SJustin T. Gibbs #endif 328ff662b5cSJustin T. Gibbs int suspend_cancelled; 329ff662b5cSJustin T. Gibbs 3302ca7463bSJustin T. Gibbs EVENTHANDLER_INVOKE(power_suspend); 3312ca7463bSJustin T. Gibbs 332428b7ca2SJustin T. Gibbs if (smp_started) { 333428b7ca2SJustin T. Gibbs thread_lock(curthread); 334428b7ca2SJustin T. Gibbs sched_bind(curthread, 0); 335428b7ca2SJustin T. Gibbs thread_unlock(curthread); 336428b7ca2SJustin T. Gibbs } 337428b7ca2SJustin T. Gibbs KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0")); 338428b7ca2SJustin T. Gibbs 339428b7ca2SJustin T. Gibbs /* 340428b7ca2SJustin T. Gibbs * Clear our XenStore node so the toolstack knows we are 341428b7ca2SJustin T. Gibbs * responding to the suspend request. 342428b7ca2SJustin T. Gibbs */ 343428b7ca2SJustin T. Gibbs xs_write(XST_NIL, "control", "shutdown", ""); 344428b7ca2SJustin T. Gibbs 3452ca7463bSJustin T. Gibbs /* 3462ca7463bSJustin T. Gibbs * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 3472ca7463bSJustin T. Gibbs * drivers need this. 3482ca7463bSJustin T. Gibbs */ 3492ca7463bSJustin T. Gibbs mtx_lock(&Giant); 350dcceabd3SMarius Strobl if (DEVICE_SUSPEND(root_bus) != 0) { 3512ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 352dcceabd3SMarius Strobl printf("%s: device_suspend failed\n", __func__); 353ff662b5cSJustin T. Gibbs return; 354ff662b5cSJustin T. Gibbs } 3552ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 356ff662b5cSJustin T. Gibbs 357428b7ca2SJustin T. Gibbs #ifdef SMP 358faf8ff5fSDimitry Andric CPU_ZERO(&cpu_suspend_map); /* silence gcc */ 359428b7ca2SJustin T. Gibbs if (smp_started) { 360428b7ca2SJustin T. Gibbs /* 361428b7ca2SJustin T. Gibbs * Suspend other CPUs. This prevents IPIs while we 362428b7ca2SJustin T. Gibbs * are resuming, and will allow us to reset per-cpu 363428b7ca2SJustin T. Gibbs * vcpu_info on resume. 364428b7ca2SJustin T. Gibbs */ 365428b7ca2SJustin T. Gibbs cpu_suspend_map = all_cpus; 366428b7ca2SJustin T. Gibbs CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map); 367428b7ca2SJustin T. Gibbs if (!CPU_EMPTY(&cpu_suspend_map)) 368428b7ca2SJustin T. Gibbs suspend_cpus(cpu_suspend_map); 369428b7ca2SJustin T. Gibbs } 370428b7ca2SJustin T. Gibbs #endif 371428b7ca2SJustin T. Gibbs 372ff662b5cSJustin T. Gibbs /* 373ff662b5cSJustin T. Gibbs * Prevent any races with evtchn_interrupt() handler. 374ff662b5cSJustin T. Gibbs */ 375ff662b5cSJustin T. Gibbs disable_intr(); 37676acc41fSJustin T. Gibbs intr_suspend(); 377428b7ca2SJustin T. Gibbs xen_hvm_suspend(); 378ff662b5cSJustin T. Gibbs 379ff662b5cSJustin T. Gibbs suspend_cancelled = HYPERVISOR_suspend(0); 38076acc41fSJustin T. Gibbs 381428b7ca2SJustin T. Gibbs xen_hvm_resume(suspend_cancelled != 0); 382428b7ca2SJustin T. Gibbs intr_resume(suspend_cancelled != 0); 383428b7ca2SJustin T. Gibbs enable_intr(); 384ff662b5cSJustin T. Gibbs 385ff662b5cSJustin T. Gibbs /* 386428b7ca2SJustin T. Gibbs * Reset grant table info. 387ff662b5cSJustin T. Gibbs */ 388428b7ca2SJustin T. Gibbs gnttab_resume(); 389428b7ca2SJustin T. Gibbs 390428b7ca2SJustin T. Gibbs #ifdef SMP 391428b7ca2SJustin T. Gibbs if (smp_started && !CPU_EMPTY(&cpu_suspend_map)) { 392428b7ca2SJustin T. Gibbs /* 393428b7ca2SJustin T. Gibbs * Now that event channels have been initialized, 394428b7ca2SJustin T. Gibbs * resume CPUs. 395428b7ca2SJustin T. Gibbs */ 396428b7ca2SJustin T. Gibbs resume_cpus(cpu_suspend_map); 397428b7ca2SJustin T. Gibbs } 398428b7ca2SJustin T. Gibbs #endif 399ff662b5cSJustin T. Gibbs 400ff662b5cSJustin T. Gibbs /* 401ff662b5cSJustin T. Gibbs * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or 402ff662b5cSJustin T. Gibbs * similar. 403ff662b5cSJustin T. Gibbs */ 4042ca7463bSJustin T. Gibbs mtx_lock(&Giant); 405ff662b5cSJustin T. Gibbs DEVICE_RESUME(root_bus); 4062ca7463bSJustin T. Gibbs mtx_unlock(&Giant); 4072ca7463bSJustin T. Gibbs 408428b7ca2SJustin T. Gibbs if (smp_started) { 409428b7ca2SJustin T. Gibbs thread_lock(curthread); 410428b7ca2SJustin T. Gibbs sched_unbind(curthread); 411428b7ca2SJustin T. Gibbs thread_unlock(curthread); 412428b7ca2SJustin T. Gibbs } 413428b7ca2SJustin T. Gibbs 4142ca7463bSJustin T. Gibbs EVENTHANDLER_INVOKE(power_resume); 415428b7ca2SJustin T. Gibbs 416428b7ca2SJustin T. Gibbs if (bootverbose) 417428b7ca2SJustin T. Gibbs printf("System resumed after suspension\n"); 418428b7ca2SJustin T. Gibbs 419ff662b5cSJustin T. Gibbs } 420ff662b5cSJustin T. Gibbs #endif 421ff662b5cSJustin T. Gibbs 422ff662b5cSJustin T. Gibbs static void 423ff662b5cSJustin T. Gibbs xctrl_crash() 424ff662b5cSJustin T. Gibbs { 425ff662b5cSJustin T. Gibbs panic("Xen directed crash"); 426ff662b5cSJustin T. Gibbs } 427ff662b5cSJustin T. Gibbs 428*bd851e63SRoger Pau Monné static void 429*bd851e63SRoger Pau Monné xen_pv_shutdown_final(void *arg, int howto) 430*bd851e63SRoger Pau Monné { 431*bd851e63SRoger Pau Monné /* 432*bd851e63SRoger Pau Monné * Inform the hypervisor that shutdown is complete. 433*bd851e63SRoger Pau Monné * This is not necessary in HVM domains since Xen 434*bd851e63SRoger Pau Monné * emulates ACPI in that mode and FreeBSD's ACPI 435*bd851e63SRoger Pau Monné * support will request this transition. 436*bd851e63SRoger Pau Monné */ 437*bd851e63SRoger Pau Monné if (howto & (RB_HALT | RB_POWEROFF)) 438*bd851e63SRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_poweroff); 439*bd851e63SRoger Pau Monné else 440*bd851e63SRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_reboot); 441*bd851e63SRoger Pau Monné } 442*bd851e63SRoger Pau Monné 443ff662b5cSJustin T. Gibbs /*------------------------------ Event Reception -----------------------------*/ 444ff662b5cSJustin T. Gibbs static void 445ff662b5cSJustin T. Gibbs xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len) 446ff662b5cSJustin T. Gibbs { 447dcceabd3SMarius Strobl const struct xctrl_shutdown_reason *reason; 448dcceabd3SMarius Strobl const struct xctrl_shutdown_reason *last_reason; 449ff662b5cSJustin T. Gibbs char *result; 450ff662b5cSJustin T. Gibbs int error; 451ff662b5cSJustin T. Gibbs int result_len; 452ff662b5cSJustin T. Gibbs 453ff662b5cSJustin T. Gibbs error = xs_read(XST_NIL, "control", "shutdown", 454ff662b5cSJustin T. Gibbs &result_len, (void **)&result); 455ff662b5cSJustin T. Gibbs if (error != 0) 456ff662b5cSJustin T. Gibbs return; 457ff662b5cSJustin T. Gibbs 458ff662b5cSJustin T. Gibbs reason = xctrl_shutdown_reasons; 459dcceabd3SMarius Strobl last_reason = reason + nitems(xctrl_shutdown_reasons); 460ff662b5cSJustin T. Gibbs while (reason < last_reason) { 461ff662b5cSJustin T. Gibbs 462ff662b5cSJustin T. Gibbs if (!strcmp(result, reason->name)) { 463ff662b5cSJustin T. Gibbs reason->handler(); 464ff662b5cSJustin T. Gibbs break; 465ff662b5cSJustin T. Gibbs } 466ff662b5cSJustin T. Gibbs reason++; 467ff662b5cSJustin T. Gibbs } 468ff662b5cSJustin T. Gibbs 469ff662b5cSJustin T. Gibbs free(result, M_XENSTORE); 470ff662b5cSJustin T. Gibbs } 471ff662b5cSJustin T. Gibbs 472ff662b5cSJustin T. Gibbs /*------------------ Private Device Attachment Functions --------------------*/ 473ff662b5cSJustin T. Gibbs /** 474ff662b5cSJustin T. Gibbs * \brief Identify instances of this device type in the system. 475ff662b5cSJustin T. Gibbs * 476ff662b5cSJustin T. Gibbs * \param driver The driver performing this identify action. 477ff662b5cSJustin T. Gibbs * \param parent The NewBus parent device for any devices this method adds. 478ff662b5cSJustin T. Gibbs */ 479ff662b5cSJustin T. Gibbs static void 480ff662b5cSJustin T. Gibbs xctrl_identify(driver_t *driver __unused, device_t parent) 481ff662b5cSJustin T. Gibbs { 482ff662b5cSJustin T. Gibbs /* 483ff662b5cSJustin T. Gibbs * A single device instance for our driver is always present 484ff662b5cSJustin T. Gibbs * in a system operating under Xen. 485ff662b5cSJustin T. Gibbs */ 486ff662b5cSJustin T. Gibbs BUS_ADD_CHILD(parent, 0, driver->name, 0); 487ff662b5cSJustin T. Gibbs } 488ff662b5cSJustin T. Gibbs 489ff662b5cSJustin T. Gibbs /** 490ff662b5cSJustin T. Gibbs * \brief Probe for the existance of the Xen Control device 491ff662b5cSJustin T. Gibbs * 492ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this Xen control instance. 493ff662b5cSJustin T. Gibbs * 494ff662b5cSJustin T. Gibbs * \return Always returns 0 indicating success. 495ff662b5cSJustin T. Gibbs */ 496ff662b5cSJustin T. Gibbs static int 497ff662b5cSJustin T. Gibbs xctrl_probe(device_t dev) 498ff662b5cSJustin T. Gibbs { 499ff662b5cSJustin T. Gibbs device_set_desc(dev, "Xen Control Device"); 500ff662b5cSJustin T. Gibbs 501ff662b5cSJustin T. Gibbs return (0); 502ff662b5cSJustin T. Gibbs } 503ff662b5cSJustin T. Gibbs 504ff662b5cSJustin T. Gibbs /** 505ff662b5cSJustin T. Gibbs * \brief Attach the Xen control device. 506ff662b5cSJustin T. Gibbs * 507ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this Xen control instance. 508ff662b5cSJustin T. Gibbs * 509ff662b5cSJustin T. Gibbs * \return On success, 0. Otherwise an errno value indicating the 510ff662b5cSJustin T. Gibbs * type of failure. 511ff662b5cSJustin T. Gibbs */ 512ff662b5cSJustin T. Gibbs static int 513ff662b5cSJustin T. Gibbs xctrl_attach(device_t dev) 514ff662b5cSJustin T. Gibbs { 515ff662b5cSJustin T. Gibbs struct xctrl_softc *xctrl; 516ff662b5cSJustin T. Gibbs 517ff662b5cSJustin T. Gibbs xctrl = device_get_softc(dev); 518ff662b5cSJustin T. Gibbs 519ff662b5cSJustin T. Gibbs /* Activate watch */ 520ff662b5cSJustin T. Gibbs xctrl->xctrl_watch.node = "control/shutdown"; 521ff662b5cSJustin T. Gibbs xctrl->xctrl_watch.callback = xctrl_on_watch_event; 522283d6f72SJustin T. Gibbs xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl; 523ff662b5cSJustin T. Gibbs xs_register_watch(&xctrl->xctrl_watch); 524ff662b5cSJustin T. Gibbs 525*bd851e63SRoger Pau Monné if (xen_pv_domain()) 526ff662b5cSJustin T. Gibbs EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL, 527ff662b5cSJustin T. Gibbs SHUTDOWN_PRI_LAST); 528ff662b5cSJustin T. Gibbs 529ff662b5cSJustin T. Gibbs return (0); 530ff662b5cSJustin T. Gibbs } 531ff662b5cSJustin T. Gibbs 532ff662b5cSJustin T. Gibbs /** 533ff662b5cSJustin T. Gibbs * \brief Detach the Xen control device. 534ff662b5cSJustin T. Gibbs * 535ff662b5cSJustin T. Gibbs * \param dev NewBus device_t for this Xen control device instance. 536ff662b5cSJustin T. Gibbs * 537ff662b5cSJustin T. Gibbs * \return On success, 0. Otherwise an errno value indicating the 538ff662b5cSJustin T. Gibbs * type of failure. 539ff662b5cSJustin T. Gibbs */ 540ff662b5cSJustin T. Gibbs static int 541ff662b5cSJustin T. Gibbs xctrl_detach(device_t dev) 542ff662b5cSJustin T. Gibbs { 543ff662b5cSJustin T. Gibbs struct xctrl_softc *xctrl; 544ff662b5cSJustin T. Gibbs 545ff662b5cSJustin T. Gibbs xctrl = device_get_softc(dev); 546ff662b5cSJustin T. Gibbs 547ff662b5cSJustin T. Gibbs /* Release watch */ 548ff662b5cSJustin T. Gibbs xs_unregister_watch(&xctrl->xctrl_watch); 549ff662b5cSJustin T. Gibbs 550ff662b5cSJustin T. Gibbs return (0); 551ff662b5cSJustin T. Gibbs } 552ff662b5cSJustin T. Gibbs 553ff662b5cSJustin T. Gibbs /*-------------------- Private Device Attachment Data -----------------------*/ 554ff662b5cSJustin T. Gibbs static device_method_t xctrl_methods[] = { 555ff662b5cSJustin T. Gibbs /* Device interface */ 556ff662b5cSJustin T. Gibbs DEVMETHOD(device_identify, xctrl_identify), 557ff662b5cSJustin T. Gibbs DEVMETHOD(device_probe, xctrl_probe), 558ff662b5cSJustin T. Gibbs DEVMETHOD(device_attach, xctrl_attach), 559ff662b5cSJustin T. Gibbs DEVMETHOD(device_detach, xctrl_detach), 560ff662b5cSJustin T. Gibbs 561dcceabd3SMarius Strobl DEVMETHOD_END 562ff662b5cSJustin T. Gibbs }; 563ff662b5cSJustin T. Gibbs 564ff662b5cSJustin T. Gibbs DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc)); 565ff662b5cSJustin T. Gibbs devclass_t xctrl_devclass; 566ff662b5cSJustin T. Gibbs 567dcceabd3SMarius Strobl DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL); 568