xref: /freebsd/sys/amd64/vmm/io/vpmtmr.c (revision 483d953a86a2507355f8287c5107dc827a0ff516)
1160ef77aSNeel Natu /*-
2ebc3c37cSMarcelo Araujo  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3ebc3c37cSMarcelo Araujo  *
4160ef77aSNeel Natu  * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5160ef77aSNeel Natu  * All rights reserved.
6160ef77aSNeel Natu  *
7160ef77aSNeel Natu  * Redistribution and use in source and binary forms, with or without
8160ef77aSNeel Natu  * modification, are permitted provided that the following conditions
9160ef77aSNeel Natu  * are met:
10160ef77aSNeel Natu  * 1. Redistributions of source code must retain the above copyright
11160ef77aSNeel Natu  *    notice unmodified, this list of conditions, and the following
12160ef77aSNeel Natu  *    disclaimer.
13160ef77aSNeel Natu  * 2. Redistributions in binary form must reproduce the above copyright
14160ef77aSNeel Natu  *    notice, this list of conditions and the following disclaimer in the
15160ef77aSNeel Natu  *    documentation and/or other materials provided with the distribution.
16160ef77aSNeel Natu  *
17160ef77aSNeel Natu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18160ef77aSNeel Natu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19160ef77aSNeel Natu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20160ef77aSNeel Natu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21160ef77aSNeel Natu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22160ef77aSNeel Natu  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23160ef77aSNeel Natu  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24160ef77aSNeel Natu  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25160ef77aSNeel Natu  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26160ef77aSNeel Natu  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27160ef77aSNeel Natu  */
28160ef77aSNeel Natu 
29160ef77aSNeel Natu #include <sys/cdefs.h>
30160ef77aSNeel Natu __FBSDID("$FreeBSD$");
31160ef77aSNeel Natu 
32*483d953aSJohn Baldwin #include "opt_bhyve_snapshot.h"
33*483d953aSJohn Baldwin 
34160ef77aSNeel Natu #include <sys/param.h>
35160ef77aSNeel Natu #include <sys/queue.h>
36160ef77aSNeel Natu #include <sys/kernel.h>
37160ef77aSNeel Natu #include <sys/malloc.h>
38160ef77aSNeel Natu #include <sys/systm.h>
39160ef77aSNeel Natu 
40160ef77aSNeel Natu #include <machine/vmm.h>
41*483d953aSJohn Baldwin #include <machine/vmm_snapshot.h>
42160ef77aSNeel Natu 
43160ef77aSNeel Natu #include "vpmtmr.h"
44160ef77aSNeel Natu 
45160ef77aSNeel Natu /*
46160ef77aSNeel Natu  * The ACPI Power Management timer is a free-running 24- or 32-bit
47160ef77aSNeel Natu  * timer with a frequency of 3.579545MHz
48160ef77aSNeel Natu  *
49160ef77aSNeel Natu  * This implementation will be 32-bits
50160ef77aSNeel Natu  */
51160ef77aSNeel Natu 
52160ef77aSNeel Natu #define PMTMR_FREQ	3579545  /* 3.579545MHz */
53160ef77aSNeel Natu 
54160ef77aSNeel Natu struct vpmtmr {
55160ef77aSNeel Natu 	sbintime_t	freq_sbt;
56160ef77aSNeel Natu 	sbintime_t	baseuptime;
57160ef77aSNeel Natu 	uint32_t	baseval;
58160ef77aSNeel Natu };
59160ef77aSNeel Natu 
60160ef77aSNeel Natu static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
61160ef77aSNeel Natu 
62160ef77aSNeel Natu struct vpmtmr *
63160ef77aSNeel Natu vpmtmr_init(struct vm *vm)
64160ef77aSNeel Natu {
65160ef77aSNeel Natu 	struct vpmtmr *vpmtmr;
66160ef77aSNeel Natu 	struct bintime bt;
67160ef77aSNeel Natu 
68160ef77aSNeel Natu 	vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
69160ef77aSNeel Natu 	vpmtmr->baseuptime = sbinuptime();
70160ef77aSNeel Natu 	vpmtmr->baseval = 0;
71160ef77aSNeel Natu 
72160ef77aSNeel Natu 	FREQ2BT(PMTMR_FREQ, &bt);
73160ef77aSNeel Natu 	vpmtmr->freq_sbt = bttosbt(bt);
74160ef77aSNeel Natu 
75160ef77aSNeel Natu 	return (vpmtmr);
76160ef77aSNeel Natu }
77160ef77aSNeel Natu 
78160ef77aSNeel Natu void
79160ef77aSNeel Natu vpmtmr_cleanup(struct vpmtmr *vpmtmr)
80160ef77aSNeel Natu {
81160ef77aSNeel Natu 
82160ef77aSNeel Natu 	free(vpmtmr, M_VPMTMR);
83160ef77aSNeel Natu }
84160ef77aSNeel Natu 
85160ef77aSNeel Natu int
86f0c8263eSNeel Natu vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
87160ef77aSNeel Natu     uint32_t *val)
88160ef77aSNeel Natu {
89160ef77aSNeel Natu 	struct vpmtmr *vpmtmr;
90160ef77aSNeel Natu 	sbintime_t now, delta;
91160ef77aSNeel Natu 
92160ef77aSNeel Natu 	if (!in || bytes != 4)
93160ef77aSNeel Natu 		return (-1);
94160ef77aSNeel Natu 
95160ef77aSNeel Natu 	vpmtmr = vm_pmtmr(vm);
96160ef77aSNeel Natu 
97160ef77aSNeel Natu 	/*
98160ef77aSNeel Natu 	 * No locking needed because 'baseuptime' and 'baseval' are
99160ef77aSNeel Natu 	 * written only during initialization.
100160ef77aSNeel Natu 	 */
101160ef77aSNeel Natu 	now = sbinuptime();
102160ef77aSNeel Natu 	delta = now - vpmtmr->baseuptime;
103160ef77aSNeel Natu 	KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
104160ef77aSNeel Natu 	    "%#lx to %#lx", vpmtmr->baseuptime, now));
105160ef77aSNeel Natu 	*val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
106160ef77aSNeel Natu 
107160ef77aSNeel Natu 	return (0);
108160ef77aSNeel Natu }
109*483d953aSJohn Baldwin 
110*483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
111*483d953aSJohn Baldwin int
112*483d953aSJohn Baldwin vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
113*483d953aSJohn Baldwin {
114*483d953aSJohn Baldwin 	int ret;
115*483d953aSJohn Baldwin 
116*483d953aSJohn Baldwin 	SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);
117*483d953aSJohn Baldwin 
118*483d953aSJohn Baldwin done:
119*483d953aSJohn Baldwin 	return (ret);
120*483d953aSJohn Baldwin }
121*483d953aSJohn Baldwin #endif
122