Skip to contentSkip to conversation

lib: utils/serial: add virtio mmio driver

PATCH New Bohdan Fedorov 2 participants 3 messages Jun 12, 2022 – Jun 18, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib: utils/serial: add virtio mmio driver

Simple virtio-console driver.
Useful for running opensbi in virtualized environments.

dt-bindings:
<linux>/Documentation/devicetree/bindings/virtio/mmio.yaml

---
 include/sbi_utils/fdt/fdt_helper.h     |  3 +
 include/sbi_utils/serial/virtio-uart.h | 19 +++++++
 lib/utils/fdt/fdt_helper.c             | 18 ++++++
 lib/utils/serial/fdt_serial_virtio.c   | 37 +++++++++++++
 lib/utils/serial/objects.mk            |  4 ++
 lib/utils/serial/virtio-uart.c         | 76 ++++++++++++++++++++++++++
 6 files changed, 157 insertions(+)
 create mode 100644 include/sbi_utils/serial/virtio-uart.h
 create mode 100644 lib/utils/serial/fdt_serial_virtio.c
 create mode 100644 lib/utils/serial/virtio-uart.c
include/sbi_utils/fdt/fdt_helper.h +3 -0
index c60af35..80307bb 100644
@@ -65,6 +65,9 @@ int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset,
 int fdt_parse_sifive_uart_node(void *fdt, int nodeoffset,
 			       struct platform_uart_data *uart);
 
+int fdt_parse_virtio_uart_node(void *fdt, int nodeoffset,
+			       struct platform_uart_data *uart);
+
 int fdt_parse_uart8250_node(void *fdt, int nodeoffset,
 			    struct platform_uart_data *uart);
 
include/sbi_utils/serial/virtio-uart.h new file +19 -0
new file mode 100644
index 0000000..4fdfebc
@@ -0,0 +1,19 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Bohdan Fedorov <[email protected]>
+ *
+ * Based on:
+ *   sifive-uart
+ */
+
+#ifndef __SERIAL_SIFIVE_UART_H__
+#define __SERIAL_SIFIVE_UART_H__
+
+
+int virtio_uart_init(unsigned long base);
+
+#endif
lib/utils/fdt/fdt_helper.c +18 -0
index 2bdb1ef..9bfd5e7 100644
@@ -388,6 +388,24 @@ int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset,
 	return 0;
 }
 
+int fdt_parse_virtio_uart_node(void *fdt, int nodeoffset,
+			       struct platform_uart_data *uart)
+{
+	int rc;
+	uint64_t reg_addr, reg_size;
+
+	if (nodeoffset < 0 || !uart || !fdt)
+		return SBI_ENODEV;
+
+	rc = fdt_get_node_addr_size(fdt, nodeoffset, 0,
+				    &reg_addr, &reg_size);
+	if (rc < 0 || !reg_addr || !reg_size)
+		return SBI_ENODEV;
+	uart->addr = reg_addr;
+
+	return 0;
+}
+
 int fdt_parse_sifive_uart_node(void *fdt, int nodeoffset,
 			       struct platform_uart_data *uart)
 {
lib/utils/serial/fdt_serial_virtio.c new file +37 -0
new file mode 100644
index 0000000..115c5ad
@@ -0,0 +1,37 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Bohdan Fedorov <[email protected]>
+ *
+ * Based on virtio-uart
+ */
+
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/serial/fdt_serial.h>
+#include <sbi_utils/serial/virtio-uart.h>
+
+static int serial_virtio_init(void *fdt, int nodeoff,
+				const struct fdt_match *match)
+{
+	int rc;
+	struct platform_uart_data uart;
+
+	rc = fdt_parse_virtio_uart_node(fdt, nodeoff, &uart);
+	if (rc)
+		return rc;
+
+	return virtio_uart_init(uart.addr);
+}
+
+static const struct fdt_match serial_virtio_match[] = {
+	{ .compatible = "virtio,mmio" },
+	{ },
+};
+
+struct fdt_serial fdt_serial_virtio = {
+	.match_table = serial_virtio_match,
+	.init = serial_virtio_init
+};
lib/utils/serial/objects.mk +4 -0
index d26a74e..60e829b 100644
@@ -22,6 +22,9 @@ libsbiutils-objs-y += serial/fdt_serial_shakti.o
 carray-fdt_serial_drivers-y += fdt_serial_sifive
 libsbiutils-objs-y += serial/fdt_serial_sifive.o
 
+carray-fdt_serial_drivers-y += fdt_serial_virtio
+libsbiutils-objs-y += serial/fdt_serial_virtio.o
+
 carray-fdt_serial_drivers-y += fdt_serial_litex
 libsbiutils-objs-y += serial/fdt_serial_litex.o
 
@@ -34,6 +37,7 @@ libsbiutils-objs-y += serial/fdt_serial_xlnx_uartlite.o
 libsbiutils-objs-y += serial/gaisler-uart.o
 libsbiutils-objs-y += serial/shakti-uart.o
 libsbiutils-objs-y += serial/sifive-uart.o
+libsbiutils-objs-y += serial/virtio-uart.o
 libsbiutils-objs-y += serial/litex-uart.o
 libsbiutils-objs-y += serial/uart8250.o
 libsbiutils-objs-y += serial/xlnx-uartlite.o
lib/utils/serial/virtio-uart.c new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.36.1
#1 lore raw

PATCH 2/2 lib: utils/serial: Check all compatible devices for driver

System can have multiple virtio,mmio compatible devices.

We need check all of them to choose serial with emergency write feature.

---
 lib/utils/serial/fdt_serial.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)
