1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/dma-resv.h>
4 
5 #include <drm/drm_atomic_state_helper.h>
6 #include <drm/drm_atomic_uapi.h>
7 #include <drm/drm_gem.h>
8 #include <drm/drm_gem_atomic_helper.h>
9 #include <drm/drm_gem_framebuffer_helper.h>
10 #include <drm/drm_simple_kms_helper.h>
11 
12 #include "drm_internal.h"
13 
14 /**
15  * DOC: overview
16  *
17  * The GEM atomic helpers library implements generic atomic-commit
18  * functions for drivers that use GEM objects. Currently, it provides
19  * synchronization helpers, and plane state and framebuffer BO mappings
20  * for planes with shadow buffers.
21  *
22  * Before scanout, a plane's framebuffer needs to be synchronized with
23  * possible writers that draw into the framebuffer. All drivers should
24  * call drm_gem_plane_helper_prepare_fb() from their implementation of
25  * struct &drm_plane_helper.prepare_fb . It sets the plane's fence from
26  * the framebuffer so that the DRM core can synchronize access automatically.
27  *
28  * drm_gem_plane_helper_prepare_fb() can also be used directly as
29  * implementation of prepare_fb. For drivers based on
30  * struct drm_simple_display_pipe, drm_gem_simple_display_pipe_prepare_fb()
31  * provides equivalent functionality.
32  *
33  * .. code-block:: c
34  *
35  *	#include <drm/drm_gem_atomic_helper.h>
36  *
37  *	struct drm_plane_helper_funcs driver_plane_helper_funcs = {
38  *		...,
39  *		. prepare_fb = drm_gem_plane_helper_prepare_fb,
40  *	};
41  *
42  *	struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
43  *		...,
44  *		. prepare_fb = drm_gem_simple_display_pipe_prepare_fb,
45  *	};
46  *
47  * A driver using a shadow buffer copies the content of the shadow buffers
48  * into the HW's framebuffer memory during an atomic update. This requires
49  * a mapping of the shadow buffer into kernel address space. The mappings
50  * cannot be established by commit-tail functions, such as atomic_update,
51  * as this would violate locking rules around dma_buf_vmap().
52  *
53  * The helpers for shadow-buffered planes establish and release mappings,
54  * and provide struct drm_shadow_plane_state, which stores the plane's mapping
55  * for commit-tail functions.
56  *
57  * Shadow-buffered planes can easily be enabled by using the provided macros
58  * %DRM_GEM_SHADOW_PLANE_FUNCS and %DRM_GEM_SHADOW_PLANE_HELPER_FUNCS.
59  * These macros set up the plane and plane-helper callbacks to point to the
60  * shadow-buffer helpers.
61  *
62  * .. code-block:: c
63  *
64  *	#include <drm/drm_gem_atomic_helper.h>
65  *
66  *	struct drm_plane_funcs driver_plane_funcs = {
67  *		...,
68  *		DRM_GEM_SHADOW_PLANE_FUNCS,
69  *	};
70  *
71  *	struct drm_plane_helper_funcs driver_plane_helper_funcs = {
72  *		...,
73  *		DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
74  *	};
75  *
76  * In the driver's atomic-update function, shadow-buffer mappings are available
77  * from the plane state. Use to_drm_shadow_plane_state() to upcast from
78  * struct drm_plane_state.
79  *
80  * .. code-block:: c
81  *
82  *	void driver_plane_atomic_update(struct drm_plane *plane,
83  *					struct drm_plane_state *old_plane_state)
84  *	{
85  *		struct drm_plane_state *plane_state = plane->state;
86  *		struct drm_shadow_plane_state *shadow_plane_state =
87  *			to_drm_shadow_plane_state(plane_state);
88  *
89  *		// access shadow buffer via shadow_plane_state->map
90  *	}
91  *
92  * A mapping address for each of the framebuffer's buffer object is stored in
93  * struct &drm_shadow_plane_state.map. The mappings are valid while the state
94  * is being used.
95  *
96  * Drivers that use struct drm_simple_display_pipe can use
97  * %DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS to initialize the rsp
98  * callbacks. Access to shadow-buffer mappings is similar to regular
99  * atomic_update.
100  *
101  * .. code-block:: c
102  *
103  *	struct drm_simple_display_pipe_funcs driver_pipe_funcs = {
104  *		...,
105  *		DRM_GEM_SIMPLE_DISPLAY_PIPE_SHADOW_PLANE_FUNCS,
106  *	};
107  *
108  *	void driver_pipe_enable(struct drm_simple_display_pipe *pipe,
109  *				struct drm_crtc_state *crtc_state,
110  *				struct drm_plane_state *plane_state)
111  *	{
112  *		struct drm_shadow_plane_state *shadow_plane_state =
113  *			to_drm_shadow_plane_state(plane_state);
114  *
115  *		// access shadow buffer via shadow_plane_state->map
116  *	}
117  */
118 
119 /*
120  * Plane Helpers
121  */
122 
123 /**
124  * drm_gem_plane_helper_prepare_fb() - Prepare a GEM backed framebuffer
125  * @plane: Plane
126  * @state: Plane state the fence will be attached to
127  *
128  * This function extracts the exclusive fence from &drm_gem_object.resv and
129  * attaches it to plane state for the atomic helper to wait on. This is
130  * necessary to correctly implement implicit synchronization for any buffers
131  * shared as a struct &dma_buf. This function can be used as the
132  * &drm_plane_helper_funcs.prepare_fb callback.
133  *
134  * There is no need for &drm_plane_helper_funcs.cleanup_fb hook for simple
135  * GEM based framebuffer drivers which have their buffers always pinned in
136  * memory.
137  *
138  * This function is the default implementation for GEM drivers of
139  * &drm_plane_helper_funcs.prepare_fb if no callback is provided.
140  *
141  * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
142  * explicit fencing in atomic modeset updates.
143  */
144 int drm_gem_plane_helper_prepare_fb(struct drm_plane *plane, struct drm_plane_state *state)
145 {
146 	struct dma_resv_iter cursor;
147 	struct drm_gem_object *obj;
148 	struct dma_fence *fence;
149 
150 	if (!state->fb)
151 		return 0;
152 
153 	obj = drm_gem_fb_get_obj(state->fb, 0);
154 	dma_resv_iter_begin(&cursor, obj->resv, false);
155 	dma_resv_for_each_fence_unlocked(&cursor, fence) {
156 		/* TODO: Currently there should be only one write fence, so this
157 		 * here works fine. But drm_atomic_set_fence_for_plane() should
158 		 * be changed to be able to handle more fences in general for
159 		 * multiple BOs per fb anyway. */
160 		dma_fence_get(fence);
161 		break;
162 	}
163 	dma_resv_iter_end(&cursor);
164 
165 	drm_atomic_set_fence_for_plane(state, fence);
166 	return 0;
167 }
168 EXPORT_SYMBOL_GPL(drm_gem_plane_helper_prepare_fb);
169 
170 /**
171  * drm_gem_simple_display_pipe_prepare_fb - prepare_fb helper for &drm_simple_display_pipe
172  * @pipe: Simple display pipe
173  * @plane_state: Plane state
174  *
175  * This function uses drm_gem_plane_helper_prepare_fb() to extract the exclusive fence
176  * from &drm_gem_object.resv and attaches it to plane state for the atomic
177  * helper to wait on. This is necessary to correctly implement implicit
178  * synchronization for any buffers shared as a struct &dma_buf. Drivers can use
179  * this as their &drm_simple_display_pipe_funcs.prepare_fb callback.
180  *
181  * See drm_atomic_set_fence_for_plane() for a discussion of implicit and
182  * explicit fencing in atomic modeset updates.
183  */
184 int drm_gem_simple_display_pipe_prepare_fb(struct drm_simple_display_pipe *pipe,
185 					   struct drm_plane_state *plane_state)
186 {
187 	return drm_gem_plane_helper_prepare_fb(&pipe->plane, plane_state);
188 }
189 EXPORT_SYMBOL(drm_gem_simple_display_pipe_prepare_fb);
190 
191 /*
192  * Shadow-buffered Planes
193  */
194 
195 /**
196  * __drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
197  * @plane: the plane
198  * @new_shadow_plane_state: the new shadow-buffered plane state
199  *
200  * This function duplicates shadow-buffered plane state. This is helpful for drivers
201  * that subclass struct drm_shadow_plane_state.
202  *
203  * The function does not duplicate existing mappings of the shadow buffers.
204  * Mappings are maintained during the atomic commit by the plane's prepare_fb
205  * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
206  * for corresponding helpers.
207  */
208 void
209 __drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane,
210 				       struct drm_shadow_plane_state *new_shadow_plane_state)
211 {
212 	__drm_atomic_helper_plane_duplicate_state(plane, &new_shadow_plane_state->base);
213 }
214 EXPORT_SYMBOL(__drm_gem_duplicate_shadow_plane_state);
215 
216 /**
217  * drm_gem_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
218  * @plane: the plane
219  *
220  * This function implements struct &drm_plane_funcs.atomic_duplicate_state for
221  * shadow-buffered planes. It assumes the existing state to be of type
222  * struct drm_shadow_plane_state and it allocates the new state to be of this
223  * type.
224  *
225  * The function does not duplicate existing mappings of the shadow buffers.
226  * Mappings are maintained during the atomic commit by the plane's prepare_fb
227  * and cleanup_fb helpers. See drm_gem_prepare_shadow_fb() and drm_gem_cleanup_shadow_fb()
228  * for corresponding helpers.
229  *
230  * Returns:
231  * A pointer to a new plane state on success, or NULL otherwise.
232  */
233 struct drm_plane_state *
234 drm_gem_duplicate_shadow_plane_state(struct drm_plane *plane)
235 {
236 	struct drm_plane_state *plane_state = plane->state;
237 	struct drm_shadow_plane_state *new_shadow_plane_state;
238 
239 	if (!plane_state)
240 		return NULL;
241 
242 	new_shadow_plane_state = kzalloc(sizeof(*new_shadow_plane_state), GFP_KERNEL);
243 	if (!new_shadow_plane_state)
244 		return NULL;
245 	__drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
246 
247 	return &new_shadow_plane_state->base;
248 }
249 EXPORT_SYMBOL(drm_gem_duplicate_shadow_plane_state);
250 
251 /**
252  * __drm_gem_destroy_shadow_plane_state - cleans up shadow-buffered plane state
253  * @shadow_plane_state: the shadow-buffered plane state
254  *
255  * This function cleans up shadow-buffered plane state. Helpful for drivers that
256  * subclass struct drm_shadow_plane_state.
257  */
258 void __drm_gem_destroy_shadow_plane_state(struct drm_shadow_plane_state *shadow_plane_state)
259 {
260 	__drm_atomic_helper_plane_destroy_state(&shadow_plane_state->base);
261 }
262 EXPORT_SYMBOL(__drm_gem_destroy_shadow_plane_state);
263 
264 /**
265  * drm_gem_destroy_shadow_plane_state - deletes shadow-buffered plane state
266  * @plane: the plane
267  * @plane_state: the plane state of type struct drm_shadow_plane_state
268  *
269  * This function implements struct &drm_plane_funcs.atomic_destroy_state
270  * for shadow-buffered planes. It expects that mappings of shadow buffers
271  * have been released already.
272  */
273 void drm_gem_destroy_shadow_plane_state(struct drm_plane *plane,
274 					struct drm_plane_state *plane_state)
275 {
276 	struct drm_shadow_plane_state *shadow_plane_state =
277 		to_drm_shadow_plane_state(plane_state);
278 
279 	__drm_gem_destroy_shadow_plane_state(shadow_plane_state);
280 	kfree(shadow_plane_state);
281 }
282 EXPORT_SYMBOL(drm_gem_destroy_shadow_plane_state);
283 
284 /**
285  * __drm_gem_reset_shadow_plane - resets a shadow-buffered plane
286  * @plane: the plane
287  * @shadow_plane_state: the shadow-buffered plane state
288  *
289  * This function resets state for shadow-buffered planes. Helpful
290  * for drivers that subclass struct drm_shadow_plane_state.
291  */
292 void __drm_gem_reset_shadow_plane(struct drm_plane *plane,
293 				  struct drm_shadow_plane_state *shadow_plane_state)
294 {
295 	__drm_atomic_helper_plane_reset(plane, &shadow_plane_state->base);
296 }
297 EXPORT_SYMBOL(__drm_gem_reset_shadow_plane);
298 
299 /**
300  * drm_gem_reset_shadow_plane - resets a shadow-buffered plane
301  * @plane: the plane
302  *
303  * This function implements struct &drm_plane_funcs.reset_plane for
304  * shadow-buffered planes. It assumes the current plane state to be
305  * of type struct drm_shadow_plane and it allocates the new state of
306  * this type.
307  */
308 void drm_gem_reset_shadow_plane(struct drm_plane *plane)
309 {
310 	struct drm_shadow_plane_state *shadow_plane_state;
311 
312 	if (plane->state) {
313 		drm_gem_destroy_shadow_plane_state(plane, plane->state);
314 		plane->state = NULL; /* must be set to NULL here */
315 	}
316 
317 	shadow_plane_state = kzalloc(sizeof(*shadow_plane_state), GFP_KERNEL);
318 	if (!shadow_plane_state)
319 		return;
320 	__drm_gem_reset_shadow_plane(plane, shadow_plane_state);
321 }
322 EXPORT_SYMBOL(drm_gem_reset_shadow_plane);
323 
324 /**
325  * drm_gem_prepare_shadow_fb - prepares shadow framebuffers
326  * @plane: the plane
327  * @plane_state: the plane state of type struct drm_shadow_plane_state
328  *
329  * This function implements struct &drm_plane_helper_funcs.prepare_fb. It
330  * maps all buffer objects of the plane's framebuffer into kernel address
331  * space and stores them in &struct drm_shadow_plane_state.map. The
332  * framebuffer will be synchronized as part of the atomic commit.
333  *
334  * See drm_gem_cleanup_shadow_fb() for cleanup.
335  *
336  * Returns:
337  * 0 on success, or a negative errno code otherwise.
338  */
339 int drm_gem_prepare_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
340 {
341 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
342 	struct drm_framebuffer *fb = plane_state->fb;
343 	int ret;
344 
345 	if (!fb)
346 		return 0;
347 
348 	ret = drm_gem_plane_helper_prepare_fb(plane, plane_state);
349 	if (ret)
350 		return ret;
351 
352 	return drm_gem_fb_vmap(fb, shadow_plane_state->map, shadow_plane_state->data);
353 }
354 EXPORT_SYMBOL(drm_gem_prepare_shadow_fb);
355 
356 /**
357  * drm_gem_cleanup_shadow_fb - releases shadow framebuffers
358  * @plane: the plane
359  * @plane_state: the plane state of type struct drm_shadow_plane_state
360  *
361  * This function implements struct &drm_plane_helper_funcs.cleanup_fb.
362  * This function unmaps all buffer objects of the plane's framebuffer.
363  *
364  * See drm_gem_prepare_shadow_fb() for more information.
365  */
366 void drm_gem_cleanup_shadow_fb(struct drm_plane *plane, struct drm_plane_state *plane_state)
367 {
368 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
369 	struct drm_framebuffer *fb = plane_state->fb;
370 
371 	if (!fb)
372 		return;
373 
374 	drm_gem_fb_vunmap(fb, shadow_plane_state->map);
375 }
376 EXPORT_SYMBOL(drm_gem_cleanup_shadow_fb);
377 
378 /**
379  * drm_gem_simple_kms_prepare_shadow_fb - prepares shadow framebuffers
380  * @pipe: the simple display pipe
381  * @plane_state: the plane state of type struct drm_shadow_plane_state
382  *
383  * This function implements struct drm_simple_display_funcs.prepare_fb. It
384  * maps all buffer objects of the plane's framebuffer into kernel address
385  * space and stores them in struct drm_shadow_plane_state.map. The
386  * framebuffer will be synchronized as part of the atomic commit.
387  *
388  * See drm_gem_simple_kms_cleanup_shadow_fb() for cleanup.
389  *
390  * Returns:
391  * 0 on success, or a negative errno code otherwise.
392  */
393 int drm_gem_simple_kms_prepare_shadow_fb(struct drm_simple_display_pipe *pipe,
394 					 struct drm_plane_state *plane_state)
395 {
396 	return drm_gem_prepare_shadow_fb(&pipe->plane, plane_state);
397 }
398 EXPORT_SYMBOL(drm_gem_simple_kms_prepare_shadow_fb);
399 
400 /**
401  * drm_gem_simple_kms_cleanup_shadow_fb - releases shadow framebuffers
402  * @pipe: the simple display pipe
403  * @plane_state: the plane state of type struct drm_shadow_plane_state
404  *
405  * This function implements struct drm_simple_display_funcs.cleanup_fb.
406  * This function unmaps all buffer objects of the plane's framebuffer.
407  *
408  * See drm_gem_simple_kms_prepare_shadow_fb().
409  */
410 void drm_gem_simple_kms_cleanup_shadow_fb(struct drm_simple_display_pipe *pipe,
411 					  struct drm_plane_state *plane_state)
412 {
413 	drm_gem_cleanup_shadow_fb(&pipe->plane, plane_state);
414 }
415 EXPORT_SYMBOL(drm_gem_simple_kms_cleanup_shadow_fb);
416 
417 /**
418  * drm_gem_simple_kms_reset_shadow_plane - resets a shadow-buffered plane
419  * @pipe: the simple display pipe
420  *
421  * This function implements struct drm_simple_display_funcs.reset_plane
422  * for shadow-buffered planes.
423  */
424 void drm_gem_simple_kms_reset_shadow_plane(struct drm_simple_display_pipe *pipe)
425 {
426 	drm_gem_reset_shadow_plane(&pipe->plane);
427 }
428 EXPORT_SYMBOL(drm_gem_simple_kms_reset_shadow_plane);
429 
430 /**
431  * drm_gem_simple_kms_duplicate_shadow_plane_state - duplicates shadow-buffered plane state
432  * @pipe: the simple display pipe
433  *
434  * This function implements struct drm_simple_display_funcs.duplicate_plane_state
435  * for shadow-buffered planes. It does not duplicate existing mappings of the shadow
436  * buffers. Mappings are maintained during the atomic commit by the plane's prepare_fb
437  * and cleanup_fb helpers.
438  *
439  * Returns:
440  * A pointer to a new plane state on success, or NULL otherwise.
441  */
442 struct drm_plane_state *
443 drm_gem_simple_kms_duplicate_shadow_plane_state(struct drm_simple_display_pipe *pipe)
444 {
445 	return drm_gem_duplicate_shadow_plane_state(&pipe->plane);
446 }
447 EXPORT_SYMBOL(drm_gem_simple_kms_duplicate_shadow_plane_state);
448 
449 /**
450  * drm_gem_simple_kms_destroy_shadow_plane_state - resets shadow-buffered plane state
451  * @pipe: the simple display pipe
452  * @plane_state: the plane state of type struct drm_shadow_plane_state
453  *
454  * This function implements struct drm_simple_display_funcs.destroy_plane_state
455  * for shadow-buffered planes. It expects that mappings of shadow buffers
456  * have been released already.
457  */
458 void drm_gem_simple_kms_destroy_shadow_plane_state(struct drm_simple_display_pipe *pipe,
459 						   struct drm_plane_state *plane_state)
460 {
461 	drm_gem_destroy_shadow_plane_state(&pipe->plane, plane_state);
462 }
463 EXPORT_SYMBOL(drm_gem_simple_kms_destroy_shadow_plane_state);
464