Skip to contentSkip to conversation

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

PATCH Superseded Tony Lindgren 3 participants 6 messages Jan 16, 2023 – Apr 20, 2023 TTY layer and serial drivers

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 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 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 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/Makefile         |   2 +-
 drivers/tty/serial/serial_core.c    | 189 ++++++++++++++++++++++++++--
 drivers/tty/serial/serial_ctrl.c    |  61 +++++++++
 drivers/tty/serial/serial_ctrl.h    |  22 ++++
 drivers/tty/serial/serial_port.c    | 109 ++++++++++++++++
 include/linux/serial_core.h         |   4 +-
 7 files changed, 379 insertions(+), 9 deletions(-)
 create mode 100644 drivers/tty/serial/serial_ctrl.c
 create mode 100644 drivers/tty/serial/serial_ctrl.h
 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/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_ctrl.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 +182 -7

253 lines not on this page · raw message

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

61 lines not on this page · raw message

drivers/tty/serial/serial_ctrl.h new file +22 -0
new file mode 100644
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/* Serial core controller data. Serial port device drivers do not need this. */
+
+struct uart_driver;
+struct uart_port;
+
+/**
+ * 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().
+ */
+struct serial_port_platdata {
+	struct uart_port *port;
+};
+
+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_port.c new file +109 -0

109 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.0
#1 lore raw
Quoted 4 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.

Quoted 8 lines

To do this, let's set up a 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.

Looking better, but why is this new device a platform device? That feels odd, you should never have a platform device hanging off of a non-platform device, right?

What does the sysfs tree look like now with this patch applied?

thanks,

greg k-h

#2 lore raw
Quoted 11 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 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 6 lines
Quoted 2 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.

Looking better, but why is this new device a platform device? That feels odd, you should never have a platform device hanging off of a non-platform device, right?

No special need for it to be a platform device. It just is easy to set up, and for my test case the serial port physical device is also a platform device.

What's your preference here?

Quoted 1 line

What does the sysfs tree look like now with this patch applied?

Below are two examples of what's in sysfs.

Regards,

Tony

8< -----------------
On arm64 with two physical 8250 devices I have:

# ls -l /sys/devices/platform/serial8250/
total 0
lrwxrwxrwx    1 root     root             0 Jan  1  1970 driver -> ../../../bus/platform/drivers/serial8250
-rw-r--r--    1 root     root          4096 Jan  1  1970 driver_override
-r--r--r--    1 root     root          4096 Jan  1  1970 modalias
drwxr-xr-x    2 root     root             0 Jan  1  1970 power
drwxr-xr-x    5 root     root             0 Jan  1  1970 serial-ctrl.0.auto
lrwxrwxrwx    1 root     root             0 Jan  1  1970 subsystem -> ../../../bus/platform
drwxr-xr-x    4 root     root             0 Jan  1  1970 tty
-rw-r--r--    1 root     root          4096 Jan  1  1970 uevent
# ls -l /sys/devices/platform/serial8250/serial-ctrl*
total 0
lrwxrwxrwx    1 root     root             0 Jan  1  1970 driver -> ../../../../bus/platform/drivers/serial-ctrl
-rw-r--r--    1 root     root          4096 Jan  1  1970 driver_override
-r--r--r--    1 root     root          4096 Jan  1  1970 modalias
drwxr-xr-x    2 root     root             0 Jan  1  1970 power
drwxr-xr-x    3 root     root             0 Jan  1  1970 serial-port.1.auto
drwxr-xr-x    3 root     root             0 Jan  1  1970 serial-port.4.auto
lrwxrwxrwx    1 root     root             0 Jan  1  1970 subsystem -> ../../../../bus/platform
-rw-r--r--    1 root     root          4096 Jan  1  1970 uevent

On x86_64 qemu with one port I have:

# ls -l /sys/devices/pnp0/00:04/
total 0
lrwxrwxrwx    1 root     root             0 Feb  1 06:13 driver -> ../../../bus/pnp/drivers/serial
lrwxrwxrwx    1 root     root             0 Feb  1 06:13 firmware_node -> ../../LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/device:01/PNP0501:00
-r--r--r--    1 root     root          4096 Feb  1 06:13 id
-r--r--r--    1 root     root          4096 Feb  1 06:13 options
-rw-r--r--    1 root     root          4096 Feb  1 06:13 resources
drwxr-xr-x    3 root     root             0 Feb  1 06:12 serial-ctrl.0.auto
lrwxrwxrwx    1 root     root             0 Feb  1 06:13 subsystem -> ../../../bus/pnp
drwxr-xr-x    3 root     root             0 Feb  1 06:12 tty
-rw-r--r--    1 root     root          4096 Feb  1 06:12 uevent
# ls -l /sys/devices/pnp0/00:04/serial-ctrl.0.auto/
total 0
lrwxrwxrwx    1 root     root             0 Feb  1 06:13 driver -> ../../../../bus/platform/drivers/serial-ctrl
-rw-r--r--    1 root     root          4096 Feb  1 06:13 driver_override
-r--r--r--    1 root     root          4096 Feb  1 06:13 modalias
drwxr-xr-x    2 root     root             0 Feb  1 06:12 serial-port.1.auto
lrwxrwxrwx    1 root     root             0 Feb  1 06:13 subsystem -> ../../../../bus/platform
-rw-r--r--    1 root     root          4096 Feb  1 06:12 uevent
#3 lore raw
Quoted 19 lines · Tony Lindgren
Quoted 11 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 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 6 lines
Quoted 2 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.

Looking better, but why is this new device a platform device? That feels odd, you should never have a platform device hanging off of a non-platform device, right?

Quoted 5 lines

No special need for it to be a platform device. It just is easy to set up, and for my test case the serial port physical device is also a platform device.

What's your preference here?

Never make up a "fake" platform device please. Only use them for real platform devices. Use a virtual device if you want a virtual one.

thanks,

greg k-h

#4 lore raw
In reply to Tony Lindgren
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 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.

A side note. Perhaps it makes sense to also clean up documentation somehow related to this change. For example, I found that Documentation/firmware-guide/acpi/enumeration.rst has this:

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

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

Quoted 6 lines

A side note. Perhaps it makes sense to also clean up documentation somehow related to this change. For example, I found that Documentation/firmware-guide/acpi/enumeration.rst has this:

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

OK good point, will update that for the next version.

FYI, I replaced the serial core platform bus with just struct device and bus, need to clean-up a bit before posting though.

Regards,

Tony