Skip to contentSkip to conversation

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

PATCH Superseded Tony Lindgren 3 participants 5 messages May 8, 2023 – May 9, 2023 TTY layer and serial drivers

v10 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v10 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.

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

---

Changes since v9:

- Built in serial_base and core related components into serial_base.ko as suggested by Greg. We now have module_init only in serial_base.c that calls serial_base_ctrl_init() and serial_base_port_init(). I renamed serial_bus.c to serial_base_bus.c to build serial_base.ko. Note that if we wanted to build these into serial_core.ko, renaming serial_core.c would be needed which is not necessarily nice for folks that may have pending patches

- Dropped string comparison for ctrl and port, and switched to using struct device_type as suggested by Greg

- Dropped port->state checks in serial_base_get_port() as noted by Greg

- Dropped EXPORT_SYMBOL_NS(), these are no longer needed with components built into serial_base.ko. I also noticed that we have some dependency loops if components are not built into serial_base.ko. And we would have hard time later on moving port specific functions to serial_port.c for example

- Dropped checks for negative ctrl_id in serial_core_ctrl_find() as suggested by Greg

- Stopped resetting ctrl_id in serial_core_remove_one_port(), instead let's properly init it in serial8250_init_port(). The ctrl_id is optionally passed to uart_add_one_port() and zero otherwise

- Moved port_mutex and UPF_DEAD handling from serial_core_add_one_port() to serial_core_register_port() to simplify things a bit

- Updated license and copyright as suggested by Greg

- Dropped Andy's reviewed-by, things still changed quite a bit

Changes since v8:

- Drop unnecessary free for name noticed by Andy, the name is freed on put_device()

- Cosmetic fix for comments in serial_port.c noted by Andy

- Spelling fix for word uninitialized in serial_base_get_port()

Changes since v7:

- Add release() put_device() to serial_base.c as noted by Andy

- Make struct serial_base_device private to serial_base.c by adding serial_base_get_port()

- Add more comments to __uart_start()

- Coding style improvments for serial_base.c from Andy

Changes since v6:

- Fix up a memory leak and a bunch of issues in serial_base.c as noted by Andy

- Replace bool with new_ctrl_dev for freeing the added device on error path

- Drop unused pm ops for serial_ctrl.c as noted by kernel test robot

- Drop documentation updates for acpi devices for now to avoid a merge conflict and make testing easier between -rc2 and Linux next

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

---
 drivers/tty/serial/8250/8250_core.c  |   1 +
 drivers/tty/serial/8250/8250_port.c  |   1 +
 drivers/tty/serial/Makefile          |   3 +-
 drivers/tty/serial/serial_base.h     |  35 +++++
 drivers/tty/serial/serial_base_bus.c | 179 +++++++++++++++++++++++++
 drivers/tty/serial/serial_core.c     | 188 +++++++++++++++++++++++----
 drivers/tty/serial/serial_ctrl.c     |  68 ++++++++++
 drivers/tty/serial/serial_port.c     | 105 +++++++++++++++
 include/linux/serial_core.h          |   4 +-
 9 files changed, 557 insertions(+), 27 deletions(-)
 create mode 100644 drivers/tty/serial/serial_base.h
 create mode 100644 drivers/tty/serial/serial_base_bus.c
 create mode 100644 drivers/tty/serial/serial_ctrl.c
 create mode 100644 drivers/tty/serial/serial_port.c
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/8250/8250_port.c +1 -0
@@ -3292,6 +3292,7 @@ void serial8250_init_port(struct uart_8250_port *up)
 	struct uart_port *port = &up->port;
 
 	spin_lock_init(&port->lock);
+	port->ctrl_id = 0;
 	port->ops = &serial8250_pops;
 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
 
drivers/tty/serial/Makefile +2 -1
@@ -3,7 +3,8 @@
 # Makefile for the kernel serial device drivers.
 #
 
-obj-$(CONFIG_SERIAL_CORE) += serial_core.o
+obj-$(CONFIG_SERIAL_CORE) += serial_base.o
+serial_base-objs := serial_core.o serial_base_bus.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.h new file +35 -0
new file mode 100644
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Serial core related functions, serial port device drivers do not need this.
+ *
+ * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
+ * Author: Tony Lindgren <[email protected]>
+ */
+
+struct uart_driver;
+struct uart_port;
+struct device_driver;
+struct device;
+
+int serial_base_ctrl_init(void);
+void serial_base_ctrl_exit(void);
+
+int serial_base_port_init(void);
+void serial_base_port_exit(void);
+
+int serial_base_driver_register(struct device_driver *driver);
+void serial_base_driver_unregister(struct device_driver *driver);
+
+struct device *serial_base_ctrl_add(struct uart_port *port,
+				    struct device *parent_dev);
+struct device *serial_base_port_add(struct uart_port *port,
+				    struct device *parent_dev);
+void serial_base_device_remove(struct device *dev);
+
+struct uart_port *serial_base_get_port(struct device *dev);
+
+int serial_ctrl_register_port(struct uart_driver *drv, struct uart_port *port);
+void serial_ctrl_unregister_port(struct uart_driver *drv, struct uart_port *port);
+
+int serial_core_register_port(struct uart_driver *drv, struct uart_port *port);
+void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port);
drivers/tty/serial/serial_base_bus.c new file +179 -0

179 lines not on this page · raw message

drivers/tty/serial/serial_core.c +163 -25

281 lines not on this page · raw message

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

68 lines not on this page · raw message

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

105 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.40.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 5 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.

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

Sorry, I'm on vacation. But just brief comments below. In general looks good to me.

...

