From 451dcc2368278f7150edcc3d1ce27f03dd7a2949 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 5 Jan 2024 13:19:34 +0800 Subject: [PATCH 1/9] NuttX prints 123 to the HTIF Console yay! --- .gitignore | 1 + arch/risc-v/src/qemu-rv/qemu_rv_head.S | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/.gitignore b/.gitignore index 34e29b22642f1..353778c5d4420 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ uImage .DS_Store tools/gdb/__pycache__ /build +init.S diff --git a/arch/risc-v/src/qemu-rv/qemu_rv_head.S b/arch/risc-v/src/qemu-rv/qemu_rv_head.S index 9e321bb62a6f0..94b1ae8cc7255 100644 --- a/arch/risc-v/src/qemu-rv/qemu_rv_head.S +++ b/arch/risc-v/src/qemu-rv/qemu_rv_head.S @@ -41,6 +41,25 @@ __start: + /* Print `123` to HTIF Console */ + /* Load HTIF Base Address to Register t0 */ + li t0, 0x40008000 + + /* Load to Register t1 the HTIF Command to print `1` */ + li t1, 0x0101000000000031 + /* Store 64-bit double-word from Register t1 to HTIF Base Address, Offset 0 */ + sd t1, 0(t0) + + /* Load to Register t1 the HTIF Command to print `2` */ + li t1, 0x0101000000000032 + /* Store 64-bit double-word from Register t1 to HTIF Base Address, Offset 0 */ + sd t1, 0(t0) + + /* Load to Register t1 the HTIF Command to print `3` */ + li t1, 0x0101000000000033 + /* Store 64-bit double-word from Register t1 to HTIF Base Address, Offset 0 */ + sd t1, 0(t0) + /* Preserve a1 as it contains the pointer to DTB */ /* Load mhartid (cpuid) */ From 6947856e1de3b2239bb1c5a8667b04b6a6e4e67b Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Fri, 5 Jan 2024 14:52:28 +0800 Subject: [PATCH 2/9] Fixed UART Output yay! nx_start: CPU0: Beginning Idle Loop --- drivers/serial/uart_16550.c | 45 ++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/drivers/serial/uart_16550.c b/drivers/serial/uart_16550.c index 418a91bca4cc7..3ccf61261447a 100644 --- a/drivers/serial/uart_16550.c +++ b/drivers/serial/uart_16550.c @@ -609,10 +609,12 @@ static uart_dev_t g_uart3port = static inline uart_datawidth_t u16550_serialin(FAR struct u16550_s *priv, int offset) { + return 0; //// + // Commented out the rest #ifdef CONFIG_SERIAL_UART_ARCH_MMIO - return *((FAR volatile uart_datawidth_t *)priv->uartbase + offset); + // return *((FAR volatile uart_datawidth_t *)priv->uartbase + offset); #else - return uart_getreg(priv->uartbase, offset); + // return uart_getreg(priv->uartbase, offset); #endif } @@ -623,10 +625,11 @@ static inline uart_datawidth_t u16550_serialin(FAR struct u16550_s *priv, static inline void u16550_serialout(FAR struct u16550_s *priv, int offset, uart_datawidth_t value) { + // Commented out the rest #ifdef CONFIG_SERIAL_UART_ARCH_MMIO - *((FAR volatile uart_datawidth_t *)priv->uartbase + offset) = value; + // *((FAR volatile uart_datawidth_t *)priv->uartbase + offset) = value; #else - uart_putreg(priv->uartbase, offset, value); + // uart_putreg(priv->uartbase, offset, value); #endif } @@ -648,22 +651,24 @@ static inline void u16550_serialout(FAR struct u16550_s *priv, int offset, static int u16550_wait(FAR struct u16550_s *priv) { - int i; + // Nopez! No waiting for now + return OK; //// + // int i; - for (i = 0; i < UART_TIMEOUT_MS; i++) - { - uint32_t status = u16550_serialin(priv, UART_USR_OFFSET); + // for (i = 0; i < UART_TIMEOUT_MS; i++) + // { + // uint32_t status = u16550_serialin(priv, UART_USR_OFFSET); - if ((status & UART_USR_BUSY) == 0) - { - return OK; - } + // if ((status & UART_USR_BUSY) == 0) + // { + // return OK; + // } - up_mdelay(1); - } + // up_mdelay(1); + // } - _err("UART timeout\n"); - return ERROR; + // _err("UART timeout\n"); + // return ERROR; } #endif /* CONFIG_16550_WAIT_LCR */ @@ -1705,8 +1710,12 @@ static bool u16550_txempty(struct uart_dev_s *dev) #ifdef HAVE_16550_CONSOLE static void u16550_putc(FAR struct u16550_s *priv, int ch) { - while ((u16550_serialin(priv, UART_LSR_OFFSET) & UART_LSR_THRE) == 0); - u16550_serialout(priv, UART_THR_OFFSET, (uart_datawidth_t)ch); + // Hardcode the HTIF Base Address + *(volatile uint64_t *) 0x40008000 = 0x0101000000000000ul | ch; + + // Previously: + // while ((u16550_serialin(priv, UART_LSR_OFFSET) & UART_LSR_THRE) == 0); + // u16550_serialout(priv, UART_THR_OFFSET, (uart_datawidth_t)ch); } #endif From 4170ca32f02242da1d68eca90363f5263904989e Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 16:19:29 +0800 Subject: [PATCH 3/9] Init VirtIO OK --- .../qemu-rv/rv-virt/src/qemu_rv_appinit.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c index e8f61fc38fd38..8c071e7d76a56 100644 --- a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c +++ b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -38,12 +39,12 @@ * Pre-processor Definitions ****************************************************************************/ -#define QEMU_VIRTIO_MMIO_BASE 0x10001000 -#define QEMU_VIRTIO_MMIO_REGSIZE 0x1000 +#define QEMU_VIRTIO_MMIO_BASE 0x40010000 // VIRTIO_BASE_ADDR. Previously: 0x10001000 +#define QEMU_VIRTIO_MMIO_REGSIZE 0x1000 // VIRTIO_SIZE #ifdef CONFIG_ARCH_USE_S_MODE -# define QEMU_VIRTIO_MMIO_IRQ 26 +# define QEMU_VIRTIO_MMIO_IRQ 26 // TODO: Should this be 1? (VIRTIO_IRQ) #else -# define QEMU_VIRTIO_MMIO_IRQ 28 +# define QEMU_VIRTIO_MMIO_IRQ 28 // TODO: Should this be 1? (VIRTIO_IRQ) #endif #define QEMU_VIRTIO_MMIO_NUM 8 @@ -114,8 +115,18 @@ int board_app_initialize(uintptr_t arg) #ifdef CONFIG_DRIVERS_VIRTIO_MMIO qemu_virtio_register_mmio_devices(); + + void test_virtio(void); + test_virtio(); //// #endif return OK; #endif } + +// Based on virtio_mmio_create_virtqueue +// https://github.com/apache/nuttx/blob/master/drivers/virtio/virtio-mmio.c#L349-L414 +void test_virtio(void) +{ + _info("\n"); +} From 9379d0e74d09370b35851665d4fabfbcfa3e20b5 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 16:44:33 +0800 Subject: [PATCH 4/9] Init VirtIO OK --- boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c index 8c071e7d76a56..87af56a084bfa 100644 --- a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c +++ b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c @@ -46,7 +46,7 @@ #else # define QEMU_VIRTIO_MMIO_IRQ 28 // TODO: Should this be 1? (VIRTIO_IRQ) #endif -#define QEMU_VIRTIO_MMIO_NUM 8 +#define QEMU_VIRTIO_MMIO_NUM 1 // Number of VirtIO Devices. Previously: 8 /**************************************************************************** * Private Functions From d7fbf8387d34a1f249e65ac4c63e4d54e41b1978 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 17:10:08 +0800 Subject: [PATCH 5/9] VirtIO tested OK yay! Hello VirtIO from NuttX! --- drivers/virtio/virtio-mmio.c | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/drivers/virtio/virtio-mmio.c b/drivers/virtio/virtio-mmio.c index 663f2865546dd..1d10e1ffae3db 100644 --- a/drivers/virtio/virtio-mmio.c +++ b/drivers/virtio/virtio-mmio.c @@ -867,6 +867,56 @@ int virtio_register_mmio_device(FAR void *regs, int irq) goto err; } + //// TODO: Init VirtIO Device + struct virtio_device *vdev = &vmdev->vdev; + DEBUGASSERT(vdev != NULL); + + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER); + virtio_set_features(vdev, 0); + virtio_set_status(vdev, VIRTIO_CONFIG_FEATURES_OK); + + #define VIRTIO_SERIAL_RX 0 + #define VIRTIO_SERIAL_TX 1 + #define VIRTIO_SERIAL_NUM 2 + const char *vqnames[VIRTIO_SERIAL_NUM]; + vqnames[VIRTIO_SERIAL_RX] = "virtio_serial_rx"; + vqnames[VIRTIO_SERIAL_TX] = "virtio_serial_tx"; + + vq_callback callbacks[VIRTIO_SERIAL_NUM]; + callbacks[VIRTIO_SERIAL_RX] = NULL; //// TODO: virtio_serial_rxready; + callbacks[VIRTIO_SERIAL_TX] = NULL; //// TODO: virtio_serial_txdone; + ret = virtio_create_virtqueues(vdev, 0, VIRTIO_SERIAL_NUM, vqnames, + callbacks); + DEBUGASSERT(ret >= 0); + virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK); + + //// TODO: Send data to VirtIO Device + DEBUGASSERT(vdev->vrings_info != NULL); + struct virtqueue *vq = vdev->vrings_info[VIRTIO_SERIAL_TX].vq; + DEBUGASSERT(vq != NULL); + + /* Set the virtqueue buffer */ + static char *HELLO_MSG = "Hello VirtIO from NuttX!\n"; + struct virtqueue_buf vb[2]; + vb[0].buf = HELLO_MSG; + vb[0].len = strlen(HELLO_MSG); + int num = 1; + + /* Get the total send length */ + uintptr_t len = strlen(HELLO_MSG); + + // if (xfer->nlength != 0) + // { + // vb[1].buf = xfer->nbuffer; + // vb[1].len = xfer->nlength; + // num = 2; + // } + + /* Add buffer to TX virtiqueue and notify the other size */ + virtqueue_add_buffer(vq, vb, num, 0, (FAR void *)len); + virtqueue_kick(vq); + //// + return ret; err: From bf9a6a93e04bf3b82b56e5f87c425daf2da1342f Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 18:12:43 +0800 Subject: [PATCH 6/9] VirtIO tested OK yay! Hello VirtIO from NuttX! --- boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c | 3 --- drivers/virtio/virtio-mmio.c | 13 ++++++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c index 87af56a084bfa..ace3ccce879c0 100644 --- a/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c +++ b/boards/risc-v/qemu-rv/rv-virt/src/qemu_rv_appinit.c @@ -115,9 +115,6 @@ int board_app_initialize(uintptr_t arg) #ifdef CONFIG_DRIVERS_VIRTIO_MMIO qemu_virtio_register_mmio_devices(); - - void test_virtio(void); - test_virtio(); //// #endif return OK; diff --git a/drivers/virtio/virtio-mmio.c b/drivers/virtio/virtio-mmio.c index 1d10e1ffae3db..91417076f63d6 100644 --- a/drivers/virtio/virtio-mmio.c +++ b/drivers/virtio/virtio-mmio.c @@ -867,7 +867,10 @@ int virtio_register_mmio_device(FAR void *regs, int irq) goto err; } - //// TODO: Init VirtIO Device + // Testing: Init VirtIO Device + // Based on virtio_serial_init + // https://github.com/apache/nuttx/blob/master/drivers/virtio/virtio-serial.c#L445-L511 + struct virtio_device *vdev = &vmdev->vdev; DEBUGASSERT(vdev != NULL); @@ -890,7 +893,10 @@ int virtio_register_mmio_device(FAR void *regs, int irq) DEBUGASSERT(ret >= 0); virtio_set_status(vdev, VIRTIO_CONFIG_STATUS_DRIVER_OK); - //// TODO: Send data to VirtIO Device + // Testing: Send data to VirtIO Device + // Based on virtio_serial_dmasend + // https://github.com/apache/nuttx/blob/master/drivers/virtio/virtio-serial.c#L310-L345 + DEBUGASSERT(vdev->vrings_info != NULL); struct virtqueue *vq = vdev->vrings_info[VIRTIO_SERIAL_TX].vq; DEBUGASSERT(vq != NULL); @@ -905,6 +911,7 @@ int virtio_register_mmio_device(FAR void *regs, int irq) /* Get the total send length */ uintptr_t len = strlen(HELLO_MSG); + // TODO: What's this? // if (xfer->nlength != 0) // { // vb[1].buf = xfer->nbuffer; @@ -915,7 +922,7 @@ int virtio_register_mmio_device(FAR void *regs, int irq) /* Add buffer to TX virtiqueue and notify the other size */ virtqueue_add_buffer(vq, vb, num, 0, (FAR void *)len); virtqueue_kick(vq); - //// + // End of Testing return ret; From 82c3a06627c70862d7a95e8e69d4cff5fbad9e09 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 18:17:28 +0800 Subject: [PATCH 7/9] VirtIO tested OK yay! Hello VirtIO from NuttX! --- drivers/virtio/virtio-mmio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/virtio/virtio-mmio.c b/drivers/virtio/virtio-mmio.c index 91417076f63d6..64fb321093cb8 100644 --- a/drivers/virtio/virtio-mmio.c +++ b/drivers/virtio/virtio-mmio.c @@ -902,7 +902,7 @@ int virtio_register_mmio_device(FAR void *regs, int irq) DEBUGASSERT(vq != NULL); /* Set the virtqueue buffer */ - static char *HELLO_MSG = "Hello VirtIO from NuttX!\n"; + static char *HELLO_MSG = "Hello VirtIO from NuttX!\r\n"; struct virtqueue_buf vb[2]; vb[0].buf = HELLO_MSG; vb[0].len = strlen(HELLO_MSG); From 0f19f417026a75b567d91da6ca946f71369e9216 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Sun, 7 Jan 2024 19:15:44 +0800 Subject: [PATCH 8/9] Clean up --- drivers/virtio/virtio-mmio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/virtio/virtio-mmio.c b/drivers/virtio/virtio-mmio.c index 64fb321093cb8..91df5e542d2fb 100644 --- a/drivers/virtio/virtio-mmio.c +++ b/drivers/virtio/virtio-mmio.c @@ -919,7 +919,7 @@ int virtio_register_mmio_device(FAR void *regs, int irq) // num = 2; // } - /* Add buffer to TX virtiqueue and notify the other size */ + /* Add buffer to TX virtiqueue and notify the VirtIO Host */ virtqueue_add_buffer(vq, vb, num, 0, (FAR void *)len); virtqueue_kick(vq); // End of Testing From 781a284190621efbf319e03640168c082076ecf6 Mon Sep 17 00:00:00 2001 From: Lee Lup Yuen Date: Tue, 9 Jan 2024 18:37:45 +0800 Subject: [PATCH 9/9] Update config --- .../qemu-rv/rv-virt/configs/nsh64/defconfig | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/nsh64/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/nsh64/defconfig index 37cd640d5fe23..26178d74f4b14 100644 --- a/boards/risc-v/qemu-rv/rv-virt/configs/nsh64/defconfig +++ b/boards/risc-v/qemu-rv/rv-virt/configs/nsh64/defconfig @@ -29,10 +29,48 @@ CONFIG_ARCH_STACKDUMP=y CONFIG_BCH=y CONFIG_BOARD_LOOPSPERMSEC=6366 CONFIG_BUILTIN=y +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_ASSERTIONS_EXPRESSION=y +CONFIG_DEBUG_BINFMT=y +CONFIG_DEBUG_BINFMT_ERROR=y +CONFIG_DEBUG_BINFMT_INFO=y +CONFIG_DEBUG_BINFMT_WARN=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FS=y +CONFIG_DEBUG_FS_ERROR=y +CONFIG_DEBUG_FS_INFO=y +CONFIG_DEBUG_FS_WARN=y CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_IPC=y +CONFIG_DEBUG_IPC_ERROR=y +CONFIG_DEBUG_IPC_INFO=y +CONFIG_DEBUG_IPC_WARN=y +CONFIG_DEBUG_LIB=y +CONFIG_DEBUG_LIB_ERROR=y +CONFIG_DEBUG_LIB_INFO=y +CONFIG_DEBUG_LIB_WARN=y +CONFIG_DEBUG_MM=y +CONFIG_DEBUG_MM_ERROR=y +CONFIG_DEBUG_MM_INFO=y +CONFIG_DEBUG_MM_WARN=y +CONFIG_DEBUG_SCHED=y +CONFIG_DEBUG_SCHED_ERROR=y +CONFIG_DEBUG_SCHED_INFO=y +CONFIG_DEBUG_SCHED_WARN=y CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_TIMER=y +CONFIG_DEBUG_TIMER_ERROR=y +CONFIG_DEBUG_TIMER_INFO=y +CONFIG_DEBUG_TIMER_WARN=y +CONFIG_DEBUG_VIRTIO=y +CONFIG_DEBUG_VIRTIO_ERROR=y +CONFIG_DEBUG_VIRTIO_INFO=y +CONFIG_DEBUG_VIRTIO_WARN=y CONFIG_DEVICE_TREE=y +CONFIG_DEV_SIMPLE_ADDRENV=y CONFIG_DEV_ZERO=y +CONFIG_DRIVERS_VIRTIO=y +CONFIG_DRIVERS_VIRTIO_MMIO=y CONFIG_ELF=y CONFIG_EXAMPLES_HELLO=m CONFIG_FS_HOSTFS=y @@ -45,6 +83,7 @@ CONFIG_LIBC_ENVPATH=y CONFIG_LIBC_EXECFUNCS=y CONFIG_LIBC_PERROR_STDOUT=y CONFIG_LIBC_STRERROR=y +CONFIG_NDEBUG=y CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 CONFIG_NSH_ARCHINIT=y CONFIG_NSH_BUILTIN_APPS=y