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/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <uapi_drm/drm_fourcc.h>
39 #include <linux/slab.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
45 struct drm_mode_fb_cmd2 *r,
46 struct drm_file *file_priv);
48 /* Avoid boilerplate. I'm tired of typing. */
49 #define DRM_ENUM_NAME_FN(fnname, list) \
50 const char *fnname(int val) \
53 for (i = 0; i < ARRAY_SIZE(list); i++) { \
54 if (list[i].type == val) \
55 return list[i].name; \
63 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
64 { { DRM_MODE_DPMS_ON, "On" },
65 { DRM_MODE_DPMS_STANDBY, "Standby" },
66 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
67 { DRM_MODE_DPMS_OFF, "Off" }
70 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
72 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
74 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
75 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
76 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
82 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
84 { DRM_MODE_SCALE_NONE, "None" },
85 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
86 { DRM_MODE_SCALE_CENTER, "Center" },
87 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
90 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
91 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
92 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
93 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
97 * Non-global properties, but "required" for certain connectors.
99 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[] =
110 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
115 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
116 drm_dvi_i_subconnector_enum_list)
118 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
120 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
121 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
123 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
124 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
127 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
129 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
131 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
132 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
133 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
134 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
135 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
138 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
139 drm_tv_subconnector_enum_list)
141 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
142 { DRM_MODE_DIRTY_OFF, "Off" },
143 { DRM_MODE_DIRTY_ON, "On" },
144 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
147 struct drm_conn_prop_enum_list {
154 * Connector and encoder types.
156 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
157 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
158 { DRM_MODE_CONNECTOR_VGA, "VGA" },
159 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
160 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
161 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
162 { DRM_MODE_CONNECTOR_Composite, "Composite" },
163 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
164 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
165 { DRM_MODE_CONNECTOR_Component, "Component" },
166 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
167 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
168 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
169 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
170 { DRM_MODE_CONNECTOR_TV, "TV" },
171 { DRM_MODE_CONNECTOR_eDP, "eDP" },
172 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
173 { DRM_MODE_CONNECTOR_DSI, "DSI" },
176 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
177 { { DRM_MODE_ENCODER_NONE, "None" },
178 { DRM_MODE_ENCODER_DAC, "DAC" },
179 { DRM_MODE_ENCODER_TMDS, "TMDS" },
180 { DRM_MODE_ENCODER_LVDS, "LVDS" },
181 { DRM_MODE_ENCODER_TVDAC, "TV" },
182 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
183 { DRM_MODE_ENCODER_DSI, "DSI" },
184 { DRM_MODE_ENCODER_DPMST, "DP MST" },
187 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
189 { SubPixelUnknown, "Unknown" },
190 { SubPixelHorizontalRGB, "Horizontal RGB" },
191 { SubPixelHorizontalBGR, "Horizontal BGR" },
192 { SubPixelVerticalRGB, "Vertical RGB" },
193 { SubPixelVerticalBGR, "Vertical BGR" },
194 { SubPixelNone, "None" },
197 void drm_connector_ida_init(void)
202 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
203 ida_init(&drm_connector_enum_list[i].ida);
207 void drm_connector_ida_destroy(void)
212 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
213 ida_destroy(&drm_connector_enum_list[i].ida);
218 * drm_get_connector_status_name - return a string for connector status
219 * @status: connector status to compute name of
221 * In contrast to the other drm_get_*_name functions this one here returns a
222 * const pointer and hence is threadsafe.
224 const char *drm_get_connector_status_name(enum drm_connector_status status)
226 if (status == connector_status_connected)
228 else if (status == connector_status_disconnected)
229 return "disconnected";
233 EXPORT_SYMBOL(drm_get_connector_status_name);
236 * drm_get_subpixel_order_name - return a string for a given subpixel enum
237 * @order: enum of subpixel_order
239 * Note you could abuse this and return something out of bounds, but that
240 * would be a caller error. No unscrubbed user data should make it here.
242 const char *drm_get_subpixel_order_name(enum subpixel_order order)
244 return drm_subpixel_enum_list[order].name;
246 EXPORT_SYMBOL(drm_get_subpixel_order_name);
248 static char printable_char(int c)
250 return isascii(c) && isprint(c) ? c : '?';
254 * drm_get_format_name - return a string for drm fourcc format
255 * @format: format to compute name of
257 * Note that the buffer used by this function is globally shared and owned by
258 * the function itself.
260 * FIXME: This isn't really multithreading safe.
262 const char *drm_get_format_name(uint32_t format)
266 ksnprintf(buf, sizeof(buf),
267 "%c%c%c%c %s-endian (0x%08x)",
268 printable_char(format & 0xff),
269 printable_char((format >> 8) & 0xff),
270 printable_char((format >> 16) & 0xff),
271 printable_char((format >> 24) & 0x7f),
272 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
277 EXPORT_SYMBOL(drm_get_format_name);
280 * drm_mode_object_get - allocate a new modeset identifier
282 * @obj: object pointer, used to generate unique ID
283 * @obj_type: object type
285 * Create a unique identifier based on @ptr in @dev's identifier space. Used
286 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
287 * modeset identifiers are _not_ reference counted. Hence don't use this for
288 * reference counted modeset objects like framebuffers.
291 * New unique (relative to other objects in @dev) integer identifier for the
294 int drm_mode_object_get(struct drm_device *dev,
295 struct drm_mode_object *obj, uint32_t obj_type)
299 mutex_lock(&dev->mode_config.idr_mutex);
300 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
303 * Set up the object linking under the protection of the idr
304 * lock so that other users can't see inconsistent state.
307 obj->type = obj_type;
309 mutex_unlock(&dev->mode_config.idr_mutex);
311 return ret < 0 ? ret : 0;
315 * drm_mode_object_put - free a modeset identifer
317 * @object: object to free
319 * Free @id from @dev's unique identifier pool. Note that despite the _get
320 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
321 * for reference counted modeset objects like framebuffers.
323 void drm_mode_object_put(struct drm_device *dev,
324 struct drm_mode_object *object)
326 mutex_lock(&dev->mode_config.idr_mutex);
327 idr_remove(&dev->mode_config.crtc_idr, object->id);
328 mutex_unlock(&dev->mode_config.idr_mutex);
331 static struct drm_mode_object *_object_find(struct drm_device *dev,
332 uint32_t id, uint32_t type)
334 struct drm_mode_object *obj = NULL;
336 mutex_lock(&dev->mode_config.idr_mutex);
337 obj = idr_find(&dev->mode_config.crtc_idr, id);
338 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
340 if (obj && obj->id != id)
342 /* don't leak out unref'd fb's */
343 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
345 mutex_unlock(&dev->mode_config.idr_mutex);
351 * drm_mode_object_find - look up a drm object with static lifetime
353 * @id: id of the mode object
354 * @type: type of the mode object
356 * Note that framebuffers cannot be looked up with this functions - since those
357 * are reference counted, they need special treatment. Even with
358 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
359 * rather than WARN_ON()).
361 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
362 uint32_t id, uint32_t type)
364 struct drm_mode_object *obj = NULL;
366 /* Framebuffers are reference counted and need their own lookup
368 WARN_ON(type == DRM_MODE_OBJECT_FB);
369 obj = _object_find(dev, id, type);
372 EXPORT_SYMBOL(drm_mode_object_find);
375 * drm_framebuffer_init - initialize a framebuffer
377 * @fb: framebuffer to be initialized
378 * @funcs: ... with these functions
380 * Allocates an ID for the framebuffer's parent mode object, sets its mode
381 * functions & device file and adds it to the master fd list.
384 * This functions publishes the fb and makes it available for concurrent access
385 * by other users. Which means by this point the fb _must_ be fully set up -
386 * since all the fb attributes are invariant over its lifetime, no further
387 * locking but only correct reference counting is required.
390 * Zero on success, error code on failure.
392 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
393 const struct drm_framebuffer_funcs *funcs)
397 mutex_lock(&dev->mode_config.fb_lock);
398 kref_init(&fb->refcount);
399 INIT_LIST_HEAD(&fb->filp_head);
403 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
407 dev->mode_config.num_fb++;
408 list_add(&fb->head, &dev->mode_config.fb_list);
410 mutex_unlock(&dev->mode_config.fb_lock);
414 EXPORT_SYMBOL(drm_framebuffer_init);
416 /* dev->mode_config.fb_lock must be held! */
417 static void __drm_framebuffer_unregister(struct drm_device *dev,
418 struct drm_framebuffer *fb)
420 mutex_lock(&dev->mode_config.idr_mutex);
421 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
422 mutex_unlock(&dev->mode_config.idr_mutex);
427 static void drm_framebuffer_free(struct kref *kref)
429 struct drm_framebuffer *fb =
430 container_of(kref, struct drm_framebuffer, refcount);
431 struct drm_device *dev = fb->dev;
434 * The lookup idr holds a weak reference, which has not necessarily been
435 * removed at this point. Check for that.
437 mutex_lock(&dev->mode_config.fb_lock);
439 /* Mark fb as reaped and drop idr ref. */
440 __drm_framebuffer_unregister(dev, fb);
442 mutex_unlock(&dev->mode_config.fb_lock);
444 fb->funcs->destroy(fb);
447 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
450 struct drm_mode_object *obj = NULL;
451 struct drm_framebuffer *fb;
453 mutex_lock(&dev->mode_config.idr_mutex);
454 obj = idr_find(&dev->mode_config.crtc_idr, id);
455 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
459 mutex_unlock(&dev->mode_config.idr_mutex);
465 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
467 * @id: id of the fb object
469 * If successful, this grabs an additional reference to the framebuffer -
470 * callers need to make sure to eventually unreference the returned framebuffer
471 * again, using @drm_framebuffer_unreference.
473 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
476 struct drm_framebuffer *fb;
478 mutex_lock(&dev->mode_config.fb_lock);
479 fb = __drm_framebuffer_lookup(dev, id);
481 if (!kref_get_unless_zero(&fb->refcount))
484 mutex_unlock(&dev->mode_config.fb_lock);
488 EXPORT_SYMBOL(drm_framebuffer_lookup);
491 * drm_framebuffer_unreference - unref a framebuffer
492 * @fb: framebuffer to unref
494 * This functions decrements the fb's refcount and frees it if it drops to zero.
496 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
498 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
499 kref_put(&fb->refcount, drm_framebuffer_free);
501 EXPORT_SYMBOL(drm_framebuffer_unreference);
504 * drm_framebuffer_reference - incr the fb refcnt
507 * This functions increments the fb's refcount.
509 void drm_framebuffer_reference(struct drm_framebuffer *fb)
511 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
512 kref_get(&fb->refcount);
514 EXPORT_SYMBOL(drm_framebuffer_reference);
516 static void drm_framebuffer_free_bug(struct kref *kref)
521 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
523 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
524 kref_put(&fb->refcount, drm_framebuffer_free_bug);
528 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
529 * @fb: fb to unregister
531 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
532 * those used for fbdev. Note that the caller must hold a reference of it's own,
533 * i.e. the object may not be destroyed through this call (since it'll lead to a
534 * locking inversion).
536 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
538 struct drm_device *dev = fb->dev;
540 mutex_lock(&dev->mode_config.fb_lock);
541 /* Mark fb as reaped and drop idr ref. */
542 __drm_framebuffer_unregister(dev, fb);
543 mutex_unlock(&dev->mode_config.fb_lock);
545 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
548 * drm_framebuffer_cleanup - remove a framebuffer object
549 * @fb: framebuffer to remove
551 * Cleanup framebuffer. This function is intended to be used from the drivers
552 * ->destroy callback. It can also be used to clean up driver private
553 * framebuffers embedded into a larger structure.
555 * Note that this function does not remove the fb from active usuage - if it is
556 * still used anywhere, hilarity can ensue since userspace could call getfb on
557 * the id and get back -EINVAL. Obviously no concern at driver unload time.
559 * Also, the framebuffer will not be removed from the lookup idr - for
560 * user-created framebuffers this will happen in in the rmfb ioctl. For
561 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
562 * drm_framebuffer_unregister_private.
564 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
566 struct drm_device *dev = fb->dev;
568 mutex_lock(&dev->mode_config.fb_lock);
570 dev->mode_config.num_fb--;
571 mutex_unlock(&dev->mode_config.fb_lock);
573 EXPORT_SYMBOL(drm_framebuffer_cleanup);
576 * drm_framebuffer_remove - remove and unreference a framebuffer object
577 * @fb: framebuffer to remove
579 * Scans all the CRTCs and planes in @dev's mode_config. If they're
580 * using @fb, removes it, setting it to NULL. Then drops the reference to the
581 * passed-in framebuffer. Might take the modeset locks.
583 * Note that this function optimizes the cleanup away if the caller holds the
584 * last reference to the framebuffer. It is also guaranteed to not take the
585 * modeset locks in this case.
587 void drm_framebuffer_remove(struct drm_framebuffer *fb)
589 struct drm_device *dev = fb->dev;
590 struct drm_crtc *crtc;
591 struct drm_plane *plane;
592 struct drm_mode_set set;
595 WARN_ON(!list_empty(&fb->filp_head));
598 * drm ABI mandates that we remove any deleted framebuffers from active
599 * useage. But since most sane clients only remove framebuffers they no
600 * longer need, try to optimize this away.
602 * Since we're holding a reference ourselves, observing a refcount of 1
603 * means that we're the last holder and can skip it. Also, the refcount
604 * can never increase from 1 again, so we don't need any barriers or
607 * Note that userspace could try to race with use and instate a new
608 * usage _after_ we've cleared all current ones. End result will be an
609 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
612 if (atomic_read(&fb->refcount.refcount) > 1) {
613 drm_modeset_lock_all(dev);
614 /* remove from any CRTC */
615 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
616 if (crtc->primary->fb == fb) {
617 /* should turn off the crtc */
618 memset(&set, 0, sizeof(struct drm_mode_set));
621 ret = drm_mode_set_config_internal(&set);
623 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
627 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
629 drm_plane_force_disable(plane);
631 drm_modeset_unlock_all(dev);
634 drm_framebuffer_unreference(fb);
636 EXPORT_SYMBOL(drm_framebuffer_remove);
638 DEFINE_WW_CLASS(crtc_ww_class);
641 * drm_crtc_init_with_planes - Initialise a new CRTC object with
642 * specified primary and cursor planes.
644 * @crtc: CRTC object to init
645 * @primary: Primary plane for CRTC
646 * @cursor: Cursor plane for CRTC
647 * @funcs: callbacks for the new CRTC
649 * Inits a new object created as base part of a driver crtc object.
652 * Zero on success, error code on failure.
654 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
655 struct drm_plane *primary,
656 struct drm_plane *cursor,
657 const struct drm_crtc_funcs *funcs)
659 struct drm_mode_config *config = &dev->mode_config;
664 crtc->invert_dimensions = false;
666 drm_modeset_lock_init(&crtc->mutex);
667 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
671 crtc->base.properties = &crtc->properties;
673 list_add_tail(&crtc->head, &config->crtc_list);
676 crtc->primary = primary;
677 crtc->cursor = cursor;
679 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
681 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
687 EXPORT_SYMBOL(drm_crtc_init_with_planes);
690 * drm_crtc_cleanup - Clean up the core crtc usage
691 * @crtc: CRTC to cleanup
693 * This function cleans up @crtc and removes it from the DRM mode setting
694 * core. Note that the function does *not* free the crtc structure itself,
695 * this is the responsibility of the caller.
697 void drm_crtc_cleanup(struct drm_crtc *crtc)
699 struct drm_device *dev = crtc->dev;
701 kfree(crtc->gamma_store);
702 crtc->gamma_store = NULL;
704 drm_modeset_lock_fini(&crtc->mutex);
706 drm_mode_object_put(dev, &crtc->base);
707 list_del(&crtc->head);
708 dev->mode_config.num_crtc--;
710 EXPORT_SYMBOL(drm_crtc_cleanup);
713 * drm_crtc_index - find the index of a registered CRTC
714 * @crtc: CRTC to find index for
716 * Given a registered CRTC, return the index of that CRTC within a DRM
717 * device's list of CRTCs.
719 unsigned int drm_crtc_index(struct drm_crtc *crtc)
721 unsigned int index = 0;
722 struct drm_crtc *tmp;
724 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
733 EXPORT_SYMBOL(drm_crtc_index);
736 * drm_mode_remove - remove and free a mode
737 * @connector: connector list to modify
738 * @mode: mode to remove
740 * Remove @mode from @connector's mode list, then free it.
742 static void drm_mode_remove(struct drm_connector *connector,
743 struct drm_display_mode *mode)
745 list_del(&mode->head);
746 drm_mode_destroy(connector->dev, mode);
750 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
751 * @connector: connector to quwery
752 * @mode: returned mode
754 * The kernel supports per-connector configration of its consoles through
755 * use of the video= parameter. This function parses that option and
756 * extracts the user's specified mode (or enable/disable status) for a
757 * particular connector. This is typically only used during the early fbdev
760 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
762 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
765 if (fb_get_options(connector->name, &option))
768 if (!drm_mode_parse_command_line_for_connector(option,
776 switch (mode->force) {
780 case DRM_FORCE_ON_DIGITAL:
789 DRM_INFO("forcing %s connector %s\n", connector->name, s);
790 connector->force = mode->force;
793 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
795 mode->xres, mode->yres,
796 mode->refresh_specified ? mode->refresh : 60,
797 mode->rb ? " reduced blanking" : "",
798 mode->margins ? " with margins" : "",
799 mode->interlace ? " interlaced" : "");
803 * drm_connector_init - Init a preallocated connector
805 * @connector: the connector to init
806 * @funcs: callbacks for this connector
807 * @connector_type: user visible type of the connector
809 * Initialises a preallocated connector. Connectors should be
810 * subclassed as part of driver connector objects.
813 * Zero on success, error code on failure.
815 int drm_connector_init(struct drm_device *dev,
816 struct drm_connector *connector,
817 const struct drm_connector_funcs *funcs,
822 drm_modeset_lock_all(dev);
824 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
828 connector->base.properties = &connector->properties;
829 connector->dev = dev;
830 connector->funcs = funcs;
831 connector->connector_type = connector_type;
832 connector->connector_type_id =
833 ++drm_connector_enum_list[connector_type].count; /* TODO */
834 if (connector->connector_type_id < 0) {
835 ret = connector->connector_type_id;
839 drm_asprintf(GFP_KERNEL, "%s-%d",
840 drm_connector_enum_list[connector_type].name,
841 connector->connector_type_id);
842 if (!connector->name) {
847 INIT_LIST_HEAD(&connector->probed_modes);
848 INIT_LIST_HEAD(&connector->modes);
849 connector->edid_blob_ptr = NULL;
850 connector->status = connector_status_unknown;
852 drm_connector_get_cmdline_mode(connector);
854 list_add_tail(&connector->head, &dev->mode_config.connector_list);
855 dev->mode_config.num_connector++;
857 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
858 drm_object_attach_property(&connector->base,
859 dev->mode_config.edid_property,
862 drm_object_attach_property(&connector->base,
863 dev->mode_config.dpms_property, 0);
865 connector->debugfs_entry = NULL;
869 drm_mode_object_put(dev, &connector->base);
872 drm_modeset_unlock_all(dev);
876 EXPORT_SYMBOL(drm_connector_init);
879 * drm_connector_cleanup - cleans up an initialised connector
880 * @connector: connector to cleanup
882 * Cleans up the connector but doesn't free the object.
884 void drm_connector_cleanup(struct drm_connector *connector)
886 struct drm_device *dev = connector->dev;
887 struct drm_display_mode *mode, *t;
889 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
890 drm_mode_remove(connector, mode);
892 list_for_each_entry_safe(mode, t, &connector->modes, head)
893 drm_mode_remove(connector, mode);
895 drm_mode_object_put(dev, &connector->base);
896 kfree(connector->name);
897 connector->name = NULL;
898 list_del(&connector->head);
899 dev->mode_config.num_connector--;
901 EXPORT_SYMBOL(drm_connector_cleanup);
904 * drm_connector_index - find the index of a registered connector
905 * @connector: connector to find index for
907 * Given a registered connector, return the index of that connector within a DRM
908 * device's list of connectors.
910 unsigned int drm_connector_index(struct drm_connector *connector)
912 unsigned int index = 0;
913 struct drm_connector *tmp;
915 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
916 if (tmp == connector)
924 EXPORT_SYMBOL(drm_connector_index);
927 * drm_connector_register - register a connector
928 * @connector: the connector to register
930 * Register userspace interfaces for a connector
933 * Zero on success, error code on failure.
935 int drm_connector_register(struct drm_connector *connector)
939 ret = drm_sysfs_connector_add(connector);
944 ret = drm_debugfs_connector_add(connector);
947 drm_sysfs_connector_remove(connector);
953 EXPORT_SYMBOL(drm_connector_register);
956 * drm_connector_unregister - unregister a connector
957 * @connector: the connector to unregister
959 * Unregister userspace interfaces for a connector
961 void drm_connector_unregister(struct drm_connector *connector)
963 drm_sysfs_connector_remove(connector);
965 drm_debugfs_connector_remove(connector);
968 EXPORT_SYMBOL(drm_connector_unregister);
972 * drm_connector_unplug_all - unregister connector userspace interfaces
975 * This function unregisters all connector userspace interfaces in sysfs. Should
976 * be call when the device is disconnected, e.g. from an usb driver's
977 * ->disconnect callback.
979 void drm_connector_unplug_all(struct drm_device *dev)
981 struct drm_connector *connector;
983 /* taking the mode config mutex ends up in a clash with sysfs */
984 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
985 drm_connector_unregister(connector);
988 EXPORT_SYMBOL(drm_connector_unplug_all);
991 * drm_bridge_init - initialize a drm transcoder/bridge
993 * @bridge: transcoder/bridge to set up
994 * @funcs: bridge function table
996 * Initialises a preallocated bridge. Bridges should be
997 * subclassed as part of driver connector objects.
1000 * Zero on success, error code on failure.
1002 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1003 const struct drm_bridge_funcs *funcs)
1007 drm_modeset_lock_all(dev);
1009 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1014 bridge->funcs = funcs;
1016 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1017 dev->mode_config.num_bridge++;
1020 drm_modeset_unlock_all(dev);
1023 EXPORT_SYMBOL(drm_bridge_init);
1026 * drm_bridge_cleanup - cleans up an initialised bridge
1027 * @bridge: bridge to cleanup
1029 * Cleans up the bridge but doesn't free the object.
1031 void drm_bridge_cleanup(struct drm_bridge *bridge)
1033 struct drm_device *dev = bridge->dev;
1035 drm_modeset_lock_all(dev);
1036 drm_mode_object_put(dev, &bridge->base);
1037 list_del(&bridge->head);
1038 dev->mode_config.num_bridge--;
1039 drm_modeset_unlock_all(dev);
1041 EXPORT_SYMBOL(drm_bridge_cleanup);
1044 * drm_encoder_init - Init a preallocated encoder
1046 * @encoder: the encoder to init
1047 * @funcs: callbacks for this encoder
1048 * @encoder_type: user visible type of the encoder
1050 * Initialises a preallocated encoder. Encoder should be
1051 * subclassed as part of driver encoder objects.
1054 * Zero on success, error code on failure.
1056 int drm_encoder_init(struct drm_device *dev,
1057 struct drm_encoder *encoder,
1058 const struct drm_encoder_funcs *funcs,
1063 drm_modeset_lock_all(dev);
1065 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1070 encoder->encoder_type = encoder_type;
1071 encoder->funcs = funcs;
1072 encoder->name = drm_asprintf(GFP_KERNEL, "%s-%d",
1073 drm_encoder_enum_list[encoder_type].name,
1075 if (!encoder->name) {
1080 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1081 dev->mode_config.num_encoder++;
1085 drm_mode_object_put(dev, &encoder->base);
1088 drm_modeset_unlock_all(dev);
1092 EXPORT_SYMBOL(drm_encoder_init);
1095 * drm_encoder_cleanup - cleans up an initialised encoder
1096 * @encoder: encoder to cleanup
1098 * Cleans up the encoder but doesn't free the object.
1100 void drm_encoder_cleanup(struct drm_encoder *encoder)
1102 struct drm_device *dev = encoder->dev;
1103 drm_modeset_lock_all(dev);
1104 drm_mode_object_put(dev, &encoder->base);
1105 kfree(encoder->name);
1106 encoder->name = NULL;
1107 list_del(&encoder->head);
1108 dev->mode_config.num_encoder--;
1109 drm_modeset_unlock_all(dev);
1111 EXPORT_SYMBOL(drm_encoder_cleanup);
1114 * drm_universal_plane_init - Initialize a new universal plane object
1116 * @plane: plane object to init
1117 * @possible_crtcs: bitmask of possible CRTCs
1118 * @funcs: callbacks for the new plane
1119 * @formats: array of supported formats (%DRM_FORMAT_*)
1120 * @format_count: number of elements in @formats
1121 * @type: type of plane (overlay, primary, cursor)
1123 * Initializes a plane object of type @type.
1126 * Zero on success, error code on failure.
1128 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1129 unsigned long possible_crtcs,
1130 const struct drm_plane_funcs *funcs,
1131 const uint32_t *formats, uint32_t format_count,
1132 enum drm_plane_type type)
1136 drm_modeset_lock_all(dev);
1138 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1142 plane->base.properties = &plane->properties;
1144 plane->funcs = funcs;
1145 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1147 if (!plane->format_types) {
1148 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1149 drm_mode_object_put(dev, &plane->base);
1154 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1155 plane->format_count = format_count;
1156 plane->possible_crtcs = possible_crtcs;
1159 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1160 dev->mode_config.num_total_plane++;
1161 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1162 dev->mode_config.num_overlay_plane++;
1164 drm_object_attach_property(&plane->base,
1165 dev->mode_config.plane_type_property,
1169 drm_modeset_unlock_all(dev);
1173 EXPORT_SYMBOL(drm_universal_plane_init);
1176 * drm_plane_init - Initialize a legacy plane
1178 * @plane: plane object to init
1179 * @possible_crtcs: bitmask of possible CRTCs
1180 * @funcs: callbacks for the new plane
1181 * @formats: array of supported formats (%DRM_FORMAT_*)
1182 * @format_count: number of elements in @formats
1183 * @is_primary: plane type (primary vs overlay)
1185 * Legacy API to initialize a DRM plane.
1187 * New drivers should call drm_universal_plane_init() instead.
1190 * Zero on success, error code on failure.
1192 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1193 unsigned long possible_crtcs,
1194 const struct drm_plane_funcs *funcs,
1195 const uint32_t *formats, uint32_t format_count,
1198 enum drm_plane_type type;
1200 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1201 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1202 formats, format_count, type);
1204 EXPORT_SYMBOL(drm_plane_init);
1207 * drm_plane_cleanup - Clean up the core plane usage
1208 * @plane: plane to cleanup
1210 * This function cleans up @plane and removes it from the DRM mode setting
1211 * core. Note that the function does *not* free the plane structure itself,
1212 * this is the responsibility of the caller.
1214 void drm_plane_cleanup(struct drm_plane *plane)
1216 struct drm_device *dev = plane->dev;
1218 drm_modeset_lock_all(dev);
1219 kfree(plane->format_types);
1220 drm_mode_object_put(dev, &plane->base);
1222 BUG_ON(list_empty(&plane->head));
1224 list_del(&plane->head);
1225 dev->mode_config.num_total_plane--;
1226 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1227 dev->mode_config.num_overlay_plane--;
1228 drm_modeset_unlock_all(dev);
1230 EXPORT_SYMBOL(drm_plane_cleanup);
1233 * drm_plane_index - find the index of a registered plane
1234 * @plane: plane to find index for
1236 * Given a registered plane, return the index of that CRTC within a DRM
1237 * device's list of planes.
1239 unsigned int drm_plane_index(struct drm_plane *plane)
1241 unsigned int index = 0;
1242 struct drm_plane *tmp;
1244 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1253 EXPORT_SYMBOL(drm_plane_index);
1256 * drm_plane_force_disable - Forcibly disable a plane
1257 * @plane: plane to disable
1259 * Forces the plane to be disabled.
1261 * Used when the plane's current framebuffer is destroyed,
1262 * and when restoring fbdev mode.
1264 void drm_plane_force_disable(struct drm_plane *plane)
1271 plane->old_fb = plane->fb;
1272 ret = plane->funcs->disable_plane(plane);
1274 DRM_ERROR("failed to disable plane with busy fb\n");
1275 plane->old_fb = NULL;
1278 /* disconnect the plane from the fb and crtc: */
1279 __drm_framebuffer_unreference(plane->old_fb);
1280 plane->old_fb = NULL;
1284 EXPORT_SYMBOL(drm_plane_force_disable);
1286 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1288 struct drm_property *edid;
1289 struct drm_property *dpms;
1290 struct drm_property *dev_path;
1293 * Standard properties (apply to all connectors)
1295 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1296 DRM_MODE_PROP_IMMUTABLE,
1298 dev->mode_config.edid_property = edid;
1300 dpms = drm_property_create_enum(dev, 0,
1301 "DPMS", drm_dpms_enum_list,
1302 ARRAY_SIZE(drm_dpms_enum_list));
1303 dev->mode_config.dpms_property = dpms;
1305 dev_path = drm_property_create(dev,
1306 DRM_MODE_PROP_BLOB |
1307 DRM_MODE_PROP_IMMUTABLE,
1309 dev->mode_config.path_property = dev_path;
1314 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1316 struct drm_property *type;
1319 * Standard properties (apply to all planes)
1321 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1322 "type", drm_plane_type_enum_list,
1323 ARRAY_SIZE(drm_plane_type_enum_list));
1324 dev->mode_config.plane_type_property = type;
1330 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1333 * Called by a driver the first time a DVI-I connector is made.
1335 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1337 struct drm_property *dvi_i_selector;
1338 struct drm_property *dvi_i_subconnector;
1340 if (dev->mode_config.dvi_i_select_subconnector_property)
1344 drm_property_create_enum(dev, 0,
1345 "select subconnector",
1346 drm_dvi_i_select_enum_list,
1347 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1348 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1350 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1352 drm_dvi_i_subconnector_enum_list,
1353 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1354 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1358 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1361 * drm_create_tv_properties - create TV specific connector properties
1363 * @num_modes: number of different TV formats (modes) supported
1364 * @modes: array of pointers to strings containing name of each format
1366 * Called by a driver's TV initialization routine, this function creates
1367 * the TV specific connector properties for a given device. Caller is
1368 * responsible for allocating a list of format names and passing them to
1371 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1374 struct drm_property *tv_selector;
1375 struct drm_property *tv_subconnector;
1378 if (dev->mode_config.tv_select_subconnector_property)
1382 * Basic connector properties
1384 tv_selector = drm_property_create_enum(dev, 0,
1385 "select subconnector",
1386 drm_tv_select_enum_list,
1387 ARRAY_SIZE(drm_tv_select_enum_list));
1388 dev->mode_config.tv_select_subconnector_property = tv_selector;
1391 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1393 drm_tv_subconnector_enum_list,
1394 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1395 dev->mode_config.tv_subconnector_property = tv_subconnector;
1398 * Other, TV specific properties: margins & TV modes.
1400 dev->mode_config.tv_left_margin_property =
1401 drm_property_create_range(dev, 0, "left margin", 0, 100);
1403 dev->mode_config.tv_right_margin_property =
1404 drm_property_create_range(dev, 0, "right margin", 0, 100);
1406 dev->mode_config.tv_top_margin_property =
1407 drm_property_create_range(dev, 0, "top margin", 0, 100);
1409 dev->mode_config.tv_bottom_margin_property =
1410 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1412 dev->mode_config.tv_mode_property =
1413 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1415 for (i = 0; i < num_modes; i++)
1416 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1419 dev->mode_config.tv_brightness_property =
1420 drm_property_create_range(dev, 0, "brightness", 0, 100);
1422 dev->mode_config.tv_contrast_property =
1423 drm_property_create_range(dev, 0, "contrast", 0, 100);
1425 dev->mode_config.tv_flicker_reduction_property =
1426 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1428 dev->mode_config.tv_overscan_property =
1429 drm_property_create_range(dev, 0, "overscan", 0, 100);
1431 dev->mode_config.tv_saturation_property =
1432 drm_property_create_range(dev, 0, "saturation", 0, 100);
1434 dev->mode_config.tv_hue_property =
1435 drm_property_create_range(dev, 0, "hue", 0, 100);
1439 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1442 * drm_mode_create_scaling_mode_property - create scaling mode property
1445 * Called by a driver the first time it's needed, must be attached to desired
1448 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1450 struct drm_property *scaling_mode;
1452 if (dev->mode_config.scaling_mode_property)
1456 drm_property_create_enum(dev, 0, "scaling mode",
1457 drm_scaling_mode_enum_list,
1458 ARRAY_SIZE(drm_scaling_mode_enum_list));
1460 dev->mode_config.scaling_mode_property = scaling_mode;
1464 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1467 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1470 * Called by a driver the first time it's needed, must be attached to desired
1474 * Zero on success, errno on failure.
1476 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1478 if (dev->mode_config.aspect_ratio_property)
1481 dev->mode_config.aspect_ratio_property =
1482 drm_property_create_enum(dev, 0, "aspect ratio",
1483 drm_aspect_ratio_enum_list,
1484 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1486 if (dev->mode_config.aspect_ratio_property == NULL)
1491 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1494 * drm_mode_create_dirty_property - create dirty property
1497 * Called by a driver the first time it's needed, must be attached to desired
1500 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1502 struct drm_property *dirty_info;
1504 if (dev->mode_config.dirty_info_property)
1508 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1510 drm_dirty_info_enum_list,
1511 ARRAY_SIZE(drm_dirty_info_enum_list));
1512 dev->mode_config.dirty_info_property = dirty_info;
1516 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1518 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1520 uint32_t total_objects = 0;
1522 total_objects += dev->mode_config.num_crtc;
1523 total_objects += dev->mode_config.num_connector;
1524 total_objects += dev->mode_config.num_encoder;
1525 total_objects += dev->mode_config.num_bridge;
1527 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1528 if (!group->id_list)
1531 group->num_crtcs = 0;
1532 group->num_connectors = 0;
1533 group->num_encoders = 0;
1534 group->num_bridges = 0;
1538 void drm_mode_group_destroy(struct drm_mode_group *group)
1540 kfree(group->id_list);
1541 group->id_list = NULL;
1545 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1546 * the drm core's responsibility to set up mode control groups.
1548 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1549 struct drm_mode_group *group)
1551 struct drm_crtc *crtc;
1552 struct drm_encoder *encoder;
1553 struct drm_connector *connector;
1554 struct drm_bridge *bridge;
1557 if ((ret = drm_mode_group_init(dev, group)))
1560 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1561 group->id_list[group->num_crtcs++] = crtc->base.id;
1563 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1564 group->id_list[group->num_crtcs + group->num_encoders++] =
1567 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1568 group->id_list[group->num_crtcs + group->num_encoders +
1569 group->num_connectors++] = connector->base.id;
1571 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1572 group->id_list[group->num_crtcs + group->num_encoders +
1573 group->num_connectors + group->num_bridges++] =
1578 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1580 void drm_reinit_primary_mode_group(struct drm_device *dev)
1582 drm_modeset_lock_all(dev);
1583 drm_mode_group_destroy(&dev->primary->mode_group);
1584 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1585 drm_modeset_unlock_all(dev);
1587 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1590 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1591 * @out: drm_mode_modeinfo struct to return to the user
1592 * @in: drm_display_mode to use
1594 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1597 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1598 const struct drm_display_mode *in)
1600 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1601 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1602 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1603 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1604 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1605 "timing values too large for mode info\n");
1607 out->clock = in->clock;
1608 out->hdisplay = in->hdisplay;
1609 out->hsync_start = in->hsync_start;
1610 out->hsync_end = in->hsync_end;
1611 out->htotal = in->htotal;
1612 out->hskew = in->hskew;
1613 out->vdisplay = in->vdisplay;
1614 out->vsync_start = in->vsync_start;
1615 out->vsync_end = in->vsync_end;
1616 out->vtotal = in->vtotal;
1617 out->vscan = in->vscan;
1618 out->vrefresh = in->vrefresh;
1619 out->flags = in->flags;
1620 out->type = in->type;
1621 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1622 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1626 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1627 * @out: drm_display_mode to return to the user
1628 * @in: drm_mode_modeinfo to use
1630 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1634 * Zero on success, errno on failure.
1636 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1637 const struct drm_mode_modeinfo *in)
1639 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1642 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1645 out->clock = in->clock;
1646 out->hdisplay = in->hdisplay;
1647 out->hsync_start = in->hsync_start;
1648 out->hsync_end = in->hsync_end;
1649 out->htotal = in->htotal;
1650 out->hskew = in->hskew;
1651 out->vdisplay = in->vdisplay;
1652 out->vsync_start = in->vsync_start;
1653 out->vsync_end = in->vsync_end;
1654 out->vtotal = in->vtotal;
1655 out->vscan = in->vscan;
1656 out->vrefresh = in->vrefresh;
1657 out->flags = in->flags;
1658 out->type = in->type;
1659 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1660 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1666 * drm_mode_getresources - get graphics configuration
1667 * @dev: drm device for the ioctl
1668 * @data: data pointer for the ioctl
1669 * @file_priv: drm file for the ioctl call
1671 * Construct a set of configuration description structures and return
1672 * them to the user, including CRTC, connector and framebuffer configuration.
1674 * Called by the user via ioctl.
1677 * Zero on success, errno on failure.
1679 int drm_mode_getresources(struct drm_device *dev, void *data,
1680 struct drm_file *file_priv)
1682 struct drm_mode_card_res *card_res = data;
1683 struct list_head *lh;
1684 struct drm_framebuffer *fb;
1685 struct drm_connector *connector;
1686 struct drm_crtc *crtc;
1687 struct drm_encoder *encoder;
1689 int connector_count = 0;
1692 int encoder_count = 0;
1694 uint32_t __user *fb_id;
1695 uint32_t __user *crtc_id;
1696 uint32_t __user *connector_id;
1697 uint32_t __user *encoder_id;
1698 struct drm_mode_group *mode_group;
1700 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1704 mutex_lock(&file_priv->fbs_lock);
1706 * For the non-control nodes we need to limit the list of resources
1707 * by IDs in the group list for this node
1709 list_for_each(lh, &file_priv->fbs)
1712 /* handle this in 4 parts */
1714 if (card_res->count_fbs >= fb_count) {
1716 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1717 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1718 if (put_user(fb->base.id, fb_id + copied)) {
1719 mutex_unlock(&file_priv->fbs_lock);
1725 card_res->count_fbs = fb_count;
1726 mutex_unlock(&file_priv->fbs_lock);
1728 drm_modeset_lock_all(dev);
1729 if (!drm_is_primary_client(file_priv)) {
1732 list_for_each(lh, &dev->mode_config.crtc_list)
1735 list_for_each(lh, &dev->mode_config.connector_list)
1738 list_for_each(lh, &dev->mode_config.encoder_list)
1743 mode_group = &file_priv->master->minor->mode_group;
1744 crtc_count = mode_group->num_crtcs;
1745 connector_count = mode_group->num_connectors;
1746 encoder_count = mode_group->num_encoders;
1750 card_res->max_height = dev->mode_config.max_height;
1751 card_res->min_height = dev->mode_config.min_height;
1752 card_res->max_width = dev->mode_config.max_width;
1753 card_res->min_width = dev->mode_config.min_width;
1756 if (card_res->count_crtcs >= crtc_count) {
1758 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1760 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1762 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1763 if (put_user(crtc->base.id, crtc_id + copied)) {
1770 for (i = 0; i < mode_group->num_crtcs; i++) {
1771 if (put_user(mode_group->id_list[i],
1772 crtc_id + copied)) {
1780 card_res->count_crtcs = crtc_count;
1783 if (card_res->count_encoders >= encoder_count) {
1785 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1787 list_for_each_entry(encoder,
1788 &dev->mode_config.encoder_list,
1790 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1792 if (put_user(encoder->base.id, encoder_id +
1800 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1801 if (put_user(mode_group->id_list[i],
1802 encoder_id + copied)) {
1811 card_res->count_encoders = encoder_count;
1814 if (card_res->count_connectors >= connector_count) {
1816 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1818 list_for_each_entry(connector,
1819 &dev->mode_config.connector_list,
1821 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1824 if (put_user(connector->base.id,
1825 connector_id + copied)) {
1832 int start = mode_group->num_crtcs +
1833 mode_group->num_encoders;
1834 for (i = start; i < start + mode_group->num_connectors; i++) {
1835 if (put_user(mode_group->id_list[i],
1836 connector_id + copied)) {
1844 card_res->count_connectors = connector_count;
1846 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1847 card_res->count_connectors, card_res->count_encoders);
1850 drm_modeset_unlock_all(dev);
1855 * drm_mode_getcrtc - get CRTC configuration
1856 * @dev: drm device for the ioctl
1857 * @data: data pointer for the ioctl
1858 * @file_priv: drm file for the ioctl call
1860 * Construct a CRTC configuration structure to return to the user.
1862 * Called by the user via ioctl.
1865 * Zero on success, errno on failure.
1867 int drm_mode_getcrtc(struct drm_device *dev,
1868 void *data, struct drm_file *file_priv)
1870 struct drm_mode_crtc *crtc_resp = data;
1871 struct drm_crtc *crtc;
1874 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1877 drm_modeset_lock_all(dev);
1879 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1885 crtc_resp->x = crtc->x;
1886 crtc_resp->y = crtc->y;
1887 crtc_resp->gamma_size = crtc->gamma_size;
1888 if (crtc->primary->fb)
1889 crtc_resp->fb_id = crtc->primary->fb->base.id;
1891 crtc_resp->fb_id = 0;
1893 if (crtc->enabled) {
1895 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1896 crtc_resp->mode_valid = 1;
1899 crtc_resp->mode_valid = 0;
1903 drm_modeset_unlock_all(dev);
1907 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1908 const struct drm_file *file_priv)
1911 * If user-space hasn't configured the driver to expose the stereo 3D
1912 * modes, don't expose them.
1914 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1921 * drm_mode_getconnector - get connector configuration
1922 * @dev: drm device for the ioctl
1923 * @data: data pointer for the ioctl
1924 * @file_priv: drm file for the ioctl call
1926 * Construct a connector configuration structure to return to the user.
1928 * Called by the user via ioctl.
1931 * Zero on success, errno on failure.
1933 int drm_mode_getconnector(struct drm_device *dev, void *data,
1934 struct drm_file *file_priv)
1936 struct drm_mode_get_connector *out_resp = data;
1937 struct drm_connector *connector;
1938 struct drm_display_mode *mode;
1940 int props_count = 0;
1941 int encoders_count = 0;
1945 struct drm_mode_modeinfo u_mode;
1946 struct drm_mode_modeinfo __user *mode_ptr;
1947 uint32_t __user *prop_ptr;
1948 uint64_t __user *prop_values;
1949 uint32_t __user *encoder_ptr;
1951 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1954 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1956 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1958 mutex_lock(&dev->mode_config.mutex);
1959 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1961 connector = drm_connector_find(dev, out_resp->connector_id);
1967 props_count = connector->properties.count;
1969 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1970 if (connector->encoder_ids[i] != 0) {
1975 if (out_resp->count_modes == 0) {
1976 connector->funcs->fill_modes(connector,
1977 dev->mode_config.max_width,
1978 dev->mode_config.max_height);
1981 /* delayed so we get modes regardless of pre-fill_modes state */
1982 list_for_each_entry(mode, &connector->modes, head)
1983 if (drm_mode_expose_to_userspace(mode, file_priv))
1986 out_resp->connector_id = connector->base.id;
1987 out_resp->connector_type = connector->connector_type;
1988 out_resp->connector_type_id = connector->connector_type_id;
1989 out_resp->mm_width = connector->display_info.width_mm;
1990 out_resp->mm_height = connector->display_info.height_mm;
1991 out_resp->subpixel = connector->display_info.subpixel_order;
1992 out_resp->connection = connector->status;
1993 if (connector->encoder)
1994 out_resp->encoder_id = connector->encoder->base.id;
1996 out_resp->encoder_id = 0;
1999 * This ioctl is called twice, once to determine how much space is
2000 * needed, and the 2nd time to fill it.
2002 if ((out_resp->count_modes >= mode_count) && mode_count) {
2004 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2005 list_for_each_entry(mode, &connector->modes, head) {
2006 if (!drm_mode_expose_to_userspace(mode, file_priv))
2009 drm_crtc_convert_to_umode(&u_mode, mode);
2010 if (copy_to_user(mode_ptr + copied,
2011 &u_mode, sizeof(u_mode))) {
2018 out_resp->count_modes = mode_count;
2020 if ((out_resp->count_props >= props_count) && props_count) {
2022 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2023 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
2024 for (i = 0; i < connector->properties.count; i++) {
2025 if (put_user(connector->properties.ids[i],
2026 prop_ptr + copied)) {
2031 if (put_user(connector->properties.values[i],
2032 prop_values + copied)) {
2039 out_resp->count_props = props_count;
2041 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2043 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2044 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2045 if (connector->encoder_ids[i] != 0) {
2046 if (put_user(connector->encoder_ids[i],
2047 encoder_ptr + copied)) {
2055 out_resp->count_encoders = encoders_count;
2058 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2059 mutex_unlock(&dev->mode_config.mutex);
2065 * drm_mode_getencoder - get encoder configuration
2066 * @dev: drm device for the ioctl
2067 * @data: data pointer for the ioctl
2068 * @file_priv: drm file for the ioctl call
2070 * Construct a encoder configuration structure to return to the user.
2072 * Called by the user via ioctl.
2075 * Zero on success, errno on failure.
2077 int drm_mode_getencoder(struct drm_device *dev, void *data,
2078 struct drm_file *file_priv)
2080 struct drm_mode_get_encoder *enc_resp = data;
2081 struct drm_encoder *encoder;
2084 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2087 drm_modeset_lock_all(dev);
2088 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2095 enc_resp->crtc_id = encoder->crtc->base.id;
2097 enc_resp->crtc_id = 0;
2098 enc_resp->encoder_type = encoder->encoder_type;
2099 enc_resp->encoder_id = encoder->base.id;
2100 enc_resp->possible_crtcs = encoder->possible_crtcs;
2101 enc_resp->possible_clones = encoder->possible_clones;
2104 drm_modeset_unlock_all(dev);
2109 * drm_mode_getplane_res - enumerate all plane resources
2112 * @file_priv: DRM file info
2114 * Construct a list of plane ids to return to the user.
2116 * Called by the user via ioctl.
2119 * Zero on success, errno on failure.
2121 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2122 struct drm_file *file_priv)
2124 struct drm_mode_get_plane_res *plane_resp = data;
2125 struct drm_mode_config *config;
2126 struct drm_plane *plane;
2127 uint32_t __user *plane_ptr;
2128 int copied = 0, ret = 0;
2129 unsigned num_planes;
2131 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2134 drm_modeset_lock_all(dev);
2135 config = &dev->mode_config;
2137 if (file_priv->universal_planes)
2138 num_planes = config->num_total_plane;
2140 num_planes = config->num_overlay_plane;
2143 * This ioctl is called twice, once to determine how much space is
2144 * needed, and the 2nd time to fill it.
2147 (plane_resp->count_planes >= num_planes)) {
2148 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2150 list_for_each_entry(plane, &config->plane_list, head) {
2152 * Unless userspace set the 'universal planes'
2153 * capability bit, only advertise overlays.
2155 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2156 !file_priv->universal_planes)
2159 if (put_user(plane->base.id, plane_ptr + copied)) {
2166 plane_resp->count_planes = num_planes;
2169 drm_modeset_unlock_all(dev);
2174 * drm_mode_getplane - get plane configuration
2177 * @file_priv: DRM file info
2179 * Construct a plane configuration structure to return to the user.
2181 * Called by the user via ioctl.
2184 * Zero on success, errno on failure.
2186 int drm_mode_getplane(struct drm_device *dev, void *data,
2187 struct drm_file *file_priv)
2189 struct drm_mode_get_plane *plane_resp = data;
2190 struct drm_plane *plane;
2191 uint32_t __user *format_ptr;
2194 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2197 drm_modeset_lock_all(dev);
2198 plane = drm_plane_find(dev, plane_resp->plane_id);
2205 plane_resp->crtc_id = plane->crtc->base.id;
2207 plane_resp->crtc_id = 0;
2210 plane_resp->fb_id = plane->fb->base.id;
2212 plane_resp->fb_id = 0;
2214 plane_resp->plane_id = plane->base.id;
2215 plane_resp->possible_crtcs = plane->possible_crtcs;
2216 plane_resp->gamma_size = 0;
2219 * This ioctl is called twice, once to determine how much space is
2220 * needed, and the 2nd time to fill it.
2222 if (plane->format_count &&
2223 (plane_resp->count_format_types >= plane->format_count)) {
2224 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2225 if (copy_to_user(format_ptr,
2226 plane->format_types,
2227 sizeof(uint32_t) * plane->format_count)) {
2232 plane_resp->count_format_types = plane->format_count;
2235 drm_modeset_unlock_all(dev);
2240 * setplane_internal - setplane handler for internal callers
2242 * Note that we assume an extra reference has already been taken on fb. If the
2243 * update fails, this reference will be dropped before return; if it succeeds,
2244 * the previous framebuffer (if any) will be unreferenced instead.
2246 * src_{x,y,w,h} are provided in 16.16 fixed point format
2248 static int __setplane_internal(struct drm_plane *plane,
2249 struct drm_crtc *crtc,
2250 struct drm_framebuffer *fb,
2251 int32_t crtc_x, int32_t crtc_y,
2252 uint32_t crtc_w, uint32_t crtc_h,
2253 /* src_{x,y,w,h} values are 16.16 fixed point */
2254 uint32_t src_x, uint32_t src_y,
2255 uint32_t src_w, uint32_t src_h)
2258 unsigned int fb_width, fb_height;
2261 /* No fb means shut it down */
2263 plane->old_fb = plane->fb;
2264 ret = plane->funcs->disable_plane(plane);
2269 plane->old_fb = NULL;
2274 /* Check whether this plane is usable on this CRTC */
2275 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2276 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2281 /* Check whether this plane supports the fb pixel format. */
2282 for (i = 0; i < plane->format_count; i++)
2283 if (fb->pixel_format == plane->format_types[i])
2285 if (i == plane->format_count) {
2286 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2287 drm_get_format_name(fb->pixel_format));
2292 fb_width = fb->width << 16;
2293 fb_height = fb->height << 16;
2295 /* Make sure source coordinates are inside the fb. */
2296 if (src_w > fb_width ||
2297 src_x > fb_width - src_w ||
2298 src_h > fb_height ||
2299 src_y > fb_height - src_h) {
2300 DRM_DEBUG_KMS("Invalid source coordinates "
2301 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2302 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2303 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2304 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2305 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2310 plane->old_fb = plane->fb;
2311 ret = plane->funcs->update_plane(plane, crtc, fb,
2312 crtc_x, crtc_y, crtc_w, crtc_h,
2313 src_x, src_y, src_w, src_h);
2319 plane->old_fb = NULL;
2324 drm_framebuffer_unreference(fb);
2326 drm_framebuffer_unreference(plane->old_fb);
2327 plane->old_fb = NULL;
2332 static int setplane_internal(struct drm_plane *plane,
2333 struct drm_crtc *crtc,
2334 struct drm_framebuffer *fb,
2335 int32_t crtc_x, int32_t crtc_y,
2336 uint32_t crtc_w, uint32_t crtc_h,
2337 /* src_{x,y,w,h} values are 16.16 fixed point */
2338 uint32_t src_x, uint32_t src_y,
2339 uint32_t src_w, uint32_t src_h)
2343 drm_modeset_lock_all(plane->dev);
2344 ret = __setplane_internal(plane, crtc, fb,
2345 crtc_x, crtc_y, crtc_w, crtc_h,
2346 src_x, src_y, src_w, src_h);
2347 drm_modeset_unlock_all(plane->dev);
2353 * drm_mode_setplane - configure a plane's configuration
2355 * @data: ioctl data*
2356 * @file_priv: DRM file info
2358 * Set plane configuration, including placement, fb, scaling, and other factors.
2359 * Or pass a NULL fb to disable (planes may be disabled without providing a
2363 * Zero on success, errno on failure.
2365 int drm_mode_setplane(struct drm_device *dev, void *data,
2366 struct drm_file *file_priv)
2368 struct drm_mode_set_plane *plane_req = data;
2369 struct drm_mode_object *obj;
2370 struct drm_plane *plane;
2371 struct drm_crtc *crtc = NULL;
2372 struct drm_framebuffer *fb = NULL;
2374 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2377 /* Give drivers some help against integer overflows */
2378 if (plane_req->crtc_w > INT_MAX ||
2379 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2380 plane_req->crtc_h > INT_MAX ||
2381 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2382 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2383 plane_req->crtc_w, plane_req->crtc_h,
2384 plane_req->crtc_x, plane_req->crtc_y);
2389 * First, find the plane, crtc, and fb objects. If not available,
2390 * we don't bother to call the driver.
2392 obj = drm_mode_object_find(dev, plane_req->plane_id,
2393 DRM_MODE_OBJECT_PLANE);
2395 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2396 plane_req->plane_id);
2399 plane = obj_to_plane(obj);
2401 if (plane_req->fb_id) {
2402 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2404 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2409 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2410 DRM_MODE_OBJECT_CRTC);
2412 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2413 plane_req->crtc_id);
2416 crtc = obj_to_crtc(obj);
2420 * setplane_internal will take care of deref'ing either the old or new
2421 * framebuffer depending on success.
2423 return setplane_internal(plane, crtc, fb,
2424 plane_req->crtc_x, plane_req->crtc_y,
2425 plane_req->crtc_w, plane_req->crtc_h,
2426 plane_req->src_x, plane_req->src_y,
2427 plane_req->src_w, plane_req->src_h);
2431 * drm_mode_set_config_internal - helper to call ->set_config
2432 * @set: modeset config to set
2434 * This is a little helper to wrap internal calls to the ->set_config driver
2435 * interface. The only thing it adds is correct refcounting dance.
2438 * Zero on success, errno on failure.
2440 int drm_mode_set_config_internal(struct drm_mode_set *set)
2442 struct drm_crtc *crtc = set->crtc;
2443 struct drm_framebuffer *fb;
2444 struct drm_crtc *tmp;
2448 * NOTE: ->set_config can also disable other crtcs (if we steal all
2449 * connectors from it), hence we need to refcount the fbs across all
2450 * crtcs. Atomic modeset will have saner semantics ...
2452 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2453 tmp->primary->old_fb = tmp->primary->fb;
2457 ret = crtc->funcs->set_config(set);
2459 crtc->primary->crtc = crtc;
2460 crtc->primary->fb = fb;
2463 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2464 if (tmp->primary->fb)
2465 drm_framebuffer_reference(tmp->primary->fb);
2466 if (tmp->primary->old_fb)
2467 drm_framebuffer_unreference(tmp->primary->old_fb);
2468 tmp->primary->old_fb = NULL;
2473 EXPORT_SYMBOL(drm_mode_set_config_internal);
2476 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2478 * @crtc: CRTC that framebuffer will be displayed on
2481 * @mode: mode that framebuffer will be displayed under
2482 * @fb: framebuffer to check size of
2484 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2486 const struct drm_display_mode *mode,
2487 const struct drm_framebuffer *fb)
2490 int hdisplay, vdisplay;
2492 hdisplay = mode->hdisplay;
2493 vdisplay = mode->vdisplay;
2495 if (drm_mode_is_stereo(mode)) {
2496 struct drm_display_mode adjusted = *mode;
2498 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2499 hdisplay = adjusted.crtc_hdisplay;
2500 vdisplay = adjusted.crtc_vdisplay;
2503 if (crtc->invert_dimensions)
2504 swap(hdisplay, vdisplay);
2506 if (hdisplay > fb->width ||
2507 vdisplay > fb->height ||
2508 x > fb->width - hdisplay ||
2509 y > fb->height - vdisplay) {
2510 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2511 fb->width, fb->height, hdisplay, vdisplay, x, y,
2512 crtc->invert_dimensions ? " (inverted)" : "");
2518 EXPORT_SYMBOL(drm_crtc_check_viewport);
2521 * drm_mode_setcrtc - set CRTC configuration
2522 * @dev: drm device for the ioctl
2523 * @data: data pointer for the ioctl
2524 * @file_priv: drm file for the ioctl call
2526 * Build a new CRTC configuration based on user request.
2528 * Called by the user via ioctl.
2531 * Zero on success, errno on failure.
2533 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2534 struct drm_file *file_priv)
2536 struct drm_mode_config *config = &dev->mode_config;
2537 struct drm_mode_crtc *crtc_req = data;
2538 struct drm_crtc *crtc;
2539 struct drm_connector **connector_set = NULL, *connector;
2540 struct drm_framebuffer *fb = NULL;
2541 struct drm_display_mode *mode = NULL;
2542 struct drm_mode_set set;
2543 uint32_t __user *set_connectors_ptr;
2547 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2550 /* For some reason crtc x/y offsets are signed internally. */
2551 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2554 drm_modeset_lock_all(dev);
2555 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2557 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2561 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2563 if (crtc_req->mode_valid) {
2564 /* If we have a mode we need a framebuffer. */
2565 /* If we pass -1, set the mode with the currently bound fb */
2566 if (crtc_req->fb_id == -1) {
2567 if (!crtc->primary->fb) {
2568 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2572 fb = crtc->primary->fb;
2573 /* Make refcounting symmetric with the lookup path. */
2574 drm_framebuffer_reference(fb);
2576 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2578 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2585 mode = drm_mode_create(dev);
2591 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2593 DRM_DEBUG_KMS("Invalid mode\n");
2597 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2599 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2606 if (crtc_req->count_connectors == 0 && mode) {
2607 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2612 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2613 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2614 crtc_req->count_connectors);
2619 if (crtc_req->count_connectors > 0) {
2622 /* Avoid unbounded kernel memory allocation */
2623 if (crtc_req->count_connectors > config->num_connector) {
2628 connector_set = kmalloc(crtc_req->count_connectors *
2629 sizeof(struct drm_connector *),
2631 if (!connector_set) {
2636 for (i = 0; i < crtc_req->count_connectors; i++) {
2637 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2638 if (get_user(out_id, &set_connectors_ptr[i])) {
2643 connector = drm_connector_find(dev, out_id);
2645 DRM_DEBUG_KMS("Connector id %d unknown\n",
2650 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2654 connector_set[i] = connector;
2659 set.x = crtc_req->x;
2660 set.y = crtc_req->y;
2662 set.connectors = connector_set;
2663 set.num_connectors = crtc_req->count_connectors;
2665 ret = drm_mode_set_config_internal(&set);
2669 drm_framebuffer_unreference(fb);
2671 kfree(connector_set);
2672 drm_mode_destroy(dev, mode);
2673 drm_modeset_unlock_all(dev);
2678 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2679 * universal plane handler call
2680 * @crtc: crtc to update cursor for
2681 * @req: data pointer for the ioctl
2682 * @file_priv: drm file for the ioctl call
2684 * Legacy cursor ioctl's work directly with driver buffer handles. To
2685 * translate legacy ioctl calls into universal plane handler calls, we need to
2686 * wrap the native buffer handle in a drm_framebuffer.
2688 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2689 * buffer with a pitch of 4*width; the universal plane interface should be used
2690 * directly in cases where the hardware can support other buffer settings and
2691 * userspace wants to make use of these capabilities.
2694 * Zero on success, errno on failure.
2696 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2697 struct drm_mode_cursor2 *req,
2698 struct drm_file *file_priv)
2700 struct drm_device *dev = crtc->dev;
2701 struct drm_framebuffer *fb = NULL;
2702 struct drm_mode_fb_cmd2 fbreq = {
2703 .width = req->width,
2704 .height = req->height,
2705 .pixel_format = DRM_FORMAT_ARGB8888,
2706 .pitches = { req->width * 4 },
2707 .handles = { req->handle },
2709 int32_t crtc_x, crtc_y;
2710 uint32_t crtc_w = 0, crtc_h = 0;
2711 uint32_t src_w = 0, src_h = 0;
2714 BUG_ON(!crtc->cursor);
2715 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2718 * Obtain fb we'll be using (either new or existing) and take an extra
2719 * reference to it if fb != null. setplane will take care of dropping
2720 * the reference if the plane update fails.
2722 if (req->flags & DRM_MODE_CURSOR_BO) {
2724 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2726 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2730 drm_framebuffer_reference(fb);
2735 fb = crtc->cursor->fb;
2737 drm_framebuffer_reference(fb);
2740 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2744 crtc_x = crtc->cursor_x;
2745 crtc_y = crtc->cursor_y;
2750 crtc_h = fb->height;
2751 src_w = fb->width << 16;
2752 src_h = fb->height << 16;
2756 * setplane_internal will take care of deref'ing either the old or new
2757 * framebuffer depending on success.
2759 ret = __setplane_internal(crtc->cursor, crtc, fb,
2760 crtc_x, crtc_y, crtc_w, crtc_h,
2761 0, 0, src_w, src_h);
2763 /* Update successful; save new cursor position, if necessary */
2764 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2765 crtc->cursor_x = req->x;
2766 crtc->cursor_y = req->y;
2772 static int drm_mode_cursor_common(struct drm_device *dev,
2773 struct drm_mode_cursor2 *req,
2774 struct drm_file *file_priv)
2776 struct drm_crtc *crtc;
2779 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2782 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2785 crtc = drm_crtc_find(dev, req->crtc_id);
2787 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2792 * If this crtc has a universal cursor plane, call that plane's update
2793 * handler rather than using legacy cursor handlers.
2795 drm_modeset_lock_crtc(crtc);
2797 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2801 if (req->flags & DRM_MODE_CURSOR_BO) {
2802 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2806 /* Turns off the cursor if handle is 0 */
2807 if (crtc->funcs->cursor_set2)
2808 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2809 req->width, req->height, req->hot_x, req->hot_y);
2811 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2812 req->width, req->height);
2815 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2816 if (crtc->funcs->cursor_move) {
2817 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2824 drm_modeset_unlock_crtc(crtc);
2832 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2833 * @dev: drm device for the ioctl
2834 * @data: data pointer for the ioctl
2835 * @file_priv: drm file for the ioctl call
2837 * Set the cursor configuration based on user request.
2839 * Called by the user via ioctl.
2842 * Zero on success, errno on failure.
2844 int drm_mode_cursor_ioctl(struct drm_device *dev,
2845 void *data, struct drm_file *file_priv)
2847 struct drm_mode_cursor *req = data;
2848 struct drm_mode_cursor2 new_req;
2850 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2851 new_req.hot_x = new_req.hot_y = 0;
2853 return drm_mode_cursor_common(dev, &new_req, file_priv);
2857 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2858 * @dev: drm device for the ioctl
2859 * @data: data pointer for the ioctl
2860 * @file_priv: drm file for the ioctl call
2862 * Set the cursor configuration based on user request. This implements the 2nd
2863 * version of the cursor ioctl, which allows userspace to additionally specify
2864 * the hotspot of the pointer.
2866 * Called by the user via ioctl.
2869 * Zero on success, errno on failure.
2871 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2872 void *data, struct drm_file *file_priv)
2874 struct drm_mode_cursor2 *req = data;
2875 return drm_mode_cursor_common(dev, req, file_priv);
2879 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2880 * @bpp: bits per pixels
2881 * @depth: bit depth per pixel
2883 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2884 * Useful in fbdev emulation code, since that deals in those values.
2886 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2892 fmt = DRM_FORMAT_C8;
2896 fmt = DRM_FORMAT_XRGB1555;
2898 fmt = DRM_FORMAT_RGB565;
2901 fmt = DRM_FORMAT_RGB888;
2905 fmt = DRM_FORMAT_XRGB8888;
2906 else if (depth == 30)
2907 fmt = DRM_FORMAT_XRGB2101010;
2909 fmt = DRM_FORMAT_ARGB8888;
2912 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2913 fmt = DRM_FORMAT_XRGB8888;
2919 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2922 * drm_mode_addfb - add an FB to the graphics configuration
2923 * @dev: drm device for the ioctl
2924 * @data: data pointer for the ioctl
2925 * @file_priv: drm file for the ioctl call
2927 * Add a new FB to the specified CRTC, given a user request. This is the
2928 * original addfb ioclt which only supported RGB formats.
2930 * Called by the user via ioctl.
2933 * Zero on success, errno on failure.
2935 int drm_mode_addfb(struct drm_device *dev,
2936 void *data, struct drm_file *file_priv)
2938 struct drm_mode_fb_cmd *or = data;
2939 struct drm_mode_fb_cmd2 r = {};
2940 struct drm_mode_config *config = &dev->mode_config;
2941 struct drm_framebuffer *fb;
2944 /* Use new struct with format internally */
2945 r.fb_id = or->fb_id;
2946 r.width = or->width;
2947 r.height = or->height;
2948 r.pitches[0] = or->pitch;
2949 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2950 r.handles[0] = or->handle;
2952 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2955 if ((config->min_width > r.width) || (r.width > config->max_width))
2958 if ((config->min_height > r.height) || (r.height > config->max_height))
2961 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2963 DRM_DEBUG_KMS("could not create framebuffer\n");
2967 mutex_lock(&file_priv->fbs_lock);
2968 or->fb_id = fb->base.id;
2969 list_add(&fb->filp_head, &file_priv->fbs);
2970 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2971 mutex_unlock(&file_priv->fbs_lock);
2976 static int format_check(const struct drm_mode_fb_cmd2 *r)
2978 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2982 case DRM_FORMAT_RGB332:
2983 case DRM_FORMAT_BGR233:
2984 case DRM_FORMAT_XRGB4444:
2985 case DRM_FORMAT_XBGR4444:
2986 case DRM_FORMAT_RGBX4444:
2987 case DRM_FORMAT_BGRX4444:
2988 case DRM_FORMAT_ARGB4444:
2989 case DRM_FORMAT_ABGR4444:
2990 case DRM_FORMAT_RGBA4444:
2991 case DRM_FORMAT_BGRA4444:
2992 case DRM_FORMAT_XRGB1555:
2993 case DRM_FORMAT_XBGR1555:
2994 case DRM_FORMAT_RGBX5551:
2995 case DRM_FORMAT_BGRX5551:
2996 case DRM_FORMAT_ARGB1555:
2997 case DRM_FORMAT_ABGR1555:
2998 case DRM_FORMAT_RGBA5551:
2999 case DRM_FORMAT_BGRA5551:
3000 case DRM_FORMAT_RGB565:
3001 case DRM_FORMAT_BGR565:
3002 case DRM_FORMAT_RGB888:
3003 case DRM_FORMAT_BGR888:
3004 case DRM_FORMAT_XRGB8888:
3005 case DRM_FORMAT_XBGR8888:
3006 case DRM_FORMAT_RGBX8888:
3007 case DRM_FORMAT_BGRX8888:
3008 case DRM_FORMAT_ARGB8888:
3009 case DRM_FORMAT_ABGR8888:
3010 case DRM_FORMAT_RGBA8888:
3011 case DRM_FORMAT_BGRA8888:
3012 case DRM_FORMAT_XRGB2101010:
3013 case DRM_FORMAT_XBGR2101010:
3014 case DRM_FORMAT_RGBX1010102:
3015 case DRM_FORMAT_BGRX1010102:
3016 case DRM_FORMAT_ARGB2101010:
3017 case DRM_FORMAT_ABGR2101010:
3018 case DRM_FORMAT_RGBA1010102:
3019 case DRM_FORMAT_BGRA1010102:
3020 case DRM_FORMAT_YUYV:
3021 case DRM_FORMAT_YVYU:
3022 case DRM_FORMAT_UYVY:
3023 case DRM_FORMAT_VYUY:
3024 case DRM_FORMAT_AYUV:
3025 case DRM_FORMAT_NV12:
3026 case DRM_FORMAT_NV21:
3027 case DRM_FORMAT_NV16:
3028 case DRM_FORMAT_NV61:
3029 case DRM_FORMAT_NV24:
3030 case DRM_FORMAT_NV42:
3031 case DRM_FORMAT_YUV410:
3032 case DRM_FORMAT_YVU410:
3033 case DRM_FORMAT_YUV411:
3034 case DRM_FORMAT_YVU411:
3035 case DRM_FORMAT_YUV420:
3036 case DRM_FORMAT_YVU420:
3037 case DRM_FORMAT_YUV422:
3038 case DRM_FORMAT_YVU422:
3039 case DRM_FORMAT_YUV444:
3040 case DRM_FORMAT_YVU444:
3043 DRM_DEBUG_KMS("invalid pixel format %s\n",
3044 drm_get_format_name(r->pixel_format));
3049 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3051 int ret, hsub, vsub, num_planes, i;
3053 ret = format_check(r);
3055 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3056 drm_get_format_name(r->pixel_format));
3060 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3061 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3062 num_planes = drm_format_num_planes(r->pixel_format);
3064 if (r->width == 0 || r->width % hsub) {
3065 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
3069 if (r->height == 0 || r->height % vsub) {
3070 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3074 for (i = 0; i < num_planes; i++) {
3075 unsigned int width = r->width / (i != 0 ? hsub : 1);
3076 unsigned int height = r->height / (i != 0 ? vsub : 1);
3077 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3079 if (!r->handles[i]) {
3080 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3084 if ((uint64_t) width * cpp > UINT_MAX)
3087 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3090 if (r->pitches[i] < width * cpp) {
3091 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3099 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3100 struct drm_mode_fb_cmd2 *r,
3101 struct drm_file *file_priv)
3103 struct drm_mode_config *config = &dev->mode_config;
3104 struct drm_framebuffer *fb;
3107 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3108 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3109 return ERR_PTR(-EINVAL);
3112 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3113 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3114 r->width, config->min_width, config->max_width);
3115 return ERR_PTR(-EINVAL);
3117 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3118 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3119 r->height, config->min_height, config->max_height);
3120 return ERR_PTR(-EINVAL);
3123 ret = framebuffer_check(r);
3125 return ERR_PTR(ret);
3127 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3129 DRM_DEBUG_KMS("could not create framebuffer\n");
3133 mutex_lock(&file_priv->fbs_lock);
3134 r->fb_id = fb->base.id;
3135 list_add(&fb->filp_head, &file_priv->fbs);
3136 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3137 mutex_unlock(&file_priv->fbs_lock);
3143 * drm_mode_addfb2 - add an FB to the graphics configuration
3144 * @dev: drm device for the ioctl
3145 * @data: data pointer for the ioctl
3146 * @file_priv: drm file for the ioctl call
3148 * Add a new FB to the specified CRTC, given a user request with format. This is
3149 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3150 * and uses fourcc codes as pixel format specifiers.
3152 * Called by the user via ioctl.
3155 * Zero on success, errno on failure.
3157 int drm_mode_addfb2(struct drm_device *dev,
3158 void *data, struct drm_file *file_priv)
3160 struct drm_framebuffer *fb;
3162 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3165 fb = add_framebuffer_internal(dev, data, file_priv);
3173 * drm_mode_rmfb - remove an FB from the configuration
3174 * @dev: drm device for the ioctl
3175 * @data: data pointer for the ioctl
3176 * @file_priv: drm file for the ioctl call
3178 * Remove the FB specified by the user.
3180 * Called by the user via ioctl.
3183 * Zero on success, errno on failure.
3185 int drm_mode_rmfb(struct drm_device *dev,
3186 void *data, struct drm_file *file_priv)
3188 struct drm_framebuffer *fb = NULL;
3189 struct drm_framebuffer *fbl = NULL;
3190 uint32_t *id = data;
3193 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3196 mutex_lock(&file_priv->fbs_lock);
3197 mutex_lock(&dev->mode_config.fb_lock);
3198 fb = __drm_framebuffer_lookup(dev, *id);
3202 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3208 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3209 __drm_framebuffer_unregister(dev, fb);
3211 list_del_init(&fb->filp_head);
3212 mutex_unlock(&dev->mode_config.fb_lock);
3213 mutex_unlock(&file_priv->fbs_lock);
3215 drm_framebuffer_remove(fb);
3220 mutex_unlock(&dev->mode_config.fb_lock);
3221 mutex_unlock(&file_priv->fbs_lock);
3227 * drm_mode_getfb - get FB info
3228 * @dev: drm device for the ioctl
3229 * @data: data pointer for the ioctl
3230 * @file_priv: drm file for the ioctl call
3232 * Lookup the FB given its ID and return info about it.
3234 * Called by the user via ioctl.
3237 * Zero on success, errno on failure.
3239 int drm_mode_getfb(struct drm_device *dev,
3240 void *data, struct drm_file *file_priv)
3242 struct drm_mode_fb_cmd *r = data;
3243 struct drm_framebuffer *fb;
3246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3249 fb = drm_framebuffer_lookup(dev, r->fb_id);
3253 r->height = fb->height;
3254 r->width = fb->width;
3255 r->depth = fb->depth;
3256 r->bpp = fb->bits_per_pixel;
3257 r->pitch = fb->pitches[0];
3258 if (fb->funcs->create_handle) {
3259 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
3264 drm_framebuffer_unreference(fb);
3270 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3271 * @dev: drm device for the ioctl
3272 * @data: data pointer for the ioctl
3273 * @file_priv: drm file for the ioctl call
3275 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3276 * rectangle list. Generic userspace which does frontbuffer rendering must call
3277 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3278 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3280 * Modesetting drivers which always update the frontbuffer do not need to
3281 * implement the corresponding ->dirty framebuffer callback.
3283 * Called by the user via ioctl.
3286 * Zero on success, errno on failure.
3288 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3289 void *data, struct drm_file *file_priv)
3291 struct drm_clip_rect __user *clips_ptr;
3292 struct drm_clip_rect *clips = NULL;
3293 struct drm_mode_fb_dirty_cmd *r = data;
3294 struct drm_framebuffer *fb;
3299 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3302 fb = drm_framebuffer_lookup(dev, r->fb_id);
3306 num_clips = r->num_clips;
3307 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3309 if (!num_clips != !clips_ptr) {
3314 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3316 /* If userspace annotates copy, clips must come in pairs */
3317 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3322 if (num_clips && clips_ptr) {
3323 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3327 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3333 ret = copy_from_user(clips, clips_ptr,
3334 num_clips * sizeof(*clips));
3341 if (fb->funcs->dirty) {
3342 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3351 drm_framebuffer_unreference(fb);
3358 * drm_fb_release - remove and free the FBs on this file
3359 * @priv: drm file for the ioctl
3361 * Destroy all the FBs associated with @filp.
3363 * Called by the user via ioctl.
3366 * Zero on success, errno on failure.
3368 void drm_fb_release(struct drm_file *priv)
3370 struct drm_device *dev = priv->dev;
3371 struct drm_framebuffer *fb, *tfb;
3374 * When the file gets released that means no one else can access the fb
3375 * list any more, so no need to grab fpriv->fbs_lock. And we need to to
3376 * avoid upsetting lockdep since the universal cursor code adds a
3377 * framebuffer while holding mutex locks.
3379 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3380 * locks is impossible here since no one else but this function can get
3383 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3385 mutex_lock(&dev->mode_config.fb_lock);
3386 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3387 __drm_framebuffer_unregister(dev, fb);
3388 mutex_unlock(&dev->mode_config.fb_lock);
3390 list_del_init(&fb->filp_head);
3392 /* This will also drop the fpriv->fbs reference. */
3393 drm_framebuffer_remove(fb);
3398 * drm_property_create - create a new property type
3400 * @flags: flags specifying the property type
3401 * @name: name of the property
3402 * @num_values: number of pre-defined values
3404 * This creates a new generic drm property which can then be attached to a drm
3405 * object with drm_object_attach_property. The returned property object must be
3406 * freed with drm_property_destroy.
3409 * A pointer to the newly created property on success, NULL on failure.
3411 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3412 const char *name, int num_values)
3414 struct drm_property *property = NULL;
3417 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3421 property->dev = dev;
3424 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3425 if (!property->values)
3429 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3433 property->flags = flags;
3434 property->num_values = num_values;
3435 INIT_LIST_HEAD(&property->enum_blob_list);
3438 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3439 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3442 list_add_tail(&property->head, &dev->mode_config.property_list);
3444 WARN_ON(!drm_property_type_valid(property));
3448 kfree(property->values);
3452 EXPORT_SYMBOL(drm_property_create);
3455 * drm_property_create_enum - create a new enumeration property type
3457 * @flags: flags specifying the property type
3458 * @name: name of the property
3459 * @props: enumeration lists with property values
3460 * @num_values: number of pre-defined values
3462 * This creates a new generic drm property which can then be attached to a drm
3463 * object with drm_object_attach_property. The returned property object must be
3464 * freed with drm_property_destroy.
3466 * Userspace is only allowed to set one of the predefined values for enumeration
3470 * A pointer to the newly created property on success, NULL on failure.
3472 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3474 const struct drm_prop_enum_list *props,
3477 struct drm_property *property;
3480 flags |= DRM_MODE_PROP_ENUM;
3482 property = drm_property_create(dev, flags, name, num_values);
3486 for (i = 0; i < num_values; i++) {
3487 ret = drm_property_add_enum(property, i,
3491 drm_property_destroy(dev, property);
3498 EXPORT_SYMBOL(drm_property_create_enum);
3501 * drm_property_create_bitmask - create a new bitmask property type
3503 * @flags: flags specifying the property type
3504 * @name: name of the property
3505 * @props: enumeration lists with property bitflags
3506 * @num_props: size of the @props array
3507 * @supported_bits: bitmask of all supported enumeration values
3509 * This creates a new bitmask drm property which can then be attached to a drm
3510 * object with drm_object_attach_property. The returned property object must be
3511 * freed with drm_property_destroy.
3513 * Compared to plain enumeration properties userspace is allowed to set any
3514 * or'ed together combination of the predefined property bitflag values
3517 * A pointer to the newly created property on success, NULL on failure.
3519 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3520 int flags, const char *name,
3521 const struct drm_prop_enum_list *props,
3523 uint64_t supported_bits)
3525 struct drm_property *property;
3526 int i, ret, index = 0;
3527 int num_values = hweight64(supported_bits);
3529 flags |= DRM_MODE_PROP_BITMASK;
3531 property = drm_property_create(dev, flags, name, num_values);
3534 for (i = 0; i < num_props; i++) {
3535 if (!(supported_bits & (1ULL << props[i].type)))
3538 if (WARN_ON(index >= num_values)) {
3539 drm_property_destroy(dev, property);
3543 ret = drm_property_add_enum(property, index++,
3547 drm_property_destroy(dev, property);
3554 EXPORT_SYMBOL(drm_property_create_bitmask);
3556 static struct drm_property *property_create_range(struct drm_device *dev,
3557 int flags, const char *name,
3558 uint64_t min, uint64_t max)
3560 struct drm_property *property;
3562 property = drm_property_create(dev, flags, name, 2);
3566 property->values[0] = min;
3567 property->values[1] = max;
3573 * drm_property_create_range - create a new ranged property type
3575 * @flags: flags specifying the property type
3576 * @name: name of the property
3577 * @min: minimum value of the property
3578 * @max: maximum value of the property
3580 * This creates a new generic drm property which can then be attached to a drm
3581 * object with drm_object_attach_property. The returned property object must be
3582 * freed with drm_property_destroy.
3584 * Userspace is allowed to set any interger value in the (min, max) range
3588 * A pointer to the newly created property on success, NULL on failure.
3590 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3592 uint64_t min, uint64_t max)
3594 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3597 EXPORT_SYMBOL(drm_property_create_range);
3599 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3600 int flags, const char *name,
3601 int64_t min, int64_t max)
3603 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3604 name, I642U64(min), I642U64(max));
3606 EXPORT_SYMBOL(drm_property_create_signed_range);
3608 struct drm_property *drm_property_create_object(struct drm_device *dev,
3609 int flags, const char *name, uint32_t type)
3611 struct drm_property *property;
3613 flags |= DRM_MODE_PROP_OBJECT;
3615 property = drm_property_create(dev, flags, name, 1);
3619 property->values[0] = type;
3623 EXPORT_SYMBOL(drm_property_create_object);
3626 * drm_property_add_enum - add a possible value to an enumeration property
3627 * @property: enumeration property to change
3628 * @index: index of the new enumeration
3629 * @value: value of the new enumeration
3630 * @name: symbolic name of the new enumeration
3632 * This functions adds enumerations to a property.
3634 * It's use is deprecated, drivers should use one of the more specific helpers
3635 * to directly create the property with all enumerations already attached.
3638 * Zero on success, error code on failure.
3640 int drm_property_add_enum(struct drm_property *property, int index,
3641 uint64_t value, const char *name)
3643 struct drm_property_enum *prop_enum;
3645 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3646 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3650 * Bitmask enum properties have the additional constraint of values
3653 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3657 if (!list_empty(&property->enum_blob_list)) {
3658 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3659 if (prop_enum->value == value) {
3660 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3661 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3667 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3671 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3672 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3673 prop_enum->value = value;
3675 property->values[index] = value;
3676 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3679 EXPORT_SYMBOL(drm_property_add_enum);
3682 * drm_property_destroy - destroy a drm property
3684 * @property: property to destry
3686 * This function frees a property including any attached resources like
3687 * enumeration values.
3689 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3691 struct drm_property_enum *prop_enum, *pt;
3693 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3694 list_del(&prop_enum->head);
3698 if (property->num_values)
3699 kfree(property->values);
3700 drm_mode_object_put(dev, &property->base);
3701 list_del(&property->head);
3704 EXPORT_SYMBOL(drm_property_destroy);
3707 * drm_object_attach_property - attach a property to a modeset object
3708 * @obj: drm modeset object
3709 * @property: property to attach
3710 * @init_val: initial value of the property
3712 * This attaches the given property to the modeset object with the given initial
3713 * value. Currently this function cannot fail since the properties are stored in
3714 * a statically sized array.
3716 void drm_object_attach_property(struct drm_mode_object *obj,
3717 struct drm_property *property,
3720 int count = obj->properties->count;
3722 if (count == DRM_OBJECT_MAX_PROPERTY) {
3723 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3724 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3725 "you see this message on the same object type.\n",
3730 obj->properties->ids[count] = property->base.id;
3731 obj->properties->values[count] = init_val;
3732 obj->properties->count++;
3734 EXPORT_SYMBOL(drm_object_attach_property);
3737 * drm_object_property_set_value - set the value of a property
3738 * @obj: drm mode object to set property value for
3739 * @property: property to set
3740 * @val: value the property should be set to
3742 * This functions sets a given property on a given object. This function only
3743 * changes the software state of the property, it does not call into the
3744 * driver's ->set_property callback.
3747 * Zero on success, error code on failure.
3749 int drm_object_property_set_value(struct drm_mode_object *obj,
3750 struct drm_property *property, uint64_t val)
3754 for (i = 0; i < obj->properties->count; i++) {
3755 if (obj->properties->ids[i] == property->base.id) {
3756 obj->properties->values[i] = val;
3763 EXPORT_SYMBOL(drm_object_property_set_value);
3766 * drm_object_property_get_value - retrieve the value of a property
3767 * @obj: drm mode object to get property value from
3768 * @property: property to retrieve
3769 * @val: storage for the property value
3771 * This function retrieves the softare state of the given property for the given
3772 * property. Since there is no driver callback to retrieve the current property
3773 * value this might be out of sync with the hardware, depending upon the driver
3777 * Zero on success, error code on failure.
3779 int drm_object_property_get_value(struct drm_mode_object *obj,
3780 struct drm_property *property, uint64_t *val)
3784 for (i = 0; i < obj->properties->count; i++) {
3785 if (obj->properties->ids[i] == property->base.id) {
3786 *val = obj->properties->values[i];
3793 EXPORT_SYMBOL(drm_object_property_get_value);
3796 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3799 * @file_priv: DRM file info
3801 * This function retrieves the current value for an connectors's property.
3803 * Called by the user via ioctl.
3806 * Zero on success, errno on failure.
3808 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3809 void *data, struct drm_file *file_priv)
3811 struct drm_mode_get_property *out_resp = data;
3812 struct drm_property *property;
3815 int value_count = 0;
3818 struct drm_property_enum *prop_enum;
3819 struct drm_mode_property_enum __user *enum_ptr;
3820 struct drm_property_blob *prop_blob;
3821 uint32_t __user *blob_id_ptr;
3822 uint64_t __user *values_ptr;
3823 uint32_t __user *blob_length_ptr;
3825 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3828 drm_modeset_lock_all(dev);
3829 property = drm_property_find(dev, out_resp->prop_id);
3835 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3836 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3837 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3839 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3840 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3844 value_count = property->num_values;
3846 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3847 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3848 out_resp->flags = property->flags;
3850 if ((out_resp->count_values >= value_count) && value_count) {
3851 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3852 for (i = 0; i < value_count; i++) {
3853 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3859 out_resp->count_values = value_count;
3861 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3862 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3863 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3865 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3866 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3868 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3873 if (copy_to_user(&enum_ptr[copied].name,
3874 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3881 out_resp->count_enum_blobs = enum_count;
3884 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3885 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3887 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3888 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3890 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3891 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3896 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3904 out_resp->count_enum_blobs = blob_count;
3907 drm_modeset_unlock_all(dev);
3911 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3914 struct drm_property_blob *blob;
3917 if (!length || !data)
3920 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3924 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3930 blob->length = length;
3932 memcpy(blob->data, data, length);
3934 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3938 static void drm_property_destroy_blob(struct drm_device *dev,
3939 struct drm_property_blob *blob)
3941 drm_mode_object_put(dev, &blob->base);
3942 list_del(&blob->head);
3947 * drm_mode_getblob_ioctl - get the contents of a blob property value
3950 * @file_priv: DRM file info
3952 * This function retrieves the contents of a blob property. The value stored in
3953 * an object's blob property is just a normal modeset object id.
3955 * Called by the user via ioctl.
3958 * Zero on success, errno on failure.
3960 int drm_mode_getblob_ioctl(struct drm_device *dev,
3961 void *data, struct drm_file *file_priv)
3963 struct drm_mode_get_blob *out_resp = data;
3964 struct drm_property_blob *blob;
3966 void __user *blob_ptr;
3968 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3971 drm_modeset_lock_all(dev);
3972 blob = drm_property_blob_find(dev, out_resp->blob_id);
3978 if (out_resp->length == blob->length) {
3979 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3980 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3985 out_resp->length = blob->length;
3988 drm_modeset_unlock_all(dev);
3992 int drm_mode_connector_set_path_property(struct drm_connector *connector,
3995 struct drm_device *dev = connector->dev;
3997 size = strlen(path) + 1;
3999 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4001 if (!connector->path_blob_ptr)
4004 ret = drm_object_property_set_value(&connector->base,
4005 dev->mode_config.path_property,
4006 connector->path_blob_ptr->base.id);
4009 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4012 * drm_mode_connector_update_edid_property - update the edid property of a connector
4013 * @connector: drm connector
4014 * @edid: new value of the edid property
4016 * This function creates a new blob modeset object and assigns its id to the
4017 * connector's edid property.
4020 * Zero on success, errno on failure.
4022 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4025 struct drm_device *dev = connector->dev;
4028 /* ignore requests to set edid when overridden */
4029 if (connector->override_edid)
4032 if (connector->edid_blob_ptr)
4033 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4035 /* Delete edid, when there is none. */
4037 connector->edid_blob_ptr = NULL;
4038 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
4042 size = EDID_LENGTH * (1 + edid->extensions);
4043 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4045 if (!connector->edid_blob_ptr)
4048 ret = drm_object_property_set_value(&connector->base,
4049 dev->mode_config.edid_property,
4050 connector->edid_blob_ptr->base.id);
4054 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4056 static bool drm_property_change_is_valid(struct drm_property *property,
4059 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4062 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4063 if (value < property->values[0] || value > property->values[1])
4066 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4067 int64_t svalue = U642I64(value);
4068 if (svalue < U642I64(property->values[0]) ||
4069 svalue > U642I64(property->values[1]))
4072 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4074 uint64_t valid_mask = 0;
4075 for (i = 0; i < property->num_values; i++)
4076 valid_mask |= (1ULL << property->values[i]);
4077 return !(value & ~valid_mask);
4078 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4079 /* Only the driver knows */
4081 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4082 struct drm_mode_object *obj;
4083 /* a zero value for an object property translates to null: */
4087 * NOTE: use _object_find() directly to bypass restriction on
4088 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4089 * object this could race against object finalization, so it
4090 * simply tells us that the object *was* valid. Which is good
4093 obj = _object_find(property->dev, value, property->values[0]);
4097 for (i = 0; i < property->num_values; i++)
4098 if (property->values[i] == value)
4105 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4108 * @file_priv: DRM file info
4110 * This function sets the current value for a connectors's property. It also
4111 * calls into a driver's ->set_property callback to update the hardware state
4113 * Called by the user via ioctl.
4116 * Zero on success, errno on failure.
4118 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4119 void *data, struct drm_file *file_priv)
4121 struct drm_mode_connector_set_property *conn_set_prop = data;
4122 struct drm_mode_obj_set_property obj_set_prop = {
4123 .value = conn_set_prop->value,
4124 .prop_id = conn_set_prop->prop_id,
4125 .obj_id = conn_set_prop->connector_id,
4126 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4129 /* It does all the locking and checking we need */
4130 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4133 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4134 struct drm_property *property,
4138 struct drm_connector *connector = obj_to_connector(obj);
4140 /* Do DPMS ourselves */
4141 if (property == connector->dev->mode_config.dpms_property) {
4142 if (connector->funcs->dpms)
4143 (*connector->funcs->dpms)(connector, (int)value);
4145 } else if (connector->funcs->set_property)
4146 ret = connector->funcs->set_property(connector, property, value);
4148 /* store the property value if successful */
4150 drm_object_property_set_value(&connector->base, property, value);
4154 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4155 struct drm_property *property,
4159 struct drm_crtc *crtc = obj_to_crtc(obj);
4161 if (crtc->funcs->set_property)
4162 ret = crtc->funcs->set_property(crtc, property, value);
4164 drm_object_property_set_value(obj, property, value);
4170 * drm_mode_plane_set_obj_prop - set the value of a property
4171 * @plane: drm plane object to set property value for
4172 * @property: property to set
4173 * @value: value the property should be set to
4175 * This functions sets a given property on a given plane object. This function
4176 * calls the driver's ->set_property callback and changes the software state of
4177 * the property if the callback succeeds.
4180 * Zero on success, error code on failure.
4182 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4183 struct drm_property *property,
4187 struct drm_mode_object *obj = &plane->base;
4189 if (plane->funcs->set_property)
4190 ret = plane->funcs->set_property(plane, property, value);
4192 drm_object_property_set_value(obj, property, value);
4196 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4199 * drm_mode_getproperty_ioctl - get the current value of a object's property
4202 * @file_priv: DRM file info
4204 * This function retrieves the current value for an object's property. Compared
4205 * to the connector specific ioctl this one is extended to also work on crtc and
4208 * Called by the user via ioctl.
4211 * Zero on success, errno on failure.
4213 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4214 struct drm_file *file_priv)
4216 struct drm_mode_obj_get_properties *arg = data;
4217 struct drm_mode_object *obj;
4221 int props_count = 0;
4222 uint32_t __user *props_ptr;
4223 uint64_t __user *prop_values_ptr;
4225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4228 drm_modeset_lock_all(dev);
4230 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4235 if (!obj->properties) {
4240 props_count = obj->properties->count;
4242 /* This ioctl is called twice, once to determine how much space is
4243 * needed, and the 2nd time to fill it. */
4244 if ((arg->count_props >= props_count) && props_count) {
4246 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4247 prop_values_ptr = (uint64_t __user *)(unsigned long)
4248 (arg->prop_values_ptr);
4249 for (i = 0; i < props_count; i++) {
4250 if (put_user(obj->properties->ids[i],
4251 props_ptr + copied)) {
4255 if (put_user(obj->properties->values[i],
4256 prop_values_ptr + copied)) {
4263 arg->count_props = props_count;
4265 drm_modeset_unlock_all(dev);
4270 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4273 * @file_priv: DRM file info
4275 * This function sets the current value for an object's property. It also calls
4276 * into a driver's ->set_property callback to update the hardware state.
4277 * Compared to the connector specific ioctl this one is extended to also work on
4278 * crtc and plane objects.
4280 * Called by the user via ioctl.
4283 * Zero on success, errno on failure.
4285 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4286 struct drm_file *file_priv)
4288 struct drm_mode_obj_set_property *arg = data;
4289 struct drm_mode_object *arg_obj;
4290 struct drm_mode_object *prop_obj;
4291 struct drm_property *property;
4295 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4298 drm_modeset_lock_all(dev);
4300 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4305 if (!arg_obj->properties)
4308 for (i = 0; i < arg_obj->properties->count; i++)
4309 if (arg_obj->properties->ids[i] == arg->prop_id)
4312 if (i == arg_obj->properties->count)
4315 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4316 DRM_MODE_OBJECT_PROPERTY);
4321 property = obj_to_property(prop_obj);
4323 if (!drm_property_change_is_valid(property, arg->value))
4326 switch (arg_obj->type) {
4327 case DRM_MODE_OBJECT_CONNECTOR:
4328 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4331 case DRM_MODE_OBJECT_CRTC:
4332 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4334 case DRM_MODE_OBJECT_PLANE:
4335 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4336 property, arg->value);
4341 drm_modeset_unlock_all(dev);
4346 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4347 * @connector: connector to attach
4348 * @encoder: encoder to attach @connector to
4350 * This function links up a connector to an encoder. Note that the routing
4351 * restrictions between encoders and crtcs are exposed to userspace through the
4352 * possible_clones and possible_crtcs bitmasks.
4355 * Zero on success, errno on failure.
4357 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4358 struct drm_encoder *encoder)
4362 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4363 if (connector->encoder_ids[i] == 0) {
4364 connector->encoder_ids[i] = encoder->base.id;
4370 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4373 * drm_mode_crtc_set_gamma_size - set the gamma table size
4374 * @crtc: CRTC to set the gamma table size for
4375 * @gamma_size: size of the gamma table
4377 * Drivers which support gamma tables should set this to the supported gamma
4378 * table size when initializing the CRTC. Currently the drm core only supports a
4379 * fixed gamma table size.
4382 * Zero on success, errno on failure.
4384 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4387 crtc->gamma_size = gamma_size;
4389 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4390 if (!crtc->gamma_store) {
4391 crtc->gamma_size = 0;
4397 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4400 * drm_mode_gamma_set_ioctl - set the gamma table
4403 * @file_priv: DRM file info
4405 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4406 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4408 * Called by the user via ioctl.
4411 * Zero on success, errno on failure.
4413 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4414 void *data, struct drm_file *file_priv)
4416 struct drm_mode_crtc_lut *crtc_lut = data;
4417 struct drm_crtc *crtc;
4418 void *r_base, *g_base, *b_base;
4422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4425 drm_modeset_lock_all(dev);
4426 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4432 if (crtc->funcs->gamma_set == NULL) {
4437 /* memcpy into gamma store */
4438 if (crtc_lut->gamma_size != crtc->gamma_size) {
4443 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4444 r_base = crtc->gamma_store;
4445 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4450 g_base = (char *)r_base + size;
4451 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4456 b_base = (char *)g_base + size;
4457 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4462 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4465 drm_modeset_unlock_all(dev);
4471 * drm_mode_gamma_get_ioctl - get the gamma table
4474 * @file_priv: DRM file info
4476 * Copy the current gamma table into the storage provided. This also provides
4477 * the gamma table size the driver expects, which can be used to size the
4478 * allocated storage.
4480 * Called by the user via ioctl.
4483 * Zero on success, errno on failure.
4485 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4486 void *data, struct drm_file *file_priv)
4488 struct drm_mode_crtc_lut *crtc_lut = data;
4489 struct drm_crtc *crtc;
4490 void *r_base, *g_base, *b_base;
4494 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4497 drm_modeset_lock_all(dev);
4498 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4504 /* memcpy into gamma store */
4505 if (crtc_lut->gamma_size != crtc->gamma_size) {
4510 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4511 r_base = crtc->gamma_store;
4512 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4517 g_base = (char *)r_base + size;
4518 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4523 b_base = (char *)g_base + size;
4524 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4529 drm_modeset_unlock_all(dev);
4534 * The Linux version of kfree() is a macro and can't be called
4535 * directly via a function pointer
4537 static void drm_kms_free(void *arg)
4543 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4546 * @file_priv: DRM file info
4548 * This schedules an asynchronous update on a given CRTC, called page flip.
4549 * Optionally a drm event is generated to signal the completion of the event.
4550 * Generic drivers cannot assume that a pageflip with changed framebuffer
4551 * properties (including driver specific metadata like tiling layout) will work,
4552 * but some drivers support e.g. pixel format changes through the pageflip
4555 * Called by the user via ioctl.
4558 * Zero on success, errno on failure.
4560 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4561 void *data, struct drm_file *file_priv)
4563 struct drm_mode_crtc_page_flip *page_flip = data;
4564 struct drm_crtc *crtc;
4565 struct drm_framebuffer *fb = NULL;
4566 struct drm_pending_vblank_event *e = NULL;
4569 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4570 page_flip->reserved != 0)
4573 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4576 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4580 drm_modeset_lock_crtc(crtc);
4581 if (crtc->primary->fb == NULL) {
4582 /* The framebuffer is currently unbound, presumably
4583 * due to a hotplug event, that userspace has not
4590 if (crtc->funcs->page_flip == NULL)
4593 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4599 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4603 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4604 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4609 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4611 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4612 if (file_priv->event_space < sizeof e->event) {
4613 lockmgr(&dev->event_lock, LK_RELEASE);
4616 file_priv->event_space -= sizeof e->event;
4617 lockmgr(&dev->event_lock, LK_RELEASE);
4619 e = kzalloc(sizeof *e, GFP_KERNEL);
4621 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4622 file_priv->event_space += sizeof e->event;
4623 lockmgr(&dev->event_lock, LK_RELEASE);
4627 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4628 e->event.base.length = sizeof e->event;
4629 e->event.user_data = page_flip->user_data;
4630 e->base.event = &e->event.base;
4631 e->base.file_priv = file_priv;
4632 #ifdef __DragonFly__
4634 (void (*) (struct drm_pending_event *)) drm_kms_free;
4637 (void (*) (struct drm_pending_event *)) kfree;
4641 crtc->primary->old_fb = crtc->primary->fb;
4642 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4644 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4645 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4646 file_priv->event_space += sizeof e->event;
4647 lockmgr(&dev->event_lock, LK_RELEASE);
4650 /* Keep the old fb, don't unref it. */
4651 crtc->primary->old_fb = NULL;
4654 * Warn if the driver hasn't properly updated the crtc->fb
4655 * field to reflect that the new framebuffer is now used.
4656 * Failing to do so will screw with the reference counting
4659 WARN_ON(crtc->primary->fb != fb);
4660 /* Unref only the old framebuffer. */
4666 drm_framebuffer_unreference(fb);
4667 if (crtc->primary->old_fb)
4668 drm_framebuffer_unreference(crtc->primary->old_fb);
4669 crtc->primary->old_fb = NULL;
4670 drm_modeset_unlock_crtc(crtc);
4676 * drm_mode_config_reset - call ->reset callbacks
4679 * This functions calls all the crtc's, encoder's and connector's ->reset
4680 * callback. Drivers can use this in e.g. their driver load or resume code to
4681 * reset hardware and software state.
4683 void drm_mode_config_reset(struct drm_device *dev)
4685 struct drm_crtc *crtc;
4686 struct drm_plane *plane;
4687 struct drm_encoder *encoder;
4688 struct drm_connector *connector;
4690 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4691 if (plane->funcs->reset)
4692 plane->funcs->reset(plane);
4694 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4695 if (crtc->funcs->reset)
4696 crtc->funcs->reset(crtc);
4698 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4699 if (encoder->funcs->reset)
4700 encoder->funcs->reset(encoder);
4702 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4703 connector->status = connector_status_unknown;
4705 if (connector->funcs->reset)
4706 connector->funcs->reset(connector);
4709 EXPORT_SYMBOL(drm_mode_config_reset);
4712 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4715 * @file_priv: DRM file info
4717 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4718 * TTM or something else entirely) and returns the resulting buffer handle. This
4719 * handle can then be wrapped up into a framebuffer modeset object.
4721 * Note that userspace is not allowed to use such objects for render
4722 * acceleration - drivers must create their own private ioctls for such a use
4725 * Called by the user via ioctl.
4728 * Zero on success, errno on failure.
4730 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4731 void *data, struct drm_file *file_priv)
4733 struct drm_mode_create_dumb *args = data;
4734 u32 cpp, stride, size;
4736 if (!dev->driver->dumb_create)
4738 if (!args->width || !args->height || !args->bpp)
4741 /* overflow checks for 32bit size calculations */
4742 /* NOTE: DIV_ROUND_UP() can overflow */
4743 cpp = DIV_ROUND_UP(args->bpp, 8);
4744 if (!cpp || cpp > 0xffffffffU / args->width)
4746 stride = cpp * args->width;
4747 if (args->height > 0xffffffffU / stride)
4750 /* test for wrap-around */
4751 size = args->height * stride;
4752 if (PAGE_ALIGN(size) == 0)
4755 return dev->driver->dumb_create(file_priv, dev, args);
4759 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4762 * @file_priv: DRM file info
4764 * Allocate an offset in the drm device node's address space to be able to
4765 * memory map a dumb buffer.
4767 * Called by the user via ioctl.
4770 * Zero on success, errno on failure.
4772 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4773 void *data, struct drm_file *file_priv)
4775 struct drm_mode_map_dumb *args = data;
4777 /* call driver ioctl to get mmap offset */
4778 if (!dev->driver->dumb_map_offset)
4781 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4785 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4788 * @file_priv: DRM file info
4790 * This destroys the userspace handle for the given dumb backing storage buffer.
4791 * Since buffer objects must be reference counted in the kernel a buffer object
4792 * won't be immediately freed if a framebuffer modeset object still uses it.
4794 * Called by the user via ioctl.
4797 * Zero on success, errno on failure.
4799 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4800 void *data, struct drm_file *file_priv)
4802 struct drm_mode_destroy_dumb *args = data;
4804 if (!dev->driver->dumb_destroy)
4807 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4811 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4812 * @format: pixel format (DRM_FORMAT_*)
4813 * @depth: storage for the depth value
4814 * @bpp: storage for the bpp value
4816 * This only supports RGB formats here for compat with code that doesn't use
4817 * pixel formats directly yet.
4819 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4824 case DRM_FORMAT_RGB332:
4825 case DRM_FORMAT_BGR233:
4829 case DRM_FORMAT_XRGB1555:
4830 case DRM_FORMAT_XBGR1555:
4831 case DRM_FORMAT_RGBX5551:
4832 case DRM_FORMAT_BGRX5551:
4833 case DRM_FORMAT_ARGB1555:
4834 case DRM_FORMAT_ABGR1555:
4835 case DRM_FORMAT_RGBA5551:
4836 case DRM_FORMAT_BGRA5551:
4840 case DRM_FORMAT_RGB565:
4841 case DRM_FORMAT_BGR565:
4845 case DRM_FORMAT_RGB888:
4846 case DRM_FORMAT_BGR888:
4850 case DRM_FORMAT_XRGB8888:
4851 case DRM_FORMAT_XBGR8888:
4852 case DRM_FORMAT_RGBX8888:
4853 case DRM_FORMAT_BGRX8888:
4857 case DRM_FORMAT_XRGB2101010:
4858 case DRM_FORMAT_XBGR2101010:
4859 case DRM_FORMAT_RGBX1010102:
4860 case DRM_FORMAT_BGRX1010102:
4861 case DRM_FORMAT_ARGB2101010:
4862 case DRM_FORMAT_ABGR2101010:
4863 case DRM_FORMAT_RGBA1010102:
4864 case DRM_FORMAT_BGRA1010102:
4868 case DRM_FORMAT_ARGB8888:
4869 case DRM_FORMAT_ABGR8888:
4870 case DRM_FORMAT_RGBA8888:
4871 case DRM_FORMAT_BGRA8888:
4876 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4877 drm_get_format_name(format));
4883 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4886 * drm_format_num_planes - get the number of planes for format
4887 * @format: pixel format (DRM_FORMAT_*)
4890 * The number of planes used by the specified pixel format.
4892 int drm_format_num_planes(uint32_t format)
4895 case DRM_FORMAT_YUV410:
4896 case DRM_FORMAT_YVU410:
4897 case DRM_FORMAT_YUV411:
4898 case DRM_FORMAT_YVU411:
4899 case DRM_FORMAT_YUV420:
4900 case DRM_FORMAT_YVU420:
4901 case DRM_FORMAT_YUV422:
4902 case DRM_FORMAT_YVU422:
4903 case DRM_FORMAT_YUV444:
4904 case DRM_FORMAT_YVU444:
4906 case DRM_FORMAT_NV12:
4907 case DRM_FORMAT_NV21:
4908 case DRM_FORMAT_NV16:
4909 case DRM_FORMAT_NV61:
4910 case DRM_FORMAT_NV24:
4911 case DRM_FORMAT_NV42:
4917 EXPORT_SYMBOL(drm_format_num_planes);
4920 * drm_format_plane_cpp - determine the bytes per pixel value
4921 * @format: pixel format (DRM_FORMAT_*)
4922 * @plane: plane index
4925 * The bytes per pixel value for the specified plane.
4927 int drm_format_plane_cpp(uint32_t format, int plane)
4932 if (plane >= drm_format_num_planes(format))
4936 case DRM_FORMAT_YUYV:
4937 case DRM_FORMAT_YVYU:
4938 case DRM_FORMAT_UYVY:
4939 case DRM_FORMAT_VYUY:
4941 case DRM_FORMAT_NV12:
4942 case DRM_FORMAT_NV21:
4943 case DRM_FORMAT_NV16:
4944 case DRM_FORMAT_NV61:
4945 case DRM_FORMAT_NV24:
4946 case DRM_FORMAT_NV42:
4947 return plane ? 2 : 1;
4948 case DRM_FORMAT_YUV410:
4949 case DRM_FORMAT_YVU410:
4950 case DRM_FORMAT_YUV411:
4951 case DRM_FORMAT_YVU411:
4952 case DRM_FORMAT_YUV420:
4953 case DRM_FORMAT_YVU420:
4954 case DRM_FORMAT_YUV422:
4955 case DRM_FORMAT_YVU422:
4956 case DRM_FORMAT_YUV444:
4957 case DRM_FORMAT_YVU444:
4960 drm_fb_get_bpp_depth(format, &depth, &bpp);
4964 EXPORT_SYMBOL(drm_format_plane_cpp);
4967 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4968 * @format: pixel format (DRM_FORMAT_*)
4971 * The horizontal chroma subsampling factor for the
4972 * specified pixel format.
4974 int drm_format_horz_chroma_subsampling(uint32_t format)
4977 case DRM_FORMAT_YUV411:
4978 case DRM_FORMAT_YVU411:
4979 case DRM_FORMAT_YUV410:
4980 case DRM_FORMAT_YVU410:
4982 case DRM_FORMAT_YUYV:
4983 case DRM_FORMAT_YVYU:
4984 case DRM_FORMAT_UYVY:
4985 case DRM_FORMAT_VYUY:
4986 case DRM_FORMAT_NV12:
4987 case DRM_FORMAT_NV21:
4988 case DRM_FORMAT_NV16:
4989 case DRM_FORMAT_NV61:
4990 case DRM_FORMAT_YUV422:
4991 case DRM_FORMAT_YVU422:
4992 case DRM_FORMAT_YUV420:
4993 case DRM_FORMAT_YVU420:
4999 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5002 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5003 * @format: pixel format (DRM_FORMAT_*)
5006 * The vertical chroma subsampling factor for the
5007 * specified pixel format.
5009 int drm_format_vert_chroma_subsampling(uint32_t format)
5012 case DRM_FORMAT_YUV410:
5013 case DRM_FORMAT_YVU410:
5015 case DRM_FORMAT_YUV420:
5016 case DRM_FORMAT_YVU420:
5017 case DRM_FORMAT_NV12:
5018 case DRM_FORMAT_NV21:
5024 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5027 * drm_mode_config_init - initialize DRM mode_configuration structure
5030 * Initialize @dev's mode_config structure, used for tracking the graphics
5031 * configuration of @dev.
5033 * Since this initializes the modeset locks, no locking is possible. Which is no
5034 * problem, since this should happen single threaded at init time. It is the
5035 * driver's problem to ensure this guarantee.
5038 void drm_mode_config_init(struct drm_device *dev)
5040 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
5041 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5042 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
5043 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
5044 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5045 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5046 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5047 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
5048 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5049 INIT_LIST_HEAD(&dev->mode_config.property_list);
5050 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5051 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5052 idr_init(&dev->mode_config.crtc_idr);
5054 drm_modeset_lock_all(dev);
5055 drm_mode_create_standard_connector_properties(dev);
5056 drm_mode_create_standard_plane_properties(dev);
5057 drm_modeset_unlock_all(dev);
5059 /* Just to be sure */
5060 dev->mode_config.num_fb = 0;
5061 dev->mode_config.num_connector = 0;
5062 dev->mode_config.num_crtc = 0;
5063 dev->mode_config.num_encoder = 0;
5064 dev->mode_config.num_overlay_plane = 0;
5065 dev->mode_config.num_total_plane = 0;
5067 EXPORT_SYMBOL(drm_mode_config_init);
5070 * drm_mode_config_cleanup - free up DRM mode_config info
5073 * Free up all the connectors and CRTCs associated with this DRM device, then
5074 * free up the framebuffers and associated buffer objects.
5076 * Note that since this /should/ happen single-threaded at driver/device
5077 * teardown time, no locking is required. It's the driver's job to ensure that
5078 * this guarantee actually holds true.
5080 * FIXME: cleanup any dangling user buffer objects too
5082 void drm_mode_config_cleanup(struct drm_device *dev)
5084 struct drm_connector *connector, *ot;
5085 struct drm_crtc *crtc, *ct;
5086 struct drm_encoder *encoder, *enct;
5087 struct drm_bridge *bridge, *brt;
5088 struct drm_framebuffer *fb, *fbt;
5089 struct drm_property *property, *pt;
5090 struct drm_property_blob *blob, *bt;
5091 struct drm_plane *plane, *plt;
5093 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5095 encoder->funcs->destroy(encoder);
5098 list_for_each_entry_safe(bridge, brt,
5099 &dev->mode_config.bridge_list, head) {
5100 bridge->funcs->destroy(bridge);
5103 list_for_each_entry_safe(connector, ot,
5104 &dev->mode_config.connector_list, head) {
5105 connector->funcs->destroy(connector);
5108 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5110 drm_property_destroy(dev, property);
5113 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5115 drm_property_destroy_blob(dev, blob);
5119 * Single-threaded teardown context, so it's not required to grab the
5120 * fb_lock to protect against concurrent fb_list access. Contrary, it
5121 * would actually deadlock with the drm_framebuffer_cleanup function.
5123 * Also, if there are any framebuffers left, that's a driver leak now,
5124 * so politely WARN about this.
5126 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5127 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5128 drm_framebuffer_remove(fb);
5131 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5133 plane->funcs->destroy(plane);
5136 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5137 crtc->funcs->destroy(crtc);
5140 idr_destroy(&dev->mode_config.crtc_idr);
5141 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5143 EXPORT_SYMBOL(drm_mode_config_cleanup);
5145 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5146 unsigned int supported_rotations)
5148 static const struct drm_prop_enum_list props[] = {
5149 { DRM_ROTATE_0, "rotate-0" },
5150 { DRM_ROTATE_90, "rotate-90" },
5151 { DRM_ROTATE_180, "rotate-180" },
5152 { DRM_ROTATE_270, "rotate-270" },
5153 { DRM_REFLECT_X, "reflect-x" },
5154 { DRM_REFLECT_Y, "reflect-y" },
5157 return drm_property_create_bitmask(dev, 0, "rotation",
5158 props, ARRAY_SIZE(props),
5159 supported_rotations);
5161 EXPORT_SYMBOL(drm_mode_create_rotation_property);