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);
891 connector
->debugfs_entry
= NULL
;
895 drm_mode_object_put(dev
, &connector
->base
);
898 drm_modeset_unlock_all(dev
);
902 EXPORT_SYMBOL(drm_connector_init
);
905 * drm_connector_cleanup - cleans up an initialised connector
906 * @connector: connector to cleanup
908 * Cleans up the connector but doesn't free the object.
910 void drm_connector_cleanup(struct drm_connector
*connector
)
912 struct drm_device
*dev
= connector
->dev
;
913 struct drm_display_mode
*mode
, *t
;
915 list_for_each_entry_safe(mode
, t
, &connector
->probed_modes
, head
)
916 drm_mode_remove(connector
, mode
);
918 list_for_each_entry_safe(mode
, t
, &connector
->modes
, head
)
919 drm_mode_remove(connector
, mode
);
921 ida_remove(&drm_connector_enum_list
[connector
->connector_type
].ida
,
922 connector
->connector_type_id
);
924 drm_mode_object_put(dev
, &connector
->base
);
925 kfree(connector
->name
);
926 connector
->name
= NULL
;
927 list_del(&connector
->head
);
928 dev
->mode_config
.num_connector
--;
930 EXPORT_SYMBOL(drm_connector_cleanup
);
933 * drm_connector_register - register a connector
934 * @connector: the connector to register
936 * Register userspace interfaces for a connector
939 * Zero on success, error code on failure.
941 int drm_connector_register(struct drm_connector
*connector
)
945 ret
= drm_sysfs_connector_add(connector
);
949 ret
= drm_debugfs_connector_add(connector
);
951 drm_sysfs_connector_remove(connector
);
957 EXPORT_SYMBOL(drm_connector_register
);
960 * drm_connector_unregister - unregister a connector
961 * @connector: the connector to unregister
963 * Unregister userspace interfaces for a connector
965 void drm_connector_unregister(struct drm_connector
*connector
)
967 drm_sysfs_connector_remove(connector
);
968 drm_debugfs_connector_remove(connector
);
970 EXPORT_SYMBOL(drm_connector_unregister
);
974 * drm_connector_unplug_all - unregister connector userspace interfaces
977 * This function unregisters all connector userspace interfaces in sysfs. Should
978 * be call when the device is disconnected, e.g. from an usb driver's
979 * ->disconnect callback.
981 void drm_connector_unplug_all(struct drm_device
*dev
)
983 struct drm_connector
*connector
;
985 /* taking the mode config mutex ends up in a clash with sysfs */
986 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
987 drm_connector_unregister(connector
);
990 EXPORT_SYMBOL(drm_connector_unplug_all
);
993 * drm_bridge_init - initialize a drm transcoder/bridge
995 * @bridge: transcoder/bridge to set up
996 * @funcs: bridge function table
998 * Initialises a preallocated bridge. Bridges should be
999 * subclassed as part of driver connector objects.
1002 * Zero on success, error code on failure.
1004 int drm_bridge_init(struct drm_device
*dev
, struct drm_bridge
*bridge
,
1005 const struct drm_bridge_funcs
*funcs
)
1009 drm_modeset_lock_all(dev
);
1011 ret
= drm_mode_object_get(dev
, &bridge
->base
, DRM_MODE_OBJECT_BRIDGE
);
1016 bridge
->funcs
= funcs
;
1018 list_add_tail(&bridge
->head
, &dev
->mode_config
.bridge_list
);
1019 dev
->mode_config
.num_bridge
++;
1022 drm_modeset_unlock_all(dev
);
1025 EXPORT_SYMBOL(drm_bridge_init
);
1028 * drm_bridge_cleanup - cleans up an initialised bridge
1029 * @bridge: bridge to cleanup
1031 * Cleans up the bridge but doesn't free the object.
1033 void drm_bridge_cleanup(struct drm_bridge
*bridge
)
1035 struct drm_device
*dev
= bridge
->dev
;
1037 drm_modeset_lock_all(dev
);
1038 drm_mode_object_put(dev
, &bridge
->base
);
1039 list_del(&bridge
->head
);
1040 dev
->mode_config
.num_bridge
--;
1041 drm_modeset_unlock_all(dev
);
1043 EXPORT_SYMBOL(drm_bridge_cleanup
);
1046 * drm_encoder_init - Init a preallocated encoder
1048 * @encoder: the encoder to init
1049 * @funcs: callbacks for this encoder
1050 * @encoder_type: user visible type of the encoder
1052 * Initialises a preallocated encoder. Encoder should be
1053 * subclassed as part of driver encoder objects.
1056 * Zero on success, error code on failure.
1058 int drm_encoder_init(struct drm_device
*dev
,
1059 struct drm_encoder
*encoder
,
1060 const struct drm_encoder_funcs
*funcs
,
1065 drm_modeset_lock_all(dev
);
1067 ret
= drm_mode_object_get(dev
, &encoder
->base
, DRM_MODE_OBJECT_ENCODER
);
1072 encoder
->encoder_type
= encoder_type
;
1073 encoder
->funcs
= funcs
;
1074 encoder
->name
= kasprintf(GFP_KERNEL
, "%s-%d",
1075 drm_encoder_enum_list
[encoder_type
].name
,
1077 if (!encoder
->name
) {
1082 list_add_tail(&encoder
->head
, &dev
->mode_config
.encoder_list
);
1083 dev
->mode_config
.num_encoder
++;
1087 drm_mode_object_put(dev
, &encoder
->base
);
1090 drm_modeset_unlock_all(dev
);
1094 EXPORT_SYMBOL(drm_encoder_init
);
1097 * drm_encoder_cleanup - cleans up an initialised encoder
1098 * @encoder: encoder to cleanup
1100 * Cleans up the encoder but doesn't free the object.
1102 void drm_encoder_cleanup(struct drm_encoder
*encoder
)
1104 struct drm_device
*dev
= encoder
->dev
;
1105 drm_modeset_lock_all(dev
);
1106 drm_mode_object_put(dev
, &encoder
->base
);
1107 kfree(encoder
->name
);
1108 encoder
->name
= NULL
;
1109 list_del(&encoder
->head
);
1110 dev
->mode_config
.num_encoder
--;
1111 drm_modeset_unlock_all(dev
);
1113 EXPORT_SYMBOL(drm_encoder_cleanup
);
1116 * drm_universal_plane_init - Initialize a new universal plane object
1118 * @plane: plane object to init
1119 * @possible_crtcs: bitmask of possible CRTCs
1120 * @funcs: callbacks for the new plane
1121 * @formats: array of supported formats (%DRM_FORMAT_*)
1122 * @format_count: number of elements in @formats
1123 * @type: type of plane (overlay, primary, cursor)
1125 * Initializes a plane object of type @type.
1128 * Zero on success, error code on failure.
1130 int drm_universal_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1131 unsigned long possible_crtcs
,
1132 const struct drm_plane_funcs
*funcs
,
1133 const uint32_t *formats
, uint32_t format_count
,
1134 enum drm_plane_type type
)
1138 drm_modeset_lock_all(dev
);
1140 ret
= drm_mode_object_get(dev
, &plane
->base
, DRM_MODE_OBJECT_PLANE
);
1144 plane
->base
.properties
= &plane
->properties
;
1146 plane
->funcs
= funcs
;
1147 plane
->format_types
= kmalloc(sizeof(uint32_t) * format_count
,
1149 if (!plane
->format_types
) {
1150 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1151 drm_mode_object_put(dev
, &plane
->base
);
1156 memcpy(plane
->format_types
, formats
, format_count
* sizeof(uint32_t));
1157 plane
->format_count
= format_count
;
1158 plane
->possible_crtcs
= possible_crtcs
;
1161 list_add_tail(&plane
->head
, &dev
->mode_config
.plane_list
);
1162 dev
->mode_config
.num_total_plane
++;
1163 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1164 dev
->mode_config
.num_overlay_plane
++;
1166 drm_object_attach_property(&plane
->base
,
1167 dev
->mode_config
.plane_type_property
,
1171 drm_modeset_unlock_all(dev
);
1175 EXPORT_SYMBOL(drm_universal_plane_init
);
1178 * drm_plane_init - Initialize a legacy plane
1180 * @plane: plane object to init
1181 * @possible_crtcs: bitmask of possible CRTCs
1182 * @funcs: callbacks for the new plane
1183 * @formats: array of supported formats (%DRM_FORMAT_*)
1184 * @format_count: number of elements in @formats
1185 * @is_primary: plane type (primary vs overlay)
1187 * Legacy API to initialize a DRM plane.
1189 * New drivers should call drm_universal_plane_init() instead.
1192 * Zero on success, error code on failure.
1194 int drm_plane_init(struct drm_device
*dev
, struct drm_plane
*plane
,
1195 unsigned long possible_crtcs
,
1196 const struct drm_plane_funcs
*funcs
,
1197 const uint32_t *formats
, uint32_t format_count
,
1200 enum drm_plane_type type
;
1202 type
= is_primary
? DRM_PLANE_TYPE_PRIMARY
: DRM_PLANE_TYPE_OVERLAY
;
1203 return drm_universal_plane_init(dev
, plane
, possible_crtcs
, funcs
,
1204 formats
, format_count
, type
);
1206 EXPORT_SYMBOL(drm_plane_init
);
1209 * drm_plane_cleanup - Clean up the core plane usage
1210 * @plane: plane to cleanup
1212 * This function cleans up @plane and removes it from the DRM mode setting
1213 * core. Note that the function does *not* free the plane structure itself,
1214 * this is the responsibility of the caller.
1216 void drm_plane_cleanup(struct drm_plane
*plane
)
1218 struct drm_device
*dev
= plane
->dev
;
1220 drm_modeset_lock_all(dev
);
1221 kfree(plane
->format_types
);
1222 drm_mode_object_put(dev
, &plane
->base
);
1224 BUG_ON(list_empty(&plane
->head
));
1226 list_del(&plane
->head
);
1227 dev
->mode_config
.num_total_plane
--;
1228 if (plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
1229 dev
->mode_config
.num_overlay_plane
--;
1230 drm_modeset_unlock_all(dev
);
1232 EXPORT_SYMBOL(drm_plane_cleanup
);
1235 * drm_plane_force_disable - Forcibly disable a plane
1236 * @plane: plane to disable
1238 * Forces the plane to be disabled.
1240 * Used when the plane's current framebuffer is destroyed,
1241 * and when restoring fbdev mode.
1243 void drm_plane_force_disable(struct drm_plane
*plane
)
1245 struct drm_framebuffer
*old_fb
= plane
->fb
;
1251 ret
= plane
->funcs
->disable_plane(plane
);
1253 DRM_ERROR("failed to disable plane with busy fb\n");
1256 /* disconnect the plane from the fb and crtc: */
1257 __drm_framebuffer_unreference(old_fb
);
1261 EXPORT_SYMBOL(drm_plane_force_disable
);
1263 static int drm_mode_create_standard_connector_properties(struct drm_device
*dev
)
1265 struct drm_property
*edid
;
1266 struct drm_property
*dpms
;
1267 struct drm_property
*dev_path
;
1270 * Standard properties (apply to all connectors)
1272 edid
= drm_property_create(dev
, DRM_MODE_PROP_BLOB
|
1273 DRM_MODE_PROP_IMMUTABLE
,
1275 dev
->mode_config
.edid_property
= edid
;
1277 dpms
= drm_property_create_enum(dev
, 0,
1278 "DPMS", drm_dpms_enum_list
,
1279 ARRAY_SIZE(drm_dpms_enum_list
));
1280 dev
->mode_config
.dpms_property
= dpms
;
1282 dev_path
= drm_property_create(dev
,
1283 DRM_MODE_PROP_BLOB
|
1284 DRM_MODE_PROP_IMMUTABLE
,
1286 dev
->mode_config
.path_property
= dev_path
;
1291 static int drm_mode_create_standard_plane_properties(struct drm_device
*dev
)
1293 struct drm_property
*type
;
1296 * Standard properties (apply to all planes)
1298 type
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1299 "type", drm_plane_type_enum_list
,
1300 ARRAY_SIZE(drm_plane_type_enum_list
));
1301 dev
->mode_config
.plane_type_property
= type
;
1307 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1310 * Called by a driver the first time a DVI-I connector is made.
1312 int drm_mode_create_dvi_i_properties(struct drm_device
*dev
)
1314 struct drm_property
*dvi_i_selector
;
1315 struct drm_property
*dvi_i_subconnector
;
1317 if (dev
->mode_config
.dvi_i_select_subconnector_property
)
1321 drm_property_create_enum(dev
, 0,
1322 "select subconnector",
1323 drm_dvi_i_select_enum_list
,
1324 ARRAY_SIZE(drm_dvi_i_select_enum_list
));
1325 dev
->mode_config
.dvi_i_select_subconnector_property
= dvi_i_selector
;
1327 dvi_i_subconnector
= drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1329 drm_dvi_i_subconnector_enum_list
,
1330 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list
));
1331 dev
->mode_config
.dvi_i_subconnector_property
= dvi_i_subconnector
;
1335 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties
);
1338 * drm_create_tv_properties - create TV specific connector properties
1340 * @num_modes: number of different TV formats (modes) supported
1341 * @modes: array of pointers to strings containing name of each format
1343 * Called by a driver's TV initialization routine, this function creates
1344 * the TV specific connector properties for a given device. Caller is
1345 * responsible for allocating a list of format names and passing them to
1348 int drm_mode_create_tv_properties(struct drm_device
*dev
, int num_modes
,
1351 struct drm_property
*tv_selector
;
1352 struct drm_property
*tv_subconnector
;
1355 if (dev
->mode_config
.tv_select_subconnector_property
)
1359 * Basic connector properties
1361 tv_selector
= drm_property_create_enum(dev
, 0,
1362 "select subconnector",
1363 drm_tv_select_enum_list
,
1364 ARRAY_SIZE(drm_tv_select_enum_list
));
1365 dev
->mode_config
.tv_select_subconnector_property
= tv_selector
;
1368 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1370 drm_tv_subconnector_enum_list
,
1371 ARRAY_SIZE(drm_tv_subconnector_enum_list
));
1372 dev
->mode_config
.tv_subconnector_property
= tv_subconnector
;
1375 * Other, TV specific properties: margins & TV modes.
1377 dev
->mode_config
.tv_left_margin_property
=
1378 drm_property_create_range(dev
, 0, "left margin", 0, 100);
1380 dev
->mode_config
.tv_right_margin_property
=
1381 drm_property_create_range(dev
, 0, "right margin", 0, 100);
1383 dev
->mode_config
.tv_top_margin_property
=
1384 drm_property_create_range(dev
, 0, "top margin", 0, 100);
1386 dev
->mode_config
.tv_bottom_margin_property
=
1387 drm_property_create_range(dev
, 0, "bottom margin", 0, 100);
1389 dev
->mode_config
.tv_mode_property
=
1390 drm_property_create(dev
, DRM_MODE_PROP_ENUM
,
1392 for (i
= 0; i
< num_modes
; i
++)
1393 drm_property_add_enum(dev
->mode_config
.tv_mode_property
, i
,
1396 dev
->mode_config
.tv_brightness_property
=
1397 drm_property_create_range(dev
, 0, "brightness", 0, 100);
1399 dev
->mode_config
.tv_contrast_property
=
1400 drm_property_create_range(dev
, 0, "contrast", 0, 100);
1402 dev
->mode_config
.tv_flicker_reduction_property
=
1403 drm_property_create_range(dev
, 0, "flicker reduction", 0, 100);
1405 dev
->mode_config
.tv_overscan_property
=
1406 drm_property_create_range(dev
, 0, "overscan", 0, 100);
1408 dev
->mode_config
.tv_saturation_property
=
1409 drm_property_create_range(dev
, 0, "saturation", 0, 100);
1411 dev
->mode_config
.tv_hue_property
=
1412 drm_property_create_range(dev
, 0, "hue", 0, 100);
1416 EXPORT_SYMBOL(drm_mode_create_tv_properties
);
1419 * drm_mode_create_scaling_mode_property - create scaling mode property
1422 * Called by a driver the first time it's needed, must be attached to desired
1425 int drm_mode_create_scaling_mode_property(struct drm_device
*dev
)
1427 struct drm_property
*scaling_mode
;
1429 if (dev
->mode_config
.scaling_mode_property
)
1433 drm_property_create_enum(dev
, 0, "scaling mode",
1434 drm_scaling_mode_enum_list
,
1435 ARRAY_SIZE(drm_scaling_mode_enum_list
));
1437 dev
->mode_config
.scaling_mode_property
= scaling_mode
;
1441 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property
);
1444 * drm_mode_create_dirty_property - create dirty property
1447 * Called by a driver the first time it's needed, must be attached to desired
1450 int drm_mode_create_dirty_info_property(struct drm_device
*dev
)
1452 struct drm_property
*dirty_info
;
1454 if (dev
->mode_config
.dirty_info_property
)
1458 drm_property_create_enum(dev
, DRM_MODE_PROP_IMMUTABLE
,
1460 drm_dirty_info_enum_list
,
1461 ARRAY_SIZE(drm_dirty_info_enum_list
));
1462 dev
->mode_config
.dirty_info_property
= dirty_info
;
1466 EXPORT_SYMBOL(drm_mode_create_dirty_info_property
);
1468 static int drm_mode_group_init(struct drm_device
*dev
, struct drm_mode_group
*group
)
1470 uint32_t total_objects
= 0;
1472 total_objects
+= dev
->mode_config
.num_crtc
;
1473 total_objects
+= dev
->mode_config
.num_connector
;
1474 total_objects
+= dev
->mode_config
.num_encoder
;
1475 total_objects
+= dev
->mode_config
.num_bridge
;
1477 group
->id_list
= kzalloc(total_objects
* sizeof(uint32_t), GFP_KERNEL
);
1478 if (!group
->id_list
)
1481 group
->num_crtcs
= 0;
1482 group
->num_connectors
= 0;
1483 group
->num_encoders
= 0;
1484 group
->num_bridges
= 0;
1488 void drm_mode_group_destroy(struct drm_mode_group
*group
)
1490 kfree(group
->id_list
);
1491 group
->id_list
= NULL
;
1495 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1496 * the drm core's responsibility to set up mode control groups.
1498 int drm_mode_group_init_legacy_group(struct drm_device
*dev
,
1499 struct drm_mode_group
*group
)
1501 struct drm_crtc
*crtc
;
1502 struct drm_encoder
*encoder
;
1503 struct drm_connector
*connector
;
1504 struct drm_bridge
*bridge
;
1507 if ((ret
= drm_mode_group_init(dev
, group
)))
1510 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
1511 group
->id_list
[group
->num_crtcs
++] = crtc
->base
.id
;
1513 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
1514 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
++] =
1517 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
)
1518 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1519 group
->num_connectors
++] = connector
->base
.id
;
1521 list_for_each_entry(bridge
, &dev
->mode_config
.bridge_list
, head
)
1522 group
->id_list
[group
->num_crtcs
+ group
->num_encoders
+
1523 group
->num_connectors
+ group
->num_bridges
++] =
1528 EXPORT_SYMBOL(drm_mode_group_init_legacy_group
);
1530 void drm_reinit_primary_mode_group(struct drm_device
*dev
)
1532 drm_modeset_lock_all(dev
);
1533 drm_mode_group_destroy(&dev
->primary
->mode_group
);
1534 drm_mode_group_init_legacy_group(dev
, &dev
->primary
->mode_group
);
1535 drm_modeset_unlock_all(dev
);
1537 EXPORT_SYMBOL(drm_reinit_primary_mode_group
);
1540 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1541 * @out: drm_mode_modeinfo struct to return to the user
1542 * @in: drm_display_mode to use
1544 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1547 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo
*out
,
1548 const struct drm_display_mode
*in
)
1550 WARN(in
->hdisplay
> USHRT_MAX
|| in
->hsync_start
> USHRT_MAX
||
1551 in
->hsync_end
> USHRT_MAX
|| in
->htotal
> USHRT_MAX
||
1552 in
->hskew
> USHRT_MAX
|| in
->vdisplay
> USHRT_MAX
||
1553 in
->vsync_start
> USHRT_MAX
|| in
->vsync_end
> USHRT_MAX
||
1554 in
->vtotal
> USHRT_MAX
|| in
->vscan
> USHRT_MAX
,
1555 "timing values too large for mode info\n");
1557 out
->clock
= in
->clock
;
1558 out
->hdisplay
= in
->hdisplay
;
1559 out
->hsync_start
= in
->hsync_start
;
1560 out
->hsync_end
= in
->hsync_end
;
1561 out
->htotal
= in
->htotal
;
1562 out
->hskew
= in
->hskew
;
1563 out
->vdisplay
= in
->vdisplay
;
1564 out
->vsync_start
= in
->vsync_start
;
1565 out
->vsync_end
= in
->vsync_end
;
1566 out
->vtotal
= in
->vtotal
;
1567 out
->vscan
= in
->vscan
;
1568 out
->vrefresh
= in
->vrefresh
;
1569 out
->flags
= in
->flags
;
1570 out
->type
= in
->type
;
1571 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1572 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1576 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1577 * @out: drm_display_mode to return to the user
1578 * @in: drm_mode_modeinfo to use
1580 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1584 * Zero on success, errno on failure.
1586 static int drm_crtc_convert_umode(struct drm_display_mode
*out
,
1587 const struct drm_mode_modeinfo
*in
)
1589 if (in
->clock
> INT_MAX
|| in
->vrefresh
> INT_MAX
)
1592 if ((in
->flags
& DRM_MODE_FLAG_3D_MASK
) > DRM_MODE_FLAG_3D_MAX
)
1595 out
->clock
= in
->clock
;
1596 out
->hdisplay
= in
->hdisplay
;
1597 out
->hsync_start
= in
->hsync_start
;
1598 out
->hsync_end
= in
->hsync_end
;
1599 out
->htotal
= in
->htotal
;
1600 out
->hskew
= in
->hskew
;
1601 out
->vdisplay
= in
->vdisplay
;
1602 out
->vsync_start
= in
->vsync_start
;
1603 out
->vsync_end
= in
->vsync_end
;
1604 out
->vtotal
= in
->vtotal
;
1605 out
->vscan
= in
->vscan
;
1606 out
->vrefresh
= in
->vrefresh
;
1607 out
->flags
= in
->flags
;
1608 out
->type
= in
->type
;
1609 strncpy(out
->name
, in
->name
, DRM_DISPLAY_MODE_LEN
);
1610 out
->name
[DRM_DISPLAY_MODE_LEN
-1] = 0;
1616 * drm_mode_getresources - get graphics configuration
1617 * @dev: drm device for the ioctl
1618 * @data: data pointer for the ioctl
1619 * @file_priv: drm file for the ioctl call
1621 * Construct a set of configuration description structures and return
1622 * them to the user, including CRTC, connector and framebuffer configuration.
1624 * Called by the user via ioctl.
1627 * Zero on success, errno on failure.
1629 int drm_mode_getresources(struct drm_device
*dev
, void *data
,
1630 struct drm_file
*file_priv
)
1632 struct drm_mode_card_res
*card_res
= data
;
1633 struct list_head
*lh
;
1634 struct drm_framebuffer
*fb
;
1635 struct drm_connector
*connector
;
1636 struct drm_crtc
*crtc
;
1637 struct drm_encoder
*encoder
;
1639 int connector_count
= 0;
1642 int encoder_count
= 0;
1644 uint32_t __user
*fb_id
;
1645 uint32_t __user
*crtc_id
;
1646 uint32_t __user
*connector_id
;
1647 uint32_t __user
*encoder_id
;
1648 struct drm_mode_group
*mode_group
;
1650 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1654 mutex_lock(&file_priv
->fbs_lock
);
1656 * For the non-control nodes we need to limit the list of resources
1657 * by IDs in the group list for this node
1659 list_for_each(lh
, &file_priv
->fbs
)
1662 /* handle this in 4 parts */
1664 if (card_res
->count_fbs
>= fb_count
) {
1666 fb_id
= (uint32_t __user
*)(unsigned long)card_res
->fb_id_ptr
;
1667 list_for_each_entry(fb
, &file_priv
->fbs
, filp_head
) {
1668 if (put_user(fb
->base
.id
, fb_id
+ copied
)) {
1669 mutex_unlock(&file_priv
->fbs_lock
);
1675 card_res
->count_fbs
= fb_count
;
1676 mutex_unlock(&file_priv
->fbs_lock
);
1678 drm_modeset_lock_all(dev
);
1679 if (!drm_is_primary_client(file_priv
)) {
1682 list_for_each(lh
, &dev
->mode_config
.crtc_list
)
1685 list_for_each(lh
, &dev
->mode_config
.connector_list
)
1688 list_for_each(lh
, &dev
->mode_config
.encoder_list
)
1692 mode_group
= &file_priv
->master
->minor
->mode_group
;
1693 crtc_count
= mode_group
->num_crtcs
;
1694 connector_count
= mode_group
->num_connectors
;
1695 encoder_count
= mode_group
->num_encoders
;
1698 card_res
->max_height
= dev
->mode_config
.max_height
;
1699 card_res
->min_height
= dev
->mode_config
.min_height
;
1700 card_res
->max_width
= dev
->mode_config
.max_width
;
1701 card_res
->min_width
= dev
->mode_config
.min_width
;
1704 if (card_res
->count_crtcs
>= crtc_count
) {
1706 crtc_id
= (uint32_t __user
*)(unsigned long)card_res
->crtc_id_ptr
;
1708 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
,
1710 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
1711 if (put_user(crtc
->base
.id
, crtc_id
+ copied
)) {
1718 for (i
= 0; i
< mode_group
->num_crtcs
; i
++) {
1719 if (put_user(mode_group
->id_list
[i
],
1720 crtc_id
+ copied
)) {
1728 card_res
->count_crtcs
= crtc_count
;
1731 if (card_res
->count_encoders
>= encoder_count
) {
1733 encoder_id
= (uint32_t __user
*)(unsigned long)card_res
->encoder_id_ptr
;
1735 list_for_each_entry(encoder
,
1736 &dev
->mode_config
.encoder_list
,
1738 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder
->base
.id
,
1740 if (put_user(encoder
->base
.id
, encoder_id
+
1748 for (i
= mode_group
->num_crtcs
; i
< mode_group
->num_crtcs
+ mode_group
->num_encoders
; i
++) {
1749 if (put_user(mode_group
->id_list
[i
],
1750 encoder_id
+ copied
)) {
1759 card_res
->count_encoders
= encoder_count
;
1762 if (card_res
->count_connectors
>= connector_count
) {
1764 connector_id
= (uint32_t __user
*)(unsigned long)card_res
->connector_id_ptr
;
1766 list_for_each_entry(connector
,
1767 &dev
->mode_config
.connector_list
,
1769 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1772 if (put_user(connector
->base
.id
,
1773 connector_id
+ copied
)) {
1780 int start
= mode_group
->num_crtcs
+
1781 mode_group
->num_encoders
;
1782 for (i
= start
; i
< start
+ mode_group
->num_connectors
; i
++) {
1783 if (put_user(mode_group
->id_list
[i
],
1784 connector_id
+ copied
)) {
1792 card_res
->count_connectors
= connector_count
;
1794 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res
->count_crtcs
,
1795 card_res
->count_connectors
, card_res
->count_encoders
);
1798 drm_modeset_unlock_all(dev
);
1803 * drm_mode_getcrtc - get CRTC configuration
1804 * @dev: drm device for the ioctl
1805 * @data: data pointer for the ioctl
1806 * @file_priv: drm file for the ioctl call
1808 * Construct a CRTC configuration structure to return to the user.
1810 * Called by the user via ioctl.
1813 * Zero on success, errno on failure.
1815 int drm_mode_getcrtc(struct drm_device
*dev
,
1816 void *data
, struct drm_file
*file_priv
)
1818 struct drm_mode_crtc
*crtc_resp
= data
;
1819 struct drm_crtc
*crtc
;
1822 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1825 drm_modeset_lock_all(dev
);
1827 crtc
= drm_crtc_find(dev
, crtc_resp
->crtc_id
);
1833 crtc_resp
->x
= crtc
->x
;
1834 crtc_resp
->y
= crtc
->y
;
1835 crtc_resp
->gamma_size
= crtc
->gamma_size
;
1836 if (crtc
->primary
->fb
)
1837 crtc_resp
->fb_id
= crtc
->primary
->fb
->base
.id
;
1839 crtc_resp
->fb_id
= 0;
1841 if (crtc
->enabled
) {
1843 drm_crtc_convert_to_umode(&crtc_resp
->mode
, &crtc
->mode
);
1844 crtc_resp
->mode_valid
= 1;
1847 crtc_resp
->mode_valid
= 0;
1851 drm_modeset_unlock_all(dev
);
1855 static bool drm_mode_expose_to_userspace(const struct drm_display_mode
*mode
,
1856 const struct drm_file
*file_priv
)
1859 * If user-space hasn't configured the driver to expose the stereo 3D
1860 * modes, don't expose them.
1862 if (!file_priv
->stereo_allowed
&& drm_mode_is_stereo(mode
))
1869 * drm_mode_getconnector - get connector configuration
1870 * @dev: drm device for the ioctl
1871 * @data: data pointer for the ioctl
1872 * @file_priv: drm file for the ioctl call
1874 * Construct a connector configuration structure to return to the user.
1876 * Called by the user via ioctl.
1879 * Zero on success, errno on failure.
1881 int drm_mode_getconnector(struct drm_device
*dev
, void *data
,
1882 struct drm_file
*file_priv
)
1884 struct drm_mode_get_connector
*out_resp
= data
;
1885 struct drm_connector
*connector
;
1886 struct drm_display_mode
*mode
;
1888 int props_count
= 0;
1889 int encoders_count
= 0;
1893 struct drm_mode_modeinfo u_mode
;
1894 struct drm_mode_modeinfo __user
*mode_ptr
;
1895 uint32_t __user
*prop_ptr
;
1896 uint64_t __user
*prop_values
;
1897 uint32_t __user
*encoder_ptr
;
1899 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
1902 memset(&u_mode
, 0, sizeof(struct drm_mode_modeinfo
));
1904 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp
->connector_id
);
1906 mutex_lock(&dev
->mode_config
.mutex
);
1908 connector
= drm_connector_find(dev
, out_resp
->connector_id
);
1914 props_count
= connector
->properties
.count
;
1916 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1917 if (connector
->encoder_ids
[i
] != 0) {
1922 if (out_resp
->count_modes
== 0) {
1923 connector
->funcs
->fill_modes(connector
,
1924 dev
->mode_config
.max_width
,
1925 dev
->mode_config
.max_height
);
1928 /* delayed so we get modes regardless of pre-fill_modes state */
1929 list_for_each_entry(mode
, &connector
->modes
, head
)
1930 if (drm_mode_expose_to_userspace(mode
, file_priv
))
1933 out_resp
->connector_id
= connector
->base
.id
;
1934 out_resp
->connector_type
= connector
->connector_type
;
1935 out_resp
->connector_type_id
= connector
->connector_type_id
;
1936 out_resp
->mm_width
= connector
->display_info
.width_mm
;
1937 out_resp
->mm_height
= connector
->display_info
.height_mm
;
1938 out_resp
->subpixel
= connector
->display_info
.subpixel_order
;
1939 out_resp
->connection
= connector
->status
;
1940 drm_modeset_lock(&dev
->mode_config
.connection_mutex
, NULL
);
1941 if (connector
->encoder
)
1942 out_resp
->encoder_id
= connector
->encoder
->base
.id
;
1944 out_resp
->encoder_id
= 0;
1945 drm_modeset_unlock(&dev
->mode_config
.connection_mutex
);
1948 * This ioctl is called twice, once to determine how much space is
1949 * needed, and the 2nd time to fill it.
1951 if ((out_resp
->count_modes
>= mode_count
) && mode_count
) {
1953 mode_ptr
= (struct drm_mode_modeinfo __user
*)(unsigned long)out_resp
->modes_ptr
;
1954 list_for_each_entry(mode
, &connector
->modes
, head
) {
1955 if (!drm_mode_expose_to_userspace(mode
, file_priv
))
1958 drm_crtc_convert_to_umode(&u_mode
, mode
);
1959 if (copy_to_user(mode_ptr
+ copied
,
1960 &u_mode
, sizeof(u_mode
))) {
1967 out_resp
->count_modes
= mode_count
;
1969 if ((out_resp
->count_props
>= props_count
) && props_count
) {
1971 prop_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->props_ptr
);
1972 prop_values
= (uint64_t __user
*)(unsigned long)(out_resp
->prop_values_ptr
);
1973 for (i
= 0; i
< connector
->properties
.count
; i
++) {
1974 if (put_user(connector
->properties
.ids
[i
],
1975 prop_ptr
+ copied
)) {
1980 if (put_user(connector
->properties
.values
[i
],
1981 prop_values
+ copied
)) {
1988 out_resp
->count_props
= props_count
;
1990 if ((out_resp
->count_encoders
>= encoders_count
) && encoders_count
) {
1992 encoder_ptr
= (uint32_t __user
*)(unsigned long)(out_resp
->encoders_ptr
);
1993 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
1994 if (connector
->encoder_ids
[i
] != 0) {
1995 if (put_user(connector
->encoder_ids
[i
],
1996 encoder_ptr
+ copied
)) {
2004 out_resp
->count_encoders
= encoders_count
;
2007 mutex_unlock(&dev
->mode_config
.mutex
);
2013 * drm_mode_getencoder - get encoder configuration
2014 * @dev: drm device for the ioctl
2015 * @data: data pointer for the ioctl
2016 * @file_priv: drm file for the ioctl call
2018 * Construct a encoder configuration structure to return to the user.
2020 * Called by the user via ioctl.
2023 * Zero on success, errno on failure.
2025 int drm_mode_getencoder(struct drm_device
*dev
, void *data
,
2026 struct drm_file
*file_priv
)
2028 struct drm_mode_get_encoder
*enc_resp
= data
;
2029 struct drm_encoder
*encoder
;
2032 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2035 drm_modeset_lock_all(dev
);
2036 encoder
= drm_encoder_find(dev
, enc_resp
->encoder_id
);
2043 enc_resp
->crtc_id
= encoder
->crtc
->base
.id
;
2045 enc_resp
->crtc_id
= 0;
2046 enc_resp
->encoder_type
= encoder
->encoder_type
;
2047 enc_resp
->encoder_id
= encoder
->base
.id
;
2048 enc_resp
->possible_crtcs
= encoder
->possible_crtcs
;
2049 enc_resp
->possible_clones
= encoder
->possible_clones
;
2052 drm_modeset_unlock_all(dev
);
2057 * drm_mode_getplane_res - enumerate all plane resources
2060 * @file_priv: DRM file info
2062 * Construct a list of plane ids to return to the user.
2064 * Called by the user via ioctl.
2067 * Zero on success, errno on failure.
2069 int drm_mode_getplane_res(struct drm_device
*dev
, void *data
,
2070 struct drm_file
*file_priv
)
2072 struct drm_mode_get_plane_res
*plane_resp
= data
;
2073 struct drm_mode_config
*config
;
2074 struct drm_plane
*plane
;
2075 uint32_t __user
*plane_ptr
;
2076 int copied
= 0, ret
= 0;
2077 unsigned num_planes
;
2079 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2082 drm_modeset_lock_all(dev
);
2083 config
= &dev
->mode_config
;
2085 if (file_priv
->universal_planes
)
2086 num_planes
= config
->num_total_plane
;
2088 num_planes
= config
->num_overlay_plane
;
2091 * This ioctl is called twice, once to determine how much space is
2092 * needed, and the 2nd time to fill it.
2095 (plane_resp
->count_planes
>= num_planes
)) {
2096 plane_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->plane_id_ptr
;
2098 list_for_each_entry(plane
, &config
->plane_list
, head
) {
2100 * Unless userspace set the 'universal planes'
2101 * capability bit, only advertise overlays.
2103 if (plane
->type
!= DRM_PLANE_TYPE_OVERLAY
&&
2104 !file_priv
->universal_planes
)
2107 if (put_user(plane
->base
.id
, plane_ptr
+ copied
)) {
2114 plane_resp
->count_planes
= num_planes
;
2117 drm_modeset_unlock_all(dev
);
2122 * drm_mode_getplane - get plane configuration
2125 * @file_priv: DRM file info
2127 * Construct a plane configuration structure to return to the user.
2129 * Called by the user via ioctl.
2132 * Zero on success, errno on failure.
2134 int drm_mode_getplane(struct drm_device
*dev
, void *data
,
2135 struct drm_file
*file_priv
)
2137 struct drm_mode_get_plane
*plane_resp
= data
;
2138 struct drm_plane
*plane
;
2139 uint32_t __user
*format_ptr
;
2142 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2145 drm_modeset_lock_all(dev
);
2146 plane
= drm_plane_find(dev
, plane_resp
->plane_id
);
2153 plane_resp
->crtc_id
= plane
->crtc
->base
.id
;
2155 plane_resp
->crtc_id
= 0;
2158 plane_resp
->fb_id
= plane
->fb
->base
.id
;
2160 plane_resp
->fb_id
= 0;
2162 plane_resp
->plane_id
= plane
->base
.id
;
2163 plane_resp
->possible_crtcs
= plane
->possible_crtcs
;
2164 plane_resp
->gamma_size
= 0;
2167 * This ioctl is called twice, once to determine how much space is
2168 * needed, and the 2nd time to fill it.
2170 if (plane
->format_count
&&
2171 (plane_resp
->count_format_types
>= plane
->format_count
)) {
2172 format_ptr
= (uint32_t __user
*)(unsigned long)plane_resp
->format_type_ptr
;
2173 if (copy_to_user(format_ptr
,
2174 plane
->format_types
,
2175 sizeof(uint32_t) * plane
->format_count
)) {
2180 plane_resp
->count_format_types
= plane
->format_count
;
2183 drm_modeset_unlock_all(dev
);
2188 * setplane_internal - setplane handler for internal callers
2190 * Note that we assume an extra reference has already been taken on fb. If the
2191 * update fails, this reference will be dropped before return; if it succeeds,
2192 * the previous framebuffer (if any) will be unreferenced instead.
2194 * src_{x,y,w,h} are provided in 16.16 fixed point format
2196 static int setplane_internal(struct drm_plane
*plane
,
2197 struct drm_crtc
*crtc
,
2198 struct drm_framebuffer
*fb
,
2199 int32_t crtc_x
, int32_t crtc_y
,
2200 uint32_t crtc_w
, uint32_t crtc_h
,
2201 /* src_{x,y,w,h} values are 16.16 fixed point */
2202 uint32_t src_x
, uint32_t src_y
,
2203 uint32_t src_w
, uint32_t src_h
)
2205 struct drm_device
*dev
= plane
->dev
;
2206 struct drm_framebuffer
*old_fb
= NULL
;
2208 unsigned int fb_width
, fb_height
;
2211 /* No fb means shut it down */
2213 drm_modeset_lock_all(dev
);
2215 ret
= plane
->funcs
->disable_plane(plane
);
2222 drm_modeset_unlock_all(dev
);
2226 /* Check whether this plane is usable on this CRTC */
2227 if (!(plane
->possible_crtcs
& drm_crtc_mask(crtc
))) {
2228 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2233 /* Check whether this plane supports the fb pixel format. */
2234 for (i
= 0; i
< plane
->format_count
; i
++)
2235 if (fb
->pixel_format
== plane
->format_types
[i
])
2237 if (i
== plane
->format_count
) {
2238 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2239 drm_get_format_name(fb
->pixel_format
));
2244 fb_width
= fb
->width
<< 16;
2245 fb_height
= fb
->height
<< 16;
2247 /* Make sure source coordinates are inside the fb. */
2248 if (src_w
> fb_width
||
2249 src_x
> fb_width
- src_w
||
2250 src_h
> fb_height
||
2251 src_y
> fb_height
- src_h
) {
2252 DRM_DEBUG_KMS("Invalid source coordinates "
2253 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2254 src_w
>> 16, ((src_w
& 0xffff) * 15625) >> 10,
2255 src_h
>> 16, ((src_h
& 0xffff) * 15625) >> 10,
2256 src_x
>> 16, ((src_x
& 0xffff) * 15625) >> 10,
2257 src_y
>> 16, ((src_y
& 0xffff) * 15625) >> 10);
2262 drm_modeset_lock_all(dev
);
2264 ret
= plane
->funcs
->update_plane(plane
, crtc
, fb
,
2265 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2266 src_x
, src_y
, src_w
, src_h
);
2274 drm_modeset_unlock_all(dev
);
2278 drm_framebuffer_unreference(fb
);
2280 drm_framebuffer_unreference(old_fb
);
2287 * drm_mode_setplane - configure a plane's configuration
2289 * @data: ioctl data*
2290 * @file_priv: DRM file info
2292 * Set plane configuration, including placement, fb, scaling, and other factors.
2293 * Or pass a NULL fb to disable (planes may be disabled without providing a
2297 * Zero on success, errno on failure.
2299 int drm_mode_setplane(struct drm_device
*dev
, void *data
,
2300 struct drm_file
*file_priv
)
2302 struct drm_mode_set_plane
*plane_req
= data
;
2303 struct drm_mode_object
*obj
;
2304 struct drm_plane
*plane
;
2305 struct drm_crtc
*crtc
= NULL
;
2306 struct drm_framebuffer
*fb
= NULL
;
2308 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2311 /* Give drivers some help against integer overflows */
2312 if (plane_req
->crtc_w
> INT_MAX
||
2313 plane_req
->crtc_x
> INT_MAX
- (int32_t) plane_req
->crtc_w
||
2314 plane_req
->crtc_h
> INT_MAX
||
2315 plane_req
->crtc_y
> INT_MAX
- (int32_t) plane_req
->crtc_h
) {
2316 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2317 plane_req
->crtc_w
, plane_req
->crtc_h
,
2318 plane_req
->crtc_x
, plane_req
->crtc_y
);
2323 * First, find the plane, crtc, and fb objects. If not available,
2324 * we don't bother to call the driver.
2326 obj
= drm_mode_object_find(dev
, plane_req
->plane_id
,
2327 DRM_MODE_OBJECT_PLANE
);
2329 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2330 plane_req
->plane_id
);
2333 plane
= obj_to_plane(obj
);
2335 if (plane_req
->fb_id
) {
2336 fb
= drm_framebuffer_lookup(dev
, plane_req
->fb_id
);
2338 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2343 obj
= drm_mode_object_find(dev
, plane_req
->crtc_id
,
2344 DRM_MODE_OBJECT_CRTC
);
2346 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2347 plane_req
->crtc_id
);
2350 crtc
= obj_to_crtc(obj
);
2354 * setplane_internal will take care of deref'ing either the old or new
2355 * framebuffer depending on success.
2357 return setplane_internal(plane
, crtc
, fb
,
2358 plane_req
->crtc_x
, plane_req
->crtc_y
,
2359 plane_req
->crtc_w
, plane_req
->crtc_h
,
2360 plane_req
->src_x
, plane_req
->src_y
,
2361 plane_req
->src_w
, plane_req
->src_h
);
2365 * drm_mode_set_config_internal - helper to call ->set_config
2366 * @set: modeset config to set
2368 * This is a little helper to wrap internal calls to the ->set_config driver
2369 * interface. The only thing it adds is correct refcounting dance.
2372 * Zero on success, errno on failure.
2374 int drm_mode_set_config_internal(struct drm_mode_set
*set
)
2376 struct drm_crtc
*crtc
= set
->crtc
;
2377 struct drm_framebuffer
*fb
;
2378 struct drm_crtc
*tmp
;
2382 * NOTE: ->set_config can also disable other crtcs (if we steal all
2383 * connectors from it), hence we need to refcount the fbs across all
2384 * crtcs. Atomic modeset will have saner semantics ...
2386 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
)
2387 tmp
->old_fb
= tmp
->primary
->fb
;
2391 ret
= crtc
->funcs
->set_config(set
);
2393 crtc
->primary
->crtc
= crtc
;
2394 crtc
->primary
->fb
= fb
;
2397 list_for_each_entry(tmp
, &crtc
->dev
->mode_config
.crtc_list
, head
) {
2398 if (tmp
->primary
->fb
)
2399 drm_framebuffer_reference(tmp
->primary
->fb
);
2401 drm_framebuffer_unreference(tmp
->old_fb
);
2406 EXPORT_SYMBOL(drm_mode_set_config_internal
);
2409 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2411 * @crtc: CRTC that framebuffer will be displayed on
2414 * @mode: mode that framebuffer will be displayed under
2415 * @fb: framebuffer to check size of
2417 int drm_crtc_check_viewport(const struct drm_crtc
*crtc
,
2419 const struct drm_display_mode
*mode
,
2420 const struct drm_framebuffer
*fb
)
2423 int hdisplay
, vdisplay
;
2425 hdisplay
= mode
->hdisplay
;
2426 vdisplay
= mode
->vdisplay
;
2428 if (drm_mode_is_stereo(mode
)) {
2429 struct drm_display_mode adjusted
= *mode
;
2431 drm_mode_set_crtcinfo(&adjusted
, CRTC_STEREO_DOUBLE
);
2432 hdisplay
= adjusted
.crtc_hdisplay
;
2433 vdisplay
= adjusted
.crtc_vdisplay
;
2436 if (crtc
->invert_dimensions
)
2437 swap(hdisplay
, vdisplay
);
2439 if (hdisplay
> fb
->width
||
2440 vdisplay
> fb
->height
||
2441 x
> fb
->width
- hdisplay
||
2442 y
> fb
->height
- vdisplay
) {
2443 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2444 fb
->width
, fb
->height
, hdisplay
, vdisplay
, x
, y
,
2445 crtc
->invert_dimensions
? " (inverted)" : "");
2451 EXPORT_SYMBOL(drm_crtc_check_viewport
);
2454 * drm_mode_setcrtc - set CRTC configuration
2455 * @dev: drm device for the ioctl
2456 * @data: data pointer for the ioctl
2457 * @file_priv: drm file for the ioctl call
2459 * Build a new CRTC configuration based on user request.
2461 * Called by the user via ioctl.
2464 * Zero on success, errno on failure.
2466 int drm_mode_setcrtc(struct drm_device
*dev
, void *data
,
2467 struct drm_file
*file_priv
)
2469 struct drm_mode_config
*config
= &dev
->mode_config
;
2470 struct drm_mode_crtc
*crtc_req
= data
;
2471 struct drm_crtc
*crtc
;
2472 struct drm_connector
**connector_set
= NULL
, *connector
;
2473 struct drm_framebuffer
*fb
= NULL
;
2474 struct drm_display_mode
*mode
= NULL
;
2475 struct drm_mode_set set
;
2476 uint32_t __user
*set_connectors_ptr
;
2480 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2483 /* For some reason crtc x/y offsets are signed internally. */
2484 if (crtc_req
->x
> INT_MAX
|| crtc_req
->y
> INT_MAX
)
2487 drm_modeset_lock_all(dev
);
2488 crtc
= drm_crtc_find(dev
, crtc_req
->crtc_id
);
2490 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req
->crtc_id
);
2494 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc
->base
.id
);
2496 if (crtc_req
->mode_valid
) {
2497 /* If we have a mode we need a framebuffer. */
2498 /* If we pass -1, set the mode with the currently bound fb */
2499 if (crtc_req
->fb_id
== -1) {
2500 if (!crtc
->primary
->fb
) {
2501 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2505 fb
= crtc
->primary
->fb
;
2506 /* Make refcounting symmetric with the lookup path. */
2507 drm_framebuffer_reference(fb
);
2509 fb
= drm_framebuffer_lookup(dev
, crtc_req
->fb_id
);
2511 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2518 mode
= drm_mode_create(dev
);
2524 ret
= drm_crtc_convert_umode(mode
, &crtc_req
->mode
);
2526 DRM_DEBUG_KMS("Invalid mode\n");
2530 drm_mode_set_crtcinfo(mode
, CRTC_INTERLACE_HALVE_V
);
2532 ret
= drm_crtc_check_viewport(crtc
, crtc_req
->x
, crtc_req
->y
,
2539 if (crtc_req
->count_connectors
== 0 && mode
) {
2540 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2545 if (crtc_req
->count_connectors
> 0 && (!mode
|| !fb
)) {
2546 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2547 crtc_req
->count_connectors
);
2552 if (crtc_req
->count_connectors
> 0) {
2555 /* Avoid unbounded kernel memory allocation */
2556 if (crtc_req
->count_connectors
> config
->num_connector
) {
2561 connector_set
= kmalloc(crtc_req
->count_connectors
*
2562 sizeof(struct drm_connector
*),
2564 if (!connector_set
) {
2569 for (i
= 0; i
< crtc_req
->count_connectors
; i
++) {
2570 set_connectors_ptr
= (uint32_t __user
*)(unsigned long)crtc_req
->set_connectors_ptr
;
2571 if (get_user(out_id
, &set_connectors_ptr
[i
])) {
2576 connector
= drm_connector_find(dev
, out_id
);
2578 DRM_DEBUG_KMS("Connector id %d unknown\n",
2583 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2587 connector_set
[i
] = connector
;
2592 set
.x
= crtc_req
->x
;
2593 set
.y
= crtc_req
->y
;
2595 set
.connectors
= connector_set
;
2596 set
.num_connectors
= crtc_req
->count_connectors
;
2598 ret
= drm_mode_set_config_internal(&set
);
2602 drm_framebuffer_unreference(fb
);
2604 kfree(connector_set
);
2605 drm_mode_destroy(dev
, mode
);
2606 drm_modeset_unlock_all(dev
);
2611 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2612 * universal plane handler call
2613 * @crtc: crtc to update cursor for
2614 * @req: data pointer for the ioctl
2615 * @file_priv: drm file for the ioctl call
2617 * Legacy cursor ioctl's work directly with driver buffer handles. To
2618 * translate legacy ioctl calls into universal plane handler calls, we need to
2619 * wrap the native buffer handle in a drm_framebuffer.
2621 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2622 * buffer with a pitch of 4*width; the universal plane interface should be used
2623 * directly in cases where the hardware can support other buffer settings and
2624 * userspace wants to make use of these capabilities.
2627 * Zero on success, errno on failure.
2629 static int drm_mode_cursor_universal(struct drm_crtc
*crtc
,
2630 struct drm_mode_cursor2
*req
,
2631 struct drm_file
*file_priv
)
2633 struct drm_device
*dev
= crtc
->dev
;
2634 struct drm_framebuffer
*fb
= NULL
;
2635 struct drm_mode_fb_cmd2 fbreq
= {
2636 .width
= req
->width
,
2637 .height
= req
->height
,
2638 .pixel_format
= DRM_FORMAT_ARGB8888
,
2639 .pitches
= { req
->width
* 4 },
2640 .handles
= { req
->handle
},
2642 int32_t crtc_x
, crtc_y
;
2643 uint32_t crtc_w
= 0, crtc_h
= 0;
2644 uint32_t src_w
= 0, src_h
= 0;
2647 BUG_ON(!crtc
->cursor
);
2650 * Obtain fb we'll be using (either new or existing) and take an extra
2651 * reference to it if fb != null. setplane will take care of dropping
2652 * the reference if the plane update fails.
2654 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2656 fb
= add_framebuffer_internal(dev
, &fbreq
, file_priv
);
2658 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2662 drm_framebuffer_reference(fb
);
2667 mutex_lock(&dev
->mode_config
.mutex
);
2668 fb
= crtc
->cursor
->fb
;
2670 drm_framebuffer_reference(fb
);
2671 mutex_unlock(&dev
->mode_config
.mutex
);
2674 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2678 crtc_x
= crtc
->cursor_x
;
2679 crtc_y
= crtc
->cursor_y
;
2684 crtc_h
= fb
->height
;
2685 src_w
= fb
->width
<< 16;
2686 src_h
= fb
->height
<< 16;
2690 * setplane_internal will take care of deref'ing either the old or new
2691 * framebuffer depending on success.
2693 ret
= setplane_internal(crtc
->cursor
, crtc
, fb
,
2694 crtc_x
, crtc_y
, crtc_w
, crtc_h
,
2695 0, 0, src_w
, src_h
);
2697 /* Update successful; save new cursor position, if necessary */
2698 if (ret
== 0 && req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2699 crtc
->cursor_x
= req
->x
;
2700 crtc
->cursor_y
= req
->y
;
2706 static int drm_mode_cursor_common(struct drm_device
*dev
,
2707 struct drm_mode_cursor2
*req
,
2708 struct drm_file
*file_priv
)
2710 struct drm_crtc
*crtc
;
2713 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2716 if (!req
->flags
|| (~DRM_MODE_CURSOR_FLAGS
& req
->flags
))
2719 crtc
= drm_crtc_find(dev
, req
->crtc_id
);
2721 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req
->crtc_id
);
2726 * If this crtc has a universal cursor plane, call that plane's update
2727 * handler rather than using legacy cursor handlers.
2730 return drm_mode_cursor_universal(crtc
, req
, file_priv
);
2732 drm_modeset_lock(&crtc
->mutex
, NULL
);
2733 if (req
->flags
& DRM_MODE_CURSOR_BO
) {
2734 if (!crtc
->funcs
->cursor_set
&& !crtc
->funcs
->cursor_set2
) {
2738 /* Turns off the cursor if handle is 0 */
2739 if (crtc
->funcs
->cursor_set2
)
2740 ret
= crtc
->funcs
->cursor_set2(crtc
, file_priv
, req
->handle
,
2741 req
->width
, req
->height
, req
->hot_x
, req
->hot_y
);
2743 ret
= crtc
->funcs
->cursor_set(crtc
, file_priv
, req
->handle
,
2744 req
->width
, req
->height
);
2747 if (req
->flags
& DRM_MODE_CURSOR_MOVE
) {
2748 if (crtc
->funcs
->cursor_move
) {
2749 ret
= crtc
->funcs
->cursor_move(crtc
, req
->x
, req
->y
);
2756 drm_modeset_unlock(&crtc
->mutex
);
2764 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2765 * @dev: drm device for the ioctl
2766 * @data: data pointer for the ioctl
2767 * @file_priv: drm file for the ioctl call
2769 * Set the cursor configuration based on user request.
2771 * Called by the user via ioctl.
2774 * Zero on success, errno on failure.
2776 int drm_mode_cursor_ioctl(struct drm_device
*dev
,
2777 void *data
, struct drm_file
*file_priv
)
2779 struct drm_mode_cursor
*req
= data
;
2780 struct drm_mode_cursor2 new_req
;
2782 memcpy(&new_req
, req
, sizeof(struct drm_mode_cursor
));
2783 new_req
.hot_x
= new_req
.hot_y
= 0;
2785 return drm_mode_cursor_common(dev
, &new_req
, file_priv
);
2789 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2790 * @dev: drm device for the ioctl
2791 * @data: data pointer for the ioctl
2792 * @file_priv: drm file for the ioctl call
2794 * Set the cursor configuration based on user request. This implements the 2nd
2795 * version of the cursor ioctl, which allows userspace to additionally specify
2796 * the hotspot of the pointer.
2798 * Called by the user via ioctl.
2801 * Zero on success, errno on failure.
2803 int drm_mode_cursor2_ioctl(struct drm_device
*dev
,
2804 void *data
, struct drm_file
*file_priv
)
2806 struct drm_mode_cursor2
*req
= data
;
2807 return drm_mode_cursor_common(dev
, req
, file_priv
);
2811 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2812 * @bpp: bits per pixels
2813 * @depth: bit depth per pixel
2815 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2816 * Useful in fbdev emulation code, since that deals in those values.
2818 uint32_t drm_mode_legacy_fb_format(uint32_t bpp
, uint32_t depth
)
2824 fmt
= DRM_FORMAT_C8
;
2828 fmt
= DRM_FORMAT_XRGB1555
;
2830 fmt
= DRM_FORMAT_RGB565
;
2833 fmt
= DRM_FORMAT_RGB888
;
2837 fmt
= DRM_FORMAT_XRGB8888
;
2838 else if (depth
== 30)
2839 fmt
= DRM_FORMAT_XRGB2101010
;
2841 fmt
= DRM_FORMAT_ARGB8888
;
2844 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2845 fmt
= DRM_FORMAT_XRGB8888
;
2851 EXPORT_SYMBOL(drm_mode_legacy_fb_format
);
2854 * drm_mode_addfb - add an FB to the graphics configuration
2855 * @dev: drm device for the ioctl
2856 * @data: data pointer for the ioctl
2857 * @file_priv: drm file for the ioctl call
2859 * Add a new FB to the specified CRTC, given a user request. This is the
2860 * original addfb ioclt which only supported RGB formats.
2862 * Called by the user via ioctl.
2865 * Zero on success, errno on failure.
2867 int drm_mode_addfb(struct drm_device
*dev
,
2868 void *data
, struct drm_file
*file_priv
)
2870 struct drm_mode_fb_cmd
*or = data
;
2871 struct drm_mode_fb_cmd2 r
= {};
2872 struct drm_mode_config
*config
= &dev
->mode_config
;
2873 struct drm_framebuffer
*fb
;
2876 /* Use new struct with format internally */
2877 r
.fb_id
= or->fb_id
;
2878 r
.width
= or->width
;
2879 r
.height
= or->height
;
2880 r
.pitches
[0] = or->pitch
;
2881 r
.pixel_format
= drm_mode_legacy_fb_format(or->bpp
, or->depth
);
2882 r
.handles
[0] = or->handle
;
2884 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
2887 if ((config
->min_width
> r
.width
) || (r
.width
> config
->max_width
))
2890 if ((config
->min_height
> r
.height
) || (r
.height
> config
->max_height
))
2893 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, &r
);
2895 DRM_DEBUG_KMS("could not create framebuffer\n");
2899 mutex_lock(&file_priv
->fbs_lock
);
2900 or->fb_id
= fb
->base
.id
;
2901 list_add(&fb
->filp_head
, &file_priv
->fbs
);
2902 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
2903 mutex_unlock(&file_priv
->fbs_lock
);
2908 static int format_check(const struct drm_mode_fb_cmd2
*r
)
2910 uint32_t format
= r
->pixel_format
& ~DRM_FORMAT_BIG_ENDIAN
;
2914 case DRM_FORMAT_RGB332
:
2915 case DRM_FORMAT_BGR233
:
2916 case DRM_FORMAT_XRGB4444
:
2917 case DRM_FORMAT_XBGR4444
:
2918 case DRM_FORMAT_RGBX4444
:
2919 case DRM_FORMAT_BGRX4444
:
2920 case DRM_FORMAT_ARGB4444
:
2921 case DRM_FORMAT_ABGR4444
:
2922 case DRM_FORMAT_RGBA4444
:
2923 case DRM_FORMAT_BGRA4444
:
2924 case DRM_FORMAT_XRGB1555
:
2925 case DRM_FORMAT_XBGR1555
:
2926 case DRM_FORMAT_RGBX5551
:
2927 case DRM_FORMAT_BGRX5551
:
2928 case DRM_FORMAT_ARGB1555
:
2929 case DRM_FORMAT_ABGR1555
:
2930 case DRM_FORMAT_RGBA5551
:
2931 case DRM_FORMAT_BGRA5551
:
2932 case DRM_FORMAT_RGB565
:
2933 case DRM_FORMAT_BGR565
:
2934 case DRM_FORMAT_RGB888
:
2935 case DRM_FORMAT_BGR888
:
2936 case DRM_FORMAT_XRGB8888
:
2937 case DRM_FORMAT_XBGR8888
:
2938 case DRM_FORMAT_RGBX8888
:
2939 case DRM_FORMAT_BGRX8888
:
2940 case DRM_FORMAT_ARGB8888
:
2941 case DRM_FORMAT_ABGR8888
:
2942 case DRM_FORMAT_RGBA8888
:
2943 case DRM_FORMAT_BGRA8888
:
2944 case DRM_FORMAT_XRGB2101010
:
2945 case DRM_FORMAT_XBGR2101010
:
2946 case DRM_FORMAT_RGBX1010102
:
2947 case DRM_FORMAT_BGRX1010102
:
2948 case DRM_FORMAT_ARGB2101010
:
2949 case DRM_FORMAT_ABGR2101010
:
2950 case DRM_FORMAT_RGBA1010102
:
2951 case DRM_FORMAT_BGRA1010102
:
2952 case DRM_FORMAT_YUYV
:
2953 case DRM_FORMAT_YVYU
:
2954 case DRM_FORMAT_UYVY
:
2955 case DRM_FORMAT_VYUY
:
2956 case DRM_FORMAT_AYUV
:
2957 case DRM_FORMAT_NV12
:
2958 case DRM_FORMAT_NV21
:
2959 case DRM_FORMAT_NV16
:
2960 case DRM_FORMAT_NV61
:
2961 case DRM_FORMAT_NV24
:
2962 case DRM_FORMAT_NV42
:
2963 case DRM_FORMAT_YUV410
:
2964 case DRM_FORMAT_YVU410
:
2965 case DRM_FORMAT_YUV411
:
2966 case DRM_FORMAT_YVU411
:
2967 case DRM_FORMAT_YUV420
:
2968 case DRM_FORMAT_YVU420
:
2969 case DRM_FORMAT_YUV422
:
2970 case DRM_FORMAT_YVU422
:
2971 case DRM_FORMAT_YUV444
:
2972 case DRM_FORMAT_YVU444
:
2975 DRM_DEBUG_KMS("invalid pixel format %s\n",
2976 drm_get_format_name(r
->pixel_format
));
2981 static int framebuffer_check(const struct drm_mode_fb_cmd2
*r
)
2983 int ret
, hsub
, vsub
, num_planes
, i
;
2985 ret
= format_check(r
);
2987 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2988 drm_get_format_name(r
->pixel_format
));
2992 hsub
= drm_format_horz_chroma_subsampling(r
->pixel_format
);
2993 vsub
= drm_format_vert_chroma_subsampling(r
->pixel_format
);
2994 num_planes
= drm_format_num_planes(r
->pixel_format
);
2996 if (r
->width
== 0 || r
->width
% hsub
) {
2997 DRM_DEBUG_KMS("bad framebuffer width %u\n", r
->height
);
3001 if (r
->height
== 0 || r
->height
% vsub
) {
3002 DRM_DEBUG_KMS("bad framebuffer height %u\n", r
->height
);
3006 for (i
= 0; i
< num_planes
; i
++) {
3007 unsigned int width
= r
->width
/ (i
!= 0 ? hsub
: 1);
3008 unsigned int height
= r
->height
/ (i
!= 0 ? vsub
: 1);
3009 unsigned int cpp
= drm_format_plane_cpp(r
->pixel_format
, i
);
3011 if (!r
->handles
[i
]) {
3012 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i
);
3016 if ((uint64_t) width
* cpp
> UINT_MAX
)
3019 if ((uint64_t) height
* r
->pitches
[i
] + r
->offsets
[i
] > UINT_MAX
)
3022 if (r
->pitches
[i
] < width
* cpp
) {
3023 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r
->pitches
[i
], i
);
3031 static struct drm_framebuffer
*add_framebuffer_internal(struct drm_device
*dev
,
3032 struct drm_mode_fb_cmd2
*r
,
3033 struct drm_file
*file_priv
)
3035 struct drm_mode_config
*config
= &dev
->mode_config
;
3036 struct drm_framebuffer
*fb
;
3039 if (r
->flags
& ~DRM_MODE_FB_INTERLACED
) {
3040 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r
->flags
);
3041 return ERR_PTR(-EINVAL
);
3044 if ((config
->min_width
> r
->width
) || (r
->width
> config
->max_width
)) {
3045 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3046 r
->width
, config
->min_width
, config
->max_width
);
3047 return ERR_PTR(-EINVAL
);
3049 if ((config
->min_height
> r
->height
) || (r
->height
> config
->max_height
)) {
3050 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3051 r
->height
, config
->min_height
, config
->max_height
);
3052 return ERR_PTR(-EINVAL
);
3055 ret
= framebuffer_check(r
);
3057 return ERR_PTR(ret
);
3059 fb
= dev
->mode_config
.funcs
->fb_create(dev
, file_priv
, r
);
3061 DRM_DEBUG_KMS("could not create framebuffer\n");
3065 mutex_lock(&file_priv
->fbs_lock
);
3066 r
->fb_id
= fb
->base
.id
;
3067 list_add(&fb
->filp_head
, &file_priv
->fbs
);
3068 DRM_DEBUG_KMS("[FB:%d]\n", fb
->base
.id
);
3069 mutex_unlock(&file_priv
->fbs_lock
);
3075 * drm_mode_addfb2 - add an FB to the graphics configuration
3076 * @dev: drm device for the ioctl
3077 * @data: data pointer for the ioctl
3078 * @file_priv: drm file for the ioctl call
3080 * Add a new FB to the specified CRTC, given a user request with format. This is
3081 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3082 * and uses fourcc codes as pixel format specifiers.
3084 * Called by the user via ioctl.
3087 * Zero on success, errno on failure.
3089 int drm_mode_addfb2(struct drm_device
*dev
,
3090 void *data
, struct drm_file
*file_priv
)
3092 struct drm_framebuffer
*fb
;
3094 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3097 fb
= add_framebuffer_internal(dev
, data
, file_priv
);
3105 * drm_mode_rmfb - remove an FB from the configuration
3106 * @dev: drm device for the ioctl
3107 * @data: data pointer for the ioctl
3108 * @file_priv: drm file for the ioctl call
3110 * Remove the FB specified by the user.
3112 * Called by the user via ioctl.
3115 * Zero on success, errno on failure.
3117 int drm_mode_rmfb(struct drm_device
*dev
,
3118 void *data
, struct drm_file
*file_priv
)
3120 struct drm_framebuffer
*fb
= NULL
;
3121 struct drm_framebuffer
*fbl
= NULL
;
3122 uint32_t *id
= data
;
3125 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3128 mutex_lock(&file_priv
->fbs_lock
);
3129 mutex_lock(&dev
->mode_config
.fb_lock
);
3130 fb
= __drm_framebuffer_lookup(dev
, *id
);
3134 list_for_each_entry(fbl
, &file_priv
->fbs
, filp_head
)
3140 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3141 __drm_framebuffer_unregister(dev
, fb
);
3143 list_del_init(&fb
->filp_head
);
3144 mutex_unlock(&dev
->mode_config
.fb_lock
);
3145 mutex_unlock(&file_priv
->fbs_lock
);
3147 drm_framebuffer_remove(fb
);
3152 mutex_unlock(&dev
->mode_config
.fb_lock
);
3153 mutex_unlock(&file_priv
->fbs_lock
);
3159 * drm_mode_getfb - get FB info
3160 * @dev: drm device for the ioctl
3161 * @data: data pointer for the ioctl
3162 * @file_priv: drm file for the ioctl call
3164 * Lookup the FB given its ID and return info about it.
3166 * Called by the user via ioctl.
3169 * Zero on success, errno on failure.
3171 int drm_mode_getfb(struct drm_device
*dev
,
3172 void *data
, struct drm_file
*file_priv
)
3174 struct drm_mode_fb_cmd
*r
= data
;
3175 struct drm_framebuffer
*fb
;
3178 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3181 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3185 r
->height
= fb
->height
;
3186 r
->width
= fb
->width
;
3187 r
->depth
= fb
->depth
;
3188 r
->bpp
= fb
->bits_per_pixel
;
3189 r
->pitch
= fb
->pitches
[0];
3190 if (fb
->funcs
->create_handle
) {
3191 if (file_priv
->is_master
|| capable(CAP_SYS_ADMIN
) ||
3192 drm_is_control_client(file_priv
)) {
3193 ret
= fb
->funcs
->create_handle(fb
, file_priv
,
3196 /* GET_FB() is an unprivileged ioctl so we must not
3197 * return a buffer-handle to non-master processes! For
3198 * backwards-compatibility reasons, we cannot make
3199 * GET_FB() privileged, so just return an invalid handle
3200 * for non-masters. */
3208 drm_framebuffer_unreference(fb
);
3214 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3215 * @dev: drm device for the ioctl
3216 * @data: data pointer for the ioctl
3217 * @file_priv: drm file for the ioctl call
3219 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3220 * rectangle list. Generic userspace which does frontbuffer rendering must call
3221 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3222 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3224 * Modesetting drivers which always update the frontbuffer do not need to
3225 * implement the corresponding ->dirty framebuffer callback.
3227 * Called by the user via ioctl.
3230 * Zero on success, errno on failure.
3232 int drm_mode_dirtyfb_ioctl(struct drm_device
*dev
,
3233 void *data
, struct drm_file
*file_priv
)
3235 struct drm_clip_rect __user
*clips_ptr
;
3236 struct drm_clip_rect
*clips
= NULL
;
3237 struct drm_mode_fb_dirty_cmd
*r
= data
;
3238 struct drm_framebuffer
*fb
;
3243 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3246 fb
= drm_framebuffer_lookup(dev
, r
->fb_id
);
3250 num_clips
= r
->num_clips
;
3251 clips_ptr
= (struct drm_clip_rect __user
*)(unsigned long)r
->clips_ptr
;
3253 if (!num_clips
!= !clips_ptr
) {
3258 flags
= DRM_MODE_FB_DIRTY_FLAGS
& r
->flags
;
3260 /* If userspace annotates copy, clips must come in pairs */
3261 if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
&& (num_clips
% 2)) {
3266 if (num_clips
&& clips_ptr
) {
3267 if (num_clips
< 0 || num_clips
> DRM_MODE_FB_DIRTY_MAX_CLIPS
) {
3271 clips
= kzalloc(num_clips
* sizeof(*clips
), GFP_KERNEL
);
3277 ret
= copy_from_user(clips
, clips_ptr
,
3278 num_clips
* sizeof(*clips
));
3285 if (fb
->funcs
->dirty
) {
3286 ret
= fb
->funcs
->dirty(fb
, file_priv
, flags
, r
->color
,
3295 drm_framebuffer_unreference(fb
);
3302 * drm_fb_release - remove and free the FBs on this file
3303 * @priv: drm file for the ioctl
3305 * Destroy all the FBs associated with @filp.
3307 * Called by the user via ioctl.
3310 * Zero on success, errno on failure.
3312 void drm_fb_release(struct drm_file
*priv
)
3314 struct drm_device
*dev
= priv
->minor
->dev
;
3315 struct drm_framebuffer
*fb
, *tfb
;
3317 mutex_lock(&priv
->fbs_lock
);
3318 list_for_each_entry_safe(fb
, tfb
, &priv
->fbs
, filp_head
) {
3320 mutex_lock(&dev
->mode_config
.fb_lock
);
3321 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3322 __drm_framebuffer_unregister(dev
, fb
);
3323 mutex_unlock(&dev
->mode_config
.fb_lock
);
3325 list_del_init(&fb
->filp_head
);
3327 /* This will also drop the fpriv->fbs reference. */
3328 drm_framebuffer_remove(fb
);
3330 mutex_unlock(&priv
->fbs_lock
);
3334 * drm_property_create - create a new property type
3336 * @flags: flags specifying the property type
3337 * @name: name of the property
3338 * @num_values: number of pre-defined values
3340 * This creates a new generic drm property which can then be attached to a drm
3341 * object with drm_object_attach_property. The returned property object must be
3342 * freed with drm_property_destroy.
3345 * A pointer to the newly created property on success, NULL on failure.
3347 struct drm_property
*drm_property_create(struct drm_device
*dev
, int flags
,
3348 const char *name
, int num_values
)
3350 struct drm_property
*property
= NULL
;
3353 property
= kzalloc(sizeof(struct drm_property
), GFP_KERNEL
);
3357 property
->dev
= dev
;
3360 property
->values
= kzalloc(sizeof(uint64_t)*num_values
, GFP_KERNEL
);
3361 if (!property
->values
)
3365 ret
= drm_mode_object_get(dev
, &property
->base
, DRM_MODE_OBJECT_PROPERTY
);
3369 property
->flags
= flags
;
3370 property
->num_values
= num_values
;
3371 INIT_LIST_HEAD(&property
->enum_blob_list
);
3374 strncpy(property
->name
, name
, DRM_PROP_NAME_LEN
);
3375 property
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3378 list_add_tail(&property
->head
, &dev
->mode_config
.property_list
);
3380 WARN_ON(!drm_property_type_valid(property
));
3384 kfree(property
->values
);
3388 EXPORT_SYMBOL(drm_property_create
);
3391 * drm_property_create - create a new enumeration property type
3393 * @flags: flags specifying the property type
3394 * @name: name of the property
3395 * @props: enumeration lists with property values
3396 * @num_values: number of pre-defined values
3398 * This creates a new generic drm property which can then be attached to a drm
3399 * object with drm_object_attach_property. The returned property object must be
3400 * freed with drm_property_destroy.
3402 * Userspace is only allowed to set one of the predefined values for enumeration
3406 * A pointer to the newly created property on success, NULL on failure.
3408 struct drm_property
*drm_property_create_enum(struct drm_device
*dev
, int flags
,
3410 const struct drm_prop_enum_list
*props
,
3413 struct drm_property
*property
;
3416 flags
|= DRM_MODE_PROP_ENUM
;
3418 property
= drm_property_create(dev
, flags
, name
, num_values
);
3422 for (i
= 0; i
< num_values
; i
++) {
3423 ret
= drm_property_add_enum(property
, i
,
3427 drm_property_destroy(dev
, property
);
3434 EXPORT_SYMBOL(drm_property_create_enum
);
3437 * drm_property_create - create a new bitmask property type
3439 * @flags: flags specifying the property type
3440 * @name: name of the property
3441 * @props: enumeration lists with property bitflags
3442 * @num_values: number of pre-defined values
3444 * This creates a new generic drm property which can then be attached to a drm
3445 * object with drm_object_attach_property. The returned property object must be
3446 * freed with drm_property_destroy.
3448 * Compared to plain enumeration properties userspace is allowed to set any
3449 * or'ed together combination of the predefined property bitflag values
3452 * A pointer to the newly created property on success, NULL on failure.
3454 struct drm_property
*drm_property_create_bitmask(struct drm_device
*dev
,
3455 int flags
, const char *name
,
3456 const struct drm_prop_enum_list
*props
,
3459 struct drm_property
*property
;
3462 flags
|= DRM_MODE_PROP_BITMASK
;
3464 property
= drm_property_create(dev
, flags
, name
, num_values
);
3468 for (i
= 0; i
< num_values
; i
++) {
3469 ret
= drm_property_add_enum(property
, i
,
3473 drm_property_destroy(dev
, property
);
3480 EXPORT_SYMBOL(drm_property_create_bitmask
);
3482 static struct drm_property
*property_create_range(struct drm_device
*dev
,
3483 int flags
, const char *name
,
3484 uint64_t min
, uint64_t max
)
3486 struct drm_property
*property
;
3488 property
= drm_property_create(dev
, flags
, name
, 2);
3492 property
->values
[0] = min
;
3493 property
->values
[1] = max
;
3499 * drm_property_create - create a new ranged property type
3501 * @flags: flags specifying the property type
3502 * @name: name of the property
3503 * @min: minimum value of the property
3504 * @max: maximum value of the property
3506 * This creates a new generic drm property which can then be attached to a drm
3507 * object with drm_object_attach_property. The returned property object must be
3508 * freed with drm_property_destroy.
3510 * Userspace is allowed to set any interger value in the (min, max) range
3514 * A pointer to the newly created property on success, NULL on failure.
3516 struct drm_property
*drm_property_create_range(struct drm_device
*dev
, int flags
,
3518 uint64_t min
, uint64_t max
)
3520 return property_create_range(dev
, DRM_MODE_PROP_RANGE
| flags
,
3523 EXPORT_SYMBOL(drm_property_create_range
);
3525 struct drm_property
*drm_property_create_signed_range(struct drm_device
*dev
,
3526 int flags
, const char *name
,
3527 int64_t min
, int64_t max
)
3529 return property_create_range(dev
, DRM_MODE_PROP_SIGNED_RANGE
| flags
,
3530 name
, I642U64(min
), I642U64(max
));
3532 EXPORT_SYMBOL(drm_property_create_signed_range
);
3534 struct drm_property
*drm_property_create_object(struct drm_device
*dev
,
3535 int flags
, const char *name
, uint32_t type
)
3537 struct drm_property
*property
;
3539 flags
|= DRM_MODE_PROP_OBJECT
;
3541 property
= drm_property_create(dev
, flags
, name
, 1);
3545 property
->values
[0] = type
;
3549 EXPORT_SYMBOL(drm_property_create_object
);
3552 * drm_property_add_enum - add a possible value to an enumeration property
3553 * @property: enumeration property to change
3554 * @index: index of the new enumeration
3555 * @value: value of the new enumeration
3556 * @name: symbolic name of the new enumeration
3558 * This functions adds enumerations to a property.
3560 * It's use is deprecated, drivers should use one of the more specific helpers
3561 * to directly create the property with all enumerations already attached.
3564 * Zero on success, error code on failure.
3566 int drm_property_add_enum(struct drm_property
*property
, int index
,
3567 uint64_t value
, const char *name
)
3569 struct drm_property_enum
*prop_enum
;
3571 if (!(drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3572 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)))
3576 * Bitmask enum properties have the additional constraint of values
3579 if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
) &&
3583 if (!list_empty(&property
->enum_blob_list
)) {
3584 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3585 if (prop_enum
->value
== value
) {
3586 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3587 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3593 prop_enum
= kzalloc(sizeof(struct drm_property_enum
), GFP_KERNEL
);
3597 strncpy(prop_enum
->name
, name
, DRM_PROP_NAME_LEN
);
3598 prop_enum
->name
[DRM_PROP_NAME_LEN
-1] = '\0';
3599 prop_enum
->value
= value
;
3601 property
->values
[index
] = value
;
3602 list_add_tail(&prop_enum
->head
, &property
->enum_blob_list
);
3605 EXPORT_SYMBOL(drm_property_add_enum
);
3608 * drm_property_destroy - destroy a drm property
3610 * @property: property to destry
3612 * This function frees a property including any attached resources like
3613 * enumeration values.
3615 void drm_property_destroy(struct drm_device
*dev
, struct drm_property
*property
)
3617 struct drm_property_enum
*prop_enum
, *pt
;
3619 list_for_each_entry_safe(prop_enum
, pt
, &property
->enum_blob_list
, head
) {
3620 list_del(&prop_enum
->head
);
3624 if (property
->num_values
)
3625 kfree(property
->values
);
3626 drm_mode_object_put(dev
, &property
->base
);
3627 list_del(&property
->head
);
3630 EXPORT_SYMBOL(drm_property_destroy
);
3633 * drm_object_attach_property - attach a property to a modeset object
3634 * @obj: drm modeset object
3635 * @property: property to attach
3636 * @init_val: initial value of the property
3638 * This attaches the given property to the modeset object with the given initial
3639 * value. Currently this function cannot fail since the properties are stored in
3640 * a statically sized array.
3642 void drm_object_attach_property(struct drm_mode_object
*obj
,
3643 struct drm_property
*property
,
3646 int count
= obj
->properties
->count
;
3648 if (count
== DRM_OBJECT_MAX_PROPERTY
) {
3649 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3650 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3651 "you see this message on the same object type.\n",
3656 obj
->properties
->ids
[count
] = property
->base
.id
;
3657 obj
->properties
->values
[count
] = init_val
;
3658 obj
->properties
->count
++;
3660 EXPORT_SYMBOL(drm_object_attach_property
);
3663 * drm_object_property_set_value - set the value of a property
3664 * @obj: drm mode object to set property value for
3665 * @property: property to set
3666 * @val: value the property should be set to
3668 * This functions sets a given property on a given object. This function only
3669 * changes the software state of the property, it does not call into the
3670 * driver's ->set_property callback.
3673 * Zero on success, error code on failure.
3675 int drm_object_property_set_value(struct drm_mode_object
*obj
,
3676 struct drm_property
*property
, uint64_t val
)
3680 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3681 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3682 obj
->properties
->values
[i
] = val
;
3689 EXPORT_SYMBOL(drm_object_property_set_value
);
3692 * drm_object_property_get_value - retrieve the value of a property
3693 * @obj: drm mode object to get property value from
3694 * @property: property to retrieve
3695 * @val: storage for the property value
3697 * This function retrieves the softare state of the given property for the given
3698 * property. Since there is no driver callback to retrieve the current property
3699 * value this might be out of sync with the hardware, depending upon the driver
3703 * Zero on success, error code on failure.
3705 int drm_object_property_get_value(struct drm_mode_object
*obj
,
3706 struct drm_property
*property
, uint64_t *val
)
3710 for (i
= 0; i
< obj
->properties
->count
; i
++) {
3711 if (obj
->properties
->ids
[i
] == property
->base
.id
) {
3712 *val
= obj
->properties
->values
[i
];
3719 EXPORT_SYMBOL(drm_object_property_get_value
);
3722 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3725 * @file_priv: DRM file info
3727 * This function retrieves the current value for an connectors's property.
3729 * Called by the user via ioctl.
3732 * Zero on success, errno on failure.
3734 int drm_mode_getproperty_ioctl(struct drm_device
*dev
,
3735 void *data
, struct drm_file
*file_priv
)
3737 struct drm_mode_get_property
*out_resp
= data
;
3738 struct drm_property
*property
;
3741 int value_count
= 0;
3744 struct drm_property_enum
*prop_enum
;
3745 struct drm_mode_property_enum __user
*enum_ptr
;
3746 struct drm_property_blob
*prop_blob
;
3747 uint32_t __user
*blob_id_ptr
;
3748 uint64_t __user
*values_ptr
;
3749 uint32_t __user
*blob_length_ptr
;
3751 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3754 drm_modeset_lock_all(dev
);
3755 property
= drm_property_find(dev
, out_resp
->prop_id
);
3761 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3762 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3763 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
)
3765 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3766 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
)
3770 value_count
= property
->num_values
;
3772 strncpy(out_resp
->name
, property
->name
, DRM_PROP_NAME_LEN
);
3773 out_resp
->name
[DRM_PROP_NAME_LEN
-1] = 0;
3774 out_resp
->flags
= property
->flags
;
3776 if ((out_resp
->count_values
>= value_count
) && value_count
) {
3777 values_ptr
= (uint64_t __user
*)(unsigned long)out_resp
->values_ptr
;
3778 for (i
= 0; i
< value_count
; i
++) {
3779 if (copy_to_user(values_ptr
+ i
, &property
->values
[i
], sizeof(uint64_t))) {
3785 out_resp
->count_values
= value_count
;
3787 if (drm_property_type_is(property
, DRM_MODE_PROP_ENUM
) ||
3788 drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
3789 if ((out_resp
->count_enum_blobs
>= enum_count
) && enum_count
) {
3791 enum_ptr
= (struct drm_mode_property_enum __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3792 list_for_each_entry(prop_enum
, &property
->enum_blob_list
, head
) {
3794 if (copy_to_user(&enum_ptr
[copied
].value
, &prop_enum
->value
, sizeof(uint64_t))) {
3799 if (copy_to_user(&enum_ptr
[copied
].name
,
3800 &prop_enum
->name
, DRM_PROP_NAME_LEN
)) {
3807 out_resp
->count_enum_blobs
= enum_count
;
3810 if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
3811 if ((out_resp
->count_enum_blobs
>= blob_count
) && blob_count
) {
3813 blob_id_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->enum_blob_ptr
;
3814 blob_length_ptr
= (uint32_t __user
*)(unsigned long)out_resp
->values_ptr
;
3816 list_for_each_entry(prop_blob
, &property
->enum_blob_list
, head
) {
3817 if (put_user(prop_blob
->base
.id
, blob_id_ptr
+ copied
)) {
3822 if (put_user(prop_blob
->length
, blob_length_ptr
+ copied
)) {
3830 out_resp
->count_enum_blobs
= blob_count
;
3833 drm_modeset_unlock_all(dev
);
3837 static struct drm_property_blob
*drm_property_create_blob(struct drm_device
*dev
, int length
,
3840 struct drm_property_blob
*blob
;
3843 if (!length
|| !data
)
3846 blob
= kzalloc(sizeof(struct drm_property_blob
)+length
, GFP_KERNEL
);
3850 ret
= drm_mode_object_get(dev
, &blob
->base
, DRM_MODE_OBJECT_BLOB
);
3856 blob
->length
= length
;
3858 memcpy(blob
->data
, data
, length
);
3860 list_add_tail(&blob
->head
, &dev
->mode_config
.property_blob_list
);
3864 static void drm_property_destroy_blob(struct drm_device
*dev
,
3865 struct drm_property_blob
*blob
)
3867 drm_mode_object_put(dev
, &blob
->base
);
3868 list_del(&blob
->head
);
3873 * drm_mode_getblob_ioctl - get the contents of a blob property value
3876 * @file_priv: DRM file info
3878 * This function retrieves the contents of a blob property. The value stored in
3879 * an object's blob property is just a normal modeset object id.
3881 * Called by the user via ioctl.
3884 * Zero on success, errno on failure.
3886 int drm_mode_getblob_ioctl(struct drm_device
*dev
,
3887 void *data
, struct drm_file
*file_priv
)
3889 struct drm_mode_get_blob
*out_resp
= data
;
3890 struct drm_property_blob
*blob
;
3892 void __user
*blob_ptr
;
3894 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
3897 drm_modeset_lock_all(dev
);
3898 blob
= drm_property_blob_find(dev
, out_resp
->blob_id
);
3904 if (out_resp
->length
== blob
->length
) {
3905 blob_ptr
= (void __user
*)(unsigned long)out_resp
->data
;
3906 if (copy_to_user(blob_ptr
, blob
->data
, blob
->length
)){
3911 out_resp
->length
= blob
->length
;
3914 drm_modeset_unlock_all(dev
);
3918 int drm_mode_connector_set_path_property(struct drm_connector
*connector
,
3921 struct drm_device
*dev
= connector
->dev
;
3923 size
= strlen(path
) + 1;
3925 connector
->path_blob_ptr
= drm_property_create_blob(connector
->dev
,
3927 if (!connector
->path_blob_ptr
)
3930 ret
= drm_object_property_set_value(&connector
->base
,
3931 dev
->mode_config
.path_property
,
3932 connector
->path_blob_ptr
->base
.id
);
3935 EXPORT_SYMBOL(drm_mode_connector_set_path_property
);
3938 * drm_mode_connector_update_edid_property - update the edid property of a connector
3939 * @connector: drm connector
3940 * @edid: new value of the edid property
3942 * This function creates a new blob modeset object and assigns its id to the
3943 * connector's edid property.
3946 * Zero on success, errno on failure.
3948 int drm_mode_connector_update_edid_property(struct drm_connector
*connector
,
3951 struct drm_device
*dev
= connector
->dev
;
3954 /* ignore requests to set edid when overridden */
3955 if (connector
->override_edid
)
3958 if (connector
->edid_blob_ptr
)
3959 drm_property_destroy_blob(dev
, connector
->edid_blob_ptr
);
3961 /* Delete edid, when there is none. */
3963 connector
->edid_blob_ptr
= NULL
;
3964 ret
= drm_object_property_set_value(&connector
->base
, dev
->mode_config
.edid_property
, 0);
3968 size
= EDID_LENGTH
* (1 + edid
->extensions
);
3969 connector
->edid_blob_ptr
= drm_property_create_blob(connector
->dev
,
3971 if (!connector
->edid_blob_ptr
)
3974 ret
= drm_object_property_set_value(&connector
->base
,
3975 dev
->mode_config
.edid_property
,
3976 connector
->edid_blob_ptr
->base
.id
);
3980 EXPORT_SYMBOL(drm_mode_connector_update_edid_property
);
3982 static bool drm_property_change_is_valid(struct drm_property
*property
,
3985 if (property
->flags
& DRM_MODE_PROP_IMMUTABLE
)
3988 if (drm_property_type_is(property
, DRM_MODE_PROP_RANGE
)) {
3989 if (value
< property
->values
[0] || value
> property
->values
[1])
3992 } else if (drm_property_type_is(property
, DRM_MODE_PROP_SIGNED_RANGE
)) {
3993 int64_t svalue
= U642I64(value
);
3994 if (svalue
< U642I64(property
->values
[0]) ||
3995 svalue
> U642I64(property
->values
[1]))
3998 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BITMASK
)) {
4000 uint64_t valid_mask
= 0;
4001 for (i
= 0; i
< property
->num_values
; i
++)
4002 valid_mask
|= (1ULL << property
->values
[i
]);
4003 return !(value
& ~valid_mask
);
4004 } else if (drm_property_type_is(property
, DRM_MODE_PROP_BLOB
)) {
4005 /* Only the driver knows */
4007 } else if (drm_property_type_is(property
, DRM_MODE_PROP_OBJECT
)) {
4008 struct drm_mode_object
*obj
;
4009 /* a zero value for an object property translates to null: */
4013 * NOTE: use _object_find() directly to bypass restriction on
4014 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4015 * object this could race against object finalization, so it
4016 * simply tells us that the object *was* valid. Which is good
4019 obj
= _object_find(property
->dev
, value
, property
->values
[0]);
4023 for (i
= 0; i
< property
->num_values
; i
++)
4024 if (property
->values
[i
] == value
)
4031 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4034 * @file_priv: DRM file info
4036 * This function sets the current value for a connectors's property. It also
4037 * calls into a driver's ->set_property callback to update the hardware state
4039 * Called by the user via ioctl.
4042 * Zero on success, errno on failure.
4044 int drm_mode_connector_property_set_ioctl(struct drm_device
*dev
,
4045 void *data
, struct drm_file
*file_priv
)
4047 struct drm_mode_connector_set_property
*conn_set_prop
= data
;
4048 struct drm_mode_obj_set_property obj_set_prop
= {
4049 .value
= conn_set_prop
->value
,
4050 .prop_id
= conn_set_prop
->prop_id
,
4051 .obj_id
= conn_set_prop
->connector_id
,
4052 .obj_type
= DRM_MODE_OBJECT_CONNECTOR
4055 /* It does all the locking and checking we need */
4056 return drm_mode_obj_set_property_ioctl(dev
, &obj_set_prop
, file_priv
);
4059 static int drm_mode_connector_set_obj_prop(struct drm_mode_object
*obj
,
4060 struct drm_property
*property
,
4064 struct drm_connector
*connector
= obj_to_connector(obj
);
4066 /* Do DPMS ourselves */
4067 if (property
== connector
->dev
->mode_config
.dpms_property
) {
4068 if (connector
->funcs
->dpms
)
4069 (*connector
->funcs
->dpms
)(connector
, (int)value
);
4071 } else if (connector
->funcs
->set_property
)
4072 ret
= connector
->funcs
->set_property(connector
, property
, value
);
4074 /* store the property value if successful */
4076 drm_object_property_set_value(&connector
->base
, property
, value
);
4080 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object
*obj
,
4081 struct drm_property
*property
,
4085 struct drm_crtc
*crtc
= obj_to_crtc(obj
);
4087 if (crtc
->funcs
->set_property
)
4088 ret
= crtc
->funcs
->set_property(crtc
, property
, value
);
4090 drm_object_property_set_value(obj
, property
, value
);
4095 static int drm_mode_plane_set_obj_prop(struct drm_mode_object
*obj
,
4096 struct drm_property
*property
,
4100 struct drm_plane
*plane
= obj_to_plane(obj
);
4102 if (plane
->funcs
->set_property
)
4103 ret
= plane
->funcs
->set_property(plane
, property
, value
);
4105 drm_object_property_set_value(obj
, property
, value
);
4111 * drm_mode_getproperty_ioctl - get the current value of a object's property
4114 * @file_priv: DRM file info
4116 * This function retrieves the current value for an object's property. Compared
4117 * to the connector specific ioctl this one is extended to also work on crtc and
4120 * Called by the user via ioctl.
4123 * Zero on success, errno on failure.
4125 int drm_mode_obj_get_properties_ioctl(struct drm_device
*dev
, void *data
,
4126 struct drm_file
*file_priv
)
4128 struct drm_mode_obj_get_properties
*arg
= data
;
4129 struct drm_mode_object
*obj
;
4133 int props_count
= 0;
4134 uint32_t __user
*props_ptr
;
4135 uint64_t __user
*prop_values_ptr
;
4137 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4140 drm_modeset_lock_all(dev
);
4142 obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4147 if (!obj
->properties
) {
4152 props_count
= obj
->properties
->count
;
4154 /* This ioctl is called twice, once to determine how much space is
4155 * needed, and the 2nd time to fill it. */
4156 if ((arg
->count_props
>= props_count
) && props_count
) {
4158 props_ptr
= (uint32_t __user
*)(unsigned long)(arg
->props_ptr
);
4159 prop_values_ptr
= (uint64_t __user
*)(unsigned long)
4160 (arg
->prop_values_ptr
);
4161 for (i
= 0; i
< props_count
; i
++) {
4162 if (put_user(obj
->properties
->ids
[i
],
4163 props_ptr
+ copied
)) {
4167 if (put_user(obj
->properties
->values
[i
],
4168 prop_values_ptr
+ copied
)) {
4175 arg
->count_props
= props_count
;
4177 drm_modeset_unlock_all(dev
);
4182 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4185 * @file_priv: DRM file info
4187 * This function sets the current value for an object's property. It also calls
4188 * into a driver's ->set_property callback to update the hardware state.
4189 * Compared to the connector specific ioctl this one is extended to also work on
4190 * crtc and plane objects.
4192 * Called by the user via ioctl.
4195 * Zero on success, errno on failure.
4197 int drm_mode_obj_set_property_ioctl(struct drm_device
*dev
, void *data
,
4198 struct drm_file
*file_priv
)
4200 struct drm_mode_obj_set_property
*arg
= data
;
4201 struct drm_mode_object
*arg_obj
;
4202 struct drm_mode_object
*prop_obj
;
4203 struct drm_property
*property
;
4207 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4210 drm_modeset_lock_all(dev
);
4212 arg_obj
= drm_mode_object_find(dev
, arg
->obj_id
, arg
->obj_type
);
4217 if (!arg_obj
->properties
)
4220 for (i
= 0; i
< arg_obj
->properties
->count
; i
++)
4221 if (arg_obj
->properties
->ids
[i
] == arg
->prop_id
)
4224 if (i
== arg_obj
->properties
->count
)
4227 prop_obj
= drm_mode_object_find(dev
, arg
->prop_id
,
4228 DRM_MODE_OBJECT_PROPERTY
);
4233 property
= obj_to_property(prop_obj
);
4235 if (!drm_property_change_is_valid(property
, arg
->value
))
4238 switch (arg_obj
->type
) {
4239 case DRM_MODE_OBJECT_CONNECTOR
:
4240 ret
= drm_mode_connector_set_obj_prop(arg_obj
, property
,
4243 case DRM_MODE_OBJECT_CRTC
:
4244 ret
= drm_mode_crtc_set_obj_prop(arg_obj
, property
, arg
->value
);
4246 case DRM_MODE_OBJECT_PLANE
:
4247 ret
= drm_mode_plane_set_obj_prop(arg_obj
, property
, arg
->value
);
4252 drm_modeset_unlock_all(dev
);
4257 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4258 * @connector: connector to attach
4259 * @encoder: encoder to attach @connector to
4261 * This function links up a connector to an encoder. Note that the routing
4262 * restrictions between encoders and crtcs are exposed to userspace through the
4263 * possible_clones and possible_crtcs bitmasks.
4266 * Zero on success, errno on failure.
4268 int drm_mode_connector_attach_encoder(struct drm_connector
*connector
,
4269 struct drm_encoder
*encoder
)
4273 for (i
= 0; i
< DRM_CONNECTOR_MAX_ENCODER
; i
++) {
4274 if (connector
->encoder_ids
[i
] == 0) {
4275 connector
->encoder_ids
[i
] = encoder
->base
.id
;
4281 EXPORT_SYMBOL(drm_mode_connector_attach_encoder
);
4284 * drm_mode_crtc_set_gamma_size - set the gamma table size
4285 * @crtc: CRTC to set the gamma table size for
4286 * @gamma_size: size of the gamma table
4288 * Drivers which support gamma tables should set this to the supported gamma
4289 * table size when initializing the CRTC. Currently the drm core only supports a
4290 * fixed gamma table size.
4293 * Zero on success, errno on failure.
4295 int drm_mode_crtc_set_gamma_size(struct drm_crtc
*crtc
,
4298 crtc
->gamma_size
= gamma_size
;
4300 crtc
->gamma_store
= kzalloc(gamma_size
* sizeof(uint16_t) * 3, GFP_KERNEL
);
4301 if (!crtc
->gamma_store
) {
4302 crtc
->gamma_size
= 0;
4308 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size
);
4311 * drm_mode_gamma_set_ioctl - set the gamma table
4314 * @file_priv: DRM file info
4316 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4317 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4319 * Called by the user via ioctl.
4322 * Zero on success, errno on failure.
4324 int drm_mode_gamma_set_ioctl(struct drm_device
*dev
,
4325 void *data
, struct drm_file
*file_priv
)
4327 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4328 struct drm_crtc
*crtc
;
4329 void *r_base
, *g_base
, *b_base
;
4333 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4336 drm_modeset_lock_all(dev
);
4337 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4343 if (crtc
->funcs
->gamma_set
== NULL
) {
4348 /* memcpy into gamma store */
4349 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4354 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4355 r_base
= crtc
->gamma_store
;
4356 if (copy_from_user(r_base
, (void __user
*)(unsigned long)crtc_lut
->red
, size
)) {
4361 g_base
= r_base
+ size
;
4362 if (copy_from_user(g_base
, (void __user
*)(unsigned long)crtc_lut
->green
, size
)) {
4367 b_base
= g_base
+ size
;
4368 if (copy_from_user(b_base
, (void __user
*)(unsigned long)crtc_lut
->blue
, size
)) {
4373 crtc
->funcs
->gamma_set(crtc
, r_base
, g_base
, b_base
, 0, crtc
->gamma_size
);
4376 drm_modeset_unlock_all(dev
);
4382 * drm_mode_gamma_get_ioctl - get the gamma table
4385 * @file_priv: DRM file info
4387 * Copy the current gamma table into the storage provided. This also provides
4388 * the gamma table size the driver expects, which can be used to size the
4389 * allocated storage.
4391 * Called by the user via ioctl.
4394 * Zero on success, errno on failure.
4396 int drm_mode_gamma_get_ioctl(struct drm_device
*dev
,
4397 void *data
, struct drm_file
*file_priv
)
4399 struct drm_mode_crtc_lut
*crtc_lut
= data
;
4400 struct drm_crtc
*crtc
;
4401 void *r_base
, *g_base
, *b_base
;
4405 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
4408 drm_modeset_lock_all(dev
);
4409 crtc
= drm_crtc_find(dev
, crtc_lut
->crtc_id
);
4415 /* memcpy into gamma store */
4416 if (crtc_lut
->gamma_size
!= crtc
->gamma_size
) {
4421 size
= crtc_lut
->gamma_size
* (sizeof(uint16_t));
4422 r_base
= crtc
->gamma_store
;
4423 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->red
, r_base
, size
)) {
4428 g_base
= r_base
+ size
;
4429 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->green
, g_base
, size
)) {
4434 b_base
= g_base
+ size
;
4435 if (copy_to_user((void __user
*)(unsigned long)crtc_lut
->blue
, b_base
, size
)) {
4440 drm_modeset_unlock_all(dev
);
4445 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4448 * @file_priv: DRM file info
4450 * This schedules an asynchronous update on a given CRTC, called page flip.
4451 * Optionally a drm event is generated to signal the completion of the event.
4452 * Generic drivers cannot assume that a pageflip with changed framebuffer
4453 * properties (including driver specific metadata like tiling layout) will work,
4454 * but some drivers support e.g. pixel format changes through the pageflip
4457 * Called by the user via ioctl.
4460 * Zero on success, errno on failure.
4462 int drm_mode_page_flip_ioctl(struct drm_device
*dev
,
4463 void *data
, struct drm_file
*file_priv
)
4465 struct drm_mode_crtc_page_flip
*page_flip
= data
;
4466 struct drm_crtc
*crtc
;
4467 struct drm_framebuffer
*fb
= NULL
, *old_fb
= NULL
;
4468 struct drm_pending_vblank_event
*e
= NULL
;
4469 unsigned long flags
;
4472 if (page_flip
->flags
& ~DRM_MODE_PAGE_FLIP_FLAGS
||
4473 page_flip
->reserved
!= 0)
4476 if ((page_flip
->flags
& DRM_MODE_PAGE_FLIP_ASYNC
) && !dev
->mode_config
.async_page_flip
)
4479 crtc
= drm_crtc_find(dev
, page_flip
->crtc_id
);
4483 drm_modeset_lock(&crtc
->mutex
, NULL
);
4484 if (crtc
->primary
->fb
== NULL
) {
4485 /* The framebuffer is currently unbound, presumably
4486 * due to a hotplug event, that userspace has not
4493 if (crtc
->funcs
->page_flip
== NULL
)
4496 fb
= drm_framebuffer_lookup(dev
, page_flip
->fb_id
);
4502 ret
= drm_crtc_check_viewport(crtc
, crtc
->x
, crtc
->y
, &crtc
->mode
, fb
);
4506 if (crtc
->primary
->fb
->pixel_format
!= fb
->pixel_format
) {
4507 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4512 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4514 spin_lock_irqsave(&dev
->event_lock
, flags
);
4515 if (file_priv
->event_space
< sizeof e
->event
) {
4516 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4519 file_priv
->event_space
-= sizeof e
->event
;
4520 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4522 e
= kzalloc(sizeof *e
, GFP_KERNEL
);
4524 spin_lock_irqsave(&dev
->event_lock
, flags
);
4525 file_priv
->event_space
+= sizeof e
->event
;
4526 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4530 e
->event
.base
.type
= DRM_EVENT_FLIP_COMPLETE
;
4531 e
->event
.base
.length
= sizeof e
->event
;
4532 e
->event
.user_data
= page_flip
->user_data
;
4533 e
->base
.event
= &e
->event
.base
;
4534 e
->base
.file_priv
= file_priv
;
4536 (void (*) (struct drm_pending_event
*)) kfree
;
4539 old_fb
= crtc
->primary
->fb
;
4540 ret
= crtc
->funcs
->page_flip(crtc
, fb
, e
, page_flip
->flags
);
4542 if (page_flip
->flags
& DRM_MODE_PAGE_FLIP_EVENT
) {
4543 spin_lock_irqsave(&dev
->event_lock
, flags
);
4544 file_priv
->event_space
+= sizeof e
->event
;
4545 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
4548 /* Keep the old fb, don't unref it. */
4552 * Warn if the driver hasn't properly updated the crtc->fb
4553 * field to reflect that the new framebuffer is now used.
4554 * Failing to do so will screw with the reference counting
4557 WARN_ON(crtc
->primary
->fb
!= fb
);
4558 /* Unref only the old framebuffer. */
4564 drm_framebuffer_unreference(fb
);
4566 drm_framebuffer_unreference(old_fb
);
4567 drm_modeset_unlock(&crtc
->mutex
);
4573 * drm_mode_config_reset - call ->reset callbacks
4576 * This functions calls all the crtc's, encoder's and connector's ->reset
4577 * callback. Drivers can use this in e.g. their driver load or resume code to
4578 * reset hardware and software state.
4580 void drm_mode_config_reset(struct drm_device
*dev
)
4582 struct drm_crtc
*crtc
;
4583 struct drm_encoder
*encoder
;
4584 struct drm_connector
*connector
;
4586 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
)
4587 if (crtc
->funcs
->reset
)
4588 crtc
->funcs
->reset(crtc
);
4590 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
4591 if (encoder
->funcs
->reset
)
4592 encoder
->funcs
->reset(encoder
);
4594 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
4595 connector
->status
= connector_status_unknown
;
4597 if (connector
->funcs
->reset
)
4598 connector
->funcs
->reset(connector
);
4601 EXPORT_SYMBOL(drm_mode_config_reset
);
4604 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4607 * @file_priv: DRM file info
4609 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4610 * TTM or something else entirely) and returns the resulting buffer handle. This
4611 * handle can then be wrapped up into a framebuffer modeset object.
4613 * Note that userspace is not allowed to use such objects for render
4614 * acceleration - drivers must create their own private ioctls for such a use
4617 * Called by the user via ioctl.
4620 * Zero on success, errno on failure.
4622 int drm_mode_create_dumb_ioctl(struct drm_device
*dev
,
4623 void *data
, struct drm_file
*file_priv
)
4625 struct drm_mode_create_dumb
*args
= data
;
4626 u32 cpp
, stride
, size
;
4628 if (!dev
->driver
->dumb_create
)
4630 if (!args
->width
|| !args
->height
|| !args
->bpp
)
4633 /* overflow checks for 32bit size calculations */
4634 cpp
= DIV_ROUND_UP(args
->bpp
, 8);
4635 if (cpp
> 0xffffffffU
/ args
->width
)
4637 stride
= cpp
* args
->width
;
4638 if (args
->height
> 0xffffffffU
/ stride
)
4641 /* test for wrap-around */
4642 size
= args
->height
* stride
;
4643 if (PAGE_ALIGN(size
) == 0)
4646 return dev
->driver
->dumb_create(file_priv
, dev
, args
);
4650 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4653 * @file_priv: DRM file info
4655 * Allocate an offset in the drm device node's address space to be able to
4656 * memory map a dumb buffer.
4658 * Called by the user via ioctl.
4661 * Zero on success, errno on failure.
4663 int drm_mode_mmap_dumb_ioctl(struct drm_device
*dev
,
4664 void *data
, struct drm_file
*file_priv
)
4666 struct drm_mode_map_dumb
*args
= data
;
4668 /* call driver ioctl to get mmap offset */
4669 if (!dev
->driver
->dumb_map_offset
)
4672 return dev
->driver
->dumb_map_offset(file_priv
, dev
, args
->handle
, &args
->offset
);
4676 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4679 * @file_priv: DRM file info
4681 * This destroys the userspace handle for the given dumb backing storage buffer.
4682 * Since buffer objects must be reference counted in the kernel a buffer object
4683 * won't be immediately freed if a framebuffer modeset object still uses it.
4685 * Called by the user via ioctl.
4688 * Zero on success, errno on failure.
4690 int drm_mode_destroy_dumb_ioctl(struct drm_device
*dev
,
4691 void *data
, struct drm_file
*file_priv
)
4693 struct drm_mode_destroy_dumb
*args
= data
;
4695 if (!dev
->driver
->dumb_destroy
)
4698 return dev
->driver
->dumb_destroy(file_priv
, dev
, args
->handle
);
4702 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4703 * @format: pixel format (DRM_FORMAT_*)
4704 * @depth: storage for the depth value
4705 * @bpp: storage for the bpp value
4707 * This only supports RGB formats here for compat with code that doesn't use
4708 * pixel formats directly yet.
4710 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
4715 case DRM_FORMAT_RGB332
:
4716 case DRM_FORMAT_BGR233
:
4720 case DRM_FORMAT_XRGB1555
:
4721 case DRM_FORMAT_XBGR1555
:
4722 case DRM_FORMAT_RGBX5551
:
4723 case DRM_FORMAT_BGRX5551
:
4724 case DRM_FORMAT_ARGB1555
:
4725 case DRM_FORMAT_ABGR1555
:
4726 case DRM_FORMAT_RGBA5551
:
4727 case DRM_FORMAT_BGRA5551
:
4731 case DRM_FORMAT_RGB565
:
4732 case DRM_FORMAT_BGR565
:
4736 case DRM_FORMAT_RGB888
:
4737 case DRM_FORMAT_BGR888
:
4741 case DRM_FORMAT_XRGB8888
:
4742 case DRM_FORMAT_XBGR8888
:
4743 case DRM_FORMAT_RGBX8888
:
4744 case DRM_FORMAT_BGRX8888
:
4748 case DRM_FORMAT_XRGB2101010
:
4749 case DRM_FORMAT_XBGR2101010
:
4750 case DRM_FORMAT_RGBX1010102
:
4751 case DRM_FORMAT_BGRX1010102
:
4752 case DRM_FORMAT_ARGB2101010
:
4753 case DRM_FORMAT_ABGR2101010
:
4754 case DRM_FORMAT_RGBA1010102
:
4755 case DRM_FORMAT_BGRA1010102
:
4759 case DRM_FORMAT_ARGB8888
:
4760 case DRM_FORMAT_ABGR8888
:
4761 case DRM_FORMAT_RGBA8888
:
4762 case DRM_FORMAT_BGRA8888
:
4767 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4768 drm_get_format_name(format
));
4774 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
4777 * drm_format_num_planes - get the number of planes for format
4778 * @format: pixel format (DRM_FORMAT_*)
4781 * The number of planes used by the specified pixel format.
4783 int drm_format_num_planes(uint32_t format
)
4786 case DRM_FORMAT_YUV410
:
4787 case DRM_FORMAT_YVU410
:
4788 case DRM_FORMAT_YUV411
:
4789 case DRM_FORMAT_YVU411
:
4790 case DRM_FORMAT_YUV420
:
4791 case DRM_FORMAT_YVU420
:
4792 case DRM_FORMAT_YUV422
:
4793 case DRM_FORMAT_YVU422
:
4794 case DRM_FORMAT_YUV444
:
4795 case DRM_FORMAT_YVU444
:
4797 case DRM_FORMAT_NV12
:
4798 case DRM_FORMAT_NV21
:
4799 case DRM_FORMAT_NV16
:
4800 case DRM_FORMAT_NV61
:
4801 case DRM_FORMAT_NV24
:
4802 case DRM_FORMAT_NV42
:
4808 EXPORT_SYMBOL(drm_format_num_planes
);
4811 * drm_format_plane_cpp - determine the bytes per pixel value
4812 * @format: pixel format (DRM_FORMAT_*)
4813 * @plane: plane index
4816 * The bytes per pixel value for the specified plane.
4818 int drm_format_plane_cpp(uint32_t format
, int plane
)
4823 if (plane
>= drm_format_num_planes(format
))
4827 case DRM_FORMAT_YUYV
:
4828 case DRM_FORMAT_YVYU
:
4829 case DRM_FORMAT_UYVY
:
4830 case DRM_FORMAT_VYUY
:
4832 case DRM_FORMAT_NV12
:
4833 case DRM_FORMAT_NV21
:
4834 case DRM_FORMAT_NV16
:
4835 case DRM_FORMAT_NV61
:
4836 case DRM_FORMAT_NV24
:
4837 case DRM_FORMAT_NV42
:
4838 return plane
? 2 : 1;
4839 case DRM_FORMAT_YUV410
:
4840 case DRM_FORMAT_YVU410
:
4841 case DRM_FORMAT_YUV411
:
4842 case DRM_FORMAT_YVU411
:
4843 case DRM_FORMAT_YUV420
:
4844 case DRM_FORMAT_YVU420
:
4845 case DRM_FORMAT_YUV422
:
4846 case DRM_FORMAT_YVU422
:
4847 case DRM_FORMAT_YUV444
:
4848 case DRM_FORMAT_YVU444
:
4851 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
4855 EXPORT_SYMBOL(drm_format_plane_cpp
);
4858 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4859 * @format: pixel format (DRM_FORMAT_*)
4862 * The horizontal chroma subsampling factor for the
4863 * specified pixel format.
4865 int drm_format_horz_chroma_subsampling(uint32_t format
)
4868 case DRM_FORMAT_YUV411
:
4869 case DRM_FORMAT_YVU411
:
4870 case DRM_FORMAT_YUV410
:
4871 case DRM_FORMAT_YVU410
:
4873 case DRM_FORMAT_YUYV
:
4874 case DRM_FORMAT_YVYU
:
4875 case DRM_FORMAT_UYVY
:
4876 case DRM_FORMAT_VYUY
:
4877 case DRM_FORMAT_NV12
:
4878 case DRM_FORMAT_NV21
:
4879 case DRM_FORMAT_NV16
:
4880 case DRM_FORMAT_NV61
:
4881 case DRM_FORMAT_YUV422
:
4882 case DRM_FORMAT_YVU422
:
4883 case DRM_FORMAT_YUV420
:
4884 case DRM_FORMAT_YVU420
:
4890 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
4893 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4894 * @format: pixel format (DRM_FORMAT_*)
4897 * The vertical chroma subsampling factor for the
4898 * specified pixel format.
4900 int drm_format_vert_chroma_subsampling(uint32_t format
)
4903 case DRM_FORMAT_YUV410
:
4904 case DRM_FORMAT_YVU410
:
4906 case DRM_FORMAT_YUV420
:
4907 case DRM_FORMAT_YVU420
:
4908 case DRM_FORMAT_NV12
:
4909 case DRM_FORMAT_NV21
:
4915 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
4918 * drm_mode_config_init - initialize DRM mode_configuration structure
4921 * Initialize @dev's mode_config structure, used for tracking the graphics
4922 * configuration of @dev.
4924 * Since this initializes the modeset locks, no locking is possible. Which is no
4925 * problem, since this should happen single threaded at init time. It is the
4926 * driver's problem to ensure this guarantee.
4929 void drm_mode_config_init(struct drm_device
*dev
)
4931 mutex_init(&dev
->mode_config
.mutex
);
4932 drm_modeset_lock_init(&dev
->mode_config
.connection_mutex
);
4933 mutex_init(&dev
->mode_config
.idr_mutex
);
4934 mutex_init(&dev
->mode_config
.fb_lock
);
4935 INIT_LIST_HEAD(&dev
->mode_config
.fb_list
);
4936 INIT_LIST_HEAD(&dev
->mode_config
.crtc_list
);
4937 INIT_LIST_HEAD(&dev
->mode_config
.connector_list
);
4938 INIT_LIST_HEAD(&dev
->mode_config
.bridge_list
);
4939 INIT_LIST_HEAD(&dev
->mode_config
.encoder_list
);
4940 INIT_LIST_HEAD(&dev
->mode_config
.property_list
);
4941 INIT_LIST_HEAD(&dev
->mode_config
.property_blob_list
);
4942 INIT_LIST_HEAD(&dev
->mode_config
.plane_list
);
4943 idr_init(&dev
->mode_config
.crtc_idr
);
4945 drm_modeset_lock_all(dev
);
4946 drm_mode_create_standard_connector_properties(dev
);
4947 drm_mode_create_standard_plane_properties(dev
);
4948 drm_modeset_unlock_all(dev
);
4950 /* Just to be sure */
4951 dev
->mode_config
.num_fb
= 0;
4952 dev
->mode_config
.num_connector
= 0;
4953 dev
->mode_config
.num_crtc
= 0;
4954 dev
->mode_config
.num_encoder
= 0;
4955 dev
->mode_config
.num_overlay_plane
= 0;
4956 dev
->mode_config
.num_total_plane
= 0;
4958 EXPORT_SYMBOL(drm_mode_config_init
);
4961 * drm_mode_config_cleanup - free up DRM mode_config info
4964 * Free up all the connectors and CRTCs associated with this DRM device, then
4965 * free up the framebuffers and associated buffer objects.
4967 * Note that since this /should/ happen single-threaded at driver/device
4968 * teardown time, no locking is required. It's the driver's job to ensure that
4969 * this guarantee actually holds true.
4971 * FIXME: cleanup any dangling user buffer objects too
4973 void drm_mode_config_cleanup(struct drm_device
*dev
)
4975 struct drm_connector
*connector
, *ot
;
4976 struct drm_crtc
*crtc
, *ct
;
4977 struct drm_encoder
*encoder
, *enct
;
4978 struct drm_bridge
*bridge
, *brt
;
4979 struct drm_framebuffer
*fb
, *fbt
;
4980 struct drm_property
*property
, *pt
;
4981 struct drm_property_blob
*blob
, *bt
;
4982 struct drm_plane
*plane
, *plt
;
4984 list_for_each_entry_safe(encoder
, enct
, &dev
->mode_config
.encoder_list
,
4986 encoder
->funcs
->destroy(encoder
);
4989 list_for_each_entry_safe(bridge
, brt
,
4990 &dev
->mode_config
.bridge_list
, head
) {
4991 bridge
->funcs
->destroy(bridge
);
4994 list_for_each_entry_safe(connector
, ot
,
4995 &dev
->mode_config
.connector_list
, head
) {
4996 connector
->funcs
->destroy(connector
);
4999 list_for_each_entry_safe(property
, pt
, &dev
->mode_config
.property_list
,
5001 drm_property_destroy(dev
, property
);
5004 list_for_each_entry_safe(blob
, bt
, &dev
->mode_config
.property_blob_list
,
5006 drm_property_destroy_blob(dev
, blob
);
5010 * Single-threaded teardown context, so it's not required to grab the
5011 * fb_lock to protect against concurrent fb_list access. Contrary, it
5012 * would actually deadlock with the drm_framebuffer_cleanup function.
5014 * Also, if there are any framebuffers left, that's a driver leak now,
5015 * so politely WARN about this.
5017 WARN_ON(!list_empty(&dev
->mode_config
.fb_list
));
5018 list_for_each_entry_safe(fb
, fbt
, &dev
->mode_config
.fb_list
, head
) {
5019 drm_framebuffer_remove(fb
);
5022 list_for_each_entry_safe(plane
, plt
, &dev
->mode_config
.plane_list
,
5024 plane
->funcs
->destroy(plane
);
5027 list_for_each_entry_safe(crtc
, ct
, &dev
->mode_config
.crtc_list
, head
) {
5028 crtc
->funcs
->destroy(crtc
);
5031 idr_destroy(&dev
->mode_config
.crtc_idr
);
5032 drm_modeset_lock_fini(&dev
->mode_config
.connection_mutex
);
5034 EXPORT_SYMBOL(drm_mode_config_cleanup
);