xref: /freebsd/sys/amd64/vmm/io/vpmtmr.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    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 ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "opt_bhyve_snapshot.h"
31 
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 
38 #include <machine/vmm.h>
39 #include <machine/vmm_snapshot.h>
40 
41 #include "vpmtmr.h"
42 
43 /*
44  * The ACPI Power Management timer is a free-running 24- or 32-bit
45  * timer with a frequency of 3.579545MHz
46  *
47  * This implementation will be 32-bits
48  */
49 
50 #define PMTMR_FREQ	3579545  /* 3.579545MHz */
51 
52 struct vpmtmr {
53 	sbintime_t	freq_sbt;
54 	sbintime_t	baseuptime;
55 	uint32_t	baseval;
56 };
57 
58 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
59 
60 struct vpmtmr *
61 vpmtmr_init(struct vm *vm)
62 {
63 	struct vpmtmr *vpmtmr;
64 	struct bintime bt;
65 
66 	vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
67 	vpmtmr->baseuptime = sbinuptime();
68 	vpmtmr->baseval = 0;
69 
70 	FREQ2BT(PMTMR_FREQ, &bt);
71 	vpmtmr->freq_sbt = bttosbt(bt);
72 
73 	return (vpmtmr);
74 }
75 
76 void
77 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
78 {
79 
80 	free(vpmtmr, M_VPMTMR);
81 }
82 
83 int
84 vpmtmr_handler(struct vm *vm, bool in, int port, int bytes, uint32_t *val)
85 {
86 	struct vpmtmr *vpmtmr;
87 	sbintime_t now, delta;
88 
89 	if (!in || bytes != 4)
90 		return (-1);
91 
92 	vpmtmr = vm_pmtmr(vm);
93 
94 	/*
95 	 * No locking needed because 'baseuptime' and 'baseval' are
96 	 * written only during initialization.
97 	 */
98 	now = sbinuptime();
99 	delta = now - vpmtmr->baseuptime;
100 	KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
101 	    "%#lx to %#lx", vpmtmr->baseuptime, now));
102 	*val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
103 
104 	return (0);
105 }
106 
107 #ifdef BHYVE_SNAPSHOT
108 int
109 vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
110 {
111 	int ret;
112 
113 	SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);
114 
115 done:
116 	return (ret);
117 }
118 #endif
119