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
);
242 static char printable_char(int c
)
244 return isascii(c
) && isprint(c
) ? c
: '?';
248 * drm_get_format_name - return a string for drm fourcc format
249 * @format: format to compute name of
251 * Note that the buffer used by this function is globally shared and owned by
252 * the function itself.
254 * FIXME: This isn't really multithreading safe.
256 const char *drm_get_format_name(uint32_t format
)
260 snprintf(buf
, sizeof(buf
),
261 "%c%c%c%c %s-endian (0x%08x)",
262 printable_char(format
& 0xff),
263 printable_char((format
>> 8) & 0xff),
264 printable_char((format
>> 16) & 0xff),
265 printable_char((format
>> 24) & 0x7f),
266 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
271 EXPORT_SYMBOL(drm_get_format_name
);
274 * Internal function to assign a slot in the object idr and optionally
275 * register the object into the idr.
277 static int drm_mode_object_get_reg(struct drm_device
*dev
,
278 struct drm_mode_object
*obj
,
281 void (*obj_free_cb
)(struct kref
*kref
))
285 mutex_lock(&dev
->mode_config
.idr_mutex
);
286 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
289 * Set up the object linking under the protection of the idr
290 * lock so that other users can't see inconsistent state.
293 obj
->type
= obj_type
;
295 obj
->free_cb
= obj_free_cb
;
296 kref_init(&obj
->refcount
);
299 mutex_unlock(&dev
->mode_config
.idr_mutex
);
301 return ret
< 0 ? ret
: 0;
305 * drm_mode_object_get - allocate a new modeset identifier
307 * @obj: object pointer, used to generate unique ID
308 * @obj_type: object type
310 * Create a unique identifier based on @ptr in @dev's identifier space. Used
311 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
312 * modeset identifiers are _not_ reference counted. Hence don't use this for
313 * reference counted modeset objects like framebuffers.
316 * Zero on success, error code on failure.
318 int drm_mode_object_get(struct drm_device
*dev
,
319 struct drm_mode_object
*obj
, uint32_t obj_type
)
321 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true, NULL
);
324 static void drm_mode_object_register(struct drm_device
*dev
,
325 struct drm_mode_object
*obj
)
327 mutex_lock(&dev
->mode_config
.idr_mutex
);
328 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
329 mutex_unlock(&dev
->mode_config
.idr_mutex
);
333 * drm_mode_object_unregister - free a modeset identifer
335 * @object: object to free
337 * Free @id from @dev's unique identifier pool.
338 * This function can be called multiple times, and guards against
340 * These modeset identifiers are _not_ reference counted. Hence don't use this
341 * for reference counted modeset objects like framebuffers.
343 void drm_mode_object_unregister(struct drm_device
*dev
,
344 struct drm_mode_object
*object
)
346 mutex_lock(&dev
->mode_config
.idr_mutex
);
348 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
351 mutex_unlock(&dev
->mode_config
.idr_mutex
);
354 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
355 uint32_t id
, uint32_t type
)
357 struct drm_mode_object
*obj
= NULL
;
359 mutex_lock(&dev
->mode_config
.idr_mutex
);
360 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
361 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
363 if (obj
&& obj
->id
!= id
)
366 if (obj
&& obj
->free_cb
) {
367 if (!kref_get_unless_zero(&obj
->refcount
))
370 mutex_unlock(&dev
->mode_config
.idr_mutex
);
376 * drm_mode_object_find - look up a drm object with static lifetime
378 * @id: id of the mode object
379 * @type: type of the mode object
381 * This function is used to look up a modeset object. It will acquire a
382 * reference for reference counted objects. This reference must be dropped again
383 * by callind drm_mode_object_unreference().
385 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
386 uint32_t id
, uint32_t type
)
388 struct drm_mode_object
*obj
= NULL
;
390 obj
= _object_find(dev
, id
, type
);
393 EXPORT_SYMBOL(drm_mode_object_find
);
396 * drm_mode_object_unreference - decr the object refcnt
399 * This functions decrements the object's refcount if it is a refcounted modeset
400 * object. It is a no-op on any other object. This is used to drop references
401 * acquired with drm_mode_object_reference().
403 void drm_mode_object_unreference(struct drm_mode_object
*obj
)
406 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
407 kref_put(&obj
->refcount
, obj
->free_cb
);
410 EXPORT_SYMBOL(drm_mode_object_unreference
);
413 * drm_mode_object_reference - incr the object refcnt
416 * This functions increments the object's refcount if it is a refcounted modeset
417 * object. It is a no-op on any other object. References should be dropped again
418 * by calling drm_mode_object_unreference().
420 void drm_mode_object_reference(struct drm_mode_object
*obj
)
423 DRM_DEBUG("OBJ ID: %d (%d)\n", obj
->id
, atomic_read(&obj
->refcount
.refcount
));
424 kref_get(&obj
->refcount
);
427 EXPORT_SYMBOL(drm_mode_object_reference
);
429 static void drm_framebuffer_free(struct kref
*kref
)
431 struct drm_framebuffer
*fb
=
432 container_of(kref
, struct drm_framebuffer
, base
.refcount
);
433 struct drm_device
*dev
= fb
->dev
;
436 * The lookup idr holds a weak reference, which has not necessarily been
437 * removed at this point. Check for that.
439 drm_mode_object_unregister(dev
, &fb
->base
);
441 fb
->funcs
->destroy(fb
);
445 * drm_framebuffer_init - initialize a framebuffer
447 * @fb: framebuffer to be initialized
448 * @funcs: ... with these functions
450 * Allocates an ID for the framebuffer's parent mode object, sets its mode
451 * functions & device file and adds it to the master fd list.
454 * This functions publishes the fb and makes it available for concurrent access
455 * by other users. Which means by this point the fb _must_ be fully set up -
456 * since all the fb attributes are invariant over its lifetime, no further
457 * locking but only correct reference counting is required.
460 * Zero on success, error code on failure.
462 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
463 const struct drm_framebuffer_funcs
*funcs
)
467 INIT_LIST_HEAD(&fb
->filp_head
);
471 ret
= drm_mode_object_get_reg(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
,
472 false, drm_framebuffer_free
);
476 mutex_lock(&dev
->mode_config
.fb_lock
);
477 dev
->mode_config
.num_fb
++;
478 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
479 mutex_unlock(&dev
->mode_config
.fb_lock
);
481 drm_mode_object_register(dev
, &fb
->base
);
485 EXPORT_SYMBOL(drm_framebuffer_init
);
488 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
490 * @id: id of the fb object
492 * If successful, this grabs an additional reference to the framebuffer -
493 * callers need to make sure to eventually unreference the returned framebuffer
494 * again, using @drm_framebuffer_unreference.
496 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
499 struct drm_mode_object
*obj
;
500 struct drm_framebuffer
*fb
= NULL
;
502 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_FB
);
507 EXPORT_SYMBOL(drm_framebuffer_lookup
);
510 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
511 * @fb: fb to unregister
513 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
514 * those used for fbdev. Note that the caller must hold a reference of it's own,
515 * i.e. the object may not be destroyed through this call (since it'll lead to a
516 * locking inversion).
518 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
520 struct drm_device
*dev
;
527 /* Mark fb as reaped and drop idr ref. */
528 drm_mode_object_unregister(dev
, &fb
->base
);
530 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
533 * drm_framebuffer_cleanup - remove a framebuffer object
534 * @fb: framebuffer to remove
536 * Cleanup framebuffer. This function is intended to be used from the drivers
537 * ->destroy callback. It can also be used to clean up driver private
538 * framebuffers embedded into a larger structure.
540 * Note that this function does not remove the fb from active usuage - if it is
541 * still used anywhere, hilarity can ensue since userspace could call getfb on
542 * the id and get back -EINVAL. Obviously no concern at driver unload time.
544 * Also, the framebuffer will not be removed from the lookup idr - for
545 * user-created framebuffers this will happen in in the rmfb ioctl. For
546 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
547 * drm_framebuffer_unregister_private.
549 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
551 struct drm_device
*dev
= fb
->dev
;
553 mutex_lock(&dev
->mode_config
.fb_lock
);
555 dev
->mode_config
.num_fb
--;
556 mutex_unlock(&dev
->mode_config
.fb_lock
);
558 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
561 * drm_framebuffer_remove - remove and unreference a framebuffer object
562 * @fb: framebuffer to remove
564 * Scans all the CRTCs and planes in @dev's mode_config. If they're
565 * using @fb, removes it, setting it to NULL. Then drops the reference to the
566 * passed-in framebuffer. Might take the modeset locks.
568 * Note that this function optimizes the cleanup away if the caller holds the
569 * last reference to the framebuffer. It is also guaranteed to not take the
570 * modeset locks in this case.
572 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
574 struct drm_device
*dev
;
575 struct drm_crtc
*crtc
;
576 struct drm_plane
*plane
;
577 struct drm_mode_set set
;
585 WARN_ON(!list_empty(&fb
->filp_head
));
588 * drm ABI mandates that we remove any deleted framebuffers from active
589 * useage. But since most sane clients only remove framebuffers they no
590 * longer need, try to optimize this away.
592 * Since we're holding a reference ourselves, observing a refcount of 1
593 * means that we're the last holder and can skip it. Also, the refcount
594 * can never increase from 1 again, so we don't need any barriers or
597 * Note that userspace could try to race with use and instate a new
598 * usage _after_ we've cleared all current ones. End result will be an
599 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
602 if (drm_framebuffer_read_refcount(fb
) > 1) {
603 drm_modeset_lock_all(dev
);
604 /* remove from any CRTC */
605 drm_for_each_crtc(crtc
, dev
) {
606 if (crtc
->primary
->fb
== fb
) {
607 /* should turn off the crtc */
608 memset(&set
, 0, sizeof(struct drm_mode_set
));
611 ret
= drm_mode_set_config_internal(&set
);
613 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
617 drm_for_each_plane(plane
, dev
) {
619 drm_plane_force_disable(plane
);
621 drm_modeset_unlock_all(dev
);
624 drm_framebuffer_unreference(fb
);
626 EXPORT_SYMBOL(drm_framebuffer_remove
);
628 DEFINE_WW_CLASS(crtc_ww_class
);
630 static unsigned int drm_num_crtcs(struct drm_device
*dev
)
632 unsigned int num
= 0;
633 struct drm_crtc
*tmp
;
635 drm_for_each_crtc(tmp
, dev
) {
643 * drm_crtc_init_with_planes - Initialise a new CRTC object with
644 * specified primary and cursor planes.
646 * @crtc: CRTC object to init
647 * @primary: Primary plane for CRTC
648 * @cursor: Cursor plane for CRTC
649 * @funcs: callbacks for the new CRTC
650 * @name: printf style format string for the CRTC name, or NULL for default name
652 * Inits a new object created as base part of a driver crtc object.
655 * Zero on success, error code on failure.
657 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
658 struct drm_plane
*primary
,
659 struct drm_plane
*cursor
,
660 const struct drm_crtc_funcs
*funcs
,
661 const char *name
, ...)
663 struct drm_mode_config
*config
= &dev
->mode_config
;
666 WARN_ON(primary
&& primary
->type
!= DRM_PLANE_TYPE_PRIMARY
);
667 WARN_ON(cursor
&& cursor
->type
!= DRM_PLANE_TYPE_CURSOR
);
672 drm_modeset_lock_init(&crtc
->mutex
);
673 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
681 crtc
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
684 crtc
->name
= kasprintf(GFP_KERNEL
, "crtc-%d",
688 drm_mode_object_unregister(dev
, &crtc
->base
);
692 crtc
->base
.properties
= &crtc
->properties
;
694 list_add_tail(&crtc
->head
, &config
->crtc_list
);
697 crtc
->primary
= primary
;
698 crtc
->cursor
= cursor
;
700 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
702 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
704 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
705 drm_object_attach_property(&crtc
->base
, config
->prop_active
, 0);
706 drm_object_attach_property(&crtc
->base
, config
->prop_mode_id
, 0);
711 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
714 * drm_crtc_cleanup - Clean up the core crtc usage
715 * @crtc: CRTC to cleanup
717 * This function cleans up @crtc and removes it from the DRM mode setting
718 * core. Note that the function does *not* free the crtc structure itself,
719 * this is the responsibility of the caller.
721 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
723 struct drm_device
*dev
= crtc
->dev
;
725 kfree(crtc
->gamma_store
);
726 crtc
->gamma_store
= NULL
;
728 drm_modeset_lock_fini(&crtc
->mutex
);
730 drm_mode_object_unregister(dev
, &crtc
->base
);
731 list_del(&crtc
->head
);
732 dev
->mode_config
.num_crtc
--;
734 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
735 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
736 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
740 memset(crtc
, 0, sizeof(*crtc
));
742 EXPORT_SYMBOL(drm_crtc_cleanup
);
745 * drm_crtc_index - find the index of a registered CRTC
746 * @crtc: CRTC to find index for
748 * Given a registered CRTC, return the index of that CRTC within a DRM
749 * device's list of CRTCs.
751 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
753 unsigned int index
= 0;
754 struct drm_crtc
*tmp
;
756 drm_for_each_crtc(tmp
, crtc
->dev
) {
765 EXPORT_SYMBOL(drm_crtc_index
);
768 * drm_mode_remove - remove and free a mode
769 * @connector: connector list to modify
770 * @mode: mode to remove
772 * Remove @mode from @connector's mode list, then free it.
774 static void drm_mode_remove(struct drm_connector
*connector
,
775 struct drm_display_mode
*mode
)
777 list_del(&mode
->head
);
778 drm_mode_destroy(connector
->dev
, mode
);
782 * drm_display_info_set_bus_formats - set the supported bus formats
783 * @info: display info to store bus formats in
784 * @formats: array containing the supported bus formats
785 * @num_formats: the number of entries in the fmts array
787 * Store the supported bus formats in display info structure.
788 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
789 * a full list of available formats.
791 int drm_display_info_set_bus_formats(struct drm_display_info
*info
,
793 unsigned int num_formats
)
797 if (!formats
&& num_formats
)
800 if (formats
&& num_formats
) {
801 fmts
= kmemdup(formats
, sizeof(*formats
) * num_formats
,
807 kfree(info
->bus_formats
);
808 info
->bus_formats
= fmts
;
809 info
->num_bus_formats
= num_formats
;
813 EXPORT_SYMBOL(drm_display_info_set_bus_formats
);
816 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
817 * @connector: connector to quwery
819 * The kernel supports per-connector configration of its consoles through
820 * use of the video= parameter. This function parses that option and
821 * extracts the user's specified mode (or enable/disable status) for a
822 * particular connector. This is typically only used during the early fbdev
825 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
827 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
830 if (fb_get_options(connector
->name
, &option
))
833 if (!drm_mode_parse_command_line_for_connector(option
,
841 switch (mode
->force
) {
845 case DRM_FORCE_ON_DIGITAL
:
854 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
855 connector
->force
= mode
->force
;
858 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
860 mode
->xres
, mode
->yres
,
861 mode
->refresh_specified
? mode
->refresh
: 60,
862 mode
->rb
? " reduced blanking" : "",
863 mode
->margins
? " with margins" : "",
864 mode
->interlace
? " interlaced" : "");
867 static void drm_connector_free(struct kref
*kref
)
869 struct drm_connector
*connector
=
870 container_of(kref
, struct drm_connector
, base
.refcount
);
871 struct drm_device
*dev
= connector
->dev
;
873 drm_mode_object_unregister(dev
, &connector
->base
);
874 connector
->funcs
->destroy(connector
);
878 * drm_connector_init - Init a preallocated connector
880 * @connector: the connector to init
881 * @funcs: callbacks for this connector
882 * @connector_type: user visible type of the connector
884 * Initialises a preallocated connector. Connectors should be
885 * subclassed as part of driver connector objects.
888 * Zero on success, error code on failure.
890 int drm_connector_init(struct drm_device
*dev
,
891 struct drm_connector
*connector
,
892 const struct drm_connector_funcs
*funcs
,
895 struct drm_mode_config
*config
= &dev
->mode_config
;
897 struct ida
*connector_ida
=
898 &drm_connector_enum_list
[connector_type
].ida
;
900 drm_modeset_lock_all(dev
);
902 ret
= drm_mode_object_get_reg(dev
, &connector
->base
,
903 DRM_MODE_OBJECT_CONNECTOR
,
904 false, drm_connector_free
);
908 connector
->base
.properties
= &connector
->properties
;
909 connector
->dev
= dev
;
910 connector
->funcs
= funcs
;
912 connector
->connector_id
= ida_simple_get(&config
->connector_ida
, 0, 0, GFP_KERNEL
);
913 if (connector
->connector_id
< 0) {
914 ret
= connector
->connector_id
;
918 connector
->connector_type
= connector_type
;
919 connector
->connector_type_id
=
920 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
921 if (connector
->connector_type_id
< 0) {
922 ret
= connector
->connector_type_id
;
926 kasprintf(GFP_KERNEL
, "%s-%d",
927 drm_connector_enum_list
[connector_type
].name
,
928 connector
->connector_type_id
);
929 if (!connector
->name
) {
931 goto out_put_type_id
;
934 INIT_LIST_HEAD(&connector
->probed_modes
);
935 INIT_LIST_HEAD(&connector
->modes
);
936 connector
->edid_blob_ptr
= NULL
;
937 connector
->status
= connector_status_unknown
;
939 drm_connector_get_cmdline_mode(connector
);
941 /* We should add connectors at the end to avoid upsetting the connector
943 list_add_tail(&connector
->head
, &config
->connector_list
);
944 config
->num_connector
++;
946 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
947 drm_object_attach_property(&connector
->base
,
948 config
->edid_property
,
951 drm_object_attach_property(&connector
->base
,
952 config
->dpms_property
, 0);
954 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
955 drm_object_attach_property(&connector
->base
, config
->prop_crtc_id
, 0);
958 connector
->debugfs_entry
= NULL
;
961 ida_remove(connector_ida
, connector
->connector_type_id
);
964 ida_remove(&config
->connector_ida
, connector
->connector_id
);
967 drm_mode_object_unregister(dev
, &connector
->base
);
970 drm_modeset_unlock_all(dev
);
974 EXPORT_SYMBOL(drm_connector_init
);
977 * drm_connector_cleanup - cleans up an initialised connector
978 * @connector: connector to cleanup
980 * Cleans up the connector but doesn't free the object.
982 void drm_connector_cleanup(struct drm_connector
*connector
)
984 struct drm_device
*dev
= connector
->dev
;
985 struct drm_display_mode
*mode
, *t
;
987 if (connector
->tile_group
) {
988 drm_mode_put_tile_group(dev
, connector
->tile_group
);
989 connector
->tile_group
= NULL
;
992 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
993 drm_mode_remove(connector
, mode
);
995 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
996 drm_mode_remove(connector
, mode
);
998 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
999 connector
->connector_type_id
);
1001 ida_remove(&dev
->mode_config
.connector_ida
,
1002 connector
->connector_id
);
1004 kfree(connector
->display_info
.bus_formats
);
1005 drm_mode_object_unregister(dev
, &connector
->base
);
1006 kfree(connector
->name
);
1007 connector
->name
= NULL
;
1008 list_del(&connector
->head
);
1009 dev
->mode_config
.num_connector
--;
1011 WARN_ON(connector
->state
&& !connector
->funcs
->atomic_destroy_state
);
1012 if (connector
->state
&& connector
->funcs
->atomic_destroy_state
)
1013 connector
->funcs
->atomic_destroy_state(connector
,
1016 memset(connector
, 0, sizeof(*connector
));
1018 EXPORT_SYMBOL(drm_connector_cleanup
);
1021 * drm_connector_register - register a connector
1022 * @connector: the connector to register
1024 * Register userspace interfaces for a connector
1027 * Zero on success, error code on failure.
1029 int drm_connector_register(struct drm_connector
*connector
)
1033 ret
= drm_sysfs_connector_add(connector
);
1037 ret
= drm_debugfs_connector_add(connector
);
1039 drm_sysfs_connector_remove(connector
);
1043 drm_mode_object_register(connector
->dev
, &connector
->base
);
1047 EXPORT_SYMBOL(drm_connector_register
);
1050 * drm_connector_unregister - unregister a connector
1051 * @connector: the connector to unregister
1053 * Unregister userspace interfaces for a connector
1055 void drm_connector_unregister(struct drm_connector
*connector
)
1057 drm_sysfs_connector_remove(connector
);
1058 drm_debugfs_connector_remove(connector
);
1060 EXPORT_SYMBOL(drm_connector_unregister
);
1063 * drm_connector_register_all - register all connectors
1066 * This function registers all connectors in sysfs and other places so that
1067 * userspace can start to access them. Drivers can call it after calling
1068 * drm_dev_register() to complete the device registration, if they don't call
1069 * drm_connector_register() on each connector individually.
1071 * When a device is unplugged and should be removed from userspace access,
1072 * call drm_connector_unregister_all(), which is the inverse of this
1076 * Zero on success, error code on failure.
1078 int drm_connector_register_all(struct drm_device
*dev
)
1080 struct drm_connector
*connector
;
1083 mutex_lock(&dev
->mode_config
.mutex
);
1085 drm_for_each_connector(connector
, dev
) {
1086 ret
= drm_connector_register(connector
);
1091 mutex_unlock(&dev
->mode_config
.mutex
);
1096 mutex_unlock(&dev
->mode_config
.mutex
);
1097 drm_connector_unregister_all(dev
);
1100 EXPORT_SYMBOL(drm_connector_register_all
);
1103 * drm_connector_unregister_all - unregister connector userspace interfaces
1106 * This functions unregisters all connectors from sysfs and other places so
1107 * that userspace can no longer access them. Drivers should call this as the
1108 * first step tearing down the device instace, or when the underlying
1109 * physical device disappeared (e.g. USB unplug), right before calling
1110 * drm_dev_unregister().
1112 void drm_connector_unregister_all(struct drm_device
*dev
)
1114 struct drm_connector
*connector
;
1116 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1117 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1118 drm_connector_unregister(connector
);
1120 EXPORT_SYMBOL(drm_connector_unregister_all
);
1123 * drm_encoder_init - Init a preallocated encoder
1125 * @encoder: the encoder to init
1126 * @funcs: callbacks for this encoder
1127 * @encoder_type: user visible type of the encoder
1128 * @name: printf style format string for the encoder name, or NULL for default name
1130 * Initialises a preallocated encoder. Encoder should be
1131 * subclassed as part of driver encoder objects.
1134 * Zero on success, error code on failure.
1136 int drm_encoder_init(struct drm_device
*dev
,
1137 struct drm_encoder
*encoder
,
1138 const struct drm_encoder_funcs
*funcs
,
1139 int encoder_type
, const char *name
, ...)
1143 drm_modeset_lock_all(dev
);
1145 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1150 encoder
->encoder_type
= encoder_type
;
1151 encoder
->funcs
= funcs
;
1156 encoder
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1159 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1160 drm_encoder_enum_list
[encoder_type
].name
,
1163 if (!encoder
->name
) {
1168 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1169 dev
->mode_config
.num_encoder
++;
1173 drm_mode_object_unregister(dev
, &encoder
->base
);
1176 drm_modeset_unlock_all(dev
);
1180 EXPORT_SYMBOL(drm_encoder_init
);
1183 * drm_encoder_index - find the index of a registered encoder
1184 * @encoder: encoder to find index for
1186 * Given a registered encoder, return the index of that encoder within a DRM
1187 * device's list of encoders.
1189 unsigned int drm_encoder_index(struct drm_encoder
*encoder
)
1191 unsigned int index
= 0;
1192 struct drm_encoder
*tmp
;
1194 drm_for_each_encoder(tmp
, encoder
->dev
) {
1203 EXPORT_SYMBOL(drm_encoder_index
);
1206 * drm_encoder_cleanup - cleans up an initialised encoder
1207 * @encoder: encoder to cleanup
1209 * Cleans up the encoder but doesn't free the object.
1211 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1213 struct drm_device
*dev
= encoder
->dev
;
1215 drm_modeset_lock_all(dev
);
1216 drm_mode_object_unregister(dev
, &encoder
->base
);
1217 kfree(encoder
->name
);
1218 list_del(&encoder
->head
);
1219 dev
->mode_config
.num_encoder
--;
1220 drm_modeset_unlock_all(dev
);
1222 memset(encoder
, 0, sizeof(*encoder
));
1224 EXPORT_SYMBOL(drm_encoder_cleanup
);
1226 static unsigned int drm_num_planes(struct drm_device
*dev
)
1228 unsigned int num
= 0;
1229 struct drm_plane
*tmp
;
1231 drm_for_each_plane(tmp
, dev
) {
1239 * drm_universal_plane_init - Initialize a new universal plane object
1241 * @plane: plane object to init
1242 * @possible_crtcs: bitmask of possible CRTCs
1243 * @funcs: callbacks for the new plane
1244 * @formats: array of supported formats (%DRM_FORMAT_*)
1245 * @format_count: number of elements in @formats
1246 * @type: type of plane (overlay, primary, cursor)
1247 * @name: printf style format string for the plane name, or NULL for default name
1249 * Initializes a plane object of type @type.
1252 * Zero on success, error code on failure.
1254 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1255 unsigned long possible_crtcs
,
1256 const struct drm_plane_funcs
*funcs
,
1257 const uint32_t *formats
, unsigned int format_count
,
1258 enum drm_plane_type type
,
1259 const char *name
, ...)
1261 struct drm_mode_config
*config
= &dev
->mode_config
;
1264 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1268 drm_modeset_lock_init(&plane
->mutex
);
1270 plane
->base
.properties
= &plane
->properties
;
1272 plane
->funcs
= funcs
;
1273 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
1275 if (!plane
->format_types
) {
1276 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1277 drm_mode_object_unregister(dev
, &plane
->base
);
1285 plane
->name
= kvasprintf(GFP_KERNEL
, name
, ap
);
1288 plane
->name
= kasprintf(GFP_KERNEL
, "plane-%d",
1289 drm_num_planes(dev
));
1292 kfree(plane
->format_types
);
1293 drm_mode_object_unregister(dev
, &plane
->base
);
1297 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1298 plane
->format_count
= format_count
;
1299 plane
->possible_crtcs
= possible_crtcs
;
1302 list_add_tail(&plane
->head
, &config
->plane_list
);
1303 config
->num_total_plane
++;
1304 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1305 config
->num_overlay_plane
++;
1307 drm_object_attach_property(&plane
->base
,
1308 config
->plane_type_property
,
1311 if (drm_core_check_feature(dev
, DRIVER_ATOMIC
)) {
1312 drm_object_attach_property(&plane
->base
, config
->prop_fb_id
, 0);
1313 drm_object_attach_property(&plane
->base
, config
->prop_crtc_id
, 0);
1314 drm_object_attach_property(&plane
->base
, config
->prop_crtc_x
, 0);
1315 drm_object_attach_property(&plane
->base
, config
->prop_crtc_y
, 0);
1316 drm_object_attach_property(&plane
->base
, config
->prop_crtc_w
, 0);
1317 drm_object_attach_property(&plane
->base
, config
->prop_crtc_h
, 0);
1318 drm_object_attach_property(&plane
->base
, config
->prop_src_x
, 0);
1319 drm_object_attach_property(&plane
->base
, config
->prop_src_y
, 0);
1320 drm_object_attach_property(&plane
->base
, config
->prop_src_w
, 0);
1321 drm_object_attach_property(&plane
->base
, config
->prop_src_h
, 0);
1326 EXPORT_SYMBOL(drm_universal_plane_init
);
1329 * drm_plane_init - Initialize a legacy plane
1331 * @plane: plane object to init
1332 * @possible_crtcs: bitmask of possible CRTCs
1333 * @funcs: callbacks for the new plane
1334 * @formats: array of supported formats (%DRM_FORMAT_*)
1335 * @format_count: number of elements in @formats
1336 * @is_primary: plane type (primary vs overlay)
1338 * Legacy API to initialize a DRM plane.
1340 * New drivers should call drm_universal_plane_init() instead.
1343 * Zero on success, error code on failure.
1345 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1346 unsigned long possible_crtcs
,
1347 const struct drm_plane_funcs
*funcs
,
1348 const uint32_t *formats
, unsigned int format_count
,
1351 enum drm_plane_type type
;
1353 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1354 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1355 formats
, format_count
, type
, NULL
);
1357 EXPORT_SYMBOL(drm_plane_init
);
1360 * drm_plane_cleanup - Clean up the core plane usage
1361 * @plane: plane to cleanup
1363 * This function cleans up @plane and removes it from the DRM mode setting
1364 * core. Note that the function does *not* free the plane structure itself,
1365 * this is the responsibility of the caller.
1367 void drm_plane_cleanup(struct drm_plane
*plane
)
1369 struct drm_device
*dev
= plane
->dev
;
1371 drm_modeset_lock_all(dev
);
1372 kfree(plane
->format_types
);
1373 drm_mode_object_unregister(dev
, &plane
->base
);
1375 BUG_ON(list_empty(&plane
->head
));
1377 list_del(&plane
->head
);
1378 dev
->mode_config
.num_total_plane
--;
1379 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1380 dev
->mode_config
.num_overlay_plane
--;
1381 drm_modeset_unlock_all(dev
);
1383 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
1384 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
1385 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1389 memset(plane
, 0, sizeof(*plane
));
1391 EXPORT_SYMBOL(drm_plane_cleanup
);
1394 * drm_plane_index - find the index of a registered plane
1395 * @plane: plane to find index for
1397 * Given a registered plane, return the index of that CRTC within a DRM
1398 * device's list of planes.
1400 unsigned int drm_plane_index(struct drm_plane
*plane
)
1402 unsigned int index
= 0;
1403 struct drm_plane
*tmp
;
1405 drm_for_each_plane(tmp
, plane
->dev
) {
1414 EXPORT_SYMBOL(drm_plane_index
);
1417 * drm_plane_from_index - find the registered plane at an index
1419 * @idx: index of registered plane to find for
1421 * Given a plane index, return the registered plane from DRM device's
1422 * list of planes with matching index.
1425 drm_plane_from_index(struct drm_device
*dev
, int idx
)
1427 struct drm_plane
*plane
;
1430 drm_for_each_plane(plane
, dev
) {
1437 EXPORT_SYMBOL(drm_plane_from_index
);
1440 * drm_plane_force_disable - Forcibly disable a plane
1441 * @plane: plane to disable
1443 * Forces the plane to be disabled.
1445 * Used when the plane's current framebuffer is destroyed,
1446 * and when restoring fbdev mode.
1448 void drm_plane_force_disable(struct drm_plane
*plane
)
1455 plane
->old_fb
= plane
->fb
;
1456 ret
= plane
->funcs
->disable_plane(plane
);
1458 DRM_ERROR("failed to disable plane with busy fb\n");
1459 plane
->old_fb
= NULL
;
1462 /* disconnect the plane from the fb and crtc: */
1463 drm_framebuffer_unreference(plane
->old_fb
);
1464 plane
->old_fb
= NULL
;
1468 EXPORT_SYMBOL(drm_plane_force_disable
);
1470 static int drm_mode_create_standard_properties(struct drm_device
*dev
)
1472 struct drm_property
*prop
;
1475 * Standard properties (apply to all connectors)
1477 prop
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1478 DRM_MODE_PROP_IMMUTABLE
,
1482 dev
->mode_config
.edid_property
= prop
;
1484 prop
= drm_property_create_enum(dev
, 0,
1485 "DPMS", drm_dpms_enum_list
,
1486 ARRAY_SIZE(drm_dpms_enum_list
));
1489 dev
->mode_config
.dpms_property
= prop
;
1491 prop
= drm_property_create(dev
,
1492 DRM_MODE_PROP_BLOB
|
1493 DRM_MODE_PROP_IMMUTABLE
,
1497 dev
->mode_config
.path_property
= prop
;
1499 prop
= drm_property_create(dev
,
1500 DRM_MODE_PROP_BLOB
|
1501 DRM_MODE_PROP_IMMUTABLE
,
1505 dev
->mode_config
.tile_property
= prop
;
1507 prop
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1508 "type", drm_plane_type_enum_list
,
1509 ARRAY_SIZE(drm_plane_type_enum_list
));
1512 dev
->mode_config
.plane_type_property
= prop
;
1514 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1515 "SRC_X", 0, UINT_MAX
);
1518 dev
->mode_config
.prop_src_x
= prop
;
1520 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1521 "SRC_Y", 0, UINT_MAX
);
1524 dev
->mode_config
.prop_src_y
= prop
;
1526 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1527 "SRC_W", 0, UINT_MAX
);
1530 dev
->mode_config
.prop_src_w
= prop
;
1532 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1533 "SRC_H", 0, UINT_MAX
);
1536 dev
->mode_config
.prop_src_h
= prop
;
1538 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1539 "CRTC_X", INT_MIN
, INT_MAX
);
1542 dev
->mode_config
.prop_crtc_x
= prop
;
1544 prop
= drm_property_create_signed_range(dev
, DRM_MODE_PROP_ATOMIC
,
1545 "CRTC_Y", INT_MIN
, INT_MAX
);
1548 dev
->mode_config
.prop_crtc_y
= prop
;
1550 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1551 "CRTC_W", 0, INT_MAX
);
1554 dev
->mode_config
.prop_crtc_w
= prop
;
1556 prop
= drm_property_create_range(dev
, DRM_MODE_PROP_ATOMIC
,
1557 "CRTC_H", 0, INT_MAX
);
1560 dev
->mode_config
.prop_crtc_h
= prop
;
1562 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1563 "FB_ID", DRM_MODE_OBJECT_FB
);
1566 dev
->mode_config
.prop_fb_id
= prop
;
1568 prop
= drm_property_create_object(dev
, DRM_MODE_PROP_ATOMIC
,
1569 "CRTC_ID", DRM_MODE_OBJECT_CRTC
);
1572 dev
->mode_config
.prop_crtc_id
= prop
;
1574 prop
= drm_property_create_bool(dev
, DRM_MODE_PROP_ATOMIC
,
1578 dev
->mode_config
.prop_active
= prop
;
1580 prop
= drm_property_create(dev
,
1581 DRM_MODE_PROP_ATOMIC
| DRM_MODE_PROP_BLOB
,
1585 dev
->mode_config
.prop_mode_id
= prop
;
1587 prop
= drm_property_create(dev
,
1592 dev
->mode_config
.degamma_lut_property
= prop
;
1594 prop
= drm_property_create_range(dev
,
1595 DRM_MODE_PROP_IMMUTABLE
,
1596 "DEGAMMA_LUT_SIZE", 0, UINT_MAX
);
1599 dev
->mode_config
.degamma_lut_size_property
= prop
;
1601 prop
= drm_property_create(dev
,
1606 dev
->mode_config
.ctm_property
= prop
;
1608 prop
= drm_property_create(dev
,
1613 dev
->mode_config
.gamma_lut_property
= prop
;
1615 prop
= drm_property_create_range(dev
,
1616 DRM_MODE_PROP_IMMUTABLE
,
1617 "GAMMA_LUT_SIZE", 0, UINT_MAX
);
1620 dev
->mode_config
.gamma_lut_size_property
= prop
;
1626 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1629 * Called by a driver the first time a DVI-I connector is made.
1631 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1633 struct drm_property
*dvi_i_selector
;
1634 struct drm_property
*dvi_i_subconnector
;
1636 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1640 drm_property_create_enum(dev
, 0,
1641 "select subconnector",
1642 drm_dvi_i_select_enum_list
,
1643 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1644 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1646 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1648 drm_dvi_i_subconnector_enum_list
,
1649 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1650 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1654 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1657 * drm_create_tv_properties - create TV specific connector properties
1659 * @num_modes: number of different TV formats (modes) supported
1660 * @modes: array of pointers to strings containing name of each format
1662 * Called by a driver's TV initialization routine, this function creates
1663 * the TV specific connector properties for a given device. Caller is
1664 * responsible for allocating a list of format names and passing them to
1667 int drm_mode_create_tv_properties(struct drm_device
*dev
,
1668 unsigned int num_modes
,
1669 const char * const modes
[])
1671 struct drm_property
*tv_selector
;
1672 struct drm_property
*tv_subconnector
;
1675 if (dev
->mode_config
.tv_select_subconnector_property
)
1679 * Basic connector properties
1681 tv_selector
= drm_property_create_enum(dev
, 0,
1682 "select subconnector",
1683 drm_tv_select_enum_list
,
1684 ARRAY_SIZE(drm_tv_select_enum_list
));
1688 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1691 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1693 drm_tv_subconnector_enum_list
,
1694 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1695 if (!tv_subconnector
)
1697 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1700 * Other, TV specific properties: margins & TV modes.
1702 dev
->mode_config
.tv_left_margin_property
=
1703 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1704 if (!dev
->mode_config
.tv_left_margin_property
)
1707 dev
->mode_config
.tv_right_margin_property
=
1708 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1709 if (!dev
->mode_config
.tv_right_margin_property
)
1712 dev
->mode_config
.tv_top_margin_property
=
1713 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1714 if (!dev
->mode_config
.tv_top_margin_property
)
1717 dev
->mode_config
.tv_bottom_margin_property
=
1718 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1719 if (!dev
->mode_config
.tv_bottom_margin_property
)
1722 dev
->mode_config
.tv_mode_property
=
1723 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1725 if (!dev
->mode_config
.tv_mode_property
)
1728 for (i
= 0; i
< num_modes
; i
++)
1729 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1732 dev
->mode_config
.tv_brightness_property
=
1733 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1734 if (!dev
->mode_config
.tv_brightness_property
)
1737 dev
->mode_config
.tv_contrast_property
=
1738 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1739 if (!dev
->mode_config
.tv_contrast_property
)
1742 dev
->mode_config
.tv_flicker_reduction_property
=
1743 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1744 if (!dev
->mode_config
.tv_flicker_reduction_property
)
1747 dev
->mode_config
.tv_overscan_property
=
1748 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1749 if (!dev
->mode_config
.tv_overscan_property
)
1752 dev
->mode_config
.tv_saturation_property
=
1753 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1754 if (!dev
->mode_config
.tv_saturation_property
)
1757 dev
->mode_config
.tv_hue_property
=
1758 drm_property_create_range(dev
, 0, "hue", 0, 100);
1759 if (!dev
->mode_config
.tv_hue_property
)
1766 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1769 * drm_mode_create_scaling_mode_property - create scaling mode property
1772 * Called by a driver the first time it's needed, must be attached to desired
1775 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1777 struct drm_property
*scaling_mode
;
1779 if (dev
->mode_config
.scaling_mode_property
)
1783 drm_property_create_enum(dev
, 0, "scaling mode",
1784 drm_scaling_mode_enum_list
,
1785 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1787 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1791 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1794 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1797 * Called by a driver the first time it's needed, must be attached to desired
1801 * Zero on success, negative errno on failure.
1803 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1805 if (dev
->mode_config
.aspect_ratio_property
)
1808 dev
->mode_config
.aspect_ratio_property
=
1809 drm_property_create_enum(dev
, 0, "aspect ratio",
1810 drm_aspect_ratio_enum_list
,
1811 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1813 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1818 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1821 * drm_mode_create_dirty_property - create dirty property
1824 * Called by a driver the first time it's needed, must be attached to desired
1827 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1829 struct drm_property
*dirty_info
;
1831 if (dev
->mode_config
.dirty_info_property
)
1835 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1837 drm_dirty_info_enum_list
,
1838 ARRAY_SIZE(drm_dirty_info_enum_list
));
1839 dev
->mode_config
.dirty_info_property
= dirty_info
;
1843 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1846 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1849 * Create the the suggested x/y offset property for connectors.
1851 int drm_mode_create_suggested_offset_properties(struct drm_device
*dev
)
1853 if (dev
->mode_config
.suggested_x_property
&& dev
->mode_config
.suggested_y_property
)
1856 dev
->mode_config
.suggested_x_property
=
1857 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested X", 0, 0xffffffff);
1859 dev
->mode_config
.suggested_y_property
=
1860 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested Y", 0, 0xffffffff);
1862 if (dev
->mode_config
.suggested_x_property
== NULL
||
1863 dev
->mode_config
.suggested_y_property
== NULL
)
1867 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties
);
1870 * drm_mode_getresources - get graphics configuration
1871 * @dev: drm device for the ioctl
1872 * @data: data pointer for the ioctl
1873 * @file_priv: drm file for the ioctl call
1875 * Construct a set of configuration description structures and return
1876 * them to the user, including CRTC, connector and framebuffer configuration.
1878 * Called by the user via ioctl.
1881 * Zero on success, negative errno on failure.
1883 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1884 struct drm_file
*file_priv
)
1886 struct drm_mode_card_res
*card_res
= data
;
1887 struct list_head
*lh
;
1888 struct drm_framebuffer
*fb
;
1889 struct drm_connector
*connector
;
1890 struct drm_crtc
*crtc
;
1891 struct drm_encoder
*encoder
;
1893 int connector_count
= 0;
1896 int encoder_count
= 0;
1898 uint32_t __user
*fb_id
;
1899 uint32_t __user
*crtc_id
;
1900 uint32_t __user
*connector_id
;
1901 uint32_t __user
*encoder_id
;
1903 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1907 mutex_lock(&file_priv
->fbs_lock
);
1909 * For the non-control nodes we need to limit the list of resources
1910 * by IDs in the group list for this node
1912 list_for_each(lh
, &file_priv
->fbs
)
1915 /* handle this in 4 parts */
1917 if (card_res
->count_fbs
>= fb_count
) {
1919 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1920 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1921 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1922 mutex_unlock(&file_priv
->fbs_lock
);
1928 card_res
->count_fbs
= fb_count
;
1929 mutex_unlock(&file_priv
->fbs_lock
);
1931 /* mode_config.mutex protects the connector list against e.g. DP MST
1932 * connector hot-adding. CRTC/Plane lists are invariant. */
1933 mutex_lock(&dev
->mode_config
.mutex
);
1934 drm_for_each_crtc(crtc
, dev
)
1937 drm_for_each_connector(connector
, dev
)
1940 drm_for_each_encoder(encoder
, dev
)
1943 card_res
->max_height
= dev
->mode_config
.max_height
;
1944 card_res
->min_height
= dev
->mode_config
.min_height
;
1945 card_res
->max_width
= dev
->mode_config
.max_width
;
1946 card_res
->min_width
= dev
->mode_config
.min_width
;
1949 if (card_res
->count_crtcs
>= crtc_count
) {
1951 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1952 drm_for_each_crtc(crtc
, dev
) {
1953 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1960 card_res
->count_crtcs
= crtc_count
;
1963 if (card_res
->count_encoders
>= encoder_count
) {
1965 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1966 drm_for_each_encoder(encoder
, dev
) {
1967 if (put_user(encoder
->base
.id
, encoder_id
+
1975 card_res
->count_encoders
= encoder_count
;
1978 if (card_res
->count_connectors
>= connector_count
) {
1980 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1981 drm_for_each_connector(connector
, dev
) {
1982 if (put_user(connector
->base
.id
,
1983 connector_id
+ copied
)) {
1990 card_res
->count_connectors
= connector_count
;
1993 mutex_unlock(&dev
->mode_config
.mutex
);
1998 * drm_mode_getcrtc - get CRTC configuration
1999 * @dev: drm device for the ioctl
2000 * @data: data pointer for the ioctl
2001 * @file_priv: drm file for the ioctl call
2003 * Construct a CRTC configuration structure to return to the user.
2005 * Called by the user via ioctl.
2008 * Zero on success, negative errno on failure.
2010 int drm_mode_getcrtc(struct drm_device
*dev
,
2011 void *data
, struct drm_file
*file_priv
)
2013 struct drm_mode_crtc
*crtc_resp
= data
;
2014 struct drm_crtc
*crtc
;
2016 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2019 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
2023 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
2024 crtc_resp
->gamma_size
= crtc
->gamma_size
;
2025 if (crtc
->primary
->fb
)
2026 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
2028 crtc_resp
->fb_id
= 0;
2031 crtc_resp
->x
= crtc
->primary
->state
->src_x
>> 16;
2032 crtc_resp
->y
= crtc
->primary
->state
->src_y
>> 16;
2033 if (crtc
->state
->enable
) {
2034 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->state
->mode
);
2035 crtc_resp
->mode_valid
= 1;
2038 crtc_resp
->mode_valid
= 0;
2041 crtc_resp
->x
= crtc
->x
;
2042 crtc_resp
->y
= crtc
->y
;
2043 if (crtc
->enabled
) {
2044 drm_mode_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
2045 crtc_resp
->mode_valid
= 1;
2048 crtc_resp
->mode_valid
= 0;
2051 drm_modeset_unlock_crtc(crtc
);
2056 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
2057 const struct drm_file
*file_priv
)
2060 * If user-space hasn't configured the driver to expose the stereo 3D
2061 * modes, don't expose them.
2063 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
2069 static struct drm_encoder
*drm_connector_get_encoder(struct drm_connector
*connector
)
2071 /* For atomic drivers only state objects are synchronously updated and
2072 * protected by modeset locks, so check those first. */
2073 if (connector
->state
)
2074 return connector
->state
->best_encoder
;
2075 return connector
->encoder
;
2078 /* helper for getconnector and getproperties ioctls */
2079 static int get_properties(struct drm_mode_object
*obj
, bool atomic
,
2080 uint32_t __user
*prop_ptr
, uint64_t __user
*prop_values
,
2081 uint32_t *arg_count_props
)
2086 props_count
= obj
->properties
->count
;
2088 props_count
-= obj
->properties
->atomic_count
;
2090 if ((*arg_count_props
>= props_count
) && props_count
) {
2091 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
2092 struct drm_property
*prop
= obj
->properties
->properties
[i
];
2095 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
2098 ret
= drm_object_property_get_value(obj
, prop
, &val
);
2102 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
2105 if (put_user(val
, prop_values
+ copied
))
2111 *arg_count_props
= props_count
;
2117 * drm_mode_getconnector - get connector configuration
2118 * @dev: drm device for the ioctl
2119 * @data: data pointer for the ioctl
2120 * @file_priv: drm file for the ioctl call
2122 * Construct a connector configuration structure to return to the user.
2124 * Called by the user via ioctl.
2127 * Zero on success, negative errno on failure.
2129 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
2130 struct drm_file
*file_priv
)
2132 struct drm_mode_get_connector
*out_resp
= data
;
2133 struct drm_connector
*connector
;
2134 struct drm_encoder
*encoder
;
2135 struct drm_display_mode
*mode
;
2137 int encoders_count
= 0;
2141 struct drm_mode_modeinfo u_mode
;
2142 struct drm_mode_modeinfo __user
*mode_ptr
;
2143 uint32_t __user
*encoder_ptr
;
2145 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2148 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
2150 mutex_lock(&dev
->mode_config
.mutex
);
2152 connector
= drm_connector_lookup(dev
, out_resp
->connector_id
);
2158 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++)
2159 if (connector
->encoder_ids
[i
] != 0)
2162 if (out_resp
->count_modes
== 0) {
2163 connector
->funcs
->fill_modes(connector
,
2164 dev
->mode_config
.max_width
,
2165 dev
->mode_config
.max_height
);
2168 /* delayed so we get modes regardless of pre-fill_modes state */
2169 list_for_each_entry(mode
, &connector
->modes
, head
)
2170 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2173 out_resp
->connector_id
= connector
->base
.id
;
2174 out_resp
->connector_type
= connector
->connector_type
;
2175 out_resp
->connector_type_id
= connector
->connector_type_id
;
2176 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2177 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2178 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2179 out_resp
->connection
= connector
->status
;
2181 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2182 encoder
= drm_connector_get_encoder(connector
);
2184 out_resp
->encoder_id
= encoder
->base
.id
;
2186 out_resp
->encoder_id
= 0;
2189 * This ioctl is called twice, once to determine how much space is
2190 * needed, and the 2nd time to fill it.
2192 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2194 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2195 list_for_each_entry(mode
, &connector
->modes
, head
) {
2196 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2199 drm_mode_convert_to_umode(&u_mode
, mode
);
2200 if (copy_to_user(mode_ptr
+ copied
,
2201 &u_mode
, sizeof(u_mode
))) {
2208 out_resp
->count_modes
= mode_count
;
2210 ret
= get_properties(&connector
->base
, file_priv
->atomic
,
2211 (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
),
2212 (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
),
2213 &out_resp
->count_props
);
2217 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2219 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2220 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2221 if (connector
->encoder_ids
[i
] != 0) {
2222 if (put_user(connector
->encoder_ids
[i
],
2223 encoder_ptr
+ copied
)) {
2231 out_resp
->count_encoders
= encoders_count
;
2234 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2236 drm_connector_unreference(connector
);
2238 mutex_unlock(&dev
->mode_config
.mutex
);
2243 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
2245 struct drm_connector
*connector
;
2246 struct drm_device
*dev
= encoder
->dev
;
2247 bool uses_atomic
= false;
2249 /* For atomic drivers only state objects are synchronously updated and
2250 * protected by modeset locks, so check those first. */
2251 drm_for_each_connector(connector
, dev
) {
2252 if (!connector
->state
)
2257 if (connector
->state
->best_encoder
!= encoder
)
2260 return connector
->state
->crtc
;
2263 /* Don't return stale data (e.g. pending async disable). */
2267 return encoder
->crtc
;
2271 * drm_mode_getencoder - get encoder configuration
2272 * @dev: drm device for the ioctl
2273 * @data: data pointer for the ioctl
2274 * @file_priv: drm file for the ioctl call
2276 * Construct a encoder configuration structure to return to the user.
2278 * Called by the user via ioctl.
2281 * Zero on success, negative errno on failure.
2283 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2284 struct drm_file
*file_priv
)
2286 struct drm_mode_get_encoder
*enc_resp
= data
;
2287 struct drm_encoder
*encoder
;
2288 struct drm_crtc
*crtc
;
2290 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2293 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2297 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2298 crtc
= drm_encoder_get_crtc(encoder
);
2300 enc_resp
->crtc_id
= crtc
->base
.id
;
2302 enc_resp
->crtc_id
= 0;
2303 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2305 enc_resp
->encoder_type
= encoder
->encoder_type
;
2306 enc_resp
->encoder_id
= encoder
->base
.id
;
2307 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2308 enc_resp
->possible_clones
= encoder
->possible_clones
;
2314 * drm_mode_getplane_res - enumerate all plane resources
2317 * @file_priv: DRM file info
2319 * Construct a list of plane ids to return to the user.
2321 * Called by the user via ioctl.
2324 * Zero on success, negative errno on failure.
2326 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2327 struct drm_file
*file_priv
)
2329 struct drm_mode_get_plane_res
*plane_resp
= data
;
2330 struct drm_mode_config
*config
;
2331 struct drm_plane
*plane
;
2332 uint32_t __user
*plane_ptr
;
2334 unsigned num_planes
;
2336 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2339 config
= &dev
->mode_config
;
2341 if (file_priv
->universal_planes
)
2342 num_planes
= config
->num_total_plane
;
2344 num_planes
= config
->num_overlay_plane
;
2347 * This ioctl is called twice, once to determine how much space is
2348 * needed, and the 2nd time to fill it.
2351 (plane_resp
->count_planes
>= num_planes
)) {
2352 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2354 /* Plane lists are invariant, no locking needed. */
2355 drm_for_each_plane(plane
, dev
) {
2357 * Unless userspace set the 'universal planes'
2358 * capability bit, only advertise overlays.
2360 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2361 !file_priv
->universal_planes
)
2364 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
2369 plane_resp
->count_planes
= num_planes
;
2375 * drm_mode_getplane - get plane configuration
2378 * @file_priv: DRM file info
2380 * Construct a plane configuration structure to return to the user.
2382 * Called by the user via ioctl.
2385 * Zero on success, negative errno on failure.
2387 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2388 struct drm_file
*file_priv
)
2390 struct drm_mode_get_plane
*plane_resp
= data
;
2391 struct drm_plane
*plane
;
2392 uint32_t __user
*format_ptr
;
2394 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2397 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2401 drm_modeset_lock(&plane
->mutex
, NULL
);
2403 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2405 plane_resp
->crtc_id
= 0;
2408 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2410 plane_resp
->fb_id
= 0;
2411 drm_modeset_unlock(&plane
->mutex
);
2413 plane_resp
->plane_id
= plane
->base
.id
;
2414 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2415 plane_resp
->gamma_size
= 0;
2418 * This ioctl is called twice, once to determine how much space is
2419 * needed, and the 2nd time to fill it.
2421 if (plane
->format_count
&&
2422 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2423 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2424 if (copy_to_user(format_ptr
,
2425 plane
->format_types
,
2426 sizeof(uint32_t) * plane
->format_count
)) {
2430 plane_resp
->count_format_types
= plane
->format_count
;
2436 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2437 * @plane: plane to check for format support
2438 * @format: the pixel format
2441 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2444 int drm_plane_check_pixel_format(const struct drm_plane
*plane
, u32 format
)
2448 for (i
= 0; i
< plane
->format_count
; i
++) {
2449 if (format
== plane
->format_types
[i
])
2456 static int check_src_coords(uint32_t src_x
, uint32_t src_y
,
2457 uint32_t src_w
, uint32_t src_h
,
2458 const struct drm_framebuffer
*fb
)
2460 unsigned int fb_width
, fb_height
;
2462 fb_width
= fb
->width
<< 16;
2463 fb_height
= fb
->height
<< 16;
2465 /* Make sure source coordinates are inside the fb. */
2466 if (src_w
> fb_width
||
2467 src_x
> fb_width
- src_w
||
2468 src_h
> fb_height
||
2469 src_y
> fb_height
- src_h
) {
2470 DRM_DEBUG_KMS("Invalid source coordinates "
2471 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2472 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2473 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2474 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2475 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2483 * setplane_internal - setplane handler for internal callers
2485 * Note that we assume an extra reference has already been taken on fb. If the
2486 * update fails, this reference will be dropped before return; if it succeeds,
2487 * the previous framebuffer (if any) will be unreferenced instead.
2489 * src_{x,y,w,h} are provided in 16.16 fixed point format
2491 static int __setplane_internal(struct drm_plane
*plane
,
2492 struct drm_crtc
*crtc
,
2493 struct drm_framebuffer
*fb
,
2494 int32_t crtc_x
, int32_t crtc_y
,
2495 uint32_t crtc_w
, uint32_t crtc_h
,
2496 /* src_{x,y,w,h} values are 16.16 fixed point */
2497 uint32_t src_x
, uint32_t src_y
,
2498 uint32_t src_w
, uint32_t src_h
)
2502 /* No fb means shut it down */
2504 plane
->old_fb
= plane
->fb
;
2505 ret
= plane
->funcs
->disable_plane(plane
);
2510 plane
->old_fb
= NULL
;
2515 /* Check whether this plane is usable on this CRTC */
2516 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2517 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2522 /* Check whether this plane supports the fb pixel format. */
2523 ret
= drm_plane_check_pixel_format(plane
, fb
->pixel_format
);
2525 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2526 drm_get_format_name(fb
->pixel_format
));
2530 /* Give drivers some help against integer overflows */
2531 if (crtc_w
> INT_MAX
||
2532 crtc_x
> INT_MAX
- (int32_t) crtc_w
||
2534 crtc_y
> INT_MAX
- (int32_t) crtc_h
) {
2535 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2536 crtc_w
, crtc_h
, crtc_x
, crtc_y
);
2541 ret
= check_src_coords(src_x
, src_y
, src_w
, src_h
, fb
);
2545 plane
->old_fb
= plane
->fb
;
2546 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2547 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2548 src_x
, src_y
, src_w
, src_h
);
2554 plane
->old_fb
= NULL
;
2559 drm_framebuffer_unreference(fb
);
2561 drm_framebuffer_unreference(plane
->old_fb
);
2562 plane
->old_fb
= NULL
;
2567 static int setplane_internal(struct drm_plane
*plane
,
2568 struct drm_crtc
*crtc
,
2569 struct drm_framebuffer
*fb
,
2570 int32_t crtc_x
, int32_t crtc_y
,
2571 uint32_t crtc_w
, uint32_t crtc_h
,
2572 /* src_{x,y,w,h} values are 16.16 fixed point */
2573 uint32_t src_x
, uint32_t src_y
,
2574 uint32_t src_w
, uint32_t src_h
)
2578 drm_modeset_lock_all(plane
->dev
);
2579 ret
= __setplane_internal(plane
, crtc
, fb
,
2580 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2581 src_x
, src_y
, src_w
, src_h
);
2582 drm_modeset_unlock_all(plane
->dev
);
2588 * drm_mode_setplane - configure a plane's configuration
2590 * @data: ioctl data*
2591 * @file_priv: DRM file info
2593 * Set plane configuration, including placement, fb, scaling, and other factors.
2594 * Or pass a NULL fb to disable (planes may be disabled without providing a
2598 * Zero on success, negative errno on failure.
2600 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2601 struct drm_file
*file_priv
)
2603 struct drm_mode_set_plane
*plane_req
= data
;
2604 struct drm_plane
*plane
;
2605 struct drm_crtc
*crtc
= NULL
;
2606 struct drm_framebuffer
*fb
= NULL
;
2608 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2612 * First, find the plane, crtc, and fb objects. If not available,
2613 * we don't bother to call the driver.
2615 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2617 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2618 plane_req
->plane_id
);
2622 if (plane_req
->fb_id
) {
2623 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2625 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2630 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2632 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2633 plane_req
->crtc_id
);
2639 * setplane_internal will take care of deref'ing either the old or new
2640 * framebuffer depending on success.
2642 return setplane_internal(plane
, crtc
, fb
,
2643 plane_req
->crtc_x
, plane_req
->crtc_y
,
2644 plane_req
->crtc_w
, plane_req
->crtc_h
,
2645 plane_req
->src_x
, plane_req
->src_y
,
2646 plane_req
->src_w
, plane_req
->src_h
);
2650 * drm_mode_set_config_internal - helper to call ->set_config
2651 * @set: modeset config to set
2653 * This is a little helper to wrap internal calls to the ->set_config driver
2654 * interface. The only thing it adds is correct refcounting dance.
2657 * Zero on success, negative errno on failure.
2659 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2661 struct drm_crtc
*crtc
= set
->crtc
;
2662 struct drm_framebuffer
*fb
;
2663 struct drm_crtc
*tmp
;
2667 * NOTE: ->set_config can also disable other crtcs (if we steal all
2668 * connectors from it), hence we need to refcount the fbs across all
2669 * crtcs. Atomic modeset will have saner semantics ...
2671 drm_for_each_crtc(tmp
, crtc
->dev
)
2672 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2676 ret
= crtc
->funcs
->set_config(set
);
2678 crtc
->primary
->crtc
= crtc
;
2679 crtc
->primary
->fb
= fb
;
2682 drm_for_each_crtc(tmp
, crtc
->dev
) {
2683 if (tmp
->primary
->fb
)
2684 drm_framebuffer_reference(tmp
->primary
->fb
);
2685 if (tmp
->primary
->old_fb
)
2686 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2687 tmp
->primary
->old_fb
= NULL
;
2692 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2695 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2696 * @mode: mode to query
2697 * @hdisplay: hdisplay value to fill in
2698 * @vdisplay: vdisplay value to fill in
2700 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2701 * the appropriate layout.
2703 void drm_crtc_get_hv_timing(const struct drm_display_mode
*mode
,
2704 int *hdisplay
, int *vdisplay
)
2706 struct drm_display_mode adjusted
;
2708 drm_mode_copy(&adjusted
, mode
);
2709 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE_ONLY
);
2710 *hdisplay
= adjusted
.crtc_hdisplay
;
2711 *vdisplay
= adjusted
.crtc_vdisplay
;
2713 EXPORT_SYMBOL(drm_crtc_get_hv_timing
);
2716 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2718 * @crtc: CRTC that framebuffer will be displayed on
2721 * @mode: mode that framebuffer will be displayed under
2722 * @fb: framebuffer to check size of
2724 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2726 const struct drm_display_mode
*mode
,
2727 const struct drm_framebuffer
*fb
)
2730 int hdisplay
, vdisplay
;
2732 drm_crtc_get_hv_timing(mode
, &hdisplay
, &vdisplay
);
2735 crtc
->primary
->state
->rotation
& (BIT(DRM_ROTATE_90
) |
2736 BIT(DRM_ROTATE_270
)))
2737 swap(hdisplay
, vdisplay
);
2739 return check_src_coords(x
<< 16, y
<< 16,
2740 hdisplay
<< 16, vdisplay
<< 16, fb
);
2742 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2745 * drm_mode_setcrtc - set CRTC configuration
2746 * @dev: drm device for the ioctl
2747 * @data: data pointer for the ioctl
2748 * @file_priv: drm file for the ioctl call
2750 * Build a new CRTC configuration based on user request.
2752 * Called by the user via ioctl.
2755 * Zero on success, negative errno on failure.
2757 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2758 struct drm_file
*file_priv
)
2760 struct drm_mode_config
*config
= &dev
->mode_config
;
2761 struct drm_mode_crtc
*crtc_req
= data
;
2762 struct drm_crtc
*crtc
;
2763 struct drm_connector
**connector_set
= NULL
, *connector
;
2764 struct drm_framebuffer
*fb
= NULL
;
2765 struct drm_display_mode
*mode
= NULL
;
2766 struct drm_mode_set set
;
2767 uint32_t __user
*set_connectors_ptr
;
2771 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2775 * Universal plane src offsets are only 16.16, prevent havoc for
2776 * drivers using universal plane code internally.
2778 if (crtc_req
->x
& 0xffff0000 || crtc_req
->y
& 0xffff0000)
2781 drm_modeset_lock_all(dev
);
2782 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2784 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2788 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc
->base
.id
, crtc
->name
);
2790 if (crtc_req
->mode_valid
) {
2791 /* If we have a mode we need a framebuffer. */
2792 /* If we pass -1, set the mode with the currently bound fb */
2793 if (crtc_req
->fb_id
== -1) {
2794 if (!crtc
->primary
->fb
) {
2795 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2799 fb
= crtc
->primary
->fb
;
2800 /* Make refcounting symmetric with the lookup path. */
2801 drm_framebuffer_reference(fb
);
2803 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2805 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2812 mode
= drm_mode_create(dev
);
2818 ret
= drm_mode_convert_umode(mode
, &crtc_req
->mode
);
2820 DRM_DEBUG_KMS("Invalid mode\n");
2824 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2827 * Check whether the primary plane supports the fb pixel format.
2828 * Drivers not implementing the universal planes API use a
2829 * default formats list provided by the DRM core which doesn't
2830 * match real hardware capabilities. Skip the check in that
2833 if (!crtc
->primary
->format_default
) {
2834 ret
= drm_plane_check_pixel_format(crtc
->primary
,
2837 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2838 drm_get_format_name(fb
->pixel_format
));
2843 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2850 if (crtc_req
->count_connectors
== 0 && mode
) {
2851 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2856 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2857 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2858 crtc_req
->count_connectors
);
2863 if (crtc_req
->count_connectors
> 0) {
2866 /* Avoid unbounded kernel memory allocation */
2867 if (crtc_req
->count_connectors
> config
->num_connector
) {
2872 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
2873 sizeof(struct drm_connector
*),
2875 if (!connector_set
) {
2880 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2881 connector_set
[i
] = NULL
;
2882 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2883 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2888 connector
= drm_connector_lookup(dev
, out_id
);
2890 DRM_DEBUG_KMS("Connector id %d unknown\n",
2895 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2899 connector_set
[i
] = connector
;
2904 set
.x
= crtc_req
->x
;
2905 set
.y
= crtc_req
->y
;
2907 set
.connectors
= connector_set
;
2908 set
.num_connectors
= crtc_req
->count_connectors
;
2910 ret
= drm_mode_set_config_internal(&set
);
2914 drm_framebuffer_unreference(fb
);
2916 if (connector_set
) {
2917 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2918 if (connector_set
[i
])
2919 drm_connector_unreference(connector_set
[i
]);
2922 kfree(connector_set
);
2923 drm_mode_destroy(dev
, mode
);
2924 drm_modeset_unlock_all(dev
);
2929 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2930 * universal plane handler call
2931 * @crtc: crtc to update cursor for
2932 * @req: data pointer for the ioctl
2933 * @file_priv: drm file for the ioctl call
2935 * Legacy cursor ioctl's work directly with driver buffer handles. To
2936 * translate legacy ioctl calls into universal plane handler calls, we need to
2937 * wrap the native buffer handle in a drm_framebuffer.
2939 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2940 * buffer with a pitch of 4*width; the universal plane interface should be used
2941 * directly in cases where the hardware can support other buffer settings and
2942 * userspace wants to make use of these capabilities.
2945 * Zero on success, negative errno on failure.
2947 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2948 struct drm_mode_cursor2
*req
,
2949 struct drm_file
*file_priv
)
2951 struct drm_device
*dev
= crtc
->dev
;
2952 struct drm_framebuffer
*fb
= NULL
;
2953 struct drm_mode_fb_cmd2 fbreq
= {
2954 .width
= req
->width
,
2955 .height
= req
->height
,
2956 .pixel_format
= DRM_FORMAT_ARGB8888
,
2957 .pitches
= { req
->width
* 4 },
2958 .handles
= { req
->handle
},
2960 int32_t crtc_x
, crtc_y
;
2961 uint32_t crtc_w
= 0, crtc_h
= 0;
2962 uint32_t src_w
= 0, src_h
= 0;
2965 BUG_ON(!crtc
->cursor
);
2966 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
2969 * Obtain fb we'll be using (either new or existing) and take an extra
2970 * reference to it if fb != null. setplane will take care of dropping
2971 * the reference if the plane update fails.
2973 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2975 fb
= internal_framebuffer_create(dev
, &fbreq
, file_priv
);
2977 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2984 fb
= crtc
->cursor
->fb
;
2986 drm_framebuffer_reference(fb
);
2989 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2993 crtc_x
= crtc
->cursor_x
;
2994 crtc_y
= crtc
->cursor_y
;
2999 crtc_h
= fb
->height
;
3000 src_w
= fb
->width
<< 16;
3001 src_h
= fb
->height
<< 16;
3005 * setplane_internal will take care of deref'ing either the old or new
3006 * framebuffer depending on success.
3008 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
3009 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
3010 0, 0, src_w
, src_h
);
3012 /* Update successful; save new cursor position, if necessary */
3013 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3014 crtc
->cursor_x
= req
->x
;
3015 crtc
->cursor_y
= req
->y
;
3021 static int drm_mode_cursor_common(struct drm_device
*dev
,
3022 struct drm_mode_cursor2
*req
,
3023 struct drm_file
*file_priv
)
3025 struct drm_crtc
*crtc
;
3028 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3031 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
3034 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
3036 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
3041 * If this crtc has a universal cursor plane, call that plane's update
3042 * handler rather than using legacy cursor handlers.
3044 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
3046 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
3050 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
3051 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
3055 /* Turns off the cursor if handle is 0 */
3056 if (crtc
->funcs
->cursor_set2
)
3057 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
3058 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
3060 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
3061 req
->width
, req
->height
);
3064 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
3065 if (crtc
->funcs
->cursor_move
) {
3066 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
3073 drm_modeset_unlock_crtc(crtc
);
3081 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3082 * @dev: drm device for the ioctl
3083 * @data: data pointer for the ioctl
3084 * @file_priv: drm file for the ioctl call
3086 * Set the cursor configuration based on user request.
3088 * Called by the user via ioctl.
3091 * Zero on success, negative errno on failure.
3093 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
3094 void *data
, struct drm_file
*file_priv
)
3096 struct drm_mode_cursor
*req
= data
;
3097 struct drm_mode_cursor2 new_req
;
3099 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
3100 new_req
.hot_x
= new_req
.hot_y
= 0;
3102 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
3106 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3107 * @dev: drm device for the ioctl
3108 * @data: data pointer for the ioctl
3109 * @file_priv: drm file for the ioctl call
3111 * Set the cursor configuration based on user request. This implements the 2nd
3112 * version of the cursor ioctl, which allows userspace to additionally specify
3113 * the hotspot of the pointer.
3115 * Called by the user via ioctl.
3118 * Zero on success, negative errno on failure.
3120 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
3121 void *data
, struct drm_file
*file_priv
)
3123 struct drm_mode_cursor2
*req
= data
;
3125 return drm_mode_cursor_common(dev
, req
, file_priv
);
3129 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3130 * @bpp: bits per pixels
3131 * @depth: bit depth per pixel
3133 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3134 * Useful in fbdev emulation code, since that deals in those values.
3136 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
3142 fmt
= DRM_FORMAT_C8
;
3146 fmt
= DRM_FORMAT_XRGB1555
;
3148 fmt
= DRM_FORMAT_RGB565
;
3151 fmt
= DRM_FORMAT_RGB888
;
3155 fmt
= DRM_FORMAT_XRGB8888
;
3156 else if (depth
== 30)
3157 fmt
= DRM_FORMAT_XRGB2101010
;
3159 fmt
= DRM_FORMAT_ARGB8888
;
3162 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3163 fmt
= DRM_FORMAT_XRGB8888
;
3169 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
3172 * drm_mode_addfb - add an FB to the graphics configuration
3173 * @dev: drm device for the ioctl
3174 * @data: data pointer for the ioctl
3175 * @file_priv: drm file for the ioctl call
3177 * Add a new FB to the specified CRTC, given a user request. This is the
3178 * original addfb ioctl which only supported RGB formats.
3180 * Called by the user via ioctl.
3183 * Zero on success, negative errno on failure.
3185 int drm_mode_addfb(struct drm_device
*dev
,
3186 void *data
, struct drm_file
*file_priv
)
3188 struct drm_mode_fb_cmd
*or = data
;
3189 struct drm_mode_fb_cmd2 r
= {};
3192 /* convert to new format and call new ioctl */
3193 r
.fb_id
= or->fb_id
;
3194 r
.width
= or->width
;
3195 r
.height
= or->height
;
3196 r
.pitches
[0] = or->pitch
;
3197 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
3198 r
.handles
[0] = or->handle
;
3200 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
3204 or->fb_id
= r
.fb_id
;
3209 static int format_check(const struct drm_mode_fb_cmd2
*r
)
3211 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
3215 case DRM_FORMAT_RGB332
:
3216 case DRM_FORMAT_BGR233
:
3217 case DRM_FORMAT_XRGB4444
:
3218 case DRM_FORMAT_XBGR4444
:
3219 case DRM_FORMAT_RGBX4444
:
3220 case DRM_FORMAT_BGRX4444
:
3221 case DRM_FORMAT_ARGB4444
:
3222 case DRM_FORMAT_ABGR4444
:
3223 case DRM_FORMAT_RGBA4444
:
3224 case DRM_FORMAT_BGRA4444
:
3225 case DRM_FORMAT_XRGB1555
:
3226 case DRM_FORMAT_XBGR1555
:
3227 case DRM_FORMAT_RGBX5551
:
3228 case DRM_FORMAT_BGRX5551
:
3229 case DRM_FORMAT_ARGB1555
:
3230 case DRM_FORMAT_ABGR1555
:
3231 case DRM_FORMAT_RGBA5551
:
3232 case DRM_FORMAT_BGRA5551
:
3233 case DRM_FORMAT_RGB565
:
3234 case DRM_FORMAT_BGR565
:
3235 case DRM_FORMAT_RGB888
:
3236 case DRM_FORMAT_BGR888
:
3237 case DRM_FORMAT_XRGB8888
:
3238 case DRM_FORMAT_XBGR8888
:
3239 case DRM_FORMAT_RGBX8888
:
3240 case DRM_FORMAT_BGRX8888
:
3241 case DRM_FORMAT_ARGB8888
:
3242 case DRM_FORMAT_ABGR8888
:
3243 case DRM_FORMAT_RGBA8888
:
3244 case DRM_FORMAT_BGRA8888
:
3245 case DRM_FORMAT_XRGB2101010
:
3246 case DRM_FORMAT_XBGR2101010
:
3247 case DRM_FORMAT_RGBX1010102
:
3248 case DRM_FORMAT_BGRX1010102
:
3249 case DRM_FORMAT_ARGB2101010
:
3250 case DRM_FORMAT_ABGR2101010
:
3251 case DRM_FORMAT_RGBA1010102
:
3252 case DRM_FORMAT_BGRA1010102
:
3253 case DRM_FORMAT_YUYV
:
3254 case DRM_FORMAT_YVYU
:
3255 case DRM_FORMAT_UYVY
:
3256 case DRM_FORMAT_VYUY
:
3257 case DRM_FORMAT_AYUV
:
3258 case DRM_FORMAT_NV12
:
3259 case DRM_FORMAT_NV21
:
3260 case DRM_FORMAT_NV16
:
3261 case DRM_FORMAT_NV61
:
3262 case DRM_FORMAT_NV24
:
3263 case DRM_FORMAT_NV42
:
3264 case DRM_FORMAT_YUV410
:
3265 case DRM_FORMAT_YVU410
:
3266 case DRM_FORMAT_YUV411
:
3267 case DRM_FORMAT_YVU411
:
3268 case DRM_FORMAT_YUV420
:
3269 case DRM_FORMAT_YVU420
:
3270 case DRM_FORMAT_YUV422
:
3271 case DRM_FORMAT_YVU422
:
3272 case DRM_FORMAT_YUV444
:
3273 case DRM_FORMAT_YVU444
:
3276 DRM_DEBUG_KMS("invalid pixel format %s\n",
3277 drm_get_format_name(r
->pixel_format
));
3282 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3284 int ret
, hsub
, vsub
, num_planes
, i
;
3286 ret
= format_check(r
);
3288 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3289 drm_get_format_name(r
->pixel_format
));
3293 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3294 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3295 num_planes
= drm_format_num_planes(r
->pixel_format
);
3297 if (r
->width
== 0 || r
->width
% hsub
) {
3298 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
3302 if (r
->height
== 0 || r
->height
% vsub
) {
3303 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3307 for (i
= 0; i
< num_planes
; i
++) {
3308 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3309 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3310 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3312 if (!r
->handles
[i
]) {
3313 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3317 if ((uint64_t) width
* cpp
> UINT_MAX
)
3320 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3323 if (r
->pitches
[i
] < width
* cpp
) {
3324 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3328 if (r
->modifier
[i
] && !(r
->flags
& DRM_MODE_FB_MODIFIERS
)) {
3329 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3334 /* modifier specific checks: */
3335 switch (r
->modifier
[i
]) {
3336 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE
:
3337 /* NOTE: the pitch restriction may be lifted later if it turns
3338 * out that no hw has this restriction:
3340 if (r
->pixel_format
!= DRM_FORMAT_NV12
||
3341 width
% 128 || height
% 32 ||
3342 r
->pitches
[i
] % 128) {
3343 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i
);
3353 for (i
= num_planes
; i
< 4; i
++) {
3354 if (r
->modifier
[i
]) {
3355 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i
);
3359 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3360 if (!(r
->flags
& DRM_MODE_FB_MODIFIERS
))
3363 if (r
->handles
[i
]) {
3364 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i
);
3368 if (r
->pitches
[i
]) {
3369 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i
);
3373 if (r
->offsets
[i
]) {
3374 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i
);
3382 static struct drm_framebuffer
*
3383 internal_framebuffer_create(struct drm_device
*dev
,
3384 const struct drm_mode_fb_cmd2
*r
,
3385 struct drm_file
*file_priv
)
3387 struct drm_mode_config
*config
= &dev
->mode_config
;
3388 struct drm_framebuffer
*fb
;
3391 if (r
->flags
& ~(DRM_MODE_FB_INTERLACED
| DRM_MODE_FB_MODIFIERS
)) {
3392 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3393 return ERR_PTR(-EINVAL
);
3396 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3397 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3398 r
->width
, config
->min_width
, config
->max_width
);
3399 return ERR_PTR(-EINVAL
);
3401 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3402 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3403 r
->height
, config
->min_height
, config
->max_height
);
3404 return ERR_PTR(-EINVAL
);
3407 if (r
->flags
& DRM_MODE_FB_MODIFIERS
&&
3408 !dev
->mode_config
.allow_fb_modifiers
) {
3409 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3410 return ERR_PTR(-EINVAL
);
3413 ret
= framebuffer_check(r
);
3415 return ERR_PTR(ret
);
3417 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3419 DRM_DEBUG_KMS("could not create framebuffer\n");
3427 * drm_mode_addfb2 - add an FB to the graphics configuration
3428 * @dev: drm device for the ioctl
3429 * @data: data pointer for the ioctl
3430 * @file_priv: drm file for the ioctl call
3432 * Add a new FB to the specified CRTC, given a user request with format. This is
3433 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3434 * and uses fourcc codes as pixel format specifiers.
3436 * Called by the user via ioctl.
3439 * Zero on success, negative errno on failure.
3441 int drm_mode_addfb2(struct drm_device
*dev
,
3442 void *data
, struct drm_file
*file_priv
)
3444 struct drm_mode_fb_cmd2
*r
= data
;
3445 struct drm_framebuffer
*fb
;
3447 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3450 fb
= internal_framebuffer_create(dev
, r
, file_priv
);
3454 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3455 r
->fb_id
= fb
->base
.id
;
3457 /* Transfer ownership to the filp for reaping on close */
3458 mutex_lock(&file_priv
->fbs_lock
);
3459 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3460 mutex_unlock(&file_priv
->fbs_lock
);
3465 struct drm_mode_rmfb_work
{
3466 struct work_struct work
;
3467 struct list_head fbs
;
3470 static void drm_mode_rmfb_work_fn(struct work_struct
*w
)
3472 struct drm_mode_rmfb_work
*arg
= container_of(w
, typeof(*arg
), work
);
3474 while (!list_empty(&arg
->fbs
)) {
3475 struct drm_framebuffer
*fb
=
3476 list_first_entry(&arg
->fbs
, typeof(*fb
), filp_head
);
3478 list_del_init(&fb
->filp_head
);
3479 drm_framebuffer_remove(fb
);
3484 * drm_mode_rmfb - remove an FB from the configuration
3485 * @dev: drm device for the ioctl
3486 * @data: data pointer for the ioctl
3487 * @file_priv: drm file for the ioctl call
3489 * Remove the FB specified by the user.
3491 * Called by the user via ioctl.
3494 * Zero on success, negative errno on failure.
3496 int drm_mode_rmfb(struct drm_device
*dev
,
3497 void *data
, struct drm_file
*file_priv
)
3499 struct drm_framebuffer
*fb
= NULL
;
3500 struct drm_framebuffer
*fbl
= NULL
;
3501 uint32_t *id
= data
;
3504 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3507 fb
= drm_framebuffer_lookup(dev
, *id
);
3511 mutex_lock(&file_priv
->fbs_lock
);
3512 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3516 mutex_unlock(&file_priv
->fbs_lock
);
3520 list_del_init(&fb
->filp_head
);
3521 mutex_unlock(&file_priv
->fbs_lock
);
3523 /* drop the reference we picked up in framebuffer lookup */
3524 drm_framebuffer_unreference(fb
);
3527 * we now own the reference that was stored in the fbs list
3529 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3530 * so run this in a separate stack as there's no way to correctly
3531 * handle this after the fb is already removed from the lookup table.
3533 if (drm_framebuffer_read_refcount(fb
) > 1) {
3534 struct drm_mode_rmfb_work arg
;
3536 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3537 INIT_LIST_HEAD(&arg
.fbs
);
3538 list_add_tail(&fb
->filp_head
, &arg
.fbs
);
3540 schedule_work(&arg
.work
);
3541 flush_work(&arg
.work
);
3542 destroy_work_on_stack(&arg
.work
);
3544 drm_framebuffer_unreference(fb
);
3549 drm_framebuffer_unreference(fb
);
3554 * drm_mode_getfb - get FB info
3555 * @dev: drm device for the ioctl
3556 * @data: data pointer for the ioctl
3557 * @file_priv: drm file for the ioctl call
3559 * Lookup the FB given its ID and return info about it.
3561 * Called by the user via ioctl.
3564 * Zero on success, negative errno on failure.
3566 int drm_mode_getfb(struct drm_device
*dev
,
3567 void *data
, struct drm_file
*file_priv
)
3569 struct drm_mode_fb_cmd
*r
= data
;
3570 struct drm_framebuffer
*fb
;
3573 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3576 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3580 r
->height
= fb
->height
;
3581 r
->width
= fb
->width
;
3582 r
->depth
= fb
->depth
;
3583 r
->bpp
= fb
->bits_per_pixel
;
3584 r
->pitch
= fb
->pitches
[0];
3585 if (fb
->funcs
->create_handle
) {
3586 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3587 drm_is_control_client(file_priv
)) {
3588 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3591 /* GET_FB() is an unprivileged ioctl so we must not
3592 * return a buffer-handle to non-master processes! For
3593 * backwards-compatibility reasons, we cannot make
3594 * GET_FB() privileged, so just return an invalid handle
3595 * for non-masters. */
3603 drm_framebuffer_unreference(fb
);
3609 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3610 * @dev: drm device for the ioctl
3611 * @data: data pointer for the ioctl
3612 * @file_priv: drm file for the ioctl call
3614 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3615 * rectangle list. Generic userspace which does frontbuffer rendering must call
3616 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3617 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3619 * Modesetting drivers which always update the frontbuffer do not need to
3620 * implement the corresponding ->dirty framebuffer callback.
3622 * Called by the user via ioctl.
3625 * Zero on success, negative errno on failure.
3627 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3628 void *data
, struct drm_file
*file_priv
)
3630 struct drm_clip_rect __user
*clips_ptr
;
3631 struct drm_clip_rect
*clips
= NULL
;
3632 struct drm_mode_fb_dirty_cmd
*r
= data
;
3633 struct drm_framebuffer
*fb
;
3638 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3641 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3645 num_clips
= r
->num_clips
;
3646 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3648 if (!num_clips
!= !clips_ptr
) {
3653 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3655 /* If userspace annotates copy, clips must come in pairs */
3656 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3661 if (num_clips
&& clips_ptr
) {
3662 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3666 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
3672 ret
= copy_from_user(clips
, clips_ptr
,
3673 num_clips
* sizeof(*clips
));
3680 if (fb
->funcs
->dirty
) {
3681 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3690 drm_framebuffer_unreference(fb
);
3696 * drm_fb_release - remove and free the FBs on this file
3697 * @priv: drm file for the ioctl
3699 * Destroy all the FBs associated with @filp.
3701 * Called by the user via ioctl.
3704 * Zero on success, negative errno on failure.
3706 void drm_fb_release(struct drm_file
*priv
)
3708 struct drm_framebuffer
*fb
, *tfb
;
3709 struct drm_mode_rmfb_work arg
;
3711 INIT_LIST_HEAD(&arg
.fbs
);
3714 * When the file gets released that means no one else can access the fb
3715 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3716 * avoid upsetting lockdep since the universal cursor code adds a
3717 * framebuffer while holding mutex locks.
3719 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3720 * locks is impossible here since no one else but this function can get
3723 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3724 if (drm_framebuffer_read_refcount(fb
) > 1) {
3725 list_move_tail(&fb
->filp_head
, &arg
.fbs
);
3727 list_del_init(&fb
->filp_head
);
3729 /* This drops the fpriv->fbs reference. */
3730 drm_framebuffer_unreference(fb
);
3734 if (!list_empty(&arg
.fbs
)) {
3735 INIT_WORK_ONSTACK(&arg
.work
, drm_mode_rmfb_work_fn
);
3737 schedule_work(&arg
.work
);
3738 flush_work(&arg
.work
);
3739 destroy_work_on_stack(&arg
.work
);
3744 * drm_property_create - create a new property type
3746 * @flags: flags specifying the property type
3747 * @name: name of the property
3748 * @num_values: number of pre-defined values
3750 * This creates a new generic drm property which can then be attached to a drm
3751 * object with drm_object_attach_property. The returned property object must be
3752 * freed with drm_property_destroy.
3754 * Note that the DRM core keeps a per-device list of properties and that, if
3755 * drm_mode_config_cleanup() is called, it will destroy all properties created
3759 * A pointer to the newly created property on success, NULL on failure.
3761 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3762 const char *name
, int num_values
)
3764 struct drm_property
*property
= NULL
;
3767 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3771 property
->dev
= dev
;
3774 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
3776 if (!property
->values
)
3780 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3784 property
->flags
= flags
;
3785 property
->num_values
= num_values
;
3786 INIT_LIST_HEAD(&property
->enum_list
);
3789 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3790 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3793 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3795 WARN_ON(!drm_property_type_valid(property
));
3799 kfree(property
->values
);
3803 EXPORT_SYMBOL(drm_property_create
);
3806 * drm_property_create_enum - create a new enumeration property type
3808 * @flags: flags specifying the property type
3809 * @name: name of the property
3810 * @props: enumeration lists with property values
3811 * @num_values: number of pre-defined values
3813 * This creates a new generic drm property which can then be attached to a drm
3814 * object with drm_object_attach_property. The returned property object must be
3815 * freed with drm_property_destroy.
3817 * Userspace is only allowed to set one of the predefined values for enumeration
3821 * A pointer to the newly created property on success, NULL on failure.
3823 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3825 const struct drm_prop_enum_list
*props
,
3828 struct drm_property
*property
;
3831 flags
|= DRM_MODE_PROP_ENUM
;
3833 property
= drm_property_create(dev
, flags
, name
, num_values
);
3837 for (i
= 0; i
< num_values
; i
++) {
3838 ret
= drm_property_add_enum(property
, i
,
3842 drm_property_destroy(dev
, property
);
3849 EXPORT_SYMBOL(drm_property_create_enum
);
3852 * drm_property_create_bitmask - create a new bitmask property type
3854 * @flags: flags specifying the property type
3855 * @name: name of the property
3856 * @props: enumeration lists with property bitflags
3857 * @num_props: size of the @props array
3858 * @supported_bits: bitmask of all supported enumeration values
3860 * This creates a new bitmask drm property which can then be attached to a drm
3861 * object with drm_object_attach_property. The returned property object must be
3862 * freed with drm_property_destroy.
3864 * Compared to plain enumeration properties userspace is allowed to set any
3865 * or'ed together combination of the predefined property bitflag values
3868 * A pointer to the newly created property on success, NULL on failure.
3870 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3871 int flags
, const char *name
,
3872 const struct drm_prop_enum_list
*props
,
3874 uint64_t supported_bits
)
3876 struct drm_property
*property
;
3877 int i
, ret
, index
= 0;
3878 int num_values
= hweight64(supported_bits
);
3880 flags
|= DRM_MODE_PROP_BITMASK
;
3882 property
= drm_property_create(dev
, flags
, name
, num_values
);
3885 for (i
= 0; i
< num_props
; i
++) {
3886 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3889 if (WARN_ON(index
>= num_values
)) {
3890 drm_property_destroy(dev
, property
);
3894 ret
= drm_property_add_enum(property
, index
++,
3898 drm_property_destroy(dev
, property
);
3905 EXPORT_SYMBOL(drm_property_create_bitmask
);
3907 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3908 int flags
, const char *name
,
3909 uint64_t min
, uint64_t max
)
3911 struct drm_property
*property
;
3913 property
= drm_property_create(dev
, flags
, name
, 2);
3917 property
->values
[0] = min
;
3918 property
->values
[1] = max
;
3924 * drm_property_create_range - create a new unsigned ranged property type
3926 * @flags: flags specifying the property type
3927 * @name: name of the property
3928 * @min: minimum value of the property
3929 * @max: maximum value of the property
3931 * This creates a new generic drm property which can then be attached to a drm
3932 * object with drm_object_attach_property. The returned property object must be
3933 * freed with drm_property_destroy.
3935 * Userspace is allowed to set any unsigned integer value in the (min, max)
3939 * A pointer to the newly created property on success, NULL on failure.
3941 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3943 uint64_t min
, uint64_t max
)
3945 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3948 EXPORT_SYMBOL(drm_property_create_range
);
3951 * drm_property_create_signed_range - create a new signed ranged property type
3953 * @flags: flags specifying the property type
3954 * @name: name of the property
3955 * @min: minimum value of the property
3956 * @max: maximum value of the property
3958 * This creates a new generic drm property which can then be attached to a drm
3959 * object with drm_object_attach_property. The returned property object must be
3960 * freed with drm_property_destroy.
3962 * Userspace is allowed to set any signed integer value in the (min, max)
3966 * A pointer to the newly created property on success, NULL on failure.
3968 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3969 int flags
, const char *name
,
3970 int64_t min
, int64_t max
)
3972 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3973 name
, I642U64(min
), I642U64(max
));
3975 EXPORT_SYMBOL(drm_property_create_signed_range
);
3978 * drm_property_create_object - create a new object property type
3980 * @flags: flags specifying the property type
3981 * @name: name of the property
3982 * @type: object type from DRM_MODE_OBJECT_* defines
3984 * This creates a new generic drm property which can then be attached to a drm
3985 * object with drm_object_attach_property. The returned property object must be
3986 * freed with drm_property_destroy.
3988 * Userspace is only allowed to set this to any property value of the given
3989 * @type. Only useful for atomic properties, which is enforced.
3992 * A pointer to the newly created property on success, NULL on failure.
3994 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3995 int flags
, const char *name
, uint32_t type
)
3997 struct drm_property
*property
;
3999 flags
|= DRM_MODE_PROP_OBJECT
;
4001 if (WARN_ON(!(flags
& DRM_MODE_PROP_ATOMIC
)))
4004 property
= drm_property_create(dev
, flags
, name
, 1);
4008 property
->values
[0] = type
;
4012 EXPORT_SYMBOL(drm_property_create_object
);
4015 * drm_property_create_bool - create a new boolean property type
4017 * @flags: flags specifying the property type
4018 * @name: name of the property
4020 * This creates a new generic drm property which can then be attached to a drm
4021 * object with drm_object_attach_property. The returned property object must be
4022 * freed with drm_property_destroy.
4024 * This is implemented as a ranged property with only {0, 1} as valid values.
4027 * A pointer to the newly created property on success, NULL on failure.
4029 struct drm_property
*drm_property_create_bool(struct drm_device
*dev
, int flags
,
4032 return drm_property_create_range(dev
, flags
, name
, 0, 1);
4034 EXPORT_SYMBOL(drm_property_create_bool
);
4037 * drm_property_add_enum - add a possible value to an enumeration property
4038 * @property: enumeration property to change
4039 * @index: index of the new enumeration
4040 * @value: value of the new enumeration
4041 * @name: symbolic name of the new enumeration
4043 * This functions adds enumerations to a property.
4045 * It's use is deprecated, drivers should use one of the more specific helpers
4046 * to directly create the property with all enumerations already attached.
4049 * Zero on success, error code on failure.
4051 int drm_property_add_enum(struct drm_property
*property
, int index
,
4052 uint64_t value
, const char *name
)
4054 struct drm_property_enum
*prop_enum
;
4056 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4057 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
4061 * Bitmask enum properties have the additional constraint of values
4064 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
4068 if (!list_empty(&property
->enum_list
)) {
4069 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4070 if (prop_enum
->value
== value
) {
4071 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
4072 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
4078 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
4082 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
4083 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
4084 prop_enum
->value
= value
;
4086 property
->values
[index
] = value
;
4087 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
4090 EXPORT_SYMBOL(drm_property_add_enum
);
4093 * drm_property_destroy - destroy a drm property
4095 * @property: property to destry
4097 * This function frees a property including any attached resources like
4098 * enumeration values.
4100 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
4102 struct drm_property_enum
*prop_enum
, *pt
;
4104 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
4105 list_del(&prop_enum
->head
);
4109 if (property
->num_values
)
4110 kfree(property
->values
);
4111 drm_mode_object_unregister(dev
, &property
->base
);
4112 list_del(&property
->head
);
4115 EXPORT_SYMBOL(drm_property_destroy
);
4118 * drm_object_attach_property - attach a property to a modeset object
4119 * @obj: drm modeset object
4120 * @property: property to attach
4121 * @init_val: initial value of the property
4123 * This attaches the given property to the modeset object with the given initial
4124 * value. Currently this function cannot fail since the properties are stored in
4125 * a statically sized array.
4127 void drm_object_attach_property(struct drm_mode_object
*obj
,
4128 struct drm_property
*property
,
4131 int count
= obj
->properties
->count
;
4133 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
4134 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4135 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4136 "you see this message on the same object type.\n",
4141 obj
->properties
->properties
[count
] = property
;
4142 obj
->properties
->values
[count
] = init_val
;
4143 obj
->properties
->count
++;
4144 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
4145 obj
->properties
->atomic_count
++;
4147 EXPORT_SYMBOL(drm_object_attach_property
);
4150 * drm_object_property_set_value - set the value of a property
4151 * @obj: drm mode object to set property value for
4152 * @property: property to set
4153 * @val: value the property should be set to
4155 * This functions sets a given property on a given object. This function only
4156 * changes the software state of the property, it does not call into the
4157 * driver's ->set_property callback.
4160 * Zero on success, error code on failure.
4162 int drm_object_property_set_value(struct drm_mode_object
*obj
,
4163 struct drm_property
*property
, uint64_t val
)
4167 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4168 if (obj
->properties
->properties
[i
] == property
) {
4169 obj
->properties
->values
[i
] = val
;
4176 EXPORT_SYMBOL(drm_object_property_set_value
);
4179 * drm_object_property_get_value - retrieve the value of a property
4180 * @obj: drm mode object to get property value from
4181 * @property: property to retrieve
4182 * @val: storage for the property value
4184 * This function retrieves the softare state of the given property for the given
4185 * property. Since there is no driver callback to retrieve the current property
4186 * value this might be out of sync with the hardware, depending upon the driver
4190 * Zero on success, error code on failure.
4192 int drm_object_property_get_value(struct drm_mode_object
*obj
,
4193 struct drm_property
*property
, uint64_t *val
)
4197 /* read-only properties bypass atomic mechanism and still store
4198 * their value in obj->properties->values[].. mostly to avoid
4199 * having to deal w/ EDID and similar props in atomic paths:
4201 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
4202 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
4203 return drm_atomic_get_property(obj
, property
, val
);
4205 for (i
= 0; i
< obj
->properties
->count
; i
++) {
4206 if (obj
->properties
->properties
[i
] == property
) {
4207 *val
= obj
->properties
->values
[i
];
4214 EXPORT_SYMBOL(drm_object_property_get_value
);
4217 * drm_mode_getproperty_ioctl - get the property metadata
4220 * @file_priv: DRM file info
4222 * This function retrieves the metadata for a given property, like the different
4223 * possible values for an enum property or the limits for a range property.
4225 * Blob properties are special
4227 * Called by the user via ioctl.
4230 * Zero on success, negative errno on failure.
4232 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
4233 void *data
, struct drm_file
*file_priv
)
4235 struct drm_mode_get_property
*out_resp
= data
;
4236 struct drm_property
*property
;
4238 int value_count
= 0;
4241 struct drm_property_enum
*prop_enum
;
4242 struct drm_mode_property_enum __user
*enum_ptr
;
4243 uint64_t __user
*values_ptr
;
4245 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4248 drm_modeset_lock_all(dev
);
4249 property
= drm_property_find(dev
, out_resp
->prop_id
);
4255 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4256 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4257 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
4261 value_count
= property
->num_values
;
4263 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
4264 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
4265 out_resp
->flags
= property
->flags
;
4267 if ((out_resp
->count_values
>= value_count
) && value_count
) {
4268 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
4269 for (i
= 0; i
< value_count
; i
++) {
4270 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
4276 out_resp
->count_values
= value_count
;
4278 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
4279 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4280 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
4282 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
4283 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
4285 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
4290 if (copy_to_user(&enum_ptr
[copied
].name
,
4291 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
4298 out_resp
->count_enum_blobs
= enum_count
;
4302 * NOTE: The idea seems to have been to use this to read all the blob
4303 * property values. But nothing ever added them to the corresponding
4304 * list, userspace always used the special-purpose get_blob ioctl to
4305 * read the value for a blob property. It also doesn't make a lot of
4306 * sense to return values here when everything else is just metadata for
4307 * the property itself.
4309 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4310 out_resp
->count_enum_blobs
= 0;
4312 drm_modeset_unlock_all(dev
);
4316 static void drm_property_free_blob(struct kref
*kref
)
4318 struct drm_property_blob
*blob
=
4319 container_of(kref
, struct drm_property_blob
, base
.refcount
);
4321 mutex_lock(&blob
->dev
->mode_config
.blob_lock
);
4322 list_del(&blob
->head_global
);
4323 mutex_unlock(&blob
->dev
->mode_config
.blob_lock
);
4325 drm_mode_object_unregister(blob
->dev
, &blob
->base
);
4331 * drm_property_create_blob - Create new blob property
4333 * Creates a new blob property for a specified DRM device, optionally
4336 * @dev: DRM device to create property for
4337 * @length: Length to allocate for blob data
4338 * @data: If specified, copies data into blob
4341 * New blob property with a single reference on success, or an ERR_PTR
4344 struct drm_property_blob
*
4345 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
4348 struct drm_property_blob
*blob
;
4351 if (!length
|| length
> ULONG_MAX
- sizeof(struct drm_property_blob
))
4352 return ERR_PTR(-EINVAL
);
4354 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
4356 return ERR_PTR(-ENOMEM
);
4358 /* This must be explicitly initialised, so we can safely call list_del
4359 * on it in the removal handler, even if it isn't in a file list. */
4360 INIT_LIST_HEAD(&blob
->head_file
);
4361 blob
->length
= length
;
4365 memcpy(blob
->data
, data
, length
);
4367 ret
= drm_mode_object_get_reg(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
,
4368 true, drm_property_free_blob
);
4371 return ERR_PTR(-EINVAL
);
4374 mutex_lock(&dev
->mode_config
.blob_lock
);
4375 list_add_tail(&blob
->head_global
,
4376 &dev
->mode_config
.property_blob_list
);
4377 mutex_unlock(&dev
->mode_config
.blob_lock
);
4381 EXPORT_SYMBOL(drm_property_create_blob
);
4384 * drm_property_unreference_blob - Unreference a blob property
4386 * Drop a reference on a blob property. May free the object.
4388 * @blob: Pointer to blob property
4390 void drm_property_unreference_blob(struct drm_property_blob
*blob
)
4395 drm_mode_object_unreference(&blob
->base
);
4397 EXPORT_SYMBOL(drm_property_unreference_blob
);
4400 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4402 * @file_priv: destroy all blobs owned by this file handle
4404 void drm_property_destroy_user_blobs(struct drm_device
*dev
,
4405 struct drm_file
*file_priv
)
4407 struct drm_property_blob
*blob
, *bt
;
4410 * When the file gets released that means no one else can access the
4411 * blob list any more, so no need to grab dev->blob_lock.
4413 list_for_each_entry_safe(blob
, bt
, &file_priv
->blobs
, head_file
) {
4414 list_del_init(&blob
->head_file
);
4415 drm_property_unreference_blob(blob
);
4420 * drm_property_reference_blob - Take a reference on an existing property
4422 * Take a new reference on an existing blob property.
4424 * @blob: Pointer to blob property
4426 struct drm_property_blob
*drm_property_reference_blob(struct drm_property_blob
*blob
)
4428 drm_mode_object_reference(&blob
->base
);
4431 EXPORT_SYMBOL(drm_property_reference_blob
);
4434 * drm_property_lookup_blob - look up a blob property and take a reference
4436 * @id: id of the blob property
4438 * If successful, this takes an additional reference to the blob property.
4439 * callers need to make sure to eventually unreference the returned property
4440 * again, using @drm_property_unreference_blob.
4442 struct drm_property_blob
*drm_property_lookup_blob(struct drm_device
*dev
,
4445 struct drm_mode_object
*obj
;
4446 struct drm_property_blob
*blob
= NULL
;
4448 obj
= _object_find(dev
, id
, DRM_MODE_OBJECT_BLOB
);
4450 blob
= obj_to_blob(obj
);
4453 EXPORT_SYMBOL(drm_property_lookup_blob
);
4456 * drm_property_replace_global_blob - atomically replace existing blob property
4458 * @replace: location of blob property pointer to be replaced
4459 * @length: length of data for new blob, or 0 for no data
4460 * @data: content for new blob, or NULL for no data
4461 * @obj_holds_id: optional object for property holding blob ID
4462 * @prop_holds_id: optional property holding blob ID
4463 * @return 0 on success or error on failure
4465 * This function will atomically replace a global property in the blob list,
4466 * optionally updating a property which holds the ID of that property. It is
4467 * guaranteed to be atomic: no caller will be allowed to see intermediate
4468 * results, and either the entire operation will succeed and clean up the
4469 * previous property, or it will fail and the state will be unchanged.
4471 * If length is 0 or data is NULL, no new blob will be created, and the holding
4472 * property, if specified, will be set to 0.
4474 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4475 * by holding the relevant modesetting object lock for its parent.
4477 * For example, a drm_connector has a 'PATH' property, which contains the ID
4478 * of a blob property with the value of the MST path information. Calling this
4479 * function with replace pointing to the connector's path_blob_ptr, length and
4480 * data set for the new path information, obj_holds_id set to the connector's
4481 * base object, and prop_holds_id set to the path property name, will perform
4482 * a completely atomic update. The access to path_blob_ptr is protected by the
4483 * caller holding a lock on the connector.
4485 static int drm_property_replace_global_blob(struct drm_device
*dev
,
4486 struct drm_property_blob
**replace
,
4489 struct drm_mode_object
*obj_holds_id
,
4490 struct drm_property
*prop_holds_id
)
4492 struct drm_property_blob
*new_blob
= NULL
;
4493 struct drm_property_blob
*old_blob
= NULL
;
4496 WARN_ON(replace
== NULL
);
4498 old_blob
= *replace
;
4500 if (length
&& data
) {
4501 new_blob
= drm_property_create_blob(dev
, length
, data
);
4502 if (IS_ERR(new_blob
))
4503 return PTR_ERR(new_blob
);
4506 /* This does not need to be synchronised with blob_lock, as the
4507 * get_properties ioctl locks all modesetting objects, and
4508 * obj_holds_id must be locked before calling here, so we cannot
4509 * have its value out of sync with the list membership modified
4510 * below under blob_lock. */
4512 ret
= drm_object_property_set_value(obj_holds_id
,
4515 new_blob
->base
.id
: 0);
4520 drm_property_unreference_blob(old_blob
);
4521 *replace
= new_blob
;
4526 drm_property_unreference_blob(new_blob
);
4531 * drm_mode_getblob_ioctl - get the contents of a blob property value
4534 * @file_priv: DRM file info
4536 * This function retrieves the contents of a blob property. The value stored in
4537 * an object's blob property is just a normal modeset object id.
4539 * Called by the user via ioctl.
4542 * Zero on success, negative errno on failure.
4544 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
4545 void *data
, struct drm_file
*file_priv
)
4547 struct drm_mode_get_blob
*out_resp
= data
;
4548 struct drm_property_blob
*blob
;
4550 void __user
*blob_ptr
;
4552 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4555 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4559 if (out_resp
->length
== blob
->length
) {
4560 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4561 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
4566 out_resp
->length
= blob
->length
;
4568 drm_property_unreference_blob(blob
);
4574 * drm_mode_createblob_ioctl - create a new blob property
4577 * @file_priv: DRM file info
4579 * This function creates a new blob property with user-defined values. In order
4580 * to give us sensible validation and checking when creating, rather than at
4581 * every potential use, we also require a type to be provided upfront.
4583 * Called by the user via ioctl.
4586 * Zero on success, negative errno on failure.
4588 int drm_mode_createblob_ioctl(struct drm_device
*dev
,
4589 void *data
, struct drm_file
*file_priv
)
4591 struct drm_mode_create_blob
*out_resp
= data
;
4592 struct drm_property_blob
*blob
;
4593 void __user
*blob_ptr
;
4596 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4599 blob
= drm_property_create_blob(dev
, out_resp
->length
, NULL
);
4601 return PTR_ERR(blob
);
4603 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4604 if (copy_from_user(blob
->data
, blob_ptr
, out_resp
->length
)) {
4609 /* Dropping the lock between create_blob and our access here is safe
4610 * as only the same file_priv can remove the blob; at this point, it is
4611 * not associated with any file_priv. */
4612 mutex_lock(&dev
->mode_config
.blob_lock
);
4613 out_resp
->blob_id
= blob
->base
.id
;
4614 list_add_tail(&blob
->head_file
, &file_priv
->blobs
);
4615 mutex_unlock(&dev
->mode_config
.blob_lock
);
4620 drm_property_unreference_blob(blob
);
4625 * drm_mode_destroyblob_ioctl - destroy a user blob property
4628 * @file_priv: DRM file info
4630 * Destroy an existing user-defined blob property.
4632 * Called by the user via ioctl.
4635 * Zero on success, negative errno on failure.
4637 int drm_mode_destroyblob_ioctl(struct drm_device
*dev
,
4638 void *data
, struct drm_file
*file_priv
)
4640 struct drm_mode_destroy_blob
*out_resp
= data
;
4641 struct drm_property_blob
*blob
= NULL
, *bt
;
4645 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4648 blob
= drm_property_lookup_blob(dev
, out_resp
->blob_id
);
4652 mutex_lock(&dev
->mode_config
.blob_lock
);
4653 /* Ensure the property was actually created by this user. */
4654 list_for_each_entry(bt
, &file_priv
->blobs
, head_file
) {
4666 /* We must drop head_file here, because we may not be the last
4667 * reference on the blob. */
4668 list_del_init(&blob
->head_file
);
4669 mutex_unlock(&dev
->mode_config
.blob_lock
);
4671 /* One reference from lookup, and one from the filp. */
4672 drm_property_unreference_blob(blob
);
4673 drm_property_unreference_blob(blob
);
4678 mutex_unlock(&dev
->mode_config
.blob_lock
);
4679 drm_property_unreference_blob(blob
);
4685 * drm_mode_connector_set_path_property - set tile property on connector
4686 * @connector: connector to set property on.
4687 * @path: path to use for property; must not be NULL.
4689 * This creates a property to expose to userspace to specify a
4690 * connector path. This is mainly used for DisplayPort MST where
4691 * connectors have a topology and we want to allow userspace to give
4692 * them more meaningful names.
4695 * Zero on success, negative errno on failure.
4697 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4700 struct drm_device
*dev
= connector
->dev
;
4703 ret
= drm_property_replace_global_blob(dev
,
4704 &connector
->path_blob_ptr
,
4708 dev
->mode_config
.path_property
);
4711 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4714 * drm_mode_connector_set_tile_property - set tile property on connector
4715 * @connector: connector to set property on.
4717 * This looks up the tile information for a connector, and creates a
4718 * property for userspace to parse if it exists. The property is of
4719 * the form of 8 integers using ':' as a separator.
4722 * Zero on success, errno on failure.
4724 int drm_mode_connector_set_tile_property(struct drm_connector
*connector
)
4726 struct drm_device
*dev
= connector
->dev
;
4730 if (!connector
->has_tile
) {
4731 ret
= drm_property_replace_global_blob(dev
,
4732 &connector
->tile_blob_ptr
,
4736 dev
->mode_config
.tile_property
);
4740 snprintf(tile
, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4741 connector
->tile_group
->id
, connector
->tile_is_single_monitor
,
4742 connector
->num_h_tile
, connector
->num_v_tile
,
4743 connector
->tile_h_loc
, connector
->tile_v_loc
,
4744 connector
->tile_h_size
, connector
->tile_v_size
);
4746 ret
= drm_property_replace_global_blob(dev
,
4747 &connector
->tile_blob_ptr
,
4751 dev
->mode_config
.tile_property
);
4754 EXPORT_SYMBOL(drm_mode_connector_set_tile_property
);
4757 * drm_mode_connector_update_edid_property - update the edid property of a connector
4758 * @connector: drm connector
4759 * @edid: new value of the edid property
4761 * This function creates a new blob modeset object and assigns its id to the
4762 * connector's edid property.
4765 * Zero on success, negative errno on failure.
4767 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4768 const struct edid
*edid
)
4770 struct drm_device
*dev
= connector
->dev
;
4774 /* ignore requests to set edid when overridden */
4775 if (connector
->override_edid
)
4779 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4781 ret
= drm_property_replace_global_blob(dev
,
4782 &connector
->edid_blob_ptr
,
4786 dev
->mode_config
.edid_property
);
4789 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4791 /* Some properties could refer to dynamic refcnt'd objects, or things that
4792 * need special locking to handle lifetime issues (ie. to ensure the prop
4793 * value doesn't become invalid part way through the property update due to
4794 * race). The value returned by reference via 'obj' should be passed back
4795 * to drm_property_change_valid_put() after the property is set (and the
4796 * object to which the property is attached has a chance to take it's own
4799 bool drm_property_change_valid_get(struct drm_property
*property
,
4800 uint64_t value
, struct drm_mode_object
**ref
)
4804 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4809 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4810 if (value
< property
->values
[0] || value
> property
->values
[1])
4813 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4814 int64_t svalue
= U642I64(value
);
4816 if (svalue
< U642I64(property
->values
[0]) ||
4817 svalue
> U642I64(property
->values
[1]))
4820 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4821 uint64_t valid_mask
= 0;
4823 for (i
= 0; i
< property
->num_values
; i
++)
4824 valid_mask
|= (1ULL << property
->values
[i
]);
4825 return !(value
& ~valid_mask
);
4826 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4827 struct drm_property_blob
*blob
;
4832 blob
= drm_property_lookup_blob(property
->dev
, value
);
4839 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4840 /* a zero value for an object property translates to null: */
4844 return _object_find(property
->dev
, value
, property
->values
[0]) != NULL
;
4847 for (i
= 0; i
< property
->num_values
; i
++)
4848 if (property
->values
[i
] == value
)
4853 void drm_property_change_valid_put(struct drm_property
*property
,
4854 struct drm_mode_object
*ref
)
4859 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4860 drm_mode_object_unreference(ref
);
4861 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4862 drm_property_unreference_blob(obj_to_blob(ref
));
4866 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4869 * @file_priv: DRM file info
4871 * This function sets the current value for a connectors's property. It also
4872 * calls into a driver's ->set_property callback to update the hardware state
4874 * Called by the user via ioctl.
4877 * Zero on success, negative errno on failure.
4879 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4880 void *data
, struct drm_file
*file_priv
)
4882 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4883 struct drm_mode_obj_set_property obj_set_prop
= {
4884 .value
= conn_set_prop
->value
,
4885 .prop_id
= conn_set_prop
->prop_id
,
4886 .obj_id
= conn_set_prop
->connector_id
,
4887 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4890 /* It does all the locking and checking we need */
4891 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4894 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4895 struct drm_property
*property
,
4899 struct drm_connector
*connector
= obj_to_connector(obj
);
4901 /* Do DPMS ourselves */
4902 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4903 ret
= (*connector
->funcs
->dpms
)(connector
, (int)value
);
4904 } else if (connector
->funcs
->set_property
)
4905 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4907 /* store the property value if successful */
4909 drm_object_property_set_value(&connector
->base
, property
, value
);
4913 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4914 struct drm_property
*property
,
4918 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4920 if (crtc
->funcs
->set_property
)
4921 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4923 drm_object_property_set_value(obj
, property
, value
);
4929 * drm_mode_plane_set_obj_prop - set the value of a property
4930 * @plane: drm plane object to set property value for
4931 * @property: property to set
4932 * @value: value the property should be set to
4934 * This functions sets a given property on a given plane object. This function
4935 * calls the driver's ->set_property callback and changes the software state of
4936 * the property if the callback succeeds.
4939 * Zero on success, error code on failure.
4941 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
4942 struct drm_property
*property
,
4946 struct drm_mode_object
*obj
= &plane
->base
;
4948 if (plane
->funcs
->set_property
)
4949 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4951 drm_object_property_set_value(obj
, property
, value
);
4955 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
4958 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4961 * @file_priv: DRM file info
4963 * This function retrieves the current value for an object's property. Compared
4964 * to the connector specific ioctl this one is extended to also work on crtc and
4967 * Called by the user via ioctl.
4970 * Zero on success, negative errno on failure.
4972 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4973 struct drm_file
*file_priv
)
4975 struct drm_mode_obj_get_properties
*arg
= data
;
4976 struct drm_mode_object
*obj
;
4979 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4982 drm_modeset_lock_all(dev
);
4984 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4989 if (!obj
->properties
) {
4994 ret
= get_properties(obj
, file_priv
->atomic
,
4995 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
4996 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
5000 drm_mode_object_unreference(obj
);
5002 drm_modeset_unlock_all(dev
);
5007 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5010 * @file_priv: DRM file info
5012 * This function sets the current value for an object's property. It also calls
5013 * into a driver's ->set_property callback to update the hardware state.
5014 * Compared to the connector specific ioctl this one is extended to also work on
5015 * crtc and plane objects.
5017 * Called by the user via ioctl.
5020 * Zero on success, negative errno on failure.
5022 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
5023 struct drm_file
*file_priv
)
5025 struct drm_mode_obj_set_property
*arg
= data
;
5026 struct drm_mode_object
*arg_obj
;
5027 struct drm_mode_object
*prop_obj
;
5028 struct drm_property
*property
;
5029 int i
, ret
= -EINVAL
;
5030 struct drm_mode_object
*ref
;
5032 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5035 drm_modeset_lock_all(dev
);
5037 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
5042 if (!arg_obj
->properties
)
5045 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
5046 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
5049 if (i
== arg_obj
->properties
->count
)
5052 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
5053 DRM_MODE_OBJECT_PROPERTY
);
5058 property
= obj_to_property(prop_obj
);
5060 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
5063 switch (arg_obj
->type
) {
5064 case DRM_MODE_OBJECT_CONNECTOR
:
5065 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
5068 case DRM_MODE_OBJECT_CRTC
:
5069 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
5071 case DRM_MODE_OBJECT_PLANE
:
5072 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
5073 property
, arg
->value
);
5077 drm_property_change_valid_put(property
, ref
);
5080 drm_mode_object_unreference(arg_obj
);
5082 drm_modeset_unlock_all(dev
);
5087 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5088 * @connector: connector to attach
5089 * @encoder: encoder to attach @connector to
5091 * This function links up a connector to an encoder. Note that the routing
5092 * restrictions between encoders and crtcs are exposed to userspace through the
5093 * possible_clones and possible_crtcs bitmasks.
5096 * Zero on success, negative errno on failure.
5098 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
5099 struct drm_encoder
*encoder
)
5104 * In the past, drivers have attempted to model the static association
5105 * of connector to encoder in simple connector/encoder devices using a
5106 * direct assignment of connector->encoder = encoder. This connection
5107 * is a logical one and the responsibility of the core, so drivers are
5108 * expected not to mess with this.
5110 * Note that the error return should've been enough here, but a large
5111 * majority of drivers ignores the return value, so add in a big WARN
5112 * to get people's attention.
5114 if (WARN_ON(connector
->encoder
))
5117 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
5118 if (connector
->encoder_ids
[i
] == 0) {
5119 connector
->encoder_ids
[i
] = encoder
->base
.id
;
5125 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
5128 * drm_mode_crtc_set_gamma_size - set the gamma table size
5129 * @crtc: CRTC to set the gamma table size for
5130 * @gamma_size: size of the gamma table
5132 * Drivers which support gamma tables should set this to the supported gamma
5133 * table size when initializing the CRTC. Currently the drm core only supports a
5134 * fixed gamma table size.
5137 * Zero on success, negative errno on failure.
5139 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
5142 crtc
->gamma_size
= gamma_size
;
5144 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
5146 if (!crtc
->gamma_store
) {
5147 crtc
->gamma_size
= 0;
5153 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
5156 * drm_mode_gamma_set_ioctl - set the gamma table
5159 * @file_priv: DRM file info
5161 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5162 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5164 * Called by the user via ioctl.
5167 * Zero on success, negative errno on failure.
5169 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
5170 void *data
, struct drm_file
*file_priv
)
5172 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5173 struct drm_crtc
*crtc
;
5174 void *r_base
, *g_base
, *b_base
;
5178 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5181 drm_modeset_lock_all(dev
);
5182 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5188 if (crtc
->funcs
->gamma_set
== NULL
) {
5193 /* memcpy into gamma store */
5194 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5199 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5200 r_base
= crtc
->gamma_store
;
5201 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
5206 g_base
= r_base
+ size
;
5207 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
5212 b_base
= g_base
+ size
;
5213 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
5218 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
5221 drm_modeset_unlock_all(dev
);
5227 * drm_mode_gamma_get_ioctl - get the gamma table
5230 * @file_priv: DRM file info
5232 * Copy the current gamma table into the storage provided. This also provides
5233 * the gamma table size the driver expects, which can be used to size the
5234 * allocated storage.
5236 * Called by the user via ioctl.
5239 * Zero on success, negative errno on failure.
5241 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
5242 void *data
, struct drm_file
*file_priv
)
5244 struct drm_mode_crtc_lut
*crtc_lut
= data
;
5245 struct drm_crtc
*crtc
;
5246 void *r_base
, *g_base
, *b_base
;
5250 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
5253 drm_modeset_lock_all(dev
);
5254 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
5260 /* memcpy into gamma store */
5261 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
5266 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
5267 r_base
= crtc
->gamma_store
;
5268 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
5273 g_base
= r_base
+ size
;
5274 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
5279 b_base
= g_base
+ size
;
5280 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
5285 drm_modeset_unlock_all(dev
);
5290 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5293 * @file_priv: DRM file info
5295 * This schedules an asynchronous update on a given CRTC, called page flip.
5296 * Optionally a drm event is generated to signal the completion of the event.
5297 * Generic drivers cannot assume that a pageflip with changed framebuffer
5298 * properties (including driver specific metadata like tiling layout) will work,
5299 * but some drivers support e.g. pixel format changes through the pageflip
5302 * Called by the user via ioctl.
5305 * Zero on success, negative errno on failure.
5307 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
5308 void *data
, struct drm_file
*file_priv
)
5310 struct drm_mode_crtc_page_flip
*page_flip
= data
;
5311 struct drm_crtc
*crtc
;
5312 struct drm_framebuffer
*fb
= NULL
;
5313 struct drm_pending_vblank_event
*e
= NULL
;
5316 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
5317 page_flip
->reserved
!= 0)
5320 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
5323 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
5327 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
5328 if (crtc
->primary
->fb
== NULL
) {
5329 /* The framebuffer is currently unbound, presumably
5330 * due to a hotplug event, that userspace has not
5337 if (crtc
->funcs
->page_flip
== NULL
)
5340 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
5347 const struct drm_plane_state
*state
= crtc
->primary
->state
;
5349 ret
= check_src_coords(state
->src_x
, state
->src_y
,
5350 state
->src_w
, state
->src_h
, fb
);
5352 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
5357 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
5358 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5363 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
5364 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
5369 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
5370 e
->event
.base
.length
= sizeof(e
->event
);
5371 e
->event
.user_data
= page_flip
->user_data
;
5372 ret
= drm_event_reserve_init(dev
, file_priv
, &e
->base
, &e
->event
.base
);
5379 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
5380 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
5382 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
)
5383 drm_event_cancel_free(dev
, &e
->base
);
5384 /* Keep the old fb, don't unref it. */
5385 crtc
->primary
->old_fb
= NULL
;
5387 crtc
->primary
->fb
= fb
;
5388 /* Unref only the old framebuffer. */
5394 drm_framebuffer_unreference(fb
);
5395 if (crtc
->primary
->old_fb
)
5396 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
5397 crtc
->primary
->old_fb
= NULL
;
5398 drm_modeset_unlock_crtc(crtc
);
5404 * drm_mode_config_reset - call ->reset callbacks
5407 * This functions calls all the crtc's, encoder's and connector's ->reset
5408 * callback. Drivers can use this in e.g. their driver load or resume code to
5409 * reset hardware and software state.
5411 void drm_mode_config_reset(struct drm_device
*dev
)
5413 struct drm_crtc
*crtc
;
5414 struct drm_plane
*plane
;
5415 struct drm_encoder
*encoder
;
5416 struct drm_connector
*connector
;
5418 drm_for_each_plane(plane
, dev
)
5419 if (plane
->funcs
->reset
)
5420 plane
->funcs
->reset(plane
);
5422 drm_for_each_crtc(crtc
, dev
)
5423 if (crtc
->funcs
->reset
)
5424 crtc
->funcs
->reset(crtc
);
5426 drm_for_each_encoder(encoder
, dev
)
5427 if (encoder
->funcs
->reset
)
5428 encoder
->funcs
->reset(encoder
);
5430 mutex_lock(&dev
->mode_config
.mutex
);
5431 drm_for_each_connector(connector
, dev
)
5432 if (connector
->funcs
->reset
)
5433 connector
->funcs
->reset(connector
);
5434 mutex_unlock(&dev
->mode_config
.mutex
);
5436 EXPORT_SYMBOL(drm_mode_config_reset
);
5439 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5442 * @file_priv: DRM file info
5444 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5445 * TTM or something else entirely) and returns the resulting buffer handle. This
5446 * handle can then be wrapped up into a framebuffer modeset object.
5448 * Note that userspace is not allowed to use such objects for render
5449 * acceleration - drivers must create their own private ioctls for such a use
5452 * Called by the user via ioctl.
5455 * Zero on success, negative errno on failure.
5457 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
5458 void *data
, struct drm_file
*file_priv
)
5460 struct drm_mode_create_dumb
*args
= data
;
5461 u32 cpp
, stride
, size
;
5463 if (!dev
->driver
->dumb_create
)
5465 if (!args
->width
|| !args
->height
|| !args
->bpp
)
5468 /* overflow checks for 32bit size calculations */
5469 /* NOTE: DIV_ROUND_UP() can overflow */
5470 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
5471 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
5473 stride
= cpp
* args
->width
;
5474 if (args
->height
> 0xffffffffU
/ stride
)
5477 /* test for wrap-around */
5478 size
= args
->height
* stride
;
5479 if (PAGE_ALIGN(size
) == 0)
5483 * handle, pitch and size are output parameters. Zero them out to
5484 * prevent drivers from accidentally using uninitialized data. Since
5485 * not all existing userspace is clearing these fields properly we
5486 * cannot reject IOCTL with garbage in them.
5492 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
5496 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5499 * @file_priv: DRM file info
5501 * Allocate an offset in the drm device node's address space to be able to
5502 * memory map a dumb buffer.
5504 * Called by the user via ioctl.
5507 * Zero on success, negative errno on failure.
5509 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
5510 void *data
, struct drm_file
*file_priv
)
5512 struct drm_mode_map_dumb
*args
= data
;
5514 /* call driver ioctl to get mmap offset */
5515 if (!dev
->driver
->dumb_map_offset
)
5518 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
5522 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5525 * @file_priv: DRM file info
5527 * This destroys the userspace handle for the given dumb backing storage buffer.
5528 * Since buffer objects must be reference counted in the kernel a buffer object
5529 * won't be immediately freed if a framebuffer modeset object still uses it.
5531 * Called by the user via ioctl.
5534 * Zero on success, negative errno on failure.
5536 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
5537 void *data
, struct drm_file
*file_priv
)
5539 struct drm_mode_destroy_dumb
*args
= data
;
5541 if (!dev
->driver
->dumb_destroy
)
5544 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
5548 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5549 * @format: pixel format (DRM_FORMAT_*)
5550 * @depth: storage for the depth value
5551 * @bpp: storage for the bpp value
5553 * This only supports RGB formats here for compat with code that doesn't use
5554 * pixel formats directly yet.
5556 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
5561 case DRM_FORMAT_RGB332
:
5562 case DRM_FORMAT_BGR233
:
5566 case DRM_FORMAT_XRGB1555
:
5567 case DRM_FORMAT_XBGR1555
:
5568 case DRM_FORMAT_RGBX5551
:
5569 case DRM_FORMAT_BGRX5551
:
5570 case DRM_FORMAT_ARGB1555
:
5571 case DRM_FORMAT_ABGR1555
:
5572 case DRM_FORMAT_RGBA5551
:
5573 case DRM_FORMAT_BGRA5551
:
5577 case DRM_FORMAT_RGB565
:
5578 case DRM_FORMAT_BGR565
:
5582 case DRM_FORMAT_RGB888
:
5583 case DRM_FORMAT_BGR888
:
5587 case DRM_FORMAT_XRGB8888
:
5588 case DRM_FORMAT_XBGR8888
:
5589 case DRM_FORMAT_RGBX8888
:
5590 case DRM_FORMAT_BGRX8888
:
5594 case DRM_FORMAT_XRGB2101010
:
5595 case DRM_FORMAT_XBGR2101010
:
5596 case DRM_FORMAT_RGBX1010102
:
5597 case DRM_FORMAT_BGRX1010102
:
5598 case DRM_FORMAT_ARGB2101010
:
5599 case DRM_FORMAT_ABGR2101010
:
5600 case DRM_FORMAT_RGBA1010102
:
5601 case DRM_FORMAT_BGRA1010102
:
5605 case DRM_FORMAT_ARGB8888
:
5606 case DRM_FORMAT_ABGR8888
:
5607 case DRM_FORMAT_RGBA8888
:
5608 case DRM_FORMAT_BGRA8888
:
5613 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5614 drm_get_format_name(format
));
5620 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
5623 * drm_format_num_planes - get the number of planes for format
5624 * @format: pixel format (DRM_FORMAT_*)
5627 * The number of planes used by the specified pixel format.
5629 int drm_format_num_planes(uint32_t format
)
5632 case DRM_FORMAT_YUV410
:
5633 case DRM_FORMAT_YVU410
:
5634 case DRM_FORMAT_YUV411
:
5635 case DRM_FORMAT_YVU411
:
5636 case DRM_FORMAT_YUV420
:
5637 case DRM_FORMAT_YVU420
:
5638 case DRM_FORMAT_YUV422
:
5639 case DRM_FORMAT_YVU422
:
5640 case DRM_FORMAT_YUV444
:
5641 case DRM_FORMAT_YVU444
:
5643 case DRM_FORMAT_NV12
:
5644 case DRM_FORMAT_NV21
:
5645 case DRM_FORMAT_NV16
:
5646 case DRM_FORMAT_NV61
:
5647 case DRM_FORMAT_NV24
:
5648 case DRM_FORMAT_NV42
:
5654 EXPORT_SYMBOL(drm_format_num_planes
);
5657 * drm_format_plane_cpp - determine the bytes per pixel value
5658 * @format: pixel format (DRM_FORMAT_*)
5659 * @plane: plane index
5662 * The bytes per pixel value for the specified plane.
5664 int drm_format_plane_cpp(uint32_t format
, int plane
)
5669 if (plane
>= drm_format_num_planes(format
))
5673 case DRM_FORMAT_YUYV
:
5674 case DRM_FORMAT_YVYU
:
5675 case DRM_FORMAT_UYVY
:
5676 case DRM_FORMAT_VYUY
:
5678 case DRM_FORMAT_NV12
:
5679 case DRM_FORMAT_NV21
:
5680 case DRM_FORMAT_NV16
:
5681 case DRM_FORMAT_NV61
:
5682 case DRM_FORMAT_NV24
:
5683 case DRM_FORMAT_NV42
:
5684 return plane
? 2 : 1;
5685 case DRM_FORMAT_YUV410
:
5686 case DRM_FORMAT_YVU410
:
5687 case DRM_FORMAT_YUV411
:
5688 case DRM_FORMAT_YVU411
:
5689 case DRM_FORMAT_YUV420
:
5690 case DRM_FORMAT_YVU420
:
5691 case DRM_FORMAT_YUV422
:
5692 case DRM_FORMAT_YVU422
:
5693 case DRM_FORMAT_YUV444
:
5694 case DRM_FORMAT_YVU444
:
5697 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
5701 EXPORT_SYMBOL(drm_format_plane_cpp
);
5704 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5705 * @format: pixel format (DRM_FORMAT_*)
5708 * The horizontal chroma subsampling factor for the
5709 * specified pixel format.
5711 int drm_format_horz_chroma_subsampling(uint32_t format
)
5714 case DRM_FORMAT_YUV411
:
5715 case DRM_FORMAT_YVU411
:
5716 case DRM_FORMAT_YUV410
:
5717 case DRM_FORMAT_YVU410
:
5719 case DRM_FORMAT_YUYV
:
5720 case DRM_FORMAT_YVYU
:
5721 case DRM_FORMAT_UYVY
:
5722 case DRM_FORMAT_VYUY
:
5723 case DRM_FORMAT_NV12
:
5724 case DRM_FORMAT_NV21
:
5725 case DRM_FORMAT_NV16
:
5726 case DRM_FORMAT_NV61
:
5727 case DRM_FORMAT_YUV422
:
5728 case DRM_FORMAT_YVU422
:
5729 case DRM_FORMAT_YUV420
:
5730 case DRM_FORMAT_YVU420
:
5736 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
5739 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5740 * @format: pixel format (DRM_FORMAT_*)
5743 * The vertical chroma subsampling factor for the
5744 * specified pixel format.
5746 int drm_format_vert_chroma_subsampling(uint32_t format
)
5749 case DRM_FORMAT_YUV410
:
5750 case DRM_FORMAT_YVU410
:
5752 case DRM_FORMAT_YUV420
:
5753 case DRM_FORMAT_YVU420
:
5754 case DRM_FORMAT_NV12
:
5755 case DRM_FORMAT_NV21
:
5761 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
5764 * drm_format_plane_width - width of the plane given the first plane
5765 * @width: width of the first plane
5766 * @format: pixel format
5767 * @plane: plane index
5770 * The width of @plane, given that the width of the first plane is @width.
5772 int drm_format_plane_width(int width
, uint32_t format
, int plane
)
5774 if (plane
>= drm_format_num_planes(format
))
5780 return width
/ drm_format_horz_chroma_subsampling(format
);
5782 EXPORT_SYMBOL(drm_format_plane_width
);
5785 * drm_format_plane_height - height of the plane given the first plane
5786 * @height: height of the first plane
5787 * @format: pixel format
5788 * @plane: plane index
5791 * The height of @plane, given that the height of the first plane is @height.
5793 int drm_format_plane_height(int height
, uint32_t format
, int plane
)
5795 if (plane
>= drm_format_num_planes(format
))
5801 return height
/ drm_format_vert_chroma_subsampling(format
);
5803 EXPORT_SYMBOL(drm_format_plane_height
);
5806 * drm_rotation_simplify() - Try to simplify the rotation
5807 * @rotation: Rotation to be simplified
5808 * @supported_rotations: Supported rotations
5810 * Attempt to simplify the rotation to a form that is supported.
5811 * Eg. if the hardware supports everything except DRM_REFLECT_X
5812 * one could call this function like this:
5814 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5815 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5816 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5818 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5819 * transforms the hardware supports, this function may not
5820 * be able to produce a supported transform, so the caller should
5821 * check the result afterwards.
5823 unsigned int drm_rotation_simplify(unsigned int rotation
,
5824 unsigned int supported_rotations
)
5826 if (rotation
& ~supported_rotations
) {
5827 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5828 rotation
= (rotation
& DRM_REFLECT_MASK
) |
5829 BIT((ffs(rotation
& DRM_ROTATE_MASK
) + 1) % 4);
5834 EXPORT_SYMBOL(drm_rotation_simplify
);
5837 * drm_mode_config_init - initialize DRM mode_configuration structure
5840 * Initialize @dev's mode_config structure, used for tracking the graphics
5841 * configuration of @dev.
5843 * Since this initializes the modeset locks, no locking is possible. Which is no
5844 * problem, since this should happen single threaded at init time. It is the
5845 * driver's problem to ensure this guarantee.
5848 void drm_mode_config_init(struct drm_device
*dev
)
5850 mutex_init(&dev
->mode_config
.mutex
);
5851 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5852 mutex_init(&dev
->mode_config
.idr_mutex
);
5853 mutex_init(&dev
->mode_config
.fb_lock
);
5854 mutex_init(&dev
->mode_config
.blob_lock
);
5855 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5856 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5857 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5858 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5859 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5860 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5861 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5862 idr_init(&dev
->mode_config
.crtc_idr
);
5863 idr_init(&dev
->mode_config
.tile_idr
);
5864 ida_init(&dev
->mode_config
.connector_ida
);
5866 drm_modeset_lock_all(dev
);
5867 drm_mode_create_standard_properties(dev
);
5868 drm_modeset_unlock_all(dev
);
5870 /* Just to be sure */
5871 dev
->mode_config
.num_fb
= 0;
5872 dev
->mode_config
.num_connector
= 0;
5873 dev
->mode_config
.num_crtc
= 0;
5874 dev
->mode_config
.num_encoder
= 0;
5875 dev
->mode_config
.num_overlay_plane
= 0;
5876 dev
->mode_config
.num_total_plane
= 0;
5878 EXPORT_SYMBOL(drm_mode_config_init
);
5881 * drm_mode_config_cleanup - free up DRM mode_config info
5884 * Free up all the connectors and CRTCs associated with this DRM device, then
5885 * free up the framebuffers and associated buffer objects.
5887 * Note that since this /should/ happen single-threaded at driver/device
5888 * teardown time, no locking is required. It's the driver's job to ensure that
5889 * this guarantee actually holds true.
5891 * FIXME: cleanup any dangling user buffer objects too
5893 void drm_mode_config_cleanup(struct drm_device
*dev
)
5895 struct drm_connector
*connector
, *ot
;
5896 struct drm_crtc
*crtc
, *ct
;
5897 struct drm_encoder
*encoder
, *enct
;
5898 struct drm_framebuffer
*fb
, *fbt
;
5899 struct drm_property
*property
, *pt
;
5900 struct drm_property_blob
*blob
, *bt
;
5901 struct drm_plane
*plane
, *plt
;
5903 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5905 encoder
->funcs
->destroy(encoder
);
5908 list_for_each_entry_safe(connector
, ot
,
5909 &dev
->mode_config
.connector_list
, head
) {
5910 connector
->funcs
->destroy(connector
);
5913 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5915 drm_property_destroy(dev
, property
);
5918 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5920 plane
->funcs
->destroy(plane
);
5923 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5924 crtc
->funcs
->destroy(crtc
);
5927 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5929 drm_property_unreference_blob(blob
);
5933 * Single-threaded teardown context, so it's not required to grab the
5934 * fb_lock to protect against concurrent fb_list access. Contrary, it
5935 * would actually deadlock with the drm_framebuffer_cleanup function.
5937 * Also, if there are any framebuffers left, that's a driver leak now,
5938 * so politely WARN about this.
5940 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5941 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5942 drm_framebuffer_free(&fb
->base
.refcount
);
5945 ida_destroy(&dev
->mode_config
.connector_ida
);
5946 idr_destroy(&dev
->mode_config
.tile_idr
);
5947 idr_destroy(&dev
->mode_config
.crtc_idr
);
5948 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5950 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5952 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5953 unsigned int supported_rotations
)
5955 static const struct drm_prop_enum_list props
[] = {
5956 { DRM_ROTATE_0
, "rotate-0" },
5957 { DRM_ROTATE_90
, "rotate-90" },
5958 { DRM_ROTATE_180
, "rotate-180" },
5959 { DRM_ROTATE_270
, "rotate-270" },
5960 { DRM_REFLECT_X
, "reflect-x" },
5961 { DRM_REFLECT_Y
, "reflect-y" },
5964 return drm_property_create_bitmask(dev
, 0, "rotation",
5965 props
, ARRAY_SIZE(props
),
5966 supported_rotations
);
5968 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
5973 * Tile groups are used to represent tiled monitors with a unique
5974 * integer identifier. Tiled monitors using DisplayID v1.3 have
5975 * a unique 8-byte handle, we store this in a tile group, so we
5976 * have a common identifier for all tiles in a monitor group.
5978 static void drm_tile_group_free(struct kref
*kref
)
5980 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
5981 struct drm_device
*dev
= tg
->dev
;
5982 mutex_lock(&dev
->mode_config
.idr_mutex
);
5983 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
5984 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5989 * drm_mode_put_tile_group - drop a reference to a tile group.
5991 * @tg: tile group to drop reference to.
5993 * drop reference to tile group and free if 0.
5995 void drm_mode_put_tile_group(struct drm_device
*dev
,
5996 struct drm_tile_group
*tg
)
5998 kref_put(&tg
->refcount
, drm_tile_group_free
);
6002 * drm_mode_get_tile_group - get a reference to an existing tile group
6004 * @topology: 8-bytes unique per monitor.
6006 * Use the unique bytes to get a reference to an existing tile group.
6009 * tile group or NULL if not found.
6011 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
6014 struct drm_tile_group
*tg
;
6016 mutex_lock(&dev
->mode_config
.idr_mutex
);
6017 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
6018 if (!memcmp(tg
->group_data
, topology
, 8)) {
6019 if (!kref_get_unless_zero(&tg
->refcount
))
6021 mutex_unlock(&dev
->mode_config
.idr_mutex
);
6025 mutex_unlock(&dev
->mode_config
.idr_mutex
);
6028 EXPORT_SYMBOL(drm_mode_get_tile_group
);
6031 * drm_mode_create_tile_group - create a tile group from a displayid description
6033 * @topology: 8-bytes unique per monitor.
6035 * Create a tile group for the unique monitor, and get a unique
6036 * identifier for the tile group.
6039 * new tile group or error.
6041 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
6044 struct drm_tile_group
*tg
;
6047 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
6049 return ERR_PTR(-ENOMEM
);
6051 kref_init(&tg
->refcount
);
6052 memcpy(tg
->group_data
, topology
, 8);
6055 mutex_lock(&dev
->mode_config
.idr_mutex
);
6056 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
6064 mutex_unlock(&dev
->mode_config
.idr_mutex
);
6067 EXPORT_SYMBOL(drm_mode_create_tile_group
);