2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
45 struct drm_mode_fb_cmd2
*r
,
46 struct drm_file
*file_priv
);
49 * drm_modeset_lock_all - take all modeset locks
52 * This function takes all modeset locks, suitable where a more fine-grained
53 * scheme isn't (yet) implemented. Locks must be dropped with
54 * drm_modeset_unlock_all.
56 void drm_modeset_lock_all(struct drm_device
*dev
)
58 struct drm_mode_config
*config
= &dev
->mode_config
;
59 struct drm_modeset_acquire_ctx
*ctx
;
62 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
66 mutex_lock(&config
->mutex
);
68 drm_modeset_acquire_init(ctx
, 0);
71 ret
= drm_modeset_lock(&config
->connection_mutex
, ctx
);
74 ret
= drm_modeset_lock_all_crtcs(dev
, ctx
);
78 WARN_ON(config
->acquire_ctx
);
80 /* now we hold the locks, so now that it is safe, stash the
81 * ctx for drm_modeset_unlock_all():
83 config
->acquire_ctx
= ctx
;
85 drm_warn_on_modeset_not_all_locked(dev
);
90 if (ret
== -EDEADLK
) {
91 drm_modeset_backoff(ctx
);
95 EXPORT_SYMBOL(drm_modeset_lock_all
);
98 * drm_modeset_unlock_all - drop all modeset locks
101 * This function drop all modeset locks taken by drm_modeset_lock_all.
103 void drm_modeset_unlock_all(struct drm_device
*dev
)
105 struct drm_mode_config
*config
= &dev
->mode_config
;
106 struct drm_modeset_acquire_ctx
*ctx
= config
->acquire_ctx
;
111 config
->acquire_ctx
= NULL
;
112 drm_modeset_drop_locks(ctx
);
113 drm_modeset_acquire_fini(ctx
);
117 mutex_unlock(&dev
->mode_config
.mutex
);
119 EXPORT_SYMBOL(drm_modeset_unlock_all
);
122 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
125 * Useful as a debug assert.
127 void drm_warn_on_modeset_not_all_locked(struct drm_device
*dev
)
129 struct drm_crtc
*crtc
;
131 /* Locking is currently fubar in the panic handler. */
132 if (oops_in_progress
)
135 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
136 WARN_ON(!drm_modeset_is_locked(&crtc
->mutex
));
138 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
139 WARN_ON(!mutex_is_locked(&dev
->mode_config
.mutex
));
141 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked
);
143 /* Avoid boilerplate. I'm tired of typing. */
144 #define DRM_ENUM_NAME_FN(fnname, list) \
145 const char *fnname(int val) \
148 for (i = 0; i < ARRAY_SIZE(list); i++) { \
149 if (list[i].type == val) \
150 return list[i].name; \
152 return "(unknown)"; \
158 static const struct drm_prop_enum_list drm_dpms_enum_list
[] =
159 { { DRM_MODE_DPMS_ON
, "On" },
160 { DRM_MODE_DPMS_STANDBY
, "Standby" },
161 { DRM_MODE_DPMS_SUSPEND
, "Suspend" },
162 { DRM_MODE_DPMS_OFF
, "Off" }
165 DRM_ENUM_NAME_FN(drm_get_dpms_name
, drm_dpms_enum_list
)
167 static const struct drm_prop_enum_list drm_plane_type_enum_list
[] =
169 { DRM_PLANE_TYPE_OVERLAY
, "Overlay" },
170 { DRM_PLANE_TYPE_PRIMARY
, "Primary" },
171 { DRM_PLANE_TYPE_CURSOR
, "Cursor" },
175 * Optional properties
177 static const struct drm_prop_enum_list drm_scaling_mode_enum_list
[] =
179 { DRM_MODE_SCALE_NONE
, "None" },
180 { DRM_MODE_SCALE_FULLSCREEN
, "Full" },
181 { DRM_MODE_SCALE_CENTER
, "Center" },
182 { DRM_MODE_SCALE_ASPECT
, "Full aspect" },
186 * Non-global properties, but "required" for certain connectors.
188 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list
[] =
190 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
191 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
192 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
195 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name
, drm_dvi_i_select_enum_list
)
197 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list
[] =
199 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
200 { DRM_MODE_SUBCONNECTOR_DVID
, "DVI-D" }, /* DVI-I */
201 { DRM_MODE_SUBCONNECTOR_DVIA
, "DVI-A" }, /* DVI-I */
204 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name
,
205 drm_dvi_i_subconnector_enum_list
)
207 static const struct drm_prop_enum_list drm_tv_select_enum_list
[] =
209 { DRM_MODE_SUBCONNECTOR_Automatic
, "Automatic" }, /* DVI-I and TV-out */
210 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
211 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
212 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
213 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
216 DRM_ENUM_NAME_FN(drm_get_tv_select_name
, drm_tv_select_enum_list
)
218 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list
[] =
220 { DRM_MODE_SUBCONNECTOR_Unknown
, "Unknown" }, /* DVI-I and TV-out */
221 { DRM_MODE_SUBCONNECTOR_Composite
, "Composite" }, /* TV-out */
222 { DRM_MODE_SUBCONNECTOR_SVIDEO
, "SVIDEO" }, /* TV-out */
223 { DRM_MODE_SUBCONNECTOR_Component
, "Component" }, /* TV-out */
224 { DRM_MODE_SUBCONNECTOR_SCART
, "SCART" }, /* TV-out */
227 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name
,
228 drm_tv_subconnector_enum_list
)
230 static const struct drm_prop_enum_list drm_dirty_info_enum_list
[] = {
231 { DRM_MODE_DIRTY_OFF
, "Off" },
232 { DRM_MODE_DIRTY_ON
, "On" },
233 { DRM_MODE_DIRTY_ANNOTATE
, "Annotate" },
236 struct drm_conn_prop_enum_list
{
243 * Connector and encoder types.
245 static struct drm_conn_prop_enum_list drm_connector_enum_list
[] =
246 { { DRM_MODE_CONNECTOR_Unknown
, "Unknown" },
247 { DRM_MODE_CONNECTOR_VGA
, "VGA" },
248 { DRM_MODE_CONNECTOR_DVII
, "DVI-I" },
249 { DRM_MODE_CONNECTOR_DVID
, "DVI-D" },
250 { DRM_MODE_CONNECTOR_DVIA
, "DVI-A" },
251 { DRM_MODE_CONNECTOR_Composite
, "Composite" },
252 { DRM_MODE_CONNECTOR_SVIDEO
, "SVIDEO" },
253 { DRM_MODE_CONNECTOR_LVDS
, "LVDS" },
254 { DRM_MODE_CONNECTOR_Component
, "Component" },
255 { DRM_MODE_CONNECTOR_9PinDIN
, "DIN" },
256 { DRM_MODE_CONNECTOR_DisplayPort
, "DP" },
257 { DRM_MODE_CONNECTOR_HDMIA
, "HDMI-A" },
258 { DRM_MODE_CONNECTOR_HDMIB
, "HDMI-B" },
259 { DRM_MODE_CONNECTOR_TV
, "TV" },
260 { DRM_MODE_CONNECTOR_eDP
, "eDP" },
261 { DRM_MODE_CONNECTOR_VIRTUAL
, "Virtual" },
262 { DRM_MODE_CONNECTOR_DSI
, "DSI" },
265 static const struct drm_prop_enum_list drm_encoder_enum_list
[] =
266 { { DRM_MODE_ENCODER_NONE
, "None" },
267 { DRM_MODE_ENCODER_DAC
, "DAC" },
268 { DRM_MODE_ENCODER_TMDS
, "TMDS" },
269 { DRM_MODE_ENCODER_LVDS
, "LVDS" },
270 { DRM_MODE_ENCODER_TVDAC
, "TV" },
271 { DRM_MODE_ENCODER_VIRTUAL
, "Virtual" },
272 { DRM_MODE_ENCODER_DSI
, "DSI" },
273 { DRM_MODE_ENCODER_DPMST
, "DP MST" },
276 static const struct drm_prop_enum_list drm_subpixel_enum_list
[] =
278 { SubPixelUnknown
, "Unknown" },
279 { SubPixelHorizontalRGB
, "Horizontal RGB" },
280 { SubPixelHorizontalBGR
, "Horizontal BGR" },
281 { SubPixelVerticalRGB
, "Vertical RGB" },
282 { SubPixelVerticalBGR
, "Vertical BGR" },
283 { SubPixelNone
, "None" },
286 void drm_connector_ida_init(void)
290 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
291 ida_init(&drm_connector_enum_list
[i
].ida
);
294 void drm_connector_ida_destroy(void)
298 for (i
= 0; i
< ARRAY_SIZE(drm_connector_enum_list
); i
++)
299 ida_destroy(&drm_connector_enum_list
[i
].ida
);
303 * drm_get_connector_status_name - return a string for connector status
304 * @status: connector status to compute name of
306 * In contrast to the other drm_get_*_name functions this one here returns a
307 * const pointer and hence is threadsafe.
309 const char *drm_get_connector_status_name(enum drm_connector_status status
)
311 if (status
== connector_status_connected
)
313 else if (status
== connector_status_disconnected
)
314 return "disconnected";
318 EXPORT_SYMBOL(drm_get_connector_status_name
);
321 * drm_get_subpixel_order_name - return a string for a given subpixel enum
322 * @order: enum of subpixel_order
324 * Note you could abuse this and return something out of bounds, but that
325 * would be a caller error. No unscrubbed user data should make it here.
327 const char *drm_get_subpixel_order_name(enum subpixel_order order
)
329 return drm_subpixel_enum_list
[order
].name
;
331 EXPORT_SYMBOL(drm_get_subpixel_order_name
);
333 static char printable_char(int c
)
335 return isascii(c
) && isprint(c
) ? c
: '?';
339 * drm_get_format_name - return a string for drm fourcc format
340 * @format: format to compute name of
342 * Note that the buffer used by this function is globally shared and owned by
343 * the function itself.
345 * FIXME: This isn't really multithreading safe.
347 const char *drm_get_format_name(uint32_t format
)
351 snprintf(buf
, sizeof(buf
),
352 "%c%c%c%c %s-endian (0x%08x)",
353 printable_char(format
& 0xff),
354 printable_char((format
>> 8) & 0xff),
355 printable_char((format
>> 16) & 0xff),
356 printable_char((format
>> 24) & 0x7f),
357 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
362 EXPORT_SYMBOL(drm_get_format_name
);
365 * drm_mode_object_get - allocate a new modeset identifier
367 * @obj: object pointer, used to generate unique ID
368 * @obj_type: object type
370 * Create a unique identifier based on @ptr in @dev's identifier space. Used
371 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
372 * modeset identifiers are _not_ reference counted. Hence don't use this for
373 * reference counted modeset objects like framebuffers.
376 * New unique (relative to other objects in @dev) integer identifier for the
379 int drm_mode_object_get(struct drm_device
*dev
,
380 struct drm_mode_object
*obj
, uint32_t obj_type
)
384 mutex_lock(&dev
->mode_config
.idr_mutex
);
385 ret
= idr_alloc(&dev
->mode_config
.crtc_idr
, obj
, 1, 0, GFP_KERNEL
);
388 * Set up the object linking under the protection of the idr
389 * lock so that other users can't see inconsistent state.
392 obj
->type
= obj_type
;
394 mutex_unlock(&dev
->mode_config
.idr_mutex
);
396 return ret
< 0 ? ret
: 0;
400 * drm_mode_object_put - free a modeset identifer
402 * @object: object to free
404 * Free @id from @dev's unique identifier pool. Note that despite the _get
405 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
406 * for reference counted modeset objects like framebuffers.
408 void drm_mode_object_put(struct drm_device
*dev
,
409 struct drm_mode_object
*object
)
411 mutex_lock(&dev
->mode_config
.idr_mutex
);
412 idr_remove(&dev
->mode_config
.crtc_idr
, object
->id
);
413 mutex_unlock(&dev
->mode_config
.idr_mutex
);
416 static struct drm_mode_object
*_object_find(struct drm_device
*dev
,
417 uint32_t id
, uint32_t type
)
419 struct drm_mode_object
*obj
= NULL
;
421 mutex_lock(&dev
->mode_config
.idr_mutex
);
422 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
423 if (!obj
|| (type
!= DRM_MODE_OBJECT_ANY
&& obj
->type
!= type
) ||
426 mutex_unlock(&dev
->mode_config
.idr_mutex
);
432 * drm_mode_object_find - look up a drm object with static lifetime
434 * @id: id of the mode object
435 * @type: type of the mode object
437 * Note that framebuffers cannot be looked up with this functions - since those
438 * are reference counted, they need special treatment. Even with
439 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
440 * rather than WARN_ON()).
442 struct drm_mode_object
*drm_mode_object_find(struct drm_device
*dev
,
443 uint32_t id
, uint32_t type
)
445 struct drm_mode_object
*obj
= NULL
;
447 /* Framebuffers are reference counted and need their own lookup
449 WARN_ON(type
== DRM_MODE_OBJECT_FB
);
450 obj
= _object_find(dev
, id
, type
);
451 /* don't leak out unref'd fb's */
452 if (obj
&& (obj
->type
== DRM_MODE_OBJECT_FB
))
456 EXPORT_SYMBOL(drm_mode_object_find
);
459 * drm_framebuffer_init - initialize a framebuffer
461 * @fb: framebuffer to be initialized
462 * @funcs: ... with these functions
464 * Allocates an ID for the framebuffer's parent mode object, sets its mode
465 * functions & device file and adds it to the master fd list.
468 * This functions publishes the fb and makes it available for concurrent access
469 * by other users. Which means by this point the fb _must_ be fully set up -
470 * since all the fb attributes are invariant over its lifetime, no further
471 * locking but only correct reference counting is required.
474 * Zero on success, error code on failure.
476 int drm_framebuffer_init(struct drm_device
*dev
, struct drm_framebuffer
*fb
,
477 const struct drm_framebuffer_funcs
*funcs
)
481 mutex_lock(&dev
->mode_config
.fb_lock
);
482 kref_init(&fb
->refcount
);
483 INIT_LIST_HEAD(&fb
->filp_head
);
487 ret
= drm_mode_object_get(dev
, &fb
->base
, DRM_MODE_OBJECT_FB
);
491 /* Grab the idr reference. */
492 drm_framebuffer_reference(fb
);
494 dev
->mode_config
.num_fb
++;
495 list_add(&fb
->head
, &dev
->mode_config
.fb_list
);
497 mutex_unlock(&dev
->mode_config
.fb_lock
);
501 EXPORT_SYMBOL(drm_framebuffer_init
);
503 static void drm_framebuffer_free(struct kref
*kref
)
505 struct drm_framebuffer
*fb
=
506 container_of(kref
, struct drm_framebuffer
, refcount
);
507 fb
->funcs
->destroy(fb
);
510 static struct drm_framebuffer
*__drm_framebuffer_lookup(struct drm_device
*dev
,
513 struct drm_mode_object
*obj
= NULL
;
514 struct drm_framebuffer
*fb
;
516 mutex_lock(&dev
->mode_config
.idr_mutex
);
517 obj
= idr_find(&dev
->mode_config
.crtc_idr
, id
);
518 if (!obj
|| (obj
->type
!= DRM_MODE_OBJECT_FB
) || (obj
->id
!= id
))
522 mutex_unlock(&dev
->mode_config
.idr_mutex
);
528 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
530 * @id: id of the fb object
532 * If successful, this grabs an additional reference to the framebuffer -
533 * callers need to make sure to eventually unreference the returned framebuffer
534 * again, using @drm_framebuffer_unreference.
536 struct drm_framebuffer
*drm_framebuffer_lookup(struct drm_device
*dev
,
539 struct drm_framebuffer
*fb
;
541 mutex_lock(&dev
->mode_config
.fb_lock
);
542 fb
= __drm_framebuffer_lookup(dev
, id
);
544 drm_framebuffer_reference(fb
);
545 mutex_unlock(&dev
->mode_config
.fb_lock
);
549 EXPORT_SYMBOL(drm_framebuffer_lookup
);
552 * drm_framebuffer_unreference - unref a framebuffer
553 * @fb: framebuffer to unref
555 * This functions decrements the fb's refcount and frees it if it drops to zero.
557 void drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
559 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
560 kref_put(&fb
->refcount
, drm_framebuffer_free
);
562 EXPORT_SYMBOL(drm_framebuffer_unreference
);
565 * drm_framebuffer_reference - incr the fb refcnt
568 * This functions increments the fb's refcount.
570 void drm_framebuffer_reference(struct drm_framebuffer
*fb
)
572 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
573 kref_get(&fb
->refcount
);
575 EXPORT_SYMBOL(drm_framebuffer_reference
);
577 static void drm_framebuffer_free_bug(struct kref
*kref
)
582 static void __drm_framebuffer_unreference(struct drm_framebuffer
*fb
)
584 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb
, fb
->base
.id
, atomic_read(&fb
->refcount
.refcount
));
585 kref_put(&fb
->refcount
, drm_framebuffer_free_bug
);
588 /* dev->mode_config.fb_lock must be held! */
589 static void __drm_framebuffer_unregister(struct drm_device
*dev
,
590 struct drm_framebuffer
*fb
)
592 mutex_lock(&dev
->mode_config
.idr_mutex
);
593 idr_remove(&dev
->mode_config
.crtc_idr
, fb
->base
.id
);
594 mutex_unlock(&dev
->mode_config
.idr_mutex
);
598 __drm_framebuffer_unreference(fb
);
602 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
603 * @fb: fb to unregister
605 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
606 * those used for fbdev. Note that the caller must hold a reference of it's own,
607 * i.e. the object may not be destroyed through this call (since it'll lead to a
608 * locking inversion).
610 void drm_framebuffer_unregister_private(struct drm_framebuffer
*fb
)
612 struct drm_device
*dev
= fb
->dev
;
614 mutex_lock(&dev
->mode_config
.fb_lock
);
615 /* Mark fb as reaped and drop idr ref. */
616 __drm_framebuffer_unregister(dev
, fb
);
617 mutex_unlock(&dev
->mode_config
.fb_lock
);
619 EXPORT_SYMBOL(drm_framebuffer_unregister_private
);
622 * drm_framebuffer_cleanup - remove a framebuffer object
623 * @fb: framebuffer to remove
625 * Cleanup framebuffer. This function is intended to be used from the drivers
626 * ->destroy callback. It can also be used to clean up driver private
627 * framebuffers embedded into a larger structure.
629 * Note that this function does not remove the fb from active usuage - if it is
630 * still used anywhere, hilarity can ensue since userspace could call getfb on
631 * the id and get back -EINVAL. Obviously no concern at driver unload time.
633 * Also, the framebuffer will not be removed from the lookup idr - for
634 * user-created framebuffers this will happen in in the rmfb ioctl. For
635 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
636 * drm_framebuffer_unregister_private.
638 void drm_framebuffer_cleanup(struct drm_framebuffer
*fb
)
640 struct drm_device
*dev
= fb
->dev
;
642 mutex_lock(&dev
->mode_config
.fb_lock
);
644 dev
->mode_config
.num_fb
--;
645 mutex_unlock(&dev
->mode_config
.fb_lock
);
647 EXPORT_SYMBOL(drm_framebuffer_cleanup
);
650 * drm_framebuffer_remove - remove and unreference a framebuffer object
651 * @fb: framebuffer to remove
653 * Scans all the CRTCs and planes in @dev's mode_config. If they're
654 * using @fb, removes it, setting it to NULL. Then drops the reference to the
655 * passed-in framebuffer. Might take the modeset locks.
657 * Note that this function optimizes the cleanup away if the caller holds the
658 * last reference to the framebuffer. It is also guaranteed to not take the
659 * modeset locks in this case.
661 void drm_framebuffer_remove(struct drm_framebuffer
*fb
)
663 struct drm_device
*dev
= fb
->dev
;
664 struct drm_crtc
*crtc
;
665 struct drm_plane
*plane
;
666 struct drm_mode_set set
;
669 WARN_ON(!list_empty(&fb
->filp_head
));
672 * drm ABI mandates that we remove any deleted framebuffers from active
673 * useage. But since most sane clients only remove framebuffers they no
674 * longer need, try to optimize this away.
676 * Since we're holding a reference ourselves, observing a refcount of 1
677 * means that we're the last holder and can skip it. Also, the refcount
678 * can never increase from 1 again, so we don't need any barriers or
681 * Note that userspace could try to race with use and instate a new
682 * usage _after_ we've cleared all current ones. End result will be an
683 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
686 if (atomic_read(&fb
->refcount
.refcount
) > 1) {
687 drm_modeset_lock_all(dev
);
688 /* remove from any CRTC */
689 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
690 if (crtc
->primary
->fb
== fb
) {
691 /* should turn off the crtc */
692 memset(&set
, 0, sizeof(struct drm_mode_set
));
695 ret
= drm_mode_set_config_internal(&set
);
697 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc
);
701 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
703 drm_plane_force_disable(plane
);
705 drm_modeset_unlock_all(dev
);
708 drm_framebuffer_unreference(fb
);
710 EXPORT_SYMBOL(drm_framebuffer_remove
);
712 DEFINE_WW_CLASS(crtc_ww_class
);
715 * drm_crtc_init_with_planes - Initialise a new CRTC object with
716 * specified primary and cursor planes.
718 * @crtc: CRTC object to init
719 * @primary: Primary plane for CRTC
720 * @cursor: Cursor plane for CRTC
721 * @funcs: callbacks for the new CRTC
723 * Inits a new object created as base part of a driver crtc object.
726 * Zero on success, error code on failure.
728 int drm_crtc_init_with_planes(struct drm_device
*dev
, struct drm_crtc
*crtc
,
729 struct drm_plane
*primary
,
731 const struct drm_crtc_funcs
*funcs
)
733 struct drm_mode_config
*config
= &dev
->mode_config
;
738 crtc
->invert_dimensions
= false;
740 drm_modeset_lock_all(dev
);
741 drm_modeset_lock_init(&crtc
->mutex
);
742 /* dropped by _unlock_all(): */
743 drm_modeset_lock(&crtc
->mutex
, config
->acquire_ctx
);
745 ret
= drm_mode_object_get(dev
, &crtc
->base
, DRM_MODE_OBJECT_CRTC
);
749 crtc
->base
.properties
= &crtc
->properties
;
751 list_add_tail(&crtc
->head
, &config
->crtc_list
);
754 crtc
->primary
= primary
;
756 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
759 drm_modeset_unlock_all(dev
);
763 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
766 * drm_crtc_cleanup - Clean up the core crtc usage
767 * @crtc: CRTC to cleanup
769 * This function cleans up @crtc and removes it from the DRM mode setting
770 * core. Note that the function does *not* free the crtc structure itself,
771 * this is the responsibility of the caller.
773 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
775 struct drm_device
*dev
= crtc
->dev
;
777 kfree(crtc
->gamma_store
);
778 crtc
->gamma_store
= NULL
;
780 drm_modeset_lock_fini(&crtc
->mutex
);
782 drm_mode_object_put(dev
, &crtc
->base
);
783 list_del(&crtc
->head
);
784 dev
->mode_config
.num_crtc
--;
786 EXPORT_SYMBOL(drm_crtc_cleanup
);
789 * drm_crtc_index - find the index of a registered CRTC
790 * @crtc: CRTC to find index for
792 * Given a registered CRTC, return the index of that CRTC within a DRM
793 * device's list of CRTCs.
795 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
797 unsigned int index
= 0;
798 struct drm_crtc
*tmp
;
800 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
809 EXPORT_SYMBOL(drm_crtc_index
);
812 * drm_mode_remove - remove and free a mode
813 * @connector: connector list to modify
814 * @mode: mode to remove
816 * Remove @mode from @connector's mode list, then free it.
818 static void drm_mode_remove(struct drm_connector
*connector
,
819 struct drm_display_mode
*mode
)
821 list_del(&mode
->head
);
822 drm_mode_destroy(connector
->dev
, mode
);
826 * drm_connector_init - Init a preallocated connector
828 * @connector: the connector to init
829 * @funcs: callbacks for this connector
830 * @connector_type: user visible type of the connector
832 * Initialises a preallocated connector. Connectors should be
833 * subclassed as part of driver connector objects.
836 * Zero on success, error code on failure.
838 int drm_connector_init(struct drm_device
*dev
,
839 struct drm_connector
*connector
,
840 const struct drm_connector_funcs
*funcs
,
844 struct ida
*connector_ida
=
845 &drm_connector_enum_list
[connector_type
].ida
;
847 drm_modeset_lock_all(dev
);
849 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
853 connector
->base
.properties
= &connector
->properties
;
854 connector
->dev
= dev
;
855 connector
->funcs
= funcs
;
856 connector
->connector_type
= connector_type
;
857 connector
->connector_type_id
=
858 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
859 if (connector
->connector_type_id
< 0) {
860 ret
= connector
->connector_type_id
;
864 kasprintf(GFP_KERNEL
, "%s-%d",
865 drm_connector_enum_list
[connector_type
].name
,
866 connector
->connector_type_id
);
867 if (!connector
->name
) {
872 INIT_LIST_HEAD(&connector
->probed_modes
);
873 INIT_LIST_HEAD(&connector
->modes
);
874 connector
->edid_blob_ptr
= NULL
;
875 connector
->status
= connector_status_unknown
;
877 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
878 dev
->mode_config
.num_connector
++;
880 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
881 drm_object_attach_property(&connector
->base
,
882 dev
->mode_config
.edid_property
,
885 drm_object_attach_property(&connector
->base
,
886 dev
->mode_config
.dpms_property
, 0);
890 drm_mode_object_put(dev
, &connector
->base
);
893 drm_modeset_unlock_all(dev
);
897 EXPORT_SYMBOL(drm_connector_init
);
900 * drm_connector_cleanup - cleans up an initialised connector
901 * @connector: connector to cleanup
903 * Cleans up the connector but doesn't free the object.
905 void drm_connector_cleanup(struct drm_connector
*connector
)
907 struct drm_device
*dev
= connector
->dev
;
908 struct drm_display_mode
*mode
, *t
;
910 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
911 drm_mode_remove(connector
, mode
);
913 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
914 drm_mode_remove(connector
, mode
);
916 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
917 connector
->connector_type_id
);
919 drm_mode_object_put(dev
, &connector
->base
);
920 kfree(connector
->name
);
921 connector
->name
= NULL
;
922 list_del(&connector
->head
);
923 dev
->mode_config
.num_connector
--;
925 EXPORT_SYMBOL(drm_connector_cleanup
);
928 * drm_connector_unplug_all - unregister connector userspace interfaces
931 * This function unregisters all connector userspace interfaces in sysfs. Should
932 * be call when the device is disconnected, e.g. from an usb driver's
933 * ->disconnect callback.
935 void drm_connector_unplug_all(struct drm_device
*dev
)
937 struct drm_connector
*connector
;
939 /* taking the mode config mutex ends up in a clash with sysfs */
940 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
941 drm_sysfs_connector_remove(connector
);
944 EXPORT_SYMBOL(drm_connector_unplug_all
);
947 * drm_bridge_init - initialize a drm transcoder/bridge
949 * @bridge: transcoder/bridge to set up
950 * @funcs: bridge function table
952 * Initialises a preallocated bridge. Bridges should be
953 * subclassed as part of driver connector objects.
956 * Zero on success, error code on failure.
958 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
959 const struct drm_bridge_funcs
*funcs
)
963 drm_modeset_lock_all(dev
);
965 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
970 bridge
->funcs
= funcs
;
972 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
973 dev
->mode_config
.num_bridge
++;
976 drm_modeset_unlock_all(dev
);
979 EXPORT_SYMBOL(drm_bridge_init
);
982 * drm_bridge_cleanup - cleans up an initialised bridge
983 * @bridge: bridge to cleanup
985 * Cleans up the bridge but doesn't free the object.
987 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
989 struct drm_device
*dev
= bridge
->dev
;
991 drm_modeset_lock_all(dev
);
992 drm_mode_object_put(dev
, &bridge
->base
);
993 list_del(&bridge
->head
);
994 dev
->mode_config
.num_bridge
--;
995 drm_modeset_unlock_all(dev
);
997 EXPORT_SYMBOL(drm_bridge_cleanup
);
1000 * drm_encoder_init - Init a preallocated encoder
1002 * @encoder: the encoder to init
1003 * @funcs: callbacks for this encoder
1004 * @encoder_type: user visible type of the encoder
1006 * Initialises a preallocated encoder. Encoder should be
1007 * subclassed as part of driver encoder objects.
1010 * Zero on success, error code on failure.
1012 int drm_encoder_init(struct drm_device
*dev
,
1013 struct drm_encoder
*encoder
,
1014 const struct drm_encoder_funcs
*funcs
,
1019 drm_modeset_lock_all(dev
);
1021 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1026 encoder
->encoder_type
= encoder_type
;
1027 encoder
->funcs
= funcs
;
1028 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1029 drm_encoder_enum_list
[encoder_type
].name
,
1031 if (!encoder
->name
) {
1036 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1037 dev
->mode_config
.num_encoder
++;
1041 drm_mode_object_put(dev
, &encoder
->base
);
1044 drm_modeset_unlock_all(dev
);
1048 EXPORT_SYMBOL(drm_encoder_init
);
1051 * drm_encoder_cleanup - cleans up an initialised encoder
1052 * @encoder: encoder to cleanup
1054 * Cleans up the encoder but doesn't free the object.
1056 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1058 struct drm_device
*dev
= encoder
->dev
;
1059 drm_modeset_lock_all(dev
);
1060 drm_mode_object_put(dev
, &encoder
->base
);
1061 kfree(encoder
->name
);
1062 encoder
->name
= NULL
;
1063 list_del(&encoder
->head
);
1064 dev
->mode_config
.num_encoder
--;
1065 drm_modeset_unlock_all(dev
);
1067 EXPORT_SYMBOL(drm_encoder_cleanup
);
1070 * drm_universal_plane_init - Initialize a new universal plane object
1072 * @plane: plane object to init
1073 * @possible_crtcs: bitmask of possible CRTCs
1074 * @funcs: callbacks for the new plane
1075 * @formats: array of supported formats (%DRM_FORMAT_*)
1076 * @format_count: number of elements in @formats
1077 * @type: type of plane (overlay, primary, cursor)
1079 * Initializes a plane object of type @type.
1082 * Zero on success, error code on failure.
1084 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1085 unsigned long possible_crtcs
,
1086 const struct drm_plane_funcs
*funcs
,
1087 const uint32_t *formats
, uint32_t format_count
,
1088 enum drm_plane_type type
)
1092 drm_modeset_lock_all(dev
);
1094 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1098 plane
->base
.properties
= &plane
->properties
;
1100 plane
->funcs
= funcs
;
1101 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1103 if (!plane
->format_types
) {
1104 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1105 drm_mode_object_put(dev
, &plane
->base
);
1110 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1111 plane
->format_count
= format_count
;
1112 plane
->possible_crtcs
= possible_crtcs
;
1115 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1116 dev
->mode_config
.num_total_plane
++;
1117 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1118 dev
->mode_config
.num_overlay_plane
++;
1120 drm_object_attach_property(&plane
->base
,
1121 dev
->mode_config
.plane_type_property
,
1125 drm_modeset_unlock_all(dev
);
1129 EXPORT_SYMBOL(drm_universal_plane_init
);
1132 * drm_plane_init - Initialize a legacy plane
1134 * @plane: plane object to init
1135 * @possible_crtcs: bitmask of possible CRTCs
1136 * @funcs: callbacks for the new plane
1137 * @formats: array of supported formats (%DRM_FORMAT_*)
1138 * @format_count: number of elements in @formats
1139 * @is_primary: plane type (primary vs overlay)
1141 * Legacy API to initialize a DRM plane.
1143 * New drivers should call drm_universal_plane_init() instead.
1146 * Zero on success, error code on failure.
1148 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1149 unsigned long possible_crtcs
,
1150 const struct drm_plane_funcs
*funcs
,
1151 const uint32_t *formats
, uint32_t format_count
,
1154 enum drm_plane_type type
;
1156 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1157 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1158 formats
, format_count
, type
);
1160 EXPORT_SYMBOL(drm_plane_init
);
1163 * drm_plane_cleanup - Clean up the core plane usage
1164 * @plane: plane to cleanup
1166 * This function cleans up @plane and removes it from the DRM mode setting
1167 * core. Note that the function does *not* free the plane structure itself,
1168 * this is the responsibility of the caller.
1170 void drm_plane_cleanup(struct drm_plane
*plane
)
1172 struct drm_device
*dev
= plane
->dev
;
1174 drm_modeset_lock_all(dev
);
1175 kfree(plane
->format_types
);
1176 drm_mode_object_put(dev
, &plane
->base
);
1178 BUG_ON(list_empty(&plane
->head
));
1180 list_del(&plane
->head
);
1181 dev
->mode_config
.num_total_plane
--;
1182 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1183 dev
->mode_config
.num_overlay_plane
--;
1184 drm_modeset_unlock_all(dev
);
1186 EXPORT_SYMBOL(drm_plane_cleanup
);
1189 * drm_plane_force_disable - Forcibly disable a plane
1190 * @plane: plane to disable
1192 * Forces the plane to be disabled.
1194 * Used when the plane's current framebuffer is destroyed,
1195 * and when restoring fbdev mode.
1197 void drm_plane_force_disable(struct drm_plane
*plane
)
1199 struct drm_framebuffer
*old_fb
= plane
->fb
;
1205 ret
= plane
->funcs
->disable_plane(plane
);
1207 DRM_ERROR("failed to disable plane with busy fb\n");
1210 /* disconnect the plane from the fb and crtc: */
1211 __drm_framebuffer_unreference(old_fb
);
1215 EXPORT_SYMBOL(drm_plane_force_disable
);
1217 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1219 struct drm_property
*edid
;
1220 struct drm_property
*dpms
;
1223 * Standard properties (apply to all connectors)
1225 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1226 DRM_MODE_PROP_IMMUTABLE
,
1228 dev
->mode_config
.edid_property
= edid
;
1230 dpms
= drm_property_create_enum(dev
, 0,
1231 "DPMS", drm_dpms_enum_list
,
1232 ARRAY_SIZE(drm_dpms_enum_list
));
1233 dev
->mode_config
.dpms_property
= dpms
;
1238 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1240 struct drm_property
*type
;
1243 * Standard properties (apply to all planes)
1245 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1246 "type", drm_plane_type_enum_list
,
1247 ARRAY_SIZE(drm_plane_type_enum_list
));
1248 dev
->mode_config
.plane_type_property
= type
;
1254 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1257 * Called by a driver the first time a DVI-I connector is made.
1259 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1261 struct drm_property
*dvi_i_selector
;
1262 struct drm_property
*dvi_i_subconnector
;
1264 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1268 drm_property_create_enum(dev
, 0,
1269 "select subconnector",
1270 drm_dvi_i_select_enum_list
,
1271 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1272 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1274 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1276 drm_dvi_i_subconnector_enum_list
,
1277 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1278 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1282 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1285 * drm_create_tv_properties - create TV specific connector properties
1287 * @num_modes: number of different TV formats (modes) supported
1288 * @modes: array of pointers to strings containing name of each format
1290 * Called by a driver's TV initialization routine, this function creates
1291 * the TV specific connector properties for a given device. Caller is
1292 * responsible for allocating a list of format names and passing them to
1295 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1298 struct drm_property
*tv_selector
;
1299 struct drm_property
*tv_subconnector
;
1302 if (dev
->mode_config
.tv_select_subconnector_property
)
1306 * Basic connector properties
1308 tv_selector
= drm_property_create_enum(dev
, 0,
1309 "select subconnector",
1310 drm_tv_select_enum_list
,
1311 ARRAY_SIZE(drm_tv_select_enum_list
));
1312 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1315 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1317 drm_tv_subconnector_enum_list
,
1318 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1319 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1322 * Other, TV specific properties: margins & TV modes.
1324 dev
->mode_config
.tv_left_margin_property
=
1325 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1327 dev
->mode_config
.tv_right_margin_property
=
1328 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1330 dev
->mode_config
.tv_top_margin_property
=
1331 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1333 dev
->mode_config
.tv_bottom_margin_property
=
1334 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1336 dev
->mode_config
.tv_mode_property
=
1337 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1339 for (i
= 0; i
< num_modes
; i
++)
1340 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1343 dev
->mode_config
.tv_brightness_property
=
1344 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1346 dev
->mode_config
.tv_contrast_property
=
1347 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1349 dev
->mode_config
.tv_flicker_reduction_property
=
1350 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1352 dev
->mode_config
.tv_overscan_property
=
1353 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1355 dev
->mode_config
.tv_saturation_property
=
1356 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1358 dev
->mode_config
.tv_hue_property
=
1359 drm_property_create_range(dev
, 0, "hue", 0, 100);
1363 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1366 * drm_mode_create_scaling_mode_property - create scaling mode property
1369 * Called by a driver the first time it's needed, must be attached to desired
1372 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1374 struct drm_property
*scaling_mode
;
1376 if (dev
->mode_config
.scaling_mode_property
)
1380 drm_property_create_enum(dev
, 0, "scaling mode",
1381 drm_scaling_mode_enum_list
,
1382 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1384 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1388 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1391 * drm_mode_create_dirty_property - create dirty property
1394 * Called by a driver the first time it's needed, must be attached to desired
1397 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1399 struct drm_property
*dirty_info
;
1401 if (dev
->mode_config
.dirty_info_property
)
1405 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1407 drm_dirty_info_enum_list
,
1408 ARRAY_SIZE(drm_dirty_info_enum_list
));
1409 dev
->mode_config
.dirty_info_property
= dirty_info
;
1413 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1415 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1417 uint32_t total_objects
= 0;
1419 total_objects
+= dev
->mode_config
.num_crtc
;
1420 total_objects
+= dev
->mode_config
.num_connector
;
1421 total_objects
+= dev
->mode_config
.num_encoder
;
1422 total_objects
+= dev
->mode_config
.num_bridge
;
1424 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1425 if (!group
->id_list
)
1428 group
->num_crtcs
= 0;
1429 group
->num_connectors
= 0;
1430 group
->num_encoders
= 0;
1431 group
->num_bridges
= 0;
1435 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1437 kfree(group
->id_list
);
1438 group
->id_list
= NULL
;
1442 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1443 * the drm core's responsibility to set up mode control groups.
1445 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1446 struct drm_mode_group
*group
)
1448 struct drm_crtc
*crtc
;
1449 struct drm_encoder
*encoder
;
1450 struct drm_connector
*connector
;
1451 struct drm_bridge
*bridge
;
1454 if ((ret
= drm_mode_group_init(dev
, group
)))
1457 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1458 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1460 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1461 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1464 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1465 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1466 group
->num_connectors
++] = connector
->base
.id
;
1468 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1469 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1470 group
->num_connectors
+ group
->num_bridges
++] =
1475 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1478 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1479 * @out: drm_mode_modeinfo struct to return to the user
1480 * @in: drm_display_mode to use
1482 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1485 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1486 const struct drm_display_mode
*in
)
1488 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1489 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1490 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1491 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1492 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1493 "timing values too large for mode info\n");
1495 out
->clock
= in
->clock
;
1496 out
->hdisplay
= in
->hdisplay
;
1497 out
->hsync_start
= in
->hsync_start
;
1498 out
->hsync_end
= in
->hsync_end
;
1499 out
->htotal
= in
->htotal
;
1500 out
->hskew
= in
->hskew
;
1501 out
->vdisplay
= in
->vdisplay
;
1502 out
->vsync_start
= in
->vsync_start
;
1503 out
->vsync_end
= in
->vsync_end
;
1504 out
->vtotal
= in
->vtotal
;
1505 out
->vscan
= in
->vscan
;
1506 out
->vrefresh
= in
->vrefresh
;
1507 out
->flags
= in
->flags
;
1508 out
->type
= in
->type
;
1509 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1510 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1514 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1515 * @out: drm_display_mode to return to the user
1516 * @in: drm_mode_modeinfo to use
1518 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1522 * Zero on success, errno on failure.
1524 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1525 const struct drm_mode_modeinfo
*in
)
1527 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1530 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1533 out
->clock
= in
->clock
;
1534 out
->hdisplay
= in
->hdisplay
;
1535 out
->hsync_start
= in
->hsync_start
;
1536 out
->hsync_end
= in
->hsync_end
;
1537 out
->htotal
= in
->htotal
;
1538 out
->hskew
= in
->hskew
;
1539 out
->vdisplay
= in
->vdisplay
;
1540 out
->vsync_start
= in
->vsync_start
;
1541 out
->vsync_end
= in
->vsync_end
;
1542 out
->vtotal
= in
->vtotal
;
1543 out
->vscan
= in
->vscan
;
1544 out
->vrefresh
= in
->vrefresh
;
1545 out
->flags
= in
->flags
;
1546 out
->type
= in
->type
;
1547 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1548 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1554 * drm_mode_getresources - get graphics configuration
1555 * @dev: drm device for the ioctl
1556 * @data: data pointer for the ioctl
1557 * @file_priv: drm file for the ioctl call
1559 * Construct a set of configuration description structures and return
1560 * them to the user, including CRTC, connector and framebuffer configuration.
1562 * Called by the user via ioctl.
1565 * Zero on success, errno on failure.
1567 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1568 struct drm_file
*file_priv
)
1570 struct drm_mode_card_res
*card_res
= data
;
1571 struct list_head
*lh
;
1572 struct drm_framebuffer
*fb
;
1573 struct drm_connector
*connector
;
1574 struct drm_crtc
*crtc
;
1575 struct drm_encoder
*encoder
;
1577 int connector_count
= 0;
1580 int encoder_count
= 0;
1582 uint32_t __user
*fb_id
;
1583 uint32_t __user
*crtc_id
;
1584 uint32_t __user
*connector_id
;
1585 uint32_t __user
*encoder_id
;
1586 struct drm_mode_group
*mode_group
;
1588 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1592 mutex_lock(&file_priv
->fbs_lock
);
1594 * For the non-control nodes we need to limit the list of resources
1595 * by IDs in the group list for this node
1597 list_for_each(lh
, &file_priv
->fbs
)
1600 /* handle this in 4 parts */
1602 if (card_res
->count_fbs
>= fb_count
) {
1604 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1605 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1606 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1607 mutex_unlock(&file_priv
->fbs_lock
);
1613 card_res
->count_fbs
= fb_count
;
1614 mutex_unlock(&file_priv
->fbs_lock
);
1616 drm_modeset_lock_all(dev
);
1617 if (!drm_is_primary_client(file_priv
)) {
1620 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1623 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1626 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1630 mode_group
= &file_priv
->master
->minor
->mode_group
;
1631 crtc_count
= mode_group
->num_crtcs
;
1632 connector_count
= mode_group
->num_connectors
;
1633 encoder_count
= mode_group
->num_encoders
;
1636 card_res
->max_height
= dev
->mode_config
.max_height
;
1637 card_res
->min_height
= dev
->mode_config
.min_height
;
1638 card_res
->max_width
= dev
->mode_config
.max_width
;
1639 card_res
->min_width
= dev
->mode_config
.min_width
;
1642 if (card_res
->count_crtcs
>= crtc_count
) {
1644 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1646 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1648 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1649 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1656 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1657 if (put_user(mode_group
->id_list
[i
],
1658 crtc_id
+ copied
)) {
1666 card_res
->count_crtcs
= crtc_count
;
1669 if (card_res
->count_encoders
>= encoder_count
) {
1671 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1673 list_for_each_entry(encoder
,
1674 &dev
->mode_config
.encoder_list
,
1676 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1678 if (put_user(encoder
->base
.id
, encoder_id
+
1686 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1687 if (put_user(mode_group
->id_list
[i
],
1688 encoder_id
+ copied
)) {
1697 card_res
->count_encoders
= encoder_count
;
1700 if (card_res
->count_connectors
>= connector_count
) {
1702 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1704 list_for_each_entry(connector
,
1705 &dev
->mode_config
.connector_list
,
1707 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1710 if (put_user(connector
->base
.id
,
1711 connector_id
+ copied
)) {
1718 int start
= mode_group
->num_crtcs
+
1719 mode_group
->num_encoders
;
1720 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1721 if (put_user(mode_group
->id_list
[i
],
1722 connector_id
+ copied
)) {
1730 card_res
->count_connectors
= connector_count
;
1732 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1733 card_res
->count_connectors
, card_res
->count_encoders
);
1736 drm_modeset_unlock_all(dev
);
1741 * drm_mode_getcrtc - get CRTC configuration
1742 * @dev: drm device for the ioctl
1743 * @data: data pointer for the ioctl
1744 * @file_priv: drm file for the ioctl call
1746 * Construct a CRTC configuration structure to return to the user.
1748 * Called by the user via ioctl.
1751 * Zero on success, errno on failure.
1753 int drm_mode_getcrtc(struct drm_device
*dev
,
1754 void *data
, struct drm_file
*file_priv
)
1756 struct drm_mode_crtc
*crtc_resp
= data
;
1757 struct drm_crtc
*crtc
;
1760 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1763 drm_modeset_lock_all(dev
);
1765 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1771 crtc_resp
->x
= crtc
->x
;
1772 crtc_resp
->y
= crtc
->y
;
1773 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1774 if (crtc
->primary
->fb
)
1775 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1777 crtc_resp
->fb_id
= 0;
1779 if (crtc
->enabled
) {
1781 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1782 crtc_resp
->mode_valid
= 1;
1785 crtc_resp
->mode_valid
= 0;
1789 drm_modeset_unlock_all(dev
);
1793 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1794 const struct drm_file
*file_priv
)
1797 * If user-space hasn't configured the driver to expose the stereo 3D
1798 * modes, don't expose them.
1800 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1807 * drm_mode_getconnector - get connector configuration
1808 * @dev: drm device for the ioctl
1809 * @data: data pointer for the ioctl
1810 * @file_priv: drm file for the ioctl call
1812 * Construct a connector configuration structure to return to the user.
1814 * Called by the user via ioctl.
1817 * Zero on success, errno on failure.
1819 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1820 struct drm_file
*file_priv
)
1822 struct drm_mode_get_connector
*out_resp
= data
;
1823 struct drm_connector
*connector
;
1824 struct drm_display_mode
*mode
;
1826 int props_count
= 0;
1827 int encoders_count
= 0;
1831 struct drm_mode_modeinfo u_mode
;
1832 struct drm_mode_modeinfo __user
*mode_ptr
;
1833 uint32_t __user
*prop_ptr
;
1834 uint64_t __user
*prop_values
;
1835 uint32_t __user
*encoder_ptr
;
1837 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1840 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1842 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1844 mutex_lock(&dev
->mode_config
.mutex
);
1846 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1852 props_count
= connector
->properties
.count
;
1854 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1855 if (connector
->encoder_ids
[i
] != 0) {
1860 if (out_resp
->count_modes
== 0) {
1861 connector
->funcs
->fill_modes(connector
,
1862 dev
->mode_config
.max_width
,
1863 dev
->mode_config
.max_height
);
1866 /* delayed so we get modes regardless of pre-fill_modes state */
1867 list_for_each_entry(mode
, &connector
->modes
, head
)
1868 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1871 out_resp
->connector_id
= connector
->base
.id
;
1872 out_resp
->connector_type
= connector
->connector_type
;
1873 out_resp
->connector_type_id
= connector
->connector_type_id
;
1874 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1875 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1876 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1877 out_resp
->connection
= connector
->status
;
1878 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1879 if (connector
->encoder
)
1880 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1882 out_resp
->encoder_id
= 0;
1883 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1886 * This ioctl is called twice, once to determine how much space is
1887 * needed, and the 2nd time to fill it.
1889 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1891 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1892 list_for_each_entry(mode
, &connector
->modes
, head
) {
1893 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
1896 drm_crtc_convert_to_umode(&u_mode
, mode
);
1897 if (copy_to_user(mode_ptr
+ copied
,
1898 &u_mode
, sizeof(u_mode
))) {
1905 out_resp
->count_modes
= mode_count
;
1907 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1909 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1910 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1911 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1912 if (put_user(connector
->properties
.ids
[i
],
1913 prop_ptr
+ copied
)) {
1918 if (put_user(connector
->properties
.values
[i
],
1919 prop_values
+ copied
)) {
1926 out_resp
->count_props
= props_count
;
1928 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1930 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1931 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1932 if (connector
->encoder_ids
[i
] != 0) {
1933 if (put_user(connector
->encoder_ids
[i
],
1934 encoder_ptr
+ copied
)) {
1942 out_resp
->count_encoders
= encoders_count
;
1945 mutex_unlock(&dev
->mode_config
.mutex
);
1951 * drm_mode_getencoder - get encoder configuration
1952 * @dev: drm device for the ioctl
1953 * @data: data pointer for the ioctl
1954 * @file_priv: drm file for the ioctl call
1956 * Construct a encoder configuration structure to return to the user.
1958 * Called by the user via ioctl.
1961 * Zero on success, errno on failure.
1963 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1964 struct drm_file
*file_priv
)
1966 struct drm_mode_get_encoder
*enc_resp
= data
;
1967 struct drm_encoder
*encoder
;
1970 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1973 drm_modeset_lock_all(dev
);
1974 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
1981 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1983 enc_resp
->crtc_id
= 0;
1984 enc_resp
->encoder_type
= encoder
->encoder_type
;
1985 enc_resp
->encoder_id
= encoder
->base
.id
;
1986 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1987 enc_resp
->possible_clones
= encoder
->possible_clones
;
1990 drm_modeset_unlock_all(dev
);
1995 * drm_mode_getplane_res - enumerate all plane resources
1998 * @file_priv: DRM file info
2000 * Construct a list of plane ids to return to the user.
2002 * Called by the user via ioctl.
2005 * Zero on success, errno on failure.
2007 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2008 struct drm_file
*file_priv
)
2010 struct drm_mode_get_plane_res
*plane_resp
= data
;
2011 struct drm_mode_config
*config
;
2012 struct drm_plane
*plane
;
2013 uint32_t __user
*plane_ptr
;
2014 int copied
= 0, ret
= 0;
2015 unsigned num_planes
;
2017 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2020 drm_modeset_lock_all(dev
);
2021 config
= &dev
->mode_config
;
2023 if (file_priv
->universal_planes
)
2024 num_planes
= config
->num_total_plane
;
2026 num_planes
= config
->num_overlay_plane
;
2029 * This ioctl is called twice, once to determine how much space is
2030 * needed, and the 2nd time to fill it.
2033 (plane_resp
->count_planes
>= num_planes
)) {
2034 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2036 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2038 * Unless userspace set the 'universal planes'
2039 * capability bit, only advertise overlays.
2041 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2042 !file_priv
->universal_planes
)
2045 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2052 plane_resp
->count_planes
= num_planes
;
2055 drm_modeset_unlock_all(dev
);
2060 * drm_mode_getplane - get plane configuration
2063 * @file_priv: DRM file info
2065 * Construct a plane configuration structure to return to the user.
2067 * Called by the user via ioctl.
2070 * Zero on success, errno on failure.
2072 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2073 struct drm_file
*file_priv
)
2075 struct drm_mode_get_plane
*plane_resp
= data
;
2076 struct drm_plane
*plane
;
2077 uint32_t __user
*format_ptr
;
2080 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2083 drm_modeset_lock_all(dev
);
2084 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2091 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2093 plane_resp
->crtc_id
= 0;
2096 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2098 plane_resp
->fb_id
= 0;
2100 plane_resp
->plane_id
= plane
->base
.id
;
2101 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2102 plane_resp
->gamma_size
= 0;
2105 * This ioctl is called twice, once to determine how much space is
2106 * needed, and the 2nd time to fill it.
2108 if (plane
->format_count
&&
2109 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2110 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2111 if (copy_to_user(format_ptr
,
2112 plane
->format_types
,
2113 sizeof(uint32_t) * plane
->format_count
)) {
2118 plane_resp
->count_format_types
= plane
->format_count
;
2121 drm_modeset_unlock_all(dev
);
2126 * setplane_internal - setplane handler for internal callers
2128 * Note that we assume an extra reference has already been taken on fb. If the
2129 * update fails, this reference will be dropped before return; if it succeeds,
2130 * the previous framebuffer (if any) will be unreferenced instead.
2132 * src_{x,y,w,h} are provided in 16.16 fixed point format
2134 static int setplane_internal(struct drm_crtc
*crtc
,
2135 struct drm_plane
*plane
,
2136 struct drm_framebuffer
*fb
,
2137 int32_t crtc_x
, int32_t crtc_y
,
2138 uint32_t crtc_w
, uint32_t crtc_h
,
2139 /* src_{x,y,w,h} values are 16.16 fixed point */
2140 uint32_t src_x
, uint32_t src_y
,
2141 uint32_t src_w
, uint32_t src_h
)
2143 struct drm_device
*dev
= crtc
->dev
;
2144 struct drm_framebuffer
*old_fb
= NULL
;
2146 unsigned int fb_width
, fb_height
;
2149 /* No fb means shut it down */
2151 drm_modeset_lock_all(dev
);
2153 ret
= plane
->funcs
->disable_plane(plane
);
2160 drm_modeset_unlock_all(dev
);
2164 /* Check whether this plane is usable on this CRTC */
2165 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2166 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2171 /* Check whether this plane supports the fb pixel format. */
2172 for (i
= 0; i
< plane
->format_count
; i
++)
2173 if (fb
->pixel_format
== plane
->format_types
[i
])
2175 if (i
== plane
->format_count
) {
2176 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2177 drm_get_format_name(fb
->pixel_format
));
2182 fb_width
= fb
->width
<< 16;
2183 fb_height
= fb
->height
<< 16;
2185 /* Make sure source coordinates are inside the fb. */
2186 if (src_w
> fb_width
||
2187 src_x
> fb_width
- src_w
||
2188 src_h
> fb_height
||
2189 src_y
> fb_height
- src_h
) {
2190 DRM_DEBUG_KMS("Invalid source coordinates "
2191 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2192 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2193 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2194 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2195 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2200 drm_modeset_lock_all(dev
);
2202 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2203 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2204 src_x
, src_y
, src_w
, src_h
);
2212 drm_modeset_unlock_all(dev
);
2216 drm_framebuffer_unreference(fb
);
2218 drm_framebuffer_unreference(old_fb
);
2225 * drm_mode_setplane - configure a plane's configuration
2227 * @data: ioctl data*
2228 * @file_priv: DRM file info
2230 * Set plane configuration, including placement, fb, scaling, and other factors.
2231 * Or pass a NULL fb to disable (planes may be disabled without providing a
2235 * Zero on success, errno on failure.
2237 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2238 struct drm_file
*file_priv
)
2240 struct drm_mode_set_plane
*plane_req
= data
;
2241 struct drm_mode_object
*obj
;
2242 struct drm_plane
*plane
;
2243 struct drm_crtc
*crtc
= NULL
;
2244 struct drm_framebuffer
*fb
= NULL
;
2246 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2249 /* Give drivers some help against integer overflows */
2250 if (plane_req
->crtc_w
> INT_MAX
||
2251 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2252 plane_req
->crtc_h
> INT_MAX
||
2253 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2254 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2255 plane_req
->crtc_w
, plane_req
->crtc_h
,
2256 plane_req
->crtc_x
, plane_req
->crtc_y
);
2261 * First, find the plane, crtc, and fb objects. If not available,
2262 * we don't bother to call the driver.
2264 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2265 DRM_MODE_OBJECT_PLANE
);
2267 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2268 plane_req
->plane_id
);
2271 plane
= obj_to_plane(obj
);
2273 if (plane_req
->fb_id
) {
2274 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2276 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2281 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2282 DRM_MODE_OBJECT_CRTC
);
2284 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2285 plane_req
->crtc_id
);
2288 crtc
= obj_to_crtc(obj
);
2291 return setplane_internal(crtc
, plane
, fb
,
2292 plane_req
->crtc_x
, plane_req
->crtc_y
,
2293 plane_req
->crtc_w
, plane_req
->crtc_h
,
2294 plane_req
->src_x
, plane_req
->src_y
,
2295 plane_req
->src_w
, plane_req
->src_h
);
2299 * drm_mode_set_config_internal - helper to call ->set_config
2300 * @set: modeset config to set
2302 * This is a little helper to wrap internal calls to the ->set_config driver
2303 * interface. The only thing it adds is correct refcounting dance.
2306 * Zero on success, errno on failure.
2308 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2310 struct drm_crtc
*crtc
= set
->crtc
;
2311 struct drm_framebuffer
*fb
;
2312 struct drm_crtc
*tmp
;
2316 * NOTE: ->set_config can also disable other crtcs (if we steal all
2317 * connectors from it), hence we need to refcount the fbs across all
2318 * crtcs. Atomic modeset will have saner semantics ...
2320 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2321 tmp
->old_fb
= tmp
->primary
->fb
;
2325 ret
= crtc
->funcs
->set_config(set
);
2327 crtc
->primary
->crtc
= crtc
;
2328 crtc
->primary
->fb
= fb
;
2331 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2332 if (tmp
->primary
->fb
)
2333 drm_framebuffer_reference(tmp
->primary
->fb
);
2335 drm_framebuffer_unreference(tmp
->old_fb
);
2340 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2343 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2345 * @crtc: CRTC that framebuffer will be displayed on
2348 * @mode: mode that framebuffer will be displayed under
2349 * @fb: framebuffer to check size of
2351 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2353 const struct drm_display_mode
*mode
,
2354 const struct drm_framebuffer
*fb
)
2357 int hdisplay
, vdisplay
;
2359 hdisplay
= mode
->hdisplay
;
2360 vdisplay
= mode
->vdisplay
;
2362 if (drm_mode_is_stereo(mode
)) {
2363 struct drm_display_mode adjusted
= *mode
;
2365 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2366 hdisplay
= adjusted
.crtc_hdisplay
;
2367 vdisplay
= adjusted
.crtc_vdisplay
;
2370 if (crtc
->invert_dimensions
)
2371 swap(hdisplay
, vdisplay
);
2373 if (hdisplay
> fb
->width
||
2374 vdisplay
> fb
->height
||
2375 x
> fb
->width
- hdisplay
||
2376 y
> fb
->height
- vdisplay
) {
2377 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2378 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2379 crtc
->invert_dimensions
? " (inverted)" : "");
2385 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2388 * drm_mode_setcrtc - set CRTC configuration
2389 * @dev: drm device for the ioctl
2390 * @data: data pointer for the ioctl
2391 * @file_priv: drm file for the ioctl call
2393 * Build a new CRTC configuration based on user request.
2395 * Called by the user via ioctl.
2398 * Zero on success, errno on failure.
2400 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2401 struct drm_file
*file_priv
)
2403 struct drm_mode_config
*config
= &dev
->mode_config
;
2404 struct drm_mode_crtc
*crtc_req
= data
;
2405 struct drm_crtc
*crtc
;
2406 struct drm_connector
**connector_set
= NULL
, *connector
;
2407 struct drm_framebuffer
*fb
= NULL
;
2408 struct drm_display_mode
*mode
= NULL
;
2409 struct drm_mode_set set
;
2410 uint32_t __user
*set_connectors_ptr
;
2414 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2417 /* For some reason crtc x/y offsets are signed internally. */
2418 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2421 drm_modeset_lock_all(dev
);
2422 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2424 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2428 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2430 if (crtc_req
->mode_valid
) {
2431 /* If we have a mode we need a framebuffer. */
2432 /* If we pass -1, set the mode with the currently bound fb */
2433 if (crtc_req
->fb_id
== -1) {
2434 if (!crtc
->primary
->fb
) {
2435 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2439 fb
= crtc
->primary
->fb
;
2440 /* Make refcounting symmetric with the lookup path. */
2441 drm_framebuffer_reference(fb
);
2443 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2445 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2452 mode
= drm_mode_create(dev
);
2458 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2460 DRM_DEBUG_KMS("Invalid mode\n");
2464 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2466 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2473 if (crtc_req
->count_connectors
== 0 && mode
) {
2474 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2479 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2480 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2481 crtc_req
->count_connectors
);
2486 if (crtc_req
->count_connectors
> 0) {
2489 /* Avoid unbounded kernel memory allocation */
2490 if (crtc_req
->count_connectors
> config
->num_connector
) {
2495 connector_set
= kmalloc(crtc_req
->count_connectors
*
2496 sizeof(struct drm_connector
*),
2498 if (!connector_set
) {
2503 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2504 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2505 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2510 connector
= drm_connector_find(dev
, out_id
);
2512 DRM_DEBUG_KMS("Connector id %d unknown\n",
2517 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2521 connector_set
[i
] = connector
;
2526 set
.x
= crtc_req
->x
;
2527 set
.y
= crtc_req
->y
;
2529 set
.connectors
= connector_set
;
2530 set
.num_connectors
= crtc_req
->count_connectors
;
2532 ret
= drm_mode_set_config_internal(&set
);
2536 drm_framebuffer_unreference(fb
);
2538 kfree(connector_set
);
2539 drm_mode_destroy(dev
, mode
);
2540 drm_modeset_unlock_all(dev
);
2544 static int drm_mode_cursor_common(struct drm_device
*dev
,
2545 struct drm_mode_cursor2
*req
,
2546 struct drm_file
*file_priv
)
2548 struct drm_crtc
*crtc
;
2551 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2554 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2557 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2559 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2563 drm_modeset_lock(&crtc
->mutex
, NULL
);
2564 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2565 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2569 /* Turns off the cursor if handle is 0 */
2570 if (crtc
->funcs
->cursor_set2
)
2571 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2572 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2574 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2575 req
->width
, req
->height
);
2578 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2579 if (crtc
->funcs
->cursor_move
) {
2580 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2587 drm_modeset_unlock(&crtc
->mutex
);
2595 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2596 * @dev: drm device for the ioctl
2597 * @data: data pointer for the ioctl
2598 * @file_priv: drm file for the ioctl call
2600 * Set the cursor configuration based on user request.
2602 * Called by the user via ioctl.
2605 * Zero on success, errno on failure.
2607 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2608 void *data
, struct drm_file
*file_priv
)
2610 struct drm_mode_cursor
*req
= data
;
2611 struct drm_mode_cursor2 new_req
;
2613 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2614 new_req
.hot_x
= new_req
.hot_y
= 0;
2616 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2620 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2621 * @dev: drm device for the ioctl
2622 * @data: data pointer for the ioctl
2623 * @file_priv: drm file for the ioctl call
2625 * Set the cursor configuration based on user request. This implements the 2nd
2626 * version of the cursor ioctl, which allows userspace to additionally specify
2627 * the hotspot of the pointer.
2629 * Called by the user via ioctl.
2632 * Zero on success, errno on failure.
2634 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2635 void *data
, struct drm_file
*file_priv
)
2637 struct drm_mode_cursor2
*req
= data
;
2638 return drm_mode_cursor_common(dev
, req
, file_priv
);
2642 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2643 * @bpp: bits per pixels
2644 * @depth: bit depth per pixel
2646 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2647 * Useful in fbdev emulation code, since that deals in those values.
2649 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2655 fmt
= DRM_FORMAT_C8
;
2659 fmt
= DRM_FORMAT_XRGB1555
;
2661 fmt
= DRM_FORMAT_RGB565
;
2664 fmt
= DRM_FORMAT_RGB888
;
2668 fmt
= DRM_FORMAT_XRGB8888
;
2669 else if (depth
== 30)
2670 fmt
= DRM_FORMAT_XRGB2101010
;
2672 fmt
= DRM_FORMAT_ARGB8888
;
2675 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2676 fmt
= DRM_FORMAT_XRGB8888
;
2682 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2685 * drm_mode_addfb - add an FB to the graphics configuration
2686 * @dev: drm device for the ioctl
2687 * @data: data pointer for the ioctl
2688 * @file_priv: drm file for the ioctl call
2690 * Add a new FB to the specified CRTC, given a user request. This is the
2691 * original addfb ioclt which only supported RGB formats.
2693 * Called by the user via ioctl.
2696 * Zero on success, errno on failure.
2698 int drm_mode_addfb(struct drm_device
*dev
,
2699 void *data
, struct drm_file
*file_priv
)
2701 struct drm_mode_fb_cmd
*or = data
;
2702 struct drm_mode_fb_cmd2 r
= {};
2703 struct drm_mode_config
*config
= &dev
->mode_config
;
2704 struct drm_framebuffer
*fb
;
2707 /* Use new struct with format internally */
2708 r
.fb_id
= or->fb_id
;
2709 r
.width
= or->width
;
2710 r
.height
= or->height
;
2711 r
.pitches
[0] = or->pitch
;
2712 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2713 r
.handles
[0] = or->handle
;
2715 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2718 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2721 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2724 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2726 DRM_DEBUG_KMS("could not create framebuffer\n");
2730 mutex_lock(&file_priv
->fbs_lock
);
2731 or->fb_id
= fb
->base
.id
;
2732 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2733 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2734 mutex_unlock(&file_priv
->fbs_lock
);
2739 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2741 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2745 case DRM_FORMAT_RGB332
:
2746 case DRM_FORMAT_BGR233
:
2747 case DRM_FORMAT_XRGB4444
:
2748 case DRM_FORMAT_XBGR4444
:
2749 case DRM_FORMAT_RGBX4444
:
2750 case DRM_FORMAT_BGRX4444
:
2751 case DRM_FORMAT_ARGB4444
:
2752 case DRM_FORMAT_ABGR4444
:
2753 case DRM_FORMAT_RGBA4444
:
2754 case DRM_FORMAT_BGRA4444
:
2755 case DRM_FORMAT_XRGB1555
:
2756 case DRM_FORMAT_XBGR1555
:
2757 case DRM_FORMAT_RGBX5551
:
2758 case DRM_FORMAT_BGRX5551
:
2759 case DRM_FORMAT_ARGB1555
:
2760 case DRM_FORMAT_ABGR1555
:
2761 case DRM_FORMAT_RGBA5551
:
2762 case DRM_FORMAT_BGRA5551
:
2763 case DRM_FORMAT_RGB565
:
2764 case DRM_FORMAT_BGR565
:
2765 case DRM_FORMAT_RGB888
:
2766 case DRM_FORMAT_BGR888
:
2767 case DRM_FORMAT_XRGB8888
:
2768 case DRM_FORMAT_XBGR8888
:
2769 case DRM_FORMAT_RGBX8888
:
2770 case DRM_FORMAT_BGRX8888
:
2771 case DRM_FORMAT_ARGB8888
:
2772 case DRM_FORMAT_ABGR8888
:
2773 case DRM_FORMAT_RGBA8888
:
2774 case DRM_FORMAT_BGRA8888
:
2775 case DRM_FORMAT_XRGB2101010
:
2776 case DRM_FORMAT_XBGR2101010
:
2777 case DRM_FORMAT_RGBX1010102
:
2778 case DRM_FORMAT_BGRX1010102
:
2779 case DRM_FORMAT_ARGB2101010
:
2780 case DRM_FORMAT_ABGR2101010
:
2781 case DRM_FORMAT_RGBA1010102
:
2782 case DRM_FORMAT_BGRA1010102
:
2783 case DRM_FORMAT_YUYV
:
2784 case DRM_FORMAT_YVYU
:
2785 case DRM_FORMAT_UYVY
:
2786 case DRM_FORMAT_VYUY
:
2787 case DRM_FORMAT_AYUV
:
2788 case DRM_FORMAT_NV12
:
2789 case DRM_FORMAT_NV21
:
2790 case DRM_FORMAT_NV16
:
2791 case DRM_FORMAT_NV61
:
2792 case DRM_FORMAT_NV24
:
2793 case DRM_FORMAT_NV42
:
2794 case DRM_FORMAT_YUV410
:
2795 case DRM_FORMAT_YVU410
:
2796 case DRM_FORMAT_YUV411
:
2797 case DRM_FORMAT_YVU411
:
2798 case DRM_FORMAT_YUV420
:
2799 case DRM_FORMAT_YVU420
:
2800 case DRM_FORMAT_YUV422
:
2801 case DRM_FORMAT_YVU422
:
2802 case DRM_FORMAT_YUV444
:
2803 case DRM_FORMAT_YVU444
:
2806 DRM_DEBUG_KMS("invalid pixel format %s\n",
2807 drm_get_format_name(r
->pixel_format
));
2812 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2814 int ret
, hsub
, vsub
, num_planes
, i
;
2816 ret
= format_check(r
);
2818 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2819 drm_get_format_name(r
->pixel_format
));
2823 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2824 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2825 num_planes
= drm_format_num_planes(r
->pixel_format
);
2827 if (r
->width
== 0 || r
->width
% hsub
) {
2828 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2832 if (r
->height
== 0 || r
->height
% vsub
) {
2833 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2837 for (i
= 0; i
< num_planes
; i
++) {
2838 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2839 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
2840 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
2842 if (!r
->handles
[i
]) {
2843 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2847 if ((uint64_t) width
* cpp
> UINT_MAX
)
2850 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
2853 if (r
->pitches
[i
] < width
* cpp
) {
2854 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2862 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
2863 struct drm_mode_fb_cmd2
*r
,
2864 struct drm_file
*file_priv
)
2866 struct drm_mode_config
*config
= &dev
->mode_config
;
2867 struct drm_framebuffer
*fb
;
2870 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
2871 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
2872 return ERR_PTR(-EINVAL
);
2875 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2876 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2877 r
->width
, config
->min_width
, config
->max_width
);
2878 return ERR_PTR(-EINVAL
);
2880 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2881 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2882 r
->height
, config
->min_height
, config
->max_height
);
2883 return ERR_PTR(-EINVAL
);
2886 ret
= framebuffer_check(r
);
2888 return ERR_PTR(ret
);
2890 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2892 DRM_DEBUG_KMS("could not create framebuffer\n");
2896 mutex_lock(&file_priv
->fbs_lock
);
2897 r
->fb_id
= fb
->base
.id
;
2898 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2899 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2900 mutex_unlock(&file_priv
->fbs_lock
);
2906 * drm_mode_addfb2 - add an FB to the graphics configuration
2907 * @dev: drm device for the ioctl
2908 * @data: data pointer for the ioctl
2909 * @file_priv: drm file for the ioctl call
2911 * Add a new FB to the specified CRTC, given a user request with format. This is
2912 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2913 * and uses fourcc codes as pixel format specifiers.
2915 * Called by the user via ioctl.
2918 * Zero on success, errno on failure.
2920 int drm_mode_addfb2(struct drm_device
*dev
,
2921 void *data
, struct drm_file
*file_priv
)
2923 struct drm_framebuffer
*fb
;
2925 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2928 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
2936 * drm_mode_rmfb - remove an FB from the configuration
2937 * @dev: drm device for the ioctl
2938 * @data: data pointer for the ioctl
2939 * @file_priv: drm file for the ioctl call
2941 * Remove the FB specified by the user.
2943 * Called by the user via ioctl.
2946 * Zero on success, errno on failure.
2948 int drm_mode_rmfb(struct drm_device
*dev
,
2949 void *data
, struct drm_file
*file_priv
)
2951 struct drm_framebuffer
*fb
= NULL
;
2952 struct drm_framebuffer
*fbl
= NULL
;
2953 uint32_t *id
= data
;
2956 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2959 mutex_lock(&file_priv
->fbs_lock
);
2960 mutex_lock(&dev
->mode_config
.fb_lock
);
2961 fb
= __drm_framebuffer_lookup(dev
, *id
);
2965 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2971 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2972 __drm_framebuffer_unregister(dev
, fb
);
2974 list_del_init(&fb
->filp_head
);
2975 mutex_unlock(&dev
->mode_config
.fb_lock
);
2976 mutex_unlock(&file_priv
->fbs_lock
);
2978 drm_framebuffer_remove(fb
);
2983 mutex_unlock(&dev
->mode_config
.fb_lock
);
2984 mutex_unlock(&file_priv
->fbs_lock
);
2990 * drm_mode_getfb - get FB info
2991 * @dev: drm device for the ioctl
2992 * @data: data pointer for the ioctl
2993 * @file_priv: drm file for the ioctl call
2995 * Lookup the FB given its ID and return info about it.
2997 * Called by the user via ioctl.
3000 * Zero on success, errno on failure.
3002 int drm_mode_getfb(struct drm_device
*dev
,
3003 void *data
, struct drm_file
*file_priv
)
3005 struct drm_mode_fb_cmd
*r
= data
;
3006 struct drm_framebuffer
*fb
;
3009 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3012 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3016 r
->height
= fb
->height
;
3017 r
->width
= fb
->width
;
3018 r
->depth
= fb
->depth
;
3019 r
->bpp
= fb
->bits_per_pixel
;
3020 r
->pitch
= fb
->pitches
[0];
3021 if (fb
->funcs
->create_handle
) {
3022 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3023 drm_is_control_client(file_priv
)) {
3024 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3027 /* GET_FB() is an unprivileged ioctl so we must not
3028 * return a buffer-handle to non-master processes! For
3029 * backwards-compatibility reasons, we cannot make
3030 * GET_FB() privileged, so just return an invalid handle
3031 * for non-masters. */
3039 drm_framebuffer_unreference(fb
);
3045 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3046 * @dev: drm device for the ioctl
3047 * @data: data pointer for the ioctl
3048 * @file_priv: drm file for the ioctl call
3050 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3051 * rectangle list. Generic userspace which does frontbuffer rendering must call
3052 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3053 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3055 * Modesetting drivers which always update the frontbuffer do not need to
3056 * implement the corresponding ->dirty framebuffer callback.
3058 * Called by the user via ioctl.
3061 * Zero on success, errno on failure.
3063 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3064 void *data
, struct drm_file
*file_priv
)
3066 struct drm_clip_rect __user
*clips_ptr
;
3067 struct drm_clip_rect
*clips
= NULL
;
3068 struct drm_mode_fb_dirty_cmd
*r
= data
;
3069 struct drm_framebuffer
*fb
;
3074 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3077 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3081 num_clips
= r
->num_clips
;
3082 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3084 if (!num_clips
!= !clips_ptr
) {
3089 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3091 /* If userspace annotates copy, clips must come in pairs */
3092 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3097 if (num_clips
&& clips_ptr
) {
3098 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3102 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3108 ret
= copy_from_user(clips
, clips_ptr
,
3109 num_clips
* sizeof(*clips
));
3116 if (fb
->funcs
->dirty
) {
3117 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3126 drm_framebuffer_unreference(fb
);
3133 * drm_fb_release - remove and free the FBs on this file
3134 * @priv: drm file for the ioctl
3136 * Destroy all the FBs associated with @filp.
3138 * Called by the user via ioctl.
3141 * Zero on success, errno on failure.
3143 void drm_fb_release(struct drm_file
*priv
)
3145 struct drm_device
*dev
= priv
->minor
->dev
;
3146 struct drm_framebuffer
*fb
, *tfb
;
3148 mutex_lock(&priv
->fbs_lock
);
3149 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3151 mutex_lock(&dev
->mode_config
.fb_lock
);
3152 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3153 __drm_framebuffer_unregister(dev
, fb
);
3154 mutex_unlock(&dev
->mode_config
.fb_lock
);
3156 list_del_init(&fb
->filp_head
);
3158 /* This will also drop the fpriv->fbs reference. */
3159 drm_framebuffer_remove(fb
);
3161 mutex_unlock(&priv
->fbs_lock
);
3165 * drm_property_create - create a new property type
3167 * @flags: flags specifying the property type
3168 * @name: name of the property
3169 * @num_values: number of pre-defined values
3171 * This creates a new generic drm property which can then be attached to a drm
3172 * object with drm_object_attach_property. The returned property object must be
3173 * freed with drm_property_destroy.
3176 * A pointer to the newly created property on success, NULL on failure.
3178 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3179 const char *name
, int num_values
)
3181 struct drm_property
*property
= NULL
;
3184 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3188 property
->dev
= dev
;
3191 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3192 if (!property
->values
)
3196 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3200 property
->flags
= flags
;
3201 property
->num_values
= num_values
;
3202 INIT_LIST_HEAD(&property
->enum_blob_list
);
3205 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3206 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3209 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3211 WARN_ON(!drm_property_type_valid(property
));
3215 kfree(property
->values
);
3219 EXPORT_SYMBOL(drm_property_create
);
3222 * drm_property_create - create a new enumeration property type
3224 * @flags: flags specifying the property type
3225 * @name: name of the property
3226 * @props: enumeration lists with property values
3227 * @num_values: number of pre-defined values
3229 * This creates a new generic drm property which can then be attached to a drm
3230 * object with drm_object_attach_property. The returned property object must be
3231 * freed with drm_property_destroy.
3233 * Userspace is only allowed to set one of the predefined values for enumeration
3237 * A pointer to the newly created property on success, NULL on failure.
3239 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3241 const struct drm_prop_enum_list
*props
,
3244 struct drm_property
*property
;
3247 flags
|= DRM_MODE_PROP_ENUM
;
3249 property
= drm_property_create(dev
, flags
, name
, num_values
);
3253 for (i
= 0; i
< num_values
; i
++) {
3254 ret
= drm_property_add_enum(property
, i
,
3258 drm_property_destroy(dev
, property
);
3265 EXPORT_SYMBOL(drm_property_create_enum
);
3268 * drm_property_create - create a new bitmask property type
3270 * @flags: flags specifying the property type
3271 * @name: name of the property
3272 * @props: enumeration lists with property bitflags
3273 * @num_values: number of pre-defined values
3275 * This creates a new generic drm property which can then be attached to a drm
3276 * object with drm_object_attach_property. The returned property object must be
3277 * freed with drm_property_destroy.
3279 * Compared to plain enumeration properties userspace is allowed to set any
3280 * or'ed together combination of the predefined property bitflag values
3283 * A pointer to the newly created property on success, NULL on failure.
3285 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3286 int flags
, const char *name
,
3287 const struct drm_prop_enum_list
*props
,
3290 struct drm_property
*property
;
3293 flags
|= DRM_MODE_PROP_BITMASK
;
3295 property
= drm_property_create(dev
, flags
, name
, num_values
);
3299 for (i
= 0; i
< num_values
; i
++) {
3300 ret
= drm_property_add_enum(property
, i
,
3304 drm_property_destroy(dev
, property
);
3311 EXPORT_SYMBOL(drm_property_create_bitmask
);
3313 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3314 int flags
, const char *name
,
3315 uint64_t min
, uint64_t max
)
3317 struct drm_property
*property
;
3319 property
= drm_property_create(dev
, flags
, name
, 2);
3323 property
->values
[0] = min
;
3324 property
->values
[1] = max
;
3330 * drm_property_create - create a new ranged property type
3332 * @flags: flags specifying the property type
3333 * @name: name of the property
3334 * @min: minimum value of the property
3335 * @max: maximum value of the property
3337 * This creates a new generic drm property which can then be attached to a drm
3338 * object with drm_object_attach_property. The returned property object must be
3339 * freed with drm_property_destroy.
3341 * Userspace is allowed to set any interger value in the (min, max) range
3345 * A pointer to the newly created property on success, NULL on failure.
3347 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3349 uint64_t min
, uint64_t max
)
3351 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3354 EXPORT_SYMBOL(drm_property_create_range
);
3356 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3357 int flags
, const char *name
,
3358 int64_t min
, int64_t max
)
3360 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3361 name
, I642U64(min
), I642U64(max
));
3363 EXPORT_SYMBOL(drm_property_create_signed_range
);
3365 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3366 int flags
, const char *name
, uint32_t type
)
3368 struct drm_property
*property
;
3370 flags
|= DRM_MODE_PROP_OBJECT
;
3372 property
= drm_property_create(dev
, flags
, name
, 1);
3376 property
->values
[0] = type
;
3380 EXPORT_SYMBOL(drm_property_create_object
);
3383 * drm_property_add_enum - add a possible value to an enumeration property
3384 * @property: enumeration property to change
3385 * @index: index of the new enumeration
3386 * @value: value of the new enumeration
3387 * @name: symbolic name of the new enumeration
3389 * This functions adds enumerations to a property.
3391 * It's use is deprecated, drivers should use one of the more specific helpers
3392 * to directly create the property with all enumerations already attached.
3395 * Zero on success, error code on failure.
3397 int drm_property_add_enum(struct drm_property
*property
, int index
,
3398 uint64_t value
, const char *name
)
3400 struct drm_property_enum
*prop_enum
;
3402 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3403 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3407 * Bitmask enum properties have the additional constraint of values
3410 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3414 if (!list_empty(&property
->enum_blob_list
)) {
3415 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3416 if (prop_enum
->value
== value
) {
3417 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3418 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3424 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3428 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3429 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3430 prop_enum
->value
= value
;
3432 property
->values
[index
] = value
;
3433 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3436 EXPORT_SYMBOL(drm_property_add_enum
);
3439 * drm_property_destroy - destroy a drm property
3441 * @property: property to destry
3443 * This function frees a property including any attached resources like
3444 * enumeration values.
3446 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3448 struct drm_property_enum
*prop_enum
, *pt
;
3450 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3451 list_del(&prop_enum
->head
);
3455 if (property
->num_values
)
3456 kfree(property
->values
);
3457 drm_mode_object_put(dev
, &property
->base
);
3458 list_del(&property
->head
);
3461 EXPORT_SYMBOL(drm_property_destroy
);
3464 * drm_object_attach_property - attach a property to a modeset object
3465 * @obj: drm modeset object
3466 * @property: property to attach
3467 * @init_val: initial value of the property
3469 * This attaches the given property to the modeset object with the given initial
3470 * value. Currently this function cannot fail since the properties are stored in
3471 * a statically sized array.
3473 void drm_object_attach_property(struct drm_mode_object
*obj
,
3474 struct drm_property
*property
,
3477 int count
= obj
->properties
->count
;
3479 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3480 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3481 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3482 "you see this message on the same object type.\n",
3487 obj
->properties
->ids
[count
] = property
->base
.id
;
3488 obj
->properties
->values
[count
] = init_val
;
3489 obj
->properties
->count
++;
3491 EXPORT_SYMBOL(drm_object_attach_property
);
3494 * drm_object_property_set_value - set the value of a property
3495 * @obj: drm mode object to set property value for
3496 * @property: property to set
3497 * @val: value the property should be set to
3499 * This functions sets a given property on a given object. This function only
3500 * changes the software state of the property, it does not call into the
3501 * driver's ->set_property callback.
3504 * Zero on success, error code on failure.
3506 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3507 struct drm_property
*property
, uint64_t val
)
3511 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3512 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3513 obj
->properties
->values
[i
] = val
;
3520 EXPORT_SYMBOL(drm_object_property_set_value
);
3523 * drm_object_property_get_value - retrieve the value of a property
3524 * @obj: drm mode object to get property value from
3525 * @property: property to retrieve
3526 * @val: storage for the property value
3528 * This function retrieves the softare state of the given property for the given
3529 * property. Since there is no driver callback to retrieve the current property
3530 * value this might be out of sync with the hardware, depending upon the driver
3534 * Zero on success, error code on failure.
3536 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3537 struct drm_property
*property
, uint64_t *val
)
3541 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3542 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3543 *val
= obj
->properties
->values
[i
];
3550 EXPORT_SYMBOL(drm_object_property_get_value
);
3553 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3556 * @file_priv: DRM file info
3558 * This function retrieves the current value for an connectors's property.
3560 * Called by the user via ioctl.
3563 * Zero on success, errno on failure.
3565 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3566 void *data
, struct drm_file
*file_priv
)
3568 struct drm_mode_get_property
*out_resp
= data
;
3569 struct drm_property
*property
;
3572 int value_count
= 0;
3575 struct drm_property_enum
*prop_enum
;
3576 struct drm_mode_property_enum __user
*enum_ptr
;
3577 struct drm_property_blob
*prop_blob
;
3578 uint32_t __user
*blob_id_ptr
;
3579 uint64_t __user
*values_ptr
;
3580 uint32_t __user
*blob_length_ptr
;
3582 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3585 drm_modeset_lock_all(dev
);
3586 property
= drm_property_find(dev
, out_resp
->prop_id
);
3592 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3593 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3594 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3596 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3597 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3601 value_count
= property
->num_values
;
3603 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3604 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3605 out_resp
->flags
= property
->flags
;
3607 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3608 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3609 for (i
= 0; i
< value_count
; i
++) {
3610 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3616 out_resp
->count_values
= value_count
;
3618 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3619 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3620 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3622 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3623 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3625 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3630 if (copy_to_user(&enum_ptr
[copied
].name
,
3631 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3638 out_resp
->count_enum_blobs
= enum_count
;
3641 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3642 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3644 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3645 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3647 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3648 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3653 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3661 out_resp
->count_enum_blobs
= blob_count
;
3664 drm_modeset_unlock_all(dev
);
3668 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3671 struct drm_property_blob
*blob
;
3674 if (!length
|| !data
)
3677 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3681 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3687 blob
->length
= length
;
3689 memcpy(blob
->data
, data
, length
);
3691 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3695 static void drm_property_destroy_blob(struct drm_device
*dev
,
3696 struct drm_property_blob
*blob
)
3698 drm_mode_object_put(dev
, &blob
->base
);
3699 list_del(&blob
->head
);
3704 * drm_mode_getblob_ioctl - get the contents of a blob property value
3707 * @file_priv: DRM file info
3709 * This function retrieves the contents of a blob property. The value stored in
3710 * an object's blob property is just a normal modeset object id.
3712 * Called by the user via ioctl.
3715 * Zero on success, errno on failure.
3717 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3718 void *data
, struct drm_file
*file_priv
)
3720 struct drm_mode_get_blob
*out_resp
= data
;
3721 struct drm_property_blob
*blob
;
3723 void __user
*blob_ptr
;
3725 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3728 drm_modeset_lock_all(dev
);
3729 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3735 if (out_resp
->length
== blob
->length
) {
3736 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3737 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3742 out_resp
->length
= blob
->length
;
3745 drm_modeset_unlock_all(dev
);
3750 * drm_mode_connector_update_edid_property - update the edid property of a connector
3751 * @connector: drm connector
3752 * @edid: new value of the edid property
3754 * This function creates a new blob modeset object and assigns its id to the
3755 * connector's edid property.
3758 * Zero on success, errno on failure.
3760 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3763 struct drm_device
*dev
= connector
->dev
;
3766 if (connector
->edid_blob_ptr
)
3767 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3769 /* Delete edid, when there is none. */
3771 connector
->edid_blob_ptr
= NULL
;
3772 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3776 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3777 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3779 if (!connector
->edid_blob_ptr
)
3782 ret
= drm_object_property_set_value(&connector
->base
,
3783 dev
->mode_config
.edid_property
,
3784 connector
->edid_blob_ptr
->base
.id
);
3788 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3790 static bool drm_property_change_is_valid(struct drm_property
*property
,
3793 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3796 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3797 if (value
< property
->values
[0] || value
> property
->values
[1])
3800 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3801 int64_t svalue
= U642I64(value
);
3802 if (svalue
< U642I64(property
->values
[0]) ||
3803 svalue
> U642I64(property
->values
[1]))
3806 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3808 uint64_t valid_mask
= 0;
3809 for (i
= 0; i
< property
->num_values
; i
++)
3810 valid_mask
|= (1ULL << property
->values
[i
]);
3811 return !(value
& ~valid_mask
);
3812 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3813 /* Only the driver knows */
3815 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3816 struct drm_mode_object
*obj
;
3817 /* a zero value for an object property translates to null: */
3821 * NOTE: use _object_find() directly to bypass restriction on
3822 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3823 * object this could race against object finalization, so it
3824 * simply tells us that the object *was* valid. Which is good
3827 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
3831 for (i
= 0; i
< property
->num_values
; i
++)
3832 if (property
->values
[i
] == value
)
3839 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3842 * @file_priv: DRM file info
3844 * This function sets the current value for a connectors's property. It also
3845 * calls into a driver's ->set_property callback to update the hardware state
3847 * Called by the user via ioctl.
3850 * Zero on success, errno on failure.
3852 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3853 void *data
, struct drm_file
*file_priv
)
3855 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3856 struct drm_mode_obj_set_property obj_set_prop
= {
3857 .value
= conn_set_prop
->value
,
3858 .prop_id
= conn_set_prop
->prop_id
,
3859 .obj_id
= conn_set_prop
->connector_id
,
3860 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3863 /* It does all the locking and checking we need */
3864 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3867 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3868 struct drm_property
*property
,
3872 struct drm_connector
*connector
= obj_to_connector(obj
);
3874 /* Do DPMS ourselves */
3875 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3876 if (connector
->funcs
->dpms
)
3877 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3879 } else if (connector
->funcs
->set_property
)
3880 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3882 /* store the property value if successful */
3884 drm_object_property_set_value(&connector
->base
, property
, value
);
3888 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3889 struct drm_property
*property
,
3893 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3895 if (crtc
->funcs
->set_property
)
3896 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3898 drm_object_property_set_value(obj
, property
, value
);
3903 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3904 struct drm_property
*property
,
3908 struct drm_plane
*plane
= obj_to_plane(obj
);
3910 if (plane
->funcs
->set_property
)
3911 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3913 drm_object_property_set_value(obj
, property
, value
);
3919 * drm_mode_getproperty_ioctl - get the current value of a object's property
3922 * @file_priv: DRM file info
3924 * This function retrieves the current value for an object's property. Compared
3925 * to the connector specific ioctl this one is extended to also work on crtc and
3928 * Called by the user via ioctl.
3931 * Zero on success, errno on failure.
3933 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3934 struct drm_file
*file_priv
)
3936 struct drm_mode_obj_get_properties
*arg
= data
;
3937 struct drm_mode_object
*obj
;
3941 int props_count
= 0;
3942 uint32_t __user
*props_ptr
;
3943 uint64_t __user
*prop_values_ptr
;
3945 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3948 drm_modeset_lock_all(dev
);
3950 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3955 if (!obj
->properties
) {
3960 props_count
= obj
->properties
->count
;
3962 /* This ioctl is called twice, once to determine how much space is
3963 * needed, and the 2nd time to fill it. */
3964 if ((arg
->count_props
>= props_count
) && props_count
) {
3966 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3967 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3968 (arg
->prop_values_ptr
);
3969 for (i
= 0; i
< props_count
; i
++) {
3970 if (put_user(obj
->properties
->ids
[i
],
3971 props_ptr
+ copied
)) {
3975 if (put_user(obj
->properties
->values
[i
],
3976 prop_values_ptr
+ copied
)) {
3983 arg
->count_props
= props_count
;
3985 drm_modeset_unlock_all(dev
);
3990 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3993 * @file_priv: DRM file info
3995 * This function sets the current value for an object's property. It also calls
3996 * into a driver's ->set_property callback to update the hardware state.
3997 * Compared to the connector specific ioctl this one is extended to also work on
3998 * crtc and plane objects.
4000 * Called by the user via ioctl.
4003 * Zero on success, errno on failure.
4005 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4006 struct drm_file
*file_priv
)
4008 struct drm_mode_obj_set_property
*arg
= data
;
4009 struct drm_mode_object
*arg_obj
;
4010 struct drm_mode_object
*prop_obj
;
4011 struct drm_property
*property
;
4015 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4018 drm_modeset_lock_all(dev
);
4020 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4025 if (!arg_obj
->properties
)
4028 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4029 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4032 if (i
== arg_obj
->properties
->count
)
4035 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4036 DRM_MODE_OBJECT_PROPERTY
);
4041 property
= obj_to_property(prop_obj
);
4043 if (!drm_property_change_is_valid(property
, arg
->value
))
4046 switch (arg_obj
->type
) {
4047 case DRM_MODE_OBJECT_CONNECTOR
:
4048 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4051 case DRM_MODE_OBJECT_CRTC
:
4052 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4054 case DRM_MODE_OBJECT_PLANE
:
4055 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4060 drm_modeset_unlock_all(dev
);
4065 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4066 * @connector: connector to attach
4067 * @encoder: encoder to attach @connector to
4069 * This function links up a connector to an encoder. Note that the routing
4070 * restrictions between encoders and crtcs are exposed to userspace through the
4071 * possible_clones and possible_crtcs bitmasks.
4074 * Zero on success, errno on failure.
4076 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4077 struct drm_encoder
*encoder
)
4081 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4082 if (connector
->encoder_ids
[i
] == 0) {
4083 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4089 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4092 * drm_mode_crtc_set_gamma_size - set the gamma table size
4093 * @crtc: CRTC to set the gamma table size for
4094 * @gamma_size: size of the gamma table
4096 * Drivers which support gamma tables should set this to the supported gamma
4097 * table size when initializing the CRTC. Currently the drm core only supports a
4098 * fixed gamma table size.
4101 * Zero on success, errno on failure.
4103 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4106 crtc
->gamma_size
= gamma_size
;
4108 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4109 if (!crtc
->gamma_store
) {
4110 crtc
->gamma_size
= 0;
4116 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4119 * drm_mode_gamma_set_ioctl - set the gamma table
4122 * @file_priv: DRM file info
4124 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4125 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4127 * Called by the user via ioctl.
4130 * Zero on success, errno on failure.
4132 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4133 void *data
, struct drm_file
*file_priv
)
4135 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4136 struct drm_crtc
*crtc
;
4137 void *r_base
, *g_base
, *b_base
;
4141 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4144 drm_modeset_lock_all(dev
);
4145 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4151 if (crtc
->funcs
->gamma_set
== NULL
) {
4156 /* memcpy into gamma store */
4157 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4162 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4163 r_base
= crtc
->gamma_store
;
4164 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4169 g_base
= r_base
+ size
;
4170 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4175 b_base
= g_base
+ size
;
4176 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4181 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4184 drm_modeset_unlock_all(dev
);
4190 * drm_mode_gamma_get_ioctl - get the gamma table
4193 * @file_priv: DRM file info
4195 * Copy the current gamma table into the storage provided. This also provides
4196 * the gamma table size the driver expects, which can be used to size the
4197 * allocated storage.
4199 * Called by the user via ioctl.
4202 * Zero on success, errno on failure.
4204 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4205 void *data
, struct drm_file
*file_priv
)
4207 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4208 struct drm_crtc
*crtc
;
4209 void *r_base
, *g_base
, *b_base
;
4213 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4216 drm_modeset_lock_all(dev
);
4217 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4223 /* memcpy into gamma store */
4224 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4229 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4230 r_base
= crtc
->gamma_store
;
4231 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4236 g_base
= r_base
+ size
;
4237 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4242 b_base
= g_base
+ size
;
4243 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4248 drm_modeset_unlock_all(dev
);
4253 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4256 * @file_priv: DRM file info
4258 * This schedules an asynchronous update on a given CRTC, called page flip.
4259 * Optionally a drm event is generated to signal the completion of the event.
4260 * Generic drivers cannot assume that a pageflip with changed framebuffer
4261 * properties (including driver specific metadata like tiling layout) will work,
4262 * but some drivers support e.g. pixel format changes through the pageflip
4265 * Called by the user via ioctl.
4268 * Zero on success, errno on failure.
4270 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4271 void *data
, struct drm_file
*file_priv
)
4273 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4274 struct drm_crtc
*crtc
;
4275 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4276 struct drm_pending_vblank_event
*e
= NULL
;
4277 unsigned long flags
;
4280 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4281 page_flip
->reserved
!= 0)
4284 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4287 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4291 drm_modeset_lock(&crtc
->mutex
, NULL
);
4292 if (crtc
->primary
->fb
== NULL
) {
4293 /* The framebuffer is currently unbound, presumably
4294 * due to a hotplug event, that userspace has not
4301 if (crtc
->funcs
->page_flip
== NULL
)
4304 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4310 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4314 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4315 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4320 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4322 spin_lock_irqsave(&dev
->event_lock
, flags
);
4323 if (file_priv
->event_space
< sizeof e
->event
) {
4324 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4327 file_priv
->event_space
-= sizeof e
->event
;
4328 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4330 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4332 spin_lock_irqsave(&dev
->event_lock
, flags
);
4333 file_priv
->event_space
+= sizeof e
->event
;
4334 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4338 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4339 e
->event
.base
.length
= sizeof e
->event
;
4340 e
->event
.user_data
= page_flip
->user_data
;
4341 e
->base
.event
= &e
->event
.base
;
4342 e
->base
.file_priv
= file_priv
;
4344 (void (*) (struct drm_pending_event
*)) kfree
;
4347 old_fb
= crtc
->primary
->fb
;
4348 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4350 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4351 spin_lock_irqsave(&dev
->event_lock
, flags
);
4352 file_priv
->event_space
+= sizeof e
->event
;
4353 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4356 /* Keep the old fb, don't unref it. */
4360 * Warn if the driver hasn't properly updated the crtc->fb
4361 * field to reflect that the new framebuffer is now used.
4362 * Failing to do so will screw with the reference counting
4365 WARN_ON(crtc
->primary
->fb
!= fb
);
4366 /* Unref only the old framebuffer. */
4372 drm_framebuffer_unreference(fb
);
4374 drm_framebuffer_unreference(old_fb
);
4375 drm_modeset_unlock(&crtc
->mutex
);
4381 * drm_mode_config_reset - call ->reset callbacks
4384 * This functions calls all the crtc's, encoder's and connector's ->reset
4385 * callback. Drivers can use this in e.g. their driver load or resume code to
4386 * reset hardware and software state.
4388 void drm_mode_config_reset(struct drm_device
*dev
)
4390 struct drm_crtc
*crtc
;
4391 struct drm_encoder
*encoder
;
4392 struct drm_connector
*connector
;
4394 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4395 if (crtc
->funcs
->reset
)
4396 crtc
->funcs
->reset(crtc
);
4398 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4399 if (encoder
->funcs
->reset
)
4400 encoder
->funcs
->reset(encoder
);
4402 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4403 connector
->status
= connector_status_unknown
;
4405 if (connector
->funcs
->reset
)
4406 connector
->funcs
->reset(connector
);
4409 EXPORT_SYMBOL(drm_mode_config_reset
);
4412 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4415 * @file_priv: DRM file info
4417 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4418 * TTM or something else entirely) and returns the resulting buffer handle. This
4419 * handle can then be wrapped up into a framebuffer modeset object.
4421 * Note that userspace is not allowed to use such objects for render
4422 * acceleration - drivers must create their own private ioctls for such a use
4425 * Called by the user via ioctl.
4428 * Zero on success, errno on failure.
4430 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4431 void *data
, struct drm_file
*file_priv
)
4433 struct drm_mode_create_dumb
*args
= data
;
4434 u32 cpp
, stride
, size
;
4436 if (!dev
->driver
->dumb_create
)
4438 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4441 /* overflow checks for 32bit size calculations */
4442 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4443 if (cpp
> 0xffffffffU
/ args
->width
)
4445 stride
= cpp
* args
->width
;
4446 if (args
->height
> 0xffffffffU
/ stride
)
4449 /* test for wrap-around */
4450 size
= args
->height
* stride
;
4451 if (PAGE_ALIGN(size
) == 0)
4454 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4458 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4461 * @file_priv: DRM file info
4463 * Allocate an offset in the drm device node's address space to be able to
4464 * memory map a dumb buffer.
4466 * Called by the user via ioctl.
4469 * Zero on success, errno on failure.
4471 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4472 void *data
, struct drm_file
*file_priv
)
4474 struct drm_mode_map_dumb
*args
= data
;
4476 /* call driver ioctl to get mmap offset */
4477 if (!dev
->driver
->dumb_map_offset
)
4480 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4484 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4487 * @file_priv: DRM file info
4489 * This destroys the userspace handle for the given dumb backing storage buffer.
4490 * Since buffer objects must be reference counted in the kernel a buffer object
4491 * won't be immediately freed if a framebuffer modeset object still uses it.
4493 * Called by the user via ioctl.
4496 * Zero on success, errno on failure.
4498 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4499 void *data
, struct drm_file
*file_priv
)
4501 struct drm_mode_destroy_dumb
*args
= data
;
4503 if (!dev
->driver
->dumb_destroy
)
4506 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4510 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4511 * @format: pixel format (DRM_FORMAT_*)
4512 * @depth: storage for the depth value
4513 * @bpp: storage for the bpp value
4515 * This only supports RGB formats here for compat with code that doesn't use
4516 * pixel formats directly yet.
4518 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4523 case DRM_FORMAT_RGB332
:
4524 case DRM_FORMAT_BGR233
:
4528 case DRM_FORMAT_XRGB1555
:
4529 case DRM_FORMAT_XBGR1555
:
4530 case DRM_FORMAT_RGBX5551
:
4531 case DRM_FORMAT_BGRX5551
:
4532 case DRM_FORMAT_ARGB1555
:
4533 case DRM_FORMAT_ABGR1555
:
4534 case DRM_FORMAT_RGBA5551
:
4535 case DRM_FORMAT_BGRA5551
:
4539 case DRM_FORMAT_RGB565
:
4540 case DRM_FORMAT_BGR565
:
4544 case DRM_FORMAT_RGB888
:
4545 case DRM_FORMAT_BGR888
:
4549 case DRM_FORMAT_XRGB8888
:
4550 case DRM_FORMAT_XBGR8888
:
4551 case DRM_FORMAT_RGBX8888
:
4552 case DRM_FORMAT_BGRX8888
:
4556 case DRM_FORMAT_XRGB2101010
:
4557 case DRM_FORMAT_XBGR2101010
:
4558 case DRM_FORMAT_RGBX1010102
:
4559 case DRM_FORMAT_BGRX1010102
:
4560 case DRM_FORMAT_ARGB2101010
:
4561 case DRM_FORMAT_ABGR2101010
:
4562 case DRM_FORMAT_RGBA1010102
:
4563 case DRM_FORMAT_BGRA1010102
:
4567 case DRM_FORMAT_ARGB8888
:
4568 case DRM_FORMAT_ABGR8888
:
4569 case DRM_FORMAT_RGBA8888
:
4570 case DRM_FORMAT_BGRA8888
:
4575 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4576 drm_get_format_name(format
));
4582 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4585 * drm_format_num_planes - get the number of planes for format
4586 * @format: pixel format (DRM_FORMAT_*)
4589 * The number of planes used by the specified pixel format.
4591 int drm_format_num_planes(uint32_t format
)
4594 case DRM_FORMAT_YUV410
:
4595 case DRM_FORMAT_YVU410
:
4596 case DRM_FORMAT_YUV411
:
4597 case DRM_FORMAT_YVU411
:
4598 case DRM_FORMAT_YUV420
:
4599 case DRM_FORMAT_YVU420
:
4600 case DRM_FORMAT_YUV422
:
4601 case DRM_FORMAT_YVU422
:
4602 case DRM_FORMAT_YUV444
:
4603 case DRM_FORMAT_YVU444
:
4605 case DRM_FORMAT_NV12
:
4606 case DRM_FORMAT_NV21
:
4607 case DRM_FORMAT_NV16
:
4608 case DRM_FORMAT_NV61
:
4609 case DRM_FORMAT_NV24
:
4610 case DRM_FORMAT_NV42
:
4616 EXPORT_SYMBOL(drm_format_num_planes
);
4619 * drm_format_plane_cpp - determine the bytes per pixel value
4620 * @format: pixel format (DRM_FORMAT_*)
4621 * @plane: plane index
4624 * The bytes per pixel value for the specified plane.
4626 int drm_format_plane_cpp(uint32_t format
, int plane
)
4631 if (plane
>= drm_format_num_planes(format
))
4635 case DRM_FORMAT_YUYV
:
4636 case DRM_FORMAT_YVYU
:
4637 case DRM_FORMAT_UYVY
:
4638 case DRM_FORMAT_VYUY
:
4640 case DRM_FORMAT_NV12
:
4641 case DRM_FORMAT_NV21
:
4642 case DRM_FORMAT_NV16
:
4643 case DRM_FORMAT_NV61
:
4644 case DRM_FORMAT_NV24
:
4645 case DRM_FORMAT_NV42
:
4646 return plane
? 2 : 1;
4647 case DRM_FORMAT_YUV410
:
4648 case DRM_FORMAT_YVU410
:
4649 case DRM_FORMAT_YUV411
:
4650 case DRM_FORMAT_YVU411
:
4651 case DRM_FORMAT_YUV420
:
4652 case DRM_FORMAT_YVU420
:
4653 case DRM_FORMAT_YUV422
:
4654 case DRM_FORMAT_YVU422
:
4655 case DRM_FORMAT_YUV444
:
4656 case DRM_FORMAT_YVU444
:
4659 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4663 EXPORT_SYMBOL(drm_format_plane_cpp
);
4666 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4667 * @format: pixel format (DRM_FORMAT_*)
4670 * The horizontal chroma subsampling factor for the
4671 * specified pixel format.
4673 int drm_format_horz_chroma_subsampling(uint32_t format
)
4676 case DRM_FORMAT_YUV411
:
4677 case DRM_FORMAT_YVU411
:
4678 case DRM_FORMAT_YUV410
:
4679 case DRM_FORMAT_YVU410
:
4681 case DRM_FORMAT_YUYV
:
4682 case DRM_FORMAT_YVYU
:
4683 case DRM_FORMAT_UYVY
:
4684 case DRM_FORMAT_VYUY
:
4685 case DRM_FORMAT_NV12
:
4686 case DRM_FORMAT_NV21
:
4687 case DRM_FORMAT_NV16
:
4688 case DRM_FORMAT_NV61
:
4689 case DRM_FORMAT_YUV422
:
4690 case DRM_FORMAT_YVU422
:
4691 case DRM_FORMAT_YUV420
:
4692 case DRM_FORMAT_YVU420
:
4698 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4701 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4702 * @format: pixel format (DRM_FORMAT_*)
4705 * The vertical chroma subsampling factor for the
4706 * specified pixel format.
4708 int drm_format_vert_chroma_subsampling(uint32_t format
)
4711 case DRM_FORMAT_YUV410
:
4712 case DRM_FORMAT_YVU410
:
4714 case DRM_FORMAT_YUV420
:
4715 case DRM_FORMAT_YVU420
:
4716 case DRM_FORMAT_NV12
:
4717 case DRM_FORMAT_NV21
:
4723 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4726 * drm_mode_config_init - initialize DRM mode_configuration structure
4729 * Initialize @dev's mode_config structure, used for tracking the graphics
4730 * configuration of @dev.
4732 * Since this initializes the modeset locks, no locking is possible. Which is no
4733 * problem, since this should happen single threaded at init time. It is the
4734 * driver's problem to ensure this guarantee.
4737 void drm_mode_config_init(struct drm_device
*dev
)
4739 mutex_init(&dev
->mode_config
.mutex
);
4740 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
4741 mutex_init(&dev
->mode_config
.idr_mutex
);
4742 mutex_init(&dev
->mode_config
.fb_lock
);
4743 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4744 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4745 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4746 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4747 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4748 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4749 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4750 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4751 idr_init(&dev
->mode_config
.crtc_idr
);
4753 drm_modeset_lock_all(dev
);
4754 drm_mode_create_standard_connector_properties(dev
);
4755 drm_mode_create_standard_plane_properties(dev
);
4756 drm_modeset_unlock_all(dev
);
4758 /* Just to be sure */
4759 dev
->mode_config
.num_fb
= 0;
4760 dev
->mode_config
.num_connector
= 0;
4761 dev
->mode_config
.num_crtc
= 0;
4762 dev
->mode_config
.num_encoder
= 0;
4763 dev
->mode_config
.num_overlay_plane
= 0;
4764 dev
->mode_config
.num_total_plane
= 0;
4766 EXPORT_SYMBOL(drm_mode_config_init
);
4769 * drm_mode_config_cleanup - free up DRM mode_config info
4772 * Free up all the connectors and CRTCs associated with this DRM device, then
4773 * free up the framebuffers and associated buffer objects.
4775 * Note that since this /should/ happen single-threaded at driver/device
4776 * teardown time, no locking is required. It's the driver's job to ensure that
4777 * this guarantee actually holds true.
4779 * FIXME: cleanup any dangling user buffer objects too
4781 void drm_mode_config_cleanup(struct drm_device
*dev
)
4783 struct drm_connector
*connector
, *ot
;
4784 struct drm_crtc
*crtc
, *ct
;
4785 struct drm_encoder
*encoder
, *enct
;
4786 struct drm_bridge
*bridge
, *brt
;
4787 struct drm_framebuffer
*fb
, *fbt
;
4788 struct drm_property
*property
, *pt
;
4789 struct drm_property_blob
*blob
, *bt
;
4790 struct drm_plane
*plane
, *plt
;
4792 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4794 encoder
->funcs
->destroy(encoder
);
4797 list_for_each_entry_safe(bridge
, brt
,
4798 &dev
->mode_config
.bridge_list
, head
) {
4799 bridge
->funcs
->destroy(bridge
);
4802 list_for_each_entry_safe(connector
, ot
,
4803 &dev
->mode_config
.connector_list
, head
) {
4804 connector
->funcs
->destroy(connector
);
4807 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
4809 drm_property_destroy(dev
, property
);
4812 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
4814 drm_property_destroy_blob(dev
, blob
);
4818 * Single-threaded teardown context, so it's not required to grab the
4819 * fb_lock to protect against concurrent fb_list access. Contrary, it
4820 * would actually deadlock with the drm_framebuffer_cleanup function.
4822 * Also, if there are any framebuffers left, that's a driver leak now,
4823 * so politely WARN about this.
4825 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
4826 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
4827 drm_framebuffer_remove(fb
);
4830 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
4832 plane
->funcs
->destroy(plane
);
4835 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
4836 crtc
->funcs
->destroy(crtc
);
4839 idr_destroy(&dev
->mode_config
.crtc_idr
);
4840 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
4842 EXPORT_SYMBOL(drm_mode_config_cleanup
);