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