summaryrefslogtreecommitdiffstats
path: root/drivers/virtio/virtio_sandbox.c
blob: aafb7beb949d2c6d03f79df3d8445d6b53ec31f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
 *
 * VirtIO Sandbox transport driver, for testing purpose only
 */

#include <common.h>
#include <dm.h>
#include <virtio_types.h>
#include <virtio.h>
#include <virtio_ring.h>
#include <linux/bug.h>
#include <linux/compat.h>
#include <linux/err.h>
#include <linux/io.h>

struct virtio_sandbox_priv {
	u8 id;
	u8 status;
	u64 device_features;
	u64 driver_features;
	ulong queue_desc;
	ulong queue_available;
	ulong queue_used;
};

static int virtio_sandbox_get_config(struct udevice *udev, unsigned int offset,
				     void *buf, unsigned int len)
{
	return 0;
}

static int virtio_sandbox_set_config(struct udevice *udev, unsigned int offset,
				     const void *buf, unsigned int len)
{
	return 0;
}

static int virtio_sandbox_get_status(struct udevice *udev, u8 *status)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);

	*status = priv->status;

	return 0;
}

static int virtio_sandbox_set_status(struct udevice *udev, u8 status)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);

	/* We should never be setting status to 0 */
	WARN_ON(status == 0);

	priv->status = status;

	return 0;
}

static int virtio_sandbox_reset(struct udevice *udev)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);

	/* 0 status means a reset */
	priv->status = 0;

	return 0;
}

static int virtio_sandbox_get_features(struct udevice *udev, u64 *features)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);

	*features = priv->device_features;

	return 0;
}

static int virtio_sandbox_set_features(struct udevice *udev)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);
	struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);

	priv->driver_features = uc_priv->features;

	return 0;
}

static struct virtqueue *virtio_sandbox_setup_vq(struct udevice *udev,
						 unsigned int index)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);
	struct virtqueue *vq;
	ulong addr;
	int err;

	/* Create the vring */
	vq = vring_create_virtqueue(index, 4, 4096, udev);
	if (!vq) {
		err = -ENOMEM;
		goto error_new_virtqueue;
	}

	addr = virtqueue_get_desc_addr(vq);
	priv->queue_desc = addr;

	addr = virtqueue_get_avail_addr(vq);
	priv->queue_available = addr;

	addr = virtqueue_get_used_addr(vq);
	priv->queue_used = addr;

	return vq;

error_new_virtqueue:
	return ERR_PTR(err);
}

static void virtio_sandbox_del_vq(struct virtqueue *vq)
{
	vring_del_virtqueue(vq);
}

static int virtio_sandbox_del_vqs(struct udevice *udev)
{
	struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
	struct virtqueue *vq, *n;

	list_for_each_entry_safe(vq, n, &uc_priv->vqs, list)
		virtio_sandbox_del_vq(vq);

	return 0;
}

static int virtio_sandbox_find_vqs(struct udevice *udev, unsigned int nvqs,
				   struct virtqueue *vqs[])
{
	int i;

	for (i = 0; i < nvqs; ++i) {
		vqs[i] = virtio_sandbox_setup_vq(udev, i);
		if (IS_ERR(vqs[i])) {
			virtio_sandbox_del_vqs(udev);
			return PTR_ERR(vqs[i]);
		}
	}

	return 0;
}

static int virtio_sandbox_notify(struct udevice *udev, struct virtqueue *vq)
{
	return 0;
}

static int virtio_sandbox_probe(struct udevice *udev)
{
	struct virtio_sandbox_priv *priv = dev_get_priv(udev);
	struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);

	/* fake some information for testing */
	priv->device_features = VIRTIO_F_VERSION_1;
	uc_priv->device = VIRTIO_ID_BLOCK;
	uc_priv->vendor = ('u' << 24) | ('b' << 16) | ('o' << 8) | 't';

	return 0;
}

/* check virtio device driver's remove routine was called to reset the device */
static int virtio_sandbox_child_post_remove(struct udevice *vdev)
{
	u8 status;

	virtio_get_status(vdev, &status);
	if (status)
		panic("virtio device was not reset\n");

	return 0;
}

static const struct dm_virtio_ops virtio_sandbox1_ops = {
	.get_config	= virtio_sandbox_get_config,
	.set_config	= virtio_sandbox_set_config,
	.get_status	= virtio_sandbox_get_status,
	.set_status	= virtio_sandbox_set_status,
	.reset		= virtio_sandbox_reset,
	.get_features	= virtio_sandbox_get_features,
	.set_features	= virtio_sandbox_set_features,
	.find_vqs	= virtio_sandbox_find_vqs,
	.del_vqs	= virtio_sandbox_del_vqs,
	.notify		= virtio_sandbox_notify,
};

static const struct udevice_id virtio_sandbox1_ids[] = {
	{ .compatible = "sandbox,virtio1" },
	{ }
};

U_BOOT_DRIVER(virtio_sandbox1) = {
	.name	= "virtio-sandbox1",
	.id	= UCLASS_VIRTIO,
	.of_match = virtio_sandbox1_ids,
	.ops	= &virtio_sandbox1_ops,
	.probe	= virtio_sandbox_probe,
	.child_post_remove = virtio_sandbox_child_post_remove,
	.priv_auto	= sizeof(struct virtio_sandbox_priv),
};

/* this one without notify op */
static const struct dm_virtio_ops virtio_sandbox2_ops = {
	.get_config	= virtio_sandbox_get_config,
	.set_config	= virtio_sandbox_set_config,
	.get_status	= virtio_sandbox_get_status,
	.set_status	= virtio_sandbox_set_status,
	.reset		= virtio_sandbox_reset,
	.get_features	= virtio_sandbox_get_features,
	.set_features	= virtio_sandbox_set_features,
	.find_vqs	= virtio_sandbox_find_vqs,
	.del_vqs	= virtio_sandbox_del_vqs,
};

static const struct udevice_id virtio_sandbox2_ids[] = {
	{ .compatible = "sandbox,virtio2" },
	{ }
};

U_BOOT_DRIVER(virtio_sandbox2) = {
	.name	= "virtio-sandbox2",
	.id	= UCLASS_VIRTIO,
	.of_match = virtio_sandbox2_ids,
	.ops	= &virtio_sandbox2_ops,
	.probe	= virtio_sandbox_probe,
	.priv_auto	= sizeof(struct virtio_sandbox_priv),
};