serial: core: Start managing serial controllers to enable runtime PM
PATCH v3 1/2 serial: core: Start managing serial controllers to enable runtime PM
We want to enable runtime PM for serial port device drivers in a generic way. To do this, we want to have the serial core layer manage the registered physical serial controller devices.
The serial layer has a few challenges to deal with:
1. The serial port mapping to a physical serial port controller device is currently not easily available after the physical serial controller struct device gets registered uart_add_one_port() time
2. The serial port device drivers have their own driver data. So we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers
To find the serial ports for a controller based on struct device, let's add a new data structure for a serial_controller. On registering a port, we can use the drv->state array to find the associated serial port controller and initialize the serial core controller.
As some serial port device drivers enable runtime PM in their probe before registering with the serial core layer, and some do not enable runtime PM at all currently, we need check the state in the serial core layer on uart_port_startup(). We need to also consider that a serial port device may have multiple ports.
Initially we enable runtime PM for all the serial port controller devices. This allows us to add runtime PM calls and properly handle any errors without a need for serial layer specific runtime PM wrapper functions.
After this patch no functional changes for the serial port device drivers are intended. We just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing PM runtime, we just keep track of the runtime PM configuration.
For the serial port drivers, the serial core layer has the following use cases to deal with:
1. If a serial port device driver does not implement runtime PM, the device state is set to active state, and the runtime PM usage count is kept until the last port for a device is unregistered
2. If a serial port device driver implements runtime PM, the runtime PM usage count is kept until the last port for the device is unregistered
3. If a serial port device driver implements runtime PM autosuspend, autosuspend is not prevented. This currently gets set only for the 8250_omap driver to keep runtime PM working for it
For system suspend, things should be mostly detached from the runtime PM. The serial port device drivers may call pm_runtime_force_suspend() and pm_runtime_force_resume() as needed.
---
Changes since v2:
- Make each serial port a proper device as suggested by Greg. This is a separate patch that flushes the TX on runtime PM resume
Changes since v1:
- Use kref as suggested by Andy
- Fix memory leak on error as noted by Andy
- Use use unsigned char for supports_autosuspend as suggested by Andy
- Coding style improvments as suggested by Andy
--- drivers/tty/serial/8250/8250_core.c | 1 + drivers/tty/serial/8250/8250_omap.c | 1 + drivers/tty/serial/serial_core.c | 179 ++++++++++++++++++++++++++++ include/linux/serial_core.h | 4 + 4 files changed, 185 insertions(+)
drivers/tty/serial/8250/8250_core.c +1 -0
@@ -1004,6 +1004,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up) uart->port.regshift = up->port.regshift; uart->port.iotype = up->port.iotype; uart->port.flags = up->port.flags |UPF_BOOT_AUTOCONF ; + uart->port.supports_autosuspend = up->port.supports_autosuspend; uart->bugs = up->bugs; uart->port.mapbase = up->port.mapbase; uart->port.mapsize = up->port.mapsize;
drivers/tty/serial/8250/8250_omap.c +1 -0
@@ -1330,6 +1330,7 @@ static int omap8250_probe(struct platform_device *pdev) up.rs485_start_tx = serial8250_em485_start_tx; up.rs485_stop_tx = serial8250_em485_stop_tx; up.port.has_sysrq =IS_ENABLED (CONFIG_SERIAL_8250_CONSOLE ); + up.port.supports_autosuspend =1 ; ret =of_alias_get_id (np,"serial" );if (ret <0 ) {
drivers/tty/serial/serial_core.c +179 -0
215 lines not on this page · raw message
include/linux/serial_core.h +4 -0
@@ -28,6 +28,7 @@struct uart_port ;struct serial_struct ; +struct serial_port_controller ;struct device ;struct gpio_desc ;
@@ -573,6 +574,7 @@ struct uart_port {unsigned char hub6;/* this should be in the 8250 driver */ unsigned char suspended;unsigned char console_reinit; +unsigned char supports_autosuspend;const char *name;/* port name */ struct attribute_group *attr_group;/* port specific attributes */ const struct attribute_group **tty_groups;/* all attributes (serial core use only) */
@@ -611,6 +613,8 @@ enum uart_pm_state {struct uart_state {struct tty_port port; +struct serial_controller *controller; +enum uart_pm_state pm_state;struct circ_buf xmit;
Signature · 1 line
2.38.1
PATCH v3 2/2 serial: core: Add port port device to flush TX on runtime resume
With PM runtime enabled for the serial port controllers, we can now flush pending TX for the port on runtime PM resume as suggested by Johan Hovold <[email protected]>.
To flush the pending TX, let's set up each port as a proper device as suggested by Greg Kroah-Hartman <[email protected]>.
We set up each port as a child device for the serial port controller device. We use platform device for this and pass the port information in platform_data.
Let's just do mimimal changes needed for now, more port specific code can be then moved from serial_core.c to serial_port.c as needed.
--- drivers/tty/serial/Makefile | 2 +- drivers/tty/serial/serial_core.c | 77 ++++++++++++++++++++- drivers/tty/serial/serial_port.c | 111 +++++++++++++++++++++++++++++++ drivers/tty/serial/serial_port.h | 16 +++++ include/linux/serial_core.h | 1 + 5 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 drivers/tty/serial/serial_port.c create mode 100644 drivers/tty/serial/serial_port.h
drivers/tty/serial/Makefile +1 -1
@@ -3,7 +3,7 @@ # Makefile for the kernel serial device drivers. # -obj-$(CONFIG_SERIAL_CORE) += serial_core.o +obj-$(CONFIG_SERIAL_CORE) += serial_core.o serial_port.o obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o obj-$(CONFIG_SERIAL_EARLYCON_ARM_SEMIHOST) += earlycon-arm-semihost.o
drivers/tty/serial/serial_core.c +75 -2
138 lines not on this page · raw message
drivers/tty/serial/serial_port.c new file +111 -0
111 lines not on this page · raw message
drivers/tty/serial/serial_port.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/** +* struct serial_port_platdata - Serial port platform data +* @state: serial port state +* +* Used by serial_core and serial_port only. Allocated on uart_add_one_port(), +* and freed on uart_remove_one_port(). Note that the life cycle for uart_state +* is different from serial_port_device. +*/ +struct serial_port_platdata { +struct uart_state *state; +}; + +extern int serial_port_get (struct uart_state *state); +extern void serial_port_put (struct uart_state *state);
include/linux/serial_core.h +1 -0
@@ -614,6 +614,7 @@ struct uart_state {struct tty_port port;struct serial_controller *controller; +struct device *port_dev;enum uart_pm_state pm_state;struct circ_buf xmit;
Signature · 1 line
2.38.1
Re: [PATCH v3 2/2] serial: core: Add port port device to flush TX on runtime resume
Quoted 3 lines · Tony Lindgren
With PM runtime enabled for the serial port controllers, we can now flush pending TX for the port on runtime PM resume as suggested by Johan Hovold <[email protected]>.
I believe it's quite a duplication of email addresses (they are in Cc and will be on lore.kernel.org) and also below.
Quoted 9 lines
To flush the pending TX, let's set up each port as a proper device as suggested by Greg Kroah-Hartman <[email protected]>.
We set up each port as a child device for the serial port controller device. We use platform device for this and pass the port information in
platform_data.Let's just do mimimal changes needed for now, more port specific code can be then moved from
serial_core.ctoserial_port.cas needed.
...
Quoted 19 lines
drivers/tty/serial/serial_core.c +19
-0+static int serial_core_add_port_device (struct uart_port *port) +{ +struct serial_port_platdata pd; +struct platform_device *pdev; +int ret; + + pdev =platform_device_alloc ("serial-port" ,PLATFORM_DEVID_AUTO ); +if (!pdev) +return -ENOMEM ; + + pdev->dev.parent = port->dev; + pd.state = port->state; + + ret =platform_device_add_data (pdev, &pd,sizeof (pd)); +if (ret) + goto err_put; + + ret =platform_device_add (pdev); +if (ret)
drivers/tty/serial/serial_core.c serial_core_find_controller(struct uart_driver *drv, struct device *dev) · line 344
Quoted 10 lines
drivers/tty/serial/serial_core.c +10
-0+ goto err_put; + + port->state->port_dev = &pdev->dev; + +return 0 ; + +err_put: +platform_device_put (pdev); + +return ret;
Can we simply utilize platform_device_register_full()?
Quoted 1 line
+}
...
Quoted 1 line
+EXPORT_SYMBOL(serial_port_get);
Can we move these to namespace from day 1?
...
drivers/tty/serial/serial_port.c line 47
Quoted 2 lines
drivers/tty/serial/serial_port.c new file +2
-0+return (!uart_tx_stopped (port) && +uart_circ_chars_pending (&port->state->xmit));
Outer parentheses are redundant.
...
drivers/tty/serial/serial_port.c line 68
Quoted 3 lines
drivers/tty/serial/serial_port.c new file +3
-0+static const struct dev_pm_ops serial_port_pm = { +SET_RUNTIME_PM_OPS (NULL , serial_port_runtime_resume,NULL ) +};
Can't we use new macros / helpers which starts from DEFINE_...
...
drivers/tty/serial/serial_port.c line 84
Quoted 2 lines
drivers/tty/serial/serial_port.c new file +2
-0+pm_runtime_set_autosuspend_delay (&pdev->dev, +SERIAL_PORT_AUTOSUSPEND_DELAY_MS );
With temporary
struct device *dev = &pdev->dev;
this in particular become one line.
...
drivers/tty/serial/serial_port.c line 101
Quoted 1 line
drivers/tty/serial/serial_port.c new file +1
-0+ .pm = &serial_port_pm,
As per above, use pm_ptr() ?
...
Quoted 1 line
+
Unneeded blank line.
Quoted 1 line
+module_platform_driver(serial_port_driver);
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 2/2] serial: core: Add port port device to flush TX on runtime resume
Hi,
Thanks for your review again, will fix what you noted. One idea for an improvment below though.
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Tony Lindgren
+EXPORT_SYMBOL(serial_port_get);
Can we move these to namespace from day 1?
Assuming you mean EXPORT_SYMBOL_NS(), sure.
But we might be better off doing the following:
- Move already exported uart_add_one_port() and uart_remove_one_port() from serial_core to serial_port as wrapper functions for serial_core
- Export new functions in serial_core for serial_core_register_port() and serial_core_unregister_port() for serial_port to call
This would ensure both serial_core and serial_port modules are always loaded when a hardware specific serial port driver is loaded.
This should also leave out the need for serial_port_get() and serial_port_put().
Regards,
Tony
Re: [PATCH v3 2/2] serial: core: Add port port device to flush TX on runtime resume
Quoted 16 lines · Tony Lindgren
Quoted 4 lines · Andy Shevchenko
Quoted 1 line · Tony Lindgren
+EXPORT_SYMBOL(serial_port_get);
Can we move these to namespace from day 1?
Assuming you mean
EXPORT_SYMBOL_NS(), sure.But we might be better off doing the following:
- Move already exported
uart_add_one_port()anduart_remove_one_port()fromserial_coretoserial_portas wrapper functions forserial_core- Export new functions in
serial_coreforserial_core_register_port()andserial_core_unregister_port()forserial_portto call
Quoted 6 lines
This would ensure both
serial_coreandserial_portmodules are always loaded when a hardware specific serial port driver is loaded.This should also leave out the need for
serial_port_get()andserial_port_put().
Yes, this is good idea!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 52 lines · Tony Lindgren
We want to enable runtime PM for serial port device drivers in a generic way. To do this, we want to have the serial core layer manage the registered physical serial controller devices.
The serial layer has a few challenges to deal with:
1. The serial port mapping to a physical serial port controller device is currently not easily available after the physical serial controller struct device gets registered
uart_add_one_port()time2. The serial port device drivers have their own driver data. So we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers
To find the serial ports for a controller based on struct device, let's add a new data structure for a
serial_controller. On registering a port, we can use thedrv->statearray to find the associated serial port controller and initialize the serial core controller.As some serial port device drivers enable runtime PM in their probe before registering with the serial core layer, and some do not enable runtime PM at all currently, we need check the state in the serial core layer on
uart_port_startup(). We need to also consider that a serial port device may have multiple ports.Initially we enable runtime PM for all the serial port controller devices. This allows us to add runtime PM calls and properly handle any errors without a need for serial layer specific runtime PM wrapper functions.
After this patch no functional changes for the serial port device drivers are intended. We just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing PM runtime, we just keep track of the runtime PM configuration.
For the serial port drivers, the serial core layer has the following use cases to deal with:
1. If a serial port device driver does not implement runtime PM, the device state is set to active state, and the runtime PM usage count is kept until the last port for a device is unregistered
2. If a serial port device driver implements runtime PM, the runtime PM usage count is kept until the last port for the device is unregistered
3. If a serial port device driver implements runtime PM autosuspend, autosuspend is not prevented. This currently gets set only for the 8250_omap driver to keep runtime PM working for it
For system suspend, things should be mostly detached from the runtime PM. The serial port device drivers may call
pm_runtime_force_suspend()andpm_runtime_force_resume()as needed.
...
Quoted 1 line
+static struct
serial_controller*to_controller(structuart_port*port)
Perhaps to_serial_controller()? Or to_serial_ctrl() ?
Quoted 6 lines
drivers/tty/serial/serial_core.c +6
-0+{ +if (!port->dev) +return NULL ; + +return port->state->controller; +}
...
Quoted 4 lines
+/*
+ * Starts runtime PM for the serial controller device if not already started
+ * by the serial port driver. Called fromuart_add_one_port()withport_mutex
+ * held.
Perhaps we may utilize lockdep_assert() here?
Quoted 1 line
+ */
Also maybe might_sleep() can be added where it's appropriate?
...
Quoted 11 lines
drivers/tty/serial/serial_core.c +11
-0+static int serial_core_register_port (struct uart_port *port, +struct uart_driver *drv) +{ +struct serial_controller *controller; +bool allocated = false; +int ret; + +if (!port->dev) +return 0 ; + + controller =serial_core_find_controller (drv, port->dev);
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 314
Quoted 10 lines
drivers/tty/serial/serial_core.c +9
-0+if (!controller) { + controller =kzalloc (sizeof (*controller),GFP_KERNEL ); +if (!controller) +return -ENOMEM ; + + controller->drv = drv; + controller->dev = port->dev; + controller->supports_autosuspend = port->supports_autosuspend; + allocated = true;
Maybe split this to a serial_core_controller_alloc()?
Quoted 18 lines
drivers/tty/serial/serial_core.c +18
-0+ } + + port->state->controller = controller; +WARN_ON (port->supports_autosuspend != controller->supports_autosuspend); + + ret =serial_core_pm_runtime_start (port); +if (ret <0 ) + goto err_free; + +return 0 ; + +err_free: + port->state->controller =NULL ; +if (allocated) +kfree (controller); + +return ret; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 173 lines · Tony Lindgren
We want to enable runtime PM for serial port device drivers in a generic way. To do this, we want to have the serial core layer manage the registered physical serial controller devices.
The serial layer has a few challenges to deal with:
1. The serial port mapping to a physical serial port controller device is currently not easily available after the physical serial controller struct device gets registered
uart_add_one_port()time2. The serial port device drivers have their own driver data. So we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers
To find the serial ports for a controller based on struct device, let's add a new data structure for a
serial_controller. On registering a port, we can use thedrv->statearray to find the associated serial port controller and initialize the serial core controller.As some serial port device drivers enable runtime PM in their probe before registering with the serial core layer, and some do not enable runtime PM at all currently, we need check the state in the serial core layer on
uart_port_startup(). We need to also consider that a serial port device may have multiple ports.Initially we enable runtime PM for all the serial port controller devices. This allows us to add runtime PM calls and properly handle any errors without a need for serial layer specific runtime PM wrapper functions.
After this patch no functional changes for the serial port device drivers are intended. We just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing PM runtime, we just keep track of the runtime PM configuration.
For the serial port drivers, the serial core layer has the following use cases to deal with:
1. If a serial port device driver does not implement runtime PM, the device state is set to active state, and the runtime PM usage count is kept until the last port for a device is unregistered
2. If a serial port device driver implements runtime PM, the runtime PM usage count is kept until the last port for the device is unregistered
3. If a serial port device driver implements runtime PM autosuspend, autosuspend is not prevented. This currently gets set only for the 8250_omap driver to keep runtime PM working for it
For system suspend, things should be mostly detached from the runtime PM. The serial port device drivers may call
pm_runtime_force_suspend()andpm_runtime_force_resume()as needed.Suggested-by Johan Hovold <[email protected]>Signed-off-by Tony Lindgren <[email protected]>---
Changes since v2:- Make each serial port a proper device as suggested by Greg. This is a separate patch that flushes the TX on runtime PM resume
Changes since v1:
- Use kref as suggested by Andy
- Fix memory leak on error as noted by Andy
- Use use unsigned char for
supports_autosuspendas suggested by Andy- Coding style improvments as suggested by Andy
--- drivers/tty/serial/8250/8250_core.c | 1 + drivers/tty/serial/8250/8250_omap.c | 1 + drivers/tty/serial/serial_core.c | 179 ++++++++++++++++++++++++++++ include/linux/serial_core.h | 4 + 4 files changed, 185 insertions(+)drivers/tty/serial/8250/8250_core.c +1
-0@@ -1004,6 +1004,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up) uart->port.regshift = up->port.regshift; uart->port.iotype = up->port.iotype; uart->port.flags = up->port.flags |UPF_BOOT_AUTOCONF ; + uart->port.supports_autosuspend = up->port.supports_autosuspend; uart->bugs = up->bugs; uart->port.mapbase = up->port.mapbase; uart->port.mapsize = up->port.mapsize;drivers/tty/serial/8250/8250_omap.c +1
-0@@ -1330,6 +1330,7 @@ static int omap8250_probe(struct platform_device *pdev) up.rs485_start_tx = serial8250_em485_start_tx; up.rs485_stop_tx = serial8250_em485_stop_tx; up.port.has_sysrq =IS_ENABLED (CONFIG_SERIAL_8250_CONSOLE ); + up.port.supports_autosuspend =1 ; ret =of_alias_get_id (np,"serial" );if (ret <0 ) {drivers/tty/serial/serial_core.c +52
-0@@ -16,6 +16,7 @@#include <linux/console.h> #include <linux/gpio/consumer.h> #include <linux/of.h> +#include <linux/pm_runtime.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/device.h> @@ -30,6 +31,20 @@#include <linux/irq.h> #include <linux/uaccess.h> +/* +* Serial port device specific data for serial core. +* +* Each port device can have multiple ports with struct uart_state allocated +* for each port. The array of ports is kept in struct uart_driver. +*/ +struct serial_controller { +struct device *dev;/* Physical controller device */ +struct uart_driver *drv;/* For port specific uart_state */ +struct kref ref;/* Registered port count */ +unsigned long implements_pm_runtime:1 ; +unsigned long supports_autosuspend:1 ; +}; +/* * This is used to lock changes in serial line configuration. */ @@ -177,6 +192,162 @@ static void uart_port_dtr_rts(struct uart_port *uport, int raise)uart_clear_mctrl (uport,TIOCM_DTR |TIOCM_RTS ); } +static struct serial_controller *to_controller (struct uart_port *port) +{ +if (!port->dev) +return NULL ; + +return port->state->controller; +} + +/* +* Starts runtime PM for the serial controller device if not already started +* by the serial port driver. Called from uart_add_one_port() with port_mutex +* held. +*/ +static int serial_core_pm_runtime_start (struct uart_port *port) +{ +struct serial_controller *controller =to_controller (port); +struct device *dev = port->dev; +int ret =0 ; + +if (kref_get_unless_zero (&controller->ref)) +return 0 ; + +/* Init controller device on first reference */ +kref_init (&controller->ref); + +/* Always enable autosuspend and consider child devices for serdev */ +pm_runtime_use_autosuspend (dev); +pm_suspend_ignore_children (dev, false); + +/* +* If the port driver did not enable runtime PM in probe, do it now. +* Devices that did not enable runtime PM get set active so we can +* properly handle the returned errors for runtime PM calls. +*/ +if (!pm_runtime_enabled (dev)) { +pm_runtime_set_active (dev); +pm_runtime_enable (dev);
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 241
Quoted 10 lines
drivers/tty/serial/serial_core.c +10
-0+ }else { + controller->implements_pm_runtime =1 ; + } + +/* +* Keep the port device enabled unless autosuspend is supported. +* Released on port shutdown. +*/ +if (!controller->supports_autosuspend) { + ret =pm_runtime_resume_and_get (dev);
Should this be done regardless of autosuspend if the port is console?
There's a problem in the current place where this being called from though, uart_console_enabled() doesn't return the correct value with at least some devices this early:
Signature · 1 line
i.
Hi,
Quoted 9 lines · Ilpo Järvinen
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 241
Quoted 6 lines · Tony Lindgren
drivers/tty/serial/serial_core.c +6
-0+/* +* Keep the port device enabled unless autosuspend is supported. +* Released on port shutdown. +*/ +if (!controller->supports_autosuspend) { + ret =pm_runtime_resume_and_get (dev);Should this be done regardless of autosuspend if the port is console?
Well hopefully no need to check anything here for the console unless enabled state needs to be inherited from the early console.
Note that with what Jiri is proposing, we can just unconditionally do the pm_runtime_resume_and_get() here on the generic serial port controller struct device. We can also leave out the tinkering of the serial port hardware struct device.
The hardware serial port controller drivers that do support autosuspend can just decrement the runtime PM usage count for the generic serial port controller child device as needed to enable aggressive PM.
Quoted 5 lines
There's a problem in the current place where this being called from though,
uart_console_enabled()doesn't return the correct value with at least some devices this early:
OK. Seems the issues with the boot console and early serial port struct device or whatever might be missing are there even without runtime PM.
Regards,
Tony