drm/tiny: arcpgu: Replace simple display pipe with regular atomic helpers
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v2 1/1 drm/tiny: arcpgu: Replace simple display pipe with regular atomic helpers
The drm_simple_display_pipe helper is considered legacy/deprecated in favor of explicitly managing individual plane, CRTC, and encoder objects using standard atomic helpers.
Migrate arcpgu to initialize its primary plane, CRTC, and encoder explicitly, and route mode validation, enable, and disable hooks through drm_crtc_helper_funcs, and plane updates through drm_plane_helper_funcs.
---
v2:
- Add .prepare_fb callback to support implicit synchronization.
- Add .atomic_check using drm_atomic_helper_check_plane_state() to validate hardware scaling/clipping constraints.
- Implement arc_pgu_plane_atomic_disable() to stop DMA and clear buffer address register, preventing hardware DMA scanout UAF.
drivers/gpu/drm/tiny/arcpgu.c | 159 ++++++++++++++++++++++++++-------- 1 file changed, 125 insertions(+), 34 deletions(-)
drivers/gpu/drm/tiny/arcpgu.c +125 -34
@@ -5,6 +5,8 @@* Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com) */ +#include "drm/drm_gem_atomic_helper.h" +#include <linux/clk.h> #include <drm/clients/drm_client_setup.h>
@@ -23,6 +25,8 @@#include <drm/drm_of.h> #include <drm/drm_probe_helper.h> #include <drm/drm_simple_kms_helper.h> +#include <drm/drm_atomic.h> +#include <drm/drm_bridge.h> #include <linux/dma-mapping.h> #include <linux/module.h> #include <linux/of_reserved_mem.h>
@@ -52,13 +56,17 @@ struct arcpgu_drm_private {struct drm_device drm;void __iomem *regs;struct clk *clk; -struct drm_simple_display_pipe pipe; +struct drm_plane plane; +struct drm_crtc crtc; +struct drm_encoder encoder;struct drm_connector sim_conn; };#define dev_to_arcpgu (x) container_of(x, struct arcpgu_drm_private, drm) -#define pipe_to_arcpgu_priv (x) container_of(x, struct arcpgu_drm_private, pipe) +#define crtc_to_arcpgu_priv (x) container_of(x, struct arcpgu_drm_private, crtc) + +#define plane_to_arcpgu (x) container_of(x, struct arcpgu_drm_private, plane)static inline void arc_pgu_write (struct arcpgu_drm_private *arcpgu,unsigned int reg,u32 value)
@@ -117,8 +125,8 @@ static const u32 arc_pgu_supported_formats[] = {static void arc_pgu_set_pxl_fmt (struct arcpgu_drm_private *arcpgu) { -const struct drm_framebuffer *fb = arcpgu->pipe.plane.state->fb; -uint32_t pixel_format = fb->format->format; +const struct drm_framebuffer *fb = arcpgu->plane.state->fb; +u32 pixel_format = fb->format->format;u32 format =DRM_FORMAT_INVALID ;int i;u32 reg_ctrl;
@@ -139,10 +147,10 @@ static void arc_pgu_set_pxl_fmt(struct arcpgu_drm_private *arcpgu)arc_pgu_write (arcpgu,ARCPGU_REG_CTRL , reg_ctrl); } -static enum drm_mode_status arc_pgu_mode_valid (struct drm_simple_display_pipe *pipe, -const struct drm_display_mode *mode) +static enum drm_mode_status arc_pgu_crtc_mode_valid (struct drm_crtc *crtc, +const struct drm_display_mode *mode) { -struct arcpgu_drm_private *arcpgu =pipe_to_arcpgu_priv (pipe); +struct arcpgu_drm_private *arcpgu =crtc_to_arcpgu_priv (crtc);long rate, clk_rate = mode->clock *1000 ;long diff = clk_rate /200 ;/* +-0.5% allowed by HDMI spec */
@@ -155,7 +163,7 @@ static enum drm_mode_status arc_pgu_mode_valid(struct drm_simple_display_pipe *pstatic void arc_pgu_mode_set (struct arcpgu_drm_private *arcpgu) { -struct drm_display_mode *m = &arcpgu->pipe.crtc.state->adjusted_mode; +struct drm_display_mode *m = &arcpgu->crtc.state->adjusted_mode;u32 val; arc_pgu_write(arcpgu, ARCPGU_REG_FMT,
@@ -194,49 +202,107 @@ static void arc_pgu_mode_set(struct arcpgu_drm_private *arcpgu)clk_set_rate (arcpgu->clk, m->crtc_clock *1000 ); } -static void arc_pgu_enable (struct drm_simple_display_pipe *pipe, -struct drm_crtc_state *crtc_state, -struct drm_plane_state *plane_state) +static void arc_pgu_crtc_atomic_enable (struct drm_crtc *crtc, +struct drm_atomic_commit *state) { -struct arcpgu_drm_private *arcpgu =pipe_to_arcpgu_priv (pipe); +struct arcpgu_drm_private *arcpgu =crtc_to_arcpgu_priv (crtc);arc_pgu_mode_set (arcpgu);clk_prepare_enable (arcpgu->clk);arc_pgu_write (arcpgu,ARCPGU_REG_CTRL ,arc_pgu_read (arcpgu,ARCPGU_REG_CTRL ) | -ARCPGU_CTRL_ENABLE_MASK ); +ARCPGU_CTRL_ENABLE_MASK ); } -static void arc_pgu_disable (struct drm_simple_display_pipe *pipe) +static void arc_pgu_crtc_atomic_disable (struct drm_crtc *crtc, +struct drm_atomic_commit *state) { -struct arcpgu_drm_private *arcpgu =pipe_to_arcpgu_priv (pipe); +struct arcpgu_drm_private *arcpgu =crtc_to_arcpgu_priv (crtc);clk_disable_unprepare (arcpgu->clk);arc_pgu_write (arcpgu,ARCPGU_REG_CTRL , -arc_pgu_read (arcpgu,ARCPGU_REG_CTRL ) & - ~ARCPGU_CTRL_ENABLE_MASK ); +arc_pgu_read (arcpgu,ARCPGU_REG_CTRL ) & + ~ARCPGU_CTRL_ENABLE_MASK ); } -static void arc_pgu_update (struct drm_simple_display_pipe *pipe, -struct drm_plane_state *state) +static void arc_pgu_plane_atomic_update (struct drm_plane *plane, +struct drm_atomic_commit *state) { -struct arcpgu_drm_private *arcpgu; +struct arcpgu_drm_private *arcpgu =plane_to_arcpgu (plane); +struct drm_plane_state *new_plane_state =drm_atomic_get_new_plane_state (state, plane);struct drm_gem_dma_object *gem; -if (!pipe->plane.state->fb) +if (!new_plane_state->fb)return ; - arcpgu =pipe_to_arcpgu_priv (pipe); - gem =drm_fb_dma_get_gem_obj (pipe->plane.state->fb,0 ); + gem =drm_fb_dma_get_gem_obj (new_plane_state->fb,0 );arc_pgu_write (arcpgu,ARCPGU_REG_BUF0_ADDR , gem->dma_addr); } -static const struct drm_simple_display_pipe_funcs arc_pgu_pipe_funcs = { - .update = arc_pgu_update, - .mode_valid = arc_pgu_mode_valid, - .enable = arc_pgu_enable, - .disable = arc_pgu_disable, +static void arc_pgu_plane_atomic_disable (struct drm_plane *plane, +struct drm_atomic_commit *state) +{ +struct arcpgu_drm_private *arcpgu =plane_to_arcpgu (plane); + +arc_pgu_write (arcpgu,ARCPGU_REG_BUF0_ADDR ,0 ); +arc_pgu_write (arcpgu,ARCPGU_REG_CTRL , +arc_pgu_read (arcpgu,ARCPGU_REG_CTRL ) & + ~ARCPGU_CTRL_ENABLE_MASK ); +} + +static int arc_pgu_plane_atomic_check (struct drm_plane *plane, +struct drm_atomic_commit *state) +{ +struct drm_plane_state *new_plane_state =drm_atomic_get_new_plane_state (state, plane); +struct drm_crtc_state *crtc_state; + +if (!new_plane_state->crtc) +return 0 ; + + crtc_state =drm_atomic_get_new_crtc_state (state, new_plane_state->crtc); +if (!crtc_state) +return -EINVAL ; + +return drm_atomic_helper_check_plane_state (new_plane_state, crtc_state, +DRM_PLANE_NO_SCALING , +DRM_PLANE_NO_SCALING , + false, true); +} + +static const struct drm_crtc_helper_funcs arc_pgu_crtc_helper_funcs = { + .mode_valid = arc_pgu_crtc_mode_valid, + .atomic_enable = arc_pgu_crtc_atomic_enable, + .atomic_disable = arc_pgu_crtc_atomic_disable, +}; + +static const struct drm_crtc_funcs arc_pgu_crtc_funcs = { + .reset = drm_atomic_helper_crtc_reset, + .destroy = drm_crtc_cleanup, + .set_config = drm_atomic_helper_set_config, + .page_flip = drm_atomic_helper_page_flip, + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, +}; + +static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = { + .atomic_update = arc_pgu_plane_atomic_update, + .prepare_fb = drm_gem_plane_helper_prepare_fb, + .atomic_check = arc_pgu_plane_atomic_check, + .atomic_disable = arc_pgu_plane_atomic_disable, +}; + +static const struct drm_plane_funcs arc_pgu_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, +}; + +static const struct drm_encoder_funcs arc_pgu_encoder_funcs = { + .destroy = drm_encoder_cleanup };static const struct drm_mode_config_funcs arcpgu_drm_modecfg_funcs = {
@@ -301,13 +367,38 @@ static int arcpgu_load(struct arcpgu_drm_private *arcpgu)return ret; } - ret =drm_simple_display_pipe_init (drm, &arcpgu->pipe, &arc_pgu_pipe_funcs, - arc_pgu_supported_formats, -ARRAY_SIZE (arc_pgu_supported_formats), -NULL , connector); + ret =drm_universal_plane_init (drm, &arcpgu->plane,0 , + &arc_pgu_plane_funcs, arc_pgu_supported_formats, +ARRAY_SIZE (arc_pgu_supported_formats),NULL , +DRM_PLANE_TYPE_PRIMARY ,NULL ); + +if (ret) +return ret; + +drm_plane_helper_add (&arcpgu->plane, &arc_pgu_plane_helper_funcs); + + ret =drm_crtc_init_with_planes (drm, &arcpgu->crtc, &arcpgu->plane,NULL , + &arc_pgu_crtc_funcs,NULL ); +if (ret)return ret; +drm_crtc_helper_add (&arcpgu->crtc, &arc_pgu_crtc_helper_funcs); + + ret =drm_encoder_init (drm, &arcpgu->encoder, &arc_pgu_encoder_funcs, +DRM_MODE_ENCODER_NONE ,NULL ); + +if (ret) +return ret; + + arcpgu->encoder.possible_crtcs =drm_crtc_mask (&arcpgu->crtc); + +if (connector) { + ret =drm_connector_attach_encoder (connector, &arcpgu->encoder); +if (ret) +return ret; + } +if (encoder_node) {/* Locate drm bridge from the hdmi encoder DT node */ struct drm_bridge *bridge __free(drm_bridge_put) =
@@ -315,7 +406,7 @@ static int arcpgu_load(struct arcpgu_drm_private *arcpgu)if (!bridge)return -EPROBE_DEFER ; - ret =drm_simple_display_pipe_attach_bridge (&arcpgu->pipe, bridge); + ret =drm_bridge_attach (&arcpgu->encoder, bridge,NULL ,0 );if (ret)return ret; }
@@ -342,7 +433,7 @@ static int arcpgu_show_pxlclock(struct seq_file *m, void *arg)struct drm_device *drm = node->minor->dev;struct arcpgu_drm_private *arcpgu =dev_to_arcpgu (drm);unsigned long clkrate =clk_get_rate (arcpgu->clk); -unsigned long mode_clock = arcpgu->pipe.crtc.mode.crtc_clock *1000 ; +unsigned long mode_clock = arcpgu->crtc.mode.crtc_clock *1000 ;seq_printf (m,"hw : %lu\n" , clkrate);seq_printf (m,"mode: %lu\n" , mode_clock);
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] Missing validation allows the CRTC to be enabled without a primary plane, causing a NULL…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] Missing validation allows the CRTC to be enabled without a primary plane, causing a NULL pointer dereference in arc_pgu_set_pxl_fmt().
- [Critical] The CRTC atomic_enable hook accesses arcpgu->plane.state directly without holding the plane's lock, allowing a concurrent plane update to cause a Use-After-Free.
- [High] Disabling the primary plane incorrectly clears the global CRTC enable bit, permanently desyncing hardware state from the DRM core.
- [High] Missing .cleanup_fb callback causes GEM object pin count leak on every plane update.
--
commit a8222dfe6a09a56b144901613fe5caafe6889898
Author: Davide Bonatto <[email protected]>
drm/tiny: arcpgu: Replace simple display pipe with regular atomic helpers
This commit migrates the arcpgu driver from the legacy drm_simple_display_pipe infrastructure to explicitly managing plane, CRTC, and encoder objects using standard atomic helpers.
Quoted 4 lines
drivers/gpu/drm/tiny/arcpgu.c +0
-0
[ ... ]
drivers/gpu/drm/tiny/arcpgu.c static const u32 arc_pgu_supported_formats[] = { · line 129
Quoted 8 lines
drivers/gpu/drm/tiny/arcpgu.c +2
-2@@ -117,8 +125,8 @@ static const u32 arc_pgu_supported_formats[] = {static void arc_pgu_set_pxl_fmt (struct arcpgu_drm_private *arcpgu) { -const struct drm_framebuffer *fb = arcpgu->pipe.plane.state->fb; -uint32_t pixel_format = fb->format->format; +const struct drm_framebuffer *fb = arcpgu->plane.state->fb; +u32 pixel_format = fb->format->format;
[Severity: Critical]
What happens if userspace enables the CRTC but disables the primary plane?
In that scenario, fb would be NULL, and the subsequent fb->format->format dereference would crash the kernel. The driver does not appear to have a CRTC atomic_check hook to prevent enabling the CRTC without a valid plane, and arc_pgu_plane_atomic_check() gracefully returns 0 when the framebuffer is missing.
[Severity: Critical]
Can a concurrent plane update cause a use-after-free here?
This function is called from the CRTC enable path:arc_pgu_crtc_atomic_enable() -> arc_pgu_mode_set() -> arc_pgu_set_pxl_fmt()
Since there is no CRTC atomic_check hook calling drm_atomic_add_affected_planes(), a modeset only locks the CRTC and not the plane. A concurrent plane update (page flip) could acquire the plane lock, swap the state, and free the old framebuffer, causing the modesetting thread to read freed memory when locklessly accessing fb->format->format.
[ ... ]
drivers/gpu/drm/tiny/arcpgu.c static void arc_pgu_mode_set(struct arcpgu_drm_private *arcpgu) · line 252
Quoted 10 lines
drivers/gpu/drm/tiny/arcpgu.c +10
-0+static void arc_pgu_plane_atomic_disable (struct drm_plane *plane, +struct drm_atomic_commit *state) +{ +struct arcpgu_drm_private *arcpgu =plane_to_arcpgu (plane); + +arc_pgu_write (arcpgu,ARCPGU_REG_BUF0_ADDR ,0 ); +arc_pgu_write (arcpgu,ARCPGU_REG_CTRL , +arc_pgu_read (arcpgu,ARCPGU_REG_CTRL ) & + ~ARCPGU_CTRL_ENABLE_MASK ); +}
[Severity: High]
Does clearing ARCPGU_CTRL_ENABLE_MASK permanently desync the hardware state?
This appears to be the global CRTC enable bit. If userspace disables just the primary plane, this clears the global enable bit for the entire CRTC. When the plane is later re-enabled, arc_pgu_plane_atomic_update() sets the buffer address but does not restore the CRTC enable bit, leaving the display controller completely disabled and causing VBLANK timeouts.
[ ... ]
drivers/gpu/drm/tiny/arcpgu.c static void arc_pgu_mode_set(struct arcpgu_drm_private *arcpgu) · line 293
Quoted 6 lines
drivers/gpu/drm/tiny/arcpgu.c +6
-0+static const struct drm_plane_helper_funcs arc_pgu_plane_helper_funcs = { + .atomic_update = arc_pgu_plane_atomic_update, + .prepare_fb = drm_gem_plane_helper_prepare_fb, + .atomic_check = arc_pgu_plane_atomic_check, + .atomic_disable = arc_pgu_plane_atomic_disable, +};
[Severity: High]
Does this leak the GEM object pin count on every plane update?
The prepare_fb hook is set to drm_gem_plane_helper_prepare_fb, which pins the backing BO for the framebuffer. However, there is no corresponding cleanup_fb callback (drm_gem_plane_helper_cleanup_fb) to unpin it. Whenever a framebuffer is replaced during a page flip or disabled, its pin count will never be decremented.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1