blob: fa56bb5da6c3a4be57cb4f036e92cb12fd42bfe6 [file] [log] [blame]
Matt Roperc103d1c2014-04-01 15:22:35 -07001/*
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>
Matt Roper7daf8d52014-05-29 08:06:52 -070028#include <drm/drm_plane_helper.h>
Matt Roperc103d1c2014-04-01 15:22:35 -070029#include <drm/drm_rect.h>
Daniel Vetteracf24a32014-07-29 15:33:05 +020030#include <drm/drm_crtc_helper.h>
Matt Roperc103d1c2014-04-01 15:22:35 -070031
32#define SUBPIXEL_MASK 0xffff
33
Daniel Vetter3150c7d2014-11-06 20:53:29 +010034/**
35 * DOC: overview
36 *
37 * This helper library has two parts. The first part has support to implement
38 * primary plane support on top of the normal CRTC configuration interface.
39 * Since the legacy ->set_config interface ties the primary plane together with
40 * the CRTC state this does not allow userspace to disable the primary plane
41 * itself. To avoid too much duplicated code use
42 * drm_plane_helper_check_update() which can be used to enforce the same
43 * restrictions as primary planes had thus. The default primary plane only
44 * expose XRBG8888 and ARGB8888 as valid pixel formats for the attached
45 * framebuffer.
46 *
47 * Drivers are highly recommended to implement proper support for primary
48 * planes, and newly merged drivers must not rely upon these transitional
49 * helpers.
50 *
51 * The second part also implements transitional helpers which allow drivers to
52 * gradually switch to the atomic helper infrastructure for plane updates. Once
53 * that switch is complete drivers shouldn't use these any longer, instead using
54 * the proper legacy implementations for update and disable plane hooks provided
55 * by the atomic helpers.
56 *
57 * Again drivers are strongly urged to switch to the new interfaces.
58 */
59
Matt Roperc103d1c2014-04-01 15:22:35 -070060/*
61 * This is the minimal list of formats that seem to be safe for modeset use
62 * with all current DRM drivers. Most hardware can actually support more
63 * formats than this and drivers may specify a more accurate list when
64 * creating the primary plane. However drivers that still call
65 * drm_plane_init() will use this minimal format list as the default.
66 */
Thierry Reding233fd4e2014-05-13 16:58:35 +020067static const uint32_t safe_modeset_formats[] = {
68 DRM_FORMAT_XRGB8888,
69 DRM_FORMAT_ARGB8888,
Matt Roperc103d1c2014-04-01 15:22:35 -070070};
71
72/*
73 * Returns the connectors currently associated with a CRTC. This function
74 * should be called twice: once with a NULL connector list to retrieve
75 * the list size, and once with the properly allocated list to be filled in.
76 */
77static int get_connectors_for_crtc(struct drm_crtc *crtc,
78 struct drm_connector **connector_list,
79 int num_connectors)
80{
81 struct drm_device *dev = crtc->dev;
82 struct drm_connector *connector;
83 int count = 0;
84
Daniel Vetter6e9f7982014-05-29 23:54:47 +020085 /*
86 * Note: Once we change the plane hooks to more fine-grained locking we
87 * need to grab the connection_mutex here to be able to make these
88 * checks.
89 */
Rob Clark51fd3712013-11-19 12:10:12 -050090 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +020091
Matt Roperc103d1c2014-04-01 15:22:35 -070092 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
93 if (connector->encoder && connector->encoder->crtc == crtc) {
94 if (connector_list != NULL && count < num_connectors)
95 *(connector_list++) = connector;
96
97 count++;
98 }
99
100 return count;
101}
102
103/**
Matt Roper7daf8d52014-05-29 08:06:52 -0700104 * drm_plane_helper_check_update() - Check plane update for validity
105 * @plane: plane object to update
106 * @crtc: owning CRTC of owning plane
107 * @fb: framebuffer to flip onto plane
108 * @src: source coordinates in 16.16 fixed point
109 * @dest: integer destination coordinates
110 * @clip: integer clipping coordinates
111 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
112 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
113 * @can_position: is it legal to position the plane such that it
114 * doesn't cover the entire crtc? This will generally
115 * only be false for primary planes.
116 * @can_update_disabled: can the plane be updated while the crtc
117 * is disabled?
118 * @visible: output parameter indicating whether plane is still visible after
119 * clipping
120 *
121 * Checks that a desired plane update is valid. Drivers that provide
122 * their own plane handling rather than helper-provided implementations may
123 * still wish to call this function to avoid duplication of error checking
124 * code.
125 *
126 * RETURNS:
127 * Zero if update appears valid, error code on failure
128 */
129int drm_plane_helper_check_update(struct drm_plane *plane,
130 struct drm_crtc *crtc,
131 struct drm_framebuffer *fb,
132 struct drm_rect *src,
133 struct drm_rect *dest,
134 const struct drm_rect *clip,
135 int min_scale,
136 int max_scale,
137 bool can_position,
138 bool can_update_disabled,
139 bool *visible)
140{
141 int hscale, vscale;
142
143 if (!crtc->enabled && !can_update_disabled) {
144 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
145 return -EINVAL;
146 }
147
148 /* Check scaling */
149 hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
150 vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
151 if (hscale < 0 || vscale < 0) {
152 DRM_DEBUG_KMS("Invalid scaling of plane\n");
153 return -ERANGE;
154 }
155
156 *visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
157 if (!*visible)
158 /*
159 * Plane isn't visible; some drivers can handle this
160 * so we just return success here. Drivers that can't
161 * (including those that use the primary plane helper's
162 * update function) will return an error from their
163 * update_plane handler.
164 */
165 return 0;
166
167 if (!can_position && !drm_rect_equals(dest, clip)) {
168 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
169 return -EINVAL;
170 }
171
172 return 0;
173}
174EXPORT_SYMBOL(drm_plane_helper_check_update);
175
176/**
Matt Roperc103d1c2014-04-01 15:22:35 -0700177 * drm_primary_helper_update() - Helper for primary plane update
178 * @plane: plane object to update
179 * @crtc: owning CRTC of owning plane
180 * @fb: framebuffer to flip onto plane
181 * @crtc_x: x offset of primary plane on crtc
182 * @crtc_y: y offset of primary plane on crtc
183 * @crtc_w: width of primary plane rectangle on crtc
184 * @crtc_h: height of primary plane rectangle on crtc
185 * @src_x: x offset of @fb for panning
186 * @src_y: y offset of @fb for panning
187 * @src_w: width of source rectangle in @fb
188 * @src_h: height of source rectangle in @fb
189 *
190 * Provides a default plane update handler for primary planes. This is handler
191 * is called in response to a userspace SetPlane operation on the plane with a
192 * non-NULL framebuffer. We call the driver's modeset handler to update the
193 * framebuffer.
194 *
195 * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
196 * return an error.
197 *
198 * Note that we make some assumptions about hardware limitations that may not be
199 * true for all hardware --
200 * 1) Primary plane cannot be repositioned.
201 * 2) Primary plane cannot be scaled.
202 * 3) Primary plane must cover the entire CRTC.
203 * 4) Subpixel positioning is not supported.
204 * Drivers for hardware that don't have these restrictions can provide their
205 * own implementation rather than using this helper.
206 *
207 * RETURNS:
208 * Zero on success, error code on failure
209 */
210int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
211 struct drm_framebuffer *fb,
212 int crtc_x, int crtc_y,
213 unsigned int crtc_w, unsigned int crtc_h,
214 uint32_t src_x, uint32_t src_y,
215 uint32_t src_w, uint32_t src_h)
216{
217 struct drm_mode_set set = {
218 .crtc = crtc,
219 .fb = fb,
220 .mode = &crtc->mode,
221 .x = src_x >> 16,
222 .y = src_y >> 16,
223 };
Matt Roper7daf8d52014-05-29 08:06:52 -0700224 struct drm_rect src = {
225 .x1 = src_x,
226 .y1 = src_y,
227 .x2 = src_x + src_w,
228 .y2 = src_y + src_h,
229 };
Matt Roperc103d1c2014-04-01 15:22:35 -0700230 struct drm_rect dest = {
231 .x1 = crtc_x,
232 .y1 = crtc_y,
233 .x2 = crtc_x + crtc_w,
234 .y2 = crtc_y + crtc_h,
235 };
Matt Roper7daf8d52014-05-29 08:06:52 -0700236 const struct drm_rect clip = {
Matt Roperc103d1c2014-04-01 15:22:35 -0700237 .x2 = crtc->mode.hdisplay,
238 .y2 = crtc->mode.vdisplay,
239 };
240 struct drm_connector **connector_list;
Matt Roperc103d1c2014-04-01 15:22:35 -0700241 int num_connectors, ret;
Matt Roper7daf8d52014-05-29 08:06:52 -0700242 bool visible;
Matt Roperc103d1c2014-04-01 15:22:35 -0700243
Matt Roper7daf8d52014-05-29 08:06:52 -0700244 ret = drm_plane_helper_check_update(plane, crtc, fb,
245 &src, &dest, &clip,
246 DRM_PLANE_HELPER_NO_SCALING,
247 DRM_PLANE_HELPER_NO_SCALING,
248 false, false, &visible);
Matt Roperc103d1c2014-04-01 15:22:35 -0700249 if (ret)
250 return ret;
251
Matt Roper7daf8d52014-05-29 08:06:52 -0700252 if (!visible)
253 /*
254 * Primary plane isn't visible. Note that unless a driver
255 * provides their own disable function, this will just
256 * wind up returning -EINVAL to userspace.
257 */
258 return plane->funcs->disable_plane(plane);
259
Matt Roperc103d1c2014-04-01 15:22:35 -0700260 /* Find current connectors for CRTC */
261 num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
262 BUG_ON(num_connectors == 0);
263 connector_list = kzalloc(num_connectors * sizeof(*connector_list),
264 GFP_KERNEL);
265 if (!connector_list)
266 return -ENOMEM;
267 get_connectors_for_crtc(crtc, connector_list, num_connectors);
268
269 set.connectors = connector_list;
270 set.num_connectors = num_connectors;
271
272 /*
Daniel Vetter0fe27f02014-04-23 17:34:06 +0200273 * We call set_config() directly here rather than using
Matt Roperc103d1c2014-04-01 15:22:35 -0700274 * drm_mode_set_config_internal. We're reprogramming the same
275 * connectors that were already in use, so we shouldn't need the extra
276 * cross-CRTC fb refcounting to accomodate stealing connectors.
277 * drm_mode_setplane() already handles the basic refcounting for the
278 * framebuffers involved in this operation.
279 */
Matt Roperc103d1c2014-04-01 15:22:35 -0700280 ret = crtc->funcs->set_config(&set);
Matt Roperc103d1c2014-04-01 15:22:35 -0700281
282 kfree(connector_list);
283 return ret;
284}
285EXPORT_SYMBOL(drm_primary_helper_update);
286
287/**
288 * drm_primary_helper_disable() - Helper for primary plane disable
289 * @plane: plane to disable
290 *
291 * Provides a default plane disable handler for primary planes. This is handler
292 * is called in response to a userspace SetPlane operation on the plane with a
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200293 * NULL framebuffer parameter. It unconditionally fails the disable call with
294 * -EINVAL the only way to disable the primary plane without driver support is
295 * to disable the entier CRTC. Which does not match the plane ->disable hook.
Matt Roperc103d1c2014-04-01 15:22:35 -0700296 *
297 * Note that some hardware may be able to disable the primary plane without
298 * disabling the whole CRTC. Drivers for such hardware should provide their
299 * own disable handler that disables just the primary plane (and they'll likely
300 * need to provide their own update handler as well to properly re-enable a
301 * disabled primary plane).
302 *
303 * RETURNS:
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200304 * Unconditionally returns -EINVAL.
Matt Roperc103d1c2014-04-01 15:22:35 -0700305 */
306int drm_primary_helper_disable(struct drm_plane *plane)
307{
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200308 return -EINVAL;
Matt Roperc103d1c2014-04-01 15:22:35 -0700309}
310EXPORT_SYMBOL(drm_primary_helper_disable);
311
312/**
313 * drm_primary_helper_destroy() - Helper for primary plane destruction
314 * @plane: plane to destroy
315 *
316 * Provides a default plane destroy handler for primary planes. This handler
317 * is called during CRTC destruction. We disable the primary plane, remove
318 * it from the DRM plane list, and deallocate the plane structure.
319 */
320void drm_primary_helper_destroy(struct drm_plane *plane)
321{
Matt Roperc103d1c2014-04-01 15:22:35 -0700322 drm_plane_cleanup(plane);
323 kfree(plane);
324}
325EXPORT_SYMBOL(drm_primary_helper_destroy);
326
327const struct drm_plane_funcs drm_primary_helper_funcs = {
328 .update_plane = drm_primary_helper_update,
329 .disable_plane = drm_primary_helper_disable,
330 .destroy = drm_primary_helper_destroy,
331};
332EXPORT_SYMBOL(drm_primary_helper_funcs);
333
334/**
335 * drm_primary_helper_create_plane() - Create a generic primary plane
336 * @dev: drm device
337 * @formats: pixel formats supported, or NULL for a default safe list
338 * @num_formats: size of @formats; ignored if @formats is NULL
339 *
340 * Allocates and initializes a primary plane that can be used with the primary
341 * plane helpers. Drivers that wish to use driver-specific plane structures or
342 * provide custom handler functions may perform their own allocation and
343 * initialization rather than calling this function.
344 */
345struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
346 const uint32_t *formats,
347 int num_formats)
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 if (formats == NULL) {
359 formats = safe_modeset_formats;
360 num_formats = ARRAY_SIZE(safe_modeset_formats);
361 }
362
363 /* possible_crtc's will be filled in later by crtc_init */
Matt Roper3281cc72014-06-30 15:37:51 -0700364 ret = drm_universal_plane_init(dev, primary, 0,
365 &drm_primary_helper_funcs,
366 formats, num_formats,
367 DRM_PLANE_TYPE_PRIMARY);
Matt Roperc103d1c2014-04-01 15:22:35 -0700368 if (ret) {
369 kfree(primary);
370 primary = NULL;
371 }
372
373 return primary;
374}
375EXPORT_SYMBOL(drm_primary_helper_create_plane);
376
Matt Ropere13161a2014-04-01 15:22:38 -0700377/**
378 * drm_crtc_init - Legacy CRTC initialization function
379 * @dev: DRM device
380 * @crtc: CRTC object to init
381 * @funcs: callbacks for the new CRTC
382 *
383 * Initialize a CRTC object with a default helper-provided primary plane and no
384 * cursor plane.
385 *
386 * Returns:
387 * Zero on success, error code on failure.
388 */
389int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
390 const struct drm_crtc_funcs *funcs)
391{
392 struct drm_plane *primary;
393
394 primary = drm_primary_helper_create_plane(dev, NULL, 0);
395 return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
396}
397EXPORT_SYMBOL(drm_crtc_init);
Daniel Vetteracf24a32014-07-29 15:33:05 +0200398
Daniel Vetter2f324b42014-10-29 11:13:47 +0100399int drm_plane_helper_commit(struct drm_plane *plane,
400 struct drm_plane_state *plane_state,
401 struct drm_framebuffer *old_fb)
Daniel Vetteracf24a32014-07-29 15:33:05 +0200402{
403 struct drm_plane_helper_funcs *plane_funcs;
404 struct drm_crtc *crtc[2];
405 struct drm_crtc_helper_funcs *crtc_funcs[2];
406 int i, ret = 0;
407
408 plane_funcs = plane->helper_private;
409
410 /* Since this is a transitional helper we can't assume that plane->state
411 * is always valid. Hence we need to use plane->crtc instead of
412 * plane->state->crtc as the old crtc. */
413 crtc[0] = plane->crtc;
414 crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
415
416 for (i = 0; i < 2; i++)
417 crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
418
419 if (plane_funcs->atomic_check) {
420 ret = plane_funcs->atomic_check(plane, plane_state);
421 if (ret)
422 goto out;
423 }
424
425 if (plane_funcs->prepare_fb && plane_state->fb) {
426 ret = plane_funcs->prepare_fb(plane, plane_state->fb);
427 if (ret)
428 goto out;
429 }
430
431 /* Point of no return, commit sw state. */
432 swap(plane->state, plane_state);
433
434 for (i = 0; i < 2; i++) {
435 if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
436 crtc_funcs[i]->atomic_begin(crtc[i]);
437 }
438
439 plane_funcs->atomic_update(plane);
440
441 for (i = 0; i < 2; i++) {
442 if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
443 crtc_funcs[i]->atomic_flush(crtc[i]);
444 }
445
446 for (i = 0; i < 2; i++) {
447 if (!crtc[i])
448 continue;
449
450 /* There's no other way to figure out whether the crtc is running. */
451 ret = drm_crtc_vblank_get(crtc[i]);
452 if (ret == 0) {
453 drm_crtc_wait_one_vblank(crtc[i]);
454 drm_crtc_vblank_put(crtc[i]);
455 }
456
457 ret = 0;
458 }
459
460 if (plane_funcs->cleanup_fb && old_fb)
461 plane_funcs->cleanup_fb(plane, old_fb);
462out:
463 if (plane_state) {
464 if (plane->funcs->atomic_destroy_state)
465 plane->funcs->atomic_destroy_state(plane, plane_state);
466 else
467 kfree(plane_state);
468 }
469
470 return ret;
471}
472
473/**
474 * drm_plane_helper_update() - Helper for primary plane update
475 * @plane: plane object to update
476 * @crtc: owning CRTC of owning plane
477 * @fb: framebuffer to flip onto plane
478 * @crtc_x: x offset of primary plane on crtc
479 * @crtc_y: y offset of primary plane on crtc
480 * @crtc_w: width of primary plane rectangle on crtc
481 * @crtc_h: height of primary plane rectangle on crtc
482 * @src_x: x offset of @fb for panning
483 * @src_y: y offset of @fb for panning
484 * @src_w: width of source rectangle in @fb
485 * @src_h: height of source rectangle in @fb
486 *
487 * Provides a default plane update handler using the atomic plane update
488 * functions. It is fully left to the driver to check plane constraints and
489 * handle corner-cases like a fully occluded or otherwise invisible plane.
490 *
491 * This is useful for piecewise transitioning of a driver to the atomic helpers.
492 *
493 * RETURNS:
494 * Zero on success, error code on failure
495 */
496int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
497 struct drm_framebuffer *fb,
498 int crtc_x, int crtc_y,
499 unsigned int crtc_w, unsigned int crtc_h,
500 uint32_t src_x, uint32_t src_y,
501 uint32_t src_w, uint32_t src_h)
502{
503 struct drm_plane_state *plane_state;
504
505 if (plane->funcs->atomic_duplicate_state)
506 plane_state = plane->funcs->atomic_duplicate_state(plane);
507 else if (plane->state)
508 plane_state = kmemdup(plane->state, sizeof(*plane_state),
509 GFP_KERNEL);
510 else
511 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
512 if (!plane_state)
513 return -ENOMEM;
514
515 plane_state->crtc = crtc;
516 plane_state->fb = fb;
517 plane_state->crtc_x = crtc_x;
518 plane_state->crtc_y = crtc_y;
519 plane_state->crtc_h = crtc_h;
520 plane_state->crtc_w = crtc_w;
521 plane_state->src_x = src_x;
522 plane_state->src_y = src_y;
523 plane_state->src_h = src_h;
524 plane_state->src_w = src_w;
525
Daniel Vetter2f324b42014-10-29 11:13:47 +0100526 return drm_plane_helper_commit(plane, plane_state, plane->fb);
Daniel Vetteracf24a32014-07-29 15:33:05 +0200527}
528EXPORT_SYMBOL(drm_plane_helper_update);
529
530/**
531 * drm_plane_helper_disable() - Helper for primary plane disable
532 * @plane: plane to disable
533 *
534 * Provides a default plane disable handler using the atomic plane update
535 * functions. It is fully left to the driver to check plane constraints and
536 * handle corner-cases like a fully occluded or otherwise invisible plane.
537 *
538 * This is useful for piecewise transitioning of a driver to the atomic helpers.
539 *
540 * RETURNS:
541 * Zero on success, error code on failure
542 */
543int drm_plane_helper_disable(struct drm_plane *plane)
544{
545 struct drm_plane_state *plane_state;
546
547 /* crtc helpers love to call disable functions for already disabled hw
548 * functions. So cope with that. */
549 if (!plane->crtc)
550 return 0;
551
552 if (plane->funcs->atomic_duplicate_state)
553 plane_state = plane->funcs->atomic_duplicate_state(plane);
554 else if (plane->state)
555 plane_state = kmemdup(plane->state, sizeof(*plane_state),
556 GFP_KERNEL);
557 else
558 plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
559 if (!plane_state)
560 return -ENOMEM;
561
562 plane_state->crtc = NULL;
563 plane_state->fb = NULL;
564
Daniel Vetter2f324b42014-10-29 11:13:47 +0100565 return drm_plane_helper_commit(plane, plane_state, plane->fb);
Daniel Vetteracf24a32014-07-29 15:33:05 +0200566}
567EXPORT_SYMBOL(drm_plane_helper_disable);