Skip to contentSkip to conversation

serial: core: Start managing serial controllers to enable runtime PM

PATCH Superseded Tony Lindgren 3 participants 9 messages Mar 9, 2023 – Mar 14, 2023

Subsystems (2)

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 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 physical serial controller devices.

To do this, let's set up a struct bus and struct device for the serial core controller as suggested by Greg and Jiri. The serial core controller devices are children of the physical serial port device. The serial core controller device is needed to support multiple different kind of ports connected to single physical serial port device.

Let's also set up a struct device for the serial core port. The serial core port instances are children of the serial core controller device.

We need to also update the documentation a bit as suggested by Andy.

With the serial core port device we can now flush pending TX on the runtime PM resume as suggested by Johan.

---

Changes since v5:

- Replace platform bus and device with bus_add() and device_add(), Greg did not like platform bus and device here. This also gets rid of the need for platform data with struct serial_base_device, see new file serial_base.c

- Update documentation to drop reference to struct uart_device as suggested by Andy

Changes since v4:

- Fix issue noted by Ilpo by calling serial_core_add_one_port() after the devices are created

Changes since v3:

- Simplify things by adding a serial core control device as the child of the physical serial port as suggested by Jiri

- Drop the tinkering of the physical serial port device for runtime PM. Serial core just needs to manage port->port_dev with the addition of the serial core control device and the device hierarchy will keep the pysical serial port device enabled as needed

- Simplify patch description with all the runtime PM tinkering gone

- Coding style improvments as noted by Andy

- Post as a single RFC patch as we're close to the merge window

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

---
 .../firmware-guide/acpi/enumeration.rst       |   5 +-
 drivers/tty/serial/8250/8250_core.c           |   1 +
 drivers/tty/serial/Makefile                   |   2 +-
 drivers/tty/serial/serial_base.c              |  98 ++++++++++
 drivers/tty/serial/serial_base.h              |  27 +++
 drivers/tty/serial/serial_core.c              | 176 +++++++++++++++++-
 drivers/tty/serial/serial_ctrl.c              |  72 +++++++
 drivers/tty/serial/serial_port.c              | 101 ++++++++++
 include/linux/serial_core.h                   |   4 +-
 9 files changed, 474 insertions(+), 12 deletions(-)
 create mode 100644 drivers/tty/serial/serial_base.c
 create mode 100644 drivers/tty/serial/serial_base.h
 create mode 100644 drivers/tty/serial/serial_ctrl.c
 create mode 100644 drivers/tty/serial/serial_port.c
Documentation/firmware-guide/acpi/enumeration.rst +2 -3
@@ -19,9 +19,8 @@ possible we decided to do following:
     platform devices.
 
   - Devices behind real busses where there is a connector resource
-    are represented as struct spi_device or struct i2c_device. Note
-    that standard UARTs are not busses so there is no struct uart_device,
-    although some of them may be represented by struct serdev_device.
+    are represented as struct spi_device, struct i2c_device or
+    struct serdev_device.
 
 As both ACPI and Device Tree represent a tree of devices (and their
 resources) this implementation follows the Device Tree way as much as
drivers/tty/serial/8250/8250_core.c +1 -0
@@ -996,6 +996,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up)
 		if (uart->port.dev)
 			uart_remove_one_port(&serial8250_reg, &uart->port);
 
+		uart->port.ctrl_id	= up->port.ctrl_id;
 		uart->port.iobase       = up->port.iobase;
 		uart->port.membase      = up->port.membase;
 		uart->port.irq          = up->port.irq;
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_base.o serial_core.o serial_ctrl.o serial_port.o
 
 obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
 obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
drivers/tty/serial/serial_base.c new file +98 -0

98 lines not on this page · raw message

drivers/tty/serial/serial_base.h new file +27 -0
new file mode 100644
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/* Serial core related functions, serial port device drivers do not need this. */
+
+struct device;
+struct uart_driver;
+struct uart_port;
+
+struct serial_base_device {
+	struct device dev;
+	struct uart_port *port;
+};
+
+#define to_serial_base_device(x) container_of((x), struct serial_base_device, dev)
+
+extern int serial_base_driver_register(struct device_driver *driver);
+extern void serial_base_driver_unregister(struct device_driver *driver);
+extern struct serial_base_device *serial_base_device_add(struct uart_port *port,
+							 const char *name,
+							 struct device *parent_dev);
+extern void serial_base_device_remove(struct serial_base_device *dev);
+
+extern int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port);
+extern void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port);
+
+extern int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
+extern void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);
drivers/tty/serial/serial_core.c +169 -7

