summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Fatoum <ahmad@a3f.at>2021-02-22 08:06:00 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-02-22 10:24:33 +0100
commit2b772387518630af946e222e97af4dd925bce7b9 (patch)
tree5492cd7f2cc1247202ed8561023b8234c4fdebce
parent3e36b143754e8a0b8e5070b6f8138e9e18f32fdd (diff)
downloadbarebox-2b772387518630af946e222e97af4dd925bce7b9.tar.gz
barebox-2b772387518630af946e222e97af4dd925bce7b9.tar.xz
hw_random: add VirtIO RNG driver
With this driver enabled, -device virtio-rng-device can now be passed to Qemu for barebox to detect a VirtIO RNG device. If barebox is passed as argument to the Qemu -kernel option, no device tree changes are necessary. Signed-off-by: Ahmad Fatoum <ahmad@a3f.at> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--drivers/hw_random/Kconfig7
-rw-r--r--drivers/hw_random/Makefile1
-rw-r--r--drivers/hw_random/virtio-rng.c120
3 files changed, 128 insertions, 0 deletions
diff --git a/drivers/hw_random/Kconfig b/drivers/hw_random/Kconfig
index 1923c755db..a84c03efef 100644
--- a/drivers/hw_random/Kconfig
+++ b/drivers/hw_random/Kconfig
@@ -29,4 +29,11 @@ config HWRNG_DEV_RANDOM
This driver allows use of the host provided /dev/urandom
as barebox HWRNGs.
+config HW_RANDOM_VIRTIO
+ tristate "VirtIO Random Number Generator support"
+ depends on VIRTIO
+ help
+ This driver provides guest-side support for the virtual Random Number
+ Generator hardware.
+
endif
diff --git a/drivers/hw_random/Makefile b/drivers/hw_random/Makefile
index 2e318be738..4bab3967fc 100644
--- a/drivers/hw_random/Makefile
+++ b/drivers/hw_random/Makefile
@@ -2,3 +2,4 @@ obj-$(CONFIG_HWRNG) += core.o
obj-$(CONFIG_HWRNG_MXC_RNGC) += mxc-rngc.o
obj-$(CONFIG_HWRNG_STM32) += stm32-rng.o
obj-$(CONFIG_HWRNG_DEV_RANDOM) += dev-random.o
+obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
diff --git a/drivers/hw_random/virtio-rng.c b/drivers/hw_random/virtio-rng.c
new file mode 100644
index 0000000000..fbf1a5715a
--- /dev/null
+++ b/drivers/hw_random/virtio-rng.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Randomness driver for virtio
+ * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
+ */
+
+#include <common.h>
+#include <linux/err.h>
+#include <linux/hw_random.h>
+#include <linux/spinlock.h>
+#include <linux/virtio.h>
+#include <linux/virtio_rng.h>
+#include <linux/virtio_ring.h>
+#include <module.h>
+#include <linux/slab.h>
+
+#define BUFFER_SIZE 16UL
+
+struct virtrng_info {
+ struct hwrng hwrng;
+ char name[25];
+ struct virtqueue *rng_vq;
+ bool hwrng_register_done;
+};
+
+static inline struct virtrng_info *to_virtrng_info(struct hwrng *hwrng)
+{
+ return container_of(hwrng, struct virtrng_info, hwrng);
+}
+
+static int virtio_rng_read(struct hwrng *hwrng, void *data, size_t len, bool wait)
+{
+ int ret;
+ unsigned int rsize;
+ unsigned char buf[BUFFER_SIZE] __aligned(4);
+ unsigned char *ptr = data;
+ struct virtio_sg sg;
+ struct virtio_sg *sgs[1];
+ struct virtrng_info *vi = to_virtrng_info(hwrng);
+ size_t remaining = len;
+
+ while (remaining) {
+ sg.addr = buf;
+ sg.length = min(remaining, sizeof(buf));
+ sgs[0] = &sg;
+
+ ret = virtqueue_add(vi->rng_vq, sgs, 0, 1);
+ if (ret)
+ return ret;
+
+ virtqueue_kick(vi->rng_vq);
+
+ while (!virtqueue_get_buf(vi->rng_vq, &rsize))
+ ;
+
+ memcpy(ptr, buf, rsize);
+ remaining -= rsize;
+ ptr += rsize;
+ }
+
+ return len;
+}
+
+static int probe_common(struct virtio_device *vdev)
+{
+ struct virtrng_info *vi;
+
+ vi = xzalloc(sizeof(*vi));
+
+ vi->hwrng.name = vdev->dev.name;
+ vi->hwrng.read = virtio_rng_read;
+
+ vdev->priv = vi;
+
+ /* We expect a single virtqueue. */
+ return virtio_find_vqs(vdev, 1, &vi->rng_vq);
+}
+
+static void remove_common(struct virtio_device *vdev)
+{
+ vdev->config->reset(vdev);
+ vdev->config->del_vqs(vdev);
+}
+
+static int virtrng_probe(struct virtio_device *vdev)
+{
+ return probe_common(vdev);
+}
+
+static void virtrng_remove(struct virtio_device *vdev)
+{
+ remove_common(vdev);
+}
+
+static void virtrng_scan(struct virtio_device *vdev)
+{
+ struct virtrng_info *vi = vdev->priv;
+ int err;
+
+ err = hwrng_register(&vdev->dev, &vi->hwrng);
+ if (!err)
+ vi->hwrng_register_done = true;
+}
+
+static const struct virtio_device_id id_table[] = {
+ { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
+ { 0 },
+};
+
+static struct virtio_driver virtio_rng_driver = {
+ .driver.name = "virtio-rng",
+ .id_table = id_table,
+ .probe = virtrng_probe,
+ .remove = virtrng_remove,
+ .scan = virtrng_scan,
+};
+
+module_virtio_driver(virtio_rng_driver);
+MODULE_DESCRIPTION("Virtio random number driver");
+MODULE_LICENSE("GPL");