drivers/tty/serial/Makefile line 7

Quoted 3 lines
drivers/tty/serial/Makefile +2 -1
-obj-$(CONFIG_SERIAL_CORE) += serial_core.o
+obj-$(CONFIG_SERIAL_CORE) += serial_base.o
+serial_base-objs := serial_core.o serial_base_bus.o serial_ctrl.o serial_port.o

'objs' is for user space tools, kernel should use 'y'

...

drivers/tty/serial/serial_port.c line 48

Quoted 2 lines
drivers/tty/serial/serial_port.c new file +2 -0
+static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm, NULL,
+				 serial_port_runtime_resume, NULL);

Maybe

static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm,

				 NULL, serial_port_runtime_resume, NULL);

for better logical split?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 11 lines · Andy Shevchenko
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 8 lines
Quoted 5 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.

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

Sorry, I'm on vacation. But just brief comments below. In general looks good to me.

OK have a nice vacation :)

Quoted 5 lines

drivers/tty/serial/Makefile line 7

Quoted 3 lines
drivers/tty/serial/Makefile +2 -1
-obj-$(CONFIG_SERIAL_CORE) += serial_core.o
+obj-$(CONFIG_SERIAL_CORE) += serial_base.o
+serial_base-objs := serial_core.o serial_base_bus.o serial_ctrl.o serial_port.o

'objs' is for user space tools, kernel should use 'y'

Oh OK.

Quoted 9 lines

drivers/tty/serial/serial_port.c line 48

Quoted 2 lines
drivers/tty/serial/serial_port.c new file +2 -0
+static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm, NULL,
+				 serial_port_runtime_resume, NULL);

Maybe

static DEFINE_RUNTIME_DEV_PM_OPS(serial_port_pm,

				 NULL, serial_port_runtime_resume, NULL);

for better logical split?

Sure.

Thanks,

Tony

#3 lore raw
In reply to Tony Lindgren
kernel test robot noticed the following build warnings:

Hi Tony,

kernel test robot noticed the following build warnings:

[auto build test WARNING on tty/tty-testing]
[also build test WARNING on tty/tty-next tty/tty-linus usb/usb-testing usb/usb-next usb/usb-linus linus/master v6.4-rc1 next-20230508] [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/20230508-190805
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-testing
patch link:    https://lore.kernel.org/r/20230508110339.38699-1-tony%40atomide.com
patch subject: [PATCH v10 1/1] serial: core: Start managing serial controllers to enable runtime PM
config: x86_64-randconfig-a001-20230508 (https://download.01.org/0day-ci/archive/20230509/[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/2f298f9dbe3a1d9550e1f15bb1415aeaf9ce4311
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/20230508-190805 git checkout 2f298f9dbe3a1d9550e1f15bb1415aeaf9ce4311
# 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=x86_64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross 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 >>):

>> drivers/tty/serial/serial_base_bus.c:97:13: warning: variable 'id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
           } else if (type == &serial_port_type) {
                      ^~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/tty/serial/serial_base_bus.c:102:77: note: uninitialized use occurs here
           err = dev_set_name(&sbd->dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
                                                                                      ^~
   drivers/tty/serial/serial_base_bus.c:97:9: note: remove the 'if' if its condition is always true
           } else if (type == &serial_port_type) {
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/tty/serial/serial_base_bus.c:83:13: note: initialize the variable 'id' to silence this warning
           int err, id;
                      ^
                       = 0

1 warning generated.

vim +97 drivers/tty/serial/serial_base_bus.c

    77	
    78	static struct device *serial_base_device_add(struct uart_port *port,
    79						     struct device *parent_dev,
    80						     const struct device_type *type)
    81	{
    82		struct serial_base_device *sbd;
    83		int err, id;
    84	
    85		sbd = kzalloc(sizeof(*sbd), GFP_KERNEL);
    86		if (!sbd)
    87			return NULL;
    88	
    89		device_initialize(&sbd->dev);
    90		sbd->dev.type = type;
    91		sbd->dev.parent = parent_dev;
    92		sbd->dev.bus = &serial_base_bus_type;
    93		sbd->dev.release = &serial_base_release;
    94	
    95		if (type == &serial_ctrl_type) {
    96			id = port->ctrl_id;
  > 97		} else if (type == &serial_port_type) {
    98			id = port->line;
    99			sbd->port = port;

100 }
101
102 err = dev_set_name(&sbd->dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
103 if (err)
104 goto err_free_dev;
105
106 err = device_add(&sbd->dev);
107 if (err)
108 goto err_put_device;
109
110 return &sbd->dev;
111
112 err_put_device:
113 put_device(&sbd->dev);
114
115 err_free_dev:
116 kfree(sbd);
117
118 return NULL;
119 }
120

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#4 lore raw
Quoted 13 lines · kernel test robot
Quoted 1 line
Quoted 1 line

drivers/tty/serial/serial_base_bus.c:97:13: warning: variable 'id' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

           } else if (type == &serial_port_type) {
                      ^~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/tty/serial/serial_base_bus.c:102:77: note: uninitialized use occurs here
           err = dev_set_name(&sbd->dev, "%s.%s.%d", type->name, dev_name(port->dev), id);
                                                                                      ^~
   drivers/tty/serial/serial_base_bus.c:97:9: note: remove the 'if' if its condition is always true
           } else if (type == &serial_port_type) {
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/tty/serial/serial_base_bus.c:83:13: note: initialize the variable 'id' to silence this warning
           int err, id;
                      ^
                       = 0
Quoted 1 line

1 warning generated.

Thanks I'll just add else goto err_free_dev for unknown types.

Regards,

Tony