1 /*- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 2 * Copyright (c) 2009-2012,2016-2017, 2022 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * VM Bus Driver Implementation 31 */ 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/linker.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/sbuf.h> 44 #include <sys/smp.h> 45 #include <sys/sysctl.h> 46 #include <sys/systm.h> 47 #include <sys/taskqueue.h> 48 49 #include <vm/vm.h> 50 #include <vm/vm_param.h> 51 #include <vm/pmap.h> 52 53 #include <machine/bus.h> 54 #include <machine/metadata.h> 55 #include <machine/md_var.h> 56 #include <machine/resource.h> 57 #include <machine/intr_machdep.h> 58 #include <contrib/dev/acpica/include/acpi.h> 59 #include <dev/acpica/acpivar.h> 60 61 #include <dev/hyperv/include/hyperv.h> 62 #include <dev/hyperv/include/vmbus_xact.h> 63 #include <dev/hyperv/vmbus/hyperv_var.h> 64 #include <dev/hyperv/vmbus/vmbus_reg.h> 65 #include <dev/hyperv/vmbus/vmbus_var.h> 66 #include <dev/hyperv/vmbus/vmbus_chanvar.h> 67 #include <x86/include/apicvar.h> 68 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h> 69 #include <dev/hyperv/vmbus/x86/hyperv_reg.h> 70 #include "acpi_if.h" 71 #include "pcib_if.h" 72 #include "vmbus_if.h" 73 74 extern inthand_t IDTVEC(vmbus_isr), IDTVEC(vmbus_isr_pti); 75 #define VMBUS_ISR_ADDR trunc_page((uintptr_t)IDTVEC(vmbus_isr_pti)) 76 77 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base, 78 struct trapframe *frame); 79 void vmbus_synic_setup1(void *xsc); 80 void vmbus_synic_teardown1(void); 81 int vmbus_setup_intr1(struct vmbus_softc *sc); 82 void vmbus_intr_teardown1(struct vmbus_softc *sc); 83 84 void 85 vmbus_handle_timer_intr1(struct vmbus_message *msg_base, 86 struct trapframe *frame) 87 { 88 volatile struct vmbus_message *msg; 89 msg = msg_base + VMBUS_SINT_TIMER; 90 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) { 91 msg->msg_type = HYPERV_MSGTYPE_NONE; 92 vmbus_et_intr(frame); 93 /* 94 * Make sure the write to msg_type (i.e. set to 95 * HYPERV_MSGTYPE_NONE) happens before we read the 96 * msg_flags and EOMing. Otherwise, the EOMing will 97 * not deliver any more messages since there is no 98 * empty slot 99 * 100 * NOTE: 101 * mb() is used here, since atomic_thread_fence_seq_cst() 102 * will become compiler fence on UP kernel. 103 */ 104 mb(); 105 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) { 106 /* 107 * This will cause message queue rescan to possibly 108 * deliver another msg from the hypervisor 109 */ 110 wrmsr(MSR_HV_EOM, 0); 111 } 112 } 113 return; 114 } 115 116 void 117 vmbus_synic_setup1(void *xsc) 118 { 119 struct vmbus_softc *sc = xsc; 120 uint32_t sint; 121 uint64_t val, orig; 122 123 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER; 124 orig = RDMSR(sint); 125 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI | 126 (orig & MSR_HV_SINT_RSVD_MASK); 127 WRMSR(sint, val); 128 return; 129 } 130 131 void 132 vmbus_synic_teardown1(void) 133 { 134 uint64_t orig; 135 uint32_t sint; 136 137 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER; 138 orig = RDMSR(sint); 139 WRMSR(sint, orig | MSR_HV_SINT_MASKED); 140 return; 141 } 142 143 int 144 vmbus_setup_intr1(struct vmbus_softc *sc) 145 { 146 #if defined(__amd64__) && defined(KLD_MODULE) 147 pmap_pti_add_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE, true); 148 #endif 149 150 /* 151 * All Hyper-V ISR required resources are setup, now let's find a 152 * free IDT vector for Hyper-V ISR and set it up. 153 */ 154 sc->vmbus_idtvec = lapic_ipi_alloc( 155 pti ? IDTVEC(vmbus_isr_pti) : IDTVEC(vmbus_isr)); 156 if (sc->vmbus_idtvec < 0) { 157 #if defined(__amd64__) && defined(KLD_MODULE) 158 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE); 159 #endif 160 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n"); 161 return ENXIO; 162 } 163 if (bootverbose) { 164 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n", 165 sc->vmbus_idtvec); 166 } 167 return 0; 168 } 169 170 void 171 vmbus_intr_teardown1(struct vmbus_softc *sc) 172 { 173 int cpu; 174 175 if (sc->vmbus_idtvec >= 0) { 176 lapic_ipi_free(sc->vmbus_idtvec); 177 sc->vmbus_idtvec = -1; 178 } 179 180 #if defined(__amd64__) && defined(KLD_MODULE) 181 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE); 182 #endif 183 184 CPU_FOREACH(cpu) { 185 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) { 186 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu)); 187 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL; 188 } 189 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) { 190 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu), 191 VMBUS_PCPU_PTR(sc, message_task, cpu)); 192 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu)); 193 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL; 194 } 195 } 196 } 197