Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Noralf Trønnes" <noralf@tronnes.org>
To: dri-devel@lists.freedesktop.org
Cc: daniel.vetter@ffwll.ch, intel-gfx@lists.freedesktop.org,
	laurent.pinchart@ideasonboard.com
Subject: [RFC v3 06/12] drm: Add DRM device iterator
Date: Thu, 22 Feb 2018 21:06:47 +0100	[thread overview]
Message-ID: <20180222200653.19453-7-noralf@tronnes.org> (raw)
In-Reply-To: <20180222200653.19453-1-noralf@tronnes.org>

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 <noralf@tronnes.org>
---

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
-- 
2.15.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2018-02-22 20:06 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-22 20:06 [RFC v3 00/12] drm: Add generic fbdev emulation Noralf Trønnes
2018-02-22 20:06 ` [RFC v3 01/12] drm: provide management functions for drm_file Noralf Trønnes
2018-02-22 20:06 ` [RFC v3 02/12] drm/file: Don't set master on in-kernel clients Noralf Trønnes
2018-03-06  8:28   ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 03/12] drm: Make ioctls available for in-kernel clients part 1 Noralf Trønnes
2018-02-22 20:06 ` [RFC v3 04/12] drm: Make ioctls available for in-kernel clients part 2 Noralf Trønnes
2018-03-06  8:41   ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 05/12] drm: Add _ioctl suffix to some functions Noralf Trønnes
2018-02-22 20:06 ` Noralf Trønnes [this message]
2018-02-22 20:06 ` [RFC v3 07/12] drm/modes: Add drm_umode_equal() Noralf Trønnes
2018-03-06  8:42   ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 08/12] drm/framebuffer: Add drm_mode_can_dirtyfb() Noralf Trønnes
2018-03-06  8:45   ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 09/12] drm: Add API for in-kernel clients Noralf Trønnes
2018-03-06  8:56   ` Daniel Vetter
2018-03-08 17:12     ` Noralf Trønnes
2018-03-12 16:51       ` Daniel Vetter
2018-03-12 20:21         ` Noralf Trønnes
2018-03-13 15:11           ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 10/12] drm/client: Add fbdev emulation client Noralf Trønnes
2018-03-06  9:06   ` Daniel Vetter
2018-02-22 20:06 ` [RFC v3 11/12] drm/client: Add bootsplash client Noralf Trønnes
2018-03-06  9:12   ` Daniel Vetter
2018-03-06 15:21     ` Max Staudt
2018-02-22 20:06 ` [RFC v3 12/12] drm/client: Add VT console client Noralf Trønnes
2018-02-22 21:03 ` ✗ Fi.CI.BAT: failure for drm: Add generic fbdev emulation (rev3) Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180222200653.19453-7-noralf@tronnes.org \
    --to=noralf@tronnes.org \
    --cc=daniel.vetter@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=laurent.pinchart@ideasonboard.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox