vmbusvar.h revision 1.2.2.2 1 1.2.2.2 christos /* $NetBSD: vmbusvar.h,v 1.2.2.2 2019/06/10 22:07:09 christos Exp $ */
2 1.2.2.2 christos /* $OpenBSD: hypervvar.h,v 1.13 2017/06/23 19:05:42 mikeb Exp $ */
3 1.2.2.2 christos
4 1.2.2.2 christos /*
5 1.2.2.2 christos * Copyright (c) 2016 Mike Belopuhov <mike (at) esdenera.com>
6 1.2.2.2 christos *
7 1.2.2.2 christos * Permission to use, copy, modify, and distribute this software for any
8 1.2.2.2 christos * purpose with or without fee is hereby granted, provided that the above
9 1.2.2.2 christos * copyright notice and this permission notice appear in all copies.
10 1.2.2.2 christos *
11 1.2.2.2 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.2.2.2 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.2.2.2 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.2.2.2 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.2.2.2 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.2.2.2 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.2.2.2 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.2.2.2 christos */
19 1.2.2.2 christos
20 1.2.2.2 christos #ifndef _VMBUSVAR_H_
21 1.2.2.2 christos #define _VMBUSVAR_H_
22 1.2.2.2 christos
23 1.2.2.2 christos #include <sys/param.h>
24 1.2.2.2 christos #include <sys/device.h>
25 1.2.2.2 christos #include <sys/atomic.h>
26 1.2.2.2 christos #include <sys/bus.h>
27 1.2.2.2 christos #include <sys/evcnt.h>
28 1.2.2.2 christos #include <sys/kcpuset.h>
29 1.2.2.2 christos #include <sys/mutex.h>
30 1.2.2.2 christos #include <sys/pool.h>
31 1.2.2.2 christos
32 1.2.2.2 christos #include <dev/hyperv/hypervreg.h>
33 1.2.2.2 christos #include <dev/hyperv/hypervvar.h>
34 1.2.2.2 christos
35 1.2.2.2 christos /* #define HYPERV_DEBUG */
36 1.2.2.2 christos
37 1.2.2.2 christos #ifdef HYPERV_DEBUG
38 1.2.2.2 christos #define DPRINTF(x...) printf(x)
39 1.2.2.2 christos #else
40 1.2.2.2 christos #define DPRINTF(x...)
41 1.2.2.2 christos #endif
42 1.2.2.2 christos
43 1.2.2.2 christos typedef void (*vmbus_channel_callback_t)(void *);
44 1.2.2.2 christos
45 1.2.2.2 christos struct vmbus_softc;
46 1.2.2.2 christos
47 1.2.2.2 christos struct vmbus_msg {
48 1.2.2.2 christos uint64_t msg_flags;
49 1.2.2.2 christos #define MSGF_NOSLEEP __BIT(0)
50 1.2.2.2 christos #define MSGF_NOQUEUE __BIT(1)
51 1.2.2.2 christos #define MSGF_ORPHANED __BIT(2)
52 1.2.2.2 christos struct hyperv_hypercall_postmsg_in msg_req __aligned(8);
53 1.2.2.2 christos void *msg_rsp;
54 1.2.2.2 christos size_t msg_rsplen;
55 1.2.2.2 christos TAILQ_ENTRY(vmbus_msg) msg_entry;
56 1.2.2.2 christos };
57 1.2.2.2 christos __CTASSERT((offsetof(struct vmbus_msg, msg_req) % 8) == 0);
58 1.2.2.2 christos TAILQ_HEAD(vmbus_queue, vmbus_msg);
59 1.2.2.2 christos
60 1.2.2.2 christos struct vmbus_offer {
61 1.2.2.2 christos struct vmbus_chanmsg_choffer co_chan;
62 1.2.2.2 christos SIMPLEQ_ENTRY(vmbus_offer) co_entry;
63 1.2.2.2 christos };
64 1.2.2.2 christos SIMPLEQ_HEAD(vmbus_offers, vmbus_offer);
65 1.2.2.2 christos
66 1.2.2.2 christos struct vmbus_ring_data {
67 1.2.2.2 christos struct vmbus_bufring *rd_ring;
68 1.2.2.2 christos uint32_t rd_size;
69 1.2.2.2 christos kmutex_t rd_lock;
70 1.2.2.2 christos uint32_t rd_prod;
71 1.2.2.2 christos uint32_t rd_cons;
72 1.2.2.2 christos uint32_t rd_dsize;
73 1.2.2.2 christos };
74 1.2.2.2 christos
75 1.2.2.2 christos struct vmbus_channel;
76 1.2.2.2 christos TAILQ_HEAD(vmbus_channels, vmbus_channel);
77 1.2.2.2 christos
78 1.2.2.2 christos struct vmbus_channel {
79 1.2.2.2 christos struct vmbus_softc *ch_sc;
80 1.2.2.2 christos device_t ch_dev;
81 1.2.2.2 christos u_int ch_refs;
82 1.2.2.2 christos
83 1.2.2.2 christos int ch_state;
84 1.2.2.2 christos #define VMBUS_CHANSTATE_OFFERED 1
85 1.2.2.2 christos #define VMBUS_CHANSTATE_OPENED 2
86 1.2.2.2 christos #define VMBUS_CHANSTATE_CLOSING 3
87 1.2.2.2 christos #define VMBUS_CHANSTATE_CLOSED 4
88 1.2.2.2 christos uint32_t ch_id;
89 1.2.2.2 christos uint16_t ch_subidx;
90 1.2.2.2 christos
91 1.2.2.2 christos struct hyperv_guid ch_type;
92 1.2.2.2 christos struct hyperv_guid ch_inst;
93 1.2.2.2 christos char ch_ident[38];
94 1.2.2.2 christos
95 1.2.2.2 christos void *ch_ring;
96 1.2.2.2 christos uint32_t ch_ring_gpadl;
97 1.2.2.2 christos u_long ch_ring_size;
98 1.2.2.2 christos struct hyperv_dma ch_ring_dma;
99 1.2.2.2 christos
100 1.2.2.2 christos struct vmbus_ring_data ch_wrd;
101 1.2.2.2 christos struct vmbus_ring_data ch_rrd;
102 1.2.2.2 christos
103 1.2.2.2 christos int ch_cpuid;
104 1.2.2.2 christos uint32_t ch_vcpu;
105 1.2.2.2 christos
106 1.2.2.2 christos void (*ch_handler)(void *);
107 1.2.2.2 christos void *ch_ctx;
108 1.2.2.2 christos struct evcnt ch_evcnt;
109 1.2.2.2 christos void *ch_taskq;
110 1.2.2.2 christos
111 1.2.2.2 christos uint32_t ch_flags;
112 1.2.2.2 christos #define CHF_BATCHED __BIT(0)
113 1.2.2.2 christos #define CHF_MONITOR __BIT(1)
114 1.2.2.2 christos
115 1.2.2.2 christos uint8_t ch_mgroup;
116 1.2.2.2 christos uint8_t ch_mindex;
117 1.2.2.2 christos struct hyperv_mon_param *ch_monprm;
118 1.2.2.2 christos struct hyperv_dma ch_monprm_dma;
119 1.2.2.2 christos
120 1.2.2.2 christos TAILQ_ENTRY(vmbus_channel) ch_entry;
121 1.2.2.2 christos
122 1.2.2.2 christos kmutex_t ch_subchannel_lock;
123 1.2.2.2 christos struct vmbus_channels ch_subchannels;
124 1.2.2.2 christos u_int ch_subchannel_count;
125 1.2.2.2 christos TAILQ_ENTRY(vmbus_channel) ch_subentry;
126 1.2.2.2 christos struct vmbus_channel *ch_primary_channel;
127 1.2.2.2 christos };
128 1.2.2.2 christos
129 1.2.2.2 christos #define VMBUS_CHAN_ISPRIMARY(chan) ((chan)->ch_subidx == 0)
130 1.2.2.2 christos
131 1.2.2.2 christos struct vmbus_attach_args {
132 1.2.2.2 christos struct hyperv_guid *aa_type;
133 1.2.2.2 christos struct hyperv_guid *aa_inst;
134 1.2.2.2 christos char *aa_ident;
135 1.2.2.2 christos struct vmbus_channel *aa_chan;
136 1.2.2.2 christos bus_space_tag_t aa_iot;
137 1.2.2.2 christos bus_space_tag_t aa_memt;
138 1.2.2.2 christos };
139 1.2.2.2 christos
140 1.2.2.2 christos struct vmbus_dev {
141 1.2.2.2 christos struct vmbus_attach_args dv_aa;
142 1.2.2.2 christos SLIST_ENTRY(vmbus_dev) dv_entry;
143 1.2.2.2 christos };
144 1.2.2.2 christos SLIST_HEAD(vmbus_devices, vmbus_dev);
145 1.2.2.2 christos
146 1.2.2.2 christos struct vmbus_percpu_data {
147 1.2.2.2 christos void *simp; /* Synthetic Interrupt Message Page */
148 1.2.2.2 christos void *siep; /* Synthetic Interrupt Event Flags Page */
149 1.2.2.2 christos uint32_t vcpuid; /* Virtual cpuid */
150 1.2.2.2 christos
151 1.2.2.2 christos /* Rarely used fields */
152 1.2.2.2 christos struct hyperv_dma simp_dma;
153 1.2.2.2 christos struct hyperv_dma siep_dma;
154 1.2.2.2 christos } __aligned(CACHE_LINE_SIZE);
155 1.2.2.2 christos
156 1.2.2.2 christos struct vmbus_softc {
157 1.2.2.2 christos device_t sc_dev;
158 1.2.2.2 christos bus_space_tag_t sc_iot;
159 1.2.2.2 christos bus_space_tag_t sc_memt;
160 1.2.2.2 christos bus_dma_tag_t sc_dmat;
161 1.2.2.2 christos
162 1.2.2.2 christos pool_cache_t sc_msgpool;
163 1.2.2.2 christos
164 1.2.2.2 christos void *sc_msg_sih;
165 1.2.2.2 christos
166 1.2.2.2 christos u_long *sc_wevents; /* Write events */
167 1.2.2.2 christos u_long *sc_revents; /* Read events */
168 1.2.2.2 christos struct vmbus_mnf *sc_monitor[2];
169 1.2.2.2 christos struct vmbus_percpu_data sc_percpu[MAXCPUS];
170 1.2.2.2 christos
171 1.2.2.2 christos /*
172 1.2.2.2 christos * Rarely used fields
173 1.2.2.2 christos */
174 1.2.2.2 christos uint32_t sc_flags;
175 1.2.2.2 christos #define VMBUS_SCFLAG_SYNIC __BIT(0)
176 1.2.2.2 christos #define VMBUS_SCFLAG_CONNECTED __BIT(1)
177 1.2.2.2 christos #define VMBUS_SCFLAG_OFFERS_DELIVERED __BIT(2)
178 1.2.2.2 christos uint32_t sc_proto;
179 1.2.2.2 christos int sc_channel_max;
180 1.2.2.2 christos
181 1.2.2.2 christos kcpuset_t *sc_intr_cpuset;
182 1.2.2.2 christos
183 1.2.2.2 christos /* Shared memory for Write/Read events */
184 1.2.2.2 christos void *sc_events;
185 1.2.2.2 christos struct hyperv_dma sc_events_dma;
186 1.2.2.2 christos
187 1.2.2.2 christos struct hyperv_dma sc_monitor_dma[2];
188 1.2.2.2 christos
189 1.2.2.2 christos struct vmbus_queue sc_reqs; /* Request queue */
190 1.2.2.2 christos kmutex_t sc_req_lock;
191 1.2.2.2 christos struct vmbus_queue sc_rsps; /* Response queue */
192 1.2.2.2 christos kmutex_t sc_rsp_lock;
193 1.2.2.2 christos
194 1.2.2.2 christos struct vmbus_offers sc_offers;
195 1.2.2.2 christos kmutex_t sc_offer_lock;
196 1.2.2.2 christos
197 1.2.2.2 christos struct vmbus_channels sc_channels;
198 1.2.2.2 christos kmutex_t sc_channel_lock;
199 1.2.2.2 christos
200 1.2.2.2 christos volatile uint32_t sc_handle;
201 1.2.2.2 christos
202 1.2.2.2 christos struct vmbus_devices sc_icdevs;
203 1.2.2.2 christos kmutex_t sc_icdev_lock;
204 1.2.2.2 christos
205 1.2.2.2 christos struct vmbus_devices sc_devs;
206 1.2.2.2 christos kmutex_t sc_dev_lock;
207 1.2.2.2 christos };
208 1.2.2.2 christos
209 1.2.2.2 christos static __inline void
210 1.2.2.2 christos clear_bit(u_int b, volatile void *p)
211 1.2.2.2 christos {
212 1.2.2.2 christos atomic_and_32(((volatile u_int *)p) + (b >> 5), ~(1 << (b & 0x1f)));
213 1.2.2.2 christos }
214 1.2.2.2 christos
215 1.2.2.2 christos static __inline void
216 1.2.2.2 christos set_bit(u_int b, volatile void *p)
217 1.2.2.2 christos {
218 1.2.2.2 christos atomic_or_32(((volatile u_int *)p) + (b >> 5), 1 << (b & 0x1f));
219 1.2.2.2 christos }
220 1.2.2.2 christos
221 1.2.2.2 christos static __inline int
222 1.2.2.2 christos test_bit(u_int b, volatile void *p)
223 1.2.2.2 christos {
224 1.2.2.2 christos return !!(((volatile u_int *)p)[b >> 5] & (1 << (b & 0x1f)));
225 1.2.2.2 christos }
226 1.2.2.2 christos
227 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_network;
228 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_ide;
229 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_scsi;
230 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_shutdown;
231 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_timesync;
232 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_heartbeat;
233 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_kvp;
234 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_vss;
235 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_dynmem;
236 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_mouse;
237 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_kbd;
238 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_video;
239 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_fc;
240 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_fcopy;
241 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_pcie;
242 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_netdir;
243 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_rdesktop;
244 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma1;
245 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma2;
246 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma3;
247 1.2.2.2 christos extern const struct hyperv_guid hyperv_guid_avma4;
248 1.2.2.2 christos
249 1.2.2.2 christos int vmbus_match(device_t, cfdata_t, void *);
250 1.2.2.2 christos int vmbus_attach(struct vmbus_softc *);
251 1.2.2.2 christos int vmbus_detach(struct vmbus_softc *, int);
252 1.2.2.2 christos
253 1.2.2.2 christos int vmbus_handle_alloc(struct vmbus_channel *, const struct hyperv_dma *,
254 1.2.2.2 christos uint32_t, uint32_t *);
255 1.2.2.2 christos void vmbus_handle_free(struct vmbus_channel *, uint32_t);
256 1.2.2.2 christos int vmbus_channel_open(struct vmbus_channel *, size_t, void *, size_t,
257 1.2.2.2 christos vmbus_channel_callback_t, void *);
258 1.2.2.2 christos int vmbus_channel_close(struct vmbus_channel *);
259 1.2.2.2 christos int vmbus_channel_close_direct(struct vmbus_channel *);
260 1.2.2.2 christos int vmbus_channel_setdeferred(struct vmbus_channel *, const char *);
261 1.2.2.2 christos void vmbus_channel_schedule(struct vmbus_channel *);
262 1.2.2.2 christos int vmbus_channel_send(struct vmbus_channel *, void *, uint32_t, uint64_t,
263 1.2.2.2 christos int, uint32_t);
264 1.2.2.2 christos int vmbus_channel_send_sgl(struct vmbus_channel *, struct vmbus_gpa *,
265 1.2.2.2 christos uint32_t, void *, uint32_t, uint64_t);
266 1.2.2.2 christos int vmbus_channel_send_prpl(struct vmbus_channel *,
267 1.2.2.2 christos struct vmbus_gpa_range *, uint32_t, void *, uint32_t, uint64_t);
268 1.2.2.2 christos int vmbus_channel_recv(struct vmbus_channel *, void *, uint32_t, uint32_t *,
269 1.2.2.2 christos uint64_t *, int);
270 1.2.2.2 christos void vmbus_channel_cpu_set(struct vmbus_channel *, int);
271 1.2.2.2 christos void vmbus_channel_cpu_rr(struct vmbus_channel *);
272 1.2.2.2 christos
273 1.2.2.2 christos struct vmbus_channel **
274 1.2.2.2 christos vmbus_subchannel_get(struct vmbus_channel *, int);
275 1.2.2.2 christos void vmbus_subchannel_put(struct vmbus_channel **, int);
276 1.2.2.2 christos
277 1.2.2.2 christos #endif /* _VMBUSVAR_H_ */
278