1 /* $OpenBSD: virtiovar.h,v 1.29 2025/01/29 14:03:19 sf Exp $ */
2 /* $NetBSD: virtiovar.h,v 1.1 2011/10/30 12:12:21 hannken Exp $ */
3
4 /*
5 * Copyright (c) 2012 Stefan Fritsch.
6 * Copyright (c) 2010 Minoura Makoto.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Part of the file derived from `Virtio PCI Card Specification v0.8.6 DRAFT'
32 * Appendix A.
33 */
34 /* An interface for efficient virtio implementation.
35 *
36 * This header is BSD licensed so anyone can use the definitions
37 * to implement compatible drivers/servers.
38 *
39 * Copyright 2007, 2009, IBM Corporation
40 * All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of IBM nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 */
65
66
67 #ifndef _DEV_PV_VIRTIOVAR_H_
68 #define _DEV_PV_VIRTIOVAR_H_
69
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/device.h>
73 #include <sys/mutex.h>
74 #include <machine/bus.h>
75
76 #include <dev/pv/virtioreg.h>
77
78 #ifndef VIRTIO_DEBUG
79 #define VIRTIO_DEBUG 0
80 #endif
81
82 /* flags for config(8) */
83 #define VIRTIO_CF_NO_INDIRECT 1
84 #define VIRTIO_CF_NO_EVENT_IDX 2
85 #define VIRTIO_CF_PREFER_VERSION_09 8
86
87 struct virtio_attach_args {
88 int va_devid; /* virtio device id */
89 unsigned int va_nintr; /* number of intr vectors */
90 };
91
92 struct vq_entry {
93 SLIST_ENTRY(vq_entry) qe_list; /* free list */
94 uint16_t qe_index; /* index in vq_desc array */
95
96 /* The following are used only in the `head' entry */
97 int16_t qe_next; /* next enq slot */
98 int16_t qe_indirect; /* 1 if using indirect */
99 int16_t qe_vr_index; /* index in sc_reqs array */
100 struct vring_desc *qe_desc_base; /* pointer to vd array */
101 };
102
103 struct virtqueue {
104 struct virtio_softc *vq_owner;
105 unsigned int vq_num; /* queue size (# of entries),
106 * 0 if unused/non-existant */
107 unsigned int vq_mask; /* (1 << vq_num - 1) */
108 int vq_index; /* queue number (0, 1, ...) */
109
110 /* vring pointers (KVA) */
111 struct vring_desc *vq_desc;
112 struct vring_avail *vq_avail;
113 struct vring_used *vq_used;
114 void *vq_indirect;
115
116 /* virtqueue allocation info */
117 void *vq_vaddr;
118 int vq_availoffset;
119 int vq_usedoffset;
120 int vq_indirectoffset;
121 bus_dma_segment_t vq_segs[1];
122 unsigned int vq_bytesize;
123 bus_dmamap_t vq_dmamap;
124
125 int vq_maxnsegs;
126
127 /* free entry management */
128 struct vq_entry *vq_entries;
129 SLIST_HEAD(, vq_entry) vq_freelist;
130
131 /* enqueue/dequeue status */
132 uint16_t vq_avail_idx;
133 uint16_t vq_used_idx;
134 int vq_queued;
135
136 /* interrupt handler */
137 int (*vq_done)(struct virtqueue*);
138 /* 1.x only: offset for notify address calculation */
139 uint32_t vq_notify_off;
140 int vq_intr_vec;
141 };
142
143 struct virtio_feature_name {
144 uint64_t bit;
145 const char *name;
146 };
147
148 struct virtio_ops {
149 void (*kick)(struct virtio_softc *, uint16_t);
150 uint8_t (*read_dev_cfg_1)(struct virtio_softc *, int);
151 uint16_t (*read_dev_cfg_2)(struct virtio_softc *, int);
152 uint32_t (*read_dev_cfg_4)(struct virtio_softc *, int);
153 uint64_t (*read_dev_cfg_8)(struct virtio_softc *, int);
154 void (*write_dev_cfg_1)(struct virtio_softc *, int, uint8_t);
155 void (*write_dev_cfg_2)(struct virtio_softc *, int, uint16_t);
156 void (*write_dev_cfg_4)(struct virtio_softc *, int, uint32_t);
157 void (*write_dev_cfg_8)(struct virtio_softc *, int, uint64_t);
158 uint16_t (*read_queue_size)(struct virtio_softc *, uint16_t);
159 void (*setup_queue)(struct virtio_softc *, struct virtqueue *, uint64_t);
160 void (*setup_intrs)(struct virtio_softc *);
161 int (*get_status)(struct virtio_softc *);
162 void (*set_status)(struct virtio_softc *, int);
163 int (*neg_features)(struct virtio_softc *, const struct virtio_feature_name *);
164 int (*attach_finish)(struct virtio_softc *, struct virtio_attach_args *);
165 int (*poll_intr)(void *);
166 void (*intr_barrier)(struct virtio_softc *);
167 int (*intr_establish)(struct virtio_softc *, struct virtio_attach_args *,
168 int, struct cpu_info *, int (*)(void *), void *);
169 };
170
171 #define VIRTIO_CHILD_ERROR ((void*)1)
172
173 struct virtio_softc {
174 struct device sc_dev;
175 bus_dma_tag_t sc_dmat; /* set by transport */
176 const struct virtio_ops *sc_ops; /* set by transport */
177
178 int sc_ipl; /* set by child */
179
180 uint64_t sc_driver_features;
181 uint64_t sc_active_features;
182 int sc_indirect;
183 int sc_version_1;
184
185 int sc_nvqs; /* size of sc_vqs, set by child */
186 struct virtqueue *sc_vqs; /* set by child */
187
188 struct device *sc_child; /* set by child,
189 * VIRTIO_CHILD_ERROR on error
190 */
191 int (*sc_config_change)(struct virtio_softc*);
192 /* set by child */
193 };
194
195 /* public interface */
196 #define virtio_read_device_config_1(sc, o) (sc)->sc_ops->read_dev_cfg_1(sc, o)
197 #define virtio_read_device_config_2(sc, o) (sc)->sc_ops->read_dev_cfg_2(sc, o)
198 #define virtio_read_device_config_4(sc, o) (sc)->sc_ops->read_dev_cfg_4(sc, o)
199 #define virtio_read_device_config_8(sc, o) (sc)->sc_ops->read_dev_cfg_8(sc, o)
200 #define virtio_write_device_config_1(sc, o, v) (sc)->sc_ops->write_dev_cfg_1(sc, o, v)
201 #define virtio_write_device_config_2(sc, o, v) (sc)->sc_ops->write_dev_cfg_2(sc, o, v)
202 #define virtio_write_device_config_4(sc, o, v) (sc)->sc_ops->write_dev_cfg_4(sc, o, v)
203 #define virtio_write_device_config_8(sc, o, v) (sc)->sc_ops->write_dev_cfg_8(sc, o, v)
204 #define virtio_read_queue_size(sc, i) (sc)->sc_ops->read_queue_size(sc, i)
205 #define virtio_setup_queue(sc, i, v) (sc)->sc_ops->setup_queue(sc, i, v)
206 #define virtio_negotiate_features(sc, n) (sc)->sc_ops->neg_features(sc, n)
207 #define virtio_poll_intr(sc) (sc)->sc_ops->poll_intr(sc)
208 #define virtio_get_status(sc) (sc)->sc_ops->get_status(sc)
209 #define virtio_set_status(sc, i) (sc)->sc_ops->set_status(sc, i)
210 #define virtio_intr_barrier(sc) (sc)->sc_ops->intr_barrier(sc)
211
212 /*
213 * virtio_intr_establish() only works if va_nintr > 1. If it is called by a
214 * child driver, the transport driver will skip automatic intr allocation and
215 * the child driver must allocate all required interrupts itself. Vector 0 is
216 * always used for the config change interrupt.
217 */
218 #define virtio_intr_establish(sc, va, v, ci, fn, a) (sc)->sc_ops->intr_establish(sc, va, v, ci, fn, a)
219
220 /* only for transport drivers */
221 #define virtio_device_reset(sc) virtio_set_status((sc), 0)
222
223 static inline int
virtio_has_feature(struct virtio_softc * sc,uint64_t fbit)224 virtio_has_feature(struct virtio_softc *sc, uint64_t fbit)
225 {
226 if (sc->sc_active_features & fbit)
227 return 1;
228 return 0;
229 }
230
231 int virtio_alloc_vq(struct virtio_softc*, struct virtqueue*, int, int,
232 const char*);
233 int virtio_free_vq(struct virtio_softc*, struct virtqueue*);
234 int virtio_attach_finish(struct virtio_softc *, struct virtio_attach_args *);
235 void virtio_reset(struct virtio_softc *);
236 void virtio_reinit_start(struct virtio_softc *);
237 void virtio_reinit_end(struct virtio_softc *);
238
239 int virtio_enqueue_prep(struct virtqueue*, int*);
240 int virtio_enqueue_reserve(struct virtqueue*, int, int);
241 int virtio_enqueue(struct virtqueue*, int, bus_dmamap_t, int);
242 int virtio_enqueue_p(struct virtqueue*, int, bus_dmamap_t, bus_addr_t,
243 bus_size_t, int);
244 void virtio_enqueue_commit(struct virtio_softc*, struct virtqueue*, int, int);
245 #define virtio_notify(sc,vq) virtio_enqueue_commit(sc, vq, -1, 1)
246
247 int virtio_enqueue_abort(struct virtqueue*, int);
248 void virtio_enqueue_trim(struct virtqueue*, int, int);
249
250 int virtio_dequeue(struct virtio_softc*, struct virtqueue*, int *, int *);
251 int virtio_dequeue_commit(struct virtqueue*, int);
252
253 int virtio_check_vqs(struct virtio_softc *);
254 int virtio_check_vq(struct virtio_softc *, struct virtqueue *);
255 void virtio_stop_vq_intr(struct virtio_softc *, struct virtqueue *);
256 int virtio_start_vq_intr(struct virtio_softc *, struct virtqueue *);
257
258 const char *virtio_device_string(int);
259 #if VIRTIO_DEBUG
260 void virtio_log_features(uint64_t, uint64_t, const struct virtio_feature_name *);
261 void virtio_vq_dump(struct virtqueue *vq);
262 #endif
263 int virtio_nused(struct virtqueue *vq);
264 int virtio_postpone_intr(struct virtqueue *vq, uint16_t nslots);
265 int virtio_postpone_intr_smart(struct virtqueue *vq);
266 int virtio_postpone_intr_far(struct virtqueue *vq);
267
268 #endif /* _DEV_PV_VIRTIOVAR_H_ */
269