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