From patchwork Tue Jul 27 05:23:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 486663 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp4197834jao; Mon, 26 Jul 2021 22:24:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwyJ1pOFF0Jg9342jmmZbf/IlZKD419X749epQjJt+WdwzOjI2VwyB6/zeOKAQAtAHDogh2 X-Received: by 2002:a05:6602:584:: with SMTP id v4mr17492990iox.181.1627363484079; Mon, 26 Jul 2021 22:24:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627363484; cv=none; d=google.com; s=arc-20160816; b=JVC2TuDLkvVePGxEWSU4d7Lw2ykcWGoMoM8bAoybvpP60vu8laq6J7v1kZ9F+EflQR i13FZeHWtgk2z+mS48D43k6D/7YIrZGftpUU9gx2iO0l+hkEeuTwsxNHirFu1KRS/qsf /vgguM9F1xkd4Gkf5h8el/3//FsgEMHBYgtrE0EQ/w1F8LraN8hb4mm2IuDyQ9ER5Yp3 e4A9XHAXidXULxBD6CcXFFpWFnhx19jkzhx2OJooYniZNxnhzlsDlAkHNLoHVY6kccG5 DEG6A3JfRlYzbgDKxTohPAwWJTlc6Sz1S0PeD9fyE8MvQGXY4fwPkwz5kbHvXAu3MqVb zMSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2bReu9/QiDlixJ/zUrMpF+Tdh8nXeZ2MTWODegeEltE=; b=nC+Ti64feme4+SsZhYi0ggpZyLW6WdRBUF/cvfO4XxY0F4EEK4uahuBoYOiMHwOKRr 49mumQLH1KIUfk5ccPjIxx8Xl3yRPMaWCTKc6Id2DQq4GHrz1HRC2sC0hL621ksoB5tu 0BAbApXHmg9Rv9f5nDuGxDtZYYstSxEuQIfxgS6Bm2Cx14093MVfWrYs/dIqy3Be7hUI yWHBQeVgNI5voeWd0PjNr6dXDVMSO7M20mfcr2IIDXN798RDQMi9syydAimyamahy8Zq dqK87Gwu+VzfjMDQfftgwhMrYmFIlle0TsQsX6HnG2pifuE6gwLZHW17TiOzffYu6tKF p17A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sguymNCU; spf=pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m19si2419395jat.15.2021.07.26.22.24.43; Mon, 26 Jul 2021 22:24:44 -0700 (PDT) Received-SPF: pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sguymNCU; spf=pass (google.com: domain of devicetree-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=devicetree-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235281AbhG0FYm (ORCPT + 7 others); Tue, 27 Jul 2021 01:24:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235365AbhG0FYd (ORCPT ); Tue, 27 Jul 2021 01:24:33 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52928C061796 for ; Mon, 26 Jul 2021 22:24:34 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id f13so3988103plj.2 for ; Mon, 26 Jul 2021 22:24:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2bReu9/QiDlixJ/zUrMpF+Tdh8nXeZ2MTWODegeEltE=; b=sguymNCUwJvY2Vm/IznufncDBF3sjETSlEYR+vj6oJ+UM67MLBhMqFWHk4mxJ2PsSK 5b/s7cah1FqzNg7kC19ikh307/G6HXGV6vMozb6gQ9Ivrinas5A5uGFE+gYR4y+sp8CP ooRPiSyF+J/ZHTpxuhxzjHDtCtY6Ig6pMCAsZA77tabFVO9u5bZFxnaz0DMP43yxUwc0 BsPvx34ZEDZ4sn9IAvNFCcHSOMaiylMSvxoU6OsQqf64kvFfB6/cJZ/Wc6nZS8In21fJ Il3PEhvnL7Rv74YUjSqCJQU+77wRAcE34sRn7mrHM27QxxrNB5RZ77Q1OyKTtOWvxq+T z91A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2bReu9/QiDlixJ/zUrMpF+Tdh8nXeZ2MTWODegeEltE=; b=tP9yYr4v4sKT2LcUy8HX1erCAEVDBCUDuasLrhbeHN6ohHUap/UixJARiM+yCpL/co 7M/UhDLRrYsi9fgbgM4fcaNJKxiWDVCR7l/6eYYWa1usmfHqEPjvKOHhbW8axIZzBW9t cCM5ZPMAocYMw5q9s61ko5Qo1eT+/hOf1KGjaF77radEw4MHQRIWvMb7+54C5zvg6KPn TmBjGVqy6UMlvKaNAk1ab4yfKl0EXLhQ82ObQukMI9t3MuSCO3A1NxGiR7yRh+v/RHNa 1mUhjg4Wg6HwsIEsFKJlOunN5jEUz6fzDUdp1aDW1MMapdj9rkkc+C7F9XMf50/AZ1KF EPDQ== X-Gm-Message-State: AOAM532tfosjyZgkBbS+JpZfHhmP+NQYE1t33yItGKw6YtP0D0ZRIWHa MxSViSfudrcs5WY2ofJttwuQ/g== X-Received: by 2002:a17:90a:86:: with SMTP id a6mr2447703pja.133.1627363473907; Mon, 26 Jul 2021 22:24:33 -0700 (PDT) Received: from localhost ([122.172.201.85]) by smtp.gmail.com with ESMTPSA id b9sm1857444pfm.124.2021.07.26.22.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 22:24:28 -0700 (PDT) From: Viresh Kumar To: Jason Wang , "Michael S. Tsirkin" , Rob Herring , Arnd Bergmann , Jean-Philippe Brucker Cc: Viresh Kumar , Vincent Guittot , Bill Mills , =?utf-8?q?Alex_Benn=C3=A9e?= , "Enrico Weigelt, metux IT consult" , Jie Deng , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, Arnd Bergmann Subject: [PATCH V4 5/5] virtio: Bind virtio device to device-tree node Date: Tue, 27 Jul 2021 10:53:52 +0530 Message-Id: <94c12705602929968477aaf27e02439eb7a7f253.1627362340.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.31.1.272.g89b43f80a514 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org Bind the virtio devices with their of_node. This will help users of the virtio devices to mention their dependencies on the device in the DT itself. Like GPIO pin users can use the phandle of the device node, or the node may contain more subnodes to add i2c or spi eeproms and other users. Reviewed-by: Arnd Bergmann Signed-off-by: Viresh Kumar --- drivers/virtio/virtio.c | 57 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) -- 2.31.1.272.g89b43f80a514 diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 4b15c00c0a0a..5f80786c2aa2 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -4,6 +4,7 @@ #include #include #include +#include #include /* Unique numbering for virtio devices. */ @@ -292,6 +293,9 @@ static int virtio_dev_remove(struct device *_d) /* Acknowledge the device's existence again. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + of_node_put(dev->dev.of_node); + return 0; } @@ -319,6 +323,43 @@ void unregister_virtio_driver(struct virtio_driver *driver) } EXPORT_SYMBOL_GPL(unregister_virtio_driver); +static int virtio_device_of_init(struct virtio_device *dev) +{ + struct device_node *np, *pnode = dev_of_node(dev->dev.parent); + char compat[] = "virtio,deviceXXXXXXXX"; + int ret, count; + + if (!pnode) + return 0; + + count = of_get_available_child_count(pnode); + if (!count) + return 0; + + /* There can be only 1 child node */ + if (WARN_ON(count > 1)) + return -EINVAL; + + np = of_get_next_available_child(pnode, NULL); + if (WARN_ON(!np)) + return -ENODEV; + + ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device); + BUG_ON(ret >= sizeof(compat)); + + if (!of_device_is_compatible(np, compat)) { + ret = -EINVAL; + goto out; + } + + dev->dev.of_node = np; + return 0; + +out: + of_node_put(np); + return ret; +} + /** * register_virtio_device - register virtio device * @dev : virtio device to be registered @@ -343,6 +384,10 @@ int register_virtio_device(struct virtio_device *dev) dev->index = err; dev_set_name(&dev->dev, "virtio%u", dev->index); + err = virtio_device_of_init(dev); + if (err) + goto out_ida_remove; + spin_lock_init(&dev->config_lock); dev->config_enabled = false; dev->config_change_pending = false; @@ -362,10 +407,16 @@ int register_virtio_device(struct virtio_device *dev) */ err = device_add(&dev->dev); if (err) - ida_simple_remove(&virtio_index_ida, dev->index); + goto out_of_node_put; + + return 0; + +out_of_node_put: + of_node_put(dev->dev.of_node); +out_ida_remove: + ida_simple_remove(&virtio_index_ida, dev->index); out: - if (err) - virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); return err; } EXPORT_SYMBOL_GPL(register_virtio_device);