2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
43 #include "drm_crtc_internal.h"
44 #include "drm_internal.h"
46 static struct drm_framebuffer *
47 internal_framebuffer_create(struct drm_device *dev,
48 const struct drm_mode_fb_cmd2 *r,
49 struct drm_file *file_priv);
51 /* Avoid boilerplate. I'm tired of typing. */
52 #define DRM_ENUM_NAME_FN(fnname, list) \
53 const char *fnname(int val) \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
66 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
68 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
73 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
76 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
84 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
85 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
91 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
98 * Non-global properties, but "required" for certain connectors.
100 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
114 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
117 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
135 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
138 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
144 struct drm_conn_prop_enum_list {
151 * Connector and encoder types.
153 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
171 { DRM_MODE_CONNECTOR_DPI, "DPI" },
174 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
175 { DRM_MODE_ENCODER_NONE, "None" },
176 { DRM_MODE_ENCODER_DAC, "DAC" },
177 { DRM_MODE_ENCODER_TMDS, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC, "TV" },
180 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
181 { DRM_MODE_ENCODER_DSI, "DSI" },
182 { DRM_MODE_ENCODER_DPMST, "DP MST" },
183 { DRM_MODE_ENCODER_DPI, "DPI" },
186 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
187 { SubPixelUnknown, "Unknown" },
188 { SubPixelHorizontalRGB, "Horizontal RGB" },
189 { SubPixelHorizontalBGR, "Horizontal BGR" },
190 { SubPixelVerticalRGB, "Vertical RGB" },
191 { SubPixelVerticalBGR, "Vertical BGR" },
192 { SubPixelNone, "None" },
195 void drm_connector_ida_init(void)
199 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
200 ida_init(&drm_connector_enum_list[i].ida);
203 void drm_connector_ida_destroy(void)
207 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
208 ida_destroy(&drm_connector_enum_list[i].ida);
212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
218 const char *drm_get_connector_status_name(enum drm_connector_status status)
220 if (status == connector_status_connected)
222 else if (status == connector_status_disconnected)
223 return "disconnected";
227 EXPORT_SYMBOL(drm_get_connector_status_name);
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
236 const char *drm_get_subpixel_order_name(enum subpixel_order order)
238 return drm_subpixel_enum_list[order].name;
240 EXPORT_SYMBOL(drm_get_subpixel_order_name);
242 static char printable_char(int c)
244 return isascii(c) && isprint(c) ? c : '?';
248 * drm_get_format_name - return a string for drm fourcc format
249 * @format: format to compute name of
251 * Note that the buffer used by this function is globally shared and owned by
252 * the function itself.
254 * FIXME: This isn't really multithreading safe.
256 const char *drm_get_format_name(uint32_t format)
260 snprintf(buf, sizeof(buf),
261 "%c%c%c%c %s-endian (0x%08x)",
262 printable_char(format & 0xff),
263 printable_char((format >> 8) & 0xff),
264 printable_char((format >> 16) & 0xff),
265 printable_char((format >> 24) & 0x7f),
266 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
271 EXPORT_SYMBOL(drm_get_format_name);
274 * Internal function to assign a slot in the object idr and optionally
275 * register the object into the idr.
277 static int drm_mode_object_get_reg(struct drm_device *dev,
278 struct drm_mode_object *obj,
281 void (*obj_free_cb)(struct kref *kref))
285 mutex_lock(&dev->mode_config.idr_mutex);
286 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
289 * Set up the object linking under the protection of the idr
290 * lock so that other users can't see inconsistent state.
293 obj->type = obj_type;
295 obj->free_cb = obj_free_cb;
296 kref_init(&obj->refcount);
299 mutex_unlock(&dev->mode_config.idr_mutex);
301 return ret < 0 ? ret : 0;
305 * drm_mode_object_get - allocate a new modeset identifier
307 * @obj: object pointer, used to generate unique ID
308 * @obj_type: object type
310 * Create a unique identifier based on @ptr in @dev's identifier space. Used
311 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
312 * modeset identifiers are _not_ reference counted. Hence don't use this for
313 * reference counted modeset objects like framebuffers.
316 * Zero on success, error code on failure.
318 int drm_mode_object_get(struct drm_device *dev,
319 struct drm_mode_object *obj, uint32_t obj_type)
321 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
324 static void drm_mode_object_register(struct drm_device *dev,
325 struct drm_mode_object *obj)
327 mutex_lock(&dev->mode_config.idr_mutex);
328 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
329 mutex_unlock(&dev->mode_config.idr_mutex);
333 * drm_mode_object_unregister - free a modeset identifer
335 * @object: object to free
337 * Free @id from @dev's unique identifier pool.
338 * This function can be called multiple times, and guards against
340 * These modeset identifiers are _not_ reference counted. Hence don't use this
341 * for reference counted modeset objects like framebuffers.
343 void drm_mode_object_unregister(struct drm_device *dev,
344 struct drm_mode_object *object)
346 mutex_lock(&dev->mode_config.idr_mutex);
348 idr_remove(&dev->mode_config.crtc_idr, object->id);
351 mutex_unlock(&dev->mode_config.idr_mutex);
354 static struct drm_mode_object *_object_find(struct drm_device *dev,
355 uint32_t id, uint32_t type)
357 struct drm_mode_object *obj = NULL;
359 mutex_lock(&dev->mode_config.idr_mutex);
360 obj = idr_find(&dev->mode_config.crtc_idr, id);
361 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
363 if (obj && obj->id != id)
366 if (obj && obj->free_cb) {
367 if (!kref_get_unless_zero(&obj->refcount))
370 mutex_unlock(&dev->mode_config.idr_mutex);
376 * drm_mode_object_find - look up a drm object with static lifetime
378 * @id: id of the mode object
379 * @type: type of the mode object
381 * This function is used to look up a modeset object. It will acquire a
382 * reference for reference counted objects. This reference must be dropped again
383 * by callind drm_mode_object_unreference().
385 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
386 uint32_t id, uint32_t type)
388 struct drm_mode_object *obj = NULL;
390 obj = _object_find(dev, id, type);
393 EXPORT_SYMBOL(drm_mode_object_find);
396 * drm_mode_object_unreference - decr the object refcnt
399 * This functions decrements the object's refcount if it is a refcounted modeset
400 * object. It is a no-op on any other object. This is used to drop references
401 * acquired with drm_mode_object_reference().
403 void drm_mode_object_unreference(struct drm_mode_object *obj)
406 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
407 kref_put(&obj->refcount, obj->free_cb);
410 EXPORT_SYMBOL(drm_mode_object_unreference);
413 * drm_mode_object_reference - incr the object refcnt
416 * This functions increments the object's refcount if it is a refcounted modeset
417 * object. It is a no-op on any other object. References should be dropped again
418 * by calling drm_mode_object_unreference().
420 void drm_mode_object_reference(struct drm_mode_object *obj)
423 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
424 kref_get(&obj->refcount);
427 EXPORT_SYMBOL(drm_mode_object_reference);
429 static void drm_framebuffer_free(struct kref *kref)
431 struct drm_framebuffer *fb =
432 container_of(kref, struct drm_framebuffer, base.refcount);
433 struct drm_device *dev = fb->dev;
436 * The lookup idr holds a weak reference, which has not necessarily been
437 * removed at this point. Check for that.
439 drm_mode_object_unregister(dev, &fb->base);
441 fb->funcs->destroy(fb);
445 * drm_framebuffer_init - initialize a framebuffer
447 * @fb: framebuffer to be initialized
448 * @funcs: ... with these functions
450 * Allocates an ID for the framebuffer's parent mode object, sets its mode
451 * functions & device file and adds it to the master fd list.
454 * This functions publishes the fb and makes it available for concurrent access
455 * by other users. Which means by this point the fb _must_ be fully set up -
456 * since all the fb attributes are invariant over its lifetime, no further
457 * locking but only correct reference counting is required.
460 * Zero on success, error code on failure.
462 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
463 const struct drm_framebuffer_funcs *funcs)
467 INIT_LIST_HEAD(&fb->filp_head);
471 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
472 false, drm_framebuffer_free);
476 mutex_lock(&dev->mode_config.fb_lock);
477 dev->mode_config.num_fb++;
478 list_add(&fb->head, &dev->mode_config.fb_list);
479 mutex_unlock(&dev->mode_config.fb_lock);
481 drm_mode_object_register(dev, &fb->base);
485 EXPORT_SYMBOL(drm_framebuffer_init);
488 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
490 * @id: id of the fb object
492 * If successful, this grabs an additional reference to the framebuffer -
493 * callers need to make sure to eventually unreference the returned framebuffer
494 * again, using @drm_framebuffer_unreference.
496 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
499 struct drm_mode_object *obj;
500 struct drm_framebuffer *fb = NULL;
502 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
507 EXPORT_SYMBOL(drm_framebuffer_lookup);
510 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
511 * @fb: fb to unregister
513 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
514 * those used for fbdev. Note that the caller must hold a reference of it's own,
515 * i.e. the object may not be destroyed through this call (since it'll lead to a
516 * locking inversion).
518 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
520 struct drm_device *dev;
527 /* Mark fb as reaped and drop idr ref. */
528 drm_mode_object_unregister(dev, &fb->base);
530 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
533 * drm_framebuffer_cleanup - remove a framebuffer object
534 * @fb: framebuffer to remove
536 * Cleanup framebuffer. This function is intended to be used from the drivers
537 * ->destroy callback. It can also be used to clean up driver private
538 * framebuffers embedded into a larger structure.
540 * Note that this function does not remove the fb from active usuage - if it is
541 * still used anywhere, hilarity can ensue since userspace could call getfb on
542 * the id and get back -EINVAL. Obviously no concern at driver unload time.
544 * Also, the framebuffer will not be removed from the lookup idr - for
545 * user-created framebuffers this will happen in in the rmfb ioctl. For
546 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
547 * drm_framebuffer_unregister_private.
549 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
551 struct drm_device *dev = fb->dev;
553 mutex_lock(&dev->mode_config.fb_lock);
555 dev->mode_config.num_fb--;
556 mutex_unlock(&dev->mode_config.fb_lock);
558 EXPORT_SYMBOL(drm_framebuffer_cleanup);
561 * drm_framebuffer_remove - remove and unreference a framebuffer object
562 * @fb: framebuffer to remove
564 * Scans all the CRTCs and planes in @dev's mode_config. If they're
565 * using @fb, removes it, setting it to NULL. Then drops the reference to the
566 * passed-in framebuffer. Might take the modeset locks.
568 * Note that this function optimizes the cleanup away if the caller holds the
569 * last reference to the framebuffer. It is also guaranteed to not take the
570 * modeset locks in this case.
572 void drm_framebuffer_remove(struct drm_framebuffer *fb)
574 struct drm_device *dev;
575 struct drm_crtc *crtc;
576 struct drm_plane *plane;
577 struct drm_mode_set set;
585 WARN_ON(!list_empty(&fb->filp_head));
588 * drm ABI mandates that we remove any deleted framebuffers from active
589 * useage. But since most sane clients only remove framebuffers they no
590 * longer need, try to optimize this away.
592 * Since we're holding a reference ourselves, observing a refcount of 1
593 * means that we're the last holder and can skip it. Also, the refcount
594 * can never increase from 1 again, so we don't need any barriers or
597 * Note that userspace could try to race with use and instate a new
598 * usage _after_ we've cleared all current ones. End result will be an
599 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
602 if (drm_framebuffer_read_refcount(fb) > 1) {
603 drm_modeset_lock_all(dev);
604 /* remove from any CRTC */
605 drm_for_each_crtc(crtc, dev) {
606 if (crtc->primary->fb == fb) {
607 /* should turn off the crtc */
608 memset(&set, 0, sizeof(struct drm_mode_set));
611 ret = drm_mode_set_config_internal(&set);
613 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
617 drm_for_each_plane(plane, dev) {
619 drm_plane_force_disable(plane);
621 drm_modeset_unlock_all(dev);
624 drm_framebuffer_unreference(fb);
626 EXPORT_SYMBOL(drm_framebuffer_remove);
628 DEFINE_WW_CLASS(crtc_ww_class);
630 static unsigned int drm_num_crtcs(struct drm_device *dev)
632 unsigned int num = 0;
633 struct drm_crtc *tmp;
635 drm_for_each_crtc(tmp, dev) {
643 * drm_crtc_init_with_planes - Initialise a new CRTC object with
644 * specified primary and cursor planes.
646 * @crtc: CRTC object to init
647 * @primary: Primary plane for CRTC
648 * @cursor: Cursor plane for CRTC
649 * @funcs: callbacks for the new CRTC
650 * @name: printf style format string for the CRTC name, or NULL for default name
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,
661 const char *name, ...)
663 struct drm_mode_config *config = &dev->mode_config;
666 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
667 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
672 drm_modeset_lock_init(&crtc->mutex);
673 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
681 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
684 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
688 drm_mode_object_unregister(dev, &crtc->base);
692 crtc->base.properties = &crtc->properties;
694 list_add_tail(&crtc->head, &config->crtc_list);
695 crtc->index = config->num_crtc++;
697 crtc->primary = primary;
698 crtc->cursor = cursor;
700 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
702 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
704 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
705 drm_object_attach_property(&crtc->base, config->prop_active, 0);
706 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
711 EXPORT_SYMBOL(drm_crtc_init_with_planes);
714 * drm_crtc_cleanup - Clean up the core crtc usage
715 * @crtc: CRTC to cleanup
717 * This function cleans up @crtc and removes it from the DRM mode setting
718 * core. Note that the function does *not* free the crtc structure itself,
719 * this is the responsibility of the caller.
721 void drm_crtc_cleanup(struct drm_crtc *crtc)
723 struct drm_device *dev = crtc->dev;
725 /* Note that the crtc_list is considered to be static; should we
726 * remove the drm_crtc at runtime we would have to decrement all
727 * the indices on the drm_crtc after us in the crtc_list.
730 kfree(crtc->gamma_store);
731 crtc->gamma_store = NULL;
733 drm_modeset_lock_fini(&crtc->mutex);
735 drm_mode_object_unregister(dev, &crtc->base);
736 list_del(&crtc->head);
737 dev->mode_config.num_crtc--;
739 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
740 if (crtc->state && crtc->funcs->atomic_destroy_state)
741 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
745 memset(crtc, 0, sizeof(*crtc));
747 EXPORT_SYMBOL(drm_crtc_cleanup);
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" : "");
849 static void drm_connector_free(struct kref *kref)
851 struct drm_connector *connector =
852 container_of(kref, struct drm_connector, base.refcount);
853 struct drm_device *dev = connector->dev;
855 drm_mode_object_unregister(dev, &connector->base);
856 connector->funcs->destroy(connector);
860 * drm_connector_init - Init a preallocated connector
862 * @connector: the connector to init
863 * @funcs: callbacks for this connector
864 * @connector_type: user visible type of the connector
866 * Initialises a preallocated connector. Connectors should be
867 * subclassed as part of driver connector objects.
870 * Zero on success, error code on failure.
872 int drm_connector_init(struct drm_device *dev,
873 struct drm_connector *connector,
874 const struct drm_connector_funcs *funcs,
877 struct drm_mode_config *config = &dev->mode_config;
879 struct ida *connector_ida =
880 &drm_connector_enum_list[connector_type].ida;
882 drm_modeset_lock_all(dev);
884 ret = drm_mode_object_get_reg(dev, &connector->base,
885 DRM_MODE_OBJECT_CONNECTOR,
886 false, drm_connector_free);
890 connector->base.properties = &connector->properties;
891 connector->dev = dev;
892 connector->funcs = funcs;
894 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
895 if (connector->connector_id < 0) {
896 ret = connector->connector_id;
900 connector->connector_type = connector_type;
901 connector->connector_type_id =
902 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
903 if (connector->connector_type_id < 0) {
904 ret = connector->connector_type_id;
908 kasprintf(GFP_KERNEL, "%s-%d",
909 drm_connector_enum_list[connector_type].name,
910 connector->connector_type_id);
911 if (!connector->name) {
913 goto out_put_type_id;
916 INIT_LIST_HEAD(&connector->probed_modes);
917 INIT_LIST_HEAD(&connector->modes);
918 connector->edid_blob_ptr = NULL;
919 connector->status = connector_status_unknown;
921 drm_connector_get_cmdline_mode(connector);
923 /* We should add connectors at the end to avoid upsetting the connector
925 list_add_tail(&connector->head, &config->connector_list);
926 config->num_connector++;
928 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
929 drm_object_attach_property(&connector->base,
930 config->edid_property,
933 drm_object_attach_property(&connector->base,
934 config->dpms_property, 0);
936 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
937 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
940 connector->debugfs_entry = NULL;
943 ida_remove(connector_ida, connector->connector_type_id);
946 ida_remove(&config->connector_ida, connector->connector_id);
949 drm_mode_object_unregister(dev, &connector->base);
952 drm_modeset_unlock_all(dev);
956 EXPORT_SYMBOL(drm_connector_init);
959 * drm_connector_cleanup - cleans up an initialised connector
960 * @connector: connector to cleanup
962 * Cleans up the connector but doesn't free the object.
964 void drm_connector_cleanup(struct drm_connector *connector)
966 struct drm_device *dev = connector->dev;
967 struct drm_display_mode *mode, *t;
969 if (connector->tile_group) {
970 drm_mode_put_tile_group(dev, connector->tile_group);
971 connector->tile_group = NULL;
974 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
975 drm_mode_remove(connector, mode);
977 list_for_each_entry_safe(mode, t, &connector->modes, head)
978 drm_mode_remove(connector, mode);
980 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
981 connector->connector_type_id);
983 ida_remove(&dev->mode_config.connector_ida,
984 connector->connector_id);
986 kfree(connector->display_info.bus_formats);
987 drm_mode_object_unregister(dev, &connector->base);
988 kfree(connector->name);
989 connector->name = NULL;
990 list_del(&connector->head);
991 dev->mode_config.num_connector--;
993 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
994 if (connector->state && connector->funcs->atomic_destroy_state)
995 connector->funcs->atomic_destroy_state(connector,
998 memset(connector, 0, sizeof(*connector));
1000 EXPORT_SYMBOL(drm_connector_cleanup);
1003 * drm_connector_register - register a connector
1004 * @connector: the connector to register
1006 * Register userspace interfaces for a connector
1009 * Zero on success, error code on failure.
1011 int drm_connector_register(struct drm_connector *connector)
1015 ret = drm_sysfs_connector_add(connector);
1019 ret = drm_debugfs_connector_add(connector);
1021 drm_sysfs_connector_remove(connector);
1025 drm_mode_object_register(connector->dev, &connector->base);
1029 EXPORT_SYMBOL(drm_connector_register);
1032 * drm_connector_unregister - unregister a connector
1033 * @connector: the connector to unregister
1035 * Unregister userspace interfaces for a connector
1037 void drm_connector_unregister(struct drm_connector *connector)
1039 drm_sysfs_connector_remove(connector);
1040 drm_debugfs_connector_remove(connector);
1042 EXPORT_SYMBOL(drm_connector_unregister);
1045 * drm_connector_register_all - register all connectors
1048 * This function registers all connectors in sysfs and other places so that
1049 * userspace can start to access them. Drivers can call it after calling
1050 * drm_dev_register() to complete the device registration, if they don't call
1051 * drm_connector_register() on each connector individually.
1053 * When a device is unplugged and should be removed from userspace access,
1054 * call drm_connector_unregister_all(), which is the inverse of this
1058 * Zero on success, error code on failure.
1060 int drm_connector_register_all(struct drm_device *dev)
1062 struct drm_connector *connector;
1065 mutex_lock(&dev->mode_config.mutex);
1067 drm_for_each_connector(connector, dev) {
1068 ret = drm_connector_register(connector);
1073 mutex_unlock(&dev->mode_config.mutex);
1078 mutex_unlock(&dev->mode_config.mutex);
1079 drm_connector_unregister_all(dev);
1082 EXPORT_SYMBOL(drm_connector_register_all);
1085 * drm_connector_unregister_all - unregister connector userspace interfaces
1088 * This functions unregisters all connectors from sysfs and other places so
1089 * that userspace can no longer access them. Drivers should call this as the
1090 * first step tearing down the device instace, or when the underlying
1091 * physical device disappeared (e.g. USB unplug), right before calling
1092 * drm_dev_unregister().
1094 void drm_connector_unregister_all(struct drm_device *dev)
1096 struct drm_connector *connector;
1098 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1099 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1100 drm_connector_unregister(connector);
1102 EXPORT_SYMBOL(drm_connector_unregister_all);
1105 * drm_encoder_init - Init a preallocated encoder
1107 * @encoder: the encoder to init
1108 * @funcs: callbacks for this encoder
1109 * @encoder_type: user visible type of the encoder
1110 * @name: printf style format string for the encoder name, or NULL for default name
1112 * Initialises a preallocated encoder. Encoder should be
1113 * subclassed as part of driver encoder objects.
1116 * Zero on success, error code on failure.
1118 int drm_encoder_init(struct drm_device *dev,
1119 struct drm_encoder *encoder,
1120 const struct drm_encoder_funcs *funcs,
1121 int encoder_type, const char *name, ...)
1125 drm_modeset_lock_all(dev);
1127 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1132 encoder->encoder_type = encoder_type;
1133 encoder->funcs = funcs;
1138 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1141 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1142 drm_encoder_enum_list[encoder_type].name,
1145 if (!encoder->name) {
1150 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1151 encoder->index = dev->mode_config.num_encoder++;
1155 drm_mode_object_unregister(dev, &encoder->base);
1158 drm_modeset_unlock_all(dev);
1162 EXPORT_SYMBOL(drm_encoder_init);
1165 * drm_encoder_cleanup - cleans up an initialised encoder
1166 * @encoder: encoder to cleanup
1168 * Cleans up the encoder but doesn't free the object.
1170 void drm_encoder_cleanup(struct drm_encoder *encoder)
1172 struct drm_device *dev = encoder->dev;
1174 /* Note that the encoder_list is considered to be static; should we
1175 * remove the drm_encoder at runtime we would have to decrement all
1176 * the indices on the drm_encoder after us in the encoder_list.
1179 drm_modeset_lock_all(dev);
1180 drm_mode_object_unregister(dev, &encoder->base);
1181 kfree(encoder->name);
1182 list_del(&encoder->head);
1183 dev->mode_config.num_encoder--;
1184 drm_modeset_unlock_all(dev);
1186 memset(encoder, 0, sizeof(*encoder));
1188 EXPORT_SYMBOL(drm_encoder_cleanup);
1190 static unsigned int drm_num_planes(struct drm_device *dev)
1192 unsigned int num = 0;
1193 struct drm_plane *tmp;
1195 drm_for_each_plane(tmp, dev) {
1203 * drm_universal_plane_init - Initialize a new universal plane object
1205 * @plane: plane object to init
1206 * @possible_crtcs: bitmask of possible CRTCs
1207 * @funcs: callbacks for the new plane
1208 * @formats: array of supported formats (%DRM_FORMAT_*)
1209 * @format_count: number of elements in @formats
1210 * @type: type of plane (overlay, primary, cursor)
1211 * @name: printf style format string for the plane name, or NULL for default name
1213 * Initializes a plane object of type @type.
1216 * Zero on success, error code on failure.
1218 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1219 unsigned long possible_crtcs,
1220 const struct drm_plane_funcs *funcs,
1221 const uint32_t *formats, unsigned int format_count,
1222 enum drm_plane_type type,
1223 const char *name, ...)
1225 struct drm_mode_config *config = &dev->mode_config;
1228 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1232 drm_modeset_lock_init(&plane->mutex);
1234 plane->base.properties = &plane->properties;
1236 plane->funcs = funcs;
1237 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1239 if (!plane->format_types) {
1240 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1241 drm_mode_object_unregister(dev, &plane->base);
1249 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1252 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1253 drm_num_planes(dev));
1256 kfree(plane->format_types);
1257 drm_mode_object_unregister(dev, &plane->base);
1261 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1262 plane->format_count = format_count;
1263 plane->possible_crtcs = possible_crtcs;
1266 list_add_tail(&plane->head, &config->plane_list);
1267 plane->index = config->num_total_plane++;
1268 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1269 config->num_overlay_plane++;
1271 drm_object_attach_property(&plane->base,
1272 config->plane_type_property,
1275 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1276 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1277 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1278 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1279 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1280 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1281 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1282 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1283 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1284 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1285 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1290 EXPORT_SYMBOL(drm_universal_plane_init);
1293 * drm_plane_init - Initialize a legacy plane
1295 * @plane: plane object to init
1296 * @possible_crtcs: bitmask of possible CRTCs
1297 * @funcs: callbacks for the new plane
1298 * @formats: array of supported formats (%DRM_FORMAT_*)
1299 * @format_count: number of elements in @formats
1300 * @is_primary: plane type (primary vs overlay)
1302 * Legacy API to initialize a DRM plane.
1304 * New drivers should call drm_universal_plane_init() instead.
1307 * Zero on success, error code on failure.
1309 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1310 unsigned long possible_crtcs,
1311 const struct drm_plane_funcs *funcs,
1312 const uint32_t *formats, unsigned int format_count,
1315 enum drm_plane_type type;
1317 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1318 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1319 formats, format_count, type, NULL);
1321 EXPORT_SYMBOL(drm_plane_init);
1324 * drm_plane_cleanup - Clean up the core plane usage
1325 * @plane: plane to cleanup
1327 * This function cleans up @plane and removes it from the DRM mode setting
1328 * core. Note that the function does *not* free the plane structure itself,
1329 * this is the responsibility of the caller.
1331 void drm_plane_cleanup(struct drm_plane *plane)
1333 struct drm_device *dev = plane->dev;
1335 drm_modeset_lock_all(dev);
1336 kfree(plane->format_types);
1337 drm_mode_object_unregister(dev, &plane->base);
1339 BUG_ON(list_empty(&plane->head));
1341 /* Note that the plane_list is considered to be static; should we
1342 * remove the drm_plane at runtime we would have to decrement all
1343 * the indices on the drm_plane after us in the plane_list.
1346 list_del(&plane->head);
1347 dev->mode_config.num_total_plane--;
1348 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1349 dev->mode_config.num_overlay_plane--;
1350 drm_modeset_unlock_all(dev);
1352 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1353 if (plane->state && plane->funcs->atomic_destroy_state)
1354 plane->funcs->atomic_destroy_state(plane, plane->state);
1358 memset(plane, 0, sizeof(*plane));
1360 EXPORT_SYMBOL(drm_plane_cleanup);
1363 * drm_plane_from_index - find the registered plane at an index
1365 * @idx: index of registered plane to find for
1367 * Given a plane index, return the registered plane from DRM device's
1368 * list of planes with matching index.
1371 drm_plane_from_index(struct drm_device *dev, int idx)
1373 struct drm_plane *plane;
1375 drm_for_each_plane(plane, dev)
1376 if (idx == plane->index)
1381 EXPORT_SYMBOL(drm_plane_from_index);
1384 * drm_plane_force_disable - Forcibly disable a plane
1385 * @plane: plane to disable
1387 * Forces the plane to be disabled.
1389 * Used when the plane's current framebuffer is destroyed,
1390 * and when restoring fbdev mode.
1392 void drm_plane_force_disable(struct drm_plane *plane)
1399 plane->old_fb = plane->fb;
1400 ret = plane->funcs->disable_plane(plane);
1402 DRM_ERROR("failed to disable plane with busy fb\n");
1403 plane->old_fb = NULL;
1406 /* disconnect the plane from the fb and crtc: */
1407 drm_framebuffer_unreference(plane->old_fb);
1408 plane->old_fb = NULL;
1412 EXPORT_SYMBOL(drm_plane_force_disable);
1414 static int drm_mode_create_standard_properties(struct drm_device *dev)
1416 struct drm_property *prop;
1419 * Standard properties (apply to all connectors)
1421 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1422 DRM_MODE_PROP_IMMUTABLE,
1426 dev->mode_config.edid_property = prop;
1428 prop = drm_property_create_enum(dev, 0,
1429 "DPMS", drm_dpms_enum_list,
1430 ARRAY_SIZE(drm_dpms_enum_list));
1433 dev->mode_config.dpms_property = prop;
1435 prop = drm_property_create(dev,
1436 DRM_MODE_PROP_BLOB |
1437 DRM_MODE_PROP_IMMUTABLE,
1441 dev->mode_config.path_property = prop;
1443 prop = drm_property_create(dev,
1444 DRM_MODE_PROP_BLOB |
1445 DRM_MODE_PROP_IMMUTABLE,
1449 dev->mode_config.tile_property = prop;
1451 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1452 "type", drm_plane_type_enum_list,
1453 ARRAY_SIZE(drm_plane_type_enum_list));
1456 dev->mode_config.plane_type_property = prop;
1458 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1459 "SRC_X", 0, UINT_MAX);
1462 dev->mode_config.prop_src_x = prop;
1464 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1465 "SRC_Y", 0, UINT_MAX);
1468 dev->mode_config.prop_src_y = prop;
1470 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1471 "SRC_W", 0, UINT_MAX);
1474 dev->mode_config.prop_src_w = prop;
1476 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1477 "SRC_H", 0, UINT_MAX);
1480 dev->mode_config.prop_src_h = prop;
1482 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1483 "CRTC_X", INT_MIN, INT_MAX);
1486 dev->mode_config.prop_crtc_x = prop;
1488 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1489 "CRTC_Y", INT_MIN, INT_MAX);
1492 dev->mode_config.prop_crtc_y = prop;
1494 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1495 "CRTC_W", 0, INT_MAX);
1498 dev->mode_config.prop_crtc_w = prop;
1500 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1501 "CRTC_H", 0, INT_MAX);
1504 dev->mode_config.prop_crtc_h = prop;
1506 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1507 "FB_ID", DRM_MODE_OBJECT_FB);
1510 dev->mode_config.prop_fb_id = prop;
1512 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1513 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1516 dev->mode_config.prop_crtc_id = prop;
1518 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1522 dev->mode_config.prop_active = prop;
1524 prop = drm_property_create(dev,
1525 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1529 dev->mode_config.prop_mode_id = prop;
1531 prop = drm_property_create(dev,
1536 dev->mode_config.degamma_lut_property = prop;
1538 prop = drm_property_create_range(dev,
1539 DRM_MODE_PROP_IMMUTABLE,
1540 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1543 dev->mode_config.degamma_lut_size_property = prop;
1545 prop = drm_property_create(dev,
1550 dev->mode_config.ctm_property = prop;
1552 prop = drm_property_create(dev,
1557 dev->mode_config.gamma_lut_property = prop;
1559 prop = drm_property_create_range(dev,
1560 DRM_MODE_PROP_IMMUTABLE,
1561 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1564 dev->mode_config.gamma_lut_size_property = prop;
1570 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1573 * Called by a driver the first time a DVI-I connector is made.
1575 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1577 struct drm_property *dvi_i_selector;
1578 struct drm_property *dvi_i_subconnector;
1580 if (dev->mode_config.dvi_i_select_subconnector_property)
1584 drm_property_create_enum(dev, 0,
1585 "select subconnector",
1586 drm_dvi_i_select_enum_list,
1587 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1588 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1590 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1592 drm_dvi_i_subconnector_enum_list,
1593 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1594 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1598 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1601 * drm_create_tv_properties - create TV specific connector properties
1603 * @num_modes: number of different TV formats (modes) supported
1604 * @modes: array of pointers to strings containing name of each format
1606 * Called by a driver's TV initialization routine, this function creates
1607 * the TV specific connector properties for a given device. Caller is
1608 * responsible for allocating a list of format names and passing them to
1611 int drm_mode_create_tv_properties(struct drm_device *dev,
1612 unsigned int num_modes,
1613 const char * const modes[])
1615 struct drm_property *tv_selector;
1616 struct drm_property *tv_subconnector;
1619 if (dev->mode_config.tv_select_subconnector_property)
1623 * Basic connector properties
1625 tv_selector = drm_property_create_enum(dev, 0,
1626 "select subconnector",
1627 drm_tv_select_enum_list,
1628 ARRAY_SIZE(drm_tv_select_enum_list));
1632 dev->mode_config.tv_select_subconnector_property = tv_selector;
1635 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1637 drm_tv_subconnector_enum_list,
1638 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1639 if (!tv_subconnector)
1641 dev->mode_config.tv_subconnector_property = tv_subconnector;
1644 * Other, TV specific properties: margins & TV modes.
1646 dev->mode_config.tv_left_margin_property =
1647 drm_property_create_range(dev, 0, "left margin", 0, 100);
1648 if (!dev->mode_config.tv_left_margin_property)
1651 dev->mode_config.tv_right_margin_property =
1652 drm_property_create_range(dev, 0, "right margin", 0, 100);
1653 if (!dev->mode_config.tv_right_margin_property)
1656 dev->mode_config.tv_top_margin_property =
1657 drm_property_create_range(dev, 0, "top margin", 0, 100);
1658 if (!dev->mode_config.tv_top_margin_property)
1661 dev->mode_config.tv_bottom_margin_property =
1662 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1663 if (!dev->mode_config.tv_bottom_margin_property)
1666 dev->mode_config.tv_mode_property =
1667 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1669 if (!dev->mode_config.tv_mode_property)
1672 for (i = 0; i < num_modes; i++)
1673 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1676 dev->mode_config.tv_brightness_property =
1677 drm_property_create_range(dev, 0, "brightness", 0, 100);
1678 if (!dev->mode_config.tv_brightness_property)
1681 dev->mode_config.tv_contrast_property =
1682 drm_property_create_range(dev, 0, "contrast", 0, 100);
1683 if (!dev->mode_config.tv_contrast_property)
1686 dev->mode_config.tv_flicker_reduction_property =
1687 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1688 if (!dev->mode_config.tv_flicker_reduction_property)
1691 dev->mode_config.tv_overscan_property =
1692 drm_property_create_range(dev, 0, "overscan", 0, 100);
1693 if (!dev->mode_config.tv_overscan_property)
1696 dev->mode_config.tv_saturation_property =
1697 drm_property_create_range(dev, 0, "saturation", 0, 100);
1698 if (!dev->mode_config.tv_saturation_property)
1701 dev->mode_config.tv_hue_property =
1702 drm_property_create_range(dev, 0, "hue", 0, 100);
1703 if (!dev->mode_config.tv_hue_property)
1710 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1713 * drm_mode_create_scaling_mode_property - create scaling mode property
1716 * Called by a driver the first time it's needed, must be attached to desired
1719 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1721 struct drm_property *scaling_mode;
1723 if (dev->mode_config.scaling_mode_property)
1727 drm_property_create_enum(dev, 0, "scaling mode",
1728 drm_scaling_mode_enum_list,
1729 ARRAY_SIZE(drm_scaling_mode_enum_list));
1731 dev->mode_config.scaling_mode_property = scaling_mode;
1735 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1738 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1741 * Called by a driver the first time it's needed, must be attached to desired
1745 * Zero on success, negative errno on failure.
1747 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1749 if (dev->mode_config.aspect_ratio_property)
1752 dev->mode_config.aspect_ratio_property =
1753 drm_property_create_enum(dev, 0, "aspect ratio",
1754 drm_aspect_ratio_enum_list,
1755 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1757 if (dev->mode_config.aspect_ratio_property == NULL)
1762 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1765 * drm_mode_create_dirty_property - create dirty property
1768 * Called by a driver the first time it's needed, must be attached to desired
1771 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1773 struct drm_property *dirty_info;
1775 if (dev->mode_config.dirty_info_property)
1779 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1781 drm_dirty_info_enum_list,
1782 ARRAY_SIZE(drm_dirty_info_enum_list));
1783 dev->mode_config.dirty_info_property = dirty_info;
1787 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1790 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1793 * Create the the suggested x/y offset property for connectors.
1795 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1797 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1800 dev->mode_config.suggested_x_property =
1801 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1803 dev->mode_config.suggested_y_property =
1804 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1806 if (dev->mode_config.suggested_x_property == NULL ||
1807 dev->mode_config.suggested_y_property == NULL)
1811 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1814 * drm_mode_getresources - get graphics configuration
1815 * @dev: drm device for the ioctl
1816 * @data: data pointer for the ioctl
1817 * @file_priv: drm file for the ioctl call
1819 * Construct a set of configuration description structures and return
1820 * them to the user, including CRTC, connector and framebuffer configuration.
1822 * Called by the user via ioctl.
1825 * Zero on success, negative errno on failure.
1827 int drm_mode_getresources(struct drm_device *dev, void *data,
1828 struct drm_file *file_priv)
1830 struct drm_mode_card_res *card_res = data;
1831 struct list_head *lh;
1832 struct drm_framebuffer *fb;
1833 struct drm_connector *connector;
1834 struct drm_crtc *crtc;
1835 struct drm_encoder *encoder;
1837 int connector_count = 0;
1840 int encoder_count = 0;
1842 uint32_t __user *fb_id;
1843 uint32_t __user *crtc_id;
1844 uint32_t __user *connector_id;
1845 uint32_t __user *encoder_id;
1847 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1851 mutex_lock(&file_priv->fbs_lock);
1853 * For the non-control nodes we need to limit the list of resources
1854 * by IDs in the group list for this node
1856 list_for_each(lh, &file_priv->fbs)
1859 /* handle this in 4 parts */
1861 if (card_res->count_fbs >= fb_count) {
1863 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1864 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1865 if (put_user(fb->base.id, fb_id + copied)) {
1866 mutex_unlock(&file_priv->fbs_lock);
1872 card_res->count_fbs = fb_count;
1873 mutex_unlock(&file_priv->fbs_lock);
1875 /* mode_config.mutex protects the connector list against e.g. DP MST
1876 * connector hot-adding. CRTC/Plane lists are invariant. */
1877 mutex_lock(&dev->mode_config.mutex);
1878 drm_for_each_crtc(crtc, dev)
1881 drm_for_each_connector(connector, dev)
1884 drm_for_each_encoder(encoder, dev)
1887 card_res->max_height = dev->mode_config.max_height;
1888 card_res->min_height = dev->mode_config.min_height;
1889 card_res->max_width = dev->mode_config.max_width;
1890 card_res->min_width = dev->mode_config.min_width;
1893 if (card_res->count_crtcs >= crtc_count) {
1895 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1896 drm_for_each_crtc(crtc, dev) {
1897 if (put_user(crtc->base.id, crtc_id + copied)) {
1904 card_res->count_crtcs = crtc_count;
1907 if (card_res->count_encoders >= encoder_count) {
1909 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1910 drm_for_each_encoder(encoder, dev) {
1911 if (put_user(encoder->base.id, encoder_id +
1919 card_res->count_encoders = encoder_count;
1922 if (card_res->count_connectors >= connector_count) {
1924 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1925 drm_for_each_connector(connector, dev) {
1926 if (put_user(connector->base.id,
1927 connector_id + copied)) {
1934 card_res->count_connectors = connector_count;
1937 mutex_unlock(&dev->mode_config.mutex);
1942 * drm_mode_getcrtc - get CRTC configuration
1943 * @dev: drm device for the ioctl
1944 * @data: data pointer for the ioctl
1945 * @file_priv: drm file for the ioctl call
1947 * Construct a CRTC configuration structure to return to the user.
1949 * Called by the user via ioctl.
1952 * Zero on success, negative errno on failure.
1954 int drm_mode_getcrtc(struct drm_device *dev,
1955 void *data, struct drm_file *file_priv)
1957 struct drm_mode_crtc *crtc_resp = data;
1958 struct drm_crtc *crtc;
1960 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1963 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1967 drm_modeset_lock_crtc(crtc, crtc->primary);
1968 crtc_resp->gamma_size = crtc->gamma_size;
1969 if (crtc->primary->fb)
1970 crtc_resp->fb_id = crtc->primary->fb->base.id;
1972 crtc_resp->fb_id = 0;
1975 crtc_resp->x = crtc->primary->state->src_x >> 16;
1976 crtc_resp->y = crtc->primary->state->src_y >> 16;
1977 if (crtc->state->enable) {
1978 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
1979 crtc_resp->mode_valid = 1;
1982 crtc_resp->mode_valid = 0;
1985 crtc_resp->x = crtc->x;
1986 crtc_resp->y = crtc->y;
1987 if (crtc->enabled) {
1988 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1989 crtc_resp->mode_valid = 1;
1992 crtc_resp->mode_valid = 0;
1995 drm_modeset_unlock_crtc(crtc);
2000 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
2001 const struct drm_file *file_priv)
2004 * If user-space hasn't configured the driver to expose the stereo 3D
2005 * modes, don't expose them.
2007 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
2013 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2015 /* For atomic drivers only state objects are synchronously updated and
2016 * protected by modeset locks, so check those first. */
2017 if (connector->state)
2018 return connector->state->best_encoder;
2019 return connector->encoder;
2022 /* helper for getconnector and getproperties ioctls */
2023 static int get_properties(struct drm_mode_object *obj, bool atomic,
2024 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2025 uint32_t *arg_count_props)
2030 props_count = obj->properties->count;
2032 props_count -= obj->properties->atomic_count;
2034 if ((*arg_count_props >= props_count) && props_count) {
2035 for (i = 0, copied = 0; copied < props_count; i++) {
2036 struct drm_property *prop = obj->properties->properties[i];
2039 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2042 ret = drm_object_property_get_value(obj, prop, &val);
2046 if (put_user(prop->base.id, prop_ptr + copied))
2049 if (put_user(val, prop_values + copied))
2055 *arg_count_props = props_count;
2061 * drm_mode_getconnector - get connector configuration
2062 * @dev: drm device for the ioctl
2063 * @data: data pointer for the ioctl
2064 * @file_priv: drm file for the ioctl call
2066 * Construct a connector configuration structure to return to the user.
2068 * Called by the user via ioctl.
2071 * Zero on success, negative errno on failure.
2073 int drm_mode_getconnector(struct drm_device *dev, void *data,
2074 struct drm_file *file_priv)
2076 struct drm_mode_get_connector *out_resp = data;
2077 struct drm_connector *connector;
2078 struct drm_encoder *encoder;
2079 struct drm_display_mode *mode;
2081 int encoders_count = 0;
2085 struct drm_mode_modeinfo u_mode;
2086 struct drm_mode_modeinfo __user *mode_ptr;
2087 uint32_t __user *encoder_ptr;
2089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2092 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2094 mutex_lock(&dev->mode_config.mutex);
2096 connector = drm_connector_lookup(dev, out_resp->connector_id);
2102 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2103 if (connector->encoder_ids[i] != 0)
2106 if (out_resp->count_modes == 0) {
2107 connector->funcs->fill_modes(connector,
2108 dev->mode_config.max_width,
2109 dev->mode_config.max_height);
2112 /* delayed so we get modes regardless of pre-fill_modes state */
2113 list_for_each_entry(mode, &connector->modes, head)
2114 if (drm_mode_expose_to_userspace(mode, file_priv))
2117 out_resp->connector_id = connector->base.id;
2118 out_resp->connector_type = connector->connector_type;
2119 out_resp->connector_type_id = connector->connector_type_id;
2120 out_resp->mm_width = connector->display_info.width_mm;
2121 out_resp->mm_height = connector->display_info.height_mm;
2122 out_resp->subpixel = connector->display_info.subpixel_order;
2123 out_resp->connection = connector->status;
2125 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2126 encoder = drm_connector_get_encoder(connector);
2128 out_resp->encoder_id = encoder->base.id;
2130 out_resp->encoder_id = 0;
2133 * This ioctl is called twice, once to determine how much space is
2134 * needed, and the 2nd time to fill it.
2136 if ((out_resp->count_modes >= mode_count) && mode_count) {
2138 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2139 list_for_each_entry(mode, &connector->modes, head) {
2140 if (!drm_mode_expose_to_userspace(mode, file_priv))
2143 drm_mode_convert_to_umode(&u_mode, mode);
2144 if (copy_to_user(mode_ptr + copied,
2145 &u_mode, sizeof(u_mode))) {
2152 out_resp->count_modes = mode_count;
2154 ret = get_properties(&connector->base, file_priv->atomic,
2155 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2156 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2157 &out_resp->count_props);
2161 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2163 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2164 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2165 if (connector->encoder_ids[i] != 0) {
2166 if (put_user(connector->encoder_ids[i],
2167 encoder_ptr + copied)) {
2175 out_resp->count_encoders = encoders_count;
2178 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2180 drm_connector_unreference(connector);
2182 mutex_unlock(&dev->mode_config.mutex);
2187 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2189 struct drm_connector *connector;
2190 struct drm_device *dev = encoder->dev;
2191 bool uses_atomic = false;
2193 /* For atomic drivers only state objects are synchronously updated and
2194 * protected by modeset locks, so check those first. */
2195 drm_for_each_connector(connector, dev) {
2196 if (!connector->state)
2201 if (connector->state->best_encoder != encoder)
2204 return connector->state->crtc;
2207 /* Don't return stale data (e.g. pending async disable). */
2211 return encoder->crtc;
2215 * drm_mode_getencoder - get encoder configuration
2216 * @dev: drm device for the ioctl
2217 * @data: data pointer for the ioctl
2218 * @file_priv: drm file for the ioctl call
2220 * Construct a encoder configuration structure to return to the user.
2222 * Called by the user via ioctl.
2225 * Zero on success, negative errno on failure.
2227 int drm_mode_getencoder(struct drm_device *dev, void *data,
2228 struct drm_file *file_priv)
2230 struct drm_mode_get_encoder *enc_resp = data;
2231 struct drm_encoder *encoder;
2232 struct drm_crtc *crtc;
2234 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2237 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2241 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2242 crtc = drm_encoder_get_crtc(encoder);
2244 enc_resp->crtc_id = crtc->base.id;
2246 enc_resp->crtc_id = 0;
2247 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2249 enc_resp->encoder_type = encoder->encoder_type;
2250 enc_resp->encoder_id = encoder->base.id;
2251 enc_resp->possible_crtcs = encoder->possible_crtcs;
2252 enc_resp->possible_clones = encoder->possible_clones;
2258 * drm_mode_getplane_res - enumerate all plane resources
2261 * @file_priv: DRM file info
2263 * Construct a list of plane ids to return to the user.
2265 * Called by the user via ioctl.
2268 * Zero on success, negative errno on failure.
2270 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2271 struct drm_file *file_priv)
2273 struct drm_mode_get_plane_res *plane_resp = data;
2274 struct drm_mode_config *config;
2275 struct drm_plane *plane;
2276 uint32_t __user *plane_ptr;
2278 unsigned num_planes;
2280 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 config = &dev->mode_config;
2285 if (file_priv->universal_planes)
2286 num_planes = config->num_total_plane;
2288 num_planes = config->num_overlay_plane;
2291 * This ioctl is called twice, once to determine how much space is
2292 * needed, and the 2nd time to fill it.
2295 (plane_resp->count_planes >= num_planes)) {
2296 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2298 /* Plane lists are invariant, no locking needed. */
2299 drm_for_each_plane(plane, dev) {
2301 * Unless userspace set the 'universal planes'
2302 * capability bit, only advertise overlays.
2304 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2305 !file_priv->universal_planes)
2308 if (put_user(plane->base.id, plane_ptr + copied))
2313 plane_resp->count_planes = num_planes;
2319 * drm_mode_getplane - get plane configuration
2322 * @file_priv: DRM file info
2324 * Construct a plane configuration structure to return to the user.
2326 * Called by the user via ioctl.
2329 * Zero on success, negative errno on failure.
2331 int drm_mode_getplane(struct drm_device *dev, void *data,
2332 struct drm_file *file_priv)
2334 struct drm_mode_get_plane *plane_resp = data;
2335 struct drm_plane *plane;
2336 uint32_t __user *format_ptr;
2338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2341 plane = drm_plane_find(dev, plane_resp->plane_id);
2345 drm_modeset_lock(&plane->mutex, NULL);
2347 plane_resp->crtc_id = plane->crtc->base.id;
2349 plane_resp->crtc_id = 0;
2352 plane_resp->fb_id = plane->fb->base.id;
2354 plane_resp->fb_id = 0;
2355 drm_modeset_unlock(&plane->mutex);
2357 plane_resp->plane_id = plane->base.id;
2358 plane_resp->possible_crtcs = plane->possible_crtcs;
2359 plane_resp->gamma_size = 0;
2362 * This ioctl is called twice, once to determine how much space is
2363 * needed, and the 2nd time to fill it.
2365 if (plane->format_count &&
2366 (plane_resp->count_format_types >= plane->format_count)) {
2367 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2368 if (copy_to_user(format_ptr,
2369 plane->format_types,
2370 sizeof(uint32_t) * plane->format_count)) {
2374 plane_resp->count_format_types = plane->format_count;
2380 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2381 * @plane: plane to check for format support
2382 * @format: the pixel format
2385 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2388 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2392 for (i = 0; i < plane->format_count; i++) {
2393 if (format == plane->format_types[i])
2400 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2401 uint32_t src_w, uint32_t src_h,
2402 const struct drm_framebuffer *fb)
2404 unsigned int fb_width, fb_height;
2406 fb_width = fb->width << 16;
2407 fb_height = fb->height << 16;
2409 /* Make sure source coordinates are inside the fb. */
2410 if (src_w > fb_width ||
2411 src_x > fb_width - src_w ||
2412 src_h > fb_height ||
2413 src_y > fb_height - src_h) {
2414 DRM_DEBUG_KMS("Invalid source coordinates "
2415 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2416 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2417 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2418 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2419 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2427 * setplane_internal - setplane handler for internal callers
2429 * Note that we assume an extra reference has already been taken on fb. If the
2430 * update fails, this reference will be dropped before return; if it succeeds,
2431 * the previous framebuffer (if any) will be unreferenced instead.
2433 * src_{x,y,w,h} are provided in 16.16 fixed point format
2435 static int __setplane_internal(struct drm_plane *plane,
2436 struct drm_crtc *crtc,
2437 struct drm_framebuffer *fb,
2438 int32_t crtc_x, int32_t crtc_y,
2439 uint32_t crtc_w, uint32_t crtc_h,
2440 /* src_{x,y,w,h} values are 16.16 fixed point */
2441 uint32_t src_x, uint32_t src_y,
2442 uint32_t src_w, uint32_t src_h)
2446 /* No fb means shut it down */
2448 plane->old_fb = plane->fb;
2449 ret = plane->funcs->disable_plane(plane);
2454 plane->old_fb = NULL;
2459 /* Check whether this plane is usable on this CRTC */
2460 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2461 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2466 /* Check whether this plane supports the fb pixel format. */
2467 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2469 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2470 drm_get_format_name(fb->pixel_format));
2474 /* Give drivers some help against integer overflows */
2475 if (crtc_w > INT_MAX ||
2476 crtc_x > INT_MAX - (int32_t) crtc_w ||
2478 crtc_y > INT_MAX - (int32_t) crtc_h) {
2479 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2480 crtc_w, crtc_h, crtc_x, crtc_y);
2485 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2489 plane->old_fb = plane->fb;
2490 ret = plane->funcs->update_plane(plane, crtc, fb,
2491 crtc_x, crtc_y, crtc_w, crtc_h,
2492 src_x, src_y, src_w, src_h);
2498 plane->old_fb = NULL;
2503 drm_framebuffer_unreference(fb);
2505 drm_framebuffer_unreference(plane->old_fb);
2506 plane->old_fb = NULL;
2511 static int setplane_internal(struct drm_plane *plane,
2512 struct drm_crtc *crtc,
2513 struct drm_framebuffer *fb,
2514 int32_t crtc_x, int32_t crtc_y,
2515 uint32_t crtc_w, uint32_t crtc_h,
2516 /* src_{x,y,w,h} values are 16.16 fixed point */
2517 uint32_t src_x, uint32_t src_y,
2518 uint32_t src_w, uint32_t src_h)
2522 drm_modeset_lock_all(plane->dev);
2523 ret = __setplane_internal(plane, crtc, fb,
2524 crtc_x, crtc_y, crtc_w, crtc_h,
2525 src_x, src_y, src_w, src_h);
2526 drm_modeset_unlock_all(plane->dev);
2532 * drm_mode_setplane - configure a plane's configuration
2534 * @data: ioctl data*
2535 * @file_priv: DRM file info
2537 * Set plane configuration, including placement, fb, scaling, and other factors.
2538 * Or pass a NULL fb to disable (planes may be disabled without providing a
2542 * Zero on success, negative errno on failure.
2544 int drm_mode_setplane(struct drm_device *dev, void *data,
2545 struct drm_file *file_priv)
2547 struct drm_mode_set_plane *plane_req = data;
2548 struct drm_plane *plane;
2549 struct drm_crtc *crtc = NULL;
2550 struct drm_framebuffer *fb = NULL;
2552 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2556 * First, find the plane, crtc, and fb objects. If not available,
2557 * we don't bother to call the driver.
2559 plane = drm_plane_find(dev, plane_req->plane_id);
2561 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2562 plane_req->plane_id);
2566 if (plane_req->fb_id) {
2567 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2569 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2574 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2576 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2577 plane_req->crtc_id);
2583 * setplane_internal will take care of deref'ing either the old or new
2584 * framebuffer depending on success.
2586 return setplane_internal(plane, crtc, fb,
2587 plane_req->crtc_x, plane_req->crtc_y,
2588 plane_req->crtc_w, plane_req->crtc_h,
2589 plane_req->src_x, plane_req->src_y,
2590 plane_req->src_w, plane_req->src_h);
2594 * drm_mode_set_config_internal - helper to call ->set_config
2595 * @set: modeset config to set
2597 * This is a little helper to wrap internal calls to the ->set_config driver
2598 * interface. The only thing it adds is correct refcounting dance.
2601 * Zero on success, negative errno on failure.
2603 int drm_mode_set_config_internal(struct drm_mode_set *set)
2605 struct drm_crtc *crtc = set->crtc;
2606 struct drm_framebuffer *fb;
2607 struct drm_crtc *tmp;
2611 * NOTE: ->set_config can also disable other crtcs (if we steal all
2612 * connectors from it), hence we need to refcount the fbs across all
2613 * crtcs. Atomic modeset will have saner semantics ...
2615 drm_for_each_crtc(tmp, crtc->dev)
2616 tmp->primary->old_fb = tmp->primary->fb;
2620 ret = crtc->funcs->set_config(set);
2622 crtc->primary->crtc = crtc;
2623 crtc->primary->fb = fb;
2626 drm_for_each_crtc(tmp, crtc->dev) {
2627 if (tmp->primary->fb)
2628 drm_framebuffer_reference(tmp->primary->fb);
2629 if (tmp->primary->old_fb)
2630 drm_framebuffer_unreference(tmp->primary->old_fb);
2631 tmp->primary->old_fb = NULL;
2636 EXPORT_SYMBOL(drm_mode_set_config_internal);
2639 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2640 * @mode: mode to query
2641 * @hdisplay: hdisplay value to fill in
2642 * @vdisplay: vdisplay value to fill in
2644 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2645 * the appropriate layout.
2647 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2648 int *hdisplay, int *vdisplay)
2650 struct drm_display_mode adjusted;
2652 drm_mode_copy(&adjusted, mode);
2653 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2654 *hdisplay = adjusted.crtc_hdisplay;
2655 *vdisplay = adjusted.crtc_vdisplay;
2657 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2660 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2662 * @crtc: CRTC that framebuffer will be displayed on
2665 * @mode: mode that framebuffer will be displayed under
2666 * @fb: framebuffer to check size of
2668 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2670 const struct drm_display_mode *mode,
2671 const struct drm_framebuffer *fb)
2674 int hdisplay, vdisplay;
2676 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2679 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2680 BIT(DRM_ROTATE_270)))
2681 swap(hdisplay, vdisplay);
2683 return check_src_coords(x << 16, y << 16,
2684 hdisplay << 16, vdisplay << 16, fb);
2686 EXPORT_SYMBOL(drm_crtc_check_viewport);
2689 * drm_mode_setcrtc - set CRTC configuration
2690 * @dev: drm device for the ioctl
2691 * @data: data pointer for the ioctl
2692 * @file_priv: drm file for the ioctl call
2694 * Build a new CRTC configuration based on user request.
2696 * Called by the user via ioctl.
2699 * Zero on success, negative errno on failure.
2701 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2702 struct drm_file *file_priv)
2704 struct drm_mode_config *config = &dev->mode_config;
2705 struct drm_mode_crtc *crtc_req = data;
2706 struct drm_crtc *crtc;
2707 struct drm_connector **connector_set = NULL, *connector;
2708 struct drm_framebuffer *fb = NULL;
2709 struct drm_display_mode *mode = NULL;
2710 struct drm_mode_set set;
2711 uint32_t __user *set_connectors_ptr;
2715 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2719 * Universal plane src offsets are only 16.16, prevent havoc for
2720 * drivers using universal plane code internally.
2722 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2725 drm_modeset_lock_all(dev);
2726 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2728 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2732 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2734 if (crtc_req->mode_valid) {
2735 /* If we have a mode we need a framebuffer. */
2736 /* If we pass -1, set the mode with the currently bound fb */
2737 if (crtc_req->fb_id == -1) {
2738 if (!crtc->primary->fb) {
2739 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2743 fb = crtc->primary->fb;
2744 /* Make refcounting symmetric with the lookup path. */
2745 drm_framebuffer_reference(fb);
2747 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2749 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2756 mode = drm_mode_create(dev);
2762 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2764 DRM_DEBUG_KMS("Invalid mode\n");
2768 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2771 * Check whether the primary plane supports the fb pixel format.
2772 * Drivers not implementing the universal planes API use a
2773 * default formats list provided by the DRM core which doesn't
2774 * match real hardware capabilities. Skip the check in that
2777 if (!crtc->primary->format_default) {
2778 ret = drm_plane_check_pixel_format(crtc->primary,
2781 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2782 drm_get_format_name(fb->pixel_format));
2787 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2794 if (crtc_req->count_connectors == 0 && mode) {
2795 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2800 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2801 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2802 crtc_req->count_connectors);
2807 if (crtc_req->count_connectors > 0) {
2810 /* Avoid unbounded kernel memory allocation */
2811 if (crtc_req->count_connectors > config->num_connector) {
2816 connector_set = kmalloc_array(crtc_req->count_connectors,
2817 sizeof(struct drm_connector *),
2819 if (!connector_set) {
2824 for (i = 0; i < crtc_req->count_connectors; i++) {
2825 connector_set[i] = NULL;
2826 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2827 if (get_user(out_id, &set_connectors_ptr[i])) {
2832 connector = drm_connector_lookup(dev, out_id);
2834 DRM_DEBUG_KMS("Connector id %d unknown\n",
2839 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2843 connector_set[i] = connector;
2848 set.x = crtc_req->x;
2849 set.y = crtc_req->y;
2851 set.connectors = connector_set;
2852 set.num_connectors = crtc_req->count_connectors;
2854 ret = drm_mode_set_config_internal(&set);
2858 drm_framebuffer_unreference(fb);
2860 if (connector_set) {
2861 for (i = 0; i < crtc_req->count_connectors; i++) {
2862 if (connector_set[i])
2863 drm_connector_unreference(connector_set[i]);
2866 kfree(connector_set);
2867 drm_mode_destroy(dev, mode);
2868 drm_modeset_unlock_all(dev);
2873 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2874 * universal plane handler call
2875 * @crtc: crtc to update cursor for
2876 * @req: data pointer for the ioctl
2877 * @file_priv: drm file for the ioctl call
2879 * Legacy cursor ioctl's work directly with driver buffer handles. To
2880 * translate legacy ioctl calls into universal plane handler calls, we need to
2881 * wrap the native buffer handle in a drm_framebuffer.
2883 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2884 * buffer with a pitch of 4*width; the universal plane interface should be used
2885 * directly in cases where the hardware can support other buffer settings and
2886 * userspace wants to make use of these capabilities.
2889 * Zero on success, negative errno on failure.
2891 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2892 struct drm_mode_cursor2 *req,
2893 struct drm_file *file_priv)
2895 struct drm_device *dev = crtc->dev;
2896 struct drm_framebuffer *fb = NULL;
2897 struct drm_mode_fb_cmd2 fbreq = {
2898 .width = req->width,
2899 .height = req->height,
2900 .pixel_format = DRM_FORMAT_ARGB8888,
2901 .pitches = { req->width * 4 },
2902 .handles = { req->handle },
2904 int32_t crtc_x, crtc_y;
2905 uint32_t crtc_w = 0, crtc_h = 0;
2906 uint32_t src_w = 0, src_h = 0;
2909 BUG_ON(!crtc->cursor);
2910 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2913 * Obtain fb we'll be using (either new or existing) and take an extra
2914 * reference to it if fb != null. setplane will take care of dropping
2915 * the reference if the plane update fails.
2917 if (req->flags & DRM_MODE_CURSOR_BO) {
2919 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2921 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2928 fb = crtc->cursor->fb;
2930 drm_framebuffer_reference(fb);
2933 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2937 crtc_x = crtc->cursor_x;
2938 crtc_y = crtc->cursor_y;
2943 crtc_h = fb->height;
2944 src_w = fb->width << 16;
2945 src_h = fb->height << 16;
2949 * setplane_internal will take care of deref'ing either the old or new
2950 * framebuffer depending on success.
2952 ret = __setplane_internal(crtc->cursor, crtc, fb,
2953 crtc_x, crtc_y, crtc_w, crtc_h,
2954 0, 0, src_w, src_h);
2956 /* Update successful; save new cursor position, if necessary */
2957 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2958 crtc->cursor_x = req->x;
2959 crtc->cursor_y = req->y;
2965 static int drm_mode_cursor_common(struct drm_device *dev,
2966 struct drm_mode_cursor2 *req,
2967 struct drm_file *file_priv)
2969 struct drm_crtc *crtc;
2972 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2975 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2978 crtc = drm_crtc_find(dev, req->crtc_id);
2980 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2985 * If this crtc has a universal cursor plane, call that plane's update
2986 * handler rather than using legacy cursor handlers.
2988 drm_modeset_lock_crtc(crtc, crtc->cursor);
2990 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2994 if (req->flags & DRM_MODE_CURSOR_BO) {
2995 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2999 /* Turns off the cursor if handle is 0 */
3000 if (crtc->funcs->cursor_set2)
3001 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
3002 req->width, req->height, req->hot_x, req->hot_y);
3004 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
3005 req->width, req->height);
3008 if (req->flags & DRM_MODE_CURSOR_MOVE) {
3009 if (crtc->funcs->cursor_move) {
3010 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3017 drm_modeset_unlock_crtc(crtc);
3025 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3026 * @dev: drm device for the ioctl
3027 * @data: data pointer for the ioctl
3028 * @file_priv: drm file for the ioctl call
3030 * Set the cursor configuration based on user request.
3032 * Called by the user via ioctl.
3035 * Zero on success, negative errno on failure.
3037 int drm_mode_cursor_ioctl(struct drm_device *dev,
3038 void *data, struct drm_file *file_priv)
3040 struct drm_mode_cursor *req = data;
3041 struct drm_mode_cursor2 new_req;
3043 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3044 new_req.hot_x = new_req.hot_y = 0;
3046 return drm_mode_cursor_common(dev, &new_req, file_priv);
3050 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3051 * @dev: drm device for the ioctl
3052 * @data: data pointer for the ioctl
3053 * @file_priv: drm file for the ioctl call
3055 * Set the cursor configuration based on user request. This implements the 2nd
3056 * version of the cursor ioctl, which allows userspace to additionally specify
3057 * the hotspot of the pointer.
3059 * Called by the user via ioctl.
3062 * Zero on success, negative errno on failure.
3064 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3065 void *data, struct drm_file *file_priv)
3067 struct drm_mode_cursor2 *req = data;
3069 return drm_mode_cursor_common(dev, req, file_priv);
3073 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3074 * @bpp: bits per pixels
3075 * @depth: bit depth per pixel
3077 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3078 * Useful in fbdev emulation code, since that deals in those values.
3080 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3086 fmt = DRM_FORMAT_C8;
3090 fmt = DRM_FORMAT_XRGB1555;
3092 fmt = DRM_FORMAT_RGB565;
3095 fmt = DRM_FORMAT_RGB888;
3099 fmt = DRM_FORMAT_XRGB8888;
3100 else if (depth == 30)
3101 fmt = DRM_FORMAT_XRGB2101010;
3103 fmt = DRM_FORMAT_ARGB8888;
3106 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3107 fmt = DRM_FORMAT_XRGB8888;
3113 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3116 * drm_mode_addfb - add an FB to the graphics configuration
3117 * @dev: drm device for the ioctl
3118 * @data: data pointer for the ioctl
3119 * @file_priv: drm file for the ioctl call
3121 * Add a new FB to the specified CRTC, given a user request. This is the
3122 * original addfb ioctl which only supported RGB formats.
3124 * Called by the user via ioctl.
3127 * Zero on success, negative errno on failure.
3129 int drm_mode_addfb(struct drm_device *dev,
3130 void *data, struct drm_file *file_priv)
3132 struct drm_mode_fb_cmd *or = data;
3133 struct drm_mode_fb_cmd2 r = {};
3136 /* convert to new format and call new ioctl */
3137 r.fb_id = or->fb_id;
3138 r.width = or->width;
3139 r.height = or->height;
3140 r.pitches[0] = or->pitch;
3141 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3142 r.handles[0] = or->handle;
3144 ret = drm_mode_addfb2(dev, &r, file_priv);
3148 or->fb_id = r.fb_id;
3153 static int format_check(const struct drm_mode_fb_cmd2 *r)
3155 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3159 case DRM_FORMAT_RGB332:
3160 case DRM_FORMAT_BGR233:
3161 case DRM_FORMAT_XRGB4444:
3162 case DRM_FORMAT_XBGR4444:
3163 case DRM_FORMAT_RGBX4444:
3164 case DRM_FORMAT_BGRX4444:
3165 case DRM_FORMAT_ARGB4444:
3166 case DRM_FORMAT_ABGR4444:
3167 case DRM_FORMAT_RGBA4444:
3168 case DRM_FORMAT_BGRA4444:
3169 case DRM_FORMAT_XRGB1555:
3170 case DRM_FORMAT_XBGR1555:
3171 case DRM_FORMAT_RGBX5551:
3172 case DRM_FORMAT_BGRX5551:
3173 case DRM_FORMAT_ARGB1555:
3174 case DRM_FORMAT_ABGR1555:
3175 case DRM_FORMAT_RGBA5551:
3176 case DRM_FORMAT_BGRA5551:
3177 case DRM_FORMAT_RGB565:
3178 case DRM_FORMAT_BGR565:
3179 case DRM_FORMAT_RGB888:
3180 case DRM_FORMAT_BGR888:
3181 case DRM_FORMAT_XRGB8888:
3182 case DRM_FORMAT_XBGR8888:
3183 case DRM_FORMAT_RGBX8888:
3184 case DRM_FORMAT_BGRX8888:
3185 case DRM_FORMAT_ARGB8888:
3186 case DRM_FORMAT_ABGR8888:
3187 case DRM_FORMAT_RGBA8888:
3188 case DRM_FORMAT_BGRA8888:
3189 case DRM_FORMAT_XRGB2101010:
3190 case DRM_FORMAT_XBGR2101010:
3191 case DRM_FORMAT_RGBX1010102:
3192 case DRM_FORMAT_BGRX1010102:
3193 case DRM_FORMAT_ARGB2101010:
3194 case DRM_FORMAT_ABGR2101010:
3195 case DRM_FORMAT_RGBA1010102:
3196 case DRM_FORMAT_BGRA1010102:
3197 case DRM_FORMAT_YUYV:
3198 case DRM_FORMAT_YVYU:
3199 case DRM_FORMAT_UYVY:
3200 case DRM_FORMAT_VYUY:
3201 case DRM_FORMAT_AYUV:
3202 case DRM_FORMAT_NV12:
3203 case DRM_FORMAT_NV21:
3204 case DRM_FORMAT_NV16:
3205 case DRM_FORMAT_NV61:
3206 case DRM_FORMAT_NV24:
3207 case DRM_FORMAT_NV42:
3208 case DRM_FORMAT_YUV410:
3209 case DRM_FORMAT_YVU410:
3210 case DRM_FORMAT_YUV411:
3211 case DRM_FORMAT_YVU411:
3212 case DRM_FORMAT_YUV420:
3213 case DRM_FORMAT_YVU420:
3214 case DRM_FORMAT_YUV422:
3215 case DRM_FORMAT_YVU422:
3216 case DRM_FORMAT_YUV444:
3217 case DRM_FORMAT_YVU444:
3220 DRM_DEBUG_KMS("invalid pixel format %s\n",
3221 drm_get_format_name(r->pixel_format));
3226 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3228 int ret, hsub, vsub, num_planes, i;
3230 ret = format_check(r);
3232 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3233 drm_get_format_name(r->pixel_format));
3237 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3238 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3239 num_planes = drm_format_num_planes(r->pixel_format);
3241 if (r->width == 0 || r->width % hsub) {
3242 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3246 if (r->height == 0 || r->height % vsub) {
3247 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3251 for (i = 0; i < num_planes; i++) {
3252 unsigned int width = r->width / (i != 0 ? hsub : 1);
3253 unsigned int height = r->height / (i != 0 ? vsub : 1);
3254 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3256 if (!r->handles[i]) {
3257 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3261 if ((uint64_t) width * cpp > UINT_MAX)
3264 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3267 if (r->pitches[i] < width * cpp) {
3268 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3272 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3273 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3278 /* modifier specific checks: */
3279 switch (r->modifier[i]) {
3280 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3281 /* NOTE: the pitch restriction may be lifted later if it turns
3282 * out that no hw has this restriction:
3284 if (r->pixel_format != DRM_FORMAT_NV12 ||
3285 width % 128 || height % 32 ||
3286 r->pitches[i] % 128) {
3287 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3297 for (i = num_planes; i < 4; i++) {
3298 if (r->modifier[i]) {
3299 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3303 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3304 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3307 if (r->handles[i]) {
3308 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3312 if (r->pitches[i]) {
3313 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3317 if (r->offsets[i]) {
3318 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3326 static struct drm_framebuffer *
3327 internal_framebuffer_create(struct drm_device *dev,
3328 const struct drm_mode_fb_cmd2 *r,
3329 struct drm_file *file_priv)
3331 struct drm_mode_config *config = &dev->mode_config;
3332 struct drm_framebuffer *fb;
3335 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3336 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3337 return ERR_PTR(-EINVAL);
3340 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3341 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3342 r->width, config->min_width, config->max_width);
3343 return ERR_PTR(-EINVAL);
3345 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3346 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3347 r->height, config->min_height, config->max_height);
3348 return ERR_PTR(-EINVAL);
3351 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3352 !dev->mode_config.allow_fb_modifiers) {
3353 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3354 return ERR_PTR(-EINVAL);
3357 ret = framebuffer_check(r);
3359 return ERR_PTR(ret);
3361 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3363 DRM_DEBUG_KMS("could not create framebuffer\n");
3371 * drm_mode_addfb2 - add an FB to the graphics configuration
3372 * @dev: drm device for the ioctl
3373 * @data: data pointer for the ioctl
3374 * @file_priv: drm file for the ioctl call
3376 * Add a new FB to the specified CRTC, given a user request with format. This is
3377 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3378 * and uses fourcc codes as pixel format specifiers.
3380 * Called by the user via ioctl.
3383 * Zero on success, negative errno on failure.
3385 int drm_mode_addfb2(struct drm_device *dev,
3386 void *data, struct drm_file *file_priv)
3388 struct drm_mode_fb_cmd2 *r = data;
3389 struct drm_framebuffer *fb;
3391 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3394 fb = internal_framebuffer_create(dev, r, file_priv);
3398 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3399 r->fb_id = fb->base.id;
3401 /* Transfer ownership to the filp for reaping on close */
3402 mutex_lock(&file_priv->fbs_lock);
3403 list_add(&fb->filp_head, &file_priv->fbs);
3404 mutex_unlock(&file_priv->fbs_lock);
3409 struct drm_mode_rmfb_work {
3410 struct work_struct work;
3411 struct list_head fbs;
3414 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3416 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3418 while (!list_empty(&arg->fbs)) {
3419 struct drm_framebuffer *fb =
3420 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3422 list_del_init(&fb->filp_head);
3423 drm_framebuffer_remove(fb);
3428 * drm_mode_rmfb - remove an FB from the configuration
3429 * @dev: drm device for the ioctl
3430 * @data: data pointer for the ioctl
3431 * @file_priv: drm file for the ioctl call
3433 * Remove the FB specified by the user.
3435 * Called by the user via ioctl.
3438 * Zero on success, negative errno on failure.
3440 int drm_mode_rmfb(struct drm_device *dev,
3441 void *data, struct drm_file *file_priv)
3443 struct drm_framebuffer *fb = NULL;
3444 struct drm_framebuffer *fbl = NULL;
3445 uint32_t *id = data;
3448 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3451 fb = drm_framebuffer_lookup(dev, *id);
3455 mutex_lock(&file_priv->fbs_lock);
3456 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3460 mutex_unlock(&file_priv->fbs_lock);
3464 list_del_init(&fb->filp_head);
3465 mutex_unlock(&file_priv->fbs_lock);
3467 /* drop the reference we picked up in framebuffer lookup */
3468 drm_framebuffer_unreference(fb);
3471 * we now own the reference that was stored in the fbs list
3473 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3474 * so run this in a separate stack as there's no way to correctly
3475 * handle this after the fb is already removed from the lookup table.
3477 if (drm_framebuffer_read_refcount(fb) > 1) {
3478 struct drm_mode_rmfb_work arg;
3480 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3481 INIT_LIST_HEAD(&arg.fbs);
3482 list_add_tail(&fb->filp_head, &arg.fbs);
3484 schedule_work(&arg.work);
3485 flush_work(&arg.work);
3486 destroy_work_on_stack(&arg.work);
3488 drm_framebuffer_unreference(fb);
3493 drm_framebuffer_unreference(fb);
3498 * drm_mode_getfb - get FB info
3499 * @dev: drm device for the ioctl
3500 * @data: data pointer for the ioctl
3501 * @file_priv: drm file for the ioctl call
3503 * Lookup the FB given its ID and return info about it.
3505 * Called by the user via ioctl.
3508 * Zero on success, negative errno on failure.
3510 int drm_mode_getfb(struct drm_device *dev,
3511 void *data, struct drm_file *file_priv)
3513 struct drm_mode_fb_cmd *r = data;
3514 struct drm_framebuffer *fb;
3517 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3520 fb = drm_framebuffer_lookup(dev, r->fb_id);
3524 r->height = fb->height;
3525 r->width = fb->width;
3526 r->depth = fb->depth;
3527 r->bpp = fb->bits_per_pixel;
3528 r->pitch = fb->pitches[0];
3529 if (fb->funcs->create_handle) {
3530 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3531 drm_is_control_client(file_priv)) {
3532 ret = fb->funcs->create_handle(fb, file_priv,
3535 /* GET_FB() is an unprivileged ioctl so we must not
3536 * return a buffer-handle to non-master processes! For
3537 * backwards-compatibility reasons, we cannot make
3538 * GET_FB() privileged, so just return an invalid handle
3539 * for non-masters. */
3547 drm_framebuffer_unreference(fb);
3553 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3554 * @dev: drm device for the ioctl
3555 * @data: data pointer for the ioctl
3556 * @file_priv: drm file for the ioctl call
3558 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3559 * rectangle list. Generic userspace which does frontbuffer rendering must call
3560 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3561 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3563 * Modesetting drivers which always update the frontbuffer do not need to
3564 * implement the corresponding ->dirty framebuffer callback.
3566 * Called by the user via ioctl.
3569 * Zero on success, negative errno on failure.
3571 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3572 void *data, struct drm_file *file_priv)
3574 struct drm_clip_rect __user *clips_ptr;
3575 struct drm_clip_rect *clips = NULL;
3576 struct drm_mode_fb_dirty_cmd *r = data;
3577 struct drm_framebuffer *fb;
3582 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3585 fb = drm_framebuffer_lookup(dev, r->fb_id);
3589 num_clips = r->num_clips;
3590 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3592 if (!num_clips != !clips_ptr) {
3597 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3599 /* If userspace annotates copy, clips must come in pairs */
3600 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3605 if (num_clips && clips_ptr) {
3606 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3610 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3616 ret = copy_from_user(clips, clips_ptr,
3617 num_clips * sizeof(*clips));
3624 if (fb->funcs->dirty) {
3625 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3634 drm_framebuffer_unreference(fb);
3640 * drm_fb_release - remove and free the FBs on this file
3641 * @priv: drm file for the ioctl
3643 * Destroy all the FBs associated with @filp.
3645 * Called by the user via ioctl.
3648 * Zero on success, negative errno on failure.
3650 void drm_fb_release(struct drm_file *priv)
3652 struct drm_framebuffer *fb, *tfb;
3653 struct drm_mode_rmfb_work arg;
3655 INIT_LIST_HEAD(&arg.fbs);
3658 * When the file gets released that means no one else can access the fb
3659 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3660 * avoid upsetting lockdep since the universal cursor code adds a
3661 * framebuffer while holding mutex locks.
3663 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3664 * locks is impossible here since no one else but this function can get
3667 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3668 if (drm_framebuffer_read_refcount(fb) > 1) {
3669 list_move_tail(&fb->filp_head, &arg.fbs);
3671 list_del_init(&fb->filp_head);
3673 /* This drops the fpriv->fbs reference. */
3674 drm_framebuffer_unreference(fb);
3678 if (!list_empty(&arg.fbs)) {
3679 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3681 schedule_work(&arg.work);
3682 flush_work(&arg.work);
3683 destroy_work_on_stack(&arg.work);
3688 * drm_property_create - create a new property type
3690 * @flags: flags specifying the property type
3691 * @name: name of the property
3692 * @num_values: number of pre-defined values
3694 * This creates a new generic drm property which can then be attached to a drm
3695 * object with drm_object_attach_property. The returned property object must be
3696 * freed with drm_property_destroy.
3698 * Note that the DRM core keeps a per-device list of properties and that, if
3699 * drm_mode_config_cleanup() is called, it will destroy all properties created
3703 * A pointer to the newly created property on success, NULL on failure.
3705 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3706 const char *name, int num_values)
3708 struct drm_property *property = NULL;
3711 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3715 property->dev = dev;
3718 property->values = kcalloc(num_values, sizeof(uint64_t),
3720 if (!property->values)
3724 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3728 property->flags = flags;
3729 property->num_values = num_values;
3730 INIT_LIST_HEAD(&property->enum_list);
3733 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3734 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3737 list_add_tail(&property->head, &dev->mode_config.property_list);
3739 WARN_ON(!drm_property_type_valid(property));
3743 kfree(property->values);
3747 EXPORT_SYMBOL(drm_property_create);
3750 * drm_property_create_enum - create a new enumeration property type
3752 * @flags: flags specifying the property type
3753 * @name: name of the property
3754 * @props: enumeration lists with property values
3755 * @num_values: number of pre-defined values
3757 * This creates a new generic drm property which can then be attached to a drm
3758 * object with drm_object_attach_property. The returned property object must be
3759 * freed with drm_property_destroy.
3761 * Userspace is only allowed to set one of the predefined values for enumeration
3765 * A pointer to the newly created property on success, NULL on failure.
3767 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3769 const struct drm_prop_enum_list *props,
3772 struct drm_property *property;
3775 flags |= DRM_MODE_PROP_ENUM;
3777 property = drm_property_create(dev, flags, name, num_values);
3781 for (i = 0; i < num_values; i++) {
3782 ret = drm_property_add_enum(property, i,
3786 drm_property_destroy(dev, property);
3793 EXPORT_SYMBOL(drm_property_create_enum);
3796 * drm_property_create_bitmask - create a new bitmask property type
3798 * @flags: flags specifying the property type
3799 * @name: name of the property
3800 * @props: enumeration lists with property bitflags
3801 * @num_props: size of the @props array
3802 * @supported_bits: bitmask of all supported enumeration values
3804 * This creates a new bitmask drm property which can then be attached to a drm
3805 * object with drm_object_attach_property. The returned property object must be
3806 * freed with drm_property_destroy.
3808 * Compared to plain enumeration properties userspace is allowed to set any
3809 * or'ed together combination of the predefined property bitflag values
3812 * A pointer to the newly created property on success, NULL on failure.
3814 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3815 int flags, const char *name,
3816 const struct drm_prop_enum_list *props,
3818 uint64_t supported_bits)
3820 struct drm_property *property;
3821 int i, ret, index = 0;
3822 int num_values = hweight64(supported_bits);
3824 flags |= DRM_MODE_PROP_BITMASK;
3826 property = drm_property_create(dev, flags, name, num_values);
3829 for (i = 0; i < num_props; i++) {
3830 if (!(supported_bits & (1ULL << props[i].type)))
3833 if (WARN_ON(index >= num_values)) {
3834 drm_property_destroy(dev, property);
3838 ret = drm_property_add_enum(property, index++,
3842 drm_property_destroy(dev, property);
3849 EXPORT_SYMBOL(drm_property_create_bitmask);
3851 static struct drm_property *property_create_range(struct drm_device *dev,
3852 int flags, const char *name,
3853 uint64_t min, uint64_t max)
3855 struct drm_property *property;
3857 property = drm_property_create(dev, flags, name, 2);
3861 property->values[0] = min;
3862 property->values[1] = max;
3868 * drm_property_create_range - create a new unsigned ranged property type
3870 * @flags: flags specifying the property type
3871 * @name: name of the property
3872 * @min: minimum value of the property
3873 * @max: maximum value of the property
3875 * This creates a new generic drm property which can then be attached to a drm
3876 * object with drm_object_attach_property. The returned property object must be
3877 * freed with drm_property_destroy.
3879 * Userspace is allowed to set any unsigned integer value in the (min, max)
3883 * A pointer to the newly created property on success, NULL on failure.
3885 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3887 uint64_t min, uint64_t max)
3889 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3892 EXPORT_SYMBOL(drm_property_create_range);
3895 * drm_property_create_signed_range - create a new signed ranged property type
3897 * @flags: flags specifying the property type
3898 * @name: name of the property
3899 * @min: minimum value of the property
3900 * @max: maximum value of the property
3902 * This creates a new generic drm property which can then be attached to a drm
3903 * object with drm_object_attach_property. The returned property object must be
3904 * freed with drm_property_destroy.
3906 * Userspace is allowed to set any signed integer value in the (min, max)
3910 * A pointer to the newly created property on success, NULL on failure.
3912 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3913 int flags, const char *name,
3914 int64_t min, int64_t max)
3916 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3917 name, I642U64(min), I642U64(max));
3919 EXPORT_SYMBOL(drm_property_create_signed_range);
3922 * drm_property_create_object - create a new object property type
3924 * @flags: flags specifying the property type
3925 * @name: name of the property
3926 * @type: object type from DRM_MODE_OBJECT_* defines
3928 * This creates a new generic drm property which can then be attached to a drm
3929 * object with drm_object_attach_property. The returned property object must be
3930 * freed with drm_property_destroy.
3932 * Userspace is only allowed to set this to any property value of the given
3933 * @type. Only useful for atomic properties, which is enforced.
3936 * A pointer to the newly created property on success, NULL on failure.
3938 struct drm_property *drm_property_create_object(struct drm_device *dev,
3939 int flags, const char *name, uint32_t type)
3941 struct drm_property *property;
3943 flags |= DRM_MODE_PROP_OBJECT;
3945 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3948 property = drm_property_create(dev, flags, name, 1);
3952 property->values[0] = type;
3956 EXPORT_SYMBOL(drm_property_create_object);
3959 * drm_property_create_bool - create a new boolean property type
3961 * @flags: flags specifying the property type
3962 * @name: name of the property
3964 * This creates a new generic drm property which can then be attached to a drm
3965 * object with drm_object_attach_property. The returned property object must be
3966 * freed with drm_property_destroy.
3968 * This is implemented as a ranged property with only {0, 1} as valid values.
3971 * A pointer to the newly created property on success, NULL on failure.
3973 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3976 return drm_property_create_range(dev, flags, name, 0, 1);
3978 EXPORT_SYMBOL(drm_property_create_bool);
3981 * drm_property_add_enum - add a possible value to an enumeration property
3982 * @property: enumeration property to change
3983 * @index: index of the new enumeration
3984 * @value: value of the new enumeration
3985 * @name: symbolic name of the new enumeration
3987 * This functions adds enumerations to a property.
3989 * It's use is deprecated, drivers should use one of the more specific helpers
3990 * to directly create the property with all enumerations already attached.
3993 * Zero on success, error code on failure.
3995 int drm_property_add_enum(struct drm_property *property, int index,
3996 uint64_t value, const char *name)
3998 struct drm_property_enum *prop_enum;
4000 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4001 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
4005 * Bitmask enum properties have the additional constraint of values
4008 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
4012 if (!list_empty(&property->enum_list)) {
4013 list_for_each_entry(prop_enum, &property->enum_list, head) {
4014 if (prop_enum->value == value) {
4015 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4016 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4022 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4026 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4027 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4028 prop_enum->value = value;
4030 property->values[index] = value;
4031 list_add_tail(&prop_enum->head, &property->enum_list);
4034 EXPORT_SYMBOL(drm_property_add_enum);
4037 * drm_property_destroy - destroy a drm property
4039 * @property: property to destry
4041 * This function frees a property including any attached resources like
4042 * enumeration values.
4044 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4046 struct drm_property_enum *prop_enum, *pt;
4048 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4049 list_del(&prop_enum->head);
4053 if (property->num_values)
4054 kfree(property->values);
4055 drm_mode_object_unregister(dev, &property->base);
4056 list_del(&property->head);
4059 EXPORT_SYMBOL(drm_property_destroy);
4062 * drm_object_attach_property - attach a property to a modeset object
4063 * @obj: drm modeset object
4064 * @property: property to attach
4065 * @init_val: initial value of the property
4067 * This attaches the given property to the modeset object with the given initial
4068 * value. Currently this function cannot fail since the properties are stored in
4069 * a statically sized array.
4071 void drm_object_attach_property(struct drm_mode_object *obj,
4072 struct drm_property *property,
4075 int count = obj->properties->count;
4077 if (count == DRM_OBJECT_MAX_PROPERTY) {
4078 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4079 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4080 "you see this message on the same object type.\n",
4085 obj->properties->properties[count] = property;
4086 obj->properties->values[count] = init_val;
4087 obj->properties->count++;
4088 if (property->flags & DRM_MODE_PROP_ATOMIC)
4089 obj->properties->atomic_count++;
4091 EXPORT_SYMBOL(drm_object_attach_property);
4094 * drm_object_property_set_value - set the value of a property
4095 * @obj: drm mode object to set property value for
4096 * @property: property to set
4097 * @val: value the property should be set to
4099 * This functions sets a given property on a given object. This function only
4100 * changes the software state of the property, it does not call into the
4101 * driver's ->set_property callback.
4104 * Zero on success, error code on failure.
4106 int drm_object_property_set_value(struct drm_mode_object *obj,
4107 struct drm_property *property, uint64_t val)
4111 for (i = 0; i < obj->properties->count; i++) {
4112 if (obj->properties->properties[i] == property) {
4113 obj->properties->values[i] = val;
4120 EXPORT_SYMBOL(drm_object_property_set_value);
4123 * drm_object_property_get_value - retrieve the value of a property
4124 * @obj: drm mode object to get property value from
4125 * @property: property to retrieve
4126 * @val: storage for the property value
4128 * This function retrieves the softare state of the given property for the given
4129 * property. Since there is no driver callback to retrieve the current property
4130 * value this might be out of sync with the hardware, depending upon the driver
4134 * Zero on success, error code on failure.
4136 int drm_object_property_get_value(struct drm_mode_object *obj,
4137 struct drm_property *property, uint64_t *val)
4141 /* read-only properties bypass atomic mechanism and still store
4142 * their value in obj->properties->values[].. mostly to avoid
4143 * having to deal w/ EDID and similar props in atomic paths:
4145 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4146 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4147 return drm_atomic_get_property(obj, property, val);
4149 for (i = 0; i < obj->properties->count; i++) {
4150 if (obj->properties->properties[i] == property) {
4151 *val = obj->properties->values[i];
4158 EXPORT_SYMBOL(drm_object_property_get_value);
4161 * drm_mode_getproperty_ioctl - get the property metadata
4164 * @file_priv: DRM file info
4166 * This function retrieves the metadata for a given property, like the different
4167 * possible values for an enum property or the limits for a range property.
4169 * Blob properties are special
4171 * Called by the user via ioctl.
4174 * Zero on success, negative errno on failure.
4176 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4177 void *data, struct drm_file *file_priv)
4179 struct drm_mode_get_property *out_resp = data;
4180 struct drm_property *property;
4182 int value_count = 0;
4185 struct drm_property_enum *prop_enum;
4186 struct drm_mode_property_enum __user *enum_ptr;
4187 uint64_t __user *values_ptr;
4189 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4192 drm_modeset_lock_all(dev);
4193 property = drm_property_find(dev, out_resp->prop_id);
4199 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4200 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4201 list_for_each_entry(prop_enum, &property->enum_list, head)
4205 value_count = property->num_values;
4207 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4208 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4209 out_resp->flags = property->flags;
4211 if ((out_resp->count_values >= value_count) && value_count) {
4212 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4213 for (i = 0; i < value_count; i++) {
4214 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4220 out_resp->count_values = value_count;
4222 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4223 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4224 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4226 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4227 list_for_each_entry(prop_enum, &property->enum_list, head) {
4229 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4234 if (copy_to_user(&enum_ptr[copied].name,
4235 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4242 out_resp->count_enum_blobs = enum_count;
4246 * NOTE: The idea seems to have been to use this to read all the blob
4247 * property values. But nothing ever added them to the corresponding
4248 * list, userspace always used the special-purpose get_blob ioctl to
4249 * read the value for a blob property. It also doesn't make a lot of
4250 * sense to return values here when everything else is just metadata for
4251 * the property itself.
4253 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4254 out_resp->count_enum_blobs = 0;
4256 drm_modeset_unlock_all(dev);
4260 static void drm_property_free_blob(struct kref *kref)
4262 struct drm_property_blob *blob =
4263 container_of(kref, struct drm_property_blob, base.refcount);
4265 mutex_lock(&blob->dev->mode_config.blob_lock);
4266 list_del(&blob->head_global);
4267 mutex_unlock(&blob->dev->mode_config.blob_lock);
4269 drm_mode_object_unregister(blob->dev, &blob->base);
4275 * drm_property_create_blob - Create new blob property
4277 * Creates a new blob property for a specified DRM device, optionally
4280 * @dev: DRM device to create property for
4281 * @length: Length to allocate for blob data
4282 * @data: If specified, copies data into blob
4285 * New blob property with a single reference on success, or an ERR_PTR
4288 struct drm_property_blob *
4289 drm_property_create_blob(struct drm_device *dev, size_t length,
4292 struct drm_property_blob *blob;
4295 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4296 return ERR_PTR(-EINVAL);
4298 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4300 return ERR_PTR(-ENOMEM);
4302 /* This must be explicitly initialised, so we can safely call list_del
4303 * on it in the removal handler, even if it isn't in a file list. */
4304 INIT_LIST_HEAD(&blob->head_file);
4305 blob->length = length;
4309 memcpy(blob->data, data, length);
4311 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4312 true, drm_property_free_blob);
4315 return ERR_PTR(-EINVAL);
4318 mutex_lock(&dev->mode_config.blob_lock);
4319 list_add_tail(&blob->head_global,
4320 &dev->mode_config.property_blob_list);
4321 mutex_unlock(&dev->mode_config.blob_lock);
4325 EXPORT_SYMBOL(drm_property_create_blob);
4328 * drm_property_unreference_blob - Unreference a blob property
4330 * Drop a reference on a blob property. May free the object.
4332 * @blob: Pointer to blob property
4334 void drm_property_unreference_blob(struct drm_property_blob *blob)
4339 drm_mode_object_unreference(&blob->base);
4341 EXPORT_SYMBOL(drm_property_unreference_blob);
4344 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4346 * @file_priv: destroy all blobs owned by this file handle
4348 void drm_property_destroy_user_blobs(struct drm_device *dev,
4349 struct drm_file *file_priv)
4351 struct drm_property_blob *blob, *bt;
4354 * When the file gets released that means no one else can access the
4355 * blob list any more, so no need to grab dev->blob_lock.
4357 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4358 list_del_init(&blob->head_file);
4359 drm_property_unreference_blob(blob);
4364 * drm_property_reference_blob - Take a reference on an existing property
4366 * Take a new reference on an existing blob property.
4368 * @blob: Pointer to blob property
4370 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4372 drm_mode_object_reference(&blob->base);
4375 EXPORT_SYMBOL(drm_property_reference_blob);
4378 * drm_property_lookup_blob - look up a blob property and take a reference
4380 * @id: id of the blob property
4382 * If successful, this takes an additional reference to the blob property.
4383 * callers need to make sure to eventually unreference the returned property
4384 * again, using @drm_property_unreference_blob.
4386 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4389 struct drm_mode_object *obj;
4390 struct drm_property_blob *blob = NULL;
4392 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4394 blob = obj_to_blob(obj);
4397 EXPORT_SYMBOL(drm_property_lookup_blob);
4400 * drm_property_replace_global_blob - atomically replace existing blob property
4402 * @replace: location of blob property pointer to be replaced
4403 * @length: length of data for new blob, or 0 for no data
4404 * @data: content for new blob, or NULL for no data
4405 * @obj_holds_id: optional object for property holding blob ID
4406 * @prop_holds_id: optional property holding blob ID
4407 * @return 0 on success or error on failure
4409 * This function will atomically replace a global property in the blob list,
4410 * optionally updating a property which holds the ID of that property. It is
4411 * guaranteed to be atomic: no caller will be allowed to see intermediate
4412 * results, and either the entire operation will succeed and clean up the
4413 * previous property, or it will fail and the state will be unchanged.
4415 * If length is 0 or data is NULL, no new blob will be created, and the holding
4416 * property, if specified, will be set to 0.
4418 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4419 * by holding the relevant modesetting object lock for its parent.
4421 * For example, a drm_connector has a 'PATH' property, which contains the ID
4422 * of a blob property with the value of the MST path information. Calling this
4423 * function with replace pointing to the connector's path_blob_ptr, length and
4424 * data set for the new path information, obj_holds_id set to the connector's
4425 * base object, and prop_holds_id set to the path property name, will perform
4426 * a completely atomic update. The access to path_blob_ptr is protected by the
4427 * caller holding a lock on the connector.
4429 static int drm_property_replace_global_blob(struct drm_device *dev,
4430 struct drm_property_blob **replace,
4433 struct drm_mode_object *obj_holds_id,
4434 struct drm_property *prop_holds_id)
4436 struct drm_property_blob *new_blob = NULL;
4437 struct drm_property_blob *old_blob = NULL;
4440 WARN_ON(replace == NULL);
4442 old_blob = *replace;
4444 if (length && data) {
4445 new_blob = drm_property_create_blob(dev, length, data);
4446 if (IS_ERR(new_blob))
4447 return PTR_ERR(new_blob);
4450 /* This does not need to be synchronised with blob_lock, as the
4451 * get_properties ioctl locks all modesetting objects, and
4452 * obj_holds_id must be locked before calling here, so we cannot
4453 * have its value out of sync with the list membership modified
4454 * below under blob_lock. */
4456 ret = drm_object_property_set_value(obj_holds_id,
4459 new_blob->base.id : 0);
4464 drm_property_unreference_blob(old_blob);
4465 *replace = new_blob;
4470 drm_property_unreference_blob(new_blob);
4475 * drm_mode_getblob_ioctl - get the contents of a blob property value
4478 * @file_priv: DRM file info
4480 * This function retrieves the contents of a blob property. The value stored in
4481 * an object's blob property is just a normal modeset object id.
4483 * Called by the user via ioctl.
4486 * Zero on success, negative errno on failure.
4488 int drm_mode_getblob_ioctl(struct drm_device *dev,
4489 void *data, struct drm_file *file_priv)
4491 struct drm_mode_get_blob *out_resp = data;
4492 struct drm_property_blob *blob;
4494 void __user *blob_ptr;
4496 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4499 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4503 if (out_resp->length == blob->length) {
4504 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4505 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4510 out_resp->length = blob->length;
4512 drm_property_unreference_blob(blob);
4518 * drm_mode_createblob_ioctl - create a new blob property
4521 * @file_priv: DRM file info
4523 * This function creates a new blob property with user-defined values. In order
4524 * to give us sensible validation and checking when creating, rather than at
4525 * every potential use, we also require a type to be provided upfront.
4527 * Called by the user via ioctl.
4530 * Zero on success, negative errno on failure.
4532 int drm_mode_createblob_ioctl(struct drm_device *dev,
4533 void *data, struct drm_file *file_priv)
4535 struct drm_mode_create_blob *out_resp = data;
4536 struct drm_property_blob *blob;
4537 void __user *blob_ptr;
4540 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4543 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4545 return PTR_ERR(blob);
4547 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4548 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4553 /* Dropping the lock between create_blob and our access here is safe
4554 * as only the same file_priv can remove the blob; at this point, it is
4555 * not associated with any file_priv. */
4556 mutex_lock(&dev->mode_config.blob_lock);
4557 out_resp->blob_id = blob->base.id;
4558 list_add_tail(&blob->head_file, &file_priv->blobs);
4559 mutex_unlock(&dev->mode_config.blob_lock);
4564 drm_property_unreference_blob(blob);
4569 * drm_mode_destroyblob_ioctl - destroy a user blob property
4572 * @file_priv: DRM file info
4574 * Destroy an existing user-defined blob property.
4576 * Called by the user via ioctl.
4579 * Zero on success, negative errno on failure.
4581 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4582 void *data, struct drm_file *file_priv)
4584 struct drm_mode_destroy_blob *out_resp = data;
4585 struct drm_property_blob *blob = NULL, *bt;
4589 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4592 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4596 mutex_lock(&dev->mode_config.blob_lock);
4597 /* Ensure the property was actually created by this user. */
4598 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4610 /* We must drop head_file here, because we may not be the last
4611 * reference on the blob. */
4612 list_del_init(&blob->head_file);
4613 mutex_unlock(&dev->mode_config.blob_lock);
4615 /* One reference from lookup, and one from the filp. */
4616 drm_property_unreference_blob(blob);
4617 drm_property_unreference_blob(blob);
4622 mutex_unlock(&dev->mode_config.blob_lock);
4623 drm_property_unreference_blob(blob);
4629 * drm_mode_connector_set_path_property - set tile property on connector
4630 * @connector: connector to set property on.
4631 * @path: path to use for property; must not be NULL.
4633 * This creates a property to expose to userspace to specify a
4634 * connector path. This is mainly used for DisplayPort MST where
4635 * connectors have a topology and we want to allow userspace to give
4636 * them more meaningful names.
4639 * Zero on success, negative errno on failure.
4641 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4644 struct drm_device *dev = connector->dev;
4647 ret = drm_property_replace_global_blob(dev,
4648 &connector->path_blob_ptr,
4652 dev->mode_config.path_property);
4655 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4658 * drm_mode_connector_set_tile_property - set tile property on connector
4659 * @connector: connector to set property on.
4661 * This looks up the tile information for a connector, and creates a
4662 * property for userspace to parse if it exists. The property is of
4663 * the form of 8 integers using ':' as a separator.
4666 * Zero on success, errno on failure.
4668 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4670 struct drm_device *dev = connector->dev;
4674 if (!connector->has_tile) {
4675 ret = drm_property_replace_global_blob(dev,
4676 &connector->tile_blob_ptr,
4680 dev->mode_config.tile_property);
4684 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4685 connector->tile_group->id, connector->tile_is_single_monitor,
4686 connector->num_h_tile, connector->num_v_tile,
4687 connector->tile_h_loc, connector->tile_v_loc,
4688 connector->tile_h_size, connector->tile_v_size);
4690 ret = drm_property_replace_global_blob(dev,
4691 &connector->tile_blob_ptr,
4695 dev->mode_config.tile_property);
4698 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4701 * drm_mode_connector_update_edid_property - update the edid property of a connector
4702 * @connector: drm connector
4703 * @edid: new value of the edid property
4705 * This function creates a new blob modeset object and assigns its id to the
4706 * connector's edid property.
4709 * Zero on success, negative errno on failure.
4711 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4712 const struct edid *edid)
4714 struct drm_device *dev = connector->dev;
4718 /* ignore requests to set edid when overridden */
4719 if (connector->override_edid)
4723 size = EDID_LENGTH * (1 + edid->extensions);
4725 ret = drm_property_replace_global_blob(dev,
4726 &connector->edid_blob_ptr,
4730 dev->mode_config.edid_property);
4733 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4735 /* Some properties could refer to dynamic refcnt'd objects, or things that
4736 * need special locking to handle lifetime issues (ie. to ensure the prop
4737 * value doesn't become invalid part way through the property update due to
4738 * race). The value returned by reference via 'obj' should be passed back
4739 * to drm_property_change_valid_put() after the property is set (and the
4740 * object to which the property is attached has a chance to take it's own
4743 bool drm_property_change_valid_get(struct drm_property *property,
4744 uint64_t value, struct drm_mode_object **ref)
4748 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4753 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4754 if (value < property->values[0] || value > property->values[1])
4757 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4758 int64_t svalue = U642I64(value);
4760 if (svalue < U642I64(property->values[0]) ||
4761 svalue > U642I64(property->values[1]))
4764 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4765 uint64_t valid_mask = 0;
4767 for (i = 0; i < property->num_values; i++)
4768 valid_mask |= (1ULL << property->values[i]);
4769 return !(value & ~valid_mask);
4770 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4771 struct drm_property_blob *blob;
4776 blob = drm_property_lookup_blob(property->dev, value);
4783 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4784 /* a zero value for an object property translates to null: */
4788 return _object_find(property->dev, value, property->values[0]) != NULL;
4791 for (i = 0; i < property->num_values; i++)
4792 if (property->values[i] == value)
4797 void drm_property_change_valid_put(struct drm_property *property,
4798 struct drm_mode_object *ref)
4803 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4804 drm_mode_object_unreference(ref);
4805 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4806 drm_property_unreference_blob(obj_to_blob(ref));
4810 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4813 * @file_priv: DRM file info
4815 * This function sets the current value for a connectors's property. It also
4816 * calls into a driver's ->set_property callback to update the hardware state
4818 * Called by the user via ioctl.
4821 * Zero on success, negative errno on failure.
4823 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4824 void *data, struct drm_file *file_priv)
4826 struct drm_mode_connector_set_property *conn_set_prop = data;
4827 struct drm_mode_obj_set_property obj_set_prop = {
4828 .value = conn_set_prop->value,
4829 .prop_id = conn_set_prop->prop_id,
4830 .obj_id = conn_set_prop->connector_id,
4831 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4834 /* It does all the locking and checking we need */
4835 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4838 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4839 struct drm_property *property,
4843 struct drm_connector *connector = obj_to_connector(obj);
4845 /* Do DPMS ourselves */
4846 if (property == connector->dev->mode_config.dpms_property) {
4847 ret = (*connector->funcs->dpms)(connector, (int)value);
4848 } else if (connector->funcs->set_property)
4849 ret = connector->funcs->set_property(connector, property, value);
4851 /* store the property value if successful */
4853 drm_object_property_set_value(&connector->base, property, value);
4857 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4858 struct drm_property *property,
4862 struct drm_crtc *crtc = obj_to_crtc(obj);
4864 if (crtc->funcs->set_property)
4865 ret = crtc->funcs->set_property(crtc, property, value);
4867 drm_object_property_set_value(obj, property, value);
4873 * drm_mode_plane_set_obj_prop - set the value of a property
4874 * @plane: drm plane object to set property value for
4875 * @property: property to set
4876 * @value: value the property should be set to
4878 * This functions sets a given property on a given plane object. This function
4879 * calls the driver's ->set_property callback and changes the software state of
4880 * the property if the callback succeeds.
4883 * Zero on success, error code on failure.
4885 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4886 struct drm_property *property,
4890 struct drm_mode_object *obj = &plane->base;
4892 if (plane->funcs->set_property)
4893 ret = plane->funcs->set_property(plane, property, value);
4895 drm_object_property_set_value(obj, property, value);
4899 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4902 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4905 * @file_priv: DRM file info
4907 * This function retrieves the current value for an object's property. Compared
4908 * to the connector specific ioctl this one is extended to also work on crtc and
4911 * Called by the user via ioctl.
4914 * Zero on success, negative errno on failure.
4916 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4917 struct drm_file *file_priv)
4919 struct drm_mode_obj_get_properties *arg = data;
4920 struct drm_mode_object *obj;
4923 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4926 drm_modeset_lock_all(dev);
4928 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4933 if (!obj->properties) {
4938 ret = get_properties(obj, file_priv->atomic,
4939 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4940 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4944 drm_mode_object_unreference(obj);
4946 drm_modeset_unlock_all(dev);
4951 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4954 * @file_priv: DRM file info
4956 * This function sets the current value for an object's property. It also calls
4957 * into a driver's ->set_property callback to update the hardware state.
4958 * Compared to the connector specific ioctl this one is extended to also work on
4959 * crtc and plane objects.
4961 * Called by the user via ioctl.
4964 * Zero on success, negative errno on failure.
4966 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4967 struct drm_file *file_priv)
4969 struct drm_mode_obj_set_property *arg = data;
4970 struct drm_mode_object *arg_obj;
4971 struct drm_mode_object *prop_obj;
4972 struct drm_property *property;
4973 int i, ret = -EINVAL;
4974 struct drm_mode_object *ref;
4976 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4979 drm_modeset_lock_all(dev);
4981 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4986 if (!arg_obj->properties)
4989 for (i = 0; i < arg_obj->properties->count; i++)
4990 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
4993 if (i == arg_obj->properties->count)
4996 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4997 DRM_MODE_OBJECT_PROPERTY);
5002 property = obj_to_property(prop_obj);
5004 if (!drm_property_change_valid_get(property, arg->value, &ref))
5007 switch (arg_obj->type) {
5008 case DRM_MODE_OBJECT_CONNECTOR:
5009 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
5012 case DRM_MODE_OBJECT_CRTC:
5013 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5015 case DRM_MODE_OBJECT_PLANE:
5016 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5017 property, arg->value);
5021 drm_property_change_valid_put(property, ref);
5024 drm_mode_object_unreference(arg_obj);
5026 drm_modeset_unlock_all(dev);
5031 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5032 * @connector: connector to attach
5033 * @encoder: encoder to attach @connector to
5035 * This function links up a connector to an encoder. Note that the routing
5036 * restrictions between encoders and crtcs are exposed to userspace through the
5037 * possible_clones and possible_crtcs bitmasks.
5040 * Zero on success, negative errno on failure.
5042 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5043 struct drm_encoder *encoder)
5048 * In the past, drivers have attempted to model the static association
5049 * of connector to encoder in simple connector/encoder devices using a
5050 * direct assignment of connector->encoder = encoder. This connection
5051 * is a logical one and the responsibility of the core, so drivers are
5052 * expected not to mess with this.
5054 * Note that the error return should've been enough here, but a large
5055 * majority of drivers ignores the return value, so add in a big WARN
5056 * to get people's attention.
5058 if (WARN_ON(connector->encoder))
5061 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5062 if (connector->encoder_ids[i] == 0) {
5063 connector->encoder_ids[i] = encoder->base.id;
5069 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5072 * drm_mode_crtc_set_gamma_size - set the gamma table size
5073 * @crtc: CRTC to set the gamma table size for
5074 * @gamma_size: size of the gamma table
5076 * Drivers which support gamma tables should set this to the supported gamma
5077 * table size when initializing the CRTC. Currently the drm core only supports a
5078 * fixed gamma table size.
5081 * Zero on success, negative errno on failure.
5083 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5086 uint16_t *r_base, *g_base, *b_base;
5089 crtc->gamma_size = gamma_size;
5091 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5093 if (!crtc->gamma_store) {
5094 crtc->gamma_size = 0;
5098 r_base = crtc->gamma_store;
5099 g_base = r_base + gamma_size;
5100 b_base = g_base + gamma_size;
5101 for (i = 0; i < gamma_size; i++) {
5110 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5113 * drm_mode_gamma_set_ioctl - set the gamma table
5116 * @file_priv: DRM file info
5118 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5119 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5121 * Called by the user via ioctl.
5124 * Zero on success, negative errno on failure.
5126 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5127 void *data, struct drm_file *file_priv)
5129 struct drm_mode_crtc_lut *crtc_lut = data;
5130 struct drm_crtc *crtc;
5131 void *r_base, *g_base, *b_base;
5135 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5138 drm_modeset_lock_all(dev);
5139 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5145 if (crtc->funcs->gamma_set == NULL) {
5150 /* memcpy into gamma store */
5151 if (crtc_lut->gamma_size != crtc->gamma_size) {
5156 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5157 r_base = crtc->gamma_store;
5158 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5163 g_base = r_base + size;
5164 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5169 b_base = g_base + size;
5170 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5175 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
5178 drm_modeset_unlock_all(dev);
5184 * drm_mode_gamma_get_ioctl - get the gamma table
5187 * @file_priv: DRM file info
5189 * Copy the current gamma table into the storage provided. This also provides
5190 * the gamma table size the driver expects, which can be used to size the
5191 * allocated storage.
5193 * Called by the user via ioctl.
5196 * Zero on success, negative errno on failure.
5198 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5199 void *data, struct drm_file *file_priv)
5201 struct drm_mode_crtc_lut *crtc_lut = data;
5202 struct drm_crtc *crtc;
5203 void *r_base, *g_base, *b_base;
5207 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5210 drm_modeset_lock_all(dev);
5211 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5217 /* memcpy into gamma store */
5218 if (crtc_lut->gamma_size != crtc->gamma_size) {
5223 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5224 r_base = crtc->gamma_store;
5225 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5230 g_base = r_base + size;
5231 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5236 b_base = g_base + size;
5237 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5242 drm_modeset_unlock_all(dev);
5247 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5250 * @file_priv: DRM file info
5252 * This schedules an asynchronous update on a given CRTC, called page flip.
5253 * Optionally a drm event is generated to signal the completion of the event.
5254 * Generic drivers cannot assume that a pageflip with changed framebuffer
5255 * properties (including driver specific metadata like tiling layout) will work,
5256 * but some drivers support e.g. pixel format changes through the pageflip
5259 * Called by the user via ioctl.
5262 * Zero on success, negative errno on failure.
5264 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5265 void *data, struct drm_file *file_priv)
5267 struct drm_mode_crtc_page_flip *page_flip = data;
5268 struct drm_crtc *crtc;
5269 struct drm_framebuffer *fb = NULL;
5270 struct drm_pending_vblank_event *e = NULL;
5273 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5274 page_flip->reserved != 0)
5277 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5280 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5284 drm_modeset_lock_crtc(crtc, crtc->primary);
5285 if (crtc->primary->fb == NULL) {
5286 /* The framebuffer is currently unbound, presumably
5287 * due to a hotplug event, that userspace has not
5294 if (crtc->funcs->page_flip == NULL)
5297 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5304 const struct drm_plane_state *state = crtc->primary->state;
5306 ret = check_src_coords(state->src_x, state->src_y,
5307 state->src_w, state->src_h, fb);
5309 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5314 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5315 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5320 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5321 e = kzalloc(sizeof *e, GFP_KERNEL);
5326 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5327 e->event.base.length = sizeof(e->event);
5328 e->event.user_data = page_flip->user_data;
5329 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5336 crtc->primary->old_fb = crtc->primary->fb;
5337 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5339 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5340 drm_event_cancel_free(dev, &e->base);
5341 /* Keep the old fb, don't unref it. */
5342 crtc->primary->old_fb = NULL;
5344 crtc->primary->fb = fb;
5345 /* Unref only the old framebuffer. */
5351 drm_framebuffer_unreference(fb);
5352 if (crtc->primary->old_fb)
5353 drm_framebuffer_unreference(crtc->primary->old_fb);
5354 crtc->primary->old_fb = NULL;
5355 drm_modeset_unlock_crtc(crtc);
5361 * drm_mode_config_reset - call ->reset callbacks
5364 * This functions calls all the crtc's, encoder's and connector's ->reset
5365 * callback. Drivers can use this in e.g. their driver load or resume code to
5366 * reset hardware and software state.
5368 void drm_mode_config_reset(struct drm_device *dev)
5370 struct drm_crtc *crtc;
5371 struct drm_plane *plane;
5372 struct drm_encoder *encoder;
5373 struct drm_connector *connector;
5375 drm_for_each_plane(plane, dev)
5376 if (plane->funcs->reset)
5377 plane->funcs->reset(plane);
5379 drm_for_each_crtc(crtc, dev)
5380 if (crtc->funcs->reset)
5381 crtc->funcs->reset(crtc);
5383 drm_for_each_encoder(encoder, dev)
5384 if (encoder->funcs->reset)
5385 encoder->funcs->reset(encoder);
5387 mutex_lock(&dev->mode_config.mutex);
5388 drm_for_each_connector(connector, dev)
5389 if (connector->funcs->reset)
5390 connector->funcs->reset(connector);
5391 mutex_unlock(&dev->mode_config.mutex);
5393 EXPORT_SYMBOL(drm_mode_config_reset);
5396 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5399 * @file_priv: DRM file info
5401 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5402 * TTM or something else entirely) and returns the resulting buffer handle. This
5403 * handle can then be wrapped up into a framebuffer modeset object.
5405 * Note that userspace is not allowed to use such objects for render
5406 * acceleration - drivers must create their own private ioctls for such a use
5409 * Called by the user via ioctl.
5412 * Zero on success, negative errno on failure.
5414 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5415 void *data, struct drm_file *file_priv)
5417 struct drm_mode_create_dumb *args = data;
5418 u32 cpp, stride, size;
5420 if (!dev->driver->dumb_create)
5422 if (!args->width || !args->height || !args->bpp)
5425 /* overflow checks for 32bit size calculations */
5426 /* NOTE: DIV_ROUND_UP() can overflow */
5427 cpp = DIV_ROUND_UP(args->bpp, 8);
5428 if (!cpp || cpp > 0xffffffffU / args->width)
5430 stride = cpp * args->width;
5431 if (args->height > 0xffffffffU / stride)
5434 /* test for wrap-around */
5435 size = args->height * stride;
5436 if (PAGE_ALIGN(size) == 0)
5440 * handle, pitch and size are output parameters. Zero them out to
5441 * prevent drivers from accidentally using uninitialized data. Since
5442 * not all existing userspace is clearing these fields properly we
5443 * cannot reject IOCTL with garbage in them.
5449 return dev->driver->dumb_create(file_priv, dev, args);
5453 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5456 * @file_priv: DRM file info
5458 * Allocate an offset in the drm device node's address space to be able to
5459 * memory map a dumb buffer.
5461 * Called by the user via ioctl.
5464 * Zero on success, negative errno on failure.
5466 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5467 void *data, struct drm_file *file_priv)
5469 struct drm_mode_map_dumb *args = data;
5471 /* call driver ioctl to get mmap offset */
5472 if (!dev->driver->dumb_map_offset)
5475 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5479 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5482 * @file_priv: DRM file info
5484 * This destroys the userspace handle for the given dumb backing storage buffer.
5485 * Since buffer objects must be reference counted in the kernel a buffer object
5486 * won't be immediately freed if a framebuffer modeset object still uses it.
5488 * Called by the user via ioctl.
5491 * Zero on success, negative errno on failure.
5493 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5494 void *data, struct drm_file *file_priv)
5496 struct drm_mode_destroy_dumb *args = data;
5498 if (!dev->driver->dumb_destroy)
5501 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5505 * drm_fb_get_bpp_depth - get the bpp/depth values for format
5506 * @format: pixel format (DRM_FORMAT_*)
5507 * @depth: storage for the depth value
5508 * @bpp: storage for the bpp value
5510 * This only supports RGB formats here for compat with code that doesn't use
5511 * pixel formats directly yet.
5513 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5518 case DRM_FORMAT_RGB332:
5519 case DRM_FORMAT_BGR233:
5523 case DRM_FORMAT_XRGB1555:
5524 case DRM_FORMAT_XBGR1555:
5525 case DRM_FORMAT_RGBX5551:
5526 case DRM_FORMAT_BGRX5551:
5527 case DRM_FORMAT_ARGB1555:
5528 case DRM_FORMAT_ABGR1555:
5529 case DRM_FORMAT_RGBA5551:
5530 case DRM_FORMAT_BGRA5551:
5534 case DRM_FORMAT_RGB565:
5535 case DRM_FORMAT_BGR565:
5539 case DRM_FORMAT_RGB888:
5540 case DRM_FORMAT_BGR888:
5544 case DRM_FORMAT_XRGB8888:
5545 case DRM_FORMAT_XBGR8888:
5546 case DRM_FORMAT_RGBX8888:
5547 case DRM_FORMAT_BGRX8888:
5551 case DRM_FORMAT_XRGB2101010:
5552 case DRM_FORMAT_XBGR2101010:
5553 case DRM_FORMAT_RGBX1010102:
5554 case DRM_FORMAT_BGRX1010102:
5555 case DRM_FORMAT_ARGB2101010:
5556 case DRM_FORMAT_ABGR2101010:
5557 case DRM_FORMAT_RGBA1010102:
5558 case DRM_FORMAT_BGRA1010102:
5562 case DRM_FORMAT_ARGB8888:
5563 case DRM_FORMAT_ABGR8888:
5564 case DRM_FORMAT_RGBA8888:
5565 case DRM_FORMAT_BGRA8888:
5570 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5571 drm_get_format_name(format));
5577 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5580 * drm_format_num_planes - get the number of planes for format
5581 * @format: pixel format (DRM_FORMAT_*)
5584 * The number of planes used by the specified pixel format.
5586 int drm_format_num_planes(uint32_t format)
5589 case DRM_FORMAT_YUV410:
5590 case DRM_FORMAT_YVU410:
5591 case DRM_FORMAT_YUV411:
5592 case DRM_FORMAT_YVU411:
5593 case DRM_FORMAT_YUV420:
5594 case DRM_FORMAT_YVU420:
5595 case DRM_FORMAT_YUV422:
5596 case DRM_FORMAT_YVU422:
5597 case DRM_FORMAT_YUV444:
5598 case DRM_FORMAT_YVU444:
5600 case DRM_FORMAT_NV12:
5601 case DRM_FORMAT_NV21:
5602 case DRM_FORMAT_NV16:
5603 case DRM_FORMAT_NV61:
5604 case DRM_FORMAT_NV24:
5605 case DRM_FORMAT_NV42:
5611 EXPORT_SYMBOL(drm_format_num_planes);
5614 * drm_format_plane_cpp - determine the bytes per pixel value
5615 * @format: pixel format (DRM_FORMAT_*)
5616 * @plane: plane index
5619 * The bytes per pixel value for the specified plane.
5621 int drm_format_plane_cpp(uint32_t format, int plane)
5626 if (plane >= drm_format_num_planes(format))
5630 case DRM_FORMAT_YUYV:
5631 case DRM_FORMAT_YVYU:
5632 case DRM_FORMAT_UYVY:
5633 case DRM_FORMAT_VYUY:
5635 case DRM_FORMAT_NV12:
5636 case DRM_FORMAT_NV21:
5637 case DRM_FORMAT_NV16:
5638 case DRM_FORMAT_NV61:
5639 case DRM_FORMAT_NV24:
5640 case DRM_FORMAT_NV42:
5641 return plane ? 2 : 1;
5642 case DRM_FORMAT_YUV410:
5643 case DRM_FORMAT_YVU410:
5644 case DRM_FORMAT_YUV411:
5645 case DRM_FORMAT_YVU411:
5646 case DRM_FORMAT_YUV420:
5647 case DRM_FORMAT_YVU420:
5648 case DRM_FORMAT_YUV422:
5649 case DRM_FORMAT_YVU422:
5650 case DRM_FORMAT_YUV444:
5651 case DRM_FORMAT_YVU444:
5654 drm_fb_get_bpp_depth(format, &depth, &bpp);
5658 EXPORT_SYMBOL(drm_format_plane_cpp);
5661 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5662 * @format: pixel format (DRM_FORMAT_*)
5665 * The horizontal chroma subsampling factor for the
5666 * specified pixel format.
5668 int drm_format_horz_chroma_subsampling(uint32_t format)
5671 case DRM_FORMAT_YUV411:
5672 case DRM_FORMAT_YVU411:
5673 case DRM_FORMAT_YUV410:
5674 case DRM_FORMAT_YVU410:
5676 case DRM_FORMAT_YUYV:
5677 case DRM_FORMAT_YVYU:
5678 case DRM_FORMAT_UYVY:
5679 case DRM_FORMAT_VYUY:
5680 case DRM_FORMAT_NV12:
5681 case DRM_FORMAT_NV21:
5682 case DRM_FORMAT_NV16:
5683 case DRM_FORMAT_NV61:
5684 case DRM_FORMAT_YUV422:
5685 case DRM_FORMAT_YVU422:
5686 case DRM_FORMAT_YUV420:
5687 case DRM_FORMAT_YVU420:
5693 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5696 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5697 * @format: pixel format (DRM_FORMAT_*)
5700 * The vertical chroma subsampling factor for the
5701 * specified pixel format.
5703 int drm_format_vert_chroma_subsampling(uint32_t format)
5706 case DRM_FORMAT_YUV410:
5707 case DRM_FORMAT_YVU410:
5709 case DRM_FORMAT_YUV420:
5710 case DRM_FORMAT_YVU420:
5711 case DRM_FORMAT_NV12:
5712 case DRM_FORMAT_NV21:
5718 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5721 * drm_format_plane_width - width of the plane given the first plane
5722 * @width: width of the first plane
5723 * @format: pixel format
5724 * @plane: plane index
5727 * The width of @plane, given that the width of the first plane is @width.
5729 int drm_format_plane_width(int width, uint32_t format, int plane)
5731 if (plane >= drm_format_num_planes(format))
5737 return width / drm_format_horz_chroma_subsampling(format);
5739 EXPORT_SYMBOL(drm_format_plane_width);
5742 * drm_format_plane_height - height of the plane given the first plane
5743 * @height: height of the first plane
5744 * @format: pixel format
5745 * @plane: plane index
5748 * The height of @plane, given that the height of the first plane is @height.
5750 int drm_format_plane_height(int height, uint32_t format, int plane)
5752 if (plane >= drm_format_num_planes(format))
5758 return height / drm_format_vert_chroma_subsampling(format);
5760 EXPORT_SYMBOL(drm_format_plane_height);
5763 * drm_rotation_simplify() - Try to simplify the rotation
5764 * @rotation: Rotation to be simplified
5765 * @supported_rotations: Supported rotations
5767 * Attempt to simplify the rotation to a form that is supported.
5768 * Eg. if the hardware supports everything except DRM_REFLECT_X
5769 * one could call this function like this:
5771 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5772 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5773 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5775 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5776 * transforms the hardware supports, this function may not
5777 * be able to produce a supported transform, so the caller should
5778 * check the result afterwards.
5780 unsigned int drm_rotation_simplify(unsigned int rotation,
5781 unsigned int supported_rotations)
5783 if (rotation & ~supported_rotations) {
5784 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5785 rotation = (rotation & DRM_REFLECT_MASK) |
5786 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5791 EXPORT_SYMBOL(drm_rotation_simplify);
5794 * drm_mode_config_init - initialize DRM mode_configuration structure
5797 * Initialize @dev's mode_config structure, used for tracking the graphics
5798 * configuration of @dev.
5800 * Since this initializes the modeset locks, no locking is possible. Which is no
5801 * problem, since this should happen single threaded at init time. It is the
5802 * driver's problem to ensure this guarantee.
5805 void drm_mode_config_init(struct drm_device *dev)
5807 mutex_init(&dev->mode_config.mutex);
5808 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5809 mutex_init(&dev->mode_config.idr_mutex);
5810 mutex_init(&dev->mode_config.fb_lock);
5811 mutex_init(&dev->mode_config.blob_lock);
5812 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5813 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5814 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5815 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5816 INIT_LIST_HEAD(&dev->mode_config.property_list);
5817 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5818 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5819 idr_init(&dev->mode_config.crtc_idr);
5820 idr_init(&dev->mode_config.tile_idr);
5821 ida_init(&dev->mode_config.connector_ida);
5823 drm_modeset_lock_all(dev);
5824 drm_mode_create_standard_properties(dev);
5825 drm_modeset_unlock_all(dev);
5827 /* Just to be sure */
5828 dev->mode_config.num_fb = 0;
5829 dev->mode_config.num_connector = 0;
5830 dev->mode_config.num_crtc = 0;
5831 dev->mode_config.num_encoder = 0;
5832 dev->mode_config.num_overlay_plane = 0;
5833 dev->mode_config.num_total_plane = 0;
5835 EXPORT_SYMBOL(drm_mode_config_init);
5838 * drm_mode_config_cleanup - free up DRM mode_config info
5841 * Free up all the connectors and CRTCs associated with this DRM device, then
5842 * free up the framebuffers and associated buffer objects.
5844 * Note that since this /should/ happen single-threaded at driver/device
5845 * teardown time, no locking is required. It's the driver's job to ensure that
5846 * this guarantee actually holds true.
5848 * FIXME: cleanup any dangling user buffer objects too
5850 void drm_mode_config_cleanup(struct drm_device *dev)
5852 struct drm_connector *connector, *ot;
5853 struct drm_crtc *crtc, *ct;
5854 struct drm_encoder *encoder, *enct;
5855 struct drm_framebuffer *fb, *fbt;
5856 struct drm_property *property, *pt;
5857 struct drm_property_blob *blob, *bt;
5858 struct drm_plane *plane, *plt;
5860 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5862 encoder->funcs->destroy(encoder);
5865 list_for_each_entry_safe(connector, ot,
5866 &dev->mode_config.connector_list, head) {
5867 connector->funcs->destroy(connector);
5870 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5872 drm_property_destroy(dev, property);
5875 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5877 plane->funcs->destroy(plane);
5880 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5881 crtc->funcs->destroy(crtc);
5884 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5886 drm_property_unreference_blob(blob);
5890 * Single-threaded teardown context, so it's not required to grab the
5891 * fb_lock to protect against concurrent fb_list access. Contrary, it
5892 * would actually deadlock with the drm_framebuffer_cleanup function.
5894 * Also, if there are any framebuffers left, that's a driver leak now,
5895 * so politely WARN about this.
5897 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5898 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5899 drm_framebuffer_free(&fb->base.refcount);
5902 ida_destroy(&dev->mode_config.connector_ida);
5903 idr_destroy(&dev->mode_config.tile_idr);
5904 idr_destroy(&dev->mode_config.crtc_idr);
5905 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5907 EXPORT_SYMBOL(drm_mode_config_cleanup);
5909 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5910 unsigned int supported_rotations)
5912 static const struct drm_prop_enum_list props[] = {
5913 { DRM_ROTATE_0, "rotate-0" },
5914 { DRM_ROTATE_90, "rotate-90" },
5915 { DRM_ROTATE_180, "rotate-180" },
5916 { DRM_ROTATE_270, "rotate-270" },
5917 { DRM_REFLECT_X, "reflect-x" },
5918 { DRM_REFLECT_Y, "reflect-y" },
5921 return drm_property_create_bitmask(dev, 0, "rotation",
5922 props, ARRAY_SIZE(props),
5923 supported_rotations);
5925 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5930 * Tile groups are used to represent tiled monitors with a unique
5931 * integer identifier. Tiled monitors using DisplayID v1.3 have
5932 * a unique 8-byte handle, we store this in a tile group, so we
5933 * have a common identifier for all tiles in a monitor group.
5935 static void drm_tile_group_free(struct kref *kref)
5937 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5938 struct drm_device *dev = tg->dev;
5939 mutex_lock(&dev->mode_config.idr_mutex);
5940 idr_remove(&dev->mode_config.tile_idr, tg->id);
5941 mutex_unlock(&dev->mode_config.idr_mutex);
5946 * drm_mode_put_tile_group - drop a reference to a tile group.
5948 * @tg: tile group to drop reference to.
5950 * drop reference to tile group and free if 0.
5952 void drm_mode_put_tile_group(struct drm_device *dev,
5953 struct drm_tile_group *tg)
5955 kref_put(&tg->refcount, drm_tile_group_free);
5959 * drm_mode_get_tile_group - get a reference to an existing tile group
5961 * @topology: 8-bytes unique per monitor.
5963 * Use the unique bytes to get a reference to an existing tile group.
5966 * tile group or NULL if not found.
5968 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5971 struct drm_tile_group *tg;
5973 mutex_lock(&dev->mode_config.idr_mutex);
5974 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5975 if (!memcmp(tg->group_data, topology, 8)) {
5976 if (!kref_get_unless_zero(&tg->refcount))
5978 mutex_unlock(&dev->mode_config.idr_mutex);
5982 mutex_unlock(&dev->mode_config.idr_mutex);
5985 EXPORT_SYMBOL(drm_mode_get_tile_group);
5988 * drm_mode_create_tile_group - create a tile group from a displayid description
5990 * @topology: 8-bytes unique per monitor.
5992 * Create a tile group for the unique monitor, and get a unique
5993 * identifier for the tile group.
5996 * new tile group or error.
5998 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
6001 struct drm_tile_group *tg;
6004 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
6006 return ERR_PTR(-ENOMEM);
6008 kref_init(&tg->refcount);
6009 memcpy(tg->group_data, topology, 8);
6012 mutex_lock(&dev->mode_config.idr_mutex);
6013 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
6021 mutex_unlock(&dev->mode_config.idr_mutex);
6024 EXPORT_SYMBOL(drm_mode_create_tile_group);