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>
41 #include <drm/drm_atomic.h>
43 #include "drm_crtc_internal.h"
44 #include "drm_internal.h"
46 static struct drm_framebuffer *
47 internal_framebuffer_create(struct drm_device *dev,
48 const struct drm_mode_fb_cmd2 *r,
49 struct drm_file *file_priv);
51 /* Avoid boilerplate. I'm tired of typing. */
52 #define DRM_ENUM_NAME_FN(fnname, list) \
53 const char *fnname(int val) \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
66 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
68 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
73 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
76 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
84 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
85 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
91 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
98 * Non-global properties, but "required" for certain connectors.
100 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
114 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
117 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
135 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
138 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
144 struct drm_conn_prop_enum_list {
151 * Connector and encoder types.
153 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
173 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
174 { DRM_MODE_ENCODER_NONE, "None" },
175 { DRM_MODE_ENCODER_DAC, "DAC" },
176 { DRM_MODE_ENCODER_TMDS, "TMDS" },
177 { DRM_MODE_ENCODER_LVDS, "LVDS" },
178 { DRM_MODE_ENCODER_TVDAC, "TV" },
179 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
180 { DRM_MODE_ENCODER_DSI, "DSI" },
181 { DRM_MODE_ENCODER_DPMST, "DP MST" },
184 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
185 { SubPixelUnknown, "Unknown" },
186 { SubPixelHorizontalRGB, "Horizontal RGB" },
187 { SubPixelHorizontalBGR, "Horizontal BGR" },
188 { SubPixelVerticalRGB, "Vertical RGB" },
189 { SubPixelVerticalBGR, "Vertical BGR" },
190 { SubPixelNone, "None" },
193 void drm_connector_ida_init(void)
197 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
198 ida_init(&drm_connector_enum_list[i].ida);
201 void drm_connector_ida_destroy(void)
205 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
206 ida_destroy(&drm_connector_enum_list[i].ida);
210 * drm_get_connector_status_name - return a string for connector status
211 * @status: connector status to compute name of
213 * In contrast to the other drm_get_*_name functions this one here returns a
214 * const pointer and hence is threadsafe.
216 const char *drm_get_connector_status_name(enum drm_connector_status status)
218 if (status == connector_status_connected)
220 else if (status == connector_status_disconnected)
221 return "disconnected";
225 EXPORT_SYMBOL(drm_get_connector_status_name);
228 * drm_get_subpixel_order_name - return a string for a given subpixel enum
229 * @order: enum of subpixel_order
231 * Note you could abuse this and return something out of bounds, but that
232 * would be a caller error. No unscrubbed user data should make it here.
234 const char *drm_get_subpixel_order_name(enum subpixel_order order)
236 return drm_subpixel_enum_list[order].name;
238 EXPORT_SYMBOL(drm_get_subpixel_order_name);
240 static char printable_char(int c)
242 return isascii(c) && isprint(c) ? c : '?';
246 * drm_get_format_name - return a string for drm fourcc format
247 * @format: format to compute name of
249 * Note that the buffer used by this function is globally shared and owned by
250 * the function itself.
252 * FIXME: This isn't really multithreading safe.
254 const char *drm_get_format_name(uint32_t format)
258 snprintf(buf, sizeof(buf),
259 "%c%c%c%c %s-endian (0x%08x)",
260 printable_char(format & 0xff),
261 printable_char((format >> 8) & 0xff),
262 printable_char((format >> 16) & 0xff),
263 printable_char((format >> 24) & 0x7f),
264 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
269 EXPORT_SYMBOL(drm_get_format_name);
272 * Internal function to assign a slot in the object idr and optionally
273 * register the object into the idr.
275 static int drm_mode_object_get_reg(struct drm_device *dev,
276 struct drm_mode_object *obj,
279 void (*obj_free_cb)(struct kref *kref))
283 mutex_lock(&dev->mode_config.idr_mutex);
284 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
287 * Set up the object linking under the protection of the idr
288 * lock so that other users can't see inconsistent state.
291 obj->type = obj_type;
293 obj->free_cb = obj_free_cb;
294 kref_init(&obj->refcount);
297 mutex_unlock(&dev->mode_config.idr_mutex);
299 return ret < 0 ? ret : 0;
303 * drm_mode_object_get - allocate a new modeset identifier
305 * @obj: object pointer, used to generate unique ID
306 * @obj_type: object type
308 * Create a unique identifier based on @ptr in @dev's identifier space. Used
309 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
310 * modeset identifiers are _not_ reference counted. Hence don't use this for
311 * reference counted modeset objects like framebuffers.
314 * Zero on success, error code on failure.
316 int drm_mode_object_get(struct drm_device *dev,
317 struct drm_mode_object *obj, uint32_t obj_type)
319 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
322 static void drm_mode_object_register(struct drm_device *dev,
323 struct drm_mode_object *obj)
325 mutex_lock(&dev->mode_config.idr_mutex);
326 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
327 mutex_unlock(&dev->mode_config.idr_mutex);
331 * drm_mode_object_unregister - free a modeset identifer
333 * @object: object to free
335 * Free @id from @dev's unique identifier pool.
336 * This function can be called multiple times, and guards against
338 * These modeset identifiers are _not_ reference counted. Hence don't use this
339 * for reference counted modeset objects like framebuffers.
341 void drm_mode_object_unregister(struct drm_device *dev,
342 struct drm_mode_object *object)
344 mutex_lock(&dev->mode_config.idr_mutex);
346 idr_remove(&dev->mode_config.crtc_idr, object->id);
349 mutex_unlock(&dev->mode_config.idr_mutex);
352 static struct drm_mode_object *_object_find(struct drm_device *dev,
353 uint32_t id, uint32_t type)
355 struct drm_mode_object *obj = NULL;
357 mutex_lock(&dev->mode_config.idr_mutex);
358 obj = idr_find(&dev->mode_config.crtc_idr, id);
359 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
361 if (obj && obj->id != id)
363 /* don't leak out unref'd fb's */
365 obj->type == DRM_MODE_OBJECT_BLOB)
367 mutex_unlock(&dev->mode_config.idr_mutex);
373 * drm_mode_object_find - look up a drm object with static lifetime
375 * @id: id of the mode object
376 * @type: type of the mode object
378 * Note that framebuffers cannot be looked up with this functions - since those
379 * are reference counted, they need special treatment. Even with
380 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
381 * rather than WARN_ON()).
383 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
384 uint32_t id, uint32_t type)
386 struct drm_mode_object *obj = NULL;
388 /* Framebuffers are reference counted and need their own lookup
390 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
391 obj = _object_find(dev, id, type);
394 EXPORT_SYMBOL(drm_mode_object_find);
396 void drm_mode_object_unreference(struct drm_mode_object *obj)
399 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
400 kref_put(&obj->refcount, obj->free_cb);
403 EXPORT_SYMBOL(drm_mode_object_unreference);
406 * drm_mode_object_reference - incr the fb refcnt
409 * This function operates only on refcounted objects.
410 * This functions increments the object's refcount.
412 void drm_mode_object_reference(struct drm_mode_object *obj)
415 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
416 kref_get(&obj->refcount);
419 EXPORT_SYMBOL(drm_mode_object_reference);
421 static void drm_framebuffer_free(struct kref *kref)
423 struct drm_framebuffer *fb =
424 container_of(kref, struct drm_framebuffer, base.refcount);
425 struct drm_device *dev = fb->dev;
428 * The lookup idr holds a weak reference, which has not necessarily been
429 * removed at this point. Check for that.
431 mutex_lock(&dev->mode_config.fb_lock);
432 drm_mode_object_unregister(dev, &fb->base);
433 mutex_unlock(&dev->mode_config.fb_lock);
435 fb->funcs->destroy(fb);
439 * drm_framebuffer_init - initialize a framebuffer
441 * @fb: framebuffer to be initialized
442 * @funcs: ... with these functions
444 * Allocates an ID for the framebuffer's parent mode object, sets its mode
445 * functions & device file and adds it to the master fd list.
448 * This functions publishes the fb and makes it available for concurrent access
449 * by other users. Which means by this point the fb _must_ be fully set up -
450 * since all the fb attributes are invariant over its lifetime, no further
451 * locking but only correct reference counting is required.
454 * Zero on success, error code on failure.
456 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
457 const struct drm_framebuffer_funcs *funcs)
461 INIT_LIST_HEAD(&fb->filp_head);
465 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
466 false, drm_framebuffer_free);
470 mutex_lock(&dev->mode_config.fb_lock);
471 dev->mode_config.num_fb++;
472 list_add(&fb->head, &dev->mode_config.fb_list);
474 drm_mode_object_register(dev, &fb->base);
475 mutex_unlock(&dev->mode_config.fb_lock);
479 EXPORT_SYMBOL(drm_framebuffer_init);
482 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
484 * @id: id of the fb object
486 * If successful, this grabs an additional reference to the framebuffer -
487 * callers need to make sure to eventually unreference the returned framebuffer
488 * again, using @drm_framebuffer_unreference.
490 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
493 struct drm_mode_object *obj;
494 struct drm_framebuffer *fb = NULL;
496 mutex_lock(&dev->mode_config.fb_lock);
497 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
500 if (!kref_get_unless_zero(&fb->base.refcount))
503 mutex_unlock(&dev->mode_config.fb_lock);
507 EXPORT_SYMBOL(drm_framebuffer_lookup);
510 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
511 * @fb: fb to unregister
513 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
514 * those used for fbdev. Note that the caller must hold a reference of it's own,
515 * i.e. the object may not be destroyed through this call (since it'll lead to a
516 * locking inversion).
518 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
520 struct drm_device *dev;
527 mutex_lock(&dev->mode_config.fb_lock);
528 /* Mark fb as reaped and drop idr ref. */
529 drm_mode_object_unregister(dev, &fb->base);
530 mutex_unlock(&dev->mode_config.fb_lock);
532 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
535 * drm_framebuffer_cleanup - remove a framebuffer object
536 * @fb: framebuffer to remove
538 * Cleanup framebuffer. This function is intended to be used from the drivers
539 * ->destroy callback. It can also be used to clean up driver private
540 * framebuffers embedded into a larger structure.
542 * Note that this function does not remove the fb from active usuage - if it is
543 * still used anywhere, hilarity can ensue since userspace could call getfb on
544 * the id and get back -EINVAL. Obviously no concern at driver unload time.
546 * Also, the framebuffer will not be removed from the lookup idr - for
547 * user-created framebuffers this will happen in in the rmfb ioctl. For
548 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
549 * drm_framebuffer_unregister_private.
551 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
553 struct drm_device *dev = fb->dev;
555 mutex_lock(&dev->mode_config.fb_lock);
557 dev->mode_config.num_fb--;
558 mutex_unlock(&dev->mode_config.fb_lock);
560 EXPORT_SYMBOL(drm_framebuffer_cleanup);
563 * drm_framebuffer_remove - remove and unreference a framebuffer object
564 * @fb: framebuffer to remove
566 * Scans all the CRTCs and planes in @dev's mode_config. If they're
567 * using @fb, removes it, setting it to NULL. Then drops the reference to the
568 * passed-in framebuffer. Might take the modeset locks.
570 * Note that this function optimizes the cleanup away if the caller holds the
571 * last reference to the framebuffer. It is also guaranteed to not take the
572 * modeset locks in this case.
574 void drm_framebuffer_remove(struct drm_framebuffer *fb)
576 struct drm_device *dev;
577 struct drm_crtc *crtc;
578 struct drm_plane *plane;
579 struct drm_mode_set set;
587 WARN_ON(!list_empty(&fb->filp_head));
590 * drm ABI mandates that we remove any deleted framebuffers from active
591 * useage. But since most sane clients only remove framebuffers they no
592 * longer need, try to optimize this away.
594 * Since we're holding a reference ourselves, observing a refcount of 1
595 * means that we're the last holder and can skip it. Also, the refcount
596 * can never increase from 1 again, so we don't need any barriers or
599 * Note that userspace could try to race with use and instate a new
600 * usage _after_ we've cleared all current ones. End result will be an
601 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
604 if (drm_framebuffer_read_refcount(fb) > 1) {
605 drm_modeset_lock_all(dev);
606 /* remove from any CRTC */
607 drm_for_each_crtc(crtc, dev) {
608 if (crtc->primary->fb == fb) {
609 /* should turn off the crtc */
610 memset(&set, 0, sizeof(struct drm_mode_set));
613 ret = drm_mode_set_config_internal(&set);
615 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
619 drm_for_each_plane(plane, dev) {
621 drm_plane_force_disable(plane);
623 drm_modeset_unlock_all(dev);
626 drm_framebuffer_unreference(fb);
628 EXPORT_SYMBOL(drm_framebuffer_remove);
630 DEFINE_WW_CLASS(crtc_ww_class);
632 static unsigned int drm_num_crtcs(struct drm_device *dev)
634 unsigned int num = 0;
635 struct drm_crtc *tmp;
637 drm_for_each_crtc(tmp, dev) {
645 * drm_crtc_init_with_planes - Initialise a new CRTC object with
646 * specified primary and cursor planes.
648 * @crtc: CRTC object to init
649 * @primary: Primary plane for CRTC
650 * @cursor: Cursor plane for CRTC
651 * @funcs: callbacks for the new CRTC
652 * @name: printf style format string for the CRTC name, or NULL for default name
654 * Inits a new object created as base part of a driver crtc object.
657 * Zero on success, error code on failure.
659 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
660 struct drm_plane *primary,
661 struct drm_plane *cursor,
662 const struct drm_crtc_funcs *funcs,
663 const char *name, ...)
665 struct drm_mode_config *config = &dev->mode_config;
668 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
669 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
674 drm_modeset_lock_init(&crtc->mutex);
675 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
683 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
686 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
690 drm_mode_object_unregister(dev, &crtc->base);
694 crtc->base.properties = &crtc->properties;
696 list_add_tail(&crtc->head, &config->crtc_list);
699 crtc->primary = primary;
700 crtc->cursor = cursor;
702 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
704 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
706 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
707 drm_object_attach_property(&crtc->base, config->prop_active, 0);
708 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
713 EXPORT_SYMBOL(drm_crtc_init_with_planes);
716 * drm_crtc_cleanup - Clean up the core crtc usage
717 * @crtc: CRTC to cleanup
719 * This function cleans up @crtc and removes it from the DRM mode setting
720 * core. Note that the function does *not* free the crtc structure itself,
721 * this is the responsibility of the caller.
723 void drm_crtc_cleanup(struct drm_crtc *crtc)
725 struct drm_device *dev = crtc->dev;
727 kfree(crtc->gamma_store);
728 crtc->gamma_store = NULL;
730 drm_modeset_lock_fini(&crtc->mutex);
732 drm_mode_object_unregister(dev, &crtc->base);
733 list_del(&crtc->head);
734 dev->mode_config.num_crtc--;
736 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
737 if (crtc->state && crtc->funcs->atomic_destroy_state)
738 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
742 memset(crtc, 0, sizeof(*crtc));
744 EXPORT_SYMBOL(drm_crtc_cleanup);
747 * drm_crtc_index - find the index of a registered CRTC
748 * @crtc: CRTC to find index for
750 * Given a registered CRTC, return the index of that CRTC within a DRM
751 * device's list of CRTCs.
753 unsigned int drm_crtc_index(struct drm_crtc *crtc)
755 unsigned int index = 0;
756 struct drm_crtc *tmp;
758 drm_for_each_crtc(tmp, crtc->dev) {
767 EXPORT_SYMBOL(drm_crtc_index);
770 * drm_mode_remove - remove and free a mode
771 * @connector: connector list to modify
772 * @mode: mode to remove
774 * Remove @mode from @connector's mode list, then free it.
776 static void drm_mode_remove(struct drm_connector *connector,
777 struct drm_display_mode *mode)
779 list_del(&mode->head);
780 drm_mode_destroy(connector->dev, mode);
784 * drm_display_info_set_bus_formats - set the supported bus formats
785 * @info: display info to store bus formats in
786 * @formats: array containing the supported bus formats
787 * @num_formats: the number of entries in the fmts array
789 * Store the supported bus formats in display info structure.
790 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
791 * a full list of available formats.
793 int drm_display_info_set_bus_formats(struct drm_display_info *info,
795 unsigned int num_formats)
799 if (!formats && num_formats)
802 if (formats && num_formats) {
803 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
809 kfree(info->bus_formats);
810 info->bus_formats = fmts;
811 info->num_bus_formats = num_formats;
815 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
818 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
819 * @connector: connector to quwery
821 * The kernel supports per-connector configration of its consoles through
822 * use of the video= parameter. This function parses that option and
823 * extracts the user's specified mode (or enable/disable status) for a
824 * particular connector. This is typically only used during the early fbdev
827 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
829 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
832 if (fb_get_options(connector->name, &option))
835 if (!drm_mode_parse_command_line_for_connector(option,
843 switch (mode->force) {
847 case DRM_FORCE_ON_DIGITAL:
856 DRM_INFO("forcing %s connector %s\n", connector->name, s);
857 connector->force = mode->force;
860 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
862 mode->xres, mode->yres,
863 mode->refresh_specified ? mode->refresh : 60,
864 mode->rb ? " reduced blanking" : "",
865 mode->margins ? " with margins" : "",
866 mode->interlace ? " interlaced" : "");
870 * drm_connector_init - Init a preallocated connector
872 * @connector: the connector to init
873 * @funcs: callbacks for this connector
874 * @connector_type: user visible type of the connector
876 * Initialises a preallocated connector. Connectors should be
877 * subclassed as part of driver connector objects.
880 * Zero on success, error code on failure.
882 int drm_connector_init(struct drm_device *dev,
883 struct drm_connector *connector,
884 const struct drm_connector_funcs *funcs,
887 struct drm_mode_config *config = &dev->mode_config;
889 struct ida *connector_ida =
890 &drm_connector_enum_list[connector_type].ida;
892 drm_modeset_lock_all(dev);
894 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
898 connector->base.properties = &connector->properties;
899 connector->dev = dev;
900 connector->funcs = funcs;
902 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
903 if (connector->connector_id < 0) {
904 ret = connector->connector_id;
908 connector->connector_type = connector_type;
909 connector->connector_type_id =
910 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
911 if (connector->connector_type_id < 0) {
912 ret = connector->connector_type_id;
916 kasprintf(GFP_KERNEL, "%s-%d",
917 drm_connector_enum_list[connector_type].name,
918 connector->connector_type_id);
919 if (!connector->name) {
921 goto out_put_type_id;
924 INIT_LIST_HEAD(&connector->probed_modes);
925 INIT_LIST_HEAD(&connector->modes);
926 connector->edid_blob_ptr = NULL;
927 connector->status = connector_status_unknown;
929 drm_connector_get_cmdline_mode(connector);
931 /* We should add connectors at the end to avoid upsetting the connector
933 list_add_tail(&connector->head, &config->connector_list);
934 config->num_connector++;
936 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
937 drm_object_attach_property(&connector->base,
938 config->edid_property,
941 drm_object_attach_property(&connector->base,
942 config->dpms_property, 0);
944 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
945 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
948 connector->debugfs_entry = NULL;
951 ida_remove(connector_ida, connector->connector_type_id);
954 ida_remove(&config->connector_ida, connector->connector_id);
957 drm_mode_object_unregister(dev, &connector->base);
960 drm_modeset_unlock_all(dev);
964 EXPORT_SYMBOL(drm_connector_init);
967 * drm_connector_cleanup - cleans up an initialised connector
968 * @connector: connector to cleanup
970 * Cleans up the connector but doesn't free the object.
972 void drm_connector_cleanup(struct drm_connector *connector)
974 struct drm_device *dev = connector->dev;
975 struct drm_display_mode *mode, *t;
977 if (connector->tile_group) {
978 drm_mode_put_tile_group(dev, connector->tile_group);
979 connector->tile_group = NULL;
982 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
983 drm_mode_remove(connector, mode);
985 list_for_each_entry_safe(mode, t, &connector->modes, head)
986 drm_mode_remove(connector, mode);
988 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
989 connector->connector_type_id);
991 ida_remove(&dev->mode_config.connector_ida,
992 connector->connector_id);
994 kfree(connector->display_info.bus_formats);
995 drm_mode_object_unregister(dev, &connector->base);
996 kfree(connector->name);
997 connector->name = NULL;
998 list_del(&connector->head);
999 dev->mode_config.num_connector--;
1001 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1002 if (connector->state && connector->funcs->atomic_destroy_state)
1003 connector->funcs->atomic_destroy_state(connector,
1006 memset(connector, 0, sizeof(*connector));
1008 EXPORT_SYMBOL(drm_connector_cleanup);
1011 * drm_connector_register - register a connector
1012 * @connector: the connector to register
1014 * Register userspace interfaces for a connector
1017 * Zero on success, error code on failure.
1019 int drm_connector_register(struct drm_connector *connector)
1023 drm_mode_object_register(connector->dev, &connector->base);
1025 ret = drm_sysfs_connector_add(connector);
1029 ret = drm_debugfs_connector_add(connector);
1031 drm_sysfs_connector_remove(connector);
1037 EXPORT_SYMBOL(drm_connector_register);
1040 * drm_connector_unregister - unregister a connector
1041 * @connector: the connector to unregister
1043 * Unregister userspace interfaces for a connector
1045 void drm_connector_unregister(struct drm_connector *connector)
1047 drm_sysfs_connector_remove(connector);
1048 drm_debugfs_connector_remove(connector);
1050 EXPORT_SYMBOL(drm_connector_unregister);
1053 * drm_connector_register_all - register all connectors
1056 * This function registers all connectors in sysfs and other places so that
1057 * userspace can start to access them. Drivers can call it after calling
1058 * drm_dev_register() to complete the device registration, if they don't call
1059 * drm_connector_register() on each connector individually.
1061 * When a device is unplugged and should be removed from userspace access,
1062 * call drm_connector_unregister_all(), which is the inverse of this
1066 * Zero on success, error code on failure.
1068 int drm_connector_register_all(struct drm_device *dev)
1070 struct drm_connector *connector;
1073 mutex_lock(&dev->mode_config.mutex);
1075 drm_for_each_connector(connector, dev) {
1076 ret = drm_connector_register(connector);
1081 mutex_unlock(&dev->mode_config.mutex);
1086 mutex_unlock(&dev->mode_config.mutex);
1087 drm_connector_unregister_all(dev);
1090 EXPORT_SYMBOL(drm_connector_register_all);
1093 * drm_connector_unregister_all - unregister connector userspace interfaces
1096 * This functions unregisters all connectors from sysfs and other places so
1097 * that userspace can no longer access them. Drivers should call this as the
1098 * first step tearing down the device instace, or when the underlying
1099 * physical device disappeared (e.g. USB unplug), right before calling
1100 * drm_dev_unregister().
1102 void drm_connector_unregister_all(struct drm_device *dev)
1104 struct drm_connector *connector;
1106 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1107 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1108 drm_connector_unregister(connector);
1110 EXPORT_SYMBOL(drm_connector_unregister_all);
1113 * drm_encoder_init - Init a preallocated encoder
1115 * @encoder: the encoder to init
1116 * @funcs: callbacks for this encoder
1117 * @encoder_type: user visible type of the encoder
1118 * @name: printf style format string for the encoder name, or NULL for default name
1120 * Initialises a preallocated encoder. Encoder should be
1121 * subclassed as part of driver encoder objects.
1124 * Zero on success, error code on failure.
1126 int drm_encoder_init(struct drm_device *dev,
1127 struct drm_encoder *encoder,
1128 const struct drm_encoder_funcs *funcs,
1129 int encoder_type, const char *name, ...)
1133 drm_modeset_lock_all(dev);
1135 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1140 encoder->encoder_type = encoder_type;
1141 encoder->funcs = funcs;
1146 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1149 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1150 drm_encoder_enum_list[encoder_type].name,
1153 if (!encoder->name) {
1158 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1159 dev->mode_config.num_encoder++;
1163 drm_mode_object_unregister(dev, &encoder->base);
1166 drm_modeset_unlock_all(dev);
1170 EXPORT_SYMBOL(drm_encoder_init);
1173 * drm_encoder_index - find the index of a registered encoder
1174 * @encoder: encoder to find index for
1176 * Given a registered encoder, return the index of that encoder within a DRM
1177 * device's list of encoders.
1179 unsigned int drm_encoder_index(struct drm_encoder *encoder)
1181 unsigned int index = 0;
1182 struct drm_encoder *tmp;
1184 drm_for_each_encoder(tmp, encoder->dev) {
1193 EXPORT_SYMBOL(drm_encoder_index);
1196 * drm_encoder_cleanup - cleans up an initialised encoder
1197 * @encoder: encoder to cleanup
1199 * Cleans up the encoder but doesn't free the object.
1201 void drm_encoder_cleanup(struct drm_encoder *encoder)
1203 struct drm_device *dev = encoder->dev;
1205 drm_modeset_lock_all(dev);
1206 drm_mode_object_unregister(dev, &encoder->base);
1207 kfree(encoder->name);
1208 list_del(&encoder->head);
1209 dev->mode_config.num_encoder--;
1210 drm_modeset_unlock_all(dev);
1212 memset(encoder, 0, sizeof(*encoder));
1214 EXPORT_SYMBOL(drm_encoder_cleanup);
1216 static unsigned int drm_num_planes(struct drm_device *dev)
1218 unsigned int num = 0;
1219 struct drm_plane *tmp;
1221 drm_for_each_plane(tmp, dev) {
1229 * drm_universal_plane_init - Initialize a new universal plane object
1231 * @plane: plane object to init
1232 * @possible_crtcs: bitmask of possible CRTCs
1233 * @funcs: callbacks for the new plane
1234 * @formats: array of supported formats (%DRM_FORMAT_*)
1235 * @format_count: number of elements in @formats
1236 * @type: type of plane (overlay, primary, cursor)
1237 * @name: printf style format string for the plane name, or NULL for default name
1239 * Initializes a plane object of type @type.
1242 * Zero on success, error code on failure.
1244 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1245 unsigned long possible_crtcs,
1246 const struct drm_plane_funcs *funcs,
1247 const uint32_t *formats, unsigned int format_count,
1248 enum drm_plane_type type,
1249 const char *name, ...)
1251 struct drm_mode_config *config = &dev->mode_config;
1254 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1258 drm_modeset_lock_init(&plane->mutex);
1260 plane->base.properties = &plane->properties;
1262 plane->funcs = funcs;
1263 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1265 if (!plane->format_types) {
1266 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1267 drm_mode_object_unregister(dev, &plane->base);
1275 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1278 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1279 drm_num_planes(dev));
1282 kfree(plane->format_types);
1283 drm_mode_object_unregister(dev, &plane->base);
1287 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1288 plane->format_count = format_count;
1289 plane->possible_crtcs = possible_crtcs;
1292 list_add_tail(&plane->head, &config->plane_list);
1293 config->num_total_plane++;
1294 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1295 config->num_overlay_plane++;
1297 drm_object_attach_property(&plane->base,
1298 config->plane_type_property,
1301 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1302 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1303 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1304 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1305 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1306 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1307 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1308 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1309 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1310 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1311 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1316 EXPORT_SYMBOL(drm_universal_plane_init);
1319 * drm_plane_init - Initialize a legacy plane
1321 * @plane: plane object to init
1322 * @possible_crtcs: bitmask of possible CRTCs
1323 * @funcs: callbacks for the new plane
1324 * @formats: array of supported formats (%DRM_FORMAT_*)
1325 * @format_count: number of elements in @formats
1326 * @is_primary: plane type (primary vs overlay)
1328 * Legacy API to initialize a DRM plane.
1330 * New drivers should call drm_universal_plane_init() instead.
1333 * Zero on success, error code on failure.
1335 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1336 unsigned long possible_crtcs,
1337 const struct drm_plane_funcs *funcs,
1338 const uint32_t *formats, unsigned int format_count,
1341 enum drm_plane_type type;
1343 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1344 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1345 formats, format_count, type, NULL);
1347 EXPORT_SYMBOL(drm_plane_init);
1350 * drm_plane_cleanup - Clean up the core plane usage
1351 * @plane: plane to cleanup
1353 * This function cleans up @plane and removes it from the DRM mode setting
1354 * core. Note that the function does *not* free the plane structure itself,
1355 * this is the responsibility of the caller.
1357 void drm_plane_cleanup(struct drm_plane *plane)
1359 struct drm_device *dev = plane->dev;
1361 drm_modeset_lock_all(dev);
1362 kfree(plane->format_types);
1363 drm_mode_object_unregister(dev, &plane->base);
1365 BUG_ON(list_empty(&plane->head));
1367 list_del(&plane->head);
1368 dev->mode_config.num_total_plane--;
1369 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1370 dev->mode_config.num_overlay_plane--;
1371 drm_modeset_unlock_all(dev);
1373 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1374 if (plane->state && plane->funcs->atomic_destroy_state)
1375 plane->funcs->atomic_destroy_state(plane, plane->state);
1379 memset(plane, 0, sizeof(*plane));
1381 EXPORT_SYMBOL(drm_plane_cleanup);
1384 * drm_plane_index - find the index of a registered plane
1385 * @plane: plane to find index for
1387 * Given a registered plane, return the index of that CRTC within a DRM
1388 * device's list of planes.
1390 unsigned int drm_plane_index(struct drm_plane *plane)
1392 unsigned int index = 0;
1393 struct drm_plane *tmp;
1395 drm_for_each_plane(tmp, plane->dev) {
1404 EXPORT_SYMBOL(drm_plane_index);
1407 * drm_plane_from_index - find the registered plane at an index
1409 * @idx: index of registered plane to find for
1411 * Given a plane index, return the registered plane from DRM device's
1412 * list of planes with matching index.
1415 drm_plane_from_index(struct drm_device *dev, int idx)
1417 struct drm_plane *plane;
1420 drm_for_each_plane(plane, dev) {
1427 EXPORT_SYMBOL(drm_plane_from_index);
1430 * drm_plane_force_disable - Forcibly disable a plane
1431 * @plane: plane to disable
1433 * Forces the plane to be disabled.
1435 * Used when the plane's current framebuffer is destroyed,
1436 * and when restoring fbdev mode.
1438 void drm_plane_force_disable(struct drm_plane *plane)
1445 plane->old_fb = plane->fb;
1446 ret = plane->funcs->disable_plane(plane);
1448 DRM_ERROR("failed to disable plane with busy fb\n");
1449 plane->old_fb = NULL;
1452 /* disconnect the plane from the fb and crtc: */
1453 drm_framebuffer_unreference(plane->old_fb);
1454 plane->old_fb = NULL;
1458 EXPORT_SYMBOL(drm_plane_force_disable);
1460 static int drm_mode_create_standard_properties(struct drm_device *dev)
1462 struct drm_property *prop;
1465 * Standard properties (apply to all connectors)
1467 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1468 DRM_MODE_PROP_IMMUTABLE,
1472 dev->mode_config.edid_property = prop;
1474 prop = drm_property_create_enum(dev, 0,
1475 "DPMS", drm_dpms_enum_list,
1476 ARRAY_SIZE(drm_dpms_enum_list));
1479 dev->mode_config.dpms_property = prop;
1481 prop = drm_property_create(dev,
1482 DRM_MODE_PROP_BLOB |
1483 DRM_MODE_PROP_IMMUTABLE,
1487 dev->mode_config.path_property = prop;
1489 prop = drm_property_create(dev,
1490 DRM_MODE_PROP_BLOB |
1491 DRM_MODE_PROP_IMMUTABLE,
1495 dev->mode_config.tile_property = prop;
1497 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1498 "type", drm_plane_type_enum_list,
1499 ARRAY_SIZE(drm_plane_type_enum_list));
1502 dev->mode_config.plane_type_property = prop;
1504 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1505 "SRC_X", 0, UINT_MAX);
1508 dev->mode_config.prop_src_x = prop;
1510 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1511 "SRC_Y", 0, UINT_MAX);
1514 dev->mode_config.prop_src_y = prop;
1516 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1517 "SRC_W", 0, UINT_MAX);
1520 dev->mode_config.prop_src_w = prop;
1522 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1523 "SRC_H", 0, UINT_MAX);
1526 dev->mode_config.prop_src_h = prop;
1528 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1529 "CRTC_X", INT_MIN, INT_MAX);
1532 dev->mode_config.prop_crtc_x = prop;
1534 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1535 "CRTC_Y", INT_MIN, INT_MAX);
1538 dev->mode_config.prop_crtc_y = prop;
1540 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1541 "CRTC_W", 0, INT_MAX);
1544 dev->mode_config.prop_crtc_w = prop;
1546 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1547 "CRTC_H", 0, INT_MAX);
1550 dev->mode_config.prop_crtc_h = prop;
1552 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1553 "FB_ID", DRM_MODE_OBJECT_FB);
1556 dev->mode_config.prop_fb_id = prop;
1558 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1559 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1562 dev->mode_config.prop_crtc_id = prop;
1564 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1568 dev->mode_config.prop_active = prop;
1570 prop = drm_property_create(dev,
1571 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1575 dev->mode_config.prop_mode_id = prop;
1577 prop = drm_property_create(dev,
1582 dev->mode_config.degamma_lut_property = prop;
1584 prop = drm_property_create_range(dev,
1585 DRM_MODE_PROP_IMMUTABLE,
1586 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1589 dev->mode_config.degamma_lut_size_property = prop;
1591 prop = drm_property_create(dev,
1596 dev->mode_config.ctm_property = prop;
1598 prop = drm_property_create(dev,
1603 dev->mode_config.gamma_lut_property = prop;
1605 prop = drm_property_create_range(dev,
1606 DRM_MODE_PROP_IMMUTABLE,
1607 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1610 dev->mode_config.gamma_lut_size_property = prop;
1616 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1619 * Called by a driver the first time a DVI-I connector is made.
1621 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1623 struct drm_property *dvi_i_selector;
1624 struct drm_property *dvi_i_subconnector;
1626 if (dev->mode_config.dvi_i_select_subconnector_property)
1630 drm_property_create_enum(dev, 0,
1631 "select subconnector",
1632 drm_dvi_i_select_enum_list,
1633 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1634 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1636 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1638 drm_dvi_i_subconnector_enum_list,
1639 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1640 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1644 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1647 * drm_create_tv_properties - create TV specific connector properties
1649 * @num_modes: number of different TV formats (modes) supported
1650 * @modes: array of pointers to strings containing name of each format
1652 * Called by a driver's TV initialization routine, this function creates
1653 * the TV specific connector properties for a given device. Caller is
1654 * responsible for allocating a list of format names and passing them to
1657 int drm_mode_create_tv_properties(struct drm_device *dev,
1658 unsigned int num_modes,
1659 const char * const modes[])
1661 struct drm_property *tv_selector;
1662 struct drm_property *tv_subconnector;
1665 if (dev->mode_config.tv_select_subconnector_property)
1669 * Basic connector properties
1671 tv_selector = drm_property_create_enum(dev, 0,
1672 "select subconnector",
1673 drm_tv_select_enum_list,
1674 ARRAY_SIZE(drm_tv_select_enum_list));
1678 dev->mode_config.tv_select_subconnector_property = tv_selector;
1681 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1683 drm_tv_subconnector_enum_list,
1684 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1685 if (!tv_subconnector)
1687 dev->mode_config.tv_subconnector_property = tv_subconnector;
1690 * Other, TV specific properties: margins & TV modes.
1692 dev->mode_config.tv_left_margin_property =
1693 drm_property_create_range(dev, 0, "left margin", 0, 100);
1694 if (!dev->mode_config.tv_left_margin_property)
1697 dev->mode_config.tv_right_margin_property =
1698 drm_property_create_range(dev, 0, "right margin", 0, 100);
1699 if (!dev->mode_config.tv_right_margin_property)
1702 dev->mode_config.tv_top_margin_property =
1703 drm_property_create_range(dev, 0, "top margin", 0, 100);
1704 if (!dev->mode_config.tv_top_margin_property)
1707 dev->mode_config.tv_bottom_margin_property =
1708 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1709 if (!dev->mode_config.tv_bottom_margin_property)
1712 dev->mode_config.tv_mode_property =
1713 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1715 if (!dev->mode_config.tv_mode_property)
1718 for (i = 0; i < num_modes; i++)
1719 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1722 dev->mode_config.tv_brightness_property =
1723 drm_property_create_range(dev, 0, "brightness", 0, 100);
1724 if (!dev->mode_config.tv_brightness_property)
1727 dev->mode_config.tv_contrast_property =
1728 drm_property_create_range(dev, 0, "contrast", 0, 100);
1729 if (!dev->mode_config.tv_contrast_property)
1732 dev->mode_config.tv_flicker_reduction_property =
1733 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1734 if (!dev->mode_config.tv_flicker_reduction_property)
1737 dev->mode_config.tv_overscan_property =
1738 drm_property_create_range(dev, 0, "overscan", 0, 100);
1739 if (!dev->mode_config.tv_overscan_property)
1742 dev->mode_config.tv_saturation_property =
1743 drm_property_create_range(dev, 0, "saturation", 0, 100);
1744 if (!dev->mode_config.tv_saturation_property)
1747 dev->mode_config.tv_hue_property =
1748 drm_property_create_range(dev, 0, "hue", 0, 100);
1749 if (!dev->mode_config.tv_hue_property)
1756 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1759 * drm_mode_create_scaling_mode_property - create scaling mode property
1762 * Called by a driver the first time it's needed, must be attached to desired
1765 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1767 struct drm_property *scaling_mode;
1769 if (dev->mode_config.scaling_mode_property)
1773 drm_property_create_enum(dev, 0, "scaling mode",
1774 drm_scaling_mode_enum_list,
1775 ARRAY_SIZE(drm_scaling_mode_enum_list));
1777 dev->mode_config.scaling_mode_property = scaling_mode;
1781 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1784 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1787 * Called by a driver the first time it's needed, must be attached to desired
1791 * Zero on success, negative errno on failure.
1793 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1795 if (dev->mode_config.aspect_ratio_property)
1798 dev->mode_config.aspect_ratio_property =
1799 drm_property_create_enum(dev, 0, "aspect ratio",
1800 drm_aspect_ratio_enum_list,
1801 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1803 if (dev->mode_config.aspect_ratio_property == NULL)
1808 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1811 * drm_mode_create_dirty_property - create dirty property
1814 * Called by a driver the first time it's needed, must be attached to desired
1817 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1819 struct drm_property *dirty_info;
1821 if (dev->mode_config.dirty_info_property)
1825 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1827 drm_dirty_info_enum_list,
1828 ARRAY_SIZE(drm_dirty_info_enum_list));
1829 dev->mode_config.dirty_info_property = dirty_info;
1833 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1836 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1839 * Create the the suggested x/y offset property for connectors.
1841 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1843 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1846 dev->mode_config.suggested_x_property =
1847 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1849 dev->mode_config.suggested_y_property =
1850 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1852 if (dev->mode_config.suggested_x_property == NULL ||
1853 dev->mode_config.suggested_y_property == NULL)
1857 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1860 * drm_mode_getresources - get graphics configuration
1861 * @dev: drm device for the ioctl
1862 * @data: data pointer for the ioctl
1863 * @file_priv: drm file for the ioctl call
1865 * Construct a set of configuration description structures and return
1866 * them to the user, including CRTC, connector and framebuffer configuration.
1868 * Called by the user via ioctl.
1871 * Zero on success, negative errno on failure.
1873 int drm_mode_getresources(struct drm_device *dev, void *data,
1874 struct drm_file *file_priv)
1876 struct drm_mode_card_res *card_res = data;
1877 struct list_head *lh;
1878 struct drm_framebuffer *fb;
1879 struct drm_connector *connector;
1880 struct drm_crtc *crtc;
1881 struct drm_encoder *encoder;
1883 int connector_count = 0;
1886 int encoder_count = 0;
1888 uint32_t __user *fb_id;
1889 uint32_t __user *crtc_id;
1890 uint32_t __user *connector_id;
1891 uint32_t __user *encoder_id;
1893 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1897 mutex_lock(&file_priv->fbs_lock);
1899 * For the non-control nodes we need to limit the list of resources
1900 * by IDs in the group list for this node
1902 list_for_each(lh, &file_priv->fbs)
1905 /* handle this in 4 parts */
1907 if (card_res->count_fbs >= fb_count) {
1909 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1910 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1911 if (put_user(fb->base.id, fb_id + copied)) {
1912 mutex_unlock(&file_priv->fbs_lock);
1918 card_res->count_fbs = fb_count;
1919 mutex_unlock(&file_priv->fbs_lock);
1921 /* mode_config.mutex protects the connector list against e.g. DP MST
1922 * connector hot-adding. CRTC/Plane lists are invariant. */
1923 mutex_lock(&dev->mode_config.mutex);
1924 drm_for_each_crtc(crtc, dev)
1927 drm_for_each_connector(connector, dev)
1930 drm_for_each_encoder(encoder, dev)
1933 card_res->max_height = dev->mode_config.max_height;
1934 card_res->min_height = dev->mode_config.min_height;
1935 card_res->max_width = dev->mode_config.max_width;
1936 card_res->min_width = dev->mode_config.min_width;
1939 if (card_res->count_crtcs >= crtc_count) {
1941 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1942 drm_for_each_crtc(crtc, dev) {
1943 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1944 crtc->base.id, crtc->name);
1945 if (put_user(crtc->base.id, crtc_id + copied)) {
1952 card_res->count_crtcs = crtc_count;
1955 if (card_res->count_encoders >= encoder_count) {
1957 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1958 drm_for_each_encoder(encoder, dev) {
1959 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1961 if (put_user(encoder->base.id, encoder_id +
1969 card_res->count_encoders = encoder_count;
1972 if (card_res->count_connectors >= connector_count) {
1974 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1975 drm_for_each_connector(connector, dev) {
1976 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1979 if (put_user(connector->base.id,
1980 connector_id + copied)) {
1987 card_res->count_connectors = connector_count;
1989 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1990 card_res->count_connectors, card_res->count_encoders);
1993 mutex_unlock(&dev->mode_config.mutex);
1998 * drm_mode_getcrtc - get CRTC configuration
1999 * @dev: drm device for the ioctl
2000 * @data: data pointer for the ioctl
2001 * @file_priv: drm file for the ioctl call
2003 * Construct a CRTC configuration structure to return to the user.
2005 * Called by the user via ioctl.
2008 * Zero on success, negative errno on failure.
2010 int drm_mode_getcrtc(struct drm_device *dev,
2011 void *data, struct drm_file *file_priv)
2013 struct drm_mode_crtc *crtc_resp = data;
2014 struct drm_crtc *crtc;
2016 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2019 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2023 drm_modeset_lock_crtc(crtc, crtc->primary);
2024 crtc_resp->gamma_size = crtc->gamma_size;
2025 if (crtc->primary->fb)
2026 crtc_resp->fb_id = crtc->primary->fb->base.id;
2028 crtc_resp->fb_id = 0;
2031 crtc_resp->x = crtc->primary->state->src_x >> 16;
2032 crtc_resp->y = crtc->primary->state->src_y >> 16;
2033 if (crtc->state->enable) {
2034 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2035 crtc_resp->mode_valid = 1;
2038 crtc_resp->mode_valid = 0;
2041 crtc_resp->x = crtc->x;
2042 crtc_resp->y = crtc->y;
2043 if (crtc->enabled) {
2044 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2045 crtc_resp->mode_valid = 1;
2048 crtc_resp->mode_valid = 0;
2051 drm_modeset_unlock_crtc(crtc);
2056 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2057 const struct drm_file *file_priv)
2060 * If user-space hasn't configured the driver to expose the stereo 3D
2061 * modes, don't expose them.
2063 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2069 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2071 /* For atomic drivers only state objects are synchronously updated and
2072 * protected by modeset locks, so check those first. */
2073 if (connector->state)
2074 return connector->state->best_encoder;
2075 return connector->encoder;
2078 /* helper for getconnector and getproperties ioctls */
2079 static int get_properties(struct drm_mode_object *obj, bool atomic,
2080 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2081 uint32_t *arg_count_props)
2086 props_count = obj->properties->count;
2088 props_count -= obj->properties->atomic_count;
2090 if ((*arg_count_props >= props_count) && props_count) {
2091 for (i = 0, copied = 0; copied < props_count; i++) {
2092 struct drm_property *prop = obj->properties->properties[i];
2095 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2098 ret = drm_object_property_get_value(obj, prop, &val);
2102 if (put_user(prop->base.id, prop_ptr + copied))
2105 if (put_user(val, prop_values + copied))
2111 *arg_count_props = props_count;
2117 * drm_mode_getconnector - get connector configuration
2118 * @dev: drm device for the ioctl
2119 * @data: data pointer for the ioctl
2120 * @file_priv: drm file for the ioctl call
2122 * Construct a connector configuration structure to return to the user.
2124 * Called by the user via ioctl.
2127 * Zero on success, negative errno on failure.
2129 int drm_mode_getconnector(struct drm_device *dev, void *data,
2130 struct drm_file *file_priv)
2132 struct drm_mode_get_connector *out_resp = data;
2133 struct drm_connector *connector;
2134 struct drm_encoder *encoder;
2135 struct drm_display_mode *mode;
2137 int encoders_count = 0;
2141 struct drm_mode_modeinfo u_mode;
2142 struct drm_mode_modeinfo __user *mode_ptr;
2143 uint32_t __user *encoder_ptr;
2145 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2148 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2150 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2152 mutex_lock(&dev->mode_config.mutex);
2154 connector = drm_connector_find(dev, out_resp->connector_id);
2160 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2161 if (connector->encoder_ids[i] != 0)
2164 if (out_resp->count_modes == 0) {
2165 connector->funcs->fill_modes(connector,
2166 dev->mode_config.max_width,
2167 dev->mode_config.max_height);
2170 /* delayed so we get modes regardless of pre-fill_modes state */
2171 list_for_each_entry(mode, &connector->modes, head)
2172 if (drm_mode_expose_to_userspace(mode, file_priv))
2175 out_resp->connector_id = connector->base.id;
2176 out_resp->connector_type = connector->connector_type;
2177 out_resp->connector_type_id = connector->connector_type_id;
2178 out_resp->mm_width = connector->display_info.width_mm;
2179 out_resp->mm_height = connector->display_info.height_mm;
2180 out_resp->subpixel = connector->display_info.subpixel_order;
2181 out_resp->connection = connector->status;
2183 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2184 encoder = drm_connector_get_encoder(connector);
2186 out_resp->encoder_id = encoder->base.id;
2188 out_resp->encoder_id = 0;
2191 * This ioctl is called twice, once to determine how much space is
2192 * needed, and the 2nd time to fill it.
2194 if ((out_resp->count_modes >= mode_count) && mode_count) {
2196 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2197 list_for_each_entry(mode, &connector->modes, head) {
2198 if (!drm_mode_expose_to_userspace(mode, file_priv))
2201 drm_mode_convert_to_umode(&u_mode, mode);
2202 if (copy_to_user(mode_ptr + copied,
2203 &u_mode, sizeof(u_mode))) {
2210 out_resp->count_modes = mode_count;
2212 ret = get_properties(&connector->base, file_priv->atomic,
2213 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2214 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2215 &out_resp->count_props);
2219 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2221 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2222 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2223 if (connector->encoder_ids[i] != 0) {
2224 if (put_user(connector->encoder_ids[i],
2225 encoder_ptr + copied)) {
2233 out_resp->count_encoders = encoders_count;
2236 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2239 mutex_unlock(&dev->mode_config.mutex);
2244 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2246 struct drm_connector *connector;
2247 struct drm_device *dev = encoder->dev;
2248 bool uses_atomic = false;
2250 /* For atomic drivers only state objects are synchronously updated and
2251 * protected by modeset locks, so check those first. */
2252 drm_for_each_connector(connector, dev) {
2253 if (!connector->state)
2258 if (connector->state->best_encoder != encoder)
2261 return connector->state->crtc;
2264 /* Don't return stale data (e.g. pending async disable). */
2268 return encoder->crtc;
2272 * drm_mode_getencoder - get encoder configuration
2273 * @dev: drm device for the ioctl
2274 * @data: data pointer for the ioctl
2275 * @file_priv: drm file for the ioctl call
2277 * Construct a encoder configuration structure to return to the user.
2279 * Called by the user via ioctl.
2282 * Zero on success, negative errno on failure.
2284 int drm_mode_getencoder(struct drm_device *dev, void *data,
2285 struct drm_file *file_priv)
2287 struct drm_mode_get_encoder *enc_resp = data;
2288 struct drm_encoder *encoder;
2289 struct drm_crtc *crtc;
2291 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2294 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2298 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2299 crtc = drm_encoder_get_crtc(encoder);
2301 enc_resp->crtc_id = crtc->base.id;
2303 enc_resp->crtc_id = 0;
2304 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2306 enc_resp->encoder_type = encoder->encoder_type;
2307 enc_resp->encoder_id = encoder->base.id;
2308 enc_resp->possible_crtcs = encoder->possible_crtcs;
2309 enc_resp->possible_clones = encoder->possible_clones;
2315 * drm_mode_getplane_res - enumerate all plane resources
2318 * @file_priv: DRM file info
2320 * Construct a list of plane ids to return to the user.
2322 * Called by the user via ioctl.
2325 * Zero on success, negative errno on failure.
2327 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2328 struct drm_file *file_priv)
2330 struct drm_mode_get_plane_res *plane_resp = data;
2331 struct drm_mode_config *config;
2332 struct drm_plane *plane;
2333 uint32_t __user *plane_ptr;
2335 unsigned num_planes;
2337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 config = &dev->mode_config;
2342 if (file_priv->universal_planes)
2343 num_planes = config->num_total_plane;
2345 num_planes = config->num_overlay_plane;
2348 * This ioctl is called twice, once to determine how much space is
2349 * needed, and the 2nd time to fill it.
2352 (plane_resp->count_planes >= num_planes)) {
2353 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2355 /* Plane lists are invariant, no locking needed. */
2356 drm_for_each_plane(plane, dev) {
2358 * Unless userspace set the 'universal planes'
2359 * capability bit, only advertise overlays.
2361 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2362 !file_priv->universal_planes)
2365 if (put_user(plane->base.id, plane_ptr + copied))
2370 plane_resp->count_planes = num_planes;
2376 * drm_mode_getplane - get plane configuration
2379 * @file_priv: DRM file info
2381 * Construct a plane configuration structure to return to the user.
2383 * Called by the user via ioctl.
2386 * Zero on success, negative errno on failure.
2388 int drm_mode_getplane(struct drm_device *dev, void *data,
2389 struct drm_file *file_priv)
2391 struct drm_mode_get_plane *plane_resp = data;
2392 struct drm_plane *plane;
2393 uint32_t __user *format_ptr;
2395 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2398 plane = drm_plane_find(dev, plane_resp->plane_id);
2402 drm_modeset_lock(&plane->mutex, NULL);
2404 plane_resp->crtc_id = plane->crtc->base.id;
2406 plane_resp->crtc_id = 0;
2409 plane_resp->fb_id = plane->fb->base.id;
2411 plane_resp->fb_id = 0;
2412 drm_modeset_unlock(&plane->mutex);
2414 plane_resp->plane_id = plane->base.id;
2415 plane_resp->possible_crtcs = plane->possible_crtcs;
2416 plane_resp->gamma_size = 0;
2419 * This ioctl is called twice, once to determine how much space is
2420 * needed, and the 2nd time to fill it.
2422 if (plane->format_count &&
2423 (plane_resp->count_format_types >= plane->format_count)) {
2424 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2425 if (copy_to_user(format_ptr,
2426 plane->format_types,
2427 sizeof(uint32_t) * plane->format_count)) {
2431 plane_resp->count_format_types = plane->format_count;
2437 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2438 * @plane: plane to check for format support
2439 * @format: the pixel format
2442 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2445 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2449 for (i = 0; i < plane->format_count; i++) {
2450 if (format == plane->format_types[i])
2457 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2458 uint32_t src_w, uint32_t src_h,
2459 const struct drm_framebuffer *fb)
2461 unsigned int fb_width, fb_height;
2463 fb_width = fb->width << 16;
2464 fb_height = fb->height << 16;
2466 /* Make sure source coordinates are inside the fb. */
2467 if (src_w > fb_width ||
2468 src_x > fb_width - src_w ||
2469 src_h > fb_height ||
2470 src_y > fb_height - src_h) {
2471 DRM_DEBUG_KMS("Invalid source coordinates "
2472 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2473 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2474 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2475 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2476 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2484 * setplane_internal - setplane handler for internal callers
2486 * Note that we assume an extra reference has already been taken on fb. If the
2487 * update fails, this reference will be dropped before return; if it succeeds,
2488 * the previous framebuffer (if any) will be unreferenced instead.
2490 * src_{x,y,w,h} are provided in 16.16 fixed point format
2492 static int __setplane_internal(struct drm_plane *plane,
2493 struct drm_crtc *crtc,
2494 struct drm_framebuffer *fb,
2495 int32_t crtc_x, int32_t crtc_y,
2496 uint32_t crtc_w, uint32_t crtc_h,
2497 /* src_{x,y,w,h} values are 16.16 fixed point */
2498 uint32_t src_x, uint32_t src_y,
2499 uint32_t src_w, uint32_t src_h)
2503 /* No fb means shut it down */
2505 plane->old_fb = plane->fb;
2506 ret = plane->funcs->disable_plane(plane);
2511 plane->old_fb = NULL;
2516 /* Check whether this plane is usable on this CRTC */
2517 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2518 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2523 /* Check whether this plane supports the fb pixel format. */
2524 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2526 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2527 drm_get_format_name(fb->pixel_format));
2531 /* Give drivers some help against integer overflows */
2532 if (crtc_w > INT_MAX ||
2533 crtc_x > INT_MAX - (int32_t) crtc_w ||
2535 crtc_y > INT_MAX - (int32_t) crtc_h) {
2536 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2537 crtc_w, crtc_h, crtc_x, crtc_y);
2542 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2546 plane->old_fb = plane->fb;
2547 ret = plane->funcs->update_plane(plane, crtc, fb,
2548 crtc_x, crtc_y, crtc_w, crtc_h,
2549 src_x, src_y, src_w, src_h);
2555 plane->old_fb = NULL;
2560 drm_framebuffer_unreference(fb);
2562 drm_framebuffer_unreference(plane->old_fb);
2563 plane->old_fb = NULL;
2568 static int setplane_internal(struct drm_plane *plane,
2569 struct drm_crtc *crtc,
2570 struct drm_framebuffer *fb,
2571 int32_t crtc_x, int32_t crtc_y,
2572 uint32_t crtc_w, uint32_t crtc_h,
2573 /* src_{x,y,w,h} values are 16.16 fixed point */
2574 uint32_t src_x, uint32_t src_y,
2575 uint32_t src_w, uint32_t src_h)
2579 drm_modeset_lock_all(plane->dev);
2580 ret = __setplane_internal(plane, crtc, fb,
2581 crtc_x, crtc_y, crtc_w, crtc_h,
2582 src_x, src_y, src_w, src_h);
2583 drm_modeset_unlock_all(plane->dev);
2589 * drm_mode_setplane - configure a plane's configuration
2591 * @data: ioctl data*
2592 * @file_priv: DRM file info
2594 * Set plane configuration, including placement, fb, scaling, and other factors.
2595 * Or pass a NULL fb to disable (planes may be disabled without providing a
2599 * Zero on success, negative errno on failure.
2601 int drm_mode_setplane(struct drm_device *dev, void *data,
2602 struct drm_file *file_priv)
2604 struct drm_mode_set_plane *plane_req = data;
2605 struct drm_plane *plane;
2606 struct drm_crtc *crtc = NULL;
2607 struct drm_framebuffer *fb = NULL;
2609 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2613 * First, find the plane, crtc, and fb objects. If not available,
2614 * we don't bother to call the driver.
2616 plane = drm_plane_find(dev, plane_req->plane_id);
2618 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2619 plane_req->plane_id);
2623 if (plane_req->fb_id) {
2624 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2626 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2631 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2633 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2634 plane_req->crtc_id);
2640 * setplane_internal will take care of deref'ing either the old or new
2641 * framebuffer depending on success.
2643 return setplane_internal(plane, crtc, fb,
2644 plane_req->crtc_x, plane_req->crtc_y,
2645 plane_req->crtc_w, plane_req->crtc_h,
2646 plane_req->src_x, plane_req->src_y,
2647 plane_req->src_w, plane_req->src_h);
2651 * drm_mode_set_config_internal - helper to call ->set_config
2652 * @set: modeset config to set
2654 * This is a little helper to wrap internal calls to the ->set_config driver
2655 * interface. The only thing it adds is correct refcounting dance.
2658 * Zero on success, negative errno on failure.
2660 int drm_mode_set_config_internal(struct drm_mode_set *set)
2662 struct drm_crtc *crtc = set->crtc;
2663 struct drm_framebuffer *fb;
2664 struct drm_crtc *tmp;
2668 * NOTE: ->set_config can also disable other crtcs (if we steal all
2669 * connectors from it), hence we need to refcount the fbs across all
2670 * crtcs. Atomic modeset will have saner semantics ...
2672 drm_for_each_crtc(tmp, crtc->dev)
2673 tmp->primary->old_fb = tmp->primary->fb;
2677 ret = crtc->funcs->set_config(set);
2679 crtc->primary->crtc = crtc;
2680 crtc->primary->fb = fb;
2683 drm_for_each_crtc(tmp, crtc->dev) {
2684 if (tmp->primary->fb)
2685 drm_framebuffer_reference(tmp->primary->fb);
2686 if (tmp->primary->old_fb)
2687 drm_framebuffer_unreference(tmp->primary->old_fb);
2688 tmp->primary->old_fb = NULL;
2693 EXPORT_SYMBOL(drm_mode_set_config_internal);
2696 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2697 * @mode: mode to query
2698 * @hdisplay: hdisplay value to fill in
2699 * @vdisplay: vdisplay value to fill in
2701 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2702 * the appropriate layout.
2704 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2705 int *hdisplay, int *vdisplay)
2707 struct drm_display_mode adjusted;
2709 drm_mode_copy(&adjusted, mode);
2710 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2711 *hdisplay = adjusted.crtc_hdisplay;
2712 *vdisplay = adjusted.crtc_vdisplay;
2714 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2717 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2719 * @crtc: CRTC that framebuffer will be displayed on
2722 * @mode: mode that framebuffer will be displayed under
2723 * @fb: framebuffer to check size of
2725 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2727 const struct drm_display_mode *mode,
2728 const struct drm_framebuffer *fb)
2731 int hdisplay, vdisplay;
2733 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2736 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2737 BIT(DRM_ROTATE_270)))
2738 swap(hdisplay, vdisplay);
2740 return check_src_coords(x << 16, y << 16,
2741 hdisplay << 16, vdisplay << 16, fb);
2743 EXPORT_SYMBOL(drm_crtc_check_viewport);
2746 * drm_mode_setcrtc - set CRTC configuration
2747 * @dev: drm device for the ioctl
2748 * @data: data pointer for the ioctl
2749 * @file_priv: drm file for the ioctl call
2751 * Build a new CRTC configuration based on user request.
2753 * Called by the user via ioctl.
2756 * Zero on success, negative errno on failure.
2758 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2759 struct drm_file *file_priv)
2761 struct drm_mode_config *config = &dev->mode_config;
2762 struct drm_mode_crtc *crtc_req = data;
2763 struct drm_crtc *crtc;
2764 struct drm_connector **connector_set = NULL, *connector;
2765 struct drm_framebuffer *fb = NULL;
2766 struct drm_display_mode *mode = NULL;
2767 struct drm_mode_set set;
2768 uint32_t __user *set_connectors_ptr;
2772 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2776 * Universal plane src offsets are only 16.16, prevent havoc for
2777 * drivers using universal plane code internally.
2779 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2782 drm_modeset_lock_all(dev);
2783 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2785 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2789 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2791 if (crtc_req->mode_valid) {
2792 /* If we have a mode we need a framebuffer. */
2793 /* If we pass -1, set the mode with the currently bound fb */
2794 if (crtc_req->fb_id == -1) {
2795 if (!crtc->primary->fb) {
2796 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2800 fb = crtc->primary->fb;
2801 /* Make refcounting symmetric with the lookup path. */
2802 drm_framebuffer_reference(fb);
2804 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2806 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2813 mode = drm_mode_create(dev);
2819 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2821 DRM_DEBUG_KMS("Invalid mode\n");
2825 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2828 * Check whether the primary plane supports the fb pixel format.
2829 * Drivers not implementing the universal planes API use a
2830 * default formats list provided by the DRM core which doesn't
2831 * match real hardware capabilities. Skip the check in that
2834 if (!crtc->primary->format_default) {
2835 ret = drm_plane_check_pixel_format(crtc->primary,
2838 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2839 drm_get_format_name(fb->pixel_format));
2844 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2851 if (crtc_req->count_connectors == 0 && mode) {
2852 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2857 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2858 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2859 crtc_req->count_connectors);
2864 if (crtc_req->count_connectors > 0) {
2867 /* Avoid unbounded kernel memory allocation */
2868 if (crtc_req->count_connectors > config->num_connector) {
2873 connector_set = kmalloc_array(crtc_req->count_connectors,
2874 sizeof(struct drm_connector *),
2876 if (!connector_set) {
2881 for (i = 0; i < crtc_req->count_connectors; i++) {
2882 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2883 if (get_user(out_id, &set_connectors_ptr[i])) {
2888 connector = drm_connector_find(dev, out_id);
2890 DRM_DEBUG_KMS("Connector id %d unknown\n",
2895 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2899 connector_set[i] = connector;
2904 set.x = crtc_req->x;
2905 set.y = crtc_req->y;
2907 set.connectors = connector_set;
2908 set.num_connectors = crtc_req->count_connectors;
2910 ret = drm_mode_set_config_internal(&set);
2914 drm_framebuffer_unreference(fb);
2916 kfree(connector_set);
2917 drm_mode_destroy(dev, mode);
2918 drm_modeset_unlock_all(dev);
2923 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2924 * universal plane handler call
2925 * @crtc: crtc to update cursor for
2926 * @req: data pointer for the ioctl
2927 * @file_priv: drm file for the ioctl call
2929 * Legacy cursor ioctl's work directly with driver buffer handles. To
2930 * translate legacy ioctl calls into universal plane handler calls, we need to
2931 * wrap the native buffer handle in a drm_framebuffer.
2933 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2934 * buffer with a pitch of 4*width; the universal plane interface should be used
2935 * directly in cases where the hardware can support other buffer settings and
2936 * userspace wants to make use of these capabilities.
2939 * Zero on success, negative errno on failure.
2941 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2942 struct drm_mode_cursor2 *req,
2943 struct drm_file *file_priv)
2945 struct drm_device *dev = crtc->dev;
2946 struct drm_framebuffer *fb = NULL;
2947 struct drm_mode_fb_cmd2 fbreq = {
2948 .width = req->width,
2949 .height = req->height,
2950 .pixel_format = DRM_FORMAT_ARGB8888,
2951 .pitches = { req->width * 4 },
2952 .handles = { req->handle },
2954 int32_t crtc_x, crtc_y;
2955 uint32_t crtc_w = 0, crtc_h = 0;
2956 uint32_t src_w = 0, src_h = 0;
2959 BUG_ON(!crtc->cursor);
2960 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2963 * Obtain fb we'll be using (either new or existing) and take an extra
2964 * reference to it if fb != null. setplane will take care of dropping
2965 * the reference if the plane update fails.
2967 if (req->flags & DRM_MODE_CURSOR_BO) {
2969 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2971 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2978 fb = crtc->cursor->fb;
2980 drm_framebuffer_reference(fb);
2983 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2987 crtc_x = crtc->cursor_x;
2988 crtc_y = crtc->cursor_y;
2993 crtc_h = fb->height;
2994 src_w = fb->width << 16;
2995 src_h = fb->height << 16;
2999 * setplane_internal will take care of deref'ing either the old or new
3000 * framebuffer depending on success.
3002 ret = __setplane_internal(crtc->cursor, crtc, fb,
3003 crtc_x, crtc_y, crtc_w, crtc_h,
3004 0, 0, src_w, src_h);
3006 /* Update successful; save new cursor position, if necessary */
3007 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3008 crtc->cursor_x = req->x;
3009 crtc->cursor_y = req->y;
3015 static int drm_mode_cursor_common(struct drm_device *dev,
3016 struct drm_mode_cursor2 *req,
3017 struct drm_file *file_priv)
3019 struct drm_crtc *crtc;
3022 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3025 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3028 crtc = drm_crtc_find(dev, req->crtc_id);
3030 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3035 * If this crtc has a universal cursor plane, call that plane's update
3036 * handler rather than using legacy cursor handlers.
3038 drm_modeset_lock_crtc(crtc, crtc->cursor);
3040 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3044 if (req->flags & DRM_MODE_CURSOR_BO) {
3045 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3049 /* Turns off the cursor if handle is 0 */
3050 if (crtc->funcs->cursor_set2)
3051 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3052 req->width, req->height, req->hot_x, req->hot_y);
3054 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3055 req->width, req->height);
3058 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3059 if (crtc->funcs->cursor_move) {
3060 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3067 drm_modeset_unlock_crtc(crtc);
3075 * drm_mode_cursor_ioctl - set CRTC's cursor 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 * Set the cursor configuration based on user request.
3082 * Called by the user via ioctl.
3085 * Zero on success, negative errno on failure.
3087 int drm_mode_cursor_ioctl(struct drm_device *dev,
3088 void *data, struct drm_file *file_priv)
3090 struct drm_mode_cursor *req = data;
3091 struct drm_mode_cursor2 new_req;
3093 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3094 new_req.hot_x = new_req.hot_y = 0;
3096 return drm_mode_cursor_common(dev, &new_req, file_priv);
3100 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3101 * @dev: drm device for the ioctl
3102 * @data: data pointer for the ioctl
3103 * @file_priv: drm file for the ioctl call
3105 * Set the cursor configuration based on user request. This implements the 2nd
3106 * version of the cursor ioctl, which allows userspace to additionally specify
3107 * the hotspot of the pointer.
3109 * Called by the user via ioctl.
3112 * Zero on success, negative errno on failure.
3114 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3115 void *data, struct drm_file *file_priv)
3117 struct drm_mode_cursor2 *req = data;
3119 return drm_mode_cursor_common(dev, req, file_priv);
3123 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3124 * @bpp: bits per pixels
3125 * @depth: bit depth per pixel
3127 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3128 * Useful in fbdev emulation code, since that deals in those values.
3130 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3136 fmt = DRM_FORMAT_C8;
3140 fmt = DRM_FORMAT_XRGB1555;
3142 fmt = DRM_FORMAT_RGB565;
3145 fmt = DRM_FORMAT_RGB888;
3149 fmt = DRM_FORMAT_XRGB8888;
3150 else if (depth == 30)
3151 fmt = DRM_FORMAT_XRGB2101010;
3153 fmt = DRM_FORMAT_ARGB8888;
3156 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3157 fmt = DRM_FORMAT_XRGB8888;
3163 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3166 * drm_mode_addfb - add an FB to the graphics configuration
3167 * @dev: drm device for the ioctl
3168 * @data: data pointer for the ioctl
3169 * @file_priv: drm file for the ioctl call
3171 * Add a new FB to the specified CRTC, given a user request. This is the
3172 * original addfb ioctl which only supported RGB formats.
3174 * Called by the user via ioctl.
3177 * Zero on success, negative errno on failure.
3179 int drm_mode_addfb(struct drm_device *dev,
3180 void *data, struct drm_file *file_priv)
3182 struct drm_mode_fb_cmd *or = data;
3183 struct drm_mode_fb_cmd2 r = {};
3186 /* convert to new format and call new ioctl */
3187 r.fb_id = or->fb_id;
3188 r.width = or->width;
3189 r.height = or->height;
3190 r.pitches[0] = or->pitch;
3191 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3192 r.handles[0] = or->handle;
3194 ret = drm_mode_addfb2(dev, &r, file_priv);
3198 or->fb_id = r.fb_id;
3203 static int format_check(const struct drm_mode_fb_cmd2 *r)
3205 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3209 case DRM_FORMAT_RGB332:
3210 case DRM_FORMAT_BGR233:
3211 case DRM_FORMAT_XRGB4444:
3212 case DRM_FORMAT_XBGR4444:
3213 case DRM_FORMAT_RGBX4444:
3214 case DRM_FORMAT_BGRX4444:
3215 case DRM_FORMAT_ARGB4444:
3216 case DRM_FORMAT_ABGR4444:
3217 case DRM_FORMAT_RGBA4444:
3218 case DRM_FORMAT_BGRA4444:
3219 case DRM_FORMAT_XRGB1555:
3220 case DRM_FORMAT_XBGR1555:
3221 case DRM_FORMAT_RGBX5551:
3222 case DRM_FORMAT_BGRX5551:
3223 case DRM_FORMAT_ARGB1555:
3224 case DRM_FORMAT_ABGR1555:
3225 case DRM_FORMAT_RGBA5551:
3226 case DRM_FORMAT_BGRA5551:
3227 case DRM_FORMAT_RGB565:
3228 case DRM_FORMAT_BGR565:
3229 case DRM_FORMAT_RGB888:
3230 case DRM_FORMAT_BGR888:
3231 case DRM_FORMAT_XRGB8888:
3232 case DRM_FORMAT_XBGR8888:
3233 case DRM_FORMAT_RGBX8888:
3234 case DRM_FORMAT_BGRX8888:
3235 case DRM_FORMAT_ARGB8888:
3236 case DRM_FORMAT_ABGR8888:
3237 case DRM_FORMAT_RGBA8888:
3238 case DRM_FORMAT_BGRA8888:
3239 case DRM_FORMAT_XRGB2101010:
3240 case DRM_FORMAT_XBGR2101010:
3241 case DRM_FORMAT_RGBX1010102:
3242 case DRM_FORMAT_BGRX1010102:
3243 case DRM_FORMAT_ARGB2101010:
3244 case DRM_FORMAT_ABGR2101010:
3245 case DRM_FORMAT_RGBA1010102:
3246 case DRM_FORMAT_BGRA1010102:
3247 case DRM_FORMAT_YUYV:
3248 case DRM_FORMAT_YVYU:
3249 case DRM_FORMAT_UYVY:
3250 case DRM_FORMAT_VYUY:
3251 case DRM_FORMAT_AYUV:
3252 case DRM_FORMAT_NV12:
3253 case DRM_FORMAT_NV21:
3254 case DRM_FORMAT_NV16:
3255 case DRM_FORMAT_NV61:
3256 case DRM_FORMAT_NV24:
3257 case DRM_FORMAT_NV42:
3258 case DRM_FORMAT_YUV410:
3259 case DRM_FORMAT_YVU410:
3260 case DRM_FORMAT_YUV411:
3261 case DRM_FORMAT_YVU411:
3262 case DRM_FORMAT_YUV420:
3263 case DRM_FORMAT_YVU420:
3264 case DRM_FORMAT_YUV422:
3265 case DRM_FORMAT_YVU422:
3266 case DRM_FORMAT_YUV444:
3267 case DRM_FORMAT_YVU444:
3270 DRM_DEBUG_KMS("invalid pixel format %s\n",
3271 drm_get_format_name(r->pixel_format));
3276 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3278 int ret, hsub, vsub, num_planes, i;
3280 ret = format_check(r);
3282 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3283 drm_get_format_name(r->pixel_format));
3287 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3288 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3289 num_planes = drm_format_num_planes(r->pixel_format);
3291 if (r->width == 0 || r->width % hsub) {
3292 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3296 if (r->height == 0 || r->height % vsub) {
3297 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3301 for (i = 0; i < num_planes; i++) {
3302 unsigned int width = r->width / (i != 0 ? hsub : 1);
3303 unsigned int height = r->height / (i != 0 ? vsub : 1);
3304 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3306 if (!r->handles[i]) {
3307 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3311 if ((uint64_t) width * cpp > UINT_MAX)
3314 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3317 if (r->pitches[i] < width * cpp) {
3318 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3322 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3323 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3328 /* modifier specific checks: */
3329 switch (r->modifier[i]) {
3330 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3331 /* NOTE: the pitch restriction may be lifted later if it turns
3332 * out that no hw has this restriction:
3334 if (r->pixel_format != DRM_FORMAT_NV12 ||
3335 width % 128 || height % 32 ||
3336 r->pitches[i] % 128) {
3337 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3347 for (i = num_planes; i < 4; i++) {
3348 if (r->modifier[i]) {
3349 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3353 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3354 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3357 if (r->handles[i]) {
3358 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3362 if (r->pitches[i]) {
3363 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3367 if (r->offsets[i]) {
3368 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3376 static struct drm_framebuffer *
3377 internal_framebuffer_create(struct drm_device *dev,
3378 const struct drm_mode_fb_cmd2 *r,
3379 struct drm_file *file_priv)
3381 struct drm_mode_config *config = &dev->mode_config;
3382 struct drm_framebuffer *fb;
3385 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3386 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3387 return ERR_PTR(-EINVAL);
3390 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3391 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3392 r->width, config->min_width, config->max_width);
3393 return ERR_PTR(-EINVAL);
3395 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3396 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3397 r->height, config->min_height, config->max_height);
3398 return ERR_PTR(-EINVAL);
3401 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3402 !dev->mode_config.allow_fb_modifiers) {
3403 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3404 return ERR_PTR(-EINVAL);
3407 ret = framebuffer_check(r);
3409 return ERR_PTR(ret);
3411 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3413 DRM_DEBUG_KMS("could not create framebuffer\n");
3421 * drm_mode_addfb2 - add an FB to the graphics configuration
3422 * @dev: drm device for the ioctl
3423 * @data: data pointer for the ioctl
3424 * @file_priv: drm file for the ioctl call
3426 * Add a new FB to the specified CRTC, given a user request with format. This is
3427 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3428 * and uses fourcc codes as pixel format specifiers.
3430 * Called by the user via ioctl.
3433 * Zero on success, negative errno on failure.
3435 int drm_mode_addfb2(struct drm_device *dev,
3436 void *data, struct drm_file *file_priv)
3438 struct drm_mode_fb_cmd2 *r = data;
3439 struct drm_framebuffer *fb;
3441 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3444 fb = internal_framebuffer_create(dev, r, file_priv);
3448 /* Transfer ownership to the filp for reaping on close */
3450 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3451 mutex_lock(&file_priv->fbs_lock);
3452 r->fb_id = fb->base.id;
3453 list_add(&fb->filp_head, &file_priv->fbs);
3454 mutex_unlock(&file_priv->fbs_lock);
3460 * drm_mode_rmfb - remove an FB from the configuration
3461 * @dev: drm device for the ioctl
3462 * @data: data pointer for the ioctl
3463 * @file_priv: drm file for the ioctl call
3465 * Remove the FB specified by the user.
3467 * Called by the user via ioctl.
3470 * Zero on success, negative errno on failure.
3472 int drm_mode_rmfb(struct drm_device *dev,
3473 void *data, struct drm_file *file_priv)
3475 struct drm_framebuffer *fb = NULL;
3476 struct drm_framebuffer *fbl = NULL;
3477 struct drm_mode_object *obj;
3478 uint32_t *id = data;
3481 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3484 mutex_lock(&file_priv->fbs_lock);
3485 mutex_lock(&dev->mode_config.fb_lock);
3486 obj = _object_find(dev, *id, DRM_MODE_OBJECT_FB);
3489 fb = obj_to_fb(obj);
3490 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3496 list_del_init(&fb->filp_head);
3497 mutex_unlock(&dev->mode_config.fb_lock);
3498 mutex_unlock(&file_priv->fbs_lock);
3500 drm_framebuffer_unreference(fb);
3505 mutex_unlock(&dev->mode_config.fb_lock);
3506 mutex_unlock(&file_priv->fbs_lock);
3512 * drm_mode_getfb - get FB info
3513 * @dev: drm device for the ioctl
3514 * @data: data pointer for the ioctl
3515 * @file_priv: drm file for the ioctl call
3517 * Lookup the FB given its ID and return info about it.
3519 * Called by the user via ioctl.
3522 * Zero on success, negative errno on failure.
3524 int drm_mode_getfb(struct drm_device *dev,
3525 void *data, struct drm_file *file_priv)
3527 struct drm_mode_fb_cmd *r = data;
3528 struct drm_framebuffer *fb;
3531 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3534 fb = drm_framebuffer_lookup(dev, r->fb_id);
3538 r->height = fb->height;
3539 r->width = fb->width;
3540 r->depth = fb->depth;
3541 r->bpp = fb->bits_per_pixel;
3542 r->pitch = fb->pitches[0];
3543 if (fb->funcs->create_handle) {
3544 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3545 drm_is_control_client(file_priv)) {
3546 ret = fb->funcs->create_handle(fb, file_priv,
3549 /* GET_FB() is an unprivileged ioctl so we must not
3550 * return a buffer-handle to non-master processes! For
3551 * backwards-compatibility reasons, we cannot make
3552 * GET_FB() privileged, so just return an invalid handle
3553 * for non-masters. */
3561 drm_framebuffer_unreference(fb);
3567 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3568 * @dev: drm device for the ioctl
3569 * @data: data pointer for the ioctl
3570 * @file_priv: drm file for the ioctl call
3572 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3573 * rectangle list. Generic userspace which does frontbuffer rendering must call
3574 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3575 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3577 * Modesetting drivers which always update the frontbuffer do not need to
3578 * implement the corresponding ->dirty framebuffer callback.
3580 * Called by the user via ioctl.
3583 * Zero on success, negative errno on failure.
3585 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3586 void *data, struct drm_file *file_priv)
3588 struct drm_clip_rect __user *clips_ptr;
3589 struct drm_clip_rect *clips = NULL;
3590 struct drm_mode_fb_dirty_cmd *r = data;
3591 struct drm_framebuffer *fb;
3596 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3599 fb = drm_framebuffer_lookup(dev, r->fb_id);
3603 num_clips = r->num_clips;
3604 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3606 if (!num_clips != !clips_ptr) {
3611 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3613 /* If userspace annotates copy, clips must come in pairs */
3614 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3619 if (num_clips && clips_ptr) {
3620 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3624 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3630 ret = copy_from_user(clips, clips_ptr,
3631 num_clips * sizeof(*clips));
3638 if (fb->funcs->dirty) {
3639 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3648 drm_framebuffer_unreference(fb);
3655 * drm_fb_release - remove and free the FBs on this file
3656 * @priv: drm file for the ioctl
3658 * Destroy all the FBs associated with @filp.
3660 * Called by the user via ioctl.
3663 * Zero on success, negative errno on failure.
3665 void drm_fb_release(struct drm_file *priv)
3667 struct drm_framebuffer *fb, *tfb;
3670 * When the file gets released that means no one else can access the fb
3671 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3672 * avoid upsetting lockdep since the universal cursor code adds a
3673 * framebuffer while holding mutex locks.
3675 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3676 * locks is impossible here since no one else but this function can get
3679 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3680 list_del_init(&fb->filp_head);
3682 /* This drops the fpriv->fbs reference. */
3683 drm_framebuffer_unreference(fb);
3688 * drm_property_create - create a new property type
3690 * @flags: flags specifying the property type
3691 * @name: name of the property
3692 * @num_values: number of pre-defined values
3694 * This creates a new generic drm property which can then be attached to a drm
3695 * object with drm_object_attach_property. The returned property object must be
3696 * freed with drm_property_destroy.
3698 * Note that the DRM core keeps a per-device list of properties and that, if
3699 * drm_mode_config_cleanup() is called, it will destroy all properties created
3703 * A pointer to the newly created property on success, NULL on failure.
3705 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3706 const char *name, int num_values)
3708 struct drm_property *property = NULL;
3711 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3715 property->dev = dev;
3718 property->values = kcalloc(num_values, sizeof(uint64_t),
3720 if (!property->values)
3724 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3728 property->flags = flags;
3729 property->num_values = num_values;
3730 INIT_LIST_HEAD(&property->enum_list);
3733 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3734 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3737 list_add_tail(&property->head, &dev->mode_config.property_list);
3739 WARN_ON(!drm_property_type_valid(property));
3743 kfree(property->values);
3747 EXPORT_SYMBOL(drm_property_create);
3750 * drm_property_create_enum - create a new enumeration property type
3752 * @flags: flags specifying the property type
3753 * @name: name of the property
3754 * @props: enumeration lists with property values
3755 * @num_values: number of pre-defined values
3757 * This creates a new generic drm property which can then be attached to a drm
3758 * object with drm_object_attach_property. The returned property object must be
3759 * freed with drm_property_destroy.
3761 * Userspace is only allowed to set one of the predefined values for enumeration
3765 * A pointer to the newly created property on success, NULL on failure.
3767 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3769 const struct drm_prop_enum_list *props,
3772 struct drm_property *property;
3775 flags |= DRM_MODE_PROP_ENUM;
3777 property = drm_property_create(dev, flags, name, num_values);
3781 for (i = 0; i < num_values; i++) {
3782 ret = drm_property_add_enum(property, i,
3786 drm_property_destroy(dev, property);
3793 EXPORT_SYMBOL(drm_property_create_enum);
3796 * drm_property_create_bitmask - create a new bitmask property type
3798 * @flags: flags specifying the property type
3799 * @name: name of the property
3800 * @props: enumeration lists with property bitflags
3801 * @num_props: size of the @props array
3802 * @supported_bits: bitmask of all supported enumeration values
3804 * This creates a new bitmask drm property which can then be attached to a drm
3805 * object with drm_object_attach_property. The returned property object must be
3806 * freed with drm_property_destroy.
3808 * Compared to plain enumeration properties userspace is allowed to set any
3809 * or'ed together combination of the predefined property bitflag values
3812 * A pointer to the newly created property on success, NULL on failure.
3814 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3815 int flags, const char *name,
3816 const struct drm_prop_enum_list *props,
3818 uint64_t supported_bits)
3820 struct drm_property *property;
3821 int i, ret, index = 0;
3822 int num_values = hweight64(supported_bits);
3824 flags |= DRM_MODE_PROP_BITMASK;
3826 property = drm_property_create(dev, flags, name, num_values);
3829 for (i = 0; i < num_props; i++) {
3830 if (!(supported_bits & (1ULL << props[i].type)))
3833 if (WARN_ON(index >= num_values)) {
3834 drm_property_destroy(dev, property);
3838 ret = drm_property_add_enum(property, index++,
3842 drm_property_destroy(dev, property);
3849 EXPORT_SYMBOL(drm_property_create_bitmask);
3851 static struct drm_property *property_create_range(struct drm_device *dev,
3852 int flags, const char *name,
3853 uint64_t min, uint64_t max)
3855 struct drm_property *property;
3857 property = drm_property_create(dev, flags, name, 2);
3861 property->values[0] = min;
3862 property->values[1] = max;
3868 * drm_property_create_range - create a new unsigned ranged property type
3870 * @flags: flags specifying the property type
3871 * @name: name of the property
3872 * @min: minimum value of the property
3873 * @max: maximum value of the property
3875 * This creates a new generic drm property which can then be attached to a drm
3876 * object with drm_object_attach_property. The returned property object must be
3877 * freed with drm_property_destroy.
3879 * Userspace is allowed to set any unsigned integer value in the (min, max)
3883 * A pointer to the newly created property on success, NULL on failure.
3885 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3887 uint64_t min, uint64_t max)
3889 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3892 EXPORT_SYMBOL(drm_property_create_range);
3895 * drm_property_create_signed_range - create a new signed ranged property type
3897 * @flags: flags specifying the property type
3898 * @name: name of the property
3899 * @min: minimum value of the property
3900 * @max: maximum value of the property
3902 * This creates a new generic drm property which can then be attached to a drm
3903 * object with drm_object_attach_property. The returned property object must be
3904 * freed with drm_property_destroy.
3906 * Userspace is allowed to set any signed integer value in the (min, max)
3910 * A pointer to the newly created property on success, NULL on failure.
3912 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3913 int flags, const char *name,
3914 int64_t min, int64_t max)
3916 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3917 name, I642U64(min), I642U64(max));
3919 EXPORT_SYMBOL(drm_property_create_signed_range);
3922 * drm_property_create_object - create a new object property type
3924 * @flags: flags specifying the property type
3925 * @name: name of the property
3926 * @type: object type from DRM_MODE_OBJECT_* defines
3928 * This creates a new generic drm property which can then be attached to a drm
3929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3932 * Userspace is only allowed to set this to any property value of the given
3933 * @type. Only useful for atomic properties, which is enforced.
3936 * A pointer to the newly created property on success, NULL on failure.
3938 struct drm_property *drm_property_create_object(struct drm_device *dev,
3939 int flags, const char *name, uint32_t type)
3941 struct drm_property *property;
3943 flags |= DRM_MODE_PROP_OBJECT;
3945 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3948 property = drm_property_create(dev, flags, name, 1);
3952 property->values[0] = type;
3956 EXPORT_SYMBOL(drm_property_create_object);
3959 * drm_property_create_bool - create a new boolean property type
3961 * @flags: flags specifying the property type
3962 * @name: name of the property
3964 * This creates a new generic drm property which can then be attached to a drm
3965 * object with drm_object_attach_property. The returned property object must be
3966 * freed with drm_property_destroy.
3968 * This is implemented as a ranged property with only {0, 1} as valid values.
3971 * A pointer to the newly created property on success, NULL on failure.
3973 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3976 return drm_property_create_range(dev, flags, name, 0, 1);
3978 EXPORT_SYMBOL(drm_property_create_bool);
3981 * drm_property_add_enum - add a possible value to an enumeration property
3982 * @property: enumeration property to change
3983 * @index: index of the new enumeration
3984 * @value: value of the new enumeration
3985 * @name: symbolic name of the new enumeration
3987 * This functions adds enumerations to a property.
3989 * It's use is deprecated, drivers should use one of the more specific helpers
3990 * to directly create the property with all enumerations already attached.
3993 * Zero on success, error code on failure.
3995 int drm_property_add_enum(struct drm_property *property, int index,
3996 uint64_t value, const char *name)
3998 struct drm_property_enum *prop_enum;
4000 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4001 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
4005 * Bitmask enum properties have the additional constraint of values
4008 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4012 if (!list_empty(&property->enum_list)) {
4013 list_for_each_entry(prop_enum, &property->enum_list, head) {
4014 if (prop_enum->value == value) {
4015 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4016 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4022 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4026 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4027 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4028 prop_enum->value = value;
4030 property->values[index] = value;
4031 list_add_tail(&prop_enum->head, &property->enum_list);
4034 EXPORT_SYMBOL(drm_property_add_enum);
4037 * drm_property_destroy - destroy a drm property
4039 * @property: property to destry
4041 * This function frees a property including any attached resources like
4042 * enumeration values.
4044 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4046 struct drm_property_enum *prop_enum, *pt;
4048 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4049 list_del(&prop_enum->head);
4053 if (property->num_values)
4054 kfree(property->values);
4055 drm_mode_object_unregister(dev, &property->base);
4056 list_del(&property->head);
4059 EXPORT_SYMBOL(drm_property_destroy);
4062 * drm_object_attach_property - attach a property to a modeset object
4063 * @obj: drm modeset object
4064 * @property: property to attach
4065 * @init_val: initial value of the property
4067 * This attaches the given property to the modeset object with the given initial
4068 * value. Currently this function cannot fail since the properties are stored in
4069 * a statically sized array.
4071 void drm_object_attach_property(struct drm_mode_object *obj,
4072 struct drm_property *property,
4075 int count = obj->properties->count;
4077 if (count == DRM_OBJECT_MAX_PROPERTY) {
4078 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4079 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4080 "you see this message on the same object type.\n",
4085 obj->properties->properties[count] = property;
4086 obj->properties->values[count] = init_val;
4087 obj->properties->count++;
4088 if (property->flags & DRM_MODE_PROP_ATOMIC)
4089 obj->properties->atomic_count++;
4091 EXPORT_SYMBOL(drm_object_attach_property);
4094 * drm_object_property_set_value - set the value of a property
4095 * @obj: drm mode object to set property value for
4096 * @property: property to set
4097 * @val: value the property should be set to
4099 * This functions sets a given property on a given object. This function only
4100 * changes the software state of the property, it does not call into the
4101 * driver's ->set_property callback.
4104 * Zero on success, error code on failure.
4106 int drm_object_property_set_value(struct drm_mode_object *obj,
4107 struct drm_property *property, uint64_t val)
4111 for (i = 0; i < obj->properties->count; i++) {
4112 if (obj->properties->properties[i] == property) {
4113 obj->properties->values[i] = val;
4120 EXPORT_SYMBOL(drm_object_property_set_value);
4123 * drm_object_property_get_value - retrieve the value of a property
4124 * @obj: drm mode object to get property value from
4125 * @property: property to retrieve
4126 * @val: storage for the property value
4128 * This function retrieves the softare state of the given property for the given
4129 * property. Since there is no driver callback to retrieve the current property
4130 * value this might be out of sync with the hardware, depending upon the driver
4134 * Zero on success, error code on failure.
4136 int drm_object_property_get_value(struct drm_mode_object *obj,
4137 struct drm_property *property, uint64_t *val)
4141 /* read-only properties bypass atomic mechanism and still store
4142 * their value in obj->properties->values[].. mostly to avoid
4143 * having to deal w/ EDID and similar props in atomic paths:
4145 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4146 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4147 return drm_atomic_get_property(obj, property, val);
4149 for (i = 0; i < obj->properties->count; i++) {
4150 if (obj->properties->properties[i] == property) {
4151 *val = obj->properties->values[i];
4158 EXPORT_SYMBOL(drm_object_property_get_value);
4161 * drm_mode_getproperty_ioctl - get the property metadata
4164 * @file_priv: DRM file info
4166 * This function retrieves the metadata for a given property, like the different
4167 * possible values for an enum property or the limits for a range property.
4169 * Blob properties are special
4171 * Called by the user via ioctl.
4174 * Zero on success, negative errno on failure.
4176 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4177 void *data, struct drm_file *file_priv)
4179 struct drm_mode_get_property *out_resp = data;
4180 struct drm_property *property;
4182 int value_count = 0;
4185 struct drm_property_enum *prop_enum;
4186 struct drm_mode_property_enum __user *enum_ptr;
4187 uint64_t __user *values_ptr;
4189 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4192 drm_modeset_lock_all(dev);
4193 property = drm_property_find(dev, out_resp->prop_id);
4199 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4200 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4201 list_for_each_entry(prop_enum, &property->enum_list, head)
4205 value_count = property->num_values;
4207 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4208 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4209 out_resp->flags = property->flags;
4211 if ((out_resp->count_values >= value_count) && value_count) {
4212 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4213 for (i = 0; i < value_count; i++) {
4214 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4220 out_resp->count_values = value_count;
4222 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4223 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4224 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4226 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4227 list_for_each_entry(prop_enum, &property->enum_list, head) {
4229 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4234 if (copy_to_user(&enum_ptr[copied].name,
4235 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4242 out_resp->count_enum_blobs = enum_count;
4246 * NOTE: The idea seems to have been to use this to read all the blob
4247 * property values. But nothing ever added them to the corresponding
4248 * list, userspace always used the special-purpose get_blob ioctl to
4249 * read the value for a blob property. It also doesn't make a lot of
4250 * sense to return values here when everything else is just metadata for
4251 * the property itself.
4253 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4254 out_resp->count_enum_blobs = 0;
4256 drm_modeset_unlock_all(dev);
4261 * drm_property_create_blob - Create new blob property
4263 * Creates a new blob property for a specified DRM device, optionally
4266 * @dev: DRM device to create property for
4267 * @length: Length to allocate for blob data
4268 * @data: If specified, copies data into blob
4271 * New blob property with a single reference on success, or an ERR_PTR
4274 struct drm_property_blob *
4275 drm_property_create_blob(struct drm_device *dev, size_t length,
4278 struct drm_property_blob *blob;
4281 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4282 return ERR_PTR(-EINVAL);
4284 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4286 return ERR_PTR(-ENOMEM);
4288 /* This must be explicitly initialised, so we can safely call list_del
4289 * on it in the removal handler, even if it isn't in a file list. */
4290 INIT_LIST_HEAD(&blob->head_file);
4291 blob->length = length;
4295 memcpy(blob->data, data, length);
4297 mutex_lock(&dev->mode_config.blob_lock);
4299 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4302 mutex_unlock(&dev->mode_config.blob_lock);
4303 return ERR_PTR(-EINVAL);
4306 kref_init(&blob->refcount);
4308 list_add_tail(&blob->head_global,
4309 &dev->mode_config.property_blob_list);
4311 mutex_unlock(&dev->mode_config.blob_lock);
4315 EXPORT_SYMBOL(drm_property_create_blob);
4318 * drm_property_free_blob - Blob property destructor
4320 * Internal free function for blob properties; must not be used directly.
4324 static void drm_property_free_blob(struct kref *kref)
4326 struct drm_property_blob *blob =
4327 container_of(kref, struct drm_property_blob, refcount);
4329 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4331 list_del(&blob->head_global);
4332 list_del(&blob->head_file);
4333 drm_mode_object_unregister(blob->dev, &blob->base);
4339 * drm_property_unreference_blob - Unreference a blob property
4341 * Drop a reference on a blob property. May free the object.
4343 * @blob: Pointer to blob property
4345 void drm_property_unreference_blob(struct drm_property_blob *blob)
4347 struct drm_device *dev;
4354 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4356 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4357 &dev->mode_config.blob_lock))
4358 mutex_unlock(&dev->mode_config.blob_lock);
4360 might_lock(&dev->mode_config.blob_lock);
4362 EXPORT_SYMBOL(drm_property_unreference_blob);
4365 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4367 * Drop a reference on a blob property. May free the object. This must be
4368 * called with blob_lock held.
4370 * @blob: Pointer to blob property
4372 static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4377 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4379 kref_put(&blob->refcount, drm_property_free_blob);
4383 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4385 * @file_priv: destroy all blobs owned by this file handle
4387 void drm_property_destroy_user_blobs(struct drm_device *dev,
4388 struct drm_file *file_priv)
4390 struct drm_property_blob *blob, *bt;
4392 mutex_lock(&dev->mode_config.blob_lock);
4394 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4395 list_del_init(&blob->head_file);
4396 drm_property_unreference_blob_locked(blob);
4399 mutex_unlock(&dev->mode_config.blob_lock);
4403 * drm_property_reference_blob - Take a reference on an existing property
4405 * Take a new reference on an existing blob property.
4407 * @blob: Pointer to blob property
4409 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4411 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4412 kref_get(&blob->refcount);
4415 EXPORT_SYMBOL(drm_property_reference_blob);
4418 * Like drm_property_lookup_blob, but does not return an additional reference.
4419 * Must be called with blob_lock held.
4421 static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4424 struct drm_mode_object *obj = NULL;
4425 struct drm_property_blob *blob;
4427 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4429 mutex_lock(&dev->mode_config.idr_mutex);
4430 obj = idr_find(&dev->mode_config.crtc_idr, id);
4431 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4434 blob = obj_to_blob(obj);
4435 mutex_unlock(&dev->mode_config.idr_mutex);
4441 * drm_property_lookup_blob - look up a blob property and take a reference
4443 * @id: id of the blob property
4445 * If successful, this takes an additional reference to the blob property.
4446 * callers need to make sure to eventually unreference the returned property
4447 * again, using @drm_property_unreference_blob.
4449 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4452 struct drm_property_blob *blob;
4454 mutex_lock(&dev->mode_config.blob_lock);
4455 blob = __drm_property_lookup_blob(dev, id);
4457 if (!kref_get_unless_zero(&blob->refcount))
4460 mutex_unlock(&dev->mode_config.blob_lock);
4464 EXPORT_SYMBOL(drm_property_lookup_blob);
4467 * drm_property_replace_global_blob - atomically replace existing blob property
4469 * @replace: location of blob property pointer to be replaced
4470 * @length: length of data for new blob, or 0 for no data
4471 * @data: content for new blob, or NULL for no data
4472 * @obj_holds_id: optional object for property holding blob ID
4473 * @prop_holds_id: optional property holding blob ID
4474 * @return 0 on success or error on failure
4476 * This function will atomically replace a global property in the blob list,
4477 * optionally updating a property which holds the ID of that property. It is
4478 * guaranteed to be atomic: no caller will be allowed to see intermediate
4479 * results, and either the entire operation will succeed and clean up the
4480 * previous property, or it will fail and the state will be unchanged.
4482 * If length is 0 or data is NULL, no new blob will be created, and the holding
4483 * property, if specified, will be set to 0.
4485 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4486 * by holding the relevant modesetting object lock for its parent.
4488 * For example, a drm_connector has a 'PATH' property, which contains the ID
4489 * of a blob property with the value of the MST path information. Calling this
4490 * function with replace pointing to the connector's path_blob_ptr, length and
4491 * data set for the new path information, obj_holds_id set to the connector's
4492 * base object, and prop_holds_id set to the path property name, will perform
4493 * a completely atomic update. The access to path_blob_ptr is protected by the
4494 * caller holding a lock on the connector.
4496 static int drm_property_replace_global_blob(struct drm_device *dev,
4497 struct drm_property_blob **replace,
4500 struct drm_mode_object *obj_holds_id,
4501 struct drm_property *prop_holds_id)
4503 struct drm_property_blob *new_blob = NULL;
4504 struct drm_property_blob *old_blob = NULL;
4507 WARN_ON(replace == NULL);
4509 old_blob = *replace;
4511 if (length && data) {
4512 new_blob = drm_property_create_blob(dev, length, data);
4513 if (IS_ERR(new_blob))
4514 return PTR_ERR(new_blob);
4517 /* This does not need to be synchronised with blob_lock, as the
4518 * get_properties ioctl locks all modesetting objects, and
4519 * obj_holds_id must be locked before calling here, so we cannot
4520 * have its value out of sync with the list membership modified
4521 * below under blob_lock. */
4523 ret = drm_object_property_set_value(obj_holds_id,
4526 new_blob->base.id : 0);
4531 drm_property_unreference_blob(old_blob);
4532 *replace = new_blob;
4537 drm_property_unreference_blob(new_blob);
4542 * drm_mode_getblob_ioctl - get the contents of a blob property value
4545 * @file_priv: DRM file info
4547 * This function retrieves the contents of a blob property. The value stored in
4548 * an object's blob property is just a normal modeset object id.
4550 * Called by the user via ioctl.
4553 * Zero on success, negative errno on failure.
4555 int drm_mode_getblob_ioctl(struct drm_device *dev,
4556 void *data, struct drm_file *file_priv)
4558 struct drm_mode_get_blob *out_resp = data;
4559 struct drm_property_blob *blob;
4561 void __user *blob_ptr;
4563 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4566 drm_modeset_lock_all(dev);
4567 mutex_lock(&dev->mode_config.blob_lock);
4568 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4574 if (out_resp->length == blob->length) {
4575 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4576 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4581 out_resp->length = blob->length;
4584 mutex_unlock(&dev->mode_config.blob_lock);
4585 drm_modeset_unlock_all(dev);
4590 * drm_mode_createblob_ioctl - create a new blob property
4593 * @file_priv: DRM file info
4595 * This function creates a new blob property with user-defined values. In order
4596 * to give us sensible validation and checking when creating, rather than at
4597 * every potential use, we also require a type to be provided upfront.
4599 * Called by the user via ioctl.
4602 * Zero on success, negative errno on failure.
4604 int drm_mode_createblob_ioctl(struct drm_device *dev,
4605 void *data, struct drm_file *file_priv)
4607 struct drm_mode_create_blob *out_resp = data;
4608 struct drm_property_blob *blob;
4609 void __user *blob_ptr;
4612 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4615 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4617 return PTR_ERR(blob);
4619 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4620 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4625 /* Dropping the lock between create_blob and our access here is safe
4626 * as only the same file_priv can remove the blob; at this point, it is
4627 * not associated with any file_priv. */
4628 mutex_lock(&dev->mode_config.blob_lock);
4629 out_resp->blob_id = blob->base.id;
4630 list_add_tail(&blob->head_file, &file_priv->blobs);
4631 mutex_unlock(&dev->mode_config.blob_lock);
4636 drm_property_unreference_blob(blob);
4641 * drm_mode_destroyblob_ioctl - destroy a user blob property
4644 * @file_priv: DRM file info
4646 * Destroy an existing user-defined blob property.
4648 * Called by the user via ioctl.
4651 * Zero on success, negative errno on failure.
4653 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4654 void *data, struct drm_file *file_priv)
4656 struct drm_mode_destroy_blob *out_resp = data;
4657 struct drm_property_blob *blob = NULL, *bt;
4661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4664 mutex_lock(&dev->mode_config.blob_lock);
4665 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4671 /* Ensure the property was actually created by this user. */
4672 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4684 /* We must drop head_file here, because we may not be the last
4685 * reference on the blob. */
4686 list_del_init(&blob->head_file);
4687 drm_property_unreference_blob_locked(blob);
4688 mutex_unlock(&dev->mode_config.blob_lock);
4693 mutex_unlock(&dev->mode_config.blob_lock);
4698 * drm_mode_connector_set_path_property - set tile property on connector
4699 * @connector: connector to set property on.
4700 * @path: path to use for property; must not be NULL.
4702 * This creates a property to expose to userspace to specify a
4703 * connector path. This is mainly used for DisplayPort MST where
4704 * connectors have a topology and we want to allow userspace to give
4705 * them more meaningful names.
4708 * Zero on success, negative errno on failure.
4710 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4713 struct drm_device *dev = connector->dev;
4716 ret = drm_property_replace_global_blob(dev,
4717 &connector->path_blob_ptr,
4721 dev->mode_config.path_property);
4724 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4727 * drm_mode_connector_set_tile_property - set tile property on connector
4728 * @connector: connector to set property on.
4730 * This looks up the tile information for a connector, and creates a
4731 * property for userspace to parse if it exists. The property is of
4732 * the form of 8 integers using ':' as a separator.
4735 * Zero on success, errno on failure.
4737 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4739 struct drm_device *dev = connector->dev;
4743 if (!connector->has_tile) {
4744 ret = drm_property_replace_global_blob(dev,
4745 &connector->tile_blob_ptr,
4749 dev->mode_config.tile_property);
4753 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4754 connector->tile_group->id, connector->tile_is_single_monitor,
4755 connector->num_h_tile, connector->num_v_tile,
4756 connector->tile_h_loc, connector->tile_v_loc,
4757 connector->tile_h_size, connector->tile_v_size);
4759 ret = drm_property_replace_global_blob(dev,
4760 &connector->tile_blob_ptr,
4764 dev->mode_config.tile_property);
4767 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4770 * drm_mode_connector_update_edid_property - update the edid property of a connector
4771 * @connector: drm connector
4772 * @edid: new value of the edid property
4774 * This function creates a new blob modeset object and assigns its id to the
4775 * connector's edid property.
4778 * Zero on success, negative errno on failure.
4780 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4781 const struct edid *edid)
4783 struct drm_device *dev = connector->dev;
4787 /* ignore requests to set edid when overridden */
4788 if (connector->override_edid)
4792 size = EDID_LENGTH * (1 + edid->extensions);
4794 ret = drm_property_replace_global_blob(dev,
4795 &connector->edid_blob_ptr,
4799 dev->mode_config.edid_property);
4802 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4804 /* Some properties could refer to dynamic refcnt'd objects, or things that
4805 * need special locking to handle lifetime issues (ie. to ensure the prop
4806 * value doesn't become invalid part way through the property update due to
4807 * race). The value returned by reference via 'obj' should be passed back
4808 * to drm_property_change_valid_put() after the property is set (and the
4809 * object to which the property is attached has a chance to take it's own
4812 bool drm_property_change_valid_get(struct drm_property *property,
4813 uint64_t value, struct drm_mode_object **ref)
4817 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4822 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4823 if (value < property->values[0] || value > property->values[1])
4826 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4827 int64_t svalue = U642I64(value);
4829 if (svalue < U642I64(property->values[0]) ||
4830 svalue > U642I64(property->values[1]))
4833 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4834 uint64_t valid_mask = 0;
4836 for (i = 0; i < property->num_values; i++)
4837 valid_mask |= (1ULL << property->values[i]);
4838 return !(value & ~valid_mask);
4839 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4840 struct drm_property_blob *blob;
4845 blob = drm_property_lookup_blob(property->dev, value);
4852 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4853 /* a zero value for an object property translates to null: */
4857 /* handle refcnt'd objects specially: */
4858 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4859 struct drm_framebuffer *fb;
4860 fb = drm_framebuffer_lookup(property->dev, value);
4868 return _object_find(property->dev, value, property->values[0]) != NULL;
4872 for (i = 0; i < property->num_values; i++)
4873 if (property->values[i] == value)
4878 void drm_property_change_valid_put(struct drm_property *property,
4879 struct drm_mode_object *ref)
4884 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4885 if (property->values[0] == DRM_MODE_OBJECT_FB)
4886 drm_framebuffer_unreference(obj_to_fb(ref));
4887 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4888 drm_property_unreference_blob(obj_to_blob(ref));
4892 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4895 * @file_priv: DRM file info
4897 * This function sets the current value for a connectors's property. It also
4898 * calls into a driver's ->set_property callback to update the hardware state
4900 * Called by the user via ioctl.
4903 * Zero on success, negative errno on failure.
4905 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4906 void *data, struct drm_file *file_priv)
4908 struct drm_mode_connector_set_property *conn_set_prop = data;
4909 struct drm_mode_obj_set_property obj_set_prop = {
4910 .value = conn_set_prop->value,
4911 .prop_id = conn_set_prop->prop_id,
4912 .obj_id = conn_set_prop->connector_id,
4913 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4916 /* It does all the locking and checking we need */
4917 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4920 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4921 struct drm_property *property,
4925 struct drm_connector *connector = obj_to_connector(obj);
4927 /* Do DPMS ourselves */
4928 if (property == connector->dev->mode_config.dpms_property) {
4929 ret = (*connector->funcs->dpms)(connector, (int)value);
4930 } else if (connector->funcs->set_property)
4931 ret = connector->funcs->set_property(connector, property, value);
4933 /* store the property value if successful */
4935 drm_object_property_set_value(&connector->base, property, value);
4939 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4940 struct drm_property *property,
4944 struct drm_crtc *crtc = obj_to_crtc(obj);
4946 if (crtc->funcs->set_property)
4947 ret = crtc->funcs->set_property(crtc, property, value);
4949 drm_object_property_set_value(obj, property, value);
4955 * drm_mode_plane_set_obj_prop - set the value of a property
4956 * @plane: drm plane object to set property value for
4957 * @property: property to set
4958 * @value: value the property should be set to
4960 * This functions sets a given property on a given plane object. This function
4961 * calls the driver's ->set_property callback and changes the software state of
4962 * the property if the callback succeeds.
4965 * Zero on success, error code on failure.
4967 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4968 struct drm_property *property,
4972 struct drm_mode_object *obj = &plane->base;
4974 if (plane->funcs->set_property)
4975 ret = plane->funcs->set_property(plane, property, value);
4977 drm_object_property_set_value(obj, property, value);
4981 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4984 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4987 * @file_priv: DRM file info
4989 * This function retrieves the current value for an object's property. Compared
4990 * to the connector specific ioctl this one is extended to also work on crtc and
4993 * Called by the user via ioctl.
4996 * Zero on success, negative errno on failure.
4998 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4999 struct drm_file *file_priv)
5001 struct drm_mode_obj_get_properties *arg = data;
5002 struct drm_mode_object *obj;
5005 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5008 drm_modeset_lock_all(dev);
5010 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5015 if (!obj->properties) {
5020 ret = get_properties(obj, file_priv->atomic,
5021 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5022 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5026 drm_modeset_unlock_all(dev);
5031 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5034 * @file_priv: DRM file info
5036 * This function sets the current value for an object's property. It also calls
5037 * into a driver's ->set_property callback to update the hardware state.
5038 * Compared to the connector specific ioctl this one is extended to also work on
5039 * crtc and plane objects.
5041 * Called by the user via ioctl.
5044 * Zero on success, negative errno on failure.
5046 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5047 struct drm_file *file_priv)
5049 struct drm_mode_obj_set_property *arg = data;
5050 struct drm_mode_object *arg_obj;
5051 struct drm_mode_object *prop_obj;
5052 struct drm_property *property;
5053 int i, ret = -EINVAL;
5054 struct drm_mode_object *ref;
5056 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5059 drm_modeset_lock_all(dev);
5061 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5066 if (!arg_obj->properties)
5069 for (i = 0; i < arg_obj->properties->count; i++)
5070 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5073 if (i == arg_obj->properties->count)
5076 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5077 DRM_MODE_OBJECT_PROPERTY);
5082 property = obj_to_property(prop_obj);
5084 if (!drm_property_change_valid_get(property, arg->value, &ref))
5087 switch (arg_obj->type) {
5088 case DRM_MODE_OBJECT_CONNECTOR:
5089 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5092 case DRM_MODE_OBJECT_CRTC:
5093 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5095 case DRM_MODE_OBJECT_PLANE:
5096 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5097 property, arg->value);
5101 drm_property_change_valid_put(property, ref);
5104 drm_modeset_unlock_all(dev);
5109 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5110 * @connector: connector to attach
5111 * @encoder: encoder to attach @connector to
5113 * This function links up a connector to an encoder. Note that the routing
5114 * restrictions between encoders and crtcs are exposed to userspace through the
5115 * possible_clones and possible_crtcs bitmasks.
5118 * Zero on success, negative errno on failure.
5120 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5121 struct drm_encoder *encoder)
5126 * In the past, drivers have attempted to model the static association
5127 * of connector to encoder in simple connector/encoder devices using a
5128 * direct assignment of connector->encoder = encoder. This connection
5129 * is a logical one and the responsibility of the core, so drivers are
5130 * expected not to mess with this.
5132 * Note that the error return should've been enough here, but a large
5133 * majority of drivers ignores the return value, so add in a big WARN
5134 * to get people's attention.
5136 if (WARN_ON(connector->encoder))
5139 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5140 if (connector->encoder_ids[i] == 0) {
5141 connector->encoder_ids[i] = encoder->base.id;
5147 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5150 * drm_mode_crtc_set_gamma_size - set the gamma table size
5151 * @crtc: CRTC to set the gamma table size for
5152 * @gamma_size: size of the gamma table
5154 * Drivers which support gamma tables should set this to the supported gamma
5155 * table size when initializing the CRTC. Currently the drm core only supports a
5156 * fixed gamma table size.
5159 * Zero on success, negative errno on failure.
5161 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5164 crtc->gamma_size = gamma_size;
5166 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5168 if (!crtc->gamma_store) {
5169 crtc->gamma_size = 0;
5175 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5178 * drm_mode_gamma_set_ioctl - set the gamma table
5181 * @file_priv: DRM file info
5183 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5184 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5186 * Called by the user via ioctl.
5189 * Zero on success, negative errno on failure.
5191 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5192 void *data, struct drm_file *file_priv)
5194 struct drm_mode_crtc_lut *crtc_lut = data;
5195 struct drm_crtc *crtc;
5196 void *r_base, *g_base, *b_base;
5200 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5203 drm_modeset_lock_all(dev);
5204 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5210 if (crtc->funcs->gamma_set == NULL) {
5215 /* memcpy into gamma store */
5216 if (crtc_lut->gamma_size != crtc->gamma_size) {
5221 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5222 r_base = crtc->gamma_store;
5223 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5228 g_base = r_base + size;
5229 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5234 b_base = g_base + size;
5235 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5240 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5243 drm_modeset_unlock_all(dev);
5249 * drm_mode_gamma_get_ioctl - get the gamma table
5252 * @file_priv: DRM file info
5254 * Copy the current gamma table into the storage provided. This also provides
5255 * the gamma table size the driver expects, which can be used to size the
5256 * allocated storage.
5258 * Called by the user via ioctl.
5261 * Zero on success, negative errno on failure.
5263 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5264 void *data, struct drm_file *file_priv)
5266 struct drm_mode_crtc_lut *crtc_lut = data;
5267 struct drm_crtc *crtc;
5268 void *r_base, *g_base, *b_base;
5272 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5275 drm_modeset_lock_all(dev);
5276 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5282 /* memcpy into gamma store */
5283 if (crtc_lut->gamma_size != crtc->gamma_size) {
5288 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5289 r_base = crtc->gamma_store;
5290 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5295 g_base = r_base + size;
5296 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5301 b_base = g_base + size;
5302 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5307 drm_modeset_unlock_all(dev);
5312 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5315 * @file_priv: DRM file info
5317 * This schedules an asynchronous update on a given CRTC, called page flip.
5318 * Optionally a drm event is generated to signal the completion of the event.
5319 * Generic drivers cannot assume that a pageflip with changed framebuffer
5320 * properties (including driver specific metadata like tiling layout) will work,
5321 * but some drivers support e.g. pixel format changes through the pageflip
5324 * Called by the user via ioctl.
5327 * Zero on success, negative errno on failure.
5329 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5330 void *data, struct drm_file *file_priv)
5332 struct drm_mode_crtc_page_flip *page_flip = data;
5333 struct drm_crtc *crtc;
5334 struct drm_framebuffer *fb = NULL;
5335 struct drm_pending_vblank_event *e = NULL;
5338 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5339 page_flip->reserved != 0)
5342 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5345 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5349 drm_modeset_lock_crtc(crtc, crtc->primary);
5350 if (crtc->primary->fb == NULL) {
5351 /* The framebuffer is currently unbound, presumably
5352 * due to a hotplug event, that userspace has not
5359 if (crtc->funcs->page_flip == NULL)
5362 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5369 const struct drm_plane_state *state = crtc->primary->state;
5371 ret = check_src_coords(state->src_x, state->src_y,
5372 state->src_w, state->src_h, fb);
5374 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5379 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5380 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5385 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5386 e = kzalloc(sizeof *e, GFP_KERNEL);
5391 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5392 e->event.base.length = sizeof(e->event);
5393 e->event.user_data = page_flip->user_data;
5394 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5401 crtc->primary->old_fb = crtc->primary->fb;
5402 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5404 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5405 drm_event_cancel_free(dev, &e->base);
5406 /* Keep the old fb, don't unref it. */
5407 crtc->primary->old_fb = NULL;
5409 crtc->primary->fb = fb;
5410 /* Unref only the old framebuffer. */
5416 drm_framebuffer_unreference(fb);
5417 if (crtc->primary->old_fb)
5418 drm_framebuffer_unreference(crtc->primary->old_fb);
5419 crtc->primary->old_fb = NULL;
5420 drm_modeset_unlock_crtc(crtc);
5426 * drm_mode_config_reset - call ->reset callbacks
5429 * This functions calls all the crtc's, encoder's and connector's ->reset
5430 * callback. Drivers can use this in e.g. their driver load or resume code to
5431 * reset hardware and software state.
5433 void drm_mode_config_reset(struct drm_device *dev)
5435 struct drm_crtc *crtc;
5436 struct drm_plane *plane;
5437 struct drm_encoder *encoder;
5438 struct drm_connector *connector;
5440 drm_for_each_plane(plane, dev)
5441 if (plane->funcs->reset)
5442 plane->funcs->reset(plane);
5444 drm_for_each_crtc(crtc, dev)
5445 if (crtc->funcs->reset)
5446 crtc->funcs->reset(crtc);
5448 drm_for_each_encoder(encoder, dev)
5449 if (encoder->funcs->reset)
5450 encoder->funcs->reset(encoder);
5452 mutex_lock(&dev->mode_config.mutex);
5453 drm_for_each_connector(connector, dev)
5454 if (connector->funcs->reset)
5455 connector->funcs->reset(connector);
5456 mutex_unlock(&dev->mode_config.mutex);
5458 EXPORT_SYMBOL(drm_mode_config_reset);
5461 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5464 * @file_priv: DRM file info
5466 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5467 * TTM or something else entirely) and returns the resulting buffer handle. This
5468 * handle can then be wrapped up into a framebuffer modeset object.
5470 * Note that userspace is not allowed to use such objects for render
5471 * acceleration - drivers must create their own private ioctls for such a use
5474 * Called by the user via ioctl.
5477 * Zero on success, negative errno on failure.
5479 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5480 void *data, struct drm_file *file_priv)
5482 struct drm_mode_create_dumb *args = data;
5483 u32 cpp, stride, size;
5485 if (!dev->driver->dumb_create)
5487 if (!args->width || !args->height || !args->bpp)
5490 /* overflow checks for 32bit size calculations */
5491 /* NOTE: DIV_ROUND_UP() can overflow */
5492 cpp = DIV_ROUND_UP(args->bpp, 8);
5493 if (!cpp || cpp > 0xffffffffU / args->width)
5495 stride = cpp * args->width;
5496 if (args->height > 0xffffffffU / stride)
5499 /* test for wrap-around */
5500 size = args->height * stride;
5501 if (PAGE_ALIGN(size) == 0)
5505 * handle, pitch and size are output parameters. Zero them out to
5506 * prevent drivers from accidentally using uninitialized data. Since
5507 * not all existing userspace is clearing these fields properly we
5508 * cannot reject IOCTL with garbage in them.
5514 return dev->driver->dumb_create(file_priv, dev, args);
5518 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5521 * @file_priv: DRM file info
5523 * Allocate an offset in the drm device node's address space to be able to
5524 * memory map a dumb buffer.
5526 * Called by the user via ioctl.
5529 * Zero on success, negative errno on failure.
5531 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5532 void *data, struct drm_file *file_priv)
5534 struct drm_mode_map_dumb *args = data;
5536 /* call driver ioctl to get mmap offset */
5537 if (!dev->driver->dumb_map_offset)
5540 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5544 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5547 * @file_priv: DRM file info
5549 * This destroys the userspace handle for the given dumb backing storage buffer.
5550 * Since buffer objects must be reference counted in the kernel a buffer object
5551 * won't be immediately freed if a framebuffer modeset object still uses it.
5553 * Called by the user via ioctl.
5556 * Zero on success, negative errno on failure.
5558 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5559 void *data, struct drm_file *file_priv)
5561 struct drm_mode_destroy_dumb *args = data;
5563 if (!dev->driver->dumb_destroy)
5566 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5570 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5571 * @format: pixel format (DRM_FORMAT_*)
5572 * @depth: storage for the depth value
5573 * @bpp: storage for the bpp value
5575 * This only supports RGB formats here for compat with code that doesn't use
5576 * pixel formats directly yet.
5578 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5583 case DRM_FORMAT_RGB332:
5584 case DRM_FORMAT_BGR233:
5588 case DRM_FORMAT_XRGB1555:
5589 case DRM_FORMAT_XBGR1555:
5590 case DRM_FORMAT_RGBX5551:
5591 case DRM_FORMAT_BGRX5551:
5592 case DRM_FORMAT_ARGB1555:
5593 case DRM_FORMAT_ABGR1555:
5594 case DRM_FORMAT_RGBA5551:
5595 case DRM_FORMAT_BGRA5551:
5599 case DRM_FORMAT_RGB565:
5600 case DRM_FORMAT_BGR565:
5604 case DRM_FORMAT_RGB888:
5605 case DRM_FORMAT_BGR888:
5609 case DRM_FORMAT_XRGB8888:
5610 case DRM_FORMAT_XBGR8888:
5611 case DRM_FORMAT_RGBX8888:
5612 case DRM_FORMAT_BGRX8888:
5616 case DRM_FORMAT_XRGB2101010:
5617 case DRM_FORMAT_XBGR2101010:
5618 case DRM_FORMAT_RGBX1010102:
5619 case DRM_FORMAT_BGRX1010102:
5620 case DRM_FORMAT_ARGB2101010:
5621 case DRM_FORMAT_ABGR2101010:
5622 case DRM_FORMAT_RGBA1010102:
5623 case DRM_FORMAT_BGRA1010102:
5627 case DRM_FORMAT_ARGB8888:
5628 case DRM_FORMAT_ABGR8888:
5629 case DRM_FORMAT_RGBA8888:
5630 case DRM_FORMAT_BGRA8888:
5635 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5636 drm_get_format_name(format));
5642 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5645 * drm_format_num_planes - get the number of planes for format
5646 * @format: pixel format (DRM_FORMAT_*)
5649 * The number of planes used by the specified pixel format.
5651 int drm_format_num_planes(uint32_t format)
5654 case DRM_FORMAT_YUV410:
5655 case DRM_FORMAT_YVU410:
5656 case DRM_FORMAT_YUV411:
5657 case DRM_FORMAT_YVU411:
5658 case DRM_FORMAT_YUV420:
5659 case DRM_FORMAT_YVU420:
5660 case DRM_FORMAT_YUV422:
5661 case DRM_FORMAT_YVU422:
5662 case DRM_FORMAT_YUV444:
5663 case DRM_FORMAT_YVU444:
5665 case DRM_FORMAT_NV12:
5666 case DRM_FORMAT_NV21:
5667 case DRM_FORMAT_NV16:
5668 case DRM_FORMAT_NV61:
5669 case DRM_FORMAT_NV24:
5670 case DRM_FORMAT_NV42:
5676 EXPORT_SYMBOL(drm_format_num_planes);
5679 * drm_format_plane_cpp - determine the bytes per pixel value
5680 * @format: pixel format (DRM_FORMAT_*)
5681 * @plane: plane index
5684 * The bytes per pixel value for the specified plane.
5686 int drm_format_plane_cpp(uint32_t format, int plane)
5691 if (plane >= drm_format_num_planes(format))
5695 case DRM_FORMAT_YUYV:
5696 case DRM_FORMAT_YVYU:
5697 case DRM_FORMAT_UYVY:
5698 case DRM_FORMAT_VYUY:
5700 case DRM_FORMAT_NV12:
5701 case DRM_FORMAT_NV21:
5702 case DRM_FORMAT_NV16:
5703 case DRM_FORMAT_NV61:
5704 case DRM_FORMAT_NV24:
5705 case DRM_FORMAT_NV42:
5706 return plane ? 2 : 1;
5707 case DRM_FORMAT_YUV410:
5708 case DRM_FORMAT_YVU410:
5709 case DRM_FORMAT_YUV411:
5710 case DRM_FORMAT_YVU411:
5711 case DRM_FORMAT_YUV420:
5712 case DRM_FORMAT_YVU420:
5713 case DRM_FORMAT_YUV422:
5714 case DRM_FORMAT_YVU422:
5715 case DRM_FORMAT_YUV444:
5716 case DRM_FORMAT_YVU444:
5719 drm_fb_get_bpp_depth(format, &depth, &bpp);
5723 EXPORT_SYMBOL(drm_format_plane_cpp);
5726 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5727 * @format: pixel format (DRM_FORMAT_*)
5730 * The horizontal chroma subsampling factor for the
5731 * specified pixel format.
5733 int drm_format_horz_chroma_subsampling(uint32_t format)
5736 case DRM_FORMAT_YUV411:
5737 case DRM_FORMAT_YVU411:
5738 case DRM_FORMAT_YUV410:
5739 case DRM_FORMAT_YVU410:
5741 case DRM_FORMAT_YUYV:
5742 case DRM_FORMAT_YVYU:
5743 case DRM_FORMAT_UYVY:
5744 case DRM_FORMAT_VYUY:
5745 case DRM_FORMAT_NV12:
5746 case DRM_FORMAT_NV21:
5747 case DRM_FORMAT_NV16:
5748 case DRM_FORMAT_NV61:
5749 case DRM_FORMAT_YUV422:
5750 case DRM_FORMAT_YVU422:
5751 case DRM_FORMAT_YUV420:
5752 case DRM_FORMAT_YVU420:
5758 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5761 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5762 * @format: pixel format (DRM_FORMAT_*)
5765 * The vertical chroma subsampling factor for the
5766 * specified pixel format.
5768 int drm_format_vert_chroma_subsampling(uint32_t format)
5771 case DRM_FORMAT_YUV410:
5772 case DRM_FORMAT_YVU410:
5774 case DRM_FORMAT_YUV420:
5775 case DRM_FORMAT_YVU420:
5776 case DRM_FORMAT_NV12:
5777 case DRM_FORMAT_NV21:
5783 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5786 * drm_format_plane_width - width of the plane given the first plane
5787 * @width: width of the first plane
5788 * @format: pixel format
5789 * @plane: plane index
5792 * The width of @plane, given that the width of the first plane is @width.
5794 int drm_format_plane_width(int width, uint32_t format, int plane)
5796 if (plane >= drm_format_num_planes(format))
5802 return width / drm_format_horz_chroma_subsampling(format);
5804 EXPORT_SYMBOL(drm_format_plane_width);
5807 * drm_format_plane_height - height of the plane given the first plane
5808 * @height: height of the first plane
5809 * @format: pixel format
5810 * @plane: plane index
5813 * The height of @plane, given that the height of the first plane is @height.
5815 int drm_format_plane_height(int height, uint32_t format, int plane)
5817 if (plane >= drm_format_num_planes(format))
5823 return height / drm_format_vert_chroma_subsampling(format);
5825 EXPORT_SYMBOL(drm_format_plane_height);
5828 * drm_rotation_simplify() - Try to simplify the rotation
5829 * @rotation: Rotation to be simplified
5830 * @supported_rotations: Supported rotations
5832 * Attempt to simplify the rotation to a form that is supported.
5833 * Eg. if the hardware supports everything except DRM_REFLECT_X
5834 * one could call this function like this:
5836 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5837 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5838 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5840 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5841 * transforms the hardware supports, this function may not
5842 * be able to produce a supported transform, so the caller should
5843 * check the result afterwards.
5845 unsigned int drm_rotation_simplify(unsigned int rotation,
5846 unsigned int supported_rotations)
5848 if (rotation & ~supported_rotations) {
5849 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5850 rotation = (rotation & DRM_REFLECT_MASK) |
5851 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5856 EXPORT_SYMBOL(drm_rotation_simplify);
5859 * drm_mode_config_init - initialize DRM mode_configuration structure
5862 * Initialize @dev's mode_config structure, used for tracking the graphics
5863 * configuration of @dev.
5865 * Since this initializes the modeset locks, no locking is possible. Which is no
5866 * problem, since this should happen single threaded at init time. It is the
5867 * driver's problem to ensure this guarantee.
5870 void drm_mode_config_init(struct drm_device *dev)
5872 mutex_init(&dev->mode_config.mutex);
5873 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5874 mutex_init(&dev->mode_config.idr_mutex);
5875 mutex_init(&dev->mode_config.fb_lock);
5876 mutex_init(&dev->mode_config.blob_lock);
5877 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5878 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5879 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5880 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5881 INIT_LIST_HEAD(&dev->mode_config.property_list);
5882 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5883 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5884 idr_init(&dev->mode_config.crtc_idr);
5885 idr_init(&dev->mode_config.tile_idr);
5886 ida_init(&dev->mode_config.connector_ida);
5888 drm_modeset_lock_all(dev);
5889 drm_mode_create_standard_properties(dev);
5890 drm_modeset_unlock_all(dev);
5892 /* Just to be sure */
5893 dev->mode_config.num_fb = 0;
5894 dev->mode_config.num_connector = 0;
5895 dev->mode_config.num_crtc = 0;
5896 dev->mode_config.num_encoder = 0;
5897 dev->mode_config.num_overlay_plane = 0;
5898 dev->mode_config.num_total_plane = 0;
5900 EXPORT_SYMBOL(drm_mode_config_init);
5903 * drm_mode_config_cleanup - free up DRM mode_config info
5906 * Free up all the connectors and CRTCs associated with this DRM device, then
5907 * free up the framebuffers and associated buffer objects.
5909 * Note that since this /should/ happen single-threaded at driver/device
5910 * teardown time, no locking is required. It's the driver's job to ensure that
5911 * this guarantee actually holds true.
5913 * FIXME: cleanup any dangling user buffer objects too
5915 void drm_mode_config_cleanup(struct drm_device *dev)
5917 struct drm_connector *connector, *ot;
5918 struct drm_crtc *crtc, *ct;
5919 struct drm_encoder *encoder, *enct;
5920 struct drm_framebuffer *fb, *fbt;
5921 struct drm_property *property, *pt;
5922 struct drm_property_blob *blob, *bt;
5923 struct drm_plane *plane, *plt;
5925 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5927 encoder->funcs->destroy(encoder);
5930 list_for_each_entry_safe(connector, ot,
5931 &dev->mode_config.connector_list, head) {
5932 connector->funcs->destroy(connector);
5935 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5937 drm_property_destroy(dev, property);
5940 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5942 plane->funcs->destroy(plane);
5945 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5946 crtc->funcs->destroy(crtc);
5949 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5951 drm_property_unreference_blob(blob);
5955 * Single-threaded teardown context, so it's not required to grab the
5956 * fb_lock to protect against concurrent fb_list access. Contrary, it
5957 * would actually deadlock with the drm_framebuffer_cleanup function.
5959 * Also, if there are any framebuffers left, that's a driver leak now,
5960 * so politely WARN about this.
5962 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5963 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5964 drm_framebuffer_free(&fb->base.refcount);
5967 ida_destroy(&dev->mode_config.connector_ida);
5968 idr_destroy(&dev->mode_config.tile_idr);
5969 idr_destroy(&dev->mode_config.crtc_idr);
5970 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5972 EXPORT_SYMBOL(drm_mode_config_cleanup);
5974 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5975 unsigned int supported_rotations)
5977 static const struct drm_prop_enum_list props[] = {
5978 { DRM_ROTATE_0, "rotate-0" },
5979 { DRM_ROTATE_90, "rotate-90" },
5980 { DRM_ROTATE_180, "rotate-180" },
5981 { DRM_ROTATE_270, "rotate-270" },
5982 { DRM_REFLECT_X, "reflect-x" },
5983 { DRM_REFLECT_Y, "reflect-y" },
5986 return drm_property_create_bitmask(dev, 0, "rotation",
5987 props, ARRAY_SIZE(props),
5988 supported_rotations);
5990 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5995 * Tile groups are used to represent tiled monitors with a unique
5996 * integer identifier. Tiled monitors using DisplayID v1.3 have
5997 * a unique 8-byte handle, we store this in a tile group, so we
5998 * have a common identifier for all tiles in a monitor group.
6000 static void drm_tile_group_free(struct kref *kref)
6002 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
6003 struct drm_device *dev = tg->dev;
6004 mutex_lock(&dev->mode_config.idr_mutex);
6005 idr_remove(&dev->mode_config.tile_idr, tg->id);
6006 mutex_unlock(&dev->mode_config.idr_mutex);
6011 * drm_mode_put_tile_group - drop a reference to a tile group.
6013 * @tg: tile group to drop reference to.
6015 * drop reference to tile group and free if 0.
6017 void drm_mode_put_tile_group(struct drm_device *dev,
6018 struct drm_tile_group *tg)
6020 kref_put(&tg->refcount, drm_tile_group_free);
6024 * drm_mode_get_tile_group - get a reference to an existing tile group
6026 * @topology: 8-bytes unique per monitor.
6028 * Use the unique bytes to get a reference to an existing tile group.
6031 * tile group or NULL if not found.
6033 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6036 struct drm_tile_group *tg;
6038 mutex_lock(&dev->mode_config.idr_mutex);
6039 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6040 if (!memcmp(tg->group_data, topology, 8)) {
6041 if (!kref_get_unless_zero(&tg->refcount))
6043 mutex_unlock(&dev->mode_config.idr_mutex);
6047 mutex_unlock(&dev->mode_config.idr_mutex);
6050 EXPORT_SYMBOL(drm_mode_get_tile_group);
6053 * drm_mode_create_tile_group - create a tile group from a displayid description
6055 * @topology: 8-bytes unique per monitor.
6057 * Create a tile group for the unique monitor, and get a unique
6058 * identifier for the tile group.
6061 * new tile group or error.
6063 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6066 struct drm_tile_group *tg;
6069 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6071 return ERR_PTR(-ENOMEM);
6073 kref_init(&tg->refcount);
6074 memcpy(tg->group_data, topology, 8);
6077 mutex_lock(&dev->mode_config.idr_mutex);
6078 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6086 mutex_unlock(&dev->mode_config.idr_mutex);
6089 EXPORT_SYMBOL(drm_mode_create_tile_group);