]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/pm.c
bhnd(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / pm.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/types.h>
34 #include <machine/vmm.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <pthread.h>
39 #include <signal.h>
40 #include <vmmapi.h>
41
42 #include "acpi.h"
43 #include "inout.h"
44 #include "mevent.h"
45 #include "pci_irq.h"
46 #include "pci_lpc.h"
47
48 static pthread_mutex_t pm_lock = PTHREAD_MUTEX_INITIALIZER;
49 static struct mevent *power_button;
50 static sig_t old_power_handler;
51
52 static unsigned gpe0_active;
53 static unsigned gpe0_enabled;
54 static const unsigned gpe0_valid = (1u << GPE_VMGENC);
55
56 /*
57  * Reset Control register at I/O port 0xcf9.  Bit 2 forces a system
58  * reset when it transitions from 0 to 1.  Bit 1 selects the type of
59  * reset to attempt: 0 selects a "soft" reset, and 1 selects a "hard"
60  * reset.
61  */
62 static int
63 reset_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
64     uint32_t *eax, void *arg)
65 {
66         int error;
67
68         static uint8_t reset_control;
69
70         if (bytes != 1)
71                 return (-1);
72         if (in)
73                 *eax = reset_control;
74         else {
75                 reset_control = *eax;
76
77                 /* Treat hard and soft resets the same. */
78                 if (reset_control & 0x4) {
79                         error = vm_suspend(ctx, VM_SUSPEND_RESET);
80                         assert(error == 0 || errno == EALREADY);
81                 }
82         }
83         return (0);
84 }
85 INOUT_PORT(reset_reg, 0xCF9, IOPORT_F_INOUT, reset_handler);
86
87 /*
88  * ACPI's SCI is a level-triggered interrupt.
89  */
90 static int sci_active;
91
92 static void
93 sci_assert(struct vmctx *ctx)
94 {
95
96         if (sci_active)
97                 return;
98         vm_isa_assert_irq(ctx, SCI_INT, SCI_INT);
99         sci_active = 1;
100 }
101
102 static void
103 sci_deassert(struct vmctx *ctx)
104 {
105
106         if (!sci_active)
107                 return;
108         vm_isa_deassert_irq(ctx, SCI_INT, SCI_INT);
109         sci_active = 0;
110 }
111
112 /*
113  * Power Management 1 Event Registers
114  *
115  * The only power management event supported is a power button upon
116  * receiving SIGTERM.
117  */
118 static uint16_t pm1_enable, pm1_status;
119
120 #define PM1_TMR_STS             0x0001
121 #define PM1_BM_STS              0x0010
122 #define PM1_GBL_STS             0x0020
123 #define PM1_PWRBTN_STS          0x0100
124 #define PM1_SLPBTN_STS          0x0200
125 #define PM1_RTC_STS             0x0400
126 #define PM1_WAK_STS             0x8000
127
128 #define PM1_TMR_EN              0x0001
129 #define PM1_GBL_EN              0x0020
130 #define PM1_PWRBTN_EN           0x0100
131 #define PM1_SLPBTN_EN           0x0200
132 #define PM1_RTC_EN              0x0400
133
134 static void
135 sci_update(struct vmctx *ctx)
136 {
137         int need_sci;
138
139         /* See if the SCI should be active or not. */
140         need_sci = 0;
141         if ((pm1_enable & PM1_TMR_EN) && (pm1_status & PM1_TMR_STS))
142                 need_sci = 1;
143         if ((pm1_enable & PM1_GBL_EN) && (pm1_status & PM1_GBL_STS))
144                 need_sci = 1;
145         if ((pm1_enable & PM1_PWRBTN_EN) && (pm1_status & PM1_PWRBTN_STS))
146                 need_sci = 1;
147         if ((pm1_enable & PM1_SLPBTN_EN) && (pm1_status & PM1_SLPBTN_STS))
148                 need_sci = 1;
149         if ((pm1_enable & PM1_RTC_EN) && (pm1_status & PM1_RTC_STS))
150                 need_sci = 1;
151         if ((gpe0_enabled & gpe0_active) != 0)
152                 need_sci = 1;
153
154         if (need_sci)
155                 sci_assert(ctx);
156         else
157                 sci_deassert(ctx);
158 }
159
160 static int
161 pm1_status_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
162     uint32_t *eax, void *arg)
163 {
164
165         if (bytes != 2)
166                 return (-1);
167
168         pthread_mutex_lock(&pm_lock);
169         if (in)
170                 *eax = pm1_status;
171         else {
172                 /*
173                  * Writes are only permitted to clear certain bits by
174                  * writing 1 to those flags.
175                  */
176                 pm1_status &= ~(*eax & (PM1_WAK_STS | PM1_RTC_STS |
177                     PM1_SLPBTN_STS | PM1_PWRBTN_STS | PM1_BM_STS));
178                 sci_update(ctx);
179         }
180         pthread_mutex_unlock(&pm_lock);
181         return (0);
182 }
183
184 static int
185 pm1_enable_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
186     uint32_t *eax, void *arg)
187 {
188
189         if (bytes != 2)
190                 return (-1);
191
192         pthread_mutex_lock(&pm_lock);
193         if (in)
194                 *eax = pm1_enable;
195         else {
196                 /*
197                  * Only permit certain bits to be set.  We never use
198                  * the global lock, but ACPI-CA whines profusely if it
199                  * can't set GBL_EN.
200                  */
201                 pm1_enable = *eax & (PM1_RTC_EN | PM1_PWRBTN_EN | PM1_GBL_EN);
202                 sci_update(ctx);
203         }
204         pthread_mutex_unlock(&pm_lock);
205         return (0);
206 }
207 INOUT_PORT(pm1_status, PM1A_EVT_ADDR, IOPORT_F_INOUT, pm1_status_handler);
208 INOUT_PORT(pm1_enable, PM1A_EVT_ADDR + 2, IOPORT_F_INOUT, pm1_enable_handler);
209
210 static void
211 power_button_handler(int signal, enum ev_type type, void *arg)
212 {
213         struct vmctx *ctx;
214
215         ctx = arg;
216         pthread_mutex_lock(&pm_lock);
217         if (!(pm1_status & PM1_PWRBTN_STS)) {
218                 pm1_status |= PM1_PWRBTN_STS;
219                 sci_update(ctx);
220         }
221         pthread_mutex_unlock(&pm_lock);
222 }
223
224 /*
225  * Power Management 1 Control Register
226  *
227  * This is mostly unimplemented except that we wish to handle writes that
228  * set SPL_EN to handle S5 (soft power off).
229  */
230 static uint16_t pm1_control;
231
232 #define PM1_SCI_EN      0x0001
233 #define PM1_SLP_TYP     0x1c00
234 #define PM1_SLP_EN      0x2000
235 #define PM1_ALWAYS_ZERO 0xc003
236
237 static int
238 pm1_control_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
239     uint32_t *eax, void *arg)
240 {
241         int error;
242
243         if (bytes != 2)
244                 return (-1);
245         if (in)
246                 *eax = pm1_control;
247         else {
248                 /*
249                  * Various bits are write-only or reserved, so force them
250                  * to zero in pm1_control.  Always preserve SCI_EN as OSPM
251                  * can never change it.
252                  */
253                 pm1_control = (pm1_control & PM1_SCI_EN) |
254                     (*eax & ~(PM1_SLP_EN | PM1_ALWAYS_ZERO));
255
256                 /*
257                  * If SLP_EN is set, check for S5.  Bhyve's _S5_ method
258                  * says that '5' should be stored in SLP_TYP for S5.
259                  */
260                 if (*eax & PM1_SLP_EN) {
261                         if ((pm1_control & PM1_SLP_TYP) >> 10 == 5) {
262                                 error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
263                                 assert(error == 0 || errno == EALREADY);
264                         }
265                 }
266         }
267         return (0);
268 }
269 INOUT_PORT(pm1_control, PM1A_CNT_ADDR, IOPORT_F_INOUT, pm1_control_handler);
270 SYSRES_IO(PM1A_EVT_ADDR, 8);
271
272 void
273 acpi_raise_gpe(struct vmctx *ctx, unsigned bit)
274 {
275         unsigned mask;
276
277         assert(bit < (IO_GPE0_LEN * (8 / 2)));
278         mask = (1u << bit);
279         assert((mask & ~gpe0_valid) == 0);
280
281         pthread_mutex_lock(&pm_lock);
282         gpe0_active |= mask;
283         sci_update(ctx);
284         pthread_mutex_unlock(&pm_lock);
285 }
286
287 static int
288 gpe0_sts(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
289     uint32_t *eax, void *arg)
290 {
291         /*
292          * ACPI 6.2 specifies the GPE register blocks are accessed
293          * byte-at-a-time.
294          */
295         if (bytes != 1)
296                 return (-1);
297
298         pthread_mutex_lock(&pm_lock);
299         if (in)
300                 *eax = gpe0_active;
301         else {
302                 /* W1C */
303                 gpe0_active &= ~(*eax & gpe0_valid);
304                 sci_update(ctx);
305         }
306         pthread_mutex_unlock(&pm_lock);
307         return (0);
308 }
309 INOUT_PORT(gpe0_sts, IO_GPE0_STS, IOPORT_F_INOUT, gpe0_sts);
310
311 static int
312 gpe0_en(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
313     uint32_t *eax, void *arg)
314 {
315         if (bytes != 1)
316                 return (-1);
317
318         pthread_mutex_lock(&pm_lock);
319         if (in)
320                 *eax = gpe0_enabled;
321         else {
322                 gpe0_enabled = (*eax & gpe0_valid);
323                 sci_update(ctx);
324         }
325         pthread_mutex_unlock(&pm_lock);
326         return (0);
327 }
328 INOUT_PORT(gpe0_en, IO_GPE0_EN, IOPORT_F_INOUT, gpe0_en);
329
330 /*
331  * ACPI SMI Command Register
332  *
333  * This write-only register is used to enable and disable ACPI.
334  */
335 static int
336 smi_cmd_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
337     uint32_t *eax, void *arg)
338 {
339
340         assert(!in);
341         if (bytes != 1)
342                 return (-1);
343
344         pthread_mutex_lock(&pm_lock);
345         switch (*eax) {
346         case BHYVE_ACPI_ENABLE:
347                 pm1_control |= PM1_SCI_EN;
348                 if (power_button == NULL) {
349                         power_button = mevent_add(SIGTERM, EVF_SIGNAL,
350                             power_button_handler, ctx);
351                         old_power_handler = signal(SIGTERM, SIG_IGN);
352                 }
353                 break;
354         case BHYVE_ACPI_DISABLE:
355                 pm1_control &= ~PM1_SCI_EN;
356                 if (power_button != NULL) {
357                         mevent_delete(power_button);
358                         power_button = NULL;
359                         signal(SIGTERM, old_power_handler);
360                 }
361                 break;
362         }
363         pthread_mutex_unlock(&pm_lock);
364         return (0);
365 }
366 INOUT_PORT(smi_cmd, SMI_CMD, IOPORT_F_OUT, smi_cmd_handler);
367 SYSRES_IO(SMI_CMD, 1);
368
369 void
370 sci_init(struct vmctx *ctx)
371 {
372
373         /*
374          * Mark ACPI's SCI as level trigger and bump its use count
375          * in the PIRQ router.
376          */
377         pci_irq_use(SCI_INT);
378         vm_isa_set_irq_trigger(ctx, SCI_INT, LEVEL_TRIGGER);
379 }