1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/dma-resv.h>
4 #include <linux/dma-fence-chain.h>
6 #include <drm/drm_atomic_state_helper.h>
7 #include <drm/drm_atomic_uapi.h>
8 #include <drm/drm_gem.h>
9 #include <drm/drm_gem_atomic_helper.h>
10 #include <drm/drm_gem_framebuffer_helper.h>
11 #include <drm/drm_simple_kms_helper.h>
13 #include "drm_internal.h"
18 * The GEM atomic helpers library implements generic atomic-commit
19 * functions for drivers that use GEM objects. Currently, it provides
20 * synchronization helpers, and plane state and framebuffer BO mappings
21 * for planes with shadow buffers.
23 * Before scanout, a plane's framebuffer needs to be synchronized with
24 * possible writers that draw into the framebuffer. All drivers should
25 * call drm_gem_plane_helper_prepare_fb() from their implementation of
26 * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from
27 * the framebuffer so that the DRM core can synchronize access automatically.
29 * drm_gem_plane_helper_prepare_fb() can also be used directly as
30 * implementation of prepare_fb. For drivers based on
31 * struct drm_simple_display_pipe, drm_gem_simple_display_pipe_prepare_fb()
32 * provides equivalent functionality.
36 * #include <drm/drm_gem_atomic_helper.h>
38 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
40 * . prepare_fb = drm_gem_plane_helper_prepare_fb,
43 * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
45 * . prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
48 * A driver using a shadow buffer copies the content of the shadow buffers
49 * into the HW's framebuffer memory during an atomic update. This requires
50 * a mapping of the shadow buffer into kernel address space. The mappings
51 * cannot be established by commit-tail functions, such as atomic_update,
52 * as this would violate locking rules around dma_buf_vmap().
54 * The helpers for shadow-buffered planes establish and release mappings,
55 * and provide struct drm_shadow_plane_state, which stores the plane's mapping
56 * for commit-tail functions.
58 * Shadow-buffered planes can easily be enabled by using the provided macros
59 * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
60 * These macros set up the plane and plane-helper callbacks to point to the
61 * shadow-buffer helpers.
65 * #include <drm/drm_gem_atomic_helper.h>
67 * struct drm_plane_funcs driver_plane_funcs = {
69 * DRM_GEM_SHADOW_PLANE_FUNCS,
72 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
74 * DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
77 * In the driver's atomic-update function, shadow-buffer mappings are available
78 * from the plane state. Use to_drm_shadow_plane_state() to upcast from
79 * struct drm_plane_state.
83 * void driver_plane_atomic_update(struct drm_plane *plane,
84 * struct drm_plane_state *old_plane_state)
86 * struct drm_plane_state *plane_state = plane->state;
87 * struct drm_shadow_plane_state *shadow_plane_state =
88 * to_drm_shadow_plane_state(plane_state);
90 * // access shadow buffer via shadow_plane_state->map
93 * A mapping address for each of the framebuffer's buffer object is stored in
94 * struct &drm_shadow_plane_state.map. The mappings are valid while the state
97 * Drivers that use struct drm_simple_display_pipe can use
98 * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
99 * callbacks. Access to shadow-buffer mappings is similar to regular
104 * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
106 * DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
109 * void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
110 * struct drm_crtc_state *crtc_state,
111 * struct drm_plane_state *plane_state)
113 * struct drm_shadow_plane_state *shadow_plane_state =
114 * to_drm_shadow_plane_state(plane_state);
116 * // access shadow buffer via shadow_plane_state->map
125 * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
127 * @state: Plane state the fence will be attached to
129 * This function extracts the exclusive fence from &drm_gem_object.resv and
130 * attaches it to plane state for the atomic helper to wait on. This is
131 * necessary to correctly implement implicit synchronization for any buffers
132 * shared as a struct &dma_buf. This function can be used as the
133 * &drm_plane_helper_funcs.prepare_fb callback.
135 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
136 * GEM based framebuffer drivers which have their buffers always pinned in
139 * This function is the default implementation for GEM drivers of
140 * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
142 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
143 struct drm_plane_state *state)
145 struct dma_fence *fence = dma_fence_get(state->fence);
146 enum dma_resv_usage usage;
154 * Only add the kernel fences here if there is already a fence set via
155 * explicit fencing interfaces on the atomic ioctl.
157 * This way explicit fencing can be used to overrule implicit fencing,
158 * which is important to make explicit fencing use-cases work: One
159 * example is using one buffer for 2 screens with different refresh
160 * rates. Implicit fencing will clamp rendering to the refresh rate of
161 * the slower screen, whereas explicit fence allows 2 independent
162 * render and display loops on a single buffer. If a driver allows
163 * obeys both implicit and explicit fences for plane updates, then it
164 * will break all the benefits of explicit fencing.
166 usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
168 for (i = 0; i < state->fb->format->num_planes; ++i) {
169 struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
170 struct dma_fence *new;
172 if (WARN_ON_ONCE(!obj))
175 ret = dma_resv_get_singleton(obj->resv, usage, &new);
180 struct dma_fence_chain *chain = dma_fence_chain_alloc();
187 dma_fence_chain_init(chain, fence, new, 1);
188 fence = &chain->base;
195 dma_fence_put(state->fence);
196 state->fence = fence;
200 dma_fence_put(fence);
203 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
206 * drm_gem_simple_display_pipe_prepare_fb - prepare_fb helper for &drm_simple_display_pipe
207 * @pipe: Simple display pipe
208 * @plane_state: Plane state
210 * This function uses drm_gem_plane_helper_prepare_fb() to extract the fences
211 * from &drm_gem_object.resv and attaches them to the plane state for the atomic
212 * helper to wait on. This is necessary to correctly implement implicit
213 * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
214 * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
216 * See drm_gem_plane_helper_prepare_fb() for a discussion of implicit and
217 * explicit fencing in atomic modeset updates.
219 int drm_gem_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
220 struct drm_plane_state *plane_state)
222 return drm_gem_plane_helper_prepare_fb(&pipe->plane, plane_state);
224 EXPORT_SYMBOL(drm_gem_simple_display_pipe_prepare_fb);
227 * Shadow-buffered Planes
231 * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
233 * @new_shadow_plane_state: the new shadow-buffered plane state
235 * This function duplicates shadow-buffered plane state. This is helpful for drivers
236 * that subclass struct drm_shadow_plane_state.
238 * The function does not duplicate existing mappings of the shadow buffers.
239 * Mappings are maintained during the atomic commit by the plane's prepare_fb
240 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
241 * for corresponding helpers.
244 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
245 struct drm_shadow_plane_state *new_shadow_plane_state)
247 __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
249 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
252 * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
255 * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
256 * shadow-buffered planes. It assumes the existing state to be of type
257 * struct drm_shadow_plane_state and it allocates the new state to be of this
260 * The function does not duplicate existing mappings of the shadow buffers.
261 * Mappings are maintained during the atomic commit by the plane's prepare_fb
262 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
263 * for corresponding helpers.
266 * A pointer to a new plane state on success, or NULL otherwise.
268 struct drm_plane_state *
269 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
271 struct drm_plane_state *plane_state = plane->state;
272 struct drm_shadow_plane_state *new_shadow_plane_state;
277 new_shadow_plane_state = kzalloc(sizeof(*new_shadow_plane_state), GFP_KERNEL);
278 if (!new_shadow_plane_state)
280 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
282 return &new_shadow_plane_state->base;
284 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
287 * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
288 * @shadow_plane_state: the shadow-buffered plane state
290 * This function cleans up shadow-buffered plane state. Helpful for drivers that
291 * subclass struct drm_shadow_plane_state.
293 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
295 __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
297 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
300 * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
302 * @plane_state: the plane state of type struct drm_shadow_plane_state
304 * This function implements struct &drm_plane_funcs.atomic_destroy_state
305 * for shadow-buffered planes. It expects that mappings of shadow buffers
306 * have been released already.
308 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
309 struct drm_plane_state *plane_state)
311 struct drm_shadow_plane_state *shadow_plane_state =
312 to_drm_shadow_plane_state(plane_state);
314 __drm_gem_destroy_shadow_plane_state(shadow_plane_state);
315 kfree(shadow_plane_state);
317 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
320 * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
322 * @shadow_plane_state: the shadow-buffered plane state
324 * This function resets state for shadow-buffered planes. Helpful
325 * for drivers that subclass struct drm_shadow_plane_state.
327 void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
328 struct drm_shadow_plane_state *shadow_plane_state)
330 __drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
332 EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
335 * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
338 * This function implements struct &drm_plane_funcs.reset_plane for
339 * shadow-buffered planes. It assumes the current plane state to be
340 * of type struct drm_shadow_plane and it allocates the new state of
343 void drm_gem_reset_shadow_plane(struct drm_plane *plane)
345 struct drm_shadow_plane_state *shadow_plane_state;
348 drm_gem_destroy_shadow_plane_state(plane, plane->state);
349 plane->state = NULL; /* must be set to NULL here */
352 shadow_plane_state = kzalloc(sizeof(*shadow_plane_state), GFP_KERNEL);
353 if (!shadow_plane_state)
355 __drm_gem_reset_shadow_plane(plane, shadow_plane_state);
357 EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
360 * drm_gem_prepare_shadow_fb - prepares shadow framebuffers
362 * @plane_state: the plane state of type struct drm_shadow_plane_state
364 * This function implements struct &drm_plane_helper_funcs.prepare_fb. It
365 * maps all buffer objects of the plane's framebuffer into kernel address
366 * space and stores them in &struct drm_shadow_plane_state.map. The
367 * framebuffer will be synchronized as part of the atomic commit.
369 * See drm_gem_cleanup_shadow_fb() for cleanup.
372 * 0 on success, or a negative errno code otherwise.
374 int drm_gem_prepare_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
376 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
377 struct drm_framebuffer *fb = plane_state->fb;
383 ret = drm_gem_plane_helper_prepare_fb(plane, plane_state);
387 return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
389 EXPORT_SYMBOL(drm_gem_prepare_shadow_fb);
392 * drm_gem_cleanup_shadow_fb - releases shadow framebuffers
394 * @plane_state: the plane state of type struct drm_shadow_plane_state
396 * This function implements struct &drm_plane_helper_funcs.cleanup_fb.
397 * This function unmaps all buffer objects of the plane's framebuffer.
399 * See drm_gem_prepare_shadow_fb() for more information.
401 void drm_gem_cleanup_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
403 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
404 struct drm_framebuffer *fb = plane_state->fb;
409 drm_gem_fb_vunmap(fb, shadow_plane_state->map);
411 EXPORT_SYMBOL(drm_gem_cleanup_shadow_fb);
414 * drm_gem_simple_kms_prepare_shadow_fb - prepares shadow framebuffers
415 * @pipe: the simple display pipe
416 * @plane_state: the plane state of type struct drm_shadow_plane_state
418 * This function implements struct drm_simple_display_funcs.prepare_fb. It
419 * maps all buffer objects of the plane's framebuffer into kernel address
420 * space and stores them in struct drm_shadow_plane_state.map. The
421 * framebuffer will be synchronized as part of the atomic commit.
423 * See drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
426 * 0 on success, or a negative errno code otherwise.
428 int drm_gem_simple_kms_prepare_shadow_fb(struct drm_simple_display_pipe *pipe,
429 struct drm_plane_state *plane_state)
431 return drm_gem_prepare_shadow_fb(&pipe->plane, plane_state);
433 EXPORT_SYMBOL(drm_gem_simple_kms_prepare_shadow_fb);
436 * drm_gem_simple_kms_cleanup_shadow_fb - releases shadow framebuffers
437 * @pipe: the simple display pipe
438 * @plane_state: the plane state of type struct drm_shadow_plane_state
440 * This function implements struct drm_simple_display_funcs.cleanup_fb.
441 * This function unmaps all buffer objects of the plane's framebuffer.
443 * See drm_gem_simple_kms_prepare_shadow_fb().
445 void drm_gem_simple_kms_cleanup_shadow_fb(struct drm_simple_display_pipe *pipe,
446 struct drm_plane_state *plane_state)
448 drm_gem_cleanup_shadow_fb(&pipe->plane, plane_state);
450 EXPORT_SYMBOL(drm_gem_simple_kms_cleanup_shadow_fb);
453 * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
454 * @pipe: the simple display pipe
456 * This function implements struct drm_simple_display_funcs.reset_plane
457 * for shadow-buffered planes.
459 void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
461 drm_gem_reset_shadow_plane(&pipe->plane);
463 EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
466 * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
467 * @pipe: the simple display pipe
469 * This function implements struct drm_simple_display_funcs.duplicate_plane_state
470 * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
471 * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
472 * and cleanup_fb helpers.
475 * A pointer to a new plane state on success, or NULL otherwise.
477 struct drm_plane_state *
478 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
480 return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
482 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
485 * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
486 * @pipe: the simple display pipe
487 * @plane_state: the plane state of type struct drm_shadow_plane_state
489 * This function implements struct drm_simple_display_funcs.destroy_plane_state
490 * for shadow-buffered planes. It expects that mappings of shadow buffers
491 * have been released already.
493 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
494 struct drm_plane_state *plane_state)
496 drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
498 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);