From patchwork Thu Aug 8 14:42:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Draszik?= X-Patchwork-Id: 13757712 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D714AC52D6F for ; Thu, 8 Aug 2024 14:44:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wAtcktpsyK1IopCF5kU8mOGUnH018rQKQhDOBPxOSZw=; b=YIlyx0amK3ZFDMHz+rRHzDzX1i YplcVB3o0EIOF+px122eov4m+VM7TD9LoFL1XMgIrrYW3eZDeNl2xflaOMD14EJMjLG+7B9xzpWz7 G5ZvQs3T4IDcgbZ7fnrNe38dDMf8nJPjMbArWZE021HlspgNu6r9l6kjHhG1uAWzmbiM639WEIq4c InSiQwlN7EYL+nYYDl0m84+7m7NFNbbwnjb/6Ryrjzw8D4AYM2qUGipKSY2RaWwn5OJnvAlck83pM jbV+bWidA9KXVR3XRY0xhGCjH9BqgTuwiW16O82c3OFGQ440bxlbtsnfe2RkUVXMjpngPQSEJrjk9 +KlveVuA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sc4N1-00000008ZpD-0puk; Thu, 08 Aug 2024 14:44:03 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sc4Lq-00000008ZNA-2wCr for linux-arm-kernel@lists.infradead.org; Thu, 08 Aug 2024 14:42:52 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a7a8e73b29cso108954366b.3 for ; Thu, 08 Aug 2024 07:42:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723128168; x=1723732968; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=wAtcktpsyK1IopCF5kU8mOGUnH018rQKQhDOBPxOSZw=; b=R/klMAdlCj5CQvb9WKduv+v6ZuujddTMO/ehFwpxeP4TcqhOXiRMu3kw4aJvc0a3U6 gPTzZMXkfNFpo3LnVqAAuVps9y0l2OfONdxZz33QWBPp7TiiggUIPbYZLbHF0jg0nXu4 KHy+nnTRe0+Xs+e/5+wJoFmDIJOXU+kau/9z4rRs2F3eEOhCv7o2ZDDH1rA5lyP1Jogr XJFswUUJtCOqwMqSXxuo8q1B8RcaZ5pW+TeWVtDiaIpLWHg1XOMC9vtqrAKMg31GFMto 3iOtXDsA2vK/svdxS+ddhW/Io1BbnZzYNTY8oMEcZ9oqh2UwWT7uP5M8zNEmqHzpoLyz DcAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723128168; x=1723732968; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wAtcktpsyK1IopCF5kU8mOGUnH018rQKQhDOBPxOSZw=; b=ZGQFg79EJ7h7pouAmheNzAN9xpqmuNjpp/rSTjA9eIN4F79HWTxPn7FJU+I1of9OyY 95TMEMu/3ELw58MWpSPut8COkSI303jeERAblO9cL9t9z5lrilwJft0pCax/sInMT/Bm EqH/qzit0pd9x6n7LXoW0JEActOBeJnVNO026jSbsX2PhgTiKlj1oCofQ1gQUAN6t1SP Gpe4Xd1cQzWMn0Xd7t4W9cnBstWuOv145CBP/YhAOS8cBi6chwI8nHCidSEe0wj21gqB XB/PrtlHRuut2YI4A1JQfSv98puuskPQkrBqRTYChrEs7OVZuV2fwuWZ9I4FV9eJp5GZ WQcA== X-Forwarded-Encrypted: i=1; AJvYcCUcFW865AyTTcwpqLyM9MjN3YaKdAtOv9EA+iYVTWPCKYJkvSudWH605yqUvMLaMbQBLcY3QwGZPQI3ICAosghdcvBKrkA9Rangtk53l7jnjWyPe9g= X-Gm-Message-State: AOJu0YwRqryS4Y+hcJMcTdegJh8NWJ3uUhwhAvLckgVq7U9/+a30Y/t9 4IPs9PN7OIZuQVd2ajE9DnGy7VZJLFSZTWj12h6okRb8fVBygICQwX9Fc4f3i1w= X-Google-Smtp-Source: AGHT+IFKqWTwUTF9It4/KiFcH08kll66MLBeAiBxCYt6pGnnfGGyfN4DPvbUWI5sbCigMk5HvKV9ng== X-Received: by 2002:a17:907:c7d4:b0:a7d:a00a:a9fe with SMTP id a640c23a62f3a-a8090c83b95mr144065666b.17.1723128168207; Thu, 08 Aug 2024 07:42:48 -0700 (PDT) Received: from puffmais.c.googlers.com (64.227.90.34.bc.googleusercontent.com. [34.90.227.64]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7dc9e80e04sm759298966b.175.2024.08.08.07.42.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 07:42:47 -0700 (PDT) From: =?utf-8?q?Andr=C3=A9_Draszik?= Date: Thu, 08 Aug 2024 15:42:42 +0100 Subject: [PATCH v6 01/20] clk: bump stdout clock usage for earlycon MIME-Version: 1.0 Message-Id: <20240808-gs101-non-essential-clocks-2-v6-1-e91c537acedc@linaro.org> References: <20240808-gs101-non-essential-clocks-2-v6-0-e91c537acedc@linaro.org> In-Reply-To: <20240808-gs101-non-essential-clocks-2-v6-0-e91c537acedc@linaro.org> To: Michael Turquette , Stephen Boyd , Peter Griffin , Krzysztof Kozlowski , Sylwester Nawrocki , Chanwoo Choi , Alim Akhtar , Sam Protsenko , Tudor Ambarus , Abel Vesa , Peng Fan , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam Cc: Will McVicker , kernel-team@android.com, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-samsung-soc@vger.kernel.org, imx@lists.linux.dev, =?utf-8?q?Andr=C3=A9_Draszik?= X-Mailer: b4 0.13.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240808_074250_798875_56A4537F X-CRM114-Status: GOOD ( 26.74 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On some platforms, earlycon depends on the bootloader setup stdout clocks being retained. In some cases stdout UART clocks (or their parents) can get disabled during loading of other drivers (e.g. i2c) causing earlycon to stop to work sometime into the boot, halting the whole system. Since there are at least two platforms where that is the case, i.MX and the Exynos-derivative gs101, this patch adds some logic to the clk core to detect these clocks if earlycon is enabled, to bump their usage count as part of of_clk_add_hw_provider() and of_clk_add_provider(), and to release them again at the end of init. This way code duplication in affected platforms can be avoided. The general idea is based on similar code in the i.MX clock driver, but this here is a bit more generic as in general (e.g. on gs101) clocks can come from various different clock units (driver instances) and therefore it can be necessary to run this code multiple times until all required stdout clocks have probed. Signed-off-by: André Draszik --- v6: * drop a stray #include from drivers/clk/samsung/clk-gs101.c --- drivers/clk/clk.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 7264cf6165ce..03c5d80e833c 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4923,6 +4923,131 @@ static void clk_core_reparent_orphans(void) clk_prepare_unlock(); } +/** + * struct of_clk_stdout_clks - holds data that is required for handling extra + * references to stdout clocks during early boot. + * + * On some platforms, earlycon depends on the bootloader setup stdout clocks + * being retained. In some cases stdout UART clocks (or their parents) can get + * disabled during loading of other drivers (e.g. i2c) causing earlycon to stop + * to work sometime into the boot, halting the system. + * + * Having logic to detect these clocks if earlycon is enabled helps with those + * cases by bumping their usage count during init. The extra usage count is + * later dropped at the end of init. + * + * @bump_refs: whether or not to add the extra stdout clock references + * @lock: mutex protecting access + * @have_all: whether or not we have acquired all clocks, to handle cases of + * clocks coming from different drivers / instances + * @clks: clocks associated with stdout + * @n_clks: number of clocks associated with stdout + */ +static struct of_clk_stdout_clks { + bool bump_refs; + + struct mutex lock; + bool have_all; + struct clk **clks; + size_t n_clks; +} of_clk_stdout_clks = { + .lock = __MUTEX_INITIALIZER(of_clk_stdout_clks.lock), +}; + +static int __init of_clk_bump_stdout_clocks_param(char *str) +{ + of_clk_stdout_clks.bump_refs = true; + return 0; +} +__setup("earlycon", of_clk_bump_stdout_clocks_param); +__setup_param("earlyprintk", of_clk_keep_stdout_clocks_earlyprintk, + of_clk_bump_stdout_clocks_param, 0); + +static void of_clk_bump_stdout_clocks(void) +{ + size_t n_clks; + + /* + * We only need to run this code if required to do so and only ever + * before late initcalls have run. Otherwise it'd be impossible to know + * when to drop the extra clock references again. + * + * This generally means that this only works if on affected platforms + * the clock drivers have been built-in (as opposed to being modules). + */ + if (!of_clk_stdout_clks.bump_refs) + return; + + n_clks = of_clk_get_parent_count(of_stdout); + if (!n_clks || !of_stdout) + return; + + mutex_lock(&of_clk_stdout_clks.lock); + + /* + * We only need to keep trying if we have not succeeded previously, + * i.e. if not all required clocks were ready during previous attempts. + */ + if (of_clk_stdout_clks.have_all) + goto out_unlock; + + if (!of_clk_stdout_clks.clks) { + of_clk_stdout_clks.n_clks = n_clks; + + of_clk_stdout_clks.clks = kcalloc(of_clk_stdout_clks.n_clks, + sizeof(*of_clk_stdout_clks.clks), + GFP_KERNEL); + if (!of_clk_stdout_clks.clks) + goto out_unlock; + } + + /* assume that this time we'll be able to grab all required clocks */ + of_clk_stdout_clks.have_all = true; + for (size_t i = 0; i < n_clks; ++i) { + struct clk *clk; + + /* we might have grabbed this clock in a previous attempt */ + if (of_clk_stdout_clks.clks[i]) + continue; + + clk = of_clk_get(of_stdout, i); + if (IS_ERR(clk)) { + /* retry next time if clock has not probed yet */ + of_clk_stdout_clks.have_all = false; + continue; + } + + if (clk_prepare_enable(clk)) { + clk_put(clk); + continue; + } + of_clk_stdout_clks.clks[i] = clk; + } + +out_unlock: + mutex_unlock(&of_clk_stdout_clks.lock); +} + +static int __init of_clk_drop_stdout_clocks(void) +{ + for (size_t i = 0; i < of_clk_stdout_clks.n_clks; ++i) { + clk_disable_unprepare(of_clk_stdout_clks.clks[i]); + clk_put(of_clk_stdout_clks.clks[i]); + } + + kfree(of_clk_stdout_clks.clks); + + /* + * Do not try to acquire stdout clocks after late initcalls, e.g. + * during further module loading, as we then wouldn't have a way to + * drop the references (and associated allocations) ever again. + */ + of_clk_stdout_clks.bump_refs = false; + + return 0; +} +late_initcall_sync(of_clk_drop_stdout_clocks); + /** * struct of_clk_provider - Clock provider registration structure * @link: Entry in global list of clock providers @@ -5031,6 +5156,8 @@ int of_clk_add_provider(struct device_node *np, fwnode_dev_initialized(&np->fwnode, true); + of_clk_bump_stdout_clocks(); + return ret; } EXPORT_SYMBOL_GPL(of_clk_add_provider); @@ -5073,6 +5200,8 @@ int of_clk_add_hw_provider(struct device_node *np, fwnode_dev_initialized(&np->fwnode, true); + of_clk_bump_stdout_clocks(); + return ret; } EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);