Skip to contentSkip to conversation

drm/hibmc: Fix plane helpers and convert to gem-shmem

PATCH Superseded Thomas Zimmermann 2 participants 13 messages Apr 13 – Apr 17

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover drm/hibmc: Fix plane helpers and convert to gem-shmem

Convert hibmc's memory management from gem-vram ot gem-shmem. Fix a number of bugs in preparation.

Patches 1 to 3 fix various bugs in the plane code.

Patch 4 replaces gem-vram with gem-shmem. The rational is that gem-vram has corner cases on low-end devices and is limited in functionality. The commit messages state a list of possible issues.

Switching to gem-shmem avoids these problems. It also enables buffer sharing with dma_buf, which improves the hibmc driver's integration with the overall graphics ecosystem.

We've done these changes on drivers for similar hardware, such as ast and mgag200. Hibmc is the last of these drivers to not run gem-shmem. Gem-vram instead is deprecated and hopefully can be removed in a later release.

Tested on an RH1288 v3 system.

Thomas Zimmermann (4):
drm/hibmc: Use drm_atomic_helper_check_plane_state() drm/hibmc: Fix list of formats on the primary plane drm/hibmc: Do not use cpp from struct drm_format_info drm/hibmc: Use gem-shmem with shadow-plane helpers for memory
management

 drivers/gpu/drm/drm_gem_shmem_helper.c        |  17 ++-
 drivers/gpu/drm/hisilicon/hibmc/Kconfig       |   2 +-
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 110 +++++++++---------
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c   |  62 +++++++---
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |   5 +
 include/drm/drm_gem_shmem_helper.h            |   5 +
 6 files changed, 128 insertions(+), 73 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/4 drm/hibmc: Use drm_atomic_helper_check_plane_state()

Call drm_atomic_helper_check_plane_state() from the primary plane's atomic-check helper and replace the custom implementation.

All plane's implementations of atomic_check should call the shared _check_plane_state() helper first. It adjusts the plane state for correct positioning, rotation and scaling of the plane. If the plane is not visible, it clears the corresponding flag in the plane state.

On errors or if the plane is not visible, the atomic-check helper can return early. Implement all this in hibmc and drop the custom code that does some of it.

Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display engine")
Cc Rongrong Zou <[email protected]>
Cc Sean Paul <[email protected]>
Cc Xinliang Liu <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Baihan Li <[email protected]>
Cc Yongbang Shi <[email protected]>
Cc <[email protected]> # v4.10+
---
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 46 ++++++-------------
 1 file changed, 14 insertions(+), 32 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +14 -32
index 89bed78f1466..8fa2a95bcdd1 100644
@@ -55,46 +55,28 @@ static const struct hibmc_dislay_pll_config hibmc_pll_table[] = {
 static int hibmc_plane_atomic_check(struct drm_plane *plane,
 				    struct drm_atomic_state *state)
 {
-	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
-										 plane);
-	struct drm_framebuffer *fb = new_plane_state->fb;
-	struct drm_crtc *crtc = new_plane_state->crtc;
-	struct drm_crtc_state *crtc_state;
-	u32 src_w = new_plane_state->src_w >> 16;
-	u32 src_h = new_plane_state->src_h >> 16;
-
-	if (!crtc || !fb)
-		return 0;
+	struct drm_plane_state *new_plane_state =
+		drm_atomic_get_new_plane_state(state, plane);
+	struct drm_crtc_state *new_crtc_state = NULL;
+	int ret;
 
-	crtc_state = drm_atomic_get_crtc_state(state, crtc);
-	if (IS_ERR(crtc_state))
-		return PTR_ERR(crtc_state);
+	if (new_plane_state->crtc)
+		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
 
-	if (src_w != new_plane_state->crtc_w || src_h != new_plane_state->crtc_h) {
-		drm_dbg_atomic(plane->dev, "scale not support\n");
-		return -EINVAL;
-	}
-
-	if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0) {
-		drm_dbg_atomic(plane->dev, "crtc_x/y of drm_plane state is invalid\n");
-		return -EINVAL;
-	}
-
-	if (!crtc_state->enable)
+	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
+						  DRM_PLANE_NO_SCALING,
+						  DRM_PLANE_NO_SCALING,
+						  false, true);
+	if (ret)
+		return ret;
+	else if (!new_plane_state->visible)
 		return 0;
 
-	if (new_plane_state->crtc_x + new_plane_state->crtc_w >
-	    crtc_state->adjusted_mode.hdisplay ||
-	    new_plane_state->crtc_y + new_plane_state->crtc_h >
-	    crtc_state->adjusted_mode.vdisplay) {
-		drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-		return -EINVAL;
-	}
-
 	if (new_plane_state->fb->pitches[0] % 128 != 0) {
 		drm_dbg_atomic(plane->dev, "wrong stride with 128-byte aligned\n");
 		return -EINVAL;
 	}
+
 	return 0;
 }
 
Signature · 1 line
2.53.0
maintainer
#2 lore raw

Re: [PATCH 1/4] drm/hibmc: Use drm_atomic_helper_check_plane_state()

Quoted 60 lines

Call drm_atomic_helper_check_plane_state() from the primary plane's atomic-check helper and replace the custom implementation.

All plane's implementations of atomic_check should call the shared _check_plane_state() helper first. It adjusts the plane state for correct positioning, rotation and scaling of the plane. If the plane is not visible, it clears the corresponding flag in the plane state.

On errors or if the plane is not visible, the atomic-check helper can return early. Implement all this in hibmc and drop the custom code that does some of it.

Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display engine")
Cc Rongrong Zou <[email protected]>
Cc Sean Paul <[email protected]>
Cc Xinliang Liu <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Baihan Li <[email protected]>
Cc Yongbang Shi <[email protected]>
Cc <[email protected]> # v4.10+
---
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 46 ++++++-------------
 1 file changed, 14 insertions(+), 32 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +6 -18
index 89bed78f1466..8fa2a95bcdd1 100644
@@ -55,46 +55,28 @@ static const struct hibmc_dislay_pll_config hibmc_pll_table[] = {
 static int hibmc_plane_atomic_check(struct drm_plane *plane,
 				    struct drm_atomic_state *state)
 {
-	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
-										 plane);
-	struct drm_framebuffer *fb = new_plane_state->fb;
-	struct drm_crtc *crtc = new_plane_state->crtc;
-	struct drm_crtc_state *crtc_state;
-	u32 src_w = new_plane_state->src_w >> 16;
-	u32 src_h = new_plane_state->src_h >> 16;
-
-	if (!crtc || !fb)
-		return 0;
+	struct drm_plane_state *new_plane_state =
+		drm_atomic_get_new_plane_state(state, plane);
+	struct drm_crtc_state *new_crtc_state = NULL;
+	int ret;
 
-	crtc_state = drm_atomic_get_crtc_state(state, crtc);
-	if (IS_ERR(crtc_state))
-		return PTR_ERR(crtc_state);
+	if (new_plane_state->crtc)
+		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
 
-	if (src_w != new_plane_state->crtc_w || src_h != new_plane_state->crtc_h) {
-		drm_dbg_atomic(plane->dev, "scale not support\n");
-		return -EINVAL;
-	}
-

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c static const struct hibmc_dislay_pll_config hibmc_pll_table[] = { · line 69

Quoted 10 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +4 -6
-	if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0) {
-		drm_dbg_atomic(plane->dev, "crtc_x/y of drm_plane state is invalid\n");
-		return -EINVAL;
-	}
-
-	if (!crtc_state->enable)
+	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
+						  DRM_PLANE_NO_SCALING,
+						  DRM_PLANE_NO_SCALING,
+						  false, true);

The last parameter, "can_update_disabled", if set to true, causes the condition "if (!crtc_state->enable && !can_update_disabled)" in the function drm_atomic_helper_check_plane_state to always evaluate to false, meaning crtc_state->enable will not be checked. This differs from the behavior prior to the modification.

before:
- crtc_state->enable(true) --> continue check
- crtc_state->enable(false) --> return 0(atomic check success)

after:

	- crtc_state->enable(true)   --> _helper_check_plane_ ->  continue check
	- crtc_state->enable(false)  --> _helper_check_plane_ ->  continue check

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c static const struct hibmc_dislay_pll_config hibmc_pll_table[] = { · line 93

Quoted 13 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +3 -8
+	if (ret)
+		return ret;
+	else if (!new_plane_state->visible)
 		return 0;
 
-	if (new_plane_state->crtc_x + new_plane_state->crtc_w >
-	    crtc_state->adjusted_mode.hdisplay ||
-	    new_plane_state->crtc_y + new_plane_state->crtc_h >
-	    crtc_state->adjusted_mode.vdisplay) {
-		drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-		return -EINVAL;
-	}
-

The purpose of this check is to ensure that the right and bottom boundaries of the plane do not extend beyond the crtc. In drm_atomic_helper_check_plane_state, drm_mode_get_hv_timing is called to retrieve the crtc boundaries, and drm_rect_clip_scaled is used to clip the plane, any portions extending beyond the right and bottom boundaries are discarded.

I'd like to confirm that my understanding is correct? previously, the check failed if the plane exceeded the boundaries, but now, after drm_atomic_helper_check_plane_state is called, the plane is clipped to fit within the boundaries.

in function drm_rect_clip_scaled:

diff = dst->x2 - clip->x2;

if (diff > 0) {
	...
	dst->x2 -= diff;
}
diff = dst->y2 - clip->y2;
if (diff > 0) {
	...
	dst->y2 -= diff;
}

Thanks.

Best regards
Yongbang.

Quoted 8 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +1 -0
 	if (new_plane_state->fb->pitches[0] % 128 != 0) {
 		drm_dbg_atomic(plane->dev, "wrong stride with 128-byte aligned\n");
 		return -EINVAL;
 	}
+
 	return 0;
 }
 
author maintainer
#3 lore raw

Re: [PATCH 1/4] drm/hibmc: Use drm_atomic_helper_check_plane_state()

Hi

Am 16.04.26 um 08:53 schrieb Yongbang Shi:

Quoted 75 lines
Quoted 59 lines

Call drm_atomic_helper_check_plane_state() from the primary plane's atomic-check helper and replace the custom implementation.

All plane's implementations of atomic_check should call the shared _check_plane_state() helper first. It adjusts the plane state for correct positioning, rotation and scaling of the plane. If the plane is not visible, it clears the corresponding flag in the plane state.

On errors or if the plane is not visible, the atomic-check helper can return early. Implement all this in hibmc and drop the custom code that does some of it.

Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display engine")
Cc Rongrong Zou <[email protected]>
Cc Sean Paul <[email protected]>
Cc Xinliang Liu <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Baihan Li <[email protected]>
Cc Yongbang Shi <[email protected]>
Cc <[email protected]> # v4.10+
---
  .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 46 ++++++-------------
  1 file changed, 14 insertions(+), 32 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +6 -18
index 89bed78f1466..8fa2a95bcdd1 100644
@@ -55,46 +55,28 @@ static const struct hibmc_dislay_pll_config hibmc_pll_table[] = {
  static int hibmc_plane_atomic_check(struct drm_plane *plane,
  				    struct drm_atomic_state *state)
  {
-	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
-										 plane);
-	struct drm_framebuffer *fb = new_plane_state->fb;
-	struct drm_crtc *crtc = new_plane_state->crtc;
-	struct drm_crtc_state *crtc_state;
-	u32 src_w = new_plane_state->src_w >> 16;
-	u32 src_h = new_plane_state->src_h >> 16;
-
-	if (!crtc || !fb)
-		return 0;
+	struct drm_plane_state *new_plane_state =
+		drm_atomic_get_new_plane_state(state, plane);
+	struct drm_crtc_state *new_crtc_state = NULL;
+	int ret;
 
-	crtc_state = drm_atomic_get_crtc_state(state, crtc);
-	if (IS_ERR(crtc_state))
-		return PTR_ERR(crtc_state);
+	if (new_plane_state->crtc)
+		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
 
-	if (src_w != new_plane_state->crtc_w || src_h != new_plane_state->crtc_h) {
-		drm_dbg_atomic(plane->dev, "scale not support\n");
-		return -EINVAL;
-	}
-

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c static const struct hibmc_dislay_pll_config hibmc_pll_table[] = { · line 69

Quoted 10 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +4 -6
-	if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0) {
-		drm_dbg_atomic(plane->dev, "crtc_x/y of drm_plane state is invalid\n");
-		return -EINVAL;
-	}
-
-	if (!crtc_state->enable)
+	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
+						  DRM_PLANE_NO_SCALING,
+						  DRM_PLANE_NO_SCALING,
+						  false, true);

The last parameter, "can_update_disabled", if set to true, causes the condition "if (!crtc_state->enable && !can_update_disabled)" in the function drm_atomic_helper_check_plane_state to always evaluate to false, meaning crtc_state->enable will not be checked. This differs from the behavior prior to the modification.

Quoted 7 lines

before:
- crtc_state->enable(true) --> continue check
- crtc_state->enable(false) --> return 0(atomic check success)

after:

	- crtc_state->enable(true)   --> _helper_check_plane_ ->  continue check
	- crtc_state->enable(false)  --> _helper_check_plane_ ->  continue check

Isn't this what the hardware supports? The plane's hardware registers can be updated even if the plane's CRTC is off?

In the old case, atomic check returned success. If we set can_update_disable to false, it would return an error in such as case.  Settings it ti true keeps the success for disabled CRTCs. The semantics of the returned value don't change. It's just that the helper fills a few more fields in drm_plane_state.

Quoted 22 lines
Quoted 5 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +3 -0
+	if (ret)
+		return ret;
+	else if (!new_plane_state->visible)

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c static const struct hibmc_dislay_pll_config hibmc_pll_table[] = { · line 93

Quoted 10 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +0 -8
  		return 0;
 
-	if (new_plane_state->crtc_x + new_plane_state->crtc_w >
-	    crtc_state->adjusted_mode.hdisplay ||
-	    new_plane_state->crtc_y + new_plane_state->crtc_h >
-	    crtc_state->adjusted_mode.vdisplay) {
-		drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-		return -EINVAL;
-	}
-

The purpose of this check is to ensure that the right and bottom boundaries of the plane do not extend beyond the crtc. In drm_atomic_helper_check_plane_state, drm_mode_get_hv_timing is called to retrieve the crtc boundaries, and drm_rect_clip_scaled is used to clip the plane, any portions extending beyond the right and bottom boundaries are discarded.

Quoted 4 lines

I'd like to confirm that my understanding is correct? previously, the check failed if the plane exceeded the boundaries, but now, after drm_atomic_helper_check_plane_state is called, the plane is clipped to fit within the boundaries.

Yes. This sets plane_state->dst, which is clipped to the size of the display mode. But it also tests that the primary plane covers the whole display.

Quoted 4 lines

in function drm_rect_clip_scaled:

diff = dst->x2 - clip->x2;

Quoted 9 lines
if (diff > 0) {
	...
	dst->x2 -= diff;
}
diff = dst->y2 - clip->y2;
if (diff > 0) {
	...
	dst->y2 -= diff;
}

I agree, the logic in drm_atomic_helper_check_plane_state() is hard to understand. It sets the clip rectangle to the size of the display mode (or zero if the CRTC is off) at [1].  Then is clips the source and destination coordinates against the clipping rectangle at [2].

Because we set can_position to false, it tests if the destination and clipping rectangles are equal at [3]. This is similar to the that is being replaced, but with plane state correctly adjusted. If both rectangles are equal, it returns success. If the destination is too small, it fails with an errno code and a warning.

If the plane is not visible, the helper already returned at [4].

[1] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/drm_atomic_helper.c#L943
[2] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/drm_atomic_helper.c#L945
[3] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/drm_atomic_helper.c#L959
[4] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/drm_atomic_helper.c#L949

It is now possible to have a primary plane that is larger than the display mode. This is a feature of the DRM API.

Best regards
Thomas

Quoted 16 lines

Thanks.

Best regards
Yongbang.

Quoted 8 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +1 -0
  	if (new_plane_state->fb->pitches[0] % 128 != 0) {
  		drm_dbg_atomic(plane->dev, "wrong stride with 128-byte aligned\n");
  		return -EINVAL;
  	}
+
  	return 0;
  }
 
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
maintainer Reviewed-by
#4 lore raw

Re: [PATCH 1/4] drm/hibmc: Use drm_atomic_helper_check_plane_state()

Quoted 89 lines

Hi

Am 16.04.26 um 08:53 schrieb Yongbang Shi:

Quoted 85 lines
Quoted 67 lines

Call drm_atomic_helper_check_plane_state() from the primary plane's atomic-check helper and replace the custom implementation.

All plane's implementations of atomic_check should call the shared _check_plane_state() helper first. It adjusts the plane state for correct positioning, rotation and scaling of the plane. If the plane is not visible, it clears the corresponding flag in the plane state.

On errors or if the plane is not visible, the atomic-check helper can return early. Implement all this in hibmc and drop the custom code that does some of it.

Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display

engine")
Cc: Rongrong Zou <[email protected]>
Cc: Sean Paul <[email protected]>
Cc: Xinliang Liu <[email protected]>
Cc: Dmitry Baryshkov <[email protected]>
Cc: Baihan Li <[email protected]>
Cc: Yongbang Shi <[email protected]>
Cc: <[email protected]> # v4.10+
---
  .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 46 ++++++-------------

  1 file changed, 14 insertions(+), 32 deletions(-)
(quoted diff) renamed from drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +0 -0

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c
index 89bed78f1466..8fa2a95bcdd1 100644

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +6 -12
@@ -55,46 +55,28 @@ static const struct hibmc_dislay_pll_config
 hibmc_pll_table[] = {
   static int hibmc_plane_atomic_check(struct drm_plane *plane,
                       struct drm_atomic_state *state)
   {
-    struct drm_plane_state *new_plane_state =
 drm_atomic_get_new_plane_state(state,
-                                         plane);
-    struct drm_framebuffer *fb = new_plane_state->fb;
-    struct drm_crtc *crtc = new_plane_state->crtc;
-    struct drm_crtc_state *crtc_state;
-    u32 src_w = new_plane_state->src_w >> 16;
-    u32 src_h = new_plane_state->src_h >> 16;
-
-    if (!crtc || !fb)
-        return 0;
+    struct drm_plane_state *new_plane_state =
+        drm_atomic_get_new_plane_state(state, plane);
+    struct drm_crtc_state *new_crtc_state = NULL;
+    int ret;
   -    crtc_state = drm_atomic_get_crtc_state(state, crtc);
-    if (IS_ERR(crtc_state))
-        return PTR_ERR(crtc_state);
+    if (new_plane_state->crtc)
+        new_crtc_state = drm_atomic_get_new_crtc_state(state,
 new_plane_state->crtc);

  -    if (src_w != new_plane_state->crtc_w || src_h != new_plane_state->crtc_h) {
-        drm_dbg_atomic(plane->dev, "scale not support\n"); -        return -EINVAL;
-    }
-
-    if (new_plane_state->crtc_x < 0 || new_plane_state->crtc_y < 0) { -        drm_dbg_atomic(plane->dev, "crtc_x/y of drm_plane state is invalid\n");
-        return -EINVAL;

Quoted 8 lines

-    }
-
-    if (!crtc_state->enable)
+    ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
+                          DRM_PLANE_NO_SCALING,
+                          DRM_PLANE_NO_SCALING,
+                          false, true);

The last parameter, "can_update_disabled", if set to true, causes the condition "if (!crtc_state->enable && !can_update_disabled)" in the function drm_atomic_helper_check_plane_state to always evaluate to false, meaning crtc_state->enable will not be checked. This differs from the behavior prior to the modification.

before:
    - crtc_state->enable(true)  --> continue check
    - crtc_state->enable(false) --> return 0(atomic check success)

Quoted 9 lines
Quoted 5 lines

after:
    - crtc_state->enable(true)   --> _helper_check_plane_ ->  continue check
    - crtc_state->enable(false)  --> _helper_check_plane_ ->  continue check

Isn't this what the hardware supports? The plane's hardware registers can be updated even if the plane's CRTC is off?

Sure, The plane's hardware registers could be update after disable plane's crtc.

Quoted 6 lines

In the old case, atomic check returned success. If we set can_update_disable to false, it would return an error in such as case. Settings it ti true keeps the success for disabled CRTCs. The semantics of the returned value don't change. It's just that the helper fills a few more fields in drm_plane_state.

Yes, can_update_disable couldn't be false.

I just wanted to confirm this change. There is a slight difference between the original code and the modified version. In the original code, at location [1], it returns "check success" directly without checking the subsequent "new_plane_state->fb->pitches[0]" at [2], whereas the modified version continues to perform the full check.

However, I believe the modified version is the better implementation, the plane check should be complete even if the CRTC is not enabled.

[1]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c#L84
[2]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c#L94

Quoted 69 lines
Quoted 24 lines
Quoted 4 lines
Quoted 13 lines

+    if (ret)
+        return ret;
+    else if (!new_plane_state->visible)
          return 0;
  -    if (new_plane_state->crtc_x + new_plane_state->crtc_w > -        crtc_state->adjusted_mode.hdisplay ||
-        new_plane_state->crtc_y + new_plane_state->crtc_h > -        crtc_state->adjusted_mode.vdisplay) {
-        drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-        return -EINVAL;
-    }
-

The purpose of this check is to ensure that the right and bottom boundaries of the plane do not extend beyond the crtc. In drm_atomic_helper_check_plane_state, drm_mode_get_hv_timing is called to retrieve the crtc boundaries, and drm_rect_clip_scaled is used to clip the plane, any portions extending beyond the right and bottom boundaries are discarded.

Quoted 4 lines

I'd like to confirm that my understanding is correct? previously, the check failed if the plane exceeded the boundaries, but now, after drm_atomic_helper_check_plane_state is called, the plane is clipped to fit within the boundaries.

Yes. This sets plane_state->dst, which is clipped to the size of the display mode. But it also tests that the primary plane covers the whole display.

Quoted 13 lines

in function drm_rect_clip_scaled:

diff = dst->x2 - clip->x2;
if (diff > 0) {
    ...
    dst->x2 -= diff;
}
diff = dst->y2 - clip->y2;
if (diff > 0) {
    ...
    dst->y2 -= diff;
}

I agree, the logic in drm_atomic_helper_check_plane_state() is hard to understand. It sets the clip rectangle to the size of the display mode (or zero if the CRTC is off) at [1].  Then is clips the source and destination coordinates against the clipping rectangle at [2].

Because we set can_position to false, it tests if the destination and clipping rectangles are equal at [3]. This is similar to the that is being replaced, but with plane state correctly adjusted. If both rectangles are equal, it returns success. If the destination is too small, it fails with an errno code and a warning.

If the plane is not visible, the helper already returned at [4].

[1] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L943
[2] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L945
[3] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L959
[4] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L949

Quoted 3 lines

It is now possible to have a primary plane that is larger than the display mode. This is a feature of the DRM API.

Thank you so much for the explanation. It's much clearer to understand drm_atomic_helper_check_plane_state.

By the way, we ran some basic tests on this set of patches using the latest BMC chip, and everything worked fine.

Thanks.

Best regards
Yongbang.

Quoted 22 lines

Best regards
Thomas

Quoted 17 lines

Thanks.

Best regards
Yongbang.

Quoted 9 lines

      if (new_plane_state->fb->pitches[0] % 128 != 0) { drm_dbg_atomic(plane->dev, "wrong stride with 128-byte aligned\n");
          return -EINVAL;
      }
+
      return 0;
  }

author maintainer
#5 lore raw

Re: [PATCH 1/4] drm/hibmc: Use drm_atomic_helper_check_plane_state()

Hi

Am 16.04.26 um 15:22 schrieb Yongbang Shi:
[...]

Quoted 2 lines

However, I believe the modified version is the better implementation, the plane check should be complete even if the CRTC is not enabled.

Great.

I agree that the logic in the helper is non-intuitive. I'll send you an updated series with an improved commit message next week.

Best regards
Thomas

Quoted 110 lines

[1]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c#L84
[2]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c#L94

Quoted 64 lines
Quoted 22 lines
Quoted 15 lines

+    if (ret)
+        return ret;
+    else if (!new_plane_state->visible)
          return 0;
  -    if (new_plane_state->crtc_x + new_plane_state->crtc_w >
-        crtc_state->adjusted_mode.hdisplay ||
-        new_plane_state->crtc_y + new_plane_state->crtc_h > -        crtc_state->adjusted_mode.vdisplay) {
-        drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-        return -EINVAL;
-    }
-

The purpose of this check is to ensure that the right and bottom boundaries of the plane do not extend beyond the crtc. In drm_atomic_helper_check_plane_state, drm_mode_get_hv_timing is called to retrieve the crtc boundaries, and drm_rect_clip_scaled is used to clip the plane, any portions extending beyond the right and bottom boundaries are discarded.

Quoted 4 lines

I'd like to confirm that my understanding is correct? previously, the check failed if the plane exceeded the boundaries, but now, after drm_atomic_helper_check_plane_state is called, the plane is clipped to fit within the boundaries.

Yes. This sets plane_state->dst, which is clipped to the size of the display mode. But it also tests that the primary plane covers the whole display.

Quoted 12 lines

in function drm_rect_clip_scaled:

diff = dst->x2 - clip->x2;
if (diff > 0) {
    ...
    dst->x2 -= diff;
}
diff = dst->y2 - clip->y2;
if (diff > 0) {
    ...
    dst->y2 -= diff;
}

I agree, the logic in drm_atomic_helper_check_plane_state() is hard to understand. It sets the clip rectangle to the size of the display mode (or zero if the CRTC is off) at [1].  Then is clips the source and destination coordinates against the clipping rectangle at [2].

Because we set can_position to false, it tests if the destination and clipping rectangles are equal at [3]. This is similar to the that is being replaced, but with plane state correctly adjusted. If both rectangles are equal, it returns success. If the destination is too small, it fails with an errno code and a warning.

If the plane is not visible, the helper already returned at [4].

[1] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L943
[2] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L945
[3] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L959
[4] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L949

Quoted 3 lines

It is now possible to have a primary plane that is larger than the display mode. This is a feature of the DRM API.

Thank you so much for the explanation. It's much clearer to understand drm_atomic_helper_check_plane_state.

By the way, we ran some basic tests on this set of patches using the latest BMC chip, and everything worked fine.

Thanks.

Best regards
Yongbang.

Quoted 20 lines

Best regards
Thomas

Quoted 16 lines

Thanks.

Best regards
Yongbang.

Quoted 9 lines

      if (new_plane_state->fb->pitches[0] % 128 != 0) { drm_dbg_atomic(plane->dev, "wrong stride with 128-byte
aligned\n");
          return -EINVAL;
      }
+
      return 0;
  }

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
maintainer
#6 lore raw

Re: [PATCH 1/4] drm/hibmc: Use drm_atomic_helper_check_plane_state()

Quoted 5 lines

Hi

Am 16.04.26 um 15:22 schrieb Yongbang Shi:
[...]

Quoted 8 lines
Quoted 2 lines

However, I believe the modified version is the better implementation, the plane check should be complete even if the CRTC is not enabled.

Great.

I agree that the logic in the helper is non-intuitive. I'll send you an updated series with an improved commit message next week.

Looking forward to the updated series next week, and I appreciate the improved commit message as well.

Thanks,
Yongbang.

Quoted 117 lines

Best regards
Thomas

Quoted 113 lines

[1]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
hisilicon/hibmc/hibmc_drm_de.c#L84
[2]
https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
hisilicon/hibmc/hibmc_drm_de.c#L94

Quoted 65 lines
Quoted 22 lines
Quoted 15 lines

+    if (ret)
+        return ret;
+    else if (!new_plane_state->visible)
           return 0;
   -    if (new_plane_state->crtc_x + new_plane_state->crtc_w > -        crtc_state->adjusted_mode.hdisplay ||
-        new_plane_state->crtc_y + new_plane_state->crtc_h > -        crtc_state->adjusted_mode.vdisplay) {
-        drm_dbg_atomic(plane->dev, "visible portion of plane is invalid\n");
-        return -EINVAL;
-    }
-

The purpose of this check is to ensure that the right and bottom boundaries of the plane do not extend beyond the crtc. In drm_atomic_helper_check_plane_state, drm_mode_get_hv_timing is called to retrieve the crtc boundaries, and drm_rect_clip_scaled is used to clip the plane, any portions extending beyond the right and bottom boundaries are discarded.

Quoted 5 lines

I'd like to confirm that my understanding is correct? previously, the check failed if the plane exceeded the boundaries, but now, after drm_atomic_helper_check_plane_state is called, the plane is clipped to
fit within the boundaries.

Yes. This sets plane_state->dst, which is clipped to the size of the display mode. But it also tests that the primary plane covers the whole display.

Quoted 12 lines

in function drm_rect_clip_scaled:

diff = dst->x2 - clip->x2;
if (diff > 0) {
     ...
     dst->x2 -= diff;
}
diff = dst->y2 - clip->y2;
if (diff > 0) {
     ...
     dst->y2 -= diff;
}

I agree, the logic in drm_atomic_helper_check_plane_state() is hard to understand. It sets the clip rectangle to the size of the display mode (or zero if the CRTC is off) at [1].  Then is clips the source and destination coordinates against the clipping rectangle at [2].

Because we set can_position to false, it tests if the destination and clipping rectangles are equal at [3]. This is similar to the that is being replaced, but with plane state correctly adjusted. If both rectangles are equal, it returns success. If the destination is too small, it fails with an errno code and a warning.

If the plane is not visible, the helper already returned at [4].

[1] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L943
[2] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L945
[3] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L959
[4] https://elixir.bootlin.com/linux/v7.0/source/drivers/gpu/drm/
drm_atomic_helper.c#L949

Quoted 3 lines

It is now possible to have a primary plane that is larger than the display mode. This is a feature of the DRM API.

Thank you so much for the explanation. It's much clearer to understand drm_atomic_helper_check_plane_state.

By the way, we ran some basic tests on this set of patches using the latest BMC chip, and everything worked fine.

Thanks.

Best regards
Yongbang.

Quoted 20 lines

Best regards
Thomas

Quoted 16 lines

Thanks.

Best regards
Yongbang.

Quoted 9 lines

       if (new_plane_state->fb->pitches[0] % 128 != 0) { drm_dbg_atomic(plane->dev, "wrong stride with 128-byte aligned\n");
           return -EINVAL;
       }
+
       return 0;
   }

author maintainer
#7 lore raw

PATCH 2/4 drm/hibmc: Fix list of formats on the primary plane

In reply to Thomas Zimmermann

Remove all formats from the primary plane that are unsupported for various reasons.

* Formats with alpha channel: planes should not announce alpha channels unless they support transparency. There's no transparency support in the primary plane's implementation.

* Formats with BGR order. The common format is in RGB channel order. There's no BGR support in the primary plane's implementation.

* RGB888: atomic_update programs the format from cpp[0] * 8 / 16. For RGB888's cpp value of 3 this returns 1.5; rounded to 1. Programming the value of 1 to HIBMC_CRT_DISP_CTL_FORMAT sets up RGB565. Hence, the output is distorted. This can be tested by booting with video=1024x768-24.

Removing all unsupported formats leaves XRGB8888 and RGB565. Both of which are supported and work correctly.

Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display engine")
Cc Rongrong Zou <[email protected]>
Cc Sean Paul <[email protected]>
Cc Xinliang Liu <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Yongbang Shi <[email protected]>
Cc Baihan Li <[email protected]>
Cc <[email protected]> # v4.10+
---
 drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +2 -4
index 8fa2a95bcdd1..c4f9ebd9250d 100644
@@ -118,10 +118,8 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 }
 
 static const u32 channel_formats1[] = {
-	DRM_FORMAT_RGB565, DRM_FORMAT_BGR565, DRM_FORMAT_RGB888,
-	DRM_FORMAT_BGR888, DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888,
-	DRM_FORMAT_RGBA8888, DRM_FORMAT_BGRA8888, DRM_FORMAT_ARGB8888,
-	DRM_FORMAT_ABGR8888
+	DRM_FORMAT_XRGB8888,
+	DRM_FORMAT_RGB565,
 };
 
 static const struct drm_plane_funcs hibmc_plane_funcs = {
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 2/4
#8 lore raw

Re: [PATCH 2/4] drm/hibmc: Fix list of formats on the primary plane

Quoted 7 lines

Remove all formats from the primary plane that are unsupported for various reasons.

* Formats with alpha channel: planes should not announce alpha channels unless they support transparency. There's no transparency support in the primary plane's implementation.

Quoted 8 lines

* Formats with BGR order. The common format is in RGB channel order. There's no BGR support in the primary plane's implementation.

* RGB888: atomic_update programs the format from cpp[0] * 8 / 16. For RGB888's cpp value of 3 this returns 1.5; rounded to 1. Programming the value of 1 to HIBMC_CRT_DISP_CTL_FORMAT sets up RGB565. Hence, the output is distorted. This can be tested by booting with video=1024x768-24.

I tested this method and was able to reproduce the issue. Thank you for pointing it out.

Quoted 3 lines

Removing all unsupported formats leaves XRGB8888 and RGB565. Both of which are supported and work correctly.

You're right, I checked the DataSheet and confirmed that it only supports RGB565 and XRGB8888.

Also, I looked at the commit history, and this format was already present in the first version of the hibmc driver. This is a historic issue, and it's a bit difficult to determine why it was implemented this way.

Quoted 20 lines
Fixes da52605eea8f ("drm/hisilicon/hibmc: Add support for display engine")
Cc Rongrong Zou <[email protected]>
Cc Sean Paul <[email protected]>
Cc Xinliang Liu <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Yongbang Shi <[email protected]>
Cc Baihan Li <[email protected]>
Cc <[email protected]> # v4.10+
---
 drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +0 -0
index 8fa2a95bcdd1..c4f9ebd9250d 100644
@@ -118,10 +118,8 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 }
 

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c static void hibmc_plane_atomic_update(struct drm_plane *plane, · line 125

Quoted 10 lines
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +2 -4
 static const u32 channel_formats1[] = {
-	DRM_FORMAT_RGB565, DRM_FORMAT_BGR565, DRM_FORMAT_RGB888,
-	DRM_FORMAT_BGR888, DRM_FORMAT_XRGB8888, DRM_FORMAT_XBGR8888,
-	DRM_FORMAT_RGBA8888, DRM_FORMAT_BGRA8888, DRM_FORMAT_ARGB8888,
-	DRM_FORMAT_ABGR8888
+	DRM_FORMAT_XRGB8888,
+	DRM_FORMAT_RGB565,
 };
 
 static const struct drm_plane_funcs hibmc_plane_funcs = {
author maintainer
#9 lore raw

Re: [PATCH 2/4] drm/hibmc: Fix list of formats on the primary plane

Hi

Am 15.04.26 um 05:19 schrieb Yongbang Shi:
[...]

Quoted 4 lines

Also, I looked at the commit history, and this format was already present in the first version of the hibmc driver. This is a historic issue, and it's a bit difficult to determine why it was implemented this way.

It's probably left over from the prototype. Back then, DRM was also less well structured and it might have been harder to see what's really needed.

Quoted 2 lines
Quoted 2 lines

Thanks.

Best regards
Thomas

Quoted 1 line
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)
author maintainer
#10 lore raw

PATCH 3/4 drm/hibmc: Do not use cpp from struct drm_format_info

In reply to Thomas Zimmermann

Replace uses of struct drm_format_info's cpp with appropriate interfaces. The cpp field contains the characters per pixel. It is deprecated and should be avoided.

Calculate the line width in bytes with drm_format_info_min_pitch(). This is the preferred way of getting pixel and line sizes.

Program HIB_CRT_DISP_CTL_FORMAT from the format's 4CC code instead of calculating the field's value from the cpp.

---
 drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +11 -5
index c4f9ebd9250d..20ab933b0f12 100644
@@ -83,8 +83,8 @@ static int hibmc_plane_atomic_check(struct drm_plane *plane,
 static void hibmc_plane_atomic_update(struct drm_plane *plane,
 				      struct drm_atomic_state *state)
 {
-	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
-									   plane);
+	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
+	struct drm_framebuffer *fb = new_state->fb;
 	u32 reg;
 	s64 gpu_addr = 0;
 	u32 line_l;
@@ -102,7 +102,7 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 
 	writel(gpu_addr, priv->mmio + HIBMC_CRT_FB_ADDRESS);
 
-	reg = new_state->fb->width * (new_state->fb->format->cpp[0]);
+	reg = drm_format_info_min_pitch(fb->format, 0, fb->width);
 
 	line_l = new_state->fb->pitches[0];
 	writel(HIBMC_FIELD(HIBMC_CRT_FB_WIDTH_WIDTH, reg) |
@@ -112,8 +112,14 @@ static void hibmc_plane_atomic_update(struct drm_plane *plane,
 	/* SET PIXEL FORMAT */
 	reg = readl(priv->mmio + HIBMC_CRT_DISP_CTL);
 	reg &= ~HIBMC_CRT_DISP_CTL_FORMAT_MASK;
-	reg |= HIBMC_FIELD(HIBMC_CRT_DISP_CTL_FORMAT,
-			   new_state->fb->format->cpp[0] * 8 / 16);
+	switch (fb->format->format) {
+	case DRM_FORMAT_XRGB8888:
+		reg |= HIBMC_FIELD(HIBMC_CRT_DISP_CTL_FORMAT, 2);
+		break;
+	case DRM_FORMAT_RGB565:
+		reg |= HIBMC_FIELD(HIBMC_CRT_DISP_CTL_FORMAT, 1);
+		break;
+	}
 	writel(reg, priv->mmio + HIBMC_CRT_DISP_CTL);
 }
 
Signature · 1 line
2.53.0
author maintainer
#11 lore raw

PATCH 4/4 drm/hibmc: Use gem-shmem with shadow-plane helpers for memory management

In reply to Thomas Zimmermann

Replace the gem-vram memory manager with gem-shmem. Makes the driver more robust and enables dma-buf sharing with other hardware.

Gem-vram was created from various drivers that used TTM for their memory management. All these drivers have meanwhile been converted to gem-shmem. Using gem-vram is deprecated because it has several problems.

* TTM requires significant overcommitment of video memory for reliable page flips. There needs to be 3 times the size of the largest possible framebuffer available or page flips can fail. This leaves the display dark without further warning. Hibmc hardware with 32 MiB and a maximum framebuffer size of 1920x2000 is at the limit.

* No dma-buf sharing without GTT support. Neither gem-vram nor hibmc hardware support a GTT address space. This is required to share buffers with other devices via dma-buf interfaces.

* TTM requires hardware-accelerated rendering into video memory for optimal results. As hibmc hardware cannot do this, hibmc renders in system memory and copies the result to video memory. This can be more effectively implemented with gem-shmem and DRM's shadow-plane helpers.

Converting hibmc to gem-shmem and shadow-plane helpers.

* Replace gem-vram entry points in struct drm_driver with gem-shmem equivalents. This makes the driver allocate struct drm_gem_shmem_object for its buffers.

* Use DRM_GEM_SHADOW_*_PLANE for its plane funcs and plane-helper funcs. The shadow-plane helpers map a plane's gem buffer objects into kernel address space during a page flip, so that atomic_update can copy them to video memory.

* Handle framebuffer damage in hibmc_plane_atomic_update(). This updates video memory from the plane's framebuffer. It automatically synchronizes shared buffers with other devices. Create the framebuffer with drm_gem_fb_create_with_dirty() to trigger the update on each page flip.

* Initialize the plane with drm_plane_enable_fb_damage_clips() to limit the damage updates to the framebuffer areas that changed. We don't want to do a full-buffer memcpy if only a small area has changed.

* Test display modes against the available video memory in hibmc_mode_config_mode_valid(). We only want to announce display modes that fit into display memory.

* Map the display memory itself into kernel address space.

* Do not set drm_mode_config.prefer_shadow. This would advise user space to install a shadow buffer. But with gem-shmem, the gem buffer object already acts as a shadow buffer for video memory.

We use these patterns in many other drivers with similar limitation as hibmc and its hardware. With these changes in place, hibmc is more robust and better integrated into the overall DRM framework.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c        | 17 ++++-
 drivers/gpu/drm/hisilicon/hibmc/Kconfig       |  2 +-
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_de.c    | 42 ++++++++-----
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c   | 62 ++++++++++++++-----
 .../gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h   |  5 ++
 include/drm/drm_gem_shmem_helper.h            |  5 ++
 6 files changed, 101 insertions(+), 32 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +16 -1
index 545933c7f712..c651245f8e91 100644
@@ -453,7 +453,21 @@ void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem,
 }
 EXPORT_SYMBOL_GPL(drm_gem_shmem_vunmap_locked);
 
-static int
+/**
+ * drm_gem_shmem_create_with_handle - Allocate an object with the given size and
+ *	returns a GEM handle
+ * @file_priv: DRM file structure to create the dumb buffer for
+ * @dev: DRM device
+ * @size: Size of the object to allocate
+ * @handle: Returns the GEM handle on success
+ *
+ * Allocates an shmem GEM buffer using drm_gem_shmem_create() and returns
+ * a GEM handle to it.
+ *
+ * Returns:
+ * Zero on success, or an error code otherwise.
+ */
+int
 drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
 				 struct drm_device *dev, size_t size,
 				 uint32_t *handle)
@@ -475,6 +489,7 @@ drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
 
 	return ret;
 }
+EXPORT_SYMBOL_GPL(drm_gem_shmem_create_with_handle);
 
 /* Update madvise status, returns true if not purged, else
  * false or -errno.
drivers/gpu/drm/hisilicon/hibmc/Kconfig +1 -1
index d1f3f5793f34..439258338460 100644
@@ -5,8 +5,8 @@ config DRM_HISI_HIBMC
 	select DRM_CLIENT_SELECTION
 	select DRM_DISPLAY_HELPER
 	select DRM_DISPLAY_DP_HELPER
+	select DRM_GEM_SHMEM_HELPER
 	select DRM_KMS_HELPER
-	select DRM_VRAM_HELPER
 	select DRM_TTM
 	select DRM_TTM_HELPER
 	select I2C
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +28 -14

83 lines not on this page · raw message

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c +46 -16

108 lines not on this page · raw message

drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h +5 -0
index ca8502e2760c..4e212af6143d 100644
@@ -37,6 +37,11 @@ struct hibmc_drm_private {
 	/* hw */
 	void __iomem   *mmio;
 
+	/* vram */
+	void __iomem *vram;
+	resource_size_t	vram_base;
+	resource_size_t	vram_size;
+
 	/* drm */
 	struct drm_device dev;
 	struct drm_plane primary_plane;
include/drm/drm_gem_shmem_helper.h +5 -0
index 5ccdae21b94a..8167a1d6f997 100644
@@ -141,6 +141,11 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
 			      struct drm_printer *p, unsigned int indent);
 
+int
+drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
+				 struct drm_device *dev, size_t size,
+				 uint32_t *handle);
+
 extern const struct vm_operations_struct drm_gem_shmem_vm_ops;
 
 /*
Signature · 1 line
2.53.0
author maintainer
#12 lore raw

Re: [PATCH 4/4] drm/hibmc: Use gem-shmem with shadow-plane helpers for memory management

Am 13.04.26 um 10:40 schrieb Thomas Zimmermann:

Quoted 7 lines

/* Update madvise status, returns true if not purged, else
* false or -errno.

drivers/gpu/drm/hisilicon/hibmc/Kconfig +0 -0
index d1f3f5793f34..439258338460 100644

drivers/gpu/drm/hisilicon/hibmc/Kconfig config DRM_HISI_HIBMC · line 11

Quoted 8 lines
drivers/gpu/drm/hisilicon/hibmc/Kconfig +1 -1
@@ -5,8 +5,8 @@ config DRM_HISI_HIBMC
  	select DRM_CLIENT_SELECTION
  	select DRM_DISPLAY_HELPER
  	select DRM_DISPLAY_DP_HELPER
+	select DRM_GEM_SHMEM_HELPER
  	select DRM_KMS_HELPER
-	select DRM_VRAM_HELPER
  	select DRM_TTM
  	select DRM_TTM_HELPER

I noticed that I forgot to remove the dependencies on TTM. I'll update the patch in the next iteration.

Quoted 242 lines
drivers/gpu/drm/hisilicon/hibmc/Kconfig +0 -0
  	select I2C
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_de.c +28 -14
index 20ab933b0f12..fd3f05ba62df 100644
@@ -15,8 +15,10 @@
 
  #include <drm/drm_atomic.h>
  #include <drm/drm_atomic_helper.h>
+#include <drm/drm_damage_helper.h>
  #include <drm/drm_fourcc.h>
-#include <drm/drm_gem_vram_helper.h>
+#include <drm/drm_gem_atomic_helper.h>
+#include <drm/drm_gem_framebuffer_helper.h>
  #include <drm/drm_vblank.h>
 
  #include "hibmc_drm_drv.h"
@@ -83,28 +85,41 @@ static int hibmc_plane_atomic_check(struct drm_plane *plane,
  static void hibmc_plane_atomic_update(struct drm_plane *plane,
  				      struct drm_atomic_state *state)
  {
+	struct hibmc_drm_private *priv = to_hibmc_drm_private(plane->dev);
  	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
+	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(new_state);
  	struct drm_framebuffer *fb = new_state->fb;
+	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
+	u32 gpu_addr = 0;
  	u32 reg;
-	s64 gpu_addr = 0;
  	u32 line_l;
-	struct hibmc_drm_private *priv = to_hibmc_drm_private(plane->dev);
-	struct drm_gem_vram_object *gbo;
 
-	if (!new_state->fb)
+	if (!fb)
  		return;
 
-	gbo = drm_gem_vram_of_gem(new_state->fb->obj[0]);
+	if (drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE) == 0) {
+		struct drm_rect damage;
+		struct drm_atomic_helper_damage_iter iter;
+
+		drm_atomic_helper_damage_iter_init(&iter, old_state, new_state);
+		drm_atomic_for_each_plane_damage(&iter, &damage) {
+			struct iosys_map dst[DRM_FORMAT_MAX_PLANES] = {
+				IOSYS_MAP_INIT_VADDR_IOMEM(priv->vram + gpu_addr),
+			};
 
-	gpu_addr = drm_gem_vram_offset(gbo);
-	if (WARN_ON_ONCE(gpu_addr < 0))
-		return; /* Bug: we didn't pin the BO to VRAM in prepare_fb. */
+			iosys_map_incr(&dst[0],
+				       drm_fb_clip_offset(fb->pitches[0], fb->format, &damage));
+			drm_fb_memcpy(dst, fb->pitches, shadow_plane_state->data, fb, &damage);
+		}
+
+		drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
+	}
 
  	writel(gpu_addr, priv->mmio + HIBMC_CRT_FB_ADDRESS);
 
  	reg = drm_format_info_min_pitch(fb->format, 0, fb->width);
 
-	line_l = new_state->fb->pitches[0];
+	line_l = fb->pitches[0];
  	writel(HIBMC_FIELD(HIBMC_CRT_FB_WIDTH_WIDTH, reg) |
  	       HIBMC_FIELD(HIBMC_CRT_FB_WIDTH_OFFS, line_l),
  	       priv->mmio + HIBMC_CRT_FB_WIDTH);
@@ -132,13 +147,11 @@ static const struct drm_plane_funcs hibmc_plane_funcs = {
  	.update_plane	= drm_atomic_helper_update_plane,
  	.disable_plane	= drm_atomic_helper_disable_plane,
  	.destroy = drm_plane_cleanup,
-	.reset = drm_atomic_helper_plane_reset,
-	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
-	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
+	DRM_GEM_SHADOW_PLANE_FUNCS,
  };
 
  static const struct drm_plane_helper_funcs hibmc_plane_helper_funcs = {
-	DRM_GEM_VRAM_PLANE_HELPER_FUNCS,
+	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
  	.atomic_check = hibmc_plane_atomic_check,
  	.atomic_update = hibmc_plane_atomic_update,
  };
@@ -505,6 +518,7 @@ int hibmc_de_init(struct hibmc_drm_private *priv)
  	}
 
  	drm_plane_helper_add(plane, &hibmc_plane_helper_funcs);
+	drm_plane_enable_fb_damage_clips(plane);
 
  	ret = drm_crtc_init_with_planes(dev, crtc, plane,
  					NULL, &hibmc_crtc_funcs, NULL);
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.c +46 -16
index 289304500ab0..a0ecf82b576f 100644
@@ -18,9 +18,10 @@
  #include <drm/clients/drm_client_setup.h>
  #include <drm/drm_atomic_helper.h>
  #include <drm/drm_drv.h>
-#include <drm/drm_fbdev_ttm.h>
+#include <drm/drm_dumb_buffers.h>
+#include <drm/drm_fbdev_shmem.h>
  #include <drm/drm_gem_framebuffer_helper.h>
-#include <drm/drm_gem_vram_helper.h>
+#include <drm/drm_gem_shmem_helper.h>
  #include <drm/drm_managed.h>
  #include <drm/drm_module.h>
  #include <drm/drm_vblank.h>
@@ -70,7 +71,13 @@ static irqreturn_t hibmc_dp_interrupt(int irq, void *arg)
  static int hibmc_dumb_create(struct drm_file *file, struct drm_device *dev,
  			     struct drm_mode_create_dumb *args)
  {
-	return drm_gem_vram_fill_create_dumb(file, dev, 0, 128, args);
+	int ret;
+
+	ret = drm_mode_size_dumb(dev, args, SZ_128, 0);
+	if (ret)
+		return ret;
+
+	return drm_gem_shmem_create_with_handle(file, dev, args->size, &args->handle);
  }
 
  static const struct drm_driver hibmc_driver = {
@@ -80,10 +87,9 @@ static const struct drm_driver hibmc_driver = {
  	.desc			= "hibmc drm driver",
  	.major			= 1,
  	.minor			= 0,
-	.debugfs_init		= drm_vram_mm_debugfs_init,
-	.dumb_create            = hibmc_dumb_create,
-	.dumb_map_offset        = drm_gem_ttm_dumb_map_offset,
-	DRM_FBDEV_TTM_DRIVER_OPS,
+	.gem_prime_import	= drm_gem_shmem_prime_import_no_map,
+	.dumb_create		= hibmc_dumb_create,
+	DRM_FBDEV_SHMEM_DRIVER_OPS,
  };
 
  static int __maybe_unused hibmc_pm_suspend(struct device *dev)
@@ -105,11 +111,32 @@ static const struct dev_pm_ops hibmc_pm_ops = {
  				hibmc_pm_resume)
  };
 
+static enum drm_mode_status hibmc_mode_config_mode_valid(struct drm_device *dev,
+							 const struct drm_display_mode *mode)
+{
+	const struct drm_format_info *info =
+		drm_get_format_info(dev, DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_LINEAR);
+	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
+	unsigned long max_fb_size = priv->vram_size;
+	u64 pitch;
+
+	if (drm_WARN_ON_ONCE(dev, !info))
+		return MODE_ERROR; /* driver bug */
+
+	pitch = drm_format_info_min_pitch(info, 0, mode->hdisplay);
+	if (!pitch)
+		return MODE_BAD_WIDTH;
+	else if (pitch > max_fb_size / mode->vdisplay)
+		return MODE_MEM;
+
+	return MODE_OK;
+}
+
  static const struct drm_mode_config_funcs hibmc_mode_funcs = {
-	.mode_valid = drm_vram_helper_mode_valid,
+	.mode_valid = hibmc_mode_config_mode_valid,
  	.atomic_check = drm_atomic_helper_check,
  	.atomic_commit = drm_atomic_helper_commit,
-	.fb_create = drm_gem_fb_create,
+	.fb_create = drm_gem_fb_create_with_dirty,
  };
 
  static int hibmc_kms_init(struct hibmc_drm_private *priv)
@@ -129,7 +156,6 @@ static int hibmc_kms_init(struct hibmc_drm_private *priv)
  	dev->mode_config.max_height = 1200;
 
  	dev->mode_config.preferred_depth = 24;
-	dev->mode_config.prefer_shadow = 1;
 
  	dev->mode_config.funcs = (void *)&hibmc_mode_funcs;
 
@@ -323,18 +349,22 @@ static int hibmc_load(struct drm_device *dev)
  {
  	struct pci_dev *pdev = to_pci_dev(dev->dev);
  	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
+	resource_size_t vram_base, vram_size;
  	int ret;
 
  	ret = hibmc_hw_init(priv);
  	if (ret)
  		return ret;
 
-	ret = drmm_vram_helper_init(dev, pci_resource_start(pdev, 0),
-				    pci_resource_len(pdev, 0));
-	if (ret) {
-		drm_err(dev, "Error initializing VRAM MM; %d\n", ret);
-		return ret;
-	}
+	vram_base = pci_resource_start(pdev, 0);
+	vram_size = pci_resource_len(pdev, 0);
+
+	priv->vram = devm_ioremap_wc(dev->dev, vram_base, vram_size);
+	if (!priv->vram)
+		return -ENOMEM;
+
+	priv->vram_base = vram_base;
+	priv->vram_size = vram_size;
 
  	ret = hibmc_kms_init(priv);
  	if (ret)
drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_drv.h +5 -0
index ca8502e2760c..4e212af6143d 100644
@@ -37,6 +37,11 @@ struct hibmc_drm_private {
  	/* hw */
  	void __iomem   *mmio;
 
+	/* vram */
+	void __iomem *vram;
+	resource_size_t	vram_base;
+	resource_size_t	vram_size;
+
  	/* drm */
  	struct drm_device dev;
  	struct drm_plane primary_plane;
include/drm/drm_gem_shmem_helper.h +5 -0
index 5ccdae21b94a..8167a1d6f997 100644
@@ -141,6 +141,11 @@ struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem)
  void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
  			      struct drm_printer *p, unsigned int indent);
 
+int
+drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
+				 struct drm_device *dev, size_t size,
+				 uint32_t *handle);
+
  extern const struct vm_operations_struct drm_gem_shmem_vm_ops;
 
  /*
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Jochen Jaser, Andrew McDonald, Werner Knoblich, (HRB 36809, AG Nürnberg)