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_framebuffer.h>
9 #include <drm/drm_gem.h>
10 #include <drm/drm_gem_atomic_helper.h>
11 #include <drm/drm_gem_framebuffer_helper.h>
12 #include <drm/drm_simple_kms_helper.h>
14 #include "drm_internal.h"
19 * The GEM atomic helpers library implements generic atomic-commit
20 * functions for drivers that use GEM objects. Currently, it provides
21 * synchronization helpers, and plane state and framebuffer BO mappings
22 * for planes with shadow buffers.
24 * Before scanout, a plane's framebuffer needs to be synchronized with
25 * possible writers that draw into the framebuffer. All drivers should
26 * call drm_gem_plane_helper_prepare_fb() from their implementation of
27 * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from
28 * the framebuffer so that the DRM core can synchronize access automatically.
30 * drm_gem_plane_helper_prepare_fb() can also be used directly as
31 * implementation of prepare_fb. For drivers based on
32 * struct drm_simple_display_pipe, drm_gem_simple_display_pipe_prepare_fb()
33 * provides equivalent functionality.
37 * #include <drm/drm_gem_atomic_helper.h>
39 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
41 * . prepare_fb = drm_gem_plane_helper_prepare_fb,
44 * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
46 * . prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
49 * A driver using a shadow buffer copies the content of the shadow buffers
50 * into the HW's framebuffer memory during an atomic update. This requires
51 * a mapping of the shadow buffer into kernel address space. The mappings
52 * cannot be established by commit-tail functions, such as atomic_update,
53 * as this would violate locking rules around dma_buf_vmap().
55 * The helpers for shadow-buffered planes establish and release mappings,
56 * and provide struct drm_shadow_plane_state, which stores the plane's mapping
57 * for commit-tail functions.
59 * Shadow-buffered planes can easily be enabled by using the provided macros
60 * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
61 * These macros set up the plane and plane-helper callbacks to point to the
62 * shadow-buffer helpers.
66 * #include <drm/drm_gem_atomic_helper.h>
68 * struct drm_plane_funcs driver_plane_funcs = {
70 * DRM_GEM_SHADOW_PLANE_FUNCS,
73 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
75 * DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
78 * In the driver's atomic-update function, shadow-buffer mappings are available
79 * from the plane state. Use to_drm_shadow_plane_state() to upcast from
80 * struct drm_plane_state.
84 * void driver_plane_atomic_update(struct drm_plane *plane,
85 * struct drm_plane_state *old_plane_state)
87 * struct drm_plane_state *plane_state = plane->state;
88 * struct drm_shadow_plane_state *shadow_plane_state =
89 * to_drm_shadow_plane_state(plane_state);
91 * // access shadow buffer via shadow_plane_state->map
94 * A mapping address for each of the framebuffer's buffer object is stored in
95 * struct &drm_shadow_plane_state.map. The mappings are valid while the state
98 * Drivers that use struct drm_simple_display_pipe can use
99 * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
100 * callbacks. Access to shadow-buffer mappings is similar to regular
105 * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
107 * DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
110 * void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
111 * struct drm_crtc_state *crtc_state,
112 * struct drm_plane_state *plane_state)
114 * struct drm_shadow_plane_state *shadow_plane_state =
115 * to_drm_shadow_plane_state(plane_state);
117 * // access shadow buffer via shadow_plane_state->map
126 * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
128 * @state: Plane state the fence will be attached to
130 * This function extracts the exclusive fence from &drm_gem_object.resv and
131 * attaches it to plane state for the atomic helper to wait on. This is
132 * necessary to correctly implement implicit synchronization for any buffers
133 * shared as a struct &dma_buf. This function can be used as the
134 * &drm_plane_helper_funcs.prepare_fb callback.
136 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
137 * GEM based framebuffer drivers which have their buffers always pinned in
140 * This function is the default implementation for GEM drivers of
141 * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
143 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
144 struct drm_plane_state *state)
146 struct dma_fence *fence = dma_fence_get(state->fence);
147 enum dma_resv_usage usage;
155 * Only add the kernel fences here if there is already a fence set via
156 * explicit fencing interfaces on the atomic ioctl.
158 * This way explicit fencing can be used to overrule implicit fencing,
159 * which is important to make explicit fencing use-cases work: One
160 * example is using one buffer for 2 screens with different refresh
161 * rates. Implicit fencing will clamp rendering to the refresh rate of
162 * the slower screen, whereas explicit fence allows 2 independent
163 * render and display loops on a single buffer. If a driver allows
164 * obeys both implicit and explicit fences for plane updates, then it
165 * will break all the benefits of explicit fencing.
167 usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
169 for (i = 0; i < state->fb->format->num_planes; ++i) {
170 struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
171 struct dma_fence *new;
178 ret = dma_resv_get_singleton(obj->resv, usage, &new);
183 struct dma_fence_chain *chain = dma_fence_chain_alloc();
190 dma_fence_chain_init(chain, fence, new, 1);
191 fence = &chain->base;
198 dma_fence_put(state->fence);
199 state->fence = fence;
203 dma_fence_put(fence);
206 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
209 * drm_gem_simple_display_pipe_prepare_fb - prepare_fb helper for &drm_simple_display_pipe
210 * @pipe: Simple display pipe
211 * @plane_state: Plane state
213 * This function uses drm_gem_plane_helper_prepare_fb() to extract the fences
214 * from &drm_gem_object.resv and attaches them to the plane state for the atomic
215 * helper to wait on. This is necessary to correctly implement implicit
216 * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
217 * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
219 * See drm_gem_plane_helper_prepare_fb() for a discussion of implicit and
220 * explicit fencing in atomic modeset updates.
222 int drm_gem_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
223 struct drm_plane_state *plane_state)
225 return drm_gem_plane_helper_prepare_fb(&pipe->plane, plane_state);
227 EXPORT_SYMBOL(drm_gem_simple_display_pipe_prepare_fb);
230 * Shadow-buffered Planes
234 * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
236 * @new_shadow_plane_state: the new shadow-buffered plane state
238 * This function duplicates shadow-buffered plane state. This is helpful for drivers
239 * that subclass struct drm_shadow_plane_state.
241 * The function does not duplicate existing mappings of the shadow buffers.
242 * Mappings are maintained during the atomic commit by the plane's prepare_fb
243 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
244 * for corresponding helpers.
247 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
248 struct drm_shadow_plane_state *new_shadow_plane_state)
250 __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
252 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
255 * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
258 * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
259 * shadow-buffered planes. It assumes the existing state to be of type
260 * struct drm_shadow_plane_state and it allocates the new state to be of this
263 * The function does not duplicate existing mappings of the shadow buffers.
264 * Mappings are maintained during the atomic commit by the plane's prepare_fb
265 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
266 * for corresponding helpers.
269 * A pointer to a new plane state on success, or NULL otherwise.
271 struct drm_plane_state *
272 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
274 struct drm_plane_state *plane_state = plane->state;
275 struct drm_shadow_plane_state *new_shadow_plane_state;
280 new_shadow_plane_state = kzalloc(sizeof(*new_shadow_plane_state), GFP_KERNEL);
281 if (!new_shadow_plane_state)
283 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
285 return &new_shadow_plane_state->base;
287 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
290 * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
291 * @shadow_plane_state: the shadow-buffered plane state
293 * This function cleans up shadow-buffered plane state. Helpful for drivers that
294 * subclass struct drm_shadow_plane_state.
296 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
298 __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
300 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
303 * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
305 * @plane_state: the plane state of type struct drm_shadow_plane_state
307 * This function implements struct &drm_plane_funcs.atomic_destroy_state
308 * for shadow-buffered planes. It expects that mappings of shadow buffers
309 * have been released already.
311 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
312 struct drm_plane_state *plane_state)
314 struct drm_shadow_plane_state *shadow_plane_state =
315 to_drm_shadow_plane_state(plane_state);
317 __drm_gem_destroy_shadow_plane_state(shadow_plane_state);
318 kfree(shadow_plane_state);
320 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
323 * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
325 * @shadow_plane_state: the shadow-buffered plane state
327 * This function resets state for shadow-buffered planes. Helpful
328 * for drivers that subclass struct drm_shadow_plane_state.
330 void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
331 struct drm_shadow_plane_state *shadow_plane_state)
333 __drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
335 EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
338 * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
341 * This function implements struct &drm_plane_funcs.reset_plane for
342 * shadow-buffered planes. It assumes the current plane state to be
343 * of type struct drm_shadow_plane and it allocates the new state of
346 void drm_gem_reset_shadow_plane(struct drm_plane *plane)
348 struct drm_shadow_plane_state *shadow_plane_state;
351 drm_gem_destroy_shadow_plane_state(plane, plane->state);
352 plane->state = NULL; /* must be set to NULL here */
355 shadow_plane_state = kzalloc(sizeof(*shadow_plane_state), GFP_KERNEL);
356 if (!shadow_plane_state)
358 __drm_gem_reset_shadow_plane(plane, shadow_plane_state);
360 EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
363 * drm_gem_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
365 * @plane_state: the plane state of type struct drm_shadow_plane_state
367 * This function implements struct &drm_plane_helper_funcs.begin_fb_access. It
368 * maps all buffer objects of the plane's framebuffer into kernel address
369 * space and stores them in struct &drm_shadow_plane_state.map. The first data
370 * bytes are available in struct &drm_shadow_plane_state.data.
372 * See drm_gem_end_shadow_fb_access() for cleanup.
375 * 0 on success, or a negative errno code otherwise.
377 int drm_gem_begin_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
379 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
380 struct drm_framebuffer *fb = plane_state->fb;
385 return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
387 EXPORT_SYMBOL(drm_gem_begin_shadow_fb_access);
390 * drm_gem_end_shadow_fb_access - releases shadow framebuffers from CPU access
392 * @plane_state: the plane state of type struct drm_shadow_plane_state
394 * This function implements struct &drm_plane_helper_funcs.end_fb_access. It
395 * undoes all effects of drm_gem_begin_shadow_fb_access() in reverse order.
397 * See drm_gem_begin_shadow_fb_access() for more information.
399 void drm_gem_end_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
401 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
402 struct drm_framebuffer *fb = plane_state->fb;
407 drm_gem_fb_vunmap(fb, shadow_plane_state->map);
409 EXPORT_SYMBOL(drm_gem_end_shadow_fb_access);
412 * drm_gem_simple_kms_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
413 * @pipe: the simple display pipe
414 * @plane_state: the plane state of type struct drm_shadow_plane_state
416 * This function implements struct drm_simple_display_funcs.begin_fb_access.
418 * See drm_gem_begin_shadow_fb_access() for details and
419 * drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
422 * 0 on success, or a negative errno code otherwise.
424 int drm_gem_simple_kms_begin_shadow_fb_access(struct drm_simple_display_pipe *pipe,
425 struct drm_plane_state *plane_state)
427 return drm_gem_begin_shadow_fb_access(&pipe->plane, plane_state);
429 EXPORT_SYMBOL(drm_gem_simple_kms_begin_shadow_fb_access);
432 * drm_gem_simple_kms_end_shadow_fb_access - releases shadow framebuffers from CPU access
433 * @pipe: the simple display pipe
434 * @plane_state: the plane state of type struct drm_shadow_plane_state
436 * This function implements struct drm_simple_display_funcs.end_fb_access.
437 * It undoes all effects of drm_gem_simple_kms_begin_shadow_fb_access() in
440 * See drm_gem_simple_kms_begin_shadow_fb_access().
442 void drm_gem_simple_kms_end_shadow_fb_access(struct drm_simple_display_pipe *pipe,
443 struct drm_plane_state *plane_state)
445 drm_gem_end_shadow_fb_access(&pipe->plane, plane_state);
447 EXPORT_SYMBOL(drm_gem_simple_kms_end_shadow_fb_access);
450 * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
451 * @pipe: the simple display pipe
453 * This function implements struct drm_simple_display_funcs.reset_plane
454 * for shadow-buffered planes.
456 void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
458 drm_gem_reset_shadow_plane(&pipe->plane);
460 EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
463 * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
464 * @pipe: the simple display pipe
466 * This function implements struct drm_simple_display_funcs.duplicate_plane_state
467 * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
468 * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
469 * and cleanup_fb helpers.
472 * A pointer to a new plane state on success, or NULL otherwise.
474 struct drm_plane_state *
475 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
477 return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
479 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
482 * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
483 * @pipe: the simple display pipe
484 * @plane_state: the plane state of type struct drm_shadow_plane_state
486 * This function implements struct drm_simple_display_funcs.destroy_plane_state
487 * for shadow-buffered planes. It expects that mappings of shadow buffers
488 * have been released already.
490 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
491 struct drm_plane_state *plane_state)
493 drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
495 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);