lib/utils/serial/fdt_serial.c +17 -12
index 1a4bf9e..761d567 100644
@@ -76,19 +76,24 @@ int fdt_serial_init(void)
 	for (pos = 0; pos < fdt_serial_drivers_size; pos++) {
 		drv = fdt_serial_drivers[pos];
 
-		noff = fdt_find_match(fdt, -1, drv->match_table, &match);
-		if (noff < 0)
-			continue;
+		noff = -1;
+		do {
+			noff = fdt_find_match(fdt, noff, drv->match_table, &match);
 
-		if (drv->init) {
-			rc = drv->init(fdt, noff, match);
-			if (rc == SBI_ENODEV)
-				continue;
-			if (rc)
-				return rc;
-		}
-		current_driver = drv;
-		break;
+			if (noff < 0)
+				break;
+
+			if (drv->init) {
+				rc = drv->init(fdt, noff, match);
+				if (rc == SBI_ENODEV)
+					continue;
+				if (rc)
+					return rc;
+			}
+
+			current_driver = drv;
+			goto done;
+		} while (noff >= 0);
 	}
 
 done:
Signature · 1 line
2.36.1
#2 lore raw

PATCH 1/2 lib: utils/serial: add virtio mmio driver

In reply to Bohdan Fedorov
Quoted 3 lines · Bohdan Fedorov

Simple virtio-console driver.
Useful for running opensbi in virtualized environments.

The term "virtualized environments" can also mean Guest/VM (VS-mode) under a hypervisor. Most hypervisors (including KVM) don't emulate/virtualize M-mode for Guest/VM and instead the hypervisor will itself handle SBI calls coming from Guest/VM.

I assume the intention is to use OpenSBI under RISC-V system emulators, simulators, or ISS which uses VirtIO devices for I/O ??

Quoted 5 lines

dt-bindings:
<linux>/Documentation/devicetree/bindings/virtio/mmio.yaml

Quoted 11 lines
---
 include/sbi_utils/fdt/fdt_helper.h     |  3 +
 include/sbi_utils/serial/virtio-uart.h | 19 +++++++
 lib/utils/fdt/fdt_helper.c             | 18 ++++++
 lib/utils/serial/fdt_serial_virtio.c   | 37 +++++++++++++
 lib/utils/serial/objects.mk            |  4 ++
 lib/utils/serial/virtio-uart.c         | 76 ++++++++++++++++++++++++++
 6 files changed, 157 insertions(+)
 create mode 100644 include/sbi_utils/serial/virtio-uart.h
 create mode 100644 lib/utils/serial/fdt_serial_virtio.c
 create mode 100644 lib/utils/serial/virtio-uart.c

Please don't use the "virtio-uart" term instead use "virtio-console".

The VirtIO spec allows different types of transports for accessing device config registers. The VirtIO MMIO is one of the possible transports and we might have PCIe or platform specific transports as well.

I suggest the following refactoring of this patch:

