From patchwork Mon Dec 18 07:09:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tony Lindgren X-Patchwork-Id: 180172 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1075990dyi; Sun, 17 Dec 2023 23:15:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IF9kKW3AFq1VL1Wg2NhlqxnQx5CeQ3awDiu3SRFLPaqCKebe+PcsH+RdE2QSgFxMQbHiqQ5 X-Received: by 2002:a17:90a:b288:b0:28a:69fc:b585 with SMTP id c8-20020a17090ab28800b0028a69fcb585mr9338562pjr.20.1702883705782; Sun, 17 Dec 2023 23:15:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702883705; cv=none; d=google.com; s=arc-20160816; b=sBLaxOcGhyAFowCHt34AKtWm02Trfn6aHvXRnB6DHIU5hmhtZgsJCNring2pMuD+f1 1RZi9zpjlAT51C/4+uZcLGT1oGlJWR3wwfGihQ6WyuL/9L2otVveX6x8/oBW5TtG6YkK 2OPrUx+VcKzANrzljUdNRcVmz4EywWajXhWnA0JF5VzEwlmqt/nZChve3ylbobUMzg5l xOm+8GBQiSxs8M2tt+CcuhwfB3t/Pmau47o1aOeurRnwWGfGVUff+JeSVuLGqmX8iQJd UD/h2A6j2M4VXziE9FzqoKyeqzqZnfY9oBoYBfKSs7LlAYpdapDv1ybaPr3RafShJYEB Yyag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=UXHXsWEpShdSPNkVJcx4mMEO58123+4cQQtbClbcnKQ=; fh=OQrZIrddo55Tm+afxTOsnGwFgD38y2NNd7tU+1OGSCE=; b=dDaOpjNhXJpfNpv7REhXqXXu9CotL4kdFIvZZjWMrYm11MXqZKIGNi41Z6zWb5qnQG CReItPFpQqcJcaXrqk8FwaRhBhAbwuDaGFsAFKO5B/e2KiC8YEKifa9EfXAztrQJn5GE oKSiX+ozpE9C8xFQ/T/Q8hXvx0xfKPDJqPt5bYEFw8KrKqwEXr5Mp2rcBA2YnT6Uuaoa RC5YTUWd1gg9//g3wN+SPcfKA3QHAxRssKiUXkdjUME8T2qnUuHZX/aJS0KiypkDzagR fCMsQja5NfTR/4PGIWzRJkIxAcX729FSmgiRTxM7c6HUpPwNmseyuZfV0lvOADk/8IgZ dKPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atomide.com header.s=25mailst header.b=IeO0O+dn; spf=pass (google.com: domain of linux-kernel+bounces-3086-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3086-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r2-20020a170902be0200b001d0cbef2eeesi17410306pls.71.2023.12.17.23.15.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Dec 2023 23:15:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-3086-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@atomide.com header.s=25mailst header.b=IeO0O+dn; spf=pass (google.com: domain of linux-kernel+bounces-3086-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-3086-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 64C2C281B65 for ; Mon, 18 Dec 2023 07:14:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 60EA7C8D8; Mon, 18 Dec 2023 07:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=atomide.com header.i=@atomide.com header.b="IeO0O+dn" X-Original-To: linux-kernel@vger.kernel.org Received: from mail5.25mail.st (mail5.25mail.st [74.50.62.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B89E101C6; Mon, 18 Dec 2023 07:13:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atomide.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=atomide.com Received: from localhost (91-158-86-216.elisa-laajakaista.fi [91.158.86.216]) by mail5.25mail.st (Postfix) with ESMTPSA id 71184603E6; Mon, 18 Dec 2023 07:13:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=atomide.com; s=25mailst; t=1702883624; bh=bSmbXIJ73bHNYUg5aFssz2mBLov16AXD7w083cyelF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IeO0O+dnZdE8whIwAdpQZzbZ2n64X6V27A73sQvf+fQXEZPZMUegEKlHcPIDlxJs6 cb1d8XRPf2Hnj55YoLrMW/K0Fxs8KPTF49lZNzb7DV2g13sUzh8Bvz5Sk1SR2AqevS IhQ9fBlAUW2B/IMVs/MeskbWc37ybdQ5m/d2A7yQ0af1KyZ4B+Xl768GlXlxA0XQfD JLp5sjwylgg9zBYMVFR0BCBdPYYDusxKhKhorgfWtMhl/8MJjB/rzSmevJQCdVG2oB 1H0b0cDEd+YpKk50DTfnKY4SJogjDNKSfJsOsUoVE29PCZoj3GaPR3SJOQpAbRiVzu IJOqTEZLqCvTQ== From: Tony Lindgren To: Greg Kroah-Hartman , Jiri Slaby , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky Cc: "David S . Miller" , Andy Shevchenko , Dhruva Gole , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Johan Hovold , Sebastian Andrzej Siewior , Vignesh Raghavendra , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org Subject: [RFC PATCH v5 4/6] serial: core: Add support for DEVNAME:0.0 style naming for kernel console Date: Mon, 18 Dec 2023 09:09:51 +0200 Message-ID: <20231218071020.21805-5-tony@atomide.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231218071020.21805-1-tony@atomide.com> References: <20231218071020.21805-1-tony@atomide.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785602984466685431 X-GMAIL-MSGID: 1785602984466685431 We can now add hardware based addressing for serial ports. Starting with commit 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM"), and all the related fixes to this commit, the serial core now knows to which serial port controller the ports are connected. The serial ports can be addressed with DEVNAME:0.0 style naming. The names are something like 00:04:0.0 for a serial port on qemu, and something like 2800000.serial:0.0 on platform device using systems like ARM64 for example. The DEVNAME is the unique serial port hardware controller device name, AKA the name for port->dev. The 0.0 are the serial core controller id and port id. Typically 0.0 are used for each controller and port instance unless the serial port hardware controller has multiple controllers or ports. Using DEVNAME:0.0 style naming actually solves two long term issues for addressing the serial ports: 1. According to Andy Shevchenko, using DEVNAME:0.0 style naming fixes an issue where depending on the BIOS settings, the kernel serial port ttyS instance number may change if HSUART is enabled 2. Device tree using architectures no longer necessarily need to specify aliases to find a specific serial port, and we can just allocate the ttyS instance numbers dynamically in whatever probe order To do this, let's match the hardware addressing style console name to the character device name used, and add a preferred console using the character device name. Note that when using console=DEVNAME:0.0 style kernel command line, the 8250 serial console gets enabled later compared to using console=ttyS naming for ISA ports. This is because the serial port DEVNAME to character device mapping is not known until the serial driver probe time. If used together with earlycon, this issue is avoided. Signed-off-by: Tony Lindgren --- drivers/tty/serial/serial_base.h | 16 +++++++ drivers/tty/serial/serial_base_bus.c | 66 ++++++++++++++++++++++++++++ drivers/tty/serial/serial_core.c | 4 ++ 3 files changed, 86 insertions(+) diff --git a/drivers/tty/serial/serial_base.h b/drivers/tty/serial/serial_base.h --- a/drivers/tty/serial/serial_base.h +++ b/drivers/tty/serial/serial_base.h @@ -45,3 +45,19 @@ 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); + +#ifdef CONFIG_SERIAL_CORE_CONSOLE + +int serial_base_add_preferred_console(struct uart_driver *drv, + struct uart_port *port); + +#else + +static inline +int serial_base_add_preferred_console(struct uart_driver *drv, + struct uart_port *port) +{ + return 0; +} + +#endif diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c --- a/drivers/tty/serial/serial_base_bus.c +++ b/drivers/tty/serial/serial_base_bus.c @@ -8,6 +8,7 @@ * The serial core bus manages the serial core controller instances. */ +#include #include #include #include @@ -204,6 +205,71 @@ void serial_base_port_device_remove(struct serial_port_device *port_dev) put_device(&port_dev->dev); } +#ifdef CONFIG_SERIAL_CORE_CONSOLE + +static int serial_base_add_one_prefcon(const char *match, const char *dev_name, + int port_id) +{ + int ret; + + ret = add_preferred_console_match(match, dev_name, port_id); + if (ret == -ENOENT) + return 0; + + return ret; +} + +static int serial_base_add_prefcon(const char *name, int idx) +{ + const char *char_match __free(kfree) = NULL; + + /* Handle the traditional character device name style console=ttyS0 */ + char_match = kasprintf(GFP_KERNEL, "%s%i", name, idx); + if (!char_match) + return -ENOMEM; + + return serial_base_add_one_prefcon(char_match, name, idx); +} + +/** + * serial_base_add_preferred_console - Adds a preferred console + * @drv: Serial port device driver + * @port: Serial port instance + * + * Tries to add a preferred console for a serial port if specified in the + * kernel command line. Supports both the traditional character device such + * as console=ttyS0, and a hardware addressing based console=DEVNAME:0.0 + * style name. + * + * Translates the kernel command line option using a hardware based addressing + * console=DEVNAME:0.0 to the serial port character device such as ttyS0. + * Cannot be called early for ISA ports, depends on struct device. + * + * Note that duplicates are ignored by add_preferred_console(). + * + * Return: 0 on success, negative error code on failure. + */ +int serial_base_add_preferred_console(struct uart_driver *drv, + struct uart_port *port) +{ + const char *port_match __free(kfree); + int ret; + + ret = serial_base_add_prefcon(drv->dev_name, port->line); + if (ret) + return ret; + + port_match = kasprintf(GFP_KERNEL, "%s:%i.%i", dev_name(port->dev), + port->ctrl_id, port->port_id); + if (!port_match) + return -ENOMEM; + + /* Translate a hardware addressing style console=DEVNAME:0.0 */ + return serial_base_add_one_prefcon(port_match, drv->dev_name, port->line); +} + +#endif + static int serial_base_init(void) { int ret; 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 @@ -3358,6 +3358,10 @@ int serial_core_register_port(struct uart_driver *drv, struct uart_port *port) if (ret) goto err_unregister_ctrl_dev; + ret = serial_base_add_preferred_console(drv, port); + if (ret) + goto err_unregister_port_dev; + ret = serial_core_add_one_port(drv, port); if (ret) goto err_unregister_port_dev;