source: mainline/uspace/lib/virtio/virtio.c@ 2d4faf7

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

Split generic and PCI specific virtio code

  • Property mode set to 100644
File size: 6.0 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
40errno_t virtio_virtq_setup(virtio_dev_t *vdev, uint16_t num, uint16_t size,
41 size_t buf_size, uint16_t buf_flags)
42{
43 virtq_t *q = &vdev->queues[num];
44 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
45
46 /* Program the queue of our interest */
47 pio_write_16(&cfg->queue_select, num);
48
49 /* Trim the size of the queue as needed */
50 size = min(pio_read_16(&cfg->queue_size), size);
51 pio_write_16(&cfg->queue_size, size);
52 ddf_msg(LVL_NOTE, "Virtq %u: %u buffers", num, (unsigned) size);
53
54 /* Allocate array to hold virtual addresses of DMA buffers */
55 void **buffers = calloc(sizeof(void *), size);
56 if (!buffers)
57 return ENOMEM;
58
59 size_t avail_offset = 0;
60 size_t used_offset = 0;
61 size_t buffers_offset = 0;
62
63 /*
64 * Compute the size of the needed DMA memory and also the offsets of
65 * the individual components
66 */
67 size_t mem_size = sizeof(virtq_desc_t[size]);
68 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_avail_t));
69 avail_offset = mem_size;
70 mem_size += sizeof(virtq_avail_t) + sizeof(ioport16_t[size]) +
71 sizeof(ioport16_t);
72 mem_size = ALIGN_UP(mem_size, _Alignof(virtq_used_t));
73 used_offset = mem_size;
74 mem_size += sizeof(virtq_used_t) + sizeof(virtq_used_elem_t[size]) +
75 sizeof(ioport16_t);
76 buffers_offset = mem_size;
77 mem_size += size * buf_size;
78
79 /*
80 * Allocate DMA memory for the virtqueues and the buffers
81 */
82 q->virt = AS_AREA_ANY;
83 errno_t rc = dmamem_map_anonymous(mem_size, DMAMEM_4GiB,
84 AS_AREA_READ | AS_AREA_WRITE, 0, &q->phys, &q->virt);
85 if (rc != EOK) {
86 free(buffers);
87 q->virt = NULL;
88 return rc;
89 }
90
91 q->size = mem_size;
92 q->queue_size = size;
93 q->desc = q->virt;
94 q->avail = q->virt + avail_offset;
95 q->used = q->virt + used_offset;
96 q->buffers = buffers;
97
98 memset(q->virt, 0, q->size);
99
100 /*
101 * Initialize the descriptor table and the buffers array
102 */
103 for (unsigned i = 0; i < size; i++) {
104 q->desc[i].addr = q->phys + buffers_offset + i * buf_size;
105 q->desc[i].len = buf_size;
106 q->desc[i].flags = buf_flags;
107
108 q->buffers[i] = q->virt + buffers_offset + i * buf_size;
109 }
110
111 /*
112 * Write the configured addresses to device's common config
113 */
114 pio_write_64(&cfg->queue_desc, q->phys);
115 pio_write_64(&cfg->queue_avail, q->phys + avail_offset);
116 pio_write_64(&cfg->queue_used, q->phys + used_offset);
117
118 ddf_msg(LVL_NOTE, "DMA memory for virtq %d: virt=%p, phys=%p, size=%zu",
119 num, q->virt, (void *) q->phys, q->size);
120
121 return rc;
122}
123
124void virtio_virtq_teardown(virtio_dev_t *vdev, uint16_t num)
125{
126 virtq_t *q = &vdev->queues[num];
127 if (q->size)
128 dmamem_unmap_anonymous(q->virt);
129 if (q->buffers)
130 free(q->buffers);
131}
132
133/**
134 * Perform device initialization as described in section 3.1.1 of the
135 * specification, steps 1 - 6.
136 */
137errno_t virtio_device_setup_start(virtio_dev_t *vdev, uint32_t features)
138{
139 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
140
141 /* 1. Reset the device */
142 uint8_t status = VIRTIO_DEV_STATUS_RESET;
143 pio_write_8(&cfg->device_status, status);
144
145 /* 2. Acknowledge we found the device */
146 status |= VIRTIO_DEV_STATUS_ACKNOWLEDGE;
147 pio_write_8(&cfg->device_status, status);
148
149 /* 3. We know how to drive the device */
150 status |= VIRTIO_DEV_STATUS_DRIVER;
151 pio_write_8(&cfg->device_status, status);
152
153 /* 4. Read the offered feature flags */
154 pio_write_32(&cfg->device_feature_select, VIRTIO_FEATURES_0_31);
155 uint32_t device_features = pio_read_32(&cfg->device_feature);
156
157 ddf_msg(LVL_NOTE, "offered features %x", device_features);
158 features &= device_features;
159
160 if (!features)
161 return ENOTSUP;
162
163 /* 4. Write the accepted feature flags */
164 pio_write_32(&cfg->driver_feature_select, VIRTIO_FEATURES_0_31);
165 pio_write_32(&cfg->driver_feature, features);
166
167 ddf_msg(LVL_NOTE, "accepted features %x", features);
168
169 /* 5. Set FEATURES_OK */
170 status |= VIRTIO_DEV_STATUS_FEATURES_OK;
171 pio_write_8(&cfg->device_status, status);
172
173 /* 6. Test if the device supports our feature subset */
174 status = pio_read_8(&cfg->device_status);
175 if (!(status & VIRTIO_DEV_STATUS_FEATURES_OK))
176 return ENOTSUP;
177
178 return EOK;
179}
180
181/**
182 * Perform device initialization as described in section 3.1.1 of the
183 * specification, step 8 (go live).
184 */
185void virtio_device_setup_finalize(virtio_dev_t *vdev)
186{
187 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
188
189 /* 8. Go live */
190 uint8_t status = pio_read_8(&cfg->device_status);
191 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_DRIVER_OK);
192}
193
194void virtio_device_setup_fail(virtio_dev_t *vdev)
195{
196 virtio_pci_common_cfg_t *cfg = vdev->common_cfg;
197
198 uint8_t status = pio_read_8(&cfg->device_status);
199 pio_write_8(&cfg->device_status, status | VIRTIO_DEV_STATUS_FAILED);
200}
201
202/** @}
203 */
Note: See TracBrowser for help on using the repository browser.