240 lines not on this page · raw message

drivers/tty/serial/serial_ctrl.c new file +72 -0

72 lines not on this page · raw message

drivers/tty/serial/serial_port.c new file +101 -0

101 lines not on this page · raw message

include/linux/serial_core.h +3 -1
@@ -458,6 +458,7 @@ struct uart_port {
 						struct serial_rs485 *rs485);
 	int			(*iso7816_config)(struct uart_port *,
 						  struct serial_iso7816 *iso7816);
+	int			ctrl_id;		/* optional serial core controller id */
 	unsigned int		irq;			/* irq number */
 	unsigned long		irqflags;		/* irq flags  */
 	unsigned int		uartclk;		/* base uart clock */
@@ -563,7 +564,8 @@ struct uart_port {
 	unsigned int		minor;
 	resource_size_t		mapbase;		/* for ioremap */
 	resource_size_t		mapsize;
-	struct device		*dev;			/* parent device */
+	struct device		*dev;			/* serial port physical parent device */
+	struct device		*port_dev;		/* serial core port device */
 
 	unsigned long		sysrq;			/* sysrq timeout */
 	unsigned int		sysrq_ch;		/* char for sysrq */
Signature · 1 line
2.39.1
#1 lore raw
Quoted 10 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.

To do this, let's set up a struct bus and struct device for the serial core controller as suggested by Greg and Jiri. The serial core controller devices are children of the physical serial port device. The serial core controller device is needed to support multiple different kind of ports connected to single physical serial port device.

Quoted 7 lines

Let's also set up a struct device for the serial core port. The serial core port instances are children of the serial core controller device.

We need to also update the documentation a bit as suggested by Andy.

With the serial core port device we can now flush pending TX on the runtime PM resume as suggested by Johan.

Thanks, my comments below.

...

Documentation/firmware-guide/acpi/enumeration.rst possible we decided to do following: · line 23

Quoted 6 lines

- Devices behind real busses where there is a connector resource

Documentation/firmware-guide/acpi/enumeration.rst +2 -3
-    are represented as struct spi_device or struct i2c_device. Note
-    that standard UARTs are not busses so there is no struct uart_device,
-    although some of them may be represented by struct serdev_device.
+    are represented as struct spi_device, struct i2c_device or
+    struct serdev_device.

JFYI: the i2c_device will be changed soon to i2c_client in the v6.3-rcX, so this will have a conflict.

...

drivers/tty/serial/serial_base.c line 57

Quoted 1 line
drivers/tty/serial/serial_base.c new file +1 -0
+	if (!strncmp(name, "ctrl", 4)) {

Wouldn't str_has_previx() be better to show the intention?

Quoted 5 lines
drivers/tty/serial/serial_base.c new file +5 -0
+		id = port->ctrl_id;
+	} else {
+		id = port->line;
+		dev->port = port;
+	}

...

drivers/tty/serial/serial_base.c line 64

Quoted 1 line
drivers/tty/serial/serial_base.c new file +1 -0
+	dev_set_name(&dev->dev, "%s.%s.%d", name, dev_name(port->dev), id);

No error check?

...

drivers/tty/serial/serial_base.c line 68

Quoted 4 lines
drivers/tty/serial/serial_base.c new file +3 -0
+	ret = device_add(&dev->dev);
+	if (ret) {
 
+		kfree(dev);

Would it free the device name?

Quoted 2 lines
(quoted diff) +2 -0
+		return NULL;
+	}

...

Quoted 1 line

+EXPORT_SYMBOL_GPL(serial_base_device_add);

I'm wondering if we can use namespace from day 1 for this.

...

drivers/tty/serial/serial_base.c line 92

Quoted 11 lines
drivers/tty/serial/serial_base.c new file +10 -0
+static int serial_base_init(void)
+{
+	return bus_register(&serial_base_bus_type);
+}
+
+static void serial_base_exit(void)
+{
+	bus_unregister(&serial_base_bus_type);
+}
 
+

Redundant blank line and...

Quoted 1 line

+module_init(serial_base_init);

...move this to be after the function itself.

Quoted 1 line

+module_exit(serial_base_exit);

...

drivers/tty/serial/serial_base.h line 27

Quoted 12 lines
drivers/tty/serial/serial_base.h new file +12 -0
+extern int serial_base_driver_register(struct device_driver *driver);
+extern void serial_base_driver_unregister(struct device_driver *driver);
+extern struct serial_base_device *serial_base_device_add(struct uart_port *port,
+							 const char *name,
+							 struct device *parent_dev);
+extern void serial_base_device_remove(struct serial_base_device *dev);
+
+extern int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port);
+extern void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port);
+
+extern int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
+extern void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);

