xref: /freebsd/sys/dev/hyperv/vmbus/x86/vmbus_x86.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*- SPDX-License-Identifier: BSD-2-Clause
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 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/linker.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sbuf.h>
42 #include <sys/smp.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_param.h>
49 #include <vm/pmap.h>
50 
51 #include <machine/bus.h>
52 #include <machine/metadata.h>
53 #include <machine/md_var.h>
54 #include <machine/resource.h>
55 #include <machine/intr_machdep.h>
56 #include <contrib/dev/acpica/include/acpi.h>
57 #include <dev/acpica/acpivar.h>
58 
59 #include <dev/hyperv/include/hyperv.h>
60 #include <dev/hyperv/include/vmbus_xact.h>
61 #include <dev/hyperv/vmbus/hyperv_var.h>
62 #include <dev/hyperv/vmbus/vmbus_reg.h>
63 #include <dev/hyperv/vmbus/vmbus_var.h>
64 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
65 #include <x86/include/apicvar.h>
66 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h>
67 #include <dev/hyperv/vmbus/x86/hyperv_reg.h>
68 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
69 #include "acpi_if.h"
70 #include "pcib_if.h"
71 #include "vmbus_if.h"
72 
73 extern inthand_t IDTVEC(vmbus_isr), IDTVEC(vmbus_isr_pti);
74 #define VMBUS_ISR_ADDR trunc_page((uintptr_t)IDTVEC(vmbus_isr_pti))
75 
76 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
77     struct trapframe *frame);
78 void vmbus_synic_setup1(void *xsc);
79 void vmbus_synic_teardown1(void);
80 int vmbus_setup_intr1(struct vmbus_softc *sc);
81 void vmbus_intr_teardown1(struct vmbus_softc *sc);
82 
83 void
84 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
85     struct trapframe *frame)
86 {
87 	volatile struct vmbus_message *msg;
88 	msg = msg_base + VMBUS_SINT_TIMER;
89 	if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
90 		msg->msg_type = HYPERV_MSGTYPE_NONE;
91 		vmbus_et_intr(frame);
92 		/*
93 		 * Make sure the write to msg_type (i.e. set to
94 		 * HYPERV_MSGTYPE_NONE) happens before we read the
95 		 * msg_flags and EOMing. Otherwise, the EOMing will
96 		 * not deliver any more messages since there is no
97 		 * empty slot
98 		 *
99 		 * NOTE:
100 		 * mb() is used here, since atomic_thread_fence_seq_cst()
101 		 * will become compiler fence on UP kernel.
102 		 */
103 		mb();
104 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
105 			/*
106 			 * This will cause message queue rescan to possibly
107 			 * deliver another msg from the hypervisor
108 			 */
109 			wrmsr(MSR_HV_EOM, 0);
110 		}
111 	}
112 	return;
113 }
114 
115 void
116 vmbus_synic_setup1(void *xsc)
117 {
118 	struct vmbus_softc *sc = xsc;
119 	uint32_t sint;
120 	uint64_t val, orig;
121 
122 	sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
123 	orig = RDMSR(sint);
124 	val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
125 	    (orig & MSR_HV_SINT_RSVD_MASK);
126 	WRMSR(sint, val);
127 	return;
128 }
129 
130 void
131 vmbus_synic_teardown1(void)
132 {
133 	uint64_t orig;
134 	uint32_t sint;
135 
136 	sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
137 	orig = RDMSR(sint);
138 	WRMSR(sint, orig | MSR_HV_SINT_MASKED);
139 	return;
140 }
141 
142 int
143 vmbus_setup_intr1(struct vmbus_softc *sc)
144 {
145 #if defined(__amd64__) && defined(KLD_MODULE)
146 	pmap_pti_add_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE, true);
147 #endif
148 
149 	/*
150 	 * All Hyper-V ISR required resources are setup, now let's find a
151 	 * free IDT vector for Hyper-V ISR and set it up.
152 	 */
153 	sc->vmbus_idtvec = lapic_ipi_alloc(
154 	    pti ? IDTVEC(vmbus_isr_pti) : IDTVEC(vmbus_isr));
155 	if (sc->vmbus_idtvec < 0) {
156 #if defined(__amd64__) && defined(KLD_MODULE)
157 		pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
158 #endif
159 		device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
160 		return ENXIO;
161 	}
162 	if (bootverbose) {
163 		device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
164 		    sc->vmbus_idtvec);
165 	}
166 	return 0;
167 }
168 
169 void
170 vmbus_intr_teardown1(struct vmbus_softc *sc)
171 {
172 	int cpu;
173 
174 	if (sc->vmbus_idtvec >= 0) {
175 		lapic_ipi_free(sc->vmbus_idtvec);
176 		sc->vmbus_idtvec = -1;
177 	}
178 
179 #if defined(__amd64__) && defined(KLD_MODULE)
180 	pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
181 #endif
182 
183 	CPU_FOREACH(cpu) {
184 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
185 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
186 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
187 		}
188 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
189 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
190 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
191 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
192 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
193 		}
194 	}
195 }
196