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 <uapi_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);
55 lockmgr(&crtc->mutex, LK_EXCLUSIVE);
57 EXPORT_SYMBOL(drm_modeset_lock_all);
60 * drm_modeset_unlock_all - drop all modeset locks
63 void drm_modeset_unlock_all(struct drm_device *dev)
65 struct drm_crtc *crtc;
67 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68 mutex_unlock(&crtc->mutex);
70 mutex_unlock(&dev->mode_config.mutex);
72 EXPORT_SYMBOL(drm_modeset_unlock_all);
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
78 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
80 struct drm_crtc *crtc;
83 /* Locking is currently fubar in the panic handler. */
88 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
89 WARN_ON(!mutex_is_locked(&crtc->mutex));
91 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
93 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
95 /* Avoid boilerplate. I'm tired of typing. */
96 #define DRM_ENUM_NAME_FN(fnname, list) \
97 char *fnname(int val) \
100 for (i = 0; i < ARRAY_SIZE(list); i++) { \
101 if (list[i].type == val) \
102 return list[i].name; \
104 return "(unknown)"; \
110 static struct drm_prop_enum_list drm_dpms_enum_list[] =
111 { { DRM_MODE_DPMS_ON, "On" },
112 { DRM_MODE_DPMS_STANDBY, "Standby" },
113 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
114 { DRM_MODE_DPMS_OFF, "Off" }
117 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
120 * Optional properties
122 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
124 { DRM_MODE_SCALE_NONE, "None" },
125 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
126 { DRM_MODE_SCALE_CENTER, "Center" },
127 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
130 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
132 { DRM_MODE_DITHERING_OFF, "Off" },
133 { DRM_MODE_DITHERING_ON, "On" },
134 { DRM_MODE_DITHERING_AUTO, "Automatic" },
138 * Non-global properties, but "required" for certain connectors.
140 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
142 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
143 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
144 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
147 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
149 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
151 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
152 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
153 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
156 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
157 drm_dvi_i_subconnector_enum_list)
159 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
161 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
162 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
163 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
164 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
165 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
168 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
170 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
172 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
173 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
174 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
175 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
176 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
179 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
180 drm_tv_subconnector_enum_list)
182 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
183 { DRM_MODE_DIRTY_OFF, "Off" },
184 { DRM_MODE_DIRTY_ON, "On" },
185 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
188 struct drm_conn_prop_enum_list {
195 * Connector and encoder types.
197 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
198 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
199 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
200 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
201 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
202 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
203 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
204 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
205 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
206 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
207 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
208 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
209 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
210 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
211 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
212 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
213 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
216 static struct drm_prop_enum_list drm_encoder_enum_list[] =
217 { { DRM_MODE_ENCODER_NONE, "None" },
218 { DRM_MODE_ENCODER_DAC, "DAC" },
219 { DRM_MODE_ENCODER_TMDS, "TMDS" },
220 { DRM_MODE_ENCODER_LVDS, "LVDS" },
221 { DRM_MODE_ENCODER_TVDAC, "TV" },
222 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
225 char *drm_get_encoder_name(struct drm_encoder *encoder)
229 ksnprintf(buf, 32, "%s-%d",
230 drm_encoder_enum_list[encoder->encoder_type].name,
234 EXPORT_SYMBOL(drm_get_encoder_name);
236 char *drm_get_connector_name(struct drm_connector *connector)
240 ksnprintf(buf, 32, "%s-%d",
241 drm_connector_enum_list[connector->connector_type].name,
242 connector->connector_type_id);
245 EXPORT_SYMBOL(drm_get_connector_name);
247 char *drm_get_connector_status_name(enum drm_connector_status status)
249 if (status == connector_status_connected)
251 else if (status == connector_status_disconnected)
252 return "disconnected";
256 EXPORT_SYMBOL(drm_get_connector_status_name);
259 * drm_mode_object_get - allocate a new modeset identifier
261 * @obj: object pointer, used to generate unique ID
262 * @obj_type: object type
264 * Create a unique identifier based on @ptr in @dev's identifier space. Used
265 * for tracking modes, CRTCs and connectors.
268 * New unique (relative to other objects in @dev) integer identifier for the
271 static int drm_mode_object_get(struct drm_device *dev,
272 struct drm_mode_object *obj, uint32_t obj_type)
278 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
279 DRM_ERROR("Ran out memory getting a mode number\n");
283 lockmgr(&dev->mode_config.idr_mutex, LK_EXCLUSIVE);
284 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
285 lockmgr(&dev->mode_config.idr_mutex, LK_RELEASE);
292 * Set up the object linking under the protection of the idr
293 * lock so that other users can't see inconsistent state.
296 obj->type = obj_type;
301 * drm_mode_object_put - free a modeset identifer
303 * @object: object to free
305 * Free @id from @dev's unique identifier pool.
307 static void drm_mode_object_put(struct drm_device *dev,
308 struct drm_mode_object *object)
310 mutex_lock(&dev->mode_config.idr_mutex);
311 idr_remove(&dev->mode_config.crtc_idr, object->id);
312 mutex_unlock(&dev->mode_config.idr_mutex);
316 * drm_mode_object_find - look up a drm object with static lifetime
318 * @id: id of the mode object
319 * @type: type of the mode object
321 * Note that framebuffers cannot be looked up with this functions - since those
322 * are reference counted, they need special treatment.
324 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
325 uint32_t id, uint32_t type)
327 struct drm_mode_object *obj = NULL;
329 /* Framebuffers are reference counted and need their own lookup
331 WARN_ON(type == DRM_MODE_OBJECT_FB);
333 mutex_lock(&dev->mode_config.idr_mutex);
334 obj = idr_find(&dev->mode_config.crtc_idr, id);
335 if (!obj || (obj->type != type) || (obj->id != id))
337 mutex_unlock(&dev->mode_config.idr_mutex);
341 EXPORT_SYMBOL(drm_mode_object_find);
344 * drm_framebuffer_init - initialize a framebuffer
346 * @fb: framebuffer to be initialized
347 * @funcs: ... with these functions
349 * Allocates an ID for the framebuffer's parent mode object, sets its mode
350 * functions & device file and adds it to the master fd list.
353 * This functions publishes the fb and makes it available for concurrent access
354 * by other users. Which means by this point the fb _must_ be fully set up -
355 * since all the fb attributes are invariant over its lifetime, no further
356 * locking but only correct reference counting is required.
359 * Zero on success, error code on failure.
361 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
362 const struct drm_framebuffer_funcs *funcs)
366 mutex_lock(&dev->mode_config.fb_lock);
367 kref_init(&fb->refcount);
368 INIT_LIST_HEAD(&fb->filp_head);
372 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
376 /* Grab the idr reference. */
377 drm_framebuffer_reference(fb);
379 dev->mode_config.num_fb++;
380 list_add(&fb->head, &dev->mode_config.fb_list);
382 mutex_unlock(&dev->mode_config.fb_lock);
386 EXPORT_SYMBOL(drm_framebuffer_init);
388 static void drm_framebuffer_free(struct kref *kref)
390 struct drm_framebuffer *fb =
391 container_of(kref, struct drm_framebuffer, refcount);
392 fb->funcs->destroy(fb);
395 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
398 struct drm_mode_object *obj = NULL;
399 struct drm_framebuffer *fb;
401 mutex_lock(&dev->mode_config.idr_mutex);
402 obj = idr_find(&dev->mode_config.crtc_idr, id);
403 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
407 mutex_unlock(&dev->mode_config.idr_mutex);
413 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
415 * @id: id of the fb object
417 * If successful, this grabs an additional reference to the framebuffer -
418 * callers need to make sure to eventually unreference the returned framebuffer
421 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
424 struct drm_framebuffer *fb;
426 mutex_lock(&dev->mode_config.fb_lock);
427 fb = __drm_framebuffer_lookup(dev, id);
429 drm_framebuffer_reference(fb);
430 mutex_unlock(&dev->mode_config.fb_lock);
434 EXPORT_SYMBOL(drm_framebuffer_lookup);
437 * drm_framebuffer_unreference - unref a framebuffer
438 * @fb: framebuffer to unref
440 * This functions decrements the fb's refcount and frees it if it drops to zero.
442 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
444 DRM_DEBUG("FB ID: %d\n", fb->base.id);
445 kref_put(&fb->refcount, drm_framebuffer_free);
447 EXPORT_SYMBOL(drm_framebuffer_unreference);
450 * drm_framebuffer_reference - incr the fb refcnt
453 void drm_framebuffer_reference(struct drm_framebuffer *fb)
455 DRM_DEBUG("FB ID: %d\n", fb->base.id);
456 kref_get(&fb->refcount);
458 EXPORT_SYMBOL(drm_framebuffer_reference);
460 static void drm_framebuffer_free_bug(struct kref *kref)
465 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
467 DRM_DEBUG("FB ID: %d\n", fb->base.id);
468 kref_put(&fb->refcount, drm_framebuffer_free_bug);
471 /* dev->mode_config.fb_lock must be held! */
472 static void __drm_framebuffer_unregister(struct drm_device *dev,
473 struct drm_framebuffer *fb)
475 mutex_lock(&dev->mode_config.idr_mutex);
476 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
477 mutex_unlock(&dev->mode_config.idr_mutex);
481 __drm_framebuffer_unreference(fb);
485 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
486 * @fb: fb to unregister
488 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
489 * those used for fbdev. Note that the caller must hold a reference of it's own,
490 * i.e. the object may not be destroyed through this call (since it'll lead to a
491 * locking inversion).
493 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
495 struct drm_device *dev = fb->dev;
497 mutex_lock(&dev->mode_config.fb_lock);
498 /* Mark fb as reaped and drop idr ref. */
499 __drm_framebuffer_unregister(dev, fb);
500 mutex_unlock(&dev->mode_config.fb_lock);
502 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
505 * drm_framebuffer_cleanup - remove a framebuffer object
506 * @fb: framebuffer to remove
508 * Cleanup references to a user-created framebuffer. This function is intended
509 * to be used from the drivers ->destroy callback.
511 * Note that this function does not remove the fb from active usuage - if it is
512 * still used anywhere, hilarity can ensue since userspace could call getfb on
513 * the id and get back -EINVAL. Obviously no concern at driver unload time.
515 * Also, the framebuffer will not be removed from the lookup idr - for
516 * user-created framebuffers this will happen in in the rmfb ioctl. For
517 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
518 * drm_framebuffer_unregister_private.
520 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
522 struct drm_device *dev = fb->dev;
524 mutex_lock(&dev->mode_config.fb_lock);
526 dev->mode_config.num_fb--;
527 mutex_unlock(&dev->mode_config.fb_lock);
529 EXPORT_SYMBOL(drm_framebuffer_cleanup);
532 * drm_framebuffer_remove - remove and unreference a framebuffer object
533 * @fb: framebuffer to remove
535 * Scans all the CRTCs and planes in @dev's mode_config. If they're
536 * using @fb, removes it, setting it to NULL. Then drops the reference to the
537 * passed-in framebuffer. Might take the modeset locks.
539 * Note that this function optimizes the cleanup away if the caller holds the
540 * last reference to the framebuffer. It is also guaranteed to not take the
541 * modeset locks in this case.
543 void drm_framebuffer_remove(struct drm_framebuffer *fb)
545 struct drm_device *dev = fb->dev;
546 struct drm_crtc *crtc;
547 struct drm_plane *plane;
548 struct drm_mode_set set;
551 WARN_ON(!list_empty(&fb->filp_head));
554 * drm ABI mandates that we remove any deleted framebuffers from active
555 * useage. But since most sane clients only remove framebuffers they no
556 * longer need, try to optimize this away.
558 * Since we're holding a reference ourselves, observing a refcount of 1
559 * means that we're the last holder and can skip it. Also, the refcount
560 * can never increase from 1 again, so we don't need any barriers or
563 * Note that userspace could try to race with use and instate a new
564 * usage _after_ we've cleared all current ones. End result will be an
565 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
568 if (atomic_read(&fb->refcount.refcount) > 1) {
569 drm_modeset_lock_all(dev);
570 /* remove from any CRTC */
571 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
572 if (crtc->fb == fb) {
573 /* should turn off the crtc */
574 memset(&set, 0, sizeof(struct drm_mode_set));
577 ret = drm_mode_set_config_internal(&set);
579 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
583 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
584 if (plane->fb == fb) {
585 /* should turn off the crtc */
586 ret = plane->funcs->disable_plane(plane);
588 DRM_ERROR("failed to disable plane with busy fb\n");
589 /* disconnect the plane from the fb and crtc: */
590 __drm_framebuffer_unreference(plane->fb);
595 drm_modeset_unlock_all(dev);
598 drm_framebuffer_unreference(fb);
600 EXPORT_SYMBOL(drm_framebuffer_remove);
603 * drm_crtc_init - Initialise a new CRTC object
605 * @crtc: CRTC object to init
606 * @funcs: callbacks for the new CRTC
608 * Inits a new object created as base part of an driver crtc object.
611 * Zero on success, error code on failure.
613 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
614 const struct drm_crtc_funcs *funcs)
620 crtc->invert_dimensions = false;
622 drm_modeset_lock_all(dev);
623 lockinit(&crtc->mutex, "drmcm", 0, LK_CANRECURSE);
624 // mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
625 lockmgr(&crtc->mutex, LK_EXCLUSIVE);
627 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
631 crtc->base.properties = &crtc->properties;
633 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
634 dev->mode_config.num_crtc++;
637 drm_modeset_unlock_all(dev);
641 EXPORT_SYMBOL(drm_crtc_init);
644 * drm_crtc_cleanup - Cleans up the core crtc usage.
645 * @crtc: CRTC to cleanup
647 * Cleanup @crtc. Removes from drm modesetting space
648 * does NOT free object, caller does that.
650 void drm_crtc_cleanup(struct drm_crtc *crtc)
652 struct drm_device *dev = crtc->dev;
654 kfree(crtc->gamma_store);
655 crtc->gamma_store = NULL;
657 drm_mode_object_put(dev, &crtc->base);
658 list_del(&crtc->head);
659 dev->mode_config.num_crtc--;
661 EXPORT_SYMBOL(drm_crtc_cleanup);
664 * drm_mode_probed_add - add a mode to a connector's probed mode list
665 * @connector: connector the new mode
668 * Add @mode to @connector's mode list for later use.
670 void drm_mode_probed_add(struct drm_connector *connector,
671 struct drm_display_mode *mode)
673 list_add(&mode->head, &connector->probed_modes);
675 EXPORT_SYMBOL(drm_mode_probed_add);
678 * drm_mode_remove - remove and free a mode
679 * @connector: connector list to modify
680 * @mode: mode to remove
682 * Remove @mode from @connector's mode list, then free it.
684 void drm_mode_remove(struct drm_connector *connector,
685 struct drm_display_mode *mode)
687 list_del(&mode->head);
688 drm_mode_destroy(connector->dev, mode);
690 EXPORT_SYMBOL(drm_mode_remove);
693 * drm_connector_init - Init a preallocated connector
695 * @connector: the connector to init
696 * @funcs: callbacks for this connector
697 * @connector_type: user visible type of the connector
699 * Initialises a preallocated connector. Connectors should be
700 * subclassed as part of driver connector objects.
703 * Zero on success, error code on failure.
705 int drm_connector_init(struct drm_device *dev,
706 struct drm_connector *connector,
707 const struct drm_connector_funcs *funcs,
712 drm_modeset_lock_all(dev);
714 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
718 connector->base.properties = &connector->properties;
719 connector->dev = dev;
720 connector->funcs = funcs;
721 connector->connector_type = connector_type;
722 connector->connector_type_id =
723 ++drm_connector_enum_list[connector_type].count; /* TODO */
724 INIT_LIST_HEAD(&connector->probed_modes);
725 INIT_LIST_HEAD(&connector->modes);
726 connector->edid_blob_ptr = NULL;
727 connector->status = connector_status_unknown;
729 list_add_tail(&connector->head, &dev->mode_config.connector_list);
730 dev->mode_config.num_connector++;
732 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
733 drm_object_attach_property(&connector->base,
734 dev->mode_config.edid_property,
737 drm_object_attach_property(&connector->base,
738 dev->mode_config.dpms_property, 0);
741 drm_modeset_unlock_all(dev);
745 EXPORT_SYMBOL(drm_connector_init);
748 * drm_connector_cleanup - cleans up an initialised connector
749 * @connector: connector to cleanup
751 * Cleans up the connector but doesn't free the object.
753 void drm_connector_cleanup(struct drm_connector *connector)
755 struct drm_device *dev = connector->dev;
756 struct drm_display_mode *mode, *t;
758 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
759 drm_mode_remove(connector, mode);
761 list_for_each_entry_safe(mode, t, &connector->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)
773 struct drm_connector *connector;
775 /* taking the mode config mutex ends up in a clash with sysfs */
776 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
777 drm_sysfs_connector_remove(connector);
781 EXPORT_SYMBOL(drm_connector_unplug_all);
783 int drm_encoder_init(struct drm_device *dev,
784 struct drm_encoder *encoder,
785 const struct drm_encoder_funcs *funcs,
790 drm_modeset_lock_all(dev);
792 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
797 encoder->encoder_type = encoder_type;
798 encoder->funcs = funcs;
800 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
801 dev->mode_config.num_encoder++;
804 drm_modeset_unlock_all(dev);
808 EXPORT_SYMBOL(drm_encoder_init);
810 void drm_encoder_cleanup(struct drm_encoder *encoder)
812 struct drm_device *dev = encoder->dev;
813 drm_modeset_lock_all(dev);
814 drm_mode_object_put(dev, &encoder->base);
815 list_del(&encoder->head);
816 dev->mode_config.num_encoder--;
817 drm_modeset_unlock_all(dev);
819 EXPORT_SYMBOL(drm_encoder_cleanup);
821 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
822 unsigned long possible_crtcs,
823 const struct drm_plane_funcs *funcs,
824 const uint32_t *formats, uint32_t format_count,
829 drm_modeset_lock_all(dev);
831 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
835 plane->base.properties = &plane->properties;
837 plane->funcs = funcs;
838 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
840 if (!plane->format_types) {
841 DRM_DEBUG_KMS("out of memory when allocating plane\n");
842 drm_mode_object_put(dev, &plane->base);
847 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
848 plane->format_count = format_count;
849 plane->possible_crtcs = possible_crtcs;
851 /* private planes are not exposed to userspace, but depending on
852 * display hardware, might be convenient to allow sharing programming
853 * for the scanout engine with the crtc implementation.
856 list_add_tail(&plane->head, &dev->mode_config.plane_list);
857 dev->mode_config.num_plane++;
859 INIT_LIST_HEAD(&plane->head);
863 drm_modeset_unlock_all(dev);
867 EXPORT_SYMBOL(drm_plane_init);
869 void drm_plane_cleanup(struct drm_plane *plane)
871 struct drm_device *dev = plane->dev;
873 drm_modeset_lock_all(dev);
874 kfree(plane->format_types);
875 drm_mode_object_put(dev, &plane->base);
876 /* if not added to a list, it must be a private plane */
877 if (!list_empty(&plane->head)) {
878 list_del(&plane->head);
879 dev->mode_config.num_plane--;
881 drm_modeset_unlock_all(dev);
883 EXPORT_SYMBOL(drm_plane_cleanup);
886 * drm_mode_create - create a new display mode
889 * Create a new drm_display_mode, give it an ID, and return it.
892 * Pointer to new mode on success, NULL on error.
894 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
896 struct drm_display_mode *nmode;
898 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
902 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
909 EXPORT_SYMBOL(drm_mode_create);
912 * drm_mode_destroy - remove a mode
914 * @mode: mode to remove
916 * Free @mode's unique identifier, then free it.
918 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
923 drm_mode_object_put(dev, &mode->base);
927 EXPORT_SYMBOL(drm_mode_destroy);
929 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
931 struct drm_property *edid;
932 struct drm_property *dpms;
935 * Standard properties (apply to all connectors)
937 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
938 DRM_MODE_PROP_IMMUTABLE,
940 dev->mode_config.edid_property = edid;
942 dpms = drm_property_create_enum(dev, 0,
943 "DPMS", drm_dpms_enum_list,
944 ARRAY_SIZE(drm_dpms_enum_list));
945 dev->mode_config.dpms_property = dpms;
951 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
954 * Called by a driver the first time a DVI-I connector is made.
956 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
958 struct drm_property *dvi_i_selector;
959 struct drm_property *dvi_i_subconnector;
961 if (dev->mode_config.dvi_i_select_subconnector_property)
965 drm_property_create_enum(dev, 0,
966 "select subconnector",
967 drm_dvi_i_select_enum_list,
968 ARRAY_SIZE(drm_dvi_i_select_enum_list));
969 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
971 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
973 drm_dvi_i_subconnector_enum_list,
974 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
975 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
979 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
982 * drm_create_tv_properties - create TV specific connector properties
984 * @num_modes: number of different TV formats (modes) supported
985 * @modes: array of pointers to strings containing name of each format
987 * Called by a driver's TV initialization routine, this function creates
988 * the TV specific connector properties for a given device. Caller is
989 * responsible for allocating a list of format names and passing them to
992 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
995 struct drm_property *tv_selector;
996 struct drm_property *tv_subconnector;
999 if (dev->mode_config.tv_select_subconnector_property)
1003 * Basic connector properties
1005 tv_selector = drm_property_create_enum(dev, 0,
1006 "select subconnector",
1007 drm_tv_select_enum_list,
1008 ARRAY_SIZE(drm_tv_select_enum_list));
1009 dev->mode_config.tv_select_subconnector_property = tv_selector;
1012 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1014 drm_tv_subconnector_enum_list,
1015 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1016 dev->mode_config.tv_subconnector_property = tv_subconnector;
1019 * Other, TV specific properties: margins & TV modes.
1021 dev->mode_config.tv_left_margin_property =
1022 drm_property_create_range(dev, 0, "left margin", 0, 100);
1024 dev->mode_config.tv_right_margin_property =
1025 drm_property_create_range(dev, 0, "right margin", 0, 100);
1027 dev->mode_config.tv_top_margin_property =
1028 drm_property_create_range(dev, 0, "top margin", 0, 100);
1030 dev->mode_config.tv_bottom_margin_property =
1031 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1033 dev->mode_config.tv_mode_property =
1034 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1036 for (i = 0; i < num_modes; i++)
1037 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1040 dev->mode_config.tv_brightness_property =
1041 drm_property_create_range(dev, 0, "brightness", 0, 100);
1043 dev->mode_config.tv_contrast_property =
1044 drm_property_create_range(dev, 0, "contrast", 0, 100);
1046 dev->mode_config.tv_flicker_reduction_property =
1047 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1049 dev->mode_config.tv_overscan_property =
1050 drm_property_create_range(dev, 0, "overscan", 0, 100);
1052 dev->mode_config.tv_saturation_property =
1053 drm_property_create_range(dev, 0, "saturation", 0, 100);
1055 dev->mode_config.tv_hue_property =
1056 drm_property_create_range(dev, 0, "hue", 0, 100);
1060 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1063 * drm_mode_create_scaling_mode_property - create scaling mode property
1066 * Called by a driver the first time it's needed, must be attached to desired
1069 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1071 struct drm_property *scaling_mode;
1073 if (dev->mode_config.scaling_mode_property)
1077 drm_property_create_enum(dev, 0, "scaling mode",
1078 drm_scaling_mode_enum_list,
1079 ARRAY_SIZE(drm_scaling_mode_enum_list));
1081 dev->mode_config.scaling_mode_property = scaling_mode;
1085 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1088 * drm_mode_create_dithering_property - create dithering property
1091 * Called by a driver the first time it's needed, must be attached to desired
1094 int drm_mode_create_dithering_property(struct drm_device *dev)
1096 struct drm_property *dithering_mode;
1098 if (dev->mode_config.dithering_mode_property)
1102 drm_property_create_enum(dev, 0, "dithering",
1103 drm_dithering_mode_enum_list,
1104 ARRAY_SIZE(drm_dithering_mode_enum_list));
1105 dev->mode_config.dithering_mode_property = dithering_mode;
1109 EXPORT_SYMBOL(drm_mode_create_dithering_property);
1112 * drm_mode_create_dirty_property - create dirty property
1115 * Called by a driver the first time it's needed, must be attached to desired
1118 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1120 struct drm_property *dirty_info;
1122 if (dev->mode_config.dirty_info_property)
1126 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1128 drm_dirty_info_enum_list,
1129 ARRAY_SIZE(drm_dirty_info_enum_list));
1130 dev->mode_config.dirty_info_property = dirty_info;
1134 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1136 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1138 uint32_t total_objects = 0;
1140 total_objects += dev->mode_config.num_crtc;
1141 total_objects += dev->mode_config.num_connector;
1142 total_objects += dev->mode_config.num_encoder;
1144 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1145 if (!group->id_list)
1148 group->num_crtcs = 0;
1149 group->num_connectors = 0;
1150 group->num_encoders = 0;
1154 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1155 struct drm_mode_group *group)
1157 struct drm_crtc *crtc;
1158 struct drm_encoder *encoder;
1159 struct drm_connector *connector;
1162 if ((ret = drm_mode_group_init(dev, group)))
1165 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1166 group->id_list[group->num_crtcs++] = crtc->base.id;
1168 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1169 group->id_list[group->num_crtcs + group->num_encoders++] =
1172 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1173 group->id_list[group->num_crtcs + group->num_encoders +
1174 group->num_connectors++] = connector->base.id;
1178 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1181 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1182 * @out: drm_mode_modeinfo struct to return to the user
1183 * @in: drm_display_mode to use
1185 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1188 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1189 const struct drm_display_mode *in)
1191 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1192 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1193 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1194 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1195 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1196 "timing values too large for mode info\n");
1198 out->clock = in->clock;
1199 out->hdisplay = in->hdisplay;
1200 out->hsync_start = in->hsync_start;
1201 out->hsync_end = in->hsync_end;
1202 out->htotal = in->htotal;
1203 out->hskew = in->hskew;
1204 out->vdisplay = in->vdisplay;
1205 out->vsync_start = in->vsync_start;
1206 out->vsync_end = in->vsync_end;
1207 out->vtotal = in->vtotal;
1208 out->vscan = in->vscan;
1209 out->vrefresh = in->vrefresh;
1210 out->flags = in->flags;
1211 out->type = in->type;
1212 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1213 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1217 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1218 * @out: drm_display_mode to return to the user
1219 * @in: drm_mode_modeinfo to use
1221 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1225 * Zero on success, errno on failure.
1227 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1228 const struct drm_mode_modeinfo *in)
1230 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1233 out->clock = in->clock;
1234 out->hdisplay = in->hdisplay;
1235 out->hsync_start = in->hsync_start;
1236 out->hsync_end = in->hsync_end;
1237 out->htotal = in->htotal;
1238 out->hskew = in->hskew;
1239 out->vdisplay = in->vdisplay;
1240 out->vsync_start = in->vsync_start;
1241 out->vsync_end = in->vsync_end;
1242 out->vtotal = in->vtotal;
1243 out->vscan = in->vscan;
1244 out->vrefresh = in->vrefresh;
1245 out->flags = in->flags;
1246 out->type = in->type;
1247 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1248 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1254 * drm_mode_getresources - get graphics configuration
1255 * @dev: drm device for the ioctl
1256 * @data: data pointer for the ioctl
1257 * @file_priv: drm file for the ioctl call
1259 * Construct a set of configuration description structures and return
1260 * them to the user, including CRTC, connector and framebuffer configuration.
1262 * Called by the user via ioctl.
1265 * Zero on success, errno on failure.
1267 int drm_mode_getresources(struct drm_device *dev, void *data,
1268 struct drm_file *file_priv)
1270 struct drm_mode_card_res *card_res = data;
1271 struct list_head *lh;
1272 struct drm_framebuffer *fb;
1273 struct drm_connector *connector;
1274 struct drm_crtc *crtc;
1275 struct drm_encoder *encoder;
1277 int connector_count = 0;
1280 int encoder_count = 0;
1282 uint32_t __user *fb_id;
1283 uint32_t __user *crtc_id;
1284 uint32_t __user *connector_id;
1285 uint32_t __user *encoder_id;
1286 struct drm_mode_group *mode_group;
1288 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1292 mutex_lock(&file_priv->fbs_lock);
1294 * For the non-control nodes we need to limit the list of resources
1295 * by IDs in the group list for this node
1297 list_for_each(lh, &file_priv->fbs)
1300 /* handle this in 4 parts */
1302 if (card_res->count_fbs >= fb_count) {
1304 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1305 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1306 if (put_user(fb->base.id, fb_id + copied)) {
1307 mutex_unlock(&file_priv->fbs_lock);
1313 card_res->count_fbs = fb_count;
1314 mutex_unlock(&file_priv->fbs_lock);
1316 drm_modeset_lock_all(dev);
1318 mode_group = &file_priv->master->minor->mode_group;
1319 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1321 mode_group = NULL; /* XXXKIB */
1322 if (1 || file_priv->master) {
1325 list_for_each(lh, &dev->mode_config.crtc_list)
1328 list_for_each(lh, &dev->mode_config.connector_list)
1331 list_for_each(lh, &dev->mode_config.encoder_list)
1335 crtc_count = mode_group->num_crtcs;
1336 connector_count = mode_group->num_connectors;
1337 encoder_count = mode_group->num_encoders;
1340 card_res->max_height = dev->mode_config.max_height;
1341 card_res->min_height = dev->mode_config.min_height;
1342 card_res->max_width = dev->mode_config.max_width;
1343 card_res->min_width = dev->mode_config.min_width;
1346 if (card_res->count_crtcs >= crtc_count) {
1348 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1350 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1352 if (1 || file_priv->master) {
1354 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1356 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1357 if (put_user(crtc->base.id, crtc_id + copied)) {
1364 for (i = 0; i < mode_group->num_crtcs; i++) {
1365 if (put_user(mode_group->id_list[i],
1366 crtc_id + copied)) {
1374 card_res->count_crtcs = crtc_count;
1377 if (card_res->count_encoders >= encoder_count) {
1379 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1381 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1383 if (file_priv->master) {
1386 list_for_each_entry(encoder,
1387 &dev->mode_config.encoder_list,
1389 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1390 drm_get_encoder_name(encoder));
1391 if (put_user(encoder->base.id, encoder_id +
1399 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1400 if (put_user(mode_group->id_list[i],
1401 encoder_id + copied)) {
1410 card_res->count_encoders = encoder_count;
1413 if (card_res->count_connectors >= connector_count) {
1415 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1417 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1419 if (file_priv->master) {
1421 list_for_each_entry(connector,
1422 &dev->mode_config.connector_list,
1424 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1426 drm_get_connector_name(connector));
1427 if (put_user(connector->base.id,
1428 connector_id + copied)) {
1435 int start = mode_group->num_crtcs +
1436 mode_group->num_encoders;
1437 for (i = start; i < start + mode_group->num_connectors; i++) {
1438 if (put_user(mode_group->id_list[i],
1439 connector_id + copied)) {
1447 card_res->count_connectors = connector_count;
1449 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1450 card_res->count_connectors, card_res->count_encoders);
1453 drm_modeset_unlock_all(dev);
1458 * drm_mode_getcrtc - get CRTC configuration
1459 * @dev: drm device for the ioctl
1460 * @data: data pointer for the ioctl
1461 * @file_priv: drm file for the ioctl call
1463 * Construct a CRTC configuration structure to return to the user.
1465 * Called by the user via ioctl.
1468 * Zero on success, errno on failure.
1470 int drm_mode_getcrtc(struct drm_device *dev,
1471 void *data, struct drm_file *file_priv)
1473 struct drm_mode_crtc *crtc_resp = data;
1474 struct drm_crtc *crtc;
1475 struct drm_mode_object *obj;
1478 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1481 drm_modeset_lock_all(dev);
1483 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1484 DRM_MODE_OBJECT_CRTC);
1489 crtc = obj_to_crtc(obj);
1491 crtc_resp->x = crtc->x;
1492 crtc_resp->y = crtc->y;
1493 crtc_resp->gamma_size = crtc->gamma_size;
1495 crtc_resp->fb_id = crtc->fb->base.id;
1497 crtc_resp->fb_id = 0;
1499 if (crtc->enabled) {
1501 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1502 crtc_resp->mode_valid = 1;
1505 crtc_resp->mode_valid = 0;
1509 drm_modeset_unlock_all(dev);
1514 * drm_mode_getconnector - get connector configuration
1515 * @dev: drm device for the ioctl
1516 * @data: data pointer for the ioctl
1517 * @file_priv: drm file for the ioctl call
1519 * Construct a connector configuration structure to return to the user.
1521 * Called by the user via ioctl.
1524 * Zero on success, errno on failure.
1526 int drm_mode_getconnector(struct drm_device *dev, void *data,
1527 struct drm_file *file_priv)
1529 struct drm_mode_get_connector *out_resp = data;
1530 struct drm_mode_object *obj;
1531 struct drm_connector *connector;
1532 struct drm_display_mode *mode;
1534 int props_count = 0;
1535 int encoders_count = 0;
1539 struct drm_mode_modeinfo u_mode;
1540 struct drm_mode_modeinfo __user *mode_ptr;
1541 uint32_t __user *prop_ptr;
1542 uint64_t __user *prop_values;
1543 uint32_t __user *encoder_ptr;
1545 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1548 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1550 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1552 mutex_lock(&dev->mode_config.mutex);
1554 obj = drm_mode_object_find(dev, out_resp->connector_id,
1555 DRM_MODE_OBJECT_CONNECTOR);
1560 connector = obj_to_connector(obj);
1562 props_count = connector->properties.count;
1564 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1565 if (connector->encoder_ids[i] != 0) {
1570 if (out_resp->count_modes == 0) {
1571 connector->funcs->fill_modes(connector,
1572 dev->mode_config.max_width,
1573 dev->mode_config.max_height);
1576 /* delayed so we get modes regardless of pre-fill_modes state */
1577 list_for_each_entry(mode, &connector->modes, head)
1580 out_resp->connector_id = connector->base.id;
1581 out_resp->connector_type = connector->connector_type;
1582 out_resp->connector_type_id = connector->connector_type_id;
1583 out_resp->mm_width = connector->display_info.width_mm;
1584 out_resp->mm_height = connector->display_info.height_mm;
1585 out_resp->subpixel = connector->display_info.subpixel_order;
1586 out_resp->connection = connector->status;
1587 if (connector->encoder)
1588 out_resp->encoder_id = connector->encoder->base.id;
1590 out_resp->encoder_id = 0;
1593 * This ioctl is called twice, once to determine how much space is
1594 * needed, and the 2nd time to fill it.
1596 if ((out_resp->count_modes >= mode_count) && mode_count) {
1598 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1599 list_for_each_entry(mode, &connector->modes, head) {
1600 drm_crtc_convert_to_umode(&u_mode, mode);
1601 if (copy_to_user(mode_ptr + copied,
1602 &u_mode, sizeof(u_mode))) {
1609 out_resp->count_modes = mode_count;
1611 if ((out_resp->count_props >= props_count) && props_count) {
1613 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1614 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1615 for (i = 0; i < connector->properties.count; i++) {
1616 if (put_user(connector->properties.ids[i],
1617 prop_ptr + copied)) {
1622 if (put_user(connector->properties.values[i],
1623 prop_values + copied)) {
1630 out_resp->count_props = props_count;
1632 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1634 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1635 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1636 if (connector->encoder_ids[i] != 0) {
1637 if (put_user(connector->encoder_ids[i],
1638 encoder_ptr + copied)) {
1646 out_resp->count_encoders = encoders_count;
1649 mutex_unlock(&dev->mode_config.mutex);
1654 int drm_mode_getencoder(struct drm_device *dev, void *data,
1655 struct drm_file *file_priv)
1657 struct drm_mode_get_encoder *enc_resp = data;
1658 struct drm_mode_object *obj;
1659 struct drm_encoder *encoder;
1662 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1665 drm_modeset_lock_all(dev);
1666 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1667 DRM_MODE_OBJECT_ENCODER);
1672 encoder = obj_to_encoder(obj);
1675 enc_resp->crtc_id = encoder->crtc->base.id;
1677 enc_resp->crtc_id = 0;
1678 enc_resp->encoder_type = encoder->encoder_type;
1679 enc_resp->encoder_id = encoder->base.id;
1680 enc_resp->possible_crtcs = encoder->possible_crtcs;
1681 enc_resp->possible_clones = encoder->possible_clones;
1684 drm_modeset_unlock_all(dev);
1689 * drm_mode_getplane_res - get plane info
1692 * @file_priv: DRM file info
1694 * Return an plane count and set of IDs.
1696 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1697 struct drm_file *file_priv)
1699 struct drm_mode_get_plane_res *plane_resp = data;
1700 struct drm_mode_config *config;
1701 struct drm_plane *plane;
1702 uint32_t __user *plane_ptr;
1703 int copied = 0, ret = 0;
1705 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1708 drm_modeset_lock_all(dev);
1709 config = &dev->mode_config;
1712 * This ioctl is called twice, once to determine how much space is
1713 * needed, and the 2nd time to fill it.
1715 if (config->num_plane &&
1716 (plane_resp->count_planes >= config->num_plane)) {
1717 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1719 list_for_each_entry(plane, &config->plane_list, head) {
1720 if (put_user(plane->base.id, plane_ptr + copied)) {
1727 plane_resp->count_planes = config->num_plane;
1730 drm_modeset_unlock_all(dev);
1735 * drm_mode_getplane - get plane info
1738 * @file_priv: DRM file info
1740 * Return plane info, including formats supported, gamma size, any
1743 int drm_mode_getplane(struct drm_device *dev, void *data,
1744 struct drm_file *file_priv)
1746 struct drm_mode_get_plane *plane_resp = data;
1747 struct drm_mode_object *obj;
1748 struct drm_plane *plane;
1749 uint32_t __user *format_ptr;
1752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1755 drm_modeset_lock_all(dev);
1756 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1757 DRM_MODE_OBJECT_PLANE);
1762 plane = obj_to_plane(obj);
1765 plane_resp->crtc_id = plane->crtc->base.id;
1767 plane_resp->crtc_id = 0;
1770 plane_resp->fb_id = plane->fb->base.id;
1772 plane_resp->fb_id = 0;
1774 plane_resp->plane_id = plane->base.id;
1775 plane_resp->possible_crtcs = plane->possible_crtcs;
1776 plane_resp->gamma_size = plane->gamma_size;
1779 * This ioctl is called twice, once to determine how much space is
1780 * needed, and the 2nd time to fill it.
1782 if (plane->format_count &&
1783 (plane_resp->count_format_types >= plane->format_count)) {
1784 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1785 if (copy_to_user(format_ptr,
1786 plane->format_types,
1787 sizeof(uint32_t) * plane->format_count)) {
1792 plane_resp->count_format_types = plane->format_count;
1795 drm_modeset_unlock_all(dev);
1800 * drm_mode_setplane - set up or tear down an plane
1802 * @data: ioctl data*
1803 * @file_priv: DRM file info
1805 * Set plane info, including placement, fb, scaling, and other factors.
1806 * Or pass a NULL fb to disable.
1808 int drm_mode_setplane(struct drm_device *dev, void *data,
1809 struct drm_file *file_priv)
1811 struct drm_mode_set_plane *plane_req = data;
1812 struct drm_mode_object *obj;
1813 struct drm_plane *plane;
1814 struct drm_crtc *crtc;
1815 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1817 unsigned int fb_width, fb_height;
1820 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1824 * First, find the plane, crtc, and fb objects. If not available,
1825 * we don't bother to call the driver.
1827 obj = drm_mode_object_find(dev, plane_req->plane_id,
1828 DRM_MODE_OBJECT_PLANE);
1830 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1831 plane_req->plane_id);
1834 plane = obj_to_plane(obj);
1836 /* No fb means shut it down */
1837 if (!plane_req->fb_id) {
1838 drm_modeset_lock_all(dev);
1840 plane->funcs->disable_plane(plane);
1843 drm_modeset_unlock_all(dev);
1847 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1848 DRM_MODE_OBJECT_CRTC);
1850 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1851 plane_req->crtc_id);
1855 crtc = obj_to_crtc(obj);
1857 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1859 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1865 /* Check whether this plane supports the fb pixel format. */
1866 for (i = 0; i < plane->format_count; i++)
1867 if (fb->pixel_format == plane->format_types[i])
1869 if (i == plane->format_count) {
1870 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1875 fb_width = fb->width << 16;
1876 fb_height = fb->height << 16;
1878 /* Make sure source coordinates are inside the fb. */
1879 if (plane_req->src_w > fb_width ||
1880 plane_req->src_x > fb_width - plane_req->src_w ||
1881 plane_req->src_h > fb_height ||
1882 plane_req->src_y > fb_height - plane_req->src_h) {
1883 DRM_DEBUG_KMS("Invalid source coordinates "
1884 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1885 plane_req->src_w >> 16,
1886 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1887 plane_req->src_h >> 16,
1888 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1889 plane_req->src_x >> 16,
1890 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1891 plane_req->src_y >> 16,
1892 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1897 /* Give drivers some help against integer overflows */
1898 if (plane_req->crtc_w > INT_MAX ||
1899 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1900 plane_req->crtc_h > INT_MAX ||
1901 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1902 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1903 plane_req->crtc_w, plane_req->crtc_h,
1904 plane_req->crtc_x, plane_req->crtc_y);
1909 drm_modeset_lock_all(dev);
1910 ret = plane->funcs->update_plane(plane, crtc, fb,
1911 plane_req->crtc_x, plane_req->crtc_y,
1912 plane_req->crtc_w, plane_req->crtc_h,
1913 plane_req->src_x, plane_req->src_y,
1914 plane_req->src_w, plane_req->src_h);
1921 drm_modeset_unlock_all(dev);
1925 drm_framebuffer_unreference(fb);
1927 drm_framebuffer_unreference(old_fb);
1933 * drm_mode_set_config_internal - helper to call ->set_config
1934 * @set: modeset config to set
1936 * This is a little helper to wrap internal calls to the ->set_config driver
1937 * interface. The only thing it adds is correct refcounting dance.
1939 int drm_mode_set_config_internal(struct drm_mode_set *set)
1941 struct drm_crtc *crtc = set->crtc;
1942 struct drm_framebuffer *fb, *old_fb;
1948 ret = crtc->funcs->set_config(set);
1951 drm_framebuffer_unreference(old_fb);
1953 drm_framebuffer_reference(fb);
1958 EXPORT_SYMBOL(drm_mode_set_config_internal);
1961 * drm_mode_setcrtc - set CRTC configuration
1962 * @dev: drm device for the ioctl
1963 * @data: data pointer for the ioctl
1964 * @file_priv: drm file for the ioctl call
1966 * Build a new CRTC configuration based on user request.
1968 * Called by the user via ioctl.
1971 * Zero on success, errno on failure.
1973 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1974 struct drm_file *file_priv)
1976 struct drm_mode_config *config = &dev->mode_config;
1977 struct drm_mode_crtc *crtc_req = data;
1978 struct drm_mode_object *obj;
1979 struct drm_crtc *crtc;
1980 struct drm_connector **connector_set = NULL, *connector;
1981 struct drm_framebuffer *fb = NULL;
1982 struct drm_display_mode *mode = NULL;
1983 struct drm_mode_set set;
1984 uint32_t __user *set_connectors_ptr;
1988 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1991 /* For some reason crtc x/y offsets are signed internally. */
1992 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1995 drm_modeset_lock_all(dev);
1996 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1997 DRM_MODE_OBJECT_CRTC);
1999 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2003 crtc = obj_to_crtc(obj);
2004 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2006 if (crtc_req->mode_valid) {
2007 int hdisplay, vdisplay;
2008 /* If we have a mode we need a framebuffer. */
2009 /* If we pass -1, set the mode with the currently bound fb */
2010 if (crtc_req->fb_id == -1) {
2012 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2017 /* Make refcounting symmetric with the lookup path. */
2018 drm_framebuffer_reference(fb);
2020 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2022 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2029 mode = drm_mode_create(dev);
2035 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2037 DRM_DEBUG_KMS("Invalid mode\n");
2041 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2043 hdisplay = mode->hdisplay;
2044 vdisplay = mode->vdisplay;
2046 if (crtc->invert_dimensions)
2047 swap(hdisplay, vdisplay);
2049 if (hdisplay > fb->width ||
2050 vdisplay > fb->height ||
2051 crtc_req->x > fb->width - hdisplay ||
2052 crtc_req->y > fb->height - vdisplay) {
2053 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2054 fb->width, fb->height,
2055 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2056 crtc->invert_dimensions ? " (inverted)" : "");
2062 if (crtc_req->count_connectors == 0 && mode) {
2063 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2068 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2069 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2070 crtc_req->count_connectors);
2075 if (crtc_req->count_connectors > 0) {
2078 /* Avoid unbounded kernel memory allocation */
2079 if (crtc_req->count_connectors > config->num_connector) {
2084 connector_set = kmalloc(crtc_req->count_connectors *
2085 sizeof(struct drm_connector *),
2087 if (!connector_set) {
2092 for (i = 0; i < crtc_req->count_connectors; i++) {
2093 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2094 if (get_user(out_id, &set_connectors_ptr[i])) {
2099 obj = drm_mode_object_find(dev, out_id,
2100 DRM_MODE_OBJECT_CONNECTOR);
2102 DRM_DEBUG_KMS("Connector id %d unknown\n",
2107 connector = obj_to_connector(obj);
2108 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2110 drm_get_connector_name(connector));
2112 connector_set[i] = connector;
2117 set.x = crtc_req->x;
2118 set.y = crtc_req->y;
2120 set.connectors = connector_set;
2121 set.num_connectors = crtc_req->count_connectors;
2123 ret = drm_mode_set_config_internal(&set);
2127 drm_framebuffer_unreference(fb);
2129 kfree(connector_set);
2130 drm_mode_destroy(dev, mode);
2131 drm_modeset_unlock_all(dev);
2135 int drm_mode_cursor_ioctl(struct drm_device *dev,
2136 void *data, struct drm_file *file_priv)
2138 struct drm_mode_cursor *req = data;
2139 struct drm_mode_object *obj;
2140 struct drm_crtc *crtc;
2143 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2146 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2149 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2151 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2154 crtc = obj_to_crtc(obj);
2156 mutex_lock(&crtc->mutex);
2157 if (req->flags & DRM_MODE_CURSOR_BO) {
2158 if (!crtc->funcs->cursor_set) {
2162 /* Turns off the cursor if handle is 0 */
2163 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2164 req->width, req->height);
2167 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2168 if (crtc->funcs->cursor_move) {
2169 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2176 mutex_unlock(&crtc->mutex);
2181 /* Original addfb only supported RGB formats, so figure out which one */
2182 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2188 fmt = DRM_FORMAT_C8;
2192 fmt = DRM_FORMAT_XRGB1555;
2194 fmt = DRM_FORMAT_RGB565;
2197 fmt = DRM_FORMAT_RGB888;
2201 fmt = DRM_FORMAT_XRGB8888;
2202 else if (depth == 30)
2203 fmt = DRM_FORMAT_XRGB2101010;
2205 fmt = DRM_FORMAT_ARGB8888;
2208 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2209 fmt = DRM_FORMAT_XRGB8888;
2215 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2218 * drm_mode_addfb - add an FB to the graphics configuration
2219 * @dev: drm device for the ioctl
2220 * @data: data pointer for the ioctl
2221 * @file_priv: drm file for the ioctl call
2223 * Add a new FB to the specified CRTC, given a user request.
2225 * Called by the user via ioctl.
2228 * Zero on success, errno on failure.
2230 int drm_mode_addfb(struct drm_device *dev,
2231 void *data, struct drm_file *file_priv)
2233 struct drm_mode_fb_cmd *or = data;
2234 struct drm_mode_fb_cmd2 r = {};
2235 struct drm_mode_config *config = &dev->mode_config;
2236 struct drm_framebuffer *fb;
2239 /* Use new struct with format internally */
2240 r.fb_id = or->fb_id;
2241 r.width = or->width;
2242 r.height = or->height;
2243 r.pitches[0] = or->pitch;
2244 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2245 r.handles[0] = or->handle;
2247 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2250 if ((config->min_width > r.width) || (r.width > config->max_width))
2253 if ((config->min_height > r.height) || (r.height > config->max_height))
2256 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2258 DRM_DEBUG_KMS("could not create framebuffer\n");
2262 mutex_lock(&file_priv->fbs_lock);
2263 or->fb_id = fb->base.id;
2264 list_add(&fb->filp_head, &file_priv->fbs);
2265 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2266 mutex_unlock(&file_priv->fbs_lock);
2271 static int format_check(const struct drm_mode_fb_cmd2 *r)
2273 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2277 case DRM_FORMAT_RGB332:
2278 case DRM_FORMAT_BGR233:
2279 case DRM_FORMAT_XRGB4444:
2280 case DRM_FORMAT_XBGR4444:
2281 case DRM_FORMAT_RGBX4444:
2282 case DRM_FORMAT_BGRX4444:
2283 case DRM_FORMAT_ARGB4444:
2284 case DRM_FORMAT_ABGR4444:
2285 case DRM_FORMAT_RGBA4444:
2286 case DRM_FORMAT_BGRA4444:
2287 case DRM_FORMAT_XRGB1555:
2288 case DRM_FORMAT_XBGR1555:
2289 case DRM_FORMAT_RGBX5551:
2290 case DRM_FORMAT_BGRX5551:
2291 case DRM_FORMAT_ARGB1555:
2292 case DRM_FORMAT_ABGR1555:
2293 case DRM_FORMAT_RGBA5551:
2294 case DRM_FORMAT_BGRA5551:
2295 case DRM_FORMAT_RGB565:
2296 case DRM_FORMAT_BGR565:
2297 case DRM_FORMAT_RGB888:
2298 case DRM_FORMAT_BGR888:
2299 case DRM_FORMAT_XRGB8888:
2300 case DRM_FORMAT_XBGR8888:
2301 case DRM_FORMAT_RGBX8888:
2302 case DRM_FORMAT_BGRX8888:
2303 case DRM_FORMAT_ARGB8888:
2304 case DRM_FORMAT_ABGR8888:
2305 case DRM_FORMAT_RGBA8888:
2306 case DRM_FORMAT_BGRA8888:
2307 case DRM_FORMAT_XRGB2101010:
2308 case DRM_FORMAT_XBGR2101010:
2309 case DRM_FORMAT_RGBX1010102:
2310 case DRM_FORMAT_BGRX1010102:
2311 case DRM_FORMAT_ARGB2101010:
2312 case DRM_FORMAT_ABGR2101010:
2313 case DRM_FORMAT_RGBA1010102:
2314 case DRM_FORMAT_BGRA1010102:
2315 case DRM_FORMAT_YUYV:
2316 case DRM_FORMAT_YVYU:
2317 case DRM_FORMAT_UYVY:
2318 case DRM_FORMAT_VYUY:
2319 case DRM_FORMAT_AYUV:
2320 case DRM_FORMAT_NV12:
2321 case DRM_FORMAT_NV21:
2322 case DRM_FORMAT_NV16:
2323 case DRM_FORMAT_NV61:
2324 case DRM_FORMAT_NV24:
2325 case DRM_FORMAT_NV42:
2326 case DRM_FORMAT_YUV410:
2327 case DRM_FORMAT_YVU410:
2328 case DRM_FORMAT_YUV411:
2329 case DRM_FORMAT_YVU411:
2330 case DRM_FORMAT_YUV420:
2331 case DRM_FORMAT_YVU420:
2332 case DRM_FORMAT_YUV422:
2333 case DRM_FORMAT_YVU422:
2334 case DRM_FORMAT_YUV444:
2335 case DRM_FORMAT_YVU444:
2342 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2344 int ret, hsub, vsub, num_planes, i;
2346 ret = format_check(r);
2348 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2352 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2353 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2354 num_planes = drm_format_num_planes(r->pixel_format);
2356 if (r->width == 0 || r->width % hsub) {
2357 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2361 if (r->height == 0 || r->height % vsub) {
2362 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2366 for (i = 0; i < num_planes; i++) {
2367 unsigned int width = r->width / (i != 0 ? hsub : 1);
2368 unsigned int height = r->height / (i != 0 ? vsub : 1);
2369 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2371 if (!r->handles[i]) {
2372 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2376 if ((uint64_t) width * cpp > UINT_MAX)
2379 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2382 if (r->pitches[i] < width * cpp) {
2383 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2392 * drm_mode_addfb2 - add an FB to the graphics configuration
2393 * @dev: drm device for the ioctl
2394 * @data: data pointer for the ioctl
2395 * @file_priv: drm file for the ioctl call
2397 * Add a new FB to the specified CRTC, given a user request with format.
2399 * Called by the user via ioctl.
2402 * Zero on success, errno on failure.
2404 int drm_mode_addfb2(struct drm_device *dev,
2405 void *data, struct drm_file *file_priv)
2407 struct drm_mode_fb_cmd2 *r = data;
2408 struct drm_mode_config *config = &dev->mode_config;
2409 struct drm_framebuffer *fb;
2412 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2415 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2416 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2420 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2421 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2422 r->width, config->min_width, config->max_width);
2425 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2426 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2427 r->height, config->min_height, config->max_height);
2431 ret = framebuffer_check(r);
2435 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2437 DRM_DEBUG_KMS("could not create framebuffer\n");
2441 mutex_lock(&file_priv->fbs_lock);
2442 r->fb_id = fb->base.id;
2443 list_add(&fb->filp_head, &file_priv->fbs);
2444 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2445 mutex_unlock(&file_priv->fbs_lock);
2452 * drm_mode_rmfb - remove an FB from the configuration
2453 * @dev: drm device for the ioctl
2454 * @data: data pointer for the ioctl
2455 * @file_priv: drm file for the ioctl call
2457 * Remove the FB specified by the user.
2459 * Called by the user via ioctl.
2462 * Zero on success, errno on failure.
2464 int drm_mode_rmfb(struct drm_device *dev,
2465 void *data, struct drm_file *file_priv)
2467 struct drm_framebuffer *fb = NULL;
2468 struct drm_framebuffer *fbl = NULL;
2469 uint32_t *id = data;
2472 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2475 mutex_lock(&file_priv->fbs_lock);
2476 mutex_lock(&dev->mode_config.fb_lock);
2477 fb = __drm_framebuffer_lookup(dev, *id);
2481 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2487 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2488 __drm_framebuffer_unregister(dev, fb);
2490 list_del_init(&fb->filp_head);
2491 mutex_unlock(&dev->mode_config.fb_lock);
2492 mutex_unlock(&file_priv->fbs_lock);
2494 drm_framebuffer_remove(fb);
2499 mutex_unlock(&dev->mode_config.fb_lock);
2500 mutex_unlock(&file_priv->fbs_lock);
2506 * drm_mode_getfb - get FB info
2507 * @dev: drm device for the ioctl
2508 * @data: data pointer for the ioctl
2509 * @file_priv: drm file for the ioctl call
2511 * Lookup the FB given its ID and return info about it.
2513 * Called by the user via ioctl.
2516 * Zero on success, errno on failure.
2518 int drm_mode_getfb(struct drm_device *dev,
2519 void *data, struct drm_file *file_priv)
2521 struct drm_mode_fb_cmd *r = data;
2522 struct drm_framebuffer *fb;
2525 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2528 fb = drm_framebuffer_lookup(dev, r->fb_id);
2532 r->height = fb->height;
2533 r->width = fb->width;
2534 r->depth = fb->depth;
2535 r->bpp = fb->bits_per_pixel;
2536 r->pitch = fb->pitches[0];
2537 if (fb->funcs->create_handle)
2538 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2542 drm_framebuffer_unreference(fb);
2547 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2548 void *data, struct drm_file *file_priv)
2550 struct drm_clip_rect __user *clips_ptr;
2551 struct drm_clip_rect *clips = NULL;
2552 struct drm_mode_fb_dirty_cmd *r = data;
2553 struct drm_framebuffer *fb;
2558 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2561 fb = drm_framebuffer_lookup(dev, r->fb_id);
2565 num_clips = r->num_clips;
2566 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2568 if (!num_clips != !clips_ptr) {
2573 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2575 /* If userspace annotates copy, clips must come in pairs */
2576 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2581 if (num_clips && clips_ptr) {
2582 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2586 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2592 ret = copy_from_user(clips, clips_ptr,
2593 num_clips * sizeof(*clips));
2600 if (fb->funcs->dirty) {
2601 drm_modeset_lock_all(dev);
2602 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2604 drm_modeset_unlock_all(dev);
2612 drm_framebuffer_unreference(fb);
2619 * drm_fb_release - remove and free the FBs on this file
2620 * @priv: drm file for the ioctl
2622 * Destroy all the FBs associated with @filp.
2624 * Called by the user via ioctl.
2627 * Zero on success, errno on failure.
2629 void drm_fb_release(struct drm_file *priv)
2632 struct drm_device *dev = priv->minor->dev;
2634 struct drm_device *dev = priv->dev;
2636 struct drm_framebuffer *fb, *tfb;
2638 mutex_lock(&priv->fbs_lock);
2639 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2641 mutex_lock(&dev->mode_config.fb_lock);
2642 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2643 __drm_framebuffer_unregister(dev, fb);
2644 mutex_unlock(&dev->mode_config.fb_lock);
2646 list_del_init(&fb->filp_head);
2648 /* This will also drop the fpriv->fbs reference. */
2649 drm_framebuffer_remove(fb);
2651 mutex_unlock(&priv->fbs_lock);
2654 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2655 const char *name, int num_values)
2657 struct drm_property *property = NULL;
2660 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2665 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2666 if (!property->values)
2670 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2674 property->flags = flags;
2675 property->num_values = num_values;
2676 INIT_LIST_HEAD(&property->enum_blob_list);
2679 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2680 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2683 list_add_tail(&property->head, &dev->mode_config.property_list);
2686 kfree(property->values);
2690 EXPORT_SYMBOL(drm_property_create);
2692 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2694 const struct drm_prop_enum_list *props,
2697 struct drm_property *property;
2700 flags |= DRM_MODE_PROP_ENUM;
2702 property = drm_property_create(dev, flags, name, num_values);
2706 for (i = 0; i < num_values; i++) {
2707 ret = drm_property_add_enum(property, i,
2711 drm_property_destroy(dev, property);
2718 EXPORT_SYMBOL(drm_property_create_enum);
2720 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2721 int flags, const char *name,
2722 const struct drm_prop_enum_list *props,
2725 struct drm_property *property;
2728 flags |= DRM_MODE_PROP_BITMASK;
2730 property = drm_property_create(dev, flags, name, num_values);
2734 for (i = 0; i < num_values; i++) {
2735 ret = drm_property_add_enum(property, i,
2739 drm_property_destroy(dev, property);
2746 EXPORT_SYMBOL(drm_property_create_bitmask);
2748 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2750 uint64_t min, uint64_t max)
2752 struct drm_property *property;
2754 flags |= DRM_MODE_PROP_RANGE;
2756 property = drm_property_create(dev, flags, name, 2);
2760 property->values[0] = min;
2761 property->values[1] = max;
2765 EXPORT_SYMBOL(drm_property_create_range);
2767 int drm_property_add_enum(struct drm_property *property, int index,
2768 uint64_t value, const char *name)
2770 struct drm_property_enum *prop_enum;
2772 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2776 * Bitmask enum properties have the additional constraint of values
2779 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2782 if (!list_empty(&property->enum_blob_list)) {
2783 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2784 if (prop_enum->value == value) {
2785 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2786 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2792 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2796 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2797 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2798 prop_enum->value = value;
2800 property->values[index] = value;
2801 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2804 EXPORT_SYMBOL(drm_property_add_enum);
2806 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2808 struct drm_property_enum *prop_enum, *pt;
2810 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2811 list_del(&prop_enum->head);
2815 if (property->num_values)
2816 kfree(property->values);
2817 drm_mode_object_put(dev, &property->base);
2818 list_del(&property->head);
2821 EXPORT_SYMBOL(drm_property_destroy);
2823 void drm_object_attach_property(struct drm_mode_object *obj,
2824 struct drm_property *property,
2827 int count = obj->properties->count;
2829 if (count == DRM_OBJECT_MAX_PROPERTY) {
2830 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2831 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2832 "you see this message on the same object type.\n",
2837 obj->properties->ids[count] = property->base.id;
2838 obj->properties->values[count] = init_val;
2839 obj->properties->count++;
2841 EXPORT_SYMBOL(drm_object_attach_property);
2843 int drm_object_property_set_value(struct drm_mode_object *obj,
2844 struct drm_property *property, uint64_t val)
2848 for (i = 0; i < obj->properties->count; i++) {
2849 if (obj->properties->ids[i] == property->base.id) {
2850 obj->properties->values[i] = val;
2857 EXPORT_SYMBOL(drm_object_property_set_value);
2859 int drm_object_property_get_value(struct drm_mode_object *obj,
2860 struct drm_property *property, uint64_t *val)
2864 for (i = 0; i < obj->properties->count; i++) {
2865 if (obj->properties->ids[i] == property->base.id) {
2866 *val = obj->properties->values[i];
2873 EXPORT_SYMBOL(drm_object_property_get_value);
2875 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2876 void *data, struct drm_file *file_priv)
2878 struct drm_mode_object *obj;
2879 struct drm_mode_get_property *out_resp = data;
2880 struct drm_property *property;
2883 int value_count = 0;
2886 struct drm_property_enum *prop_enum;
2887 struct drm_mode_property_enum __user *enum_ptr;
2888 struct drm_property_blob *prop_blob;
2889 uint32_t __user *blob_id_ptr;
2890 uint64_t __user *values_ptr;
2891 uint32_t __user *blob_length_ptr;
2893 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2896 drm_modeset_lock_all(dev);
2897 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2902 property = obj_to_property(obj);
2904 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2905 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2907 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2908 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2912 value_count = property->num_values;
2914 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2915 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2916 out_resp->flags = property->flags;
2918 if ((out_resp->count_values >= value_count) && value_count) {
2919 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2920 for (i = 0; i < value_count; i++) {
2921 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2927 out_resp->count_values = value_count;
2929 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2930 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2932 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2933 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2935 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2940 if (copy_to_user(&enum_ptr[copied].name,
2941 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2948 out_resp->count_enum_blobs = enum_count;
2951 if (property->flags & DRM_MODE_PROP_BLOB) {
2952 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2954 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2955 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
2957 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2958 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2963 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2971 out_resp->count_enum_blobs = blob_count;
2974 drm_modeset_unlock_all(dev);
2978 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2981 struct drm_property_blob *blob;
2984 if (!length || !data)
2987 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2991 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2997 blob->length = length;
2999 memcpy(blob->data, data, length);
3001 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3005 static void drm_property_destroy_blob(struct drm_device *dev,
3006 struct drm_property_blob *blob)
3008 drm_mode_object_put(dev, &blob->base);
3009 list_del(&blob->head);
3013 int drm_mode_getblob_ioctl(struct drm_device *dev,
3014 void *data, struct drm_file *file_priv)
3016 struct drm_mode_object *obj;
3017 struct drm_mode_get_blob *out_resp = data;
3018 struct drm_property_blob *blob;
3020 void __user *blob_ptr;
3022 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3025 drm_modeset_lock_all(dev);
3026 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3031 blob = obj_to_blob(obj);
3033 if (out_resp->length == blob->length) {
3034 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3035 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3040 out_resp->length = blob->length;
3043 drm_modeset_unlock_all(dev);
3047 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3050 struct drm_device *dev = connector->dev;
3053 if (connector->edid_blob_ptr)
3054 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3056 /* Delete edid, when there is none. */
3058 connector->edid_blob_ptr = NULL;
3059 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3063 size = EDID_LENGTH * (1 + edid->extensions);
3064 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3066 if (!connector->edid_blob_ptr)
3069 ret = drm_object_property_set_value(&connector->base,
3070 dev->mode_config.edid_property,
3071 connector->edid_blob_ptr->base.id);
3075 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3077 static bool drm_property_change_is_valid(struct drm_property *property,
3080 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3082 if (property->flags & DRM_MODE_PROP_RANGE) {
3083 if (value < property->values[0] || value > property->values[1])
3086 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3088 uint64_t valid_mask = 0;
3089 for (i = 0; i < property->num_values; i++)
3090 valid_mask |= (1ULL << property->values[i]);
3091 return !(value & ~valid_mask);
3092 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3093 /* Only the driver knows */
3097 for (i = 0; i < property->num_values; i++)
3098 if (property->values[i] == value)
3104 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3105 void *data, struct drm_file *file_priv)
3107 struct drm_mode_connector_set_property *conn_set_prop = data;
3108 struct drm_mode_obj_set_property obj_set_prop = {
3109 .value = conn_set_prop->value,
3110 .prop_id = conn_set_prop->prop_id,
3111 .obj_id = conn_set_prop->connector_id,
3112 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3115 /* It does all the locking and checking we need */
3116 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3119 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3120 struct drm_property *property,
3124 struct drm_connector *connector = obj_to_connector(obj);
3126 /* Do DPMS ourselves */
3127 if (property == connector->dev->mode_config.dpms_property) {
3128 if (connector->funcs->dpms)
3129 (*connector->funcs->dpms)(connector, (int)value);
3131 } else if (connector->funcs->set_property)
3132 ret = connector->funcs->set_property(connector, property, value);
3134 /* store the property value if successful */
3136 drm_object_property_set_value(&connector->base, property, value);
3140 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3141 struct drm_property *property,
3145 struct drm_crtc *crtc = obj_to_crtc(obj);
3147 if (crtc->funcs->set_property)
3148 ret = crtc->funcs->set_property(crtc, property, value);
3150 drm_object_property_set_value(obj, property, value);
3155 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3156 struct drm_property *property,
3160 struct drm_plane *plane = obj_to_plane(obj);
3162 if (plane->funcs->set_property)
3163 ret = plane->funcs->set_property(plane, property, value);
3165 drm_object_property_set_value(obj, property, value);
3170 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3171 struct drm_file *file_priv)
3173 struct drm_mode_obj_get_properties *arg = data;
3174 struct drm_mode_object *obj;
3178 int props_count = 0;
3179 uint32_t __user *props_ptr;
3180 uint64_t __user *prop_values_ptr;
3182 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3185 drm_modeset_lock_all(dev);
3187 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3192 if (!obj->properties) {
3197 props_count = obj->properties->count;
3199 /* This ioctl is called twice, once to determine how much space is
3200 * needed, and the 2nd time to fill it. */
3201 if ((arg->count_props >= props_count) && props_count) {
3203 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3204 prop_values_ptr = (uint64_t __user *)(unsigned long)
3205 (arg->prop_values_ptr);
3206 for (i = 0; i < props_count; i++) {
3207 if (put_user(obj->properties->ids[i],
3208 props_ptr + copied)) {
3212 if (put_user(obj->properties->values[i],
3213 prop_values_ptr + copied)) {
3220 arg->count_props = props_count;
3222 drm_modeset_unlock_all(dev);
3226 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3227 struct drm_file *file_priv)
3229 struct drm_mode_obj_set_property *arg = data;
3230 struct drm_mode_object *arg_obj;
3231 struct drm_mode_object *prop_obj;
3232 struct drm_property *property;
3236 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3239 drm_modeset_lock_all(dev);
3241 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3244 if (!arg_obj->properties)
3247 for (i = 0; i < arg_obj->properties->count; i++)
3248 if (arg_obj->properties->ids[i] == arg->prop_id)
3251 if (i == arg_obj->properties->count)
3254 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3255 DRM_MODE_OBJECT_PROPERTY);
3258 property = obj_to_property(prop_obj);
3260 if (!drm_property_change_is_valid(property, arg->value))
3263 switch (arg_obj->type) {
3264 case DRM_MODE_OBJECT_CONNECTOR:
3265 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3268 case DRM_MODE_OBJECT_CRTC:
3269 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3271 case DRM_MODE_OBJECT_PLANE:
3272 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3277 drm_modeset_unlock_all(dev);
3281 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3282 struct drm_encoder *encoder)
3286 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3287 if (connector->encoder_ids[i] == 0) {
3288 connector->encoder_ids[i] = encoder->base.id;
3294 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3296 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3297 struct drm_encoder *encoder)
3300 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3301 if (connector->encoder_ids[i] == encoder->base.id) {
3302 connector->encoder_ids[i] = 0;
3303 if (connector->encoder == encoder)
3304 connector->encoder = NULL;
3309 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3311 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3314 crtc->gamma_size = gamma_size;
3316 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3317 if (!crtc->gamma_store) {
3318 crtc->gamma_size = 0;
3324 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3326 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3327 void *data, struct drm_file *file_priv)
3329 struct drm_mode_crtc_lut *crtc_lut = data;
3330 struct drm_mode_object *obj;
3331 struct drm_crtc *crtc;
3332 void *r_base, *g_base, *b_base;
3336 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3339 drm_modeset_lock_all(dev);
3340 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3345 crtc = obj_to_crtc(obj);
3347 if (crtc->funcs->gamma_set == NULL) {
3352 /* memcpy into gamma store */
3353 if (crtc_lut->gamma_size != crtc->gamma_size) {
3358 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3359 r_base = crtc->gamma_store;
3360 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3365 g_base = (char *)r_base + size;
3366 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3371 b_base = (char *)r_base + size;
3372 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3377 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3380 drm_modeset_unlock_all(dev);
3385 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3386 void *data, struct drm_file *file_priv)
3388 struct drm_mode_crtc_lut *crtc_lut = data;
3389 struct drm_mode_object *obj;
3390 struct drm_crtc *crtc;
3391 void *r_base, *g_base, *b_base;
3395 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3398 drm_modeset_lock_all(dev);
3399 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3404 crtc = obj_to_crtc(obj);
3406 /* memcpy into gamma store */
3407 if (crtc_lut->gamma_size != crtc->gamma_size) {
3412 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3413 r_base = crtc->gamma_store;
3414 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3419 g_base = (char *)r_base + size;
3420 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3425 b_base = (char *)g_base + size;
3426 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3431 drm_modeset_unlock_all(dev);
3435 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3436 void *data, struct drm_file *file_priv)
3438 struct drm_mode_crtc_page_flip *page_flip = data;
3439 struct drm_mode_object *obj;
3440 struct drm_crtc *crtc;
3441 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3442 struct drm_pending_vblank_event *e = NULL;
3443 int hdisplay, vdisplay;
3446 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3447 page_flip->reserved != 0)
3450 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3453 crtc = obj_to_crtc(obj);
3455 mutex_lock(&crtc->mutex);
3456 if (crtc->fb == NULL) {
3457 /* The framebuffer is currently unbound, presumably
3458 * due to a hotplug event, that userspace has not
3465 if (crtc->funcs->page_flip == NULL)
3468 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3472 hdisplay = crtc->mode.hdisplay;
3473 vdisplay = crtc->mode.vdisplay;
3475 if (crtc->invert_dimensions)
3476 swap(hdisplay, vdisplay);
3478 if (hdisplay > fb->width ||
3479 vdisplay > fb->height ||
3480 crtc->x > fb->width - hdisplay ||
3481 crtc->y > fb->height - vdisplay) {
3482 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3483 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3484 crtc->invert_dimensions ? " (inverted)" : "");
3489 if (crtc->fb->pixel_format != fb->pixel_format) {
3490 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3495 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3497 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3498 if (file_priv->event_space < sizeof e->event) {
3499 lockmgr(&dev->event_lock, LK_RELEASE);
3502 file_priv->event_space -= sizeof e->event;
3503 lockmgr(&dev->event_lock, LK_RELEASE);
3505 e = kzalloc(sizeof *e, GFP_KERNEL);
3507 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3508 file_priv->event_space += sizeof e->event;
3509 lockmgr(&dev->event_lock, LK_RELEASE);
3513 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3514 e->event.base.length = sizeof e->event;
3515 e->event.user_data = page_flip->user_data;
3516 e->base.event = &e->event.base;
3517 e->base.file_priv = file_priv;
3519 (void (*) (struct drm_pending_event *)) kfree;
3523 ret = crtc->funcs->page_flip(crtc, fb, e);
3525 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3526 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3527 file_priv->event_space += sizeof e->event;
3528 lockmgr(&dev->event_lock, LK_RELEASE);
3531 /* Keep the old fb, don't unref it. */
3535 * Warn if the driver hasn't properly updated the crtc->fb
3536 * field to reflect that the new framebuffer is now used.
3537 * Failing to do so will screw with the reference counting
3540 WARN_ON(crtc->fb != fb);
3541 /* Unref only the old framebuffer. */
3547 drm_framebuffer_unreference(fb);
3549 drm_framebuffer_unreference(old_fb);
3550 mutex_unlock(&crtc->mutex);
3555 void drm_mode_config_reset(struct drm_device *dev)
3557 struct drm_crtc *crtc;
3558 struct drm_encoder *encoder;
3559 struct drm_connector *connector;
3561 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3562 if (crtc->funcs->reset)
3563 crtc->funcs->reset(crtc);
3565 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3566 if (encoder->funcs->reset)
3567 encoder->funcs->reset(encoder);
3569 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3570 connector->status = connector_status_unknown;
3572 if (connector->funcs->reset)
3573 connector->funcs->reset(connector);
3576 EXPORT_SYMBOL(drm_mode_config_reset);
3578 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3579 void *data, struct drm_file *file_priv)
3581 struct drm_mode_create_dumb *args = data;
3583 if (!dev->driver->dumb_create)
3585 return dev->driver->dumb_create(file_priv, dev, args);
3588 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3589 void *data, struct drm_file *file_priv)
3591 struct drm_mode_map_dumb *args = data;
3593 /* call driver ioctl to get mmap offset */
3594 if (!dev->driver->dumb_map_offset)
3597 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3600 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3601 void *data, struct drm_file *file_priv)
3603 struct drm_mode_destroy_dumb *args = data;
3605 if (!dev->driver->dumb_destroy)
3608 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3612 * Just need to support RGB formats here for compat with code that doesn't
3613 * use pixel formats directly yet.
3615 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3620 case DRM_FORMAT_RGB332:
3621 case DRM_FORMAT_BGR233:
3625 case DRM_FORMAT_XRGB1555:
3626 case DRM_FORMAT_XBGR1555:
3627 case DRM_FORMAT_RGBX5551:
3628 case DRM_FORMAT_BGRX5551:
3629 case DRM_FORMAT_ARGB1555:
3630 case DRM_FORMAT_ABGR1555:
3631 case DRM_FORMAT_RGBA5551:
3632 case DRM_FORMAT_BGRA5551:
3636 case DRM_FORMAT_RGB565:
3637 case DRM_FORMAT_BGR565:
3641 case DRM_FORMAT_RGB888:
3642 case DRM_FORMAT_BGR888:
3646 case DRM_FORMAT_XRGB8888:
3647 case DRM_FORMAT_XBGR8888:
3648 case DRM_FORMAT_RGBX8888:
3649 case DRM_FORMAT_BGRX8888:
3653 case DRM_FORMAT_XRGB2101010:
3654 case DRM_FORMAT_XBGR2101010:
3655 case DRM_FORMAT_RGBX1010102:
3656 case DRM_FORMAT_BGRX1010102:
3657 case DRM_FORMAT_ARGB2101010:
3658 case DRM_FORMAT_ABGR2101010:
3659 case DRM_FORMAT_RGBA1010102:
3660 case DRM_FORMAT_BGRA1010102:
3664 case DRM_FORMAT_ARGB8888:
3665 case DRM_FORMAT_ABGR8888:
3666 case DRM_FORMAT_RGBA8888:
3667 case DRM_FORMAT_BGRA8888:
3672 DRM_DEBUG_KMS("unsupported pixel format\n");
3678 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3681 * drm_format_num_planes - get the number of planes for format
3682 * @format: pixel format (DRM_FORMAT_*)
3685 * The number of planes used by the specified pixel format.
3687 int drm_format_num_planes(uint32_t format)
3690 case DRM_FORMAT_YUV410:
3691 case DRM_FORMAT_YVU410:
3692 case DRM_FORMAT_YUV411:
3693 case DRM_FORMAT_YVU411:
3694 case DRM_FORMAT_YUV420:
3695 case DRM_FORMAT_YVU420:
3696 case DRM_FORMAT_YUV422:
3697 case DRM_FORMAT_YVU422:
3698 case DRM_FORMAT_YUV444:
3699 case DRM_FORMAT_YVU444:
3701 case DRM_FORMAT_NV12:
3702 case DRM_FORMAT_NV21:
3703 case DRM_FORMAT_NV16:
3704 case DRM_FORMAT_NV61:
3705 case DRM_FORMAT_NV24:
3706 case DRM_FORMAT_NV42:
3712 EXPORT_SYMBOL(drm_format_num_planes);
3715 * drm_format_plane_cpp - determine the bytes per pixel value
3716 * @format: pixel format (DRM_FORMAT_*)
3717 * @plane: plane index
3720 * The bytes per pixel value for the specified plane.
3722 int drm_format_plane_cpp(uint32_t format, int plane)
3727 if (plane >= drm_format_num_planes(format))
3731 case DRM_FORMAT_YUYV:
3732 case DRM_FORMAT_YVYU:
3733 case DRM_FORMAT_UYVY:
3734 case DRM_FORMAT_VYUY:
3736 case DRM_FORMAT_NV12:
3737 case DRM_FORMAT_NV21:
3738 case DRM_FORMAT_NV16:
3739 case DRM_FORMAT_NV61:
3740 case DRM_FORMAT_NV24:
3741 case DRM_FORMAT_NV42:
3742 return plane ? 2 : 1;
3743 case DRM_FORMAT_YUV410:
3744 case DRM_FORMAT_YVU410:
3745 case DRM_FORMAT_YUV411:
3746 case DRM_FORMAT_YVU411:
3747 case DRM_FORMAT_YUV420:
3748 case DRM_FORMAT_YVU420:
3749 case DRM_FORMAT_YUV422:
3750 case DRM_FORMAT_YVU422:
3751 case DRM_FORMAT_YUV444:
3752 case DRM_FORMAT_YVU444:
3755 drm_fb_get_bpp_depth(format, &depth, &bpp);
3759 EXPORT_SYMBOL(drm_format_plane_cpp);
3762 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3763 * @format: pixel format (DRM_FORMAT_*)
3766 * The horizontal chroma subsampling factor for the
3767 * specified pixel format.
3769 int drm_format_horz_chroma_subsampling(uint32_t format)
3772 case DRM_FORMAT_YUV411:
3773 case DRM_FORMAT_YVU411:
3774 case DRM_FORMAT_YUV410:
3775 case DRM_FORMAT_YVU410:
3777 case DRM_FORMAT_YUYV:
3778 case DRM_FORMAT_YVYU:
3779 case DRM_FORMAT_UYVY:
3780 case DRM_FORMAT_VYUY:
3781 case DRM_FORMAT_NV12:
3782 case DRM_FORMAT_NV21:
3783 case DRM_FORMAT_NV16:
3784 case DRM_FORMAT_NV61:
3785 case DRM_FORMAT_YUV422:
3786 case DRM_FORMAT_YVU422:
3787 case DRM_FORMAT_YUV420:
3788 case DRM_FORMAT_YVU420:
3794 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3797 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3798 * @format: pixel format (DRM_FORMAT_*)
3801 * The vertical chroma subsampling factor for the
3802 * specified pixel format.
3804 int drm_format_vert_chroma_subsampling(uint32_t format)
3807 case DRM_FORMAT_YUV410:
3808 case DRM_FORMAT_YVU410:
3810 case DRM_FORMAT_YUV420:
3811 case DRM_FORMAT_YVU420:
3812 case DRM_FORMAT_NV12:
3813 case DRM_FORMAT_NV21:
3819 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3822 * drm_mode_config_init - initialize DRM mode_configuration structure
3825 * Initialize @dev's mode_config structure, used for tracking the graphics
3826 * configuration of @dev.
3828 * Since this initializes the modeset locks, no locking is possible. Which is no
3829 * problem, since this should happen single threaded at init time. It is the
3830 * driver's problem to ensure this guarantee.
3833 void drm_mode_config_init(struct drm_device *dev)
3835 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
3836 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
3837 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
3838 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3839 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3840 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3841 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3842 INIT_LIST_HEAD(&dev->mode_config.property_list);
3843 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3844 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3845 idr_init(&dev->mode_config.crtc_idr);
3847 drm_modeset_lock_all(dev);
3848 drm_mode_create_standard_connector_properties(dev);
3849 drm_modeset_unlock_all(dev);
3851 /* Just to be sure */
3852 dev->mode_config.num_fb = 0;
3853 dev->mode_config.num_connector = 0;
3854 dev->mode_config.num_crtc = 0;
3855 dev->mode_config.num_encoder = 0;
3857 EXPORT_SYMBOL(drm_mode_config_init);
3860 * drm_mode_config_cleanup - free up DRM mode_config info
3863 * Free up all the connectors and CRTCs associated with this DRM device, then
3864 * free up the framebuffers and associated buffer objects.
3866 * Note that since this /should/ happen single-threaded at driver/device
3867 * teardown time, no locking is required. It's the driver's job to ensure that
3868 * this guarantee actually holds true.
3870 * FIXME: cleanup any dangling user buffer objects too
3872 void drm_mode_config_cleanup(struct drm_device *dev)
3874 struct drm_connector *connector, *ot;
3875 struct drm_crtc *crtc, *ct;
3876 struct drm_encoder *encoder, *enct;
3877 struct drm_framebuffer *fb, *fbt;
3878 struct drm_property *property, *pt;
3879 struct drm_property_blob *blob, *bt;
3880 struct drm_plane *plane, *plt;
3882 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3884 encoder->funcs->destroy(encoder);
3887 list_for_each_entry_safe(connector, ot,
3888 &dev->mode_config.connector_list, head) {
3889 connector->funcs->destroy(connector);
3892 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3894 drm_property_destroy(dev, property);
3897 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3899 drm_property_destroy_blob(dev, blob);
3903 * Single-threaded teardown context, so it's not required to grab the
3904 * fb_lock to protect against concurrent fb_list access. Contrary, it
3905 * would actually deadlock with the drm_framebuffer_cleanup function.
3907 * Also, if there are any framebuffers left, that's a driver leak now,
3908 * so politely WARN about this.
3910 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3911 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3912 drm_framebuffer_remove(fb);
3915 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3917 plane->funcs->destroy(plane);
3920 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3921 crtc->funcs->destroy(crtc);
3924 idr_destroy(&dev->mode_config.crtc_idr);
3926 EXPORT_SYMBOL(drm_mode_config_cleanup);