xref: /freebsd/sys/amd64/vmm/io/vpmtmr.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014, Neel Natu (neel@freebsd.org)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 
38 #include <machine/vmm.h>
39 
40 #include "vpmtmr.h"
41 
42 /*
43  * The ACPI Power Management timer is a free-running 24- or 32-bit
44  * timer with a frequency of 3.579545MHz
45  *
46  * This implementation will be 32-bits
47  */
48 
49 #define PMTMR_FREQ	3579545  /* 3.579545MHz */
50 
51 struct vpmtmr {
52 	sbintime_t	freq_sbt;
53 	sbintime_t	baseuptime;
54 	uint32_t	baseval;
55 };
56 
57 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
58 
59 struct vpmtmr *
60 vpmtmr_init(struct vm *vm)
61 {
62 	struct vpmtmr *vpmtmr;
63 	struct bintime bt;
64 
65 	vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
66 	vpmtmr->baseuptime = sbinuptime();
67 	vpmtmr->baseval = 0;
68 
69 	FREQ2BT(PMTMR_FREQ, &bt);
70 	vpmtmr->freq_sbt = bttosbt(bt);
71 
72 	return (vpmtmr);
73 }
74 
75 void
76 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
77 {
78 
79 	free(vpmtmr, M_VPMTMR);
80 }
81 
82 int
83 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
84     uint32_t *val)
85 {
86 	struct vpmtmr *vpmtmr;
87 	sbintime_t now, delta;
88 
89 	if (!in || bytes != 4)
90 		return (-1);
91 
92 	vpmtmr = vm_pmtmr(vm);
93 
94 	/*
95 	 * No locking needed because 'baseuptime' and 'baseval' are
96 	 * written only during initialization.
97 	 */
98 	now = sbinuptime();
99 	delta = now - vpmtmr->baseuptime;
100 	KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: "
101 	    "%#lx to %#lx", vpmtmr->baseuptime, now));
102 	*val = vpmtmr->baseval + delta / vpmtmr->freq_sbt;
103 
104 	return (0);
105 }
106