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/list.h>
33 #include <linux/slab.h>
34 #include <linux/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_fourcc.h>
41 * drm_modeset_lock_all - take all modeset locks
44 * This function takes all modeset locks, suitable where a more fine-grained
45 * scheme isn't (yet) implemented.
47 void drm_modeset_lock_all(struct drm_device *dev)
49 struct drm_crtc *crtc;
51 mutex_lock(&dev->mode_config.mutex);
53 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
54 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
56 EXPORT_SYMBOL(drm_modeset_lock_all);
59 * drm_modeset_unlock_all - drop all modeset locks
62 void drm_modeset_unlock_all(struct drm_device *dev)
64 struct drm_crtc *crtc;
66 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
67 mutex_unlock(&crtc->mutex);
69 mutex_unlock(&dev->mode_config.mutex);
71 EXPORT_SYMBOL(drm_modeset_unlock_all);
74 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
77 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
79 struct drm_crtc *crtc;
81 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
82 WARN_ON(!mutex_is_locked(&crtc->mutex));
84 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
86 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
88 /* Avoid boilerplate. I'm tired of typing. */
89 #define DRM_ENUM_NAME_FN(fnname, list) \
90 char *fnname(int val) \
93 for (i = 0; i < ARRAY_SIZE(list); i++) { \
94 if (list[i].type == val) \
95 return list[i].name; \
103 static struct drm_prop_enum_list drm_dpms_enum_list[] =
104 { { DRM_MODE_DPMS_ON, "On" },
105 { DRM_MODE_DPMS_STANDBY, "Standby" },
106 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
107 { DRM_MODE_DPMS_OFF, "Off" }
110 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
113 * Optional properties
115 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
117 { DRM_MODE_SCALE_NONE, "None" },
118 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
119 { DRM_MODE_SCALE_CENTER, "Center" },
120 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
123 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
125 { DRM_MODE_DITHERING_OFF, "Off" },
126 { DRM_MODE_DITHERING_ON, "On" },
127 { DRM_MODE_DITHERING_AUTO, "Automatic" },
131 * Non-global properties, but "required" for certain connectors.
133 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
140 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
142 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
149 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
150 drm_dvi_i_subconnector_enum_list)
152 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
161 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
163 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
172 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
173 drm_tv_subconnector_enum_list)
175 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
176 { DRM_MODE_DIRTY_OFF, "Off" },
177 { DRM_MODE_DIRTY_ON, "On" },
178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
181 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
182 drm_dirty_info_enum_list)
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", 0 },
195 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
196 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
197 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
198 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
199 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
200 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
201 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
202 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
203 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
204 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
205 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
206 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
207 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
208 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
209 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
212 static struct drm_prop_enum_list drm_encoder_enum_list[] =
213 { { DRM_MODE_ENCODER_NONE, "None" },
214 { DRM_MODE_ENCODER_DAC, "DAC" },
215 { DRM_MODE_ENCODER_TMDS, "TMDS" },
216 { DRM_MODE_ENCODER_LVDS, "LVDS" },
217 { DRM_MODE_ENCODER_TVDAC, "TV" },
218 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
221 char *drm_get_encoder_name(struct drm_encoder *encoder)
225 snprintf(buf, 32, "%s-%d",
226 drm_encoder_enum_list[encoder->encoder_type].name,
230 EXPORT_SYMBOL(drm_get_encoder_name);
232 char *drm_get_connector_name(struct drm_connector *connector)
236 snprintf(buf, 32, "%s-%d",
237 drm_connector_enum_list[connector->connector_type].name,
238 connector->connector_type_id);
241 EXPORT_SYMBOL(drm_get_connector_name);
243 char *drm_get_connector_status_name(enum drm_connector_status status)
245 if (status == connector_status_connected)
247 else if (status == connector_status_disconnected)
248 return "disconnected";
254 * drm_mode_object_get - allocate a new modeset identifier
256 * @obj: object pointer, used to generate unique ID
257 * @obj_type: object type
259 * Create a unique identifier based on @ptr in @dev's identifier space. Used
260 * for tracking modes, CRTCs and connectors.
263 * New unique (relative to other objects in @dev) integer identifier for the
266 static int drm_mode_object_get(struct drm_device *dev,
267 struct drm_mode_object *obj, uint32_t obj_type)
273 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
274 DRM_ERROR("Ran out memory getting a mode number\n");
278 mutex_lock(&dev->mode_config.idr_mutex);
279 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
283 * Set up the object linking under the protection of the idr
284 * lock so that other users can't see inconsistent state.
287 obj->type = obj_type;
289 mutex_unlock(&dev->mode_config.idr_mutex);
298 * drm_mode_object_put - free a modeset identifer
300 * @object: object to free
302 * Free @id from @dev's unique identifier pool.
304 static void drm_mode_object_put(struct drm_device *dev,
305 struct drm_mode_object *object)
307 mutex_lock(&dev->mode_config.idr_mutex);
308 idr_remove(&dev->mode_config.crtc_idr, object->id);
309 mutex_unlock(&dev->mode_config.idr_mutex);
313 * drm_mode_object_find - look up a drm object with static lifetime
315 * @id: id of the mode object
316 * @type: type of the mode object
318 * Note that framebuffers cannot be looked up with this functions - since those
319 * are reference counted, they need special treatment.
321 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
322 uint32_t id, uint32_t type)
324 struct drm_mode_object *obj = NULL;
326 /* Framebuffers are reference counted and need their own lookup
328 WARN_ON(type == DRM_MODE_OBJECT_FB);
330 mutex_lock(&dev->mode_config.idr_mutex);
331 obj = idr_find(&dev->mode_config.crtc_idr, id);
332 if (!obj || (obj->type != type) || (obj->id != id))
334 mutex_unlock(&dev->mode_config.idr_mutex);
338 EXPORT_SYMBOL(drm_mode_object_find);
341 * drm_framebuffer_init - initialize a framebuffer
343 * @fb: framebuffer to be initialized
344 * @funcs: ... with these functions
346 * Allocates an ID for the framebuffer's parent mode object, sets its mode
347 * functions & device file and adds it to the master fd list.
350 * This functions publishes the fb and makes it available for concurrent access
351 * by other users. Which means by this point the fb _must_ be fully set up -
352 * since all the fb attributes are invariant over its lifetime, no further
353 * locking but only correct reference counting is required.
356 * Zero on success, error code on failure.
358 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
359 const struct drm_framebuffer_funcs *funcs)
363 mutex_lock(&dev->mode_config.fb_lock);
364 kref_init(&fb->refcount);
365 INIT_LIST_HEAD(&fb->filp_head);
369 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
373 /* Grab the idr reference. */
374 drm_framebuffer_reference(fb);
376 dev->mode_config.num_fb++;
377 list_add(&fb->head, &dev->mode_config.fb_list);
379 mutex_unlock(&dev->mode_config.fb_lock);
383 EXPORT_SYMBOL(drm_framebuffer_init);
385 static void drm_framebuffer_free(struct kref *kref)
387 struct drm_framebuffer *fb =
388 container_of(kref, struct drm_framebuffer, refcount);
389 fb->funcs->destroy(fb);
392 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
395 struct drm_mode_object *obj = NULL;
396 struct drm_framebuffer *fb;
398 mutex_lock(&dev->mode_config.idr_mutex);
399 obj = idr_find(&dev->mode_config.crtc_idr, id);
400 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
404 mutex_unlock(&dev->mode_config.idr_mutex);
410 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
412 * @id: id of the fb object
414 * If successful, this grabs an additional reference to the framebuffer -
415 * callers need to make sure to eventually unreference the returned framebuffer
418 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
421 struct drm_framebuffer *fb;
423 mutex_lock(&dev->mode_config.fb_lock);
424 fb = __drm_framebuffer_lookup(dev, id);
426 kref_get(&fb->refcount);
427 mutex_unlock(&dev->mode_config.fb_lock);
431 EXPORT_SYMBOL(drm_framebuffer_lookup);
434 * drm_framebuffer_unreference - unref a framebuffer
435 * @fb: framebuffer to unref
437 * This functions decrements the fb's refcount and frees it if it drops to zero.
439 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
441 DRM_DEBUG("FB ID: %d\n", fb->base.id);
442 kref_put(&fb->refcount, drm_framebuffer_free);
444 EXPORT_SYMBOL(drm_framebuffer_unreference);
447 * drm_framebuffer_reference - incr the fb refcnt
450 void drm_framebuffer_reference(struct drm_framebuffer *fb)
452 DRM_DEBUG("FB ID: %d\n", fb->base.id);
453 kref_get(&fb->refcount);
455 EXPORT_SYMBOL(drm_framebuffer_reference);
457 static void drm_framebuffer_free_bug(struct kref *kref)
462 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
464 DRM_DEBUG("FB ID: %d\n", fb->base.id);
465 kref_put(&fb->refcount, drm_framebuffer_free_bug);
468 /* dev->mode_config.fb_lock must be held! */
469 static void __drm_framebuffer_unregister(struct drm_device *dev,
470 struct drm_framebuffer *fb)
472 mutex_lock(&dev->mode_config.idr_mutex);
473 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
474 mutex_unlock(&dev->mode_config.idr_mutex);
478 __drm_framebuffer_unreference(fb);
482 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
483 * @fb: fb to unregister
485 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
486 * those used for fbdev. Note that the caller must hold a reference of it's own,
487 * i.e. the object may not be destroyed through this call (since it'll lead to a
488 * locking inversion).
490 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
492 struct drm_device *dev = fb->dev;
494 mutex_lock(&dev->mode_config.fb_lock);
495 /* Mark fb as reaped and drop idr ref. */
496 __drm_framebuffer_unregister(dev, fb);
497 mutex_unlock(&dev->mode_config.fb_lock);
499 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
505 * Cleanup references to a user-created framebuffer. This function is intended
506 * to be used from the drivers ->destroy callback.
508 * Note that this function does not remove the fb from active usuage - if it is
509 * still used anywhere, hilarity can ensue since userspace could call getfb on
510 * the id and get back -EINVAL. Obviously no concern at driver unload time.
512 * Also, the framebuffer will not be removed from the lookup idr - for
513 * user-created framebuffers this will happen in in the rmfb ioctl. For
514 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
515 * drm_framebuffer_unregister_private.
517 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
519 struct drm_device *dev = fb->dev;
521 mutex_lock(&dev->mode_config.fb_lock);
523 dev->mode_config.num_fb--;
524 mutex_unlock(&dev->mode_config.fb_lock);
526 EXPORT_SYMBOL(drm_framebuffer_cleanup);
529 * drm_framebuffer_remove - remove and unreference a framebuffer object
530 * @fb: framebuffer to remove
532 * Scans all the CRTCs and planes in @dev's mode_config. If they're
533 * using @fb, removes it, setting it to NULL. Then drops the reference to the
534 * passed-in framebuffer. Might take the modeset locks.
536 * Note that this function optimizes the cleanup away if the caller holds the
537 * last reference to the framebuffer. It is also guaranteed to not take the
538 * modeset locks in this case.
540 void drm_framebuffer_remove(struct drm_framebuffer *fb)
542 struct drm_device *dev = fb->dev;
543 struct drm_crtc *crtc;
544 struct drm_plane *plane;
545 struct drm_mode_set set;
548 WARN_ON(!list_empty(&fb->filp_head));
551 * drm ABI mandates that we remove any deleted framebuffers from active
552 * useage. But since most sane clients only remove framebuffers they no
553 * longer need, try to optimize this away.
555 * Since we're holding a reference ourselves, observing a refcount of 1
556 * means that we're the last holder and can skip it. Also, the refcount
557 * can never increase from 1 again, so we don't need any barriers or
560 * Note that userspace could try to race with use and instate a new
561 * usage _after_ we've cleared all current ones. End result will be an
562 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
565 if (atomic_read(&fb->refcount.refcount) > 1) {
566 drm_modeset_lock_all(dev);
567 /* remove from any CRTC */
568 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
569 if (crtc->fb == fb) {
570 /* should turn off the crtc */
571 memset(&set, 0, sizeof(struct drm_mode_set));
574 ret = drm_mode_set_config_internal(&set);
576 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
580 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
581 if (plane->fb == fb) {
582 /* should turn off the crtc */
583 ret = plane->funcs->disable_plane(plane);
585 DRM_ERROR("failed to disable plane with busy fb\n");
586 /* disconnect the plane from the fb and crtc: */
587 __drm_framebuffer_unreference(plane->fb);
592 drm_modeset_unlock_all(dev);
595 drm_framebuffer_unreference(fb);
597 EXPORT_SYMBOL(drm_framebuffer_remove);
600 * drm_crtc_init - Initialise a new CRTC object
602 * @crtc: CRTC object to init
603 * @funcs: callbacks for the new CRTC
605 * Inits a new object created as base part of an driver crtc object.
608 * Zero on success, error code on failure.
610 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
611 const struct drm_crtc_funcs *funcs)
617 crtc->invert_dimensions = false;
619 drm_modeset_lock_all(dev);
620 mutex_init(&crtc->mutex);
621 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
623 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
627 crtc->base.properties = &crtc->properties;
629 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
630 dev->mode_config.num_crtc++;
633 drm_modeset_unlock_all(dev);
637 EXPORT_SYMBOL(drm_crtc_init);
640 * drm_crtc_cleanup - Cleans up the core crtc usage.
641 * @crtc: CRTC to cleanup
643 * Cleanup @crtc. Removes from drm modesetting space
644 * does NOT free object, caller does that.
646 void drm_crtc_cleanup(struct drm_crtc *crtc)
648 struct drm_device *dev = crtc->dev;
650 kfree(crtc->gamma_store);
651 crtc->gamma_store = NULL;
653 drm_mode_object_put(dev, &crtc->base);
654 list_del(&crtc->head);
655 dev->mode_config.num_crtc--;
657 EXPORT_SYMBOL(drm_crtc_cleanup);
660 * drm_mode_probed_add - add a mode to a connector's probed mode list
661 * @connector: connector the new mode
664 * Add @mode to @connector's mode list for later use.
666 void drm_mode_probed_add(struct drm_connector *connector,
667 struct drm_display_mode *mode)
669 list_add(&mode->head, &connector->probed_modes);
671 EXPORT_SYMBOL(drm_mode_probed_add);
674 * drm_mode_remove - remove and free a mode
675 * @connector: connector list to modify
676 * @mode: mode to remove
678 * Remove @mode from @connector's mode list, then free it.
680 void drm_mode_remove(struct drm_connector *connector,
681 struct drm_display_mode *mode)
683 list_del(&mode->head);
684 drm_mode_destroy(connector->dev, mode);
686 EXPORT_SYMBOL(drm_mode_remove);
689 * drm_connector_init - Init a preallocated connector
691 * @connector: the connector to init
692 * @funcs: callbacks for this connector
693 * @connector_type: user visible type of the connector
695 * Initialises a preallocated connector. Connectors should be
696 * subclassed as part of driver connector objects.
699 * Zero on success, error code on failure.
701 int drm_connector_init(struct drm_device *dev,
702 struct drm_connector *connector,
703 const struct drm_connector_funcs *funcs,
708 drm_modeset_lock_all(dev);
710 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
714 connector->base.properties = &connector->properties;
715 connector->dev = dev;
716 connector->funcs = funcs;
717 connector->connector_type = connector_type;
718 connector->connector_type_id =
719 ++drm_connector_enum_list[connector_type].count; /* TODO */
720 INIT_LIST_HEAD(&connector->user_modes);
721 INIT_LIST_HEAD(&connector->probed_modes);
722 INIT_LIST_HEAD(&connector->modes);
723 connector->edid_blob_ptr = NULL;
724 connector->status = connector_status_unknown;
726 list_add_tail(&connector->head, &dev->mode_config.connector_list);
727 dev->mode_config.num_connector++;
729 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
730 drm_object_attach_property(&connector->base,
731 dev->mode_config.edid_property,
734 drm_object_attach_property(&connector->base,
735 dev->mode_config.dpms_property, 0);
738 drm_modeset_unlock_all(dev);
742 EXPORT_SYMBOL(drm_connector_init);
745 * drm_connector_cleanup - cleans up an initialised connector
746 * @connector: connector to cleanup
748 * Cleans up the connector but doesn't free the object.
750 void drm_connector_cleanup(struct drm_connector *connector)
752 struct drm_device *dev = connector->dev;
753 struct drm_display_mode *mode, *t;
755 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
756 drm_mode_remove(connector, mode);
758 list_for_each_entry_safe(mode, t, &connector->modes, head)
759 drm_mode_remove(connector, mode);
761 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
762 drm_mode_remove(connector, mode);
764 drm_mode_object_put(dev, &connector->base);
765 list_del(&connector->head);
766 dev->mode_config.num_connector--;
768 EXPORT_SYMBOL(drm_connector_cleanup);
770 void drm_connector_unplug_all(struct drm_device *dev)
772 struct drm_connector *connector;
774 /* taking the mode config mutex ends up in a clash with sysfs */
775 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
776 drm_sysfs_connector_remove(connector);
779 EXPORT_SYMBOL(drm_connector_unplug_all);
781 int drm_encoder_init(struct drm_device *dev,
782 struct drm_encoder *encoder,
783 const struct drm_encoder_funcs *funcs,
788 drm_modeset_lock_all(dev);
790 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
795 encoder->encoder_type = encoder_type;
796 encoder->funcs = funcs;
798 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
799 dev->mode_config.num_encoder++;
802 drm_modeset_unlock_all(dev);
806 EXPORT_SYMBOL(drm_encoder_init);
808 void drm_encoder_cleanup(struct drm_encoder *encoder)
810 struct drm_device *dev = encoder->dev;
811 drm_modeset_lock_all(dev);
812 drm_mode_object_put(dev, &encoder->base);
813 list_del(&encoder->head);
814 dev->mode_config.num_encoder--;
815 drm_modeset_unlock_all(dev);
817 EXPORT_SYMBOL(drm_encoder_cleanup);
819 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
820 unsigned long possible_crtcs,
821 const struct drm_plane_funcs *funcs,
822 const uint32_t *formats, uint32_t format_count,
827 drm_modeset_lock_all(dev);
829 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
833 plane->base.properties = &plane->properties;
835 plane->funcs = funcs;
836 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
838 if (!plane->format_types) {
839 DRM_DEBUG_KMS("out of memory when allocating plane\n");
840 drm_mode_object_put(dev, &plane->base);
845 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
846 plane->format_count = format_count;
847 plane->possible_crtcs = possible_crtcs;
849 /* private planes are not exposed to userspace, but depending on
850 * display hardware, might be convenient to allow sharing programming
851 * for the scanout engine with the crtc implementation.
854 list_add_tail(&plane->head, &dev->mode_config.plane_list);
855 dev->mode_config.num_plane++;
857 INIT_LIST_HEAD(&plane->head);
861 drm_modeset_unlock_all(dev);
865 EXPORT_SYMBOL(drm_plane_init);
867 void drm_plane_cleanup(struct drm_plane *plane)
869 struct drm_device *dev = plane->dev;
871 drm_modeset_lock_all(dev);
872 kfree(plane->format_types);
873 drm_mode_object_put(dev, &plane->base);
874 /* if not added to a list, it must be a private plane */
875 if (!list_empty(&plane->head)) {
876 list_del(&plane->head);
877 dev->mode_config.num_plane--;
879 drm_modeset_unlock_all(dev);
881 EXPORT_SYMBOL(drm_plane_cleanup);
884 * drm_mode_create - create a new display mode
887 * Create a new drm_display_mode, give it an ID, and return it.
890 * Pointer to new mode on success, NULL on error.
892 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
894 struct drm_display_mode *nmode;
896 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
900 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
907 EXPORT_SYMBOL(drm_mode_create);
910 * drm_mode_destroy - remove a mode
912 * @mode: mode to remove
914 * Free @mode's unique identifier, then free it.
916 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
921 drm_mode_object_put(dev, &mode->base);
925 EXPORT_SYMBOL(drm_mode_destroy);
927 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
929 struct drm_property *edid;
930 struct drm_property *dpms;
933 * Standard properties (apply to all connectors)
935 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
936 DRM_MODE_PROP_IMMUTABLE,
938 dev->mode_config.edid_property = edid;
940 dpms = drm_property_create_enum(dev, 0,
941 "DPMS", drm_dpms_enum_list,
942 ARRAY_SIZE(drm_dpms_enum_list));
943 dev->mode_config.dpms_property = dpms;
949 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
952 * Called by a driver the first time a DVI-I connector is made.
954 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
956 struct drm_property *dvi_i_selector;
957 struct drm_property *dvi_i_subconnector;
959 if (dev->mode_config.dvi_i_select_subconnector_property)
963 drm_property_create_enum(dev, 0,
964 "select subconnector",
965 drm_dvi_i_select_enum_list,
966 ARRAY_SIZE(drm_dvi_i_select_enum_list));
967 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
969 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
971 drm_dvi_i_subconnector_enum_list,
972 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
973 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
977 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
980 * drm_create_tv_properties - create TV specific connector properties
982 * @num_modes: number of different TV formats (modes) supported
983 * @modes: array of pointers to strings containing name of each format
985 * Called by a driver's TV initialization routine, this function creates
986 * the TV specific connector properties for a given device. Caller is
987 * responsible for allocating a list of format names and passing them to
990 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
993 struct drm_property *tv_selector;
994 struct drm_property *tv_subconnector;
997 if (dev->mode_config.tv_select_subconnector_property)
1001 * Basic connector properties
1003 tv_selector = drm_property_create_enum(dev, 0,
1004 "select subconnector",
1005 drm_tv_select_enum_list,
1006 ARRAY_SIZE(drm_tv_select_enum_list));
1007 dev->mode_config.tv_select_subconnector_property = tv_selector;
1010 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1012 drm_tv_subconnector_enum_list,
1013 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1014 dev->mode_config.tv_subconnector_property = tv_subconnector;
1017 * Other, TV specific properties: margins & TV modes.
1019 dev->mode_config.tv_left_margin_property =
1020 drm_property_create_range(dev, 0, "left margin", 0, 100);
1022 dev->mode_config.tv_right_margin_property =
1023 drm_property_create_range(dev, 0, "right margin", 0, 100);
1025 dev->mode_config.tv_top_margin_property =
1026 drm_property_create_range(dev, 0, "top margin", 0, 100);
1028 dev->mode_config.tv_bottom_margin_property =
1029 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1031 dev->mode_config.tv_mode_property =
1032 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1034 for (i = 0; i < num_modes; i++)
1035 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1038 dev->mode_config.tv_brightness_property =
1039 drm_property_create_range(dev, 0, "brightness", 0, 100);
1041 dev->mode_config.tv_contrast_property =
1042 drm_property_create_range(dev, 0, "contrast", 0, 100);
1044 dev->mode_config.tv_flicker_reduction_property =
1045 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1047 dev->mode_config.tv_overscan_property =
1048 drm_property_create_range(dev, 0, "overscan", 0, 100);
1050 dev->mode_config.tv_saturation_property =
1051 drm_property_create_range(dev, 0, "saturation", 0, 100);
1053 dev->mode_config.tv_hue_property =
1054 drm_property_create_range(dev, 0, "hue", 0, 100);
1058 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1061 * drm_mode_create_scaling_mode_property - create scaling mode property
1064 * Called by a driver the first time it's needed, must be attached to desired
1067 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1069 struct drm_property *scaling_mode;
1071 if (dev->mode_config.scaling_mode_property)
1075 drm_property_create_enum(dev, 0, "scaling mode",
1076 drm_scaling_mode_enum_list,
1077 ARRAY_SIZE(drm_scaling_mode_enum_list));
1079 dev->mode_config.scaling_mode_property = scaling_mode;
1083 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1086 * drm_mode_create_dithering_property - create dithering property
1089 * Called by a driver the first time it's needed, must be attached to desired
1092 int drm_mode_create_dithering_property(struct drm_device *dev)
1094 struct drm_property *dithering_mode;
1096 if (dev->mode_config.dithering_mode_property)
1100 drm_property_create_enum(dev, 0, "dithering",
1101 drm_dithering_mode_enum_list,
1102 ARRAY_SIZE(drm_dithering_mode_enum_list));
1103 dev->mode_config.dithering_mode_property = dithering_mode;
1107 EXPORT_SYMBOL(drm_mode_create_dithering_property);
1110 * drm_mode_create_dirty_property - create dirty property
1113 * Called by a driver the first time it's needed, must be attached to desired
1116 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1118 struct drm_property *dirty_info;
1120 if (dev->mode_config.dirty_info_property)
1124 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1126 drm_dirty_info_enum_list,
1127 ARRAY_SIZE(drm_dirty_info_enum_list));
1128 dev->mode_config.dirty_info_property = dirty_info;
1132 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1135 * drm_mode_config_init - initialize DRM mode_configuration structure
1138 * Initialize @dev's mode_config structure, used for tracking the graphics
1139 * configuration of @dev.
1141 * Since this initializes the modeset locks, no locking is possible. Which is no
1142 * problem, since this should happen single threaded at init time. It is the
1143 * driver's problem to ensure this guarantee.
1146 void drm_mode_config_init(struct drm_device *dev)
1148 mutex_init(&dev->mode_config.mutex);
1149 mutex_init(&dev->mode_config.idr_mutex);
1150 mutex_init(&dev->mode_config.fb_lock);
1151 INIT_LIST_HEAD(&dev->mode_config.fb_list);
1152 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1153 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1154 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1155 INIT_LIST_HEAD(&dev->mode_config.property_list);
1156 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1157 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1158 idr_init(&dev->mode_config.crtc_idr);
1160 drm_modeset_lock_all(dev);
1161 drm_mode_create_standard_connector_properties(dev);
1162 drm_modeset_unlock_all(dev);
1164 /* Just to be sure */
1165 dev->mode_config.num_fb = 0;
1166 dev->mode_config.num_connector = 0;
1167 dev->mode_config.num_crtc = 0;
1168 dev->mode_config.num_encoder = 0;
1170 EXPORT_SYMBOL(drm_mode_config_init);
1172 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1174 uint32_t total_objects = 0;
1176 total_objects += dev->mode_config.num_crtc;
1177 total_objects += dev->mode_config.num_connector;
1178 total_objects += dev->mode_config.num_encoder;
1180 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1181 if (!group->id_list)
1184 group->num_crtcs = 0;
1185 group->num_connectors = 0;
1186 group->num_encoders = 0;
1190 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1191 struct drm_mode_group *group)
1193 struct drm_crtc *crtc;
1194 struct drm_encoder *encoder;
1195 struct drm_connector *connector;
1198 if ((ret = drm_mode_group_init(dev, group)))
1201 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1202 group->id_list[group->num_crtcs++] = crtc->base.id;
1204 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1205 group->id_list[group->num_crtcs + group->num_encoders++] =
1208 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1209 group->id_list[group->num_crtcs + group->num_encoders +
1210 group->num_connectors++] = connector->base.id;
1214 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1217 * drm_mode_config_cleanup - free up DRM mode_config info
1220 * Free up all the connectors and CRTCs associated with this DRM device, then
1221 * free up the framebuffers and associated buffer objects.
1223 * Note that since this /should/ happen single-threaded at driver/device
1224 * teardown time, no locking is required. It's the driver's job to ensure that
1225 * this guarantee actually holds true.
1227 * FIXME: cleanup any dangling user buffer objects too
1229 void drm_mode_config_cleanup(struct drm_device *dev)
1231 struct drm_connector *connector, *ot;
1232 struct drm_crtc *crtc, *ct;
1233 struct drm_encoder *encoder, *enct;
1234 struct drm_framebuffer *fb, *fbt;
1235 struct drm_property *property, *pt;
1236 struct drm_plane *plane, *plt;
1238 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1240 encoder->funcs->destroy(encoder);
1243 list_for_each_entry_safe(connector, ot,
1244 &dev->mode_config.connector_list, head) {
1245 connector->funcs->destroy(connector);
1248 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1250 drm_property_destroy(dev, property);
1254 * Single-threaded teardown context, so it's not required to grab the
1255 * fb_lock to protect against concurrent fb_list access. Contrary, it
1256 * would actually deadlock with the drm_framebuffer_cleanup function.
1258 * Also, if there are any framebuffers left, that's a driver leak now,
1259 * so politely WARN about this.
1261 WARN_ON(!list_empty(&dev->mode_config.fb_list));
1262 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1263 drm_framebuffer_remove(fb);
1266 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1268 plane->funcs->destroy(plane);
1271 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1272 crtc->funcs->destroy(crtc);
1275 idr_remove_all(&dev->mode_config.crtc_idr);
1276 idr_destroy(&dev->mode_config.crtc_idr);
1278 EXPORT_SYMBOL(drm_mode_config_cleanup);
1281 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1282 * @out: drm_mode_modeinfo struct to return to the user
1283 * @in: drm_display_mode to use
1285 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1288 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1289 const struct drm_display_mode *in)
1291 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1292 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1293 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1294 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1295 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1296 "timing values too large for mode info\n");
1298 out->clock = in->clock;
1299 out->hdisplay = in->hdisplay;
1300 out->hsync_start = in->hsync_start;
1301 out->hsync_end = in->hsync_end;
1302 out->htotal = in->htotal;
1303 out->hskew = in->hskew;
1304 out->vdisplay = in->vdisplay;
1305 out->vsync_start = in->vsync_start;
1306 out->vsync_end = in->vsync_end;
1307 out->vtotal = in->vtotal;
1308 out->vscan = in->vscan;
1309 out->vrefresh = in->vrefresh;
1310 out->flags = in->flags;
1311 out->type = in->type;
1312 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1313 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1317 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1318 * @out: drm_display_mode to return to the user
1319 * @in: drm_mode_modeinfo to use
1321 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1325 * Zero on success, errno on failure.
1327 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1328 const struct drm_mode_modeinfo *in)
1330 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1333 out->clock = in->clock;
1334 out->hdisplay = in->hdisplay;
1335 out->hsync_start = in->hsync_start;
1336 out->hsync_end = in->hsync_end;
1337 out->htotal = in->htotal;
1338 out->hskew = in->hskew;
1339 out->vdisplay = in->vdisplay;
1340 out->vsync_start = in->vsync_start;
1341 out->vsync_end = in->vsync_end;
1342 out->vtotal = in->vtotal;
1343 out->vscan = in->vscan;
1344 out->vrefresh = in->vrefresh;
1345 out->flags = in->flags;
1346 out->type = in->type;
1347 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1348 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1354 * drm_mode_getresources - get graphics configuration
1355 * @dev: drm device for the ioctl
1356 * @data: data pointer for the ioctl
1357 * @file_priv: drm file for the ioctl call
1359 * Construct a set of configuration description structures and return
1360 * them to the user, including CRTC, connector and framebuffer configuration.
1362 * Called by the user via ioctl.
1365 * Zero on success, errno on failure.
1367 int drm_mode_getresources(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
1370 struct drm_mode_card_res *card_res = data;
1371 struct list_head *lh;
1372 struct drm_framebuffer *fb;
1373 struct drm_connector *connector;
1374 struct drm_crtc *crtc;
1375 struct drm_encoder *encoder;
1377 int connector_count = 0;
1380 int encoder_count = 0;
1382 uint32_t __user *fb_id;
1383 uint32_t __user *crtc_id;
1384 uint32_t __user *connector_id;
1385 uint32_t __user *encoder_id;
1386 struct drm_mode_group *mode_group;
1388 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1392 mutex_lock(&file_priv->fbs_lock);
1394 * For the non-control nodes we need to limit the list of resources
1395 * by IDs in the group list for this node
1397 list_for_each(lh, &file_priv->fbs)
1400 /* handle this in 4 parts */
1402 if (card_res->count_fbs >= fb_count) {
1404 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1405 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1406 if (put_user(fb->base.id, fb_id + copied)) {
1407 mutex_unlock(&file_priv->fbs_lock);
1413 card_res->count_fbs = fb_count;
1414 mutex_unlock(&file_priv->fbs_lock);
1416 drm_modeset_lock_all(dev);
1417 mode_group = &file_priv->master->minor->mode_group;
1418 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1420 list_for_each(lh, &dev->mode_config.crtc_list)
1423 list_for_each(lh, &dev->mode_config.connector_list)
1426 list_for_each(lh, &dev->mode_config.encoder_list)
1430 crtc_count = mode_group->num_crtcs;
1431 connector_count = mode_group->num_connectors;
1432 encoder_count = mode_group->num_encoders;
1435 card_res->max_height = dev->mode_config.max_height;
1436 card_res->min_height = dev->mode_config.min_height;
1437 card_res->max_width = dev->mode_config.max_width;
1438 card_res->min_width = dev->mode_config.min_width;
1441 if (card_res->count_crtcs >= crtc_count) {
1443 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1444 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1445 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1447 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1448 if (put_user(crtc->base.id, crtc_id + copied)) {
1455 for (i = 0; i < mode_group->num_crtcs; i++) {
1456 if (put_user(mode_group->id_list[i],
1457 crtc_id + copied)) {
1465 card_res->count_crtcs = crtc_count;
1468 if (card_res->count_encoders >= encoder_count) {
1470 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1471 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1472 list_for_each_entry(encoder,
1473 &dev->mode_config.encoder_list,
1475 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1476 drm_get_encoder_name(encoder));
1477 if (put_user(encoder->base.id, encoder_id +
1485 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1486 if (put_user(mode_group->id_list[i],
1487 encoder_id + copied)) {
1496 card_res->count_encoders = encoder_count;
1499 if (card_res->count_connectors >= connector_count) {
1501 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1502 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1503 list_for_each_entry(connector,
1504 &dev->mode_config.connector_list,
1506 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1508 drm_get_connector_name(connector));
1509 if (put_user(connector->base.id,
1510 connector_id + copied)) {
1517 int start = mode_group->num_crtcs +
1518 mode_group->num_encoders;
1519 for (i = start; i < start + mode_group->num_connectors; i++) {
1520 if (put_user(mode_group->id_list[i],
1521 connector_id + copied)) {
1529 card_res->count_connectors = connector_count;
1531 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1532 card_res->count_connectors, card_res->count_encoders);
1535 drm_modeset_unlock_all(dev);
1540 * drm_mode_getcrtc - get CRTC configuration
1541 * @dev: drm device for the ioctl
1542 * @data: data pointer for the ioctl
1543 * @file_priv: drm file for the ioctl call
1545 * Construct a CRTC configuration structure to return to the user.
1547 * Called by the user via ioctl.
1550 * Zero on success, errno on failure.
1552 int drm_mode_getcrtc(struct drm_device *dev,
1553 void *data, struct drm_file *file_priv)
1555 struct drm_mode_crtc *crtc_resp = data;
1556 struct drm_crtc *crtc;
1557 struct drm_mode_object *obj;
1560 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1563 drm_modeset_lock_all(dev);
1565 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1566 DRM_MODE_OBJECT_CRTC);
1571 crtc = obj_to_crtc(obj);
1573 crtc_resp->x = crtc->x;
1574 crtc_resp->y = crtc->y;
1575 crtc_resp->gamma_size = crtc->gamma_size;
1577 crtc_resp->fb_id = crtc->fb->base.id;
1579 crtc_resp->fb_id = 0;
1581 if (crtc->enabled) {
1583 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1584 crtc_resp->mode_valid = 1;
1587 crtc_resp->mode_valid = 0;
1591 drm_modeset_unlock_all(dev);
1596 * drm_mode_getconnector - get connector configuration
1597 * @dev: drm device for the ioctl
1598 * @data: data pointer for the ioctl
1599 * @file_priv: drm file for the ioctl call
1601 * Construct a connector configuration structure to return to the user.
1603 * Called by the user via ioctl.
1606 * Zero on success, errno on failure.
1608 int drm_mode_getconnector(struct drm_device *dev, void *data,
1609 struct drm_file *file_priv)
1611 struct drm_mode_get_connector *out_resp = data;
1612 struct drm_mode_object *obj;
1613 struct drm_connector *connector;
1614 struct drm_display_mode *mode;
1616 int props_count = 0;
1617 int encoders_count = 0;
1621 struct drm_mode_modeinfo u_mode;
1622 struct drm_mode_modeinfo __user *mode_ptr;
1623 uint32_t __user *prop_ptr;
1624 uint64_t __user *prop_values;
1625 uint32_t __user *encoder_ptr;
1627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1630 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1632 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1634 mutex_lock(&dev->mode_config.mutex);
1636 obj = drm_mode_object_find(dev, out_resp->connector_id,
1637 DRM_MODE_OBJECT_CONNECTOR);
1642 connector = obj_to_connector(obj);
1644 props_count = connector->properties.count;
1646 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1647 if (connector->encoder_ids[i] != 0) {
1652 if (out_resp->count_modes == 0) {
1653 connector->funcs->fill_modes(connector,
1654 dev->mode_config.max_width,
1655 dev->mode_config.max_height);
1658 /* delayed so we get modes regardless of pre-fill_modes state */
1659 list_for_each_entry(mode, &connector->modes, head)
1662 out_resp->connector_id = connector->base.id;
1663 out_resp->connector_type = connector->connector_type;
1664 out_resp->connector_type_id = connector->connector_type_id;
1665 out_resp->mm_width = connector->display_info.width_mm;
1666 out_resp->mm_height = connector->display_info.height_mm;
1667 out_resp->subpixel = connector->display_info.subpixel_order;
1668 out_resp->connection = connector->status;
1669 if (connector->encoder)
1670 out_resp->encoder_id = connector->encoder->base.id;
1672 out_resp->encoder_id = 0;
1675 * This ioctl is called twice, once to determine how much space is
1676 * needed, and the 2nd time to fill it.
1678 if ((out_resp->count_modes >= mode_count) && mode_count) {
1680 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1681 list_for_each_entry(mode, &connector->modes, head) {
1682 drm_crtc_convert_to_umode(&u_mode, mode);
1683 if (copy_to_user(mode_ptr + copied,
1684 &u_mode, sizeof(u_mode))) {
1691 out_resp->count_modes = mode_count;
1693 if ((out_resp->count_props >= props_count) && props_count) {
1695 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1696 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1697 for (i = 0; i < connector->properties.count; i++) {
1698 if (put_user(connector->properties.ids[i],
1699 prop_ptr + copied)) {
1704 if (put_user(connector->properties.values[i],
1705 prop_values + copied)) {
1712 out_resp->count_props = props_count;
1714 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1716 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1717 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1718 if (connector->encoder_ids[i] != 0) {
1719 if (put_user(connector->encoder_ids[i],
1720 encoder_ptr + copied)) {
1728 out_resp->count_encoders = encoders_count;
1731 mutex_unlock(&dev->mode_config.mutex);
1736 int drm_mode_getencoder(struct drm_device *dev, void *data,
1737 struct drm_file *file_priv)
1739 struct drm_mode_get_encoder *enc_resp = data;
1740 struct drm_mode_object *obj;
1741 struct drm_encoder *encoder;
1744 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1747 drm_modeset_lock_all(dev);
1748 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1749 DRM_MODE_OBJECT_ENCODER);
1754 encoder = obj_to_encoder(obj);
1757 enc_resp->crtc_id = encoder->crtc->base.id;
1759 enc_resp->crtc_id = 0;
1760 enc_resp->encoder_type = encoder->encoder_type;
1761 enc_resp->encoder_id = encoder->base.id;
1762 enc_resp->possible_crtcs = encoder->possible_crtcs;
1763 enc_resp->possible_clones = encoder->possible_clones;
1766 drm_modeset_unlock_all(dev);
1771 * drm_mode_getplane_res - get plane info
1774 * @file_priv: DRM file info
1776 * Return an plane count and set of IDs.
1778 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1779 struct drm_file *file_priv)
1781 struct drm_mode_get_plane_res *plane_resp = data;
1782 struct drm_mode_config *config;
1783 struct drm_plane *plane;
1784 uint32_t __user *plane_ptr;
1785 int copied = 0, ret = 0;
1787 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1790 drm_modeset_lock_all(dev);
1791 config = &dev->mode_config;
1794 * This ioctl is called twice, once to determine how much space is
1795 * needed, and the 2nd time to fill it.
1797 if (config->num_plane &&
1798 (plane_resp->count_planes >= config->num_plane)) {
1799 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1801 list_for_each_entry(plane, &config->plane_list, head) {
1802 if (put_user(plane->base.id, plane_ptr + copied)) {
1809 plane_resp->count_planes = config->num_plane;
1812 drm_modeset_unlock_all(dev);
1817 * drm_mode_getplane - get plane info
1820 * @file_priv: DRM file info
1822 * Return plane info, including formats supported, gamma size, any
1825 int drm_mode_getplane(struct drm_device *dev, void *data,
1826 struct drm_file *file_priv)
1828 struct drm_mode_get_plane *plane_resp = data;
1829 struct drm_mode_object *obj;
1830 struct drm_plane *plane;
1831 uint32_t __user *format_ptr;
1834 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1837 drm_modeset_lock_all(dev);
1838 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1839 DRM_MODE_OBJECT_PLANE);
1844 plane = obj_to_plane(obj);
1847 plane_resp->crtc_id = plane->crtc->base.id;
1849 plane_resp->crtc_id = 0;
1852 plane_resp->fb_id = plane->fb->base.id;
1854 plane_resp->fb_id = 0;
1856 plane_resp->plane_id = plane->base.id;
1857 plane_resp->possible_crtcs = plane->possible_crtcs;
1858 plane_resp->gamma_size = plane->gamma_size;
1861 * This ioctl is called twice, once to determine how much space is
1862 * needed, and the 2nd time to fill it.
1864 if (plane->format_count &&
1865 (plane_resp->count_format_types >= plane->format_count)) {
1866 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1867 if (copy_to_user(format_ptr,
1868 plane->format_types,
1869 sizeof(uint32_t) * plane->format_count)) {
1874 plane_resp->count_format_types = plane->format_count;
1877 drm_modeset_unlock_all(dev);
1882 * drm_mode_setplane - set up or tear down an plane
1884 * @data: ioctl data*
1885 * @file_priv: DRM file info
1887 * Set plane info, including placement, fb, scaling, and other factors.
1888 * Or pass a NULL fb to disable.
1890 int drm_mode_setplane(struct drm_device *dev, void *data,
1891 struct drm_file *file_priv)
1893 struct drm_mode_set_plane *plane_req = data;
1894 struct drm_mode_object *obj;
1895 struct drm_plane *plane;
1896 struct drm_crtc *crtc;
1897 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1899 unsigned int fb_width, fb_height;
1902 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1906 * First, find the plane, crtc, and fb objects. If not available,
1907 * we don't bother to call the driver.
1909 obj = drm_mode_object_find(dev, plane_req->plane_id,
1910 DRM_MODE_OBJECT_PLANE);
1912 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1913 plane_req->plane_id);
1916 plane = obj_to_plane(obj);
1918 /* No fb means shut it down */
1919 if (!plane_req->fb_id) {
1920 drm_modeset_lock_all(dev);
1922 plane->funcs->disable_plane(plane);
1925 drm_modeset_unlock_all(dev);
1929 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1930 DRM_MODE_OBJECT_CRTC);
1932 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1933 plane_req->crtc_id);
1937 crtc = obj_to_crtc(obj);
1939 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1941 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1947 /* Check whether this plane supports the fb pixel format. */
1948 for (i = 0; i < plane->format_count; i++)
1949 if (fb->pixel_format == plane->format_types[i])
1951 if (i == plane->format_count) {
1952 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1957 fb_width = fb->width << 16;
1958 fb_height = fb->height << 16;
1960 /* Make sure source coordinates are inside the fb. */
1961 if (plane_req->src_w > fb_width ||
1962 plane_req->src_x > fb_width - plane_req->src_w ||
1963 plane_req->src_h > fb_height ||
1964 plane_req->src_y > fb_height - plane_req->src_h) {
1965 DRM_DEBUG_KMS("Invalid source coordinates "
1966 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1967 plane_req->src_w >> 16,
1968 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1969 plane_req->src_h >> 16,
1970 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1971 plane_req->src_x >> 16,
1972 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1973 plane_req->src_y >> 16,
1974 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1979 /* Give drivers some help against integer overflows */
1980 if (plane_req->crtc_w > INT_MAX ||
1981 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1982 plane_req->crtc_h > INT_MAX ||
1983 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1984 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1985 plane_req->crtc_w, plane_req->crtc_h,
1986 plane_req->crtc_x, plane_req->crtc_y);
1991 drm_modeset_lock_all(dev);
1992 ret = plane->funcs->update_plane(plane, crtc, fb,
1993 plane_req->crtc_x, plane_req->crtc_y,
1994 plane_req->crtc_w, plane_req->crtc_h,
1995 plane_req->src_x, plane_req->src_y,
1996 plane_req->src_w, plane_req->src_h);
2003 drm_modeset_unlock_all(dev);
2007 drm_framebuffer_unreference(fb);
2009 drm_framebuffer_unreference(old_fb);
2015 * drm_mode_set_config_internal - helper to call ->set_config
2016 * @set: modeset config to set
2018 * This is a little helper to wrap internal calls to the ->set_config driver
2019 * interface. The only thing it adds is correct refcounting dance.
2021 int drm_mode_set_config_internal(struct drm_mode_set *set)
2023 struct drm_crtc *crtc = set->crtc;
2024 struct drm_framebuffer *fb, *old_fb;
2030 ret = crtc->funcs->set_config(set);
2033 drm_framebuffer_unreference(old_fb);
2035 drm_framebuffer_reference(fb);
2040 EXPORT_SYMBOL(drm_mode_set_config_internal);
2043 * drm_mode_setcrtc - set CRTC configuration
2044 * @dev: drm device for the ioctl
2045 * @data: data pointer for the ioctl
2046 * @file_priv: drm file for the ioctl call
2048 * Build a new CRTC configuration based on user request.
2050 * Called by the user via ioctl.
2053 * Zero on success, errno on failure.
2055 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2056 struct drm_file *file_priv)
2058 struct drm_mode_config *config = &dev->mode_config;
2059 struct drm_mode_crtc *crtc_req = data;
2060 struct drm_mode_object *obj;
2061 struct drm_crtc *crtc;
2062 struct drm_connector **connector_set = NULL, *connector;
2063 struct drm_framebuffer *fb = NULL;
2064 struct drm_display_mode *mode = NULL;
2065 struct drm_mode_set set;
2066 uint32_t __user *set_connectors_ptr;
2070 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2073 /* For some reason crtc x/y offsets are signed internally. */
2074 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2077 drm_modeset_lock_all(dev);
2078 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2079 DRM_MODE_OBJECT_CRTC);
2081 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2085 crtc = obj_to_crtc(obj);
2086 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2088 if (crtc_req->mode_valid) {
2089 int hdisplay, vdisplay;
2090 /* If we have a mode we need a framebuffer. */
2091 /* If we pass -1, set the mode with the currently bound fb */
2092 if (crtc_req->fb_id == -1) {
2094 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2099 /* Make refcounting symmetric with the lookup path. */
2100 drm_framebuffer_reference(fb);
2102 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2104 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2111 mode = drm_mode_create(dev);
2117 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2119 DRM_DEBUG_KMS("Invalid mode\n");
2123 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2125 hdisplay = mode->hdisplay;
2126 vdisplay = mode->vdisplay;
2128 if (crtc->invert_dimensions)
2129 swap(hdisplay, vdisplay);
2131 if (hdisplay > fb->width ||
2132 vdisplay > fb->height ||
2133 crtc_req->x > fb->width - hdisplay ||
2134 crtc_req->y > fb->height - vdisplay) {
2135 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2136 fb->width, fb->height,
2137 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2138 crtc->invert_dimensions ? " (inverted)" : "");
2144 if (crtc_req->count_connectors == 0 && mode) {
2145 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2150 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2151 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2152 crtc_req->count_connectors);
2157 if (crtc_req->count_connectors > 0) {
2160 /* Avoid unbounded kernel memory allocation */
2161 if (crtc_req->count_connectors > config->num_connector) {
2166 connector_set = kmalloc(crtc_req->count_connectors *
2167 sizeof(struct drm_connector *),
2169 if (!connector_set) {
2174 for (i = 0; i < crtc_req->count_connectors; i++) {
2175 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2176 if (get_user(out_id, &set_connectors_ptr[i])) {
2181 obj = drm_mode_object_find(dev, out_id,
2182 DRM_MODE_OBJECT_CONNECTOR);
2184 DRM_DEBUG_KMS("Connector id %d unknown\n",
2189 connector = obj_to_connector(obj);
2190 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2192 drm_get_connector_name(connector));
2194 connector_set[i] = connector;
2199 set.x = crtc_req->x;
2200 set.y = crtc_req->y;
2202 set.connectors = connector_set;
2203 set.num_connectors = crtc_req->count_connectors;
2205 ret = drm_mode_set_config_internal(&set);
2209 drm_framebuffer_unreference(fb);
2211 kfree(connector_set);
2212 drm_mode_destroy(dev, mode);
2213 drm_modeset_unlock_all(dev);
2217 int drm_mode_cursor_ioctl(struct drm_device *dev,
2218 void *data, struct drm_file *file_priv)
2220 struct drm_mode_cursor *req = data;
2221 struct drm_mode_object *obj;
2222 struct drm_crtc *crtc;
2225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2228 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2231 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2233 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2236 crtc = obj_to_crtc(obj);
2238 mutex_lock(&crtc->mutex);
2239 if (req->flags & DRM_MODE_CURSOR_BO) {
2240 if (!crtc->funcs->cursor_set) {
2244 /* Turns off the cursor if handle is 0 */
2245 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2246 req->width, req->height);
2249 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2250 if (crtc->funcs->cursor_move) {
2251 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2258 mutex_unlock(&crtc->mutex);
2263 /* Original addfb only supported RGB formats, so figure out which one */
2264 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2270 fmt = DRM_FORMAT_C8;
2274 fmt = DRM_FORMAT_XRGB1555;
2276 fmt = DRM_FORMAT_RGB565;
2279 fmt = DRM_FORMAT_RGB888;
2283 fmt = DRM_FORMAT_XRGB8888;
2284 else if (depth == 30)
2285 fmt = DRM_FORMAT_XRGB2101010;
2287 fmt = DRM_FORMAT_ARGB8888;
2290 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2291 fmt = DRM_FORMAT_XRGB8888;
2297 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2300 * drm_mode_addfb - add an FB to the graphics configuration
2301 * @dev: drm device for the ioctl
2302 * @data: data pointer for the ioctl
2303 * @file_priv: drm file for the ioctl call
2305 * Add a new FB to the specified CRTC, given a user request.
2307 * Called by the user via ioctl.
2310 * Zero on success, errno on failure.
2312 int drm_mode_addfb(struct drm_device *dev,
2313 void *data, struct drm_file *file_priv)
2315 struct drm_mode_fb_cmd *or = data;
2316 struct drm_mode_fb_cmd2 r = {};
2317 struct drm_mode_config *config = &dev->mode_config;
2318 struct drm_framebuffer *fb;
2321 /* Use new struct with format internally */
2322 r.fb_id = or->fb_id;
2323 r.width = or->width;
2324 r.height = or->height;
2325 r.pitches[0] = or->pitch;
2326 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2327 r.handles[0] = or->handle;
2329 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2332 if ((config->min_width > r.width) || (r.width > config->max_width))
2335 if ((config->min_height > r.height) || (r.height > config->max_height))
2338 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2340 DRM_DEBUG_KMS("could not create framebuffer\n");
2341 drm_modeset_unlock_all(dev);
2345 mutex_lock(&file_priv->fbs_lock);
2346 or->fb_id = fb->base.id;
2347 list_add(&fb->filp_head, &file_priv->fbs);
2348 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2349 mutex_unlock(&file_priv->fbs_lock);
2354 static int format_check(const struct drm_mode_fb_cmd2 *r)
2356 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2360 case DRM_FORMAT_RGB332:
2361 case DRM_FORMAT_BGR233:
2362 case DRM_FORMAT_XRGB4444:
2363 case DRM_FORMAT_XBGR4444:
2364 case DRM_FORMAT_RGBX4444:
2365 case DRM_FORMAT_BGRX4444:
2366 case DRM_FORMAT_ARGB4444:
2367 case DRM_FORMAT_ABGR4444:
2368 case DRM_FORMAT_RGBA4444:
2369 case DRM_FORMAT_BGRA4444:
2370 case DRM_FORMAT_XRGB1555:
2371 case DRM_FORMAT_XBGR1555:
2372 case DRM_FORMAT_RGBX5551:
2373 case DRM_FORMAT_BGRX5551:
2374 case DRM_FORMAT_ARGB1555:
2375 case DRM_FORMAT_ABGR1555:
2376 case DRM_FORMAT_RGBA5551:
2377 case DRM_FORMAT_BGRA5551:
2378 case DRM_FORMAT_RGB565:
2379 case DRM_FORMAT_BGR565:
2380 case DRM_FORMAT_RGB888:
2381 case DRM_FORMAT_BGR888:
2382 case DRM_FORMAT_XRGB8888:
2383 case DRM_FORMAT_XBGR8888:
2384 case DRM_FORMAT_RGBX8888:
2385 case DRM_FORMAT_BGRX8888:
2386 case DRM_FORMAT_ARGB8888:
2387 case DRM_FORMAT_ABGR8888:
2388 case DRM_FORMAT_RGBA8888:
2389 case DRM_FORMAT_BGRA8888:
2390 case DRM_FORMAT_XRGB2101010:
2391 case DRM_FORMAT_XBGR2101010:
2392 case DRM_FORMAT_RGBX1010102:
2393 case DRM_FORMAT_BGRX1010102:
2394 case DRM_FORMAT_ARGB2101010:
2395 case DRM_FORMAT_ABGR2101010:
2396 case DRM_FORMAT_RGBA1010102:
2397 case DRM_FORMAT_BGRA1010102:
2398 case DRM_FORMAT_YUYV:
2399 case DRM_FORMAT_YVYU:
2400 case DRM_FORMAT_UYVY:
2401 case DRM_FORMAT_VYUY:
2402 case DRM_FORMAT_AYUV:
2403 case DRM_FORMAT_NV12:
2404 case DRM_FORMAT_NV21:
2405 case DRM_FORMAT_NV16:
2406 case DRM_FORMAT_NV61:
2407 case DRM_FORMAT_NV24:
2408 case DRM_FORMAT_NV42:
2409 case DRM_FORMAT_YUV410:
2410 case DRM_FORMAT_YVU410:
2411 case DRM_FORMAT_YUV411:
2412 case DRM_FORMAT_YVU411:
2413 case DRM_FORMAT_YUV420:
2414 case DRM_FORMAT_YVU420:
2415 case DRM_FORMAT_YUV422:
2416 case DRM_FORMAT_YVU422:
2417 case DRM_FORMAT_YUV444:
2418 case DRM_FORMAT_YVU444:
2425 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2427 int ret, hsub, vsub, num_planes, i;
2429 ret = format_check(r);
2431 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2435 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2436 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2437 num_planes = drm_format_num_planes(r->pixel_format);
2439 if (r->width == 0 || r->width % hsub) {
2440 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2444 if (r->height == 0 || r->height % vsub) {
2445 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2449 for (i = 0; i < num_planes; i++) {
2450 unsigned int width = r->width / (i != 0 ? hsub : 1);
2451 unsigned int height = r->height / (i != 0 ? vsub : 1);
2452 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2454 if (!r->handles[i]) {
2455 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2459 if ((uint64_t) width * cpp > UINT_MAX)
2462 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2465 if (r->pitches[i] < width * cpp) {
2466 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2475 * drm_mode_addfb2 - add an FB to the graphics configuration
2476 * @dev: drm device for the ioctl
2477 * @data: data pointer for the ioctl
2478 * @file_priv: drm file for the ioctl call
2480 * Add a new FB to the specified CRTC, given a user request with format.
2482 * Called by the user via ioctl.
2485 * Zero on success, errno on failure.
2487 int drm_mode_addfb2(struct drm_device *dev,
2488 void *data, struct drm_file *file_priv)
2490 struct drm_mode_fb_cmd2 *r = data;
2491 struct drm_mode_config *config = &dev->mode_config;
2492 struct drm_framebuffer *fb;
2495 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2498 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2499 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2503 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2504 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2505 r->width, config->min_width, config->max_width);
2508 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2509 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2510 r->height, config->min_height, config->max_height);
2514 ret = framebuffer_check(r);
2518 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2520 DRM_DEBUG_KMS("could not create framebuffer\n");
2521 drm_modeset_unlock_all(dev);
2525 mutex_lock(&file_priv->fbs_lock);
2526 r->fb_id = fb->base.id;
2527 list_add(&fb->filp_head, &file_priv->fbs);
2528 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2529 mutex_unlock(&file_priv->fbs_lock);
2536 * drm_mode_rmfb - remove an FB from the configuration
2537 * @dev: drm device for the ioctl
2538 * @data: data pointer for the ioctl
2539 * @file_priv: drm file for the ioctl call
2541 * Remove the FB specified by the user.
2543 * Called by the user via ioctl.
2546 * Zero on success, errno on failure.
2548 int drm_mode_rmfb(struct drm_device *dev,
2549 void *data, struct drm_file *file_priv)
2551 struct drm_framebuffer *fb = NULL;
2552 struct drm_framebuffer *fbl = NULL;
2553 uint32_t *id = data;
2556 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2559 mutex_lock(&file_priv->fbs_lock);
2560 mutex_lock(&dev->mode_config.fb_lock);
2561 fb = __drm_framebuffer_lookup(dev, *id);
2565 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2571 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2572 __drm_framebuffer_unregister(dev, fb);
2574 list_del_init(&fb->filp_head);
2575 mutex_unlock(&dev->mode_config.fb_lock);
2576 mutex_unlock(&file_priv->fbs_lock);
2578 drm_framebuffer_remove(fb);
2583 mutex_unlock(&dev->mode_config.fb_lock);
2584 mutex_unlock(&file_priv->fbs_lock);
2590 * drm_mode_getfb - get FB info
2591 * @dev: drm device for the ioctl
2592 * @data: data pointer for the ioctl
2593 * @file_priv: drm file for the ioctl call
2595 * Lookup the FB given its ID and return info about it.
2597 * Called by the user via ioctl.
2600 * Zero on success, errno on failure.
2602 int drm_mode_getfb(struct drm_device *dev,
2603 void *data, struct drm_file *file_priv)
2605 struct drm_mode_fb_cmd *r = data;
2606 struct drm_framebuffer *fb;
2609 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2612 fb = drm_framebuffer_lookup(dev, r->fb_id);
2616 r->height = fb->height;
2617 r->width = fb->width;
2618 r->depth = fb->depth;
2619 r->bpp = fb->bits_per_pixel;
2620 r->pitch = fb->pitches[0];
2621 if (fb->funcs->create_handle)
2622 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2626 drm_framebuffer_unreference(fb);
2631 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2632 void *data, struct drm_file *file_priv)
2634 struct drm_clip_rect __user *clips_ptr;
2635 struct drm_clip_rect *clips = NULL;
2636 struct drm_mode_fb_dirty_cmd *r = data;
2637 struct drm_framebuffer *fb;
2642 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2645 fb = drm_framebuffer_lookup(dev, r->fb_id);
2649 num_clips = r->num_clips;
2650 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2652 if (!num_clips != !clips_ptr) {
2657 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2659 /* If userspace annotates copy, clips must come in pairs */
2660 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2665 if (num_clips && clips_ptr) {
2666 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2670 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2676 ret = copy_from_user(clips, clips_ptr,
2677 num_clips * sizeof(*clips));
2684 if (fb->funcs->dirty) {
2685 drm_modeset_lock_all(dev);
2686 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2688 drm_modeset_unlock_all(dev);
2696 drm_framebuffer_unreference(fb);
2703 * drm_fb_release - remove and free the FBs on this file
2704 * @priv: drm file for the ioctl
2706 * Destroy all the FBs associated with @filp.
2708 * Called by the user via ioctl.
2711 * Zero on success, errno on failure.
2713 void drm_fb_release(struct drm_file *priv)
2715 struct drm_device *dev = priv->minor->dev;
2716 struct drm_framebuffer *fb, *tfb;
2718 mutex_lock(&priv->fbs_lock);
2719 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2721 mutex_lock(&dev->mode_config.fb_lock);
2722 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2723 __drm_framebuffer_unregister(dev, fb);
2724 mutex_unlock(&dev->mode_config.fb_lock);
2726 list_del_init(&fb->filp_head);
2728 /* This will also drop the fpriv->fbs reference. */
2729 drm_framebuffer_remove(fb);
2731 mutex_unlock(&priv->fbs_lock);
2735 * drm_mode_attachmode - add a mode to the user mode list
2737 * @connector: connector to add the mode to
2738 * @mode: mode to add
2740 * Add @mode to @connector's user mode list.
2742 static void drm_mode_attachmode(struct drm_device *dev,
2743 struct drm_connector *connector,
2744 struct drm_display_mode *mode)
2746 list_add_tail(&mode->head, &connector->user_modes);
2749 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2750 const struct drm_display_mode *mode)
2752 struct drm_connector *connector;
2754 struct drm_display_mode *dup_mode, *next;
2757 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2758 if (!connector->encoder)
2760 if (connector->encoder->crtc == crtc) {
2761 dup_mode = drm_mode_duplicate(dev, mode);
2766 list_add_tail(&dup_mode->head, &list);
2770 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2771 if (!connector->encoder)
2773 if (connector->encoder->crtc == crtc)
2774 list_move_tail(list.next, &connector->user_modes);
2777 WARN_ON(!list_empty(&list));
2780 list_for_each_entry_safe(dup_mode, next, &list, head)
2781 drm_mode_destroy(dev, dup_mode);
2785 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2787 static int drm_mode_detachmode(struct drm_device *dev,
2788 struct drm_connector *connector,
2789 struct drm_display_mode *mode)
2793 struct drm_display_mode *match_mode, *t;
2795 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2796 if (drm_mode_equal(match_mode, mode)) {
2797 list_del(&match_mode->head);
2798 drm_mode_destroy(dev, match_mode);
2810 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2812 struct drm_connector *connector;
2814 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2815 drm_mode_detachmode(dev, connector, mode);
2819 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2822 * drm_fb_attachmode - Attach a user mode to an connector
2823 * @dev: drm device for the ioctl
2824 * @data: data pointer for the ioctl
2825 * @file_priv: drm file for the ioctl call
2827 * This attaches a user specified mode to an connector.
2828 * Called by the user via ioctl.
2831 * Zero on success, errno on failure.
2833 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2834 void *data, struct drm_file *file_priv)
2836 struct drm_mode_mode_cmd *mode_cmd = data;
2837 struct drm_connector *connector;
2838 struct drm_display_mode *mode;
2839 struct drm_mode_object *obj;
2840 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2843 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2846 drm_modeset_lock_all(dev);
2848 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2853 connector = obj_to_connector(obj);
2855 mode = drm_mode_create(dev);
2861 ret = drm_crtc_convert_umode(mode, umode);
2863 DRM_DEBUG_KMS("Invalid mode\n");
2864 drm_mode_destroy(dev, mode);
2868 drm_mode_attachmode(dev, connector, mode);
2870 drm_modeset_unlock_all(dev);
2876 * drm_fb_detachmode - Detach a user specified mode from an connector
2877 * @dev: drm device for the ioctl
2878 * @data: data pointer for the ioctl
2879 * @file_priv: drm file for the ioctl call
2881 * Called by the user via ioctl.
2884 * Zero on success, errno on failure.
2886 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2887 void *data, struct drm_file *file_priv)
2889 struct drm_mode_object *obj;
2890 struct drm_mode_mode_cmd *mode_cmd = data;
2891 struct drm_connector *connector;
2892 struct drm_display_mode mode;
2893 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2899 drm_modeset_lock_all(dev);
2901 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2906 connector = obj_to_connector(obj);
2908 ret = drm_crtc_convert_umode(&mode, umode);
2910 DRM_DEBUG_KMS("Invalid mode\n");
2914 ret = drm_mode_detachmode(dev, connector, &mode);
2916 drm_modeset_unlock_all(dev);
2920 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2921 const char *name, int num_values)
2923 struct drm_property *property = NULL;
2926 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2931 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2932 if (!property->values)
2936 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2940 property->flags = flags;
2941 property->num_values = num_values;
2942 INIT_LIST_HEAD(&property->enum_blob_list);
2945 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2946 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2949 list_add_tail(&property->head, &dev->mode_config.property_list);
2952 kfree(property->values);
2956 EXPORT_SYMBOL(drm_property_create);
2958 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2960 const struct drm_prop_enum_list *props,
2963 struct drm_property *property;
2966 flags |= DRM_MODE_PROP_ENUM;
2968 property = drm_property_create(dev, flags, name, num_values);
2972 for (i = 0; i < num_values; i++) {
2973 ret = drm_property_add_enum(property, i,
2977 drm_property_destroy(dev, property);
2984 EXPORT_SYMBOL(drm_property_create_enum);
2986 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2987 int flags, const char *name,
2988 const struct drm_prop_enum_list *props,
2991 struct drm_property *property;
2994 flags |= DRM_MODE_PROP_BITMASK;
2996 property = drm_property_create(dev, flags, name, num_values);
3000 for (i = 0; i < num_values; i++) {
3001 ret = drm_property_add_enum(property, i,
3005 drm_property_destroy(dev, property);
3012 EXPORT_SYMBOL(drm_property_create_bitmask);
3014 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3016 uint64_t min, uint64_t max)
3018 struct drm_property *property;
3020 flags |= DRM_MODE_PROP_RANGE;
3022 property = drm_property_create(dev, flags, name, 2);
3026 property->values[0] = min;
3027 property->values[1] = max;
3031 EXPORT_SYMBOL(drm_property_create_range);
3033 int drm_property_add_enum(struct drm_property *property, int index,
3034 uint64_t value, const char *name)
3036 struct drm_property_enum *prop_enum;
3038 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3042 * Bitmask enum properties have the additional constraint of values
3045 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
3048 if (!list_empty(&property->enum_blob_list)) {
3049 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3050 if (prop_enum->value == value) {
3051 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3052 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3058 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3062 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3063 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3064 prop_enum->value = value;
3066 property->values[index] = value;
3067 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3070 EXPORT_SYMBOL(drm_property_add_enum);
3072 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3074 struct drm_property_enum *prop_enum, *pt;
3076 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3077 list_del(&prop_enum->head);
3081 if (property->num_values)
3082 kfree(property->values);
3083 drm_mode_object_put(dev, &property->base);
3084 list_del(&property->head);
3087 EXPORT_SYMBOL(drm_property_destroy);
3089 void drm_object_attach_property(struct drm_mode_object *obj,
3090 struct drm_property *property,
3093 int count = obj->properties->count;
3095 if (count == DRM_OBJECT_MAX_PROPERTY) {
3096 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3097 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3098 "you see this message on the same object type.\n",
3103 obj->properties->ids[count] = property->base.id;
3104 obj->properties->values[count] = init_val;
3105 obj->properties->count++;
3107 EXPORT_SYMBOL(drm_object_attach_property);
3109 int drm_object_property_set_value(struct drm_mode_object *obj,
3110 struct drm_property *property, uint64_t val)
3114 for (i = 0; i < obj->properties->count; i++) {
3115 if (obj->properties->ids[i] == property->base.id) {
3116 obj->properties->values[i] = val;
3123 EXPORT_SYMBOL(drm_object_property_set_value);
3125 int drm_object_property_get_value(struct drm_mode_object *obj,
3126 struct drm_property *property, uint64_t *val)
3130 for (i = 0; i < obj->properties->count; i++) {
3131 if (obj->properties->ids[i] == property->base.id) {
3132 *val = obj->properties->values[i];
3139 EXPORT_SYMBOL(drm_object_property_get_value);
3141 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3142 void *data, struct drm_file *file_priv)
3144 struct drm_mode_object *obj;
3145 struct drm_mode_get_property *out_resp = data;
3146 struct drm_property *property;
3149 int value_count = 0;
3152 struct drm_property_enum *prop_enum;
3153 struct drm_mode_property_enum __user *enum_ptr;
3154 struct drm_property_blob *prop_blob;
3155 uint32_t __user *blob_id_ptr;
3156 uint64_t __user *values_ptr;
3157 uint32_t __user *blob_length_ptr;
3159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3162 drm_modeset_lock_all(dev);
3163 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3168 property = obj_to_property(obj);
3170 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3171 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3173 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3174 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3178 value_count = property->num_values;
3180 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3181 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3182 out_resp->flags = property->flags;
3184 if ((out_resp->count_values >= value_count) && value_count) {
3185 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3186 for (i = 0; i < value_count; i++) {
3187 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3193 out_resp->count_values = value_count;
3195 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3196 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3198 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3199 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3201 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3206 if (copy_to_user(&enum_ptr[copied].name,
3207 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3214 out_resp->count_enum_blobs = enum_count;
3217 if (property->flags & DRM_MODE_PROP_BLOB) {
3218 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3220 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3221 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3223 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3224 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3229 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3237 out_resp->count_enum_blobs = blob_count;
3240 drm_modeset_unlock_all(dev);
3244 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3247 struct drm_property_blob *blob;
3250 if (!length || !data)
3253 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3257 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3263 blob->length = length;
3265 memcpy(blob->data, data, length);
3267 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3271 static void drm_property_destroy_blob(struct drm_device *dev,
3272 struct drm_property_blob *blob)
3274 drm_mode_object_put(dev, &blob->base);
3275 list_del(&blob->head);
3279 int drm_mode_getblob_ioctl(struct drm_device *dev,
3280 void *data, struct drm_file *file_priv)
3282 struct drm_mode_object *obj;
3283 struct drm_mode_get_blob *out_resp = data;
3284 struct drm_property_blob *blob;
3286 void __user *blob_ptr;
3288 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3291 drm_modeset_lock_all(dev);
3292 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3297 blob = obj_to_blob(obj);
3299 if (out_resp->length == blob->length) {
3300 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3301 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3306 out_resp->length = blob->length;
3309 drm_modeset_unlock_all(dev);
3313 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3316 struct drm_device *dev = connector->dev;
3319 if (connector->edid_blob_ptr)
3320 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3322 /* Delete edid, when there is none. */
3324 connector->edid_blob_ptr = NULL;
3325 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3329 size = EDID_LENGTH * (1 + edid->extensions);
3330 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3332 if (!connector->edid_blob_ptr)
3335 ret = drm_object_property_set_value(&connector->base,
3336 dev->mode_config.edid_property,
3337 connector->edid_blob_ptr->base.id);
3341 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3343 static bool drm_property_change_is_valid(struct drm_property *property,
3346 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3348 if (property->flags & DRM_MODE_PROP_RANGE) {
3349 if (value < property->values[0] || value > property->values[1])
3352 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3354 uint64_t valid_mask = 0;
3355 for (i = 0; i < property->num_values; i++)
3356 valid_mask |= (1ULL << property->values[i]);
3357 return !(value & ~valid_mask);
3358 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3359 /* Only the driver knows */
3363 for (i = 0; i < property->num_values; i++)
3364 if (property->values[i] == value)
3370 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3371 void *data, struct drm_file *file_priv)
3373 struct drm_mode_connector_set_property *conn_set_prop = data;
3374 struct drm_mode_obj_set_property obj_set_prop = {
3375 .value = conn_set_prop->value,
3376 .prop_id = conn_set_prop->prop_id,
3377 .obj_id = conn_set_prop->connector_id,
3378 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3381 /* It does all the locking and checking we need */
3382 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3385 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3386 struct drm_property *property,
3390 struct drm_connector *connector = obj_to_connector(obj);
3392 /* Do DPMS ourselves */
3393 if (property == connector->dev->mode_config.dpms_property) {
3394 if (connector->funcs->dpms)
3395 (*connector->funcs->dpms)(connector, (int)value);
3397 } else if (connector->funcs->set_property)
3398 ret = connector->funcs->set_property(connector, property, value);
3400 /* store the property value if successful */
3402 drm_object_property_set_value(&connector->base, property, value);
3406 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3407 struct drm_property *property,
3411 struct drm_crtc *crtc = obj_to_crtc(obj);
3413 if (crtc->funcs->set_property)
3414 ret = crtc->funcs->set_property(crtc, property, value);
3416 drm_object_property_set_value(obj, property, value);
3421 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3422 struct drm_property *property,
3426 struct drm_plane *plane = obj_to_plane(obj);
3428 if (plane->funcs->set_property)
3429 ret = plane->funcs->set_property(plane, property, value);
3431 drm_object_property_set_value(obj, property, value);
3436 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3437 struct drm_file *file_priv)
3439 struct drm_mode_obj_get_properties *arg = data;
3440 struct drm_mode_object *obj;
3444 int props_count = 0;
3445 uint32_t __user *props_ptr;
3446 uint64_t __user *prop_values_ptr;
3448 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3451 drm_modeset_lock_all(dev);
3453 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3458 if (!obj->properties) {
3463 props_count = obj->properties->count;
3465 /* This ioctl is called twice, once to determine how much space is
3466 * needed, and the 2nd time to fill it. */
3467 if ((arg->count_props >= props_count) && props_count) {
3469 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3470 prop_values_ptr = (uint64_t __user *)(unsigned long)
3471 (arg->prop_values_ptr);
3472 for (i = 0; i < props_count; i++) {
3473 if (put_user(obj->properties->ids[i],
3474 props_ptr + copied)) {
3478 if (put_user(obj->properties->values[i],
3479 prop_values_ptr + copied)) {
3486 arg->count_props = props_count;
3488 drm_modeset_unlock_all(dev);
3492 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3493 struct drm_file *file_priv)
3495 struct drm_mode_obj_set_property *arg = data;
3496 struct drm_mode_object *arg_obj;
3497 struct drm_mode_object *prop_obj;
3498 struct drm_property *property;
3502 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3505 drm_modeset_lock_all(dev);
3507 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3510 if (!arg_obj->properties)
3513 for (i = 0; i < arg_obj->properties->count; i++)
3514 if (arg_obj->properties->ids[i] == arg->prop_id)
3517 if (i == arg_obj->properties->count)
3520 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3521 DRM_MODE_OBJECT_PROPERTY);
3524 property = obj_to_property(prop_obj);
3526 if (!drm_property_change_is_valid(property, arg->value))
3529 switch (arg_obj->type) {
3530 case DRM_MODE_OBJECT_CONNECTOR:
3531 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3534 case DRM_MODE_OBJECT_CRTC:
3535 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3537 case DRM_MODE_OBJECT_PLANE:
3538 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3543 drm_modeset_unlock_all(dev);
3547 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3548 struct drm_encoder *encoder)
3552 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3553 if (connector->encoder_ids[i] == 0) {
3554 connector->encoder_ids[i] = encoder->base.id;
3560 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3562 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3563 struct drm_encoder *encoder)
3566 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3567 if (connector->encoder_ids[i] == encoder->base.id) {
3568 connector->encoder_ids[i] = 0;
3569 if (connector->encoder == encoder)
3570 connector->encoder = NULL;
3575 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3577 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3580 crtc->gamma_size = gamma_size;
3582 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3583 if (!crtc->gamma_store) {
3584 crtc->gamma_size = 0;
3590 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3592 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3593 void *data, struct drm_file *file_priv)
3595 struct drm_mode_crtc_lut *crtc_lut = data;
3596 struct drm_mode_object *obj;
3597 struct drm_crtc *crtc;
3598 void *r_base, *g_base, *b_base;
3602 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3605 drm_modeset_lock_all(dev);
3606 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3611 crtc = obj_to_crtc(obj);
3613 if (crtc->funcs->gamma_set == NULL) {
3618 /* memcpy into gamma store */
3619 if (crtc_lut->gamma_size != crtc->gamma_size) {
3624 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3625 r_base = crtc->gamma_store;
3626 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3631 g_base = r_base + size;
3632 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3637 b_base = g_base + size;
3638 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3643 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3646 drm_modeset_unlock_all(dev);
3651 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3652 void *data, struct drm_file *file_priv)
3654 struct drm_mode_crtc_lut *crtc_lut = data;
3655 struct drm_mode_object *obj;
3656 struct drm_crtc *crtc;
3657 void *r_base, *g_base, *b_base;
3661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3664 drm_modeset_lock_all(dev);
3665 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3670 crtc = obj_to_crtc(obj);
3672 /* memcpy into gamma store */
3673 if (crtc_lut->gamma_size != crtc->gamma_size) {
3678 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3679 r_base = crtc->gamma_store;
3680 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3685 g_base = r_base + size;
3686 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3691 b_base = g_base + size;
3692 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3697 drm_modeset_unlock_all(dev);
3701 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3702 void *data, struct drm_file *file_priv)
3704 struct drm_mode_crtc_page_flip *page_flip = data;
3705 struct drm_mode_object *obj;
3706 struct drm_crtc *crtc;
3707 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3708 struct drm_pending_vblank_event *e = NULL;
3709 unsigned long flags;
3710 int hdisplay, vdisplay;
3713 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3714 page_flip->reserved != 0)
3717 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3720 crtc = obj_to_crtc(obj);
3722 mutex_lock(&crtc->mutex);
3723 if (crtc->fb == NULL) {
3724 /* The framebuffer is currently unbound, presumably
3725 * due to a hotplug event, that userspace has not
3732 if (crtc->funcs->page_flip == NULL)
3735 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3739 hdisplay = crtc->mode.hdisplay;
3740 vdisplay = crtc->mode.vdisplay;
3742 if (crtc->invert_dimensions)
3743 swap(hdisplay, vdisplay);
3745 if (hdisplay > fb->width ||
3746 vdisplay > fb->height ||
3747 crtc->x > fb->width - hdisplay ||
3748 crtc->y > fb->height - vdisplay) {
3749 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3750 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3751 crtc->invert_dimensions ? " (inverted)" : "");
3756 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3758 spin_lock_irqsave(&dev->event_lock, flags);
3759 if (file_priv->event_space < sizeof e->event) {
3760 spin_unlock_irqrestore(&dev->event_lock, flags);
3763 file_priv->event_space -= sizeof e->event;
3764 spin_unlock_irqrestore(&dev->event_lock, flags);
3766 e = kzalloc(sizeof *e, GFP_KERNEL);
3768 spin_lock_irqsave(&dev->event_lock, flags);
3769 file_priv->event_space += sizeof e->event;
3770 spin_unlock_irqrestore(&dev->event_lock, flags);
3774 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3775 e->event.base.length = sizeof e->event;
3776 e->event.user_data = page_flip->user_data;
3777 e->base.event = &e->event.base;
3778 e->base.file_priv = file_priv;
3780 (void (*) (struct drm_pending_event *)) kfree;
3784 ret = crtc->funcs->page_flip(crtc, fb, e);
3786 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3787 spin_lock_irqsave(&dev->event_lock, flags);
3788 file_priv->event_space += sizeof e->event;
3789 spin_unlock_irqrestore(&dev->event_lock, flags);
3792 /* Keep the old fb, don't unref it. */
3796 * Warn if the driver hasn't properly updated the crtc->fb
3797 * field to reflect that the new framebuffer is now used.
3798 * Failing to do so will screw with the reference counting
3801 WARN_ON(crtc->fb != fb);
3802 /* Unref only the old framebuffer. */
3808 drm_framebuffer_unreference(fb);
3810 drm_framebuffer_unreference(old_fb);
3811 mutex_unlock(&crtc->mutex);
3816 void drm_mode_config_reset(struct drm_device *dev)
3818 struct drm_crtc *crtc;
3819 struct drm_encoder *encoder;
3820 struct drm_connector *connector;
3822 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3823 if (crtc->funcs->reset)
3824 crtc->funcs->reset(crtc);
3826 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3827 if (encoder->funcs->reset)
3828 encoder->funcs->reset(encoder);
3830 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3831 connector->status = connector_status_unknown;
3833 if (connector->funcs->reset)
3834 connector->funcs->reset(connector);
3837 EXPORT_SYMBOL(drm_mode_config_reset);
3839 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3840 void *data, struct drm_file *file_priv)
3842 struct drm_mode_create_dumb *args = data;
3844 if (!dev->driver->dumb_create)
3846 return dev->driver->dumb_create(file_priv, dev, args);
3849 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3850 void *data, struct drm_file *file_priv)
3852 struct drm_mode_map_dumb *args = data;
3854 /* call driver ioctl to get mmap offset */
3855 if (!dev->driver->dumb_map_offset)
3858 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3861 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3862 void *data, struct drm_file *file_priv)
3864 struct drm_mode_destroy_dumb *args = data;
3866 if (!dev->driver->dumb_destroy)
3869 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3873 * Just need to support RGB formats here for compat with code that doesn't
3874 * use pixel formats directly yet.
3876 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3881 case DRM_FORMAT_RGB332:
3882 case DRM_FORMAT_BGR233:
3886 case DRM_FORMAT_XRGB1555:
3887 case DRM_FORMAT_XBGR1555:
3888 case DRM_FORMAT_RGBX5551:
3889 case DRM_FORMAT_BGRX5551:
3890 case DRM_FORMAT_ARGB1555:
3891 case DRM_FORMAT_ABGR1555:
3892 case DRM_FORMAT_RGBA5551:
3893 case DRM_FORMAT_BGRA5551:
3897 case DRM_FORMAT_RGB565:
3898 case DRM_FORMAT_BGR565:
3902 case DRM_FORMAT_RGB888:
3903 case DRM_FORMAT_BGR888:
3907 case DRM_FORMAT_XRGB8888:
3908 case DRM_FORMAT_XBGR8888:
3909 case DRM_FORMAT_RGBX8888:
3910 case DRM_FORMAT_BGRX8888:
3914 case DRM_FORMAT_XRGB2101010:
3915 case DRM_FORMAT_XBGR2101010:
3916 case DRM_FORMAT_RGBX1010102:
3917 case DRM_FORMAT_BGRX1010102:
3918 case DRM_FORMAT_ARGB2101010:
3919 case DRM_FORMAT_ABGR2101010:
3920 case DRM_FORMAT_RGBA1010102:
3921 case DRM_FORMAT_BGRA1010102:
3925 case DRM_FORMAT_ARGB8888:
3926 case DRM_FORMAT_ABGR8888:
3927 case DRM_FORMAT_RGBA8888:
3928 case DRM_FORMAT_BGRA8888:
3933 DRM_DEBUG_KMS("unsupported pixel format\n");
3939 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3942 * drm_format_num_planes - get the number of planes for format
3943 * @format: pixel format (DRM_FORMAT_*)
3946 * The number of planes used by the specified pixel format.
3948 int drm_format_num_planes(uint32_t format)
3951 case DRM_FORMAT_YUV410:
3952 case DRM_FORMAT_YVU410:
3953 case DRM_FORMAT_YUV411:
3954 case DRM_FORMAT_YVU411:
3955 case DRM_FORMAT_YUV420:
3956 case DRM_FORMAT_YVU420:
3957 case DRM_FORMAT_YUV422:
3958 case DRM_FORMAT_YVU422:
3959 case DRM_FORMAT_YUV444:
3960 case DRM_FORMAT_YVU444:
3962 case DRM_FORMAT_NV12:
3963 case DRM_FORMAT_NV21:
3964 case DRM_FORMAT_NV16:
3965 case DRM_FORMAT_NV61:
3966 case DRM_FORMAT_NV24:
3967 case DRM_FORMAT_NV42:
3973 EXPORT_SYMBOL(drm_format_num_planes);
3976 * drm_format_plane_cpp - determine the bytes per pixel value
3977 * @format: pixel format (DRM_FORMAT_*)
3978 * @plane: plane index
3981 * The bytes per pixel value for the specified plane.
3983 int drm_format_plane_cpp(uint32_t format, int plane)
3988 if (plane >= drm_format_num_planes(format))
3992 case DRM_FORMAT_YUYV:
3993 case DRM_FORMAT_YVYU:
3994 case DRM_FORMAT_UYVY:
3995 case DRM_FORMAT_VYUY:
3997 case DRM_FORMAT_NV12:
3998 case DRM_FORMAT_NV21:
3999 case DRM_FORMAT_NV16:
4000 case DRM_FORMAT_NV61:
4001 case DRM_FORMAT_NV24:
4002 case DRM_FORMAT_NV42:
4003 return plane ? 2 : 1;
4004 case DRM_FORMAT_YUV410:
4005 case DRM_FORMAT_YVU410:
4006 case DRM_FORMAT_YUV411:
4007 case DRM_FORMAT_YVU411:
4008 case DRM_FORMAT_YUV420:
4009 case DRM_FORMAT_YVU420:
4010 case DRM_FORMAT_YUV422:
4011 case DRM_FORMAT_YVU422:
4012 case DRM_FORMAT_YUV444:
4013 case DRM_FORMAT_YVU444:
4016 drm_fb_get_bpp_depth(format, &depth, &bpp);
4020 EXPORT_SYMBOL(drm_format_plane_cpp);
4023 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4024 * @format: pixel format (DRM_FORMAT_*)
4027 * The horizontal chroma subsampling factor for the
4028 * specified pixel format.
4030 int drm_format_horz_chroma_subsampling(uint32_t format)
4033 case DRM_FORMAT_YUV411:
4034 case DRM_FORMAT_YVU411:
4035 case DRM_FORMAT_YUV410:
4036 case DRM_FORMAT_YVU410:
4038 case DRM_FORMAT_YUYV:
4039 case DRM_FORMAT_YVYU:
4040 case DRM_FORMAT_UYVY:
4041 case DRM_FORMAT_VYUY:
4042 case DRM_FORMAT_NV12:
4043 case DRM_FORMAT_NV21:
4044 case DRM_FORMAT_NV16:
4045 case DRM_FORMAT_NV61:
4046 case DRM_FORMAT_YUV422:
4047 case DRM_FORMAT_YVU422:
4048 case DRM_FORMAT_YUV420:
4049 case DRM_FORMAT_YVU420:
4055 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4058 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4059 * @format: pixel format (DRM_FORMAT_*)
4062 * The vertical chroma subsampling factor for the
4063 * specified pixel format.
4065 int drm_format_vert_chroma_subsampling(uint32_t format)
4068 case DRM_FORMAT_YUV410:
4069 case DRM_FORMAT_YVU410:
4071 case DRM_FORMAT_YUV420:
4072 case DRM_FORMAT_YVU420:
4073 case DRM_FORMAT_NV12:
4074 case DRM_FORMAT_NV21:
4080 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);