From patchwork Thu Feb 22 20:06:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Noralf_Tr=C3=B8nnes?= X-Patchwork-Id: 10236319 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4D66D60349 for ; Thu, 22 Feb 2018 20:15:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 43A3F28BFE for ; Thu, 22 Feb 2018 20:15:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 38A3128DAE; Thu, 22 Feb 2018 20:15:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B98D428BFE for ; Thu, 22 Feb 2018 20:15:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 923526ED4A; Thu, 22 Feb 2018 20:15:55 +0000 (UTC) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from smtp.domeneshop.no (smtp.domeneshop.no [IPv6:2a01:5b40:0:3005::1]) by gabe.freedesktop.org (Postfix) with ESMTPS id 15BF86ECA0 for ; Thu, 22 Feb 2018 20:15:54 +0000 (UTC) Received: from 211.81-166-168.customer.lyse.net ([81.166.168.211]:34576 helo=localhost.localdomain) by smtp.domeneshop.no with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1eox92-0004nQ-JL; Thu, 22 Feb 2018 21:07:08 +0100 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= To: dri-devel@lists.freedesktop.org Date: Thu, 22 Feb 2018 21:06:47 +0100 Message-Id: <20180222200653.19453-7-noralf@tronnes.org> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180222200653.19453-1-noralf@tronnes.org> References: <20180222200653.19453-1-noralf@tronnes.org> MIME-Version: 1.0 Subject: [Intel-gfx] [RFC v3 06/12] drm: Add DRM device iterator X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: daniel.vetter@ffwll.ch, intel-gfx@lists.freedesktop.org, =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= , laurent.pinchart@ideasonboard.com, dh.herrmann@gmail.com Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP Add functions for iterating the registered DRM devices. This is done looping through the primary minors instead of using an iter on drm_class which is also a possibility. The reason is that drm_minor_acquire() takes a ref on the drm_device which is needed. Another option would be to add a separate drm_device list. Signed-off-by: Noralf Trønnes --- Does anyone know how I can make checkpatch happy, I've tried parentheses around both dev and iter: -:129: ERROR: Macros with complex values should be enclosed in parentheses #129: FILE: include/drm/drm_drv.h:679: +#define drm_for_each_device_iter(dev, iter) \ + while ((dev = drm_device_list_iter_next(iter))) drivers/gpu/drm/drm_drv.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++ include/drm/drm_drv.h | 34 +++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c index 9acc1e157813..f869de185986 100644 --- a/drivers/gpu/drm/drm_drv.c +++ b/drivers/gpu/drm/drm_drv.c @@ -862,6 +862,66 @@ int drm_dev_set_unique(struct drm_device *dev, const char *name) } EXPORT_SYMBOL(drm_dev_set_unique); +/** + * drm_device_list_iter_begin - Initialize a DRM device iterator + * @iter: DRM device iterator + * + * Sets @iter up to walk the registered DRM devices. @iter must always be + * cleaned up again by calling drm_device_list_iter_end(). Iteration itself + * happens using drm_device_list_iter_next() or drm_for_each_device_iter(). + */ +void drm_device_list_iter_begin(struct drm_device_list_iter *iter) +{ + iter->dev = NULL; + iter->minor_id = 0; +} +EXPORT_SYMBOL(drm_device_list_iter_begin); + +/** + * drm_device_list_iter_next - Return the next DRM device + * @iter: DRM device iterator + * + * Returns the next DRM device for @iter, or NULL when there are no more + * devices. + */ +struct drm_device * +drm_device_list_iter_next(struct drm_device_list_iter *iter) +{ + struct drm_minor *minor; + + drm_dev_put(iter->dev); + iter->dev = NULL; + + /* Loop through the primary minors */ + for (; iter->minor_id < 64; iter->minor_id++) { + minor = drm_minor_acquire(iter->minor_id); + if (IS_ERR(minor)) + continue; + + iter->dev = minor->dev; + iter->minor_id++; + return minor->dev; + } + + return NULL; +} +EXPORT_SYMBOL(drm_device_list_iter_next); + +/** + * drm_device_list_iter_end - Tear down a DRM device iterator + * @iter: DRM device iterator + * + * Tears down @iter and releases any resources (like &drm_device references) + * acquired while walking the devices. This must always be called, both when + * the iteration completes fully or when it was aborted without walking the + * entire list. + */ +void drm_device_list_iter_end(struct drm_device_list_iter *iter) +{ + drm_dev_put(iter->dev); +} +EXPORT_SYMBOL(drm_device_list_iter_end); + /* * DRM Core * The DRM core module initializes all global DRM objects and makes them diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index d32b688eb346..313a23ee7b30 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -644,5 +644,39 @@ static inline int drm_dev_is_unplugged(struct drm_device *dev) int drm_dev_set_unique(struct drm_device *dev, const char *name); +/** + * struct drm_device_list_iter - DRM device iterator + * + * This iterator tracks state needed to be able to walk the registered + * DRM devices. Only use together with drm_device_list_iter_begin(), + * drm_device_list_iter_end() and drm_device_list_iter_next() respectively + * the convenience macro drm_for_each_device_iter(). + */ +struct drm_device_list_iter { +/* private: */ + unsigned int minor_id; + struct drm_device *dev; +}; + +void drm_device_list_iter_begin(struct drm_device_list_iter *iter); +struct drm_device * +drm_device_list_iter_next(struct drm_device_list_iter *iter); +void drm_device_list_iter_end(struct drm_device_list_iter *iter); + +/** + * drm_for_each_device_iter - DRM device iterator macro + * @dev: DRM device pointer used as cursor + * @iter: DRM device iterator + * + * Note that @dev is only valid within the list body, if you want to use @dev + * after calling drm_device_list_iter_end() then you need to grab your own + * reference first using drm_dev_get(). + * + * Note: + * The DRM device was registered at the point when the reference was taken, + * but it's not guaranteed that this is still the case inside the loop. + */ +#define drm_for_each_device_iter(dev, iter) \ + while ((dev = drm_device_list_iter_next(iter))) #endif