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