From patchwork Wed Nov 23 08:28:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 24796 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2668080wrr; Wed, 23 Nov 2022 00:46:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf64ai3A15LGvQCMCpAFFTgdttTtawbR3JVbaZZkCMM0KDioTnTunGh/bkMuOKn07yCGlVG9 X-Received: by 2002:a17:906:404:b0:781:f54c:1947 with SMTP id d4-20020a170906040400b00781f54c1947mr9681466eja.69.1669193192593; Wed, 23 Nov 2022 00:46:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669193192; cv=none; d=google.com; s=arc-20160816; b=ebR2RQPHRvPrnqHziUc7uWgb2S39boLU6YH5ZiFhCx2MYBTDev2ordHisInGy1JnxL sl6MomCC0/05lHfQ9k64IeiBZO8E3ChekY8V9sfz4R5FZCkAKTRRDhLKzmRhPtqxeXTk ehpGyxSw8BSH5Rz17Ehhev7Cjs85v8Cp7tspFJN9P2rZWeYOvJHhgCdrAWxT0/e7Fu8F qfAcKd/6LrQlHOees3+3U9dIfFVbn9cGbcfBu34ozpLt+Va+tKRy6etfLk8qEMgYcH7m 5GcH/Kl/h5K0qbDxo4PBNhpN55UsU5Iy9kcV3grGY0QZIfYs31/u4+0XSD0b/UnoRqX0 5mBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=/SaKuyewkUdRo/IJNccgKH+BAegsiPuSwq8WIEm1IAM=; b=0vqb61Jx5T5EvOci+gwOsNXppKEBJOmxLO0/PWLXtzDUedkkl1CAi6AzCnGVD1POTE xCennXV3HgcMKv+07YC8kuCN8AsVQqqT5bDkdBdW+szSylk8NpK5earBTehV9RSUz+kK tyoT8nXTt5XtMZA0uvrwS2B1ESVg2xI8MeB7vjwW9I0aZkq9/A33DrNP7ROslhpMAfoW s4KhOIcZFUMKjtQz0/H+MVc8Qfq1eu1Kw9Rn//OMfe/99TyvGr5IBMMfOqxySJq/By6e 1lNq607BdKyPz0spcLz1SaXlPY6CaRzfpYVHq13KGjaT9VFjOTByzKDLO2JkoSpwj1S+ Lu5g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l23-20020a50d6d7000000b0046a222caa96si1389785edj.516.2022.11.23.00.46.02; Wed, 23 Nov 2022 00:46:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236549AbiKWI3G (ORCPT + 99 others); Wed, 23 Nov 2022 03:29:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236489AbiKWI2g (ORCPT ); Wed, 23 Nov 2022 03:28:36 -0500 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 319D014082; Wed, 23 Nov 2022 00:28:29 -0800 (PST) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id C178280F1; Wed, 23 Nov 2022 08:28:27 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman Cc: Andy Shevchenko , =?utf-8?q?Ilpo_J=C3=A4rvi?= =?utf-8?q?nen?= , Jiri Slaby , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-serial@vger.kernel.org, linux-omap@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] serial: core: Start managing serial controllers to enable runtime PM Date: Wed, 23 Nov 2022 10:28:24 +0200 Message-Id: <20221123082825.32820-1-tony@atomide.com> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750275921035953261?= X-GMAIL-MSGID: =?utf-8?q?1750275921035953261?= 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. The serial layer has a few challenges to deal with: 1. The serial port mapping to a physical serial port controller device is currently not easily available after the physical serial controller struct device gets registered uart_add_one_port() time 2. The serial port device drivers have their own driver data. So we cannot currently start making use of serial core generic data easily without changing all the serial port device drivers To find the serial ports for a controller based on struct device, let's add a new data structure for a serial_controller. On registering a port, we can use the drv->state array to find the associated serial port controller and initialize the serial core controller. As some serial port device drivers enable runtime PM in their probe before registering with the serial core layer, and some do not enable runtime PM at all currently, we need check the state in the serial core layer on uart_port_startup(). We need to also consider that a serial port device may have multiple ports. Initially we enable runtime PM for all the serial port controller devices. This allows us to add runtime PM calls and properly handle any errors without a need for serial layer specific runtime PM wrapper functions. After this patch no functional changes for the serial port device drivers are intended. We just enable runtime PM and keep the runtime PM usage count until all the serial controller ports are unregistered. For drivers implementing PM runtime, we just keep track of the runtime PM configuration. For the serial port drivers, the serial core layer has the following use cases to deal with: 1. If a serial port device driver does not implement runtime PM, the device state is set to active state, and the runtime PM usage count is kept until the last port for a device is unregistered 2. If a serial port device driver implements runtime PM, the runtime PM usage count is kept until the last port for the device is unregistered 3. If a serial port device driver implements runtime PM autosuspend, autosuspend is not prevented. This currently gets set only for the 8250_omap driver to keep runtime PM working for it For system suspend, things should be mostly detached from the runtime PM. The serial port device drivers may call pm_runtime_force_suspend() and pm_runtime_force_resume() as needed. Suggested-by: Johan Hovold Signed-off-by: Tony Lindgren --- 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_omap.c | 1 + drivers/tty/serial/serial_core.c | 179 ++++++++++++++++++++++++++++ include/linux/serial_core.h | 4 + 4 files changed, 185 insertions(+) diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/8250_core.c --- a/drivers/tty/serial/8250/8250_core.c +++ b/drivers/tty/serial/8250/8250_core.c @@ -1004,6 +1004,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up) uart->port.regshift = up->port.regshift; uart->port.iotype = up->port.iotype; uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF; + uart->port.supports_autosuspend = up->port.supports_autosuspend; uart->bugs = up->bugs; uart->port.mapbase = up->port.mapbase; uart->port.mapsize = up->port.mapsize; diff --git a/drivers/tty/serial/8250/8250_omap.c b/drivers/tty/serial/8250/8250_omap.c --- a/drivers/tty/serial/8250/8250_omap.c +++ b/drivers/tty/serial/8250/8250_omap.c @@ -1330,6 +1330,7 @@ static int omap8250_probe(struct platform_device *pdev) up.rs485_start_tx = serial8250_em485_start_tx; up.rs485_stop_tx = serial8250_em485_stop_tx; up.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); + up.port.supports_autosuspend = 1; ret = of_alias_get_id(np, "serial"); if (ret < 0) { diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,20 @@ #include #include +/* + * Serial port device specific data for serial core. + * + * Each port device can have multiple ports with struct uart_state allocated + * for each port. The array of ports is kept in struct uart_driver. + */ +struct serial_controller { + struct device *dev; /* Physical controller device */ + struct uart_driver *drv; /* For port specific uart_state */ + struct kref ref; /* Registered port count */ + unsigned long implements_pm_runtime:1; + unsigned long supports_autosuspend:1; +}; + /* * This is used to lock changes in serial line configuration. */ @@ -177,6 +192,162 @@ static void uart_port_dtr_rts(struct uart_port *uport, int raise) uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS); } +static struct serial_controller *to_controller(struct uart_port *port) +{ + if (!port->dev) + return NULL; + + return port->state->controller; +} + +/* + * Starts runtime PM for the serial controller device if not already started + * by the serial port driver. Called from uart_add_one_port() with port_mutex + * held. + */ +static int serial_core_pm_runtime_start(struct uart_port *port) +{ + struct serial_controller *controller = to_controller(port); + struct device *dev = port->dev; + int ret = 0; + + if (kref_get_unless_zero(&controller->ref)) + return 0; + + /* Init controller device on first reference */ + kref_init(&controller->ref); + + /* Always enable autosuspend and consider child devices for serdev */ + pm_runtime_use_autosuspend(dev); + pm_suspend_ignore_children(dev, false); + + /* + * If the port driver did not enable runtime PM in probe, do it now. + * Devices that did not enable runtime PM get set active so we can + * properly handle the returned errors for runtime PM calls. + */ + if (!pm_runtime_enabled(dev)) { + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + } else { + controller->implements_pm_runtime = 1; + } + + /* + * Keep the port device enabled unless autosuspend is supported. + * Released on port shutdown. + */ + if (!controller->supports_autosuspend) { + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) { + pm_runtime_dont_use_autosuspend(dev); + pm_runtime_disable(dev); + } + } + + return ret; +} + +/* Clean up the runtime PM settings done on serial_core_register_port() */ +static void serial_core_pm_runtime_cleanup(struct kref *ref) +{ + struct serial_controller *controller = + container_of(ref, struct serial_controller, ref); + struct device *dev = controller->dev; + + pm_runtime_dont_use_autosuspend(dev); + pm_suspend_ignore_children(dev, true); + if (!controller->supports_autosuspend) + pm_runtime_put_sync(dev); + if (!controller->implements_pm_runtime) { + pm_runtime_set_suspended(dev); + pm_runtime_disable(dev); + } + kfree(controller); +} + +/* + * Find the registered serial port controller if one exists. Caller must + * hold port_mutex. + */ +static struct serial_controller * +serial_core_find_controller(struct uart_driver *drv, struct device *dev) +{ + struct uart_state *state; + int i; + + for (i = 0; i < drv->nr; i++) { + state = drv->state + i; + if (!state->uart_port->dev) + continue; + + if (state->uart_port->dev == dev) + return state->controller; + } + + return NULL; +} + +/* + * Initialize a serial port device and serial port controller as + * needed. Called from uart_add_one_port() with port_mutex held. + */ +static int serial_core_register_port(struct uart_port *port, + struct uart_driver *drv) +{ + struct serial_controller *controller; + bool allocated = false; + int ret; + + if (!port->dev) + return 0; + + controller = serial_core_find_controller(drv, port->dev); + if (!controller) { + controller = kzalloc(sizeof(*controller), GFP_KERNEL); + if (!controller) + return -ENOMEM; + + controller->drv = drv; + controller->dev = port->dev; + controller->supports_autosuspend = port->supports_autosuspend; + allocated = true; + } + + port->state->controller = controller; + WARN_ON(port->supports_autosuspend != controller->supports_autosuspend); + + ret = serial_core_pm_runtime_start(port); + if (ret < 0) + goto err_free; + + return 0; + +err_free: + port->state->controller = NULL; + if (allocated) + kfree(controller); + + return ret; +} + +/* + * Removes a serial port device and the serial port controller if the last + * port instance. Called from uart_add_one_port() error path with port_mutex + * held. + */ +static void serial_core_unregister_port(struct uart_port *port) +{ + struct serial_controller *controller = to_controller(port); + + /* Check for a registered controller, no struct device early on */ + if (!controller) + return; + + port->state->controller = NULL; + kref_put(&controller->ref, serial_core_pm_runtime_cleanup); +} + /* * Startup the port. This will be called once per open. All calls * will be serialised by the per-port mutex. @@ -3082,6 +3253,10 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) goto out; } + ret = serial_core_register_port(uport, drv); + if (ret) + goto out; + /* * If this port is in use as a console then the spinlock is already * initialised. @@ -3105,6 +3280,7 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport) GFP_KERNEL); if (!uport->tty_groups) { ret = -ENOMEM; + serial_core_unregister_port(uport); goto out; } uport->tty_groups[0] = &tty_dev_attr_group; @@ -3173,6 +3349,9 @@ int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport) goto out; } uport->flags |= UPF_DEAD; + + serial_core_unregister_port(uport); + mutex_unlock(&port->mutex); /* diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -28,6 +28,7 @@ struct uart_port; struct serial_struct; +struct serial_port_controller; struct device; struct gpio_desc; @@ -573,6 +574,7 @@ struct uart_port { unsigned char hub6; /* this should be in the 8250 driver */ unsigned char suspended; unsigned char console_reinit; + unsigned char supports_autosuspend; const char *name; /* port name */ struct attribute_group *attr_group; /* port specific attributes */ const struct attribute_group **tty_groups; /* all attributes (serial core use only) */ @@ -611,6 +613,8 @@ enum uart_pm_state { struct uart_state { struct tty_port port; + struct serial_controller *controller; + enum uart_pm_state pm_state; struct circ_buf xmit; From patchwork Wed Nov 23 08:28:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 24794 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2667369wrr; Wed, 23 Nov 2022 00:44:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf6RupwYjf0c8Am6vKn2/rvo0pceAUpzNC8+rRpEyESa53Pgr9kuTF9W0KPMyChqWP3Ti4yG X-Received: by 2002:a17:906:81cc:b0:7ae:4373:c8aa with SMTP id e12-20020a17090681cc00b007ae4373c8aamr23394216ejx.466.1669193064841; Wed, 23 Nov 2022 00:44:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669193064; cv=none; d=google.com; s=arc-20160816; b=sdC5roSE1euC/5YosMPn5RhKbUWweqN93vvHNx90I3yDYF/vfBFmic7MFEooTxvKaw IRuNibWXlLac2U0+iPXa+Yj8h7oKEgvzecBFIDRqjhk5dwgf22PfVNCt1NGArt30E7EZ HonummZVdn8DdbSmKNlwAj7fNcGXEXB1kPxsy030dG14JLRiFOvSawKEREd67y+DK5nt 4XrRV4g8M9pBzgojlG9rj2UMY/bJoDARyJfk3LDoX+RUYQ40FxOf5r6irxXgqbIpEz/f gLR6/PIM/zsOin09wlZy77R9IAPhz67O7Ees5qfzY6GHoyliURkb+rhoGtvnvzH7SoJt 0VGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Fl2cVpLTUY90zM7tZ50dBFd7bR16zPPlxEv1KfDwWZA=; b=G3V7nQoMg7j8pSuTuxenYpgFkT1Imc0SVzTRqIgEZfboa5ZPcMwtzSIKX/CMcZK8pw h1taVEvlveh0ZtAet1uIodK0HnxArN27YO1QbvRD5DKGEmgqjqvWG5uu6WySBsYwhi1m UX71JNHVrz7D95O9/+CpakqjRvncPwfq7PIWqobufLmeLNIhhnpksFIdNWyjGxX+cPTK Y0MeWOweJ2u9WO/DOgwJA5Ab+1n7nBg0j5zki+cBR4miHJw/zVfLKfn6T/9XSlFX2SxS nKXuS7A/U3Fxp0SIoLK2wHpC9KP0eoQdRQJyLKe8la5gFbRMb4uDQf+zrc/eHhZVvVLb 3b7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y11-20020a056402358b00b004637e16cf97si14870469edc.597.2022.11.23.00.44.00; Wed, 23 Nov 2022 00:44:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236556AbiKWI3K (ORCPT + 99 others); Wed, 23 Nov 2022 03:29:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236594AbiKWI2i (ORCPT ); Wed, 23 Nov 2022 03:28:38 -0500 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1253C5CD25; Wed, 23 Nov 2022 00:28:34 -0800 (PST) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id D696E816A; Wed, 23 Nov 2022 08:28:31 +0000 (UTC) From: Tony Lindgren To: Greg Kroah-Hartman Cc: Andy Shevchenko , =?utf-8?q?Ilpo_J=C3=A4rvi?= =?utf-8?q?nen?= , Jiri Slaby , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-serial@vger.kernel.org, linux-omap@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] serial: core: Add port port device to flush TX on runtime resume Date: Wed, 23 Nov 2022 10:28:25 +0200 Message-Id: <20221123082825.32820-2-tony@atomide.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221123082825.32820-1-tony@atomide.com> References: <20221123082825.32820-1-tony@atomide.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750275786874718208?= X-GMAIL-MSGID: =?utf-8?q?1750275786874718208?= With PM runtime enabled for the serial port controllers, we can now flush pending TX for the port on runtime PM resume as suggested by Johan Hovold . To flush the pending TX, let's set up each port as a proper device as suggested by Greg Kroah-Hartman . We set up each port as a child device for the serial port controller device. We use platform device for this and pass the port information in platform_data. Let's just do mimimal changes needed for now, more port specific code can be then moved from serial_core.c to serial_port.c as needed. Suggested-by: Johan Hovold Suggested-by: Greg Kroah-Hartman Signed-off-by: Tony Lindgren --- drivers/tty/serial/Makefile | 2 +- drivers/tty/serial/serial_core.c | 77 ++++++++++++++++++++- drivers/tty/serial/serial_port.c | 111 +++++++++++++++++++++++++++++++ drivers/tty/serial/serial_port.h | 16 +++++ include/linux/serial_core.h | 1 + 5 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 drivers/tty/serial/serial_port.c create mode 100644 drivers/tty/serial/serial_port.h diff --git a/drivers/tty/serial/Makefile b/drivers/tty/serial/Makefile --- a/drivers/tty/serial/Makefile +++ b/drivers/tty/serial/Makefile @@ -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_port.o obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o obj-$(CONFIG_SERIAL_EARLYCON_ARM_SEMIHOST) += earlycon-arm-semihost.o diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,8 @@ #include #include +#include "serial_port.h" + /* * Serial port device specific data for serial core. * @@ -151,9 +154,31 @@ static void __uart_start(struct tty_struct *tty) { struct uart_state *state = tty->driver_data; struct uart_port *port = state->uart_port; + struct device *port_dev; + int err; + + if (!port || uart_tx_stopped(port)) + return; - if (port && !uart_tx_stopped(port)) + port_dev = port->state->port_dev; + + err = pm_runtime_get(port_dev); + if (err < 0) { + /* Something went wrong, attempt to start TX anyways */ + port->ops->start_tx(port); + pm_runtime_put_noidle(port_dev); + return; + } + + /* + * Start TX if enabled, and kick runtime PM. Otherwise we must + * wait for a retry. See also serial_port.c for runtime PM + * autosuspend timeout. + */ + if (pm_runtime_active(port_dev)) port->ops->start_tx(port); + pm_runtime_mark_last_busy(port_dev); + pm_runtime_put_autosuspend(port_dev); } static void uart_start(struct tty_struct *tty) @@ -288,6 +313,37 @@ serial_core_find_controller(struct uart_driver *drv, struct device *dev) return NULL; } +static int serial_core_add_port_device(struct uart_port *port) +{ + struct serial_port_platdata pd; + struct platform_device *pdev; + int ret; + + pdev = platform_device_alloc("serial-port", PLATFORM_DEVID_AUTO); + if (!pdev) + return -ENOMEM; + + pdev->dev.parent = port->dev; + pd.state = port->state; + + ret = platform_device_add_data(pdev, &pd, sizeof(pd)); + if (ret) + goto err_put; + + ret = platform_device_add(pdev); + if (ret) + goto err_put; + + port->state->port_dev = &pdev->dev; + + return 0; + +err_put: + platform_device_put(pdev); + + return ret; +} + /* * Initialize a serial port device and serial port controller as * needed. Called from uart_add_one_port() with port_mutex held. @@ -317,13 +373,21 @@ static int serial_core_register_port(struct uart_port *port, port->state->controller = controller; WARN_ON(port->supports_autosuspend != controller->supports_autosuspend); + ret = serial_core_add_port_device(port); + if (ret) + goto err_free; + ret = serial_core_pm_runtime_start(port); if (ret < 0) - goto err_free; + goto err_del; return 0; +err_del: + platform_device_del(to_platform_device(port->state->port_dev)); + err_free: + port->state->port_dev = NULL; port->state->controller = NULL; if (allocated) kfree(controller); @@ -339,11 +403,14 @@ static int serial_core_register_port(struct uart_port *port, static void serial_core_unregister_port(struct uart_port *port) { struct serial_controller *controller = to_controller(port); + struct device *dev = port->state->port_dev; /* Check for a registered controller, no struct device early on */ if (!controller) return; + platform_device_del(to_platform_device(dev)); + port->state->port_dev = NULL; port->state->controller = NULL; kref_put(&controller->ref, serial_core_pm_runtime_cleanup); } @@ -363,6 +430,10 @@ static int uart_port_startup(struct tty_struct *tty, struct uart_state *state, if (uport->type == PORT_UNKNOWN) return 1; + retval = serial_port_get(state); + if (retval) + return retval; + /* * Make sure the device is in D0 state. */ @@ -2030,6 +2101,8 @@ static void uart_port_shutdown(struct tty_port *port) /* Ensure that the IRQ handler isn't running on another CPU. */ synchronize_irq(uport->irq); } + + serial_port_put(state); } static int uart_carrier_raised(struct tty_port *port) diff --git a/drivers/tty/serial/serial_port.c b/drivers/tty/serial/serial_port.c new file mode 100644 --- /dev/null +++ b/drivers/tty/serial/serial_port.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +/* + * Serial port driver to provide port specific services for serial_core + */ + +#include +#include +#include +#include + +#include "serial_port.h" + +#define SERIAL_PORT_AUTOSUSPEND_DELAY_MS 500 + +struct serial_port_data { + struct uart_state *state; +}; + +int serial_port_get(struct uart_state *state) +{ + if (!state) + return -ENODEV; + + /* Prevent uart_port from unloading */ + if (!state->port_dev || !state->port_dev->driver || + !try_module_get(state->port_dev->driver->owner)) + return -ENODEV; + + return 0; +} +EXPORT_SYMBOL(serial_port_get); + +void serial_port_put(struct uart_state *state) +{ + if (!state || !state->port_dev || !state->port_dev->driver) + return; + + module_put(state->port_dev->driver->owner); +} +EXPORT_SYMBOL(serial_port_put); + +/* Only considers TX for now. Caller must take care of locking */ +static int __serial_port_busy(struct uart_port *port) +{ + return (!uart_tx_stopped(port) && + uart_circ_chars_pending(&port->state->xmit)); +} + +static int serial_port_runtime_resume(struct device *dev) +{ + struct serial_port_data *ddata = dev_get_drvdata(dev); + struct uart_port *port = ddata->state->uart_port; + unsigned long flags; + + /* Flush any pending TX for the port */ + spin_lock_irqsave(&port->lock, flags); + if (__serial_port_busy(port)) + port->ops->start_tx(port); + spin_unlock_irqrestore(&port->lock, flags); + pm_runtime_mark_last_busy(dev); + + return 0; +} + +static const struct dev_pm_ops serial_port_pm = { + SET_RUNTIME_PM_OPS(NULL, serial_port_runtime_resume, NULL) +}; + +static int serial_port_probe(struct platform_device *pdev) +{ + struct serial_port_platdata *pd = dev_get_platdata(&pdev->dev); + struct serial_port_data *ddata; + + ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); + if (!ddata) + return -ENOMEM; + + ddata->state = pd->state; + platform_set_drvdata(pdev, ddata); + + pm_runtime_enable(&pdev->dev); + pm_runtime_set_autosuspend_delay(&pdev->dev, + SERIAL_PORT_AUTOSUSPEND_DELAY_MS); + pm_runtime_use_autosuspend(&pdev->dev); + + return 0; +} + +static int serial_port_remove(struct platform_device *pdev) +{ + pm_runtime_dont_use_autosuspend(&pdev->dev); + pm_runtime_disable(&pdev->dev); + + return 0; +} + +static struct platform_driver serial_port_driver = { + .driver = { + .name = "serial-port", + .pm = &serial_port_pm, + }, + .probe = serial_port_probe, + .remove = serial_port_remove, +}; + +module_platform_driver(serial_port_driver); + +MODULE_AUTHOR("Tony Lindgren "); +MODULE_DESCRIPTION("Serial controller port driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/tty/serial/serial_port.h b/drivers/tty/serial/serial_port.h new file mode 100644 --- /dev/null +++ b/drivers/tty/serial/serial_port.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/** + * 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(). Note that the life cycle for uart_state + * is different from serial_port_device. + */ +struct serial_port_platdata { + struct uart_state *state; +}; + +extern int serial_port_get(struct uart_state *state); +extern void serial_port_put(struct uart_state *state); diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -614,6 +614,7 @@ struct uart_state { struct tty_port port; struct serial_controller *controller; + struct device *port_dev; enum uart_pm_state pm_state; struct circ_buf xmit;