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