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