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 <dev/hyperv/vmbus/hyperv_common_reg.h>
71 #include "acpi_if.h"
72 #include "pcib_if.h"
73 #include "vmbus_if.h"
74
75 extern inthand_t IDTVEC(vmbus_isr), IDTVEC(vmbus_isr_pti);
76 #define VMBUS_ISR_ADDR trunc_page((uintptr_t)IDTVEC(vmbus_isr_pti))
77
78 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
79 struct trapframe *frame);
80 void vmbus_synic_setup1(void *xsc);
81 void vmbus_synic_teardown1(void);
82 int vmbus_setup_intr1(struct vmbus_softc *sc);
83 void vmbus_intr_teardown1(struct vmbus_softc *sc);
84
85 void
86 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
87 struct trapframe *frame)
88 {
89 volatile struct vmbus_message *msg;
90 msg = msg_base + VMBUS_SINT_TIMER;
91 if (msg->msg_type == HYPERV_MSGTYPE_TIMER_EXPIRED) {
92 msg->msg_type = HYPERV_MSGTYPE_NONE;
93 vmbus_et_intr(frame);
94 /*
95 * Make sure the write to msg_type (i.e. set to
96 * HYPERV_MSGTYPE_NONE) happens before we read the
97 * msg_flags and EOMing. Otherwise, the EOMing will
98 * not deliver any more messages since there is no
99 * empty slot
100 *
101 * NOTE:
102 * mb() is used here, since atomic_thread_fence_seq_cst()
103 * will become compiler fence on UP kernel.
104 */
105 mb();
106 if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
107 /*
108 * This will cause message queue rescan to possibly
109 * deliver another msg from the hypervisor
110 */
111 wrmsr(MSR_HV_EOM, 0);
112 }
113 }
114 return;
115 }
116
117 void
118 vmbus_synic_setup1(void *xsc)
119 {
120 struct vmbus_softc *sc = xsc;
121 uint32_t sint;
122 uint64_t val, orig;
123
124 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
125 orig = RDMSR(sint);
126 val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
127 (orig & MSR_HV_SINT_RSVD_MASK);
128 WRMSR(sint, val);
129 return;
130 }
131
132 void
133 vmbus_synic_teardown1(void)
134 {
135 uint64_t orig;
136 uint32_t sint;
137
138 sint = MSR_HV_SINT0 + VMBUS_SINT_TIMER;
139 orig = RDMSR(sint);
140 WRMSR(sint, orig | MSR_HV_SINT_MASKED);
141 return;
142 }
143
144 int
145 vmbus_setup_intr1(struct vmbus_softc *sc)
146 {
147 #if defined(__amd64__) && defined(KLD_MODULE)
148 pmap_pti_add_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE, true);
149 #endif
150
151 /*
152 * All Hyper-V ISR required resources are setup, now let's find a
153 * free IDT vector for Hyper-V ISR and set it up.
154 */
155 sc->vmbus_idtvec = lapic_ipi_alloc(
156 pti ? IDTVEC(vmbus_isr_pti) : IDTVEC(vmbus_isr));
157 if (sc->vmbus_idtvec < 0) {
158 #if defined(__amd64__) && defined(KLD_MODULE)
159 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
160 #endif
161 device_printf(sc->vmbus_dev, "cannot find free IDT vector\n");
162 return ENXIO;
163 }
164 if (bootverbose) {
165 device_printf(sc->vmbus_dev, "vmbus IDT vector %d\n",
166 sc->vmbus_idtvec);
167 }
168 return 0;
169 }
170
171 void
172 vmbus_intr_teardown1(struct vmbus_softc *sc)
173 {
174 int cpu;
175
176 if (sc->vmbus_idtvec >= 0) {
177 lapic_ipi_free(sc->vmbus_idtvec);
178 sc->vmbus_idtvec = -1;
179 }
180
181 #if defined(__amd64__) && defined(KLD_MODULE)
182 pmap_pti_remove_kva(VMBUS_ISR_ADDR, VMBUS_ISR_ADDR + PAGE_SIZE);
183 #endif
184
185 CPU_FOREACH(cpu) {
186 if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
187 taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
188 VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
189 }
190 if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
191 taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
192 VMBUS_PCPU_PTR(sc, message_task, cpu));
193 taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
194 VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
195 }
196 }
197 }
Cache object: 0c6527e9301067ead923d455f6e8976f
|