Skip to contentSkip to conversation

drm/sti: move commit work out of .atomic_check()

PATCH New Minseo Park 2 participants 8 messages Nov 26

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

v2 cover drm/sti: move commit work out of .atomic_check()

The three plane .atomic_check() implementations in drivers/gpu/drm/sti do work that belongs in the commit phase. The cursor frees and reallocates its pixmap, the GDP reparents and reclocks the pixel clock, and the HQVDP boots its coprocessor and registers a VTG notifier.

DRM_MODE_ATOMIC_TEST_ONLY runs drm_atomic_check_only(), which calls the plane .atomic_check() hooks and returns without ever calling .atomic_update(), and a commit that fails a check on any other object is dropped the same way. Either path leaves the hardware changed by a configuration that was never applied.

The cursor is the worst of the three. The buffer it frees is the one CUR_PML points at, so the display engine keeps reading it after it has gone back to the DMA pool.

I do not have STiH4xx hardware, so this comes from reading the code rather than from an observed failure. The series builds warning free with W=1 and COMPILE_TEST on x86_64. Testing on a real board would be appreciated.

Tool disclosure, per Documentation/process/generated-content.rst:

This series was produced with the help of Claude (Anthropic), used interactively over a long session rather than from one prompt. The assistant did the code reading that located these three hooks, wrote the patches and wrote the changelogs. I reviewed every hunk and checked every claim in the changelogs against the source before sending, and mine is the only Signed-off-by.

The starting point was a request to look for work done in .atomic_check() that a DRM_MODE_ATOMIC_TEST_ONLY commit would leave behind, after the same question had been put to V4L2, ALSA, DVB and fbdev. Candidates came from a script that flagged .atomic_check() implementations containing register writes, clock calls or allocations; it was calibrated against these three hooks and its output was then read by hand. Of 17 implementations it flagged in the tree, 8 were a pattern match inside the identifier writeback_job, 6 were the normal per-commit allocation idiom, and these three were the only real ones.

Testing is as described above: git am onto the base commit below, W=1 with COMPILE_TEST on x86_64, and checkpatch.pl --strict. No runtime testing and no reproducer.

Changes in v2, all from Thomas Zimmermann's review of v1:
- cursor: the new error message uses drm_err() rather than DRM_ERROR().
- gdp: .atomic_check() now validates the GDP pixel clock with clk_round_rate(), and the clk_set_rate() failure in .atomic_update() is logged with drm_err_once().
- hqvdp: split in two. The XP70 boot moves to device init, as suggested. The clock enable and the VTG registration move to .atomic_update() instead, for the reason below.

On the clk question in v1 2/3: clk_round_rate() is the interface for testing a rate without applying it -- its kerneldoc says it does not "modify the clock hardware in any way" -- and this driver already uses it that way in sti_dvo_connector_mode_valid(), sti_hdmi_connector_mode_valid() and sti_hda_connector_mode_valid(), each against a 50 Hz tolerance window. Those validate the encoder clocks and not the per-GDP clk_pix, so 2/4 does the same check on the GDP clock. The limit is that clk_round_rate() rounds against the parent selected at the time of the call, so the check is skipped when the commit would reparent the clock; only .atomic_update() can catch that case.

On v1 3/3: the XP70 boot indeed belongs in device init and 3/4 moves it there. The other two steps cannot follow it. sti_vtg_register_client() stores the CRTC the plane is attached to, which only a commit knows, and the clock enable is undone by the clk_disable_unprepare() in sti_hqvdp_disable(), so it tracks the streaming lifetime. 4/4 moves both to .atomic_update(), where sti_gdp already registers its own VTG client the same way.

3/4 turns the firmware request into a one-shot at device init. If the deferral matters for a built-in driver whose firmware lives in the root filesystem, request_firmware_nowait() would keep it without putting the load in a commit hook -- happy to respin that way.

Minseo Park (4):
drm/sti: cursor: do not resize the pixmap from .atomic_check() drm/sti: gdp: set the pixel clock from .atomic_update() drm/sti: hqvdp: start the XP70 from device init drm/sti: hqvdp: register the VTG client from .atomic_update()

 drivers/gpu/drm/sti/sti_cursor.c | 65 ++++++++++++----------------
 drivers/gpu/drm/sti/sti_gdp.c    | 73 ++++++++++++++++++++++----------
 drivers/gpu/drm/sti/sti_hqvdp.c  | 44 +++++++++----------
 3 files changed, 101 insertions(+), 81 deletions(-)

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 drm/sti: cursor: do not resize the pixmap from .atomic_check()

