source: mainline/uspace/lib/virtio/virtio.c@ 1e472ee

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e472ee was 1e472ee, checked in by Jakub Jermar <jakub@…>, 7 years ago

Move DMA buffers allocation functions to libvirtio

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2018 Jakub Jermar
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
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
29/** @file VIRTIO support
30 */
31
32#include "virtio-pci.h"
33
34#include <as.h>
35#include <align.h>
36#include <macros.h>
37
38#include <ddf/log.h>
39#include <libarch/barrier.h>
40
41/** Allocate DMA buffers
42 *
43 * @param buffers[in] Number of buffers to allocate.
44 * @param size[in] Size of each buffer.
45 * @param write[in] True if the buffers are writable by the driver, false
46 * otherwise.
47 * @param buf[out] Output array holding virtual addresses of the allocated
48 * buffers.
49 * @param buf_p[out] Output array holding physical addresses of the allocated
50 * buffers.
51 *
52 * The buffers can be deallocated by virtio_net_teardown_bufs().
53 *
54 * @return EOK on success or error code.
55 */
56errno_t virtio_setup_dma_bufs(unsigned int buffers, size_t size,
57 bool write, void *buf[], uintptr_t buf_p[])
58{
59 /*
60 * Allocate all buffers at once in one large chunk.
61 */
62 void *virt = AS_AREA_ANY;
63 uintptr_t phys;
64 errno_t rc = dmamem_map_anonymous(buffers * size, 0,
65 write ? AS_AREA_WRITE : AS_AREA_READ, 0, &phys, &virt);
66 if (rc != EOK)
67 return rc;
68
69 ddf_msg(LVL_NOTE, "DMA buffers: %p-%p", virt, virt + buffers * size);
70
71 /*
72 * Calculate addresses of the individual buffers for easy access.
73 */
74 for (unsigned i = 0; i < buffers; i++) {
75 buf[i] = virt + i * size;
76 buf_p[i] = phys + i * size;
77 }
78
79 return EOK;
80}
81
82/** Deallocate DMA buffers
83 *
84 * @param buf[in] Array holding the virtual addresses of the DMA buffers
85 * previously allocated by virtio_net_setup_bufs().
86 */
87extern void virtio_teardown_dma_bufs(void *buf[])
88{
89 if (buf[0]) {
90 dmamem_unmap_anonymous(buf[0]);
91 buf[0] = NULL;
92 }
93}
94
95void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t num, uint16_t descno,
96 uint64_t addr, uint32_t len, uint16_t flags, uint16_t next)
97{
98 virtq_desc_t *d = &vdev->queues[num].desc[descno];
99 pio_write_le64(&d->addr, addr);
100 pio_write_le32(&d->len, len);
101 pio_write_le16(&d->flags, flags);
102 pio_write_le16(&d->next, next);
103}
104
105uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t num,
106 uint16_t descno)
107{
108 virtq_desc_t *d = &vdev->queues[num].desc[descno];
109 if (!(pio_read_le16(&d->flags) & VIRTQ_DESC_F_NEXT))
110 return (uint16_t) -1U;
111 return pio_read_le16(&d->next);
112}
113
114/** Create free descriptor list from the unused VIRTIO descriptors
115 *
116 * @param vdev[in] VIRTIO device for which the free list will be created.
117 * @param num[in] Index of the virtqueue for which the free list will be
118 * created.
119 * @param size[in] Number of descriptors on the free list. The free list will
120 * contain descriptors starting from 0 to \a size - 1.
121 * @param head[out] Variable that will hold the VIRTIO descriptor at the head
122 * of the free list.
123 */
124void virtio_create_desc_free_list(virtio_dev_t *vdev, uint16_t num,
125 uint16_t size, uint16_t *head)
126{
127 for (unsigned i = 0; i < size; i++) {
128 virtio_virtq_desc_set(vdev, num, i, 0, 0,
129 VIRTQ_DESC_F_NEXT, (i + 1 == size) ? -1U : i + 1);
130 }
131 *head = 0;
132}
133
134/** Allocate a descriptor from the free list
135 *
136 * @param vdev[in] VIRTIO device with the free list.
137 * @param num[in] Index of the virtqueue with free list.
138 * @param head[in,out] Head of the free list.
139 *
140 * @return Allocated descriptor or 0xFFFF if the list is empty.
141 */
142uint16_t virtio_alloc_desc(virtio_dev_t *vdev, uint16_t num, uint16_t *head)
143{
144 virtq_t *q = &vdev->queues[num];
145 fibril_mutex_lock(&q->lock);
146 uint16_t descno = *head;
147 if (descno != (uint16_t) -1U)
148 *head = virtio_virtq_desc_get_next(vdev, num, descno);
149 fibril_mutex_unlock(&q->lock);
150 return descno;
151}
152
153/** Free a descriptor into the free list
154 *
155 * @param vdev[in] VIRTIO device with the free list.
156 * @param num[in] Index of the virtqueue with free list.
157 * @param head[in,out] Head of the free list.
158 * @param descno[in] The freed descriptor.
159 */
160void virtio_free_desc(virtio_dev_t *vdev, uint16_t num, uint16_t *head,
161 uint16_t descno)
162{
163 virtq_t *q = &vdev->queues[num];
164 fibril_mutex_lock(&q->lock);
165 virtio_virtq_desc_set(vdev, num, descno, 0, 0, VIRTQ_DESC_F_NEXT,
166 *head);
167 *head = descno;
168 fibril_mutex_unlock(&q->lock);
169}
170
171
172void virtio_virtq_produce_available(virtio_dev_t *vdev, uint16_t num,
173 uint16_t descno)
174{
175 virtq_t *q = &vdev->queues[num];
176
177 fibril_mutex_lock(&q->lock);
178 uint16_t idx = pio_read_le16(&q->avail->idx);
179 pio_write_le16(&q->avail->ring[idx % q->queue_size], descno);
180 write_barrier();
181 pio_write_le16(&q->avail->idx, idx + 1);
182 write_barrier();
183 pio_write_le16(q->notify, num);
184 fibril_mutex_unlock(&q->lock);
185}
186
187bool virtio_virtq_consume_used(virtio_dev_t *vdev, uint16_t num,
188 uint16_t *descno, uint32_t *len)
189{
190 virtq_t *q = &vdev->queues[num];
191
192 fibril_mutex_lock(&q->lock);
193 uint16_t last_idx = q->used_last_idx % q->queue_size;
194 if (last_idx == (pio_read_le16(&q->used->idx) % q->queue_size)) {
195 fibril_mutex_unlock(&q->lock);
196 return false;
197 }
198
199 *descno = (uint16_t) pio_read_le32(&q->used->ring[last_idx].id);
200 *len = pio_read_le32(&q->used->ring[last_idx].len);
201
202 q->used_last_idx++;
203 fibril_mutex_unlock(&q->lock);
204
205 return true;
206}
207
208errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size)
209{
210 virtq_t *q = &vdev->queues[num];
211 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
212
213 /* Program the queue of our interest */
214 pio_write_le16(&cfg->queue_select, num);
215
216 /* Trim the size of the queue as needed */
217 if (size > pio_read_16(&cfg->queue_size)) {
218 ddf_msg(LVL_ERROR, "Virtq %u: not enough descriptors", num);
219 return ENOMEM;
220 }
221 pio_write_le16(&cfg->queue_size, size);
222 ddf_msg(LVL_NOTE, "Virtq %u: %u descriptors", num, (unsigned) size);
223
224 size_t avail_offset = 0;
225 size_t used_offset = 0;
226
227 /*
228 * Compute the size of the needed DMA memory and also the offsets of
229 * the individual components
230 */
231 size_t mem_size = sizeof(virtq_desc_t[size]);
232 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
233 avail_offset = mem_size;
234 mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
235 sizeof(ioport16_t);
236 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
237 used_offset = mem_size;
238 mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
239 sizeof(ioport16_t);
240
241 /*
242 * Allocate DMA memory for the virtqueues
243 */
244 q->virt = AS_AREA_ANY;
245 errno_t rc = dmamem_map_anonymous(mem_size, 0,
246 AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
247 if (rc != EOK) {
248 q->virt = NULL;
249 return rc;
250 }
251
252 fibril_mutex_initialize(&q->lock);
253
254 q->size = mem_size;
255 q->queue_size = size;
256 q->desc = q->virt;
257 q->avail = q->virt + avail_offset;
258 q->used = q->virt + used_offset;
259 q->used_last_idx = 0;
260
261 memset(q->virt, 0, q->size);
262
263 /*
264 * Write the configured addresses to device's common config
265 */
266 pio_write_le64(&cfg->queue_desc, q->phys);
267 pio_write_le64(&cfg->queue_avail, q->phys + avail_offset);
268 pio_write_le64(&cfg->queue_used, q->phys + used_offset);
269
270 ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
271 num, q->virt, (void *) q->phys, q->size);
272
273 /* Determine virtq's notification address */
274 q->notify = vdev->notify_base +
275 pio_read_le16(&cfg->queue_notif_off) * vdev->notify_off_multiplier;
276
277 ddf_msg(LVL_NOTE, "notification register: %p", q->notify);
278
279 /* Enable the queue */
280 pio_write_le16(&cfg->queue_enable, 1);
281 ddf_msg(LVL_NOTE, "virtq %d set", num);
282
283 return rc;
284}
285
286void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
287{
288 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
289
290 /* Disable the queue */
291 pio_write_le16(&cfg->queue_enable, 0);
292
293 virtq_t *q = &vdev->queues[num];
294 if (q->size)
295 dmamem_unmap_anonymous(q->virt);
296}
297
298/**
299 * Perform device initialization as described in section 3.1.1 of the
300 * specification, steps 1 - 6.
301 */
302errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
303{
304 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
305
306 /* 1. Reset the device */
307 uint8_t status = VIRTIO_DEV_STATUS_RESET;
308 pio_write_8(&cfg->device_status, status);
309
310 /* 2. Acknowledge we found the device */
311 status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
312 pio_write_8(&cfg->device_status, status);
313
314 /* 3. We know how to drive the device */
315 status |= VIRTIO_DEV_STATUS_DRIVER;
316 pio_write_8(&cfg->device_status, status);
317
318 /* 4. Read the offered feature flags */
319 pio_write_le32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
320 uint32_t device_features = pio_read_le32(&cfg->device_feature);
321
322 ddf_msg(LVL_NOTE, "offered features %x", device_features);
323
324 if (features != (features & device_features))
325 return ENOTSUP;
326 features &= device_features;
327
328 /* 4. Write the accepted feature flags */
329 pio_write_le32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
330 pio_write_le32(&cfg->driver_feature, features);
331
332 ddf_msg(LVL_NOTE, "accepted features %x", features);
333
334 /* 5. Set FEATURES_OK */
335 status |= VIRTIO_DEV_STATUS_FEATURES_OK;
336 pio_write_8(&cfg->device_status, status);
337
338 /* 6. Test if the device supports our feature subset */
339 status = pio_read_8(&cfg->device_status);
340 if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
341 return ENOTSUP;
342
343 return EOK;
344}
345
346/**
347 * Perform device initialization as described in section 3.1.1 of the
348 * specification, step 8 (go live).
349 */
350void virtio_device_setup_finalize(virtio_dev_t *vdev)
351{
352 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
353
354 /* 8. Go live */
355 uint8_t status = pio_read_8(&cfg->device_status);
356 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
357}
358
359void virtio_device_setup_fail(virtio_dev_t *vdev)
360{
361 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
362
363 uint8_t status = pio_read_8(&cfg->device_status);
364 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
365}
366
367/** @}
368 */
Note: See TracBrowser for help on using the repository browser.