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