Skip to contentSkip to conversation

drm/vram: Provide helpers for prepare_fb() and cleanup_fb()

PATCH In review Thomas Zimmermann 2 participants 7 messages Oct 24, 2019

Subsystems (4)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

v2 cover drm/vram: Provide helpers for prepare_fb() and cleanup_fb()

The implementation of the plane's call-back functions prepare_fb() and cleanup_fb() for GEM VRAM helpers are sharable among drivers.

Patch #3 also fixes two bugs that have been present in hibmc since it was first added. The primary plane's atomic_update() is not responsible for pinning BOs. And it never unpinned unused BOs. VRAM is being exausted over time.

The new helpers have been tested to work.

v2:
* provide helpers for struct drm_simple_display_pipe_funcs, and...
* ...use them in bochs

Thomas Zimmermann (4):
drm/vram-helpers: Add helpers for prepare_fb() and cleanup_fb() drm/bochs: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers drm/hisilicon/hibmc: Use GEM VRAM's prepare_fb() and cleanup_fb()
helpers
drm/vboxvideo: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers

 drivers/gpu/drm/bochs/bochs_kms.c             |  26 +---
 drivers/gpu/drm/drm_gem_vram_helper.c         | 126 ++++++++++++++++++
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    |  14 +-
 drivers/gpu/drm/vboxvideo/vbox_mode.c         |  61 +--------
 include/drm/drm_gem_vram_helper.h             |  25 ++++
 5 files changed, 161 insertions(+), 91 deletions(-)

--
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#1 lore raw

PATCH v2 1/4 drm/vram-helpers: Add helpers for prepare_fb() and cleanup_fb()

The new helpers pin and unpin a framebuffer's GEM VRAM objects during plane updates. This should be sufficient for most drivers' implementation of prepare_fb() and cleanup_fb().

v2:
* provide helpers for struct drm_simple_display_pipe_funcs
* rename plane-helper funcs

---
 drivers/gpu/drm/drm_gem_vram_helper.c | 126 ++++++++++++++++++++++++++
 include/drm/drm_gem_vram_helper.h     |  25 +++++
 2 files changed, 151 insertions(+)
drivers/gpu/drm/drm_gem_vram_helper.c +126 -0
index b86fe0fa9d05..7fe93cd38eea 100644
@@ -3,10 +3,13 @@
 #include <drm/drm_debugfs.h>
 #include <drm/drm_device.h>
 #include <drm/drm_file.h>
+#include <drm/drm_framebuffer.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/drm_gem_vram_helper.h>
 #include <drm/drm_mode.h>
+#include <drm/drm_plane.h>
 #include <drm/drm_prime.h>
+#include <drm/drm_simple_kms_helper.h>
 #include <drm/ttm/ttm_page_alloc.h>
 
 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
@@ -646,6 +649,129 @@ int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
 }
 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_mmap_offset);
 
