Skip to contentSkip to conversation

RISC-V SBI debug console extension support

PATCH Superseded Anup Patel 5 participants 19 messages Nov 18, 2023 – Nov 23, 2023

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v4 cover RISC-V SBI debug console extension support

The SBI v2.0 specification is now frozen. The SBI v2.0 specification defines SBI debug console (DBCN) extension which replaces the legacy SBI v0.1 functions sbi_console_putchar() and sbi_console_getchar(). (Refer v2.0-rc5 at https://github.com/riscv-non-isa/riscv-sbi-doc/releases)

This series adds support for SBI debug console (DBCN) extension in KVM RISC-V and Linux RISC-V.

To try these patches with KVM RISC-V, use KVMTOOL from riscv_sbi_dbcn_v1 branch at: https://github.com/avpatel/kvmtool.git

These patches can also be found in the riscv_sbi_dbcn_v4 branch at:
https://github.com/avpatel/linux.git

Changes since v3:
- Rebased on Linux-6.7-rc1
- Dropped PATCH1 to PATCH5 of v3 series since these were merged through KVM RISC-V tree for Linux-6.7
- Used proper error code in PATCH1
- Added new PATCH2 which add common SBI debug console helper functions
- Updated PATCH3 and PATCH4 to use SBI debug console helper functions

Changes since v2:
- Rebased on Linux-6.6-rc5
- Handled page-crossing in PATCH7 of v2 series
- Addressed Drew's comment in PATCH3 of v2 series
- Added new PATCH5 to make get-reg-list test aware of SBI DBCN extension

Changes since v1:
- Remove use of #ifdef from PATCH4 and PATCH5 of the v1 series
- Improved commit description of PATCH3 in v1 series
- Introduced new PATCH3 in this series to allow some SBI extensions (such as SBI DBCN) do to disabled by default so that older KVM user space work fine and newer KVM user space have to explicitly opt-in for emulating SBI DBCN.
- Introduced new PATCH5 in this series which adds inline version of sbi_console_getchar() and sbi_console_putchar() for the case where CONFIG_RISCV_SBI_V01 is disabled.

Anup Patel (4):
RISC-V: Add stubs for sbi_console_putchar/getchar()
RISC-V: Add SBI debug console helper routines tty/serial: Add RISC-V SBI debug console based earlycon
RISC-V: Enable SBI based earlycon support

Atish Patra (1):
tty: Add SBI debug console support to HVC SBI driver

 arch/riscv/configs/defconfig            |  1 +
 arch/riscv/configs/rv32_defconfig       |  1 +
 arch/riscv/include/asm/sbi.h            | 10 +++++
 arch/riscv/kernel/sbi.c                 | 43 ++++++++++++++++++
 drivers/tty/hvc/Kconfig                 |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c         | 59 ++++++++++++++++++++++---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 24 ++++++++--
 8 files changed, 129 insertions(+), 13 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 1/5 RISC-V: Add stubs for sbi_console_putchar/getchar()

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add stub of these functions to avoid "#ifdef" on user side.

---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 0892f4421bc4..66f3933c14f6 100644
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -ENOENT; }
+#endif
 long sbi_get_mvendorid(void);
 long sbi_get_marchid(void);
 long sbi_get_mimpid(void);
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v4 1/5] RISC-V: Add stubs for sbi_console_putchar/getchar()

Hi Anup,

Quoted 15 lines · Anup Patel

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add stub of these functions to avoid "#ifdef" on user side.

---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +0 -0
index 0892f4421bc4..66f3933c14f6 100644

arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, · line 279

Quoted 9 lines
arch/riscv/include/asm/sbi.h +4 -0
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 			unsigned long arg3, unsigned long arg4,
 			unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -ENOENT; }

"The SBI call returns the byte on success, or -1 for failure."

So -ENOENT is not really an appropriate value to return here.

Regards,
Samuel

Quoted 4 lines
arch/riscv/include/asm/sbi.h +1 -0
+#endif
 long sbi_get_mvendorid(void);
 long sbi_get_marchid(void);
 long sbi_get_mimpid(void);
author
#3 lore raw

Re: [PATCH v4 1/5] RISC-V: Add stubs for sbi_console_putchar/getchar()

Quoted 31 lines · Samuel Holland

Hi Anup,

Quoted 15 lines · Anup Patel

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add stub of these functions to avoid "#ifdef" on user side.

---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +0 -0
index 0892f4421bc4..66f3933c14f6 100644

arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, · line 279

