source: mainline/uspace/lib/virtio/virtio.c@ 5f36841

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

Protect the virtqueue with a mutex

  • Property mode set to 100644
File size: 7.5 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
41void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t num, uint16_t descno,
42 uint64_t addr, uint32_t len, uint16_t flags, uint16_t next)
43{
44 virtq_desc_t *d = &vdev->queues[num].desc[descno];
45 pio_write_le64(&d->addr, addr);
46 pio_write_le32(&d->len, len);
47 pio_write_le16(&d->flags, flags);
48 pio_write_le16(&d->next, next);
49}
50
51uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t num,
52 uint16_t descno)
53{
54 virtq_desc_t *d = &vdev->queues[num].desc[descno];
55 if (!(pio_read_le16(&d->flags) & VIRTQ_DESC_F_NEXT))
56 return (uint16_t) -1U;
57 return pio_read_le16(&d->next);
58}
59
60void virtio_virtq_produce_available(virtio_dev_t *vdev, uint16_t num,
61 uint16_t descno)
62{
63 virtq_t *q = &vdev->queues[num];
64
65 fibril_mutex_lock(&q->lock);
66 uint16_t idx = pio_read_le16(&q->avail->idx);
67 pio_write_le16(&q->avail->ring[idx % q->queue_size], descno);
68 write_barrier();
69 pio_write_le16(&q->avail->idx, idx + 1);
70 write_barrier();
71 pio_write_le16(q->notify, num);
72 fibril_mutex_unlock(&q->lock);
73}
74
75bool virtio_virtq_consume_used(virtio_dev_t *vdev, uint16_t num,
76 uint16_t *descno, uint32_t *len)
77{
78 virtq_t *q = &vdev->queues[num];
79
80 fibril_mutex_lock(&q->lock);
81 uint16_t last_idx = q->used_last_idx % q->queue_size;
82 if (last_idx == (pio_read_le16(&q->used->idx) % q->queue_size)) {
83 fibril_mutex_unlock(&q->lock);
84 return false;
85 }
86
87 *descno = (uint16_t) pio_read_le32(&q->used->ring[last_idx].id);
88 *len = pio_read_le32(&q->used->ring[last_idx].len);
89
90 q->used_last_idx++;
91 fibril_mutex_unlock(&q->lock);
92
93 return true;
94}
95
96errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size)
97{
98 virtq_t *q = &vdev->queues[num];
99 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
100
101 /* Program the queue of our interest */
102 pio_write_le16(&cfg->queue_select, num);
103
104 /* Trim the size of the queue as needed */
105 if (size > pio_read_16(&cfg->queue_size)) {
106 ddf_msg(LVL_ERROR, "Virtq %u: not enough descriptors", num);
107 return ENOMEM;
108 }
109 pio_write_le16(&cfg->queue_size, size);
110 ddf_msg(LVL_NOTE, "Virtq %u: %u descriptors", num, (unsigned) size);
111
112 size_t avail_offset = 0;
113 size_t used_offset = 0;
114
115 /*
116 * Compute the size of the needed DMA memory and also the offsets of
117 * the individual components
118 */
119 size_t mem_size = sizeof(virtq_desc_t[size]);
120 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
121 avail_offset = mem_size;
122 mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
123 sizeof(ioport16_t);
124 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
125 used_offset = mem_size;
126 mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
127 sizeof(ioport16_t);
128
129 /*
130 * Allocate DMA memory for the virtqueues
131 */
132 q->virt = AS_AREA_ANY;
133 errno_t rc = dmamem_map_anonymous(mem_size, 0,
134 AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
135 if (rc != EOK) {
136 q->virt = NULL;
137 return rc;
138 }
139
140 fibril_mutex_initialize(&q->lock);
141
142 q->size = mem_size;
143 q->queue_size = size;
144 q->desc = q->virt;
145 q->avail = q->virt + avail_offset;
146 q->used = q->virt + used_offset;
147 q->used_last_idx = 0;
148
149 memset(q->virt, 0, q->size);
150
151 /*
152 * Write the configured addresses to device's common config
153 */
154 pio_write_le64(&cfg->queue_desc, q->phys);
155 pio_write_le64(&cfg->queue_avail, q->phys + avail_offset);
156 pio_write_le64(&cfg->queue_used, q->phys + used_offset);
157
158 ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
159 num, q->virt, (void *) q->phys, q->size);
160
161 /* Determine virtq's notification address */
162 q->notify = vdev->notify_base +
163 pio_read_le16(&cfg->queue_notif_off) * vdev->notify_off_multiplier;
164
165 ddf_msg(LVL_NOTE, "notification register: %p", q->notify);
166
167 /* Enable the queue */
168 pio_write_le16(&cfg->queue_enable, 1);
169 ddf_msg(LVL_NOTE, "virtq %d set", num);
170
171 return rc;
172}
173
174void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
175{
176 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
177
178 /* Disable the queue */
179 pio_write_le16(&cfg->queue_enable, 0);
180
181 virtq_t *q = &vdev->queues[num];
182 if (q->size)
183 dmamem_unmap_anonymous(q->virt);
184}
185
186/**
187 * Perform device initialization as described in section 3.1.1 of the
188 * specification, steps 1 - 6.
189 */
190errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
191{
192 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
193
194 /* 1. Reset the device */
195 uint8_t status = VIRTIO_DEV_STATUS_RESET;
196 pio_write_8(&cfg->device_status, status);
197
198 /* 2. Acknowledge we found the device */
199 status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
200 pio_write_8(&cfg->device_status, status);
201
202 /* 3. We know how to drive the device */
203 status |= VIRTIO_DEV_STATUS_DRIVER;
204 pio_write_8(&cfg->device_status, status);
205
206 /* 4. Read the offered feature flags */
207 pio_write_le32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
208 uint32_t device_features = pio_read_le32(&cfg->device_feature);
209
210 ddf_msg(LVL_NOTE, "offered features %x", device_features);
211
212 if (features != (features & device_features))
213 return ENOTSUP;
214 features &= device_features;
215
216 /* 4. Write the accepted feature flags */
217 pio_write_le32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
218 pio_write_le32(&cfg->driver_feature, features);
219
220 ddf_msg(LVL_NOTE, "accepted features %x", features);
221
222 /* 5. Set FEATURES_OK */
223 status |= VIRTIO_DEV_STATUS_FEATURES_OK;
224 pio_write_8(&cfg->device_status, status);
225
226 /* 6. Test if the device supports our feature subset */
227 status = pio_read_8(&cfg->device_status);
228 if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
229 return ENOTSUP;
230
231 return EOK;
232}
233
234/**
235 * Perform device initialization as described in section 3.1.1 of the
236 * specification, step 8 (go live).
237 */
238void virtio_device_setup_finalize(virtio_dev_t *vdev)
239{
240 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
241
242 /* 8. Go live */
243 uint8_t status = pio_read_8(&cfg->device_status);
244 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
245}
246
247void virtio_device_setup_fail(virtio_dev_t *vdev)
248{
249 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
250
251 uint8_t status = pio_read_8(&cfg->device_status);
252 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
253}
254
255/** @}
256 */
Note: See TracBrowser for help on using the repository browser.