1) Add a include/sbi_utils/virtio/virtio.h header which defines an abstract

    VirtIO device. This abstract VirtIO device will be instantiated by some
    VirtIO transport code. The include/sbi_utils/virtio/virtio.h will have
    something like below:

    struct virtio_transport {
        char name[32];
        int (*write_config)(struct virtio_device *dev,
                                    u32 offset, void *dst, u32 dst_len);
        int (*read_config)(struct virtio_device *dev,
                                     u32 offset, void *src, u32 src_len);
    };

    struct virtio_device {
        char name[32];
        struct virtio_transport *trans;
    };

    static inline int virtio_device_read_config(struct virtio_device *dev,

u32 offset, void *dst, u32 dst_len)

    {
         ....
    }

    static inline int virtio_device_write_config(struct virtio_device *dev,

u32 offset, void *dst, u32 dst_len)

    {
         ....
    }

2) Add include/sbi_utils/virtio/virtio-mmio.h and lib/utils/virtio/virtio-mmio.c which help platforms to instantiate VirtIO device using MMIO transport. The virtio-mmio.h and virtio-mmio.c can have just one function.

int virtio_mmio_setup(struct virtio_device *dev, const char *name,
unsigned long addr);

3) The include/sbi_utils/serial/virtio-console.h and lib/utils/serial/virtio-console.c
should only use include/sbi_utils/virtio/virtio.h

There can be separate patches for the above points as well.

Quoted 56 lines
include/sbi_utils/fdt/fdt_helper.h +3 -0
index c60af35..80307bb 100644
@@ -65,6 +65,9 @@ int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset,
 int fdt_parse_sifive_uart_node(void *fdt, int nodeoffset,
                               struct platform_uart_data *uart);
 
+int fdt_parse_virtio_uart_node(void *fdt, int nodeoffset,
+                              struct platform_uart_data *uart);
+
 int fdt_parse_uart8250_node(void *fdt, int nodeoffset,
                            struct platform_uart_data *uart);
 
include/sbi_utils/serial/virtio-uart.h new file +19 -0
new file mode 100644
index 0000000..4fdfebc
@@ -0,0 +1,19 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Bohdan Fedorov <[email protected]>
+ *
+ * Based on:
+ *   sifive-uart
+ */
+
+#ifndef __SERIAL_SIFIVE_UART_H__
+#define __SERIAL_SIFIVE_UART_H__
+
+
+int virtio_uart_init(unsigned long base);
+
+#endif
lib/utils/fdt/fdt_helper.c +8 -0
index 2bdb1ef..9bfd5e7 100644
@@ -388,6 +388,24 @@ int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset,
        return 0;
 }
 
