xref: /freebsd/usr.sbin/bhyve/amd64/pm.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1*75d1e855SMark Johnston /*-
2*75d1e855SMark Johnston  * SPDX-License-Identifier: BSD-2-Clause
3*75d1e855SMark Johnston  *
4*75d1e855SMark Johnston  * Copyright (c) 2013 Hudson River Trading LLC
5*75d1e855SMark Johnston  * Written by: John H. Baldwin <jhb@FreeBSD.org>
6*75d1e855SMark Johnston  * All rights reserved.
7*75d1e855SMark Johnston  *
8*75d1e855SMark Johnston  * Redistribution and use in source and binary forms, with or without
9*75d1e855SMark Johnston  * modification, are permitted provided that the following conditions
10*75d1e855SMark Johnston  * are met:
11*75d1e855SMark Johnston  * 1. Redistributions of source code must retain the above copyright
12*75d1e855SMark Johnston  *    notice, this list of conditions and the following disclaimer.
13*75d1e855SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
14*75d1e855SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
15*75d1e855SMark Johnston  *    documentation and/or other materials provided with the distribution.
16*75d1e855SMark Johnston  *
17*75d1e855SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*75d1e855SMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*75d1e855SMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*75d1e855SMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*75d1e855SMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*75d1e855SMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*75d1e855SMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*75d1e855SMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*75d1e855SMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*75d1e855SMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*75d1e855SMark Johnston  * SUCH DAMAGE.
28*75d1e855SMark Johnston  */
29*75d1e855SMark Johnston 
30*75d1e855SMark Johnston #include <sys/types.h>
31*75d1e855SMark Johnston #include <machine/vmm.h>
32*75d1e855SMark Johnston 
33*75d1e855SMark Johnston #include <assert.h>
34*75d1e855SMark Johnston #include <errno.h>
35*75d1e855SMark Johnston #include <pthread.h>
36*75d1e855SMark Johnston #include <signal.h>
37*75d1e855SMark Johnston #include <vmmapi.h>
38*75d1e855SMark Johnston 
39*75d1e855SMark Johnston #include "acpi.h"
40*75d1e855SMark Johnston #include "inout.h"
41*75d1e855SMark Johnston #include "mevent.h"
42*75d1e855SMark Johnston #include "pci_irq.h"
43*75d1e855SMark Johnston #include "pci_lpc.h"
44*75d1e855SMark Johnston 
45*75d1e855SMark Johnston static pthread_mutex_t pm_lock = PTHREAD_MUTEX_INITIALIZER;
46*75d1e855SMark Johnston static struct mevent *power_button;
47*75d1e855SMark Johnston static sig_t old_power_handler;
48*75d1e855SMark Johnston 
49*75d1e855SMark Johnston static unsigned gpe0_active;
50*75d1e855SMark Johnston static unsigned gpe0_enabled;
51*75d1e855SMark Johnston static const unsigned gpe0_valid = (1u << GPE_VMGENC);
52*75d1e855SMark Johnston 
53*75d1e855SMark Johnston /*
54*75d1e855SMark Johnston  * Reset Control register at I/O port 0xcf9.  Bit 2 forces a system
55*75d1e855SMark Johnston  * reset when it transitions from 0 to 1.  Bit 1 selects the type of
56*75d1e855SMark Johnston  * reset to attempt: 0 selects a "soft" reset, and 1 selects a "hard"
57*75d1e855SMark Johnston  * reset.
58*75d1e855SMark Johnston  */
59*75d1e855SMark Johnston static int
reset_handler(struct vmctx * ctx __unused,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)60*75d1e855SMark Johnston reset_handler(struct vmctx *ctx __unused, int in,
61*75d1e855SMark Johnston     int port __unused, int bytes, uint32_t *eax, void *arg __unused)
62*75d1e855SMark Johnston {
63*75d1e855SMark Johnston 	int error;
64*75d1e855SMark Johnston 
65*75d1e855SMark Johnston 	static uint8_t reset_control;
66*75d1e855SMark Johnston 
67*75d1e855SMark Johnston 	if (bytes != 1)
68*75d1e855SMark Johnston 		return (-1);
69*75d1e855SMark Johnston 	if (in)
70*75d1e855SMark Johnston 		*eax = reset_control;
71*75d1e855SMark Johnston 	else {
72*75d1e855SMark Johnston 		reset_control = *eax;
73*75d1e855SMark Johnston 
74*75d1e855SMark Johnston 		/* Treat hard and soft resets the same. */
75*75d1e855SMark Johnston 		if (reset_control & 0x4) {
76*75d1e855SMark Johnston 			error = vm_suspend(ctx, VM_SUSPEND_RESET);
77*75d1e855SMark Johnston 			assert(error == 0 || errno == EALREADY);
78*75d1e855SMark Johnston 		}
79*75d1e855SMark Johnston 	}
80*75d1e855SMark Johnston 	return (0);
81*75d1e855SMark Johnston }
82*75d1e855SMark Johnston INOUT_PORT(reset_reg, 0xCF9, IOPORT_F_INOUT, reset_handler);
83*75d1e855SMark Johnston 
84*75d1e855SMark Johnston /*
85*75d1e855SMark Johnston  * ACPI's SCI is a level-triggered interrupt.
86*75d1e855SMark Johnston  */
87*75d1e855SMark Johnston static int sci_active;
88*75d1e855SMark Johnston 
89*75d1e855SMark Johnston static void
sci_assert(struct vmctx * ctx)90*75d1e855SMark Johnston sci_assert(struct vmctx *ctx)
91*75d1e855SMark Johnston {
92*75d1e855SMark Johnston 
93*75d1e855SMark Johnston 	if (sci_active)
94*75d1e855SMark Johnston 		return;
95*75d1e855SMark Johnston 	vm_isa_assert_irq(ctx, SCI_INT, SCI_INT);
96*75d1e855SMark Johnston 	sci_active = 1;
97*75d1e855SMark Johnston }
98*75d1e855SMark Johnston 
99*75d1e855SMark Johnston static void
sci_deassert(struct vmctx * ctx)100*75d1e855SMark Johnston sci_deassert(struct vmctx *ctx)
101*75d1e855SMark Johnston {
102*75d1e855SMark Johnston 
103*75d1e855SMark Johnston 	if (!sci_active)
104*75d1e855SMark Johnston 		return;
105*75d1e855SMark Johnston 	vm_isa_deassert_irq(ctx, SCI_INT, SCI_INT);
106*75d1e855SMark Johnston 	sci_active = 0;
107*75d1e855SMark Johnston }
108*75d1e855SMark Johnston 
109*75d1e855SMark Johnston /*
110*75d1e855SMark Johnston  * Power Management 1 Event Registers
111*75d1e855SMark Johnston  *
112*75d1e855SMark Johnston  * The only power management event supported is a power button upon
113*75d1e855SMark Johnston  * receiving SIGTERM.
114*75d1e855SMark Johnston  */
115*75d1e855SMark Johnston static uint16_t pm1_enable, pm1_status;
116*75d1e855SMark Johnston 
117*75d1e855SMark Johnston #define	PM1_TMR_STS		0x0001
118*75d1e855SMark Johnston #define	PM1_BM_STS		0x0010
119*75d1e855SMark Johnston #define	PM1_GBL_STS		0x0020
120*75d1e855SMark Johnston #define	PM1_PWRBTN_STS		0x0100
121*75d1e855SMark Johnston #define	PM1_SLPBTN_STS		0x0200
122*75d1e855SMark Johnston #define	PM1_RTC_STS		0x0400
123*75d1e855SMark Johnston #define	PM1_WAK_STS		0x8000
124*75d1e855SMark Johnston 
125*75d1e855SMark Johnston #define	PM1_TMR_EN		0x0001
126*75d1e855SMark Johnston #define	PM1_GBL_EN		0x0020
127*75d1e855SMark Johnston #define	PM1_PWRBTN_EN		0x0100
128*75d1e855SMark Johnston #define	PM1_SLPBTN_EN		0x0200
129*75d1e855SMark Johnston #define	PM1_RTC_EN		0x0400
130*75d1e855SMark Johnston 
131*75d1e855SMark Johnston static void
sci_update(struct vmctx * ctx)132*75d1e855SMark Johnston sci_update(struct vmctx *ctx)
133*75d1e855SMark Johnston {
134*75d1e855SMark Johnston 	int need_sci;
135*75d1e855SMark Johnston 
136*75d1e855SMark Johnston 	/* See if the SCI should be active or not. */
137*75d1e855SMark Johnston 	need_sci = 0;
138*75d1e855SMark Johnston 	if ((pm1_enable & PM1_TMR_EN) && (pm1_status & PM1_TMR_STS))
139*75d1e855SMark Johnston 		need_sci = 1;
140*75d1e855SMark Johnston 	if ((pm1_enable & PM1_GBL_EN) && (pm1_status & PM1_GBL_STS))
141*75d1e855SMark Johnston 		need_sci = 1;
142*75d1e855SMark Johnston 	if ((pm1_enable & PM1_PWRBTN_EN) && (pm1_status & PM1_PWRBTN_STS))
143*75d1e855SMark Johnston 		need_sci = 1;
144*75d1e855SMark Johnston 	if ((pm1_enable & PM1_SLPBTN_EN) && (pm1_status & PM1_SLPBTN_STS))
145*75d1e855SMark Johnston 		need_sci = 1;
146*75d1e855SMark Johnston 	if ((pm1_enable & PM1_RTC_EN) && (pm1_status & PM1_RTC_STS))
147*75d1e855SMark Johnston 		need_sci = 1;
148*75d1e855SMark Johnston 	if ((gpe0_enabled & gpe0_active) != 0)
149*75d1e855SMark Johnston 		need_sci = 1;
150*75d1e855SMark Johnston 
151*75d1e855SMark Johnston 	if (need_sci)
152*75d1e855SMark Johnston 		sci_assert(ctx);
153*75d1e855SMark Johnston 	else
154*75d1e855SMark Johnston 		sci_deassert(ctx);
155*75d1e855SMark Johnston }
156*75d1e855SMark Johnston 
157*75d1e855SMark Johnston static int
pm1_status_handler(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)158*75d1e855SMark Johnston pm1_status_handler(struct vmctx *ctx, int in,
159*75d1e855SMark Johnston     int port __unused, int bytes, uint32_t *eax, void *arg __unused)
160*75d1e855SMark Johnston {
161*75d1e855SMark Johnston 
162*75d1e855SMark Johnston 	if (bytes != 2)
163*75d1e855SMark Johnston 		return (-1);
164*75d1e855SMark Johnston 
165*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
166*75d1e855SMark Johnston 	if (in)
167*75d1e855SMark Johnston 		*eax = pm1_status;
168*75d1e855SMark Johnston 	else {
169*75d1e855SMark Johnston 		/*
170*75d1e855SMark Johnston 		 * Writes are only permitted to clear certain bits by
171*75d1e855SMark Johnston 		 * writing 1 to those flags.
172*75d1e855SMark Johnston 		 */
173*75d1e855SMark Johnston 		pm1_status &= ~(*eax & (PM1_WAK_STS | PM1_RTC_STS |
174*75d1e855SMark Johnston 		    PM1_SLPBTN_STS | PM1_PWRBTN_STS | PM1_BM_STS));
175*75d1e855SMark Johnston 		sci_update(ctx);
176*75d1e855SMark Johnston 	}
177*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
178*75d1e855SMark Johnston 	return (0);
179*75d1e855SMark Johnston }
180*75d1e855SMark Johnston 
181*75d1e855SMark Johnston static int
pm1_enable_handler(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)182*75d1e855SMark Johnston pm1_enable_handler(struct vmctx *ctx, int in,
183*75d1e855SMark Johnston     int port __unused, int bytes, uint32_t *eax, void *arg __unused)
184*75d1e855SMark Johnston {
185*75d1e855SMark Johnston 
186*75d1e855SMark Johnston 	if (bytes != 2)
187*75d1e855SMark Johnston 		return (-1);
188*75d1e855SMark Johnston 
189*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
190*75d1e855SMark Johnston 	if (in)
191*75d1e855SMark Johnston 		*eax = pm1_enable;
192*75d1e855SMark Johnston 	else {
193*75d1e855SMark Johnston 		/*
194*75d1e855SMark Johnston 		 * Only permit certain bits to be set.  We never use
195*75d1e855SMark Johnston 		 * the global lock, but ACPI-CA whines profusely if it
196*75d1e855SMark Johnston 		 * can't set GBL_EN.
197*75d1e855SMark Johnston 		 */
198*75d1e855SMark Johnston 		pm1_enable = *eax & (PM1_RTC_EN | PM1_PWRBTN_EN | PM1_GBL_EN);
199*75d1e855SMark Johnston 		sci_update(ctx);
200*75d1e855SMark Johnston 	}
201*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
202*75d1e855SMark Johnston 	return (0);
203*75d1e855SMark Johnston }
204*75d1e855SMark Johnston INOUT_PORT(pm1_status, PM1A_EVT_ADDR, IOPORT_F_INOUT, pm1_status_handler);
205*75d1e855SMark Johnston INOUT_PORT(pm1_enable, PM1A_EVT_ADDR + 2, IOPORT_F_INOUT, pm1_enable_handler);
206*75d1e855SMark Johnston 
207*75d1e855SMark Johnston static void
power_button_handler(int signal __unused,enum ev_type type __unused,void * arg)208*75d1e855SMark Johnston power_button_handler(int signal __unused, enum ev_type type __unused, void *arg)
209*75d1e855SMark Johnston {
210*75d1e855SMark Johnston 	struct vmctx *ctx;
211*75d1e855SMark Johnston 
212*75d1e855SMark Johnston 	ctx = arg;
213*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
214*75d1e855SMark Johnston 	if (!(pm1_status & PM1_PWRBTN_STS)) {
215*75d1e855SMark Johnston 		pm1_status |= PM1_PWRBTN_STS;
216*75d1e855SMark Johnston 		sci_update(ctx);
217*75d1e855SMark Johnston 	}
218*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
219*75d1e855SMark Johnston }
220*75d1e855SMark Johnston 
221*75d1e855SMark Johnston /*
222*75d1e855SMark Johnston  * Power Management 1 Control Register
223*75d1e855SMark Johnston  *
224*75d1e855SMark Johnston  * This is mostly unimplemented except that we wish to handle writes that
225*75d1e855SMark Johnston  * set SPL_EN to handle S5 (soft power off).
226*75d1e855SMark Johnston  */
227*75d1e855SMark Johnston static uint16_t pm1_control;
228*75d1e855SMark Johnston 
229*75d1e855SMark Johnston #define	PM1_SCI_EN	0x0001
230*75d1e855SMark Johnston #define	PM1_SLP_TYP	0x1c00
231*75d1e855SMark Johnston #define	PM1_SLP_EN	0x2000
232*75d1e855SMark Johnston #define	PM1_ALWAYS_ZERO	0xc003
233*75d1e855SMark Johnston 
234*75d1e855SMark Johnston static int
pm1_control_handler(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)235*75d1e855SMark Johnston pm1_control_handler(struct vmctx *ctx, int in,
236*75d1e855SMark Johnston     int port __unused, int bytes, uint32_t *eax, void *arg __unused)
237*75d1e855SMark Johnston {
238*75d1e855SMark Johnston 	int error;
239*75d1e855SMark Johnston 
240*75d1e855SMark Johnston 	if (bytes != 2)
241*75d1e855SMark Johnston 		return (-1);
242*75d1e855SMark Johnston 	if (in)
243*75d1e855SMark Johnston 		*eax = pm1_control;
244*75d1e855SMark Johnston 	else {
245*75d1e855SMark Johnston 		/*
246*75d1e855SMark Johnston 		 * Various bits are write-only or reserved, so force them
247*75d1e855SMark Johnston 		 * to zero in pm1_control.  Always preserve SCI_EN as OSPM
248*75d1e855SMark Johnston 		 * can never change it.
249*75d1e855SMark Johnston 		 */
250*75d1e855SMark Johnston 		pm1_control = (pm1_control & PM1_SCI_EN) |
251*75d1e855SMark Johnston 		    (*eax & ~(PM1_SLP_EN | PM1_ALWAYS_ZERO));
252*75d1e855SMark Johnston 
253*75d1e855SMark Johnston 		/*
254*75d1e855SMark Johnston 		 * If SLP_EN is set, check for S5.  Bhyve's _S5_ method
255*75d1e855SMark Johnston 		 * says that '5' should be stored in SLP_TYP for S5.
256*75d1e855SMark Johnston 		 */
257*75d1e855SMark Johnston 		if (*eax & PM1_SLP_EN) {
258*75d1e855SMark Johnston 			if ((pm1_control & PM1_SLP_TYP) >> 10 == 5) {
259*75d1e855SMark Johnston 				error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
260*75d1e855SMark Johnston 				assert(error == 0 || errno == EALREADY);
261*75d1e855SMark Johnston 			}
262*75d1e855SMark Johnston 		}
263*75d1e855SMark Johnston 	}
264*75d1e855SMark Johnston 	return (0);
265*75d1e855SMark Johnston }
266*75d1e855SMark Johnston INOUT_PORT(pm1_control, PM1A_CNT_ADDR, IOPORT_F_INOUT, pm1_control_handler);
267*75d1e855SMark Johnston SYSRES_IO(PM1A_EVT_ADDR, 8);
268*75d1e855SMark Johnston 
269*75d1e855SMark Johnston void
acpi_raise_gpe(struct vmctx * ctx,unsigned bit)270*75d1e855SMark Johnston acpi_raise_gpe(struct vmctx *ctx, unsigned bit)
271*75d1e855SMark Johnston {
272*75d1e855SMark Johnston 	unsigned mask;
273*75d1e855SMark Johnston 
274*75d1e855SMark Johnston 	assert(bit < (IO_GPE0_LEN * (8 / 2)));
275*75d1e855SMark Johnston 	mask = (1u << bit);
276*75d1e855SMark Johnston 	assert((mask & ~gpe0_valid) == 0);
277*75d1e855SMark Johnston 
278*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
279*75d1e855SMark Johnston 	gpe0_active |= mask;
280*75d1e855SMark Johnston 	sci_update(ctx);
281*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
282*75d1e855SMark Johnston }
283*75d1e855SMark Johnston 
284*75d1e855SMark Johnston static int
gpe0_sts(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)285*75d1e855SMark Johnston gpe0_sts(struct vmctx *ctx, int in, int port __unused,
286*75d1e855SMark Johnston     int bytes, uint32_t *eax, void *arg __unused)
287*75d1e855SMark Johnston {
288*75d1e855SMark Johnston 	/*
289*75d1e855SMark Johnston 	 * ACPI 6.2 specifies the GPE register blocks are accessed
290*75d1e855SMark Johnston 	 * byte-at-a-time.
291*75d1e855SMark Johnston 	 */
292*75d1e855SMark Johnston 	if (bytes != 1)
293*75d1e855SMark Johnston 		return (-1);
294*75d1e855SMark Johnston 
295*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
296*75d1e855SMark Johnston 	if (in)
297*75d1e855SMark Johnston 		*eax = gpe0_active;
298*75d1e855SMark Johnston 	else {
299*75d1e855SMark Johnston 		/* W1C */
300*75d1e855SMark Johnston 		gpe0_active &= ~(*eax & gpe0_valid);
301*75d1e855SMark Johnston 		sci_update(ctx);
302*75d1e855SMark Johnston 	}
303*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
304*75d1e855SMark Johnston 	return (0);
305*75d1e855SMark Johnston }
306*75d1e855SMark Johnston INOUT_PORT(gpe0_sts, IO_GPE0_STS, IOPORT_F_INOUT, gpe0_sts);
307*75d1e855SMark Johnston 
308*75d1e855SMark Johnston static int
gpe0_en(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)309*75d1e855SMark Johnston gpe0_en(struct vmctx *ctx, int in, int port __unused,
310*75d1e855SMark Johnston     int bytes, uint32_t *eax, void *arg __unused)
311*75d1e855SMark Johnston {
312*75d1e855SMark Johnston 	if (bytes != 1)
313*75d1e855SMark Johnston 		return (-1);
314*75d1e855SMark Johnston 
315*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
316*75d1e855SMark Johnston 	if (in)
317*75d1e855SMark Johnston 		*eax = gpe0_enabled;
318*75d1e855SMark Johnston 	else {
319*75d1e855SMark Johnston 		gpe0_enabled = (*eax & gpe0_valid);
320*75d1e855SMark Johnston 		sci_update(ctx);
321*75d1e855SMark Johnston 	}
322*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
323*75d1e855SMark Johnston 	return (0);
324*75d1e855SMark Johnston }
325*75d1e855SMark Johnston INOUT_PORT(gpe0_en, IO_GPE0_EN, IOPORT_F_INOUT, gpe0_en);
326*75d1e855SMark Johnston 
327*75d1e855SMark Johnston /*
328*75d1e855SMark Johnston  * ACPI SMI Command Register
329*75d1e855SMark Johnston  *
330*75d1e855SMark Johnston  * This write-only register is used to enable and disable ACPI.
331*75d1e855SMark Johnston  */
332*75d1e855SMark Johnston static int
smi_cmd_handler(struct vmctx * ctx,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)333*75d1e855SMark Johnston smi_cmd_handler(struct vmctx *ctx, int in, int port __unused,
334*75d1e855SMark Johnston     int bytes, uint32_t *eax, void *arg __unused)
335*75d1e855SMark Johnston {
336*75d1e855SMark Johnston 
337*75d1e855SMark Johnston 	assert(!in);
338*75d1e855SMark Johnston 	if (bytes != 1)
339*75d1e855SMark Johnston 		return (-1);
340*75d1e855SMark Johnston 
341*75d1e855SMark Johnston 	pthread_mutex_lock(&pm_lock);
342*75d1e855SMark Johnston 	switch (*eax) {
343*75d1e855SMark Johnston 	case BHYVE_ACPI_ENABLE:
344*75d1e855SMark Johnston 		pm1_control |= PM1_SCI_EN;
345*75d1e855SMark Johnston 		if (power_button == NULL) {
346*75d1e855SMark Johnston 			power_button = mevent_add(SIGTERM, EVF_SIGNAL,
347*75d1e855SMark Johnston 			    power_button_handler, ctx);
348*75d1e855SMark Johnston 			old_power_handler = signal(SIGTERM, SIG_IGN);
349*75d1e855SMark Johnston 		}
350*75d1e855SMark Johnston 		break;
351*75d1e855SMark Johnston 	case BHYVE_ACPI_DISABLE:
352*75d1e855SMark Johnston 		pm1_control &= ~PM1_SCI_EN;
353*75d1e855SMark Johnston 		if (power_button != NULL) {
354*75d1e855SMark Johnston 			mevent_delete(power_button);
355*75d1e855SMark Johnston 			power_button = NULL;
356*75d1e855SMark Johnston 			signal(SIGTERM, old_power_handler);
357*75d1e855SMark Johnston 		}
358*75d1e855SMark Johnston 		break;
359*75d1e855SMark Johnston 	}
360*75d1e855SMark Johnston 	pthread_mutex_unlock(&pm_lock);
361*75d1e855SMark Johnston 	return (0);
362*75d1e855SMark Johnston }
363*75d1e855SMark Johnston INOUT_PORT(smi_cmd, SMI_CMD, IOPORT_F_OUT, smi_cmd_handler);
364*75d1e855SMark Johnston SYSRES_IO(SMI_CMD, 1);
365*75d1e855SMark Johnston 
366*75d1e855SMark Johnston void
sci_init(struct vmctx * ctx)367*75d1e855SMark Johnston sci_init(struct vmctx *ctx)
368*75d1e855SMark Johnston {
369*75d1e855SMark Johnston 
370*75d1e855SMark Johnston 	/*
371*75d1e855SMark Johnston 	 * Mark ACPI's SCI as level trigger and bump its use count
372*75d1e855SMark Johnston 	 * in the PIRQ router.
373*75d1e855SMark Johnston 	 */
374*75d1e855SMark Johnston 	pci_irq_use(SCI_INT);
375*75d1e855SMark Johnston 	vm_isa_set_irq_trigger(ctx, SCI_INT, LEVEL_TRIGGER);
376*75d1e855SMark Johnston }
377