2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_framebuffer.h>
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
51 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] = {
52 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
57 static const struct drm_prop_enum_list drm_encoder_enum_list
[] = {
58 { DRM_MODE_ENCODER_NONE
, "None" },
59 { DRM_MODE_ENCODER_DAC
, "DAC" },
60 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
61 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
62 { DRM_MODE_ENCODER_TVDAC
, "TV" },
63 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
64 { DRM_MODE_ENCODER_DSI
, "DSI" },
65 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
66 { DRM_MODE_ENCODER_DPI
, "DPI" },
73 * Internal function to assign a slot in the object idr and optionally
74 * register the object into the idr.
76 int drm_mode_object_get_reg(struct drm_device
*dev
,
77 struct drm_mode_object
*obj
,
80 void (*obj_free_cb
)(struct kref
*kref
))
84 mutex_lock(&dev
->mode_config
.idr_mutex
);
85 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
88 * Set up the object linking under the protection of the idr
89 * lock so that other users can't see inconsistent state.
94 obj
->free_cb
= obj_free_cb
;
95 kref_init(&obj
->refcount
);
98 mutex_unlock(&dev
->mode_config
.idr_mutex
);
100 return ret
< 0 ? ret
: 0;
104 * drm_mode_object_get - allocate a new modeset identifier
106 * @obj: object pointer, used to generate unique ID
107 * @obj_type: object type
109 * Create a unique identifier based on @ptr in @dev's identifier space. Used
110 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
111 * modeset identifiers are _not_ reference counted. Hence don't use this for
112 * reference counted modeset objects like framebuffers.
115 * Zero on success, error code on failure.
117 int drm_mode_object_get(struct drm_device
*dev
,
118 struct drm_mode_object
*obj
, uint32_t obj_type
)
120 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true, NULL
);
123 void drm_mode_object_register(struct drm_device
*dev
,
124 struct drm_mode_object
*obj
)
126 mutex_lock(&dev
->mode_config
.idr_mutex
);
127 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
128 mutex_unlock(&dev
->mode_config
.idr_mutex
);
132 * drm_mode_object_unregister - free a modeset identifer
134 * @object: object to free
136 * Free @id from @dev's unique identifier pool.
137 * This function can be called multiple times, and guards against
139 * These modeset identifiers are _not_ reference counted. Hence don't use this
140 * for reference counted modeset objects like framebuffers.
142 void drm_mode_object_unregister(struct drm_device
*dev
,
143 struct drm_mode_object
*object
)
145 mutex_lock(&dev
->mode_config
.idr_mutex
);
147 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
150 mutex_unlock(&dev
->mode_config
.idr_mutex
);
153 struct drm_mode_object
*__drm_mode_object_find(struct drm_device
*dev
,
154 uint32_t id
, uint32_t type
)
156 struct drm_mode_object
*obj
= NULL
;
158 mutex_lock(&dev
->mode_config
.idr_mutex
);
159 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
160 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
162 if (obj
&& obj
->id
!= id
)
165 if (obj
&& obj
->free_cb
) {
166 if (!kref_get_unless_zero(&obj
->refcount
))
169 mutex_unlock(&dev
->mode_config
.idr_mutex
);
175 * drm_mode_object_find - look up a drm object with static lifetime
177 * @id: id of the mode object
178 * @type: type of the mode object
180 * This function is used to look up a modeset object. It will acquire a
181 * reference for reference counted objects. This reference must be dropped again
182 * by callind drm_mode_object_unreference().
184 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
185 uint32_t id
, uint32_t type
)
187 struct drm_mode_object
*obj
= NULL
;
189 obj
= __drm_mode_object_find(dev
, id
, type
);
192 EXPORT_SYMBOL(drm_mode_object_find
);
195 * drm_mode_object_unreference - decr the object refcnt
198 * This functions decrements the object's refcount if it is a refcounted modeset
199 * object. It is a no-op on any other object. This is used to drop references
200 * acquired with drm_mode_object_reference().
202 void drm_mode_object_unreference(struct drm_mode_object
*obj
)
205 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
206 kref_put(&obj
->refcount
, obj
->free_cb
);
209 EXPORT_SYMBOL(drm_mode_object_unreference
);
212 * drm_mode_object_reference - incr the object refcnt
215 * This functions increments the object's refcount if it is a refcounted modeset
216 * object. It is a no-op on any other object. References should be dropped again
217 * by calling drm_mode_object_unreference().
219 void drm_mode_object_reference(struct drm_mode_object
*obj
)
222 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
223 kref_get(&obj
->refcount
);
226 EXPORT_SYMBOL(drm_mode_object_reference
);
229 * drm_crtc_force_disable - Forcibly turn off a CRTC
230 * @crtc: CRTC to turn off
233 * Zero on success, error code on failure.
235 int drm_crtc_force_disable(struct drm_crtc
*crtc
)
237 struct drm_mode_set set
= {
241 return drm_mode_set_config_internal(&set
);
243 EXPORT_SYMBOL(drm_crtc_force_disable
);
246 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
247 * @dev: DRM device whose CRTCs to turn off
249 * Drivers may want to call this on unload to ensure that all displays are
250 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
253 * Zero on success, error code on failure.
255 int drm_crtc_force_disable_all(struct drm_device
*dev
)
257 struct drm_crtc
*crtc
;
260 drm_modeset_lock_all(dev
);
261 drm_for_each_crtc(crtc
, dev
)
263 ret
= drm_crtc_force_disable(crtc
);
268 drm_modeset_unlock_all(dev
);
271 EXPORT_SYMBOL(drm_crtc_force_disable_all
);
273 DEFINE_WW_CLASS(crtc_ww_class
);
275 static unsigned int drm_num_crtcs(struct drm_device
*dev
)
277 unsigned int num
= 0;
278 struct drm_crtc
*tmp
;
280 drm_for_each_crtc(tmp
, dev
) {
287 static int drm_crtc_register_all(struct drm_device
*dev
)
289 struct drm_crtc
*crtc
;
292 drm_for_each_crtc(crtc
, dev
) {
293 if (crtc
->funcs
->late_register
)
294 ret
= crtc
->funcs
->late_register(crtc
);
302 static void drm_crtc_unregister_all(struct drm_device
*dev
)
304 struct drm_crtc
*crtc
;
306 drm_for_each_crtc(crtc
, dev
) {
307 if (crtc
->funcs
->early_unregister
)
308 crtc
->funcs
->early_unregister(crtc
);
313 * drm_crtc_init_with_planes - Initialise a new CRTC object with
314 * specified primary and cursor planes.
316 * @crtc: CRTC object to init
317 * @primary: Primary plane for CRTC
318 * @cursor: Cursor plane for CRTC
319 * @funcs: callbacks for the new CRTC
320 * @name: printf style format string for the CRTC name, or NULL for default name
322 * Inits a new object created as base part of a driver crtc object.
325 * Zero on success, error code on failure.
327 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
328 struct drm_plane
*primary
,
329 struct drm_plane
*cursor
,
330 const struct drm_crtc_funcs
*funcs
,
331 const char *name
, ...)
333 struct drm_mode_config
*config
= &dev
->mode_config
;
336 WARN_ON(primary
&& primary
->type
!= DRM_PLANE_TYPE_PRIMARY
);
337 WARN_ON(cursor
&& cursor
->type
!= DRM_PLANE_TYPE_CURSOR
);
342 INIT_LIST_HEAD(&crtc
->commit_list
);
343 spin_lock_init(&crtc
->commit_lock
);
345 drm_modeset_lock_init(&crtc
->mutex
);
346 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
354 crtc
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
357 crtc
->name
= kasprintf(GFP_KERNEL
, "crtc-%d",
361 drm_mode_object_unregister(dev
, &crtc
->base
);
365 crtc
->base
.properties
= &crtc
->properties
;
367 list_add_tail(&crtc
->head
, &config
->crtc_list
);
368 crtc
->index
= config
->num_crtc
++;
370 crtc
->primary
= primary
;
371 crtc
->cursor
= cursor
;
373 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
375 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
377 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
378 drm_object_attach_property(&crtc
->base
, config
->prop_active
, 0);
379 drm_object_attach_property(&crtc
->base
, config
->prop_mode_id
, 0);
384 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
387 * drm_crtc_cleanup - Clean up the core crtc usage
388 * @crtc: CRTC to cleanup
390 * This function cleans up @crtc and removes it from the DRM mode setting
391 * core. Note that the function does *not* free the crtc structure itself,
392 * this is the responsibility of the caller.
394 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
396 struct drm_device
*dev
= crtc
->dev
;
398 /* Note that the crtc_list is considered to be static; should we
399 * remove the drm_crtc at runtime we would have to decrement all
400 * the indices on the drm_crtc after us in the crtc_list.
403 kfree(crtc
->gamma_store
);
404 crtc
->gamma_store
= NULL
;
406 drm_modeset_lock_fini(&crtc
->mutex
);
408 drm_mode_object_unregister(dev
, &crtc
->base
);
409 list_del(&crtc
->head
);
410 dev
->mode_config
.num_crtc
--;
412 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
413 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
414 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
418 memset(crtc
, 0, sizeof(*crtc
));
420 EXPORT_SYMBOL(drm_crtc_cleanup
);
422 static int drm_encoder_register_all(struct drm_device
*dev
)
424 struct drm_encoder
*encoder
;
427 drm_for_each_encoder(encoder
, dev
) {
428 if (encoder
->funcs
->late_register
)
429 ret
= encoder
->funcs
->late_register(encoder
);
437 static void drm_encoder_unregister_all(struct drm_device
*dev
)
439 struct drm_encoder
*encoder
;
441 drm_for_each_encoder(encoder
, dev
) {
442 if (encoder
->funcs
->early_unregister
)
443 encoder
->funcs
->early_unregister(encoder
);
448 * drm_encoder_init - Init a preallocated encoder
450 * @encoder: the encoder to init
451 * @funcs: callbacks for this encoder
452 * @encoder_type: user visible type of the encoder
453 * @name: printf style format string for the encoder name, or NULL for default name
455 * Initialises a preallocated encoder. Encoder should be
456 * subclassed as part of driver encoder objects.
459 * Zero on success, error code on failure.
461 int drm_encoder_init(struct drm_device
*dev
,
462 struct drm_encoder
*encoder
,
463 const struct drm_encoder_funcs
*funcs
,
464 int encoder_type
, const char *name
, ...)
468 drm_modeset_lock_all(dev
);
470 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
475 encoder
->encoder_type
= encoder_type
;
476 encoder
->funcs
= funcs
;
481 encoder
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
484 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
485 drm_encoder_enum_list
[encoder_type
].name
,
488 if (!encoder
->name
) {
493 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
494 encoder
->index
= dev
->mode_config
.num_encoder
++;
498 drm_mode_object_unregister(dev
, &encoder
->base
);
501 drm_modeset_unlock_all(dev
);
505 EXPORT_SYMBOL(drm_encoder_init
);
508 * drm_encoder_cleanup - cleans up an initialised encoder
509 * @encoder: encoder to cleanup
511 * Cleans up the encoder but doesn't free the object.
513 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
515 struct drm_device
*dev
= encoder
->dev
;
517 /* Note that the encoder_list is considered to be static; should we
518 * remove the drm_encoder at runtime we would have to decrement all
519 * the indices on the drm_encoder after us in the encoder_list.
522 drm_modeset_lock_all(dev
);
523 drm_mode_object_unregister(dev
, &encoder
->base
);
524 kfree(encoder
->name
);
525 list_del(&encoder
->head
);
526 dev
->mode_config
.num_encoder
--;
527 drm_modeset_unlock_all(dev
);
529 memset(encoder
, 0, sizeof(*encoder
));
531 EXPORT_SYMBOL(drm_encoder_cleanup
);
533 static unsigned int drm_num_planes(struct drm_device
*dev
)
535 unsigned int num
= 0;
536 struct drm_plane
*tmp
;
538 drm_for_each_plane(tmp
, dev
) {
546 * drm_universal_plane_init - Initialize a new universal plane object
548 * @plane: plane object to init
549 * @possible_crtcs: bitmask of possible CRTCs
550 * @funcs: callbacks for the new plane
551 * @formats: array of supported formats (DRM_FORMAT\_\*)
552 * @format_count: number of elements in @formats
553 * @type: type of plane (overlay, primary, cursor)
554 * @name: printf style format string for the plane name, or NULL for default name
556 * Initializes a plane object of type @type.
559 * Zero on success, error code on failure.
561 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
562 unsigned long possible_crtcs
,
563 const struct drm_plane_funcs
*funcs
,
564 const uint32_t *formats
, unsigned int format_count
,
565 enum drm_plane_type type
,
566 const char *name
, ...)
568 struct drm_mode_config
*config
= &dev
->mode_config
;
571 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
575 drm_modeset_lock_init(&plane
->mutex
);
577 plane
->base
.properties
= &plane
->properties
;
579 plane
->funcs
= funcs
;
580 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
582 if (!plane
->format_types
) {
583 DRM_DEBUG_KMS("out of memory when allocating plane\n");
584 drm_mode_object_unregister(dev
, &plane
->base
);
592 plane
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
595 plane
->name
= kasprintf(GFP_KERNEL
, "plane-%d",
596 drm_num_planes(dev
));
599 kfree(plane
->format_types
);
600 drm_mode_object_unregister(dev
, &plane
->base
);
604 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
605 plane
->format_count
= format_count
;
606 plane
->possible_crtcs
= possible_crtcs
;
609 list_add_tail(&plane
->head
, &config
->plane_list
);
610 plane
->index
= config
->num_total_plane
++;
611 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
612 config
->num_overlay_plane
++;
614 drm_object_attach_property(&plane
->base
,
615 config
->plane_type_property
,
618 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
619 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
620 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
621 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
622 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
623 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
624 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
625 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
626 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
627 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
628 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
633 EXPORT_SYMBOL(drm_universal_plane_init
);
635 static int drm_plane_register_all(struct drm_device
*dev
)
637 struct drm_plane
*plane
;
640 drm_for_each_plane(plane
, dev
) {
641 if (plane
->funcs
->late_register
)
642 ret
= plane
->funcs
->late_register(plane
);
650 static void drm_plane_unregister_all(struct drm_device
*dev
)
652 struct drm_plane
*plane
;
654 drm_for_each_plane(plane
, dev
) {
655 if (plane
->funcs
->early_unregister
)
656 plane
->funcs
->early_unregister(plane
);
661 * drm_plane_init - Initialize a legacy plane
663 * @plane: plane object to init
664 * @possible_crtcs: bitmask of possible CRTCs
665 * @funcs: callbacks for the new plane
666 * @formats: array of supported formats (DRM_FORMAT\_\*)
667 * @format_count: number of elements in @formats
668 * @is_primary: plane type (primary vs overlay)
670 * Legacy API to initialize a DRM plane.
672 * New drivers should call drm_universal_plane_init() instead.
675 * Zero on success, error code on failure.
677 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
678 unsigned long possible_crtcs
,
679 const struct drm_plane_funcs
*funcs
,
680 const uint32_t *formats
, unsigned int format_count
,
683 enum drm_plane_type type
;
685 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
686 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
687 formats
, format_count
, type
, NULL
);
689 EXPORT_SYMBOL(drm_plane_init
);
692 * drm_plane_cleanup - Clean up the core plane usage
693 * @plane: plane to cleanup
695 * This function cleans up @plane and removes it from the DRM mode setting
696 * core. Note that the function does *not* free the plane structure itself,
697 * this is the responsibility of the caller.
699 void drm_plane_cleanup(struct drm_plane
*plane
)
701 struct drm_device
*dev
= plane
->dev
;
703 drm_modeset_lock_all(dev
);
704 kfree(plane
->format_types
);
705 drm_mode_object_unregister(dev
, &plane
->base
);
707 BUG_ON(list_empty(&plane
->head
));
709 /* Note that the plane_list is considered to be static; should we
710 * remove the drm_plane at runtime we would have to decrement all
711 * the indices on the drm_plane after us in the plane_list.
714 list_del(&plane
->head
);
715 dev
->mode_config
.num_total_plane
--;
716 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
717 dev
->mode_config
.num_overlay_plane
--;
718 drm_modeset_unlock_all(dev
);
720 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
721 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
722 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
726 memset(plane
, 0, sizeof(*plane
));
728 EXPORT_SYMBOL(drm_plane_cleanup
);
731 * drm_plane_from_index - find the registered plane at an index
733 * @idx: index of registered plane to find for
735 * Given a plane index, return the registered plane from DRM device's
736 * list of planes with matching index.
739 drm_plane_from_index(struct drm_device
*dev
, int idx
)
741 struct drm_plane
*plane
;
743 drm_for_each_plane(plane
, dev
)
744 if (idx
== plane
->index
)
749 EXPORT_SYMBOL(drm_plane_from_index
);
752 * drm_plane_force_disable - Forcibly disable a plane
753 * @plane: plane to disable
755 * Forces the plane to be disabled.
757 * Used when the plane's current framebuffer is destroyed,
758 * and when restoring fbdev mode.
760 void drm_plane_force_disable(struct drm_plane
*plane
)
767 plane
->old_fb
= plane
->fb
;
768 ret
= plane
->funcs
->disable_plane(plane
);
770 DRM_ERROR("failed to disable plane with busy fb\n");
771 plane
->old_fb
= NULL
;
774 /* disconnect the plane from the fb and crtc: */
775 drm_framebuffer_unreference(plane
->old_fb
);
776 plane
->old_fb
= NULL
;
780 EXPORT_SYMBOL(drm_plane_force_disable
);
782 int drm_modeset_register_all(struct drm_device
*dev
)
786 ret
= drm_plane_register_all(dev
);
790 ret
= drm_crtc_register_all(dev
);
794 ret
= drm_encoder_register_all(dev
);
798 ret
= drm_connector_register_all(dev
);
805 drm_encoder_unregister_all(dev
);
807 drm_crtc_unregister_all(dev
);
809 drm_plane_unregister_all(dev
);
814 void drm_modeset_unregister_all(struct drm_device
*dev
)
816 drm_connector_unregister_all(dev
);
817 drm_encoder_unregister_all(dev
);
818 drm_crtc_unregister_all(dev
);
819 drm_plane_unregister_all(dev
);
822 static int drm_mode_create_standard_properties(struct drm_device
*dev
)
824 struct drm_property
*prop
;
827 ret
= drm_connector_create_standard_properties(dev
);
831 prop
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
832 "type", drm_plane_type_enum_list
,
833 ARRAY_SIZE(drm_plane_type_enum_list
));
836 dev
->mode_config
.plane_type_property
= prop
;
838 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
839 "SRC_X", 0, UINT_MAX
);
842 dev
->mode_config
.prop_src_x
= prop
;
844 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
845 "SRC_Y", 0, UINT_MAX
);
848 dev
->mode_config
.prop_src_y
= prop
;
850 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
851 "SRC_W", 0, UINT_MAX
);
854 dev
->mode_config
.prop_src_w
= prop
;
856 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
857 "SRC_H", 0, UINT_MAX
);
860 dev
->mode_config
.prop_src_h
= prop
;
862 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
863 "CRTC_X", INT_MIN
, INT_MAX
);
866 dev
->mode_config
.prop_crtc_x
= prop
;
868 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
869 "CRTC_Y", INT_MIN
, INT_MAX
);
872 dev
->mode_config
.prop_crtc_y
= prop
;
874 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
875 "CRTC_W", 0, INT_MAX
);
878 dev
->mode_config
.prop_crtc_w
= prop
;
880 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
881 "CRTC_H", 0, INT_MAX
);
884 dev
->mode_config
.prop_crtc_h
= prop
;
886 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
887 "FB_ID", DRM_MODE_OBJECT_FB
);
890 dev
->mode_config
.prop_fb_id
= prop
;
892 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
893 "CRTC_ID", DRM_MODE_OBJECT_CRTC
);
896 dev
->mode_config
.prop_crtc_id
= prop
;
898 prop
= drm_property_create_bool(dev
, DRM_MODE_PROP_ATOMIC
,
902 dev
->mode_config
.prop_active
= prop
;
904 prop
= drm_property_create(dev
,
905 DRM_MODE_PROP_ATOMIC
| DRM_MODE_PROP_BLOB
,
909 dev
->mode_config
.prop_mode_id
= prop
;
911 prop
= drm_property_create(dev
,
916 dev
->mode_config
.degamma_lut_property
= prop
;
918 prop
= drm_property_create_range(dev
,
919 DRM_MODE_PROP_IMMUTABLE
,
920 "DEGAMMA_LUT_SIZE", 0, UINT_MAX
);
923 dev
->mode_config
.degamma_lut_size_property
= prop
;
925 prop
= drm_property_create(dev
,
930 dev
->mode_config
.ctm_property
= prop
;
932 prop
= drm_property_create(dev
,
937 dev
->mode_config
.gamma_lut_property
= prop
;
939 prop
= drm_property_create_range(dev
,
940 DRM_MODE_PROP_IMMUTABLE
,
941 "GAMMA_LUT_SIZE", 0, UINT_MAX
);
944 dev
->mode_config
.gamma_lut_size_property
= prop
;
950 * drm_mode_getresources - get graphics configuration
951 * @dev: drm device for the ioctl
952 * @data: data pointer for the ioctl
953 * @file_priv: drm file for the ioctl call
955 * Construct a set of configuration description structures and return
956 * them to the user, including CRTC, connector and framebuffer configuration.
958 * Called by the user via ioctl.
961 * Zero on success, negative errno on failure.
963 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
964 struct drm_file
*file_priv
)
966 struct drm_mode_card_res
*card_res
= data
;
967 struct list_head
*lh
;
968 struct drm_framebuffer
*fb
;
969 struct drm_connector
*connector
;
970 struct drm_crtc
*crtc
;
971 struct drm_encoder
*encoder
;
973 int connector_count
= 0;
976 int encoder_count
= 0;
978 uint32_t __user
*fb_id
;
979 uint32_t __user
*crtc_id
;
980 uint32_t __user
*connector_id
;
981 uint32_t __user
*encoder_id
;
983 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
987 mutex_lock(&file_priv
->fbs_lock
);
989 * For the non-control nodes we need to limit the list of resources
990 * by IDs in the group list for this node
992 list_for_each(lh
, &file_priv
->fbs
)
995 /* handle this in 4 parts */
997 if (card_res
->count_fbs
>= fb_count
) {
999 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1000 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1001 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1002 mutex_unlock(&file_priv
->fbs_lock
);
1008 card_res
->count_fbs
= fb_count
;
1009 mutex_unlock(&file_priv
->fbs_lock
);
1011 /* mode_config.mutex protects the connector list against e.g. DP MST
1012 * connector hot-adding. CRTC/Plane lists are invariant. */
1013 mutex_lock(&dev
->mode_config
.mutex
);
1014 drm_for_each_crtc(crtc
, dev
)
1017 drm_for_each_connector(connector
, dev
)
1020 drm_for_each_encoder(encoder
, dev
)
1023 card_res
->max_height
= dev
->mode_config
.max_height
;
1024 card_res
->min_height
= dev
->mode_config
.min_height
;
1025 card_res
->max_width
= dev
->mode_config
.max_width
;
1026 card_res
->min_width
= dev
->mode_config
.min_width
;
1029 if (card_res
->count_crtcs
>= crtc_count
) {
1031 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1032 drm_for_each_crtc(crtc
, dev
) {
1033 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1040 card_res
->count_crtcs
= crtc_count
;
1043 if (card_res
->count_encoders
>= encoder_count
) {
1045 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1046 drm_for_each_encoder(encoder
, dev
) {
1047 if (put_user(encoder
->base
.id
, encoder_id
+
1055 card_res
->count_encoders
= encoder_count
;
1058 if (card_res
->count_connectors
>= connector_count
) {
1060 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1061 drm_for_each_connector(connector
, dev
) {
1062 if (put_user(connector
->base
.id
,
1063 connector_id
+ copied
)) {
1070 card_res
->count_connectors
= connector_count
;
1073 mutex_unlock(&dev
->mode_config
.mutex
);
1078 * drm_mode_getcrtc - get CRTC configuration
1079 * @dev: drm device for the ioctl
1080 * @data: data pointer for the ioctl
1081 * @file_priv: drm file for the ioctl call
1083 * Construct a CRTC configuration structure to return to the user.
1085 * Called by the user via ioctl.
1088 * Zero on success, negative errno on failure.
1090 int drm_mode_getcrtc(struct drm_device
*dev
,
1091 void *data
, struct drm_file
*file_priv
)
1093 struct drm_mode_crtc
*crtc_resp
= data
;
1094 struct drm_crtc
*crtc
;
1096 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1099 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1103 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
1104 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1105 if (crtc
->primary
->fb
)
1106 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1108 crtc_resp
->fb_id
= 0;
1111 crtc_resp
->x
= crtc
->primary
->state
->src_x
>> 16;
1112 crtc_resp
->y
= crtc
->primary
->state
->src_y
>> 16;
1113 if (crtc
->state
->enable
) {
1114 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->state
->mode
);
1115 crtc_resp
->mode_valid
= 1;
1118 crtc_resp
->mode_valid
= 0;
1121 crtc_resp
->x
= crtc
->x
;
1122 crtc_resp
->y
= crtc
->y
;
1123 if (crtc
->enabled
) {
1124 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1125 crtc_resp
->mode_valid
= 1;
1128 crtc_resp
->mode_valid
= 0;
1131 drm_modeset_unlock_crtc(crtc
);
1136 /* helper for getconnector and getproperties ioctls */
1137 int drm_mode_object_get_properties(struct drm_mode_object
*obj
, bool atomic
,
1138 uint32_t __user
*prop_ptr
,
1139 uint64_t __user
*prop_values
,
1140 uint32_t *arg_count_props
)
1145 props_count
= obj
->properties
->count
;
1147 props_count
-= obj
->properties
->atomic_count
;
1149 if ((*arg_count_props
>= props_count
) && props_count
) {
1150 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
1151 struct drm_property
*prop
= obj
->properties
->properties
[i
];
1154 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
1157 ret
= drm_object_property_get_value(obj
, prop
, &val
);
1161 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
1164 if (put_user(val
, prop_values
+ copied
))
1170 *arg_count_props
= props_count
;
1175 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
1177 struct drm_connector
*connector
;
1178 struct drm_device
*dev
= encoder
->dev
;
1179 bool uses_atomic
= false;
1181 /* For atomic drivers only state objects are synchronously updated and
1182 * protected by modeset locks, so check those first. */
1183 drm_for_each_connector(connector
, dev
) {
1184 if (!connector
->state
)
1189 if (connector
->state
->best_encoder
!= encoder
)
1192 return connector
->state
->crtc
;
1195 /* Don't return stale data (e.g. pending async disable). */
1199 return encoder
->crtc
;
1203 * drm_mode_getencoder - get encoder configuration
1204 * @dev: drm device for the ioctl
1205 * @data: data pointer for the ioctl
1206 * @file_priv: drm file for the ioctl call
1208 * Construct a encoder configuration structure to return to the user.
1210 * Called by the user via ioctl.
1213 * Zero on success, negative errno on failure.
1215 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1216 struct drm_file
*file_priv
)
1218 struct drm_mode_get_encoder
*enc_resp
= data
;
1219 struct drm_encoder
*encoder
;
1220 struct drm_crtc
*crtc
;
1222 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1225 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
1229 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1230 crtc
= drm_encoder_get_crtc(encoder
);
1232 enc_resp
->crtc_id
= crtc
->base
.id
;
1234 enc_resp
->crtc_id
= 0;
1235 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1237 enc_resp
->encoder_type
= encoder
->encoder_type
;
1238 enc_resp
->encoder_id
= encoder
->base
.id
;
1239 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1240 enc_resp
->possible_clones
= encoder
->possible_clones
;
1246 * drm_mode_getplane_res - enumerate all plane resources
1249 * @file_priv: DRM file info
1251 * Construct a list of plane ids to return to the user.
1253 * Called by the user via ioctl.
1256 * Zero on success, negative errno on failure.
1258 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
1259 struct drm_file
*file_priv
)
1261 struct drm_mode_get_plane_res
*plane_resp
= data
;
1262 struct drm_mode_config
*config
;
1263 struct drm_plane
*plane
;
1264 uint32_t __user
*plane_ptr
;
1266 unsigned num_planes
;
1268 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1271 config
= &dev
->mode_config
;
1273 if (file_priv
->universal_planes
)
1274 num_planes
= config
->num_total_plane
;
1276 num_planes
= config
->num_overlay_plane
;
1279 * This ioctl is called twice, once to determine how much space is
1280 * needed, and the 2nd time to fill it.
1283 (plane_resp
->count_planes
>= num_planes
)) {
1284 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
1286 /* Plane lists are invariant, no locking needed. */
1287 drm_for_each_plane(plane
, dev
) {
1289 * Unless userspace set the 'universal planes'
1290 * capability bit, only advertise overlays.
1292 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
1293 !file_priv
->universal_planes
)
1296 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
1301 plane_resp
->count_planes
= num_planes
;
1307 * drm_mode_getplane - get plane configuration
1310 * @file_priv: DRM file info
1312 * Construct a plane configuration structure to return to the user.
1314 * Called by the user via ioctl.
1317 * Zero on success, negative errno on failure.
1319 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
1320 struct drm_file
*file_priv
)
1322 struct drm_mode_get_plane
*plane_resp
= data
;
1323 struct drm_plane
*plane
;
1324 uint32_t __user
*format_ptr
;
1326 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1329 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
1333 drm_modeset_lock(&plane
->mutex
, NULL
);
1335 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
1337 plane_resp
->crtc_id
= 0;
1340 plane_resp
->fb_id
= plane
->fb
->base
.id
;
1342 plane_resp
->fb_id
= 0;
1343 drm_modeset_unlock(&plane
->mutex
);
1345 plane_resp
->plane_id
= plane
->base
.id
;
1346 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
1347 plane_resp
->gamma_size
= 0;
1350 * This ioctl is called twice, once to determine how much space is
1351 * needed, and the 2nd time to fill it.
1353 if (plane
->format_count
&&
1354 (plane_resp
->count_format_types
>= plane
->format_count
)) {
1355 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
1356 if (copy_to_user(format_ptr
,
1357 plane
->format_types
,
1358 sizeof(uint32_t) * plane
->format_count
)) {
1362 plane_resp
->count_format_types
= plane
->format_count
;
1368 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
1369 * @plane: plane to check for format support
1370 * @format: the pixel format
1373 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
1376 int drm_plane_check_pixel_format(const struct drm_plane
*plane
, u32 format
)
1380 for (i
= 0; i
< plane
->format_count
; i
++) {
1381 if (format
== plane
->format_types
[i
])
1388 static int check_src_coords(uint32_t src_x
, uint32_t src_y
,
1389 uint32_t src_w
, uint32_t src_h
,
1390 const struct drm_framebuffer
*fb
)
1392 unsigned int fb_width
, fb_height
;
1394 fb_width
= fb
->width
<< 16;
1395 fb_height
= fb
->height
<< 16;
1397 /* Make sure source coordinates are inside the fb. */
1398 if (src_w
> fb_width
||
1399 src_x
> fb_width
- src_w
||
1400 src_h
> fb_height
||
1401 src_y
> fb_height
- src_h
) {
1402 DRM_DEBUG_KMS("Invalid source coordinates "
1403 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1404 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
1405 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
1406 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
1407 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
1415 * setplane_internal - setplane handler for internal callers
1417 * Note that we assume an extra reference has already been taken on fb. If the
1418 * update fails, this reference will be dropped before return; if it succeeds,
1419 * the previous framebuffer (if any) will be unreferenced instead.
1421 * src_{x,y,w,h} are provided in 16.16 fixed point format
1423 static int __setplane_internal(struct drm_plane
*plane
,
1424 struct drm_crtc
*crtc
,
1425 struct drm_framebuffer
*fb
,
1426 int32_t crtc_x
, int32_t crtc_y
,
1427 uint32_t crtc_w
, uint32_t crtc_h
,
1428 /* src_{x,y,w,h} values are 16.16 fixed point */
1429 uint32_t src_x
, uint32_t src_y
,
1430 uint32_t src_w
, uint32_t src_h
)
1434 /* No fb means shut it down */
1436 plane
->old_fb
= plane
->fb
;
1437 ret
= plane
->funcs
->disable_plane(plane
);
1442 plane
->old_fb
= NULL
;
1447 /* Check whether this plane is usable on this CRTC */
1448 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
1449 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1454 /* Check whether this plane supports the fb pixel format. */
1455 ret
= drm_plane_check_pixel_format(plane
, fb
->pixel_format
);
1457 char *format_name
= drm_get_format_name(fb
->pixel_format
);
1458 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name
);
1463 /* Give drivers some help against integer overflows */
1464 if (crtc_w
> INT_MAX
||
1465 crtc_x
> INT_MAX
- (int32_t) crtc_w
||
1467 crtc_y
> INT_MAX
- (int32_t) crtc_h
) {
1468 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1469 crtc_w
, crtc_h
, crtc_x
, crtc_y
);
1474 ret
= check_src_coords(src_x
, src_y
, src_w
, src_h
, fb
);
1478 plane
->old_fb
= plane
->fb
;
1479 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
1480 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
1481 src_x
, src_y
, src_w
, src_h
);
1487 plane
->old_fb
= NULL
;
1492 drm_framebuffer_unreference(fb
);
1494 drm_framebuffer_unreference(plane
->old_fb
);
1495 plane
->old_fb
= NULL
;
1500 static int setplane_internal(struct drm_plane
*plane
,
1501 struct drm_crtc
*crtc
,
1502 struct drm_framebuffer
*fb
,
1503 int32_t crtc_x
, int32_t crtc_y
,
1504 uint32_t crtc_w
, uint32_t crtc_h
,
1505 /* src_{x,y,w,h} values are 16.16 fixed point */
1506 uint32_t src_x
, uint32_t src_y
,
1507 uint32_t src_w
, uint32_t src_h
)
1511 drm_modeset_lock_all(plane
->dev
);
1512 ret
= __setplane_internal(plane
, crtc
, fb
,
1513 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
1514 src_x
, src_y
, src_w
, src_h
);
1515 drm_modeset_unlock_all(plane
->dev
);
1521 * drm_mode_setplane - configure a plane's configuration
1523 * @data: ioctl data*
1524 * @file_priv: DRM file info
1526 * Set plane configuration, including placement, fb, scaling, and other factors.
1527 * Or pass a NULL fb to disable (planes may be disabled without providing a
1531 * Zero on success, negative errno on failure.
1533 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
1534 struct drm_file
*file_priv
)
1536 struct drm_mode_set_plane
*plane_req
= data
;
1537 struct drm_plane
*plane
;
1538 struct drm_crtc
*crtc
= NULL
;
1539 struct drm_framebuffer
*fb
= NULL
;
1541 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1545 * First, find the plane, crtc, and fb objects. If not available,
1546 * we don't bother to call the driver.
1548 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
1550 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1551 plane_req
->plane_id
);
1555 if (plane_req
->fb_id
) {
1556 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
1558 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1563 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
1565 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1566 plane_req
->crtc_id
);
1572 * setplane_internal will take care of deref'ing either the old or new
1573 * framebuffer depending on success.
1575 return setplane_internal(plane
, crtc
, fb
,
1576 plane_req
->crtc_x
, plane_req
->crtc_y
,
1577 plane_req
->crtc_w
, plane_req
->crtc_h
,
1578 plane_req
->src_x
, plane_req
->src_y
,
1579 plane_req
->src_w
, plane_req
->src_h
);
1583 * drm_mode_set_config_internal - helper to call ->set_config
1584 * @set: modeset config to set
1586 * This is a little helper to wrap internal calls to the ->set_config driver
1587 * interface. The only thing it adds is correct refcounting dance.
1590 * Zero on success, negative errno on failure.
1592 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
1594 struct drm_crtc
*crtc
= set
->crtc
;
1595 struct drm_framebuffer
*fb
;
1596 struct drm_crtc
*tmp
;
1600 * NOTE: ->set_config can also disable other crtcs (if we steal all
1601 * connectors from it), hence we need to refcount the fbs across all
1602 * crtcs. Atomic modeset will have saner semantics ...
1604 drm_for_each_crtc(tmp
, crtc
->dev
)
1605 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
1609 ret
= crtc
->funcs
->set_config(set
);
1611 crtc
->primary
->crtc
= crtc
;
1612 crtc
->primary
->fb
= fb
;
1615 drm_for_each_crtc(tmp
, crtc
->dev
) {
1616 if (tmp
->primary
->fb
)
1617 drm_framebuffer_reference(tmp
->primary
->fb
);
1618 if (tmp
->primary
->old_fb
)
1619 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
1620 tmp
->primary
->old_fb
= NULL
;
1625 EXPORT_SYMBOL(drm_mode_set_config_internal
);
1628 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1629 * @mode: mode to query
1630 * @hdisplay: hdisplay value to fill in
1631 * @vdisplay: vdisplay value to fill in
1633 * The vdisplay value will be doubled if the specified mode is a stereo mode of
1634 * the appropriate layout.
1636 void drm_crtc_get_hv_timing(const struct drm_display_mode
*mode
,
1637 int *hdisplay
, int *vdisplay
)
1639 struct drm_display_mode adjusted
;
1641 drm_mode_copy(&adjusted
, mode
);
1642 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE_ONLY
);
1643 *hdisplay
= adjusted
.crtc_hdisplay
;
1644 *vdisplay
= adjusted
.crtc_vdisplay
;
1646 EXPORT_SYMBOL(drm_crtc_get_hv_timing
);
1649 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1651 * @crtc: CRTC that framebuffer will be displayed on
1654 * @mode: mode that framebuffer will be displayed under
1655 * @fb: framebuffer to check size of
1657 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
1659 const struct drm_display_mode
*mode
,
1660 const struct drm_framebuffer
*fb
)
1663 int hdisplay
, vdisplay
;
1665 drm_crtc_get_hv_timing(mode
, &hdisplay
, &vdisplay
);
1668 crtc
->primary
->state
->rotation
& (DRM_ROTATE_90
|
1670 swap(hdisplay
, vdisplay
);
1672 return check_src_coords(x
<< 16, y
<< 16,
1673 hdisplay
<< 16, vdisplay
<< 16, fb
);
1675 EXPORT_SYMBOL(drm_crtc_check_viewport
);
1678 * drm_mode_setcrtc - set CRTC configuration
1679 * @dev: drm device for the ioctl
1680 * @data: data pointer for the ioctl
1681 * @file_priv: drm file for the ioctl call
1683 * Build a new CRTC configuration based on user request.
1685 * Called by the user via ioctl.
1688 * Zero on success, negative errno on failure.
1690 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
1691 struct drm_file
*file_priv
)
1693 struct drm_mode_config
*config
= &dev
->mode_config
;
1694 struct drm_mode_crtc
*crtc_req
= data
;
1695 struct drm_crtc
*crtc
;
1696 struct drm_connector
**connector_set
= NULL
, *connector
;
1697 struct drm_framebuffer
*fb
= NULL
;
1698 struct drm_display_mode
*mode
= NULL
;
1699 struct drm_mode_set set
;
1700 uint32_t __user
*set_connectors_ptr
;
1704 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1708 * Universal plane src offsets are only 16.16, prevent havoc for
1709 * drivers using universal plane code internally.
1711 if (crtc_req
->x
& 0xffff0000 || crtc_req
->y
& 0xffff0000)
1714 drm_modeset_lock_all(dev
);
1715 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
1717 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
1721 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc
->base
.id
, crtc
->name
);
1723 if (crtc_req
->mode_valid
) {
1724 /* If we have a mode we need a framebuffer. */
1725 /* If we pass -1, set the mode with the currently bound fb */
1726 if (crtc_req
->fb_id
== -1) {
1727 if (!crtc
->primary
->fb
) {
1728 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1732 fb
= crtc
->primary
->fb
;
1733 /* Make refcounting symmetric with the lookup path. */
1734 drm_framebuffer_reference(fb
);
1736 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
1738 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1745 mode
= drm_mode_create(dev
);
1751 ret
= drm_mode_convert_umode(mode
, &crtc_req
->mode
);
1753 DRM_DEBUG_KMS("Invalid mode\n");
1758 * Check whether the primary plane supports the fb pixel format.
1759 * Drivers not implementing the universal planes API use a
1760 * default formats list provided by the DRM core which doesn't
1761 * match real hardware capabilities. Skip the check in that
1764 if (!crtc
->primary
->format_default
) {
1765 ret
= drm_plane_check_pixel_format(crtc
->primary
,
1768 char *format_name
= drm_get_format_name(fb
->pixel_format
);
1769 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name
);
1775 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
1782 if (crtc_req
->count_connectors
== 0 && mode
) {
1783 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1788 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
1789 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1790 crtc_req
->count_connectors
);
1795 if (crtc_req
->count_connectors
> 0) {
1798 /* Avoid unbounded kernel memory allocation */
1799 if (crtc_req
->count_connectors
> config
->num_connector
) {
1804 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
1805 sizeof(struct drm_connector
*),
1807 if (!connector_set
) {
1812 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
1813 connector_set
[i
] = NULL
;
1814 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
1815 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
1820 connector
= drm_connector_lookup(dev
, out_id
);
1822 DRM_DEBUG_KMS("Connector id %d unknown\n",
1827 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1831 connector_set
[i
] = connector
;
1836 set
.x
= crtc_req
->x
;
1837 set
.y
= crtc_req
->y
;
1839 set
.connectors
= connector_set
;
1840 set
.num_connectors
= crtc_req
->count_connectors
;
1842 ret
= drm_mode_set_config_internal(&set
);
1846 drm_framebuffer_unreference(fb
);
1848 if (connector_set
) {
1849 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
1850 if (connector_set
[i
])
1851 drm_connector_unreference(connector_set
[i
]);
1854 kfree(connector_set
);
1855 drm_mode_destroy(dev
, mode
);
1856 drm_modeset_unlock_all(dev
);
1861 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1862 * universal plane handler call
1863 * @crtc: crtc to update cursor for
1864 * @req: data pointer for the ioctl
1865 * @file_priv: drm file for the ioctl call
1867 * Legacy cursor ioctl's work directly with driver buffer handles. To
1868 * translate legacy ioctl calls into universal plane handler calls, we need to
1869 * wrap the native buffer handle in a drm_framebuffer.
1871 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1872 * buffer with a pitch of 4*width; the universal plane interface should be used
1873 * directly in cases where the hardware can support other buffer settings and
1874 * userspace wants to make use of these capabilities.
1877 * Zero on success, negative errno on failure.
1879 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
1880 struct drm_mode_cursor2
*req
,
1881 struct drm_file
*file_priv
)
1883 struct drm_device
*dev
= crtc
->dev
;
1884 struct drm_framebuffer
*fb
= NULL
;
1885 struct drm_mode_fb_cmd2 fbreq
= {
1886 .width
= req
->width
,
1887 .height
= req
->height
,
1888 .pixel_format
= DRM_FORMAT_ARGB8888
,
1889 .pitches
= { req
->width
* 4 },
1890 .handles
= { req
->handle
},
1892 int32_t crtc_x
, crtc_y
;
1893 uint32_t crtc_w
= 0, crtc_h
= 0;
1894 uint32_t src_w
= 0, src_h
= 0;
1897 BUG_ON(!crtc
->cursor
);
1898 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
1901 * Obtain fb we'll be using (either new or existing) and take an extra
1902 * reference to it if fb != null. setplane will take care of dropping
1903 * the reference if the plane update fails.
1905 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
1907 fb
= drm_internal_framebuffer_create(dev
, &fbreq
, file_priv
);
1909 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1912 fb
->hot_x
= req
->hot_x
;
1913 fb
->hot_y
= req
->hot_y
;
1918 fb
= crtc
->cursor
->fb
;
1920 drm_framebuffer_reference(fb
);
1923 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
1927 crtc_x
= crtc
->cursor_x
;
1928 crtc_y
= crtc
->cursor_y
;
1933 crtc_h
= fb
->height
;
1934 src_w
= fb
->width
<< 16;
1935 src_h
= fb
->height
<< 16;
1939 * setplane_internal will take care of deref'ing either the old or new
1940 * framebuffer depending on success.
1942 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
1943 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
1944 0, 0, src_w
, src_h
);
1946 /* Update successful; save new cursor position, if necessary */
1947 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
1948 crtc
->cursor_x
= req
->x
;
1949 crtc
->cursor_y
= req
->y
;
1955 static int drm_mode_cursor_common(struct drm_device
*dev
,
1956 struct drm_mode_cursor2
*req
,
1957 struct drm_file
*file_priv
)
1959 struct drm_crtc
*crtc
;
1962 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1965 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
1968 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
1970 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
1975 * If this crtc has a universal cursor plane, call that plane's update
1976 * handler rather than using legacy cursor handlers.
1978 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
1980 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
1984 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
1985 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
1989 /* Turns off the cursor if handle is 0 */
1990 if (crtc
->funcs
->cursor_set2
)
1991 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
1992 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
1994 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
1995 req
->width
, req
->height
);
1998 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
1999 if (crtc
->funcs
->cursor_move
) {
2000 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2007 drm_modeset_unlock_crtc(crtc
);
2015 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2016 * @dev: drm device for the ioctl
2017 * @data: data pointer for the ioctl
2018 * @file_priv: drm file for the ioctl call
2020 * Set the cursor configuration based on user request.
2022 * Called by the user via ioctl.
2025 * Zero on success, negative errno on failure.
2027 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2028 void *data
, struct drm_file
*file_priv
)
2030 struct drm_mode_cursor
*req
= data
;
2031 struct drm_mode_cursor2 new_req
;
2033 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2034 new_req
.hot_x
= new_req
.hot_y
= 0;
2036 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2040 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2041 * @dev: drm device for the ioctl
2042 * @data: data pointer for the ioctl
2043 * @file_priv: drm file for the ioctl call
2045 * Set the cursor configuration based on user request. This implements the 2nd
2046 * version of the cursor ioctl, which allows userspace to additionally specify
2047 * the hotspot of the pointer.
2049 * Called by the user via ioctl.
2052 * Zero on success, negative errno on failure.
2054 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2055 void *data
, struct drm_file
*file_priv
)
2057 struct drm_mode_cursor2
*req
= data
;
2059 return drm_mode_cursor_common(dev
, req
, file_priv
);
2063 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2064 * @bpp: bits per pixels
2065 * @depth: bit depth per pixel
2067 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2068 * Useful in fbdev emulation code, since that deals in those values.
2070 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2076 fmt
= DRM_FORMAT_C8
;
2080 fmt
= DRM_FORMAT_XRGB1555
;
2082 fmt
= DRM_FORMAT_RGB565
;
2085 fmt
= DRM_FORMAT_RGB888
;
2089 fmt
= DRM_FORMAT_XRGB8888
;
2090 else if (depth
== 30)
2091 fmt
= DRM_FORMAT_XRGB2101010
;
2093 fmt
= DRM_FORMAT_ARGB8888
;
2096 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2097 fmt
= DRM_FORMAT_XRGB8888
;
2103 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2105 static bool drm_property_type_valid(struct drm_property
*property
)
2107 if (property
->flags
& DRM_MODE_PROP_EXTENDED_TYPE
)
2108 return !(property
->flags
& DRM_MODE_PROP_LEGACY_TYPE
);
2109 return !!(property
->flags
& DRM_MODE_PROP_LEGACY_TYPE
);
2113 * drm_property_create - create a new property type
2115 * @flags: flags specifying the property type
2116 * @name: name of the property
2117 * @num_values: number of pre-defined values
2119 * This creates a new generic drm property which can then be attached to a drm
2120 * object with drm_object_attach_property. The returned property object must be
2121 * freed with drm_property_destroy.
2123 * Note that the DRM core keeps a per-device list of properties and that, if
2124 * drm_mode_config_cleanup() is called, it will destroy all properties created
2128 * A pointer to the newly created property on success, NULL on failure.
2130 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
2131 const char *name
, int num_values
)
2133 struct drm_property
*property
= NULL
;
2136 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
2140 property
->dev
= dev
;
2143 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
2145 if (!property
->values
)
2149 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
2153 property
->flags
= flags
;
2154 property
->num_values
= num_values
;
2155 INIT_LIST_HEAD(&property
->enum_list
);
2158 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
2159 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2162 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
2164 WARN_ON(!drm_property_type_valid(property
));
2168 kfree(property
->values
);
2172 EXPORT_SYMBOL(drm_property_create
);
2175 * drm_property_create_enum - create a new enumeration property type
2177 * @flags: flags specifying the property type
2178 * @name: name of the property
2179 * @props: enumeration lists with property values
2180 * @num_values: number of pre-defined values
2182 * This creates a new generic drm property which can then be attached to a drm
2183 * object with drm_object_attach_property. The returned property object must be
2184 * freed with drm_property_destroy.
2186 * Userspace is only allowed to set one of the predefined values for enumeration
2190 * A pointer to the newly created property on success, NULL on failure.
2192 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
2194 const struct drm_prop_enum_list
*props
,
2197 struct drm_property
*property
;
2200 flags
|= DRM_MODE_PROP_ENUM
;
2202 property
= drm_property_create(dev
, flags
, name
, num_values
);
2206 for (i
= 0; i
< num_values
; i
++) {
2207 ret
= drm_property_add_enum(property
, i
,
2211 drm_property_destroy(dev
, property
);
2218 EXPORT_SYMBOL(drm_property_create_enum
);
2221 * drm_property_create_bitmask - create a new bitmask property type
2223 * @flags: flags specifying the property type
2224 * @name: name of the property
2225 * @props: enumeration lists with property bitflags
2226 * @num_props: size of the @props array
2227 * @supported_bits: bitmask of all supported enumeration values
2229 * This creates a new bitmask drm property which can then be attached to a drm
2230 * object with drm_object_attach_property. The returned property object must be
2231 * freed with drm_property_destroy.
2233 * Compared to plain enumeration properties userspace is allowed to set any
2234 * or'ed together combination of the predefined property bitflag values
2237 * A pointer to the newly created property on success, NULL on failure.
2239 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
2240 int flags
, const char *name
,
2241 const struct drm_prop_enum_list
*props
,
2243 uint64_t supported_bits
)
2245 struct drm_property
*property
;
2246 int i
, ret
, index
= 0;
2247 int num_values
= hweight64(supported_bits
);
2249 flags
|= DRM_MODE_PROP_BITMASK
;
2251 property
= drm_property_create(dev
, flags
, name
, num_values
);
2254 for (i
= 0; i
< num_props
; i
++) {
2255 if (!(supported_bits
& (1ULL << props
[i
].type
)))
2258 if (WARN_ON(index
>= num_values
)) {
2259 drm_property_destroy(dev
, property
);
2263 ret
= drm_property_add_enum(property
, index
++,
2267 drm_property_destroy(dev
, property
);
2274 EXPORT_SYMBOL(drm_property_create_bitmask
);
2276 static struct drm_property
*property_create_range(struct drm_device
*dev
,
2277 int flags
, const char *name
,
2278 uint64_t min
, uint64_t max
)
2280 struct drm_property
*property
;
2282 property
= drm_property_create(dev
, flags
, name
, 2);
2286 property
->values
[0] = min
;
2287 property
->values
[1] = max
;
2293 * drm_property_create_range - create a new unsigned ranged property type
2295 * @flags: flags specifying the property type
2296 * @name: name of the property
2297 * @min: minimum value of the property
2298 * @max: maximum value of the property
2300 * This creates a new generic drm property which can then be attached to a drm
2301 * object with drm_object_attach_property. The returned property object must be
2302 * freed with drm_property_destroy.
2304 * Userspace is allowed to set any unsigned integer value in the (min, max)
2308 * A pointer to the newly created property on success, NULL on failure.
2310 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
2312 uint64_t min
, uint64_t max
)
2314 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
2317 EXPORT_SYMBOL(drm_property_create_range
);
2320 * drm_property_create_signed_range - create a new signed ranged property type
2322 * @flags: flags specifying the property type
2323 * @name: name of the property
2324 * @min: minimum value of the property
2325 * @max: maximum value of the property
2327 * This creates a new generic drm property which can then be attached to a drm
2328 * object with drm_object_attach_property. The returned property object must be
2329 * freed with drm_property_destroy.
2331 * Userspace is allowed to set any signed integer value in the (min, max)
2335 * A pointer to the newly created property on success, NULL on failure.
2337 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
2338 int flags
, const char *name
,
2339 int64_t min
, int64_t max
)
2341 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
2342 name
, I642U64(min
), I642U64(max
));
2344 EXPORT_SYMBOL(drm_property_create_signed_range
);
2347 * drm_property_create_object - create a new object property type
2349 * @flags: flags specifying the property type
2350 * @name: name of the property
2351 * @type: object type from DRM_MODE_OBJECT_* defines
2353 * This creates a new generic drm property which can then be attached to a drm
2354 * object with drm_object_attach_property. The returned property object must be
2355 * freed with drm_property_destroy.
2357 * Userspace is only allowed to set this to any property value of the given
2358 * @type. Only useful for atomic properties, which is enforced.
2361 * A pointer to the newly created property on success, NULL on failure.
2363 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
2364 int flags
, const char *name
, uint32_t type
)
2366 struct drm_property
*property
;
2368 flags
|= DRM_MODE_PROP_OBJECT
;
2370 if (WARN_ON(!(flags
& DRM_MODE_PROP_ATOMIC
)))
2373 property
= drm_property_create(dev
, flags
, name
, 1);
2377 property
->values
[0] = type
;
2381 EXPORT_SYMBOL(drm_property_create_object
);
2384 * drm_property_create_bool - create a new boolean property type
2386 * @flags: flags specifying the property type
2387 * @name: name of the property
2389 * This creates a new generic drm property which can then be attached to a drm
2390 * object with drm_object_attach_property. The returned property object must be
2391 * freed with drm_property_destroy.
2393 * This is implemented as a ranged property with only {0, 1} as valid values.
2396 * A pointer to the newly created property on success, NULL on failure.
2398 struct drm_property
*drm_property_create_bool(struct drm_device
*dev
, int flags
,
2401 return drm_property_create_range(dev
, flags
, name
, 0, 1);
2403 EXPORT_SYMBOL(drm_property_create_bool
);
2406 * drm_property_add_enum - add a possible value to an enumeration property
2407 * @property: enumeration property to change
2408 * @index: index of the new enumeration
2409 * @value: value of the new enumeration
2410 * @name: symbolic name of the new enumeration
2412 * This functions adds enumerations to a property.
2414 * It's use is deprecated, drivers should use one of the more specific helpers
2415 * to directly create the property with all enumerations already attached.
2418 * Zero on success, error code on failure.
2420 int drm_property_add_enum(struct drm_property
*property
, int index
,
2421 uint64_t value
, const char *name
)
2423 struct drm_property_enum
*prop_enum
;
2425 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
2426 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
2430 * Bitmask enum properties have the additional constraint of values
2433 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
2437 if (!list_empty(&property
->enum_list
)) {
2438 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
2439 if (prop_enum
->value
== value
) {
2440 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2441 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2447 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
2451 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
2452 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
2453 prop_enum
->value
= value
;
2455 property
->values
[index
] = value
;
2456 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
2459 EXPORT_SYMBOL(drm_property_add_enum
);
2462 * drm_property_destroy - destroy a drm property
2464 * @property: property to destry
2466 * This function frees a property including any attached resources like
2467 * enumeration values.
2469 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
2471 struct drm_property_enum
*prop_enum
, *pt
;
2473 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
2474 list_del(&prop_enum
->head
);
2478 if (property
->num_values
)
2479 kfree(property
->values
);
2480 drm_mode_object_unregister(dev
, &property
->base
);
2481 list_del(&property
->head
);
2484 EXPORT_SYMBOL(drm_property_destroy
);
2487 * drm_object_attach_property - attach a property to a modeset object
2488 * @obj: drm modeset object
2489 * @property: property to attach
2490 * @init_val: initial value of the property
2492 * This attaches the given property to the modeset object with the given initial
2493 * value. Currently this function cannot fail since the properties are stored in
2494 * a statically sized array.
2496 void drm_object_attach_property(struct drm_mode_object
*obj
,
2497 struct drm_property
*property
,
2500 int count
= obj
->properties
->count
;
2502 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
2503 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2504 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2505 "you see this message on the same object type.\n",
2510 obj
->properties
->properties
[count
] = property
;
2511 obj
->properties
->values
[count
] = init_val
;
2512 obj
->properties
->count
++;
2513 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
2514 obj
->properties
->atomic_count
++;
2516 EXPORT_SYMBOL(drm_object_attach_property
);
2519 * drm_object_property_set_value - set the value of a property
2520 * @obj: drm mode object to set property value for
2521 * @property: property to set
2522 * @val: value the property should be set to
2524 * This functions sets a given property on a given object. This function only
2525 * changes the software state of the property, it does not call into the
2526 * driver's ->set_property callback.
2529 * Zero on success, error code on failure.
2531 int drm_object_property_set_value(struct drm_mode_object
*obj
,
2532 struct drm_property
*property
, uint64_t val
)
2536 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2537 if (obj
->properties
->properties
[i
] == property
) {
2538 obj
->properties
->values
[i
] = val
;
2545 EXPORT_SYMBOL(drm_object_property_set_value
);
2548 * drm_object_property_get_value - retrieve the value of a property
2549 * @obj: drm mode object to get property value from
2550 * @property: property to retrieve
2551 * @val: storage for the property value
2553 * This function retrieves the softare state of the given property for the given
2554 * property. Since there is no driver callback to retrieve the current property
2555 * value this might be out of sync with the hardware, depending upon the driver
2559 * Zero on success, error code on failure.
2561 int drm_object_property_get_value(struct drm_mode_object
*obj
,
2562 struct drm_property
*property
, uint64_t *val
)
2566 /* read-only properties bypass atomic mechanism and still store
2567 * their value in obj->properties->values[].. mostly to avoid
2568 * having to deal w/ EDID and similar props in atomic paths:
2570 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
2571 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
2572 return drm_atomic_get_property(obj
, property
, val
);
2574 for (i
= 0; i
< obj
->properties
->count
; i
++) {
2575 if (obj
->properties
->properties
[i
] == property
) {
2576 *val
= obj
->properties
->values
[i
];
2583 EXPORT_SYMBOL(drm_object_property_get_value
);
2586 * drm_mode_getproperty_ioctl - get the property metadata
2589 * @file_priv: DRM file info
2591 * This function retrieves the metadata for a given property, like the different
2592 * possible values for an enum property or the limits for a range property.
2594 * Blob properties are special
2596 * Called by the user via ioctl.
2599 * Zero on success, negative errno on failure.
2601 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
2602 void *data
, struct drm_file
*file_priv
)
2604 struct drm_mode_get_property
*out_resp
= data
;
2605 struct drm_property
*property
;
2607 int value_count
= 0;
2610 struct drm_property_enum
*prop_enum
;
2611 struct drm_mode_property_enum __user
*enum_ptr
;
2612 uint64_t __user
*values_ptr
;
2614 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2617 drm_modeset_lock_all(dev
);
2618 property
= drm_property_find(dev
, out_resp
->prop_id
);
2624 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
2625 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
2626 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
2630 value_count
= property
->num_values
;
2632 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
2633 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
2634 out_resp
->flags
= property
->flags
;
2636 if ((out_resp
->count_values
>= value_count
) && value_count
) {
2637 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
2638 for (i
= 0; i
< value_count
; i
++) {
2639 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
2645 out_resp
->count_values
= value_count
;
2647 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
2648 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
2649 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
2651 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
2652 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
2654 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
2659 if (copy_to_user(&enum_ptr
[copied
].name
,
2660 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
2667 out_resp
->count_enum_blobs
= enum_count
;
2671 * NOTE: The idea seems to have been to use this to read all the blob
2672 * property values. But nothing ever added them to the corresponding
2673 * list, userspace always used the special-purpose get_blob ioctl to
2674 * read the value for a blob property. It also doesn't make a lot of
2675 * sense to return values here when everything else is just metadata for
2676 * the property itself.
2678 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
2679 out_resp
->count_enum_blobs
= 0;
2681 drm_modeset_unlock_all(dev
);
2685 static void drm_property_free_blob(struct kref
*kref
)
2687 struct drm_property_blob
*blob
=
2688 container_of(kref
, struct drm_property_blob
, base
.refcount
);
2690 mutex_lock(&blob
->dev
->mode_config
.blob_lock
);
2691 list_del(&blob
->head_global
);
2692 mutex_unlock(&blob
->dev
->mode_config
.blob_lock
);
2694 drm_mode_object_unregister(blob
->dev
, &blob
->base
);
2700 * drm_property_create_blob - Create new blob property
2702 * Creates a new blob property for a specified DRM device, optionally
2705 * @dev: DRM device to create property for
2706 * @length: Length to allocate for blob data
2707 * @data: If specified, copies data into blob
2710 * New blob property with a single reference on success, or an ERR_PTR
2713 struct drm_property_blob
*
2714 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
2717 struct drm_property_blob
*blob
;
2720 if (!length
|| length
> ULONG_MAX
- sizeof(struct drm_property_blob
))
2721 return ERR_PTR(-EINVAL
);
2723 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
2725 return ERR_PTR(-ENOMEM
);
2727 /* This must be explicitly initialised, so we can safely call list_del
2728 * on it in the removal handler, even if it isn't in a file list. */
2729 INIT_LIST_HEAD(&blob
->head_file
);
2730 blob
->length
= length
;
2734 memcpy(blob
->data
, data
, length
);
2736 ret
= drm_mode_object_get_reg(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
,
2737 true, drm_property_free_blob
);
2740 return ERR_PTR(-EINVAL
);
2743 mutex_lock(&dev
->mode_config
.blob_lock
);
2744 list_add_tail(&blob
->head_global
,
2745 &dev
->mode_config
.property_blob_list
);
2746 mutex_unlock(&dev
->mode_config
.blob_lock
);
2750 EXPORT_SYMBOL(drm_property_create_blob
);
2753 * drm_property_unreference_blob - Unreference a blob property
2755 * Drop a reference on a blob property. May free the object.
2757 * @blob: Pointer to blob property
2759 void drm_property_unreference_blob(struct drm_property_blob
*blob
)
2764 drm_mode_object_unreference(&blob
->base
);
2766 EXPORT_SYMBOL(drm_property_unreference_blob
);
2769 * drm_property_destroy_user_blobs - destroy all blobs created by this client
2771 * @file_priv: destroy all blobs owned by this file handle
2773 void drm_property_destroy_user_blobs(struct drm_device
*dev
,
2774 struct drm_file
*file_priv
)
2776 struct drm_property_blob
*blob
, *bt
;
2779 * When the file gets released that means no one else can access the
2780 * blob list any more, so no need to grab dev->blob_lock.
2782 list_for_each_entry_safe(blob
, bt
, &file_priv
->blobs
, head_file
) {
2783 list_del_init(&blob
->head_file
);
2784 drm_property_unreference_blob(blob
);
2789 * drm_property_reference_blob - Take a reference on an existing property
2791 * Take a new reference on an existing blob property.
2793 * @blob: Pointer to blob property
2795 struct drm_property_blob
*drm_property_reference_blob(struct drm_property_blob
*blob
)
2797 drm_mode_object_reference(&blob
->base
);
2800 EXPORT_SYMBOL(drm_property_reference_blob
);
2803 * drm_property_lookup_blob - look up a blob property and take a reference
2805 * @id: id of the blob property
2807 * If successful, this takes an additional reference to the blob property.
2808 * callers need to make sure to eventually unreference the returned property
2809 * again, using @drm_property_unreference_blob.
2811 struct drm_property_blob
*drm_property_lookup_blob(struct drm_device
*dev
,
2814 struct drm_mode_object
*obj
;
2815 struct drm_property_blob
*blob
= NULL
;
2817 obj
= __drm_mode_object_find(dev
, id
, DRM_MODE_OBJECT_BLOB
);
2819 blob
= obj_to_blob(obj
);
2822 EXPORT_SYMBOL(drm_property_lookup_blob
);
2825 * drm_property_replace_global_blob - atomically replace existing blob property
2827 * @replace: location of blob property pointer to be replaced
2828 * @length: length of data for new blob, or 0 for no data
2829 * @data: content for new blob, or NULL for no data
2830 * @obj_holds_id: optional object for property holding blob ID
2831 * @prop_holds_id: optional property holding blob ID
2832 * @return 0 on success or error on failure
2834 * This function will atomically replace a global property in the blob list,
2835 * optionally updating a property which holds the ID of that property. It is
2836 * guaranteed to be atomic: no caller will be allowed to see intermediate
2837 * results, and either the entire operation will succeed and clean up the
2838 * previous property, or it will fail and the state will be unchanged.
2840 * If length is 0 or data is NULL, no new blob will be created, and the holding
2841 * property, if specified, will be set to 0.
2843 * Access to the replace pointer is assumed to be protected by the caller, e.g.
2844 * by holding the relevant modesetting object lock for its parent.
2846 * For example, a drm_connector has a 'PATH' property, which contains the ID
2847 * of a blob property with the value of the MST path information. Calling this
2848 * function with replace pointing to the connector's path_blob_ptr, length and
2849 * data set for the new path information, obj_holds_id set to the connector's
2850 * base object, and prop_holds_id set to the path property name, will perform
2851 * a completely atomic update. The access to path_blob_ptr is protected by the
2852 * caller holding a lock on the connector.
2854 int drm_property_replace_global_blob(struct drm_device
*dev
,
2855 struct drm_property_blob
**replace
,
2858 struct drm_mode_object
*obj_holds_id
,
2859 struct drm_property
*prop_holds_id
)
2861 struct drm_property_blob
*new_blob
= NULL
;
2862 struct drm_property_blob
*old_blob
= NULL
;
2865 WARN_ON(replace
== NULL
);
2867 old_blob
= *replace
;
2869 if (length
&& data
) {
2870 new_blob
= drm_property_create_blob(dev
, length
, data
);
2871 if (IS_ERR(new_blob
))
2872 return PTR_ERR(new_blob
);
2875 /* This does not need to be synchronised with blob_lock, as the
2876 * get_properties ioctl locks all modesetting objects, and
2877 * obj_holds_id must be locked before calling here, so we cannot
2878 * have its value out of sync with the list membership modified
2879 * below under blob_lock. */
2881 ret
= drm_object_property_set_value(obj_holds_id
,
2884 new_blob
->base
.id
: 0);
2889 drm_property_unreference_blob(old_blob
);
2890 *replace
= new_blob
;
2895 drm_property_unreference_blob(new_blob
);
2898 EXPORT_SYMBOL(drm_property_replace_global_blob
);
2901 * drm_mode_getblob_ioctl - get the contents of a blob property value
2904 * @file_priv: DRM file info
2906 * This function retrieves the contents of a blob property. The value stored in
2907 * an object's blob property is just a normal modeset object id.
2909 * Called by the user via ioctl.
2912 * Zero on success, negative errno on failure.
2914 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
2915 void *data
, struct drm_file
*file_priv
)
2917 struct drm_mode_get_blob
*out_resp
= data
;
2918 struct drm_property_blob
*blob
;
2920 void __user
*blob_ptr
;
2922 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2925 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
2929 if (out_resp
->length
== blob
->length
) {
2930 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
2931 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
2936 out_resp
->length
= blob
->length
;
2938 drm_property_unreference_blob(blob
);
2944 * drm_mode_createblob_ioctl - create a new blob property
2947 * @file_priv: DRM file info
2949 * This function creates a new blob property with user-defined values. In order
2950 * to give us sensible validation and checking when creating, rather than at
2951 * every potential use, we also require a type to be provided upfront.
2953 * Called by the user via ioctl.
2956 * Zero on success, negative errno on failure.
2958 int drm_mode_createblob_ioctl(struct drm_device
*dev
,
2959 void *data
, struct drm_file
*file_priv
)
2961 struct drm_mode_create_blob
*out_resp
= data
;
2962 struct drm_property_blob
*blob
;
2963 void __user
*blob_ptr
;
2966 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2969 blob
= drm_property_create_blob(dev
, out_resp
->length
, NULL
);
2971 return PTR_ERR(blob
);
2973 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
2974 if (copy_from_user(blob
->data
, blob_ptr
, out_resp
->length
)) {
2979 /* Dropping the lock between create_blob and our access here is safe
2980 * as only the same file_priv can remove the blob; at this point, it is
2981 * not associated with any file_priv. */
2982 mutex_lock(&dev
->mode_config
.blob_lock
);
2983 out_resp
->blob_id
= blob
->base
.id
;
2984 list_add_tail(&blob
->head_file
, &file_priv
->blobs
);
2985 mutex_unlock(&dev
->mode_config
.blob_lock
);
2990 drm_property_unreference_blob(blob
);
2995 * drm_mode_destroyblob_ioctl - destroy a user blob property
2998 * @file_priv: DRM file info
3000 * Destroy an existing user-defined blob property.
3002 * Called by the user via ioctl.
3005 * Zero on success, negative errno on failure.
3007 int drm_mode_destroyblob_ioctl(struct drm_device
*dev
,
3008 void *data
, struct drm_file
*file_priv
)
3010 struct drm_mode_destroy_blob
*out_resp
= data
;
3011 struct drm_property_blob
*blob
= NULL
, *bt
;
3015 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3018 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
3022 mutex_lock(&dev
->mode_config
.blob_lock
);
3023 /* Ensure the property was actually created by this user. */
3024 list_for_each_entry(bt
, &file_priv
->blobs
, head_file
) {
3036 /* We must drop head_file here, because we may not be the last
3037 * reference on the blob. */
3038 list_del_init(&blob
->head_file
);
3039 mutex_unlock(&dev
->mode_config
.blob_lock
);
3041 /* One reference from lookup, and one from the filp. */
3042 drm_property_unreference_blob(blob
);
3043 drm_property_unreference_blob(blob
);
3048 mutex_unlock(&dev
->mode_config
.blob_lock
);
3049 drm_property_unreference_blob(blob
);
3054 /* Some properties could refer to dynamic refcnt'd objects, or things that
3055 * need special locking to handle lifetime issues (ie. to ensure the prop
3056 * value doesn't become invalid part way through the property update due to
3057 * race). The value returned by reference via 'obj' should be passed back
3058 * to drm_property_change_valid_put() after the property is set (and the
3059 * object to which the property is attached has a chance to take it's own
3062 bool drm_property_change_valid_get(struct drm_property
*property
,
3063 uint64_t value
, struct drm_mode_object
**ref
)
3067 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3072 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3073 if (value
< property
->values
[0] || value
> property
->values
[1])
3076 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3077 int64_t svalue
= U642I64(value
);
3079 if (svalue
< U642I64(property
->values
[0]) ||
3080 svalue
> U642I64(property
->values
[1]))
3083 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3084 uint64_t valid_mask
= 0;
3086 for (i
= 0; i
< property
->num_values
; i
++)
3087 valid_mask
|= (1ULL << property
->values
[i
]);
3088 return !(value
& ~valid_mask
);
3089 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3090 struct drm_property_blob
*blob
;
3095 blob
= drm_property_lookup_blob(property
->dev
, value
);
3102 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3103 /* a zero value for an object property translates to null: */
3107 *ref
= __drm_mode_object_find(property
->dev
, value
,
3108 property
->values
[0]);
3109 return *ref
!= NULL
;
3112 for (i
= 0; i
< property
->num_values
; i
++)
3113 if (property
->values
[i
] == value
)
3118 void drm_property_change_valid_put(struct drm_property
*property
,
3119 struct drm_mode_object
*ref
)
3124 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3125 drm_mode_object_unreference(ref
);
3126 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
3127 drm_property_unreference_blob(obj_to_blob(ref
));
3130 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3131 struct drm_property
*property
,
3135 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3137 if (crtc
->funcs
->set_property
)
3138 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3140 drm_object_property_set_value(obj
, property
, value
);
3146 * drm_mode_plane_set_obj_prop - set the value of a property
3147 * @plane: drm plane object to set property value for
3148 * @property: property to set
3149 * @value: value the property should be set to
3151 * This functions sets a given property on a given plane object. This function
3152 * calls the driver's ->set_property callback and changes the software state of
3153 * the property if the callback succeeds.
3156 * Zero on success, error code on failure.
3158 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
3159 struct drm_property
*property
,
3163 struct drm_mode_object
*obj
= &plane
->base
;
3165 if (plane
->funcs
->set_property
)
3166 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3168 drm_object_property_set_value(obj
, property
, value
);
3172 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
3175 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
3178 * @file_priv: DRM file info
3180 * This function retrieves the current value for an object's property. Compared
3181 * to the connector specific ioctl this one is extended to also work on crtc and
3184 * Called by the user via ioctl.
3187 * Zero on success, negative errno on failure.
3189 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3190 struct drm_file
*file_priv
)
3192 struct drm_mode_obj_get_properties
*arg
= data
;
3193 struct drm_mode_object
*obj
;
3196 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3199 drm_modeset_lock_all(dev
);
3201 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3206 if (!obj
->properties
) {
3211 ret
= drm_mode_object_get_properties(obj
, file_priv
->atomic
,
3212 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
3213 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
3217 drm_mode_object_unreference(obj
);
3219 drm_modeset_unlock_all(dev
);
3223 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
3224 struct drm_file
*file_priv
)
3226 struct drm_mode_obj_set_property
*arg
= data
;
3227 struct drm_mode_object
*arg_obj
;
3228 struct drm_mode_object
*prop_obj
;
3229 struct drm_property
*property
;
3230 int i
, ret
= -EINVAL
;
3231 struct drm_mode_object
*ref
;
3233 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3236 drm_modeset_lock_all(dev
);
3238 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3243 if (!arg_obj
->properties
)
3246 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
3247 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
3250 if (i
== arg_obj
->properties
->count
)
3253 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
3254 DRM_MODE_OBJECT_PROPERTY
);
3259 property
= obj_to_property(prop_obj
);
3261 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
3264 switch (arg_obj
->type
) {
3265 case DRM_MODE_OBJECT_CONNECTOR
:
3266 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
3269 case DRM_MODE_OBJECT_CRTC
:
3270 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
3272 case DRM_MODE_OBJECT_PLANE
:
3273 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
3274 property
, arg
->value
);
3278 drm_property_change_valid_put(property
, ref
);
3281 drm_mode_object_unreference(arg_obj
);
3283 drm_modeset_unlock_all(dev
);
3288 * drm_mode_crtc_set_gamma_size - set the gamma table size
3289 * @crtc: CRTC to set the gamma table size for
3290 * @gamma_size: size of the gamma table
3292 * Drivers which support gamma tables should set this to the supported gamma
3293 * table size when initializing the CRTC. Currently the drm core only supports a
3294 * fixed gamma table size.
3297 * Zero on success, negative errno on failure.
3299 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
3302 uint16_t *r_base
, *g_base
, *b_base
;
3305 crtc
->gamma_size
= gamma_size
;
3307 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
3309 if (!crtc
->gamma_store
) {
3310 crtc
->gamma_size
= 0;
3314 r_base
= crtc
->gamma_store
;
3315 g_base
= r_base
+ gamma_size
;
3316 b_base
= g_base
+ gamma_size
;
3317 for (i
= 0; i
< gamma_size
; i
++) {
3326 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
3329 * drm_mode_gamma_set_ioctl - set the gamma table
3332 * @file_priv: DRM file info
3334 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3335 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3337 * Called by the user via ioctl.
3340 * Zero on success, negative errno on failure.
3342 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
3343 void *data
, struct drm_file
*file_priv
)
3345 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3346 struct drm_crtc
*crtc
;
3347 void *r_base
, *g_base
, *b_base
;
3351 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3354 drm_modeset_lock_all(dev
);
3355 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
3361 if (crtc
->funcs
->gamma_set
== NULL
) {
3366 /* memcpy into gamma store */
3367 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3372 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3373 r_base
= crtc
->gamma_store
;
3374 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
3379 g_base
= r_base
+ size
;
3380 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
3385 b_base
= g_base
+ size
;
3386 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
3391 ret
= crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, crtc
->gamma_size
);
3394 drm_modeset_unlock_all(dev
);
3400 * drm_mode_gamma_get_ioctl - get the gamma table
3403 * @file_priv: DRM file info
3405 * Copy the current gamma table into the storage provided. This also provides
3406 * the gamma table size the driver expects, which can be used to size the
3407 * allocated storage.
3409 * Called by the user via ioctl.
3412 * Zero on success, negative errno on failure.
3414 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
3415 void *data
, struct drm_file
*file_priv
)
3417 struct drm_mode_crtc_lut
*crtc_lut
= data
;
3418 struct drm_crtc
*crtc
;
3419 void *r_base
, *g_base
, *b_base
;
3423 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3426 drm_modeset_lock_all(dev
);
3427 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
3433 /* memcpy into gamma store */
3434 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
3439 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
3440 r_base
= crtc
->gamma_store
;
3441 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
3446 g_base
= r_base
+ size
;
3447 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
3452 b_base
= g_base
+ size
;
3453 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
3458 drm_modeset_unlock_all(dev
);
3463 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
3466 * @file_priv: DRM file info
3468 * This schedules an asynchronous update on a given CRTC, called page flip.
3469 * Optionally a drm event is generated to signal the completion of the event.
3470 * Generic drivers cannot assume that a pageflip with changed framebuffer
3471 * properties (including driver specific metadata like tiling layout) will work,
3472 * but some drivers support e.g. pixel format changes through the pageflip
3475 * Called by the user via ioctl.
3478 * Zero on success, negative errno on failure.
3480 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
3481 void *data
, struct drm_file
*file_priv
)
3483 struct drm_mode_crtc_page_flip
*page_flip
= data
;
3484 struct drm_crtc
*crtc
;
3485 struct drm_framebuffer
*fb
= NULL
;
3486 struct drm_pending_vblank_event
*e
= NULL
;
3489 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
3490 page_flip
->reserved
!= 0)
3493 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
3496 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
3500 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
3501 if (crtc
->primary
->fb
== NULL
) {
3502 /* The framebuffer is currently unbound, presumably
3503 * due to a hotplug event, that userspace has not
3510 if (crtc
->funcs
->page_flip
== NULL
)
3513 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
3520 const struct drm_plane_state
*state
= crtc
->primary
->state
;
3522 ret
= check_src_coords(state
->src_x
, state
->src_y
,
3523 state
->src_w
, state
->src_h
, fb
);
3525 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
3530 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
3531 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3536 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
3537 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
3542 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
3543 e
->event
.base
.length
= sizeof(e
->event
);
3544 e
->event
.user_data
= page_flip
->user_data
;
3545 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
, &e
->event
.base
);
3552 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
3553 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
3555 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
)
3556 drm_event_cancel_free(dev
, &e
->base
);
3557 /* Keep the old fb, don't unref it. */
3558 crtc
->primary
->old_fb
= NULL
;
3560 crtc
->primary
->fb
= fb
;
3561 /* Unref only the old framebuffer. */
3567 drm_framebuffer_unreference(fb
);
3568 if (crtc
->primary
->old_fb
)
3569 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
3570 crtc
->primary
->old_fb
= NULL
;
3571 drm_modeset_unlock_crtc(crtc
);
3577 * drm_mode_config_reset - call ->reset callbacks
3580 * This functions calls all the crtc's, encoder's and connector's ->reset
3581 * callback. Drivers can use this in e.g. their driver load or resume code to
3582 * reset hardware and software state.
3584 void drm_mode_config_reset(struct drm_device
*dev
)
3586 struct drm_crtc
*crtc
;
3587 struct drm_plane
*plane
;
3588 struct drm_encoder
*encoder
;
3589 struct drm_connector
*connector
;
3591 drm_for_each_plane(plane
, dev
)
3592 if (plane
->funcs
->reset
)
3593 plane
->funcs
->reset(plane
);
3595 drm_for_each_crtc(crtc
, dev
)
3596 if (crtc
->funcs
->reset
)
3597 crtc
->funcs
->reset(crtc
);
3599 drm_for_each_encoder(encoder
, dev
)
3600 if (encoder
->funcs
->reset
)
3601 encoder
->funcs
->reset(encoder
);
3603 mutex_lock(&dev
->mode_config
.mutex
);
3604 drm_for_each_connector(connector
, dev
)
3605 if (connector
->funcs
->reset
)
3606 connector
->funcs
->reset(connector
);
3607 mutex_unlock(&dev
->mode_config
.mutex
);
3609 EXPORT_SYMBOL(drm_mode_config_reset
);
3612 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
3615 * @file_priv: DRM file info
3617 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
3618 * TTM or something else entirely) and returns the resulting buffer handle. This
3619 * handle can then be wrapped up into a framebuffer modeset object.
3621 * Note that userspace is not allowed to use such objects for render
3622 * acceleration - drivers must create their own private ioctls for such a use
3625 * Called by the user via ioctl.
3628 * Zero on success, negative errno on failure.
3630 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
3631 void *data
, struct drm_file
*file_priv
)
3633 struct drm_mode_create_dumb
*args
= data
;
3634 u32 cpp
, stride
, size
;
3636 if (!dev
->driver
->dumb_create
)
3638 if (!args
->width
|| !args
->height
|| !args
->bpp
)
3641 /* overflow checks for 32bit size calculations */
3642 /* NOTE: DIV_ROUND_UP() can overflow */
3643 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
3644 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
3646 stride
= cpp
* args
->width
;
3647 if (args
->height
> 0xffffffffU
/ stride
)
3650 /* test for wrap-around */
3651 size
= args
->height
* stride
;
3652 if (PAGE_ALIGN(size
) == 0)
3656 * handle, pitch and size are output parameters. Zero them out to
3657 * prevent drivers from accidentally using uninitialized data. Since
3658 * not all existing userspace is clearing these fields properly we
3659 * cannot reject IOCTL with garbage in them.
3665 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
3669 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
3672 * @file_priv: DRM file info
3674 * Allocate an offset in the drm device node's address space to be able to
3675 * memory map a dumb buffer.
3677 * Called by the user via ioctl.
3680 * Zero on success, negative errno on failure.
3682 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
3683 void *data
, struct drm_file
*file_priv
)
3685 struct drm_mode_map_dumb
*args
= data
;
3687 /* call driver ioctl to get mmap offset */
3688 if (!dev
->driver
->dumb_map_offset
)
3691 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
3695 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
3698 * @file_priv: DRM file info
3700 * This destroys the userspace handle for the given dumb backing storage buffer.
3701 * Since buffer objects must be reference counted in the kernel a buffer object
3702 * won't be immediately freed if a framebuffer modeset object still uses it.
3704 * Called by the user via ioctl.
3707 * Zero on success, negative errno on failure.
3709 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
3710 void *data
, struct drm_file
*file_priv
)
3712 struct drm_mode_destroy_dumb
*args
= data
;
3714 if (!dev
->driver
->dumb_destroy
)
3717 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
3721 * drm_rotation_simplify() - Try to simplify the rotation
3722 * @rotation: Rotation to be simplified
3723 * @supported_rotations: Supported rotations
3725 * Attempt to simplify the rotation to a form that is supported.
3726 * Eg. if the hardware supports everything except DRM_REFLECT_X
3727 * one could call this function like this:
3729 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
3730 * DRM_ROTATE_90 | DRM_ROTATE_180 |
3731 * DRM_ROTATE_270 | DRM_REFLECT_Y);
3733 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
3734 * transforms the hardware supports, this function may not
3735 * be able to produce a supported transform, so the caller should
3736 * check the result afterwards.
3738 unsigned int drm_rotation_simplify(unsigned int rotation
,
3739 unsigned int supported_rotations
)
3741 if (rotation
& ~supported_rotations
) {
3742 rotation
^= DRM_REFLECT_X
| DRM_REFLECT_Y
;
3743 rotation
= (rotation
& DRM_REFLECT_MASK
) |
3744 BIT((ffs(rotation
& DRM_ROTATE_MASK
) + 1) % 4);
3749 EXPORT_SYMBOL(drm_rotation_simplify
);
3752 * drm_mode_config_init - initialize DRM mode_configuration structure
3755 * Initialize @dev's mode_config structure, used for tracking the graphics
3756 * configuration of @dev.
3758 * Since this initializes the modeset locks, no locking is possible. Which is no
3759 * problem, since this should happen single threaded at init time. It is the
3760 * driver's problem to ensure this guarantee.
3763 void drm_mode_config_init(struct drm_device
*dev
)
3765 mutex_init(&dev
->mode_config
.mutex
);
3766 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
3767 mutex_init(&dev
->mode_config
.idr_mutex
);
3768 mutex_init(&dev
->mode_config
.fb_lock
);
3769 mutex_init(&dev
->mode_config
.blob_lock
);
3770 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
3771 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
3772 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
3773 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
3774 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
3775 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
3776 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
3777 idr_init(&dev
->mode_config
.crtc_idr
);
3778 idr_init(&dev
->mode_config
.tile_idr
);
3779 ida_init(&dev
->mode_config
.connector_ida
);
3781 drm_modeset_lock_all(dev
);
3782 drm_mode_create_standard_properties(dev
);
3783 drm_modeset_unlock_all(dev
);
3785 /* Just to be sure */
3786 dev
->mode_config
.num_fb
= 0;
3787 dev
->mode_config
.num_connector
= 0;
3788 dev
->mode_config
.num_crtc
= 0;
3789 dev
->mode_config
.num_encoder
= 0;
3790 dev
->mode_config
.num_overlay_plane
= 0;
3791 dev
->mode_config
.num_total_plane
= 0;
3793 EXPORT_SYMBOL(drm_mode_config_init
);
3796 * drm_mode_config_cleanup - free up DRM mode_config info
3799 * Free up all the connectors and CRTCs associated with this DRM device, then
3800 * free up the framebuffers and associated buffer objects.
3802 * Note that since this /should/ happen single-threaded at driver/device
3803 * teardown time, no locking is required. It's the driver's job to ensure that
3804 * this guarantee actually holds true.
3806 * FIXME: cleanup any dangling user buffer objects too
3808 void drm_mode_config_cleanup(struct drm_device
*dev
)
3810 struct drm_connector
*connector
, *ot
;
3811 struct drm_crtc
*crtc
, *ct
;
3812 struct drm_encoder
*encoder
, *enct
;
3813 struct drm_framebuffer
*fb
, *fbt
;
3814 struct drm_property
*property
, *pt
;
3815 struct drm_property_blob
*blob
, *bt
;
3816 struct drm_plane
*plane
, *plt
;
3818 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
3820 encoder
->funcs
->destroy(encoder
);
3823 list_for_each_entry_safe(connector
, ot
,
3824 &dev
->mode_config
.connector_list
, head
) {
3825 connector
->funcs
->destroy(connector
);
3828 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
3830 drm_property_destroy(dev
, property
);
3833 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
3835 plane
->funcs
->destroy(plane
);
3838 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
3839 crtc
->funcs
->destroy(crtc
);
3842 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
3844 drm_property_unreference_blob(blob
);
3848 * Single-threaded teardown context, so it's not required to grab the
3849 * fb_lock to protect against concurrent fb_list access. Contrary, it
3850 * would actually deadlock with the drm_framebuffer_cleanup function.
3852 * Also, if there are any framebuffers left, that's a driver leak now,
3853 * so politely WARN about this.
3855 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
3856 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
3857 drm_framebuffer_free(&fb
->base
.refcount
);
3860 ida_destroy(&dev
->mode_config
.connector_ida
);
3861 idr_destroy(&dev
->mode_config
.tile_idr
);
3862 idr_destroy(&dev
->mode_config
.crtc_idr
);
3863 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
3865 EXPORT_SYMBOL(drm_mode_config_cleanup
);
3867 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
3868 unsigned int supported_rotations
)
3870 static const struct drm_prop_enum_list props
[] = {
3871 { __builtin_ffs(DRM_ROTATE_0
) - 1, "rotate-0" },
3872 { __builtin_ffs(DRM_ROTATE_90
) - 1, "rotate-90" },
3873 { __builtin_ffs(DRM_ROTATE_180
) - 1, "rotate-180" },
3874 { __builtin_ffs(DRM_ROTATE_270
) - 1, "rotate-270" },
3875 { __builtin_ffs(DRM_REFLECT_X
) - 1, "reflect-x" },
3876 { __builtin_ffs(DRM_REFLECT_Y
) - 1, "reflect-y" },
3879 return drm_property_create_bitmask(dev
, 0, "rotation",
3880 props
, ARRAY_SIZE(props
),
3881 supported_rotations
);
3883 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
3888 * Tile groups are used to represent tiled monitors with a unique
3889 * integer identifier. Tiled monitors using DisplayID v1.3 have
3890 * a unique 8-byte handle, we store this in a tile group, so we
3891 * have a common identifier for all tiles in a monitor group.
3893 static void drm_tile_group_free(struct kref
*kref
)
3895 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
3896 struct drm_device
*dev
= tg
->dev
;
3897 mutex_lock(&dev
->mode_config
.idr_mutex
);
3898 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
3899 mutex_unlock(&dev
->mode_config
.idr_mutex
);
3904 * drm_mode_put_tile_group - drop a reference to a tile group.
3906 * @tg: tile group to drop reference to.
3908 * drop reference to tile group and free if 0.
3910 void drm_mode_put_tile_group(struct drm_device
*dev
,
3911 struct drm_tile_group
*tg
)
3913 kref_put(&tg
->refcount
, drm_tile_group_free
);
3917 * drm_mode_get_tile_group - get a reference to an existing tile group
3919 * @topology: 8-bytes unique per monitor.
3921 * Use the unique bytes to get a reference to an existing tile group.
3924 * tile group or NULL if not found.
3926 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
3929 struct drm_tile_group
*tg
;
3931 mutex_lock(&dev
->mode_config
.idr_mutex
);
3932 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
3933 if (!memcmp(tg
->group_data
, topology
, 8)) {
3934 if (!kref_get_unless_zero(&tg
->refcount
))
3936 mutex_unlock(&dev
->mode_config
.idr_mutex
);
3940 mutex_unlock(&dev
->mode_config
.idr_mutex
);
3943 EXPORT_SYMBOL(drm_mode_get_tile_group
);
3946 * drm_mode_create_tile_group - create a tile group from a displayid description
3948 * @topology: 8-bytes unique per monitor.
3950 * Create a tile group for the unique monitor, and get a unique
3951 * identifier for the tile group.
3954 * new tile group or error.
3956 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
3959 struct drm_tile_group
*tg
;
3962 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
3964 return ERR_PTR(-ENOMEM
);
3966 kref_init(&tg
->refcount
);
3967 memcpy(tg
->group_data
, topology
, 8);
3970 mutex_lock(&dev
->mode_config
.idr_mutex
);
3971 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
3979 mutex_unlock(&dev
->mode_config
.idr_mutex
);
3982 EXPORT_SYMBOL(drm_mode_create_tile_group
);
3985 * drm_crtc_enable_color_mgmt - enable color management properties
3987 * @degamma_lut_size: the size of the degamma lut (before CSC)
3988 * @has_ctm: whether to attach ctm_property for CSC matrix
3989 * @gamma_lut_size: the size of the gamma lut (after CSC)
3991 * This function lets the driver enable the color correction
3992 * properties on a CRTC. This includes 3 degamma, csc and gamma
3993 * properties that userspace can set and 2 size properties to inform
3994 * the userspace of the lut sizes. Each of the properties are
3995 * optional. The gamma and degamma properties are only attached if
3996 * their size is not 0 and ctm_property is only attached if has_ctm is
3999 void drm_crtc_enable_color_mgmt(struct drm_crtc
*crtc
,
4000 uint degamma_lut_size
,
4002 uint gamma_lut_size
)
4004 struct drm_device
*dev
= crtc
->dev
;
4005 struct drm_mode_config
*config
= &dev
->mode_config
;
4007 if (degamma_lut_size
) {
4008 drm_object_attach_property(&crtc
->base
,
4009 config
->degamma_lut_property
, 0);
4010 drm_object_attach_property(&crtc
->base
,
4011 config
->degamma_lut_size_property
,
4016 drm_object_attach_property(&crtc
->base
,
4017 config
->ctm_property
, 0);
4019 if (gamma_lut_size
) {
4020 drm_object_attach_property(&crtc
->base
,
4021 config
->gamma_lut_property
, 0);
4022 drm_object_attach_property(&crtc
->base
,
4023 config
->gamma_lut_size_property
,
4027 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt
);