xref: /freebsd/sys/amd64/vmm/io/vpmtmr.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1160ef77aSNeel Natu /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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>
30483d953aSJohn Baldwin #include "opt_bhyve_snapshot.h"
31483d953aSJohn Baldwin 
32160ef77aSNeel Natu #include <sys/param.h>
33160ef77aSNeel Natu #include <sys/queue.h>
34160ef77aSNeel Natu #include <sys/kernel.h>
35160ef77aSNeel Natu #include <sys/malloc.h>
36160ef77aSNeel Natu #include <sys/systm.h>
37160ef77aSNeel Natu 
38160ef77aSNeel Natu #include <machine/vmm.h>
39483d953aSJohn Baldwin #include <machine/vmm_snapshot.h>
40160ef77aSNeel Natu 
41160ef77aSNeel Natu #include "vpmtmr.h"
42160ef77aSNeel Natu 
43160ef77aSNeel Natu /*
44160ef77aSNeel Natu  * The ACPI Power Management timer is a free-running 24- or 32-bit
45160ef77aSNeel Natu  * timer with a frequency of 3.579545MHz
46160ef77aSNeel Natu  *
47160ef77aSNeel Natu  * This implementation will be 32-bits
48160ef77aSNeel Natu  */
49160ef77aSNeel Natu 
50160ef77aSNeel Natu #define PMTMR_FREQ	3579545  /* 3.579545MHz */
51160ef77aSNeel Natu 
52160ef77aSNeel Natu struct vpmtmr {
53160ef77aSNeel Natu 	sbintime_t	freq_sbt;
54160ef77aSNeel Natu 	sbintime_t	baseuptime;
55160ef77aSNeel Natu 	uint32_t	baseval;
56160ef77aSNeel Natu };
57160ef77aSNeel Natu 
58160ef77aSNeel Natu static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
59160ef77aSNeel Natu 
60160ef77aSNeel Natu struct vpmtmr *
vpmtmr_init(struct vm * vm)61160ef77aSNeel Natu vpmtmr_init(struct vm *vm)
62160ef77aSNeel Natu {
63160ef77aSNeel Natu 	struct vpmtmr *vpmtmr;
64160ef77aSNeel Natu 	struct bintime bt;
65160ef77aSNeel Natu 
66160ef77aSNeel Natu 	vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
67160ef77aSNeel Natu 	vpmtmr->baseuptime = sbinuptime();
68160ef77aSNeel Natu 	vpmtmr->baseval = 0;
69160ef77aSNeel Natu 
70160ef77aSNeel Natu 	FREQ2BT(PMTMR_FREQ, &bt);
71160ef77aSNeel Natu 	vpmtmr->freq_sbt = bttosbt(bt);
72160ef77aSNeel Natu 
73160ef77aSNeel Natu 	return (vpmtmr);
74160ef77aSNeel Natu }
75160ef77aSNeel Natu 
76160ef77aSNeel Natu void
vpmtmr_cleanup(struct vpmtmr * vpmtmr)77160ef77aSNeel Natu vpmtmr_cleanup(struct vpmtmr *vpmtmr)
78160ef77aSNeel Natu {
79160ef77aSNeel Natu 
80160ef77aSNeel Natu 	free(vpmtmr, M_VPMTMR);
81160ef77aSNeel Natu }
82160ef77aSNeel Natu 
83160ef77aSNeel Natu int
vpmtmr_handler(struct vm * vm,bool in,int port,int bytes,uint32_t * val)849388bc1eSJohn Baldwin vpmtmr_handler(struct vm *vm, bool in, int port, int bytes, uint32_t *val)
85160ef77aSNeel Natu {
86160ef77aSNeel Natu 	struct vpmtmr *vpmtmr;
87160ef77aSNeel Natu 	sbintime_t now, delta;
88160ef77aSNeel Natu 
89160ef77aSNeel Natu 	if (!in || bytes != 4)
90160ef77aSNeel Natu 		return (-1);
91160ef77aSNeel Natu 
92160ef77aSNeel Natu 	vpmtmr = vm_pmtmr(vm);
93160ef77aSNeel Natu 
94160ef77aSNeel Natu 	/*
95160ef77aSNeel Natu 	 * No locking needed because 'baseuptime' and 'baseval' are
96160ef77aSNeel Natu 	 * written only during initialization.
97160ef77aSNeel Natu 	 */
98160ef77aSNeel Natu 	now = sbinuptime();
99160ef77aSNeel Natu 	delta = now - vpmtmr->baseuptime;
100160ef77aSNeel Natu 	KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
101160ef77aSNeel Natu 	    "%#lx to %#lx", vpmtmr->baseuptime, now));
102160ef77aSNeel Natu 	*val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
103160ef77aSNeel Natu 
104160ef77aSNeel Natu 	return (0);
105160ef77aSNeel Natu }
106483d953aSJohn Baldwin 
107483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
108483d953aSJohn Baldwin int
vpmtmr_snapshot(struct vpmtmr * vpmtmr,struct vm_snapshot_meta * meta)109483d953aSJohn Baldwin vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
110483d953aSJohn Baldwin {
111483d953aSJohn Baldwin 	int ret;
112483d953aSJohn Baldwin 
113483d953aSJohn Baldwin 	SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);
114483d953aSJohn Baldwin 
115483d953aSJohn Baldwin done:
116483d953aSJohn Baldwin 	return (ret);
117483d953aSJohn Baldwin }
118483d953aSJohn Baldwin #endif
119