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