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: releng/11.0/sys/amd64/vmm/io/vpmtmr.c 282287 2015-04-30 22:23:22Z neel $"); 29273683Sneel 30273683Sneel#include <sys/param.h> 31273683Sneel#include <sys/queue.h> 32273683Sneel#include <sys/kernel.h> 33273683Sneel#include <sys/malloc.h> 34273683Sneel#include <sys/systm.h> 35273683Sneel 36273683Sneel#include <machine/vmm.h> 37273683Sneel 38273683Sneel#include "vpmtmr.h" 39273683Sneel 40273683Sneel/* 41273683Sneel * The ACPI Power Management timer is a free-running 24- or 32-bit 42273683Sneel * timer with a frequency of 3.579545MHz 43273683Sneel * 44273683Sneel * This implementation will be 32-bits 45273683Sneel */ 46273683Sneel 47273683Sneel#define PMTMR_FREQ 3579545 /* 3.579545MHz */ 48273683Sneel 49273683Sneelstruct vpmtmr { 50273683Sneel sbintime_t freq_sbt; 51273683Sneel sbintime_t baseuptime; 52273683Sneel uint32_t baseval; 53273683Sneel}; 54273683Sneel 55273683Sneelstatic MALLOC_DEFINE(M_VPMTMR, "vpmtmr", "bhyve virtual acpi timer"); 56273683Sneel 57273683Sneelstruct vpmtmr * 58273683Sneelvpmtmr_init(struct vm *vm) 59273683Sneel{ 60273683Sneel struct vpmtmr *vpmtmr; 61273683Sneel struct bintime bt; 62273683Sneel 63273683Sneel vpmtmr = malloc(sizeof(struct vpmtmr), M_VPMTMR, M_WAITOK | M_ZERO); 64273683Sneel vpmtmr->baseuptime = sbinuptime(); 65273683Sneel vpmtmr->baseval = 0; 66273683Sneel 67273683Sneel FREQ2BT(PMTMR_FREQ, &bt); 68273683Sneel vpmtmr->freq_sbt = bttosbt(bt); 69273683Sneel 70273683Sneel return (vpmtmr); 71273683Sneel} 72273683Sneel 73273683Sneelvoid 74273683Sneelvpmtmr_cleanup(struct vpmtmr *vpmtmr) 75273683Sneel{ 76273683Sneel 77273683Sneel free(vpmtmr, M_VPMTMR); 78273683Sneel} 79273683Sneel 80273683Sneelint 81273706Sneelvpmtmr_handler(struct vm *vm, int vcpuid, bool in, int port, int bytes, 82273683Sneel uint32_t *val) 83273683Sneel{ 84273683Sneel struct vpmtmr *vpmtmr; 85273683Sneel sbintime_t now, delta; 86273683Sneel 87273683Sneel if (!in || bytes != 4) 88273683Sneel return (-1); 89273683Sneel 90273683Sneel vpmtmr = vm_pmtmr(vm); 91273683Sneel 92273683Sneel /* 93273683Sneel * No locking needed because 'baseuptime' and 'baseval' are 94273683Sneel * written only during initialization. 95273683Sneel */ 96273683Sneel now = sbinuptime(); 97273683Sneel delta = now - vpmtmr->baseuptime; 98273683Sneel KASSERT(delta >= 0, ("vpmtmr_handler: uptime went backwards: " 99273683Sneel "%#lx to %#lx", vpmtmr->baseuptime, now)); 100273683Sneel *val = vpmtmr->baseval + delta / vpmtmr->freq_sbt; 101273683Sneel 102273683Sneel return (0); 103273683Sneel} 104