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/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_param.h>
49 #include <vm/pmap.h>
50 
51 #include <machine/bus.h>
52 #include <machine/metadata.h>
53 #include <machine/md_var.h>
54 #include <machine/resource.h>
55 #include <contrib/dev/acpica/include/acpi.h>
56 #include <dev/acpica/acpivar.h>
57 
58 #include <dev/hyperv/include/hyperv.h>
59 #include <dev/hyperv/include/vmbus_xact.h>
60 #include <dev/hyperv/vmbus/hyperv_var.h>
61 #include <dev/hyperv/vmbus/vmbus_reg.h>
62 #include <dev/hyperv/vmbus/vmbus_var.h>
63 #include <dev/hyperv/vmbus/vmbus_chanvar.h>
64 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
65 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
66 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
67 #include "acpi_if.h"
68 #include "pcib_if.h"
69 #include "vmbus_if.h"
70 
71 static int vmbus_handle_intr_new(void *);
72 
73 void vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
74     struct trapframe *frame);
75 void vmbus_synic_setup1(void *xsc);
76 void vmbus_synic_teardown1(void);
77 int vmbus_setup_intr1(struct vmbus_softc *sc);
78 void vmbus_intr_teardown1(struct vmbus_softc *sc);
79 
80 void
81 vmbus_handle_timer_intr1(struct vmbus_message *msg_base,
82     struct trapframe *frame)
83 {
84 	// do nothing for arm64, as we are using generic timer
85 	return;
86 }
87 
88 static int
89 vmbus_handle_intr_new(void *arg)
90 {
91 	vmbus_handle_intr(NULL);
92 	return (FILTER_HANDLED);
93 }
94 
95 void
96 vmbus_synic_setup1(void *xsc)
97 {
98 	return;
99 }
100 
101 void
102 vmbus_synic_teardown1(void)
103 {
104 	return;
105 }
106 
107 int
108 vmbus_setup_intr1(struct vmbus_softc *sc)
109 {
110 	int err;
111 	struct intr_map_data_acpi *irq_data;
112 	device_t dev;
113 
114 	dev =  devclass_get_device(devclass_find("vmbus_res"), 0);
115 	sc->ires = bus_alloc_resource_any(dev,
116 	    SYS_RES_IRQ, &sc->vector, RF_ACTIVE | RF_SHAREABLE);
117 	if (sc->ires == NULL) {
118 		device_printf(sc->vmbus_dev, "bus_alloc_resouce_any failed\n");
119 		return (ENXIO);
120 	} else {
121 		device_printf(sc->vmbus_dev, "irq 0x%lx, vector %d end 0x%lx\n",
122 		    (uint64_t)rman_get_start(sc->ires), sc->vector,
123 		    (uint64_t)rman_get_end(sc->ires));
124 	}
125 	err = bus_setup_intr(sc->vmbus_dev, sc->ires, INTR_TYPE_MISC | INTR_MPSAFE,
126 	    vmbus_handle_intr_new, NULL, sc, &sc->icookie);
127 	if (err) {
128 		device_printf(sc->vmbus_dev, "failed to setup IRQ %d\n", err);
129 		return (err);
130 	}
131 	irq_data = (struct intr_map_data_acpi *)rman_get_virtual(sc->ires);
132 	device_printf(sc->vmbus_dev, "the irq %u\n", irq_data->irq);
133 	sc->vmbus_idtvec = irq_data->irq;
134 	return 0;
135 }
136 
137 void
138 vmbus_intr_teardown1(struct vmbus_softc *sc)
139 {
140 	int cpu;
141 
142 	sc->vmbus_idtvec = -1;
143 	bus_teardown_intr(sc->vmbus_dev, sc->ires, sc->icookie);
144 
145 	CPU_FOREACH(cpu) {
146 		if (VMBUS_PCPU_GET(sc, event_tq, cpu) != NULL) {
147 			taskqueue_free(VMBUS_PCPU_GET(sc, event_tq, cpu));
148 			VMBUS_PCPU_GET(sc, event_tq, cpu) = NULL;
149 		}
150 		if (VMBUS_PCPU_GET(sc, message_tq, cpu) != NULL) {
151 			taskqueue_drain(VMBUS_PCPU_GET(sc, message_tq, cpu),
152 			    VMBUS_PCPU_PTR(sc, message_task, cpu));
153 			taskqueue_free(VMBUS_PCPU_GET(sc, message_tq, cpu));
154 			VMBUS_PCPU_GET(sc, message_tq, cpu) = NULL;
155 		}
156 	}
157 }
158