vmbus_et.c revision 300994
1293873Ssephe/*- 2298446Ssephe * Copyright (c) 2015,2016 Microsoft Corp. 3293873Ssephe * All rights reserved. 4293873Ssephe * 5293873Ssephe * Redistribution and use in source and binary forms, with or without 6293873Ssephe * modification, are permitted provided that the following conditions 7293873Ssephe * are met: 8293873Ssephe * 1. Redistributions of source code must retain the above copyright 9293873Ssephe * notice, this list of conditions and the following disclaimer. 10293873Ssephe * 2. Redistributions in binary form must reproduce the above copyright 11293873Ssephe * notice, this list of conditions and the following disclaimer in the 12293873Ssephe * documentation and/or other materials provided with the distribution. 13293873Ssephe * 14293873Ssephe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15293873Ssephe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16293873Ssephe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17293873Ssephe * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18293873Ssephe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19293873Ssephe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20293873Ssephe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21293873Ssephe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22293873Ssephe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23293873Ssephe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24293873Ssephe * SUCH DAMAGE. 25293873Ssephe */ 26293873Ssephe 27293873Ssephe#include <sys/cdefs.h> 28293873Ssephe__FBSDID("$FreeBSD: head/sys/dev/hyperv/vmbus/hv_et.c 300994 2016-05-30 09:44:17Z sephe $"); 29293873Ssephe 30293873Ssephe#include <sys/param.h> 31298449Ssephe#include <sys/bus.h> 32298449Ssephe#include <sys/kernel.h> 33298449Ssephe#include <sys/module.h> 34293873Ssephe#include <sys/proc.h> 35293873Ssephe#include <sys/systm.h> 36293873Ssephe#include <sys/smp.h> 37293873Ssephe#include <sys/time.h> 38293873Ssephe#include <sys/timeet.h> 39293873Ssephe 40300827Ssephe#include <dev/hyperv/vmbus/hyperv_reg.h> 41300834Ssephe#include <dev/hyperv/vmbus/hyperv_var.h> 42300988Ssephe#include <dev/hyperv/vmbus/vmbus_var.h> 43293873Ssephe 44300993Ssephe#define VMBUS_ET_NAME "hvet" 45300993Ssephe 46300827Ssephe#define MSR_HV_STIMER0_CFG_SINT \ 47300988Ssephe ((((uint64_t)VMBUS_SINT_TIMER) << MSR_HV_STIMER_CFG_SINT_SHIFT) & \ 48300827Ssephe MSR_HV_STIMER_CFG_SINT_MASK) 49300827Ssephe 50300834Ssephe/* 51300834Ssephe * Two additionally required features: 52300834Ssephe * - SynIC is needed for interrupt generation. 53300834Ssephe * - Time reference counter is needed to set ABS reference count to 54300834Ssephe * STIMER0_COUNT. 55300834Ssephe */ 56300834Ssephe#define CPUID_HV_ET_MASK (CPUID_HV_MSR_TIME_REFCNT | \ 57300834Ssephe CPUID_HV_MSR_SYNIC | \ 58300834Ssephe CPUID_HV_MSR_SYNTIMER) 59300834Ssephe 60300989Ssephestatic struct eventtimer vmbus_et; 61293873Ssephe 62300987Ssephestatic __inline uint64_t 63300993Ssephehyperv_sbintime2count(sbintime_t time) 64293873Ssephe{ 65293873Ssephe struct timespec val; 66293873Ssephe 67293873Ssephe val = sbttots(time); 68300992Ssephe return (val.tv_sec * HYPERV_TIMER_FREQ) + 69300992Ssephe (val.tv_nsec / HYPERV_TIMER_NS_FACTOR); 70293873Ssephe} 71293873Ssephe 72293873Ssephestatic int 73300993Ssephevmbus_et_start(struct eventtimer *et __unused, sbintime_t first, 74300993Ssephe sbintime_t period __unused) 75293873Ssephe{ 76300987Ssephe uint64_t current; 77293873Ssephe 78300827Ssephe current = rdmsr(MSR_HV_TIME_REF_COUNT); 79300993Ssephe current += hyperv_sbintime2count(first); 80300827Ssephe wrmsr(MSR_HV_STIMER0_COUNT, current); 81293873Ssephe 82293873Ssephe return (0); 83293873Ssephe} 84293873Ssephe 85293873Ssephevoid 86300988Ssephevmbus_et_intr(struct trapframe *frame) 87293873Ssephe{ 88293873Ssephe struct trapframe *oldframe; 89293873Ssephe struct thread *td; 90293873Ssephe 91300989Ssephe if (vmbus_et.et_active) { 92293873Ssephe td = curthread; 93293873Ssephe td->td_intr_nesting_level++; 94293873Ssephe oldframe = td->td_intr_frame; 95293873Ssephe td->td_intr_frame = frame; 96300989Ssephe vmbus_et.et_event_cb(&vmbus_et, vmbus_et.et_arg); 97293873Ssephe td->td_intr_frame = oldframe; 98293873Ssephe td->td_intr_nesting_level--; 99293873Ssephe } 100293873Ssephe} 101293873Ssephe 102298449Ssephestatic void 103300993Ssephevmbus_et_identify(driver_t *driver, device_t parent) 104293873Ssephe{ 105300989Ssephe if (device_get_unit(parent) != 0 || 106300993Ssephe device_find_child(parent, VMBUS_ET_NAME, -1) != NULL || 107300834Ssephe (hyperv_features & CPUID_HV_ET_MASK) != CPUID_HV_ET_MASK) 108298449Ssephe return; 109298449Ssephe 110300993Ssephe device_add_child(parent, VMBUS_ET_NAME, -1); 111293873Ssephe} 112293873Ssephe 113298449Ssephestatic int 114300993Ssephevmbus_et_probe(device_t dev) 115298449Ssephe{ 116300994Ssephe if (resource_disabled(VMBUS_ET_NAME, 0)) 117300994Ssephe return (ENXIO); 118300994Ssephe 119298449Ssephe device_set_desc(dev, "Hyper-V event timer"); 120298449Ssephe 121298449Ssephe return (BUS_PROBE_NOWILDCARD); 122298449Ssephe} 123298449Ssephe 124300987Ssephestatic void 125300987Ssephevmbus_et_config(void *arg __unused) 126300987Ssephe{ 127300987Ssephe /* 128300987Ssephe * Make sure that STIMER0 is really disabled before writing 129300987Ssephe * to STIMER0_CONFIG. 130300987Ssephe * 131300987Ssephe * "Writing to the configuration register of a timer that 132300987Ssephe * is already enabled may result in undefined behaviour." 133300987Ssephe */ 134300987Ssephe for (;;) { 135300987Ssephe uint64_t val; 136300987Ssephe 137300987Ssephe /* Stop counting, and this also implies disabling STIMER0 */ 138300987Ssephe wrmsr(MSR_HV_STIMER0_COUNT, 0); 139300987Ssephe 140300987Ssephe val = rdmsr(MSR_HV_STIMER0_CONFIG); 141300987Ssephe if ((val & MSR_HV_STIMER_CFG_ENABLE) == 0) 142300987Ssephe break; 143300987Ssephe cpu_spinwait(); 144300987Ssephe } 145300987Ssephe wrmsr(MSR_HV_STIMER0_CONFIG, 146300987Ssephe MSR_HV_STIMER_CFG_AUTOEN | MSR_HV_STIMER0_CFG_SINT); 147300987Ssephe} 148300987Ssephe 149298449Ssephestatic int 150300993Ssephevmbus_et_attach(device_t dev) 151298449Ssephe{ 152300989Ssephe /* TODO: use independent IDT vector */ 153298449Ssephe 154300989Ssephe vmbus_et.et_name = "Hyper-V"; 155300989Ssephe vmbus_et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU; 156300989Ssephe vmbus_et.et_quality = 1000; 157300992Ssephe vmbus_et.et_frequency = HYPERV_TIMER_FREQ; 158300992Ssephe vmbus_et.et_min_period = (0x00000001ULL << 32) / HYPERV_TIMER_FREQ; 159300992Ssephe vmbus_et.et_max_period = (0xfffffffeULL << 32) / HYPERV_TIMER_FREQ; 160300993Ssephe vmbus_et.et_start = vmbus_et_start; 161298449Ssephe 162300987Ssephe /* 163300987Ssephe * Delay a bit to make sure that MSR_HV_TIME_REF_COUNT will 164300987Ssephe * not return 0, since writing 0 to STIMER0_COUNT will disable 165300987Ssephe * STIMER0. 166300987Ssephe */ 167300987Ssephe DELAY(100); 168300987Ssephe smp_rendezvous(NULL, vmbus_et_config, NULL, NULL); 169300987Ssephe 170300989Ssephe return (et_register(&vmbus_et)); 171298449Ssephe} 172298449Ssephe 173298449Ssephestatic int 174300993Ssephevmbus_et_detach(device_t dev) 175298449Ssephe{ 176300989Ssephe return (et_deregister(&vmbus_et)); 177298449Ssephe} 178298449Ssephe 179300993Ssephestatic device_method_t vmbus_et_methods[] = { 180300993Ssephe DEVMETHOD(device_identify, vmbus_et_identify), 181300993Ssephe DEVMETHOD(device_probe, vmbus_et_probe), 182300993Ssephe DEVMETHOD(device_attach, vmbus_et_attach), 183300993Ssephe DEVMETHOD(device_detach, vmbus_et_detach), 184298449Ssephe 185298449Ssephe DEVMETHOD_END 186298449Ssephe}; 187298449Ssephe 188300993Ssephestatic driver_t vmbus_et_driver = { 189300993Ssephe VMBUS_ET_NAME, 190300993Ssephe vmbus_et_methods, 191300989Ssephe 0 192298449Ssephe}; 193298449Ssephe 194300993Ssephestatic devclass_t vmbus_et_devclass; 195300993SsepheDRIVER_MODULE(hv_et, vmbus, vmbus_et_driver, vmbus_et_devclass, NULL, NULL); 196298449SsepheMODULE_VERSION(hv_et, 1); 197