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