1 /*- SPDX-License-Identifier: BSD-2-Clause
2  * Copyright (c) 2009-2012,2016-2017, 2022 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * VM Bus Driver Implementation
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/sbuf.h>
45 #include <sys/smp.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
49 
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52 #include <vm/pmap.h>
53 
54 #include <machine/bus.h>
55 #include <machine/metadata.h>
56 #include <machine/md_var.h>
57 #include <machine/resource.h>
58 #include <contrib/dev/acpica/include/acpi.h>
59 #include <dev/acpica/acpivar.h>
60 
61 #include <dev/hyperv/include/hyperv.h>
62 #include <dev/hyperv/include/vmbus_xact.h>
63 #include <dev/hyperv/vmbus/hyperv_var.h>
64 #include <dev/hyperv/vmbus/vmbus_reg.h>
65 #include <dev/hyperv/vmbus/vmbus_var.h>
66 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
67 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
68 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
69 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
70 #include "acpi_if.h"
71 #include "pcib_if.h"
72 #include "vmbus_if.h"
73 
74 static int vmbus_handle_intr_new(void *);
75 
76 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
77     struct trapframe *frame);
78 void vmbus_synic_setup1(void *xsc);
79 void vmbus_synic_teardown1(void);
80 int vmbus_setup_intr1(struct vmbus_softc *sc);
81 void vmbus_intr_teardown1(struct vmbus_softc *sc);
82 
83 void
84 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
85     struct trapframe *frame)
86 {
87 	// do nothing for arm64, as we are using generic timer
88 	return;
89 }
90 
91 static int
92 vmbus_handle_intr_new(void *arg)
93 {
94 	vmbus_handle_intr(NULL);
95 	return (FILTER_HANDLED);
96 }
97 
98 void
99 vmbus_synic_setup1(void *xsc)
100 {
101 	return;
102 }
103 
104 void
105 vmbus_synic_teardown1(void)
106 {
107 	return;
108 }
109 
110 int
111 vmbus_setup_intr1(struct vmbus_softc *sc)
112 {
113 	int err;
114 	struct intr_map_data_acpi *irq_data;
115 	device_t dev;
116 
117 	dev =  devclass_get_device(devclass_find("vmbus_res"), 0);
118 	sc->ires = bus_alloc_resource_any(dev,
119 	    SYS_RES_IRQ, &sc->vector, RF_ACTIVE | RF_SHAREABLE);
120 	if (sc->ires == NULL) {
121 		device_printf(sc->vmbus_dev, "bus_alloc_resouce_any failed\n");
122 		return (ENXIO);
123 	} else {
124 		device_printf(sc->vmbus_dev, "irq 0x%lx, vector %d end 0x%lx\n",
125 		    (uint64_t)rman_get_start(sc->ires), sc->vector,
126 		    (uint64_t)rman_get_end(sc->ires));
127 	}
128 	err = bus_setup_intr(sc->vmbus_dev, sc->ires, INTR_TYPE_MISC | INTR_MPSAFE,
129 	    vmbus_handle_intr_new, NULL, sc, &sc->icookie);
130 	if (err) {
131 		device_printf(sc->vmbus_dev, "failed to setup IRQ %d\n", err);
132 		return (err);
133 	}
134 	irq_data = (struct intr_map_data_acpi *)rman_get_virtual(sc->ires);
135 	device_printf(sc->vmbus_dev, "the irq %u\n", irq_data->irq);
136 	sc->vmbus_idtvec = irq_data->irq;
137 	return 0;
138 }
139 
140 void
141 vmbus_intr_teardown1(struct vmbus_softc *sc)
142 {
143 	int cpu;
144 
145 	sc->vmbus_idtvec = -1;
146 	bus_teardown_intr(sc->vmbus_dev, sc->ires, sc->icookie);
147 
148 	CPU_FOREACH(cpu) {
149 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
150 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
151 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
152 		}
153 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
154 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
155 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
156 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
157 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
158 		}
159 	}
160 }
161