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_managed.h>
34 #include <drm/drm_vblank.h>
36 #include "drm_crtc_internal.h"
41 * A plane represents an image source that can be blended with or overlayed on
42 * top of a CRTC during the scanout process. Planes take their input data from a
43 * &drm_framebuffer object. The plane itself specifies the cropping and scaling
44 * of that image, and where it is placed on the visible area of a display
45 * pipeline, represented by &drm_crtc. A plane can also have additional
46 * properties that specify how the pixels are positioned and blended, like
47 * rotation or Z-position. All these properties are stored in &drm_plane_state.
49 * To create a plane, a KMS drivers allocates and zeroes an instances of
50 * &struct drm_plane (possibly as part of a larger structure) and registers it
51 * with a call to drm_universal_plane_init().
53 * The type of a plane is exposed in the immutable "type" enumeration property,
54 * which has one of the following values: "Overlay", "Primary", "Cursor" (see
55 * enum drm_plane_type). A plane can be compatible with multiple CRTCs, see
56 * &drm_plane.possible_crtcs.
58 * Each CRTC must have a unique primary plane userspace can attach to enable
59 * the CRTC. In other words, userspace must be able to attach a different
60 * primary plane to each CRTC at the same time. Primary planes can still be
61 * compatible with multiple CRTCs. There must be exactly as many primary planes
64 * Legacy uAPI doesn't expose the primary and cursor planes directly. DRM core
65 * relies on the driver to set the primary and optionally the cursor plane used
66 * for legacy IOCTLs. This is done by calling drm_crtc_init_with_planes(). All
67 * drivers must provide one primary plane per CRTC to avoid surprising legacy
72 * DOC: standard plane properties
74 * DRM planes have a few standardized properties:
77 * Blob property which contains the set of buffer format and modifier
78 * pairs supported by this plane. The blob is a struct
79 * drm_format_modifier_blob. Without this property the plane doesn't
80 * support buffers with modifiers. Userspace cannot change this property.
83 static unsigned int drm_num_planes(struct drm_device *dev)
86 struct drm_plane *tmp;
88 drm_for_each_plane(tmp, dev) {
96 formats_ptr(struct drm_format_modifier_blob *blob)
98 return (u32 *)(((char *)blob) + blob->formats_offset);
101 static inline struct drm_format_modifier *
102 modifiers_ptr(struct drm_format_modifier_blob *blob)
104 return (struct drm_format_modifier *)(((char *)blob) + blob->modifiers_offset);
107 static int create_in_format_blob(struct drm_device *dev, struct drm_plane *plane)
109 const struct drm_mode_config *config = &dev->mode_config;
110 struct drm_property_blob *blob;
111 struct drm_format_modifier *mod;
112 size_t blob_size, formats_size, modifiers_size;
113 struct drm_format_modifier_blob *blob_data;
116 formats_size = sizeof(__u32) * plane->format_count;
117 if (WARN_ON(!formats_size)) {
118 /* 0 formats are never expected */
123 sizeof(struct drm_format_modifier) * plane->modifier_count;
125 blob_size = sizeof(struct drm_format_modifier_blob);
126 /* Modifiers offset is a pointer to a struct with a 64 bit field so it
127 * should be naturally aligned to 8B.
129 BUILD_BUG_ON(sizeof(struct drm_format_modifier_blob) % 8);
130 blob_size += ALIGN(formats_size, 8);
131 blob_size += modifiers_size;
133 blob = drm_property_create_blob(dev, blob_size, NULL);
137 blob_data = blob->data;
138 blob_data->version = FORMAT_BLOB_CURRENT;
139 blob_data->count_formats = plane->format_count;
140 blob_data->formats_offset = sizeof(struct drm_format_modifier_blob);
141 blob_data->count_modifiers = plane->modifier_count;
143 blob_data->modifiers_offset =
144 ALIGN(blob_data->formats_offset + formats_size, 8);
146 memcpy(formats_ptr(blob_data), plane->format_types, formats_size);
148 /* If we can't determine support, just bail */
149 if (!plane->funcs->format_mod_supported)
152 mod = modifiers_ptr(blob_data);
153 for (i = 0; i < plane->modifier_count; i++) {
154 for (j = 0; j < plane->format_count; j++) {
155 if (plane->funcs->format_mod_supported(plane,
156 plane->format_types[j],
157 plane->modifiers[i])) {
159 mod->formats |= 1ULL << j;
163 mod->modifier = plane->modifiers[i];
170 drm_object_attach_property(&plane->base, config->modifiers_property,
177 static int __drm_universal_plane_init(struct drm_device *dev,
178 struct drm_plane *plane,
179 uint32_t possible_crtcs,
180 const struct drm_plane_funcs *funcs,
181 const uint32_t *formats,
182 unsigned int format_count,
183 const uint64_t *format_modifiers,
184 enum drm_plane_type type,
185 const char *name, va_list ap)
187 struct drm_mode_config *config = &dev->mode_config;
188 unsigned int format_modifier_count = 0;
191 /* plane index is used with 32bit bitmasks */
192 if (WARN_ON(config->num_total_plane >= 32))
195 WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
196 (!funcs->atomic_destroy_state ||
197 !funcs->atomic_duplicate_state));
199 ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
203 drm_modeset_lock_init(&plane->mutex);
205 plane->base.properties = &plane->properties;
207 plane->funcs = funcs;
208 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
210 if (!plane->format_types) {
211 DRM_DEBUG_KMS("out of memory when allocating plane\n");
212 drm_mode_object_unregister(dev, &plane->base);
217 * First driver to need more than 64 formats needs to fix this. Each
218 * format is encoded as a bit and the current code only supports a u64.
220 if (WARN_ON(format_count > 64))
223 if (format_modifiers) {
224 const uint64_t *temp_modifiers = format_modifiers;
226 while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
227 format_modifier_count++;
230 if (format_modifier_count)
231 config->allow_fb_modifiers = true;
233 plane->modifier_count = format_modifier_count;
234 plane->modifiers = kmalloc_array(format_modifier_count,
235 sizeof(format_modifiers[0]),
238 if (format_modifier_count && !plane->modifiers) {
239 DRM_DEBUG_KMS("out of memory when allocating plane\n");
240 kfree(plane->format_types);
241 drm_mode_object_unregister(dev, &plane->base);
246 plane->name = kvasprintf(GFP_KERNEL, name, ap);
248 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
249 drm_num_planes(dev));
252 kfree(plane->format_types);
253 kfree(plane->modifiers);
254 drm_mode_object_unregister(dev, &plane->base);
258 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
259 plane->format_count = format_count;
260 memcpy(plane->modifiers, format_modifiers,
261 format_modifier_count * sizeof(format_modifiers[0]));
262 plane->possible_crtcs = possible_crtcs;
265 list_add_tail(&plane->head, &config->plane_list);
266 plane->index = config->num_total_plane++;
268 drm_object_attach_property(&plane->base,
269 config->plane_type_property,
272 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
273 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
274 drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
275 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
276 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
277 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
278 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
279 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
280 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
281 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
282 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
283 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
286 if (config->allow_fb_modifiers)
287 create_in_format_blob(dev, plane);
293 * drm_universal_plane_init - Initialize a new universal plane object
295 * @plane: plane object to init
296 * @possible_crtcs: bitmask of possible CRTCs
297 * @funcs: callbacks for the new plane
298 * @formats: array of supported formats (DRM_FORMAT\_\*)
299 * @format_count: number of elements in @formats
300 * @format_modifiers: array of struct drm_format modifiers terminated by
301 * DRM_FORMAT_MOD_INVALID
302 * @type: type of plane (overlay, primary, cursor)
303 * @name: printf style format string for the plane name, or NULL for default name
305 * Initializes a plane object of type @type. The &drm_plane_funcs.destroy hook
306 * should call drm_plane_cleanup() and kfree() the plane structure. The plane
307 * structure should not be allocated with devm_kzalloc().
309 * Note: consider using drmm_universal_plane_alloc() instead of
310 * drm_universal_plane_init() to let the DRM managed resource infrastructure
311 * take care of cleanup and deallocation.
314 * Zero on success, error code on failure.
316 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
317 uint32_t possible_crtcs,
318 const struct drm_plane_funcs *funcs,
319 const uint32_t *formats, unsigned int format_count,
320 const uint64_t *format_modifiers,
321 enum drm_plane_type type,
322 const char *name, ...)
327 WARN_ON(!funcs->destroy);
330 ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
331 formats, format_count, format_modifiers,
336 EXPORT_SYMBOL(drm_universal_plane_init);
338 static void drmm_universal_plane_alloc_release(struct drm_device *dev, void *ptr)
340 struct drm_plane *plane = ptr;
342 if (WARN_ON(!plane->dev))
345 drm_plane_cleanup(plane);
348 void *__drmm_universal_plane_alloc(struct drm_device *dev, size_t size,
349 size_t offset, uint32_t possible_crtcs,
350 const struct drm_plane_funcs *funcs,
351 const uint32_t *formats, unsigned int format_count,
352 const uint64_t *format_modifiers,
353 enum drm_plane_type type,
354 const char *name, ...)
357 struct drm_plane *plane;
361 if (WARN_ON(!funcs || funcs->destroy))
362 return ERR_PTR(-EINVAL);
364 container = drmm_kzalloc(dev, size, GFP_KERNEL);
366 return ERR_PTR(-ENOMEM);
368 plane = container + offset;
371 ret = __drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
372 formats, format_count, format_modifiers,
378 ret = drmm_add_action_or_reset(dev, drmm_universal_plane_alloc_release,
385 EXPORT_SYMBOL(__drmm_universal_plane_alloc);
387 int drm_plane_register_all(struct drm_device *dev)
389 unsigned int num_planes = 0;
390 unsigned int num_zpos = 0;
391 struct drm_plane *plane;
394 drm_for_each_plane(plane, dev) {
395 if (plane->funcs->late_register)
396 ret = plane->funcs->late_register(plane);
400 if (plane->zpos_property)
405 drm_WARN(dev, num_zpos && num_planes != num_zpos,
406 "Mixing planes with and without zpos property is invalid\n");
411 void drm_plane_unregister_all(struct drm_device *dev)
413 struct drm_plane *plane;
415 drm_for_each_plane(plane, dev) {
416 if (plane->funcs->early_unregister)
417 plane->funcs->early_unregister(plane);
422 * drm_plane_init - Initialize a legacy plane
424 * @plane: plane object to init
425 * @possible_crtcs: bitmask of possible CRTCs
426 * @funcs: callbacks for the new plane
427 * @formats: array of supported formats (DRM_FORMAT\_\*)
428 * @format_count: number of elements in @formats
429 * @is_primary: plane type (primary vs overlay)
431 * Legacy API to initialize a DRM plane.
433 * New drivers should call drm_universal_plane_init() instead.
436 * Zero on success, error code on failure.
438 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
439 uint32_t possible_crtcs,
440 const struct drm_plane_funcs *funcs,
441 const uint32_t *formats, unsigned int format_count,
444 enum drm_plane_type type;
446 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
447 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
448 formats, format_count,
451 EXPORT_SYMBOL(drm_plane_init);
454 * drm_plane_cleanup - Clean up the core plane usage
455 * @plane: plane to cleanup
457 * This function cleans up @plane and removes it from the DRM mode setting
458 * core. Note that the function does *not* free the plane structure itself,
459 * this is the responsibility of the caller.
461 void drm_plane_cleanup(struct drm_plane *plane)
463 struct drm_device *dev = plane->dev;
465 drm_modeset_lock_fini(&plane->mutex);
467 kfree(plane->format_types);
468 kfree(plane->modifiers);
469 drm_mode_object_unregister(dev, &plane->base);
471 BUG_ON(list_empty(&plane->head));
473 /* Note that the plane_list is considered to be static; should we
474 * remove the drm_plane at runtime we would have to decrement all
475 * the indices on the drm_plane after us in the plane_list.
478 list_del(&plane->head);
479 dev->mode_config.num_total_plane--;
481 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
482 if (plane->state && plane->funcs->atomic_destroy_state)
483 plane->funcs->atomic_destroy_state(plane, plane->state);
487 memset(plane, 0, sizeof(*plane));
489 EXPORT_SYMBOL(drm_plane_cleanup);
492 * drm_plane_from_index - find the registered plane at an index
494 * @idx: index of registered plane to find for
496 * Given a plane index, return the registered plane from DRM device's
497 * list of planes with matching index. This is the inverse of drm_plane_index().
500 drm_plane_from_index(struct drm_device *dev, int idx)
502 struct drm_plane *plane;
504 drm_for_each_plane(plane, dev)
505 if (idx == plane->index)
510 EXPORT_SYMBOL(drm_plane_from_index);
513 * drm_plane_force_disable - Forcibly disable a plane
514 * @plane: plane to disable
516 * Forces the plane to be disabled.
518 * Used when the plane's current framebuffer is destroyed,
519 * and when restoring fbdev mode.
521 * Note that this function is not suitable for atomic drivers, since it doesn't
522 * wire through the lock acquisition context properly and hence can't handle
523 * retries or driver private locks. You probably want to use
524 * drm_atomic_helper_disable_plane() or
525 * drm_atomic_helper_disable_planes_on_crtc() instead.
527 void drm_plane_force_disable(struct drm_plane *plane)
534 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
536 plane->old_fb = plane->fb;
537 ret = plane->funcs->disable_plane(plane, NULL);
539 DRM_ERROR("failed to disable plane with busy fb\n");
540 plane->old_fb = NULL;
543 /* disconnect the plane from the fb and crtc: */
544 drm_framebuffer_put(plane->old_fb);
545 plane->old_fb = NULL;
549 EXPORT_SYMBOL(drm_plane_force_disable);
552 * drm_mode_plane_set_obj_prop - set the value of a property
553 * @plane: drm plane object to set property value for
554 * @property: property to set
555 * @value: value the property should be set to
557 * This functions sets a given property on a given plane object. This function
558 * calls the driver's ->set_property callback and changes the software state of
559 * the property if the callback succeeds.
562 * Zero on success, error code on failure.
564 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
565 struct drm_property *property,
569 struct drm_mode_object *obj = &plane->base;
571 if (plane->funcs->set_property)
572 ret = plane->funcs->set_property(plane, property, value);
574 drm_object_property_set_value(obj, property, value);
578 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
580 int drm_mode_getplane_res(struct drm_device *dev, void *data,
581 struct drm_file *file_priv)
583 struct drm_mode_get_plane_res *plane_resp = data;
584 struct drm_plane *plane;
585 uint32_t __user *plane_ptr;
588 if (!drm_core_check_feature(dev, DRIVER_MODESET))
591 plane_ptr = u64_to_user_ptr(plane_resp->plane_id_ptr);
594 * This ioctl is called twice, once to determine how much space is
595 * needed, and the 2nd time to fill it.
597 drm_for_each_plane(plane, dev) {
599 * Unless userspace set the 'universal planes'
600 * capability bit, only advertise overlays.
602 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
603 !file_priv->universal_planes)
606 if (drm_lease_held(file_priv, plane->base.id)) {
607 if (count < plane_resp->count_planes &&
608 put_user(plane->base.id, plane_ptr + count))
613 plane_resp->count_planes = count;
618 int drm_mode_getplane(struct drm_device *dev, void *data,
619 struct drm_file *file_priv)
621 struct drm_mode_get_plane *plane_resp = data;
622 struct drm_plane *plane;
623 uint32_t __user *format_ptr;
625 if (!drm_core_check_feature(dev, DRIVER_MODESET))
628 plane = drm_plane_find(dev, file_priv, plane_resp->plane_id);
632 drm_modeset_lock(&plane->mutex, NULL);
633 if (plane->state && plane->state->crtc && drm_lease_held(file_priv, plane->state->crtc->base.id))
634 plane_resp->crtc_id = plane->state->crtc->base.id;
635 else if (!plane->state && plane->crtc && drm_lease_held(file_priv, plane->crtc->base.id))
636 plane_resp->crtc_id = plane->crtc->base.id;
638 plane_resp->crtc_id = 0;
640 if (plane->state && plane->state->fb)
641 plane_resp->fb_id = plane->state->fb->base.id;
642 else if (!plane->state && plane->fb)
643 plane_resp->fb_id = plane->fb->base.id;
645 plane_resp->fb_id = 0;
646 drm_modeset_unlock(&plane->mutex);
648 plane_resp->plane_id = plane->base.id;
649 plane_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,
650 plane->possible_crtcs);
652 plane_resp->gamma_size = 0;
655 * This ioctl is called twice, once to determine how much space is
656 * needed, and the 2nd time to fill it.
658 if (plane->format_count &&
659 (plane_resp->count_format_types >= plane->format_count)) {
660 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
661 if (copy_to_user(format_ptr,
663 sizeof(uint32_t) * plane->format_count)) {
667 plane_resp->count_format_types = plane->format_count;
672 int drm_plane_check_pixel_format(struct drm_plane *plane,
673 u32 format, u64 modifier)
677 for (i = 0; i < plane->format_count; i++) {
678 if (format == plane->format_types[i])
681 if (i == plane->format_count)
684 if (plane->funcs->format_mod_supported) {
685 if (!plane->funcs->format_mod_supported(plane, format, modifier))
688 if (!plane->modifier_count)
691 for (i = 0; i < plane->modifier_count; i++) {
692 if (modifier == plane->modifiers[i])
695 if (i == plane->modifier_count)
702 static int __setplane_check(struct drm_plane *plane,
703 struct drm_crtc *crtc,
704 struct drm_framebuffer *fb,
705 int32_t crtc_x, int32_t crtc_y,
706 uint32_t crtc_w, uint32_t crtc_h,
707 uint32_t src_x, uint32_t src_y,
708 uint32_t src_w, uint32_t src_h)
712 /* Check whether this plane is usable on this CRTC */
713 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
714 DRM_DEBUG_KMS("Invalid crtc for plane\n");
718 /* Check whether this plane supports the fb pixel format. */
719 ret = drm_plane_check_pixel_format(plane, fb->format->format,
722 struct drm_format_name_buf format_name;
724 DRM_DEBUG_KMS("Invalid pixel format %s, modifier 0x%llx\n",
725 drm_get_format_name(fb->format->format,
731 /* Give drivers some help against integer overflows */
732 if (crtc_w > INT_MAX ||
733 crtc_x > INT_MAX - (int32_t) crtc_w ||
735 crtc_y > INT_MAX - (int32_t) crtc_h) {
736 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
737 crtc_w, crtc_h, crtc_x, crtc_y);
741 ret = drm_framebuffer_check_src_coords(src_x, src_y, src_w, src_h, fb);
749 * drm_any_plane_has_format - Check whether any plane supports this format and modifier combination
751 * @format: pixel format (DRM_FORMAT_*)
752 * @modifier: data layout modifier
755 * Whether at least one plane supports the specified format and modifier combination.
757 bool drm_any_plane_has_format(struct drm_device *dev,
758 u32 format, u64 modifier)
760 struct drm_plane *plane;
762 drm_for_each_plane(plane, dev) {
763 if (drm_plane_check_pixel_format(plane, format, modifier) == 0)
769 EXPORT_SYMBOL(drm_any_plane_has_format);
772 * __setplane_internal - setplane handler for internal callers
774 * This function will take a reference on the new fb for the plane
777 * src_{x,y,w,h} are provided in 16.16 fixed point format
779 static int __setplane_internal(struct drm_plane *plane,
780 struct drm_crtc *crtc,
781 struct drm_framebuffer *fb,
782 int32_t crtc_x, int32_t crtc_y,
783 uint32_t crtc_w, uint32_t crtc_h,
784 /* src_{x,y,w,h} values are 16.16 fixed point */
785 uint32_t src_x, uint32_t src_y,
786 uint32_t src_w, uint32_t src_h,
787 struct drm_modeset_acquire_ctx *ctx)
791 WARN_ON(drm_drv_uses_atomic_modeset(plane->dev));
793 /* No fb means shut it down */
795 plane->old_fb = plane->fb;
796 ret = plane->funcs->disable_plane(plane, ctx);
801 plane->old_fb = NULL;
806 ret = __setplane_check(plane, crtc, fb,
807 crtc_x, crtc_y, crtc_w, crtc_h,
808 src_x, src_y, src_w, src_h);
812 plane->old_fb = plane->fb;
813 ret = plane->funcs->update_plane(plane, crtc, fb,
814 crtc_x, crtc_y, crtc_w, crtc_h,
815 src_x, src_y, src_w, src_h, ctx);
819 drm_framebuffer_get(plane->fb);
821 plane->old_fb = NULL;
826 drm_framebuffer_put(plane->old_fb);
827 plane->old_fb = NULL;
832 static int __setplane_atomic(struct drm_plane *plane,
833 struct drm_crtc *crtc,
834 struct drm_framebuffer *fb,
835 int32_t crtc_x, int32_t crtc_y,
836 uint32_t crtc_w, uint32_t crtc_h,
837 uint32_t src_x, uint32_t src_y,
838 uint32_t src_w, uint32_t src_h,
839 struct drm_modeset_acquire_ctx *ctx)
843 WARN_ON(!drm_drv_uses_atomic_modeset(plane->dev));
845 /* No fb means shut it down */
847 return plane->funcs->disable_plane(plane, ctx);
850 * FIXME: This is redundant with drm_atomic_plane_check(),
851 * but the legacy cursor/"async" .update_plane() tricks
852 * don't call that so we still need this here. Should remove
853 * this when all .update_plane() implementations have been
854 * fixed to call drm_atomic_plane_check().
856 ret = __setplane_check(plane, crtc, fb,
857 crtc_x, crtc_y, crtc_w, crtc_h,
858 src_x, src_y, src_w, src_h);
862 return plane->funcs->update_plane(plane, crtc, fb,
863 crtc_x, crtc_y, crtc_w, crtc_h,
864 src_x, src_y, src_w, src_h, ctx);
867 static int setplane_internal(struct drm_plane *plane,
868 struct drm_crtc *crtc,
869 struct drm_framebuffer *fb,
870 int32_t crtc_x, int32_t crtc_y,
871 uint32_t crtc_w, uint32_t crtc_h,
872 /* src_{x,y,w,h} values are 16.16 fixed point */
873 uint32_t src_x, uint32_t src_y,
874 uint32_t src_w, uint32_t src_h)
876 struct drm_modeset_acquire_ctx ctx;
879 DRM_MODESET_LOCK_ALL_BEGIN(plane->dev, ctx,
880 DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
882 if (drm_drv_uses_atomic_modeset(plane->dev))
883 ret = __setplane_atomic(plane, crtc, fb,
884 crtc_x, crtc_y, crtc_w, crtc_h,
885 src_x, src_y, src_w, src_h, &ctx);
887 ret = __setplane_internal(plane, crtc, fb,
888 crtc_x, crtc_y, crtc_w, crtc_h,
889 src_x, src_y, src_w, src_h, &ctx);
891 DRM_MODESET_LOCK_ALL_END(plane->dev, ctx, ret);
896 int drm_mode_setplane(struct drm_device *dev, void *data,
897 struct drm_file *file_priv)
899 struct drm_mode_set_plane *plane_req = data;
900 struct drm_plane *plane;
901 struct drm_crtc *crtc = NULL;
902 struct drm_framebuffer *fb = NULL;
905 if (!drm_core_check_feature(dev, DRIVER_MODESET))
909 * First, find the plane, crtc, and fb objects. If not available,
910 * we don't bother to call the driver.
912 plane = drm_plane_find(dev, file_priv, plane_req->plane_id);
914 DRM_DEBUG_KMS("Unknown plane ID %d\n",
915 plane_req->plane_id);
919 if (plane_req->fb_id) {
920 fb = drm_framebuffer_lookup(dev, file_priv, plane_req->fb_id);
922 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
927 crtc = drm_crtc_find(dev, file_priv, plane_req->crtc_id);
929 drm_framebuffer_put(fb);
930 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
936 ret = setplane_internal(plane, crtc, fb,
937 plane_req->crtc_x, plane_req->crtc_y,
938 plane_req->crtc_w, plane_req->crtc_h,
939 plane_req->src_x, plane_req->src_y,
940 plane_req->src_w, plane_req->src_h);
943 drm_framebuffer_put(fb);
948 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
949 struct drm_mode_cursor2 *req,
950 struct drm_file *file_priv,
951 struct drm_modeset_acquire_ctx *ctx)
953 struct drm_device *dev = crtc->dev;
954 struct drm_plane *plane = crtc->cursor;
955 struct drm_framebuffer *fb = NULL;
956 struct drm_mode_fb_cmd2 fbreq = {
958 .height = req->height,
959 .pixel_format = DRM_FORMAT_ARGB8888,
960 .pitches = { req->width * 4 },
961 .handles = { req->handle },
963 int32_t crtc_x, crtc_y;
964 uint32_t crtc_w = 0, crtc_h = 0;
965 uint32_t src_w = 0, src_h = 0;
969 WARN_ON(plane->crtc != crtc && plane->crtc != NULL);
972 * Obtain fb we'll be using (either new or existing) and take an extra
973 * reference to it if fb != null. setplane will take care of dropping
974 * the reference if the plane update fails.
976 if (req->flags & DRM_MODE_CURSOR_BO) {
978 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
980 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
984 fb->hot_x = req->hot_x;
985 fb->hot_y = req->hot_y;
991 fb = plane->state->fb;
996 drm_framebuffer_get(fb);
999 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1003 crtc_x = crtc->cursor_x;
1004 crtc_y = crtc->cursor_y;
1009 crtc_h = fb->height;
1010 src_w = fb->width << 16;
1011 src_h = fb->height << 16;
1014 if (drm_drv_uses_atomic_modeset(dev))
1015 ret = __setplane_atomic(plane, crtc, fb,
1016 crtc_x, crtc_y, crtc_w, crtc_h,
1017 0, 0, src_w, src_h, ctx);
1019 ret = __setplane_internal(plane, crtc, fb,
1020 crtc_x, crtc_y, crtc_w, crtc_h,
1021 0, 0, src_w, src_h, ctx);
1024 drm_framebuffer_put(fb);
1026 /* Update successful; save new cursor position, if necessary */
1027 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1028 crtc->cursor_x = req->x;
1029 crtc->cursor_y = req->y;
1035 static int drm_mode_cursor_common(struct drm_device *dev,
1036 struct drm_mode_cursor2 *req,
1037 struct drm_file *file_priv)
1039 struct drm_crtc *crtc;
1040 struct drm_modeset_acquire_ctx ctx;
1043 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1046 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1049 crtc = drm_crtc_find(dev, file_priv, req->crtc_id);
1051 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1055 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1057 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1061 * If this crtc has a universal cursor plane, call that plane's update
1062 * handler rather than using legacy cursor handlers.
1065 ret = drm_modeset_lock(&crtc->cursor->mutex, &ctx);
1069 if (!drm_lease_held(file_priv, crtc->cursor->base.id)) {
1074 ret = drm_mode_cursor_universal(crtc, req, file_priv, &ctx);
1078 if (req->flags & DRM_MODE_CURSOR_BO) {
1079 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1083 /* Turns off the cursor if handle is 0 */
1084 if (crtc->funcs->cursor_set2)
1085 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1086 req->width, req->height, req->hot_x, req->hot_y);
1088 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1089 req->width, req->height);
1092 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1093 if (crtc->funcs->cursor_move) {
1094 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1101 if (ret == -EDEADLK) {
1102 ret = drm_modeset_backoff(&ctx);
1107 drm_modeset_drop_locks(&ctx);
1108 drm_modeset_acquire_fini(&ctx);
1115 int drm_mode_cursor_ioctl(struct drm_device *dev,
1116 void *data, struct drm_file *file_priv)
1118 struct drm_mode_cursor *req = data;
1119 struct drm_mode_cursor2 new_req;
1121 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1122 new_req.hot_x = new_req.hot_y = 0;
1124 return drm_mode_cursor_common(dev, &new_req, file_priv);
1128 * Set the cursor configuration based on user request. This implements the 2nd
1129 * version of the cursor ioctl, which allows userspace to additionally specify
1130 * the hotspot of the pointer.
1132 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1133 void *data, struct drm_file *file_priv)
1135 struct drm_mode_cursor2 *req = data;
1137 return drm_mode_cursor_common(dev, req, file_priv);
1140 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1141 void *data, struct drm_file *file_priv)
1143 struct drm_mode_crtc_page_flip_target *page_flip = data;
1144 struct drm_crtc *crtc;
1145 struct drm_plane *plane;
1146 struct drm_framebuffer *fb = NULL, *old_fb;
1147 struct drm_pending_vblank_event *e = NULL;
1148 u32 target_vblank = page_flip->sequence;
1149 struct drm_modeset_acquire_ctx ctx;
1152 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1155 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1158 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1161 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1164 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1167 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1170 crtc = drm_crtc_find(dev, file_priv, page_flip->crtc_id);
1174 plane = crtc->primary;
1176 if (!drm_lease_held(file_priv, plane->base.id))
1179 if (crtc->funcs->page_flip_target) {
1183 r = drm_crtc_vblank_get(crtc);
1187 current_vblank = (u32)drm_crtc_vblank_count(crtc);
1189 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1190 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1191 if ((int)(target_vblank - current_vblank) > 1) {
1192 DRM_DEBUG("Invalid absolute flip target %u, "
1193 "must be <= %u\n", target_vblank,
1194 current_vblank + 1);
1195 drm_crtc_vblank_put(crtc);
1199 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1200 if (target_vblank != 0 && target_vblank != 1) {
1201 DRM_DEBUG("Invalid relative flip target %u, "
1202 "must be 0 or 1\n", target_vblank);
1203 drm_crtc_vblank_put(crtc);
1206 target_vblank += current_vblank;
1209 target_vblank = current_vblank +
1210 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1213 } else if (crtc->funcs->page_flip == NULL ||
1214 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1218 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1220 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1223 ret = drm_modeset_lock(&plane->mutex, &ctx);
1228 old_fb = plane->state->fb;
1232 if (old_fb == NULL) {
1233 /* The framebuffer is currently unbound, presumably
1234 * due to a hotplug event, that userspace has not
1241 fb = drm_framebuffer_lookup(dev, file_priv, page_flip->fb_id);
1248 const struct drm_plane_state *state = plane->state;
1250 ret = drm_framebuffer_check_src_coords(state->src_x,
1256 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y,
1262 if (old_fb->format != fb->format) {
1263 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
1268 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1269 e = kzalloc(sizeof *e, GFP_KERNEL);
1275 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1276 e->event.base.length = sizeof(e->event);
1277 e->event.vbl.user_data = page_flip->user_data;
1278 e->event.vbl.crtc_id = crtc->base.id;
1280 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
1288 plane->old_fb = plane->fb;
1289 if (crtc->funcs->page_flip_target)
1290 ret = crtc->funcs->page_flip_target(crtc, fb, e,
1295 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags,
1298 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
1299 drm_event_cancel_free(dev, &e->base);
1300 /* Keep the old fb, don't unref it. */
1301 plane->old_fb = NULL;
1303 if (!plane->state) {
1305 drm_framebuffer_get(fb);
1311 drm_framebuffer_put(fb);
1313 drm_framebuffer_put(plane->old_fb);
1314 plane->old_fb = NULL;
1316 if (ret == -EDEADLK) {
1317 ret = drm_modeset_backoff(&ctx);
1322 drm_modeset_drop_locks(&ctx);
1323 drm_modeset_acquire_fini(&ctx);
1325 if (ret && crtc->funcs->page_flip_target)
1326 drm_crtc_vblank_put(crtc);
1331 struct drm_property *
1332 drm_create_scaling_filter_prop(struct drm_device *dev,
1333 unsigned int supported_filters)
1335 struct drm_property *prop;
1336 static const struct drm_prop_enum_list props[] = {
1337 { DRM_SCALING_FILTER_DEFAULT, "Default" },
1338 { DRM_SCALING_FILTER_NEAREST_NEIGHBOR, "Nearest Neighbor" },
1340 unsigned int valid_mode_mask = BIT(DRM_SCALING_FILTER_DEFAULT) |
1341 BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR);
1344 if (WARN_ON((supported_filters & ~valid_mode_mask) ||
1345 ((supported_filters & BIT(DRM_SCALING_FILTER_DEFAULT)) == 0)))
1346 return ERR_PTR(-EINVAL);
1348 prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
1350 hweight32(supported_filters));
1352 return ERR_PTR(-ENOMEM);
1354 for (i = 0; i < ARRAY_SIZE(props); i++) {
1357 if (!(BIT(props[i].type) & supported_filters))
1360 ret = drm_property_add_enum(prop, props[i].type,
1364 drm_property_destroy(dev, prop);
1366 return ERR_PTR(ret);
1374 * drm_plane_create_scaling_filter_property - create a new scaling filter
1378 * @supported_filters: bitmask of supported scaling filters, must include
1379 * BIT(DRM_SCALING_FILTER_DEFAULT).
1381 * This function lets driver to enable the scaling filter property on a given
1385 * Zero for success or -errno
1387 int drm_plane_create_scaling_filter_property(struct drm_plane *plane,
1388 unsigned int supported_filters)
1390 struct drm_property *prop =
1391 drm_create_scaling_filter_prop(plane->dev, supported_filters);
1394 return PTR_ERR(prop);
1396 drm_object_attach_property(&plane->base, prop,
1397 DRM_SCALING_FILTER_DEFAULT);
1398 plane->scaling_filter_property = prop;
1402 EXPORT_SYMBOL(drm_plane_create_scaling_filter_property);