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
,
730 struct drm_plane
*cursor
,
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
;
755 crtc
->cursor
= cursor
;
757 primary
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
759 cursor
->possible_crtcs
= 1 << drm_crtc_index(crtc
);
762 drm_modeset_unlock_all(dev
);
766 EXPORT_SYMBOL(drm_crtc_init_with_planes
);
769 * drm_crtc_cleanup - Clean up the core crtc usage
770 * @crtc: CRTC to cleanup
772 * This function cleans up @crtc and removes it from the DRM mode setting
773 * core. Note that the function does *not* free the crtc structure itself,
774 * this is the responsibility of the caller.
776 void drm_crtc_cleanup(struct drm_crtc
*crtc
)
778 struct drm_device
*dev
= crtc
->dev
;
780 kfree(crtc
->gamma_store
);
781 crtc
->gamma_store
= NULL
;
783 drm_modeset_lock_fini(&crtc
->mutex
);
785 drm_mode_object_put(dev
, &crtc
->base
);
786 list_del(&crtc
->head
);
787 dev
->mode_config
.num_crtc
--;
789 EXPORT_SYMBOL(drm_crtc_cleanup
);
792 * drm_crtc_index - find the index of a registered CRTC
793 * @crtc: CRTC to find index for
795 * Given a registered CRTC, return the index of that CRTC within a DRM
796 * device's list of CRTCs.
798 unsigned int drm_crtc_index(struct drm_crtc
*crtc
)
800 unsigned int index
= 0;
801 struct drm_crtc
*tmp
;
803 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
812 EXPORT_SYMBOL(drm_crtc_index
);
815 * drm_mode_remove - remove and free a mode
816 * @connector: connector list to modify
817 * @mode: mode to remove
819 * Remove @mode from @connector's mode list, then free it.
821 static void drm_mode_remove(struct drm_connector
*connector
,
822 struct drm_display_mode
*mode
)
824 list_del(&mode
->head
);
825 drm_mode_destroy(connector
->dev
, mode
);
829 * drm_connector_init - Init a preallocated connector
831 * @connector: the connector to init
832 * @funcs: callbacks for this connector
833 * @connector_type: user visible type of the connector
835 * Initialises a preallocated connector. Connectors should be
836 * subclassed as part of driver connector objects.
839 * Zero on success, error code on failure.
841 int drm_connector_init(struct drm_device
*dev
,
842 struct drm_connector
*connector
,
843 const struct drm_connector_funcs
*funcs
,
847 struct ida
*connector_ida
=
848 &drm_connector_enum_list
[connector_type
].ida
;
850 drm_modeset_lock_all(dev
);
852 ret
= drm_mode_object_get(dev
, &connector
->base
, DRM_MODE_OBJECT_CONNECTOR
);
856 connector
->base
.properties
= &connector
->properties
;
857 connector
->dev
= dev
;
858 connector
->funcs
= funcs
;
859 connector
->connector_type
= connector_type
;
860 connector
->connector_type_id
=
861 ida_simple_get(connector_ida
, 1, 0, GFP_KERNEL
);
862 if (connector
->connector_type_id
< 0) {
863 ret
= connector
->connector_type_id
;
867 kasprintf(GFP_KERNEL
, "%s-%d",
868 drm_connector_enum_list
[connector_type
].name
,
869 connector
->connector_type_id
);
870 if (!connector
->name
) {
875 INIT_LIST_HEAD(&connector
->probed_modes
);
876 INIT_LIST_HEAD(&connector
->modes
);
877 connector
->edid_blob_ptr
= NULL
;
878 connector
->status
= connector_status_unknown
;
880 list_add_tail(&connector
->head
, &dev
->mode_config
.connector_list
);
881 dev
->mode_config
.num_connector
++;
883 if (connector_type
!= DRM_MODE_CONNECTOR_VIRTUAL
)
884 drm_object_attach_property(&connector
->base
,
885 dev
->mode_config
.edid_property
,
888 drm_object_attach_property(&connector
->base
,
889 dev
->mode_config
.dpms_property
, 0);
893 drm_mode_object_put(dev
, &connector
->base
);
896 drm_modeset_unlock_all(dev
);
900 EXPORT_SYMBOL(drm_connector_init
);
903 * drm_connector_cleanup - cleans up an initialised connector
904 * @connector: connector to cleanup
906 * Cleans up the connector but doesn't free the object.
908 void drm_connector_cleanup(struct drm_connector
*connector
)
910 struct drm_device
*dev
= connector
->dev
;
911 struct drm_display_mode
*mode
, *t
;
913 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
914 drm_mode_remove(connector
, mode
);
916 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
917 drm_mode_remove(connector
, mode
);
919 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
920 connector
->connector_type_id
);
922 drm_mode_object_put(dev
, &connector
->base
);
923 kfree(connector
->name
);
924 connector
->name
= NULL
;
925 list_del(&connector
->head
);
926 dev
->mode_config
.num_connector
--;
928 EXPORT_SYMBOL(drm_connector_cleanup
);
931 * drm_connector_unplug_all - unregister connector userspace interfaces
934 * This function unregisters all connector userspace interfaces in sysfs. Should
935 * be call when the device is disconnected, e.g. from an usb driver's
936 * ->disconnect callback.
938 void drm_connector_unplug_all(struct drm_device
*dev
)
940 struct drm_connector
*connector
;
942 /* taking the mode config mutex ends up in a clash with sysfs */
943 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
944 drm_sysfs_connector_remove(connector
);
947 EXPORT_SYMBOL(drm_connector_unplug_all
);
950 * drm_bridge_init - initialize a drm transcoder/bridge
952 * @bridge: transcoder/bridge to set up
953 * @funcs: bridge function table
955 * Initialises a preallocated bridge. Bridges should be
956 * subclassed as part of driver connector objects.
959 * Zero on success, error code on failure.
961 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
962 const struct drm_bridge_funcs
*funcs
)
966 drm_modeset_lock_all(dev
);
968 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
973 bridge
->funcs
= funcs
;
975 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
976 dev
->mode_config
.num_bridge
++;
979 drm_modeset_unlock_all(dev
);
982 EXPORT_SYMBOL(drm_bridge_init
);
985 * drm_bridge_cleanup - cleans up an initialised bridge
986 * @bridge: bridge to cleanup
988 * Cleans up the bridge but doesn't free the object.
990 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
992 struct drm_device
*dev
= bridge
->dev
;
994 drm_modeset_lock_all(dev
);
995 drm_mode_object_put(dev
, &bridge
->base
);
996 list_del(&bridge
->head
);
997 dev
->mode_config
.num_bridge
--;
998 drm_modeset_unlock_all(dev
);
1000 EXPORT_SYMBOL(drm_bridge_cleanup
);
1003 * drm_encoder_init - Init a preallocated encoder
1005 * @encoder: the encoder to init
1006 * @funcs: callbacks for this encoder
1007 * @encoder_type: user visible type of the encoder
1009 * Initialises a preallocated encoder. Encoder should be
1010 * subclassed as part of driver encoder objects.
1013 * Zero on success, error code on failure.
1015 int drm_encoder_init(struct drm_device
*dev
,
1016 struct drm_encoder
*encoder
,
1017 const struct drm_encoder_funcs
*funcs
,
1022 drm_modeset_lock_all(dev
);
1024 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1029 encoder
->encoder_type
= encoder_type
;
1030 encoder
->funcs
= funcs
;
1031 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1032 drm_encoder_enum_list
[encoder_type
].name
,
1034 if (!encoder
->name
) {
1039 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1040 dev
->mode_config
.num_encoder
++;
1044 drm_mode_object_put(dev
, &encoder
->base
);
1047 drm_modeset_unlock_all(dev
);
1051 EXPORT_SYMBOL(drm_encoder_init
);
1054 * drm_encoder_cleanup - cleans up an initialised encoder
1055 * @encoder: encoder to cleanup
1057 * Cleans up the encoder but doesn't free the object.
1059 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1061 struct drm_device
*dev
= encoder
->dev
;
1062 drm_modeset_lock_all(dev
);
1063 drm_mode_object_put(dev
, &encoder
->base
);
1064 kfree(encoder
->name
);
1065 encoder
->name
= NULL
;
1066 list_del(&encoder
->head
);
1067 dev
->mode_config
.num_encoder
--;
1068 drm_modeset_unlock_all(dev
);
1070 EXPORT_SYMBOL(drm_encoder_cleanup
);
1073 * drm_universal_plane_init - Initialize a new universal plane object
1075 * @plane: plane object to init
1076 * @possible_crtcs: bitmask of possible CRTCs
1077 * @funcs: callbacks for the new plane
1078 * @formats: array of supported formats (%DRM_FORMAT_*)
1079 * @format_count: number of elements in @formats
1080 * @type: type of plane (overlay, primary, cursor)
1082 * Initializes a plane object of type @type.
1085 * Zero on success, error code on failure.
1087 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1088 unsigned long possible_crtcs
,
1089 const struct drm_plane_funcs
*funcs
,
1090 const uint32_t *formats
, uint32_t format_count
,
1091 enum drm_plane_type type
)
1095 drm_modeset_lock_all(dev
);
1097 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1101 plane
->base
.properties
= &plane
->properties
;
1103 plane
->funcs
= funcs
;
1104 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1106 if (!plane
->format_types
) {
1107 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1108 drm_mode_object_put(dev
, &plane
->base
);
1113 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1114 plane
->format_count
= format_count
;
1115 plane
->possible_crtcs
= possible_crtcs
;
1118 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1119 dev
->mode_config
.num_total_plane
++;
1120 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1121 dev
->mode_config
.num_overlay_plane
++;
1123 drm_object_attach_property(&plane
->base
,
1124 dev
->mode_config
.plane_type_property
,
1128 drm_modeset_unlock_all(dev
);
1132 EXPORT_SYMBOL(drm_universal_plane_init
);
1135 * drm_plane_init - Initialize a legacy plane
1137 * @plane: plane object to init
1138 * @possible_crtcs: bitmask of possible CRTCs
1139 * @funcs: callbacks for the new plane
1140 * @formats: array of supported formats (%DRM_FORMAT_*)
1141 * @format_count: number of elements in @formats
1142 * @is_primary: plane type (primary vs overlay)
1144 * Legacy API to initialize a DRM plane.
1146 * New drivers should call drm_universal_plane_init() instead.
1149 * Zero on success, error code on failure.
1151 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1152 unsigned long possible_crtcs
,
1153 const struct drm_plane_funcs
*funcs
,
1154 const uint32_t *formats
, uint32_t format_count
,
1157 enum drm_plane_type type
;
1159 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1160 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1161 formats
, format_count
, type
);
1163 EXPORT_SYMBOL(drm_plane_init
);
1166 * drm_plane_cleanup - Clean up the core plane usage
1167 * @plane: plane to cleanup
1169 * This function cleans up @plane and removes it from the DRM mode setting
1170 * core. Note that the function does *not* free the plane structure itself,
1171 * this is the responsibility of the caller.
1173 void drm_plane_cleanup(struct drm_plane
*plane
)
1175 struct drm_device
*dev
= plane
->dev
;
1177 drm_modeset_lock_all(dev
);
1178 kfree(plane
->format_types
);
1179 drm_mode_object_put(dev
, &plane
->base
);
1181 BUG_ON(list_empty(&plane
->head
));
1183 list_del(&plane
->head
);
1184 dev
->mode_config
.num_total_plane
--;
1185 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1186 dev
->mode_config
.num_overlay_plane
--;
1187 drm_modeset_unlock_all(dev
);
1189 EXPORT_SYMBOL(drm_plane_cleanup
);
1192 * drm_plane_force_disable - Forcibly disable a plane
1193 * @plane: plane to disable
1195 * Forces the plane to be disabled.
1197 * Used when the plane's current framebuffer is destroyed,
1198 * and when restoring fbdev mode.
1200 void drm_plane_force_disable(struct drm_plane
*plane
)
1202 struct drm_framebuffer
*old_fb
= plane
->fb
;
1208 ret
= plane
->funcs
->disable_plane(plane
);
1210 DRM_ERROR("failed to disable plane with busy fb\n");
1213 /* disconnect the plane from the fb and crtc: */
1214 __drm_framebuffer_unreference(old_fb
);
1218 EXPORT_SYMBOL(drm_plane_force_disable
);
1220 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1222 struct drm_property
*edid
;
1223 struct drm_property
*dpms
;
1226 * Standard properties (apply to all connectors)
1228 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1229 DRM_MODE_PROP_IMMUTABLE
,
1231 dev
->mode_config
.edid_property
= edid
;
1233 dpms
= drm_property_create_enum(dev
, 0,
1234 "DPMS", drm_dpms_enum_list
,
1235 ARRAY_SIZE(drm_dpms_enum_list
));
1236 dev
->mode_config
.dpms_property
= dpms
;
1241 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1243 struct drm_property
*type
;
1246 * Standard properties (apply to all planes)
1248 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1249 "type", drm_plane_type_enum_list
,
1250 ARRAY_SIZE(drm_plane_type_enum_list
));
1251 dev
->mode_config
.plane_type_property
= type
;
1257 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1260 * Called by a driver the first time a DVI-I connector is made.
1262 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1264 struct drm_property
*dvi_i_selector
;
1265 struct drm_property
*dvi_i_subconnector
;
1267 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1271 drm_property_create_enum(dev
, 0,
1272 "select subconnector",
1273 drm_dvi_i_select_enum_list
,
1274 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1275 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1277 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1279 drm_dvi_i_subconnector_enum_list
,
1280 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1281 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1285 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1288 * drm_create_tv_properties - create TV specific connector properties
1290 * @num_modes: number of different TV formats (modes) supported
1291 * @modes: array of pointers to strings containing name of each format
1293 * Called by a driver's TV initialization routine, this function creates
1294 * the TV specific connector properties for a given device. Caller is
1295 * responsible for allocating a list of format names and passing them to
1298 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1301 struct drm_property
*tv_selector
;
1302 struct drm_property
*tv_subconnector
;
1305 if (dev
->mode_config
.tv_select_subconnector_property
)
1309 * Basic connector properties
1311 tv_selector
= drm_property_create_enum(dev
, 0,
1312 "select subconnector",
1313 drm_tv_select_enum_list
,
1314 ARRAY_SIZE(drm_tv_select_enum_list
));
1315 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1318 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1320 drm_tv_subconnector_enum_list
,
1321 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1322 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1325 * Other, TV specific properties: margins & TV modes.
1327 dev
->mode_config
.tv_left_margin_property
=
1328 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1330 dev
->mode_config
.tv_right_margin_property
=
1331 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1333 dev
->mode_config
.tv_top_margin_property
=
1334 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1336 dev
->mode_config
.tv_bottom_margin_property
=
1337 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1339 dev
->mode_config
.tv_mode_property
=
1340 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1342 for (i
= 0; i
< num_modes
; i
++)
1343 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1346 dev
->mode_config
.tv_brightness_property
=
1347 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1349 dev
->mode_config
.tv_contrast_property
=
1350 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1352 dev
->mode_config
.tv_flicker_reduction_property
=
1353 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1355 dev
->mode_config
.tv_overscan_property
=
1356 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1358 dev
->mode_config
.tv_saturation_property
=
1359 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1361 dev
->mode_config
.tv_hue_property
=
1362 drm_property_create_range(dev
, 0, "hue", 0, 100);
1366 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1369 * drm_mode_create_scaling_mode_property - create scaling mode property
1372 * Called by a driver the first time it's needed, must be attached to desired
1375 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1377 struct drm_property
*scaling_mode
;
1379 if (dev
->mode_config
.scaling_mode_property
)
1383 drm_property_create_enum(dev
, 0, "scaling mode",
1384 drm_scaling_mode_enum_list
,
1385 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1387 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1391 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1394 * drm_mode_create_dirty_property - create dirty property
1397 * Called by a driver the first time it's needed, must be attached to desired
1400 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1402 struct drm_property
*dirty_info
;
1404 if (dev
->mode_config
.dirty_info_property
)
1408 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1410 drm_dirty_info_enum_list
,
1411 ARRAY_SIZE(drm_dirty_info_enum_list
));
1412 dev
->mode_config
.dirty_info_property
= dirty_info
;
1416 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1418 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1420 uint32_t total_objects
= 0;
1422 total_objects
+= dev
->mode_config
.num_crtc
;
1423 total_objects
+= dev
->mode_config
.num_connector
;
1424 total_objects
+= dev
->mode_config
.num_encoder
;
1425 total_objects
+= dev
->mode_config
.num_bridge
;
1427 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1428 if (!group
->id_list
)
1431 group
->num_crtcs
= 0;
1432 group
->num_connectors
= 0;
1433 group
->num_encoders
= 0;
1434 group
->num_bridges
= 0;
1438 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1440 kfree(group
->id_list
);
1441 group
->id_list
= NULL
;
1445 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1446 * the drm core's responsibility to set up mode control groups.
1448 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1449 struct drm_mode_group
*group
)
1451 struct drm_crtc
*crtc
;
1452 struct drm_encoder
*encoder
;
1453 struct drm_connector
*connector
;
1454 struct drm_bridge
*bridge
;
1457 if ((ret
= drm_mode_group_init(dev
, group
)))
1460 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1461 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1463 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1464 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1467 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1468 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1469 group
->num_connectors
++] = connector
->base
.id
;
1471 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1472 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1473 group
->num_connectors
+ group
->num_bridges
++] =
1478 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1481 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1482 * @out: drm_mode_modeinfo struct to return to the user
1483 * @in: drm_display_mode to use
1485 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1488 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1489 const struct drm_display_mode
*in
)
1491 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1492 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1493 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1494 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1495 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1496 "timing values too large for mode info\n");
1498 out
->clock
= in
->clock
;
1499 out
->hdisplay
= in
->hdisplay
;
1500 out
->hsync_start
= in
->hsync_start
;
1501 out
->hsync_end
= in
->hsync_end
;
1502 out
->htotal
= in
->htotal
;
1503 out
->hskew
= in
->hskew
;
1504 out
->vdisplay
= in
->vdisplay
;
1505 out
->vsync_start
= in
->vsync_start
;
1506 out
->vsync_end
= in
->vsync_end
;
1507 out
->vtotal
= in
->vtotal
;
1508 out
->vscan
= in
->vscan
;
1509 out
->vrefresh
= in
->vrefresh
;
1510 out
->flags
= in
->flags
;
1511 out
->type
= in
->type
;
1512 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1513 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1517 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1518 * @out: drm_display_mode to return to the user
1519 * @in: drm_mode_modeinfo to use
1521 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1525 * Zero on success, errno on failure.
1527 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1528 const struct drm_mode_modeinfo
*in
)
1530 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1533 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1536 out
->clock
= in
->clock
;
1537 out
->hdisplay
= in
->hdisplay
;
1538 out
->hsync_start
= in
->hsync_start
;
1539 out
->hsync_end
= in
->hsync_end
;
1540 out
->htotal
= in
->htotal
;
1541 out
->hskew
= in
->hskew
;
1542 out
->vdisplay
= in
->vdisplay
;
1543 out
->vsync_start
= in
->vsync_start
;
1544 out
->vsync_end
= in
->vsync_end
;
1545 out
->vtotal
= in
->vtotal
;
1546 out
->vscan
= in
->vscan
;
1547 out
->vrefresh
= in
->vrefresh
;
1548 out
->flags
= in
->flags
;
1549 out
->type
= in
->type
;
1550 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1551 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1557 * drm_mode_getresources - get graphics configuration
1558 * @dev: drm device for the ioctl
1559 * @data: data pointer for the ioctl
1560 * @file_priv: drm file for the ioctl call
1562 * Construct a set of configuration description structures and return
1563 * them to the user, including CRTC, connector and framebuffer configuration.
1565 * Called by the user via ioctl.
1568 * Zero on success, errno on failure.
1570 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1571 struct drm_file
*file_priv
)
1573 struct drm_mode_card_res
*card_res
= data
;
1574 struct list_head
*lh
;
1575 struct drm_framebuffer
*fb
;
1576 struct drm_connector
*connector
;
1577 struct drm_crtc
*crtc
;
1578 struct drm_encoder
*encoder
;
1580 int connector_count
= 0;
1583 int encoder_count
= 0;
1585 uint32_t __user
*fb_id
;
1586 uint32_t __user
*crtc_id
;
1587 uint32_t __user
*connector_id
;
1588 uint32_t __user
*encoder_id
;
1589 struct drm_mode_group
*mode_group
;
1591 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1595 mutex_lock(&file_priv
->fbs_lock
);
1597 * For the non-control nodes we need to limit the list of resources
1598 * by IDs in the group list for this node
1600 list_for_each(lh
, &file_priv
->fbs
)
1603 /* handle this in 4 parts */
1605 if (card_res
->count_fbs
>= fb_count
) {
1607 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1608 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1609 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1610 mutex_unlock(&file_priv
->fbs_lock
);
1616 card_res
->count_fbs
= fb_count
;
1617 mutex_unlock(&file_priv
->fbs_lock
);
1619 drm_modeset_lock_all(dev
);
1620 if (!drm_is_primary_client(file_priv
)) {
1623 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1626 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1629 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1633 mode_group
= &file_priv
->master
->minor
->mode_group
;
1634 crtc_count
= mode_group
->num_crtcs
;
1635 connector_count
= mode_group
->num_connectors
;
1636 encoder_count
= mode_group
->num_encoders
;
1639 card_res
->max_height
= dev
->mode_config
.max_height
;
1640 card_res
->min_height
= dev
->mode_config
.min_height
;
1641 card_res
->max_width
= dev
->mode_config
.max_width
;
1642 card_res
->min_width
= dev
->mode_config
.min_width
;
1645 if (card_res
->count_crtcs
>= crtc_count
) {
1647 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1649 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1651 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1652 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1659 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1660 if (put_user(mode_group
->id_list
[i
],
1661 crtc_id
+ copied
)) {
1669 card_res
->count_crtcs
= crtc_count
;
1672 if (card_res
->count_encoders
>= encoder_count
) {
1674 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1676 list_for_each_entry(encoder
,
1677 &dev
->mode_config
.encoder_list
,
1679 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1681 if (put_user(encoder
->base
.id
, encoder_id
+
1689 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1690 if (put_user(mode_group
->id_list
[i
],
1691 encoder_id
+ copied
)) {
1700 card_res
->count_encoders
= encoder_count
;
1703 if (card_res
->count_connectors
>= connector_count
) {
1705 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1707 list_for_each_entry(connector
,
1708 &dev
->mode_config
.connector_list
,
1710 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1713 if (put_user(connector
->base
.id
,
1714 connector_id
+ copied
)) {
1721 int start
= mode_group
->num_crtcs
+
1722 mode_group
->num_encoders
;
1723 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1724 if (put_user(mode_group
->id_list
[i
],
1725 connector_id
+ copied
)) {
1733 card_res
->count_connectors
= connector_count
;
1735 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1736 card_res
->count_connectors
, card_res
->count_encoders
);
1739 drm_modeset_unlock_all(dev
);
1744 * drm_mode_getcrtc - get CRTC configuration
1745 * @dev: drm device for the ioctl
1746 * @data: data pointer for the ioctl
1747 * @file_priv: drm file for the ioctl call
1749 * Construct a CRTC configuration structure to return to the user.
1751 * Called by the user via ioctl.
1754 * Zero on success, errno on failure.
1756 int drm_mode_getcrtc(struct drm_device
*dev
,
1757 void *data
, struct drm_file
*file_priv
)
1759 struct drm_mode_crtc
*crtc_resp
= data
;
1760 struct drm_crtc
*crtc
;
1763 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1766 drm_modeset_lock_all(dev
);
1768 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1774 crtc_resp
->x
= crtc
->x
;
1775 crtc_resp
->y
= crtc
->y
;
1776 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1777 if (crtc
->primary
->fb
)
1778 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1780 crtc_resp
->fb_id
= 0;
1782 if (crtc
->enabled
) {
1784 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1785 crtc_resp
->mode_valid
= 1;
1788 crtc_resp
->mode_valid
= 0;
1792 drm_modeset_unlock_all(dev
);
1796 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1797 const struct drm_file
*file_priv
)
1800 * If user-space hasn't configured the driver to expose the stereo 3D
1801 * modes, don't expose them.
1803 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1810 * drm_mode_getconnector - get connector configuration
1811 * @dev: drm device for the ioctl
1812 * @data: data pointer for the ioctl
1813 * @file_priv: drm file for the ioctl call
1815 * Construct a connector configuration structure to return to the user.
1817 * Called by the user via ioctl.
1820 * Zero on success, errno on failure.
1822 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1823 struct drm_file
*file_priv
)
1825 struct drm_mode_get_connector
*out_resp
= data
;
1826 struct drm_connector
*connector
;
1827 struct drm_display_mode
*mode
;
1829 int props_count
= 0;
1830 int encoders_count
= 0;
1834 struct drm_mode_modeinfo u_mode
;
1835 struct drm_mode_modeinfo __user
*mode_ptr
;
1836 uint32_t __user
*prop_ptr
;
1837 uint64_t __user
*prop_values
;
1838 uint32_t __user
*encoder_ptr
;
1840 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1843 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1845 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1847 mutex_lock(&dev
->mode_config
.mutex
);
1849 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1855 props_count
= connector
->properties
.count
;
1857 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1858 if (connector
->encoder_ids
[i
] != 0) {
1863 if (out_resp
->count_modes
== 0) {
1864 connector
->funcs
->fill_modes(connector
,
1865 dev
->mode_config
.max_width
,
1866 dev
->mode_config
.max_height
);
1869 /* delayed so we get modes regardless of pre-fill_modes state */
1870 list_for_each_entry(mode
, &connector
->modes
, head
)
1871 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1874 out_resp
->connector_id
= connector
->base
.id
;
1875 out_resp
->connector_type
= connector
->connector_type
;
1876 out_resp
->connector_type_id
= connector
->connector_type_id
;
1877 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1878 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1879 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1880 out_resp
->connection
= connector
->status
;
1881 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1882 if (connector
->encoder
)
1883 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1885 out_resp
->encoder_id
= 0;
1886 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1889 * This ioctl is called twice, once to determine how much space is
1890 * needed, and the 2nd time to fill it.
1892 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1894 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1895 list_for_each_entry(mode
, &connector
->modes
, head
) {
1896 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
1899 drm_crtc_convert_to_umode(&u_mode
, mode
);
1900 if (copy_to_user(mode_ptr
+ copied
,
1901 &u_mode
, sizeof(u_mode
))) {
1908 out_resp
->count_modes
= mode_count
;
1910 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1912 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1913 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1914 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1915 if (put_user(connector
->properties
.ids
[i
],
1916 prop_ptr
+ copied
)) {
1921 if (put_user(connector
->properties
.values
[i
],
1922 prop_values
+ copied
)) {
1929 out_resp
->count_props
= props_count
;
1931 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1933 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1934 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1935 if (connector
->encoder_ids
[i
] != 0) {
1936 if (put_user(connector
->encoder_ids
[i
],
1937 encoder_ptr
+ copied
)) {
1945 out_resp
->count_encoders
= encoders_count
;
1948 mutex_unlock(&dev
->mode_config
.mutex
);
1954 * drm_mode_getencoder - get encoder configuration
1955 * @dev: drm device for the ioctl
1956 * @data: data pointer for the ioctl
1957 * @file_priv: drm file for the ioctl call
1959 * Construct a encoder configuration structure to return to the user.
1961 * Called by the user via ioctl.
1964 * Zero on success, errno on failure.
1966 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
1967 struct drm_file
*file_priv
)
1969 struct drm_mode_get_encoder
*enc_resp
= data
;
1970 struct drm_encoder
*encoder
;
1973 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1976 drm_modeset_lock_all(dev
);
1977 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
1984 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
1986 enc_resp
->crtc_id
= 0;
1987 enc_resp
->encoder_type
= encoder
->encoder_type
;
1988 enc_resp
->encoder_id
= encoder
->base
.id
;
1989 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
1990 enc_resp
->possible_clones
= encoder
->possible_clones
;
1993 drm_modeset_unlock_all(dev
);
1998 * drm_mode_getplane_res - enumerate all plane resources
2001 * @file_priv: DRM file info
2003 * Construct a list of plane ids to return to the user.
2005 * Called by the user via ioctl.
2008 * Zero on success, errno on failure.
2010 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2011 struct drm_file
*file_priv
)
2013 struct drm_mode_get_plane_res
*plane_resp
= data
;
2014 struct drm_mode_config
*config
;
2015 struct drm_plane
*plane
;
2016 uint32_t __user
*plane_ptr
;
2017 int copied
= 0, ret
= 0;
2018 unsigned num_planes
;
2020 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2023 drm_modeset_lock_all(dev
);
2024 config
= &dev
->mode_config
;
2026 if (file_priv
->universal_planes
)
2027 num_planes
= config
->num_total_plane
;
2029 num_planes
= config
->num_overlay_plane
;
2032 * This ioctl is called twice, once to determine how much space is
2033 * needed, and the 2nd time to fill it.
2036 (plane_resp
->count_planes
>= num_planes
)) {
2037 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2039 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2041 * Unless userspace set the 'universal planes'
2042 * capability bit, only advertise overlays.
2044 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2045 !file_priv
->universal_planes
)
2048 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2055 plane_resp
->count_planes
= num_planes
;
2058 drm_modeset_unlock_all(dev
);
2063 * drm_mode_getplane - get plane configuration
2066 * @file_priv: DRM file info
2068 * Construct a plane configuration structure to return to the user.
2070 * Called by the user via ioctl.
2073 * Zero on success, errno on failure.
2075 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2076 struct drm_file
*file_priv
)
2078 struct drm_mode_get_plane
*plane_resp
= data
;
2079 struct drm_plane
*plane
;
2080 uint32_t __user
*format_ptr
;
2083 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2086 drm_modeset_lock_all(dev
);
2087 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2094 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2096 plane_resp
->crtc_id
= 0;
2099 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2101 plane_resp
->fb_id
= 0;
2103 plane_resp
->plane_id
= plane
->base
.id
;
2104 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2105 plane_resp
->gamma_size
= 0;
2108 * This ioctl is called twice, once to determine how much space is
2109 * needed, and the 2nd time to fill it.
2111 if (plane
->format_count
&&
2112 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2113 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2114 if (copy_to_user(format_ptr
,
2115 plane
->format_types
,
2116 sizeof(uint32_t) * plane
->format_count
)) {
2121 plane_resp
->count_format_types
= plane
->format_count
;
2124 drm_modeset_unlock_all(dev
);
2129 * setplane_internal - setplane handler for internal callers
2131 * Note that we assume an extra reference has already been taken on fb. If the
2132 * update fails, this reference will be dropped before return; if it succeeds,
2133 * the previous framebuffer (if any) will be unreferenced instead.
2135 * src_{x,y,w,h} are provided in 16.16 fixed point format
2137 static int setplane_internal(struct drm_plane
*plane
,
2138 struct drm_crtc
*crtc
,
2139 struct drm_framebuffer
*fb
,
2140 int32_t crtc_x
, int32_t crtc_y
,
2141 uint32_t crtc_w
, uint32_t crtc_h
,
2142 /* src_{x,y,w,h} values are 16.16 fixed point */
2143 uint32_t src_x
, uint32_t src_y
,
2144 uint32_t src_w
, uint32_t src_h
)
2146 struct drm_device
*dev
= plane
->dev
;
2147 struct drm_framebuffer
*old_fb
= NULL
;
2149 unsigned int fb_width
, fb_height
;
2152 /* No fb means shut it down */
2154 drm_modeset_lock_all(dev
);
2156 ret
= plane
->funcs
->disable_plane(plane
);
2163 drm_modeset_unlock_all(dev
);
2167 /* Check whether this plane is usable on this CRTC */
2168 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2169 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2174 /* Check whether this plane supports the fb pixel format. */
2175 for (i
= 0; i
< plane
->format_count
; i
++)
2176 if (fb
->pixel_format
== plane
->format_types
[i
])
2178 if (i
== plane
->format_count
) {
2179 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2180 drm_get_format_name(fb
->pixel_format
));
2185 fb_width
= fb
->width
<< 16;
2186 fb_height
= fb
->height
<< 16;
2188 /* Make sure source coordinates are inside the fb. */
2189 if (src_w
> fb_width
||
2190 src_x
> fb_width
- src_w
||
2191 src_h
> fb_height
||
2192 src_y
> fb_height
- src_h
) {
2193 DRM_DEBUG_KMS("Invalid source coordinates "
2194 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2195 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2196 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2197 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2198 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2203 drm_modeset_lock_all(dev
);
2205 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2206 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2207 src_x
, src_y
, src_w
, src_h
);
2215 drm_modeset_unlock_all(dev
);
2219 drm_framebuffer_unreference(fb
);
2221 drm_framebuffer_unreference(old_fb
);
2228 * drm_mode_setplane - configure a plane's configuration
2230 * @data: ioctl data*
2231 * @file_priv: DRM file info
2233 * Set plane configuration, including placement, fb, scaling, and other factors.
2234 * Or pass a NULL fb to disable (planes may be disabled without providing a
2238 * Zero on success, errno on failure.
2240 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2241 struct drm_file
*file_priv
)
2243 struct drm_mode_set_plane
*plane_req
= data
;
2244 struct drm_mode_object
*obj
;
2245 struct drm_plane
*plane
;
2246 struct drm_crtc
*crtc
= NULL
;
2247 struct drm_framebuffer
*fb
= NULL
;
2249 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2252 /* Give drivers some help against integer overflows */
2253 if (plane_req
->crtc_w
> INT_MAX
||
2254 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2255 plane_req
->crtc_h
> INT_MAX
||
2256 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2257 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2258 plane_req
->crtc_w
, plane_req
->crtc_h
,
2259 plane_req
->crtc_x
, plane_req
->crtc_y
);
2264 * First, find the plane, crtc, and fb objects. If not available,
2265 * we don't bother to call the driver.
2267 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2268 DRM_MODE_OBJECT_PLANE
);
2270 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2271 plane_req
->plane_id
);
2274 plane
= obj_to_plane(obj
);
2276 if (plane_req
->fb_id
) {
2277 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2279 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2284 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2285 DRM_MODE_OBJECT_CRTC
);
2287 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2288 plane_req
->crtc_id
);
2291 crtc
= obj_to_crtc(obj
);
2295 * setplane_internal will take care of deref'ing either the old or new
2296 * framebuffer depending on success.
2298 return setplane_internal(plane
, crtc
, fb
,
2299 plane_req
->crtc_x
, plane_req
->crtc_y
,
2300 plane_req
->crtc_w
, plane_req
->crtc_h
,
2301 plane_req
->src_x
, plane_req
->src_y
,
2302 plane_req
->src_w
, plane_req
->src_h
);
2306 * drm_mode_set_config_internal - helper to call ->set_config
2307 * @set: modeset config to set
2309 * This is a little helper to wrap internal calls to the ->set_config driver
2310 * interface. The only thing it adds is correct refcounting dance.
2313 * Zero on success, errno on failure.
2315 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2317 struct drm_crtc
*crtc
= set
->crtc
;
2318 struct drm_framebuffer
*fb
;
2319 struct drm_crtc
*tmp
;
2323 * NOTE: ->set_config can also disable other crtcs (if we steal all
2324 * connectors from it), hence we need to refcount the fbs across all
2325 * crtcs. Atomic modeset will have saner semantics ...
2327 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2328 tmp
->old_fb
= tmp
->primary
->fb
;
2332 ret
= crtc
->funcs
->set_config(set
);
2334 crtc
->primary
->crtc
= crtc
;
2335 crtc
->primary
->fb
= fb
;
2338 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2339 if (tmp
->primary
->fb
)
2340 drm_framebuffer_reference(tmp
->primary
->fb
);
2342 drm_framebuffer_unreference(tmp
->old_fb
);
2347 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2350 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2352 * @crtc: CRTC that framebuffer will be displayed on
2355 * @mode: mode that framebuffer will be displayed under
2356 * @fb: framebuffer to check size of
2358 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2360 const struct drm_display_mode
*mode
,
2361 const struct drm_framebuffer
*fb
)
2364 int hdisplay
, vdisplay
;
2366 hdisplay
= mode
->hdisplay
;
2367 vdisplay
= mode
->vdisplay
;
2369 if (drm_mode_is_stereo(mode
)) {
2370 struct drm_display_mode adjusted
= *mode
;
2372 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2373 hdisplay
= adjusted
.crtc_hdisplay
;
2374 vdisplay
= adjusted
.crtc_vdisplay
;
2377 if (crtc
->invert_dimensions
)
2378 swap(hdisplay
, vdisplay
);
2380 if (hdisplay
> fb
->width
||
2381 vdisplay
> fb
->height
||
2382 x
> fb
->width
- hdisplay
||
2383 y
> fb
->height
- vdisplay
) {
2384 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2385 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2386 crtc
->invert_dimensions
? " (inverted)" : "");
2392 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2395 * drm_mode_setcrtc - set CRTC configuration
2396 * @dev: drm device for the ioctl
2397 * @data: data pointer for the ioctl
2398 * @file_priv: drm file for the ioctl call
2400 * Build a new CRTC configuration based on user request.
2402 * Called by the user via ioctl.
2405 * Zero on success, errno on failure.
2407 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2408 struct drm_file
*file_priv
)
2410 struct drm_mode_config
*config
= &dev
->mode_config
;
2411 struct drm_mode_crtc
*crtc_req
= data
;
2412 struct drm_crtc
*crtc
;
2413 struct drm_connector
**connector_set
= NULL
, *connector
;
2414 struct drm_framebuffer
*fb
= NULL
;
2415 struct drm_display_mode
*mode
= NULL
;
2416 struct drm_mode_set set
;
2417 uint32_t __user
*set_connectors_ptr
;
2421 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2424 /* For some reason crtc x/y offsets are signed internally. */
2425 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2428 drm_modeset_lock_all(dev
);
2429 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2431 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2435 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2437 if (crtc_req
->mode_valid
) {
2438 /* If we have a mode we need a framebuffer. */
2439 /* If we pass -1, set the mode with the currently bound fb */
2440 if (crtc_req
->fb_id
== -1) {
2441 if (!crtc
->primary
->fb
) {
2442 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2446 fb
= crtc
->primary
->fb
;
2447 /* Make refcounting symmetric with the lookup path. */
2448 drm_framebuffer_reference(fb
);
2450 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2452 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2459 mode
= drm_mode_create(dev
);
2465 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2467 DRM_DEBUG_KMS("Invalid mode\n");
2471 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2473 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2480 if (crtc_req
->count_connectors
== 0 && mode
) {
2481 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2486 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2487 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2488 crtc_req
->count_connectors
);
2493 if (crtc_req
->count_connectors
> 0) {
2496 /* Avoid unbounded kernel memory allocation */
2497 if (crtc_req
->count_connectors
> config
->num_connector
) {
2502 connector_set
= kmalloc(crtc_req
->count_connectors
*
2503 sizeof(struct drm_connector
*),
2505 if (!connector_set
) {
2510 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2511 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2512 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2517 connector
= drm_connector_find(dev
, out_id
);
2519 DRM_DEBUG_KMS("Connector id %d unknown\n",
2524 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2528 connector_set
[i
] = connector
;
2533 set
.x
= crtc_req
->x
;
2534 set
.y
= crtc_req
->y
;
2536 set
.connectors
= connector_set
;
2537 set
.num_connectors
= crtc_req
->count_connectors
;
2539 ret
= drm_mode_set_config_internal(&set
);
2543 drm_framebuffer_unreference(fb
);
2545 kfree(connector_set
);
2546 drm_mode_destroy(dev
, mode
);
2547 drm_modeset_unlock_all(dev
);
2552 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2553 * universal plane handler call
2554 * @crtc: crtc to update cursor for
2555 * @req: data pointer for the ioctl
2556 * @file_priv: drm file for the ioctl call
2558 * Legacy cursor ioctl's work directly with driver buffer handles. To
2559 * translate legacy ioctl calls into universal plane handler calls, we need to
2560 * wrap the native buffer handle in a drm_framebuffer.
2562 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2563 * buffer with a pitch of 4*width; the universal plane interface should be used
2564 * directly in cases where the hardware can support other buffer settings and
2565 * userspace wants to make use of these capabilities.
2568 * Zero on success, errno on failure.
2570 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2571 struct drm_mode_cursor2
*req
,
2572 struct drm_file
*file_priv
)
2574 struct drm_device
*dev
= crtc
->dev
;
2575 struct drm_framebuffer
*fb
= NULL
;
2576 struct drm_mode_fb_cmd2 fbreq
= {
2577 .width
= req
->width
,
2578 .height
= req
->height
,
2579 .pixel_format
= DRM_FORMAT_ARGB8888
,
2580 .pitches
= { req
->width
* 4 },
2581 .handles
= { req
->handle
},
2583 int32_t crtc_x
, crtc_y
;
2584 uint32_t crtc_w
= 0, crtc_h
= 0;
2585 uint32_t src_w
= 0, src_h
= 0;
2588 BUG_ON(!crtc
->cursor
);
2591 * Obtain fb we'll be using (either new or existing) and take an extra
2592 * reference to it if fb != null. setplane will take care of dropping
2593 * the reference if the plane update fails.
2595 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2597 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2599 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2603 drm_framebuffer_reference(fb
);
2608 mutex_lock(&dev
->mode_config
.mutex
);
2609 fb
= crtc
->cursor
->fb
;
2611 drm_framebuffer_reference(fb
);
2612 mutex_unlock(&dev
->mode_config
.mutex
);
2615 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2619 crtc_x
= crtc
->cursor_x
;
2620 crtc_y
= crtc
->cursor_y
;
2625 crtc_h
= fb
->height
;
2626 src_w
= fb
->width
<< 16;
2627 src_h
= fb
->height
<< 16;
2631 * setplane_internal will take care of deref'ing either the old or new
2632 * framebuffer depending on success.
2634 ret
= setplane_internal(crtc
->cursor
, crtc
, fb
,
2635 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2636 0, 0, src_w
, src_h
);
2638 /* Update successful; save new cursor position, if necessary */
2639 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2640 crtc
->cursor_x
= req
->x
;
2641 crtc
->cursor_y
= req
->y
;
2647 static int drm_mode_cursor_common(struct drm_device
*dev
,
2648 struct drm_mode_cursor2
*req
,
2649 struct drm_file
*file_priv
)
2651 struct drm_crtc
*crtc
;
2654 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2657 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2660 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2662 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2667 * If this crtc has a universal cursor plane, call that plane's update
2668 * handler rather than using legacy cursor handlers.
2671 return drm_mode_cursor_universal(crtc
, req
, file_priv
);
2673 drm_modeset_lock(&crtc
->mutex
, NULL
);
2674 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2675 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2679 /* Turns off the cursor if handle is 0 */
2680 if (crtc
->funcs
->cursor_set2
)
2681 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2682 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2684 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2685 req
->width
, req
->height
);
2688 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2689 if (crtc
->funcs
->cursor_move
) {
2690 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2697 drm_modeset_unlock(&crtc
->mutex
);
2705 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2706 * @dev: drm device for the ioctl
2707 * @data: data pointer for the ioctl
2708 * @file_priv: drm file for the ioctl call
2710 * Set the cursor configuration based on user request.
2712 * Called by the user via ioctl.
2715 * Zero on success, errno on failure.
2717 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2718 void *data
, struct drm_file
*file_priv
)
2720 struct drm_mode_cursor
*req
= data
;
2721 struct drm_mode_cursor2 new_req
;
2723 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2724 new_req
.hot_x
= new_req
.hot_y
= 0;
2726 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2730 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2731 * @dev: drm device for the ioctl
2732 * @data: data pointer for the ioctl
2733 * @file_priv: drm file for the ioctl call
2735 * Set the cursor configuration based on user request. This implements the 2nd
2736 * version of the cursor ioctl, which allows userspace to additionally specify
2737 * the hotspot of the pointer.
2739 * Called by the user via ioctl.
2742 * Zero on success, errno on failure.
2744 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2745 void *data
, struct drm_file
*file_priv
)
2747 struct drm_mode_cursor2
*req
= data
;
2748 return drm_mode_cursor_common(dev
, req
, file_priv
);
2752 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2753 * @bpp: bits per pixels
2754 * @depth: bit depth per pixel
2756 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2757 * Useful in fbdev emulation code, since that deals in those values.
2759 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2765 fmt
= DRM_FORMAT_C8
;
2769 fmt
= DRM_FORMAT_XRGB1555
;
2771 fmt
= DRM_FORMAT_RGB565
;
2774 fmt
= DRM_FORMAT_RGB888
;
2778 fmt
= DRM_FORMAT_XRGB8888
;
2779 else if (depth
== 30)
2780 fmt
= DRM_FORMAT_XRGB2101010
;
2782 fmt
= DRM_FORMAT_ARGB8888
;
2785 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2786 fmt
= DRM_FORMAT_XRGB8888
;
2792 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2795 * drm_mode_addfb - add an FB to the graphics configuration
2796 * @dev: drm device for the ioctl
2797 * @data: data pointer for the ioctl
2798 * @file_priv: drm file for the ioctl call
2800 * Add a new FB to the specified CRTC, given a user request. This is the
2801 * original addfb ioclt which only supported RGB formats.
2803 * Called by the user via ioctl.
2806 * Zero on success, errno on failure.
2808 int drm_mode_addfb(struct drm_device
*dev
,
2809 void *data
, struct drm_file
*file_priv
)
2811 struct drm_mode_fb_cmd
*or = data
;
2812 struct drm_mode_fb_cmd2 r
= {};
2813 struct drm_mode_config
*config
= &dev
->mode_config
;
2814 struct drm_framebuffer
*fb
;
2817 /* Use new struct with format internally */
2818 r
.fb_id
= or->fb_id
;
2819 r
.width
= or->width
;
2820 r
.height
= or->height
;
2821 r
.pitches
[0] = or->pitch
;
2822 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2823 r
.handles
[0] = or->handle
;
2825 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2828 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2831 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2834 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2836 DRM_DEBUG_KMS("could not create framebuffer\n");
2840 mutex_lock(&file_priv
->fbs_lock
);
2841 or->fb_id
= fb
->base
.id
;
2842 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2843 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2844 mutex_unlock(&file_priv
->fbs_lock
);
2849 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2851 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2855 case DRM_FORMAT_RGB332
:
2856 case DRM_FORMAT_BGR233
:
2857 case DRM_FORMAT_XRGB4444
:
2858 case DRM_FORMAT_XBGR4444
:
2859 case DRM_FORMAT_RGBX4444
:
2860 case DRM_FORMAT_BGRX4444
:
2861 case DRM_FORMAT_ARGB4444
:
2862 case DRM_FORMAT_ABGR4444
:
2863 case DRM_FORMAT_RGBA4444
:
2864 case DRM_FORMAT_BGRA4444
:
2865 case DRM_FORMAT_XRGB1555
:
2866 case DRM_FORMAT_XBGR1555
:
2867 case DRM_FORMAT_RGBX5551
:
2868 case DRM_FORMAT_BGRX5551
:
2869 case DRM_FORMAT_ARGB1555
:
2870 case DRM_FORMAT_ABGR1555
:
2871 case DRM_FORMAT_RGBA5551
:
2872 case DRM_FORMAT_BGRA5551
:
2873 case DRM_FORMAT_RGB565
:
2874 case DRM_FORMAT_BGR565
:
2875 case DRM_FORMAT_RGB888
:
2876 case DRM_FORMAT_BGR888
:
2877 case DRM_FORMAT_XRGB8888
:
2878 case DRM_FORMAT_XBGR8888
:
2879 case DRM_FORMAT_RGBX8888
:
2880 case DRM_FORMAT_BGRX8888
:
2881 case DRM_FORMAT_ARGB8888
:
2882 case DRM_FORMAT_ABGR8888
:
2883 case DRM_FORMAT_RGBA8888
:
2884 case DRM_FORMAT_BGRA8888
:
2885 case DRM_FORMAT_XRGB2101010
:
2886 case DRM_FORMAT_XBGR2101010
:
2887 case DRM_FORMAT_RGBX1010102
:
2888 case DRM_FORMAT_BGRX1010102
:
2889 case DRM_FORMAT_ARGB2101010
:
2890 case DRM_FORMAT_ABGR2101010
:
2891 case DRM_FORMAT_RGBA1010102
:
2892 case DRM_FORMAT_BGRA1010102
:
2893 case DRM_FORMAT_YUYV
:
2894 case DRM_FORMAT_YVYU
:
2895 case DRM_FORMAT_UYVY
:
2896 case DRM_FORMAT_VYUY
:
2897 case DRM_FORMAT_AYUV
:
2898 case DRM_FORMAT_NV12
:
2899 case DRM_FORMAT_NV21
:
2900 case DRM_FORMAT_NV16
:
2901 case DRM_FORMAT_NV61
:
2902 case DRM_FORMAT_NV24
:
2903 case DRM_FORMAT_NV42
:
2904 case DRM_FORMAT_YUV410
:
2905 case DRM_FORMAT_YVU410
:
2906 case DRM_FORMAT_YUV411
:
2907 case DRM_FORMAT_YVU411
:
2908 case DRM_FORMAT_YUV420
:
2909 case DRM_FORMAT_YVU420
:
2910 case DRM_FORMAT_YUV422
:
2911 case DRM_FORMAT_YVU422
:
2912 case DRM_FORMAT_YUV444
:
2913 case DRM_FORMAT_YVU444
:
2916 DRM_DEBUG_KMS("invalid pixel format %s\n",
2917 drm_get_format_name(r
->pixel_format
));
2922 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2924 int ret
, hsub
, vsub
, num_planes
, i
;
2926 ret
= format_check(r
);
2928 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2929 drm_get_format_name(r
->pixel_format
));
2933 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2934 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2935 num_planes
= drm_format_num_planes(r
->pixel_format
);
2937 if (r
->width
== 0 || r
->width
% hsub
) {
2938 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
2942 if (r
->height
== 0 || r
->height
% vsub
) {
2943 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
2947 for (i
= 0; i
< num_planes
; i
++) {
2948 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
2949 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
2950 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
2952 if (!r
->handles
[i
]) {
2953 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
2957 if ((uint64_t) width
* cpp
> UINT_MAX
)
2960 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
2963 if (r
->pitches
[i
] < width
* cpp
) {
2964 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
2972 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
2973 struct drm_mode_fb_cmd2
*r
,
2974 struct drm_file
*file_priv
)
2976 struct drm_mode_config
*config
= &dev
->mode_config
;
2977 struct drm_framebuffer
*fb
;
2980 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
2981 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
2982 return ERR_PTR(-EINVAL
);
2985 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
2986 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2987 r
->width
, config
->min_width
, config
->max_width
);
2988 return ERR_PTR(-EINVAL
);
2990 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
2991 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2992 r
->height
, config
->min_height
, config
->max_height
);
2993 return ERR_PTR(-EINVAL
);
2996 ret
= framebuffer_check(r
);
2998 return ERR_PTR(ret
);
3000 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3002 DRM_DEBUG_KMS("could not create framebuffer\n");
3006 mutex_lock(&file_priv
->fbs_lock
);
3007 r
->fb_id
= fb
->base
.id
;
3008 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3009 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3010 mutex_unlock(&file_priv
->fbs_lock
);
3016 * drm_mode_addfb2 - add an FB to the graphics configuration
3017 * @dev: drm device for the ioctl
3018 * @data: data pointer for the ioctl
3019 * @file_priv: drm file for the ioctl call
3021 * Add a new FB to the specified CRTC, given a user request with format. This is
3022 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3023 * and uses fourcc codes as pixel format specifiers.
3025 * Called by the user via ioctl.
3028 * Zero on success, errno on failure.
3030 int drm_mode_addfb2(struct drm_device
*dev
,
3031 void *data
, struct drm_file
*file_priv
)
3033 struct drm_framebuffer
*fb
;
3035 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3038 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3046 * drm_mode_rmfb - remove an FB from the configuration
3047 * @dev: drm device for the ioctl
3048 * @data: data pointer for the ioctl
3049 * @file_priv: drm file for the ioctl call
3051 * Remove the FB specified by the user.
3053 * Called by the user via ioctl.
3056 * Zero on success, errno on failure.
3058 int drm_mode_rmfb(struct drm_device
*dev
,
3059 void *data
, struct drm_file
*file_priv
)
3061 struct drm_framebuffer
*fb
= NULL
;
3062 struct drm_framebuffer
*fbl
= NULL
;
3063 uint32_t *id
= data
;
3066 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3069 mutex_lock(&file_priv
->fbs_lock
);
3070 mutex_lock(&dev
->mode_config
.fb_lock
);
3071 fb
= __drm_framebuffer_lookup(dev
, *id
);
3075 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3081 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3082 __drm_framebuffer_unregister(dev
, fb
);
3084 list_del_init(&fb
->filp_head
);
3085 mutex_unlock(&dev
->mode_config
.fb_lock
);
3086 mutex_unlock(&file_priv
->fbs_lock
);
3088 drm_framebuffer_remove(fb
);
3093 mutex_unlock(&dev
->mode_config
.fb_lock
);
3094 mutex_unlock(&file_priv
->fbs_lock
);
3100 * drm_mode_getfb - get FB info
3101 * @dev: drm device for the ioctl
3102 * @data: data pointer for the ioctl
3103 * @file_priv: drm file for the ioctl call
3105 * Lookup the FB given its ID and return info about it.
3107 * Called by the user via ioctl.
3110 * Zero on success, errno on failure.
3112 int drm_mode_getfb(struct drm_device
*dev
,
3113 void *data
, struct drm_file
*file_priv
)
3115 struct drm_mode_fb_cmd
*r
= data
;
3116 struct drm_framebuffer
*fb
;
3119 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3122 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3126 r
->height
= fb
->height
;
3127 r
->width
= fb
->width
;
3128 r
->depth
= fb
->depth
;
3129 r
->bpp
= fb
->bits_per_pixel
;
3130 r
->pitch
= fb
->pitches
[0];
3131 if (fb
->funcs
->create_handle
) {
3132 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3133 drm_is_control_client(file_priv
)) {
3134 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3137 /* GET_FB() is an unprivileged ioctl so we must not
3138 * return a buffer-handle to non-master processes! For
3139 * backwards-compatibility reasons, we cannot make
3140 * GET_FB() privileged, so just return an invalid handle
3141 * for non-masters. */
3149 drm_framebuffer_unreference(fb
);
3155 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3156 * @dev: drm device for the ioctl
3157 * @data: data pointer for the ioctl
3158 * @file_priv: drm file for the ioctl call
3160 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3161 * rectangle list. Generic userspace which does frontbuffer rendering must call
3162 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3163 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3165 * Modesetting drivers which always update the frontbuffer do not need to
3166 * implement the corresponding ->dirty framebuffer callback.
3168 * Called by the user via ioctl.
3171 * Zero on success, errno on failure.
3173 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3174 void *data
, struct drm_file
*file_priv
)
3176 struct drm_clip_rect __user
*clips_ptr
;
3177 struct drm_clip_rect
*clips
= NULL
;
3178 struct drm_mode_fb_dirty_cmd
*r
= data
;
3179 struct drm_framebuffer
*fb
;
3184 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3187 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3191 num_clips
= r
->num_clips
;
3192 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3194 if (!num_clips
!= !clips_ptr
) {
3199 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3201 /* If userspace annotates copy, clips must come in pairs */
3202 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3207 if (num_clips
&& clips_ptr
) {
3208 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3212 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3218 ret
= copy_from_user(clips
, clips_ptr
,
3219 num_clips
* sizeof(*clips
));
3226 if (fb
->funcs
->dirty
) {
3227 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3236 drm_framebuffer_unreference(fb
);
3243 * drm_fb_release - remove and free the FBs on this file
3244 * @priv: drm file for the ioctl
3246 * Destroy all the FBs associated with @filp.
3248 * Called by the user via ioctl.
3251 * Zero on success, errno on failure.
3253 void drm_fb_release(struct drm_file
*priv
)
3255 struct drm_device
*dev
= priv
->minor
->dev
;
3256 struct drm_framebuffer
*fb
, *tfb
;
3258 mutex_lock(&priv
->fbs_lock
);
3259 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3261 mutex_lock(&dev
->mode_config
.fb_lock
);
3262 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3263 __drm_framebuffer_unregister(dev
, fb
);
3264 mutex_unlock(&dev
->mode_config
.fb_lock
);
3266 list_del_init(&fb
->filp_head
);
3268 /* This will also drop the fpriv->fbs reference. */
3269 drm_framebuffer_remove(fb
);
3271 mutex_unlock(&priv
->fbs_lock
);
3275 * drm_property_create - create a new property type
3277 * @flags: flags specifying the property type
3278 * @name: name of the property
3279 * @num_values: number of pre-defined values
3281 * This creates a new generic drm property which can then be attached to a drm
3282 * object with drm_object_attach_property. The returned property object must be
3283 * freed with drm_property_destroy.
3286 * A pointer to the newly created property on success, NULL on failure.
3288 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3289 const char *name
, int num_values
)
3291 struct drm_property
*property
= NULL
;
3294 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3298 property
->dev
= dev
;
3301 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3302 if (!property
->values
)
3306 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3310 property
->flags
= flags
;
3311 property
->num_values
= num_values
;
3312 INIT_LIST_HEAD(&property
->enum_blob_list
);
3315 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3316 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3319 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3321 WARN_ON(!drm_property_type_valid(property
));
3325 kfree(property
->values
);
3329 EXPORT_SYMBOL(drm_property_create
);
3332 * drm_property_create - create a new enumeration property type
3334 * @flags: flags specifying the property type
3335 * @name: name of the property
3336 * @props: enumeration lists with property values
3337 * @num_values: number of pre-defined values
3339 * This creates a new generic drm property which can then be attached to a drm
3340 * object with drm_object_attach_property. The returned property object must be
3341 * freed with drm_property_destroy.
3343 * Userspace is only allowed to set one of the predefined values for enumeration
3347 * A pointer to the newly created property on success, NULL on failure.
3349 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3351 const struct drm_prop_enum_list
*props
,
3354 struct drm_property
*property
;
3357 flags
|= DRM_MODE_PROP_ENUM
;
3359 property
= drm_property_create(dev
, flags
, name
, num_values
);
3363 for (i
= 0; i
< num_values
; i
++) {
3364 ret
= drm_property_add_enum(property
, i
,
3368 drm_property_destroy(dev
, property
);
3375 EXPORT_SYMBOL(drm_property_create_enum
);
3378 * drm_property_create - create a new bitmask property type
3380 * @flags: flags specifying the property type
3381 * @name: name of the property
3382 * @props: enumeration lists with property bitflags
3383 * @num_values: number of pre-defined values
3385 * This creates a new generic drm property which can then be attached to a drm
3386 * object with drm_object_attach_property. The returned property object must be
3387 * freed with drm_property_destroy.
3389 * Compared to plain enumeration properties userspace is allowed to set any
3390 * or'ed together combination of the predefined property bitflag values
3393 * A pointer to the newly created property on success, NULL on failure.
3395 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3396 int flags
, const char *name
,
3397 const struct drm_prop_enum_list
*props
,
3399 uint64_t supported_bits
)
3401 struct drm_property
*property
;
3402 int i
, ret
, index
= 0;
3403 int num_values
= hweight64(supported_bits
);
3405 flags
|= DRM_MODE_PROP_BITMASK
;
3407 property
= drm_property_create(dev
, flags
, name
, num_values
);
3410 for (i
= 0; i
< num_props
; i
++) {
3411 if (!(supported_bits
& (1ULL << props
[i
].type
)))
3414 if (WARN_ON(index
>= num_values
)) {
3415 drm_property_destroy(dev
, property
);
3419 ret
= drm_property_add_enum(property
, index
++,
3423 drm_property_destroy(dev
, property
);
3430 EXPORT_SYMBOL(drm_property_create_bitmask
);
3432 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3433 int flags
, const char *name
,
3434 uint64_t min
, uint64_t max
)
3436 struct drm_property
*property
;
3438 property
= drm_property_create(dev
, flags
, name
, 2);
3442 property
->values
[0] = min
;
3443 property
->values
[1] = max
;
3449 * drm_property_create - create a new ranged property type
3451 * @flags: flags specifying the property type
3452 * @name: name of the property
3453 * @min: minimum value of the property
3454 * @max: maximum value of the property
3456 * This creates a new generic drm property which can then be attached to a drm
3457 * object with drm_object_attach_property. The returned property object must be
3458 * freed with drm_property_destroy.
3460 * Userspace is allowed to set any interger value in the (min, max) range
3464 * A pointer to the newly created property on success, NULL on failure.
3466 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3468 uint64_t min
, uint64_t max
)
3470 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3473 EXPORT_SYMBOL(drm_property_create_range
);
3475 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3476 int flags
, const char *name
,
3477 int64_t min
, int64_t max
)
3479 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3480 name
, I642U64(min
), I642U64(max
));
3482 EXPORT_SYMBOL(drm_property_create_signed_range
);
3484 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3485 int flags
, const char *name
, uint32_t type
)
3487 struct drm_property
*property
;
3489 flags
|= DRM_MODE_PROP_OBJECT
;
3491 property
= drm_property_create(dev
, flags
, name
, 1);
3495 property
->values
[0] = type
;
3499 EXPORT_SYMBOL(drm_property_create_object
);
3502 * drm_property_add_enum - add a possible value to an enumeration property
3503 * @property: enumeration property to change
3504 * @index: index of the new enumeration
3505 * @value: value of the new enumeration
3506 * @name: symbolic name of the new enumeration
3508 * This functions adds enumerations to a property.
3510 * It's use is deprecated, drivers should use one of the more specific helpers
3511 * to directly create the property with all enumerations already attached.
3514 * Zero on success, error code on failure.
3516 int drm_property_add_enum(struct drm_property
*property
, int index
,
3517 uint64_t value
, const char *name
)
3519 struct drm_property_enum
*prop_enum
;
3521 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3522 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3526 * Bitmask enum properties have the additional constraint of values
3529 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3533 if (!list_empty(&property
->enum_blob_list
)) {
3534 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3535 if (prop_enum
->value
== value
) {
3536 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3537 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3543 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3547 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3548 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3549 prop_enum
->value
= value
;
3551 property
->values
[index
] = value
;
3552 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3555 EXPORT_SYMBOL(drm_property_add_enum
);
3558 * drm_property_destroy - destroy a drm property
3560 * @property: property to destry
3562 * This function frees a property including any attached resources like
3563 * enumeration values.
3565 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3567 struct drm_property_enum
*prop_enum
, *pt
;
3569 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3570 list_del(&prop_enum
->head
);
3574 if (property
->num_values
)
3575 kfree(property
->values
);
3576 drm_mode_object_put(dev
, &property
->base
);
3577 list_del(&property
->head
);
3580 EXPORT_SYMBOL(drm_property_destroy
);
3583 * drm_object_attach_property - attach a property to a modeset object
3584 * @obj: drm modeset object
3585 * @property: property to attach
3586 * @init_val: initial value of the property
3588 * This attaches the given property to the modeset object with the given initial
3589 * value. Currently this function cannot fail since the properties are stored in
3590 * a statically sized array.
3592 void drm_object_attach_property(struct drm_mode_object
*obj
,
3593 struct drm_property
*property
,
3596 int count
= obj
->properties
->count
;
3598 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3599 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3600 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3601 "you see this message on the same object type.\n",
3606 obj
->properties
->ids
[count
] = property
->base
.id
;
3607 obj
->properties
->values
[count
] = init_val
;
3608 obj
->properties
->count
++;
3610 EXPORT_SYMBOL(drm_object_attach_property
);
3613 * drm_object_property_set_value - set the value of a property
3614 * @obj: drm mode object to set property value for
3615 * @property: property to set
3616 * @val: value the property should be set to
3618 * This functions sets a given property on a given object. This function only
3619 * changes the software state of the property, it does not call into the
3620 * driver's ->set_property callback.
3623 * Zero on success, error code on failure.
3625 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3626 struct drm_property
*property
, uint64_t val
)
3630 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3631 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3632 obj
->properties
->values
[i
] = val
;
3639 EXPORT_SYMBOL(drm_object_property_set_value
);
3642 * drm_object_property_get_value - retrieve the value of a property
3643 * @obj: drm mode object to get property value from
3644 * @property: property to retrieve
3645 * @val: storage for the property value
3647 * This function retrieves the softare state of the given property for the given
3648 * property. Since there is no driver callback to retrieve the current property
3649 * value this might be out of sync with the hardware, depending upon the driver
3653 * Zero on success, error code on failure.
3655 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3656 struct drm_property
*property
, uint64_t *val
)
3660 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3661 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3662 *val
= obj
->properties
->values
[i
];
3669 EXPORT_SYMBOL(drm_object_property_get_value
);
3672 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3675 * @file_priv: DRM file info
3677 * This function retrieves the current value for an connectors's property.
3679 * Called by the user via ioctl.
3682 * Zero on success, errno on failure.
3684 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3685 void *data
, struct drm_file
*file_priv
)
3687 struct drm_mode_get_property
*out_resp
= data
;
3688 struct drm_property
*property
;
3691 int value_count
= 0;
3694 struct drm_property_enum
*prop_enum
;
3695 struct drm_mode_property_enum __user
*enum_ptr
;
3696 struct drm_property_blob
*prop_blob
;
3697 uint32_t __user
*blob_id_ptr
;
3698 uint64_t __user
*values_ptr
;
3699 uint32_t __user
*blob_length_ptr
;
3701 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3704 drm_modeset_lock_all(dev
);
3705 property
= drm_property_find(dev
, out_resp
->prop_id
);
3711 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3712 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3713 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3715 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3716 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3720 value_count
= property
->num_values
;
3722 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3723 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3724 out_resp
->flags
= property
->flags
;
3726 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3727 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3728 for (i
= 0; i
< value_count
; i
++) {
3729 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3735 out_resp
->count_values
= value_count
;
3737 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3738 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3739 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3741 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3742 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3744 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3749 if (copy_to_user(&enum_ptr
[copied
].name
,
3750 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3757 out_resp
->count_enum_blobs
= enum_count
;
3760 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3761 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3763 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3764 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3766 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3767 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3772 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3780 out_resp
->count_enum_blobs
= blob_count
;
3783 drm_modeset_unlock_all(dev
);
3787 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3790 struct drm_property_blob
*blob
;
3793 if (!length
|| !data
)
3796 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3800 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3806 blob
->length
= length
;
3808 memcpy(blob
->data
, data
, length
);
3810 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3814 static void drm_property_destroy_blob(struct drm_device
*dev
,
3815 struct drm_property_blob
*blob
)
3817 drm_mode_object_put(dev
, &blob
->base
);
3818 list_del(&blob
->head
);
3823 * drm_mode_getblob_ioctl - get the contents of a blob property value
3826 * @file_priv: DRM file info
3828 * This function retrieves the contents of a blob property. The value stored in
3829 * an object's blob property is just a normal modeset object id.
3831 * Called by the user via ioctl.
3834 * Zero on success, errno on failure.
3836 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3837 void *data
, struct drm_file
*file_priv
)
3839 struct drm_mode_get_blob
*out_resp
= data
;
3840 struct drm_property_blob
*blob
;
3842 void __user
*blob_ptr
;
3844 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3847 drm_modeset_lock_all(dev
);
3848 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3854 if (out_resp
->length
== blob
->length
) {
3855 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3856 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3861 out_resp
->length
= blob
->length
;
3864 drm_modeset_unlock_all(dev
);
3869 * drm_mode_connector_update_edid_property - update the edid property of a connector
3870 * @connector: drm connector
3871 * @edid: new value of the edid property
3873 * This function creates a new blob modeset object and assigns its id to the
3874 * connector's edid property.
3877 * Zero on success, errno on failure.
3879 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3882 struct drm_device
*dev
= connector
->dev
;
3885 if (connector
->edid_blob_ptr
)
3886 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3888 /* Delete edid, when there is none. */
3890 connector
->edid_blob_ptr
= NULL
;
3891 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3895 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3896 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3898 if (!connector
->edid_blob_ptr
)
3901 ret
= drm_object_property_set_value(&connector
->base
,
3902 dev
->mode_config
.edid_property
,
3903 connector
->edid_blob_ptr
->base
.id
);
3907 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3909 static bool drm_property_change_is_valid(struct drm_property
*property
,
3912 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3915 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3916 if (value
< property
->values
[0] || value
> property
->values
[1])
3919 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3920 int64_t svalue
= U642I64(value
);
3921 if (svalue
< U642I64(property
->values
[0]) ||
3922 svalue
> U642I64(property
->values
[1]))
3925 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3927 uint64_t valid_mask
= 0;
3928 for (i
= 0; i
< property
->num_values
; i
++)
3929 valid_mask
|= (1ULL << property
->values
[i
]);
3930 return !(value
& ~valid_mask
);
3931 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3932 /* Only the driver knows */
3934 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
3935 struct drm_mode_object
*obj
;
3936 /* a zero value for an object property translates to null: */
3940 * NOTE: use _object_find() directly to bypass restriction on
3941 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3942 * object this could race against object finalization, so it
3943 * simply tells us that the object *was* valid. Which is good
3946 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
3950 for (i
= 0; i
< property
->num_values
; i
++)
3951 if (property
->values
[i
] == value
)
3958 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3961 * @file_priv: DRM file info
3963 * This function sets the current value for a connectors's property. It also
3964 * calls into a driver's ->set_property callback to update the hardware state
3966 * Called by the user via ioctl.
3969 * Zero on success, errno on failure.
3971 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
3972 void *data
, struct drm_file
*file_priv
)
3974 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
3975 struct drm_mode_obj_set_property obj_set_prop
= {
3976 .value
= conn_set_prop
->value
,
3977 .prop_id
= conn_set_prop
->prop_id
,
3978 .obj_id
= conn_set_prop
->connector_id
,
3979 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
3982 /* It does all the locking and checking we need */
3983 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
3986 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
3987 struct drm_property
*property
,
3991 struct drm_connector
*connector
= obj_to_connector(obj
);
3993 /* Do DPMS ourselves */
3994 if (property
== connector
->dev
->mode_config
.dpms_property
) {
3995 if (connector
->funcs
->dpms
)
3996 (*connector
->funcs
->dpms
)(connector
, (int)value
);
3998 } else if (connector
->funcs
->set_property
)
3999 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4001 /* store the property value if successful */
4003 drm_object_property_set_value(&connector
->base
, property
, value
);
4007 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4008 struct drm_property
*property
,
4012 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4014 if (crtc
->funcs
->set_property
)
4015 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4017 drm_object_property_set_value(obj
, property
, value
);
4022 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
4023 struct drm_property
*property
,
4027 struct drm_plane
*plane
= obj_to_plane(obj
);
4029 if (plane
->funcs
->set_property
)
4030 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4032 drm_object_property_set_value(obj
, property
, value
);
4038 * drm_mode_getproperty_ioctl - get the current value of a object's property
4041 * @file_priv: DRM file info
4043 * This function retrieves the current value for an object's property. Compared
4044 * to the connector specific ioctl this one is extended to also work on crtc and
4047 * Called by the user via ioctl.
4050 * Zero on success, errno on failure.
4052 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4053 struct drm_file
*file_priv
)
4055 struct drm_mode_obj_get_properties
*arg
= data
;
4056 struct drm_mode_object
*obj
;
4060 int props_count
= 0;
4061 uint32_t __user
*props_ptr
;
4062 uint64_t __user
*prop_values_ptr
;
4064 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4067 drm_modeset_lock_all(dev
);
4069 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4074 if (!obj
->properties
) {
4079 props_count
= obj
->properties
->count
;
4081 /* This ioctl is called twice, once to determine how much space is
4082 * needed, and the 2nd time to fill it. */
4083 if ((arg
->count_props
>= props_count
) && props_count
) {
4085 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
4086 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
4087 (arg
->prop_values_ptr
);
4088 for (i
= 0; i
< props_count
; i
++) {
4089 if (put_user(obj
->properties
->ids
[i
],
4090 props_ptr
+ copied
)) {
4094 if (put_user(obj
->properties
->values
[i
],
4095 prop_values_ptr
+ copied
)) {
4102 arg
->count_props
= props_count
;
4104 drm_modeset_unlock_all(dev
);
4109 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4112 * @file_priv: DRM file info
4114 * This function sets the current value for an object's property. It also calls
4115 * into a driver's ->set_property callback to update the hardware state.
4116 * Compared to the connector specific ioctl this one is extended to also work on
4117 * crtc and plane objects.
4119 * Called by the user via ioctl.
4122 * Zero on success, errno on failure.
4124 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4125 struct drm_file
*file_priv
)
4127 struct drm_mode_obj_set_property
*arg
= data
;
4128 struct drm_mode_object
*arg_obj
;
4129 struct drm_mode_object
*prop_obj
;
4130 struct drm_property
*property
;
4134 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4137 drm_modeset_lock_all(dev
);
4139 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4144 if (!arg_obj
->properties
)
4147 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4148 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4151 if (i
== arg_obj
->properties
->count
)
4154 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4155 DRM_MODE_OBJECT_PROPERTY
);
4160 property
= obj_to_property(prop_obj
);
4162 if (!drm_property_change_is_valid(property
, arg
->value
))
4165 switch (arg_obj
->type
) {
4166 case DRM_MODE_OBJECT_CONNECTOR
:
4167 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4170 case DRM_MODE_OBJECT_CRTC
:
4171 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4173 case DRM_MODE_OBJECT_PLANE
:
4174 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4179 drm_modeset_unlock_all(dev
);
4184 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4185 * @connector: connector to attach
4186 * @encoder: encoder to attach @connector to
4188 * This function links up a connector to an encoder. Note that the routing
4189 * restrictions between encoders and crtcs are exposed to userspace through the
4190 * possible_clones and possible_crtcs bitmasks.
4193 * Zero on success, errno on failure.
4195 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4196 struct drm_encoder
*encoder
)
4200 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4201 if (connector
->encoder_ids
[i
] == 0) {
4202 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4208 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4211 * drm_mode_crtc_set_gamma_size - set the gamma table size
4212 * @crtc: CRTC to set the gamma table size for
4213 * @gamma_size: size of the gamma table
4215 * Drivers which support gamma tables should set this to the supported gamma
4216 * table size when initializing the CRTC. Currently the drm core only supports a
4217 * fixed gamma table size.
4220 * Zero on success, errno on failure.
4222 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4225 crtc
->gamma_size
= gamma_size
;
4227 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4228 if (!crtc
->gamma_store
) {
4229 crtc
->gamma_size
= 0;
4235 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4238 * drm_mode_gamma_set_ioctl - set the gamma table
4241 * @file_priv: DRM file info
4243 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4244 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4246 * Called by the user via ioctl.
4249 * Zero on success, errno on failure.
4251 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4252 void *data
, struct drm_file
*file_priv
)
4254 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4255 struct drm_crtc
*crtc
;
4256 void *r_base
, *g_base
, *b_base
;
4260 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4263 drm_modeset_lock_all(dev
);
4264 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4270 if (crtc
->funcs
->gamma_set
== NULL
) {
4275 /* memcpy into gamma store */
4276 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4281 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4282 r_base
= crtc
->gamma_store
;
4283 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4288 g_base
= r_base
+ size
;
4289 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4294 b_base
= g_base
+ size
;
4295 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4300 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4303 drm_modeset_unlock_all(dev
);
4309 * drm_mode_gamma_get_ioctl - get the gamma table
4312 * @file_priv: DRM file info
4314 * Copy the current gamma table into the storage provided. This also provides
4315 * the gamma table size the driver expects, which can be used to size the
4316 * allocated storage.
4318 * Called by the user via ioctl.
4321 * Zero on success, errno on failure.
4323 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4324 void *data
, struct drm_file
*file_priv
)
4326 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4327 struct drm_crtc
*crtc
;
4328 void *r_base
, *g_base
, *b_base
;
4332 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4335 drm_modeset_lock_all(dev
);
4336 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4342 /* memcpy into gamma store */
4343 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4348 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4349 r_base
= crtc
->gamma_store
;
4350 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4355 g_base
= r_base
+ size
;
4356 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4361 b_base
= g_base
+ size
;
4362 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4367 drm_modeset_unlock_all(dev
);
4372 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4375 * @file_priv: DRM file info
4377 * This schedules an asynchronous update on a given CRTC, called page flip.
4378 * Optionally a drm event is generated to signal the completion of the event.
4379 * Generic drivers cannot assume that a pageflip with changed framebuffer
4380 * properties (including driver specific metadata like tiling layout) will work,
4381 * but some drivers support e.g. pixel format changes through the pageflip
4384 * Called by the user via ioctl.
4387 * Zero on success, errno on failure.
4389 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4390 void *data
, struct drm_file
*file_priv
)
4392 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4393 struct drm_crtc
*crtc
;
4394 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4395 struct drm_pending_vblank_event
*e
= NULL
;
4396 unsigned long flags
;
4399 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4400 page_flip
->reserved
!= 0)
4403 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4406 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4410 drm_modeset_lock(&crtc
->mutex
, NULL
);
4411 if (crtc
->primary
->fb
== NULL
) {
4412 /* The framebuffer is currently unbound, presumably
4413 * due to a hotplug event, that userspace has not
4420 if (crtc
->funcs
->page_flip
== NULL
)
4423 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4429 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4433 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4434 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4439 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4441 spin_lock_irqsave(&dev
->event_lock
, flags
);
4442 if (file_priv
->event_space
< sizeof e
->event
) {
4443 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4446 file_priv
->event_space
-= sizeof e
->event
;
4447 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4449 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4451 spin_lock_irqsave(&dev
->event_lock
, flags
);
4452 file_priv
->event_space
+= sizeof e
->event
;
4453 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4457 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4458 e
->event
.base
.length
= sizeof e
->event
;
4459 e
->event
.user_data
= page_flip
->user_data
;
4460 e
->base
.event
= &e
->event
.base
;
4461 e
->base
.file_priv
= file_priv
;
4463 (void (*) (struct drm_pending_event
*)) kfree
;
4466 old_fb
= crtc
->primary
->fb
;
4467 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4469 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4470 spin_lock_irqsave(&dev
->event_lock
, flags
);
4471 file_priv
->event_space
+= sizeof e
->event
;
4472 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4475 /* Keep the old fb, don't unref it. */
4479 * Warn if the driver hasn't properly updated the crtc->fb
4480 * field to reflect that the new framebuffer is now used.
4481 * Failing to do so will screw with the reference counting
4484 WARN_ON(crtc
->primary
->fb
!= fb
);
4485 /* Unref only the old framebuffer. */
4491 drm_framebuffer_unreference(fb
);
4493 drm_framebuffer_unreference(old_fb
);
4494 drm_modeset_unlock(&crtc
->mutex
);
4500 * drm_mode_config_reset - call ->reset callbacks
4503 * This functions calls all the crtc's, encoder's and connector's ->reset
4504 * callback. Drivers can use this in e.g. their driver load or resume code to
4505 * reset hardware and software state.
4507 void drm_mode_config_reset(struct drm_device
*dev
)
4509 struct drm_crtc
*crtc
;
4510 struct drm_encoder
*encoder
;
4511 struct drm_connector
*connector
;
4513 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4514 if (crtc
->funcs
->reset
)
4515 crtc
->funcs
->reset(crtc
);
4517 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4518 if (encoder
->funcs
->reset
)
4519 encoder
->funcs
->reset(encoder
);
4521 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4522 connector
->status
= connector_status_unknown
;
4524 if (connector
->funcs
->reset
)
4525 connector
->funcs
->reset(connector
);
4528 EXPORT_SYMBOL(drm_mode_config_reset
);
4531 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4534 * @file_priv: DRM file info
4536 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4537 * TTM or something else entirely) and returns the resulting buffer handle. This
4538 * handle can then be wrapped up into a framebuffer modeset object.
4540 * Note that userspace is not allowed to use such objects for render
4541 * acceleration - drivers must create their own private ioctls for such a use
4544 * Called by the user via ioctl.
4547 * Zero on success, errno on failure.
4549 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4550 void *data
, struct drm_file
*file_priv
)
4552 struct drm_mode_create_dumb
*args
= data
;
4553 u32 cpp
, stride
, size
;
4555 if (!dev
->driver
->dumb_create
)
4557 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4560 /* overflow checks for 32bit size calculations */
4561 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4562 if (cpp
> 0xffffffffU
/ args
->width
)
4564 stride
= cpp
* args
->width
;
4565 if (args
->height
> 0xffffffffU
/ stride
)
4568 /* test for wrap-around */
4569 size
= args
->height
* stride
;
4570 if (PAGE_ALIGN(size
) == 0)
4573 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4577 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4580 * @file_priv: DRM file info
4582 * Allocate an offset in the drm device node's address space to be able to
4583 * memory map a dumb buffer.
4585 * Called by the user via ioctl.
4588 * Zero on success, errno on failure.
4590 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4591 void *data
, struct drm_file
*file_priv
)
4593 struct drm_mode_map_dumb
*args
= data
;
4595 /* call driver ioctl to get mmap offset */
4596 if (!dev
->driver
->dumb_map_offset
)
4599 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4603 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4606 * @file_priv: DRM file info
4608 * This destroys the userspace handle for the given dumb backing storage buffer.
4609 * Since buffer objects must be reference counted in the kernel a buffer object
4610 * won't be immediately freed if a framebuffer modeset object still uses it.
4612 * Called by the user via ioctl.
4615 * Zero on success, errno on failure.
4617 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4618 void *data
, struct drm_file
*file_priv
)
4620 struct drm_mode_destroy_dumb
*args
= data
;
4622 if (!dev
->driver
->dumb_destroy
)
4625 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4629 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4630 * @format: pixel format (DRM_FORMAT_*)
4631 * @depth: storage for the depth value
4632 * @bpp: storage for the bpp value
4634 * This only supports RGB formats here for compat with code that doesn't use
4635 * pixel formats directly yet.
4637 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4642 case DRM_FORMAT_RGB332
:
4643 case DRM_FORMAT_BGR233
:
4647 case DRM_FORMAT_XRGB1555
:
4648 case DRM_FORMAT_XBGR1555
:
4649 case DRM_FORMAT_RGBX5551
:
4650 case DRM_FORMAT_BGRX5551
:
4651 case DRM_FORMAT_ARGB1555
:
4652 case DRM_FORMAT_ABGR1555
:
4653 case DRM_FORMAT_RGBA5551
:
4654 case DRM_FORMAT_BGRA5551
:
4658 case DRM_FORMAT_RGB565
:
4659 case DRM_FORMAT_BGR565
:
4663 case DRM_FORMAT_RGB888
:
4664 case DRM_FORMAT_BGR888
:
4668 case DRM_FORMAT_XRGB8888
:
4669 case DRM_FORMAT_XBGR8888
:
4670 case DRM_FORMAT_RGBX8888
:
4671 case DRM_FORMAT_BGRX8888
:
4675 case DRM_FORMAT_XRGB2101010
:
4676 case DRM_FORMAT_XBGR2101010
:
4677 case DRM_FORMAT_RGBX1010102
:
4678 case DRM_FORMAT_BGRX1010102
:
4679 case DRM_FORMAT_ARGB2101010
:
4680 case DRM_FORMAT_ABGR2101010
:
4681 case DRM_FORMAT_RGBA1010102
:
4682 case DRM_FORMAT_BGRA1010102
:
4686 case DRM_FORMAT_ARGB8888
:
4687 case DRM_FORMAT_ABGR8888
:
4688 case DRM_FORMAT_RGBA8888
:
4689 case DRM_FORMAT_BGRA8888
:
4694 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4695 drm_get_format_name(format
));
4701 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4704 * drm_format_num_planes - get the number of planes for format
4705 * @format: pixel format (DRM_FORMAT_*)
4708 * The number of planes used by the specified pixel format.
4710 int drm_format_num_planes(uint32_t format
)
4713 case DRM_FORMAT_YUV410
:
4714 case DRM_FORMAT_YVU410
:
4715 case DRM_FORMAT_YUV411
:
4716 case DRM_FORMAT_YVU411
:
4717 case DRM_FORMAT_YUV420
:
4718 case DRM_FORMAT_YVU420
:
4719 case DRM_FORMAT_YUV422
:
4720 case DRM_FORMAT_YVU422
:
4721 case DRM_FORMAT_YUV444
:
4722 case DRM_FORMAT_YVU444
:
4724 case DRM_FORMAT_NV12
:
4725 case DRM_FORMAT_NV21
:
4726 case DRM_FORMAT_NV16
:
4727 case DRM_FORMAT_NV61
:
4728 case DRM_FORMAT_NV24
:
4729 case DRM_FORMAT_NV42
:
4735 EXPORT_SYMBOL(drm_format_num_planes
);
4738 * drm_format_plane_cpp - determine the bytes per pixel value
4739 * @format: pixel format (DRM_FORMAT_*)
4740 * @plane: plane index
4743 * The bytes per pixel value for the specified plane.
4745 int drm_format_plane_cpp(uint32_t format
, int plane
)
4750 if (plane
>= drm_format_num_planes(format
))
4754 case DRM_FORMAT_YUYV
:
4755 case DRM_FORMAT_YVYU
:
4756 case DRM_FORMAT_UYVY
:
4757 case DRM_FORMAT_VYUY
:
4759 case DRM_FORMAT_NV12
:
4760 case DRM_FORMAT_NV21
:
4761 case DRM_FORMAT_NV16
:
4762 case DRM_FORMAT_NV61
:
4763 case DRM_FORMAT_NV24
:
4764 case DRM_FORMAT_NV42
:
4765 return plane
? 2 : 1;
4766 case DRM_FORMAT_YUV410
:
4767 case DRM_FORMAT_YVU410
:
4768 case DRM_FORMAT_YUV411
:
4769 case DRM_FORMAT_YVU411
:
4770 case DRM_FORMAT_YUV420
:
4771 case DRM_FORMAT_YVU420
:
4772 case DRM_FORMAT_YUV422
:
4773 case DRM_FORMAT_YVU422
:
4774 case DRM_FORMAT_YUV444
:
4775 case DRM_FORMAT_YVU444
:
4778 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4782 EXPORT_SYMBOL(drm_format_plane_cpp
);
4785 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4786 * @format: pixel format (DRM_FORMAT_*)
4789 * The horizontal chroma subsampling factor for the
4790 * specified pixel format.
4792 int drm_format_horz_chroma_subsampling(uint32_t format
)
4795 case DRM_FORMAT_YUV411
:
4796 case DRM_FORMAT_YVU411
:
4797 case DRM_FORMAT_YUV410
:
4798 case DRM_FORMAT_YVU410
:
4800 case DRM_FORMAT_YUYV
:
4801 case DRM_FORMAT_YVYU
:
4802 case DRM_FORMAT_UYVY
:
4803 case DRM_FORMAT_VYUY
:
4804 case DRM_FORMAT_NV12
:
4805 case DRM_FORMAT_NV21
:
4806 case DRM_FORMAT_NV16
:
4807 case DRM_FORMAT_NV61
:
4808 case DRM_FORMAT_YUV422
:
4809 case DRM_FORMAT_YVU422
:
4810 case DRM_FORMAT_YUV420
:
4811 case DRM_FORMAT_YVU420
:
4817 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4820 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4821 * @format: pixel format (DRM_FORMAT_*)
4824 * The vertical chroma subsampling factor for the
4825 * specified pixel format.
4827 int drm_format_vert_chroma_subsampling(uint32_t format
)
4830 case DRM_FORMAT_YUV410
:
4831 case DRM_FORMAT_YVU410
:
4833 case DRM_FORMAT_YUV420
:
4834 case DRM_FORMAT_YVU420
:
4835 case DRM_FORMAT_NV12
:
4836 case DRM_FORMAT_NV21
:
4842 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4845 * drm_mode_config_init - initialize DRM mode_configuration structure
4848 * Initialize @dev's mode_config structure, used for tracking the graphics
4849 * configuration of @dev.
4851 * Since this initializes the modeset locks, no locking is possible. Which is no
4852 * problem, since this should happen single threaded at init time. It is the
4853 * driver's problem to ensure this guarantee.
4856 void drm_mode_config_init(struct drm_device
*dev
)
4858 mutex_init(&dev
->mode_config
.mutex
);
4859 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
4860 mutex_init(&dev
->mode_config
.idr_mutex
);
4861 mutex_init(&dev
->mode_config
.fb_lock
);
4862 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4863 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4864 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4865 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4866 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4867 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4868 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4869 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4870 idr_init(&dev
->mode_config
.crtc_idr
);
4872 drm_modeset_lock_all(dev
);
4873 drm_mode_create_standard_connector_properties(dev
);
4874 drm_mode_create_standard_plane_properties(dev
);
4875 drm_modeset_unlock_all(dev
);
4877 /* Just to be sure */
4878 dev
->mode_config
.num_fb
= 0;
4879 dev
->mode_config
.num_connector
= 0;
4880 dev
->mode_config
.num_crtc
= 0;
4881 dev
->mode_config
.num_encoder
= 0;
4882 dev
->mode_config
.num_overlay_plane
= 0;
4883 dev
->mode_config
.num_total_plane
= 0;
4885 EXPORT_SYMBOL(drm_mode_config_init
);
4888 * drm_mode_config_cleanup - free up DRM mode_config info
4891 * Free up all the connectors and CRTCs associated with this DRM device, then
4892 * free up the framebuffers and associated buffer objects.
4894 * Note that since this /should/ happen single-threaded at driver/device
4895 * teardown time, no locking is required. It's the driver's job to ensure that
4896 * this guarantee actually holds true.
4898 * FIXME: cleanup any dangling user buffer objects too
4900 void drm_mode_config_cleanup(struct drm_device
*dev
)
4902 struct drm_connector
*connector
, *ot
;
4903 struct drm_crtc
*crtc
, *ct
;
4904 struct drm_encoder
*encoder
, *enct
;
4905 struct drm_bridge
*bridge
, *brt
;
4906 struct drm_framebuffer
*fb
, *fbt
;
4907 struct drm_property
*property
, *pt
;
4908 struct drm_property_blob
*blob
, *bt
;
4909 struct drm_plane
*plane
, *plt
;
4911 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4913 encoder
->funcs
->destroy(encoder
);
4916 list_for_each_entry_safe(bridge
, brt
,
4917 &dev
->mode_config
.bridge_list
, head
) {
4918 bridge
->funcs
->destroy(bridge
);
4921 list_for_each_entry_safe(connector
, ot
,
4922 &dev
->mode_config
.connector_list
, head
) {
4923 connector
->funcs
->destroy(connector
);
4926 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
4928 drm_property_destroy(dev
, property
);
4931 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
4933 drm_property_destroy_blob(dev
, blob
);
4937 * Single-threaded teardown context, so it's not required to grab the
4938 * fb_lock to protect against concurrent fb_list access. Contrary, it
4939 * would actually deadlock with the drm_framebuffer_cleanup function.
4941 * Also, if there are any framebuffers left, that's a driver leak now,
4942 * so politely WARN about this.
4944 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
4945 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
4946 drm_framebuffer_remove(fb
);
4949 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
4951 plane
->funcs
->destroy(plane
);
4954 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
4955 crtc
->funcs
->destroy(crtc
);
4958 idr_destroy(&dev
->mode_config
.crtc_idr
);
4959 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
4961 EXPORT_SYMBOL(drm_mode_config_cleanup
);