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
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
26 #include <drm/drm_plane.h>
27 #include <drm/drm_drv.h>
28 #include <drm/drm_print.h>
29 #include <drm/drm_framebuffer.h>
30 #include <drm/drm_file.h>
31 #include <drm/drm_crtc.h>
32 #include <drm/drm_fourcc.h>
33 #include <drm/drm_vblank.h>
35 #include "drm_crtc_internal.h"
40 * A plane represents an image source that can be blended with or overlayed on
41 * top of a CRTC during the scanout process. Planes take their input data from a
42 * &drm_framebuffer object. The plane itself specifies the cropping and scaling
43 * of that image, and where it is placed on the visible area of a display
44 * pipeline, represented by &drm_crtc. A plane can also have additional
45 * properties that specify how the pixels are positioned and blended, like
46 * rotation or Z-position. All these properties are stored in &drm_plane_state.
48 * To create a plane, a KMS drivers allocates and zeroes an instances of
49 * &struct drm_plane (possibly as part of a larger structure) and registers it
50 * with a call to drm_universal_plane_init().
52 * The type of a plane is exposed in the immutable "type" enumeration property,
53 * which has one of the following values: "Overlay", "Primary", "Cursor" (see
54 * enum drm_plane_type). A plane can be compatible with multiple CRTCs, see
55 * &drm_plane.possible_crtcs.
57 * Each CRTC must have a unique primary plane userspace can attach to enable
58 * the CRTC. In other words, userspace must be able to attach a different
59 * primary plane to each CRTC at the same time. Primary planes can still be
60 * compatible with multiple CRTCs. There must be exactly as many primary planes
63 * Legacy uAPI doesn't expose the primary and cursor planes directly. DRM core
64 * relies on the driver to set the primary and optionally the cursor plane used
65 * for legacy IOCTLs. This is done by calling drm_crtc_init_with_planes(). All
66 * drivers must provide one primary plane per CRTC to avoid surprising legacy
71 * DOC: standard plane properties
73 * DRM planes have a few standardized properties:
76 * Blob property which contains the set of buffer format and modifier
77 * pairs supported by this plane. The blob is a struct
78 * drm_format_modifier_blob. Without this property the plane doesn't
79 * support buffers with modifiers. Userspace cannot change this property.
82 static unsigned int drm_num_planes(struct drm_device *dev)
85 struct drm_plane *tmp;
87 drm_for_each_plane(tmp, dev) {
95 formats_ptr(struct drm_format_modifier_blob *blob)
97 return (u32 *)(((char *)blob) + blob->formats_offset);
100 static inline struct drm_format_modifier *
101 modifiers_ptr(struct drm_format_modifier_blob *blob)
103 return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
106 static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
108 const struct drm_mode_config *config = &dev->mode_config;
109 struct drm_property_blob *blob;
110 struct drm_format_modifier *mod;
111 size_t blob_size, formats_size, modifiers_size;
112 struct drm_format_modifier_blob *blob_data;
115 formats_size = sizeof(__u32) * plane->format_count;
116 if (WARN_ON(!formats_size)) {
117 /* 0 formats are never expected */
122 sizeof(struct drm_format_modifier) * plane->modifier_count;
124 blob_size = sizeof(struct drm_format_modifier_blob);
125 /* Modifiers offset is a pointer to a struct with a 64 bit field so it
126 * should be naturally aligned to 8B.
128 BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
129 blob_size += ALIGN(formats_size, 8);
130 blob_size += modifiers_size;
132 blob = drm_property_create_blob(dev, blob_size, NULL);
136 blob_data = blob->data;
137 blob_data->version = FORMAT_BLOB_CURRENT;
138 blob_data->count_formats = plane->format_count;
139 blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
140 blob_data->count_modifiers = plane->modifier_count;
142 blob_data->modifiers_offset =
143 ALIGN(blob_data->formats_offset + formats_size, 8);
145 memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
147 /* If we can't determine support, just bail */
148 if (!plane->funcs->format_mod_supported)
151 mod = modifiers_ptr(blob_data);
152 for (i = 0; i < plane->modifier_count; i++) {
153 for (j = 0; j < plane->format_count; j++) {
154 if (plane->funcs->format_mod_supported(plane,
155 plane->format_types[j],
156 plane->modifiers[i])) {
158 mod->formats |= 1ULL << j;
162 mod->modifier = plane->modifiers[i];
169 drm_object_attach_property(&plane->base, config->modifiers_property,
176 * drm_universal_plane_init - Initialize a new universal plane object
178 * @plane: plane object to init
179 * @possible_crtcs: bitmask of possible CRTCs
180 * @funcs: callbacks for the new plane
181 * @formats: array of supported formats (DRM_FORMAT\_\*)
182 * @format_count: number of elements in @formats
183 * @format_modifiers: array of struct drm_format modifiers terminated by
184 * DRM_FORMAT_MOD_INVALID
185 * @type: type of plane (overlay, primary, cursor)
186 * @name: printf style format string for the plane name, or NULL for default name
188 * Initializes a plane object of type @type.
191 * Zero on success, error code on failure.
193 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
194 uint32_t possible_crtcs,
195 const struct drm_plane_funcs *funcs,
196 const uint32_t *formats, unsigned int format_count,
197 const uint64_t *format_modifiers,
198 enum drm_plane_type type,
199 const char *name, ...)
201 struct drm_mode_config *config = &dev->mode_config;
202 unsigned int format_modifier_count = 0;
205 /* plane index is used with 32bit bitmasks */
206 if (WARN_ON(config->num_total_plane >= 32))
209 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
210 (!funcs->atomic_destroy_state ||
211 !funcs->atomic_duplicate_state));
213 ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
217 drm_modeset_lock_init(&plane->mutex);
219 plane->base.properties = &plane->properties;
221 plane->funcs = funcs;
222 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
224 if (!plane->format_types) {
225 DRM_DEBUG_KMS("out of memory when allocating plane\n");
226 drm_mode_object_unregister(dev, &plane->base);
231 * First driver to need more than 64 formats needs to fix this. Each
232 * format is encoded as a bit and the current code only supports a u64.
234 if (WARN_ON(format_count > 64))
237 if (format_modifiers) {
238 const uint64_t *temp_modifiers = format_modifiers;
240 while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
241 format_modifier_count++;
244 if (format_modifier_count)
245 config->allow_fb_modifiers = true;
247 plane->modifier_count = format_modifier_count;
248 plane->modifiers = kmalloc_array(format_modifier_count,
249 sizeof(format_modifiers[0]),
252 if (format_modifier_count && !plane->modifiers) {
253 DRM_DEBUG_KMS("out of memory when allocating plane\n");
254 kfree(plane->format_types);
255 drm_mode_object_unregister(dev, &plane->base);
263 plane->name = kvasprintf(GFP_KERNEL, name, ap);
266 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
267 drm_num_planes(dev));
270 kfree(plane->format_types);
271 kfree(plane->modifiers);
272 drm_mode_object_unregister(dev, &plane->base);
276 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
277 plane->format_count = format_count;
278 memcpy(plane->modifiers, format_modifiers,
279 format_modifier_count * sizeof(format_modifiers[0]));
280 plane->possible_crtcs = possible_crtcs;
283 list_add_tail(&plane->head, &config->plane_list);
284 plane->index = config->num_total_plane++;
286 drm_object_attach_property(&plane->base,
287 config->plane_type_property,
290 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
291 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
292 drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
293 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
294 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
295 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
296 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
297 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
298 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
299 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
300 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
301 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
304 if (config->allow_fb_modifiers)
305 create_in_format_blob(dev, plane);
309 EXPORT_SYMBOL(drm_universal_plane_init);
311 int drm_plane_register_all(struct drm_device *dev)
313 unsigned int num_planes = 0;
314 unsigned int num_zpos = 0;
315 struct drm_plane *plane;
318 drm_for_each_plane(plane, dev) {
319 if (plane->funcs->late_register)
320 ret = plane->funcs->late_register(plane);
324 if (plane->zpos_property)
329 drm_WARN(dev, num_zpos && num_planes != num_zpos,
330 "Mixing planes with and without zpos property is invalid\n");
335 void drm_plane_unregister_all(struct drm_device *dev)
337 struct drm_plane *plane;
339 drm_for_each_plane(plane, dev) {
340 if (plane->funcs->early_unregister)
341 plane->funcs->early_unregister(plane);
346 * drm_plane_init - Initialize a legacy plane
348 * @plane: plane object to init
349 * @possible_crtcs: bitmask of possible CRTCs
350 * @funcs: callbacks for the new plane
351 * @formats: array of supported formats (DRM_FORMAT\_\*)
352 * @format_count: number of elements in @formats
353 * @is_primary: plane type (primary vs overlay)
355 * Legacy API to initialize a DRM plane.
357 * New drivers should call drm_universal_plane_init() instead.
360 * Zero on success, error code on failure.
362 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
363 uint32_t possible_crtcs,
364 const struct drm_plane_funcs *funcs,
365 const uint32_t *formats, unsigned int format_count,
368 enum drm_plane_type type;
370 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
371 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
372 formats, format_count,
375 EXPORT_SYMBOL(drm_plane_init);
378 * drm_plane_cleanup - Clean up the core plane usage
379 * @plane: plane to cleanup
381 * This function cleans up @plane and removes it from the DRM mode setting
382 * core. Note that the function does *not* free the plane structure itself,
383 * this is the responsibility of the caller.
385 void drm_plane_cleanup(struct drm_plane *plane)
387 struct drm_device *dev = plane->dev;
389 drm_modeset_lock_fini(&plane->mutex);
391 kfree(plane->format_types);
392 kfree(plane->modifiers);
393 drm_mode_object_unregister(dev, &plane->base);
395 BUG_ON(list_empty(&plane->head));
397 /* Note that the plane_list is considered to be static; should we
398 * remove the drm_plane at runtime we would have to decrement all
399 * the indices on the drm_plane after us in the plane_list.
402 list_del(&plane->head);
403 dev->mode_config.num_total_plane--;
405 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
406 if (plane->state && plane->funcs->atomic_destroy_state)
407 plane->funcs->atomic_destroy_state(plane, plane->state);
411 memset(plane, 0, sizeof(*plane));
413 EXPORT_SYMBOL(drm_plane_cleanup);
416 * drm_plane_from_index - find the registered plane at an index
418 * @idx: index of registered plane to find for
420 * Given a plane index, return the registered plane from DRM device's
421 * list of planes with matching index. This is the inverse of drm_plane_index().
424 drm_plane_from_index(struct drm_device *dev, int idx)
426 struct drm_plane *plane;
428 drm_for_each_plane(plane, dev)
429 if (idx == plane->index)
434 EXPORT_SYMBOL(drm_plane_from_index);
437 * drm_plane_force_disable - Forcibly disable a plane
438 * @plane: plane to disable
440 * Forces the plane to be disabled.
442 * Used when the plane's current framebuffer is destroyed,
443 * and when restoring fbdev mode.
445 * Note that this function is not suitable for atomic drivers, since it doesn't
446 * wire through the lock acquisition context properly and hence can't handle
447 * retries or driver private locks. You probably want to use
448 * drm_atomic_helper_disable_plane() or
449 * drm_atomic_helper_disable_planes_on_crtc() instead.
451 void drm_plane_force_disable(struct drm_plane *plane)
458 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
460 plane->old_fb = plane->fb;
461 ret = plane->funcs->disable_plane(plane, NULL);
463 DRM_ERROR("failed to disable plane with busy fb\n");
464 plane->old_fb = NULL;
467 /* disconnect the plane from the fb and crtc: */
468 drm_framebuffer_put(plane->old_fb);
469 plane->old_fb = NULL;
473 EXPORT_SYMBOL(drm_plane_force_disable);
476 * drm_mode_plane_set_obj_prop - set the value of a property
477 * @plane: drm plane object to set property value for
478 * @property: property to set
479 * @value: value the property should be set to
481 * This functions sets a given property on a given plane object. This function
482 * calls the driver's ->set_property callback and changes the software state of
483 * the property if the callback succeeds.
486 * Zero on success, error code on failure.
488 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
489 struct drm_property *property,
493 struct drm_mode_object *obj = &plane->base;
495 if (plane->funcs->set_property)
496 ret = plane->funcs->set_property(plane, property, value);
498 drm_object_property_set_value(obj, property, value);
502 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
504 int drm_mode_getplane_res(struct drm_device *dev, void *data,
505 struct drm_file *file_priv)
507 struct drm_mode_get_plane_res *plane_resp = data;
508 struct drm_plane *plane;
509 uint32_t __user *plane_ptr;
512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
515 plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
518 * This ioctl is called twice, once to determine how much space is
519 * needed, and the 2nd time to fill it.
521 drm_for_each_plane(plane, dev) {
523 * Unless userspace set the 'universal planes'
524 * capability bit, only advertise overlays.
526 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
527 !file_priv->universal_planes)
530 if (drm_lease_held(file_priv, plane->base.id)) {
531 if (count < plane_resp->count_planes &&
532 put_user(plane->base.id, plane_ptr + count))
537 plane_resp->count_planes = count;
542 int drm_mode_getplane(struct drm_device *dev, void *data,
543 struct drm_file *file_priv)
545 struct drm_mode_get_plane *plane_resp = data;
546 struct drm_plane *plane;
547 uint32_t __user *format_ptr;
549 if (!drm_core_check_feature(dev, DRIVER_MODESET))
552 plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
556 drm_modeset_lock(&plane->mutex, NULL);
557 if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
558 plane_resp->crtc_id = plane->state->crtc->base.id;
559 else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
560 plane_resp->crtc_id = plane->crtc->base.id;
562 plane_resp->crtc_id = 0;
564 if (plane->state && plane->state->fb)
565 plane_resp->fb_id = plane->state->fb->base.id;
566 else if (!plane->state && plane->fb)
567 plane_resp->fb_id = plane->fb->base.id;
569 plane_resp->fb_id = 0;
570 drm_modeset_unlock(&plane->mutex);
572 plane_resp->plane_id = plane->base.id;
573 plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
574 plane->possible_crtcs);
576 plane_resp->gamma_size = 0;
579 * This ioctl is called twice, once to determine how much space is
580 * needed, and the 2nd time to fill it.
582 if (plane->format_count &&
583 (plane_resp->count_format_types >= plane->format_count)) {
584 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
585 if (copy_to_user(format_ptr,
587 sizeof(uint32_t) * plane->format_count)) {
591 plane_resp->count_format_types = plane->format_count;
596 int drm_plane_check_pixel_format(struct drm_plane *plane,
597 u32 format, u64 modifier)
601 for (i = 0; i < plane->format_count; i++) {
602 if (format == plane->format_types[i])
605 if (i == plane->format_count)
608 if (plane->funcs->format_mod_supported) {
609 if (!plane->funcs->format_mod_supported(plane, format, modifier))
612 if (!plane->modifier_count)
615 for (i = 0; i < plane->modifier_count; i++) {
616 if (modifier == plane->modifiers[i])
619 if (i == plane->modifier_count)
626 static int __setplane_check(struct drm_plane *plane,
627 struct drm_crtc *crtc,
628 struct drm_framebuffer *fb,
629 int32_t crtc_x, int32_t crtc_y,
630 uint32_t crtc_w, uint32_t crtc_h,
631 uint32_t src_x, uint32_t src_y,
632 uint32_t src_w, uint32_t src_h)
636 /* Check whether this plane is usable on this CRTC */
637 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
638 DRM_DEBUG_KMS("Invalid crtc for plane\n");
642 /* Check whether this plane supports the fb pixel format. */
643 ret = drm_plane_check_pixel_format(plane, fb->format->format,
646 struct drm_format_name_buf format_name;
648 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
649 drm_get_format_name(fb->format->format,
655 /* Give drivers some help against integer overflows */
656 if (crtc_w > INT_MAX ||
657 crtc_x > INT_MAX - (int32_t) crtc_w ||
659 crtc_y > INT_MAX - (int32_t) crtc_h) {
660 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
661 crtc_w, crtc_h, crtc_x, crtc_y);
665 ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
673 * drm_any_plane_has_format - Check whether any plane supports this format and modifier combination
675 * @format: pixel format (DRM_FORMAT_*)
676 * @modifier: data layout modifier
679 * Whether at least one plane supports the specified format and modifier combination.
681 bool drm_any_plane_has_format(struct drm_device *dev,
682 u32 format, u64 modifier)
684 struct drm_plane *plane;
686 drm_for_each_plane(plane, dev) {
687 if (drm_plane_check_pixel_format(plane, format, modifier) == 0)
693 EXPORT_SYMBOL(drm_any_plane_has_format);
696 * __setplane_internal - setplane handler for internal callers
698 * This function will take a reference on the new fb for the plane
701 * src_{x,y,w,h} are provided in 16.16 fixed point format
703 static int __setplane_internal(struct drm_plane *plane,
704 struct drm_crtc *crtc,
705 struct drm_framebuffer *fb,
706 int32_t crtc_x, int32_t crtc_y,
707 uint32_t crtc_w, uint32_t crtc_h,
708 /* src_{x,y,w,h} values are 16.16 fixed point */
709 uint32_t src_x, uint32_t src_y,
710 uint32_t src_w, uint32_t src_h,
711 struct drm_modeset_acquire_ctx *ctx)
715 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
717 /* No fb means shut it down */
719 plane->old_fb = plane->fb;
720 ret = plane->funcs->disable_plane(plane, ctx);
725 plane->old_fb = NULL;
730 ret = __setplane_check(plane, crtc, fb,
731 crtc_x, crtc_y, crtc_w, crtc_h,
732 src_x, src_y, src_w, src_h);
736 plane->old_fb = plane->fb;
737 ret = plane->funcs->update_plane(plane, crtc, fb,
738 crtc_x, crtc_y, crtc_w, crtc_h,
739 src_x, src_y, src_w, src_h, ctx);
743 drm_framebuffer_get(plane->fb);
745 plane->old_fb = NULL;
750 drm_framebuffer_put(plane->old_fb);
751 plane->old_fb = NULL;
756 static int __setplane_atomic(struct drm_plane *plane,
757 struct drm_crtc *crtc,
758 struct drm_framebuffer *fb,
759 int32_t crtc_x, int32_t crtc_y,
760 uint32_t crtc_w, uint32_t crtc_h,
761 uint32_t src_x, uint32_t src_y,
762 uint32_t src_w, uint32_t src_h,
763 struct drm_modeset_acquire_ctx *ctx)
767 WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
769 /* No fb means shut it down */
771 return plane->funcs->disable_plane(plane, ctx);
774 * FIXME: This is redundant with drm_atomic_plane_check(),
775 * but the legacy cursor/"async" .update_plane() tricks
776 * don't call that so we still need this here. Should remove
777 * this when all .update_plane() implementations have been
778 * fixed to call drm_atomic_plane_check().
780 ret = __setplane_check(plane, crtc, fb,
781 crtc_x, crtc_y, crtc_w, crtc_h,
782 src_x, src_y, src_w, src_h);
786 return plane->funcs->update_plane(plane, crtc, fb,
787 crtc_x, crtc_y, crtc_w, crtc_h,
788 src_x, src_y, src_w, src_h, ctx);
791 static int setplane_internal(struct drm_plane *plane,
792 struct drm_crtc *crtc,
793 struct drm_framebuffer *fb,
794 int32_t crtc_x, int32_t crtc_y,
795 uint32_t crtc_w, uint32_t crtc_h,
796 /* src_{x,y,w,h} values are 16.16 fixed point */
797 uint32_t src_x, uint32_t src_y,
798 uint32_t src_w, uint32_t src_h)
800 struct drm_modeset_acquire_ctx ctx;
803 DRM_MODESET_LOCK_ALL_BEGIN(plane->dev, ctx,
804 DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
806 if (drm_drv_uses_atomic_modeset(plane->dev))
807 ret = __setplane_atomic(plane, crtc, fb,
808 crtc_x, crtc_y, crtc_w, crtc_h,
809 src_x, src_y, src_w, src_h, &ctx);
811 ret = __setplane_internal(plane, crtc, fb,
812 crtc_x, crtc_y, crtc_w, crtc_h,
813 src_x, src_y, src_w, src_h, &ctx);
815 DRM_MODESET_LOCK_ALL_END(plane->dev, ctx, ret);
820 int drm_mode_setplane(struct drm_device *dev, void *data,
821 struct drm_file *file_priv)
823 struct drm_mode_set_plane *plane_req = data;
824 struct drm_plane *plane;
825 struct drm_crtc *crtc = NULL;
826 struct drm_framebuffer *fb = NULL;
829 if (!drm_core_check_feature(dev, DRIVER_MODESET))
833 * First, find the plane, crtc, and fb objects. If not available,
834 * we don't bother to call the driver.
836 plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
838 DRM_DEBUG_KMS("Unknown plane ID %d\n",
839 plane_req->plane_id);
843 if (plane_req->fb_id) {
844 fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
846 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
851 crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
853 drm_framebuffer_put(fb);
854 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
860 ret = setplane_internal(plane, crtc, fb,
861 plane_req->crtc_x, plane_req->crtc_y,
862 plane_req->crtc_w, plane_req->crtc_h,
863 plane_req->src_x, plane_req->src_y,
864 plane_req->src_w, plane_req->src_h);
867 drm_framebuffer_put(fb);
872 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
873 struct drm_mode_cursor2 *req,
874 struct drm_file *file_priv,
875 struct drm_modeset_acquire_ctx *ctx)
877 struct drm_device *dev = crtc->dev;
878 struct drm_plane *plane = crtc->cursor;
879 struct drm_framebuffer *fb = NULL;
880 struct drm_mode_fb_cmd2 fbreq = {
882 .height = req->height,
883 .pixel_format = DRM_FORMAT_ARGB8888,
884 .pitches = { req->width * 4 },
885 .handles = { req->handle },
887 int32_t crtc_x, crtc_y;
888 uint32_t crtc_w = 0, crtc_h = 0;
889 uint32_t src_w = 0, src_h = 0;
893 WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
896 * Obtain fb we'll be using (either new or existing) and take an extra
897 * reference to it if fb != null. setplane will take care of dropping
898 * the reference if the plane update fails.
900 if (req->flags & DRM_MODE_CURSOR_BO) {
902 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
904 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
908 fb->hot_x = req->hot_x;
909 fb->hot_y = req->hot_y;
915 fb = plane->state->fb;
920 drm_framebuffer_get(fb);
923 if (req->flags & DRM_MODE_CURSOR_MOVE) {
927 crtc_x = crtc->cursor_x;
928 crtc_y = crtc->cursor_y;
934 src_w = fb->width << 16;
935 src_h = fb->height << 16;
938 if (drm_drv_uses_atomic_modeset(dev))
939 ret = __setplane_atomic(plane, crtc, fb,
940 crtc_x, crtc_y, crtc_w, crtc_h,
941 0, 0, src_w, src_h, ctx);
943 ret = __setplane_internal(plane, crtc, fb,
944 crtc_x, crtc_y, crtc_w, crtc_h,
945 0, 0, src_w, src_h, ctx);
948 drm_framebuffer_put(fb);
950 /* Update successful; save new cursor position, if necessary */
951 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
952 crtc->cursor_x = req->x;
953 crtc->cursor_y = req->y;
959 static int drm_mode_cursor_common(struct drm_device *dev,
960 struct drm_mode_cursor2 *req,
961 struct drm_file *file_priv)
963 struct drm_crtc *crtc;
964 struct drm_modeset_acquire_ctx ctx;
967 if (!drm_core_check_feature(dev, DRIVER_MODESET))
970 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
973 crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
975 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
979 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
981 ret = drm_modeset_lock(&crtc->mutex, &ctx);
985 * If this crtc has a universal cursor plane, call that plane's update
986 * handler rather than using legacy cursor handlers.
989 ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
993 if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
998 ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
1002 if (req->flags & DRM_MODE_CURSOR_BO) {
1003 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1007 /* Turns off the cursor if handle is 0 */
1008 if (crtc->funcs->cursor_set2)
1009 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1010 req->width, req->height, req->hot_x, req->hot_y);
1012 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1013 req->width, req->height);
1016 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1017 if (crtc->funcs->cursor_move) {
1018 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1025 if (ret == -EDEADLK) {
1026 ret = drm_modeset_backoff(&ctx);
1031 drm_modeset_drop_locks(&ctx);
1032 drm_modeset_acquire_fini(&ctx);
1039 int drm_mode_cursor_ioctl(struct drm_device *dev,
1040 void *data, struct drm_file *file_priv)
1042 struct drm_mode_cursor *req = data;
1043 struct drm_mode_cursor2 new_req;
1045 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1046 new_req.hot_x = new_req.hot_y = 0;
1048 return drm_mode_cursor_common(dev, &new_req, file_priv);
1052 * Set the cursor configuration based on user request. This implements the 2nd
1053 * version of the cursor ioctl, which allows userspace to additionally specify
1054 * the hotspot of the pointer.
1056 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1057 void *data, struct drm_file *file_priv)
1059 struct drm_mode_cursor2 *req = data;
1061 return drm_mode_cursor_common(dev, req, file_priv);
1064 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1065 void *data, struct drm_file *file_priv)
1067 struct drm_mode_crtc_page_flip_target *page_flip = data;
1068 struct drm_crtc *crtc;
1069 struct drm_plane *plane;
1070 struct drm_framebuffer *fb = NULL, *old_fb;
1071 struct drm_pending_vblank_event *e = NULL;
1072 u32 target_vblank = page_flip->sequence;
1073 struct drm_modeset_acquire_ctx ctx;
1076 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1079 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1082 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1085 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1088 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1091 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1094 crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
1098 plane = crtc->primary;
1100 if (!drm_lease_held(file_priv, plane->base.id))
1103 if (crtc->funcs->page_flip_target) {
1107 r = drm_crtc_vblank_get(crtc);
1111 current_vblank = (u32)drm_crtc_vblank_count(crtc);
1113 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1114 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1115 if ((int)(target_vblank - current_vblank) > 1) {
1116 DRM_DEBUG("Invalid absolute flip target %u, "
1117 "must be <= %u\n", target_vblank,
1118 current_vblank + 1);
1119 drm_crtc_vblank_put(crtc);
1123 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1124 if (target_vblank != 0 && target_vblank != 1) {
1125 DRM_DEBUG("Invalid relative flip target %u, "
1126 "must be 0 or 1\n", target_vblank);
1127 drm_crtc_vblank_put(crtc);
1130 target_vblank += current_vblank;
1133 target_vblank = current_vblank +
1134 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1137 } else if (crtc->funcs->page_flip == NULL ||
1138 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1142 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1144 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1147 ret = drm_modeset_lock(&plane->mutex, &ctx);
1152 old_fb = plane->state->fb;
1156 if (old_fb == NULL) {
1157 /* The framebuffer is currently unbound, presumably
1158 * due to a hotplug event, that userspace has not
1165 fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
1172 const struct drm_plane_state *state = plane->state;
1174 ret = drm_framebuffer_check_src_coords(state->src_x,
1180 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
1186 if (old_fb->format != fb->format) {
1187 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
1192 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1193 e = kzalloc(sizeof *e, GFP_KERNEL);
1199 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1200 e->event.base.length = sizeof(e->event);
1201 e->event.vbl.user_data = page_flip->user_data;
1202 e->event.vbl.crtc_id = crtc->base.id;
1204 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1212 plane->old_fb = plane->fb;
1213 if (crtc->funcs->page_flip_target)
1214 ret = crtc->funcs->page_flip_target(crtc, fb, e,
1219 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
1222 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
1223 drm_event_cancel_free(dev, &e->base);
1224 /* Keep the old fb, don't unref it. */
1225 plane->old_fb = NULL;
1227 if (!plane->state) {
1229 drm_framebuffer_get(fb);
1235 drm_framebuffer_put(fb);
1237 drm_framebuffer_put(plane->old_fb);
1238 plane->old_fb = NULL;
1240 if (ret == -EDEADLK) {
1241 ret = drm_modeset_backoff(&ctx);
1246 drm_modeset_drop_locks(&ctx);
1247 drm_modeset_acquire_fini(&ctx);
1249 if (ret && crtc->funcs->page_flip_target)
1250 drm_crtc_vblank_put(crtc);
1255 struct drm_property *
1256 drm_create_scaling_filter_prop(struct drm_device *dev,
1257 unsigned int supported_filters)
1259 struct drm_property *prop;
1260 static const struct drm_prop_enum_list props[] = {
1261 { DRM_SCALING_FILTER_DEFAULT, "Default" },
1262 { DRM_SCALING_FILTER_NEAREST_NEIGHBOR, "Nearest Neighbor" },
1264 unsigned int valid_mode_mask = BIT(DRM_SCALING_FILTER_DEFAULT) |
1265 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR);
1268 if (WARN_ON((supported_filters & ~valid_mode_mask) ||
1269 ((supported_filters & BIT(DRM_SCALING_FILTER_DEFAULT)) == 0)))
1270 return ERR_PTR(-EINVAL);
1272 prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
1274 hweight32(supported_filters));
1276 return ERR_PTR(-ENOMEM);
1278 for (i = 0; i < ARRAY_SIZE(props); i++) {
1281 if (!(BIT(props[i].type) & supported_filters))
1284 ret = drm_property_add_enum(prop, props[i].type,
1288 drm_property_destroy(dev, prop);
1290 return ERR_PTR(ret);
1298 * drm_plane_create_scaling_filter_property - create a new scaling filter
1302 * @supported_filters: bitmask of supported scaling filters, must include
1303 * BIT(DRM_SCALING_FILTER_DEFAULT).
1305 * This function lets driver to enable the scaling filter property on a given
1309 * Zero for success or -errno
1311 int drm_plane_create_scaling_filter_property(struct drm_plane *plane,
1312 unsigned int supported_filters)
1314 struct drm_property *prop =
1315 drm_create_scaling_filter_prop(plane->dev, supported_filters);
1318 return PTR_ERR(prop);
1320 drm_object_attach_property(&plane->base, prop,
1321 DRM_SCALING_FILTER_DEFAULT);
1322 plane->scaling_filter_property = prop;
1326 EXPORT_SYMBOL(drm_plane_create_scaling_filter_property);