2 * Copyright (c) 2016 Intel Corporation
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 #include <drm/drm_plane.h>
26 #include "drm_crtc_internal.h"
31 * A plane represents an image source that can be blended with or overlayed on
32 * top of a CRTC during the scanout process. Planes take their input data from a
33 * &drm_framebuffer object. The plane itself specifies the cropping and scaling
34 * of that image, and where it is placed on the visible are of a display
35 * pipeline, represented by &drm_crtc. A plane can also have additional
36 * properties that specify how the pixels are positioned and blended, like
37 * rotation or Z-position. All these properties are stored in &drm_plane_state.
39 * To create a plane, a KMS drivers allocates and zeroes an instances of
40 * struct &drm_plane (possibly as part of a larger structure) and registers it
41 * with a call to drm_universal_plane_init().
43 * Cursor and overlay planes are optional. All drivers should provide one
44 * primary plane per CRTC to avoid surprising userspace too much. See enum
45 * &drm_plane_type for a more in-depth discussion of these special uapi-relevant
46 * plane types. Special planes are associated with their CRTC by calling
47 * drm_crtc_init_with_planes().
49 * The type of a plane is exposed in the immutable "type" enumeration property,
50 * which has one of the following values: "Overlay", "Primary", "Cursor".
53 static unsigned int drm_num_planes(struct drm_device
*dev
)
56 struct drm_plane
*tmp
;
58 drm_for_each_plane(tmp
, dev
) {
66 * drm_universal_plane_init - Initialize a new universal plane object
68 * @plane: plane object to init
69 * @possible_crtcs: bitmask of possible CRTCs
70 * @funcs: callbacks for the new plane
71 * @formats: array of supported formats (DRM_FORMAT\_\*)
72 * @format_count: number of elements in @formats
73 * @type: type of plane (overlay, primary, cursor)
74 * @name: printf style format string for the plane name, or NULL for default name
76 * Initializes a plane object of type @type.
79 * Zero on success, error code on failure.
81 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
82 unsigned long possible_crtcs
,
83 const struct drm_plane_funcs
*funcs
,
84 const uint32_t *formats
, unsigned int format_count
,
85 enum drm_plane_type type
,
86 const char *name
, ...)
88 struct drm_mode_config
*config
= &dev
->mode_config
;
91 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
95 drm_modeset_lock_init(&plane
->mutex
);
97 plane
->base
.properties
= &plane
->properties
;
100 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
102 if (!plane
->format_types
) {
103 DRM_DEBUG_KMS("out of memory when allocating plane\n");
104 drm_mode_object_unregister(dev
, &plane
->base
);
112 plane
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
115 plane
->name
= kasprintf(GFP_KERNEL
, "plane-%d",
116 drm_num_planes(dev
));
119 kfree(plane
->format_types
);
120 drm_mode_object_unregister(dev
, &plane
->base
);
124 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
125 plane
->format_count
= format_count
;
126 plane
->possible_crtcs
= possible_crtcs
;
129 list_add_tail(&plane
->head
, &config
->plane_list
);
130 plane
->index
= config
->num_total_plane
++;
131 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
132 config
->num_overlay_plane
++;
134 drm_object_attach_property(&plane
->base
,
135 config
->plane_type_property
,
138 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
139 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
140 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
141 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
142 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
143 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
144 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
145 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
146 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
147 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
148 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
153 EXPORT_SYMBOL(drm_universal_plane_init
);
155 int drm_plane_register_all(struct drm_device
*dev
)
157 struct drm_plane
*plane
;
160 drm_for_each_plane(plane
, dev
) {
161 if (plane
->funcs
->late_register
)
162 ret
= plane
->funcs
->late_register(plane
);
170 void drm_plane_unregister_all(struct drm_device
*dev
)
172 struct drm_plane
*plane
;
174 drm_for_each_plane(plane
, dev
) {
175 if (plane
->funcs
->early_unregister
)
176 plane
->funcs
->early_unregister(plane
);
181 * drm_plane_init - Initialize a legacy plane
183 * @plane: plane object to init
184 * @possible_crtcs: bitmask of possible CRTCs
185 * @funcs: callbacks for the new plane
186 * @formats: array of supported formats (DRM_FORMAT\_\*)
187 * @format_count: number of elements in @formats
188 * @is_primary: plane type (primary vs overlay)
190 * Legacy API to initialize a DRM plane.
192 * New drivers should call drm_universal_plane_init() instead.
195 * Zero on success, error code on failure.
197 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
198 unsigned long possible_crtcs
,
199 const struct drm_plane_funcs
*funcs
,
200 const uint32_t *formats
, unsigned int format_count
,
203 enum drm_plane_type type
;
205 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
206 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
207 formats
, format_count
, type
, NULL
);
209 EXPORT_SYMBOL(drm_plane_init
);
212 * drm_plane_cleanup - Clean up the core plane usage
213 * @plane: plane to cleanup
215 * This function cleans up @plane and removes it from the DRM mode setting
216 * core. Note that the function does *not* free the plane structure itself,
217 * this is the responsibility of the caller.
219 void drm_plane_cleanup(struct drm_plane
*plane
)
221 struct drm_device
*dev
= plane
->dev
;
223 drm_modeset_lock_all(dev
);
224 kfree(plane
->format_types
);
225 drm_mode_object_unregister(dev
, &plane
->base
);
227 BUG_ON(list_empty(&plane
->head
));
229 /* Note that the plane_list is considered to be static; should we
230 * remove the drm_plane at runtime we would have to decrement all
231 * the indices on the drm_plane after us in the plane_list.
234 list_del(&plane
->head
);
235 dev
->mode_config
.num_total_plane
--;
236 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
237 dev
->mode_config
.num_overlay_plane
--;
238 drm_modeset_unlock_all(dev
);
240 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
241 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
242 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
246 memset(plane
, 0, sizeof(*plane
));
248 EXPORT_SYMBOL(drm_plane_cleanup
);
251 * drm_plane_from_index - find the registered plane at an index
253 * @idx: index of registered plane to find for
255 * Given a plane index, return the registered plane from DRM device's
256 * list of planes with matching index.
259 drm_plane_from_index(struct drm_device
*dev
, int idx
)
261 struct drm_plane
*plane
;
263 drm_for_each_plane(plane
, dev
)
264 if (idx
== plane
->index
)
269 EXPORT_SYMBOL(drm_plane_from_index
);
272 * drm_plane_force_disable - Forcibly disable a plane
273 * @plane: plane to disable
275 * Forces the plane to be disabled.
277 * Used when the plane's current framebuffer is destroyed,
278 * and when restoring fbdev mode.
280 void drm_plane_force_disable(struct drm_plane
*plane
)
287 plane
->old_fb
= plane
->fb
;
288 ret
= plane
->funcs
->disable_plane(plane
);
290 DRM_ERROR("failed to disable plane with busy fb\n");
291 plane
->old_fb
= NULL
;
294 /* disconnect the plane from the fb and crtc: */
295 drm_framebuffer_unreference(plane
->old_fb
);
296 plane
->old_fb
= NULL
;
300 EXPORT_SYMBOL(drm_plane_force_disable
);
303 * drm_mode_plane_set_obj_prop - set the value of a property
304 * @plane: drm plane object to set property value for
305 * @property: property to set
306 * @value: value the property should be set to
308 * This functions sets a given property on a given plane object. This function
309 * calls the driver's ->set_property callback and changes the software state of
310 * the property if the callback succeeds.
313 * Zero on success, error code on failure.
315 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
316 struct drm_property
*property
,
320 struct drm_mode_object
*obj
= &plane
->base
;
322 if (plane
->funcs
->set_property
)
323 ret
= plane
->funcs
->set_property(plane
, property
, value
);
325 drm_object_property_set_value(obj
, property
, value
);
329 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
331 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
332 struct drm_file
*file_priv
)
334 struct drm_mode_get_plane_res
*plane_resp
= data
;
335 struct drm_mode_config
*config
;
336 struct drm_plane
*plane
;
337 uint32_t __user
*plane_ptr
;
341 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
344 config
= &dev
->mode_config
;
346 if (file_priv
->universal_planes
)
347 num_planes
= config
->num_total_plane
;
349 num_planes
= config
->num_overlay_plane
;
352 * This ioctl is called twice, once to determine how much space is
353 * needed, and the 2nd time to fill it.
356 (plane_resp
->count_planes
>= num_planes
)) {
357 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
359 /* Plane lists are invariant, no locking needed. */
360 drm_for_each_plane(plane
, dev
) {
362 * Unless userspace set the 'universal planes'
363 * capability bit, only advertise overlays.
365 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
366 !file_priv
->universal_planes
)
369 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
374 plane_resp
->count_planes
= num_planes
;
379 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
380 struct drm_file
*file_priv
)
382 struct drm_mode_get_plane
*plane_resp
= data
;
383 struct drm_plane
*plane
;
384 uint32_t __user
*format_ptr
;
386 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
389 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
393 drm_modeset_lock(&plane
->mutex
, NULL
);
395 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
397 plane_resp
->crtc_id
= 0;
400 plane_resp
->fb_id
= plane
->fb
->base
.id
;
402 plane_resp
->fb_id
= 0;
403 drm_modeset_unlock(&plane
->mutex
);
405 plane_resp
->plane_id
= plane
->base
.id
;
406 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
407 plane_resp
->gamma_size
= 0;
410 * This ioctl is called twice, once to determine how much space is
411 * needed, and the 2nd time to fill it.
413 if (plane
->format_count
&&
414 (plane_resp
->count_format_types
>= plane
->format_count
)) {
415 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
416 if (copy_to_user(format_ptr
,
418 sizeof(uint32_t) * plane
->format_count
)) {
422 plane_resp
->count_format_types
= plane
->format_count
;
427 int drm_plane_check_pixel_format(const struct drm_plane
*plane
, u32 format
)
431 for (i
= 0; i
< plane
->format_count
; i
++) {
432 if (format
== plane
->format_types
[i
])
440 * setplane_internal - setplane handler for internal callers
442 * Note that we assume an extra reference has already been taken on fb. If the
443 * update fails, this reference will be dropped before return; if it succeeds,
444 * the previous framebuffer (if any) will be unreferenced instead.
446 * src_{x,y,w,h} are provided in 16.16 fixed point format
448 static int __setplane_internal(struct drm_plane
*plane
,
449 struct drm_crtc
*crtc
,
450 struct drm_framebuffer
*fb
,
451 int32_t crtc_x
, int32_t crtc_y
,
452 uint32_t crtc_w
, uint32_t crtc_h
,
453 /* src_{x,y,w,h} values are 16.16 fixed point */
454 uint32_t src_x
, uint32_t src_y
,
455 uint32_t src_w
, uint32_t src_h
)
459 /* No fb means shut it down */
461 plane
->old_fb
= plane
->fb
;
462 ret
= plane
->funcs
->disable_plane(plane
);
467 plane
->old_fb
= NULL
;
472 /* Check whether this plane is usable on this CRTC */
473 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
474 DRM_DEBUG_KMS("Invalid crtc for plane\n");
479 /* Check whether this plane supports the fb pixel format. */
480 ret
= drm_plane_check_pixel_format(plane
, fb
->pixel_format
);
482 char *format_name
= drm_get_format_name(fb
->pixel_format
);
483 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name
);
488 /* Give drivers some help against integer overflows */
489 if (crtc_w
> INT_MAX
||
490 crtc_x
> INT_MAX
- (int32_t) crtc_w
||
492 crtc_y
> INT_MAX
- (int32_t) crtc_h
) {
493 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
494 crtc_w
, crtc_h
, crtc_x
, crtc_y
);
499 ret
= drm_framebuffer_check_src_coords(src_x
, src_y
, src_w
, src_h
, fb
);
503 plane
->old_fb
= plane
->fb
;
504 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
505 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
506 src_x
, src_y
, src_w
, src_h
);
512 plane
->old_fb
= NULL
;
517 drm_framebuffer_unreference(fb
);
519 drm_framebuffer_unreference(plane
->old_fb
);
520 plane
->old_fb
= NULL
;
525 static int setplane_internal(struct drm_plane
*plane
,
526 struct drm_crtc
*crtc
,
527 struct drm_framebuffer
*fb
,
528 int32_t crtc_x
, int32_t crtc_y
,
529 uint32_t crtc_w
, uint32_t crtc_h
,
530 /* src_{x,y,w,h} values are 16.16 fixed point */
531 uint32_t src_x
, uint32_t src_y
,
532 uint32_t src_w
, uint32_t src_h
)
536 drm_modeset_lock_all(plane
->dev
);
537 ret
= __setplane_internal(plane
, crtc
, fb
,
538 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
539 src_x
, src_y
, src_w
, src_h
);
540 drm_modeset_unlock_all(plane
->dev
);
545 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
546 struct drm_file
*file_priv
)
548 struct drm_mode_set_plane
*plane_req
= data
;
549 struct drm_plane
*plane
;
550 struct drm_crtc
*crtc
= NULL
;
551 struct drm_framebuffer
*fb
= NULL
;
553 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
557 * First, find the plane, crtc, and fb objects. If not available,
558 * we don't bother to call the driver.
560 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
562 DRM_DEBUG_KMS("Unknown plane ID %d\n",
563 plane_req
->plane_id
);
567 if (plane_req
->fb_id
) {
568 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
570 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
575 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
577 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
584 * setplane_internal will take care of deref'ing either the old or new
585 * framebuffer depending on success.
587 return setplane_internal(plane
, crtc
, fb
,
588 plane_req
->crtc_x
, plane_req
->crtc_y
,
589 plane_req
->crtc_w
, plane_req
->crtc_h
,
590 plane_req
->src_x
, plane_req
->src_y
,
591 plane_req
->src_w
, plane_req
->src_h
);
594 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
595 struct drm_mode_cursor2
*req
,
596 struct drm_file
*file_priv
)
598 struct drm_device
*dev
= crtc
->dev
;
599 struct drm_framebuffer
*fb
= NULL
;
600 struct drm_mode_fb_cmd2 fbreq
= {
602 .height
= req
->height
,
603 .pixel_format
= DRM_FORMAT_ARGB8888
,
604 .pitches
= { req
->width
* 4 },
605 .handles
= { req
->handle
},
607 int32_t crtc_x
, crtc_y
;
608 uint32_t crtc_w
= 0, crtc_h
= 0;
609 uint32_t src_w
= 0, src_h
= 0;
612 BUG_ON(!crtc
->cursor
);
613 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
616 * Obtain fb we'll be using (either new or existing) and take an extra
617 * reference to it if fb != null. setplane will take care of dropping
618 * the reference if the plane update fails.
620 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
622 fb
= drm_internal_framebuffer_create(dev
, &fbreq
, file_priv
);
624 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
627 fb
->hot_x
= req
->hot_x
;
628 fb
->hot_y
= req
->hot_y
;
633 fb
= crtc
->cursor
->fb
;
635 drm_framebuffer_reference(fb
);
638 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
642 crtc_x
= crtc
->cursor_x
;
643 crtc_y
= crtc
->cursor_y
;
649 src_w
= fb
->width
<< 16;
650 src_h
= fb
->height
<< 16;
654 * setplane_internal will take care of deref'ing either the old or new
655 * framebuffer depending on success.
657 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
658 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
661 /* Update successful; save new cursor position, if necessary */
662 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
663 crtc
->cursor_x
= req
->x
;
664 crtc
->cursor_y
= req
->y
;
670 static int drm_mode_cursor_common(struct drm_device
*dev
,
671 struct drm_mode_cursor2
*req
,
672 struct drm_file
*file_priv
)
674 struct drm_crtc
*crtc
;
677 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
680 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
683 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
685 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
690 * If this crtc has a universal cursor plane, call that plane's update
691 * handler rather than using legacy cursor handlers.
693 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
695 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
699 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
700 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
704 /* Turns off the cursor if handle is 0 */
705 if (crtc
->funcs
->cursor_set2
)
706 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
707 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
709 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
710 req
->width
, req
->height
);
713 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
714 if (crtc
->funcs
->cursor_move
) {
715 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
722 drm_modeset_unlock_crtc(crtc
);
729 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
730 void *data
, struct drm_file
*file_priv
)
732 struct drm_mode_cursor
*req
= data
;
733 struct drm_mode_cursor2 new_req
;
735 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
736 new_req
.hot_x
= new_req
.hot_y
= 0;
738 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
742 * Set the cursor configuration based on user request. This implements the 2nd
743 * version of the cursor ioctl, which allows userspace to additionally specify
744 * the hotspot of the pointer.
746 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
747 void *data
, struct drm_file
*file_priv
)
749 struct drm_mode_cursor2
*req
= data
;
751 return drm_mode_cursor_common(dev
, req
, file_priv
);
754 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
755 void *data
, struct drm_file
*file_priv
)
757 struct drm_mode_crtc_page_flip_target
*page_flip
= data
;
758 struct drm_crtc
*crtc
;
759 struct drm_framebuffer
*fb
= NULL
;
760 struct drm_pending_vblank_event
*e
= NULL
;
761 u32 target_vblank
= page_flip
->sequence
;
764 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
767 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
)
770 if (page_flip
->sequence
!= 0 && !(page_flip
->flags
& DRM_MODE_PAGE_FLIP_TARGET
))
773 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
776 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_TARGET
) == DRM_MODE_PAGE_FLIP_TARGET
)
779 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
782 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
786 if (crtc
->funcs
->page_flip_target
) {
790 r
= drm_crtc_vblank_get(crtc
);
794 current_vblank
= drm_crtc_vblank_count(crtc
);
796 switch (page_flip
->flags
& DRM_MODE_PAGE_FLIP_TARGET
) {
797 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE
:
798 if ((int)(target_vblank
- current_vblank
) > 1) {
799 DRM_DEBUG("Invalid absolute flip target %u, "
800 "must be <= %u\n", target_vblank
,
802 drm_crtc_vblank_put(crtc
);
806 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE
:
807 if (target_vblank
!= 0 && target_vblank
!= 1) {
808 DRM_DEBUG("Invalid relative flip target %u, "
809 "must be 0 or 1\n", target_vblank
);
810 drm_crtc_vblank_put(crtc
);
813 target_vblank
+= current_vblank
;
816 target_vblank
= current_vblank
+
817 !(page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
);
820 } else if (crtc
->funcs
->page_flip
== NULL
||
821 (page_flip
->flags
& DRM_MODE_PAGE_FLIP_TARGET
)) {
825 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
826 if (crtc
->primary
->fb
== NULL
) {
827 /* The framebuffer is currently unbound, presumably
828 * due to a hotplug event, that userspace has not
835 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
842 const struct drm_plane_state
*state
= crtc
->primary
->state
;
844 ret
= drm_framebuffer_check_src_coords(state
->src_x
,
850 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
855 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
856 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
861 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
862 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
867 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
868 e
->event
.base
.length
= sizeof(e
->event
);
869 e
->event
.user_data
= page_flip
->user_data
;
870 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
, &e
->event
.base
);
877 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
878 if (crtc
->funcs
->page_flip_target
)
879 ret
= crtc
->funcs
->page_flip_target(crtc
, fb
, e
,
883 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
885 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
)
886 drm_event_cancel_free(dev
, &e
->base
);
887 /* Keep the old fb, don't unref it. */
888 crtc
->primary
->old_fb
= NULL
;
890 crtc
->primary
->fb
= fb
;
891 /* Unref only the old framebuffer. */
896 if (ret
&& crtc
->funcs
->page_flip_target
)
897 drm_crtc_vblank_put(crtc
);
899 drm_framebuffer_unreference(fb
);
900 if (crtc
->primary
->old_fb
)
901 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
902 crtc
->primary
->old_fb
= NULL
;
903 drm_modeset_unlock_crtc(crtc
);