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 * drm_mode_setplane - configure a plane's configuration
2128 * @data: ioctl data*
2129 * @file_priv: DRM file info
2131 * Set plane configuration, including placement, fb, scaling, and other factors.
2132 * Or pass a NULL fb to disable.
2135 * Zero on success, errno on failure.
2137 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2138 struct drm_file
*file_priv
)
2140 struct drm_mode_set_plane
*plane_req
= data
;
2141 struct drm_plane
*plane
;
2142 struct drm_crtc
*crtc
;
2143 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
2145 unsigned int fb_width
, fb_height
;
2148 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2152 * First, find the plane, crtc, and fb objects. If not available,
2153 * we don't bother to call the driver.
2155 plane
= drm_plane_find(dev
, plane_req
->plane_id
);
2157 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2158 plane_req
->plane_id
);
2162 /* No fb means shut it down */
2163 if (!plane_req
->fb_id
) {
2164 drm_modeset_lock_all(dev
);
2166 ret
= plane
->funcs
->disable_plane(plane
);
2173 drm_modeset_unlock_all(dev
);
2177 crtc
= drm_crtc_find(dev
, plane_req
->crtc_id
);
2179 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2180 plane_req
->crtc_id
);
2185 /* Check whether this plane is usable on this CRTC */
2186 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2187 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2192 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2194 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2200 /* Check whether this plane supports the fb pixel format. */
2201 for (i
= 0; i
< plane
->format_count
; i
++)
2202 if (fb
->pixel_format
== plane
->format_types
[i
])
2204 if (i
== plane
->format_count
) {
2205 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2206 drm_get_format_name(fb
->pixel_format
));
2211 fb_width
= fb
->width
<< 16;
2212 fb_height
= fb
->height
<< 16;
2214 /* Make sure source coordinates are inside the fb. */
2215 if (plane_req
->src_w
> fb_width
||
2216 plane_req
->src_x
> fb_width
- plane_req
->src_w
||
2217 plane_req
->src_h
> fb_height
||
2218 plane_req
->src_y
> fb_height
- plane_req
->src_h
) {
2219 DRM_DEBUG_KMS("Invalid source coordinates "
2220 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2221 plane_req
->src_w
>> 16,
2222 ((plane_req
->src_w
& 0xffff) * 15625) >> 10,
2223 plane_req
->src_h
>> 16,
2224 ((plane_req
->src_h
& 0xffff) * 15625) >> 10,
2225 plane_req
->src_x
>> 16,
2226 ((plane_req
->src_x
& 0xffff) * 15625) >> 10,
2227 plane_req
->src_y
>> 16,
2228 ((plane_req
->src_y
& 0xffff) * 15625) >> 10);
2233 /* Give drivers some help against integer overflows */
2234 if (plane_req
->crtc_w
> INT_MAX
||
2235 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2236 plane_req
->crtc_h
> INT_MAX
||
2237 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2238 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2239 plane_req
->crtc_w
, plane_req
->crtc_h
,
2240 plane_req
->crtc_x
, plane_req
->crtc_y
);
2245 drm_modeset_lock_all(dev
);
2247 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2248 plane_req
->crtc_x
, plane_req
->crtc_y
,
2249 plane_req
->crtc_w
, plane_req
->crtc_h
,
2250 plane_req
->src_x
, plane_req
->src_y
,
2251 plane_req
->src_w
, plane_req
->src_h
);
2259 drm_modeset_unlock_all(dev
);
2263 drm_framebuffer_unreference(fb
);
2265 drm_framebuffer_unreference(old_fb
);
2271 * drm_mode_set_config_internal - helper to call ->set_config
2272 * @set: modeset config to set
2274 * This is a little helper to wrap internal calls to the ->set_config driver
2275 * interface. The only thing it adds is correct refcounting dance.
2278 * Zero on success, errno on failure.
2280 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2282 struct drm_crtc
*crtc
= set
->crtc
;
2283 struct drm_framebuffer
*fb
;
2284 struct drm_crtc
*tmp
;
2288 * NOTE: ->set_config can also disable other crtcs (if we steal all
2289 * connectors from it), hence we need to refcount the fbs across all
2290 * crtcs. Atomic modeset will have saner semantics ...
2292 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2293 tmp
->old_fb
= tmp
->primary
->fb
;
2297 ret
= crtc
->funcs
->set_config(set
);
2299 crtc
->primary
->crtc
= crtc
;
2300 crtc
->primary
->fb
= fb
;
2303 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2304 if (tmp
->primary
->fb
)
2305 drm_framebuffer_reference(tmp
->primary
->fb
);
2307 drm_framebuffer_unreference(tmp
->old_fb
);
2312 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2315 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2317 * @crtc: CRTC that framebuffer will be displayed on
2320 * @mode: mode that framebuffer will be displayed under
2321 * @fb: framebuffer to check size of
2323 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2325 const struct drm_display_mode
*mode
,
2326 const struct drm_framebuffer
*fb
)
2329 int hdisplay
, vdisplay
;
2331 hdisplay
= mode
->hdisplay
;
2332 vdisplay
= mode
->vdisplay
;
2334 if (drm_mode_is_stereo(mode
)) {
2335 struct drm_display_mode adjusted
= *mode
;
2337 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2338 hdisplay
= adjusted
.crtc_hdisplay
;
2339 vdisplay
= adjusted
.crtc_vdisplay
;
2342 if (crtc
->invert_dimensions
)
2343 swap(hdisplay
, vdisplay
);
2345 if (hdisplay
> fb
->width
||
2346 vdisplay
> fb
->height
||
2347 x
> fb
->width
- hdisplay
||
2348 y
> fb
->height
- vdisplay
) {
2349 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2350 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2351 crtc
->invert_dimensions
? " (inverted)" : "");
2357 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2360 * drm_mode_setcrtc - set CRTC configuration
2361 * @dev: drm device for the ioctl
2362 * @data: data pointer for the ioctl
2363 * @file_priv: drm file for the ioctl call
2365 * Build a new CRTC configuration based on user request.
2367 * Called by the user via ioctl.
2370 * Zero on success, errno on failure.
2372 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2373 struct drm_file
*file_priv
)
2375 struct drm_mode_config
*config
= &dev
->mode_config
;
2376 struct drm_mode_crtc
*crtc_req
= data
;
2377 struct drm_crtc
*crtc
;
2378 struct drm_connector
**connector_set
= NULL
, *connector
;
2379 struct drm_framebuffer
*fb
= NULL
;
2380 struct drm_display_mode
*mode
= NULL
;
2381 struct drm_mode_set set
;
2382 uint32_t __user
*set_connectors_ptr
;
2386 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2389 /* For some reason crtc x/y offsets are signed internally. */
2390 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2393 drm_modeset_lock_all(dev
);
2394 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2396 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2400 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2402 if (crtc_req
->mode_valid
) {
2403 /* If we have a mode we need a framebuffer. */
2404 /* If we pass -1, set the mode with the currently bound fb */
2405 if (crtc_req
->fb_id
== -1) {
2406 if (!crtc
->primary
->fb
) {
2407 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2411 fb
= crtc
->primary
->fb
;
2412 /* Make refcounting symmetric with the lookup path. */
2413 drm_framebuffer_reference(fb
);
2415 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2417 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2424 mode
= drm_mode_create(dev
);
2430 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2432 DRM_DEBUG_KMS("Invalid mode\n");
2436 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2438 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2445 if (crtc_req
->count_connectors
== 0 && mode
) {
2446 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2451 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2452 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2453 crtc_req
->count_connectors
);
2458 if (crtc_req
->count_connectors
> 0) {
2461 /* Avoid unbounded kernel memory allocation */
2462 if (crtc_req
->count_connectors
> config
->num_connector
) {
2467 connector_set
= kmalloc(crtc_req
->count_connectors
*
2468 sizeof(struct drm_connector
*),
2470 if (!connector_set
) {
2475 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2476 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2477 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2482 connector
= drm_connector_find(dev
, out_id
);
2484 DRM_DEBUG_KMS("Connector id %d unknown\n",
2489 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2493 connector_set
[i
] = connector
;
2498 set
.x
= crtc_req
->x
;
2499 set
.y
= crtc_req
->y
;
2501 set
.connectors
= connector_set
;
2502 set
.num_connectors
= crtc_req
->count_connectors
;
2504 ret
= drm_mode_set_config_internal(&set
);
2508 drm_framebuffer_unreference(fb
);
2510 kfree(connector_set
);
2511 drm_mode_destroy(dev
, mode
);
2512 drm_modeset_unlock_all(dev
);
2516 static int drm_mode_cursor_common(struct drm_device
*dev
,
2517 struct drm_mode_cursor2
*req
,
2518 struct drm_file
*file_priv
)
2520 struct drm_crtc
*crtc
;
2523 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2526 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2529 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2531 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2535 drm_modeset_lock(&crtc
->mutex
, NULL
);
2536 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2537 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2541 /* Turns off the cursor if handle is 0 */
2542 if (crtc
->funcs
->cursor_set2
)
2543 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2544 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2546 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2547 req
->width
, req
->height
);
2550 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2551 if (crtc
->funcs
->cursor_move
) {
2552 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2559 drm_modeset_unlock(&crtc
->mutex
);
2567 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2568 * @dev: drm device for the ioctl
2569 * @data: data pointer for the ioctl
2570 * @file_priv: drm file for the ioctl call
2572 * Set the cursor configuration based on user request.
2574 * Called by the user via ioctl.
2577 * Zero on success, errno on failure.
2579 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2580 void *data
, struct drm_file
*file_priv
)
2582 struct drm_mode_cursor
*req
= data
;
2583 struct drm_mode_cursor2 new_req
;
2585 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2586 new_req
.hot_x
= new_req
.hot_y
= 0;
2588 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2592 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2593 * @dev: drm device for the ioctl
2594 * @data: data pointer for the ioctl
2595 * @file_priv: drm file for the ioctl call
2597 * Set the cursor configuration based on user request. This implements the 2nd
2598 * version of the cursor ioctl, which allows userspace to additionally specify
2599 * the hotspot of the pointer.
2601 * Called by the user via ioctl.
2604 * Zero on success, errno on failure.
2606 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2607 void *data
, struct drm_file
*file_priv
)
2609 struct drm_mode_cursor2
*req
= data
;
2610 return drm_mode_cursor_common(dev
, req
, file_priv
);
2614 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2615 * @bpp: bits per pixels
2616 * @depth: bit depth per pixel
2618 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2619 * Useful in fbdev emulation code, since that deals in those values.
2621 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2627 fmt
= DRM_FORMAT_C8
;
2631 fmt
= DRM_FORMAT_XRGB1555
;
2633 fmt
= DRM_FORMAT_RGB565
;
2636 fmt
= DRM_FORMAT_RGB888
;
2640 fmt
= DRM_FORMAT_XRGB8888
;
2641 else if (depth
== 30)
2642 fmt
= DRM_FORMAT_XRGB2101010
;
2644 fmt
= DRM_FORMAT_ARGB8888
;
2647 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2648 fmt
= DRM_FORMAT_XRGB8888
;
2654 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2657 * drm_mode_addfb - add an FB to the graphics configuration
2658 * @dev: drm device for the ioctl
2659 * @data: data pointer for the ioctl
2660 * @file_priv: drm file for the ioctl call
2662 * Add a new FB to the specified CRTC, given a user request. This is the
2663 * original addfb ioclt which only supported RGB formats.
2665 * Called by the user via ioctl.
2668 * Zero on success, errno on failure.
2670 int drm_mode_addfb(struct drm_device
*dev
,
2671 void *data
, struct drm_file
*file_priv
)
2673 struct drm_mode_fb_cmd
*or = data
;
2674 struct drm_mode_fb_cmd2 r
= {};
2675 struct drm_mode_config
*config
= &dev
->mode_config
;
2676 struct drm_framebuffer
*fb
;
2679 /* Use new struct with format internally */
2680 r
.fb_id
= or->fb_id
;
2681 r
.width
= or->width
;
2682 r
.height
= or->height
;
2683 r
.pitches
[0] = or->pitch
;
2684 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2685 r
.handles
[0] = or->handle
;
2687 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2690 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2693 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2696 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2698 DRM_DEBUG_KMS("could not create framebuffer\n");
2702 mutex_lock(&file_priv
->fbs_lock
);
2703 or->fb_id
= fb
->base
.id
;
2704 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2705 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2706 mutex_unlock(&file_priv
->fbs_lock
);
2711 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2713 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2717 case DRM_FORMAT_RGB332
:
2718 case DRM_FORMAT_BGR233
:
2719 case DRM_FORMAT_XRGB4444
:
2720 case DRM_FORMAT_XBGR4444
:
2721 case DRM_FORMAT_RGBX4444
:
2722 case DRM_FORMAT_BGRX4444
:
2723 case DRM_FORMAT_ARGB4444
:
2724 case DRM_FORMAT_ABGR4444
:
2725 case DRM_FORMAT_RGBA4444
:
2726 case DRM_FORMAT_BGRA4444
:
2727 case DRM_FORMAT_XRGB1555
:
2728 case DRM_FORMAT_XBGR1555
:
2729 case DRM_FORMAT_RGBX5551
:
2730 case DRM_FORMAT_BGRX5551
:
2731 case DRM_FORMAT_ARGB1555
:
2732 case DRM_FORMAT_ABGR1555
:
2733 case DRM_FORMAT_RGBA5551
:
2734 case DRM_FORMAT_BGRA5551
:
2735 case DRM_FORMAT_RGB565
:
2736 case DRM_FORMAT_BGR565
:
2737 case DRM_FORMAT_RGB888
:
2738 case DRM_FORMAT_BGR888
:
2739 case DRM_FORMAT_XRGB8888
:
2740 case DRM_FORMAT_XBGR8888
:
2741 case DRM_FORMAT_RGBX8888
:
2742 case DRM_FORMAT_BGRX8888
:
2743 case DRM_FORMAT_ARGB8888
:
2744 case DRM_FORMAT_ABGR8888
:
2745 case DRM_FORMAT_RGBA8888
:
2746 case DRM_FORMAT_BGRA8888
:
2747 case DRM_FORMAT_XRGB2101010
:
2748 case DRM_FORMAT_XBGR2101010
:
2749 case DRM_FORMAT_RGBX1010102
:
2750 case DRM_FORMAT_BGRX1010102
:
2751 case DRM_FORMAT_ARGB2101010
:
2752 case DRM_FORMAT_ABGR2101010
:
2753 case DRM_FORMAT_RGBA1010102
:
2754 case DRM_FORMAT_BGRA1010102
:
2755 case DRM_FORMAT_YUYV
:
2756 case DRM_FORMAT_YVYU
:
2757 case DRM_FORMAT_UYVY
:
2758 case DRM_FORMAT_VYUY
:
2759 case DRM_FORMAT_AYUV
:
2760 case DRM_FORMAT_NV12
:
2761 case DRM_FORMAT_NV21
:
2762 case DRM_FORMAT_NV16
:
2763 case DRM_FORMAT_NV61
:
2764 case DRM_FORMAT_NV24
:
2765 case DRM_FORMAT_NV42
:
2766 case DRM_FORMAT_YUV410
:
2767 case DRM_FORMAT_YVU410
:
2768 case DRM_FORMAT_YUV411
:
2769 case DRM_FORMAT_YVU411
:
2770 case DRM_FORMAT_YUV420
:
2771 case DRM_FORMAT_YVU420
:
2772 case DRM_FORMAT_YUV422
:
2773 case DRM_FORMAT_YVU422
:
2774 case DRM_FORMAT_YUV444
:
2775 case DRM_FORMAT_YVU444
:
2778 DRM_DEBUG_KMS("invalid pixel format %s\n",
2779 drm_get_format_name(r
->pixel_format
));
2784 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2786 int ret
, hsub
, vsub
, num_planes
, i
;
2788 ret
= format_check(r
);
2790 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2791 drm_get_format_name(r
->pixel_format
));
2795 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2796 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2797 num_planes
= drm_format_num_planes(r
->pixel_format
);
2799 if (r
->width
== 0 || r
->width
% hsub
) {
2800 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2804 if (r
->height
== 0 || r
->height
% vsub
) {
2805 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2809 for (i
= 0; i
< num_planes
; i
++) {
2810 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2811 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
2812 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
2814 if (!r
->handles
[i
]) {
2815 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2819 if ((uint64_t) width
* cpp
> UINT_MAX
)
2822 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
2825 if (r
->pitches
[i
] < width
* cpp
) {
2826 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2834 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
2835 struct drm_mode_fb_cmd2
*r
,
2836 struct drm_file
*file_priv
)
2838 struct drm_mode_config
*config
= &dev
->mode_config
;
2839 struct drm_framebuffer
*fb
;
2842 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
2843 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
2844 return ERR_PTR(-EINVAL
);
2847 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2848 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2849 r
->width
, config
->min_width
, config
->max_width
);
2850 return ERR_PTR(-EINVAL
);
2852 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2853 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2854 r
->height
, config
->min_height
, config
->max_height
);
2855 return ERR_PTR(-EINVAL
);
2858 ret
= framebuffer_check(r
);
2860 return ERR_PTR(ret
);
2862 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
2864 DRM_DEBUG_KMS("could not create framebuffer\n");
2868 mutex_lock(&file_priv
->fbs_lock
);
2869 r
->fb_id
= fb
->base
.id
;
2870 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2871 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2872 mutex_unlock(&file_priv
->fbs_lock
);
2878 * drm_mode_addfb2 - add an FB to the graphics configuration
2879 * @dev: drm device for the ioctl
2880 * @data: data pointer for the ioctl
2881 * @file_priv: drm file for the ioctl call
2883 * Add a new FB to the specified CRTC, given a user request with format. This is
2884 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2885 * and uses fourcc codes as pixel format specifiers.
2887 * Called by the user via ioctl.
2890 * Zero on success, errno on failure.
2892 int drm_mode_addfb2(struct drm_device
*dev
,
2893 void *data
, struct drm_file
*file_priv
)
2895 struct drm_framebuffer
*fb
;
2897 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2900 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
2908 * drm_mode_rmfb - remove an FB from the configuration
2909 * @dev: drm device for the ioctl
2910 * @data: data pointer for the ioctl
2911 * @file_priv: drm file for the ioctl call
2913 * Remove the FB specified by the user.
2915 * Called by the user via ioctl.
2918 * Zero on success, errno on failure.
2920 int drm_mode_rmfb(struct drm_device
*dev
,
2921 void *data
, struct drm_file
*file_priv
)
2923 struct drm_framebuffer
*fb
= NULL
;
2924 struct drm_framebuffer
*fbl
= NULL
;
2925 uint32_t *id
= data
;
2928 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2931 mutex_lock(&file_priv
->fbs_lock
);
2932 mutex_lock(&dev
->mode_config
.fb_lock
);
2933 fb
= __drm_framebuffer_lookup(dev
, *id
);
2937 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
2943 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2944 __drm_framebuffer_unregister(dev
, fb
);
2946 list_del_init(&fb
->filp_head
);
2947 mutex_unlock(&dev
->mode_config
.fb_lock
);
2948 mutex_unlock(&file_priv
->fbs_lock
);
2950 drm_framebuffer_remove(fb
);
2955 mutex_unlock(&dev
->mode_config
.fb_lock
);
2956 mutex_unlock(&file_priv
->fbs_lock
);
2962 * drm_mode_getfb - get FB info
2963 * @dev: drm device for the ioctl
2964 * @data: data pointer for the ioctl
2965 * @file_priv: drm file for the ioctl call
2967 * Lookup the FB given its ID and return info about it.
2969 * Called by the user via ioctl.
2972 * Zero on success, errno on failure.
2974 int drm_mode_getfb(struct drm_device
*dev
,
2975 void *data
, struct drm_file
*file_priv
)
2977 struct drm_mode_fb_cmd
*r
= data
;
2978 struct drm_framebuffer
*fb
;
2981 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2984 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
2988 r
->height
= fb
->height
;
2989 r
->width
= fb
->width
;
2990 r
->depth
= fb
->depth
;
2991 r
->bpp
= fb
->bits_per_pixel
;
2992 r
->pitch
= fb
->pitches
[0];
2993 if (fb
->funcs
->create_handle
) {
2994 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
2995 drm_is_control_client(file_priv
)) {
2996 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
2999 /* GET_FB() is an unprivileged ioctl so we must not
3000 * return a buffer-handle to non-master processes! For
3001 * backwards-compatibility reasons, we cannot make
3002 * GET_FB() privileged, so just return an invalid handle
3003 * for non-masters. */
3011 drm_framebuffer_unreference(fb
);
3017 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3018 * @dev: drm device for the ioctl
3019 * @data: data pointer for the ioctl
3020 * @file_priv: drm file for the ioctl call
3022 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3023 * rectangle list. Generic userspace which does frontbuffer rendering must call
3024 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3025 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3027 * Modesetting drivers which always update the frontbuffer do not need to
3028 * implement the corresponding ->dirty framebuffer callback.
3030 * Called by the user via ioctl.
3033 * Zero on success, errno on failure.
3035 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3036 void *data
, struct drm_file
*file_priv
)
3038 struct drm_clip_rect __user
*clips_ptr
;
3039 struct drm_clip_rect
*clips
= NULL
;
3040 struct drm_mode_fb_dirty_cmd
*r
= data
;
3041 struct drm_framebuffer
*fb
;
3046 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3049 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3053 num_clips
= r
->num_clips
;
3054 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3056 if (!num_clips
!= !clips_ptr
) {
3061 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3063 /* If userspace annotates copy, clips must come in pairs */
3064 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3069 if (num_clips
&& clips_ptr
) {
3070 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3074 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3080 ret
= copy_from_user(clips
, clips_ptr
,
3081 num_clips
* sizeof(*clips
));
3088 if (fb
->funcs
->dirty
) {
3089 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3098 drm_framebuffer_unreference(fb
);
3105 * drm_fb_release - remove and free the FBs on this file
3106 * @priv: drm file for the ioctl
3108 * Destroy all the FBs associated with @filp.
3110 * Called by the user via ioctl.
3113 * Zero on success, errno on failure.
3115 void drm_fb_release(struct drm_file
*priv
)
3117 struct drm_device
*dev
= priv
->minor
->dev
;
3118 struct drm_framebuffer
*fb
, *tfb
;
3120 mutex_lock(&priv
->fbs_lock
);
3121 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3123 mutex_lock(&dev
->mode_config
.fb_lock
);
3124 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3125 __drm_framebuffer_unregister(dev
, fb
);
3126 mutex_unlock(&dev
->mode_config
.fb_lock
);
3128 list_del_init(&fb
->filp_head
);
3130 /* This will also drop the fpriv->fbs reference. */
3131 drm_framebuffer_remove(fb
);
3133 mutex_unlock(&priv
->fbs_lock
);
3137 * drm_property_create - create a new property type
3139 * @flags: flags specifying the property type
3140 * @name: name of the property
3141 * @num_values: number of pre-defined values
3143 * This creates a new generic drm property which can then be attached to a drm
3144 * object with drm_object_attach_property. The returned property object must be
3145 * freed with drm_property_destroy.
3148 * A pointer to the newly created property on success, NULL on failure.
3150 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3151 const char *name
, int num_values
)
3153 struct drm_property
*property
= NULL
;
3156 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3160 property
->dev
= dev
;
3163 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3164 if (!property
->values
)
3168 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3172 property
->flags
= flags
;
3173 property
->num_values
= num_values
;
3174 INIT_LIST_HEAD(&property
->enum_blob_list
);
3177 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3178 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3181 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3183 WARN_ON(!drm_property_type_valid(property
));
3187 kfree(property
->values
);
3191 EXPORT_SYMBOL(drm_property_create
);
3194 * drm_property_create - create a new enumeration property type
3196 * @flags: flags specifying the property type
3197 * @name: name of the property
3198 * @props: enumeration lists with property values
3199 * @num_values: number of pre-defined values
3201 * This creates a new generic drm property which can then be attached to a drm
3202 * object with drm_object_attach_property. The returned property object must be
3203 * freed with drm_property_destroy.
3205 * Userspace is only allowed to set one of the predefined values for enumeration
3209 * A pointer to the newly created property on success, NULL on failure.
3211 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3213 const struct drm_prop_enum_list
*props
,
3216 struct drm_property
*property
;
3219 flags
|= DRM_MODE_PROP_ENUM
;
3221 property
= drm_property_create(dev
, flags
, name
, num_values
);
3225 for (i
= 0; i
< num_values
; i
++) {
3226 ret
= drm_property_add_enum(property
, i
,
3230 drm_property_destroy(dev
, property
);
3237 EXPORT_SYMBOL(drm_property_create_enum
);
3240 * drm_property_create - create a new bitmask property type
3242 * @flags: flags specifying the property type
3243 * @name: name of the property
3244 * @props: enumeration lists with property bitflags
3245 * @num_values: number of pre-defined values
3247 * This creates a new generic drm property which can then be attached to a drm
3248 * object with drm_object_attach_property. The returned property object must be
3249 * freed with drm_property_destroy.
3251 * Compared to plain enumeration properties userspace is allowed to set any
3252 * or'ed together combination of the predefined property bitflag values
3255 * A pointer to the newly created property on success, NULL on failure.
3257 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3258 int flags
, const char *name
,
3259 const struct drm_prop_enum_list
*props
,
3262 struct drm_property
*property
;
3265 flags
|= DRM_MODE_PROP_BITMASK
;
3267 property
= drm_property_create(dev
, flags
, name
, num_values
);
3271 for (i
= 0; i
< num_values
; i
++) {
3272 ret
= drm_property_add_enum(property
, i
,
3276 drm_property_destroy(dev
, property
);
3283 EXPORT_SYMBOL(drm_property_create_bitmask
);
3285 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3286 int flags
, const char *name
,
3287 uint64_t min
, uint64_t max
)
3289 struct drm_property
*property
;
3291 property
= drm_property_create(dev
, flags
, name
, 2);
3295 property
->values
[0] = min
;
3296 property
->values
[1] = max
;
3302 * drm_property_create - create a new ranged property type
3304 * @flags: flags specifying the property type
3305 * @name: name of the property
3306 * @min: minimum value of the property
3307 * @max: maximum value of the property
3309 * This creates a new generic drm property which can then be attached to a drm
3310 * object with drm_object_attach_property. The returned property object must be
3311 * freed with drm_property_destroy.
3313 * Userspace is allowed to set any interger value in the (min, max) range
3317 * A pointer to the newly created property on success, NULL on failure.
3319 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3321 uint64_t min
, uint64_t max
)
3323 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3326 EXPORT_SYMBOL(drm_property_create_range
);
3328 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3329 int flags
, const char *name
,
3330 int64_t min
, int64_t max
)
3332 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3333 name
, I642U64(min
), I642U64(max
));
3335 EXPORT_SYMBOL(drm_property_create_signed_range
);
3337 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3338 int flags
, const char *name
, uint32_t type
)
3340 struct drm_property
*property
;
3342 flags
|= DRM_MODE_PROP_OBJECT
;
3344 property
= drm_property_create(dev
, flags
, name
, 1);
3348 property
->values
[0] = type
;
3352 EXPORT_SYMBOL(drm_property_create_object
);
3355 * drm_property_add_enum - add a possible value to an enumeration property
3356 * @property: enumeration property to change
3357 * @index: index of the new enumeration
3358 * @value: value of the new enumeration
3359 * @name: symbolic name of the new enumeration
3361 * This functions adds enumerations to a property.
3363 * It's use is deprecated, drivers should use one of the more specific helpers
3364 * to directly create the property with all enumerations already attached.
3367 * Zero on success, error code on failure.
3369 int drm_property_add_enum(struct drm_property
*property
, int index
,
3370 uint64_t value
, const char *name
)
3372 struct drm_property_enum
*prop_enum
;
3374 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3375 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3379 * Bitmask enum properties have the additional constraint of values
3382 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3386 if (!list_empty(&property
->enum_blob_list
)) {
3387 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3388 if (prop_enum
->value
== value
) {
3389 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3390 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3396 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3400 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3401 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3402 prop_enum
->value
= value
;
3404 property
->values
[index
] = value
;
3405 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3408 EXPORT_SYMBOL(drm_property_add_enum
);
3411 * drm_property_destroy - destroy a drm property
3413 * @property: property to destry
3415 * This function frees a property including any attached resources like
3416 * enumeration values.
3418 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3420 struct drm_property_enum
*prop_enum
, *pt
;
3422 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3423 list_del(&prop_enum
->head
);
3427 if (property
->num_values
)
3428 kfree(property
->values
);
3429 drm_mode_object_put(dev
, &property
->base
);
3430 list_del(&property
->head
);
3433 EXPORT_SYMBOL(drm_property_destroy
);
3436 * drm_object_attach_property - attach a property to a modeset object
3437 * @obj: drm modeset object
3438 * @property: property to attach
3439 * @init_val: initial value of the property
3441 * This attaches the given property to the modeset object with the given initial
3442 * value. Currently this function cannot fail since the properties are stored in
3443 * a statically sized array.
3445 void drm_object_attach_property(struct drm_mode_object
*obj
,
3446 struct drm_property
*property
,
3449 int count
= obj
->properties
->count
;
3451 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3452 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3453 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3454 "you see this message on the same object type.\n",
3459 obj
->properties
->ids
[count
] = property
->base
.id
;
3460 obj
->properties
->values
[count
] = init_val
;
3461 obj
->properties
->count
++;
3463 EXPORT_SYMBOL(drm_object_attach_property
);
3466 * drm_object_property_set_value - set the value of a property
3467 * @obj: drm mode object to set property value for
3468 * @property: property to set
3469 * @val: value the property should be set to
3471 * This functions sets a given property on a given object. This function only
3472 * changes the software state of the property, it does not call into the
3473 * driver's ->set_property callback.
3476 * Zero on success, error code on failure.
3478 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3479 struct drm_property
*property
, uint64_t val
)
3483 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3484 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3485 obj
->properties
->values
[i
] = val
;
3492 EXPORT_SYMBOL(drm_object_property_set_value
);
3495 * drm_object_property_get_value - retrieve the value of a property
3496 * @obj: drm mode object to get property value from
3497 * @property: property to retrieve
3498 * @val: storage for the property value
3500 * This function retrieves the softare state of the given property for the given
3501 * property. Since there is no driver callback to retrieve the current property
3502 * value this might be out of sync with the hardware, depending upon the driver
3506 * Zero on success, error code on failure.
3508 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3509 struct drm_property
*property
, uint64_t *val
)
3513 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3514 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3515 *val
= obj
->properties
->values
[i
];
3522 EXPORT_SYMBOL(drm_object_property_get_value
);
3525 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3528 * @file_priv: DRM file info
3530 * This function retrieves the current value for an connectors's property.
3532 * Called by the user via ioctl.
3535 * Zero on success, errno on failure.
3537 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3538 void *data
, struct drm_file
*file_priv
)
3540 struct drm_mode_get_property
*out_resp
= data
;
3541 struct drm_property
*property
;
3544 int value_count
= 0;
3547 struct drm_property_enum
*prop_enum
;
3548 struct drm_mode_property_enum __user
*enum_ptr
;
3549 struct drm_property_blob
*prop_blob
;
3550 uint32_t __user
*blob_id_ptr
;
3551 uint64_t __user
*values_ptr
;
3552 uint32_t __user
*blob_length_ptr
;
3554 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3557 drm_modeset_lock_all(dev
);
3558 property
= drm_property_find(dev
, out_resp
->prop_id
);
3564 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3565 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3566 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3568 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3569 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3573 value_count
= property
->num_values
;
3575 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3576 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3577 out_resp
->flags
= property
->flags
;
3579 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3580 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3581 for (i
= 0; i
< value_count
; i
++) {
3582 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3588 out_resp
->count_values
= value_count
;
3590 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3591 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3592 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3594 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3595 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3597 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3602 if (copy_to_user(&enum_ptr
[copied
].name
,
3603 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3610 out_resp
->count_enum_blobs
= enum_count
;
3613 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3614 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3616 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3617 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3619 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3620 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3625 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3633 out_resp
->count_enum_blobs
= blob_count
;
3636 drm_modeset_unlock_all(dev
);
3640 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3643 struct drm_property_blob
*blob
;
3646 if (!length
|| !data
)
3649 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3653 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3659 blob
->length
= length
;
3661 memcpy(blob
->data
, data
, length
);
3663 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3667 static void drm_property_destroy_blob(struct drm_device
*dev
,
3668 struct drm_property_blob
*blob
)
3670 drm_mode_object_put(dev
, &blob
->base
);
3671 list_del(&blob
->head
);
3676 * drm_mode_getblob_ioctl - get the contents of a blob property value
3679 * @file_priv: DRM file info
3681 * This function retrieves the contents of a blob property. The value stored in
3682 * an object's blob property is just a normal modeset object id.
3684 * Called by the user via ioctl.
3687 * Zero on success, errno on failure.
3689 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3690 void *data
, struct drm_file
*file_priv
)
3692 struct drm_mode_get_blob
*out_resp
= data
;
3693 struct drm_property_blob
*blob
;
3695 void __user
*blob_ptr
;
3697 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3700 drm_modeset_lock_all(dev
);
3701 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3707 if (out_resp
->length
== blob
->length
) {
3708 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3709 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3714 out_resp
->length
= blob
->length
;
3717 drm_modeset_unlock_all(dev
);
3722 * drm_mode_connector_update_edid_property - update the edid property of a connector
3723 * @connector: drm connector
3724 * @edid: new value of the edid property
3726 * This function creates a new blob modeset object and assigns its id to the
3727 * connector's edid property.
3730 * Zero on success, errno on failure.
3732 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3735 struct drm_device
*dev
= connector
->dev
;
3738 if (connector
->edid_blob_ptr
)
3739 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3741 /* Delete edid, when there is none. */
3743 connector
->edid_blob_ptr
= NULL
;
3744 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3748 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3749 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3751 if (!connector
->edid_blob_ptr
)
3754 ret
= drm_object_property_set_value(&connector
->base
,
3755 dev
->mode_config
.edid_property
,
3756 connector
->edid_blob_ptr
->base
.id
);
3760 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3762 static bool drm_property_change_is_valid(struct drm_property
*property
,
3765 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3768 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3769 if (value
< property
->values
[0] || value
> property
->values
[1])
3772 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3773 int64_t svalue
= U642I64(value
);
3774 if (svalue
< U642I64(property
->values
[0]) ||
3775 svalue
> U642I64(property
->values
[1]))
3778 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3780 uint64_t valid_mask
= 0;
3781 for (i
= 0; i
< property
->num_values
; i
++)
3782 valid_mask
|= (1ULL << property
->values
[i
]);
3783 return !(value
& ~valid_mask
);
3784 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3785 /* Only the driver knows */
3787 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3788 struct drm_mode_object
*obj
;
3789 /* a zero value for an object property translates to null: */
3793 * NOTE: use _object_find() directly to bypass restriction on
3794 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3795 * object this could race against object finalization, so it
3796 * simply tells us that the object *was* valid. Which is good
3799 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
3803 for (i
= 0; i
< property
->num_values
; i
++)
3804 if (property
->values
[i
] == value
)
3811 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3814 * @file_priv: DRM file info
3816 * This function sets the current value for a connectors's property. It also
3817 * calls into a driver's ->set_property callback to update the hardware state
3819 * Called by the user via ioctl.
3822 * Zero on success, errno on failure.
3824 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3825 void *data
, struct drm_file
*file_priv
)
3827 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3828 struct drm_mode_obj_set_property obj_set_prop
= {
3829 .value
= conn_set_prop
->value
,
3830 .prop_id
= conn_set_prop
->prop_id
,
3831 .obj_id
= conn_set_prop
->connector_id
,
3832 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3835 /* It does all the locking and checking we need */
3836 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3839 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3840 struct drm_property
*property
,
3844 struct drm_connector
*connector
= obj_to_connector(obj
);
3846 /* Do DPMS ourselves */
3847 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3848 if (connector
->funcs
->dpms
)
3849 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3851 } else if (connector
->funcs
->set_property
)
3852 ret
= connector
->funcs
->set_property(connector
, property
, value
);
3854 /* store the property value if successful */
3856 drm_object_property_set_value(&connector
->base
, property
, value
);
3860 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
3861 struct drm_property
*property
,
3865 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
3867 if (crtc
->funcs
->set_property
)
3868 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
3870 drm_object_property_set_value(obj
, property
, value
);
3875 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
3876 struct drm_property
*property
,
3880 struct drm_plane
*plane
= obj_to_plane(obj
);
3882 if (plane
->funcs
->set_property
)
3883 ret
= plane
->funcs
->set_property(plane
, property
, value
);
3885 drm_object_property_set_value(obj
, property
, value
);
3891 * drm_mode_getproperty_ioctl - get the current value of a object's property
3894 * @file_priv: DRM file info
3896 * This function retrieves the current value for an object's property. Compared
3897 * to the connector specific ioctl this one is extended to also work on crtc and
3900 * Called by the user via ioctl.
3903 * Zero on success, errno on failure.
3905 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
3906 struct drm_file
*file_priv
)
3908 struct drm_mode_obj_get_properties
*arg
= data
;
3909 struct drm_mode_object
*obj
;
3913 int props_count
= 0;
3914 uint32_t __user
*props_ptr
;
3915 uint64_t __user
*prop_values_ptr
;
3917 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3920 drm_modeset_lock_all(dev
);
3922 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3927 if (!obj
->properties
) {
3932 props_count
= obj
->properties
->count
;
3934 /* This ioctl is called twice, once to determine how much space is
3935 * needed, and the 2nd time to fill it. */
3936 if ((arg
->count_props
>= props_count
) && props_count
) {
3938 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
3939 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
3940 (arg
->prop_values_ptr
);
3941 for (i
= 0; i
< props_count
; i
++) {
3942 if (put_user(obj
->properties
->ids
[i
],
3943 props_ptr
+ copied
)) {
3947 if (put_user(obj
->properties
->values
[i
],
3948 prop_values_ptr
+ copied
)) {
3955 arg
->count_props
= props_count
;
3957 drm_modeset_unlock_all(dev
);
3962 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3965 * @file_priv: DRM file info
3967 * This function sets the current value for an object's property. It also calls
3968 * into a driver's ->set_property callback to update the hardware state.
3969 * Compared to the connector specific ioctl this one is extended to also work on
3970 * crtc and plane objects.
3972 * Called by the user via ioctl.
3975 * Zero on success, errno on failure.
3977 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
3978 struct drm_file
*file_priv
)
3980 struct drm_mode_obj_set_property
*arg
= data
;
3981 struct drm_mode_object
*arg_obj
;
3982 struct drm_mode_object
*prop_obj
;
3983 struct drm_property
*property
;
3987 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3990 drm_modeset_lock_all(dev
);
3992 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
3997 if (!arg_obj
->properties
)
4000 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4001 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4004 if (i
== arg_obj
->properties
->count
)
4007 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4008 DRM_MODE_OBJECT_PROPERTY
);
4013 property
= obj_to_property(prop_obj
);
4015 if (!drm_property_change_is_valid(property
, arg
->value
))
4018 switch (arg_obj
->type
) {
4019 case DRM_MODE_OBJECT_CONNECTOR
:
4020 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4023 case DRM_MODE_OBJECT_CRTC
:
4024 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4026 case DRM_MODE_OBJECT_PLANE
:
4027 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4032 drm_modeset_unlock_all(dev
);
4037 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4038 * @connector: connector to attach
4039 * @encoder: encoder to attach @connector to
4041 * This function links up a connector to an encoder. Note that the routing
4042 * restrictions between encoders and crtcs are exposed to userspace through the
4043 * possible_clones and possible_crtcs bitmasks.
4046 * Zero on success, errno on failure.
4048 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4049 struct drm_encoder
*encoder
)
4053 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4054 if (connector
->encoder_ids
[i
] == 0) {
4055 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4061 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4064 * drm_mode_crtc_set_gamma_size - set the gamma table size
4065 * @crtc: CRTC to set the gamma table size for
4066 * @gamma_size: size of the gamma table
4068 * Drivers which support gamma tables should set this to the supported gamma
4069 * table size when initializing the CRTC. Currently the drm core only supports a
4070 * fixed gamma table size.
4073 * Zero on success, errno on failure.
4075 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4078 crtc
->gamma_size
= gamma_size
;
4080 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4081 if (!crtc
->gamma_store
) {
4082 crtc
->gamma_size
= 0;
4088 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4091 * drm_mode_gamma_set_ioctl - set the gamma table
4094 * @file_priv: DRM file info
4096 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4097 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4099 * Called by the user via ioctl.
4102 * Zero on success, errno on failure.
4104 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4105 void *data
, struct drm_file
*file_priv
)
4107 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4108 struct drm_crtc
*crtc
;
4109 void *r_base
, *g_base
, *b_base
;
4113 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4116 drm_modeset_lock_all(dev
);
4117 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4123 if (crtc
->funcs
->gamma_set
== NULL
) {
4128 /* memcpy into gamma store */
4129 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4134 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4135 r_base
= crtc
->gamma_store
;
4136 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4141 g_base
= r_base
+ size
;
4142 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4147 b_base
= g_base
+ size
;
4148 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4153 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4156 drm_modeset_unlock_all(dev
);
4162 * drm_mode_gamma_get_ioctl - get the gamma table
4165 * @file_priv: DRM file info
4167 * Copy the current gamma table into the storage provided. This also provides
4168 * the gamma table size the driver expects, which can be used to size the
4169 * allocated storage.
4171 * Called by the user via ioctl.
4174 * Zero on success, errno on failure.
4176 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4177 void *data
, struct drm_file
*file_priv
)
4179 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4180 struct drm_crtc
*crtc
;
4181 void *r_base
, *g_base
, *b_base
;
4185 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4188 drm_modeset_lock_all(dev
);
4189 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4195 /* memcpy into gamma store */
4196 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4201 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4202 r_base
= crtc
->gamma_store
;
4203 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4208 g_base
= r_base
+ size
;
4209 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4214 b_base
= g_base
+ size
;
4215 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4220 drm_modeset_unlock_all(dev
);
4225 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4228 * @file_priv: DRM file info
4230 * This schedules an asynchronous update on a given CRTC, called page flip.
4231 * Optionally a drm event is generated to signal the completion of the event.
4232 * Generic drivers cannot assume that a pageflip with changed framebuffer
4233 * properties (including driver specific metadata like tiling layout) will work,
4234 * but some drivers support e.g. pixel format changes through the pageflip
4237 * Called by the user via ioctl.
4240 * Zero on success, errno on failure.
4242 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4243 void *data
, struct drm_file
*file_priv
)
4245 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4246 struct drm_crtc
*crtc
;
4247 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4248 struct drm_pending_vblank_event
*e
= NULL
;
4249 unsigned long flags
;
4252 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4253 page_flip
->reserved
!= 0)
4256 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4259 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4263 drm_modeset_lock(&crtc
->mutex
, NULL
);
4264 if (crtc
->primary
->fb
== NULL
) {
4265 /* The framebuffer is currently unbound, presumably
4266 * due to a hotplug event, that userspace has not
4273 if (crtc
->funcs
->page_flip
== NULL
)
4276 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4282 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4286 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4287 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4292 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4294 spin_lock_irqsave(&dev
->event_lock
, flags
);
4295 if (file_priv
->event_space
< sizeof e
->event
) {
4296 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4299 file_priv
->event_space
-= sizeof e
->event
;
4300 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4302 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4304 spin_lock_irqsave(&dev
->event_lock
, flags
);
4305 file_priv
->event_space
+= sizeof e
->event
;
4306 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4310 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4311 e
->event
.base
.length
= sizeof e
->event
;
4312 e
->event
.user_data
= page_flip
->user_data
;
4313 e
->base
.event
= &e
->event
.base
;
4314 e
->base
.file_priv
= file_priv
;
4316 (void (*) (struct drm_pending_event
*)) kfree
;
4319 old_fb
= crtc
->primary
->fb
;
4320 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4322 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4323 spin_lock_irqsave(&dev
->event_lock
, flags
);
4324 file_priv
->event_space
+= sizeof e
->event
;
4325 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4328 /* Keep the old fb, don't unref it. */
4332 * Warn if the driver hasn't properly updated the crtc->fb
4333 * field to reflect that the new framebuffer is now used.
4334 * Failing to do so will screw with the reference counting
4337 WARN_ON(crtc
->primary
->fb
!= fb
);
4338 /* Unref only the old framebuffer. */
4344 drm_framebuffer_unreference(fb
);
4346 drm_framebuffer_unreference(old_fb
);
4347 drm_modeset_unlock(&crtc
->mutex
);
4353 * drm_mode_config_reset - call ->reset callbacks
4356 * This functions calls all the crtc's, encoder's and connector's ->reset
4357 * callback. Drivers can use this in e.g. their driver load or resume code to
4358 * reset hardware and software state.
4360 void drm_mode_config_reset(struct drm_device
*dev
)
4362 struct drm_crtc
*crtc
;
4363 struct drm_encoder
*encoder
;
4364 struct drm_connector
*connector
;
4366 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4367 if (crtc
->funcs
->reset
)
4368 crtc
->funcs
->reset(crtc
);
4370 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4371 if (encoder
->funcs
->reset
)
4372 encoder
->funcs
->reset(encoder
);
4374 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4375 connector
->status
= connector_status_unknown
;
4377 if (connector
->funcs
->reset
)
4378 connector
->funcs
->reset(connector
);
4381 EXPORT_SYMBOL(drm_mode_config_reset
);
4384 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4387 * @file_priv: DRM file info
4389 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4390 * TTM or something else entirely) and returns the resulting buffer handle. This
4391 * handle can then be wrapped up into a framebuffer modeset object.
4393 * Note that userspace is not allowed to use such objects for render
4394 * acceleration - drivers must create their own private ioctls for such a use
4397 * Called by the user via ioctl.
4400 * Zero on success, errno on failure.
4402 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4403 void *data
, struct drm_file
*file_priv
)
4405 struct drm_mode_create_dumb
*args
= data
;
4406 u32 cpp
, stride
, size
;
4408 if (!dev
->driver
->dumb_create
)
4410 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4413 /* overflow checks for 32bit size calculations */
4414 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4415 if (cpp
> 0xffffffffU
/ args
->width
)
4417 stride
= cpp
* args
->width
;
4418 if (args
->height
> 0xffffffffU
/ stride
)
4421 /* test for wrap-around */
4422 size
= args
->height
* stride
;
4423 if (PAGE_ALIGN(size
) == 0)
4426 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4430 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4433 * @file_priv: DRM file info
4435 * Allocate an offset in the drm device node's address space to be able to
4436 * memory map a dumb buffer.
4438 * Called by the user via ioctl.
4441 * Zero on success, errno on failure.
4443 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4444 void *data
, struct drm_file
*file_priv
)
4446 struct drm_mode_map_dumb
*args
= data
;
4448 /* call driver ioctl to get mmap offset */
4449 if (!dev
->driver
->dumb_map_offset
)
4452 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4456 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4459 * @file_priv: DRM file info
4461 * This destroys the userspace handle for the given dumb backing storage buffer.
4462 * Since buffer objects must be reference counted in the kernel a buffer object
4463 * won't be immediately freed if a framebuffer modeset object still uses it.
4465 * Called by the user via ioctl.
4468 * Zero on success, errno on failure.
4470 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4471 void *data
, struct drm_file
*file_priv
)
4473 struct drm_mode_destroy_dumb
*args
= data
;
4475 if (!dev
->driver
->dumb_destroy
)
4478 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4482 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4483 * @format: pixel format (DRM_FORMAT_*)
4484 * @depth: storage for the depth value
4485 * @bpp: storage for the bpp value
4487 * This only supports RGB formats here for compat with code that doesn't use
4488 * pixel formats directly yet.
4490 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4495 case DRM_FORMAT_RGB332
:
4496 case DRM_FORMAT_BGR233
:
4500 case DRM_FORMAT_XRGB1555
:
4501 case DRM_FORMAT_XBGR1555
:
4502 case DRM_FORMAT_RGBX5551
:
4503 case DRM_FORMAT_BGRX5551
:
4504 case DRM_FORMAT_ARGB1555
:
4505 case DRM_FORMAT_ABGR1555
:
4506 case DRM_FORMAT_RGBA5551
:
4507 case DRM_FORMAT_BGRA5551
:
4511 case DRM_FORMAT_RGB565
:
4512 case DRM_FORMAT_BGR565
:
4516 case DRM_FORMAT_RGB888
:
4517 case DRM_FORMAT_BGR888
:
4521 case DRM_FORMAT_XRGB8888
:
4522 case DRM_FORMAT_XBGR8888
:
4523 case DRM_FORMAT_RGBX8888
:
4524 case DRM_FORMAT_BGRX8888
:
4528 case DRM_FORMAT_XRGB2101010
:
4529 case DRM_FORMAT_XBGR2101010
:
4530 case DRM_FORMAT_RGBX1010102
:
4531 case DRM_FORMAT_BGRX1010102
:
4532 case DRM_FORMAT_ARGB2101010
:
4533 case DRM_FORMAT_ABGR2101010
:
4534 case DRM_FORMAT_RGBA1010102
:
4535 case DRM_FORMAT_BGRA1010102
:
4539 case DRM_FORMAT_ARGB8888
:
4540 case DRM_FORMAT_ABGR8888
:
4541 case DRM_FORMAT_RGBA8888
:
4542 case DRM_FORMAT_BGRA8888
:
4547 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4548 drm_get_format_name(format
));
4554 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4557 * drm_format_num_planes - get the number of planes for format
4558 * @format: pixel format (DRM_FORMAT_*)
4561 * The number of planes used by the specified pixel format.
4563 int drm_format_num_planes(uint32_t format
)
4566 case DRM_FORMAT_YUV410
:
4567 case DRM_FORMAT_YVU410
:
4568 case DRM_FORMAT_YUV411
:
4569 case DRM_FORMAT_YVU411
:
4570 case DRM_FORMAT_YUV420
:
4571 case DRM_FORMAT_YVU420
:
4572 case DRM_FORMAT_YUV422
:
4573 case DRM_FORMAT_YVU422
:
4574 case DRM_FORMAT_YUV444
:
4575 case DRM_FORMAT_YVU444
:
4577 case DRM_FORMAT_NV12
:
4578 case DRM_FORMAT_NV21
:
4579 case DRM_FORMAT_NV16
:
4580 case DRM_FORMAT_NV61
:
4581 case DRM_FORMAT_NV24
:
4582 case DRM_FORMAT_NV42
:
4588 EXPORT_SYMBOL(drm_format_num_planes
);
4591 * drm_format_plane_cpp - determine the bytes per pixel value
4592 * @format: pixel format (DRM_FORMAT_*)
4593 * @plane: plane index
4596 * The bytes per pixel value for the specified plane.
4598 int drm_format_plane_cpp(uint32_t format
, int plane
)
4603 if (plane
>= drm_format_num_planes(format
))
4607 case DRM_FORMAT_YUYV
:
4608 case DRM_FORMAT_YVYU
:
4609 case DRM_FORMAT_UYVY
:
4610 case DRM_FORMAT_VYUY
:
4612 case DRM_FORMAT_NV12
:
4613 case DRM_FORMAT_NV21
:
4614 case DRM_FORMAT_NV16
:
4615 case DRM_FORMAT_NV61
:
4616 case DRM_FORMAT_NV24
:
4617 case DRM_FORMAT_NV42
:
4618 return plane
? 2 : 1;
4619 case DRM_FORMAT_YUV410
:
4620 case DRM_FORMAT_YVU410
:
4621 case DRM_FORMAT_YUV411
:
4622 case DRM_FORMAT_YVU411
:
4623 case DRM_FORMAT_YUV420
:
4624 case DRM_FORMAT_YVU420
:
4625 case DRM_FORMAT_YUV422
:
4626 case DRM_FORMAT_YVU422
:
4627 case DRM_FORMAT_YUV444
:
4628 case DRM_FORMAT_YVU444
:
4631 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4635 EXPORT_SYMBOL(drm_format_plane_cpp
);
4638 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4639 * @format: pixel format (DRM_FORMAT_*)
4642 * The horizontal chroma subsampling factor for the
4643 * specified pixel format.
4645 int drm_format_horz_chroma_subsampling(uint32_t format
)
4648 case DRM_FORMAT_YUV411
:
4649 case DRM_FORMAT_YVU411
:
4650 case DRM_FORMAT_YUV410
:
4651 case DRM_FORMAT_YVU410
:
4653 case DRM_FORMAT_YUYV
:
4654 case DRM_FORMAT_YVYU
:
4655 case DRM_FORMAT_UYVY
:
4656 case DRM_FORMAT_VYUY
:
4657 case DRM_FORMAT_NV12
:
4658 case DRM_FORMAT_NV21
:
4659 case DRM_FORMAT_NV16
:
4660 case DRM_FORMAT_NV61
:
4661 case DRM_FORMAT_YUV422
:
4662 case DRM_FORMAT_YVU422
:
4663 case DRM_FORMAT_YUV420
:
4664 case DRM_FORMAT_YVU420
:
4670 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4673 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4674 * @format: pixel format (DRM_FORMAT_*)
4677 * The vertical chroma subsampling factor for the
4678 * specified pixel format.
4680 int drm_format_vert_chroma_subsampling(uint32_t format
)
4683 case DRM_FORMAT_YUV410
:
4684 case DRM_FORMAT_YVU410
:
4686 case DRM_FORMAT_YUV420
:
4687 case DRM_FORMAT_YVU420
:
4688 case DRM_FORMAT_NV12
:
4689 case DRM_FORMAT_NV21
:
4695 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4698 * drm_mode_config_init - initialize DRM mode_configuration structure
4701 * Initialize @dev's mode_config structure, used for tracking the graphics
4702 * configuration of @dev.
4704 * Since this initializes the modeset locks, no locking is possible. Which is no
4705 * problem, since this should happen single threaded at init time. It is the
4706 * driver's problem to ensure this guarantee.
4709 void drm_mode_config_init(struct drm_device
*dev
)
4711 mutex_init(&dev
->mode_config
.mutex
);
4712 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
4713 mutex_init(&dev
->mode_config
.idr_mutex
);
4714 mutex_init(&dev
->mode_config
.fb_lock
);
4715 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4716 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4717 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4718 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4719 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4720 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4721 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4722 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4723 idr_init(&dev
->mode_config
.crtc_idr
);
4725 drm_modeset_lock_all(dev
);
4726 drm_mode_create_standard_connector_properties(dev
);
4727 drm_mode_create_standard_plane_properties(dev
);
4728 drm_modeset_unlock_all(dev
);
4730 /* Just to be sure */
4731 dev
->mode_config
.num_fb
= 0;
4732 dev
->mode_config
.num_connector
= 0;
4733 dev
->mode_config
.num_crtc
= 0;
4734 dev
->mode_config
.num_encoder
= 0;
4735 dev
->mode_config
.num_overlay_plane
= 0;
4736 dev
->mode_config
.num_total_plane
= 0;
4738 EXPORT_SYMBOL(drm_mode_config_init
);
4741 * drm_mode_config_cleanup - free up DRM mode_config info
4744 * Free up all the connectors and CRTCs associated with this DRM device, then
4745 * free up the framebuffers and associated buffer objects.
4747 * Note that since this /should/ happen single-threaded at driver/device
4748 * teardown time, no locking is required. It's the driver's job to ensure that
4749 * this guarantee actually holds true.
4751 * FIXME: cleanup any dangling user buffer objects too
4753 void drm_mode_config_cleanup(struct drm_device
*dev
)
4755 struct drm_connector
*connector
, *ot
;
4756 struct drm_crtc
*crtc
, *ct
;
4757 struct drm_encoder
*encoder
, *enct
;
4758 struct drm_bridge
*bridge
, *brt
;
4759 struct drm_framebuffer
*fb
, *fbt
;
4760 struct drm_property
*property
, *pt
;
4761 struct drm_property_blob
*blob
, *bt
;
4762 struct drm_plane
*plane
, *plt
;
4764 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4766 encoder
->funcs
->destroy(encoder
);
4769 list_for_each_entry_safe(bridge
, brt
,
4770 &dev
->mode_config
.bridge_list
, head
) {
4771 bridge
->funcs
->destroy(bridge
);
4774 list_for_each_entry_safe(connector
, ot
,
4775 &dev
->mode_config
.connector_list
, head
) {
4776 connector
->funcs
->destroy(connector
);
4779 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
4781 drm_property_destroy(dev
, property
);
4784 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
4786 drm_property_destroy_blob(dev
, blob
);
4790 * Single-threaded teardown context, so it's not required to grab the
4791 * fb_lock to protect against concurrent fb_list access. Contrary, it
4792 * would actually deadlock with the drm_framebuffer_cleanup function.
4794 * Also, if there are any framebuffers left, that's a driver leak now,
4795 * so politely WARN about this.
4797 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
4798 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
4799 drm_framebuffer_remove(fb
);
4802 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
4804 plane
->funcs
->destroy(plane
);
4807 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
4808 crtc
->funcs
->destroy(crtc
);
4811 idr_destroy(&dev
->mode_config
.crtc_idr
);
4812 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
4814 EXPORT_SYMBOL(drm_mode_config_cleanup
);