+/*
+ * Helpers for struct drm_plane_helper_funcs
+ */
+
+/**
+ * drm_gem_vram_plane_helper_prepare_fb() - \
+ *	Implements &struct drm_plane_helper_funcs.prepare_fb
+ * @plane:	a DRM plane
+ * @new_state:	the plane's new state
+ *
+ * During plane updates, this function pins the GEM VRAM
+ * objects of the plane's new framebuffer to VRAM. Call
+ * drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
+ *
+ * Returns:
+ *	0 on success, or
+ *	a negative errno code otherwise.
+ */
+int
+drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
+				     struct drm_plane_state *new_state)
+{
+	size_t i;
+	struct drm_gem_vram_object *gbo;
+	int ret;
+
+	if (!new_state->fb)
+		return 0;
+
+	for (i = 0; i < ARRAY_SIZE(new_state->fb->obj); ++i) {
+		if (!new_state->fb->obj[i])
+			continue;
+		gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
+		ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
+		if (ret)
+			goto err_drm_gem_vram_unpin;
+	}
+
+	return 0;
+
+err_drm_gem_vram_unpin:
+	while (i) {
+		--i;
+		gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
+		drm_gem_vram_unpin(gbo);
+	}
+	return ret;
+}
+EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
+
+/**
+ * drm_gem_vram_plane_helper_cleanup_fb() - \
+ *	Implements &struct drm_plane_helper_funcs.cleanup_fb
+ * @plane:	a DRM plane
+ * @old_state:	the plane's old state
+ *
+ * During plane updates, this function unpins the GEM VRAM
+ * objects of the plane's old framebuffer from VRAM. Complements
+ * drm_gem_vram_plane_helper_prepare_fb().
+ */
+void
+drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
+				     struct drm_plane_state *old_state)
+{
+	size_t i;
+	struct drm_gem_vram_object *gbo;
+
+	if (!old_state->fb)
+		return;
+
+	for (i = 0; i < ARRAY_SIZE(old_state->fb->obj); ++i) {
+		if (!old_state->fb->obj[i])
+			continue;
+		gbo = drm_gem_vram_of_gem(old_state->fb->obj[i]);
+		drm_gem_vram_unpin(gbo);
+	}
+}
+EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
+
+/*
+ * Helpers for struct drm_simple_display_pipe_funcs
+ */
+
+/**
+ * drm_gem_vram_simple_display_pipe_prepare_fb() - \
+ *	Implements &struct drm_simple_display_pipe_funcs.prepare_fb
+ * @pipe:	a simple display pipe
+ * @new_state:	the plane's new state
+ *
+ * During plane updates, this function pins the GEM VRAM
+ * objects of the plane's new framebuffer to VRAM. Call
+ * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
+ *
+ * Returns:
+ *	0 on success, or
+ *	a negative errno code otherwise.
+ */
+int drm_gem_vram_simple_display_pipe_prepare_fb(
+	struct drm_simple_display_pipe *pipe,
+	struct drm_plane_state *new_state)
+{
+	return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
+}
+EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
+
+/**
+ * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
+ *	Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
+ * @pipe:	a simple display pipe
+ * @old_state:	the plane's old state
+ *
+ * During plane updates, this function unpins the GEM VRAM
+ * objects of the plane's old framebuffer from VRAM. Complements
+ * drm_gem_vram_simple_display_pipe_prepare_fb().
+ */
+void drm_gem_vram_simple_display_pipe_cleanup_fb(
+	struct drm_simple_display_pipe *pipe,
+	struct drm_plane_state *old_state)
+{
+	drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
+}
+EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
+
 /*
  * PRIME helpers
  */
include/drm/drm_gem_vram_helper.h +25 -0
index b8ad4531ebb4..e040541a105f 100644
@@ -13,6 +13,9 @@
 #include <linux/kernel.h> /* for container_of() */
 
 struct drm_mode_create_dumb;
+struct drm_plane;
+struct drm_plane_state;
+struct drm_simple_display_pipe;
 struct drm_vram_mm_funcs;
 struct filp;
 struct vm_area_struct;
@@ -124,6 +127,28 @@ int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
 					 struct drm_device *dev,
 					 uint32_t handle, uint64_t *offset);
 