I believe you do not need "extern" for the function declarations here.

...

drivers/tty/serial/serial_core.c static void __uart_start(struct tty_struct *tty) · line 150

Quoted 1 line
drivers/tty/serial/serial_core.c +1 -0
+	err = pm_runtime_get(port_dev);

Is not sync API a deliberate choice? Do we need to comment on why is so?

...

Quoted 6 lines
drivers/tty/serial/serial_core.c +5 -0
+	bool added = false;
 
+	/* Inititalize a serial core controller device if needed */
+	ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id);
+	if (!ctrl_dev) {
+		ctrl_dev = serial_core_ctrl_device_add(port);

drivers/tty/serial/serial_core.c bool uart_match_port(const struct uart_port *port1, · line 3384

Quoted 10 lines
drivers/tty/serial/serial_core.c +8 -0
+		if (!ctrl_dev) {
+			ret = -ENODEV;
+			goto err_unlock;
+		}
+		added = true;
+	}
 
 
+	if (added)
+		serial_base_device_remove(to_serial_base_device(ctrl_dev));

Wondering if it makes sense to add a boolean directly into uart_port and drop this conditional here and move it to the callee.

...

drivers/tty/serial/serial_ctrl.c line 67

Quoted 3 lines
drivers/tty/serial/serial_ctrl.c new file +3 -0
+
+module_init(serial_ctrl_init);
+module_exit(serial_ctrl_exit);

Can we also move these closer to the respective functions?

...

drivers/tty/serial/serial_port.c line 96

Quoted 3 lines
drivers/tty/serial/serial_port.c new file +3 -0
+
+module_init(serial_port_init);
+module_exit(serial_port_exit);

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 4 lines · Andy Shevchenko

drivers/tty/serial/serial_core.c static void __uart_start(struct tty_struct *tty) · line 150

Quoted 1 line · Tony Lindgren
drivers/tty/serial/serial_core.c +1 -0
+	err = pm_runtime_get(port_dev);

Is not sync API a deliberate choice? Do we need to comment on why is so?

I don't think it can be sync version.. See where all __uart_start() may get called from. The pm_runtime_get() here is needed so the check for pm_runtime_active() won't be racy. Maybe we can now leave out the "start TX anyways" part though.

Thanks for your other comments too, will fix up those for the next revision.

Regards,

Tony

#4 lore raw
In reply to Tony Lindgren
I love your patch! Perhaps something to improve:

Hi Tony,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tty/tty-testing]
[also build test WARNING on tty/tty-next tty/tty-linus rafael-pm/linux-next usb/usb-testing usb/usb-next usb/usb-linus linus/master v6.3-rc1 next-20230309] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tony-Lindgren/serial-core-Start-managing-serial-controllers-to-enable-runtime-PM/20230309-170149
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
patch link:    https://lore.kernel.org/r/20230309085713.57700-1-tony%40atomide.com
patch subject: [PATCH v6 1/1] serial: core: Start managing serial controllers to enable runtime PM
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20230310/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/08f76f28e32bcd3c093e4af349b0e1b60328395e
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Tony-Lindgren/serial-core-Start-managing-serial-controllers-to-enable-runtime-PM/20230309-170149 git checkout 08f76f28e32bcd3c093e4af349b0e1b60328395e
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=x86_64 olddefconfig
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash drivers/tty/serial/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/linux/device.h:25,
from include/linux/pm_runtime.h:11, from drivers/tty/serial/serial_ctrl.c:12:

