From patchwork Mon Feb 8 22:21:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 378795 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0A4EC433E6 for ; Mon, 8 Feb 2021 22:25:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 69EB364E7B for ; Mon, 8 Feb 2021 22:25:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231434AbhBHWY6 (ORCPT ); Mon, 8 Feb 2021 17:24:58 -0500 Received: from mout.kundenserver.de ([217.72.192.75]:46091 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231194AbhBHWYy (ORCPT ); Mon, 8 Feb 2021 17:24:54 -0500 Received: from orion.localdomain ([95.115.15.83]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MFslN-1l8MNn1nEV-00HQRS; Mon, 08 Feb 2021 23:22:12 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: rafael@kernel.org, info@metux.net, linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, frowand.list@gmail.com, pantelis.antoniou@konsulko.com, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC PATCH 01/12] of: base: improve error message in of_phandle_iterator_next() Date: Mon, 8 Feb 2021 23:21:52 +0100 Message-Id: <20210208222203.22335-2-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210208222203.22335-1-info@metux.net> References: <20210208222203.22335-1-info@metux.net> X-Provags-ID: V03:K1:StHwu6XV//m6RRcLywFyUHvKNrRov/gvJTl5SbpmNL78cQ82VgA gMBLVnnSz5vwUqWv7NIq021AIJYvyIawbNpv7OqnBSUTIZT8gWhEw/xY1C2HX0fp1+Zjkpq bKUXCHBTco8CJ6YBRO2kXsvzdxfpXRwGP21FM+5Q+95/rd0x1FLyr2jWzp7QTshBM+rjaKs CKv88AY48zy3drrYg8bBA== X-UI-Out-Filterresults: notjunk:1; V03:K0:I6bLjPej9cM=:SHPhq4kKOgXVRSliHDROYW BlaL5EeJ9U9u/fv45try5TZqLZCeEtnA/f2SgNO37cn53FF0xdIArwoV87YwzazAL7Z3mj8M+ RaK1YkDhXJF2BYN6byjojTbwslnT+9LtkbU+30dZSd7NHmqPq5K6XhRa5B5Ol2wbvsrDGXXhB 5C2cija0WXQl8EoGrGksCTqzUTUz0aPjNuPPHUSc27TmOSaoSmZBBKNLTdDk9Jixvj/ASAQZL hGKpJaWhFbW8/r1tLcG/31jzyDG5Wrnl+30DeTkMjdt2YNmJUoLrrg/egVblWK8VY2uxl+nvD XWPlsI5PZt1UNw/4Z0AXBLKzWdt5c1/fnMZE599zoyczQ8GeV0uy+3H/eoBU1TcxU6LD+UdZX GI2INH6/d3E8Ytny5QT7L7snp2/UUGvgY5+jg28tPrhqV/W+3xbgkxF8jOy6U Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Also print out the phandle ID on error message, as a debug aid. Signed-off-by: Enrico Weigelt, metux IT consult --- drivers/of/base.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 161a23631472..8a348f0d3c5e 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -1297,8 +1297,8 @@ int of_phandle_iterator_next(struct of_phandle_iterator *it) if (it->cells_name) { if (!it->node) { - pr_err("%pOF: could not find phandle\n", - it->parent); + pr_err("%pOF: could not find phandle %d\n", + it->parent, it->phandle); goto err; } From patchwork Mon Feb 8 22:21:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 378797 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EAC22C43381 for ; Mon, 8 Feb 2021 22:24:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BC8A664E77 for ; Mon, 8 Feb 2021 22:24:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231256AbhBHWYx (ORCPT ); Mon, 8 Feb 2021 17:24:53 -0500 Received: from mout.kundenserver.de ([217.72.192.73]:53483 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229760AbhBHWYt (ORCPT ); Mon, 8 Feb 2021 17:24:49 -0500 Received: from orion.localdomain ([95.115.15.83]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MmD2M-1lZSw122Zk-00iCcL; Mon, 08 Feb 2021 23:22:13 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: rafael@kernel.org, info@metux.net, linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, frowand.list@gmail.com, pantelis.antoniou@konsulko.com, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC PATCH 03/12] of: base: record root node in interator and use it for phandle lookup Date: Mon, 8 Feb 2021 23:21:54 +0100 Message-Id: <20210208222203.22335-4-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210208222203.22335-1-info@metux.net> References: <20210208222203.22335-1-info@metux.net> X-Provags-ID: V03:K1:L07WfYEKaUHFQXWge6SNQV7uWaSzApb9DBlUnN7eDKenKnVc9cR wReeAdO0XeeuDjURl89qC/QDWRLaPENdF+n1sDQVpo59lDEM1dNCE5i9CVo/W9irbkivn3M U9JZI099qVXl3Nji6F8vE0xyLS9fRj77i6N+b5JmiLhvG2PReQUZYeF6B2j8IxDWHe0+SeW EVCW878DP7MXUa/3HgaSA== X-UI-Out-Filterresults: notjunk:1; V03:K0:3sXEFVMRuSo=:oizTkM24xt/Gez0Dn9xwqo 7zUxaRlqh3OeBp8Jjdj06fXRypKJp9Z2gz3RDogbgfNUe6A66ucDw5DD458kDsbKX3eEiZrnf MgAPqiDuLpgytc9GXNdlf1QMaVGRJ+FqSf1ev+EBU9/W3VYqdgkdC28D7FHt7dKrvmJKS0Qgm T256AyPwTPxAGRk3yhsglxBZ6Po/XBpMvC3gEPcqmH0k8trAHOOkCXzeXTuxx5shLtdd2VvA1 v29px/uzWeqPZkKqS/id45qb0JzSWHekMjNBPSHYOTolmwCmzPO/hs6S7dnur5leVDB3ESNDY 5eZT47aNvvvjTkOLhTwXkrAT4S/rbWD6zTfr95T1koqx1oQ7sEXWUREX6RgKYC7B3xhcKFvrR +C8GmSmufHgHJJXV6pWMSjCPmBj47UZVS2cGTKSDWkf6uvCtu45wDs4aAmbdS Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org For detached oftree support, find the root node and record it, on iterator creation. If we find the root of the global oftree, record NULL, in order to have a clear distinction between detached and non-detached cases. The recorded root node is then used for resolving phandles. Note that in the detached case, phandle cache can't be used, so we have a little performance penalty on repeated phandle lookups. Signed-off-by: Enrico Weigelt, metux IT consult --- drivers/of/base.c | 13 ++++++++++++- include/linux/of.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/of/base.c b/drivers/of/base.c index 6b3d1e817808..e5ef611ed233 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -1249,6 +1249,7 @@ int of_phandle_iterator_init(struct of_phandle_iterator *it, { const __be32 *list; int size; + struct device_node *walk; memset(it, 0, sizeof(*it)); @@ -1270,6 +1271,16 @@ int of_phandle_iterator_init(struct of_phandle_iterator *it, it->phandle_end = list; it->cur = list; + /* + * find the root of our tree and record it, if we're dealing with an + * detached oftree - in non-detached case, we record NULL, for clear + * distinction between these two cases. + */ + for (walk=(struct device_node*)np; + walk->parent; + walk=(struct device_node*)walk->parent); + it->root = ((walk == of_root) ? NULL : walk); + return 0; } EXPORT_SYMBOL_GPL(of_phandle_iterator_init); @@ -1297,7 +1308,7 @@ int of_phandle_iterator_next(struct of_phandle_iterator *it) * Find the provider node and parse the #*-cells property to * determine the argument length. */ - it->node = of_find_node_by_phandle(it->phandle); + it->node = of_find_node_by_phandle_from(it->root, it->phandle); if (it->cells_name) { if (!it->node) { diff --git a/include/linux/of.h b/include/linux/of.h index c285141653e5..dbf2c7442389 100644 --- a/include/linux/of.h +++ b/include/linux/of.h @@ -82,6 +82,7 @@ struct of_phandle_iterator { const char *cells_name; int cell_count; const struct device_node *parent; + struct device_node *root; /* List size information */ const __be32 *list_end; From patchwork Mon Feb 8 22:22:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 378794 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82689C433DB for ; Mon, 8 Feb 2021 22:25:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4BB4964E7B for ; Mon, 8 Feb 2021 22:25:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231478AbhBHWZA (ORCPT ); Mon, 8 Feb 2021 17:25:00 -0500 Received: from mout.kundenserver.de ([217.72.192.74]:52711 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231271AbhBHWYz (ORCPT ); Mon, 8 Feb 2021 17:24:55 -0500 Received: from orion.localdomain ([95.115.15.83]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N2VGj-1luhmR3RJ1-013sEY; Mon, 08 Feb 2021 23:22:16 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: rafael@kernel.org, info@metux.net, linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, frowand.list@gmail.com, pantelis.antoniou@konsulko.com, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC PATCH 09/12] drivers: base: reintroduce find_bus() Date: Mon, 8 Feb 2021 23:22:00 +0100 Message-Id: <20210208222203.22335-10-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210208222203.22335-1-info@metux.net> References: <20210208222203.22335-1-info@metux.net> X-Provags-ID: V03:K1:rzHVi5GoHivXWlnfqQmxW9ROEO3dTvvCGJjkmHY1rYU58qOtQdB BWExFeO+Lo7FQnJw4FivMBV8vVs5qdqC8Daz5Q4ov4Da0LJhOvhFlR2siSolW392MDgY9cH 8XXxJ+Jsbc1T1eAY/QQvG6JTPj2JQU031dT6WF7IOT1GkP5PSJ/eJ5zfvyp2hkFU/FXNikO 2tQpzN1WMr8i6gawh2pSQ== X-UI-Out-Filterresults: notjunk:1; V03:K0:3jDwSQW1sfw=:S+k5ohg/fvN1L6nK+an5kA +R/OSvwPagPSZlWc9jBeQRyCOsKSYmcnZ6bF0GFMGDOmdX8fJtZJp1GS3sOshQtcqou/bjxDV qdNYObRRD4gTkYQOGao7jMJzlMiH/Ac2uM4LBRda23m8yqNhnCPEk5/Loq96ANivz4m8CCcbj /s+VFTolH9CbObyDG2qp+fF8tJrl/H0GwmVkxxV8NyVFHHHDHVXoyoDVsLqZVovSt7T1tTS6t BMJQmQBWcjVricSiOzaFEpI+OgXkx1n4vG78rHYOe1xEXcm7gr85UmUGQloXjulge/oKPFkKj vPAE5uZex4dZ9T+qQ6PLsHnt2nL01nQp67LXPvksmkPXP5n8/xzMIPYQFdcW2HereIzUKBRru jjB4jWNWlZrcXptDCJjplCphEmEPyr9eMjg9IMJVdaFFGJ9xGkFyjo0GKfVOB Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org --- drivers/base/bus.c | 14 ++++++++++---- include/linux/device/bus.h | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 450d3ed6cf1f..a06ae2786092 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -754,13 +754,19 @@ EXPORT_SYMBOL_GPL(device_reprobe); * * Note that kset_find_obj increments bus' reference count. */ -#if 0 -struct bus_type *find_bus(char *name) +struct bus_type *find_bus(const char *name) { struct kobject *k = kset_find_obj(bus_kset, name); - return k ? to_bus(k) : NULL; + struct subsys_private *subsys_priv; + + if (!k) + return NULL; + + subsys_priv = container_of(to_kset(k), struct subsys_private, subsys); + + return subsys_priv->bus; } -#endif /* 0 */ +EXPORT_SYMBOL_GPL(find_bus); static int bus_add_groups(struct bus_type *bus, const struct attribute_group **groups) diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index 36a1dae26c95..b4cbcfe176c5 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -254,6 +254,8 @@ void bus_sort_breadthfirst(struct bus_type *bus, int (*compare)(const struct device *a, const struct device *b)); +struct bus_type *find_bus(const char *name); + /** * bus_unregister_device_by_name - remove device by bus id from specific bus * and unregister it from device core From patchwork Mon Feb 8 22:22:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 378796 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5271FC433E6 for ; Mon, 8 Feb 2021 22:24:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0FFE664E7A for ; Mon, 8 Feb 2021 22:24:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231330AbhBHWY4 (ORCPT ); Mon, 8 Feb 2021 17:24:56 -0500 Received: from mout.kundenserver.de ([212.227.17.13]:33945 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231230AbhBHWYy (ORCPT ); Mon, 8 Feb 2021 17:24:54 -0500 Received: from orion.localdomain ([95.115.15.83]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1M8QFi-1lDcdN1Uhs-004WHh; Mon, 08 Feb 2021 23:22:17 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: rafael@kernel.org, info@metux.net, linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, frowand.list@gmail.com, pantelis.antoniou@konsulko.com, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC PATCH 10/12] export bus_get() / bus_put() Date: Mon, 8 Feb 2021 23:22:01 +0100 Message-Id: <20210208222203.22335-11-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210208222203.22335-1-info@metux.net> References: <20210208222203.22335-1-info@metux.net> X-Provags-ID: V03:K1:kDRmEzIQ/F5sQbE196NAs1VlxNWRXpNXbH/MXduf2nSFEhfshKh Pho+/pjgZO0/8botPezg8A8IsqGKMi4GXn5N7nGB5CjPbdQUuG+mNHjXaZs4/KUm6Tg0ACu d1hAzte3az5tvT8GsFsfwWicGAftGJo45kCNRkBjy1G2hE5omPD8bD9Rau5wP6IOhm+I0Jr IShaMZT+84jKYiXPQa00g== X-UI-Out-Filterresults: notjunk:1; V03:K0:pxUWDFfYvE4=:glNHz9gYCeZb+E7U3fAb5c xJ0nCG21DGUYAjCD2bD0dAOOk4zbmeCuHKpmGHsEUpa6fNeTF2tqf1RETA5TR+ybt0GSpN9Dt 7Xy3fcXl4u/HX02GUU6ZcytLsqs0Htc5/Vc0vchfsxaQPUGPN36UBmXNO4Q20paDFxhSf33tF CzKe03kY2wfMyU6ElkDVybFUBkWjlpL7Kraz1Lh4GY0amWO6n2VoOLKNziuG3Tv/e8VDdwRLI wJ8vIKpqu/Mkig89QwWt2nRqSeyDcrMqWvcU2YxK6D0mqyWm4hc+bIpGEySwpiv3IZbBd3Vd0 Lrdh6F4mXODb8DDDNYM6A+xecFnCRfMFpAHcjE9FYjrW4XG0EPNF4KNfimUCTfcr73rNIgY2w XbW7WcHYv3sk0+W7JyHf82Q4Rt0iuG4dFRL7LP8H6f4CpImdWY8dDH6HwflUA Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org --- drivers/base/bus.c | 6 ++++-- include/linux/device/bus.h | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/base/bus.c b/drivers/base/bus.c index a06ae2786092..2ef92a3c5d7b 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c @@ -39,7 +39,7 @@ static struct kset *system_kset; static int __must_check bus_rescan_devices_helper(struct device *dev, void *data); -static struct bus_type *bus_get(struct bus_type *bus) +struct bus_type *bus_get(struct bus_type *bus) { if (bus) { kset_get(&bus->p->subsys); @@ -47,12 +47,14 @@ static struct bus_type *bus_get(struct bus_type *bus) } return NULL; } +EXPORT_SYMBOL_GPL(bus_get); -static void bus_put(struct bus_type *bus) +void bus_put(struct bus_type *bus) { if (bus) kset_put(&bus->p->subsys); } +EXPORT_SYMBOL_GPL(bus_put); static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h index b4cbcfe176c5..8d6b45df0a82 100644 --- a/include/linux/device/bus.h +++ b/include/linux/device/bus.h @@ -120,6 +120,9 @@ extern void bus_unregister(struct bus_type *bus); extern int __must_check bus_rescan_devices(struct bus_type *bus); +struct bus_type *bus_get(struct bus_type *bus); +void bus_put(struct bus_type *bus); + struct bus_attribute { struct attribute attr; ssize_t (*show)(struct bus_type *bus, char *buf); From patchwork Mon Feb 8 22:22:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Enrico Weigelt, metux IT consult" X-Patchwork-Id: 378793 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 584A8C43381 for ; Mon, 8 Feb 2021 22:25:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2ACA264E77 for ; Mon, 8 Feb 2021 22:25:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231618AbhBHWZW (ORCPT ); Mon, 8 Feb 2021 17:25:22 -0500 Received: from mout.kundenserver.de ([217.72.192.75]:41129 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231518AbhBHWZE (ORCPT ); Mon, 8 Feb 2021 17:25:04 -0500 Received: from orion.localdomain ([95.115.15.83]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1Mf0yy-1lpQQN3nSc-00gcBn; Mon, 08 Feb 2021 23:22:18 +0100 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org Cc: rafael@kernel.org, info@metux.net, linus.walleij@linaro.org, bgolaszewski@baylibre.com, robh+dt@kernel.org, frowand.list@gmail.com, pantelis.antoniou@konsulko.com, linux-gpio@vger.kernel.org, devicetree@vger.kernel.org Subject: [RFC PATCH 11/12] platform/x86: skeleton for oftree based board device initialization Date: Mon, 8 Feb 2021 23:22:02 +0100 Message-Id: <20210208222203.22335-12-info@metux.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210208222203.22335-1-info@metux.net> References: <20210208222203.22335-1-info@metux.net> X-Provags-ID: V03:K1:5KSHO+mZyjEhDezzvwlTZtaq1BeKWhLjf5NtiYXIs+OFGBPy7EN l1EpRKIOv8DC5JQZYZ4QQHdDC3f8UR4I2F/jkZ0aERqtQ8Mp/InvotLjkTmqi0pVp1hXlAr Sy46hIhNUb8Yp+E/wqhUaaP21HkYGk8Ig6/PwmgoVHaZAxFlXFPBppI/z3bY9Dj0w063RFN g49/Zgi270Mr1SU2U0oEw== X-UI-Out-Filterresults: notjunk:1; V03:K0:vPnCi//+1uw=:oDvmtkqLFE+8f70CNahU/3 j+u1BKblgDLihU5g8YskOzPKg03rnnW+X0AIqPunJelZ8nlgiI7s09vkC49kK5yplL/mJ82dG iwGK9QBVDGO6hSST2DYzqAEMVtdXiNw4JOPmFriVN29OExEJserhEVv7kPe+laPSmbAgJoqCj i5jxkx30VqHNXaYOrPxE/2D75P7Rtb508bbe1P4WSP6vJxPKnGqkFwbnU9pAjEV6z6nVPHIBV xXaRWaZZvflWs2UIiM6ymBvDzOYEv3/5xh2AjRxWjFSbZYCCWH17wSo2lVhQSwqYOWMkfM+CD VrWLeqqzpKEXdfB9QbAxSYLjyV4s2lYTdNLg2tZbq/uA99Ld/wwnvdwr6/upbiJwFMOO53kBK 4uUYtD4EPz97t7uap3JkPo26X48GrXbePqMkDm4kVuHnxDxZvuHmpm64VbE84 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Lots of boards have extra devices that can't be fully probed via bus'es (like PCI) or generic firmware mechanisms like ACPI. Often those capabilities are just partial or even highly depend on firmware version. Instead of hand-writing board specific drivers merely for the correct initialization / parameterization of generic drivers, hereby introducing a generic mechanism, using the already well supported oftree. These oftrees are compiled into the driver, which first tries to match machine identifications (eg. DMI strings) against rules defined in the individual oftrees, and on success, probes the devices that are defined by them. For the time being, we just support matching on DMI_BOARD_NAME and DMI_SYS_VENDOR - other criteria, even bus- or ACPI-id's can be added later, when needed. Signed-off-by: Enrico Weigelt, metux IT consult --- drivers/platform/Kconfig | 2 + drivers/platform/Makefile | 1 + drivers/platform/of/Kconfig | 41 ++++++++++++++ drivers/platform/of/Makefile | 5 ++ drivers/platform/of/drv.c | 123 +++++++++++++++++++++++++++++++++++++++++ drivers/platform/of/init.c | 126 ++++++++++++++++++++++++++++++++++++++++++ drivers/platform/of/ofboard.h | 8 +++ 7 files changed, 306 insertions(+) create mode 100644 drivers/platform/of/Kconfig create mode 100644 drivers/platform/of/Makefile create mode 100644 drivers/platform/of/drv.c create mode 100644 drivers/platform/of/init.c create mode 100644 drivers/platform/of/ofboard.h diff --git a/drivers/platform/Kconfig b/drivers/platform/Kconfig index 18fc6a08569e..9ac6d4e2a762 100644 --- a/drivers/platform/Kconfig +++ b/drivers/platform/Kconfig @@ -15,3 +15,5 @@ source "drivers/platform/mellanox/Kconfig" source "drivers/platform/olpc/Kconfig" source "drivers/platform/surface/Kconfig" + +source "drivers/platform/of/Kconfig" diff --git a/drivers/platform/Makefile b/drivers/platform/Makefile index 4de08ef4ec9d..ca4d74701fd7 100644 --- a/drivers/platform/Makefile +++ b/drivers/platform/Makefile @@ -10,3 +10,4 @@ obj-$(CONFIG_OLPC_EC) += olpc/ obj-$(CONFIG_GOLDFISH) += goldfish/ obj-$(CONFIG_CHROME_PLATFORMS) += chrome/ obj-$(CONFIG_SURFACE_PLATFORMS) += surface/ +obj-$(CONFIG_PLATFORM_OF_DRV) += of/ diff --git a/drivers/platform/of/Kconfig b/drivers/platform/of/Kconfig new file mode 100644 index 000000000000..a0b5a641a7c6 --- /dev/null +++ b/drivers/platform/of/Kconfig @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# X86 Platform Specific Drivers +# + +menuconfig PLATFORM_OF_DRV + tristate "Platform support via device tree" + select OF + select OF_FLATTREE + help + Say Y here to get to see options for board support that's initialized + via compiled-in flattened device trees. + + This is entirely independent from traditional DT-based booting (or DT + overlays) and meant for additional devices on non-OF-based (eg. APCI) + boards or composite devices behind probing-capable busses (eg. PCI). + + Instead of writing individual drivers for just the initialization of + subdevices, this option provides a generic mechanism for describing + these devices via device tree. + + If you say N, all options in this submenu will be skipped and disabled. + +if PLATFORM_OF_DRV + +config PLATFORM_OF_DRV_SYSFS_DTB + bool "Expose device tree binaries in sysfs" + default y + depends on SYSFS + help + Say Y here to enable exposing device tree binaries at /sys/firmware. + +config PLATFORM_OF_DRV_SYSFS_DT + bool "Expose parsed device tree in sysfs" + default y + depends on SYSFS + help + Say Y here to enable exposing device tree nodes at + /sys/firmware/devicetree. + +endif # PLATFORM_OF_DRV diff --git a/drivers/platform/of/Makefile b/drivers/platform/of/Makefile new file mode 100644 index 000000000000..84cf3003c500 --- /dev/null +++ b/drivers/platform/of/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +ofboard-y := init.o drv.o + +obj-$(CONFIG_PLATFORM_OF_DRV) += ofboard.o diff --git a/drivers/platform/of/drv.c b/drivers/platform/of/drv.c new file mode 100644 index 000000000000..ff7006c24cf7 --- /dev/null +++ b/drivers/platform/of/drv.c @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/* + * Copyright (C) 2021 metux IT consult + * Author: Enrico Weigelt + */ + +#include +#include +#include +#include +#include +#include + +#include "ofboard.h" + +static bool __init ofboard_match_dmi(struct device *dev) +{ +#ifdef CONFIG_DMI + const char *board = dmi_get_system_info(DMI_BOARD_NAME); + const char *vendor = dmi_get_system_info(DMI_SYS_VENDOR); + const struct device_node *node = dev->of_node; + + if (!of_match_string(node, "dmi-sys-vendor", vendor)) + return false; + + if (!of_match_string(node, "dmi-board-name", board)) + return false; + + dev_info(dev, "matched dmi: vendor=\"%s\" board=\"%s\"\n", vendor, + board); + + return true; +#else + return false; +#endif +} + +static void __init ofboard_kick_devs(struct device *dev, + struct device_node *np, + const char *bus_name) +{ + struct property *prop; + const char *walk; + struct bus_type *bus; + int ret; + + if (strcmp(bus_name, "name")==0) + return; + + bus = find_bus(bus_name); + if (!bus) { + dev_warn(dev, "cant find bus \"%s\"\n", bus_name); + return; + } + + of_property_for_each_string(np, bus_name, prop, walk) { + ret = bus_unregister_device_by_name(bus, walk); + if (ret) + dev_warn(dev, "failed removing device \"%s\" on bus " + "\"%s\": %d\n", walk, bus_name, ret); + else + dev_info(dev, "removed device \"%s\" from bus " + "\"%s\"\n", walk, bus_name); + } + + bus_put(bus); +} + +static void __init ofboard_unbind(struct device *dev) +{ + struct property *pr; + struct device_node *np = of_get_child_by_name(dev->of_node, "unbind"); + + if (!IS_ERR_OR_NULL(np)) + for_each_property_of_node(np, pr) + ofboard_kick_devs(dev, np, pr->name); +} + +static int ofboard_populate(struct device *dev) +{ + int ret; + struct device_node *of_node = dev->of_node; + struct device_node *np = of_get_child_by_name(of_node, "devices"); + + if (IS_ERR_OR_NULL(np)) { + dev_info(dev, "board oftree has no devices\n"); + return -ENOENT; + } + + ret = of_platform_populate(np, NULL, NULL, dev); + if (ret) { + dev_err(dev, "failed probing of childs: %d\n", ret); + return ret; + } + + return 0; +} + +static int ofboard_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + + if (!ofboard_match_dmi(dev)) + return -EINVAL; + + ofboard_unbind(&pdev->dev); + + return ofboard_populate(dev); +} + +static const struct of_device_id ofboard_of_match[] = { + { .compatible = "virtual,dmi-board" }, + {} +}; + +struct platform_driver ofboard_driver = { + .driver = { + .name = "ofboard", + .of_match_table = ofboard_of_match, + }, + .probe = ofboard_probe, +}; diff --git a/drivers/platform/of/init.c b/drivers/platform/of/init.c new file mode 100644 index 000000000000..3b8373cda77a --- /dev/null +++ b/drivers/platform/of/init.c @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0+ + +/* + * Copyright (C) 2021 metux IT consult + * Author: Enrico Weigelt + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include "ofboard.h" + +#define DECLARE_FDT_EXTERN(n) \ + extern char __dtb_##n##_begin[]; \ + static struct bin_attribute __dtb_##n##_attr = { \ + .attr = { .name = "fdt-" #n, .mode = S_IRUSR }, \ + .private = __dtb_##n##_begin, \ + .read = fdt_image_raw_read, \ + }; + +struct fdt_image { + char *begin; + size_t size; + char *basename; + struct bin_attribute *bin_attr; + struct device_node *root; +}; + +#define FDT_IMAGE_ENT(n) \ + { \ + .begin = __dtb_##n##_begin, \ + .bin_attr = &__dtb_##n##_attr, \ + .basename = "ofboard-" #n \ + } + +static ssize_t fdt_image_raw_read(struct file *filep, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) +{ + memcpy(buf, bin_attr->private + off, count); + return count; +} + +static struct fdt_image fdt[] = { +}; + +static int __init ofdrv_init_sysfs(struct fdt_image *image) +{ + image->bin_attr->size = image->size; + image->bin_attr->private = image->begin; + + if (sysfs_create_bin_file(firmware_kobj, image->bin_attr)) + pr_warn("failed creating sysfs bin_file\n"); + + of_attach_tree_sysfs(image->root, image->basename); + + return 0; +} + +static int __init ofdrv_parse_image(struct fdt_image *image) +{ + struct device_node* root; + void *new_fdt; + + image->size = fdt_totalsize(image->begin); + new_fdt = kmemdup(image->begin, image->size, GFP_KERNEL); + if (!new_fdt) + return -ENOMEM; + + image->begin = new_fdt; + of_fdt_unflatten_tree(new_fdt, NULL, &root); + + if (IS_ERR_OR_NULL(root)) + return PTR_ERR(root); + + image->root = root; + + return 0; +} + +static int __init ofdrv_init_image(struct fdt_image *image) +{ + struct device_node *np; + int ret; + + ret = ofdrv_parse_image(image); + if (ret) + return ret; + + ofdrv_init_sysfs(image); + + for_each_child_of_node(image->root, np) { + struct platform_device_info pdevinfo = { + .name = np->name, + .fwnode = &np->fwnode, + .id = PLATFORM_DEVID_NONE, + }; + platform_device_register_full(&pdevinfo); + } + + return 0; +} + +static int __init ofdrv_init(void) +{ + int x; + + platform_driver_register(&ofboard_driver); + + for (x=0; x"); +MODULE_DESCRIPTION("Generic oftree based initialization of custom board devices"); +MODULE_LICENSE("GPL"); diff --git a/drivers/platform/of/ofboard.h b/drivers/platform/of/ofboard.h new file mode 100644 index 000000000000..7516e5df4f18 --- /dev/null +++ b/drivers/platform/of/ofboard.h @@ -0,0 +1,8 @@ +#ifndef __DRIVERS_PLATFORM_OFBOARD_H +#define __DRIVERS_PLATFORM_OFBOARD_H + +#include + +extern struct platform_driver ofboard_driver; + +#endif