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 const 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" },
171 { DRM_MODE_CONNECTOR_DPI
, "DPI" },
174 static const struct drm_prop_enum_list drm_encoder_enum_list
[] = {
175 { DRM_MODE_ENCODER_NONE
, "None" },
176 { DRM_MODE_ENCODER_DAC
, "DAC" },
177 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC
, "TV" },
180 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
181 { DRM_MODE_ENCODER_DSI
, "DSI" },
182 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
183 { DRM_MODE_ENCODER_DPI
, "DPI" },
186 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] = {
187 { SubPixelUnknown
, "Unknown" },
188 { SubPixelHorizontalRGB
, "Horizontal RGB" },
189 { SubPixelHorizontalBGR
, "Horizontal BGR" },
190 { SubPixelVerticalRGB
, "Vertical RGB" },
191 { SubPixelVerticalBGR
, "Vertical BGR" },
192 { SubPixelNone
, "None" },
195 void drm_connector_ida_init(void)
199 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
200 ida_init(&drm_connector_enum_list
[i
].ida
);
203 void drm_connector_ida_destroy(void)
207 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
208 ida_destroy(&drm_connector_enum_list
[i
].ida
);
212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
218 const char *drm_get_connector_status_name(enum drm_connector_status status
)
220 if (status
== connector_status_connected
)
222 else if (status
== connector_status_disconnected
)
223 return "disconnected";
227 EXPORT_SYMBOL(drm_get_connector_status_name
);
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
236 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
238 return drm_subpixel_enum_list
[order
].name
;
240 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
243 * Internal function to assign a slot in the object idr and optionally
244 * register the object into the idr.
246 static int drm_mode_object_get_reg(struct drm_device
*dev
,
247 struct drm_mode_object
*obj
,
250 void (*obj_free_cb
)(struct kref
*kref
))
254 mutex_lock(&dev
->mode_config
.idr_mutex
);
255 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
258 * Set up the object linking under the protection of the idr
259 * lock so that other users can't see inconsistent state.
262 obj
->type
= obj_type
;
264 obj
->free_cb
= obj_free_cb
;
265 kref_init(&obj
->refcount
);
268 mutex_unlock(&dev
->mode_config
.idr_mutex
);
270 return ret
< 0 ? ret
: 0;
274 * drm_mode_object_get - allocate a new modeset identifier
276 * @obj: object pointer, used to generate unique ID
277 * @obj_type: object type
279 * Create a unique identifier based on @ptr in @dev's identifier space. Used
280 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
281 * modeset identifiers are _not_ reference counted. Hence don't use this for
282 * reference counted modeset objects like framebuffers.
285 * Zero on success, error code on failure.
287 int drm_mode_object_get(struct drm_device
*dev
,
288 struct drm_mode_object
*obj
, uint32_t obj_type
)
290 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true, NULL
);
293 static void drm_mode_object_register(struct drm_device
*dev
,
294 struct drm_mode_object
*obj
)
296 mutex_lock(&dev
->mode_config
.idr_mutex
);
297 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
298 mutex_unlock(&dev
->mode_config
.idr_mutex
);
302 * drm_mode_object_unregister - free a modeset identifer
304 * @object: object to free
306 * Free @id from @dev's unique identifier pool.
307 * This function can be called multiple times, and guards against
309 * These modeset identifiers are _not_ reference counted. Hence don't use this
310 * for reference counted modeset objects like framebuffers.
312 void drm_mode_object_unregister(struct drm_device
*dev
,
313 struct drm_mode_object
*object
)
315 mutex_lock(&dev
->mode_config
.idr_mutex
);
317 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
320 mutex_unlock(&dev
->mode_config
.idr_mutex
);
323 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
324 uint32_t id
, uint32_t type
)
326 struct drm_mode_object
*obj
= NULL
;
328 mutex_lock(&dev
->mode_config
.idr_mutex
);
329 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
330 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
332 if (obj
&& obj
->id
!= id
)
335 if (obj
&& obj
->free_cb
) {
336 if (!kref_get_unless_zero(&obj
->refcount
))
339 mutex_unlock(&dev
->mode_config
.idr_mutex
);
345 * drm_mode_object_find - look up a drm object with static lifetime
347 * @id: id of the mode object
348 * @type: type of the mode object
350 * This function is used to look up a modeset object. It will acquire a
351 * reference for reference counted objects. This reference must be dropped again
352 * by callind drm_mode_object_unreference().
354 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
355 uint32_t id
, uint32_t type
)
357 struct drm_mode_object
*obj
= NULL
;
359 obj
= _object_find(dev
, id
, type
);
362 EXPORT_SYMBOL(drm_mode_object_find
);
365 * drm_mode_object_unreference - decr the object refcnt
368 * This functions decrements the object's refcount if it is a refcounted modeset
369 * object. It is a no-op on any other object. This is used to drop references
370 * acquired with drm_mode_object_reference().
372 void drm_mode_object_unreference(struct drm_mode_object
*obj
)
375 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
376 kref_put(&obj
->refcount
, obj
->free_cb
);
379 EXPORT_SYMBOL(drm_mode_object_unreference
);
382 * drm_mode_object_reference - incr the object refcnt
385 * This functions increments the object's refcount if it is a refcounted modeset
386 * object. It is a no-op on any other object. References should be dropped again
387 * by calling drm_mode_object_unreference().
389 void drm_mode_object_reference(struct drm_mode_object
*obj
)
392 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
393 kref_get(&obj
->refcount
);
396 EXPORT_SYMBOL(drm_mode_object_reference
);
398 static void drm_framebuffer_free(struct kref
*kref
)
400 struct drm_framebuffer
*fb
=
401 container_of(kref
, struct drm_framebuffer
, base
.refcount
);
402 struct drm_device
*dev
= fb
->dev
;
405 * The lookup idr holds a weak reference, which has not necessarily been
406 * removed at this point. Check for that.
408 drm_mode_object_unregister(dev
, &fb
->base
);
410 fb
->funcs
->destroy(fb
);
414 * drm_framebuffer_init - initialize a framebuffer
416 * @fb: framebuffer to be initialized
417 * @funcs: ... with these functions
419 * Allocates an ID for the framebuffer's parent mode object, sets its mode
420 * functions & device file and adds it to the master fd list.
423 * This functions publishes the fb and makes it available for concurrent access
424 * by other users. Which means by this point the fb _must_ be fully set up -
425 * since all the fb attributes are invariant over its lifetime, no further
426 * locking but only correct reference counting is required.
429 * Zero on success, error code on failure.
431 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
432 const struct drm_framebuffer_funcs
*funcs
)
436 INIT_LIST_HEAD(&fb
->filp_head
);
440 ret
= drm_mode_object_get_reg(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
,
441 false, drm_framebuffer_free
);
445 mutex_lock(&dev
->mode_config
.fb_lock
);
446 dev
->mode_config
.num_fb
++;
447 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
448 mutex_unlock(&dev
->mode_config
.fb_lock
);
450 drm_mode_object_register(dev
, &fb
->base
);
454 EXPORT_SYMBOL(drm_framebuffer_init
);
457 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
459 * @id: id of the fb object
461 * If successful, this grabs an additional reference to the framebuffer -
462 * callers need to make sure to eventually unreference the returned framebuffer
463 * again, using @drm_framebuffer_unreference.
465 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
468 struct drm_mode_object
*obj
;
469 struct drm_framebuffer
*fb
= NULL
;
471 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_FB
);
476 EXPORT_SYMBOL(drm_framebuffer_lookup
);
479 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
480 * @fb: fb to unregister
482 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
483 * those used for fbdev. Note that the caller must hold a reference of it's own,
484 * i.e. the object may not be destroyed through this call (since it'll lead to a
485 * locking inversion).
487 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
489 struct drm_device
*dev
;
496 /* Mark fb as reaped and drop idr ref. */
497 drm_mode_object_unregister(dev
, &fb
->base
);
499 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
505 * Cleanup framebuffer. This function is intended to be used from the drivers
506 * ->destroy callback. It can also be used to clean up driver private
507 * framebuffers embedded into a larger structure.
509 * Note that this function does not remove the fb from active usuage - if it is
510 * still used anywhere, hilarity can ensue since userspace could call getfb on
511 * the id and get back -EINVAL. Obviously no concern at driver unload time.
513 * Also, the framebuffer will not be removed from the lookup idr - for
514 * user-created framebuffers this will happen in in the rmfb ioctl. For
515 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
516 * drm_framebuffer_unregister_private.
518 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
520 struct drm_device
*dev
= fb
->dev
;
522 mutex_lock(&dev
->mode_config
.fb_lock
);
524 dev
->mode_config
.num_fb
--;
525 mutex_unlock(&dev
->mode_config
.fb_lock
);
527 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
530 * drm_framebuffer_remove - remove and unreference a framebuffer object
531 * @fb: framebuffer to remove
533 * Scans all the CRTCs and planes in @dev's mode_config. If they're
534 * using @fb, removes it, setting it to NULL. Then drops the reference to the
535 * passed-in framebuffer. Might take the modeset locks.
537 * Note that this function optimizes the cleanup away if the caller holds the
538 * last reference to the framebuffer. It is also guaranteed to not take the
539 * modeset locks in this case.
541 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
543 struct drm_device
*dev
;
544 struct drm_crtc
*crtc
;
545 struct drm_plane
*plane
;
546 struct drm_mode_set set
;
554 WARN_ON(!list_empty(&fb
->filp_head
));
557 * drm ABI mandates that we remove any deleted framebuffers from active
558 * useage. But since most sane clients only remove framebuffers they no
559 * longer need, try to optimize this away.
561 * Since we're holding a reference ourselves, observing a refcount of 1
562 * means that we're the last holder and can skip it. Also, the refcount
563 * can never increase from 1 again, so we don't need any barriers or
566 * Note that userspace could try to race with use and instate a new
567 * usage _after_ we've cleared all current ones. End result will be an
568 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
571 if (drm_framebuffer_read_refcount(fb
) > 1) {
572 drm_modeset_lock_all(dev
);
573 /* remove from any CRTC */
574 drm_for_each_crtc(crtc
, dev
) {
575 if (crtc
->primary
->fb
== fb
) {
576 /* should turn off the crtc */
577 memset(&set
, 0, sizeof(struct drm_mode_set
));
580 ret
= drm_mode_set_config_internal(&set
);
582 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
586 drm_for_each_plane(plane
, dev
) {
588 drm_plane_force_disable(plane
);
590 drm_modeset_unlock_all(dev
);
593 drm_framebuffer_unreference(fb
);
595 EXPORT_SYMBOL(drm_framebuffer_remove
);
597 DEFINE_WW_CLASS(crtc_ww_class
);
599 static unsigned int drm_num_crtcs(struct drm_device
*dev
)
601 unsigned int num
= 0;
602 struct drm_crtc
*tmp
;
604 drm_for_each_crtc(tmp
, dev
) {
612 * drm_crtc_init_with_planes - Initialise a new CRTC object with
613 * specified primary and cursor planes.
615 * @crtc: CRTC object to init
616 * @primary: Primary plane for CRTC
617 * @cursor: Cursor plane for CRTC
618 * @funcs: callbacks for the new CRTC
619 * @name: printf style format string for the CRTC name, or NULL for default name
621 * Inits a new object created as base part of a driver crtc object.
624 * Zero on success, error code on failure.
626 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
627 struct drm_plane
*primary
,
628 struct drm_plane
*cursor
,
629 const struct drm_crtc_funcs
*funcs
,
630 const char *name
, ...)
632 struct drm_mode_config
*config
= &dev
->mode_config
;
635 WARN_ON(primary
&& primary
->type
!= DRM_PLANE_TYPE_PRIMARY
);
636 WARN_ON(cursor
&& cursor
->type
!= DRM_PLANE_TYPE_CURSOR
);
641 INIT_LIST_HEAD(&crtc
->commit_list
);
642 spin_lock_init(&crtc
->commit_lock
);
644 drm_modeset_lock_init(&crtc
->mutex
);
645 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
653 crtc
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
656 crtc
->name
= kasprintf(GFP_KERNEL
, "crtc-%d",
660 drm_mode_object_unregister(dev
, &crtc
->base
);
664 crtc
->base
.properties
= &crtc
->properties
;
666 list_add_tail(&crtc
->head
, &config
->crtc_list
);
667 crtc
->index
= config
->num_crtc
++;
669 crtc
->primary
= primary
;
670 crtc
->cursor
= cursor
;
672 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
674 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
676 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
677 drm_object_attach_property(&crtc
->base
, config
->prop_active
, 0);
678 drm_object_attach_property(&crtc
->base
, config
->prop_mode_id
, 0);
683 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
686 * drm_crtc_cleanup - Clean up the core crtc usage
687 * @crtc: CRTC to cleanup
689 * This function cleans up @crtc and removes it from the DRM mode setting
690 * core. Note that the function does *not* free the crtc structure itself,
691 * this is the responsibility of the caller.
693 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
695 struct drm_device
*dev
= crtc
->dev
;
697 /* Note that the crtc_list is considered to be static; should we
698 * remove the drm_crtc at runtime we would have to decrement all
699 * the indices on the drm_crtc after us in the crtc_list.
702 kfree(crtc
->gamma_store
);
703 crtc
->gamma_store
= NULL
;
705 drm_modeset_lock_fini(&crtc
->mutex
);
707 drm_mode_object_unregister(dev
, &crtc
->base
);
708 list_del(&crtc
->head
);
709 dev
->mode_config
.num_crtc
--;
711 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
712 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
713 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
717 memset(crtc
, 0, sizeof(*crtc
));
719 EXPORT_SYMBOL(drm_crtc_cleanup
);
722 * drm_mode_remove - remove and free a mode
723 * @connector: connector list to modify
724 * @mode: mode to remove
726 * Remove @mode from @connector's mode list, then free it.
728 static void drm_mode_remove(struct drm_connector
*connector
,
729 struct drm_display_mode
*mode
)
731 list_del(&mode
->head
);
732 drm_mode_destroy(connector
->dev
, mode
);
736 * drm_display_info_set_bus_formats - set the supported bus formats
737 * @info: display info to store bus formats in
738 * @formats: array containing the supported bus formats
739 * @num_formats: the number of entries in the fmts array
741 * Store the supported bus formats in display info structure.
742 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
743 * a full list of available formats.
745 int drm_display_info_set_bus_formats(struct drm_display_info
*info
,
747 unsigned int num_formats
)
751 if (!formats
&& num_formats
)
754 if (formats
&& num_formats
) {
755 fmts
= kmemdup(formats
, sizeof(*formats
) * num_formats
,
761 kfree(info
->bus_formats
);
762 info
->bus_formats
= fmts
;
763 info
->num_bus_formats
= num_formats
;
767 EXPORT_SYMBOL(drm_display_info_set_bus_formats
);
770 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
771 * @connector: connector to quwery
773 * The kernel supports per-connector configration of its consoles through
774 * use of the video= parameter. This function parses that option and
775 * extracts the user's specified mode (or enable/disable status) for a
776 * particular connector. This is typically only used during the early fbdev
779 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
781 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
784 if (fb_get_options(connector
->name
, &option
))
787 if (!drm_mode_parse_command_line_for_connector(option
,
795 switch (mode
->force
) {
799 case DRM_FORCE_ON_DIGITAL
:
808 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
809 connector
->force
= mode
->force
;
812 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
814 mode
->xres
, mode
->yres
,
815 mode
->refresh_specified
? mode
->refresh
: 60,
816 mode
->rb
? " reduced blanking" : "",
817 mode
->margins
? " with margins" : "",
818 mode
->interlace
? " interlaced" : "");
821 static void drm_connector_free(struct kref
*kref
)
823 struct drm_connector
*connector
=
824 container_of(kref
, struct drm_connector
, base
.refcount
);
825 struct drm_device
*dev
= connector
->dev
;
827 drm_mode_object_unregister(dev
, &connector
->base
);
828 connector
->funcs
->destroy(connector
);
832 * drm_connector_init - Init a preallocated connector
834 * @connector: the connector to init
835 * @funcs: callbacks for this connector
836 * @connector_type: user visible type of the connector
838 * Initialises a preallocated connector. Connectors should be
839 * subclassed as part of driver connector objects.
842 * Zero on success, error code on failure.
844 int drm_connector_init(struct drm_device
*dev
,
845 struct drm_connector
*connector
,
846 const struct drm_connector_funcs
*funcs
,
849 struct drm_mode_config
*config
= &dev
->mode_config
;
851 struct ida
*connector_ida
=
852 &drm_connector_enum_list
[connector_type
].ida
;
854 drm_modeset_lock_all(dev
);
856 ret
= drm_mode_object_get_reg(dev
, &connector
->base
,
857 DRM_MODE_OBJECT_CONNECTOR
,
858 false, drm_connector_free
);
862 connector
->base
.properties
= &connector
->properties
;
863 connector
->dev
= dev
;
864 connector
->funcs
= funcs
;
866 connector
->connector_id
= ida_simple_get(&config
->connector_ida
, 0, 0, GFP_KERNEL
);
867 if (connector
->connector_id
< 0) {
868 ret
= connector
->connector_id
;
872 connector
->connector_type
= connector_type
;
873 connector
->connector_type_id
=
874 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
875 if (connector
->connector_type_id
< 0) {
876 ret
= connector
->connector_type_id
;
880 kasprintf(GFP_KERNEL
, "%s-%d",
881 drm_connector_enum_list
[connector_type
].name
,
882 connector
->connector_type_id
);
883 if (!connector
->name
) {
885 goto out_put_type_id
;
888 INIT_LIST_HEAD(&connector
->probed_modes
);
889 INIT_LIST_HEAD(&connector
->modes
);
890 connector
->edid_blob_ptr
= NULL
;
891 connector
->status
= connector_status_unknown
;
893 drm_connector_get_cmdline_mode(connector
);
895 /* We should add connectors at the end to avoid upsetting the connector
897 list_add_tail(&connector
->head
, &config
->connector_list
);
898 config
->num_connector
++;
900 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
901 drm_object_attach_property(&connector
->base
,
902 config
->edid_property
,
905 drm_object_attach_property(&connector
->base
,
906 config
->dpms_property
, 0);
908 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
909 drm_object_attach_property(&connector
->base
, config
->prop_crtc_id
, 0);
912 connector
->debugfs_entry
= NULL
;
915 ida_remove(connector_ida
, connector
->connector_type_id
);
918 ida_remove(&config
->connector_ida
, connector
->connector_id
);
921 drm_mode_object_unregister(dev
, &connector
->base
);
924 drm_modeset_unlock_all(dev
);
928 EXPORT_SYMBOL(drm_connector_init
);
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
934 * Cleans up the connector but doesn't free the object.
936 void drm_connector_cleanup(struct drm_connector
*connector
)
938 struct drm_device
*dev
= connector
->dev
;
939 struct drm_display_mode
*mode
, *t
;
941 if (connector
->tile_group
) {
942 drm_mode_put_tile_group(dev
, connector
->tile_group
);
943 connector
->tile_group
= NULL
;
946 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
947 drm_mode_remove(connector
, mode
);
949 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
950 drm_mode_remove(connector
, mode
);
952 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
953 connector
->connector_type_id
);
955 ida_remove(&dev
->mode_config
.connector_ida
,
956 connector
->connector_id
);
958 kfree(connector
->display_info
.bus_formats
);
959 drm_mode_object_unregister(dev
, &connector
->base
);
960 kfree(connector
->name
);
961 connector
->name
= NULL
;
962 list_del(&connector
->head
);
963 dev
->mode_config
.num_connector
--;
965 WARN_ON(connector
->state
&& !connector
->funcs
->atomic_destroy_state
);
966 if (connector
->state
&& connector
->funcs
->atomic_destroy_state
)
967 connector
->funcs
->atomic_destroy_state(connector
,
970 memset(connector
, 0, sizeof(*connector
));
972 EXPORT_SYMBOL(drm_connector_cleanup
);
975 * drm_connector_register - register a connector
976 * @connector: the connector to register
978 * Register userspace interfaces for a connector
981 * Zero on success, error code on failure.
983 int drm_connector_register(struct drm_connector
*connector
)
987 ret
= drm_sysfs_connector_add(connector
);
991 ret
= drm_debugfs_connector_add(connector
);
993 drm_sysfs_connector_remove(connector
);
997 drm_mode_object_register(connector
->dev
, &connector
->base
);
1001 EXPORT_SYMBOL(drm_connector_register
);
1004 * drm_connector_unregister - unregister a connector
1005 * @connector: the connector to unregister
1007 * Unregister userspace interfaces for a connector
1009 void drm_connector_unregister(struct drm_connector
*connector
)
1011 drm_sysfs_connector_remove(connector
);
1012 drm_debugfs_connector_remove(connector
);
1014 EXPORT_SYMBOL(drm_connector_unregister
);
1017 * drm_connector_register_all - register all connectors
1020 * This function registers all connectors in sysfs and other places so that
1021 * userspace can start to access them. Drivers can call it after calling
1022 * drm_dev_register() to complete the device registration, if they don't call
1023 * drm_connector_register() on each connector individually.
1025 * When a device is unplugged and should be removed from userspace access,
1026 * call drm_connector_unregister_all(), which is the inverse of this
1030 * Zero on success, error code on failure.
1032 int drm_connector_register_all(struct drm_device
*dev
)
1034 struct drm_connector
*connector
;
1037 mutex_lock(&dev
->mode_config
.mutex
);
1039 drm_for_each_connector(connector
, dev
) {
1040 ret
= drm_connector_register(connector
);
1045 mutex_unlock(&dev
->mode_config
.mutex
);
1050 mutex_unlock(&dev
->mode_config
.mutex
);
1051 drm_connector_unregister_all(dev
);
1054 EXPORT_SYMBOL(drm_connector_register_all
);
1057 * drm_connector_unregister_all - unregister connector userspace interfaces
1060 * This functions unregisters all connectors from sysfs and other places so
1061 * that userspace can no longer access them. Drivers should call this as the
1062 * first step tearing down the device instace, or when the underlying
1063 * physical device disappeared (e.g. USB unplug), right before calling
1064 * drm_dev_unregister().
1066 void drm_connector_unregister_all(struct drm_device
*dev
)
1068 struct drm_connector
*connector
;
1070 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1071 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1072 drm_connector_unregister(connector
);
1074 EXPORT_SYMBOL(drm_connector_unregister_all
);
1077 * drm_encoder_init - Init a preallocated encoder
1079 * @encoder: the encoder to init
1080 * @funcs: callbacks for this encoder
1081 * @encoder_type: user visible type of the encoder
1082 * @name: printf style format string for the encoder name, or NULL for default name
1084 * Initialises a preallocated encoder. Encoder should be
1085 * subclassed as part of driver encoder objects.
1088 * Zero on success, error code on failure.
1090 int drm_encoder_init(struct drm_device
*dev
,
1091 struct drm_encoder
*encoder
,
1092 const struct drm_encoder_funcs
*funcs
,
1093 int encoder_type
, const char *name
, ...)
1097 drm_modeset_lock_all(dev
);
1099 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1104 encoder
->encoder_type
= encoder_type
;
1105 encoder
->funcs
= funcs
;
1110 encoder
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1113 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1114 drm_encoder_enum_list
[encoder_type
].name
,
1117 if (!encoder
->name
) {
1122 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1123 encoder
->index
= dev
->mode_config
.num_encoder
++;
1127 drm_mode_object_unregister(dev
, &encoder
->base
);
1130 drm_modeset_unlock_all(dev
);
1134 EXPORT_SYMBOL(drm_encoder_init
);
1137 * drm_encoder_cleanup - cleans up an initialised encoder
1138 * @encoder: encoder to cleanup
1140 * Cleans up the encoder but doesn't free the object.
1142 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1144 struct drm_device
*dev
= encoder
->dev
;
1146 /* Note that the encoder_list is considered to be static; should we
1147 * remove the drm_encoder at runtime we would have to decrement all
1148 * the indices on the drm_encoder after us in the encoder_list.
1151 drm_modeset_lock_all(dev
);
1152 drm_mode_object_unregister(dev
, &encoder
->base
);
1153 kfree(encoder
->name
);
1154 list_del(&encoder
->head
);
1155 dev
->mode_config
.num_encoder
--;
1156 drm_modeset_unlock_all(dev
);
1158 memset(encoder
, 0, sizeof(*encoder
));
1160 EXPORT_SYMBOL(drm_encoder_cleanup
);
1162 static unsigned int drm_num_planes(struct drm_device
*dev
)
1164 unsigned int num
= 0;
1165 struct drm_plane
*tmp
;
1167 drm_for_each_plane(tmp
, dev
) {
1175 * drm_universal_plane_init - Initialize a new universal plane object
1177 * @plane: plane object to init
1178 * @possible_crtcs: bitmask of possible CRTCs
1179 * @funcs: callbacks for the new plane
1180 * @formats: array of supported formats (%DRM_FORMAT_*)
1181 * @format_count: number of elements in @formats
1182 * @type: type of plane (overlay, primary, cursor)
1183 * @name: printf style format string for the plane name, or NULL for default name
1185 * Initializes a plane object of type @type.
1188 * Zero on success, error code on failure.
1190 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1191 unsigned long possible_crtcs
,
1192 const struct drm_plane_funcs
*funcs
,
1193 const uint32_t *formats
, unsigned int format_count
,
1194 enum drm_plane_type type
,
1195 const char *name
, ...)
1197 struct drm_mode_config
*config
= &dev
->mode_config
;
1200 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1204 drm_modeset_lock_init(&plane
->mutex
);
1206 plane
->base
.properties
= &plane
->properties
;
1208 plane
->funcs
= funcs
;
1209 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
1211 if (!plane
->format_types
) {
1212 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1213 drm_mode_object_unregister(dev
, &plane
->base
);
1221 plane
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1224 plane
->name
= kasprintf(GFP_KERNEL
, "plane-%d",
1225 drm_num_planes(dev
));
1228 kfree(plane
->format_types
);
1229 drm_mode_object_unregister(dev
, &plane
->base
);
1233 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1234 plane
->format_count
= format_count
;
1235 plane
->possible_crtcs
= possible_crtcs
;
1238 list_add_tail(&plane
->head
, &config
->plane_list
);
1239 plane
->index
= config
->num_total_plane
++;
1240 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1241 config
->num_overlay_plane
++;
1243 drm_object_attach_property(&plane
->base
,
1244 config
->plane_type_property
,
1247 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
1248 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
1249 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
1250 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
1251 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
1252 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
1253 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
1254 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
1255 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
1256 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
1257 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
1262 EXPORT_SYMBOL(drm_universal_plane_init
);
1265 * drm_plane_init - Initialize a legacy plane
1267 * @plane: plane object to init
1268 * @possible_crtcs: bitmask of possible CRTCs
1269 * @funcs: callbacks for the new plane
1270 * @formats: array of supported formats (%DRM_FORMAT_*)
1271 * @format_count: number of elements in @formats
1272 * @is_primary: plane type (primary vs overlay)
1274 * Legacy API to initialize a DRM plane.
1276 * New drivers should call drm_universal_plane_init() instead.
1279 * Zero on success, error code on failure.
1281 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1282 unsigned long possible_crtcs
,
1283 const struct drm_plane_funcs
*funcs
,
1284 const uint32_t *formats
, unsigned int format_count
,
1287 enum drm_plane_type type
;
1289 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1290 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1291 formats
, format_count
, type
, NULL
);
1293 EXPORT_SYMBOL(drm_plane_init
);
1296 * drm_plane_cleanup - Clean up the core plane usage
1297 * @plane: plane to cleanup
1299 * This function cleans up @plane and removes it from the DRM mode setting
1300 * core. Note that the function does *not* free the plane structure itself,
1301 * this is the responsibility of the caller.
1303 void drm_plane_cleanup(struct drm_plane
*plane
)
1305 struct drm_device
*dev
= plane
->dev
;
1307 drm_modeset_lock_all(dev
);
1308 kfree(plane
->format_types
);
1309 drm_mode_object_unregister(dev
, &plane
->base
);
1311 BUG_ON(list_empty(&plane
->head
));
1313 /* Note that the plane_list is considered to be static; should we
1314 * remove the drm_plane at runtime we would have to decrement all
1315 * the indices on the drm_plane after us in the plane_list.
1318 list_del(&plane
->head
);
1319 dev
->mode_config
.num_total_plane
--;
1320 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1321 dev
->mode_config
.num_overlay_plane
--;
1322 drm_modeset_unlock_all(dev
);
1324 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
1325 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
1326 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1330 memset(plane
, 0, sizeof(*plane
));
1332 EXPORT_SYMBOL(drm_plane_cleanup
);
1335 * drm_plane_from_index - find the registered plane at an index
1337 * @idx: index of registered plane to find for
1339 * Given a plane index, return the registered plane from DRM device's
1340 * list of planes with matching index.
1343 drm_plane_from_index(struct drm_device
*dev
, int idx
)
1345 struct drm_plane
*plane
;
1347 drm_for_each_plane(plane
, dev
)
1348 if (idx
== plane
->index
)
1353 EXPORT_SYMBOL(drm_plane_from_index
);
1356 * drm_plane_force_disable - Forcibly disable a plane
1357 * @plane: plane to disable
1359 * Forces the plane to be disabled.
1361 * Used when the plane's current framebuffer is destroyed,
1362 * and when restoring fbdev mode.
1364 void drm_plane_force_disable(struct drm_plane
*plane
)
1371 plane
->old_fb
= plane
->fb
;
1372 ret
= plane
->funcs
->disable_plane(plane
);
1374 DRM_ERROR("failed to disable plane with busy fb\n");
1375 plane
->old_fb
= NULL
;
1378 /* disconnect the plane from the fb and crtc: */
1379 drm_framebuffer_unreference(plane
->old_fb
);
1380 plane
->old_fb
= NULL
;
1384 EXPORT_SYMBOL(drm_plane_force_disable
);
1386 static int drm_mode_create_standard_properties(struct drm_device
*dev
)
1388 struct drm_property
*prop
;
1391 * Standard properties (apply to all connectors)
1393 prop
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1394 DRM_MODE_PROP_IMMUTABLE
,
1398 dev
->mode_config
.edid_property
= prop
;
1400 prop
= drm_property_create_enum(dev
, 0,
1401 "DPMS", drm_dpms_enum_list
,
1402 ARRAY_SIZE(drm_dpms_enum_list
));
1405 dev
->mode_config
.dpms_property
= prop
;
1407 prop
= drm_property_create(dev
,
1408 DRM_MODE_PROP_BLOB
|
1409 DRM_MODE_PROP_IMMUTABLE
,
1413 dev
->mode_config
.path_property
= prop
;
1415 prop
= drm_property_create(dev
,
1416 DRM_MODE_PROP_BLOB
|
1417 DRM_MODE_PROP_IMMUTABLE
,
1421 dev
->mode_config
.tile_property
= prop
;
1423 prop
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1424 "type", drm_plane_type_enum_list
,
1425 ARRAY_SIZE(drm_plane_type_enum_list
));
1428 dev
->mode_config
.plane_type_property
= prop
;
1430 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1431 "SRC_X", 0, UINT_MAX
);
1434 dev
->mode_config
.prop_src_x
= prop
;
1436 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1437 "SRC_Y", 0, UINT_MAX
);
1440 dev
->mode_config
.prop_src_y
= prop
;
1442 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1443 "SRC_W", 0, UINT_MAX
);
1446 dev
->mode_config
.prop_src_w
= prop
;
1448 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1449 "SRC_H", 0, UINT_MAX
);
1452 dev
->mode_config
.prop_src_h
= prop
;
1454 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1455 "CRTC_X", INT_MIN
, INT_MAX
);
1458 dev
->mode_config
.prop_crtc_x
= prop
;
1460 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1461 "CRTC_Y", INT_MIN
, INT_MAX
);
1464 dev
->mode_config
.prop_crtc_y
= prop
;
1466 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1467 "CRTC_W", 0, INT_MAX
);
1470 dev
->mode_config
.prop_crtc_w
= prop
;
1472 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1473 "CRTC_H", 0, INT_MAX
);
1476 dev
->mode_config
.prop_crtc_h
= prop
;
1478 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1479 "FB_ID", DRM_MODE_OBJECT_FB
);
1482 dev
->mode_config
.prop_fb_id
= prop
;
1484 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1485 "CRTC_ID", DRM_MODE_OBJECT_CRTC
);
1488 dev
->mode_config
.prop_crtc_id
= prop
;
1490 prop
= drm_property_create_bool(dev
, DRM_MODE_PROP_ATOMIC
,
1494 dev
->mode_config
.prop_active
= prop
;
1496 prop
= drm_property_create(dev
,
1497 DRM_MODE_PROP_ATOMIC
| DRM_MODE_PROP_BLOB
,
1501 dev
->mode_config
.prop_mode_id
= prop
;
1503 prop
= drm_property_create(dev
,
1508 dev
->mode_config
.degamma_lut_property
= prop
;
1510 prop
= drm_property_create_range(dev
,
1511 DRM_MODE_PROP_IMMUTABLE
,
1512 "DEGAMMA_LUT_SIZE", 0, UINT_MAX
);
1515 dev
->mode_config
.degamma_lut_size_property
= prop
;
1517 prop
= drm_property_create(dev
,
1522 dev
->mode_config
.ctm_property
= prop
;
1524 prop
= drm_property_create(dev
,
1529 dev
->mode_config
.gamma_lut_property
= prop
;
1531 prop
= drm_property_create_range(dev
,
1532 DRM_MODE_PROP_IMMUTABLE
,
1533 "GAMMA_LUT_SIZE", 0, UINT_MAX
);
1536 dev
->mode_config
.gamma_lut_size_property
= prop
;
1542 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1545 * Called by a driver the first time a DVI-I connector is made.
1547 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1549 struct drm_property
*dvi_i_selector
;
1550 struct drm_property
*dvi_i_subconnector
;
1552 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1556 drm_property_create_enum(dev
, 0,
1557 "select subconnector",
1558 drm_dvi_i_select_enum_list
,
1559 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1560 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1562 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1564 drm_dvi_i_subconnector_enum_list
,
1565 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1566 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1570 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1573 * drm_create_tv_properties - create TV specific connector properties
1575 * @num_modes: number of different TV formats (modes) supported
1576 * @modes: array of pointers to strings containing name of each format
1578 * Called by a driver's TV initialization routine, this function creates
1579 * the TV specific connector properties for a given device. Caller is
1580 * responsible for allocating a list of format names and passing them to
1583 int drm_mode_create_tv_properties(struct drm_device
*dev
,
1584 unsigned int num_modes
,
1585 const char * const modes
[])
1587 struct drm_property
*tv_selector
;
1588 struct drm_property
*tv_subconnector
;
1591 if (dev
->mode_config
.tv_select_subconnector_property
)
1595 * Basic connector properties
1597 tv_selector
= drm_property_create_enum(dev
, 0,
1598 "select subconnector",
1599 drm_tv_select_enum_list
,
1600 ARRAY_SIZE(drm_tv_select_enum_list
));
1604 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1607 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1609 drm_tv_subconnector_enum_list
,
1610 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1611 if (!tv_subconnector
)
1613 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1616 * Other, TV specific properties: margins & TV modes.
1618 dev
->mode_config
.tv_left_margin_property
=
1619 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1620 if (!dev
->mode_config
.tv_left_margin_property
)
1623 dev
->mode_config
.tv_right_margin_property
=
1624 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1625 if (!dev
->mode_config
.tv_right_margin_property
)
1628 dev
->mode_config
.tv_top_margin_property
=
1629 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1630 if (!dev
->mode_config
.tv_top_margin_property
)
1633 dev
->mode_config
.tv_bottom_margin_property
=
1634 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1635 if (!dev
->mode_config
.tv_bottom_margin_property
)
1638 dev
->mode_config
.tv_mode_property
=
1639 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1641 if (!dev
->mode_config
.tv_mode_property
)
1644 for (i
= 0; i
< num_modes
; i
++)
1645 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1648 dev
->mode_config
.tv_brightness_property
=
1649 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1650 if (!dev
->mode_config
.tv_brightness_property
)
1653 dev
->mode_config
.tv_contrast_property
=
1654 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1655 if (!dev
->mode_config
.tv_contrast_property
)
1658 dev
->mode_config
.tv_flicker_reduction_property
=
1659 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1660 if (!dev
->mode_config
.tv_flicker_reduction_property
)
1663 dev
->mode_config
.tv_overscan_property
=
1664 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1665 if (!dev
->mode_config
.tv_overscan_property
)
1668 dev
->mode_config
.tv_saturation_property
=
1669 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1670 if (!dev
->mode_config
.tv_saturation_property
)
1673 dev
->mode_config
.tv_hue_property
=
1674 drm_property_create_range(dev
, 0, "hue", 0, 100);
1675 if (!dev
->mode_config
.tv_hue_property
)
1682 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1685 * drm_mode_create_scaling_mode_property - create scaling mode property
1688 * Called by a driver the first time it's needed, must be attached to desired
1691 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1693 struct drm_property
*scaling_mode
;
1695 if (dev
->mode_config
.scaling_mode_property
)
1699 drm_property_create_enum(dev
, 0, "scaling mode",
1700 drm_scaling_mode_enum_list
,
1701 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1703 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1707 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1710 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1713 * Called by a driver the first time it's needed, must be attached to desired
1717 * Zero on success, negative errno on failure.
1719 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1721 if (dev
->mode_config
.aspect_ratio_property
)
1724 dev
->mode_config
.aspect_ratio_property
=
1725 drm_property_create_enum(dev
, 0, "aspect ratio",
1726 drm_aspect_ratio_enum_list
,
1727 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1729 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1734 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1737 * drm_mode_create_dirty_property - create dirty property
1740 * Called by a driver the first time it's needed, must be attached to desired
1743 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1745 struct drm_property
*dirty_info
;
1747 if (dev
->mode_config
.dirty_info_property
)
1751 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1753 drm_dirty_info_enum_list
,
1754 ARRAY_SIZE(drm_dirty_info_enum_list
));
1755 dev
->mode_config
.dirty_info_property
= dirty_info
;
1759 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1762 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1765 * Create the the suggested x/y offset property for connectors.
1767 int drm_mode_create_suggested_offset_properties(struct drm_device
*dev
)
1769 if (dev
->mode_config
.suggested_x_property
&& dev
->mode_config
.suggested_y_property
)
1772 dev
->mode_config
.suggested_x_property
=
1773 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested X", 0, 0xffffffff);
1775 dev
->mode_config
.suggested_y_property
=
1776 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested Y", 0, 0xffffffff);
1778 if (dev
->mode_config
.suggested_x_property
== NULL
||
1779 dev
->mode_config
.suggested_y_property
== NULL
)
1783 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties
);
1786 * drm_mode_getresources - get graphics configuration
1787 * @dev: drm device for the ioctl
1788 * @data: data pointer for the ioctl
1789 * @file_priv: drm file for the ioctl call
1791 * Construct a set of configuration description structures and return
1792 * them to the user, including CRTC, connector and framebuffer configuration.
1794 * Called by the user via ioctl.
1797 * Zero on success, negative errno on failure.
1799 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1800 struct drm_file
*file_priv
)
1802 struct drm_mode_card_res
*card_res
= data
;
1803 struct list_head
*lh
;
1804 struct drm_framebuffer
*fb
;
1805 struct drm_connector
*connector
;
1806 struct drm_crtc
*crtc
;
1807 struct drm_encoder
*encoder
;
1809 int connector_count
= 0;
1812 int encoder_count
= 0;
1814 uint32_t __user
*fb_id
;
1815 uint32_t __user
*crtc_id
;
1816 uint32_t __user
*connector_id
;
1817 uint32_t __user
*encoder_id
;
1819 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1823 mutex_lock(&file_priv
->fbs_lock
);
1825 * For the non-control nodes we need to limit the list of resources
1826 * by IDs in the group list for this node
1828 list_for_each(lh
, &file_priv
->fbs
)
1831 /* handle this in 4 parts */
1833 if (card_res
->count_fbs
>= fb_count
) {
1835 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1836 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1837 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1838 mutex_unlock(&file_priv
->fbs_lock
);
1844 card_res
->count_fbs
= fb_count
;
1845 mutex_unlock(&file_priv
->fbs_lock
);
1847 /* mode_config.mutex protects the connector list against e.g. DP MST
1848 * connector hot-adding. CRTC/Plane lists are invariant. */
1849 mutex_lock(&dev
->mode_config
.mutex
);
1850 drm_for_each_crtc(crtc
, dev
)
1853 drm_for_each_connector(connector
, dev
)
1856 drm_for_each_encoder(encoder
, dev
)
1859 card_res
->max_height
= dev
->mode_config
.max_height
;
1860 card_res
->min_height
= dev
->mode_config
.min_height
;
1861 card_res
->max_width
= dev
->mode_config
.max_width
;
1862 card_res
->min_width
= dev
->mode_config
.min_width
;
1865 if (card_res
->count_crtcs
>= crtc_count
) {
1867 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1868 drm_for_each_crtc(crtc
, dev
) {
1869 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1876 card_res
->count_crtcs
= crtc_count
;
1879 if (card_res
->count_encoders
>= encoder_count
) {
1881 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1882 drm_for_each_encoder(encoder
, dev
) {
1883 if (put_user(encoder
->base
.id
, encoder_id
+
1891 card_res
->count_encoders
= encoder_count
;
1894 if (card_res
->count_connectors
>= connector_count
) {
1896 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1897 drm_for_each_connector(connector
, dev
) {
1898 if (put_user(connector
->base
.id
,
1899 connector_id
+ copied
)) {
1906 card_res
->count_connectors
= connector_count
;
1909 mutex_unlock(&dev
->mode_config
.mutex
);
1914 * drm_mode_getcrtc - get CRTC configuration
1915 * @dev: drm device for the ioctl
1916 * @data: data pointer for the ioctl
1917 * @file_priv: drm file for the ioctl call
1919 * Construct a CRTC configuration structure to return to the user.
1921 * Called by the user via ioctl.
1924 * Zero on success, negative errno on failure.
1926 int drm_mode_getcrtc(struct drm_device
*dev
,
1927 void *data
, struct drm_file
*file_priv
)
1929 struct drm_mode_crtc
*crtc_resp
= data
;
1930 struct drm_crtc
*crtc
;
1932 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1935 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1939 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
1940 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1941 if (crtc
->primary
->fb
)
1942 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1944 crtc_resp
->fb_id
= 0;
1947 crtc_resp
->x
= crtc
->primary
->state
->src_x
>> 16;
1948 crtc_resp
->y
= crtc
->primary
->state
->src_y
>> 16;
1949 if (crtc
->state
->enable
) {
1950 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->state
->mode
);
1951 crtc_resp
->mode_valid
= 1;
1954 crtc_resp
->mode_valid
= 0;
1957 crtc_resp
->x
= crtc
->x
;
1958 crtc_resp
->y
= crtc
->y
;
1959 if (crtc
->enabled
) {
1960 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1961 crtc_resp
->mode_valid
= 1;
1964 crtc_resp
->mode_valid
= 0;
1967 drm_modeset_unlock_crtc(crtc
);
1972 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1973 const struct drm_file
*file_priv
)
1976 * If user-space hasn't configured the driver to expose the stereo 3D
1977 * modes, don't expose them.
1979 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1985 static struct drm_encoder
*drm_connector_get_encoder(struct drm_connector
*connector
)
1987 /* For atomic drivers only state objects are synchronously updated and
1988 * protected by modeset locks, so check those first. */
1989 if (connector
->state
)
1990 return connector
->state
->best_encoder
;
1991 return connector
->encoder
;
1994 /* helper for getconnector and getproperties ioctls */
1995 static int get_properties(struct drm_mode_object
*obj
, bool atomic
,
1996 uint32_t __user
*prop_ptr
, uint64_t __user
*prop_values
,
1997 uint32_t *arg_count_props
)
2002 props_count
= obj
->properties
->count
;
2004 props_count
-= obj
->properties
->atomic_count
;
2006 if ((*arg_count_props
>= props_count
) && props_count
) {
2007 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
2008 struct drm_property
*prop
= obj
->properties
->properties
[i
];
2011 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
2014 ret
= drm_object_property_get_value(obj
, prop
, &val
);
2018 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
2021 if (put_user(val
, prop_values
+ copied
))
2027 *arg_count_props
= props_count
;
2033 * drm_mode_getconnector - get connector configuration
2034 * @dev: drm device for the ioctl
2035 * @data: data pointer for the ioctl
2036 * @file_priv: drm file for the ioctl call
2038 * Construct a connector configuration structure to return to the user.
2040 * Called by the user via ioctl.
2043 * Zero on success, negative errno on failure.
2045 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
2046 struct drm_file
*file_priv
)
2048 struct drm_mode_get_connector
*out_resp
= data
;
2049 struct drm_connector
*connector
;
2050 struct drm_encoder
*encoder
;
2051 struct drm_display_mode
*mode
;
2053 int encoders_count
= 0;
2057 struct drm_mode_modeinfo u_mode
;
2058 struct drm_mode_modeinfo __user
*mode_ptr
;
2059 uint32_t __user
*encoder_ptr
;
2061 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2064 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
2066 mutex_lock(&dev
->mode_config
.mutex
);
2068 connector
= drm_connector_lookup(dev
, out_resp
->connector_id
);
2074 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++)
2075 if (connector
->encoder_ids
[i
] != 0)
2078 if (out_resp
->count_modes
== 0) {
2079 connector
->funcs
->fill_modes(connector
,
2080 dev
->mode_config
.max_width
,
2081 dev
->mode_config
.max_height
);
2084 /* delayed so we get modes regardless of pre-fill_modes state */
2085 list_for_each_entry(mode
, &connector
->modes
, head
)
2086 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2089 out_resp
->connector_id
= connector
->base
.id
;
2090 out_resp
->connector_type
= connector
->connector_type
;
2091 out_resp
->connector_type_id
= connector
->connector_type_id
;
2092 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2093 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2094 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2095 out_resp
->connection
= connector
->status
;
2097 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2098 encoder
= drm_connector_get_encoder(connector
);
2100 out_resp
->encoder_id
= encoder
->base
.id
;
2102 out_resp
->encoder_id
= 0;
2105 * This ioctl is called twice, once to determine how much space is
2106 * needed, and the 2nd time to fill it.
2108 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2110 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2111 list_for_each_entry(mode
, &connector
->modes
, head
) {
2112 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2115 drm_mode_convert_to_umode(&u_mode
, mode
);
2116 if (copy_to_user(mode_ptr
+ copied
,
2117 &u_mode
, sizeof(u_mode
))) {
2124 out_resp
->count_modes
= mode_count
;
2126 ret
= get_properties(&connector
->base
, file_priv
->atomic
,
2127 (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
),
2128 (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
),
2129 &out_resp
->count_props
);
2133 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2135 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2136 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2137 if (connector
->encoder_ids
[i
] != 0) {
2138 if (put_user(connector
->encoder_ids
[i
],
2139 encoder_ptr
+ copied
)) {
2147 out_resp
->count_encoders
= encoders_count
;
2150 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2152 drm_connector_unreference(connector
);
2154 mutex_unlock(&dev
->mode_config
.mutex
);
2159 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
2161 struct drm_connector
*connector
;
2162 struct drm_device
*dev
= encoder
->dev
;
2163 bool uses_atomic
= false;
2165 /* For atomic drivers only state objects are synchronously updated and
2166 * protected by modeset locks, so check those first. */
2167 drm_for_each_connector(connector
, dev
) {
2168 if (!connector
->state
)
2173 if (connector
->state
->best_encoder
!= encoder
)
2176 return connector
->state
->crtc
;
2179 /* Don't return stale data (e.g. pending async disable). */
2183 return encoder
->crtc
;
2187 * drm_mode_getencoder - get encoder configuration
2188 * @dev: drm device for the ioctl
2189 * @data: data pointer for the ioctl
2190 * @file_priv: drm file for the ioctl call
2192 * Construct a encoder configuration structure to return to the user.
2194 * Called by the user via ioctl.
2197 * Zero on success, negative errno on failure.
2199 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2200 struct drm_file
*file_priv
)
2202 struct drm_mode_get_encoder
*enc_resp
= data
;
2203 struct drm_encoder
*encoder
;
2204 struct drm_crtc
*crtc
;
2206 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2209 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2213 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2214 crtc
= drm_encoder_get_crtc(encoder
);
2216 enc_resp
->crtc_id
= crtc
->base
.id
;
2218 enc_resp
->crtc_id
= 0;
2219 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2221 enc_resp
->encoder_type
= encoder
->encoder_type
;
2222 enc_resp
->encoder_id
= encoder
->base
.id
;
2223 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2224 enc_resp
->possible_clones
= encoder
->possible_clones
;
2230 * drm_mode_getplane_res - enumerate all plane resources
2233 * @file_priv: DRM file info
2235 * Construct a list of plane ids to return to the user.
2237 * Called by the user via ioctl.
2240 * Zero on success, negative errno on failure.
2242 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2243 struct drm_file
*file_priv
)
2245 struct drm_mode_get_plane_res
*plane_resp
= data
;
2246 struct drm_mode_config
*config
;
2247 struct drm_plane
*plane
;
2248 uint32_t __user
*plane_ptr
;
2250 unsigned num_planes
;
2252 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2255 config
= &dev
->mode_config
;
2257 if (file_priv
->universal_planes
)
2258 num_planes
= config
->num_total_plane
;
2260 num_planes
= config
->num_overlay_plane
;
2263 * This ioctl is called twice, once to determine how much space is
2264 * needed, and the 2nd time to fill it.
2267 (plane_resp
->count_planes
>= num_planes
)) {
2268 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2270 /* Plane lists are invariant, no locking needed. */
2271 drm_for_each_plane(plane
, dev
) {
2273 * Unless userspace set the 'universal planes'
2274 * capability bit, only advertise overlays.
2276 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2277 !file_priv
->universal_planes
)
2280 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
2285 plane_resp
->count_planes
= num_planes
;
2291 * drm_mode_getplane - get plane configuration
2294 * @file_priv: DRM file info
2296 * Construct a plane configuration structure to return to the user.
2298 * Called by the user via ioctl.
2301 * Zero on success, negative errno on failure.
2303 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2304 struct drm_file
*file_priv
)
2306 struct drm_mode_get_plane
*plane_resp
= data
;
2307 struct drm_plane
*plane
;
2308 uint32_t __user
*format_ptr
;
2310 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2313 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2317 drm_modeset_lock(&plane
->mutex
, NULL
);
2319 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2321 plane_resp
->crtc_id
= 0;
2324 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2326 plane_resp
->fb_id
= 0;
2327 drm_modeset_unlock(&plane
->mutex
);
2329 plane_resp
->plane_id
= plane
->base
.id
;
2330 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2331 plane_resp
->gamma_size
= 0;
2334 * This ioctl is called twice, once to determine how much space is
2335 * needed, and the 2nd time to fill it.
2337 if (plane
->format_count
&&
2338 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2339 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2340 if (copy_to_user(format_ptr
,
2341 plane
->format_types
,
2342 sizeof(uint32_t) * plane
->format_count
)) {
2346 plane_resp
->count_format_types
= plane
->format_count
;
2352 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2353 * @plane: plane to check for format support
2354 * @format: the pixel format
2357 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2360 int drm_plane_check_pixel_format(const struct drm_plane
*plane
, u32 format
)
2364 for (i
= 0; i
< plane
->format_count
; i
++) {
2365 if (format
== plane
->format_types
[i
])
2372 static int check_src_coords(uint32_t src_x
, uint32_t src_y
,
2373 uint32_t src_w
, uint32_t src_h
,
2374 const struct drm_framebuffer
*fb
)
2376 unsigned int fb_width
, fb_height
;
2378 fb_width
= fb
->width
<< 16;
2379 fb_height
= fb
->height
<< 16;
2381 /* Make sure source coordinates are inside the fb. */
2382 if (src_w
> fb_width
||
2383 src_x
> fb_width
- src_w
||
2384 src_h
> fb_height
||
2385 src_y
> fb_height
- src_h
) {
2386 DRM_DEBUG_KMS("Invalid source coordinates "
2387 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2388 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2389 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2390 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2391 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2399 * setplane_internal - setplane handler for internal callers
2401 * Note that we assume an extra reference has already been taken on fb. If the
2402 * update fails, this reference will be dropped before return; if it succeeds,
2403 * the previous framebuffer (if any) will be unreferenced instead.
2405 * src_{x,y,w,h} are provided in 16.16 fixed point format
2407 static int __setplane_internal(struct drm_plane
*plane
,
2408 struct drm_crtc
*crtc
,
2409 struct drm_framebuffer
*fb
,
2410 int32_t crtc_x
, int32_t crtc_y
,
2411 uint32_t crtc_w
, uint32_t crtc_h
,
2412 /* src_{x,y,w,h} values are 16.16 fixed point */
2413 uint32_t src_x
, uint32_t src_y
,
2414 uint32_t src_w
, uint32_t src_h
)
2418 /* No fb means shut it down */
2420 plane
->old_fb
= plane
->fb
;
2421 ret
= plane
->funcs
->disable_plane(plane
);
2426 plane
->old_fb
= NULL
;
2431 /* Check whether this plane is usable on this CRTC */
2432 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2433 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2438 /* Check whether this plane supports the fb pixel format. */
2439 ret
= drm_plane_check_pixel_format(plane
, fb
->pixel_format
);
2441 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2442 drm_get_format_name(fb
->pixel_format
));
2446 /* Give drivers some help against integer overflows */
2447 if (crtc_w
> INT_MAX
||
2448 crtc_x
> INT_MAX
- (int32_t) crtc_w
||
2450 crtc_y
> INT_MAX
- (int32_t) crtc_h
) {
2451 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2452 crtc_w
, crtc_h
, crtc_x
, crtc_y
);
2457 ret
= check_src_coords(src_x
, src_y
, src_w
, src_h
, fb
);
2461 plane
->old_fb
= plane
->fb
;
2462 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2463 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2464 src_x
, src_y
, src_w
, src_h
);
2470 plane
->old_fb
= NULL
;
2475 drm_framebuffer_unreference(fb
);
2477 drm_framebuffer_unreference(plane
->old_fb
);
2478 plane
->old_fb
= NULL
;
2483 static int setplane_internal(struct drm_plane
*plane
,
2484 struct drm_crtc
*crtc
,
2485 struct drm_framebuffer
*fb
,
2486 int32_t crtc_x
, int32_t crtc_y
,
2487 uint32_t crtc_w
, uint32_t crtc_h
,
2488 /* src_{x,y,w,h} values are 16.16 fixed point */
2489 uint32_t src_x
, uint32_t src_y
,
2490 uint32_t src_w
, uint32_t src_h
)
2494 drm_modeset_lock_all(plane
->dev
);
2495 ret
= __setplane_internal(plane
, crtc
, fb
,
2496 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2497 src_x
, src_y
, src_w
, src_h
);
2498 drm_modeset_unlock_all(plane
->dev
);
2504 * drm_mode_setplane - configure a plane's configuration
2506 * @data: ioctl data*
2507 * @file_priv: DRM file info
2509 * Set plane configuration, including placement, fb, scaling, and other factors.
2510 * Or pass a NULL fb to disable (planes may be disabled without providing a
2514 * Zero on success, negative errno on failure.
2516 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2517 struct drm_file
*file_priv
)
2519 struct drm_mode_set_plane
*plane_req
= data
;
2520 struct drm_plane
*plane
;
2521 struct drm_crtc
*crtc
= NULL
;
2522 struct drm_framebuffer
*fb
= NULL
;
2524 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2528 * First, find the plane, crtc, and fb objects. If not available,
2529 * we don't bother to call the driver.
2531 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2533 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2534 plane_req
->plane_id
);
2538 if (plane_req
->fb_id
) {
2539 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2541 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2546 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2548 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2549 plane_req
->crtc_id
);
2555 * setplane_internal will take care of deref'ing either the old or new
2556 * framebuffer depending on success.
2558 return setplane_internal(plane
, crtc
, fb
,
2559 plane_req
->crtc_x
, plane_req
->crtc_y
,
2560 plane_req
->crtc_w
, plane_req
->crtc_h
,
2561 plane_req
->src_x
, plane_req
->src_y
,
2562 plane_req
->src_w
, plane_req
->src_h
);
2566 * drm_mode_set_config_internal - helper to call ->set_config
2567 * @set: modeset config to set
2569 * This is a little helper to wrap internal calls to the ->set_config driver
2570 * interface. The only thing it adds is correct refcounting dance.
2573 * Zero on success, negative errno on failure.
2575 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2577 struct drm_crtc
*crtc
= set
->crtc
;
2578 struct drm_framebuffer
*fb
;
2579 struct drm_crtc
*tmp
;
2583 * NOTE: ->set_config can also disable other crtcs (if we steal all
2584 * connectors from it), hence we need to refcount the fbs across all
2585 * crtcs. Atomic modeset will have saner semantics ...
2587 drm_for_each_crtc(tmp
, crtc
->dev
)
2588 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2592 ret
= crtc
->funcs
->set_config(set
);
2594 crtc
->primary
->crtc
= crtc
;
2595 crtc
->primary
->fb
= fb
;
2598 drm_for_each_crtc(tmp
, crtc
->dev
) {
2599 if (tmp
->primary
->fb
)
2600 drm_framebuffer_reference(tmp
->primary
->fb
);
2601 if (tmp
->primary
->old_fb
)
2602 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2603 tmp
->primary
->old_fb
= NULL
;
2608 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2611 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2612 * @mode: mode to query
2613 * @hdisplay: hdisplay value to fill in
2614 * @vdisplay: vdisplay value to fill in
2616 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2617 * the appropriate layout.
2619 void drm_crtc_get_hv_timing(const struct drm_display_mode
*mode
,
2620 int *hdisplay
, int *vdisplay
)
2622 struct drm_display_mode adjusted
;
2624 drm_mode_copy(&adjusted
, mode
);
2625 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE_ONLY
);
2626 *hdisplay
= adjusted
.crtc_hdisplay
;
2627 *vdisplay
= adjusted
.crtc_vdisplay
;
2629 EXPORT_SYMBOL(drm_crtc_get_hv_timing
);
2632 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2634 * @crtc: CRTC that framebuffer will be displayed on
2637 * @mode: mode that framebuffer will be displayed under
2638 * @fb: framebuffer to check size of
2640 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2642 const struct drm_display_mode
*mode
,
2643 const struct drm_framebuffer
*fb
)
2646 int hdisplay
, vdisplay
;
2648 drm_crtc_get_hv_timing(mode
, &hdisplay
, &vdisplay
);
2651 crtc
->primary
->state
->rotation
& (BIT(DRM_ROTATE_90
) |
2652 BIT(DRM_ROTATE_270
)))
2653 swap(hdisplay
, vdisplay
);
2655 return check_src_coords(x
<< 16, y
<< 16,
2656 hdisplay
<< 16, vdisplay
<< 16, fb
);
2658 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2661 * drm_mode_setcrtc - set CRTC configuration
2662 * @dev: drm device for the ioctl
2663 * @data: data pointer for the ioctl
2664 * @file_priv: drm file for the ioctl call
2666 * Build a new CRTC configuration based on user request.
2668 * Called by the user via ioctl.
2671 * Zero on success, negative errno on failure.
2673 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2674 struct drm_file
*file_priv
)
2676 struct drm_mode_config
*config
= &dev
->mode_config
;
2677 struct drm_mode_crtc
*crtc_req
= data
;
2678 struct drm_crtc
*crtc
;
2679 struct drm_connector
**connector_set
= NULL
, *connector
;
2680 struct drm_framebuffer
*fb
= NULL
;
2681 struct drm_display_mode
*mode
= NULL
;
2682 struct drm_mode_set set
;
2683 uint32_t __user
*set_connectors_ptr
;
2687 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2691 * Universal plane src offsets are only 16.16, prevent havoc for
2692 * drivers using universal plane code internally.
2694 if (crtc_req
->x
& 0xffff0000 || crtc_req
->y
& 0xffff0000)
2697 drm_modeset_lock_all(dev
);
2698 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2700 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2704 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc
->base
.id
, crtc
->name
);
2706 if (crtc_req
->mode_valid
) {
2707 /* If we have a mode we need a framebuffer. */
2708 /* If we pass -1, set the mode with the currently bound fb */
2709 if (crtc_req
->fb_id
== -1) {
2710 if (!crtc
->primary
->fb
) {
2711 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2715 fb
= crtc
->primary
->fb
;
2716 /* Make refcounting symmetric with the lookup path. */
2717 drm_framebuffer_reference(fb
);
2719 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2721 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2728 mode
= drm_mode_create(dev
);
2734 ret
= drm_mode_convert_umode(mode
, &crtc_req
->mode
);
2736 DRM_DEBUG_KMS("Invalid mode\n");
2741 * Check whether the primary plane supports the fb pixel format.
2742 * Drivers not implementing the universal planes API use a
2743 * default formats list provided by the DRM core which doesn't
2744 * match real hardware capabilities. Skip the check in that
2747 if (!crtc
->primary
->format_default
) {
2748 ret
= drm_plane_check_pixel_format(crtc
->primary
,
2751 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2752 drm_get_format_name(fb
->pixel_format
));
2757 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2764 if (crtc_req
->count_connectors
== 0 && mode
) {
2765 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2770 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2771 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2772 crtc_req
->count_connectors
);
2777 if (crtc_req
->count_connectors
> 0) {
2780 /* Avoid unbounded kernel memory allocation */
2781 if (crtc_req
->count_connectors
> config
->num_connector
) {
2786 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
2787 sizeof(struct drm_connector
*),
2789 if (!connector_set
) {
2794 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2795 connector_set
[i
] = NULL
;
2796 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2797 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2802 connector
= drm_connector_lookup(dev
, out_id
);
2804 DRM_DEBUG_KMS("Connector id %d unknown\n",
2809 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2813 connector_set
[i
] = connector
;
2818 set
.x
= crtc_req
->x
;
2819 set
.y
= crtc_req
->y
;
2821 set
.connectors
= connector_set
;
2822 set
.num_connectors
= crtc_req
->count_connectors
;
2824 ret
= drm_mode_set_config_internal(&set
);
2828 drm_framebuffer_unreference(fb
);
2830 if (connector_set
) {
2831 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2832 if (connector_set
[i
])
2833 drm_connector_unreference(connector_set
[i
]);
2836 kfree(connector_set
);
2837 drm_mode_destroy(dev
, mode
);
2838 drm_modeset_unlock_all(dev
);
2843 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2844 * universal plane handler call
2845 * @crtc: crtc to update cursor for
2846 * @req: data pointer for the ioctl
2847 * @file_priv: drm file for the ioctl call
2849 * Legacy cursor ioctl's work directly with driver buffer handles. To
2850 * translate legacy ioctl calls into universal plane handler calls, we need to
2851 * wrap the native buffer handle in a drm_framebuffer.
2853 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2854 * buffer with a pitch of 4*width; the universal plane interface should be used
2855 * directly in cases where the hardware can support other buffer settings and
2856 * userspace wants to make use of these capabilities.
2859 * Zero on success, negative errno on failure.
2861 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2862 struct drm_mode_cursor2
*req
,
2863 struct drm_file
*file_priv
)
2865 struct drm_device
*dev
= crtc
->dev
;
2866 struct drm_framebuffer
*fb
= NULL
;
2867 struct drm_mode_fb_cmd2 fbreq
= {
2868 .width
= req
->width
,
2869 .height
= req
->height
,
2870 .pixel_format
= DRM_FORMAT_ARGB8888
,
2871 .pitches
= { req
->width
* 4 },
2872 .handles
= { req
->handle
},
2874 int32_t crtc_x
, crtc_y
;
2875 uint32_t crtc_w
= 0, crtc_h
= 0;
2876 uint32_t src_w
= 0, src_h
= 0;
2879 BUG_ON(!crtc
->cursor
);
2880 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
2883 * Obtain fb we'll be using (either new or existing) and take an extra
2884 * reference to it if fb != null. setplane will take care of dropping
2885 * the reference if the plane update fails.
2887 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2889 fb
= internal_framebuffer_create(dev
, &fbreq
, file_priv
);
2891 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2894 fb
->hot_x
= req
->hot_x
;
2895 fb
->hot_y
= req
->hot_y
;
2900 fb
= crtc
->cursor
->fb
;
2902 drm_framebuffer_reference(fb
);
2905 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2909 crtc_x
= crtc
->cursor_x
;
2910 crtc_y
= crtc
->cursor_y
;
2915 crtc_h
= fb
->height
;
2916 src_w
= fb
->width
<< 16;
2917 src_h
= fb
->height
<< 16;
2921 * setplane_internal will take care of deref'ing either the old or new
2922 * framebuffer depending on success.
2924 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
2925 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2926 0, 0, src_w
, src_h
);
2928 /* Update successful; save new cursor position, if necessary */
2929 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2930 crtc
->cursor_x
= req
->x
;
2931 crtc
->cursor_y
= req
->y
;
2937 static int drm_mode_cursor_common(struct drm_device
*dev
,
2938 struct drm_mode_cursor2
*req
,
2939 struct drm_file
*file_priv
)
2941 struct drm_crtc
*crtc
;
2944 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2947 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2950 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2952 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2957 * If this crtc has a universal cursor plane, call that plane's update
2958 * handler rather than using legacy cursor handlers.
2960 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
2962 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
2966 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2967 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2971 /* Turns off the cursor if handle is 0 */
2972 if (crtc
->funcs
->cursor_set2
)
2973 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2974 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2976 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2977 req
->width
, req
->height
);
2980 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2981 if (crtc
->funcs
->cursor_move
) {
2982 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2989 drm_modeset_unlock_crtc(crtc
);
2997 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2998 * @dev: drm device for the ioctl
2999 * @data: data pointer for the ioctl
3000 * @file_priv: drm file for the ioctl call
3002 * Set the cursor configuration based on user request.
3004 * Called by the user via ioctl.
3007 * Zero on success, negative errno on failure.
3009 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
3010 void *data
, struct drm_file
*file_priv
)
3012 struct drm_mode_cursor
*req
= data
;
3013 struct drm_mode_cursor2 new_req
;
3015 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
3016 new_req
.hot_x
= new_req
.hot_y
= 0;
3018 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
3022 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3023 * @dev: drm device for the ioctl
3024 * @data: data pointer for the ioctl
3025 * @file_priv: drm file for the ioctl call
3027 * Set the cursor configuration based on user request. This implements the 2nd
3028 * version of the cursor ioctl, which allows userspace to additionally specify
3029 * the hotspot of the pointer.
3031 * Called by the user via ioctl.
3034 * Zero on success, negative errno on failure.
3036 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
3037 void *data
, struct drm_file
*file_priv
)
3039 struct drm_mode_cursor2
*req
= data
;
3041 return drm_mode_cursor_common(dev
, req
, file_priv
);
3045 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3046 * @bpp: bits per pixels
3047 * @depth: bit depth per pixel
3049 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3050 * Useful in fbdev emulation code, since that deals in those values.
3052 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
3058 fmt
= DRM_FORMAT_C8
;
3062 fmt
= DRM_FORMAT_XRGB1555
;
3064 fmt
= DRM_FORMAT_RGB565
;
3067 fmt
= DRM_FORMAT_RGB888
;
3071 fmt
= DRM_FORMAT_XRGB8888
;
3072 else if (depth
== 30)
3073 fmt
= DRM_FORMAT_XRGB2101010
;
3075 fmt
= DRM_FORMAT_ARGB8888
;
3078 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3079 fmt
= DRM_FORMAT_XRGB8888
;
3085 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
3088 * drm_mode_addfb - add an FB to the graphics configuration
3089 * @dev: drm device for the ioctl
3090 * @data: data pointer for the ioctl
3091 * @file_priv: drm file for the ioctl call
3093 * Add a new FB to the specified CRTC, given a user request. This is the
3094 * original addfb ioctl which only supported RGB formats.
3096 * Called by the user via ioctl.
3099 * Zero on success, negative errno on failure.
3101 int drm_mode_addfb(struct drm_device
*dev
,
3102 void *data
, struct drm_file
*file_priv
)
3104 struct drm_mode_fb_cmd
*or = data
;
3105 struct drm_mode_fb_cmd2 r
= {};
3108 /* convert to new format and call new ioctl */
3109 r
.fb_id
= or->fb_id
;
3110 r
.width
= or->width
;
3111 r
.height
= or->height
;
3112 r
.pitches
[0] = or->pitch
;
3113 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
3114 r
.handles
[0] = or->handle
;
3116 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
3120 or->fb_id
= r
.fb_id
;
3125 static int format_check(const struct drm_mode_fb_cmd2
*r
)
3127 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
3131 case DRM_FORMAT_RGB332
:
3132 case DRM_FORMAT_BGR233
:
3133 case DRM_FORMAT_XRGB4444
:
3134 case DRM_FORMAT_XBGR4444
:
3135 case DRM_FORMAT_RGBX4444
:
3136 case DRM_FORMAT_BGRX4444
:
3137 case DRM_FORMAT_ARGB4444
:
3138 case DRM_FORMAT_ABGR4444
:
3139 case DRM_FORMAT_RGBA4444
:
3140 case DRM_FORMAT_BGRA4444
:
3141 case DRM_FORMAT_XRGB1555
:
3142 case DRM_FORMAT_XBGR1555
:
3143 case DRM_FORMAT_RGBX5551
:
3144 case DRM_FORMAT_BGRX5551
:
3145 case DRM_FORMAT_ARGB1555
:
3146 case DRM_FORMAT_ABGR1555
:
3147 case DRM_FORMAT_RGBA5551
:
3148 case DRM_FORMAT_BGRA5551
:
3149 case DRM_FORMAT_RGB565
:
3150 case DRM_FORMAT_BGR565
:
3151 case DRM_FORMAT_RGB888
:
3152 case DRM_FORMAT_BGR888
:
3153 case DRM_FORMAT_XRGB8888
:
3154 case DRM_FORMAT_XBGR8888
:
3155 case DRM_FORMAT_RGBX8888
:
3156 case DRM_FORMAT_BGRX8888
:
3157 case DRM_FORMAT_ARGB8888
:
3158 case DRM_FORMAT_ABGR8888
:
3159 case DRM_FORMAT_RGBA8888
:
3160 case DRM_FORMAT_BGRA8888
:
3161 case DRM_FORMAT_XRGB2101010
:
3162 case DRM_FORMAT_XBGR2101010
:
3163 case DRM_FORMAT_RGBX1010102
:
3164 case DRM_FORMAT_BGRX1010102
:
3165 case DRM_FORMAT_ARGB2101010
:
3166 case DRM_FORMAT_ABGR2101010
:
3167 case DRM_FORMAT_RGBA1010102
:
3168 case DRM_FORMAT_BGRA1010102
:
3169 case DRM_FORMAT_YUYV
:
3170 case DRM_FORMAT_YVYU
:
3171 case DRM_FORMAT_UYVY
:
3172 case DRM_FORMAT_VYUY
:
3173 case DRM_FORMAT_AYUV
:
3174 case DRM_FORMAT_NV12
:
3175 case DRM_FORMAT_NV21
:
3176 case DRM_FORMAT_NV16
:
3177 case DRM_FORMAT_NV61
:
3178 case DRM_FORMAT_NV24
:
3179 case DRM_FORMAT_NV42
:
3180 case DRM_FORMAT_YUV410
:
3181 case DRM_FORMAT_YVU410
:
3182 case DRM_FORMAT_YUV411
:
3183 case DRM_FORMAT_YVU411
:
3184 case DRM_FORMAT_YUV420
:
3185 case DRM_FORMAT_YVU420
:
3186 case DRM_FORMAT_YUV422
:
3187 case DRM_FORMAT_YVU422
:
3188 case DRM_FORMAT_YUV444
:
3189 case DRM_FORMAT_YVU444
:
3192 DRM_DEBUG_KMS("invalid pixel format %s\n",
3193 drm_get_format_name(r
->pixel_format
));
3198 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3200 int ret
, hsub
, vsub
, num_planes
, i
;
3202 ret
= format_check(r
);
3204 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3205 drm_get_format_name(r
->pixel_format
));
3209 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3210 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3211 num_planes
= drm_format_num_planes(r
->pixel_format
);
3213 if (r
->width
== 0 || r
->width
% hsub
) {
3214 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
3218 if (r
->height
== 0 || r
->height
% vsub
) {
3219 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3223 for (i
= 0; i
< num_planes
; i
++) {
3224 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3225 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3226 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3228 if (!r
->handles
[i
]) {
3229 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3233 if ((uint64_t) width
* cpp
> UINT_MAX
)
3236 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3239 if (r
->pitches
[i
] < width
* cpp
) {
3240 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3244 if (r
->modifier
[i
] && !(r
->flags
& DRM_MODE_FB_MODIFIERS
)) {
3245 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3250 /* modifier specific checks: */
3251 switch (r
->modifier
[i
]) {
3252 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE
:
3253 /* NOTE: the pitch restriction may be lifted later if it turns
3254 * out that no hw has this restriction:
3256 if (r
->pixel_format
!= DRM_FORMAT_NV12
||
3257 width
% 128 || height
% 32 ||
3258 r
->pitches
[i
] % 128) {
3259 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i
);
3269 for (i
= num_planes
; i
< 4; i
++) {
3270 if (r
->modifier
[i
]) {
3271 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i
);
3275 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3276 if (!(r
->flags
& DRM_MODE_FB_MODIFIERS
))
3279 if (r
->handles
[i
]) {
3280 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i
);
3284 if (r
->pitches
[i
]) {
3285 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i
);
3289 if (r
->offsets
[i
]) {
3290 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i
);
3298 static struct drm_framebuffer
*
3299 internal_framebuffer_create(struct drm_device
*dev
,
3300 const struct drm_mode_fb_cmd2
*r
,
3301 struct drm_file
*file_priv
)
3303 struct drm_mode_config
*config
= &dev
->mode_config
;
3304 struct drm_framebuffer
*fb
;
3307 if (r
->flags
& ~(DRM_MODE_FB_INTERLACED
| DRM_MODE_FB_MODIFIERS
)) {
3308 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3309 return ERR_PTR(-EINVAL
);
3312 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3313 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3314 r
->width
, config
->min_width
, config
->max_width
);
3315 return ERR_PTR(-EINVAL
);
3317 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3318 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3319 r
->height
, config
->min_height
, config
->max_height
);
3320 return ERR_PTR(-EINVAL
);
3323 if (r
->flags
& DRM_MODE_FB_MODIFIERS
&&
3324 !dev
->mode_config
.allow_fb_modifiers
) {
3325 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3326 return ERR_PTR(-EINVAL
);
3329 ret
= framebuffer_check(r
);
3331 return ERR_PTR(ret
);
3333 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3335 DRM_DEBUG_KMS("could not create framebuffer\n");
3343 * drm_mode_addfb2 - add an FB to the graphics configuration
3344 * @dev: drm device for the ioctl
3345 * @data: data pointer for the ioctl
3346 * @file_priv: drm file for the ioctl call
3348 * Add a new FB to the specified CRTC, given a user request with format. This is
3349 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3350 * and uses fourcc codes as pixel format specifiers.
3352 * Called by the user via ioctl.
3355 * Zero on success, negative errno on failure.
3357 int drm_mode_addfb2(struct drm_device
*dev
,
3358 void *data
, struct drm_file
*file_priv
)
3360 struct drm_mode_fb_cmd2
*r
= data
;
3361 struct drm_framebuffer
*fb
;
3363 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3366 fb
= internal_framebuffer_create(dev
, r
, file_priv
);
3370 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3371 r
->fb_id
= fb
->base
.id
;
3373 /* Transfer ownership to the filp for reaping on close */
3374 mutex_lock(&file_priv
->fbs_lock
);
3375 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3376 mutex_unlock(&file_priv
->fbs_lock
);
3381 struct drm_mode_rmfb_work
{
3382 struct work_struct work
;
3383 struct list_head fbs
;
3386 static void drm_mode_rmfb_work_fn(struct work_struct
*w
)
3388 struct drm_mode_rmfb_work
*arg
= container_of(w
, typeof(*arg
), work
);
3390 while (!list_empty(&arg
->fbs
)) {
3391 struct drm_framebuffer
*fb
=
3392 list_first_entry(&arg
->fbs
, typeof(*fb
), filp_head
);
3394 list_del_init(&fb
->filp_head
);
3395 drm_framebuffer_remove(fb
);
3400 * drm_mode_rmfb - remove an FB from the configuration
3401 * @dev: drm device for the ioctl
3402 * @data: data pointer for the ioctl
3403 * @file_priv: drm file for the ioctl call
3405 * Remove the FB specified by the user.
3407 * Called by the user via ioctl.
3410 * Zero on success, negative errno on failure.
3412 int drm_mode_rmfb(struct drm_device
*dev
,
3413 void *data
, struct drm_file
*file_priv
)
3415 struct drm_framebuffer
*fb
= NULL
;
3416 struct drm_framebuffer
*fbl
= NULL
;
3417 uint32_t *id
= data
;
3420 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3423 fb
= drm_framebuffer_lookup(dev
, *id
);
3427 mutex_lock(&file_priv
->fbs_lock
);
3428 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3432 mutex_unlock(&file_priv
->fbs_lock
);
3436 list_del_init(&fb
->filp_head
);
3437 mutex_unlock(&file_priv
->fbs_lock
);
3439 /* drop the reference we picked up in framebuffer lookup */
3440 drm_framebuffer_unreference(fb
);
3443 * we now own the reference that was stored in the fbs list
3445 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3446 * so run this in a separate stack as there's no way to correctly
3447 * handle this after the fb is already removed from the lookup table.
3449 if (drm_framebuffer_read_refcount(fb
) > 1) {
3450 struct drm_mode_rmfb_work arg
;
3452 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3453 INIT_LIST_HEAD(&arg
.fbs
);
3454 list_add_tail(&fb
->filp_head
, &arg
.fbs
);
3456 schedule_work(&arg
.work
);
3457 flush_work(&arg
.work
);
3458 destroy_work_on_stack(&arg
.work
);
3460 drm_framebuffer_unreference(fb
);
3465 drm_framebuffer_unreference(fb
);
3470 * drm_mode_getfb - get FB info
3471 * @dev: drm device for the ioctl
3472 * @data: data pointer for the ioctl
3473 * @file_priv: drm file for the ioctl call
3475 * Lookup the FB given its ID and return info about it.
3477 * Called by the user via ioctl.
3480 * Zero on success, negative errno on failure.
3482 int drm_mode_getfb(struct drm_device
*dev
,
3483 void *data
, struct drm_file
*file_priv
)
3485 struct drm_mode_fb_cmd
*r
= data
;
3486 struct drm_framebuffer
*fb
;
3489 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3492 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3496 r
->height
= fb
->height
;
3497 r
->width
= fb
->width
;
3498 r
->depth
= fb
->depth
;
3499 r
->bpp
= fb
->bits_per_pixel
;
3500 r
->pitch
= fb
->pitches
[0];
3501 if (fb
->funcs
->create_handle
) {
3502 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3503 drm_is_control_client(file_priv
)) {
3504 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3507 /* GET_FB() is an unprivileged ioctl so we must not
3508 * return a buffer-handle to non-master processes! For
3509 * backwards-compatibility reasons, we cannot make
3510 * GET_FB() privileged, so just return an invalid handle
3511 * for non-masters. */
3519 drm_framebuffer_unreference(fb
);
3525 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3526 * @dev: drm device for the ioctl
3527 * @data: data pointer for the ioctl
3528 * @file_priv: drm file for the ioctl call
3530 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3531 * rectangle list. Generic userspace which does frontbuffer rendering must call
3532 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3533 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3535 * Modesetting drivers which always update the frontbuffer do not need to
3536 * implement the corresponding ->dirty framebuffer callback.
3538 * Called by the user via ioctl.
3541 * Zero on success, negative errno on failure.
3543 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3544 void *data
, struct drm_file
*file_priv
)
3546 struct drm_clip_rect __user
*clips_ptr
;
3547 struct drm_clip_rect
*clips
= NULL
;
3548 struct drm_mode_fb_dirty_cmd
*r
= data
;
3549 struct drm_framebuffer
*fb
;
3554 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3557 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3561 num_clips
= r
->num_clips
;
3562 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3564 if (!num_clips
!= !clips_ptr
) {
3569 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3571 /* If userspace annotates copy, clips must come in pairs */
3572 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3577 if (num_clips
&& clips_ptr
) {
3578 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3582 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
3588 ret
= copy_from_user(clips
, clips_ptr
,
3589 num_clips
* sizeof(*clips
));
3596 if (fb
->funcs
->dirty
) {
3597 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3606 drm_framebuffer_unreference(fb
);
3612 * drm_fb_release - remove and free the FBs on this file
3613 * @priv: drm file for the ioctl
3615 * Destroy all the FBs associated with @filp.
3617 * Called by the user via ioctl.
3620 * Zero on success, negative errno on failure.
3622 void drm_fb_release(struct drm_file
*priv
)
3624 struct drm_framebuffer
*fb
, *tfb
;
3625 struct drm_mode_rmfb_work arg
;
3627 INIT_LIST_HEAD(&arg
.fbs
);
3630 * When the file gets released that means no one else can access the fb
3631 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3632 * avoid upsetting lockdep since the universal cursor code adds a
3633 * framebuffer while holding mutex locks.
3635 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3636 * locks is impossible here since no one else but this function can get
3639 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3640 if (drm_framebuffer_read_refcount(fb
) > 1) {
3641 list_move_tail(&fb
->filp_head
, &arg
.fbs
);
3643 list_del_init(&fb
->filp_head
);
3645 /* This drops the fpriv->fbs reference. */
3646 drm_framebuffer_unreference(fb
);
3650 if (!list_empty(&arg
.fbs
)) {
3651 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3653 schedule_work(&arg
.work
);
3654 flush_work(&arg
.work
);
3655 destroy_work_on_stack(&arg
.work
);
3660 * drm_property_create - create a new property type
3662 * @flags: flags specifying the property type
3663 * @name: name of the property
3664 * @num_values: number of pre-defined values
3666 * This creates a new generic drm property which can then be attached to a drm
3667 * object with drm_object_attach_property. The returned property object must be
3668 * freed with drm_property_destroy.
3670 * Note that the DRM core keeps a per-device list of properties and that, if
3671 * drm_mode_config_cleanup() is called, it will destroy all properties created
3675 * A pointer to the newly created property on success, NULL on failure.
3677 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3678 const char *name
, int num_values
)
3680 struct drm_property
*property
= NULL
;
3683 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3687 property
->dev
= dev
;
3690 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
3692 if (!property
->values
)
3696 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3700 property
->flags
= flags
;
3701 property
->num_values
= num_values
;
3702 INIT_LIST_HEAD(&property
->enum_list
);
3705 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3706 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3709 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3711 WARN_ON(!drm_property_type_valid(property
));
3715 kfree(property
->values
);
3719 EXPORT_SYMBOL(drm_property_create
);
3722 * drm_property_create_enum - create a new enumeration property type
3724 * @flags: flags specifying the property type
3725 * @name: name of the property
3726 * @props: enumeration lists with property values
3727 * @num_values: number of pre-defined values
3729 * This creates a new generic drm property which can then be attached to a drm
3730 * object with drm_object_attach_property. The returned property object must be
3731 * freed with drm_property_destroy.
3733 * Userspace is only allowed to set one of the predefined values for enumeration
3737 * A pointer to the newly created property on success, NULL on failure.
3739 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3741 const struct drm_prop_enum_list
*props
,
3744 struct drm_property
*property
;
3747 flags
|= DRM_MODE_PROP_ENUM
;
3749 property
= drm_property_create(dev
, flags
, name
, num_values
);
3753 for (i
= 0; i
< num_values
; i
++) {
3754 ret
= drm_property_add_enum(property
, i
,
3758 drm_property_destroy(dev
, property
);
3765 EXPORT_SYMBOL(drm_property_create_enum
);
3768 * drm_property_create_bitmask - create a new bitmask property type
3770 * @flags: flags specifying the property type
3771 * @name: name of the property
3772 * @props: enumeration lists with property bitflags
3773 * @num_props: size of the @props array
3774 * @supported_bits: bitmask of all supported enumeration values
3776 * This creates a new bitmask drm property which can then be attached to a drm
3777 * object with drm_object_attach_property. The returned property object must be
3778 * freed with drm_property_destroy.
3780 * Compared to plain enumeration properties userspace is allowed to set any
3781 * or'ed together combination of the predefined property bitflag values
3784 * A pointer to the newly created property on success, NULL on failure.
3786 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3787 int flags
, const char *name
,
3788 const struct drm_prop_enum_list
*props
,
3790 uint64_t supported_bits
)
3792 struct drm_property
*property
;
3793 int i
, ret
, index
= 0;
3794 int num_values
= hweight64(supported_bits
);
3796 flags
|= DRM_MODE_PROP_BITMASK
;
3798 property
= drm_property_create(dev
, flags
, name
, num_values
);
3801 for (i
= 0; i
< num_props
; i
++) {
3802 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3805 if (WARN_ON(index
>= num_values
)) {
3806 drm_property_destroy(dev
, property
);
3810 ret
= drm_property_add_enum(property
, index
++,
3814 drm_property_destroy(dev
, property
);
3821 EXPORT_SYMBOL(drm_property_create_bitmask
);
3823 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3824 int flags
, const char *name
,
3825 uint64_t min
, uint64_t max
)
3827 struct drm_property
*property
;
3829 property
= drm_property_create(dev
, flags
, name
, 2);
3833 property
->values
[0] = min
;
3834 property
->values
[1] = max
;
3840 * drm_property_create_range - create a new unsigned ranged property type
3842 * @flags: flags specifying the property type
3843 * @name: name of the property
3844 * @min: minimum value of the property
3845 * @max: maximum value of the property
3847 * This creates a new generic drm property which can then be attached to a drm
3848 * object with drm_object_attach_property. The returned property object must be
3849 * freed with drm_property_destroy.
3851 * Userspace is allowed to set any unsigned integer value in the (min, max)
3855 * A pointer to the newly created property on success, NULL on failure.
3857 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3859 uint64_t min
, uint64_t max
)
3861 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3864 EXPORT_SYMBOL(drm_property_create_range
);
3867 * drm_property_create_signed_range - create a new signed ranged property type
3869 * @flags: flags specifying the property type
3870 * @name: name of the property
3871 * @min: minimum value of the property
3872 * @max: maximum value of the property
3874 * This creates a new generic drm property which can then be attached to a drm
3875 * object with drm_object_attach_property. The returned property object must be
3876 * freed with drm_property_destroy.
3878 * Userspace is allowed to set any signed integer value in the (min, max)
3882 * A pointer to the newly created property on success, NULL on failure.
3884 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3885 int flags
, const char *name
,
3886 int64_t min
, int64_t max
)
3888 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3889 name
, I642U64(min
), I642U64(max
));
3891 EXPORT_SYMBOL(drm_property_create_signed_range
);
3894 * drm_property_create_object - create a new object property type
3896 * @flags: flags specifying the property type
3897 * @name: name of the property
3898 * @type: object type from DRM_MODE_OBJECT_* defines
3900 * This creates a new generic drm property which can then be attached to a drm
3901 * object with drm_object_attach_property. The returned property object must be
3902 * freed with drm_property_destroy.
3904 * Userspace is only allowed to set this to any property value of the given
3905 * @type. Only useful for atomic properties, which is enforced.
3908 * A pointer to the newly created property on success, NULL on failure.
3910 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3911 int flags
, const char *name
, uint32_t type
)
3913 struct drm_property
*property
;
3915 flags
|= DRM_MODE_PROP_OBJECT
;
3917 if (WARN_ON(!(flags
& DRM_MODE_PROP_ATOMIC
)))
3920 property
= drm_property_create(dev
, flags
, name
, 1);
3924 property
->values
[0] = type
;
3928 EXPORT_SYMBOL(drm_property_create_object
);
3931 * drm_property_create_bool - create a new boolean property type
3933 * @flags: flags specifying the property type
3934 * @name: name of the property
3936 * This creates a new generic drm property which can then be attached to a drm
3937 * object with drm_object_attach_property. The returned property object must be
3938 * freed with drm_property_destroy.
3940 * This is implemented as a ranged property with only {0, 1} as valid values.
3943 * A pointer to the newly created property on success, NULL on failure.
3945 struct drm_property
*drm_property_create_bool(struct drm_device
*dev
, int flags
,
3948 return drm_property_create_range(dev
, flags
, name
, 0, 1);
3950 EXPORT_SYMBOL(drm_property_create_bool
);
3953 * drm_property_add_enum - add a possible value to an enumeration property
3954 * @property: enumeration property to change
3955 * @index: index of the new enumeration
3956 * @value: value of the new enumeration
3957 * @name: symbolic name of the new enumeration
3959 * This functions adds enumerations to a property.
3961 * It's use is deprecated, drivers should use one of the more specific helpers
3962 * to directly create the property with all enumerations already attached.
3965 * Zero on success, error code on failure.
3967 int drm_property_add_enum(struct drm_property
*property
, int index
,
3968 uint64_t value
, const char *name
)
3970 struct drm_property_enum
*prop_enum
;
3972 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3973 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3977 * Bitmask enum properties have the additional constraint of values
3980 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3984 if (!list_empty(&property
->enum_list
)) {
3985 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
3986 if (prop_enum
->value
== value
) {
3987 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3988 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3994 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3998 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3999 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
4000 prop_enum
->value
= value
;
4002 property
->values
[index
] = value
;
4003 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
4006 EXPORT_SYMBOL(drm_property_add_enum
);
4009 * drm_property_destroy - destroy a drm property
4011 * @property: property to destry
4013 * This function frees a property including any attached resources like
4014 * enumeration values.
4016 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
4018 struct drm_property_enum
*prop_enum
, *pt
;
4020 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
4021 list_del(&prop_enum
->head
);
4025 if (property
->num_values
)
4026 kfree(property
->values
);
4027 drm_mode_object_unregister(dev
, &property
->base
);
4028 list_del(&property
->head
);
4031 EXPORT_SYMBOL(drm_property_destroy
);
4034 * drm_object_attach_property - attach a property to a modeset object
4035 * @obj: drm modeset object
4036 * @property: property to attach
4037 * @init_val: initial value of the property
4039 * This attaches the given property to the modeset object with the given initial
4040 * value. Currently this function cannot fail since the properties are stored in
4041 * a statically sized array.
4043 void drm_object_attach_property(struct drm_mode_object
*obj
,
4044 struct drm_property
*property
,
4047 int count
= obj
->properties
->count
;
4049 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
4050 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4051 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4052 "you see this message on the same object type.\n",
4057 obj
->properties
->properties
[count
] = property
;
4058 obj
->properties
->values
[count
] = init_val
;
4059 obj
->properties
->count
++;
4060 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
4061 obj
->properties
->atomic_count
++;
4063 EXPORT_SYMBOL(drm_object_attach_property
);
4066 * drm_object_property_set_value - set the value of a property
4067 * @obj: drm mode object to set property value for
4068 * @property: property to set
4069 * @val: value the property should be set to
4071 * This functions sets a given property on a given object. This function only
4072 * changes the software state of the property, it does not call into the
4073 * driver's ->set_property callback.
4076 * Zero on success, error code on failure.
4078 int drm_object_property_set_value(struct drm_mode_object
*obj
,
4079 struct drm_property
*property
, uint64_t val
)
4083 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4084 if (obj
->properties
->properties
[i
] == property
) {
4085 obj
->properties
->values
[i
] = val
;
4092 EXPORT_SYMBOL(drm_object_property_set_value
);
4095 * drm_object_property_get_value - retrieve the value of a property
4096 * @obj: drm mode object to get property value from
4097 * @property: property to retrieve
4098 * @val: storage for the property value
4100 * This function retrieves the softare state of the given property for the given
4101 * property. Since there is no driver callback to retrieve the current property
4102 * value this might be out of sync with the hardware, depending upon the driver
4106 * Zero on success, error code on failure.
4108 int drm_object_property_get_value(struct drm_mode_object
*obj
,
4109 struct drm_property
*property
, uint64_t *val
)
4113 /* read-only properties bypass atomic mechanism and still store
4114 * their value in obj->properties->values[].. mostly to avoid
4115 * having to deal w/ EDID and similar props in atomic paths:
4117 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
4118 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
4119 return drm_atomic_get_property(obj
, property
, val
);
4121 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4122 if (obj
->properties
->properties
[i
] == property
) {
4123 *val
= obj
->properties
->values
[i
];
4130 EXPORT_SYMBOL(drm_object_property_get_value
);
4133 * drm_mode_getproperty_ioctl - get the property metadata
4136 * @file_priv: DRM file info
4138 * This function retrieves the metadata for a given property, like the different
4139 * possible values for an enum property or the limits for a range property.
4141 * Blob properties are special
4143 * Called by the user via ioctl.
4146 * Zero on success, negative errno on failure.
4148 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
4149 void *data
, struct drm_file
*file_priv
)
4151 struct drm_mode_get_property
*out_resp
= data
;
4152 struct drm_property
*property
;
4154 int value_count
= 0;
4157 struct drm_property_enum
*prop_enum
;
4158 struct drm_mode_property_enum __user
*enum_ptr
;
4159 uint64_t __user
*values_ptr
;
4161 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4164 drm_modeset_lock_all(dev
);
4165 property
= drm_property_find(dev
, out_resp
->prop_id
);
4171 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4172 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4173 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
4177 value_count
= property
->num_values
;
4179 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
4180 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
4181 out_resp
->flags
= property
->flags
;
4183 if ((out_resp
->count_values
>= value_count
) && value_count
) {
4184 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
4185 for (i
= 0; i
< value_count
; i
++) {
4186 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
4192 out_resp
->count_values
= value_count
;
4194 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4195 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4196 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
4198 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
4199 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4201 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
4206 if (copy_to_user(&enum_ptr
[copied
].name
,
4207 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
4214 out_resp
->count_enum_blobs
= enum_count
;
4218 * NOTE: The idea seems to have been to use this to read all the blob
4219 * property values. But nothing ever added them to the corresponding
4220 * list, userspace always used the special-purpose get_blob ioctl to
4221 * read the value for a blob property. It also doesn't make a lot of
4222 * sense to return values here when everything else is just metadata for
4223 * the property itself.
4225 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4226 out_resp
->count_enum_blobs
= 0;
4228 drm_modeset_unlock_all(dev
);
4232 static void drm_property_free_blob(struct kref
*kref
)
4234 struct drm_property_blob
*blob
=
4235 container_of(kref
, struct drm_property_blob
, base
.refcount
);
4237 mutex_lock(&blob
->dev
->mode_config
.blob_lock
);
4238 list_del(&blob
->head_global
);
4239 mutex_unlock(&blob
->dev
->mode_config
.blob_lock
);
4241 drm_mode_object_unregister(blob
->dev
, &blob
->base
);
4247 * drm_property_create_blob - Create new blob property
4249 * Creates a new blob property for a specified DRM device, optionally
4252 * @dev: DRM device to create property for
4253 * @length: Length to allocate for blob data
4254 * @data: If specified, copies data into blob
4257 * New blob property with a single reference on success, or an ERR_PTR
4260 struct drm_property_blob
*
4261 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
4264 struct drm_property_blob
*blob
;
4267 if (!length
|| length
> ULONG_MAX
- sizeof(struct drm_property_blob
))
4268 return ERR_PTR(-EINVAL
);
4270 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
4272 return ERR_PTR(-ENOMEM
);
4274 /* This must be explicitly initialised, so we can safely call list_del
4275 * on it in the removal handler, even if it isn't in a file list. */
4276 INIT_LIST_HEAD(&blob
->head_file
);
4277 blob
->length
= length
;
4281 memcpy(blob
->data
, data
, length
);
4283 ret
= drm_mode_object_get_reg(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
,
4284 true, drm_property_free_blob
);
4287 return ERR_PTR(-EINVAL
);
4290 mutex_lock(&dev
->mode_config
.blob_lock
);
4291 list_add_tail(&blob
->head_global
,
4292 &dev
->mode_config
.property_blob_list
);
4293 mutex_unlock(&dev
->mode_config
.blob_lock
);
4297 EXPORT_SYMBOL(drm_property_create_blob
);
4300 * drm_property_unreference_blob - Unreference a blob property
4302 * Drop a reference on a blob property. May free the object.
4304 * @blob: Pointer to blob property
4306 void drm_property_unreference_blob(struct drm_property_blob
*blob
)
4311 drm_mode_object_unreference(&blob
->base
);
4313 EXPORT_SYMBOL(drm_property_unreference_blob
);
4316 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4318 * @file_priv: destroy all blobs owned by this file handle
4320 void drm_property_destroy_user_blobs(struct drm_device
*dev
,
4321 struct drm_file
*file_priv
)
4323 struct drm_property_blob
*blob
, *bt
;
4326 * When the file gets released that means no one else can access the
4327 * blob list any more, so no need to grab dev->blob_lock.
4329 list_for_each_entry_safe(blob
, bt
, &file_priv
->blobs
, head_file
) {
4330 list_del_init(&blob
->head_file
);
4331 drm_property_unreference_blob(blob
);
4336 * drm_property_reference_blob - Take a reference on an existing property
4338 * Take a new reference on an existing blob property.
4340 * @blob: Pointer to blob property
4342 struct drm_property_blob
*drm_property_reference_blob(struct drm_property_blob
*blob
)
4344 drm_mode_object_reference(&blob
->base
);
4347 EXPORT_SYMBOL(drm_property_reference_blob
);
4350 * drm_property_lookup_blob - look up a blob property and take a reference
4352 * @id: id of the blob property
4354 * If successful, this takes an additional reference to the blob property.
4355 * callers need to make sure to eventually unreference the returned property
4356 * again, using @drm_property_unreference_blob.
4358 struct drm_property_blob
*drm_property_lookup_blob(struct drm_device
*dev
,
4361 struct drm_mode_object
*obj
;
4362 struct drm_property_blob
*blob
= NULL
;
4364 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_BLOB
);
4366 blob
= obj_to_blob(obj
);
4369 EXPORT_SYMBOL(drm_property_lookup_blob
);
4372 * drm_property_replace_global_blob - atomically replace existing blob property
4374 * @replace: location of blob property pointer to be replaced
4375 * @length: length of data for new blob, or 0 for no data
4376 * @data: content for new blob, or NULL for no data
4377 * @obj_holds_id: optional object for property holding blob ID
4378 * @prop_holds_id: optional property holding blob ID
4379 * @return 0 on success or error on failure
4381 * This function will atomically replace a global property in the blob list,
4382 * optionally updating a property which holds the ID of that property. It is
4383 * guaranteed to be atomic: no caller will be allowed to see intermediate
4384 * results, and either the entire operation will succeed and clean up the
4385 * previous property, or it will fail and the state will be unchanged.
4387 * If length is 0 or data is NULL, no new blob will be created, and the holding
4388 * property, if specified, will be set to 0.
4390 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4391 * by holding the relevant modesetting object lock for its parent.
4393 * For example, a drm_connector has a 'PATH' property, which contains the ID
4394 * of a blob property with the value of the MST path information. Calling this
4395 * function with replace pointing to the connector's path_blob_ptr, length and
4396 * data set for the new path information, obj_holds_id set to the connector's
4397 * base object, and prop_holds_id set to the path property name, will perform
4398 * a completely atomic update. The access to path_blob_ptr is protected by the
4399 * caller holding a lock on the connector.
4401 static int drm_property_replace_global_blob(struct drm_device
*dev
,
4402 struct drm_property_blob
**replace
,
4405 struct drm_mode_object
*obj_holds_id
,
4406 struct drm_property
*prop_holds_id
)
4408 struct drm_property_blob
*new_blob
= NULL
;
4409 struct drm_property_blob
*old_blob
= NULL
;
4412 WARN_ON(replace
== NULL
);
4414 old_blob
= *replace
;
4416 if (length
&& data
) {
4417 new_blob
= drm_property_create_blob(dev
, length
, data
);
4418 if (IS_ERR(new_blob
))
4419 return PTR_ERR(new_blob
);
4422 /* This does not need to be synchronised with blob_lock, as the
4423 * get_properties ioctl locks all modesetting objects, and
4424 * obj_holds_id must be locked before calling here, so we cannot
4425 * have its value out of sync with the list membership modified
4426 * below under blob_lock. */
4428 ret
= drm_object_property_set_value(obj_holds_id
,
4431 new_blob
->base
.id
: 0);
4436 drm_property_unreference_blob(old_blob
);
4437 *replace
= new_blob
;
4442 drm_property_unreference_blob(new_blob
);
4447 * drm_mode_getblob_ioctl - get the contents of a blob property value
4450 * @file_priv: DRM file info
4452 * This function retrieves the contents of a blob property. The value stored in
4453 * an object's blob property is just a normal modeset object id.
4455 * Called by the user via ioctl.
4458 * Zero on success, negative errno on failure.
4460 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
4461 void *data
, struct drm_file
*file_priv
)
4463 struct drm_mode_get_blob
*out_resp
= data
;
4464 struct drm_property_blob
*blob
;
4466 void __user
*blob_ptr
;
4468 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4471 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4475 if (out_resp
->length
== blob
->length
) {
4476 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4477 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
4482 out_resp
->length
= blob
->length
;
4484 drm_property_unreference_blob(blob
);
4490 * drm_mode_createblob_ioctl - create a new blob property
4493 * @file_priv: DRM file info
4495 * This function creates a new blob property with user-defined values. In order
4496 * to give us sensible validation and checking when creating, rather than at
4497 * every potential use, we also require a type to be provided upfront.
4499 * Called by the user via ioctl.
4502 * Zero on success, negative errno on failure.
4504 int drm_mode_createblob_ioctl(struct drm_device
*dev
,
4505 void *data
, struct drm_file
*file_priv
)
4507 struct drm_mode_create_blob
*out_resp
= data
;
4508 struct drm_property_blob
*blob
;
4509 void __user
*blob_ptr
;
4512 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4515 blob
= drm_property_create_blob(dev
, out_resp
->length
, NULL
);
4517 return PTR_ERR(blob
);
4519 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4520 if (copy_from_user(blob
->data
, blob_ptr
, out_resp
->length
)) {
4525 /* Dropping the lock between create_blob and our access here is safe
4526 * as only the same file_priv can remove the blob; at this point, it is
4527 * not associated with any file_priv. */
4528 mutex_lock(&dev
->mode_config
.blob_lock
);
4529 out_resp
->blob_id
= blob
->base
.id
;
4530 list_add_tail(&blob
->head_file
, &file_priv
->blobs
);
4531 mutex_unlock(&dev
->mode_config
.blob_lock
);
4536 drm_property_unreference_blob(blob
);
4541 * drm_mode_destroyblob_ioctl - destroy a user blob property
4544 * @file_priv: DRM file info
4546 * Destroy an existing user-defined blob property.
4548 * Called by the user via ioctl.
4551 * Zero on success, negative errno on failure.
4553 int drm_mode_destroyblob_ioctl(struct drm_device
*dev
,
4554 void *data
, struct drm_file
*file_priv
)
4556 struct drm_mode_destroy_blob
*out_resp
= data
;
4557 struct drm_property_blob
*blob
= NULL
, *bt
;
4561 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4564 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4568 mutex_lock(&dev
->mode_config
.blob_lock
);
4569 /* Ensure the property was actually created by this user. */
4570 list_for_each_entry(bt
, &file_priv
->blobs
, head_file
) {
4582 /* We must drop head_file here, because we may not be the last
4583 * reference on the blob. */
4584 list_del_init(&blob
->head_file
);
4585 mutex_unlock(&dev
->mode_config
.blob_lock
);
4587 /* One reference from lookup, and one from the filp. */
4588 drm_property_unreference_blob(blob
);
4589 drm_property_unreference_blob(blob
);
4594 mutex_unlock(&dev
->mode_config
.blob_lock
);
4595 drm_property_unreference_blob(blob
);
4601 * drm_mode_connector_set_path_property - set tile property on connector
4602 * @connector: connector to set property on.
4603 * @path: path to use for property; must not be NULL.
4605 * This creates a property to expose to userspace to specify a
4606 * connector path. This is mainly used for DisplayPort MST where
4607 * connectors have a topology and we want to allow userspace to give
4608 * them more meaningful names.
4611 * Zero on success, negative errno on failure.
4613 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4616 struct drm_device
*dev
= connector
->dev
;
4619 ret
= drm_property_replace_global_blob(dev
,
4620 &connector
->path_blob_ptr
,
4624 dev
->mode_config
.path_property
);
4627 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4630 * drm_mode_connector_set_tile_property - set tile property on connector
4631 * @connector: connector to set property on.
4633 * This looks up the tile information for a connector, and creates a
4634 * property for userspace to parse if it exists. The property is of
4635 * the form of 8 integers using ':' as a separator.
4638 * Zero on success, errno on failure.
4640 int drm_mode_connector_set_tile_property(struct drm_connector
*connector
)
4642 struct drm_device
*dev
= connector
->dev
;
4646 if (!connector
->has_tile
) {
4647 ret
= drm_property_replace_global_blob(dev
,
4648 &connector
->tile_blob_ptr
,
4652 dev
->mode_config
.tile_property
);
4656 snprintf(tile
, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4657 connector
->tile_group
->id
, connector
->tile_is_single_monitor
,
4658 connector
->num_h_tile
, connector
->num_v_tile
,
4659 connector
->tile_h_loc
, connector
->tile_v_loc
,
4660 connector
->tile_h_size
, connector
->tile_v_size
);
4662 ret
= drm_property_replace_global_blob(dev
,
4663 &connector
->tile_blob_ptr
,
4667 dev
->mode_config
.tile_property
);
4670 EXPORT_SYMBOL(drm_mode_connector_set_tile_property
);
4673 * drm_mode_connector_update_edid_property - update the edid property of a connector
4674 * @connector: drm connector
4675 * @edid: new value of the edid property
4677 * This function creates a new blob modeset object and assigns its id to the
4678 * connector's edid property.
4681 * Zero on success, negative errno on failure.
4683 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4684 const struct edid
*edid
)
4686 struct drm_device
*dev
= connector
->dev
;
4690 /* ignore requests to set edid when overridden */
4691 if (connector
->override_edid
)
4695 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4697 ret
= drm_property_replace_global_blob(dev
,
4698 &connector
->edid_blob_ptr
,
4702 dev
->mode_config
.edid_property
);
4705 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4707 /* Some properties could refer to dynamic refcnt'd objects, or things that
4708 * need special locking to handle lifetime issues (ie. to ensure the prop
4709 * value doesn't become invalid part way through the property update due to
4710 * race). The value returned by reference via 'obj' should be passed back
4711 * to drm_property_change_valid_put() after the property is set (and the
4712 * object to which the property is attached has a chance to take it's own
4715 bool drm_property_change_valid_get(struct drm_property
*property
,
4716 uint64_t value
, struct drm_mode_object
**ref
)
4720 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4725 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4726 if (value
< property
->values
[0] || value
> property
->values
[1])
4729 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4730 int64_t svalue
= U642I64(value
);
4732 if (svalue
< U642I64(property
->values
[0]) ||
4733 svalue
> U642I64(property
->values
[1]))
4736 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4737 uint64_t valid_mask
= 0;
4739 for (i
= 0; i
< property
->num_values
; i
++)
4740 valid_mask
|= (1ULL << property
->values
[i
]);
4741 return !(value
& ~valid_mask
);
4742 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4743 struct drm_property_blob
*blob
;
4748 blob
= drm_property_lookup_blob(property
->dev
, value
);
4755 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4756 /* a zero value for an object property translates to null: */
4760 *ref
= _object_find(property
->dev
, value
, property
->values
[0]);
4761 return *ref
!= NULL
;
4764 for (i
= 0; i
< property
->num_values
; i
++)
4765 if (property
->values
[i
] == value
)
4770 void drm_property_change_valid_put(struct drm_property
*property
,
4771 struct drm_mode_object
*ref
)
4776 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4777 drm_mode_object_unreference(ref
);
4778 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4779 drm_property_unreference_blob(obj_to_blob(ref
));
4783 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4786 * @file_priv: DRM file info
4788 * This function sets the current value for a connectors's property. It also
4789 * calls into a driver's ->set_property callback to update the hardware state
4791 * Called by the user via ioctl.
4794 * Zero on success, negative errno on failure.
4796 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4797 void *data
, struct drm_file
*file_priv
)
4799 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4800 struct drm_mode_obj_set_property obj_set_prop
= {
4801 .value
= conn_set_prop
->value
,
4802 .prop_id
= conn_set_prop
->prop_id
,
4803 .obj_id
= conn_set_prop
->connector_id
,
4804 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4807 /* It does all the locking and checking we need */
4808 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4811 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4812 struct drm_property
*property
,
4816 struct drm_connector
*connector
= obj_to_connector(obj
);
4818 /* Do DPMS ourselves */
4819 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4820 ret
= (*connector
->funcs
->dpms
)(connector
, (int)value
);
4821 } else if (connector
->funcs
->set_property
)
4822 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4824 /* store the property value if successful */
4826 drm_object_property_set_value(&connector
->base
, property
, value
);
4830 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4831 struct drm_property
*property
,
4835 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4837 if (crtc
->funcs
->set_property
)
4838 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4840 drm_object_property_set_value(obj
, property
, value
);
4846 * drm_mode_plane_set_obj_prop - set the value of a property
4847 * @plane: drm plane object to set property value for
4848 * @property: property to set
4849 * @value: value the property should be set to
4851 * This functions sets a given property on a given plane object. This function
4852 * calls the driver's ->set_property callback and changes the software state of
4853 * the property if the callback succeeds.
4856 * Zero on success, error code on failure.
4858 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
4859 struct drm_property
*property
,
4863 struct drm_mode_object
*obj
= &plane
->base
;
4865 if (plane
->funcs
->set_property
)
4866 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4868 drm_object_property_set_value(obj
, property
, value
);
4872 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
4875 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4878 * @file_priv: DRM file info
4880 * This function retrieves the current value for an object's property. Compared
4881 * to the connector specific ioctl this one is extended to also work on crtc and
4884 * Called by the user via ioctl.
4887 * Zero on success, negative errno on failure.
4889 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4890 struct drm_file
*file_priv
)
4892 struct drm_mode_obj_get_properties
*arg
= data
;
4893 struct drm_mode_object
*obj
;
4896 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4899 drm_modeset_lock_all(dev
);
4901 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4906 if (!obj
->properties
) {
4911 ret
= get_properties(obj
, file_priv
->atomic
,
4912 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
4913 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
4917 drm_mode_object_unreference(obj
);
4919 drm_modeset_unlock_all(dev
);
4924 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4927 * @file_priv: DRM file info
4929 * This function sets the current value for an object's property. It also calls
4930 * into a driver's ->set_property callback to update the hardware state.
4931 * Compared to the connector specific ioctl this one is extended to also work on
4932 * crtc and plane objects.
4934 * Called by the user via ioctl.
4937 * Zero on success, negative errno on failure.
4939 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4940 struct drm_file
*file_priv
)
4942 struct drm_mode_obj_set_property
*arg
= data
;
4943 struct drm_mode_object
*arg_obj
;
4944 struct drm_mode_object
*prop_obj
;
4945 struct drm_property
*property
;
4946 int i
, ret
= -EINVAL
;
4947 struct drm_mode_object
*ref
;
4949 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4952 drm_modeset_lock_all(dev
);
4954 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4959 if (!arg_obj
->properties
)
4962 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4963 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
4966 if (i
== arg_obj
->properties
->count
)
4969 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4970 DRM_MODE_OBJECT_PROPERTY
);
4975 property
= obj_to_property(prop_obj
);
4977 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
4980 switch (arg_obj
->type
) {
4981 case DRM_MODE_OBJECT_CONNECTOR
:
4982 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4985 case DRM_MODE_OBJECT_CRTC
:
4986 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4988 case DRM_MODE_OBJECT_PLANE
:
4989 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
4990 property
, arg
->value
);
4994 drm_property_change_valid_put(property
, ref
);
4997 drm_mode_object_unreference(arg_obj
);
4999 drm_modeset_unlock_all(dev
);
5004 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5005 * @connector: connector to attach
5006 * @encoder: encoder to attach @connector to
5008 * This function links up a connector to an encoder. Note that the routing
5009 * restrictions between encoders and crtcs are exposed to userspace through the
5010 * possible_clones and possible_crtcs bitmasks.
5013 * Zero on success, negative errno on failure.
5015 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
5016 struct drm_encoder
*encoder
)
5021 * In the past, drivers have attempted to model the static association
5022 * of connector to encoder in simple connector/encoder devices using a
5023 * direct assignment of connector->encoder = encoder. This connection
5024 * is a logical one and the responsibility of the core, so drivers are
5025 * expected not to mess with this.
5027 * Note that the error return should've been enough here, but a large
5028 * majority of drivers ignores the return value, so add in a big WARN
5029 * to get people's attention.
5031 if (WARN_ON(connector
->encoder
))
5034 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
5035 if (connector
->encoder_ids
[i
] == 0) {
5036 connector
->encoder_ids
[i
] = encoder
->base
.id
;
5042 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
5045 * drm_mode_crtc_set_gamma_size - set the gamma table size
5046 * @crtc: CRTC to set the gamma table size for
5047 * @gamma_size: size of the gamma table
5049 * Drivers which support gamma tables should set this to the supported gamma
5050 * table size when initializing the CRTC. Currently the drm core only supports a
5051 * fixed gamma table size.
5054 * Zero on success, negative errno on failure.
5056 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
5059 uint16_t *r_base
, *g_base
, *b_base
;
5062 crtc
->gamma_size
= gamma_size
;
5064 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
5066 if (!crtc
->gamma_store
) {
5067 crtc
->gamma_size
= 0;
5071 r_base
= crtc
->gamma_store
;
5072 g_base
= r_base
+ gamma_size
;
5073 b_base
= g_base
+ gamma_size
;
5074 for (i
= 0; i
< gamma_size
; i
++) {
5083 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
5086 * drm_mode_gamma_set_ioctl - set the gamma table
5089 * @file_priv: DRM file info
5091 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5092 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5094 * Called by the user via ioctl.
5097 * Zero on success, negative errno on failure.
5099 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
5100 void *data
, struct drm_file
*file_priv
)
5102 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5103 struct drm_crtc
*crtc
;
5104 void *r_base
, *g_base
, *b_base
;
5108 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5111 drm_modeset_lock_all(dev
);
5112 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5118 if (crtc
->funcs
->gamma_set
== NULL
) {
5123 /* memcpy into gamma store */
5124 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5129 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5130 r_base
= crtc
->gamma_store
;
5131 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
5136 g_base
= r_base
+ size
;
5137 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
5142 b_base
= g_base
+ size
;
5143 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
5148 ret
= crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, crtc
->gamma_size
);
5151 drm_modeset_unlock_all(dev
);
5157 * drm_mode_gamma_get_ioctl - get the gamma table
5160 * @file_priv: DRM file info
5162 * Copy the current gamma table into the storage provided. This also provides
5163 * the gamma table size the driver expects, which can be used to size the
5164 * allocated storage.
5166 * Called by the user via ioctl.
5169 * Zero on success, negative errno on failure.
5171 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
5172 void *data
, struct drm_file
*file_priv
)
5174 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5175 struct drm_crtc
*crtc
;
5176 void *r_base
, *g_base
, *b_base
;
5180 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5183 drm_modeset_lock_all(dev
);
5184 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5190 /* memcpy into gamma store */
5191 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5196 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5197 r_base
= crtc
->gamma_store
;
5198 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
5203 g_base
= r_base
+ size
;
5204 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
5209 b_base
= g_base
+ size
;
5210 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
5215 drm_modeset_unlock_all(dev
);
5220 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5223 * @file_priv: DRM file info
5225 * This schedules an asynchronous update on a given CRTC, called page flip.
5226 * Optionally a drm event is generated to signal the completion of the event.
5227 * Generic drivers cannot assume that a pageflip with changed framebuffer
5228 * properties (including driver specific metadata like tiling layout) will work,
5229 * but some drivers support e.g. pixel format changes through the pageflip
5232 * Called by the user via ioctl.
5235 * Zero on success, negative errno on failure.
5237 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
5238 void *data
, struct drm_file
*file_priv
)
5240 struct drm_mode_crtc_page_flip
*page_flip
= data
;
5241 struct drm_crtc
*crtc
;
5242 struct drm_framebuffer
*fb
= NULL
;
5243 struct drm_pending_vblank_event
*e
= NULL
;
5246 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
5247 page_flip
->reserved
!= 0)
5250 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
5253 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
5257 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
5258 if (crtc
->primary
->fb
== NULL
) {
5259 /* The framebuffer is currently unbound, presumably
5260 * due to a hotplug event, that userspace has not
5267 if (crtc
->funcs
->page_flip
== NULL
)
5270 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
5277 const struct drm_plane_state
*state
= crtc
->primary
->state
;
5279 ret
= check_src_coords(state
->src_x
, state
->src_y
,
5280 state
->src_w
, state
->src_h
, fb
);
5282 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
5287 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
5288 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5293 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
5294 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
5299 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
5300 e
->event
.base
.length
= sizeof(e
->event
);
5301 e
->event
.user_data
= page_flip
->user_data
;
5302 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
, &e
->event
.base
);
5309 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
5310 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
5312 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
)
5313 drm_event_cancel_free(dev
, &e
->base
);
5314 /* Keep the old fb, don't unref it. */
5315 crtc
->primary
->old_fb
= NULL
;
5317 crtc
->primary
->fb
= fb
;
5318 /* Unref only the old framebuffer. */
5324 drm_framebuffer_unreference(fb
);
5325 if (crtc
->primary
->old_fb
)
5326 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
5327 crtc
->primary
->old_fb
= NULL
;
5328 drm_modeset_unlock_crtc(crtc
);
5334 * drm_mode_config_reset - call ->reset callbacks
5337 * This functions calls all the crtc's, encoder's and connector's ->reset
5338 * callback. Drivers can use this in e.g. their driver load or resume code to
5339 * reset hardware and software state.
5341 void drm_mode_config_reset(struct drm_device
*dev
)
5343 struct drm_crtc
*crtc
;
5344 struct drm_plane
*plane
;
5345 struct drm_encoder
*encoder
;
5346 struct drm_connector
*connector
;
5348 drm_for_each_plane(plane
, dev
)
5349 if (plane
->funcs
->reset
)
5350 plane
->funcs
->reset(plane
);
5352 drm_for_each_crtc(crtc
, dev
)
5353 if (crtc
->funcs
->reset
)
5354 crtc
->funcs
->reset(crtc
);
5356 drm_for_each_encoder(encoder
, dev
)
5357 if (encoder
->funcs
->reset
)
5358 encoder
->funcs
->reset(encoder
);
5360 mutex_lock(&dev
->mode_config
.mutex
);
5361 drm_for_each_connector(connector
, dev
)
5362 if (connector
->funcs
->reset
)
5363 connector
->funcs
->reset(connector
);
5364 mutex_unlock(&dev
->mode_config
.mutex
);
5366 EXPORT_SYMBOL(drm_mode_config_reset
);
5369 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5372 * @file_priv: DRM file info
5374 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5375 * TTM or something else entirely) and returns the resulting buffer handle. This
5376 * handle can then be wrapped up into a framebuffer modeset object.
5378 * Note that userspace is not allowed to use such objects for render
5379 * acceleration - drivers must create their own private ioctls for such a use
5382 * Called by the user via ioctl.
5385 * Zero on success, negative errno on failure.
5387 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
5388 void *data
, struct drm_file
*file_priv
)
5390 struct drm_mode_create_dumb
*args
= data
;
5391 u32 cpp
, stride
, size
;
5393 if (!dev
->driver
->dumb_create
)
5395 if (!args
->width
|| !args
->height
|| !args
->bpp
)
5398 /* overflow checks for 32bit size calculations */
5399 /* NOTE: DIV_ROUND_UP() can overflow */
5400 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
5401 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
5403 stride
= cpp
* args
->width
;
5404 if (args
->height
> 0xffffffffU
/ stride
)
5407 /* test for wrap-around */
5408 size
= args
->height
* stride
;
5409 if (PAGE_ALIGN(size
) == 0)
5413 * handle, pitch and size are output parameters. Zero them out to
5414 * prevent drivers from accidentally using uninitialized data. Since
5415 * not all existing userspace is clearing these fields properly we
5416 * cannot reject IOCTL with garbage in them.
5422 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
5426 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5429 * @file_priv: DRM file info
5431 * Allocate an offset in the drm device node's address space to be able to
5432 * memory map a dumb buffer.
5434 * Called by the user via ioctl.
5437 * Zero on success, negative errno on failure.
5439 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
5440 void *data
, struct drm_file
*file_priv
)
5442 struct drm_mode_map_dumb
*args
= data
;
5444 /* call driver ioctl to get mmap offset */
5445 if (!dev
->driver
->dumb_map_offset
)
5448 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
5452 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5455 * @file_priv: DRM file info
5457 * This destroys the userspace handle for the given dumb backing storage buffer.
5458 * Since buffer objects must be reference counted in the kernel a buffer object
5459 * won't be immediately freed if a framebuffer modeset object still uses it.
5461 * Called by the user via ioctl.
5464 * Zero on success, negative errno on failure.
5466 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
5467 void *data
, struct drm_file
*file_priv
)
5469 struct drm_mode_destroy_dumb
*args
= data
;
5471 if (!dev
->driver
->dumb_destroy
)
5474 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
5478 * drm_rotation_simplify() - Try to simplify the rotation
5479 * @rotation: Rotation to be simplified
5480 * @supported_rotations: Supported rotations
5482 * Attempt to simplify the rotation to a form that is supported.
5483 * Eg. if the hardware supports everything except DRM_REFLECT_X
5484 * one could call this function like this:
5486 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5487 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5488 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5490 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5491 * transforms the hardware supports, this function may not
5492 * be able to produce a supported transform, so the caller should
5493 * check the result afterwards.
5495 unsigned int drm_rotation_simplify(unsigned int rotation
,
5496 unsigned int supported_rotations
)
5498 if (rotation
& ~supported_rotations
) {
5499 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5500 rotation
= (rotation
& DRM_REFLECT_MASK
) |
5501 BIT((ffs(rotation
& DRM_ROTATE_MASK
) + 1) % 4);
5506 EXPORT_SYMBOL(drm_rotation_simplify
);
5509 * drm_mode_config_init - initialize DRM mode_configuration structure
5512 * Initialize @dev's mode_config structure, used for tracking the graphics
5513 * configuration of @dev.
5515 * Since this initializes the modeset locks, no locking is possible. Which is no
5516 * problem, since this should happen single threaded at init time. It is the
5517 * driver's problem to ensure this guarantee.
5520 void drm_mode_config_init(struct drm_device
*dev
)
5522 mutex_init(&dev
->mode_config
.mutex
);
5523 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5524 mutex_init(&dev
->mode_config
.idr_mutex
);
5525 mutex_init(&dev
->mode_config
.fb_lock
);
5526 mutex_init(&dev
->mode_config
.blob_lock
);
5527 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5528 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5529 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5530 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5531 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5532 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5533 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5534 idr_init(&dev
->mode_config
.crtc_idr
);
5535 idr_init(&dev
->mode_config
.tile_idr
);
5536 ida_init(&dev
->mode_config
.connector_ida
);
5538 drm_modeset_lock_all(dev
);
5539 drm_mode_create_standard_properties(dev
);
5540 drm_modeset_unlock_all(dev
);
5542 /* Just to be sure */
5543 dev
->mode_config
.num_fb
= 0;
5544 dev
->mode_config
.num_connector
= 0;
5545 dev
->mode_config
.num_crtc
= 0;
5546 dev
->mode_config
.num_encoder
= 0;
5547 dev
->mode_config
.num_overlay_plane
= 0;
5548 dev
->mode_config
.num_total_plane
= 0;
5550 EXPORT_SYMBOL(drm_mode_config_init
);
5553 * drm_mode_config_cleanup - free up DRM mode_config info
5556 * Free up all the connectors and CRTCs associated with this DRM device, then
5557 * free up the framebuffers and associated buffer objects.
5559 * Note that since this /should/ happen single-threaded at driver/device
5560 * teardown time, no locking is required. It's the driver's job to ensure that
5561 * this guarantee actually holds true.
5563 * FIXME: cleanup any dangling user buffer objects too
5565 void drm_mode_config_cleanup(struct drm_device
*dev
)
5567 struct drm_connector
*connector
, *ot
;
5568 struct drm_crtc
*crtc
, *ct
;
5569 struct drm_encoder
*encoder
, *enct
;
5570 struct drm_framebuffer
*fb
, *fbt
;
5571 struct drm_property
*property
, *pt
;
5572 struct drm_property_blob
*blob
, *bt
;
5573 struct drm_plane
*plane
, *plt
;
5575 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5577 encoder
->funcs
->destroy(encoder
);
5580 list_for_each_entry_safe(connector
, ot
,
5581 &dev
->mode_config
.connector_list
, head
) {
5582 connector
->funcs
->destroy(connector
);
5585 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5587 drm_property_destroy(dev
, property
);
5590 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5592 plane
->funcs
->destroy(plane
);
5595 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5596 crtc
->funcs
->destroy(crtc
);
5599 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5601 drm_property_unreference_blob(blob
);
5605 * Single-threaded teardown context, so it's not required to grab the
5606 * fb_lock to protect against concurrent fb_list access. Contrary, it
5607 * would actually deadlock with the drm_framebuffer_cleanup function.
5609 * Also, if there are any framebuffers left, that's a driver leak now,
5610 * so politely WARN about this.
5612 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5613 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5614 drm_framebuffer_free(&fb
->base
.refcount
);
5617 ida_destroy(&dev
->mode_config
.connector_ida
);
5618 idr_destroy(&dev
->mode_config
.tile_idr
);
5619 idr_destroy(&dev
->mode_config
.crtc_idr
);
5620 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5622 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5624 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5625 unsigned int supported_rotations
)
5627 static const struct drm_prop_enum_list props
[] = {
5628 { DRM_ROTATE_0
, "rotate-0" },
5629 { DRM_ROTATE_90
, "rotate-90" },
5630 { DRM_ROTATE_180
, "rotate-180" },
5631 { DRM_ROTATE_270
, "rotate-270" },
5632 { DRM_REFLECT_X
, "reflect-x" },
5633 { DRM_REFLECT_Y
, "reflect-y" },
5636 return drm_property_create_bitmask(dev
, 0, "rotation",
5637 props
, ARRAY_SIZE(props
),
5638 supported_rotations
);
5640 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
5645 * Tile groups are used to represent tiled monitors with a unique
5646 * integer identifier. Tiled monitors using DisplayID v1.3 have
5647 * a unique 8-byte handle, we store this in a tile group, so we
5648 * have a common identifier for all tiles in a monitor group.
5650 static void drm_tile_group_free(struct kref
*kref
)
5652 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
5653 struct drm_device
*dev
= tg
->dev
;
5654 mutex_lock(&dev
->mode_config
.idr_mutex
);
5655 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
5656 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5661 * drm_mode_put_tile_group - drop a reference to a tile group.
5663 * @tg: tile group to drop reference to.
5665 * drop reference to tile group and free if 0.
5667 void drm_mode_put_tile_group(struct drm_device
*dev
,
5668 struct drm_tile_group
*tg
)
5670 kref_put(&tg
->refcount
, drm_tile_group_free
);
5674 * drm_mode_get_tile_group - get a reference to an existing tile group
5676 * @topology: 8-bytes unique per monitor.
5678 * Use the unique bytes to get a reference to an existing tile group.
5681 * tile group or NULL if not found.
5683 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
5686 struct drm_tile_group
*tg
;
5688 mutex_lock(&dev
->mode_config
.idr_mutex
);
5689 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
5690 if (!memcmp(tg
->group_data
, topology
, 8)) {
5691 if (!kref_get_unless_zero(&tg
->refcount
))
5693 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5697 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5700 EXPORT_SYMBOL(drm_mode_get_tile_group
);
5703 * drm_mode_create_tile_group - create a tile group from a displayid description
5705 * @topology: 8-bytes unique per monitor.
5707 * Create a tile group for the unique monitor, and get a unique
5708 * identifier for the tile group.
5711 * new tile group or error.
5713 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
5716 struct drm_tile_group
*tg
;
5719 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
5721 return ERR_PTR(-ENOMEM
);
5723 kref_init(&tg
->refcount
);
5724 memcpy(tg
->group_data
, topology
, 8);
5727 mutex_lock(&dev
->mode_config
.idr_mutex
);
5728 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
5736 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5739 EXPORT_SYMBOL(drm_mode_create_tile_group
);
5742 * drm_crtc_enable_color_mgmt - enable color management properties
5744 * @degamma_lut_size: the size of the degamma lut (before CSC)
5745 * @has_ctm: whether to attach ctm_property for CSC matrix
5746 * @gamma_lut_size: the size of the gamma lut (after CSC)
5748 * This function lets the driver enable the color correction
5749 * properties on a CRTC. This includes 3 degamma, csc and gamma
5750 * properties that userspace can set and 2 size properties to inform
5751 * the userspace of the lut sizes. Each of the properties are
5752 * optional. The gamma and degamma properties are only attached if
5753 * their size is not 0 and ctm_property is only attached if has_ctm is
5756 void drm_crtc_enable_color_mgmt(struct drm_crtc
*crtc
,
5757 uint degamma_lut_size
,
5759 uint gamma_lut_size
)
5761 struct drm_device
*dev
= crtc
->dev
;
5762 struct drm_mode_config
*config
= &dev
->mode_config
;
5764 if (degamma_lut_size
) {
5765 drm_object_attach_property(&crtc
->base
,
5766 config
->degamma_lut_property
, 0);
5767 drm_object_attach_property(&crtc
->base
,
5768 config
->degamma_lut_size_property
,
5773 drm_object_attach_property(&crtc
->base
,
5774 config
->ctm_property
, 0);
5776 if (gamma_lut_size
) {
5777 drm_object_attach_property(&crtc
->base
,
5778 config
->gamma_lut_property
, 0);
5779 drm_object_attach_property(&crtc
->base
,
5780 config
->gamma_lut_size_property
,
5784 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt
);