sti_cursor_atomic_check() resizes the cursor pixmap whenever the requested size differs from the one it cached: it frees the buffer the hardware is using and allocates a new one.

That is commit work done in the check phase. An atomic commit with DRM_MODE_ATOMIC_TEST_ONLY runs drm_atomic_check_only(), which calls the plane .atomic_check() hooks and returns without ever calling .atomic_update(), and a commit whose check fails on some other object is dropped the same way. In both cases the buffer is freed while CUR_PML still points at it, so the display engine keeps fetching the cursor from memory that has gone back to the DMA pool, until some later commit reprograms the register.

.atomic_check() already rejects a cursor wider or taller than STI_CURS_MAX_SIZE, and the pixmap is CLUT8, so it never needs to be larger than 128 * 128 bytes. Allocate it once when the plane is created and keep it for the lifetime of the device, with the managed allocator so that it is released on unbind. Nothing has to be resized afterwards.

With the pixmap no longer resized, struct sti_cursor does not need to cache the cursor size either. .atomic_update() takes it from the plane state it is handed, which is the state .atomic_check() validated. The dev member goes with them, it only had users in the code being removed.

Fixes dd86dc2f9ae1 ("drm/sti: implement atomic_check for the planes")
---
 drivers/gpu/drm/sti/sti_cursor.c | 65 ++++++++++++++------------------
 1 file changed, 28 insertions(+), 37 deletions(-)
