1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 * VirtIO Sandbox transport driver, for testing purpose only
9 #include <virtio_types.h>
11 #include <virtio_ring.h>
12 #include <linux/bug.h>
13 #include <linux/compat.h>
14 #include <linux/err.h>
17 struct virtio_sandbox_priv {
23 ulong queue_available;
27 static int virtio_sandbox_get_config(struct udevice *udev, unsigned int offset,
28 void *buf, unsigned int len)
33 static int virtio_sandbox_set_config(struct udevice *udev, unsigned int offset,
34 const void *buf, unsigned int len)
39 static int virtio_sandbox_get_status(struct udevice *udev, u8 *status)
41 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
43 *status = priv->status;
48 static int virtio_sandbox_set_status(struct udevice *udev, u8 status)
50 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
52 /* We should never be setting status to 0 */
55 priv->status = status;
60 static int virtio_sandbox_reset(struct udevice *udev)
62 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
64 /* 0 status means a reset */
70 static int virtio_sandbox_get_features(struct udevice *udev, u64 *features)
72 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
74 *features = priv->device_features;
79 static int virtio_sandbox_set_features(struct udevice *udev)
81 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
82 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
84 priv->driver_features = uc_priv->features;
89 static struct virtqueue *virtio_sandbox_setup_vq(struct udevice *udev,
92 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
97 /* Create the vring */
98 vq = vring_create_virtqueue(index, 4, 4096, udev);
101 goto error_new_virtqueue;
104 addr = virtqueue_get_desc_addr(vq);
105 priv->queue_desc = addr;
107 addr = virtqueue_get_avail_addr(vq);
108 priv->queue_available = addr;
110 addr = virtqueue_get_used_addr(vq);
111 priv->queue_used = addr;
119 static void virtio_sandbox_del_vq(struct virtqueue *vq)
121 vring_del_virtqueue(vq);
124 static int virtio_sandbox_del_vqs(struct udevice *udev)
126 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
127 struct virtqueue *vq, *n;
129 list_for_each_entry_safe(vq, n, &uc_priv->vqs, list)
130 virtio_sandbox_del_vq(vq);
135 static int virtio_sandbox_find_vqs(struct udevice *udev, unsigned int nvqs,
136 struct virtqueue *vqs[])
140 for (i = 0; i < nvqs; ++i) {
141 vqs[i] = virtio_sandbox_setup_vq(udev, i);
142 if (IS_ERR(vqs[i])) {
143 virtio_sandbox_del_vqs(udev);
144 return PTR_ERR(vqs[i]);
151 static int virtio_sandbox_notify(struct udevice *udev, struct virtqueue *vq)
156 static int virtio_sandbox_probe(struct udevice *udev)
158 struct virtio_sandbox_priv *priv = dev_get_priv(udev);
159 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
161 /* fake some information for testing */
162 priv->device_features = BIT_ULL(VIRTIO_F_VERSION_1);
163 uc_priv->device = dev_read_u32_default(udev, "virtio-type",
165 uc_priv->vendor = ('u' << 24) | ('b' << 16) | ('o' << 8) | 't';
170 static const struct dm_virtio_ops virtio_sandbox1_ops = {
171 .get_config = virtio_sandbox_get_config,
172 .set_config = virtio_sandbox_set_config,
173 .get_status = virtio_sandbox_get_status,
174 .set_status = virtio_sandbox_set_status,
175 .reset = virtio_sandbox_reset,
176 .get_features = virtio_sandbox_get_features,
177 .set_features = virtio_sandbox_set_features,
178 .find_vqs = virtio_sandbox_find_vqs,
179 .del_vqs = virtio_sandbox_del_vqs,
180 .notify = virtio_sandbox_notify,
183 static const struct udevice_id virtio_sandbox1_ids[] = {
184 { .compatible = "sandbox,virtio1" },
188 U_BOOT_DRIVER(virtio_sandbox1) = {
189 .name = "virtio-sandbox1",
191 .of_match = virtio_sandbox1_ids,
192 .ops = &virtio_sandbox1_ops,
193 .probe = virtio_sandbox_probe,
194 .priv_auto = sizeof(struct virtio_sandbox_priv),
197 /* this one without notify op */
198 static const struct dm_virtio_ops virtio_sandbox2_ops = {
199 .get_config = virtio_sandbox_get_config,
200 .set_config = virtio_sandbox_set_config,
201 .get_status = virtio_sandbox_get_status,
202 .set_status = virtio_sandbox_set_status,
203 .reset = virtio_sandbox_reset,
204 .get_features = virtio_sandbox_get_features,
205 .set_features = virtio_sandbox_set_features,
206 .find_vqs = virtio_sandbox_find_vqs,
207 .del_vqs = virtio_sandbox_del_vqs,
210 static const struct udevice_id virtio_sandbox2_ids[] = {
211 { .compatible = "sandbox,virtio2" },
215 U_BOOT_DRIVER(virtio_sandbox2) = {
216 .name = "virtio-sandbox2",
218 .of_match = virtio_sandbox2_ids,
219 .ops = &virtio_sandbox2_ops,
220 .probe = virtio_sandbox_probe,
221 .priv_auto = sizeof(struct virtio_sandbox_priv),