[PATCH] ns16550: add support for polling mode when device tree is used

Oleksii Kurochko posted 1 patch 8 months, 3 weeks ago
Failed in applying to current master (apply log)
There is a newer version of this series
docs/misc/xen-command-line.pandoc |  7 +++-
xen/drivers/char/ns16550.c        | 67 ++++++++++++++++++++-----------
2 files changed, 49 insertions(+), 25 deletions(-)
[PATCH] ns16550: add support for polling mode when device tree is used
Posted by Oleksii Kurochko 8 months, 3 weeks ago
RISC-V doesn't support interrupts for the time being, so it would be nice to
have polling mode.

To force poll mode it was added parsing of new 'poll' property under "com1="

If there is no interrupt property in UART node, then polling will be used.
According to 4.2.2 National Semiconductor 16450/16550 Compatible UART
Requirements from The Devicetree Specification v0.4
( https://www.devicetree.org/specifications/ ) the interrupt property is
optional.

Also, it is possible that interrupt '0' can be used for some architectures as
an legal UART interrupt number ( according to dts files in Linux kernel ), so
the check of the return value of platform_get_irq() was updated in case of when
device tree is used for UART initialization.
For example:
https://github.com/torvalds/linux/blob/master/arch/powerpc/boot/dts/ebony.dts#L197

Signed-off-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
Changes in V5:
 - Update xen-command-line.pandoc.
 - move poll check to ns16550_uart_dt_init as in case of device tree
   initialization it is needed only to check if 'poll' is present in com1.
 - Don't init uart->irq in case when platform_get_irq() return negative value.
---
Changes in V4:
 - fix code style for ns16550_irq().
 - partially revert changes in pci_uart_config().
 - add check of "poll" property under "com<N>=" instead of "console=".
 - remove seting of polling mode in parsing of msi in parse_positional().
 - update comment above opt_com1 about Configuration string of serial port.
---
 docs/misc/xen-command-line.pandoc |  7 +++-
 xen/drivers/char/ns16550.c        | 67 ++++++++++++++++++++-----------
 2 files changed, 49 insertions(+), 25 deletions(-)

diff --git a/docs/misc/xen-command-line.pandoc b/docs/misc/xen-command-line.pandoc
index 4060ebdc5d..67e355dab8 100644
--- a/docs/misc/xen-command-line.pandoc
+++ b/docs/misc/xen-command-line.pandoc
@@ -331,9 +331,12 @@ Interrupts.  Specifying zero disables CMCI handling.
 Flag to indicate whether to probe for a CMOS Real Time Clock irrespective of
 ACPI indicating none to be there.
 
+### com1 (device tree based initialization)
+> `= [poll]`
+
 ### com1 (x86)
 ### com2 (x86)
-> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
+> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi|poll][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
 
 Both option `com1` and `com2` follow the same format.
 
@@ -353,7 +356,7 @@ Both option `com1` and `com2` follow the same format.
   * `S` is an integer 1 or 2 for the number of stop bits.
 * `<io-base>` is an integer which specifies the IO base port for UART
   registers.
-* `<irq>` is the IRQ number to use, or `0` to use the UART in poll
+* `<irq>` is the IRQ number to use, or `poll` to use the UART in poll
   mode only, or `msi` to set up a Message Signaled Interrupt.
 * `<port-bdf>` is the PCI location of the UART, in
   `<bus>:<device>.<function>` notation.
diff --git a/xen/drivers/char/ns16550.c b/xen/drivers/char/ns16550.c
index 2aed6ec707..d8cfbbfae4 100644
--- a/xen/drivers/char/ns16550.c
+++ b/xen/drivers/char/ns16550.c
@@ -58,7 +58,11 @@ static struct ns16550 {
     struct timer timer;
     struct timer resume_timer;
     unsigned int timeout_ms;
-    bool_t intr_works;
+    enum {
+        intr_off,
+        intr_on,
+        polling,
+    } intr_works;
     bool_t dw_usr_bsy;
 #ifdef NS16550_PCI
     /* PCI card parameters. */
@@ -112,6 +116,19 @@ struct ns16550_config_param {
 static void enable_exar_enhanced_bits(const struct ns16550 *uart);
 #endif
 
+/*
+ * Configure serial port with a string:
+ *   <baud>[/<base_baud>][,DPS[,<io-base>[,<irq>|msi|poll[,<port-bdf>[,<bridge-bdf>]]]]].
+ * The tail of the string can be omitted if platform defaults are sufficient.
+ * If the baud rate is pre-configured, perhaps by a bootloader, then 'auto'
+ * can be specified in place of a numeric baud rate. Polled mode is specified
+ * by poll property.
+ */
+static char __initdata opt_com1[128] = "";
+static char __initdata opt_com2[128] = "";
+string_param("com1", opt_com1);
+string_param("com2", opt_com2);
+
 static void cf_check ns16550_delayed_resume(void *data);
 
 static u8 ns_read_reg(const struct ns16550 *uart, unsigned int reg)
@@ -181,7 +198,7 @@ static void cf_check ns16550_interrupt(
     struct serial_port *port = dev_id;
     struct ns16550 *uart = port->uart;
 
-    uart->intr_works = 1;
+    uart->intr_works = intr_on;
 
     while ( !(ns_read_reg(uart, UART_IIR) & UART_IIR_NOINT) )
     {
@@ -212,7 +229,7 @@ static void cf_check __ns16550_poll(struct cpu_user_regs *regs)
     struct serial_port *port = this_cpu(poll_port);
     struct ns16550 *uart = port->uart;
 
-    if ( uart->intr_works )
+    if ( uart->intr_works == intr_on )
         return; /* Interrupts work - no more polling */
 
     while ( ns_read_reg(uart, UART_LSR) & UART_LSR_DR )
@@ -305,7 +322,8 @@ static void ns16550_setup_preirq(struct ns16550 *uart)
     unsigned char lcr;
     unsigned int  divisor;
 
-    uart->intr_works = 0;
+    if ( uart->intr_works != polling )
+        uart->intr_works = intr_off;
 
     pci_serial_early_init(uart);
 
@@ -394,7 +412,7 @@ static void __init cf_check ns16550_init_irq(struct serial_port *port)
 
 static void ns16550_setup_postirq(struct ns16550 *uart)
 {
-    if ( uart->irq > 0 )
+    if ( uart->intr_works != polling )
     {
         /* Master interrupt enable; also keep DTR/RTS asserted. */
         ns_write_reg(uart,
@@ -473,6 +491,7 @@ static void __init cf_check ns16550_init_postirq(struct serial_port *port)
                 if ( rc )
                 {
                     uart->irq = 0;
+                    uart->intr_works = polling;
                     if ( msi_desc )
                         msi_free_irq(msi_desc);
                     else
@@ -488,7 +507,7 @@ static void __init cf_check ns16550_init_postirq(struct serial_port *port)
     }
 #endif
 
-    if ( uart->irq > 0 )
+    if ( uart->intr_works != polling )
     {
         uart->irqaction.handler = ns16550_interrupt;
         uart->irqaction.name    = "ns16550";
@@ -595,7 +614,8 @@ static void __init cf_check ns16550_endboot(struct serial_port *port)
 static int __init cf_check ns16550_irq(struct serial_port *port)
 {
     struct ns16550 *uart = port->uart;
-    return ((uart->irq > 0) ? uart->irq : -1);
+
+    return ((uart->intr_works != polling) ? uart->irq : -1);
 }
 
 static void cf_check ns16550_start_tx(struct serial_port *port)
@@ -1333,9 +1353,13 @@ pci_uart_config(struct ns16550 *uart, bool_t skip_amt, unsigned int idx)
                     uart->irq = 0;
 #endif
                 if ( !uart->irq )
+                {
+                    uart->intr_works = polling;
+
                     printk(XENLOG_INFO
                            "ns16550: %pp: no legacy IRQ, using poll mode\n",
                            &PCI_SBDF(0, b, d, f));
+                }
 
                 return 0;
             }
@@ -1374,19 +1398,6 @@ static void enable_exar_enhanced_bits(const struct ns16550 *uart)
 
 #endif /* CONFIG_HAS_PCI */
 
-/*
- * Configure serial port with a string:
- *   <baud>[/<base_baud>][,DPS[,<io-base>[,<irq>[,<port-bdf>[,<bridge-bdf>]]]]].
- * The tail of the string can be omitted if platform defaults are sufficient.
- * If the baud rate is pre-configured, perhaps by a bootloader, then 'auto'
- * can be specified in place of a numeric baud rate. Polled mode is specified
- * by requesting irq 0.
- */
-static char __initdata opt_com1[128] = "";
-static char __initdata opt_com2[128] = "";
-string_param("com1", opt_com1);
-string_param("com2", opt_com2);
-
 enum serial_param_type {
     baud,
     clock_hz,
@@ -1555,6 +1566,9 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
         }
         else
 #endif
+        if ( strncmp(conf, "poll", 4) == 0 )
+            uart->intr_works = polling;
+        else
             uart->irq = simple_strtol(conf, &conf, 10);
     }
 
@@ -1760,6 +1774,9 @@ static int __init ns16550_uart_dt_init(struct dt_device_node *dev,
 
     ns16550_init_common(uart);
 
+    if ( strstr(opt_com1, "poll") )
+        uart->intr_works = polling;
+
     uart->baud      = BAUD_AUTO;
     uart->data_bits = 8;
     uart->parity    = UART_PARITY_NONE;
@@ -1791,9 +1808,13 @@ static int __init ns16550_uart_dt_init(struct dt_device_node *dev,
     }
 
     res = platform_get_irq(dev, 0);
-    if ( ! res )
-        return -EINVAL;
-    uart->irq = res;
+    if ( res < 0 )
+    {
+        printk("there is no interrupt property, polling will be used\n");
+        uart->intr_works = polling;
+    }
+    else
+        uart->irq = res;
 
     uart->dw_usr_bsy = dt_device_is_compatible(dev, "snps,dw-apb-uart");
 
-- 
2.41.0
Re: [PATCH] ns16550: add support for polling mode when device tree is used
Posted by Jan Beulich 8 months, 2 weeks ago
On 11.08.2023 17:30, Oleksii Kurochko wrote:
> @@ -1555,6 +1566,9 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
>          }
>          else
>  #endif
> +        if ( strncmp(conf, "poll", 4) == 0 )
> +            uart->intr_works = polling;

Don't you need to update "conf" here as well then?

As said before, please also update parse_namevalue_pairs(). I would
appreciate (but not insist) if you also added recognition of "msi"
at this occasion.

> +        else
>              uart->irq = simple_strtol(conf, &conf, 10);
>      }
>  
> @@ -1760,6 +1774,9 @@ static int __init ns16550_uart_dt_init(struct dt_device_node *dev,
>  
>      ns16550_init_common(uart);
>  
> +    if ( strstr(opt_com1, "poll") )
> +        uart->intr_works = polling;

Is strstr() really appropriate? Shouldn't it simply be strcmp(), with
there not being any other sub-options in the non-x86 case?

Plus the question remains of it necessarily being com1: Is there no
way with DT to have multiple serial ports (e.g. one for the console
and one for a debugger)? If there indeed isn't, then unconditionally
using opt_com1[] here is of course okay, but then opt_com2[]
is effectively a dead variable and recognizing "com2" on the command
line (rather than spitting out an error) is then also a mistake. IOW
in that case both would want keeping x86-only (with a new #ifdef, as
we certainly don't want to have com1 and com2 stuff in separate
places).

Jan
Re: [PATCH] ns16550: add support for polling mode when device tree is used
Posted by Oleksii 8 months, 2 weeks ago
On Wed, 2023-08-16 at 15:39 +0200, Jan Beulich wrote:
> On 11.08.2023 17:30, Oleksii Kurochko wrote:
> > @@ -1555,6 +1566,9 @@ static bool __init parse_positional(struct
> > ns16550 *uart, char **str)
> >          }
> >          else
> >  #endif
> > +        if ( strncmp(conf, "poll", 4) == 0 )
> > +            uart->intr_works = polling;
> 
> Don't you need to update "conf" here as well then?
Yes, sure, 'conf' shoud be updated too.

> 
> As said before, please also update parse_namevalue_pairs(). I would
> appreciate (but not insist) if you also added recognition of "msi"
> at this occasion.
I'll add 'msi' too.

> 
> > +        else
> >              uart->irq = simple_strtol(conf, &conf, 10);
> >      }
> >  
> > @@ -1760,6 +1774,9 @@ static int __init ns16550_uart_dt_init(struct
> > dt_device_node *dev,
> >  
> >      ns16550_init_common(uart);
> >  
> > +    if ( strstr(opt_com1, "poll") )
> > +        uart->intr_works = polling;
> 
> Is strstr() really appropriate? Shouldn't it simply be strcmp(), with
> there not being any other sub-options in the non-x86 case?
It would better to use strcmp(). Thanks.

> 
> Plus the question remains of it necessarily being com1: Is there no
> way with DT to have multiple serial ports (e.g. one for the console
> and one for a debugger)? If there indeed isn't, then unconditionally
> using opt_com1[] here is of course okay, but then opt_com2[]
> is effectively a dead variable and recognizing "com2" on the command
> line (rather than spitting out an error) is then also a mistake. IOW
> in that case both would want keeping x86-only (with a new #ifdef, as
> we certainly don't want to have com1 and com2 stuff in separate
> places).
Actually it can be even more serial ports. For example, I have a board
with 3 UARTs ( serial ports ). 
In this case, it looks that I should have 3 variable of opt_com{1-3}[]?

Taking into account that opt_com{1-2} variables are needed only for
configuration of serial ports in X86 ( in DT-based architectures all
configuration info is inside a node of UART ) then we can check only
opt_com1[].

~ Oleksii