From patchwork Wed Jun 17 13:42:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomeu Vizoso X-Patchwork-Id: 6626311 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 6F05E9F1C1 for ; Wed, 17 Jun 2015 13:52:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5ED29205E6 for ; Wed, 17 Jun 2015 13:52:18 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 94357204AD for ; Wed, 17 Jun 2015 13:52:16 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Z5Di0-0007wV-0q; Wed, 17 Jun 2015 13:48:52 +0000 Received: from mail-wi0-x232.google.com ([2a00:1450:400c:c05::232]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Z5Dcz-00033t-Ob for linux-arm-kernel@lists.infradead.org; Wed, 17 Jun 2015 13:43:43 +0000 Received: by wicnd19 with SMTP id nd19so29548389wic.1 for ; Wed, 17 Jun 2015 06:43:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=LfzQBCkxdX6txLP9YccDH825aqxFlIXFH9kRwFUAh68=; b=I9JReclDPXM+eeF2SJgsOy4nDBzFI/TXF7BzcBkoJSl/k1Veb3yS03bbsPpdvQf367 IjImobCz1bNy3BG8TNBYNv4+oDfqz7tkQJjteN/27nXnvrPZlT1eJ96W8fUieeh36KXC Zi0Kzz7D+8vv4jcZQBKVfD9j/4z14CxBOr4gOskavH6WQEOctOPAFT16oFj0s9n/titm KTRJJVMVrv073Y0s7oFllmz6jsqejVwyWZuFVJJpOR1e2ttdkkiJoPcySRyesJpSuQIU 4Sep++ApuT6bZXLUHHJLU5wWJ9iSw2+a9NUnmeXxpseLNqQhGwFpFGlvtYpjD9Z2kY9h 6Z+A== X-Received: by 10.180.101.138 with SMTP id fg10mr17993434wib.46.1434548602648; Wed, 17 Jun 2015 06:43:22 -0700 (PDT) Received: from cizrna.lan ([109.72.12.132]) by mx.google.com with ESMTPSA id v3sm6920361wja.31.2015.06.17.06.43.20 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Jun 2015 06:43:21 -0700 (PDT) From: Tomeu Vizoso To: linux-arm-kernel@lists.infradead.org Subject: [PATCH 13/13] driver-core: probe dependencies before probing Date: Wed, 17 Jun 2015 15:42:23 +0200 Message-Id: <1434548543-22949-14-git-send-email-tomeu.vizoso@collabora.com> X-Mailer: git-send-email 2.4.1 In-Reply-To: <1434548543-22949-1-git-send-email-tomeu.vizoso@collabora.com> References: <1434548543-22949-1-git-send-email-tomeu.vizoso@collabora.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150617_064342_193982_96362FD1 X-CRM114-Status: GOOD ( 17.83 ) X-Spam-Score: -0.7 (/) Cc: Mark Rutland , Krzysztof Kozlowski , Linus Walleij , Andrzej Hajda , Thierry Reding , Lv Zheng , Alexander Holler , Alexandre Courbot , Russell King , Pawel Moll , Stephen Warren , Robert Moore , Grant Likely , Len Brown , Arnd Bergmann , Ian Campbell , Rob Herring , =?UTF-8?q?Terje=20Bergstr=C3=B6m?= , Tomeu Vizoso , Greg Kroah-Hartman , Dmitry Torokhov , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, Mark Brown , Kumar Gala , Javier Martinez Canillas X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Before actually probing a device, find out what dependencies it has and do our best to ensure that they are available at this point. This is accomplished by finding out what platform devices need to be probed so the dependencies are available. If any dependencies are still unavailable after that (most probably a missing driver or an error in the HW description from the firmware), we print a nice error message so that people don't have to add a zillion of printks to find out why a device asked for its probe to be deferred. Dependencies are discovered with the help of the subsystems that already implement the firmware bindings that specify the naming scheme of properties that point to other nodes, via class.get_deps(). Currently the dependencies list is discarded but it could be stored for later usage. Signed-off-by: Tomeu Vizoso --- drivers/base/dd.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 18438aa..8a64a29 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -25,6 +25,9 @@ #include #include #include +#include +#include +#include #include "base.h" #include "power/power.h" @@ -54,6 +57,121 @@ static LIST_HEAD(deferred_probe_active_list); static struct workqueue_struct *deferred_wq; static atomic_t deferred_trigger_count = ATOMIC_INIT(0); +static bool device_is_bound(struct device *dev) +{ + return klist_node_attached(&dev->p->knode_driver); +} + +static int fwnode_match(struct device *dev, void *data) +{ + return dev->fwnode == data; +} + +static bool fwnode_is_bound(struct fwnode_handle *fwnode) +{ + struct device *dev; + + dev = bus_find_device(&platform_bus_type, NULL, fwnode, fwnode_match); + + /* Check whether device is bound or is being probed right now */ + return dev ? dev->driver : false; +} + +static struct fwnode_handle *get_enclosing_platform_dev( + struct fwnode_handle *fwnode) +{ + struct fwnode_handle *iter, *node = NULL; + + for (iter = fwnode; + iter && fwnode_get_parent(iter); + iter = fwnode_get_parent(iter)) { + struct fwnode_handle *parent = fwnode_get_parent(iter); + + /* Only nodes with the compatible property can be probed */ + if (fwnode_property_present(iter, "compatible")) + node = iter; + + /* Stop just below root or if the parent is bound already */ + if (!fwnode_get_parent(parent) || + fwnode_is_bound(parent)) + break; + } + + return node; +} + +static bool check_dependency(struct fwnode_handle *fwnode) +{ + struct fwnode_handle *target; + struct device *dev; + + if (!fwnode) + return true; + + target = get_enclosing_platform_dev(fwnode); + if (!target) + return true; + + dev = bus_find_device(&platform_bus_type, NULL, target, fwnode_match); + if (!dev) { + pr_debug("Couldn't find device for %s\n", + fwnode_get_name(fwnode)); + return false; + } + + /* + * Device is bound or is being probed right now. If we have bad luck + * and the dependency isn't ready when it's needed, deferred probe + * will save us. + */ + if (dev->driver) + return true; + + bus_probe_device(dev); + + /* If the dependency hasn't finished probing, we'll want a warning */ + return device_is_bound(dev); +} + +static void check_dependencies_per_class(struct class *class, void *data) +{ + struct fwnode_handle *fwnode = data; + struct list_head *deps; + struct fwnode_dependency *dep, *tmp; + + if (!class->get_dependencies) + return; + + deps = class->get_dependencies(fwnode); + if (!deps) + return; + + list_for_each_entry_safe(dep, tmp, deps, dependency) { + if (!check_dependency(dep->fwnode)) + pr_debug("Dependency '%s' not available\n", + fwnode_get_name(dep->fwnode)); + + list_del(&dep->dependency); + kfree(dep); + } + + kfree(deps); +} + +static void check_dependencies(struct device *dev) +{ + if (dev->parent && !check_dependency(dev->parent->fwnode)) + pr_debug("Parent '%s' of device '%s' not available\n", + dev_name(dev->parent), dev_name(dev)); + + if (!dev->fwnode) { + pr_debug("Device '%s' doesn't have a fwnode\n", dev_name(dev)); + return; + } + + for_each_class(check_dependencies_per_class, dev->fwnode); +} + /* * deferred_probe_work_func() - Retry probing devices in the active list. */ @@ -413,6 +531,8 @@ int driver_probe_device(struct device_driver *drv, struct device *dev) return 0; } + check_dependencies(dev); + pr_debug("bus: '%s': %s: matched device %s with driver %s\n", drv->bus->name, __func__, dev_name(dev), drv->name);