xref: /dragonfly/sys/dev/drm/drm_plane_helper.c (revision 9317c2d0)
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  * The plane helpers share the function table structures with other helpers,
62  * specifically also the atomic helpers. See struct &drm_plane_helper_funcs for
63  * the details.
64  */
65 
66 /*
67  * Returns the connectors currently associated with a CRTC.  This function
68  * should be called twice:  once with a NULL connector list to retrieve
69  * the list size, and once with the properly allocated list to be filled in.
70  */
71 static int get_connectors_for_crtc(struct drm_crtc *crtc,
72 				   struct drm_connector **connector_list,
73 				   int num_connectors)
74 {
75 	struct drm_device *dev = crtc->dev;
76 	struct drm_connector *connector;
77 	int count = 0;
78 
79 	/*
80 	 * Note: Once we change the plane hooks to more fine-grained locking we
81 	 * need to grab the connection_mutex here to be able to make these
82 	 * checks.
83 	 */
84 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
85 
86 	drm_for_each_connector(connector, dev) {
87 		if (connector->encoder && connector->encoder->crtc == crtc) {
88 			if (connector_list != NULL && count < num_connectors)
89 				*(connector_list++) = connector;
90 
91 			count++;
92 		}
93 	}
94 
95 	return count;
96 }
97 
98 /**
99  * drm_plane_helper_check_state() - Check plane state for validity
100  * @state: plane state to check
101  * @clip: integer clipping coordinates
102  * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
103  * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
104  * @can_position: is it legal to position the plane such that it
105  *                doesn't cover the entire crtc?  This will generally
106  *                only be false for primary planes.
107  * @can_update_disabled: can the plane be updated while the crtc
108  *                       is disabled?
109  *
110  * Checks that a desired plane update is valid, and updates various
111  * bits of derived state (clipped coordinates etc.). Drivers that provide
112  * their own plane handling rather than helper-provided implementations may
113  * still wish to call this function to avoid duplication of error checking
114  * code.
115  *
116  * RETURNS:
117  * Zero if update appears valid, error code on failure
118  */
119 int drm_plane_helper_check_state(struct drm_plane_state *state,
120 				 const struct drm_rect *clip,
121 				 int min_scale,
122 				 int max_scale,
123 				 bool can_position,
124 				 bool can_update_disabled)
125 {
126 	struct drm_crtc *crtc = state->crtc;
127 	struct drm_framebuffer *fb = state->fb;
128 	struct drm_rect *src = &state->src;
129 	struct drm_rect *dst = &state->dst;
130 	unsigned int rotation = state->rotation;
131 	int hscale, vscale;
132 
133 	*src = drm_plane_state_src(state);
134 	*dst = drm_plane_state_dest(state);
135 
136 	if (!fb) {
137 		state->visible = false;
138 		return 0;
139 	}
140 
141 	/* crtc should only be NULL when disabling (i.e., !fb) */
142 	if (WARN_ON(!crtc)) {
143 		state->visible = false;
144 		return 0;
145 	}
146 
147 	if (!crtc->enabled && !can_update_disabled) {
148 		DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
149 		return -EINVAL;
150 	}
151 
152 	drm_rect_rotate(src, fb->width << 16, fb->height << 16, rotation);
153 
154 	/* Check scaling */
155 	hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
156 	vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
157 	if (hscale < 0 || vscale < 0) {
158 		DRM_DEBUG_KMS("Invalid scaling of plane\n");
159 		drm_rect_debug_print("src: ", &state->src, true);
160 		drm_rect_debug_print("dst: ", &state->dst, false);
161 		return -ERANGE;
162 	}
163 
164 	state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
165 
166 	drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16, rotation);
167 
168 	if (!state->visible)
169 		/*
170 		 * Plane isn't visible; some drivers can handle this
171 		 * so we just return success here.  Drivers that can't
172 		 * (including those that use the primary plane helper's
173 		 * update function) will return an error from their
174 		 * update_plane handler.
175 		 */
176 		return 0;
177 
178 	if (!can_position && !drm_rect_equals(dst, clip)) {
179 		DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
180 		drm_rect_debug_print("dst: ", dst, false);
181 		drm_rect_debug_print("clip: ", clip, false);
182 		return -EINVAL;
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(drm_plane_helper_check_state);
188 
189 /**
190  * drm_plane_helper_check_update() - Check plane update for validity
191  * @plane: plane object to update
192  * @crtc: owning CRTC of owning plane
193  * @fb: framebuffer to flip onto plane
194  * @src: source coordinates in 16.16 fixed point
195  * @dst: integer destination coordinates
196  * @clip: integer clipping coordinates
197  * @rotation: plane rotation
198  * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
199  * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
200  * @can_position: is it legal to position the plane such that it
201  *                doesn't cover the entire crtc?  This will generally
202  *                only be false for primary planes.
203  * @can_update_disabled: can the plane be updated while the crtc
204  *                       is disabled?
205  * @visible: output parameter indicating whether plane is still visible after
206  *           clipping
207  *
208  * Checks that a desired plane update is valid.  Drivers that provide
209  * their own plane handling rather than helper-provided implementations may
210  * still wish to call this function to avoid duplication of error checking
211  * code.
212  *
213  * RETURNS:
214  * Zero if update appears valid, error code on failure
215  */
216 int drm_plane_helper_check_update(struct drm_plane *plane,
217 				  struct drm_crtc *crtc,
218 				  struct drm_framebuffer *fb,
219 				  struct drm_rect *src,
220 				  struct drm_rect *dst,
221 				  const struct drm_rect *clip,
222 				  unsigned int rotation,
223 				  int min_scale,
224 				  int max_scale,
225 				  bool can_position,
226 				  bool can_update_disabled,
227 				  bool *visible)
228 {
229 	struct drm_plane_state state = {
230 		.plane = plane,
231 		.crtc = crtc,
232 		.fb = fb,
233 		.src_x = src->x1,
234 		.src_y = src->y1,
235 		.src_w = drm_rect_width(src),
236 		.src_h = drm_rect_height(src),
237 		.crtc_x = dst->x1,
238 		.crtc_y = dst->y1,
239 		.crtc_w = drm_rect_width(dst),
240 		.crtc_h = drm_rect_height(dst),
241 		.rotation = rotation,
242 		.visible = *visible,
243 	};
244 	int ret;
245 
246 	ret = drm_plane_helper_check_state(&state, clip,
247 					   min_scale, max_scale,
248 					   can_position,
249 					   can_update_disabled);
250 	if (ret)
251 		return ret;
252 
253 	*src = state.src;
254 	*dst = state.dst;
255 	*visible = state.visible;
256 
257 	return 0;
258 }
259 EXPORT_SYMBOL(drm_plane_helper_check_update);
260 
261 /**
262  * drm_primary_helper_update() - Helper for primary plane update
263  * @plane: plane object to update
264  * @crtc: owning CRTC of owning plane
265  * @fb: framebuffer to flip onto plane
266  * @crtc_x: x offset of primary plane on crtc
267  * @crtc_y: y offset of primary plane on crtc
268  * @crtc_w: width of primary plane rectangle on crtc
269  * @crtc_h: height of primary plane rectangle on crtc
270  * @src_x: x offset of @fb for panning
271  * @src_y: y offset of @fb for panning
272  * @src_w: width of source rectangle in @fb
273  * @src_h: height of source rectangle in @fb
274  *
275  * Provides a default plane update handler for primary planes.  This is handler
276  * is called in response to a userspace SetPlane operation on the plane with a
277  * non-NULL framebuffer.  We call the driver's modeset handler to update the
278  * framebuffer.
279  *
280  * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
281  * return an error.
282  *
283  * Note that we make some assumptions about hardware limitations that may not be
284  * true for all hardware --
285  *
286  * 1. Primary plane cannot be repositioned.
287  * 2. Primary plane cannot be scaled.
288  * 3. Primary plane must cover the entire CRTC.
289  * 4. Subpixel positioning is not supported.
290  *
291  * Drivers for hardware that don't have these restrictions can provide their
292  * own implementation rather than using this helper.
293  *
294  * RETURNS:
295  * Zero on success, error code on failure
296  */
297 int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
298 			      struct drm_framebuffer *fb,
299 			      int crtc_x, int crtc_y,
300 			      unsigned int crtc_w, unsigned int crtc_h,
301 			      uint32_t src_x, uint32_t src_y,
302 			      uint32_t src_w, uint32_t src_h)
303 {
304 	struct drm_mode_set set = {
305 		.crtc = crtc,
306 		.fb = fb,
307 		.mode = &crtc->mode,
308 		.x = src_x >> 16,
309 		.y = src_y >> 16,
310 	};
311 	struct drm_rect src = {
312 		.x1 = src_x,
313 		.y1 = src_y,
314 		.x2 = src_x + src_w,
315 		.y2 = src_y + src_h,
316 	};
317 	struct drm_rect dest = {
318 		.x1 = crtc_x,
319 		.y1 = crtc_y,
320 		.x2 = crtc_x + crtc_w,
321 		.y2 = crtc_y + crtc_h,
322 	};
323 	const struct drm_rect clip = {
324 		.x2 = crtc->mode.hdisplay,
325 		.y2 = crtc->mode.vdisplay,
326 	};
327 	struct drm_connector **connector_list;
328 	int num_connectors, ret;
329 	bool visible;
330 
331 	ret = drm_plane_helper_check_update(plane, crtc, fb,
332 					    &src, &dest, &clip,
333 					    DRM_ROTATE_0,
334 					    DRM_PLANE_HELPER_NO_SCALING,
335 					    DRM_PLANE_HELPER_NO_SCALING,
336 					    false, false, &visible);
337 	if (ret)
338 		return ret;
339 
340 	if (!visible)
341 		/*
342 		 * Primary plane isn't visible.  Note that unless a driver
343 		 * provides their own disable function, this will just
344 		 * wind up returning -EINVAL to userspace.
345 		 */
346 		return plane->funcs->disable_plane(plane);
347 
348 	/* Find current connectors for CRTC */
349 	num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
350 	BUG_ON(num_connectors == 0);
351 	connector_list = kzalloc(num_connectors * sizeof(*connector_list),
352 				 GFP_KERNEL);
353 	if (!connector_list)
354 		return -ENOMEM;
355 	get_connectors_for_crtc(crtc, connector_list, num_connectors);
356 
357 	set.connectors = connector_list;
358 	set.num_connectors = num_connectors;
359 
360 	/*
361 	 * We call set_config() directly here rather than using
362 	 * drm_mode_set_config_internal.  We're reprogramming the same
363 	 * connectors that were already in use, so we shouldn't need the extra
364 	 * cross-CRTC fb refcounting to accomodate stealing connectors.
365 	 * drm_mode_setplane() already handles the basic refcounting for the
366 	 * framebuffers involved in this operation.
367 	 */
368 	ret = crtc->funcs->set_config(&set);
369 
370 	kfree(connector_list);
371 	return ret;
372 }
373 EXPORT_SYMBOL(drm_primary_helper_update);
374 
375 /**
376  * drm_primary_helper_disable() - Helper for primary plane disable
377  * @plane: plane to disable
378  *
379  * Provides a default plane disable handler for primary planes.  This is handler
380  * is called in response to a userspace SetPlane operation on the plane with a
381  * NULL framebuffer parameter.  It unconditionally fails the disable call with
382  * -EINVAL the only way to disable the primary plane without driver support is
383  * to disable the entier CRTC. Which does not match the plane ->disable hook.
384  *
385  * Note that some hardware may be able to disable the primary plane without
386  * disabling the whole CRTC.  Drivers for such hardware should provide their
387  * own disable handler that disables just the primary plane (and they'll likely
388  * need to provide their own update handler as well to properly re-enable a
389  * disabled primary plane).
390  *
391  * RETURNS:
392  * Unconditionally returns -EINVAL.
393  */
394 int drm_primary_helper_disable(struct drm_plane *plane)
395 {
396 	return -EINVAL;
397 }
398 EXPORT_SYMBOL(drm_primary_helper_disable);
399 
400 /**
401  * drm_primary_helper_destroy() - Helper for primary plane destruction
402  * @plane: plane to destroy
403  *
404  * Provides a default plane destroy handler for primary planes.  This handler
405  * is called during CRTC destruction.  We disable the primary plane, remove
406  * it from the DRM plane list, and deallocate the plane structure.
407  */
408 void drm_primary_helper_destroy(struct drm_plane *plane)
409 {
410 	drm_plane_cleanup(plane);
411 	kfree(plane);
412 }
413 EXPORT_SYMBOL(drm_primary_helper_destroy);
414 
415 const struct drm_plane_funcs drm_primary_helper_funcs = {
416 	.update_plane = drm_primary_helper_update,
417 	.disable_plane = drm_primary_helper_disable,
418 	.destroy = drm_primary_helper_destroy,
419 };
420 EXPORT_SYMBOL(drm_primary_helper_funcs);
421 
422 int drm_plane_helper_commit(struct drm_plane *plane,
423 			    struct drm_plane_state *plane_state,
424 			    struct drm_framebuffer *old_fb)
425 {
426 	const struct drm_plane_helper_funcs *plane_funcs;
427 	struct drm_crtc *crtc[2];
428 	const struct drm_crtc_helper_funcs *crtc_funcs[2];
429 	int i, ret = 0;
430 
431 	plane_funcs = plane->helper_private;
432 
433 	/* Since this is a transitional helper we can't assume that plane->state
434 	 * is always valid. Hence we need to use plane->crtc instead of
435 	 * plane->state->crtc as the old crtc. */
436 	crtc[0] = plane->crtc;
437 	crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
438 
439 	for (i = 0; i < 2; i++)
440 		crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
441 
442 	if (plane_funcs->atomic_check) {
443 		ret = plane_funcs->atomic_check(plane, plane_state);
444 		if (ret)
445 			goto out;
446 	}
447 
448 	if (plane_funcs->prepare_fb && plane_state->fb &&
449 	    plane_state->fb != old_fb) {
450 		ret = plane_funcs->prepare_fb(plane,
451 					      plane_state);
452 		if (ret)
453 			goto out;
454 	}
455 
456 	/* Point of no return, commit sw state. */
457 	swap(plane->state, plane_state);
458 
459 	for (i = 0; i < 2; i++) {
460 		if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
461 			crtc_funcs[i]->atomic_begin(crtc[i], crtc[i]->state);
462 	}
463 
464 	/*
465 	 * Drivers may optionally implement the ->atomic_disable callback, so
466 	 * special-case that here.
467 	 */
468 	if (drm_atomic_plane_disabling(plane, plane_state) &&
469 	    plane_funcs->atomic_disable)
470 		plane_funcs->atomic_disable(plane, plane_state);
471 	else
472 		plane_funcs->atomic_update(plane, plane_state);
473 
474 	for (i = 0; i < 2; i++) {
475 		if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
476 			crtc_funcs[i]->atomic_flush(crtc[i], crtc[i]->state);
477 	}
478 
479 	/*
480 	 * If we only moved the plane and didn't change fb's, there's no need to
481 	 * wait for vblank.
482 	 */
483 	if (plane->state->fb == old_fb)
484 		goto out;
485 
486 	for (i = 0; i < 2; i++) {
487 		if (!crtc[i])
488 			continue;
489 
490 		if (crtc[i]->cursor == plane)
491 			continue;
492 
493 		/* There's no other way to figure out whether the crtc is running. */
494 		ret = drm_crtc_vblank_get(crtc[i]);
495 		if (ret == 0) {
496 			drm_crtc_wait_one_vblank(crtc[i]);
497 			drm_crtc_vblank_put(crtc[i]);
498 		}
499 
500 		ret = 0;
501 	}
502 
503 	if (plane_funcs->cleanup_fb)
504 		plane_funcs->cleanup_fb(plane, plane_state);
505 out:
506 	if (plane_state) {
507 		if (plane->funcs->atomic_destroy_state)
508 			plane->funcs->atomic_destroy_state(plane, plane_state);
509 		else
510 			drm_atomic_helper_plane_destroy_state(plane, plane_state);
511 	}
512 
513 	return ret;
514 }
515 
516 /**
517  * drm_plane_helper_update() - Transitional helper for plane update
518  * @plane: plane object to update
519  * @crtc: owning CRTC of owning plane
520  * @fb: framebuffer to flip onto plane
521  * @crtc_x: x offset of primary plane on crtc
522  * @crtc_y: y offset of primary plane on crtc
523  * @crtc_w: width of primary plane rectangle on crtc
524  * @crtc_h: height of primary plane rectangle on crtc
525  * @src_x: x offset of @fb for panning
526  * @src_y: y offset of @fb for panning
527  * @src_w: width of source rectangle in @fb
528  * @src_h: height of source rectangle in @fb
529  *
530  * Provides a default plane update handler using the atomic plane update
531  * functions. It is fully left to the driver to check plane constraints and
532  * handle corner-cases like a fully occluded or otherwise invisible plane.
533  *
534  * This is useful for piecewise transitioning of a driver to the atomic helpers.
535  *
536  * RETURNS:
537  * Zero on success, error code on failure
538  */
539 int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
540 			    struct drm_framebuffer *fb,
541 			    int crtc_x, int crtc_y,
542 			    unsigned int crtc_w, unsigned int crtc_h,
543 			    uint32_t src_x, uint32_t src_y,
544 			    uint32_t src_w, uint32_t src_h)
545 {
546 	struct drm_plane_state *plane_state;
547 
548 	if (plane->funcs->atomic_duplicate_state)
549 		plane_state = plane->funcs->atomic_duplicate_state(plane);
550 	else {
551 		if (!plane->state)
552 			drm_atomic_helper_plane_reset(plane);
553 
554 		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
555 	}
556 	if (!plane_state)
557 		return -ENOMEM;
558 	plane_state->plane = plane;
559 
560 	plane_state->crtc = crtc;
561 	drm_atomic_set_fb_for_plane(plane_state, fb);
562 	plane_state->crtc_x = crtc_x;
563 	plane_state->crtc_y = crtc_y;
564 	plane_state->crtc_h = crtc_h;
565 	plane_state->crtc_w = crtc_w;
566 	plane_state->src_x = src_x;
567 	plane_state->src_y = src_y;
568 	plane_state->src_h = src_h;
569 	plane_state->src_w = src_w;
570 
571 	return drm_plane_helper_commit(plane, plane_state, plane->fb);
572 }
573 EXPORT_SYMBOL(drm_plane_helper_update);
574 
575 /**
576  * drm_plane_helper_disable() - Transitional helper for plane disable
577  * @plane: plane to disable
578  *
579  * Provides a default plane disable handler using the atomic plane update
580  * functions. It is fully left to the driver to check plane constraints and
581  * handle corner-cases like a fully occluded or otherwise invisible plane.
582  *
583  * This is useful for piecewise transitioning of a driver to the atomic helpers.
584  *
585  * RETURNS:
586  * Zero on success, error code on failure
587  */
588 int drm_plane_helper_disable(struct drm_plane *plane)
589 {
590 	struct drm_plane_state *plane_state;
591 
592 	/* crtc helpers love to call disable functions for already disabled hw
593 	 * functions. So cope with that. */
594 	if (!plane->crtc)
595 		return 0;
596 
597 	if (plane->funcs->atomic_duplicate_state)
598 		plane_state = plane->funcs->atomic_duplicate_state(plane);
599 	else {
600 		if (!plane->state)
601 			drm_atomic_helper_plane_reset(plane);
602 
603 		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
604 	}
605 	if (!plane_state)
606 		return -ENOMEM;
607 	plane_state->plane = plane;
608 
609 	plane_state->crtc = NULL;
610 	drm_atomic_set_fb_for_plane(plane_state, NULL);
611 
612 	return drm_plane_helper_commit(plane, plane_state, plane->fb);
613 }
614 EXPORT_SYMBOL(drm_plane_helper_disable);
615