virtio.h revision 267312
1316099Sngie/*-
2316099Sngie * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
3316099Sngie * All rights reserved.
4316099Sngie *
5316099Sngie * Redistribution and use in source and binary forms, with or without
6316099Sngie * modification, are permitted provided that the following conditions
7316099Sngie * are met:
8316099Sngie * 1. Redistributions of source code must retain the above copyright
9316099Sngie *    notice unmodified, this list of conditions, and the following
10316099Sngie *    disclaimer.
11316099Sngie * 2. Redistributions in binary form must reproduce the above copyright
12316099Sngie *    notice, this list of conditions and the following disclaimer in the
13316099Sngie *    documentation and/or other materials provided with the distribution.
14316099Sngie *
15316099Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16316099Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17316099Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18316099Sngie * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19316099Sngie * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20316099Sngie * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21316099Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22316099Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23316099Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24316099Sngie * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25316099Sngie *
26316099Sngie * $FreeBSD: stable/10/sys/dev/virtio/virtio.h 267312 2014-06-10 03:23:35Z bryanv $
27316099Sngie */
28316099Sngie
29316099Sngie#ifndef _VIRTIO_H_
30316099Sngie#define _VIRTIO_H_
31316099Sngie
32316099Sngie#include <dev/virtio/virtio_ids.h>
33316099Sngie
34316099Sngiestruct vq_alloc_info;
35316099Sngie
36316099Sngie/*
37316099Sngie * Each virtqueue indirect descriptor list must be physically contiguous.
38316099Sngie * To allow us to malloc(9) each list individually, limit the number
39316099Sngie * supported to what will fit in one page. With 4KB pages, this is a limit
40316099Sngie * of 256 descriptors. If there is ever a need for more, we can switch to
41316099Sngie * contigmalloc(9) for the larger allocations, similar to what
42316099Sngie * bus_dmamem_alloc(9) does.
43316099Sngie *
44316099Sngie * Note the sizeof(struct vring_desc) is 16 bytes.
45316099Sngie */
46316099Sngie#define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
47316099Sngie
48316099Sngie/*
49316099Sngie * VirtIO instance variables indices.
50316099Sngie */
51316099Sngie#define VIRTIO_IVAR_DEVTYPE		1
52316099Sngie#define VIRTIO_IVAR_FEATURE_DESC	2
53316099Sngie#define VIRTIO_IVAR_VENDOR		3
54316099Sngie#define VIRTIO_IVAR_DEVICE		4
55316099Sngie#define VIRTIO_IVAR_SUBVENDOR		5
56316099Sngie#define VIRTIO_IVAR_SUBDEVICE		6
57316099Sngie
58struct virtio_feature_desc {
59	uint64_t	 vfd_val;
60	const char	*vfd_str;
61};
62
63const char *virtio_device_name(uint16_t devid);
64void	 virtio_describe(device_t dev, const char *msg,
65	     uint64_t features, struct virtio_feature_desc *feature_desc);
66
67/*
68 * VirtIO Bus Methods.
69 */
70void	 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val);
71void	 virtio_write_ivar(device_t dev, int ivar, uintptr_t val);
72uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features);
73int	 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
74	     struct vq_alloc_info *info);
75int	 virtio_setup_intr(device_t dev, enum intr_type type);
76int	 virtio_with_feature(device_t dev, uint64_t feature);
77void	 virtio_stop(device_t dev);
78int	 virtio_reinit(device_t dev, uint64_t features);
79void	 virtio_reinit_complete(device_t dev);
80
81/*
82 * Read/write a variable amount from the device specific (ie, network)
83 * configuration region. This region is encoded in the same endian as
84 * the guest.
85 */
86void	 virtio_read_device_config(device_t dev, bus_size_t offset,
87	     void *dst, int length);
88void	 virtio_write_device_config(device_t dev, bus_size_t offset,
89	     void *src, int length);
90
91/* Inlined device specific read/write functions for common lengths. */
92#define VIRTIO_RDWR_DEVICE_CONFIG(size, type)				\
93static inline type							\
94__CONCAT(virtio_read_dev_config_,size)(device_t dev,			\
95    bus_size_t offset)							\
96{									\
97	type val;							\
98	virtio_read_device_config(dev, offset, &val, sizeof(type));	\
99	return (val);							\
100}									\
101									\
102static inline void							\
103__CONCAT(virtio_write_dev_config_,size)(device_t dev,			\
104    bus_size_t offset, type val)					\
105{									\
106	virtio_write_device_config(dev, offset, &val, sizeof(type));	\
107}
108
109VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t);
110VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t);
111VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t);
112
113#undef VIRTIO_RDWR_DEVICE_CONFIG
114
115#define VIRTIO_READ_IVAR(name, ivar)					\
116static inline int							\
117__CONCAT(virtio_get_,name)(device_t dev)				\
118{									\
119	uintptr_t val;							\
120	virtio_read_ivar(dev, ivar, &val);				\
121	return ((int) val);						\
122}
123
124VIRTIO_READ_IVAR(device_type,	VIRTIO_IVAR_DEVTYPE);
125VIRTIO_READ_IVAR(vendor,	VIRTIO_IVAR_VENDOR);
126VIRTIO_READ_IVAR(device,	VIRTIO_IVAR_DEVICE);
127VIRTIO_READ_IVAR(subvendor,	VIRTIO_IVAR_SUBVENDOR);
128VIRTIO_READ_IVAR(subdevice,	VIRTIO_IVAR_SUBDEVICE);
129
130#undef VIRTIO_READ_IVAR
131
132#define VIRTIO_WRITE_IVAR(name, ivar)					\
133static inline void							\
134__CONCAT(virtio_set_,name)(device_t dev, void *val)			\
135{									\
136	virtio_write_ivar(dev, ivar, (uintptr_t) val);			\
137}
138
139VIRTIO_WRITE_IVAR(feature_desc,	VIRTIO_IVAR_FEATURE_DESC);
140
141#undef VIRTIO_WRITE_IVAR
142
143#endif /* _VIRTIO_H_ */
144