drivers/gpu/drm/sti/sti_cursor.c +28 -37
index 66f56355309f..7295b6edb8ef 100644
@@ -53,20 +53,14 @@ struct dma_pixmap {
  * STI Cursor structure
  *
  * @sti_plane:    sti_plane structure
- * @dev:          driver device
  * @regs:         cursor registers
- * @width:        cursor width
- * @height:       cursor height
  * @clut:         color look up table
  * @clut_paddr:   color look up table physical address
  * @pixmap:       pixmap dma buffer (clut8-format cursor)
  */
 struct sti_cursor {
 	struct sti_plane plane;
-	struct device *dev;
 	void __iomem *regs;
-	unsigned int width;
-	unsigned int height;
 	unsigned short *clut;
 	dma_addr_t clut_paddr;
 	struct dma_pixmap pixmap;
@@ -146,14 +140,15 @@ static void cursor_debugfs_init(struct sti_cursor *cursor,
 				 minor->debugfs_root, minor);
 }
 
-static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src)
+static void sti_cursor_argb8888_to_clut8(struct sti_cursor *cursor, u32 *src,
+					 unsigned int width, unsigned int height)
 {
 	u8  *dst = cursor->pixmap.base;
 	unsigned int i, j;
 	u32 a, r, g, b;
 
-	for (i = 0; i < cursor->height; i++) {
-		for (j = 0; j < cursor->width; j++) {
+	for (i = 0; i < height; i++) {
+		for (j = 0; j < width; j++) {
 			/* Pick the 2 higher bits of each component */
 			a = (*src >> 30) & 3;
 			r = (*src >> 22) & 3;
@@ -188,7 +183,6 @@ static int sti_cursor_atomic_check(struct drm_plane *drm_plane,
 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
 										 drm_plane);
 	struct sti_plane *plane = to_sti_plane(drm_plane);
-	struct sti_cursor *cursor = to_sti_cursor(plane);
 	struct drm_crtc *crtc = new_plane_state->crtc;
 	struct drm_framebuffer *fb = new_plane_state->fb;
 	struct drm_crtc_state *crtc_state;
@@ -224,29 +218,6 @@ static int sti_cursor_atomic_check(struct drm_plane *drm_plane,
 		return -EINVAL;
 	}
 
-	/* If the cursor size has changed, re-allocated the pixmap */
-	if (!cursor->pixmap.base ||
-	    (cursor->width != src_w) ||
-	    (cursor->height != src_h)) {
-		cursor->width = src_w;
-		cursor->height = src_h;
-
-		if (cursor->pixmap.base)
-			dma_free_wc(cursor->dev, cursor->pixmap.size,
-				    cursor->pixmap.base, cursor->pixmap.paddr);
-
-		cursor->pixmap.size = cursor->width * cursor->height;
-
-		cursor->pixmap.base = dma_alloc_wc(cursor->dev,
-						   cursor->pixmap.size,
-						   &cursor->pixmap.paddr,
-						   GFP_KERNEL);
-		if (!cursor->pixmap.base) {
-			DRM_ERROR("Failed to allocate memory for pixmap\n");
-			return -EINVAL;
-		}
-	}
-
 	if (!drm_fb_dma_get_gem_obj(fb, 0)) {
 		DRM_ERROR("Can't get DMA GEM object for fb\n");
 		return -EINVAL;
@@ -271,6 +242,7 @@ static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
 	struct drm_framebuffer *fb = newstate->fb;
 	struct drm_display_mode *mode;
 	int dst_x, dst_y;
+	unsigned int src_w, src_h;
 	struct drm_gem_dma_object *dma_obj;
 	u32 y, x;
 	u32 val;
@@ -281,11 +253,15 @@ static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
 	mode = &crtc->mode;
 	dst_x = newstate->crtc_x;
 	dst_y = newstate->crtc_y;
+	/* src_w and src_h are in 16.16 format */
+	src_w = newstate->src_w >> 16;
+	src_h = newstate->src_h >> 16;
 
 	dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
 
 	/* Convert ARGB8888 to CLUT8 */
-	sti_cursor_argb8888_to_clut8(cursor, (u32 *)dma_obj->vaddr);
+	sti_cursor_argb8888_to_clut8(cursor, (u32 *)dma_obj->vaddr,
+				     src_w, src_h);
 
 	/* AWS and AWE depend on the mode */
 	y = sti_vtg_get_line_number(*mode, 0);
@@ -299,8 +275,8 @@ static void sti_cursor_atomic_update(struct drm_plane *drm_plane,
 
 	/* Set memory location, size, and position */
 	writel(cursor->pixmap.paddr, cursor->regs + CUR_PML);
-	writel(cursor->width, cursor->regs + CUR_PMP);
-	writel(cursor->height << 16 | cursor->width, cursor->regs + CUR_SIZE);
+	writel(src_w, cursor->regs + CUR_PMP);
+	writel(src_h << 16 | src_w, cursor->regs + CUR_SIZE);
 
 	y = sti_vtg_get_line_number(*mode, dst_y);
 	x = sti_vtg_get_pixel_number(*mode, dst_x);
@@ -377,6 +353,22 @@ struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
 		return NULL;
 	}
 
+	/*
+	 * Allocate the pixmap once, for the largest cursor the hardware
+	 * supports.  It used to be reallocated from .atomic_check(), which
+	 * freed a buffer the display engine could still be scanning out.
+	 * Use the managed helper so it is released with the device.
+	 */
+	cursor->pixmap.size = STI_CURS_MAX_SIZE * STI_CURS_MAX_SIZE;
+	cursor->pixmap.base = dmam_alloc_attrs(dev, cursor->pixmap.size,
+					       &cursor->pixmap.paddr,
+					       GFP_KERNEL,
+					       DMA_ATTR_WRITE_COMBINE);
+	if (!cursor->pixmap.base) {
+		drm_err(drm_dev, "Failed to allocate memory for pixmap\n");
+		goto err_clut;
+	}
+
 	/* Allocate clut buffer */
 	size = 0x100 * sizeof(unsigned short);
 	cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr,
@@ -387,7 +379,6 @@ struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
 		goto err_clut;
 	}
 
-	cursor->dev = dev;
 	cursor->regs = baseaddr;
 	cursor->plane.desc = desc;
 	cursor->plane.status = STI_PLANE_DISABLED;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/4] drm/sti: cursor: do not resize the pixmap from .atomic_check()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/sti: leak of managed pixmap DMA buffer on error in sti_cursor_create() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/sti: leak of managed pixmap DMA buffer on error in sti_cursor_create()
--

commit e56d841601d0e4ed868b107dc1847844ca728b69
Author: Minseo Park <[email protected]>

drm/sti: cursor: do not resize the pixmap from .atomic_check()

This commit allocates the cursor pixmap DMA buffer once at plane creation time instead of reallocating it inside sti_cursor_atomic_check().

Quoted 4 lines
drivers/gpu/drm/sti/sti_cursor.c +0 -0
index 66f56355309f9..7295b6edb8ef9 100644

[ ... ]

Quoted 24 lines
drivers/gpu/drm/sti/sti_cursor.c +16 -0
@@ -377,6 +353,22 @@ struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
 		return NULL;
 	}
 
+	/*
+	 * Allocate the pixmap once, for the largest cursor the hardware
+	 * supports.  It used to be reallocated from .atomic_check(), which
+	 * freed a buffer the display engine could still be scanning out.
+	 * Use the managed helper so it is released with the device.
+	 */
+	cursor->pixmap.size = STI_CURS_MAX_SIZE * STI_CURS_MAX_SIZE;
+	cursor->pixmap.base = dmam_alloc_attrs(dev, cursor->pixmap.size,
+					       &cursor->pixmap.paddr,
+					       GFP_KERNEL,
+					       DMA_ATTR_WRITE_COMBINE);
+	if (!cursor->pixmap.base) {
+		drm_err(drm_dev, "Failed to allocate memory for pixmap\n");
+		goto err_clut;
+	}
+
 	/* Allocate clut buffer */
 	size = 0x100 * sizeof(unsigned short);
 	cursor->clut = dma_alloc_wc(dev, size, &cursor->clut_paddr,

drivers/gpu/drm/sti/sti_cursor.c struct drm_plane *sti_cursor_create(struct drm_device *drm_dev, · line 384

Quoted 7 lines
drivers/gpu/drm/sti/sti_cursor.c +0 -1
@@ -387,7 +379,6 @@ struct drm_plane *sti_cursor_create(struct drm_device *drm_dev,
 		goto err_clut;
 	}
 
-	cursor->dev = dev;
 	cursor->regs = baseaddr;
 	cursor->plane.desc = desc;
 	cursor->plane.status = STI_PLANE_DISABLED;

[Severity: Medium]
Does sti_cursor_create() leak cursor->pixmap.base when allocating cursor->clut or calling drm_universal_plane_init() fails?

The err_plane and err_clut paths free cursor->clut and cursor, without releasing cursor->pixmap.base. Because sti_compositor_bind() treats a NULL return from sti_cursor_create() as non-fatal and still returns 0, devres on compo->dev is not released on failure and the 16 KiB DMA buffer stays allocated while the compositor remains bound.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/4 drm/sti: gdp: set the pixel clock from .atomic_update()

In reply to Minseo Park

sti_gdp_atomic_check() reparents the GDP pixel clock and calls clk_set_rate() on it with the clock of the mode being checked.

.atomic_check() must not touch the hardware. A commit with DRM_MODE_ATOMIC_TEST_ONLY only runs the check hooks, and a commit that fails a later check is dropped, so in both cases the clock of a mode that was never applied is programmed on a display that is still scanning out the previous one.

Move the clock setup to .atomic_update(), where the rest of the plane is programmed. A clk_set_rate() failure can no longer reject the commit, so it is logged once and the plane is left as it was.

The check still rejects a mode the clock cannot reach. clk_round_rate() reports the rate that would be selected without touching the hardware, which is how the connectors already validate their own pixel clocks in .mode_valid(). It rounds against the parent that is selected now, so the check is skipped when this commit would reparent the clock.

Fixes dd86dc2f9ae1 ("drm/sti: implement atomic_check for the planes")
---
 drivers/gpu/drm/sti/sti_gdp.c | 73 ++++++++++++++++++++++++-----------
 1 file changed, 51 insertions(+), 22 deletions(-)
drivers/gpu/drm/sti/sti_gdp.c +51 -22
index d0c886029c57..4a01bc64f4ab 100644
@@ -6,6 +6,7 @@
  *          for STMicroelectronics.
  */
 
+#include <linux/clk.h>
 #include <linux/dma-mapping.h>
 #include <linux/of.h>
 #include <linux/seq_file.h>
@@ -28,6 +29,9 @@
 #define BIGNOTLITTLE    BIT(23)
 #define WAIT_NEXT_VSYNC BIT(31)
 
+/* Same tolerance as the connectors use in their .mode_valid() */
+#define CLK_TOLERANCE_HZ 50
+
 /* GDP color formats */
 #define GDP_RGB565      0x00
 #define GDP_RGB888      0x01
@@ -667,29 +671,28 @@ static int sti_gdp_atomic_check(struct drm_plane *drm_plane,
 		return -EINVAL;
 	}
 
-	/* Set gdp clock */
+	/*
+	 * The pixel clock is programmed from .atomic_update().  clk_round_rate()
+	 * reports the rate that would be selected without touching the
+	 * hardware, so the mode can still be rejected here -- but it rounds
+	 * against the parent that is selected now, so only rely on it when
+	 * this commit is not going to reparent the clock.
+	 */
 	if (mode->clock && gdp->clk_pix) {
-		struct clk *clkp;
-		int rate = mode->clock * 1000;
-		int res;
-
-		/*
-		 * According to the mixer used, the gdp pixel clock
-		 * should have a different parent clock.
-		 */
-		if (mixer->id == STI_MIXER_MAIN)
-			clkp = gdp->clk_main_parent;
-		else
-			clkp = gdp->clk_aux_parent;
-
-		if (clkp)
-			clk_set_parent(gdp->clk_pix, clkp);
-
-		res = clk_set_rate(gdp->clk_pix, rate);
-		if (res < 0) {
-			DRM_ERROR("Cannot set rate (%dHz) for gdp\n",
-				  rate);
-			return -EINVAL;
+		struct clk *clkp = mixer->id == STI_MIXER_MAIN ?
+				   gdp->clk_main_parent : gdp->clk_aux_parent;
+		int target = mode->clock * 1000;
+		long rounded;
+
+		if (!clkp || clk_is_match(clk_get_parent(gdp->clk_pix), clkp)) {
+			rounded = clk_round_rate(gdp->clk_pix, target);
+			if (rounded < target - CLK_TOLERANCE_HZ ||
+			    rounded > target + CLK_TOLERANCE_HZ) {
+				drm_dbg_kms(drm_plane->dev,
+					    "gdp pixclk=%d not supported\n",
+					    target);
+				return -EINVAL;
+			}
 		}
 	}
 
@@ -757,6 +760,32 @@ static void sti_gdp_atomic_update(struct drm_plane *drm_plane,
 	}
 
 	mode = &crtc->mode;
+
+	/* Set gdp clock */
+	if (mode->clock && gdp->clk_pix) {
+		struct sti_mixer *mixer = to_sti_mixer(crtc);
+		int rate = mode->clock * 1000;
+		struct clk *clkp;
+
+		/*
+		 * According to the mixer used, the gdp pixel clock
+		 * should have a different parent clock.
+		 */
+		if (mixer->id == STI_MIXER_MAIN)
+			clkp = gdp->clk_main_parent;
+		else
+			clkp = gdp->clk_aux_parent;
+
+		if (clkp)
+			clk_set_parent(gdp->clk_pix, clkp);
+
+		if (clk_set_rate(gdp->clk_pix, rate) < 0) {
+			drm_err_once(drm_plane->dev,
+				     "Cannot set rate (%dHz) for gdp\n", rate);
+			return;
+		}
+	}
+
 	dst_x = newstate->crtc_x;
 	dst_y = newstate->crtc_y;
 	dst_w = clamp_val(newstate->crtc_w, 0, mode->hdisplay - dst_x);
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 2/4] drm/sti: gdp: set the pixel clock from .atomic_update()

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/sti: skipped GDP clock update on modeset in sti_gdp_atomic_update() - [Medium] drm/sti: clock…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/sti: skipped GDP clock update on modeset in sti_gdp_atomic_update()
- [Medium] drm/sti: clock enabled before rate setup and leaked in sti_gdp_atomic_update()
--

commit d592a33824a375f249463d88ff389958a6b8400e
Author: Minseo Park <[email protected]>

drm/sti: gdp: set the pixel clock from .atomic_update()

This commit moves the GDP pixel clock reparenting and rate configuration from sti_gdp_atomic_check() to sti_gdp_atomic_update(), using clk_round_rate() in sti_gdp_atomic_check() to validate the mode clock when the parent clock does not change.

Quoted 4 lines
drivers/gpu/drm/sti/sti_gdp.c +0 -0
index d0c886029c570..4a01bc64f4ab0 100644

[ ... ]

Quoted 19 lines
drivers/gpu/drm/sti/sti_gdp.c +15 -0
@@ -757,6 +760,32 @@ static void sti_gdp_atomic_update(struct drm_plane *drm_plane,
 	}
 
 	mode = &crtc->mode;
+
+	/* Set gdp clock */
+	if (mode->clock && gdp->clk_pix) {
+		struct sti_mixer *mixer = to_sti_mixer(crtc);
+		int rate = mode->clock * 1000;
+		struct clk *clkp;
+
+		/*
+		 * According to the mixer used, the gdp pixel clock
+		 * should have a different parent clock.
+		 */
+		if (mixer->id == STI_MIXER_MAIN)
+			clkp = gdp->clk_main_parent;
+		else
+			clkp = gdp->clk_aux_parent;

drivers/gpu/drm/sti/sti_gdp.c static void sti_gdp_atomic_update(struct drm_plane *drm_plane, · line 787

Quoted 10 lines
drivers/gpu/drm/sti/sti_gdp.c +10 -0
+
+		if (clkp)
+			clk_set_parent(gdp->clk_pix, clkp);
+
+		if (clk_set_rate(gdp->clk_pix, rate) < 0) {
+			drm_err_once(drm_plane->dev,
+				     "Cannot set rate (%dHz) for gdp\n", rate);
+			return;
+		}
+	}

[Severity: High]
Can sti_gdp_atomic_update() skip this clock update when a modeset changes mode->clock or moves the plane to another CRTC without changing the plane's framebuffer or coordinates?

Earlier in sti_gdp_atomic_update(), the function returns early if fb and the crtc_* and src_* coordinates are unchanged:

drivers/gpu/drm/sti/sti_gdp.c:sti_gdp_atomic_update() {
    ...
	if ((oldstate->fb == newstate->fb) &&
	    (oldstate->crtc_x == newstate->crtc_x) &&
	    (oldstate->crtc_y == newstate->crtc_y) &&
	    (oldstate->crtc_w == newstate->crtc_w) &&
	    (oldstate->crtc_h == newstate->crtc_h) &&
	    (oldstate->src_x == newstate->src_x) &&
	    (oldstate->src_y == newstate->src_y) &&
	    (oldstate->src_w == newstate->src_w) &&
	    (oldstate->src_h == newstate->src_h)) {
		/* No change since last update, do not post cmd */
		DRM_DEBUG_DRIVER("No change, not posting cmd\n");
		plane->status = STI_PLANE_UPDATED;
		return;
	}
    ...
}

Previously, sti_gdp_atomic_check() always ran clk_set_parent() and clk_set_rate() when the CRTC mode or CRTC assignment changed. With the clock setup placed after this early return, a refresh-rate change at the same resolution or a switch between STI_MIXER_MAIN and STI_MIXER_AUX with the same fb and coordinates returns before updating gdp->clk_pix, leaving the plane scanning out with the old pixel clock rate or wrong parent clock.

[Severity: Medium]
When a disabled GDP plane is first enabled (!gdp->vtg), does returning on clk_set_rate() failure leak the VTG client registration and gdp->clk_pix enable count?

Just above the new clock block, sti_gdp_atomic_update() sets gdp->vtg, registers gdp->vtg_field_nb, and calls clk_prepare_enable(gdp->clk_pix):

drivers/gpu/drm/sti/sti_gdp.c:sti_gdp_atomic_update() {
    ...
	if (!gdp->vtg) {
		struct sti_compositor *compo = dev_get_drvdata(gdp->dev);
		struct sti_mixer *mixer = to_sti_mixer(crtc);

		/* Register gdp callback */
		gdp->vtg = compo->vtg[mixer->id];
		sti_vtg_register_client(gdp->vtg, &gdp->vtg_field_nb, crtc);
		clk_prepare_enable(gdp->clk_pix);
	}
    ...
}

Besides enabling gdp->clk_pix before its parent and rate are configured, sti_gdp_atomic_check() skips clk_round_rate() when the parent clock changes, so clk_set_rate() can fail here. Returning early leaves gdp->clk_pix enabled and gdp->vtg_field_nb registered while plane->status remains STI_PLANE_DISABLED, so disabling the CRTC will not call sti_gdp_disable() to clean them up.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v2 3/4 drm/sti: hqvdp: start the XP70 from device init

In reply to Minseo Park

sti_hqvdp_atomic_check() boots the XP70 coprocessor on the first commit that uses the plane, which means it requests the HQVDP firmware and writes it into the coprocessor's memories from the check hook.

.atomic_check() must not touch the hardware. A commit with DRM_MODE_ATOMIC_TEST_ONLY only runs the check hooks, so a client asking whether a configuration is possible ends up loading firmware.

The boot does not depend on anything a commit provides, and sti_hqvdp_bind() already says it creates the plane "once xp70 is initialized", so start it there. sti_hqvdp_start_xp70() keeps its own xp70_initialized guard, so the caller no longer needs one.

This moves the firmware request from the first modeset to device init. The lazy call retried on every check until it succeeded; this one runs once, so for a built-in driver whose firmware lives in the root filesystem the plane can now stay unusable where it used to recover on a later modeset.

Fixes dd86dc2f9ae1 ("drm/sti: implement atomic_check for the planes")
---
 drivers/gpu/drm/sti/sti_hqvdp.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/gpu/drm/sti/sti_hqvdp.c +3 -4
index 360d213f2094..c6b6ed54b899 100644
@@ -1081,10 +1081,6 @@ static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
 		return -EINVAL;
 	}
 
-	if (!hqvdp->xp70_initialized)
-		/* Start HQVDP XP70 coprocessor */
-		sti_hqvdp_start_xp70(hqvdp);
-
 	if (!hqvdp->vtg_registered) {
 		/* Prevent VTG shutdown */
 		if (clk_prepare_enable(hqvdp->clk_pix_main)) {
@@ -1333,6 +1329,9 @@ static int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
 
 	hqvdp->drm_dev = drm_dev;
 
+	/* Start HQVDP XP70 coprocessor */
+	sti_hqvdp_start_xp70(hqvdp);
+
 	/* Create HQVDP plane once xp70 is initialized */
 	plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
 	if (!plane)
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v2 3/4] drm/sti: hqvdp: start the XP70 from device init

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/sti: hqvdp: sti_hqvdp_bind() exposes plane with clk off on init failure - [Medium] drm/sti…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/sti: hqvdp: sti_hqvdp_bind() exposes plane with clk off on init failure
- [Medium] drm/sti: hqvdp: sti_hqvdp_bind() leaks hqvdp->clk on error and unbind
--

commit a8cf3e26ef27767a003bfe97a37331241f497584
Author: Minseo Park <[email protected]>

drm/sti: hqvdp: start the XP70 from device init

This commit moves the sti_hqvdp_start_xp70() call out of sti_hqvdp_atomic_check() and into sti_hqvdp_bind() so that atomic check does not touch hardware or load firmware.

Quoted 16 lines
drivers/gpu/drm/sti/sti_hqvdp.c +0 -4
index 360d213f2094f..c6b6ed54b8995 100644
@@ -1081,10 +1081,6 @@ static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
 		return -EINVAL;
 	}
 
-	if (!hqvdp->xp70_initialized)
-		/* Start HQVDP XP70 coprocessor */
-		sti_hqvdp_start_xp70(hqvdp);
-
 	if (!hqvdp->vtg_registered) {
 		/* Prevent VTG shutdown */
 		if (clk_prepare_enable(hqvdp->clk_pix_main)) {

drivers/gpu/drm/sti/sti_hqvdp.c static int sti_hqvdp_bind(struct device *dev, struct device *master, void *data) · line 1333

Quoted 5 lines
drivers/gpu/drm/sti/sti_hqvdp.c +2 -0
@@ -1333,6 +1329,9 @@ static int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
 
 	hqvdp->drm_dev = drm_dev;
 
+	/* Start HQVDP XP70 coprocessor */
+	sti_hqvdp_start_xp70(hqvdp);

[Severity: High]
Can this expose the HQVDP plane with hqvdp->clk disabled when sti_hqvdp_start_xp70() fails?

Commit e00fe64a5ccd ("drm/sti: load HQVDP firmware the first time HQVDP's plane is used") moved firmware loading out of sti_hqvdp_bind() because the root filesystem may not be mounted yet at bind time. When request_firmware() or the XP70 boot poll fails in sti_hqvdp_start_xp70(), hqvdp->xp70_initialized stays false and hqvdp->clk is left disabled, yet sti_hqvdp_bind() still registers the DRM plane and neither sti_hqvdp_atomic_check() nor sti_hqvdp_atomic_update() checks hqvdp->xp70_initialized.

A subsequent commit on the plane then performs unclocked MMIO reads and writes in sti_hqvdp_atomic_update() (via sti_hqvdp_get_free_cmd() and HQVDP_MBX_NEXT_CMD), which can trigger an external abort or bus hang, and disabling the plane runs sti_hqvdp_disable() from vtg_irq_thread(), doing unclocked MMIO and sleeping for 1000 ms in the VSync IRQ thread.

Could sti_hqvdp_start_xp70() be called from sti_hqvdp_atomic_update() instead, with register accesses guarded by hqvdp->xp70_initialized?

drivers/gpu/drm/sti/sti_hqvdp.c static int sti_hqvdp_bind(struct device *dev, struct device *master, void *data) · line 1337

Quoted 4 lines
drivers/gpu/drm/sti/sti_hqvdp.c +1 -0
+
 	/* Create HQVDP plane once xp70 is initialized */
 	plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
 	if (!plane)

[Severity: Medium]
Does this leak the prepare and enable references on hqvdp->clk?

sti_hqvdp_start_xp70() calls clk_prepare_enable(hqvdp->clk), which is now enabled unconditionally at bind time even if the plane is never used, and nothing calls clk_disable_unprepare(hqvdp->clk) if sti_hqvdp_create() or a later component bind fails, or when the component is unbound in sti_hqvdp_unbind().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#7 lore raw

PATCH v2 4/4 drm/sti: hqvdp: register the VTG client from .atomic_update()

In reply to Minseo Park

sti_hqvdp_atomic_check() enables the main pixel clock and registers the VTG vsync notifier.

.atomic_check() must not touch the hardware. A commit with DRM_MODE_ATOMIC_TEST_ONLY only runs the check hooks, and a commit that fails a later check is dropped, so either way the clock is left enabled and the notifier registered for a configuration that was never applied.

Neither of the two belongs in device init. sti_vtg_register_client() stores the CRTC the plane is attached to, which a commit provides and device init does not, and the clock enable is undone by the matching clk_disable_unprepare() in sti_hqvdp_disable(), so it follows the streaming lifetime rather than the device lifetime. Move both to .atomic_update(), where sti_gdp already registers its own VTG client the same way.

The failure paths can no longer reject the commit, so they are logged once and the plane is left alone.

Fixes dd86dc2f9ae1 ("drm/sti: implement atomic_check for the planes")
---
 drivers/gpu/drm/sti/sti_hqvdp.c | 37 +++++++++++++++++----------------
 1 file changed, 19 insertions(+), 18 deletions(-)
drivers/gpu/drm/sti/sti_hqvdp.c +19 -18
index c6b6ed54b899..17c7f5a7854c 100644
@@ -1081,24 +1081,6 @@ static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
 		return -EINVAL;
 	}
 
-	if (!hqvdp->vtg_registered) {
-		/* Prevent VTG shutdown */
-		if (clk_prepare_enable(hqvdp->clk_pix_main)) {
-			DRM_ERROR("Failed to prepare/enable pix main clk\n");
-			return -EINVAL;
-		}
-
-		/* Register VTG Vsync callback to handle bottom fields */
-		if (sti_vtg_register_client(hqvdp->vtg,
-					    &hqvdp->vtg_nb,
-					    crtc)) {
-			DRM_ERROR("Cannot register VTG notifier\n");
-			clk_disable_unprepare(hqvdp->clk_pix_main);
-			return -EINVAL;
-		}
-		hqvdp->vtg_registered = true;
-	}
-
 	DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
 		      crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
 		      drm_plane->base.id, sti_plane_to_str(plane));
@@ -1148,6 +1130,25 @@ static void sti_hqvdp_atomic_update(struct drm_plane *drm_plane,
 	}
 
 	mode = &crtc->mode;
+
+	if (!hqvdp->vtg_registered) {
+		/* Prevent VTG shutdown */
+		if (clk_prepare_enable(hqvdp->clk_pix_main)) {
+			drm_err_once(drm_plane->dev,
+				     "Failed to prepare/enable pix main clk\n");
+			return;
+		}
+
+		/* Register VTG Vsync callback to handle bottom fields */
+		if (sti_vtg_register_client(hqvdp->vtg, &hqvdp->vtg_nb, crtc)) {
+			drm_err_once(drm_plane->dev,
+				     "Cannot register VTG notifier\n");
+			clk_disable_unprepare(hqvdp->clk_pix_main);
+			return;
+		}
+		hqvdp->vtg_registered = true;
+	}
+
 	dst_x = newstate->crtc_x;
 	dst_y = newstate->crtc_y;
 	dst_w = clamp_val(newstate->crtc_w, 0, mode->hdisplay - dst_x);
Signature · 1 line
2.43.0