+/*
+ * Helpers for struct drm_plane_helper_funcs
+ */
+int
+drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
+				     struct drm_plane_state *new_state);
+void
+drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
+				     struct drm_plane_state *old_state);
+
+/*
+ * Helpers for struct drm_simple_display_pipe_funcs
+ */
+
+int drm_gem_vram_simple_display_pipe_prepare_fb(
+	struct drm_simple_display_pipe *pipe,
+	struct drm_plane_state *new_state);
+
+void drm_gem_vram_simple_display_pipe_cleanup_fb(
+	struct drm_simple_display_pipe *pipe,
+	struct drm_plane_state *old_state);
+
 /**
  * define DRM_GEM_VRAM_DRIVER - default callback functions for \
 	&struct drm_driver
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#2 lore raw

PATCH v2 2/4 drm/bochs: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers

In reply to Thomas Zimmermann

GEM VRAM provides an implementation for prepare_fb() and cleanup_fb() of struct drm_simple_display_pipe_funcs. Switch over bochs.

v2:
* use helpers for struct drm_simple_display_pipe_funcs

---
 drivers/gpu/drm/bochs/bochs_kms.c | 26 ++------------------------
 1 file changed, 2 insertions(+), 24 deletions(-)
drivers/gpu/drm/bochs/bochs_kms.c +2 -24
index 02a9c1ed165b..3f0006c2470d 100644
@@ -69,33 +69,11 @@ static void bochs_pipe_update(struct drm_simple_display_pipe *pipe,
 	}
 }
 
-static int bochs_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
-				 struct drm_plane_state *new_state)
-{
-	struct drm_gem_vram_object *gbo;
-
-	if (!new_state->fb)
-		return 0;
-	gbo = drm_gem_vram_of_gem(new_state->fb->obj[0]);
-	return drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
-}
-
-static void bochs_pipe_cleanup_fb(struct drm_simple_display_pipe *pipe,
-				  struct drm_plane_state *old_state)
-{
-	struct drm_gem_vram_object *gbo;
-
-	if (!old_state->fb)
-		return;
-	gbo = drm_gem_vram_of_gem(old_state->fb->obj[0]);
-	drm_gem_vram_unpin(gbo);
-}
-
 static const struct drm_simple_display_pipe_funcs bochs_pipe_funcs = {
 	.enable	    = bochs_pipe_enable,
 	.update	    = bochs_pipe_update,
-	.prepare_fb = bochs_pipe_prepare_fb,
-	.cleanup_fb = bochs_pipe_cleanup_fb,
+	.prepare_fb = drm_gem_vram_simple_display_pipe_prepare_fb,
+	.cleanup_fb = drm_gem_vram_simple_display_pipe_cleanup_fb,
 };
 
 static int bochs_connector_get_modes(struct drm_connector *connector)
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#3 lore raw

PATCH v2 3/4 drm/hisilicon/hibmc: Use GEM VRAM's prepare_fb() and cleanup_fb() helpers

In reply to Thomas Zimmermann

This patch implements prepare_fb() and cleanup_fb() in hibmc with the GEM VRAM helpers. In the current code, pinning the BO is performed by hibmc_plane_atomic_update(), where the operation does not belong.

This patch also fixes a bug where the pinned BO was never unpinned. Pinning multiple BOs would have exhaused the available VRAM and further pin operations would have failed, leaving the display in a corrupt state.

---
 drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +4 -10
index cc4c41748cfb..6527a97f68a3 100644
@@ -96,7 +96,6 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 {
 	struct drm_plane_state	*state	= plane->state;
 	u32 reg;
-	int ret;
 	s64 gpu_addr = 0;
 	unsigned int line_l;
 	struct hibmc_drm_private *priv = plane->dev->dev_private;
@@ -109,16 +108,9 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 	hibmc_fb = to_hibmc_framebuffer(state->fb);
 	gbo = drm_gem_vram_of_gem(hibmc_fb->obj);
 
-	ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
-	if (ret) {
-		DRM_ERROR("failed to pin bo: %d", ret);
-		return;
-	}
 	gpu_addr = drm_gem_vram_offset(gbo);
-	if (gpu_addr < 0) {
-		drm_gem_vram_unpin(gbo);
-		return;
-	}
+	if (WARN_ON_ONCE(gpu_addr < 0))
+		return; /* Bug: we didn't pin the BO to VRAM in prepare_fb. */
 
 	writel(gpu_addr, priv->mmio + HIBMC_CRT_FB_ADDRESS);
 
@@ -157,6 +149,8 @@ static struct drm_plane_funcs hibmc_plane_funcs = {
 };
 
 static const struct drm_plane_helper_funcs hibmc_plane_helper_funcs = {
+	.prepare_fb	= drm_gem_vram_plane_helper_prepare_fb,
+	.cleanup_fb	= drm_gem_vram_plane_helper_cleanup_fb,
 	.atomic_check = hibmc_plane_atomic_check,
 	.atomic_update = hibmc_plane_atomic_update,
 };
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#4 lore raw

PATCH v2 4/4 drm/vboxvideo: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers

In reply to Thomas Zimmermann

GEM VRAM provides an implementation for prepare_fb() and cleanup_fb() of struct drm_plane_helper_funcs. Switch over vboxvideo.

---
 drivers/gpu/drm/vboxvideo/vbox_mode.c | 61 ++-------------------------
 1 file changed, 4 insertions(+), 57 deletions(-)
drivers/gpu/drm/vboxvideo/vbox_mode.c +4 -57
index b5604d32122e..cea38c5345c6 100644
@@ -334,35 +334,6 @@ static void vbox_primary_atomic_disable(struct drm_plane *plane,
 				    old_state->src_y >> 16);
 }
 
-static int vbox_primary_prepare_fb(struct drm_plane *plane,
-				   struct drm_plane_state *new_state)
-{
-	struct drm_gem_vram_object *gbo;
-	int ret;
-
-	if (!new_state->fb)
-		return 0;
-
-	gbo = drm_gem_vram_of_gem(new_state->fb->obj[0]);
-	ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
-	if (ret)
-		DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret);
-
-	return ret;
-}
-
-static void vbox_primary_cleanup_fb(struct drm_plane *plane,
-				    struct drm_plane_state *old_state)
-{
-	struct drm_gem_vram_object *gbo;
-
-	if (!old_state->fb)
-		return;
-
-	gbo = drm_gem_vram_of_gem(old_state->fb->obj[0]);
-	drm_gem_vram_unpin(gbo);
-}
-
 static int vbox_cursor_atomic_check(struct drm_plane *plane,
 				    struct drm_plane_state *new_state)
 {
@@ -492,30 +463,6 @@ static void vbox_cursor_atomic_disable(struct drm_plane *plane,
 	mutex_unlock(&vbox->hw_mutex);
 }
 
-static int vbox_cursor_prepare_fb(struct drm_plane *plane,
-				  struct drm_plane_state *new_state)
-{
-	struct drm_gem_vram_object *gbo;
-
-	if (!new_state->fb)
-		return 0;
-
-	gbo = drm_gem_vram_of_gem(new_state->fb->obj[0]);
-	return drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
-}
-
-static void vbox_cursor_cleanup_fb(struct drm_plane *plane,
-				   struct drm_plane_state *old_state)
-{
-	struct drm_gem_vram_object *gbo;
-
-	if (!plane->state->fb)
-		return;
-
-	gbo = drm_gem_vram_of_gem(plane->state->fb->obj[0]);
-	drm_gem_vram_unpin(gbo);
-}
-
 static const u32 vbox_cursor_plane_formats[] = {
 	DRM_FORMAT_ARGB8888,
 };
@@ -524,8 +471,8 @@ static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
 	.atomic_check	= vbox_cursor_atomic_check,
 	.atomic_update	= vbox_cursor_atomic_update,
 	.atomic_disable	= vbox_cursor_atomic_disable,
-	.prepare_fb	= vbox_cursor_prepare_fb,
-	.cleanup_fb	= vbox_cursor_cleanup_fb,
+	.prepare_fb	= drm_gem_vram_plane_helper_prepare_fb,
+	.cleanup_fb	= drm_gem_vram_plane_helper_cleanup_fb,
 };
 
 static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
@@ -546,8 +493,8 @@ static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
 	.atomic_check = vbox_primary_atomic_check,
 	.atomic_update = vbox_primary_atomic_update,
 	.atomic_disable = vbox_primary_atomic_disable,
