xref: /dragonfly/sys/dev/drm/drm_plane_helper.c (revision cc87404e)
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
4  * DRM universal plane helper functions
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <linux/list.h>
27 #include <drm/drmP.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_rect.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_atomic_helper.h>
33 
34 #define SUBPIXEL_MASK 0xffff
35 
36 /**
37  * DOC: overview
38  *
39  * This helper library has two parts. The first part has support to implement
40  * primary plane support on top of the normal CRTC configuration interface.
41  * Since the legacy ->set_config interface ties the primary plane together with
42  * the CRTC state this does not allow userspace to disable the primary plane
43  * itself.  To avoid too much duplicated code use
44  * drm_plane_helper_check_update() which can be used to enforce the same
45  * restrictions as primary planes had thus. The default primary plane only
46  * expose XRBG8888 and ARGB8888 as valid pixel formats for the attached
47  * framebuffer.
48  *
49  * Drivers are highly recommended to implement proper support for primary
50  * planes, and newly merged drivers must not rely upon these transitional
51  * helpers.
52  *
53  * The second part also implements transitional helpers which allow drivers to
54  * gradually switch to the atomic helper infrastructure for plane updates. Once
55  * that switch is complete drivers shouldn't use these any longer, instead using
56  * the proper legacy implementations for update and disable plane hooks provided
57  * by the atomic helpers.
58  *
59  * Again drivers are strongly urged to switch to the new interfaces.
60  */
61 
62 /*
63  * This is the minimal list of formats that seem to be safe for modeset use
64  * with all current DRM drivers.  Most hardware can actually support more
65  * formats than this and drivers may specify a more accurate list when
66  * creating the primary plane.  However drivers that still call
67  * drm_plane_init() will use this minimal format list as the default.
68  */
69 static const uint32_t safe_modeset_formats[] = {
70 	DRM_FORMAT_XRGB8888,
71 	DRM_FORMAT_ARGB8888,
72 };
73 
74 /*
75  * Returns the connectors currently associated with a CRTC.  This function
76  * should be called twice:  once with a NULL connector list to retrieve
77  * the list size, and once with the properly allocated list to be filled in.
78  */
79 static int get_connectors_for_crtc(struct drm_crtc *crtc,
80 				   struct drm_connector **connector_list,
81 				   int num_connectors)
82 {
83 	struct drm_device *dev = crtc->dev;
84 	struct drm_connector *connector;
85 	int count = 0;
86 
87 	/*
88 	 * Note: Once we change the plane hooks to more fine-grained locking we
89 	 * need to grab the connection_mutex here to be able to make these
90 	 * checks.
91 	 */
92 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
93 
94 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
95 		if (connector->encoder && connector->encoder->crtc == crtc) {
96 			if (connector_list != NULL && count < num_connectors)
97 				*(connector_list++) = connector;
98 
99 			count++;
100 		}
101 
102 	return count;
103 }
104 
105 /**
106  * drm_plane_helper_check_update() - Check plane update for validity
107  * @plane: plane object to update
108  * @crtc: owning CRTC of owning plane
109  * @fb: framebuffer to flip onto plane
110  * @src: source coordinates in 16.16 fixed point
111  * @dest: integer destination coordinates
112  * @clip: integer clipping coordinates
113  * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
114  * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
115  * @can_position: is it legal to position the plane such that it
116  *                doesn't cover the entire crtc?  This will generally
117  *                only be false for primary planes.
118  * @can_update_disabled: can the plane be updated while the crtc
119  *                       is disabled?
120  * @visible: output parameter indicating whether plane is still visible after
121  *           clipping
122  *
123  * Checks that a desired plane update is valid.  Drivers that provide
124  * their own plane handling rather than helper-provided implementations may
125  * still wish to call this function to avoid duplication of error checking
126  * code.
127  *
128  * RETURNS:
129  * Zero if update appears valid, error code on failure
130  */
131 int drm_plane_helper_check_update(struct drm_plane *plane,
132 				    struct drm_crtc *crtc,
133 				    struct drm_framebuffer *fb,
134 				    struct drm_rect *src,
135 				    struct drm_rect *dest,
136 				    const struct drm_rect *clip,
137 				    int min_scale,
138 				    int max_scale,
139 				    bool can_position,
140 				    bool can_update_disabled,
141 				    bool *visible)
142 {
143 	int hscale, vscale;
144 
145 	if (!fb) {
146 		*visible = false;
147 		return 0;
148 	}
149 
150 	/* crtc should only be NULL when disabling (i.e., !fb) */
151 	if (WARN_ON(!crtc)) {
152 		*visible = false;
153 		return 0;
154 	}
155 
156 	if (!crtc->enabled && !can_update_disabled) {
157 		DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
158 		return -EINVAL;
159 	}
160 
161 	/* Check scaling */
162 	hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
163 	vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
164 	if (hscale < 0 || vscale < 0) {
165 		DRM_DEBUG_KMS("Invalid scaling of plane\n");
166 		return -ERANGE;
167 	}
168 
169 	*visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
170 	if (!*visible)
171 		/*
172 		 * Plane isn't visible; some drivers can handle this
173 		 * so we just return success here.  Drivers that can't
174 		 * (including those that use the primary plane helper's
175 		 * update function) will return an error from their
176 		 * update_plane handler.
177 		 */
178 		return 0;
179 
180 	if (!can_position && !drm_rect_equals(dest, clip)) {
181 		DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
182 		return -EINVAL;
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(drm_plane_helper_check_update);
188 
189 /**
190  * drm_primary_helper_update() - Helper for primary plane update
191  * @plane: plane object to update
192  * @crtc: owning CRTC of owning plane
193  * @fb: framebuffer to flip onto plane
194  * @crtc_x: x offset of primary plane on crtc
195  * @crtc_y: y offset of primary plane on crtc
196  * @crtc_w: width of primary plane rectangle on crtc
197  * @crtc_h: height of primary plane rectangle on crtc
198  * @src_x: x offset of @fb for panning
199  * @src_y: y offset of @fb for panning
200  * @src_w: width of source rectangle in @fb
201  * @src_h: height of source rectangle in @fb
202  *
203  * Provides a default plane update handler for primary planes.  This is handler
204  * is called in response to a userspace SetPlane operation on the plane with a
205  * non-NULL framebuffer.  We call the driver's modeset handler to update the
206  * framebuffer.
207  *
208  * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
209  * return an error.
210  *
211  * Note that we make some assumptions about hardware limitations that may not be
212  * true for all hardware --
213  *   1) Primary plane cannot be repositioned.
214  *   2) Primary plane cannot be scaled.
215  *   3) Primary plane must cover the entire CRTC.
216  *   4) Subpixel positioning is not supported.
217  * Drivers for hardware that don't have these restrictions can provide their
218  * own implementation rather than using this helper.
219  *
220  * RETURNS:
221  * Zero on success, error code on failure
222  */
223 int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
224 			      struct drm_framebuffer *fb,
225 			      int crtc_x, int crtc_y,
226 			      unsigned int crtc_w, unsigned int crtc_h,
227 			      uint32_t src_x, uint32_t src_y,
228 			      uint32_t src_w, uint32_t src_h)
229 {
230 	struct drm_mode_set set = {
231 		.crtc = crtc,
232 		.fb = fb,
233 		.mode = &crtc->mode,
234 		.x = src_x >> 16,
235 		.y = src_y >> 16,
236 	};
237 	struct drm_rect src = {
238 		.x1 = src_x,
239 		.y1 = src_y,
240 		.x2 = src_x + src_w,
241 		.y2 = src_y + src_h,
242 	};
243 	struct drm_rect dest = {
244 		.x1 = crtc_x,
245 		.y1 = crtc_y,
246 		.x2 = crtc_x + crtc_w,
247 		.y2 = crtc_y + crtc_h,
248 	};
249 	const struct drm_rect clip = {
250 		.x2 = crtc->mode.hdisplay,
251 		.y2 = crtc->mode.vdisplay,
252 	};
253 	struct drm_connector **connector_list;
254 	int num_connectors, ret;
255 	bool visible;
256 
257 	ret = drm_plane_helper_check_update(plane, crtc, fb,
258 					    &src, &dest, &clip,
259 					    DRM_PLANE_HELPER_NO_SCALING,
260 					    DRM_PLANE_HELPER_NO_SCALING,
261 					    false, false, &visible);
262 	if (ret)
263 		return ret;
264 
265 	if (!visible)
266 		/*
267 		 * Primary plane isn't visible.  Note that unless a driver
268 		 * provides their own disable function, this will just
269 		 * wind up returning -EINVAL to userspace.
270 		 */
271 		return plane->funcs->disable_plane(plane);
272 
273 	/* Find current connectors for CRTC */
274 	num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
275 	BUG_ON(num_connectors == 0);
276 	connector_list = kzalloc(num_connectors * sizeof(*connector_list),
277 				 GFP_KERNEL);
278 	if (!connector_list)
279 		return -ENOMEM;
280 	get_connectors_for_crtc(crtc, connector_list, num_connectors);
281 
282 	set.connectors = connector_list;
283 	set.num_connectors = num_connectors;
284 
285 	/*
286 	 * We call set_config() directly here rather than using
287 	 * drm_mode_set_config_internal.  We're reprogramming the same
288 	 * connectors that were already in use, so we shouldn't need the extra
289 	 * cross-CRTC fb refcounting to accomodate stealing connectors.
290 	 * drm_mode_setplane() already handles the basic refcounting for the
291 	 * framebuffers involved in this operation.
292 	 */
293 	ret = crtc->funcs->set_config(&set);
294 
295 	kfree(connector_list);
296 	return ret;
297 }
298 EXPORT_SYMBOL(drm_primary_helper_update);
299 
300 /**
301  * drm_primary_helper_disable() - Helper for primary plane disable
302  * @plane: plane to disable
303  *
304  * Provides a default plane disable handler for primary planes.  This is handler
305  * is called in response to a userspace SetPlane operation on the plane with a
306  * NULL framebuffer parameter.  It unconditionally fails the disable call with
307  * -EINVAL the only way to disable the primary plane without driver support is
308  * to disable the entier CRTC. Which does not match the plane ->disable hook.
309  *
310  * Note that some hardware may be able to disable the primary plane without
311  * disabling the whole CRTC.  Drivers for such hardware should provide their
312  * own disable handler that disables just the primary plane (and they'll likely
313  * need to provide their own update handler as well to properly re-enable a
314  * disabled primary plane).
315  *
316  * RETURNS:
317  * Unconditionally returns -EINVAL.
318  */
319 int drm_primary_helper_disable(struct drm_plane *plane)
320 {
321 	return -EINVAL;
322 }
323 EXPORT_SYMBOL(drm_primary_helper_disable);
324 
325 /**
326  * drm_primary_helper_destroy() - Helper for primary plane destruction
327  * @plane: plane to destroy
328  *
329  * Provides a default plane destroy handler for primary planes.  This handler
330  * is called during CRTC destruction.  We disable the primary plane, remove
331  * it from the DRM plane list, and deallocate the plane structure.
332  */
333 void drm_primary_helper_destroy(struct drm_plane *plane)
334 {
335 	drm_plane_cleanup(plane);
336 	kfree(plane);
337 }
338 EXPORT_SYMBOL(drm_primary_helper_destroy);
339 
340 const struct drm_plane_funcs drm_primary_helper_funcs = {
341 	.update_plane = drm_primary_helper_update,
342 	.disable_plane = drm_primary_helper_disable,
343 	.destroy = drm_primary_helper_destroy,
344 };
345 EXPORT_SYMBOL(drm_primary_helper_funcs);
346 
347 static struct drm_plane *create_primary_plane(struct drm_device *dev)
348 {
349 	struct drm_plane *primary;
350 	int ret;
351 
352 	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
353 	if (primary == NULL) {
354 		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
355 		return NULL;
356 	}
357 
358 	/*
359 	 * Remove the format_default field from drm_plane when dropping
360 	 * this helper.
361 	 */
362 	primary->format_default = true;
363 
364 	/* possible_crtc's will be filled in later by crtc_init */
365 	ret = drm_universal_plane_init(dev, primary, 0,
366 				       &drm_primary_helper_funcs,
367 				       safe_modeset_formats,
368 				       ARRAY_SIZE(safe_modeset_formats),
369 				       DRM_PLANE_TYPE_PRIMARY);
370 	if (ret) {
371 		kfree(primary);
372 		primary = NULL;
373 	}
374 
375 	return primary;
376 }
377 
378 /**
379  * drm_crtc_init - Legacy CRTC initialization function
380  * @dev: DRM device
381  * @crtc: CRTC object to init
382  * @funcs: callbacks for the new CRTC
383  *
384  * Initialize a CRTC object with a default helper-provided primary plane and no
385  * cursor plane.
386  *
387  * Returns:
388  * Zero on success, error code on failure.
389  */
390 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
391 		  const struct drm_crtc_funcs *funcs)
392 {
393 	struct drm_plane *primary;
394 
395 	primary = create_primary_plane(dev);
396 	return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
397 }
398 EXPORT_SYMBOL(drm_crtc_init);
399 
400 int drm_plane_helper_commit(struct drm_plane *plane,
401 			    struct drm_plane_state *plane_state,
402 			    struct drm_framebuffer *old_fb)
403 {
404 	const struct drm_plane_helper_funcs *plane_funcs;
405 	struct drm_crtc *crtc[2];
406 	const struct drm_crtc_helper_funcs *crtc_funcs[2];
407 	int i, ret = 0;
408 
409 	plane_funcs = plane->helper_private;
410 
411 	/* Since this is a transitional helper we can't assume that plane->state
412 	 * is always valid. Hence we need to use plane->crtc instead of
413 	 * plane->state->crtc as the old crtc. */
414 	crtc[0] = plane->crtc;
415 	crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
416 
417 	for (i = 0; i < 2; i++)
418 		crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
419 
420 	if (plane_funcs->atomic_check) {
421 		ret = plane_funcs->atomic_check(plane, plane_state);
422 		if (ret)
423 			goto out;
424 	}
425 
426 	if (plane_funcs->prepare_fb && plane_state->fb &&
427 	    plane_state->fb != old_fb) {
428 		ret = plane_funcs->prepare_fb(plane, plane_state->fb,
429 					      plane_state);
430 		if (ret)
431 			goto out;
432 	}
433 
434 	/* Point of no return, commit sw state. */
435 	swap(plane->state, plane_state);
436 
437 	for (i = 0; i < 2; i++) {
438 		if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
439 			crtc_funcs[i]->atomic_begin(crtc[i]);
440 	}
441 
442 	/*
443 	 * Drivers may optionally implement the ->atomic_disable callback, so
444 	 * special-case that here.
445 	 */
446 	if (drm_atomic_plane_disabling(plane, plane_state) &&
447 	    plane_funcs->atomic_disable)
448 		plane_funcs->atomic_disable(plane, plane_state);
449 	else
450 		plane_funcs->atomic_update(plane, plane_state);
451 
452 	for (i = 0; i < 2; i++) {
453 		if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
454 			crtc_funcs[i]->atomic_flush(crtc[i]);
455 	}
456 
457 	/*
458 	 * If we only moved the plane and didn't change fb's, there's no need to
459 	 * wait for vblank.
460 	 */
461 	if (plane->state->fb == old_fb)
462 		goto out;
463 
464 	for (i = 0; i < 2; i++) {
465 		if (!crtc[i])
466 			continue;
467 
468 		if (crtc[i]->cursor == plane)
469 			continue;
470 
471 		/* There's no other way to figure out whether the crtc is running. */
472 		ret = drm_crtc_vblank_get(crtc[i]);
473 		if (ret == 0) {
474 			drm_crtc_wait_one_vblank(crtc[i]);
475 			drm_crtc_vblank_put(crtc[i]);
476 		}
477 
478 		ret = 0;
479 	}
480 
481 	if (plane_funcs->cleanup_fb && old_fb)
482 		plane_funcs->cleanup_fb(plane, old_fb, plane_state);
483 out:
484 	if (plane_state) {
485 		if (plane->funcs->atomic_destroy_state)
486 			plane->funcs->atomic_destroy_state(plane, plane_state);
487 		else
488 			drm_atomic_helper_plane_destroy_state(plane, plane_state);
489 	}
490 
491 	return ret;
492 }
493 
494 /**
495  * drm_plane_helper_update() - Transitional helper for plane update
496  * @plane: plane object to update
497  * @crtc: owning CRTC of owning plane
498  * @fb: framebuffer to flip onto plane
499  * @crtc_x: x offset of primary plane on crtc
500  * @crtc_y: y offset of primary plane on crtc
501  * @crtc_w: width of primary plane rectangle on crtc
502  * @crtc_h: height of primary plane rectangle on crtc
503  * @src_x: x offset of @fb for panning
504  * @src_y: y offset of @fb for panning
505  * @src_w: width of source rectangle in @fb
506  * @src_h: height of source rectangle in @fb
507  *
508  * Provides a default plane update handler using the atomic plane update
509  * functions. It is fully left to the driver to check plane constraints and
510  * handle corner-cases like a fully occluded or otherwise invisible plane.
511  *
512  * This is useful for piecewise transitioning of a driver to the atomic helpers.
513  *
514  * RETURNS:
515  * Zero on success, error code on failure
516  */
517 int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
518 			    struct drm_framebuffer *fb,
519 			    int crtc_x, int crtc_y,
520 			    unsigned int crtc_w, unsigned int crtc_h,
521 			    uint32_t src_x, uint32_t src_y,
522 			    uint32_t src_w, uint32_t src_h)
523 {
524 	struct drm_plane_state *plane_state;
525 
526 	if (plane->funcs->atomic_duplicate_state)
527 		plane_state = plane->funcs->atomic_duplicate_state(plane);
528 	else if (plane->state)
529 		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
530 	else
531 		plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
532 	if (!plane_state)
533 		return -ENOMEM;
534 	plane_state->plane = plane;
535 
536 	plane_state->crtc = crtc;
537 	drm_atomic_set_fb_for_plane(plane_state, fb);
538 	plane_state->crtc_x = crtc_x;
539 	plane_state->crtc_y = crtc_y;
540 	plane_state->crtc_h = crtc_h;
541 	plane_state->crtc_w = crtc_w;
542 	plane_state->src_x = src_x;
543 	plane_state->src_y = src_y;
544 	plane_state->src_h = src_h;
545 	plane_state->src_w = src_w;
546 
547 	return drm_plane_helper_commit(plane, plane_state, plane->fb);
548 }
549 EXPORT_SYMBOL(drm_plane_helper_update);
550 
551 /**
552  * drm_plane_helper_disable() - Transitional helper for plane disable
553  * @plane: plane to disable
554  *
555  * Provides a default plane disable handler using the atomic plane update
556  * functions. It is fully left to the driver to check plane constraints and
557  * handle corner-cases like a fully occluded or otherwise invisible plane.
558  *
559  * This is useful for piecewise transitioning of a driver to the atomic helpers.
560  *
561  * RETURNS:
562  * Zero on success, error code on failure
563  */
564 int drm_plane_helper_disable(struct drm_plane *plane)
565 {
566 	struct drm_plane_state *plane_state;
567 
568 	/* crtc helpers love to call disable functions for already disabled hw
569 	 * functions. So cope with that. */
570 	if (!plane->crtc)
571 		return 0;
572 
573 	if (plane->funcs->atomic_duplicate_state)
574 		plane_state = plane->funcs->atomic_duplicate_state(plane);
575 	else if (plane->state)
576 		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
577 	else
578 		plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
579 	if (!plane_state)
580 		return -ENOMEM;
581 	plane_state->plane = plane;
582 
583 	plane_state->crtc = NULL;
584 	drm_atomic_set_fb_for_plane(plane_state, NULL);
585 
586 	return drm_plane_helper_commit(plane, plane_state, plane->fb);
587 }
588 EXPORT_SYMBOL(drm_plane_helper_disable);
589