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 formats_ptr(struct drm_format_modifier_blob *blob)
68 return (u32 *)(((char *)blob) + blob->formats_offset);
71 static inline struct drm_format_modifier *
72 modifiers_ptr(struct drm_format_modifier_blob *blob)
74 return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
77 static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
79 const struct drm_mode_config *config = &dev->mode_config;
80 struct drm_property_blob *blob;
81 struct drm_format_modifier *mod;
82 size_t blob_size, formats_size, modifiers_size;
83 struct drm_format_modifier_blob *blob_data;
86 formats_size = sizeof(__u32) * plane->format_count;
87 if (WARN_ON(!formats_size)) {
88 /* 0 formats are never expected */
93 sizeof(struct drm_format_modifier) * plane->modifier_count;
95 blob_size = sizeof(struct drm_format_modifier_blob);
96 /* Modifiers offset is a pointer to a struct with a 64 bit field so it
97 * should be naturally aligned to 8B.
99 BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
100 blob_size += ALIGN(formats_size, 8);
101 blob_size += modifiers_size;
103 blob = drm_property_create_blob(dev, blob_size, NULL);
107 blob_data = blob->data;
108 blob_data->version = FORMAT_BLOB_CURRENT;
109 blob_data->count_formats = plane->format_count;
110 blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
111 blob_data->count_modifiers = plane->modifier_count;
113 blob_data->modifiers_offset =
114 ALIGN(blob_data->formats_offset + formats_size, 8);
116 memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
118 /* If we can't determine support, just bail */
119 if (!plane->funcs->format_mod_supported)
122 mod = modifiers_ptr(blob_data);
123 for (i = 0; i < plane->modifier_count; i++) {
124 for (j = 0; j < plane->format_count; j++) {
125 if (plane->funcs->format_mod_supported(plane,
126 plane->format_types[j],
127 plane->modifiers[i])) {
129 mod->formats |= 1ULL << j;
133 mod->modifier = plane->modifiers[i];
140 drm_object_attach_property(&plane->base, config->modifiers_property,
147 * drm_universal_plane_init - Initialize a new universal plane object
149 * @plane: plane object to init
150 * @possible_crtcs: bitmask of possible CRTCs
151 * @funcs: callbacks for the new plane
152 * @formats: array of supported formats (DRM_FORMAT\_\*)
153 * @format_count: number of elements in @formats
154 * @format_modifiers: array of struct drm_format modifiers terminated by
155 * DRM_FORMAT_MOD_INVALID
156 * @type: type of plane (overlay, primary, cursor)
157 * @name: printf style format string for the plane name, or NULL for default name
159 * Initializes a plane object of type @type.
162 * Zero on success, error code on failure.
164 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
165 uint32_t possible_crtcs,
166 const struct drm_plane_funcs *funcs,
167 const uint32_t *formats, unsigned int format_count,
168 const uint64_t *format_modifiers,
169 enum drm_plane_type type,
170 const char *name, ...)
172 struct drm_mode_config *config = &dev->mode_config;
173 unsigned int format_modifier_count = 0;
176 /* plane index is used with 32bit bitmasks */
177 if (WARN_ON(config->num_total_plane >= 32))
181 * First driver to need more than 64 formats needs to fix this. Each
182 * format is encoded as a bit and the current code only supports a u64.
184 if (WARN_ON(format_count > 64))
187 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
188 (!funcs->atomic_destroy_state ||
189 !funcs->atomic_duplicate_state));
191 ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
195 drm_modeset_lock_init(&plane->mutex);
197 plane->base.properties = &plane->properties;
199 plane->funcs = funcs;
200 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
202 if (!plane->format_types) {
203 DRM_DEBUG_KMS("out of memory when allocating plane\n");
204 drm_mode_object_unregister(dev, &plane->base);
208 if (format_modifiers) {
209 const uint64_t *temp_modifiers = format_modifiers;
210 while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
211 format_modifier_count++;
214 if (format_modifier_count)
215 config->allow_fb_modifiers = true;
217 plane->modifier_count = format_modifier_count;
218 plane->modifiers = kmalloc_array(format_modifier_count,
219 sizeof(format_modifiers[0]),
222 if (format_modifier_count && !plane->modifiers) {
223 DRM_DEBUG_KMS("out of memory when allocating plane\n");
224 kfree(plane->format_types);
225 drm_mode_object_unregister(dev, &plane->base);
233 plane->name = kvasprintf(GFP_KERNEL, name, ap);
236 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
237 drm_num_planes(dev));
240 kfree(plane->format_types);
241 kfree(plane->modifiers);
242 drm_mode_object_unregister(dev, &plane->base);
246 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
247 plane->format_count = format_count;
248 memcpy(plane->modifiers, format_modifiers,
249 format_modifier_count * sizeof(format_modifiers[0]));
250 plane->possible_crtcs = possible_crtcs;
253 list_add_tail(&plane->head, &config->plane_list);
254 plane->index = config->num_total_plane++;
256 drm_object_attach_property(&plane->base,
257 config->plane_type_property,
260 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
261 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
262 drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
263 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
264 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
265 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
266 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
267 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
268 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
269 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
270 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
271 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
274 if (config->allow_fb_modifiers)
275 create_in_format_blob(dev, plane);
279 EXPORT_SYMBOL(drm_universal_plane_init);
281 int drm_plane_register_all(struct drm_device *dev)
283 struct drm_plane *plane;
286 drm_for_each_plane(plane, dev) {
287 if (plane->funcs->late_register)
288 ret = plane->funcs->late_register(plane);
296 void drm_plane_unregister_all(struct drm_device *dev)
298 struct drm_plane *plane;
300 drm_for_each_plane(plane, dev) {
301 if (plane->funcs->early_unregister)
302 plane->funcs->early_unregister(plane);
307 * drm_plane_init - Initialize a legacy plane
309 * @plane: plane object to init
310 * @possible_crtcs: bitmask of possible CRTCs
311 * @funcs: callbacks for the new plane
312 * @formats: array of supported formats (DRM_FORMAT\_\*)
313 * @format_count: number of elements in @formats
314 * @is_primary: plane type (primary vs overlay)
316 * Legacy API to initialize a DRM plane.
318 * New drivers should call drm_universal_plane_init() instead.
321 * Zero on success, error code on failure.
323 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
324 uint32_t possible_crtcs,
325 const struct drm_plane_funcs *funcs,
326 const uint32_t *formats, unsigned int format_count,
329 enum drm_plane_type type;
331 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
332 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
333 formats, format_count,
336 EXPORT_SYMBOL(drm_plane_init);
339 * drm_plane_cleanup - Clean up the core plane usage
340 * @plane: plane to cleanup
342 * This function cleans up @plane and removes it from the DRM mode setting
343 * core. Note that the function does *not* free the plane structure itself,
344 * this is the responsibility of the caller.
346 void drm_plane_cleanup(struct drm_plane *plane)
348 struct drm_device *dev = plane->dev;
350 drm_modeset_lock_fini(&plane->mutex);
352 kfree(plane->format_types);
353 kfree(plane->modifiers);
354 drm_mode_object_unregister(dev, &plane->base);
356 BUG_ON(list_empty(&plane->head));
358 /* Note that the plane_list is considered to be static; should we
359 * remove the drm_plane at runtime we would have to decrement all
360 * the indices on the drm_plane after us in the plane_list.
363 list_del(&plane->head);
364 dev->mode_config.num_total_plane--;
366 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
367 if (plane->state && plane->funcs->atomic_destroy_state)
368 plane->funcs->atomic_destroy_state(plane, plane->state);
372 memset(plane, 0, sizeof(*plane));
374 EXPORT_SYMBOL(drm_plane_cleanup);
377 * drm_plane_from_index - find the registered plane at an index
379 * @idx: index of registered plane to find for
381 * Given a plane index, return the registered plane from DRM device's
382 * list of planes with matching index. This is the inverse of drm_plane_index().
385 drm_plane_from_index(struct drm_device *dev, int idx)
387 struct drm_plane *plane;
389 drm_for_each_plane(plane, dev)
390 if (idx == plane->index)
395 EXPORT_SYMBOL(drm_plane_from_index);
398 * drm_plane_force_disable - Forcibly disable a plane
399 * @plane: plane to disable
401 * Forces the plane to be disabled.
403 * Used when the plane's current framebuffer is destroyed,
404 * and when restoring fbdev mode.
406 * Note that this function is not suitable for atomic drivers, since it doesn't
407 * wire through the lock acquisition context properly and hence can't handle
408 * retries or driver private locks. You probably want to use
409 * drm_atomic_helper_disable_plane() or
410 * drm_atomic_helper_disable_planes_on_crtc() instead.
412 void drm_plane_force_disable(struct drm_plane *plane)
419 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
421 plane->old_fb = plane->fb;
422 ret = plane->funcs->disable_plane(plane, NULL);
424 DRM_ERROR("failed to disable plane with busy fb\n");
425 plane->old_fb = NULL;
428 /* disconnect the plane from the fb and crtc: */
429 drm_framebuffer_put(plane->old_fb);
430 plane->old_fb = NULL;
434 EXPORT_SYMBOL(drm_plane_force_disable);
437 * drm_mode_plane_set_obj_prop - set the value of a property
438 * @plane: drm plane object to set property value for
439 * @property: property to set
440 * @value: value the property should be set to
442 * This functions sets a given property on a given plane object. This function
443 * calls the driver's ->set_property callback and changes the software state of
444 * the property if the callback succeeds.
447 * Zero on success, error code on failure.
449 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
450 struct drm_property *property,
454 struct drm_mode_object *obj = &plane->base;
456 if (plane->funcs->set_property)
457 ret = plane->funcs->set_property(plane, property, value);
459 drm_object_property_set_value(obj, property, value);
463 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
465 int drm_mode_getplane_res(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
468 struct drm_mode_get_plane_res *plane_resp = data;
469 struct drm_mode_config *config;
470 struct drm_plane *plane;
471 uint32_t __user *plane_ptr;
474 if (!drm_core_check_feature(dev, DRIVER_MODESET))
477 config = &dev->mode_config;
478 plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
481 * This ioctl is called twice, once to determine how much space is
482 * needed, and the 2nd time to fill it.
484 drm_for_each_plane(plane, dev) {
486 * Unless userspace set the 'universal planes'
487 * capability bit, only advertise overlays.
489 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
490 !file_priv->universal_planes)
493 if (drm_lease_held(file_priv, plane->base.id)) {
494 if (count < plane_resp->count_planes &&
495 put_user(plane->base.id, plane_ptr + count))
500 plane_resp->count_planes = count;
505 int drm_mode_getplane(struct drm_device *dev, void *data,
506 struct drm_file *file_priv)
508 struct drm_mode_get_plane *plane_resp = data;
509 struct drm_plane *plane;
510 uint32_t __user *format_ptr;
512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
515 plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
519 drm_modeset_lock(&plane->mutex, NULL);
520 if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
521 plane_resp->crtc_id = plane->state->crtc->base.id;
522 else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
523 plane_resp->crtc_id = plane->crtc->base.id;
525 plane_resp->crtc_id = 0;
527 if (plane->state && plane->state->fb)
528 plane_resp->fb_id = plane->state->fb->base.id;
529 else if (!plane->state && plane->fb)
530 plane_resp->fb_id = plane->fb->base.id;
532 plane_resp->fb_id = 0;
533 drm_modeset_unlock(&plane->mutex);
535 plane_resp->plane_id = plane->base.id;
536 plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
537 plane->possible_crtcs);
539 plane_resp->gamma_size = 0;
542 * This ioctl is called twice, once to determine how much space is
543 * needed, and the 2nd time to fill it.
545 if (plane->format_count &&
546 (plane_resp->count_format_types >= plane->format_count)) {
547 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
548 if (copy_to_user(format_ptr,
550 sizeof(uint32_t) * plane->format_count)) {
554 plane_resp->count_format_types = plane->format_count;
559 int drm_plane_check_pixel_format(struct drm_plane *plane,
560 u32 format, u64 modifier)
564 for (i = 0; i < plane->format_count; i++) {
565 if (format == plane->format_types[i])
568 if (i == plane->format_count)
571 if (plane->funcs->format_mod_supported) {
572 if (!plane->funcs->format_mod_supported(plane, format, modifier))
575 if (!plane->modifier_count)
578 for (i = 0; i < plane->modifier_count; i++) {
579 if (modifier == plane->modifiers[i])
582 if (i == plane->modifier_count)
589 static int __setplane_check(struct drm_plane *plane,
590 struct drm_crtc *crtc,
591 struct drm_framebuffer *fb,
592 int32_t crtc_x, int32_t crtc_y,
593 uint32_t crtc_w, uint32_t crtc_h,
594 uint32_t src_x, uint32_t src_y,
595 uint32_t src_w, uint32_t src_h)
599 /* Check whether this plane is usable on this CRTC */
600 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
601 DRM_DEBUG_KMS("Invalid crtc for plane\n");
605 /* Check whether this plane supports the fb pixel format. */
606 ret = drm_plane_check_pixel_format(plane, fb->format->format,
609 struct drm_format_name_buf format_name;
611 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
612 drm_get_format_name(fb->format->format,
618 /* Give drivers some help against integer overflows */
619 if (crtc_w > INT_MAX ||
620 crtc_x > INT_MAX - (int32_t) crtc_w ||
622 crtc_y > INT_MAX - (int32_t) crtc_h) {
623 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
624 crtc_w, crtc_h, crtc_x, crtc_y);
628 ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
636 * __setplane_internal - setplane handler for internal callers
638 * This function will take a reference on the new fb for the plane
641 * src_{x,y,w,h} are provided in 16.16 fixed point format
643 static int __setplane_internal(struct drm_plane *plane,
644 struct drm_crtc *crtc,
645 struct drm_framebuffer *fb,
646 int32_t crtc_x, int32_t crtc_y,
647 uint32_t crtc_w, uint32_t crtc_h,
648 /* src_{x,y,w,h} values are 16.16 fixed point */
649 uint32_t src_x, uint32_t src_y,
650 uint32_t src_w, uint32_t src_h,
651 struct drm_modeset_acquire_ctx *ctx)
655 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
657 /* No fb means shut it down */
659 plane->old_fb = plane->fb;
660 ret = plane->funcs->disable_plane(plane, ctx);
665 plane->old_fb = NULL;
670 ret = __setplane_check(plane, crtc, fb,
671 crtc_x, crtc_y, crtc_w, crtc_h,
672 src_x, src_y, src_w, src_h);
676 plane->old_fb = plane->fb;
677 ret = plane->funcs->update_plane(plane, crtc, fb,
678 crtc_x, crtc_y, crtc_w, crtc_h,
679 src_x, src_y, src_w, src_h, ctx);
683 drm_framebuffer_get(plane->fb);
685 plane->old_fb = NULL;
690 drm_framebuffer_put(plane->old_fb);
691 plane->old_fb = NULL;
696 static int __setplane_atomic(struct drm_plane *plane,
697 struct drm_crtc *crtc,
698 struct drm_framebuffer *fb,
699 int32_t crtc_x, int32_t crtc_y,
700 uint32_t crtc_w, uint32_t crtc_h,
701 uint32_t src_x, uint32_t src_y,
702 uint32_t src_w, uint32_t src_h,
703 struct drm_modeset_acquire_ctx *ctx)
707 WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
709 /* No fb means shut it down */
711 return plane->funcs->disable_plane(plane, ctx);
714 * FIXME: This is redundant with drm_atomic_plane_check(),
715 * but the legacy cursor/"async" .update_plane() tricks
716 * don't call that so we still need this here. Should remove
717 * this when all .update_plane() implementations have been
718 * fixed to call drm_atomic_plane_check().
720 ret = __setplane_check(plane, crtc, fb,
721 crtc_x, crtc_y, crtc_w, crtc_h,
722 src_x, src_y, src_w, src_h);
726 return plane->funcs->update_plane(plane, crtc, fb,
727 crtc_x, crtc_y, crtc_w, crtc_h,
728 src_x, src_y, src_w, src_h, ctx);
731 static int setplane_internal(struct drm_plane *plane,
732 struct drm_crtc *crtc,
733 struct drm_framebuffer *fb,
734 int32_t crtc_x, int32_t crtc_y,
735 uint32_t crtc_w, uint32_t crtc_h,
736 /* src_{x,y,w,h} values are 16.16 fixed point */
737 uint32_t src_x, uint32_t src_y,
738 uint32_t src_w, uint32_t src_h)
740 struct drm_modeset_acquire_ctx ctx;
743 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
745 ret = drm_modeset_lock_all_ctx(plane->dev, &ctx);
749 if (drm_drv_uses_atomic_modeset(plane->dev))
750 ret = __setplane_atomic(plane, crtc, fb,
751 crtc_x, crtc_y, crtc_w, crtc_h,
752 src_x, src_y, src_w, src_h, &ctx);
754 ret = __setplane_internal(plane, crtc, fb,
755 crtc_x, crtc_y, crtc_w, crtc_h,
756 src_x, src_y, src_w, src_h, &ctx);
759 if (ret == -EDEADLK) {
760 ret = drm_modeset_backoff(&ctx);
764 drm_modeset_drop_locks(&ctx);
765 drm_modeset_acquire_fini(&ctx);
770 int drm_mode_setplane(struct drm_device *dev, void *data,
771 struct drm_file *file_priv)
773 struct drm_mode_set_plane *plane_req = data;
774 struct drm_plane *plane;
775 struct drm_crtc *crtc = NULL;
776 struct drm_framebuffer *fb = NULL;
779 if (!drm_core_check_feature(dev, DRIVER_MODESET))
783 * First, find the plane, crtc, and fb objects. If not available,
784 * we don't bother to call the driver.
786 plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
788 DRM_DEBUG_KMS("Unknown plane ID %d\n",
789 plane_req->plane_id);
793 if (plane_req->fb_id) {
794 fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
796 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
801 crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
803 drm_framebuffer_put(fb);
804 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
810 ret = setplane_internal(plane, crtc, fb,
811 plane_req->crtc_x, plane_req->crtc_y,
812 plane_req->crtc_w, plane_req->crtc_h,
813 plane_req->src_x, plane_req->src_y,
814 plane_req->src_w, plane_req->src_h);
817 drm_framebuffer_put(fb);
822 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
823 struct drm_mode_cursor2 *req,
824 struct drm_file *file_priv,
825 struct drm_modeset_acquire_ctx *ctx)
827 struct drm_device *dev = crtc->dev;
828 struct drm_plane *plane = crtc->cursor;
829 struct drm_framebuffer *fb = NULL;
830 struct drm_mode_fb_cmd2 fbreq = {
832 .height = req->height,
833 .pixel_format = DRM_FORMAT_ARGB8888,
834 .pitches = { req->width * 4 },
835 .handles = { req->handle },
837 int32_t crtc_x, crtc_y;
838 uint32_t crtc_w = 0, crtc_h = 0;
839 uint32_t src_w = 0, src_h = 0;
843 WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
846 * Obtain fb we'll be using (either new or existing) and take an extra
847 * reference to it if fb != null. setplane will take care of dropping
848 * the reference if the plane update fails.
850 if (req->flags & DRM_MODE_CURSOR_BO) {
852 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
854 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
858 fb->hot_x = req->hot_x;
859 fb->hot_y = req->hot_y;
865 fb = plane->state->fb;
870 drm_framebuffer_get(fb);
873 if (req->flags & DRM_MODE_CURSOR_MOVE) {
877 crtc_x = crtc->cursor_x;
878 crtc_y = crtc->cursor_y;
884 src_w = fb->width << 16;
885 src_h = fb->height << 16;
888 if (drm_drv_uses_atomic_modeset(dev))
889 ret = __setplane_atomic(plane, crtc, fb,
890 crtc_x, crtc_y, crtc_w, crtc_h,
891 0, 0, src_w, src_h, ctx);
893 ret = __setplane_internal(plane, crtc, fb,
894 crtc_x, crtc_y, crtc_w, crtc_h,
895 0, 0, src_w, src_h, ctx);
898 drm_framebuffer_put(fb);
900 /* Update successful; save new cursor position, if necessary */
901 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
902 crtc->cursor_x = req->x;
903 crtc->cursor_y = req->y;
909 static int drm_mode_cursor_common(struct drm_device *dev,
910 struct drm_mode_cursor2 *req,
911 struct drm_file *file_priv)
913 struct drm_crtc *crtc;
914 struct drm_modeset_acquire_ctx ctx;
917 if (!drm_core_check_feature(dev, DRIVER_MODESET))
920 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
923 crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
925 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
929 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
931 ret = drm_modeset_lock(&crtc->mutex, &ctx);
935 * If this crtc has a universal cursor plane, call that plane's update
936 * handler rather than using legacy cursor handlers.
939 ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
943 if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
948 ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
952 if (req->flags & DRM_MODE_CURSOR_BO) {
953 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
957 /* Turns off the cursor if handle is 0 */
958 if (crtc->funcs->cursor_set2)
959 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
960 req->width, req->height, req->hot_x, req->hot_y);
962 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
963 req->width, req->height);
966 if (req->flags & DRM_MODE_CURSOR_MOVE) {
967 if (crtc->funcs->cursor_move) {
968 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
975 if (ret == -EDEADLK) {
976 ret = drm_modeset_backoff(&ctx);
981 drm_modeset_drop_locks(&ctx);
982 drm_modeset_acquire_fini(&ctx);
989 int drm_mode_cursor_ioctl(struct drm_device *dev,
990 void *data, struct drm_file *file_priv)
992 struct drm_mode_cursor *req = data;
993 struct drm_mode_cursor2 new_req;
995 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
996 new_req.hot_x = new_req.hot_y = 0;
998 return drm_mode_cursor_common(dev, &new_req, file_priv);
1002 * Set the cursor configuration based on user request. This implements the 2nd
1003 * version of the cursor ioctl, which allows userspace to additionally specify
1004 * the hotspot of the pointer.
1006 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1007 void *data, struct drm_file *file_priv)
1009 struct drm_mode_cursor2 *req = data;
1011 return drm_mode_cursor_common(dev, req, file_priv);
1014 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1015 void *data, struct drm_file *file_priv)
1017 struct drm_mode_crtc_page_flip_target *page_flip = data;
1018 struct drm_crtc *crtc;
1019 struct drm_plane *plane;
1020 struct drm_framebuffer *fb = NULL, *old_fb;
1021 struct drm_pending_vblank_event *e = NULL;
1022 u32 target_vblank = page_flip->sequence;
1023 struct drm_modeset_acquire_ctx ctx;
1026 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1029 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1032 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1035 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1038 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1041 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1044 crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
1048 plane = crtc->primary;
1050 if (!drm_lease_held(file_priv, plane->base.id))
1053 if (crtc->funcs->page_flip_target) {
1057 r = drm_crtc_vblank_get(crtc);
1061 current_vblank = (u32)drm_crtc_vblank_count(crtc);
1063 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1064 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1065 if ((int)(target_vblank - current_vblank) > 1) {
1066 DRM_DEBUG("Invalid absolute flip target %u, "
1067 "must be <= %u\n", target_vblank,
1068 current_vblank + 1);
1069 drm_crtc_vblank_put(crtc);
1073 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1074 if (target_vblank != 0 && target_vblank != 1) {
1075 DRM_DEBUG("Invalid relative flip target %u, "
1076 "must be 0 or 1\n", target_vblank);
1077 drm_crtc_vblank_put(crtc);
1080 target_vblank += current_vblank;
1083 target_vblank = current_vblank +
1084 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1087 } else if (crtc->funcs->page_flip == NULL ||
1088 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1092 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1094 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1097 ret = drm_modeset_lock(&plane->mutex, &ctx);
1102 old_fb = plane->state->fb;
1106 if (old_fb == NULL) {
1107 /* The framebuffer is currently unbound, presumably
1108 * due to a hotplug event, that userspace has not
1115 fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
1122 const struct drm_plane_state *state = plane->state;
1124 ret = drm_framebuffer_check_src_coords(state->src_x,
1130 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
1136 if (old_fb->format != fb->format) {
1137 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
1142 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1143 e = kzalloc(sizeof *e, GFP_KERNEL);
1149 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1150 e->event.base.length = sizeof(e->event);
1151 e->event.vbl.user_data = page_flip->user_data;
1152 e->event.vbl.crtc_id = crtc->base.id;
1154 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1162 plane->old_fb = plane->fb;
1163 if (crtc->funcs->page_flip_target)
1164 ret = crtc->funcs->page_flip_target(crtc, fb, e,
1169 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
1172 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
1173 drm_event_cancel_free(dev, &e->base);
1174 /* Keep the old fb, don't unref it. */
1175 plane->old_fb = NULL;
1177 if (!plane->state) {
1179 drm_framebuffer_get(fb);
1185 drm_framebuffer_put(fb);
1187 drm_framebuffer_put(plane->old_fb);
1188 plane->old_fb = NULL;
1190 if (ret == -EDEADLK) {
1191 ret = drm_modeset_backoff(&ctx);
1196 drm_modeset_drop_locks(&ctx);
1197 drm_modeset_acquire_fini(&ctx);
1199 if (ret && crtc->funcs->page_flip_target)
1200 drm_crtc_vblank_put(crtc);