2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
42 * drm_modeset_lock_all - take all modeset locks
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented. Locks must be dropped with
47 * drm_modeset_unlock_all.
49 void drm_modeset_lock_all(struct drm_device *dev)
51 struct drm_crtc *crtc;
53 mutex_lock(&dev->mode_config.mutex);
55 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
56 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
58 EXPORT_SYMBOL(drm_modeset_lock_all);
61 * drm_modeset_unlock_all - drop all modeset locks
64 * This function drop all modeset locks taken by drm_modeset_lock_all.
66 void drm_modeset_unlock_all(struct drm_device *dev)
68 struct drm_crtc *crtc;
70 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
71 mutex_unlock(&crtc->mutex);
73 mutex_unlock(&dev->mode_config.mutex);
75 EXPORT_SYMBOL(drm_modeset_unlock_all);
78 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
81 * Useful as a debug assert.
83 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
85 struct drm_crtc *crtc;
87 /* Locking is currently fubar in the panic handler. */
91 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
92 WARN_ON(!mutex_is_locked(&crtc->mutex));
94 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
96 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
98 /* Avoid boilerplate. I'm tired of typing. */
99 #define DRM_ENUM_NAME_FN(fnname, list) \
100 const char *fnname(int val) \
103 for (i = 0; i < ARRAY_SIZE(list); i++) { \
104 if (list[i].type == val) \
105 return list[i].name; \
107 return "(unknown)"; \
113 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
114 { { DRM_MODE_DPMS_ON, "On" },
115 { DRM_MODE_DPMS_STANDBY, "Standby" },
116 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
117 { DRM_MODE_DPMS_OFF, "Off" }
120 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
123 * Optional properties
125 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
127 { DRM_MODE_SCALE_NONE, "None" },
128 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
129 { DRM_MODE_SCALE_CENTER, "Center" },
130 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
134 * Non-global properties, but "required" for certain connectors.
136 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
138 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
139 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
140 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
143 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
145 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
147 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
148 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
149 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
152 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
153 drm_dvi_i_subconnector_enum_list)
155 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
157 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
158 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
159 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
160 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
161 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
164 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
166 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
168 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
169 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
170 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
171 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
172 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
175 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
176 drm_tv_subconnector_enum_list)
178 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
179 { DRM_MODE_DIRTY_OFF, "Off" },
180 { DRM_MODE_DIRTY_ON, "On" },
181 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
184 struct drm_conn_prop_enum_list {
191 * Connector and encoder types.
193 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
194 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
195 { DRM_MODE_CONNECTOR_VGA, "VGA" },
196 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
197 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
198 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
199 { DRM_MODE_CONNECTOR_Composite, "Composite" },
200 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
201 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
202 { DRM_MODE_CONNECTOR_Component, "Component" },
203 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
204 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
205 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
206 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
207 { DRM_MODE_CONNECTOR_TV, "TV" },
208 { DRM_MODE_CONNECTOR_eDP, "eDP" },
209 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
210 { DRM_MODE_CONNECTOR_DSI, "DSI" },
213 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
214 { { DRM_MODE_ENCODER_NONE, "None" },
215 { DRM_MODE_ENCODER_DAC, "DAC" },
216 { DRM_MODE_ENCODER_TMDS, "TMDS" },
217 { DRM_MODE_ENCODER_LVDS, "LVDS" },
218 { DRM_MODE_ENCODER_TVDAC, "TV" },
219 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
220 { DRM_MODE_ENCODER_DSI, "DSI" },
223 void drm_connector_ida_init(void)
227 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
228 ida_init(&drm_connector_enum_list[i].ida);
231 void drm_connector_ida_destroy(void)
235 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
236 ida_destroy(&drm_connector_enum_list[i].ida);
240 * drm_get_encoder_name - return a string for encoder
241 * @encoder: encoder to compute name of
243 * Note that the buffer used by this function is globally shared and owned by
244 * the function itself.
246 * FIXME: This isn't really multithreading safe.
248 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
252 snprintf(buf, 32, "%s-%d",
253 drm_encoder_enum_list[encoder->encoder_type].name,
257 EXPORT_SYMBOL(drm_get_encoder_name);
260 * drm_get_connector_name - return a string for connector
261 * @connector: connector to compute name of
263 * Note that the buffer used by this function is globally shared and owned by
264 * the function itself.
266 * FIXME: This isn't really multithreading safe.
268 const char *drm_get_connector_name(const struct drm_connector *connector)
272 snprintf(buf, 32, "%s-%d",
273 drm_connector_enum_list[connector->connector_type].name,
274 connector->connector_type_id);
277 EXPORT_SYMBOL(drm_get_connector_name);
280 * drm_get_connector_status_name - return a string for connector status
281 * @status: connector status to compute name of
283 * In contrast to the other drm_get_*_name functions this one here returns a
284 * const pointer and hence is threadsafe.
286 const char *drm_get_connector_status_name(enum drm_connector_status status)
288 if (status == connector_status_connected)
290 else if (status == connector_status_disconnected)
291 return "disconnected";
295 EXPORT_SYMBOL(drm_get_connector_status_name);
297 static char printable_char(int c)
299 return isascii(c) && isprint(c) ? c : '?';
303 * drm_get_format_name - return a string for drm fourcc format
304 * @format: format to compute name of
306 * Note that the buffer used by this function is globally shared and owned by
307 * the function itself.
309 * FIXME: This isn't really multithreading safe.
311 const char *drm_get_format_name(uint32_t format)
315 snprintf(buf, sizeof(buf),
316 "%c%c%c%c %s-endian (0x%08x)",
317 printable_char(format & 0xff),
318 printable_char((format >> 8) & 0xff),
319 printable_char((format >> 16) & 0xff),
320 printable_char((format >> 24) & 0x7f),
321 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
326 EXPORT_SYMBOL(drm_get_format_name);
329 * drm_mode_object_get - allocate a new modeset identifier
331 * @obj: object pointer, used to generate unique ID
332 * @obj_type: object type
334 * Create a unique identifier based on @ptr in @dev's identifier space. Used
335 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
336 * modeset identifiers are _not_ reference counted. Hence don't use this for
337 * reference counted modeset objects like framebuffers.
340 * New unique (relative to other objects in @dev) integer identifier for the
343 static int drm_mode_object_get(struct drm_device *dev,
344 struct drm_mode_object *obj, uint32_t obj_type)
348 mutex_lock(&dev->mode_config.idr_mutex);
349 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
352 * Set up the object linking under the protection of the idr
353 * lock so that other users can't see inconsistent state.
356 obj->type = obj_type;
358 mutex_unlock(&dev->mode_config.idr_mutex);
360 return ret < 0 ? ret : 0;
364 * drm_mode_object_put - free a modeset identifer
366 * @object: object to free
368 * Free @id from @dev's unique identifier pool. Note that despite the _get
369 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
370 * for reference counted modeset objects like framebuffers.
372 static void drm_mode_object_put(struct drm_device *dev,
373 struct drm_mode_object *object)
375 mutex_lock(&dev->mode_config.idr_mutex);
376 idr_remove(&dev->mode_config.crtc_idr, object->id);
377 mutex_unlock(&dev->mode_config.idr_mutex);
381 * drm_mode_object_find - look up a drm object with static lifetime
383 * @id: id of the mode object
384 * @type: type of the mode object
386 * Note that framebuffers cannot be looked up with this functions - since those
387 * are reference counted, they need special treatment.
389 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
390 uint32_t id, uint32_t type)
392 struct drm_mode_object *obj = NULL;
394 /* Framebuffers are reference counted and need their own lookup
396 WARN_ON(type == DRM_MODE_OBJECT_FB);
398 mutex_lock(&dev->mode_config.idr_mutex);
399 obj = idr_find(&dev->mode_config.crtc_idr, id);
400 if (!obj || (obj->type != type) || (obj->id != id))
402 mutex_unlock(&dev->mode_config.idr_mutex);
406 EXPORT_SYMBOL(drm_mode_object_find);
409 * drm_framebuffer_init - initialize a framebuffer
411 * @fb: framebuffer to be initialized
412 * @funcs: ... with these functions
414 * Allocates an ID for the framebuffer's parent mode object, sets its mode
415 * functions & device file and adds it to the master fd list.
418 * This functions publishes the fb and makes it available for concurrent access
419 * by other users. Which means by this point the fb _must_ be fully set up -
420 * since all the fb attributes are invariant over its lifetime, no further
421 * locking but only correct reference counting is required.
424 * Zero on success, error code on failure.
426 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
427 const struct drm_framebuffer_funcs *funcs)
431 mutex_lock(&dev->mode_config.fb_lock);
432 kref_init(&fb->refcount);
433 INIT_LIST_HEAD(&fb->filp_head);
437 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
441 /* Grab the idr reference. */
442 drm_framebuffer_reference(fb);
444 dev->mode_config.num_fb++;
445 list_add(&fb->head, &dev->mode_config.fb_list);
447 mutex_unlock(&dev->mode_config.fb_lock);
451 EXPORT_SYMBOL(drm_framebuffer_init);
453 static void drm_framebuffer_free(struct kref *kref)
455 struct drm_framebuffer *fb =
456 container_of(kref, struct drm_framebuffer, refcount);
457 fb->funcs->destroy(fb);
460 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
463 struct drm_mode_object *obj = NULL;
464 struct drm_framebuffer *fb;
466 mutex_lock(&dev->mode_config.idr_mutex);
467 obj = idr_find(&dev->mode_config.crtc_idr, id);
468 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
472 mutex_unlock(&dev->mode_config.idr_mutex);
478 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
480 * @id: id of the fb object
482 * If successful, this grabs an additional reference to the framebuffer -
483 * callers need to make sure to eventually unreference the returned framebuffer
484 * again, using @drm_framebuffer_unreference.
486 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
489 struct drm_framebuffer *fb;
491 mutex_lock(&dev->mode_config.fb_lock);
492 fb = __drm_framebuffer_lookup(dev, id);
494 drm_framebuffer_reference(fb);
495 mutex_unlock(&dev->mode_config.fb_lock);
499 EXPORT_SYMBOL(drm_framebuffer_lookup);
502 * drm_framebuffer_unreference - unref a framebuffer
503 * @fb: framebuffer to unref
505 * This functions decrements the fb's refcount and frees it if it drops to zero.
507 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
509 DRM_DEBUG("FB ID: %d\n", fb->base.id);
510 kref_put(&fb->refcount, drm_framebuffer_free);
512 EXPORT_SYMBOL(drm_framebuffer_unreference);
515 * drm_framebuffer_reference - incr the fb refcnt
518 * This functions increments the fb's refcount.
520 void drm_framebuffer_reference(struct drm_framebuffer *fb)
522 DRM_DEBUG("FB ID: %d\n", fb->base.id);
523 kref_get(&fb->refcount);
525 EXPORT_SYMBOL(drm_framebuffer_reference);
527 static void drm_framebuffer_free_bug(struct kref *kref)
532 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
534 DRM_DEBUG("FB ID: %d\n", fb->base.id);
535 kref_put(&fb->refcount, drm_framebuffer_free_bug);
538 /* dev->mode_config.fb_lock must be held! */
539 static void __drm_framebuffer_unregister(struct drm_device *dev,
540 struct drm_framebuffer *fb)
542 mutex_lock(&dev->mode_config.idr_mutex);
543 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
544 mutex_unlock(&dev->mode_config.idr_mutex);
548 __drm_framebuffer_unreference(fb);
552 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
553 * @fb: fb to unregister
555 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
556 * those used for fbdev. Note that the caller must hold a reference of it's own,
557 * i.e. the object may not be destroyed through this call (since it'll lead to a
558 * locking inversion).
560 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
562 struct drm_device *dev = fb->dev;
564 mutex_lock(&dev->mode_config.fb_lock);
565 /* Mark fb as reaped and drop idr ref. */
566 __drm_framebuffer_unregister(dev, fb);
567 mutex_unlock(&dev->mode_config.fb_lock);
569 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
572 * drm_framebuffer_cleanup - remove a framebuffer object
573 * @fb: framebuffer to remove
575 * Cleanup framebuffer. This function is intended to be used from the drivers
576 * ->destroy callback. It can also be used to clean up driver private
577 * framebuffers embedded into a larger structure.
579 * Note that this function does not remove the fb from active usuage - if it is
580 * still used anywhere, hilarity can ensue since userspace could call getfb on
581 * the id and get back -EINVAL. Obviously no concern at driver unload time.
583 * Also, the framebuffer will not be removed from the lookup idr - for
584 * user-created framebuffers this will happen in in the rmfb ioctl. For
585 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
586 * drm_framebuffer_unregister_private.
588 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
590 struct drm_device *dev = fb->dev;
592 mutex_lock(&dev->mode_config.fb_lock);
594 dev->mode_config.num_fb--;
595 mutex_unlock(&dev->mode_config.fb_lock);
597 EXPORT_SYMBOL(drm_framebuffer_cleanup);
600 * drm_framebuffer_remove - remove and unreference a framebuffer object
601 * @fb: framebuffer to remove
603 * Scans all the CRTCs and planes in @dev's mode_config. If they're
604 * using @fb, removes it, setting it to NULL. Then drops the reference to the
605 * passed-in framebuffer. Might take the modeset locks.
607 * Note that this function optimizes the cleanup away if the caller holds the
608 * last reference to the framebuffer. It is also guaranteed to not take the
609 * modeset locks in this case.
611 void drm_framebuffer_remove(struct drm_framebuffer *fb)
613 struct drm_device *dev = fb->dev;
614 struct drm_crtc *crtc;
615 struct drm_plane *plane;
616 struct drm_mode_set set;
619 WARN_ON(!list_empty(&fb->filp_head));
622 * drm ABI mandates that we remove any deleted framebuffers from active
623 * useage. But since most sane clients only remove framebuffers they no
624 * longer need, try to optimize this away.
626 * Since we're holding a reference ourselves, observing a refcount of 1
627 * means that we're the last holder and can skip it. Also, the refcount
628 * can never increase from 1 again, so we don't need any barriers or
631 * Note that userspace could try to race with use and instate a new
632 * usage _after_ we've cleared all current ones. End result will be an
633 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
636 if (atomic_read(&fb->refcount.refcount) > 1) {
637 drm_modeset_lock_all(dev);
638 /* remove from any CRTC */
639 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
640 if (crtc->fb == fb) {
641 /* should turn off the crtc */
642 memset(&set, 0, sizeof(struct drm_mode_set));
645 ret = drm_mode_set_config_internal(&set);
647 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
651 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
653 drm_plane_force_disable(plane);
655 drm_modeset_unlock_all(dev);
658 drm_framebuffer_unreference(fb);
660 EXPORT_SYMBOL(drm_framebuffer_remove);
663 * drm_crtc_init - Initialise a new CRTC object
665 * @crtc: CRTC object to init
666 * @funcs: callbacks for the new CRTC
668 * Inits a new object created as base part of a driver crtc object.
671 * Zero on success, error code on failure.
673 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
674 const struct drm_crtc_funcs *funcs)
680 crtc->invert_dimensions = false;
682 drm_modeset_lock_all(dev);
683 mutex_init(&crtc->mutex);
684 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
686 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
690 crtc->base.properties = &crtc->properties;
692 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
693 dev->mode_config.num_crtc++;
696 drm_modeset_unlock_all(dev);
700 EXPORT_SYMBOL(drm_crtc_init);
703 * drm_crtc_cleanup - Clean up the core crtc usage
704 * @crtc: CRTC to cleanup
706 * This function cleans up @crtc and removes it from the DRM mode setting
707 * core. Note that the function does *not* free the crtc structure itself,
708 * this is the responsibility of the caller.
710 void drm_crtc_cleanup(struct drm_crtc *crtc)
712 struct drm_device *dev = crtc->dev;
714 kfree(crtc->gamma_store);
715 crtc->gamma_store = NULL;
717 drm_mode_object_put(dev, &crtc->base);
718 list_del(&crtc->head);
719 dev->mode_config.num_crtc--;
721 EXPORT_SYMBOL(drm_crtc_cleanup);
724 * drm_crtc_index - find the index of a registered CRTC
725 * @crtc: CRTC to find index for
727 * Given a registered CRTC, return the index of that CRTC within a DRM
728 * device's list of CRTCs.
730 unsigned int drm_crtc_index(struct drm_crtc *crtc)
732 unsigned int index = 0;
733 struct drm_crtc *tmp;
735 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
744 EXPORT_SYMBOL(drm_crtc_index);
747 * drm_mode_probed_add - add a mode to a connector's probed mode list
748 * @connector: connector the new mode
751 * Add @mode to @connector's mode list for later use.
753 void drm_mode_probed_add(struct drm_connector *connector,
754 struct drm_display_mode *mode)
756 list_add_tail(&mode->head, &connector->probed_modes);
758 EXPORT_SYMBOL(drm_mode_probed_add);
761 * drm_mode_remove - remove and free a mode
762 * @connector: connector list to modify
763 * @mode: mode to remove
765 * Remove @mode from @connector's mode list, then free it.
767 static void drm_mode_remove(struct drm_connector *connector,
768 struct drm_display_mode *mode)
770 list_del(&mode->head);
771 drm_mode_destroy(connector->dev, mode);
775 * drm_connector_init - Init a preallocated connector
777 * @connector: the connector to init
778 * @funcs: callbacks for this connector
779 * @connector_type: user visible type of the connector
781 * Initialises a preallocated connector. Connectors should be
782 * subclassed as part of driver connector objects.
785 * Zero on success, error code on failure.
787 int drm_connector_init(struct drm_device *dev,
788 struct drm_connector *connector,
789 const struct drm_connector_funcs *funcs,
793 struct ida *connector_ida =
794 &drm_connector_enum_list[connector_type].ida;
796 drm_modeset_lock_all(dev);
798 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
802 connector->base.properties = &connector->properties;
803 connector->dev = dev;
804 connector->funcs = funcs;
805 connector->connector_type = connector_type;
806 connector->connector_type_id =
807 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
808 if (connector->connector_type_id < 0) {
809 ret = connector->connector_type_id;
810 drm_mode_object_put(dev, &connector->base);
813 INIT_LIST_HEAD(&connector->probed_modes);
814 INIT_LIST_HEAD(&connector->modes);
815 connector->edid_blob_ptr = NULL;
816 connector->status = connector_status_unknown;
818 list_add_tail(&connector->head, &dev->mode_config.connector_list);
819 dev->mode_config.num_connector++;
821 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
822 drm_object_attach_property(&connector->base,
823 dev->mode_config.edid_property,
826 drm_object_attach_property(&connector->base,
827 dev->mode_config.dpms_property, 0);
830 drm_modeset_unlock_all(dev);
834 EXPORT_SYMBOL(drm_connector_init);
837 * drm_connector_cleanup - cleans up an initialised connector
838 * @connector: connector to cleanup
840 * Cleans up the connector but doesn't free the object.
842 void drm_connector_cleanup(struct drm_connector *connector)
844 struct drm_device *dev = connector->dev;
845 struct drm_display_mode *mode, *t;
847 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
848 drm_mode_remove(connector, mode);
850 list_for_each_entry_safe(mode, t, &connector->modes, head)
851 drm_mode_remove(connector, mode);
853 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
854 connector->connector_type_id);
856 drm_mode_object_put(dev, &connector->base);
857 list_del(&connector->head);
858 dev->mode_config.num_connector--;
860 EXPORT_SYMBOL(drm_connector_cleanup);
863 * drm_connector_unplug_all - unregister connector userspace interfaces
866 * This function unregisters all connector userspace interfaces in sysfs. Should
867 * be call when the device is disconnected, e.g. from an usb driver's
868 * ->disconnect callback.
870 void drm_connector_unplug_all(struct drm_device *dev)
872 struct drm_connector *connector;
874 /* taking the mode config mutex ends up in a clash with sysfs */
875 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
876 drm_sysfs_connector_remove(connector);
879 EXPORT_SYMBOL(drm_connector_unplug_all);
882 * drm_bridge_init - initialize a drm transcoder/bridge
884 * @bridge: transcoder/bridge to set up
885 * @funcs: bridge function table
887 * Initialises a preallocated bridge. Bridges should be
888 * subclassed as part of driver connector objects.
891 * Zero on success, error code on failure.
893 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
894 const struct drm_bridge_funcs *funcs)
898 drm_modeset_lock_all(dev);
900 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
905 bridge->funcs = funcs;
907 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
908 dev->mode_config.num_bridge++;
911 drm_modeset_unlock_all(dev);
914 EXPORT_SYMBOL(drm_bridge_init);
917 * drm_bridge_cleanup - cleans up an initialised bridge
918 * @bridge: bridge to cleanup
920 * Cleans up the bridge but doesn't free the object.
922 void drm_bridge_cleanup(struct drm_bridge *bridge)
924 struct drm_device *dev = bridge->dev;
926 drm_modeset_lock_all(dev);
927 drm_mode_object_put(dev, &bridge->base);
928 list_del(&bridge->head);
929 dev->mode_config.num_bridge--;
930 drm_modeset_unlock_all(dev);
932 EXPORT_SYMBOL(drm_bridge_cleanup);
935 * drm_encoder_init - Init a preallocated encoder
937 * @encoder: the encoder to init
938 * @funcs: callbacks for this encoder
939 * @encoder_type: user visible type of the encoder
941 * Initialises a preallocated encoder. Encoder should be
942 * subclassed as part of driver encoder objects.
945 * Zero on success, error code on failure.
947 int drm_encoder_init(struct drm_device *dev,
948 struct drm_encoder *encoder,
949 const struct drm_encoder_funcs *funcs,
954 drm_modeset_lock_all(dev);
956 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
961 encoder->encoder_type = encoder_type;
962 encoder->funcs = funcs;
964 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
965 dev->mode_config.num_encoder++;
968 drm_modeset_unlock_all(dev);
972 EXPORT_SYMBOL(drm_encoder_init);
975 * drm_encoder_cleanup - cleans up an initialised encoder
976 * @encoder: encoder to cleanup
978 * Cleans up the encoder but doesn't free the object.
980 void drm_encoder_cleanup(struct drm_encoder *encoder)
982 struct drm_device *dev = encoder->dev;
983 drm_modeset_lock_all(dev);
984 drm_mode_object_put(dev, &encoder->base);
985 list_del(&encoder->head);
986 dev->mode_config.num_encoder--;
987 drm_modeset_unlock_all(dev);
989 EXPORT_SYMBOL(drm_encoder_cleanup);
992 * drm_plane_init - Initialise a new plane object
994 * @plane: plane object to init
995 * @possible_crtcs: bitmask of possible CRTCs
996 * @funcs: callbacks for the new plane
997 * @formats: array of supported formats (%DRM_FORMAT_*)
998 * @format_count: number of elements in @formats
999 * @priv: plane is private (hidden from userspace)?
1001 * Inits a preallocate plane object created as base part of a driver plane
1005 * Zero on success, error code on failure.
1007 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1008 unsigned long possible_crtcs,
1009 const struct drm_plane_funcs *funcs,
1010 const uint32_t *formats, uint32_t format_count,
1015 drm_modeset_lock_all(dev);
1017 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1021 plane->base.properties = &plane->properties;
1023 plane->funcs = funcs;
1024 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1026 if (!plane->format_types) {
1027 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1028 drm_mode_object_put(dev, &plane->base);
1033 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1034 plane->format_count = format_count;
1035 plane->possible_crtcs = possible_crtcs;
1037 /* private planes are not exposed to userspace, but depending on
1038 * display hardware, might be convenient to allow sharing programming
1039 * for the scanout engine with the crtc implementation.
1042 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1043 dev->mode_config.num_plane++;
1045 INIT_LIST_HEAD(&plane->head);
1049 drm_modeset_unlock_all(dev);
1053 EXPORT_SYMBOL(drm_plane_init);
1056 * drm_plane_cleanup - Clean up the core plane usage
1057 * @plane: plane to cleanup
1059 * This function cleans up @plane and removes it from the DRM mode setting
1060 * core. Note that the function does *not* free the plane structure itself,
1061 * this is the responsibility of the caller.
1063 void drm_plane_cleanup(struct drm_plane *plane)
1065 struct drm_device *dev = plane->dev;
1067 drm_modeset_lock_all(dev);
1068 kfree(plane->format_types);
1069 drm_mode_object_put(dev, &plane->base);
1070 /* if not added to a list, it must be a private plane */
1071 if (!list_empty(&plane->head)) {
1072 list_del(&plane->head);
1073 dev->mode_config.num_plane--;
1075 drm_modeset_unlock_all(dev);
1077 EXPORT_SYMBOL(drm_plane_cleanup);
1080 * drm_plane_force_disable - Forcibly disable a plane
1081 * @plane: plane to disable
1083 * Forces the plane to be disabled.
1085 * Used when the plane's current framebuffer is destroyed,
1086 * and when restoring fbdev mode.
1088 void drm_plane_force_disable(struct drm_plane *plane)
1095 ret = plane->funcs->disable_plane(plane);
1097 DRM_ERROR("failed to disable plane with busy fb\n");
1098 /* disconnect the plane from the fb and crtc: */
1099 __drm_framebuffer_unreference(plane->fb);
1103 EXPORT_SYMBOL(drm_plane_force_disable);
1106 * drm_mode_create - create a new display mode
1109 * Create a new drm_display_mode, give it an ID, and return it.
1112 * Pointer to new mode on success, NULL on error.
1114 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1116 struct drm_display_mode *nmode;
1118 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1122 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1129 EXPORT_SYMBOL(drm_mode_create);
1132 * drm_mode_destroy - remove a mode
1134 * @mode: mode to remove
1136 * Free @mode's unique identifier, then free it.
1138 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1143 drm_mode_object_put(dev, &mode->base);
1147 EXPORT_SYMBOL(drm_mode_destroy);
1149 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1151 struct drm_property *edid;
1152 struct drm_property *dpms;
1155 * Standard properties (apply to all connectors)
1157 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1158 DRM_MODE_PROP_IMMUTABLE,
1160 dev->mode_config.edid_property = edid;
1162 dpms = drm_property_create_enum(dev, 0,
1163 "DPMS", drm_dpms_enum_list,
1164 ARRAY_SIZE(drm_dpms_enum_list));
1165 dev->mode_config.dpms_property = dpms;
1171 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1174 * Called by a driver the first time a DVI-I connector is made.
1176 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1178 struct drm_property *dvi_i_selector;
1179 struct drm_property *dvi_i_subconnector;
1181 if (dev->mode_config.dvi_i_select_subconnector_property)
1185 drm_property_create_enum(dev, 0,
1186 "select subconnector",
1187 drm_dvi_i_select_enum_list,
1188 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1189 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1191 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1193 drm_dvi_i_subconnector_enum_list,
1194 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1195 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1199 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1202 * drm_create_tv_properties - create TV specific connector properties
1204 * @num_modes: number of different TV formats (modes) supported
1205 * @modes: array of pointers to strings containing name of each format
1207 * Called by a driver's TV initialization routine, this function creates
1208 * the TV specific connector properties for a given device. Caller is
1209 * responsible for allocating a list of format names and passing them to
1212 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1215 struct drm_property *tv_selector;
1216 struct drm_property *tv_subconnector;
1219 if (dev->mode_config.tv_select_subconnector_property)
1223 * Basic connector properties
1225 tv_selector = drm_property_create_enum(dev, 0,
1226 "select subconnector",
1227 drm_tv_select_enum_list,
1228 ARRAY_SIZE(drm_tv_select_enum_list));
1229 dev->mode_config.tv_select_subconnector_property = tv_selector;
1232 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1234 drm_tv_subconnector_enum_list,
1235 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1236 dev->mode_config.tv_subconnector_property = tv_subconnector;
1239 * Other, TV specific properties: margins & TV modes.
1241 dev->mode_config.tv_left_margin_property =
1242 drm_property_create_range(dev, 0, "left margin", 0, 100);
1244 dev->mode_config.tv_right_margin_property =
1245 drm_property_create_range(dev, 0, "right margin", 0, 100);
1247 dev->mode_config.tv_top_margin_property =
1248 drm_property_create_range(dev, 0, "top margin", 0, 100);
1250 dev->mode_config.tv_bottom_margin_property =
1251 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1253 dev->mode_config.tv_mode_property =
1254 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1256 for (i = 0; i < num_modes; i++)
1257 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1260 dev->mode_config.tv_brightness_property =
1261 drm_property_create_range(dev, 0, "brightness", 0, 100);
1263 dev->mode_config.tv_contrast_property =
1264 drm_property_create_range(dev, 0, "contrast", 0, 100);
1266 dev->mode_config.tv_flicker_reduction_property =
1267 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1269 dev->mode_config.tv_overscan_property =
1270 drm_property_create_range(dev, 0, "overscan", 0, 100);
1272 dev->mode_config.tv_saturation_property =
1273 drm_property_create_range(dev, 0, "saturation", 0, 100);
1275 dev->mode_config.tv_hue_property =
1276 drm_property_create_range(dev, 0, "hue", 0, 100);
1280 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1283 * drm_mode_create_scaling_mode_property - create scaling mode property
1286 * Called by a driver the first time it's needed, must be attached to desired
1289 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1291 struct drm_property *scaling_mode;
1293 if (dev->mode_config.scaling_mode_property)
1297 drm_property_create_enum(dev, 0, "scaling mode",
1298 drm_scaling_mode_enum_list,
1299 ARRAY_SIZE(drm_scaling_mode_enum_list));
1301 dev->mode_config.scaling_mode_property = scaling_mode;
1305 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1308 * drm_mode_create_dirty_property - create dirty property
1311 * Called by a driver the first time it's needed, must be attached to desired
1314 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1316 struct drm_property *dirty_info;
1318 if (dev->mode_config.dirty_info_property)
1322 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1324 drm_dirty_info_enum_list,
1325 ARRAY_SIZE(drm_dirty_info_enum_list));
1326 dev->mode_config.dirty_info_property = dirty_info;
1330 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1332 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1334 uint32_t total_objects = 0;
1336 total_objects += dev->mode_config.num_crtc;
1337 total_objects += dev->mode_config.num_connector;
1338 total_objects += dev->mode_config.num_encoder;
1339 total_objects += dev->mode_config.num_bridge;
1341 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1342 if (!group->id_list)
1345 group->num_crtcs = 0;
1346 group->num_connectors = 0;
1347 group->num_encoders = 0;
1348 group->num_bridges = 0;
1353 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1354 * the drm core's responsibility to set up mode control groups.
1356 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1357 struct drm_mode_group *group)
1359 struct drm_crtc *crtc;
1360 struct drm_encoder *encoder;
1361 struct drm_connector *connector;
1362 struct drm_bridge *bridge;
1365 if ((ret = drm_mode_group_init(dev, group)))
1368 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1369 group->id_list[group->num_crtcs++] = crtc->base.id;
1371 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1372 group->id_list[group->num_crtcs + group->num_encoders++] =
1375 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1376 group->id_list[group->num_crtcs + group->num_encoders +
1377 group->num_connectors++] = connector->base.id;
1379 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1380 group->id_list[group->num_crtcs + group->num_encoders +
1381 group->num_connectors + group->num_bridges++] =
1386 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1389 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1390 * @out: drm_mode_modeinfo struct to return to the user
1391 * @in: drm_display_mode to use
1393 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1396 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1397 const struct drm_display_mode *in)
1399 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1400 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1401 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1402 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1403 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1404 "timing values too large for mode info\n");
1406 out->clock = in->clock;
1407 out->hdisplay = in->hdisplay;
1408 out->hsync_start = in->hsync_start;
1409 out->hsync_end = in->hsync_end;
1410 out->htotal = in->htotal;
1411 out->hskew = in->hskew;
1412 out->vdisplay = in->vdisplay;
1413 out->vsync_start = in->vsync_start;
1414 out->vsync_end = in->vsync_end;
1415 out->vtotal = in->vtotal;
1416 out->vscan = in->vscan;
1417 out->vrefresh = in->vrefresh;
1418 out->flags = in->flags;
1419 out->type = in->type;
1420 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1421 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1425 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1426 * @out: drm_display_mode to return to the user
1427 * @in: drm_mode_modeinfo to use
1429 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1433 * Zero on success, errno on failure.
1435 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1436 const struct drm_mode_modeinfo *in)
1438 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1441 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1444 out->clock = in->clock;
1445 out->hdisplay = in->hdisplay;
1446 out->hsync_start = in->hsync_start;
1447 out->hsync_end = in->hsync_end;
1448 out->htotal = in->htotal;
1449 out->hskew = in->hskew;
1450 out->vdisplay = in->vdisplay;
1451 out->vsync_start = in->vsync_start;
1452 out->vsync_end = in->vsync_end;
1453 out->vtotal = in->vtotal;
1454 out->vscan = in->vscan;
1455 out->vrefresh = in->vrefresh;
1456 out->flags = in->flags;
1457 out->type = in->type;
1458 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1459 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1465 * drm_mode_getresources - get graphics configuration
1466 * @dev: drm device for the ioctl
1467 * @data: data pointer for the ioctl
1468 * @file_priv: drm file for the ioctl call
1470 * Construct a set of configuration description structures and return
1471 * them to the user, including CRTC, connector and framebuffer configuration.
1473 * Called by the user via ioctl.
1476 * Zero on success, errno on failure.
1478 int drm_mode_getresources(struct drm_device *dev, void *data,
1479 struct drm_file *file_priv)
1481 struct drm_mode_card_res *card_res = data;
1482 struct list_head *lh;
1483 struct drm_framebuffer *fb;
1484 struct drm_connector *connector;
1485 struct drm_crtc *crtc;
1486 struct drm_encoder *encoder;
1488 int connector_count = 0;
1491 int encoder_count = 0;
1493 uint32_t __user *fb_id;
1494 uint32_t __user *crtc_id;
1495 uint32_t __user *connector_id;
1496 uint32_t __user *encoder_id;
1497 struct drm_mode_group *mode_group;
1499 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1503 mutex_lock(&file_priv->fbs_lock);
1505 * For the non-control nodes we need to limit the list of resources
1506 * by IDs in the group list for this node
1508 list_for_each(lh, &file_priv->fbs)
1511 /* handle this in 4 parts */
1513 if (card_res->count_fbs >= fb_count) {
1515 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1516 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1517 if (put_user(fb->base.id, fb_id + copied)) {
1518 mutex_unlock(&file_priv->fbs_lock);
1524 card_res->count_fbs = fb_count;
1525 mutex_unlock(&file_priv->fbs_lock);
1527 drm_modeset_lock_all(dev);
1528 if (!drm_is_primary_client(file_priv)) {
1531 list_for_each(lh, &dev->mode_config.crtc_list)
1534 list_for_each(lh, &dev->mode_config.connector_list)
1537 list_for_each(lh, &dev->mode_config.encoder_list)
1541 mode_group = &file_priv->master->minor->mode_group;
1542 crtc_count = mode_group->num_crtcs;
1543 connector_count = mode_group->num_connectors;
1544 encoder_count = mode_group->num_encoders;
1547 card_res->max_height = dev->mode_config.max_height;
1548 card_res->min_height = dev->mode_config.min_height;
1549 card_res->max_width = dev->mode_config.max_width;
1550 card_res->min_width = dev->mode_config.min_width;
1553 if (card_res->count_crtcs >= crtc_count) {
1555 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1557 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1559 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1560 if (put_user(crtc->base.id, crtc_id + copied)) {
1567 for (i = 0; i < mode_group->num_crtcs; i++) {
1568 if (put_user(mode_group->id_list[i],
1569 crtc_id + copied)) {
1577 card_res->count_crtcs = crtc_count;
1580 if (card_res->count_encoders >= encoder_count) {
1582 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1584 list_for_each_entry(encoder,
1585 &dev->mode_config.encoder_list,
1587 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1588 drm_get_encoder_name(encoder));
1589 if (put_user(encoder->base.id, encoder_id +
1597 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1598 if (put_user(mode_group->id_list[i],
1599 encoder_id + copied)) {
1608 card_res->count_encoders = encoder_count;
1611 if (card_res->count_connectors >= connector_count) {
1613 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1615 list_for_each_entry(connector,
1616 &dev->mode_config.connector_list,
1618 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1620 drm_get_connector_name(connector));
1621 if (put_user(connector->base.id,
1622 connector_id + copied)) {
1629 int start = mode_group->num_crtcs +
1630 mode_group->num_encoders;
1631 for (i = start; i < start + mode_group->num_connectors; i++) {
1632 if (put_user(mode_group->id_list[i],
1633 connector_id + copied)) {
1641 card_res->count_connectors = connector_count;
1643 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1644 card_res->count_connectors, card_res->count_encoders);
1647 drm_modeset_unlock_all(dev);
1652 * drm_mode_getcrtc - get CRTC configuration
1653 * @dev: drm device for the ioctl
1654 * @data: data pointer for the ioctl
1655 * @file_priv: drm file for the ioctl call
1657 * Construct a CRTC configuration structure to return to the user.
1659 * Called by the user via ioctl.
1662 * Zero on success, errno on failure.
1664 int drm_mode_getcrtc(struct drm_device *dev,
1665 void *data, struct drm_file *file_priv)
1667 struct drm_mode_crtc *crtc_resp = data;
1668 struct drm_crtc *crtc;
1669 struct drm_mode_object *obj;
1672 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1675 drm_modeset_lock_all(dev);
1677 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1678 DRM_MODE_OBJECT_CRTC);
1683 crtc = obj_to_crtc(obj);
1685 crtc_resp->x = crtc->x;
1686 crtc_resp->y = crtc->y;
1687 crtc_resp->gamma_size = crtc->gamma_size;
1689 crtc_resp->fb_id = crtc->fb->base.id;
1691 crtc_resp->fb_id = 0;
1693 if (crtc->enabled) {
1695 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1696 crtc_resp->mode_valid = 1;
1699 crtc_resp->mode_valid = 0;
1703 drm_modeset_unlock_all(dev);
1707 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1708 const struct drm_file *file_priv)
1711 * If user-space hasn't configured the driver to expose the stereo 3D
1712 * modes, don't expose them.
1714 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1721 * drm_mode_getconnector - get connector configuration
1722 * @dev: drm device for the ioctl
1723 * @data: data pointer for the ioctl
1724 * @file_priv: drm file for the ioctl call
1726 * Construct a connector configuration structure to return to the user.
1728 * Called by the user via ioctl.
1731 * Zero on success, errno on failure.
1733 int drm_mode_getconnector(struct drm_device *dev, void *data,
1734 struct drm_file *file_priv)
1736 struct drm_mode_get_connector *out_resp = data;
1737 struct drm_mode_object *obj;
1738 struct drm_connector *connector;
1739 struct drm_display_mode *mode;
1741 int props_count = 0;
1742 int encoders_count = 0;
1746 struct drm_mode_modeinfo u_mode;
1747 struct drm_mode_modeinfo __user *mode_ptr;
1748 uint32_t __user *prop_ptr;
1749 uint64_t __user *prop_values;
1750 uint32_t __user *encoder_ptr;
1752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1755 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1757 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1759 mutex_lock(&dev->mode_config.mutex);
1761 obj = drm_mode_object_find(dev, out_resp->connector_id,
1762 DRM_MODE_OBJECT_CONNECTOR);
1767 connector = obj_to_connector(obj);
1769 props_count = connector->properties.count;
1771 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1772 if (connector->encoder_ids[i] != 0) {
1777 if (out_resp->count_modes == 0) {
1778 connector->funcs->fill_modes(connector,
1779 dev->mode_config.max_width,
1780 dev->mode_config.max_height);
1783 /* delayed so we get modes regardless of pre-fill_modes state */
1784 list_for_each_entry(mode, &connector->modes, head)
1785 if (drm_mode_expose_to_userspace(mode, file_priv))
1788 out_resp->connector_id = connector->base.id;
1789 out_resp->connector_type = connector->connector_type;
1790 out_resp->connector_type_id = connector->connector_type_id;
1791 out_resp->mm_width = connector->display_info.width_mm;
1792 out_resp->mm_height = connector->display_info.height_mm;
1793 out_resp->subpixel = connector->display_info.subpixel_order;
1794 out_resp->connection = connector->status;
1795 if (connector->encoder)
1796 out_resp->encoder_id = connector->encoder->base.id;
1798 out_resp->encoder_id = 0;
1801 * This ioctl is called twice, once to determine how much space is
1802 * needed, and the 2nd time to fill it.
1804 if ((out_resp->count_modes >= mode_count) && mode_count) {
1806 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1807 list_for_each_entry(mode, &connector->modes, head) {
1808 if (!drm_mode_expose_to_userspace(mode, file_priv))
1811 drm_crtc_convert_to_umode(&u_mode, mode);
1812 if (copy_to_user(mode_ptr + copied,
1813 &u_mode, sizeof(u_mode))) {
1820 out_resp->count_modes = mode_count;
1822 if ((out_resp->count_props >= props_count) && props_count) {
1824 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1825 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1826 for (i = 0; i < connector->properties.count; i++) {
1827 if (put_user(connector->properties.ids[i],
1828 prop_ptr + copied)) {
1833 if (put_user(connector->properties.values[i],
1834 prop_values + copied)) {
1841 out_resp->count_props = props_count;
1843 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1845 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1846 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1847 if (connector->encoder_ids[i] != 0) {
1848 if (put_user(connector->encoder_ids[i],
1849 encoder_ptr + copied)) {
1857 out_resp->count_encoders = encoders_count;
1860 mutex_unlock(&dev->mode_config.mutex);
1866 * drm_mode_getencoder - get encoder configuration
1867 * @dev: drm device for the ioctl
1868 * @data: data pointer for the ioctl
1869 * @file_priv: drm file for the ioctl call
1871 * Construct a encoder configuration structure to return to the user.
1873 * Called by the user via ioctl.
1876 * Zero on success, errno on failure.
1878 int drm_mode_getencoder(struct drm_device *dev, void *data,
1879 struct drm_file *file_priv)
1881 struct drm_mode_get_encoder *enc_resp = data;
1882 struct drm_mode_object *obj;
1883 struct drm_encoder *encoder;
1886 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1889 drm_modeset_lock_all(dev);
1890 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1891 DRM_MODE_OBJECT_ENCODER);
1896 encoder = obj_to_encoder(obj);
1899 enc_resp->crtc_id = encoder->crtc->base.id;
1901 enc_resp->crtc_id = 0;
1902 enc_resp->encoder_type = encoder->encoder_type;
1903 enc_resp->encoder_id = encoder->base.id;
1904 enc_resp->possible_crtcs = encoder->possible_crtcs;
1905 enc_resp->possible_clones = encoder->possible_clones;
1908 drm_modeset_unlock_all(dev);
1913 * drm_mode_getplane_res - enumerate all plane resources
1916 * @file_priv: DRM file info
1918 * Construct a list of plane ids to return to the user.
1920 * Called by the user via ioctl.
1923 * Zero on success, errno on failure.
1925 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1926 struct drm_file *file_priv)
1928 struct drm_mode_get_plane_res *plane_resp = data;
1929 struct drm_mode_config *config;
1930 struct drm_plane *plane;
1931 uint32_t __user *plane_ptr;
1932 int copied = 0, ret = 0;
1934 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1937 drm_modeset_lock_all(dev);
1938 config = &dev->mode_config;
1941 * This ioctl is called twice, once to determine how much space is
1942 * needed, and the 2nd time to fill it.
1944 if (config->num_plane &&
1945 (plane_resp->count_planes >= config->num_plane)) {
1946 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1948 list_for_each_entry(plane, &config->plane_list, head) {
1949 if (put_user(plane->base.id, plane_ptr + copied)) {
1956 plane_resp->count_planes = config->num_plane;
1959 drm_modeset_unlock_all(dev);
1964 * drm_mode_getplane - get plane configuration
1967 * @file_priv: DRM file info
1969 * Construct a plane configuration structure to return to the user.
1971 * Called by the user via ioctl.
1974 * Zero on success, errno on failure.
1976 int drm_mode_getplane(struct drm_device *dev, void *data,
1977 struct drm_file *file_priv)
1979 struct drm_mode_get_plane *plane_resp = data;
1980 struct drm_mode_object *obj;
1981 struct drm_plane *plane;
1982 uint32_t __user *format_ptr;
1985 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1988 drm_modeset_lock_all(dev);
1989 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1990 DRM_MODE_OBJECT_PLANE);
1995 plane = obj_to_plane(obj);
1998 plane_resp->crtc_id = plane->crtc->base.id;
2000 plane_resp->crtc_id = 0;
2003 plane_resp->fb_id = plane->fb->base.id;
2005 plane_resp->fb_id = 0;
2007 plane_resp->plane_id = plane->base.id;
2008 plane_resp->possible_crtcs = plane->possible_crtcs;
2009 plane_resp->gamma_size = 0;
2012 * This ioctl is called twice, once to determine how much space is
2013 * needed, and the 2nd time to fill it.
2015 if (plane->format_count &&
2016 (plane_resp->count_format_types >= plane->format_count)) {
2017 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2018 if (copy_to_user(format_ptr,
2019 plane->format_types,
2020 sizeof(uint32_t) * plane->format_count)) {
2025 plane_resp->count_format_types = plane->format_count;
2028 drm_modeset_unlock_all(dev);
2033 * drm_mode_setplane - configure a plane's configuration
2035 * @data: ioctl data*
2036 * @file_priv: DRM file info
2038 * Set plane configuration, including placement, fb, scaling, and other factors.
2039 * Or pass a NULL fb to disable.
2042 * Zero on success, errno on failure.
2044 int drm_mode_setplane(struct drm_device *dev, void *data,
2045 struct drm_file *file_priv)
2047 struct drm_mode_set_plane *plane_req = data;
2048 struct drm_mode_object *obj;
2049 struct drm_plane *plane;
2050 struct drm_crtc *crtc;
2051 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2053 unsigned int fb_width, fb_height;
2056 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2060 * First, find the plane, crtc, and fb objects. If not available,
2061 * we don't bother to call the driver.
2063 obj = drm_mode_object_find(dev, plane_req->plane_id,
2064 DRM_MODE_OBJECT_PLANE);
2066 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2067 plane_req->plane_id);
2070 plane = obj_to_plane(obj);
2072 /* No fb means shut it down */
2073 if (!plane_req->fb_id) {
2074 drm_modeset_lock_all(dev);
2076 plane->funcs->disable_plane(plane);
2079 drm_modeset_unlock_all(dev);
2083 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2084 DRM_MODE_OBJECT_CRTC);
2086 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2087 plane_req->crtc_id);
2091 crtc = obj_to_crtc(obj);
2093 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2095 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2101 /* Check whether this plane supports the fb pixel format. */
2102 for (i = 0; i < plane->format_count; i++)
2103 if (fb->pixel_format == plane->format_types[i])
2105 if (i == plane->format_count) {
2106 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2107 drm_get_format_name(fb->pixel_format));
2112 fb_width = fb->width << 16;
2113 fb_height = fb->height << 16;
2115 /* Make sure source coordinates are inside the fb. */
2116 if (plane_req->src_w > fb_width ||
2117 plane_req->src_x > fb_width - plane_req->src_w ||
2118 plane_req->src_h > fb_height ||
2119 plane_req->src_y > fb_height - plane_req->src_h) {
2120 DRM_DEBUG_KMS("Invalid source coordinates "
2121 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2122 plane_req->src_w >> 16,
2123 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2124 plane_req->src_h >> 16,
2125 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2126 plane_req->src_x >> 16,
2127 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2128 plane_req->src_y >> 16,
2129 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2134 /* Give drivers some help against integer overflows */
2135 if (plane_req->crtc_w > INT_MAX ||
2136 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2137 plane_req->crtc_h > INT_MAX ||
2138 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2139 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2140 plane_req->crtc_w, plane_req->crtc_h,
2141 plane_req->crtc_x, plane_req->crtc_y);
2146 drm_modeset_lock_all(dev);
2147 ret = plane->funcs->update_plane(plane, crtc, fb,
2148 plane_req->crtc_x, plane_req->crtc_y,
2149 plane_req->crtc_w, plane_req->crtc_h,
2150 plane_req->src_x, plane_req->src_y,
2151 plane_req->src_w, plane_req->src_h);
2158 drm_modeset_unlock_all(dev);
2162 drm_framebuffer_unreference(fb);
2164 drm_framebuffer_unreference(old_fb);
2170 * drm_mode_set_config_internal - helper to call ->set_config
2171 * @set: modeset config to set
2173 * This is a little helper to wrap internal calls to the ->set_config driver
2174 * interface. The only thing it adds is correct refcounting dance.
2177 * Zero on success, errno on failure.
2179 int drm_mode_set_config_internal(struct drm_mode_set *set)
2181 struct drm_crtc *crtc = set->crtc;
2182 struct drm_framebuffer *fb;
2183 struct drm_crtc *tmp;
2187 * NOTE: ->set_config can also disable other crtcs (if we steal all
2188 * connectors from it), hence we need to refcount the fbs across all
2189 * crtcs. Atomic modeset will have saner semantics ...
2191 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2192 tmp->old_fb = tmp->fb;
2196 ret = crtc->funcs->set_config(set);
2198 /* crtc->fb must be updated by ->set_config, enforces this. */
2199 WARN_ON(fb != crtc->fb);
2202 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2204 drm_framebuffer_reference(tmp->fb);
2206 drm_framebuffer_unreference(tmp->old_fb);
2211 EXPORT_SYMBOL(drm_mode_set_config_internal);
2214 * Checks that the framebuffer is big enough for the CRTC viewport
2215 * (x, y, hdisplay, vdisplay)
2217 static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2219 const struct drm_display_mode *mode,
2220 const struct drm_framebuffer *fb)
2223 int hdisplay, vdisplay;
2225 hdisplay = mode->hdisplay;
2226 vdisplay = mode->vdisplay;
2228 if (drm_mode_is_stereo(mode)) {
2229 struct drm_display_mode adjusted = *mode;
2231 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2232 hdisplay = adjusted.crtc_hdisplay;
2233 vdisplay = adjusted.crtc_vdisplay;
2236 if (crtc->invert_dimensions)
2237 swap(hdisplay, vdisplay);
2239 if (hdisplay > fb->width ||
2240 vdisplay > fb->height ||
2241 x > fb->width - hdisplay ||
2242 y > fb->height - vdisplay) {
2243 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2244 fb->width, fb->height, hdisplay, vdisplay, x, y,
2245 crtc->invert_dimensions ? " (inverted)" : "");
2253 * drm_mode_setcrtc - set CRTC configuration
2254 * @dev: drm device for the ioctl
2255 * @data: data pointer for the ioctl
2256 * @file_priv: drm file for the ioctl call
2258 * Build a new CRTC configuration based on user request.
2260 * Called by the user via ioctl.
2263 * Zero on success, errno on failure.
2265 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2266 struct drm_file *file_priv)
2268 struct drm_mode_config *config = &dev->mode_config;
2269 struct drm_mode_crtc *crtc_req = data;
2270 struct drm_mode_object *obj;
2271 struct drm_crtc *crtc;
2272 struct drm_connector **connector_set = NULL, *connector;
2273 struct drm_framebuffer *fb = NULL;
2274 struct drm_display_mode *mode = NULL;
2275 struct drm_mode_set set;
2276 uint32_t __user *set_connectors_ptr;
2280 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2283 /* For some reason crtc x/y offsets are signed internally. */
2284 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2287 drm_modeset_lock_all(dev);
2288 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2289 DRM_MODE_OBJECT_CRTC);
2291 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2295 crtc = obj_to_crtc(obj);
2296 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2298 if (crtc_req->mode_valid) {
2299 /* If we have a mode we need a framebuffer. */
2300 /* If we pass -1, set the mode with the currently bound fb */
2301 if (crtc_req->fb_id == -1) {
2303 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2308 /* Make refcounting symmetric with the lookup path. */
2309 drm_framebuffer_reference(fb);
2311 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2313 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2320 mode = drm_mode_create(dev);
2326 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2328 DRM_DEBUG_KMS("Invalid mode\n");
2332 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2334 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2341 if (crtc_req->count_connectors == 0 && mode) {
2342 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2347 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2348 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2349 crtc_req->count_connectors);
2354 if (crtc_req->count_connectors > 0) {
2357 /* Avoid unbounded kernel memory allocation */
2358 if (crtc_req->count_connectors > config->num_connector) {
2363 connector_set = kmalloc(crtc_req->count_connectors *
2364 sizeof(struct drm_connector *),
2366 if (!connector_set) {
2371 for (i = 0; i < crtc_req->count_connectors; i++) {
2372 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2373 if (get_user(out_id, &set_connectors_ptr[i])) {
2378 obj = drm_mode_object_find(dev, out_id,
2379 DRM_MODE_OBJECT_CONNECTOR);
2381 DRM_DEBUG_KMS("Connector id %d unknown\n",
2386 connector = obj_to_connector(obj);
2387 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2389 drm_get_connector_name(connector));
2391 connector_set[i] = connector;
2396 set.x = crtc_req->x;
2397 set.y = crtc_req->y;
2399 set.connectors = connector_set;
2400 set.num_connectors = crtc_req->count_connectors;
2402 ret = drm_mode_set_config_internal(&set);
2406 drm_framebuffer_unreference(fb);
2408 kfree(connector_set);
2409 drm_mode_destroy(dev, mode);
2410 drm_modeset_unlock_all(dev);
2414 static int drm_mode_cursor_common(struct drm_device *dev,
2415 struct drm_mode_cursor2 *req,
2416 struct drm_file *file_priv)
2418 struct drm_mode_object *obj;
2419 struct drm_crtc *crtc;
2422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2425 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2428 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2430 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2433 crtc = obj_to_crtc(obj);
2435 mutex_lock(&crtc->mutex);
2436 if (req->flags & DRM_MODE_CURSOR_BO) {
2437 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2441 /* Turns off the cursor if handle is 0 */
2442 if (crtc->funcs->cursor_set2)
2443 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2444 req->width, req->height, req->hot_x, req->hot_y);
2446 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2447 req->width, req->height);
2450 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2451 if (crtc->funcs->cursor_move) {
2452 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2459 mutex_unlock(&crtc->mutex);
2467 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2468 * @dev: drm device for the ioctl
2469 * @data: data pointer for the ioctl
2470 * @file_priv: drm file for the ioctl call
2472 * Set the cursor configuration based on user request.
2474 * Called by the user via ioctl.
2477 * Zero on success, errno on failure.
2479 int drm_mode_cursor_ioctl(struct drm_device *dev,
2480 void *data, struct drm_file *file_priv)
2482 struct drm_mode_cursor *req = data;
2483 struct drm_mode_cursor2 new_req;
2485 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2486 new_req.hot_x = new_req.hot_y = 0;
2488 return drm_mode_cursor_common(dev, &new_req, file_priv);
2492 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2493 * @dev: drm device for the ioctl
2494 * @data: data pointer for the ioctl
2495 * @file_priv: drm file for the ioctl call
2497 * Set the cursor configuration based on user request. This implements the 2nd
2498 * version of the cursor ioctl, which allows userspace to additionally specify
2499 * the hotspot of the pointer.
2501 * Called by the user via ioctl.
2504 * Zero on success, errno on failure.
2506 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2507 void *data, struct drm_file *file_priv)
2509 struct drm_mode_cursor2 *req = data;
2510 return drm_mode_cursor_common(dev, req, file_priv);
2514 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2515 * @bpp: bits per pixels
2516 * @depth: bit depth per pixel
2518 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2519 * Useful in fbdev emulation code, since that deals in those values.
2521 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2527 fmt = DRM_FORMAT_C8;
2531 fmt = DRM_FORMAT_XRGB1555;
2533 fmt = DRM_FORMAT_RGB565;
2536 fmt = DRM_FORMAT_RGB888;
2540 fmt = DRM_FORMAT_XRGB8888;
2541 else if (depth == 30)
2542 fmt = DRM_FORMAT_XRGB2101010;
2544 fmt = DRM_FORMAT_ARGB8888;
2547 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2548 fmt = DRM_FORMAT_XRGB8888;
2554 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2557 * drm_mode_addfb - add an FB to the graphics configuration
2558 * @dev: drm device for the ioctl
2559 * @data: data pointer for the ioctl
2560 * @file_priv: drm file for the ioctl call
2562 * Add a new FB to the specified CRTC, given a user request. This is the
2563 * original addfb ioclt which only supported RGB formats.
2565 * Called by the user via ioctl.
2568 * Zero on success, errno on failure.
2570 int drm_mode_addfb(struct drm_device *dev,
2571 void *data, struct drm_file *file_priv)
2573 struct drm_mode_fb_cmd *or = data;
2574 struct drm_mode_fb_cmd2 r = {};
2575 struct drm_mode_config *config = &dev->mode_config;
2576 struct drm_framebuffer *fb;
2579 /* Use new struct with format internally */
2580 r.fb_id = or->fb_id;
2581 r.width = or->width;
2582 r.height = or->height;
2583 r.pitches[0] = or->pitch;
2584 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2585 r.handles[0] = or->handle;
2587 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2590 if ((config->min_width > r.width) || (r.width > config->max_width))
2593 if ((config->min_height > r.height) || (r.height > config->max_height))
2596 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2598 DRM_DEBUG_KMS("could not create framebuffer\n");
2602 mutex_lock(&file_priv->fbs_lock);
2603 or->fb_id = fb->base.id;
2604 list_add(&fb->filp_head, &file_priv->fbs);
2605 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2606 mutex_unlock(&file_priv->fbs_lock);
2611 static int format_check(const struct drm_mode_fb_cmd2 *r)
2613 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2617 case DRM_FORMAT_RGB332:
2618 case DRM_FORMAT_BGR233:
2619 case DRM_FORMAT_XRGB4444:
2620 case DRM_FORMAT_XBGR4444:
2621 case DRM_FORMAT_RGBX4444:
2622 case DRM_FORMAT_BGRX4444:
2623 case DRM_FORMAT_ARGB4444:
2624 case DRM_FORMAT_ABGR4444:
2625 case DRM_FORMAT_RGBA4444:
2626 case DRM_FORMAT_BGRA4444:
2627 case DRM_FORMAT_XRGB1555:
2628 case DRM_FORMAT_XBGR1555:
2629 case DRM_FORMAT_RGBX5551:
2630 case DRM_FORMAT_BGRX5551:
2631 case DRM_FORMAT_ARGB1555:
2632 case DRM_FORMAT_ABGR1555:
2633 case DRM_FORMAT_RGBA5551:
2634 case DRM_FORMAT_BGRA5551:
2635 case DRM_FORMAT_RGB565:
2636 case DRM_FORMAT_BGR565:
2637 case DRM_FORMAT_RGB888:
2638 case DRM_FORMAT_BGR888:
2639 case DRM_FORMAT_XRGB8888:
2640 case DRM_FORMAT_XBGR8888:
2641 case DRM_FORMAT_RGBX8888:
2642 case DRM_FORMAT_BGRX8888:
2643 case DRM_FORMAT_ARGB8888:
2644 case DRM_FORMAT_ABGR8888:
2645 case DRM_FORMAT_RGBA8888:
2646 case DRM_FORMAT_BGRA8888:
2647 case DRM_FORMAT_XRGB2101010:
2648 case DRM_FORMAT_XBGR2101010:
2649 case DRM_FORMAT_RGBX1010102:
2650 case DRM_FORMAT_BGRX1010102:
2651 case DRM_FORMAT_ARGB2101010:
2652 case DRM_FORMAT_ABGR2101010:
2653 case DRM_FORMAT_RGBA1010102:
2654 case DRM_FORMAT_BGRA1010102:
2655 case DRM_FORMAT_YUYV:
2656 case DRM_FORMAT_YVYU:
2657 case DRM_FORMAT_UYVY:
2658 case DRM_FORMAT_VYUY:
2659 case DRM_FORMAT_AYUV:
2660 case DRM_FORMAT_NV12:
2661 case DRM_FORMAT_NV21:
2662 case DRM_FORMAT_NV16:
2663 case DRM_FORMAT_NV61:
2664 case DRM_FORMAT_NV24:
2665 case DRM_FORMAT_NV42:
2666 case DRM_FORMAT_YUV410:
2667 case DRM_FORMAT_YVU410:
2668 case DRM_FORMAT_YUV411:
2669 case DRM_FORMAT_YVU411:
2670 case DRM_FORMAT_YUV420:
2671 case DRM_FORMAT_YVU420:
2672 case DRM_FORMAT_YUV422:
2673 case DRM_FORMAT_YVU422:
2674 case DRM_FORMAT_YUV444:
2675 case DRM_FORMAT_YVU444:
2678 DRM_DEBUG_KMS("invalid pixel format %s\n",
2679 drm_get_format_name(r->pixel_format));
2684 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2686 int ret, hsub, vsub, num_planes, i;
2688 ret = format_check(r);
2690 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2691 drm_get_format_name(r->pixel_format));
2695 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2696 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2697 num_planes = drm_format_num_planes(r->pixel_format);
2699 if (r->width == 0 || r->width % hsub) {
2700 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2704 if (r->height == 0 || r->height % vsub) {
2705 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2709 for (i = 0; i < num_planes; i++) {
2710 unsigned int width = r->width / (i != 0 ? hsub : 1);
2711 unsigned int height = r->height / (i != 0 ? vsub : 1);
2712 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2714 if (!r->handles[i]) {
2715 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2719 if ((uint64_t) width * cpp > UINT_MAX)
2722 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2725 if (r->pitches[i] < width * cpp) {
2726 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2735 * drm_mode_addfb2 - add an FB to the graphics configuration
2736 * @dev: drm device for the ioctl
2737 * @data: data pointer for the ioctl
2738 * @file_priv: drm file for the ioctl call
2740 * Add a new FB to the specified CRTC, given a user request with format. This is
2741 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2742 * and uses fourcc codes as pixel format specifiers.
2744 * Called by the user via ioctl.
2747 * Zero on success, errno on failure.
2749 int drm_mode_addfb2(struct drm_device *dev,
2750 void *data, struct drm_file *file_priv)
2752 struct drm_mode_fb_cmd2 *r = data;
2753 struct drm_mode_config *config = &dev->mode_config;
2754 struct drm_framebuffer *fb;
2757 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2760 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2761 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2765 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2766 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2767 r->width, config->min_width, config->max_width);
2770 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2771 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2772 r->height, config->min_height, config->max_height);
2776 ret = framebuffer_check(r);
2780 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2782 DRM_DEBUG_KMS("could not create framebuffer\n");
2786 mutex_lock(&file_priv->fbs_lock);
2787 r->fb_id = fb->base.id;
2788 list_add(&fb->filp_head, &file_priv->fbs);
2789 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2790 mutex_unlock(&file_priv->fbs_lock);
2797 * drm_mode_rmfb - remove an FB from the configuration
2798 * @dev: drm device for the ioctl
2799 * @data: data pointer for the ioctl
2800 * @file_priv: drm file for the ioctl call
2802 * Remove the FB specified by the user.
2804 * Called by the user via ioctl.
2807 * Zero on success, errno on failure.
2809 int drm_mode_rmfb(struct drm_device *dev,
2810 void *data, struct drm_file *file_priv)
2812 struct drm_framebuffer *fb = NULL;
2813 struct drm_framebuffer *fbl = NULL;
2814 uint32_t *id = data;
2817 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2820 mutex_lock(&file_priv->fbs_lock);
2821 mutex_lock(&dev->mode_config.fb_lock);
2822 fb = __drm_framebuffer_lookup(dev, *id);
2826 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2832 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2833 __drm_framebuffer_unregister(dev, fb);
2835 list_del_init(&fb->filp_head);
2836 mutex_unlock(&dev->mode_config.fb_lock);
2837 mutex_unlock(&file_priv->fbs_lock);
2839 drm_framebuffer_remove(fb);
2844 mutex_unlock(&dev->mode_config.fb_lock);
2845 mutex_unlock(&file_priv->fbs_lock);
2851 * drm_mode_getfb - get FB info
2852 * @dev: drm device for the ioctl
2853 * @data: data pointer for the ioctl
2854 * @file_priv: drm file for the ioctl call
2856 * Lookup the FB given its ID and return info about it.
2858 * Called by the user via ioctl.
2861 * Zero on success, errno on failure.
2863 int drm_mode_getfb(struct drm_device *dev,
2864 void *data, struct drm_file *file_priv)
2866 struct drm_mode_fb_cmd *r = data;
2867 struct drm_framebuffer *fb;
2870 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2873 fb = drm_framebuffer_lookup(dev, r->fb_id);
2877 r->height = fb->height;
2878 r->width = fb->width;
2879 r->depth = fb->depth;
2880 r->bpp = fb->bits_per_pixel;
2881 r->pitch = fb->pitches[0];
2882 if (fb->funcs->create_handle) {
2883 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2884 drm_is_control_client(file_priv)) {
2885 ret = fb->funcs->create_handle(fb, file_priv,
2888 /* GET_FB() is an unprivileged ioctl so we must not
2889 * return a buffer-handle to non-master processes! For
2890 * backwards-compatibility reasons, we cannot make
2891 * GET_FB() privileged, so just return an invalid handle
2892 * for non-masters. */
2900 drm_framebuffer_unreference(fb);
2906 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2907 * @dev: drm device for the ioctl
2908 * @data: data pointer for the ioctl
2909 * @file_priv: drm file for the ioctl call
2911 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2912 * rectangle list. Generic userspace which does frontbuffer rendering must call
2913 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2914 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2916 * Modesetting drivers which always update the frontbuffer do not need to
2917 * implement the corresponding ->dirty framebuffer callback.
2919 * Called by the user via ioctl.
2922 * Zero on success, errno on failure.
2924 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2925 void *data, struct drm_file *file_priv)
2927 struct drm_clip_rect __user *clips_ptr;
2928 struct drm_clip_rect *clips = NULL;
2929 struct drm_mode_fb_dirty_cmd *r = data;
2930 struct drm_framebuffer *fb;
2935 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2938 fb = drm_framebuffer_lookup(dev, r->fb_id);
2942 num_clips = r->num_clips;
2943 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2945 if (!num_clips != !clips_ptr) {
2950 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2952 /* If userspace annotates copy, clips must come in pairs */
2953 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2958 if (num_clips && clips_ptr) {
2959 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2963 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2969 ret = copy_from_user(clips, clips_ptr,
2970 num_clips * sizeof(*clips));
2977 if (fb->funcs->dirty) {
2978 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2987 drm_framebuffer_unreference(fb);
2994 * drm_fb_release - remove and free the FBs on this file
2995 * @priv: drm file for the ioctl
2997 * Destroy all the FBs associated with @filp.
2999 * Called by the user via ioctl.
3002 * Zero on success, errno on failure.
3004 void drm_fb_release(struct drm_file *priv)
3006 struct drm_device *dev = priv->minor->dev;
3007 struct drm_framebuffer *fb, *tfb;
3009 mutex_lock(&priv->fbs_lock);
3010 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3012 mutex_lock(&dev->mode_config.fb_lock);
3013 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3014 __drm_framebuffer_unregister(dev, fb);
3015 mutex_unlock(&dev->mode_config.fb_lock);
3017 list_del_init(&fb->filp_head);
3019 /* This will also drop the fpriv->fbs reference. */
3020 drm_framebuffer_remove(fb);
3022 mutex_unlock(&priv->fbs_lock);
3026 * drm_property_create - create a new property type
3028 * @flags: flags specifying the property type
3029 * @name: name of the property
3030 * @num_values: number of pre-defined values
3032 * This creates a new generic drm property which can then be attached to a drm
3033 * object with drm_object_attach_property. The returned property object must be
3034 * freed with drm_property_destroy.
3037 * A pointer to the newly created property on success, NULL on failure.
3039 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3040 const char *name, int num_values)
3042 struct drm_property *property = NULL;
3045 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3050 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3051 if (!property->values)
3055 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3059 property->flags = flags;
3060 property->num_values = num_values;
3061 INIT_LIST_HEAD(&property->enum_blob_list);
3064 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3065 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3068 list_add_tail(&property->head, &dev->mode_config.property_list);
3071 kfree(property->values);
3075 EXPORT_SYMBOL(drm_property_create);
3078 * drm_property_create - create a new enumeration property type
3080 * @flags: flags specifying the property type
3081 * @name: name of the property
3082 * @props: enumeration lists with property values
3083 * @num_values: number of pre-defined values
3085 * This creates a new generic drm property which can then be attached to a drm
3086 * object with drm_object_attach_property. The returned property object must be
3087 * freed with drm_property_destroy.
3089 * Userspace is only allowed to set one of the predefined values for enumeration
3093 * A pointer to the newly created property on success, NULL on failure.
3095 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3097 const struct drm_prop_enum_list *props,
3100 struct drm_property *property;
3103 flags |= DRM_MODE_PROP_ENUM;
3105 property = drm_property_create(dev, flags, name, num_values);
3109 for (i = 0; i < num_values; i++) {
3110 ret = drm_property_add_enum(property, i,
3114 drm_property_destroy(dev, property);
3121 EXPORT_SYMBOL(drm_property_create_enum);
3124 * drm_property_create - create a new bitmask property type
3126 * @flags: flags specifying the property type
3127 * @name: name of the property
3128 * @props: enumeration lists with property bitflags
3129 * @num_values: number of pre-defined values
3131 * This creates a new generic drm property which can then be attached to a drm
3132 * object with drm_object_attach_property. The returned property object must be
3133 * freed with drm_property_destroy.
3135 * Compared to plain enumeration properties userspace is allowed to set any
3136 * or'ed together combination of the predefined property bitflag values
3139 * A pointer to the newly created property on success, NULL on failure.
3141 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3142 int flags, const char *name,
3143 const struct drm_prop_enum_list *props,
3146 struct drm_property *property;
3149 flags |= DRM_MODE_PROP_BITMASK;
3151 property = drm_property_create(dev, flags, name, num_values);
3155 for (i = 0; i < num_values; i++) {
3156 ret = drm_property_add_enum(property, i,
3160 drm_property_destroy(dev, property);
3167 EXPORT_SYMBOL(drm_property_create_bitmask);
3170 * drm_property_create - create a new ranged property type
3172 * @flags: flags specifying the property type
3173 * @name: name of the property
3174 * @min: minimum value of the property
3175 * @max: maximum value of the property
3177 * This creates a new generic drm property which can then be attached to a drm
3178 * object with drm_object_attach_property. The returned property object must be
3179 * freed with drm_property_destroy.
3181 * Userspace is allowed to set any interger value in the (min, max) range
3185 * A pointer to the newly created property on success, NULL on failure.
3187 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3189 uint64_t min, uint64_t max)
3191 struct drm_property *property;
3193 flags |= DRM_MODE_PROP_RANGE;
3195 property = drm_property_create(dev, flags, name, 2);
3199 property->values[0] = min;
3200 property->values[1] = max;
3204 EXPORT_SYMBOL(drm_property_create_range);
3207 * drm_property_add_enum - add a possible value to an enumeration property
3208 * @property: enumeration property to change
3209 * @index: index of the new enumeration
3210 * @value: value of the new enumeration
3211 * @name: symbolic name of the new enumeration
3213 * This functions adds enumerations to a property.
3215 * It's use is deprecated, drivers should use one of the more specific helpers
3216 * to directly create the property with all enumerations already attached.
3219 * Zero on success, error code on failure.
3221 int drm_property_add_enum(struct drm_property *property, int index,
3222 uint64_t value, const char *name)
3224 struct drm_property_enum *prop_enum;
3226 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3230 * Bitmask enum properties have the additional constraint of values
3233 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
3236 if (!list_empty(&property->enum_blob_list)) {
3237 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3238 if (prop_enum->value == value) {
3239 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3240 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3246 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3250 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3251 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3252 prop_enum->value = value;
3254 property->values[index] = value;
3255 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3258 EXPORT_SYMBOL(drm_property_add_enum);
3261 * drm_property_destroy - destroy a drm property
3263 * @property: property to destry
3265 * This function frees a property including any attached resources like
3266 * enumeration values.
3268 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3270 struct drm_property_enum *prop_enum, *pt;
3272 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3273 list_del(&prop_enum->head);
3277 if (property->num_values)
3278 kfree(property->values);
3279 drm_mode_object_put(dev, &property->base);
3280 list_del(&property->head);
3283 EXPORT_SYMBOL(drm_property_destroy);
3286 * drm_object_attach_property - attach a property to a modeset object
3287 * @obj: drm modeset object
3288 * @property: property to attach
3289 * @init_val: initial value of the property
3291 * This attaches the given property to the modeset object with the given initial
3292 * value. Currently this function cannot fail since the properties are stored in
3293 * a statically sized array.
3295 void drm_object_attach_property(struct drm_mode_object *obj,
3296 struct drm_property *property,
3299 int count = obj->properties->count;
3301 if (count == DRM_OBJECT_MAX_PROPERTY) {
3302 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3303 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3304 "you see this message on the same object type.\n",
3309 obj->properties->ids[count] = property->base.id;
3310 obj->properties->values[count] = init_val;
3311 obj->properties->count++;
3313 EXPORT_SYMBOL(drm_object_attach_property);
3316 * drm_object_property_set_value - set the value of a property
3317 * @obj: drm mode object to set property value for
3318 * @property: property to set
3319 * @val: value the property should be set to
3321 * This functions sets a given property on a given object. This function only
3322 * changes the software state of the property, it does not call into the
3323 * driver's ->set_property callback.
3326 * Zero on success, error code on failure.
3328 int drm_object_property_set_value(struct drm_mode_object *obj,
3329 struct drm_property *property, uint64_t val)
3333 for (i = 0; i < obj->properties->count; i++) {
3334 if (obj->properties->ids[i] == property->base.id) {
3335 obj->properties->values[i] = val;
3342 EXPORT_SYMBOL(drm_object_property_set_value);
3345 * drm_object_property_get_value - retrieve the value of a property
3346 * @obj: drm mode object to get property value from
3347 * @property: property to retrieve
3348 * @val: storage for the property value
3350 * This function retrieves the softare state of the given property for the given
3351 * property. Since there is no driver callback to retrieve the current property
3352 * value this might be out of sync with the hardware, depending upon the driver
3356 * Zero on success, error code on failure.
3358 int drm_object_property_get_value(struct drm_mode_object *obj,
3359 struct drm_property *property, uint64_t *val)
3363 for (i = 0; i < obj->properties->count; i++) {
3364 if (obj->properties->ids[i] == property->base.id) {
3365 *val = obj->properties->values[i];
3372 EXPORT_SYMBOL(drm_object_property_get_value);
3375 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3378 * @file_priv: DRM file info
3380 * This function retrieves the current value for an connectors's property.
3382 * Called by the user via ioctl.
3385 * Zero on success, errno on failure.
3387 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3388 void *data, struct drm_file *file_priv)
3390 struct drm_mode_object *obj;
3391 struct drm_mode_get_property *out_resp = data;
3392 struct drm_property *property;
3395 int value_count = 0;
3398 struct drm_property_enum *prop_enum;
3399 struct drm_mode_property_enum __user *enum_ptr;
3400 struct drm_property_blob *prop_blob;
3401 uint32_t __user *blob_id_ptr;
3402 uint64_t __user *values_ptr;
3403 uint32_t __user *blob_length_ptr;
3405 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3408 drm_modeset_lock_all(dev);
3409 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3414 property = obj_to_property(obj);
3416 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3417 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3419 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3420 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3424 value_count = property->num_values;
3426 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3427 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3428 out_resp->flags = property->flags;
3430 if ((out_resp->count_values >= value_count) && value_count) {
3431 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3432 for (i = 0; i < value_count; i++) {
3433 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3439 out_resp->count_values = value_count;
3441 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3442 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3444 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3445 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3447 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3452 if (copy_to_user(&enum_ptr[copied].name,
3453 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3460 out_resp->count_enum_blobs = enum_count;
3463 if (property->flags & DRM_MODE_PROP_BLOB) {
3464 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3466 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3467 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3469 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3470 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3475 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3483 out_resp->count_enum_blobs = blob_count;
3486 drm_modeset_unlock_all(dev);
3490 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3493 struct drm_property_blob *blob;
3496 if (!length || !data)
3499 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3503 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3509 blob->length = length;
3511 memcpy(blob->data, data, length);
3513 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3517 static void drm_property_destroy_blob(struct drm_device *dev,
3518 struct drm_property_blob *blob)
3520 drm_mode_object_put(dev, &blob->base);
3521 list_del(&blob->head);
3526 * drm_mode_getblob_ioctl - get the contents of a blob property value
3529 * @file_priv: DRM file info
3531 * This function retrieves the contents of a blob property. The value stored in
3532 * an object's blob property is just a normal modeset object id.
3534 * Called by the user via ioctl.
3537 * Zero on success, errno on failure.
3539 int drm_mode_getblob_ioctl(struct drm_device *dev,
3540 void *data, struct drm_file *file_priv)
3542 struct drm_mode_object *obj;
3543 struct drm_mode_get_blob *out_resp = data;
3544 struct drm_property_blob *blob;
3546 void __user *blob_ptr;
3548 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3551 drm_modeset_lock_all(dev);
3552 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3557 blob = obj_to_blob(obj);
3559 if (out_resp->length == blob->length) {
3560 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3561 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3566 out_resp->length = blob->length;
3569 drm_modeset_unlock_all(dev);
3574 * drm_mode_connector_update_edid_property - update the edid property of a connector
3575 * @connector: drm connector
3576 * @edid: new value of the edid property
3578 * This function creates a new blob modeset object and assigns its id to the
3579 * connector's edid property.
3582 * Zero on success, errno on failure.
3584 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3587 struct drm_device *dev = connector->dev;
3590 if (connector->edid_blob_ptr)
3591 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3593 /* Delete edid, when there is none. */
3595 connector->edid_blob_ptr = NULL;
3596 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3600 size = EDID_LENGTH * (1 + edid->extensions);
3601 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3603 if (!connector->edid_blob_ptr)
3606 ret = drm_object_property_set_value(&connector->base,
3607 dev->mode_config.edid_property,
3608 connector->edid_blob_ptr->base.id);
3612 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3614 static bool drm_property_change_is_valid(struct drm_property *property,
3617 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3619 if (property->flags & DRM_MODE_PROP_RANGE) {
3620 if (value < property->values[0] || value > property->values[1])
3623 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3625 uint64_t valid_mask = 0;
3626 for (i = 0; i < property->num_values; i++)
3627 valid_mask |= (1ULL << property->values[i]);
3628 return !(value & ~valid_mask);
3629 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3630 /* Only the driver knows */
3634 for (i = 0; i < property->num_values; i++)
3635 if (property->values[i] == value)
3642 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3645 * @file_priv: DRM file info
3647 * This function sets the current value for a connectors's property. It also
3648 * calls into a driver's ->set_property callback to update the hardware state
3650 * Called by the user via ioctl.
3653 * Zero on success, errno on failure.
3655 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3656 void *data, struct drm_file *file_priv)
3658 struct drm_mode_connector_set_property *conn_set_prop = data;
3659 struct drm_mode_obj_set_property obj_set_prop = {
3660 .value = conn_set_prop->value,
3661 .prop_id = conn_set_prop->prop_id,
3662 .obj_id = conn_set_prop->connector_id,
3663 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3666 /* It does all the locking and checking we need */
3667 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3670 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3671 struct drm_property *property,
3675 struct drm_connector *connector = obj_to_connector(obj);
3677 /* Do DPMS ourselves */
3678 if (property == connector->dev->mode_config.dpms_property) {
3679 if (connector->funcs->dpms)
3680 (*connector->funcs->dpms)(connector, (int)value);
3682 } else if (connector->funcs->set_property)
3683 ret = connector->funcs->set_property(connector, property, value);
3685 /* store the property value if successful */
3687 drm_object_property_set_value(&connector->base, property, value);
3691 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3692 struct drm_property *property,
3696 struct drm_crtc *crtc = obj_to_crtc(obj);
3698 if (crtc->funcs->set_property)
3699 ret = crtc->funcs->set_property(crtc, property, value);
3701 drm_object_property_set_value(obj, property, value);
3706 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3707 struct drm_property *property,
3711 struct drm_plane *plane = obj_to_plane(obj);
3713 if (plane->funcs->set_property)
3714 ret = plane->funcs->set_property(plane, property, value);
3716 drm_object_property_set_value(obj, property, value);
3722 * drm_mode_getproperty_ioctl - get the current value of a object's property
3725 * @file_priv: DRM file info
3727 * This function retrieves the current value for an object's property. Compared
3728 * to the connector specific ioctl this one is extended to also work on crtc and
3731 * Called by the user via ioctl.
3734 * Zero on success, errno on failure.
3736 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3737 struct drm_file *file_priv)
3739 struct drm_mode_obj_get_properties *arg = data;
3740 struct drm_mode_object *obj;
3744 int props_count = 0;
3745 uint32_t __user *props_ptr;
3746 uint64_t __user *prop_values_ptr;
3748 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3751 drm_modeset_lock_all(dev);
3753 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3758 if (!obj->properties) {
3763 props_count = obj->properties->count;
3765 /* This ioctl is called twice, once to determine how much space is
3766 * needed, and the 2nd time to fill it. */
3767 if ((arg->count_props >= props_count) && props_count) {
3769 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3770 prop_values_ptr = (uint64_t __user *)(unsigned long)
3771 (arg->prop_values_ptr);
3772 for (i = 0; i < props_count; i++) {
3773 if (put_user(obj->properties->ids[i],
3774 props_ptr + copied)) {
3778 if (put_user(obj->properties->values[i],
3779 prop_values_ptr + copied)) {
3786 arg->count_props = props_count;
3788 drm_modeset_unlock_all(dev);
3793 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3796 * @file_priv: DRM file info
3798 * This function sets the current value for an object's property. It also calls
3799 * into a driver's ->set_property callback to update the hardware state.
3800 * Compared to the connector specific ioctl this one is extended to also work on
3801 * crtc and plane objects.
3803 * Called by the user via ioctl.
3806 * Zero on success, errno on failure.
3808 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3809 struct drm_file *file_priv)
3811 struct drm_mode_obj_set_property *arg = data;
3812 struct drm_mode_object *arg_obj;
3813 struct drm_mode_object *prop_obj;
3814 struct drm_property *property;
3818 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3821 drm_modeset_lock_all(dev);
3823 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3828 if (!arg_obj->properties)
3831 for (i = 0; i < arg_obj->properties->count; i++)
3832 if (arg_obj->properties->ids[i] == arg->prop_id)
3835 if (i == arg_obj->properties->count)
3838 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3839 DRM_MODE_OBJECT_PROPERTY);
3844 property = obj_to_property(prop_obj);
3846 if (!drm_property_change_is_valid(property, arg->value))
3849 switch (arg_obj->type) {
3850 case DRM_MODE_OBJECT_CONNECTOR:
3851 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3854 case DRM_MODE_OBJECT_CRTC:
3855 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3857 case DRM_MODE_OBJECT_PLANE:
3858 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3863 drm_modeset_unlock_all(dev);
3868 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3869 * @connector: connector to attach
3870 * @encoder: encoder to attach @connector to
3872 * This function links up a connector to an encoder. Note that the routing
3873 * restrictions between encoders and crtcs are exposed to userspace through the
3874 * possible_clones and possible_crtcs bitmasks.
3877 * Zero on success, errno on failure.
3879 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3880 struct drm_encoder *encoder)
3884 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3885 if (connector->encoder_ids[i] == 0) {
3886 connector->encoder_ids[i] = encoder->base.id;
3892 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3894 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3895 struct drm_encoder *encoder)
3898 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3899 if (connector->encoder_ids[i] == encoder->base.id) {
3900 connector->encoder_ids[i] = 0;
3901 if (connector->encoder == encoder)
3902 connector->encoder = NULL;
3907 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3910 * drm_mode_crtc_set_gamma_size - set the gamma table size
3911 * @crtc: CRTC to set the gamma table size for
3912 * @gamma_size: size of the gamma table
3914 * Drivers which support gamma tables should set this to the supported gamma
3915 * table size when initializing the CRTC. Currently the drm core only supports a
3916 * fixed gamma table size.
3919 * Zero on success, errno on failure.
3921 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3924 crtc->gamma_size = gamma_size;
3926 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3927 if (!crtc->gamma_store) {
3928 crtc->gamma_size = 0;
3934 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3937 * drm_mode_gamma_set_ioctl - set the gamma table
3940 * @file_priv: DRM file info
3942 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3943 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3945 * Called by the user via ioctl.
3948 * Zero on success, errno on failure.
3950 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3951 void *data, struct drm_file *file_priv)
3953 struct drm_mode_crtc_lut *crtc_lut = data;
3954 struct drm_mode_object *obj;
3955 struct drm_crtc *crtc;
3956 void *r_base, *g_base, *b_base;
3960 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3963 drm_modeset_lock_all(dev);
3964 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3969 crtc = obj_to_crtc(obj);
3971 if (crtc->funcs->gamma_set == NULL) {
3976 /* memcpy into gamma store */
3977 if (crtc_lut->gamma_size != crtc->gamma_size) {
3982 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3983 r_base = crtc->gamma_store;
3984 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3989 g_base = r_base + size;
3990 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3995 b_base = g_base + size;
3996 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4001 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4004 drm_modeset_unlock_all(dev);
4010 * drm_mode_gamma_get_ioctl - get the gamma table
4013 * @file_priv: DRM file info
4015 * Copy the current gamma table into the storage provided. This also provides
4016 * the gamma table size the driver expects, which can be used to size the
4017 * allocated storage.
4019 * Called by the user via ioctl.
4022 * Zero on success, errno on failure.
4024 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4025 void *data, struct drm_file *file_priv)
4027 struct drm_mode_crtc_lut *crtc_lut = data;
4028 struct drm_mode_object *obj;
4029 struct drm_crtc *crtc;
4030 void *r_base, *g_base, *b_base;
4034 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4037 drm_modeset_lock_all(dev);
4038 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4043 crtc = obj_to_crtc(obj);
4045 /* memcpy into gamma store */
4046 if (crtc_lut->gamma_size != crtc->gamma_size) {
4051 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4052 r_base = crtc->gamma_store;
4053 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4058 g_base = r_base + size;
4059 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4064 b_base = g_base + size;
4065 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4070 drm_modeset_unlock_all(dev);
4075 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4078 * @file_priv: DRM file info
4080 * This schedules an asynchronous update on a given CRTC, called page flip.
4081 * Optionally a drm event is generated to signal the completion of the event.
4082 * Generic drivers cannot assume that a pageflip with changed framebuffer
4083 * properties (including driver specific metadata like tiling layout) will work,
4084 * but some drivers support e.g. pixel format changes through the pageflip
4087 * Called by the user via ioctl.
4090 * Zero on success, errno on failure.
4092 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4093 void *data, struct drm_file *file_priv)
4095 struct drm_mode_crtc_page_flip *page_flip = data;
4096 struct drm_mode_object *obj;
4097 struct drm_crtc *crtc;
4098 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4099 struct drm_pending_vblank_event *e = NULL;
4100 unsigned long flags;
4103 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4104 page_flip->reserved != 0)
4107 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4110 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4113 crtc = obj_to_crtc(obj);
4115 mutex_lock(&crtc->mutex);
4116 if (crtc->fb == NULL) {
4117 /* The framebuffer is currently unbound, presumably
4118 * due to a hotplug event, that userspace has not
4125 if (crtc->funcs->page_flip == NULL)
4128 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4134 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4138 if (crtc->fb->pixel_format != fb->pixel_format) {
4139 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4144 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4146 spin_lock_irqsave(&dev->event_lock, flags);
4147 if (file_priv->event_space < sizeof e->event) {
4148 spin_unlock_irqrestore(&dev->event_lock, flags);
4151 file_priv->event_space -= sizeof e->event;
4152 spin_unlock_irqrestore(&dev->event_lock, flags);
4154 e = kzalloc(sizeof *e, GFP_KERNEL);
4156 spin_lock_irqsave(&dev->event_lock, flags);
4157 file_priv->event_space += sizeof e->event;
4158 spin_unlock_irqrestore(&dev->event_lock, flags);
4162 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4163 e->event.base.length = sizeof e->event;
4164 e->event.user_data = page_flip->user_data;
4165 e->base.event = &e->event.base;
4166 e->base.file_priv = file_priv;
4168 (void (*) (struct drm_pending_event *)) kfree;
4172 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4174 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4175 spin_lock_irqsave(&dev->event_lock, flags);
4176 file_priv->event_space += sizeof e->event;
4177 spin_unlock_irqrestore(&dev->event_lock, flags);
4180 /* Keep the old fb, don't unref it. */
4184 * Warn if the driver hasn't properly updated the crtc->fb
4185 * field to reflect that the new framebuffer is now used.
4186 * Failing to do so will screw with the reference counting
4189 WARN_ON(crtc->fb != fb);
4190 /* Unref only the old framebuffer. */
4196 drm_framebuffer_unreference(fb);
4198 drm_framebuffer_unreference(old_fb);
4199 mutex_unlock(&crtc->mutex);
4205 * drm_mode_config_reset - call ->reset callbacks
4208 * This functions calls all the crtc's, encoder's and connector's ->reset
4209 * callback. Drivers can use this in e.g. their driver load or resume code to
4210 * reset hardware and software state.
4212 void drm_mode_config_reset(struct drm_device *dev)
4214 struct drm_crtc *crtc;
4215 struct drm_encoder *encoder;
4216 struct drm_connector *connector;
4218 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4219 if (crtc->funcs->reset)
4220 crtc->funcs->reset(crtc);
4222 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4223 if (encoder->funcs->reset)
4224 encoder->funcs->reset(encoder);
4226 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4227 connector->status = connector_status_unknown;
4229 if (connector->funcs->reset)
4230 connector->funcs->reset(connector);
4233 EXPORT_SYMBOL(drm_mode_config_reset);
4236 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4239 * @file_priv: DRM file info
4241 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4242 * TTM or something else entirely) and returns the resulting buffer handle. This
4243 * handle can then be wrapped up into a framebuffer modeset object.
4245 * Note that userspace is not allowed to use such objects for render
4246 * acceleration - drivers must create their own private ioctls for such a use
4249 * Called by the user via ioctl.
4252 * Zero on success, errno on failure.
4254 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4255 void *data, struct drm_file *file_priv)
4257 struct drm_mode_create_dumb *args = data;
4259 if (!dev->driver->dumb_create)
4261 return dev->driver->dumb_create(file_priv, dev, args);
4265 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4268 * @file_priv: DRM file info
4270 * Allocate an offset in the drm device node's address space to be able to
4271 * memory map a dumb buffer.
4273 * Called by the user via ioctl.
4276 * Zero on success, errno on failure.
4278 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4279 void *data, struct drm_file *file_priv)
4281 struct drm_mode_map_dumb *args = data;
4283 /* call driver ioctl to get mmap offset */
4284 if (!dev->driver->dumb_map_offset)
4287 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4291 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4294 * @file_priv: DRM file info
4296 * This destroys the userspace handle for the given dumb backing storage buffer.
4297 * Since buffer objects must be reference counted in the kernel a buffer object
4298 * won't be immediately freed if a framebuffer modeset object still uses it.
4300 * Called by the user via ioctl.
4303 * Zero on success, errno on failure.
4305 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4306 void *data, struct drm_file *file_priv)
4308 struct drm_mode_destroy_dumb *args = data;
4310 if (!dev->driver->dumb_destroy)
4313 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4317 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4318 * @format: pixel format (DRM_FORMAT_*)
4319 * @depth: storage for the depth value
4320 * @bpp: storage for the bpp value
4322 * This only supports RGB formats here for compat with code that doesn't use
4323 * pixel formats directly yet.
4325 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4330 case DRM_FORMAT_RGB332:
4331 case DRM_FORMAT_BGR233:
4335 case DRM_FORMAT_XRGB1555:
4336 case DRM_FORMAT_XBGR1555:
4337 case DRM_FORMAT_RGBX5551:
4338 case DRM_FORMAT_BGRX5551:
4339 case DRM_FORMAT_ARGB1555:
4340 case DRM_FORMAT_ABGR1555:
4341 case DRM_FORMAT_RGBA5551:
4342 case DRM_FORMAT_BGRA5551:
4346 case DRM_FORMAT_RGB565:
4347 case DRM_FORMAT_BGR565:
4351 case DRM_FORMAT_RGB888:
4352 case DRM_FORMAT_BGR888:
4356 case DRM_FORMAT_XRGB8888:
4357 case DRM_FORMAT_XBGR8888:
4358 case DRM_FORMAT_RGBX8888:
4359 case DRM_FORMAT_BGRX8888:
4363 case DRM_FORMAT_XRGB2101010:
4364 case DRM_FORMAT_XBGR2101010:
4365 case DRM_FORMAT_RGBX1010102:
4366 case DRM_FORMAT_BGRX1010102:
4367 case DRM_FORMAT_ARGB2101010:
4368 case DRM_FORMAT_ABGR2101010:
4369 case DRM_FORMAT_RGBA1010102:
4370 case DRM_FORMAT_BGRA1010102:
4374 case DRM_FORMAT_ARGB8888:
4375 case DRM_FORMAT_ABGR8888:
4376 case DRM_FORMAT_RGBA8888:
4377 case DRM_FORMAT_BGRA8888:
4382 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4383 drm_get_format_name(format));
4389 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4392 * drm_format_num_planes - get the number of planes for format
4393 * @format: pixel format (DRM_FORMAT_*)
4396 * The number of planes used by the specified pixel format.
4398 int drm_format_num_planes(uint32_t format)
4401 case DRM_FORMAT_YUV410:
4402 case DRM_FORMAT_YVU410:
4403 case DRM_FORMAT_YUV411:
4404 case DRM_FORMAT_YVU411:
4405 case DRM_FORMAT_YUV420:
4406 case DRM_FORMAT_YVU420:
4407 case DRM_FORMAT_YUV422:
4408 case DRM_FORMAT_YVU422:
4409 case DRM_FORMAT_YUV444:
4410 case DRM_FORMAT_YVU444:
4412 case DRM_FORMAT_NV12:
4413 case DRM_FORMAT_NV21:
4414 case DRM_FORMAT_NV16:
4415 case DRM_FORMAT_NV61:
4416 case DRM_FORMAT_NV24:
4417 case DRM_FORMAT_NV42:
4423 EXPORT_SYMBOL(drm_format_num_planes);
4426 * drm_format_plane_cpp - determine the bytes per pixel value
4427 * @format: pixel format (DRM_FORMAT_*)
4428 * @plane: plane index
4431 * The bytes per pixel value for the specified plane.
4433 int drm_format_plane_cpp(uint32_t format, int plane)
4438 if (plane >= drm_format_num_planes(format))
4442 case DRM_FORMAT_YUYV:
4443 case DRM_FORMAT_YVYU:
4444 case DRM_FORMAT_UYVY:
4445 case DRM_FORMAT_VYUY:
4447 case DRM_FORMAT_NV12:
4448 case DRM_FORMAT_NV21:
4449 case DRM_FORMAT_NV16:
4450 case DRM_FORMAT_NV61:
4451 case DRM_FORMAT_NV24:
4452 case DRM_FORMAT_NV42:
4453 return plane ? 2 : 1;
4454 case DRM_FORMAT_YUV410:
4455 case DRM_FORMAT_YVU410:
4456 case DRM_FORMAT_YUV411:
4457 case DRM_FORMAT_YVU411:
4458 case DRM_FORMAT_YUV420:
4459 case DRM_FORMAT_YVU420:
4460 case DRM_FORMAT_YUV422:
4461 case DRM_FORMAT_YVU422:
4462 case DRM_FORMAT_YUV444:
4463 case DRM_FORMAT_YVU444:
4466 drm_fb_get_bpp_depth(format, &depth, &bpp);
4470 EXPORT_SYMBOL(drm_format_plane_cpp);
4473 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4474 * @format: pixel format (DRM_FORMAT_*)
4477 * The horizontal chroma subsampling factor for the
4478 * specified pixel format.
4480 int drm_format_horz_chroma_subsampling(uint32_t format)
4483 case DRM_FORMAT_YUV411:
4484 case DRM_FORMAT_YVU411:
4485 case DRM_FORMAT_YUV410:
4486 case DRM_FORMAT_YVU410:
4488 case DRM_FORMAT_YUYV:
4489 case DRM_FORMAT_YVYU:
4490 case DRM_FORMAT_UYVY:
4491 case DRM_FORMAT_VYUY:
4492 case DRM_FORMAT_NV12:
4493 case DRM_FORMAT_NV21:
4494 case DRM_FORMAT_NV16:
4495 case DRM_FORMAT_NV61:
4496 case DRM_FORMAT_YUV422:
4497 case DRM_FORMAT_YVU422:
4498 case DRM_FORMAT_YUV420:
4499 case DRM_FORMAT_YVU420:
4505 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4508 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4509 * @format: pixel format (DRM_FORMAT_*)
4512 * The vertical chroma subsampling factor for the
4513 * specified pixel format.
4515 int drm_format_vert_chroma_subsampling(uint32_t format)
4518 case DRM_FORMAT_YUV410:
4519 case DRM_FORMAT_YVU410:
4521 case DRM_FORMAT_YUV420:
4522 case DRM_FORMAT_YVU420:
4523 case DRM_FORMAT_NV12:
4524 case DRM_FORMAT_NV21:
4530 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4533 * drm_mode_config_init - initialize DRM mode_configuration structure
4536 * Initialize @dev's mode_config structure, used for tracking the graphics
4537 * configuration of @dev.
4539 * Since this initializes the modeset locks, no locking is possible. Which is no
4540 * problem, since this should happen single threaded at init time. It is the
4541 * driver's problem to ensure this guarantee.
4544 void drm_mode_config_init(struct drm_device *dev)
4546 mutex_init(&dev->mode_config.mutex);
4547 mutex_init(&dev->mode_config.idr_mutex);
4548 mutex_init(&dev->mode_config.fb_lock);
4549 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4550 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4551 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4552 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4553 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4554 INIT_LIST_HEAD(&dev->mode_config.property_list);
4555 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4556 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4557 idr_init(&dev->mode_config.crtc_idr);
4559 drm_modeset_lock_all(dev);
4560 drm_mode_create_standard_connector_properties(dev);
4561 drm_modeset_unlock_all(dev);
4563 /* Just to be sure */
4564 dev->mode_config.num_fb = 0;
4565 dev->mode_config.num_connector = 0;
4566 dev->mode_config.num_crtc = 0;
4567 dev->mode_config.num_encoder = 0;
4569 EXPORT_SYMBOL(drm_mode_config_init);
4572 * drm_mode_config_cleanup - free up DRM mode_config info
4575 * Free up all the connectors and CRTCs associated with this DRM device, then
4576 * free up the framebuffers and associated buffer objects.
4578 * Note that since this /should/ happen single-threaded at driver/device
4579 * teardown time, no locking is required. It's the driver's job to ensure that
4580 * this guarantee actually holds true.
4582 * FIXME: cleanup any dangling user buffer objects too
4584 void drm_mode_config_cleanup(struct drm_device *dev)
4586 struct drm_connector *connector, *ot;
4587 struct drm_crtc *crtc, *ct;
4588 struct drm_encoder *encoder, *enct;
4589 struct drm_bridge *bridge, *brt;
4590 struct drm_framebuffer *fb, *fbt;
4591 struct drm_property *property, *pt;
4592 struct drm_property_blob *blob, *bt;
4593 struct drm_plane *plane, *plt;
4595 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4597 encoder->funcs->destroy(encoder);
4600 list_for_each_entry_safe(bridge, brt,
4601 &dev->mode_config.bridge_list, head) {
4602 bridge->funcs->destroy(bridge);
4605 list_for_each_entry_safe(connector, ot,
4606 &dev->mode_config.connector_list, head) {
4607 connector->funcs->destroy(connector);
4610 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4612 drm_property_destroy(dev, property);
4615 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4617 drm_property_destroy_blob(dev, blob);
4621 * Single-threaded teardown context, so it's not required to grab the
4622 * fb_lock to protect against concurrent fb_list access. Contrary, it
4623 * would actually deadlock with the drm_framebuffer_cleanup function.
4625 * Also, if there are any framebuffers left, that's a driver leak now,
4626 * so politely WARN about this.
4628 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4629 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4630 drm_framebuffer_remove(fb);
4633 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4635 plane->funcs->destroy(plane);
4638 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4639 crtc->funcs->destroy(crtc);
4642 idr_destroy(&dev->mode_config.crtc_idr);
4644 EXPORT_SYMBOL(drm_mode_config_cleanup);