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
*add_framebuffer_internal(struct drm_device
*dev
,
47 struct drm_mode_fb_cmd2
*r
,
48 struct drm_file
*file_priv
);
50 /* Avoid boilerplate. I'm tired of typing. */
51 #define DRM_ENUM_NAME_FN(fnname, list) \
52 const char *fnname(int val) \
55 for (i = 0; i < ARRAY_SIZE(list); i++) { \
56 if (list[i].type == val) \
57 return list[i].name; \
65 static const struct drm_prop_enum_list drm_dpms_enum_list
[] = {
66 { DRM_MODE_DPMS_ON
, "On" },
67 { DRM_MODE_DPMS_STANDBY
, "Standby" },
68 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
69 { DRM_MODE_DPMS_OFF
, "Off" }
72 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
74 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] = {
75 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
76 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
77 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
83 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] = {
84 { DRM_MODE_SCALE_NONE
, "None" },
85 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
86 { DRM_MODE_SCALE_CENTER
, "Center" },
87 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
90 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list
[] = {
91 { DRM_MODE_PICTURE_ASPECT_NONE
, "Automatic" },
92 { DRM_MODE_PICTURE_ASPECT_4_3
, "4:3" },
93 { DRM_MODE_PICTURE_ASPECT_16_9
, "16:9" },
97 * Non-global properties, but "required" for certain connectors.
99 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] = {
100 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
101 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
102 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
105 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
107 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] = {
108 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
109 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
110 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
113 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
114 drm_dvi_i_subconnector_enum_list
)
116 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] = {
117 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
124 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
126 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] = {
127 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
128 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
129 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
134 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
135 drm_tv_subconnector_enum_list
)
137 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
138 { DRM_MODE_DIRTY_OFF
, "Off" },
139 { DRM_MODE_DIRTY_ON
, "On" },
140 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
143 struct drm_conn_prop_enum_list
{
150 * Connector and encoder types.
152 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] = {
153 { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
154 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
155 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
156 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
157 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
158 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
159 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
160 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
161 { DRM_MODE_CONNECTOR_Component
, "Component" },
162 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
163 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
164 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
165 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
166 { DRM_MODE_CONNECTOR_TV
, "TV" },
167 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
168 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
169 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
172 static const struct drm_prop_enum_list drm_encoder_enum_list
[] = {
173 { DRM_MODE_ENCODER_NONE
, "None" },
174 { DRM_MODE_ENCODER_DAC
, "DAC" },
175 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
176 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
177 { DRM_MODE_ENCODER_TVDAC
, "TV" },
178 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
179 { DRM_MODE_ENCODER_DSI
, "DSI" },
180 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
183 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] = {
184 { SubPixelUnknown
, "Unknown" },
185 { SubPixelHorizontalRGB
, "Horizontal RGB" },
186 { SubPixelHorizontalBGR
, "Horizontal BGR" },
187 { SubPixelVerticalRGB
, "Vertical RGB" },
188 { SubPixelVerticalBGR
, "Vertical BGR" },
189 { SubPixelNone
, "None" },
192 void drm_connector_ida_init(void)
196 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
197 ida_init(&drm_connector_enum_list
[i
].ida
);
200 void drm_connector_ida_destroy(void)
204 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
205 ida_destroy(&drm_connector_enum_list
[i
].ida
);
209 * drm_get_connector_status_name - return a string for connector status
210 * @status: connector status to compute name of
212 * In contrast to the other drm_get_*_name functions this one here returns a
213 * const pointer and hence is threadsafe.
215 const char *drm_get_connector_status_name(enum drm_connector_status status
)
217 if (status
== connector_status_connected
)
219 else if (status
== connector_status_disconnected
)
220 return "disconnected";
224 EXPORT_SYMBOL(drm_get_connector_status_name
);
227 * drm_get_subpixel_order_name - return a string for a given subpixel enum
228 * @order: enum of subpixel_order
230 * Note you could abuse this and return something out of bounds, but that
231 * would be a caller error. No unscrubbed user data should make it here.
233 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
235 return drm_subpixel_enum_list
[order
].name
;
237 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
239 static char printable_char(int c
)
241 return isascii(c
) && isprint(c
) ? c
: '?';
245 * drm_get_format_name - return a string for drm fourcc format
246 * @format: format to compute name of
248 * Note that the buffer used by this function is globally shared and owned by
249 * the function itself.
251 * FIXME: This isn't really multithreading safe.
253 const char *drm_get_format_name(uint32_t format
)
257 snprintf(buf
, sizeof(buf
),
258 "%c%c%c%c %s-endian (0x%08x)",
259 printable_char(format
& 0xff),
260 printable_char((format
>> 8) & 0xff),
261 printable_char((format
>> 16) & 0xff),
262 printable_char((format
>> 24) & 0x7f),
263 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
268 EXPORT_SYMBOL(drm_get_format_name
);
271 * Internal function to assign a slot in the object idr and optionally
272 * register the object into the idr.
274 static int drm_mode_object_get_reg(struct drm_device
*dev
,
275 struct drm_mode_object
*obj
,
281 mutex_lock(&dev
->mode_config
.idr_mutex
);
282 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
285 * Set up the object linking under the protection of the idr
286 * lock so that other users can't see inconsistent state.
289 obj
->type
= obj_type
;
291 mutex_unlock(&dev
->mode_config
.idr_mutex
);
293 return ret
< 0 ? ret
: 0;
297 * drm_mode_object_get - allocate a new modeset identifier
299 * @obj: object pointer, used to generate unique ID
300 * @obj_type: object type
302 * Create a unique identifier based on @ptr in @dev's identifier space. Used
303 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
304 * modeset identifiers are _not_ reference counted. Hence don't use this for
305 * reference counted modeset objects like framebuffers.
308 * New unique (relative to other objects in @dev) integer identifier for the
311 int drm_mode_object_get(struct drm_device
*dev
,
312 struct drm_mode_object
*obj
, uint32_t obj_type
)
314 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true);
317 static void drm_mode_object_register(struct drm_device
*dev
,
318 struct drm_mode_object
*obj
)
320 mutex_lock(&dev
->mode_config
.idr_mutex
);
321 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
322 mutex_unlock(&dev
->mode_config
.idr_mutex
);
326 * drm_mode_object_put - free a modeset identifer
328 * @object: object to free
330 * Free @id from @dev's unique identifier pool. Note that despite the _get
331 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
332 * for reference counted modeset objects like framebuffers.
334 void drm_mode_object_put(struct drm_device
*dev
,
335 struct drm_mode_object
*object
)
337 mutex_lock(&dev
->mode_config
.idr_mutex
);
338 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
339 mutex_unlock(&dev
->mode_config
.idr_mutex
);
342 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
343 uint32_t id
, uint32_t type
)
345 struct drm_mode_object
*obj
= NULL
;
347 mutex_lock(&dev
->mode_config
.idr_mutex
);
348 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
349 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
351 if (obj
&& obj
->id
!= id
)
353 /* don't leak out unref'd fb's */
354 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
356 mutex_unlock(&dev
->mode_config
.idr_mutex
);
362 * drm_mode_object_find - look up a drm object with static lifetime
364 * @id: id of the mode object
365 * @type: type of the mode object
367 * Note that framebuffers cannot be looked up with this functions - since those
368 * are reference counted, they need special treatment. Even with
369 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
370 * rather than WARN_ON()).
372 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
373 uint32_t id
, uint32_t type
)
375 struct drm_mode_object
*obj
= NULL
;
377 /* Framebuffers are reference counted and need their own lookup
379 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
380 obj
= _object_find(dev
, id
, type
);
383 EXPORT_SYMBOL(drm_mode_object_find
);
386 * drm_framebuffer_init - initialize a framebuffer
388 * @fb: framebuffer to be initialized
389 * @funcs: ... with these functions
391 * Allocates an ID for the framebuffer's parent mode object, sets its mode
392 * functions & device file and adds it to the master fd list.
395 * This functions publishes the fb and makes it available for concurrent access
396 * by other users. Which means by this point the fb _must_ be fully set up -
397 * since all the fb attributes are invariant over its lifetime, no further
398 * locking but only correct reference counting is required.
401 * Zero on success, error code on failure.
403 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
404 const struct drm_framebuffer_funcs
*funcs
)
408 mutex_lock(&dev
->mode_config
.fb_lock
);
409 kref_init(&fb
->refcount
);
410 INIT_LIST_HEAD(&fb
->filp_head
);
414 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
418 dev
->mode_config
.num_fb
++;
419 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
421 mutex_unlock(&dev
->mode_config
.fb_lock
);
425 EXPORT_SYMBOL(drm_framebuffer_init
);
427 /* dev->mode_config.fb_lock must be held! */
428 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
429 struct drm_framebuffer
*fb
)
431 mutex_lock(&dev
->mode_config
.idr_mutex
);
432 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
433 mutex_unlock(&dev
->mode_config
.idr_mutex
);
438 static void drm_framebuffer_free(struct kref
*kref
)
440 struct drm_framebuffer
*fb
=
441 container_of(kref
, struct drm_framebuffer
, refcount
);
442 struct drm_device
*dev
= fb
->dev
;
445 * The lookup idr holds a weak reference, which has not necessarily been
446 * removed at this point. Check for that.
448 mutex_lock(&dev
->mode_config
.fb_lock
);
450 /* Mark fb as reaped and drop idr ref. */
451 __drm_framebuffer_unregister(dev
, fb
);
453 mutex_unlock(&dev
->mode_config
.fb_lock
);
455 fb
->funcs
->destroy(fb
);
458 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
461 struct drm_mode_object
*obj
= NULL
;
462 struct drm_framebuffer
*fb
;
464 mutex_lock(&dev
->mode_config
.idr_mutex
);
465 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
466 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
470 mutex_unlock(&dev
->mode_config
.idr_mutex
);
476 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
478 * @id: id of the fb object
480 * If successful, this grabs an additional reference to the framebuffer -
481 * callers need to make sure to eventually unreference the returned framebuffer
482 * again, using @drm_framebuffer_unreference.
484 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
487 struct drm_framebuffer
*fb
;
489 mutex_lock(&dev
->mode_config
.fb_lock
);
490 fb
= __drm_framebuffer_lookup(dev
, id
);
492 if (!kref_get_unless_zero(&fb
->refcount
))
495 mutex_unlock(&dev
->mode_config
.fb_lock
);
499 EXPORT_SYMBOL(drm_framebuffer_lookup
);
502 * drm_framebuffer_unreference - unref a framebuffer
503 * @fb: framebuffer to unref
505 * This functions decrements the fb's refcount and frees it if it drops to zero.
507 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
509 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
510 kref_put(&fb
->refcount
, drm_framebuffer_free
);
512 EXPORT_SYMBOL(drm_framebuffer_unreference
);
515 * drm_framebuffer_reference - incr the fb refcnt
518 * This functions increments the fb's refcount.
520 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
522 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
523 kref_get(&fb
->refcount
);
525 EXPORT_SYMBOL(drm_framebuffer_reference
);
527 static void drm_framebuffer_free_bug(struct kref
*kref
)
532 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
534 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
535 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
539 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
540 * @fb: fb to unregister
542 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
543 * those used for fbdev. Note that the caller must hold a reference of it's own,
544 * i.e. the object may not be destroyed through this call (since it'll lead to a
545 * locking inversion).
547 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
549 struct drm_device
*dev
= fb
->dev
;
551 mutex_lock(&dev
->mode_config
.fb_lock
);
552 /* Mark fb as reaped and drop idr ref. */
553 __drm_framebuffer_unregister(dev
, fb
);
554 mutex_unlock(&dev
->mode_config
.fb_lock
);
556 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
559 * drm_framebuffer_cleanup - remove a framebuffer object
560 * @fb: framebuffer to remove
562 * Cleanup framebuffer. This function is intended to be used from the drivers
563 * ->destroy callback. It can also be used to clean up driver private
564 * framebuffers embedded into a larger structure.
566 * Note that this function does not remove the fb from active usuage - if it is
567 * still used anywhere, hilarity can ensue since userspace could call getfb on
568 * the id and get back -EINVAL. Obviously no concern at driver unload time.
570 * Also, the framebuffer will not be removed from the lookup idr - for
571 * user-created framebuffers this will happen in in the rmfb ioctl. For
572 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
573 * drm_framebuffer_unregister_private.
575 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
577 struct drm_device
*dev
= fb
->dev
;
579 mutex_lock(&dev
->mode_config
.fb_lock
);
581 dev
->mode_config
.num_fb
--;
582 mutex_unlock(&dev
->mode_config
.fb_lock
);
584 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
587 * drm_framebuffer_remove - remove and unreference a framebuffer object
588 * @fb: framebuffer to remove
590 * Scans all the CRTCs and planes in @dev's mode_config. If they're
591 * using @fb, removes it, setting it to NULL. Then drops the reference to the
592 * passed-in framebuffer. Might take the modeset locks.
594 * Note that this function optimizes the cleanup away if the caller holds the
595 * last reference to the framebuffer. It is also guaranteed to not take the
596 * modeset locks in this case.
598 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
600 struct drm_device
*dev
= fb
->dev
;
601 struct drm_crtc
*crtc
;
602 struct drm_plane
*plane
;
603 struct drm_mode_set set
;
606 WARN_ON(!list_empty(&fb
->filp_head
));
609 * drm ABI mandates that we remove any deleted framebuffers from active
610 * useage. But since most sane clients only remove framebuffers they no
611 * longer need, try to optimize this away.
613 * Since we're holding a reference ourselves, observing a refcount of 1
614 * means that we're the last holder and can skip it. Also, the refcount
615 * can never increase from 1 again, so we don't need any barriers or
618 * Note that userspace could try to race with use and instate a new
619 * usage _after_ we've cleared all current ones. End result will be an
620 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
623 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
624 drm_modeset_lock_all(dev
);
625 /* remove from any CRTC */
626 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
627 if (crtc
->primary
->fb
== fb
) {
628 /* should turn off the crtc */
629 memset(&set
, 0, sizeof(struct drm_mode_set
));
632 ret
= drm_mode_set_config_internal(&set
);
634 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
638 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
640 drm_plane_force_disable(plane
);
642 drm_modeset_unlock_all(dev
);
645 drm_framebuffer_unreference(fb
);
647 EXPORT_SYMBOL(drm_framebuffer_remove
);
649 DEFINE_WW_CLASS(crtc_ww_class
);
652 * drm_crtc_init_with_planes - Initialise a new CRTC object with
653 * specified primary and cursor planes.
655 * @crtc: CRTC object to init
656 * @primary: Primary plane for CRTC
657 * @cursor: Cursor plane for CRTC
658 * @funcs: callbacks for the new CRTC
660 * Inits a new object created as base part of a driver crtc object.
663 * Zero on success, error code on failure.
665 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
666 struct drm_plane
*primary
,
667 struct drm_plane
*cursor
,
668 const struct drm_crtc_funcs
*funcs
)
670 struct drm_mode_config
*config
= &dev
->mode_config
;
675 crtc
->invert_dimensions
= false;
677 drm_modeset_lock_init(&crtc
->mutex
);
678 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
682 crtc
->base
.properties
= &crtc
->properties
;
684 list_add_tail(&crtc
->head
, &config
->crtc_list
);
687 crtc
->primary
= primary
;
688 crtc
->cursor
= cursor
;
690 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
692 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
696 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
699 * drm_crtc_cleanup - Clean up the core crtc usage
700 * @crtc: CRTC to cleanup
702 * This function cleans up @crtc and removes it from the DRM mode setting
703 * core. Note that the function does *not* free the crtc structure itself,
704 * this is the responsibility of the caller.
706 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
708 struct drm_device
*dev
= crtc
->dev
;
710 kfree(crtc
->gamma_store
);
711 crtc
->gamma_store
= NULL
;
713 drm_modeset_lock_fini(&crtc
->mutex
);
715 drm_mode_object_put(dev
, &crtc
->base
);
716 list_del(&crtc
->head
);
717 dev
->mode_config
.num_crtc
--;
719 WARN_ON(crtc
->state
&& !crtc
->funcs
->atomic_destroy_state
);
720 if (crtc
->state
&& crtc
->funcs
->atomic_destroy_state
)
721 crtc
->funcs
->atomic_destroy_state(crtc
, crtc
->state
);
723 memset(crtc
, 0, sizeof(*crtc
));
725 EXPORT_SYMBOL(drm_crtc_cleanup
);
728 * drm_crtc_index - find the index of a registered CRTC
729 * @crtc: CRTC to find index for
731 * Given a registered CRTC, return the index of that CRTC within a DRM
732 * device's list of CRTCs.
734 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
736 unsigned int index
= 0;
737 struct drm_crtc
*tmp
;
739 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
748 EXPORT_SYMBOL(drm_crtc_index
);
751 * drm_mode_remove - remove and free a mode
752 * @connector: connector list to modify
753 * @mode: mode to remove
755 * Remove @mode from @connector's mode list, then free it.
757 static void drm_mode_remove(struct drm_connector
*connector
,
758 struct drm_display_mode
*mode
)
760 list_del(&mode
->head
);
761 drm_mode_destroy(connector
->dev
, mode
);
765 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
766 * @connector: connector to quwery
768 * The kernel supports per-connector configration of its consoles through
769 * use of the video= parameter. This function parses that option and
770 * extracts the user's specified mode (or enable/disable status) for a
771 * particular connector. This is typically only used during the early fbdev
774 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
776 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
779 if (fb_get_options(connector
->name
, &option
))
782 if (!drm_mode_parse_command_line_for_connector(option
,
790 switch (mode
->force
) {
794 case DRM_FORCE_ON_DIGITAL
:
803 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
804 connector
->force
= mode
->force
;
807 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
809 mode
->xres
, mode
->yres
,
810 mode
->refresh_specified
? mode
->refresh
: 60,
811 mode
->rb
? " reduced blanking" : "",
812 mode
->margins
? " with margins" : "",
813 mode
->interlace
? " interlaced" : "");
817 * drm_connector_init - Init a preallocated connector
819 * @connector: the connector to init
820 * @funcs: callbacks for this connector
821 * @connector_type: user visible type of the connector
823 * Initialises a preallocated connector. Connectors should be
824 * subclassed as part of driver connector objects.
827 * Zero on success, error code on failure.
829 int drm_connector_init(struct drm_device
*dev
,
830 struct drm_connector
*connector
,
831 const struct drm_connector_funcs
*funcs
,
835 struct ida
*connector_ida
=
836 &drm_connector_enum_list
[connector_type
].ida
;
838 drm_modeset_lock_all(dev
);
840 ret
= drm_mode_object_get_reg(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
, false);
844 connector
->base
.properties
= &connector
->properties
;
845 connector
->dev
= dev
;
846 connector
->funcs
= funcs
;
847 connector
->connector_type
= connector_type
;
848 connector
->connector_type_id
=
849 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
850 if (connector
->connector_type_id
< 0) {
851 ret
= connector
->connector_type_id
;
855 kasprintf(GFP_KERNEL
, "%s-%d",
856 drm_connector_enum_list
[connector_type
].name
,
857 connector
->connector_type_id
);
858 if (!connector
->name
) {
863 INIT_LIST_HEAD(&connector
->probed_modes
);
864 INIT_LIST_HEAD(&connector
->modes
);
865 connector
->edid_blob_ptr
= NULL
;
866 connector
->status
= connector_status_unknown
;
868 drm_connector_get_cmdline_mode(connector
);
870 /* We should add connectors at the end to avoid upsetting the connector
872 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
873 dev
->mode_config
.num_connector
++;
875 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
876 drm_object_attach_property(&connector
->base
,
877 dev
->mode_config
.edid_property
,
880 drm_object_attach_property(&connector
->base
,
881 dev
->mode_config
.dpms_property
, 0);
883 connector
->debugfs_entry
= NULL
;
887 drm_mode_object_put(dev
, &connector
->base
);
890 drm_modeset_unlock_all(dev
);
894 EXPORT_SYMBOL(drm_connector_init
);
897 * drm_connector_cleanup - cleans up an initialised connector
898 * @connector: connector to cleanup
900 * Cleans up the connector but doesn't free the object.
902 void drm_connector_cleanup(struct drm_connector
*connector
)
904 struct drm_device
*dev
= connector
->dev
;
905 struct drm_display_mode
*mode
, *t
;
907 if (connector
->tile_group
) {
908 drm_mode_put_tile_group(dev
, connector
->tile_group
);
909 connector
->tile_group
= NULL
;
912 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
913 drm_mode_remove(connector
, mode
);
915 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
916 drm_mode_remove(connector
, mode
);
918 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
919 connector
->connector_type_id
);
921 drm_mode_object_put(dev
, &connector
->base
);
922 kfree(connector
->name
);
923 connector
->name
= NULL
;
924 list_del(&connector
->head
);
925 dev
->mode_config
.num_connector
--;
927 WARN_ON(connector
->state
&& !connector
->funcs
->atomic_destroy_state
);
928 if (connector
->state
&& connector
->funcs
->atomic_destroy_state
)
929 connector
->funcs
->atomic_destroy_state(connector
,
932 memset(connector
, 0, sizeof(*connector
));
934 EXPORT_SYMBOL(drm_connector_cleanup
);
937 * drm_connector_index - find the index of a registered connector
938 * @connector: connector to find index for
940 * Given a registered connector, return the index of that connector within a DRM
941 * device's list of connectors.
943 unsigned int drm_connector_index(struct drm_connector
*connector
)
945 unsigned int index
= 0;
946 struct drm_connector
*tmp
;
947 struct drm_mode_config
*config
= &connector
->dev
->mode_config
;
949 WARN_ON(!drm_modeset_is_locked(&config
->connection_mutex
));
951 list_for_each_entry(tmp
, &connector
->dev
->mode_config
.connector_list
, head
) {
952 if (tmp
== connector
)
960 EXPORT_SYMBOL(drm_connector_index
);
963 * drm_connector_register - register a connector
964 * @connector: the connector to register
966 * Register userspace interfaces for a connector
969 * Zero on success, error code on failure.
971 int drm_connector_register(struct drm_connector
*connector
)
975 drm_mode_object_register(connector
->dev
, &connector
->base
);
977 ret
= drm_sysfs_connector_add(connector
);
981 ret
= drm_debugfs_connector_add(connector
);
983 drm_sysfs_connector_remove(connector
);
989 EXPORT_SYMBOL(drm_connector_register
);
992 * drm_connector_unregister - unregister a connector
993 * @connector: the connector to unregister
995 * Unregister userspace interfaces for a connector
997 void drm_connector_unregister(struct drm_connector
*connector
)
999 drm_sysfs_connector_remove(connector
);
1000 drm_debugfs_connector_remove(connector
);
1002 EXPORT_SYMBOL(drm_connector_unregister
);
1006 * drm_connector_unplug_all - unregister connector userspace interfaces
1009 * This function unregisters all connector userspace interfaces in sysfs. Should
1010 * be call when the device is disconnected, e.g. from an usb driver's
1011 * ->disconnect callback.
1013 void drm_connector_unplug_all(struct drm_device
*dev
)
1015 struct drm_connector
*connector
;
1017 /* taking the mode config mutex ends up in a clash with sysfs */
1018 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1019 drm_connector_unregister(connector
);
1022 EXPORT_SYMBOL(drm_connector_unplug_all
);
1025 * drm_bridge_init - initialize a drm transcoder/bridge
1027 * @bridge: transcoder/bridge to set up
1028 * @funcs: bridge function table
1030 * Initialises a preallocated bridge. Bridges should be
1031 * subclassed as part of driver connector objects.
1034 * Zero on success, error code on failure.
1036 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
1037 const struct drm_bridge_funcs
*funcs
)
1041 drm_modeset_lock_all(dev
);
1043 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1048 bridge
->funcs
= funcs
;
1050 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1051 dev
->mode_config
.num_bridge
++;
1054 drm_modeset_unlock_all(dev
);
1057 EXPORT_SYMBOL(drm_bridge_init
);
1060 * drm_bridge_cleanup - cleans up an initialised bridge
1061 * @bridge: bridge to cleanup
1063 * Cleans up the bridge but doesn't free the object.
1065 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1067 struct drm_device
*dev
= bridge
->dev
;
1069 drm_modeset_lock_all(dev
);
1070 drm_mode_object_put(dev
, &bridge
->base
);
1071 list_del(&bridge
->head
);
1072 dev
->mode_config
.num_bridge
--;
1073 drm_modeset_unlock_all(dev
);
1075 memset(bridge
, 0, sizeof(*bridge
));
1077 EXPORT_SYMBOL(drm_bridge_cleanup
);
1080 * drm_encoder_init - Init a preallocated encoder
1082 * @encoder: the encoder to init
1083 * @funcs: callbacks for this encoder
1084 * @encoder_type: user visible type of the encoder
1086 * Initialises a preallocated encoder. Encoder should be
1087 * subclassed as part of driver encoder objects.
1090 * Zero on success, error code on failure.
1092 int drm_encoder_init(struct drm_device
*dev
,
1093 struct drm_encoder
*encoder
,
1094 const struct drm_encoder_funcs
*funcs
,
1099 drm_modeset_lock_all(dev
);
1101 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1106 encoder
->encoder_type
= encoder_type
;
1107 encoder
->funcs
= funcs
;
1108 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1109 drm_encoder_enum_list
[encoder_type
].name
,
1111 if (!encoder
->name
) {
1116 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1117 dev
->mode_config
.num_encoder
++;
1121 drm_mode_object_put(dev
, &encoder
->base
);
1124 drm_modeset_unlock_all(dev
);
1128 EXPORT_SYMBOL(drm_encoder_init
);
1131 * drm_encoder_cleanup - cleans up an initialised encoder
1132 * @encoder: encoder to cleanup
1134 * Cleans up the encoder but doesn't free the object.
1136 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1138 struct drm_device
*dev
= encoder
->dev
;
1140 drm_modeset_lock_all(dev
);
1141 drm_mode_object_put(dev
, &encoder
->base
);
1142 kfree(encoder
->name
);
1143 list_del(&encoder
->head
);
1144 dev
->mode_config
.num_encoder
--;
1145 drm_modeset_unlock_all(dev
);
1147 memset(encoder
, 0, sizeof(*encoder
));
1149 EXPORT_SYMBOL(drm_encoder_cleanup
);
1152 * drm_universal_plane_init - Initialize a new universal plane object
1154 * @plane: plane object to init
1155 * @possible_crtcs: bitmask of possible CRTCs
1156 * @funcs: callbacks for the new plane
1157 * @formats: array of supported formats (%DRM_FORMAT_*)
1158 * @format_count: number of elements in @formats
1159 * @type: type of plane (overlay, primary, cursor)
1161 * Initializes a plane object of type @type.
1164 * Zero on success, error code on failure.
1166 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1167 unsigned long possible_crtcs
,
1168 const struct drm_plane_funcs
*funcs
,
1169 const uint32_t *formats
, uint32_t format_count
,
1170 enum drm_plane_type type
)
1174 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1178 drm_modeset_lock_init(&plane
->mutex
);
1180 plane
->base
.properties
= &plane
->properties
;
1182 plane
->funcs
= funcs
;
1183 plane
->format_types
= kmalloc_array(format_count
, sizeof(uint32_t),
1185 if (!plane
->format_types
) {
1186 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1187 drm_mode_object_put(dev
, &plane
->base
);
1191 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1192 plane
->format_count
= format_count
;
1193 plane
->possible_crtcs
= possible_crtcs
;
1196 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1197 dev
->mode_config
.num_total_plane
++;
1198 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1199 dev
->mode_config
.num_overlay_plane
++;
1201 drm_object_attach_property(&plane
->base
,
1202 dev
->mode_config
.plane_type_property
,
1207 EXPORT_SYMBOL(drm_universal_plane_init
);
1210 * drm_plane_init - Initialize a legacy plane
1212 * @plane: plane object to init
1213 * @possible_crtcs: bitmask of possible CRTCs
1214 * @funcs: callbacks for the new plane
1215 * @formats: array of supported formats (%DRM_FORMAT_*)
1216 * @format_count: number of elements in @formats
1217 * @is_primary: plane type (primary vs overlay)
1219 * Legacy API to initialize a DRM plane.
1221 * New drivers should call drm_universal_plane_init() instead.
1224 * Zero on success, error code on failure.
1226 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1227 unsigned long possible_crtcs
,
1228 const struct drm_plane_funcs
*funcs
,
1229 const uint32_t *formats
, uint32_t format_count
,
1232 enum drm_plane_type type
;
1234 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1235 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1236 formats
, format_count
, type
);
1238 EXPORT_SYMBOL(drm_plane_init
);
1241 * drm_plane_cleanup - Clean up the core plane usage
1242 * @plane: plane to cleanup
1244 * This function cleans up @plane and removes it from the DRM mode setting
1245 * core. Note that the function does *not* free the plane structure itself,
1246 * this is the responsibility of the caller.
1248 void drm_plane_cleanup(struct drm_plane
*plane
)
1250 struct drm_device
*dev
= plane
->dev
;
1252 drm_modeset_lock_all(dev
);
1253 kfree(plane
->format_types
);
1254 drm_mode_object_put(dev
, &plane
->base
);
1256 BUG_ON(list_empty(&plane
->head
));
1258 list_del(&plane
->head
);
1259 dev
->mode_config
.num_total_plane
--;
1260 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1261 dev
->mode_config
.num_overlay_plane
--;
1262 drm_modeset_unlock_all(dev
);
1264 WARN_ON(plane
->state
&& !plane
->funcs
->atomic_destroy_state
);
1265 if (plane
->state
&& plane
->funcs
->atomic_destroy_state
)
1266 plane
->funcs
->atomic_destroy_state(plane
, plane
->state
);
1268 memset(plane
, 0, sizeof(*plane
));
1270 EXPORT_SYMBOL(drm_plane_cleanup
);
1273 * drm_plane_index - find the index of a registered plane
1274 * @plane: plane to find index for
1276 * Given a registered plane, return the index of that CRTC within a DRM
1277 * device's list of planes.
1279 unsigned int drm_plane_index(struct drm_plane
*plane
)
1281 unsigned int index
= 0;
1282 struct drm_plane
*tmp
;
1284 list_for_each_entry(tmp
, &plane
->dev
->mode_config
.plane_list
, head
) {
1293 EXPORT_SYMBOL(drm_plane_index
);
1296 * drm_plane_force_disable - Forcibly disable a plane
1297 * @plane: plane to disable
1299 * Forces the plane to be disabled.
1301 * Used when the plane's current framebuffer is destroyed,
1302 * and when restoring fbdev mode.
1304 void drm_plane_force_disable(struct drm_plane
*plane
)
1311 plane
->old_fb
= plane
->fb
;
1312 ret
= plane
->funcs
->disable_plane(plane
);
1314 DRM_ERROR("failed to disable plane with busy fb\n");
1315 plane
->old_fb
= NULL
;
1318 /* disconnect the plane from the fb and crtc: */
1319 __drm_framebuffer_unreference(plane
->old_fb
);
1320 plane
->old_fb
= NULL
;
1324 EXPORT_SYMBOL(drm_plane_force_disable
);
1326 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1328 struct drm_property
*edid
;
1329 struct drm_property
*dpms
;
1330 struct drm_property
*dev_path
;
1333 * Standard properties (apply to all connectors)
1335 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1336 DRM_MODE_PROP_IMMUTABLE
,
1338 dev
->mode_config
.edid_property
= edid
;
1340 dpms
= drm_property_create_enum(dev
, 0,
1341 "DPMS", drm_dpms_enum_list
,
1342 ARRAY_SIZE(drm_dpms_enum_list
));
1343 dev
->mode_config
.dpms_property
= dpms
;
1345 dev_path
= drm_property_create(dev
,
1346 DRM_MODE_PROP_BLOB
|
1347 DRM_MODE_PROP_IMMUTABLE
,
1349 dev
->mode_config
.path_property
= dev_path
;
1351 dev
->mode_config
.tile_property
= drm_property_create(dev
,
1352 DRM_MODE_PROP_BLOB
|
1353 DRM_MODE_PROP_IMMUTABLE
,
1359 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1361 struct drm_property
*type
;
1364 * Standard properties (apply to all planes)
1366 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1367 "type", drm_plane_type_enum_list
,
1368 ARRAY_SIZE(drm_plane_type_enum_list
));
1369 dev
->mode_config
.plane_type_property
= type
;
1375 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1378 * Called by a driver the first time a DVI-I connector is made.
1380 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1382 struct drm_property
*dvi_i_selector
;
1383 struct drm_property
*dvi_i_subconnector
;
1385 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1389 drm_property_create_enum(dev
, 0,
1390 "select subconnector",
1391 drm_dvi_i_select_enum_list
,
1392 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1393 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1395 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1397 drm_dvi_i_subconnector_enum_list
,
1398 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1399 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1403 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1406 * drm_create_tv_properties - create TV specific connector properties
1408 * @num_modes: number of different TV formats (modes) supported
1409 * @modes: array of pointers to strings containing name of each format
1411 * Called by a driver's TV initialization routine, this function creates
1412 * the TV specific connector properties for a given device. Caller is
1413 * responsible for allocating a list of format names and passing them to
1416 int drm_mode_create_tv_properties(struct drm_device
*dev
,
1417 unsigned int num_modes
,
1420 struct drm_property
*tv_selector
;
1421 struct drm_property
*tv_subconnector
;
1424 if (dev
->mode_config
.tv_select_subconnector_property
)
1428 * Basic connector properties
1430 tv_selector
= drm_property_create_enum(dev
, 0,
1431 "select subconnector",
1432 drm_tv_select_enum_list
,
1433 ARRAY_SIZE(drm_tv_select_enum_list
));
1434 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1437 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1439 drm_tv_subconnector_enum_list
,
1440 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1441 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1444 * Other, TV specific properties: margins & TV modes.
1446 dev
->mode_config
.tv_left_margin_property
=
1447 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1449 dev
->mode_config
.tv_right_margin_property
=
1450 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1452 dev
->mode_config
.tv_top_margin_property
=
1453 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1455 dev
->mode_config
.tv_bottom_margin_property
=
1456 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1458 dev
->mode_config
.tv_mode_property
=
1459 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1461 for (i
= 0; i
< num_modes
; i
++)
1462 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1465 dev
->mode_config
.tv_brightness_property
=
1466 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1468 dev
->mode_config
.tv_contrast_property
=
1469 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1471 dev
->mode_config
.tv_flicker_reduction_property
=
1472 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1474 dev
->mode_config
.tv_overscan_property
=
1475 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1477 dev
->mode_config
.tv_saturation_property
=
1478 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1480 dev
->mode_config
.tv_hue_property
=
1481 drm_property_create_range(dev
, 0, "hue", 0, 100);
1485 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1488 * drm_mode_create_scaling_mode_property - create scaling mode property
1491 * Called by a driver the first time it's needed, must be attached to desired
1494 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1496 struct drm_property
*scaling_mode
;
1498 if (dev
->mode_config
.scaling_mode_property
)
1502 drm_property_create_enum(dev
, 0, "scaling mode",
1503 drm_scaling_mode_enum_list
,
1504 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1506 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1510 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1513 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1516 * Called by a driver the first time it's needed, must be attached to desired
1520 * Zero on success, negative errno on failure.
1522 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1524 if (dev
->mode_config
.aspect_ratio_property
)
1527 dev
->mode_config
.aspect_ratio_property
=
1528 drm_property_create_enum(dev
, 0, "aspect ratio",
1529 drm_aspect_ratio_enum_list
,
1530 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1532 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1537 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1540 * drm_mode_create_dirty_property - create dirty property
1543 * Called by a driver the first time it's needed, must be attached to desired
1546 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1548 struct drm_property
*dirty_info
;
1550 if (dev
->mode_config
.dirty_info_property
)
1554 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1556 drm_dirty_info_enum_list
,
1557 ARRAY_SIZE(drm_dirty_info_enum_list
));
1558 dev
->mode_config
.dirty_info_property
= dirty_info
;
1562 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1565 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1568 * Create the the suggested x/y offset property for connectors.
1570 int drm_mode_create_suggested_offset_properties(struct drm_device
*dev
)
1572 if (dev
->mode_config
.suggested_x_property
&& dev
->mode_config
.suggested_y_property
)
1575 dev
->mode_config
.suggested_x_property
=
1576 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested X", 0, 0xffffffff);
1578 dev
->mode_config
.suggested_y_property
=
1579 drm_property_create_range(dev
, DRM_MODE_PROP_IMMUTABLE
, "suggested Y", 0, 0xffffffff);
1581 if (dev
->mode_config
.suggested_x_property
== NULL
||
1582 dev
->mode_config
.suggested_y_property
== NULL
)
1586 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties
);
1588 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1590 uint32_t total_objects
= 0;
1592 total_objects
+= dev
->mode_config
.num_crtc
;
1593 total_objects
+= dev
->mode_config
.num_connector
;
1594 total_objects
+= dev
->mode_config
.num_encoder
;
1595 total_objects
+= dev
->mode_config
.num_bridge
;
1597 group
->id_list
= kcalloc(total_objects
, sizeof(uint32_t), GFP_KERNEL
);
1598 if (!group
->id_list
)
1601 group
->num_crtcs
= 0;
1602 group
->num_connectors
= 0;
1603 group
->num_encoders
= 0;
1604 group
->num_bridges
= 0;
1608 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1610 kfree(group
->id_list
);
1611 group
->id_list
= NULL
;
1615 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1616 * the drm core's responsibility to set up mode control groups.
1618 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1619 struct drm_mode_group
*group
)
1621 struct drm_crtc
*crtc
;
1622 struct drm_encoder
*encoder
;
1623 struct drm_connector
*connector
;
1624 struct drm_bridge
*bridge
;
1627 ret
= drm_mode_group_init(dev
, group
);
1631 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1632 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1634 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1635 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1638 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1639 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1640 group
->num_connectors
++] = connector
->base
.id
;
1642 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1643 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1644 group
->num_connectors
+ group
->num_bridges
++] =
1649 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1651 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1653 drm_modeset_lock_all(dev
);
1654 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1655 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1656 drm_modeset_unlock_all(dev
);
1658 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1661 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1662 * @out: drm_mode_modeinfo struct to return to the user
1663 * @in: drm_display_mode to use
1665 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1668 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1669 const struct drm_display_mode
*in
)
1671 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1672 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1673 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1674 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1675 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1676 "timing values too large for mode info\n");
1678 out
->clock
= in
->clock
;
1679 out
->hdisplay
= in
->hdisplay
;
1680 out
->hsync_start
= in
->hsync_start
;
1681 out
->hsync_end
= in
->hsync_end
;
1682 out
->htotal
= in
->htotal
;
1683 out
->hskew
= in
->hskew
;
1684 out
->vdisplay
= in
->vdisplay
;
1685 out
->vsync_start
= in
->vsync_start
;
1686 out
->vsync_end
= in
->vsync_end
;
1687 out
->vtotal
= in
->vtotal
;
1688 out
->vscan
= in
->vscan
;
1689 out
->vrefresh
= in
->vrefresh
;
1690 out
->flags
= in
->flags
;
1691 out
->type
= in
->type
;
1692 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1693 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1697 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1698 * @out: drm_display_mode to return to the user
1699 * @in: drm_mode_modeinfo to use
1701 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1705 * Zero on success, negative errno on failure.
1707 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1708 const struct drm_mode_modeinfo
*in
)
1710 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1713 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1716 out
->clock
= in
->clock
;
1717 out
->hdisplay
= in
->hdisplay
;
1718 out
->hsync_start
= in
->hsync_start
;
1719 out
->hsync_end
= in
->hsync_end
;
1720 out
->htotal
= in
->htotal
;
1721 out
->hskew
= in
->hskew
;
1722 out
->vdisplay
= in
->vdisplay
;
1723 out
->vsync_start
= in
->vsync_start
;
1724 out
->vsync_end
= in
->vsync_end
;
1725 out
->vtotal
= in
->vtotal
;
1726 out
->vscan
= in
->vscan
;
1727 out
->vrefresh
= in
->vrefresh
;
1728 out
->flags
= in
->flags
;
1729 out
->type
= in
->type
;
1730 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1731 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1737 * drm_mode_getresources - get graphics configuration
1738 * @dev: drm device for the ioctl
1739 * @data: data pointer for the ioctl
1740 * @file_priv: drm file for the ioctl call
1742 * Construct a set of configuration description structures and return
1743 * them to the user, including CRTC, connector and framebuffer configuration.
1745 * Called by the user via ioctl.
1748 * Zero on success, negative errno on failure.
1750 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1751 struct drm_file
*file_priv
)
1753 struct drm_mode_card_res
*card_res
= data
;
1754 struct list_head
*lh
;
1755 struct drm_framebuffer
*fb
;
1756 struct drm_connector
*connector
;
1757 struct drm_crtc
*crtc
;
1758 struct drm_encoder
*encoder
;
1760 int connector_count
= 0;
1763 int encoder_count
= 0;
1765 uint32_t __user
*fb_id
;
1766 uint32_t __user
*crtc_id
;
1767 uint32_t __user
*connector_id
;
1768 uint32_t __user
*encoder_id
;
1769 struct drm_mode_group
*mode_group
;
1771 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1775 mutex_lock(&file_priv
->fbs_lock
);
1777 * For the non-control nodes we need to limit the list of resources
1778 * by IDs in the group list for this node
1780 list_for_each(lh
, &file_priv
->fbs
)
1783 /* handle this in 4 parts */
1785 if (card_res
->count_fbs
>= fb_count
) {
1787 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1788 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1789 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1790 mutex_unlock(&file_priv
->fbs_lock
);
1796 card_res
->count_fbs
= fb_count
;
1797 mutex_unlock(&file_priv
->fbs_lock
);
1799 /* mode_config.mutex protects the connector list against e.g. DP MST
1800 * connector hot-adding. CRTC/Plane lists are invariant. */
1801 mutex_lock(&dev
->mode_config
.mutex
);
1802 if (!drm_is_primary_client(file_priv
)) {
1805 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1808 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1811 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1815 mode_group
= &file_priv
->master
->minor
->mode_group
;
1816 crtc_count
= mode_group
->num_crtcs
;
1817 connector_count
= mode_group
->num_connectors
;
1818 encoder_count
= mode_group
->num_encoders
;
1821 card_res
->max_height
= dev
->mode_config
.max_height
;
1822 card_res
->min_height
= dev
->mode_config
.min_height
;
1823 card_res
->max_width
= dev
->mode_config
.max_width
;
1824 card_res
->min_width
= dev
->mode_config
.min_width
;
1827 if (card_res
->count_crtcs
>= crtc_count
) {
1829 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1831 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1833 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1834 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1841 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1842 if (put_user(mode_group
->id_list
[i
],
1843 crtc_id
+ copied
)) {
1851 card_res
->count_crtcs
= crtc_count
;
1854 if (card_res
->count_encoders
>= encoder_count
) {
1856 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1858 list_for_each_entry(encoder
,
1859 &dev
->mode_config
.encoder_list
,
1861 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1863 if (put_user(encoder
->base
.id
, encoder_id
+
1871 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1872 if (put_user(mode_group
->id_list
[i
],
1873 encoder_id
+ copied
)) {
1882 card_res
->count_encoders
= encoder_count
;
1885 if (card_res
->count_connectors
>= connector_count
) {
1887 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1889 list_for_each_entry(connector
,
1890 &dev
->mode_config
.connector_list
,
1892 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1895 if (put_user(connector
->base
.id
,
1896 connector_id
+ copied
)) {
1903 int start
= mode_group
->num_crtcs
+
1904 mode_group
->num_encoders
;
1905 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1906 if (put_user(mode_group
->id_list
[i
],
1907 connector_id
+ copied
)) {
1915 card_res
->count_connectors
= connector_count
;
1917 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1918 card_res
->count_connectors
, card_res
->count_encoders
);
1921 mutex_unlock(&dev
->mode_config
.mutex
);
1926 * drm_mode_getcrtc - get CRTC configuration
1927 * @dev: drm device for the ioctl
1928 * @data: data pointer for the ioctl
1929 * @file_priv: drm file for the ioctl call
1931 * Construct a CRTC configuration structure to return to the user.
1933 * Called by the user via ioctl.
1936 * Zero on success, negative errno on failure.
1938 int drm_mode_getcrtc(struct drm_device
*dev
,
1939 void *data
, struct drm_file
*file_priv
)
1941 struct drm_mode_crtc
*crtc_resp
= data
;
1942 struct drm_crtc
*crtc
;
1944 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1947 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1951 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
1952 crtc_resp
->x
= crtc
->x
;
1953 crtc_resp
->y
= crtc
->y
;
1954 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1955 if (crtc
->primary
->fb
)
1956 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1958 crtc_resp
->fb_id
= 0;
1960 if (crtc
->enabled
) {
1962 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1963 crtc_resp
->mode_valid
= 1;
1966 crtc_resp
->mode_valid
= 0;
1968 drm_modeset_unlock_crtc(crtc
);
1973 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1974 const struct drm_file
*file_priv
)
1977 * If user-space hasn't configured the driver to expose the stereo 3D
1978 * modes, don't expose them.
1980 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1986 static struct drm_encoder
*drm_connector_get_encoder(struct drm_connector
*connector
)
1988 /* For atomic drivers only state objects are synchronously updated and
1989 * protected by modeset locks, so check those first. */
1990 if (connector
->state
)
1991 return connector
->state
->best_encoder
;
1992 return connector
->encoder
;
1995 /* helper for getconnector and getproperties ioctls */
1996 static int get_properties(struct drm_mode_object
*obj
, bool atomic
,
1997 uint32_t __user
*prop_ptr
, uint64_t __user
*prop_values
,
1998 uint32_t *arg_count_props
)
2003 props_count
= obj
->properties
->count
;
2005 props_count
-= obj
->properties
->atomic_count
;
2007 if ((*arg_count_props
>= props_count
) && props_count
) {
2008 for (i
= 0, copied
= 0; copied
< props_count
; i
++) {
2009 struct drm_property
*prop
= obj
->properties
->properties
[i
];
2012 if ((prop
->flags
& DRM_MODE_PROP_ATOMIC
) && !atomic
)
2015 ret
= drm_object_property_get_value(obj
, prop
, &val
);
2019 if (put_user(prop
->base
.id
, prop_ptr
+ copied
))
2022 if (put_user(val
, prop_values
+ copied
))
2028 *arg_count_props
= props_count
;
2034 * drm_mode_getconnector - get connector configuration
2035 * @dev: drm device for the ioctl
2036 * @data: data pointer for the ioctl
2037 * @file_priv: drm file for the ioctl call
2039 * Construct a connector configuration structure to return to the user.
2041 * Called by the user via ioctl.
2044 * Zero on success, negative errno on failure.
2046 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
2047 struct drm_file
*file_priv
)
2049 struct drm_mode_get_connector
*out_resp
= data
;
2050 struct drm_connector
*connector
;
2051 struct drm_encoder
*encoder
;
2052 struct drm_display_mode
*mode
;
2054 int encoders_count
= 0;
2058 struct drm_mode_modeinfo u_mode
;
2059 struct drm_mode_modeinfo __user
*mode_ptr
;
2060 uint32_t __user
*encoder_ptr
;
2062 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2065 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
2067 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
2069 mutex_lock(&dev
->mode_config
.mutex
);
2070 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2072 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
2078 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++)
2079 if (connector
->encoder_ids
[i
] != 0)
2082 if (out_resp
->count_modes
== 0) {
2083 connector
->funcs
->fill_modes(connector
,
2084 dev
->mode_config
.max_width
,
2085 dev
->mode_config
.max_height
);
2088 /* delayed so we get modes regardless of pre-fill_modes state */
2089 list_for_each_entry(mode
, &connector
->modes
, head
)
2090 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2093 out_resp
->connector_id
= connector
->base
.id
;
2094 out_resp
->connector_type
= connector
->connector_type
;
2095 out_resp
->connector_type_id
= connector
->connector_type_id
;
2096 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2097 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2098 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2099 out_resp
->connection
= connector
->status
;
2100 encoder
= drm_connector_get_encoder(connector
);
2102 out_resp
->encoder_id
= encoder
->base
.id
;
2104 out_resp
->encoder_id
= 0;
2107 * This ioctl is called twice, once to determine how much space is
2108 * needed, and the 2nd time to fill it.
2110 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2112 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2113 list_for_each_entry(mode
, &connector
->modes
, head
) {
2114 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2117 drm_crtc_convert_to_umode(&u_mode
, mode
);
2118 if (copy_to_user(mode_ptr
+ copied
,
2119 &u_mode
, sizeof(u_mode
))) {
2126 out_resp
->count_modes
= mode_count
;
2128 ret
= get_properties(&connector
->base
, file_priv
->atomic
,
2129 (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
),
2130 (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
),
2131 &out_resp
->count_props
);
2135 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2137 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2138 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2139 if (connector
->encoder_ids
[i
] != 0) {
2140 if (put_user(connector
->encoder_ids
[i
],
2141 encoder_ptr
+ copied
)) {
2149 out_resp
->count_encoders
= encoders_count
;
2152 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2153 mutex_unlock(&dev
->mode_config
.mutex
);
2158 static struct drm_crtc
*drm_encoder_get_crtc(struct drm_encoder
*encoder
)
2160 struct drm_connector
*connector
;
2161 struct drm_device
*dev
= encoder
->dev
;
2162 bool uses_atomic
= false;
2164 /* For atomic drivers only state objects are synchronously updated and
2165 * protected by modeset locks, so check those first. */
2166 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
2167 if (!connector
->state
)
2172 if (connector
->state
->best_encoder
!= encoder
)
2175 return connector
->state
->crtc
;
2178 /* Don't return stale data (e.g. pending async disable). */
2182 return encoder
->crtc
;
2186 * drm_mode_getencoder - get encoder configuration
2187 * @dev: drm device for the ioctl
2188 * @data: data pointer for the ioctl
2189 * @file_priv: drm file for the ioctl call
2191 * Construct a encoder configuration structure to return to the user.
2193 * Called by the user via ioctl.
2196 * Zero on success, negative errno on failure.
2198 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2199 struct drm_file
*file_priv
)
2201 struct drm_mode_get_encoder
*enc_resp
= data
;
2202 struct drm_encoder
*encoder
;
2203 struct drm_crtc
*crtc
;
2205 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2208 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2212 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2213 crtc
= drm_encoder_get_crtc(encoder
);
2215 enc_resp
->crtc_id
= crtc
->base
.id
;
2216 else if (encoder
->crtc
)
2217 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2219 enc_resp
->crtc_id
= 0;
2220 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2222 enc_resp
->encoder_type
= encoder
->encoder_type
;
2223 enc_resp
->encoder_id
= encoder
->base
.id
;
2224 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2225 enc_resp
->possible_clones
= encoder
->possible_clones
;
2231 * drm_mode_getplane_res - enumerate all plane resources
2234 * @file_priv: DRM file info
2236 * Construct a list of plane ids to return to the user.
2238 * Called by the user via ioctl.
2241 * Zero on success, negative errno on failure.
2243 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2244 struct drm_file
*file_priv
)
2246 struct drm_mode_get_plane_res
*plane_resp
= data
;
2247 struct drm_mode_config
*config
;
2248 struct drm_plane
*plane
;
2249 uint32_t __user
*plane_ptr
;
2251 unsigned num_planes
;
2253 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2256 config
= &dev
->mode_config
;
2258 if (file_priv
->universal_planes
)
2259 num_planes
= config
->num_total_plane
;
2261 num_planes
= config
->num_overlay_plane
;
2264 * This ioctl is called twice, once to determine how much space is
2265 * needed, and the 2nd time to fill it.
2268 (plane_resp
->count_planes
>= num_planes
)) {
2269 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2271 /* Plane lists are invariant, no locking needed. */
2272 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2274 * Unless userspace set the 'universal planes'
2275 * capability bit, only advertise overlays.
2277 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2278 !file_priv
->universal_planes
)
2281 if (put_user(plane
->base
.id
, plane_ptr
+ copied
))
2286 plane_resp
->count_planes
= num_planes
;
2292 * drm_mode_getplane - get plane configuration
2295 * @file_priv: DRM file info
2297 * Construct a plane configuration structure to return to the user.
2299 * Called by the user via ioctl.
2302 * Zero on success, negative errno on failure.
2304 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2305 struct drm_file
*file_priv
)
2307 struct drm_mode_get_plane
*plane_resp
= data
;
2308 struct drm_plane
*plane
;
2309 uint32_t __user
*format_ptr
;
2311 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2314 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2318 drm_modeset_lock(&plane
->mutex
, NULL
);
2320 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2322 plane_resp
->crtc_id
= 0;
2325 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2327 plane_resp
->fb_id
= 0;
2328 drm_modeset_unlock(&plane
->mutex
);
2330 plane_resp
->plane_id
= plane
->base
.id
;
2331 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2332 plane_resp
->gamma_size
= 0;
2335 * This ioctl is called twice, once to determine how much space is
2336 * needed, and the 2nd time to fill it.
2338 if (plane
->format_count
&&
2339 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2340 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2341 if (copy_to_user(format_ptr
,
2342 plane
->format_types
,
2343 sizeof(uint32_t) * plane
->format_count
)) {
2347 plane_resp
->count_format_types
= plane
->format_count
;
2353 * setplane_internal - setplane handler for internal callers
2355 * Note that we assume an extra reference has already been taken on fb. If the
2356 * update fails, this reference will be dropped before return; if it succeeds,
2357 * the previous framebuffer (if any) will be unreferenced instead.
2359 * src_{x,y,w,h} are provided in 16.16 fixed point format
2361 static int __setplane_internal(struct drm_plane
*plane
,
2362 struct drm_crtc
*crtc
,
2363 struct drm_framebuffer
*fb
,
2364 int32_t crtc_x
, int32_t crtc_y
,
2365 uint32_t crtc_w
, uint32_t crtc_h
,
2366 /* src_{x,y,w,h} values are 16.16 fixed point */
2367 uint32_t src_x
, uint32_t src_y
,
2368 uint32_t src_w
, uint32_t src_h
)
2371 unsigned int fb_width
, fb_height
;
2374 /* No fb means shut it down */
2376 plane
->old_fb
= plane
->fb
;
2377 ret
= plane
->funcs
->disable_plane(plane
);
2382 plane
->old_fb
= NULL
;
2387 /* Check whether this plane is usable on this CRTC */
2388 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2389 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2394 /* Check whether this plane supports the fb pixel format. */
2395 for (i
= 0; i
< plane
->format_count
; i
++)
2396 if (fb
->pixel_format
== plane
->format_types
[i
])
2398 if (i
== plane
->format_count
) {
2399 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2400 drm_get_format_name(fb
->pixel_format
));
2405 fb_width
= fb
->width
<< 16;
2406 fb_height
= fb
->height
<< 16;
2408 /* Make sure source coordinates are inside the fb. */
2409 if (src_w
> fb_width
||
2410 src_x
> fb_width
- src_w
||
2411 src_h
> fb_height
||
2412 src_y
> fb_height
- src_h
) {
2413 DRM_DEBUG_KMS("Invalid source coordinates "
2414 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2415 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2416 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2417 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2418 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2423 plane
->old_fb
= plane
->fb
;
2424 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2425 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2426 src_x
, src_y
, src_w
, src_h
);
2432 plane
->old_fb
= NULL
;
2437 drm_framebuffer_unreference(fb
);
2439 drm_framebuffer_unreference(plane
->old_fb
);
2440 plane
->old_fb
= NULL
;
2445 static int setplane_internal(struct drm_plane
*plane
,
2446 struct drm_crtc
*crtc
,
2447 struct drm_framebuffer
*fb
,
2448 int32_t crtc_x
, int32_t crtc_y
,
2449 uint32_t crtc_w
, uint32_t crtc_h
,
2450 /* src_{x,y,w,h} values are 16.16 fixed point */
2451 uint32_t src_x
, uint32_t src_y
,
2452 uint32_t src_w
, uint32_t src_h
)
2456 drm_modeset_lock_all(plane
->dev
);
2457 ret
= __setplane_internal(plane
, crtc
, fb
,
2458 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2459 src_x
, src_y
, src_w
, src_h
);
2460 drm_modeset_unlock_all(plane
->dev
);
2466 * drm_mode_setplane - configure a plane's configuration
2468 * @data: ioctl data*
2469 * @file_priv: DRM file info
2471 * Set plane configuration, including placement, fb, scaling, and other factors.
2472 * Or pass a NULL fb to disable (planes may be disabled without providing a
2476 * Zero on success, negative errno on failure.
2478 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2479 struct drm_file
*file_priv
)
2481 struct drm_mode_set_plane
*plane_req
= data
;
2482 struct drm_plane
*plane
;
2483 struct drm_crtc
*crtc
= NULL
;
2484 struct drm_framebuffer
*fb
= NULL
;
2486 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2489 /* Give drivers some help against integer overflows */
2490 if (plane_req
->crtc_w
> INT_MAX
||
2491 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2492 plane_req
->crtc_h
> INT_MAX
||
2493 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2494 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2495 plane_req
->crtc_w
, plane_req
->crtc_h
,
2496 plane_req
->crtc_x
, plane_req
->crtc_y
);
2501 * First, find the plane, crtc, and fb objects. If not available,
2502 * we don't bother to call the driver.
2504 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2506 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2507 plane_req
->plane_id
);
2511 if (plane_req
->fb_id
) {
2512 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2514 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2519 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2521 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2522 plane_req
->crtc_id
);
2528 * setplane_internal will take care of deref'ing either the old or new
2529 * framebuffer depending on success.
2531 return setplane_internal(plane
, crtc
, fb
,
2532 plane_req
->crtc_x
, plane_req
->crtc_y
,
2533 plane_req
->crtc_w
, plane_req
->crtc_h
,
2534 plane_req
->src_x
, plane_req
->src_y
,
2535 plane_req
->src_w
, plane_req
->src_h
);
2539 * drm_mode_set_config_internal - helper to call ->set_config
2540 * @set: modeset config to set
2542 * This is a little helper to wrap internal calls to the ->set_config driver
2543 * interface. The only thing it adds is correct refcounting dance.
2546 * Zero on success, negative errno on failure.
2548 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2550 struct drm_crtc
*crtc
= set
->crtc
;
2551 struct drm_framebuffer
*fb
;
2552 struct drm_crtc
*tmp
;
2556 * NOTE: ->set_config can also disable other crtcs (if we steal all
2557 * connectors from it), hence we need to refcount the fbs across all
2558 * crtcs. Atomic modeset will have saner semantics ...
2560 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2561 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2565 ret
= crtc
->funcs
->set_config(set
);
2567 crtc
->primary
->crtc
= crtc
;
2568 crtc
->primary
->fb
= fb
;
2571 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2572 if (tmp
->primary
->fb
)
2573 drm_framebuffer_reference(tmp
->primary
->fb
);
2574 if (tmp
->primary
->old_fb
)
2575 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2576 tmp
->primary
->old_fb
= NULL
;
2581 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2584 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2586 * @crtc: CRTC that framebuffer will be displayed on
2589 * @mode: mode that framebuffer will be displayed under
2590 * @fb: framebuffer to check size of
2592 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2594 const struct drm_display_mode
*mode
,
2595 const struct drm_framebuffer
*fb
)
2598 int hdisplay
, vdisplay
;
2600 hdisplay
= mode
->hdisplay
;
2601 vdisplay
= mode
->vdisplay
;
2603 if (drm_mode_is_stereo(mode
)) {
2604 struct drm_display_mode adjusted
= *mode
;
2606 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2607 hdisplay
= adjusted
.crtc_hdisplay
;
2608 vdisplay
= adjusted
.crtc_vdisplay
;
2611 if (crtc
->invert_dimensions
)
2612 swap(hdisplay
, vdisplay
);
2614 if (hdisplay
> fb
->width
||
2615 vdisplay
> fb
->height
||
2616 x
> fb
->width
- hdisplay
||
2617 y
> fb
->height
- vdisplay
) {
2618 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2619 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2620 crtc
->invert_dimensions
? " (inverted)" : "");
2626 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2629 * drm_mode_setcrtc - set CRTC configuration
2630 * @dev: drm device for the ioctl
2631 * @data: data pointer for the ioctl
2632 * @file_priv: drm file for the ioctl call
2634 * Build a new CRTC configuration based on user request.
2636 * Called by the user via ioctl.
2639 * Zero on success, negative errno on failure.
2641 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2642 struct drm_file
*file_priv
)
2644 struct drm_mode_config
*config
= &dev
->mode_config
;
2645 struct drm_mode_crtc
*crtc_req
= data
;
2646 struct drm_crtc
*crtc
;
2647 struct drm_connector
**connector_set
= NULL
, *connector
;
2648 struct drm_framebuffer
*fb
= NULL
;
2649 struct drm_display_mode
*mode
= NULL
;
2650 struct drm_mode_set set
;
2651 uint32_t __user
*set_connectors_ptr
;
2655 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2658 /* For some reason crtc x/y offsets are signed internally. */
2659 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2662 drm_modeset_lock_all(dev
);
2663 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2665 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2669 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2671 if (crtc_req
->mode_valid
) {
2672 /* If we have a mode we need a framebuffer. */
2673 /* If we pass -1, set the mode with the currently bound fb */
2674 if (crtc_req
->fb_id
== -1) {
2675 if (!crtc
->primary
->fb
) {
2676 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2680 fb
= crtc
->primary
->fb
;
2681 /* Make refcounting symmetric with the lookup path. */
2682 drm_framebuffer_reference(fb
);
2684 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2686 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2693 mode
= drm_mode_create(dev
);
2699 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2701 DRM_DEBUG_KMS("Invalid mode\n");
2705 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2707 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2714 if (crtc_req
->count_connectors
== 0 && mode
) {
2715 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2720 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2721 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2722 crtc_req
->count_connectors
);
2727 if (crtc_req
->count_connectors
> 0) {
2730 /* Avoid unbounded kernel memory allocation */
2731 if (crtc_req
->count_connectors
> config
->num_connector
) {
2736 connector_set
= kmalloc_array(crtc_req
->count_connectors
,
2737 sizeof(struct drm_connector
*),
2739 if (!connector_set
) {
2744 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2745 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2746 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2751 connector
= drm_connector_find(dev
, out_id
);
2753 DRM_DEBUG_KMS("Connector id %d unknown\n",
2758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2762 connector_set
[i
] = connector
;
2767 set
.x
= crtc_req
->x
;
2768 set
.y
= crtc_req
->y
;
2770 set
.connectors
= connector_set
;
2771 set
.num_connectors
= crtc_req
->count_connectors
;
2773 ret
= drm_mode_set_config_internal(&set
);
2777 drm_framebuffer_unreference(fb
);
2779 kfree(connector_set
);
2780 drm_mode_destroy(dev
, mode
);
2781 drm_modeset_unlock_all(dev
);
2786 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2787 * universal plane handler call
2788 * @crtc: crtc to update cursor for
2789 * @req: data pointer for the ioctl
2790 * @file_priv: drm file for the ioctl call
2792 * Legacy cursor ioctl's work directly with driver buffer handles. To
2793 * translate legacy ioctl calls into universal plane handler calls, we need to
2794 * wrap the native buffer handle in a drm_framebuffer.
2796 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2797 * buffer with a pitch of 4*width; the universal plane interface should be used
2798 * directly in cases where the hardware can support other buffer settings and
2799 * userspace wants to make use of these capabilities.
2802 * Zero on success, negative errno on failure.
2804 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2805 struct drm_mode_cursor2
*req
,
2806 struct drm_file
*file_priv
)
2808 struct drm_device
*dev
= crtc
->dev
;
2809 struct drm_framebuffer
*fb
= NULL
;
2810 struct drm_mode_fb_cmd2 fbreq
= {
2811 .width
= req
->width
,
2812 .height
= req
->height
,
2813 .pixel_format
= DRM_FORMAT_ARGB8888
,
2814 .pitches
= { req
->width
* 4 },
2815 .handles
= { req
->handle
},
2817 int32_t crtc_x
, crtc_y
;
2818 uint32_t crtc_w
= 0, crtc_h
= 0;
2819 uint32_t src_w
= 0, src_h
= 0;
2822 BUG_ON(!crtc
->cursor
);
2823 WARN_ON(crtc
->cursor
->crtc
!= crtc
&& crtc
->cursor
->crtc
!= NULL
);
2826 * Obtain fb we'll be using (either new or existing) and take an extra
2827 * reference to it if fb != null. setplane will take care of dropping
2828 * the reference if the plane update fails.
2830 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2832 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2834 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2838 drm_framebuffer_reference(fb
);
2843 fb
= crtc
->cursor
->fb
;
2845 drm_framebuffer_reference(fb
);
2848 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2852 crtc_x
= crtc
->cursor_x
;
2853 crtc_y
= crtc
->cursor_y
;
2858 crtc_h
= fb
->height
;
2859 src_w
= fb
->width
<< 16;
2860 src_h
= fb
->height
<< 16;
2864 * setplane_internal will take care of deref'ing either the old or new
2865 * framebuffer depending on success.
2867 ret
= __setplane_internal(crtc
->cursor
, crtc
, fb
,
2868 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2869 0, 0, src_w
, src_h
);
2871 /* Update successful; save new cursor position, if necessary */
2872 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2873 crtc
->cursor_x
= req
->x
;
2874 crtc
->cursor_y
= req
->y
;
2880 static int drm_mode_cursor_common(struct drm_device
*dev
,
2881 struct drm_mode_cursor2
*req
,
2882 struct drm_file
*file_priv
)
2884 struct drm_crtc
*crtc
;
2887 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2890 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2893 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2895 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2900 * If this crtc has a universal cursor plane, call that plane's update
2901 * handler rather than using legacy cursor handlers.
2903 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
2905 ret
= drm_mode_cursor_universal(crtc
, req
, file_priv
);
2909 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2910 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2914 /* Turns off the cursor if handle is 0 */
2915 if (crtc
->funcs
->cursor_set2
)
2916 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2917 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2919 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2920 req
->width
, req
->height
);
2923 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2924 if (crtc
->funcs
->cursor_move
) {
2925 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2932 drm_modeset_unlock_crtc(crtc
);
2940 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2941 * @dev: drm device for the ioctl
2942 * @data: data pointer for the ioctl
2943 * @file_priv: drm file for the ioctl call
2945 * Set the cursor configuration based on user request.
2947 * Called by the user via ioctl.
2950 * Zero on success, negative errno on failure.
2952 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2953 void *data
, struct drm_file
*file_priv
)
2955 struct drm_mode_cursor
*req
= data
;
2956 struct drm_mode_cursor2 new_req
;
2958 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2959 new_req
.hot_x
= new_req
.hot_y
= 0;
2961 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2965 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2966 * @dev: drm device for the ioctl
2967 * @data: data pointer for the ioctl
2968 * @file_priv: drm file for the ioctl call
2970 * Set the cursor configuration based on user request. This implements the 2nd
2971 * version of the cursor ioctl, which allows userspace to additionally specify
2972 * the hotspot of the pointer.
2974 * Called by the user via ioctl.
2977 * Zero on success, negative errno on failure.
2979 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2980 void *data
, struct drm_file
*file_priv
)
2982 struct drm_mode_cursor2
*req
= data
;
2984 return drm_mode_cursor_common(dev
, req
, file_priv
);
2988 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2989 * @bpp: bits per pixels
2990 * @depth: bit depth per pixel
2992 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2993 * Useful in fbdev emulation code, since that deals in those values.
2995 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
3001 fmt
= DRM_FORMAT_C8
;
3005 fmt
= DRM_FORMAT_XRGB1555
;
3007 fmt
= DRM_FORMAT_RGB565
;
3010 fmt
= DRM_FORMAT_RGB888
;
3014 fmt
= DRM_FORMAT_XRGB8888
;
3015 else if (depth
== 30)
3016 fmt
= DRM_FORMAT_XRGB2101010
;
3018 fmt
= DRM_FORMAT_ARGB8888
;
3021 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3022 fmt
= DRM_FORMAT_XRGB8888
;
3028 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
3031 * drm_mode_addfb - add an FB to the graphics configuration
3032 * @dev: drm device for the ioctl
3033 * @data: data pointer for the ioctl
3034 * @file_priv: drm file for the ioctl call
3036 * Add a new FB to the specified CRTC, given a user request. This is the
3037 * original addfb ioctl which only supported RGB formats.
3039 * Called by the user via ioctl.
3042 * Zero on success, negative errno on failure.
3044 int drm_mode_addfb(struct drm_device
*dev
,
3045 void *data
, struct drm_file
*file_priv
)
3047 struct drm_mode_fb_cmd
*or = data
;
3048 struct drm_mode_fb_cmd2 r
= {};
3051 /* convert to new format and call new ioctl */
3052 r
.fb_id
= or->fb_id
;
3053 r
.width
= or->width
;
3054 r
.height
= or->height
;
3055 r
.pitches
[0] = or->pitch
;
3056 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
3057 r
.handles
[0] = or->handle
;
3059 ret
= drm_mode_addfb2(dev
, &r
, file_priv
);
3063 or->fb_id
= r
.fb_id
;
3068 static int format_check(const struct drm_mode_fb_cmd2
*r
)
3070 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
3074 case DRM_FORMAT_RGB332
:
3075 case DRM_FORMAT_BGR233
:
3076 case DRM_FORMAT_XRGB4444
:
3077 case DRM_FORMAT_XBGR4444
:
3078 case DRM_FORMAT_RGBX4444
:
3079 case DRM_FORMAT_BGRX4444
:
3080 case DRM_FORMAT_ARGB4444
:
3081 case DRM_FORMAT_ABGR4444
:
3082 case DRM_FORMAT_RGBA4444
:
3083 case DRM_FORMAT_BGRA4444
:
3084 case DRM_FORMAT_XRGB1555
:
3085 case DRM_FORMAT_XBGR1555
:
3086 case DRM_FORMAT_RGBX5551
:
3087 case DRM_FORMAT_BGRX5551
:
3088 case DRM_FORMAT_ARGB1555
:
3089 case DRM_FORMAT_ABGR1555
:
3090 case DRM_FORMAT_RGBA5551
:
3091 case DRM_FORMAT_BGRA5551
:
3092 case DRM_FORMAT_RGB565
:
3093 case DRM_FORMAT_BGR565
:
3094 case DRM_FORMAT_RGB888
:
3095 case DRM_FORMAT_BGR888
:
3096 case DRM_FORMAT_XRGB8888
:
3097 case DRM_FORMAT_XBGR8888
:
3098 case DRM_FORMAT_RGBX8888
:
3099 case DRM_FORMAT_BGRX8888
:
3100 case DRM_FORMAT_ARGB8888
:
3101 case DRM_FORMAT_ABGR8888
:
3102 case DRM_FORMAT_RGBA8888
:
3103 case DRM_FORMAT_BGRA8888
:
3104 case DRM_FORMAT_XRGB2101010
:
3105 case DRM_FORMAT_XBGR2101010
:
3106 case DRM_FORMAT_RGBX1010102
:
3107 case DRM_FORMAT_BGRX1010102
:
3108 case DRM_FORMAT_ARGB2101010
:
3109 case DRM_FORMAT_ABGR2101010
:
3110 case DRM_FORMAT_RGBA1010102
:
3111 case DRM_FORMAT_BGRA1010102
:
3112 case DRM_FORMAT_YUYV
:
3113 case DRM_FORMAT_YVYU
:
3114 case DRM_FORMAT_UYVY
:
3115 case DRM_FORMAT_VYUY
:
3116 case DRM_FORMAT_AYUV
:
3117 case DRM_FORMAT_NV12
:
3118 case DRM_FORMAT_NV21
:
3119 case DRM_FORMAT_NV16
:
3120 case DRM_FORMAT_NV61
:
3121 case DRM_FORMAT_NV24
:
3122 case DRM_FORMAT_NV42
:
3123 case DRM_FORMAT_YUV410
:
3124 case DRM_FORMAT_YVU410
:
3125 case DRM_FORMAT_YUV411
:
3126 case DRM_FORMAT_YVU411
:
3127 case DRM_FORMAT_YUV420
:
3128 case DRM_FORMAT_YVU420
:
3129 case DRM_FORMAT_YUV422
:
3130 case DRM_FORMAT_YVU422
:
3131 case DRM_FORMAT_YUV444
:
3132 case DRM_FORMAT_YVU444
:
3135 DRM_DEBUG_KMS("invalid pixel format %s\n",
3136 drm_get_format_name(r
->pixel_format
));
3141 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3143 int ret
, hsub
, vsub
, num_planes
, i
;
3145 ret
= format_check(r
);
3147 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3148 drm_get_format_name(r
->pixel_format
));
3152 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3153 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3154 num_planes
= drm_format_num_planes(r
->pixel_format
);
3156 if (r
->width
== 0 || r
->width
% hsub
) {
3157 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->width
);
3161 if (r
->height
== 0 || r
->height
% vsub
) {
3162 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3166 for (i
= 0; i
< num_planes
; i
++) {
3167 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3168 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3169 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3171 if (!r
->handles
[i
]) {
3172 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3176 if ((uint64_t) width
* cpp
> UINT_MAX
)
3179 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3182 if (r
->pitches
[i
] < width
* cpp
) {
3183 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3191 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
3192 struct drm_mode_fb_cmd2
*r
,
3193 struct drm_file
*file_priv
)
3195 struct drm_mode_config
*config
= &dev
->mode_config
;
3196 struct drm_framebuffer
*fb
;
3199 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3200 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3201 return ERR_PTR(-EINVAL
);
3204 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3205 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3206 r
->width
, config
->min_width
, config
->max_width
);
3207 return ERR_PTR(-EINVAL
);
3209 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3210 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3211 r
->height
, config
->min_height
, config
->max_height
);
3212 return ERR_PTR(-EINVAL
);
3215 ret
= framebuffer_check(r
);
3217 return ERR_PTR(ret
);
3219 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3221 DRM_DEBUG_KMS("could not create framebuffer\n");
3225 mutex_lock(&file_priv
->fbs_lock
);
3226 r
->fb_id
= fb
->base
.id
;
3227 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3228 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3229 mutex_unlock(&file_priv
->fbs_lock
);
3235 * drm_mode_addfb2 - add an FB to the graphics configuration
3236 * @dev: drm device for the ioctl
3237 * @data: data pointer for the ioctl
3238 * @file_priv: drm file for the ioctl call
3240 * Add a new FB to the specified CRTC, given a user request with format. This is
3241 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3242 * and uses fourcc codes as pixel format specifiers.
3244 * Called by the user via ioctl.
3247 * Zero on success, negative errno on failure.
3249 int drm_mode_addfb2(struct drm_device
*dev
,
3250 void *data
, struct drm_file
*file_priv
)
3252 struct drm_framebuffer
*fb
;
3254 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3257 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3265 * drm_mode_rmfb - remove an FB from the configuration
3266 * @dev: drm device for the ioctl
3267 * @data: data pointer for the ioctl
3268 * @file_priv: drm file for the ioctl call
3270 * Remove the FB specified by the user.
3272 * Called by the user via ioctl.
3275 * Zero on success, negative errno on failure.
3277 int drm_mode_rmfb(struct drm_device
*dev
,
3278 void *data
, struct drm_file
*file_priv
)
3280 struct drm_framebuffer
*fb
= NULL
;
3281 struct drm_framebuffer
*fbl
= NULL
;
3282 uint32_t *id
= data
;
3285 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3288 mutex_lock(&file_priv
->fbs_lock
);
3289 mutex_lock(&dev
->mode_config
.fb_lock
);
3290 fb
= __drm_framebuffer_lookup(dev
, *id
);
3294 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3300 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3301 __drm_framebuffer_unregister(dev
, fb
);
3303 list_del_init(&fb
->filp_head
);
3304 mutex_unlock(&dev
->mode_config
.fb_lock
);
3305 mutex_unlock(&file_priv
->fbs_lock
);
3307 drm_framebuffer_remove(fb
);
3312 mutex_unlock(&dev
->mode_config
.fb_lock
);
3313 mutex_unlock(&file_priv
->fbs_lock
);
3319 * drm_mode_getfb - get FB info
3320 * @dev: drm device for the ioctl
3321 * @data: data pointer for the ioctl
3322 * @file_priv: drm file for the ioctl call
3324 * Lookup the FB given its ID and return info about it.
3326 * Called by the user via ioctl.
3329 * Zero on success, negative errno on failure.
3331 int drm_mode_getfb(struct drm_device
*dev
,
3332 void *data
, struct drm_file
*file_priv
)
3334 struct drm_mode_fb_cmd
*r
= data
;
3335 struct drm_framebuffer
*fb
;
3338 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3341 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3345 r
->height
= fb
->height
;
3346 r
->width
= fb
->width
;
3347 r
->depth
= fb
->depth
;
3348 r
->bpp
= fb
->bits_per_pixel
;
3349 r
->pitch
= fb
->pitches
[0];
3350 if (fb
->funcs
->create_handle
) {
3351 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3352 drm_is_control_client(file_priv
)) {
3353 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3356 /* GET_FB() is an unprivileged ioctl so we must not
3357 * return a buffer-handle to non-master processes! For
3358 * backwards-compatibility reasons, we cannot make
3359 * GET_FB() privileged, so just return an invalid handle
3360 * for non-masters. */
3368 drm_framebuffer_unreference(fb
);
3374 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3375 * @dev: drm device for the ioctl
3376 * @data: data pointer for the ioctl
3377 * @file_priv: drm file for the ioctl call
3379 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3380 * rectangle list. Generic userspace which does frontbuffer rendering must call
3381 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3382 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3384 * Modesetting drivers which always update the frontbuffer do not need to
3385 * implement the corresponding ->dirty framebuffer callback.
3387 * Called by the user via ioctl.
3390 * Zero on success, negative errno on failure.
3392 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3393 void *data
, struct drm_file
*file_priv
)
3395 struct drm_clip_rect __user
*clips_ptr
;
3396 struct drm_clip_rect
*clips
= NULL
;
3397 struct drm_mode_fb_dirty_cmd
*r
= data
;
3398 struct drm_framebuffer
*fb
;
3403 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3406 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3410 num_clips
= r
->num_clips
;
3411 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3413 if (!num_clips
!= !clips_ptr
) {
3418 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3420 /* If userspace annotates copy, clips must come in pairs */
3421 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3426 if (num_clips
&& clips_ptr
) {
3427 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3431 clips
= kcalloc(num_clips
, sizeof(*clips
), GFP_KERNEL
);
3437 ret
= copy_from_user(clips
, clips_ptr
,
3438 num_clips
* sizeof(*clips
));
3445 if (fb
->funcs
->dirty
) {
3446 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3455 drm_framebuffer_unreference(fb
);
3462 * drm_fb_release - remove and free the FBs on this file
3463 * @priv: drm file for the ioctl
3465 * Destroy all the FBs associated with @filp.
3467 * Called by the user via ioctl.
3470 * Zero on success, negative errno on failure.
3472 void drm_fb_release(struct drm_file
*priv
)
3474 struct drm_device
*dev
= priv
->minor
->dev
;
3475 struct drm_framebuffer
*fb
, *tfb
;
3478 * When the file gets released that means no one else can access the fb
3479 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3480 * avoid upsetting lockdep since the universal cursor code adds a
3481 * framebuffer while holding mutex locks.
3483 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3484 * locks is impossible here since no one else but this function can get
3487 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3489 mutex_lock(&dev
->mode_config
.fb_lock
);
3490 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3491 __drm_framebuffer_unregister(dev
, fb
);
3492 mutex_unlock(&dev
->mode_config
.fb_lock
);
3494 list_del_init(&fb
->filp_head
);
3496 /* This will also drop the fpriv->fbs reference. */
3497 drm_framebuffer_remove(fb
);
3502 * drm_property_create - create a new property type
3504 * @flags: flags specifying the property type
3505 * @name: name of the property
3506 * @num_values: number of pre-defined values
3508 * This creates a new generic drm property which can then be attached to a drm
3509 * object with drm_object_attach_property. The returned property object must be
3510 * freed with drm_property_destroy.
3512 * Note that the DRM core keeps a per-device list of properties and that, if
3513 * drm_mode_config_cleanup() is called, it will destroy all properties created
3517 * A pointer to the newly created property on success, NULL on failure.
3519 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3520 const char *name
, int num_values
)
3522 struct drm_property
*property
= NULL
;
3525 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3529 property
->dev
= dev
;
3532 property
->values
= kcalloc(num_values
, sizeof(uint64_t),
3534 if (!property
->values
)
3538 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3542 property
->flags
= flags
;
3543 property
->num_values
= num_values
;
3544 INIT_LIST_HEAD(&property
->enum_list
);
3547 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3548 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3551 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3553 WARN_ON(!drm_property_type_valid(property
));
3557 kfree(property
->values
);
3561 EXPORT_SYMBOL(drm_property_create
);
3564 * drm_property_create_enum - create a new enumeration property type
3566 * @flags: flags specifying the property type
3567 * @name: name of the property
3568 * @props: enumeration lists with property values
3569 * @num_values: number of pre-defined values
3571 * This creates a new generic drm property which can then be attached to a drm
3572 * object with drm_object_attach_property. The returned property object must be
3573 * freed with drm_property_destroy.
3575 * Userspace is only allowed to set one of the predefined values for enumeration
3579 * A pointer to the newly created property on success, NULL on failure.
3581 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3583 const struct drm_prop_enum_list
*props
,
3586 struct drm_property
*property
;
3589 flags
|= DRM_MODE_PROP_ENUM
;
3591 property
= drm_property_create(dev
, flags
, name
, num_values
);
3595 for (i
= 0; i
< num_values
; i
++) {
3596 ret
= drm_property_add_enum(property
, i
,
3600 drm_property_destroy(dev
, property
);
3607 EXPORT_SYMBOL(drm_property_create_enum
);
3610 * drm_property_create_bitmask - create a new bitmask property type
3612 * @flags: flags specifying the property type
3613 * @name: name of the property
3614 * @props: enumeration lists with property bitflags
3615 * @num_props: size of the @props array
3616 * @supported_bits: bitmask of all supported enumeration values
3618 * This creates a new bitmask drm property which can then be attached to a drm
3619 * object with drm_object_attach_property. The returned property object must be
3620 * freed with drm_property_destroy.
3622 * Compared to plain enumeration properties userspace is allowed to set any
3623 * or'ed together combination of the predefined property bitflag values
3626 * A pointer to the newly created property on success, NULL on failure.
3628 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3629 int flags
, const char *name
,
3630 const struct drm_prop_enum_list
*props
,
3632 uint64_t supported_bits
)
3634 struct drm_property
*property
;
3635 int i
, ret
, index
= 0;
3636 int num_values
= hweight64(supported_bits
);
3638 flags
|= DRM_MODE_PROP_BITMASK
;
3640 property
= drm_property_create(dev
, flags
, name
, num_values
);
3643 for (i
= 0; i
< num_props
; i
++) {
3644 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3647 if (WARN_ON(index
>= num_values
)) {
3648 drm_property_destroy(dev
, property
);
3652 ret
= drm_property_add_enum(property
, index
++,
3656 drm_property_destroy(dev
, property
);
3663 EXPORT_SYMBOL(drm_property_create_bitmask
);
3665 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3666 int flags
, const char *name
,
3667 uint64_t min
, uint64_t max
)
3669 struct drm_property
*property
;
3671 property
= drm_property_create(dev
, flags
, name
, 2);
3675 property
->values
[0] = min
;
3676 property
->values
[1] = max
;
3682 * drm_property_create_range - create a new ranged property type
3684 * @flags: flags specifying the property type
3685 * @name: name of the property
3686 * @min: minimum value of the property
3687 * @max: maximum value of the property
3689 * This creates a new generic drm property which can then be attached to a drm
3690 * object with drm_object_attach_property. The returned property object must be
3691 * freed with drm_property_destroy.
3693 * Userspace is allowed to set any integer value in the (min, max) range
3697 * A pointer to the newly created property on success, NULL on failure.
3699 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3701 uint64_t min
, uint64_t max
)
3703 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3706 EXPORT_SYMBOL(drm_property_create_range
);
3708 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3709 int flags
, const char *name
,
3710 int64_t min
, int64_t max
)
3712 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3713 name
, I642U64(min
), I642U64(max
));
3715 EXPORT_SYMBOL(drm_property_create_signed_range
);
3717 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3718 int flags
, const char *name
, uint32_t type
)
3720 struct drm_property
*property
;
3722 flags
|= DRM_MODE_PROP_OBJECT
;
3724 property
= drm_property_create(dev
, flags
, name
, 1);
3728 property
->values
[0] = type
;
3732 EXPORT_SYMBOL(drm_property_create_object
);
3735 * drm_property_add_enum - add a possible value to an enumeration property
3736 * @property: enumeration property to change
3737 * @index: index of the new enumeration
3738 * @value: value of the new enumeration
3739 * @name: symbolic name of the new enumeration
3741 * This functions adds enumerations to a property.
3743 * It's use is deprecated, drivers should use one of the more specific helpers
3744 * to directly create the property with all enumerations already attached.
3747 * Zero on success, error code on failure.
3749 int drm_property_add_enum(struct drm_property
*property
, int index
,
3750 uint64_t value
, const char *name
)
3752 struct drm_property_enum
*prop_enum
;
3754 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3755 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3759 * Bitmask enum properties have the additional constraint of values
3762 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3766 if (!list_empty(&property
->enum_list
)) {
3767 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
3768 if (prop_enum
->value
== value
) {
3769 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3770 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3776 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3780 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3781 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3782 prop_enum
->value
= value
;
3784 property
->values
[index
] = value
;
3785 list_add_tail(&prop_enum
->head
, &property
->enum_list
);
3788 EXPORT_SYMBOL(drm_property_add_enum
);
3791 * drm_property_destroy - destroy a drm property
3793 * @property: property to destry
3795 * This function frees a property including any attached resources like
3796 * enumeration values.
3798 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3800 struct drm_property_enum
*prop_enum
, *pt
;
3802 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_list
, head
) {
3803 list_del(&prop_enum
->head
);
3807 if (property
->num_values
)
3808 kfree(property
->values
);
3809 drm_mode_object_put(dev
, &property
->base
);
3810 list_del(&property
->head
);
3813 EXPORT_SYMBOL(drm_property_destroy
);
3816 * drm_object_attach_property - attach a property to a modeset object
3817 * @obj: drm modeset object
3818 * @property: property to attach
3819 * @init_val: initial value of the property
3821 * This attaches the given property to the modeset object with the given initial
3822 * value. Currently this function cannot fail since the properties are stored in
3823 * a statically sized array.
3825 void drm_object_attach_property(struct drm_mode_object
*obj
,
3826 struct drm_property
*property
,
3829 int count
= obj
->properties
->count
;
3831 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3832 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3833 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3834 "you see this message on the same object type.\n",
3839 obj
->properties
->properties
[count
] = property
;
3840 obj
->properties
->values
[count
] = init_val
;
3841 obj
->properties
->count
++;
3842 if (property
->flags
& DRM_MODE_PROP_ATOMIC
)
3843 obj
->properties
->atomic_count
++;
3845 EXPORT_SYMBOL(drm_object_attach_property
);
3848 * drm_object_property_set_value - set the value of a property
3849 * @obj: drm mode object to set property value for
3850 * @property: property to set
3851 * @val: value the property should be set to
3853 * This functions sets a given property on a given object. This function only
3854 * changes the software state of the property, it does not call into the
3855 * driver's ->set_property callback.
3858 * Zero on success, error code on failure.
3860 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3861 struct drm_property
*property
, uint64_t val
)
3865 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3866 if (obj
->properties
->properties
[i
] == property
) {
3867 obj
->properties
->values
[i
] = val
;
3874 EXPORT_SYMBOL(drm_object_property_set_value
);
3877 * drm_object_property_get_value - retrieve the value of a property
3878 * @obj: drm mode object to get property value from
3879 * @property: property to retrieve
3880 * @val: storage for the property value
3882 * This function retrieves the softare state of the given property for the given
3883 * property. Since there is no driver callback to retrieve the current property
3884 * value this might be out of sync with the hardware, depending upon the driver
3888 * Zero on success, error code on failure.
3890 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3891 struct drm_property
*property
, uint64_t *val
)
3895 /* read-only properties bypass atomic mechanism and still store
3896 * their value in obj->properties->values[].. mostly to avoid
3897 * having to deal w/ EDID and similar props in atomic paths:
3899 if (drm_core_check_feature(property
->dev
, DRIVER_ATOMIC
) &&
3900 !(property
->flags
& DRM_MODE_PROP_IMMUTABLE
))
3901 return drm_atomic_get_property(obj
, property
, val
);
3903 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3904 if (obj
->properties
->properties
[i
] == property
) {
3905 *val
= obj
->properties
->values
[i
];
3912 EXPORT_SYMBOL(drm_object_property_get_value
);
3915 * drm_mode_getproperty_ioctl - get the property metadata
3918 * @file_priv: DRM file info
3920 * This function retrieves the metadata for a given property, like the different
3921 * possible values for an enum property or the limits for a range property.
3923 * Blob properties are special
3925 * Called by the user via ioctl.
3928 * Zero on success, negative errno on failure.
3930 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3931 void *data
, struct drm_file
*file_priv
)
3933 struct drm_mode_get_property
*out_resp
= data
;
3934 struct drm_property
*property
;
3936 int value_count
= 0;
3939 struct drm_property_enum
*prop_enum
;
3940 struct drm_mode_property_enum __user
*enum_ptr
;
3941 uint64_t __user
*values_ptr
;
3943 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3946 drm_modeset_lock_all(dev
);
3947 property
= drm_property_find(dev
, out_resp
->prop_id
);
3953 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3954 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3955 list_for_each_entry(prop_enum
, &property
->enum_list
, head
)
3959 value_count
= property
->num_values
;
3961 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3962 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3963 out_resp
->flags
= property
->flags
;
3965 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3966 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3967 for (i
= 0; i
< value_count
; i
++) {
3968 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3974 out_resp
->count_values
= value_count
;
3976 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3977 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3978 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3980 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3981 list_for_each_entry(prop_enum
, &property
->enum_list
, head
) {
3983 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3988 if (copy_to_user(&enum_ptr
[copied
].name
,
3989 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3996 out_resp
->count_enum_blobs
= enum_count
;
4000 * NOTE: The idea seems to have been to use this to read all the blob
4001 * property values. But nothing ever added them to the corresponding
4002 * list, userspace always used the special-purpose get_blob ioctl to
4003 * read the value for a blob property. It also doesn't make a lot of
4004 * sense to return values here when everything else is just metadata for
4005 * the property itself.
4007 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
))
4008 out_resp
->count_enum_blobs
= 0;
4010 drm_modeset_unlock_all(dev
);
4014 static struct drm_property_blob
*
4015 drm_property_create_blob(struct drm_device
*dev
, size_t length
,
4018 struct drm_property_blob
*blob
;
4021 if (!length
|| !data
)
4024 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
4028 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
4034 blob
->length
= length
;
4036 memcpy(blob
->data
, data
, length
);
4038 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
4042 static void drm_property_destroy_blob(struct drm_device
*dev
,
4043 struct drm_property_blob
*blob
)
4045 drm_mode_object_put(dev
, &blob
->base
);
4046 list_del(&blob
->head
);
4051 * drm_mode_getblob_ioctl - get the contents of a blob property value
4054 * @file_priv: DRM file info
4056 * This function retrieves the contents of a blob property. The value stored in
4057 * an object's blob property is just a normal modeset object id.
4059 * Called by the user via ioctl.
4062 * Zero on success, negative errno on failure.
4064 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
4065 void *data
, struct drm_file
*file_priv
)
4067 struct drm_mode_get_blob
*out_resp
= data
;
4068 struct drm_property_blob
*blob
;
4070 void __user
*blob_ptr
;
4072 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4075 drm_modeset_lock_all(dev
);
4076 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
4082 if (out_resp
->length
== blob
->length
) {
4083 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
4084 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)) {
4089 out_resp
->length
= blob
->length
;
4092 drm_modeset_unlock_all(dev
);
4097 * drm_mode_connector_set_path_property - set tile property on connector
4098 * @connector: connector to set property on.
4099 * @path: path to use for property.
4101 * This creates a property to expose to userspace to specify a
4102 * connector path. This is mainly used for DisplayPort MST where
4103 * connectors have a topology and we want to allow userspace to give
4104 * them more meaningful names.
4107 * Zero on success, negative errno on failure.
4109 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4112 struct drm_device
*dev
= connector
->dev
;
4113 size_t size
= strlen(path
) + 1;
4116 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
4118 if (!connector
->path_blob_ptr
)
4121 ret
= drm_object_property_set_value(&connector
->base
,
4122 dev
->mode_config
.path_property
,
4123 connector
->path_blob_ptr
->base
.id
);
4126 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4129 * drm_mode_connector_set_tile_property - set tile property on connector
4130 * @connector: connector to set property on.
4132 * This looks up the tile information for a connector, and creates a
4133 * property for userspace to parse if it exists. The property is of
4134 * the form of 8 integers using ':' as a separator.
4137 * Zero on success, errno on failure.
4139 int drm_mode_connector_set_tile_property(struct drm_connector
*connector
)
4141 struct drm_device
*dev
= connector
->dev
;
4145 if (connector
->tile_blob_ptr
)
4146 drm_property_destroy_blob(dev
, connector
->tile_blob_ptr
);
4148 if (!connector
->has_tile
) {
4149 connector
->tile_blob_ptr
= NULL
;
4150 ret
= drm_object_property_set_value(&connector
->base
,
4151 dev
->mode_config
.tile_property
, 0);
4155 snprintf(tile
, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4156 connector
->tile_group
->id
, connector
->tile_is_single_monitor
,
4157 connector
->num_h_tile
, connector
->num_v_tile
,
4158 connector
->tile_h_loc
, connector
->tile_v_loc
,
4159 connector
->tile_h_size
, connector
->tile_v_size
);
4160 size
= strlen(tile
) + 1;
4162 connector
->tile_blob_ptr
= drm_property_create_blob(connector
->dev
,
4164 if (!connector
->tile_blob_ptr
)
4167 ret
= drm_object_property_set_value(&connector
->base
,
4168 dev
->mode_config
.tile_property
,
4169 connector
->tile_blob_ptr
->base
.id
);
4172 EXPORT_SYMBOL(drm_mode_connector_set_tile_property
);
4175 * drm_mode_connector_update_edid_property - update the edid property of a connector
4176 * @connector: drm connector
4177 * @edid: new value of the edid property
4179 * This function creates a new blob modeset object and assigns its id to the
4180 * connector's edid property.
4183 * Zero on success, negative errno on failure.
4185 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4186 const struct edid
*edid
)
4188 struct drm_device
*dev
= connector
->dev
;
4192 /* ignore requests to set edid when overridden */
4193 if (connector
->override_edid
)
4196 if (connector
->edid_blob_ptr
)
4197 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
4199 /* Delete edid, when there is none. */
4201 connector
->edid_blob_ptr
= NULL
;
4202 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
4206 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4207 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
4209 if (!connector
->edid_blob_ptr
)
4212 ret
= drm_object_property_set_value(&connector
->base
,
4213 dev
->mode_config
.edid_property
,
4214 connector
->edid_blob_ptr
->base
.id
);
4218 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4220 /* Some properties could refer to dynamic refcnt'd objects, or things that
4221 * need special locking to handle lifetime issues (ie. to ensure the prop
4222 * value doesn't become invalid part way through the property update due to
4223 * race). The value returned by reference via 'obj' should be passed back
4224 * to drm_property_change_valid_put() after the property is set (and the
4225 * object to which the property is attached has a chance to take it's own
4228 static bool drm_property_change_valid_get(struct drm_property
*property
,
4229 uint64_t value
, struct drm_mode_object
**ref
)
4233 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4238 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4239 if (value
< property
->values
[0] || value
> property
->values
[1])
4242 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4243 int64_t svalue
= U642I64(value
);
4245 if (svalue
< U642I64(property
->values
[0]) ||
4246 svalue
> U642I64(property
->values
[1]))
4249 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4250 uint64_t valid_mask
= 0;
4252 for (i
= 0; i
< property
->num_values
; i
++)
4253 valid_mask
|= (1ULL << property
->values
[i
]);
4254 return !(value
& ~valid_mask
);
4255 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4256 /* Only the driver knows */
4258 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4259 /* a zero value for an object property translates to null: */
4263 /* handle refcnt'd objects specially: */
4264 if (property
->values
[0] == DRM_MODE_OBJECT_FB
) {
4265 struct drm_framebuffer
*fb
;
4266 fb
= drm_framebuffer_lookup(property
->dev
, value
);
4274 return _object_find(property
->dev
, value
, property
->values
[0]) != NULL
;
4278 for (i
= 0; i
< property
->num_values
; i
++)
4279 if (property
->values
[i
] == value
)
4284 for (i
= 0; i
< property
->num_values
; i
++)
4285 if (property
->values
[i
] == value
)
4290 static void drm_property_change_valid_put(struct drm_property
*property
,
4291 struct drm_mode_object
*ref
)
4296 if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4297 if (property
->values
[0] == DRM_MODE_OBJECT_FB
)
4298 drm_framebuffer_unreference(obj_to_fb(ref
));
4303 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4306 * @file_priv: DRM file info
4308 * This function sets the current value for a connectors's property. It also
4309 * calls into a driver's ->set_property callback to update the hardware state
4311 * Called by the user via ioctl.
4314 * Zero on success, negative errno on failure.
4316 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4317 void *data
, struct drm_file
*file_priv
)
4319 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4320 struct drm_mode_obj_set_property obj_set_prop
= {
4321 .value
= conn_set_prop
->value
,
4322 .prop_id
= conn_set_prop
->prop_id
,
4323 .obj_id
= conn_set_prop
->connector_id
,
4324 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4327 /* It does all the locking and checking we need */
4328 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4331 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4332 struct drm_property
*property
,
4336 struct drm_connector
*connector
= obj_to_connector(obj
);
4338 /* Do DPMS ourselves */
4339 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4340 if (connector
->funcs
->dpms
)
4341 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4343 } else if (connector
->funcs
->set_property
)
4344 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4346 /* store the property value if successful */
4348 drm_object_property_set_value(&connector
->base
, property
, value
);
4352 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4353 struct drm_property
*property
,
4357 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4359 if (crtc
->funcs
->set_property
)
4360 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4362 drm_object_property_set_value(obj
, property
, value
);
4368 * drm_mode_plane_set_obj_prop - set the value of a property
4369 * @plane: drm plane object to set property value for
4370 * @property: property to set
4371 * @value: value the property should be set to
4373 * This functions sets a given property on a given plane object. This function
4374 * calls the driver's ->set_property callback and changes the software state of
4375 * the property if the callback succeeds.
4378 * Zero on success, error code on failure.
4380 int drm_mode_plane_set_obj_prop(struct drm_plane
*plane
,
4381 struct drm_property
*property
,
4385 struct drm_mode_object
*obj
= &plane
->base
;
4387 if (plane
->funcs
->set_property
)
4388 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4390 drm_object_property_set_value(obj
, property
, value
);
4394 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop
);
4397 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4400 * @file_priv: DRM file info
4402 * This function retrieves the current value for an object's property. Compared
4403 * to the connector specific ioctl this one is extended to also work on crtc and
4406 * Called by the user via ioctl.
4409 * Zero on success, negative errno on failure.
4411 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4412 struct drm_file
*file_priv
)
4414 struct drm_mode_obj_get_properties
*arg
= data
;
4415 struct drm_mode_object
*obj
;
4418 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4421 drm_modeset_lock_all(dev
);
4423 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4428 if (!obj
->properties
) {
4433 ret
= get_properties(obj
, file_priv
->atomic
,
4434 (uint32_t __user
*)(unsigned long)(arg
->props_ptr
),
4435 (uint64_t __user
*)(unsigned long)(arg
->prop_values_ptr
),
4439 drm_modeset_unlock_all(dev
);
4444 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4447 * @file_priv: DRM file info
4449 * This function sets the current value for an object's property. It also calls
4450 * into a driver's ->set_property callback to update the hardware state.
4451 * Compared to the connector specific ioctl this one is extended to also work on
4452 * crtc and plane objects.
4454 * Called by the user via ioctl.
4457 * Zero on success, negative errno on failure.
4459 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4460 struct drm_file
*file_priv
)
4462 struct drm_mode_obj_set_property
*arg
= data
;
4463 struct drm_mode_object
*arg_obj
;
4464 struct drm_mode_object
*prop_obj
;
4465 struct drm_property
*property
;
4466 int i
, ret
= -EINVAL
;
4467 struct drm_mode_object
*ref
;
4469 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4472 drm_modeset_lock_all(dev
);
4474 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4479 if (!arg_obj
->properties
)
4482 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4483 if (arg_obj
->properties
->properties
[i
]->base
.id
== arg
->prop_id
)
4486 if (i
== arg_obj
->properties
->count
)
4489 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4490 DRM_MODE_OBJECT_PROPERTY
);
4495 property
= obj_to_property(prop_obj
);
4497 if (!drm_property_change_valid_get(property
, arg
->value
, &ref
))
4500 switch (arg_obj
->type
) {
4501 case DRM_MODE_OBJECT_CONNECTOR
:
4502 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4505 case DRM_MODE_OBJECT_CRTC
:
4506 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4508 case DRM_MODE_OBJECT_PLANE
:
4509 ret
= drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj
),
4510 property
, arg
->value
);
4514 drm_property_change_valid_put(property
, ref
);
4517 drm_modeset_unlock_all(dev
);
4522 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4523 * @connector: connector to attach
4524 * @encoder: encoder to attach @connector to
4526 * This function links up a connector to an encoder. Note that the routing
4527 * restrictions between encoders and crtcs are exposed to userspace through the
4528 * possible_clones and possible_crtcs bitmasks.
4531 * Zero on success, negative errno on failure.
4533 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4534 struct drm_encoder
*encoder
)
4538 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4539 if (connector
->encoder_ids
[i
] == 0) {
4540 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4546 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4549 * drm_mode_crtc_set_gamma_size - set the gamma table size
4550 * @crtc: CRTC to set the gamma table size for
4551 * @gamma_size: size of the gamma table
4553 * Drivers which support gamma tables should set this to the supported gamma
4554 * table size when initializing the CRTC. Currently the drm core only supports a
4555 * fixed gamma table size.
4558 * Zero on success, negative errno on failure.
4560 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4563 crtc
->gamma_size
= gamma_size
;
4565 crtc
->gamma_store
= kcalloc(gamma_size
, sizeof(uint16_t) * 3,
4567 if (!crtc
->gamma_store
) {
4568 crtc
->gamma_size
= 0;
4574 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4577 * drm_mode_gamma_set_ioctl - set the gamma table
4580 * @file_priv: DRM file info
4582 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4583 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4585 * Called by the user via ioctl.
4588 * Zero on success, negative errno on failure.
4590 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4591 void *data
, struct drm_file
*file_priv
)
4593 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4594 struct drm_crtc
*crtc
;
4595 void *r_base
, *g_base
, *b_base
;
4599 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4602 drm_modeset_lock_all(dev
);
4603 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4609 if (crtc
->funcs
->gamma_set
== NULL
) {
4614 /* memcpy into gamma store */
4615 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4620 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4621 r_base
= crtc
->gamma_store
;
4622 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4627 g_base
= r_base
+ size
;
4628 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4633 b_base
= g_base
+ size
;
4634 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4639 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4642 drm_modeset_unlock_all(dev
);
4648 * drm_mode_gamma_get_ioctl - get the gamma table
4651 * @file_priv: DRM file info
4653 * Copy the current gamma table into the storage provided. This also provides
4654 * the gamma table size the driver expects, which can be used to size the
4655 * allocated storage.
4657 * Called by the user via ioctl.
4660 * Zero on success, negative errno on failure.
4662 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4663 void *data
, struct drm_file
*file_priv
)
4665 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4666 struct drm_crtc
*crtc
;
4667 void *r_base
, *g_base
, *b_base
;
4671 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4674 drm_modeset_lock_all(dev
);
4675 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4681 /* memcpy into gamma store */
4682 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4687 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4688 r_base
= crtc
->gamma_store
;
4689 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4694 g_base
= r_base
+ size
;
4695 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4700 b_base
= g_base
+ size
;
4701 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4706 drm_modeset_unlock_all(dev
);
4711 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4714 * @file_priv: DRM file info
4716 * This schedules an asynchronous update on a given CRTC, called page flip.
4717 * Optionally a drm event is generated to signal the completion of the event.
4718 * Generic drivers cannot assume that a pageflip with changed framebuffer
4719 * properties (including driver specific metadata like tiling layout) will work,
4720 * but some drivers support e.g. pixel format changes through the pageflip
4723 * Called by the user via ioctl.
4726 * Zero on success, negative errno on failure.
4728 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4729 void *data
, struct drm_file
*file_priv
)
4731 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4732 struct drm_crtc
*crtc
;
4733 struct drm_framebuffer
*fb
= NULL
;
4734 struct drm_pending_vblank_event
*e
= NULL
;
4735 unsigned long flags
;
4738 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4739 page_flip
->reserved
!= 0)
4742 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4745 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4749 drm_modeset_lock_crtc(crtc
, crtc
->primary
);
4750 if (crtc
->primary
->fb
== NULL
) {
4751 /* The framebuffer is currently unbound, presumably
4752 * due to a hotplug event, that userspace has not
4759 if (crtc
->funcs
->page_flip
== NULL
)
4762 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4768 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4772 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4773 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4778 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4780 spin_lock_irqsave(&dev
->event_lock
, flags
);
4781 if (file_priv
->event_space
< sizeof(e
->event
)) {
4782 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4785 file_priv
->event_space
-= sizeof(e
->event
);
4786 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4788 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
4790 spin_lock_irqsave(&dev
->event_lock
, flags
);
4791 file_priv
->event_space
+= sizeof(e
->event
);
4792 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4796 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4797 e
->event
.base
.length
= sizeof(e
->event
);
4798 e
->event
.user_data
= page_flip
->user_data
;
4799 e
->base
.event
= &e
->event
.base
;
4800 e
->base
.file_priv
= file_priv
;
4802 (void (*) (struct drm_pending_event
*)) kfree
;
4805 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
4806 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4808 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4809 spin_lock_irqsave(&dev
->event_lock
, flags
);
4810 file_priv
->event_space
+= sizeof(e
->event
);
4811 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4814 /* Keep the old fb, don't unref it. */
4815 crtc
->primary
->old_fb
= NULL
;
4818 * Warn if the driver hasn't properly updated the crtc->fb
4819 * field to reflect that the new framebuffer is now used.
4820 * Failing to do so will screw with the reference counting
4823 WARN_ON(crtc
->primary
->fb
!= fb
);
4824 /* Unref only the old framebuffer. */
4830 drm_framebuffer_unreference(fb
);
4831 if (crtc
->primary
->old_fb
)
4832 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
4833 crtc
->primary
->old_fb
= NULL
;
4834 drm_modeset_unlock_crtc(crtc
);
4840 * drm_mode_config_reset - call ->reset callbacks
4843 * This functions calls all the crtc's, encoder's and connector's ->reset
4844 * callback. Drivers can use this in e.g. their driver load or resume code to
4845 * reset hardware and software state.
4847 void drm_mode_config_reset(struct drm_device
*dev
)
4849 struct drm_crtc
*crtc
;
4850 struct drm_plane
*plane
;
4851 struct drm_encoder
*encoder
;
4852 struct drm_connector
*connector
;
4854 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
)
4855 if (plane
->funcs
->reset
)
4856 plane
->funcs
->reset(plane
);
4858 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4859 if (crtc
->funcs
->reset
)
4860 crtc
->funcs
->reset(crtc
);
4862 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4863 if (encoder
->funcs
->reset
)
4864 encoder
->funcs
->reset(encoder
);
4866 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4867 connector
->status
= connector_status_unknown
;
4869 if (connector
->funcs
->reset
)
4870 connector
->funcs
->reset(connector
);
4873 EXPORT_SYMBOL(drm_mode_config_reset
);
4876 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4879 * @file_priv: DRM file info
4881 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4882 * TTM or something else entirely) and returns the resulting buffer handle. This
4883 * handle can then be wrapped up into a framebuffer modeset object.
4885 * Note that userspace is not allowed to use such objects for render
4886 * acceleration - drivers must create their own private ioctls for such a use
4889 * Called by the user via ioctl.
4892 * Zero on success, negative errno on failure.
4894 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4895 void *data
, struct drm_file
*file_priv
)
4897 struct drm_mode_create_dumb
*args
= data
;
4898 u32 cpp
, stride
, size
;
4900 if (!dev
->driver
->dumb_create
)
4902 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4905 /* overflow checks for 32bit size calculations */
4906 /* NOTE: DIV_ROUND_UP() can overflow */
4907 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4908 if (!cpp
|| cpp
> 0xffffffffU
/ args
->width
)
4910 stride
= cpp
* args
->width
;
4911 if (args
->height
> 0xffffffffU
/ stride
)
4914 /* test for wrap-around */
4915 size
= args
->height
* stride
;
4916 if (PAGE_ALIGN(size
) == 0)
4920 * handle, pitch and size are output parameters. Zero them out to
4921 * prevent drivers from accidentally using uninitialized data. Since
4922 * not all existing userspace is clearing these fields properly we
4923 * cannot reject IOCTL with garbage in them.
4929 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4933 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4936 * @file_priv: DRM file info
4938 * Allocate an offset in the drm device node's address space to be able to
4939 * memory map a dumb buffer.
4941 * Called by the user via ioctl.
4944 * Zero on success, negative errno on failure.
4946 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4947 void *data
, struct drm_file
*file_priv
)
4949 struct drm_mode_map_dumb
*args
= data
;
4951 /* call driver ioctl to get mmap offset */
4952 if (!dev
->driver
->dumb_map_offset
)
4955 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4959 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4962 * @file_priv: DRM file info
4964 * This destroys the userspace handle for the given dumb backing storage buffer.
4965 * Since buffer objects must be reference counted in the kernel a buffer object
4966 * won't be immediately freed if a framebuffer modeset object still uses it.
4968 * Called by the user via ioctl.
4971 * Zero on success, negative errno on failure.
4973 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4974 void *data
, struct drm_file
*file_priv
)
4976 struct drm_mode_destroy_dumb
*args
= data
;
4978 if (!dev
->driver
->dumb_destroy
)
4981 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4985 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4986 * @format: pixel format (DRM_FORMAT_*)
4987 * @depth: storage for the depth value
4988 * @bpp: storage for the bpp value
4990 * This only supports RGB formats here for compat with code that doesn't use
4991 * pixel formats directly yet.
4993 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4998 case DRM_FORMAT_RGB332
:
4999 case DRM_FORMAT_BGR233
:
5003 case DRM_FORMAT_XRGB1555
:
5004 case DRM_FORMAT_XBGR1555
:
5005 case DRM_FORMAT_RGBX5551
:
5006 case DRM_FORMAT_BGRX5551
:
5007 case DRM_FORMAT_ARGB1555
:
5008 case DRM_FORMAT_ABGR1555
:
5009 case DRM_FORMAT_RGBA5551
:
5010 case DRM_FORMAT_BGRA5551
:
5014 case DRM_FORMAT_RGB565
:
5015 case DRM_FORMAT_BGR565
:
5019 case DRM_FORMAT_RGB888
:
5020 case DRM_FORMAT_BGR888
:
5024 case DRM_FORMAT_XRGB8888
:
5025 case DRM_FORMAT_XBGR8888
:
5026 case DRM_FORMAT_RGBX8888
:
5027 case DRM_FORMAT_BGRX8888
:
5031 case DRM_FORMAT_XRGB2101010
:
5032 case DRM_FORMAT_XBGR2101010
:
5033 case DRM_FORMAT_RGBX1010102
:
5034 case DRM_FORMAT_BGRX1010102
:
5035 case DRM_FORMAT_ARGB2101010
:
5036 case DRM_FORMAT_ABGR2101010
:
5037 case DRM_FORMAT_RGBA1010102
:
5038 case DRM_FORMAT_BGRA1010102
:
5042 case DRM_FORMAT_ARGB8888
:
5043 case DRM_FORMAT_ABGR8888
:
5044 case DRM_FORMAT_RGBA8888
:
5045 case DRM_FORMAT_BGRA8888
:
5050 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5051 drm_get_format_name(format
));
5057 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
5060 * drm_format_num_planes - get the number of planes for format
5061 * @format: pixel format (DRM_FORMAT_*)
5064 * The number of planes used by the specified pixel format.
5066 int drm_format_num_planes(uint32_t format
)
5069 case DRM_FORMAT_YUV410
:
5070 case DRM_FORMAT_YVU410
:
5071 case DRM_FORMAT_YUV411
:
5072 case DRM_FORMAT_YVU411
:
5073 case DRM_FORMAT_YUV420
:
5074 case DRM_FORMAT_YVU420
:
5075 case DRM_FORMAT_YUV422
:
5076 case DRM_FORMAT_YVU422
:
5077 case DRM_FORMAT_YUV444
:
5078 case DRM_FORMAT_YVU444
:
5080 case DRM_FORMAT_NV12
:
5081 case DRM_FORMAT_NV21
:
5082 case DRM_FORMAT_NV16
:
5083 case DRM_FORMAT_NV61
:
5084 case DRM_FORMAT_NV24
:
5085 case DRM_FORMAT_NV42
:
5091 EXPORT_SYMBOL(drm_format_num_planes
);
5094 * drm_format_plane_cpp - determine the bytes per pixel value
5095 * @format: pixel format (DRM_FORMAT_*)
5096 * @plane: plane index
5099 * The bytes per pixel value for the specified plane.
5101 int drm_format_plane_cpp(uint32_t format
, int plane
)
5106 if (plane
>= drm_format_num_planes(format
))
5110 case DRM_FORMAT_YUYV
:
5111 case DRM_FORMAT_YVYU
:
5112 case DRM_FORMAT_UYVY
:
5113 case DRM_FORMAT_VYUY
:
5115 case DRM_FORMAT_NV12
:
5116 case DRM_FORMAT_NV21
:
5117 case DRM_FORMAT_NV16
:
5118 case DRM_FORMAT_NV61
:
5119 case DRM_FORMAT_NV24
:
5120 case DRM_FORMAT_NV42
:
5121 return plane
? 2 : 1;
5122 case DRM_FORMAT_YUV410
:
5123 case DRM_FORMAT_YVU410
:
5124 case DRM_FORMAT_YUV411
:
5125 case DRM_FORMAT_YVU411
:
5126 case DRM_FORMAT_YUV420
:
5127 case DRM_FORMAT_YVU420
:
5128 case DRM_FORMAT_YUV422
:
5129 case DRM_FORMAT_YVU422
:
5130 case DRM_FORMAT_YUV444
:
5131 case DRM_FORMAT_YVU444
:
5134 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
5138 EXPORT_SYMBOL(drm_format_plane_cpp
);
5141 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5142 * @format: pixel format (DRM_FORMAT_*)
5145 * The horizontal chroma subsampling factor for the
5146 * specified pixel format.
5148 int drm_format_horz_chroma_subsampling(uint32_t format
)
5151 case DRM_FORMAT_YUV411
:
5152 case DRM_FORMAT_YVU411
:
5153 case DRM_FORMAT_YUV410
:
5154 case DRM_FORMAT_YVU410
:
5156 case DRM_FORMAT_YUYV
:
5157 case DRM_FORMAT_YVYU
:
5158 case DRM_FORMAT_UYVY
:
5159 case DRM_FORMAT_VYUY
:
5160 case DRM_FORMAT_NV12
:
5161 case DRM_FORMAT_NV21
:
5162 case DRM_FORMAT_NV16
:
5163 case DRM_FORMAT_NV61
:
5164 case DRM_FORMAT_YUV422
:
5165 case DRM_FORMAT_YVU422
:
5166 case DRM_FORMAT_YUV420
:
5167 case DRM_FORMAT_YVU420
:
5173 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
5176 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5177 * @format: pixel format (DRM_FORMAT_*)
5180 * The vertical chroma subsampling factor for the
5181 * specified pixel format.
5183 int drm_format_vert_chroma_subsampling(uint32_t format
)
5186 case DRM_FORMAT_YUV410
:
5187 case DRM_FORMAT_YVU410
:
5189 case DRM_FORMAT_YUV420
:
5190 case DRM_FORMAT_YVU420
:
5191 case DRM_FORMAT_NV12
:
5192 case DRM_FORMAT_NV21
:
5198 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
5201 * drm_rotation_simplify() - Try to simplify the rotation
5202 * @rotation: Rotation to be simplified
5203 * @supported_rotations: Supported rotations
5205 * Attempt to simplify the rotation to a form that is supported.
5206 * Eg. if the hardware supports everything except DRM_REFLECT_X
5207 * one could call this function like this:
5209 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5210 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5211 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5213 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5214 * transforms the hardware supports, this function may not
5215 * be able to produce a supported transform, so the caller should
5216 * check the result afterwards.
5218 unsigned int drm_rotation_simplify(unsigned int rotation
,
5219 unsigned int supported_rotations
)
5221 if (rotation
& ~supported_rotations
) {
5222 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5223 rotation
= (rotation
& ~0xf) | BIT((ffs(rotation
& 0xf) + 1) % 4);
5228 EXPORT_SYMBOL(drm_rotation_simplify
);
5231 * drm_mode_config_init - initialize DRM mode_configuration structure
5234 * Initialize @dev's mode_config structure, used for tracking the graphics
5235 * configuration of @dev.
5237 * Since this initializes the modeset locks, no locking is possible. Which is no
5238 * problem, since this should happen single threaded at init time. It is the
5239 * driver's problem to ensure this guarantee.
5242 void drm_mode_config_init(struct drm_device
*dev
)
5244 mutex_init(&dev
->mode_config
.mutex
);
5245 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5246 mutex_init(&dev
->mode_config
.idr_mutex
);
5247 mutex_init(&dev
->mode_config
.fb_lock
);
5248 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5249 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5250 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5251 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
5252 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5253 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5254 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5255 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5256 idr_init(&dev
->mode_config
.crtc_idr
);
5257 idr_init(&dev
->mode_config
.tile_idr
);
5259 drm_modeset_lock_all(dev
);
5260 drm_mode_create_standard_connector_properties(dev
);
5261 drm_mode_create_standard_plane_properties(dev
);
5262 drm_modeset_unlock_all(dev
);
5264 /* Just to be sure */
5265 dev
->mode_config
.num_fb
= 0;
5266 dev
->mode_config
.num_connector
= 0;
5267 dev
->mode_config
.num_crtc
= 0;
5268 dev
->mode_config
.num_encoder
= 0;
5269 dev
->mode_config
.num_overlay_plane
= 0;
5270 dev
->mode_config
.num_total_plane
= 0;
5272 EXPORT_SYMBOL(drm_mode_config_init
);
5275 * drm_mode_config_cleanup - free up DRM mode_config info
5278 * Free up all the connectors and CRTCs associated with this DRM device, then
5279 * free up the framebuffers and associated buffer objects.
5281 * Note that since this /should/ happen single-threaded at driver/device
5282 * teardown time, no locking is required. It's the driver's job to ensure that
5283 * this guarantee actually holds true.
5285 * FIXME: cleanup any dangling user buffer objects too
5287 void drm_mode_config_cleanup(struct drm_device
*dev
)
5289 struct drm_connector
*connector
, *ot
;
5290 struct drm_crtc
*crtc
, *ct
;
5291 struct drm_encoder
*encoder
, *enct
;
5292 struct drm_bridge
*bridge
, *brt
;
5293 struct drm_framebuffer
*fb
, *fbt
;
5294 struct drm_property
*property
, *pt
;
5295 struct drm_property_blob
*blob
, *bt
;
5296 struct drm_plane
*plane
, *plt
;
5298 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5300 encoder
->funcs
->destroy(encoder
);
5303 list_for_each_entry_safe(bridge
, brt
,
5304 &dev
->mode_config
.bridge_list
, head
) {
5305 bridge
->funcs
->destroy(bridge
);
5308 list_for_each_entry_safe(connector
, ot
,
5309 &dev
->mode_config
.connector_list
, head
) {
5310 connector
->funcs
->destroy(connector
);
5313 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5315 drm_property_destroy(dev
, property
);
5318 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5320 drm_property_destroy_blob(dev
, blob
);
5324 * Single-threaded teardown context, so it's not required to grab the
5325 * fb_lock to protect against concurrent fb_list access. Contrary, it
5326 * would actually deadlock with the drm_framebuffer_cleanup function.
5328 * Also, if there are any framebuffers left, that's a driver leak now,
5329 * so politely WARN about this.
5331 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5332 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5333 drm_framebuffer_remove(fb
);
5336 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5338 plane
->funcs
->destroy(plane
);
5341 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5342 crtc
->funcs
->destroy(crtc
);
5345 idr_destroy(&dev
->mode_config
.tile_idr
);
5346 idr_destroy(&dev
->mode_config
.crtc_idr
);
5347 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5349 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5351 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5352 unsigned int supported_rotations
)
5354 static const struct drm_prop_enum_list props
[] = {
5355 { DRM_ROTATE_0
, "rotate-0" },
5356 { DRM_ROTATE_90
, "rotate-90" },
5357 { DRM_ROTATE_180
, "rotate-180" },
5358 { DRM_ROTATE_270
, "rotate-270" },
5359 { DRM_REFLECT_X
, "reflect-x" },
5360 { DRM_REFLECT_Y
, "reflect-y" },
5363 return drm_property_create_bitmask(dev
, 0, "rotation",
5364 props
, ARRAY_SIZE(props
),
5365 supported_rotations
);
5367 EXPORT_SYMBOL(drm_mode_create_rotation_property
);
5372 * Tile groups are used to represent tiled monitors with a unique
5373 * integer identifier. Tiled monitors using DisplayID v1.3 have
5374 * a unique 8-byte handle, we store this in a tile group, so we
5375 * have a common identifier for all tiles in a monitor group.
5377 static void drm_tile_group_free(struct kref
*kref
)
5379 struct drm_tile_group
*tg
= container_of(kref
, struct drm_tile_group
, refcount
);
5380 struct drm_device
*dev
= tg
->dev
;
5381 mutex_lock(&dev
->mode_config
.idr_mutex
);
5382 idr_remove(&dev
->mode_config
.tile_idr
, tg
->id
);
5383 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5388 * drm_mode_put_tile_group - drop a reference to a tile group.
5390 * @tg: tile group to drop reference to.
5392 * drop reference to tile group and free if 0.
5394 void drm_mode_put_tile_group(struct drm_device
*dev
,
5395 struct drm_tile_group
*tg
)
5397 kref_put(&tg
->refcount
, drm_tile_group_free
);
5401 * drm_mode_get_tile_group - get a reference to an existing tile group
5403 * @topology: 8-bytes unique per monitor.
5405 * Use the unique bytes to get a reference to an existing tile group.
5408 * tile group or NULL if not found.
5410 struct drm_tile_group
*drm_mode_get_tile_group(struct drm_device
*dev
,
5413 struct drm_tile_group
*tg
;
5415 mutex_lock(&dev
->mode_config
.idr_mutex
);
5416 idr_for_each_entry(&dev
->mode_config
.tile_idr
, tg
, id
) {
5417 if (!memcmp(tg
->group_data
, topology
, 8)) {
5418 if (!kref_get_unless_zero(&tg
->refcount
))
5420 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5424 mutex_unlock(&dev
->mode_config
.idr_mutex
);
5429 * drm_mode_create_tile_group - create a tile group from a displayid description
5431 * @topology: 8-bytes unique per monitor.
5433 * Create a tile group for the unique monitor, and get a unique
5434 * identifier for the tile group.
5437 * new tile group or error.
5439 struct drm_tile_group
*drm_mode_create_tile_group(struct drm_device
*dev
,
5442 struct drm_tile_group
*tg
;
5445 tg
= kzalloc(sizeof(*tg
), GFP_KERNEL
);
5447 return ERR_PTR(-ENOMEM
);
5449 kref_init(&tg
->refcount
);
5450 memcpy(tg
->group_data
, topology
, 8);
5453 mutex_lock(&dev
->mode_config
.idr_mutex
);
5454 ret
= idr_alloc(&dev
->mode_config
.tile_idr
, tg
, 1, 0, GFP_KERNEL
);
5462 mutex_unlock(&dev
->mode_config
.idr_mutex
);