+int fdt_parse_virtio_uart_node(void *fdt, int nodeoffset,
+                              struct platform_uart_data *uart)
+{
+       int rc;
+       uint64_t reg_addr, reg_size;
+
+       if (nodeoffset < 0 || !uart || !fdt)
+               return SBI_ENODEV;

lib/utils/fdt/fdt_helper.c int fdt_parse_shakti_uart_node(void *fdt, int nodeoffset, · line 408

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+
+       rc = fdt_get_node_addr_size(fdt, nodeoffset, 0,
+                                   &reg_addr, &reg_size);
+       if (rc < 0 || !reg_addr || !reg_size)
+               return SBI_ENODEV;
+       uart->addr = reg_addr;
+
+       return 0;
+}
+

Please drop fdt_parse_virtio_uart_node() and instead directly use fdt_get_node_addr_size().

Quoted 122 lines
lib/utils/fdt/fdt_helper.c +0 -0
 int fdt_parse_sifive_uart_node(void *fdt, int nodeoffset,
                               struct platform_uart_data *uart)
 {
lib/utils/serial/fdt_serial_virtio.c new file +37 -0
new file mode 100644
index 0000000..115c5ad
@@ -0,0 +1,37 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Bohdan Fedorov <[email protected]>
+ *
+ * Based on virtio-uart
+ */
+
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/serial/fdt_serial.h>
+#include <sbi_utils/serial/virtio-uart.h>
+
+static int serial_virtio_init(void *fdt, int nodeoff,
+                               const struct fdt_match *match)
+{
+       int rc;
+       struct platform_uart_data uart;
+
+       rc = fdt_parse_virtio_uart_node(fdt, nodeoff, &uart);
+       if (rc)
+               return rc;
+
+       return virtio_uart_init(uart.addr);
+}
+
+static const struct fdt_match serial_virtio_match[] = {
+       { .compatible = "virtio,mmio" },
+       { },
+};
+
+struct fdt_serial fdt_serial_virtio = {
+       .match_table = serial_virtio_match,
+       .init = serial_virtio_init
+};
lib/utils/serial/objects.mk +4 -0
index d26a74e..60e829b 100644
@@ -22,6 +22,9 @@ libsbiutils-objs-y += serial/fdt_serial_shakti.o
 carray-fdt_serial_drivers-y += fdt_serial_sifive
 libsbiutils-objs-y += serial/fdt_serial_sifive.o
 
+carray-fdt_serial_drivers-y += fdt_serial_virtio
+libsbiutils-objs-y += serial/fdt_serial_virtio.o
+
 carray-fdt_serial_drivers-y += fdt_serial_litex
 libsbiutils-objs-y += serial/fdt_serial_litex.o
 
@@ -34,6 +37,7 @@ libsbiutils-objs-y += serial/fdt_serial_xlnx_uartlite.o
 libsbiutils-objs-y += serial/gaisler-uart.o
 libsbiutils-objs-y += serial/shakti-uart.o
 libsbiutils-objs-y += serial/sifive-uart.o
+libsbiutils-objs-y += serial/virtio-uart.o
 libsbiutils-objs-y += serial/litex-uart.o
 libsbiutils-objs-y += serial/uart8250.o
 libsbiutils-objs-y += serial/xlnx-uartlite.o
lib/utils/serial/virtio-uart.c new file +48 -0
new file mode 100644
index 0000000..1d4266f
@@ -0,0 +1,76 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Bohdan Fedorov <[email protected]>
+ *
+ * Based on:
+ *   sifive-uart
+ */
+
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+#include <sbi_utils/serial/virtio-uart.h>
+
+/* clang-format off */
+
+#define VIRTIO_MAGIC                  0x0
+#define VIRTIO_MAGIC_VALUE            0x74726976
+#define VIRTIO_DEVICE_ID             0x8
+#define VIRTIO_DEVICE_ID_CONSOLE      0x3
+#define VIRTIO_CONSOLE_F_EMERG_WRITE  (1 << 2)
+#define VIRTIO_DEVICE_FEATURES               0x10
+#define VIRTIO_DEVICE_FEATURES_SEL    0x14
+#define VIRTIO_CONSOLE_EMERG_WR       0x108
+
+/* clang-format on */
+
+static volatile void *uart_base;
+
+static u32 get_reg(u32 off)
+{
+       return readl(uart_base + off);
+}
+
+static void set_reg(u32 off, u32 val)
+{
+       writel(val, uart_base + off);
+}
+
+static void virtio_uart_putc(char ch)
+{
+       set_reg(VIRTIO_CONSOLE_EMERG_WR, ch);
+}
+
+static struct sbi_console_device virtio_console = {

lib/utils/serial/virtio-uart.c line 58

Quoted 10 lines
lib/utils/serial/virtio-uart.c new file +10 -0
+       .name = "virtio_uart",
+       .console_putc = virtio_uart_putc,
+       .console_getc = NULL,
+};
+
+int virtio_uart_init(unsigned long base)
+{
+       uart_base = (volatile void *)base;
+
+       // Chech magic

Please use "/* */" comments instead of "//".

Quoted 22 lines
lib/utils/serial/virtio-uart.c new file +18 -1
+       if(get_reg(VIRTIO_MAGIC) != VIRTIO_MAGIC_VALUE) {
+               return SBI_ENODEV;
+       }
+
+       // Virtio device is console
+       if(get_reg(VIRTIO_DEVICE_ID) != VIRTIO_DEVICE_ID_CONSOLE) {
+               return SBI_ENODEV;
+       }
+
+       // Virtio console has emergency write feature
+       set_reg(VIRTIO_DEVICE_FEATURES_SEL, 0);
+       if((get_reg(VIRTIO_DEVICE_FEATURES) & VIRTIO_CONSOLE_F_EMERG_WRITE) == 0) {
+               return SBI_ENODEV;
+       }
+
+       sbi_console_set_device(&virtio_console);
+       return 0;
+}
--

2.36.1

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup