Skip to contentSkip to conversation

tty: hvc: riscv_sbi: instantiate the legcay console earlier

PATCH In review Jisheng Zhang 2 participants 2 messages Oct 14, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: hvc: riscv_sbi: instantiate the legcay console earlier

The hvc_instantiate() is an early console discovery mechanism, it is usually called before allocating hvc terminal devices. In fact, if we check hvc_riscv_sbi's hvc_instantiate() return value, we'll find that it's -1. So the calling hvc_instantiate() is too late.

We can remove the hvc_instantiate() to only rely on the hvc_alloc() to register the kernel console. We can also move its calling earlier so the kernel console is registered earlier, so that we can get kernel console msg earlier. We take the 2nd choice in this patch.

Before the patch:

[    0.367440] printk: legacy console [hvc0] enabled
[    0.401397] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled

After the patch:

[    0.004665] printk: legacy console [hvc0] enabled
[    0.050183] Calibrating delay loop (skipped), value calculated using timer frequency.. 20.00 BogoMIPS (lpj=100000)

As can be seen, now the kernel console is registered much earlier before the BogoMIPS calibrating.

---
 drivers/tty/hvc/hvc_riscv_sbi.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvc_riscv_sbi.c +15 -2
index cede8a572594..d2ecfbf7c84a 100644
@@ -68,12 +68,10 @@ static int __init hvc_sbi_init(void)
 		err = PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_dbcn_ops, 256));
 		if (err)
 			return err;
-		hvc_instantiate(0, 0, &hvc_sbi_dbcn_ops);
 	} else if (IS_ENABLED(CONFIG_RISCV_SBI_V01)) {
 		err = PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_v01_ops, 256));
 		if (err)
 			return err;
-		hvc_instantiate(0, 0, &hvc_sbi_v01_ops);
 	} else {
 		return -ENODEV;
 	}
@@ -81,3 +79,18 @@ static int __init hvc_sbi_init(void)
 	return 0;
 }
 device_initcall(hvc_sbi_init);
+
+static int __init hvc_sbi_console_init(void)
+{
+	int err;
+
+	if (sbi_debug_console_available)
+		err = hvc_instantiate(0, 0, &hvc_sbi_dbcn_ops);
+	else if (IS_ENABLED(CONFIG_RISCV_SBI_V01))
+		err = hvc_instantiate(0, 0, &hvc_sbi_v01_ops);
+	else
+		return -ENODEV;
+
+	return err < 0 ? -ENODEV : 0;
+}
+console_initcall(hvc_sbi_console_init);
Signature · 1 line
2.45.2
#1 lore raw
Quoted 16 lines · Jisheng Zhang

The hvc_instantiate() is an early console discovery mechanism, it is usually called before allocating hvc terminal devices. In fact, if we check hvc_riscv_sbi's hvc_instantiate() return value, we'll find that it's -1. So the calling hvc_instantiate() is too late.

We can remove the hvc_instantiate() to only rely on the hvc_alloc() to register the kernel console. We can also move its calling earlier so the kernel console is registered earlier, so that we can get kernel console msg earlier. We take the 2nd choice in this patch.

Before the patch:

[    0.367440] printk: legacy console [hvc0] enabled
[    0.401397] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled

After the patch:

Quoted 7 lines
[    0.004665] printk: legacy console [hvc0] enabled
[    0.050183] Calibrating delay loop (skipped), value calculated using timer frequency.. 20.00 BogoMIPS (lpj=100000)

As can be seen, now the kernel console is registered much earlier before the BogoMIPS calibrating.

What commit id does this fix?

Quoted 28 lines
---
 drivers/tty/hvc/hvc_riscv_sbi.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/tty/hvc/hvc_riscv_sbi.c +3 -2
index cede8a572594..d2ecfbf7c84a 100644
@@ -68,12 +68,10 @@ static int __init hvc_sbi_init(void)
 		err = PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_dbcn_ops, 256));
 		if (err)
 			return err;
-		hvc_instantiate(0, 0, &hvc_sbi_dbcn_ops);
 	} else if (IS_ENABLED(CONFIG_RISCV_SBI_V01)) {
 		err = PTR_ERR_OR_ZERO(hvc_alloc(0, 0, &hvc_sbi_v01_ops, 256));
 		if (err)
 			return err;
-		hvc_instantiate(0, 0, &hvc_sbi_v01_ops);
 	} else {
 		return -ENODEV;
 	}
@@ -81,3 +79,18 @@ static int __init hvc_sbi_init(void)
 	return 0;
 }
 device_initcall(hvc_sbi_init);
+
+static int __init hvc_sbi_console_init(void)
+{

drivers/tty/hvc/hvc_riscv_sbi.c static int __init hvc_sbi_init(void) · line 94

Quoted 10 lines
drivers/tty/hvc/hvc_riscv_sbi.c +10 -0
+	int err;
+
+	if (sbi_debug_console_available)
+		err = hvc_instantiate(0, 0, &hvc_sbi_dbcn_ops);
+	else if (IS_ENABLED(CONFIG_RISCV_SBI_V01))
+		err = hvc_instantiate(0, 0, &hvc_sbi_v01_ops);
+	else
+		return -ENODEV;
+
+	return err < 0 ? -ENODEV : 0;

Please spell out a ? : line, it's not required here.

drivers/tty/hvc/hvc_riscv_sbi.c static int __init hvc_sbi_init(void) · line 96

Quoted 2 lines
drivers/tty/hvc/hvc_riscv_sbi.c +2 -0
+}
+console_initcall(hvc_sbi_console_init);

Are you sure this will always work properly? For some reason the original code did not do this, you might want to check the lore.kernel.org archives to find out why.

thanks,

greg k-h