virtio.h revision 331722
1/*- 2 * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: stable/11/sys/dev/virtio/virtio.h 331722 2018-03-29 02:50:57Z eadler $ 27 */ 28 29#ifndef _VIRTIO_H_ 30#define _VIRTIO_H_ 31 32#include <dev/virtio/virtio_ids.h> 33#include <dev/virtio/virtio_config.h> 34 35struct vq_alloc_info; 36 37/* 38 * Each virtqueue indirect descriptor list must be physically contiguous. 39 * To allow us to malloc(9) each list individually, limit the number 40 * supported to what will fit in one page. With 4KB pages, this is a limit 41 * of 256 descriptors. If there is ever a need for more, we can switch to 42 * contigmalloc(9) for the larger allocations, similar to what 43 * bus_dmamem_alloc(9) does. 44 * 45 * Note the sizeof(struct vring_desc) is 16 bytes. 46 */ 47#define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16)) 48 49/* 50 * VirtIO instance variables indices. 51 */ 52#define VIRTIO_IVAR_DEVTYPE 1 53#define VIRTIO_IVAR_FEATURE_DESC 2 54#define VIRTIO_IVAR_VENDOR 3 55#define VIRTIO_IVAR_DEVICE 4 56#define VIRTIO_IVAR_SUBVENDOR 5 57#define VIRTIO_IVAR_SUBDEVICE 6 58 59struct virtio_feature_desc { 60 uint64_t vfd_val; 61 const char *vfd_str; 62}; 63 64const char *virtio_device_name(uint16_t devid); 65void virtio_describe(device_t dev, const char *msg, 66 uint64_t features, struct virtio_feature_desc *feature_desc); 67 68/* 69 * VirtIO Bus Methods. 70 */ 71void virtio_read_ivar(device_t dev, int ivar, uintptr_t *val); 72void virtio_write_ivar(device_t dev, int ivar, uintptr_t val); 73uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features); 74int virtio_alloc_virtqueues(device_t dev, int flags, int nvqs, 75 struct vq_alloc_info *info); 76int virtio_setup_intr(device_t dev, enum intr_type type); 77int virtio_with_feature(device_t dev, uint64_t feature); 78void virtio_stop(device_t dev); 79int virtio_config_generation(device_t dev); 80int virtio_reinit(device_t dev, uint64_t features); 81void virtio_reinit_complete(device_t dev); 82 83/* 84 * Read/write a variable amount from the device specific (ie, network) 85 * configuration region. This region is encoded in the same endian as 86 * the guest. 87 */ 88void virtio_read_device_config(device_t dev, bus_size_t offset, 89 void *dst, int length); 90void virtio_write_device_config(device_t dev, bus_size_t offset, 91 void *src, int length); 92 93/* Inlined device specific read/write functions for common lengths. */ 94#define VIRTIO_RDWR_DEVICE_CONFIG(size, type) \ 95static inline type \ 96__CONCAT(virtio_read_dev_config_,size)(device_t dev, \ 97 bus_size_t offset) \ 98{ \ 99 type val; \ 100 virtio_read_device_config(dev, offset, &val, sizeof(type)); \ 101 return (val); \ 102} \ 103 \ 104static inline void \ 105__CONCAT(virtio_write_dev_config_,size)(device_t dev, \ 106 bus_size_t offset, type val) \ 107{ \ 108 virtio_write_device_config(dev, offset, &val, sizeof(type)); \ 109} 110 111VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t); 112VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t); 113VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t); 114 115#undef VIRTIO_RDWR_DEVICE_CONFIG 116 117#define VIRTIO_READ_IVAR(name, ivar) \ 118static inline int \ 119__CONCAT(virtio_get_,name)(device_t dev) \ 120{ \ 121 uintptr_t val; \ 122 virtio_read_ivar(dev, ivar, &val); \ 123 return ((int) val); \ 124} 125 126VIRTIO_READ_IVAR(device_type, VIRTIO_IVAR_DEVTYPE); 127VIRTIO_READ_IVAR(vendor, VIRTIO_IVAR_VENDOR); 128VIRTIO_READ_IVAR(device, VIRTIO_IVAR_DEVICE); 129VIRTIO_READ_IVAR(subvendor, VIRTIO_IVAR_SUBVENDOR); 130VIRTIO_READ_IVAR(subdevice, VIRTIO_IVAR_SUBDEVICE); 131 132#undef VIRTIO_READ_IVAR 133 134#define VIRTIO_WRITE_IVAR(name, ivar) \ 135static inline void \ 136__CONCAT(virtio_set_,name)(device_t dev, void *val) \ 137{ \ 138 virtio_write_ivar(dev, ivar, (uintptr_t) val); \ 139} 140 141VIRTIO_WRITE_IVAR(feature_desc, VIRTIO_IVAR_FEATURE_DESC); 142 143#undef VIRTIO_WRITE_IVAR 144 145#endif /* _VIRTIO_H_ */ 146