2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_bhyve_snapshot.h"
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
40 #include <machine/vmm.h>
41 #include <machine/vmm_snapshot.h>
46 * The ACPI Power Management timer is a free-running 24- or 32-bit
47 * timer with a frequency of 3.579545MHz
49 * This implementation will be 32-bits
52 #define PMTMR_FREQ 3579545 /* 3.579545MHz */
56 sbintime_t baseuptime;
60 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
63 vpmtmr_init(struct vm *vm)
65 struct vpmtmr *vpmtmr;
68 vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
69 vpmtmr->baseuptime = sbinuptime();
72 FREQ2BT(PMTMR_FREQ, &bt);
73 vpmtmr->freq_sbt = bttosbt(bt);
79 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
82 free(vpmtmr, M_VPMTMR);
86 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
89 struct vpmtmr *vpmtmr;
90 sbintime_t now, delta;
92 if (!in || bytes != 4)
95 vpmtmr = vm_pmtmr(vm);
98 * No locking needed because 'baseuptime' and 'baseval' are
99 * written only during initialization.
102 delta = now - vpmtmr->baseuptime;
103 KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
104 "%#lx to %#lx", vpmtmr->baseuptime, now));
105 *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
110 #ifdef BHYVE_SNAPSHOT
112 vpmtmr_snapshot(struct vpmtmr *vpmtmr, struct vm_snapshot_meta *meta)
116 SNAPSHOT_VAR_OR_LEAVE(vpmtmr->baseval, meta, ret, done);