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)
364 if (obj && obj->free_cb) {
365 if (!kref_get_unless_zero(&obj->refcount))
368 mutex_unlock(&dev->mode_config.idr_mutex);
374 * drm_mode_object_find - look up a drm object with static lifetime
376 * @id: id of the mode object
377 * @type: type of the mode object
379 * This function is used to look up a modeset object. It will acquire a
380 * reference for reference counted objects. This reference must be dropped again
381 * by callind drm_mode_object_unreference().
383 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
384 uint32_t id, uint32_t type)
386 struct drm_mode_object *obj = NULL;
388 obj = _object_find(dev, id, type);
391 EXPORT_SYMBOL(drm_mode_object_find);
394 * drm_mode_object_unreference - decr the object refcnt
397 * This functions decrements the object's refcount if it is a refcounted modeset
398 * object. It is a no-op on any other object. This is used to drop references
399 * acquired with drm_mode_object_reference().
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 object refcnt
414 * This functions increments the object's refcount if it is a refcounted modeset
415 * object. It is a no-op on any other object. References should be dropped again
416 * by calling drm_mode_object_unreference().
418 void drm_mode_object_reference(struct drm_mode_object *obj)
421 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
422 kref_get(&obj->refcount);
425 EXPORT_SYMBOL(drm_mode_object_reference);
427 static void drm_framebuffer_free(struct kref *kref)
429 struct drm_framebuffer *fb =
430 container_of(kref, struct drm_framebuffer, base.refcount);
431 struct drm_device *dev = fb->dev;
434 * The lookup idr holds a weak reference, which has not necessarily been
435 * removed at this point. Check for that.
437 drm_mode_object_unregister(dev, &fb->base);
439 fb->funcs->destroy(fb);
443 * drm_framebuffer_init - initialize a framebuffer
445 * @fb: framebuffer to be initialized
446 * @funcs: ... with these functions
448 * Allocates an ID for the framebuffer's parent mode object, sets its mode
449 * functions & device file and adds it to the master fd list.
452 * This functions publishes the fb and makes it available for concurrent access
453 * by other users. Which means by this point the fb _must_ be fully set up -
454 * since all the fb attributes are invariant over its lifetime, no further
455 * locking but only correct reference counting is required.
458 * Zero on success, error code on failure.
460 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
461 const struct drm_framebuffer_funcs *funcs)
465 INIT_LIST_HEAD(&fb->filp_head);
469 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
470 false, drm_framebuffer_free);
474 mutex_lock(&dev->mode_config.fb_lock);
475 dev->mode_config.num_fb++;
476 list_add(&fb->head, &dev->mode_config.fb_list);
477 mutex_unlock(&dev->mode_config.fb_lock);
479 drm_mode_object_register(dev, &fb->base);
483 EXPORT_SYMBOL(drm_framebuffer_init);
486 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
488 * @id: id of the fb object
490 * If successful, this grabs an additional reference to the framebuffer -
491 * callers need to make sure to eventually unreference the returned framebuffer
492 * again, using @drm_framebuffer_unreference.
494 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
497 struct drm_mode_object *obj;
498 struct drm_framebuffer *fb = NULL;
500 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
505 EXPORT_SYMBOL(drm_framebuffer_lookup);
508 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
509 * @fb: fb to unregister
511 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
512 * those used for fbdev. Note that the caller must hold a reference of it's own,
513 * i.e. the object may not be destroyed through this call (since it'll lead to a
514 * locking inversion).
516 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
518 struct drm_device *dev;
525 /* Mark fb as reaped and drop idr ref. */
526 drm_mode_object_unregister(dev, &fb->base);
528 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
531 * drm_framebuffer_cleanup - remove a framebuffer object
532 * @fb: framebuffer to remove
534 * Cleanup framebuffer. This function is intended to be used from the drivers
535 * ->destroy callback. It can also be used to clean up driver private
536 * framebuffers embedded into a larger structure.
538 * Note that this function does not remove the fb from active usuage - if it is
539 * still used anywhere, hilarity can ensue since userspace could call getfb on
540 * the id and get back -EINVAL. Obviously no concern at driver unload time.
542 * Also, the framebuffer will not be removed from the lookup idr - for
543 * user-created framebuffers this will happen in in the rmfb ioctl. For
544 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
545 * drm_framebuffer_unregister_private.
547 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
549 struct drm_device *dev = fb->dev;
551 mutex_lock(&dev->mode_config.fb_lock);
553 dev->mode_config.num_fb--;
554 mutex_unlock(&dev->mode_config.fb_lock);
556 EXPORT_SYMBOL(drm_framebuffer_cleanup);
559 * drm_framebuffer_remove - remove and unreference a framebuffer object
560 * @fb: framebuffer to remove
562 * Scans all the CRTCs and planes in @dev's mode_config. If they're
563 * using @fb, removes it, setting it to NULL. Then drops the reference to the
564 * passed-in framebuffer. Might take the modeset locks.
566 * Note that this function optimizes the cleanup away if the caller holds the
567 * last reference to the framebuffer. It is also guaranteed to not take the
568 * modeset locks in this case.
570 void drm_framebuffer_remove(struct drm_framebuffer *fb)
572 struct drm_device *dev;
573 struct drm_crtc *crtc;
574 struct drm_plane *plane;
575 struct drm_mode_set set;
583 WARN_ON(!list_empty(&fb->filp_head));
586 * drm ABI mandates that we remove any deleted framebuffers from active
587 * useage. But since most sane clients only remove framebuffers they no
588 * longer need, try to optimize this away.
590 * Since we're holding a reference ourselves, observing a refcount of 1
591 * means that we're the last holder and can skip it. Also, the refcount
592 * can never increase from 1 again, so we don't need any barriers or
595 * Note that userspace could try to race with use and instate a new
596 * usage _after_ we've cleared all current ones. End result will be an
597 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
600 if (drm_framebuffer_read_refcount(fb) > 1) {
601 drm_modeset_lock_all(dev);
602 /* remove from any CRTC */
603 drm_for_each_crtc(crtc, dev) {
604 if (crtc->primary->fb == fb) {
605 /* should turn off the crtc */
606 memset(&set, 0, sizeof(struct drm_mode_set));
609 ret = drm_mode_set_config_internal(&set);
611 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
615 drm_for_each_plane(plane, dev) {
617 drm_plane_force_disable(plane);
619 drm_modeset_unlock_all(dev);
622 drm_framebuffer_unreference(fb);
624 EXPORT_SYMBOL(drm_framebuffer_remove);
626 DEFINE_WW_CLASS(crtc_ww_class);
628 static unsigned int drm_num_crtcs(struct drm_device *dev)
630 unsigned int num = 0;
631 struct drm_crtc *tmp;
633 drm_for_each_crtc(tmp, dev) {
641 * drm_crtc_init_with_planes - Initialise a new CRTC object with
642 * specified primary and cursor planes.
644 * @crtc: CRTC object to init
645 * @primary: Primary plane for CRTC
646 * @cursor: Cursor plane for CRTC
647 * @funcs: callbacks for the new CRTC
648 * @name: printf style format string for the CRTC name, or NULL for default name
650 * Inits a new object created as base part of a driver crtc object.
653 * Zero on success, error code on failure.
655 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
656 struct drm_plane *primary,
657 struct drm_plane *cursor,
658 const struct drm_crtc_funcs *funcs,
659 const char *name, ...)
661 struct drm_mode_config *config = &dev->mode_config;
664 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
665 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
670 drm_modeset_lock_init(&crtc->mutex);
671 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
679 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
682 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
686 drm_mode_object_unregister(dev, &crtc->base);
690 crtc->base.properties = &crtc->properties;
692 list_add_tail(&crtc->head, &config->crtc_list);
695 crtc->primary = primary;
696 crtc->cursor = cursor;
698 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
700 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
702 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
703 drm_object_attach_property(&crtc->base, config->prop_active, 0);
704 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
709 EXPORT_SYMBOL(drm_crtc_init_with_planes);
712 * drm_crtc_cleanup - Clean up the core crtc usage
713 * @crtc: CRTC to cleanup
715 * This function cleans up @crtc and removes it from the DRM mode setting
716 * core. Note that the function does *not* free the crtc structure itself,
717 * this is the responsibility of the caller.
719 void drm_crtc_cleanup(struct drm_crtc *crtc)
721 struct drm_device *dev = crtc->dev;
723 kfree(crtc->gamma_store);
724 crtc->gamma_store = NULL;
726 drm_modeset_lock_fini(&crtc->mutex);
728 drm_mode_object_unregister(dev, &crtc->base);
729 list_del(&crtc->head);
730 dev->mode_config.num_crtc--;
732 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
733 if (crtc->state && crtc->funcs->atomic_destroy_state)
734 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
738 memset(crtc, 0, sizeof(*crtc));
740 EXPORT_SYMBOL(drm_crtc_cleanup);
743 * drm_crtc_index - find the index of a registered CRTC
744 * @crtc: CRTC to find index for
746 * Given a registered CRTC, return the index of that CRTC within a DRM
747 * device's list of CRTCs.
749 unsigned int drm_crtc_index(struct drm_crtc *crtc)
751 unsigned int index = 0;
752 struct drm_crtc *tmp;
754 drm_for_each_crtc(tmp, crtc->dev) {
763 EXPORT_SYMBOL(drm_crtc_index);
766 * drm_mode_remove - remove and free a mode
767 * @connector: connector list to modify
768 * @mode: mode to remove
770 * Remove @mode from @connector's mode list, then free it.
772 static void drm_mode_remove(struct drm_connector *connector,
773 struct drm_display_mode *mode)
775 list_del(&mode->head);
776 drm_mode_destroy(connector->dev, mode);
780 * drm_display_info_set_bus_formats - set the supported bus formats
781 * @info: display info to store bus formats in
782 * @formats: array containing the supported bus formats
783 * @num_formats: the number of entries in the fmts array
785 * Store the supported bus formats in display info structure.
786 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
787 * a full list of available formats.
789 int drm_display_info_set_bus_formats(struct drm_display_info *info,
791 unsigned int num_formats)
795 if (!formats && num_formats)
798 if (formats && num_formats) {
799 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
805 kfree(info->bus_formats);
806 info->bus_formats = fmts;
807 info->num_bus_formats = num_formats;
811 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
814 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
815 * @connector: connector to quwery
817 * The kernel supports per-connector configration of its consoles through
818 * use of the video= parameter. This function parses that option and
819 * extracts the user's specified mode (or enable/disable status) for a
820 * particular connector. This is typically only used during the early fbdev
823 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
825 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
828 if (fb_get_options(connector->name, &option))
831 if (!drm_mode_parse_command_line_for_connector(option,
839 switch (mode->force) {
843 case DRM_FORCE_ON_DIGITAL:
852 DRM_INFO("forcing %s connector %s\n", connector->name, s);
853 connector->force = mode->force;
856 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
858 mode->xres, mode->yres,
859 mode->refresh_specified ? mode->refresh : 60,
860 mode->rb ? " reduced blanking" : "",
861 mode->margins ? " with margins" : "",
862 mode->interlace ? " interlaced" : "");
866 * drm_connector_init - Init a preallocated connector
868 * @connector: the connector to init
869 * @funcs: callbacks for this connector
870 * @connector_type: user visible type of the connector
872 * Initialises a preallocated connector. Connectors should be
873 * subclassed as part of driver connector objects.
876 * Zero on success, error code on failure.
878 int drm_connector_init(struct drm_device *dev,
879 struct drm_connector *connector,
880 const struct drm_connector_funcs *funcs,
883 struct drm_mode_config *config = &dev->mode_config;
885 struct ida *connector_ida =
886 &drm_connector_enum_list[connector_type].ida;
888 drm_modeset_lock_all(dev);
890 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false, NULL);
894 connector->base.properties = &connector->properties;
895 connector->dev = dev;
896 connector->funcs = funcs;
898 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
899 if (connector->connector_id < 0) {
900 ret = connector->connector_id;
904 connector->connector_type = connector_type;
905 connector->connector_type_id =
906 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
907 if (connector->connector_type_id < 0) {
908 ret = connector->connector_type_id;
912 kasprintf(GFP_KERNEL, "%s-%d",
913 drm_connector_enum_list[connector_type].name,
914 connector->connector_type_id);
915 if (!connector->name) {
917 goto out_put_type_id;
920 INIT_LIST_HEAD(&connector->probed_modes);
921 INIT_LIST_HEAD(&connector->modes);
922 connector->edid_blob_ptr = NULL;
923 connector->status = connector_status_unknown;
925 drm_connector_get_cmdline_mode(connector);
927 /* We should add connectors at the end to avoid upsetting the connector
929 list_add_tail(&connector->head, &config->connector_list);
930 config->num_connector++;
932 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
933 drm_object_attach_property(&connector->base,
934 config->edid_property,
937 drm_object_attach_property(&connector->base,
938 config->dpms_property, 0);
940 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
941 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
944 connector->debugfs_entry = NULL;
947 ida_remove(connector_ida, connector->connector_type_id);
950 ida_remove(&config->connector_ida, connector->connector_id);
953 drm_mode_object_unregister(dev, &connector->base);
956 drm_modeset_unlock_all(dev);
960 EXPORT_SYMBOL(drm_connector_init);
963 * drm_connector_cleanup - cleans up an initialised connector
964 * @connector: connector to cleanup
966 * Cleans up the connector but doesn't free the object.
968 void drm_connector_cleanup(struct drm_connector *connector)
970 struct drm_device *dev = connector->dev;
971 struct drm_display_mode *mode, *t;
973 if (connector->tile_group) {
974 drm_mode_put_tile_group(dev, connector->tile_group);
975 connector->tile_group = NULL;
978 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
979 drm_mode_remove(connector, mode);
981 list_for_each_entry_safe(mode, t, &connector->modes, head)
982 drm_mode_remove(connector, mode);
984 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
985 connector->connector_type_id);
987 ida_remove(&dev->mode_config.connector_ida,
988 connector->connector_id);
990 kfree(connector->display_info.bus_formats);
991 drm_mode_object_unregister(dev, &connector->base);
992 kfree(connector->name);
993 connector->name = NULL;
994 list_del(&connector->head);
995 dev->mode_config.num_connector--;
997 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
998 if (connector->state && connector->funcs->atomic_destroy_state)
999 connector->funcs->atomic_destroy_state(connector,
1002 memset(connector, 0, sizeof(*connector));
1004 EXPORT_SYMBOL(drm_connector_cleanup);
1007 * drm_connector_register - register a connector
1008 * @connector: the connector to register
1010 * Register userspace interfaces for a connector
1013 * Zero on success, error code on failure.
1015 int drm_connector_register(struct drm_connector *connector)
1019 drm_mode_object_register(connector->dev, &connector->base);
1021 ret = drm_sysfs_connector_add(connector);
1025 ret = drm_debugfs_connector_add(connector);
1027 drm_sysfs_connector_remove(connector);
1033 EXPORT_SYMBOL(drm_connector_register);
1036 * drm_connector_unregister - unregister a connector
1037 * @connector: the connector to unregister
1039 * Unregister userspace interfaces for a connector
1041 void drm_connector_unregister(struct drm_connector *connector)
1043 drm_sysfs_connector_remove(connector);
1044 drm_debugfs_connector_remove(connector);
1046 EXPORT_SYMBOL(drm_connector_unregister);
1049 * drm_connector_register_all - register all connectors
1052 * This function registers all connectors in sysfs and other places so that
1053 * userspace can start to access them. Drivers can call it after calling
1054 * drm_dev_register() to complete the device registration, if they don't call
1055 * drm_connector_register() on each connector individually.
1057 * When a device is unplugged and should be removed from userspace access,
1058 * call drm_connector_unregister_all(), which is the inverse of this
1062 * Zero on success, error code on failure.
1064 int drm_connector_register_all(struct drm_device *dev)
1066 struct drm_connector *connector;
1069 mutex_lock(&dev->mode_config.mutex);
1071 drm_for_each_connector(connector, dev) {
1072 ret = drm_connector_register(connector);
1077 mutex_unlock(&dev->mode_config.mutex);
1082 mutex_unlock(&dev->mode_config.mutex);
1083 drm_connector_unregister_all(dev);
1086 EXPORT_SYMBOL(drm_connector_register_all);
1089 * drm_connector_unregister_all - unregister connector userspace interfaces
1092 * This functions unregisters all connectors from sysfs and other places so
1093 * that userspace can no longer access them. Drivers should call this as the
1094 * first step tearing down the device instace, or when the underlying
1095 * physical device disappeared (e.g. USB unplug), right before calling
1096 * drm_dev_unregister().
1098 void drm_connector_unregister_all(struct drm_device *dev)
1100 struct drm_connector *connector;
1102 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1103 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1104 drm_connector_unregister(connector);
1106 EXPORT_SYMBOL(drm_connector_unregister_all);
1109 * drm_encoder_init - Init a preallocated encoder
1111 * @encoder: the encoder to init
1112 * @funcs: callbacks for this encoder
1113 * @encoder_type: user visible type of the encoder
1114 * @name: printf style format string for the encoder name, or NULL for default name
1116 * Initialises a preallocated encoder. Encoder should be
1117 * subclassed as part of driver encoder objects.
1120 * Zero on success, error code on failure.
1122 int drm_encoder_init(struct drm_device *dev,
1123 struct drm_encoder *encoder,
1124 const struct drm_encoder_funcs *funcs,
1125 int encoder_type, const char *name, ...)
1129 drm_modeset_lock_all(dev);
1131 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1136 encoder->encoder_type = encoder_type;
1137 encoder->funcs = funcs;
1142 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1145 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1146 drm_encoder_enum_list[encoder_type].name,
1149 if (!encoder->name) {
1154 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1155 dev->mode_config.num_encoder++;
1159 drm_mode_object_unregister(dev, &encoder->base);
1162 drm_modeset_unlock_all(dev);
1166 EXPORT_SYMBOL(drm_encoder_init);
1169 * drm_encoder_index - find the index of a registered encoder
1170 * @encoder: encoder to find index for
1172 * Given a registered encoder, return the index of that encoder within a DRM
1173 * device's list of encoders.
1175 unsigned int drm_encoder_index(struct drm_encoder *encoder)
1177 unsigned int index = 0;
1178 struct drm_encoder *tmp;
1180 drm_for_each_encoder(tmp, encoder->dev) {
1189 EXPORT_SYMBOL(drm_encoder_index);
1192 * drm_encoder_cleanup - cleans up an initialised encoder
1193 * @encoder: encoder to cleanup
1195 * Cleans up the encoder but doesn't free the object.
1197 void drm_encoder_cleanup(struct drm_encoder *encoder)
1199 struct drm_device *dev = encoder->dev;
1201 drm_modeset_lock_all(dev);
1202 drm_mode_object_unregister(dev, &encoder->base);
1203 kfree(encoder->name);
1204 list_del(&encoder->head);
1205 dev->mode_config.num_encoder--;
1206 drm_modeset_unlock_all(dev);
1208 memset(encoder, 0, sizeof(*encoder));
1210 EXPORT_SYMBOL(drm_encoder_cleanup);
1212 static unsigned int drm_num_planes(struct drm_device *dev)
1214 unsigned int num = 0;
1215 struct drm_plane *tmp;
1217 drm_for_each_plane(tmp, dev) {
1225 * drm_universal_plane_init - Initialize a new universal plane object
1227 * @plane: plane object to init
1228 * @possible_crtcs: bitmask of possible CRTCs
1229 * @funcs: callbacks for the new plane
1230 * @formats: array of supported formats (%DRM_FORMAT_*)
1231 * @format_count: number of elements in @formats
1232 * @type: type of plane (overlay, primary, cursor)
1233 * @name: printf style format string for the plane name, or NULL for default name
1235 * Initializes a plane object of type @type.
1238 * Zero on success, error code on failure.
1240 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1241 unsigned long possible_crtcs,
1242 const struct drm_plane_funcs *funcs,
1243 const uint32_t *formats, unsigned int format_count,
1244 enum drm_plane_type type,
1245 const char *name, ...)
1247 struct drm_mode_config *config = &dev->mode_config;
1250 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1254 drm_modeset_lock_init(&plane->mutex);
1256 plane->base.properties = &plane->properties;
1258 plane->funcs = funcs;
1259 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1261 if (!plane->format_types) {
1262 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1263 drm_mode_object_unregister(dev, &plane->base);
1271 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1274 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1275 drm_num_planes(dev));
1278 kfree(plane->format_types);
1279 drm_mode_object_unregister(dev, &plane->base);
1283 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1284 plane->format_count = format_count;
1285 plane->possible_crtcs = possible_crtcs;
1288 list_add_tail(&plane->head, &config->plane_list);
1289 config->num_total_plane++;
1290 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1291 config->num_overlay_plane++;
1293 drm_object_attach_property(&plane->base,
1294 config->plane_type_property,
1297 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1298 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1299 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1300 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1301 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1302 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1303 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1304 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1305 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1306 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1307 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1312 EXPORT_SYMBOL(drm_universal_plane_init);
1315 * drm_plane_init - Initialize a legacy plane
1317 * @plane: plane object to init
1318 * @possible_crtcs: bitmask of possible CRTCs
1319 * @funcs: callbacks for the new plane
1320 * @formats: array of supported formats (%DRM_FORMAT_*)
1321 * @format_count: number of elements in @formats
1322 * @is_primary: plane type (primary vs overlay)
1324 * Legacy API to initialize a DRM plane.
1326 * New drivers should call drm_universal_plane_init() instead.
1329 * Zero on success, error code on failure.
1331 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1332 unsigned long possible_crtcs,
1333 const struct drm_plane_funcs *funcs,
1334 const uint32_t *formats, unsigned int format_count,
1337 enum drm_plane_type type;
1339 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1340 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1341 formats, format_count, type, NULL);
1343 EXPORT_SYMBOL(drm_plane_init);
1346 * drm_plane_cleanup - Clean up the core plane usage
1347 * @plane: plane to cleanup
1349 * This function cleans up @plane and removes it from the DRM mode setting
1350 * core. Note that the function does *not* free the plane structure itself,
1351 * this is the responsibility of the caller.
1353 void drm_plane_cleanup(struct drm_plane *plane)
1355 struct drm_device *dev = plane->dev;
1357 drm_modeset_lock_all(dev);
1358 kfree(plane->format_types);
1359 drm_mode_object_unregister(dev, &plane->base);
1361 BUG_ON(list_empty(&plane->head));
1363 list_del(&plane->head);
1364 dev->mode_config.num_total_plane--;
1365 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1366 dev->mode_config.num_overlay_plane--;
1367 drm_modeset_unlock_all(dev);
1369 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1370 if (plane->state && plane->funcs->atomic_destroy_state)
1371 plane->funcs->atomic_destroy_state(plane, plane->state);
1375 memset(plane, 0, sizeof(*plane));
1377 EXPORT_SYMBOL(drm_plane_cleanup);
1380 * drm_plane_index - find the index of a registered plane
1381 * @plane: plane to find index for
1383 * Given a registered plane, return the index of that CRTC within a DRM
1384 * device's list of planes.
1386 unsigned int drm_plane_index(struct drm_plane *plane)
1388 unsigned int index = 0;
1389 struct drm_plane *tmp;
1391 drm_for_each_plane(tmp, plane->dev) {
1400 EXPORT_SYMBOL(drm_plane_index);
1403 * drm_plane_from_index - find the registered plane at an index
1405 * @idx: index of registered plane to find for
1407 * Given a plane index, return the registered plane from DRM device's
1408 * list of planes with matching index.
1411 drm_plane_from_index(struct drm_device *dev, int idx)
1413 struct drm_plane *plane;
1416 drm_for_each_plane(plane, dev) {
1423 EXPORT_SYMBOL(drm_plane_from_index);
1426 * drm_plane_force_disable - Forcibly disable a plane
1427 * @plane: plane to disable
1429 * Forces the plane to be disabled.
1431 * Used when the plane's current framebuffer is destroyed,
1432 * and when restoring fbdev mode.
1434 void drm_plane_force_disable(struct drm_plane *plane)
1441 plane->old_fb = plane->fb;
1442 ret = plane->funcs->disable_plane(plane);
1444 DRM_ERROR("failed to disable plane with busy fb\n");
1445 plane->old_fb = NULL;
1448 /* disconnect the plane from the fb and crtc: */
1449 drm_framebuffer_unreference(plane->old_fb);
1450 plane->old_fb = NULL;
1454 EXPORT_SYMBOL(drm_plane_force_disable);
1456 static int drm_mode_create_standard_properties(struct drm_device *dev)
1458 struct drm_property *prop;
1461 * Standard properties (apply to all connectors)
1463 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1464 DRM_MODE_PROP_IMMUTABLE,
1468 dev->mode_config.edid_property = prop;
1470 prop = drm_property_create_enum(dev, 0,
1471 "DPMS", drm_dpms_enum_list,
1472 ARRAY_SIZE(drm_dpms_enum_list));
1475 dev->mode_config.dpms_property = prop;
1477 prop = drm_property_create(dev,
1478 DRM_MODE_PROP_BLOB |
1479 DRM_MODE_PROP_IMMUTABLE,
1483 dev->mode_config.path_property = prop;
1485 prop = drm_property_create(dev,
1486 DRM_MODE_PROP_BLOB |
1487 DRM_MODE_PROP_IMMUTABLE,
1491 dev->mode_config.tile_property = prop;
1493 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1494 "type", drm_plane_type_enum_list,
1495 ARRAY_SIZE(drm_plane_type_enum_list));
1498 dev->mode_config.plane_type_property = prop;
1500 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1501 "SRC_X", 0, UINT_MAX);
1504 dev->mode_config.prop_src_x = prop;
1506 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1507 "SRC_Y", 0, UINT_MAX);
1510 dev->mode_config.prop_src_y = prop;
1512 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1513 "SRC_W", 0, UINT_MAX);
1516 dev->mode_config.prop_src_w = prop;
1518 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1519 "SRC_H", 0, UINT_MAX);
1522 dev->mode_config.prop_src_h = prop;
1524 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1525 "CRTC_X", INT_MIN, INT_MAX);
1528 dev->mode_config.prop_crtc_x = prop;
1530 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1531 "CRTC_Y", INT_MIN, INT_MAX);
1534 dev->mode_config.prop_crtc_y = prop;
1536 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1537 "CRTC_W", 0, INT_MAX);
1540 dev->mode_config.prop_crtc_w = prop;
1542 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1543 "CRTC_H", 0, INT_MAX);
1546 dev->mode_config.prop_crtc_h = prop;
1548 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1549 "FB_ID", DRM_MODE_OBJECT_FB);
1552 dev->mode_config.prop_fb_id = prop;
1554 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1555 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1558 dev->mode_config.prop_crtc_id = prop;
1560 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1564 dev->mode_config.prop_active = prop;
1566 prop = drm_property_create(dev,
1567 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1571 dev->mode_config.prop_mode_id = prop;
1573 prop = drm_property_create(dev,
1578 dev->mode_config.degamma_lut_property = prop;
1580 prop = drm_property_create_range(dev,
1581 DRM_MODE_PROP_IMMUTABLE,
1582 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1585 dev->mode_config.degamma_lut_size_property = prop;
1587 prop = drm_property_create(dev,
1592 dev->mode_config.ctm_property = prop;
1594 prop = drm_property_create(dev,
1599 dev->mode_config.gamma_lut_property = prop;
1601 prop = drm_property_create_range(dev,
1602 DRM_MODE_PROP_IMMUTABLE,
1603 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1606 dev->mode_config.gamma_lut_size_property = prop;
1612 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1615 * Called by a driver the first time a DVI-I connector is made.
1617 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1619 struct drm_property *dvi_i_selector;
1620 struct drm_property *dvi_i_subconnector;
1622 if (dev->mode_config.dvi_i_select_subconnector_property)
1626 drm_property_create_enum(dev, 0,
1627 "select subconnector",
1628 drm_dvi_i_select_enum_list,
1629 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1630 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1632 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1634 drm_dvi_i_subconnector_enum_list,
1635 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1636 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1640 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1643 * drm_create_tv_properties - create TV specific connector properties
1645 * @num_modes: number of different TV formats (modes) supported
1646 * @modes: array of pointers to strings containing name of each format
1648 * Called by a driver's TV initialization routine, this function creates
1649 * the TV specific connector properties for a given device. Caller is
1650 * responsible for allocating a list of format names and passing them to
1653 int drm_mode_create_tv_properties(struct drm_device *dev,
1654 unsigned int num_modes,
1655 const char * const modes[])
1657 struct drm_property *tv_selector;
1658 struct drm_property *tv_subconnector;
1661 if (dev->mode_config.tv_select_subconnector_property)
1665 * Basic connector properties
1667 tv_selector = drm_property_create_enum(dev, 0,
1668 "select subconnector",
1669 drm_tv_select_enum_list,
1670 ARRAY_SIZE(drm_tv_select_enum_list));
1674 dev->mode_config.tv_select_subconnector_property = tv_selector;
1677 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1679 drm_tv_subconnector_enum_list,
1680 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1681 if (!tv_subconnector)
1683 dev->mode_config.tv_subconnector_property = tv_subconnector;
1686 * Other, TV specific properties: margins & TV modes.
1688 dev->mode_config.tv_left_margin_property =
1689 drm_property_create_range(dev, 0, "left margin", 0, 100);
1690 if (!dev->mode_config.tv_left_margin_property)
1693 dev->mode_config.tv_right_margin_property =
1694 drm_property_create_range(dev, 0, "right margin", 0, 100);
1695 if (!dev->mode_config.tv_right_margin_property)
1698 dev->mode_config.tv_top_margin_property =
1699 drm_property_create_range(dev, 0, "top margin", 0, 100);
1700 if (!dev->mode_config.tv_top_margin_property)
1703 dev->mode_config.tv_bottom_margin_property =
1704 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1705 if (!dev->mode_config.tv_bottom_margin_property)
1708 dev->mode_config.tv_mode_property =
1709 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1711 if (!dev->mode_config.tv_mode_property)
1714 for (i = 0; i < num_modes; i++)
1715 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1718 dev->mode_config.tv_brightness_property =
1719 drm_property_create_range(dev, 0, "brightness", 0, 100);
1720 if (!dev->mode_config.tv_brightness_property)
1723 dev->mode_config.tv_contrast_property =
1724 drm_property_create_range(dev, 0, "contrast", 0, 100);
1725 if (!dev->mode_config.tv_contrast_property)
1728 dev->mode_config.tv_flicker_reduction_property =
1729 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1730 if (!dev->mode_config.tv_flicker_reduction_property)
1733 dev->mode_config.tv_overscan_property =
1734 drm_property_create_range(dev, 0, "overscan", 0, 100);
1735 if (!dev->mode_config.tv_overscan_property)
1738 dev->mode_config.tv_saturation_property =
1739 drm_property_create_range(dev, 0, "saturation", 0, 100);
1740 if (!dev->mode_config.tv_saturation_property)
1743 dev->mode_config.tv_hue_property =
1744 drm_property_create_range(dev, 0, "hue", 0, 100);
1745 if (!dev->mode_config.tv_hue_property)
1752 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1755 * drm_mode_create_scaling_mode_property - create scaling mode property
1758 * Called by a driver the first time it's needed, must be attached to desired
1761 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1763 struct drm_property *scaling_mode;
1765 if (dev->mode_config.scaling_mode_property)
1769 drm_property_create_enum(dev, 0, "scaling mode",
1770 drm_scaling_mode_enum_list,
1771 ARRAY_SIZE(drm_scaling_mode_enum_list));
1773 dev->mode_config.scaling_mode_property = scaling_mode;
1777 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1780 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1783 * Called by a driver the first time it's needed, must be attached to desired
1787 * Zero on success, negative errno on failure.
1789 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1791 if (dev->mode_config.aspect_ratio_property)
1794 dev->mode_config.aspect_ratio_property =
1795 drm_property_create_enum(dev, 0, "aspect ratio",
1796 drm_aspect_ratio_enum_list,
1797 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1799 if (dev->mode_config.aspect_ratio_property == NULL)
1804 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1807 * drm_mode_create_dirty_property - create dirty property
1810 * Called by a driver the first time it's needed, must be attached to desired
1813 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1815 struct drm_property *dirty_info;
1817 if (dev->mode_config.dirty_info_property)
1821 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1823 drm_dirty_info_enum_list,
1824 ARRAY_SIZE(drm_dirty_info_enum_list));
1825 dev->mode_config.dirty_info_property = dirty_info;
1829 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1832 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1835 * Create the the suggested x/y offset property for connectors.
1837 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1839 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1842 dev->mode_config.suggested_x_property =
1843 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1845 dev->mode_config.suggested_y_property =
1846 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1848 if (dev->mode_config.suggested_x_property == NULL ||
1849 dev->mode_config.suggested_y_property == NULL)
1853 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1856 * drm_mode_getresources - get graphics configuration
1857 * @dev: drm device for the ioctl
1858 * @data: data pointer for the ioctl
1859 * @file_priv: drm file for the ioctl call
1861 * Construct a set of configuration description structures and return
1862 * them to the user, including CRTC, connector and framebuffer configuration.
1864 * Called by the user via ioctl.
1867 * Zero on success, negative errno on failure.
1869 int drm_mode_getresources(struct drm_device *dev, void *data,
1870 struct drm_file *file_priv)
1872 struct drm_mode_card_res *card_res = data;
1873 struct list_head *lh;
1874 struct drm_framebuffer *fb;
1875 struct drm_connector *connector;
1876 struct drm_crtc *crtc;
1877 struct drm_encoder *encoder;
1879 int connector_count = 0;
1882 int encoder_count = 0;
1884 uint32_t __user *fb_id;
1885 uint32_t __user *crtc_id;
1886 uint32_t __user *connector_id;
1887 uint32_t __user *encoder_id;
1889 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1893 mutex_lock(&file_priv->fbs_lock);
1895 * For the non-control nodes we need to limit the list of resources
1896 * by IDs in the group list for this node
1898 list_for_each(lh, &file_priv->fbs)
1901 /* handle this in 4 parts */
1903 if (card_res->count_fbs >= fb_count) {
1905 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1906 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1907 if (put_user(fb->base.id, fb_id + copied)) {
1908 mutex_unlock(&file_priv->fbs_lock);
1914 card_res->count_fbs = fb_count;
1915 mutex_unlock(&file_priv->fbs_lock);
1917 /* mode_config.mutex protects the connector list against e.g. DP MST
1918 * connector hot-adding. CRTC/Plane lists are invariant. */
1919 mutex_lock(&dev->mode_config.mutex);
1920 drm_for_each_crtc(crtc, dev)
1923 drm_for_each_connector(connector, dev)
1926 drm_for_each_encoder(encoder, dev)
1929 card_res->max_height = dev->mode_config.max_height;
1930 card_res->min_height = dev->mode_config.min_height;
1931 card_res->max_width = dev->mode_config.max_width;
1932 card_res->min_width = dev->mode_config.min_width;
1935 if (card_res->count_crtcs >= crtc_count) {
1937 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1938 drm_for_each_crtc(crtc, dev) {
1939 if (put_user(crtc->base.id, crtc_id + copied)) {
1946 card_res->count_crtcs = crtc_count;
1949 if (card_res->count_encoders >= encoder_count) {
1951 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1952 drm_for_each_encoder(encoder, dev) {
1953 if (put_user(encoder->base.id, encoder_id +
1961 card_res->count_encoders = encoder_count;
1964 if (card_res->count_connectors >= connector_count) {
1966 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1967 drm_for_each_connector(connector, dev) {
1968 if (put_user(connector->base.id,
1969 connector_id + copied)) {
1976 card_res->count_connectors = connector_count;
1979 mutex_unlock(&dev->mode_config.mutex);
1984 * drm_mode_getcrtc - get CRTC configuration
1985 * @dev: drm device for the ioctl
1986 * @data: data pointer for the ioctl
1987 * @file_priv: drm file for the ioctl call
1989 * Construct a CRTC configuration structure to return to the user.
1991 * Called by the user via ioctl.
1994 * Zero on success, negative errno on failure.
1996 int drm_mode_getcrtc(struct drm_device *dev,
1997 void *data, struct drm_file *file_priv)
1999 struct drm_mode_crtc *crtc_resp = data;
2000 struct drm_crtc *crtc;
2002 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2005 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2009 drm_modeset_lock_crtc(crtc, crtc->primary);
2010 crtc_resp->gamma_size = crtc->gamma_size;
2011 if (crtc->primary->fb)
2012 crtc_resp->fb_id = crtc->primary->fb->base.id;
2014 crtc_resp->fb_id = 0;
2017 crtc_resp->x = crtc->primary->state->src_x >> 16;
2018 crtc_resp->y = crtc->primary->state->src_y >> 16;
2019 if (crtc->state->enable) {
2020 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2021 crtc_resp->mode_valid = 1;
2024 crtc_resp->mode_valid = 0;
2027 crtc_resp->x = crtc->x;
2028 crtc_resp->y = crtc->y;
2029 if (crtc->enabled) {
2030 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2031 crtc_resp->mode_valid = 1;
2034 crtc_resp->mode_valid = 0;
2037 drm_modeset_unlock_crtc(crtc);
2042 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2043 const struct drm_file *file_priv)
2046 * If user-space hasn't configured the driver to expose the stereo 3D
2047 * modes, don't expose them.
2049 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2055 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2057 /* For atomic drivers only state objects are synchronously updated and
2058 * protected by modeset locks, so check those first. */
2059 if (connector->state)
2060 return connector->state->best_encoder;
2061 return connector->encoder;
2064 /* helper for getconnector and getproperties ioctls */
2065 static int get_properties(struct drm_mode_object *obj, bool atomic,
2066 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2067 uint32_t *arg_count_props)
2072 props_count = obj->properties->count;
2074 props_count -= obj->properties->atomic_count;
2076 if ((*arg_count_props >= props_count) && props_count) {
2077 for (i = 0, copied = 0; copied < props_count; i++) {
2078 struct drm_property *prop = obj->properties->properties[i];
2081 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2084 ret = drm_object_property_get_value(obj, prop, &val);
2088 if (put_user(prop->base.id, prop_ptr + copied))
2091 if (put_user(val, prop_values + copied))
2097 *arg_count_props = props_count;
2103 * drm_mode_getconnector - get connector configuration
2104 * @dev: drm device for the ioctl
2105 * @data: data pointer for the ioctl
2106 * @file_priv: drm file for the ioctl call
2108 * Construct a connector configuration structure to return to the user.
2110 * Called by the user via ioctl.
2113 * Zero on success, negative errno on failure.
2115 int drm_mode_getconnector(struct drm_device *dev, void *data,
2116 struct drm_file *file_priv)
2118 struct drm_mode_get_connector *out_resp = data;
2119 struct drm_connector *connector;
2120 struct drm_encoder *encoder;
2121 struct drm_display_mode *mode;
2123 int encoders_count = 0;
2127 struct drm_mode_modeinfo u_mode;
2128 struct drm_mode_modeinfo __user *mode_ptr;
2129 uint32_t __user *encoder_ptr;
2131 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2134 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2136 mutex_lock(&dev->mode_config.mutex);
2138 connector = drm_connector_find(dev, out_resp->connector_id);
2144 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2145 if (connector->encoder_ids[i] != 0)
2148 if (out_resp->count_modes == 0) {
2149 connector->funcs->fill_modes(connector,
2150 dev->mode_config.max_width,
2151 dev->mode_config.max_height);
2154 /* delayed so we get modes regardless of pre-fill_modes state */
2155 list_for_each_entry(mode, &connector->modes, head)
2156 if (drm_mode_expose_to_userspace(mode, file_priv))
2159 out_resp->connector_id = connector->base.id;
2160 out_resp->connector_type = connector->connector_type;
2161 out_resp->connector_type_id = connector->connector_type_id;
2162 out_resp->mm_width = connector->display_info.width_mm;
2163 out_resp->mm_height = connector->display_info.height_mm;
2164 out_resp->subpixel = connector->display_info.subpixel_order;
2165 out_resp->connection = connector->status;
2167 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2168 encoder = drm_connector_get_encoder(connector);
2170 out_resp->encoder_id = encoder->base.id;
2172 out_resp->encoder_id = 0;
2175 * This ioctl is called twice, once to determine how much space is
2176 * needed, and the 2nd time to fill it.
2178 if ((out_resp->count_modes >= mode_count) && mode_count) {
2180 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2181 list_for_each_entry(mode, &connector->modes, head) {
2182 if (!drm_mode_expose_to_userspace(mode, file_priv))
2185 drm_mode_convert_to_umode(&u_mode, mode);
2186 if (copy_to_user(mode_ptr + copied,
2187 &u_mode, sizeof(u_mode))) {
2194 out_resp->count_modes = mode_count;
2196 ret = get_properties(&connector->base, file_priv->atomic,
2197 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2198 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2199 &out_resp->count_props);
2203 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2205 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2206 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2207 if (connector->encoder_ids[i] != 0) {
2208 if (put_user(connector->encoder_ids[i],
2209 encoder_ptr + copied)) {
2217 out_resp->count_encoders = encoders_count;
2220 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2223 mutex_unlock(&dev->mode_config.mutex);
2228 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2230 struct drm_connector *connector;
2231 struct drm_device *dev = encoder->dev;
2232 bool uses_atomic = false;
2234 /* For atomic drivers only state objects are synchronously updated and
2235 * protected by modeset locks, so check those first. */
2236 drm_for_each_connector(connector, dev) {
2237 if (!connector->state)
2242 if (connector->state->best_encoder != encoder)
2245 return connector->state->crtc;
2248 /* Don't return stale data (e.g. pending async disable). */
2252 return encoder->crtc;
2256 * drm_mode_getencoder - get encoder configuration
2257 * @dev: drm device for the ioctl
2258 * @data: data pointer for the ioctl
2259 * @file_priv: drm file for the ioctl call
2261 * Construct a encoder configuration structure to return to the user.
2263 * Called by the user via ioctl.
2266 * Zero on success, negative errno on failure.
2268 int drm_mode_getencoder(struct drm_device *dev, void *data,
2269 struct drm_file *file_priv)
2271 struct drm_mode_get_encoder *enc_resp = data;
2272 struct drm_encoder *encoder;
2273 struct drm_crtc *crtc;
2275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2278 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2282 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2283 crtc = drm_encoder_get_crtc(encoder);
2285 enc_resp->crtc_id = crtc->base.id;
2287 enc_resp->crtc_id = 0;
2288 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2290 enc_resp->encoder_type = encoder->encoder_type;
2291 enc_resp->encoder_id = encoder->base.id;
2292 enc_resp->possible_crtcs = encoder->possible_crtcs;
2293 enc_resp->possible_clones = encoder->possible_clones;
2299 * drm_mode_getplane_res - enumerate all plane resources
2302 * @file_priv: DRM file info
2304 * Construct a list of plane ids to return to the user.
2306 * Called by the user via ioctl.
2309 * Zero on success, negative errno on failure.
2311 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2312 struct drm_file *file_priv)
2314 struct drm_mode_get_plane_res *plane_resp = data;
2315 struct drm_mode_config *config;
2316 struct drm_plane *plane;
2317 uint32_t __user *plane_ptr;
2319 unsigned num_planes;
2321 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2324 config = &dev->mode_config;
2326 if (file_priv->universal_planes)
2327 num_planes = config->num_total_plane;
2329 num_planes = config->num_overlay_plane;
2332 * This ioctl is called twice, once to determine how much space is
2333 * needed, and the 2nd time to fill it.
2336 (plane_resp->count_planes >= num_planes)) {
2337 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2339 /* Plane lists are invariant, no locking needed. */
2340 drm_for_each_plane(plane, dev) {
2342 * Unless userspace set the 'universal planes'
2343 * capability bit, only advertise overlays.
2345 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2346 !file_priv->universal_planes)
2349 if (put_user(plane->base.id, plane_ptr + copied))
2354 plane_resp->count_planes = num_planes;
2360 * drm_mode_getplane - get plane configuration
2363 * @file_priv: DRM file info
2365 * Construct a plane configuration structure to return to the user.
2367 * Called by the user via ioctl.
2370 * Zero on success, negative errno on failure.
2372 int drm_mode_getplane(struct drm_device *dev, void *data,
2373 struct drm_file *file_priv)
2375 struct drm_mode_get_plane *plane_resp = data;
2376 struct drm_plane *plane;
2377 uint32_t __user *format_ptr;
2379 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2382 plane = drm_plane_find(dev, plane_resp->plane_id);
2386 drm_modeset_lock(&plane->mutex, NULL);
2388 plane_resp->crtc_id = plane->crtc->base.id;
2390 plane_resp->crtc_id = 0;
2393 plane_resp->fb_id = plane->fb->base.id;
2395 plane_resp->fb_id = 0;
2396 drm_modeset_unlock(&plane->mutex);
2398 plane_resp->plane_id = plane->base.id;
2399 plane_resp->possible_crtcs = plane->possible_crtcs;
2400 plane_resp->gamma_size = 0;
2403 * This ioctl is called twice, once to determine how much space is
2404 * needed, and the 2nd time to fill it.
2406 if (plane->format_count &&
2407 (plane_resp->count_format_types >= plane->format_count)) {
2408 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2409 if (copy_to_user(format_ptr,
2410 plane->format_types,
2411 sizeof(uint32_t) * plane->format_count)) {
2415 plane_resp->count_format_types = plane->format_count;
2421 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2422 * @plane: plane to check for format support
2423 * @format: the pixel format
2426 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2429 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2433 for (i = 0; i < plane->format_count; i++) {
2434 if (format == plane->format_types[i])
2441 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2442 uint32_t src_w, uint32_t src_h,
2443 const struct drm_framebuffer *fb)
2445 unsigned int fb_width, fb_height;
2447 fb_width = fb->width << 16;
2448 fb_height = fb->height << 16;
2450 /* Make sure source coordinates are inside the fb. */
2451 if (src_w > fb_width ||
2452 src_x > fb_width - src_w ||
2453 src_h > fb_height ||
2454 src_y > fb_height - src_h) {
2455 DRM_DEBUG_KMS("Invalid source coordinates "
2456 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2457 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2458 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2459 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2460 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2468 * setplane_internal - setplane handler for internal callers
2470 * Note that we assume an extra reference has already been taken on fb. If the
2471 * update fails, this reference will be dropped before return; if it succeeds,
2472 * the previous framebuffer (if any) will be unreferenced instead.
2474 * src_{x,y,w,h} are provided in 16.16 fixed point format
2476 static int __setplane_internal(struct drm_plane *plane,
2477 struct drm_crtc *crtc,
2478 struct drm_framebuffer *fb,
2479 int32_t crtc_x, int32_t crtc_y,
2480 uint32_t crtc_w, uint32_t crtc_h,
2481 /* src_{x,y,w,h} values are 16.16 fixed point */
2482 uint32_t src_x, uint32_t src_y,
2483 uint32_t src_w, uint32_t src_h)
2487 /* No fb means shut it down */
2489 plane->old_fb = plane->fb;
2490 ret = plane->funcs->disable_plane(plane);
2495 plane->old_fb = NULL;
2500 /* Check whether this plane is usable on this CRTC */
2501 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2502 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2507 /* Check whether this plane supports the fb pixel format. */
2508 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2510 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2511 drm_get_format_name(fb->pixel_format));
2515 /* Give drivers some help against integer overflows */
2516 if (crtc_w > INT_MAX ||
2517 crtc_x > INT_MAX - (int32_t) crtc_w ||
2519 crtc_y > INT_MAX - (int32_t) crtc_h) {
2520 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2521 crtc_w, crtc_h, crtc_x, crtc_y);
2526 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2530 plane->old_fb = plane->fb;
2531 ret = plane->funcs->update_plane(plane, crtc, fb,
2532 crtc_x, crtc_y, crtc_w, crtc_h,
2533 src_x, src_y, src_w, src_h);
2539 plane->old_fb = NULL;
2544 drm_framebuffer_unreference(fb);
2546 drm_framebuffer_unreference(plane->old_fb);
2547 plane->old_fb = NULL;
2552 static int setplane_internal(struct drm_plane *plane,
2553 struct drm_crtc *crtc,
2554 struct drm_framebuffer *fb,
2555 int32_t crtc_x, int32_t crtc_y,
2556 uint32_t crtc_w, uint32_t crtc_h,
2557 /* src_{x,y,w,h} values are 16.16 fixed point */
2558 uint32_t src_x, uint32_t src_y,
2559 uint32_t src_w, uint32_t src_h)
2563 drm_modeset_lock_all(plane->dev);
2564 ret = __setplane_internal(plane, crtc, fb,
2565 crtc_x, crtc_y, crtc_w, crtc_h,
2566 src_x, src_y, src_w, src_h);
2567 drm_modeset_unlock_all(plane->dev);
2573 * drm_mode_setplane - configure a plane's configuration
2575 * @data: ioctl data*
2576 * @file_priv: DRM file info
2578 * Set plane configuration, including placement, fb, scaling, and other factors.
2579 * Or pass a NULL fb to disable (planes may be disabled without providing a
2583 * Zero on success, negative errno on failure.
2585 int drm_mode_setplane(struct drm_device *dev, void *data,
2586 struct drm_file *file_priv)
2588 struct drm_mode_set_plane *plane_req = data;
2589 struct drm_plane *plane;
2590 struct drm_crtc *crtc = NULL;
2591 struct drm_framebuffer *fb = NULL;
2593 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2597 * First, find the plane, crtc, and fb objects. If not available,
2598 * we don't bother to call the driver.
2600 plane = drm_plane_find(dev, plane_req->plane_id);
2602 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2603 plane_req->plane_id);
2607 if (plane_req->fb_id) {
2608 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2610 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2615 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2617 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2618 plane_req->crtc_id);
2624 * setplane_internal will take care of deref'ing either the old or new
2625 * framebuffer depending on success.
2627 return setplane_internal(plane, crtc, fb,
2628 plane_req->crtc_x, plane_req->crtc_y,
2629 plane_req->crtc_w, plane_req->crtc_h,
2630 plane_req->src_x, plane_req->src_y,
2631 plane_req->src_w, plane_req->src_h);
2635 * drm_mode_set_config_internal - helper to call ->set_config
2636 * @set: modeset config to set
2638 * This is a little helper to wrap internal calls to the ->set_config driver
2639 * interface. The only thing it adds is correct refcounting dance.
2642 * Zero on success, negative errno on failure.
2644 int drm_mode_set_config_internal(struct drm_mode_set *set)
2646 struct drm_crtc *crtc = set->crtc;
2647 struct drm_framebuffer *fb;
2648 struct drm_crtc *tmp;
2652 * NOTE: ->set_config can also disable other crtcs (if we steal all
2653 * connectors from it), hence we need to refcount the fbs across all
2654 * crtcs. Atomic modeset will have saner semantics ...
2656 drm_for_each_crtc(tmp, crtc->dev)
2657 tmp->primary->old_fb = tmp->primary->fb;
2661 ret = crtc->funcs->set_config(set);
2663 crtc->primary->crtc = crtc;
2664 crtc->primary->fb = fb;
2667 drm_for_each_crtc(tmp, crtc->dev) {
2668 if (tmp->primary->fb)
2669 drm_framebuffer_reference(tmp->primary->fb);
2670 if (tmp->primary->old_fb)
2671 drm_framebuffer_unreference(tmp->primary->old_fb);
2672 tmp->primary->old_fb = NULL;
2677 EXPORT_SYMBOL(drm_mode_set_config_internal);
2680 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2681 * @mode: mode to query
2682 * @hdisplay: hdisplay value to fill in
2683 * @vdisplay: vdisplay value to fill in
2685 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2686 * the appropriate layout.
2688 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2689 int *hdisplay, int *vdisplay)
2691 struct drm_display_mode adjusted;
2693 drm_mode_copy(&adjusted, mode);
2694 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2695 *hdisplay = adjusted.crtc_hdisplay;
2696 *vdisplay = adjusted.crtc_vdisplay;
2698 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2701 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2703 * @crtc: CRTC that framebuffer will be displayed on
2706 * @mode: mode that framebuffer will be displayed under
2707 * @fb: framebuffer to check size of
2709 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2711 const struct drm_display_mode *mode,
2712 const struct drm_framebuffer *fb)
2715 int hdisplay, vdisplay;
2717 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2720 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2721 BIT(DRM_ROTATE_270)))
2722 swap(hdisplay, vdisplay);
2724 return check_src_coords(x << 16, y << 16,
2725 hdisplay << 16, vdisplay << 16, fb);
2727 EXPORT_SYMBOL(drm_crtc_check_viewport);
2730 * drm_mode_setcrtc - set CRTC configuration
2731 * @dev: drm device for the ioctl
2732 * @data: data pointer for the ioctl
2733 * @file_priv: drm file for the ioctl call
2735 * Build a new CRTC configuration based on user request.
2737 * Called by the user via ioctl.
2740 * Zero on success, negative errno on failure.
2742 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2743 struct drm_file *file_priv)
2745 struct drm_mode_config *config = &dev->mode_config;
2746 struct drm_mode_crtc *crtc_req = data;
2747 struct drm_crtc *crtc;
2748 struct drm_connector **connector_set = NULL, *connector;
2749 struct drm_framebuffer *fb = NULL;
2750 struct drm_display_mode *mode = NULL;
2751 struct drm_mode_set set;
2752 uint32_t __user *set_connectors_ptr;
2756 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2760 * Universal plane src offsets are only 16.16, prevent havoc for
2761 * drivers using universal plane code internally.
2763 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2766 drm_modeset_lock_all(dev);
2767 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2769 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2773 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2775 if (crtc_req->mode_valid) {
2776 /* If we have a mode we need a framebuffer. */
2777 /* If we pass -1, set the mode with the currently bound fb */
2778 if (crtc_req->fb_id == -1) {
2779 if (!crtc->primary->fb) {
2780 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2784 fb = crtc->primary->fb;
2785 /* Make refcounting symmetric with the lookup path. */
2786 drm_framebuffer_reference(fb);
2788 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2790 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2797 mode = drm_mode_create(dev);
2803 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2805 DRM_DEBUG_KMS("Invalid mode\n");
2809 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2812 * Check whether the primary plane supports the fb pixel format.
2813 * Drivers not implementing the universal planes API use a
2814 * default formats list provided by the DRM core which doesn't
2815 * match real hardware capabilities. Skip the check in that
2818 if (!crtc->primary->format_default) {
2819 ret = drm_plane_check_pixel_format(crtc->primary,
2822 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2823 drm_get_format_name(fb->pixel_format));
2828 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2835 if (crtc_req->count_connectors == 0 && mode) {
2836 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2841 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2842 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2843 crtc_req->count_connectors);
2848 if (crtc_req->count_connectors > 0) {
2851 /* Avoid unbounded kernel memory allocation */
2852 if (crtc_req->count_connectors > config->num_connector) {
2857 connector_set = kmalloc_array(crtc_req->count_connectors,
2858 sizeof(struct drm_connector *),
2860 if (!connector_set) {
2865 for (i = 0; i < crtc_req->count_connectors; i++) {
2866 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2867 if (get_user(out_id, &set_connectors_ptr[i])) {
2872 connector = drm_connector_find(dev, out_id);
2874 DRM_DEBUG_KMS("Connector id %d unknown\n",
2879 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2883 connector_set[i] = connector;
2888 set.x = crtc_req->x;
2889 set.y = crtc_req->y;
2891 set.connectors = connector_set;
2892 set.num_connectors = crtc_req->count_connectors;
2894 ret = drm_mode_set_config_internal(&set);
2898 drm_framebuffer_unreference(fb);
2900 kfree(connector_set);
2901 drm_mode_destroy(dev, mode);
2902 drm_modeset_unlock_all(dev);
2907 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2908 * universal plane handler call
2909 * @crtc: crtc to update cursor for
2910 * @req: data pointer for the ioctl
2911 * @file_priv: drm file for the ioctl call
2913 * Legacy cursor ioctl's work directly with driver buffer handles. To
2914 * translate legacy ioctl calls into universal plane handler calls, we need to
2915 * wrap the native buffer handle in a drm_framebuffer.
2917 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2918 * buffer with a pitch of 4*width; the universal plane interface should be used
2919 * directly in cases where the hardware can support other buffer settings and
2920 * userspace wants to make use of these capabilities.
2923 * Zero on success, negative errno on failure.
2925 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2926 struct drm_mode_cursor2 *req,
2927 struct drm_file *file_priv)
2929 struct drm_device *dev = crtc->dev;
2930 struct drm_framebuffer *fb = NULL;
2931 struct drm_mode_fb_cmd2 fbreq = {
2932 .width = req->width,
2933 .height = req->height,
2934 .pixel_format = DRM_FORMAT_ARGB8888,
2935 .pitches = { req->width * 4 },
2936 .handles = { req->handle },
2938 int32_t crtc_x, crtc_y;
2939 uint32_t crtc_w = 0, crtc_h = 0;
2940 uint32_t src_w = 0, src_h = 0;
2943 BUG_ON(!crtc->cursor);
2944 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2947 * Obtain fb we'll be using (either new or existing) and take an extra
2948 * reference to it if fb != null. setplane will take care of dropping
2949 * the reference if the plane update fails.
2951 if (req->flags & DRM_MODE_CURSOR_BO) {
2953 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2955 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2962 fb = crtc->cursor->fb;
2964 drm_framebuffer_reference(fb);
2967 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2971 crtc_x = crtc->cursor_x;
2972 crtc_y = crtc->cursor_y;
2977 crtc_h = fb->height;
2978 src_w = fb->width << 16;
2979 src_h = fb->height << 16;
2983 * setplane_internal will take care of deref'ing either the old or new
2984 * framebuffer depending on success.
2986 ret = __setplane_internal(crtc->cursor, crtc, fb,
2987 crtc_x, crtc_y, crtc_w, crtc_h,
2988 0, 0, src_w, src_h);
2990 /* Update successful; save new cursor position, if necessary */
2991 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2992 crtc->cursor_x = req->x;
2993 crtc->cursor_y = req->y;
2999 static int drm_mode_cursor_common(struct drm_device *dev,
3000 struct drm_mode_cursor2 *req,
3001 struct drm_file *file_priv)
3003 struct drm_crtc *crtc;
3006 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3009 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3012 crtc = drm_crtc_find(dev, req->crtc_id);
3014 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3019 * If this crtc has a universal cursor plane, call that plane's update
3020 * handler rather than using legacy cursor handlers.
3022 drm_modeset_lock_crtc(crtc, crtc->cursor);
3024 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3028 if (req->flags & DRM_MODE_CURSOR_BO) {
3029 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3033 /* Turns off the cursor if handle is 0 */
3034 if (crtc->funcs->cursor_set2)
3035 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3036 req->width, req->height, req->hot_x, req->hot_y);
3038 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3039 req->width, req->height);
3042 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3043 if (crtc->funcs->cursor_move) {
3044 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3051 drm_modeset_unlock_crtc(crtc);
3059 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3060 * @dev: drm device for the ioctl
3061 * @data: data pointer for the ioctl
3062 * @file_priv: drm file for the ioctl call
3064 * Set the cursor configuration based on user request.
3066 * Called by the user via ioctl.
3069 * Zero on success, negative errno on failure.
3071 int drm_mode_cursor_ioctl(struct drm_device *dev,
3072 void *data, struct drm_file *file_priv)
3074 struct drm_mode_cursor *req = data;
3075 struct drm_mode_cursor2 new_req;
3077 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3078 new_req.hot_x = new_req.hot_y = 0;
3080 return drm_mode_cursor_common(dev, &new_req, file_priv);
3084 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3085 * @dev: drm device for the ioctl
3086 * @data: data pointer for the ioctl
3087 * @file_priv: drm file for the ioctl call
3089 * Set the cursor configuration based on user request. This implements the 2nd
3090 * version of the cursor ioctl, which allows userspace to additionally specify
3091 * the hotspot of the pointer.
3093 * Called by the user via ioctl.
3096 * Zero on success, negative errno on failure.
3098 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3099 void *data, struct drm_file *file_priv)
3101 struct drm_mode_cursor2 *req = data;
3103 return drm_mode_cursor_common(dev, req, file_priv);
3107 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3108 * @bpp: bits per pixels
3109 * @depth: bit depth per pixel
3111 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3112 * Useful in fbdev emulation code, since that deals in those values.
3114 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3120 fmt = DRM_FORMAT_C8;
3124 fmt = DRM_FORMAT_XRGB1555;
3126 fmt = DRM_FORMAT_RGB565;
3129 fmt = DRM_FORMAT_RGB888;
3133 fmt = DRM_FORMAT_XRGB8888;
3134 else if (depth == 30)
3135 fmt = DRM_FORMAT_XRGB2101010;
3137 fmt = DRM_FORMAT_ARGB8888;
3140 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3141 fmt = DRM_FORMAT_XRGB8888;
3147 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3150 * drm_mode_addfb - add an FB to the graphics configuration
3151 * @dev: drm device for the ioctl
3152 * @data: data pointer for the ioctl
3153 * @file_priv: drm file for the ioctl call
3155 * Add a new FB to the specified CRTC, given a user request. This is the
3156 * original addfb ioctl which only supported RGB formats.
3158 * Called by the user via ioctl.
3161 * Zero on success, negative errno on failure.
3163 int drm_mode_addfb(struct drm_device *dev,
3164 void *data, struct drm_file *file_priv)
3166 struct drm_mode_fb_cmd *or = data;
3167 struct drm_mode_fb_cmd2 r = {};
3170 /* convert to new format and call new ioctl */
3171 r.fb_id = or->fb_id;
3172 r.width = or->width;
3173 r.height = or->height;
3174 r.pitches[0] = or->pitch;
3175 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3176 r.handles[0] = or->handle;
3178 ret = drm_mode_addfb2(dev, &r, file_priv);
3182 or->fb_id = r.fb_id;
3187 static int format_check(const struct drm_mode_fb_cmd2 *r)
3189 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3193 case DRM_FORMAT_RGB332:
3194 case DRM_FORMAT_BGR233:
3195 case DRM_FORMAT_XRGB4444:
3196 case DRM_FORMAT_XBGR4444:
3197 case DRM_FORMAT_RGBX4444:
3198 case DRM_FORMAT_BGRX4444:
3199 case DRM_FORMAT_ARGB4444:
3200 case DRM_FORMAT_ABGR4444:
3201 case DRM_FORMAT_RGBA4444:
3202 case DRM_FORMAT_BGRA4444:
3203 case DRM_FORMAT_XRGB1555:
3204 case DRM_FORMAT_XBGR1555:
3205 case DRM_FORMAT_RGBX5551:
3206 case DRM_FORMAT_BGRX5551:
3207 case DRM_FORMAT_ARGB1555:
3208 case DRM_FORMAT_ABGR1555:
3209 case DRM_FORMAT_RGBA5551:
3210 case DRM_FORMAT_BGRA5551:
3211 case DRM_FORMAT_RGB565:
3212 case DRM_FORMAT_BGR565:
3213 case DRM_FORMAT_RGB888:
3214 case DRM_FORMAT_BGR888:
3215 case DRM_FORMAT_XRGB8888:
3216 case DRM_FORMAT_XBGR8888:
3217 case DRM_FORMAT_RGBX8888:
3218 case DRM_FORMAT_BGRX8888:
3219 case DRM_FORMAT_ARGB8888:
3220 case DRM_FORMAT_ABGR8888:
3221 case DRM_FORMAT_RGBA8888:
3222 case DRM_FORMAT_BGRA8888:
3223 case DRM_FORMAT_XRGB2101010:
3224 case DRM_FORMAT_XBGR2101010:
3225 case DRM_FORMAT_RGBX1010102:
3226 case DRM_FORMAT_BGRX1010102:
3227 case DRM_FORMAT_ARGB2101010:
3228 case DRM_FORMAT_ABGR2101010:
3229 case DRM_FORMAT_RGBA1010102:
3230 case DRM_FORMAT_BGRA1010102:
3231 case DRM_FORMAT_YUYV:
3232 case DRM_FORMAT_YVYU:
3233 case DRM_FORMAT_UYVY:
3234 case DRM_FORMAT_VYUY:
3235 case DRM_FORMAT_AYUV:
3236 case DRM_FORMAT_NV12:
3237 case DRM_FORMAT_NV21:
3238 case DRM_FORMAT_NV16:
3239 case DRM_FORMAT_NV61:
3240 case DRM_FORMAT_NV24:
3241 case DRM_FORMAT_NV42:
3242 case DRM_FORMAT_YUV410:
3243 case DRM_FORMAT_YVU410:
3244 case DRM_FORMAT_YUV411:
3245 case DRM_FORMAT_YVU411:
3246 case DRM_FORMAT_YUV420:
3247 case DRM_FORMAT_YVU420:
3248 case DRM_FORMAT_YUV422:
3249 case DRM_FORMAT_YVU422:
3250 case DRM_FORMAT_YUV444:
3251 case DRM_FORMAT_YVU444:
3254 DRM_DEBUG_KMS("invalid pixel format %s\n",
3255 drm_get_format_name(r->pixel_format));
3260 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3262 int ret, hsub, vsub, num_planes, i;
3264 ret = format_check(r);
3266 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3267 drm_get_format_name(r->pixel_format));
3271 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3272 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3273 num_planes = drm_format_num_planes(r->pixel_format);
3275 if (r->width == 0 || r->width % hsub) {
3276 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3280 if (r->height == 0 || r->height % vsub) {
3281 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3285 for (i = 0; i < num_planes; i++) {
3286 unsigned int width = r->width / (i != 0 ? hsub : 1);
3287 unsigned int height = r->height / (i != 0 ? vsub : 1);
3288 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3290 if (!r->handles[i]) {
3291 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3295 if ((uint64_t) width * cpp > UINT_MAX)
3298 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3301 if (r->pitches[i] < width * cpp) {
3302 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3306 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3307 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3312 /* modifier specific checks: */
3313 switch (r->modifier[i]) {
3314 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3315 /* NOTE: the pitch restriction may be lifted later if it turns
3316 * out that no hw has this restriction:
3318 if (r->pixel_format != DRM_FORMAT_NV12 ||
3319 width % 128 || height % 32 ||
3320 r->pitches[i] % 128) {
3321 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3331 for (i = num_planes; i < 4; i++) {
3332 if (r->modifier[i]) {
3333 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3337 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3338 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3341 if (r->handles[i]) {
3342 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3346 if (r->pitches[i]) {
3347 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3351 if (r->offsets[i]) {
3352 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3360 static struct drm_framebuffer *
3361 internal_framebuffer_create(struct drm_device *dev,
3362 const struct drm_mode_fb_cmd2 *r,
3363 struct drm_file *file_priv)
3365 struct drm_mode_config *config = &dev->mode_config;
3366 struct drm_framebuffer *fb;
3369 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3370 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3371 return ERR_PTR(-EINVAL);
3374 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3375 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3376 r->width, config->min_width, config->max_width);
3377 return ERR_PTR(-EINVAL);
3379 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3380 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3381 r->height, config->min_height, config->max_height);
3382 return ERR_PTR(-EINVAL);
3385 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3386 !dev->mode_config.allow_fb_modifiers) {
3387 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3388 return ERR_PTR(-EINVAL);
3391 ret = framebuffer_check(r);
3393 return ERR_PTR(ret);
3395 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3397 DRM_DEBUG_KMS("could not create framebuffer\n");
3405 * drm_mode_addfb2 - add an FB to the graphics configuration
3406 * @dev: drm device for the ioctl
3407 * @data: data pointer for the ioctl
3408 * @file_priv: drm file for the ioctl call
3410 * Add a new FB to the specified CRTC, given a user request with format. This is
3411 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3412 * and uses fourcc codes as pixel format specifiers.
3414 * Called by the user via ioctl.
3417 * Zero on success, negative errno on failure.
3419 int drm_mode_addfb2(struct drm_device *dev,
3420 void *data, struct drm_file *file_priv)
3422 struct drm_mode_fb_cmd2 *r = data;
3423 struct drm_framebuffer *fb;
3425 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3428 fb = internal_framebuffer_create(dev, r, file_priv);
3432 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3433 r->fb_id = fb->base.id;
3435 /* Transfer ownership to the filp for reaping on close */
3436 mutex_lock(&file_priv->fbs_lock);
3437 list_add(&fb->filp_head, &file_priv->fbs);
3438 mutex_unlock(&file_priv->fbs_lock);
3444 * drm_mode_rmfb - remove an FB from the configuration
3445 * @dev: drm device for the ioctl
3446 * @data: data pointer for the ioctl
3447 * @file_priv: drm file for the ioctl call
3449 * Remove the FB specified by the user.
3451 * Called by the user via ioctl.
3454 * Zero on success, negative errno on failure.
3456 int drm_mode_rmfb(struct drm_device *dev,
3457 void *data, struct drm_file *file_priv)
3459 struct drm_framebuffer *fb = NULL;
3460 struct drm_framebuffer *fbl = NULL;
3461 uint32_t *id = data;
3464 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3467 fb = drm_framebuffer_lookup(dev, *id);
3471 mutex_lock(&file_priv->fbs_lock);
3472 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3476 mutex_unlock(&file_priv->fbs_lock);
3480 list_del_init(&fb->filp_head);
3481 mutex_unlock(&file_priv->fbs_lock);
3483 /* we now own the reference that was stored in the fbs list */
3484 drm_framebuffer_unreference(fb);
3486 /* drop the reference we picked up in framebuffer lookup */
3487 drm_framebuffer_unreference(fb);
3492 drm_framebuffer_unreference(fb);
3497 * drm_mode_getfb - get FB info
3498 * @dev: drm device for the ioctl
3499 * @data: data pointer for the ioctl
3500 * @file_priv: drm file for the ioctl call
3502 * Lookup the FB given its ID and return info about it.
3504 * Called by the user via ioctl.
3507 * Zero on success, negative errno on failure.
3509 int drm_mode_getfb(struct drm_device *dev,
3510 void *data, struct drm_file *file_priv)
3512 struct drm_mode_fb_cmd *r = data;
3513 struct drm_framebuffer *fb;
3516 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3519 fb = drm_framebuffer_lookup(dev, r->fb_id);
3523 r->height = fb->height;
3524 r->width = fb->width;
3525 r->depth = fb->depth;
3526 r->bpp = fb->bits_per_pixel;
3527 r->pitch = fb->pitches[0];
3528 if (fb->funcs->create_handle) {
3529 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3530 drm_is_control_client(file_priv)) {
3531 ret = fb->funcs->create_handle(fb, file_priv,
3534 /* GET_FB() is an unprivileged ioctl so we must not
3535 * return a buffer-handle to non-master processes! For
3536 * backwards-compatibility reasons, we cannot make
3537 * GET_FB() privileged, so just return an invalid handle
3538 * for non-masters. */
3546 drm_framebuffer_unreference(fb);
3552 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3553 * @dev: drm device for the ioctl
3554 * @data: data pointer for the ioctl
3555 * @file_priv: drm file for the ioctl call
3557 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3558 * rectangle list. Generic userspace which does frontbuffer rendering must call
3559 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3560 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3562 * Modesetting drivers which always update the frontbuffer do not need to
3563 * implement the corresponding ->dirty framebuffer callback.
3565 * Called by the user via ioctl.
3568 * Zero on success, negative errno on failure.
3570 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3571 void *data, struct drm_file *file_priv)
3573 struct drm_clip_rect __user *clips_ptr;
3574 struct drm_clip_rect *clips = NULL;
3575 struct drm_mode_fb_dirty_cmd *r = data;
3576 struct drm_framebuffer *fb;
3581 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3584 fb = drm_framebuffer_lookup(dev, r->fb_id);
3588 num_clips = r->num_clips;
3589 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3591 if (!num_clips != !clips_ptr) {
3596 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3598 /* If userspace annotates copy, clips must come in pairs */
3599 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3604 if (num_clips && clips_ptr) {
3605 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3609 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3615 ret = copy_from_user(clips, clips_ptr,
3616 num_clips * sizeof(*clips));
3623 if (fb->funcs->dirty) {
3624 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3633 drm_framebuffer_unreference(fb);
3640 * drm_fb_release - remove and free the FBs on this file
3641 * @priv: drm file for the ioctl
3643 * Destroy all the FBs associated with @filp.
3645 * Called by the user via ioctl.
3648 * Zero on success, negative errno on failure.
3650 void drm_fb_release(struct drm_file *priv)
3652 struct drm_framebuffer *fb, *tfb;
3655 * When the file gets released that means no one else can access the fb
3656 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3657 * avoid upsetting lockdep since the universal cursor code adds a
3658 * framebuffer while holding mutex locks.
3660 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3661 * locks is impossible here since no one else but this function can get
3664 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3665 list_del_init(&fb->filp_head);
3667 /* This drops the fpriv->fbs reference. */
3668 drm_framebuffer_unreference(fb);
3673 * drm_property_create - create a new property type
3675 * @flags: flags specifying the property type
3676 * @name: name of the property
3677 * @num_values: number of pre-defined values
3679 * This creates a new generic drm property which can then be attached to a drm
3680 * object with drm_object_attach_property. The returned property object must be
3681 * freed with drm_property_destroy.
3683 * Note that the DRM core keeps a per-device list of properties and that, if
3684 * drm_mode_config_cleanup() is called, it will destroy all properties created
3688 * A pointer to the newly created property on success, NULL on failure.
3690 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3691 const char *name, int num_values)
3693 struct drm_property *property = NULL;
3696 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3700 property->dev = dev;
3703 property->values = kcalloc(num_values, sizeof(uint64_t),
3705 if (!property->values)
3709 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3713 property->flags = flags;
3714 property->num_values = num_values;
3715 INIT_LIST_HEAD(&property->enum_list);
3718 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3719 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3722 list_add_tail(&property->head, &dev->mode_config.property_list);
3724 WARN_ON(!drm_property_type_valid(property));
3728 kfree(property->values);
3732 EXPORT_SYMBOL(drm_property_create);
3735 * drm_property_create_enum - create a new enumeration property type
3737 * @flags: flags specifying the property type
3738 * @name: name of the property
3739 * @props: enumeration lists with property values
3740 * @num_values: number of pre-defined values
3742 * This creates a new generic drm property which can then be attached to a drm
3743 * object with drm_object_attach_property. The returned property object must be
3744 * freed with drm_property_destroy.
3746 * Userspace is only allowed to set one of the predefined values for enumeration
3750 * A pointer to the newly created property on success, NULL on failure.
3752 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3754 const struct drm_prop_enum_list *props,
3757 struct drm_property *property;
3760 flags |= DRM_MODE_PROP_ENUM;
3762 property = drm_property_create(dev, flags, name, num_values);
3766 for (i = 0; i < num_values; i++) {
3767 ret = drm_property_add_enum(property, i,
3771 drm_property_destroy(dev, property);
3778 EXPORT_SYMBOL(drm_property_create_enum);
3781 * drm_property_create_bitmask - create a new bitmask property type
3783 * @flags: flags specifying the property type
3784 * @name: name of the property
3785 * @props: enumeration lists with property bitflags
3786 * @num_props: size of the @props array
3787 * @supported_bits: bitmask of all supported enumeration values
3789 * This creates a new bitmask drm property which can then be attached to a drm
3790 * object with drm_object_attach_property. The returned property object must be
3791 * freed with drm_property_destroy.
3793 * Compared to plain enumeration properties userspace is allowed to set any
3794 * or'ed together combination of the predefined property bitflag values
3797 * A pointer to the newly created property on success, NULL on failure.
3799 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3800 int flags, const char *name,
3801 const struct drm_prop_enum_list *props,
3803 uint64_t supported_bits)
3805 struct drm_property *property;
3806 int i, ret, index = 0;
3807 int num_values = hweight64(supported_bits);
3809 flags |= DRM_MODE_PROP_BITMASK;
3811 property = drm_property_create(dev, flags, name, num_values);
3814 for (i = 0; i < num_props; i++) {
3815 if (!(supported_bits & (1ULL << props[i].type)))
3818 if (WARN_ON(index >= num_values)) {
3819 drm_property_destroy(dev, property);
3823 ret = drm_property_add_enum(property, index++,
3827 drm_property_destroy(dev, property);
3834 EXPORT_SYMBOL(drm_property_create_bitmask);
3836 static struct drm_property *property_create_range(struct drm_device *dev,
3837 int flags, const char *name,
3838 uint64_t min, uint64_t max)
3840 struct drm_property *property;
3842 property = drm_property_create(dev, flags, name, 2);
3846 property->values[0] = min;
3847 property->values[1] = max;
3853 * drm_property_create_range - create a new unsigned ranged property type
3855 * @flags: flags specifying the property type
3856 * @name: name of the property
3857 * @min: minimum value of the property
3858 * @max: maximum value of the property
3860 * This creates a new generic drm property which can then be attached to a drm
3861 * object with drm_object_attach_property. The returned property object must be
3862 * freed with drm_property_destroy.
3864 * Userspace is allowed to set any unsigned integer value in the (min, max)
3868 * A pointer to the newly created property on success, NULL on failure.
3870 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3872 uint64_t min, uint64_t max)
3874 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3877 EXPORT_SYMBOL(drm_property_create_range);
3880 * drm_property_create_signed_range - create a new signed ranged property type
3882 * @flags: flags specifying the property type
3883 * @name: name of the property
3884 * @min: minimum value of the property
3885 * @max: maximum value of the property
3887 * This creates a new generic drm property which can then be attached to a drm
3888 * object with drm_object_attach_property. The returned property object must be
3889 * freed with drm_property_destroy.
3891 * Userspace is allowed to set any signed integer value in the (min, max)
3895 * A pointer to the newly created property on success, NULL on failure.
3897 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3898 int flags, const char *name,
3899 int64_t min, int64_t max)
3901 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3902 name, I642U64(min), I642U64(max));
3904 EXPORT_SYMBOL(drm_property_create_signed_range);
3907 * drm_property_create_object - create a new object property type
3909 * @flags: flags specifying the property type
3910 * @name: name of the property
3911 * @type: object type from DRM_MODE_OBJECT_* defines
3913 * This creates a new generic drm property which can then be attached to a drm
3914 * object with drm_object_attach_property. The returned property object must be
3915 * freed with drm_property_destroy.
3917 * Userspace is only allowed to set this to any property value of the given
3918 * @type. Only useful for atomic properties, which is enforced.
3921 * A pointer to the newly created property on success, NULL on failure.
3923 struct drm_property *drm_property_create_object(struct drm_device *dev,
3924 int flags, const char *name, uint32_t type)
3926 struct drm_property *property;
3928 flags |= DRM_MODE_PROP_OBJECT;
3930 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3933 property = drm_property_create(dev, flags, name, 1);
3937 property->values[0] = type;
3941 EXPORT_SYMBOL(drm_property_create_object);
3944 * drm_property_create_bool - create a new boolean property type
3946 * @flags: flags specifying the property type
3947 * @name: name of the property
3949 * This creates a new generic drm property which can then be attached to a drm
3950 * object with drm_object_attach_property. The returned property object must be
3951 * freed with drm_property_destroy.
3953 * This is implemented as a ranged property with only {0, 1} as valid values.
3956 * A pointer to the newly created property on success, NULL on failure.
3958 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3961 return drm_property_create_range(dev, flags, name, 0, 1);
3963 EXPORT_SYMBOL(drm_property_create_bool);
3966 * drm_property_add_enum - add a possible value to an enumeration property
3967 * @property: enumeration property to change
3968 * @index: index of the new enumeration
3969 * @value: value of the new enumeration
3970 * @name: symbolic name of the new enumeration
3972 * This functions adds enumerations to a property.
3974 * It's use is deprecated, drivers should use one of the more specific helpers
3975 * to directly create the property with all enumerations already attached.
3978 * Zero on success, error code on failure.
3980 int drm_property_add_enum(struct drm_property *property, int index,
3981 uint64_t value, const char *name)
3983 struct drm_property_enum *prop_enum;
3985 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3986 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3990 * Bitmask enum properties have the additional constraint of values
3993 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3997 if (!list_empty(&property->enum_list)) {
3998 list_for_each_entry(prop_enum, &property->enum_list, head) {
3999 if (prop_enum->value == value) {
4000 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4001 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4007 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4011 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4012 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4013 prop_enum->value = value;
4015 property->values[index] = value;
4016 list_add_tail(&prop_enum->head, &property->enum_list);
4019 EXPORT_SYMBOL(drm_property_add_enum);
4022 * drm_property_destroy - destroy a drm property
4024 * @property: property to destry
4026 * This function frees a property including any attached resources like
4027 * enumeration values.
4029 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4031 struct drm_property_enum *prop_enum, *pt;
4033 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4034 list_del(&prop_enum->head);
4038 if (property->num_values)
4039 kfree(property->values);
4040 drm_mode_object_unregister(dev, &property->base);
4041 list_del(&property->head);
4044 EXPORT_SYMBOL(drm_property_destroy);
4047 * drm_object_attach_property - attach a property to a modeset object
4048 * @obj: drm modeset object
4049 * @property: property to attach
4050 * @init_val: initial value of the property
4052 * This attaches the given property to the modeset object with the given initial
4053 * value. Currently this function cannot fail since the properties are stored in
4054 * a statically sized array.
4056 void drm_object_attach_property(struct drm_mode_object *obj,
4057 struct drm_property *property,
4060 int count = obj->properties->count;
4062 if (count == DRM_OBJECT_MAX_PROPERTY) {
4063 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4064 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4065 "you see this message on the same object type.\n",
4070 obj->properties->properties[count] = property;
4071 obj->properties->values[count] = init_val;
4072 obj->properties->count++;
4073 if (property->flags & DRM_MODE_PROP_ATOMIC)
4074 obj->properties->atomic_count++;
4076 EXPORT_SYMBOL(drm_object_attach_property);
4079 * drm_object_property_set_value - set the value of a property
4080 * @obj: drm mode object to set property value for
4081 * @property: property to set
4082 * @val: value the property should be set to
4084 * This functions sets a given property on a given object. This function only
4085 * changes the software state of the property, it does not call into the
4086 * driver's ->set_property callback.
4089 * Zero on success, error code on failure.
4091 int drm_object_property_set_value(struct drm_mode_object *obj,
4092 struct drm_property *property, uint64_t val)
4096 for (i = 0; i < obj->properties->count; i++) {
4097 if (obj->properties->properties[i] == property) {
4098 obj->properties->values[i] = val;
4105 EXPORT_SYMBOL(drm_object_property_set_value);
4108 * drm_object_property_get_value - retrieve the value of a property
4109 * @obj: drm mode object to get property value from
4110 * @property: property to retrieve
4111 * @val: storage for the property value
4113 * This function retrieves the softare state of the given property for the given
4114 * property. Since there is no driver callback to retrieve the current property
4115 * value this might be out of sync with the hardware, depending upon the driver
4119 * Zero on success, error code on failure.
4121 int drm_object_property_get_value(struct drm_mode_object *obj,
4122 struct drm_property *property, uint64_t *val)
4126 /* read-only properties bypass atomic mechanism and still store
4127 * their value in obj->properties->values[].. mostly to avoid
4128 * having to deal w/ EDID and similar props in atomic paths:
4130 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4131 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4132 return drm_atomic_get_property(obj, property, val);
4134 for (i = 0; i < obj->properties->count; i++) {
4135 if (obj->properties->properties[i] == property) {
4136 *val = obj->properties->values[i];
4143 EXPORT_SYMBOL(drm_object_property_get_value);
4146 * drm_mode_getproperty_ioctl - get the property metadata
4149 * @file_priv: DRM file info
4151 * This function retrieves the metadata for a given property, like the different
4152 * possible values for an enum property or the limits for a range property.
4154 * Blob properties are special
4156 * Called by the user via ioctl.
4159 * Zero on success, negative errno on failure.
4161 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4162 void *data, struct drm_file *file_priv)
4164 struct drm_mode_get_property *out_resp = data;
4165 struct drm_property *property;
4167 int value_count = 0;
4170 struct drm_property_enum *prop_enum;
4171 struct drm_mode_property_enum __user *enum_ptr;
4172 uint64_t __user *values_ptr;
4174 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4177 drm_modeset_lock_all(dev);
4178 property = drm_property_find(dev, out_resp->prop_id);
4184 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4185 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4186 list_for_each_entry(prop_enum, &property->enum_list, head)
4190 value_count = property->num_values;
4192 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4193 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4194 out_resp->flags = property->flags;
4196 if ((out_resp->count_values >= value_count) && value_count) {
4197 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4198 for (i = 0; i < value_count; i++) {
4199 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4205 out_resp->count_values = value_count;
4207 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4208 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4209 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4211 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4212 list_for_each_entry(prop_enum, &property->enum_list, head) {
4214 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4219 if (copy_to_user(&enum_ptr[copied].name,
4220 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4227 out_resp->count_enum_blobs = enum_count;
4231 * NOTE: The idea seems to have been to use this to read all the blob
4232 * property values. But nothing ever added them to the corresponding
4233 * list, userspace always used the special-purpose get_blob ioctl to
4234 * read the value for a blob property. It also doesn't make a lot of
4235 * sense to return values here when everything else is just metadata for
4236 * the property itself.
4238 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4239 out_resp->count_enum_blobs = 0;
4241 drm_modeset_unlock_all(dev);
4245 static void drm_property_free_blob(struct kref *kref)
4247 struct drm_property_blob *blob =
4248 container_of(kref, struct drm_property_blob, base.refcount);
4250 mutex_lock(&blob->dev->mode_config.blob_lock);
4251 list_del(&blob->head_global);
4252 mutex_unlock(&blob->dev->mode_config.blob_lock);
4254 drm_mode_object_unregister(blob->dev, &blob->base);
4260 * drm_property_create_blob - Create new blob property
4262 * Creates a new blob property for a specified DRM device, optionally
4265 * @dev: DRM device to create property for
4266 * @length: Length to allocate for blob data
4267 * @data: If specified, copies data into blob
4270 * New blob property with a single reference on success, or an ERR_PTR
4273 struct drm_property_blob *
4274 drm_property_create_blob(struct drm_device *dev, size_t length,
4277 struct drm_property_blob *blob;
4280 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4281 return ERR_PTR(-EINVAL);
4283 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4285 return ERR_PTR(-ENOMEM);
4287 /* This must be explicitly initialised, so we can safely call list_del
4288 * on it in the removal handler, even if it isn't in a file list. */
4289 INIT_LIST_HEAD(&blob->head_file);
4290 blob->length = length;
4294 memcpy(blob->data, data, length);
4296 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4297 true, drm_property_free_blob);
4300 return ERR_PTR(-EINVAL);
4303 mutex_lock(&dev->mode_config.blob_lock);
4304 list_add_tail(&blob->head_global,
4305 &dev->mode_config.property_blob_list);
4306 mutex_unlock(&dev->mode_config.blob_lock);
4310 EXPORT_SYMBOL(drm_property_create_blob);
4313 * drm_property_unreference_blob - Unreference a blob property
4315 * Drop a reference on a blob property. May free the object.
4317 * @blob: Pointer to blob property
4319 void drm_property_unreference_blob(struct drm_property_blob *blob)
4324 drm_mode_object_unreference(&blob->base);
4326 EXPORT_SYMBOL(drm_property_unreference_blob);
4329 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4331 * @file_priv: destroy all blobs owned by this file handle
4333 void drm_property_destroy_user_blobs(struct drm_device *dev,
4334 struct drm_file *file_priv)
4336 struct drm_property_blob *blob, *bt;
4339 * When the file gets released that means no one else can access the
4340 * blob list any more, so no need to grab dev->blob_lock.
4342 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4343 list_del_init(&blob->head_file);
4344 drm_property_unreference_blob(blob);
4349 * drm_property_reference_blob - Take a reference on an existing property
4351 * Take a new reference on an existing blob property.
4353 * @blob: Pointer to blob property
4355 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4357 drm_mode_object_reference(&blob->base);
4360 EXPORT_SYMBOL(drm_property_reference_blob);
4363 * drm_property_lookup_blob - look up a blob property and take a reference
4365 * @id: id of the blob property
4367 * If successful, this takes an additional reference to the blob property.
4368 * callers need to make sure to eventually unreference the returned property
4369 * again, using @drm_property_unreference_blob.
4371 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4374 struct drm_mode_object *obj;
4375 struct drm_property_blob *blob = NULL;
4377 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4379 blob = obj_to_blob(obj);
4382 EXPORT_SYMBOL(drm_property_lookup_blob);
4385 * drm_property_replace_global_blob - atomically replace existing blob property
4387 * @replace: location of blob property pointer to be replaced
4388 * @length: length of data for new blob, or 0 for no data
4389 * @data: content for new blob, or NULL for no data
4390 * @obj_holds_id: optional object for property holding blob ID
4391 * @prop_holds_id: optional property holding blob ID
4392 * @return 0 on success or error on failure
4394 * This function will atomically replace a global property in the blob list,
4395 * optionally updating a property which holds the ID of that property. It is
4396 * guaranteed to be atomic: no caller will be allowed to see intermediate
4397 * results, and either the entire operation will succeed and clean up the
4398 * previous property, or it will fail and the state will be unchanged.
4400 * If length is 0 or data is NULL, no new blob will be created, and the holding
4401 * property, if specified, will be set to 0.
4403 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4404 * by holding the relevant modesetting object lock for its parent.
4406 * For example, a drm_connector has a 'PATH' property, which contains the ID
4407 * of a blob property with the value of the MST path information. Calling this
4408 * function with replace pointing to the connector's path_blob_ptr, length and
4409 * data set for the new path information, obj_holds_id set to the connector's
4410 * base object, and prop_holds_id set to the path property name, will perform
4411 * a completely atomic update. The access to path_blob_ptr is protected by the
4412 * caller holding a lock on the connector.
4414 static int drm_property_replace_global_blob(struct drm_device *dev,
4415 struct drm_property_blob **replace,
4418 struct drm_mode_object *obj_holds_id,
4419 struct drm_property *prop_holds_id)
4421 struct drm_property_blob *new_blob = NULL;
4422 struct drm_property_blob *old_blob = NULL;
4425 WARN_ON(replace == NULL);
4427 old_blob = *replace;
4429 if (length && data) {
4430 new_blob = drm_property_create_blob(dev, length, data);
4431 if (IS_ERR(new_blob))
4432 return PTR_ERR(new_blob);
4435 /* This does not need to be synchronised with blob_lock, as the
4436 * get_properties ioctl locks all modesetting objects, and
4437 * obj_holds_id must be locked before calling here, so we cannot
4438 * have its value out of sync with the list membership modified
4439 * below under blob_lock. */
4441 ret = drm_object_property_set_value(obj_holds_id,
4444 new_blob->base.id : 0);
4449 drm_property_unreference_blob(old_blob);
4450 *replace = new_blob;
4455 drm_property_unreference_blob(new_blob);
4460 * drm_mode_getblob_ioctl - get the contents of a blob property value
4463 * @file_priv: DRM file info
4465 * This function retrieves the contents of a blob property. The value stored in
4466 * an object's blob property is just a normal modeset object id.
4468 * Called by the user via ioctl.
4471 * Zero on success, negative errno on failure.
4473 int drm_mode_getblob_ioctl(struct drm_device *dev,
4474 void *data, struct drm_file *file_priv)
4476 struct drm_mode_get_blob *out_resp = data;
4477 struct drm_property_blob *blob;
4479 void __user *blob_ptr;
4481 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4484 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4488 if (out_resp->length == blob->length) {
4489 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4490 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4495 out_resp->length = blob->length;
4497 drm_property_unreference_blob(blob);
4503 * drm_mode_createblob_ioctl - create a new blob property
4506 * @file_priv: DRM file info
4508 * This function creates a new blob property with user-defined values. In order
4509 * to give us sensible validation and checking when creating, rather than at
4510 * every potential use, we also require a type to be provided upfront.
4512 * Called by the user via ioctl.
4515 * Zero on success, negative errno on failure.
4517 int drm_mode_createblob_ioctl(struct drm_device *dev,
4518 void *data, struct drm_file *file_priv)
4520 struct drm_mode_create_blob *out_resp = data;
4521 struct drm_property_blob *blob;
4522 void __user *blob_ptr;
4525 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4528 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4530 return PTR_ERR(blob);
4532 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4533 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4538 /* Dropping the lock between create_blob and our access here is safe
4539 * as only the same file_priv can remove the blob; at this point, it is
4540 * not associated with any file_priv. */
4541 mutex_lock(&dev->mode_config.blob_lock);
4542 out_resp->blob_id = blob->base.id;
4543 list_add_tail(&blob->head_file, &file_priv->blobs);
4544 mutex_unlock(&dev->mode_config.blob_lock);
4549 drm_property_unreference_blob(blob);
4554 * drm_mode_destroyblob_ioctl - destroy a user blob property
4557 * @file_priv: DRM file info
4559 * Destroy an existing user-defined blob property.
4561 * Called by the user via ioctl.
4564 * Zero on success, negative errno on failure.
4566 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4567 void *data, struct drm_file *file_priv)
4569 struct drm_mode_destroy_blob *out_resp = data;
4570 struct drm_property_blob *blob = NULL, *bt;
4574 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4577 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4581 mutex_lock(&dev->mode_config.blob_lock);
4582 /* Ensure the property was actually created by this user. */
4583 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4595 /* We must drop head_file here, because we may not be the last
4596 * reference on the blob. */
4597 list_del_init(&blob->head_file);
4598 mutex_unlock(&dev->mode_config.blob_lock);
4600 /* One reference from lookup, and one from the filp. */
4601 drm_property_unreference_blob(blob);
4602 drm_property_unreference_blob(blob);
4607 mutex_unlock(&dev->mode_config.blob_lock);
4608 drm_property_unreference_blob(blob);
4614 * drm_mode_connector_set_path_property - set tile property on connector
4615 * @connector: connector to set property on.
4616 * @path: path to use for property; must not be NULL.
4618 * This creates a property to expose to userspace to specify a
4619 * connector path. This is mainly used for DisplayPort MST where
4620 * connectors have a topology and we want to allow userspace to give
4621 * them more meaningful names.
4624 * Zero on success, negative errno on failure.
4626 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4629 struct drm_device *dev = connector->dev;
4632 ret = drm_property_replace_global_blob(dev,
4633 &connector->path_blob_ptr,
4637 dev->mode_config.path_property);
4640 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4643 * drm_mode_connector_set_tile_property - set tile property on connector
4644 * @connector: connector to set property on.
4646 * This looks up the tile information for a connector, and creates a
4647 * property for userspace to parse if it exists. The property is of
4648 * the form of 8 integers using ':' as a separator.
4651 * Zero on success, errno on failure.
4653 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4655 struct drm_device *dev = connector->dev;
4659 if (!connector->has_tile) {
4660 ret = drm_property_replace_global_blob(dev,
4661 &connector->tile_blob_ptr,
4665 dev->mode_config.tile_property);
4669 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4670 connector->tile_group->id, connector->tile_is_single_monitor,
4671 connector->num_h_tile, connector->num_v_tile,
4672 connector->tile_h_loc, connector->tile_v_loc,
4673 connector->tile_h_size, connector->tile_v_size);
4675 ret = drm_property_replace_global_blob(dev,
4676 &connector->tile_blob_ptr,
4680 dev->mode_config.tile_property);
4683 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4686 * drm_mode_connector_update_edid_property - update the edid property of a connector
4687 * @connector: drm connector
4688 * @edid: new value of the edid property
4690 * This function creates a new blob modeset object and assigns its id to the
4691 * connector's edid property.
4694 * Zero on success, negative errno on failure.
4696 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4697 const struct edid *edid)
4699 struct drm_device *dev = connector->dev;
4703 /* ignore requests to set edid when overridden */
4704 if (connector->override_edid)
4708 size = EDID_LENGTH * (1 + edid->extensions);
4710 ret = drm_property_replace_global_blob(dev,
4711 &connector->edid_blob_ptr,
4715 dev->mode_config.edid_property);
4718 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4720 /* Some properties could refer to dynamic refcnt'd objects, or things that
4721 * need special locking to handle lifetime issues (ie. to ensure the prop
4722 * value doesn't become invalid part way through the property update due to
4723 * race). The value returned by reference via 'obj' should be passed back
4724 * to drm_property_change_valid_put() after the property is set (and the
4725 * object to which the property is attached has a chance to take it's own
4728 bool drm_property_change_valid_get(struct drm_property *property,
4729 uint64_t value, struct drm_mode_object **ref)
4733 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4738 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4739 if (value < property->values[0] || value > property->values[1])
4742 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4743 int64_t svalue = U642I64(value);
4745 if (svalue < U642I64(property->values[0]) ||
4746 svalue > U642I64(property->values[1]))
4749 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4750 uint64_t valid_mask = 0;
4752 for (i = 0; i < property->num_values; i++)
4753 valid_mask |= (1ULL << property->values[i]);
4754 return !(value & ~valid_mask);
4755 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4756 struct drm_property_blob *blob;
4761 blob = drm_property_lookup_blob(property->dev, value);
4768 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4769 /* a zero value for an object property translates to null: */
4773 return _object_find(property->dev, value, property->values[0]) != NULL;
4776 for (i = 0; i < property->num_values; i++)
4777 if (property->values[i] == value)
4782 void drm_property_change_valid_put(struct drm_property *property,
4783 struct drm_mode_object *ref)
4788 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4789 drm_mode_object_unreference(ref);
4790 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4791 drm_property_unreference_blob(obj_to_blob(ref));
4795 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4798 * @file_priv: DRM file info
4800 * This function sets the current value for a connectors's property. It also
4801 * calls into a driver's ->set_property callback to update the hardware state
4803 * Called by the user via ioctl.
4806 * Zero on success, negative errno on failure.
4808 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4809 void *data, struct drm_file *file_priv)
4811 struct drm_mode_connector_set_property *conn_set_prop = data;
4812 struct drm_mode_obj_set_property obj_set_prop = {
4813 .value = conn_set_prop->value,
4814 .prop_id = conn_set_prop->prop_id,
4815 .obj_id = conn_set_prop->connector_id,
4816 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4819 /* It does all the locking and checking we need */
4820 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4823 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4824 struct drm_property *property,
4828 struct drm_connector *connector = obj_to_connector(obj);
4830 /* Do DPMS ourselves */
4831 if (property == connector->dev->mode_config.dpms_property) {
4832 ret = (*connector->funcs->dpms)(connector, (int)value);
4833 } else if (connector->funcs->set_property)
4834 ret = connector->funcs->set_property(connector, property, value);
4836 /* store the property value if successful */
4838 drm_object_property_set_value(&connector->base, property, value);
4842 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4843 struct drm_property *property,
4847 struct drm_crtc *crtc = obj_to_crtc(obj);
4849 if (crtc->funcs->set_property)
4850 ret = crtc->funcs->set_property(crtc, property, value);
4852 drm_object_property_set_value(obj, property, value);
4858 * drm_mode_plane_set_obj_prop - set the value of a property
4859 * @plane: drm plane object to set property value for
4860 * @property: property to set
4861 * @value: value the property should be set to
4863 * This functions sets a given property on a given plane object. This function
4864 * calls the driver's ->set_property callback and changes the software state of
4865 * the property if the callback succeeds.
4868 * Zero on success, error code on failure.
4870 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4871 struct drm_property *property,
4875 struct drm_mode_object *obj = &plane->base;
4877 if (plane->funcs->set_property)
4878 ret = plane->funcs->set_property(plane, property, value);
4880 drm_object_property_set_value(obj, property, value);
4884 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4887 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4890 * @file_priv: DRM file info
4892 * This function retrieves the current value for an object's property. Compared
4893 * to the connector specific ioctl this one is extended to also work on crtc and
4896 * Called by the user via ioctl.
4899 * Zero on success, negative errno on failure.
4901 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4902 struct drm_file *file_priv)
4904 struct drm_mode_obj_get_properties *arg = data;
4905 struct drm_mode_object *obj;
4908 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4911 drm_modeset_lock_all(dev);
4913 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4918 if (!obj->properties) {
4923 ret = get_properties(obj, file_priv->atomic,
4924 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4925 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4929 drm_mode_object_unreference(obj);
4931 drm_modeset_unlock_all(dev);
4936 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4939 * @file_priv: DRM file info
4941 * This function sets the current value for an object's property. It also calls
4942 * into a driver's ->set_property callback to update the hardware state.
4943 * Compared to the connector specific ioctl this one is extended to also work on
4944 * crtc and plane objects.
4946 * Called by the user via ioctl.
4949 * Zero on success, negative errno on failure.
4951 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4952 struct drm_file *file_priv)
4954 struct drm_mode_obj_set_property *arg = data;
4955 struct drm_mode_object *arg_obj;
4956 struct drm_mode_object *prop_obj;
4957 struct drm_property *property;
4958 int i, ret = -EINVAL;
4959 struct drm_mode_object *ref;
4961 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4964 drm_modeset_lock_all(dev);
4966 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4971 if (!arg_obj->properties)
4974 for (i = 0; i < arg_obj->properties->count; i++)
4975 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
4978 if (i == arg_obj->properties->count)
4981 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4982 DRM_MODE_OBJECT_PROPERTY);
4987 property = obj_to_property(prop_obj);
4989 if (!drm_property_change_valid_get(property, arg->value, &ref))
4992 switch (arg_obj->type) {
4993 case DRM_MODE_OBJECT_CONNECTOR:
4994 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4997 case DRM_MODE_OBJECT_CRTC:
4998 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5000 case DRM_MODE_OBJECT_PLANE:
5001 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5002 property, arg->value);
5006 drm_property_change_valid_put(property, ref);
5009 drm_mode_object_unreference(arg_obj);
5011 drm_modeset_unlock_all(dev);
5016 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5017 * @connector: connector to attach
5018 * @encoder: encoder to attach @connector to
5020 * This function links up a connector to an encoder. Note that the routing
5021 * restrictions between encoders and crtcs are exposed to userspace through the
5022 * possible_clones and possible_crtcs bitmasks.
5025 * Zero on success, negative errno on failure.
5027 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5028 struct drm_encoder *encoder)
5033 * In the past, drivers have attempted to model the static association
5034 * of connector to encoder in simple connector/encoder devices using a
5035 * direct assignment of connector->encoder = encoder. This connection
5036 * is a logical one and the responsibility of the core, so drivers are
5037 * expected not to mess with this.
5039 * Note that the error return should've been enough here, but a large
5040 * majority of drivers ignores the return value, so add in a big WARN
5041 * to get people's attention.
5043 if (WARN_ON(connector->encoder))
5046 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5047 if (connector->encoder_ids[i] == 0) {
5048 connector->encoder_ids[i] = encoder->base.id;
5054 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5057 * drm_mode_crtc_set_gamma_size - set the gamma table size
5058 * @crtc: CRTC to set the gamma table size for
5059 * @gamma_size: size of the gamma table
5061 * Drivers which support gamma tables should set this to the supported gamma
5062 * table size when initializing the CRTC. Currently the drm core only supports a
5063 * fixed gamma table size.
5066 * Zero on success, negative errno on failure.
5068 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5071 crtc->gamma_size = gamma_size;
5073 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5075 if (!crtc->gamma_store) {
5076 crtc->gamma_size = 0;
5082 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5085 * drm_mode_gamma_set_ioctl - set the gamma table
5088 * @file_priv: DRM file info
5090 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5091 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5093 * Called by the user via ioctl.
5096 * Zero on success, negative errno on failure.
5098 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5099 void *data, struct drm_file *file_priv)
5101 struct drm_mode_crtc_lut *crtc_lut = data;
5102 struct drm_crtc *crtc;
5103 void *r_base, *g_base, *b_base;
5107 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5110 drm_modeset_lock_all(dev);
5111 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5117 if (crtc->funcs->gamma_set == NULL) {
5122 /* memcpy into gamma store */
5123 if (crtc_lut->gamma_size != crtc->gamma_size) {
5128 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5129 r_base = crtc->gamma_store;
5130 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5135 g_base = r_base + size;
5136 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5141 b_base = g_base + size;
5142 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5147 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5150 drm_modeset_unlock_all(dev);
5156 * drm_mode_gamma_get_ioctl - get the gamma table
5159 * @file_priv: DRM file info
5161 * Copy the current gamma table into the storage provided. This also provides
5162 * the gamma table size the driver expects, which can be used to size the
5163 * allocated storage.
5165 * Called by the user via ioctl.
5168 * Zero on success, negative errno on failure.
5170 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5171 void *data, struct drm_file *file_priv)
5173 struct drm_mode_crtc_lut *crtc_lut = data;
5174 struct drm_crtc *crtc;
5175 void *r_base, *g_base, *b_base;
5179 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5182 drm_modeset_lock_all(dev);
5183 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5189 /* memcpy into gamma store */
5190 if (crtc_lut->gamma_size != crtc->gamma_size) {
5195 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5196 r_base = crtc->gamma_store;
5197 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5202 g_base = r_base + size;
5203 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5208 b_base = g_base + size;
5209 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5214 drm_modeset_unlock_all(dev);
5219 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5222 * @file_priv: DRM file info
5224 * This schedules an asynchronous update on a given CRTC, called page flip.
5225 * Optionally a drm event is generated to signal the completion of the event.
5226 * Generic drivers cannot assume that a pageflip with changed framebuffer
5227 * properties (including driver specific metadata like tiling layout) will work,
5228 * but some drivers support e.g. pixel format changes through the pageflip
5231 * Called by the user via ioctl.
5234 * Zero on success, negative errno on failure.
5236 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5237 void *data, struct drm_file *file_priv)
5239 struct drm_mode_crtc_page_flip *page_flip = data;
5240 struct drm_crtc *crtc;
5241 struct drm_framebuffer *fb = NULL;
5242 struct drm_pending_vblank_event *e = NULL;
5245 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5246 page_flip->reserved != 0)
5249 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5252 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5256 drm_modeset_lock_crtc(crtc, crtc->primary);
5257 if (crtc->primary->fb == NULL) {
5258 /* The framebuffer is currently unbound, presumably
5259 * due to a hotplug event, that userspace has not
5266 if (crtc->funcs->page_flip == NULL)
5269 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5276 const struct drm_plane_state *state = crtc->primary->state;
5278 ret = check_src_coords(state->src_x, state->src_y,
5279 state->src_w, state->src_h, fb);
5281 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5286 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5287 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5292 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5293 e = kzalloc(sizeof *e, GFP_KERNEL);
5298 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5299 e->event.base.length = sizeof(e->event);
5300 e->event.user_data = page_flip->user_data;
5301 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5308 crtc->primary->old_fb = crtc->primary->fb;
5309 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5311 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5312 drm_event_cancel_free(dev, &e->base);
5313 /* Keep the old fb, don't unref it. */
5314 crtc->primary->old_fb = NULL;
5316 crtc->primary->fb = fb;
5317 /* Unref only the old framebuffer. */
5323 drm_framebuffer_unreference(fb);
5324 if (crtc->primary->old_fb)
5325 drm_framebuffer_unreference(crtc->primary->old_fb);
5326 crtc->primary->old_fb = NULL;
5327 drm_modeset_unlock_crtc(crtc);
5333 * drm_mode_config_reset - call ->reset callbacks
5336 * This functions calls all the crtc's, encoder's and connector's ->reset
5337 * callback. Drivers can use this in e.g. their driver load or resume code to
5338 * reset hardware and software state.
5340 void drm_mode_config_reset(struct drm_device *dev)
5342 struct drm_crtc *crtc;
5343 struct drm_plane *plane;
5344 struct drm_encoder *encoder;
5345 struct drm_connector *connector;
5347 drm_for_each_plane(plane, dev)
5348 if (plane->funcs->reset)
5349 plane->funcs->reset(plane);
5351 drm_for_each_crtc(crtc, dev)
5352 if (crtc->funcs->reset)
5353 crtc->funcs->reset(crtc);
5355 drm_for_each_encoder(encoder, dev)
5356 if (encoder->funcs->reset)
5357 encoder->funcs->reset(encoder);
5359 mutex_lock(&dev->mode_config.mutex);
5360 drm_for_each_connector(connector, dev)
5361 if (connector->funcs->reset)
5362 connector->funcs->reset(connector);
5363 mutex_unlock(&dev->mode_config.mutex);
5365 EXPORT_SYMBOL(drm_mode_config_reset);
5368 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5371 * @file_priv: DRM file info
5373 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5374 * TTM or something else entirely) and returns the resulting buffer handle. This
5375 * handle can then be wrapped up into a framebuffer modeset object.
5377 * Note that userspace is not allowed to use such objects for render
5378 * acceleration - drivers must create their own private ioctls for such a use
5381 * Called by the user via ioctl.
5384 * Zero on success, negative errno on failure.
5386 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5387 void *data, struct drm_file *file_priv)
5389 struct drm_mode_create_dumb *args = data;
5390 u32 cpp, stride, size;
5392 if (!dev->driver->dumb_create)
5394 if (!args->width || !args->height || !args->bpp)
5397 /* overflow checks for 32bit size calculations */
5398 /* NOTE: DIV_ROUND_UP() can overflow */
5399 cpp = DIV_ROUND_UP(args->bpp, 8);
5400 if (!cpp || cpp > 0xffffffffU / args->width)
5402 stride = cpp * args->width;
5403 if (args->height > 0xffffffffU / stride)
5406 /* test for wrap-around */
5407 size = args->height * stride;
5408 if (PAGE_ALIGN(size) == 0)
5412 * handle, pitch and size are output parameters. Zero them out to
5413 * prevent drivers from accidentally using uninitialized data. Since
5414 * not all existing userspace is clearing these fields properly we
5415 * cannot reject IOCTL with garbage in them.
5421 return dev->driver->dumb_create(file_priv, dev, args);
5425 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5428 * @file_priv: DRM file info
5430 * Allocate an offset in the drm device node's address space to be able to
5431 * memory map a dumb buffer.
5433 * Called by the user via ioctl.
5436 * Zero on success, negative errno on failure.
5438 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5439 void *data, struct drm_file *file_priv)
5441 struct drm_mode_map_dumb *args = data;
5443 /* call driver ioctl to get mmap offset */
5444 if (!dev->driver->dumb_map_offset)
5447 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5451 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5454 * @file_priv: DRM file info
5456 * This destroys the userspace handle for the given dumb backing storage buffer.
5457 * Since buffer objects must be reference counted in the kernel a buffer object
5458 * won't be immediately freed if a framebuffer modeset object still uses it.
5460 * Called by the user via ioctl.
5463 * Zero on success, negative errno on failure.
5465 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5466 void *data, struct drm_file *file_priv)
5468 struct drm_mode_destroy_dumb *args = data;
5470 if (!dev->driver->dumb_destroy)
5473 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5477 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5478 * @format: pixel format (DRM_FORMAT_*)
5479 * @depth: storage for the depth value
5480 * @bpp: storage for the bpp value
5482 * This only supports RGB formats here for compat with code that doesn't use
5483 * pixel formats directly yet.
5485 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5490 case DRM_FORMAT_RGB332:
5491 case DRM_FORMAT_BGR233:
5495 case DRM_FORMAT_XRGB1555:
5496 case DRM_FORMAT_XBGR1555:
5497 case DRM_FORMAT_RGBX5551:
5498 case DRM_FORMAT_BGRX5551:
5499 case DRM_FORMAT_ARGB1555:
5500 case DRM_FORMAT_ABGR1555:
5501 case DRM_FORMAT_RGBA5551:
5502 case DRM_FORMAT_BGRA5551:
5506 case DRM_FORMAT_RGB565:
5507 case DRM_FORMAT_BGR565:
5511 case DRM_FORMAT_RGB888:
5512 case DRM_FORMAT_BGR888:
5516 case DRM_FORMAT_XRGB8888:
5517 case DRM_FORMAT_XBGR8888:
5518 case DRM_FORMAT_RGBX8888:
5519 case DRM_FORMAT_BGRX8888:
5523 case DRM_FORMAT_XRGB2101010:
5524 case DRM_FORMAT_XBGR2101010:
5525 case DRM_FORMAT_RGBX1010102:
5526 case DRM_FORMAT_BGRX1010102:
5527 case DRM_FORMAT_ARGB2101010:
5528 case DRM_FORMAT_ABGR2101010:
5529 case DRM_FORMAT_RGBA1010102:
5530 case DRM_FORMAT_BGRA1010102:
5534 case DRM_FORMAT_ARGB8888:
5535 case DRM_FORMAT_ABGR8888:
5536 case DRM_FORMAT_RGBA8888:
5537 case DRM_FORMAT_BGRA8888:
5542 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5543 drm_get_format_name(format));
5549 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5552 * drm_format_num_planes - get the number of planes for format
5553 * @format: pixel format (DRM_FORMAT_*)
5556 * The number of planes used by the specified pixel format.
5558 int drm_format_num_planes(uint32_t format)
5561 case DRM_FORMAT_YUV410:
5562 case DRM_FORMAT_YVU410:
5563 case DRM_FORMAT_YUV411:
5564 case DRM_FORMAT_YVU411:
5565 case DRM_FORMAT_YUV420:
5566 case DRM_FORMAT_YVU420:
5567 case DRM_FORMAT_YUV422:
5568 case DRM_FORMAT_YVU422:
5569 case DRM_FORMAT_YUV444:
5570 case DRM_FORMAT_YVU444:
5572 case DRM_FORMAT_NV12:
5573 case DRM_FORMAT_NV21:
5574 case DRM_FORMAT_NV16:
5575 case DRM_FORMAT_NV61:
5576 case DRM_FORMAT_NV24:
5577 case DRM_FORMAT_NV42:
5583 EXPORT_SYMBOL(drm_format_num_planes);
5586 * drm_format_plane_cpp - determine the bytes per pixel value
5587 * @format: pixel format (DRM_FORMAT_*)
5588 * @plane: plane index
5591 * The bytes per pixel value for the specified plane.
5593 int drm_format_plane_cpp(uint32_t format, int plane)
5598 if (plane >= drm_format_num_planes(format))
5602 case DRM_FORMAT_YUYV:
5603 case DRM_FORMAT_YVYU:
5604 case DRM_FORMAT_UYVY:
5605 case DRM_FORMAT_VYUY:
5607 case DRM_FORMAT_NV12:
5608 case DRM_FORMAT_NV21:
5609 case DRM_FORMAT_NV16:
5610 case DRM_FORMAT_NV61:
5611 case DRM_FORMAT_NV24:
5612 case DRM_FORMAT_NV42:
5613 return plane ? 2 : 1;
5614 case DRM_FORMAT_YUV410:
5615 case DRM_FORMAT_YVU410:
5616 case DRM_FORMAT_YUV411:
5617 case DRM_FORMAT_YVU411:
5618 case DRM_FORMAT_YUV420:
5619 case DRM_FORMAT_YVU420:
5620 case DRM_FORMAT_YUV422:
5621 case DRM_FORMAT_YVU422:
5622 case DRM_FORMAT_YUV444:
5623 case DRM_FORMAT_YVU444:
5626 drm_fb_get_bpp_depth(format, &depth, &bpp);
5630 EXPORT_SYMBOL(drm_format_plane_cpp);
5633 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5634 * @format: pixel format (DRM_FORMAT_*)
5637 * The horizontal chroma subsampling factor for the
5638 * specified pixel format.
5640 int drm_format_horz_chroma_subsampling(uint32_t format)
5643 case DRM_FORMAT_YUV411:
5644 case DRM_FORMAT_YVU411:
5645 case DRM_FORMAT_YUV410:
5646 case DRM_FORMAT_YVU410:
5648 case DRM_FORMAT_YUYV:
5649 case DRM_FORMAT_YVYU:
5650 case DRM_FORMAT_UYVY:
5651 case DRM_FORMAT_VYUY:
5652 case DRM_FORMAT_NV12:
5653 case DRM_FORMAT_NV21:
5654 case DRM_FORMAT_NV16:
5655 case DRM_FORMAT_NV61:
5656 case DRM_FORMAT_YUV422:
5657 case DRM_FORMAT_YVU422:
5658 case DRM_FORMAT_YUV420:
5659 case DRM_FORMAT_YVU420:
5665 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5668 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5669 * @format: pixel format (DRM_FORMAT_*)
5672 * The vertical chroma subsampling factor for the
5673 * specified pixel format.
5675 int drm_format_vert_chroma_subsampling(uint32_t format)
5678 case DRM_FORMAT_YUV410:
5679 case DRM_FORMAT_YVU410:
5681 case DRM_FORMAT_YUV420:
5682 case DRM_FORMAT_YVU420:
5683 case DRM_FORMAT_NV12:
5684 case DRM_FORMAT_NV21:
5690 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5693 * drm_format_plane_width - width of the plane given the first plane
5694 * @width: width of the first plane
5695 * @format: pixel format
5696 * @plane: plane index
5699 * The width of @plane, given that the width of the first plane is @width.
5701 int drm_format_plane_width(int width, uint32_t format, int plane)
5703 if (plane >= drm_format_num_planes(format))
5709 return width / drm_format_horz_chroma_subsampling(format);
5711 EXPORT_SYMBOL(drm_format_plane_width);
5714 * drm_format_plane_height - height of the plane given the first plane
5715 * @height: height of the first plane
5716 * @format: pixel format
5717 * @plane: plane index
5720 * The height of @plane, given that the height of the first plane is @height.
5722 int drm_format_plane_height(int height, uint32_t format, int plane)
5724 if (plane >= drm_format_num_planes(format))
5730 return height / drm_format_vert_chroma_subsampling(format);
5732 EXPORT_SYMBOL(drm_format_plane_height);
5735 * drm_rotation_simplify() - Try to simplify the rotation
5736 * @rotation: Rotation to be simplified
5737 * @supported_rotations: Supported rotations
5739 * Attempt to simplify the rotation to a form that is supported.
5740 * Eg. if the hardware supports everything except DRM_REFLECT_X
5741 * one could call this function like this:
5743 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5744 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5745 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5747 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5748 * transforms the hardware supports, this function may not
5749 * be able to produce a supported transform, so the caller should
5750 * check the result afterwards.
5752 unsigned int drm_rotation_simplify(unsigned int rotation,
5753 unsigned int supported_rotations)
5755 if (rotation & ~supported_rotations) {
5756 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5757 rotation = (rotation & DRM_REFLECT_MASK) |
5758 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5763 EXPORT_SYMBOL(drm_rotation_simplify);
5766 * drm_mode_config_init - initialize DRM mode_configuration structure
5769 * Initialize @dev's mode_config structure, used for tracking the graphics
5770 * configuration of @dev.
5772 * Since this initializes the modeset locks, no locking is possible. Which is no
5773 * problem, since this should happen single threaded at init time. It is the
5774 * driver's problem to ensure this guarantee.
5777 void drm_mode_config_init(struct drm_device *dev)
5779 mutex_init(&dev->mode_config.mutex);
5780 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5781 mutex_init(&dev->mode_config.idr_mutex);
5782 mutex_init(&dev->mode_config.fb_lock);
5783 mutex_init(&dev->mode_config.blob_lock);
5784 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5785 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5786 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5787 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5788 INIT_LIST_HEAD(&dev->mode_config.property_list);
5789 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5790 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5791 idr_init(&dev->mode_config.crtc_idr);
5792 idr_init(&dev->mode_config.tile_idr);
5793 ida_init(&dev->mode_config.connector_ida);
5795 drm_modeset_lock_all(dev);
5796 drm_mode_create_standard_properties(dev);
5797 drm_modeset_unlock_all(dev);
5799 /* Just to be sure */
5800 dev->mode_config.num_fb = 0;
5801 dev->mode_config.num_connector = 0;
5802 dev->mode_config.num_crtc = 0;
5803 dev->mode_config.num_encoder = 0;
5804 dev->mode_config.num_overlay_plane = 0;
5805 dev->mode_config.num_total_plane = 0;
5807 EXPORT_SYMBOL(drm_mode_config_init);
5810 * drm_mode_config_cleanup - free up DRM mode_config info
5813 * Free up all the connectors and CRTCs associated with this DRM device, then
5814 * free up the framebuffers and associated buffer objects.
5816 * Note that since this /should/ happen single-threaded at driver/device
5817 * teardown time, no locking is required. It's the driver's job to ensure that
5818 * this guarantee actually holds true.
5820 * FIXME: cleanup any dangling user buffer objects too
5822 void drm_mode_config_cleanup(struct drm_device *dev)
5824 struct drm_connector *connector, *ot;
5825 struct drm_crtc *crtc, *ct;
5826 struct drm_encoder *encoder, *enct;
5827 struct drm_framebuffer *fb, *fbt;
5828 struct drm_property *property, *pt;
5829 struct drm_property_blob *blob, *bt;
5830 struct drm_plane *plane, *plt;
5832 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5834 encoder->funcs->destroy(encoder);
5837 list_for_each_entry_safe(connector, ot,
5838 &dev->mode_config.connector_list, head) {
5839 connector->funcs->destroy(connector);
5842 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5844 drm_property_destroy(dev, property);
5847 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5849 plane->funcs->destroy(plane);
5852 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5853 crtc->funcs->destroy(crtc);
5856 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5858 drm_property_unreference_blob(blob);
5862 * Single-threaded teardown context, so it's not required to grab the
5863 * fb_lock to protect against concurrent fb_list access. Contrary, it
5864 * would actually deadlock with the drm_framebuffer_cleanup function.
5866 * Also, if there are any framebuffers left, that's a driver leak now,
5867 * so politely WARN about this.
5869 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5870 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5871 drm_framebuffer_free(&fb->base.refcount);
5874 ida_destroy(&dev->mode_config.connector_ida);
5875 idr_destroy(&dev->mode_config.tile_idr);
5876 idr_destroy(&dev->mode_config.crtc_idr);
5877 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5879 EXPORT_SYMBOL(drm_mode_config_cleanup);
5881 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5882 unsigned int supported_rotations)
5884 static const struct drm_prop_enum_list props[] = {
5885 { DRM_ROTATE_0, "rotate-0" },
5886 { DRM_ROTATE_90, "rotate-90" },
5887 { DRM_ROTATE_180, "rotate-180" },
5888 { DRM_ROTATE_270, "rotate-270" },
5889 { DRM_REFLECT_X, "reflect-x" },
5890 { DRM_REFLECT_Y, "reflect-y" },
5893 return drm_property_create_bitmask(dev, 0, "rotation",
5894 props, ARRAY_SIZE(props),
5895 supported_rotations);
5897 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5902 * Tile groups are used to represent tiled monitors with a unique
5903 * integer identifier. Tiled monitors using DisplayID v1.3 have
5904 * a unique 8-byte handle, we store this in a tile group, so we
5905 * have a common identifier for all tiles in a monitor group.
5907 static void drm_tile_group_free(struct kref *kref)
5909 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5910 struct drm_device *dev = tg->dev;
5911 mutex_lock(&dev->mode_config.idr_mutex);
5912 idr_remove(&dev->mode_config.tile_idr, tg->id);
5913 mutex_unlock(&dev->mode_config.idr_mutex);
5918 * drm_mode_put_tile_group - drop a reference to a tile group.
5920 * @tg: tile group to drop reference to.
5922 * drop reference to tile group and free if 0.
5924 void drm_mode_put_tile_group(struct drm_device *dev,
5925 struct drm_tile_group *tg)
5927 kref_put(&tg->refcount, drm_tile_group_free);
5931 * drm_mode_get_tile_group - get a reference to an existing tile group
5933 * @topology: 8-bytes unique per monitor.
5935 * Use the unique bytes to get a reference to an existing tile group.
5938 * tile group or NULL if not found.
5940 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5943 struct drm_tile_group *tg;
5945 mutex_lock(&dev->mode_config.idr_mutex);
5946 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5947 if (!memcmp(tg->group_data, topology, 8)) {
5948 if (!kref_get_unless_zero(&tg->refcount))
5950 mutex_unlock(&dev->mode_config.idr_mutex);
5954 mutex_unlock(&dev->mode_config.idr_mutex);
5957 EXPORT_SYMBOL(drm_mode_get_tile_group);
5960 * drm_mode_create_tile_group - create a tile group from a displayid description
5962 * @topology: 8-bytes unique per monitor.
5964 * Create a tile group for the unique monitor, and get a unique
5965 * identifier for the tile group.
5968 * new tile group or error.
5970 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5973 struct drm_tile_group *tg;
5976 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5978 return ERR_PTR(-ENOMEM);
5980 kref_init(&tg->refcount);
5981 memcpy(tg->group_data, topology, 8);
5984 mutex_lock(&dev->mode_config.idr_mutex);
5985 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5993 mutex_unlock(&dev->mode_config.idr_mutex);
5996 EXPORT_SYMBOL(drm_mode_create_tile_group);