From: Thomas Zimmermann tzimmermann@suse.de
[ Upstream commit 136c374d8c80378d2982a46b2adabfc007299641 ]
Use DRM's shadow-plane helper to map and access the GEM object's buffer within kernel address space. Encapsulates the vmap logic in the GEM-DMA helpers.
The sharp-memory driver currently reads the vaddr field from the GME buffer object directly. This only works because GEM code 'automagically' sets vaddr.
Shadow-plane helpers perform the same steps, but with correct abstraction behind drm_gem_vmap(). The shadow-plane state provides the buffer address in kernel address space and the format-conversion state.
v2: - fix typo in commit description
Signed-off-by: Thomas Zimmermann tzimmermann@suse.de Reviewed-by: Javier Martinez Canillas javierm@redhat.com Link: https://lore.kernel.org/r/20250627152327.8244-1-tzimmermann@suse.de Signed-off-by: Sasha Levin sashal@kernel.org ---
LLM Generated explanations, may be completely bogus:
YES
Explanation - Fixes an architectural misuse and a real correctness bug. The driver previously read the DMA GEM buffer’s CPU pointer directly via `dma_obj->vaddr` in `sharp_memory_set_tx_buffer_data()` and fed it to `drm_fb_xrgb8888_to_mono()`. That bypasses the proper vmap abstraction and, more importantly, fails to account for framebuffer plane offsets. The conversion helpers expect src pointers that already include `fb->offsets[0]`. See `drm_fb_xrgb8888_to_mono()` which uses `src[0].vaddr` directly (drivers/gpu/drm/drm_format_helper.c:1210) and does not add `fb->offsets[0]`. Properly computed data pointers are provided by `drm_gem_fb_vmap()` into the shadow state’s `data[]` array (drivers/gpu/drm/drm_gem_framebuffer_helper.c:352 and :320), where `data[i]` is `map[i] + fb->offsets[i]`. This commit switches to that path, fixing potential misreads when offsets are non‑zero. - Uses standard GEM shadow-plane helpers already present in stable. The patch adopts `DRM_GEM_SHADOW_PLANE_HELPER_FUNCS` and `DRM_GEM_SHADOW_PLANE_FUNCS`, which wire up `.begin_fb_access`/`.end_fb_access` and state reset/dup/destroy for shadow-buffered planes. These helpers exist in stable trees (e.g., 6.12.x). See include/drm/drm_gem_atomic_helper.h:109 and :125 and their implementation in drivers/gpu/drm/drm_gem_atomic_helper.c:365 and :416, which call `drm_gem_fb_vmap()`/`drm_gem_fb_vunmap()` to manage mappings correctly and surface `shadow_plane_state->data` for use in `atomic_update`. - Changes are small and self-contained to the tiny driver. Only `drivers/gpu/drm/tiny/sharp-memory.c` is touched: - `sharp_memory_set_tx_buffer_data()` now takes `const struct iosys_map *vmap` and uses that instead of `dma_obj->vaddr`. - `sharp_memory_update_display()` and `sharp_memory_fb_dirty()` are updated to thread `vmap` through. - `sharp_memory_plane_atomic_update()` switches to `to_drm_shadow_plane_state(plane_state)` and uses `shadow_plane_state->data` and `shadow_plane_state->fmtcnv_state`, removing manual `drm_format_conv_state` handling. - Plane helper/func tables add `DRM_GEM_SHADOW_PLANE_HELPER_FUNCS` and replace manual reset/dup/destroy with `DRM_GEM_SHADOW_PLANE_FUNCS`. - No feature additions or architecture changes. This is a correctness/abstraction fix with minimal surface area. The functional behavior (convert XR24 to mono and push via SPI) remains the same; mapping and format-conversion state are now handled via shared helpers. - Low regression risk and consistent with other drivers. Multiple DRM tiny and simple drivers already use these shadow-plane helpers in stable (for example tiny/simpledrm.c:690 and tiny/ofdrm.c:876, vboxvideo/vbox_mode.c:474, and ast/ast_mode.c:721 reference the same macros). The conversion helper semantics remain unchanged. - Stable policy alignment. While the commit message doesn’t carry “Fixes:”/“Cc: stable”, it resolves a real bug (incorrect source pointer when `fb->offsets[0] != 0`) and removes a layering violation that could break if underlying implementation details change. It is localized, low-risk, and improves ABI/locking correctness by using `drm_gem_fb_vmap()` and the begin/end access hooks.
Notes - Ensure the target stable branch actually contains this driver. If the driver isn’t present in the target tree, the backport is moot. Where present and the shadow-plane helpers are available (they are in 6.12.x), this change should apply cleanly.
drivers/gpu/drm/tiny/sharp-memory.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/drivers/gpu/drm/tiny/sharp-memory.c b/drivers/gpu/drm/tiny/sharp-memory.c index 03d2850310c47..64272cd0f6e22 100644 --- a/drivers/gpu/drm/tiny/sharp-memory.c +++ b/drivers/gpu/drm/tiny/sharp-memory.c @@ -126,28 +126,28 @@ static inline void sharp_memory_set_tx_buffer_addresses(u8 *buffer,
static void sharp_memory_set_tx_buffer_data(u8 *buffer, struct drm_framebuffer *fb, + const struct iosys_map *vmap, struct drm_rect clip, u32 pitch, struct drm_format_conv_state *fmtcnv_state) { int ret; - struct iosys_map dst, vmap; - struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0); + struct iosys_map dst;
ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); if (ret) return;
iosys_map_set_vaddr(&dst, buffer); - iosys_map_set_vaddr(&vmap, dma_obj->vaddr);
- drm_fb_xrgb8888_to_mono(&dst, &pitch, &vmap, fb, &clip, fmtcnv_state); + drm_fb_xrgb8888_to_mono(&dst, &pitch, vmap, fb, &clip, fmtcnv_state);
drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); }
static int sharp_memory_update_display(struct sharp_memory_device *smd, struct drm_framebuffer *fb, + const struct iosys_map *vmap, struct drm_rect clip, struct drm_format_conv_state *fmtcnv_state) { @@ -163,7 +163,7 @@ static int sharp_memory_update_display(struct sharp_memory_device *smd, sharp_memory_set_tx_buffer_mode(&tx_buffer[0], SHARP_MEMORY_DISPLAY_UPDATE_MODE, vcom); sharp_memory_set_tx_buffer_addresses(&tx_buffer[1], clip, pitch); - sharp_memory_set_tx_buffer_data(&tx_buffer[2], fb, clip, pitch, fmtcnv_state); + sharp_memory_set_tx_buffer_data(&tx_buffer[2], fb, vmap, clip, pitch, fmtcnv_state);
ret = sharp_memory_spi_write(smd->spi, tx_buffer, tx_buffer_size);
@@ -206,7 +206,8 @@ static int sharp_memory_clear_display(struct sharp_memory_device *smd) return ret; }
-static void sharp_memory_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *rect, +static void sharp_memory_fb_dirty(struct drm_framebuffer *fb, const struct iosys_map *vmap, + struct drm_rect *rect, struct drm_format_conv_state *fmtconv_state) { struct drm_rect clip; @@ -218,7 +219,7 @@ static void sharp_memory_fb_dirty(struct drm_framebuffer *fb, struct drm_rect *r clip.y1 = rect->y1; clip.y2 = rect->y2;
- sharp_memory_update_display(smd, fb, clip, fmtconv_state); + sharp_memory_update_display(smd, fb, vmap, clip, fmtconv_state); }
static int sharp_memory_plane_atomic_check(struct drm_plane *plane, @@ -242,7 +243,7 @@ static void sharp_memory_plane_atomic_update(struct drm_plane *plane, { struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane); struct drm_plane_state *plane_state = plane->state; - struct drm_format_conv_state fmtcnv_state = DRM_FORMAT_CONV_STATE_INIT; + struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); struct sharp_memory_device *smd; struct drm_rect rect;
@@ -251,15 +252,15 @@ static void sharp_memory_plane_atomic_update(struct drm_plane *plane, return;
if (drm_atomic_helper_damage_merged(old_state, plane_state, &rect)) - sharp_memory_fb_dirty(plane_state->fb, &rect, &fmtcnv_state); - - drm_format_conv_state_release(&fmtcnv_state); + sharp_memory_fb_dirty(plane_state->fb, shadow_plane_state->data, + &rect, &shadow_plane_state->fmtcnv_state); }
static const struct drm_plane_helper_funcs sharp_memory_plane_helper_funcs = { .prepare_fb = drm_gem_plane_helper_prepare_fb, .atomic_check = sharp_memory_plane_atomic_check, .atomic_update = sharp_memory_plane_atomic_update, + DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, };
static bool sharp_memory_format_mod_supported(struct drm_plane *plane, @@ -273,9 +274,7 @@ static const struct drm_plane_funcs sharp_memory_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, .format_mod_supported = sharp_memory_format_mod_supported, };