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 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,
282 mutex_lock(&dev->mode_config.idr_mutex);
283 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
286 * Set up the object linking under the protection of the idr
287 * lock so that other users can't see inconsistent state.
290 obj->type = obj_type;
292 mutex_unlock(&dev->mode_config.idr_mutex);
294 return ret < 0 ? ret : 0;
298 * drm_mode_object_get - allocate a new modeset identifier
300 * @obj: object pointer, used to generate unique ID
301 * @obj_type: object type
303 * Create a unique identifier based on @ptr in @dev's identifier space. Used
304 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
305 * modeset identifiers are _not_ reference counted. Hence don't use this for
306 * reference counted modeset objects like framebuffers.
309 * New unique (relative to other objects in @dev) integer identifier for the
312 int drm_mode_object_get(struct drm_device *dev,
313 struct drm_mode_object *obj, uint32_t obj_type)
315 return drm_mode_object_get_reg(dev, obj, obj_type, true);
318 static void drm_mode_object_register(struct drm_device *dev,
319 struct drm_mode_object *obj)
321 mutex_lock(&dev->mode_config.idr_mutex);
322 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
323 mutex_unlock(&dev->mode_config.idr_mutex);
327 * drm_mode_object_put - free a modeset identifer
329 * @object: object to free
331 * Free @id from @dev's unique identifier pool. Note that despite the _get
332 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
333 * for reference counted modeset objects like framebuffers.
335 void drm_mode_object_put(struct drm_device *dev,
336 struct drm_mode_object *object)
338 mutex_lock(&dev->mode_config.idr_mutex);
339 idr_remove(&dev->mode_config.crtc_idr, object->id);
340 mutex_unlock(&dev->mode_config.idr_mutex);
343 static struct drm_mode_object *_object_find(struct drm_device *dev,
344 uint32_t id, uint32_t type)
346 struct drm_mode_object *obj = NULL;
348 mutex_lock(&dev->mode_config.idr_mutex);
349 obj = idr_find(&dev->mode_config.crtc_idr, id);
350 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
352 if (obj && obj->id != id)
354 /* don't leak out unref'd fb's */
356 (obj->type == DRM_MODE_OBJECT_FB ||
357 obj->type == DRM_MODE_OBJECT_BLOB))
359 mutex_unlock(&dev->mode_config.idr_mutex);
365 * drm_mode_object_find - look up a drm object with static lifetime
367 * @id: id of the mode object
368 * @type: type of the mode object
370 * Note that framebuffers cannot be looked up with this functions - since those
371 * are reference counted, they need special treatment. Even with
372 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
373 * rather than WARN_ON()).
375 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
376 uint32_t id, uint32_t type)
378 struct drm_mode_object *obj = NULL;
380 /* Framebuffers are reference counted and need their own lookup
382 WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
383 obj = _object_find(dev, id, type);
386 EXPORT_SYMBOL(drm_mode_object_find);
389 * drm_framebuffer_init - initialize a framebuffer
391 * @fb: framebuffer to be initialized
392 * @funcs: ... with these functions
394 * Allocates an ID for the framebuffer's parent mode object, sets its mode
395 * functions & device file and adds it to the master fd list.
398 * This functions publishes the fb and makes it available for concurrent access
399 * by other users. Which means by this point the fb _must_ be fully set up -
400 * since all the fb attributes are invariant over its lifetime, no further
401 * locking but only correct reference counting is required.
404 * Zero on success, error code on failure.
406 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
407 const struct drm_framebuffer_funcs *funcs)
411 mutex_lock(&dev->mode_config.fb_lock);
412 kref_init(&fb->refcount);
413 INIT_LIST_HEAD(&fb->filp_head);
417 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
421 dev->mode_config.num_fb++;
422 list_add(&fb->head, &dev->mode_config.fb_list);
424 mutex_unlock(&dev->mode_config.fb_lock);
428 EXPORT_SYMBOL(drm_framebuffer_init);
430 /* dev->mode_config.fb_lock must be held! */
431 static void __drm_framebuffer_unregister(struct drm_device *dev,
432 struct drm_framebuffer *fb)
434 mutex_lock(&dev->mode_config.idr_mutex);
435 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
436 mutex_unlock(&dev->mode_config.idr_mutex);
441 static void drm_framebuffer_free(struct kref *kref)
443 struct drm_framebuffer *fb =
444 container_of(kref, struct drm_framebuffer, refcount);
445 struct drm_device *dev = fb->dev;
448 * The lookup idr holds a weak reference, which has not necessarily been
449 * removed at this point. Check for that.
451 mutex_lock(&dev->mode_config.fb_lock);
453 /* Mark fb as reaped and drop idr ref. */
454 __drm_framebuffer_unregister(dev, fb);
456 mutex_unlock(&dev->mode_config.fb_lock);
458 fb->funcs->destroy(fb);
461 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
464 struct drm_mode_object *obj = NULL;
465 struct drm_framebuffer *fb;
467 mutex_lock(&dev->mode_config.idr_mutex);
468 obj = idr_find(&dev->mode_config.crtc_idr, id);
469 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
473 mutex_unlock(&dev->mode_config.idr_mutex);
479 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
481 * @id: id of the fb object
483 * If successful, this grabs an additional reference to the framebuffer -
484 * callers need to make sure to eventually unreference the returned framebuffer
485 * again, using @drm_framebuffer_unreference.
487 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
490 struct drm_framebuffer *fb;
492 mutex_lock(&dev->mode_config.fb_lock);
493 fb = __drm_framebuffer_lookup(dev, id);
495 if (!kref_get_unless_zero(&fb->refcount))
498 mutex_unlock(&dev->mode_config.fb_lock);
502 EXPORT_SYMBOL(drm_framebuffer_lookup);
505 * drm_framebuffer_unreference - unref a framebuffer
506 * @fb: framebuffer to unref
508 * This functions decrements the fb's refcount and frees it if it drops to zero.
510 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
512 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
513 kref_put(&fb->refcount, drm_framebuffer_free);
515 EXPORT_SYMBOL(drm_framebuffer_unreference);
518 * drm_framebuffer_reference - incr the fb refcnt
521 * This functions increments the fb's refcount.
523 void drm_framebuffer_reference(struct drm_framebuffer *fb)
525 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
526 kref_get(&fb->refcount);
528 EXPORT_SYMBOL(drm_framebuffer_reference);
531 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
532 * @fb: fb to unregister
534 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
535 * those used for fbdev. Note that the caller must hold a reference of it's own,
536 * i.e. the object may not be destroyed through this call (since it'll lead to a
537 * locking inversion).
539 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
541 struct drm_device *dev = fb->dev;
543 mutex_lock(&dev->mode_config.fb_lock);
544 /* Mark fb as reaped and drop idr ref. */
545 __drm_framebuffer_unregister(dev, fb);
546 mutex_unlock(&dev->mode_config.fb_lock);
548 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
551 * drm_framebuffer_cleanup - remove a framebuffer object
552 * @fb: framebuffer to remove
554 * Cleanup framebuffer. This function is intended to be used from the drivers
555 * ->destroy callback. It can also be used to clean up driver private
556 * framebuffers embedded into a larger structure.
558 * Note that this function does not remove the fb from active usuage - if it is
559 * still used anywhere, hilarity can ensue since userspace could call getfb on
560 * the id and get back -EINVAL. Obviously no concern at driver unload time.
562 * Also, the framebuffer will not be removed from the lookup idr - for
563 * user-created framebuffers this will happen in in the rmfb ioctl. For
564 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
565 * drm_framebuffer_unregister_private.
567 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
569 struct drm_device *dev = fb->dev;
571 mutex_lock(&dev->mode_config.fb_lock);
573 dev->mode_config.num_fb--;
574 mutex_unlock(&dev->mode_config.fb_lock);
576 EXPORT_SYMBOL(drm_framebuffer_cleanup);
579 * drm_framebuffer_remove - remove and unreference a framebuffer object
580 * @fb: framebuffer to remove
582 * Scans all the CRTCs and planes in @dev's mode_config. If they're
583 * using @fb, removes it, setting it to NULL. Then drops the reference to the
584 * passed-in framebuffer. Might take the modeset locks.
586 * Note that this function optimizes the cleanup away if the caller holds the
587 * last reference to the framebuffer. It is also guaranteed to not take the
588 * modeset locks in this case.
590 void drm_framebuffer_remove(struct drm_framebuffer *fb)
592 struct drm_device *dev = fb->dev;
593 struct drm_crtc *crtc;
594 struct drm_plane *plane;
595 struct drm_mode_set set;
598 WARN_ON(!list_empty(&fb->filp_head));
601 * drm ABI mandates that we remove any deleted framebuffers from active
602 * useage. But since most sane clients only remove framebuffers they no
603 * longer need, try to optimize this away.
605 * Since we're holding a reference ourselves, observing a refcount of 1
606 * means that we're the last holder and can skip it. Also, the refcount
607 * can never increase from 1 again, so we don't need any barriers or
610 * Note that userspace could try to race with use and instate a new
611 * usage _after_ we've cleared all current ones. End result will be an
612 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
615 if (atomic_read(&fb->refcount.refcount) > 1) {
616 drm_modeset_lock_all(dev);
617 /* remove from any CRTC */
618 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
619 if (crtc->primary->fb == fb) {
620 /* should turn off the crtc */
621 memset(&set, 0, sizeof(struct drm_mode_set));
624 ret = drm_mode_set_config_internal(&set);
626 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
630 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
632 drm_plane_force_disable(plane);
634 drm_modeset_unlock_all(dev);
637 drm_framebuffer_unreference(fb);
639 EXPORT_SYMBOL(drm_framebuffer_remove);
641 DEFINE_WW_CLASS(crtc_ww_class);
644 * drm_crtc_init_with_planes - Initialise a new CRTC object with
645 * specified primary and cursor planes.
647 * @crtc: CRTC object to init
648 * @primary: Primary plane for CRTC
649 * @cursor: Cursor plane for CRTC
650 * @funcs: callbacks for the new CRTC
652 * Inits a new object created as base part of a driver crtc object.
655 * Zero on success, error code on failure.
657 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
658 struct drm_plane *primary,
659 struct drm_plane *cursor,
660 const struct drm_crtc_funcs *funcs)
662 struct drm_mode_config *config = &dev->mode_config;
665 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
666 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
670 crtc->invert_dimensions = false;
672 drm_modeset_lock_init(&crtc->mutex);
673 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
677 crtc->base.properties = &crtc->properties;
679 list_add_tail(&crtc->head, &config->crtc_list);
682 crtc->primary = primary;
683 crtc->cursor = cursor;
685 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
687 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
689 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
690 drm_object_attach_property(&crtc->base, config->prop_active, 0);
695 EXPORT_SYMBOL(drm_crtc_init_with_planes);
698 * drm_crtc_cleanup - Clean up the core crtc usage
699 * @crtc: CRTC to cleanup
701 * This function cleans up @crtc and removes it from the DRM mode setting
702 * core. Note that the function does *not* free the crtc structure itself,
703 * this is the responsibility of the caller.
705 void drm_crtc_cleanup(struct drm_crtc *crtc)
707 struct drm_device *dev = crtc->dev;
709 kfree(crtc->gamma_store);
710 crtc->gamma_store = NULL;
712 drm_modeset_lock_fini(&crtc->mutex);
714 drm_mode_object_put(dev, &crtc->base);
715 list_del(&crtc->head);
716 dev->mode_config.num_crtc--;
718 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
719 if (crtc->state && crtc->funcs->atomic_destroy_state)
720 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
722 memset(crtc, 0, sizeof(*crtc));
724 EXPORT_SYMBOL(drm_crtc_cleanup);
727 * drm_crtc_index - find the index of a registered CRTC
728 * @crtc: CRTC to find index for
730 * Given a registered CRTC, return the index of that CRTC within a DRM
731 * device's list of CRTCs.
733 unsigned int drm_crtc_index(struct drm_crtc *crtc)
735 unsigned int index = 0;
736 struct drm_crtc *tmp;
738 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
747 EXPORT_SYMBOL(drm_crtc_index);
750 * drm_mode_remove - remove and free a mode
751 * @connector: connector list to modify
752 * @mode: mode to remove
754 * Remove @mode from @connector's mode list, then free it.
756 static void drm_mode_remove(struct drm_connector *connector,
757 struct drm_display_mode *mode)
759 list_del(&mode->head);
760 drm_mode_destroy(connector->dev, mode);
764 * drm_display_info_set_bus_formats - set the supported bus formats
765 * @info: display info to store bus formats in
766 * @formats: array containing the supported bus formats
767 * @num_formats: the number of entries in the fmts array
769 * Store the supported bus formats in display info structure.
770 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
771 * a full list of available formats.
773 int drm_display_info_set_bus_formats(struct drm_display_info *info,
775 unsigned int num_formats)
779 if (!formats && num_formats)
782 if (formats && num_formats) {
783 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
789 kfree(info->bus_formats);
790 info->bus_formats = fmts;
791 info->num_bus_formats = num_formats;
795 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
798 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
799 * @connector: connector to quwery
801 * The kernel supports per-connector configration of its consoles through
802 * use of the video= parameter. This function parses that option and
803 * extracts the user's specified mode (or enable/disable status) for a
804 * particular connector. This is typically only used during the early fbdev
807 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
809 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
812 if (fb_get_options(connector->name, &option))
815 if (!drm_mode_parse_command_line_for_connector(option,
823 switch (mode->force) {
827 case DRM_FORCE_ON_DIGITAL:
836 DRM_INFO("forcing %s connector %s\n", connector->name, s);
837 connector->force = mode->force;
840 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
842 mode->xres, mode->yres,
843 mode->refresh_specified ? mode->refresh : 60,
844 mode->rb ? " reduced blanking" : "",
845 mode->margins ? " with margins" : "",
846 mode->interlace ? " interlaced" : "");
850 * drm_connector_init - Init a preallocated connector
852 * @connector: the connector to init
853 * @funcs: callbacks for this connector
854 * @connector_type: user visible type of the connector
856 * Initialises a preallocated connector. Connectors should be
857 * subclassed as part of driver connector objects.
860 * Zero on success, error code on failure.
862 int drm_connector_init(struct drm_device *dev,
863 struct drm_connector *connector,
864 const struct drm_connector_funcs *funcs,
867 struct drm_mode_config *config = &dev->mode_config;
869 struct ida *connector_ida =
870 &drm_connector_enum_list[connector_type].ida;
872 drm_modeset_lock_all(dev);
874 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
878 connector->base.properties = &connector->properties;
879 connector->dev = dev;
880 connector->funcs = funcs;
881 connector->connector_type = connector_type;
882 connector->connector_type_id =
883 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
884 if (connector->connector_type_id < 0) {
885 ret = connector->connector_type_id;
889 kasprintf(GFP_KERNEL, "%s-%d",
890 drm_connector_enum_list[connector_type].name,
891 connector->connector_type_id);
892 if (!connector->name) {
897 INIT_LIST_HEAD(&connector->probed_modes);
898 INIT_LIST_HEAD(&connector->modes);
899 connector->edid_blob_ptr = NULL;
900 connector->status = connector_status_unknown;
902 drm_connector_get_cmdline_mode(connector);
904 /* We should add connectors at the end to avoid upsetting the connector
906 list_add_tail(&connector->head, &config->connector_list);
907 config->num_connector++;
909 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
910 drm_object_attach_property(&connector->base,
911 config->edid_property,
914 drm_object_attach_property(&connector->base,
915 config->dpms_property, 0);
917 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
918 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
921 connector->debugfs_entry = NULL;
925 drm_mode_object_put(dev, &connector->base);
928 drm_modeset_unlock_all(dev);
932 EXPORT_SYMBOL(drm_connector_init);
935 * drm_connector_cleanup - cleans up an initialised connector
936 * @connector: connector to cleanup
938 * Cleans up the connector but doesn't free the object.
940 void drm_connector_cleanup(struct drm_connector *connector)
942 struct drm_device *dev = connector->dev;
943 struct drm_display_mode *mode, *t;
945 if (connector->tile_group) {
946 drm_mode_put_tile_group(dev, connector->tile_group);
947 connector->tile_group = NULL;
950 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
951 drm_mode_remove(connector, mode);
953 list_for_each_entry_safe(mode, t, &connector->modes, head)
954 drm_mode_remove(connector, mode);
956 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
957 connector->connector_type_id);
959 kfree(connector->display_info.bus_formats);
960 drm_mode_object_put(dev, &connector->base);
961 kfree(connector->name);
962 connector->name = NULL;
963 list_del(&connector->head);
964 dev->mode_config.num_connector--;
966 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
967 if (connector->state && connector->funcs->atomic_destroy_state)
968 connector->funcs->atomic_destroy_state(connector,
971 memset(connector, 0, sizeof(*connector));
973 EXPORT_SYMBOL(drm_connector_cleanup);
976 * drm_connector_index - find the index of a registered connector
977 * @connector: connector to find index for
979 * Given a registered connector, return the index of that connector within a DRM
980 * device's list of connectors.
982 unsigned int drm_connector_index(struct drm_connector *connector)
984 unsigned int index = 0;
985 struct drm_connector *tmp;
986 struct drm_mode_config *config = &connector->dev->mode_config;
988 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
990 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
991 if (tmp == connector)
999 EXPORT_SYMBOL(drm_connector_index);
1002 * drm_connector_register - register a connector
1003 * @connector: the connector to register
1005 * Register userspace interfaces for a connector
1008 * Zero on success, error code on failure.
1010 int drm_connector_register(struct drm_connector *connector)
1014 drm_mode_object_register(connector->dev, &connector->base);
1016 ret = drm_sysfs_connector_add(connector);
1020 ret = drm_debugfs_connector_add(connector);
1022 drm_sysfs_connector_remove(connector);
1028 EXPORT_SYMBOL(drm_connector_register);
1031 * drm_connector_unregister - unregister a connector
1032 * @connector: the connector to unregister
1034 * Unregister userspace interfaces for a connector
1036 void drm_connector_unregister(struct drm_connector *connector)
1038 drm_sysfs_connector_remove(connector);
1039 drm_debugfs_connector_remove(connector);
1041 EXPORT_SYMBOL(drm_connector_unregister);
1045 * drm_connector_unplug_all - unregister connector userspace interfaces
1048 * This function unregisters all connector userspace interfaces in sysfs. Should
1049 * be call when the device is disconnected, e.g. from an usb driver's
1050 * ->disconnect callback.
1052 void drm_connector_unplug_all(struct drm_device *dev)
1054 struct drm_connector *connector;
1056 /* taking the mode config mutex ends up in a clash with sysfs */
1057 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1058 drm_connector_unregister(connector);
1061 EXPORT_SYMBOL(drm_connector_unplug_all);
1064 * drm_encoder_init - Init a preallocated encoder
1066 * @encoder: the encoder to init
1067 * @funcs: callbacks for this encoder
1068 * @encoder_type: user visible type of the encoder
1070 * Initialises a preallocated encoder. Encoder should be
1071 * subclassed as part of driver encoder objects.
1074 * Zero on success, error code on failure.
1076 int drm_encoder_init(struct drm_device *dev,
1077 struct drm_encoder *encoder,
1078 const struct drm_encoder_funcs *funcs,
1083 drm_modeset_lock_all(dev);
1085 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1090 encoder->encoder_type = encoder_type;
1091 encoder->funcs = funcs;
1092 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1093 drm_encoder_enum_list[encoder_type].name,
1095 if (!encoder->name) {
1100 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1101 dev->mode_config.num_encoder++;
1105 drm_mode_object_put(dev, &encoder->base);
1108 drm_modeset_unlock_all(dev);
1112 EXPORT_SYMBOL(drm_encoder_init);
1115 * drm_encoder_cleanup - cleans up an initialised encoder
1116 * @encoder: encoder to cleanup
1118 * Cleans up the encoder but doesn't free the object.
1120 void drm_encoder_cleanup(struct drm_encoder *encoder)
1122 struct drm_device *dev = encoder->dev;
1124 drm_modeset_lock_all(dev);
1125 drm_mode_object_put(dev, &encoder->base);
1126 kfree(encoder->name);
1127 list_del(&encoder->head);
1128 dev->mode_config.num_encoder--;
1129 drm_modeset_unlock_all(dev);
1131 memset(encoder, 0, sizeof(*encoder));
1133 EXPORT_SYMBOL(drm_encoder_cleanup);
1136 * drm_universal_plane_init - Initialize a new universal plane object
1138 * @plane: plane object to init
1139 * @possible_crtcs: bitmask of possible CRTCs
1140 * @funcs: callbacks for the new plane
1141 * @formats: array of supported formats (%DRM_FORMAT_*)
1142 * @format_count: number of elements in @formats
1143 * @type: type of plane (overlay, primary, cursor)
1145 * Initializes a plane object of type @type.
1148 * Zero on success, error code on failure.
1150 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1151 unsigned long possible_crtcs,
1152 const struct drm_plane_funcs *funcs,
1153 const uint32_t *formats, uint32_t format_count,
1154 enum drm_plane_type type)
1156 struct drm_mode_config *config = &dev->mode_config;
1159 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1163 drm_modeset_lock_init(&plane->mutex);
1165 plane->base.properties = &plane->properties;
1167 plane->funcs = funcs;
1168 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1170 if (!plane->format_types) {
1171 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1172 drm_mode_object_put(dev, &plane->base);
1176 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1177 plane->format_count = format_count;
1178 plane->possible_crtcs = possible_crtcs;
1181 list_add_tail(&plane->head, &config->plane_list);
1182 config->num_total_plane++;
1183 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1184 config->num_overlay_plane++;
1186 drm_object_attach_property(&plane->base,
1187 config->plane_type_property,
1190 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1191 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1192 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1193 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1194 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1195 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1196 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1197 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1198 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1199 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1200 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1205 EXPORT_SYMBOL(drm_universal_plane_init);
1208 * drm_plane_init - Initialize a legacy plane
1210 * @plane: plane object to init
1211 * @possible_crtcs: bitmask of possible CRTCs
1212 * @funcs: callbacks for the new plane
1213 * @formats: array of supported formats (%DRM_FORMAT_*)
1214 * @format_count: number of elements in @formats
1215 * @is_primary: plane type (primary vs overlay)
1217 * Legacy API to initialize a DRM plane.
1219 * New drivers should call drm_universal_plane_init() instead.
1222 * Zero on success, error code on failure.
1224 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1225 unsigned long possible_crtcs,
1226 const struct drm_plane_funcs *funcs,
1227 const uint32_t *formats, uint32_t format_count,
1230 enum drm_plane_type type;
1232 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1233 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1234 formats, format_count, type);
1236 EXPORT_SYMBOL(drm_plane_init);
1239 * drm_plane_cleanup - Clean up the core plane usage
1240 * @plane: plane to cleanup
1242 * This function cleans up @plane and removes it from the DRM mode setting
1243 * core. Note that the function does *not* free the plane structure itself,
1244 * this is the responsibility of the caller.
1246 void drm_plane_cleanup(struct drm_plane *plane)
1248 struct drm_device *dev = plane->dev;
1250 drm_modeset_lock_all(dev);
1251 kfree(plane->format_types);
1252 drm_mode_object_put(dev, &plane->base);
1254 BUG_ON(list_empty(&plane->head));
1256 list_del(&plane->head);
1257 dev->mode_config.num_total_plane--;
1258 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1259 dev->mode_config.num_overlay_plane--;
1260 drm_modeset_unlock_all(dev);
1262 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1263 if (plane->state && plane->funcs->atomic_destroy_state)
1264 plane->funcs->atomic_destroy_state(plane, plane->state);
1266 memset(plane, 0, sizeof(*plane));
1268 EXPORT_SYMBOL(drm_plane_cleanup);
1271 * drm_plane_index - find the index of a registered plane
1272 * @plane: plane to find index for
1274 * Given a registered plane, return the index of that CRTC within a DRM
1275 * device's list of planes.
1277 unsigned int drm_plane_index(struct drm_plane *plane)
1279 unsigned int index = 0;
1280 struct drm_plane *tmp;
1282 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1291 EXPORT_SYMBOL(drm_plane_index);
1294 * drm_plane_force_disable - Forcibly disable a plane
1295 * @plane: plane to disable
1297 * Forces the plane to be disabled.
1299 * Used when the plane's current framebuffer is destroyed,
1300 * and when restoring fbdev mode.
1302 void drm_plane_force_disable(struct drm_plane *plane)
1309 plane->old_fb = plane->fb;
1310 ret = plane->funcs->disable_plane(plane);
1312 DRM_ERROR("failed to disable plane with busy fb\n");
1313 plane->old_fb = NULL;
1316 /* disconnect the plane from the fb and crtc: */
1317 drm_framebuffer_unreference(plane->old_fb);
1318 plane->old_fb = NULL;
1322 EXPORT_SYMBOL(drm_plane_force_disable);
1324 static int drm_mode_create_standard_properties(struct drm_device *dev)
1326 struct drm_property *prop;
1329 * Standard properties (apply to all connectors)
1331 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1332 DRM_MODE_PROP_IMMUTABLE,
1336 dev->mode_config.edid_property = prop;
1338 prop = drm_property_create_enum(dev, 0,
1339 "DPMS", drm_dpms_enum_list,
1340 ARRAY_SIZE(drm_dpms_enum_list));
1343 dev->mode_config.dpms_property = prop;
1345 prop = drm_property_create(dev,
1346 DRM_MODE_PROP_BLOB |
1347 DRM_MODE_PROP_IMMUTABLE,
1351 dev->mode_config.path_property = prop;
1353 prop = drm_property_create(dev,
1354 DRM_MODE_PROP_BLOB |
1355 DRM_MODE_PROP_IMMUTABLE,
1359 dev->mode_config.tile_property = prop;
1361 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1362 "type", drm_plane_type_enum_list,
1363 ARRAY_SIZE(drm_plane_type_enum_list));
1366 dev->mode_config.plane_type_property = prop;
1368 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1369 "SRC_X", 0, UINT_MAX);
1372 dev->mode_config.prop_src_x = prop;
1374 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1375 "SRC_Y", 0, UINT_MAX);
1378 dev->mode_config.prop_src_y = prop;
1380 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1381 "SRC_W", 0, UINT_MAX);
1384 dev->mode_config.prop_src_w = prop;
1386 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1387 "SRC_H", 0, UINT_MAX);
1390 dev->mode_config.prop_src_h = prop;
1392 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1393 "CRTC_X", INT_MIN, INT_MAX);
1396 dev->mode_config.prop_crtc_x = prop;
1398 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1399 "CRTC_Y", INT_MIN, INT_MAX);
1402 dev->mode_config.prop_crtc_y = prop;
1404 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1405 "CRTC_W", 0, INT_MAX);
1408 dev->mode_config.prop_crtc_w = prop;
1410 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1411 "CRTC_H", 0, INT_MAX);
1414 dev->mode_config.prop_crtc_h = prop;
1416 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1417 "FB_ID", DRM_MODE_OBJECT_FB);
1420 dev->mode_config.prop_fb_id = prop;
1422 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1423 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1426 dev->mode_config.prop_crtc_id = prop;
1428 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1432 dev->mode_config.prop_active = prop;
1438 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1441 * Called by a driver the first time a DVI-I connector is made.
1443 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1445 struct drm_property *dvi_i_selector;
1446 struct drm_property *dvi_i_subconnector;
1448 if (dev->mode_config.dvi_i_select_subconnector_property)
1452 drm_property_create_enum(dev, 0,
1453 "select subconnector",
1454 drm_dvi_i_select_enum_list,
1455 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1456 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1458 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1460 drm_dvi_i_subconnector_enum_list,
1461 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1462 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1466 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1469 * drm_create_tv_properties - create TV specific connector properties
1471 * @num_modes: number of different TV formats (modes) supported
1472 * @modes: array of pointers to strings containing name of each format
1474 * Called by a driver's TV initialization routine, this function creates
1475 * the TV specific connector properties for a given device. Caller is
1476 * responsible for allocating a list of format names and passing them to
1479 int drm_mode_create_tv_properties(struct drm_device *dev,
1480 unsigned int num_modes,
1483 struct drm_property *tv_selector;
1484 struct drm_property *tv_subconnector;
1487 if (dev->mode_config.tv_select_subconnector_property)
1491 * Basic connector properties
1493 tv_selector = drm_property_create_enum(dev, 0,
1494 "select subconnector",
1495 drm_tv_select_enum_list,
1496 ARRAY_SIZE(drm_tv_select_enum_list));
1497 dev->mode_config.tv_select_subconnector_property = tv_selector;
1500 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1502 drm_tv_subconnector_enum_list,
1503 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1504 dev->mode_config.tv_subconnector_property = tv_subconnector;
1507 * Other, TV specific properties: margins & TV modes.
1509 dev->mode_config.tv_left_margin_property =
1510 drm_property_create_range(dev, 0, "left margin", 0, 100);
1512 dev->mode_config.tv_right_margin_property =
1513 drm_property_create_range(dev, 0, "right margin", 0, 100);
1515 dev->mode_config.tv_top_margin_property =
1516 drm_property_create_range(dev, 0, "top margin", 0, 100);
1518 dev->mode_config.tv_bottom_margin_property =
1519 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1521 dev->mode_config.tv_mode_property =
1522 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1524 for (i = 0; i < num_modes; i++)
1525 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1528 dev->mode_config.tv_brightness_property =
1529 drm_property_create_range(dev, 0, "brightness", 0, 100);
1531 dev->mode_config.tv_contrast_property =
1532 drm_property_create_range(dev, 0, "contrast", 0, 100);
1534 dev->mode_config.tv_flicker_reduction_property =
1535 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1537 dev->mode_config.tv_overscan_property =
1538 drm_property_create_range(dev, 0, "overscan", 0, 100);
1540 dev->mode_config.tv_saturation_property =
1541 drm_property_create_range(dev, 0, "saturation", 0, 100);
1543 dev->mode_config.tv_hue_property =
1544 drm_property_create_range(dev, 0, "hue", 0, 100);
1548 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1551 * drm_mode_create_scaling_mode_property - create scaling mode property
1554 * Called by a driver the first time it's needed, must be attached to desired
1557 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1559 struct drm_property *scaling_mode;
1561 if (dev->mode_config.scaling_mode_property)
1565 drm_property_create_enum(dev, 0, "scaling mode",
1566 drm_scaling_mode_enum_list,
1567 ARRAY_SIZE(drm_scaling_mode_enum_list));
1569 dev->mode_config.scaling_mode_property = scaling_mode;
1573 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1576 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1579 * Called by a driver the first time it's needed, must be attached to desired
1583 * Zero on success, negative errno on failure.
1585 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1587 if (dev->mode_config.aspect_ratio_property)
1590 dev->mode_config.aspect_ratio_property =
1591 drm_property_create_enum(dev, 0, "aspect ratio",
1592 drm_aspect_ratio_enum_list,
1593 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1595 if (dev->mode_config.aspect_ratio_property == NULL)
1600 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1603 * drm_mode_create_dirty_property - create dirty property
1606 * Called by a driver the first time it's needed, must be attached to desired
1609 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1611 struct drm_property *dirty_info;
1613 if (dev->mode_config.dirty_info_property)
1617 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1619 drm_dirty_info_enum_list,
1620 ARRAY_SIZE(drm_dirty_info_enum_list));
1621 dev->mode_config.dirty_info_property = dirty_info;
1625 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1628 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1631 * Create the the suggested x/y offset property for connectors.
1633 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1635 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1638 dev->mode_config.suggested_x_property =
1639 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1641 dev->mode_config.suggested_y_property =
1642 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1644 if (dev->mode_config.suggested_x_property == NULL ||
1645 dev->mode_config.suggested_y_property == NULL)
1649 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1651 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1653 uint32_t total_objects = 0;
1655 total_objects += dev->mode_config.num_crtc;
1656 total_objects += dev->mode_config.num_connector;
1657 total_objects += dev->mode_config.num_encoder;
1659 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
1660 if (!group->id_list)
1663 group->num_crtcs = 0;
1664 group->num_connectors = 0;
1665 group->num_encoders = 0;
1669 void drm_mode_group_destroy(struct drm_mode_group *group)
1671 kfree(group->id_list);
1672 group->id_list = NULL;
1676 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1677 * the drm core's responsibility to set up mode control groups.
1679 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1680 struct drm_mode_group *group)
1682 struct drm_crtc *crtc;
1683 struct drm_encoder *encoder;
1684 struct drm_connector *connector;
1687 ret = drm_mode_group_init(dev, group);
1691 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1692 group->id_list[group->num_crtcs++] = crtc->base.id;
1694 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1695 group->id_list[group->num_crtcs + group->num_encoders++] =
1698 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1699 group->id_list[group->num_crtcs + group->num_encoders +
1700 group->num_connectors++] = connector->base.id;
1704 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1706 void drm_reinit_primary_mode_group(struct drm_device *dev)
1708 drm_modeset_lock_all(dev);
1709 drm_mode_group_destroy(&dev->primary->mode_group);
1710 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1711 drm_modeset_unlock_all(dev);
1713 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1716 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1717 * @out: drm_mode_modeinfo struct to return to the user
1718 * @in: drm_display_mode to use
1720 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1723 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1724 const struct drm_display_mode *in)
1726 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1727 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1728 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1729 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1730 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1731 "timing values too large for mode info\n");
1733 out->clock = in->clock;
1734 out->hdisplay = in->hdisplay;
1735 out->hsync_start = in->hsync_start;
1736 out->hsync_end = in->hsync_end;
1737 out->htotal = in->htotal;
1738 out->hskew = in->hskew;
1739 out->vdisplay = in->vdisplay;
1740 out->vsync_start = in->vsync_start;
1741 out->vsync_end = in->vsync_end;
1742 out->vtotal = in->vtotal;
1743 out->vscan = in->vscan;
1744 out->vrefresh = in->vrefresh;
1745 out->flags = in->flags;
1746 out->type = in->type;
1747 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1748 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1752 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1753 * @out: drm_display_mode to return to the user
1754 * @in: drm_mode_modeinfo to use
1756 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1760 * Zero on success, negative errno on failure.
1762 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1763 const struct drm_mode_modeinfo *in)
1765 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1768 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1771 out->clock = in->clock;
1772 out->hdisplay = in->hdisplay;
1773 out->hsync_start = in->hsync_start;
1774 out->hsync_end = in->hsync_end;
1775 out->htotal = in->htotal;
1776 out->hskew = in->hskew;
1777 out->vdisplay = in->vdisplay;
1778 out->vsync_start = in->vsync_start;
1779 out->vsync_end = in->vsync_end;
1780 out->vtotal = in->vtotal;
1781 out->vscan = in->vscan;
1782 out->vrefresh = in->vrefresh;
1783 out->flags = in->flags;
1784 out->type = in->type;
1785 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1786 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1792 * drm_mode_getresources - get graphics configuration
1793 * @dev: drm device for the ioctl
1794 * @data: data pointer for the ioctl
1795 * @file_priv: drm file for the ioctl call
1797 * Construct a set of configuration description structures and return
1798 * them to the user, including CRTC, connector and framebuffer configuration.
1800 * Called by the user via ioctl.
1803 * Zero on success, negative errno on failure.
1805 int drm_mode_getresources(struct drm_device *dev, void *data,
1806 struct drm_file *file_priv)
1808 struct drm_mode_card_res *card_res = data;
1809 struct list_head *lh;
1810 struct drm_framebuffer *fb;
1811 struct drm_connector *connector;
1812 struct drm_crtc *crtc;
1813 struct drm_encoder *encoder;
1815 int connector_count = 0;
1818 int encoder_count = 0;
1820 uint32_t __user *fb_id;
1821 uint32_t __user *crtc_id;
1822 uint32_t __user *connector_id;
1823 uint32_t __user *encoder_id;
1824 struct drm_mode_group *mode_group;
1826 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1830 mutex_lock(&file_priv->fbs_lock);
1832 * For the non-control nodes we need to limit the list of resources
1833 * by IDs in the group list for this node
1835 list_for_each(lh, &file_priv->fbs)
1838 /* handle this in 4 parts */
1840 if (card_res->count_fbs >= fb_count) {
1842 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1843 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1844 if (put_user(fb->base.id, fb_id + copied)) {
1845 mutex_unlock(&file_priv->fbs_lock);
1851 card_res->count_fbs = fb_count;
1852 mutex_unlock(&file_priv->fbs_lock);
1854 /* mode_config.mutex protects the connector list against e.g. DP MST
1855 * connector hot-adding. CRTC/Plane lists are invariant. */
1856 mutex_lock(&dev->mode_config.mutex);
1857 if (!drm_is_primary_client(file_priv)) {
1860 list_for_each(lh, &dev->mode_config.crtc_list)
1863 list_for_each(lh, &dev->mode_config.connector_list)
1866 list_for_each(lh, &dev->mode_config.encoder_list)
1870 mode_group = &file_priv->master->minor->mode_group;
1871 crtc_count = mode_group->num_crtcs;
1872 connector_count = mode_group->num_connectors;
1873 encoder_count = mode_group->num_encoders;
1876 card_res->max_height = dev->mode_config.max_height;
1877 card_res->min_height = dev->mode_config.min_height;
1878 card_res->max_width = dev->mode_config.max_width;
1879 card_res->min_width = dev->mode_config.min_width;
1882 if (card_res->count_crtcs >= crtc_count) {
1884 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1886 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1888 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1889 if (put_user(crtc->base.id, crtc_id + copied)) {
1896 for (i = 0; i < mode_group->num_crtcs; i++) {
1897 if (put_user(mode_group->id_list[i],
1898 crtc_id + copied)) {
1906 card_res->count_crtcs = crtc_count;
1909 if (card_res->count_encoders >= encoder_count) {
1911 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1913 list_for_each_entry(encoder,
1914 &dev->mode_config.encoder_list,
1916 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1918 if (put_user(encoder->base.id, encoder_id +
1926 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1927 if (put_user(mode_group->id_list[i],
1928 encoder_id + copied)) {
1937 card_res->count_encoders = encoder_count;
1940 if (card_res->count_connectors >= connector_count) {
1942 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1944 list_for_each_entry(connector,
1945 &dev->mode_config.connector_list,
1947 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1950 if (put_user(connector->base.id,
1951 connector_id + copied)) {
1958 int start = mode_group->num_crtcs +
1959 mode_group->num_encoders;
1960 for (i = start; i < start + mode_group->num_connectors; i++) {
1961 if (put_user(mode_group->id_list[i],
1962 connector_id + copied)) {
1970 card_res->count_connectors = connector_count;
1972 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1973 card_res->count_connectors, card_res->count_encoders);
1976 mutex_unlock(&dev->mode_config.mutex);
1981 * drm_mode_getcrtc - get CRTC configuration
1982 * @dev: drm device for the ioctl
1983 * @data: data pointer for the ioctl
1984 * @file_priv: drm file for the ioctl call
1986 * Construct a CRTC configuration structure to return to the user.
1988 * Called by the user via ioctl.
1991 * Zero on success, negative errno on failure.
1993 int drm_mode_getcrtc(struct drm_device *dev,
1994 void *data, struct drm_file *file_priv)
1996 struct drm_mode_crtc *crtc_resp = data;
1997 struct drm_crtc *crtc;
1999 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2002 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
2006 drm_modeset_lock_crtc(crtc, crtc->primary);
2007 crtc_resp->gamma_size = crtc->gamma_size;
2008 if (crtc->primary->fb)
2009 crtc_resp->fb_id = crtc->primary->fb->base.id;
2011 crtc_resp->fb_id = 0;
2014 crtc_resp->x = crtc->primary->state->src_x >> 16;
2015 crtc_resp->y = crtc->primary->state->src_y >> 16;
2016 if (crtc->state->enable) {
2017 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
2018 crtc_resp->mode_valid = 1;
2021 crtc_resp->mode_valid = 0;
2024 crtc_resp->x = crtc->x;
2025 crtc_resp->y = crtc->y;
2026 if (crtc->enabled) {
2027 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
2028 crtc_resp->mode_valid = 1;
2031 crtc_resp->mode_valid = 0;
2034 drm_modeset_unlock_crtc(crtc);
2039 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2040 const struct drm_file *file_priv)
2043 * If user-space hasn't configured the driver to expose the stereo 3D
2044 * modes, don't expose them.
2046 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2052 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2054 /* For atomic drivers only state objects are synchronously updated and
2055 * protected by modeset locks, so check those first. */
2056 if (connector->state)
2057 return connector->state->best_encoder;
2058 return connector->encoder;
2061 /* helper for getconnector and getproperties ioctls */
2062 static int get_properties(struct drm_mode_object *obj, bool atomic,
2063 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2064 uint32_t *arg_count_props)
2069 props_count = obj->properties->count;
2071 props_count -= obj->properties->atomic_count;
2073 if ((*arg_count_props >= props_count) && props_count) {
2074 for (i = 0, copied = 0; copied < props_count; i++) {
2075 struct drm_property *prop = obj->properties->properties[i];
2078 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2081 ret = drm_object_property_get_value(obj, prop, &val);
2085 if (put_user(prop->base.id, prop_ptr + copied))
2088 if (put_user(val, prop_values + copied))
2094 *arg_count_props = props_count;
2100 * drm_mode_getconnector - get connector configuration
2101 * @dev: drm device for the ioctl
2102 * @data: data pointer for the ioctl
2103 * @file_priv: drm file for the ioctl call
2105 * Construct a connector configuration structure to return to the user.
2107 * Called by the user via ioctl.
2110 * Zero on success, negative errno on failure.
2112 int drm_mode_getconnector(struct drm_device *dev, void *data,
2113 struct drm_file *file_priv)
2115 struct drm_mode_get_connector *out_resp = data;
2116 struct drm_connector *connector;
2117 struct drm_encoder *encoder;
2118 struct drm_display_mode *mode;
2120 int encoders_count = 0;
2124 struct drm_mode_modeinfo u_mode;
2125 struct drm_mode_modeinfo __user *mode_ptr;
2126 uint32_t __user *encoder_ptr;
2128 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2131 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2133 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2135 mutex_lock(&dev->mode_config.mutex);
2137 connector = drm_connector_find(dev, out_resp->connector_id);
2143 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2144 if (connector->encoder_ids[i] != 0)
2147 if (out_resp->count_modes == 0) {
2148 connector->funcs->fill_modes(connector,
2149 dev->mode_config.max_width,
2150 dev->mode_config.max_height);
2153 /* delayed so we get modes regardless of pre-fill_modes state */
2154 list_for_each_entry(mode, &connector->modes, head)
2155 if (drm_mode_expose_to_userspace(mode, file_priv))
2158 out_resp->connector_id = connector->base.id;
2159 out_resp->connector_type = connector->connector_type;
2160 out_resp->connector_type_id = connector->connector_type_id;
2161 out_resp->mm_width = connector->display_info.width_mm;
2162 out_resp->mm_height = connector->display_info.height_mm;
2163 out_resp->subpixel = connector->display_info.subpixel_order;
2164 out_resp->connection = connector->status;
2166 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2167 encoder = drm_connector_get_encoder(connector);
2169 out_resp->encoder_id = encoder->base.id;
2171 out_resp->encoder_id = 0;
2174 * This ioctl is called twice, once to determine how much space is
2175 * needed, and the 2nd time to fill it.
2177 if ((out_resp->count_modes >= mode_count) && mode_count) {
2179 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2180 list_for_each_entry(mode, &connector->modes, head) {
2181 if (!drm_mode_expose_to_userspace(mode, file_priv))
2184 drm_crtc_convert_to_umode(&u_mode, mode);
2185 if (copy_to_user(mode_ptr + copied,
2186 &u_mode, sizeof(u_mode))) {
2193 out_resp->count_modes = mode_count;
2195 ret = get_properties(&connector->base, file_priv->atomic,
2196 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2197 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2198 &out_resp->count_props);
2202 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2204 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2205 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2206 if (connector->encoder_ids[i] != 0) {
2207 if (put_user(connector->encoder_ids[i],
2208 encoder_ptr + copied)) {
2216 out_resp->count_encoders = encoders_count;
2219 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2222 mutex_unlock(&dev->mode_config.mutex);
2227 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2229 struct drm_connector *connector;
2230 struct drm_device *dev = encoder->dev;
2231 bool uses_atomic = false;
2233 /* For atomic drivers only state objects are synchronously updated and
2234 * protected by modeset locks, so check those first. */
2235 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2236 if (!connector->state)
2241 if (connector->state->best_encoder != encoder)
2244 return connector->state->crtc;
2247 /* Don't return stale data (e.g. pending async disable). */
2251 return encoder->crtc;
2255 * drm_mode_getencoder - get encoder configuration
2256 * @dev: drm device for the ioctl
2257 * @data: data pointer for the ioctl
2258 * @file_priv: drm file for the ioctl call
2260 * Construct a encoder configuration structure to return to the user.
2262 * Called by the user via ioctl.
2265 * Zero on success, negative errno on failure.
2267 int drm_mode_getencoder(struct drm_device *dev, void *data,
2268 struct drm_file *file_priv)
2270 struct drm_mode_get_encoder *enc_resp = data;
2271 struct drm_encoder *encoder;
2272 struct drm_crtc *crtc;
2274 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2277 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2281 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2282 crtc = drm_encoder_get_crtc(encoder);
2284 enc_resp->crtc_id = crtc->base.id;
2286 enc_resp->crtc_id = 0;
2287 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2289 enc_resp->encoder_type = encoder->encoder_type;
2290 enc_resp->encoder_id = encoder->base.id;
2291 enc_resp->possible_crtcs = encoder->possible_crtcs;
2292 enc_resp->possible_clones = encoder->possible_clones;
2298 * drm_mode_getplane_res - enumerate all plane resources
2301 * @file_priv: DRM file info
2303 * Construct a list of plane ids to return to the user.
2305 * Called by the user via ioctl.
2308 * Zero on success, negative errno on failure.
2310 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2311 struct drm_file *file_priv)
2313 struct drm_mode_get_plane_res *plane_resp = data;
2314 struct drm_mode_config *config;
2315 struct drm_plane *plane;
2316 uint32_t __user *plane_ptr;
2318 unsigned num_planes;
2320 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2323 config = &dev->mode_config;
2325 if (file_priv->universal_planes)
2326 num_planes = config->num_total_plane;
2328 num_planes = config->num_overlay_plane;
2331 * This ioctl is called twice, once to determine how much space is
2332 * needed, and the 2nd time to fill it.
2335 (plane_resp->count_planes >= num_planes)) {
2336 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2338 /* Plane lists are invariant, no locking needed. */
2339 list_for_each_entry(plane, &config->plane_list, head) {
2341 * Unless userspace set the 'universal planes'
2342 * capability bit, only advertise overlays.
2344 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2345 !file_priv->universal_planes)
2348 if (put_user(plane->base.id, plane_ptr + copied))
2353 plane_resp->count_planes = num_planes;
2359 * drm_mode_getplane - get plane configuration
2362 * @file_priv: DRM file info
2364 * Construct a plane configuration structure to return to the user.
2366 * Called by the user via ioctl.
2369 * Zero on success, negative errno on failure.
2371 int drm_mode_getplane(struct drm_device *dev, void *data,
2372 struct drm_file *file_priv)
2374 struct drm_mode_get_plane *plane_resp = data;
2375 struct drm_plane *plane;
2376 uint32_t __user *format_ptr;
2378 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2381 plane = drm_plane_find(dev, plane_resp->plane_id);
2385 drm_modeset_lock(&plane->mutex, NULL);
2387 plane_resp->crtc_id = plane->crtc->base.id;
2389 plane_resp->crtc_id = 0;
2392 plane_resp->fb_id = plane->fb->base.id;
2394 plane_resp->fb_id = 0;
2395 drm_modeset_unlock(&plane->mutex);
2397 plane_resp->plane_id = plane->base.id;
2398 plane_resp->possible_crtcs = plane->possible_crtcs;
2399 plane_resp->gamma_size = 0;
2402 * This ioctl is called twice, once to determine how much space is
2403 * needed, and the 2nd time to fill it.
2405 if (plane->format_count &&
2406 (plane_resp->count_format_types >= plane->format_count)) {
2407 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2408 if (copy_to_user(format_ptr,
2409 plane->format_types,
2410 sizeof(uint32_t) * plane->format_count)) {
2414 plane_resp->count_format_types = plane->format_count;
2420 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2421 * @plane: plane to check for format support
2422 * @format: the pixel format
2425 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2428 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2432 for (i = 0; i < plane->format_count; i++) {
2433 if (format == plane->format_types[i])
2441 * setplane_internal - setplane handler for internal callers
2443 * Note that we assume an extra reference has already been taken on fb. If the
2444 * update fails, this reference will be dropped before return; if it succeeds,
2445 * the previous framebuffer (if any) will be unreferenced instead.
2447 * src_{x,y,w,h} are provided in 16.16 fixed point format
2449 static int __setplane_internal(struct drm_plane *plane,
2450 struct drm_crtc *crtc,
2451 struct drm_framebuffer *fb,
2452 int32_t crtc_x, int32_t crtc_y,
2453 uint32_t crtc_w, uint32_t crtc_h,
2454 /* src_{x,y,w,h} values are 16.16 fixed point */
2455 uint32_t src_x, uint32_t src_y,
2456 uint32_t src_w, uint32_t src_h)
2459 unsigned int fb_width, fb_height;
2461 /* No fb means shut it down */
2463 plane->old_fb = plane->fb;
2464 ret = plane->funcs->disable_plane(plane);
2469 plane->old_fb = NULL;
2474 /* Check whether this plane is usable on this CRTC */
2475 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2476 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2481 /* Check whether this plane supports the fb pixel format. */
2482 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2484 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2485 drm_get_format_name(fb->pixel_format));
2489 /* Give drivers some help against integer overflows */
2490 if (crtc_w > INT_MAX ||
2491 crtc_x > INT_MAX - (int32_t) crtc_w ||
2493 crtc_y > INT_MAX - (int32_t) crtc_h) {
2494 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2495 crtc_w, crtc_h, crtc_x, crtc_y);
2500 fb_width = fb->width << 16;
2501 fb_height = fb->height << 16;
2503 /* Make sure source coordinates are inside the fb. */
2504 if (src_w > fb_width ||
2505 src_x > fb_width - src_w ||
2506 src_h > fb_height ||
2507 src_y > fb_height - src_h) {
2508 DRM_DEBUG_KMS("Invalid source coordinates "
2509 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2510 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2511 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2512 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2513 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2518 plane->old_fb = plane->fb;
2519 ret = plane->funcs->update_plane(plane, crtc, fb,
2520 crtc_x, crtc_y, crtc_w, crtc_h,
2521 src_x, src_y, src_w, src_h);
2527 plane->old_fb = NULL;
2532 drm_framebuffer_unreference(fb);
2534 drm_framebuffer_unreference(plane->old_fb);
2535 plane->old_fb = NULL;
2540 static int setplane_internal(struct drm_plane *plane,
2541 struct drm_crtc *crtc,
2542 struct drm_framebuffer *fb,
2543 int32_t crtc_x, int32_t crtc_y,
2544 uint32_t crtc_w, uint32_t crtc_h,
2545 /* src_{x,y,w,h} values are 16.16 fixed point */
2546 uint32_t src_x, uint32_t src_y,
2547 uint32_t src_w, uint32_t src_h)
2551 drm_modeset_lock_all(plane->dev);
2552 ret = __setplane_internal(plane, crtc, fb,
2553 crtc_x, crtc_y, crtc_w, crtc_h,
2554 src_x, src_y, src_w, src_h);
2555 drm_modeset_unlock_all(plane->dev);
2561 * drm_mode_setplane - configure a plane's configuration
2563 * @data: ioctl data*
2564 * @file_priv: DRM file info
2566 * Set plane configuration, including placement, fb, scaling, and other factors.
2567 * Or pass a NULL fb to disable (planes may be disabled without providing a
2571 * Zero on success, negative errno on failure.
2573 int drm_mode_setplane(struct drm_device *dev, void *data,
2574 struct drm_file *file_priv)
2576 struct drm_mode_set_plane *plane_req = data;
2577 struct drm_plane *plane;
2578 struct drm_crtc *crtc = NULL;
2579 struct drm_framebuffer *fb = NULL;
2581 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2585 * First, find the plane, crtc, and fb objects. If not available,
2586 * we don't bother to call the driver.
2588 plane = drm_plane_find(dev, plane_req->plane_id);
2590 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2591 plane_req->plane_id);
2595 if (plane_req->fb_id) {
2596 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2598 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2603 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2605 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2606 plane_req->crtc_id);
2612 * setplane_internal will take care of deref'ing either the old or new
2613 * framebuffer depending on success.
2615 return setplane_internal(plane, crtc, fb,
2616 plane_req->crtc_x, plane_req->crtc_y,
2617 plane_req->crtc_w, plane_req->crtc_h,
2618 plane_req->src_x, plane_req->src_y,
2619 plane_req->src_w, plane_req->src_h);
2623 * drm_mode_set_config_internal - helper to call ->set_config
2624 * @set: modeset config to set
2626 * This is a little helper to wrap internal calls to the ->set_config driver
2627 * interface. The only thing it adds is correct refcounting dance.
2630 * Zero on success, negative errno on failure.
2632 int drm_mode_set_config_internal(struct drm_mode_set *set)
2634 struct drm_crtc *crtc = set->crtc;
2635 struct drm_framebuffer *fb;
2636 struct drm_crtc *tmp;
2640 * NOTE: ->set_config can also disable other crtcs (if we steal all
2641 * connectors from it), hence we need to refcount the fbs across all
2642 * crtcs. Atomic modeset will have saner semantics ...
2644 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2645 tmp->primary->old_fb = tmp->primary->fb;
2649 ret = crtc->funcs->set_config(set);
2651 crtc->primary->crtc = crtc;
2652 crtc->primary->fb = fb;
2655 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2656 if (tmp->primary->fb)
2657 drm_framebuffer_reference(tmp->primary->fb);
2658 if (tmp->primary->old_fb)
2659 drm_framebuffer_unreference(tmp->primary->old_fb);
2660 tmp->primary->old_fb = NULL;
2665 EXPORT_SYMBOL(drm_mode_set_config_internal);
2668 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2669 * @mode: mode to query
2670 * @hdisplay: hdisplay value to fill in
2671 * @vdisplay: vdisplay value to fill in
2673 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2674 * the appropriate layout.
2676 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2677 int *hdisplay, int *vdisplay)
2679 struct drm_display_mode adjusted;
2681 drm_mode_copy(&adjusted, mode);
2682 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2683 *hdisplay = adjusted.crtc_hdisplay;
2684 *vdisplay = adjusted.crtc_vdisplay;
2686 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2689 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2691 * @crtc: CRTC that framebuffer will be displayed on
2694 * @mode: mode that framebuffer will be displayed under
2695 * @fb: framebuffer to check size of
2697 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2699 const struct drm_display_mode *mode,
2700 const struct drm_framebuffer *fb)
2703 int hdisplay, vdisplay;
2705 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2707 if (crtc->invert_dimensions)
2708 swap(hdisplay, vdisplay);
2710 if (hdisplay > fb->width ||
2711 vdisplay > fb->height ||
2712 x > fb->width - hdisplay ||
2713 y > fb->height - vdisplay) {
2714 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2715 fb->width, fb->height, hdisplay, vdisplay, x, y,
2716 crtc->invert_dimensions ? " (inverted)" : "");
2722 EXPORT_SYMBOL(drm_crtc_check_viewport);
2725 * drm_mode_setcrtc - set CRTC configuration
2726 * @dev: drm device for the ioctl
2727 * @data: data pointer for the ioctl
2728 * @file_priv: drm file for the ioctl call
2730 * Build a new CRTC configuration based on user request.
2732 * Called by the user via ioctl.
2735 * Zero on success, negative errno on failure.
2737 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2738 struct drm_file *file_priv)
2740 struct drm_mode_config *config = &dev->mode_config;
2741 struct drm_mode_crtc *crtc_req = data;
2742 struct drm_crtc *crtc;
2743 struct drm_connector **connector_set = NULL, *connector;
2744 struct drm_framebuffer *fb = NULL;
2745 struct drm_display_mode *mode = NULL;
2746 struct drm_mode_set set;
2747 uint32_t __user *set_connectors_ptr;
2751 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2754 /* For some reason crtc x/y offsets are signed internally. */
2755 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2758 drm_modeset_lock_all(dev);
2759 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2761 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2765 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2767 if (crtc_req->mode_valid) {
2768 /* If we have a mode we need a framebuffer. */
2769 /* If we pass -1, set the mode with the currently bound fb */
2770 if (crtc_req->fb_id == -1) {
2771 if (!crtc->primary->fb) {
2772 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2776 fb = crtc->primary->fb;
2777 /* Make refcounting symmetric with the lookup path. */
2778 drm_framebuffer_reference(fb);
2780 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2782 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2789 mode = drm_mode_create(dev);
2795 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2797 DRM_DEBUG_KMS("Invalid mode\n");
2801 mode->status = drm_mode_validate_basic(mode);
2802 if (mode->status != MODE_OK) {
2807 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2810 * Check whether the primary plane supports the fb pixel format.
2811 * Drivers not implementing the universal planes API use a
2812 * default formats list provided by the DRM core which doesn't
2813 * match real hardware capabilities. Skip the check in that
2816 if (!crtc->primary->format_default) {
2817 ret = drm_plane_check_pixel_format(crtc->primary,
2820 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2821 drm_get_format_name(fb->pixel_format));
2826 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2833 if (crtc_req->count_connectors == 0 && mode) {
2834 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2839 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2840 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2841 crtc_req->count_connectors);
2846 if (crtc_req->count_connectors > 0) {
2849 /* Avoid unbounded kernel memory allocation */
2850 if (crtc_req->count_connectors > config->num_connector) {
2855 connector_set = kmalloc_array(crtc_req->count_connectors,
2856 sizeof(struct drm_connector *),
2858 if (!connector_set) {
2863 for (i = 0; i < crtc_req->count_connectors; i++) {
2864 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2865 if (get_user(out_id, &set_connectors_ptr[i])) {
2870 connector = drm_connector_find(dev, out_id);
2872 DRM_DEBUG_KMS("Connector id %d unknown\n",
2877 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2881 connector_set[i] = connector;
2886 set.x = crtc_req->x;
2887 set.y = crtc_req->y;
2889 set.connectors = connector_set;
2890 set.num_connectors = crtc_req->count_connectors;
2892 ret = drm_mode_set_config_internal(&set);
2896 drm_framebuffer_unreference(fb);
2898 kfree(connector_set);
2899 drm_mode_destroy(dev, mode);
2900 drm_modeset_unlock_all(dev);
2905 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2906 * universal plane handler call
2907 * @crtc: crtc to update cursor for
2908 * @req: data pointer for the ioctl
2909 * @file_priv: drm file for the ioctl call
2911 * Legacy cursor ioctl's work directly with driver buffer handles. To
2912 * translate legacy ioctl calls into universal plane handler calls, we need to
2913 * wrap the native buffer handle in a drm_framebuffer.
2915 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2916 * buffer with a pitch of 4*width; the universal plane interface should be used
2917 * directly in cases where the hardware can support other buffer settings and
2918 * userspace wants to make use of these capabilities.
2921 * Zero on success, negative errno on failure.
2923 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2924 struct drm_mode_cursor2 *req,
2925 struct drm_file *file_priv)
2927 struct drm_device *dev = crtc->dev;
2928 struct drm_framebuffer *fb = NULL;
2929 struct drm_mode_fb_cmd2 fbreq = {
2930 .width = req->width,
2931 .height = req->height,
2932 .pixel_format = DRM_FORMAT_ARGB8888,
2933 .pitches = { req->width * 4 },
2934 .handles = { req->handle },
2936 int32_t crtc_x, crtc_y;
2937 uint32_t crtc_w = 0, crtc_h = 0;
2938 uint32_t src_w = 0, src_h = 0;
2941 BUG_ON(!crtc->cursor);
2942 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2945 * Obtain fb we'll be using (either new or existing) and take an extra
2946 * reference to it if fb != null. setplane will take care of dropping
2947 * the reference if the plane update fails.
2949 if (req->flags & DRM_MODE_CURSOR_BO) {
2951 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2953 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2960 fb = crtc->cursor->fb;
2962 drm_framebuffer_reference(fb);
2965 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2969 crtc_x = crtc->cursor_x;
2970 crtc_y = crtc->cursor_y;
2975 crtc_h = fb->height;
2976 src_w = fb->width << 16;
2977 src_h = fb->height << 16;
2981 * setplane_internal will take care of deref'ing either the old or new
2982 * framebuffer depending on success.
2984 ret = __setplane_internal(crtc->cursor, crtc, fb,
2985 crtc_x, crtc_y, crtc_w, crtc_h,
2986 0, 0, src_w, src_h);
2988 /* Update successful; save new cursor position, if necessary */
2989 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2990 crtc->cursor_x = req->x;
2991 crtc->cursor_y = req->y;
2997 static int drm_mode_cursor_common(struct drm_device *dev,
2998 struct drm_mode_cursor2 *req,
2999 struct drm_file *file_priv)
3001 struct drm_crtc *crtc;
3004 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3007 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
3010 crtc = drm_crtc_find(dev, req->crtc_id);
3012 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
3017 * If this crtc has a universal cursor plane, call that plane's update
3018 * handler rather than using legacy cursor handlers.
3020 drm_modeset_lock_crtc(crtc, crtc->cursor);
3022 ret = drm_mode_cursor_universal(crtc, req, file_priv);
3026 if (req->flags & DRM_MODE_CURSOR_BO) {
3027 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
3031 /* Turns off the cursor if handle is 0 */
3032 if (crtc->funcs->cursor_set2)
3033 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3034 req->width, req->height, req->hot_x, req->hot_y);
3036 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3037 req->width, req->height);
3040 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3041 if (crtc->funcs->cursor_move) {
3042 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3049 drm_modeset_unlock_crtc(crtc);
3057 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3058 * @dev: drm device for the ioctl
3059 * @data: data pointer for the ioctl
3060 * @file_priv: drm file for the ioctl call
3062 * Set the cursor configuration based on user request.
3064 * Called by the user via ioctl.
3067 * Zero on success, negative errno on failure.
3069 int drm_mode_cursor_ioctl(struct drm_device *dev,
3070 void *data, struct drm_file *file_priv)
3072 struct drm_mode_cursor *req = data;
3073 struct drm_mode_cursor2 new_req;
3075 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3076 new_req.hot_x = new_req.hot_y = 0;
3078 return drm_mode_cursor_common(dev, &new_req, file_priv);
3082 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3083 * @dev: drm device for the ioctl
3084 * @data: data pointer for the ioctl
3085 * @file_priv: drm file for the ioctl call
3087 * Set the cursor configuration based on user request. This implements the 2nd
3088 * version of the cursor ioctl, which allows userspace to additionally specify
3089 * the hotspot of the pointer.
3091 * Called by the user via ioctl.
3094 * Zero on success, negative errno on failure.
3096 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3097 void *data, struct drm_file *file_priv)
3099 struct drm_mode_cursor2 *req = data;
3101 return drm_mode_cursor_common(dev, req, file_priv);
3105 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3106 * @bpp: bits per pixels
3107 * @depth: bit depth per pixel
3109 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3110 * Useful in fbdev emulation code, since that deals in those values.
3112 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3118 fmt = DRM_FORMAT_C8;
3122 fmt = DRM_FORMAT_XRGB1555;
3124 fmt = DRM_FORMAT_RGB565;
3127 fmt = DRM_FORMAT_RGB888;
3131 fmt = DRM_FORMAT_XRGB8888;
3132 else if (depth == 30)
3133 fmt = DRM_FORMAT_XRGB2101010;
3135 fmt = DRM_FORMAT_ARGB8888;
3138 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3139 fmt = DRM_FORMAT_XRGB8888;
3145 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3148 * drm_mode_addfb - add an FB to the graphics configuration
3149 * @dev: drm device for the ioctl
3150 * @data: data pointer for the ioctl
3151 * @file_priv: drm file for the ioctl call
3153 * Add a new FB to the specified CRTC, given a user request. This is the
3154 * original addfb ioctl which only supported RGB formats.
3156 * Called by the user via ioctl.
3159 * Zero on success, negative errno on failure.
3161 int drm_mode_addfb(struct drm_device *dev,
3162 void *data, struct drm_file *file_priv)
3164 struct drm_mode_fb_cmd *or = data;
3165 struct drm_mode_fb_cmd2 r = {};
3168 /* convert to new format and call new ioctl */
3169 r.fb_id = or->fb_id;
3170 r.width = or->width;
3171 r.height = or->height;
3172 r.pitches[0] = or->pitch;
3173 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3174 r.handles[0] = or->handle;
3176 ret = drm_mode_addfb2(dev, &r, file_priv);
3180 or->fb_id = r.fb_id;
3185 static int format_check(const struct drm_mode_fb_cmd2 *r)
3187 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3191 case DRM_FORMAT_RGB332:
3192 case DRM_FORMAT_BGR233:
3193 case DRM_FORMAT_XRGB4444:
3194 case DRM_FORMAT_XBGR4444:
3195 case DRM_FORMAT_RGBX4444:
3196 case DRM_FORMAT_BGRX4444:
3197 case DRM_FORMAT_ARGB4444:
3198 case DRM_FORMAT_ABGR4444:
3199 case DRM_FORMAT_RGBA4444:
3200 case DRM_FORMAT_BGRA4444:
3201 case DRM_FORMAT_XRGB1555:
3202 case DRM_FORMAT_XBGR1555:
3203 case DRM_FORMAT_RGBX5551:
3204 case DRM_FORMAT_BGRX5551:
3205 case DRM_FORMAT_ARGB1555:
3206 case DRM_FORMAT_ABGR1555:
3207 case DRM_FORMAT_RGBA5551:
3208 case DRM_FORMAT_BGRA5551:
3209 case DRM_FORMAT_RGB565:
3210 case DRM_FORMAT_BGR565:
3211 case DRM_FORMAT_RGB888:
3212 case DRM_FORMAT_BGR888:
3213 case DRM_FORMAT_XRGB8888:
3214 case DRM_FORMAT_XBGR8888:
3215 case DRM_FORMAT_RGBX8888:
3216 case DRM_FORMAT_BGRX8888:
3217 case DRM_FORMAT_ARGB8888:
3218 case DRM_FORMAT_ABGR8888:
3219 case DRM_FORMAT_RGBA8888:
3220 case DRM_FORMAT_BGRA8888:
3221 case DRM_FORMAT_XRGB2101010:
3222 case DRM_FORMAT_XBGR2101010:
3223 case DRM_FORMAT_RGBX1010102:
3224 case DRM_FORMAT_BGRX1010102:
3225 case DRM_FORMAT_ARGB2101010:
3226 case DRM_FORMAT_ABGR2101010:
3227 case DRM_FORMAT_RGBA1010102:
3228 case DRM_FORMAT_BGRA1010102:
3229 case DRM_FORMAT_YUYV:
3230 case DRM_FORMAT_YVYU:
3231 case DRM_FORMAT_UYVY:
3232 case DRM_FORMAT_VYUY:
3233 case DRM_FORMAT_AYUV:
3234 case DRM_FORMAT_NV12:
3235 case DRM_FORMAT_NV21:
3236 case DRM_FORMAT_NV16:
3237 case DRM_FORMAT_NV61:
3238 case DRM_FORMAT_NV24:
3239 case DRM_FORMAT_NV42:
3240 case DRM_FORMAT_YUV410:
3241 case DRM_FORMAT_YVU410:
3242 case DRM_FORMAT_YUV411:
3243 case DRM_FORMAT_YVU411:
3244 case DRM_FORMAT_YUV420:
3245 case DRM_FORMAT_YVU420:
3246 case DRM_FORMAT_YUV422:
3247 case DRM_FORMAT_YVU422:
3248 case DRM_FORMAT_YUV444:
3249 case DRM_FORMAT_YVU444:
3252 DRM_DEBUG_KMS("invalid pixel format %s\n",
3253 drm_get_format_name(r->pixel_format));
3258 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3260 int ret, hsub, vsub, num_planes, i;
3262 ret = format_check(r);
3264 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3265 drm_get_format_name(r->pixel_format));
3269 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3270 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3271 num_planes = drm_format_num_planes(r->pixel_format);
3273 if (r->width == 0 || r->width % hsub) {
3274 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3278 if (r->height == 0 || r->height % vsub) {
3279 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3283 for (i = 0; i < num_planes; i++) {
3284 unsigned int width = r->width / (i != 0 ? hsub : 1);
3285 unsigned int height = r->height / (i != 0 ? vsub : 1);
3286 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3288 if (!r->handles[i]) {
3289 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3293 if ((uint64_t) width * cpp > UINT_MAX)
3296 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3299 if (r->pitches[i] < width * cpp) {
3300 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3304 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3305 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3314 static struct drm_framebuffer *
3315 internal_framebuffer_create(struct drm_device *dev,
3316 struct drm_mode_fb_cmd2 *r,
3317 struct drm_file *file_priv)
3319 struct drm_mode_config *config = &dev->mode_config;
3320 struct drm_framebuffer *fb;
3323 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3324 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3325 return ERR_PTR(-EINVAL);
3328 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3329 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3330 r->width, config->min_width, config->max_width);
3331 return ERR_PTR(-EINVAL);
3333 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3334 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3335 r->height, config->min_height, config->max_height);
3336 return ERR_PTR(-EINVAL);
3339 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3340 !dev->mode_config.allow_fb_modifiers) {
3341 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3342 return ERR_PTR(-EINVAL);
3345 ret = framebuffer_check(r);
3347 return ERR_PTR(ret);
3349 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3351 DRM_DEBUG_KMS("could not create framebuffer\n");
3359 * drm_mode_addfb2 - add an FB to the graphics configuration
3360 * @dev: drm device for the ioctl
3361 * @data: data pointer for the ioctl
3362 * @file_priv: drm file for the ioctl call
3364 * Add a new FB to the specified CRTC, given a user request with format. This is
3365 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3366 * and uses fourcc codes as pixel format specifiers.
3368 * Called by the user via ioctl.
3371 * Zero on success, negative errno on failure.
3373 int drm_mode_addfb2(struct drm_device *dev,
3374 void *data, struct drm_file *file_priv)
3376 struct drm_mode_fb_cmd2 *r = data;
3377 struct drm_framebuffer *fb;
3379 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3382 fb = internal_framebuffer_create(dev, r, file_priv);
3386 /* Transfer ownership to the filp for reaping on close */
3388 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3389 mutex_lock(&file_priv->fbs_lock);
3390 r->fb_id = fb->base.id;
3391 list_add(&fb->filp_head, &file_priv->fbs);
3392 mutex_unlock(&file_priv->fbs_lock);
3398 * drm_mode_rmfb - remove an FB from the configuration
3399 * @dev: drm device for the ioctl
3400 * @data: data pointer for the ioctl
3401 * @file_priv: drm file for the ioctl call
3403 * Remove the FB specified by the user.
3405 * Called by the user via ioctl.
3408 * Zero on success, negative errno on failure.
3410 int drm_mode_rmfb(struct drm_device *dev,
3411 void *data, struct drm_file *file_priv)
3413 struct drm_framebuffer *fb = NULL;
3414 struct drm_framebuffer *fbl = NULL;
3415 uint32_t *id = data;
3418 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3421 mutex_lock(&file_priv->fbs_lock);
3422 mutex_lock(&dev->mode_config.fb_lock);
3423 fb = __drm_framebuffer_lookup(dev, *id);
3427 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3433 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3434 __drm_framebuffer_unregister(dev, fb);
3436 list_del_init(&fb->filp_head);
3437 mutex_unlock(&dev->mode_config.fb_lock);
3438 mutex_unlock(&file_priv->fbs_lock);
3440 drm_framebuffer_remove(fb);
3445 mutex_unlock(&dev->mode_config.fb_lock);
3446 mutex_unlock(&file_priv->fbs_lock);
3452 * drm_mode_getfb - get FB info
3453 * @dev: drm device for the ioctl
3454 * @data: data pointer for the ioctl
3455 * @file_priv: drm file for the ioctl call
3457 * Lookup the FB given its ID and return info about it.
3459 * Called by the user via ioctl.
3462 * Zero on success, negative errno on failure.
3464 int drm_mode_getfb(struct drm_device *dev,
3465 void *data, struct drm_file *file_priv)
3467 struct drm_mode_fb_cmd *r = data;
3468 struct drm_framebuffer *fb;
3471 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3474 fb = drm_framebuffer_lookup(dev, r->fb_id);
3478 r->height = fb->height;
3479 r->width = fb->width;
3480 r->depth = fb->depth;
3481 r->bpp = fb->bits_per_pixel;
3482 r->pitch = fb->pitches[0];
3483 if (fb->funcs->create_handle) {
3484 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3485 drm_is_control_client(file_priv)) {
3486 ret = fb->funcs->create_handle(fb, file_priv,
3489 /* GET_FB() is an unprivileged ioctl so we must not
3490 * return a buffer-handle to non-master processes! For
3491 * backwards-compatibility reasons, we cannot make
3492 * GET_FB() privileged, so just return an invalid handle
3493 * for non-masters. */
3501 drm_framebuffer_unreference(fb);
3507 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3508 * @dev: drm device for the ioctl
3509 * @data: data pointer for the ioctl
3510 * @file_priv: drm file for the ioctl call
3512 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3513 * rectangle list. Generic userspace which does frontbuffer rendering must call
3514 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3515 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3517 * Modesetting drivers which always update the frontbuffer do not need to
3518 * implement the corresponding ->dirty framebuffer callback.
3520 * Called by the user via ioctl.
3523 * Zero on success, negative errno on failure.
3525 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3526 void *data, struct drm_file *file_priv)
3528 struct drm_clip_rect __user *clips_ptr;
3529 struct drm_clip_rect *clips = NULL;
3530 struct drm_mode_fb_dirty_cmd *r = data;
3531 struct drm_framebuffer *fb;
3536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3539 fb = drm_framebuffer_lookup(dev, r->fb_id);
3543 num_clips = r->num_clips;
3544 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3546 if (!num_clips != !clips_ptr) {
3551 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3553 /* If userspace annotates copy, clips must come in pairs */
3554 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3559 if (num_clips && clips_ptr) {
3560 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3564 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3570 ret = copy_from_user(clips, clips_ptr,
3571 num_clips * sizeof(*clips));
3578 if (fb->funcs->dirty) {
3579 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3588 drm_framebuffer_unreference(fb);
3595 * drm_fb_release - remove and free the FBs on this file
3596 * @priv: drm file for the ioctl
3598 * Destroy all the FBs associated with @filp.
3600 * Called by the user via ioctl.
3603 * Zero on success, negative errno on failure.
3605 void drm_fb_release(struct drm_file *priv)
3607 struct drm_device *dev = priv->minor->dev;
3608 struct drm_framebuffer *fb, *tfb;
3611 * When the file gets released that means no one else can access the fb
3612 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3613 * avoid upsetting lockdep since the universal cursor code adds a
3614 * framebuffer while holding mutex locks.
3616 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3617 * locks is impossible here since no one else but this function can get
3620 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3622 mutex_lock(&dev->mode_config.fb_lock);
3623 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3624 __drm_framebuffer_unregister(dev, fb);
3625 mutex_unlock(&dev->mode_config.fb_lock);
3627 list_del_init(&fb->filp_head);
3629 /* This will also drop the fpriv->fbs reference. */
3630 drm_framebuffer_remove(fb);
3635 * drm_property_create - create a new property type
3637 * @flags: flags specifying the property type
3638 * @name: name of the property
3639 * @num_values: number of pre-defined values
3641 * This creates a new generic drm property which can then be attached to a drm
3642 * object with drm_object_attach_property. The returned property object must be
3643 * freed with drm_property_destroy.
3645 * Note that the DRM core keeps a per-device list of properties and that, if
3646 * drm_mode_config_cleanup() is called, it will destroy all properties created
3650 * A pointer to the newly created property on success, NULL on failure.
3652 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3653 const char *name, int num_values)
3655 struct drm_property *property = NULL;
3658 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3662 property->dev = dev;
3665 property->values = kcalloc(num_values, sizeof(uint64_t),
3667 if (!property->values)
3671 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3675 property->flags = flags;
3676 property->num_values = num_values;
3677 INIT_LIST_HEAD(&property->enum_list);
3680 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3681 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3684 list_add_tail(&property->head, &dev->mode_config.property_list);
3686 WARN_ON(!drm_property_type_valid(property));
3690 kfree(property->values);
3694 EXPORT_SYMBOL(drm_property_create);
3697 * drm_property_create_enum - create a new enumeration property type
3699 * @flags: flags specifying the property type
3700 * @name: name of the property
3701 * @props: enumeration lists with property values
3702 * @num_values: number of pre-defined values
3704 * This creates a new generic drm property which can then be attached to a drm
3705 * object with drm_object_attach_property. The returned property object must be
3706 * freed with drm_property_destroy.
3708 * Userspace is only allowed to set one of the predefined values for enumeration
3712 * A pointer to the newly created property on success, NULL on failure.
3714 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3716 const struct drm_prop_enum_list *props,
3719 struct drm_property *property;
3722 flags |= DRM_MODE_PROP_ENUM;
3724 property = drm_property_create(dev, flags, name, num_values);
3728 for (i = 0; i < num_values; i++) {
3729 ret = drm_property_add_enum(property, i,
3733 drm_property_destroy(dev, property);
3740 EXPORT_SYMBOL(drm_property_create_enum);
3743 * drm_property_create_bitmask - create a new bitmask property type
3745 * @flags: flags specifying the property type
3746 * @name: name of the property
3747 * @props: enumeration lists with property bitflags
3748 * @num_props: size of the @props array
3749 * @supported_bits: bitmask of all supported enumeration values
3751 * This creates a new bitmask drm property which can then be attached to a drm
3752 * object with drm_object_attach_property. The returned property object must be
3753 * freed with drm_property_destroy.
3755 * Compared to plain enumeration properties userspace is allowed to set any
3756 * or'ed together combination of the predefined property bitflag values
3759 * A pointer to the newly created property on success, NULL on failure.
3761 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3762 int flags, const char *name,
3763 const struct drm_prop_enum_list *props,
3765 uint64_t supported_bits)
3767 struct drm_property *property;
3768 int i, ret, index = 0;
3769 int num_values = hweight64(supported_bits);
3771 flags |= DRM_MODE_PROP_BITMASK;
3773 property = drm_property_create(dev, flags, name, num_values);
3776 for (i = 0; i < num_props; i++) {
3777 if (!(supported_bits & (1ULL << props[i].type)))
3780 if (WARN_ON(index >= num_values)) {
3781 drm_property_destroy(dev, property);
3785 ret = drm_property_add_enum(property, index++,
3789 drm_property_destroy(dev, property);
3796 EXPORT_SYMBOL(drm_property_create_bitmask);
3798 static struct drm_property *property_create_range(struct drm_device *dev,
3799 int flags, const char *name,
3800 uint64_t min, uint64_t max)
3802 struct drm_property *property;
3804 property = drm_property_create(dev, flags, name, 2);
3808 property->values[0] = min;
3809 property->values[1] = max;
3815 * drm_property_create_range - create a new unsigned ranged property type
3817 * @flags: flags specifying the property type
3818 * @name: name of the property
3819 * @min: minimum value of the property
3820 * @max: maximum value of the property
3822 * This creates a new generic drm property which can then be attached to a drm
3823 * object with drm_object_attach_property. The returned property object must be
3824 * freed with drm_property_destroy.
3826 * Userspace is allowed to set any unsigned integer value in the (min, max)
3830 * A pointer to the newly created property on success, NULL on failure.
3832 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3834 uint64_t min, uint64_t max)
3836 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3839 EXPORT_SYMBOL(drm_property_create_range);
3842 * drm_property_create_signed_range - create a new signed ranged property type
3844 * @flags: flags specifying the property type
3845 * @name: name of the property
3846 * @min: minimum value of the property
3847 * @max: maximum value of the property
3849 * This creates a new generic drm property which can then be attached to a drm
3850 * object with drm_object_attach_property. The returned property object must be
3851 * freed with drm_property_destroy.
3853 * Userspace is allowed to set any signed integer value in the (min, max)
3857 * A pointer to the newly created property on success, NULL on failure.
3859 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3860 int flags, const char *name,
3861 int64_t min, int64_t max)
3863 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3864 name, I642U64(min), I642U64(max));
3866 EXPORT_SYMBOL(drm_property_create_signed_range);
3869 * drm_property_create_object - create a new object property type
3871 * @flags: flags specifying the property type
3872 * @name: name of the property
3873 * @type: object type from DRM_MODE_OBJECT_* defines
3875 * This creates a new generic drm property which can then be attached to a drm
3876 * object with drm_object_attach_property. The returned property object must be
3877 * freed with drm_property_destroy.
3879 * Userspace is only allowed to set this to any property value of the given
3880 * @type. Only useful for atomic properties, which is enforced.
3883 * A pointer to the newly created property on success, NULL on failure.
3885 struct drm_property *drm_property_create_object(struct drm_device *dev,
3886 int flags, const char *name, uint32_t type)
3888 struct drm_property *property;
3890 flags |= DRM_MODE_PROP_OBJECT;
3892 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3895 property = drm_property_create(dev, flags, name, 1);
3899 property->values[0] = type;
3903 EXPORT_SYMBOL(drm_property_create_object);
3906 * drm_property_create_bool - create a new boolean property type
3908 * @flags: flags specifying the property type
3909 * @name: name of the property
3911 * This creates a new generic drm property which can then be attached to a drm
3912 * object with drm_object_attach_property. The returned property object must be
3913 * freed with drm_property_destroy.
3915 * This is implemented as a ranged property with only {0, 1} as valid values.
3918 * A pointer to the newly created property on success, NULL on failure.
3920 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3923 return drm_property_create_range(dev, flags, name, 0, 1);
3925 EXPORT_SYMBOL(drm_property_create_bool);
3928 * drm_property_add_enum - add a possible value to an enumeration property
3929 * @property: enumeration property to change
3930 * @index: index of the new enumeration
3931 * @value: value of the new enumeration
3932 * @name: symbolic name of the new enumeration
3934 * This functions adds enumerations to a property.
3936 * It's use is deprecated, drivers should use one of the more specific helpers
3937 * to directly create the property with all enumerations already attached.
3940 * Zero on success, error code on failure.
3942 int drm_property_add_enum(struct drm_property *property, int index,
3943 uint64_t value, const char *name)
3945 struct drm_property_enum *prop_enum;
3947 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3948 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3952 * Bitmask enum properties have the additional constraint of values
3955 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3959 if (!list_empty(&property->enum_list)) {
3960 list_for_each_entry(prop_enum, &property->enum_list, head) {
3961 if (prop_enum->value == value) {
3962 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3963 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3969 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3973 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3974 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3975 prop_enum->value = value;
3977 property->values[index] = value;
3978 list_add_tail(&prop_enum->head, &property->enum_list);
3981 EXPORT_SYMBOL(drm_property_add_enum);
3984 * drm_property_destroy - destroy a drm property
3986 * @property: property to destry
3988 * This function frees a property including any attached resources like
3989 * enumeration values.
3991 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3993 struct drm_property_enum *prop_enum, *pt;
3995 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3996 list_del(&prop_enum->head);
4000 if (property->num_values)
4001 kfree(property->values);
4002 drm_mode_object_put(dev, &property->base);
4003 list_del(&property->head);
4006 EXPORT_SYMBOL(drm_property_destroy);
4009 * drm_object_attach_property - attach a property to a modeset object
4010 * @obj: drm modeset object
4011 * @property: property to attach
4012 * @init_val: initial value of the property
4014 * This attaches the given property to the modeset object with the given initial
4015 * value. Currently this function cannot fail since the properties are stored in
4016 * a statically sized array.
4018 void drm_object_attach_property(struct drm_mode_object *obj,
4019 struct drm_property *property,
4022 int count = obj->properties->count;
4024 if (count == DRM_OBJECT_MAX_PROPERTY) {
4025 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4026 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4027 "you see this message on the same object type.\n",
4032 obj->properties->properties[count] = property;
4033 obj->properties->values[count] = init_val;
4034 obj->properties->count++;
4035 if (property->flags & DRM_MODE_PROP_ATOMIC)
4036 obj->properties->atomic_count++;
4038 EXPORT_SYMBOL(drm_object_attach_property);
4041 * drm_object_property_set_value - set the value of a property
4042 * @obj: drm mode object to set property value for
4043 * @property: property to set
4044 * @val: value the property should be set to
4046 * This functions sets a given property on a given object. This function only
4047 * changes the software state of the property, it does not call into the
4048 * driver's ->set_property callback.
4051 * Zero on success, error code on failure.
4053 int drm_object_property_set_value(struct drm_mode_object *obj,
4054 struct drm_property *property, uint64_t val)
4058 for (i = 0; i < obj->properties->count; i++) {
4059 if (obj->properties->properties[i] == property) {
4060 obj->properties->values[i] = val;
4067 EXPORT_SYMBOL(drm_object_property_set_value);
4070 * drm_object_property_get_value - retrieve the value of a property
4071 * @obj: drm mode object to get property value from
4072 * @property: property to retrieve
4073 * @val: storage for the property value
4075 * This function retrieves the softare state of the given property for the given
4076 * property. Since there is no driver callback to retrieve the current property
4077 * value this might be out of sync with the hardware, depending upon the driver
4081 * Zero on success, error code on failure.
4083 int drm_object_property_get_value(struct drm_mode_object *obj,
4084 struct drm_property *property, uint64_t *val)
4088 /* read-only properties bypass atomic mechanism and still store
4089 * their value in obj->properties->values[].. mostly to avoid
4090 * having to deal w/ EDID and similar props in atomic paths:
4092 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4093 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4094 return drm_atomic_get_property(obj, property, val);
4096 for (i = 0; i < obj->properties->count; i++) {
4097 if (obj->properties->properties[i] == property) {
4098 *val = obj->properties->values[i];
4105 EXPORT_SYMBOL(drm_object_property_get_value);
4108 * drm_mode_getproperty_ioctl - get the property metadata
4111 * @file_priv: DRM file info
4113 * This function retrieves the metadata for a given property, like the different
4114 * possible values for an enum property or the limits for a range property.
4116 * Blob properties are special
4118 * Called by the user via ioctl.
4121 * Zero on success, negative errno on failure.
4123 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4124 void *data, struct drm_file *file_priv)
4126 struct drm_mode_get_property *out_resp = data;
4127 struct drm_property *property;
4129 int value_count = 0;
4132 struct drm_property_enum *prop_enum;
4133 struct drm_mode_property_enum __user *enum_ptr;
4134 uint64_t __user *values_ptr;
4136 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4139 drm_modeset_lock_all(dev);
4140 property = drm_property_find(dev, out_resp->prop_id);
4146 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4147 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4148 list_for_each_entry(prop_enum, &property->enum_list, head)
4152 value_count = property->num_values;
4154 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4155 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4156 out_resp->flags = property->flags;
4158 if ((out_resp->count_values >= value_count) && value_count) {
4159 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4160 for (i = 0; i < value_count; i++) {
4161 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4167 out_resp->count_values = value_count;
4169 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4170 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4171 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4173 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4174 list_for_each_entry(prop_enum, &property->enum_list, head) {
4176 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4181 if (copy_to_user(&enum_ptr[copied].name,
4182 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4189 out_resp->count_enum_blobs = enum_count;
4193 * NOTE: The idea seems to have been to use this to read all the blob
4194 * property values. But nothing ever added them to the corresponding
4195 * list, userspace always used the special-purpose get_blob ioctl to
4196 * read the value for a blob property. It also doesn't make a lot of
4197 * sense to return values here when everything else is just metadata for
4198 * the property itself.
4200 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4201 out_resp->count_enum_blobs = 0;
4203 drm_modeset_unlock_all(dev);
4207 struct drm_property_blob *
4208 drm_property_create_blob(struct drm_device *dev, size_t length,
4211 struct drm_property_blob *blob;
4214 if (!length || !data)
4217 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4221 blob->length = length;
4224 memcpy(blob->data, data, length);
4226 mutex_lock(&dev->mode_config.blob_lock);
4228 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4231 mutex_unlock(&dev->mode_config.blob_lock);
4235 kref_init(&blob->refcount);
4237 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4239 mutex_unlock(&dev->mode_config.blob_lock);
4243 EXPORT_SYMBOL(drm_property_create_blob);
4246 * drm_property_free_blob - Blob property destructor
4248 * Internal free function for blob properties; must not be used directly.
4250 * @param kref Reference
4252 static void drm_property_free_blob(struct kref *kref)
4254 struct drm_property_blob *blob =
4255 container_of(kref, struct drm_property_blob, refcount);
4257 WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
4259 list_del(&blob->head);
4260 drm_mode_object_put(blob->dev, &blob->base);
4266 * drm_property_unreference_blob - Unreference a blob property
4268 * Drop a reference on a blob property. May free the object.
4270 * @param blob Pointer to blob property
4272 void drm_property_unreference_blob(struct drm_property_blob *blob)
4274 struct drm_device *dev;
4281 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4283 if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
4284 &dev->mode_config.blob_lock))
4285 mutex_unlock(&dev->mode_config.blob_lock);
4287 might_lock(&dev->mode_config.blob_lock);
4290 EXPORT_SYMBOL(drm_property_unreference_blob);
4293 * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
4295 * Drop a reference on a blob property. May free the object. This must be
4296 * called with blob_lock held.
4298 * @param dev Device the blob was created on
4299 * @param blob Pointer to blob property
4301 static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
4306 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4308 kref_put(&blob->refcount, drm_property_free_blob);
4312 * drm_property_reference_blob - Take a reference on an existing property
4314 * Take a new reference on an existing blob property.
4316 * @param blob Pointer to blob property
4318 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4320 DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
4321 kref_get(&blob->refcount);
4324 EXPORT_SYMBOL(drm_property_reference_blob);
4327 * Like drm_property_lookup_blob, but does not return an additional reference.
4328 * Must be called with blob_lock held.
4330 static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
4333 struct drm_mode_object *obj = NULL;
4334 struct drm_property_blob *blob;
4336 WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
4338 mutex_lock(&dev->mode_config.idr_mutex);
4339 obj = idr_find(&dev->mode_config.crtc_idr, id);
4340 if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
4343 blob = obj_to_blob(obj);
4344 mutex_unlock(&dev->mode_config.idr_mutex);
4350 * drm_property_lookup_blob - look up a blob property and take a reference
4352 * @id: id of the blob property
4354 * If successful, this takes an additional reference to the blob property.
4355 * callers need to make sure to eventually unreference the returned property
4356 * again, using @drm_property_unreference_blob.
4358 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4361 struct drm_property_blob *blob;
4363 mutex_lock(&dev->mode_config.blob_lock);
4364 blob = __drm_property_lookup_blob(dev, id);
4366 if (!kref_get_unless_zero(&blob->refcount))
4369 mutex_unlock(&dev->mode_config.blob_lock);
4373 EXPORT_SYMBOL(drm_property_lookup_blob);
4376 * drm_property_replace_global_blob - atomically replace existing blob property
4378 * @replace: location of blob property pointer to be replaced
4379 * @length: length of data for new blob, or 0 for no data
4380 * @data: content for new blob, or NULL for no data
4381 * @obj_holds_id: optional object for property holding blob ID
4382 * @prop_holds_id: optional property holding blob ID
4383 * @return 0 on success or error on failure
4385 * This function will atomically replace a global property in the blob list,
4386 * optionally updating a property which holds the ID of that property. It is
4387 * guaranteed to be atomic: no caller will be allowed to see intermediate
4388 * results, and either the entire operation will succeed and clean up the
4389 * previous property, or it will fail and the state will be unchanged.
4391 * If length is 0 or data is NULL, no new blob will be created, and the holding
4392 * property, if specified, will be set to 0.
4394 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4395 * by holding the relevant modesetting object lock for its parent.
4397 * For example, a drm_connector has a 'PATH' property, which contains the ID
4398 * of a blob property with the value of the MST path information. Calling this
4399 * function with replace pointing to the connector's path_blob_ptr, length and
4400 * data set for the new path information, obj_holds_id set to the connector's
4401 * base object, and prop_holds_id set to the path property name, will perform
4402 * a completely atomic update. The access to path_blob_ptr is protected by the
4403 * caller holding a lock on the connector.
4405 static int drm_property_replace_global_blob(struct drm_device *dev,
4406 struct drm_property_blob **replace,
4409 struct drm_mode_object *obj_holds_id,
4410 struct drm_property *prop_holds_id)
4412 struct drm_property_blob *new_blob = NULL;
4413 struct drm_property_blob *old_blob = NULL;
4416 WARN_ON(replace == NULL);
4418 old_blob = *replace;
4420 if (length && data) {
4421 new_blob = drm_property_create_blob(dev, length, data);
4426 /* This does not need to be synchronised with blob_lock, as the
4427 * get_properties ioctl locks all modesetting objects, and
4428 * obj_holds_id must be locked before calling here, so we cannot
4429 * have its value out of sync with the list membership modified
4430 * below under blob_lock. */
4432 ret = drm_object_property_set_value(obj_holds_id,
4435 new_blob->base.id : 0);
4441 drm_property_unreference_blob(old_blob);
4443 *replace = new_blob;
4448 drm_property_unreference_blob(new_blob);
4453 * drm_mode_getblob_ioctl - get the contents of a blob property value
4456 * @file_priv: DRM file info
4458 * This function retrieves the contents of a blob property. The value stored in
4459 * an object's blob property is just a normal modeset object id.
4461 * Called by the user via ioctl.
4464 * Zero on success, negative errno on failure.
4466 int drm_mode_getblob_ioctl(struct drm_device *dev,
4467 void *data, struct drm_file *file_priv)
4469 struct drm_mode_get_blob *out_resp = data;
4470 struct drm_property_blob *blob;
4472 void __user *blob_ptr;
4474 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4477 drm_modeset_lock_all(dev);
4478 mutex_lock(&dev->mode_config.blob_lock);
4479 blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
4485 if (out_resp->length == blob->length) {
4486 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4487 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4492 out_resp->length = blob->length;
4495 mutex_unlock(&dev->mode_config.blob_lock);
4496 drm_modeset_unlock_all(dev);
4501 * drm_mode_connector_set_path_property - set tile property on connector
4502 * @connector: connector to set property on.
4503 * @path: path to use for property; must not be NULL.
4505 * This creates a property to expose to userspace to specify a
4506 * connector path. This is mainly used for DisplayPort MST where
4507 * connectors have a topology and we want to allow userspace to give
4508 * them more meaningful names.
4511 * Zero on success, negative errno on failure.
4513 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4516 struct drm_device *dev = connector->dev;
4519 ret = drm_property_replace_global_blob(dev,
4520 &connector->path_blob_ptr,
4524 dev->mode_config.path_property);
4527 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4530 * drm_mode_connector_set_tile_property - set tile property on connector
4531 * @connector: connector to set property on.
4533 * This looks up the tile information for a connector, and creates a
4534 * property for userspace to parse if it exists. The property is of
4535 * the form of 8 integers using ':' as a separator.
4538 * Zero on success, errno on failure.
4540 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4542 struct drm_device *dev = connector->dev;
4546 if (!connector->has_tile) {
4547 ret = drm_property_replace_global_blob(dev,
4548 &connector->tile_blob_ptr,
4552 dev->mode_config.tile_property);
4556 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4557 connector->tile_group->id, connector->tile_is_single_monitor,
4558 connector->num_h_tile, connector->num_v_tile,
4559 connector->tile_h_loc, connector->tile_v_loc,
4560 connector->tile_h_size, connector->tile_v_size);
4562 ret = drm_property_replace_global_blob(dev,
4563 &connector->tile_blob_ptr,
4567 dev->mode_config.tile_property);
4570 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4573 * drm_mode_connector_update_edid_property - update the edid property of a connector
4574 * @connector: drm connector
4575 * @edid: new value of the edid property
4577 * This function creates a new blob modeset object and assigns its id to the
4578 * connector's edid property.
4581 * Zero on success, negative errno on failure.
4583 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4584 const struct edid *edid)
4586 struct drm_device *dev = connector->dev;
4590 /* ignore requests to set edid when overridden */
4591 if (connector->override_edid)
4595 size = EDID_LENGTH + (1 + edid->extensions);
4597 ret = drm_property_replace_global_blob(dev,
4598 &connector->edid_blob_ptr,
4602 dev->mode_config.edid_property);
4605 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4607 /* Some properties could refer to dynamic refcnt'd objects, or things that
4608 * need special locking to handle lifetime issues (ie. to ensure the prop
4609 * value doesn't become invalid part way through the property update due to
4610 * race). The value returned by reference via 'obj' should be passed back
4611 * to drm_property_change_valid_put() after the property is set (and the
4612 * object to which the property is attached has a chance to take it's own
4615 bool drm_property_change_valid_get(struct drm_property *property,
4616 uint64_t value, struct drm_mode_object **ref)
4620 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4625 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4626 if (value < property->values[0] || value > property->values[1])
4629 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4630 int64_t svalue = U642I64(value);
4632 if (svalue < U642I64(property->values[0]) ||
4633 svalue > U642I64(property->values[1]))
4636 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4637 uint64_t valid_mask = 0;
4639 for (i = 0; i < property->num_values; i++)
4640 valid_mask |= (1ULL << property->values[i]);
4641 return !(value & ~valid_mask);
4642 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4643 struct drm_property_blob *blob;
4648 blob = drm_property_lookup_blob(property->dev, value);
4655 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4656 /* a zero value for an object property translates to null: */
4660 /* handle refcnt'd objects specially: */
4661 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4662 struct drm_framebuffer *fb;
4663 fb = drm_framebuffer_lookup(property->dev, value);
4671 return _object_find(property->dev, value, property->values[0]) != NULL;
4675 for (i = 0; i < property->num_values; i++)
4676 if (property->values[i] == value)
4681 void drm_property_change_valid_put(struct drm_property *property,
4682 struct drm_mode_object *ref)
4687 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4688 if (property->values[0] == DRM_MODE_OBJECT_FB)
4689 drm_framebuffer_unreference(obj_to_fb(ref));
4694 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4697 * @file_priv: DRM file info
4699 * This function sets the current value for a connectors's property. It also
4700 * calls into a driver's ->set_property callback to update the hardware state
4702 * Called by the user via ioctl.
4705 * Zero on success, negative errno on failure.
4707 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4708 void *data, struct drm_file *file_priv)
4710 struct drm_mode_connector_set_property *conn_set_prop = data;
4711 struct drm_mode_obj_set_property obj_set_prop = {
4712 .value = conn_set_prop->value,
4713 .prop_id = conn_set_prop->prop_id,
4714 .obj_id = conn_set_prop->connector_id,
4715 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4718 /* It does all the locking and checking we need */
4719 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4722 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4723 struct drm_property *property,
4727 struct drm_connector *connector = obj_to_connector(obj);
4729 /* Do DPMS ourselves */
4730 if (property == connector->dev->mode_config.dpms_property) {
4731 if (connector->funcs->dpms)
4732 (*connector->funcs->dpms)(connector, (int)value);
4734 } else if (connector->funcs->set_property)
4735 ret = connector->funcs->set_property(connector, property, value);
4737 /* store the property value if successful */
4739 drm_object_property_set_value(&connector->base, property, value);
4743 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4744 struct drm_property *property,
4748 struct drm_crtc *crtc = obj_to_crtc(obj);
4750 if (crtc->funcs->set_property)
4751 ret = crtc->funcs->set_property(crtc, property, value);
4753 drm_object_property_set_value(obj, property, value);
4759 * drm_mode_plane_set_obj_prop - set the value of a property
4760 * @plane: drm plane object to set property value for
4761 * @property: property to set
4762 * @value: value the property should be set to
4764 * This functions sets a given property on a given plane object. This function
4765 * calls the driver's ->set_property callback and changes the software state of
4766 * the property if the callback succeeds.
4769 * Zero on success, error code on failure.
4771 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4772 struct drm_property *property,
4776 struct drm_mode_object *obj = &plane->base;
4778 if (plane->funcs->set_property)
4779 ret = plane->funcs->set_property(plane, property, value);
4781 drm_object_property_set_value(obj, property, value);
4785 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4788 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4791 * @file_priv: DRM file info
4793 * This function retrieves the current value for an object's property. Compared
4794 * to the connector specific ioctl this one is extended to also work on crtc and
4797 * Called by the user via ioctl.
4800 * Zero on success, negative errno on failure.
4802 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4803 struct drm_file *file_priv)
4805 struct drm_mode_obj_get_properties *arg = data;
4806 struct drm_mode_object *obj;
4809 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4812 drm_modeset_lock_all(dev);
4814 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4819 if (!obj->properties) {
4824 ret = get_properties(obj, file_priv->atomic,
4825 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4826 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4830 drm_modeset_unlock_all(dev);
4835 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4838 * @file_priv: DRM file info
4840 * This function sets the current value for an object's property. It also calls
4841 * into a driver's ->set_property callback to update the hardware state.
4842 * Compared to the connector specific ioctl this one is extended to also work on
4843 * crtc and plane objects.
4845 * Called by the user via ioctl.
4848 * Zero on success, negative errno on failure.
4850 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4851 struct drm_file *file_priv)
4853 struct drm_mode_obj_set_property *arg = data;
4854 struct drm_mode_object *arg_obj;
4855 struct drm_mode_object *prop_obj;
4856 struct drm_property *property;
4857 int i, ret = -EINVAL;
4858 struct drm_mode_object *ref;
4860 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4863 drm_modeset_lock_all(dev);
4865 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4870 if (!arg_obj->properties)
4873 for (i = 0; i < arg_obj->properties->count; i++)
4874 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
4877 if (i == arg_obj->properties->count)
4880 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4881 DRM_MODE_OBJECT_PROPERTY);
4886 property = obj_to_property(prop_obj);
4888 if (!drm_property_change_valid_get(property, arg->value, &ref))
4891 switch (arg_obj->type) {
4892 case DRM_MODE_OBJECT_CONNECTOR:
4893 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4896 case DRM_MODE_OBJECT_CRTC:
4897 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4899 case DRM_MODE_OBJECT_PLANE:
4900 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4901 property, arg->value);
4905 drm_property_change_valid_put(property, ref);
4908 drm_modeset_unlock_all(dev);
4913 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4914 * @connector: connector to attach
4915 * @encoder: encoder to attach @connector to
4917 * This function links up a connector to an encoder. Note that the routing
4918 * restrictions between encoders and crtcs are exposed to userspace through the
4919 * possible_clones and possible_crtcs bitmasks.
4922 * Zero on success, negative errno on failure.
4924 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4925 struct drm_encoder *encoder)
4929 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4930 if (connector->encoder_ids[i] == 0) {
4931 connector->encoder_ids[i] = encoder->base.id;
4937 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4940 * drm_mode_crtc_set_gamma_size - set the gamma table size
4941 * @crtc: CRTC to set the gamma table size for
4942 * @gamma_size: size of the gamma table
4944 * Drivers which support gamma tables should set this to the supported gamma
4945 * table size when initializing the CRTC. Currently the drm core only supports a
4946 * fixed gamma table size.
4949 * Zero on success, negative errno on failure.
4951 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4954 crtc->gamma_size = gamma_size;
4956 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4958 if (!crtc->gamma_store) {
4959 crtc->gamma_size = 0;
4965 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4968 * drm_mode_gamma_set_ioctl - set the gamma table
4971 * @file_priv: DRM file info
4973 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4974 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4976 * Called by the user via ioctl.
4979 * Zero on success, negative errno on failure.
4981 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4982 void *data, struct drm_file *file_priv)
4984 struct drm_mode_crtc_lut *crtc_lut = data;
4985 struct drm_crtc *crtc;
4986 void *r_base, *g_base, *b_base;
4990 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4993 drm_modeset_lock_all(dev);
4994 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5000 if (crtc->funcs->gamma_set == NULL) {
5005 /* memcpy into gamma store */
5006 if (crtc_lut->gamma_size != crtc->gamma_size) {
5011 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5012 r_base = crtc->gamma_store;
5013 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5018 g_base = r_base + size;
5019 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5024 b_base = g_base + size;
5025 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5030 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5033 drm_modeset_unlock_all(dev);
5039 * drm_mode_gamma_get_ioctl - get the gamma table
5042 * @file_priv: DRM file info
5044 * Copy the current gamma table into the storage provided. This also provides
5045 * the gamma table size the driver expects, which can be used to size the
5046 * allocated storage.
5048 * Called by the user via ioctl.
5051 * Zero on success, negative errno on failure.
5053 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5054 void *data, struct drm_file *file_priv)
5056 struct drm_mode_crtc_lut *crtc_lut = data;
5057 struct drm_crtc *crtc;
5058 void *r_base, *g_base, *b_base;
5062 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5065 drm_modeset_lock_all(dev);
5066 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5072 /* memcpy into gamma store */
5073 if (crtc_lut->gamma_size != crtc->gamma_size) {
5078 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5079 r_base = crtc->gamma_store;
5080 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5085 g_base = r_base + size;
5086 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5091 b_base = g_base + size;
5092 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5097 drm_modeset_unlock_all(dev);
5102 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5105 * @file_priv: DRM file info
5107 * This schedules an asynchronous update on a given CRTC, called page flip.
5108 * Optionally a drm event is generated to signal the completion of the event.
5109 * Generic drivers cannot assume that a pageflip with changed framebuffer
5110 * properties (including driver specific metadata like tiling layout) will work,
5111 * but some drivers support e.g. pixel format changes through the pageflip
5114 * Called by the user via ioctl.
5117 * Zero on success, negative errno on failure.
5119 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5120 void *data, struct drm_file *file_priv)
5122 struct drm_mode_crtc_page_flip *page_flip = data;
5123 struct drm_crtc *crtc;
5124 struct drm_framebuffer *fb = NULL;
5125 struct drm_pending_vblank_event *e = NULL;
5126 unsigned long flags;
5129 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5130 page_flip->reserved != 0)
5133 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5136 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5140 drm_modeset_lock_crtc(crtc, crtc->primary);
5141 if (crtc->primary->fb == NULL) {
5142 /* The framebuffer is currently unbound, presumably
5143 * due to a hotplug event, that userspace has not
5150 if (crtc->funcs->page_flip == NULL)
5153 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5159 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5163 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5164 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5169 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5171 spin_lock_irqsave(&dev->event_lock, flags);
5172 if (file_priv->event_space < sizeof(e->event)) {
5173 spin_unlock_irqrestore(&dev->event_lock, flags);
5176 file_priv->event_space -= sizeof(e->event);
5177 spin_unlock_irqrestore(&dev->event_lock, flags);
5179 e = kzalloc(sizeof(*e), GFP_KERNEL);
5181 spin_lock_irqsave(&dev->event_lock, flags);
5182 file_priv->event_space += sizeof(e->event);
5183 spin_unlock_irqrestore(&dev->event_lock, flags);
5187 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5188 e->event.base.length = sizeof(e->event);
5189 e->event.user_data = page_flip->user_data;
5190 e->base.event = &e->event.base;
5191 e->base.file_priv = file_priv;
5193 (void (*) (struct drm_pending_event *)) kfree;
5196 crtc->primary->old_fb = crtc->primary->fb;
5197 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5199 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5200 spin_lock_irqsave(&dev->event_lock, flags);
5201 file_priv->event_space += sizeof(e->event);
5202 spin_unlock_irqrestore(&dev->event_lock, flags);
5205 /* Keep the old fb, don't unref it. */
5206 crtc->primary->old_fb = NULL;
5209 * Warn if the driver hasn't properly updated the crtc->fb
5210 * field to reflect that the new framebuffer is now used.
5211 * Failing to do so will screw with the reference counting
5214 WARN_ON(crtc->primary->fb != fb);
5215 /* Unref only the old framebuffer. */
5221 drm_framebuffer_unreference(fb);
5222 if (crtc->primary->old_fb)
5223 drm_framebuffer_unreference(crtc->primary->old_fb);
5224 crtc->primary->old_fb = NULL;
5225 drm_modeset_unlock_crtc(crtc);
5231 * drm_mode_config_reset - call ->reset callbacks
5234 * This functions calls all the crtc's, encoder's and connector's ->reset
5235 * callback. Drivers can use this in e.g. their driver load or resume code to
5236 * reset hardware and software state.
5238 void drm_mode_config_reset(struct drm_device *dev)
5240 struct drm_crtc *crtc;
5241 struct drm_plane *plane;
5242 struct drm_encoder *encoder;
5243 struct drm_connector *connector;
5245 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
5246 if (plane->funcs->reset)
5247 plane->funcs->reset(plane);
5249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
5250 if (crtc->funcs->reset)
5251 crtc->funcs->reset(crtc);
5253 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
5254 if (encoder->funcs->reset)
5255 encoder->funcs->reset(encoder);
5257 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5258 connector->status = connector_status_unknown;
5260 if (connector->funcs->reset)
5261 connector->funcs->reset(connector);
5264 EXPORT_SYMBOL(drm_mode_config_reset);
5267 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5270 * @file_priv: DRM file info
5272 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5273 * TTM or something else entirely) and returns the resulting buffer handle. This
5274 * handle can then be wrapped up into a framebuffer modeset object.
5276 * Note that userspace is not allowed to use such objects for render
5277 * acceleration - drivers must create their own private ioctls for such a use
5280 * Called by the user via ioctl.
5283 * Zero on success, negative errno on failure.
5285 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5286 void *data, struct drm_file *file_priv)
5288 struct drm_mode_create_dumb *args = data;
5289 u32 cpp, stride, size;
5291 if (!dev->driver->dumb_create)
5293 if (!args->width || !args->height || !args->bpp)
5296 /* overflow checks for 32bit size calculations */
5297 /* NOTE: DIV_ROUND_UP() can overflow */
5298 cpp = DIV_ROUND_UP(args->bpp, 8);
5299 if (!cpp || cpp > 0xffffffffU / args->width)
5301 stride = cpp * args->width;
5302 if (args->height > 0xffffffffU / stride)
5305 /* test for wrap-around */
5306 size = args->height * stride;
5307 if (PAGE_ALIGN(size) == 0)
5311 * handle, pitch and size are output parameters. Zero them out to
5312 * prevent drivers from accidentally using uninitialized data. Since
5313 * not all existing userspace is clearing these fields properly we
5314 * cannot reject IOCTL with garbage in them.
5320 return dev->driver->dumb_create(file_priv, dev, args);
5324 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5327 * @file_priv: DRM file info
5329 * Allocate an offset in the drm device node's address space to be able to
5330 * memory map a dumb buffer.
5332 * Called by the user via ioctl.
5335 * Zero on success, negative errno on failure.
5337 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5338 void *data, struct drm_file *file_priv)
5340 struct drm_mode_map_dumb *args = data;
5342 /* call driver ioctl to get mmap offset */
5343 if (!dev->driver->dumb_map_offset)
5346 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5350 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5353 * @file_priv: DRM file info
5355 * This destroys the userspace handle for the given dumb backing storage buffer.
5356 * Since buffer objects must be reference counted in the kernel a buffer object
5357 * won't be immediately freed if a framebuffer modeset object still uses it.
5359 * Called by the user via ioctl.
5362 * Zero on success, negative errno on failure.
5364 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5365 void *data, struct drm_file *file_priv)
5367 struct drm_mode_destroy_dumb *args = data;
5369 if (!dev->driver->dumb_destroy)
5372 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5376 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5377 * @format: pixel format (DRM_FORMAT_*)
5378 * @depth: storage for the depth value
5379 * @bpp: storage for the bpp value
5381 * This only supports RGB formats here for compat with code that doesn't use
5382 * pixel formats directly yet.
5384 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5389 case DRM_FORMAT_RGB332:
5390 case DRM_FORMAT_BGR233:
5394 case DRM_FORMAT_XRGB1555:
5395 case DRM_FORMAT_XBGR1555:
5396 case DRM_FORMAT_RGBX5551:
5397 case DRM_FORMAT_BGRX5551:
5398 case DRM_FORMAT_ARGB1555:
5399 case DRM_FORMAT_ABGR1555:
5400 case DRM_FORMAT_RGBA5551:
5401 case DRM_FORMAT_BGRA5551:
5405 case DRM_FORMAT_RGB565:
5406 case DRM_FORMAT_BGR565:
5410 case DRM_FORMAT_RGB888:
5411 case DRM_FORMAT_BGR888:
5415 case DRM_FORMAT_XRGB8888:
5416 case DRM_FORMAT_XBGR8888:
5417 case DRM_FORMAT_RGBX8888:
5418 case DRM_FORMAT_BGRX8888:
5422 case DRM_FORMAT_XRGB2101010:
5423 case DRM_FORMAT_XBGR2101010:
5424 case DRM_FORMAT_RGBX1010102:
5425 case DRM_FORMAT_BGRX1010102:
5426 case DRM_FORMAT_ARGB2101010:
5427 case DRM_FORMAT_ABGR2101010:
5428 case DRM_FORMAT_RGBA1010102:
5429 case DRM_FORMAT_BGRA1010102:
5433 case DRM_FORMAT_ARGB8888:
5434 case DRM_FORMAT_ABGR8888:
5435 case DRM_FORMAT_RGBA8888:
5436 case DRM_FORMAT_BGRA8888:
5441 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5442 drm_get_format_name(format));
5448 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5451 * drm_format_num_planes - get the number of planes for format
5452 * @format: pixel format (DRM_FORMAT_*)
5455 * The number of planes used by the specified pixel format.
5457 int drm_format_num_planes(uint32_t format)
5460 case DRM_FORMAT_YUV410:
5461 case DRM_FORMAT_YVU410:
5462 case DRM_FORMAT_YUV411:
5463 case DRM_FORMAT_YVU411:
5464 case DRM_FORMAT_YUV420:
5465 case DRM_FORMAT_YVU420:
5466 case DRM_FORMAT_YUV422:
5467 case DRM_FORMAT_YVU422:
5468 case DRM_FORMAT_YUV444:
5469 case DRM_FORMAT_YVU444:
5471 case DRM_FORMAT_NV12:
5472 case DRM_FORMAT_NV21:
5473 case DRM_FORMAT_NV16:
5474 case DRM_FORMAT_NV61:
5475 case DRM_FORMAT_NV24:
5476 case DRM_FORMAT_NV42:
5482 EXPORT_SYMBOL(drm_format_num_planes);
5485 * drm_format_plane_cpp - determine the bytes per pixel value
5486 * @format: pixel format (DRM_FORMAT_*)
5487 * @plane: plane index
5490 * The bytes per pixel value for the specified plane.
5492 int drm_format_plane_cpp(uint32_t format, int plane)
5497 if (plane >= drm_format_num_planes(format))
5501 case DRM_FORMAT_YUYV:
5502 case DRM_FORMAT_YVYU:
5503 case DRM_FORMAT_UYVY:
5504 case DRM_FORMAT_VYUY:
5506 case DRM_FORMAT_NV12:
5507 case DRM_FORMAT_NV21:
5508 case DRM_FORMAT_NV16:
5509 case DRM_FORMAT_NV61:
5510 case DRM_FORMAT_NV24:
5511 case DRM_FORMAT_NV42:
5512 return plane ? 2 : 1;
5513 case DRM_FORMAT_YUV410:
5514 case DRM_FORMAT_YVU410:
5515 case DRM_FORMAT_YUV411:
5516 case DRM_FORMAT_YVU411:
5517 case DRM_FORMAT_YUV420:
5518 case DRM_FORMAT_YVU420:
5519 case DRM_FORMAT_YUV422:
5520 case DRM_FORMAT_YVU422:
5521 case DRM_FORMAT_YUV444:
5522 case DRM_FORMAT_YVU444:
5525 drm_fb_get_bpp_depth(format, &depth, &bpp);
5529 EXPORT_SYMBOL(drm_format_plane_cpp);
5532 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5533 * @format: pixel format (DRM_FORMAT_*)
5536 * The horizontal chroma subsampling factor for the
5537 * specified pixel format.
5539 int drm_format_horz_chroma_subsampling(uint32_t format)
5542 case DRM_FORMAT_YUV411:
5543 case DRM_FORMAT_YVU411:
5544 case DRM_FORMAT_YUV410:
5545 case DRM_FORMAT_YVU410:
5547 case DRM_FORMAT_YUYV:
5548 case DRM_FORMAT_YVYU:
5549 case DRM_FORMAT_UYVY:
5550 case DRM_FORMAT_VYUY:
5551 case DRM_FORMAT_NV12:
5552 case DRM_FORMAT_NV21:
5553 case DRM_FORMAT_NV16:
5554 case DRM_FORMAT_NV61:
5555 case DRM_FORMAT_YUV422:
5556 case DRM_FORMAT_YVU422:
5557 case DRM_FORMAT_YUV420:
5558 case DRM_FORMAT_YVU420:
5564 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5567 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5568 * @format: pixel format (DRM_FORMAT_*)
5571 * The vertical chroma subsampling factor for the
5572 * specified pixel format.
5574 int drm_format_vert_chroma_subsampling(uint32_t format)
5577 case DRM_FORMAT_YUV410:
5578 case DRM_FORMAT_YVU410:
5580 case DRM_FORMAT_YUV420:
5581 case DRM_FORMAT_YVU420:
5582 case DRM_FORMAT_NV12:
5583 case DRM_FORMAT_NV21:
5589 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5592 * drm_rotation_simplify() - Try to simplify the rotation
5593 * @rotation: Rotation to be simplified
5594 * @supported_rotations: Supported rotations
5596 * Attempt to simplify the rotation to a form that is supported.
5597 * Eg. if the hardware supports everything except DRM_REFLECT_X
5598 * one could call this function like this:
5600 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5601 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5602 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5604 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5605 * transforms the hardware supports, this function may not
5606 * be able to produce a supported transform, so the caller should
5607 * check the result afterwards.
5609 unsigned int drm_rotation_simplify(unsigned int rotation,
5610 unsigned int supported_rotations)
5612 if (rotation & ~supported_rotations) {
5613 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5614 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5619 EXPORT_SYMBOL(drm_rotation_simplify);
5622 * drm_mode_config_init - initialize DRM mode_configuration structure
5625 * Initialize @dev's mode_config structure, used for tracking the graphics
5626 * configuration of @dev.
5628 * Since this initializes the modeset locks, no locking is possible. Which is no
5629 * problem, since this should happen single threaded at init time. It is the
5630 * driver's problem to ensure this guarantee.
5633 void drm_mode_config_init(struct drm_device *dev)
5635 mutex_init(&dev->mode_config.mutex);
5636 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5637 mutex_init(&dev->mode_config.idr_mutex);
5638 mutex_init(&dev->mode_config.fb_lock);
5639 mutex_init(&dev->mode_config.blob_lock);
5640 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5641 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5642 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5643 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5644 INIT_LIST_HEAD(&dev->mode_config.property_list);
5645 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5646 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5647 idr_init(&dev->mode_config.crtc_idr);
5648 idr_init(&dev->mode_config.tile_idr);
5650 drm_modeset_lock_all(dev);
5651 drm_mode_create_standard_properties(dev);
5652 drm_modeset_unlock_all(dev);
5654 /* Just to be sure */
5655 dev->mode_config.num_fb = 0;
5656 dev->mode_config.num_connector = 0;
5657 dev->mode_config.num_crtc = 0;
5658 dev->mode_config.num_encoder = 0;
5659 dev->mode_config.num_overlay_plane = 0;
5660 dev->mode_config.num_total_plane = 0;
5662 EXPORT_SYMBOL(drm_mode_config_init);
5665 * drm_mode_config_cleanup - free up DRM mode_config info
5668 * Free up all the connectors and CRTCs associated with this DRM device, then
5669 * free up the framebuffers and associated buffer objects.
5671 * Note that since this /should/ happen single-threaded at driver/device
5672 * teardown time, no locking is required. It's the driver's job to ensure that
5673 * this guarantee actually holds true.
5675 * FIXME: cleanup any dangling user buffer objects too
5677 void drm_mode_config_cleanup(struct drm_device *dev)
5679 struct drm_connector *connector, *ot;
5680 struct drm_crtc *crtc, *ct;
5681 struct drm_encoder *encoder, *enct;
5682 struct drm_framebuffer *fb, *fbt;
5683 struct drm_property *property, *pt;
5684 struct drm_property_blob *blob, *bt;
5685 struct drm_plane *plane, *plt;
5687 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5689 encoder->funcs->destroy(encoder);
5692 list_for_each_entry_safe(connector, ot,
5693 &dev->mode_config.connector_list, head) {
5694 connector->funcs->destroy(connector);
5697 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5699 drm_property_destroy(dev, property);
5702 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5704 drm_property_unreference_blob(blob);
5708 * Single-threaded teardown context, so it's not required to grab the
5709 * fb_lock to protect against concurrent fb_list access. Contrary, it
5710 * would actually deadlock with the drm_framebuffer_cleanup function.
5712 * Also, if there are any framebuffers left, that's a driver leak now,
5713 * so politely WARN about this.
5715 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5716 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5717 drm_framebuffer_remove(fb);
5720 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5722 plane->funcs->destroy(plane);
5725 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5726 crtc->funcs->destroy(crtc);
5729 idr_destroy(&dev->mode_config.tile_idr);
5730 idr_destroy(&dev->mode_config.crtc_idr);
5731 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5733 EXPORT_SYMBOL(drm_mode_config_cleanup);
5735 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5736 unsigned int supported_rotations)
5738 static const struct drm_prop_enum_list props[] = {
5739 { DRM_ROTATE_0, "rotate-0" },
5740 { DRM_ROTATE_90, "rotate-90" },
5741 { DRM_ROTATE_180, "rotate-180" },
5742 { DRM_ROTATE_270, "rotate-270" },
5743 { DRM_REFLECT_X, "reflect-x" },
5744 { DRM_REFLECT_Y, "reflect-y" },
5747 return drm_property_create_bitmask(dev, 0, "rotation",
5748 props, ARRAY_SIZE(props),
5749 supported_rotations);
5751 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5756 * Tile groups are used to represent tiled monitors with a unique
5757 * integer identifier. Tiled monitors using DisplayID v1.3 have
5758 * a unique 8-byte handle, we store this in a tile group, so we
5759 * have a common identifier for all tiles in a monitor group.
5761 static void drm_tile_group_free(struct kref *kref)
5763 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5764 struct drm_device *dev = tg->dev;
5765 mutex_lock(&dev->mode_config.idr_mutex);
5766 idr_remove(&dev->mode_config.tile_idr, tg->id);
5767 mutex_unlock(&dev->mode_config.idr_mutex);
5772 * drm_mode_put_tile_group - drop a reference to a tile group.
5774 * @tg: tile group to drop reference to.
5776 * drop reference to tile group and free if 0.
5778 void drm_mode_put_tile_group(struct drm_device *dev,
5779 struct drm_tile_group *tg)
5781 kref_put(&tg->refcount, drm_tile_group_free);
5785 * drm_mode_get_tile_group - get a reference to an existing tile group
5787 * @topology: 8-bytes unique per monitor.
5789 * Use the unique bytes to get a reference to an existing tile group.
5792 * tile group or NULL if not found.
5794 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5797 struct drm_tile_group *tg;
5799 mutex_lock(&dev->mode_config.idr_mutex);
5800 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5801 if (!memcmp(tg->group_data, topology, 8)) {
5802 if (!kref_get_unless_zero(&tg->refcount))
5804 mutex_unlock(&dev->mode_config.idr_mutex);
5808 mutex_unlock(&dev->mode_config.idr_mutex);
5811 EXPORT_SYMBOL(drm_mode_get_tile_group);
5814 * drm_mode_create_tile_group - create a tile group from a displayid description
5816 * @topology: 8-bytes unique per monitor.
5818 * Create a tile group for the unique monitor, and get a unique
5819 * identifier for the tile group.
5822 * new tile group or error.
5824 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5827 struct drm_tile_group *tg;
5830 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5832 return ERR_PTR(-ENOMEM);
5834 kref_init(&tg->refcount);
5835 memcpy(tg->group_data, topology, 8);
5838 mutex_lock(&dev->mode_config.idr_mutex);
5839 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5847 mutex_unlock(&dev->mode_config.idr_mutex);
5850 EXPORT_SYMBOL(drm_mode_create_tile_group);