xref: /freebsd/sys/dev/hyperv/vmbus/vmbus.c (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
1 /*-
2  * Copyright (c) 2009-2012,2016-2017 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_extern.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 
52 #include <machine/bus.h>
53 #if defined(__aarch64__)
54 #include <dev/psci/smccc.h>
55 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
56 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
57 #else
58 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h>
59 #include <dev/hyperv/vmbus/x86/hyperv_reg.h>
60 #include <machine/intr_machdep.h>
61 #include <x86/include/apicvar.h>
62 #endif
63 #include <machine/metadata.h>
64 #include <machine/md_var.h>
65 #include <machine/resource.h>
66 #include <contrib/dev/acpica/include/acpi.h>
67 #include <dev/acpica/acpivar.h>
68 
69 #include <dev/hyperv/include/hyperv.h>
70 #include <dev/hyperv/include/vmbus_xact.h>
71 #include <dev/hyperv/vmbus/hyperv_var.h>
72 #include <dev/hyperv/vmbus/vmbus_reg.h>
73 #include <dev/hyperv/vmbus/vmbus_var.h>
74 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
75 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
76 #include "acpi_if.h"
77 #include "pcib_if.h"
78 #include "vmbus_if.h"
79 
80 #define VMBUS_GPADL_START		0xe1e10
81 
82 struct vmbus_msghc {
83 	struct vmbus_xact		*mh_xact;
84 	struct hypercall_postmsg_in	mh_inprm_save;
85 };
86 
87 static void			vmbus_identify(driver_t *, device_t);
88 static int			vmbus_probe(device_t);
89 static int			vmbus_attach(device_t);
90 static int			vmbus_detach(device_t);
91 static int			vmbus_read_ivar(device_t, device_t, int,
92 				    uintptr_t *);
93 static int			vmbus_child_pnpinfo(device_t, device_t, struct sbuf *);
94 static struct resource		*vmbus_alloc_resource(device_t dev,
95 				    device_t child, int type, int *rid,
96 				    rman_res_t start, rman_res_t end,
97 				    rman_res_t count, u_int flags);
98 static int			vmbus_alloc_msi(device_t bus, device_t dev,
99 				    int count, int maxcount, int *irqs);
100 static int			vmbus_release_msi(device_t bus, device_t dev,
101 				    int count, int *irqs);
102 static int			vmbus_alloc_msix(device_t bus, device_t dev,
103 				    int *irq);
104 static int			vmbus_release_msix(device_t bus, device_t dev,
105 				    int irq);
106 static int			vmbus_map_msi(device_t bus, device_t dev,
107 				    int irq, uint64_t *addr, uint32_t *data);
108 static uint32_t			vmbus_get_version_method(device_t, device_t);
109 static int			vmbus_probe_guid_method(device_t, device_t,
110 				    const struct hyperv_guid *);
111 static uint32_t			vmbus_get_vcpu_id_method(device_t bus,
112 				    device_t dev, int cpu);
113 static struct taskqueue		*vmbus_get_eventtq_method(device_t, device_t,
114 				    int);
115 #if defined(EARLY_AP_STARTUP)
116 static void			vmbus_intrhook(void *);
117 #endif
118 
119 static int			vmbus_init(struct vmbus_softc *);
120 static int			vmbus_connect(struct vmbus_softc *, uint32_t);
121 static int			vmbus_req_channels(struct vmbus_softc *sc);
122 static void			vmbus_disconnect(struct vmbus_softc *);
123 static int			vmbus_scan(struct vmbus_softc *);
124 static void			vmbus_scan_teardown(struct vmbus_softc *);
125 static void			vmbus_scan_done(struct vmbus_softc *,
126 				    const struct vmbus_message *);
127 static void			vmbus_chanmsg_handle(struct vmbus_softc *,
128 				    const struct vmbus_message *);
129 static void			vmbus_msg_task(void *, int);
130 static void			vmbus_synic_setup(void *);
131 static void			vmbus_synic_teardown(void *);
132 static int			vmbus_sysctl_version(SYSCTL_HANDLER_ARGS);
133 static int			vmbus_dma_alloc(struct vmbus_softc *);
134 static void			vmbus_dma_free(struct vmbus_softc *);
135 static int			vmbus_intr_setup(struct vmbus_softc *);
136 static void			vmbus_intr_teardown(struct vmbus_softc *);
137 static int			vmbus_doattach(struct vmbus_softc *);
138 static void			vmbus_event_proc_dummy(struct vmbus_softc *,
139 				    int);
140 static bus_dma_tag_t	vmbus_get_dma_tag(device_t parent, device_t child);
141 static struct vmbus_softc	*vmbus_sc;
142 #if defined(__x86_64__)
143 static int vmbus_alloc_cpu_mem(struct vmbus_softc *sc);
144 static void vmbus_free_cpu_mem(struct vmbus_softc *sc);
145 #endif
146 
147 SYSCTL_NODE(_hw, OID_AUTO, vmbus, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
148     "Hyper-V vmbus");
149 
150 static int			vmbus_pin_evttask = 1;
151 SYSCTL_INT(_hw_vmbus, OID_AUTO, pin_evttask, CTLFLAG_RDTUN,
152     &vmbus_pin_evttask, 0, "Pin event tasks to their respective CPU");
153 
154 #if defined(__x86_64__)
155 static int			hv_tlb_hcall = 1;
156 SYSCTL_INT(_hw_vmbus, OID_AUTO, tlb_hcall , CTLFLAG_RDTUN,
157     &hv_tlb_hcall, 0, "Use Hyper_V hyercall for tlb flush");
158 #endif
159 
160 uint32_t			vmbus_current_version;
161 
162 static const uint32_t		vmbus_version[] = {
163 	VMBUS_VERSION_WIN10,
164 	VMBUS_VERSION_WIN8_1,
165 	VMBUS_VERSION_WIN8,
166 	VMBUS_VERSION_WIN7,
167 	VMBUS_VERSION_WS2008
168 };
169 
170 static const vmbus_chanmsg_proc_t
171 vmbus_chanmsg_handlers[VMBUS_CHANMSG_TYPE_MAX] = {
172 	VMBUS_CHANMSG_PROC(CHOFFER_DONE, vmbus_scan_done),
173 	VMBUS_CHANMSG_PROC_WAKEUP(CONNECT_RESP)
174 };
175 
176 static device_method_t vmbus_methods[] = {
177 	/* Device interface */
178 	DEVMETHOD(device_identify,		vmbus_identify),
179 	DEVMETHOD(device_probe,			vmbus_probe),
180 	DEVMETHOD(device_attach,		vmbus_attach),
181 	DEVMETHOD(device_detach,		vmbus_detach),
182 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
183 	DEVMETHOD(device_suspend,		bus_generic_suspend),
184 	DEVMETHOD(device_resume,		bus_generic_resume),
185 
186 	/* Bus interface */
187 	DEVMETHOD(bus_add_child,		bus_generic_add_child),
188 	DEVMETHOD(bus_print_child,		bus_generic_print_child),
189 	DEVMETHOD(bus_read_ivar,		vmbus_read_ivar),
190 	DEVMETHOD(bus_child_pnpinfo,		vmbus_child_pnpinfo),
191 	DEVMETHOD(bus_alloc_resource,		vmbus_alloc_resource),
192 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
193 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
194 	DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
195 	DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
196 	DEVMETHOD(bus_teardown_intr,		bus_generic_teardown_intr),
197 	DEVMETHOD(bus_get_cpus,			bus_generic_get_cpus),
198 	DEVMETHOD(bus_get_dma_tag,		vmbus_get_dma_tag),
199 
200 	/* pcib interface */
201 	DEVMETHOD(pcib_alloc_msi,		vmbus_alloc_msi),
202 	DEVMETHOD(pcib_release_msi,		vmbus_release_msi),
203 	DEVMETHOD(pcib_alloc_msix,		vmbus_alloc_msix),
204 	DEVMETHOD(pcib_release_msix,		vmbus_release_msix),
205 	DEVMETHOD(pcib_map_msi,			vmbus_map_msi),
206 
207 	/* Vmbus interface */
208 	DEVMETHOD(vmbus_get_version,		vmbus_get_version_method),
209 	DEVMETHOD(vmbus_probe_guid,		vmbus_probe_guid_method),
210 	DEVMETHOD(vmbus_get_vcpu_id,		vmbus_get_vcpu_id_method),
211 	DEVMETHOD(vmbus_get_event_taskq,	vmbus_get_eventtq_method),
212 
213 	DEVMETHOD_END
214 };
215 
216 static driver_t vmbus_driver = {
217 	"vmbus",
218 	vmbus_methods,
219 	sizeof(struct vmbus_softc)
220 };
221 
222 uint32_t hv_max_vp_index;
223 
224 DRIVER_MODULE(vmbus, pcib, vmbus_driver, NULL, NULL);
225 DRIVER_MODULE(vmbus, acpi_syscontainer, vmbus_driver, NULL, NULL);
226 
227 MODULE_DEPEND(vmbus, acpi, 1, 1, 1);
228 MODULE_DEPEND(vmbus, pci, 1, 1, 1);
229 MODULE_VERSION(vmbus, 1);
230 
231 static __inline struct vmbus_softc *
232 vmbus_get_softc(void)
233 {
234 	return vmbus_sc;
235 }
236 
237 static bus_dma_tag_t
238 vmbus_get_dma_tag(device_t dev, device_t child)
239 {
240 	struct vmbus_softc *sc = vmbus_get_softc();
241 	return (sc->dmat);
242 }
243 
244 void
245 vmbus_msghc_reset(struct vmbus_msghc *mh, size_t dsize)
246 {
247 	struct hypercall_postmsg_in *inprm;
248 
249 	if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
250 		panic("invalid data size %zu", dsize);
251 
252 	inprm = vmbus_xact_req_data(mh->mh_xact);
253 	memset(inprm, 0, HYPERCALL_POSTMSGIN_SIZE);
254 	inprm->hc_connid = VMBUS_CONNID_MESSAGE;
255 	inprm->hc_msgtype = HYPERV_MSGTYPE_CHANNEL;
256 	inprm->hc_dsize = dsize;
257 }
258 
259 struct vmbus_msghc *
260 vmbus_msghc_get(struct vmbus_softc *sc, size_t dsize)
261 {
262 	struct vmbus_msghc *mh;
263 	struct vmbus_xact *xact;
264 
265 	if (dsize > HYPERCALL_POSTMSGIN_DSIZE_MAX)
266 		panic("invalid data size %zu", dsize);
267 
268 	xact = vmbus_xact_get(sc->vmbus_xc,
269 	    dsize + __offsetof(struct hypercall_postmsg_in, hc_data[0]));
270 	if (xact == NULL)
271 		return (NULL);
272 
273 	mh = vmbus_xact_priv(xact, sizeof(*mh));
274 	mh->mh_xact = xact;
275 
276 	vmbus_msghc_reset(mh, dsize);
277 	return (mh);
278 }
279 
280 void
281 vmbus_msghc_put(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
282 {
283 
284 	vmbus_xact_put(mh->mh_xact);
285 }
286 
287 void *
288 vmbus_msghc_dataptr(struct vmbus_msghc *mh)
289 {
290 	struct hypercall_postmsg_in *inprm;
291 
292 	inprm = vmbus_xact_req_data(mh->mh_xact);
293 	return (inprm->hc_data);
294 }
295 
296 int
297 vmbus_msghc_exec_noresult(struct vmbus_msghc *mh)
298 {
299 	sbintime_t time = SBT_1MS;
300 	struct hypercall_postmsg_in *inprm;
301 	bus_addr_t inprm_paddr;
302 	int i;
303 
304 	inprm = vmbus_xact_req_data(mh->mh_xact);
305 	inprm_paddr = vmbus_xact_req_paddr(mh->mh_xact);
306 
307 	/*
308 	 * Save the input parameter so that we could restore the input
309 	 * parameter if the Hypercall failed.
310 	 *
311 	 * XXX
312 	 * Is this really necessary?!  i.e. Will the Hypercall ever
313 	 * overwrite the input parameter?
314 	 */
315 	memcpy(&mh->mh_inprm_save, inprm, HYPERCALL_POSTMSGIN_SIZE);
316 
317 	/*
318 	 * In order to cope with transient failures, e.g. insufficient
319 	 * resources on host side, we retry the post message Hypercall
320 	 * several times.  20 retries seem sufficient.
321 	 */
322 #define HC_RETRY_MAX	20
323 
324 	for (i = 0; i < HC_RETRY_MAX; ++i) {
325 		uint64_t status;
326 
327 		status = hypercall_post_message(inprm_paddr);
328 		if (status == HYPERCALL_STATUS_SUCCESS)
329 			return 0;
330 
331 		pause_sbt("hcpmsg", time, 0, C_HARDCLOCK);
332 		if (time < SBT_1S * 2)
333 			time *= 2;
334 
335 		/* Restore input parameter and try again */
336 		memcpy(inprm, &mh->mh_inprm_save, HYPERCALL_POSTMSGIN_SIZE);
337 	}
338 
339 #undef HC_RETRY_MAX
340 
341 	return EIO;
342 }
343 
344 int
345 vmbus_msghc_exec(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
346 {
347 	int error;
348 
349 	vmbus_xact_activate(mh->mh_xact);
350 	error = vmbus_msghc_exec_noresult(mh);
351 	if (error)
352 		vmbus_xact_deactivate(mh->mh_xact);
353 	return error;
354 }
355 
356 void
357 vmbus_msghc_exec_cancel(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
358 {
359 
360 	vmbus_xact_deactivate(mh->mh_xact);
361 }
362 
363 const struct vmbus_message *
364 vmbus_msghc_wait_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
365 {
366 	size_t resp_len;
367 
368 	return (vmbus_xact_wait(mh->mh_xact, &resp_len));
369 }
370 
371 const struct vmbus_message *
372 vmbus_msghc_poll_result(struct vmbus_softc *sc __unused, struct vmbus_msghc *mh)
373 {
374 	size_t resp_len;
375 
376 	return (vmbus_xact_poll(mh->mh_xact, &resp_len));
377 }
378 
379 void
380 vmbus_msghc_wakeup(struct vmbus_softc *sc, const struct vmbus_message *msg)
381 {
382 
383 	vmbus_xact_ctx_wakeup(sc->vmbus_xc, msg, sizeof(*msg));
384 }
385 
386 uint32_t
387 vmbus_gpadl_alloc(struct vmbus_softc *sc)
388 {
389 	uint32_t gpadl;
390 
391 again:
392 	gpadl = atomic_fetchadd_int(&sc->vmbus_gpadl, 1);
393 	if (gpadl == 0)
394 		goto again;
395 	return (gpadl);
396 }
397 
398 /* Used for Hyper-V socket when guest client connects to host */
399 int
400 vmbus_req_tl_connect(struct hyperv_guid *guest_srv_id,
401     struct hyperv_guid *host_srv_id)
402 {
403 	struct vmbus_softc *sc = vmbus_get_softc();
404 	struct vmbus_chanmsg_tl_connect *req;
405 	struct vmbus_msghc *mh;
406 	int error;
407 
408 	if (!sc)
409 		return ENXIO;
410 
411 	mh = vmbus_msghc_get(sc, sizeof(*req));
412 	if (mh == NULL) {
413 		device_printf(sc->vmbus_dev,
414 		    "can not get msg hypercall for tl connect\n");
415 		return ENXIO;
416 	}
417 
418 	req = vmbus_msghc_dataptr(mh);
419 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_TL_CONN;
420 	req->guest_endpoint_id = *guest_srv_id;
421 	req->host_service_id = *host_srv_id;
422 
423 	error = vmbus_msghc_exec_noresult(mh);
424 	vmbus_msghc_put(sc, mh);
425 
426 	if (error) {
427 		device_printf(sc->vmbus_dev,
428 		    "tl connect msg hypercall failed\n");
429 	}
430 
431 	return error;
432 }
433 
434 static int
435 vmbus_connect(struct vmbus_softc *sc, uint32_t version)
436 {
437 	struct vmbus_chanmsg_connect *req;
438 	const struct vmbus_message *msg;
439 	struct vmbus_msghc *mh;
440 	int error, done = 0;
441 
442 	mh = vmbus_msghc_get(sc, sizeof(*req));
443 	if (mh == NULL)
444 		return ENXIO;
445 
446 	req = vmbus_msghc_dataptr(mh);
447 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CONNECT;
448 	req->chm_ver = version;
449 	req->chm_evtflags = pmap_kextract((vm_offset_t)sc->vmbus_evtflags);
450 	req->chm_mnf1 = pmap_kextract((vm_offset_t)sc->vmbus_mnf1);
451 	req->chm_mnf2 = pmap_kextract((vm_offset_t)sc->vmbus_mnf2);
452 
453 	error = vmbus_msghc_exec(sc, mh);
454 	if (error) {
455 		vmbus_msghc_put(sc, mh);
456 		return error;
457 	}
458 
459 	msg = vmbus_msghc_wait_result(sc, mh);
460 	done = ((const struct vmbus_chanmsg_connect_resp *)
461 	    msg->msg_data)->chm_done;
462 
463 	vmbus_msghc_put(sc, mh);
464 
465 	return (done ? 0 : EOPNOTSUPP);
466 }
467 
468 static int
469 vmbus_init(struct vmbus_softc *sc)
470 {
471 	int i;
472 
473 	for (i = 0; i < nitems(vmbus_version); ++i) {
474 		int error;
475 
476 		error = vmbus_connect(sc, vmbus_version[i]);
477 		if (!error) {
478 			vmbus_current_version = vmbus_version[i];
479 			sc->vmbus_version = vmbus_version[i];
480 			device_printf(sc->vmbus_dev, "version %u.%u\n",
481 			    VMBUS_VERSION_MAJOR(sc->vmbus_version),
482 			    VMBUS_VERSION_MINOR(sc->vmbus_version));
483 			return 0;
484 		}
485 	}
486 	return ENXIO;
487 }
488 
489 static void
490 vmbus_disconnect(struct vmbus_softc *sc)
491 {
492 	struct vmbus_chanmsg_disconnect *req;
493 	struct vmbus_msghc *mh;
494 	int error;
495 
496 	mh = vmbus_msghc_get(sc, sizeof(*req));
497 	if (mh == NULL) {
498 		device_printf(sc->vmbus_dev,
499 		    "can not get msg hypercall for disconnect\n");
500 		return;
501 	}
502 
503 	req = vmbus_msghc_dataptr(mh);
504 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_DISCONNECT;
505 
506 	error = vmbus_msghc_exec_noresult(mh);
507 	vmbus_msghc_put(sc, mh);
508 
509 	if (error) {
510 		device_printf(sc->vmbus_dev,
511 		    "disconnect msg hypercall failed\n");
512 	}
513 }
514 
515 static int
516 vmbus_req_channels(struct vmbus_softc *sc)
517 {
518 	struct vmbus_chanmsg_chrequest *req;
519 	struct vmbus_msghc *mh;
520 	int error;
521 
522 	mh = vmbus_msghc_get(sc, sizeof(*req));
523 	if (mh == NULL)
524 		return ENXIO;
525 
526 	req = vmbus_msghc_dataptr(mh);
527 	req->chm_hdr.chm_type = VMBUS_CHANMSG_TYPE_CHREQUEST;
528 
529 	error = vmbus_msghc_exec_noresult(mh);
530 	vmbus_msghc_put(sc, mh);
531 
532 	return error;
533 }
534 
535 static void
536 vmbus_scan_done_task(void *xsc, int pending __unused)
537 {
538 	struct vmbus_softc *sc = xsc;
539 
540 	bus_topo_lock();
541 	sc->vmbus_scandone = true;
542 	bus_topo_unlock();
543 	wakeup(&sc->vmbus_scandone);
544 }
545 
546 static void
547 vmbus_scan_done(struct vmbus_softc *sc,
548     const struct vmbus_message *msg __unused)
549 {
550 
551 	taskqueue_enqueue(sc->vmbus_devtq, &sc->vmbus_scandone_task);
552 }
553 
554 static int
555 vmbus_scan(struct vmbus_softc *sc)
556 {
557 	int error;
558 
559 	/*
560 	 * Identify, probe and attach for non-channel devices.
561 	 */
562 	bus_generic_probe(sc->vmbus_dev);
563 	bus_generic_attach(sc->vmbus_dev);
564 
565 	/*
566 	 * This taskqueue serializes vmbus devices' attach and detach
567 	 * for channel offer and rescind messages.
568 	 */
569 	sc->vmbus_devtq = taskqueue_create("vmbus dev", M_WAITOK,
570 	    taskqueue_thread_enqueue, &sc->vmbus_devtq);
571 	taskqueue_start_threads(&sc->vmbus_devtq, 1, PI_NET, "vmbusdev");
572 	TASK_INIT(&sc->vmbus_scandone_task, 0, vmbus_scan_done_task, sc);
573 
574 	/*
575 	 * This taskqueue handles sub-channel detach, so that vmbus
576 	 * device's detach running in vmbus_devtq can drain its sub-
577 	 * channels.
578 	 */
579 	sc->vmbus_subchtq = taskqueue_create("vmbus subch", M_WAITOK,
580 	    taskqueue_thread_enqueue, &sc->vmbus_subchtq);
581 	taskqueue_start_threads(&sc->vmbus_subchtq, 1, PI_NET, "vmbussch");
582 
583 	/*
584 	 * Start vmbus scanning.
585 	 */
586 	error = vmbus_req_channels(sc);
587 	if (error) {
588 		device_printf(sc->vmbus_dev, "channel request failed: %d\n",
589 		    error);
590 		return (error);
591 	}
592 
593 	/*
594 	 * Wait for all vmbus devices from the initial channel offers to be
595 	 * attached.
596 	 */
597 	bus_topo_assert();
598 	while (!sc->vmbus_scandone)
599 		mtx_sleep(&sc->vmbus_scandone, bus_topo_mtx(), 0, "vmbusdev", 0);
600 
601 	if (bootverbose) {
602 		device_printf(sc->vmbus_dev, "device scan, probe and attach "
603 		    "done\n");
604 	}
605 	return (0);
606 }
607 
608 static void
609 vmbus_scan_teardown(struct vmbus_softc *sc)
610 {
611 
612 	bus_topo_assert();
613 	if (sc->vmbus_devtq != NULL) {
614 		bus_topo_unlock();
615 		taskqueue_free(sc->vmbus_devtq);
616 		bus_topo_lock();
617 		sc->vmbus_devtq = NULL;
618 	}
619 	if (sc->vmbus_subchtq != NULL) {
620 		bus_topo_unlock();
621 		taskqueue_free(sc->vmbus_subchtq);
622 		bus_topo_lock();
623 		sc->vmbus_subchtq = NULL;
624 	}
625 }
626 
627 static void
628 vmbus_chanmsg_handle(struct vmbus_softc *sc, const struct vmbus_message *msg)
629 {
630 	vmbus_chanmsg_proc_t msg_proc;
631 	uint32_t msg_type;
632 
633 	msg_type = ((const struct vmbus_chanmsg_hdr *)msg->msg_data)->chm_type;
634 	if (msg_type >= VMBUS_CHANMSG_TYPE_MAX) {
635 		device_printf(sc->vmbus_dev, "unknown message type 0x%x\n",
636 		    msg_type);
637 		return;
638 	}
639 
640 	msg_proc = vmbus_chanmsg_handlers[msg_type];
641 	if (msg_proc != NULL)
642 		msg_proc(sc, msg);
643 
644 	/* Channel specific processing */
645 	vmbus_chan_msgproc(sc, msg);
646 }
647 
648 static void
649 vmbus_msg_task(void *xsc, int pending __unused)
650 {
651 	struct vmbus_softc *sc = xsc;
652 	volatile struct vmbus_message *msg;
653 
654 	msg = VMBUS_PCPU_GET(sc, message, curcpu) + VMBUS_SINT_MESSAGE;
655 	for (;;) {
656 		if (msg->msg_type == HYPERV_MSGTYPE_NONE) {
657 			/* No message */
658 			break;
659 		} else if (msg->msg_type == HYPERV_MSGTYPE_CHANNEL) {
660 			/* Channel message */
661 			vmbus_chanmsg_handle(sc,
662 			    __DEVOLATILE(const struct vmbus_message *, msg));
663 		}
664 
665 		msg->msg_type = HYPERV_MSGTYPE_NONE;
666 		/*
667 		 * Make sure the write to msg_type (i.e. set to
668 		 * HYPERV_MSGTYPE_NONE) happens before we read the
669 		 * msg_flags and EOMing. Otherwise, the EOMing will
670 		 * not deliver any more messages since there is no
671 		 * empty slot
672 		 *
673 		 * NOTE:
674 		 * mb() is used here, since atomic_thread_fence_seq_cst()
675 		 * will become compiler fence on UP kernel.
676 		 */
677 		mb();
678 		if (msg->msg_flags & VMBUS_MSGFLAG_PENDING) {
679 			/*
680 			 * This will cause message queue rescan to possibly
681 			 * deliver another msg from the hypervisor
682 			 */
683 			WRMSR(MSR_HV_EOM, 0);
684 		}
685 	}
686 }
687 static __inline int
688 vmbus_handle_intr1(struct vmbus_softc *sc, struct trapframe *frame, int cpu)
689 {
690 	volatile struct vmbus_message *msg;
691 	struct vmbus_message *msg_base;
692 
693 	msg_base = VMBUS_PCPU_GET(sc, message, cpu);
694 
695 	/*
696 	 * Check event timer.
697 	 *
698 	 * TODO: move this to independent IDT vector.
699 	 */
700 	vmbus_handle_timer_intr1(msg_base, frame);
701 	/*
702 	 * Check events.  Hot path for network and storage I/O data; high rate.
703 	 *
704 	 * NOTE:
705 	 * As recommended by the Windows guest fellows, we check events before
706 	 * checking messages.
707 	 */
708 	sc->vmbus_event_proc(sc, cpu);
709 
710 	/*
711 	 * Check messages.  Mainly management stuffs; ultra low rate.
712 	 */
713 	msg = msg_base + VMBUS_SINT_MESSAGE;
714 	if (__predict_false(msg->msg_type != HYPERV_MSGTYPE_NONE)) {
715 		taskqueue_enqueue(VMBUS_PCPU_GET(sc, message_tq, cpu),
716 		    VMBUS_PCPU_PTR(sc, message_task, cpu));
717 	}
718 
719 	return (FILTER_HANDLED);
720 }
721 
722 void
723 vmbus_handle_intr(struct trapframe *trap_frame)
724 {
725 	struct vmbus_softc *sc = vmbus_get_softc();
726 	int cpu = curcpu;
727 
728 	/*
729 	 * Disable preemption.
730 	 */
731 	critical_enter();
732 
733 	/*
734 	 * Do a little interrupt counting. This used x86 specific
735 	 * intrcnt_add function
736 	 */
737 #if !defined(__aarch64__)
738 	(*VMBUS_PCPU_GET(sc, intr_cnt, cpu))++;
739 #endif /* not for aarch64 */
740 	vmbus_handle_intr1(sc, trap_frame, cpu);
741 
742 	/*
743 	 * Enable preemption.
744 	 */
745 	critical_exit();
746 }
747 
748 static void
749 vmbus_synic_setup(void *xsc)
750 {
751 	struct vmbus_softc *sc = xsc;
752 	int cpu = curcpu;
753 	uint64_t val, orig;
754 	uint32_t sint;
755 
756 	if (hyperv_features & CPUID_HV_MSR_VP_INDEX) {
757 		/* Save virtual processor id. */
758 		VMBUS_PCPU_GET(sc, vcpuid, cpu) = RDMSR(MSR_HV_VP_INDEX);
759 	} else {
760 		/* Set virtual processor id to 0 for compatibility. */
761 		VMBUS_PCPU_GET(sc, vcpuid, cpu) = 0;
762 	}
763 
764 	if (VMBUS_PCPU_GET(sc, vcpuid, cpu) > hv_max_vp_index)
765 		hv_max_vp_index = VMBUS_PCPU_GET(sc, vcpuid, cpu);
766 
767 	/*
768 	 * Setup the SynIC message.
769 	 */
770 	orig = RDMSR(MSR_HV_SIMP);
771 	val = pmap_kextract((vm_offset_t)VMBUS_PCPU_GET(sc, message, cpu)) &
772 	    MSR_HV_SIMP_PGMASK;
773 	val |= MSR_HV_SIMP_ENABLE | (orig & MSR_HV_SIMP_RSVD_MASK);
774 	WRMSR(MSR_HV_SIMP, val);
775 	/*
776 	 * Setup the SynIC event flags.
777 	 */
778 	orig = RDMSR(MSR_HV_SIEFP);
779 	val = pmap_kextract((vm_offset_t)VMBUS_PCPU_GET(sc, event_flags, cpu)) &
780 	    MSR_HV_SIMP_PGMASK;
781 	val |= MSR_HV_SIEFP_ENABLE | (orig & MSR_HV_SIEFP_RSVD_MASK);
782 	WRMSR(MSR_HV_SIEFP, val);
783 
784 	/*
785 	 * Configure and unmask SINT for message and event flags.
786 	 */
787 	sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
788 	orig = RDMSR(sint);
789 	val = sc->vmbus_idtvec | MSR_HV_SINT_AUTOEOI |
790 	    (orig & MSR_HV_SINT_RSVD_MASK);
791 	WRMSR(sint, val);
792 
793 	/*
794 	 * Configure and unmask SINT for timer.
795 	 */
796 	vmbus_synic_setup1(sc);
797 	/*
798 	 * All done; enable SynIC.
799 	 */
800 	orig = RDMSR(MSR_HV_SCONTROL);
801 	val = MSR_HV_SCTRL_ENABLE | (orig & MSR_HV_SCTRL_RSVD_MASK);
802 	WRMSR(MSR_HV_SCONTROL, val);
803 }
804 
805 #if defined(__x86_64__)
806 void
807 hyperv_vm_tlb_flush(pmap_t pmap, vm_offset_t addr1, vm_offset_t addr2,
808     smp_invl_local_cb_t curcpu_cb, enum invl_op_codes op)
809 {
810 	struct vmbus_softc *sc = vmbus_get_softc();
811 	return hv_vm_tlb_flush(pmap, addr1, addr2, op, sc, curcpu_cb);
812 }
813 #endif /*__x86_64__*/
814 
815 static void
816 vmbus_synic_teardown(void *arg)
817 {
818 	uint64_t orig;
819 	uint32_t sint;
820 
821 	/*
822 	 * Disable SynIC.
823 	 */
824 	orig = RDMSR(MSR_HV_SCONTROL);
825 	WRMSR(MSR_HV_SCONTROL, (orig & MSR_HV_SCTRL_RSVD_MASK));
826 
827 	/*
828 	 * Mask message and event flags SINT.
829 	 */
830 	sint = MSR_HV_SINT0 + VMBUS_SINT_MESSAGE;
831 	orig = RDMSR(sint);
832 	WRMSR(sint, orig | MSR_HV_SINT_MASKED);
833 
834 	/*
835 	 * Mask timer SINT.
836 	 */
837 	vmbus_synic_teardown1();
838 	/*
839 	 * Teardown SynIC message.
840 	 */
841 	orig = RDMSR(MSR_HV_SIMP);
842 	WRMSR(MSR_HV_SIMP, (orig & MSR_HV_SIMP_RSVD_MASK));
843 
844 	/*
845 	 * Teardown SynIC event flags.
846 	 */
847 	orig = RDMSR(MSR_HV_SIEFP);
848 	WRMSR(MSR_HV_SIEFP, (orig & MSR_HV_SIEFP_RSVD_MASK));
849 }
850 
851 static int
852 vmbus_dma_alloc(struct vmbus_softc *sc)
853 {
854 	uint8_t *evtflags;
855 	int cpu;
856 
857 	CPU_FOREACH(cpu) {
858 		void *ptr;
859 
860 		/*
861 		 * Per-cpu messages and event flags.
862 		 */
863 		ptr = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO,
864 		    0ul, ~0ul, PAGE_SIZE, 0);
865 		if (ptr == NULL)
866 			return ENOMEM;
867 		VMBUS_PCPU_GET(sc, message, cpu) = ptr;
868 
869 		ptr = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO,
870 		    0ul, ~0ul, PAGE_SIZE, 0);
871 		if (ptr == NULL)
872 			return ENOMEM;
873 		VMBUS_PCPU_GET(sc, event_flags, cpu) = ptr;
874 	}
875 
876 	evtflags = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO,
877 	    0ul, ~0ul, PAGE_SIZE, 0);
878 	if (evtflags == NULL)
879 		return ENOMEM;
880 	sc->vmbus_rx_evtflags = (u_long *)evtflags;
881 	sc->vmbus_tx_evtflags = (u_long *)(evtflags + (PAGE_SIZE / 2));
882 	sc->vmbus_evtflags = evtflags;
883 
884 	sc->vmbus_mnf1 = contigmalloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO,
885 	    0ul, ~0ul, PAGE_SIZE, 0);
886 	if (sc->vmbus_mnf1 == NULL)
887 		return ENOMEM;
888 
889 	sc->vmbus_mnf2 = contigmalloc(sizeof(struct vmbus_mnf), M_DEVBUF,
890 	    M_WAITOK | M_ZERO, 0ul, ~0ul, PAGE_SIZE, 0);
891 	if (sc->vmbus_mnf2 == NULL)
892 		return ENOMEM;
893 
894 	return 0;
895 }
896 
897 static void
898 vmbus_dma_free(struct vmbus_softc *sc)
899 {
900 	int cpu;
901 
902 	if (sc->vmbus_evtflags != NULL) {
903 		free(sc->vmbus_evtflags, M_DEVBUF);
904 		sc->vmbus_evtflags = NULL;
905 		sc->vmbus_rx_evtflags = NULL;
906 		sc->vmbus_tx_evtflags = NULL;
907 	}
908 	if (sc->vmbus_mnf1 != NULL) {
909 		free(sc->vmbus_mnf1, M_DEVBUF);
910 		sc->vmbus_mnf1 = NULL;
911 	}
912 	if (sc->vmbus_mnf2 != NULL) {
913 		free(sc->vmbus_mnf2, M_DEVBUF);
914 		sc->vmbus_mnf2 = NULL;
915 	}
916 
917 	CPU_FOREACH(cpu) {
918 		if (VMBUS_PCPU_GET(sc, message, cpu) != NULL) {
919 			free(VMBUS_PCPU_GET(sc, message, cpu), M_DEVBUF);
920 			VMBUS_PCPU_GET(sc, message, cpu) = NULL;
921 		}
922 		if (VMBUS_PCPU_GET(sc, event_flags, cpu) != NULL) {
923 			free(VMBUS_PCPU_GET(sc, event_flags, cpu), M_DEVBUF);
924 			VMBUS_PCPU_GET(sc, event_flags, cpu) = NULL;
925 		}
926 	}
927 }
928 
929 static int
930 vmbus_intr_setup(struct vmbus_softc *sc)
931 {
932 	int cpu;
933 
934 	CPU_FOREACH(cpu) {
935 		char buf[MAXCOMLEN + 1];
936 		cpuset_t cpu_mask;
937 
938 		/* Allocate an interrupt counter for Hyper-V interrupt */
939 		snprintf(buf, sizeof(buf), "cpu%d:hyperv", cpu);
940 #if !defined(__aarch64__)
941 		intrcnt_add(buf, VMBUS_PCPU_PTR(sc, intr_cnt, cpu));
942 #endif /* not for aarch64 */
943 		/*
944 		 * Setup taskqueue to handle events.  Task will be per-
945 		 * channel.
946 		 */
947 		VMBUS_PCPU_GET(sc, event_tq, cpu) = taskqueue_create_fast(
948 		    "hyperv event", M_WAITOK, taskqueue_thread_enqueue,
949 		    VMBUS_PCPU_PTR(sc, event_tq, cpu));
950 		if (vmbus_pin_evttask) {
951 			CPU_SETOF(cpu, &cpu_mask);
952 			taskqueue_start_threads_cpuset(
953 			    VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET,
954 			    &cpu_mask, "hvevent%d", cpu);
955 		} else {
956 			taskqueue_start_threads(
957 			    VMBUS_PCPU_PTR(sc, event_tq, cpu), 1, PI_NET,
958 			    "hvevent%d", cpu);
959 		}
960 
961 		/*
962 		 * Setup tasks and taskqueues to handle messages.
963 		 */
964 		VMBUS_PCPU_GET(sc, message_tq, cpu) = taskqueue_create_fast(
965 		    "hyperv msg", M_WAITOK, taskqueue_thread_enqueue,
966 		    VMBUS_PCPU_PTR(sc, message_tq, cpu));
967 		CPU_SETOF(cpu, &cpu_mask);
968 		taskqueue_start_threads_cpuset(
969 		    VMBUS_PCPU_PTR(sc, message_tq, cpu), 1, PI_NET, &cpu_mask,
970 		    "hvmsg%d", cpu);
971 		TASK_INIT(VMBUS_PCPU_PTR(sc, message_task, cpu), 0,
972 		    vmbus_msg_task, sc);
973 	}
974 	return (vmbus_setup_intr1(sc));
975 }
976 static void
977 vmbus_intr_teardown(struct vmbus_softc *sc)
978 {
979 	vmbus_intr_teardown1(sc);
980 }
981 
982 static int
983 vmbus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
984 {
985 	return (ENOENT);
986 }
987 
988 static int
989 vmbus_child_pnpinfo(device_t dev, device_t child, struct sbuf *sb)
990 {
991 	const struct vmbus_channel *chan;
992 	char guidbuf[HYPERV_GUID_STRLEN];
993 
994 	chan = vmbus_get_channel(child);
995 	if (chan == NULL) {
996 		/* Event timer device, which does not belong to a channel */
997 		return (0);
998 	}
999 
1000 	hyperv_guid2str(&chan->ch_guid_type, guidbuf, sizeof(guidbuf));
1001 	sbuf_printf(sb, "classid=%s", guidbuf);
1002 
1003 	hyperv_guid2str(&chan->ch_guid_inst, guidbuf, sizeof(guidbuf));
1004 	sbuf_printf(sb, " deviceid=%s", guidbuf);
1005 
1006 	return (0);
1007 }
1008 
1009 int
1010 vmbus_add_child(struct vmbus_channel *chan)
1011 {
1012 	struct vmbus_softc *sc = chan->ch_vmbus;
1013 	device_t parent = sc->vmbus_dev;
1014 
1015 	bus_topo_lock();
1016 
1017 	chan->ch_dev = device_add_child(parent, NULL, DEVICE_UNIT_ANY);
1018 	if (chan->ch_dev == NULL) {
1019 		bus_topo_unlock();
1020 		device_printf(parent, "device_add_child for chan%u failed\n",
1021 		    chan->ch_id);
1022 		return (ENXIO);
1023 	}
1024 	device_set_ivars(chan->ch_dev, chan);
1025 	device_probe_and_attach(chan->ch_dev);
1026 	bus_topo_unlock();
1027 
1028 	return (0);
1029 }
1030 
1031 int
1032 vmbus_delete_child(struct vmbus_channel *chan)
1033 {
1034 	int error = 0;
1035 
1036 	bus_topo_lock();
1037 	if (chan->ch_dev != NULL) {
1038 		error = device_delete_child(chan->ch_vmbus->vmbus_dev,
1039 		    chan->ch_dev);
1040 		chan->ch_dev = NULL;
1041 	}
1042 	bus_topo_unlock();
1043 	return (error);
1044 }
1045 
1046 static int
1047 vmbus_sysctl_version(SYSCTL_HANDLER_ARGS)
1048 {
1049 	struct vmbus_softc *sc = arg1;
1050 	char verstr[16];
1051 
1052 	snprintf(verstr, sizeof(verstr), "%u.%u",
1053 	    VMBUS_VERSION_MAJOR(sc->vmbus_version),
1054 	    VMBUS_VERSION_MINOR(sc->vmbus_version));
1055 	return sysctl_handle_string(oidp, verstr, sizeof(verstr), req);
1056 }
1057 
1058 /*
1059  * We need the function to make sure the MMIO resource is allocated from the
1060  * ranges found in _CRS.
1061  *
1062  * For the release function, we can use bus_generic_release_resource().
1063  */
1064 static struct resource *
1065 vmbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
1066     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1067 {
1068 	device_t parent = device_get_parent(dev);
1069 	struct resource *res;
1070 
1071 	if (type == SYS_RES_MEMORY) {
1072 		struct vmbus_softc *sc = device_get_softc(dev);
1073 
1074 		res = pcib_host_res_alloc(&sc->vmbus_mmio_res, child, type,
1075 		    rid, start, end, count, flags);
1076 	} else {
1077 		res = BUS_ALLOC_RESOURCE(parent, child, type, rid, start,
1078 		    end, count, flags);
1079 	}
1080 
1081 	return (res);
1082 }
1083 
1084 static int
1085 vmbus_alloc_msi(device_t bus, device_t dev, int count, int maxcount, int *irqs)
1086 {
1087 
1088 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1089 	    irqs));
1090 }
1091 
1092 static int
1093 vmbus_release_msi(device_t bus, device_t dev, int count, int *irqs)
1094 {
1095 
1096 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1097 }
1098 
1099 static int
1100 vmbus_alloc_msix(device_t bus, device_t dev, int *irq)
1101 {
1102 
1103 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1104 }
1105 
1106 static int
1107 vmbus_release_msix(device_t bus, device_t dev, int irq)
1108 {
1109 
1110 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1111 }
1112 
1113 static int
1114 vmbus_map_msi(device_t bus, device_t dev, int irq, uint64_t *addr,
1115 	uint32_t *data)
1116 {
1117 
1118 	return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
1119 }
1120 
1121 static uint32_t
1122 vmbus_get_version_method(device_t bus, device_t dev)
1123 {
1124 	struct vmbus_softc *sc = device_get_softc(bus);
1125 
1126 	return sc->vmbus_version;
1127 }
1128 
1129 static int
1130 vmbus_probe_guid_method(device_t bus, device_t dev,
1131     const struct hyperv_guid *guid)
1132 {
1133 	const struct vmbus_channel *chan = vmbus_get_channel(dev);
1134 
1135 	if (memcmp(&chan->ch_guid_type, guid, sizeof(struct hyperv_guid)) == 0)
1136 		return 0;
1137 	return ENXIO;
1138 }
1139 
1140 static uint32_t
1141 vmbus_get_vcpu_id_method(device_t bus, device_t dev, int cpu)
1142 {
1143 	const struct vmbus_softc *sc = device_get_softc(bus);
1144 
1145 	return (VMBUS_PCPU_GET(sc, vcpuid, cpu));
1146 }
1147 
1148 static struct taskqueue *
1149 vmbus_get_eventtq_method(device_t bus, device_t dev __unused, int cpu)
1150 {
1151 	const struct vmbus_softc *sc = device_get_softc(bus);
1152 
1153 	KASSERT(cpu >= 0 && cpu < mp_ncpus, ("invalid cpu%d", cpu));
1154 	return (VMBUS_PCPU_GET(sc, event_tq, cpu));
1155 }
1156 
1157 #define VTPM_BASE_ADDR 0xfed40000
1158 #define FOUR_GB (1ULL << 32)
1159 
1160 enum parse_pass { parse_64, parse_32 };
1161 
1162 struct parse_context {
1163 	device_t vmbus_dev;
1164 	enum parse_pass pass;
1165 };
1166 
1167 static ACPI_STATUS
1168 parse_crs(ACPI_RESOURCE *res, void *ctx)
1169 {
1170 	const struct parse_context *pc = ctx;
1171 	device_t vmbus_dev = pc->vmbus_dev;
1172 
1173 	struct vmbus_softc *sc = device_get_softc(vmbus_dev);
1174 	UINT64 start, end;
1175 
1176 	switch (res->Type) {
1177 	case ACPI_RESOURCE_TYPE_ADDRESS32:
1178 		start = res->Data.Address32.Address.Minimum;
1179 		end = res->Data.Address32.Address.Maximum;
1180 		break;
1181 
1182 	case ACPI_RESOURCE_TYPE_ADDRESS64:
1183 		start = res->Data.Address64.Address.Minimum;
1184 		end = res->Data.Address64.Address.Maximum;
1185 		break;
1186 
1187 	default:
1188 		/* Unused types. */
1189 		return (AE_OK);
1190 	}
1191 
1192 	/*
1193 	 * We don't use <1MB addresses.
1194 	 */
1195 	if (end < 0x100000)
1196 		return (AE_OK);
1197 
1198 	/* Don't conflict with vTPM. */
1199 	if (end >= VTPM_BASE_ADDR && start < VTPM_BASE_ADDR)
1200 		end = VTPM_BASE_ADDR - 1;
1201 
1202 	if ((pc->pass == parse_32 && start < FOUR_GB) ||
1203 	    (pc->pass == parse_64 && start >= FOUR_GB))
1204 		pcib_host_res_decodes(&sc->vmbus_mmio_res, SYS_RES_MEMORY,
1205 		    start, end, 0);
1206 
1207 	return (AE_OK);
1208 }
1209 
1210 static void
1211 vmbus_get_crs(device_t dev, device_t vmbus_dev, enum parse_pass pass)
1212 {
1213 	struct parse_context pc;
1214 	ACPI_STATUS status;
1215 
1216 	if (bootverbose)
1217 		device_printf(dev, "walking _CRS, pass=%d\n", pass);
1218 
1219 	pc.vmbus_dev = vmbus_dev;
1220 	pc.pass = pass;
1221 	status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
1222 			parse_crs, &pc);
1223 
1224 	if (bootverbose && ACPI_FAILURE(status))
1225 		device_printf(dev, "_CRS: not found, pass=%d\n", pass);
1226 }
1227 
1228 static void
1229 vmbus_get_mmio_res_pass(device_t dev, enum parse_pass pass)
1230 {
1231 	device_t acpi0, parent;
1232 
1233 	parent = device_get_parent(dev);
1234 
1235 	acpi0 = device_get_parent(parent);
1236 	if (strcmp("acpi0", device_get_nameunit(acpi0)) == 0) {
1237 		device_t *children;
1238 		int count;
1239 
1240 		/*
1241 		 * Try to locate VMBUS resources and find _CRS on them.
1242 		 */
1243 		if (device_get_children(acpi0, &children, &count) == 0) {
1244 			int i;
1245 
1246 			for (i = 0; i < count; ++i) {
1247 				if (!device_is_attached(children[i]))
1248 					continue;
1249 
1250 				if (strcmp("vmbus_res",
1251 				    device_get_name(children[i])) == 0)
1252 					vmbus_get_crs(children[i], dev, pass);
1253 			}
1254 			free(children, M_TEMP);
1255 		}
1256 
1257 		/*
1258 		 * Try to find _CRS on acpi.
1259 		 */
1260 		vmbus_get_crs(acpi0, dev, pass);
1261 	} else {
1262 		device_printf(dev, "not grandchild of acpi\n");
1263 	}
1264 
1265 	/*
1266 	 * Try to find _CRS on parent.
1267 	 */
1268 	vmbus_get_crs(parent, dev, pass);
1269 }
1270 
1271 static void
1272 vmbus_get_mmio_res(device_t dev)
1273 {
1274 	struct vmbus_softc *sc = device_get_softc(dev);
1275 	/*
1276 	 * We walk the resources twice to make sure that: in the resource
1277 	 * list, the 32-bit resources appear behind the 64-bit resources.
1278 	 * NB: resource_list_add() uses INSERT_TAIL. This way, when we
1279 	 * iterate through the list to find a range for a 64-bit BAR in
1280 	 * vmbus_alloc_resource(), we can make sure we try to use >4GB
1281 	 * ranges first.
1282 	 */
1283 	pcib_host_res_init(dev, &sc->vmbus_mmio_res);
1284 
1285 	vmbus_get_mmio_res_pass(dev, parse_64);
1286 	vmbus_get_mmio_res_pass(dev, parse_32);
1287 }
1288 
1289 /*
1290  * On Gen2 VMs, Hyper-V provides mmio space for framebuffer.
1291  * This mmio address range is not useable for other PCI devices.
1292  * Currently only efifb and vbefb drivers are using this range without
1293  * reserving it from system.
1294  * Therefore, vmbus driver reserves it before any other PCI device
1295  * drivers start to request mmio addresses.
1296  */
1297 static struct resource *hv_fb_res;
1298 
1299 static void
1300 vmbus_fb_mmio_res(device_t dev)
1301 {
1302 	struct efi_fb *efifb;
1303 #if !defined(__aarch64__)
1304 	struct vbe_fb *vbefb;
1305 #endif /* aarch64 */
1306 	rman_res_t fb_start, fb_end, fb_count;
1307 	int fb_height, fb_width;
1308 	caddr_t kmdp;
1309 
1310 	struct vmbus_softc *sc = device_get_softc(dev);
1311 	int rid = 0;
1312 
1313 	kmdp = preload_search_by_type("elf kernel");
1314 	if (kmdp == NULL)
1315 		kmdp = preload_search_by_type("elf64 kernel");
1316 	efifb = (struct efi_fb *)preload_search_info(kmdp,
1317 	    MODINFO_METADATA | MODINFOMD_EFI_FB);
1318 #if !defined(__aarch64__)
1319 	vbefb = (struct vbe_fb *)preload_search_info(kmdp,
1320 	    MODINFO_METADATA | MODINFOMD_VBE_FB);
1321 #endif /* aarch64 */
1322 	if (efifb != NULL) {
1323 		fb_start = efifb->fb_addr;
1324 		fb_end = efifb->fb_addr + efifb->fb_size;
1325 		fb_count = efifb->fb_size;
1326 		fb_height = efifb->fb_height;
1327 		fb_width = efifb->fb_width;
1328 	}
1329 #if !defined(__aarch64__)
1330 	else if (vbefb != NULL) {
1331 		fb_start = vbefb->fb_addr;
1332 		fb_end = vbefb->fb_addr + vbefb->fb_size;
1333 		fb_count = vbefb->fb_size;
1334 		fb_height = vbefb->fb_height;
1335 		fb_width = vbefb->fb_width;
1336 	}
1337 #endif /* aarch64 */
1338 	else {
1339 		if (bootverbose)
1340 			device_printf(dev,
1341 			    "no preloaded kernel fb information\n");
1342 		/* We are on Gen1 VM, just return. */
1343 		return;
1344 	}
1345 
1346 	if (bootverbose)
1347 		device_printf(dev,
1348 		    "fb: fb_addr: %#jx, size: %#jx, "
1349 		    "actual size needed: 0x%x\n",
1350 		    fb_start, fb_count, fb_height * fb_width);
1351 
1352 	hv_fb_res = pcib_host_res_alloc(&sc->vmbus_mmio_res, dev,
1353 	    SYS_RES_MEMORY, &rid, fb_start, fb_end, fb_count,
1354 	    RF_ACTIVE | rman_make_alignment_flags(PAGE_SIZE));
1355 
1356 	if (hv_fb_res && bootverbose)
1357 		device_printf(dev,
1358 		    "successfully reserved memory for framebuffer "
1359 		    "starting at %#jx, size %#jx\n",
1360 		    fb_start, fb_count);
1361 }
1362 
1363 static void
1364 vmbus_free_mmio_res(device_t dev)
1365 {
1366 	struct vmbus_softc *sc = device_get_softc(dev);
1367 
1368 	pcib_host_res_free(dev, &sc->vmbus_mmio_res);
1369 
1370 	if (hv_fb_res)
1371 		hv_fb_res = NULL;
1372 }
1373 
1374 static void
1375 vmbus_identify(driver_t *driver, device_t parent)
1376 {
1377 
1378 	if (device_get_unit(parent) != 0 || vm_guest != VM_GUEST_HV ||
1379 	    (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1380 		return;
1381 	device_add_child(parent, "vmbus", DEVICE_UNIT_ANY);
1382 }
1383 
1384 static int
1385 vmbus_probe(device_t dev)
1386 {
1387 
1388 	if (device_get_unit(dev) != 0 || vm_guest != VM_GUEST_HV ||
1389 	    (hyperv_features & CPUID_HV_MSR_SYNIC) == 0)
1390 		return (ENXIO);
1391 
1392 	device_set_desc(dev, "Hyper-V Vmbus");
1393 	return (BUS_PROBE_DEFAULT);
1394 }
1395 
1396 #if defined(__x86_64__)
1397 static int
1398 vmbus_alloc_cpu_mem(struct vmbus_softc *sc)
1399 {
1400 	int cpu;
1401 
1402 	CPU_FOREACH(cpu) {
1403 		void **hv_cpu_mem;
1404 
1405 		hv_cpu_mem = VMBUS_PCPU_PTR(sc, cpu_mem, cpu);
1406 		*hv_cpu_mem = contigmalloc(PAGE_SIZE, M_DEVBUF,
1407 		    M_NOWAIT | M_ZERO, 0ul, ~0ul, PAGE_SIZE, 0);
1408 
1409 		if (*hv_cpu_mem == NULL)
1410 			return ENOMEM;
1411 	}
1412 
1413 	return 0;
1414 }
1415 
1416 static void
1417 vmbus_free_cpu_mem(struct vmbus_softc *sc)
1418 {
1419 	int cpu;
1420 
1421 	CPU_FOREACH(cpu) {
1422 		void **hv_cpu_mem;
1423 		hv_cpu_mem = VMBUS_PCPU_PTR(sc, cpu_mem, cpu);
1424 		if(*hv_cpu_mem != NULL) {
1425 			free(*hv_cpu_mem, M_DEVBUF);
1426 			*hv_cpu_mem = NULL;
1427 		}
1428 	}
1429 }
1430 #endif
1431 
1432 /**
1433  * @brief Main vmbus driver initialization routine.
1434  *
1435  * Here, we
1436  * - initialize the vmbus driver context
1437  * - setup various driver entry points
1438  * - invoke the vmbus hv main init routine
1439  * - get the irq resource
1440  * - invoke the vmbus to add the vmbus root device
1441  * - setup the vmbus root device
1442  * - retrieve the channel offers
1443  */
1444 static int
1445 vmbus_doattach(struct vmbus_softc *sc)
1446 {
1447 	struct sysctl_oid_list *child;
1448 	struct sysctl_ctx_list *ctx;
1449 	int ret;
1450 	device_t dev_res;
1451 	ACPI_HANDLE handle;
1452 	unsigned int coherent = 0;
1453 
1454 	if (sc->vmbus_flags & VMBUS_FLAG_ATTACHED)
1455 		return (0);
1456 
1457 	vmbus_get_mmio_res(sc->vmbus_dev);
1458 	vmbus_fb_mmio_res(sc->vmbus_dev);
1459 
1460 	sc->vmbus_flags |= VMBUS_FLAG_ATTACHED;
1461 
1462 	sc->vmbus_gpadl = VMBUS_GPADL_START;
1463 	mtx_init(&sc->vmbus_prichan_lock, "vmbus prichan", NULL, MTX_DEF);
1464 	TAILQ_INIT(&sc->vmbus_prichans);
1465 	mtx_init(&sc->vmbus_chan_lock, "vmbus channel", NULL, MTX_DEF);
1466 	TAILQ_INIT(&sc->vmbus_chans);
1467 	sc->vmbus_chmap = malloc(
1468 	    sizeof(struct vmbus_channel *) * VMBUS_CHAN_MAX, M_DEVBUF,
1469 	    M_WAITOK | M_ZERO);
1470 
1471 	/* Coherency attribute */
1472 	dev_res =  devclass_get_device(devclass_find("vmbus_res"), 0);
1473 	if (dev_res != NULL) {
1474 		handle = acpi_get_handle(dev_res);
1475 
1476 		if (ACPI_FAILURE(acpi_GetInteger(handle, "_CCA", &coherent)))
1477 			coherent = 0;
1478 	}
1479 	if (bootverbose)
1480 		device_printf(sc->vmbus_dev, "Bus is%s cache-coherent\n",
1481 			coherent ? "" : " not");
1482 
1483 	bus_dma_tag_create(bus_get_dma_tag(sc->vmbus_dev),
1484 		1, 0,
1485 		BUS_SPACE_MAXADDR,
1486 		BUS_SPACE_MAXADDR,
1487 		NULL, NULL,
1488 		BUS_SPACE_MAXSIZE,
1489 		BUS_SPACE_UNRESTRICTED,
1490 		BUS_SPACE_MAXSIZE,
1491 		coherent ? BUS_DMA_COHERENT : 0,
1492 		NULL, NULL,
1493 		&sc->dmat);
1494 	/*
1495 	 * Create context for "post message" Hypercalls
1496 	 */
1497 	sc->vmbus_xc = vmbus_xact_ctx_create(bus_get_dma_tag(sc->vmbus_dev),
1498 	    HYPERCALL_POSTMSGIN_SIZE, VMBUS_MSG_SIZE,
1499 	    sizeof(struct vmbus_msghc));
1500 	if (sc->vmbus_xc == NULL) {
1501 		ret = ENXIO;
1502 		goto cleanup;
1503 	}
1504 
1505 	/*
1506 	 * Allocate DMA stuffs.
1507 	 */
1508 	ret = vmbus_dma_alloc(sc);
1509 	if (ret != 0)
1510 		goto cleanup;
1511 
1512 	/*
1513 	 * Setup interrupt.
1514 	 */
1515 	ret = vmbus_intr_setup(sc);
1516 	if (ret != 0)
1517 		goto cleanup;
1518 
1519 #if defined(__x86_64__)
1520 	/*
1521 	 * Alloc per cpu memory for tlb flush hypercall
1522 	 */
1523 	if (hv_tlb_hcall) {
1524 		ret = vmbus_alloc_cpu_mem(sc);
1525 		if (ret != 0) {
1526 			hv_tlb_hcall = 0;
1527 			if (bootverbose)
1528 				device_printf(sc->vmbus_dev,
1529 				    "cannot alloc contig memory for "
1530 				    "cpu_mem, use system provided "
1531 				    "tlb flush call.\n");
1532 
1533 			vmbus_free_cpu_mem(sc);
1534 		}
1535 	}
1536 #endif
1537 
1538 	/*
1539 	 * Setup SynIC.
1540 	 */
1541 	if (bootverbose)
1542 		device_printf(sc->vmbus_dev, "smp_started = %d\n", smp_started);
1543 	smp_rendezvous(NULL, vmbus_synic_setup, NULL, sc);
1544 	sc->vmbus_flags |= VMBUS_FLAG_SYNIC;
1545 
1546 #if defined(__x86_64__)
1547 	if (hv_tlb_hcall)
1548 		smp_targeted_tlb_shootdown = &hyperv_vm_tlb_flush;
1549 #endif
1550 
1551 	/*
1552 	 * Initialize vmbus, e.g. connect to Hypervisor.
1553 	 */
1554 	ret = vmbus_init(sc);
1555 	if (ret != 0)
1556 		goto cleanup;
1557 
1558 	if (sc->vmbus_version == VMBUS_VERSION_WS2008 ||
1559 	    sc->vmbus_version == VMBUS_VERSION_WIN7)
1560 		sc->vmbus_event_proc = vmbus_event_proc_compat;
1561 	else
1562 		sc->vmbus_event_proc = vmbus_event_proc;
1563 
1564 	ret = vmbus_scan(sc);
1565 	if (ret != 0)
1566 		goto cleanup;
1567 
1568 	ctx = device_get_sysctl_ctx(sc->vmbus_dev);
1569 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vmbus_dev));
1570 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "version",
1571 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
1572 	    vmbus_sysctl_version, "A", "vmbus version");
1573 
1574 	return (ret);
1575 
1576 cleanup:
1577 	vmbus_scan_teardown(sc);
1578 	vmbus_intr_teardown(sc);
1579 	vmbus_dma_free(sc);
1580 	if (sc->vmbus_xc != NULL) {
1581 		vmbus_xact_ctx_destroy(sc->vmbus_xc);
1582 		sc->vmbus_xc = NULL;
1583 	}
1584 	free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF);
1585 	mtx_destroy(&sc->vmbus_prichan_lock);
1586 	mtx_destroy(&sc->vmbus_chan_lock);
1587 
1588 	return (ret);
1589 }
1590 
1591 static void
1592 vmbus_event_proc_dummy(struct vmbus_softc *sc __unused, int cpu __unused)
1593 {
1594 }
1595 
1596 #if defined(EARLY_AP_STARTUP)
1597 
1598 static void
1599 vmbus_intrhook(void *xsc)
1600 {
1601 	struct vmbus_softc *sc = xsc;
1602 
1603 	if (bootverbose)
1604 		device_printf(sc->vmbus_dev, "intrhook\n");
1605 	vmbus_doattach(sc);
1606 	config_intrhook_disestablish(&sc->vmbus_intrhook);
1607 }
1608 
1609 #endif /* EARLY_AP_STARTUP */
1610 
1611 static int
1612 vmbus_attach(device_t dev)
1613 {
1614 	vmbus_sc = device_get_softc(dev);
1615 	vmbus_sc->vmbus_dev = dev;
1616 	vmbus_sc->vmbus_idtvec = -1;
1617 
1618 	/*
1619 	 * Event processing logic will be configured:
1620 	 * - After the vmbus protocol version negotiation.
1621 	 * - Before we request channel offers.
1622 	 */
1623 	vmbus_sc->vmbus_event_proc = vmbus_event_proc_dummy;
1624 
1625 #if defined(EARLY_AP_STARTUP)
1626 	/*
1627 	 * Defer the real attach until the pause(9) works as expected.
1628 	 */
1629 	vmbus_sc->vmbus_intrhook.ich_func = vmbus_intrhook;
1630 	vmbus_sc->vmbus_intrhook.ich_arg = vmbus_sc;
1631 	config_intrhook_establish(&vmbus_sc->vmbus_intrhook);
1632 #endif /* EARLY_AP_STARTUP  and aarch64 */
1633 
1634 	return (0);
1635 }
1636 
1637 static int
1638 vmbus_detach(device_t dev)
1639 {
1640 	struct vmbus_softc *sc = device_get_softc(dev);
1641 
1642 	bus_generic_detach(dev);
1643 	vmbus_chan_destroy_all(sc);
1644 
1645 	vmbus_scan_teardown(sc);
1646 
1647 	vmbus_disconnect(sc);
1648 
1649 	if (sc->vmbus_flags & VMBUS_FLAG_SYNIC) {
1650 		sc->vmbus_flags &= ~VMBUS_FLAG_SYNIC;
1651 		smp_rendezvous(NULL, vmbus_synic_teardown, NULL, NULL);
1652 	}
1653 
1654 #if defined(__x86_64__)
1655 	/*
1656 	 * Restore the tlb flush to native call
1657 	 */
1658 	if (hv_tlb_hcall) {
1659 		smp_targeted_tlb_shootdown = &smp_targeted_tlb_shootdown_native;
1660 		vmbus_free_cpu_mem(sc);
1661 	}
1662 #endif
1663 
1664 	vmbus_intr_teardown(sc);
1665 	vmbus_dma_free(sc);
1666 
1667 	if (sc->vmbus_xc != NULL) {
1668 		vmbus_xact_ctx_destroy(sc->vmbus_xc);
1669 		sc->vmbus_xc = NULL;
1670 	}
1671 
1672 	free(__DEVOLATILE(void *, sc->vmbus_chmap), M_DEVBUF);
1673 	mtx_destroy(&sc->vmbus_prichan_lock);
1674 	mtx_destroy(&sc->vmbus_chan_lock);
1675 
1676 	vmbus_free_mmio_res(dev);
1677 
1678 #if defined(__aarch64__)
1679 	bus_release_resource(device_get_parent(dev), SYS_RES_IRQ, sc->vector,
1680 	    sc->ires);
1681 #endif
1682 	return (0);
1683 }
1684 
1685 #if !defined(EARLY_AP_STARTUP)
1686 
1687 static void
1688 vmbus_sysinit(void *arg __unused)
1689 {
1690 	struct vmbus_softc *sc = vmbus_get_softc();
1691 
1692 	if (vm_guest != VM_GUEST_HV || sc == NULL)
1693 		return;
1694 
1695 	vmbus_doattach(sc);
1696 }
1697 /*
1698  * NOTE:
1699  * We have to start as the last step of SI_SUB_SMP, i.e. after SMP is
1700  * initialized.
1701  */
1702 SYSINIT(vmbus_initialize, SI_SUB_SMP, SI_ORDER_ANY, vmbus_sysinit, NULL);
1703 #endif	/* !EARLY_AP_STARTUP */
1704