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)
368 if (obj && obj->free_cb) {
369 if (!kref_get_unless_zero(&obj->refcount))
372 mutex_unlock(&dev->mode_config.idr_mutex);
378 * drm_mode_object_find - look up a drm object with static lifetime
380 * @id: id of the mode object
381 * @type: type of the mode object
383 * Note that framebuffers cannot be looked up with this functions - since those
384 * are reference counted, they need special treatment. Even with
385 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
386 * rather than WARN_ON()).
388 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
389 uint32_t id, uint32_t type)
391 struct drm_mode_object *obj = NULL;
393 /* Framebuffers are reference counted and need their own lookup
395 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
396 obj = _object_find(dev, id, type);
399 EXPORT_SYMBOL(drm_mode_object_find);
401 void drm_mode_object_unreference(struct drm_mode_object *obj)
404 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
405 kref_put(&obj->refcount, obj->free_cb);
408 EXPORT_SYMBOL(drm_mode_object_unreference);
411 * drm_mode_object_reference - incr the fb refcnt
414 * This function operates only on refcounted objects.
415 * This functions increments the object's refcount.
417 void drm_mode_object_reference(struct drm_mode_object *obj)
420 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
421 kref_get(&obj->refcount);
424 EXPORT_SYMBOL(drm_mode_object_reference);
426 static void drm_framebuffer_free(struct kref *kref)
428 struct drm_framebuffer *fb =
429 container_of(kref, struct drm_framebuffer, base.refcount);
430 struct drm_device *dev = fb->dev;
433 * The lookup idr holds a weak reference, which has not necessarily been
434 * removed at this point. Check for that.
436 mutex_lock(&dev->mode_config.fb_lock);
437 drm_mode_object_unregister(dev, &fb->base);
438 mutex_unlock(&dev->mode_config.fb_lock);
440 fb->funcs->destroy(fb);
444 * drm_framebuffer_init - initialize a framebuffer
446 * @fb: framebuffer to be initialized
447 * @funcs: ... with these functions
449 * Allocates an ID for the framebuffer's parent mode object, sets its mode
450 * functions & device file and adds it to the master fd list.
453 * This functions publishes the fb and makes it available for concurrent access
454 * by other users. Which means by this point the fb _must_ be fully set up -
455 * since all the fb attributes are invariant over its lifetime, no further
456 * locking but only correct reference counting is required.
459 * Zero on success, error code on failure.
461 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
462 const struct drm_framebuffer_funcs *funcs)
466 INIT_LIST_HEAD(&fb->filp_head);
470 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
471 false, drm_framebuffer_free);
475 mutex_lock(&dev->mode_config.fb_lock);
476 dev->mode_config.num_fb++;
477 list_add(&fb->head, &dev->mode_config.fb_list);
479 drm_mode_object_register(dev, &fb->base);
480 mutex_unlock(&dev->mode_config.fb_lock);
484 EXPORT_SYMBOL(drm_framebuffer_init);
487 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
489 * @id: id of the fb object
491 * If successful, this grabs an additional reference to the framebuffer -
492 * callers need to make sure to eventually unreference the returned framebuffer
493 * again, using @drm_framebuffer_unreference.
495 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
498 struct drm_mode_object *obj;
499 struct drm_framebuffer *fb = NULL;
501 mutex_lock(&dev->mode_config.fb_lock);
502 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
505 mutex_unlock(&dev->mode_config.fb_lock);
509 EXPORT_SYMBOL(drm_framebuffer_lookup);
512 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
513 * @fb: fb to unregister
515 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
516 * those used for fbdev. Note that the caller must hold a reference of it's own,
517 * i.e. the object may not be destroyed through this call (since it'll lead to a
518 * locking inversion).
520 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
522 struct drm_device *dev;
529 mutex_lock(&dev->mode_config.fb_lock);
530 /* Mark fb as reaped and drop idr ref. */
531 drm_mode_object_unregister(dev, &fb->base);
532 mutex_unlock(&dev->mode_config.fb_lock);
534 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
537 * drm_framebuffer_cleanup - remove a framebuffer object
538 * @fb: framebuffer to remove
540 * Cleanup framebuffer. This function is intended to be used from the drivers
541 * ->destroy callback. It can also be used to clean up driver private
542 * framebuffers embedded into a larger structure.
544 * Note that this function does not remove the fb from active usuage - if it is
545 * still used anywhere, hilarity can ensue since userspace could call getfb on
546 * the id and get back -EINVAL. Obviously no concern at driver unload time.
548 * Also, the framebuffer will not be removed from the lookup idr - for
549 * user-created framebuffers this will happen in in the rmfb ioctl. For
550 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
551 * drm_framebuffer_unregister_private.
553 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
555 struct drm_device *dev = fb->dev;
557 mutex_lock(&dev->mode_config.fb_lock);
559 dev->mode_config.num_fb--;
560 mutex_unlock(&dev->mode_config.fb_lock);
562 EXPORT_SYMBOL(drm_framebuffer_cleanup);
565 * drm_framebuffer_remove - remove and unreference a framebuffer object
566 * @fb: framebuffer to remove
568 * Scans all the CRTCs and planes in @dev's mode_config. If they're
569 * using @fb, removes it, setting it to NULL. Then drops the reference to the
570 * passed-in framebuffer. Might take the modeset locks.
572 * Note that this function optimizes the cleanup away if the caller holds the
573 * last reference to the framebuffer. It is also guaranteed to not take the
574 * modeset locks in this case.
576 void drm_framebuffer_remove(struct drm_framebuffer *fb)
578 struct drm_device *dev;
579 struct drm_crtc *crtc;
580 struct drm_plane *plane;
581 struct drm_mode_set set;
589 WARN_ON(!list_empty(&fb->filp_head));
592 * drm ABI mandates that we remove any deleted framebuffers from active
593 * useage. But since most sane clients only remove framebuffers they no
594 * longer need, try to optimize this away.
596 * Since we're holding a reference ourselves, observing a refcount of 1
597 * means that we're the last holder and can skip it. Also, the refcount
598 * can never increase from 1 again, so we don't need any barriers or
601 * Note that userspace could try to race with use and instate a new
602 * usage _after_ we've cleared all current ones. End result will be an
603 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
606 if (drm_framebuffer_read_refcount(fb) > 1) {
607 drm_modeset_lock_all(dev);
608 /* remove from any CRTC */
609 drm_for_each_crtc(crtc, dev) {
610 if (crtc->primary->fb == fb) {
611 /* should turn off the crtc */
612 memset(&set, 0, sizeof(struct drm_mode_set));
615 ret = drm_mode_set_config_internal(&set);
617 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
621 drm_for_each_plane(plane, dev) {
623 drm_plane_force_disable(plane);
625 drm_modeset_unlock_all(dev);
628 drm_framebuffer_unreference(fb);
630 EXPORT_SYMBOL(drm_framebuffer_remove);
632 DEFINE_WW_CLASS(crtc_ww_class);
634 static unsigned int drm_num_crtcs(struct drm_device *dev)
636 unsigned int num = 0;
637 struct drm_crtc *tmp;
639 drm_for_each_crtc(tmp, dev) {
647 * drm_crtc_init_with_planes - Initialise a new CRTC object with
648 * specified primary and cursor planes.
650 * @crtc: CRTC object to init
651 * @primary: Primary plane for CRTC
652 * @cursor: Cursor plane for CRTC
653 * @funcs: callbacks for the new CRTC
654 * @name: printf style format string for the CRTC name, or NULL for default name
656 * Inits a new object created as base part of a driver crtc object.
659 * Zero on success, error code on failure.
661 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
662 struct drm_plane *primary,
663 struct drm_plane *cursor,
664 const struct drm_crtc_funcs *funcs,
665 const char *name, ...)
667 struct drm_mode_config *config = &dev->mode_config;
670 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
671 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
676 drm_modeset_lock_init(&crtc->mutex);
677 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
685 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
688 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
692 drm_mode_object_unregister(dev, &crtc->base);
696 crtc->base.properties = &crtc->properties;
698 list_add_tail(&crtc->head, &config->crtc_list);
701 crtc->primary = primary;
702 crtc->cursor = cursor;
704 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
706 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
708 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
709 drm_object_attach_property(&crtc->base, config->prop_active, 0);
710 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
715 EXPORT_SYMBOL(drm_crtc_init_with_planes);
718 * drm_crtc_cleanup - Clean up the core crtc usage
719 * @crtc: CRTC to cleanup
721 * This function cleans up @crtc and removes it from the DRM mode setting
722 * core. Note that the function does *not* free the crtc structure itself,
723 * this is the responsibility of the caller.
725 void drm_crtc_cleanup(struct drm_crtc *crtc)
727 struct drm_device *dev = crtc->dev;
729 kfree(crtc->gamma_store);
730 crtc->gamma_store = NULL;
732 drm_modeset_lock_fini(&crtc->mutex);
734 drm_mode_object_unregister(dev, &crtc->base);
735 list_del(&crtc->head);
736 dev->mode_config.num_crtc--;
738 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
739 if (crtc->state && crtc->funcs->atomic_destroy_state)
740 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
744 memset(crtc, 0, sizeof(*crtc));
746 EXPORT_SYMBOL(drm_crtc_cleanup);
749 * drm_crtc_index - find the index of a registered CRTC
750 * @crtc: CRTC to find index for
752 * Given a registered CRTC, return the index of that CRTC within a DRM
753 * device's list of CRTCs.
755 unsigned int drm_crtc_index(struct drm_crtc *crtc)
757 unsigned int index = 0;
758 struct drm_crtc *tmp;
760 drm_for_each_crtc(tmp, crtc->dev) {
769 EXPORT_SYMBOL(drm_crtc_index);
772 * drm_mode_remove - remove and free a mode
773 * @connector: connector list to modify
774 * @mode: mode to remove
776 * Remove @mode from @connector's mode list, then free it.
778 static void drm_mode_remove(struct drm_connector *connector,
779 struct drm_display_mode *mode)
781 list_del(&mode->head);
782 drm_mode_destroy(connector->dev, mode);
786 * drm_display_info_set_bus_formats - set the supported bus formats
787 * @info: display info to store bus formats in
788 * @formats: array containing the supported bus formats
789 * @num_formats: the number of entries in the fmts array
791 * Store the supported bus formats in display info structure.
792 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
793 * a full list of available formats.
795 int drm_display_info_set_bus_formats(struct drm_display_info *info,
797 unsigned int num_formats)
801 if (!formats && num_formats)
804 if (formats && num_formats) {
805 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
811 kfree(info->bus_formats);
812 info->bus_formats = fmts;
813 info->num_bus_formats = num_formats;
817 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
820 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
821 * @connector: connector to quwery
823 * The kernel supports per-connector configration of its consoles through
824 * use of the video= parameter. This function parses that option and
825 * extracts the user's specified mode (or enable/disable status) for a
826 * particular connector. This is typically only used during the early fbdev
829 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
831 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
834 if (fb_get_options(connector->name, &option))
837 if (!drm_mode_parse_command_line_for_connector(option,
845 switch (mode->force) {
849 case DRM_FORCE_ON_DIGITAL:
858 DRM_INFO("forcing %s connector %s\n", connector->name, s);
859 connector->force = mode->force;
862 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
864 mode->xres, mode->yres,
865 mode->refresh_specified ? mode->refresh : 60,
866 mode->rb ? " reduced blanking" : "",
867 mode->margins ? " with margins" : "",
868 mode->interlace ? " interlaced" : "");
872 * drm_connector_init - Init a preallocated connector
874 * @connector: the connector to init
875 * @funcs: callbacks for this connector
876 * @connector_type: user visible type of the connector
878 * Initialises a preallocated connector. Connectors should be
879 * subclassed as part of driver connector objects.
882 * Zero on success, error code on failure.
884 int drm_connector_init(struct drm_device *dev,
885 struct drm_connector *connector,
886 const struct drm_connector_funcs *funcs,
889 struct drm_mode_config *config = &dev->mode_config;
891 struct ida *connector_ida =
892 &drm_connector_enum_list[connector_type].ida;
894 drm_modeset_lock_all(dev);
896 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
900 connector->base.properties = &connector->properties;
901 connector->dev = dev;
902 connector->funcs = funcs;
904 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
905 if (connector->connector_id < 0) {
906 ret = connector->connector_id;
910 connector->connector_type = connector_type;
911 connector->connector_type_id =
912 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
913 if (connector->connector_type_id < 0) {
914 ret = connector->connector_type_id;
918 kasprintf(GFP_KERNEL, "%s-%d",
919 drm_connector_enum_list[connector_type].name,
920 connector->connector_type_id);
921 if (!connector->name) {
923 goto out_put_type_id;
926 INIT_LIST_HEAD(&connector->probed_modes);
927 INIT_LIST_HEAD(&connector->modes);
928 connector->edid_blob_ptr = NULL;
929 connector->status = connector_status_unknown;
931 drm_connector_get_cmdline_mode(connector);
933 /* We should add connectors at the end to avoid upsetting the connector
935 list_add_tail(&connector->head, &config->connector_list);
936 config->num_connector++;
938 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
939 drm_object_attach_property(&connector->base,
940 config->edid_property,
943 drm_object_attach_property(&connector->base,
944 config->dpms_property, 0);
946 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
947 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
950 connector->debugfs_entry = NULL;
953 ida_remove(connector_ida, connector->connector_type_id);
956 ida_remove(&config->connector_ida, connector->connector_id);
959 drm_mode_object_unregister(dev, &connector->base);
962 drm_modeset_unlock_all(dev);
966 EXPORT_SYMBOL(drm_connector_init);
969 * drm_connector_cleanup - cleans up an initialised connector
970 * @connector: connector to cleanup
972 * Cleans up the connector but doesn't free the object.
974 void drm_connector_cleanup(struct drm_connector *connector)
976 struct drm_device *dev = connector->dev;
977 struct drm_display_mode *mode, *t;
979 if (connector->tile_group) {
980 drm_mode_put_tile_group(dev, connector->tile_group);
981 connector->tile_group = NULL;
984 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
985 drm_mode_remove(connector, mode);
987 list_for_each_entry_safe(mode, t, &connector->modes, head)
988 drm_mode_remove(connector, mode);
990 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
991 connector->connector_type_id);
993 ida_remove(&dev->mode_config.connector_ida,
994 connector->connector_id);
996 kfree(connector->display_info.bus_formats);
997 drm_mode_object_unregister(dev, &connector->base);
998 kfree(connector->name);
999 connector->name = NULL;
1000 list_del(&connector->head);
1001 dev->mode_config.num_connector--;
1003 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
1004 if (connector->state && connector->funcs->atomic_destroy_state)
1005 connector->funcs->atomic_destroy_state(connector,
1008 memset(connector, 0, sizeof(*connector));
1010 EXPORT_SYMBOL(drm_connector_cleanup);
1013 * drm_connector_register - register a connector
1014 * @connector: the connector to register
1016 * Register userspace interfaces for a connector
1019 * Zero on success, error code on failure.
1021 int drm_connector_register(struct drm_connector *connector)
1025 drm_mode_object_register(connector->dev, &connector->base);
1027 ret = drm_sysfs_connector_add(connector);
1031 ret = drm_debugfs_connector_add(connector);
1033 drm_sysfs_connector_remove(connector);
1039 EXPORT_SYMBOL(drm_connector_register);
1042 * drm_connector_unregister - unregister a connector
1043 * @connector: the connector to unregister
1045 * Unregister userspace interfaces for a connector
1047 void drm_connector_unregister(struct drm_connector *connector)
1049 drm_sysfs_connector_remove(connector);
1050 drm_debugfs_connector_remove(connector);
1052 EXPORT_SYMBOL(drm_connector_unregister);
1055 * drm_connector_register_all - register all connectors
1058 * This function registers all connectors in sysfs and other places so that
1059 * userspace can start to access them. Drivers can call it after calling
1060 * drm_dev_register() to complete the device registration, if they don't call
1061 * drm_connector_register() on each connector individually.
1063 * When a device is unplugged and should be removed from userspace access,
1064 * call drm_connector_unregister_all(), which is the inverse of this
1068 * Zero on success, error code on failure.
1070 int drm_connector_register_all(struct drm_device *dev)
1072 struct drm_connector *connector;
1075 mutex_lock(&dev->mode_config.mutex);
1077 drm_for_each_connector(connector, dev) {
1078 ret = drm_connector_register(connector);
1083 mutex_unlock(&dev->mode_config.mutex);
1088 mutex_unlock(&dev->mode_config.mutex);
1089 drm_connector_unregister_all(dev);
1092 EXPORT_SYMBOL(drm_connector_register_all);
1095 * drm_connector_unregister_all - unregister connector userspace interfaces
1098 * This functions unregisters all connectors from sysfs and other places so
1099 * that userspace can no longer access them. Drivers should call this as the
1100 * first step tearing down the device instace, or when the underlying
1101 * physical device disappeared (e.g. USB unplug), right before calling
1102 * drm_dev_unregister().
1104 void drm_connector_unregister_all(struct drm_device *dev)
1106 struct drm_connector *connector;
1108 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1109 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1110 drm_connector_unregister(connector);
1112 EXPORT_SYMBOL(drm_connector_unregister_all);
1115 * drm_encoder_init - Init a preallocated encoder
1117 * @encoder: the encoder to init
1118 * @funcs: callbacks for this encoder
1119 * @encoder_type: user visible type of the encoder
1120 * @name: printf style format string for the encoder name, or NULL for default name
1122 * Initialises a preallocated encoder. Encoder should be
1123 * subclassed as part of driver encoder objects.
1126 * Zero on success, error code on failure.
1128 int drm_encoder_init(struct drm_device *dev,
1129 struct drm_encoder *encoder,
1130 const struct drm_encoder_funcs *funcs,
1131 int encoder_type, const char *name, ...)
1135 drm_modeset_lock_all(dev);
1137 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1142 encoder->encoder_type = encoder_type;
1143 encoder->funcs = funcs;
1148 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1151 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1152 drm_encoder_enum_list[encoder_type].name,
1155 if (!encoder->name) {
1160 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1161 dev->mode_config.num_encoder++;
1165 drm_mode_object_unregister(dev, &encoder->base);
1168 drm_modeset_unlock_all(dev);
1172 EXPORT_SYMBOL(drm_encoder_init);
1175 * drm_encoder_index - find the index of a registered encoder
1176 * @encoder: encoder to find index for
1178 * Given a registered encoder, return the index of that encoder within a DRM
1179 * device's list of encoders.
1181 unsigned int drm_encoder_index(struct drm_encoder *encoder)
1183 unsigned int index = 0;
1184 struct drm_encoder *tmp;
1186 drm_for_each_encoder(tmp, encoder->dev) {
1195 EXPORT_SYMBOL(drm_encoder_index);
1198 * drm_encoder_cleanup - cleans up an initialised encoder
1199 * @encoder: encoder to cleanup
1201 * Cleans up the encoder but doesn't free the object.
1203 void drm_encoder_cleanup(struct drm_encoder *encoder)
1205 struct drm_device *dev = encoder->dev;
1207 drm_modeset_lock_all(dev);
1208 drm_mode_object_unregister(dev, &encoder->base);
1209 kfree(encoder->name);
1210 list_del(&encoder->head);
1211 dev->mode_config.num_encoder--;
1212 drm_modeset_unlock_all(dev);
1214 memset(encoder, 0, sizeof(*encoder));
1216 EXPORT_SYMBOL(drm_encoder_cleanup);
1218 static unsigned int drm_num_planes(struct drm_device *dev)
1220 unsigned int num = 0;
1221 struct drm_plane *tmp;
1223 drm_for_each_plane(tmp, dev) {
1231 * drm_universal_plane_init - Initialize a new universal plane object
1233 * @plane: plane object to init
1234 * @possible_crtcs: bitmask of possible CRTCs
1235 * @funcs: callbacks for the new plane
1236 * @formats: array of supported formats (%DRM_FORMAT_*)
1237 * @format_count: number of elements in @formats
1238 * @type: type of plane (overlay, primary, cursor)
1239 * @name: printf style format string for the plane name, or NULL for default name
1241 * Initializes a plane object of type @type.
1244 * Zero on success, error code on failure.
1246 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1247 unsigned long possible_crtcs,
1248 const struct drm_plane_funcs *funcs,
1249 const uint32_t *formats, unsigned int format_count,
1250 enum drm_plane_type type,
1251 const char *name, ...)
1253 struct drm_mode_config *config = &dev->mode_config;
1256 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1260 drm_modeset_lock_init(&plane->mutex);
1262 plane->base.properties = &plane->properties;
1264 plane->funcs = funcs;
1265 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1267 if (!plane->format_types) {
1268 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1269 drm_mode_object_unregister(dev, &plane->base);
1277 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1280 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1281 drm_num_planes(dev));
1284 kfree(plane->format_types);
1285 drm_mode_object_unregister(dev, &plane->base);
1289 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1290 plane->format_count = format_count;
1291 plane->possible_crtcs = possible_crtcs;
1294 list_add_tail(&plane->head, &config->plane_list);
1295 config->num_total_plane++;
1296 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1297 config->num_overlay_plane++;
1299 drm_object_attach_property(&plane->base,
1300 config->plane_type_property,
1303 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1304 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1305 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1306 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1307 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1308 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1309 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1310 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1311 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1312 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1313 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1318 EXPORT_SYMBOL(drm_universal_plane_init);
1321 * drm_plane_init - Initialize a legacy plane
1323 * @plane: plane object to init
1324 * @possible_crtcs: bitmask of possible CRTCs
1325 * @funcs: callbacks for the new plane
1326 * @formats: array of supported formats (%DRM_FORMAT_*)
1327 * @format_count: number of elements in @formats
1328 * @is_primary: plane type (primary vs overlay)
1330 * Legacy API to initialize a DRM plane.
1332 * New drivers should call drm_universal_plane_init() instead.
1335 * Zero on success, error code on failure.
1337 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1338 unsigned long possible_crtcs,
1339 const struct drm_plane_funcs *funcs,
1340 const uint32_t *formats, unsigned int format_count,
1343 enum drm_plane_type type;
1345 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1346 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1347 formats, format_count, type, NULL);
1349 EXPORT_SYMBOL(drm_plane_init);
1352 * drm_plane_cleanup - Clean up the core plane usage
1353 * @plane: plane to cleanup
1355 * This function cleans up @plane and removes it from the DRM mode setting
1356 * core. Note that the function does *not* free the plane structure itself,
1357 * this is the responsibility of the caller.
1359 void drm_plane_cleanup(struct drm_plane *plane)
1361 struct drm_device *dev = plane->dev;
1363 drm_modeset_lock_all(dev);
1364 kfree(plane->format_types);
1365 drm_mode_object_unregister(dev, &plane->base);
1367 BUG_ON(list_empty(&plane->head));
1369 list_del(&plane->head);
1370 dev->mode_config.num_total_plane--;
1371 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1372 dev->mode_config.num_overlay_plane--;
1373 drm_modeset_unlock_all(dev);
1375 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1376 if (plane->state && plane->funcs->atomic_destroy_state)
1377 plane->funcs->atomic_destroy_state(plane, plane->state);
1381 memset(plane, 0, sizeof(*plane));
1383 EXPORT_SYMBOL(drm_plane_cleanup);
1386 * drm_plane_index - find the index of a registered plane
1387 * @plane: plane to find index for
1389 * Given a registered plane, return the index of that CRTC within a DRM
1390 * device's list of planes.
1392 unsigned int drm_plane_index(struct drm_plane *plane)
1394 unsigned int index = 0;
1395 struct drm_plane *tmp;
1397 drm_for_each_plane(tmp, plane->dev) {
1406 EXPORT_SYMBOL(drm_plane_index);
1409 * drm_plane_from_index - find the registered plane at an index
1411 * @idx: index of registered plane to find for
1413 * Given a plane index, return the registered plane from DRM device's
1414 * list of planes with matching index.
1417 drm_plane_from_index(struct drm_device *dev, int idx)
1419 struct drm_plane *plane;
1422 drm_for_each_plane(plane, dev) {
1429 EXPORT_SYMBOL(drm_plane_from_index);
1432 * drm_plane_force_disable - Forcibly disable a plane
1433 * @plane: plane to disable
1435 * Forces the plane to be disabled.
1437 * Used when the plane's current framebuffer is destroyed,
1438 * and when restoring fbdev mode.
1440 void drm_plane_force_disable(struct drm_plane *plane)
1447 plane->old_fb = plane->fb;
1448 ret = plane->funcs->disable_plane(plane);
1450 DRM_ERROR("failed to disable plane with busy fb\n");
1451 plane->old_fb = NULL;
1454 /* disconnect the plane from the fb and crtc: */
1455 drm_framebuffer_unreference(plane->old_fb);
1456 plane->old_fb = NULL;
1460 EXPORT_SYMBOL(drm_plane_force_disable);
1462 static int drm_mode_create_standard_properties(struct drm_device *dev)
1464 struct drm_property *prop;
1467 * Standard properties (apply to all connectors)
1469 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1470 DRM_MODE_PROP_IMMUTABLE,
1474 dev->mode_config.edid_property = prop;
1476 prop = drm_property_create_enum(dev, 0,
1477 "DPMS", drm_dpms_enum_list,
1478 ARRAY_SIZE(drm_dpms_enum_list));
1481 dev->mode_config.dpms_property = prop;
1483 prop = drm_property_create(dev,
1484 DRM_MODE_PROP_BLOB |
1485 DRM_MODE_PROP_IMMUTABLE,
1489 dev->mode_config.path_property = prop;
1491 prop = drm_property_create(dev,
1492 DRM_MODE_PROP_BLOB |
1493 DRM_MODE_PROP_IMMUTABLE,
1497 dev->mode_config.tile_property = prop;
1499 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1500 "type", drm_plane_type_enum_list,
1501 ARRAY_SIZE(drm_plane_type_enum_list));
1504 dev->mode_config.plane_type_property = prop;
1506 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1507 "SRC_X", 0, UINT_MAX);
1510 dev->mode_config.prop_src_x = prop;
1512 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1513 "SRC_Y", 0, UINT_MAX);
1516 dev->mode_config.prop_src_y = prop;
1518 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1519 "SRC_W", 0, UINT_MAX);
1522 dev->mode_config.prop_src_w = prop;
1524 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1525 "SRC_H", 0, UINT_MAX);
1528 dev->mode_config.prop_src_h = prop;
1530 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1531 "CRTC_X", INT_MIN, INT_MAX);
1534 dev->mode_config.prop_crtc_x = prop;
1536 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1537 "CRTC_Y", INT_MIN, INT_MAX);
1540 dev->mode_config.prop_crtc_y = prop;
1542 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1543 "CRTC_W", 0, INT_MAX);
1546 dev->mode_config.prop_crtc_w = prop;
1548 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1549 "CRTC_H", 0, INT_MAX);
1552 dev->mode_config.prop_crtc_h = prop;
1554 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1555 "FB_ID", DRM_MODE_OBJECT_FB);
1558 dev->mode_config.prop_fb_id = prop;
1560 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1561 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1564 dev->mode_config.prop_crtc_id = prop;
1566 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1570 dev->mode_config.prop_active = prop;
1572 prop = drm_property_create(dev,
1573 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1577 dev->mode_config.prop_mode_id = prop;
1579 prop = drm_property_create(dev,
1584 dev->mode_config.degamma_lut_property = prop;
1586 prop = drm_property_create_range(dev,
1587 DRM_MODE_PROP_IMMUTABLE,
1588 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1591 dev->mode_config.degamma_lut_size_property = prop;
1593 prop = drm_property_create(dev,
1598 dev->mode_config.ctm_property = prop;
1600 prop = drm_property_create(dev,
1605 dev->mode_config.gamma_lut_property = prop;
1607 prop = drm_property_create_range(dev,
1608 DRM_MODE_PROP_IMMUTABLE,
1609 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1612 dev->mode_config.gamma_lut_size_property = prop;
1618 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1621 * Called by a driver the first time a DVI-I connector is made.
1623 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1625 struct drm_property *dvi_i_selector;
1626 struct drm_property *dvi_i_subconnector;
1628 if (dev->mode_config.dvi_i_select_subconnector_property)
1632 drm_property_create_enum(dev, 0,
1633 "select subconnector",
1634 drm_dvi_i_select_enum_list,
1635 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1636 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1638 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1640 drm_dvi_i_subconnector_enum_list,
1641 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1642 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1646 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1649 * drm_create_tv_properties - create TV specific connector properties
1651 * @num_modes: number of different TV formats (modes) supported
1652 * @modes: array of pointers to strings containing name of each format
1654 * Called by a driver's TV initialization routine, this function creates
1655 * the TV specific connector properties for a given device. Caller is
1656 * responsible for allocating a list of format names and passing them to
1659 int drm_mode_create_tv_properties(struct drm_device *dev,
1660 unsigned int num_modes,
1661 const char * const modes[])
1663 struct drm_property *tv_selector;
1664 struct drm_property *tv_subconnector;
1667 if (dev->mode_config.tv_select_subconnector_property)
1671 * Basic connector properties
1673 tv_selector = drm_property_create_enum(dev, 0,
1674 "select subconnector",
1675 drm_tv_select_enum_list,
1676 ARRAY_SIZE(drm_tv_select_enum_list));
1680 dev->mode_config.tv_select_subconnector_property = tv_selector;
1683 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1685 drm_tv_subconnector_enum_list,
1686 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1687 if (!tv_subconnector)
1689 dev->mode_config.tv_subconnector_property = tv_subconnector;
1692 * Other, TV specific properties: margins & TV modes.
1694 dev->mode_config.tv_left_margin_property =
1695 drm_property_create_range(dev, 0, "left margin", 0, 100);
1696 if (!dev->mode_config.tv_left_margin_property)
1699 dev->mode_config.tv_right_margin_property =
1700 drm_property_create_range(dev, 0, "right margin", 0, 100);
1701 if (!dev->mode_config.tv_right_margin_property)
1704 dev->mode_config.tv_top_margin_property =
1705 drm_property_create_range(dev, 0, "top margin", 0, 100);
1706 if (!dev->mode_config.tv_top_margin_property)
1709 dev->mode_config.tv_bottom_margin_property =
1710 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1711 if (!dev->mode_config.tv_bottom_margin_property)
1714 dev->mode_config.tv_mode_property =
1715 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1717 if (!dev->mode_config.tv_mode_property)
1720 for (i = 0; i < num_modes; i++)
1721 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1724 dev->mode_config.tv_brightness_property =
1725 drm_property_create_range(dev, 0, "brightness", 0, 100);
1726 if (!dev->mode_config.tv_brightness_property)
1729 dev->mode_config.tv_contrast_property =
1730 drm_property_create_range(dev, 0, "contrast", 0, 100);
1731 if (!dev->mode_config.tv_contrast_property)
1734 dev->mode_config.tv_flicker_reduction_property =
1735 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1736 if (!dev->mode_config.tv_flicker_reduction_property)
1739 dev->mode_config.tv_overscan_property =
1740 drm_property_create_range(dev, 0, "overscan", 0, 100);
1741 if (!dev->mode_config.tv_overscan_property)
1744 dev->mode_config.tv_saturation_property =
1745 drm_property_create_range(dev, 0, "saturation", 0, 100);
1746 if (!dev->mode_config.tv_saturation_property)
1749 dev->mode_config.tv_hue_property =
1750 drm_property_create_range(dev, 0, "hue", 0, 100);
1751 if (!dev->mode_config.tv_hue_property)
1758 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1761 * drm_mode_create_scaling_mode_property - create scaling mode property
1764 * Called by a driver the first time it's needed, must be attached to desired
1767 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1769 struct drm_property *scaling_mode;
1771 if (dev->mode_config.scaling_mode_property)
1775 drm_property_create_enum(dev, 0, "scaling mode",
1776 drm_scaling_mode_enum_list,
1777 ARRAY_SIZE(drm_scaling_mode_enum_list));
1779 dev->mode_config.scaling_mode_property = scaling_mode;
1783 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1786 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1789 * Called by a driver the first time it's needed, must be attached to desired
1793 * Zero on success, negative errno on failure.
1795 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1797 if (dev->mode_config.aspect_ratio_property)
1800 dev->mode_config.aspect_ratio_property =
1801 drm_property_create_enum(dev, 0, "aspect ratio",
1802 drm_aspect_ratio_enum_list,
1803 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1805 if (dev->mode_config.aspect_ratio_property == NULL)
1810 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1813 * drm_mode_create_dirty_property - create dirty property
1816 * Called by a driver the first time it's needed, must be attached to desired
1819 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1821 struct drm_property *dirty_info;
1823 if (dev->mode_config.dirty_info_property)
1827 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1829 drm_dirty_info_enum_list,
1830 ARRAY_SIZE(drm_dirty_info_enum_list));
1831 dev->mode_config.dirty_info_property = dirty_info;
1835 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1838 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1841 * Create the the suggested x/y offset property for connectors.
1843 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1845 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1848 dev->mode_config.suggested_x_property =
1849 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1851 dev->mode_config.suggested_y_property =
1852 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1854 if (dev->mode_config.suggested_x_property == NULL ||
1855 dev->mode_config.suggested_y_property == NULL)
1859 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1862 * drm_mode_getresources - get graphics configuration
1863 * @dev: drm device for the ioctl
1864 * @data: data pointer for the ioctl
1865 * @file_priv: drm file for the ioctl call
1867 * Construct a set of configuration description structures and return
1868 * them to the user, including CRTC, connector and framebuffer configuration.
1870 * Called by the user via ioctl.
1873 * Zero on success, negative errno on failure.
1875 int drm_mode_getresources(struct drm_device *dev, void *data,
1876 struct drm_file *file_priv)
1878 struct drm_mode_card_res *card_res = data;
1879 struct list_head *lh;
1880 struct drm_framebuffer *fb;
1881 struct drm_connector *connector;
1882 struct drm_crtc *crtc;
1883 struct drm_encoder *encoder;
1885 int connector_count = 0;
1888 int encoder_count = 0;
1890 uint32_t __user *fb_id;
1891 uint32_t __user *crtc_id;
1892 uint32_t __user *connector_id;
1893 uint32_t __user *encoder_id;
1895 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1899 mutex_lock(&file_priv->fbs_lock);
1901 * For the non-control nodes we need to limit the list of resources
1902 * by IDs in the group list for this node
1904 list_for_each(lh, &file_priv->fbs)
1907 /* handle this in 4 parts */
1909 if (card_res->count_fbs >= fb_count) {
1911 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1912 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1913 if (put_user(fb->base.id, fb_id + copied)) {
1914 mutex_unlock(&file_priv->fbs_lock);
1920 card_res->count_fbs = fb_count;
1921 mutex_unlock(&file_priv->fbs_lock);
1923 /* mode_config.mutex protects the connector list against e.g. DP MST
1924 * connector hot-adding. CRTC/Plane lists are invariant. */
1925 mutex_lock(&dev->mode_config.mutex);
1926 drm_for_each_crtc(crtc, dev)
1929 drm_for_each_connector(connector, dev)
1932 drm_for_each_encoder(encoder, dev)
1935 card_res->max_height = dev->mode_config.max_height;
1936 card_res->min_height = dev->mode_config.min_height;
1937 card_res->max_width = dev->mode_config.max_width;
1938 card_res->min_width = dev->mode_config.min_width;
1941 if (card_res->count_crtcs >= crtc_count) {
1943 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1944 drm_for_each_crtc(crtc, dev) {
1945 DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
1946 crtc->base.id, crtc->name);
1947 if (put_user(crtc->base.id, crtc_id + copied)) {
1954 card_res->count_crtcs = crtc_count;
1957 if (card_res->count_encoders >= encoder_count) {
1959 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1960 drm_for_each_encoder(encoder, dev) {
1961 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1963 if (put_user(encoder->base.id, encoder_id +
1971 card_res->count_encoders = encoder_count;
1974 if (card_res->count_connectors >= connector_count) {
1976 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1977 drm_for_each_connector(connector, dev) {
1978 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1981 if (put_user(connector->base.id,
1982 connector_id + copied)) {
1989 card_res->count_connectors = connector_count;
1991 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1992 card_res->count_connectors, card_res->count_encoders);
1995 mutex_unlock(&dev->mode_config.mutex);
2000 * drm_mode_getcrtc - get CRTC configuration
2001 * @dev: drm device for the ioctl
2002 * @data: data pointer for the ioctl
2003 * @file_priv: drm file for the ioctl call
2005 * Construct a CRTC configuration structure to return to the user.
2007 * Called by the user via ioctl.
2010 * Zero on success, negative errno on failure.
2012 int drm_mode_getcrtc(struct drm_device *dev,
2013 void *data, struct drm_file *file_priv)
2015 struct drm_mode_crtc *crtc_resp = data;
2016 struct drm_crtc *crtc;
2018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2021 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2025 drm_modeset_lock_crtc(crtc, crtc->primary);
2026 crtc_resp->gamma_size = crtc->gamma_size;
2027 if (crtc->primary->fb)
2028 crtc_resp->fb_id = crtc->primary->fb->base.id;
2030 crtc_resp->fb_id = 0;
2033 crtc_resp->x = crtc->primary->state->src_x >> 16;
2034 crtc_resp->y = crtc->primary->state->src_y >> 16;
2035 if (crtc->state->enable) {
2036 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2037 crtc_resp->mode_valid = 1;
2040 crtc_resp->mode_valid = 0;
2043 crtc_resp->x = crtc->x;
2044 crtc_resp->y = crtc->y;
2045 if (crtc->enabled) {
2046 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2047 crtc_resp->mode_valid = 1;
2050 crtc_resp->mode_valid = 0;
2053 drm_modeset_unlock_crtc(crtc);
2058 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2059 const struct drm_file *file_priv)
2062 * If user-space hasn't configured the driver to expose the stereo 3D
2063 * modes, don't expose them.
2065 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2071 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2073 /* For atomic drivers only state objects are synchronously updated and
2074 * protected by modeset locks, so check those first. */
2075 if (connector->state)
2076 return connector->state->best_encoder;
2077 return connector->encoder;
2080 /* helper for getconnector and getproperties ioctls */
2081 static int get_properties(struct drm_mode_object *obj, bool atomic,
2082 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2083 uint32_t *arg_count_props)
2088 props_count = obj->properties->count;
2090 props_count -= obj->properties->atomic_count;
2092 if ((*arg_count_props >= props_count) && props_count) {
2093 for (i = 0, copied = 0; copied < props_count; i++) {
2094 struct drm_property *prop = obj->properties->properties[i];
2097 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2100 ret = drm_object_property_get_value(obj, prop, &val);
2104 if (put_user(prop->base.id, prop_ptr + copied))
2107 if (put_user(val, prop_values + copied))
2113 *arg_count_props = props_count;
2119 * drm_mode_getconnector - get connector configuration
2120 * @dev: drm device for the ioctl
2121 * @data: data pointer for the ioctl
2122 * @file_priv: drm file for the ioctl call
2124 * Construct a connector configuration structure to return to the user.
2126 * Called by the user via ioctl.
2129 * Zero on success, negative errno on failure.
2131 int drm_mode_getconnector(struct drm_device *dev, void *data,
2132 struct drm_file *file_priv)
2134 struct drm_mode_get_connector *out_resp = data;
2135 struct drm_connector *connector;
2136 struct drm_encoder *encoder;
2137 struct drm_display_mode *mode;
2139 int encoders_count = 0;
2143 struct drm_mode_modeinfo u_mode;
2144 struct drm_mode_modeinfo __user *mode_ptr;
2145 uint32_t __user *encoder_ptr;
2147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2150 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2152 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2154 mutex_lock(&dev->mode_config.mutex);
2156 connector = drm_connector_find(dev, out_resp->connector_id);
2162 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2163 if (connector->encoder_ids[i] != 0)
2166 if (out_resp->count_modes == 0) {
2167 connector->funcs->fill_modes(connector,
2168 dev->mode_config.max_width,
2169 dev->mode_config.max_height);
2172 /* delayed so we get modes regardless of pre-fill_modes state */
2173 list_for_each_entry(mode, &connector->modes, head)
2174 if (drm_mode_expose_to_userspace(mode, file_priv))
2177 out_resp->connector_id = connector->base.id;
2178 out_resp->connector_type = connector->connector_type;
2179 out_resp->connector_type_id = connector->connector_type_id;
2180 out_resp->mm_width = connector->display_info.width_mm;
2181 out_resp->mm_height = connector->display_info.height_mm;
2182 out_resp->subpixel = connector->display_info.subpixel_order;
2183 out_resp->connection = connector->status;
2185 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2186 encoder = drm_connector_get_encoder(connector);
2188 out_resp->encoder_id = encoder->base.id;
2190 out_resp->encoder_id = 0;
2193 * This ioctl is called twice, once to determine how much space is
2194 * needed, and the 2nd time to fill it.
2196 if ((out_resp->count_modes >= mode_count) && mode_count) {
2198 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2199 list_for_each_entry(mode, &connector->modes, head) {
2200 if (!drm_mode_expose_to_userspace(mode, file_priv))
2203 drm_mode_convert_to_umode(&u_mode, mode);
2204 if (copy_to_user(mode_ptr + copied,
2205 &u_mode, sizeof(u_mode))) {
2212 out_resp->count_modes = mode_count;
2214 ret = get_properties(&connector->base, file_priv->atomic,
2215 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2216 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2217 &out_resp->count_props);
2221 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2223 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2224 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2225 if (connector->encoder_ids[i] != 0) {
2226 if (put_user(connector->encoder_ids[i],
2227 encoder_ptr + copied)) {
2235 out_resp->count_encoders = encoders_count;
2238 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2241 mutex_unlock(&dev->mode_config.mutex);
2246 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2248 struct drm_connector *connector;
2249 struct drm_device *dev = encoder->dev;
2250 bool uses_atomic = false;
2252 /* For atomic drivers only state objects are synchronously updated and
2253 * protected by modeset locks, so check those first. */
2254 drm_for_each_connector(connector, dev) {
2255 if (!connector->state)
2260 if (connector->state->best_encoder != encoder)
2263 return connector->state->crtc;
2266 /* Don't return stale data (e.g. pending async disable). */
2270 return encoder->crtc;
2274 * drm_mode_getencoder - get encoder configuration
2275 * @dev: drm device for the ioctl
2276 * @data: data pointer for the ioctl
2277 * @file_priv: drm file for the ioctl call
2279 * Construct a encoder configuration structure to return to the user.
2281 * Called by the user via ioctl.
2284 * Zero on success, negative errno on failure.
2286 int drm_mode_getencoder(struct drm_device *dev, void *data,
2287 struct drm_file *file_priv)
2289 struct drm_mode_get_encoder *enc_resp = data;
2290 struct drm_encoder *encoder;
2291 struct drm_crtc *crtc;
2293 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2296 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2300 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2301 crtc = drm_encoder_get_crtc(encoder);
2303 enc_resp->crtc_id = crtc->base.id;
2305 enc_resp->crtc_id = 0;
2306 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2308 enc_resp->encoder_type = encoder->encoder_type;
2309 enc_resp->encoder_id = encoder->base.id;
2310 enc_resp->possible_crtcs = encoder->possible_crtcs;
2311 enc_resp->possible_clones = encoder->possible_clones;
2317 * drm_mode_getplane_res - enumerate all plane resources
2320 * @file_priv: DRM file info
2322 * Construct a list of plane ids to return to the user.
2324 * Called by the user via ioctl.
2327 * Zero on success, negative errno on failure.
2329 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2330 struct drm_file *file_priv)
2332 struct drm_mode_get_plane_res *plane_resp = data;
2333 struct drm_mode_config *config;
2334 struct drm_plane *plane;
2335 uint32_t __user *plane_ptr;
2337 unsigned num_planes;
2339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2342 config = &dev->mode_config;
2344 if (file_priv->universal_planes)
2345 num_planes = config->num_total_plane;
2347 num_planes = config->num_overlay_plane;
2350 * This ioctl is called twice, once to determine how much space is
2351 * needed, and the 2nd time to fill it.
2354 (plane_resp->count_planes >= num_planes)) {
2355 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2357 /* Plane lists are invariant, no locking needed. */
2358 drm_for_each_plane(plane, dev) {
2360 * Unless userspace set the 'universal planes'
2361 * capability bit, only advertise overlays.
2363 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2364 !file_priv->universal_planes)
2367 if (put_user(plane->base.id, plane_ptr + copied))
2372 plane_resp->count_planes = num_planes;
2378 * drm_mode_getplane - get plane configuration
2381 * @file_priv: DRM file info
2383 * Construct a plane configuration structure to return to the user.
2385 * Called by the user via ioctl.
2388 * Zero on success, negative errno on failure.
2390 int drm_mode_getplane(struct drm_device *dev, void *data,
2391 struct drm_file *file_priv)
2393 struct drm_mode_get_plane *plane_resp = data;
2394 struct drm_plane *plane;
2395 uint32_t __user *format_ptr;
2397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2400 plane = drm_plane_find(dev, plane_resp->plane_id);
2404 drm_modeset_lock(&plane->mutex, NULL);
2406 plane_resp->crtc_id = plane->crtc->base.id;
2408 plane_resp->crtc_id = 0;
2411 plane_resp->fb_id = plane->fb->base.id;
2413 plane_resp->fb_id = 0;
2414 drm_modeset_unlock(&plane->mutex);
2416 plane_resp->plane_id = plane->base.id;
2417 plane_resp->possible_crtcs = plane->possible_crtcs;
2418 plane_resp->gamma_size = 0;
2421 * This ioctl is called twice, once to determine how much space is
2422 * needed, and the 2nd time to fill it.
2424 if (plane->format_count &&
2425 (plane_resp->count_format_types >= plane->format_count)) {
2426 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2427 if (copy_to_user(format_ptr,
2428 plane->format_types,
2429 sizeof(uint32_t) * plane->format_count)) {
2433 plane_resp->count_format_types = plane->format_count;
2439 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2440 * @plane: plane to check for format support
2441 * @format: the pixel format
2444 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2447 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2451 for (i = 0; i < plane->format_count; i++) {
2452 if (format == plane->format_types[i])
2459 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2460 uint32_t src_w, uint32_t src_h,
2461 const struct drm_framebuffer *fb)
2463 unsigned int fb_width, fb_height;
2465 fb_width = fb->width << 16;
2466 fb_height = fb->height << 16;
2468 /* Make sure source coordinates are inside the fb. */
2469 if (src_w > fb_width ||
2470 src_x > fb_width - src_w ||
2471 src_h > fb_height ||
2472 src_y > fb_height - src_h) {
2473 DRM_DEBUG_KMS("Invalid source coordinates "
2474 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2475 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2476 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2477 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2478 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2486 * setplane_internal - setplane handler for internal callers
2488 * Note that we assume an extra reference has already been taken on fb. If the
2489 * update fails, this reference will be dropped before return; if it succeeds,
2490 * the previous framebuffer (if any) will be unreferenced instead.
2492 * src_{x,y,w,h} are provided in 16.16 fixed point format
2494 static int __setplane_internal(struct drm_plane *plane,
2495 struct drm_crtc *crtc,
2496 struct drm_framebuffer *fb,
2497 int32_t crtc_x, int32_t crtc_y,
2498 uint32_t crtc_w, uint32_t crtc_h,
2499 /* src_{x,y,w,h} values are 16.16 fixed point */
2500 uint32_t src_x, uint32_t src_y,
2501 uint32_t src_w, uint32_t src_h)
2505 /* No fb means shut it down */
2507 plane->old_fb = plane->fb;
2508 ret = plane->funcs->disable_plane(plane);
2513 plane->old_fb = NULL;
2518 /* Check whether this plane is usable on this CRTC */
2519 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2520 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2525 /* Check whether this plane supports the fb pixel format. */
2526 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2528 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2529 drm_get_format_name(fb->pixel_format));
2533 /* Give drivers some help against integer overflows */
2534 if (crtc_w > INT_MAX ||
2535 crtc_x > INT_MAX - (int32_t) crtc_w ||
2537 crtc_y > INT_MAX - (int32_t) crtc_h) {
2538 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2539 crtc_w, crtc_h, crtc_x, crtc_y);
2544 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2548 plane->old_fb = plane->fb;
2549 ret = plane->funcs->update_plane(plane, crtc, fb,
2550 crtc_x, crtc_y, crtc_w, crtc_h,
2551 src_x, src_y, src_w, src_h);
2557 plane->old_fb = NULL;
2562 drm_framebuffer_unreference(fb);
2564 drm_framebuffer_unreference(plane->old_fb);
2565 plane->old_fb = NULL;
2570 static int setplane_internal(struct drm_plane *plane,
2571 struct drm_crtc *crtc,
2572 struct drm_framebuffer *fb,
2573 int32_t crtc_x, int32_t crtc_y,
2574 uint32_t crtc_w, uint32_t crtc_h,
2575 /* src_{x,y,w,h} values are 16.16 fixed point */
2576 uint32_t src_x, uint32_t src_y,
2577 uint32_t src_w, uint32_t src_h)
2581 drm_modeset_lock_all(plane->dev);
2582 ret = __setplane_internal(plane, crtc, fb,
2583 crtc_x, crtc_y, crtc_w, crtc_h,
2584 src_x, src_y, src_w, src_h);
2585 drm_modeset_unlock_all(plane->dev);
2591 * drm_mode_setplane - configure a plane's configuration
2593 * @data: ioctl data*
2594 * @file_priv: DRM file info
2596 * Set plane configuration, including placement, fb, scaling, and other factors.
2597 * Or pass a NULL fb to disable (planes may be disabled without providing a
2601 * Zero on success, negative errno on failure.
2603 int drm_mode_setplane(struct drm_device *dev, void *data,
2604 struct drm_file *file_priv)
2606 struct drm_mode_set_plane *plane_req = data;
2607 struct drm_plane *plane;
2608 struct drm_crtc *crtc = NULL;
2609 struct drm_framebuffer *fb = NULL;
2611 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2615 * First, find the plane, crtc, and fb objects. If not available,
2616 * we don't bother to call the driver.
2618 plane = drm_plane_find(dev, plane_req->plane_id);
2620 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2621 plane_req->plane_id);
2625 if (plane_req->fb_id) {
2626 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2628 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2633 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2635 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2636 plane_req->crtc_id);
2642 * setplane_internal will take care of deref'ing either the old or new
2643 * framebuffer depending on success.
2645 return setplane_internal(plane, crtc, fb,
2646 plane_req->crtc_x, plane_req->crtc_y,
2647 plane_req->crtc_w, plane_req->crtc_h,
2648 plane_req->src_x, plane_req->src_y,
2649 plane_req->src_w, plane_req->src_h);
2653 * drm_mode_set_config_internal - helper to call ->set_config
2654 * @set: modeset config to set
2656 * This is a little helper to wrap internal calls to the ->set_config driver
2657 * interface. The only thing it adds is correct refcounting dance.
2660 * Zero on success, negative errno on failure.
2662 int drm_mode_set_config_internal(struct drm_mode_set *set)
2664 struct drm_crtc *crtc = set->crtc;
2665 struct drm_framebuffer *fb;
2666 struct drm_crtc *tmp;
2670 * NOTE: ->set_config can also disable other crtcs (if we steal all
2671 * connectors from it), hence we need to refcount the fbs across all
2672 * crtcs. Atomic modeset will have saner semantics ...
2674 drm_for_each_crtc(tmp, crtc->dev)
2675 tmp->primary->old_fb = tmp->primary->fb;
2679 ret = crtc->funcs->set_config(set);
2681 crtc->primary->crtc = crtc;
2682 crtc->primary->fb = fb;
2685 drm_for_each_crtc(tmp, crtc->dev) {
2686 if (tmp->primary->fb)
2687 drm_framebuffer_reference(tmp->primary->fb);
2688 if (tmp->primary->old_fb)
2689 drm_framebuffer_unreference(tmp->primary->old_fb);
2690 tmp->primary->old_fb = NULL;
2695 EXPORT_SYMBOL(drm_mode_set_config_internal);
2698 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2699 * @mode: mode to query
2700 * @hdisplay: hdisplay value to fill in
2701 * @vdisplay: vdisplay value to fill in
2703 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2704 * the appropriate layout.
2706 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2707 int *hdisplay, int *vdisplay)
2709 struct drm_display_mode adjusted;
2711 drm_mode_copy(&adjusted, mode);
2712 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2713 *hdisplay = adjusted.crtc_hdisplay;
2714 *vdisplay = adjusted.crtc_vdisplay;
2716 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2719 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2721 * @crtc: CRTC that framebuffer will be displayed on
2724 * @mode: mode that framebuffer will be displayed under
2725 * @fb: framebuffer to check size of
2727 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2729 const struct drm_display_mode *mode,
2730 const struct drm_framebuffer *fb)
2733 int hdisplay, vdisplay;
2735 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2738 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2739 BIT(DRM_ROTATE_270)))
2740 swap(hdisplay, vdisplay);
2742 return check_src_coords(x << 16, y << 16,
2743 hdisplay << 16, vdisplay << 16, fb);
2745 EXPORT_SYMBOL(drm_crtc_check_viewport);
2748 * drm_mode_setcrtc - set CRTC configuration
2749 * @dev: drm device for the ioctl
2750 * @data: data pointer for the ioctl
2751 * @file_priv: drm file for the ioctl call
2753 * Build a new CRTC configuration based on user request.
2755 * Called by the user via ioctl.
2758 * Zero on success, negative errno on failure.
2760 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2761 struct drm_file *file_priv)
2763 struct drm_mode_config *config = &dev->mode_config;
2764 struct drm_mode_crtc *crtc_req = data;
2765 struct drm_crtc *crtc;
2766 struct drm_connector **connector_set = NULL, *connector;
2767 struct drm_framebuffer *fb = NULL;
2768 struct drm_display_mode *mode = NULL;
2769 struct drm_mode_set set;
2770 uint32_t __user *set_connectors_ptr;
2774 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2778 * Universal plane src offsets are only 16.16, prevent havoc for
2779 * drivers using universal plane code internally.
2781 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2784 drm_modeset_lock_all(dev);
2785 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2787 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2791 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2793 if (crtc_req->mode_valid) {
2794 /* If we have a mode we need a framebuffer. */
2795 /* If we pass -1, set the mode with the currently bound fb */
2796 if (crtc_req->fb_id == -1) {
2797 if (!crtc->primary->fb) {
2798 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2802 fb = crtc->primary->fb;
2803 /* Make refcounting symmetric with the lookup path. */
2804 drm_framebuffer_reference(fb);
2806 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2808 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2815 mode = drm_mode_create(dev);
2821 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2823 DRM_DEBUG_KMS("Invalid mode\n");
2827 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2830 * Check whether the primary plane supports the fb pixel format.
2831 * Drivers not implementing the universal planes API use a
2832 * default formats list provided by the DRM core which doesn't
2833 * match real hardware capabilities. Skip the check in that
2836 if (!crtc->primary->format_default) {
2837 ret = drm_plane_check_pixel_format(crtc->primary,
2840 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2841 drm_get_format_name(fb->pixel_format));
2846 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2853 if (crtc_req->count_connectors == 0 && mode) {
2854 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2859 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2860 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2861 crtc_req->count_connectors);
2866 if (crtc_req->count_connectors > 0) {
2869 /* Avoid unbounded kernel memory allocation */
2870 if (crtc_req->count_connectors > config->num_connector) {
2875 connector_set = kmalloc_array(crtc_req->count_connectors,
2876 sizeof(struct drm_connector *),
2878 if (!connector_set) {
2883 for (i = 0; i < crtc_req->count_connectors; i++) {
2884 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2885 if (get_user(out_id, &set_connectors_ptr[i])) {
2890 connector = drm_connector_find(dev, out_id);
2892 DRM_DEBUG_KMS("Connector id %d unknown\n",
2897 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2901 connector_set[i] = connector;
2906 set.x = crtc_req->x;
2907 set.y = crtc_req->y;
2909 set.connectors = connector_set;
2910 set.num_connectors = crtc_req->count_connectors;
2912 ret = drm_mode_set_config_internal(&set);
2916 drm_framebuffer_unreference(fb);
2918 kfree(connector_set);
2919 drm_mode_destroy(dev, mode);
2920 drm_modeset_unlock_all(dev);
2925 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2926 * universal plane handler call
2927 * @crtc: crtc to update cursor for
2928 * @req: data pointer for the ioctl
2929 * @file_priv: drm file for the ioctl call
2931 * Legacy cursor ioctl's work directly with driver buffer handles. To
2932 * translate legacy ioctl calls into universal plane handler calls, we need to
2933 * wrap the native buffer handle in a drm_framebuffer.
2935 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2936 * buffer with a pitch of 4*width; the universal plane interface should be used
2937 * directly in cases where the hardware can support other buffer settings and
2938 * userspace wants to make use of these capabilities.
2941 * Zero on success, negative errno on failure.
2943 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2944 struct drm_mode_cursor2 *req,
2945 struct drm_file *file_priv)
2947 struct drm_device *dev = crtc->dev;
2948 struct drm_framebuffer *fb = NULL;
2949 struct drm_mode_fb_cmd2 fbreq = {
2950 .width = req->width,
2951 .height = req->height,
2952 .pixel_format = DRM_FORMAT_ARGB8888,
2953 .pitches = { req->width * 4 },
2954 .handles = { req->handle },
2956 int32_t crtc_x, crtc_y;
2957 uint32_t crtc_w = 0, crtc_h = 0;
2958 uint32_t src_w = 0, src_h = 0;
2961 BUG_ON(!crtc->cursor);
2962 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2965 * Obtain fb we'll be using (either new or existing) and take an extra
2966 * reference to it if fb != null. setplane will take care of dropping
2967 * the reference if the plane update fails.
2969 if (req->flags & DRM_MODE_CURSOR_BO) {
2971 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2973 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2980 fb = crtc->cursor->fb;
2982 drm_framebuffer_reference(fb);
2985 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2989 crtc_x = crtc->cursor_x;
2990 crtc_y = crtc->cursor_y;
2995 crtc_h = fb->height;
2996 src_w = fb->width << 16;
2997 src_h = fb->height << 16;
3001 * setplane_internal will take care of deref'ing either the old or new
3002 * framebuffer depending on success.
3004 ret = __setplane_internal(crtc->cursor, crtc, fb,
3005 crtc_x, crtc_y, crtc_w, crtc_h,
3006 0, 0, src_w, src_h);
3008 /* Update successful; save new cursor position, if necessary */
3009 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
3010 crtc->cursor_x = req->x;
3011 crtc->cursor_y = req->y;
3017 static int drm_mode_cursor_common(struct drm_device *dev,
3018 struct drm_mode_cursor2 *req,
3019 struct drm_file *file_priv)
3021 struct drm_crtc *crtc;
3024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3027 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3030 crtc = drm_crtc_find(dev, req->crtc_id);
3032 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3037 * If this crtc has a universal cursor plane, call that plane's update
3038 * handler rather than using legacy cursor handlers.
3040 drm_modeset_lock_crtc(crtc, crtc->cursor);
3042 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3046 if (req->flags & DRM_MODE_CURSOR_BO) {
3047 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3051 /* Turns off the cursor if handle is 0 */
3052 if (crtc->funcs->cursor_set2)
3053 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3054 req->width, req->height, req->hot_x, req->hot_y);
3056 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3057 req->width, req->height);
3060 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3061 if (crtc->funcs->cursor_move) {
3062 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3069 drm_modeset_unlock_crtc(crtc);
3077 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3078 * @dev: drm device for the ioctl
3079 * @data: data pointer for the ioctl
3080 * @file_priv: drm file for the ioctl call
3082 * Set the cursor configuration based on user request.
3084 * Called by the user via ioctl.
3087 * Zero on success, negative errno on failure.
3089 int drm_mode_cursor_ioctl(struct drm_device *dev,
3090 void *data, struct drm_file *file_priv)
3092 struct drm_mode_cursor *req = data;
3093 struct drm_mode_cursor2 new_req;
3095 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3096 new_req.hot_x = new_req.hot_y = 0;
3098 return drm_mode_cursor_common(dev, &new_req, file_priv);
3102 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3103 * @dev: drm device for the ioctl
3104 * @data: data pointer for the ioctl
3105 * @file_priv: drm file for the ioctl call
3107 * Set the cursor configuration based on user request. This implements the 2nd
3108 * version of the cursor ioctl, which allows userspace to additionally specify
3109 * the hotspot of the pointer.
3111 * Called by the user via ioctl.
3114 * Zero on success, negative errno on failure.
3116 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3117 void *data, struct drm_file *file_priv)
3119 struct drm_mode_cursor2 *req = data;
3121 return drm_mode_cursor_common(dev, req, file_priv);
3125 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3126 * @bpp: bits per pixels
3127 * @depth: bit depth per pixel
3129 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3130 * Useful in fbdev emulation code, since that deals in those values.
3132 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3138 fmt = DRM_FORMAT_C8;
3142 fmt = DRM_FORMAT_XRGB1555;
3144 fmt = DRM_FORMAT_RGB565;
3147 fmt = DRM_FORMAT_RGB888;
3151 fmt = DRM_FORMAT_XRGB8888;
3152 else if (depth == 30)
3153 fmt = DRM_FORMAT_XRGB2101010;
3155 fmt = DRM_FORMAT_ARGB8888;
3158 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3159 fmt = DRM_FORMAT_XRGB8888;
3165 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3168 * drm_mode_addfb - add an FB to the graphics configuration
3169 * @dev: drm device for the ioctl
3170 * @data: data pointer for the ioctl
3171 * @file_priv: drm file for the ioctl call
3173 * Add a new FB to the specified CRTC, given a user request. This is the
3174 * original addfb ioctl which only supported RGB formats.
3176 * Called by the user via ioctl.
3179 * Zero on success, negative errno on failure.
3181 int drm_mode_addfb(struct drm_device *dev,
3182 void *data, struct drm_file *file_priv)
3184 struct drm_mode_fb_cmd *or = data;
3185 struct drm_mode_fb_cmd2 r = {};
3188 /* convert to new format and call new ioctl */
3189 r.fb_id = or->fb_id;
3190 r.width = or->width;
3191 r.height = or->height;
3192 r.pitches[0] = or->pitch;
3193 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3194 r.handles[0] = or->handle;
3196 ret = drm_mode_addfb2(dev, &r, file_priv);
3200 or->fb_id = r.fb_id;
3205 static int format_check(const struct drm_mode_fb_cmd2 *r)
3207 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3211 case DRM_FORMAT_RGB332:
3212 case DRM_FORMAT_BGR233:
3213 case DRM_FORMAT_XRGB4444:
3214 case DRM_FORMAT_XBGR4444:
3215 case DRM_FORMAT_RGBX4444:
3216 case DRM_FORMAT_BGRX4444:
3217 case DRM_FORMAT_ARGB4444:
3218 case DRM_FORMAT_ABGR4444:
3219 case DRM_FORMAT_RGBA4444:
3220 case DRM_FORMAT_BGRA4444:
3221 case DRM_FORMAT_XRGB1555:
3222 case DRM_FORMAT_XBGR1555:
3223 case DRM_FORMAT_RGBX5551:
3224 case DRM_FORMAT_BGRX5551:
3225 case DRM_FORMAT_ARGB1555:
3226 case DRM_FORMAT_ABGR1555:
3227 case DRM_FORMAT_RGBA5551:
3228 case DRM_FORMAT_BGRA5551:
3229 case DRM_FORMAT_RGB565:
3230 case DRM_FORMAT_BGR565:
3231 case DRM_FORMAT_RGB888:
3232 case DRM_FORMAT_BGR888:
3233 case DRM_FORMAT_XRGB8888:
3234 case DRM_FORMAT_XBGR8888:
3235 case DRM_FORMAT_RGBX8888:
3236 case DRM_FORMAT_BGRX8888:
3237 case DRM_FORMAT_ARGB8888:
3238 case DRM_FORMAT_ABGR8888:
3239 case DRM_FORMAT_RGBA8888:
3240 case DRM_FORMAT_BGRA8888:
3241 case DRM_FORMAT_XRGB2101010:
3242 case DRM_FORMAT_XBGR2101010:
3243 case DRM_FORMAT_RGBX1010102:
3244 case DRM_FORMAT_BGRX1010102:
3245 case DRM_FORMAT_ARGB2101010:
3246 case DRM_FORMAT_ABGR2101010:
3247 case DRM_FORMAT_RGBA1010102:
3248 case DRM_FORMAT_BGRA1010102:
3249 case DRM_FORMAT_YUYV:
3250 case DRM_FORMAT_YVYU:
3251 case DRM_FORMAT_UYVY:
3252 case DRM_FORMAT_VYUY:
3253 case DRM_FORMAT_AYUV:
3254 case DRM_FORMAT_NV12:
3255 case DRM_FORMAT_NV21:
3256 case DRM_FORMAT_NV16:
3257 case DRM_FORMAT_NV61:
3258 case DRM_FORMAT_NV24:
3259 case DRM_FORMAT_NV42:
3260 case DRM_FORMAT_YUV410:
3261 case DRM_FORMAT_YVU410:
3262 case DRM_FORMAT_YUV411:
3263 case DRM_FORMAT_YVU411:
3264 case DRM_FORMAT_YUV420:
3265 case DRM_FORMAT_YVU420:
3266 case DRM_FORMAT_YUV422:
3267 case DRM_FORMAT_YVU422:
3268 case DRM_FORMAT_YUV444:
3269 case DRM_FORMAT_YVU444:
3272 DRM_DEBUG_KMS("invalid pixel format %s\n",
3273 drm_get_format_name(r->pixel_format));
3278 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3280 int ret, hsub, vsub, num_planes, i;
3282 ret = format_check(r);
3284 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3285 drm_get_format_name(r->pixel_format));
3289 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3290 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3291 num_planes = drm_format_num_planes(r->pixel_format);
3293 if (r->width == 0 || r->width % hsub) {
3294 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3298 if (r->height == 0 || r->height % vsub) {
3299 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3303 for (i = 0; i < num_planes; i++) {
3304 unsigned int width = r->width / (i != 0 ? hsub : 1);
3305 unsigned int height = r->height / (i != 0 ? vsub : 1);
3306 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3308 if (!r->handles[i]) {
3309 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3313 if ((uint64_t) width * cpp > UINT_MAX)
3316 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3319 if (r->pitches[i] < width * cpp) {
3320 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3324 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3325 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3330 /* modifier specific checks: */
3331 switch (r->modifier[i]) {
3332 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3333 /* NOTE: the pitch restriction may be lifted later if it turns
3334 * out that no hw has this restriction:
3336 if (r->pixel_format != DRM_FORMAT_NV12 ||
3337 width % 128 || height % 32 ||
3338 r->pitches[i] % 128) {
3339 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3349 for (i = num_planes; i < 4; i++) {
3350 if (r->modifier[i]) {
3351 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3355 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3356 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3359 if (r->handles[i]) {
3360 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3364 if (r->pitches[i]) {
3365 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3369 if (r->offsets[i]) {
3370 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3378 static struct drm_framebuffer *
3379 internal_framebuffer_create(struct drm_device *dev,
3380 const struct drm_mode_fb_cmd2 *r,
3381 struct drm_file *file_priv)
3383 struct drm_mode_config *config = &dev->mode_config;
3384 struct drm_framebuffer *fb;
3387 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3388 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3389 return ERR_PTR(-EINVAL);
3392 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3393 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3394 r->width, config->min_width, config->max_width);
3395 return ERR_PTR(-EINVAL);
3397 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3398 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3399 r->height, config->min_height, config->max_height);
3400 return ERR_PTR(-EINVAL);
3403 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3404 !dev->mode_config.allow_fb_modifiers) {
3405 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3406 return ERR_PTR(-EINVAL);
3409 ret = framebuffer_check(r);
3411 return ERR_PTR(ret);
3413 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3415 DRM_DEBUG_KMS("could not create framebuffer\n");
3423 * drm_mode_addfb2 - add an FB to the graphics configuration
3424 * @dev: drm device for the ioctl
3425 * @data: data pointer for the ioctl
3426 * @file_priv: drm file for the ioctl call
3428 * Add a new FB to the specified CRTC, given a user request with format. This is
3429 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3430 * and uses fourcc codes as pixel format specifiers.
3432 * Called by the user via ioctl.
3435 * Zero on success, negative errno on failure.
3437 int drm_mode_addfb2(struct drm_device *dev,
3438 void *data, struct drm_file *file_priv)
3440 struct drm_mode_fb_cmd2 *r = data;
3441 struct drm_framebuffer *fb;
3443 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3446 fb = internal_framebuffer_create(dev, r, file_priv);
3450 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3451 r->fb_id = fb->base.id;
3453 /* Transfer ownership to the filp for reaping on close */
3454 mutex_lock(&file_priv->fbs_lock);
3455 list_add(&fb->filp_head, &file_priv->fbs);
3456 mutex_unlock(&file_priv->fbs_lock);
3462 * drm_mode_rmfb - remove an FB from the configuration
3463 * @dev: drm device for the ioctl
3464 * @data: data pointer for the ioctl
3465 * @file_priv: drm file for the ioctl call
3467 * Remove the FB specified by the user.
3469 * Called by the user via ioctl.
3472 * Zero on success, negative errno on failure.
3474 int drm_mode_rmfb(struct drm_device *dev,
3475 void *data, struct drm_file *file_priv)
3477 struct drm_framebuffer *fb = NULL;
3478 struct drm_framebuffer *fbl = NULL;
3479 uint32_t *id = data;
3482 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3485 fb = drm_framebuffer_lookup(dev, *id);
3489 mutex_lock(&file_priv->fbs_lock);
3490 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3494 mutex_unlock(&file_priv->fbs_lock);
3498 list_del_init(&fb->filp_head);
3499 mutex_unlock(&file_priv->fbs_lock);
3501 /* we now own the reference that was stored in the fbs list */
3502 drm_framebuffer_unreference(fb);
3504 /* drop the reference we picked up in framebuffer lookup */
3505 drm_framebuffer_unreference(fb);
3510 drm_framebuffer_unreference(fb);
3515 * drm_mode_getfb - get FB info
3516 * @dev: drm device for the ioctl
3517 * @data: data pointer for the ioctl
3518 * @file_priv: drm file for the ioctl call
3520 * Lookup the FB given its ID and return info about it.
3522 * Called by the user via ioctl.
3525 * Zero on success, negative errno on failure.
3527 int drm_mode_getfb(struct drm_device *dev,
3528 void *data, struct drm_file *file_priv)
3530 struct drm_mode_fb_cmd *r = data;
3531 struct drm_framebuffer *fb;
3534 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3537 fb = drm_framebuffer_lookup(dev, r->fb_id);
3541 r->height = fb->height;
3542 r->width = fb->width;
3543 r->depth = fb->depth;
3544 r->bpp = fb->bits_per_pixel;
3545 r->pitch = fb->pitches[0];
3546 if (fb->funcs->create_handle) {
3547 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3548 drm_is_control_client(file_priv)) {
3549 ret = fb->funcs->create_handle(fb, file_priv,
3552 /* GET_FB() is an unprivileged ioctl so we must not
3553 * return a buffer-handle to non-master processes! For
3554 * backwards-compatibility reasons, we cannot make
3555 * GET_FB() privileged, so just return an invalid handle
3556 * for non-masters. */
3564 drm_framebuffer_unreference(fb);
3570 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3571 * @dev: drm device for the ioctl
3572 * @data: data pointer for the ioctl
3573 * @file_priv: drm file for the ioctl call
3575 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3576 * rectangle list. Generic userspace which does frontbuffer rendering must call
3577 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3578 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3580 * Modesetting drivers which always update the frontbuffer do not need to
3581 * implement the corresponding ->dirty framebuffer callback.
3583 * Called by the user via ioctl.
3586 * Zero on success, negative errno on failure.
3588 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3589 void *data, struct drm_file *file_priv)
3591 struct drm_clip_rect __user *clips_ptr;
3592 struct drm_clip_rect *clips = NULL;
3593 struct drm_mode_fb_dirty_cmd *r = data;
3594 struct drm_framebuffer *fb;
3599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3602 fb = drm_framebuffer_lookup(dev, r->fb_id);
3606 num_clips = r->num_clips;
3607 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3609 if (!num_clips != !clips_ptr) {
3614 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3616 /* If userspace annotates copy, clips must come in pairs */
3617 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3622 if (num_clips && clips_ptr) {
3623 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3627 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3633 ret = copy_from_user(clips, clips_ptr,
3634 num_clips * sizeof(*clips));
3641 if (fb->funcs->dirty) {
3642 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3651 drm_framebuffer_unreference(fb);
3658 * drm_fb_release - remove and free the FBs on this file
3659 * @priv: drm file for the ioctl
3661 * Destroy all the FBs associated with @filp.
3663 * Called by the user via ioctl.
3666 * Zero on success, negative errno on failure.
3668 void drm_fb_release(struct drm_file *priv)
3670 struct drm_framebuffer *fb, *tfb;
3673 * When the file gets released that means no one else can access the fb
3674 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3675 * avoid upsetting lockdep since the universal cursor code adds a
3676 * framebuffer while holding mutex locks.
3678 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3679 * locks is impossible here since no one else but this function can get
3682 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3683 list_del_init(&fb->filp_head);
3685 /* This drops the fpriv->fbs reference. */
3686 drm_framebuffer_unreference(fb);
3691 * drm_property_create - create a new property type
3693 * @flags: flags specifying the property type
3694 * @name: name of the property
3695 * @num_values: number of pre-defined values
3697 * This creates a new generic drm property which can then be attached to a drm
3698 * object with drm_object_attach_property. The returned property object must be
3699 * freed with drm_property_destroy.
3701 * Note that the DRM core keeps a per-device list of properties and that, if
3702 * drm_mode_config_cleanup() is called, it will destroy all properties created
3706 * A pointer to the newly created property on success, NULL on failure.
3708 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3709 const char *name, int num_values)
3711 struct drm_property *property = NULL;
3714 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3718 property->dev = dev;
3721 property->values = kcalloc(num_values, sizeof(uint64_t),
3723 if (!property->values)
3727 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3731 property->flags = flags;
3732 property->num_values = num_values;
3733 INIT_LIST_HEAD(&property->enum_list);
3736 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3737 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3740 list_add_tail(&property->head, &dev->mode_config.property_list);
3742 WARN_ON(!drm_property_type_valid(property));
3746 kfree(property->values);
3750 EXPORT_SYMBOL(drm_property_create);
3753 * drm_property_create_enum - create a new enumeration property type
3755 * @flags: flags specifying the property type
3756 * @name: name of the property
3757 * @props: enumeration lists with property values
3758 * @num_values: number of pre-defined values
3760 * This creates a new generic drm property which can then be attached to a drm
3761 * object with drm_object_attach_property. The returned property object must be
3762 * freed with drm_property_destroy.
3764 * Userspace is only allowed to set one of the predefined values for enumeration
3768 * A pointer to the newly created property on success, NULL on failure.
3770 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3772 const struct drm_prop_enum_list *props,
3775 struct drm_property *property;
3778 flags |= DRM_MODE_PROP_ENUM;
3780 property = drm_property_create(dev, flags, name, num_values);
3784 for (i = 0; i < num_values; i++) {
3785 ret = drm_property_add_enum(property, i,
3789 drm_property_destroy(dev, property);
3796 EXPORT_SYMBOL(drm_property_create_enum);
3799 * drm_property_create_bitmask - create a new bitmask property type
3801 * @flags: flags specifying the property type
3802 * @name: name of the property
3803 * @props: enumeration lists with property bitflags
3804 * @num_props: size of the @props array
3805 * @supported_bits: bitmask of all supported enumeration values
3807 * This creates a new bitmask drm property which can then be attached to a drm
3808 * object with drm_object_attach_property. The returned property object must be
3809 * freed with drm_property_destroy.
3811 * Compared to plain enumeration properties userspace is allowed to set any
3812 * or'ed together combination of the predefined property bitflag values
3815 * A pointer to the newly created property on success, NULL on failure.
3817 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3818 int flags, const char *name,
3819 const struct drm_prop_enum_list *props,
3821 uint64_t supported_bits)
3823 struct drm_property *property;
3824 int i, ret, index = 0;
3825 int num_values = hweight64(supported_bits);
3827 flags |= DRM_MODE_PROP_BITMASK;
3829 property = drm_property_create(dev, flags, name, num_values);
3832 for (i = 0; i < num_props; i++) {
3833 if (!(supported_bits & (1ULL << props[i].type)))
3836 if (WARN_ON(index >= num_values)) {
3837 drm_property_destroy(dev, property);
3841 ret = drm_property_add_enum(property, index++,
3845 drm_property_destroy(dev, property);
3852 EXPORT_SYMBOL(drm_property_create_bitmask);
3854 static struct drm_property *property_create_range(struct drm_device *dev,
3855 int flags, const char *name,
3856 uint64_t min, uint64_t max)
3858 struct drm_property *property;
3860 property = drm_property_create(dev, flags, name, 2);
3864 property->values[0] = min;
3865 property->values[1] = max;
3871 * drm_property_create_range - create a new unsigned ranged property type
3873 * @flags: flags specifying the property type
3874 * @name: name of the property
3875 * @min: minimum value of the property
3876 * @max: maximum value of the property
3878 * This creates a new generic drm property which can then be attached to a drm
3879 * object with drm_object_attach_property. The returned property object must be
3880 * freed with drm_property_destroy.
3882 * Userspace is allowed to set any unsigned integer value in the (min, max)
3886 * A pointer to the newly created property on success, NULL on failure.
3888 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3890 uint64_t min, uint64_t max)
3892 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3895 EXPORT_SYMBOL(drm_property_create_range);
3898 * drm_property_create_signed_range - create a new signed ranged property type
3900 * @flags: flags specifying the property type
3901 * @name: name of the property
3902 * @min: minimum value of the property
3903 * @max: maximum value of the property
3905 * This creates a new generic drm property which can then be attached to a drm
3906 * object with drm_object_attach_property. The returned property object must be
3907 * freed with drm_property_destroy.
3909 * Userspace is allowed to set any signed integer value in the (min, max)
3913 * A pointer to the newly created property on success, NULL on failure.
3915 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3916 int flags, const char *name,
3917 int64_t min, int64_t max)
3919 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3920 name, I642U64(min), I642U64(max));
3922 EXPORT_SYMBOL(drm_property_create_signed_range);
3925 * drm_property_create_object - create a new object property type
3927 * @flags: flags specifying the property type
3928 * @name: name of the property
3929 * @type: object type from DRM_MODE_OBJECT_* defines
3931 * This creates a new generic drm property which can then be attached to a drm
3932 * object with drm_object_attach_property. The returned property object must be
3933 * freed with drm_property_destroy.
3935 * Userspace is only allowed to set this to any property value of the given
3936 * @type. Only useful for atomic properties, which is enforced.
3939 * A pointer to the newly created property on success, NULL on failure.
3941 struct drm_property *drm_property_create_object(struct drm_device *dev,
3942 int flags, const char *name, uint32_t type)
3944 struct drm_property *property;
3946 flags |= DRM_MODE_PROP_OBJECT;
3948 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3951 property = drm_property_create(dev, flags, name, 1);
3955 property->values[0] = type;
3959 EXPORT_SYMBOL(drm_property_create_object);
3962 * drm_property_create_bool - create a new boolean property type
3964 * @flags: flags specifying the property type
3965 * @name: name of the property
3967 * This creates a new generic drm property which can then be attached to a drm
3968 * object with drm_object_attach_property. The returned property object must be
3969 * freed with drm_property_destroy.
3971 * This is implemented as a ranged property with only {0, 1} as valid values.
3974 * A pointer to the newly created property on success, NULL on failure.
3976 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3979 return drm_property_create_range(dev, flags, name, 0, 1);
3981 EXPORT_SYMBOL(drm_property_create_bool);
3984 * drm_property_add_enum - add a possible value to an enumeration property
3985 * @property: enumeration property to change
3986 * @index: index of the new enumeration
3987 * @value: value of the new enumeration
3988 * @name: symbolic name of the new enumeration
3990 * This functions adds enumerations to a property.
3992 * It's use is deprecated, drivers should use one of the more specific helpers
3993 * to directly create the property with all enumerations already attached.
3996 * Zero on success, error code on failure.
3998 int drm_property_add_enum(struct drm_property *property, int index,
3999 uint64_t value, const char *name)
4001 struct drm_property_enum *prop_enum;
4003 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4004 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
4008 * Bitmask enum properties have the additional constraint of values
4011 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4015 if (!list_empty(&property->enum_list)) {
4016 list_for_each_entry(prop_enum, &property->enum_list, head) {
4017 if (prop_enum->value == value) {
4018 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4019 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4025 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4029 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4030 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4031 prop_enum->value = value;
4033 property->values[index] = value;
4034 list_add_tail(&prop_enum->head, &property->enum_list);
4037 EXPORT_SYMBOL(drm_property_add_enum);
4040 * drm_property_destroy - destroy a drm property
4042 * @property: property to destry
4044 * This function frees a property including any attached resources like
4045 * enumeration values.
4047 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4049 struct drm_property_enum *prop_enum, *pt;
4051 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4052 list_del(&prop_enum->head);
4056 if (property->num_values)
4057 kfree(property->values);
4058 drm_mode_object_unregister(dev, &property->base);
4059 list_del(&property->head);
4062 EXPORT_SYMBOL(drm_property_destroy);
4065 * drm_object_attach_property - attach a property to a modeset object
4066 * @obj: drm modeset object
4067 * @property: property to attach
4068 * @init_val: initial value of the property
4070 * This attaches the given property to the modeset object with the given initial
4071 * value. Currently this function cannot fail since the properties are stored in
4072 * a statically sized array.
4074 void drm_object_attach_property(struct drm_mode_object *obj,
4075 struct drm_property *property,
4078 int count = obj->properties->count;
4080 if (count == DRM_OBJECT_MAX_PROPERTY) {
4081 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4082 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4083 "you see this message on the same object type.\n",
4088 obj->properties->properties[count] = property;
4089 obj->properties->values[count] = init_val;
4090 obj->properties->count++;
4091 if (property->flags & DRM_MODE_PROP_ATOMIC)
4092 obj->properties->atomic_count++;
4094 EXPORT_SYMBOL(drm_object_attach_property);
4097 * drm_object_property_set_value - set the value of a property
4098 * @obj: drm mode object to set property value for
4099 * @property: property to set
4100 * @val: value the property should be set to
4102 * This functions sets a given property on a given object. This function only
4103 * changes the software state of the property, it does not call into the
4104 * driver's ->set_property callback.
4107 * Zero on success, error code on failure.
4109 int drm_object_property_set_value(struct drm_mode_object *obj,
4110 struct drm_property *property, uint64_t val)
4114 for (i = 0; i < obj->properties->count; i++) {
4115 if (obj->properties->properties[i] == property) {
4116 obj->properties->values[i] = val;
4123 EXPORT_SYMBOL(drm_object_property_set_value);
4126 * drm_object_property_get_value - retrieve the value of a property
4127 * @obj: drm mode object to get property value from
4128 * @property: property to retrieve
4129 * @val: storage for the property value
4131 * This function retrieves the softare state of the given property for the given
4132 * property. Since there is no driver callback to retrieve the current property
4133 * value this might be out of sync with the hardware, depending upon the driver
4137 * Zero on success, error code on failure.
4139 int drm_object_property_get_value(struct drm_mode_object *obj,
4140 struct drm_property *property, uint64_t *val)
4144 /* read-only properties bypass atomic mechanism and still store
4145 * their value in obj->properties->values[].. mostly to avoid
4146 * having to deal w/ EDID and similar props in atomic paths:
4148 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4149 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4150 return drm_atomic_get_property(obj, property, val);
4152 for (i = 0; i < obj->properties->count; i++) {
4153 if (obj->properties->properties[i] == property) {
4154 *val = obj->properties->values[i];
4161 EXPORT_SYMBOL(drm_object_property_get_value);
4164 * drm_mode_getproperty_ioctl - get the property metadata
4167 * @file_priv: DRM file info
4169 * This function retrieves the metadata for a given property, like the different
4170 * possible values for an enum property or the limits for a range property.
4172 * Blob properties are special
4174 * Called by the user via ioctl.
4177 * Zero on success, negative errno on failure.
4179 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4180 void *data, struct drm_file *file_priv)
4182 struct drm_mode_get_property *out_resp = data;
4183 struct drm_property *property;
4185 int value_count = 0;
4188 struct drm_property_enum *prop_enum;
4189 struct drm_mode_property_enum __user *enum_ptr;
4190 uint64_t __user *values_ptr;
4192 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4195 drm_modeset_lock_all(dev);
4196 property = drm_property_find(dev, out_resp->prop_id);
4202 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4203 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4204 list_for_each_entry(prop_enum, &property->enum_list, head)
4208 value_count = property->num_values;
4210 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4211 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4212 out_resp->flags = property->flags;
4214 if ((out_resp->count_values >= value_count) && value_count) {
4215 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4216 for (i = 0; i < value_count; i++) {
4217 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4223 out_resp->count_values = value_count;
4225 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4226 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4227 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4229 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4230 list_for_each_entry(prop_enum, &property->enum_list, head) {
4232 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4237 if (copy_to_user(&enum_ptr[copied].name,
4238 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4245 out_resp->count_enum_blobs = enum_count;
4249 * NOTE: The idea seems to have been to use this to read all the blob
4250 * property values. But nothing ever added them to the corresponding
4251 * list, userspace always used the special-purpose get_blob ioctl to
4252 * read the value for a blob property. It also doesn't make a lot of
4253 * sense to return values here when everything else is just metadata for
4254 * the property itself.
4256 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4257 out_resp->count_enum_blobs = 0;
4259 drm_modeset_unlock_all(dev);
4264 * drm_property_create_blob - Create new blob property
4266 * Creates a new blob property for a specified DRM device, optionally
4269 * @dev: DRM device to create property for
4270 * @length: Length to allocate for blob data
4271 * @data: If specified, copies data into blob
4274 * New blob property with a single reference on success, or an ERR_PTR
4277 struct drm_property_blob *
4278 drm_property_create_blob(struct drm_device *dev, size_t length,
4281 struct drm_property_blob *blob;
4284 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4285 return ERR_PTR(-EINVAL);
4287 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4289 return ERR_PTR(-ENOMEM);
4291 /* This must be explicitly initialised, so we can safely call list_del
4292 * on it in the removal handler, even if it isn't in a file list. */
4293 INIT_LIST_HEAD(&blob->head_file);
4294 blob->length = length;
4298 memcpy(blob->data, data, length);
4300 mutex_lock(&dev->mode_config.blob_lock);
4302 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4305 mutex_unlock(&dev->mode_config.blob_lock);
4306 return ERR_PTR(-EINVAL);
4309 kref_init(&blob->refcount);
4311 list_add_tail(&blob->head_global,
4312 &dev->mode_config.property_blob_list);
4314 mutex_unlock(&dev->mode_config.blob_lock);
4318 EXPORT_SYMBOL(drm_property_create_blob);
4321 * drm_property_free_blob - Blob property destructor
4323 * Internal free function for blob properties; must not be used directly.
4327 static void drm_property_free_blob(struct kref *kref)
4329 struct drm_property_blob *blob =
4330 container_of(kref, struct drm_property_blob, refcount);
4332 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4334 list_del(&blob->head_global);
4335 list_del(&blob->head_file);
4336 drm_mode_object_unregister(blob->dev, &blob->base);
4342 * drm_property_unreference_blob - Unreference a blob property
4344 * Drop a reference on a blob property. May free the object.
4346 * @blob: Pointer to blob property
4348 void drm_property_unreference_blob(struct drm_property_blob *blob)
4350 struct drm_device *dev;
4357 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4359 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4360 &dev->mode_config.blob_lock))
4361 mutex_unlock(&dev->mode_config.blob_lock);
4363 might_lock(&dev->mode_config.blob_lock);
4365 EXPORT_SYMBOL(drm_property_unreference_blob);
4368 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4370 * Drop a reference on a blob property. May free the object. This must be
4371 * called with blob_lock held.
4373 * @blob: Pointer to blob property
4375 static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4380 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4382 kref_put(&blob->refcount, drm_property_free_blob);
4386 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4388 * @file_priv: destroy all blobs owned by this file handle
4390 void drm_property_destroy_user_blobs(struct drm_device *dev,
4391 struct drm_file *file_priv)
4393 struct drm_property_blob *blob, *bt;
4395 mutex_lock(&dev->mode_config.blob_lock);
4397 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4398 list_del_init(&blob->head_file);
4399 drm_property_unreference_blob_locked(blob);
4402 mutex_unlock(&dev->mode_config.blob_lock);
4406 * drm_property_reference_blob - Take a reference on an existing property
4408 * Take a new reference on an existing blob property.
4410 * @blob: Pointer to blob property
4412 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4414 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4415 kref_get(&blob->refcount);
4418 EXPORT_SYMBOL(drm_property_reference_blob);
4421 * Like drm_property_lookup_blob, but does not return an additional reference.
4422 * Must be called with blob_lock held.
4424 static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4427 struct drm_mode_object *obj = NULL;
4428 struct drm_property_blob *blob;
4430 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4432 mutex_lock(&dev->mode_config.idr_mutex);
4433 obj = idr_find(&dev->mode_config.crtc_idr, id);
4434 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4437 blob = obj_to_blob(obj);
4438 mutex_unlock(&dev->mode_config.idr_mutex);
4444 * drm_property_lookup_blob - look up a blob property and take a reference
4446 * @id: id of the blob property
4448 * If successful, this takes an additional reference to the blob property.
4449 * callers need to make sure to eventually unreference the returned property
4450 * again, using @drm_property_unreference_blob.
4452 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4455 struct drm_property_blob *blob;
4457 mutex_lock(&dev->mode_config.blob_lock);
4458 blob = __drm_property_lookup_blob(dev, id);
4460 if (!kref_get_unless_zero(&blob->refcount))
4463 mutex_unlock(&dev->mode_config.blob_lock);
4467 EXPORT_SYMBOL(drm_property_lookup_blob);
4470 * drm_property_replace_global_blob - atomically replace existing blob property
4472 * @replace: location of blob property pointer to be replaced
4473 * @length: length of data for new blob, or 0 for no data
4474 * @data: content for new blob, or NULL for no data
4475 * @obj_holds_id: optional object for property holding blob ID
4476 * @prop_holds_id: optional property holding blob ID
4477 * @return 0 on success or error on failure
4479 * This function will atomically replace a global property in the blob list,
4480 * optionally updating a property which holds the ID of that property. It is
4481 * guaranteed to be atomic: no caller will be allowed to see intermediate
4482 * results, and either the entire operation will succeed and clean up the
4483 * previous property, or it will fail and the state will be unchanged.
4485 * If length is 0 or data is NULL, no new blob will be created, and the holding
4486 * property, if specified, will be set to 0.
4488 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4489 * by holding the relevant modesetting object lock for its parent.
4491 * For example, a drm_connector has a 'PATH' property, which contains the ID
4492 * of a blob property with the value of the MST path information. Calling this
4493 * function with replace pointing to the connector's path_blob_ptr, length and
4494 * data set for the new path information, obj_holds_id set to the connector's
4495 * base object, and prop_holds_id set to the path property name, will perform
4496 * a completely atomic update. The access to path_blob_ptr is protected by the
4497 * caller holding a lock on the connector.
4499 static int drm_property_replace_global_blob(struct drm_device *dev,
4500 struct drm_property_blob **replace,
4503 struct drm_mode_object *obj_holds_id,
4504 struct drm_property *prop_holds_id)
4506 struct drm_property_blob *new_blob = NULL;
4507 struct drm_property_blob *old_blob = NULL;
4510 WARN_ON(replace == NULL);
4512 old_blob = *replace;
4514 if (length && data) {
4515 new_blob = drm_property_create_blob(dev, length, data);
4516 if (IS_ERR(new_blob))
4517 return PTR_ERR(new_blob);
4520 /* This does not need to be synchronised with blob_lock, as the
4521 * get_properties ioctl locks all modesetting objects, and
4522 * obj_holds_id must be locked before calling here, so we cannot
4523 * have its value out of sync with the list membership modified
4524 * below under blob_lock. */
4526 ret = drm_object_property_set_value(obj_holds_id,
4529 new_blob->base.id : 0);
4534 drm_property_unreference_blob(old_blob);
4535 *replace = new_blob;
4540 drm_property_unreference_blob(new_blob);
4545 * drm_mode_getblob_ioctl - get the contents of a blob property value
4548 * @file_priv: DRM file info
4550 * This function retrieves the contents of a blob property. The value stored in
4551 * an object's blob property is just a normal modeset object id.
4553 * Called by the user via ioctl.
4556 * Zero on success, negative errno on failure.
4558 int drm_mode_getblob_ioctl(struct drm_device *dev,
4559 void *data, struct drm_file *file_priv)
4561 struct drm_mode_get_blob *out_resp = data;
4562 struct drm_property_blob *blob;
4564 void __user *blob_ptr;
4566 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4569 drm_modeset_lock_all(dev);
4570 mutex_lock(&dev->mode_config.blob_lock);
4571 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4577 if (out_resp->length == blob->length) {
4578 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4579 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4584 out_resp->length = blob->length;
4587 mutex_unlock(&dev->mode_config.blob_lock);
4588 drm_modeset_unlock_all(dev);
4593 * drm_mode_createblob_ioctl - create a new blob property
4596 * @file_priv: DRM file info
4598 * This function creates a new blob property with user-defined values. In order
4599 * to give us sensible validation and checking when creating, rather than at
4600 * every potential use, we also require a type to be provided upfront.
4602 * Called by the user via ioctl.
4605 * Zero on success, negative errno on failure.
4607 int drm_mode_createblob_ioctl(struct drm_device *dev,
4608 void *data, struct drm_file *file_priv)
4610 struct drm_mode_create_blob *out_resp = data;
4611 struct drm_property_blob *blob;
4612 void __user *blob_ptr;
4615 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4618 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4620 return PTR_ERR(blob);
4622 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4623 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4628 /* Dropping the lock between create_blob and our access here is safe
4629 * as only the same file_priv can remove the blob; at this point, it is
4630 * not associated with any file_priv. */
4631 mutex_lock(&dev->mode_config.blob_lock);
4632 out_resp->blob_id = blob->base.id;
4633 list_add_tail(&blob->head_file, &file_priv->blobs);
4634 mutex_unlock(&dev->mode_config.blob_lock);
4639 drm_property_unreference_blob(blob);
4644 * drm_mode_destroyblob_ioctl - destroy a user blob property
4647 * @file_priv: DRM file info
4649 * Destroy an existing user-defined blob property.
4651 * Called by the user via ioctl.
4654 * Zero on success, negative errno on failure.
4656 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4657 void *data, struct drm_file *file_priv)
4659 struct drm_mode_destroy_blob *out_resp = data;
4660 struct drm_property_blob *blob = NULL, *bt;
4664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4667 mutex_lock(&dev->mode_config.blob_lock);
4668 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4674 /* Ensure the property was actually created by this user. */
4675 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4687 /* We must drop head_file here, because we may not be the last
4688 * reference on the blob. */
4689 list_del_init(&blob->head_file);
4690 drm_property_unreference_blob_locked(blob);
4691 mutex_unlock(&dev->mode_config.blob_lock);
4696 mutex_unlock(&dev->mode_config.blob_lock);
4701 * drm_mode_connector_set_path_property - set tile property on connector
4702 * @connector: connector to set property on.
4703 * @path: path to use for property; must not be NULL.
4705 * This creates a property to expose to userspace to specify a
4706 * connector path. This is mainly used for DisplayPort MST where
4707 * connectors have a topology and we want to allow userspace to give
4708 * them more meaningful names.
4711 * Zero on success, negative errno on failure.
4713 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4716 struct drm_device *dev = connector->dev;
4719 ret = drm_property_replace_global_blob(dev,
4720 &connector->path_blob_ptr,
4724 dev->mode_config.path_property);
4727 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4730 * drm_mode_connector_set_tile_property - set tile property on connector
4731 * @connector: connector to set property on.
4733 * This looks up the tile information for a connector, and creates a
4734 * property for userspace to parse if it exists. The property is of
4735 * the form of 8 integers using ':' as a separator.
4738 * Zero on success, errno on failure.
4740 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4742 struct drm_device *dev = connector->dev;
4746 if (!connector->has_tile) {
4747 ret = drm_property_replace_global_blob(dev,
4748 &connector->tile_blob_ptr,
4752 dev->mode_config.tile_property);
4756 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4757 connector->tile_group->id, connector->tile_is_single_monitor,
4758 connector->num_h_tile, connector->num_v_tile,
4759 connector->tile_h_loc, connector->tile_v_loc,
4760 connector->tile_h_size, connector->tile_v_size);
4762 ret = drm_property_replace_global_blob(dev,
4763 &connector->tile_blob_ptr,
4767 dev->mode_config.tile_property);
4770 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4773 * drm_mode_connector_update_edid_property - update the edid property of a connector
4774 * @connector: drm connector
4775 * @edid: new value of the edid property
4777 * This function creates a new blob modeset object and assigns its id to the
4778 * connector's edid property.
4781 * Zero on success, negative errno on failure.
4783 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4784 const struct edid *edid)
4786 struct drm_device *dev = connector->dev;
4790 /* ignore requests to set edid when overridden */
4791 if (connector->override_edid)
4795 size = EDID_LENGTH * (1 + edid->extensions);
4797 ret = drm_property_replace_global_blob(dev,
4798 &connector->edid_blob_ptr,
4802 dev->mode_config.edid_property);
4805 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4807 /* Some properties could refer to dynamic refcnt'd objects, or things that
4808 * need special locking to handle lifetime issues (ie. to ensure the prop
4809 * value doesn't become invalid part way through the property update due to
4810 * race). The value returned by reference via 'obj' should be passed back
4811 * to drm_property_change_valid_put() after the property is set (and the
4812 * object to which the property is attached has a chance to take it's own
4815 bool drm_property_change_valid_get(struct drm_property *property,
4816 uint64_t value, struct drm_mode_object **ref)
4820 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4825 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4826 if (value < property->values[0] || value > property->values[1])
4829 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4830 int64_t svalue = U642I64(value);
4832 if (svalue < U642I64(property->values[0]) ||
4833 svalue > U642I64(property->values[1]))
4836 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4837 uint64_t valid_mask = 0;
4839 for (i = 0; i < property->num_values; i++)
4840 valid_mask |= (1ULL << property->values[i]);
4841 return !(value & ~valid_mask);
4842 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4843 struct drm_property_blob *blob;
4848 blob = drm_property_lookup_blob(property->dev, value);
4855 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4856 /* a zero value for an object property translates to null: */
4860 /* handle refcnt'd objects specially: */
4861 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4862 struct drm_framebuffer *fb;
4863 fb = drm_framebuffer_lookup(property->dev, value);
4871 return _object_find(property->dev, value, property->values[0]) != NULL;
4875 for (i = 0; i < property->num_values; i++)
4876 if (property->values[i] == value)
4881 void drm_property_change_valid_put(struct drm_property *property,
4882 struct drm_mode_object *ref)
4887 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4888 if (property->values[0] == DRM_MODE_OBJECT_FB)
4889 drm_framebuffer_unreference(obj_to_fb(ref));
4890 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4891 drm_property_unreference_blob(obj_to_blob(ref));
4895 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4898 * @file_priv: DRM file info
4900 * This function sets the current value for a connectors's property. It also
4901 * calls into a driver's ->set_property callback to update the hardware state
4903 * Called by the user via ioctl.
4906 * Zero on success, negative errno on failure.
4908 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4909 void *data, struct drm_file *file_priv)
4911 struct drm_mode_connector_set_property *conn_set_prop = data;
4912 struct drm_mode_obj_set_property obj_set_prop = {
4913 .value = conn_set_prop->value,
4914 .prop_id = conn_set_prop->prop_id,
4915 .obj_id = conn_set_prop->connector_id,
4916 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4919 /* It does all the locking and checking we need */
4920 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4923 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4924 struct drm_property *property,
4928 struct drm_connector *connector = obj_to_connector(obj);
4930 /* Do DPMS ourselves */
4931 if (property == connector->dev->mode_config.dpms_property) {
4932 ret = (*connector->funcs->dpms)(connector, (int)value);
4933 } else if (connector->funcs->set_property)
4934 ret = connector->funcs->set_property(connector, property, value);
4936 /* store the property value if successful */
4938 drm_object_property_set_value(&connector->base, property, value);
4942 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4943 struct drm_property *property,
4947 struct drm_crtc *crtc = obj_to_crtc(obj);
4949 if (crtc->funcs->set_property)
4950 ret = crtc->funcs->set_property(crtc, property, value);
4952 drm_object_property_set_value(obj, property, value);
4958 * drm_mode_plane_set_obj_prop - set the value of a property
4959 * @plane: drm plane object to set property value for
4960 * @property: property to set
4961 * @value: value the property should be set to
4963 * This functions sets a given property on a given plane object. This function
4964 * calls the driver's ->set_property callback and changes the software state of
4965 * the property if the callback succeeds.
4968 * Zero on success, error code on failure.
4970 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4971 struct drm_property *property,
4975 struct drm_mode_object *obj = &plane->base;
4977 if (plane->funcs->set_property)
4978 ret = plane->funcs->set_property(plane, property, value);
4980 drm_object_property_set_value(obj, property, value);
4984 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4987 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4990 * @file_priv: DRM file info
4992 * This function retrieves the current value for an object's property. Compared
4993 * to the connector specific ioctl this one is extended to also work on crtc and
4996 * Called by the user via ioctl.
4999 * Zero on success, negative errno on failure.
5001 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
5002 struct drm_file *file_priv)
5004 struct drm_mode_obj_get_properties *arg = data;
5005 struct drm_mode_object *obj;
5008 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5011 drm_modeset_lock_all(dev);
5013 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5018 if (!obj->properties) {
5023 ret = get_properties(obj, file_priv->atomic,
5024 (uint32_t __user *)(unsigned long)(arg->props_ptr),
5025 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
5029 drm_modeset_unlock_all(dev);
5034 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
5037 * @file_priv: DRM file info
5039 * This function sets the current value for an object's property. It also calls
5040 * into a driver's ->set_property callback to update the hardware state.
5041 * Compared to the connector specific ioctl this one is extended to also work on
5042 * crtc and plane objects.
5044 * Called by the user via ioctl.
5047 * Zero on success, negative errno on failure.
5049 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
5050 struct drm_file *file_priv)
5052 struct drm_mode_obj_set_property *arg = data;
5053 struct drm_mode_object *arg_obj;
5054 struct drm_mode_object *prop_obj;
5055 struct drm_property *property;
5056 int i, ret = -EINVAL;
5057 struct drm_mode_object *ref;
5059 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5062 drm_modeset_lock_all(dev);
5064 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
5069 if (!arg_obj->properties)
5072 for (i = 0; i < arg_obj->properties->count; i++)
5073 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
5076 if (i == arg_obj->properties->count)
5079 prop_obj = drm_mode_object_find(dev, arg->prop_id,
5080 DRM_MODE_OBJECT_PROPERTY);
5085 property = obj_to_property(prop_obj);
5087 if (!drm_property_change_valid_get(property, arg->value, &ref))
5090 switch (arg_obj->type) {
5091 case DRM_MODE_OBJECT_CONNECTOR:
5092 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5095 case DRM_MODE_OBJECT_CRTC:
5096 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5098 case DRM_MODE_OBJECT_PLANE:
5099 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5100 property, arg->value);
5104 drm_property_change_valid_put(property, ref);
5107 drm_modeset_unlock_all(dev);
5112 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5113 * @connector: connector to attach
5114 * @encoder: encoder to attach @connector to
5116 * This function links up a connector to an encoder. Note that the routing
5117 * restrictions between encoders and crtcs are exposed to userspace through the
5118 * possible_clones and possible_crtcs bitmasks.
5121 * Zero on success, negative errno on failure.
5123 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5124 struct drm_encoder *encoder)
5129 * In the past, drivers have attempted to model the static association
5130 * of connector to encoder in simple connector/encoder devices using a
5131 * direct assignment of connector->encoder = encoder. This connection
5132 * is a logical one and the responsibility of the core, so drivers are
5133 * expected not to mess with this.
5135 * Note that the error return should've been enough here, but a large
5136 * majority of drivers ignores the return value, so add in a big WARN
5137 * to get people's attention.
5139 if (WARN_ON(connector->encoder))
5142 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5143 if (connector->encoder_ids[i] == 0) {
5144 connector->encoder_ids[i] = encoder->base.id;
5150 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5153 * drm_mode_crtc_set_gamma_size - set the gamma table size
5154 * @crtc: CRTC to set the gamma table size for
5155 * @gamma_size: size of the gamma table
5157 * Drivers which support gamma tables should set this to the supported gamma
5158 * table size when initializing the CRTC. Currently the drm core only supports a
5159 * fixed gamma table size.
5162 * Zero on success, negative errno on failure.
5164 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5167 crtc->gamma_size = gamma_size;
5169 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5171 if (!crtc->gamma_store) {
5172 crtc->gamma_size = 0;
5178 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5181 * drm_mode_gamma_set_ioctl - set the gamma table
5184 * @file_priv: DRM file info
5186 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5187 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5189 * Called by the user via ioctl.
5192 * Zero on success, negative errno on failure.
5194 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5195 void *data, struct drm_file *file_priv)
5197 struct drm_mode_crtc_lut *crtc_lut = data;
5198 struct drm_crtc *crtc;
5199 void *r_base, *g_base, *b_base;
5203 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5206 drm_modeset_lock_all(dev);
5207 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5213 if (crtc->funcs->gamma_set == NULL) {
5218 /* memcpy into gamma store */
5219 if (crtc_lut->gamma_size != crtc->gamma_size) {
5224 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5225 r_base = crtc->gamma_store;
5226 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5231 g_base = r_base + size;
5232 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5237 b_base = g_base + size;
5238 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5243 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5246 drm_modeset_unlock_all(dev);
5252 * drm_mode_gamma_get_ioctl - get the gamma table
5255 * @file_priv: DRM file info
5257 * Copy the current gamma table into the storage provided. This also provides
5258 * the gamma table size the driver expects, which can be used to size the
5259 * allocated storage.
5261 * Called by the user via ioctl.
5264 * Zero on success, negative errno on failure.
5266 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5267 void *data, struct drm_file *file_priv)
5269 struct drm_mode_crtc_lut *crtc_lut = data;
5270 struct drm_crtc *crtc;
5271 void *r_base, *g_base, *b_base;
5275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5278 drm_modeset_lock_all(dev);
5279 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5285 /* memcpy into gamma store */
5286 if (crtc_lut->gamma_size != crtc->gamma_size) {
5291 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5292 r_base = crtc->gamma_store;
5293 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5298 g_base = r_base + size;
5299 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5304 b_base = g_base + size;
5305 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5310 drm_modeset_unlock_all(dev);
5315 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5318 * @file_priv: DRM file info
5320 * This schedules an asynchronous update on a given CRTC, called page flip.
5321 * Optionally a drm event is generated to signal the completion of the event.
5322 * Generic drivers cannot assume that a pageflip with changed framebuffer
5323 * properties (including driver specific metadata like tiling layout) will work,
5324 * but some drivers support e.g. pixel format changes through the pageflip
5327 * Called by the user via ioctl.
5330 * Zero on success, negative errno on failure.
5332 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5333 void *data, struct drm_file *file_priv)
5335 struct drm_mode_crtc_page_flip *page_flip = data;
5336 struct drm_crtc *crtc;
5337 struct drm_framebuffer *fb = NULL;
5338 struct drm_pending_vblank_event *e = NULL;
5341 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5342 page_flip->reserved != 0)
5345 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5348 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5352 drm_modeset_lock_crtc(crtc, crtc->primary);
5353 if (crtc->primary->fb == NULL) {
5354 /* The framebuffer is currently unbound, presumably
5355 * due to a hotplug event, that userspace has not
5362 if (crtc->funcs->page_flip == NULL)
5365 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5372 const struct drm_plane_state *state = crtc->primary->state;
5374 ret = check_src_coords(state->src_x, state->src_y,
5375 state->src_w, state->src_h, fb);
5377 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5382 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5383 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5388 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5389 e = kzalloc(sizeof *e, GFP_KERNEL);
5394 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5395 e->event.base.length = sizeof(e->event);
5396 e->event.user_data = page_flip->user_data;
5397 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5404 crtc->primary->old_fb = crtc->primary->fb;
5405 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5407 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5408 drm_event_cancel_free(dev, &e->base);
5409 /* Keep the old fb, don't unref it. */
5410 crtc->primary->old_fb = NULL;
5412 crtc->primary->fb = fb;
5413 /* Unref only the old framebuffer. */
5419 drm_framebuffer_unreference(fb);
5420 if (crtc->primary->old_fb)
5421 drm_framebuffer_unreference(crtc->primary->old_fb);
5422 crtc->primary->old_fb = NULL;
5423 drm_modeset_unlock_crtc(crtc);
5429 * drm_mode_config_reset - call ->reset callbacks
5432 * This functions calls all the crtc's, encoder's and connector's ->reset
5433 * callback. Drivers can use this in e.g. their driver load or resume code to
5434 * reset hardware and software state.
5436 void drm_mode_config_reset(struct drm_device *dev)
5438 struct drm_crtc *crtc;
5439 struct drm_plane *plane;
5440 struct drm_encoder *encoder;
5441 struct drm_connector *connector;
5443 drm_for_each_plane(plane, dev)
5444 if (plane->funcs->reset)
5445 plane->funcs->reset(plane);
5447 drm_for_each_crtc(crtc, dev)
5448 if (crtc->funcs->reset)
5449 crtc->funcs->reset(crtc);
5451 drm_for_each_encoder(encoder, dev)
5452 if (encoder->funcs->reset)
5453 encoder->funcs->reset(encoder);
5455 mutex_lock(&dev->mode_config.mutex);
5456 drm_for_each_connector(connector, dev)
5457 if (connector->funcs->reset)
5458 connector->funcs->reset(connector);
5459 mutex_unlock(&dev->mode_config.mutex);
5461 EXPORT_SYMBOL(drm_mode_config_reset);
5464 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5467 * @file_priv: DRM file info
5469 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5470 * TTM or something else entirely) and returns the resulting buffer handle. This
5471 * handle can then be wrapped up into a framebuffer modeset object.
5473 * Note that userspace is not allowed to use such objects for render
5474 * acceleration - drivers must create their own private ioctls for such a use
5477 * Called by the user via ioctl.
5480 * Zero on success, negative errno on failure.
5482 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5483 void *data, struct drm_file *file_priv)
5485 struct drm_mode_create_dumb *args = data;
5486 u32 cpp, stride, size;
5488 if (!dev->driver->dumb_create)
5490 if (!args->width || !args->height || !args->bpp)
5493 /* overflow checks for 32bit size calculations */
5494 /* NOTE: DIV_ROUND_UP() can overflow */
5495 cpp = DIV_ROUND_UP(args->bpp, 8);
5496 if (!cpp || cpp > 0xffffffffU / args->width)
5498 stride = cpp * args->width;
5499 if (args->height > 0xffffffffU / stride)
5502 /* test for wrap-around */
5503 size = args->height * stride;
5504 if (PAGE_ALIGN(size) == 0)
5508 * handle, pitch and size are output parameters. Zero them out to
5509 * prevent drivers from accidentally using uninitialized data. Since
5510 * not all existing userspace is clearing these fields properly we
5511 * cannot reject IOCTL with garbage in them.
5517 return dev->driver->dumb_create(file_priv, dev, args);
5521 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5524 * @file_priv: DRM file info
5526 * Allocate an offset in the drm device node's address space to be able to
5527 * memory map a dumb buffer.
5529 * Called by the user via ioctl.
5532 * Zero on success, negative errno on failure.
5534 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5535 void *data, struct drm_file *file_priv)
5537 struct drm_mode_map_dumb *args = data;
5539 /* call driver ioctl to get mmap offset */
5540 if (!dev->driver->dumb_map_offset)
5543 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5547 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5550 * @file_priv: DRM file info
5552 * This destroys the userspace handle for the given dumb backing storage buffer.
5553 * Since buffer objects must be reference counted in the kernel a buffer object
5554 * won't be immediately freed if a framebuffer modeset object still uses it.
5556 * Called by the user via ioctl.
5559 * Zero on success, negative errno on failure.
5561 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5562 void *data, struct drm_file *file_priv)
5564 struct drm_mode_destroy_dumb *args = data;
5566 if (!dev->driver->dumb_destroy)
5569 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5573 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5574 * @format: pixel format (DRM_FORMAT_*)
5575 * @depth: storage for the depth value
5576 * @bpp: storage for the bpp value
5578 * This only supports RGB formats here for compat with code that doesn't use
5579 * pixel formats directly yet.
5581 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5586 case DRM_FORMAT_RGB332:
5587 case DRM_FORMAT_BGR233:
5591 case DRM_FORMAT_XRGB1555:
5592 case DRM_FORMAT_XBGR1555:
5593 case DRM_FORMAT_RGBX5551:
5594 case DRM_FORMAT_BGRX5551:
5595 case DRM_FORMAT_ARGB1555:
5596 case DRM_FORMAT_ABGR1555:
5597 case DRM_FORMAT_RGBA5551:
5598 case DRM_FORMAT_BGRA5551:
5602 case DRM_FORMAT_RGB565:
5603 case DRM_FORMAT_BGR565:
5607 case DRM_FORMAT_RGB888:
5608 case DRM_FORMAT_BGR888:
5612 case DRM_FORMAT_XRGB8888:
5613 case DRM_FORMAT_XBGR8888:
5614 case DRM_FORMAT_RGBX8888:
5615 case DRM_FORMAT_BGRX8888:
5619 case DRM_FORMAT_XRGB2101010:
5620 case DRM_FORMAT_XBGR2101010:
5621 case DRM_FORMAT_RGBX1010102:
5622 case DRM_FORMAT_BGRX1010102:
5623 case DRM_FORMAT_ARGB2101010:
5624 case DRM_FORMAT_ABGR2101010:
5625 case DRM_FORMAT_RGBA1010102:
5626 case DRM_FORMAT_BGRA1010102:
5630 case DRM_FORMAT_ARGB8888:
5631 case DRM_FORMAT_ABGR8888:
5632 case DRM_FORMAT_RGBA8888:
5633 case DRM_FORMAT_BGRA8888:
5638 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5639 drm_get_format_name(format));
5645 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5648 * drm_format_num_planes - get the number of planes for format
5649 * @format: pixel format (DRM_FORMAT_*)
5652 * The number of planes used by the specified pixel format.
5654 int drm_format_num_planes(uint32_t format)
5657 case DRM_FORMAT_YUV410:
5658 case DRM_FORMAT_YVU410:
5659 case DRM_FORMAT_YUV411:
5660 case DRM_FORMAT_YVU411:
5661 case DRM_FORMAT_YUV420:
5662 case DRM_FORMAT_YVU420:
5663 case DRM_FORMAT_YUV422:
5664 case DRM_FORMAT_YVU422:
5665 case DRM_FORMAT_YUV444:
5666 case DRM_FORMAT_YVU444:
5668 case DRM_FORMAT_NV12:
5669 case DRM_FORMAT_NV21:
5670 case DRM_FORMAT_NV16:
5671 case DRM_FORMAT_NV61:
5672 case DRM_FORMAT_NV24:
5673 case DRM_FORMAT_NV42:
5679 EXPORT_SYMBOL(drm_format_num_planes);
5682 * drm_format_plane_cpp - determine the bytes per pixel value
5683 * @format: pixel format (DRM_FORMAT_*)
5684 * @plane: plane index
5687 * The bytes per pixel value for the specified plane.
5689 int drm_format_plane_cpp(uint32_t format, int plane)
5694 if (plane >= drm_format_num_planes(format))
5698 case DRM_FORMAT_YUYV:
5699 case DRM_FORMAT_YVYU:
5700 case DRM_FORMAT_UYVY:
5701 case DRM_FORMAT_VYUY:
5703 case DRM_FORMAT_NV12:
5704 case DRM_FORMAT_NV21:
5705 case DRM_FORMAT_NV16:
5706 case DRM_FORMAT_NV61:
5707 case DRM_FORMAT_NV24:
5708 case DRM_FORMAT_NV42:
5709 return plane ? 2 : 1;
5710 case DRM_FORMAT_YUV410:
5711 case DRM_FORMAT_YVU410:
5712 case DRM_FORMAT_YUV411:
5713 case DRM_FORMAT_YVU411:
5714 case DRM_FORMAT_YUV420:
5715 case DRM_FORMAT_YVU420:
5716 case DRM_FORMAT_YUV422:
5717 case DRM_FORMAT_YVU422:
5718 case DRM_FORMAT_YUV444:
5719 case DRM_FORMAT_YVU444:
5722 drm_fb_get_bpp_depth(format, &depth, &bpp);
5726 EXPORT_SYMBOL(drm_format_plane_cpp);
5729 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5730 * @format: pixel format (DRM_FORMAT_*)
5733 * The horizontal chroma subsampling factor for the
5734 * specified pixel format.
5736 int drm_format_horz_chroma_subsampling(uint32_t format)
5739 case DRM_FORMAT_YUV411:
5740 case DRM_FORMAT_YVU411:
5741 case DRM_FORMAT_YUV410:
5742 case DRM_FORMAT_YVU410:
5744 case DRM_FORMAT_YUYV:
5745 case DRM_FORMAT_YVYU:
5746 case DRM_FORMAT_UYVY:
5747 case DRM_FORMAT_VYUY:
5748 case DRM_FORMAT_NV12:
5749 case DRM_FORMAT_NV21:
5750 case DRM_FORMAT_NV16:
5751 case DRM_FORMAT_NV61:
5752 case DRM_FORMAT_YUV422:
5753 case DRM_FORMAT_YVU422:
5754 case DRM_FORMAT_YUV420:
5755 case DRM_FORMAT_YVU420:
5761 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5764 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5765 * @format: pixel format (DRM_FORMAT_*)
5768 * The vertical chroma subsampling factor for the
5769 * specified pixel format.
5771 int drm_format_vert_chroma_subsampling(uint32_t format)
5774 case DRM_FORMAT_YUV410:
5775 case DRM_FORMAT_YVU410:
5777 case DRM_FORMAT_YUV420:
5778 case DRM_FORMAT_YVU420:
5779 case DRM_FORMAT_NV12:
5780 case DRM_FORMAT_NV21:
5786 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5789 * drm_format_plane_width - width of the plane given the first plane
5790 * @width: width of the first plane
5791 * @format: pixel format
5792 * @plane: plane index
5795 * The width of @plane, given that the width of the first plane is @width.
5797 int drm_format_plane_width(int width, uint32_t format, int plane)
5799 if (plane >= drm_format_num_planes(format))
5805 return width / drm_format_horz_chroma_subsampling(format);
5807 EXPORT_SYMBOL(drm_format_plane_width);
5810 * drm_format_plane_height - height of the plane given the first plane
5811 * @height: height of the first plane
5812 * @format: pixel format
5813 * @plane: plane index
5816 * The height of @plane, given that the height of the first plane is @height.
5818 int drm_format_plane_height(int height, uint32_t format, int plane)
5820 if (plane >= drm_format_num_planes(format))
5826 return height / drm_format_vert_chroma_subsampling(format);
5828 EXPORT_SYMBOL(drm_format_plane_height);
5831 * drm_rotation_simplify() - Try to simplify the rotation
5832 * @rotation: Rotation to be simplified
5833 * @supported_rotations: Supported rotations
5835 * Attempt to simplify the rotation to a form that is supported.
5836 * Eg. if the hardware supports everything except DRM_REFLECT_X
5837 * one could call this function like this:
5839 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5840 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5841 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5843 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5844 * transforms the hardware supports, this function may not
5845 * be able to produce a supported transform, so the caller should
5846 * check the result afterwards.
5848 unsigned int drm_rotation_simplify(unsigned int rotation,
5849 unsigned int supported_rotations)
5851 if (rotation & ~supported_rotations) {
5852 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5853 rotation = (rotation & DRM_REFLECT_MASK) |
5854 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5859 EXPORT_SYMBOL(drm_rotation_simplify);
5862 * drm_mode_config_init - initialize DRM mode_configuration structure
5865 * Initialize @dev's mode_config structure, used for tracking the graphics
5866 * configuration of @dev.
5868 * Since this initializes the modeset locks, no locking is possible. Which is no
5869 * problem, since this should happen single threaded at init time. It is the
5870 * driver's problem to ensure this guarantee.
5873 void drm_mode_config_init(struct drm_device *dev)
5875 mutex_init(&dev->mode_config.mutex);
5876 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5877 mutex_init(&dev->mode_config.idr_mutex);
5878 mutex_init(&dev->mode_config.fb_lock);
5879 mutex_init(&dev->mode_config.blob_lock);
5880 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5881 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5882 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5883 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5884 INIT_LIST_HEAD(&dev->mode_config.property_list);
5885 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5886 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5887 idr_init(&dev->mode_config.crtc_idr);
5888 idr_init(&dev->mode_config.tile_idr);
5889 ida_init(&dev->mode_config.connector_ida);
5891 drm_modeset_lock_all(dev);
5892 drm_mode_create_standard_properties(dev);
5893 drm_modeset_unlock_all(dev);
5895 /* Just to be sure */
5896 dev->mode_config.num_fb = 0;
5897 dev->mode_config.num_connector = 0;
5898 dev->mode_config.num_crtc = 0;
5899 dev->mode_config.num_encoder = 0;
5900 dev->mode_config.num_overlay_plane = 0;
5901 dev->mode_config.num_total_plane = 0;
5903 EXPORT_SYMBOL(drm_mode_config_init);
5906 * drm_mode_config_cleanup - free up DRM mode_config info
5909 * Free up all the connectors and CRTCs associated with this DRM device, then
5910 * free up the framebuffers and associated buffer objects.
5912 * Note that since this /should/ happen single-threaded at driver/device
5913 * teardown time, no locking is required. It's the driver's job to ensure that
5914 * this guarantee actually holds true.
5916 * FIXME: cleanup any dangling user buffer objects too
5918 void drm_mode_config_cleanup(struct drm_device *dev)
5920 struct drm_connector *connector, *ot;
5921 struct drm_crtc *crtc, *ct;
5922 struct drm_encoder *encoder, *enct;
5923 struct drm_framebuffer *fb, *fbt;
5924 struct drm_property *property, *pt;
5925 struct drm_property_blob *blob, *bt;
5926 struct drm_plane *plane, *plt;
5928 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5930 encoder->funcs->destroy(encoder);
5933 list_for_each_entry_safe(connector, ot,
5934 &dev->mode_config.connector_list, head) {
5935 connector->funcs->destroy(connector);
5938 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5940 drm_property_destroy(dev, property);
5943 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5945 plane->funcs->destroy(plane);
5948 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5949 crtc->funcs->destroy(crtc);
5952 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5954 drm_property_unreference_blob(blob);
5958 * Single-threaded teardown context, so it's not required to grab the
5959 * fb_lock to protect against concurrent fb_list access. Contrary, it
5960 * would actually deadlock with the drm_framebuffer_cleanup function.
5962 * Also, if there are any framebuffers left, that's a driver leak now,
5963 * so politely WARN about this.
5965 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5966 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5967 drm_framebuffer_free(&fb->base.refcount);
5970 ida_destroy(&dev->mode_config.connector_ida);
5971 idr_destroy(&dev->mode_config.tile_idr);
5972 idr_destroy(&dev->mode_config.crtc_idr);
5973 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5975 EXPORT_SYMBOL(drm_mode_config_cleanup);
5977 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5978 unsigned int supported_rotations)
5980 static const struct drm_prop_enum_list props[] = {
5981 { DRM_ROTATE_0, "rotate-0" },
5982 { DRM_ROTATE_90, "rotate-90" },
5983 { DRM_ROTATE_180, "rotate-180" },
5984 { DRM_ROTATE_270, "rotate-270" },
5985 { DRM_REFLECT_X, "reflect-x" },
5986 { DRM_REFLECT_Y, "reflect-y" },
5989 return drm_property_create_bitmask(dev, 0, "rotation",
5990 props, ARRAY_SIZE(props),
5991 supported_rotations);
5993 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5998 * Tile groups are used to represent tiled monitors with a unique
5999 * integer identifier. Tiled monitors using DisplayID v1.3 have
6000 * a unique 8-byte handle, we store this in a tile group, so we
6001 * have a common identifier for all tiles in a monitor group.
6003 static void drm_tile_group_free(struct kref *kref)
6005 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
6006 struct drm_device *dev = tg->dev;
6007 mutex_lock(&dev->mode_config.idr_mutex);
6008 idr_remove(&dev->mode_config.tile_idr, tg->id);
6009 mutex_unlock(&dev->mode_config.idr_mutex);
6014 * drm_mode_put_tile_group - drop a reference to a tile group.
6016 * @tg: tile group to drop reference to.
6018 * drop reference to tile group and free if 0.
6020 void drm_mode_put_tile_group(struct drm_device *dev,
6021 struct drm_tile_group *tg)
6023 kref_put(&tg->refcount, drm_tile_group_free);
6027 * drm_mode_get_tile_group - get a reference to an existing tile group
6029 * @topology: 8-bytes unique per monitor.
6031 * Use the unique bytes to get a reference to an existing tile group.
6034 * tile group or NULL if not found.
6036 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
6039 struct drm_tile_group *tg;
6041 mutex_lock(&dev->mode_config.idr_mutex);
6042 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
6043 if (!memcmp(tg->group_data, topology, 8)) {
6044 if (!kref_get_unless_zero(&tg->refcount))
6046 mutex_unlock(&dev->mode_config.idr_mutex);
6050 mutex_unlock(&dev->mode_config.idr_mutex);
6053 EXPORT_SYMBOL(drm_mode_get_tile_group);
6056 * drm_mode_create_tile_group - create a tile group from a displayid description
6058 * @topology: 8-bytes unique per monitor.
6060 * Create a tile group for the unique monitor, and get a unique
6061 * identifier for the tile group.
6064 * new tile group or error.
6066 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6069 struct drm_tile_group *tg;
6072 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6074 return ERR_PTR(-ENOMEM);
6076 kref_init(&tg->refcount);
6077 memcpy(tg->group_data, topology, 8);
6080 mutex_lock(&dev->mode_config.idr_mutex);
6081 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6089 mutex_unlock(&dev->mode_config.idr_mutex);
6092 EXPORT_SYMBOL(drm_mode_create_tile_group);