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 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/sbuf.h>
45 #include <sys/smp.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/pmap.h>
53 
54 #include <machine/bus.h>
55 #include <machine/metadata.h>
56 #include <machine/md_var.h>
57 #include <machine/resource.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 <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
68 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
69 #include "acpi_if.h"
70 #include "pcib_if.h"
71 #include "vmbus_if.h"
72 
73 static int vmbus_handle_intr_new(void *);
74 
75 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
76     struct trapframe *frame);
77 void vmbus_synic_setup1(void *xsc);
78 void vmbus_synic_teardown1(void);
79 int vmbus_setup_intr1(struct vmbus_softc *sc);
80 void vmbus_intr_teardown1(struct vmbus_softc *sc);
81 
82 void
83 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
84     struct trapframe *frame)
85 {
86 	// do nothing for arm64, as we are using generic timer
87 	return;
88 }
89 
90 static int
91 vmbus_handle_intr_new(void *arg)
92 {
93 	vmbus_handle_intr(NULL);
94 	return (FILTER_HANDLED);
95 }
96 
97 void
98 vmbus_synic_setup1(void *xsc)
99 {
100 	return;
101 }
102 
103 void
104 vmbus_synic_teardown1(void)
105 {
106 	return;
107 }
108 
109 int
110 vmbus_setup_intr1(struct vmbus_softc *sc)
111 {
112 	int err;
113 	struct intr_map_data_acpi *irq_data;
114 
115 	sc->ires = bus_alloc_resource_any(device_get_parent(sc->vmbus_dev),
116 	    SYS_RES_IRQ, &sc->vector, RF_ACTIVE | RF_SHAREABLE);
117 	if (sc->ires == NULL) {
118 		device_printf(sc->vmbus_dev, "bus_alloc_resouce_any failed\n");
119 		return (ENXIO);
120 	} else {
121 		device_printf(sc->vmbus_dev, "irq 0x%lx, vector %d end 0x%lx\n",
122 		    (uint64_t)rman_get_start(sc->ires), sc->vector,
123 		    (uint64_t)rman_get_end(sc->ires));
124 	}
125 	err = bus_setup_intr(sc->vmbus_dev, sc->ires, INTR_TYPE_MISC,
126 	    vmbus_handle_intr_new, NULL, sc, &sc->icookie);
127 	if (err) {
128 		device_printf(sc->vmbus_dev, "failed to setup IRQ %d\n", err);
129 		return (err);
130 	}
131 	irq_data = (struct intr_map_data_acpi *)rman_get_virtual(sc->ires);
132 	device_printf(sc->vmbus_dev, "the irq %u\n", irq_data->irq);
133 	sc->vmbus_idtvec = irq_data->irq;
134 	return 0;
135 }
136 
137 void
138 vmbus_intr_teardown1(struct vmbus_softc *sc)
139 {
140 	int cpu;
141 
142 	sc->vmbus_idtvec = -1;
143 	bus_teardown_intr(sc->vmbus_dev, sc->ires, sc->icookie);
144 
145 	CPU_FOREACH(cpu) {
146 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
147 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
148 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
149 		}
150 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
151 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
152 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
153 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
154 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
155 		}
156 	}
157 }
158 /*- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
159  * Copyright (c) 2009-2012,2016-2017, 2022 Microsoft Corp.
160  * Copyright (c) 2012 NetApp Inc.
161  * Copyright (c) 2012 Citrix Inc.
162  * All rights reserved.
163  *
164  * Redistribution and use in source and binary forms, with or without
165  * modification, are permitted provided that the following conditions
166  * are met:
167  * 1. Redistributions of source code must retain the above copyright
168  *    notice unmodified, this list of conditions, and the following
169  *    disclaimer.
170  * 2. Redistributions in binary form must reproduce the above copyright
171  *    notice, this list of conditions and the following disclaimer in the
172  *    documentation and/or other materials provided with the distribution.
173  *
174  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
178  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
179  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
180  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
181  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
182  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
183  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184  */
185 
186 /*
187  * VM Bus Driver Implementation
188  */
189 
190 #include <sys/cdefs.h>
191 __FBSDID("$FreeBSD$");
192 
193 #include <sys/param.h>
194 #include <sys/bus.h>
195 #include <sys/kernel.h>
196 #include <sys/linker.h>
197 #include <sys/lock.h>
198 #include <sys/malloc.h>
199 #include <sys/module.h>
200 #include <sys/mutex.h>
201 #include <sys/sbuf.h>
202 #include <sys/smp.h>
203 #include <sys/sysctl.h>
204 #include <sys/systm.h>
205 #include <sys/taskqueue.h>
206 
207 #include <vm/vm.h>
208 #include <vm/vm_param.h>
209 #include <vm/pmap.h>
210 
211 #include <machine/bus.h>
212 #include <machine/metadata.h>
213 #include <machine/md_var.h>
214 #include <machine/resource.h>
215 #include <contrib/dev/acpica/include/acpi.h>
216 #include <dev/acpica/acpivar.h>
217 
218 #include <dev/hyperv/include/hyperv.h>
219 #include <dev/hyperv/include/vmbus_xact.h>
220 #include <dev/hyperv/vmbus/hyperv_var.h>
221 #include <dev/hyperv/vmbus/vmbus_reg.h>
222 #include <dev/hyperv/vmbus/vmbus_var.h>
223 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
224 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
225 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
226 #include "acpi_if.h"
227 #include "pcib_if.h"
228 #include "vmbus_if.h"
229 
230 static int vmbus_handle_intr_new(void *);
231 
232 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
233     struct trapframe *frame);
234 void vmbus_synic_setup1(void *xsc);
235 void vmbus_synic_teardown1(void);
236 int vmbus_setup_intr1(struct vmbus_softc *sc);
237 void vmbus_intr_teardown1(struct vmbus_softc *sc);
238 
239 void
240 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
241     struct trapframe *frame)
242 {
243 	// do nothing for arm64, as we are using generic timer
244 	return;
245 }
246 
247 static int
248 vmbus_handle_intr_new(void *arg)
249 {
250 	vmbus_handle_intr(NULL);
251 	return (FILTER_HANDLED);
252 }
253 
254 void
255 vmbus_synic_setup1(void *xsc)
256 {
257 	return;
258 }
259 
260 void
261 vmbus_synic_teardown1(void)
262 {
263 	return;
264 }
265 
266 int
267 vmbus_setup_intr1(struct vmbus_softc *sc)
268 {
269 	int err;
270 	struct intr_map_data_acpi *irq_data;
271 
272 	sc->ires = bus_alloc_resource_any(device_get_parent(sc->vmbus_dev),
273 	    SYS_RES_IRQ, &sc->vector, RF_ACTIVE | RF_SHAREABLE);
274 	if (sc->ires == NULL) {
275 		device_printf(sc->vmbus_dev, "bus_alloc_resouce_any failed\n");
276 		return (ENXIO);
277 	} else {
278 		device_printf(sc->vmbus_dev, "irq 0x%lx, vector %d end 0x%lx\n",
279 		    (uint64_t)rman_get_start(sc->ires), sc->vector,
280 		    (uint64_t)rman_get_end(sc->ires));
281 	}
282 	err = bus_setup_intr(sc->vmbus_dev, sc->ires, INTR_TYPE_MISC,
283 	    vmbus_handle_intr_new, NULL, sc, &sc->icookie);
284 	if (err) {
285 		device_printf(sc->vmbus_dev, "failed to setup IRQ %d\n", err);
286 		return (err);
287 	}
288 	irq_data = (struct intr_map_data_acpi *)rman_get_virtual(sc->ires);
289 	device_printf(sc->vmbus_dev, "the irq %u\n", irq_data->irq);
290 	sc->vmbus_idtvec = irq_data->irq;
291 	return 0;
292 }
293 
294 void
295 vmbus_intr_teardown1(struct vmbus_softc *sc)
296 {
297 	int cpu;
298 
299 	sc->vmbus_idtvec = -1;
300 	bus_teardown_intr(sc->vmbus_dev, sc->ires, sc->icookie);
301 
302 	CPU_FOREACH(cpu) {
303 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
304 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
305 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
306 		}
307 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
308 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
309 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
310 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
311 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
312 		}
313 	}
314 }
315