serial: core: Start managing serial controllers to enable runtime PM
PATCH 1/1 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 serial port controllers. For runtime PM, we need a way to find the serial ports for each serial port controller device.
The serial core manages ports. Each serial controller can have multiple ports. As serial core has no struct device, and the serial port device drivers have their own driver data, we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers.
We could consider adding a serial core specific struct device. It would be a child of the serial port device, and would allow us eventually to use device_links to add generic runtime PM calls for example. But as the serial core layer is not a device driver, driver specific features would need to be added, and are probably not justified for a virtual device.
Considering the above, let's improve the serial core layer so we can manage the serial port controllers better. Let's register the controllers with the serial core layer in addition to the serial ports.
To find the serial ports for a controller based on struct device, let's add a new data structure for a serial_controller. Let's add the registered devices into a radix_tree so we can look up the controller easily even with many controllers registered. This allows us to keep track of the runtime PM state for each serial port controller device.
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 just want to 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. For most cases, we just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing runtime PM, we just keep track of the configuration.
The serial core layer has the following use cases to deal with:
- 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
- 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
- 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.
--- drivers/tty/serial/8250/8250_core.c | 1 + drivers/tty/serial/8250/8250_omap.c | 1 + drivers/tty/serial/serial_core.c | 181 ++++++++++++++++++++++++++++ include/linux/serial_core.h | 3 + 4 files changed, 186 insertions(+)
drivers/tty/serial/8250/8250_core.c +1 -0
@@ -996,6 +996,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
@@ -1338,6 +1338,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 +181 -0
230 lines not on this page · raw message
include/linux/serial_core.h +3 -0
@@ -249,6 +249,7 @@ struct uart_port {unsigned char hub6;/* this should be in the 8250 driver */ unsigned char suspended;unsigned char console_reinit; + unsigned long supports_autosuspend:1;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) */
@@ -284,6 +285,8 @@ enum uart_pm_state {* This is the state information which is persistent across opens. */ struct uart_state { +struct serial_controller *controller; +struct tty_port port;enum uart_pm_state pm_state;
Signature · 1 line
2.35.1
Quoted 60 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 serial port controllers. For runtime PM, we need a way to find the serial ports for each serial port controller device.
The serial core manages ports. Each serial controller can have multiple ports. As serial core has no struct device, and the serial port device drivers have their own driver data, we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers.
We could consider adding a serial core specific struct device. It would be a child of the serial port device, and would allow us eventually to use
device_linksto add generic runtime PM calls for example. But as the serial core layer is not a device driver, driver specific features would need to be added, and are probably not justified for a virtual device.Considering the above, let's improve the serial core layer so we can manage the serial port controllers better. Let's register the controllers with the serial core layer in addition to the serial ports.
To find the serial ports for a controller based on struct device, let's add a new data structure for a
serial_controller. Let's add the registered devices into aradix_treeso we can look up the controller easily even with many controllers registered. This allows us to keep track of the runtime PM state for each serial port controller device.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 just want to 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. For most cases, we just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing runtime PM, we just keep track of the configuration.
The serial core layer has the following use cases to deal with:
- 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
- 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
- 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.
...
drivers/tty/serial/serial_core.c line 43
Quoted 5 lines
drivers/tty/serial/serial_core.c +4
-0+struct serial_controller { +struct uart_driver *drv;/* For port specific uart_state */ +struct mutex lock;/* For changing enabled_count */ +int enabled_count;/* Enable count for runtime PM */
Wondering if we may use kref instead which will check for saturation as well.
Quoted 3 lines
drivers/tty/serial/serial_core.c +3
-0+ unsigned long implements_pm_runtime:1; + unsigned long supports_autosuspend:1; +};
...
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 207
Quoted 2 lines
drivers/tty/serial/serial_core.c +2
-0+WARN_ON (port->supports_autosuspend != + controller->supports_autosuspend);
One line?
...
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 220
Quoted 10 lines
drivers/tty/serial/serial_core.c +10
-0+ controller =kzalloc (sizeof (*controller),GFP_KERNEL ); +if (!controller) +return -ENOMEM ; + +mutex_init (&controller->lock); + controller->drv = drv; + controller->supports_autosuspend = port->supports_autosuspend; + port->state->controller = controller; + +return radix_tree_insert (&serial_core_devices, idx, controller);
Hmm... Memory leak at error?
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!idx) + return;
Do you really need this?
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 230
Quoted 3 lines
drivers/tty/serial/serial_core.c +3
-0+ controller =radix_tree_lookup (&serial_core_devices, idx); +if (!controller) +return ;
...and/or this?
Quoted 3 lines
drivers/tty/serial/serial_core.c +3
-0+ controller =radix_tree_delete (&serial_core_devices, idx); +if (!controller) +return ;
...
Review
Quoted 1 line
(quoted diff) +1
-0+ struct device *dev = port->dev;
I would split assignment to be closer to first user...
...
...somewhere here.
Quoted 2 lines
(quoted diff) +2
-0+ if (!dev) + return 0;
...
drivers/tty/serial/serial_core.c static void uart_port_dtr_rts(struct uart_port *uport, int raise) · line 310
Quoted 6 lines
drivers/tty/serial/serial_core.c +5
-0+struct serial_controller *controller; +struct device *dev = port->dev; +struct uart_state *state; +if (!dev) +return ;
Ditto.
...
include/linux/serial_core.h struct uart_port { · line 252
Quoted 4 lines
include/linux/serial_core.h +1
-0unsigned char hub6;/* this should be in the 8250 driver */ unsigned char suspended;unsigned char console_reinit; + unsigned long supports_autosuspend:1;
Hmm... Maybe use unsigned char and convert all of them to something else if needed?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
drivers/tty/serial/serial_core.c line 43
Quoted 5 lines · Tony Lindgren
drivers/tty/serial/serial_core.c +4
-0+struct serial_controller { +struct uart_driver *drv;/* For port specific uart_state */ +struct mutex lock;/* For changing enabled_count */ +int enabled_count;/* Enable count for runtime PM */ Wondering if we may use kref instead which will check for saturation as well.
Thanks for the quick review, using kref is a good idea.
Regards,
Tony
Quoted 7 lines · Andy Shevchenko
include/linux/serial_core.h struct uart_port { · line 252
Quoted 4 lines · Tony Lindgren
include/linux/serial_core.h +1
-0unsigned char hub6;/* this should be in the 8250 driver */ unsigned char suspended;unsigned char console_reinit; + unsigned long supports_autosuspend:1;Hmm... Maybe use unsigned char and convert all of them to something else if needed?
Sorry forgot to reply to this. This can be unsigned char no problem. Most of the runtime PM related flags are in the struct serial_controller anyways now.
Regards,
Tony