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>
44 #include "drm_crtc_internal.h"
45 #include "drm_internal.h"
47 static struct drm_framebuffer
*
48 internal_framebuffer_create(struct drm_device
*dev
,
49 const struct drm_mode_fb_cmd2
*r
,
50 struct drm_file
*file_priv
);
52 /* Avoid boilerplate. I'm tired of typing. */
53 #define DRM_ENUM_NAME_FN(fnname, list) \
54 const char *fnname(int val) \
57 for (i = 0; i < ARRAY_SIZE(list); i++) { \
58 if (list[i].type == val) \
59 return list[i].name; \
67 static const struct drm_prop_enum_list drm_dpms_enum_list
[] = {
68 { DRM_MODE_DPMS_ON
, "On" },
69 { DRM_MODE_DPMS_STANDBY
, "Standby" },
70 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
71 { DRM_MODE_DPMS_OFF
, "Off" }
74 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
76 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] = {
77 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
78 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
79 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
85 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] = {
86 { DRM_MODE_SCALE_NONE
, "None" },
87 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
88 { DRM_MODE_SCALE_CENTER
, "Center" },
89 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
92 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list
[] = {
93 { DRM_MODE_PICTURE_ASPECT_NONE
, "Automatic" },
94 { DRM_MODE_PICTURE_ASPECT_4_3
, "4:3" },
95 { DRM_MODE_PICTURE_ASPECT_16_9
, "16:9" },
99 * Non-global properties, but "required" for certain connectors.
101 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] = {
102 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
107 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
109 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] = {
110 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
115 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
116 drm_dvi_i_subconnector_enum_list
)
118 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] = {
119 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
126 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
128 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] = {
129 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
136 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
137 drm_tv_subconnector_enum_list
)
139 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
140 { DRM_MODE_DIRTY_OFF
, "Off" },
141 { DRM_MODE_DIRTY_ON
, "On" },
142 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
145 struct drm_conn_prop_enum_list
{
152 * Connector and encoder types.
154 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] = {
155 { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
156 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
157 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
158 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
159 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
160 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
161 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
162 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
163 { DRM_MODE_CONNECTOR_Component
, "Component" },
164 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
165 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
166 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
167 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
168 { DRM_MODE_CONNECTOR_TV
, "TV" },
169 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
170 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
171 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
172 { DRM_MODE_CONNECTOR_DPI
, "DPI" },
175 static const struct drm_prop_enum_list drm_encoder_enum_list
[] = {
176 { DRM_MODE_ENCODER_NONE
, "None" },
177 { DRM_MODE_ENCODER_DAC
, "DAC" },
178 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
179 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
180 { DRM_MODE_ENCODER_TVDAC
, "TV" },
181 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
182 { DRM_MODE_ENCODER_DSI
, "DSI" },
183 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
184 { DRM_MODE_ENCODER_DPI
, "DPI" },
187 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] = {
188 { SubPixelUnknown
, "Unknown" },
189 { SubPixelHorizontalRGB
, "Horizontal RGB" },
190 { SubPixelHorizontalBGR
, "Horizontal BGR" },
191 { SubPixelVerticalRGB
, "Vertical RGB" },
192 { SubPixelVerticalBGR
, "Vertical BGR" },
193 { SubPixelNone
, "None" },
196 void drm_connector_ida_init(void)
200 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
201 ida_init(&drm_connector_enum_list
[i
].ida
);
204 void drm_connector_ida_destroy(void)
208 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
209 ida_destroy(&drm_connector_enum_list
[i
].ida
);
213 * drm_get_connector_status_name - return a string for connector status
214 * @status: connector status to compute name of
216 * In contrast to the other drm_get_*_name functions this one here returns a
217 * const pointer and hence is threadsafe.
219 const char *drm_get_connector_status_name(enum drm_connector_status status
)
221 if (status
== connector_status_connected
)
223 else if (status
== connector_status_disconnected
)
224 return "disconnected";
228 EXPORT_SYMBOL(drm_get_connector_status_name
);
231 * drm_get_subpixel_order_name - return a string for a given subpixel enum
232 * @order: enum of subpixel_order
234 * Note you could abuse this and return something out of bounds, but that
235 * would be a caller error. No unscrubbed user data should make it here.
237 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
239 return drm_subpixel_enum_list
[order
].name
;
241 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
244 * Internal function to assign a slot in the object idr and optionally
245 * register the object into the idr.
247 static int drm_mode_object_get_reg(struct drm_device
*dev
,
248 struct drm_mode_object
*obj
,
251 void (*obj_free_cb
)(struct kref
*kref
))
255 mutex_lock(&dev
->mode_config
.idr_mutex
);
256 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
259 * Set up the object linking under the protection of the idr
260 * lock so that other users can't see inconsistent state.
263 obj
->type
= obj_type
;
265 obj
->free_cb
= obj_free_cb
;
266 kref_init(&obj
->refcount
);
269 mutex_unlock(&dev
->mode_config
.idr_mutex
);
271 return ret
< 0 ? ret
: 0;
275 * drm_mode_object_get - allocate a new modeset identifier
277 * @obj: object pointer, used to generate unique ID
278 * @obj_type: object type
280 * Create a unique identifier based on @ptr in @dev's identifier space. Used
281 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
282 * modeset identifiers are _not_ reference counted. Hence don't use this for
283 * reference counted modeset objects like framebuffers.
286 * Zero on success, error code on failure.
288 int drm_mode_object_get(struct drm_device
*dev
,
289 struct drm_mode_object
*obj
, uint32_t obj_type
)
291 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true, NULL
);
294 static void drm_mode_object_register(struct drm_device
*dev
,
295 struct drm_mode_object
*obj
)
297 mutex_lock(&dev
->mode_config
.idr_mutex
);
298 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
299 mutex_unlock(&dev
->mode_config
.idr_mutex
);
303 * drm_mode_object_unregister - free a modeset identifer
305 * @object: object to free
307 * Free @id from @dev's unique identifier pool.
308 * This function can be called multiple times, and guards against
310 * These modeset identifiers are _not_ reference counted. Hence don't use this
311 * for reference counted modeset objects like framebuffers.
313 void drm_mode_object_unregister(struct drm_device
*dev
,
314 struct drm_mode_object
*object
)
316 mutex_lock(&dev
->mode_config
.idr_mutex
);
318 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
321 mutex_unlock(&dev
->mode_config
.idr_mutex
);
324 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
325 uint32_t id
, uint32_t type
)
327 struct drm_mode_object
*obj
= NULL
;
329 mutex_lock(&dev
->mode_config
.idr_mutex
);
330 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
331 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
333 if (obj
&& obj
->id
!= id
)
336 if (obj
&& obj
->free_cb
) {
337 if (!kref_get_unless_zero(&obj
->refcount
))
340 mutex_unlock(&dev
->mode_config
.idr_mutex
);
346 * drm_mode_object_find - look up a drm object with static lifetime
348 * @id: id of the mode object
349 * @type: type of the mode object
351 * This function is used to look up a modeset object. It will acquire a
352 * reference for reference counted objects. This reference must be dropped again
353 * by callind drm_mode_object_unreference().
355 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
356 uint32_t id
, uint32_t type
)
358 struct drm_mode_object
*obj
= NULL
;
360 obj
= _object_find(dev
, id
, type
);
363 EXPORT_SYMBOL(drm_mode_object_find
);
366 * drm_mode_object_unreference - decr the object refcnt
369 * This functions decrements the object's refcount if it is a refcounted modeset
370 * object. It is a no-op on any other object. This is used to drop references
371 * acquired with drm_mode_object_reference().
373 void drm_mode_object_unreference(struct drm_mode_object
*obj
)
376 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
377 kref_put(&obj
->refcount
, obj
->free_cb
);
380 EXPORT_SYMBOL(drm_mode_object_unreference
);
383 * drm_mode_object_reference - incr the object refcnt
386 * This functions increments the object's refcount if it is a refcounted modeset
387 * object. It is a no-op on any other object. References should be dropped again
388 * by calling drm_mode_object_unreference().
390 void drm_mode_object_reference(struct drm_mode_object
*obj
)
393 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
394 kref_get(&obj
->refcount
);
397 EXPORT_SYMBOL(drm_mode_object_reference
);
399 static void drm_framebuffer_free(struct kref
*kref
)
401 struct drm_framebuffer
*fb
=
402 container_of(kref
, struct drm_framebuffer
, base
.refcount
);
403 struct drm_device
*dev
= fb
->dev
;
406 * The lookup idr holds a weak reference, which has not necessarily been
407 * removed at this point. Check for that.
409 drm_mode_object_unregister(dev
, &fb
->base
);
411 fb
->funcs
->destroy(fb
);
415 * drm_framebuffer_init - initialize a framebuffer
417 * @fb: framebuffer to be initialized
418 * @funcs: ... with these functions
420 * Allocates an ID for the framebuffer's parent mode object, sets its mode
421 * functions & device file and adds it to the master fd list.
424 * This functions publishes the fb and makes it available for concurrent access
425 * by other users. Which means by this point the fb _must_ be fully set up -
426 * since all the fb attributes are invariant over its lifetime, no further
427 * locking but only correct reference counting is required.
430 * Zero on success, error code on failure.
432 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
433 const struct drm_framebuffer_funcs
*funcs
)
437 INIT_LIST_HEAD(&fb
->filp_head
);
441 ret
= drm_mode_object_get_reg(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
,
442 false, drm_framebuffer_free
);
446 mutex_lock(&dev
->mode_config
.fb_lock
);
447 dev
->mode_config
.num_fb
++;
448 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
449 mutex_unlock(&dev
->mode_config
.fb_lock
);
451 drm_mode_object_register(dev
, &fb
->base
);
455 EXPORT_SYMBOL(drm_framebuffer_init
);
458 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
460 * @id: id of the fb object
462 * If successful, this grabs an additional reference to the framebuffer -
463 * callers need to make sure to eventually unreference the returned framebuffer
464 * again, using @drm_framebuffer_unreference.
466 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
469 struct drm_mode_object
*obj
;
470 struct drm_framebuffer
*fb
= NULL
;
472 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_FB
);
477 EXPORT_SYMBOL(drm_framebuffer_lookup
);
480 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
481 * @fb: fb to unregister
483 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
484 * those used for fbdev. Note that the caller must hold a reference of it's own,
485 * i.e. the object may not be destroyed through this call (since it'll lead to a
486 * locking inversion).
488 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
490 struct drm_device
*dev
;
497 /* Mark fb as reaped and drop idr ref. */
498 drm_mode_object_unregister(dev
, &fb
->base
);
500 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
503 * drm_framebuffer_cleanup - remove a framebuffer object
504 * @fb: framebuffer to remove
506 * Cleanup framebuffer. This function is intended to be used from the drivers
507 * ->destroy callback. It can also be used to clean up driver private
508 * framebuffers embedded into a larger structure.
510 * Note that this function does not remove the fb from active usuage - if it is
511 * still used anywhere, hilarity can ensue since userspace could call getfb on
512 * the id and get back -EINVAL. Obviously no concern at driver unload time.
514 * Also, the framebuffer will not be removed from the lookup idr - for
515 * user-created framebuffers this will happen in in the rmfb ioctl. For
516 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
517 * drm_framebuffer_unregister_private.
519 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
521 struct drm_device
*dev
= fb
->dev
;
523 mutex_lock(&dev
->mode_config
.fb_lock
);
525 dev
->mode_config
.num_fb
--;
526 mutex_unlock(&dev
->mode_config
.fb_lock
);
528 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
531 * drm_framebuffer_remove - remove and unreference a framebuffer object
532 * @fb: framebuffer to remove
534 * Scans all the CRTCs and planes in @dev's mode_config. If they're
535 * using @fb, removes it, setting it to NULL. Then drops the reference to the
536 * passed-in framebuffer. Might take the modeset locks.
538 * Note that this function optimizes the cleanup away if the caller holds the
539 * last reference to the framebuffer. It is also guaranteed to not take the
540 * modeset locks in this case.
542 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
544 struct drm_device
*dev
;
545 struct drm_crtc
*crtc
;
546 struct drm_plane
*plane
;
547 struct drm_mode_set set
;
555 WARN_ON(!list_empty(&fb
->filp_head
));
558 * drm ABI mandates that we remove any deleted framebuffers from active
559 * useage. But since most sane clients only remove framebuffers they no
560 * longer need, try to optimize this away.
562 * Since we're holding a reference ourselves, observing a refcount of 1
563 * means that we're the last holder and can skip it. Also, the refcount
564 * can never increase from 1 again, so we don't need any barriers or
567 * Note that userspace could try to race with use and instate a new
568 * usage _after_ we've cleared all current ones. End result will be an
569 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
572 if (drm_framebuffer_read_refcount(fb
) > 1) {
573 drm_modeset_lock_all(dev
);
574 /* remove from any CRTC */
575 drm_for_each_crtc(crtc
, dev
) {
576 if (crtc
->primary
->fb
== fb
) {
577 /* should turn off the crtc */
578 memset(&set
, 0, sizeof(struct drm_mode_set
));
581 ret
= drm_mode_set_config_internal(&set
);
583 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
587 drm_for_each_plane(plane
, dev
) {
589 drm_plane_force_disable(plane
);
591 drm_modeset_unlock_all(dev
);
594 drm_framebuffer_unreference(fb
);
596 EXPORT_SYMBOL(drm_framebuffer_remove
);
598 DEFINE_WW_CLASS(crtc_ww_class
);
600 static unsigned int drm_num_crtcs(struct drm_device
*dev
)
602 unsigned int num
= 0;
603 struct drm_crtc
*tmp
;
605 drm_for_each_crtc(tmp
, dev
) {
612 static int drm_crtc_register_all(struct drm_device
*dev
)
614 struct drm_crtc
*crtc
;
617 drm_for_each_crtc(crtc
, dev
) {
618 if (crtc
->funcs
->late_register
)
619 ret
= crtc
->funcs
->late_register(crtc
);
627 static void drm_crtc_unregister_all(struct drm_device
*dev
)
629 struct drm_crtc
*crtc
;
631 drm_for_each_crtc(crtc
, dev
) {
632 if (crtc
->funcs
->early_unregister
)
633 crtc
->funcs
->early_unregister(crtc
);
638 * drm_crtc_init_with_planes - Initialise a new CRTC object with
639 * specified primary and cursor planes.
641 * @crtc: CRTC object to init
642 * @primary: Primary plane for CRTC
643 * @cursor: Cursor plane for CRTC
644 * @funcs: callbacks for the new CRTC
645 * @name: printf style format string for the CRTC name, or NULL for default name
647 * Inits a new object created as base part of a driver crtc object.
650 * Zero on success, error code on failure.
652 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
653 struct drm_plane
*primary
,
654 struct drm_plane
*cursor
,
655 const struct drm_crtc_funcs
*funcs
,
656 const char *name
, ...)
658 struct drm_mode_config
*config
= &dev
->mode_config
;
661 WARN_ON(primary
&& primary
->type
!= DRM_PLANE_TYPE_PRIMARY
);
662 WARN_ON(cursor
&& cursor
->type
!= DRM_PLANE_TYPE_CURSOR
);
667 INIT_LIST_HEAD(&crtc
->commit_list
);
668 spin_lock_init(&crtc
->commit_lock
);
670 drm_modeset_lock_init(&crtc
->mutex
);
671 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
679 crtc
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
682 crtc
->name
= kasprintf(GFP_KERNEL
, "crtc-%d",
686 drm_mode_object_unregister(dev
, &crtc
->base
);
690 crtc
->base
.properties
= &crtc
->properties
;
692 list_add_tail(&crtc
->head
, &config
->crtc_list
);
693 crtc
->index
= config
->num_crtc
++;
695 crtc
->primary
= primary
;
696 crtc
->cursor
= cursor
;
698 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
700 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
702 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
703 drm_object_attach_property(&crtc
->base
, config
->prop_active
, 0);
704 drm_object_attach_property(&crtc
->base
, config
->prop_mode_id
, 0);
709 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
712 * drm_crtc_cleanup - Clean up the core crtc usage
713 * @crtc: CRTC to cleanup
715 * This function cleans up @crtc and removes it from the DRM mode setting
716 * core. Note that the function does *not* free the crtc structure itself,
717 * this is the responsibility of the caller.
719 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
721 struct drm_device
*dev
= crtc
->dev
;
723 /* Note that the crtc_list is considered to be static; should we
724 * remove the drm_crtc at runtime we would have to decrement all
725 * the indices on the drm_crtc after us in the crtc_list.
728 kfree(crtc
->gamma_store
);
729 crtc
->gamma_store
= NULL
;
731 drm_modeset_lock_fini(&crtc
->mutex
);
733 drm_mode_object_unregister(dev
, &crtc
->base
);
734 list_del(&crtc
->head
);
735 dev
->mode_config
.num_crtc
--;
737 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
738 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
739 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
743 memset(crtc
, 0, sizeof(*crtc
));
745 EXPORT_SYMBOL(drm_crtc_cleanup
);
748 * drm_mode_remove - remove and free a mode
749 * @connector: connector list to modify
750 * @mode: mode to remove
752 * Remove @mode from @connector's mode list, then free it.
754 static void drm_mode_remove(struct drm_connector
*connector
,
755 struct drm_display_mode
*mode
)
757 list_del(&mode
->head
);
758 drm_mode_destroy(connector
->dev
, mode
);
762 * drm_display_info_set_bus_formats - set the supported bus formats
763 * @info: display info to store bus formats in
764 * @formats: array containing the supported bus formats
765 * @num_formats: the number of entries in the fmts array
767 * Store the supported bus formats in display info structure.
768 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
769 * a full list of available formats.
771 int drm_display_info_set_bus_formats(struct drm_display_info
*info
,
773 unsigned int num_formats
)
777 if (!formats
&& num_formats
)
780 if (formats
&& num_formats
) {
781 fmts
= kmemdup(formats
, sizeof(*formats
) * num_formats
,
787 kfree(info
->bus_formats
);
788 info
->bus_formats
= fmts
;
789 info
->num_bus_formats
= num_formats
;
793 EXPORT_SYMBOL(drm_display_info_set_bus_formats
);
796 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
797 * @connector: connector to quwery
799 * The kernel supports per-connector configration of its consoles through
800 * use of the video= parameter. This function parses that option and
801 * extracts the user's specified mode (or enable/disable status) for a
802 * particular connector. This is typically only used during the early fbdev
805 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
807 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
810 if (fb_get_options(connector
->name
, &option
))
813 if (!drm_mode_parse_command_line_for_connector(option
,
821 switch (mode
->force
) {
825 case DRM_FORCE_ON_DIGITAL
:
834 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
835 connector
->force
= mode
->force
;
838 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
840 mode
->xres
, mode
->yres
,
841 mode
->refresh_specified
? mode
->refresh
: 60,
842 mode
->rb
? " reduced blanking" : "",
843 mode
->margins
? " with margins" : "",
844 mode
->interlace
? " interlaced" : "");
847 static void drm_connector_free(struct kref
*kref
)
849 struct drm_connector
*connector
=
850 container_of(kref
, struct drm_connector
, base
.refcount
);
851 struct drm_device
*dev
= connector
->dev
;
853 drm_mode_object_unregister(dev
, &connector
->base
);
854 connector
->funcs
->destroy(connector
);
858 * drm_connector_init - Init a preallocated connector
860 * @connector: the connector to init
861 * @funcs: callbacks for this connector
862 * @connector_type: user visible type of the connector
864 * Initialises a preallocated connector. Connectors should be
865 * subclassed as part of driver connector objects.
868 * Zero on success, error code on failure.
870 int drm_connector_init(struct drm_device
*dev
,
871 struct drm_connector
*connector
,
872 const struct drm_connector_funcs
*funcs
,
875 struct drm_mode_config
*config
= &dev
->mode_config
;
877 struct ida
*connector_ida
=
878 &drm_connector_enum_list
[connector_type
].ida
;
880 drm_modeset_lock_all(dev
);
882 ret
= drm_mode_object_get_reg(dev
, &connector
->base
,
883 DRM_MODE_OBJECT_CONNECTOR
,
884 false, drm_connector_free
);
888 connector
->base
.properties
= &connector
->properties
;
889 connector
->dev
= dev
;
890 connector
->funcs
= funcs
;
892 connector
->connector_id
= ida_simple_get(&config
->connector_ida
, 0, 0, GFP_KERNEL
);
893 if (connector
->connector_id
< 0) {
894 ret
= connector
->connector_id
;
898 connector
->connector_type
= connector_type
;
899 connector
->connector_type_id
=
900 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
901 if (connector
->connector_type_id
< 0) {
902 ret
= connector
->connector_type_id
;
906 kasprintf(GFP_KERNEL
, "%s-%d",
907 drm_connector_enum_list
[connector_type
].name
,
908 connector
->connector_type_id
);
909 if (!connector
->name
) {
911 goto out_put_type_id
;
914 INIT_LIST_HEAD(&connector
->probed_modes
);
915 INIT_LIST_HEAD(&connector
->modes
);
916 connector
->edid_blob_ptr
= NULL
;
917 connector
->status
= connector_status_unknown
;
919 drm_connector_get_cmdline_mode(connector
);
921 /* We should add connectors at the end to avoid upsetting the connector
923 list_add_tail(&connector
->head
, &config
->connector_list
);
924 config
->num_connector
++;
926 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
927 drm_object_attach_property(&connector
->base
,
928 config
->edid_property
,
931 drm_object_attach_property(&connector
->base
,
932 config
->dpms_property
, 0);
934 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
935 drm_object_attach_property(&connector
->base
, config
->prop_crtc_id
, 0);
938 connector
->debugfs_entry
= NULL
;
941 ida_remove(connector_ida
, connector
->connector_type_id
);
944 ida_remove(&config
->connector_ida
, connector
->connector_id
);
947 drm_mode_object_unregister(dev
, &connector
->base
);
950 drm_modeset_unlock_all(dev
);
954 EXPORT_SYMBOL(drm_connector_init
);
957 * drm_connector_cleanup - cleans up an initialised connector
958 * @connector: connector to cleanup
960 * Cleans up the connector but doesn't free the object.
962 void drm_connector_cleanup(struct drm_connector
*connector
)
964 struct drm_device
*dev
= connector
->dev
;
965 struct drm_display_mode
*mode
, *t
;
967 /* The connector should have been removed from userspace long before
968 * it is finally destroyed.
970 if (WARN_ON(connector
->registered
))
971 drm_connector_unregister(connector
);
973 if (connector
->tile_group
) {
974 drm_mode_put_tile_group(dev
, connector
->tile_group
);
975 connector
->tile_group
= NULL
;
978 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
979 drm_mode_remove(connector
, mode
);
981 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
982 drm_mode_remove(connector
, mode
);
984 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
985 connector
->connector_type_id
);
987 ida_remove(&dev
->mode_config
.connector_ida
,
988 connector
->connector_id
);
990 kfree(connector
->display_info
.bus_formats
);
991 drm_mode_object_unregister(dev
, &connector
->base
);
992 kfree(connector
->name
);
993 connector
->name
= NULL
;
994 list_del(&connector
->head
);
995 dev
->mode_config
.num_connector
--;
997 WARN_ON(connector
->state
&& !connector
->funcs
->atomic_destroy_state
);
998 if (connector
->state
&& connector
->funcs
->atomic_destroy_state
)
999 connector
->funcs
->atomic_destroy_state(connector
,
1002 memset(connector
, 0, sizeof(*connector
));
1004 EXPORT_SYMBOL(drm_connector_cleanup
);
1007 * drm_connector_register - register a connector
1008 * @connector: the connector to register
1010 * Register userspace interfaces for a connector
1013 * Zero on success, error code on failure.
1015 int drm_connector_register(struct drm_connector
*connector
)
1019 if (connector
->registered
)
1022 ret
= drm_sysfs_connector_add(connector
);
1026 ret
= drm_debugfs_connector_add(connector
);
1031 if (connector
->funcs
->late_register
) {
1032 ret
= connector
->funcs
->late_register(connector
);
1037 drm_mode_object_register(connector
->dev
, &connector
->base
);
1039 connector
->registered
= true;
1043 drm_debugfs_connector_remove(connector
);
1045 drm_sysfs_connector_remove(connector
);
1048 EXPORT_SYMBOL(drm_connector_register
);
1051 * drm_connector_unregister - unregister a connector
1052 * @connector: the connector to unregister
1054 * Unregister userspace interfaces for a connector
1056 void drm_connector_unregister(struct drm_connector
*connector
)
1058 if (!connector
->registered
)
1061 if (connector
->funcs
->early_unregister
)
1062 connector
->funcs
->early_unregister(connector
);
1064 drm_sysfs_connector_remove(connector
);
1065 drm_debugfs_connector_remove(connector
);
1067 connector
->registered
= false;
1069 EXPORT_SYMBOL(drm_connector_unregister
);
1072 * drm_connector_register_all - register all connectors
1075 * This function registers all connectors in sysfs and other places so that
1076 * userspace can start to access them. drm_connector_register_all() is called
1077 * automatically from drm_dev_register() to complete the device registration,
1078 * if they don't call drm_connector_register() on each connector individually.
1080 * When a device is unplugged and should be removed from userspace access,
1081 * call drm_connector_unregister_all(), which is the inverse of this
1085 * Zero on success, error code on failure.
1087 int drm_connector_register_all(struct drm_device
*dev
)
1089 struct drm_connector
*connector
;
1092 mutex_lock(&dev
->mode_config
.mutex
);
1094 drm_for_each_connector(connector
, dev
) {
1095 ret
= drm_connector_register(connector
);
1100 mutex_unlock(&dev
->mode_config
.mutex
);
1105 mutex_unlock(&dev
->mode_config
.mutex
);
1106 drm_connector_unregister_all(dev
);
1109 EXPORT_SYMBOL(drm_connector_register_all
);
1112 * drm_connector_unregister_all - unregister connector userspace interfaces
1115 * This functions unregisters all connectors from sysfs and other places so
1116 * that userspace can no longer access them. Drivers should call this as the
1117 * first step tearing down the device instace, or when the underlying
1118 * physical device disappeared (e.g. USB unplug), right before calling
1119 * drm_dev_unregister().
1121 void drm_connector_unregister_all(struct drm_device
*dev
)
1123 struct drm_connector
*connector
;
1125 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1126 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1127 drm_connector_unregister(connector
);
1129 EXPORT_SYMBOL(drm_connector_unregister_all
);
1131 static int drm_encoder_register_all(struct drm_device
*dev
)
1133 struct drm_encoder
*encoder
;
1136 drm_for_each_encoder(encoder
, dev
) {
1137 if (encoder
->funcs
->late_register
)
1138 ret
= encoder
->funcs
->late_register(encoder
);
1146 static void drm_encoder_unregister_all(struct drm_device
*dev
)
1148 struct drm_encoder
*encoder
;
1150 drm_for_each_encoder(encoder
, dev
) {
1151 if (encoder
->funcs
->early_unregister
)
1152 encoder
->funcs
->early_unregister(encoder
);
1157 * drm_encoder_init - Init a preallocated encoder
1159 * @encoder: the encoder to init
1160 * @funcs: callbacks for this encoder
1161 * @encoder_type: user visible type of the encoder
1162 * @name: printf style format string for the encoder name, or NULL for default name
1164 * Initialises a preallocated encoder. Encoder should be
1165 * subclassed as part of driver encoder objects.
1168 * Zero on success, error code on failure.
1170 int drm_encoder_init(struct drm_device
*dev
,
1171 struct drm_encoder
*encoder
,
1172 const struct drm_encoder_funcs
*funcs
,
1173 int encoder_type
, const char *name
, ...)
1177 drm_modeset_lock_all(dev
);
1179 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1184 encoder
->encoder_type
= encoder_type
;
1185 encoder
->funcs
= funcs
;
1190 encoder
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1193 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1194 drm_encoder_enum_list
[encoder_type
].name
,
1197 if (!encoder
->name
) {
1202 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1203 encoder
->index
= dev
->mode_config
.num_encoder
++;
1207 drm_mode_object_unregister(dev
, &encoder
->base
);
1210 drm_modeset_unlock_all(dev
);
1214 EXPORT_SYMBOL(drm_encoder_init
);
1217 * drm_encoder_cleanup - cleans up an initialised encoder
1218 * @encoder: encoder to cleanup
1220 * Cleans up the encoder but doesn't free the object.
1222 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1224 struct drm_device
*dev
= encoder
->dev
;
1226 /* Note that the encoder_list is considered to be static; should we
1227 * remove the drm_encoder at runtime we would have to decrement all
1228 * the indices on the drm_encoder after us in the encoder_list.
1231 drm_modeset_lock_all(dev
);
1232 drm_mode_object_unregister(dev
, &encoder
->base
);
1233 kfree(encoder
->name
);
1234 list_del(&encoder
->head
);
1235 dev
->mode_config
.num_encoder
--;
1236 drm_modeset_unlock_all(dev
);
1238 memset(encoder
, 0, sizeof(*encoder
));
1240 EXPORT_SYMBOL(drm_encoder_cleanup
);
1242 static unsigned int drm_num_planes(struct drm_device
*dev
)
1244 unsigned int num
= 0;
1245 struct drm_plane
*tmp
;
1247 drm_for_each_plane(tmp
, dev
) {
1255 * drm_universal_plane_init - Initialize a new universal plane object
1257 * @plane: plane object to init
1258 * @possible_crtcs: bitmask of possible CRTCs
1259 * @funcs: callbacks for the new plane
1260 * @formats: array of supported formats (%DRM_FORMAT_*)
1261 * @format_count: number of elements in @formats
1262 * @type: type of plane (overlay, primary, cursor)
1263 * @name: printf style format string for the plane name, or NULL for default name
1265 * Initializes a plane object of type @type.
1268 * Zero on success, error code on failure.
1270 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1271 unsigned long possible_crtcs
,
1272 const struct drm_plane_funcs
*funcs
,
1273 const uint32_t *formats
, unsigned int format_count
,
1274 enum drm_plane_type type
,
1275 const char *name
, ...)
1277 struct drm_mode_config
*config
= &dev
->mode_config
;
1280 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1284 drm_modeset_lock_init(&plane
->mutex
);
1286 plane
->base
.properties
= &plane
->properties
;
1288 plane
->funcs
= funcs
;
1289 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
1291 if (!plane
->format_types
) {
1292 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1293 drm_mode_object_unregister(dev
, &plane
->base
);
1301 plane
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1304 plane
->name
= kasprintf(GFP_KERNEL
, "plane-%d",
1305 drm_num_planes(dev
));
1308 kfree(plane
->format_types
);
1309 drm_mode_object_unregister(dev
, &plane
->base
);
1313 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1314 plane
->format_count
= format_count
;
1315 plane
->possible_crtcs
= possible_crtcs
;
1318 list_add_tail(&plane
->head
, &config
->plane_list
);
1319 plane
->index
= config
->num_total_plane
++;
1320 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1321 config
->num_overlay_plane
++;
1323 drm_object_attach_property(&plane
->base
,
1324 config
->plane_type_property
,
1327 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
1328 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
1329 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
1330 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
1331 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
1332 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
1333 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
1334 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
1335 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
1336 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
1337 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
1342 EXPORT_SYMBOL(drm_universal_plane_init
);
1344 static int drm_plane_register_all(struct drm_device
*dev
)
1346 struct drm_plane
*plane
;
1349 drm_for_each_plane(plane
, dev
) {
1350 if (plane
->funcs
->late_register
)
1351 ret
= plane
->funcs
->late_register(plane
);
1359 static void drm_plane_unregister_all(struct drm_device
*dev
)
1361 struct drm_plane
*plane
;
1363 drm_for_each_plane(plane
, dev
) {
1364 if (plane
->funcs
->early_unregister
)
1365 plane
->funcs
->early_unregister(plane
);
1370 * drm_plane_init - Initialize a legacy plane
1372 * @plane: plane object to init
1373 * @possible_crtcs: bitmask of possible CRTCs
1374 * @funcs: callbacks for the new plane
1375 * @formats: array of supported formats (%DRM_FORMAT_*)
1376 * @format_count: number of elements in @formats
1377 * @is_primary: plane type (primary vs overlay)
1379 * Legacy API to initialize a DRM plane.
1381 * New drivers should call drm_universal_plane_init() instead.
1384 * Zero on success, error code on failure.
1386 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1387 unsigned long possible_crtcs
,
1388 const struct drm_plane_funcs
*funcs
,
1389 const uint32_t *formats
, unsigned int format_count
,
1392 enum drm_plane_type type
;
1394 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1395 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1396 formats
, format_count
, type
, NULL
);
1398 EXPORT_SYMBOL(drm_plane_init
);
1401 * drm_plane_cleanup - Clean up the core plane usage
1402 * @plane: plane to cleanup
1404 * This function cleans up @plane and removes it from the DRM mode setting
1405 * core. Note that the function does *not* free the plane structure itself,
1406 * this is the responsibility of the caller.
1408 void drm_plane_cleanup(struct drm_plane
*plane
)
1410 struct drm_device
*dev
= plane
->dev
;
1412 drm_modeset_lock_all(dev
);
1413 kfree(plane
->format_types
);
1414 drm_mode_object_unregister(dev
, &plane
->base
);
1416 BUG_ON(list_empty(&plane
->head
));
1418 /* Note that the plane_list is considered to be static; should we
1419 * remove the drm_plane at runtime we would have to decrement all
1420 * the indices on the drm_plane after us in the plane_list.
1423 list_del(&plane
->head
);
1424 dev
->mode_config
.num_total_plane
--;
1425 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1426 dev
->mode_config
.num_overlay_plane
--;
1427 drm_modeset_unlock_all(dev
);
1429 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
1430 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
1431 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1435 memset(plane
, 0, sizeof(*plane
));
1437 EXPORT_SYMBOL(drm_plane_cleanup
);
1440 * drm_plane_from_index - find the registered plane at an index
1442 * @idx: index of registered plane to find for
1444 * Given a plane index, return the registered plane from DRM device's
1445 * list of planes with matching index.
1448 drm_plane_from_index(struct drm_device
*dev
, int idx
)
1450 struct drm_plane
*plane
;
1452 drm_for_each_plane(plane
, dev
)
1453 if (idx
== plane
->index
)
1458 EXPORT_SYMBOL(drm_plane_from_index
);
1461 * drm_plane_force_disable - Forcibly disable a plane
1462 * @plane: plane to disable
1464 * Forces the plane to be disabled.
1466 * Used when the plane's current framebuffer is destroyed,
1467 * and when restoring fbdev mode.
1469 void drm_plane_force_disable(struct drm_plane
*plane
)
1476 plane
->old_fb
= plane
->fb
;
1477 ret
= plane
->funcs
->disable_plane(plane
);
1479 DRM_ERROR("failed to disable plane with busy fb\n");
1480 plane
->old_fb
= NULL
;
1483 /* disconnect the plane from the fb and crtc: */
1484 drm_framebuffer_unreference(plane
->old_fb
);
1485 plane
->old_fb
= NULL
;
1489 EXPORT_SYMBOL(drm_plane_force_disable
);
1491 int drm_modeset_register_all(struct drm_device
*dev
)
1495 ret
= drm_plane_register_all(dev
);
1499 ret
= drm_crtc_register_all(dev
);
1503 ret
= drm_encoder_register_all(dev
);
1507 ret
= drm_connector_register_all(dev
);
1514 drm_encoder_unregister_all(dev
);
1516 drm_crtc_unregister_all(dev
);
1518 drm_plane_unregister_all(dev
);
1523 void drm_modeset_unregister_all(struct drm_device
*dev
)
1525 drm_connector_unregister_all(dev
);
1526 drm_encoder_unregister_all(dev
);
1527 drm_crtc_unregister_all(dev
);
1528 drm_plane_unregister_all(dev
);
1531 static int drm_mode_create_standard_properties(struct drm_device
*dev
)
1533 struct drm_property
*prop
;
1536 * Standard properties (apply to all connectors)
1538 prop
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1539 DRM_MODE_PROP_IMMUTABLE
,
1543 dev
->mode_config
.edid_property
= prop
;
1545 prop
= drm_property_create_enum(dev
, 0,
1546 "DPMS", drm_dpms_enum_list
,
1547 ARRAY_SIZE(drm_dpms_enum_list
));
1550 dev
->mode_config
.dpms_property
= prop
;
1552 prop
= drm_property_create(dev
,
1553 DRM_MODE_PROP_BLOB
|
1554 DRM_MODE_PROP_IMMUTABLE
,
1558 dev
->mode_config
.path_property
= prop
;
1560 prop
= drm_property_create(dev
,
1561 DRM_MODE_PROP_BLOB
|
1562 DRM_MODE_PROP_IMMUTABLE
,
1566 dev
->mode_config
.tile_property
= prop
;
1568 prop
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1569 "type", drm_plane_type_enum_list
,
1570 ARRAY_SIZE(drm_plane_type_enum_list
));
1573 dev
->mode_config
.plane_type_property
= prop
;
1575 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1576 "SRC_X", 0, UINT_MAX
);
1579 dev
->mode_config
.prop_src_x
= prop
;
1581 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1582 "SRC_Y", 0, UINT_MAX
);
1585 dev
->mode_config
.prop_src_y
= prop
;
1587 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1588 "SRC_W", 0, UINT_MAX
);
1591 dev
->mode_config
.prop_src_w
= prop
;
1593 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1594 "SRC_H", 0, UINT_MAX
);
1597 dev
->mode_config
.prop_src_h
= prop
;
1599 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1600 "CRTC_X", INT_MIN
, INT_MAX
);
1603 dev
->mode_config
.prop_crtc_x
= prop
;
1605 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1606 "CRTC_Y", INT_MIN
, INT_MAX
);
1609 dev
->mode_config
.prop_crtc_y
= prop
;
1611 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1612 "CRTC_W", 0, INT_MAX
);
1615 dev
->mode_config
.prop_crtc_w
= prop
;
1617 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1618 "CRTC_H", 0, INT_MAX
);
1621 dev
->mode_config
.prop_crtc_h
= prop
;
1623 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1624 "FB_ID", DRM_MODE_OBJECT_FB
);
1627 dev
->mode_config
.prop_fb_id
= prop
;
1629 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1630 "CRTC_ID", DRM_MODE_OBJECT_CRTC
);
1633 dev
->mode_config
.prop_crtc_id
= prop
;
1635 prop
= drm_property_create_bool(dev
, DRM_MODE_PROP_ATOMIC
,
1639 dev
->mode_config
.prop_active
= prop
;
1641 prop
= drm_property_create(dev
,
1642 DRM_MODE_PROP_ATOMIC
| DRM_MODE_PROP_BLOB
,
1646 dev
->mode_config
.prop_mode_id
= prop
;
1648 prop
= drm_property_create(dev
,
1653 dev
->mode_config
.degamma_lut_property
= prop
;
1655 prop
= drm_property_create_range(dev
,
1656 DRM_MODE_PROP_IMMUTABLE
,
1657 "DEGAMMA_LUT_SIZE", 0, UINT_MAX
);
1660 dev
->mode_config
.degamma_lut_size_property
= prop
;
1662 prop
= drm_property_create(dev
,
1667 dev
->mode_config
.ctm_property
= prop
;
1669 prop
= drm_property_create(dev
,
1674 dev
->mode_config
.gamma_lut_property
= prop
;
1676 prop
= drm_property_create_range(dev
,
1677 DRM_MODE_PROP_IMMUTABLE
,
1678 "GAMMA_LUT_SIZE", 0, UINT_MAX
);
1681 dev
->mode_config
.gamma_lut_size_property
= prop
;
1687 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1690 * Called by a driver the first time a DVI-I connector is made.
1692 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1694 struct drm_property
*dvi_i_selector
;
1695 struct drm_property
*dvi_i_subconnector
;
1697 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1701 drm_property_create_enum(dev
, 0,
1702 "select subconnector",
1703 drm_dvi_i_select_enum_list
,
1704 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1705 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1707 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1709 drm_dvi_i_subconnector_enum_list
,
1710 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1711 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1715 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1718 * drm_create_tv_properties - create TV specific connector properties
1720 * @num_modes: number of different TV formats (modes) supported
1721 * @modes: array of pointers to strings containing name of each format
1723 * Called by a driver's TV initialization routine, this function creates
1724 * the TV specific connector properties for a given device. Caller is
1725 * responsible for allocating a list of format names and passing them to
1728 int drm_mode_create_tv_properties(struct drm_device
*dev
,
1729 unsigned int num_modes
,
1730 const char * const modes
[])
1732 struct drm_property
*tv_selector
;
1733 struct drm_property
*tv_subconnector
;
1736 if (dev
->mode_config
.tv_select_subconnector_property
)
1740 * Basic connector properties
1742 tv_selector
= drm_property_create_enum(dev
, 0,
1743 "select subconnector",
1744 drm_tv_select_enum_list
,
1745 ARRAY_SIZE(drm_tv_select_enum_list
));
1749 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1752 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1754 drm_tv_subconnector_enum_list
,
1755 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1756 if (!tv_subconnector
)
1758 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1761 * Other, TV specific properties: margins & TV modes.
1763 dev
->mode_config
.tv_left_margin_property
=
1764 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1765 if (!dev
->mode_config
.tv_left_margin_property
)
1768 dev
->mode_config
.tv_right_margin_property
=
1769 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1770 if (!dev
->mode_config
.tv_right_margin_property
)
1773 dev
->mode_config
.tv_top_margin_property
=
1774 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1775 if (!dev
->mode_config
.tv_top_margin_property
)
1778 dev
->mode_config
.tv_bottom_margin_property
=
1779 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1780 if (!dev
->mode_config
.tv_bottom_margin_property
)
1783 dev
->mode_config
.tv_mode_property
=
1784 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1786 if (!dev
->mode_config
.tv_mode_property
)
1789 for (i
= 0; i
< num_modes
; i
++)
1790 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1793 dev
->mode_config
.tv_brightness_property
=
1794 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1795 if (!dev
->mode_config
.tv_brightness_property
)
1798 dev
->mode_config
.tv_contrast_property
=
1799 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1800 if (!dev
->mode_config
.tv_contrast_property
)
1803 dev
->mode_config
.tv_flicker_reduction_property
=
1804 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1805 if (!dev
->mode_config
.tv_flicker_reduction_property
)
1808 dev
->mode_config
.tv_overscan_property
=
1809 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1810 if (!dev
->mode_config
.tv_overscan_property
)
1813 dev
->mode_config
.tv_saturation_property
=
1814 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1815 if (!dev
->mode_config
.tv_saturation_property
)
1818 dev
->mode_config
.tv_hue_property
=
1819 drm_property_create_range(dev
, 0, "hue", 0, 100);
1820 if (!dev
->mode_config
.tv_hue_property
)
1827 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1830 * drm_mode_create_scaling_mode_property - create scaling mode property
1833 * Called by a driver the first time it's needed, must be attached to desired
1836 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1838 struct drm_property
*scaling_mode
;
1840 if (dev
->mode_config
.scaling_mode_property
)
1844 drm_property_create_enum(dev
, 0, "scaling mode",
1845 drm_scaling_mode_enum_list
,
1846 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1848 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1852 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1855 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1858 * Called by a driver the first time it's needed, must be attached to desired
1862 * Zero on success, negative errno on failure.
1864 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1866 if (dev
->mode_config
.aspect_ratio_property
)
1869 dev
->mode_config
.aspect_ratio_property
=
1870 drm_property_create_enum(dev
, 0, "aspect ratio",
1871 drm_aspect_ratio_enum_list
,
1872 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1874 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1879 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1882 * drm_mode_create_dirty_property - create dirty property
1885 * Called by a driver the first time it's needed, must be attached to desired
1888 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1890 struct drm_property
*dirty_info
;
1892 if (dev
->mode_config
.dirty_info_property
)
1896 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1898 drm_dirty_info_enum_list
,
1899 ARRAY_SIZE(drm_dirty_info_enum_list
));
1900 dev
->mode_config
.dirty_info_property
= dirty_info
;
1904 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1907 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1910 * Create the the suggested x/y offset property for connectors.
1912 int drm_mode_create_suggested_offset_properties(struct drm_device
*dev
)
1914 if (dev
->mode_config
.suggested_x_property
&& dev
->mode_config
.suggested_y_property
)
1917 dev
->mode_config
.suggested_x_property
=
1918 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested X", 0, 0xffffffff);
1920 dev
->mode_config
.suggested_y_property
=
1921 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested Y", 0, 0xffffffff);
1923 if (dev
->mode_config
.suggested_x_property
== NULL
||
1924 dev
->mode_config
.suggested_y_property
== NULL
)
1928 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties
);
1931 * drm_mode_getresources - get graphics configuration
1932 * @dev: drm device for the ioctl
1933 * @data: data pointer for the ioctl
1934 * @file_priv: drm file for the ioctl call
1936 * Construct a set of configuration description structures and return
1937 * them to the user, including CRTC, connector and framebuffer configuration.
1939 * Called by the user via ioctl.
1942 * Zero on success, negative errno on failure.
1944 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1945 struct drm_file
*file_priv
)
1947 struct drm_mode_card_res
*card_res
= data
;
1948 struct list_head
*lh
;
1949 struct drm_framebuffer
*fb
;
1950 struct drm_connector
*connector
;
1951 struct drm_crtc
*crtc
;
1952 struct drm_encoder
*encoder
;
1954 int connector_count
= 0;
1957 int encoder_count
= 0;
1959 uint32_t __user
*fb_id
;
1960 uint32_t __user
*crtc_id
;
1961 uint32_t __user
*connector_id
;
1962 uint32_t __user
*encoder_id
;
1964 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1968 mutex_lock(&file_priv
->fbs_lock
);
1970 * For the non-control nodes we need to limit the list of resources
1971 * by IDs in the group list for this node
1973 list_for_each(lh
, &file_priv
->fbs
)
1976 /* handle this in 4 parts */
1978 if (card_res
->count_fbs
>= fb_count
) {
1980 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1981 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1982 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1983 mutex_unlock(&file_priv
->fbs_lock
);
1989 card_res
->count_fbs
= fb_count
;
1990 mutex_unlock(&file_priv
->fbs_lock
);
1992 /* mode_config.mutex protects the connector list against e.g. DP MST
1993 * connector hot-adding. CRTC/Plane lists are invariant. */
1994 mutex_lock(&dev
->mode_config
.mutex
);
1995 drm_for_each_crtc(crtc
, dev
)
1998 drm_for_each_connector(connector
, dev
)
2001 drm_for_each_encoder(encoder
, dev
)
2004 card_res
->max_height
= dev
->mode_config
.max_height
;
2005 card_res
->min_height
= dev
->mode_config
.min_height
;
2006 card_res
->max_width
= dev
->mode_config
.max_width
;
2007 card_res
->min_width
= dev
->mode_config
.min_width
;
2010 if (card_res
->count_crtcs
>= crtc_count
) {
2012 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
2013 drm_for_each_crtc(crtc
, dev
) {
2014 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
2021 card_res
->count_crtcs
= crtc_count
;
2024 if (card_res
->count_encoders
>= encoder_count
) {
2026 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
2027 drm_for_each_encoder(encoder
, dev
) {
2028 if (put_user(encoder
->base
.id
, encoder_id
+
2036 card_res
->count_encoders
= encoder_count
;
2039 if (card_res
->count_connectors
>= connector_count
) {
2041 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
2042 drm_for_each_connector(connector
, dev
) {
2043 if (put_user(connector
->base
.id
,
2044 connector_id
+ copied
)) {
2051 card_res
->count_connectors
= connector_count
;
2054 mutex_unlock(&dev
->mode_config
.mutex
);
2059 * drm_mode_getcrtc - get CRTC configuration
2060 * @dev: drm device for the ioctl
2061 * @data: data pointer for the ioctl
2062 * @file_priv: drm file for the ioctl call
2064 * Construct a CRTC configuration structure to return to the user.
2066 * Called by the user via ioctl.
2069 * Zero on success, negative errno on failure.
2071 int drm_mode_getcrtc(struct drm_device
*dev
,
2072 void *data
, struct drm_file
*file_priv
)
2074 struct drm_mode_crtc
*crtc_resp
= data
;
2075 struct drm_crtc
*crtc
;
2077 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2080 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
2084 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
2085 crtc_resp
->gamma_size
= crtc
->gamma_size
;
2086 if (crtc
->primary
->fb
)
2087 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
2089 crtc_resp
->fb_id
= 0;
2092 crtc_resp
->x
= crtc
->primary
->state
->src_x
>> 16;
2093 crtc_resp
->y
= crtc
->primary
->state
->src_y
>> 16;
2094 if (crtc
->state
->enable
) {
2095 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->state
->mode
);
2096 crtc_resp
->mode_valid
= 1;
2099 crtc_resp
->mode_valid
= 0;
2102 crtc_resp
->x
= crtc
->x
;
2103 crtc_resp
->y
= crtc
->y
;
2104 if (crtc
->enabled
) {
2105 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
2106 crtc_resp
->mode_valid
= 1;
2109 crtc_resp
->mode_valid
= 0;
2112 drm_modeset_unlock_crtc(crtc
);
2117 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
2118 const struct drm_file
*file_priv
)
2121 * If user-space hasn't configured the driver to expose the stereo 3D
2122 * modes, don't expose them.
2124 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
2130 static struct drm_encoder
*drm_connector_get_encoder(struct drm_connector
*connector
)
2132 /* For atomic drivers only state objects are synchronously updated and
2133 * protected by modeset locks, so check those first. */
2134 if (connector
->state
)
2135 return connector
->state
->best_encoder
;
2136 return connector
->encoder
;
2139 /* helper for getconnector and getproperties ioctls */
2140 static int get_properties(struct drm_mode_object
*obj
, bool atomic
,
2141 uint32_t __user
*prop_ptr
, uint64_t __user
*prop_values
,
2142 uint32_t *arg_count_props
)
2147 props_count
= obj
->properties
->count
;
2149 props_count
-= obj
->properties
->atomic_count
;
2151 if ((*arg_count_props
>= props_count
) && props_count
) {
2152 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
2153 struct drm_property
*prop
= obj
->properties
->properties
[i
];
2156 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
2159 ret
= drm_object_property_get_value(obj
, prop
, &val
);
2163 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
2166 if (put_user(val
, prop_values
+ copied
))
2172 *arg_count_props
= props_count
;
2178 * drm_mode_getconnector - get connector configuration
2179 * @dev: drm device for the ioctl
2180 * @data: data pointer for the ioctl
2181 * @file_priv: drm file for the ioctl call
2183 * Construct a connector configuration structure to return to the user.
2185 * Called by the user via ioctl.
2188 * Zero on success, negative errno on failure.
2190 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
2191 struct drm_file
*file_priv
)
2193 struct drm_mode_get_connector
*out_resp
= data
;
2194 struct drm_connector
*connector
;
2195 struct drm_encoder
*encoder
;
2196 struct drm_display_mode
*mode
;
2198 int encoders_count
= 0;
2202 struct drm_mode_modeinfo u_mode
;
2203 struct drm_mode_modeinfo __user
*mode_ptr
;
2204 uint32_t __user
*encoder_ptr
;
2206 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2209 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
2211 mutex_lock(&dev
->mode_config
.mutex
);
2213 connector
= drm_connector_lookup(dev
, out_resp
->connector_id
);
2219 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++)
2220 if (connector
->encoder_ids
[i
] != 0)
2223 if (out_resp
->count_modes
== 0) {
2224 connector
->funcs
->fill_modes(connector
,
2225 dev
->mode_config
.max_width
,
2226 dev
->mode_config
.max_height
);
2229 /* delayed so we get modes regardless of pre-fill_modes state */
2230 list_for_each_entry(mode
, &connector
->modes
, head
)
2231 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2234 out_resp
->connector_id
= connector
->base
.id
;
2235 out_resp
->connector_type
= connector
->connector_type
;
2236 out_resp
->connector_type_id
= connector
->connector_type_id
;
2237 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2238 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2239 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2240 out_resp
->connection
= connector
->status
;
2242 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2243 encoder
= drm_connector_get_encoder(connector
);
2245 out_resp
->encoder_id
= encoder
->base
.id
;
2247 out_resp
->encoder_id
= 0;
2250 * This ioctl is called twice, once to determine how much space is
2251 * needed, and the 2nd time to fill it.
2253 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2255 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2256 list_for_each_entry(mode
, &connector
->modes
, head
) {
2257 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2260 drm_mode_convert_to_umode(&u_mode
, mode
);
2261 if (copy_to_user(mode_ptr
+ copied
,
2262 &u_mode
, sizeof(u_mode
))) {
2269 out_resp
->count_modes
= mode_count
;
2271 ret
= get_properties(&connector
->base
, file_priv
->atomic
,
2272 (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
),
2273 (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
),
2274 &out_resp
->count_props
);
2278 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2280 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2281 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2282 if (connector
->encoder_ids
[i
] != 0) {
2283 if (put_user(connector
->encoder_ids
[i
],
2284 encoder_ptr
+ copied
)) {
2292 out_resp
->count_encoders
= encoders_count
;
2295 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2297 drm_connector_unreference(connector
);
2299 mutex_unlock(&dev
->mode_config
.mutex
);
2304 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
2306 struct drm_connector
*connector
;
2307 struct drm_device
*dev
= encoder
->dev
;
2308 bool uses_atomic
= false;
2310 /* For atomic drivers only state objects are synchronously updated and
2311 * protected by modeset locks, so check those first. */
2312 drm_for_each_connector(connector
, dev
) {
2313 if (!connector
->state
)
2318 if (connector
->state
->best_encoder
!= encoder
)
2321 return connector
->state
->crtc
;
2324 /* Don't return stale data (e.g. pending async disable). */
2328 return encoder
->crtc
;
2332 * drm_mode_getencoder - get encoder configuration
2333 * @dev: drm device for the ioctl
2334 * @data: data pointer for the ioctl
2335 * @file_priv: drm file for the ioctl call
2337 * Construct a encoder configuration structure to return to the user.
2339 * Called by the user via ioctl.
2342 * Zero on success, negative errno on failure.
2344 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2345 struct drm_file
*file_priv
)
2347 struct drm_mode_get_encoder
*enc_resp
= data
;
2348 struct drm_encoder
*encoder
;
2349 struct drm_crtc
*crtc
;
2351 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2354 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2358 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2359 crtc
= drm_encoder_get_crtc(encoder
);
2361 enc_resp
->crtc_id
= crtc
->base
.id
;
2363 enc_resp
->crtc_id
= 0;
2364 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2366 enc_resp
->encoder_type
= encoder
->encoder_type
;
2367 enc_resp
->encoder_id
= encoder
->base
.id
;
2368 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2369 enc_resp
->possible_clones
= encoder
->possible_clones
;
2375 * drm_mode_getplane_res - enumerate all plane resources
2378 * @file_priv: DRM file info
2380 * Construct a list of plane ids to return to the user.
2382 * Called by the user via ioctl.
2385 * Zero on success, negative errno on failure.
2387 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2388 struct drm_file
*file_priv
)
2390 struct drm_mode_get_plane_res
*plane_resp
= data
;
2391 struct drm_mode_config
*config
;
2392 struct drm_plane
*plane
;
2393 uint32_t __user
*plane_ptr
;
2395 unsigned num_planes
;
2397 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2400 config
= &dev
->mode_config
;
2402 if (file_priv
->universal_planes
)
2403 num_planes
= config
->num_total_plane
;
2405 num_planes
= config
->num_overlay_plane
;
2408 * This ioctl is called twice, once to determine how much space is
2409 * needed, and the 2nd time to fill it.
2412 (plane_resp
->count_planes
>= num_planes
)) {
2413 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2415 /* Plane lists are invariant, no locking needed. */
2416 drm_for_each_plane(plane
, dev
) {
2418 * Unless userspace set the 'universal planes'
2419 * capability bit, only advertise overlays.
2421 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2422 !file_priv
->universal_planes
)
2425 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
2430 plane_resp
->count_planes
= num_planes
;
2436 * drm_mode_getplane - get plane configuration
2439 * @file_priv: DRM file info
2441 * Construct a plane configuration structure to return to the user.
2443 * Called by the user via ioctl.
2446 * Zero on success, negative errno on failure.
2448 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2449 struct drm_file
*file_priv
)
2451 struct drm_mode_get_plane
*plane_resp
= data
;
2452 struct drm_plane
*plane
;
2453 uint32_t __user
*format_ptr
;
2455 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2458 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2462 drm_modeset_lock(&plane
->mutex
, NULL
);
2464 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2466 plane_resp
->crtc_id
= 0;
2469 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2471 plane_resp
->fb_id
= 0;
2472 drm_modeset_unlock(&plane
->mutex
);
2474 plane_resp
->plane_id
= plane
->base
.id
;
2475 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2476 plane_resp
->gamma_size
= 0;
2479 * This ioctl is called twice, once to determine how much space is
2480 * needed, and the 2nd time to fill it.
2482 if (plane
->format_count
&&
2483 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2484 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2485 if (copy_to_user(format_ptr
,
2486 plane
->format_types
,
2487 sizeof(uint32_t) * plane
->format_count
)) {
2491 plane_resp
->count_format_types
= plane
->format_count
;
2497 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2498 * @plane: plane to check for format support
2499 * @format: the pixel format
2502 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2505 int drm_plane_check_pixel_format(const struct drm_plane
*plane
, u32 format
)
2509 for (i
= 0; i
< plane
->format_count
; i
++) {
2510 if (format
== plane
->format_types
[i
])
2517 static int check_src_coords(uint32_t src_x
, uint32_t src_y
,
2518 uint32_t src_w
, uint32_t src_h
,
2519 const struct drm_framebuffer
*fb
)
2521 unsigned int fb_width
, fb_height
;
2523 fb_width
= fb
->width
<< 16;
2524 fb_height
= fb
->height
<< 16;
2526 /* Make sure source coordinates are inside the fb. */
2527 if (src_w
> fb_width
||
2528 src_x
> fb_width
- src_w
||
2529 src_h
> fb_height
||
2530 src_y
> fb_height
- src_h
) {
2531 DRM_DEBUG_KMS("Invalid source coordinates "
2532 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2533 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2534 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2535 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2536 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2544 * setplane_internal - setplane handler for internal callers
2546 * Note that we assume an extra reference has already been taken on fb. If the
2547 * update fails, this reference will be dropped before return; if it succeeds,
2548 * the previous framebuffer (if any) will be unreferenced instead.
2550 * src_{x,y,w,h} are provided in 16.16 fixed point format
2552 static int __setplane_internal(struct drm_plane
*plane
,
2553 struct drm_crtc
*crtc
,
2554 struct drm_framebuffer
*fb
,
2555 int32_t crtc_x
, int32_t crtc_y
,
2556 uint32_t crtc_w
, uint32_t crtc_h
,
2557 /* src_{x,y,w,h} values are 16.16 fixed point */
2558 uint32_t src_x
, uint32_t src_y
,
2559 uint32_t src_w
, uint32_t src_h
)
2563 /* No fb means shut it down */
2565 plane
->old_fb
= plane
->fb
;
2566 ret
= plane
->funcs
->disable_plane(plane
);
2571 plane
->old_fb
= NULL
;
2576 /* Check whether this plane is usable on this CRTC */
2577 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2578 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2583 /* Check whether this plane supports the fb pixel format. */
2584 ret
= drm_plane_check_pixel_format(plane
, fb
->pixel_format
);
2586 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2587 drm_get_format_name(fb
->pixel_format
));
2591 /* Give drivers some help against integer overflows */
2592 if (crtc_w
> INT_MAX
||
2593 crtc_x
> INT_MAX
- (int32_t) crtc_w
||
2595 crtc_y
> INT_MAX
- (int32_t) crtc_h
) {
2596 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2597 crtc_w
, crtc_h
, crtc_x
, crtc_y
);
2602 ret
= check_src_coords(src_x
, src_y
, src_w
, src_h
, fb
);
2606 plane
->old_fb
= plane
->fb
;
2607 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2608 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2609 src_x
, src_y
, src_w
, src_h
);
2615 plane
->old_fb
= NULL
;
2620 drm_framebuffer_unreference(fb
);
2622 drm_framebuffer_unreference(plane
->old_fb
);
2623 plane
->old_fb
= NULL
;
2628 static int setplane_internal(struct drm_plane
*plane
,
2629 struct drm_crtc
*crtc
,
2630 struct drm_framebuffer
*fb
,
2631 int32_t crtc_x
, int32_t crtc_y
,
2632 uint32_t crtc_w
, uint32_t crtc_h
,
2633 /* src_{x,y,w,h} values are 16.16 fixed point */
2634 uint32_t src_x
, uint32_t src_y
,
2635 uint32_t src_w
, uint32_t src_h
)
2639 drm_modeset_lock_all(plane
->dev
);
2640 ret
= __setplane_internal(plane
, crtc
, fb
,
2641 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2642 src_x
, src_y
, src_w
, src_h
);
2643 drm_modeset_unlock_all(plane
->dev
);
2649 * drm_mode_setplane - configure a plane's configuration
2651 * @data: ioctl data*
2652 * @file_priv: DRM file info
2654 * Set plane configuration, including placement, fb, scaling, and other factors.
2655 * Or pass a NULL fb to disable (planes may be disabled without providing a
2659 * Zero on success, negative errno on failure.
2661 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2662 struct drm_file
*file_priv
)
2664 struct drm_mode_set_plane
*plane_req
= data
;
2665 struct drm_plane
*plane
;
2666 struct drm_crtc
*crtc
= NULL
;
2667 struct drm_framebuffer
*fb
= NULL
;
2669 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2673 * First, find the plane, crtc, and fb objects. If not available,
2674 * we don't bother to call the driver.
2676 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2678 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2679 plane_req
->plane_id
);
2683 if (plane_req
->fb_id
) {
2684 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2686 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2691 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2693 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2694 plane_req
->crtc_id
);
2700 * setplane_internal will take care of deref'ing either the old or new
2701 * framebuffer depending on success.
2703 return setplane_internal(plane
, crtc
, fb
,
2704 plane_req
->crtc_x
, plane_req
->crtc_y
,
2705 plane_req
->crtc_w
, plane_req
->crtc_h
,
2706 plane_req
->src_x
, plane_req
->src_y
,
2707 plane_req
->src_w
, plane_req
->src_h
);
2711 * drm_mode_set_config_internal - helper to call ->set_config
2712 * @set: modeset config to set
2714 * This is a little helper to wrap internal calls to the ->set_config driver
2715 * interface. The only thing it adds is correct refcounting dance.
2718 * Zero on success, negative errno on failure.
2720 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2722 struct drm_crtc
*crtc
= set
->crtc
;
2723 struct drm_framebuffer
*fb
;
2724 struct drm_crtc
*tmp
;
2728 * NOTE: ->set_config can also disable other crtcs (if we steal all
2729 * connectors from it), hence we need to refcount the fbs across all
2730 * crtcs. Atomic modeset will have saner semantics ...
2732 drm_for_each_crtc(tmp
, crtc
->dev
)
2733 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2737 ret
= crtc
->funcs
->set_config(set
);
2739 crtc
->primary
->crtc
= crtc
;
2740 crtc
->primary
->fb
= fb
;
2743 drm_for_each_crtc(tmp
, crtc
->dev
) {
2744 if (tmp
->primary
->fb
)
2745 drm_framebuffer_reference(tmp
->primary
->fb
);
2746 if (tmp
->primary
->old_fb
)
2747 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2748 tmp
->primary
->old_fb
= NULL
;
2753 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2756 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2757 * @mode: mode to query
2758 * @hdisplay: hdisplay value to fill in
2759 * @vdisplay: vdisplay value to fill in
2761 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2762 * the appropriate layout.
2764 void drm_crtc_get_hv_timing(const struct drm_display_mode
*mode
,
2765 int *hdisplay
, int *vdisplay
)
2767 struct drm_display_mode adjusted
;
2769 drm_mode_copy(&adjusted
, mode
);
2770 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE_ONLY
);
2771 *hdisplay
= adjusted
.crtc_hdisplay
;
2772 *vdisplay
= adjusted
.crtc_vdisplay
;
2774 EXPORT_SYMBOL(drm_crtc_get_hv_timing
);
2777 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2779 * @crtc: CRTC that framebuffer will be displayed on
2782 * @mode: mode that framebuffer will be displayed under
2783 * @fb: framebuffer to check size of
2785 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2787 const struct drm_display_mode
*mode
,
2788 const struct drm_framebuffer
*fb
)
2791 int hdisplay
, vdisplay
;
2793 drm_crtc_get_hv_timing(mode
, &hdisplay
, &vdisplay
);
2796 crtc
->primary
->state
->rotation
& (BIT(DRM_ROTATE_90
) |
2797 BIT(DRM_ROTATE_270
)))
2798 swap(hdisplay
, vdisplay
);
2800 return check_src_coords(x
<< 16, y
<< 16,
2801 hdisplay
<< 16, vdisplay
<< 16, fb
);
2803 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2806 * drm_mode_setcrtc - set CRTC configuration
2807 * @dev: drm device for the ioctl
2808 * @data: data pointer for the ioctl
2809 * @file_priv: drm file for the ioctl call
2811 * Build a new CRTC configuration based on user request.
2813 * Called by the user via ioctl.
2816 * Zero on success, negative errno on failure.
2818 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2819 struct drm_file
*file_priv
)
2821 struct drm_mode_config
*config
= &dev
->mode_config
;
2822 struct drm_mode_crtc
*crtc_req
= data
;
2823 struct drm_crtc
*crtc
;
2824 struct drm_connector
**connector_set
= NULL
, *connector
;
2825 struct drm_framebuffer
*fb
= NULL
;
2826 struct drm_display_mode
*mode
= NULL
;
2827 struct drm_mode_set set
;
2828 uint32_t __user
*set_connectors_ptr
;
2832 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2836 * Universal plane src offsets are only 16.16, prevent havoc for
2837 * drivers using universal plane code internally.
2839 if (crtc_req
->x
& 0xffff0000 || crtc_req
->y
& 0xffff0000)
2842 drm_modeset_lock_all(dev
);
2843 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2845 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2849 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc
->base
.id
, crtc
->name
);
2851 if (crtc_req
->mode_valid
) {
2852 /* If we have a mode we need a framebuffer. */
2853 /* If we pass -1, set the mode with the currently bound fb */
2854 if (crtc_req
->fb_id
== -1) {
2855 if (!crtc
->primary
->fb
) {
2856 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2860 fb
= crtc
->primary
->fb
;
2861 /* Make refcounting symmetric with the lookup path. */
2862 drm_framebuffer_reference(fb
);
2864 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2866 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2873 mode
= drm_mode_create(dev
);
2879 ret
= drm_mode_convert_umode(mode
, &crtc_req
->mode
);
2881 DRM_DEBUG_KMS("Invalid mode\n");
2886 * Check whether the primary plane supports the fb pixel format.
2887 * Drivers not implementing the universal planes API use a
2888 * default formats list provided by the DRM core which doesn't
2889 * match real hardware capabilities. Skip the check in that
2892 if (!crtc
->primary
->format_default
) {
2893 ret
= drm_plane_check_pixel_format(crtc
->primary
,
2896 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2897 drm_get_format_name(fb
->pixel_format
));
2902 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2909 if (crtc_req
->count_connectors
== 0 && mode
) {
2910 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2915 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2916 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2917 crtc_req
->count_connectors
);
2922 if (crtc_req
->count_connectors
> 0) {
2925 /* Avoid unbounded kernel memory allocation */
2926 if (crtc_req
->count_connectors
> config
->num_connector
) {
2931 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
2932 sizeof(struct drm_connector
*),
2934 if (!connector_set
) {
2939 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2940 connector_set
[i
] = NULL
;
2941 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2942 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2947 connector
= drm_connector_lookup(dev
, out_id
);
2949 DRM_DEBUG_KMS("Connector id %d unknown\n",
2954 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2958 connector_set
[i
] = connector
;
2963 set
.x
= crtc_req
->x
;
2964 set
.y
= crtc_req
->y
;
2966 set
.connectors
= connector_set
;
2967 set
.num_connectors
= crtc_req
->count_connectors
;
2969 ret
= drm_mode_set_config_internal(&set
);
2973 drm_framebuffer_unreference(fb
);
2975 if (connector_set
) {
2976 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2977 if (connector_set
[i
])
2978 drm_connector_unreference(connector_set
[i
]);
2981 kfree(connector_set
);
2982 drm_mode_destroy(dev
, mode
);
2983 drm_modeset_unlock_all(dev
);
2988 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2989 * universal plane handler call
2990 * @crtc: crtc to update cursor for
2991 * @req: data pointer for the ioctl
2992 * @file_priv: drm file for the ioctl call
2994 * Legacy cursor ioctl's work directly with driver buffer handles. To
2995 * translate legacy ioctl calls into universal plane handler calls, we need to
2996 * wrap the native buffer handle in a drm_framebuffer.
2998 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2999 * buffer with a pitch of 4*width; the universal plane interface should be used
3000 * directly in cases where the hardware can support other buffer settings and
3001 * userspace wants to make use of these capabilities.
3004 * Zero on success, negative errno on failure.
3006 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
3007 struct drm_mode_cursor2
*req
,
3008 struct drm_file
*file_priv
)
3010 struct drm_device
*dev
= crtc
->dev
;
3011 struct drm_framebuffer
*fb
= NULL
;
3012 struct drm_mode_fb_cmd2 fbreq
= {
3013 .width
= req
->width
,
3014 .height
= req
->height
,
3015 .pixel_format
= DRM_FORMAT_ARGB8888
,
3016 .pitches
= { req
->width
* 4 },
3017 .handles
= { req
->handle
},
3019 int32_t crtc_x
, crtc_y
;
3020 uint32_t crtc_w
= 0, crtc_h
= 0;
3021 uint32_t src_w
= 0, src_h
= 0;
3024 BUG_ON(!crtc
->cursor
);
3025 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
3028 * Obtain fb we'll be using (either new or existing) and take an extra
3029 * reference to it if fb != null. setplane will take care of dropping
3030 * the reference if the plane update fails.
3032 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
3034 fb
= internal_framebuffer_create(dev
, &fbreq
, file_priv
);
3036 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
3039 fb
->hot_x
= req
->hot_x
;
3040 fb
->hot_y
= req
->hot_y
;
3045 fb
= crtc
->cursor
->fb
;
3047 drm_framebuffer_reference(fb
);
3050 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3054 crtc_x
= crtc
->cursor_x
;
3055 crtc_y
= crtc
->cursor_y
;
3060 crtc_h
= fb
->height
;
3061 src_w
= fb
->width
<< 16;
3062 src_h
= fb
->height
<< 16;
3066 * setplane_internal will take care of deref'ing either the old or new
3067 * framebuffer depending on success.
3069 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
3070 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
3071 0, 0, src_w
, src_h
);
3073 /* Update successful; save new cursor position, if necessary */
3074 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3075 crtc
->cursor_x
= req
->x
;
3076 crtc
->cursor_y
= req
->y
;
3082 static int drm_mode_cursor_common(struct drm_device
*dev
,
3083 struct drm_mode_cursor2
*req
,
3084 struct drm_file
*file_priv
)
3086 struct drm_crtc
*crtc
;
3089 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3092 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
3095 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
3097 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
3102 * If this crtc has a universal cursor plane, call that plane's update
3103 * handler rather than using legacy cursor handlers.
3105 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
3107 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
3111 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
3112 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
3116 /* Turns off the cursor if handle is 0 */
3117 if (crtc
->funcs
->cursor_set2
)
3118 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
3119 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
3121 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
3122 req
->width
, req
->height
);
3125 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3126 if (crtc
->funcs
->cursor_move
) {
3127 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
3134 drm_modeset_unlock_crtc(crtc
);
3142 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3143 * @dev: drm device for the ioctl
3144 * @data: data pointer for the ioctl
3145 * @file_priv: drm file for the ioctl call
3147 * Set the cursor configuration based on user request.
3149 * Called by the user via ioctl.
3152 * Zero on success, negative errno on failure.
3154 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
3155 void *data
, struct drm_file
*file_priv
)
3157 struct drm_mode_cursor
*req
= data
;
3158 struct drm_mode_cursor2 new_req
;
3160 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
3161 new_req
.hot_x
= new_req
.hot_y
= 0;
3163 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
3167 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3168 * @dev: drm device for the ioctl
3169 * @data: data pointer for the ioctl
3170 * @file_priv: drm file for the ioctl call
3172 * Set the cursor configuration based on user request. This implements the 2nd
3173 * version of the cursor ioctl, which allows userspace to additionally specify
3174 * the hotspot of the pointer.
3176 * Called by the user via ioctl.
3179 * Zero on success, negative errno on failure.
3181 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
3182 void *data
, struct drm_file
*file_priv
)
3184 struct drm_mode_cursor2
*req
= data
;
3186 return drm_mode_cursor_common(dev
, req
, file_priv
);
3190 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3191 * @bpp: bits per pixels
3192 * @depth: bit depth per pixel
3194 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3195 * Useful in fbdev emulation code, since that deals in those values.
3197 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
3203 fmt
= DRM_FORMAT_C8
;
3207 fmt
= DRM_FORMAT_XRGB1555
;
3209 fmt
= DRM_FORMAT_RGB565
;
3212 fmt
= DRM_FORMAT_RGB888
;
3216 fmt
= DRM_FORMAT_XRGB8888
;
3217 else if (depth
== 30)
3218 fmt
= DRM_FORMAT_XRGB2101010
;
3220 fmt
= DRM_FORMAT_ARGB8888
;
3223 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3224 fmt
= DRM_FORMAT_XRGB8888
;
3230 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
3233 * drm_mode_addfb - add an FB to the graphics configuration
3234 * @dev: drm device for the ioctl
3235 * @data: data pointer for the ioctl
3236 * @file_priv: drm file for the ioctl call
3238 * Add a new FB to the specified CRTC, given a user request. This is the
3239 * original addfb ioctl which only supported RGB formats.
3241 * Called by the user via ioctl.
3244 * Zero on success, negative errno on failure.
3246 int drm_mode_addfb(struct drm_device
*dev
,
3247 void *data
, struct drm_file
*file_priv
)
3249 struct drm_mode_fb_cmd
*or = data
;
3250 struct drm_mode_fb_cmd2 r
= {};
3253 /* convert to new format and call new ioctl */
3254 r
.fb_id
= or->fb_id
;
3255 r
.width
= or->width
;
3256 r
.height
= or->height
;
3257 r
.pitches
[0] = or->pitch
;
3258 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
3259 r
.handles
[0] = or->handle
;
3261 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
3265 or->fb_id
= r
.fb_id
;
3270 static int format_check(const struct drm_mode_fb_cmd2
*r
)
3272 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
3276 case DRM_FORMAT_RGB332
:
3277 case DRM_FORMAT_BGR233
:
3278 case DRM_FORMAT_XRGB4444
:
3279 case DRM_FORMAT_XBGR4444
:
3280 case DRM_FORMAT_RGBX4444
:
3281 case DRM_FORMAT_BGRX4444
:
3282 case DRM_FORMAT_ARGB4444
:
3283 case DRM_FORMAT_ABGR4444
:
3284 case DRM_FORMAT_RGBA4444
:
3285 case DRM_FORMAT_BGRA4444
:
3286 case DRM_FORMAT_XRGB1555
:
3287 case DRM_FORMAT_XBGR1555
:
3288 case DRM_FORMAT_RGBX5551
:
3289 case DRM_FORMAT_BGRX5551
:
3290 case DRM_FORMAT_ARGB1555
:
3291 case DRM_FORMAT_ABGR1555
:
3292 case DRM_FORMAT_RGBA5551
:
3293 case DRM_FORMAT_BGRA5551
:
3294 case DRM_FORMAT_RGB565
:
3295 case DRM_FORMAT_BGR565
:
3296 case DRM_FORMAT_RGB888
:
3297 case DRM_FORMAT_BGR888
:
3298 case DRM_FORMAT_XRGB8888
:
3299 case DRM_FORMAT_XBGR8888
:
3300 case DRM_FORMAT_RGBX8888
:
3301 case DRM_FORMAT_BGRX8888
:
3302 case DRM_FORMAT_ARGB8888
:
3303 case DRM_FORMAT_ABGR8888
:
3304 case DRM_FORMAT_RGBA8888
:
3305 case DRM_FORMAT_BGRA8888
:
3306 case DRM_FORMAT_XRGB2101010
:
3307 case DRM_FORMAT_XBGR2101010
:
3308 case DRM_FORMAT_RGBX1010102
:
3309 case DRM_FORMAT_BGRX1010102
:
3310 case DRM_FORMAT_ARGB2101010
:
3311 case DRM_FORMAT_ABGR2101010
:
3312 case DRM_FORMAT_RGBA1010102
:
3313 case DRM_FORMAT_BGRA1010102
:
3314 case DRM_FORMAT_YUYV
:
3315 case DRM_FORMAT_YVYU
:
3316 case DRM_FORMAT_UYVY
:
3317 case DRM_FORMAT_VYUY
:
3318 case DRM_FORMAT_AYUV
:
3319 case DRM_FORMAT_NV12
:
3320 case DRM_FORMAT_NV21
:
3321 case DRM_FORMAT_NV16
:
3322 case DRM_FORMAT_NV61
:
3323 case DRM_FORMAT_NV24
:
3324 case DRM_FORMAT_NV42
:
3325 case DRM_FORMAT_YUV410
:
3326 case DRM_FORMAT_YVU410
:
3327 case DRM_FORMAT_YUV411
:
3328 case DRM_FORMAT_YVU411
:
3329 case DRM_FORMAT_YUV420
:
3330 case DRM_FORMAT_YVU420
:
3331 case DRM_FORMAT_YUV422
:
3332 case DRM_FORMAT_YVU422
:
3333 case DRM_FORMAT_YUV444
:
3334 case DRM_FORMAT_YVU444
:
3337 DRM_DEBUG_KMS("invalid pixel format %s\n",
3338 drm_get_format_name(r
->pixel_format
));
3343 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3345 int ret
, hsub
, vsub
, num_planes
, i
;
3347 ret
= format_check(r
);
3349 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3350 drm_get_format_name(r
->pixel_format
));
3354 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3355 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3356 num_planes
= drm_format_num_planes(r
->pixel_format
);
3358 if (r
->width
== 0 || r
->width
% hsub
) {
3359 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
3363 if (r
->height
== 0 || r
->height
% vsub
) {
3364 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3368 for (i
= 0; i
< num_planes
; i
++) {
3369 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3370 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3371 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3373 if (!r
->handles
[i
]) {
3374 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3378 if ((uint64_t) width
* cpp
> UINT_MAX
)
3381 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3384 if (r
->pitches
[i
] < width
* cpp
) {
3385 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3389 if (r
->modifier
[i
] && !(r
->flags
& DRM_MODE_FB_MODIFIERS
)) {
3390 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3395 /* modifier specific checks: */
3396 switch (r
->modifier
[i
]) {
3397 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE
:
3398 /* NOTE: the pitch restriction may be lifted later if it turns
3399 * out that no hw has this restriction:
3401 if (r
->pixel_format
!= DRM_FORMAT_NV12
||
3402 width
% 128 || height
% 32 ||
3403 r
->pitches
[i
] % 128) {
3404 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i
);
3414 for (i
= num_planes
; i
< 4; i
++) {
3415 if (r
->modifier
[i
]) {
3416 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i
);
3420 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3421 if (!(r
->flags
& DRM_MODE_FB_MODIFIERS
))
3424 if (r
->handles
[i
]) {
3425 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i
);
3429 if (r
->pitches
[i
]) {
3430 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i
);
3434 if (r
->offsets
[i
]) {
3435 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i
);
3443 static struct drm_framebuffer
*
3444 internal_framebuffer_create(struct drm_device
*dev
,
3445 const struct drm_mode_fb_cmd2
*r
,
3446 struct drm_file
*file_priv
)
3448 struct drm_mode_config
*config
= &dev
->mode_config
;
3449 struct drm_framebuffer
*fb
;
3452 if (r
->flags
& ~(DRM_MODE_FB_INTERLACED
| DRM_MODE_FB_MODIFIERS
)) {
3453 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3454 return ERR_PTR(-EINVAL
);
3457 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3458 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3459 r
->width
, config
->min_width
, config
->max_width
);
3460 return ERR_PTR(-EINVAL
);
3462 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3463 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3464 r
->height
, config
->min_height
, config
->max_height
);
3465 return ERR_PTR(-EINVAL
);
3468 if (r
->flags
& DRM_MODE_FB_MODIFIERS
&&
3469 !dev
->mode_config
.allow_fb_modifiers
) {
3470 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3471 return ERR_PTR(-EINVAL
);
3474 ret
= framebuffer_check(r
);
3476 return ERR_PTR(ret
);
3478 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3480 DRM_DEBUG_KMS("could not create framebuffer\n");
3488 * drm_mode_addfb2 - add an FB to the graphics configuration
3489 * @dev: drm device for the ioctl
3490 * @data: data pointer for the ioctl
3491 * @file_priv: drm file for the ioctl call
3493 * Add a new FB to the specified CRTC, given a user request with format. This is
3494 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3495 * and uses fourcc codes as pixel format specifiers.
3497 * Called by the user via ioctl.
3500 * Zero on success, negative errno on failure.
3502 int drm_mode_addfb2(struct drm_device
*dev
,
3503 void *data
, struct drm_file
*file_priv
)
3505 struct drm_mode_fb_cmd2
*r
= data
;
3506 struct drm_framebuffer
*fb
;
3508 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3511 fb
= internal_framebuffer_create(dev
, r
, file_priv
);
3515 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3516 r
->fb_id
= fb
->base
.id
;
3518 /* Transfer ownership to the filp for reaping on close */
3519 mutex_lock(&file_priv
->fbs_lock
);
3520 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3521 mutex_unlock(&file_priv
->fbs_lock
);
3526 struct drm_mode_rmfb_work
{
3527 struct work_struct work
;
3528 struct list_head fbs
;
3531 static void drm_mode_rmfb_work_fn(struct work_struct
*w
)
3533 struct drm_mode_rmfb_work
*arg
= container_of(w
, typeof(*arg
), work
);
3535 while (!list_empty(&arg
->fbs
)) {
3536 struct drm_framebuffer
*fb
=
3537 list_first_entry(&arg
->fbs
, typeof(*fb
), filp_head
);
3539 list_del_init(&fb
->filp_head
);
3540 drm_framebuffer_remove(fb
);
3545 * drm_mode_rmfb - remove an FB from the configuration
3546 * @dev: drm device for the ioctl
3547 * @data: data pointer for the ioctl
3548 * @file_priv: drm file for the ioctl call
3550 * Remove the FB specified by the user.
3552 * Called by the user via ioctl.
3555 * Zero on success, negative errno on failure.
3557 int drm_mode_rmfb(struct drm_device
*dev
,
3558 void *data
, struct drm_file
*file_priv
)
3560 struct drm_framebuffer
*fb
= NULL
;
3561 struct drm_framebuffer
*fbl
= NULL
;
3562 uint32_t *id
= data
;
3565 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3568 fb
= drm_framebuffer_lookup(dev
, *id
);
3572 mutex_lock(&file_priv
->fbs_lock
);
3573 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3577 mutex_unlock(&file_priv
->fbs_lock
);
3581 list_del_init(&fb
->filp_head
);
3582 mutex_unlock(&file_priv
->fbs_lock
);
3584 /* drop the reference we picked up in framebuffer lookup */
3585 drm_framebuffer_unreference(fb
);
3588 * we now own the reference that was stored in the fbs list
3590 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3591 * so run this in a separate stack as there's no way to correctly
3592 * handle this after the fb is already removed from the lookup table.
3594 if (drm_framebuffer_read_refcount(fb
) > 1) {
3595 struct drm_mode_rmfb_work arg
;
3597 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3598 INIT_LIST_HEAD(&arg
.fbs
);
3599 list_add_tail(&fb
->filp_head
, &arg
.fbs
);
3601 schedule_work(&arg
.work
);
3602 flush_work(&arg
.work
);
3603 destroy_work_on_stack(&arg
.work
);
3605 drm_framebuffer_unreference(fb
);
3610 drm_framebuffer_unreference(fb
);
3615 * drm_mode_getfb - get FB info
3616 * @dev: drm device for the ioctl
3617 * @data: data pointer for the ioctl
3618 * @file_priv: drm file for the ioctl call
3620 * Lookup the FB given its ID and return info about it.
3622 * Called by the user via ioctl.
3625 * Zero on success, negative errno on failure.
3627 int drm_mode_getfb(struct drm_device
*dev
,
3628 void *data
, struct drm_file
*file_priv
)
3630 struct drm_mode_fb_cmd
*r
= data
;
3631 struct drm_framebuffer
*fb
;
3634 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3637 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3641 r
->height
= fb
->height
;
3642 r
->width
= fb
->width
;
3643 r
->depth
= fb
->depth
;
3644 r
->bpp
= fb
->bits_per_pixel
;
3645 r
->pitch
= fb
->pitches
[0];
3646 if (fb
->funcs
->create_handle
) {
3647 if (drm_is_current_master(file_priv
) || capable(CAP_SYS_ADMIN
) ||
3648 drm_is_control_client(file_priv
)) {
3649 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3652 /* GET_FB() is an unprivileged ioctl so we must not
3653 * return a buffer-handle to non-master processes! For
3654 * backwards-compatibility reasons, we cannot make
3655 * GET_FB() privileged, so just return an invalid handle
3656 * for non-masters. */
3664 drm_framebuffer_unreference(fb
);
3670 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3671 * @dev: drm device for the ioctl
3672 * @data: data pointer for the ioctl
3673 * @file_priv: drm file for the ioctl call
3675 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3676 * rectangle list. Generic userspace which does frontbuffer rendering must call
3677 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3678 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3680 * Modesetting drivers which always update the frontbuffer do not need to
3681 * implement the corresponding ->dirty framebuffer callback.
3683 * Called by the user via ioctl.
3686 * Zero on success, negative errno on failure.
3688 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3689 void *data
, struct drm_file
*file_priv
)
3691 struct drm_clip_rect __user
*clips_ptr
;
3692 struct drm_clip_rect
*clips
= NULL
;
3693 struct drm_mode_fb_dirty_cmd
*r
= data
;
3694 struct drm_framebuffer
*fb
;
3699 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3702 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3706 num_clips
= r
->num_clips
;
3707 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3709 if (!num_clips
!= !clips_ptr
) {
3714 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3716 /* If userspace annotates copy, clips must come in pairs */
3717 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3722 if (num_clips
&& clips_ptr
) {
3723 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3727 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
3733 ret
= copy_from_user(clips
, clips_ptr
,
3734 num_clips
* sizeof(*clips
));
3741 if (fb
->funcs
->dirty
) {
3742 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3751 drm_framebuffer_unreference(fb
);
3757 * drm_fb_release - remove and free the FBs on this file
3758 * @priv: drm file for the ioctl
3760 * Destroy all the FBs associated with @filp.
3762 * Called by the user via ioctl.
3765 * Zero on success, negative errno on failure.
3767 void drm_fb_release(struct drm_file
*priv
)
3769 struct drm_framebuffer
*fb
, *tfb
;
3770 struct drm_mode_rmfb_work arg
;
3772 INIT_LIST_HEAD(&arg
.fbs
);
3775 * When the file gets released that means no one else can access the fb
3776 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3777 * avoid upsetting lockdep since the universal cursor code adds a
3778 * framebuffer while holding mutex locks.
3780 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3781 * locks is impossible here since no one else but this function can get
3784 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3785 if (drm_framebuffer_read_refcount(fb
) > 1) {
3786 list_move_tail(&fb
->filp_head
, &arg
.fbs
);
3788 list_del_init(&fb
->filp_head
);
3790 /* This drops the fpriv->fbs reference. */
3791 drm_framebuffer_unreference(fb
);
3795 if (!list_empty(&arg
.fbs
)) {
3796 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3798 schedule_work(&arg
.work
);
3799 flush_work(&arg
.work
);
3800 destroy_work_on_stack(&arg
.work
);
3804 static bool drm_property_type_valid(struct drm_property
*property
)
3806 if (property
->flags
& DRM_MODE_PROP_EXTENDED_TYPE
)
3807 return !(property
->flags
& DRM_MODE_PROP_LEGACY_TYPE
);
3808 return !!(property
->flags
& DRM_MODE_PROP_LEGACY_TYPE
);
3812 * drm_property_create - create a new property type
3814 * @flags: flags specifying the property type
3815 * @name: name of the property
3816 * @num_values: number of pre-defined values
3818 * This creates a new generic drm property which can then be attached to a drm
3819 * object with drm_object_attach_property. The returned property object must be
3820 * freed with drm_property_destroy.
3822 * Note that the DRM core keeps a per-device list of properties and that, if
3823 * drm_mode_config_cleanup() is called, it will destroy all properties created
3827 * A pointer to the newly created property on success, NULL on failure.
3829 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3830 const char *name
, int num_values
)
3832 struct drm_property
*property
= NULL
;
3835 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3839 property
->dev
= dev
;
3842 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
3844 if (!property
->values
)
3848 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3852 property
->flags
= flags
;
3853 property
->num_values
= num_values
;
3854 INIT_LIST_HEAD(&property
->enum_list
);
3857 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3858 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3861 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3863 WARN_ON(!drm_property_type_valid(property
));
3867 kfree(property
->values
);
3871 EXPORT_SYMBOL(drm_property_create
);
3874 * drm_property_create_enum - create a new enumeration property type
3876 * @flags: flags specifying the property type
3877 * @name: name of the property
3878 * @props: enumeration lists with property values
3879 * @num_values: number of pre-defined values
3881 * This creates a new generic drm property which can then be attached to a drm
3882 * object with drm_object_attach_property. The returned property object must be
3883 * freed with drm_property_destroy.
3885 * Userspace is only allowed to set one of the predefined values for enumeration
3889 * A pointer to the newly created property on success, NULL on failure.
3891 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3893 const struct drm_prop_enum_list
*props
,
3896 struct drm_property
*property
;
3899 flags
|= DRM_MODE_PROP_ENUM
;
3901 property
= drm_property_create(dev
, flags
, name
, num_values
);
3905 for (i
= 0; i
< num_values
; i
++) {
3906 ret
= drm_property_add_enum(property
, i
,
3910 drm_property_destroy(dev
, property
);
3917 EXPORT_SYMBOL(drm_property_create_enum
);
3920 * drm_property_create_bitmask - create a new bitmask property type
3922 * @flags: flags specifying the property type
3923 * @name: name of the property
3924 * @props: enumeration lists with property bitflags
3925 * @num_props: size of the @props array
3926 * @supported_bits: bitmask of all supported enumeration values
3928 * This creates a new bitmask drm property which can then be attached to a drm
3929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3932 * Compared to plain enumeration properties userspace is allowed to set any
3933 * or'ed together combination of the predefined property bitflag values
3936 * A pointer to the newly created property on success, NULL on failure.
3938 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3939 int flags
, const char *name
,
3940 const struct drm_prop_enum_list
*props
,
3942 uint64_t supported_bits
)
3944 struct drm_property
*property
;
3945 int i
, ret
, index
= 0;
3946 int num_values
= hweight64(supported_bits
);
3948 flags
|= DRM_MODE_PROP_BITMASK
;
3950 property
= drm_property_create(dev
, flags
, name
, num_values
);
3953 for (i
= 0; i
< num_props
; i
++) {
3954 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3957 if (WARN_ON(index
>= num_values
)) {
3958 drm_property_destroy(dev
, property
);
3962 ret
= drm_property_add_enum(property
, index
++,
3966 drm_property_destroy(dev
, property
);
3973 EXPORT_SYMBOL(drm_property_create_bitmask
);
3975 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3976 int flags
, const char *name
,
3977 uint64_t min
, uint64_t max
)
3979 struct drm_property
*property
;
3981 property
= drm_property_create(dev
, flags
, name
, 2);
3985 property
->values
[0] = min
;
3986 property
->values
[1] = max
;
3992 * drm_property_create_range - create a new unsigned ranged property type
3994 * @flags: flags specifying the property type
3995 * @name: name of the property
3996 * @min: minimum value of the property
3997 * @max: maximum value of the property
3999 * This creates a new generic drm property which can then be attached to a drm
4000 * object with drm_object_attach_property. The returned property object must be
4001 * freed with drm_property_destroy.
4003 * Userspace is allowed to set any unsigned integer value in the (min, max)
4007 * A pointer to the newly created property on success, NULL on failure.
4009 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
4011 uint64_t min
, uint64_t max
)
4013 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
4016 EXPORT_SYMBOL(drm_property_create_range
);
4019 * drm_property_create_signed_range - create a new signed ranged property type
4021 * @flags: flags specifying the property type
4022 * @name: name of the property
4023 * @min: minimum value of the property
4024 * @max: maximum value of the property
4026 * This creates a new generic drm property which can then be attached to a drm
4027 * object with drm_object_attach_property. The returned property object must be
4028 * freed with drm_property_destroy.
4030 * Userspace is allowed to set any signed integer value in the (min, max)
4034 * A pointer to the newly created property on success, NULL on failure.
4036 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
4037 int flags
, const char *name
,
4038 int64_t min
, int64_t max
)
4040 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
4041 name
, I642U64(min
), I642U64(max
));
4043 EXPORT_SYMBOL(drm_property_create_signed_range
);
4046 * drm_property_create_object - create a new object property type
4048 * @flags: flags specifying the property type
4049 * @name: name of the property
4050 * @type: object type from DRM_MODE_OBJECT_* defines
4052 * This creates a new generic drm property which can then be attached to a drm
4053 * object with drm_object_attach_property. The returned property object must be
4054 * freed with drm_property_destroy.
4056 * Userspace is only allowed to set this to any property value of the given
4057 * @type. Only useful for atomic properties, which is enforced.
4060 * A pointer to the newly created property on success, NULL on failure.
4062 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
4063 int flags
, const char *name
, uint32_t type
)
4065 struct drm_property
*property
;
4067 flags
|= DRM_MODE_PROP_OBJECT
;
4069 if (WARN_ON(!(flags
& DRM_MODE_PROP_ATOMIC
)))
4072 property
= drm_property_create(dev
, flags
, name
, 1);
4076 property
->values
[0] = type
;
4080 EXPORT_SYMBOL(drm_property_create_object
);
4083 * drm_property_create_bool - create a new boolean property type
4085 * @flags: flags specifying the property type
4086 * @name: name of the property
4088 * This creates a new generic drm property which can then be attached to a drm
4089 * object with drm_object_attach_property. The returned property object must be
4090 * freed with drm_property_destroy.
4092 * This is implemented as a ranged property with only {0, 1} as valid values.
4095 * A pointer to the newly created property on success, NULL on failure.
4097 struct drm_property
*drm_property_create_bool(struct drm_device
*dev
, int flags
,
4100 return drm_property_create_range(dev
, flags
, name
, 0, 1);
4102 EXPORT_SYMBOL(drm_property_create_bool
);
4105 * drm_property_add_enum - add a possible value to an enumeration property
4106 * @property: enumeration property to change
4107 * @index: index of the new enumeration
4108 * @value: value of the new enumeration
4109 * @name: symbolic name of the new enumeration
4111 * This functions adds enumerations to a property.
4113 * It's use is deprecated, drivers should use one of the more specific helpers
4114 * to directly create the property with all enumerations already attached.
4117 * Zero on success, error code on failure.
4119 int drm_property_add_enum(struct drm_property
*property
, int index
,
4120 uint64_t value
, const char *name
)
4122 struct drm_property_enum
*prop_enum
;
4124 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4125 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
4129 * Bitmask enum properties have the additional constraint of values
4132 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
4136 if (!list_empty(&property
->enum_list
)) {
4137 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4138 if (prop_enum
->value
== value
) {
4139 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
4140 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
4146 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
4150 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
4151 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
4152 prop_enum
->value
= value
;
4154 property
->values
[index
] = value
;
4155 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
4158 EXPORT_SYMBOL(drm_property_add_enum
);
4161 * drm_property_destroy - destroy a drm property
4163 * @property: property to destry
4165 * This function frees a property including any attached resources like
4166 * enumeration values.
4168 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
4170 struct drm_property_enum
*prop_enum
, *pt
;
4172 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
4173 list_del(&prop_enum
->head
);
4177 if (property
->num_values
)
4178 kfree(property
->values
);
4179 drm_mode_object_unregister(dev
, &property
->base
);
4180 list_del(&property
->head
);
4183 EXPORT_SYMBOL(drm_property_destroy
);
4186 * drm_object_attach_property - attach a property to a modeset object
4187 * @obj: drm modeset object
4188 * @property: property to attach
4189 * @init_val: initial value of the property
4191 * This attaches the given property to the modeset object with the given initial
4192 * value. Currently this function cannot fail since the properties are stored in
4193 * a statically sized array.
4195 void drm_object_attach_property(struct drm_mode_object
*obj
,
4196 struct drm_property
*property
,
4199 int count
= obj
->properties
->count
;
4201 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
4202 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4203 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4204 "you see this message on the same object type.\n",
4209 obj
->properties
->properties
[count
] = property
;
4210 obj
->properties
->values
[count
] = init_val
;
4211 obj
->properties
->count
++;
4212 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
4213 obj
->properties
->atomic_count
++;
4215 EXPORT_SYMBOL(drm_object_attach_property
);
4218 * drm_object_property_set_value - set the value of a property
4219 * @obj: drm mode object to set property value for
4220 * @property: property to set
4221 * @val: value the property should be set to
4223 * This functions sets a given property on a given object. This function only
4224 * changes the software state of the property, it does not call into the
4225 * driver's ->set_property callback.
4228 * Zero on success, error code on failure.
4230 int drm_object_property_set_value(struct drm_mode_object
*obj
,
4231 struct drm_property
*property
, uint64_t val
)
4235 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4236 if (obj
->properties
->properties
[i
] == property
) {
4237 obj
->properties
->values
[i
] = val
;
4244 EXPORT_SYMBOL(drm_object_property_set_value
);
4247 * drm_object_property_get_value - retrieve the value of a property
4248 * @obj: drm mode object to get property value from
4249 * @property: property to retrieve
4250 * @val: storage for the property value
4252 * This function retrieves the softare state of the given property for the given
4253 * property. Since there is no driver callback to retrieve the current property
4254 * value this might be out of sync with the hardware, depending upon the driver
4258 * Zero on success, error code on failure.
4260 int drm_object_property_get_value(struct drm_mode_object
*obj
,
4261 struct drm_property
*property
, uint64_t *val
)
4265 /* read-only properties bypass atomic mechanism and still store
4266 * their value in obj->properties->values[].. mostly to avoid
4267 * having to deal w/ EDID and similar props in atomic paths:
4269 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
4270 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
4271 return drm_atomic_get_property(obj
, property
, val
);
4273 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4274 if (obj
->properties
->properties
[i
] == property
) {
4275 *val
= obj
->properties
->values
[i
];
4282 EXPORT_SYMBOL(drm_object_property_get_value
);
4285 * drm_mode_getproperty_ioctl - get the property metadata
4288 * @file_priv: DRM file info
4290 * This function retrieves the metadata for a given property, like the different
4291 * possible values for an enum property or the limits for a range property.
4293 * Blob properties are special
4295 * Called by the user via ioctl.
4298 * Zero on success, negative errno on failure.
4300 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
4301 void *data
, struct drm_file
*file_priv
)
4303 struct drm_mode_get_property
*out_resp
= data
;
4304 struct drm_property
*property
;
4306 int value_count
= 0;
4309 struct drm_property_enum
*prop_enum
;
4310 struct drm_mode_property_enum __user
*enum_ptr
;
4311 uint64_t __user
*values_ptr
;
4313 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4316 drm_modeset_lock_all(dev
);
4317 property
= drm_property_find(dev
, out_resp
->prop_id
);
4323 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4324 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4325 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
4329 value_count
= property
->num_values
;
4331 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
4332 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
4333 out_resp
->flags
= property
->flags
;
4335 if ((out_resp
->count_values
>= value_count
) && value_count
) {
4336 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
4337 for (i
= 0; i
< value_count
; i
++) {
4338 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
4344 out_resp
->count_values
= value_count
;
4346 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4347 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4348 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
4350 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
4351 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4353 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
4358 if (copy_to_user(&enum_ptr
[copied
].name
,
4359 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
4366 out_resp
->count_enum_blobs
= enum_count
;
4370 * NOTE: The idea seems to have been to use this to read all the blob
4371 * property values. But nothing ever added them to the corresponding
4372 * list, userspace always used the special-purpose get_blob ioctl to
4373 * read the value for a blob property. It also doesn't make a lot of
4374 * sense to return values here when everything else is just metadata for
4375 * the property itself.
4377 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4378 out_resp
->count_enum_blobs
= 0;
4380 drm_modeset_unlock_all(dev
);
4384 static void drm_property_free_blob(struct kref
*kref
)
4386 struct drm_property_blob
*blob
=
4387 container_of(kref
, struct drm_property_blob
, base
.refcount
);
4389 mutex_lock(&blob
->dev
->mode_config
.blob_lock
);
4390 list_del(&blob
->head_global
);
4391 mutex_unlock(&blob
->dev
->mode_config
.blob_lock
);
4393 drm_mode_object_unregister(blob
->dev
, &blob
->base
);
4399 * drm_property_create_blob - Create new blob property
4401 * Creates a new blob property for a specified DRM device, optionally
4404 * @dev: DRM device to create property for
4405 * @length: Length to allocate for blob data
4406 * @data: If specified, copies data into blob
4409 * New blob property with a single reference on success, or an ERR_PTR
4412 struct drm_property_blob
*
4413 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
4416 struct drm_property_blob
*blob
;
4419 if (!length
|| length
> ULONG_MAX
- sizeof(struct drm_property_blob
))
4420 return ERR_PTR(-EINVAL
);
4422 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
4424 return ERR_PTR(-ENOMEM
);
4426 /* This must be explicitly initialised, so we can safely call list_del
4427 * on it in the removal handler, even if it isn't in a file list. */
4428 INIT_LIST_HEAD(&blob
->head_file
);
4429 blob
->length
= length
;
4433 memcpy(blob
->data
, data
, length
);
4435 ret
= drm_mode_object_get_reg(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
,
4436 true, drm_property_free_blob
);
4439 return ERR_PTR(-EINVAL
);
4442 mutex_lock(&dev
->mode_config
.blob_lock
);
4443 list_add_tail(&blob
->head_global
,
4444 &dev
->mode_config
.property_blob_list
);
4445 mutex_unlock(&dev
->mode_config
.blob_lock
);
4449 EXPORT_SYMBOL(drm_property_create_blob
);
4452 * drm_property_unreference_blob - Unreference a blob property
4454 * Drop a reference on a blob property. May free the object.
4456 * @blob: Pointer to blob property
4458 void drm_property_unreference_blob(struct drm_property_blob
*blob
)
4463 drm_mode_object_unreference(&blob
->base
);
4465 EXPORT_SYMBOL(drm_property_unreference_blob
);
4468 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4470 * @file_priv: destroy all blobs owned by this file handle
4472 void drm_property_destroy_user_blobs(struct drm_device
*dev
,
4473 struct drm_file
*file_priv
)
4475 struct drm_property_blob
*blob
, *bt
;
4478 * When the file gets released that means no one else can access the
4479 * blob list any more, so no need to grab dev->blob_lock.
4481 list_for_each_entry_safe(blob
, bt
, &file_priv
->blobs
, head_file
) {
4482 list_del_init(&blob
->head_file
);
4483 drm_property_unreference_blob(blob
);
4488 * drm_property_reference_blob - Take a reference on an existing property
4490 * Take a new reference on an existing blob property.
4492 * @blob: Pointer to blob property
4494 struct drm_property_blob
*drm_property_reference_blob(struct drm_property_blob
*blob
)
4496 drm_mode_object_reference(&blob
->base
);
4499 EXPORT_SYMBOL(drm_property_reference_blob
);
4502 * drm_property_lookup_blob - look up a blob property and take a reference
4504 * @id: id of the blob property
4506 * If successful, this takes an additional reference to the blob property.
4507 * callers need to make sure to eventually unreference the returned property
4508 * again, using @drm_property_unreference_blob.
4510 struct drm_property_blob
*drm_property_lookup_blob(struct drm_device
*dev
,
4513 struct drm_mode_object
*obj
;
4514 struct drm_property_blob
*blob
= NULL
;
4516 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_BLOB
);
4518 blob
= obj_to_blob(obj
);
4521 EXPORT_SYMBOL(drm_property_lookup_blob
);
4524 * drm_property_replace_global_blob - atomically replace existing blob property
4526 * @replace: location of blob property pointer to be replaced
4527 * @length: length of data for new blob, or 0 for no data
4528 * @data: content for new blob, or NULL for no data
4529 * @obj_holds_id: optional object for property holding blob ID
4530 * @prop_holds_id: optional property holding blob ID
4531 * @return 0 on success or error on failure
4533 * This function will atomically replace a global property in the blob list,
4534 * optionally updating a property which holds the ID of that property. It is
4535 * guaranteed to be atomic: no caller will be allowed to see intermediate
4536 * results, and either the entire operation will succeed and clean up the
4537 * previous property, or it will fail and the state will be unchanged.
4539 * If length is 0 or data is NULL, no new blob will be created, and the holding
4540 * property, if specified, will be set to 0.
4542 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4543 * by holding the relevant modesetting object lock for its parent.
4545 * For example, a drm_connector has a 'PATH' property, which contains the ID
4546 * of a blob property with the value of the MST path information. Calling this
4547 * function with replace pointing to the connector's path_blob_ptr, length and
4548 * data set for the new path information, obj_holds_id set to the connector's
4549 * base object, and prop_holds_id set to the path property name, will perform
4550 * a completely atomic update. The access to path_blob_ptr is protected by the
4551 * caller holding a lock on the connector.
4553 static int drm_property_replace_global_blob(struct drm_device
*dev
,
4554 struct drm_property_blob
**replace
,
4557 struct drm_mode_object
*obj_holds_id
,
4558 struct drm_property
*prop_holds_id
)
4560 struct drm_property_blob
*new_blob
= NULL
;
4561 struct drm_property_blob
*old_blob
= NULL
;
4564 WARN_ON(replace
== NULL
);
4566 old_blob
= *replace
;
4568 if (length
&& data
) {
4569 new_blob
= drm_property_create_blob(dev
, length
, data
);
4570 if (IS_ERR(new_blob
))
4571 return PTR_ERR(new_blob
);
4574 /* This does not need to be synchronised with blob_lock, as the
4575 * get_properties ioctl locks all modesetting objects, and
4576 * obj_holds_id must be locked before calling here, so we cannot
4577 * have its value out of sync with the list membership modified
4578 * below under blob_lock. */
4580 ret
= drm_object_property_set_value(obj_holds_id
,
4583 new_blob
->base
.id
: 0);
4588 drm_property_unreference_blob(old_blob
);
4589 *replace
= new_blob
;
4594 drm_property_unreference_blob(new_blob
);
4599 * drm_mode_getblob_ioctl - get the contents of a blob property value
4602 * @file_priv: DRM file info
4604 * This function retrieves the contents of a blob property. The value stored in
4605 * an object's blob property is just a normal modeset object id.
4607 * Called by the user via ioctl.
4610 * Zero on success, negative errno on failure.
4612 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
4613 void *data
, struct drm_file
*file_priv
)
4615 struct drm_mode_get_blob
*out_resp
= data
;
4616 struct drm_property_blob
*blob
;
4618 void __user
*blob_ptr
;
4620 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4623 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4627 if (out_resp
->length
== blob
->length
) {
4628 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4629 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
4634 out_resp
->length
= blob
->length
;
4636 drm_property_unreference_blob(blob
);
4642 * drm_mode_createblob_ioctl - create a new blob property
4645 * @file_priv: DRM file info
4647 * This function creates a new blob property with user-defined values. In order
4648 * to give us sensible validation and checking when creating, rather than at
4649 * every potential use, we also require a type to be provided upfront.
4651 * Called by the user via ioctl.
4654 * Zero on success, negative errno on failure.
4656 int drm_mode_createblob_ioctl(struct drm_device
*dev
,
4657 void *data
, struct drm_file
*file_priv
)
4659 struct drm_mode_create_blob
*out_resp
= data
;
4660 struct drm_property_blob
*blob
;
4661 void __user
*blob_ptr
;
4664 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4667 blob
= drm_property_create_blob(dev
, out_resp
->length
, NULL
);
4669 return PTR_ERR(blob
);
4671 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4672 if (copy_from_user(blob
->data
, blob_ptr
, out_resp
->length
)) {
4677 /* Dropping the lock between create_blob and our access here is safe
4678 * as only the same file_priv can remove the blob; at this point, it is
4679 * not associated with any file_priv. */
4680 mutex_lock(&dev
->mode_config
.blob_lock
);
4681 out_resp
->blob_id
= blob
->base
.id
;
4682 list_add_tail(&blob
->head_file
, &file_priv
->blobs
);
4683 mutex_unlock(&dev
->mode_config
.blob_lock
);
4688 drm_property_unreference_blob(blob
);
4693 * drm_mode_destroyblob_ioctl - destroy a user blob property
4696 * @file_priv: DRM file info
4698 * Destroy an existing user-defined blob property.
4700 * Called by the user via ioctl.
4703 * Zero on success, negative errno on failure.
4705 int drm_mode_destroyblob_ioctl(struct drm_device
*dev
,
4706 void *data
, struct drm_file
*file_priv
)
4708 struct drm_mode_destroy_blob
*out_resp
= data
;
4709 struct drm_property_blob
*blob
= NULL
, *bt
;
4713 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4716 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4720 mutex_lock(&dev
->mode_config
.blob_lock
);
4721 /* Ensure the property was actually created by this user. */
4722 list_for_each_entry(bt
, &file_priv
->blobs
, head_file
) {
4734 /* We must drop head_file here, because we may not be the last
4735 * reference on the blob. */
4736 list_del_init(&blob
->head_file
);
4737 mutex_unlock(&dev
->mode_config
.blob_lock
);
4739 /* One reference from lookup, and one from the filp. */
4740 drm_property_unreference_blob(blob
);
4741 drm_property_unreference_blob(blob
);
4746 mutex_unlock(&dev
->mode_config
.blob_lock
);
4747 drm_property_unreference_blob(blob
);
4753 * drm_mode_connector_set_path_property - set tile property on connector
4754 * @connector: connector to set property on.
4755 * @path: path to use for property; must not be NULL.
4757 * This creates a property to expose to userspace to specify a
4758 * connector path. This is mainly used for DisplayPort MST where
4759 * connectors have a topology and we want to allow userspace to give
4760 * them more meaningful names.
4763 * Zero on success, negative errno on failure.
4765 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4768 struct drm_device
*dev
= connector
->dev
;
4771 ret
= drm_property_replace_global_blob(dev
,
4772 &connector
->path_blob_ptr
,
4776 dev
->mode_config
.path_property
);
4779 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4782 * drm_mode_connector_set_tile_property - set tile property on connector
4783 * @connector: connector to set property on.
4785 * This looks up the tile information for a connector, and creates a
4786 * property for userspace to parse if it exists. The property is of
4787 * the form of 8 integers using ':' as a separator.
4790 * Zero on success, errno on failure.
4792 int drm_mode_connector_set_tile_property(struct drm_connector
*connector
)
4794 struct drm_device
*dev
= connector
->dev
;
4798 if (!connector
->has_tile
) {
4799 ret
= drm_property_replace_global_blob(dev
,
4800 &connector
->tile_blob_ptr
,
4804 dev
->mode_config
.tile_property
);
4808 snprintf(tile
, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4809 connector
->tile_group
->id
, connector
->tile_is_single_monitor
,
4810 connector
->num_h_tile
, connector
->num_v_tile
,
4811 connector
->tile_h_loc
, connector
->tile_v_loc
,
4812 connector
->tile_h_size
, connector
->tile_v_size
);
4814 ret
= drm_property_replace_global_blob(dev
,
4815 &connector
->tile_blob_ptr
,
4819 dev
->mode_config
.tile_property
);
4822 EXPORT_SYMBOL(drm_mode_connector_set_tile_property
);
4825 * drm_mode_connector_update_edid_property - update the edid property of a connector
4826 * @connector: drm connector
4827 * @edid: new value of the edid property
4829 * This function creates a new blob modeset object and assigns its id to the
4830 * connector's edid property.
4833 * Zero on success, negative errno on failure.
4835 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4836 const struct edid
*edid
)
4838 struct drm_device
*dev
= connector
->dev
;
4842 /* ignore requests to set edid when overridden */
4843 if (connector
->override_edid
)
4847 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4849 ret
= drm_property_replace_global_blob(dev
,
4850 &connector
->edid_blob_ptr
,
4854 dev
->mode_config
.edid_property
);
4857 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4859 /* Some properties could refer to dynamic refcnt'd objects, or things that
4860 * need special locking to handle lifetime issues (ie. to ensure the prop
4861 * value doesn't become invalid part way through the property update due to
4862 * race). The value returned by reference via 'obj' should be passed back
4863 * to drm_property_change_valid_put() after the property is set (and the
4864 * object to which the property is attached has a chance to take it's own
4867 bool drm_property_change_valid_get(struct drm_property
*property
,
4868 uint64_t value
, struct drm_mode_object
**ref
)
4872 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4877 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4878 if (value
< property
->values
[0] || value
> property
->values
[1])
4881 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4882 int64_t svalue
= U642I64(value
);
4884 if (svalue
< U642I64(property
->values
[0]) ||
4885 svalue
> U642I64(property
->values
[1]))
4888 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4889 uint64_t valid_mask
= 0;
4891 for (i
= 0; i
< property
->num_values
; i
++)
4892 valid_mask
|= (1ULL << property
->values
[i
]);
4893 return !(value
& ~valid_mask
);
4894 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4895 struct drm_property_blob
*blob
;
4900 blob
= drm_property_lookup_blob(property
->dev
, value
);
4907 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4908 /* a zero value for an object property translates to null: */
4912 *ref
= _object_find(property
->dev
, value
, property
->values
[0]);
4913 return *ref
!= NULL
;
4916 for (i
= 0; i
< property
->num_values
; i
++)
4917 if (property
->values
[i
] == value
)
4922 void drm_property_change_valid_put(struct drm_property
*property
,
4923 struct drm_mode_object
*ref
)
4928 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4929 drm_mode_object_unreference(ref
);
4930 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4931 drm_property_unreference_blob(obj_to_blob(ref
));
4935 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4938 * @file_priv: DRM file info
4940 * This function sets the current value for a connectors's property. It also
4941 * calls into a driver's ->set_property callback to update the hardware state
4943 * Called by the user via ioctl.
4946 * Zero on success, negative errno on failure.
4948 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4949 void *data
, struct drm_file
*file_priv
)
4951 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4952 struct drm_mode_obj_set_property obj_set_prop
= {
4953 .value
= conn_set_prop
->value
,
4954 .prop_id
= conn_set_prop
->prop_id
,
4955 .obj_id
= conn_set_prop
->connector_id
,
4956 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4959 /* It does all the locking and checking we need */
4960 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4963 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4964 struct drm_property
*property
,
4968 struct drm_connector
*connector
= obj_to_connector(obj
);
4970 /* Do DPMS ourselves */
4971 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4972 ret
= (*connector
->funcs
->dpms
)(connector
, (int)value
);
4973 } else if (connector
->funcs
->set_property
)
4974 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4976 /* store the property value if successful */
4978 drm_object_property_set_value(&connector
->base
, property
, value
);
4982 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4983 struct drm_property
*property
,
4987 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4989 if (crtc
->funcs
->set_property
)
4990 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4992 drm_object_property_set_value(obj
, property
, value
);
4998 * drm_mode_plane_set_obj_prop - set the value of a property
4999 * @plane: drm plane object to set property value for
5000 * @property: property to set
5001 * @value: value the property should be set to
5003 * This functions sets a given property on a given plane object. This function
5004 * calls the driver's ->set_property callback and changes the software state of
5005 * the property if the callback succeeds.
5008 * Zero on success, error code on failure.
5010 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
5011 struct drm_property
*property
,
5015 struct drm_mode_object
*obj
= &plane
->base
;
5017 if (plane
->funcs
->set_property
)
5018 ret
= plane
->funcs
->set_property(plane
, property
, value
);
5020 drm_object_property_set_value(obj
, property
, value
);
5024 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
5027 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
5030 * @file_priv: DRM file info
5032 * This function retrieves the current value for an object's property. Compared
5033 * to the connector specific ioctl this one is extended to also work on crtc and
5036 * Called by the user via ioctl.
5039 * Zero on success, negative errno on failure.
5041 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
5042 struct drm_file
*file_priv
)
5044 struct drm_mode_obj_get_properties
*arg
= data
;
5045 struct drm_mode_object
*obj
;
5048 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5051 drm_modeset_lock_all(dev
);
5053 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
5058 if (!obj
->properties
) {
5063 ret
= get_properties(obj
, file_priv
->atomic
,
5064 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
5065 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
5069 drm_mode_object_unreference(obj
);
5071 drm_modeset_unlock_all(dev
);
5076 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5079 * @file_priv: DRM file info
5081 * This function sets the current value for an object's property. It also calls
5082 * into a driver's ->set_property callback to update the hardware state.
5083 * Compared to the connector specific ioctl this one is extended to also work on
5084 * crtc and plane objects.
5086 * Called by the user via ioctl.
5089 * Zero on success, negative errno on failure.
5091 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
5092 struct drm_file
*file_priv
)
5094 struct drm_mode_obj_set_property
*arg
= data
;
5095 struct drm_mode_object
*arg_obj
;
5096 struct drm_mode_object
*prop_obj
;
5097 struct drm_property
*property
;
5098 int i
, ret
= -EINVAL
;
5099 struct drm_mode_object
*ref
;
5101 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5104 drm_modeset_lock_all(dev
);
5106 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
5111 if (!arg_obj
->properties
)
5114 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
5115 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
5118 if (i
== arg_obj
->properties
->count
)
5121 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
5122 DRM_MODE_OBJECT_PROPERTY
);
5127 property
= obj_to_property(prop_obj
);
5129 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
5132 switch (arg_obj
->type
) {
5133 case DRM_MODE_OBJECT_CONNECTOR
:
5134 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
5137 case DRM_MODE_OBJECT_CRTC
:
5138 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
5140 case DRM_MODE_OBJECT_PLANE
:
5141 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
5142 property
, arg
->value
);
5146 drm_property_change_valid_put(property
, ref
);
5149 drm_mode_object_unreference(arg_obj
);
5151 drm_modeset_unlock_all(dev
);
5156 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5157 * @connector: connector to attach
5158 * @encoder: encoder to attach @connector to
5160 * This function links up a connector to an encoder. Note that the routing
5161 * restrictions between encoders and crtcs are exposed to userspace through the
5162 * possible_clones and possible_crtcs bitmasks.
5165 * Zero on success, negative errno on failure.
5167 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
5168 struct drm_encoder
*encoder
)
5173 * In the past, drivers have attempted to model the static association
5174 * of connector to encoder in simple connector/encoder devices using a
5175 * direct assignment of connector->encoder = encoder. This connection
5176 * is a logical one and the responsibility of the core, so drivers are
5177 * expected not to mess with this.
5179 * Note that the error return should've been enough here, but a large
5180 * majority of drivers ignores the return value, so add in a big WARN
5181 * to get people's attention.
5183 if (WARN_ON(connector
->encoder
))
5186 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
5187 if (connector
->encoder_ids
[i
] == 0) {
5188 connector
->encoder_ids
[i
] = encoder
->base
.id
;
5194 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
5197 * drm_mode_crtc_set_gamma_size - set the gamma table size
5198 * @crtc: CRTC to set the gamma table size for
5199 * @gamma_size: size of the gamma table
5201 * Drivers which support gamma tables should set this to the supported gamma
5202 * table size when initializing the CRTC. Currently the drm core only supports a
5203 * fixed gamma table size.
5206 * Zero on success, negative errno on failure.
5208 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
5211 uint16_t *r_base
, *g_base
, *b_base
;
5214 crtc
->gamma_size
= gamma_size
;
5216 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
5218 if (!crtc
->gamma_store
) {
5219 crtc
->gamma_size
= 0;
5223 r_base
= crtc
->gamma_store
;
5224 g_base
= r_base
+ gamma_size
;
5225 b_base
= g_base
+ gamma_size
;
5226 for (i
= 0; i
< gamma_size
; i
++) {
5235 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
5238 * drm_mode_gamma_set_ioctl - set the gamma table
5241 * @file_priv: DRM file info
5243 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5244 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5246 * Called by the user via ioctl.
5249 * Zero on success, negative errno on failure.
5251 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
5252 void *data
, struct drm_file
*file_priv
)
5254 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5255 struct drm_crtc
*crtc
;
5256 void *r_base
, *g_base
, *b_base
;
5260 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5263 drm_modeset_lock_all(dev
);
5264 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5270 if (crtc
->funcs
->gamma_set
== NULL
) {
5275 /* memcpy into gamma store */
5276 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5281 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5282 r_base
= crtc
->gamma_store
;
5283 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
5288 g_base
= r_base
+ size
;
5289 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
5294 b_base
= g_base
+ size
;
5295 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
5300 ret
= crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, crtc
->gamma_size
);
5303 drm_modeset_unlock_all(dev
);
5309 * drm_mode_gamma_get_ioctl - get the gamma table
5312 * @file_priv: DRM file info
5314 * Copy the current gamma table into the storage provided. This also provides
5315 * the gamma table size the driver expects, which can be used to size the
5316 * allocated storage.
5318 * Called by the user via ioctl.
5321 * Zero on success, negative errno on failure.
5323 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
5324 void *data
, struct drm_file
*file_priv
)
5326 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5327 struct drm_crtc
*crtc
;
5328 void *r_base
, *g_base
, *b_base
;
5332 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5335 drm_modeset_lock_all(dev
);
5336 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5342 /* memcpy into gamma store */
5343 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5348 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5349 r_base
= crtc
->gamma_store
;
5350 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
5355 g_base
= r_base
+ size
;
5356 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
5361 b_base
= g_base
+ size
;
5362 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
5367 drm_modeset_unlock_all(dev
);
5372 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5375 * @file_priv: DRM file info
5377 * This schedules an asynchronous update on a given CRTC, called page flip.
5378 * Optionally a drm event is generated to signal the completion of the event.
5379 * Generic drivers cannot assume that a pageflip with changed framebuffer
5380 * properties (including driver specific metadata like tiling layout) will work,
5381 * but some drivers support e.g. pixel format changes through the pageflip
5384 * Called by the user via ioctl.
5387 * Zero on success, negative errno on failure.
5389 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
5390 void *data
, struct drm_file
*file_priv
)
5392 struct drm_mode_crtc_page_flip
*page_flip
= data
;
5393 struct drm_crtc
*crtc
;
5394 struct drm_framebuffer
*fb
= NULL
;
5395 struct drm_pending_vblank_event
*e
= NULL
;
5398 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
5399 page_flip
->reserved
!= 0)
5402 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
5405 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
5409 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
5410 if (crtc
->primary
->fb
== NULL
) {
5411 /* The framebuffer is currently unbound, presumably
5412 * due to a hotplug event, that userspace has not
5419 if (crtc
->funcs
->page_flip
== NULL
)
5422 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
5429 const struct drm_plane_state
*state
= crtc
->primary
->state
;
5431 ret
= check_src_coords(state
->src_x
, state
->src_y
,
5432 state
->src_w
, state
->src_h
, fb
);
5434 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
5439 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
5440 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5445 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
5446 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
5451 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
5452 e
->event
.base
.length
= sizeof(e
->event
);
5453 e
->event
.user_data
= page_flip
->user_data
;
5454 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
, &e
->event
.base
);
5461 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
5462 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
5464 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
)
5465 drm_event_cancel_free(dev
, &e
->base
);
5466 /* Keep the old fb, don't unref it. */
5467 crtc
->primary
->old_fb
= NULL
;
5469 crtc
->primary
->fb
= fb
;
5470 /* Unref only the old framebuffer. */
5476 drm_framebuffer_unreference(fb
);
5477 if (crtc
->primary
->old_fb
)
5478 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
5479 crtc
->primary
->old_fb
= NULL
;
5480 drm_modeset_unlock_crtc(crtc
);
5486 * drm_mode_config_reset - call ->reset callbacks
5489 * This functions calls all the crtc's, encoder's and connector's ->reset
5490 * callback. Drivers can use this in e.g. their driver load or resume code to
5491 * reset hardware and software state.
5493 void drm_mode_config_reset(struct drm_device
*dev
)
5495 struct drm_crtc
*crtc
;
5496 struct drm_plane
*plane
;
5497 struct drm_encoder
*encoder
;
5498 struct drm_connector
*connector
;
5500 drm_for_each_plane(plane
, dev
)
5501 if (plane
->funcs
->reset
)
5502 plane
->funcs
->reset(plane
);
5504 drm_for_each_crtc(crtc
, dev
)
5505 if (crtc
->funcs
->reset
)
5506 crtc
->funcs
->reset(crtc
);
5508 drm_for_each_encoder(encoder
, dev
)
5509 if (encoder
->funcs
->reset
)
5510 encoder
->funcs
->reset(encoder
);
5512 mutex_lock(&dev
->mode_config
.mutex
);
5513 drm_for_each_connector(connector
, dev
)
5514 if (connector
->funcs
->reset
)
5515 connector
->funcs
->reset(connector
);
5516 mutex_unlock(&dev
->mode_config
.mutex
);
5518 EXPORT_SYMBOL(drm_mode_config_reset
);
5521 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5524 * @file_priv: DRM file info
5526 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5527 * TTM or something else entirely) and returns the resulting buffer handle. This
5528 * handle can then be wrapped up into a framebuffer modeset object.
5530 * Note that userspace is not allowed to use such objects for render
5531 * acceleration - drivers must create their own private ioctls for such a use
5534 * Called by the user via ioctl.
5537 * Zero on success, negative errno on failure.
5539 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
5540 void *data
, struct drm_file
*file_priv
)
5542 struct drm_mode_create_dumb
*args
= data
;
5543 u32 cpp
, stride
, size
;
5545 if (!dev
->driver
->dumb_create
)
5547 if (!args
->width
|| !args
->height
|| !args
->bpp
)
5550 /* overflow checks for 32bit size calculations */
5551 /* NOTE: DIV_ROUND_UP() can overflow */
5552 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
5553 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
5555 stride
= cpp
* args
->width
;
5556 if (args
->height
> 0xffffffffU
/ stride
)
5559 /* test for wrap-around */
5560 size
= args
->height
* stride
;
5561 if (PAGE_ALIGN(size
) == 0)
5565 * handle, pitch and size are output parameters. Zero them out to
5566 * prevent drivers from accidentally using uninitialized data. Since
5567 * not all existing userspace is clearing these fields properly we
5568 * cannot reject IOCTL with garbage in them.
5574 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
5578 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5581 * @file_priv: DRM file info
5583 * Allocate an offset in the drm device node's address space to be able to
5584 * memory map a dumb buffer.
5586 * Called by the user via ioctl.
5589 * Zero on success, negative errno on failure.
5591 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
5592 void *data
, struct drm_file
*file_priv
)
5594 struct drm_mode_map_dumb
*args
= data
;
5596 /* call driver ioctl to get mmap offset */
5597 if (!dev
->driver
->dumb_map_offset
)
5600 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
5604 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5607 * @file_priv: DRM file info
5609 * This destroys the userspace handle for the given dumb backing storage buffer.
5610 * Since buffer objects must be reference counted in the kernel a buffer object
5611 * won't be immediately freed if a framebuffer modeset object still uses it.
5613 * Called by the user via ioctl.
5616 * Zero on success, negative errno on failure.
5618 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
5619 void *data
, struct drm_file
*file_priv
)
5621 struct drm_mode_destroy_dumb
*args
= data
;
5623 if (!dev
->driver
->dumb_destroy
)
5626 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
5630 * drm_rotation_simplify() - Try to simplify the rotation
5631 * @rotation: Rotation to be simplified
5632 * @supported_rotations: Supported rotations
5634 * Attempt to simplify the rotation to a form that is supported.
5635 * Eg. if the hardware supports everything except DRM_REFLECT_X
5636 * one could call this function like this:
5638 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5639 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5640 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5642 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5643 * transforms the hardware supports, this function may not
5644 * be able to produce a supported transform, so the caller should
5645 * check the result afterwards.
5647 unsigned int drm_rotation_simplify(unsigned int rotation
,
5648 unsigned int supported_rotations
)
5650 if (rotation
& ~supported_rotations
) {
5651 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5652 rotation
= (rotation
& DRM_REFLECT_MASK
) |
5653 BIT((ffs(rotation
& DRM_ROTATE_MASK
) + 1) % 4);
5658 EXPORT_SYMBOL(drm_rotation_simplify
);
5661 * drm_mode_config_init - initialize DRM mode_configuration structure
5664 * Initialize @dev's mode_config structure, used for tracking the graphics
5665 * configuration of @dev.
5667 * Since this initializes the modeset locks, no locking is possible. Which is no
5668 * problem, since this should happen single threaded at init time. It is the
5669 * driver's problem to ensure this guarantee.
5672 void drm_mode_config_init(struct drm_device
*dev
)
5674 mutex_init(&dev
->mode_config
.mutex
);
5675 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5676 mutex_init(&dev
->mode_config
.idr_mutex
);
5677 mutex_init(&dev
->mode_config
.fb_lock
);
5678 mutex_init(&dev
->mode_config
.blob_lock
);
5679 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5680 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5681 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5682 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5683 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5684 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5685 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5686 idr_init(&dev
->mode_config
.crtc_idr
);
5687 idr_init(&dev
->mode_config
.tile_idr
);
5688 ida_init(&dev
->mode_config
.connector_ida
);
5690 drm_modeset_lock_all(dev
);
5691 drm_mode_create_standard_properties(dev
);
5692 drm_modeset_unlock_all(dev
);
5694 /* Just to be sure */
5695 dev
->mode_config
.num_fb
= 0;
5696 dev
->mode_config
.num_connector
= 0;
5697 dev
->mode_config
.num_crtc
= 0;
5698 dev
->mode_config
.num_encoder
= 0;
5699 dev
->mode_config
.num_overlay_plane
= 0;
5700 dev
->mode_config
.num_total_plane
= 0;
5702 EXPORT_SYMBOL(drm_mode_config_init
);
5705 * drm_mode_config_cleanup - free up DRM mode_config info
5708 * Free up all the connectors and CRTCs associated with this DRM device, then
5709 * free up the framebuffers and associated buffer objects.
5711 * Note that since this /should/ happen single-threaded at driver/device
5712 * teardown time, no locking is required. It's the driver's job to ensure that
5713 * this guarantee actually holds true.
5715 * FIXME: cleanup any dangling user buffer objects too
5717 void drm_mode_config_cleanup(struct drm_device
*dev
)
5719 struct drm_connector
*connector
, *ot
;
5720 struct drm_crtc
*crtc
, *ct
;
5721 struct drm_encoder
*encoder
, *enct
;
5722 struct drm_framebuffer
*fb
, *fbt
;
5723 struct drm_property
*property
, *pt
;
5724 struct drm_property_blob
*blob
, *bt
;
5725 struct drm_plane
*plane
, *plt
;
5727 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5729 encoder
->funcs
->destroy(encoder
);
5732 list_for_each_entry_safe(connector
, ot
,
5733 &dev
->mode_config
.connector_list
, head
) {
5734 connector
->funcs
->destroy(connector
);
5737 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5739 drm_property_destroy(dev
, property
);
5742 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5744 plane
->funcs
->destroy(plane
);
5747 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5748 crtc
->funcs
->destroy(crtc
);
5751 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5753 drm_property_unreference_blob(blob
);
5757 * Single-threaded teardown context, so it's not required to grab the
5758 * fb_lock to protect against concurrent fb_list access. Contrary, it
5759 * would actually deadlock with the drm_framebuffer_cleanup function.
5761 * Also, if there are any framebuffers left, that's a driver leak now,
5762 * so politely WARN about this.
5764 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5765 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5766 drm_framebuffer_free(&fb
->base
.refcount
);
5769 ida_destroy(&dev
->mode_config
.connector_ida
);
5770 idr_destroy(&dev
->mode_config
.tile_idr
);
5771 idr_destroy(&dev
->mode_config
.crtc_idr
);
5772 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5774 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5776 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5777 unsigned int supported_rotations
)
5779 static const struct drm_prop_enum_list props
[] = {
5780 { DRM_ROTATE_0
, "rotate-0" },
5781 { DRM_ROTATE_90
, "rotate-90" },
5782 { DRM_ROTATE_180
, "rotate-180" },
5783 { DRM_ROTATE_270
, "rotate-270" },
5784 { DRM_REFLECT_X
, "reflect-x" },
5785 { DRM_REFLECT_Y
, "reflect-y" },
5788 return drm_property_create_bitmask(dev
, 0, "rotation",
5789 props
, ARRAY_SIZE(props
),
5790 supported_rotations
);
5792 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
5797 * Tile groups are used to represent tiled monitors with a unique
5798 * integer identifier. Tiled monitors using DisplayID v1.3 have
5799 * a unique 8-byte handle, we store this in a tile group, so we
5800 * have a common identifier for all tiles in a monitor group.
5802 static void drm_tile_group_free(struct kref
*kref
)
5804 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
5805 struct drm_device
*dev
= tg
->dev
;
5806 mutex_lock(&dev
->mode_config
.idr_mutex
);
5807 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
5808 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5813 * drm_mode_put_tile_group - drop a reference to a tile group.
5815 * @tg: tile group to drop reference to.
5817 * drop reference to tile group and free if 0.
5819 void drm_mode_put_tile_group(struct drm_device
*dev
,
5820 struct drm_tile_group
*tg
)
5822 kref_put(&tg
->refcount
, drm_tile_group_free
);
5826 * drm_mode_get_tile_group - get a reference to an existing tile group
5828 * @topology: 8-bytes unique per monitor.
5830 * Use the unique bytes to get a reference to an existing tile group.
5833 * tile group or NULL if not found.
5835 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
5838 struct drm_tile_group
*tg
;
5840 mutex_lock(&dev
->mode_config
.idr_mutex
);
5841 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
5842 if (!memcmp(tg
->group_data
, topology
, 8)) {
5843 if (!kref_get_unless_zero(&tg
->refcount
))
5845 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5849 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5852 EXPORT_SYMBOL(drm_mode_get_tile_group
);
5855 * drm_mode_create_tile_group - create a tile group from a displayid description
5857 * @topology: 8-bytes unique per monitor.
5859 * Create a tile group for the unique monitor, and get a unique
5860 * identifier for the tile group.
5863 * new tile group or error.
5865 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
5868 struct drm_tile_group
*tg
;
5871 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
5873 return ERR_PTR(-ENOMEM
);
5875 kref_init(&tg
->refcount
);
5876 memcpy(tg
->group_data
, topology
, 8);
5879 mutex_lock(&dev
->mode_config
.idr_mutex
);
5880 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
5888 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5891 EXPORT_SYMBOL(drm_mode_create_tile_group
);
5894 * drm_crtc_enable_color_mgmt - enable color management properties
5896 * @degamma_lut_size: the size of the degamma lut (before CSC)
5897 * @has_ctm: whether to attach ctm_property for CSC matrix
5898 * @gamma_lut_size: the size of the gamma lut (after CSC)
5900 * This function lets the driver enable the color correction
5901 * properties on a CRTC. This includes 3 degamma, csc and gamma
5902 * properties that userspace can set and 2 size properties to inform
5903 * the userspace of the lut sizes. Each of the properties are
5904 * optional. The gamma and degamma properties are only attached if
5905 * their size is not 0 and ctm_property is only attached if has_ctm is
5908 void drm_crtc_enable_color_mgmt(struct drm_crtc
*crtc
,
5909 uint degamma_lut_size
,
5911 uint gamma_lut_size
)
5913 struct drm_device
*dev
= crtc
->dev
;
5914 struct drm_mode_config
*config
= &dev
->mode_config
;
5916 if (degamma_lut_size
) {
5917 drm_object_attach_property(&crtc
->base
,
5918 config
->degamma_lut_property
, 0);
5919 drm_object_attach_property(&crtc
->base
,
5920 config
->degamma_lut_size_property
,
5925 drm_object_attach_property(&crtc
->base
,
5926 config
->ctm_property
, 0);
5928 if (gamma_lut_size
) {
5929 drm_object_attach_property(&crtc
->base
,
5930 config
->gamma_lut_property
, 0);
5931 drm_object_attach_property(&crtc
->base
,
5932 config
->gamma_lut_size_property
,
5936 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt
);