1 /*-
2  * Copyright Rusty Russell IBM Corporation 2007.
3  * Copyright 2019,2022 NXP
4  * This header is BSD licensed so anyone can use the definitions to implement
5  * compatible drivers/servers.
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, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of IBM nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef VIRTIO_RING_H
34 #define VIRTIO_RING_H
35 
36 /* This marks a buffer as continuing via the next field. */
37 #define VRING_DESC_F_NEXT 1U
38 /* This marks a buffer as write-only (otherwise read-only). */
39 #define VRING_DESC_F_WRITE 2U
40 /* This means the buffer contains a list of buffer descriptors. */
41 #define VRING_DESC_F_INDIRECT 4U
42 
43 /* The Host uses this in used->flags to advise the Guest: don't kick me
44  * when you add a buffer.  It's unreliable, so it's simply an
45  * optimization.  Guest will still kick if it's out of buffers. */
46 #define VRING_USED_F_NO_NOTIFY 1U
47 /* The Guest uses this in avail->flags to advise the Host: don't
48  * interrupt me when you consume a buffer.  It's unreliable, so it's
49  * simply an optimization.  */
50 #define VRING_AVAIL_F_NO_INTERRUPT 1U
51 
52 /* VirtIO ring descriptors: 16 bytes.
53  * These can chain together via "next". */
54 struct vring_desc
55 {
56     /* Address (guest-physical). */
57     uint64_t addr;
58     /* Length. */
59     uint32_t len;
60     /* The flags as indicated above. */
61     uint16_t flags;
62     /* We chain unused descriptors via this, too. */
63     uint16_t next;
64 };
65 
66 struct vring_avail
67 {
68     uint16_t flags;
69     uint16_t idx;
70     uint16_t ring[1];
71 };
72 
73 /* uint32_t is used here for ids for padding reasons. */
74 struct vring_used_elem
75 {
76     /* Index of start of used descriptor chain. */
77     uint32_t id;
78     /* Total length of the descriptor chain which was written to. */
79     uint32_t len;
80 };
81 
82 struct vring_used
83 {
84     uint16_t flags;
85     uint16_t idx;
86     struct vring_used_elem ring[1];
87 };
88 
89 struct vring
90 {
91     uint32_t num;
92 
93     struct vring_desc *desc;
94     struct vring_avail *avail;
95     struct vring_used *used;
96 };
97 
98 /* The standard layout for the ring is a continuous chunk of memory which
99  * looks like this.  We assume num is a power of 2.
100  *
101  * struct vring {
102  *      # The actual descriptors (16 bytes each)
103  *      struct vring_desc desc[num];
104  *
105  *      # A ring of available descriptor heads with free-running index.
106  *      __u16 avail_flags;
107  *      __u16 avail_idx;
108  *      __u16 available[num];
109  *      __u16 used_event_idx;
110  *
111  *      # Padding to the next align boundary.
112  *      char pad[];
113  *
114  *      # A ring of used descriptor heads with free-running index.
115  *      __u16 used_flags;
116  *      __u16 used_idx;
117  *      struct vring_used_elem used[num];
118  *      __u16 avail_event_idx;
119  * };
120  *
121  * NOTE: for VirtIO PCI, align is 4096.
122  */
123 
124 /*
125  * We publish the used event index at the end of the available ring, and vice
126  * versa. They are at the end for backwards compatibility.
127  */
128 #define vring_used_event(vr)  ((vr)->avail->ring[(vr)->num])
129 #define vring_avail_event(vr) ((vr)->used->ring[(vr)->num].id)
130 
vring_size(uint32_t num,uint32_t align)131 static inline int32_t vring_size(uint32_t num, uint32_t align)
132 {
133     uint32_t size;
134 
135     size = num * sizeof(struct vring_desc);
136     size += sizeof(struct vring_avail) + (num * sizeof(uint16_t)) + sizeof(uint16_t);
137     size = (size + align - 1UL) & ~(align - 1UL);
138     size += sizeof(struct vring_used) + (num * sizeof(struct vring_used_elem)) + sizeof(uint16_t);
139     return ((int32_t)size);
140 }
141 
vring_init(struct vring * vr,uint32_t num,uint8_t * p,uint32_t align)142 static inline void vring_init(struct vring *vr, uint32_t num, uint8_t *p, uint32_t align)
143 {
144     vr->num   = num;
145     vr->desc  = (struct vring_desc *)(void *)p;
146     vr->avail = (struct vring_avail *)(void *)(p + num * sizeof(struct vring_desc));
147     vr->used  = (struct vring_used *)(((uintptr_t)&vr->avail->ring[num] + align - 1UL) & ~(align - 1UL));
148 }
149 
150 /*
151  * The following is used with VIRTIO_RING_F_EVENT_IDX.
152  *
153  * Assuming a given event_idx value from the other size, if we have
154  * just incremented index from old to new_idx, should we trigger an
155  * event?
156  */
vring_need_event(uint16_t event_idx,uint16_t new_idx,uint16_t old)157 static inline int32_t vring_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
158 {
159     /* coco begin validated: This function does not need to be tested because it is not used in rpmsg_lite
160      * implementation (only called from unused part of vq_ring_must_notify_host() ). */
161     if ((uint16_t)(new_idx - event_idx - 1U) < (uint16_t)(new_idx - old))
162     {
163         return 1;
164     }
165     else
166     {
167         return 0;
168     }
169 }
170 /* coco end */
171 #endif /* VIRTIO_RING_H */
172