Skip to contentSkip to conversation

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

PATCH Superseded Tony Lindgren 2 participants 3 messages May 11, 2023 – May 14, 2023 TTY layer and serial drivers

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v11 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 v10:

- Added missing handling for unknown type in serial_base_device_add() as noted by kernel test robot

- Use y instead of objs fro serial_base in Makefile as noted by Andy

- Improve serial_port pm_ops alignment as noted by Andy

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 | 181 ++++++++++++++++++++++++++
 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, 559 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-y := 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 +181 -0

181 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.

Much better, thanks!

One thing jumps out at me though, you are passing around "raw" struct device pointers as the serial port structure, why?

Shouldn't:

include/linux/serial_core.h struct uart_port { · line 568

Quoted 7 lines
include/linux/serial_core.h +2 -1
@@ -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 */

port_dev here be something like "struct serial_port" (or some better name)? That way you enforce the type being passed around to the serial code in this change which will help catch any type mistakes.

Yes, this structure can just be a "wrapper" around 'struct device' but at least it's a unique type.

Or am I missing why this was done this way?

thanks,

greg k-h

#2 lore raw
Quoted 36 lines · Greg Kroah-Hartman
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.

Much better, thanks!

One thing jumps out at me though, you are passing around "raw" struct device pointers as the serial port structure, why?

Shouldn't:

include/linux/serial_core.h struct uart_port { · line 568

Quoted 7 lines
include/linux/serial_core.h +2 -1
@@ -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 */

port_dev here be something like "struct serial_port" (or some better name)? That way you enforce the type being passed around to the serial code in this change which will help catch any type mistakes.

Quoted 2 lines

Yes, this structure can just be a "wrapper" around 'struct device' but at least it's a unique type.

Good idea thanks, will change.

Quoted 1 line

Or am I missing why this was done this way?

No reason to keep it as struct device.

Regards,

Tony