xref: /freebsd/sys/dev/virtio/virtio.h (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
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  * $FreeBSD$
29  */
30 
31 #ifndef _VIRTIO_H_
32 #define _VIRTIO_H_
33 
34 #include <dev/virtio/virtio_ids.h>
35 #include <dev/virtio/virtio_config.h>
36 
37 struct vq_alloc_info;
38 
39 /*
40  * Each virtqueue indirect descriptor list must be physically contiguous.
41  * To allow us to malloc(9) each list individually, limit the number
42  * supported to what will fit in one page. With 4KB pages, this is a limit
43  * of 256 descriptors. If there is ever a need for more, we can switch to
44  * contigmalloc(9) for the larger allocations, similar to what
45  * bus_dmamem_alloc(9) does.
46  *
47  * Note the sizeof(struct vring_desc) is 16 bytes.
48  */
49 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
50 
51 /*
52  * VirtIO instance variables indices.
53  */
54 #define VIRTIO_IVAR_DEVTYPE		1
55 #define VIRTIO_IVAR_FEATURE_DESC	2
56 #define VIRTIO_IVAR_VENDOR		3
57 #define VIRTIO_IVAR_DEVICE		4
58 #define VIRTIO_IVAR_SUBVENDOR		5
59 #define VIRTIO_IVAR_SUBDEVICE		6
60 
61 struct virtio_feature_desc {
62 	uint64_t	 vfd_val;
63 	const char	*vfd_str;
64 };
65 
66 const char *virtio_device_name(uint16_t devid);
67 void	 virtio_describe(device_t dev, const char *msg,
68 	     uint64_t features, struct virtio_feature_desc *feature_desc);
69 
70 /*
71  * VirtIO Bus Methods.
72  */
73 void	 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val);
74 void	 virtio_write_ivar(device_t dev, int ivar, uintptr_t val);
75 uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features);
76 int	 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
77 	     struct vq_alloc_info *info);
78 int	 virtio_setup_intr(device_t dev, enum intr_type type);
79 int	 virtio_with_feature(device_t dev, uint64_t feature);
80 void	 virtio_stop(device_t dev);
81 int	 virtio_reinit(device_t dev, uint64_t features);
82 void	 virtio_reinit_complete(device_t dev);
83 
84 /*
85  * Read/write a variable amount from the device specific (ie, network)
86  * configuration region. This region is encoded in the same endian as
87  * the guest.
88  */
89 void	 virtio_read_device_config(device_t dev, bus_size_t offset,
90 	     void *dst, int length);
91 void	 virtio_write_device_config(device_t dev, bus_size_t offset,
92 	     void *src, int length);
93 
94 /* Inlined device specific read/write functions for common lengths. */
95 #define VIRTIO_RDWR_DEVICE_CONFIG(size, type)				\
96 static inline type							\
97 __CONCAT(virtio_read_dev_config_,size)(device_t dev,			\
98     bus_size_t offset)							\
99 {									\
100 	type val;							\
101 	virtio_read_device_config(dev, offset, &val, sizeof(type));	\
102 	return (val);							\
103 }									\
104 									\
105 static inline void							\
106 __CONCAT(virtio_write_dev_config_,size)(device_t dev,			\
107     bus_size_t offset, type val)					\
108 {									\
109 	virtio_write_device_config(dev, offset, &val, sizeof(type));	\
110 }
111 
112 VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t);
113 VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t);
114 VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t);
115 
116 #undef VIRTIO_RDWR_DEVICE_CONFIG
117 
118 #define VIRTIO_READ_IVAR(name, ivar)					\
119 static inline int							\
120 __CONCAT(virtio_get_,name)(device_t dev)				\
121 {									\
122 	uintptr_t val;							\
123 	virtio_read_ivar(dev, ivar, &val);				\
124 	return ((int) val);						\
125 }
126 
127 VIRTIO_READ_IVAR(device_type,	VIRTIO_IVAR_DEVTYPE);
128 VIRTIO_READ_IVAR(vendor,	VIRTIO_IVAR_VENDOR);
129 VIRTIO_READ_IVAR(device,	VIRTIO_IVAR_DEVICE);
130 VIRTIO_READ_IVAR(subvendor,	VIRTIO_IVAR_SUBVENDOR);
131 VIRTIO_READ_IVAR(subdevice,	VIRTIO_IVAR_SUBDEVICE);
132 
133 #undef VIRTIO_READ_IVAR
134 
135 #define VIRTIO_WRITE_IVAR(name, ivar)					\
136 static inline void							\
137 __CONCAT(virtio_set_,name)(device_t dev, void *val)			\
138 {									\
139 	virtio_write_ivar(dev, ivar, (uintptr_t) val);			\
140 }
141 
142 VIRTIO_WRITE_IVAR(feature_desc,	VIRTIO_IVAR_FEATURE_DESC);
143 
144 #undef VIRTIO_WRITE_IVAR
145 
146 #endif /* _VIRTIO_H_ */
147