2 * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
36 #include <machine/vmm.h>
41 * The ACPI Power Management timer is a free-running 24- or 32-bit
42 * timer with a frequency of 3.579545MHz
44 * This implementation will be 32-bits
47 #define PMTMR_FREQ 3579545 /* 3.579545MHz */
51 sbintime_t baseuptime;
55 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
58 vpmtmr_init(struct vm *vm)
60 struct vpmtmr *vpmtmr;
63 vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
64 vpmtmr->baseuptime = sbinuptime();
67 FREQ2BT(PMTMR_FREQ, &bt);
68 vpmtmr->freq_sbt = bttosbt(bt);
74 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
77 free(vpmtmr, M_VPMTMR);
81 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
84 struct vpmtmr *vpmtmr;
85 sbintime_t now, delta;
87 if (!in || bytes != 4)
90 vpmtmr = vm_pmtmr(vm);
93 * No locking needed because 'baseuptime' and 'baseval' are
94 * written only during initialization.
97 delta = now - vpmtmr->baseuptime;
98 KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
99 "%#lx to %#lx", vpmtmr->baseuptime, now));
100 *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;