vpmtmr.c revision 273683
1273683Sneel/*- 2273683Sneel * Copyright (c) 2014, Neel Natu (neel@freebsd.org) 3273683Sneel * All rights reserved. 4273683Sneel * 5273683Sneel * Redistribution and use in source and binary forms, with or without 6273683Sneel * modification, are permitted provided that the following conditions 7273683Sneel * are met: 8273683Sneel * 1. Redistributions of source code must retain the above copyright 9273683Sneel * notice unmodified, this list of conditions, and the following 10273683Sneel * disclaimer. 11273683Sneel * 2. Redistributions in binary form must reproduce the above copyright 12273683Sneel * notice, this list of conditions and the following disclaimer in the 13273683Sneel * documentation and/or other materials provided with the distribution. 14273683Sneel * 15273683Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16273683Sneel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17273683Sneel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18273683Sneel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19273683Sneel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20273683Sneel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21273683Sneel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22273683Sneel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23273683Sneel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24273683Sneel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25273683Sneel */ 26273683Sneel 27273683Sneel#include <sys/cdefs.h> 28273683Sneel__FBSDID("$FreeBSD: head/sys/amd64/vmm/io/vpmtmr.c 273683 2014-10-26 04:44:28Z neel $"); 29273683Sneel 30273683Sneel#include <sys/param.h> 31273683Sneel#include <sys/queue.h> 32273683Sneel#include <sys/cpuset.h> 33273683Sneel#include <sys/kernel.h> 34273683Sneel#include <sys/malloc.h> 35273683Sneel#include <sys/systm.h> 36273683Sneel 37273683Sneel#include <machine/vmm.h> 38273683Sneel 39273683Sneel#include "vpmtmr.h" 40273683Sneel 41273683Sneel/* 42273683Sneel * The ACPI Power Management timer is a free-running 24- or 32-bit 43273683Sneel * timer with a frequency of 3.579545MHz 44273683Sneel * 45273683Sneel * This implementation will be 32-bits 46273683Sneel */ 47273683Sneel 48273683Sneel#define PMTMR_FREQ 3579545 /* 3.579545MHz */ 49273683Sneel 50273683Sneelstruct vpmtmr { 51273683Sneel sbintime_t freq_sbt; 52273683Sneel sbintime_t baseuptime; 53273683Sneel uint32_t baseval; 54273683Sneel}; 55273683Sneel 56273683Sneelstatic MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer"); 57273683Sneel 58273683Sneelstruct vpmtmr * 59273683Sneelvpmtmr_init(struct vm *vm) 60273683Sneel{ 61273683Sneel struct vpmtmr *vpmtmr; 62273683Sneel struct bintime bt; 63273683Sneel 64273683Sneel vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO); 65273683Sneel vpmtmr->baseuptime = sbinuptime(); 66273683Sneel vpmtmr->baseval = 0; 67273683Sneel 68273683Sneel FREQ2BT(PMTMR_FREQ, &bt); 69273683Sneel vpmtmr->freq_sbt = bttosbt(bt); 70273683Sneel 71273683Sneel return (vpmtmr); 72273683Sneel} 73273683Sneel 74273683Sneelvoid 75273683Sneelvpmtmr_cleanup(struct vpmtmr *vpmtmr) 76273683Sneel{ 77273683Sneel 78273683Sneel free(vpmtmr, M_VPMTMR); 79273683Sneel} 80273683Sneel 81273683Sneelint 82273683Sneelvpmtmr_handler(void *vm, int vcpuid, bool in, int port, int bytes, 83273683Sneel uint32_t *val) 84273683Sneel{ 85273683Sneel struct vpmtmr *vpmtmr; 86273683Sneel sbintime_t now, delta; 87273683Sneel 88273683Sneel if (!in || bytes != 4) 89273683Sneel return (-1); 90273683Sneel 91273683Sneel vpmtmr = vm_pmtmr(vm); 92273683Sneel 93273683Sneel /* 94273683Sneel * No locking needed because 'baseuptime' and 'baseval' are 95273683Sneel * written only during initialization. 96273683Sneel */ 97273683Sneel now = sbinuptime(); 98273683Sneel delta = now - vpmtmr->baseuptime; 99273683Sneel KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: " 100273683Sneel "%#lx to %#lx", vpmtmr->baseuptime, now)); 101273683Sneel *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt; 102273683Sneel 103273683Sneel return (0); 104273683Sneel} 105