From mboxrd@z Thu Jan 1 00:00:00 1970 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=-17.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED 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 8D247C4338F for ; Fri, 23 Jul 2021 10:33:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 75CDB60F12 for ; Fri, 23 Jul 2021 10:33:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231660AbhGWJxE (ORCPT ); Fri, 23 Jul 2021 05:53:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23283 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230238AbhGWJw7 (ORCPT ); Fri, 23 Jul 2021 05:52:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627036412; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=IWXyBqJzTfNuRmfNKIdetBtP1RNy5LmtjYqPiHgsQrU=; b=cp48jOk+g7Ot3kFvLXWnTfzUIP7gMls5vnN5CY7Sd0Hf4X1644+kMvHBPWQ4MULZBuxMI8 HMuLH+hkREopDQALaylPKRX1/z2fcNWYH3HrG5oAQ2jbaSppI1CvbgObod7OEmD1/Y5mbi I2Ee0soSsPXLRRXPnQBeHlkpGnSmoZs= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-272-YAwn3BSjNEyi3XGa6fh5NQ-1; Fri, 23 Jul 2021 06:33:31 -0400 X-MC-Unique: YAwn3BSjNEyi3XGa6fh5NQ-1 Received: by mail-ed1-f70.google.com with SMTP id eg50-20020a05640228b2b02903a2e0d2acb7so493834edb.16 for ; Fri, 23 Jul 2021 03:33:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=IWXyBqJzTfNuRmfNKIdetBtP1RNy5LmtjYqPiHgsQrU=; b=f3BBcABIidfWA+YCWwJRgmVu8fLIqoMO1tsBufveMnOhGa/8DZTGPTECTXQcQvaRA3 0BrAArl3jdRyGC7KLWEawU3m3kSaDSPOl0dbadEabgwK2VgESb58pSvavOEQokMEu+Z3 cdMOWjb1xgJHAhc3pfYk0bLLLzEeRb6eVBePQPkEA4V6B5Xn3x0rgtUuPvo0/WZTqTOT 0UjvD2jr128yD0/rSgFLlJjSsUJuEhOFTAp+EnhYEk6lqILiuBRosy1C76NoewM/1Qqw v+M7zQZSDMcpvjIWwGI8Gt05/7m/RhJrsIHE2co2igJDoT9jDGfrMwx8GQnC4BrO8YvB Zi8w== X-Gm-Message-State: AOAM530bLa1iZdlWXqg65WZf+SEusZ68SJw5XfZXgdBHdaIaCAMC+4hu tUH6Okam1LYSnZMij5zQTGzg+R4Pm8g68sZihf7wLvC320qndaD7iOH0+9Gw67p769oPlRn4xP6 CqzHzeDjdBsBeU9H1vVui5w== X-Received: by 2002:aa7:d84a:: with SMTP id f10mr4786518eds.45.1627036410171; Fri, 23 Jul 2021 03:33:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzRTzuSNg37MjiloriLMkgLspNTzQ30700CgwYyTWYABO4p2OU/0d1DQ2yMLQ5IlbC1/5DVhA== X-Received: by 2002:aa7:d84a:: with SMTP id f10mr4786499eds.45.1627036409965; Fri, 23 Jul 2021 03:33:29 -0700 (PDT) Received: from redhat.com ([2.55.16.196]) by smtp.gmail.com with ESMTPSA id cq22sm13751399edb.77.2021.07.23.03.33.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jul 2021 03:33:29 -0700 (PDT) Date: Fri, 23 Jul 2021 06:33:24 -0400 From: "Michael S. Tsirkin" To: Viresh Kumar Cc: Jason Wang , Rob Herring , Arnd Bergmann , Jean-Philippe Brucker , Vincent Guittot , Bill Mills , Alex =?iso-8859-1?Q?Benn=E9e?= , "Enrico Weigelt, metux IT consult" , Jie Deng , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: Re: [PATCH V2.1 5/5] virtio: Bind virtio device to device-tree node Message-ID: <20210723062427-mutt-send-email-mst@kernel.org> References: <026ad5f274d64d46590623f9f3a04b8abfbe62d7.1626947324.git.viresh.kumar@linaro.org> <3606cdcc637682a3eb401d617e6e247431b78ec6.1627019436.git.viresh.kumar@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3606cdcc637682a3eb401d617e6e247431b78ec6.1627019436.git.viresh.kumar@linaro.org> Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org On Fri, Jul 23, 2021 at 11:41:31AM +0530, Viresh Kumar wrote: > 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. > > Signed-off-by: Viresh Kumar > --- > V2->V2.1 > - Remove list of virtio device and use of_device_is_compatible() instead. > > drivers/virtio/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 53 insertions(+), 3 deletions(-) > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > index 4b15c00c0a0a..7c56b3416895 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,42 @@ 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->dev.parent->of_node; dev_of_node? I think gcc will then be smart enough to make this a nop with !IS_ENABLED(CONFIG_OF). > + int ret, count; > + char compat[12]; I think this assumes device id is 16 bits but it's defined as u32 just in case. If it's ever extended we will then get into need to handle snprintf errors which is currently missing. To keep things simple we can do char compat[] = "virtio,XXXXXXXX"; > + > + 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; > + > + snprintf(compat, sizeof(compat), "virtio,%x", dev->id.device); worth checking this returns < sizeof(compat) and BUG_ON. > + > + 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 +383,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 +406,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); > -- > 2.31.1.272.g89b43f80a514