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"
45 * drm_modeset_lock_all - take all modeset locks
48 * This function takes all modeset locks, suitable where a more fine-grained
49 * scheme isn't (yet) implemented. Locks must be dropped with
50 * drm_modeset_unlock_all.
52 void drm_modeset_lock_all(struct drm_device
*dev
)
54 struct drm_mode_config
*config
= &dev
->mode_config
;
55 struct drm_modeset_acquire_ctx
*ctx
;
58 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
62 mutex_lock(&config
->mutex
);
64 drm_modeset_acquire_init(ctx
, 0);
67 ret
= drm_modeset_lock(&config
->connection_mutex
, ctx
);
70 ret
= drm_modeset_lock_all_crtcs(dev
, ctx
);
74 WARN_ON(config
->acquire_ctx
);
76 /* now we hold the locks, so now that it is safe, stash the
77 * ctx for drm_modeset_unlock_all():
79 config
->acquire_ctx
= ctx
;
81 drm_warn_on_modeset_not_all_locked(dev
);
86 if (ret
== -EDEADLK
) {
87 drm_modeset_backoff(ctx
);
91 EXPORT_SYMBOL(drm_modeset_lock_all
);
94 * drm_modeset_unlock_all - drop all modeset locks
97 * This function drop all modeset locks taken by drm_modeset_lock_all.
99 void drm_modeset_unlock_all(struct drm_device
*dev
)
101 struct drm_mode_config
*config
= &dev
->mode_config
;
102 struct drm_modeset_acquire_ctx
*ctx
= config
->acquire_ctx
;
107 config
->acquire_ctx
= NULL
;
108 drm_modeset_drop_locks(ctx
);
109 drm_modeset_acquire_fini(ctx
);
113 mutex_unlock(&dev
->mode_config
.mutex
);
115 EXPORT_SYMBOL(drm_modeset_unlock_all
);
118 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
121 * Useful as a debug assert.
123 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
125 struct drm_crtc
*crtc
;
127 /* Locking is currently fubar in the panic handler. */
128 if (oops_in_progress
)
131 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
132 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
134 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
135 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
137 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
139 /* Avoid boilerplate. I'm tired of typing. */
140 #define DRM_ENUM_NAME_FN(fnname, list) \
141 const char *fnname(int val) \
144 for (i = 0; i < ARRAY_SIZE(list); i++) { \
145 if (list[i].type == val) \
146 return list[i].name; \
148 return "(unknown)"; \
154 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
155 { { DRM_MODE_DPMS_ON
, "On" },
156 { DRM_MODE_DPMS_STANDBY
, "Standby" },
157 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
158 { DRM_MODE_DPMS_OFF
, "Off" }
161 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
163 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] =
165 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
166 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
167 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
171 * Optional properties
173 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
175 { DRM_MODE_SCALE_NONE
, "None" },
176 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
177 { DRM_MODE_SCALE_CENTER
, "Center" },
178 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
182 * Non-global properties, but "required" for certain connectors.
184 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
186 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
187 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
188 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
191 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
193 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
195 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
196 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
197 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
200 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
201 drm_dvi_i_subconnector_enum_list
)
203 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
205 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
206 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
207 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
208 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
209 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
212 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
214 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
216 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
217 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
218 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
219 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
220 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
223 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
224 drm_tv_subconnector_enum_list
)
226 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
227 { DRM_MODE_DIRTY_OFF
, "Off" },
228 { DRM_MODE_DIRTY_ON
, "On" },
229 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
232 struct drm_conn_prop_enum_list
{
239 * Connector and encoder types.
241 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
242 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
243 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
244 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
245 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
246 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
247 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
248 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
249 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
250 { DRM_MODE_CONNECTOR_Component
, "Component" },
251 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
252 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
253 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
254 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
255 { DRM_MODE_CONNECTOR_TV
, "TV" },
256 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
257 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
258 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
261 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
262 { { DRM_MODE_ENCODER_NONE
, "None" },
263 { DRM_MODE_ENCODER_DAC
, "DAC" },
264 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
265 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
266 { DRM_MODE_ENCODER_TVDAC
, "TV" },
267 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
268 { DRM_MODE_ENCODER_DSI
, "DSI" },
269 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
272 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
274 { SubPixelUnknown
, "Unknown" },
275 { SubPixelHorizontalRGB
, "Horizontal RGB" },
276 { SubPixelHorizontalBGR
, "Horizontal BGR" },
277 { SubPixelVerticalRGB
, "Vertical RGB" },
278 { SubPixelVerticalBGR
, "Vertical BGR" },
279 { SubPixelNone
, "None" },
282 void drm_connector_ida_init(void)
286 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
287 ida_init(&drm_connector_enum_list
[i
].ida
);
290 void drm_connector_ida_destroy(void)
294 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
295 ida_destroy(&drm_connector_enum_list
[i
].ida
);
299 * drm_get_connector_status_name - return a string for connector status
300 * @status: connector status to compute name of
302 * In contrast to the other drm_get_*_name functions this one here returns a
303 * const pointer and hence is threadsafe.
305 const char *drm_get_connector_status_name(enum drm_connector_status status
)
307 if (status
== connector_status_connected
)
309 else if (status
== connector_status_disconnected
)
310 return "disconnected";
314 EXPORT_SYMBOL(drm_get_connector_status_name
);
317 * drm_get_subpixel_order_name - return a string for a given subpixel enum
318 * @order: enum of subpixel_order
320 * Note you could abuse this and return something out of bounds, but that
321 * would be a caller error. No unscrubbed user data should make it here.
323 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
325 return drm_subpixel_enum_list
[order
].name
;
327 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
329 static char printable_char(int c
)
331 return isascii(c
) && isprint(c
) ? c
: '?';
335 * drm_get_format_name - return a string for drm fourcc format
336 * @format: format to compute name of
338 * Note that the buffer used by this function is globally shared and owned by
339 * the function itself.
341 * FIXME: This isn't really multithreading safe.
343 const char *drm_get_format_name(uint32_t format
)
347 snprintf(buf
, sizeof(buf
),
348 "%c%c%c%c %s-endian (0x%08x)",
349 printable_char(format
& 0xff),
350 printable_char((format
>> 8) & 0xff),
351 printable_char((format
>> 16) & 0xff),
352 printable_char((format
>> 24) & 0x7f),
353 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
358 EXPORT_SYMBOL(drm_get_format_name
);
361 * drm_mode_object_get - allocate a new modeset identifier
363 * @obj: object pointer, used to generate unique ID
364 * @obj_type: object type
366 * Create a unique identifier based on @ptr in @dev's identifier space. Used
367 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368 * modeset identifiers are _not_ reference counted. Hence don't use this for
369 * reference counted modeset objects like framebuffers.
372 * New unique (relative to other objects in @dev) integer identifier for the
375 int drm_mode_object_get(struct drm_device
*dev
,
376 struct drm_mode_object
*obj
, uint32_t obj_type
)
380 mutex_lock(&dev
->mode_config
.idr_mutex
);
381 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, obj
, 1, 0, GFP_KERNEL
);
384 * Set up the object linking under the protection of the idr
385 * lock so that other users can't see inconsistent state.
388 obj
->type
= obj_type
;
390 mutex_unlock(&dev
->mode_config
.idr_mutex
);
392 return ret
< 0 ? ret
: 0;
396 * drm_mode_object_put - free a modeset identifer
398 * @object: object to free
400 * Free @id from @dev's unique identifier pool. Note that despite the _get
401 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
402 * for reference counted modeset objects like framebuffers.
404 void drm_mode_object_put(struct drm_device
*dev
,
405 struct drm_mode_object
*object
)
407 mutex_lock(&dev
->mode_config
.idr_mutex
);
408 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
409 mutex_unlock(&dev
->mode_config
.idr_mutex
);
412 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
413 uint32_t id
, uint32_t type
)
415 struct drm_mode_object
*obj
= NULL
;
417 mutex_lock(&dev
->mode_config
.idr_mutex
);
418 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
419 if (!obj
|| (type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
) ||
422 mutex_unlock(&dev
->mode_config
.idr_mutex
);
428 * drm_mode_object_find - look up a drm object with static lifetime
430 * @id: id of the mode object
431 * @type: type of the mode object
433 * Note that framebuffers cannot be looked up with this functions - since those
434 * are reference counted, they need special treatment. Even with
435 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
436 * rather than WARN_ON()).
438 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
439 uint32_t id
, uint32_t type
)
441 struct drm_mode_object
*obj
= NULL
;
443 /* Framebuffers are reference counted and need their own lookup
445 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
446 obj
= _object_find(dev
, id
, type
);
447 /* don't leak out unref'd fb's */
448 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
452 EXPORT_SYMBOL(drm_mode_object_find
);
455 * drm_framebuffer_init - initialize a framebuffer
457 * @fb: framebuffer to be initialized
458 * @funcs: ... with these functions
460 * Allocates an ID for the framebuffer's parent mode object, sets its mode
461 * functions & device file and adds it to the master fd list.
464 * This functions publishes the fb and makes it available for concurrent access
465 * by other users. Which means by this point the fb _must_ be fully set up -
466 * since all the fb attributes are invariant over its lifetime, no further
467 * locking but only correct reference counting is required.
470 * Zero on success, error code on failure.
472 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
473 const struct drm_framebuffer_funcs
*funcs
)
477 mutex_lock(&dev
->mode_config
.fb_lock
);
478 kref_init(&fb
->refcount
);
479 INIT_LIST_HEAD(&fb
->filp_head
);
483 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
487 /* Grab the idr reference. */
488 drm_framebuffer_reference(fb
);
490 dev
->mode_config
.num_fb
++;
491 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
493 mutex_unlock(&dev
->mode_config
.fb_lock
);
497 EXPORT_SYMBOL(drm_framebuffer_init
);
499 static void drm_framebuffer_free(struct kref
*kref
)
501 struct drm_framebuffer
*fb
=
502 container_of(kref
, struct drm_framebuffer
, refcount
);
503 fb
->funcs
->destroy(fb
);
506 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
509 struct drm_mode_object
*obj
= NULL
;
510 struct drm_framebuffer
*fb
;
512 mutex_lock(&dev
->mode_config
.idr_mutex
);
513 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
514 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
518 mutex_unlock(&dev
->mode_config
.idr_mutex
);
524 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
526 * @id: id of the fb object
528 * If successful, this grabs an additional reference to the framebuffer -
529 * callers need to make sure to eventually unreference the returned framebuffer
530 * again, using @drm_framebuffer_unreference.
532 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
535 struct drm_framebuffer
*fb
;
537 mutex_lock(&dev
->mode_config
.fb_lock
);
538 fb
= __drm_framebuffer_lookup(dev
, id
);
540 drm_framebuffer_reference(fb
);
541 mutex_unlock(&dev
->mode_config
.fb_lock
);
545 EXPORT_SYMBOL(drm_framebuffer_lookup
);
548 * drm_framebuffer_unreference - unref a framebuffer
549 * @fb: framebuffer to unref
551 * This functions decrements the fb's refcount and frees it if it drops to zero.
553 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
555 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
556 kref_put(&fb
->refcount
, drm_framebuffer_free
);
558 EXPORT_SYMBOL(drm_framebuffer_unreference
);
561 * drm_framebuffer_reference - incr the fb refcnt
564 * This functions increments the fb's refcount.
566 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
568 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
569 kref_get(&fb
->refcount
);
571 EXPORT_SYMBOL(drm_framebuffer_reference
);
573 static void drm_framebuffer_free_bug(struct kref
*kref
)
578 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
580 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
581 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
584 /* dev->mode_config.fb_lock must be held! */
585 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
586 struct drm_framebuffer
*fb
)
588 mutex_lock(&dev
->mode_config
.idr_mutex
);
589 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
590 mutex_unlock(&dev
->mode_config
.idr_mutex
);
594 __drm_framebuffer_unreference(fb
);
598 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
599 * @fb: fb to unregister
601 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
602 * those used for fbdev. Note that the caller must hold a reference of it's own,
603 * i.e. the object may not be destroyed through this call (since it'll lead to a
604 * locking inversion).
606 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
608 struct drm_device
*dev
= fb
->dev
;
610 mutex_lock(&dev
->mode_config
.fb_lock
);
611 /* Mark fb as reaped and drop idr ref. */
612 __drm_framebuffer_unregister(dev
, fb
);
613 mutex_unlock(&dev
->mode_config
.fb_lock
);
615 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
618 * drm_framebuffer_cleanup - remove a framebuffer object
619 * @fb: framebuffer to remove
621 * Cleanup framebuffer. This function is intended to be used from the drivers
622 * ->destroy callback. It can also be used to clean up driver private
623 * framebuffers embedded into a larger structure.
625 * Note that this function does not remove the fb from active usuage - if it is
626 * still used anywhere, hilarity can ensue since userspace could call getfb on
627 * the id and get back -EINVAL. Obviously no concern at driver unload time.
629 * Also, the framebuffer will not be removed from the lookup idr - for
630 * user-created framebuffers this will happen in in the rmfb ioctl. For
631 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
632 * drm_framebuffer_unregister_private.
634 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
636 struct drm_device
*dev
= fb
->dev
;
638 mutex_lock(&dev
->mode_config
.fb_lock
);
640 dev
->mode_config
.num_fb
--;
641 mutex_unlock(&dev
->mode_config
.fb_lock
);
643 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
646 * drm_framebuffer_remove - remove and unreference a framebuffer object
647 * @fb: framebuffer to remove
649 * Scans all the CRTCs and planes in @dev's mode_config. If they're
650 * using @fb, removes it, setting it to NULL. Then drops the reference to the
651 * passed-in framebuffer. Might take the modeset locks.
653 * Note that this function optimizes the cleanup away if the caller holds the
654 * last reference to the framebuffer. It is also guaranteed to not take the
655 * modeset locks in this case.
657 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
659 struct drm_device
*dev
= fb
->dev
;
660 struct drm_crtc
*crtc
;
661 struct drm_plane
*plane
;
662 struct drm_mode_set set
;
665 WARN_ON(!list_empty(&fb
->filp_head
));
668 * drm ABI mandates that we remove any deleted framebuffers from active
669 * useage. But since most sane clients only remove framebuffers they no
670 * longer need, try to optimize this away.
672 * Since we're holding a reference ourselves, observing a refcount of 1
673 * means that we're the last holder and can skip it. Also, the refcount
674 * can never increase from 1 again, so we don't need any barriers or
677 * Note that userspace could try to race with use and instate a new
678 * usage _after_ we've cleared all current ones. End result will be an
679 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
682 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
683 drm_modeset_lock_all(dev
);
684 /* remove from any CRTC */
685 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
686 if (crtc
->primary
->fb
== fb
) {
687 /* should turn off the crtc */
688 memset(&set
, 0, sizeof(struct drm_mode_set
));
691 ret
= drm_mode_set_config_internal(&set
);
693 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
697 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
699 drm_plane_force_disable(plane
);
701 drm_modeset_unlock_all(dev
);
704 drm_framebuffer_unreference(fb
);
706 EXPORT_SYMBOL(drm_framebuffer_remove
);
708 DEFINE_WW_CLASS(crtc_ww_class
);
711 * drm_crtc_init_with_planes - Initialise a new CRTC object with
712 * specified primary and cursor planes.
714 * @crtc: CRTC object to init
715 * @primary: Primary plane for CRTC
716 * @cursor: Cursor plane for CRTC
717 * @funcs: callbacks for the new CRTC
719 * Inits a new object created as base part of a driver crtc object.
722 * Zero on success, error code on failure.
724 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
725 struct drm_plane
*primary
,
727 const struct drm_crtc_funcs
*funcs
)
729 struct drm_mode_config
*config
= &dev
->mode_config
;
734 crtc
->invert_dimensions
= false;
736 drm_modeset_lock_all(dev
);
737 drm_modeset_lock_init(&crtc
->mutex
);
738 /* dropped by _unlock_all(): */
739 drm_modeset_lock(&crtc
->mutex
, config
->acquire_ctx
);
741 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
745 crtc
->base
.properties
= &crtc
->properties
;
747 list_add_tail(&crtc
->head
, &config
->crtc_list
);
750 crtc
->primary
= primary
;
752 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
755 drm_modeset_unlock_all(dev
);
759 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
762 * drm_crtc_cleanup - Clean up the core crtc usage
763 * @crtc: CRTC to cleanup
765 * This function cleans up @crtc and removes it from the DRM mode setting
766 * core. Note that the function does *not* free the crtc structure itself,
767 * this is the responsibility of the caller.
769 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
771 struct drm_device
*dev
= crtc
->dev
;
773 kfree(crtc
->gamma_store
);
774 crtc
->gamma_store
= NULL
;
776 drm_modeset_lock_fini(&crtc
->mutex
);
778 drm_mode_object_put(dev
, &crtc
->base
);
779 list_del(&crtc
->head
);
780 dev
->mode_config
.num_crtc
--;
782 EXPORT_SYMBOL(drm_crtc_cleanup
);
785 * drm_crtc_index - find the index of a registered CRTC
786 * @crtc: CRTC to find index for
788 * Given a registered CRTC, return the index of that CRTC within a DRM
789 * device's list of CRTCs.
791 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
793 unsigned int index
= 0;
794 struct drm_crtc
*tmp
;
796 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
805 EXPORT_SYMBOL(drm_crtc_index
);
808 * drm_mode_remove - remove and free a mode
809 * @connector: connector list to modify
810 * @mode: mode to remove
812 * Remove @mode from @connector's mode list, then free it.
814 static void drm_mode_remove(struct drm_connector
*connector
,
815 struct drm_display_mode
*mode
)
817 list_del(&mode
->head
);
818 drm_mode_destroy(connector
->dev
, mode
);
822 * drm_connector_init - Init a preallocated connector
824 * @connector: the connector to init
825 * @funcs: callbacks for this connector
826 * @connector_type: user visible type of the connector
828 * Initialises a preallocated connector. Connectors should be
829 * subclassed as part of driver connector objects.
832 * Zero on success, error code on failure.
834 int drm_connector_init(struct drm_device
*dev
,
835 struct drm_connector
*connector
,
836 const struct drm_connector_funcs
*funcs
,
840 struct ida
*connector_ida
=
841 &drm_connector_enum_list
[connector_type
].ida
;
843 drm_modeset_lock_all(dev
);
845 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
849 connector
->base
.properties
= &connector
->properties
;
850 connector
->dev
= dev
;
851 connector
->funcs
= funcs
;
852 connector
->connector_type
= connector_type
;
853 connector
->connector_type_id
=
854 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
855 if (connector
->connector_type_id
< 0) {
856 ret
= connector
->connector_type_id
;
860 kasprintf(GFP_KERNEL
, "%s-%d",
861 drm_connector_enum_list
[connector_type
].name
,
862 connector
->connector_type_id
);
863 if (!connector
->name
) {
868 INIT_LIST_HEAD(&connector
->probed_modes
);
869 INIT_LIST_HEAD(&connector
->modes
);
870 connector
->edid_blob_ptr
= NULL
;
871 connector
->status
= connector_status_unknown
;
873 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
874 dev
->mode_config
.num_connector
++;
876 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
877 drm_object_attach_property(&connector
->base
,
878 dev
->mode_config
.edid_property
,
881 drm_object_attach_property(&connector
->base
,
882 dev
->mode_config
.dpms_property
, 0);
884 connector
->debugfs_entry
= NULL
;
888 drm_mode_object_put(dev
, &connector
->base
);
891 drm_modeset_unlock_all(dev
);
895 EXPORT_SYMBOL(drm_connector_init
);
898 * drm_connector_cleanup - cleans up an initialised connector
899 * @connector: connector to cleanup
901 * Cleans up the connector but doesn't free the object.
903 void drm_connector_cleanup(struct drm_connector
*connector
)
905 struct drm_device
*dev
= connector
->dev
;
906 struct drm_display_mode
*mode
, *t
;
908 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
909 drm_mode_remove(connector
, mode
);
911 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
912 drm_mode_remove(connector
, mode
);
914 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
915 connector
->connector_type_id
);
917 drm_mode_object_put(dev
, &connector
->base
);
918 kfree(connector
->name
);
919 connector
->name
= NULL
;
920 list_del(&connector
->head
);
921 dev
->mode_config
.num_connector
--;
923 EXPORT_SYMBOL(drm_connector_cleanup
);
926 * drm_connector_register - register a connector
927 * @connector: the connector to register
929 * Register userspace interfaces for a connector
932 * Zero on success, error code on failure.
934 int drm_connector_register(struct drm_connector
*connector
)
938 ret
= drm_sysfs_connector_add(connector
);
942 ret
= drm_debugfs_connector_add(connector
);
944 drm_sysfs_connector_remove(connector
);
950 EXPORT_SYMBOL(drm_connector_register
);
953 * drm_connector_unregister - unregister a connector
954 * @connector: the connector to unregister
956 * Unregister userspace interfaces for a connector
958 void drm_connector_unregister(struct drm_connector
*connector
)
960 drm_sysfs_connector_remove(connector
);
961 drm_debugfs_connector_remove(connector
);
963 EXPORT_SYMBOL(drm_connector_unregister
);
967 * drm_connector_unplug_all - unregister connector userspace interfaces
970 * This function unregisters all connector userspace interfaces in sysfs. Should
971 * be call when the device is disconnected, e.g. from an usb driver's
972 * ->disconnect callback.
974 void drm_connector_unplug_all(struct drm_device
*dev
)
976 struct drm_connector
*connector
;
978 /* taking the mode config mutex ends up in a clash with sysfs */
979 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
980 drm_connector_unregister(connector
);
983 EXPORT_SYMBOL(drm_connector_unplug_all
);
986 * drm_bridge_init - initialize a drm transcoder/bridge
988 * @bridge: transcoder/bridge to set up
989 * @funcs: bridge function table
991 * Initialises a preallocated bridge. Bridges should be
992 * subclassed as part of driver connector objects.
995 * Zero on success, error code on failure.
997 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
998 const struct drm_bridge_funcs
*funcs
)
1002 drm_modeset_lock_all(dev
);
1004 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1009 bridge
->funcs
= funcs
;
1011 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1012 dev
->mode_config
.num_bridge
++;
1015 drm_modeset_unlock_all(dev
);
1018 EXPORT_SYMBOL(drm_bridge_init
);
1021 * drm_bridge_cleanup - cleans up an initialised bridge
1022 * @bridge: bridge to cleanup
1024 * Cleans up the bridge but doesn't free the object.
1026 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1028 struct drm_device
*dev
= bridge
->dev
;
1030 drm_modeset_lock_all(dev
);
1031 drm_mode_object_put(dev
, &bridge
->base
);
1032 list_del(&bridge
->head
);
1033 dev
->mode_config
.num_bridge
--;
1034 drm_modeset_unlock_all(dev
);
1036 EXPORT_SYMBOL(drm_bridge_cleanup
);
1039 * drm_encoder_init - Init a preallocated encoder
1041 * @encoder: the encoder to init
1042 * @funcs: callbacks for this encoder
1043 * @encoder_type: user visible type of the encoder
1045 * Initialises a preallocated encoder. Encoder should be
1046 * subclassed as part of driver encoder objects.
1049 * Zero on success, error code on failure.
1051 int drm_encoder_init(struct drm_device
*dev
,
1052 struct drm_encoder
*encoder
,
1053 const struct drm_encoder_funcs
*funcs
,
1058 drm_modeset_lock_all(dev
);
1060 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1065 encoder
->encoder_type
= encoder_type
;
1066 encoder
->funcs
= funcs
;
1067 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1068 drm_encoder_enum_list
[encoder_type
].name
,
1070 if (!encoder
->name
) {
1075 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1076 dev
->mode_config
.num_encoder
++;
1080 drm_mode_object_put(dev
, &encoder
->base
);
1083 drm_modeset_unlock_all(dev
);
1087 EXPORT_SYMBOL(drm_encoder_init
);
1090 * drm_encoder_cleanup - cleans up an initialised encoder
1091 * @encoder: encoder to cleanup
1093 * Cleans up the encoder but doesn't free the object.
1095 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1097 struct drm_device
*dev
= encoder
->dev
;
1098 drm_modeset_lock_all(dev
);
1099 drm_mode_object_put(dev
, &encoder
->base
);
1100 kfree(encoder
->name
);
1101 encoder
->name
= NULL
;
1102 list_del(&encoder
->head
);
1103 dev
->mode_config
.num_encoder
--;
1104 drm_modeset_unlock_all(dev
);
1106 EXPORT_SYMBOL(drm_encoder_cleanup
);
1109 * drm_universal_plane_init - Initialize a new universal plane object
1111 * @plane: plane object to init
1112 * @possible_crtcs: bitmask of possible CRTCs
1113 * @funcs: callbacks for the new plane
1114 * @formats: array of supported formats (%DRM_FORMAT_*)
1115 * @format_count: number of elements in @formats
1116 * @type: type of plane (overlay, primary, cursor)
1118 * Initializes a plane object of type @type.
1121 * Zero on success, error code on failure.
1123 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1124 unsigned long possible_crtcs
,
1125 const struct drm_plane_funcs
*funcs
,
1126 const uint32_t *formats
, uint32_t format_count
,
1127 enum drm_plane_type type
)
1131 drm_modeset_lock_all(dev
);
1133 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1137 plane
->base
.properties
= &plane
->properties
;
1139 plane
->funcs
= funcs
;
1140 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1142 if (!plane
->format_types
) {
1143 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1144 drm_mode_object_put(dev
, &plane
->base
);
1149 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1150 plane
->format_count
= format_count
;
1151 plane
->possible_crtcs
= possible_crtcs
;
1154 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1155 dev
->mode_config
.num_total_plane
++;
1156 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1157 dev
->mode_config
.num_overlay_plane
++;
1159 drm_object_attach_property(&plane
->base
,
1160 dev
->mode_config
.plane_type_property
,
1164 drm_modeset_unlock_all(dev
);
1168 EXPORT_SYMBOL(drm_universal_plane_init
);
1171 * drm_plane_init - Initialize a legacy plane
1173 * @plane: plane object to init
1174 * @possible_crtcs: bitmask of possible CRTCs
1175 * @funcs: callbacks for the new plane
1176 * @formats: array of supported formats (%DRM_FORMAT_*)
1177 * @format_count: number of elements in @formats
1178 * @is_primary: plane type (primary vs overlay)
1180 * Legacy API to initialize a DRM plane.
1182 * New drivers should call drm_universal_plane_init() instead.
1185 * Zero on success, error code on failure.
1187 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1188 unsigned long possible_crtcs
,
1189 const struct drm_plane_funcs
*funcs
,
1190 const uint32_t *formats
, uint32_t format_count
,
1193 enum drm_plane_type type
;
1195 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1196 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1197 formats
, format_count
, type
);
1199 EXPORT_SYMBOL(drm_plane_init
);
1202 * drm_plane_cleanup - Clean up the core plane usage
1203 * @plane: plane to cleanup
1205 * This function cleans up @plane and removes it from the DRM mode setting
1206 * core. Note that the function does *not* free the plane structure itself,
1207 * this is the responsibility of the caller.
1209 void drm_plane_cleanup(struct drm_plane
*plane
)
1211 struct drm_device
*dev
= plane
->dev
;
1213 drm_modeset_lock_all(dev
);
1214 kfree(plane
->format_types
);
1215 drm_mode_object_put(dev
, &plane
->base
);
1217 BUG_ON(list_empty(&plane
->head
));
1219 list_del(&plane
->head
);
1220 dev
->mode_config
.num_total_plane
--;
1221 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1222 dev
->mode_config
.num_overlay_plane
--;
1223 drm_modeset_unlock_all(dev
);
1225 EXPORT_SYMBOL(drm_plane_cleanup
);
1228 * drm_plane_force_disable - Forcibly disable a plane
1229 * @plane: plane to disable
1231 * Forces the plane to be disabled.
1233 * Used when the plane's current framebuffer is destroyed,
1234 * and when restoring fbdev mode.
1236 void drm_plane_force_disable(struct drm_plane
*plane
)
1238 struct drm_framebuffer
*old_fb
= plane
->fb
;
1244 ret
= plane
->funcs
->disable_plane(plane
);
1246 DRM_ERROR("failed to disable plane with busy fb\n");
1249 /* disconnect the plane from the fb and crtc: */
1250 __drm_framebuffer_unreference(old_fb
);
1254 EXPORT_SYMBOL(drm_plane_force_disable
);
1256 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1258 struct drm_property
*edid
;
1259 struct drm_property
*dpms
;
1262 * Standard properties (apply to all connectors)
1264 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1265 DRM_MODE_PROP_IMMUTABLE
,
1267 dev
->mode_config
.edid_property
= edid
;
1269 dpms
= drm_property_create_enum(dev
, 0,
1270 "DPMS", drm_dpms_enum_list
,
1271 ARRAY_SIZE(drm_dpms_enum_list
));
1272 dev
->mode_config
.dpms_property
= dpms
;
1277 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1279 struct drm_property
*type
;
1282 * Standard properties (apply to all planes)
1284 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1285 "type", drm_plane_type_enum_list
,
1286 ARRAY_SIZE(drm_plane_type_enum_list
));
1287 dev
->mode_config
.plane_type_property
= type
;
1293 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1296 * Called by a driver the first time a DVI-I connector is made.
1298 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1300 struct drm_property
*dvi_i_selector
;
1301 struct drm_property
*dvi_i_subconnector
;
1303 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1307 drm_property_create_enum(dev
, 0,
1308 "select subconnector",
1309 drm_dvi_i_select_enum_list
,
1310 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1311 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1313 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1315 drm_dvi_i_subconnector_enum_list
,
1316 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1317 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1321 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1324 * drm_create_tv_properties - create TV specific connector properties
1326 * @num_modes: number of different TV formats (modes) supported
1327 * @modes: array of pointers to strings containing name of each format
1329 * Called by a driver's TV initialization routine, this function creates
1330 * the TV specific connector properties for a given device. Caller is
1331 * responsible for allocating a list of format names and passing them to
1334 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1337 struct drm_property
*tv_selector
;
1338 struct drm_property
*tv_subconnector
;
1341 if (dev
->mode_config
.tv_select_subconnector_property
)
1345 * Basic connector properties
1347 tv_selector
= drm_property_create_enum(dev
, 0,
1348 "select subconnector",
1349 drm_tv_select_enum_list
,
1350 ARRAY_SIZE(drm_tv_select_enum_list
));
1351 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1354 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1356 drm_tv_subconnector_enum_list
,
1357 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1358 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1361 * Other, TV specific properties: margins & TV modes.
1363 dev
->mode_config
.tv_left_margin_property
=
1364 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1366 dev
->mode_config
.tv_right_margin_property
=
1367 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1369 dev
->mode_config
.tv_top_margin_property
=
1370 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1372 dev
->mode_config
.tv_bottom_margin_property
=
1373 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1375 dev
->mode_config
.tv_mode_property
=
1376 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1378 for (i
= 0; i
< num_modes
; i
++)
1379 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1382 dev
->mode_config
.tv_brightness_property
=
1383 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1385 dev
->mode_config
.tv_contrast_property
=
1386 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1388 dev
->mode_config
.tv_flicker_reduction_property
=
1389 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1391 dev
->mode_config
.tv_overscan_property
=
1392 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1394 dev
->mode_config
.tv_saturation_property
=
1395 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1397 dev
->mode_config
.tv_hue_property
=
1398 drm_property_create_range(dev
, 0, "hue", 0, 100);
1402 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1405 * drm_mode_create_scaling_mode_property - create scaling mode property
1408 * Called by a driver the first time it's needed, must be attached to desired
1411 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1413 struct drm_property
*scaling_mode
;
1415 if (dev
->mode_config
.scaling_mode_property
)
1419 drm_property_create_enum(dev
, 0, "scaling mode",
1420 drm_scaling_mode_enum_list
,
1421 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1423 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1427 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1430 * drm_mode_create_dirty_property - create dirty property
1433 * Called by a driver the first time it's needed, must be attached to desired
1436 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1438 struct drm_property
*dirty_info
;
1440 if (dev
->mode_config
.dirty_info_property
)
1444 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1446 drm_dirty_info_enum_list
,
1447 ARRAY_SIZE(drm_dirty_info_enum_list
));
1448 dev
->mode_config
.dirty_info_property
= dirty_info
;
1452 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1454 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1456 uint32_t total_objects
= 0;
1458 total_objects
+= dev
->mode_config
.num_crtc
;
1459 total_objects
+= dev
->mode_config
.num_connector
;
1460 total_objects
+= dev
->mode_config
.num_encoder
;
1461 total_objects
+= dev
->mode_config
.num_bridge
;
1463 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1464 if (!group
->id_list
)
1467 group
->num_crtcs
= 0;
1468 group
->num_connectors
= 0;
1469 group
->num_encoders
= 0;
1470 group
->num_bridges
= 0;
1474 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1476 kfree(group
->id_list
);
1477 group
->id_list
= NULL
;
1481 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1482 * the drm core's responsibility to set up mode control groups.
1484 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1485 struct drm_mode_group
*group
)
1487 struct drm_crtc
*crtc
;
1488 struct drm_encoder
*encoder
;
1489 struct drm_connector
*connector
;
1490 struct drm_bridge
*bridge
;
1493 if ((ret
= drm_mode_group_init(dev
, group
)))
1496 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1497 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1499 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1500 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1503 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1504 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1505 group
->num_connectors
++] = connector
->base
.id
;
1507 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1508 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1509 group
->num_connectors
+ group
->num_bridges
++] =
1514 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1516 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1518 drm_modeset_lock_all(dev
);
1519 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1520 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1521 drm_modeset_unlock_all(dev
);
1523 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1526 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1527 * @out: drm_mode_modeinfo struct to return to the user
1528 * @in: drm_display_mode to use
1530 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1533 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1534 const struct drm_display_mode
*in
)
1536 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1537 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1538 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1539 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1540 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1541 "timing values too large for mode info\n");
1543 out
->clock
= in
->clock
;
1544 out
->hdisplay
= in
->hdisplay
;
1545 out
->hsync_start
= in
->hsync_start
;
1546 out
->hsync_end
= in
->hsync_end
;
1547 out
->htotal
= in
->htotal
;
1548 out
->hskew
= in
->hskew
;
1549 out
->vdisplay
= in
->vdisplay
;
1550 out
->vsync_start
= in
->vsync_start
;
1551 out
->vsync_end
= in
->vsync_end
;
1552 out
->vtotal
= in
->vtotal
;
1553 out
->vscan
= in
->vscan
;
1554 out
->vrefresh
= in
->vrefresh
;
1555 out
->flags
= in
->flags
;
1556 out
->type
= in
->type
;
1557 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1558 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1562 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1563 * @out: drm_display_mode to return to the user
1564 * @in: drm_mode_modeinfo to use
1566 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1570 * Zero on success, errno on failure.
1572 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1573 const struct drm_mode_modeinfo
*in
)
1575 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1578 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1581 out
->clock
= in
->clock
;
1582 out
->hdisplay
= in
->hdisplay
;
1583 out
->hsync_start
= in
->hsync_start
;
1584 out
->hsync_end
= in
->hsync_end
;
1585 out
->htotal
= in
->htotal
;
1586 out
->hskew
= in
->hskew
;
1587 out
->vdisplay
= in
->vdisplay
;
1588 out
->vsync_start
= in
->vsync_start
;
1589 out
->vsync_end
= in
->vsync_end
;
1590 out
->vtotal
= in
->vtotal
;
1591 out
->vscan
= in
->vscan
;
1592 out
->vrefresh
= in
->vrefresh
;
1593 out
->flags
= in
->flags
;
1594 out
->type
= in
->type
;
1595 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1596 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1602 * drm_mode_getresources - get graphics configuration
1603 * @dev: drm device for the ioctl
1604 * @data: data pointer for the ioctl
1605 * @file_priv: drm file for the ioctl call
1607 * Construct a set of configuration description structures and return
1608 * them to the user, including CRTC, connector and framebuffer configuration.
1610 * Called by the user via ioctl.
1613 * Zero on success, errno on failure.
1615 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1616 struct drm_file
*file_priv
)
1618 struct drm_mode_card_res
*card_res
= data
;
1619 struct list_head
*lh
;
1620 struct drm_framebuffer
*fb
;
1621 struct drm_connector
*connector
;
1622 struct drm_crtc
*crtc
;
1623 struct drm_encoder
*encoder
;
1625 int connector_count
= 0;
1628 int encoder_count
= 0;
1630 uint32_t __user
*fb_id
;
1631 uint32_t __user
*crtc_id
;
1632 uint32_t __user
*connector_id
;
1633 uint32_t __user
*encoder_id
;
1634 struct drm_mode_group
*mode_group
;
1636 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1640 mutex_lock(&file_priv
->fbs_lock
);
1642 * For the non-control nodes we need to limit the list of resources
1643 * by IDs in the group list for this node
1645 list_for_each(lh
, &file_priv
->fbs
)
1648 /* handle this in 4 parts */
1650 if (card_res
->count_fbs
>= fb_count
) {
1652 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1653 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1654 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1655 mutex_unlock(&file_priv
->fbs_lock
);
1661 card_res
->count_fbs
= fb_count
;
1662 mutex_unlock(&file_priv
->fbs_lock
);
1664 drm_modeset_lock_all(dev
);
1665 if (!drm_is_primary_client(file_priv
)) {
1668 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1671 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1674 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1678 mode_group
= &file_priv
->master
->minor
->mode_group
;
1679 crtc_count
= mode_group
->num_crtcs
;
1680 connector_count
= mode_group
->num_connectors
;
1681 encoder_count
= mode_group
->num_encoders
;
1684 card_res
->max_height
= dev
->mode_config
.max_height
;
1685 card_res
->min_height
= dev
->mode_config
.min_height
;
1686 card_res
->max_width
= dev
->mode_config
.max_width
;
1687 card_res
->min_width
= dev
->mode_config
.min_width
;
1690 if (card_res
->count_crtcs
>= crtc_count
) {
1692 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1694 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1696 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1697 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1704 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1705 if (put_user(mode_group
->id_list
[i
],
1706 crtc_id
+ copied
)) {
1714 card_res
->count_crtcs
= crtc_count
;
1717 if (card_res
->count_encoders
>= encoder_count
) {
1719 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1721 list_for_each_entry(encoder
,
1722 &dev
->mode_config
.encoder_list
,
1724 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1726 if (put_user(encoder
->base
.id
, encoder_id
+
1734 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1735 if (put_user(mode_group
->id_list
[i
],
1736 encoder_id
+ copied
)) {
1745 card_res
->count_encoders
= encoder_count
;
1748 if (card_res
->count_connectors
>= connector_count
) {
1750 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1752 list_for_each_entry(connector
,
1753 &dev
->mode_config
.connector_list
,
1755 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1758 if (put_user(connector
->base
.id
,
1759 connector_id
+ copied
)) {
1766 int start
= mode_group
->num_crtcs
+
1767 mode_group
->num_encoders
;
1768 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1769 if (put_user(mode_group
->id_list
[i
],
1770 connector_id
+ copied
)) {
1778 card_res
->count_connectors
= connector_count
;
1780 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1781 card_res
->count_connectors
, card_res
->count_encoders
);
1784 drm_modeset_unlock_all(dev
);
1789 * drm_mode_getcrtc - get CRTC configuration
1790 * @dev: drm device for the ioctl
1791 * @data: data pointer for the ioctl
1792 * @file_priv: drm file for the ioctl call
1794 * Construct a CRTC configuration structure to return to the user.
1796 * Called by the user via ioctl.
1799 * Zero on success, errno on failure.
1801 int drm_mode_getcrtc(struct drm_device
*dev
,
1802 void *data
, struct drm_file
*file_priv
)
1804 struct drm_mode_crtc
*crtc_resp
= data
;
1805 struct drm_crtc
*crtc
;
1808 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1811 drm_modeset_lock_all(dev
);
1813 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1819 crtc_resp
->x
= crtc
->x
;
1820 crtc_resp
->y
= crtc
->y
;
1821 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1822 if (crtc
->primary
->fb
)
1823 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1825 crtc_resp
->fb_id
= 0;
1827 if (crtc
->enabled
) {
1829 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1830 crtc_resp
->mode_valid
= 1;
1833 crtc_resp
->mode_valid
= 0;
1837 drm_modeset_unlock_all(dev
);
1841 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1842 const struct drm_file
*file_priv
)
1845 * If user-space hasn't configured the driver to expose the stereo 3D
1846 * modes, don't expose them.
1848 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1855 * drm_mode_getconnector - get connector configuration
1856 * @dev: drm device for the ioctl
1857 * @data: data pointer for the ioctl
1858 * @file_priv: drm file for the ioctl call
1860 * Construct a connector configuration structure to return to the user.
1862 * Called by the user via ioctl.
1865 * Zero on success, errno on failure.
1867 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1868 struct drm_file
*file_priv
)
1870 struct drm_mode_get_connector
*out_resp
= data
;
1871 struct drm_connector
*connector
;
1872 struct drm_display_mode
*mode
;
1874 int props_count
= 0;
1875 int encoders_count
= 0;
1879 struct drm_mode_modeinfo u_mode
;
1880 struct drm_mode_modeinfo __user
*mode_ptr
;
1881 uint32_t __user
*prop_ptr
;
1882 uint64_t __user
*prop_values
;
1883 uint32_t __user
*encoder_ptr
;
1885 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1888 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1890 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1892 mutex_lock(&dev
->mode_config
.mutex
);
1894 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1900 props_count
= connector
->properties
.count
;
1902 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1903 if (connector
->encoder_ids
[i
] != 0) {
1908 if (out_resp
->count_modes
== 0) {
1909 connector
->funcs
->fill_modes(connector
,
1910 dev
->mode_config
.max_width
,
1911 dev
->mode_config
.max_height
);
1914 /* delayed so we get modes regardless of pre-fill_modes state */
1915 list_for_each_entry(mode
, &connector
->modes
, head
)
1916 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1919 out_resp
->connector_id
= connector
->base
.id
;
1920 out_resp
->connector_type
= connector
->connector_type
;
1921 out_resp
->connector_type_id
= connector
->connector_type_id
;
1922 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1923 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1924 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1925 out_resp
->connection
= connector
->status
;
1926 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1927 if (connector
->encoder
)
1928 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1930 out_resp
->encoder_id
= 0;
1931 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1934 * This ioctl is called twice, once to determine how much space is
1935 * needed, and the 2nd time to fill it.
1937 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1939 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1940 list_for_each_entry(mode
, &connector
->modes
, head
) {
1941 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
1944 drm_crtc_convert_to_umode(&u_mode
, mode
);
1945 if (copy_to_user(mode_ptr
+ copied
,
1946 &u_mode
, sizeof(u_mode
))) {
1953 out_resp
->count_modes
= mode_count
;
1955 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1957 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1958 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1959 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1960 if (put_user(connector
->properties
.ids
[i
],
1961 prop_ptr
+ copied
)) {
1966 if (put_user(connector
->properties
.values
[i
],
1967 prop_values
+ copied
)) {
1974 out_resp
->count_props
= props_count
;
1976 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1978 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1979 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1980 if (connector
->encoder_ids
[i
] != 0) {
1981 if (put_user(connector
->encoder_ids
[i
],
1982 encoder_ptr
+ copied
)) {
1990 out_resp
->count_encoders
= encoders_count
;
1993 mutex_unlock(&dev
->mode_config
.mutex
);
1999 * drm_mode_getencoder - get encoder configuration
2000 * @dev: drm device for the ioctl
2001 * @data: data pointer for the ioctl
2002 * @file_priv: drm file for the ioctl call
2004 * Construct a encoder configuration structure to return to the user.
2006 * Called by the user via ioctl.
2009 * Zero on success, errno on failure.
2011 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2012 struct drm_file
*file_priv
)
2014 struct drm_mode_get_encoder
*enc_resp
= data
;
2015 struct drm_encoder
*encoder
;
2018 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2021 drm_modeset_lock_all(dev
);
2022 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2029 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2031 enc_resp
->crtc_id
= 0;
2032 enc_resp
->encoder_type
= encoder
->encoder_type
;
2033 enc_resp
->encoder_id
= encoder
->base
.id
;
2034 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2035 enc_resp
->possible_clones
= encoder
->possible_clones
;
2038 drm_modeset_unlock_all(dev
);
2043 * drm_mode_getplane_res - enumerate all plane resources
2046 * @file_priv: DRM file info
2048 * Construct a list of plane ids to return to the user.
2050 * Called by the user via ioctl.
2053 * Zero on success, errno on failure.
2055 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2056 struct drm_file
*file_priv
)
2058 struct drm_mode_get_plane_res
*plane_resp
= data
;
2059 struct drm_mode_config
*config
;
2060 struct drm_plane
*plane
;
2061 uint32_t __user
*plane_ptr
;
2062 int copied
= 0, ret
= 0;
2063 unsigned num_planes
;
2065 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2068 drm_modeset_lock_all(dev
);
2069 config
= &dev
->mode_config
;
2071 if (file_priv
->universal_planes
)
2072 num_planes
= config
->num_total_plane
;
2074 num_planes
= config
->num_overlay_plane
;
2077 * This ioctl is called twice, once to determine how much space is
2078 * needed, and the 2nd time to fill it.
2081 (plane_resp
->count_planes
>= num_planes
)) {
2082 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2084 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2086 * Unless userspace set the 'universal planes'
2087 * capability bit, only advertise overlays.
2089 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2090 !file_priv
->universal_planes
)
2093 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2100 plane_resp
->count_planes
= num_planes
;
2103 drm_modeset_unlock_all(dev
);
2108 * drm_mode_getplane - get plane configuration
2111 * @file_priv: DRM file info
2113 * Construct a plane configuration structure to return to the user.
2115 * Called by the user via ioctl.
2118 * Zero on success, errno on failure.
2120 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2121 struct drm_file
*file_priv
)
2123 struct drm_mode_get_plane
*plane_resp
= data
;
2124 struct drm_plane
*plane
;
2125 uint32_t __user
*format_ptr
;
2128 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2131 drm_modeset_lock_all(dev
);
2132 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2139 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2141 plane_resp
->crtc_id
= 0;
2144 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2146 plane_resp
->fb_id
= 0;
2148 plane_resp
->plane_id
= plane
->base
.id
;
2149 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2150 plane_resp
->gamma_size
= 0;
2153 * This ioctl is called twice, once to determine how much space is
2154 * needed, and the 2nd time to fill it.
2156 if (plane
->format_count
&&
2157 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2158 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2159 if (copy_to_user(format_ptr
,
2160 plane
->format_types
,
2161 sizeof(uint32_t) * plane
->format_count
)) {
2166 plane_resp
->count_format_types
= plane
->format_count
;
2169 drm_modeset_unlock_all(dev
);
2174 * drm_mode_setplane - configure a plane's configuration
2176 * @data: ioctl data*
2177 * @file_priv: DRM file info
2179 * Set plane configuration, including placement, fb, scaling, and other factors.
2180 * Or pass a NULL fb to disable.
2183 * Zero on success, errno on failure.
2185 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2186 struct drm_file
*file_priv
)
2188 struct drm_mode_set_plane
*plane_req
= data
;
2189 struct drm_plane
*plane
;
2190 struct drm_crtc
*crtc
;
2191 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
2193 unsigned int fb_width
, fb_height
;
2196 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2200 * First, find the plane, crtc, and fb objects. If not available,
2201 * we don't bother to call the driver.
2203 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2205 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2206 plane_req
->plane_id
);
2210 /* No fb means shut it down */
2211 if (!plane_req
->fb_id
) {
2212 drm_modeset_lock_all(dev
);
2214 ret
= plane
->funcs
->disable_plane(plane
);
2221 drm_modeset_unlock_all(dev
);
2225 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2227 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2228 plane_req
->crtc_id
);
2233 /* Check whether this plane is usable on this CRTC */
2234 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2235 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2240 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2242 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2248 /* Check whether this plane supports the fb pixel format. */
2249 for (i
= 0; i
< plane
->format_count
; i
++)
2250 if (fb
->pixel_format
== plane
->format_types
[i
])
2252 if (i
== plane
->format_count
) {
2253 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2254 drm_get_format_name(fb
->pixel_format
));
2259 fb_width
= fb
->width
<< 16;
2260 fb_height
= fb
->height
<< 16;
2262 /* Make sure source coordinates are inside the fb. */
2263 if (plane_req
->src_w
> fb_width
||
2264 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
2265 plane_req
->src_h
> fb_height
||
2266 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
2267 DRM_DEBUG_KMS("Invalid source coordinates "
2268 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2269 plane_req
->src_w
>> 16,
2270 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
2271 plane_req
->src_h
>> 16,
2272 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
2273 plane_req
->src_x
>> 16,
2274 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
2275 plane_req
->src_y
>> 16,
2276 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
2281 /* Give drivers some help against integer overflows */
2282 if (plane_req
->crtc_w
> INT_MAX
||
2283 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2284 plane_req
->crtc_h
> INT_MAX
||
2285 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2286 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2287 plane_req
->crtc_w
, plane_req
->crtc_h
,
2288 plane_req
->crtc_x
, plane_req
->crtc_y
);
2293 drm_modeset_lock_all(dev
);
2295 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2296 plane_req
->crtc_x
, plane_req
->crtc_y
,
2297 plane_req
->crtc_w
, plane_req
->crtc_h
,
2298 plane_req
->src_x
, plane_req
->src_y
,
2299 plane_req
->src_w
, plane_req
->src_h
);
2307 drm_modeset_unlock_all(dev
);
2311 drm_framebuffer_unreference(fb
);
2313 drm_framebuffer_unreference(old_fb
);
2319 * drm_mode_set_config_internal - helper to call ->set_config
2320 * @set: modeset config to set
2322 * This is a little helper to wrap internal calls to the ->set_config driver
2323 * interface. The only thing it adds is correct refcounting dance.
2326 * Zero on success, errno on failure.
2328 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2330 struct drm_crtc
*crtc
= set
->crtc
;
2331 struct drm_framebuffer
*fb
;
2332 struct drm_crtc
*tmp
;
2336 * NOTE: ->set_config can also disable other crtcs (if we steal all
2337 * connectors from it), hence we need to refcount the fbs across all
2338 * crtcs. Atomic modeset will have saner semantics ...
2340 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2341 tmp
->old_fb
= tmp
->primary
->fb
;
2345 ret
= crtc
->funcs
->set_config(set
);
2347 crtc
->primary
->crtc
= crtc
;
2348 crtc
->primary
->fb
= fb
;
2351 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2352 if (tmp
->primary
->fb
)
2353 drm_framebuffer_reference(tmp
->primary
->fb
);
2355 drm_framebuffer_unreference(tmp
->old_fb
);
2360 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2363 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2365 * @crtc: CRTC that framebuffer will be displayed on
2368 * @mode: mode that framebuffer will be displayed under
2369 * @fb: framebuffer to check size of
2371 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2373 const struct drm_display_mode
*mode
,
2374 const struct drm_framebuffer
*fb
)
2377 int hdisplay
, vdisplay
;
2379 hdisplay
= mode
->hdisplay
;
2380 vdisplay
= mode
->vdisplay
;
2382 if (drm_mode_is_stereo(mode
)) {
2383 struct drm_display_mode adjusted
= *mode
;
2385 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2386 hdisplay
= adjusted
.crtc_hdisplay
;
2387 vdisplay
= adjusted
.crtc_vdisplay
;
2390 if (crtc
->invert_dimensions
)
2391 swap(hdisplay
, vdisplay
);
2393 if (hdisplay
> fb
->width
||
2394 vdisplay
> fb
->height
||
2395 x
> fb
->width
- hdisplay
||
2396 y
> fb
->height
- vdisplay
) {
2397 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2398 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2399 crtc
->invert_dimensions
? " (inverted)" : "");
2405 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2408 * drm_mode_setcrtc - set CRTC configuration
2409 * @dev: drm device for the ioctl
2410 * @data: data pointer for the ioctl
2411 * @file_priv: drm file for the ioctl call
2413 * Build a new CRTC configuration based on user request.
2415 * Called by the user via ioctl.
2418 * Zero on success, errno on failure.
2420 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2421 struct drm_file
*file_priv
)
2423 struct drm_mode_config
*config
= &dev
->mode_config
;
2424 struct drm_mode_crtc
*crtc_req
= data
;
2425 struct drm_crtc
*crtc
;
2426 struct drm_connector
**connector_set
= NULL
, *connector
;
2427 struct drm_framebuffer
*fb
= NULL
;
2428 struct drm_display_mode
*mode
= NULL
;
2429 struct drm_mode_set set
;
2430 uint32_t __user
*set_connectors_ptr
;
2434 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2437 /* For some reason crtc x/y offsets are signed internally. */
2438 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2441 drm_modeset_lock_all(dev
);
2442 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2444 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2448 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2450 if (crtc_req
->mode_valid
) {
2451 /* If we have a mode we need a framebuffer. */
2452 /* If we pass -1, set the mode with the currently bound fb */
2453 if (crtc_req
->fb_id
== -1) {
2454 if (!crtc
->primary
->fb
) {
2455 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2459 fb
= crtc
->primary
->fb
;
2460 /* Make refcounting symmetric with the lookup path. */
2461 drm_framebuffer_reference(fb
);
2463 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2465 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2472 mode
= drm_mode_create(dev
);
2478 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2480 DRM_DEBUG_KMS("Invalid mode\n");
2484 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2486 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2493 if (crtc_req
->count_connectors
== 0 && mode
) {
2494 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2499 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2500 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2501 crtc_req
->count_connectors
);
2506 if (crtc_req
->count_connectors
> 0) {
2509 /* Avoid unbounded kernel memory allocation */
2510 if (crtc_req
->count_connectors
> config
->num_connector
) {
2515 connector_set
= kmalloc(crtc_req
->count_connectors
*
2516 sizeof(struct drm_connector
*),
2518 if (!connector_set
) {
2523 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2524 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2525 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2530 connector
= drm_connector_find(dev
, out_id
);
2532 DRM_DEBUG_KMS("Connector id %d unknown\n",
2537 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2541 connector_set
[i
] = connector
;
2546 set
.x
= crtc_req
->x
;
2547 set
.y
= crtc_req
->y
;
2549 set
.connectors
= connector_set
;
2550 set
.num_connectors
= crtc_req
->count_connectors
;
2552 ret
= drm_mode_set_config_internal(&set
);
2556 drm_framebuffer_unreference(fb
);
2558 kfree(connector_set
);
2559 drm_mode_destroy(dev
, mode
);
2560 drm_modeset_unlock_all(dev
);
2564 static int drm_mode_cursor_common(struct drm_device
*dev
,
2565 struct drm_mode_cursor2
*req
,
2566 struct drm_file
*file_priv
)
2568 struct drm_crtc
*crtc
;
2571 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2574 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2577 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2579 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2583 drm_modeset_lock(&crtc
->mutex
, NULL
);
2584 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2585 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2589 /* Turns off the cursor if handle is 0 */
2590 if (crtc
->funcs
->cursor_set2
)
2591 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2592 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2594 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2595 req
->width
, req
->height
);
2598 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2599 if (crtc
->funcs
->cursor_move
) {
2600 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2607 drm_modeset_unlock(&crtc
->mutex
);
2615 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2616 * @dev: drm device for the ioctl
2617 * @data: data pointer for the ioctl
2618 * @file_priv: drm file for the ioctl call
2620 * Set the cursor configuration based on user request.
2622 * Called by the user via ioctl.
2625 * Zero on success, errno on failure.
2627 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2628 void *data
, struct drm_file
*file_priv
)
2630 struct drm_mode_cursor
*req
= data
;
2631 struct drm_mode_cursor2 new_req
;
2633 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2634 new_req
.hot_x
= new_req
.hot_y
= 0;
2636 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2640 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2641 * @dev: drm device for the ioctl
2642 * @data: data pointer for the ioctl
2643 * @file_priv: drm file for the ioctl call
2645 * Set the cursor configuration based on user request. This implements the 2nd
2646 * version of the cursor ioctl, which allows userspace to additionally specify
2647 * the hotspot of the pointer.
2649 * Called by the user via ioctl.
2652 * Zero on success, errno on failure.
2654 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2655 void *data
, struct drm_file
*file_priv
)
2657 struct drm_mode_cursor2
*req
= data
;
2658 return drm_mode_cursor_common(dev
, req
, file_priv
);
2662 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2663 * @bpp: bits per pixels
2664 * @depth: bit depth per pixel
2666 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2667 * Useful in fbdev emulation code, since that deals in those values.
2669 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2675 fmt
= DRM_FORMAT_C8
;
2679 fmt
= DRM_FORMAT_XRGB1555
;
2681 fmt
= DRM_FORMAT_RGB565
;
2684 fmt
= DRM_FORMAT_RGB888
;
2688 fmt
= DRM_FORMAT_XRGB8888
;
2689 else if (depth
== 30)
2690 fmt
= DRM_FORMAT_XRGB2101010
;
2692 fmt
= DRM_FORMAT_ARGB8888
;
2695 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2696 fmt
= DRM_FORMAT_XRGB8888
;
2702 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2705 * drm_mode_addfb - add an FB to the graphics configuration
2706 * @dev: drm device for the ioctl
2707 * @data: data pointer for the ioctl
2708 * @file_priv: drm file for the ioctl call
2710 * Add a new FB to the specified CRTC, given a user request. This is the
2711 * original addfb ioclt which only supported RGB formats.
2713 * Called by the user via ioctl.
2716 * Zero on success, errno on failure.
2718 int drm_mode_addfb(struct drm_device
*dev
,
2719 void *data
, struct drm_file
*file_priv
)
2721 struct drm_mode_fb_cmd
*or = data
;
2722 struct drm_mode_fb_cmd2 r
= {};
2723 struct drm_mode_config
*config
= &dev
->mode_config
;
2724 struct drm_framebuffer
*fb
;
2727 /* Use new struct with format internally */
2728 r
.fb_id
= or->fb_id
;
2729 r
.width
= or->width
;
2730 r
.height
= or->height
;
2731 r
.pitches
[0] = or->pitch
;
2732 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2733 r
.handles
[0] = or->handle
;
2735 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2738 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2741 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2744 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2746 DRM_DEBUG_KMS("could not create framebuffer\n");
2750 mutex_lock(&file_priv
->fbs_lock
);
2751 or->fb_id
= fb
->base
.id
;
2752 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2753 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2754 mutex_unlock(&file_priv
->fbs_lock
);
2759 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2761 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2765 case DRM_FORMAT_RGB332
:
2766 case DRM_FORMAT_BGR233
:
2767 case DRM_FORMAT_XRGB4444
:
2768 case DRM_FORMAT_XBGR4444
:
2769 case DRM_FORMAT_RGBX4444
:
2770 case DRM_FORMAT_BGRX4444
:
2771 case DRM_FORMAT_ARGB4444
:
2772 case DRM_FORMAT_ABGR4444
:
2773 case DRM_FORMAT_RGBA4444
:
2774 case DRM_FORMAT_BGRA4444
:
2775 case DRM_FORMAT_XRGB1555
:
2776 case DRM_FORMAT_XBGR1555
:
2777 case DRM_FORMAT_RGBX5551
:
2778 case DRM_FORMAT_BGRX5551
:
2779 case DRM_FORMAT_ARGB1555
:
2780 case DRM_FORMAT_ABGR1555
:
2781 case DRM_FORMAT_RGBA5551
:
2782 case DRM_FORMAT_BGRA5551
:
2783 case DRM_FORMAT_RGB565
:
2784 case DRM_FORMAT_BGR565
:
2785 case DRM_FORMAT_RGB888
:
2786 case DRM_FORMAT_BGR888
:
2787 case DRM_FORMAT_XRGB8888
:
2788 case DRM_FORMAT_XBGR8888
:
2789 case DRM_FORMAT_RGBX8888
:
2790 case DRM_FORMAT_BGRX8888
:
2791 case DRM_FORMAT_ARGB8888
:
2792 case DRM_FORMAT_ABGR8888
:
2793 case DRM_FORMAT_RGBA8888
:
2794 case DRM_FORMAT_BGRA8888
:
2795 case DRM_FORMAT_XRGB2101010
:
2796 case DRM_FORMAT_XBGR2101010
:
2797 case DRM_FORMAT_RGBX1010102
:
2798 case DRM_FORMAT_BGRX1010102
:
2799 case DRM_FORMAT_ARGB2101010
:
2800 case DRM_FORMAT_ABGR2101010
:
2801 case DRM_FORMAT_RGBA1010102
:
2802 case DRM_FORMAT_BGRA1010102
:
2803 case DRM_FORMAT_YUYV
:
2804 case DRM_FORMAT_YVYU
:
2805 case DRM_FORMAT_UYVY
:
2806 case DRM_FORMAT_VYUY
:
2807 case DRM_FORMAT_AYUV
:
2808 case DRM_FORMAT_NV12
:
2809 case DRM_FORMAT_NV21
:
2810 case DRM_FORMAT_NV16
:
2811 case DRM_FORMAT_NV61
:
2812 case DRM_FORMAT_NV24
:
2813 case DRM_FORMAT_NV42
:
2814 case DRM_FORMAT_YUV410
:
2815 case DRM_FORMAT_YVU410
:
2816 case DRM_FORMAT_YUV411
:
2817 case DRM_FORMAT_YVU411
:
2818 case DRM_FORMAT_YUV420
:
2819 case DRM_FORMAT_YVU420
:
2820 case DRM_FORMAT_YUV422
:
2821 case DRM_FORMAT_YVU422
:
2822 case DRM_FORMAT_YUV444
:
2823 case DRM_FORMAT_YVU444
:
2826 DRM_DEBUG_KMS("invalid pixel format %s\n",
2827 drm_get_format_name(r
->pixel_format
));
2832 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2834 int ret
, hsub
, vsub
, num_planes
, i
;
2836 ret
= format_check(r
);
2838 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2839 drm_get_format_name(r
->pixel_format
));
2843 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2844 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2845 num_planes
= drm_format_num_planes(r
->pixel_format
);
2847 if (r
->width
== 0 || r
->width
% hsub
) {
2848 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2852 if (r
->height
== 0 || r
->height
% vsub
) {
2853 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2857 for (i
= 0; i
< num_planes
; i
++) {
2858 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2859 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
2860 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
2862 if (!r
->handles
[i
]) {
2863 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2867 if ((uint64_t) width
* cpp
> UINT_MAX
)
2870 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
2873 if (r
->pitches
[i
] < width
* cpp
) {
2874 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2883 * drm_mode_addfb2 - add an FB to the graphics configuration
2884 * @dev: drm device for the ioctl
2885 * @data: data pointer for the ioctl
2886 * @file_priv: drm file for the ioctl call
2888 * Add a new FB to the specified CRTC, given a user request with format. This is
2889 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2890 * and uses fourcc codes as pixel format specifiers.
2892 * Called by the user via ioctl.
2895 * Zero on success, errno on failure.
2897 int drm_mode_addfb2(struct drm_device
*dev
,
2898 void *data
, struct drm_file
*file_priv
)
2900 struct drm_mode_fb_cmd2
*r
= data
;
2901 struct drm_mode_config
*config
= &dev
->mode_config
;
2902 struct drm_framebuffer
*fb
;
2905 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2908 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
2909 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
2913 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2914 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2915 r
->width
, config
->min_width
, config
->max_width
);
2918 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2919 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2920 r
->height
, config
->min_height
, config
->max_height
);
2924 ret
= framebuffer_check(r
);
2928 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2930 DRM_DEBUG_KMS("could not create framebuffer\n");
2934 mutex_lock(&file_priv
->fbs_lock
);
2935 r
->fb_id
= fb
->base
.id
;
2936 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2937 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2938 mutex_unlock(&file_priv
->fbs_lock
);
2945 * drm_mode_rmfb - remove an FB from the configuration
2946 * @dev: drm device for the ioctl
2947 * @data: data pointer for the ioctl
2948 * @file_priv: drm file for the ioctl call
2950 * Remove the FB specified by the user.
2952 * Called by the user via ioctl.
2955 * Zero on success, errno on failure.
2957 int drm_mode_rmfb(struct drm_device
*dev
,
2958 void *data
, struct drm_file
*file_priv
)
2960 struct drm_framebuffer
*fb
= NULL
;
2961 struct drm_framebuffer
*fbl
= NULL
;
2962 uint32_t *id
= data
;
2965 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2968 mutex_lock(&file_priv
->fbs_lock
);
2969 mutex_lock(&dev
->mode_config
.fb_lock
);
2970 fb
= __drm_framebuffer_lookup(dev
, *id
);
2974 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2980 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2981 __drm_framebuffer_unregister(dev
, fb
);
2983 list_del_init(&fb
->filp_head
);
2984 mutex_unlock(&dev
->mode_config
.fb_lock
);
2985 mutex_unlock(&file_priv
->fbs_lock
);
2987 drm_framebuffer_remove(fb
);
2992 mutex_unlock(&dev
->mode_config
.fb_lock
);
2993 mutex_unlock(&file_priv
->fbs_lock
);
2999 * drm_mode_getfb - get FB info
3000 * @dev: drm device for the ioctl
3001 * @data: data pointer for the ioctl
3002 * @file_priv: drm file for the ioctl call
3004 * Lookup the FB given its ID and return info about it.
3006 * Called by the user via ioctl.
3009 * Zero on success, errno on failure.
3011 int drm_mode_getfb(struct drm_device
*dev
,
3012 void *data
, struct drm_file
*file_priv
)
3014 struct drm_mode_fb_cmd
*r
= data
;
3015 struct drm_framebuffer
*fb
;
3018 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3021 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3025 r
->height
= fb
->height
;
3026 r
->width
= fb
->width
;
3027 r
->depth
= fb
->depth
;
3028 r
->bpp
= fb
->bits_per_pixel
;
3029 r
->pitch
= fb
->pitches
[0];
3030 if (fb
->funcs
->create_handle
) {
3031 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3032 drm_is_control_client(file_priv
)) {
3033 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3036 /* GET_FB() is an unprivileged ioctl so we must not
3037 * return a buffer-handle to non-master processes! For
3038 * backwards-compatibility reasons, we cannot make
3039 * GET_FB() privileged, so just return an invalid handle
3040 * for non-masters. */
3048 drm_framebuffer_unreference(fb
);
3054 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3055 * @dev: drm device for the ioctl
3056 * @data: data pointer for the ioctl
3057 * @file_priv: drm file for the ioctl call
3059 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3060 * rectangle list. Generic userspace which does frontbuffer rendering must call
3061 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3062 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3064 * Modesetting drivers which always update the frontbuffer do not need to
3065 * implement the corresponding ->dirty framebuffer callback.
3067 * Called by the user via ioctl.
3070 * Zero on success, errno on failure.
3072 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3073 void *data
, struct drm_file
*file_priv
)
3075 struct drm_clip_rect __user
*clips_ptr
;
3076 struct drm_clip_rect
*clips
= NULL
;
3077 struct drm_mode_fb_dirty_cmd
*r
= data
;
3078 struct drm_framebuffer
*fb
;
3083 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3086 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3090 num_clips
= r
->num_clips
;
3091 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3093 if (!num_clips
!= !clips_ptr
) {
3098 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3100 /* If userspace annotates copy, clips must come in pairs */
3101 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3106 if (num_clips
&& clips_ptr
) {
3107 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3111 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3117 ret
= copy_from_user(clips
, clips_ptr
,
3118 num_clips
* sizeof(*clips
));
3125 if (fb
->funcs
->dirty
) {
3126 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3135 drm_framebuffer_unreference(fb
);
3142 * drm_fb_release - remove and free the FBs on this file
3143 * @priv: drm file for the ioctl
3145 * Destroy all the FBs associated with @filp.
3147 * Called by the user via ioctl.
3150 * Zero on success, errno on failure.
3152 void drm_fb_release(struct drm_file
*priv
)
3154 struct drm_device
*dev
= priv
->minor
->dev
;
3155 struct drm_framebuffer
*fb
, *tfb
;
3157 mutex_lock(&priv
->fbs_lock
);
3158 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3160 mutex_lock(&dev
->mode_config
.fb_lock
);
3161 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3162 __drm_framebuffer_unregister(dev
, fb
);
3163 mutex_unlock(&dev
->mode_config
.fb_lock
);
3165 list_del_init(&fb
->filp_head
);
3167 /* This will also drop the fpriv->fbs reference. */
3168 drm_framebuffer_remove(fb
);
3170 mutex_unlock(&priv
->fbs_lock
);
3174 * drm_property_create - create a new property type
3176 * @flags: flags specifying the property type
3177 * @name: name of the property
3178 * @num_values: number of pre-defined values
3180 * This creates a new generic drm property which can then be attached to a drm
3181 * object with drm_object_attach_property. The returned property object must be
3182 * freed with drm_property_destroy.
3185 * A pointer to the newly created property on success, NULL on failure.
3187 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3188 const char *name
, int num_values
)
3190 struct drm_property
*property
= NULL
;
3193 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3197 property
->dev
= dev
;
3200 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3201 if (!property
->values
)
3205 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3209 property
->flags
= flags
;
3210 property
->num_values
= num_values
;
3211 INIT_LIST_HEAD(&property
->enum_blob_list
);
3214 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3215 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3218 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3220 WARN_ON(!drm_property_type_valid(property
));
3224 kfree(property
->values
);
3228 EXPORT_SYMBOL(drm_property_create
);
3231 * drm_property_create - create a new enumeration property type
3233 * @flags: flags specifying the property type
3234 * @name: name of the property
3235 * @props: enumeration lists with property values
3236 * @num_values: number of pre-defined values
3238 * This creates a new generic drm property which can then be attached to a drm
3239 * object with drm_object_attach_property. The returned property object must be
3240 * freed with drm_property_destroy.
3242 * Userspace is only allowed to set one of the predefined values for enumeration
3246 * A pointer to the newly created property on success, NULL on failure.
3248 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3250 const struct drm_prop_enum_list
*props
,
3253 struct drm_property
*property
;
3256 flags
|= DRM_MODE_PROP_ENUM
;
3258 property
= drm_property_create(dev
, flags
, name
, num_values
);
3262 for (i
= 0; i
< num_values
; i
++) {
3263 ret
= drm_property_add_enum(property
, i
,
3267 drm_property_destroy(dev
, property
);
3274 EXPORT_SYMBOL(drm_property_create_enum
);
3277 * drm_property_create - create a new bitmask property type
3279 * @flags: flags specifying the property type
3280 * @name: name of the property
3281 * @props: enumeration lists with property bitflags
3282 * @num_values: number of pre-defined values
3284 * This creates a new generic drm property which can then be attached to a drm
3285 * object with drm_object_attach_property. The returned property object must be
3286 * freed with drm_property_destroy.
3288 * Compared to plain enumeration properties userspace is allowed to set any
3289 * or'ed together combination of the predefined property bitflag values
3292 * A pointer to the newly created property on success, NULL on failure.
3294 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3295 int flags
, const char *name
,
3296 const struct drm_prop_enum_list
*props
,
3299 struct drm_property
*property
;
3302 flags
|= DRM_MODE_PROP_BITMASK
;
3304 property
= drm_property_create(dev
, flags
, name
, num_values
);
3308 for (i
= 0; i
< num_values
; i
++) {
3309 ret
= drm_property_add_enum(property
, i
,
3313 drm_property_destroy(dev
, property
);
3320 EXPORT_SYMBOL(drm_property_create_bitmask
);
3322 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3323 int flags
, const char *name
,
3324 uint64_t min
, uint64_t max
)
3326 struct drm_property
*property
;
3328 property
= drm_property_create(dev
, flags
, name
, 2);
3332 property
->values
[0] = min
;
3333 property
->values
[1] = max
;
3339 * drm_property_create - create a new ranged property type
3341 * @flags: flags specifying the property type
3342 * @name: name of the property
3343 * @min: minimum value of the property
3344 * @max: maximum value of the property
3346 * This creates a new generic drm property which can then be attached to a drm
3347 * object with drm_object_attach_property. The returned property object must be
3348 * freed with drm_property_destroy.
3350 * Userspace is allowed to set any interger value in the (min, max) range
3354 * A pointer to the newly created property on success, NULL on failure.
3356 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3358 uint64_t min
, uint64_t max
)
3360 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3363 EXPORT_SYMBOL(drm_property_create_range
);
3365 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3366 int flags
, const char *name
,
3367 int64_t min
, int64_t max
)
3369 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3370 name
, I642U64(min
), I642U64(max
));
3372 EXPORT_SYMBOL(drm_property_create_signed_range
);
3374 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3375 int flags
, const char *name
, uint32_t type
)
3377 struct drm_property
*property
;
3379 flags
|= DRM_MODE_PROP_OBJECT
;
3381 property
= drm_property_create(dev
, flags
, name
, 1);
3385 property
->values
[0] = type
;
3389 EXPORT_SYMBOL(drm_property_create_object
);
3392 * drm_property_add_enum - add a possible value to an enumeration property
3393 * @property: enumeration property to change
3394 * @index: index of the new enumeration
3395 * @value: value of the new enumeration
3396 * @name: symbolic name of the new enumeration
3398 * This functions adds enumerations to a property.
3400 * It's use is deprecated, drivers should use one of the more specific helpers
3401 * to directly create the property with all enumerations already attached.
3404 * Zero on success, error code on failure.
3406 int drm_property_add_enum(struct drm_property
*property
, int index
,
3407 uint64_t value
, const char *name
)
3409 struct drm_property_enum
*prop_enum
;
3411 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3412 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3416 * Bitmask enum properties have the additional constraint of values
3419 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3423 if (!list_empty(&property
->enum_blob_list
)) {
3424 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3425 if (prop_enum
->value
== value
) {
3426 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3427 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3433 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3437 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3438 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3439 prop_enum
->value
= value
;
3441 property
->values
[index
] = value
;
3442 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3445 EXPORT_SYMBOL(drm_property_add_enum
);
3448 * drm_property_destroy - destroy a drm property
3450 * @property: property to destry
3452 * This function frees a property including any attached resources like
3453 * enumeration values.
3455 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3457 struct drm_property_enum
*prop_enum
, *pt
;
3459 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3460 list_del(&prop_enum
->head
);
3464 if (property
->num_values
)
3465 kfree(property
->values
);
3466 drm_mode_object_put(dev
, &property
->base
);
3467 list_del(&property
->head
);
3470 EXPORT_SYMBOL(drm_property_destroy
);
3473 * drm_object_attach_property - attach a property to a modeset object
3474 * @obj: drm modeset object
3475 * @property: property to attach
3476 * @init_val: initial value of the property
3478 * This attaches the given property to the modeset object with the given initial
3479 * value. Currently this function cannot fail since the properties are stored in
3480 * a statically sized array.
3482 void drm_object_attach_property(struct drm_mode_object
*obj
,
3483 struct drm_property
*property
,
3486 int count
= obj
->properties
->count
;
3488 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3489 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3490 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3491 "you see this message on the same object type.\n",
3496 obj
->properties
->ids
[count
] = property
->base
.id
;
3497 obj
->properties
->values
[count
] = init_val
;
3498 obj
->properties
->count
++;
3500 EXPORT_SYMBOL(drm_object_attach_property
);
3503 * drm_object_property_set_value - set the value of a property
3504 * @obj: drm mode object to set property value for
3505 * @property: property to set
3506 * @val: value the property should be set to
3508 * This functions sets a given property on a given object. This function only
3509 * changes the software state of the property, it does not call into the
3510 * driver's ->set_property callback.
3513 * Zero on success, error code on failure.
3515 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3516 struct drm_property
*property
, uint64_t val
)
3520 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3521 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3522 obj
->properties
->values
[i
] = val
;
3529 EXPORT_SYMBOL(drm_object_property_set_value
);
3532 * drm_object_property_get_value - retrieve the value of a property
3533 * @obj: drm mode object to get property value from
3534 * @property: property to retrieve
3535 * @val: storage for the property value
3537 * This function retrieves the softare state of the given property for the given
3538 * property. Since there is no driver callback to retrieve the current property
3539 * value this might be out of sync with the hardware, depending upon the driver
3543 * Zero on success, error code on failure.
3545 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3546 struct drm_property
*property
, uint64_t *val
)
3550 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3551 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3552 *val
= obj
->properties
->values
[i
];
3559 EXPORT_SYMBOL(drm_object_property_get_value
);
3562 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3565 * @file_priv: DRM file info
3567 * This function retrieves the current value for an connectors's property.
3569 * Called by the user via ioctl.
3572 * Zero on success, errno on failure.
3574 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3575 void *data
, struct drm_file
*file_priv
)
3577 struct drm_mode_get_property
*out_resp
= data
;
3578 struct drm_property
*property
;
3581 int value_count
= 0;
3584 struct drm_property_enum
*prop_enum
;
3585 struct drm_mode_property_enum __user
*enum_ptr
;
3586 struct drm_property_blob
*prop_blob
;
3587 uint32_t __user
*blob_id_ptr
;
3588 uint64_t __user
*values_ptr
;
3589 uint32_t __user
*blob_length_ptr
;
3591 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3594 drm_modeset_lock_all(dev
);
3595 property
= drm_property_find(dev
, out_resp
->prop_id
);
3601 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3602 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3603 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3605 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3606 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3610 value_count
= property
->num_values
;
3612 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3613 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3614 out_resp
->flags
= property
->flags
;
3616 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3617 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3618 for (i
= 0; i
< value_count
; i
++) {
3619 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3625 out_resp
->count_values
= value_count
;
3627 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3628 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3629 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3631 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3632 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3634 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3639 if (copy_to_user(&enum_ptr
[copied
].name
,
3640 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3647 out_resp
->count_enum_blobs
= enum_count
;
3650 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3651 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3653 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3654 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3656 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3657 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3662 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3670 out_resp
->count_enum_blobs
= blob_count
;
3673 drm_modeset_unlock_all(dev
);
3677 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3680 struct drm_property_blob
*blob
;
3683 if (!length
|| !data
)
3686 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3690 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3696 blob
->length
= length
;
3698 memcpy(blob
->data
, data
, length
);
3700 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3704 static void drm_property_destroy_blob(struct drm_device
*dev
,
3705 struct drm_property_blob
*blob
)
3707 drm_mode_object_put(dev
, &blob
->base
);
3708 list_del(&blob
->head
);
3713 * drm_mode_getblob_ioctl - get the contents of a blob property value
3716 * @file_priv: DRM file info
3718 * This function retrieves the contents of a blob property. The value stored in
3719 * an object's blob property is just a normal modeset object id.
3721 * Called by the user via ioctl.
3724 * Zero on success, errno on failure.
3726 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3727 void *data
, struct drm_file
*file_priv
)
3729 struct drm_mode_get_blob
*out_resp
= data
;
3730 struct drm_property_blob
*blob
;
3732 void __user
*blob_ptr
;
3734 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3737 drm_modeset_lock_all(dev
);
3738 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3744 if (out_resp
->length
== blob
->length
) {
3745 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3746 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3751 out_resp
->length
= blob
->length
;
3754 drm_modeset_unlock_all(dev
);
3759 * drm_mode_connector_update_edid_property - update the edid property of a connector
3760 * @connector: drm connector
3761 * @edid: new value of the edid property
3763 * This function creates a new blob modeset object and assigns its id to the
3764 * connector's edid property.
3767 * Zero on success, errno on failure.
3769 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3772 struct drm_device
*dev
= connector
->dev
;
3775 /* ignore requests to set edid when overridden */
3776 if (connector
->override_edid
)
3779 if (connector
->edid_blob_ptr
)
3780 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3782 /* Delete edid, when there is none. */
3784 connector
->edid_blob_ptr
= NULL
;
3785 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3789 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3790 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3792 if (!connector
->edid_blob_ptr
)
3795 ret
= drm_object_property_set_value(&connector
->base
,
3796 dev
->mode_config
.edid_property
,
3797 connector
->edid_blob_ptr
->base
.id
);
3801 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3803 static bool drm_property_change_is_valid(struct drm_property
*property
,
3806 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3809 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3810 if (value
< property
->values
[0] || value
> property
->values
[1])
3813 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3814 int64_t svalue
= U642I64(value
);
3815 if (svalue
< U642I64(property
->values
[0]) ||
3816 svalue
> U642I64(property
->values
[1]))
3819 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3821 uint64_t valid_mask
= 0;
3822 for (i
= 0; i
< property
->num_values
; i
++)
3823 valid_mask
|= (1ULL << property
->values
[i
]);
3824 return !(value
& ~valid_mask
);
3825 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3826 /* Only the driver knows */
3828 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3829 struct drm_mode_object
*obj
;
3830 /* a zero value for an object property translates to null: */
3834 * NOTE: use _object_find() directly to bypass restriction on
3835 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3836 * object this could race against object finalization, so it
3837 * simply tells us that the object *was* valid. Which is good
3840 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
3844 for (i
= 0; i
< property
->num_values
; i
++)
3845 if (property
->values
[i
] == value
)
3852 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3855 * @file_priv: DRM file info
3857 * This function sets the current value for a connectors's property. It also
3858 * calls into a driver's ->set_property callback to update the hardware state
3860 * Called by the user via ioctl.
3863 * Zero on success, errno on failure.
3865 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3866 void *data
, struct drm_file
*file_priv
)
3868 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3869 struct drm_mode_obj_set_property obj_set_prop
= {
3870 .value
= conn_set_prop
->value
,
3871 .prop_id
= conn_set_prop
->prop_id
,
3872 .obj_id
= conn_set_prop
->connector_id
,
3873 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3876 /* It does all the locking and checking we need */
3877 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3880 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3881 struct drm_property
*property
,
3885 struct drm_connector
*connector
= obj_to_connector(obj
);
3887 /* Do DPMS ourselves */
3888 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3889 if (connector
->funcs
->dpms
)
3890 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3892 } else if (connector
->funcs
->set_property
)
3893 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3895 /* store the property value if successful */
3897 drm_object_property_set_value(&connector
->base
, property
, value
);
3901 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3902 struct drm_property
*property
,
3906 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3908 if (crtc
->funcs
->set_property
)
3909 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3911 drm_object_property_set_value(obj
, property
, value
);
3916 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3917 struct drm_property
*property
,
3921 struct drm_plane
*plane
= obj_to_plane(obj
);
3923 if (plane
->funcs
->set_property
)
3924 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3926 drm_object_property_set_value(obj
, property
, value
);
3932 * drm_mode_getproperty_ioctl - get the current value of a object's property
3935 * @file_priv: DRM file info
3937 * This function retrieves the current value for an object's property. Compared
3938 * to the connector specific ioctl this one is extended to also work on crtc and
3941 * Called by the user via ioctl.
3944 * Zero on success, errno on failure.
3946 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3947 struct drm_file
*file_priv
)
3949 struct drm_mode_obj_get_properties
*arg
= data
;
3950 struct drm_mode_object
*obj
;
3954 int props_count
= 0;
3955 uint32_t __user
*props_ptr
;
3956 uint64_t __user
*prop_values_ptr
;
3958 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3961 drm_modeset_lock_all(dev
);
3963 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3968 if (!obj
->properties
) {
3973 props_count
= obj
->properties
->count
;
3975 /* This ioctl is called twice, once to determine how much space is
3976 * needed, and the 2nd time to fill it. */
3977 if ((arg
->count_props
>= props_count
) && props_count
) {
3979 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3980 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3981 (arg
->prop_values_ptr
);
3982 for (i
= 0; i
< props_count
; i
++) {
3983 if (put_user(obj
->properties
->ids
[i
],
3984 props_ptr
+ copied
)) {
3988 if (put_user(obj
->properties
->values
[i
],
3989 prop_values_ptr
+ copied
)) {
3996 arg
->count_props
= props_count
;
3998 drm_modeset_unlock_all(dev
);
4003 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4006 * @file_priv: DRM file info
4008 * This function sets the current value for an object's property. It also calls
4009 * into a driver's ->set_property callback to update the hardware state.
4010 * Compared to the connector specific ioctl this one is extended to also work on
4011 * crtc and plane objects.
4013 * Called by the user via ioctl.
4016 * Zero on success, errno on failure.
4018 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4019 struct drm_file
*file_priv
)
4021 struct drm_mode_obj_set_property
*arg
= data
;
4022 struct drm_mode_object
*arg_obj
;
4023 struct drm_mode_object
*prop_obj
;
4024 struct drm_property
*property
;
4028 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4031 drm_modeset_lock_all(dev
);
4033 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4038 if (!arg_obj
->properties
)
4041 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4042 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4045 if (i
== arg_obj
->properties
->count
)
4048 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4049 DRM_MODE_OBJECT_PROPERTY
);
4054 property
= obj_to_property(prop_obj
);
4056 if (!drm_property_change_is_valid(property
, arg
->value
))
4059 switch (arg_obj
->type
) {
4060 case DRM_MODE_OBJECT_CONNECTOR
:
4061 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4064 case DRM_MODE_OBJECT_CRTC
:
4065 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4067 case DRM_MODE_OBJECT_PLANE
:
4068 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4073 drm_modeset_unlock_all(dev
);
4078 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4079 * @connector: connector to attach
4080 * @encoder: encoder to attach @connector to
4082 * This function links up a connector to an encoder. Note that the routing
4083 * restrictions between encoders and crtcs are exposed to userspace through the
4084 * possible_clones and possible_crtcs bitmasks.
4087 * Zero on success, errno on failure.
4089 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4090 struct drm_encoder
*encoder
)
4094 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4095 if (connector
->encoder_ids
[i
] == 0) {
4096 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4102 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4105 * drm_mode_crtc_set_gamma_size - set the gamma table size
4106 * @crtc: CRTC to set the gamma table size for
4107 * @gamma_size: size of the gamma table
4109 * Drivers which support gamma tables should set this to the supported gamma
4110 * table size when initializing the CRTC. Currently the drm core only supports a
4111 * fixed gamma table size.
4114 * Zero on success, errno on failure.
4116 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4119 crtc
->gamma_size
= gamma_size
;
4121 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4122 if (!crtc
->gamma_store
) {
4123 crtc
->gamma_size
= 0;
4129 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4132 * drm_mode_gamma_set_ioctl - set the gamma table
4135 * @file_priv: DRM file info
4137 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4138 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4140 * Called by the user via ioctl.
4143 * Zero on success, errno on failure.
4145 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4146 void *data
, struct drm_file
*file_priv
)
4148 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4149 struct drm_crtc
*crtc
;
4150 void *r_base
, *g_base
, *b_base
;
4154 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4157 drm_modeset_lock_all(dev
);
4158 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4164 if (crtc
->funcs
->gamma_set
== NULL
) {
4169 /* memcpy into gamma store */
4170 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4175 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4176 r_base
= crtc
->gamma_store
;
4177 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4182 g_base
= r_base
+ size
;
4183 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4188 b_base
= g_base
+ size
;
4189 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4194 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4197 drm_modeset_unlock_all(dev
);
4203 * drm_mode_gamma_get_ioctl - get the gamma table
4206 * @file_priv: DRM file info
4208 * Copy the current gamma table into the storage provided. This also provides
4209 * the gamma table size the driver expects, which can be used to size the
4210 * allocated storage.
4212 * Called by the user via ioctl.
4215 * Zero on success, errno on failure.
4217 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4218 void *data
, struct drm_file
*file_priv
)
4220 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4221 struct drm_crtc
*crtc
;
4222 void *r_base
, *g_base
, *b_base
;
4226 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4229 drm_modeset_lock_all(dev
);
4230 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4236 /* memcpy into gamma store */
4237 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4242 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4243 r_base
= crtc
->gamma_store
;
4244 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4249 g_base
= r_base
+ size
;
4250 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4255 b_base
= g_base
+ size
;
4256 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4261 drm_modeset_unlock_all(dev
);
4266 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4269 * @file_priv: DRM file info
4271 * This schedules an asynchronous update on a given CRTC, called page flip.
4272 * Optionally a drm event is generated to signal the completion of the event.
4273 * Generic drivers cannot assume that a pageflip with changed framebuffer
4274 * properties (including driver specific metadata like tiling layout) will work,
4275 * but some drivers support e.g. pixel format changes through the pageflip
4278 * Called by the user via ioctl.
4281 * Zero on success, errno on failure.
4283 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4284 void *data
, struct drm_file
*file_priv
)
4286 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4287 struct drm_crtc
*crtc
;
4288 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4289 struct drm_pending_vblank_event
*e
= NULL
;
4290 unsigned long flags
;
4293 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4294 page_flip
->reserved
!= 0)
4297 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4300 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4304 drm_modeset_lock(&crtc
->mutex
, NULL
);
4305 if (crtc
->primary
->fb
== NULL
) {
4306 /* The framebuffer is currently unbound, presumably
4307 * due to a hotplug event, that userspace has not
4314 if (crtc
->funcs
->page_flip
== NULL
)
4317 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4323 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4327 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4328 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4333 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4335 spin_lock_irqsave(&dev
->event_lock
, flags
);
4336 if (file_priv
->event_space
< sizeof e
->event
) {
4337 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4340 file_priv
->event_space
-= sizeof e
->event
;
4341 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4343 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4345 spin_lock_irqsave(&dev
->event_lock
, flags
);
4346 file_priv
->event_space
+= sizeof e
->event
;
4347 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4351 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4352 e
->event
.base
.length
= sizeof e
->event
;
4353 e
->event
.user_data
= page_flip
->user_data
;
4354 e
->base
.event
= &e
->event
.base
;
4355 e
->base
.file_priv
= file_priv
;
4357 (void (*) (struct drm_pending_event
*)) kfree
;
4360 old_fb
= crtc
->primary
->fb
;
4361 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4363 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4364 spin_lock_irqsave(&dev
->event_lock
, flags
);
4365 file_priv
->event_space
+= sizeof e
->event
;
4366 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4369 /* Keep the old fb, don't unref it. */
4373 * Warn if the driver hasn't properly updated the crtc->fb
4374 * field to reflect that the new framebuffer is now used.
4375 * Failing to do so will screw with the reference counting
4378 WARN_ON(crtc
->primary
->fb
!= fb
);
4379 /* Unref only the old framebuffer. */
4385 drm_framebuffer_unreference(fb
);
4387 drm_framebuffer_unreference(old_fb
);
4388 drm_modeset_unlock(&crtc
->mutex
);
4394 * drm_mode_config_reset - call ->reset callbacks
4397 * This functions calls all the crtc's, encoder's and connector's ->reset
4398 * callback. Drivers can use this in e.g. their driver load or resume code to
4399 * reset hardware and software state.
4401 void drm_mode_config_reset(struct drm_device
*dev
)
4403 struct drm_crtc
*crtc
;
4404 struct drm_encoder
*encoder
;
4405 struct drm_connector
*connector
;
4407 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4408 if (crtc
->funcs
->reset
)
4409 crtc
->funcs
->reset(crtc
);
4411 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4412 if (encoder
->funcs
->reset
)
4413 encoder
->funcs
->reset(encoder
);
4415 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4416 connector
->status
= connector_status_unknown
;
4418 if (connector
->funcs
->reset
)
4419 connector
->funcs
->reset(connector
);
4422 EXPORT_SYMBOL(drm_mode_config_reset
);
4425 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4428 * @file_priv: DRM file info
4430 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4431 * TTM or something else entirely) and returns the resulting buffer handle. This
4432 * handle can then be wrapped up into a framebuffer modeset object.
4434 * Note that userspace is not allowed to use such objects for render
4435 * acceleration - drivers must create their own private ioctls for such a use
4438 * Called by the user via ioctl.
4441 * Zero on success, errno on failure.
4443 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4444 void *data
, struct drm_file
*file_priv
)
4446 struct drm_mode_create_dumb
*args
= data
;
4447 u32 cpp
, stride
, size
;
4449 if (!dev
->driver
->dumb_create
)
4451 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4454 /* overflow checks for 32bit size calculations */
4455 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4456 if (cpp
> 0xffffffffU
/ args
->width
)
4458 stride
= cpp
* args
->width
;
4459 if (args
->height
> 0xffffffffU
/ stride
)
4462 /* test for wrap-around */
4463 size
= args
->height
* stride
;
4464 if (PAGE_ALIGN(size
) == 0)
4467 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4471 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4474 * @file_priv: DRM file info
4476 * Allocate an offset in the drm device node's address space to be able to
4477 * memory map a dumb buffer.
4479 * Called by the user via ioctl.
4482 * Zero on success, errno on failure.
4484 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4485 void *data
, struct drm_file
*file_priv
)
4487 struct drm_mode_map_dumb
*args
= data
;
4489 /* call driver ioctl to get mmap offset */
4490 if (!dev
->driver
->dumb_map_offset
)
4493 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4497 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4500 * @file_priv: DRM file info
4502 * This destroys the userspace handle for the given dumb backing storage buffer.
4503 * Since buffer objects must be reference counted in the kernel a buffer object
4504 * won't be immediately freed if a framebuffer modeset object still uses it.
4506 * Called by the user via ioctl.
4509 * Zero on success, errno on failure.
4511 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4512 void *data
, struct drm_file
*file_priv
)
4514 struct drm_mode_destroy_dumb
*args
= data
;
4516 if (!dev
->driver
->dumb_destroy
)
4519 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4523 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4524 * @format: pixel format (DRM_FORMAT_*)
4525 * @depth: storage for the depth value
4526 * @bpp: storage for the bpp value
4528 * This only supports RGB formats here for compat with code that doesn't use
4529 * pixel formats directly yet.
4531 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4536 case DRM_FORMAT_RGB332
:
4537 case DRM_FORMAT_BGR233
:
4541 case DRM_FORMAT_XRGB1555
:
4542 case DRM_FORMAT_XBGR1555
:
4543 case DRM_FORMAT_RGBX5551
:
4544 case DRM_FORMAT_BGRX5551
:
4545 case DRM_FORMAT_ARGB1555
:
4546 case DRM_FORMAT_ABGR1555
:
4547 case DRM_FORMAT_RGBA5551
:
4548 case DRM_FORMAT_BGRA5551
:
4552 case DRM_FORMAT_RGB565
:
4553 case DRM_FORMAT_BGR565
:
4557 case DRM_FORMAT_RGB888
:
4558 case DRM_FORMAT_BGR888
:
4562 case DRM_FORMAT_XRGB8888
:
4563 case DRM_FORMAT_XBGR8888
:
4564 case DRM_FORMAT_RGBX8888
:
4565 case DRM_FORMAT_BGRX8888
:
4569 case DRM_FORMAT_XRGB2101010
:
4570 case DRM_FORMAT_XBGR2101010
:
4571 case DRM_FORMAT_RGBX1010102
:
4572 case DRM_FORMAT_BGRX1010102
:
4573 case DRM_FORMAT_ARGB2101010
:
4574 case DRM_FORMAT_ABGR2101010
:
4575 case DRM_FORMAT_RGBA1010102
:
4576 case DRM_FORMAT_BGRA1010102
:
4580 case DRM_FORMAT_ARGB8888
:
4581 case DRM_FORMAT_ABGR8888
:
4582 case DRM_FORMAT_RGBA8888
:
4583 case DRM_FORMAT_BGRA8888
:
4588 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4589 drm_get_format_name(format
));
4595 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4598 * drm_format_num_planes - get the number of planes for format
4599 * @format: pixel format (DRM_FORMAT_*)
4602 * The number of planes used by the specified pixel format.
4604 int drm_format_num_planes(uint32_t format
)
4607 case DRM_FORMAT_YUV410
:
4608 case DRM_FORMAT_YVU410
:
4609 case DRM_FORMAT_YUV411
:
4610 case DRM_FORMAT_YVU411
:
4611 case DRM_FORMAT_YUV420
:
4612 case DRM_FORMAT_YVU420
:
4613 case DRM_FORMAT_YUV422
:
4614 case DRM_FORMAT_YVU422
:
4615 case DRM_FORMAT_YUV444
:
4616 case DRM_FORMAT_YVU444
:
4618 case DRM_FORMAT_NV12
:
4619 case DRM_FORMAT_NV21
:
4620 case DRM_FORMAT_NV16
:
4621 case DRM_FORMAT_NV61
:
4622 case DRM_FORMAT_NV24
:
4623 case DRM_FORMAT_NV42
:
4629 EXPORT_SYMBOL(drm_format_num_planes
);
4632 * drm_format_plane_cpp - determine the bytes per pixel value
4633 * @format: pixel format (DRM_FORMAT_*)
4634 * @plane: plane index
4637 * The bytes per pixel value for the specified plane.
4639 int drm_format_plane_cpp(uint32_t format
, int plane
)
4644 if (plane
>= drm_format_num_planes(format
))
4648 case DRM_FORMAT_YUYV
:
4649 case DRM_FORMAT_YVYU
:
4650 case DRM_FORMAT_UYVY
:
4651 case DRM_FORMAT_VYUY
:
4653 case DRM_FORMAT_NV12
:
4654 case DRM_FORMAT_NV21
:
4655 case DRM_FORMAT_NV16
:
4656 case DRM_FORMAT_NV61
:
4657 case DRM_FORMAT_NV24
:
4658 case DRM_FORMAT_NV42
:
4659 return plane
? 2 : 1;
4660 case DRM_FORMAT_YUV410
:
4661 case DRM_FORMAT_YVU410
:
4662 case DRM_FORMAT_YUV411
:
4663 case DRM_FORMAT_YVU411
:
4664 case DRM_FORMAT_YUV420
:
4665 case DRM_FORMAT_YVU420
:
4666 case DRM_FORMAT_YUV422
:
4667 case DRM_FORMAT_YVU422
:
4668 case DRM_FORMAT_YUV444
:
4669 case DRM_FORMAT_YVU444
:
4672 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4676 EXPORT_SYMBOL(drm_format_plane_cpp
);
4679 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4680 * @format: pixel format (DRM_FORMAT_*)
4683 * The horizontal chroma subsampling factor for the
4684 * specified pixel format.
4686 int drm_format_horz_chroma_subsampling(uint32_t format
)
4689 case DRM_FORMAT_YUV411
:
4690 case DRM_FORMAT_YVU411
:
4691 case DRM_FORMAT_YUV410
:
4692 case DRM_FORMAT_YVU410
:
4694 case DRM_FORMAT_YUYV
:
4695 case DRM_FORMAT_YVYU
:
4696 case DRM_FORMAT_UYVY
:
4697 case DRM_FORMAT_VYUY
:
4698 case DRM_FORMAT_NV12
:
4699 case DRM_FORMAT_NV21
:
4700 case DRM_FORMAT_NV16
:
4701 case DRM_FORMAT_NV61
:
4702 case DRM_FORMAT_YUV422
:
4703 case DRM_FORMAT_YVU422
:
4704 case DRM_FORMAT_YUV420
:
4705 case DRM_FORMAT_YVU420
:
4711 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4714 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4715 * @format: pixel format (DRM_FORMAT_*)
4718 * The vertical chroma subsampling factor for the
4719 * specified pixel format.
4721 int drm_format_vert_chroma_subsampling(uint32_t format
)
4724 case DRM_FORMAT_YUV410
:
4725 case DRM_FORMAT_YVU410
:
4727 case DRM_FORMAT_YUV420
:
4728 case DRM_FORMAT_YVU420
:
4729 case DRM_FORMAT_NV12
:
4730 case DRM_FORMAT_NV21
:
4736 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4739 * drm_mode_config_init - initialize DRM mode_configuration structure
4742 * Initialize @dev's mode_config structure, used for tracking the graphics
4743 * configuration of @dev.
4745 * Since this initializes the modeset locks, no locking is possible. Which is no
4746 * problem, since this should happen single threaded at init time. It is the
4747 * driver's problem to ensure this guarantee.
4750 void drm_mode_config_init(struct drm_device
*dev
)
4752 mutex_init(&dev
->mode_config
.mutex
);
4753 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
4754 mutex_init(&dev
->mode_config
.idr_mutex
);
4755 mutex_init(&dev
->mode_config
.fb_lock
);
4756 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4757 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4758 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4759 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4760 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4761 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4762 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4763 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4764 idr_init(&dev
->mode_config
.crtc_idr
);
4766 drm_modeset_lock_all(dev
);
4767 drm_mode_create_standard_connector_properties(dev
);
4768 drm_mode_create_standard_plane_properties(dev
);
4769 drm_modeset_unlock_all(dev
);
4771 /* Just to be sure */
4772 dev
->mode_config
.num_fb
= 0;
4773 dev
->mode_config
.num_connector
= 0;
4774 dev
->mode_config
.num_crtc
= 0;
4775 dev
->mode_config
.num_encoder
= 0;
4776 dev
->mode_config
.num_overlay_plane
= 0;
4777 dev
->mode_config
.num_total_plane
= 0;
4779 EXPORT_SYMBOL(drm_mode_config_init
);
4782 * drm_mode_config_cleanup - free up DRM mode_config info
4785 * Free up all the connectors and CRTCs associated with this DRM device, then
4786 * free up the framebuffers and associated buffer objects.
4788 * Note that since this /should/ happen single-threaded at driver/device
4789 * teardown time, no locking is required. It's the driver's job to ensure that
4790 * this guarantee actually holds true.
4792 * FIXME: cleanup any dangling user buffer objects too
4794 void drm_mode_config_cleanup(struct drm_device
*dev
)
4796 struct drm_connector
*connector
, *ot
;
4797 struct drm_crtc
*crtc
, *ct
;
4798 struct drm_encoder
*encoder
, *enct
;
4799 struct drm_bridge
*bridge
, *brt
;
4800 struct drm_framebuffer
*fb
, *fbt
;
4801 struct drm_property
*property
, *pt
;
4802 struct drm_property_blob
*blob
, *bt
;
4803 struct drm_plane
*plane
, *plt
;
4805 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4807 encoder
->funcs
->destroy(encoder
);
4810 list_for_each_entry_safe(bridge
, brt
,
4811 &dev
->mode_config
.bridge_list
, head
) {
4812 bridge
->funcs
->destroy(bridge
);
4815 list_for_each_entry_safe(connector
, ot
,
4816 &dev
->mode_config
.connector_list
, head
) {
4817 connector
->funcs
->destroy(connector
);
4820 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
4822 drm_property_destroy(dev
, property
);
4825 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
4827 drm_property_destroy_blob(dev
, blob
);
4831 * Single-threaded teardown context, so it's not required to grab the
4832 * fb_lock to protect against concurrent fb_list access. Contrary, it
4833 * would actually deadlock with the drm_framebuffer_cleanup function.
4835 * Also, if there are any framebuffers left, that's a driver leak now,
4836 * so politely WARN about this.
4838 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
4839 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
4840 drm_framebuffer_remove(fb
);
4843 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
4845 plane
->funcs
->destroy(plane
);
4848 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
4849 crtc
->funcs
->destroy(crtc
);
4852 idr_destroy(&dev
->mode_config
.crtc_idr
);
4853 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
4855 EXPORT_SYMBOL(drm_mode_config_cleanup
);