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>
43 #include "drm_crtc_internal.h"
44 #include "drm_internal.h"
46 static struct drm_framebuffer
*
47 internal_framebuffer_create(struct drm_device
*dev
,
48 struct drm_mode_fb_cmd2
*r
,
49 struct drm_file
*file_priv
);
51 /* Avoid boilerplate. I'm tired of typing. */
52 #define DRM_ENUM_NAME_FN(fnname, list) \
53 const char *fnname(int val) \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
66 static const struct drm_prop_enum_list drm_dpms_enum_list
[] = {
67 { DRM_MODE_DPMS_ON
, "On" },
68 { DRM_MODE_DPMS_STANDBY
, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
70 { DRM_MODE_DPMS_OFF
, "Off" }
73 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
75 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] = {
76 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
84 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] = {
85 { DRM_MODE_SCALE_NONE
, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
87 { DRM_MODE_SCALE_CENTER
, "Center" },
88 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
91 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list
[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE
, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3
, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9
, "16:9" },
98 * Non-global properties, but "required" for certain connectors.
100 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] = {
101 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] = {
109 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
114 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
115 drm_dvi_i_subconnector_enum_list
)
117 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] = {
118 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
127 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] = {
128 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
135 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
136 drm_tv_subconnector_enum_list
)
138 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
139 { DRM_MODE_DIRTY_OFF
, "Off" },
140 { DRM_MODE_DIRTY_ON
, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
144 struct drm_conn_prop_enum_list
{
151 * Connector and encoder types.
153 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] = {
154 { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
155 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component
, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV
, "TV" },
168 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
170 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
173 static const struct drm_prop_enum_list drm_encoder_enum_list
[] = {
174 { DRM_MODE_ENCODER_NONE
, "None" },
175 { DRM_MODE_ENCODER_DAC
, "DAC" },
176 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
177 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
178 { DRM_MODE_ENCODER_TVDAC
, "TV" },
179 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
180 { DRM_MODE_ENCODER_DSI
, "DSI" },
181 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
184 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] = {
185 { SubPixelUnknown
, "Unknown" },
186 { SubPixelHorizontalRGB
, "Horizontal RGB" },
187 { SubPixelHorizontalBGR
, "Horizontal BGR" },
188 { SubPixelVerticalRGB
, "Vertical RGB" },
189 { SubPixelVerticalBGR
, "Vertical BGR" },
190 { SubPixelNone
, "None" },
193 void drm_connector_ida_init(void)
197 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
198 ida_init(&drm_connector_enum_list
[i
].ida
);
201 void drm_connector_ida_destroy(void)
205 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
206 ida_destroy(&drm_connector_enum_list
[i
].ida
);
210 * drm_get_connector_status_name - return a string for connector status
211 * @status: connector status to compute name of
213 * In contrast to the other drm_get_*_name functions this one here returns a
214 * const pointer and hence is threadsafe.
216 const char *drm_get_connector_status_name(enum drm_connector_status status
)
218 if (status
== connector_status_connected
)
220 else if (status
== connector_status_disconnected
)
221 return "disconnected";
225 EXPORT_SYMBOL(drm_get_connector_status_name
);
228 * drm_get_subpixel_order_name - return a string for a given subpixel enum
229 * @order: enum of subpixel_order
231 * Note you could abuse this and return something out of bounds, but that
232 * would be a caller error. No unscrubbed user data should make it here.
234 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
236 return drm_subpixel_enum_list
[order
].name
;
238 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
240 static char printable_char(int c
)
242 return isascii(c
) && isprint(c
) ? c
: '?';
246 * drm_get_format_name - return a string for drm fourcc format
247 * @format: format to compute name of
249 * Note that the buffer used by this function is globally shared and owned by
250 * the function itself.
252 * FIXME: This isn't really multithreading safe.
254 const char *drm_get_format_name(uint32_t format
)
258 snprintf(buf
, sizeof(buf
),
259 "%c%c%c%c %s-endian (0x%08x)",
260 printable_char(format
& 0xff),
261 printable_char((format
>> 8) & 0xff),
262 printable_char((format
>> 16) & 0xff),
263 printable_char((format
>> 24) & 0x7f),
264 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
269 EXPORT_SYMBOL(drm_get_format_name
);
272 * Internal function to assign a slot in the object idr and optionally
273 * register the object into the idr.
275 static int drm_mode_object_get_reg(struct drm_device
*dev
,
276 struct drm_mode_object
*obj
,
282 mutex_lock(&dev
->mode_config
.idr_mutex
);
283 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
286 * Set up the object linking under the protection of the idr
287 * lock so that other users can't see inconsistent state.
290 obj
->type
= obj_type
;
292 mutex_unlock(&dev
->mode_config
.idr_mutex
);
294 return ret
< 0 ? ret
: 0;
298 * drm_mode_object_get - allocate a new modeset identifier
300 * @obj: object pointer, used to generate unique ID
301 * @obj_type: object type
303 * Create a unique identifier based on @ptr in @dev's identifier space. Used
304 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
305 * modeset identifiers are _not_ reference counted. Hence don't use this for
306 * reference counted modeset objects like framebuffers.
309 * New unique (relative to other objects in @dev) integer identifier for the
312 int drm_mode_object_get(struct drm_device
*dev
,
313 struct drm_mode_object
*obj
, uint32_t obj_type
)
315 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true);
318 static void drm_mode_object_register(struct drm_device
*dev
,
319 struct drm_mode_object
*obj
)
321 mutex_lock(&dev
->mode_config
.idr_mutex
);
322 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
323 mutex_unlock(&dev
->mode_config
.idr_mutex
);
327 * drm_mode_object_put - free a modeset identifer
329 * @object: object to free
331 * Free @id from @dev's unique identifier pool. Note that despite the _get
332 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
333 * for reference counted modeset objects like framebuffers.
335 void drm_mode_object_put(struct drm_device
*dev
,
336 struct drm_mode_object
*object
)
338 mutex_lock(&dev
->mode_config
.idr_mutex
);
339 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
340 mutex_unlock(&dev
->mode_config
.idr_mutex
);
343 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
344 uint32_t id
, uint32_t type
)
346 struct drm_mode_object
*obj
= NULL
;
348 mutex_lock(&dev
->mode_config
.idr_mutex
);
349 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
350 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
352 if (obj
&& obj
->id
!= id
)
354 /* don't leak out unref'd fb's */
355 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
357 mutex_unlock(&dev
->mode_config
.idr_mutex
);
363 * drm_mode_object_find - look up a drm object with static lifetime
365 * @id: id of the mode object
366 * @type: type of the mode object
368 * Note that framebuffers cannot be looked up with this functions - since those
369 * are reference counted, they need special treatment. Even with
370 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
371 * rather than WARN_ON()).
373 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
374 uint32_t id
, uint32_t type
)
376 struct drm_mode_object
*obj
= NULL
;
378 /* Framebuffers are reference counted and need their own lookup
380 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
381 obj
= _object_find(dev
, id
, type
);
384 EXPORT_SYMBOL(drm_mode_object_find
);
387 * drm_framebuffer_init - initialize a framebuffer
389 * @fb: framebuffer to be initialized
390 * @funcs: ... with these functions
392 * Allocates an ID for the framebuffer's parent mode object, sets its mode
393 * functions & device file and adds it to the master fd list.
396 * This functions publishes the fb and makes it available for concurrent access
397 * by other users. Which means by this point the fb _must_ be fully set up -
398 * since all the fb attributes are invariant over its lifetime, no further
399 * locking but only correct reference counting is required.
402 * Zero on success, error code on failure.
404 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
405 const struct drm_framebuffer_funcs
*funcs
)
409 mutex_lock(&dev
->mode_config
.fb_lock
);
410 kref_init(&fb
->refcount
);
411 INIT_LIST_HEAD(&fb
->filp_head
);
415 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
419 dev
->mode_config
.num_fb
++;
420 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
422 mutex_unlock(&dev
->mode_config
.fb_lock
);
426 EXPORT_SYMBOL(drm_framebuffer_init
);
428 /* dev->mode_config.fb_lock must be held! */
429 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
430 struct drm_framebuffer
*fb
)
432 mutex_lock(&dev
->mode_config
.idr_mutex
);
433 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
434 mutex_unlock(&dev
->mode_config
.idr_mutex
);
439 static void drm_framebuffer_free(struct kref
*kref
)
441 struct drm_framebuffer
*fb
=
442 container_of(kref
, struct drm_framebuffer
, refcount
);
443 struct drm_device
*dev
= fb
->dev
;
446 * The lookup idr holds a weak reference, which has not necessarily been
447 * removed at this point. Check for that.
449 mutex_lock(&dev
->mode_config
.fb_lock
);
451 /* Mark fb as reaped and drop idr ref. */
452 __drm_framebuffer_unregister(dev
, fb
);
454 mutex_unlock(&dev
->mode_config
.fb_lock
);
456 fb
->funcs
->destroy(fb
);
459 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
462 struct drm_mode_object
*obj
= NULL
;
463 struct drm_framebuffer
*fb
;
465 mutex_lock(&dev
->mode_config
.idr_mutex
);
466 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
467 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
471 mutex_unlock(&dev
->mode_config
.idr_mutex
);
477 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
479 * @id: id of the fb object
481 * If successful, this grabs an additional reference to the framebuffer -
482 * callers need to make sure to eventually unreference the returned framebuffer
483 * again, using @drm_framebuffer_unreference.
485 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
488 struct drm_framebuffer
*fb
;
490 mutex_lock(&dev
->mode_config
.fb_lock
);
491 fb
= __drm_framebuffer_lookup(dev
, id
);
493 if (!kref_get_unless_zero(&fb
->refcount
))
496 mutex_unlock(&dev
->mode_config
.fb_lock
);
500 EXPORT_SYMBOL(drm_framebuffer_lookup
);
503 * drm_framebuffer_unreference - unref a framebuffer
504 * @fb: framebuffer to unref
506 * This functions decrements the fb's refcount and frees it if it drops to zero.
508 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
510 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
511 kref_put(&fb
->refcount
, drm_framebuffer_free
);
513 EXPORT_SYMBOL(drm_framebuffer_unreference
);
516 * drm_framebuffer_reference - incr the fb refcnt
519 * This functions increments the fb's refcount.
521 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
523 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
524 kref_get(&fb
->refcount
);
526 EXPORT_SYMBOL(drm_framebuffer_reference
);
528 static void drm_framebuffer_free_bug(struct kref
*kref
)
533 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
535 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
536 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
540 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
541 * @fb: fb to unregister
543 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
544 * those used for fbdev. Note that the caller must hold a reference of it's own,
545 * i.e. the object may not be destroyed through this call (since it'll lead to a
546 * locking inversion).
548 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
550 struct drm_device
*dev
= fb
->dev
;
552 mutex_lock(&dev
->mode_config
.fb_lock
);
553 /* Mark fb as reaped and drop idr ref. */
554 __drm_framebuffer_unregister(dev
, fb
);
555 mutex_unlock(&dev
->mode_config
.fb_lock
);
557 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
560 * drm_framebuffer_cleanup - remove a framebuffer object
561 * @fb: framebuffer to remove
563 * Cleanup framebuffer. This function is intended to be used from the drivers
564 * ->destroy callback. It can also be used to clean up driver private
565 * framebuffers embedded into a larger structure.
567 * Note that this function does not remove the fb from active usuage - if it is
568 * still used anywhere, hilarity can ensue since userspace could call getfb on
569 * the id and get back -EINVAL. Obviously no concern at driver unload time.
571 * Also, the framebuffer will not be removed from the lookup idr - for
572 * user-created framebuffers this will happen in in the rmfb ioctl. For
573 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
574 * drm_framebuffer_unregister_private.
576 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
578 struct drm_device
*dev
= fb
->dev
;
580 mutex_lock(&dev
->mode_config
.fb_lock
);
582 dev
->mode_config
.num_fb
--;
583 mutex_unlock(&dev
->mode_config
.fb_lock
);
585 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
588 * drm_framebuffer_remove - remove and unreference a framebuffer object
589 * @fb: framebuffer to remove
591 * Scans all the CRTCs and planes in @dev's mode_config. If they're
592 * using @fb, removes it, setting it to NULL. Then drops the reference to the
593 * passed-in framebuffer. Might take the modeset locks.
595 * Note that this function optimizes the cleanup away if the caller holds the
596 * last reference to the framebuffer. It is also guaranteed to not take the
597 * modeset locks in this case.
599 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
601 struct drm_device
*dev
= fb
->dev
;
602 struct drm_crtc
*crtc
;
603 struct drm_plane
*plane
;
604 struct drm_mode_set set
;
607 WARN_ON(!list_empty(&fb
->filp_head
));
610 * drm ABI mandates that we remove any deleted framebuffers from active
611 * useage. But since most sane clients only remove framebuffers they no
612 * longer need, try to optimize this away.
614 * Since we're holding a reference ourselves, observing a refcount of 1
615 * means that we're the last holder and can skip it. Also, the refcount
616 * can never increase from 1 again, so we don't need any barriers or
619 * Note that userspace could try to race with use and instate a new
620 * usage _after_ we've cleared all current ones. End result will be an
621 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
624 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
625 drm_modeset_lock_all(dev
);
626 /* remove from any CRTC */
627 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
628 if (crtc
->primary
->fb
== fb
) {
629 /* should turn off the crtc */
630 memset(&set
, 0, sizeof(struct drm_mode_set
));
633 ret
= drm_mode_set_config_internal(&set
);
635 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
639 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
641 drm_plane_force_disable(plane
);
643 drm_modeset_unlock_all(dev
);
646 drm_framebuffer_unreference(fb
);
648 EXPORT_SYMBOL(drm_framebuffer_remove
);
650 DEFINE_WW_CLASS(crtc_ww_class
);
653 * drm_crtc_init_with_planes - Initialise a new CRTC object with
654 * specified primary and cursor planes.
656 * @crtc: CRTC object to init
657 * @primary: Primary plane for CRTC
658 * @cursor: Cursor plane for CRTC
659 * @funcs: callbacks for the new CRTC
661 * Inits a new object created as base part of a driver crtc object.
664 * Zero on success, error code on failure.
666 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
667 struct drm_plane
*primary
,
668 struct drm_plane
*cursor
,
669 const struct drm_crtc_funcs
*funcs
)
671 struct drm_mode_config
*config
= &dev
->mode_config
;
676 crtc
->invert_dimensions
= false;
678 drm_modeset_lock_init(&crtc
->mutex
);
679 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
683 crtc
->base
.properties
= &crtc
->properties
;
685 list_add_tail(&crtc
->head
, &config
->crtc_list
);
688 crtc
->primary
= primary
;
689 crtc
->cursor
= cursor
;
691 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
693 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
695 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
696 drm_object_attach_property(&crtc
->base
, config
->prop_active
, 0);
701 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
704 * drm_crtc_cleanup - Clean up the core crtc usage
705 * @crtc: CRTC to cleanup
707 * This function cleans up @crtc and removes it from the DRM mode setting
708 * core. Note that the function does *not* free the crtc structure itself,
709 * this is the responsibility of the caller.
711 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
713 struct drm_device
*dev
= crtc
->dev
;
715 kfree(crtc
->gamma_store
);
716 crtc
->gamma_store
= NULL
;
718 drm_modeset_lock_fini(&crtc
->mutex
);
720 drm_mode_object_put(dev
, &crtc
->base
);
721 list_del(&crtc
->head
);
722 dev
->mode_config
.num_crtc
--;
724 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
725 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
726 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
728 memset(crtc
, 0, sizeof(*crtc
));
730 EXPORT_SYMBOL(drm_crtc_cleanup
);
733 * drm_crtc_index - find the index of a registered CRTC
734 * @crtc: CRTC to find index for
736 * Given a registered CRTC, return the index of that CRTC within a DRM
737 * device's list of CRTCs.
739 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
741 unsigned int index
= 0;
742 struct drm_crtc
*tmp
;
744 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
753 EXPORT_SYMBOL(drm_crtc_index
);
756 * drm_mode_remove - remove and free a mode
757 * @connector: connector list to modify
758 * @mode: mode to remove
760 * Remove @mode from @connector's mode list, then free it.
762 static void drm_mode_remove(struct drm_connector
*connector
,
763 struct drm_display_mode
*mode
)
765 list_del(&mode
->head
);
766 drm_mode_destroy(connector
->dev
, mode
);
770 * drm_display_info_set_bus_formats - set the supported bus formats
771 * @info: display info to store bus formats in
772 * @formats: array containing the supported bus formats
773 * @num_formats: the number of entries in the fmts array
775 * Store the supported bus formats in display info structure.
776 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
777 * a full list of available formats.
779 int drm_display_info_set_bus_formats(struct drm_display_info
*info
,
781 unsigned int num_formats
)
785 if (!formats
&& num_formats
)
788 if (formats
&& num_formats
) {
789 fmts
= kmemdup(formats
, sizeof(*formats
) * num_formats
,
795 kfree(info
->bus_formats
);
796 info
->bus_formats
= fmts
;
797 info
->num_bus_formats
= num_formats
;
801 EXPORT_SYMBOL(drm_display_info_set_bus_formats
);
804 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
805 * @connector: connector to quwery
807 * The kernel supports per-connector configration of its consoles through
808 * use of the video= parameter. This function parses that option and
809 * extracts the user's specified mode (or enable/disable status) for a
810 * particular connector. This is typically only used during the early fbdev
813 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
815 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
818 if (fb_get_options(connector
->name
, &option
))
821 if (!drm_mode_parse_command_line_for_connector(option
,
829 switch (mode
->force
) {
833 case DRM_FORCE_ON_DIGITAL
:
842 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
843 connector
->force
= mode
->force
;
846 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
848 mode
->xres
, mode
->yres
,
849 mode
->refresh_specified
? mode
->refresh
: 60,
850 mode
->rb
? " reduced blanking" : "",
851 mode
->margins
? " with margins" : "",
852 mode
->interlace
? " interlaced" : "");
856 * drm_connector_init - Init a preallocated connector
858 * @connector: the connector to init
859 * @funcs: callbacks for this connector
860 * @connector_type: user visible type of the connector
862 * Initialises a preallocated connector. Connectors should be
863 * subclassed as part of driver connector objects.
866 * Zero on success, error code on failure.
868 int drm_connector_init(struct drm_device
*dev
,
869 struct drm_connector
*connector
,
870 const struct drm_connector_funcs
*funcs
,
873 struct drm_mode_config
*config
= &dev
->mode_config
;
875 struct ida
*connector_ida
=
876 &drm_connector_enum_list
[connector_type
].ida
;
878 drm_modeset_lock_all(dev
);
880 ret
= drm_mode_object_get_reg(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
, false);
884 connector
->base
.properties
= &connector
->properties
;
885 connector
->dev
= dev
;
886 connector
->funcs
= funcs
;
887 connector
->connector_type
= connector_type
;
888 connector
->connector_type_id
=
889 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
890 if (connector
->connector_type_id
< 0) {
891 ret
= connector
->connector_type_id
;
895 kasprintf(GFP_KERNEL
, "%s-%d",
896 drm_connector_enum_list
[connector_type
].name
,
897 connector
->connector_type_id
);
898 if (!connector
->name
) {
903 INIT_LIST_HEAD(&connector
->probed_modes
);
904 INIT_LIST_HEAD(&connector
->modes
);
905 connector
->edid_blob_ptr
= NULL
;
906 connector
->status
= connector_status_unknown
;
908 drm_connector_get_cmdline_mode(connector
);
910 /* We should add connectors at the end to avoid upsetting the connector
912 list_add_tail(&connector
->head
, &config
->connector_list
);
913 config
->num_connector
++;
915 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
916 drm_object_attach_property(&connector
->base
,
917 config
->edid_property
,
920 drm_object_attach_property(&connector
->base
,
921 config
->dpms_property
, 0);
923 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
924 drm_object_attach_property(&connector
->base
, config
->prop_crtc_id
, 0);
927 connector
->debugfs_entry
= NULL
;
931 drm_mode_object_put(dev
, &connector
->base
);
934 drm_modeset_unlock_all(dev
);
938 EXPORT_SYMBOL(drm_connector_init
);
941 * drm_connector_cleanup - cleans up an initialised connector
942 * @connector: connector to cleanup
944 * Cleans up the connector but doesn't free the object.
946 void drm_connector_cleanup(struct drm_connector
*connector
)
948 struct drm_device
*dev
= connector
->dev
;
949 struct drm_display_mode
*mode
, *t
;
951 if (connector
->tile_group
) {
952 drm_mode_put_tile_group(dev
, connector
->tile_group
);
953 connector
->tile_group
= NULL
;
956 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
957 drm_mode_remove(connector
, mode
);
959 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
960 drm_mode_remove(connector
, mode
);
962 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
963 connector
->connector_type_id
);
965 kfree(connector
->display_info
.bus_formats
);
966 drm_mode_object_put(dev
, &connector
->base
);
967 kfree(connector
->name
);
968 connector
->name
= NULL
;
969 list_del(&connector
->head
);
970 dev
->mode_config
.num_connector
--;
972 WARN_ON(connector
->state
&& !connector
->funcs
->atomic_destroy_state
);
973 if (connector
->state
&& connector
->funcs
->atomic_destroy_state
)
974 connector
->funcs
->atomic_destroy_state(connector
,
977 memset(connector
, 0, sizeof(*connector
));
979 EXPORT_SYMBOL(drm_connector_cleanup
);
982 * drm_connector_index - find the index of a registered connector
983 * @connector: connector to find index for
985 * Given a registered connector, return the index of that connector within a DRM
986 * device's list of connectors.
988 unsigned int drm_connector_index(struct drm_connector
*connector
)
990 unsigned int index
= 0;
991 struct drm_connector
*tmp
;
992 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
994 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
996 list_for_each_entry(tmp
, &connector
->dev
->mode_config
.connector_list
, head
) {
997 if (tmp
== connector
)
1005 EXPORT_SYMBOL(drm_connector_index
);
1008 * drm_connector_register - register a connector
1009 * @connector: the connector to register
1011 * Register userspace interfaces for a connector
1014 * Zero on success, error code on failure.
1016 int drm_connector_register(struct drm_connector
*connector
)
1020 drm_mode_object_register(connector
->dev
, &connector
->base
);
1022 ret
= drm_sysfs_connector_add(connector
);
1026 ret
= drm_debugfs_connector_add(connector
);
1028 drm_sysfs_connector_remove(connector
);
1034 EXPORT_SYMBOL(drm_connector_register
);
1037 * drm_connector_unregister - unregister a connector
1038 * @connector: the connector to unregister
1040 * Unregister userspace interfaces for a connector
1042 void drm_connector_unregister(struct drm_connector
*connector
)
1044 drm_sysfs_connector_remove(connector
);
1045 drm_debugfs_connector_remove(connector
);
1047 EXPORT_SYMBOL(drm_connector_unregister
);
1051 * drm_connector_unplug_all - unregister connector userspace interfaces
1054 * This function unregisters all connector userspace interfaces in sysfs. Should
1055 * be call when the device is disconnected, e.g. from an usb driver's
1056 * ->disconnect callback.
1058 void drm_connector_unplug_all(struct drm_device
*dev
)
1060 struct drm_connector
*connector
;
1062 /* taking the mode config mutex ends up in a clash with sysfs */
1063 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1064 drm_connector_unregister(connector
);
1067 EXPORT_SYMBOL(drm_connector_unplug_all
);
1070 * drm_encoder_init - Init a preallocated encoder
1072 * @encoder: the encoder to init
1073 * @funcs: callbacks for this encoder
1074 * @encoder_type: user visible type of the encoder
1076 * Initialises a preallocated encoder. Encoder should be
1077 * subclassed as part of driver encoder objects.
1080 * Zero on success, error code on failure.
1082 int drm_encoder_init(struct drm_device
*dev
,
1083 struct drm_encoder
*encoder
,
1084 const struct drm_encoder_funcs
*funcs
,
1089 drm_modeset_lock_all(dev
);
1091 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1096 encoder
->encoder_type
= encoder_type
;
1097 encoder
->funcs
= funcs
;
1098 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1099 drm_encoder_enum_list
[encoder_type
].name
,
1101 if (!encoder
->name
) {
1106 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1107 dev
->mode_config
.num_encoder
++;
1111 drm_mode_object_put(dev
, &encoder
->base
);
1114 drm_modeset_unlock_all(dev
);
1118 EXPORT_SYMBOL(drm_encoder_init
);
1121 * drm_encoder_cleanup - cleans up an initialised encoder
1122 * @encoder: encoder to cleanup
1124 * Cleans up the encoder but doesn't free the object.
1126 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1128 struct drm_device
*dev
= encoder
->dev
;
1130 drm_modeset_lock_all(dev
);
1131 drm_mode_object_put(dev
, &encoder
->base
);
1132 kfree(encoder
->name
);
1133 list_del(&encoder
->head
);
1134 dev
->mode_config
.num_encoder
--;
1135 drm_modeset_unlock_all(dev
);
1137 memset(encoder
, 0, sizeof(*encoder
));
1139 EXPORT_SYMBOL(drm_encoder_cleanup
);
1142 * drm_universal_plane_init - Initialize a new universal plane object
1144 * @plane: plane object to init
1145 * @possible_crtcs: bitmask of possible CRTCs
1146 * @funcs: callbacks for the new plane
1147 * @formats: array of supported formats (%DRM_FORMAT_*)
1148 * @format_count: number of elements in @formats
1149 * @type: type of plane (overlay, primary, cursor)
1151 * Initializes a plane object of type @type.
1154 * Zero on success, error code on failure.
1156 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1157 unsigned long possible_crtcs
,
1158 const struct drm_plane_funcs
*funcs
,
1159 const uint32_t *formats
, uint32_t format_count
,
1160 enum drm_plane_type type
)
1162 struct drm_mode_config
*config
= &dev
->mode_config
;
1165 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1169 drm_modeset_lock_init(&plane
->mutex
);
1171 plane
->base
.properties
= &plane
->properties
;
1173 plane
->funcs
= funcs
;
1174 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
1176 if (!plane
->format_types
) {
1177 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1178 drm_mode_object_put(dev
, &plane
->base
);
1182 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1183 plane
->format_count
= format_count
;
1184 plane
->possible_crtcs
= possible_crtcs
;
1187 list_add_tail(&plane
->head
, &config
->plane_list
);
1188 config
->num_total_plane
++;
1189 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1190 config
->num_overlay_plane
++;
1192 drm_object_attach_property(&plane
->base
,
1193 config
->plane_type_property
,
1196 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
1197 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
1198 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
1199 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
1200 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
1201 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
1202 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
1203 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
1204 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
1205 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
1206 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
1211 EXPORT_SYMBOL(drm_universal_plane_init
);
1214 * drm_plane_init - Initialize a legacy plane
1216 * @plane: plane object to init
1217 * @possible_crtcs: bitmask of possible CRTCs
1218 * @funcs: callbacks for the new plane
1219 * @formats: array of supported formats (%DRM_FORMAT_*)
1220 * @format_count: number of elements in @formats
1221 * @is_primary: plane type (primary vs overlay)
1223 * Legacy API to initialize a DRM plane.
1225 * New drivers should call drm_universal_plane_init() instead.
1228 * Zero on success, error code on failure.
1230 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1231 unsigned long possible_crtcs
,
1232 const struct drm_plane_funcs
*funcs
,
1233 const uint32_t *formats
, uint32_t format_count
,
1236 enum drm_plane_type type
;
1238 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1239 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1240 formats
, format_count
, type
);
1242 EXPORT_SYMBOL(drm_plane_init
);
1245 * drm_plane_cleanup - Clean up the core plane usage
1246 * @plane: plane to cleanup
1248 * This function cleans up @plane and removes it from the DRM mode setting
1249 * core. Note that the function does *not* free the plane structure itself,
1250 * this is the responsibility of the caller.
1252 void drm_plane_cleanup(struct drm_plane
*plane
)
1254 struct drm_device
*dev
= plane
->dev
;
1256 drm_modeset_lock_all(dev
);
1257 kfree(plane
->format_types
);
1258 drm_mode_object_put(dev
, &plane
->base
);
1260 BUG_ON(list_empty(&plane
->head
));
1262 list_del(&plane
->head
);
1263 dev
->mode_config
.num_total_plane
--;
1264 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1265 dev
->mode_config
.num_overlay_plane
--;
1266 drm_modeset_unlock_all(dev
);
1268 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
1269 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
1270 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1272 memset(plane
, 0, sizeof(*plane
));
1274 EXPORT_SYMBOL(drm_plane_cleanup
);
1277 * drm_plane_index - find the index of a registered plane
1278 * @plane: plane to find index for
1280 * Given a registered plane, return the index of that CRTC within a DRM
1281 * device's list of planes.
1283 unsigned int drm_plane_index(struct drm_plane
*plane
)
1285 unsigned int index
= 0;
1286 struct drm_plane
*tmp
;
1288 list_for_each_entry(tmp
, &plane
->dev
->mode_config
.plane_list
, head
) {
1297 EXPORT_SYMBOL(drm_plane_index
);
1300 * drm_plane_force_disable - Forcibly disable a plane
1301 * @plane: plane to disable
1303 * Forces the plane to be disabled.
1305 * Used when the plane's current framebuffer is destroyed,
1306 * and when restoring fbdev mode.
1308 void drm_plane_force_disable(struct drm_plane
*plane
)
1315 plane
->old_fb
= plane
->fb
;
1316 ret
= plane
->funcs
->disable_plane(plane
);
1318 DRM_ERROR("failed to disable plane with busy fb\n");
1319 plane
->old_fb
= NULL
;
1322 /* disconnect the plane from the fb and crtc: */
1323 __drm_framebuffer_unreference(plane
->old_fb
);
1324 plane
->old_fb
= NULL
;
1328 EXPORT_SYMBOL(drm_plane_force_disable
);
1330 static int drm_mode_create_standard_properties(struct drm_device
*dev
)
1332 struct drm_property
*prop
;
1335 * Standard properties (apply to all connectors)
1337 prop
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1338 DRM_MODE_PROP_IMMUTABLE
,
1342 dev
->mode_config
.edid_property
= prop
;
1344 prop
= drm_property_create_enum(dev
, 0,
1345 "DPMS", drm_dpms_enum_list
,
1346 ARRAY_SIZE(drm_dpms_enum_list
));
1349 dev
->mode_config
.dpms_property
= prop
;
1351 prop
= drm_property_create(dev
,
1352 DRM_MODE_PROP_BLOB
|
1353 DRM_MODE_PROP_IMMUTABLE
,
1357 dev
->mode_config
.path_property
= prop
;
1359 prop
= drm_property_create(dev
,
1360 DRM_MODE_PROP_BLOB
|
1361 DRM_MODE_PROP_IMMUTABLE
,
1365 dev
->mode_config
.tile_property
= prop
;
1367 prop
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1368 "type", drm_plane_type_enum_list
,
1369 ARRAY_SIZE(drm_plane_type_enum_list
));
1372 dev
->mode_config
.plane_type_property
= prop
;
1374 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1375 "SRC_X", 0, UINT_MAX
);
1378 dev
->mode_config
.prop_src_x
= prop
;
1380 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1381 "SRC_Y", 0, UINT_MAX
);
1384 dev
->mode_config
.prop_src_y
= prop
;
1386 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1387 "SRC_W", 0, UINT_MAX
);
1390 dev
->mode_config
.prop_src_w
= prop
;
1392 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1393 "SRC_H", 0, UINT_MAX
);
1396 dev
->mode_config
.prop_src_h
= prop
;
1398 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1399 "CRTC_X", INT_MIN
, INT_MAX
);
1402 dev
->mode_config
.prop_crtc_x
= prop
;
1404 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1405 "CRTC_Y", INT_MIN
, INT_MAX
);
1408 dev
->mode_config
.prop_crtc_y
= prop
;
1410 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1411 "CRTC_W", 0, INT_MAX
);
1414 dev
->mode_config
.prop_crtc_w
= prop
;
1416 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1417 "CRTC_H", 0, INT_MAX
);
1420 dev
->mode_config
.prop_crtc_h
= prop
;
1422 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1423 "FB_ID", DRM_MODE_OBJECT_FB
);
1426 dev
->mode_config
.prop_fb_id
= prop
;
1428 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1429 "CRTC_ID", DRM_MODE_OBJECT_CRTC
);
1432 dev
->mode_config
.prop_crtc_id
= prop
;
1434 prop
= drm_property_create_bool(dev
, DRM_MODE_PROP_ATOMIC
,
1438 dev
->mode_config
.prop_active
= prop
;
1444 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1447 * Called by a driver the first time a DVI-I connector is made.
1449 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1451 struct drm_property
*dvi_i_selector
;
1452 struct drm_property
*dvi_i_subconnector
;
1454 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1458 drm_property_create_enum(dev
, 0,
1459 "select subconnector",
1460 drm_dvi_i_select_enum_list
,
1461 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1462 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1464 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1466 drm_dvi_i_subconnector_enum_list
,
1467 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1468 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1472 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1475 * drm_create_tv_properties - create TV specific connector properties
1477 * @num_modes: number of different TV formats (modes) supported
1478 * @modes: array of pointers to strings containing name of each format
1480 * Called by a driver's TV initialization routine, this function creates
1481 * the TV specific connector properties for a given device. Caller is
1482 * responsible for allocating a list of format names and passing them to
1485 int drm_mode_create_tv_properties(struct drm_device
*dev
,
1486 unsigned int num_modes
,
1489 struct drm_property
*tv_selector
;
1490 struct drm_property
*tv_subconnector
;
1493 if (dev
->mode_config
.tv_select_subconnector_property
)
1497 * Basic connector properties
1499 tv_selector
= drm_property_create_enum(dev
, 0,
1500 "select subconnector",
1501 drm_tv_select_enum_list
,
1502 ARRAY_SIZE(drm_tv_select_enum_list
));
1503 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1506 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1508 drm_tv_subconnector_enum_list
,
1509 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1510 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1513 * Other, TV specific properties: margins & TV modes.
1515 dev
->mode_config
.tv_left_margin_property
=
1516 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1518 dev
->mode_config
.tv_right_margin_property
=
1519 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1521 dev
->mode_config
.tv_top_margin_property
=
1522 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1524 dev
->mode_config
.tv_bottom_margin_property
=
1525 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1527 dev
->mode_config
.tv_mode_property
=
1528 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1530 for (i
= 0; i
< num_modes
; i
++)
1531 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1534 dev
->mode_config
.tv_brightness_property
=
1535 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1537 dev
->mode_config
.tv_contrast_property
=
1538 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1540 dev
->mode_config
.tv_flicker_reduction_property
=
1541 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1543 dev
->mode_config
.tv_overscan_property
=
1544 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1546 dev
->mode_config
.tv_saturation_property
=
1547 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1549 dev
->mode_config
.tv_hue_property
=
1550 drm_property_create_range(dev
, 0, "hue", 0, 100);
1554 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1557 * drm_mode_create_scaling_mode_property - create scaling mode property
1560 * Called by a driver the first time it's needed, must be attached to desired
1563 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1565 struct drm_property
*scaling_mode
;
1567 if (dev
->mode_config
.scaling_mode_property
)
1571 drm_property_create_enum(dev
, 0, "scaling mode",
1572 drm_scaling_mode_enum_list
,
1573 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1575 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1579 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1582 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1585 * Called by a driver the first time it's needed, must be attached to desired
1589 * Zero on success, negative errno on failure.
1591 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1593 if (dev
->mode_config
.aspect_ratio_property
)
1596 dev
->mode_config
.aspect_ratio_property
=
1597 drm_property_create_enum(dev
, 0, "aspect ratio",
1598 drm_aspect_ratio_enum_list
,
1599 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1601 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1606 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1609 * drm_mode_create_dirty_property - create dirty property
1612 * Called by a driver the first time it's needed, must be attached to desired
1615 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1617 struct drm_property
*dirty_info
;
1619 if (dev
->mode_config
.dirty_info_property
)
1623 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1625 drm_dirty_info_enum_list
,
1626 ARRAY_SIZE(drm_dirty_info_enum_list
));
1627 dev
->mode_config
.dirty_info_property
= dirty_info
;
1631 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1634 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1637 * Create the the suggested x/y offset property for connectors.
1639 int drm_mode_create_suggested_offset_properties(struct drm_device
*dev
)
1641 if (dev
->mode_config
.suggested_x_property
&& dev
->mode_config
.suggested_y_property
)
1644 dev
->mode_config
.suggested_x_property
=
1645 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested X", 0, 0xffffffff);
1647 dev
->mode_config
.suggested_y_property
=
1648 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested Y", 0, 0xffffffff);
1650 if (dev
->mode_config
.suggested_x_property
== NULL
||
1651 dev
->mode_config
.suggested_y_property
== NULL
)
1655 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties
);
1657 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1659 uint32_t total_objects
= 0;
1661 total_objects
+= dev
->mode_config
.num_crtc
;
1662 total_objects
+= dev
->mode_config
.num_connector
;
1663 total_objects
+= dev
->mode_config
.num_encoder
;
1665 group
->id_list
= kcalloc(total_objects
, sizeof(uint32_t), GFP_KERNEL
);
1666 if (!group
->id_list
)
1669 group
->num_crtcs
= 0;
1670 group
->num_connectors
= 0;
1671 group
->num_encoders
= 0;
1675 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1677 kfree(group
->id_list
);
1678 group
->id_list
= NULL
;
1682 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1683 * the drm core's responsibility to set up mode control groups.
1685 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1686 struct drm_mode_group
*group
)
1688 struct drm_crtc
*crtc
;
1689 struct drm_encoder
*encoder
;
1690 struct drm_connector
*connector
;
1693 ret
= drm_mode_group_init(dev
, group
);
1697 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1698 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1700 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1701 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1704 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1705 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1706 group
->num_connectors
++] = connector
->base
.id
;
1710 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1712 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1714 drm_modeset_lock_all(dev
);
1715 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1716 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1717 drm_modeset_unlock_all(dev
);
1719 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1722 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1723 * @out: drm_mode_modeinfo struct to return to the user
1724 * @in: drm_display_mode to use
1726 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1729 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1730 const struct drm_display_mode
*in
)
1732 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1733 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1734 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1735 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1736 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1737 "timing values too large for mode info\n");
1739 out
->clock
= in
->clock
;
1740 out
->hdisplay
= in
->hdisplay
;
1741 out
->hsync_start
= in
->hsync_start
;
1742 out
->hsync_end
= in
->hsync_end
;
1743 out
->htotal
= in
->htotal
;
1744 out
->hskew
= in
->hskew
;
1745 out
->vdisplay
= in
->vdisplay
;
1746 out
->vsync_start
= in
->vsync_start
;
1747 out
->vsync_end
= in
->vsync_end
;
1748 out
->vtotal
= in
->vtotal
;
1749 out
->vscan
= in
->vscan
;
1750 out
->vrefresh
= in
->vrefresh
;
1751 out
->flags
= in
->flags
;
1752 out
->type
= in
->type
;
1753 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1754 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1758 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1759 * @out: drm_display_mode to return to the user
1760 * @in: drm_mode_modeinfo to use
1762 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1766 * Zero on success, negative errno on failure.
1768 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1769 const struct drm_mode_modeinfo
*in
)
1771 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1774 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1777 out
->clock
= in
->clock
;
1778 out
->hdisplay
= in
->hdisplay
;
1779 out
->hsync_start
= in
->hsync_start
;
1780 out
->hsync_end
= in
->hsync_end
;
1781 out
->htotal
= in
->htotal
;
1782 out
->hskew
= in
->hskew
;
1783 out
->vdisplay
= in
->vdisplay
;
1784 out
->vsync_start
= in
->vsync_start
;
1785 out
->vsync_end
= in
->vsync_end
;
1786 out
->vtotal
= in
->vtotal
;
1787 out
->vscan
= in
->vscan
;
1788 out
->vrefresh
= in
->vrefresh
;
1789 out
->flags
= in
->flags
;
1790 out
->type
= in
->type
;
1791 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1792 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1798 * drm_mode_getresources - get graphics configuration
1799 * @dev: drm device for the ioctl
1800 * @data: data pointer for the ioctl
1801 * @file_priv: drm file for the ioctl call
1803 * Construct a set of configuration description structures and return
1804 * them to the user, including CRTC, connector and framebuffer configuration.
1806 * Called by the user via ioctl.
1809 * Zero on success, negative errno on failure.
1811 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1812 struct drm_file
*file_priv
)
1814 struct drm_mode_card_res
*card_res
= data
;
1815 struct list_head
*lh
;
1816 struct drm_framebuffer
*fb
;
1817 struct drm_connector
*connector
;
1818 struct drm_crtc
*crtc
;
1819 struct drm_encoder
*encoder
;
1821 int connector_count
= 0;
1824 int encoder_count
= 0;
1826 uint32_t __user
*fb_id
;
1827 uint32_t __user
*crtc_id
;
1828 uint32_t __user
*connector_id
;
1829 uint32_t __user
*encoder_id
;
1830 struct drm_mode_group
*mode_group
;
1832 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1836 mutex_lock(&file_priv
->fbs_lock
);
1838 * For the non-control nodes we need to limit the list of resources
1839 * by IDs in the group list for this node
1841 list_for_each(lh
, &file_priv
->fbs
)
1844 /* handle this in 4 parts */
1846 if (card_res
->count_fbs
>= fb_count
) {
1848 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1849 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1850 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1851 mutex_unlock(&file_priv
->fbs_lock
);
1857 card_res
->count_fbs
= fb_count
;
1858 mutex_unlock(&file_priv
->fbs_lock
);
1860 /* mode_config.mutex protects the connector list against e.g. DP MST
1861 * connector hot-adding. CRTC/Plane lists are invariant. */
1862 mutex_lock(&dev
->mode_config
.mutex
);
1863 if (!drm_is_primary_client(file_priv
)) {
1866 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1869 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1872 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1876 mode_group
= &file_priv
->master
->minor
->mode_group
;
1877 crtc_count
= mode_group
->num_crtcs
;
1878 connector_count
= mode_group
->num_connectors
;
1879 encoder_count
= mode_group
->num_encoders
;
1882 card_res
->max_height
= dev
->mode_config
.max_height
;
1883 card_res
->min_height
= dev
->mode_config
.min_height
;
1884 card_res
->max_width
= dev
->mode_config
.max_width
;
1885 card_res
->min_width
= dev
->mode_config
.min_width
;
1888 if (card_res
->count_crtcs
>= crtc_count
) {
1890 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1892 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1894 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1895 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1902 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1903 if (put_user(mode_group
->id_list
[i
],
1904 crtc_id
+ copied
)) {
1912 card_res
->count_crtcs
= crtc_count
;
1915 if (card_res
->count_encoders
>= encoder_count
) {
1917 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1919 list_for_each_entry(encoder
,
1920 &dev
->mode_config
.encoder_list
,
1922 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1924 if (put_user(encoder
->base
.id
, encoder_id
+
1932 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1933 if (put_user(mode_group
->id_list
[i
],
1934 encoder_id
+ copied
)) {
1943 card_res
->count_encoders
= encoder_count
;
1946 if (card_res
->count_connectors
>= connector_count
) {
1948 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1950 list_for_each_entry(connector
,
1951 &dev
->mode_config
.connector_list
,
1953 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1956 if (put_user(connector
->base
.id
,
1957 connector_id
+ copied
)) {
1964 int start
= mode_group
->num_crtcs
+
1965 mode_group
->num_encoders
;
1966 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1967 if (put_user(mode_group
->id_list
[i
],
1968 connector_id
+ copied
)) {
1976 card_res
->count_connectors
= connector_count
;
1978 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1979 card_res
->count_connectors
, card_res
->count_encoders
);
1982 mutex_unlock(&dev
->mode_config
.mutex
);
1987 * drm_mode_getcrtc - get CRTC configuration
1988 * @dev: drm device for the ioctl
1989 * @data: data pointer for the ioctl
1990 * @file_priv: drm file for the ioctl call
1992 * Construct a CRTC configuration structure to return to the user.
1994 * Called by the user via ioctl.
1997 * Zero on success, negative errno on failure.
1999 int drm_mode_getcrtc(struct drm_device
*dev
,
2000 void *data
, struct drm_file
*file_priv
)
2002 struct drm_mode_crtc
*crtc_resp
= data
;
2003 struct drm_crtc
*crtc
;
2005 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2008 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
2012 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
2013 crtc_resp
->x
= crtc
->x
;
2014 crtc_resp
->y
= crtc
->y
;
2015 crtc_resp
->gamma_size
= crtc
->gamma_size
;
2016 if (crtc
->primary
->fb
)
2017 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
2019 crtc_resp
->fb_id
= 0;
2021 if (crtc
->enabled
) {
2023 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
2024 crtc_resp
->mode_valid
= 1;
2027 crtc_resp
->mode_valid
= 0;
2029 drm_modeset_unlock_crtc(crtc
);
2034 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
2035 const struct drm_file
*file_priv
)
2038 * If user-space hasn't configured the driver to expose the stereo 3D
2039 * modes, don't expose them.
2041 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
2047 static struct drm_encoder
*drm_connector_get_encoder(struct drm_connector
*connector
)
2049 /* For atomic drivers only state objects are synchronously updated and
2050 * protected by modeset locks, so check those first. */
2051 if (connector
->state
)
2052 return connector
->state
->best_encoder
;
2053 return connector
->encoder
;
2056 /* helper for getconnector and getproperties ioctls */
2057 static int get_properties(struct drm_mode_object
*obj
, bool atomic
,
2058 uint32_t __user
*prop_ptr
, uint64_t __user
*prop_values
,
2059 uint32_t *arg_count_props
)
2064 props_count
= obj
->properties
->count
;
2066 props_count
-= obj
->properties
->atomic_count
;
2068 if ((*arg_count_props
>= props_count
) && props_count
) {
2069 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
2070 struct drm_property
*prop
= obj
->properties
->properties
[i
];
2073 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
2076 ret
= drm_object_property_get_value(obj
, prop
, &val
);
2080 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
2083 if (put_user(val
, prop_values
+ copied
))
2089 *arg_count_props
= props_count
;
2095 * drm_mode_getconnector - get connector configuration
2096 * @dev: drm device for the ioctl
2097 * @data: data pointer for the ioctl
2098 * @file_priv: drm file for the ioctl call
2100 * Construct a connector configuration structure to return to the user.
2102 * Called by the user via ioctl.
2105 * Zero on success, negative errno on failure.
2107 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
2108 struct drm_file
*file_priv
)
2110 struct drm_mode_get_connector
*out_resp
= data
;
2111 struct drm_connector
*connector
;
2112 struct drm_encoder
*encoder
;
2113 struct drm_display_mode
*mode
;
2115 int encoders_count
= 0;
2119 struct drm_mode_modeinfo u_mode
;
2120 struct drm_mode_modeinfo __user
*mode_ptr
;
2121 uint32_t __user
*encoder_ptr
;
2123 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2126 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
2128 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
2130 mutex_lock(&dev
->mode_config
.mutex
);
2132 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
2138 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++)
2139 if (connector
->encoder_ids
[i
] != 0)
2142 if (out_resp
->count_modes
== 0) {
2143 connector
->funcs
->fill_modes(connector
,
2144 dev
->mode_config
.max_width
,
2145 dev
->mode_config
.max_height
);
2148 /* delayed so we get modes regardless of pre-fill_modes state */
2149 list_for_each_entry(mode
, &connector
->modes
, head
)
2150 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2153 out_resp
->connector_id
= connector
->base
.id
;
2154 out_resp
->connector_type
= connector
->connector_type
;
2155 out_resp
->connector_type_id
= connector
->connector_type_id
;
2156 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2157 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2158 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2159 out_resp
->connection
= connector
->status
;
2161 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2162 encoder
= drm_connector_get_encoder(connector
);
2164 out_resp
->encoder_id
= encoder
->base
.id
;
2166 out_resp
->encoder_id
= 0;
2169 * This ioctl is called twice, once to determine how much space is
2170 * needed, and the 2nd time to fill it.
2172 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2174 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2175 list_for_each_entry(mode
, &connector
->modes
, head
) {
2176 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2179 drm_crtc_convert_to_umode(&u_mode
, mode
);
2180 if (copy_to_user(mode_ptr
+ copied
,
2181 &u_mode
, sizeof(u_mode
))) {
2188 out_resp
->count_modes
= mode_count
;
2190 ret
= get_properties(&connector
->base
, file_priv
->atomic
,
2191 (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
),
2192 (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
),
2193 &out_resp
->count_props
);
2197 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2199 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2200 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2201 if (connector
->encoder_ids
[i
] != 0) {
2202 if (put_user(connector
->encoder_ids
[i
],
2203 encoder_ptr
+ copied
)) {
2211 out_resp
->count_encoders
= encoders_count
;
2214 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2215 mutex_unlock(&dev
->mode_config
.mutex
);
2220 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
2222 struct drm_connector
*connector
;
2223 struct drm_device
*dev
= encoder
->dev
;
2224 bool uses_atomic
= false;
2226 /* For atomic drivers only state objects are synchronously updated and
2227 * protected by modeset locks, so check those first. */
2228 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2229 if (!connector
->state
)
2234 if (connector
->state
->best_encoder
!= encoder
)
2237 return connector
->state
->crtc
;
2240 /* Don't return stale data (e.g. pending async disable). */
2244 return encoder
->crtc
;
2248 * drm_mode_getencoder - get encoder configuration
2249 * @dev: drm device for the ioctl
2250 * @data: data pointer for the ioctl
2251 * @file_priv: drm file for the ioctl call
2253 * Construct a encoder configuration structure to return to the user.
2255 * Called by the user via ioctl.
2258 * Zero on success, negative errno on failure.
2260 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2261 struct drm_file
*file_priv
)
2263 struct drm_mode_get_encoder
*enc_resp
= data
;
2264 struct drm_encoder
*encoder
;
2265 struct drm_crtc
*crtc
;
2267 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2270 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2274 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2275 crtc
= drm_encoder_get_crtc(encoder
);
2277 enc_resp
->crtc_id
= crtc
->base
.id
;
2278 else if (encoder
->crtc
)
2279 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2281 enc_resp
->crtc_id
= 0;
2282 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2284 enc_resp
->encoder_type
= encoder
->encoder_type
;
2285 enc_resp
->encoder_id
= encoder
->base
.id
;
2286 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2287 enc_resp
->possible_clones
= encoder
->possible_clones
;
2293 * drm_mode_getplane_res - enumerate all plane resources
2296 * @file_priv: DRM file info
2298 * Construct a list of plane ids to return to the user.
2300 * Called by the user via ioctl.
2303 * Zero on success, negative errno on failure.
2305 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2306 struct drm_file
*file_priv
)
2308 struct drm_mode_get_plane_res
*plane_resp
= data
;
2309 struct drm_mode_config
*config
;
2310 struct drm_plane
*plane
;
2311 uint32_t __user
*plane_ptr
;
2313 unsigned num_planes
;
2315 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2318 config
= &dev
->mode_config
;
2320 if (file_priv
->universal_planes
)
2321 num_planes
= config
->num_total_plane
;
2323 num_planes
= config
->num_overlay_plane
;
2326 * This ioctl is called twice, once to determine how much space is
2327 * needed, and the 2nd time to fill it.
2330 (plane_resp
->count_planes
>= num_planes
)) {
2331 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2333 /* Plane lists are invariant, no locking needed. */
2334 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2336 * Unless userspace set the 'universal planes'
2337 * capability bit, only advertise overlays.
2339 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2340 !file_priv
->universal_planes
)
2343 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
2348 plane_resp
->count_planes
= num_planes
;
2354 * drm_mode_getplane - get plane configuration
2357 * @file_priv: DRM file info
2359 * Construct a plane configuration structure to return to the user.
2361 * Called by the user via ioctl.
2364 * Zero on success, negative errno on failure.
2366 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2367 struct drm_file
*file_priv
)
2369 struct drm_mode_get_plane
*plane_resp
= data
;
2370 struct drm_plane
*plane
;
2371 uint32_t __user
*format_ptr
;
2373 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2376 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2380 drm_modeset_lock(&plane
->mutex
, NULL
);
2382 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2384 plane_resp
->crtc_id
= 0;
2387 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2389 plane_resp
->fb_id
= 0;
2390 drm_modeset_unlock(&plane
->mutex
);
2392 plane_resp
->plane_id
= plane
->base
.id
;
2393 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2394 plane_resp
->gamma_size
= 0;
2397 * This ioctl is called twice, once to determine how much space is
2398 * needed, and the 2nd time to fill it.
2400 if (plane
->format_count
&&
2401 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2402 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2403 if (copy_to_user(format_ptr
,
2404 plane
->format_types
,
2405 sizeof(uint32_t) * plane
->format_count
)) {
2409 plane_resp
->count_format_types
= plane
->format_count
;
2415 * setplane_internal - setplane handler for internal callers
2417 * Note that we assume an extra reference has already been taken on fb. If the
2418 * update fails, this reference will be dropped before return; if it succeeds,
2419 * the previous framebuffer (if any) will be unreferenced instead.
2421 * src_{x,y,w,h} are provided in 16.16 fixed point format
2423 static int __setplane_internal(struct drm_plane
*plane
,
2424 struct drm_crtc
*crtc
,
2425 struct drm_framebuffer
*fb
,
2426 int32_t crtc_x
, int32_t crtc_y
,
2427 uint32_t crtc_w
, uint32_t crtc_h
,
2428 /* src_{x,y,w,h} values are 16.16 fixed point */
2429 uint32_t src_x
, uint32_t src_y
,
2430 uint32_t src_w
, uint32_t src_h
)
2433 unsigned int fb_width
, fb_height
;
2436 /* No fb means shut it down */
2438 plane
->old_fb
= plane
->fb
;
2439 ret
= plane
->funcs
->disable_plane(plane
);
2444 plane
->old_fb
= NULL
;
2449 /* Check whether this plane is usable on this CRTC */
2450 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2451 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2456 /* Check whether this plane supports the fb pixel format. */
2457 for (i
= 0; i
< plane
->format_count
; i
++)
2458 if (fb
->pixel_format
== plane
->format_types
[i
])
2460 if (i
== plane
->format_count
) {
2461 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2462 drm_get_format_name(fb
->pixel_format
));
2467 fb_width
= fb
->width
<< 16;
2468 fb_height
= fb
->height
<< 16;
2470 /* Make sure source coordinates are inside the fb. */
2471 if (src_w
> fb_width
||
2472 src_x
> fb_width
- src_w
||
2473 src_h
> fb_height
||
2474 src_y
> fb_height
- src_h
) {
2475 DRM_DEBUG_KMS("Invalid source coordinates "
2476 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2477 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2478 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2479 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2480 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2485 plane
->old_fb
= plane
->fb
;
2486 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2487 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2488 src_x
, src_y
, src_w
, src_h
);
2494 plane
->old_fb
= NULL
;
2499 drm_framebuffer_unreference(fb
);
2501 drm_framebuffer_unreference(plane
->old_fb
);
2502 plane
->old_fb
= NULL
;
2507 static int setplane_internal(struct drm_plane
*plane
,
2508 struct drm_crtc
*crtc
,
2509 struct drm_framebuffer
*fb
,
2510 int32_t crtc_x
, int32_t crtc_y
,
2511 uint32_t crtc_w
, uint32_t crtc_h
,
2512 /* src_{x,y,w,h} values are 16.16 fixed point */
2513 uint32_t src_x
, uint32_t src_y
,
2514 uint32_t src_w
, uint32_t src_h
)
2518 drm_modeset_lock_all(plane
->dev
);
2519 ret
= __setplane_internal(plane
, crtc
, fb
,
2520 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2521 src_x
, src_y
, src_w
, src_h
);
2522 drm_modeset_unlock_all(plane
->dev
);
2528 * drm_mode_setplane - configure a plane's configuration
2530 * @data: ioctl data*
2531 * @file_priv: DRM file info
2533 * Set plane configuration, including placement, fb, scaling, and other factors.
2534 * Or pass a NULL fb to disable (planes may be disabled without providing a
2538 * Zero on success, negative errno on failure.
2540 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2541 struct drm_file
*file_priv
)
2543 struct drm_mode_set_plane
*plane_req
= data
;
2544 struct drm_plane
*plane
;
2545 struct drm_crtc
*crtc
= NULL
;
2546 struct drm_framebuffer
*fb
= NULL
;
2548 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2551 /* Give drivers some help against integer overflows */
2552 if (plane_req
->crtc_w
> INT_MAX
||
2553 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2554 plane_req
->crtc_h
> INT_MAX
||
2555 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2556 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2557 plane_req
->crtc_w
, plane_req
->crtc_h
,
2558 plane_req
->crtc_x
, plane_req
->crtc_y
);
2563 * First, find the plane, crtc, and fb objects. If not available,
2564 * we don't bother to call the driver.
2566 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2568 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2569 plane_req
->plane_id
);
2573 if (plane_req
->fb_id
) {
2574 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2576 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2581 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2583 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2584 plane_req
->crtc_id
);
2590 * setplane_internal will take care of deref'ing either the old or new
2591 * framebuffer depending on success.
2593 return setplane_internal(plane
, crtc
, fb
,
2594 plane_req
->crtc_x
, plane_req
->crtc_y
,
2595 plane_req
->crtc_w
, plane_req
->crtc_h
,
2596 plane_req
->src_x
, plane_req
->src_y
,
2597 plane_req
->src_w
, plane_req
->src_h
);
2601 * drm_mode_set_config_internal - helper to call ->set_config
2602 * @set: modeset config to set
2604 * This is a little helper to wrap internal calls to the ->set_config driver
2605 * interface. The only thing it adds is correct refcounting dance.
2608 * Zero on success, negative errno on failure.
2610 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2612 struct drm_crtc
*crtc
= set
->crtc
;
2613 struct drm_framebuffer
*fb
;
2614 struct drm_crtc
*tmp
;
2618 * NOTE: ->set_config can also disable other crtcs (if we steal all
2619 * connectors from it), hence we need to refcount the fbs across all
2620 * crtcs. Atomic modeset will have saner semantics ...
2622 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2623 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2627 ret
= crtc
->funcs
->set_config(set
);
2629 crtc
->primary
->crtc
= crtc
;
2630 crtc
->primary
->fb
= fb
;
2633 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2634 if (tmp
->primary
->fb
)
2635 drm_framebuffer_reference(tmp
->primary
->fb
);
2636 if (tmp
->primary
->old_fb
)
2637 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2638 tmp
->primary
->old_fb
= NULL
;
2643 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2646 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2647 * @mode: mode to query
2648 * @hdisplay: hdisplay value to fill in
2649 * @vdisplay: vdisplay value to fill in
2651 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2652 * the appropriate layout.
2654 void drm_crtc_get_hv_timing(const struct drm_display_mode
*mode
,
2655 int *hdisplay
, int *vdisplay
)
2657 struct drm_display_mode adjusted
;
2659 drm_mode_copy(&adjusted
, mode
);
2660 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE_ONLY
);
2661 *hdisplay
= adjusted
.crtc_hdisplay
;
2662 *vdisplay
= adjusted
.crtc_vdisplay
;
2664 EXPORT_SYMBOL(drm_crtc_get_hv_timing
);
2667 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2669 * @crtc: CRTC that framebuffer will be displayed on
2672 * @mode: mode that framebuffer will be displayed under
2673 * @fb: framebuffer to check size of
2675 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2677 const struct drm_display_mode
*mode
,
2678 const struct drm_framebuffer
*fb
)
2681 int hdisplay
, vdisplay
;
2683 drm_crtc_get_hv_timing(mode
, &hdisplay
, &vdisplay
);
2685 if (crtc
->invert_dimensions
)
2686 swap(hdisplay
, vdisplay
);
2688 if (hdisplay
> fb
->width
||
2689 vdisplay
> fb
->height
||
2690 x
> fb
->width
- hdisplay
||
2691 y
> fb
->height
- vdisplay
) {
2692 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2693 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2694 crtc
->invert_dimensions
? " (inverted)" : "");
2700 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2703 * drm_mode_setcrtc - set CRTC configuration
2704 * @dev: drm device for the ioctl
2705 * @data: data pointer for the ioctl
2706 * @file_priv: drm file for the ioctl call
2708 * Build a new CRTC configuration based on user request.
2710 * Called by the user via ioctl.
2713 * Zero on success, negative errno on failure.
2715 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2716 struct drm_file
*file_priv
)
2718 struct drm_mode_config
*config
= &dev
->mode_config
;
2719 struct drm_mode_crtc
*crtc_req
= data
;
2720 struct drm_crtc
*crtc
;
2721 struct drm_connector
**connector_set
= NULL
, *connector
;
2722 struct drm_framebuffer
*fb
= NULL
;
2723 struct drm_display_mode
*mode
= NULL
;
2724 struct drm_mode_set set
;
2725 uint32_t __user
*set_connectors_ptr
;
2729 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2732 /* For some reason crtc x/y offsets are signed internally. */
2733 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2736 drm_modeset_lock_all(dev
);
2737 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2739 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2743 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2745 if (crtc_req
->mode_valid
) {
2746 /* If we have a mode we need a framebuffer. */
2747 /* If we pass -1, set the mode with the currently bound fb */
2748 if (crtc_req
->fb_id
== -1) {
2749 if (!crtc
->primary
->fb
) {
2750 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2754 fb
= crtc
->primary
->fb
;
2755 /* Make refcounting symmetric with the lookup path. */
2756 drm_framebuffer_reference(fb
);
2758 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2760 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2767 mode
= drm_mode_create(dev
);
2773 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2775 DRM_DEBUG_KMS("Invalid mode\n");
2779 mode
->status
= drm_mode_validate_basic(mode
);
2780 if (mode
->status
!= MODE_OK
) {
2785 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2787 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2794 if (crtc_req
->count_connectors
== 0 && mode
) {
2795 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2800 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2801 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2802 crtc_req
->count_connectors
);
2807 if (crtc_req
->count_connectors
> 0) {
2810 /* Avoid unbounded kernel memory allocation */
2811 if (crtc_req
->count_connectors
> config
->num_connector
) {
2816 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
2817 sizeof(struct drm_connector
*),
2819 if (!connector_set
) {
2824 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2825 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2826 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2831 connector
= drm_connector_find(dev
, out_id
);
2833 DRM_DEBUG_KMS("Connector id %d unknown\n",
2838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2842 connector_set
[i
] = connector
;
2847 set
.x
= crtc_req
->x
;
2848 set
.y
= crtc_req
->y
;
2850 set
.connectors
= connector_set
;
2851 set
.num_connectors
= crtc_req
->count_connectors
;
2853 ret
= drm_mode_set_config_internal(&set
);
2857 drm_framebuffer_unreference(fb
);
2859 kfree(connector_set
);
2860 drm_mode_destroy(dev
, mode
);
2861 drm_modeset_unlock_all(dev
);
2866 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2867 * universal plane handler call
2868 * @crtc: crtc to update cursor for
2869 * @req: data pointer for the ioctl
2870 * @file_priv: drm file for the ioctl call
2872 * Legacy cursor ioctl's work directly with driver buffer handles. To
2873 * translate legacy ioctl calls into universal plane handler calls, we need to
2874 * wrap the native buffer handle in a drm_framebuffer.
2876 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2877 * buffer with a pitch of 4*width; the universal plane interface should be used
2878 * directly in cases where the hardware can support other buffer settings and
2879 * userspace wants to make use of these capabilities.
2882 * Zero on success, negative errno on failure.
2884 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2885 struct drm_mode_cursor2
*req
,
2886 struct drm_file
*file_priv
)
2888 struct drm_device
*dev
= crtc
->dev
;
2889 struct drm_framebuffer
*fb
= NULL
;
2890 struct drm_mode_fb_cmd2 fbreq
= {
2891 .width
= req
->width
,
2892 .height
= req
->height
,
2893 .pixel_format
= DRM_FORMAT_ARGB8888
,
2894 .pitches
= { req
->width
* 4 },
2895 .handles
= { req
->handle
},
2897 int32_t crtc_x
, crtc_y
;
2898 uint32_t crtc_w
= 0, crtc_h
= 0;
2899 uint32_t src_w
= 0, src_h
= 0;
2902 BUG_ON(!crtc
->cursor
);
2903 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
2906 * Obtain fb we'll be using (either new or existing) and take an extra
2907 * reference to it if fb != null. setplane will take care of dropping
2908 * the reference if the plane update fails.
2910 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2912 fb
= internal_framebuffer_create(dev
, &fbreq
, file_priv
);
2914 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2921 fb
= crtc
->cursor
->fb
;
2923 drm_framebuffer_reference(fb
);
2926 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2930 crtc_x
= crtc
->cursor_x
;
2931 crtc_y
= crtc
->cursor_y
;
2936 crtc_h
= fb
->height
;
2937 src_w
= fb
->width
<< 16;
2938 src_h
= fb
->height
<< 16;
2942 * setplane_internal will take care of deref'ing either the old or new
2943 * framebuffer depending on success.
2945 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
2946 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2947 0, 0, src_w
, src_h
);
2949 /* Update successful; save new cursor position, if necessary */
2950 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2951 crtc
->cursor_x
= req
->x
;
2952 crtc
->cursor_y
= req
->y
;
2958 static int drm_mode_cursor_common(struct drm_device
*dev
,
2959 struct drm_mode_cursor2
*req
,
2960 struct drm_file
*file_priv
)
2962 struct drm_crtc
*crtc
;
2965 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2968 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2971 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2973 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2978 * If this crtc has a universal cursor plane, call that plane's update
2979 * handler rather than using legacy cursor handlers.
2981 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
2983 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
2987 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2988 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2992 /* Turns off the cursor if handle is 0 */
2993 if (crtc
->funcs
->cursor_set2
)
2994 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2995 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2997 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2998 req
->width
, req
->height
);
3001 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3002 if (crtc
->funcs
->cursor_move
) {
3003 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
3010 drm_modeset_unlock_crtc(crtc
);
3018 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3019 * @dev: drm device for the ioctl
3020 * @data: data pointer for the ioctl
3021 * @file_priv: drm file for the ioctl call
3023 * Set the cursor configuration based on user request.
3025 * Called by the user via ioctl.
3028 * Zero on success, negative errno on failure.
3030 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
3031 void *data
, struct drm_file
*file_priv
)
3033 struct drm_mode_cursor
*req
= data
;
3034 struct drm_mode_cursor2 new_req
;
3036 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
3037 new_req
.hot_x
= new_req
.hot_y
= 0;
3039 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
3043 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3044 * @dev: drm device for the ioctl
3045 * @data: data pointer for the ioctl
3046 * @file_priv: drm file for the ioctl call
3048 * Set the cursor configuration based on user request. This implements the 2nd
3049 * version of the cursor ioctl, which allows userspace to additionally specify
3050 * the hotspot of the pointer.
3052 * Called by the user via ioctl.
3055 * Zero on success, negative errno on failure.
3057 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
3058 void *data
, struct drm_file
*file_priv
)
3060 struct drm_mode_cursor2
*req
= data
;
3062 return drm_mode_cursor_common(dev
, req
, file_priv
);
3066 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3067 * @bpp: bits per pixels
3068 * @depth: bit depth per pixel
3070 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3071 * Useful in fbdev emulation code, since that deals in those values.
3073 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
3079 fmt
= DRM_FORMAT_C8
;
3083 fmt
= DRM_FORMAT_XRGB1555
;
3085 fmt
= DRM_FORMAT_RGB565
;
3088 fmt
= DRM_FORMAT_RGB888
;
3092 fmt
= DRM_FORMAT_XRGB8888
;
3093 else if (depth
== 30)
3094 fmt
= DRM_FORMAT_XRGB2101010
;
3096 fmt
= DRM_FORMAT_ARGB8888
;
3099 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3100 fmt
= DRM_FORMAT_XRGB8888
;
3106 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
3109 * drm_mode_addfb - add an FB to the graphics configuration
3110 * @dev: drm device for the ioctl
3111 * @data: data pointer for the ioctl
3112 * @file_priv: drm file for the ioctl call
3114 * Add a new FB to the specified CRTC, given a user request. This is the
3115 * original addfb ioctl which only supported RGB formats.
3117 * Called by the user via ioctl.
3120 * Zero on success, negative errno on failure.
3122 int drm_mode_addfb(struct drm_device
*dev
,
3123 void *data
, struct drm_file
*file_priv
)
3125 struct drm_mode_fb_cmd
*or = data
;
3126 struct drm_mode_fb_cmd2 r
= {};
3129 /* convert to new format and call new ioctl */
3130 r
.fb_id
= or->fb_id
;
3131 r
.width
= or->width
;
3132 r
.height
= or->height
;
3133 r
.pitches
[0] = or->pitch
;
3134 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
3135 r
.handles
[0] = or->handle
;
3137 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
3141 or->fb_id
= r
.fb_id
;
3146 static int format_check(const struct drm_mode_fb_cmd2
*r
)
3148 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
3152 case DRM_FORMAT_RGB332
:
3153 case DRM_FORMAT_BGR233
:
3154 case DRM_FORMAT_XRGB4444
:
3155 case DRM_FORMAT_XBGR4444
:
3156 case DRM_FORMAT_RGBX4444
:
3157 case DRM_FORMAT_BGRX4444
:
3158 case DRM_FORMAT_ARGB4444
:
3159 case DRM_FORMAT_ABGR4444
:
3160 case DRM_FORMAT_RGBA4444
:
3161 case DRM_FORMAT_BGRA4444
:
3162 case DRM_FORMAT_XRGB1555
:
3163 case DRM_FORMAT_XBGR1555
:
3164 case DRM_FORMAT_RGBX5551
:
3165 case DRM_FORMAT_BGRX5551
:
3166 case DRM_FORMAT_ARGB1555
:
3167 case DRM_FORMAT_ABGR1555
:
3168 case DRM_FORMAT_RGBA5551
:
3169 case DRM_FORMAT_BGRA5551
:
3170 case DRM_FORMAT_RGB565
:
3171 case DRM_FORMAT_BGR565
:
3172 case DRM_FORMAT_RGB888
:
3173 case DRM_FORMAT_BGR888
:
3174 case DRM_FORMAT_XRGB8888
:
3175 case DRM_FORMAT_XBGR8888
:
3176 case DRM_FORMAT_RGBX8888
:
3177 case DRM_FORMAT_BGRX8888
:
3178 case DRM_FORMAT_ARGB8888
:
3179 case DRM_FORMAT_ABGR8888
:
3180 case DRM_FORMAT_RGBA8888
:
3181 case DRM_FORMAT_BGRA8888
:
3182 case DRM_FORMAT_XRGB2101010
:
3183 case DRM_FORMAT_XBGR2101010
:
3184 case DRM_FORMAT_RGBX1010102
:
3185 case DRM_FORMAT_BGRX1010102
:
3186 case DRM_FORMAT_ARGB2101010
:
3187 case DRM_FORMAT_ABGR2101010
:
3188 case DRM_FORMAT_RGBA1010102
:
3189 case DRM_FORMAT_BGRA1010102
:
3190 case DRM_FORMAT_YUYV
:
3191 case DRM_FORMAT_YVYU
:
3192 case DRM_FORMAT_UYVY
:
3193 case DRM_FORMAT_VYUY
:
3194 case DRM_FORMAT_AYUV
:
3195 case DRM_FORMAT_NV12
:
3196 case DRM_FORMAT_NV21
:
3197 case DRM_FORMAT_NV16
:
3198 case DRM_FORMAT_NV61
:
3199 case DRM_FORMAT_NV24
:
3200 case DRM_FORMAT_NV42
:
3201 case DRM_FORMAT_YUV410
:
3202 case DRM_FORMAT_YVU410
:
3203 case DRM_FORMAT_YUV411
:
3204 case DRM_FORMAT_YVU411
:
3205 case DRM_FORMAT_YUV420
:
3206 case DRM_FORMAT_YVU420
:
3207 case DRM_FORMAT_YUV422
:
3208 case DRM_FORMAT_YVU422
:
3209 case DRM_FORMAT_YUV444
:
3210 case DRM_FORMAT_YVU444
:
3213 DRM_DEBUG_KMS("invalid pixel format %s\n",
3214 drm_get_format_name(r
->pixel_format
));
3219 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3221 int ret
, hsub
, vsub
, num_planes
, i
;
3223 ret
= format_check(r
);
3225 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3226 drm_get_format_name(r
->pixel_format
));
3230 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3231 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3232 num_planes
= drm_format_num_planes(r
->pixel_format
);
3234 if (r
->width
== 0 || r
->width
% hsub
) {
3235 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
3239 if (r
->height
== 0 || r
->height
% vsub
) {
3240 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3244 for (i
= 0; i
< num_planes
; i
++) {
3245 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3246 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3247 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3249 if (!r
->handles
[i
]) {
3250 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3254 if ((uint64_t) width
* cpp
> UINT_MAX
)
3257 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3260 if (r
->pitches
[i
] < width
* cpp
) {
3261 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3269 static struct drm_framebuffer
*
3270 internal_framebuffer_create(struct drm_device
*dev
,
3271 struct drm_mode_fb_cmd2
*r
,
3272 struct drm_file
*file_priv
)
3274 struct drm_mode_config
*config
= &dev
->mode_config
;
3275 struct drm_framebuffer
*fb
;
3278 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3279 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3280 return ERR_PTR(-EINVAL
);
3283 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3284 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3285 r
->width
, config
->min_width
, config
->max_width
);
3286 return ERR_PTR(-EINVAL
);
3288 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3289 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3290 r
->height
, config
->min_height
, config
->max_height
);
3291 return ERR_PTR(-EINVAL
);
3294 ret
= framebuffer_check(r
);
3296 return ERR_PTR(ret
);
3298 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3300 DRM_DEBUG_KMS("could not create framebuffer\n");
3308 * drm_mode_addfb2 - add an FB to the graphics configuration
3309 * @dev: drm device for the ioctl
3310 * @data: data pointer for the ioctl
3311 * @file_priv: drm file for the ioctl call
3313 * Add a new FB to the specified CRTC, given a user request with format. This is
3314 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3315 * and uses fourcc codes as pixel format specifiers.
3317 * Called by the user via ioctl.
3320 * Zero on success, negative errno on failure.
3322 int drm_mode_addfb2(struct drm_device
*dev
,
3323 void *data
, struct drm_file
*file_priv
)
3325 struct drm_mode_fb_cmd2
*r
= data
;
3326 struct drm_framebuffer
*fb
;
3328 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3331 fb
= internal_framebuffer_create(dev
, r
, file_priv
);
3335 /* Transfer ownership to the filp for reaping on close */
3337 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3338 mutex_lock(&file_priv
->fbs_lock
);
3339 r
->fb_id
= fb
->base
.id
;
3340 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3341 mutex_unlock(&file_priv
->fbs_lock
);
3347 * drm_mode_rmfb - remove an FB from the configuration
3348 * @dev: drm device for the ioctl
3349 * @data: data pointer for the ioctl
3350 * @file_priv: drm file for the ioctl call
3352 * Remove the FB specified by the user.
3354 * Called by the user via ioctl.
3357 * Zero on success, negative errno on failure.
3359 int drm_mode_rmfb(struct drm_device
*dev
,
3360 void *data
, struct drm_file
*file_priv
)
3362 struct drm_framebuffer
*fb
= NULL
;
3363 struct drm_framebuffer
*fbl
= NULL
;
3364 uint32_t *id
= data
;
3367 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3370 mutex_lock(&file_priv
->fbs_lock
);
3371 mutex_lock(&dev
->mode_config
.fb_lock
);
3372 fb
= __drm_framebuffer_lookup(dev
, *id
);
3376 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3382 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3383 __drm_framebuffer_unregister(dev
, fb
);
3385 list_del_init(&fb
->filp_head
);
3386 mutex_unlock(&dev
->mode_config
.fb_lock
);
3387 mutex_unlock(&file_priv
->fbs_lock
);
3389 drm_framebuffer_remove(fb
);
3394 mutex_unlock(&dev
->mode_config
.fb_lock
);
3395 mutex_unlock(&file_priv
->fbs_lock
);
3401 * drm_mode_getfb - get FB info
3402 * @dev: drm device for the ioctl
3403 * @data: data pointer for the ioctl
3404 * @file_priv: drm file for the ioctl call
3406 * Lookup the FB given its ID and return info about it.
3408 * Called by the user via ioctl.
3411 * Zero on success, negative errno on failure.
3413 int drm_mode_getfb(struct drm_device
*dev
,
3414 void *data
, struct drm_file
*file_priv
)
3416 struct drm_mode_fb_cmd
*r
= data
;
3417 struct drm_framebuffer
*fb
;
3420 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3423 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3427 r
->height
= fb
->height
;
3428 r
->width
= fb
->width
;
3429 r
->depth
= fb
->depth
;
3430 r
->bpp
= fb
->bits_per_pixel
;
3431 r
->pitch
= fb
->pitches
[0];
3432 if (fb
->funcs
->create_handle
) {
3433 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3434 drm_is_control_client(file_priv
)) {
3435 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3438 /* GET_FB() is an unprivileged ioctl so we must not
3439 * return a buffer-handle to non-master processes! For
3440 * backwards-compatibility reasons, we cannot make
3441 * GET_FB() privileged, so just return an invalid handle
3442 * for non-masters. */
3450 drm_framebuffer_unreference(fb
);
3456 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3457 * @dev: drm device for the ioctl
3458 * @data: data pointer for the ioctl
3459 * @file_priv: drm file for the ioctl call
3461 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3462 * rectangle list. Generic userspace which does frontbuffer rendering must call
3463 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3464 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3466 * Modesetting drivers which always update the frontbuffer do not need to
3467 * implement the corresponding ->dirty framebuffer callback.
3469 * Called by the user via ioctl.
3472 * Zero on success, negative errno on failure.
3474 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3475 void *data
, struct drm_file
*file_priv
)
3477 struct drm_clip_rect __user
*clips_ptr
;
3478 struct drm_clip_rect
*clips
= NULL
;
3479 struct drm_mode_fb_dirty_cmd
*r
= data
;
3480 struct drm_framebuffer
*fb
;
3485 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3488 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3492 num_clips
= r
->num_clips
;
3493 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3495 if (!num_clips
!= !clips_ptr
) {
3500 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3502 /* If userspace annotates copy, clips must come in pairs */
3503 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3508 if (num_clips
&& clips_ptr
) {
3509 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3513 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
3519 ret
= copy_from_user(clips
, clips_ptr
,
3520 num_clips
* sizeof(*clips
));
3527 if (fb
->funcs
->dirty
) {
3528 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3537 drm_framebuffer_unreference(fb
);
3544 * drm_fb_release - remove and free the FBs on this file
3545 * @priv: drm file for the ioctl
3547 * Destroy all the FBs associated with @filp.
3549 * Called by the user via ioctl.
3552 * Zero on success, negative errno on failure.
3554 void drm_fb_release(struct drm_file
*priv
)
3556 struct drm_device
*dev
= priv
->minor
->dev
;
3557 struct drm_framebuffer
*fb
, *tfb
;
3560 * When the file gets released that means no one else can access the fb
3561 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3562 * avoid upsetting lockdep since the universal cursor code adds a
3563 * framebuffer while holding mutex locks.
3565 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3566 * locks is impossible here since no one else but this function can get
3569 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3571 mutex_lock(&dev
->mode_config
.fb_lock
);
3572 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3573 __drm_framebuffer_unregister(dev
, fb
);
3574 mutex_unlock(&dev
->mode_config
.fb_lock
);
3576 list_del_init(&fb
->filp_head
);
3578 /* This will also drop the fpriv->fbs reference. */
3579 drm_framebuffer_remove(fb
);
3584 * drm_property_create - create a new property type
3586 * @flags: flags specifying the property type
3587 * @name: name of the property
3588 * @num_values: number of pre-defined values
3590 * This creates a new generic drm property which can then be attached to a drm
3591 * object with drm_object_attach_property. The returned property object must be
3592 * freed with drm_property_destroy.
3594 * Note that the DRM core keeps a per-device list of properties and that, if
3595 * drm_mode_config_cleanup() is called, it will destroy all properties created
3599 * A pointer to the newly created property on success, NULL on failure.
3601 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3602 const char *name
, int num_values
)
3604 struct drm_property
*property
= NULL
;
3607 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3611 property
->dev
= dev
;
3614 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
3616 if (!property
->values
)
3620 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3624 property
->flags
= flags
;
3625 property
->num_values
= num_values
;
3626 INIT_LIST_HEAD(&property
->enum_list
);
3629 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3630 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3633 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3635 WARN_ON(!drm_property_type_valid(property
));
3639 kfree(property
->values
);
3643 EXPORT_SYMBOL(drm_property_create
);
3646 * drm_property_create_enum - create a new enumeration property type
3648 * @flags: flags specifying the property type
3649 * @name: name of the property
3650 * @props: enumeration lists with property values
3651 * @num_values: number of pre-defined values
3653 * This creates a new generic drm property which can then be attached to a drm
3654 * object with drm_object_attach_property. The returned property object must be
3655 * freed with drm_property_destroy.
3657 * Userspace is only allowed to set one of the predefined values for enumeration
3661 * A pointer to the newly created property on success, NULL on failure.
3663 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3665 const struct drm_prop_enum_list
*props
,
3668 struct drm_property
*property
;
3671 flags
|= DRM_MODE_PROP_ENUM
;
3673 property
= drm_property_create(dev
, flags
, name
, num_values
);
3677 for (i
= 0; i
< num_values
; i
++) {
3678 ret
= drm_property_add_enum(property
, i
,
3682 drm_property_destroy(dev
, property
);
3689 EXPORT_SYMBOL(drm_property_create_enum
);
3692 * drm_property_create_bitmask - create a new bitmask property type
3694 * @flags: flags specifying the property type
3695 * @name: name of the property
3696 * @props: enumeration lists with property bitflags
3697 * @num_props: size of the @props array
3698 * @supported_bits: bitmask of all supported enumeration values
3700 * This creates a new bitmask drm property which can then be attached to a drm
3701 * object with drm_object_attach_property. The returned property object must be
3702 * freed with drm_property_destroy.
3704 * Compared to plain enumeration properties userspace is allowed to set any
3705 * or'ed together combination of the predefined property bitflag values
3708 * A pointer to the newly created property on success, NULL on failure.
3710 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3711 int flags
, const char *name
,
3712 const struct drm_prop_enum_list
*props
,
3714 uint64_t supported_bits
)
3716 struct drm_property
*property
;
3717 int i
, ret
, index
= 0;
3718 int num_values
= hweight64(supported_bits
);
3720 flags
|= DRM_MODE_PROP_BITMASK
;
3722 property
= drm_property_create(dev
, flags
, name
, num_values
);
3725 for (i
= 0; i
< num_props
; i
++) {
3726 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3729 if (WARN_ON(index
>= num_values
)) {
3730 drm_property_destroy(dev
, property
);
3734 ret
= drm_property_add_enum(property
, index
++,
3738 drm_property_destroy(dev
, property
);
3745 EXPORT_SYMBOL(drm_property_create_bitmask
);
3747 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3748 int flags
, const char *name
,
3749 uint64_t min
, uint64_t max
)
3751 struct drm_property
*property
;
3753 property
= drm_property_create(dev
, flags
, name
, 2);
3757 property
->values
[0] = min
;
3758 property
->values
[1] = max
;
3764 * drm_property_create_range - create a new unsigned ranged property type
3766 * @flags: flags specifying the property type
3767 * @name: name of the property
3768 * @min: minimum value of the property
3769 * @max: maximum value of the property
3771 * This creates a new generic drm property which can then be attached to a drm
3772 * object with drm_object_attach_property. The returned property object must be
3773 * freed with drm_property_destroy.
3775 * Userspace is allowed to set any unsigned integer value in the (min, max)
3779 * A pointer to the newly created property on success, NULL on failure.
3781 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3783 uint64_t min
, uint64_t max
)
3785 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3788 EXPORT_SYMBOL(drm_property_create_range
);
3791 * drm_property_create_signed_range - create a new signed ranged property type
3793 * @flags: flags specifying the property type
3794 * @name: name of the property
3795 * @min: minimum value of the property
3796 * @max: maximum value of the property
3798 * This creates a new generic drm property which can then be attached to a drm
3799 * object with drm_object_attach_property. The returned property object must be
3800 * freed with drm_property_destroy.
3802 * Userspace is allowed to set any signed integer value in the (min, max)
3806 * A pointer to the newly created property on success, NULL on failure.
3808 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3809 int flags
, const char *name
,
3810 int64_t min
, int64_t max
)
3812 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3813 name
, I642U64(min
), I642U64(max
));
3815 EXPORT_SYMBOL(drm_property_create_signed_range
);
3818 * drm_property_create_object - create a new object property type
3820 * @flags: flags specifying the property type
3821 * @name: name of the property
3822 * @type: object type from DRM_MODE_OBJECT_* defines
3824 * This creates a new generic drm property which can then be attached to a drm
3825 * object with drm_object_attach_property. The returned property object must be
3826 * freed with drm_property_destroy.
3828 * Userspace is only allowed to set this to any property value of the given
3829 * @type. Only useful for atomic properties, which is enforced.
3832 * A pointer to the newly created property on success, NULL on failure.
3834 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3835 int flags
, const char *name
, uint32_t type
)
3837 struct drm_property
*property
;
3839 flags
|= DRM_MODE_PROP_OBJECT
;
3841 if (WARN_ON(!(flags
& DRM_MODE_PROP_ATOMIC
)))
3844 property
= drm_property_create(dev
, flags
, name
, 1);
3848 property
->values
[0] = type
;
3852 EXPORT_SYMBOL(drm_property_create_object
);
3855 * drm_property_create_bool - create a new boolean property type
3857 * @flags: flags specifying the property type
3858 * @name: name of the property
3860 * This creates a new generic drm property which can then be attached to a drm
3861 * object with drm_object_attach_property. The returned property object must be
3862 * freed with drm_property_destroy.
3864 * This is implemented as a ranged property with only {0, 1} as valid values.
3867 * A pointer to the newly created property on success, NULL on failure.
3869 struct drm_property
*drm_property_create_bool(struct drm_device
*dev
, int flags
,
3872 return drm_property_create_range(dev
, flags
, name
, 0, 1);
3874 EXPORT_SYMBOL(drm_property_create_bool
);
3877 * drm_property_add_enum - add a possible value to an enumeration property
3878 * @property: enumeration property to change
3879 * @index: index of the new enumeration
3880 * @value: value of the new enumeration
3881 * @name: symbolic name of the new enumeration
3883 * This functions adds enumerations to a property.
3885 * It's use is deprecated, drivers should use one of the more specific helpers
3886 * to directly create the property with all enumerations already attached.
3889 * Zero on success, error code on failure.
3891 int drm_property_add_enum(struct drm_property
*property
, int index
,
3892 uint64_t value
, const char *name
)
3894 struct drm_property_enum
*prop_enum
;
3896 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3897 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3901 * Bitmask enum properties have the additional constraint of values
3904 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3908 if (!list_empty(&property
->enum_list
)) {
3909 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
3910 if (prop_enum
->value
== value
) {
3911 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3912 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3918 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3922 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3923 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3924 prop_enum
->value
= value
;
3926 property
->values
[index
] = value
;
3927 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
3930 EXPORT_SYMBOL(drm_property_add_enum
);
3933 * drm_property_destroy - destroy a drm property
3935 * @property: property to destry
3937 * This function frees a property including any attached resources like
3938 * enumeration values.
3940 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3942 struct drm_property_enum
*prop_enum
, *pt
;
3944 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
3945 list_del(&prop_enum
->head
);
3949 if (property
->num_values
)
3950 kfree(property
->values
);
3951 drm_mode_object_put(dev
, &property
->base
);
3952 list_del(&property
->head
);
3955 EXPORT_SYMBOL(drm_property_destroy
);
3958 * drm_object_attach_property - attach a property to a modeset object
3959 * @obj: drm modeset object
3960 * @property: property to attach
3961 * @init_val: initial value of the property
3963 * This attaches the given property to the modeset object with the given initial
3964 * value. Currently this function cannot fail since the properties are stored in
3965 * a statically sized array.
3967 void drm_object_attach_property(struct drm_mode_object
*obj
,
3968 struct drm_property
*property
,
3971 int count
= obj
->properties
->count
;
3973 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3974 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3975 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3976 "you see this message on the same object type.\n",
3981 obj
->properties
->properties
[count
] = property
;
3982 obj
->properties
->values
[count
] = init_val
;
3983 obj
->properties
->count
++;
3984 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
3985 obj
->properties
->atomic_count
++;
3987 EXPORT_SYMBOL(drm_object_attach_property
);
3990 * drm_object_property_set_value - set the value of a property
3991 * @obj: drm mode object to set property value for
3992 * @property: property to set
3993 * @val: value the property should be set to
3995 * This functions sets a given property on a given object. This function only
3996 * changes the software state of the property, it does not call into the
3997 * driver's ->set_property callback.
4000 * Zero on success, error code on failure.
4002 int drm_object_property_set_value(struct drm_mode_object
*obj
,
4003 struct drm_property
*property
, uint64_t val
)
4007 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4008 if (obj
->properties
->properties
[i
] == property
) {
4009 obj
->properties
->values
[i
] = val
;
4016 EXPORT_SYMBOL(drm_object_property_set_value
);
4019 * drm_object_property_get_value - retrieve the value of a property
4020 * @obj: drm mode object to get property value from
4021 * @property: property to retrieve
4022 * @val: storage for the property value
4024 * This function retrieves the softare state of the given property for the given
4025 * property. Since there is no driver callback to retrieve the current property
4026 * value this might be out of sync with the hardware, depending upon the driver
4030 * Zero on success, error code on failure.
4032 int drm_object_property_get_value(struct drm_mode_object
*obj
,
4033 struct drm_property
*property
, uint64_t *val
)
4037 /* read-only properties bypass atomic mechanism and still store
4038 * their value in obj->properties->values[].. mostly to avoid
4039 * having to deal w/ EDID and similar props in atomic paths:
4041 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
4042 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
4043 return drm_atomic_get_property(obj
, property
, val
);
4045 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4046 if (obj
->properties
->properties
[i
] == property
) {
4047 *val
= obj
->properties
->values
[i
];
4054 EXPORT_SYMBOL(drm_object_property_get_value
);
4057 * drm_mode_getproperty_ioctl - get the property metadata
4060 * @file_priv: DRM file info
4062 * This function retrieves the metadata for a given property, like the different
4063 * possible values for an enum property or the limits for a range property.
4065 * Blob properties are special
4067 * Called by the user via ioctl.
4070 * Zero on success, negative errno on failure.
4072 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
4073 void *data
, struct drm_file
*file_priv
)
4075 struct drm_mode_get_property
*out_resp
= data
;
4076 struct drm_property
*property
;
4078 int value_count
= 0;
4081 struct drm_property_enum
*prop_enum
;
4082 struct drm_mode_property_enum __user
*enum_ptr
;
4083 uint64_t __user
*values_ptr
;
4085 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4088 drm_modeset_lock_all(dev
);
4089 property
= drm_property_find(dev
, out_resp
->prop_id
);
4095 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4096 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4097 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
4101 value_count
= property
->num_values
;
4103 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
4104 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
4105 out_resp
->flags
= property
->flags
;
4107 if ((out_resp
->count_values
>= value_count
) && value_count
) {
4108 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
4109 for (i
= 0; i
< value_count
; i
++) {
4110 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
4116 out_resp
->count_values
= value_count
;
4118 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4119 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4120 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
4122 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
4123 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4125 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
4130 if (copy_to_user(&enum_ptr
[copied
].name
,
4131 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
4138 out_resp
->count_enum_blobs
= enum_count
;
4142 * NOTE: The idea seems to have been to use this to read all the blob
4143 * property values. But nothing ever added them to the corresponding
4144 * list, userspace always used the special-purpose get_blob ioctl to
4145 * read the value for a blob property. It also doesn't make a lot of
4146 * sense to return values here when everything else is just metadata for
4147 * the property itself.
4149 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4150 out_resp
->count_enum_blobs
= 0;
4152 drm_modeset_unlock_all(dev
);
4156 static struct drm_property_blob
*
4157 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
4160 struct drm_property_blob
*blob
;
4163 if (!length
|| !data
)
4166 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
4170 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
4176 blob
->length
= length
;
4178 memcpy(blob
->data
, data
, length
);
4180 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
4184 static void drm_property_destroy_blob(struct drm_device
*dev
,
4185 struct drm_property_blob
*blob
)
4187 drm_mode_object_put(dev
, &blob
->base
);
4188 list_del(&blob
->head
);
4193 * drm_mode_getblob_ioctl - get the contents of a blob property value
4196 * @file_priv: DRM file info
4198 * This function retrieves the contents of a blob property. The value stored in
4199 * an object's blob property is just a normal modeset object id.
4201 * Called by the user via ioctl.
4204 * Zero on success, negative errno on failure.
4206 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
4207 void *data
, struct drm_file
*file_priv
)
4209 struct drm_mode_get_blob
*out_resp
= data
;
4210 struct drm_property_blob
*blob
;
4212 void __user
*blob_ptr
;
4214 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4217 drm_modeset_lock_all(dev
);
4218 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
4224 if (out_resp
->length
== blob
->length
) {
4225 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4226 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
4231 out_resp
->length
= blob
->length
;
4234 drm_modeset_unlock_all(dev
);
4239 * drm_mode_connector_set_path_property - set tile property on connector
4240 * @connector: connector to set property on.
4241 * @path: path to use for property.
4243 * This creates a property to expose to userspace to specify a
4244 * connector path. This is mainly used for DisplayPort MST where
4245 * connectors have a topology and we want to allow userspace to give
4246 * them more meaningful names.
4249 * Zero on success, negative errno on failure.
4251 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4254 struct drm_device
*dev
= connector
->dev
;
4255 size_t size
= strlen(path
) + 1;
4258 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
4260 if (!connector
->path_blob_ptr
)
4263 ret
= drm_object_property_set_value(&connector
->base
,
4264 dev
->mode_config
.path_property
,
4265 connector
->path_blob_ptr
->base
.id
);
4268 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4271 * drm_mode_connector_set_tile_property - set tile property on connector
4272 * @connector: connector to set property on.
4274 * This looks up the tile information for a connector, and creates a
4275 * property for userspace to parse if it exists. The property is of
4276 * the form of 8 integers using ':' as a separator.
4279 * Zero on success, errno on failure.
4281 int drm_mode_connector_set_tile_property(struct drm_connector
*connector
)
4283 struct drm_device
*dev
= connector
->dev
;
4287 if (connector
->tile_blob_ptr
)
4288 drm_property_destroy_blob(dev
, connector
->tile_blob_ptr
);
4290 if (!connector
->has_tile
) {
4291 connector
->tile_blob_ptr
= NULL
;
4292 ret
= drm_object_property_set_value(&connector
->base
,
4293 dev
->mode_config
.tile_property
, 0);
4297 snprintf(tile
, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4298 connector
->tile_group
->id
, connector
->tile_is_single_monitor
,
4299 connector
->num_h_tile
, connector
->num_v_tile
,
4300 connector
->tile_h_loc
, connector
->tile_v_loc
,
4301 connector
->tile_h_size
, connector
->tile_v_size
);
4302 size
= strlen(tile
) + 1;
4304 connector
->tile_blob_ptr
= drm_property_create_blob(connector
->dev
,
4306 if (!connector
->tile_blob_ptr
)
4309 ret
= drm_object_property_set_value(&connector
->base
,
4310 dev
->mode_config
.tile_property
,
4311 connector
->tile_blob_ptr
->base
.id
);
4314 EXPORT_SYMBOL(drm_mode_connector_set_tile_property
);
4317 * drm_mode_connector_update_edid_property - update the edid property of a connector
4318 * @connector: drm connector
4319 * @edid: new value of the edid property
4321 * This function creates a new blob modeset object and assigns its id to the
4322 * connector's edid property.
4325 * Zero on success, negative errno on failure.
4327 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4328 const struct edid
*edid
)
4330 struct drm_device
*dev
= connector
->dev
;
4334 /* ignore requests to set edid when overridden */
4335 if (connector
->override_edid
)
4338 if (connector
->edid_blob_ptr
)
4339 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
4341 /* Delete edid, when there is none. */
4343 connector
->edid_blob_ptr
= NULL
;
4344 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
4348 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4349 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
4351 if (!connector
->edid_blob_ptr
)
4354 ret
= drm_object_property_set_value(&connector
->base
,
4355 dev
->mode_config
.edid_property
,
4356 connector
->edid_blob_ptr
->base
.id
);
4360 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4362 /* Some properties could refer to dynamic refcnt'd objects, or things that
4363 * need special locking to handle lifetime issues (ie. to ensure the prop
4364 * value doesn't become invalid part way through the property update due to
4365 * race). The value returned by reference via 'obj' should be passed back
4366 * to drm_property_change_valid_put() after the property is set (and the
4367 * object to which the property is attached has a chance to take it's own
4370 bool drm_property_change_valid_get(struct drm_property
*property
,
4371 uint64_t value
, struct drm_mode_object
**ref
)
4375 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4380 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4381 if (value
< property
->values
[0] || value
> property
->values
[1])
4384 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4385 int64_t svalue
= U642I64(value
);
4387 if (svalue
< U642I64(property
->values
[0]) ||
4388 svalue
> U642I64(property
->values
[1]))
4391 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4392 uint64_t valid_mask
= 0;
4394 for (i
= 0; i
< property
->num_values
; i
++)
4395 valid_mask
|= (1ULL << property
->values
[i
]);
4396 return !(value
& ~valid_mask
);
4397 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4398 /* Only the driver knows */
4400 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4401 /* a zero value for an object property translates to null: */
4405 /* handle refcnt'd objects specially: */
4406 if (property
->values
[0] == DRM_MODE_OBJECT_FB
) {
4407 struct drm_framebuffer
*fb
;
4408 fb
= drm_framebuffer_lookup(property
->dev
, value
);
4416 return _object_find(property
->dev
, value
, property
->values
[0]) != NULL
;
4420 for (i
= 0; i
< property
->num_values
; i
++)
4421 if (property
->values
[i
] == value
)
4426 void drm_property_change_valid_put(struct drm_property
*property
,
4427 struct drm_mode_object
*ref
)
4432 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4433 if (property
->values
[0] == DRM_MODE_OBJECT_FB
)
4434 drm_framebuffer_unreference(obj_to_fb(ref
));
4439 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4442 * @file_priv: DRM file info
4444 * This function sets the current value for a connectors's property. It also
4445 * calls into a driver's ->set_property callback to update the hardware state
4447 * Called by the user via ioctl.
4450 * Zero on success, negative errno on failure.
4452 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4453 void *data
, struct drm_file
*file_priv
)
4455 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4456 struct drm_mode_obj_set_property obj_set_prop
= {
4457 .value
= conn_set_prop
->value
,
4458 .prop_id
= conn_set_prop
->prop_id
,
4459 .obj_id
= conn_set_prop
->connector_id
,
4460 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4463 /* It does all the locking and checking we need */
4464 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4467 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4468 struct drm_property
*property
,
4472 struct drm_connector
*connector
= obj_to_connector(obj
);
4474 /* Do DPMS ourselves */
4475 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4476 if (connector
->funcs
->dpms
)
4477 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4479 } else if (connector
->funcs
->set_property
)
4480 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4482 /* store the property value if successful */
4484 drm_object_property_set_value(&connector
->base
, property
, value
);
4488 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4489 struct drm_property
*property
,
4493 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4495 if (crtc
->funcs
->set_property
)
4496 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4498 drm_object_property_set_value(obj
, property
, value
);
4504 * drm_mode_plane_set_obj_prop - set the value of a property
4505 * @plane: drm plane object to set property value for
4506 * @property: property to set
4507 * @value: value the property should be set to
4509 * This functions sets a given property on a given plane object. This function
4510 * calls the driver's ->set_property callback and changes the software state of
4511 * the property if the callback succeeds.
4514 * Zero on success, error code on failure.
4516 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
4517 struct drm_property
*property
,
4521 struct drm_mode_object
*obj
= &plane
->base
;
4523 if (plane
->funcs
->set_property
)
4524 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4526 drm_object_property_set_value(obj
, property
, value
);
4530 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
4533 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4536 * @file_priv: DRM file info
4538 * This function retrieves the current value for an object's property. Compared
4539 * to the connector specific ioctl this one is extended to also work on crtc and
4542 * Called by the user via ioctl.
4545 * Zero on success, negative errno on failure.
4547 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4548 struct drm_file
*file_priv
)
4550 struct drm_mode_obj_get_properties
*arg
= data
;
4551 struct drm_mode_object
*obj
;
4554 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4557 drm_modeset_lock_all(dev
);
4559 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4564 if (!obj
->properties
) {
4569 ret
= get_properties(obj
, file_priv
->atomic
,
4570 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
4571 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
4575 drm_modeset_unlock_all(dev
);
4580 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4583 * @file_priv: DRM file info
4585 * This function sets the current value for an object's property. It also calls
4586 * into a driver's ->set_property callback to update the hardware state.
4587 * Compared to the connector specific ioctl this one is extended to also work on
4588 * crtc and plane objects.
4590 * Called by the user via ioctl.
4593 * Zero on success, negative errno on failure.
4595 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4596 struct drm_file
*file_priv
)
4598 struct drm_mode_obj_set_property
*arg
= data
;
4599 struct drm_mode_object
*arg_obj
;
4600 struct drm_mode_object
*prop_obj
;
4601 struct drm_property
*property
;
4602 int i
, ret
= -EINVAL
;
4603 struct drm_mode_object
*ref
;
4605 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4608 drm_modeset_lock_all(dev
);
4610 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4615 if (!arg_obj
->properties
)
4618 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4619 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
4622 if (i
== arg_obj
->properties
->count
)
4625 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4626 DRM_MODE_OBJECT_PROPERTY
);
4631 property
= obj_to_property(prop_obj
);
4633 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
4636 switch (arg_obj
->type
) {
4637 case DRM_MODE_OBJECT_CONNECTOR
:
4638 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4641 case DRM_MODE_OBJECT_CRTC
:
4642 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4644 case DRM_MODE_OBJECT_PLANE
:
4645 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
4646 property
, arg
->value
);
4650 drm_property_change_valid_put(property
, ref
);
4653 drm_modeset_unlock_all(dev
);
4658 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4659 * @connector: connector to attach
4660 * @encoder: encoder to attach @connector to
4662 * This function links up a connector to an encoder. Note that the routing
4663 * restrictions between encoders and crtcs are exposed to userspace through the
4664 * possible_clones and possible_crtcs bitmasks.
4667 * Zero on success, negative errno on failure.
4669 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4670 struct drm_encoder
*encoder
)
4674 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4675 if (connector
->encoder_ids
[i
] == 0) {
4676 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4682 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4685 * drm_mode_crtc_set_gamma_size - set the gamma table size
4686 * @crtc: CRTC to set the gamma table size for
4687 * @gamma_size: size of the gamma table
4689 * Drivers which support gamma tables should set this to the supported gamma
4690 * table size when initializing the CRTC. Currently the drm core only supports a
4691 * fixed gamma table size.
4694 * Zero on success, negative errno on failure.
4696 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4699 crtc
->gamma_size
= gamma_size
;
4701 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
4703 if (!crtc
->gamma_store
) {
4704 crtc
->gamma_size
= 0;
4710 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4713 * drm_mode_gamma_set_ioctl - set the gamma table
4716 * @file_priv: DRM file info
4718 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4719 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4721 * Called by the user via ioctl.
4724 * Zero on success, negative errno on failure.
4726 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4727 void *data
, struct drm_file
*file_priv
)
4729 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4730 struct drm_crtc
*crtc
;
4731 void *r_base
, *g_base
, *b_base
;
4735 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4738 drm_modeset_lock_all(dev
);
4739 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4745 if (crtc
->funcs
->gamma_set
== NULL
) {
4750 /* memcpy into gamma store */
4751 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4756 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4757 r_base
= crtc
->gamma_store
;
4758 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4763 g_base
= r_base
+ size
;
4764 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4769 b_base
= g_base
+ size
;
4770 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4775 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4778 drm_modeset_unlock_all(dev
);
4784 * drm_mode_gamma_get_ioctl - get the gamma table
4787 * @file_priv: DRM file info
4789 * Copy the current gamma table into the storage provided. This also provides
4790 * the gamma table size the driver expects, which can be used to size the
4791 * allocated storage.
4793 * Called by the user via ioctl.
4796 * Zero on success, negative errno on failure.
4798 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4799 void *data
, struct drm_file
*file_priv
)
4801 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4802 struct drm_crtc
*crtc
;
4803 void *r_base
, *g_base
, *b_base
;
4807 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4810 drm_modeset_lock_all(dev
);
4811 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4817 /* memcpy into gamma store */
4818 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4823 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4824 r_base
= crtc
->gamma_store
;
4825 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4830 g_base
= r_base
+ size
;
4831 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4836 b_base
= g_base
+ size
;
4837 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4842 drm_modeset_unlock_all(dev
);
4847 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4850 * @file_priv: DRM file info
4852 * This schedules an asynchronous update on a given CRTC, called page flip.
4853 * Optionally a drm event is generated to signal the completion of the event.
4854 * Generic drivers cannot assume that a pageflip with changed framebuffer
4855 * properties (including driver specific metadata like tiling layout) will work,
4856 * but some drivers support e.g. pixel format changes through the pageflip
4859 * Called by the user via ioctl.
4862 * Zero on success, negative errno on failure.
4864 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4865 void *data
, struct drm_file
*file_priv
)
4867 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4868 struct drm_crtc
*crtc
;
4869 struct drm_framebuffer
*fb
= NULL
;
4870 struct drm_pending_vblank_event
*e
= NULL
;
4871 unsigned long flags
;
4874 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4875 page_flip
->reserved
!= 0)
4878 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4881 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4885 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
4886 if (crtc
->primary
->fb
== NULL
) {
4887 /* The framebuffer is currently unbound, presumably
4888 * due to a hotplug event, that userspace has not
4895 if (crtc
->funcs
->page_flip
== NULL
)
4898 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4904 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4908 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4909 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4914 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4916 spin_lock_irqsave(&dev
->event_lock
, flags
);
4917 if (file_priv
->event_space
< sizeof(e
->event
)) {
4918 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4921 file_priv
->event_space
-= sizeof(e
->event
);
4922 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4924 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
4926 spin_lock_irqsave(&dev
->event_lock
, flags
);
4927 file_priv
->event_space
+= sizeof(e
->event
);
4928 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4932 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4933 e
->event
.base
.length
= sizeof(e
->event
);
4934 e
->event
.user_data
= page_flip
->user_data
;
4935 e
->base
.event
= &e
->event
.base
;
4936 e
->base
.file_priv
= file_priv
;
4938 (void (*) (struct drm_pending_event
*)) kfree
;
4941 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
4942 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4944 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4945 spin_lock_irqsave(&dev
->event_lock
, flags
);
4946 file_priv
->event_space
+= sizeof(e
->event
);
4947 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4950 /* Keep the old fb, don't unref it. */
4951 crtc
->primary
->old_fb
= NULL
;
4954 * Warn if the driver hasn't properly updated the crtc->fb
4955 * field to reflect that the new framebuffer is now used.
4956 * Failing to do so will screw with the reference counting
4959 WARN_ON(crtc
->primary
->fb
!= fb
);
4960 /* Unref only the old framebuffer. */
4966 drm_framebuffer_unreference(fb
);
4967 if (crtc
->primary
->old_fb
)
4968 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
4969 crtc
->primary
->old_fb
= NULL
;
4970 drm_modeset_unlock_crtc(crtc
);
4976 * drm_mode_config_reset - call ->reset callbacks
4979 * This functions calls all the crtc's, encoder's and connector's ->reset
4980 * callback. Drivers can use this in e.g. their driver load or resume code to
4981 * reset hardware and software state.
4983 void drm_mode_config_reset(struct drm_device
*dev
)
4985 struct drm_crtc
*crtc
;
4986 struct drm_plane
*plane
;
4987 struct drm_encoder
*encoder
;
4988 struct drm_connector
*connector
;
4990 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
)
4991 if (plane
->funcs
->reset
)
4992 plane
->funcs
->reset(plane
);
4994 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4995 if (crtc
->funcs
->reset
)
4996 crtc
->funcs
->reset(crtc
);
4998 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4999 if (encoder
->funcs
->reset
)
5000 encoder
->funcs
->reset(encoder
);
5002 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
5003 connector
->status
= connector_status_unknown
;
5005 if (connector
->funcs
->reset
)
5006 connector
->funcs
->reset(connector
);
5009 EXPORT_SYMBOL(drm_mode_config_reset
);
5012 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5015 * @file_priv: DRM file info
5017 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5018 * TTM or something else entirely) and returns the resulting buffer handle. This
5019 * handle can then be wrapped up into a framebuffer modeset object.
5021 * Note that userspace is not allowed to use such objects for render
5022 * acceleration - drivers must create their own private ioctls for such a use
5025 * Called by the user via ioctl.
5028 * Zero on success, negative errno on failure.
5030 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
5031 void *data
, struct drm_file
*file_priv
)
5033 struct drm_mode_create_dumb
*args
= data
;
5034 u32 cpp
, stride
, size
;
5036 if (!dev
->driver
->dumb_create
)
5038 if (!args
->width
|| !args
->height
|| !args
->bpp
)
5041 /* overflow checks for 32bit size calculations */
5042 /* NOTE: DIV_ROUND_UP() can overflow */
5043 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
5044 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
5046 stride
= cpp
* args
->width
;
5047 if (args
->height
> 0xffffffffU
/ stride
)
5050 /* test for wrap-around */
5051 size
= args
->height
* stride
;
5052 if (PAGE_ALIGN(size
) == 0)
5056 * handle, pitch and size are output parameters. Zero them out to
5057 * prevent drivers from accidentally using uninitialized data. Since
5058 * not all existing userspace is clearing these fields properly we
5059 * cannot reject IOCTL with garbage in them.
5065 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
5069 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5072 * @file_priv: DRM file info
5074 * Allocate an offset in the drm device node's address space to be able to
5075 * memory map a dumb buffer.
5077 * Called by the user via ioctl.
5080 * Zero on success, negative errno on failure.
5082 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
5083 void *data
, struct drm_file
*file_priv
)
5085 struct drm_mode_map_dumb
*args
= data
;
5087 /* call driver ioctl to get mmap offset */
5088 if (!dev
->driver
->dumb_map_offset
)
5091 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
5095 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5098 * @file_priv: DRM file info
5100 * This destroys the userspace handle for the given dumb backing storage buffer.
5101 * Since buffer objects must be reference counted in the kernel a buffer object
5102 * won't be immediately freed if a framebuffer modeset object still uses it.
5104 * Called by the user via ioctl.
5107 * Zero on success, negative errno on failure.
5109 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
5110 void *data
, struct drm_file
*file_priv
)
5112 struct drm_mode_destroy_dumb
*args
= data
;
5114 if (!dev
->driver
->dumb_destroy
)
5117 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
5121 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5122 * @format: pixel format (DRM_FORMAT_*)
5123 * @depth: storage for the depth value
5124 * @bpp: storage for the bpp value
5126 * This only supports RGB formats here for compat with code that doesn't use
5127 * pixel formats directly yet.
5129 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
5134 case DRM_FORMAT_RGB332
:
5135 case DRM_FORMAT_BGR233
:
5139 case DRM_FORMAT_XRGB1555
:
5140 case DRM_FORMAT_XBGR1555
:
5141 case DRM_FORMAT_RGBX5551
:
5142 case DRM_FORMAT_BGRX5551
:
5143 case DRM_FORMAT_ARGB1555
:
5144 case DRM_FORMAT_ABGR1555
:
5145 case DRM_FORMAT_RGBA5551
:
5146 case DRM_FORMAT_BGRA5551
:
5150 case DRM_FORMAT_RGB565
:
5151 case DRM_FORMAT_BGR565
:
5155 case DRM_FORMAT_RGB888
:
5156 case DRM_FORMAT_BGR888
:
5160 case DRM_FORMAT_XRGB8888
:
5161 case DRM_FORMAT_XBGR8888
:
5162 case DRM_FORMAT_RGBX8888
:
5163 case DRM_FORMAT_BGRX8888
:
5167 case DRM_FORMAT_XRGB2101010
:
5168 case DRM_FORMAT_XBGR2101010
:
5169 case DRM_FORMAT_RGBX1010102
:
5170 case DRM_FORMAT_BGRX1010102
:
5171 case DRM_FORMAT_ARGB2101010
:
5172 case DRM_FORMAT_ABGR2101010
:
5173 case DRM_FORMAT_RGBA1010102
:
5174 case DRM_FORMAT_BGRA1010102
:
5178 case DRM_FORMAT_ARGB8888
:
5179 case DRM_FORMAT_ABGR8888
:
5180 case DRM_FORMAT_RGBA8888
:
5181 case DRM_FORMAT_BGRA8888
:
5186 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5187 drm_get_format_name(format
));
5193 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
5196 * drm_format_num_planes - get the number of planes for format
5197 * @format: pixel format (DRM_FORMAT_*)
5200 * The number of planes used by the specified pixel format.
5202 int drm_format_num_planes(uint32_t format
)
5205 case DRM_FORMAT_YUV410
:
5206 case DRM_FORMAT_YVU410
:
5207 case DRM_FORMAT_YUV411
:
5208 case DRM_FORMAT_YVU411
:
5209 case DRM_FORMAT_YUV420
:
5210 case DRM_FORMAT_YVU420
:
5211 case DRM_FORMAT_YUV422
:
5212 case DRM_FORMAT_YVU422
:
5213 case DRM_FORMAT_YUV444
:
5214 case DRM_FORMAT_YVU444
:
5216 case DRM_FORMAT_NV12
:
5217 case DRM_FORMAT_NV21
:
5218 case DRM_FORMAT_NV16
:
5219 case DRM_FORMAT_NV61
:
5220 case DRM_FORMAT_NV24
:
5221 case DRM_FORMAT_NV42
:
5227 EXPORT_SYMBOL(drm_format_num_planes
);
5230 * drm_format_plane_cpp - determine the bytes per pixel value
5231 * @format: pixel format (DRM_FORMAT_*)
5232 * @plane: plane index
5235 * The bytes per pixel value for the specified plane.
5237 int drm_format_plane_cpp(uint32_t format
, int plane
)
5242 if (plane
>= drm_format_num_planes(format
))
5246 case DRM_FORMAT_YUYV
:
5247 case DRM_FORMAT_YVYU
:
5248 case DRM_FORMAT_UYVY
:
5249 case DRM_FORMAT_VYUY
:
5251 case DRM_FORMAT_NV12
:
5252 case DRM_FORMAT_NV21
:
5253 case DRM_FORMAT_NV16
:
5254 case DRM_FORMAT_NV61
:
5255 case DRM_FORMAT_NV24
:
5256 case DRM_FORMAT_NV42
:
5257 return plane
? 2 : 1;
5258 case DRM_FORMAT_YUV410
:
5259 case DRM_FORMAT_YVU410
:
5260 case DRM_FORMAT_YUV411
:
5261 case DRM_FORMAT_YVU411
:
5262 case DRM_FORMAT_YUV420
:
5263 case DRM_FORMAT_YVU420
:
5264 case DRM_FORMAT_YUV422
:
5265 case DRM_FORMAT_YVU422
:
5266 case DRM_FORMAT_YUV444
:
5267 case DRM_FORMAT_YVU444
:
5270 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
5274 EXPORT_SYMBOL(drm_format_plane_cpp
);
5277 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5278 * @format: pixel format (DRM_FORMAT_*)
5281 * The horizontal chroma subsampling factor for the
5282 * specified pixel format.
5284 int drm_format_horz_chroma_subsampling(uint32_t format
)
5287 case DRM_FORMAT_YUV411
:
5288 case DRM_FORMAT_YVU411
:
5289 case DRM_FORMAT_YUV410
:
5290 case DRM_FORMAT_YVU410
:
5292 case DRM_FORMAT_YUYV
:
5293 case DRM_FORMAT_YVYU
:
5294 case DRM_FORMAT_UYVY
:
5295 case DRM_FORMAT_VYUY
:
5296 case DRM_FORMAT_NV12
:
5297 case DRM_FORMAT_NV21
:
5298 case DRM_FORMAT_NV16
:
5299 case DRM_FORMAT_NV61
:
5300 case DRM_FORMAT_YUV422
:
5301 case DRM_FORMAT_YVU422
:
5302 case DRM_FORMAT_YUV420
:
5303 case DRM_FORMAT_YVU420
:
5309 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
5312 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5313 * @format: pixel format (DRM_FORMAT_*)
5316 * The vertical chroma subsampling factor for the
5317 * specified pixel format.
5319 int drm_format_vert_chroma_subsampling(uint32_t format
)
5322 case DRM_FORMAT_YUV410
:
5323 case DRM_FORMAT_YVU410
:
5325 case DRM_FORMAT_YUV420
:
5326 case DRM_FORMAT_YVU420
:
5327 case DRM_FORMAT_NV12
:
5328 case DRM_FORMAT_NV21
:
5334 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
5337 * drm_rotation_simplify() - Try to simplify the rotation
5338 * @rotation: Rotation to be simplified
5339 * @supported_rotations: Supported rotations
5341 * Attempt to simplify the rotation to a form that is supported.
5342 * Eg. if the hardware supports everything except DRM_REFLECT_X
5343 * one could call this function like this:
5345 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5346 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5347 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5349 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5350 * transforms the hardware supports, this function may not
5351 * be able to produce a supported transform, so the caller should
5352 * check the result afterwards.
5354 unsigned int drm_rotation_simplify(unsigned int rotation
,
5355 unsigned int supported_rotations
)
5357 if (rotation
& ~supported_rotations
) {
5358 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5359 rotation
= (rotation
& ~0xf) | BIT((ffs(rotation
& 0xf) + 1) % 4);
5364 EXPORT_SYMBOL(drm_rotation_simplify
);
5367 * drm_mode_config_init - initialize DRM mode_configuration structure
5370 * Initialize @dev's mode_config structure, used for tracking the graphics
5371 * configuration of @dev.
5373 * Since this initializes the modeset locks, no locking is possible. Which is no
5374 * problem, since this should happen single threaded at init time. It is the
5375 * driver's problem to ensure this guarantee.
5378 void drm_mode_config_init(struct drm_device
*dev
)
5380 mutex_init(&dev
->mode_config
.mutex
);
5381 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5382 mutex_init(&dev
->mode_config
.idr_mutex
);
5383 mutex_init(&dev
->mode_config
.fb_lock
);
5384 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5385 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5386 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5387 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5388 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5389 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5390 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5391 idr_init(&dev
->mode_config
.crtc_idr
);
5392 idr_init(&dev
->mode_config
.tile_idr
);
5394 drm_modeset_lock_all(dev
);
5395 drm_mode_create_standard_properties(dev
);
5396 drm_modeset_unlock_all(dev
);
5398 /* Just to be sure */
5399 dev
->mode_config
.num_fb
= 0;
5400 dev
->mode_config
.num_connector
= 0;
5401 dev
->mode_config
.num_crtc
= 0;
5402 dev
->mode_config
.num_encoder
= 0;
5403 dev
->mode_config
.num_overlay_plane
= 0;
5404 dev
->mode_config
.num_total_plane
= 0;
5406 EXPORT_SYMBOL(drm_mode_config_init
);
5409 * drm_mode_config_cleanup - free up DRM mode_config info
5412 * Free up all the connectors and CRTCs associated with this DRM device, then
5413 * free up the framebuffers and associated buffer objects.
5415 * Note that since this /should/ happen single-threaded at driver/device
5416 * teardown time, no locking is required. It's the driver's job to ensure that
5417 * this guarantee actually holds true.
5419 * FIXME: cleanup any dangling user buffer objects too
5421 void drm_mode_config_cleanup(struct drm_device
*dev
)
5423 struct drm_connector
*connector
, *ot
;
5424 struct drm_crtc
*crtc
, *ct
;
5425 struct drm_encoder
*encoder
, *enct
;
5426 struct drm_framebuffer
*fb
, *fbt
;
5427 struct drm_property
*property
, *pt
;
5428 struct drm_property_blob
*blob
, *bt
;
5429 struct drm_plane
*plane
, *plt
;
5431 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5433 encoder
->funcs
->destroy(encoder
);
5436 list_for_each_entry_safe(connector
, ot
,
5437 &dev
->mode_config
.connector_list
, head
) {
5438 connector
->funcs
->destroy(connector
);
5441 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5443 drm_property_destroy(dev
, property
);
5446 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5448 drm_property_destroy_blob(dev
, blob
);
5452 * Single-threaded teardown context, so it's not required to grab the
5453 * fb_lock to protect against concurrent fb_list access. Contrary, it
5454 * would actually deadlock with the drm_framebuffer_cleanup function.
5456 * Also, if there are any framebuffers left, that's a driver leak now,
5457 * so politely WARN about this.
5459 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5460 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5461 drm_framebuffer_remove(fb
);
5464 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5466 plane
->funcs
->destroy(plane
);
5469 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5470 crtc
->funcs
->destroy(crtc
);
5473 idr_destroy(&dev
->mode_config
.tile_idr
);
5474 idr_destroy(&dev
->mode_config
.crtc_idr
);
5475 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5477 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5479 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5480 unsigned int supported_rotations
)
5482 static const struct drm_prop_enum_list props
[] = {
5483 { DRM_ROTATE_0
, "rotate-0" },
5484 { DRM_ROTATE_90
, "rotate-90" },
5485 { DRM_ROTATE_180
, "rotate-180" },
5486 { DRM_ROTATE_270
, "rotate-270" },
5487 { DRM_REFLECT_X
, "reflect-x" },
5488 { DRM_REFLECT_Y
, "reflect-y" },
5491 return drm_property_create_bitmask(dev
, 0, "rotation",
5492 props
, ARRAY_SIZE(props
),
5493 supported_rotations
);
5495 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
5500 * Tile groups are used to represent tiled monitors with a unique
5501 * integer identifier. Tiled monitors using DisplayID v1.3 have
5502 * a unique 8-byte handle, we store this in a tile group, so we
5503 * have a common identifier for all tiles in a monitor group.
5505 static void drm_tile_group_free(struct kref
*kref
)
5507 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
5508 struct drm_device
*dev
= tg
->dev
;
5509 mutex_lock(&dev
->mode_config
.idr_mutex
);
5510 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
5511 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5516 * drm_mode_put_tile_group - drop a reference to a tile group.
5518 * @tg: tile group to drop reference to.
5520 * drop reference to tile group and free if 0.
5522 void drm_mode_put_tile_group(struct drm_device
*dev
,
5523 struct drm_tile_group
*tg
)
5525 kref_put(&tg
->refcount
, drm_tile_group_free
);
5529 * drm_mode_get_tile_group - get a reference to an existing tile group
5531 * @topology: 8-bytes unique per monitor.
5533 * Use the unique bytes to get a reference to an existing tile group.
5536 * tile group or NULL if not found.
5538 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
5541 struct drm_tile_group
*tg
;
5543 mutex_lock(&dev
->mode_config
.idr_mutex
);
5544 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
5545 if (!memcmp(tg
->group_data
, topology
, 8)) {
5546 if (!kref_get_unless_zero(&tg
->refcount
))
5548 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5552 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5557 * drm_mode_create_tile_group - create a tile group from a displayid description
5559 * @topology: 8-bytes unique per monitor.
5561 * Create a tile group for the unique monitor, and get a unique
5562 * identifier for the tile group.
5565 * new tile group or error.
5567 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
5570 struct drm_tile_group
*tg
;
5573 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
5575 return ERR_PTR(-ENOMEM
);
5577 kref_init(&tg
->refcount
);
5578 memcpy(tg
->group_data
, topology
, 8);
5581 mutex_lock(&dev
->mode_config
.idr_mutex
);
5582 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
5590 mutex_unlock(&dev
->mode_config
.idr_mutex
);