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/cpuset.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
37 #include <machine/vmm.h>
42 * The ACPI Power Management timer is a free-running 24- or 32-bit
43 * timer with a frequency of 3.579545MHz
45 * This implementation will be 32-bits
48 #define PMTMR_FREQ 3579545 /* 3.579545MHz */
52 sbintime_t baseuptime;
56 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
59 vpmtmr_init(struct vm *vm)
61 struct vpmtmr *vpmtmr;
64 vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
65 vpmtmr->baseuptime = sbinuptime();
68 FREQ2BT(PMTMR_FREQ, &bt);
69 vpmtmr->freq_sbt = bttosbt(bt);
75 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
78 free(vpmtmr, M_VPMTMR);
82 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
85 struct vpmtmr *vpmtmr;
86 sbintime_t now, delta;
88 if (!in || bytes != 4)
91 vpmtmr = vm_pmtmr(vm);
94 * No locking needed because 'baseuptime' and 'baseval' are
95 * written only during initialization.
98 delta = now - vpmtmr->baseuptime;
99 KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
100 "%#lx to %#lx", vpmtmr->baseuptime, now));
101 *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;