xref: /freebsd/sys/amd64/vmm/io/vpmtmr.c (revision a812392203d7c4c3f0db9d8a0f3391374c49c71f)
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/cpuset.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
36 
37 #include <machine/vmm.h>
38 
39 #include "vpmtmr.h"
40 
41 /*
42  * The ACPI Power Management timer is a free-running 24- or 32-bit
43  * timer with a frequency of 3.579545MHz
44  *
45  * This implementation will be 32-bits
46  */
47 
48 #define PMTMR_FREQ	3579545  /* 3.579545MHz */
49 
50 struct vpmtmr {
51 	sbintime_t	freq_sbt;
52 	sbintime_t	baseuptime;
53 	uint32_t	baseval;
54 };
55 
56 static MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer");
57 
58 struct vpmtmr *
59 vpmtmr_init(struct vm *vm)
60 {
61 	struct vpmtmr *vpmtmr;
62 	struct bintime bt;
63 
64 	vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO);
65 	vpmtmr->baseuptime = sbinuptime();
66 	vpmtmr->baseval = 0;
67 
68 	FREQ2BT(PMTMR_FREQ, &bt);
69 	vpmtmr->freq_sbt = bttosbt(bt);
70 
71 	return (vpmtmr);
72 }
73 
74 void
75 vpmtmr_cleanup(struct vpmtmr *vpmtmr)
76 {
77 
78 	free(vpmtmr, M_VPMTMR);
79 }
80 
81 int
82 vpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes,
83     uint32_t *val)
84 {
85 	struct vpmtmr *vpmtmr;
86 	sbintime_t now, delta;
87 
88 	if (!in || bytes != 4)
89 		return (-1);
90 
91 	vpmtmr = vm_pmtmr(vm);
92 
93 	/*
94 	 * No locking needed because 'baseuptime' and 'baseval' are
95 	 * written only during initialization.
96 	 */
97 	now = sbinuptime();
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;
102 
103 	return (0);
104 }
105