![]() |
|
|||
0001 #ifndef _UAPI_LINUX_VIRTIO_RING_H 0002 #define _UAPI_LINUX_VIRTIO_RING_H 0003 /* An interface for efficient virtio implementation, currently for use by KVM, 0004 * but hopefully others soon. Do NOT change this since it will 0005 * break existing servers and clients. 0006 * 0007 * This header is BSD licensed so anyone can use the definitions to implement 0008 * compatible drivers/servers. 0009 * 0010 * Redistribution and use in source and binary forms, with or without 0011 * modification, are permitted provided that the following conditions 0012 * are met: 0013 * 1. Redistributions of source code must retain the above copyright 0014 * notice, this list of conditions and the following disclaimer. 0015 * 2. Redistributions in binary form must reproduce the above copyright 0016 * notice, this list of conditions and the following disclaimer in the 0017 * documentation and/or other materials provided with the distribution. 0018 * 3. Neither the name of IBM nor the names of its contributors 0019 * may be used to endorse or promote products derived from this software 0020 * without specific prior written permission. 0021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 0022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 0023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 0024 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 0025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 0026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 0027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 0028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 0029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 0030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 0031 * SUCH DAMAGE. 0032 * 0033 * Copyright Rusty Russell IBM Corporation 2007. */ 0034 #ifndef __KERNEL__ 0035 #include <stdint.h> 0036 #endif 0037 #include <linux/types.h> 0038 #include <linux/virtio_types.h> 0039 0040 /* This marks a buffer as continuing via the next field. */ 0041 #define VRING_DESC_F_NEXT 1 0042 /* This marks a buffer as write-only (otherwise read-only). */ 0043 #define VRING_DESC_F_WRITE 2 0044 /* This means the buffer contains a list of buffer descriptors. */ 0045 #define VRING_DESC_F_INDIRECT 4 0046 0047 /* 0048 * Mark a descriptor as available or used in packed ring. 0049 * Notice: they are defined as shifts instead of shifted values. 0050 */ 0051 #define VRING_PACKED_DESC_F_AVAIL 7 0052 #define VRING_PACKED_DESC_F_USED 15 0053 0054 /* The Host uses this in used->flags to advise the Guest: don't kick me when 0055 * you add a buffer. It's unreliable, so it's simply an optimization. Guest 0056 * will still kick if it's out of buffers. */ 0057 #define VRING_USED_F_NO_NOTIFY 1 0058 /* The Guest uses this in avail->flags to advise the Host: don't interrupt me 0059 * when you consume a buffer. It's unreliable, so it's simply an 0060 * optimization. */ 0061 #define VRING_AVAIL_F_NO_INTERRUPT 1 0062 0063 /* Enable events in packed ring. */ 0064 #define VRING_PACKED_EVENT_FLAG_ENABLE 0x0 0065 /* Disable events in packed ring. */ 0066 #define VRING_PACKED_EVENT_FLAG_DISABLE 0x1 0067 /* 0068 * Enable events for a specific descriptor in packed ring. 0069 * (as specified by Descriptor Ring Change Event Offset/Wrap Counter). 0070 * Only valid if VIRTIO_RING_F_EVENT_IDX has been negotiated. 0071 */ 0072 #define VRING_PACKED_EVENT_FLAG_DESC 0x2 0073 0074 /* 0075 * Wrap counter bit shift in event suppression structure 0076 * of packed ring. 0077 */ 0078 #define VRING_PACKED_EVENT_F_WRAP_CTR 15 0079 0080 /* We support indirect buffer descriptors */ 0081 #define VIRTIO_RING_F_INDIRECT_DESC 28 0082 0083 /* The Guest publishes the used index for which it expects an interrupt 0084 * at the end of the avail ring. Host should ignore the avail->flags field. */ 0085 /* The Host publishes the avail index for which it expects a kick 0086 * at the end of the used ring. Guest should ignore the used->flags field. */ 0087 #define VIRTIO_RING_F_EVENT_IDX 29 0088 0089 /* Alignment requirements for vring elements. 0090 * When using pre-virtio 1.0 layout, these fall out naturally. 0091 */ 0092 #define VRING_AVAIL_ALIGN_SIZE 2 0093 #define VRING_USED_ALIGN_SIZE 4 0094 #define VRING_DESC_ALIGN_SIZE 16 0095 0096 /** 0097 * struct vring_desc - Virtio ring descriptors, 0098 * 16 bytes long. These can chain together via @next. 0099 * 0100 * @addr: buffer address (guest-physical) 0101 * @len: buffer length 0102 * @flags: descriptor flags 0103 * @next: index of the next descriptor in the chain, 0104 * if the VRING_DESC_F_NEXT flag is set. We chain unused 0105 * descriptors via this, too. 0106 */ 0107 struct vring_desc { 0108 __virtio64 addr; 0109 __virtio32 len; 0110 __virtio16 flags; 0111 __virtio16 next; 0112 }; 0113 0114 struct vring_avail { 0115 __virtio16 flags; 0116 __virtio16 idx; 0117 __virtio16 ring[]; 0118 }; 0119 0120 /* u32 is used here for ids for padding reasons. */ 0121 struct vring_used_elem { 0122 /* Index of start of used descriptor chain. */ 0123 __virtio32 id; 0124 /* Total length of the descriptor chain which was used (written to) */ 0125 __virtio32 len; 0126 }; 0127 0128 typedef struct vring_used_elem __attribute__((aligned(VRING_USED_ALIGN_SIZE))) 0129 vring_used_elem_t; 0130 0131 struct vring_used { 0132 __virtio16 flags; 0133 __virtio16 idx; 0134 vring_used_elem_t ring[]; 0135 }; 0136 0137 /* 0138 * The ring element addresses are passed between components with different 0139 * alignments assumptions. Thus, we might need to decrease the compiler-selected 0140 * alignment, and so must use a typedef to make sure the aligned attribute 0141 * actually takes hold: 0142 * 0143 * https://gcc.gnu.org/onlinedocs//gcc/Common-Type-Attributes.html#Common-Type-Attributes 0144 * 0145 * When used on a struct, or struct member, the aligned attribute can only 0146 * increase the alignment; in order to decrease it, the packed attribute must 0147 * be specified as well. When used as part of a typedef, the aligned attribute 0148 * can both increase and decrease alignment, and specifying the packed 0149 * attribute generates a warning. 0150 */ 0151 typedef struct vring_desc __attribute__((aligned(VRING_DESC_ALIGN_SIZE))) 0152 vring_desc_t; 0153 typedef struct vring_avail __attribute__((aligned(VRING_AVAIL_ALIGN_SIZE))) 0154 vring_avail_t; 0155 typedef struct vring_used __attribute__((aligned(VRING_USED_ALIGN_SIZE))) 0156 vring_used_t; 0157 0158 struct vring { 0159 unsigned int num; 0160 0161 vring_desc_t *desc; 0162 0163 vring_avail_t *avail; 0164 0165 vring_used_t *used; 0166 }; 0167 0168 #ifndef VIRTIO_RING_NO_LEGACY 0169 0170 /* The standard layout for the ring is a continuous chunk of memory which looks 0171 * like this. We assume num is a power of 2. 0172 * 0173 * struct vring 0174 * { 0175 * // The actual descriptors (16 bytes each) 0176 * struct vring_desc desc[num]; 0177 * 0178 * // A ring of available descriptor heads with free-running index. 0179 * __virtio16 avail_flags; 0180 * __virtio16 avail_idx; 0181 * __virtio16 available[num]; 0182 * __virtio16 used_event_idx; 0183 * 0184 * // Padding to the next align boundary. 0185 * char pad[]; 0186 * 0187 * // A ring of used descriptor heads with free-running index. 0188 * __virtio16 used_flags; 0189 * __virtio16 used_idx; 0190 * struct vring_used_elem used[num]; 0191 * __virtio16 avail_event_idx; 0192 * }; 0193 */ 0194 /* We publish the used event index at the end of the available ring, and vice 0195 * versa. They are at the end for backwards compatibility. */ 0196 #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) 0197 #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num]) 0198 0199 static inline void vring_init(struct vring *vr, unsigned int num, void *p, 0200 unsigned long align) 0201 { 0202 vr->num = num; 0203 vr->desc = p; 0204 vr->avail = (struct vring_avail *)((char *)p + num * sizeof(struct vring_desc)); 0205 vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + sizeof(__virtio16) 0206 + align-1) & ~(align - 1)); 0207 } 0208 0209 static inline unsigned vring_size(unsigned int num, unsigned long align) 0210 { 0211 return ((sizeof(struct vring_desc) * num + sizeof(__virtio16) * (3 + num) 0212 + align - 1) & ~(align - 1)) 0213 + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; 0214 } 0215 0216 #endif /* VIRTIO_RING_NO_LEGACY */ 0217 0218 /* The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX */ 0219 /* Assuming a given event_idx value from the other side, if 0220 * we have just incremented index from old to new_idx, 0221 * should we trigger an event? */ 0222 static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old) 0223 { 0224 /* Note: Xen has similar logic for notification hold-off 0225 * in include/xen/interface/io/ring.h with req_event and req_prod 0226 * corresponding to event_idx + 1 and new_idx respectively. 0227 * Note also that req_event and req_prod in Xen start at 1, 0228 * event indexes in virtio start at 0. */ 0229 return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old); 0230 } 0231 0232 struct vring_packed_desc_event { 0233 /* Descriptor Ring Change Event Offset/Wrap Counter. */ 0234 __le16 off_wrap; 0235 /* Descriptor Ring Change Event Flags. */ 0236 __le16 flags; 0237 }; 0238 0239 struct vring_packed_desc { 0240 /* Buffer Address. */ 0241 __le64 addr; 0242 /* Buffer Length. */ 0243 __le32 len; 0244 /* Buffer ID. */ 0245 __le16 id; 0246 /* The flags depending on descriptor type. */ 0247 __le16 flags; 0248 }; 0249 0250 #endif /* _UAPI_LINUX_VIRTIO_RING_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |