Skip to contentSkip to conversation

drm: Add and use drm_fb_dma_get_gem_clipped_addr() helper

PATCH In review Chen-Yu Tsai 2 participants 10 messages Nov 24

Subsystems (9)
lore
Share & tools
lore
#0 lore raw

v3 cover drm: Add and use drm_fb_dma_get_gem_clipped_addr() helper

Hi,

This is v3 of my drm_fb_dma_get_gem_clipped_addr() series.

Changes since v2:
- Change drm_framebuffer_get_block_offset() offset variable and return type to u64, and use mul_u32_u32() to avoid 32-bit overflow (Thomas)

Changes since v1:
- Add and use new drm_framebuffer_get_block_offset() helper (Thomas)

This series adds a helper to retrieve the buffer starting address of a "clipped" framebuffer. This contrasts with drm_fb_dma_get_gem_addr(), which gives the address of the full buffer.

Some drivers program their hardware with clipped dimensions, so they should be using the clipped buffer address as well, unless the hardware can advance the scanout directly. (Side note: many drivers still use the non-clipped dimensions.)

While at it, also pull out the offset calculation of drm_fb_dma_get_gem_addr() into a separate helper in drm_framebuffer.[ch], thereby separating responsibilities.

The sun4i driver was recently incorrectly converted to use the unclipped drm_fb_dma_get_gem_addr() helper. This broke offsets into subsampled pixel groups, but also exposed the mismatch between the dimensions used vs the buffer address. Two other drivers were also touched.

Patch 1 adds a new helper to return the byte offset into a framebuffer for the start of the pixel block of the given pixel coordinates.

Patch 2 adds the new helper to return the buffer address based on clipped coordinates.

Patch 3 switches the sun4i driver to the new helper, and fixes the luma plane buffer address offset for subsampled YUV formats.

Patch 4 converts the imx/dc driver to use the new helper. This fixes a mismatch between the programmed coordinates and the buffer address.

Patch 5 replaces the open coded buffer address calculation in the imx/dcss driver with the new helper. Existing behavior, which might be wrong, is preserved.

Please help test. The series is only compile tested on my end. The sun4i changes should revert its behavior to before the drm_fb_dma_get_gem_addr() helper was adopted. The imx/dcss changes should not have any behavioral difference.

Thanks
ChenYu

Chen-Yu Tsai (5):
drm: Split framebuffer pixel offset calculation from

    drm_fb_dma_get_gem_addr()

drm/fb-dma-helper: Add drm_fb_dma_get_gem_clipped_addr() drm/sun4i: layers: Fix VI buffer address for clipped offsets drm/imx/dc: plane: Switch to drm_fb_dma_get_gem_clipped_addr() drm/imx/dcss: plane: Switch to drm_fb_dma_get_gem_clipped_addr()

 drivers/gpu/drm/drm_fb_dma_helper.c    | 61 ++++++++++++++------------
 drivers/gpu/drm/drm_framebuffer.c      | 46 +++++++++++++++++++
 drivers/gpu/drm/imx/dc/dc-plane.c      |  4 +-
 drivers/gpu/drm/imx/dcss/dcss-plane.c  | 34 ++++++--------
 drivers/gpu/drm/sun4i/sun8i_ui_layer.c |  2 +-
 drivers/gpu/drm/sun4i/sun8i_vi_layer.c | 16 ++++++-
 include/drm/drm_fb_dma_helper.h        |  4 ++
 include/drm/drm_framebuffer.h          |  3 ++
 8 files changed, 119 insertions(+), 51 deletions(-)
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
#1 lore raw

PATCH v3 1/5 drm: Split framebuffer pixel offset calculation from drm_fb_dma_get_gem_addr()

Currently drm_fb_dma_get_gem_addr() calculates the offset into the framebuffer memory for the framebuffer's unclipped source coordinates, adds that to the framebuffer's backing storage, and returns the result.

We are about to add a variant that uses the clipped source coordinates, so there is already some reuse of code. However, calculating the data offset for a given pixel is not specific to the DMA FB helpers. The offset is only related to the framebuffer.

Split out the offset calculation into a new framebuffer helper so that non-DMA users can also reuse the same code.

Cc <[email protected]> # dependency for next patch

---
fb->offsets and fb->pitches are (or should have been) checked by the driver's .fb_create callback, either in drm_gem_fb_init_with_funcs() or reimplementing the checks directly.

Changes since v2:
- Change offset local variable and return type to u64, and use mul_u32_u32() to avoid 32-bit overflow before widening
Changes since v1:
- New patch

---
 drivers/gpu/drm/drm_fb_dma_helper.c | 28 ++----------------
 drivers/gpu/drm/drm_framebuffer.c   | 46 +++++++++++++++++++++++++++++
 include/drm/drm_framebuffer.h       |  3 ++
 3 files changed, 52 insertions(+), 25 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +3 -25
index fd71969d2fb1..ab0f37d8a5ff 100644
@@ -75,36 +75,14 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
 				   unsigned int plane)
 {
 	struct drm_gem_dma_object *obj;
-	dma_addr_t dma_addr;
-	u8 h_div = 1, v_div = 1;
-	u32 block_w = drm_format_info_block_width(fb->format, plane);
-	u32 block_h = drm_format_info_block_height(fb->format, plane);
-	u32 block_size = fb->format->char_per_block[plane];
-	u32 sample_x;
-	u32 sample_y;
-	u32 block_start_y;
-	u32 num_hblocks;
 
 	obj = drm_fb_dma_get_gem_obj(fb, plane);
 	if (!obj)
 		return 0;
 
-	dma_addr = obj->dma_addr + fb->offsets[plane];
-
-	if (plane > 0) {
-		h_div = fb->format->hsub;
-		v_div = fb->format->vsub;
-	}
-
-	sample_x = (state->src_x >> 16) / h_div;
-	sample_y = (state->src_y >> 16) / v_div;
-	block_start_y = (sample_y / block_h) * block_h;
-	num_hblocks = sample_x / block_w;
-
-	dma_addr += fb->pitches[plane] * block_start_y;
-	dma_addr += block_size * num_hblocks;
-
-	return dma_addr;
+	return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+								state->src_x >> 16,
+								state->src_y >> 16);
 }
 EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
 
drivers/gpu/drm/drm_framebuffer.c +46 -0

58 lines not on this page · raw message

include/drm/drm_framebuffer.h +3 -0
index 38b24fc8978d..6f838b485730 100644
@@ -220,6 +220,9 @@ void drm_framebuffer_remove(struct drm_framebuffer *fb);
 void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
 
+u64 drm_framebuffer_get_block_offset(struct drm_framebuffer *fb, unsigned int plane,
+				     unsigned int x, unsigned int y);
+
 /**
  * drm_framebuffer_get - acquire a framebuffer reference
  * @fb: DRM framebuffer
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
maintainer Reviewed-by on v3 1/5
#2 lore raw

Re: [PATCH RFT v3 1/5] drm: Split framebuffer pixel offset calculation from drm_fb_dma_get_gem_addr()

Hi

Am 24.11.26 um 08:39 schrieb Chen-Yu Tsai:

Quoted 9 lines

Currently drm_fb_dma_get_gem_addr() calculates the offset into the framebuffer memory for the framebuffer's unclipped source coordinates, adds that to the framebuffer's backing storage, and returns the result.

We are about to add a variant that uses the clipped source coordinates, so there is already some reuse of code. However, calculating the data offset for a given pixel is not specific to the DMA FB helpers. The offset is only related to the framebuffer.

Quoted 6 lines

Split out the offset calculation into a new framebuffer helper so that non-DMA users can also reuse the same code.

Cc <[email protected]> # dependency for next patch

This patch is useful on its own and could be merged independently from the others.

Best regards
Thomas

Quoted 138 lines

---
fb->offsets and fb->pitches are (or should have been) checked by the driver's .fb_create callback, either in drm_gem_fb_init_with_funcs() or reimplementing the checks directly.

Changes since v2:
- Change offset local variable and return type to u64, and use mul_u32_u32() to avoid 32-bit overflow before widening
Changes since v1:
- New patch

---
  drivers/gpu/drm/drm_fb_dma_helper.c | 28 ++----------------
  drivers/gpu/drm/drm_framebuffer.c   | 46 +++++++++++++++++++++++++++++
  include/drm/drm_framebuffer.h       |  3 ++
  3 files changed, 52 insertions(+), 25 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +3 -25
index fd71969d2fb1..ab0f37d8a5ff 100644
@@ -75,36 +75,14 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
  				   unsigned int plane)
  {
  	struct drm_gem_dma_object *obj;
-	dma_addr_t dma_addr;
-	u8 h_div = 1, v_div = 1;
-	u32 block_w = drm_format_info_block_width(fb->format, plane);
-	u32 block_h = drm_format_info_block_height(fb->format, plane);
-	u32 block_size = fb->format->char_per_block[plane];
-	u32 sample_x;
-	u32 sample_y;
-	u32 block_start_y;
-	u32 num_hblocks;
 
  	obj = drm_fb_dma_get_gem_obj(fb, plane);
  	if (!obj)
  		return 0;
 
-	dma_addr = obj->dma_addr + fb->offsets[plane];
-
-	if (plane > 0) {
-		h_div = fb->format->hsub;
-		v_div = fb->format->vsub;
-	}
-
-	sample_x = (state->src_x >> 16) / h_div;
-	sample_y = (state->src_y >> 16) / v_div;
-	block_start_y = (sample_y / block_h) * block_h;
-	num_hblocks = sample_x / block_w;
-
-	dma_addr += fb->pitches[plane] * block_start_y;
-	dma_addr += block_size * num_hblocks;
-
-	return dma_addr;
+	return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+								state->src_x >> 16,
+								state->src_y >> 16);
  }
  EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
 
drivers/gpu/drm/drm_framebuffer.c +46 -0
index d32aceb6ca9b..589619bd8f0b 100644
@@ -21,6 +21,7 @@
   */
 
  #include <linux/export.h>
+#include <linux/math64.h>
  #include <linux/uaccess.h>
 
  #include <drm/drm_atomic.h>
@@ -1208,6 +1209,51 @@ void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent,
  	}
  }
 
+/**
+ * drm_framebuffer_get_block_offset() - Get offset to start of pixel block for
+ * the given framebuffer and coordinates.
+ * @fb: The framebuffer
+ * @plane: Which plane
+ * @x: x coordinate for pixel
+ * @y: y coordinate for pixel
+ *
+ * This function will usually be called from the PLANE callback functions,
+ * or from one of the helpers that calculates the framebuffer's DMA address.
+ *
+ * Return: offset from start of framebuffer to start of pixel block
+ */
+u64 drm_framebuffer_get_block_offset(struct drm_framebuffer *fb, unsigned int plane,
+				     unsigned int x, unsigned int y)
+{
+	u8 h_div = 1, v_div = 1;
+	u32 block_w = drm_format_info_block_width(fb->format, plane);
+	u32 block_h = drm_format_info_block_height(fb->format, plane);
+	u32 block_size = fb->format->char_per_block[plane];
+	u32 sample_x;
+	u32 sample_y;
+	u32 block_start_y;
+	u32 num_hblocks;
+	u64 offset;
+
+	offset = fb->offsets[plane];
+
+	if (plane > 0) {
+		h_div = fb->format->hsub;
+		v_div = fb->format->vsub;
+	}
+
+	sample_x = x / h_div;
+	sample_y = y / v_div;
+	block_start_y = (sample_y / block_h) * block_h;
+	num_hblocks = sample_x / block_w;
+
+	offset += mul_u32_u32(fb->pitches[plane], block_start_y);
+	offset += mul_u32_u32(block_size, num_hblocks);
+
+	return offset;
+}
+EXPORT_SYMBOL(drm_framebuffer_get_block_offset);
+
  #ifdef CONFIG_DEBUG_FS
  static int drm_framebuffer_info(struct seq_file *m, void *data)
  {
include/drm/drm_framebuffer.h +3 -0
index 38b24fc8978d..6f838b485730 100644
@@ -220,6 +220,9 @@ void drm_framebuffer_remove(struct drm_framebuffer *fb);
  void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
  void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
 
+u64 drm_framebuffer_get_block_offset(struct drm_framebuffer *fb, unsigned int plane,
+				     unsigned int x, unsigned int y);
+
  /**
   * drm_framebuffer_get - acquire a framebuffer reference
   * @fb: DRM framebuffer
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
#3 lore raw

PATCH v3 2/5 drm/fb-dma-helper: Add drm_fb_dma_get_gem_clipped_addr()

In reply to Chen-Yu Tsai

drm_fb_dma_get_gem_addr() returns the DMA address to the "unclipped" framebuffer. However some display drivers want the "clipped" framebuffer instead, as they are also using the clipped coordinates to program the hardware.

Some of these drivers are open-coding drm_fb_dma_get_gem_addr() with the source coordinates replaced, while others have been incorrectly converted to using drm_fb_dma_get_gem_addr(), which would end up causing incorrect parts of the framebuffer to be displayed if it were somehow clipped.

Add drm_fb_dma_get_gem_clipped_addr(), a "clipped" version of drm_fb_dma_get_gem_addr() for these drivers to use.

Cc <[email protected]> # dependency for next patch

---
Changes since v1:
- Use new drm_framebuffer_get_block_offset() helper

---
 drivers/gpu/drm/drm_fb_dma_helper.c | 37 +++++++++++++++++++++++++----
 include/drm/drm_fb_dma_helper.h     |  4 ++++
 2 files changed, 37 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +33 -4
index ab0f37d8a5ff..0aaf4926db4e 100644
@@ -60,15 +60,16 @@ struct drm_gem_dma_object *drm_fb_dma_get_gem_obj(struct drm_framebuffer *fb,
 EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_obj);
 
 /**
- * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for framebuffer, for pixel
- * formats where values are grouped in blocks this will get you the beginning of
- * the block
+ * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for unclipped framebuffer,
+ * for pixel formats where values are grouped in blocks this will get you the
+ * beginning of the block
  * @fb: The framebuffer
  * @state: Which state of drm plane
  * @plane: Which plane
- * Return the DMA GEM address for given framebuffer.
  *
  * This function will usually be called from the PLANE callback functions.
+ *
+ * Return: GEM DMA address for given framebuffer, unclipped.
  */
 dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
 				   struct drm_plane_state *state,
@@ -86,6 +87,34 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
 }
 EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
 
+/**
+ * drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for clipped
+ * framebuffer, for pixel formats where values are grouped in blocks this
+ * will get you the beginning of the block
+ * @fb: The framebuffer
+ * @state: Which state of drm plane
+ * @plane: Which plane
+ *
+ * This function will usually be called from the PLANE callback functions.
+ *
+ * Return: GEM DMA address for given framebuffer, clipped.
+ */
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+					   struct drm_plane_state *state,
+					   unsigned int plane)
+{
+	struct drm_gem_dma_object *obj;
+
+	obj = drm_fb_dma_get_gem_obj(fb, plane);
+	if (!obj)
+		return 0;
+
+	return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+								state->src.x1 >> 16,
+								state->src.y1 >> 16);
+}
+EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_clipped_addr);
+
 /**
  * drm_fb_dma_sync_non_coherent - Sync GEM object to non-coherent backing
  *	memory
include/drm/drm_fb_dma_helper.h +4 -0
index c950732c6d36..b2a0bd7ef9d0 100644
@@ -17,6 +17,10 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
 				   struct drm_plane_state *state,
 				   unsigned int plane);
 
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+					   struct drm_plane_state *state,
+					   unsigned int plane);
+
 void drm_fb_dma_sync_non_coherent(struct drm_device *drm,
 				  struct drm_plane_state *old_state,
 				  struct drm_plane_state *state);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
maintainer Reviewed-by on v3 2/5
#4 lore raw

Re: [PATCH RFT v3 2/5] drm/fb-dma-helper: Add drm_fb_dma_get_gem_clipped_addr()

Hi

Am 24.11.26 um 08:39 schrieb Chen-Yu Tsai:

Quoted 52 lines

drm_fb_dma_get_gem_addr() returns the DMA address to the "unclipped" framebuffer. However some display drivers want the "clipped" framebuffer instead, as they are also using the clipped coordinates to program the hardware.

Some of these drivers are open-coding drm_fb_dma_get_gem_addr() with the source coordinates replaced, while others have been incorrectly converted to using drm_fb_dma_get_gem_addr(), which would end up causing incorrect parts of the framebuffer to be displayed if it were somehow clipped.

Add drm_fb_dma_get_gem_clipped_addr(), a "clipped" version of drm_fb_dma_get_gem_addr() for these drivers to use.

Cc <[email protected]> # dependency for next patch

---
Changes since v1:
- Use new drm_framebuffer_get_block_offset() helper

---
  drivers/gpu/drm/drm_fb_dma_helper.c | 37 +++++++++++++++++++++++++----
  include/drm/drm_fb_dma_helper.h     |  4 ++++
  2 files changed, 37 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +5 -4
index ab0f37d8a5ff..0aaf4926db4e 100644
@@ -60,15 +60,16 @@ struct drm_gem_dma_object *drm_fb_dma_get_gem_obj(struct drm_framebuffer *fb,
  EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_obj);
 
  /**
- * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for framebuffer, for pixel
- * formats where values are grouped in blocks this will get you the beginning of
- * the block
+ * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for unclipped framebuffer,
+ * for pixel formats where values are grouped in blocks this will get you the
+ * beginning of the block
   * @fb: The framebuffer
   * @state: Which state of drm plane
   * @plane: Which plane
- * Return the DMA GEM address for given framebuffer.
   *
   * This function will usually be called from the PLANE callback functions.
+ *
+ * Return: GEM DMA address for given framebuffer, unclipped.
   */
  dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
  				   struct drm_plane_state *state,
@@ -86,6 +87,34 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
  }
  EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 98

Quoted 10 lines
drivers/gpu/drm/drm_fb_dma_helper.c +9 -0
 
+/**
+ * drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for clipped
+ * framebuffer, for pixel formats where values are grouped in blocks this
+ * will get you the beginning of the block
+ * @fb: The framebuffer
+ * @state: Which state of drm plane
+ * @plane: Which plane
+ *
+ * This function will usually be called from the PLANE callback functions.

You should mention what clipping means in this context. It's not obvious what it's clipping against. Or maybe point to other docs that explain it.

Quoted 6 lines
drivers/gpu/drm/drm_fb_dma_helper.c +6 -0
+ *
+ * Return: GEM DMA address for given framebuffer, clipped.
+ */
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+					   struct drm_plane_state *state,
+					   unsigned int plane)

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 114

Quoted 10 lines
drivers/gpu/drm/drm_fb_dma_helper.c +10 -0
+{
+	struct drm_gem_dma_object *obj;
+
+	obj = drm_fb_dma_get_gem_obj(fb, plane);
+	if (!obj)
+		return 0;
+
+	return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+								state->src.x1 >> 16,
+								state->src.y1 >> 16);

I had to look several times to spot the difference to the regular helper. In any case

Quoted 21 lines
drivers/gpu/drm/drm_fb_dma_helper.c +3 -0
+}
+EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_clipped_addr);
+
  /**
   * drm_fb_dma_sync_non_coherent - Sync GEM object to non-coherent backing
   *	memory
include/drm/drm_fb_dma_helper.h +4 -0
index c950732c6d36..b2a0bd7ef9d0 100644
@@ -17,6 +17,10 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
  				   struct drm_plane_state *state,
  				   unsigned int plane);
 
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+					   struct drm_plane_state *state,
+					   unsigned int plane);
+
  void drm_fb_dma_sync_non_coherent(struct drm_device *drm,
  				  struct drm_plane_state *old_state,
  				  struct drm_plane_state *state);
Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
#5 lore raw

Re: [PATCH RFT v3 2/5] drm/fb-dma-helper: Add drm_fb_dma_get_gem_clipped_addr()

Quoted 57 lines · Thomas Zimmermann

Hi

Am 24.11.26 um 08:39 schrieb Chen-Yu Tsai:

Quoted 53 lines

drm_fb_dma_get_gem_addr() returns the DMA address to the "unclipped" framebuffer. However some display drivers want the "clipped" framebuffer instead, as they are also using the clipped coordinates to program the hardware.

Some of these drivers are open-coding drm_fb_dma_get_gem_addr() with the source coordinates replaced, while others have been incorrectly converted to using drm_fb_dma_get_gem_addr(), which would end up causing incorrect parts of the framebuffer to be displayed if it were somehow clipped.

Add drm_fb_dma_get_gem_clipped_addr(), a "clipped" version of drm_fb_dma_get_gem_addr() for these drivers to use.

Cc <[email protected]> # dependency for next patch

---
Changes since v1:
- Use new drm_framebuffer_get_block_offset() helper

---
  drivers/gpu/drm/drm_fb_dma_helper.c | 37 +++++++++++++++++++++++++----
  include/drm/drm_fb_dma_helper.h     |  4 ++++
  2 files changed, 37 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +5 -4
index ab0f37d8a5ff..0aaf4926db4e 100644
@@ -60,15 +60,16 @@ struct drm_gem_dma_object *drm_fb_dma_get_gem_obj(struct drm_framebuffer *fb,
  EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_obj);
 
  /**
- * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for framebuffer, for pixel
- * formats where values are grouped in blocks this will get you the beginning of
- * the block
+ * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for unclipped framebuffer,
+ * for pixel formats where values are grouped in blocks this will get you the
+ * beginning of the block
   * @fb: The framebuffer
   * @state: Which state of drm plane
   * @plane: Which plane
- * Return the DMA GEM address for given framebuffer.
   *
   * This function will usually be called from the PLANE callback functions.
+ *
+ * Return: GEM DMA address for given framebuffer, unclipped.
   */
  dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
                                 struct drm_plane_state *state,
@@ -86,6 +87,34 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
  }
  EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
 
Quoted 12 lines

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 98

Quoted 9 lines
drivers/gpu/drm/drm_fb_dma_helper.c +9 -0
+/**
+ * drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for clipped
+ * framebuffer, for pixel formats where values are grouped in blocks this
+ * will get you the beginning of the block
+ * @fb: The framebuffer
+ * @state: Which state of drm plane
+ * @plane: Which plane
+ *
+ * This function will usually be called from the PLANE callback functions.

You should mention what clipping means in this context. It's not obvious what it's clipping against. Or maybe point to other docs that explain it.

What about:

/**
* drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for the start
* of the visible (CRTC-clipped) part of the framebuffer
* @fb: The framebuffer
* @state: Which state of drm plane
* @plane: Which plane
*
* A plane may be positioned partially off-screen, in which case it gets
* clipped to the visible CRTC area by drm_atomic_helper_check_plane_state()
* (populating the clipped coordinates in &drm_plane_state.src and
* &drm_plane_state.dst). For example, if a plane extends past the left or
* top edge of the screen, only its right and/or bottom portion is visible.
* Drivers whose hardware cannot clip by itself (i.e. accept negative
* destination coordinates) should use this helper instead of
* drm_fb_dma_get_gem_addr() so scanout starts from the visible portion of
* the framebuffer.
*
* For pixel formats where values are grouped in blocks, this returns the
* beginning of the block.
*
* This function will usually be called from the PLANE callback functions. *
* Return: GEM DMA address for the visible part of the framebuffer. */

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/drm_fb_dma_helper.c +10 -0
+ *
+ * Return: GEM DMA address for given framebuffer, clipped.
+ */
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+                                        struct drm_plane_state *state,
+                                        unsigned int plane)
+{
+     struct drm_gem_dma_object *obj;
+
+     obj = drm_fb_dma_get_gem_obj(fb, plane);
Quoted 8 lines

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 114

Quoted 6 lines
drivers/gpu/drm/drm_fb_dma_helper.c +6 -0
+     if (!obj)
+             return 0;
+
+     return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+                                                             state->src.x1 >> 16,
+                                                             state->src.y1 >> 16);

I had to look several times to spot the difference to the regular

I suppose I should add a comment to make the difference more visible:

    /* Use clipped source coordinates state->src.[xy]1 instead ->src_[xy] */
Quoted 3 lines

helper. In any case

If the changes above are acceptable to you I'll send out a new version with your tag kept.

Thanks
ChenYu

#6 lore raw

Re: [PATCH RFT v3 2/5] drm/fb-dma-helper: Add drm_fb_dma_get_gem_clipped_addr()

Hi

Am 24.11.26 um 11:22 schrieb Chen-Yu Tsai:

Quoted 86 lines · Thomas Zimmermann
Quoted 56 lines

Hi

Am 24.11.26 um 08:39 schrieb Chen-Yu Tsai:

Quoted 53 lines

drm_fb_dma_get_gem_addr() returns the DMA address to the "unclipped" framebuffer. However some display drivers want the "clipped" framebuffer instead, as they are also using the clipped coordinates to program the hardware.

Some of these drivers are open-coding drm_fb_dma_get_gem_addr() with the source coordinates replaced, while others have been incorrectly converted to using drm_fb_dma_get_gem_addr(), which would end up causing incorrect parts of the framebuffer to be displayed if it were somehow clipped.

Add drm_fb_dma_get_gem_clipped_addr(), a "clipped" version of drm_fb_dma_get_gem_addr() for these drivers to use.

Cc <[email protected]> # dependency for next patch

---
Changes since v1:
- Use new drm_framebuffer_get_block_offset() helper

---
   drivers/gpu/drm/drm_fb_dma_helper.c | 37 +++++++++++++++++++++++++----
   include/drm/drm_fb_dma_helper.h     |  4 ++++
   2 files changed, 37 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_fb_dma_helper.c +5 -4
index ab0f37d8a5ff..0aaf4926db4e 100644
@@ -60,15 +60,16 @@ struct drm_gem_dma_object *drm_fb_dma_get_gem_obj(struct drm_framebuffer *fb,
   EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_obj);
 
   /**
- * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for framebuffer, for pixel
- * formats where values are grouped in blocks this will get you the beginning of
- * the block
+ * drm_fb_dma_get_gem_addr() - Get DMA (bus) address for unclipped framebuffer,
+ * for pixel formats where values are grouped in blocks this will get you the
+ * beginning of the block
    * @fb: The framebuffer
    * @state: Which state of drm plane
    * @plane: Which plane
- * Return the DMA GEM address for given framebuffer.
    *
    * This function will usually be called from the PLANE callback functions.
+ *
+ * Return: GEM DMA address for given framebuffer, unclipped.
    */
   dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
                                  struct drm_plane_state *state,
@@ -86,6 +87,34 @@ dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb,
   }
   EXPORT_SYMBOL_GPL(drm_fb_dma_get_gem_addr);
 
Quoted 11 lines

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 98

Quoted 9 lines
drivers/gpu/drm/drm_fb_dma_helper.c +9 -0
+/**
+ * drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for clipped
+ * framebuffer, for pixel formats where values are grouped in blocks this
+ * will get you the beginning of the block
+ * @fb: The framebuffer
+ * @state: Which state of drm plane
+ * @plane: Which plane
+ *
+ * This function will usually be called from the PLANE callback functions.

You should mention what clipping means in this context. It's not obvious what it's clipping against. Or maybe point to other docs that explain it.

What about:

/**
* drm_fb_dma_get_gem_clipped_addr() - Get DMA (bus) address for the start
* of the visible (CRTC-clipped) part of the framebuffer
* @fb: The framebuffer
* @state: Which state of drm plane
* @plane: Which plane
*
* A plane may be positioned partially off-screen, in which case it gets
* clipped to the visible CRTC area by drm_atomic_helper_check_plane_state()
* (populating the clipped coordinates in &drm_plane_state.src and
* &drm_plane_state.dst). For example, if a plane extends past the left or
* top edge of the screen, only its right and/or bottom portion is visible.
* Drivers whose hardware cannot clip by itself (i.e. accept negative
* destination coordinates) should use this helper instead of
* drm_fb_dma_get_gem_addr() so scanout starts from the visible portion of
* the framebuffer.

Quoted 8 lines

*
* For pixel formats where values are grouped in blocks, this returns the
* beginning of the block.
*
* This function will usually be called from the PLANE callback functions. *
* Return: GEM DMA address for the visible part of the framebuffer. */

