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>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
45 struct drm_mode_fb_cmd2
*r
,
46 struct drm_file
*file_priv
);
48 /* Avoid boilerplate. I'm tired of typing. */
49 #define DRM_ENUM_NAME_FN(fnname, list) \
50 const char *fnname(int val) \
53 for (i = 0; i < ARRAY_SIZE(list); i++) { \
54 if (list[i].type == val) \
55 return list[i].name; \
63 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
64 { { DRM_MODE_DPMS_ON
, "On" },
65 { DRM_MODE_DPMS_STANDBY
, "Standby" },
66 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
67 { DRM_MODE_DPMS_OFF
, "Off" }
70 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
72 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] =
74 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
75 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
76 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
82 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
[] =
101 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
110 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
115 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
116 drm_dvi_i_subconnector_enum_list
)
118 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
120 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
124 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
127 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
129 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
131 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
132 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
134 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
135 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
138 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
139 drm_tv_subconnector_enum_list
)
141 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
142 { DRM_MODE_DIRTY_OFF
, "Off" },
143 { DRM_MODE_DIRTY_ON
, "On" },
144 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
147 struct drm_conn_prop_enum_list
{
154 * Connector and encoder types.
156 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
157 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
158 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
159 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
160 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
161 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
162 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
163 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
164 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
165 { DRM_MODE_CONNECTOR_Component
, "Component" },
166 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
167 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
168 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
169 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
170 { DRM_MODE_CONNECTOR_TV
, "TV" },
171 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
172 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
173 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
176 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
177 { { DRM_MODE_ENCODER_NONE
, "None" },
178 { DRM_MODE_ENCODER_DAC
, "DAC" },
179 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
180 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
181 { DRM_MODE_ENCODER_TVDAC
, "TV" },
182 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
183 { DRM_MODE_ENCODER_DSI
, "DSI" },
184 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
187 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
189 { SubPixelUnknown
, "Unknown" },
190 { SubPixelHorizontalRGB
, "Horizontal RGB" },
191 { SubPixelHorizontalBGR
, "Horizontal BGR" },
192 { SubPixelVerticalRGB
, "Vertical RGB" },
193 { SubPixelVerticalBGR
, "Vertical BGR" },
194 { SubPixelNone
, "None" },
197 void drm_connector_ida_init(void)
201 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
202 ida_init(&drm_connector_enum_list
[i
].ida
);
205 void drm_connector_ida_destroy(void)
209 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
210 ida_destroy(&drm_connector_enum_list
[i
].ida
);
214 * drm_get_connector_status_name - return a string for connector status
215 * @status: connector status to compute name of
217 * In contrast to the other drm_get_*_name functions this one here returns a
218 * const pointer and hence is threadsafe.
220 const char *drm_get_connector_status_name(enum drm_connector_status status
)
222 if (status
== connector_status_connected
)
224 else if (status
== connector_status_disconnected
)
225 return "disconnected";
229 EXPORT_SYMBOL(drm_get_connector_status_name
);
232 * drm_get_subpixel_order_name - return a string for a given subpixel enum
233 * @order: enum of subpixel_order
235 * Note you could abuse this and return something out of bounds, but that
236 * would be a caller error. No unscrubbed user data should make it here.
238 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
240 return drm_subpixel_enum_list
[order
].name
;
242 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
244 static char printable_char(int c
)
246 return isascii(c
) && isprint(c
) ? c
: '?';
250 * drm_get_format_name - return a string for drm fourcc format
251 * @format: format to compute name of
253 * Note that the buffer used by this function is globally shared and owned by
254 * the function itself.
256 * FIXME: This isn't really multithreading safe.
258 const char *drm_get_format_name(uint32_t format
)
262 snprintf(buf
, sizeof(buf
),
263 "%c%c%c%c %s-endian (0x%08x)",
264 printable_char(format
& 0xff),
265 printable_char((format
>> 8) & 0xff),
266 printable_char((format
>> 16) & 0xff),
267 printable_char((format
>> 24) & 0x7f),
268 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
273 EXPORT_SYMBOL(drm_get_format_name
);
276 * Internal function to assign a slot in the object idr and optionally
277 * register the object into the idr.
279 static int drm_mode_object_get_reg(struct drm_device
*dev
,
280 struct drm_mode_object
*obj
,
286 mutex_lock(&dev
->mode_config
.idr_mutex
);
287 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, register_obj
? obj
: NULL
, 1, 0, GFP_KERNEL
);
290 * Set up the object linking under the protection of the idr
291 * lock so that other users can't see inconsistent state.
294 obj
->type
= obj_type
;
296 mutex_unlock(&dev
->mode_config
.idr_mutex
);
298 return ret
< 0 ? ret
: 0;
302 * drm_mode_object_get - allocate a new modeset identifier
304 * @obj: object pointer, used to generate unique ID
305 * @obj_type: object type
307 * Create a unique identifier based on @ptr in @dev's identifier space. Used
308 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
309 * modeset identifiers are _not_ reference counted. Hence don't use this for
310 * reference counted modeset objects like framebuffers.
313 * New unique (relative to other objects in @dev) integer identifier for the
316 int drm_mode_object_get(struct drm_device
*dev
,
317 struct drm_mode_object
*obj
, uint32_t obj_type
)
319 return drm_mode_object_get_reg(dev
, obj
, obj_type
, true);
322 static void drm_mode_object_register(struct drm_device
*dev
,
323 struct drm_mode_object
*obj
)
325 mutex_lock(&dev
->mode_config
.idr_mutex
);
326 idr_replace(&dev
->mode_config
.crtc_idr
, obj
, obj
->id
);
327 mutex_unlock(&dev
->mode_config
.idr_mutex
);
331 * drm_mode_object_put - free a modeset identifer
333 * @object: object to free
335 * Free @id from @dev's unique identifier pool. Note that despite the _get
336 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
337 * for reference counted modeset objects like framebuffers.
339 void drm_mode_object_put(struct drm_device
*dev
,
340 struct drm_mode_object
*object
)
342 mutex_lock(&dev
->mode_config
.idr_mutex
);
343 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
344 mutex_unlock(&dev
->mode_config
.idr_mutex
);
347 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
348 uint32_t id
, uint32_t type
)
350 struct drm_mode_object
*obj
= NULL
;
352 mutex_lock(&dev
->mode_config
.idr_mutex
);
353 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
354 if (obj
&& type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
)
356 if (obj
&& obj
->id
!= id
)
358 /* don't leak out unref'd fb's */
359 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
361 mutex_unlock(&dev
->mode_config
.idr_mutex
);
367 * drm_mode_object_find - look up a drm object with static lifetime
369 * @id: id of the mode object
370 * @type: type of the mode object
372 * Note that framebuffers cannot be looked up with this functions - since those
373 * are reference counted, they need special treatment. Even with
374 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
375 * rather than WARN_ON()).
377 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
378 uint32_t id
, uint32_t type
)
380 struct drm_mode_object
*obj
= NULL
;
382 /* Framebuffers are reference counted and need their own lookup
384 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
385 obj
= _object_find(dev
, id
, type
);
388 EXPORT_SYMBOL(drm_mode_object_find
);
391 * drm_framebuffer_init - initialize a framebuffer
393 * @fb: framebuffer to be initialized
394 * @funcs: ... with these functions
396 * Allocates an ID for the framebuffer's parent mode object, sets its mode
397 * functions & device file and adds it to the master fd list.
400 * This functions publishes the fb and makes it available for concurrent access
401 * by other users. Which means by this point the fb _must_ be fully set up -
402 * since all the fb attributes are invariant over its lifetime, no further
403 * locking but only correct reference counting is required.
406 * Zero on success, error code on failure.
408 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
409 const struct drm_framebuffer_funcs
*funcs
)
413 mutex_lock(&dev
->mode_config
.fb_lock
);
414 kref_init(&fb
->refcount
);
415 INIT_LIST_HEAD(&fb
->filp_head
);
419 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
423 dev
->mode_config
.num_fb
++;
424 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
426 mutex_unlock(&dev
->mode_config
.fb_lock
);
430 EXPORT_SYMBOL(drm_framebuffer_init
);
432 /* dev->mode_config.fb_lock must be held! */
433 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
434 struct drm_framebuffer
*fb
)
436 mutex_lock(&dev
->mode_config
.idr_mutex
);
437 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
438 mutex_unlock(&dev
->mode_config
.idr_mutex
);
443 static void drm_framebuffer_free(struct kref
*kref
)
445 struct drm_framebuffer
*fb
=
446 container_of(kref
, struct drm_framebuffer
, refcount
);
447 struct drm_device
*dev
= fb
->dev
;
450 * The lookup idr holds a weak reference, which has not necessarily been
451 * removed at this point. Check for that.
453 mutex_lock(&dev
->mode_config
.fb_lock
);
455 /* Mark fb as reaped and drop idr ref. */
456 __drm_framebuffer_unregister(dev
, fb
);
458 mutex_unlock(&dev
->mode_config
.fb_lock
);
460 fb
->funcs
->destroy(fb
);
463 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
466 struct drm_mode_object
*obj
= NULL
;
467 struct drm_framebuffer
*fb
;
469 mutex_lock(&dev
->mode_config
.idr_mutex
);
470 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
471 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
475 mutex_unlock(&dev
->mode_config
.idr_mutex
);
481 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
483 * @id: id of the fb object
485 * If successful, this grabs an additional reference to the framebuffer -
486 * callers need to make sure to eventually unreference the returned framebuffer
487 * again, using @drm_framebuffer_unreference.
489 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
492 struct drm_framebuffer
*fb
;
494 mutex_lock(&dev
->mode_config
.fb_lock
);
495 fb
= __drm_framebuffer_lookup(dev
, id
);
497 if (!kref_get_unless_zero(&fb
->refcount
))
500 mutex_unlock(&dev
->mode_config
.fb_lock
);
504 EXPORT_SYMBOL(drm_framebuffer_lookup
);
507 * drm_framebuffer_unreference - unref a framebuffer
508 * @fb: framebuffer to unref
510 * This functions decrements the fb's refcount and frees it if it drops to zero.
512 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
514 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
515 kref_put(&fb
->refcount
, drm_framebuffer_free
);
517 EXPORT_SYMBOL(drm_framebuffer_unreference
);
520 * drm_framebuffer_reference - incr the fb refcnt
523 * This functions increments the fb's refcount.
525 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
527 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
528 kref_get(&fb
->refcount
);
530 EXPORT_SYMBOL(drm_framebuffer_reference
);
532 static void drm_framebuffer_free_bug(struct kref
*kref
)
537 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
539 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
540 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
544 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
545 * @fb: fb to unregister
547 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
548 * those used for fbdev. Note that the caller must hold a reference of it's own,
549 * i.e. the object may not be destroyed through this call (since it'll lead to a
550 * locking inversion).
552 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
554 struct drm_device
*dev
= fb
->dev
;
556 mutex_lock(&dev
->mode_config
.fb_lock
);
557 /* Mark fb as reaped and drop idr ref. */
558 __drm_framebuffer_unregister(dev
, fb
);
559 mutex_unlock(&dev
->mode_config
.fb_lock
);
561 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
564 * drm_framebuffer_cleanup - remove a framebuffer object
565 * @fb: framebuffer to remove
567 * Cleanup framebuffer. This function is intended to be used from the drivers
568 * ->destroy callback. It can also be used to clean up driver private
569 * framebuffers embedded into a larger structure.
571 * Note that this function does not remove the fb from active usuage - if it is
572 * still used anywhere, hilarity can ensue since userspace could call getfb on
573 * the id and get back -EINVAL. Obviously no concern at driver unload time.
575 * Also, the framebuffer will not be removed from the lookup idr - for
576 * user-created framebuffers this will happen in in the rmfb ioctl. For
577 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
578 * drm_framebuffer_unregister_private.
580 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
582 struct drm_device
*dev
= fb
->dev
;
584 mutex_lock(&dev
->mode_config
.fb_lock
);
586 dev
->mode_config
.num_fb
--;
587 mutex_unlock(&dev
->mode_config
.fb_lock
);
589 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
592 * drm_framebuffer_remove - remove and unreference a framebuffer object
593 * @fb: framebuffer to remove
595 * Scans all the CRTCs and planes in @dev's mode_config. If they're
596 * using @fb, removes it, setting it to NULL. Then drops the reference to the
597 * passed-in framebuffer. Might take the modeset locks.
599 * Note that this function optimizes the cleanup away if the caller holds the
600 * last reference to the framebuffer. It is also guaranteed to not take the
601 * modeset locks in this case.
603 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
605 struct drm_device
*dev
= fb
->dev
;
606 struct drm_crtc
*crtc
;
607 struct drm_plane
*plane
;
608 struct drm_mode_set set
;
611 WARN_ON(!list_empty(&fb
->filp_head
));
614 * drm ABI mandates that we remove any deleted framebuffers from active
615 * useage. But since most sane clients only remove framebuffers they no
616 * longer need, try to optimize this away.
618 * Since we're holding a reference ourselves, observing a refcount of 1
619 * means that we're the last holder and can skip it. Also, the refcount
620 * can never increase from 1 again, so we don't need any barriers or
623 * Note that userspace could try to race with use and instate a new
624 * usage _after_ we've cleared all current ones. End result will be an
625 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
628 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
629 drm_modeset_lock_all(dev
);
630 /* remove from any CRTC */
631 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
632 if (crtc
->primary
->fb
== fb
) {
633 /* should turn off the crtc */
634 memset(&set
, 0, sizeof(struct drm_mode_set
));
637 ret
= drm_mode_set_config_internal(&set
);
639 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
643 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
645 drm_plane_force_disable(plane
);
647 drm_modeset_unlock_all(dev
);
650 drm_framebuffer_unreference(fb
);
652 EXPORT_SYMBOL(drm_framebuffer_remove
);
654 DEFINE_WW_CLASS(crtc_ww_class
);
657 * drm_crtc_init_with_planes - Initialise a new CRTC object with
658 * specified primary and cursor planes.
660 * @crtc: CRTC object to init
661 * @primary: Primary plane for CRTC
662 * @cursor: Cursor plane for CRTC
663 * @funcs: callbacks for the new CRTC
665 * Inits a new object created as base part of a driver crtc object.
668 * Zero on success, error code on failure.
670 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
671 struct drm_plane
*primary
,
672 struct drm_plane
*cursor
,
673 const struct drm_crtc_funcs
*funcs
)
675 struct drm_mode_config
*config
= &dev
->mode_config
;
680 crtc
->invert_dimensions
= false;
682 drm_modeset_lock_all(dev
);
683 drm_modeset_lock_init(&crtc
->mutex
);
684 /* dropped by _unlock_all(): */
685 drm_modeset_lock(&crtc
->mutex
, config
->acquire_ctx
);
687 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
691 crtc
->base
.properties
= &crtc
->properties
;
693 list_add_tail(&crtc
->head
, &config
->crtc_list
);
696 crtc
->primary
= primary
;
697 crtc
->cursor
= cursor
;
699 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
701 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
704 drm_modeset_unlock_all(dev
);
708 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
711 * drm_crtc_cleanup - Clean up the core crtc usage
712 * @crtc: CRTC to cleanup
714 * This function cleans up @crtc and removes it from the DRM mode setting
715 * core. Note that the function does *not* free the crtc structure itself,
716 * this is the responsibility of the caller.
718 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
720 struct drm_device
*dev
= crtc
->dev
;
722 kfree(crtc
->gamma_store
);
723 crtc
->gamma_store
= NULL
;
725 drm_modeset_lock_fini(&crtc
->mutex
);
727 drm_mode_object_put(dev
, &crtc
->base
);
728 list_del(&crtc
->head
);
729 dev
->mode_config
.num_crtc
--;
731 EXPORT_SYMBOL(drm_crtc_cleanup
);
734 * drm_crtc_index - find the index of a registered CRTC
735 * @crtc: CRTC to find index for
737 * Given a registered CRTC, return the index of that CRTC within a DRM
738 * device's list of CRTCs.
740 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
742 unsigned int index
= 0;
743 struct drm_crtc
*tmp
;
745 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
754 EXPORT_SYMBOL(drm_crtc_index
);
757 * drm_mode_remove - remove and free a mode
758 * @connector: connector list to modify
759 * @mode: mode to remove
761 * Remove @mode from @connector's mode list, then free it.
763 static void drm_mode_remove(struct drm_connector
*connector
,
764 struct drm_display_mode
*mode
)
766 list_del(&mode
->head
);
767 drm_mode_destroy(connector
->dev
, mode
);
771 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
772 * @connector: connector to quwery
773 * @mode: returned mode
775 * The kernel supports per-connector configration of its consoles through
776 * use of the video= parameter. This function parses that option and
777 * extracts the user's specified mode (or enable/disable status) for a
778 * particular connector. This is typically only used during the early fbdev
781 static void drm_connector_get_cmdline_mode(struct drm_connector
*connector
)
783 struct drm_cmdline_mode
*mode
= &connector
->cmdline_mode
;
786 if (fb_get_options(connector
->name
, &option
))
789 if (!drm_mode_parse_command_line_for_connector(option
,
797 switch (mode
->force
) {
801 case DRM_FORCE_ON_DIGITAL
:
810 DRM_INFO("forcing %s connector %s\n", connector
->name
, s
);
811 connector
->force
= mode
->force
;
814 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
816 mode
->xres
, mode
->yres
,
817 mode
->refresh_specified
? mode
->refresh
: 60,
818 mode
->rb
? " reduced blanking" : "",
819 mode
->margins
? " with margins" : "",
820 mode
->interlace
? " interlaced" : "");
824 * drm_connector_init - Init a preallocated connector
826 * @connector: the connector to init
827 * @funcs: callbacks for this connector
828 * @connector_type: user visible type of the connector
830 * Initialises a preallocated connector. Connectors should be
831 * subclassed as part of driver connector objects.
834 * Zero on success, error code on failure.
836 int drm_connector_init(struct drm_device
*dev
,
837 struct drm_connector
*connector
,
838 const struct drm_connector_funcs
*funcs
,
842 struct ida
*connector_ida
=
843 &drm_connector_enum_list
[connector_type
].ida
;
845 drm_modeset_lock_all(dev
);
847 ret
= drm_mode_object_get_reg(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
, false);
851 connector
->base
.properties
= &connector
->properties
;
852 connector
->dev
= dev
;
853 connector
->funcs
= funcs
;
854 connector
->connector_type
= connector_type
;
855 connector
->connector_type_id
=
856 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
857 if (connector
->connector_type_id
< 0) {
858 ret
= connector
->connector_type_id
;
862 kasprintf(GFP_KERNEL
, "%s-%d",
863 drm_connector_enum_list
[connector_type
].name
,
864 connector
->connector_type_id
);
865 if (!connector
->name
) {
870 INIT_LIST_HEAD(&connector
->probed_modes
);
871 INIT_LIST_HEAD(&connector
->modes
);
872 connector
->edid_blob_ptr
= NULL
;
873 connector
->status
= connector_status_unknown
;
875 drm_connector_get_cmdline_mode(connector
);
877 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
878 dev
->mode_config
.num_connector
++;
880 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
881 drm_object_attach_property(&connector
->base
,
882 dev
->mode_config
.edid_property
,
885 drm_object_attach_property(&connector
->base
,
886 dev
->mode_config
.dpms_property
, 0);
888 connector
->debugfs_entry
= NULL
;
892 drm_mode_object_put(dev
, &connector
->base
);
895 drm_modeset_unlock_all(dev
);
899 EXPORT_SYMBOL(drm_connector_init
);
902 * drm_connector_cleanup - cleans up an initialised connector
903 * @connector: connector to cleanup
905 * Cleans up the connector but doesn't free the object.
907 void drm_connector_cleanup(struct drm_connector
*connector
)
909 struct drm_device
*dev
= connector
->dev
;
910 struct drm_display_mode
*mode
, *t
;
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 EXPORT_SYMBOL(drm_connector_cleanup
);
930 * drm_connector_index - find the index of a registered connector
931 * @connector: connector to find index for
933 * Given a registered connector, return the index of that connector within a DRM
934 * device's list of connectors.
936 unsigned int drm_connector_index(struct drm_connector
*connector
)
938 unsigned int index
= 0;
939 struct drm_connector
*tmp
;
941 list_for_each_entry(tmp
, &connector
->dev
->mode_config
.connector_list
, head
) {
942 if (tmp
== connector
)
950 EXPORT_SYMBOL(drm_connector_index
);
953 * drm_connector_register - register a connector
954 * @connector: the connector to register
956 * Register userspace interfaces for a connector
959 * Zero on success, error code on failure.
961 int drm_connector_register(struct drm_connector
*connector
)
965 drm_mode_object_register(connector
->dev
, &connector
->base
);
967 ret
= drm_sysfs_connector_add(connector
);
971 ret
= drm_debugfs_connector_add(connector
);
973 drm_sysfs_connector_remove(connector
);
979 EXPORT_SYMBOL(drm_connector_register
);
982 * drm_connector_unregister - unregister a connector
983 * @connector: the connector to unregister
985 * Unregister userspace interfaces for a connector
987 void drm_connector_unregister(struct drm_connector
*connector
)
989 drm_sysfs_connector_remove(connector
);
990 drm_debugfs_connector_remove(connector
);
992 EXPORT_SYMBOL(drm_connector_unregister
);
996 * drm_connector_unplug_all - unregister connector userspace interfaces
999 * This function unregisters all connector userspace interfaces in sysfs. Should
1000 * be call when the device is disconnected, e.g. from an usb driver's
1001 * ->disconnect callback.
1003 void drm_connector_unplug_all(struct drm_device
*dev
)
1005 struct drm_connector
*connector
;
1007 /* taking the mode config mutex ends up in a clash with sysfs */
1008 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1009 drm_connector_unregister(connector
);
1012 EXPORT_SYMBOL(drm_connector_unplug_all
);
1015 * drm_bridge_init - initialize a drm transcoder/bridge
1017 * @bridge: transcoder/bridge to set up
1018 * @funcs: bridge function table
1020 * Initialises a preallocated bridge. Bridges should be
1021 * subclassed as part of driver connector objects.
1024 * Zero on success, error code on failure.
1026 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
1027 const struct drm_bridge_funcs
*funcs
)
1031 drm_modeset_lock_all(dev
);
1033 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1038 bridge
->funcs
= funcs
;
1040 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1041 dev
->mode_config
.num_bridge
++;
1044 drm_modeset_unlock_all(dev
);
1047 EXPORT_SYMBOL(drm_bridge_init
);
1050 * drm_bridge_cleanup - cleans up an initialised bridge
1051 * @bridge: bridge to cleanup
1053 * Cleans up the bridge but doesn't free the object.
1055 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1057 struct drm_device
*dev
= bridge
->dev
;
1059 drm_modeset_lock_all(dev
);
1060 drm_mode_object_put(dev
, &bridge
->base
);
1061 list_del(&bridge
->head
);
1062 dev
->mode_config
.num_bridge
--;
1063 drm_modeset_unlock_all(dev
);
1065 EXPORT_SYMBOL(drm_bridge_cleanup
);
1068 * drm_encoder_init - Init a preallocated encoder
1070 * @encoder: the encoder to init
1071 * @funcs: callbacks for this encoder
1072 * @encoder_type: user visible type of the encoder
1074 * Initialises a preallocated encoder. Encoder should be
1075 * subclassed as part of driver encoder objects.
1078 * Zero on success, error code on failure.
1080 int drm_encoder_init(struct drm_device
*dev
,
1081 struct drm_encoder
*encoder
,
1082 const struct drm_encoder_funcs
*funcs
,
1087 drm_modeset_lock_all(dev
);
1089 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1094 encoder
->encoder_type
= encoder_type
;
1095 encoder
->funcs
= funcs
;
1096 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1097 drm_encoder_enum_list
[encoder_type
].name
,
1099 if (!encoder
->name
) {
1104 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1105 dev
->mode_config
.num_encoder
++;
1109 drm_mode_object_put(dev
, &encoder
->base
);
1112 drm_modeset_unlock_all(dev
);
1116 EXPORT_SYMBOL(drm_encoder_init
);
1119 * drm_encoder_cleanup - cleans up an initialised encoder
1120 * @encoder: encoder to cleanup
1122 * Cleans up the encoder but doesn't free the object.
1124 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1126 struct drm_device
*dev
= encoder
->dev
;
1127 drm_modeset_lock_all(dev
);
1128 drm_mode_object_put(dev
, &encoder
->base
);
1129 kfree(encoder
->name
);
1130 encoder
->name
= NULL
;
1131 list_del(&encoder
->head
);
1132 dev
->mode_config
.num_encoder
--;
1133 drm_modeset_unlock_all(dev
);
1135 EXPORT_SYMBOL(drm_encoder_cleanup
);
1138 * drm_universal_plane_init - Initialize a new universal plane object
1140 * @plane: plane object to init
1141 * @possible_crtcs: bitmask of possible CRTCs
1142 * @funcs: callbacks for the new plane
1143 * @formats: array of supported formats (%DRM_FORMAT_*)
1144 * @format_count: number of elements in @formats
1145 * @type: type of plane (overlay, primary, cursor)
1147 * Initializes a plane object of type @type.
1150 * Zero on success, error code on failure.
1152 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1153 unsigned long possible_crtcs
,
1154 const struct drm_plane_funcs
*funcs
,
1155 const uint32_t *formats
, uint32_t format_count
,
1156 enum drm_plane_type type
)
1160 drm_modeset_lock_all(dev
);
1162 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1166 plane
->base
.properties
= &plane
->properties
;
1168 plane
->funcs
= funcs
;
1169 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1171 if (!plane
->format_types
) {
1172 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1173 drm_mode_object_put(dev
, &plane
->base
);
1178 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1179 plane
->format_count
= format_count
;
1180 plane
->possible_crtcs
= possible_crtcs
;
1183 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1184 dev
->mode_config
.num_total_plane
++;
1185 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1186 dev
->mode_config
.num_overlay_plane
++;
1188 drm_object_attach_property(&plane
->base
,
1189 dev
->mode_config
.plane_type_property
,
1193 drm_modeset_unlock_all(dev
);
1197 EXPORT_SYMBOL(drm_universal_plane_init
);
1200 * drm_plane_init - Initialize a legacy plane
1202 * @plane: plane object to init
1203 * @possible_crtcs: bitmask of possible CRTCs
1204 * @funcs: callbacks for the new plane
1205 * @formats: array of supported formats (%DRM_FORMAT_*)
1206 * @format_count: number of elements in @formats
1207 * @is_primary: plane type (primary vs overlay)
1209 * Legacy API to initialize a DRM plane.
1211 * New drivers should call drm_universal_plane_init() instead.
1214 * Zero on success, error code on failure.
1216 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1217 unsigned long possible_crtcs
,
1218 const struct drm_plane_funcs
*funcs
,
1219 const uint32_t *formats
, uint32_t format_count
,
1222 enum drm_plane_type type
;
1224 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1225 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1226 formats
, format_count
, type
);
1228 EXPORT_SYMBOL(drm_plane_init
);
1231 * drm_plane_cleanup - Clean up the core plane usage
1232 * @plane: plane to cleanup
1234 * This function cleans up @plane and removes it from the DRM mode setting
1235 * core. Note that the function does *not* free the plane structure itself,
1236 * this is the responsibility of the caller.
1238 void drm_plane_cleanup(struct drm_plane
*plane
)
1240 struct drm_device
*dev
= plane
->dev
;
1242 drm_modeset_lock_all(dev
);
1243 kfree(plane
->format_types
);
1244 drm_mode_object_put(dev
, &plane
->base
);
1246 BUG_ON(list_empty(&plane
->head
));
1248 list_del(&plane
->head
);
1249 dev
->mode_config
.num_total_plane
--;
1250 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1251 dev
->mode_config
.num_overlay_plane
--;
1252 drm_modeset_unlock_all(dev
);
1254 EXPORT_SYMBOL(drm_plane_cleanup
);
1257 * drm_plane_index - find the index of a registered plane
1258 * @plane: plane to find index for
1260 * Given a registered plane, return the index of that CRTC within a DRM
1261 * device's list of planes.
1263 unsigned int drm_plane_index(struct drm_plane
*plane
)
1265 unsigned int index
= 0;
1266 struct drm_plane
*tmp
;
1268 list_for_each_entry(tmp
, &plane
->dev
->mode_config
.plane_list
, head
) {
1277 EXPORT_SYMBOL(drm_plane_index
);
1280 * drm_plane_force_disable - Forcibly disable a plane
1281 * @plane: plane to disable
1283 * Forces the plane to be disabled.
1285 * Used when the plane's current framebuffer is destroyed,
1286 * and when restoring fbdev mode.
1288 void drm_plane_force_disable(struct drm_plane
*plane
)
1295 plane
->old_fb
= plane
->fb
;
1296 ret
= plane
->funcs
->disable_plane(plane
);
1298 DRM_ERROR("failed to disable plane with busy fb\n");
1299 plane
->old_fb
= NULL
;
1302 /* disconnect the plane from the fb and crtc: */
1303 __drm_framebuffer_unreference(plane
->old_fb
);
1304 plane
->old_fb
= NULL
;
1308 EXPORT_SYMBOL(drm_plane_force_disable
);
1310 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1312 struct drm_property
*edid
;
1313 struct drm_property
*dpms
;
1314 struct drm_property
*dev_path
;
1317 * Standard properties (apply to all connectors)
1319 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1320 DRM_MODE_PROP_IMMUTABLE
,
1322 dev
->mode_config
.edid_property
= edid
;
1324 dpms
= drm_property_create_enum(dev
, 0,
1325 "DPMS", drm_dpms_enum_list
,
1326 ARRAY_SIZE(drm_dpms_enum_list
));
1327 dev
->mode_config
.dpms_property
= dpms
;
1329 dev_path
= drm_property_create(dev
,
1330 DRM_MODE_PROP_BLOB
|
1331 DRM_MODE_PROP_IMMUTABLE
,
1333 dev
->mode_config
.path_property
= dev_path
;
1338 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1340 struct drm_property
*type
;
1343 * Standard properties (apply to all planes)
1345 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1346 "type", drm_plane_type_enum_list
,
1347 ARRAY_SIZE(drm_plane_type_enum_list
));
1348 dev
->mode_config
.plane_type_property
= type
;
1354 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1357 * Called by a driver the first time a DVI-I connector is made.
1359 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1361 struct drm_property
*dvi_i_selector
;
1362 struct drm_property
*dvi_i_subconnector
;
1364 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1368 drm_property_create_enum(dev
, 0,
1369 "select subconnector",
1370 drm_dvi_i_select_enum_list
,
1371 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1372 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1374 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1376 drm_dvi_i_subconnector_enum_list
,
1377 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1378 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1382 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1385 * drm_create_tv_properties - create TV specific connector properties
1387 * @num_modes: number of different TV formats (modes) supported
1388 * @modes: array of pointers to strings containing name of each format
1390 * Called by a driver's TV initialization routine, this function creates
1391 * the TV specific connector properties for a given device. Caller is
1392 * responsible for allocating a list of format names and passing them to
1395 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1398 struct drm_property
*tv_selector
;
1399 struct drm_property
*tv_subconnector
;
1402 if (dev
->mode_config
.tv_select_subconnector_property
)
1406 * Basic connector properties
1408 tv_selector
= drm_property_create_enum(dev
, 0,
1409 "select subconnector",
1410 drm_tv_select_enum_list
,
1411 ARRAY_SIZE(drm_tv_select_enum_list
));
1412 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1415 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1417 drm_tv_subconnector_enum_list
,
1418 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1419 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1422 * Other, TV specific properties: margins & TV modes.
1424 dev
->mode_config
.tv_left_margin_property
=
1425 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1427 dev
->mode_config
.tv_right_margin_property
=
1428 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1430 dev
->mode_config
.tv_top_margin_property
=
1431 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1433 dev
->mode_config
.tv_bottom_margin_property
=
1434 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1436 dev
->mode_config
.tv_mode_property
=
1437 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1439 for (i
= 0; i
< num_modes
; i
++)
1440 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1443 dev
->mode_config
.tv_brightness_property
=
1444 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1446 dev
->mode_config
.tv_contrast_property
=
1447 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1449 dev
->mode_config
.tv_flicker_reduction_property
=
1450 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1452 dev
->mode_config
.tv_overscan_property
=
1453 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1455 dev
->mode_config
.tv_saturation_property
=
1456 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1458 dev
->mode_config
.tv_hue_property
=
1459 drm_property_create_range(dev
, 0, "hue", 0, 100);
1463 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1466 * drm_mode_create_scaling_mode_property - create scaling mode property
1469 * Called by a driver the first time it's needed, must be attached to desired
1472 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1474 struct drm_property
*scaling_mode
;
1476 if (dev
->mode_config
.scaling_mode_property
)
1480 drm_property_create_enum(dev
, 0, "scaling mode",
1481 drm_scaling_mode_enum_list
,
1482 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1484 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1488 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1491 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1494 * Called by a driver the first time it's needed, must be attached to desired
1498 * Zero on success, errno on failure.
1500 int drm_mode_create_aspect_ratio_property(struct drm_device
*dev
)
1502 if (dev
->mode_config
.aspect_ratio_property
)
1505 dev
->mode_config
.aspect_ratio_property
=
1506 drm_property_create_enum(dev
, 0, "aspect ratio",
1507 drm_aspect_ratio_enum_list
,
1508 ARRAY_SIZE(drm_aspect_ratio_enum_list
));
1510 if (dev
->mode_config
.aspect_ratio_property
== NULL
)
1515 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property
);
1518 * drm_mode_create_dirty_property - create dirty property
1521 * Called by a driver the first time it's needed, must be attached to desired
1524 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1526 struct drm_property
*dirty_info
;
1528 if (dev
->mode_config
.dirty_info_property
)
1532 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1534 drm_dirty_info_enum_list
,
1535 ARRAY_SIZE(drm_dirty_info_enum_list
));
1536 dev
->mode_config
.dirty_info_property
= dirty_info
;
1540 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1542 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1544 uint32_t total_objects
= 0;
1546 total_objects
+= dev
->mode_config
.num_crtc
;
1547 total_objects
+= dev
->mode_config
.num_connector
;
1548 total_objects
+= dev
->mode_config
.num_encoder
;
1549 total_objects
+= dev
->mode_config
.num_bridge
;
1551 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1552 if (!group
->id_list
)
1555 group
->num_crtcs
= 0;
1556 group
->num_connectors
= 0;
1557 group
->num_encoders
= 0;
1558 group
->num_bridges
= 0;
1562 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1564 kfree(group
->id_list
);
1565 group
->id_list
= NULL
;
1569 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1570 * the drm core's responsibility to set up mode control groups.
1572 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1573 struct drm_mode_group
*group
)
1575 struct drm_crtc
*crtc
;
1576 struct drm_encoder
*encoder
;
1577 struct drm_connector
*connector
;
1578 struct drm_bridge
*bridge
;
1581 if ((ret
= drm_mode_group_init(dev
, group
)))
1584 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1585 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1587 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1588 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1591 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1592 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1593 group
->num_connectors
++] = connector
->base
.id
;
1595 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1596 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1597 group
->num_connectors
+ group
->num_bridges
++] =
1602 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1604 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1606 drm_modeset_lock_all(dev
);
1607 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1608 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1609 drm_modeset_unlock_all(dev
);
1611 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1614 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1615 * @out: drm_mode_modeinfo struct to return to the user
1616 * @in: drm_display_mode to use
1618 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1621 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1622 const struct drm_display_mode
*in
)
1624 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1625 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1626 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1627 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1628 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1629 "timing values too large for mode info\n");
1631 out
->clock
= in
->clock
;
1632 out
->hdisplay
= in
->hdisplay
;
1633 out
->hsync_start
= in
->hsync_start
;
1634 out
->hsync_end
= in
->hsync_end
;
1635 out
->htotal
= in
->htotal
;
1636 out
->hskew
= in
->hskew
;
1637 out
->vdisplay
= in
->vdisplay
;
1638 out
->vsync_start
= in
->vsync_start
;
1639 out
->vsync_end
= in
->vsync_end
;
1640 out
->vtotal
= in
->vtotal
;
1641 out
->vscan
= in
->vscan
;
1642 out
->vrefresh
= in
->vrefresh
;
1643 out
->flags
= in
->flags
;
1644 out
->type
= in
->type
;
1645 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1646 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1650 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1651 * @out: drm_display_mode to return to the user
1652 * @in: drm_mode_modeinfo to use
1654 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1658 * Zero on success, errno on failure.
1660 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1661 const struct drm_mode_modeinfo
*in
)
1663 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1666 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1669 out
->clock
= in
->clock
;
1670 out
->hdisplay
= in
->hdisplay
;
1671 out
->hsync_start
= in
->hsync_start
;
1672 out
->hsync_end
= in
->hsync_end
;
1673 out
->htotal
= in
->htotal
;
1674 out
->hskew
= in
->hskew
;
1675 out
->vdisplay
= in
->vdisplay
;
1676 out
->vsync_start
= in
->vsync_start
;
1677 out
->vsync_end
= in
->vsync_end
;
1678 out
->vtotal
= in
->vtotal
;
1679 out
->vscan
= in
->vscan
;
1680 out
->vrefresh
= in
->vrefresh
;
1681 out
->flags
= in
->flags
;
1682 out
->type
= in
->type
;
1683 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1684 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1690 * drm_mode_getresources - get graphics configuration
1691 * @dev: drm device for the ioctl
1692 * @data: data pointer for the ioctl
1693 * @file_priv: drm file for the ioctl call
1695 * Construct a set of configuration description structures and return
1696 * them to the user, including CRTC, connector and framebuffer configuration.
1698 * Called by the user via ioctl.
1701 * Zero on success, errno on failure.
1703 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1704 struct drm_file
*file_priv
)
1706 struct drm_mode_card_res
*card_res
= data
;
1707 struct list_head
*lh
;
1708 struct drm_framebuffer
*fb
;
1709 struct drm_connector
*connector
;
1710 struct drm_crtc
*crtc
;
1711 struct drm_encoder
*encoder
;
1713 int connector_count
= 0;
1716 int encoder_count
= 0;
1718 uint32_t __user
*fb_id
;
1719 uint32_t __user
*crtc_id
;
1720 uint32_t __user
*connector_id
;
1721 uint32_t __user
*encoder_id
;
1722 struct drm_mode_group
*mode_group
;
1724 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1728 mutex_lock(&file_priv
->fbs_lock
);
1730 * For the non-control nodes we need to limit the list of resources
1731 * by IDs in the group list for this node
1733 list_for_each(lh
, &file_priv
->fbs
)
1736 /* handle this in 4 parts */
1738 if (card_res
->count_fbs
>= fb_count
) {
1740 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1741 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1742 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1743 mutex_unlock(&file_priv
->fbs_lock
);
1749 card_res
->count_fbs
= fb_count
;
1750 mutex_unlock(&file_priv
->fbs_lock
);
1752 drm_modeset_lock_all(dev
);
1753 if (!drm_is_primary_client(file_priv
)) {
1756 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1759 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1762 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1766 mode_group
= &file_priv
->master
->minor
->mode_group
;
1767 crtc_count
= mode_group
->num_crtcs
;
1768 connector_count
= mode_group
->num_connectors
;
1769 encoder_count
= mode_group
->num_encoders
;
1772 card_res
->max_height
= dev
->mode_config
.max_height
;
1773 card_res
->min_height
= dev
->mode_config
.min_height
;
1774 card_res
->max_width
= dev
->mode_config
.max_width
;
1775 card_res
->min_width
= dev
->mode_config
.min_width
;
1778 if (card_res
->count_crtcs
>= crtc_count
) {
1780 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1782 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1784 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1785 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1792 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1793 if (put_user(mode_group
->id_list
[i
],
1794 crtc_id
+ copied
)) {
1802 card_res
->count_crtcs
= crtc_count
;
1805 if (card_res
->count_encoders
>= encoder_count
) {
1807 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1809 list_for_each_entry(encoder
,
1810 &dev
->mode_config
.encoder_list
,
1812 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1814 if (put_user(encoder
->base
.id
, encoder_id
+
1822 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1823 if (put_user(mode_group
->id_list
[i
],
1824 encoder_id
+ copied
)) {
1833 card_res
->count_encoders
= encoder_count
;
1836 if (card_res
->count_connectors
>= connector_count
) {
1838 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1840 list_for_each_entry(connector
,
1841 &dev
->mode_config
.connector_list
,
1843 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1846 if (put_user(connector
->base
.id
,
1847 connector_id
+ copied
)) {
1854 int start
= mode_group
->num_crtcs
+
1855 mode_group
->num_encoders
;
1856 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1857 if (put_user(mode_group
->id_list
[i
],
1858 connector_id
+ copied
)) {
1866 card_res
->count_connectors
= connector_count
;
1868 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1869 card_res
->count_connectors
, card_res
->count_encoders
);
1872 drm_modeset_unlock_all(dev
);
1877 * drm_mode_getcrtc - get CRTC configuration
1878 * @dev: drm device for the ioctl
1879 * @data: data pointer for the ioctl
1880 * @file_priv: drm file for the ioctl call
1882 * Construct a CRTC configuration structure to return to the user.
1884 * Called by the user via ioctl.
1887 * Zero on success, errno on failure.
1889 int drm_mode_getcrtc(struct drm_device
*dev
,
1890 void *data
, struct drm_file
*file_priv
)
1892 struct drm_mode_crtc
*crtc_resp
= data
;
1893 struct drm_crtc
*crtc
;
1896 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1899 drm_modeset_lock_all(dev
);
1901 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1907 crtc_resp
->x
= crtc
->x
;
1908 crtc_resp
->y
= crtc
->y
;
1909 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1910 if (crtc
->primary
->fb
)
1911 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1913 crtc_resp
->fb_id
= 0;
1915 if (crtc
->enabled
) {
1917 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1918 crtc_resp
->mode_valid
= 1;
1921 crtc_resp
->mode_valid
= 0;
1925 drm_modeset_unlock_all(dev
);
1929 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1930 const struct drm_file
*file_priv
)
1933 * If user-space hasn't configured the driver to expose the stereo 3D
1934 * modes, don't expose them.
1936 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1943 * drm_mode_getconnector - get connector configuration
1944 * @dev: drm device for the ioctl
1945 * @data: data pointer for the ioctl
1946 * @file_priv: drm file for the ioctl call
1948 * Construct a connector configuration structure to return to the user.
1950 * Called by the user via ioctl.
1953 * Zero on success, errno on failure.
1955 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1956 struct drm_file
*file_priv
)
1958 struct drm_mode_get_connector
*out_resp
= data
;
1959 struct drm_connector
*connector
;
1960 struct drm_display_mode
*mode
;
1962 int props_count
= 0;
1963 int encoders_count
= 0;
1967 struct drm_mode_modeinfo u_mode
;
1968 struct drm_mode_modeinfo __user
*mode_ptr
;
1969 uint32_t __user
*prop_ptr
;
1970 uint64_t __user
*prop_values
;
1971 uint32_t __user
*encoder_ptr
;
1973 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1976 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1978 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1980 mutex_lock(&dev
->mode_config
.mutex
);
1982 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1988 props_count
= connector
->properties
.count
;
1990 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1991 if (connector
->encoder_ids
[i
] != 0) {
1996 if (out_resp
->count_modes
== 0) {
1997 connector
->funcs
->fill_modes(connector
,
1998 dev
->mode_config
.max_width
,
1999 dev
->mode_config
.max_height
);
2002 /* delayed so we get modes regardless of pre-fill_modes state */
2003 list_for_each_entry(mode
, &connector
->modes
, head
)
2004 if (drm_mode_expose_to_userspace(mode
, file_priv
))
2007 out_resp
->connector_id
= connector
->base
.id
;
2008 out_resp
->connector_type
= connector
->connector_type
;
2009 out_resp
->connector_type_id
= connector
->connector_type_id
;
2010 out_resp
->mm_width
= connector
->display_info
.width_mm
;
2011 out_resp
->mm_height
= connector
->display_info
.height_mm
;
2012 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
2013 out_resp
->connection
= connector
->status
;
2014 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
2015 if (connector
->encoder
)
2016 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
2018 out_resp
->encoder_id
= 0;
2019 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
2022 * This ioctl is called twice, once to determine how much space is
2023 * needed, and the 2nd time to fill it.
2025 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
2027 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
2028 list_for_each_entry(mode
, &connector
->modes
, head
) {
2029 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
2032 drm_crtc_convert_to_umode(&u_mode
, mode
);
2033 if (copy_to_user(mode_ptr
+ copied
,
2034 &u_mode
, sizeof(u_mode
))) {
2041 out_resp
->count_modes
= mode_count
;
2043 if ((out_resp
->count_props
>= props_count
) && props_count
) {
2045 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
2046 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
2047 for (i
= 0; i
< connector
->properties
.count
; i
++) {
2048 if (put_user(connector
->properties
.ids
[i
],
2049 prop_ptr
+ copied
)) {
2054 if (put_user(connector
->properties
.values
[i
],
2055 prop_values
+ copied
)) {
2062 out_resp
->count_props
= props_count
;
2064 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
2066 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
2067 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
2068 if (connector
->encoder_ids
[i
] != 0) {
2069 if (put_user(connector
->encoder_ids
[i
],
2070 encoder_ptr
+ copied
)) {
2078 out_resp
->count_encoders
= encoders_count
;
2081 mutex_unlock(&dev
->mode_config
.mutex
);
2087 * drm_mode_getencoder - get encoder configuration
2088 * @dev: drm device for the ioctl
2089 * @data: data pointer for the ioctl
2090 * @file_priv: drm file for the ioctl call
2092 * Construct a encoder configuration structure to return to the user.
2094 * Called by the user via ioctl.
2097 * Zero on success, errno on failure.
2099 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2100 struct drm_file
*file_priv
)
2102 struct drm_mode_get_encoder
*enc_resp
= data
;
2103 struct drm_encoder
*encoder
;
2106 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2109 drm_modeset_lock_all(dev
);
2110 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2117 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2119 enc_resp
->crtc_id
= 0;
2120 enc_resp
->encoder_type
= encoder
->encoder_type
;
2121 enc_resp
->encoder_id
= encoder
->base
.id
;
2122 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2123 enc_resp
->possible_clones
= encoder
->possible_clones
;
2126 drm_modeset_unlock_all(dev
);
2131 * drm_mode_getplane_res - enumerate all plane resources
2134 * @file_priv: DRM file info
2136 * Construct a list of plane ids to return to the user.
2138 * Called by the user via ioctl.
2141 * Zero on success, errno on failure.
2143 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2144 struct drm_file
*file_priv
)
2146 struct drm_mode_get_plane_res
*plane_resp
= data
;
2147 struct drm_mode_config
*config
;
2148 struct drm_plane
*plane
;
2149 uint32_t __user
*plane_ptr
;
2150 int copied
= 0, ret
= 0;
2151 unsigned num_planes
;
2153 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2156 drm_modeset_lock_all(dev
);
2157 config
= &dev
->mode_config
;
2159 if (file_priv
->universal_planes
)
2160 num_planes
= config
->num_total_plane
;
2162 num_planes
= config
->num_overlay_plane
;
2165 * This ioctl is called twice, once to determine how much space is
2166 * needed, and the 2nd time to fill it.
2169 (plane_resp
->count_planes
>= num_planes
)) {
2170 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2172 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2174 * Unless userspace set the 'universal planes'
2175 * capability bit, only advertise overlays.
2177 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2178 !file_priv
->universal_planes
)
2181 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2188 plane_resp
->count_planes
= num_planes
;
2191 drm_modeset_unlock_all(dev
);
2196 * drm_mode_getplane - get plane configuration
2199 * @file_priv: DRM file info
2201 * Construct a plane configuration structure to return to the user.
2203 * Called by the user via ioctl.
2206 * Zero on success, errno on failure.
2208 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2209 struct drm_file
*file_priv
)
2211 struct drm_mode_get_plane
*plane_resp
= data
;
2212 struct drm_plane
*plane
;
2213 uint32_t __user
*format_ptr
;
2216 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2219 drm_modeset_lock_all(dev
);
2220 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2227 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2229 plane_resp
->crtc_id
= 0;
2232 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2234 plane_resp
->fb_id
= 0;
2236 plane_resp
->plane_id
= plane
->base
.id
;
2237 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2238 plane_resp
->gamma_size
= 0;
2241 * This ioctl is called twice, once to determine how much space is
2242 * needed, and the 2nd time to fill it.
2244 if (plane
->format_count
&&
2245 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2246 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2247 if (copy_to_user(format_ptr
,
2248 plane
->format_types
,
2249 sizeof(uint32_t) * plane
->format_count
)) {
2254 plane_resp
->count_format_types
= plane
->format_count
;
2257 drm_modeset_unlock_all(dev
);
2262 * setplane_internal - setplane handler for internal callers
2264 * Note that we assume an extra reference has already been taken on fb. If the
2265 * update fails, this reference will be dropped before return; if it succeeds,
2266 * the previous framebuffer (if any) will be unreferenced instead.
2268 * src_{x,y,w,h} are provided in 16.16 fixed point format
2270 static int setplane_internal(struct drm_plane
*plane
,
2271 struct drm_crtc
*crtc
,
2272 struct drm_framebuffer
*fb
,
2273 int32_t crtc_x
, int32_t crtc_y
,
2274 uint32_t crtc_w
, uint32_t crtc_h
,
2275 /* src_{x,y,w,h} values are 16.16 fixed point */
2276 uint32_t src_x
, uint32_t src_y
,
2277 uint32_t src_w
, uint32_t src_h
)
2279 struct drm_device
*dev
= plane
->dev
;
2281 unsigned int fb_width
, fb_height
;
2284 drm_modeset_lock_all(dev
);
2285 /* No fb means shut it down */
2287 plane
->old_fb
= plane
->fb
;
2288 ret
= plane
->funcs
->disable_plane(plane
);
2293 plane
->old_fb
= NULL
;
2298 /* Check whether this plane is usable on this CRTC */
2299 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2300 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2305 /* Check whether this plane supports the fb pixel format. */
2306 for (i
= 0; i
< plane
->format_count
; i
++)
2307 if (fb
->pixel_format
== plane
->format_types
[i
])
2309 if (i
== plane
->format_count
) {
2310 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2311 drm_get_format_name(fb
->pixel_format
));
2316 fb_width
= fb
->width
<< 16;
2317 fb_height
= fb
->height
<< 16;
2319 /* Make sure source coordinates are inside the fb. */
2320 if (src_w
> fb_width
||
2321 src_x
> fb_width
- src_w
||
2322 src_h
> fb_height
||
2323 src_y
> fb_height
- src_h
) {
2324 DRM_DEBUG_KMS("Invalid source coordinates "
2325 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2326 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2327 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2328 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2329 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2334 plane
->old_fb
= plane
->fb
;
2335 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2336 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2337 src_x
, src_y
, src_w
, src_h
);
2343 plane
->old_fb
= NULL
;
2348 drm_framebuffer_unreference(fb
);
2350 drm_framebuffer_unreference(plane
->old_fb
);
2351 plane
->old_fb
= NULL
;
2352 drm_modeset_unlock_all(dev
);
2359 * drm_mode_setplane - configure a plane's configuration
2361 * @data: ioctl data*
2362 * @file_priv: DRM file info
2364 * Set plane configuration, including placement, fb, scaling, and other factors.
2365 * Or pass a NULL fb to disable (planes may be disabled without providing a
2369 * Zero on success, errno on failure.
2371 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2372 struct drm_file
*file_priv
)
2374 struct drm_mode_set_plane
*plane_req
= data
;
2375 struct drm_mode_object
*obj
;
2376 struct drm_plane
*plane
;
2377 struct drm_crtc
*crtc
= NULL
;
2378 struct drm_framebuffer
*fb
= NULL
;
2380 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2383 /* Give drivers some help against integer overflows */
2384 if (plane_req
->crtc_w
> INT_MAX
||
2385 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2386 plane_req
->crtc_h
> INT_MAX
||
2387 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2388 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2389 plane_req
->crtc_w
, plane_req
->crtc_h
,
2390 plane_req
->crtc_x
, plane_req
->crtc_y
);
2395 * First, find the plane, crtc, and fb objects. If not available,
2396 * we don't bother to call the driver.
2398 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2399 DRM_MODE_OBJECT_PLANE
);
2401 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2402 plane_req
->plane_id
);
2405 plane
= obj_to_plane(obj
);
2407 if (plane_req
->fb_id
) {
2408 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2410 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2415 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2416 DRM_MODE_OBJECT_CRTC
);
2418 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2419 plane_req
->crtc_id
);
2422 crtc
= obj_to_crtc(obj
);
2426 * setplane_internal will take care of deref'ing either the old or new
2427 * framebuffer depending on success.
2429 return setplane_internal(plane
, crtc
, fb
,
2430 plane_req
->crtc_x
, plane_req
->crtc_y
,
2431 plane_req
->crtc_w
, plane_req
->crtc_h
,
2432 plane_req
->src_x
, plane_req
->src_y
,
2433 plane_req
->src_w
, plane_req
->src_h
);
2437 * drm_mode_set_config_internal - helper to call ->set_config
2438 * @set: modeset config to set
2440 * This is a little helper to wrap internal calls to the ->set_config driver
2441 * interface. The only thing it adds is correct refcounting dance.
2444 * Zero on success, errno on failure.
2446 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2448 struct drm_crtc
*crtc
= set
->crtc
;
2449 struct drm_framebuffer
*fb
;
2450 struct drm_crtc
*tmp
;
2454 * NOTE: ->set_config can also disable other crtcs (if we steal all
2455 * connectors from it), hence we need to refcount the fbs across all
2456 * crtcs. Atomic modeset will have saner semantics ...
2458 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2459 tmp
->primary
->old_fb
= tmp
->primary
->fb
;
2463 ret
= crtc
->funcs
->set_config(set
);
2465 crtc
->primary
->crtc
= crtc
;
2466 crtc
->primary
->fb
= fb
;
2469 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2470 if (tmp
->primary
->fb
)
2471 drm_framebuffer_reference(tmp
->primary
->fb
);
2472 if (tmp
->primary
->old_fb
)
2473 drm_framebuffer_unreference(tmp
->primary
->old_fb
);
2474 tmp
->primary
->old_fb
= NULL
;
2479 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2482 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2484 * @crtc: CRTC that framebuffer will be displayed on
2487 * @mode: mode that framebuffer will be displayed under
2488 * @fb: framebuffer to check size of
2490 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2492 const struct drm_display_mode
*mode
,
2493 const struct drm_framebuffer
*fb
)
2496 int hdisplay
, vdisplay
;
2498 hdisplay
= mode
->hdisplay
;
2499 vdisplay
= mode
->vdisplay
;
2501 if (drm_mode_is_stereo(mode
)) {
2502 struct drm_display_mode adjusted
= *mode
;
2504 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2505 hdisplay
= adjusted
.crtc_hdisplay
;
2506 vdisplay
= adjusted
.crtc_vdisplay
;
2509 if (crtc
->invert_dimensions
)
2510 swap(hdisplay
, vdisplay
);
2512 if (hdisplay
> fb
->width
||
2513 vdisplay
> fb
->height
||
2514 x
> fb
->width
- hdisplay
||
2515 y
> fb
->height
- vdisplay
) {
2516 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2517 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2518 crtc
->invert_dimensions
? " (inverted)" : "");
2524 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2527 * drm_mode_setcrtc - set CRTC configuration
2528 * @dev: drm device for the ioctl
2529 * @data: data pointer for the ioctl
2530 * @file_priv: drm file for the ioctl call
2532 * Build a new CRTC configuration based on user request.
2534 * Called by the user via ioctl.
2537 * Zero on success, errno on failure.
2539 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2540 struct drm_file
*file_priv
)
2542 struct drm_mode_config
*config
= &dev
->mode_config
;
2543 struct drm_mode_crtc
*crtc_req
= data
;
2544 struct drm_crtc
*crtc
;
2545 struct drm_connector
**connector_set
= NULL
, *connector
;
2546 struct drm_framebuffer
*fb
= NULL
;
2547 struct drm_display_mode
*mode
= NULL
;
2548 struct drm_mode_set set
;
2549 uint32_t __user
*set_connectors_ptr
;
2553 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2556 /* For some reason crtc x/y offsets are signed internally. */
2557 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2560 drm_modeset_lock_all(dev
);
2561 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2563 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2567 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2569 if (crtc_req
->mode_valid
) {
2570 /* If we have a mode we need a framebuffer. */
2571 /* If we pass -1, set the mode with the currently bound fb */
2572 if (crtc_req
->fb_id
== -1) {
2573 if (!crtc
->primary
->fb
) {
2574 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2578 fb
= crtc
->primary
->fb
;
2579 /* Make refcounting symmetric with the lookup path. */
2580 drm_framebuffer_reference(fb
);
2582 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2584 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2591 mode
= drm_mode_create(dev
);
2597 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2599 DRM_DEBUG_KMS("Invalid mode\n");
2603 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2605 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2612 if (crtc_req
->count_connectors
== 0 && mode
) {
2613 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2618 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2619 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2620 crtc_req
->count_connectors
);
2625 if (crtc_req
->count_connectors
> 0) {
2628 /* Avoid unbounded kernel memory allocation */
2629 if (crtc_req
->count_connectors
> config
->num_connector
) {
2634 connector_set
= kmalloc(crtc_req
->count_connectors
*
2635 sizeof(struct drm_connector
*),
2637 if (!connector_set
) {
2642 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2643 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2644 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2649 connector
= drm_connector_find(dev
, out_id
);
2651 DRM_DEBUG_KMS("Connector id %d unknown\n",
2656 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2660 connector_set
[i
] = connector
;
2665 set
.x
= crtc_req
->x
;
2666 set
.y
= crtc_req
->y
;
2668 set
.connectors
= connector_set
;
2669 set
.num_connectors
= crtc_req
->count_connectors
;
2671 ret
= drm_mode_set_config_internal(&set
);
2675 drm_framebuffer_unreference(fb
);
2677 kfree(connector_set
);
2678 drm_mode_destroy(dev
, mode
);
2679 drm_modeset_unlock_all(dev
);
2684 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2685 * universal plane handler call
2686 * @crtc: crtc to update cursor for
2687 * @req: data pointer for the ioctl
2688 * @file_priv: drm file for the ioctl call
2690 * Legacy cursor ioctl's work directly with driver buffer handles. To
2691 * translate legacy ioctl calls into universal plane handler calls, we need to
2692 * wrap the native buffer handle in a drm_framebuffer.
2694 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2695 * buffer with a pitch of 4*width; the universal plane interface should be used
2696 * directly in cases where the hardware can support other buffer settings and
2697 * userspace wants to make use of these capabilities.
2700 * Zero on success, errno on failure.
2702 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2703 struct drm_mode_cursor2
*req
,
2704 struct drm_file
*file_priv
)
2706 struct drm_device
*dev
= crtc
->dev
;
2707 struct drm_framebuffer
*fb
= NULL
;
2708 struct drm_mode_fb_cmd2 fbreq
= {
2709 .width
= req
->width
,
2710 .height
= req
->height
,
2711 .pixel_format
= DRM_FORMAT_ARGB8888
,
2712 .pitches
= { req
->width
* 4 },
2713 .handles
= { req
->handle
},
2715 int32_t crtc_x
, crtc_y
;
2716 uint32_t crtc_w
= 0, crtc_h
= 0;
2717 uint32_t src_w
= 0, src_h
= 0;
2720 BUG_ON(!crtc
->cursor
);
2723 * Obtain fb we'll be using (either new or existing) and take an extra
2724 * reference to it if fb != null. setplane will take care of dropping
2725 * the reference if the plane update fails.
2727 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2729 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2731 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2735 drm_framebuffer_reference(fb
);
2740 mutex_lock(&dev
->mode_config
.mutex
);
2741 fb
= crtc
->cursor
->fb
;
2743 drm_framebuffer_reference(fb
);
2744 mutex_unlock(&dev
->mode_config
.mutex
);
2747 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2751 crtc_x
= crtc
->cursor_x
;
2752 crtc_y
= crtc
->cursor_y
;
2757 crtc_h
= fb
->height
;
2758 src_w
= fb
->width
<< 16;
2759 src_h
= fb
->height
<< 16;
2763 * setplane_internal will take care of deref'ing either the old or new
2764 * framebuffer depending on success.
2766 ret
= setplane_internal(crtc
->cursor
, crtc
, fb
,
2767 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2768 0, 0, src_w
, src_h
);
2770 /* Update successful; save new cursor position, if necessary */
2771 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2772 crtc
->cursor_x
= req
->x
;
2773 crtc
->cursor_y
= req
->y
;
2779 static int drm_mode_cursor_common(struct drm_device
*dev
,
2780 struct drm_mode_cursor2
*req
,
2781 struct drm_file
*file_priv
)
2783 struct drm_crtc
*crtc
;
2786 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2789 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2792 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2794 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2799 * If this crtc has a universal cursor plane, call that plane's update
2800 * handler rather than using legacy cursor handlers.
2803 return drm_mode_cursor_universal(crtc
, req
, file_priv
);
2805 drm_modeset_lock_crtc(crtc
);
2806 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2807 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2811 /* Turns off the cursor if handle is 0 */
2812 if (crtc
->funcs
->cursor_set2
)
2813 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2814 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2816 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2817 req
->width
, req
->height
);
2820 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2821 if (crtc
->funcs
->cursor_move
) {
2822 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2829 drm_modeset_unlock_crtc(crtc
);
2837 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2838 * @dev: drm device for the ioctl
2839 * @data: data pointer for the ioctl
2840 * @file_priv: drm file for the ioctl call
2842 * Set the cursor configuration based on user request.
2844 * Called by the user via ioctl.
2847 * Zero on success, errno on failure.
2849 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2850 void *data
, struct drm_file
*file_priv
)
2852 struct drm_mode_cursor
*req
= data
;
2853 struct drm_mode_cursor2 new_req
;
2855 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2856 new_req
.hot_x
= new_req
.hot_y
= 0;
2858 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2862 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2863 * @dev: drm device for the ioctl
2864 * @data: data pointer for the ioctl
2865 * @file_priv: drm file for the ioctl call
2867 * Set the cursor configuration based on user request. This implements the 2nd
2868 * version of the cursor ioctl, which allows userspace to additionally specify
2869 * the hotspot of the pointer.
2871 * Called by the user via ioctl.
2874 * Zero on success, errno on failure.
2876 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2877 void *data
, struct drm_file
*file_priv
)
2879 struct drm_mode_cursor2
*req
= data
;
2880 return drm_mode_cursor_common(dev
, req
, file_priv
);
2884 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2885 * @bpp: bits per pixels
2886 * @depth: bit depth per pixel
2888 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2889 * Useful in fbdev emulation code, since that deals in those values.
2891 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2897 fmt
= DRM_FORMAT_C8
;
2901 fmt
= DRM_FORMAT_XRGB1555
;
2903 fmt
= DRM_FORMAT_RGB565
;
2906 fmt
= DRM_FORMAT_RGB888
;
2910 fmt
= DRM_FORMAT_XRGB8888
;
2911 else if (depth
== 30)
2912 fmt
= DRM_FORMAT_XRGB2101010
;
2914 fmt
= DRM_FORMAT_ARGB8888
;
2917 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2918 fmt
= DRM_FORMAT_XRGB8888
;
2924 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2927 * drm_mode_addfb - add an FB to the graphics configuration
2928 * @dev: drm device for the ioctl
2929 * @data: data pointer for the ioctl
2930 * @file_priv: drm file for the ioctl call
2932 * Add a new FB to the specified CRTC, given a user request. This is the
2933 * original addfb ioclt which only supported RGB formats.
2935 * Called by the user via ioctl.
2938 * Zero on success, errno on failure.
2940 int drm_mode_addfb(struct drm_device
*dev
,
2941 void *data
, struct drm_file
*file_priv
)
2943 struct drm_mode_fb_cmd
*or = data
;
2944 struct drm_mode_fb_cmd2 r
= {};
2945 struct drm_mode_config
*config
= &dev
->mode_config
;
2946 struct drm_framebuffer
*fb
;
2949 /* Use new struct with format internally */
2950 r
.fb_id
= or->fb_id
;
2951 r
.width
= or->width
;
2952 r
.height
= or->height
;
2953 r
.pitches
[0] = or->pitch
;
2954 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2955 r
.handles
[0] = or->handle
;
2957 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2960 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2963 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2966 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2968 DRM_DEBUG_KMS("could not create framebuffer\n");
2972 mutex_lock(&file_priv
->fbs_lock
);
2973 or->fb_id
= fb
->base
.id
;
2974 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2975 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2976 mutex_unlock(&file_priv
->fbs_lock
);
2981 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2983 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2987 case DRM_FORMAT_RGB332
:
2988 case DRM_FORMAT_BGR233
:
2989 case DRM_FORMAT_XRGB4444
:
2990 case DRM_FORMAT_XBGR4444
:
2991 case DRM_FORMAT_RGBX4444
:
2992 case DRM_FORMAT_BGRX4444
:
2993 case DRM_FORMAT_ARGB4444
:
2994 case DRM_FORMAT_ABGR4444
:
2995 case DRM_FORMAT_RGBA4444
:
2996 case DRM_FORMAT_BGRA4444
:
2997 case DRM_FORMAT_XRGB1555
:
2998 case DRM_FORMAT_XBGR1555
:
2999 case DRM_FORMAT_RGBX5551
:
3000 case DRM_FORMAT_BGRX5551
:
3001 case DRM_FORMAT_ARGB1555
:
3002 case DRM_FORMAT_ABGR1555
:
3003 case DRM_FORMAT_RGBA5551
:
3004 case DRM_FORMAT_BGRA5551
:
3005 case DRM_FORMAT_RGB565
:
3006 case DRM_FORMAT_BGR565
:
3007 case DRM_FORMAT_RGB888
:
3008 case DRM_FORMAT_BGR888
:
3009 case DRM_FORMAT_XRGB8888
:
3010 case DRM_FORMAT_XBGR8888
:
3011 case DRM_FORMAT_RGBX8888
:
3012 case DRM_FORMAT_BGRX8888
:
3013 case DRM_FORMAT_ARGB8888
:
3014 case DRM_FORMAT_ABGR8888
:
3015 case DRM_FORMAT_RGBA8888
:
3016 case DRM_FORMAT_BGRA8888
:
3017 case DRM_FORMAT_XRGB2101010
:
3018 case DRM_FORMAT_XBGR2101010
:
3019 case DRM_FORMAT_RGBX1010102
:
3020 case DRM_FORMAT_BGRX1010102
:
3021 case DRM_FORMAT_ARGB2101010
:
3022 case DRM_FORMAT_ABGR2101010
:
3023 case DRM_FORMAT_RGBA1010102
:
3024 case DRM_FORMAT_BGRA1010102
:
3025 case DRM_FORMAT_YUYV
:
3026 case DRM_FORMAT_YVYU
:
3027 case DRM_FORMAT_UYVY
:
3028 case DRM_FORMAT_VYUY
:
3029 case DRM_FORMAT_AYUV
:
3030 case DRM_FORMAT_NV12
:
3031 case DRM_FORMAT_NV21
:
3032 case DRM_FORMAT_NV16
:
3033 case DRM_FORMAT_NV61
:
3034 case DRM_FORMAT_NV24
:
3035 case DRM_FORMAT_NV42
:
3036 case DRM_FORMAT_YUV410
:
3037 case DRM_FORMAT_YVU410
:
3038 case DRM_FORMAT_YUV411
:
3039 case DRM_FORMAT_YVU411
:
3040 case DRM_FORMAT_YUV420
:
3041 case DRM_FORMAT_YVU420
:
3042 case DRM_FORMAT_YUV422
:
3043 case DRM_FORMAT_YVU422
:
3044 case DRM_FORMAT_YUV444
:
3045 case DRM_FORMAT_YVU444
:
3048 DRM_DEBUG_KMS("invalid pixel format %s\n",
3049 drm_get_format_name(r
->pixel_format
));
3054 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
3056 int ret
, hsub
, vsub
, num_planes
, i
;
3058 ret
= format_check(r
);
3060 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3061 drm_get_format_name(r
->pixel_format
));
3065 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
3066 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
3067 num_planes
= drm_format_num_planes(r
->pixel_format
);
3069 if (r
->width
== 0 || r
->width
% hsub
) {
3070 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
3074 if (r
->height
== 0 || r
->height
% vsub
) {
3075 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3079 for (i
= 0; i
< num_planes
; i
++) {
3080 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3081 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3082 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3084 if (!r
->handles
[i
]) {
3085 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3089 if ((uint64_t) width
* cpp
> UINT_MAX
)
3092 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3095 if (r
->pitches
[i
] < width
* cpp
) {
3096 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3104 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
3105 struct drm_mode_fb_cmd2
*r
,
3106 struct drm_file
*file_priv
)
3108 struct drm_mode_config
*config
= &dev
->mode_config
;
3109 struct drm_framebuffer
*fb
;
3112 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3113 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3114 return ERR_PTR(-EINVAL
);
3117 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3118 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3119 r
->width
, config
->min_width
, config
->max_width
);
3120 return ERR_PTR(-EINVAL
);
3122 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3123 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3124 r
->height
, config
->min_height
, config
->max_height
);
3125 return ERR_PTR(-EINVAL
);
3128 ret
= framebuffer_check(r
);
3130 return ERR_PTR(ret
);
3132 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3134 DRM_DEBUG_KMS("could not create framebuffer\n");
3138 mutex_lock(&file_priv
->fbs_lock
);
3139 r
->fb_id
= fb
->base
.id
;
3140 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3141 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3142 mutex_unlock(&file_priv
->fbs_lock
);
3148 * drm_mode_addfb2 - add an FB to the graphics configuration
3149 * @dev: drm device for the ioctl
3150 * @data: data pointer for the ioctl
3151 * @file_priv: drm file for the ioctl call
3153 * Add a new FB to the specified CRTC, given a user request with format. This is
3154 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3155 * and uses fourcc codes as pixel format specifiers.
3157 * Called by the user via ioctl.
3160 * Zero on success, errno on failure.
3162 int drm_mode_addfb2(struct drm_device
*dev
,
3163 void *data
, struct drm_file
*file_priv
)
3165 struct drm_framebuffer
*fb
;
3167 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3170 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3178 * drm_mode_rmfb - remove an FB from the configuration
3179 * @dev: drm device for the ioctl
3180 * @data: data pointer for the ioctl
3181 * @file_priv: drm file for the ioctl call
3183 * Remove the FB specified by the user.
3185 * Called by the user via ioctl.
3188 * Zero on success, errno on failure.
3190 int drm_mode_rmfb(struct drm_device
*dev
,
3191 void *data
, struct drm_file
*file_priv
)
3193 struct drm_framebuffer
*fb
= NULL
;
3194 struct drm_framebuffer
*fbl
= NULL
;
3195 uint32_t *id
= data
;
3198 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3201 mutex_lock(&file_priv
->fbs_lock
);
3202 mutex_lock(&dev
->mode_config
.fb_lock
);
3203 fb
= __drm_framebuffer_lookup(dev
, *id
);
3207 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3213 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3214 __drm_framebuffer_unregister(dev
, fb
);
3216 list_del_init(&fb
->filp_head
);
3217 mutex_unlock(&dev
->mode_config
.fb_lock
);
3218 mutex_unlock(&file_priv
->fbs_lock
);
3220 drm_framebuffer_remove(fb
);
3225 mutex_unlock(&dev
->mode_config
.fb_lock
);
3226 mutex_unlock(&file_priv
->fbs_lock
);
3232 * drm_mode_getfb - get FB info
3233 * @dev: drm device for the ioctl
3234 * @data: data pointer for the ioctl
3235 * @file_priv: drm file for the ioctl call
3237 * Lookup the FB given its ID and return info about it.
3239 * Called by the user via ioctl.
3242 * Zero on success, errno on failure.
3244 int drm_mode_getfb(struct drm_device
*dev
,
3245 void *data
, struct drm_file
*file_priv
)
3247 struct drm_mode_fb_cmd
*r
= data
;
3248 struct drm_framebuffer
*fb
;
3251 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3254 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3258 r
->height
= fb
->height
;
3259 r
->width
= fb
->width
;
3260 r
->depth
= fb
->depth
;
3261 r
->bpp
= fb
->bits_per_pixel
;
3262 r
->pitch
= fb
->pitches
[0];
3263 if (fb
->funcs
->create_handle
) {
3264 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3265 drm_is_control_client(file_priv
)) {
3266 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3269 /* GET_FB() is an unprivileged ioctl so we must not
3270 * return a buffer-handle to non-master processes! For
3271 * backwards-compatibility reasons, we cannot make
3272 * GET_FB() privileged, so just return an invalid handle
3273 * for non-masters. */
3281 drm_framebuffer_unreference(fb
);
3287 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3288 * @dev: drm device for the ioctl
3289 * @data: data pointer for the ioctl
3290 * @file_priv: drm file for the ioctl call
3292 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3293 * rectangle list. Generic userspace which does frontbuffer rendering must call
3294 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3295 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3297 * Modesetting drivers which always update the frontbuffer do not need to
3298 * implement the corresponding ->dirty framebuffer callback.
3300 * Called by the user via ioctl.
3303 * Zero on success, errno on failure.
3305 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3306 void *data
, struct drm_file
*file_priv
)
3308 struct drm_clip_rect __user
*clips_ptr
;
3309 struct drm_clip_rect
*clips
= NULL
;
3310 struct drm_mode_fb_dirty_cmd
*r
= data
;
3311 struct drm_framebuffer
*fb
;
3316 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3319 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3323 num_clips
= r
->num_clips
;
3324 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3326 if (!num_clips
!= !clips_ptr
) {
3331 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3333 /* If userspace annotates copy, clips must come in pairs */
3334 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3339 if (num_clips
&& clips_ptr
) {
3340 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3344 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3350 ret
= copy_from_user(clips
, clips_ptr
,
3351 num_clips
* sizeof(*clips
));
3358 if (fb
->funcs
->dirty
) {
3359 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3368 drm_framebuffer_unreference(fb
);
3375 * drm_fb_release - remove and free the FBs on this file
3376 * @priv: drm file for the ioctl
3378 * Destroy all the FBs associated with @filp.
3380 * Called by the user via ioctl.
3383 * Zero on success, errno on failure.
3385 void drm_fb_release(struct drm_file
*priv
)
3387 struct drm_device
*dev
= priv
->minor
->dev
;
3388 struct drm_framebuffer
*fb
, *tfb
;
3390 mutex_lock(&priv
->fbs_lock
);
3391 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3393 mutex_lock(&dev
->mode_config
.fb_lock
);
3394 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3395 __drm_framebuffer_unregister(dev
, fb
);
3396 mutex_unlock(&dev
->mode_config
.fb_lock
);
3398 list_del_init(&fb
->filp_head
);
3400 /* This will also drop the fpriv->fbs reference. */
3401 drm_framebuffer_remove(fb
);
3403 mutex_unlock(&priv
->fbs_lock
);
3407 * drm_property_create - create a new property type
3409 * @flags: flags specifying the property type
3410 * @name: name of the property
3411 * @num_values: number of pre-defined values
3413 * This creates a new generic drm property which can then be attached to a drm
3414 * object with drm_object_attach_property. The returned property object must be
3415 * freed with drm_property_destroy.
3418 * A pointer to the newly created property on success, NULL on failure.
3420 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3421 const char *name
, int num_values
)
3423 struct drm_property
*property
= NULL
;
3426 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3430 property
->dev
= dev
;
3433 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3434 if (!property
->values
)
3438 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3442 property
->flags
= flags
;
3443 property
->num_values
= num_values
;
3444 INIT_LIST_HEAD(&property
->enum_blob_list
);
3447 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3448 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3451 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3453 WARN_ON(!drm_property_type_valid(property
));
3457 kfree(property
->values
);
3461 EXPORT_SYMBOL(drm_property_create
);
3464 * drm_property_create_enum - create a new enumeration property type
3466 * @flags: flags specifying the property type
3467 * @name: name of the property
3468 * @props: enumeration lists with property values
3469 * @num_values: number of pre-defined values
3471 * This creates a new generic drm property which can then be attached to a drm
3472 * object with drm_object_attach_property. The returned property object must be
3473 * freed with drm_property_destroy.
3475 * Userspace is only allowed to set one of the predefined values for enumeration
3479 * A pointer to the newly created property on success, NULL on failure.
3481 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3483 const struct drm_prop_enum_list
*props
,
3486 struct drm_property
*property
;
3489 flags
|= DRM_MODE_PROP_ENUM
;
3491 property
= drm_property_create(dev
, flags
, name
, num_values
);
3495 for (i
= 0; i
< num_values
; i
++) {
3496 ret
= drm_property_add_enum(property
, i
,
3500 drm_property_destroy(dev
, property
);
3507 EXPORT_SYMBOL(drm_property_create_enum
);
3510 * drm_property_create_bitmask - create a new bitmask property type
3512 * @flags: flags specifying the property type
3513 * @name: name of the property
3514 * @props: enumeration lists with property bitflags
3515 * @num_props: size of the @props array
3516 * @supported_bits: bitmask of all supported enumeration values
3518 * This creates a new bitmask drm property which can then be attached to a drm
3519 * object with drm_object_attach_property. The returned property object must be
3520 * freed with drm_property_destroy.
3522 * Compared to plain enumeration properties userspace is allowed to set any
3523 * or'ed together combination of the predefined property bitflag values
3526 * A pointer to the newly created property on success, NULL on failure.
3528 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3529 int flags
, const char *name
,
3530 const struct drm_prop_enum_list
*props
,
3532 uint64_t supported_bits
)
3534 struct drm_property
*property
;
3535 int i
, ret
, index
= 0;
3536 int num_values
= hweight64(supported_bits
);
3538 flags
|= DRM_MODE_PROP_BITMASK
;
3540 property
= drm_property_create(dev
, flags
, name
, num_values
);
3543 for (i
= 0; i
< num_props
; i
++) {
3544 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3547 if (WARN_ON(index
>= num_values
)) {
3548 drm_property_destroy(dev
, property
);
3552 ret
= drm_property_add_enum(property
, index
++,
3556 drm_property_destroy(dev
, property
);
3563 EXPORT_SYMBOL(drm_property_create_bitmask
);
3565 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3566 int flags
, const char *name
,
3567 uint64_t min
, uint64_t max
)
3569 struct drm_property
*property
;
3571 property
= drm_property_create(dev
, flags
, name
, 2);
3575 property
->values
[0] = min
;
3576 property
->values
[1] = max
;
3582 * drm_property_create_range - create a new ranged property type
3584 * @flags: flags specifying the property type
3585 * @name: name of the property
3586 * @min: minimum value of the property
3587 * @max: maximum value of the property
3589 * This creates a new generic drm property which can then be attached to a drm
3590 * object with drm_object_attach_property. The returned property object must be
3591 * freed with drm_property_destroy.
3593 * Userspace is allowed to set any interger value in the (min, max) range
3597 * A pointer to the newly created property on success, NULL on failure.
3599 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3601 uint64_t min
, uint64_t max
)
3603 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3606 EXPORT_SYMBOL(drm_property_create_range
);
3608 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3609 int flags
, const char *name
,
3610 int64_t min
, int64_t max
)
3612 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3613 name
, I642U64(min
), I642U64(max
));
3615 EXPORT_SYMBOL(drm_property_create_signed_range
);
3617 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3618 int flags
, const char *name
, uint32_t type
)
3620 struct drm_property
*property
;
3622 flags
|= DRM_MODE_PROP_OBJECT
;
3624 property
= drm_property_create(dev
, flags
, name
, 1);
3628 property
->values
[0] = type
;
3632 EXPORT_SYMBOL(drm_property_create_object
);
3635 * drm_property_add_enum - add a possible value to an enumeration property
3636 * @property: enumeration property to change
3637 * @index: index of the new enumeration
3638 * @value: value of the new enumeration
3639 * @name: symbolic name of the new enumeration
3641 * This functions adds enumerations to a property.
3643 * It's use is deprecated, drivers should use one of the more specific helpers
3644 * to directly create the property with all enumerations already attached.
3647 * Zero on success, error code on failure.
3649 int drm_property_add_enum(struct drm_property
*property
, int index
,
3650 uint64_t value
, const char *name
)
3652 struct drm_property_enum
*prop_enum
;
3654 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3655 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3659 * Bitmask enum properties have the additional constraint of values
3662 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3666 if (!list_empty(&property
->enum_blob_list
)) {
3667 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3668 if (prop_enum
->value
== value
) {
3669 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3670 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3676 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3680 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3681 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3682 prop_enum
->value
= value
;
3684 property
->values
[index
] = value
;
3685 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3688 EXPORT_SYMBOL(drm_property_add_enum
);
3691 * drm_property_destroy - destroy a drm property
3693 * @property: property to destry
3695 * This function frees a property including any attached resources like
3696 * enumeration values.
3698 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3700 struct drm_property_enum
*prop_enum
, *pt
;
3702 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3703 list_del(&prop_enum
->head
);
3707 if (property
->num_values
)
3708 kfree(property
->values
);
3709 drm_mode_object_put(dev
, &property
->base
);
3710 list_del(&property
->head
);
3713 EXPORT_SYMBOL(drm_property_destroy
);
3716 * drm_object_attach_property - attach a property to a modeset object
3717 * @obj: drm modeset object
3718 * @property: property to attach
3719 * @init_val: initial value of the property
3721 * This attaches the given property to the modeset object with the given initial
3722 * value. Currently this function cannot fail since the properties are stored in
3723 * a statically sized array.
3725 void drm_object_attach_property(struct drm_mode_object
*obj
,
3726 struct drm_property
*property
,
3729 int count
= obj
->properties
->count
;
3731 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3732 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3733 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3734 "you see this message on the same object type.\n",
3739 obj
->properties
->ids
[count
] = property
->base
.id
;
3740 obj
->properties
->values
[count
] = init_val
;
3741 obj
->properties
->count
++;
3743 EXPORT_SYMBOL(drm_object_attach_property
);
3746 * drm_object_property_set_value - set the value of a property
3747 * @obj: drm mode object to set property value for
3748 * @property: property to set
3749 * @val: value the property should be set to
3751 * This functions sets a given property on a given object. This function only
3752 * changes the software state of the property, it does not call into the
3753 * driver's ->set_property callback.
3756 * Zero on success, error code on failure.
3758 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3759 struct drm_property
*property
, uint64_t val
)
3763 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3764 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3765 obj
->properties
->values
[i
] = val
;
3772 EXPORT_SYMBOL(drm_object_property_set_value
);
3775 * drm_object_property_get_value - retrieve the value of a property
3776 * @obj: drm mode object to get property value from
3777 * @property: property to retrieve
3778 * @val: storage for the property value
3780 * This function retrieves the softare state of the given property for the given
3781 * property. Since there is no driver callback to retrieve the current property
3782 * value this might be out of sync with the hardware, depending upon the driver
3786 * Zero on success, error code on failure.
3788 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3789 struct drm_property
*property
, uint64_t *val
)
3793 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3794 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3795 *val
= obj
->properties
->values
[i
];
3802 EXPORT_SYMBOL(drm_object_property_get_value
);
3805 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3808 * @file_priv: DRM file info
3810 * This function retrieves the current value for an connectors's property.
3812 * Called by the user via ioctl.
3815 * Zero on success, errno on failure.
3817 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3818 void *data
, struct drm_file
*file_priv
)
3820 struct drm_mode_get_property
*out_resp
= data
;
3821 struct drm_property
*property
;
3824 int value_count
= 0;
3827 struct drm_property_enum
*prop_enum
;
3828 struct drm_mode_property_enum __user
*enum_ptr
;
3829 struct drm_property_blob
*prop_blob
;
3830 uint32_t __user
*blob_id_ptr
;
3831 uint64_t __user
*values_ptr
;
3832 uint32_t __user
*blob_length_ptr
;
3834 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3837 drm_modeset_lock_all(dev
);
3838 property
= drm_property_find(dev
, out_resp
->prop_id
);
3844 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3845 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3846 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3848 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3849 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3853 value_count
= property
->num_values
;
3855 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3856 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3857 out_resp
->flags
= property
->flags
;
3859 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3860 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3861 for (i
= 0; i
< value_count
; i
++) {
3862 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3868 out_resp
->count_values
= value_count
;
3870 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3871 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3872 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3874 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3875 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3877 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3882 if (copy_to_user(&enum_ptr
[copied
].name
,
3883 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3890 out_resp
->count_enum_blobs
= enum_count
;
3893 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3894 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3896 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3897 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3899 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3900 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3905 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3913 out_resp
->count_enum_blobs
= blob_count
;
3916 drm_modeset_unlock_all(dev
);
3920 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3923 struct drm_property_blob
*blob
;
3926 if (!length
|| !data
)
3929 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3933 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3939 blob
->length
= length
;
3941 memcpy(blob
->data
, data
, length
);
3943 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3947 static void drm_property_destroy_blob(struct drm_device
*dev
,
3948 struct drm_property_blob
*blob
)
3950 drm_mode_object_put(dev
, &blob
->base
);
3951 list_del(&blob
->head
);
3956 * drm_mode_getblob_ioctl - get the contents of a blob property value
3959 * @file_priv: DRM file info
3961 * This function retrieves the contents of a blob property. The value stored in
3962 * an object's blob property is just a normal modeset object id.
3964 * Called by the user via ioctl.
3967 * Zero on success, errno on failure.
3969 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3970 void *data
, struct drm_file
*file_priv
)
3972 struct drm_mode_get_blob
*out_resp
= data
;
3973 struct drm_property_blob
*blob
;
3975 void __user
*blob_ptr
;
3977 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3980 drm_modeset_lock_all(dev
);
3981 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3987 if (out_resp
->length
== blob
->length
) {
3988 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3989 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3994 out_resp
->length
= blob
->length
;
3997 drm_modeset_unlock_all(dev
);
4001 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
4004 struct drm_device
*dev
= connector
->dev
;
4006 size
= strlen(path
) + 1;
4008 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
4010 if (!connector
->path_blob_ptr
)
4013 ret
= drm_object_property_set_value(&connector
->base
,
4014 dev
->mode_config
.path_property
,
4015 connector
->path_blob_ptr
->base
.id
);
4018 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
4021 * drm_mode_connector_update_edid_property - update the edid property of a connector
4022 * @connector: drm connector
4023 * @edid: new value of the edid property
4025 * This function creates a new blob modeset object and assigns its id to the
4026 * connector's edid property.
4029 * Zero on success, errno on failure.
4031 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
4034 struct drm_device
*dev
= connector
->dev
;
4037 /* ignore requests to set edid when overridden */
4038 if (connector
->override_edid
)
4041 if (connector
->edid_blob_ptr
)
4042 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
4044 /* Delete edid, when there is none. */
4046 connector
->edid_blob_ptr
= NULL
;
4047 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
4051 size
= EDID_LENGTH
* (1 + edid
->extensions
);
4052 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
4054 if (!connector
->edid_blob_ptr
)
4057 ret
= drm_object_property_set_value(&connector
->base
,
4058 dev
->mode_config
.edid_property
,
4059 connector
->edid_blob_ptr
->base
.id
);
4063 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
4065 static bool drm_property_change_is_valid(struct drm_property
*property
,
4068 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
4071 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
4072 if (value
< property
->values
[0] || value
> property
->values
[1])
4075 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
4076 int64_t svalue
= U642I64(value
);
4077 if (svalue
< U642I64(property
->values
[0]) ||
4078 svalue
> U642I64(property
->values
[1]))
4081 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4083 uint64_t valid_mask
= 0;
4084 for (i
= 0; i
< property
->num_values
; i
++)
4085 valid_mask
|= (1ULL << property
->values
[i
]);
4086 return !(value
& ~valid_mask
);
4087 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4088 /* Only the driver knows */
4090 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4091 struct drm_mode_object
*obj
;
4092 /* a zero value for an object property translates to null: */
4096 * NOTE: use _object_find() directly to bypass restriction on
4097 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4098 * object this could race against object finalization, so it
4099 * simply tells us that the object *was* valid. Which is good
4102 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
4106 for (i
= 0; i
< property
->num_values
; i
++)
4107 if (property
->values
[i
] == value
)
4114 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4117 * @file_priv: DRM file info
4119 * This function sets the current value for a connectors's property. It also
4120 * calls into a driver's ->set_property callback to update the hardware state
4122 * Called by the user via ioctl.
4125 * Zero on success, errno on failure.
4127 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4128 void *data
, struct drm_file
*file_priv
)
4130 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4131 struct drm_mode_obj_set_property obj_set_prop
= {
4132 .value
= conn_set_prop
->value
,
4133 .prop_id
= conn_set_prop
->prop_id
,
4134 .obj_id
= conn_set_prop
->connector_id
,
4135 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4138 /* It does all the locking and checking we need */
4139 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4142 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4143 struct drm_property
*property
,
4147 struct drm_connector
*connector
= obj_to_connector(obj
);
4149 /* Do DPMS ourselves */
4150 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4151 if (connector
->funcs
->dpms
)
4152 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4154 } else if (connector
->funcs
->set_property
)
4155 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4157 /* store the property value if successful */
4159 drm_object_property_set_value(&connector
->base
, property
, value
);
4163 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4164 struct drm_property
*property
,
4168 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4170 if (crtc
->funcs
->set_property
)
4171 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4173 drm_object_property_set_value(obj
, property
, value
);
4178 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
4179 struct drm_property
*property
,
4183 struct drm_plane
*plane
= obj_to_plane(obj
);
4185 if (plane
->funcs
->set_property
)
4186 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4188 drm_object_property_set_value(obj
, property
, value
);
4194 * drm_mode_getproperty_ioctl - get the current value of a object's property
4197 * @file_priv: DRM file info
4199 * This function retrieves the current value for an object's property. Compared
4200 * to the connector specific ioctl this one is extended to also work on crtc and
4203 * Called by the user via ioctl.
4206 * Zero on success, errno on failure.
4208 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4209 struct drm_file
*file_priv
)
4211 struct drm_mode_obj_get_properties
*arg
= data
;
4212 struct drm_mode_object
*obj
;
4216 int props_count
= 0;
4217 uint32_t __user
*props_ptr
;
4218 uint64_t __user
*prop_values_ptr
;
4220 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4223 drm_modeset_lock_all(dev
);
4225 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4230 if (!obj
->properties
) {
4235 props_count
= obj
->properties
->count
;
4237 /* This ioctl is called twice, once to determine how much space is
4238 * needed, and the 2nd time to fill it. */
4239 if ((arg
->count_props
>= props_count
) && props_count
) {
4241 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
4242 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
4243 (arg
->prop_values_ptr
);
4244 for (i
= 0; i
< props_count
; i
++) {
4245 if (put_user(obj
->properties
->ids
[i
],
4246 props_ptr
+ copied
)) {
4250 if (put_user(obj
->properties
->values
[i
],
4251 prop_values_ptr
+ copied
)) {
4258 arg
->count_props
= props_count
;
4260 drm_modeset_unlock_all(dev
);
4265 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4268 * @file_priv: DRM file info
4270 * This function sets the current value for an object's property. It also calls
4271 * into a driver's ->set_property callback to update the hardware state.
4272 * Compared to the connector specific ioctl this one is extended to also work on
4273 * crtc and plane objects.
4275 * Called by the user via ioctl.
4278 * Zero on success, errno on failure.
4280 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4281 struct drm_file
*file_priv
)
4283 struct drm_mode_obj_set_property
*arg
= data
;
4284 struct drm_mode_object
*arg_obj
;
4285 struct drm_mode_object
*prop_obj
;
4286 struct drm_property
*property
;
4290 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4293 drm_modeset_lock_all(dev
);
4295 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4300 if (!arg_obj
->properties
)
4303 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4304 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4307 if (i
== arg_obj
->properties
->count
)
4310 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4311 DRM_MODE_OBJECT_PROPERTY
);
4316 property
= obj_to_property(prop_obj
);
4318 if (!drm_property_change_is_valid(property
, arg
->value
))
4321 switch (arg_obj
->type
) {
4322 case DRM_MODE_OBJECT_CONNECTOR
:
4323 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4326 case DRM_MODE_OBJECT_CRTC
:
4327 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4329 case DRM_MODE_OBJECT_PLANE
:
4330 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4335 drm_modeset_unlock_all(dev
);
4340 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4341 * @connector: connector to attach
4342 * @encoder: encoder to attach @connector to
4344 * This function links up a connector to an encoder. Note that the routing
4345 * restrictions between encoders and crtcs are exposed to userspace through the
4346 * possible_clones and possible_crtcs bitmasks.
4349 * Zero on success, errno on failure.
4351 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4352 struct drm_encoder
*encoder
)
4356 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4357 if (connector
->encoder_ids
[i
] == 0) {
4358 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4364 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4367 * drm_mode_crtc_set_gamma_size - set the gamma table size
4368 * @crtc: CRTC to set the gamma table size for
4369 * @gamma_size: size of the gamma table
4371 * Drivers which support gamma tables should set this to the supported gamma
4372 * table size when initializing the CRTC. Currently the drm core only supports a
4373 * fixed gamma table size.
4376 * Zero on success, errno on failure.
4378 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4381 crtc
->gamma_size
= gamma_size
;
4383 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4384 if (!crtc
->gamma_store
) {
4385 crtc
->gamma_size
= 0;
4391 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4394 * drm_mode_gamma_set_ioctl - set the gamma table
4397 * @file_priv: DRM file info
4399 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4400 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4402 * Called by the user via ioctl.
4405 * Zero on success, errno on failure.
4407 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4408 void *data
, struct drm_file
*file_priv
)
4410 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4411 struct drm_crtc
*crtc
;
4412 void *r_base
, *g_base
, *b_base
;
4416 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4419 drm_modeset_lock_all(dev
);
4420 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4426 if (crtc
->funcs
->gamma_set
== NULL
) {
4431 /* memcpy into gamma store */
4432 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4437 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4438 r_base
= crtc
->gamma_store
;
4439 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4444 g_base
= r_base
+ size
;
4445 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4450 b_base
= g_base
+ size
;
4451 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4456 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4459 drm_modeset_unlock_all(dev
);
4465 * drm_mode_gamma_get_ioctl - get the gamma table
4468 * @file_priv: DRM file info
4470 * Copy the current gamma table into the storage provided. This also provides
4471 * the gamma table size the driver expects, which can be used to size the
4472 * allocated storage.
4474 * Called by the user via ioctl.
4477 * Zero on success, errno on failure.
4479 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4480 void *data
, struct drm_file
*file_priv
)
4482 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4483 struct drm_crtc
*crtc
;
4484 void *r_base
, *g_base
, *b_base
;
4488 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4491 drm_modeset_lock_all(dev
);
4492 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4498 /* memcpy into gamma store */
4499 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4504 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4505 r_base
= crtc
->gamma_store
;
4506 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4511 g_base
= r_base
+ size
;
4512 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4517 b_base
= g_base
+ size
;
4518 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4523 drm_modeset_unlock_all(dev
);
4528 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4531 * @file_priv: DRM file info
4533 * This schedules an asynchronous update on a given CRTC, called page flip.
4534 * Optionally a drm event is generated to signal the completion of the event.
4535 * Generic drivers cannot assume that a pageflip with changed framebuffer
4536 * properties (including driver specific metadata like tiling layout) will work,
4537 * but some drivers support e.g. pixel format changes through the pageflip
4540 * Called by the user via ioctl.
4543 * Zero on success, errno on failure.
4545 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4546 void *data
, struct drm_file
*file_priv
)
4548 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4549 struct drm_crtc
*crtc
;
4550 struct drm_framebuffer
*fb
= NULL
;
4551 struct drm_pending_vblank_event
*e
= NULL
;
4552 unsigned long flags
;
4555 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4556 page_flip
->reserved
!= 0)
4559 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4562 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4566 drm_modeset_lock_crtc(crtc
);
4567 if (crtc
->primary
->fb
== NULL
) {
4568 /* The framebuffer is currently unbound, presumably
4569 * due to a hotplug event, that userspace has not
4576 if (crtc
->funcs
->page_flip
== NULL
)
4579 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4585 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4589 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4590 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4595 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4597 spin_lock_irqsave(&dev
->event_lock
, flags
);
4598 if (file_priv
->event_space
< sizeof e
->event
) {
4599 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4602 file_priv
->event_space
-= sizeof e
->event
;
4603 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4605 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4607 spin_lock_irqsave(&dev
->event_lock
, flags
);
4608 file_priv
->event_space
+= sizeof e
->event
;
4609 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4613 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4614 e
->event
.base
.length
= sizeof e
->event
;
4615 e
->event
.user_data
= page_flip
->user_data
;
4616 e
->base
.event
= &e
->event
.base
;
4617 e
->base
.file_priv
= file_priv
;
4619 (void (*) (struct drm_pending_event
*)) kfree
;
4622 crtc
->primary
->old_fb
= crtc
->primary
->fb
;
4623 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4625 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4626 spin_lock_irqsave(&dev
->event_lock
, flags
);
4627 file_priv
->event_space
+= sizeof e
->event
;
4628 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4631 /* Keep the old fb, don't unref it. */
4632 crtc
->primary
->old_fb
= NULL
;
4635 * Warn if the driver hasn't properly updated the crtc->fb
4636 * field to reflect that the new framebuffer is now used.
4637 * Failing to do so will screw with the reference counting
4640 WARN_ON(crtc
->primary
->fb
!= fb
);
4641 /* Unref only the old framebuffer. */
4647 drm_framebuffer_unreference(fb
);
4648 if (crtc
->primary
->old_fb
)
4649 drm_framebuffer_unreference(crtc
->primary
->old_fb
);
4650 crtc
->primary
->old_fb
= NULL
;
4651 drm_modeset_unlock_crtc(crtc
);
4657 * drm_mode_config_reset - call ->reset callbacks
4660 * This functions calls all the crtc's, encoder's and connector's ->reset
4661 * callback. Drivers can use this in e.g. their driver load or resume code to
4662 * reset hardware and software state.
4664 void drm_mode_config_reset(struct drm_device
*dev
)
4666 struct drm_crtc
*crtc
;
4667 struct drm_plane
*plane
;
4668 struct drm_encoder
*encoder
;
4669 struct drm_connector
*connector
;
4671 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
)
4672 if (plane
->funcs
->reset
)
4673 plane
->funcs
->reset(plane
);
4675 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4676 if (crtc
->funcs
->reset
)
4677 crtc
->funcs
->reset(crtc
);
4679 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4680 if (encoder
->funcs
->reset
)
4681 encoder
->funcs
->reset(encoder
);
4683 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4684 connector
->status
= connector_status_unknown
;
4686 if (connector
->funcs
->reset
)
4687 connector
->funcs
->reset(connector
);
4690 EXPORT_SYMBOL(drm_mode_config_reset
);
4693 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4696 * @file_priv: DRM file info
4698 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4699 * TTM or something else entirely) and returns the resulting buffer handle. This
4700 * handle can then be wrapped up into a framebuffer modeset object.
4702 * Note that userspace is not allowed to use such objects for render
4703 * acceleration - drivers must create their own private ioctls for such a use
4706 * Called by the user via ioctl.
4709 * Zero on success, errno on failure.
4711 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4712 void *data
, struct drm_file
*file_priv
)
4714 struct drm_mode_create_dumb
*args
= data
;
4715 u32 cpp
, stride
, size
;
4717 if (!dev
->driver
->dumb_create
)
4719 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4722 /* overflow checks for 32bit size calculations */
4723 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4724 if (cpp
> 0xffffffffU
/ args
->width
)
4726 stride
= cpp
* args
->width
;
4727 if (args
->height
> 0xffffffffU
/ stride
)
4730 /* test for wrap-around */
4731 size
= args
->height
* stride
;
4732 if (PAGE_ALIGN(size
) == 0)
4735 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4739 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4742 * @file_priv: DRM file info
4744 * Allocate an offset in the drm device node's address space to be able to
4745 * memory map a dumb buffer.
4747 * Called by the user via ioctl.
4750 * Zero on success, errno on failure.
4752 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4753 void *data
, struct drm_file
*file_priv
)
4755 struct drm_mode_map_dumb
*args
= data
;
4757 /* call driver ioctl to get mmap offset */
4758 if (!dev
->driver
->dumb_map_offset
)
4761 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4765 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4768 * @file_priv: DRM file info
4770 * This destroys the userspace handle for the given dumb backing storage buffer.
4771 * Since buffer objects must be reference counted in the kernel a buffer object
4772 * won't be immediately freed if a framebuffer modeset object still uses it.
4774 * Called by the user via ioctl.
4777 * Zero on success, errno on failure.
4779 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4780 void *data
, struct drm_file
*file_priv
)
4782 struct drm_mode_destroy_dumb
*args
= data
;
4784 if (!dev
->driver
->dumb_destroy
)
4787 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4791 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4792 * @format: pixel format (DRM_FORMAT_*)
4793 * @depth: storage for the depth value
4794 * @bpp: storage for the bpp value
4796 * This only supports RGB formats here for compat with code that doesn't use
4797 * pixel formats directly yet.
4799 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4804 case DRM_FORMAT_RGB332
:
4805 case DRM_FORMAT_BGR233
:
4809 case DRM_FORMAT_XRGB1555
:
4810 case DRM_FORMAT_XBGR1555
:
4811 case DRM_FORMAT_RGBX5551
:
4812 case DRM_FORMAT_BGRX5551
:
4813 case DRM_FORMAT_ARGB1555
:
4814 case DRM_FORMAT_ABGR1555
:
4815 case DRM_FORMAT_RGBA5551
:
4816 case DRM_FORMAT_BGRA5551
:
4820 case DRM_FORMAT_RGB565
:
4821 case DRM_FORMAT_BGR565
:
4825 case DRM_FORMAT_RGB888
:
4826 case DRM_FORMAT_BGR888
:
4830 case DRM_FORMAT_XRGB8888
:
4831 case DRM_FORMAT_XBGR8888
:
4832 case DRM_FORMAT_RGBX8888
:
4833 case DRM_FORMAT_BGRX8888
:
4837 case DRM_FORMAT_XRGB2101010
:
4838 case DRM_FORMAT_XBGR2101010
:
4839 case DRM_FORMAT_RGBX1010102
:
4840 case DRM_FORMAT_BGRX1010102
:
4841 case DRM_FORMAT_ARGB2101010
:
4842 case DRM_FORMAT_ABGR2101010
:
4843 case DRM_FORMAT_RGBA1010102
:
4844 case DRM_FORMAT_BGRA1010102
:
4848 case DRM_FORMAT_ARGB8888
:
4849 case DRM_FORMAT_ABGR8888
:
4850 case DRM_FORMAT_RGBA8888
:
4851 case DRM_FORMAT_BGRA8888
:
4856 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4857 drm_get_format_name(format
));
4863 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4866 * drm_format_num_planes - get the number of planes for format
4867 * @format: pixel format (DRM_FORMAT_*)
4870 * The number of planes used by the specified pixel format.
4872 int drm_format_num_planes(uint32_t format
)
4875 case DRM_FORMAT_YUV410
:
4876 case DRM_FORMAT_YVU410
:
4877 case DRM_FORMAT_YUV411
:
4878 case DRM_FORMAT_YVU411
:
4879 case DRM_FORMAT_YUV420
:
4880 case DRM_FORMAT_YVU420
:
4881 case DRM_FORMAT_YUV422
:
4882 case DRM_FORMAT_YVU422
:
4883 case DRM_FORMAT_YUV444
:
4884 case DRM_FORMAT_YVU444
:
4886 case DRM_FORMAT_NV12
:
4887 case DRM_FORMAT_NV21
:
4888 case DRM_FORMAT_NV16
:
4889 case DRM_FORMAT_NV61
:
4890 case DRM_FORMAT_NV24
:
4891 case DRM_FORMAT_NV42
:
4897 EXPORT_SYMBOL(drm_format_num_planes
);
4900 * drm_format_plane_cpp - determine the bytes per pixel value
4901 * @format: pixel format (DRM_FORMAT_*)
4902 * @plane: plane index
4905 * The bytes per pixel value for the specified plane.
4907 int drm_format_plane_cpp(uint32_t format
, int plane
)
4912 if (plane
>= drm_format_num_planes(format
))
4916 case DRM_FORMAT_YUYV
:
4917 case DRM_FORMAT_YVYU
:
4918 case DRM_FORMAT_UYVY
:
4919 case DRM_FORMAT_VYUY
:
4921 case DRM_FORMAT_NV12
:
4922 case DRM_FORMAT_NV21
:
4923 case DRM_FORMAT_NV16
:
4924 case DRM_FORMAT_NV61
:
4925 case DRM_FORMAT_NV24
:
4926 case DRM_FORMAT_NV42
:
4927 return plane
? 2 : 1;
4928 case DRM_FORMAT_YUV410
:
4929 case DRM_FORMAT_YVU410
:
4930 case DRM_FORMAT_YUV411
:
4931 case DRM_FORMAT_YVU411
:
4932 case DRM_FORMAT_YUV420
:
4933 case DRM_FORMAT_YVU420
:
4934 case DRM_FORMAT_YUV422
:
4935 case DRM_FORMAT_YVU422
:
4936 case DRM_FORMAT_YUV444
:
4937 case DRM_FORMAT_YVU444
:
4940 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4944 EXPORT_SYMBOL(drm_format_plane_cpp
);
4947 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4948 * @format: pixel format (DRM_FORMAT_*)
4951 * The horizontal chroma subsampling factor for the
4952 * specified pixel format.
4954 int drm_format_horz_chroma_subsampling(uint32_t format
)
4957 case DRM_FORMAT_YUV411
:
4958 case DRM_FORMAT_YVU411
:
4959 case DRM_FORMAT_YUV410
:
4960 case DRM_FORMAT_YVU410
:
4962 case DRM_FORMAT_YUYV
:
4963 case DRM_FORMAT_YVYU
:
4964 case DRM_FORMAT_UYVY
:
4965 case DRM_FORMAT_VYUY
:
4966 case DRM_FORMAT_NV12
:
4967 case DRM_FORMAT_NV21
:
4968 case DRM_FORMAT_NV16
:
4969 case DRM_FORMAT_NV61
:
4970 case DRM_FORMAT_YUV422
:
4971 case DRM_FORMAT_YVU422
:
4972 case DRM_FORMAT_YUV420
:
4973 case DRM_FORMAT_YVU420
:
4979 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4982 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4983 * @format: pixel format (DRM_FORMAT_*)
4986 * The vertical chroma subsampling factor for the
4987 * specified pixel format.
4989 int drm_format_vert_chroma_subsampling(uint32_t format
)
4992 case DRM_FORMAT_YUV410
:
4993 case DRM_FORMAT_YVU410
:
4995 case DRM_FORMAT_YUV420
:
4996 case DRM_FORMAT_YVU420
:
4997 case DRM_FORMAT_NV12
:
4998 case DRM_FORMAT_NV21
:
5004 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
5007 * drm_rotation_simplify() - Try to simplify the rotation
5008 * @rotation: Rotation to be simplified
5009 * @supported_rotations: Supported rotations
5011 * Attempt to simplify the rotation to a form that is supported.
5012 * Eg. if the hardware supports everything except DRM_REFLECT_X
5013 * one could call this function like this:
5015 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5016 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5017 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5019 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5020 * transforms the hardware supports, this function may not
5021 * be able to produce a supported transform, so the caller should
5022 * check the result afterwards.
5024 unsigned int drm_rotation_simplify(unsigned int rotation
,
5025 unsigned int supported_rotations
)
5027 if (rotation
& ~supported_rotations
) {
5028 rotation
^= BIT(DRM_REFLECT_X
) | BIT(DRM_REFLECT_Y
);
5029 rotation
= (rotation
& ~0xf) | BIT((ffs(rotation
& 0xf) + 1) % 4);
5034 EXPORT_SYMBOL(drm_rotation_simplify
);
5037 * drm_mode_config_init - initialize DRM mode_configuration structure
5040 * Initialize @dev's mode_config structure, used for tracking the graphics
5041 * configuration of @dev.
5043 * Since this initializes the modeset locks, no locking is possible. Which is no
5044 * problem, since this should happen single threaded at init time. It is the
5045 * driver's problem to ensure this guarantee.
5048 void drm_mode_config_init(struct drm_device
*dev
)
5050 mutex_init(&dev
->mode_config
.mutex
);
5051 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
5052 mutex_init(&dev
->mode_config
.idr_mutex
);
5053 mutex_init(&dev
->mode_config
.fb_lock
);
5054 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
5055 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
5056 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
5057 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
5058 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
5059 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
5060 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
5061 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
5062 idr_init(&dev
->mode_config
.crtc_idr
);
5064 drm_modeset_lock_all(dev
);
5065 drm_mode_create_standard_connector_properties(dev
);
5066 drm_mode_create_standard_plane_properties(dev
);
5067 drm_modeset_unlock_all(dev
);
5069 /* Just to be sure */
5070 dev
->mode_config
.num_fb
= 0;
5071 dev
->mode_config
.num_connector
= 0;
5072 dev
->mode_config
.num_crtc
= 0;
5073 dev
->mode_config
.num_encoder
= 0;
5074 dev
->mode_config
.num_overlay_plane
= 0;
5075 dev
->mode_config
.num_total_plane
= 0;
5077 EXPORT_SYMBOL(drm_mode_config_init
);
5080 * drm_mode_config_cleanup - free up DRM mode_config info
5083 * Free up all the connectors and CRTCs associated with this DRM device, then
5084 * free up the framebuffers and associated buffer objects.
5086 * Note that since this /should/ happen single-threaded at driver/device
5087 * teardown time, no locking is required. It's the driver's job to ensure that
5088 * this guarantee actually holds true.
5090 * FIXME: cleanup any dangling user buffer objects too
5092 void drm_mode_config_cleanup(struct drm_device
*dev
)
5094 struct drm_connector
*connector
, *ot
;
5095 struct drm_crtc
*crtc
, *ct
;
5096 struct drm_encoder
*encoder
, *enct
;
5097 struct drm_bridge
*bridge
, *brt
;
5098 struct drm_framebuffer
*fb
, *fbt
;
5099 struct drm_property
*property
, *pt
;
5100 struct drm_property_blob
*blob
, *bt
;
5101 struct drm_plane
*plane
, *plt
;
5103 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
5105 encoder
->funcs
->destroy(encoder
);
5108 list_for_each_entry_safe(bridge
, brt
,
5109 &dev
->mode_config
.bridge_list
, head
) {
5110 bridge
->funcs
->destroy(bridge
);
5113 list_for_each_entry_safe(connector
, ot
,
5114 &dev
->mode_config
.connector_list
, head
) {
5115 connector
->funcs
->destroy(connector
);
5118 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5120 drm_property_destroy(dev
, property
);
5123 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5125 drm_property_destroy_blob(dev
, blob
);
5129 * Single-threaded teardown context, so it's not required to grab the
5130 * fb_lock to protect against concurrent fb_list access. Contrary, it
5131 * would actually deadlock with the drm_framebuffer_cleanup function.
5133 * Also, if there are any framebuffers left, that's a driver leak now,
5134 * so politely WARN about this.
5136 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5137 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5138 drm_framebuffer_remove(fb
);
5141 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5143 plane
->funcs
->destroy(plane
);
5146 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5147 crtc
->funcs
->destroy(crtc
);
5150 idr_destroy(&dev
->mode_config
.crtc_idr
);
5151 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5153 EXPORT_SYMBOL(drm_mode_config_cleanup
);
5155 struct drm_property
*drm_mode_create_rotation_property(struct drm_device
*dev
,
5156 unsigned int supported_rotations
)
5158 static const struct drm_prop_enum_list props
[] = {
5159 { DRM_ROTATE_0
, "rotate-0" },
5160 { DRM_ROTATE_90
, "rotate-90" },
5161 { DRM_ROTATE_180
, "rotate-180" },
5162 { DRM_ROTATE_270
, "rotate-270" },
5163 { DRM_REFLECT_X
, "reflect-x" },
5164 { DRM_REFLECT_Y
, "reflect-y" },
5167 return drm_property_create_bitmask(dev
, 0, "rotation",
5168 props
, ARRAY_SIZE(props
),
5169 supported_rotations
);
5171 EXPORT_SYMBOL(drm_mode_create_rotation_property
);