-	.prepare_fb = vbox_primary_prepare_fb,
-	.cleanup_fb = vbox_primary_cleanup_fb,
+	.prepare_fb	= drm_gem_vram_plane_helper_prepare_fb,
+	.cleanup_fb	= drm_gem_vram_plane_helper_cleanup_fb,
 };
 
 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on the v2 series
#5 lore raw
In reply to Thomas Zimmermann
Quoted 8 lines · Thomas Zimmermann

The implementation of the plane's call-back functions prepare_fb() and cleanup_fb() for GEM VRAM helpers are sharable among drivers.

Patch #3 also fixes two bugs that have been present in hibmc since it was first added. The primary plane's atomic_update() is not responsible for pinning BOs. And it never unpinned unused BOs. VRAM is being exausted over time.

Quoted 5 lines

The new helpers have been tested to work.

v2:
* provide helpers for struct drm_simple_display_pipe_funcs, and...
* ...use them in bochs

Oh I thought we agreed on changing the simple_pipe type for prepare/cleanup_fb ... But this works too ofc. On the series:

Quoted 18 lines

Thomas Zimmermann (4):
drm/vram-helpers: Add helpers for prepare_fb() and cleanup_fb() drm/bochs: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers drm/hisilicon/hibmc: Use GEM VRAM's prepare_fb() and cleanup_fb()
helpers
drm/vboxvideo: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers

 drivers/gpu/drm/bochs/bochs_kms.c             |  26 +---
 drivers/gpu/drm/drm_gem_vram_helper.c         | 126 ++++++++++++++++++
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    |  14 +-
 drivers/gpu/drm/vboxvideo/vbox_mode.c         |  61 +--------
 include/drm/drm_gem_vram_helper.h             |  25 ++++
 5 files changed, 161 insertions(+), 91 deletions(-)

--
2.23.0

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#6 lore raw

Hi

Am 24.10.19 um 14:37 schrieb Daniel Vetter:

Quoted 9 lines · Thomas Zimmermann
Quoted 8 lines

The implementation of the plane's call-back functions prepare_fb() and cleanup_fb() for GEM VRAM helpers are sharable among drivers.

Patch #3 also fixes two bugs that have been present in hibmc since it was first added. The primary plane's atomic_update() is not responsible for pinning BOs. And it never unpinned unused BOs. VRAM is being exausted over time.

Quoted 8 lines
Quoted 5 lines

The new helpers have been tested to work.

v2:
* provide helpers for struct drm_simple_display_pipe_funcs, and...
* ...use them in bochs

Oh I thought we agreed on changing the simple_pipe type for prepare/cleanup_fb ... But this works too ofc. On the series:

Well, I'm still no fan of the current simple pipe helpers. But after you changed the signature of mode_valid() and explained the reasons, I thought it was more important to have consistent interfaces.

Quoted 2 lines

Thanks!

Best regards
Thomas

Quoted 20 lines
Quoted 19 lines

Thomas Zimmermann (4):
drm/vram-helpers: Add helpers for prepare_fb() and cleanup_fb() drm/bochs: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers drm/hisilicon/hibmc: Use GEM VRAM's prepare_fb() and cleanup_fb()
helpers
drm/vboxvideo: Replace prepare_fb()/cleanup_fb() with GEM VRAM helpers

 drivers/gpu/drm/bochs/bochs_kms.c             |  26 +---
 drivers/gpu/drm/drm_gem_vram_helper.c         | 126 ++++++++++++++++++
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    |  14 +-
 drivers/gpu/drm/vboxvideo/vbox_mode.c         |  61 +--------
 include/drm/drm_gem_vram_helper.h             |  25 ++++
 5 files changed, 161 insertions(+), 91 deletions(-)

--
2.23.0

Signature · 6 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Maxfeldstr. 5, 90409 Nürnberg, Germany
(HRB 36809, AG Nürnberg)
Geschäftsführer: Felix Imendörffer

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel