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