Might as well embed drm_device since tinydrm_device (embeds pipe struct and fbdev pointer) needs to stick around after driver-device unbind to handle open fd's after device removal. tinydrm_release() will be expanded in a later patch. Cc: David Lechner <david@xxxxxxxxxxxxxx> Signed-off-by: Noralf Trønnes <noralf@xxxxxxxxxxx> --- Changes since version 1: - Add drm_driver.release callbacks - Free structure if devm_tinydrm_init() fails. - Probe message has been removed in the previous patch, so no need to fix up those variables. drivers/gpu/drm/tinydrm/core/tinydrm-core.c | 61 +++++++++++++++++------------ drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c | 2 +- drivers/gpu/drm/tinydrm/mi0283qt.c | 9 +++-- drivers/gpu/drm/tinydrm/mipi-dbi.c | 30 +++++++++++--- drivers/gpu/drm/tinydrm/repaper.c | 20 ++++++++-- drivers/gpu/drm/tinydrm/st7586.c | 17 ++++---- include/drm/tinydrm/mipi-dbi.h | 1 + include/drm/tinydrm/tinydrm.h | 10 ++++- 8 files changed, 102 insertions(+), 48 deletions(-) diff --git a/drivers/gpu/drm/tinydrm/core/tinydrm-core.c b/drivers/gpu/drm/tinydrm/core/tinydrm-core.c index 551709e..c8ae2e9 100644 --- a/drivers/gpu/drm/tinydrm/core/tinydrm-core.c +++ b/drivers/gpu/drm/tinydrm/core/tinydrm-core.c @@ -44,7 +44,7 @@ */ void tinydrm_lastclose(struct drm_device *drm) { - struct tinydrm_device *tdev = drm->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(drm); DRM_DEBUG_KMS("\n"); drm_fbdev_cma_restore_mode(tdev->fbdev_cma); @@ -126,7 +126,7 @@ static struct drm_framebuffer * tinydrm_fb_create(struct drm_device *drm, struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd) { - struct tinydrm_device *tdev = drm->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(drm); return drm_fb_cma_create_with_funcs(drm, file_priv, mode_cmd, tdev->fb_funcs); @@ -138,27 +138,37 @@ static const struct drm_mode_config_funcs tinydrm_mode_config_funcs = { .atomic_commit = drm_atomic_helper_commit, }; +/** + * tinydrm_release - DRM driver release helper + * @drm: DRM device + * + * This function finalizes &drm_device. The caller is responsible for freeing + * &tinydrm_device. + * + * Drivers must use this in their &drm_driver->release callback. + */ +void tinydrm_release(struct drm_device *drm) +{ + DRM_DEBUG_DRIVER("\n"); + + drm_dev_fini(drm); +} +EXPORT_SYMBOL(tinydrm_release); + static int tinydrm_init(struct device *parent, struct tinydrm_device *tdev, const struct drm_framebuffer_funcs *fb_funcs, struct drm_driver *driver) { - struct drm_device *drm; + struct drm_device *drm = &tdev->drm; + int ret; mutex_init(&tdev->dirty_lock); tdev->fb_funcs = fb_funcs; - /* - * We don't embed drm_device, because that prevent us from using - * devm_kzalloc() to allocate tinydrm_device in the driver since - * drm_dev_unref() frees the structure. The devm_ functions provide - * for easy error handling. - */ - drm = drm_dev_alloc(driver, parent); - if (IS_ERR(drm)) - return PTR_ERR(drm); - - tdev->drm = drm; - drm->dev_private = tdev; + ret = drm_dev_init(drm, driver, parent); + if (ret) + return ret; + drm_mode_config_init(drm); drm->mode_config.funcs = &tinydrm_mode_config_funcs; @@ -167,10 +177,9 @@ static int tinydrm_init(struct device *parent, struct tinydrm_device *tdev, static void tinydrm_fini(struct tinydrm_device *tdev) { - drm_mode_config_cleanup(tdev->drm); + drm_mode_config_cleanup(&tdev->drm); mutex_destroy(&tdev->dirty_lock); - tdev->drm->dev_private = NULL; - drm_dev_unref(tdev->drm); + drm_dev_unref(&tdev->drm); } static void devm_tinydrm_release(void *data) @@ -212,12 +221,12 @@ EXPORT_SYMBOL(devm_tinydrm_init); static int tinydrm_register(struct tinydrm_device *tdev) { - struct drm_device *drm = tdev->drm; + struct drm_device *drm = &tdev->drm; int bpp = drm->mode_config.preferred_depth; struct drm_fbdev_cma *fbdev; int ret; - ret = drm_dev_register(tdev->drm, 0); + ret = drm_dev_register(drm, 0); if (ret) return ret; @@ -236,10 +245,10 @@ static void tinydrm_unregister(struct tinydrm_device *tdev) { struct drm_fbdev_cma *fbdev_cma = tdev->fbdev_cma; - drm_atomic_helper_shutdown(tdev->drm); + drm_atomic_helper_shutdown(&tdev->drm); /* don't restore fbdev in lastclose, keep pipeline disabled */ tdev->fbdev_cma = NULL; - drm_dev_unregister(tdev->drm); + drm_dev_unregister(&tdev->drm); if (fbdev_cma) drm_fbdev_cma_fini(fbdev_cma); } @@ -262,7 +271,7 @@ static void devm_tinydrm_register_release(void *data) */ int devm_tinydrm_register(struct tinydrm_device *tdev) { - struct device *dev = tdev->drm->dev; + struct device *dev = tdev->drm.dev; int ret; ret = tinydrm_register(tdev); @@ -287,7 +296,7 @@ EXPORT_SYMBOL(devm_tinydrm_register); */ void tinydrm_shutdown(struct tinydrm_device *tdev) { - drm_atomic_helper_shutdown(tdev->drm); + drm_atomic_helper_shutdown(&tdev->drm); } EXPORT_SYMBOL(tinydrm_shutdown); @@ -312,7 +321,7 @@ int tinydrm_suspend(struct tinydrm_device *tdev) } drm_fbdev_cma_set_suspend_unlocked(tdev->fbdev_cma, 1); - state = drm_atomic_helper_suspend(tdev->drm); + state = drm_atomic_helper_suspend(&tdev->drm); if (IS_ERR(state)) { drm_fbdev_cma_set_suspend_unlocked(tdev->fbdev_cma, 0); return PTR_ERR(state); @@ -346,7 +355,7 @@ int tinydrm_resume(struct tinydrm_device *tdev) tdev->suspend_state = NULL; - ret = drm_atomic_helper_resume(tdev->drm, state); + ret = drm_atomic_helper_resume(&tdev->drm, state); if (ret) { DRM_ERROR("Error resuming state: %d\n", ret); return ret; diff --git a/drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c b/drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c index 177e9d8..1bcb43a 100644 --- a/drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c +++ b/drivers/gpu/drm/tinydrm/core/tinydrm-pipe.c @@ -198,7 +198,7 @@ tinydrm_display_pipe_init(struct tinydrm_device *tdev, const struct drm_display_mode *mode, unsigned int rotation) { - struct drm_device *drm = tdev->drm; + struct drm_device *drm = &tdev->drm; struct drm_display_mode *mode_copy; struct drm_connector *connector; int ret; diff --git a/drivers/gpu/drm/tinydrm/mi0283qt.c b/drivers/gpu/drm/tinydrm/mi0283qt.c index 7fd2691..d61f987 100644 --- a/drivers/gpu/drm/tinydrm/mi0283qt.c +++ b/drivers/gpu/drm/tinydrm/mi0283qt.c @@ -23,7 +23,7 @@ static int mi0283qt_init(struct mipi_dbi *mipi) { struct tinydrm_device *tdev = &mipi->tinydrm; - struct device *dev = tdev->drm->dev; + struct device *dev = tdev->drm.dev; u8 addr_mode; int ret; @@ -139,6 +139,7 @@ static struct drm_driver mi0283qt_driver = { DRIVER_ATOMIC, .fops = &mi0283qt_fops, TINYDRM_GEM_DRIVER_OPS, + .release = mipi_dbi_release, .lastclose = tinydrm_lastclose, .debugfs_init = mipi_dbi_debugfs_init, .name = "mi0283qt", @@ -168,7 +169,7 @@ static int mi0283qt_probe(struct spi_device *spi) u32 rotation = 0; int ret; - mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL); + mipi = kzalloc(sizeof(*mipi), GFP_KERNEL); if (!mipi) return -ENOMEM; @@ -200,8 +201,10 @@ static int mi0283qt_probe(struct spi_device *spi) ret = mipi_dbi_init(&spi->dev, mipi, &mi0283qt_pipe_funcs, &mi0283qt_driver, &mi0283qt_mode, rotation); - if (ret) + if (ret) { + kfree(mipi); return ret; + } ret = mi0283qt_init(mipi); if (ret) diff --git a/drivers/gpu/drm/tinydrm/mipi-dbi.c b/drivers/gpu/drm/tinydrm/mipi-dbi.c index f0dedc2..ec8f001 100644 --- a/drivers/gpu/drm/tinydrm/mipi-dbi.c +++ b/drivers/gpu/drm/tinydrm/mipi-dbi.c @@ -199,7 +199,7 @@ static int mipi_dbi_fb_dirty(struct drm_framebuffer *fb, unsigned int num_clips) { struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0); - struct tinydrm_device *tdev = fb->dev->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(fb->dev); struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); bool swap = mipi->swap_bytes; struct drm_clip_rect clip; @@ -285,7 +285,7 @@ EXPORT_SYMBOL(mipi_dbi_pipe_enable); static void mipi_dbi_blank(struct mipi_dbi *mipi) { - struct drm_device *drm = mipi->tinydrm.drm; + struct drm_device *drm = &mipi->tinydrm.drm; u16 height = drm->mode_config.min_height; u16 width = drm->mode_config.min_width; size_t len = width * height * 2; @@ -324,6 +324,24 @@ void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe) } EXPORT_SYMBOL(mipi_dbi_pipe_disable); +/** + * mipi_dbi_release - DRM driver release helper + * @drm: DRM device + * + * This function finalizes and frees &mipi_dbi. + * + * Drivers can use this as their &drm_driver->release callback. + */ +void mipi_dbi_release(struct drm_device *drm) +{ + struct tinydrm_device *tdev = drm_to_tinydrm(drm); + struct mipi_dbi *dbi = mipi_dbi_from_tinydrm(tdev); + + tinydrm_release(drm); + kfree(dbi); +} +EXPORT_SYMBOL(mipi_dbi_release); + static const uint32_t mipi_dbi_formats[] = { DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888, @@ -380,13 +398,13 @@ int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi, if (ret) return ret; - tdev->drm->mode_config.preferred_depth = 16; + tdev->drm.mode_config.preferred_depth = 16; mipi->rotation = rotation; - drm_mode_config_reset(tdev->drm); + drm_mode_config_reset(&tdev->drm); DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n", - tdev->drm->mode_config.preferred_depth, rotation); + tdev->drm.mode_config.preferred_depth, rotation); return 0; } @@ -977,7 +995,7 @@ static const struct drm_info_list mipi_dbi_debugfs_list[] = { */ int mipi_dbi_debugfs_init(struct drm_minor *minor) { - struct tinydrm_device *tdev = minor->dev->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(minor->dev); struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); umode_t mode = S_IFREG | S_IWUSR; diff --git a/drivers/gpu/drm/tinydrm/repaper.c b/drivers/gpu/drm/tinydrm/repaper.c index 5fbe147..9236efb 100644 --- a/drivers/gpu/drm/tinydrm/repaper.c +++ b/drivers/gpu/drm/tinydrm/repaper.c @@ -528,7 +528,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb, { struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0); struct dma_buf_attachment *import_attach = cma_obj->base.import_attach; - struct tinydrm_device *tdev = fb->dev->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(fb->dev); struct repaper_epd *epd = epd_from_tinydrm(tdev); struct drm_clip_rect clip; u8 *buf = NULL; @@ -855,6 +855,15 @@ static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = { .prepare_fb = tinydrm_display_pipe_prepare_fb, }; +static void repaper_release(struct drm_device *drm) +{ + struct tinydrm_device *tdev = drm_to_tinydrm(drm); + struct repaper_epd *epd = epd_from_tinydrm(tdev); + + tinydrm_release(drm); + kfree(epd); +} + static const uint32_t repaper_formats[] = { DRM_FORMAT_XRGB8888, }; @@ -894,6 +903,7 @@ static struct drm_driver repaper_driver = { DRIVER_ATOMIC, .fops = &repaper_fops, TINYDRM_GEM_DRIVER_OPS, + .release = repaper_release, .name = "repaper", .desc = "Pervasive Displays RePaper e-ink panels", .date = "20170405", @@ -949,7 +959,7 @@ static int repaper_probe(struct spi_device *spi) } } - epd = devm_kzalloc(dev, sizeof(*epd), GFP_KERNEL); + epd = kzalloc(sizeof(*epd), GFP_KERNEL); if (!epd) return -ENOMEM; @@ -1067,8 +1077,10 @@ static int repaper_probe(struct spi_device *spi) tdev = &epd->tinydrm; ret = devm_tinydrm_init(dev, tdev, &repaper_fb_funcs, &repaper_driver); - if (ret) + if (ret) { + kfree(epd); return ret; + } ret = tinydrm_display_pipe_init(tdev, &repaper_pipe_funcs, DRM_MODE_CONNECTOR_VIRTUAL, @@ -1077,7 +1089,7 @@ static int repaper_probe(struct spi_device *spi) if (ret) return ret; - drm_mode_config_reset(tdev->drm); + drm_mode_config_reset(&tdev->drm); spi_set_drvdata(spi, tdev); DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000); diff --git a/drivers/gpu/drm/tinydrm/st7586.c b/drivers/gpu/drm/tinydrm/st7586.c index 07b4d31..67f5c83 100644 --- a/drivers/gpu/drm/tinydrm/st7586.c +++ b/drivers/gpu/drm/tinydrm/st7586.c @@ -112,7 +112,7 @@ static int st7586_fb_dirty(struct drm_framebuffer *fb, unsigned int color, struct drm_clip_rect *clips, unsigned int num_clips) { - struct tinydrm_device *tdev = fb->dev->dev_private; + struct tinydrm_device *tdev = drm_to_tinydrm(fb->dev); struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); struct drm_clip_rect clip; int start, end; @@ -178,7 +178,7 @@ static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe, struct tinydrm_device *tdev = pipe_to_tinydrm(pipe); struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); struct drm_framebuffer *fb = pipe->plane.fb; - struct device *dev = tdev->drm->dev; + struct device *dev = tdev->drm.dev; int ret; u8 addr_mode; @@ -290,13 +290,13 @@ static int st7586_init(struct device *dev, struct mipi_dbi *mipi, if (ret) return ret; - tdev->drm->mode_config.preferred_depth = 32; + tdev->drm.mode_config.preferred_depth = 32; mipi->rotation = rotation; - drm_mode_config_reset(tdev->drm); + drm_mode_config_reset(&tdev->drm); DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n", - tdev->drm->mode_config.preferred_depth, rotation); + tdev->drm.mode_config.preferred_depth, rotation); return 0; } @@ -319,6 +319,7 @@ static struct drm_driver st7586_driver = { DRIVER_ATOMIC, .fops = &st7586_fops, TINYDRM_GEM_DRIVER_OPS, + .release = mipi_dbi_release, .lastclose = tinydrm_lastclose, .debugfs_init = mipi_dbi_debugfs_init, .name = "st7586", @@ -348,7 +349,7 @@ static int st7586_probe(struct spi_device *spi) u32 rotation = 0; int ret; - mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL); + mipi = kzalloc(sizeof(*mipi), GFP_KERNEL); if (!mipi) return -ENOMEM; @@ -384,8 +385,10 @@ static int st7586_probe(struct spi_device *spi) ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver, &st7586_mode, rotation); - if (ret) + if (ret) { + kfree(mipi); return ret; + } spi_set_drvdata(spi, mipi); diff --git a/include/drm/tinydrm/mipi-dbi.h b/include/drm/tinydrm/mipi-dbi.h index 83346dd..f03b586 100644 --- a/include/drm/tinydrm/mipi-dbi.h +++ b/include/drm/tinydrm/mipi-dbi.h @@ -63,6 +63,7 @@ mipi_dbi_from_tinydrm(struct tinydrm_device *tdev) int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *mipi, struct gpio_desc *dc); +void mipi_dbi_release(struct drm_device *drm); int mipi_dbi_init(struct device *dev, struct mipi_dbi *mipi, const struct drm_simple_display_pipe_funcs *pipe_funcs, struct drm_driver *driver, diff --git a/include/drm/tinydrm/tinydrm.h b/include/drm/tinydrm/tinydrm.h index 4774fe3..a895ad1 100644 --- a/include/drm/tinydrm/tinydrm.h +++ b/include/drm/tinydrm/tinydrm.h @@ -10,6 +10,7 @@ #ifndef __LINUX_TINYDRM_H #define __LINUX_TINYDRM_H +#include <drm/drm_device.h> #include <drm/drm_gem_cma_helper.h> #include <drm/drm_fb_cma_helper.h> #include <drm/drm_simple_kms_helper.h> @@ -24,7 +25,7 @@ * @fb_funcs: Framebuffer functions used when creating framebuffers */ struct tinydrm_device { - struct drm_device *drm; + struct drm_device drm; struct drm_simple_display_pipe pipe; struct mutex dirty_lock; struct drm_fbdev_cma *fbdev_cma; @@ -33,6 +34,12 @@ struct tinydrm_device { }; static inline struct tinydrm_device * +drm_to_tinydrm(struct drm_device *drm) +{ + return container_of(drm, struct tinydrm_device, drm); +} + +static inline struct tinydrm_device * pipe_to_tinydrm(struct drm_simple_display_pipe *pipe) { return container_of(pipe, struct tinydrm_device, pipe); @@ -87,6 +94,7 @@ struct drm_gem_object * tinydrm_gem_cma_prime_import_sg_table(struct drm_device *drm, struct dma_buf_attachment *attach, struct sg_table *sgt); +void tinydrm_release(struct drm_device *drm); int devm_tinydrm_init(struct device *parent, struct tinydrm_device *tdev, const struct drm_framebuffer_funcs *fb_funcs, struct drm_driver *driver); -- 2.7.4 _______________________________________________ dri-devel mailing list dri-devel@xxxxxxxxxxxxxxxxxxxxx https://lists.freedesktop.org/mailman/listinfo/dri-devel