Message ID | 20240112163608.528453-3-krzysztof.kozlowski@linaro.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp294723dyc; Fri, 12 Jan 2024 08:37:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IHndL6iUrKhEOOVnsIuVgVMbwXE20KK5+GAi6WbciStRucZ3N+b2n0Wm87yxixTTvQjS3iE X-Received: by 2002:a17:90b:3650:b0:28d:e16c:7055 with SMTP id nh16-20020a17090b365000b0028de16c7055mr1295129pjb.73.1705077472271; Fri, 12 Jan 2024 08:37:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705077472; cv=none; d=google.com; s=arc-20160816; b=RfdQp/FCNahKsIq5obzwusT93/PlhF8HilRCk0Mil2NuSYKw8zgUBDQ+OzNRg5KHjf Gnc1N0FvOIcToOccPEb+wOadw1VJM9rblexbWAr5Kgs1P6EezC7TvEBW36uutoXveuEd 7h7GBzzwgZncHZVNALzr3VAPYtrZh+FuQFr2NeaO07Xv23t3hI5TMUtE7qHeJ6MhRm1D Ty8+L1ePfbQ6m9ky1LuGQ53k8Cjl6UmQANa5ao/oRAnbxZ1cSL2izNKuCEcN+/UnLLWG oLHMJzcPIoM6Qi3Bu4IlE04zEwPxetRSa0grb1TnyN2DmnBmLr/UMw+jdGWmglxiXaVN LEZw== 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=gyDDK8wz61UD70/QcPoYFZCJBphOvhD/N3TrrEWzs04=; fh=O48av0w63oqyBwyJ444pZAVrdLa46/f2S34FJDLoWVA=; b=IU85PNwiAJp1disgg6/sQ1kPrzPdWEmEpkPOMqVTeqWFzYJbK23Nfa7/59n+yqfiue 3BkxijixLnNS6G3CkLt4rq795JtmxsjhAIPkwwmkKHeAJw9LwOa/6DeqGPjpzi14NFk1 6rMo00RetFmRPpMjDtff2b/mfJXdSQG4h/HPURHx137MfB2OA/YfM7TZvuy5A+y2nrG2 Z83mkLL6z1x0PPao5+2oOygZ5dc94rJtnA+Yv1NDKr2oNi03L9gnTpR+1hmM++LyKZlA 3b+NrdB6aMXhsT4PoEqIiNl7a+P4InEotA/XuZPt9IQtqhAZVOPe6Vy9F8dEzOilR7pM RsUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WFMKF70M; spf=pass (google.com: domain of linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id jy13-20020a17090342cd00b001d55aa9ffecsi3640305plb.253.2024.01.12.08.37.52 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 08:37:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WFMKF70M; spf=pass (google.com: domain of linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24864-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.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 0C033282D50 for <ouuuleilei@gmail.com>; Fri, 12 Jan 2024 16:37:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C21147E78A; Fri, 12 Jan 2024 16:36:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="WFMKF70M" Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4497777641 for <linux-kernel@vger.kernel.org>; Fri, 12 Jan 2024 16:36:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-wr1-f50.google.com with SMTP id ffacd0b85a97d-3367a304091so5596968f8f.3 for <linux-kernel@vger.kernel.org>; Fri, 12 Jan 2024 08:36:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1705077384; x=1705682184; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gyDDK8wz61UD70/QcPoYFZCJBphOvhD/N3TrrEWzs04=; b=WFMKF70MQdvbpCQNxtFCRG4HC2JPJ+F8sKSGod/UI3nSLF7tQW4qLRgcGPw7x/OVeX 0+/dwlkJCWSqGKbC55TcQ6iwJM+FTAb4o4Eh9X8cxkwHKZtxuf7HfgqdB5YPf9zfBKQN pIjguKLVEo5ypA8hu1TXFkU2ZhkQG5YPf48z4/gWoyYz7YT10TGAalMwKwPuiAsND4gr jT5+cwydZzGqe/MeqZc8mjcmWAK0U5Dpk9ll/GEaQSgYGgsFnEZYhzekOdBPghHJVT4V yYhsLQWplnKHJ+Eooz/b0emwV/lVJ5LrXVry1BdgPICbjL8K39Nq6WF+0r0CR06OeapX ZVFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705077384; x=1705682184; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gyDDK8wz61UD70/QcPoYFZCJBphOvhD/N3TrrEWzs04=; b=F786W0+Z6AmHIc36aZ2eCLlXvRvnqmuGJa4yfI5WD16dnYC2fzE6TAJZXwCXBguGUv UFCP3vv8MQpkFNb1PGQRTdKnVqp4+7BAOKtuvG8BywOAPUwR5zs7jIqDOUTxt2O8I8Fj LhOivwBYUok7pPhg+2Y6nN2mpce0Kusp1Z4frrOloZvwxlfDRB6BP8zdruSaC+f1Km2Q MkpVxPpqMK47Bjb7bVh5l0sxPQoW3ResXw87JTRb6SJxlfn0vM4XUs67SjIGexugcdki PQzOOOGsysatzdJv0CoCdCZBtBr586Y2dpNhny40TZvboZMeZ7bcN5xewLt+POKmXARp TZfg== X-Gm-Message-State: AOJu0YxGzZZiLGqdiTItQ7JXhv3FWL0xWvUIM15W0WAHUetVicMNan/U Xk7K4JT/zLvKtncIeAva6vUADc1wZY8HXQ== X-Received: by 2002:a7b:c5cb:0:b0:40e:4921:5038 with SMTP id n11-20020a7bc5cb000000b0040e49215038mr648930wmk.91.1705077384525; Fri, 12 Jan 2024 08:36:24 -0800 (PST) Received: from krzk-bin.. ([178.197.223.112]) by smtp.gmail.com with ESMTPSA id bd16-20020a05600c1f1000b0040e5a93ae53sm6573195wmb.22.2024.01.12.08.36.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 08:36:24 -0800 (PST) From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> To: Bjorn Andersson <andersson@kernel.org>, Konrad Dybcio <konrad.dybcio@linaro.org>, Srinivas Kandagatla <srinivas.kandagatla@linaro.org>, Banajit Goswami <bgoswami@quicinc.com>, Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>, Rob Herring <robh+dt@kernel.org>, Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>, Conor Dooley <conor+dt@kernel.org>, Peter Rosin <peda@axentia.se>, Philipp Zabel <p.zabel@pengutronix.de>, Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>, linux-arm-msm@vger.kernel.org, alsa-devel@alsa-project.org, linux-sound@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>, Bartosz Golaszewski <brgl@bgdev.pl>, Chris Packham <chris.packham@alliedtelesis.co.nz>, Sean Anderson <sean.anderson@seco.com> Subject: [PATCH v3 2/5] reset: Instantiate reset GPIO controller for shared reset-gpios Date: Fri, 12 Jan 2024 17:36:05 +0100 Message-Id: <20240112163608.528453-3-krzysztof.kozlowski@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240112163608.528453-1-krzysztof.kozlowski@linaro.org> References: <20240112163608.528453-1-krzysztof.kozlowski@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787903315867154603 X-GMAIL-MSGID: 1787903315867154603 |
Series |
reset: gpio: ASoC: shared GPIO resets
|
|
Commit Message
Krzysztof Kozlowski
Jan. 12, 2024, 4:36 p.m. UTC
Devices sharing a reset GPIO could use the reset framework for
coordinated handling of that shared GPIO line. We have several cases of
such needs, at least for Devicetree-based platforms.
If Devicetree-based device requests a reset line, while "resets"
Devicetree property is missing but there is a "reset-gpios" one,
instantiate a new "reset-gpio" platform device which will handle such
reset line. This allows seamless handling of such shared reset-gpios
without need of changing Devicetree binding [1].
To avoid creating multiple "reset-gpio" platform devices, store the
Devicetree "reset-gpios" GPIO specifiers used for new devices on a
linked list. Later such Devicetree GPIO specifier (phandle to GPIO
controller, GPIO number and GPIO flags) is used to check if reset
controller for given GPIO was already registered.
If two devices have conflicting "reset-gpios" property, e.g. with
different ACTIVE_xxx flags, this would allow to spawn two separate
"reset-gpio" devices, where the second would fail probing on busy GPIO
request.
Link: https://lore.kernel.org/all/YXi5CUCEi7YmNxXM@robh.at.kernel.org/ [1]
Cc: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Chris Packham <chris.packham@alliedtelesis.co.nz>
Cc: Sean Anderson <sean.anderson@seco.com>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
drivers/reset/core.c | 227 +++++++++++++++++++++++++++++--
include/linux/reset-controller.h | 4 +
2 files changed, 217 insertions(+), 14 deletions(-)
Comments
On Fri, Jan 12, 2024 at 5:36 PM Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> wrote: > > Devices sharing a reset GPIO could use the reset framework for > coordinated handling of that shared GPIO line. We have several cases of > such needs, at least for Devicetree-based platforms. > > If Devicetree-based device requests a reset line, while "resets" > Devicetree property is missing but there is a "reset-gpios" one, > instantiate a new "reset-gpio" platform device which will handle such > reset line. This allows seamless handling of such shared reset-gpios > without need of changing Devicetree binding [1]. > > To avoid creating multiple "reset-gpio" platform devices, store the > Devicetree "reset-gpios" GPIO specifiers used for new devices on a > linked list. Later such Devicetree GPIO specifier (phandle to GPIO > controller, GPIO number and GPIO flags) is used to check if reset > controller for given GPIO was already registered. > > If two devices have conflicting "reset-gpios" property, e.g. with > different ACTIVE_xxx flags, this would allow to spawn two separate > "reset-gpio" devices, where the second would fail probing on busy GPIO > request. > > Link: https://lore.kernel.org/all/YXi5CUCEi7YmNxXM@robh.at.kernel.org/ [1] > Cc: Bartosz Golaszewski <brgl@bgdev.pl> > Cc: Chris Packham <chris.packham@alliedtelesis.co.nz> > Cc: Sean Anderson <sean.anderson@seco.com> > Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org> > --- > drivers/reset/core.c | 227 +++++++++++++++++++++++++++++-- > include/linux/reset-controller.h | 4 + > 2 files changed, 217 insertions(+), 14 deletions(-) > > diff --git a/drivers/reset/core.c b/drivers/reset/core.c > index 4d5a78d3c085..86e33a703ad2 100644 > --- a/drivers/reset/core.c > +++ b/drivers/reset/core.c > @@ -10,9 +10,13 @@ > #include <linux/export.h> > #include <linux/kernel.h> > #include <linux/kref.h> > +#include <linux/gpio/driver.h> > +#include <linux/gpio/machine.h> > +#include <linux/idr.h> > #include <linux/module.h> > #include <linux/of.h> > #include <linux/acpi.h> > +#include <linux/platform_device.h> > #include <linux/reset.h> > #include <linux/reset-controller.h> > #include <linux/slab.h> > @@ -23,6 +27,11 @@ static LIST_HEAD(reset_controller_list); > static DEFINE_MUTEX(reset_lookup_mutex); > static LIST_HEAD(reset_lookup_list); > > +/* Protects reset_gpio_lookup_list */ > +static DEFINE_MUTEX(reset_gpio_lookup_mutex); > +static LIST_HEAD(reset_gpio_lookup_list); > +static DEFINE_IDA(reset_gpio_ida); > + > /** > * struct reset_control - a reset control > * @rcdev: a pointer to the reset controller device > @@ -63,6 +72,16 @@ struct reset_control_array { > struct reset_control *rstc[] __counted_by(num_rstcs); > }; > > +/** > + * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices > + * @of_args: phandle to the reset controller with all the args like GPIO number > + * @list: list entry for the reset_gpio_lookup_list > + */ > +struct reset_gpio_lookup { > + struct of_phandle_args of_args; > + struct list_head list; > +}; > + > static const char *rcdev_name(struct reset_controller_dev *rcdev) > { > if (rcdev->dev) > @@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc) > kref_put(&rstc->refcnt, __reset_control_release); > } > > +static bool __reset_gpios_args_match(const struct of_phandle_args *a1, > + const struct of_phandle_args *a2) > +{ > + unsigned int i; > + > + if (!a2) > + return false; > + > + if (a1->args_count != a2->args_count) > + return false; > + > + for (i = 0; i < a1->args_count; i++) > + if (a1->args[i] != a2->args[i]) > + return false; > + > + return true; > +} > + > +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, > + unsigned int gpio, > + unsigned int of_flags) > +{ > + struct gpiod_lookup_table *lookup __free(kfree) = NULL; > + struct gpio_device *gdev __free(gpio_device_put) = NULL; > + char *label __free(kfree) = NULL; I got yelled at by Linus Torvalds personally for doing it like this. I know this is a common pattern in code using GLib but Linus wants auto variables to be initialized where they're declared... > + unsigned int lookup_flags; > + > + /* > + * Later we map GPIO flags between OF and Linux, however not all > + * constants from include/dt-bindings/gpio/gpio.h and > + * include/linux/gpio/machine.h match each other. > + */ > + if (of_flags > GPIO_ACTIVE_LOW) { > + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", > + of_flags, gpio); > + return -EINVAL; > + } > + > + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np)); .. so this should become: struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...) and same for the rest. Don't get me wrong, I love cleanup.h but there's a (unofficial for now) coding style. > + if (!gdev) > + return -EPROBE_DEFER; > + > + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL); > + if (!label) > + return -EINVAL; > + > + /* Size: one lookup entry plus sentinel */ > + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); > + if (!lookup) > + return -ENOMEM; > + > + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); > + if (!lookup->dev_id) > + return -ENOMEM; > + > + lookup_flags = GPIO_PERSISTENT; > + lookup_flags |= of_flags & GPIO_ACTIVE_LOW; > + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", > + lookup_flags); > + > + gpiod_add_lookup_table(no_free_ptr(lookup)); You told me that this doesn't need to be removed or ever freed but a comment on that would be in order. > + > + return 0; > +} > + > +/* > + * @reset_args: phandle to the GPIO provider with all the args like GPIO number > + */ > +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) > +{ > + struct reset_gpio_lookup *rgpio_dev; > + struct platform_device *pdev; > + int id, ret; > + > + /* > + * Registering reset-gpio device might cause immediate > + * bind, resulting in its probe() registering new reset controller thus > + * taking reset_list_mutex lock via reset_controller_register(). > + */ > + lockdep_assert_not_held(&reset_list_mutex); So how does dumping the stack help here exactly? > + > + mutex_lock(&reset_gpio_lookup_mutex); > + > + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { > + if (args->np == rgpio_dev->of_args.np) { > + if (__reset_gpios_args_match(args, &rgpio_dev->of_args)) > + goto out; /* Already on the list, done */ > + } > + } > + > + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); > + if (id < 0) { > + ret = id; > + goto err_unlock; > + } > + > + /* > + * Not freed in normal path, persisent subsystem data (which is assumed > + * also in the reset-gpio driver). > + */ > + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); > + if (!rgpio_dev) { > + ret = -ENOMEM; > + goto err_ida_free; > + } > + > + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], > + args->args[1]); > + if (ret < 0) > + goto err_kfree; > + > + rgpio_dev->of_args = *args; > + /* > + * We keep the device_node reference, but of_args.np is put at the end > + * of __of_reset_control_get(), so get it one more time. > + * Hold reference as long as rgpio_dev memory is valid. > + */ > + of_node_get(rgpio_dev->of_args.np); > + pdev = platform_device_register_data(NULL, "reset-gpio", id, > + &rgpio_dev->of_args, > + sizeof(rgpio_dev->of_args)); > + ret = PTR_ERR_OR_ZERO(pdev); > + if (ret) > + goto err_put; > + > + list_add(&rgpio_dev->list, &reset_gpio_lookup_list); > + > +out: > + mutex_unlock(&reset_gpio_lookup_mutex); > + > + return 0; > + > +err_put: > + of_node_put(rgpio_dev->of_args.np); > +err_kfree: > + kfree(rgpio_dev); > +err_ida_free: > + ida_free(&reset_gpio_ida, id); > +err_unlock: > + mutex_unlock(&reset_gpio_lookup_mutex); > + You're already using cleanup helpers above, why not here too? Would make this function much more readable and allow you to drop all but the ida_free() here. Possibly you'd need to define the __free() callback for of_node_put() though. > + return ret; > +} > + > +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, > + bool gpio_fallback) > +{ > + struct reset_controller_dev *r, *rcdev; > + > + lockdep_assert_held(&reset_list_mutex); > + > + rcdev = NULL; > + list_for_each_entry(r, &reset_controller_list, list) { > + if (args->np == r->of_node) { > + if (gpio_fallback) { > + if (__reset_gpios_args_match(args, r->of_args)) { > + rcdev = r; > + break; > + } > + } else { > + rcdev = r; > + break; > + } > + } > + } > + > + return rcdev; > +} > + > struct reset_control * > __of_reset_control_get(struct device_node *node, const char *id, int index, > bool shared, bool optional, bool acquired) > { > + struct of_phandle_args args = {0}; > + bool gpio_fallback = false; > struct reset_control *rstc; > - struct reset_controller_dev *r, *rcdev; > - struct of_phandle_args args; > + struct reset_controller_dev *rcdev; > int rstc_id; > int ret; > > @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, > index, &args); > if (ret == -EINVAL) > return ERR_PTR(ret); > - if (ret) > - return optional ? NULL : ERR_PTR(ret); > + if (ret) { > + /* > + * There can be only one reset-gpio for regular devices, so > + * don't bother with GPIO index. > + */ > + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", > + 0, &args); > + if (ret) > + return optional ? NULL : ERR_PTR(ret); > > - mutex_lock(&reset_list_mutex); > - rcdev = NULL; > - list_for_each_entry(r, &reset_controller_list, list) { > - if (args.np == r->of_node) { > - rcdev = r; > - break; > + gpio_fallback = true; > + > + ret = __reset_add_reset_gpio_device(&args); > + if (ret) { > + rstc = ERR_PTR(ret); > + goto out_put; > } > } > > + mutex_lock(&reset_list_mutex); > + rcdev = __reset_find_rcdev(&args, gpio_fallback); > if (!rcdev) { > rstc = ERR_PTR(-EPROBE_DEFER); > - goto out; > + goto out_unlock; > } > > if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { > rstc = ERR_PTR(-EINVAL); > - goto out; > + goto out_unlock; > } > > rstc_id = rcdev->of_xlate(rcdev, &args); > if (rstc_id < 0) { > rstc = ERR_PTR(rstc_id); > - goto out; > + goto out_unlock; > } > > /* reset_list_mutex also protects the rcdev's reset_control list */ > rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); > > -out: > +out_unlock: > mutex_unlock(&reset_list_mutex); > +out_put: > of_node_put(args.np); I suggest reworking this to use cleanup.h as well. Bartosz > > return rstc; > diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h > index 0fa4f60e1186..e064473215de 100644 > --- a/include/linux/reset-controller.h > +++ b/include/linux/reset-controller.h > @@ -61,6 +61,9 @@ struct reset_control_lookup { > * @dev: corresponding driver model device struct > * @of_node: corresponding device tree node as phandle target > * @of_reset_n_cells: number of cells in reset line specifiers > + * TODO: of_args have of_node, so we have here duplication > + * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO > + * number complementing of_node > * @of_xlate: translation function to translate from specifier as found in the > * device tree to id as given to the reset control ops, defaults > * to :c:func:`of_reset_simple_xlate`. > @@ -74,6 +77,7 @@ struct reset_controller_dev { > struct device *dev; > struct device_node *of_node; > int of_reset_n_cells; > + const struct of_phandle_args *of_args; > int (*of_xlate)(struct reset_controller_dev *rcdev, > const struct of_phandle_args *reset_spec); > unsigned int nr_resets; > -- > 2.34.1 >
On 15/01/2024 17:06, Bartosz Golaszewski wrote: >> + >> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, >> + unsigned int gpio, >> + unsigned int of_flags) >> +{ >> + struct gpiod_lookup_table *lookup __free(kfree) = NULL; >> + struct gpio_device *gdev __free(gpio_device_put) = NULL; >> + char *label __free(kfree) = NULL; > > I got yelled at by Linus Torvalds personally for doing it like this. I > know this is a common pattern in code using GLib but Linus wants auto > variables to be initialized where they're declared... Declaration is here. Initialization is here. Therefore this is initialized where it is declared. What's more it is initialized to a valid value, because __free() accepts NULLs. > >> + unsigned int lookup_flags; >> + >> + /* >> + * Later we map GPIO flags between OF and Linux, however not all >> + * constants from include/dt-bindings/gpio/gpio.h and >> + * include/linux/gpio/machine.h match each other. >> + */ >> + if (of_flags > GPIO_ACTIVE_LOW) { >> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", >> + of_flags, gpio); >> + return -EINVAL; >> + } >> + >> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np)); > > ... so this should become: > > struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...) > > and same for the rest. > > Don't get me wrong, I love cleanup.h but there's a (unofficial for > now) coding style. So you just want to declare it not in top-part of the function but just before first use? > >> + if (!gdev) >> + return -EPROBE_DEFER; >> + >> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL); >> + if (!label) >> + return -EINVAL; >> + >> + /* Size: one lookup entry plus sentinel */ >> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); >> + if (!lookup) >> + return -ENOMEM; >> + >> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); >> + if (!lookup->dev_id) >> + return -ENOMEM; >> + >> + lookup_flags = GPIO_PERSISTENT; >> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW; >> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", >> + lookup_flags); >> + >> + gpiod_add_lookup_table(no_free_ptr(lookup)); > > You told me that this doesn't need to be removed or ever freed but a > comment on that would be in order. Sure, code further comments on this but I can make it explicit here as well. > >> + >> + return 0; >> +} >> + >> +/* >> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number >> + */ >> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) >> +{ >> + struct reset_gpio_lookup *rgpio_dev; >> + struct platform_device *pdev; >> + int id, ret; >> + >> + /* >> + * Registering reset-gpio device might cause immediate >> + * bind, resulting in its probe() registering new reset controller thus >> + * taking reset_list_mutex lock via reset_controller_register(). >> + */ >> + lockdep_assert_not_held(&reset_list_mutex); > > So how does dumping the stack help here exactly? This is self-documenting code. dumping stack does not matter, the point is that future developers should see this lockdep before they start playing with new locks. > >> + >> + mutex_lock(&reset_gpio_lookup_mutex); >> + >> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { >> + if (args->np == rgpio_dev->of_args.np) { >> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args)) >> + goto out; /* Already on the list, done */ >> + } >> + } >> + >> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); >> + if (id < 0) { >> + ret = id; >> + goto err_unlock; >> + } >> + >> + /* >> + * Not freed in normal path, persisent subsystem data (which is assumed >> + * also in the reset-gpio driver). >> + */ >> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); >> + if (!rgpio_dev) { >> + ret = -ENOMEM; >> + goto err_ida_free; >> + } >> + >> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], >> + args->args[1]); >> + if (ret < 0) >> + goto err_kfree; >> + >> + rgpio_dev->of_args = *args; >> + /* >> + * We keep the device_node reference, but of_args.np is put at the end >> + * of __of_reset_control_get(), so get it one more time. >> + * Hold reference as long as rgpio_dev memory is valid. >> + */ >> + of_node_get(rgpio_dev->of_args.np); >> + pdev = platform_device_register_data(NULL, "reset-gpio", id, >> + &rgpio_dev->of_args, >> + sizeof(rgpio_dev->of_args)); >> + ret = PTR_ERR_OR_ZERO(pdev); >> + if (ret) >> + goto err_put; >> + >> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list); >> + >> +out: >> + mutex_unlock(&reset_gpio_lookup_mutex); >> + >> + return 0; >> + >> +err_put: >> + of_node_put(rgpio_dev->of_args.np); >> +err_kfree: >> + kfree(rgpio_dev); >> +err_ida_free: >> + ida_free(&reset_gpio_ida, id); >> +err_unlock: >> + mutex_unlock(&reset_gpio_lookup_mutex); >> + > > You're already using cleanup helpers above, why not here too? Would > make this function much more readable and allow you to drop all but Not sure how much it would be cleaner considering that these are not free on success. > the ida_free() here. Possibly you'd need to define the __free() > callback for of_node_put() though. > >> + return ret; >> +} >> + >> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, >> + bool gpio_fallback) >> +{ >> + struct reset_controller_dev *r, *rcdev; >> + >> + lockdep_assert_held(&reset_list_mutex); >> + >> + rcdev = NULL; >> + list_for_each_entry(r, &reset_controller_list, list) { >> + if (args->np == r->of_node) { >> + if (gpio_fallback) { >> + if (__reset_gpios_args_match(args, r->of_args)) { >> + rcdev = r; >> + break; >> + } >> + } else { >> + rcdev = r; >> + break; >> + } >> + } >> + } >> + >> + return rcdev; >> +} >> + >> struct reset_control * >> __of_reset_control_get(struct device_node *node, const char *id, int index, >> bool shared, bool optional, bool acquired) >> { >> + struct of_phandle_args args = {0}; >> + bool gpio_fallback = false; >> struct reset_control *rstc; >> - struct reset_controller_dev *r, *rcdev; >> - struct of_phandle_args args; >> + struct reset_controller_dev *rcdev; >> int rstc_id; >> int ret; >> >> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, >> index, &args); >> if (ret == -EINVAL) >> return ERR_PTR(ret); >> - if (ret) >> - return optional ? NULL : ERR_PTR(ret); >> + if (ret) { >> + /* >> + * There can be only one reset-gpio for regular devices, so >> + * don't bother with GPIO index. >> + */ >> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", >> + 0, &args); >> + if (ret) >> + return optional ? NULL : ERR_PTR(ret); >> >> - mutex_lock(&reset_list_mutex); >> - rcdev = NULL; >> - list_for_each_entry(r, &reset_controller_list, list) { >> - if (args.np == r->of_node) { >> - rcdev = r; >> - break; >> + gpio_fallback = true; >> + >> + ret = __reset_add_reset_gpio_device(&args); >> + if (ret) { >> + rstc = ERR_PTR(ret); >> + goto out_put; >> } >> } >> >> + mutex_lock(&reset_list_mutex); >> + rcdev = __reset_find_rcdev(&args, gpio_fallback); >> if (!rcdev) { >> rstc = ERR_PTR(-EPROBE_DEFER); >> - goto out; >> + goto out_unlock; >> } >> >> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { >> rstc = ERR_PTR(-EINVAL); >> - goto out; >> + goto out_unlock; >> } >> >> rstc_id = rcdev->of_xlate(rcdev, &args); >> if (rstc_id < 0) { >> rstc = ERR_PTR(rstc_id); >> - goto out; >> + goto out_unlock; >> } >> >> /* reset_list_mutex also protects the rcdev's reset_control list */ >> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); >> >> -out: >> +out_unlock: >> mutex_unlock(&reset_list_mutex); >> +out_put: >> of_node_put(args.np); > > I suggest reworking this to use cleanup.h as well. It's independent task. This is an existing code and any refactoring to cleanup or not is independent thing. Best regards, Krzysztof
On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote: [...] > diff --git a/drivers/reset/core.c b/drivers/reset/core.c > index 4d5a78d3c085..86e33a703ad2 100644 > --- a/drivers/reset/core.c > +++ b/drivers/reset/core.c [...] > @@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc) > kref_put(&rstc->refcnt, __reset_control_release); > } > > +static bool __reset_gpios_args_match(const struct of_phandle_args *a1, > + const struct of_phandle_args *a2) > +{ > + unsigned int i; > + > + if (!a2) > + return false; > + > + if (a1->args_count != a2->args_count) > + return false; > + > + for (i = 0; i < a1->args_count; i++) > + if (a1->args[i] != a2->args[i]) > + return false; > + > + return true; > +} How about making this return a2 && a1->np == a2->np && a1->args_count == a2->args_count && !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count); ? There's similar code in include/linux/cpufreq.h, maybe this could later be lifted into a common of_phandle_args_equal(). > + > +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, > + unsigned int gpio, > + unsigned int of_flags) > +{ > + struct gpiod_lookup_table *lookup __free(kfree) = NULL; > + struct gpio_device *gdev __free(gpio_device_put) = NULL; > + char *label __free(kfree) = NULL; > + unsigned int lookup_flags; > + > + /* > + * Later we map GPIO flags between OF and Linux, however not all > + * constants from include/dt-bindings/gpio/gpio.h and > + * include/linux/gpio/machine.h match each other. > + */ > + if (of_flags > GPIO_ACTIVE_LOW) { > + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", > + of_flags, gpio); > + return -EINVAL; > + } > + > + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np)); > + if (!gdev) > + return -EPROBE_DEFER; > + > + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL); > + if (!label) > + return -EINVAL; The kstrdup() failure looks like it should be -ENOMEM to me. I'd check the gpio_device_get_label(gdev) return value separately. Is this going to be in v6.8-rc1, or does using gpio_device_get_label() introduce a dependency? > + > + /* Size: one lookup entry plus sentinel */ > + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); > + if (!lookup) > + return -ENOMEM; > + > + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); > + if (!lookup->dev_id) > + return -ENOMEM; > + > + lookup_flags = GPIO_PERSISTENT; > + lookup_flags |= of_flags & GPIO_ACTIVE_LOW; > + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", > + lookup_flags); > + > + gpiod_add_lookup_table(no_free_ptr(lookup)); > + > + return 0; > +} > + > +/* > + * @reset_args: phandle to the GPIO provider with all the args like GPIO number s/reset_// > + */ > +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) > +{ > + struct reset_gpio_lookup *rgpio_dev; > + struct platform_device *pdev; > + int id, ret; > + > + /* > + * Registering reset-gpio device might cause immediate > + * bind, resulting in its probe() registering new reset controller thus > + * taking reset_list_mutex lock via reset_controller_register(). > + */ > + lockdep_assert_not_held(&reset_list_mutex); > + > + mutex_lock(&reset_gpio_lookup_mutex); > + > + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { > + if (args->np == rgpio_dev->of_args.np) { > + if (__reset_gpios_args_match(args, &rgpio_dev->of_args)) > + goto out; /* Already on the list, done */ > + } > + } > + > + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); > + if (id < 0) { > + ret = id; > + goto err_unlock; > + } > + > + /* > + * Not freed in normal path, persisent subsystem data (which is assumed > + * also in the reset-gpio driver). > + */ > + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); > + if (!rgpio_dev) { > + ret = -ENOMEM; > + goto err_ida_free; > + } > + > + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], > + args->args[1]); > + if (ret < 0) > + goto err_kfree; > + > + rgpio_dev->of_args = *args; > + /* > + * We keep the device_node reference, but of_args.np is put at the end > + * of __of_reset_control_get(), so get it one more time. > + * Hold reference as long as rgpio_dev memory is valid. > + */ > + of_node_get(rgpio_dev->of_args.np); > + pdev = platform_device_register_data(NULL, "reset-gpio", id, > + &rgpio_dev->of_args, > + sizeof(rgpio_dev->of_args)); > + ret = PTR_ERR_OR_ZERO(pdev); > + if (ret) > + goto err_put; > + > + list_add(&rgpio_dev->list, &reset_gpio_lookup_list); > + > +out: > + mutex_unlock(&reset_gpio_lookup_mutex); > + > + return 0; > + > +err_put: > + of_node_put(rgpio_dev->of_args.np); > +err_kfree: > + kfree(rgpio_dev); > +err_ida_free: > + ida_free(&reset_gpio_ida, id); > +err_unlock: > + mutex_unlock(&reset_gpio_lookup_mutex); > + > + return ret; > +} > + > +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, > + bool gpio_fallback) > +{ > + struct reset_controller_dev *r, *rcdev; Now that this is moved into a function, there's no need for the r, rcdev split anymore. Just return a match when found, and NULL at the end: struct reset_controller_dev *rcdev; > + > + lockdep_assert_held(&reset_list_mutex); > + > + rcdev = NULL; > + list_for_each_entry(r, &reset_controller_list, list) { list_for_each_entry(rcdev, &reset_controller_list, list) { > + if (args->np == r->of_node) { > + if (gpio_fallback) { > + if (__reset_gpios_args_match(args, r->of_args)) { > + rcdev = r; > + break; return rcdev; > + } > + } else { > + rcdev = r; > + break; > + } > + } With the np check moved into __reset_gpios_args_match() above, the whole loop could be turned into: if (gpio_fallback) { if (__reset_gpios_args_match(args, rcdev->of_args)) return rcdev; } else { if (args->np == rcdev->of_node) return rcdev; } Explicitly checking against rcdev->of_args->np instead of rcdev- >of_node in gpio_fallback mode could avoid false positives in case anybody ever creates a combined GPIO and reset controller device and then uses its GPIOs to drive a shared reset line.. > + } > + > + return rcdev; return NULL; > +} > > struct reset_control * > __of_reset_control_get(struct device_node *node, const char *id, int index, > bool shared, bool optional, bool acquired) > { > + struct of_phandle_args args = {0}; Is this still needed? > + bool gpio_fallback = false; > struct reset_control *rstc; > - struct reset_controller_dev *r, *rcdev; > - struct of_phandle_args args; > + struct reset_controller_dev *rcdev; > int rstc_id; > int ret; > > @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, > index, &args); > if (ret == -EINVAL) > return ERR_PTR(ret); > - if (ret) > - return optional ? NULL : ERR_PTR(ret); > + if (ret) { > + /* > + * There can be only one reset-gpio for regular devices, so > + * don't bother with GPIO index. > + */ > + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", > + 0, &args); > + if (ret) > + return optional ? NULL : ERR_PTR(ret); > > - mutex_lock(&reset_list_mutex); > - rcdev = NULL; > - list_for_each_entry(r, &reset_controller_list, list) { > - if (args.np == r->of_node) { > - rcdev = r; > - break; > + gpio_fallback = true; > + > + ret = __reset_add_reset_gpio_device(&args); > + if (ret) { > + rstc = ERR_PTR(ret); > + goto out_put; > } > } > > + mutex_lock(&reset_list_mutex); > + rcdev = __reset_find_rcdev(&args, gpio_fallback); > if (!rcdev) { > rstc = ERR_PTR(-EPROBE_DEFER); > - goto out; > + goto out_unlock; > } > > if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { Nice. I like that the __of_reset_control_get() changes are much less invasive now. > rstc = ERR_PTR(-EINVAL); > - goto out; > + goto out_unlock; > } > > rstc_id = rcdev->of_xlate(rcdev, &args); > if (rstc_id < 0) { > rstc = ERR_PTR(rstc_id); > - goto out; > + goto out_unlock; > } > > /* reset_list_mutex also protects the rcdev's reset_control list */ > rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); > > -out: > +out_unlock: > mutex_unlock(&reset_list_mutex); > +out_put: > of_node_put(args.np); > > return rstc; > diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h > index 0fa4f60e1186..e064473215de 100644 > --- a/include/linux/reset-controller.h > +++ b/include/linux/reset-controller.h > @@ -61,6 +61,9 @@ struct reset_control_lookup { > * @dev: corresponding driver model device struct > * @of_node: corresponding device tree node as phandle target > * @of_reset_n_cells: number of cells in reset line specifiers > + * TODO: of_args have of_node, so we have here duplication Any plans what to do about this? With the above changes we could mandate that either of_node or of_args should be set, never both. > + * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO > + * number complementing of_node regards Philipp
On Mo, 2024-01-15 at 17:13 +0100, Krzysztof Kozlowski wrote: > On 15/01/2024 17:06, Bartosz Golaszewski wrote: > > > + > > > +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, > > > + unsigned int gpio, > > > + unsigned int of_flags) > > > +{ > > > + struct gpiod_lookup_table *lookup __free(kfree) = NULL; > > > + struct gpio_device *gdev __free(gpio_device_put) = NULL; > > > + char *label __free(kfree) = NULL; > > > > I got yelled at by Linus Torvalds personally for doing it like this. I > > know this is a common pattern in code using GLib but Linus wants auto > > variables to be initialized where they're declared... > > Declaration is here. Initialization is here. Therefore this is > initialized where it is declared. What's more it is initialized to a > valid value, because __free() accepts NULLs. [...] > > ... so this should become: > > > > struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...) > > > > and same for the rest. > > > > Don't get me wrong, I love cleanup.h but there's a (unofficial for > > now) coding style. > > So you just want to declare it not in top-part of the function but just > before first use? IIUC, Linus wants exactly this: https://lore.kernel.org/all/CAHk-=wgRHiV5VSxtfXA4S6aLUmcQYEuB67u3BJPJPtuESs1JyA@mail.gmail.com/ [...] > > > > > + goto out_unlock; > > > } > > > > > > rstc_id = rcdev->of_xlate(rcdev, &args); > > > if (rstc_id < 0) { > > > rstc = ERR_PTR(rstc_id); > > > - goto out; > > > + goto out_unlock; > > > } > > > > > > /* reset_list_mutex also protects the rcdev's reset_control list */ > > > rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); > > > > > > -out: > > > +out_unlock: > > > mutex_unlock(&reset_list_mutex); > > > +out_put: > > > of_node_put(args.np); > > > > I suggest reworking this to use cleanup.h as well. > > It's independent task. This is an existing code and any refactoring to > cleanup or not is independent thing. Seconded. Separate cleanup very welcome, but this series is about adding functionality. regards Philipp
On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote: [...] > struct reset_control * > __of_reset_control_get(struct device_node *node, const char *id, int index, > bool shared, bool optional, bool acquired) > { > + struct of_phandle_args args = {0}; > + bool gpio_fallback = false; > struct reset_control *rstc; > - struct reset_controller_dev *r, *rcdev; > - struct of_phandle_args args; > + struct reset_controller_dev *rcdev; > int rstc_id; > int ret; > > @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, > index, &args); > if (ret == -EINVAL) > return ERR_PTR(ret); > - if (ret) > - return optional ? NULL : ERR_PTR(ret); > + if (ret) { I think this should continue to return optional ? NULL : ERR_PTR(ret) if !IS_ENABLED(CONFIG_RESET_GPIO), for example by just skipping the of_parse_phandle_with_args(). That should allow the GPIO fallback in patch 5 to work as expected. > + /* > + * There can be only one reset-gpio for regular devices, so > + * don't bother with GPIO index. > + */ > + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", > + 0, &args); > + if (ret) > + return optional ? NULL : ERR_PTR(ret); > > - mutex_lock(&reset_list_mutex); > - rcdev = NULL; > - list_for_each_entry(r, &reset_controller_list, list) { > - if (args.np == r->of_node) { > - rcdev = r; > - break; > + gpio_fallback = true; > + > + ret = __reset_add_reset_gpio_device(&args); > + if (ret) { > + rstc = ERR_PTR(ret); > + goto out_put; > } > } regards Philipp
On 15/01/2024 18:32, Philipp Zabel wrote: > On Mo, 2024-01-15 at 17:13 +0100, Krzysztof Kozlowski wrote: >> On 15/01/2024 17:06, Bartosz Golaszewski wrote: >>>> + >>>> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, >>>> + unsigned int gpio, >>>> + unsigned int of_flags) >>>> +{ >>>> + struct gpiod_lookup_table *lookup __free(kfree) = NULL; >>>> + struct gpio_device *gdev __free(gpio_device_put) = NULL; >>>> + char *label __free(kfree) = NULL; >>> >>> I got yelled at by Linus Torvalds personally for doing it like this. I >>> know this is a common pattern in code using GLib but Linus wants auto >>> variables to be initialized where they're declared... >> >> Declaration is here. Initialization is here. Therefore this is >> initialized where it is declared. What's more it is initialized to a >> valid value, because __free() accepts NULLs. > [...] >>> ... so this should become: >>> >>> struct gpio_device *gdev __free(gpio_device_put) = gpio_device_find(...) >>> >>> and same for the rest. >>> >>> Don't get me wrong, I love cleanup.h but there's a (unofficial for >>> now) coding style. >> >> So you just want to declare it not in top-part of the function but just >> before first use? > > IIUC, Linus wants exactly this: > > https://lore.kernel.org/all/CAHk-=wgRHiV5VSxtfXA4S6aLUmcQYEuB67u3BJPJPtuESs1JyA@mail.gmail.com/ Sure. Best regards, Krzysztof
On 15/01/2024 17:55, Philipp Zabel wrote: > On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote: >> >> +static bool __reset_gpios_args_match(const struct of_phandle_args *a1, >> + const struct of_phandle_args *a2) >> +{ >> + unsigned int i; >> + >> + if (!a2) >> + return false; >> + >> + if (a1->args_count != a2->args_count) >> + return false; >> + >> + for (i = 0; i < a1->args_count; i++) >> + if (a1->args[i] != a2->args[i]) >> + return false; >> + >> + return true; >> +} > > How about making this > > return a2 && > a1->np == a2->np && > a1->args_count == a2->args_count && > !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count); > > ? > > There's similar code in include/linux/cpufreq.h, maybe this could later > be lifted into a common of_phandle_args_equal(). I'll make a helper because such long return is also not the fastest to parse by brain. > >> + >> +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, >> + unsigned int gpio, >> + unsigned int of_flags) >> +{ >> + struct gpiod_lookup_table *lookup __free(kfree) = NULL; >> + struct gpio_device *gdev __free(gpio_device_put) = NULL; >> + char *label __free(kfree) = NULL; >> + unsigned int lookup_flags; >> + >> + /* >> + * Later we map GPIO flags between OF and Linux, however not all >> + * constants from include/dt-bindings/gpio/gpio.h and >> + * include/linux/gpio/machine.h match each other. >> + */ >> + if (of_flags > GPIO_ACTIVE_LOW) { >> + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", >> + of_flags, gpio); >> + return -EINVAL; >> + } >> + >> + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np)); >> + if (!gdev) >> + return -EPROBE_DEFER; >> + >> + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL); >> + if (!label) >> + return -EINVAL; > > The kstrdup() failure looks like it should be -ENOMEM to me. > I'd check the gpio_device_get_label(gdev) return value separately. OK, makes sense. One more local variable will be needed for that. > > Is this going to be in v6.8-rc1, or does using gpio_device_get_label() > introduce a dependency? We were already in the merge window, so no problem here. gpio_device_get_label() is in v6.8-rc1. > >> + >> + /* Size: one lookup entry plus sentinel */ >> + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); >> + if (!lookup) >> + return -ENOMEM; >> + >> + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); >> + if (!lookup->dev_id) >> + return -ENOMEM; >> + >> + lookup_flags = GPIO_PERSISTENT; >> + lookup_flags |= of_flags & GPIO_ACTIVE_LOW; >> + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", >> + lookup_flags); >> + >> + gpiod_add_lookup_table(no_free_ptr(lookup)); >> + >> + return 0; >> +} >> + >> +/* >> + * @reset_args: phandle to the GPIO provider with all the args like GPIO number > > s/reset_// ack > >> + */ >> +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) >> +{ >> + struct reset_gpio_lookup *rgpio_dev; >> + struct platform_device *pdev; >> + int id, ret; >> + >> + /* >> + * Registering reset-gpio device might cause immediate >> + * bind, resulting in its probe() registering new reset controller thus >> + * taking reset_list_mutex lock via reset_controller_register(). >> + */ >> + lockdep_assert_not_held(&reset_list_mutex); >> + >> + mutex_lock(&reset_gpio_lookup_mutex); >> + >> + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { >> + if (args->np == rgpio_dev->of_args.np) { >> + if (__reset_gpios_args_match(args, &rgpio_dev->of_args)) >> + goto out; /* Already on the list, done */ >> + } >> + } >> + >> + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); >> + if (id < 0) { >> + ret = id; >> + goto err_unlock; >> + } >> + >> + /* >> + * Not freed in normal path, persisent subsystem data (which is assumed >> + * also in the reset-gpio driver). >> + */ >> + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); >> + if (!rgpio_dev) { >> + ret = -ENOMEM; >> + goto err_ida_free; >> + } >> + >> + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], >> + args->args[1]); >> + if (ret < 0) >> + goto err_kfree; >> + >> + rgpio_dev->of_args = *args; >> + /* >> + * We keep the device_node reference, but of_args.np is put at the end >> + * of __of_reset_control_get(), so get it one more time. >> + * Hold reference as long as rgpio_dev memory is valid. >> + */ >> + of_node_get(rgpio_dev->of_args.np); >> + pdev = platform_device_register_data(NULL, "reset-gpio", id, >> + &rgpio_dev->of_args, >> + sizeof(rgpio_dev->of_args)); >> + ret = PTR_ERR_OR_ZERO(pdev); >> + if (ret) >> + goto err_put; >> + >> + list_add(&rgpio_dev->list, &reset_gpio_lookup_list); >> + >> +out: >> + mutex_unlock(&reset_gpio_lookup_mutex); >> + >> + return 0; >> + >> +err_put: >> + of_node_put(rgpio_dev->of_args.np); >> +err_kfree: >> + kfree(rgpio_dev); >> +err_ida_free: >> + ida_free(&reset_gpio_ida, id); >> +err_unlock: >> + mutex_unlock(&reset_gpio_lookup_mutex); >> + >> + return ret; >> +} >> + >> +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, >> + bool gpio_fallback) >> +{ >> + struct reset_controller_dev *r, *rcdev; > > Now that this is moved into a function, there's no need for the r, > rcdev split anymore. Just return a match when found, and NULL at the > end: > > struct reset_controller_dev *rcdev; Indeed, thanks. > >> + >> + lockdep_assert_held(&reset_list_mutex); >> + >> + rcdev = NULL; >> + list_for_each_entry(r, &reset_controller_list, list) { > > list_for_each_entry(rcdev, &reset_controller_list, list) { > >> + if (args->np == r->of_node) { >> + if (gpio_fallback) { >> + if (__reset_gpios_args_match(args, r->of_args)) { >> + rcdev = r; >> + break; > > return rcdev; > >> + } >> + } else { >> + rcdev = r; >> + break; >> + } >> + } > > With the np check moved into __reset_gpios_args_match() above, the > whole loop could be turned into: > > if (gpio_fallback) { > if (__reset_gpios_args_match(args, rcdev->of_args)) > return rcdev; > } else { > if (args->np == rcdev->of_node) > return rcdev; > } > > Explicitly checking against rcdev->of_args->np instead of rcdev- >> of_node in gpio_fallback mode could avoid false positives in case > anybody ever creates a combined GPIO and reset controller device and > then uses its GPIOs to drive a shared reset line.. ack > >> + } >> + >> + return rcdev; > > return NULL; ack > >> +} >> >> struct reset_control * >> __of_reset_control_get(struct device_node *node, const char *id, int index, >> bool shared, bool optional, bool acquired) >> { >> + struct of_phandle_args args = {0}; > > Is this still needed? I will double check. > >> + bool gpio_fallback = false; >> struct reset_control *rstc; >> - struct reset_controller_dev *r, *rcdev; >> - struct of_phandle_args args; >> + struct reset_controller_dev *rcdev; >> int rstc_id; >> int ret; >> >> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, >> index, &args); >> if (ret == -EINVAL) >> return ERR_PTR(ret); >> - if (ret) >> - return optional ? NULL : ERR_PTR(ret); >> + if (ret) { >> + /* >> + * There can be only one reset-gpio for regular devices, so >> + * don't bother with GPIO index. >> + */ >> + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", >> + 0, &args); >> + if (ret) >> + return optional ? NULL : ERR_PTR(ret); >> >> - mutex_lock(&reset_list_mutex); >> - rcdev = NULL; >> - list_for_each_entry(r, &reset_controller_list, list) { >> - if (args.np == r->of_node) { >> - rcdev = r; >> - break; >> + gpio_fallback = true; >> + >> + ret = __reset_add_reset_gpio_device(&args); >> + if (ret) { >> + rstc = ERR_PTR(ret); >> + goto out_put; >> } >> } >> >> + mutex_lock(&reset_list_mutex); >> + rcdev = __reset_find_rcdev(&args, gpio_fallback); >> if (!rcdev) { >> rstc = ERR_PTR(-EPROBE_DEFER); >> - goto out; >> + goto out_unlock; >> } >> >> if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { > > Nice. I like that the __of_reset_control_get() changes are much less > invasive now. > >> rstc = ERR_PTR(-EINVAL); >> - goto out; >> + goto out_unlock; >> } >> >> rstc_id = rcdev->of_xlate(rcdev, &args); >> if (rstc_id < 0) { >> rstc = ERR_PTR(rstc_id); >> - goto out; >> + goto out_unlock; >> } >> >> /* reset_list_mutex also protects the rcdev's reset_control list */ >> rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); >> >> -out: >> +out_unlock: >> mutex_unlock(&reset_list_mutex); >> +out_put: >> of_node_put(args.np); >> >> return rstc; >> diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h >> index 0fa4f60e1186..e064473215de 100644 >> --- a/include/linux/reset-controller.h >> +++ b/include/linux/reset-controller.h >> @@ -61,6 +61,9 @@ struct reset_control_lookup { >> * @dev: corresponding driver model device struct >> * @of_node: corresponding device tree node as phandle target >> * @of_reset_n_cells: number of cells in reset line specifiers >> + * TODO: of_args have of_node, so we have here duplication > > Any plans what to do about this? With the above changes we could > mandate that either of_node or of_args should be set, never both. Yes, makes sense. We could also drop of_node, but the code won't be more readable. Best regards, Krzysztof
On 17/01/2024 12:26, Philipp Zabel wrote: > On Fr, 2024-01-12 at 17:36 +0100, Krzysztof Kozlowski wrote: > [...] >> struct reset_control * >> __of_reset_control_get(struct device_node *node, const char *id, int index, >> bool shared, bool optional, bool acquired) >> { >> + struct of_phandle_args args = {0}; >> + bool gpio_fallback = false; >> struct reset_control *rstc; >> - struct reset_controller_dev *r, *rcdev; >> - struct of_phandle_args args; >> + struct reset_controller_dev *rcdev; >> int rstc_id; >> int ret; >> >> @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, >> index, &args); >> if (ret == -EINVAL) >> return ERR_PTR(ret); >> - if (ret) >> - return optional ? NULL : ERR_PTR(ret); >> + if (ret) { > > I think this should continue to return optional ? NULL : ERR_PTR(ret) > if !IS_ENABLED(CONFIG_RESET_GPIO), for example by just skipping the > of_parse_phandle_with_args(). That should allow the GPIO fallback in > patch 5 to work as expected. > ack Best regards, Krzysztof
diff --git a/drivers/reset/core.c b/drivers/reset/core.c index 4d5a78d3c085..86e33a703ad2 100644 --- a/drivers/reset/core.c +++ b/drivers/reset/core.c @@ -10,9 +10,13 @@ #include <linux/export.h> #include <linux/kernel.h> #include <linux/kref.h> +#include <linux/gpio/driver.h> +#include <linux/gpio/machine.h> +#include <linux/idr.h> #include <linux/module.h> #include <linux/of.h> #include <linux/acpi.h> +#include <linux/platform_device.h> #include <linux/reset.h> #include <linux/reset-controller.h> #include <linux/slab.h> @@ -23,6 +27,11 @@ static LIST_HEAD(reset_controller_list); static DEFINE_MUTEX(reset_lookup_mutex); static LIST_HEAD(reset_lookup_list); +/* Protects reset_gpio_lookup_list */ +static DEFINE_MUTEX(reset_gpio_lookup_mutex); +static LIST_HEAD(reset_gpio_lookup_list); +static DEFINE_IDA(reset_gpio_ida); + /** * struct reset_control - a reset control * @rcdev: a pointer to the reset controller device @@ -63,6 +72,16 @@ struct reset_control_array { struct reset_control *rstc[] __counted_by(num_rstcs); }; +/** + * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices + * @of_args: phandle to the reset controller with all the args like GPIO number + * @list: list entry for the reset_gpio_lookup_list + */ +struct reset_gpio_lookup { + struct of_phandle_args of_args; + struct list_head list; +}; + static const char *rcdev_name(struct reset_controller_dev *rcdev) { if (rcdev->dev) @@ -813,13 +832,183 @@ static void __reset_control_put_internal(struct reset_control *rstc) kref_put(&rstc->refcnt, __reset_control_release); } +static bool __reset_gpios_args_match(const struct of_phandle_args *a1, + const struct of_phandle_args *a2) +{ + unsigned int i; + + if (!a2) + return false; + + if (a1->args_count != a2->args_count) + return false; + + for (i = 0; i < a1->args_count; i++) + if (a1->args[i] != a2->args[i]) + return false; + + return true; +} + +static int __reset_add_reset_gpio_lookup(int id, struct device_node *np, + unsigned int gpio, + unsigned int of_flags) +{ + struct gpiod_lookup_table *lookup __free(kfree) = NULL; + struct gpio_device *gdev __free(gpio_device_put) = NULL; + char *label __free(kfree) = NULL; + unsigned int lookup_flags; + + /* + * Later we map GPIO flags between OF and Linux, however not all + * constants from include/dt-bindings/gpio/gpio.h and + * include/linux/gpio/machine.h match each other. + */ + if (of_flags > GPIO_ACTIVE_LOW) { + pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n", + of_flags, gpio); + return -EINVAL; + } + + gdev = gpio_device_find_by_fwnode(of_fwnode_handle(np)); + if (!gdev) + return -EPROBE_DEFER; + + label = kstrdup(gpio_device_get_label(gdev), GFP_KERNEL); + if (!label) + return -EINVAL; + + /* Size: one lookup entry plus sentinel */ + lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); + if (!lookup) + return -ENOMEM; + + lookup->dev_id = kasprintf(GFP_KERNEL, "reset-gpio.%d", id); + if (!lookup->dev_id) + return -ENOMEM; + + lookup_flags = GPIO_PERSISTENT; + lookup_flags |= of_flags & GPIO_ACTIVE_LOW; + lookup->table[0] = GPIO_LOOKUP(no_free_ptr(label), gpio, "reset", + lookup_flags); + + gpiod_add_lookup_table(no_free_ptr(lookup)); + + return 0; +} + +/* + * @reset_args: phandle to the GPIO provider with all the args like GPIO number + */ +static int __reset_add_reset_gpio_device(const struct of_phandle_args *args) +{ + struct reset_gpio_lookup *rgpio_dev; + struct platform_device *pdev; + int id, ret; + + /* + * Registering reset-gpio device might cause immediate + * bind, resulting in its probe() registering new reset controller thus + * taking reset_list_mutex lock via reset_controller_register(). + */ + lockdep_assert_not_held(&reset_list_mutex); + + mutex_lock(&reset_gpio_lookup_mutex); + + list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) { + if (args->np == rgpio_dev->of_args.np) { + if (__reset_gpios_args_match(args, &rgpio_dev->of_args)) + goto out; /* Already on the list, done */ + } + } + + id = ida_alloc(&reset_gpio_ida, GFP_KERNEL); + if (id < 0) { + ret = id; + goto err_unlock; + } + + /* + * Not freed in normal path, persisent subsystem data (which is assumed + * also in the reset-gpio driver). + */ + rgpio_dev = kzalloc(sizeof(*rgpio_dev), GFP_KERNEL); + if (!rgpio_dev) { + ret = -ENOMEM; + goto err_ida_free; + } + + ret = __reset_add_reset_gpio_lookup(id, args->np, args->args[0], + args->args[1]); + if (ret < 0) + goto err_kfree; + + rgpio_dev->of_args = *args; + /* + * We keep the device_node reference, but of_args.np is put at the end + * of __of_reset_control_get(), so get it one more time. + * Hold reference as long as rgpio_dev memory is valid. + */ + of_node_get(rgpio_dev->of_args.np); + pdev = platform_device_register_data(NULL, "reset-gpio", id, + &rgpio_dev->of_args, + sizeof(rgpio_dev->of_args)); + ret = PTR_ERR_OR_ZERO(pdev); + if (ret) + goto err_put; + + list_add(&rgpio_dev->list, &reset_gpio_lookup_list); + +out: + mutex_unlock(&reset_gpio_lookup_mutex); + + return 0; + +err_put: + of_node_put(rgpio_dev->of_args.np); +err_kfree: + kfree(rgpio_dev); +err_ida_free: + ida_free(&reset_gpio_ida, id); +err_unlock: + mutex_unlock(&reset_gpio_lookup_mutex); + + return ret; +} + +static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args, + bool gpio_fallback) +{ + struct reset_controller_dev *r, *rcdev; + + lockdep_assert_held(&reset_list_mutex); + + rcdev = NULL; + list_for_each_entry(r, &reset_controller_list, list) { + if (args->np == r->of_node) { + if (gpio_fallback) { + if (__reset_gpios_args_match(args, r->of_args)) { + rcdev = r; + break; + } + } else { + rcdev = r; + break; + } + } + } + + return rcdev; +} + struct reset_control * __of_reset_control_get(struct device_node *node, const char *id, int index, bool shared, bool optional, bool acquired) { + struct of_phandle_args args = {0}; + bool gpio_fallback = false; struct reset_control *rstc; - struct reset_controller_dev *r, *rcdev; - struct of_phandle_args args; + struct reset_controller_dev *rcdev; int rstc_id; int ret; @@ -839,39 +1028,49 @@ __of_reset_control_get(struct device_node *node, const char *id, int index, index, &args); if (ret == -EINVAL) return ERR_PTR(ret); - if (ret) - return optional ? NULL : ERR_PTR(ret); + if (ret) { + /* + * There can be only one reset-gpio for regular devices, so + * don't bother with GPIO index. + */ + ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells", + 0, &args); + if (ret) + return optional ? NULL : ERR_PTR(ret); - mutex_lock(&reset_list_mutex); - rcdev = NULL; - list_for_each_entry(r, &reset_controller_list, list) { - if (args.np == r->of_node) { - rcdev = r; - break; + gpio_fallback = true; + + ret = __reset_add_reset_gpio_device(&args); + if (ret) { + rstc = ERR_PTR(ret); + goto out_put; } } + mutex_lock(&reset_list_mutex); + rcdev = __reset_find_rcdev(&args, gpio_fallback); if (!rcdev) { rstc = ERR_PTR(-EPROBE_DEFER); - goto out; + goto out_unlock; } if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) { rstc = ERR_PTR(-EINVAL); - goto out; + goto out_unlock; } rstc_id = rcdev->of_xlate(rcdev, &args); if (rstc_id < 0) { rstc = ERR_PTR(rstc_id); - goto out; + goto out_unlock; } /* reset_list_mutex also protects the rcdev's reset_control list */ rstc = __reset_control_get_internal(rcdev, rstc_id, shared, acquired); -out: +out_unlock: mutex_unlock(&reset_list_mutex); +out_put: of_node_put(args.np); return rstc; diff --git a/include/linux/reset-controller.h b/include/linux/reset-controller.h index 0fa4f60e1186..e064473215de 100644 --- a/include/linux/reset-controller.h +++ b/include/linux/reset-controller.h @@ -61,6 +61,9 @@ struct reset_control_lookup { * @dev: corresponding driver model device struct * @of_node: corresponding device tree node as phandle target * @of_reset_n_cells: number of cells in reset line specifiers + * TODO: of_args have of_node, so we have here duplication + * @of_args: for reset-gpios controllers: corresponding phandle args with GPIO + * number complementing of_node * @of_xlate: translation function to translate from specifier as found in the * device tree to id as given to the reset control ops, defaults * to :c:func:`of_reset_simple_xlate`. @@ -74,6 +77,7 @@ struct reset_controller_dev { struct device *dev; struct device_node *of_node; int of_reset_n_cells; + const struct of_phandle_args *of_args; int (*of_xlate)(struct reset_controller_dev *rcdev, const struct of_phandle_args *reset_spec); unsigned int nr_resets;