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@xxxxxxxxxxx> --- 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@xxxxxxxxxxxxxxxxxxxxx https://lists.freedesktop.org/mailman/listinfo/dri-devel