>> drivers/tty/serial/serial_ctrl.c:17:34: warning: 'serial_ctrl_pm' defined but not used [-Wunused-const-variable=]
      17 | static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
         |                                  ^~~~~~~~~~~~~~
   include/linux/pm.h:372:25: note: in definition of macro '_DEFINE_DEV_PM_OPS'
     372 | const struct dev_pm_ops name = { \
         |                         ^~~~
   drivers/tty/serial/serial_ctrl.c:17:8: note: in expansion of macro 'DEFINE_RUNTIME_DEV_PM_OPS'
      17 | static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
         |        ^~~~~~~~~~~~~~~~~~~~~~~~~

vim +/serial_ctrl_pm +17 drivers/tty/serial/serial_ctrl.c

     2	
     3	/*
     4	 * Serial core controller driver
     5	 *
     6	 * This driver manages the serial core controller struct device instances.
     7	 * The serial core controller devices are children of the physical serial
     8	 * port device.
     9	 */
    10	
    11	#include <linux/module.h>
  > 12	#include <linux/pm_runtime.h>
    13	#include <linux/serial_core.h>
    14	
    15	#include "serial_base.h"
    16	
  > 17	static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
    18	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#8 lore raw
In reply to Tony Lindgren
I love your patch! Perhaps something to improve:

Hi Tony,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tty/tty-testing]
[also build test WARNING on tty/tty-next tty/tty-linus rafael-pm/linux-next usb/usb-testing usb/usb-next usb/usb-linus linus/master v6.3-rc1 next-20230309] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tony-Lindgren/serial-core-Start-managing-serial-controllers-to-enable-runtime-PM/20230309-170149
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
patch link:    https://lore.kernel.org/r/20230309085713.57700-1-tony%40atomide.com
patch subject: [PATCH v6 1/1] serial: core: Start managing serial controllers to enable runtime PM
config: i386-allnoconfig (https://download.01.org/0day-ci/archive/20230310/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/08f76f28e32bcd3c093e4af349b0e1b60328395e
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Tony-Lindgren/serial-core-Start-managing-serial-controllers-to-enable-runtime-PM/20230309-170149 git checkout 08f76f28e32bcd3c093e4af349b0e1b60328395e
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/tty/serial/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/tty/serial/serial_ctrl.c:17:34: warning: unused variable 'serial_ctrl_pm' [-Wunused-const-variable]
   static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
                                    ^

1 warning generated.

vim +/serial_ctrl_pm +17 drivers/tty/serial/serial_ctrl.c

    16	
  > 17	static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
    18	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#3 lore raw
In reply to Tony Lindgren
Quoted 10 lines · Tony Lindgren
Quoted 4 lines · Andy Shevchenko

drivers/tty/serial/serial_core.c static void __uart_start(struct tty_struct *tty) · line 150

Quoted 1 line · Tony Lindgren
drivers/tty/serial/serial_core.c +1 -0
+	err = pm_runtime_get(port_dev);

Is not sync API a deliberate choice? Do we need to comment on why is so?

I don't think it can be sync version.. See where all __uart_start() may get called from. The pm_runtime_get() here is needed so the check for pm_runtime_active() won't be racy. Maybe we can now leave out the "start TX anyways" part though.

To clarify, the reason we no longer need the check for "start TX anyways" is we now do pm_runtime_get() on the new port_dev. Earlier we tried to do it on the physical serial port driver dev where runtime PM possibly was not enabled.

Regards,

Tony

#5 lore raw
In reply to kernel test robot
Quoted 3 lines · kernel test robot
Quoted 1 line
Quoted 1 line

drivers/tty/serial/serial_ctrl.c:17:34: warning: 'serial_ctrl_pm' defined but not used [-Wunused-const-variable=]

      17 | static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
         |                                  ^~~~~~~~~~~~~~

Thanks I'll tag it with __maybe_unused.

Regards,

Tony

#6 lore raw
Quoted 6 lines · Tony Lindgren
Quoted 3 lines · kernel test robot
Quoted 1 line
Quoted 1 line

drivers/tty/serial/serial_ctrl.c:17:34: warning: 'serial_ctrl_pm' defined but not used [-Wunused-const-variable=]

      17 | static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
         |                                  ^~~~~~~~~~~~~~

Thanks I'll tag it with __maybe_unused.

It requires to use pm_ptr() macro, it was designed exactly to *not* spread __maybe_unused.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Hi,

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Tony Lindgren
Quoted 3 lines · kernel test robot
Quoted 1 line
Quoted 1 line

drivers/tty/serial/serial_ctrl.c:17:34: warning: 'serial_ctrl_pm' defined but not used [-Wunused-const-variable=]

      17 | static DEFINE_RUNTIME_DEV_PM_OPS(serial_ctrl_pm, NULL, NULL, NULL);
         |                                  ^~~~~~~~~~~~~~

Thanks I'll tag it with __maybe_unused.

It requires to use pm_ptr() macro, it was designed exactly to *not* spread __maybe_unused.

Heh yeah.. Turns out the issue here is that the serial_ctrl_pm ops is not used at all for serial_ctrl.c and can be just dropped.

Regards,

Tony