Quoted 9 lines
arch/riscv/include/asm/sbi.h +4 -0
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
                      unsigned long arg3, unsigned long arg4,
                      unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -ENOENT; }

"The SBI call returns the byte on success, or -1 for failure."

Quoted 1 line

So -ENOENT is not really an appropriate value to return here.

Actually, I had -1 over here previously but based on GregKH's suggestion, we are now returning proper Linux error code here.

Also, all users of sbi_console_getchar() onlyl expect a negative value upon error so better to return proper Linux error code.

Quoted 9 lines

Regards,
Samuel

Quoted 4 lines
arch/riscv/include/asm/sbi.h +1 -0
+#endif
 long sbi_get_mvendorid(void);
 long sbi_get_marchid(void);
 long sbi_get_mimpid(void);

Regards,
Anup

#4 lore raw

Re: [PATCH v4 1/5] RISC-V: Add stubs for sbi_console_putchar/getchar()

Hi Anup,

Quoted 30 lines · Anup Patel
Quoted 28 lines · Samuel Holland
Quoted 15 lines · Anup Patel

The functions sbi_console_putchar() and sbi_console_getchar() are not defined when CONFIG_RISCV_SBI_V01 is disabled so let us add stub of these functions to avoid "#ifdef" on user side.

---
 arch/riscv/include/asm/sbi.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/riscv/include/asm/sbi.h +0 -0
index 0892f4421bc4..66f3933c14f6 100644

arch/riscv/include/asm/sbi.h struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, · line 279

Quoted 9 lines
arch/riscv/include/asm/sbi.h +4 -0
@@ -271,8 +271,13 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
                      unsigned long arg3, unsigned long arg4,
                      unsigned long arg5);
 
+#ifdef CONFIG_RISCV_SBI_V01
 void sbi_console_putchar(int ch);
 int sbi_console_getchar(void);
+#else
+static inline void sbi_console_putchar(int ch) { }
+static inline int sbi_console_getchar(void) { return -ENOENT; }

"The SBI call returns the byte on success, or -1 for failure."

Quoted 7 lines
Quoted 1 line

So -ENOENT is not really an appropriate value to return here.

Actually, I had -1 over here previously but based on GregKH's suggestion, we are now returning proper Linux error code here.

Also, all users of sbi_console_getchar() onlyl expect a negative value upon error so better to return proper Linux error code.

Alright, makes sense to me.

Regards,
Samuel

author
#5 lore raw

PATCH v4 2/5 RISC-V: Add SBI debug console helper routines

In reply to Anup Patel

Let us provide SBI debug console helper routines which can be shared by serial/earlycon-riscv-sbi.c and hvc/hvc_riscv_sbi.c.

---
 arch/riscv/include/asm/sbi.h |  5 +++++
 arch/riscv/kernel/sbi.c      | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 66f3933c14f6..ee7aef5f6233 100644
@@ -334,6 +334,11 @@ static inline unsigned long sbi_mk_version(unsigned long major,
 }
 
 int sbi_err_map_linux_errno(int err);
+
+extern bool sbi_debug_console_available;
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr);
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr);
+
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
 static inline void sbi_init(void) {}
arch/riscv/kernel/sbi.c +43 -0

55 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v4 2/5
#6 lore raw

Re: [PATCH v4 2/5] RISC-V: Add SBI debug console helper routines

Quoted 41 lines · Anup Patel

Let us provide SBI debug console helper routines which can be shared by serial/earlycon-riscv-sbi.c and hvc/hvc_riscv_sbi.c.

---
 arch/riscv/include/asm/sbi.h |  5 +++++
 arch/riscv/kernel/sbi.c      | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 66f3933c14f6..ee7aef5f6233 100644
@@ -334,6 +334,11 @@ static inline unsigned long sbi_mk_version(unsigned long major,
 }
 
 int sbi_err_map_linux_errno(int err);
+
+extern bool sbi_debug_console_available;
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr);
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr);
+
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
 static inline void sbi_init(void) {}
arch/riscv/kernel/sbi.c +8 -0
index 5a62ed1da453..73a9c22c3945 100644
@@ -571,6 +571,44 @@ long sbi_get_mimpid(void)
 }
 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
 
+bool sbi_debug_console_available;
+
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr)
+{
+	struct sbiret ret;
+
+	if (!sbi_debug_console_available)
+		return -EOPNOTSUPP;

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 591

Quoted 10 lines
arch/riscv/kernel/sbi.c +10 -0
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				num_bytes, lower_32_bits(base_addr),
+				upper_32_bits(base_addr), 0, 0, 0);
+	else
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				num_bytes, base_addr, 0, 0, 0, 0);
+
+	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

We can't get perfect mappings, but I wonder if we can do better than returning ENOTSUPP for "Failed to write the byte due to I/O errors."

How about

if (ret.error == SBI_ERR_FAILURE)

     return -EIO;

return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

Quoted 8 lines
arch/riscv/kernel/sbi.c +8 -0
+}
+
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr)
+{
+	struct sbiret ret;
+
+	if (!sbi_debug_console_available)
+		return -EOPNOTSUPP;

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 609

Quoted 10 lines
arch/riscv/kernel/sbi.c +10 -0
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+				num_bytes, lower_32_bits(base_addr),
+				upper_32_bits(base_addr), 0, 0, 0);
+	else
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+				num_bytes, base_addr, 0, 0, 0, 0);
+
+	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

Same comment as above.

Quoted 20 lines
arch/riscv/kernel/sbi.c +7 -0
+}
+
 void __init sbi_init(void)
 {
 	int ret;
@@ -612,6 +650,11 @@ void __init sbi_init(void)
 			sbi_srst_reboot_nb.priority = 192;
 			register_restart_handler(&sbi_srst_reboot_nb);
 		}
+		if ((sbi_spec_version >= sbi_mk_version(2, 0)) &&
+		    (sbi_probe_extension(SBI_EXT_DBCN) > 0)) {
+			pr_info("SBI DBCN extension detected\n");
+			sbi_debug_console_available = true;
+		}
 	} else {
 		__sbi_set_timer = __sbi_set_timer_v01;
 		__sbi_send_ipi	= __sbi_send_ipi_v01;
Signature · 1 line
2.34.1

Otherwise,

Thanks,
drew

author
#7 lore raw

Re: [PATCH v4 2/5] RISC-V: Add SBI debug console helper routines

Quoted 57 lines · Andrew Jones
Quoted 41 lines · Anup Patel

Let us provide SBI debug console helper routines which can be shared by serial/earlycon-riscv-sbi.c and hvc/hvc_riscv_sbi.c.

---
 arch/riscv/include/asm/sbi.h |  5 +++++
 arch/riscv/kernel/sbi.c      | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 66f3933c14f6..ee7aef5f6233 100644
@@ -334,6 +334,11 @@ static inline unsigned long sbi_mk_version(unsigned long major,
 }
 
 int sbi_err_map_linux_errno(int err);
+
+extern bool sbi_debug_console_available;
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr);
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr);
+
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
 static inline void sbi_init(void) {}
arch/riscv/kernel/sbi.c +8 -0
index 5a62ed1da453..73a9c22c3945 100644
@@ -571,6 +571,44 @@ long sbi_get_mimpid(void)
 }
 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
 
+bool sbi_debug_console_available;
+
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr)
+{
+     struct sbiret ret;
+
+     if (!sbi_debug_console_available)
+             return -EOPNOTSUPP;

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 591

Quoted 10 lines
arch/riscv/kernel/sbi.c +10 -0
+
+     if (IS_ENABLED(CONFIG_32BIT))
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             num_bytes, lower_32_bits(base_addr),
+                             upper_32_bits(base_addr), 0, 0, 0);
+     else
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             num_bytes, base_addr, 0, 0, 0, 0);
+
+     return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

We can't get perfect mappings, but I wonder if we can do better than returning ENOTSUPP for "Failed to write the byte due to I/O errors."

Quoted 6 lines

How about

if (ret.error == SBI_ERR_FAILURE)

     return -EIO;

return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

Seems overkill but I will update anyway.

Quoted 14 lines
Quoted 14 lines
arch/riscv/kernel/sbi.c +12 -0
+}
+
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr)
+{
+     struct sbiret ret;
+
+     if (!sbi_debug_console_available)
+             return -EOPNOTSUPP;
+
+     if (IS_ENABLED(CONFIG_32BIT))
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+                             num_bytes, lower_32_bits(base_addr),
Quoted 8 lines

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 609

Quoted 6 lines
arch/riscv/kernel/sbi.c +6 -0
+                             upper_32_bits(base_addr), 0, 0, 0);
+     else
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+                             num_bytes, base_addr, 0, 0, 0, 0);
+
+     return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;

Same comment as above.

Okay.

Quoted 24 lines
Quoted 19 lines
arch/riscv/kernel/sbi.c +7 -1
+}
+
 void __init sbi_init(void)
 {
      int ret;
@@ -612,6 +650,11 @@ void __init sbi_init(void)
                      sbi_srst_reboot_nb.priority = 192;
                      register_restart_handler(&sbi_srst_reboot_nb);
              }
+             if ((sbi_spec_version >= sbi_mk_version(2, 0)) &&
+                 (sbi_probe_extension(SBI_EXT_DBCN) > 0)) {
+                     pr_info("SBI DBCN extension detected\n");
+                     sbi_debug_console_available = true;
+             }
      } else {
              __sbi_set_timer = __sbi_set_timer_v01;
              __sbi_send_ipi  = __sbi_send_ipi_v01;
--
Quoted 2 lines

2.34.1

Otherwise,

Quoted 4 lines

Thanks,
drew

Regards,
Anup

#8 lore raw

Re: [PATCH v4 2/5] RISC-V: Add SBI debug console helper routines

In reply to Anup Patel

Hi Anup,

Quoted 60 lines · Anup Patel

Let us provide SBI debug console helper routines which can be shared by serial/earlycon-riscv-sbi.c and hvc/hvc_riscv_sbi.c.

---
 arch/riscv/include/asm/sbi.h |  5 +++++
 arch/riscv/kernel/sbi.c      | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 66f3933c14f6..ee7aef5f6233 100644
@@ -334,6 +334,11 @@ static inline unsigned long sbi_mk_version(unsigned long major,
 }
 
 int sbi_err_map_linux_errno(int err);
+
+extern bool sbi_debug_console_available;
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr);
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr);
+
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
 static inline void sbi_init(void) {}
arch/riscv/kernel/sbi.c +27 -0
index 5a62ed1da453..73a9c22c3945 100644
@@ -571,6 +571,44 @@ long sbi_get_mimpid(void)
 }
 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
 
+bool sbi_debug_console_available;
+
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr)
+{
+	struct sbiret ret;
+
+	if (!sbi_debug_console_available)
+		return -EOPNOTSUPP;
+
+	if (IS_ENABLED(CONFIG_32BIT))
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				num_bytes, lower_32_bits(base_addr),
+				upper_32_bits(base_addr), 0, 0, 0);
+	else
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+				num_bytes, base_addr, 0, 0, 0, 0);
+
+	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
+}
+
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr)
+{
+	struct sbiret ret;
+
+	if (!sbi_debug_console_available)
+		return -EOPNOTSUPP;
+

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 610

Quoted 10 lines
arch/riscv/kernel/sbi.c +10 -0
+	if (IS_ENABLED(CONFIG_32BIT))
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+				num_bytes, lower_32_bits(base_addr),
+				upper_32_bits(base_addr), 0, 0, 0);
+	else
+		ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+				num_bytes, base_addr, 0, 0, 0, 0);
+
+	return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
+}

Since every place that calls these functions will need to do the vmalloc lookup, would it make sense to do it here, and have these take a pointer instead?

Regards,
Samuel

author
#9 lore raw

Re: [PATCH v4 2/5] RISC-V: Add SBI debug console helper routines

Quoted 69 lines · Samuel Holland

Hi Anup,

Quoted 65 lines · Anup Patel

Let us provide SBI debug console helper routines which can be shared by serial/earlycon-riscv-sbi.c and hvc/hvc_riscv_sbi.c.

---
 arch/riscv/include/asm/sbi.h |  5 +++++
 arch/riscv/kernel/sbi.c      | 43 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
arch/riscv/include/asm/sbi.h +5 -0
index 66f3933c14f6..ee7aef5f6233 100644
@@ -334,6 +334,11 @@ static inline unsigned long sbi_mk_version(unsigned long major,
 }
 
 int sbi_err_map_linux_errno(int err);
+
+extern bool sbi_debug_console_available;
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr);
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr);
+
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; }
 static inline void sbi_init(void) {}
arch/riscv/kernel/sbi.c +32 -0
index 5a62ed1da453..73a9c22c3945 100644
@@ -571,6 +571,44 @@ long sbi_get_mimpid(void)
 }
 EXPORT_SYMBOL_GPL(sbi_get_mimpid);
 
+bool sbi_debug_console_available;
+
+int sbi_debug_console_write(unsigned int num_bytes, phys_addr_t base_addr)
+{
+     struct sbiret ret;
+
+     if (!sbi_debug_console_available)
+             return -EOPNOTSUPP;
+
+     if (IS_ENABLED(CONFIG_32BIT))
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             num_bytes, lower_32_bits(base_addr),
+                             upper_32_bits(base_addr), 0, 0, 0);
+     else
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE,
+                             num_bytes, base_addr, 0, 0, 0, 0);
+
+     return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
+}
+
+int sbi_debug_console_read(unsigned int num_bytes, phys_addr_t base_addr)
+{
+     struct sbiret ret;
+
+     if (!sbi_debug_console_available)
+             return -EOPNOTSUPP;
+
+     if (IS_ENABLED(CONFIG_32BIT))
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+                             num_bytes, lower_32_bits(base_addr),
+                             upper_32_bits(base_addr), 0, 0, 0);
+     else
Quoted 8 lines

arch/riscv/kernel/sbi.c long sbi_get_mimpid(void) · line 610

Quoted 5 lines
arch/riscv/kernel/sbi.c +5 -0
+             ret = sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_READ,
+                             num_bytes, base_addr, 0, 0, 0, 0);
+
+     return ret.error ? sbi_err_map_linux_errno(ret.error) : ret.value;
+}

Since every place that calls these functions will need to do the vmalloc lookup, would it make sense to do it here, and have these take a pointer instead?

Yes, that's better. I will update.

Regards,
Anup

author
#10 lore raw

PATCH v4 3/5 tty/serial: Add RISC-V SBI debug console based earlycon

In reply to Anup Patel

We extend the existing RISC-V SBI earlycon support to use the new RISC-V SBI debug console extension.

---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)
drivers/tty/serial/Kconfig +1 -1
index 732c893c8d16..1f2594b8ab9d 100644
@@ -87,7 +87,7 @@ config SERIAL_EARLYCON_SEMIHOST
 
 config SERIAL_EARLYCON_RISCV_SBI
 	bool "Early console using RISC-V SBI"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select SERIAL_CORE
 	select SERIAL_CORE_CONSOLE
 	select SERIAL_EARLYCON
drivers/tty/serial/earlycon-riscv-sbi.c +20 -4
index 27afb0b74ea7..5351e1e31f45 100644
@@ -15,17 +15,33 @@ static void sbi_putc(struct uart_port *port, unsigned char c)
 	sbi_console_putchar(c);
 }
 
-static void sbi_console_write(struct console *con,
-			      const char *s, unsigned n)
+static void sbi_0_1_console_write(struct console *con,
+				  const char *s, unsigned int n)
 {
 	struct earlycon_device *dev = con->data;
 	uart_console_write(&dev->port, s, n, sbi_putc);
 }
 
+static void sbi_dbcn_console_write(struct console *con,
+				   const char *s, unsigned int n)
+{
+	sbi_debug_console_write(n, __pa(s));
+}
+
 static int __init early_sbi_setup(struct earlycon_device *device,
 				  const char *opt)
 {
-	device->con->write = sbi_console_write;
-	return 0;
+	int ret = 0;
+
+	if (sbi_debug_console_available) {
+		device->con->write = sbi_dbcn_console_write;
+	} else {
+		if (IS_ENABLED(CONFIG_RISCV_SBI_V01))
+			device->con->write = sbi_0_1_console_write;
+		else
+			ret = -ENODEV;
+	}
+
+	return ret;
 }
 EARLYCON_DECLARE(sbi, early_sbi_setup);
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v4 3/5] tty/serial: Add RISC-V SBI debug console based earlycon

Hi Anup,

Quoted 34 lines · Anup Patel

We extend the existing RISC-V SBI earlycon support to use the new RISC-V SBI debug console extension.

---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)
drivers/tty/serial/Kconfig +1 -1
index 732c893c8d16..1f2594b8ab9d 100644
@@ -87,7 +87,7 @@ config SERIAL_EARLYCON_SEMIHOST
 
 config SERIAL_EARLYCON_RISCV_SBI
 	bool "Early console using RISC-V SBI"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select SERIAL_CORE
 	select SERIAL_CORE_CONSOLE
 	select SERIAL_EARLYCON
drivers/tty/serial/earlycon-riscv-sbi.c +1 -2
index 27afb0b74ea7..5351e1e31f45 100644
@@ -15,17 +15,33 @@ static void sbi_putc(struct uart_port *port, unsigned char c)
 	sbi_console_putchar(c);
 }
 
-static void sbi_console_write(struct console *con,
-			      const char *s, unsigned n)
+static void sbi_0_1_console_write(struct console *con,

drivers/tty/serial/earlycon-riscv-sbi.c static void sbi_putc(struct uart_port *port, unsigned char c) · line 28

Quoted 10 lines
drivers/tty/serial/earlycon-riscv-sbi.c +5 -0
+				  const char *s, unsigned int n)
 {
 	struct earlycon_device *dev = con->data;
 	uart_console_write(&dev->port, s, n, sbi_putc);
 }
 
+static void sbi_dbcn_console_write(struct console *con,
+				   const char *s, unsigned int n)
+{
+	sbi_debug_console_write(n, __pa(s));

This only works for strings in the linear mapping or the kernel mapping (not vmalloc, which includes the stack). So I don't think we can use __pa() here.

drivers/tty/serial/earlycon-riscv-sbi.c static void sbi_putc(struct uart_port *port, unsigned char c) · line 39

Quoted 13 lines
drivers/tty/serial/earlycon-riscv-sbi.c +8 -2
+}
+
 static int __init early_sbi_setup(struct earlycon_device *device,
 				  const char *opt)
 {
-	device->con->write = sbi_console_write;
-	return 0;
+	int ret = 0;
+
+	if (sbi_debug_console_available) {
+		device->con->write = sbi_dbcn_console_write;
+	} else {
+		if (IS_ENABLED(CONFIG_RISCV_SBI_V01))

"else if", no need for the extra block/indentation.

Regards,
Samuel

Quoted 8 lines
drivers/tty/serial/earlycon-riscv-sbi.c +6 -0
+			device->con->write = sbi_0_1_console_write;
+		else
+			ret = -ENODEV;
+	}
+
+	return ret;
 }
 EARLYCON_DECLARE(sbi, early_sbi_setup);
author
#12 lore raw

Re: [PATCH v4 3/5] tty/serial: Add RISC-V SBI debug console based earlycon

Quoted 44 lines · Samuel Holland

Hi Anup,

Quoted 40 lines · Anup Patel

We extend the existing RISC-V SBI earlycon support to use the new RISC-V SBI debug console extension.

---
 drivers/tty/serial/Kconfig              |  2 +-
 drivers/tty/serial/earlycon-riscv-sbi.c | 24 ++++++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)
drivers/tty/serial/Kconfig +1 -1
index 732c893c8d16..1f2594b8ab9d 100644
@@ -87,7 +87,7 @@ config SERIAL_EARLYCON_SEMIHOST
 
 config SERIAL_EARLYCON_RISCV_SBI
      bool "Early console using RISC-V SBI"
-     depends on RISCV_SBI_V01
+     depends on RISCV_SBI
      select SERIAL_CORE
      select SERIAL_CORE_CONSOLE
      select SERIAL_EARLYCON
drivers/tty/serial/earlycon-riscv-sbi.c +2 -2
index 27afb0b74ea7..5351e1e31f45 100644
@@ -15,17 +15,33 @@ static void sbi_putc(struct uart_port *port, unsigned char c)
      sbi_console_putchar(c);
 }
 
-static void sbi_console_write(struct console *con,
-                           const char *s, unsigned n)
+static void sbi_0_1_console_write(struct console *con,
+                               const char *s, unsigned int n)
 {
      struct earlycon_device *dev = con->data;
      uart_console_write(&dev->port, s, n, sbi_putc);
 }
 
Quoted 7 lines

drivers/tty/serial/earlycon-riscv-sbi.c static void sbi_putc(struct uart_port *port, unsigned char c) · line 28

Quoted 4 lines
drivers/tty/serial/earlycon-riscv-sbi.c +4 -0
+static void sbi_dbcn_console_write(struct console *con,
+                                const char *s, unsigned int n)
+{
+     sbi_debug_console_write(n, __pa(s));

This only works for strings in the linear mapping or the kernel mapping (not vmalloc, which includes the stack). So I don't think we can use __pa() here.

In which case, we need extend sbi_debug_console_write() to do the va-to-pa conversion for both earlycon-riscv-sbi.c and hvc_riscv_sbi.c

Quoted 16 lines
Quoted 4 lines
drivers/tty/serial/earlycon-riscv-sbi.c +2 -0
+}
+
 static int __init early_sbi_setup(struct earlycon_device *device,

drivers/tty/serial/earlycon-riscv-sbi.c static void sbi_putc(struct uart_port *port, unsigned char c) · line 39

Quoted 10 lines
drivers/tty/serial/earlycon-riscv-sbi.c +6 -2
                                const char *opt)
 {
-     device->con->write = sbi_console_write;
-     return 0;
+     int ret = 0;
+
+     if (sbi_debug_console_available) {
+             device->con->write = sbi_dbcn_console_write;
+     } else {
+             if (IS_ENABLED(CONFIG_RISCV_SBI_V01))

"else if", no need for the extra block/indentation.

Okay, I will update.

Quoted 13 lines

Regards,
Samuel

Quoted 8 lines
drivers/tty/serial/earlycon-riscv-sbi.c +6 -0
+                     device->con->write = sbi_0_1_console_write;
+             else
+                     ret = -ENODEV;
+     }
+
+     return ret;
 }
 EARLYCON_DECLARE(sbi, early_sbi_setup);

Regards,
Anup

author
#13 lore raw

PATCH v4 4/5 tty: Add SBI debug console support to HVC SBI driver

In reply to Anup Patel

From: Atish Patra <[email protected]>

RISC-V SBI specification supports advanced debug console support via SBI DBCN extension.

Extend the HVC SBI driver to support it.

---
 drivers/tty/hvc/Kconfig         |  2 +-
 drivers/tty/hvc/hvc_riscv_sbi.c | 59 +++++++++++++++++++++++++++++----
 2 files changed, 53 insertions(+), 8 deletions(-)
drivers/tty/hvc/Kconfig +1 -1
index 4f9264d005c0..6e05c5c7bca1 100644
@@ -108,7 +108,7 @@ config HVC_DCC_SERIALIZE_SMP
 
 config HVC_RISCV_SBI
 	bool "RISC-V SBI console support"
-	depends on RISCV_SBI_V01
+	depends on RISCV_SBI
 	select HVC_DRIVER
 	help
 	  This enables support for console output via RISC-V SBI calls, which
drivers/tty/hvc/hvc_riscv_sbi.c +52 -7

73 lines not on this page · raw message

Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#14 lore raw

Re: [PATCH v4 4/5] tty: Add SBI debug console support to HVC SBI driver

Quoted 4 lines · Anup Patel
drivers/tty/hvc/hvc_riscv_sbi.c +0 -0
index 31f53fa77e4a..697c981221b5 100644

...

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 66

Quoted 7 lines
drivers/tty/hvc/hvc_riscv_sbi.c +4 -2
-static int __init hvc_sbi_console_init(void)
+static int hvc_sbi_dbcn_tty_get(uint32_t vtermno, char *buf, int count)
  {
-	hvc_instantiate(0, 0, &hvc_sbi_ops);
+	phys_addr_t pa;
+
+	if (is_vmalloc_addr(buf)) {

I wonder, where does this buf come from, so that you have to check for vmalloc?

drivers/tty/hvc/hvc_riscv_sbi.c

Quoted 2 lines
drivers/tty/hvc/hvc_riscv_sbi.c +2 -0
+		pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);
+		if (PAGE_SIZE < (offset_in_page(buf) + count))

Am I the only one who would prefer:
if (count + offset_in_page(buf) > PAGE_SIZE)
?

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 75

Quoted 7 lines
drivers/tty/hvc/hvc_riscv_sbi.c +7 -0
+			count = PAGE_SIZE - offset_in_page(buf);
+	} else {
+		pa = __pa(buf);
+	}
+
+	return sbi_debug_console_read(count, pa);
+}

thanks,

Signature · 2 lines
js
suse labs
#15 lore raw

Re: [PATCH v4 4/5] tty: Add SBI debug console support to HVC SBI driver

Quoted 7 lines · Jiri Slaby
Quoted 4 lines · Anup Patel
drivers/tty/hvc/hvc_riscv_sbi.c +0 -0
index 31f53fa77e4a..697c981221b5 100644

...

Quoted 11 lines

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 66

Quoted 7 lines
drivers/tty/hvc/hvc_riscv_sbi.c +4 -2
-static int __init hvc_sbi_console_init(void)
+static int hvc_sbi_dbcn_tty_get(uint32_t vtermno, char *buf, int count)
  {
-     hvc_instantiate(0, 0, &hvc_sbi_ops);
+     phys_addr_t pa;
+
+     if (is_vmalloc_addr(buf)) {

I wonder, where does this buf come from, so that you have to check for vmalloc?

When VMAP_STCK is enabled, stack allocation depends on the vmalloc. That's why we have to if the buf is allocated using vmalloc.

Quoted 21 lines

drivers/tty/hvc/hvc_riscv_sbi.c

Quoted 2 lines
drivers/tty/hvc/hvc_riscv_sbi.c +2 -0
+             pa = page_to_phys(vmalloc_to_page(buf)) + offset_in_page(buf);
+             if (PAGE_SIZE < (offset_in_page(buf) + count))

Am I the only one who would prefer:
if (count + offset_in_page(buf) > PAGE_SIZE)
?

drivers/tty/hvc/hvc_riscv_sbi.c static int hvc_sbi_tty_get(uint32_t vtermno, char *buf, int count) · line 75

Quoted 7 lines
drivers/tty/hvc/hvc_riscv_sbi.c +7 -0
+                     count = PAGE_SIZE - offset_in_page(buf);
+     } else {
+             pa = __pa(buf);
+     }
+
+     return sbi_debug_console_read(count, pa);
+}

thanks,
--
js
suse labs

author
#16 lore raw

PATCH v4 5/5 RISC-V: Enable SBI based earlycon support

In reply to Anup Patel

Let us enable SBI based earlycon support in defconfigs for both RV32 and RV64 so that "earlycon=sbi" can be used again.

---
 arch/riscv/configs/defconfig      | 1 +
 arch/riscv/configs/rv32_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/riscv/configs/defconfig +1 -0
index 905881282a7c..eaf34e871e30 100644
@@ -149,6 +149,7 @@ CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_8250_DW=y
 CONFIG_SERIAL_OF_PLATFORM=y
 CONFIG_SERIAL_SH_SCI=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
arch/riscv/configs/rv32_defconfig +1 -0
index 89b601e253a6..5721af39afd1 100644
@@ -66,6 +66,7 @@ CONFIG_INPUT_MOUSEDEV=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
Signature · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#17 lore raw

Re: [PATCH v4 5/5] RISC-V: Enable SBI based earlycon support

Hi Anup,

arch/riscv/configs/rv32_defconfig

Quoted 26 lines · Anup Patel

Let us enable SBI based earlycon support in defconfigs for both RV32 and RV64 so that "earlycon=sbi" can be used again.

---
 arch/riscv/configs/defconfig      | 1 +
 arch/riscv/configs/rv32_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/riscv/configs/defconfig +1 -0
index 905881282a7c..eaf34e871e30 100644
@@ -149,6 +149,7 @@ CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_8250_DW=y
 CONFIG_SERIAL_OF_PLATFORM=y
 CONFIG_SERIAL_SH_SCI=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
arch/riscv/configs/rv32_defconfig +0 -0
index 89b601e253a6..5721af39afd1 100644

This file isn't used anymore since 72f045d19f25 ("riscv: Fixup difference with defconfig"), so there's no need to update it. I'll send a patch deleting it.

Regards,
Samuel

Quoted 8 lines
arch/riscv/configs/rv32_defconfig +1 -0
@@ -66,6 +66,7 @@ CONFIG_INPUT_MOUSEDEV=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y
author
#18 lore raw

Re: [PATCH v4 5/5] RISC-V: Enable SBI based earlycon support

Quoted 25 lines · Samuel Holland

Hi Anup,

Quoted 21 lines · Anup Patel

Let us enable SBI based earlycon support in defconfigs for both RV32 and RV64 so that "earlycon=sbi" can be used again.

---
 arch/riscv/configs/defconfig      | 1 +
 arch/riscv/configs/rv32_defconfig | 1 +
 2 files changed, 2 insertions(+)
arch/riscv/configs/defconfig +1 -0
index 905881282a7c..eaf34e871e30 100644
@@ -149,6 +149,7 @@ CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_8250_DW=y
 CONFIG_SERIAL_OF_PLATFORM=y
 CONFIG_SERIAL_SH_SCI=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
Quoted 8 lines

arch/riscv/configs/rv32_defconfig

Quoted 5 lines
arch/riscv/configs/defconfig +0 -0
 CONFIG_HW_RANDOM_VIRTIO=y
arch/riscv/configs/rv32_defconfig +0 -0
index 89b601e253a6..5721af39afd1 100644

This file isn't used anymore since 72f045d19f25 ("riscv: Fixup difference with defconfig"), so there's no need to update it. I'll send a patch deleting it.

Okay, I will drop the changes in rv32_defconfig.

Quoted 4 lines

Regards,
Samuel

Quoted 9 lines

arch/riscv/configs/rv32_defconfig CONFIG_INPUT_MOUSEDEV=y · line 72

Quoted 8 lines
arch/riscv/configs/rv32_defconfig +1 -0
@@ -66,6 +66,7 @@ CONFIG_INPUT_MOUSEDEV=y
 CONFIG_SERIAL_8250=y
 CONFIG_SERIAL_8250_CONSOLE=y
 CONFIG_SERIAL_OF_PLATFORM=y
+CONFIG_SERIAL_EARLYCON_RISCV_SBI=y
 CONFIG_VIRTIO_CONSOLE=y
 CONFIG_HW_RANDOM=y
 CONFIG_HW_RANDOM_VIRTIO=y

Regards,
Anup