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.
29 * drm_gem_plane_helper_prepare_fb() can also be used directly as
30 * implementation of prepare_fb.
34 * #include <drm/drm_gem_atomic_helper.h>
36 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
38 * . prepare_fb = drm_gem_plane_helper_prepare_fb,
41 * A driver using a shadow buffer copies the content of the shadow buffers
42 * into the HW's framebuffer memory during an atomic update. This requires
43 * a mapping of the shadow buffer into kernel address space. The mappings
44 * cannot be established by commit-tail functions, such as atomic_update,
45 * as this would violate locking rules around dma_buf_vmap().
47 * The helpers for shadow-buffered planes establish and release mappings,
48 * and provide struct drm_shadow_plane_state, which stores the plane's mapping
49 * for commit-tail functions.
51 * Shadow-buffered planes can easily be enabled by using the provided macros
52 * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
53 * These macros set up the plane and plane-helper callbacks to point to the
54 * shadow-buffer helpers.
58 * #include <drm/drm_gem_atomic_helper.h>
60 * struct drm_plane_funcs driver_plane_funcs = {
62 * DRM_GEM_SHADOW_PLANE_FUNCS,
65 * struct drm_plane_helper_funcs driver_plane_helper_funcs = {
67 * DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
70 * In the driver's atomic-update function, shadow-buffer mappings are available
71 * from the plane state. Use to_drm_shadow_plane_state() to upcast from
72 * struct drm_plane_state.
76 * void driver_plane_atomic_update(struct drm_plane *plane,
77 * struct drm_plane_state *old_plane_state)
79 * struct drm_plane_state *plane_state = plane->state;
80 * struct drm_shadow_plane_state *shadow_plane_state =
81 * to_drm_shadow_plane_state(plane_state);
83 * // access shadow buffer via shadow_plane_state->map
86 * A mapping address for each of the framebuffer's buffer object is stored in
87 * struct &drm_shadow_plane_state.map. The mappings are valid while the state
90 * Drivers that use struct drm_simple_display_pipe can use
91 * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
92 * callbacks. Access to shadow-buffer mappings is similar to regular
97 * struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
99 * DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
102 * void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
103 * struct drm_crtc_state *crtc_state,
104 * struct drm_plane_state *plane_state)
106 * struct drm_shadow_plane_state *shadow_plane_state =
107 * to_drm_shadow_plane_state(plane_state);
109 * // access shadow buffer via shadow_plane_state->map
118 * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
120 * @state: Plane state the fence will be attached to
122 * This function extracts the exclusive fence from &drm_gem_object.resv and
123 * attaches it to plane state for the atomic helper to wait on. This is
124 * necessary to correctly implement implicit synchronization for any buffers
125 * shared as a struct &dma_buf. This function can be used as the
126 * &drm_plane_helper_funcs.prepare_fb callback.
128 * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
129 * GEM based framebuffer drivers which have their buffers always pinned in
132 * This function is the default implementation for GEM drivers of
133 * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
135 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane,
136 struct drm_plane_state *state)
138 struct dma_fence *fence = dma_fence_get(state->fence);
139 enum dma_resv_usage usage;
147 * Only add the kernel fences here if there is already a fence set via
148 * explicit fencing interfaces on the atomic ioctl.
150 * This way explicit fencing can be used to overrule implicit fencing,
151 * which is important to make explicit fencing use-cases work: One
152 * example is using one buffer for 2 screens with different refresh
153 * rates. Implicit fencing will clamp rendering to the refresh rate of
154 * the slower screen, whereas explicit fence allows 2 independent
155 * render and display loops on a single buffer. If a driver allows
156 * obeys both implicit and explicit fences for plane updates, then it
157 * will break all the benefits of explicit fencing.
159 usage = fence ? DMA_RESV_USAGE_KERNEL : DMA_RESV_USAGE_WRITE;
161 for (i = 0; i < state->fb->format->num_planes; ++i) {
162 struct drm_gem_object *obj = drm_gem_fb_get_obj(state->fb, i);
163 struct dma_fence *new;
170 ret = dma_resv_get_singleton(obj->resv, usage, &new);
175 struct dma_fence_chain *chain = dma_fence_chain_alloc();
182 dma_fence_chain_init(chain, fence, new, 1);
183 fence = &chain->base;
190 dma_fence_put(state->fence);
191 state->fence = fence;
195 dma_fence_put(fence);
198 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
201 * Shadow-buffered Planes
205 * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
207 * @new_shadow_plane_state: the new shadow-buffered plane state
209 * This function duplicates shadow-buffered plane state. This is helpful for drivers
210 * that subclass struct drm_shadow_plane_state.
212 * The function does not duplicate existing mappings of the shadow buffers.
213 * Mappings are maintained during the atomic commit by the plane's prepare_fb
214 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
215 * for corresponding helpers.
218 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
219 struct drm_shadow_plane_state *new_shadow_plane_state)
221 __drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
223 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
226 * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
229 * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
230 * shadow-buffered planes. It assumes the existing state to be of type
231 * struct drm_shadow_plane_state and it allocates the new state to be of this
234 * The function does not duplicate existing mappings of the shadow buffers.
235 * Mappings are maintained during the atomic commit by the plane's prepare_fb
236 * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
237 * for corresponding helpers.
240 * A pointer to a new plane state on success, or NULL otherwise.
242 struct drm_plane_state *
243 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
245 struct drm_plane_state *plane_state = plane->state;
246 struct drm_shadow_plane_state *new_shadow_plane_state;
251 new_shadow_plane_state = kzalloc(sizeof(*new_shadow_plane_state), GFP_KERNEL);
252 if (!new_shadow_plane_state)
254 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
256 return &new_shadow_plane_state->base;
258 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
261 * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
262 * @shadow_plane_state: the shadow-buffered plane state
264 * This function cleans up shadow-buffered plane state. Helpful for drivers that
265 * subclass struct drm_shadow_plane_state.
267 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
269 __drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
271 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
274 * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
276 * @plane_state: the plane state of type struct drm_shadow_plane_state
278 * This function implements struct &drm_plane_funcs.atomic_destroy_state
279 * for shadow-buffered planes. It expects that mappings of shadow buffers
280 * have been released already.
282 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
283 struct drm_plane_state *plane_state)
285 struct drm_shadow_plane_state *shadow_plane_state =
286 to_drm_shadow_plane_state(plane_state);
288 __drm_gem_destroy_shadow_plane_state(shadow_plane_state);
289 kfree(shadow_plane_state);
291 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
294 * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
296 * @shadow_plane_state: the shadow-buffered plane state
298 * This function resets state for shadow-buffered planes. Helpful
299 * for drivers that subclass struct drm_shadow_plane_state.
301 void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
302 struct drm_shadow_plane_state *shadow_plane_state)
304 __drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
306 EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
309 * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
312 * This function implements struct &drm_plane_funcs.reset_plane for
313 * shadow-buffered planes. It assumes the current plane state to be
314 * of type struct drm_shadow_plane and it allocates the new state of
317 void drm_gem_reset_shadow_plane(struct drm_plane *plane)
319 struct drm_shadow_plane_state *shadow_plane_state;
322 drm_gem_destroy_shadow_plane_state(plane, plane->state);
323 plane->state = NULL; /* must be set to NULL here */
326 shadow_plane_state = kzalloc(sizeof(*shadow_plane_state), GFP_KERNEL);
327 if (!shadow_plane_state)
329 __drm_gem_reset_shadow_plane(plane, shadow_plane_state);
331 EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
334 * drm_gem_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
336 * @plane_state: the plane state of type struct drm_shadow_plane_state
338 * This function implements struct &drm_plane_helper_funcs.begin_fb_access. It
339 * maps all buffer objects of the plane's framebuffer into kernel address
340 * space and stores them in struct &drm_shadow_plane_state.map. The first data
341 * bytes are available in struct &drm_shadow_plane_state.data.
343 * See drm_gem_end_shadow_fb_access() for cleanup.
346 * 0 on success, or a negative errno code otherwise.
348 int drm_gem_begin_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
350 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
351 struct drm_framebuffer *fb = plane_state->fb;
356 return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
358 EXPORT_SYMBOL(drm_gem_begin_shadow_fb_access);
361 * drm_gem_end_shadow_fb_access - releases shadow framebuffers from CPU access
363 * @plane_state: the plane state of type struct drm_shadow_plane_state
365 * This function implements struct &drm_plane_helper_funcs.end_fb_access. It
366 * undoes all effects of drm_gem_begin_shadow_fb_access() in reverse order.
368 * See drm_gem_begin_shadow_fb_access() for more information.
370 void drm_gem_end_shadow_fb_access(struct drm_plane *plane, struct drm_plane_state *plane_state)
372 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
373 struct drm_framebuffer *fb = plane_state->fb;
378 drm_gem_fb_vunmap(fb, shadow_plane_state->map);
380 EXPORT_SYMBOL(drm_gem_end_shadow_fb_access);
383 * drm_gem_simple_kms_begin_shadow_fb_access - prepares shadow framebuffers for CPU access
384 * @pipe: the simple display pipe
385 * @plane_state: the plane state of type struct drm_shadow_plane_state
387 * This function implements struct drm_simple_display_funcs.begin_fb_access.
389 * See drm_gem_begin_shadow_fb_access() for details and
390 * drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
393 * 0 on success, or a negative errno code otherwise.
395 int drm_gem_simple_kms_begin_shadow_fb_access(struct drm_simple_display_pipe *pipe,
396 struct drm_plane_state *plane_state)
398 return drm_gem_begin_shadow_fb_access(&pipe->plane, plane_state);
400 EXPORT_SYMBOL(drm_gem_simple_kms_begin_shadow_fb_access);
403 * drm_gem_simple_kms_end_shadow_fb_access - releases shadow framebuffers from CPU access
404 * @pipe: the simple display pipe
405 * @plane_state: the plane state of type struct drm_shadow_plane_state
407 * This function implements struct drm_simple_display_funcs.end_fb_access.
408 * It undoes all effects of drm_gem_simple_kms_begin_shadow_fb_access() in
411 * See drm_gem_simple_kms_begin_shadow_fb_access().
413 void drm_gem_simple_kms_end_shadow_fb_access(struct drm_simple_display_pipe *pipe,
414 struct drm_plane_state *plane_state)
416 drm_gem_end_shadow_fb_access(&pipe->plane, plane_state);
418 EXPORT_SYMBOL(drm_gem_simple_kms_end_shadow_fb_access);
421 * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
422 * @pipe: the simple display pipe
424 * This function implements struct drm_simple_display_funcs.reset_plane
425 * for shadow-buffered planes.
427 void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
429 drm_gem_reset_shadow_plane(&pipe->plane);
431 EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
434 * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
435 * @pipe: the simple display pipe
437 * This function implements struct drm_simple_display_funcs.duplicate_plane_state
438 * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
439 * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
440 * and cleanup_fb helpers.
443 * A pointer to a new plane state on success, or NULL otherwise.
445 struct drm_plane_state *
446 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
448 return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
450 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
453 * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
454 * @pipe: the simple display pipe
455 * @plane_state: the plane state of type struct drm_shadow_plane_state
457 * This function implements struct drm_simple_display_funcs.destroy_plane_state
458 * for shadow-buffered planes. It expects that mappings of shadow buffers
459 * have been released already.
461 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
462 struct drm_plane_state *plane_state)
464 drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
466 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);