Looks good.

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/drm_fb_dma_helper.c +9 -0
+ *
+ * Return: GEM DMA address for given framebuffer, clipped.
+ */
+dma_addr_t drm_fb_dma_get_gem_clipped_addr(struct drm_framebuffer *fb,
+                                        struct drm_plane_state *state,
+                                        unsigned int plane)
+{
+     struct drm_gem_dma_object *obj;
+
Quoted 8 lines

drivers/gpu/drm/drm_fb_dma_helper.c dma_addr_t drm_fb_dma_get_gem_addr(struct drm_framebuffer *fb, · line 114

Quoted 7 lines
drivers/gpu/drm/drm_fb_dma_helper.c +7 -0
+     obj = drm_fb_dma_get_gem_obj(fb, plane);
+     if (!obj)
+             return 0;
+
+     return obj->dma_addr + drm_framebuffer_get_block_offset(fb, plane,
+                                                             state->src.x1 >> 16,
+                                                             state->src.y1 >> 16);

I had to look several times to spot the difference to the regular

Quoted 3 lines

I suppose I should add a comment to make the difference more visible:

     /* Use clipped source coordinates state->src.[xy]1 instead ->src_[xy] */

Your choice.

Quoted 6 lines
Quoted 4 lines

helper. In any case

If the changes above are acceptable to you I'll send out a new version with your tag kept.

Sure, go ahead.

Best regards
Thomas

Quoted 4 lines

Thanks
ChenYu

Signature · 6 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstr. 146, 90461 Nürnberg, Germany, www.suse.com
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri, (HRB 36809, AG Nürnberg)
#7 lore raw

PATCH v3 3/5 drm/sun4i: layers: Fix VI buffer address for clipped offsets

In reply to Chen-Yu Tsai

Commit 79ac1c945ab8 ("drm/sun4i: layers: Use drm_fb_dma_get_gem_addr() to get display memory") dropped the code to calculate the framebuffer's DMA address in favor of drm_fb_dma_get_gem_addr().

This turned out to be wrong in a couple ways. The hardware is programmed with clipped dimensions, so it needs the buffer address to start at the clipped boundary. Moving to the helper negated the clipping. Also, when clipping on the left, the buffer address needs to start at the first pixel in the sub-sampling group even for the luma plane. The hardware handles the interpolation internally.

Switch to the new drm_fb_dma_get_gem_clipped_addr(), which provides the buffer address starting at the clipped boundary. Calculate the intra-group offset and adjust the luma plane buffer address so that it points to the start of the sub-sampling group.

Fixes 79ac1c945ab8 ("drm/sun4i: layers: Use drm_fb_dma_get_gem_addr() to get display memory")
Cc <[email protected]> # v7.1+, needs drm_fb_dma_get_gem_clipped_addr()

---
This is an alternative to Jernej's original revert:
https://lore.kernel.org/all/3980ea1aeb3f7fe8b4700e36560deeba3d050664.1785772659.git.jernej.skrabec@gmail.com/

---
 drivers/gpu/drm/sun4i/sun8i_ui_layer.c |  2 +-
 drivers/gpu/drm/sun4i/sun8i_vi_layer.c | 16 +++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/sun4i/sun8i_ui_layer.c +1 -1
index f9c7f28f9fa2..67cce1b0e70f 100644
@@ -130,7 +130,7 @@ static void sun8i_ui_layer_update_buffer(struct sun8i_layer *layer,
 	ch_base = sun8i_channel_base(layer);
 
 	/* Get the start of the displayed memory */
-	dma_addr = drm_fb_dma_get_gem_addr(fb, state, 0);
+	dma_addr = drm_fb_dma_get_gem_clipped_addr(fb, state, 0);
 
 	/* Set the line width */
 	DRM_DEBUG_DRIVER("Layer line width: %d bytes\n", fb->pitches[0]);
drivers/gpu/drm/sun4i/sun8i_vi_layer.c +15 -1
index 4d3fc03f9877..b6796416d550 100644
@@ -205,7 +205,21 @@ static void sun8i_vi_layer_update_buffer(struct sun8i_layer *layer,
 
 	for (i = 0; i < format->num_planes; i++) {
 		/* Get the start of the displayed memory */
-		dma_addr = drm_fb_dma_get_gem_addr(fb, state, i);
+		dma_addr = drm_fb_dma_get_gem_clipped_addr(fb, state, i);
+
+		/*
+		 * The mixer can handle odd offsets into sub-sampled YUV
+		 * planes, but needs the address of the first pixel in each
+		 * sub-sampled block. Adjust the luma buffer address backwards.
+		 */
+		if (i == 0) {
+			u32 x_diff, y_diff;
+
+			x_diff = (state->src.x1 >> 16) & (format->hsub - 1);
+			y_diff = (state->src.y1 >> 16) & (format->vsub - 1);
+			dma_addr -= y_diff * fb->pitches[i];
+			dma_addr -= x_diff * format->cpp[i];
+		}
 
 		/* Set the line width */
 		DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
#8 lore raw

PATCH v3 4/5 drm/imx/dc: plane: Switch to drm_fb_dma_get_gem_clipped_addr()

In reply to Chen-Yu Tsai

The hardware is programmed with clipped source and destination dimensions, but the framebuffer address is calculated using drm_fb_dma_get_gem_addr(), which uses the full source dimensions. This will not match the source offset if the top and/or left sides are clipped.

Switch to the new drm_fb_dma_get_gem_clipped_addr(), which provides the buffer address starting at the clipped boundary.

Fixes 711a3b878366 ("drm/imx: Add i.MX8qxp Display Controller KMS")
Cc <[email protected]> # Needs drm_fb_dma_get_gem_clipped_addr()
---
 drivers/gpu/drm/imx/dc/dc-plane.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/imx/dc/dc-plane.c +2 -2
index 62dd0576fadc..35eb5039f5ac 100644
@@ -65,7 +65,7 @@ static int dc_plane_check_max_source_resolution(struct drm_plane_state *state)
 static int dc_plane_check_fb(struct drm_plane_state *state)
 {
 	struct drm_framebuffer *fb = state->fb;
-	dma_addr_t baseaddr = drm_fb_dma_get_gem_addr(fb, state, 0);
+	dma_addr_t baseaddr = drm_fb_dma_get_gem_clipped_addr(fb, state, 0);
 
 	/* base address alignment */
 	if (baseaddr & 0x3) {
@@ -146,7 +146,7 @@ dc_plane_atomic_update(struct drm_plane *plane, struct drm_atomic_commit *state)
 	src_w = drm_rect_width(&new_state->src) >> 16;
 	src_h = drm_rect_height(&new_state->src) >> 16;
 
-	baseaddr = drm_fb_dma_get_gem_addr(fb, new_state, 0);
+	baseaddr = drm_fb_dma_get_gem_clipped_addr(fb, new_state, 0);
 
 	fu_ops = dc_fu_get_ops(dplane->fu);
 
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
#9 lore raw

PATCH v3 5/5 drm/imx/dcss: plane: Switch to drm_fb_dma_get_gem_clipped_addr()

In reply to Chen-Yu Tsai

The i.MX DCSS driver is open coding drm_fb_dma_get_gem_clipped_addr(), with only a slight difference of rounding down the X offset for the first plane if the format is packed, sub-sampled YUV. This is likely to correct the buffer address to the first pixel of the 2-pixel group. Otherwise the hardware will start the scan-out from the second pixel, which leads to the U/V components getting swapped around, and the chroma component of the next pixel group being used.

Switch to drm_fb_dma_get_gem_clipped_addr(), and offset the address by a pixel if the X offset is odd.

---
 drivers/gpu/drm/imx/dcss/dcss-plane.c | 34 +++++++++++----------------
 1 file changed, 14 insertions(+), 20 deletions(-)
drivers/gpu/drm/imx/dcss/dcss-plane.c +14 -20
index 303e93fd036c..580d9cfb4053 100644
@@ -219,28 +219,22 @@ static void dcss_plane_atomic_set_base(struct dcss_plane *dcss_plane)
 	struct dcss_dev *dcss = plane->dev->dev_private;
 	struct drm_framebuffer *fb = state->fb;
 	const struct drm_format_info *format = fb->format;
-	struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 	unsigned long p1_ba = 0, p2_ba = 0;
 
-	if (!format->is_yuv ||
-	    format->format == DRM_FORMAT_NV12 ||
-	    format->format == DRM_FORMAT_NV21)
-		p1_ba = dma_obj->dma_addr + fb->offsets[0] +
-			fb->pitches[0] * (state->src.y1 >> 16) +
-			format->char_per_block[0] * (state->src.x1 >> 16);
-	else if (format->format == DRM_FORMAT_UYVY ||
-		 format->format == DRM_FORMAT_VYUY ||
-		 format->format == DRM_FORMAT_YUYV ||
-		 format->format == DRM_FORMAT_YVYU)
-		p1_ba = dma_obj->dma_addr + fb->offsets[0] +
-			fb->pitches[0] * (state->src.y1 >> 16) +
-			2 * format->char_per_block[0] * (state->src.x1 >> 17);
-
-	if (format->format == DRM_FORMAT_NV12 ||
-	    format->format == DRM_FORMAT_NV21)
-		p2_ba = dma_obj->dma_addr + fb->offsets[1] +
-			(((fb->pitches[1] >> 1) * (state->src.y1 >> 17) +
-			(state->src.x1 >> 17)) << 1);
+	p1_ba = drm_fb_dma_get_gem_clipped_addr(fb, state, 0);
+
+	/*
+	 * TODO fix address until helpers know packed, sub-sampled YUV format block size
+	 *
+	 * The buffer address for packed, sub-sampled YUV formats such as DRM_FORMAT_UYVY
+	 * need to be on the first pixel of each pixel group or block. Otherwise the first
+	 * pixel of the next pixel group is read and the U/V values get swapped around.
+	 */
+	if (drm_format_info_is_yuv_packed(format))
+		p1_ba -= ((state->src.x1 >> 16) & 1) * format->cpp[0];
+
+	if (format->num_planes > 1)
+		p2_ba = drm_fb_dma_get_gem_clipped_addr(fb, state, 1);
 
 	dcss_dpr_addr_set(dcss->dpr, dcss_plane->ch_num, p1_ba, p2_ba,
 			  fb->pitches[0]);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog