serial: core: Start managing serial controllers to enable runtime PM
PATCH v8 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 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/Makefile | 2 +- drivers/tty/serial/serial_base.c | 144 +++++++++++++++++++++++++ drivers/tty/serial/serial_base.h | 21 ++++ drivers/tty/serial/serial_core.c | 160 ++++++++++++++++++++++++++-- drivers/tty/serial/serial_ctrl.c | 71 ++++++++++++ drivers/tty/serial/serial_port.c | 104 ++++++++++++++++++ include/linux/serial_core.h | 4 +- 8 files changed, 498 insertions(+), 9 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
drivers/tty/serial/8250/8250_core.c +1 -0
@@ -996,6 +996,7 @@ int serial8250_register_8250_port(constif (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 +144 -0
144 lines not on this page · raw message
drivers/tty/serial/serial_base.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* Serial core related functions, serial port device drivers do not need this. */ + +struct uart_driver ; +struct uart_port ; +struct device_driver ; +struct device ; + +int serial_base_driver_register (struct device_driver *driver); +void serial_base_driver_unregister (struct device_driver *driver); +struct device *serial_base_device_add (struct uart_port *port,const char *name, +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_core.c +153 -7
224 lines not on this page · raw message
drivers/tty/serial/serial_ctrl.c new file +71 -0
71 lines not on this page · raw message
drivers/tty/serial/serial_port.c new file +104 -0
104 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 */
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.
With below addressed, FWIW,
...
Quoted 16 lines
drivers/tty/serial/serial_base.c new file +16
-0+device_initialize (&sbd->dev); + sbd->dev.parent = parent_dev; + sbd->dev.bus = &serial_base_bus_type; + sbd->dev.release = &serial_base_release; + +if (str_has_prefix (name,"ctrl" )) { + id = port->ctrl_id; + }else { + id = port->line; + sbd->port = port; + } + + err =dev_set_name (&sbd->dev,"%s.%s.%d" , name,dev_name (port->dev), id); +if (err) + goto err_free_dev; +
drivers/tty/serial/serial_base.c line 112
Quoted 10 lines
drivers/tty/serial/serial_base.c new file +9
-0+ err =device_add (&sbd->dev); +if (err) + goto err_put_device; + +return &sbd->dev; + +err_put_device: +put_device (&sbd->dev); +kfree_const (sbd->dev.kobj.name);
This is double free if not const, right?
At least that's how I read kobject_cleanup() implementation.
Sorry I haven't paid attention to this earlier.
...
Quoted 3 lines
+/*
+ * Serial core port device driver
+ */
Put it on one line for now?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 26 lines · Andy Shevchenko
Quoted 24 lines · Tony Lindgren
drivers/tty/serial/serial_base.c new file +24
-0+device_initialize (&sbd->dev); + sbd->dev.parent = parent_dev; + sbd->dev.bus = &serial_base_bus_type; + sbd->dev.release = &serial_base_release; + +if (str_has_prefix (name,"ctrl" )) { + id = port->ctrl_id; + }else { + id = port->line; + sbd->port = port; + } + + err =dev_set_name (&sbd->dev,"%s.%s.%d" , name,dev_name (port->dev), id); +if (err) + goto err_free_dev; + + err =device_add (&sbd->dev); +if (err) + goto err_put_device; + +return &sbd->dev; + +err_put_device: +put_device (&sbd->dev);
Quoted 6 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ kfree_const(sbd->dev.kobj.name);This is double free if not const, right?
At least that's how I readkobject_cleanup()implementation.Sorry I haven't paid attention to this earlier.
Thanks for spotting that, will drop it. Looks like we have the name allocated by kobject_set_name_vargs(), and then kobject_cleanup() frees it.
Quoted 7 lines
...
Quoted 3 lines
+/*
+ * Serial core port device driver
+ */Put it on one line for now?
Sure, will do.
Thanks,
Tony