2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
42 * drm_modeset_lock_all - take all modeset locks
45 * This function takes all modeset locks, suitable where a more fine-grained
46 * scheme isn't (yet) implemented.
48 void drm_modeset_lock_all(struct drm_device *dev)
50 struct drm_crtc *crtc;
52 mutex_lock(&dev->mode_config.mutex);
54 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
55 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
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;
82 /* Locking is currently fubar in the panic handler. */
86 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
87 WARN_ON(!mutex_is_locked(&crtc->mutex));
89 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
91 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
93 /* Avoid boilerplate. I'm tired of typing. */
94 #define DRM_ENUM_NAME_FN(fnname, list) \
95 const char *fnname(int val) \
98 for (i = 0; i < ARRAY_SIZE(list); i++) { \
99 if (list[i].type == val) \
100 return list[i].name; \
102 return "(unknown)"; \
108 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
109 { { DRM_MODE_DPMS_ON, "On" },
110 { DRM_MODE_DPMS_STANDBY, "Standby" },
111 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
112 { DRM_MODE_DPMS_OFF, "Off" }
115 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
118 * Optional properties
120 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
122 { DRM_MODE_SCALE_NONE, "None" },
123 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
124 { DRM_MODE_SCALE_CENTER, "Center" },
125 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
129 * Non-global properties, but "required" for certain connectors.
131 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
133 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
134 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
135 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
138 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
140 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
142 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* 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_subconnector_name,
148 drm_dvi_i_subconnector_enum_list)
150 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
152 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
153 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
154 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
155 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
159 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
161 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
163 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
164 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
165 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
166 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
170 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
171 drm_tv_subconnector_enum_list)
173 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
174 { DRM_MODE_DIRTY_OFF, "Off" },
175 { DRM_MODE_DIRTY_ON, "On" },
176 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
179 struct drm_conn_prop_enum_list {
186 * Connector and encoder types.
188 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
189 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
190 { DRM_MODE_CONNECTOR_VGA, "VGA" },
191 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
192 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
193 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
194 { DRM_MODE_CONNECTOR_Composite, "Composite" },
195 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
196 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
197 { DRM_MODE_CONNECTOR_Component, "Component" },
198 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
199 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
200 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
201 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
202 { DRM_MODE_CONNECTOR_TV, "TV" },
203 { DRM_MODE_CONNECTOR_eDP, "eDP" },
204 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
205 { DRM_MODE_CONNECTOR_DSI, "DSI" },
208 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
209 { { DRM_MODE_ENCODER_NONE, "None" },
210 { DRM_MODE_ENCODER_DAC, "DAC" },
211 { DRM_MODE_ENCODER_TMDS, "TMDS" },
212 { DRM_MODE_ENCODER_LVDS, "LVDS" },
213 { DRM_MODE_ENCODER_TVDAC, "TV" },
214 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
215 { DRM_MODE_ENCODER_DSI, "DSI" },
218 void drm_connector_ida_init(void)
222 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
223 ida_init(&drm_connector_enum_list[i].ida);
226 void drm_connector_ida_destroy(void)
230 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
231 ida_destroy(&drm_connector_enum_list[i].ida);
234 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
238 snprintf(buf, 32, "%s-%d",
239 drm_encoder_enum_list[encoder->encoder_type].name,
243 EXPORT_SYMBOL(drm_get_encoder_name);
245 const char *drm_get_connector_name(const struct drm_connector *connector)
249 snprintf(buf, 32, "%s-%d",
250 drm_connector_enum_list[connector->connector_type].name,
251 connector->connector_type_id);
254 EXPORT_SYMBOL(drm_get_connector_name);
256 const char *drm_get_connector_status_name(enum drm_connector_status status)
258 if (status == connector_status_connected)
260 else if (status == connector_status_disconnected)
261 return "disconnected";
265 EXPORT_SYMBOL(drm_get_connector_status_name);
267 static char printable_char(int c)
269 return isascii(c) && isprint(c) ? c : '?';
272 const char *drm_get_format_name(uint32_t format)
276 snprintf(buf, sizeof(buf),
277 "%c%c%c%c %s-endian (0x%08x)",
278 printable_char(format & 0xff),
279 printable_char((format >> 8) & 0xff),
280 printable_char((format >> 16) & 0xff),
281 printable_char((format >> 24) & 0x7f),
282 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
287 EXPORT_SYMBOL(drm_get_format_name);
290 * drm_mode_object_get - allocate a new modeset identifier
292 * @obj: object pointer, used to generate unique ID
293 * @obj_type: object type
295 * Create a unique identifier based on @ptr in @dev's identifier space. Used
296 * for tracking modes, CRTCs and connectors.
299 * New unique (relative to other objects in @dev) integer identifier for the
302 static int drm_mode_object_get(struct drm_device *dev,
303 struct drm_mode_object *obj, uint32_t obj_type)
307 mutex_lock(&dev->mode_config.idr_mutex);
308 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
311 * Set up the object linking under the protection of the idr
312 * lock so that other users can't see inconsistent state.
315 obj->type = obj_type;
317 mutex_unlock(&dev->mode_config.idr_mutex);
319 return ret < 0 ? ret : 0;
323 * drm_mode_object_put - free a modeset identifer
325 * @object: object to free
327 * Free @id from @dev's unique identifier pool.
329 static void drm_mode_object_put(struct drm_device *dev,
330 struct drm_mode_object *object)
332 mutex_lock(&dev->mode_config.idr_mutex);
333 idr_remove(&dev->mode_config.crtc_idr, object->id);
334 mutex_unlock(&dev->mode_config.idr_mutex);
338 * drm_mode_object_find - look up a drm object with static lifetime
340 * @id: id of the mode object
341 * @type: type of the mode object
343 * Note that framebuffers cannot be looked up with this functions - since those
344 * are reference counted, they need special treatment.
346 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
347 uint32_t id, uint32_t type)
349 struct drm_mode_object *obj = NULL;
351 /* Framebuffers are reference counted and need their own lookup
353 WARN_ON(type == DRM_MODE_OBJECT_FB);
355 mutex_lock(&dev->mode_config.idr_mutex);
356 obj = idr_find(&dev->mode_config.crtc_idr, id);
357 if (!obj || (obj->type != type) || (obj->id != id))
359 mutex_unlock(&dev->mode_config.idr_mutex);
363 EXPORT_SYMBOL(drm_mode_object_find);
366 * drm_framebuffer_init - initialize a framebuffer
368 * @fb: framebuffer to be initialized
369 * @funcs: ... with these functions
371 * Allocates an ID for the framebuffer's parent mode object, sets its mode
372 * functions & device file and adds it to the master fd list.
375 * This functions publishes the fb and makes it available for concurrent access
376 * by other users. Which means by this point the fb _must_ be fully set up -
377 * since all the fb attributes are invariant over its lifetime, no further
378 * locking but only correct reference counting is required.
381 * Zero on success, error code on failure.
383 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
384 const struct drm_framebuffer_funcs *funcs)
388 mutex_lock(&dev->mode_config.fb_lock);
389 kref_init(&fb->refcount);
390 INIT_LIST_HEAD(&fb->filp_head);
394 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
398 /* Grab the idr reference. */
399 drm_framebuffer_reference(fb);
401 dev->mode_config.num_fb++;
402 list_add(&fb->head, &dev->mode_config.fb_list);
404 mutex_unlock(&dev->mode_config.fb_lock);
408 EXPORT_SYMBOL(drm_framebuffer_init);
410 static void drm_framebuffer_free(struct kref *kref)
412 struct drm_framebuffer *fb =
413 container_of(kref, struct drm_framebuffer, refcount);
414 fb->funcs->destroy(fb);
417 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
420 struct drm_mode_object *obj = NULL;
421 struct drm_framebuffer *fb;
423 mutex_lock(&dev->mode_config.idr_mutex);
424 obj = idr_find(&dev->mode_config.crtc_idr, id);
425 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
429 mutex_unlock(&dev->mode_config.idr_mutex);
435 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
437 * @id: id of the fb object
439 * If successful, this grabs an additional reference to the framebuffer -
440 * callers need to make sure to eventually unreference the returned framebuffer
443 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
446 struct drm_framebuffer *fb;
448 mutex_lock(&dev->mode_config.fb_lock);
449 fb = __drm_framebuffer_lookup(dev, id);
451 drm_framebuffer_reference(fb);
452 mutex_unlock(&dev->mode_config.fb_lock);
456 EXPORT_SYMBOL(drm_framebuffer_lookup);
459 * drm_framebuffer_unreference - unref a framebuffer
460 * @fb: framebuffer to unref
462 * This functions decrements the fb's refcount and frees it if it drops to zero.
464 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
466 DRM_DEBUG("FB ID: %d\n", fb->base.id);
467 kref_put(&fb->refcount, drm_framebuffer_free);
469 EXPORT_SYMBOL(drm_framebuffer_unreference);
472 * drm_framebuffer_reference - incr the fb refcnt
475 void drm_framebuffer_reference(struct drm_framebuffer *fb)
477 DRM_DEBUG("FB ID: %d\n", fb->base.id);
478 kref_get(&fb->refcount);
480 EXPORT_SYMBOL(drm_framebuffer_reference);
482 static void drm_framebuffer_free_bug(struct kref *kref)
487 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
489 DRM_DEBUG("FB ID: %d\n", fb->base.id);
490 kref_put(&fb->refcount, drm_framebuffer_free_bug);
493 /* dev->mode_config.fb_lock must be held! */
494 static void __drm_framebuffer_unregister(struct drm_device *dev,
495 struct drm_framebuffer *fb)
497 mutex_lock(&dev->mode_config.idr_mutex);
498 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
499 mutex_unlock(&dev->mode_config.idr_mutex);
503 __drm_framebuffer_unreference(fb);
507 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
508 * @fb: fb to unregister
510 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
511 * those used for fbdev. Note that the caller must hold a reference of it's own,
512 * i.e. the object may not be destroyed through this call (since it'll lead to a
513 * locking inversion).
515 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
517 struct drm_device *dev = fb->dev;
519 mutex_lock(&dev->mode_config.fb_lock);
520 /* Mark fb as reaped and drop idr ref. */
521 __drm_framebuffer_unregister(dev, fb);
522 mutex_unlock(&dev->mode_config.fb_lock);
524 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
527 * drm_framebuffer_cleanup - remove a framebuffer object
528 * @fb: framebuffer to remove
530 * Cleanup references to a user-created framebuffer. This function is intended
531 * to be used from the drivers ->destroy callback.
533 * Note that this function does not remove the fb from active usuage - if it is
534 * still used anywhere, hilarity can ensue since userspace could call getfb on
535 * the id and get back -EINVAL. Obviously no concern at driver unload time.
537 * Also, the framebuffer will not be removed from the lookup idr - for
538 * user-created framebuffers this will happen in in the rmfb ioctl. For
539 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
540 * drm_framebuffer_unregister_private.
542 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
544 struct drm_device *dev = fb->dev;
546 mutex_lock(&dev->mode_config.fb_lock);
548 dev->mode_config.num_fb--;
549 mutex_unlock(&dev->mode_config.fb_lock);
551 EXPORT_SYMBOL(drm_framebuffer_cleanup);
554 * drm_framebuffer_remove - remove and unreference a framebuffer object
555 * @fb: framebuffer to remove
557 * Scans all the CRTCs and planes in @dev's mode_config. If they're
558 * using @fb, removes it, setting it to NULL. Then drops the reference to the
559 * passed-in framebuffer. Might take the modeset locks.
561 * Note that this function optimizes the cleanup away if the caller holds the
562 * last reference to the framebuffer. It is also guaranteed to not take the
563 * modeset locks in this case.
565 void drm_framebuffer_remove(struct drm_framebuffer *fb)
567 struct drm_device *dev = fb->dev;
568 struct drm_crtc *crtc;
569 struct drm_plane *plane;
570 struct drm_mode_set set;
573 WARN_ON(!list_empty(&fb->filp_head));
576 * drm ABI mandates that we remove any deleted framebuffers from active
577 * useage. But since most sane clients only remove framebuffers they no
578 * longer need, try to optimize this away.
580 * Since we're holding a reference ourselves, observing a refcount of 1
581 * means that we're the last holder and can skip it. Also, the refcount
582 * can never increase from 1 again, so we don't need any barriers or
585 * Note that userspace could try to race with use and instate a new
586 * usage _after_ we've cleared all current ones. End result will be an
587 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
590 if (atomic_read(&fb->refcount.refcount) > 1) {
591 drm_modeset_lock_all(dev);
592 /* remove from any CRTC */
593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
594 if (crtc->fb == fb) {
595 /* should turn off the crtc */
596 memset(&set, 0, sizeof(struct drm_mode_set));
599 ret = drm_mode_set_config_internal(&set);
601 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
605 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
607 drm_plane_force_disable(plane);
609 drm_modeset_unlock_all(dev);
612 drm_framebuffer_unreference(fb);
614 EXPORT_SYMBOL(drm_framebuffer_remove);
617 * drm_crtc_init - Initialise a new CRTC object
619 * @crtc: CRTC object to init
620 * @funcs: callbacks for the new CRTC
622 * Inits a new object created as base part of a driver crtc object.
625 * Zero on success, error code on failure.
627 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
628 const struct drm_crtc_funcs *funcs)
634 crtc->invert_dimensions = false;
636 drm_modeset_lock_all(dev);
637 mutex_init(&crtc->mutex);
638 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
640 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
644 crtc->base.properties = &crtc->properties;
646 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
647 dev->mode_config.num_crtc++;
650 drm_modeset_unlock_all(dev);
654 EXPORT_SYMBOL(drm_crtc_init);
657 * drm_crtc_cleanup - Clean up the core crtc usage
658 * @crtc: CRTC to cleanup
660 * This function cleans up @crtc and removes it from the DRM mode setting
661 * core. Note that the function does *not* free the crtc structure itself,
662 * this is the responsibility of the caller.
664 void drm_crtc_cleanup(struct drm_crtc *crtc)
666 struct drm_device *dev = crtc->dev;
668 kfree(crtc->gamma_store);
669 crtc->gamma_store = NULL;
671 drm_mode_object_put(dev, &crtc->base);
672 list_del(&crtc->head);
673 dev->mode_config.num_crtc--;
675 EXPORT_SYMBOL(drm_crtc_cleanup);
678 * drm_crtc_index - find the index of a registered CRTC
679 * @crtc: CRTC to find index for
681 * Given a registered CRTC, return the index of that CRTC within a DRM
682 * device's list of CRTCs.
684 unsigned int drm_crtc_index(struct drm_crtc *crtc)
686 unsigned int index = 0;
687 struct drm_crtc *tmp;
689 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
698 EXPORT_SYMBOL(drm_crtc_index);
701 * drm_mode_probed_add - add a mode to a connector's probed mode list
702 * @connector: connector the new mode
705 * Add @mode to @connector's mode list for later use.
707 void drm_mode_probed_add(struct drm_connector *connector,
708 struct drm_display_mode *mode)
710 list_add_tail(&mode->head, &connector->probed_modes);
712 EXPORT_SYMBOL(drm_mode_probed_add);
715 * drm_mode_remove - remove and free a mode
716 * @connector: connector list to modify
717 * @mode: mode to remove
719 * Remove @mode from @connector's mode list, then free it.
721 static void drm_mode_remove(struct drm_connector *connector,
722 struct drm_display_mode *mode)
724 list_del(&mode->head);
725 drm_mode_destroy(connector->dev, mode);
729 * drm_connector_init - Init a preallocated connector
731 * @connector: the connector to init
732 * @funcs: callbacks for this connector
733 * @connector_type: user visible type of the connector
735 * Initialises a preallocated connector. Connectors should be
736 * subclassed as part of driver connector objects.
739 * Zero on success, error code on failure.
741 int drm_connector_init(struct drm_device *dev,
742 struct drm_connector *connector,
743 const struct drm_connector_funcs *funcs,
747 struct ida *connector_ida =
748 &drm_connector_enum_list[connector_type].ida;
750 drm_modeset_lock_all(dev);
752 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
756 connector->base.properties = &connector->properties;
757 connector->dev = dev;
758 connector->funcs = funcs;
759 connector->connector_type = connector_type;
760 connector->connector_type_id =
761 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
762 if (connector->connector_type_id < 0) {
763 ret = connector->connector_type_id;
764 drm_mode_object_put(dev, &connector->base);
767 INIT_LIST_HEAD(&connector->probed_modes);
768 INIT_LIST_HEAD(&connector->modes);
769 connector->edid_blob_ptr = NULL;
770 connector->status = connector_status_unknown;
772 list_add_tail(&connector->head, &dev->mode_config.connector_list);
773 dev->mode_config.num_connector++;
775 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
776 drm_object_attach_property(&connector->base,
777 dev->mode_config.edid_property,
780 drm_object_attach_property(&connector->base,
781 dev->mode_config.dpms_property, 0);
784 drm_modeset_unlock_all(dev);
788 EXPORT_SYMBOL(drm_connector_init);
791 * drm_connector_cleanup - cleans up an initialised connector
792 * @connector: connector to cleanup
794 * Cleans up the connector but doesn't free the object.
796 void drm_connector_cleanup(struct drm_connector *connector)
798 struct drm_device *dev = connector->dev;
799 struct drm_display_mode *mode, *t;
801 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
802 drm_mode_remove(connector, mode);
804 list_for_each_entry_safe(mode, t, &connector->modes, head)
805 drm_mode_remove(connector, mode);
807 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
808 connector->connector_type_id);
810 drm_mode_object_put(dev, &connector->base);
811 list_del(&connector->head);
812 dev->mode_config.num_connector--;
814 EXPORT_SYMBOL(drm_connector_cleanup);
816 void drm_connector_unplug_all(struct drm_device *dev)
818 struct drm_connector *connector;
820 /* taking the mode config mutex ends up in a clash with sysfs */
821 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
822 drm_sysfs_connector_remove(connector);
825 EXPORT_SYMBOL(drm_connector_unplug_all);
827 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
828 const struct drm_bridge_funcs *funcs)
832 drm_modeset_lock_all(dev);
834 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
839 bridge->funcs = funcs;
841 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
842 dev->mode_config.num_bridge++;
845 drm_modeset_unlock_all(dev);
848 EXPORT_SYMBOL(drm_bridge_init);
850 void drm_bridge_cleanup(struct drm_bridge *bridge)
852 struct drm_device *dev = bridge->dev;
854 drm_modeset_lock_all(dev);
855 drm_mode_object_put(dev, &bridge->base);
856 list_del(&bridge->head);
857 dev->mode_config.num_bridge--;
858 drm_modeset_unlock_all(dev);
860 EXPORT_SYMBOL(drm_bridge_cleanup);
862 int drm_encoder_init(struct drm_device *dev,
863 struct drm_encoder *encoder,
864 const struct drm_encoder_funcs *funcs,
869 drm_modeset_lock_all(dev);
871 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
876 encoder->encoder_type = encoder_type;
877 encoder->funcs = funcs;
879 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
880 dev->mode_config.num_encoder++;
883 drm_modeset_unlock_all(dev);
887 EXPORT_SYMBOL(drm_encoder_init);
889 void drm_encoder_cleanup(struct drm_encoder *encoder)
891 struct drm_device *dev = encoder->dev;
892 drm_modeset_lock_all(dev);
893 drm_mode_object_put(dev, &encoder->base);
894 list_del(&encoder->head);
895 dev->mode_config.num_encoder--;
896 drm_modeset_unlock_all(dev);
898 EXPORT_SYMBOL(drm_encoder_cleanup);
901 * drm_plane_init - Initialise a new plane object
903 * @plane: plane object to init
904 * @possible_crtcs: bitmask of possible CRTCs
905 * @funcs: callbacks for the new plane
906 * @formats: array of supported formats (%DRM_FORMAT_*)
907 * @format_count: number of elements in @formats
908 * @priv: plane is private (hidden from userspace)?
910 * Inits a new object created as base part of a driver plane object.
913 * Zero on success, error code on failure.
915 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
916 unsigned long possible_crtcs,
917 const struct drm_plane_funcs *funcs,
918 const uint32_t *formats, uint32_t format_count,
923 drm_modeset_lock_all(dev);
925 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
929 plane->base.properties = &plane->properties;
931 plane->funcs = funcs;
932 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
934 if (!plane->format_types) {
935 DRM_DEBUG_KMS("out of memory when allocating plane\n");
936 drm_mode_object_put(dev, &plane->base);
941 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
942 plane->format_count = format_count;
943 plane->possible_crtcs = possible_crtcs;
945 /* private planes are not exposed to userspace, but depending on
946 * display hardware, might be convenient to allow sharing programming
947 * for the scanout engine with the crtc implementation.
950 list_add_tail(&plane->head, &dev->mode_config.plane_list);
951 dev->mode_config.num_plane++;
953 INIT_LIST_HEAD(&plane->head);
957 drm_modeset_unlock_all(dev);
961 EXPORT_SYMBOL(drm_plane_init);
964 * drm_plane_cleanup - Clean up the core plane usage
965 * @plane: plane to cleanup
967 * This function cleans up @plane and removes it from the DRM mode setting
968 * core. Note that the function does *not* free the plane structure itself,
969 * this is the responsibility of the caller.
971 void drm_plane_cleanup(struct drm_plane *plane)
973 struct drm_device *dev = plane->dev;
975 drm_modeset_lock_all(dev);
976 kfree(plane->format_types);
977 drm_mode_object_put(dev, &plane->base);
978 /* if not added to a list, it must be a private plane */
979 if (!list_empty(&plane->head)) {
980 list_del(&plane->head);
981 dev->mode_config.num_plane--;
983 drm_modeset_unlock_all(dev);
985 EXPORT_SYMBOL(drm_plane_cleanup);
988 * drm_plane_force_disable - Forcibly disable a plane
989 * @plane: plane to disable
991 * Forces the plane to be disabled.
993 * Used when the plane's current framebuffer is destroyed,
994 * and when restoring fbdev mode.
996 void drm_plane_force_disable(struct drm_plane *plane)
1003 ret = plane->funcs->disable_plane(plane);
1005 DRM_ERROR("failed to disable plane with busy fb\n");
1006 /* disconnect the plane from the fb and crtc: */
1007 __drm_framebuffer_unreference(plane->fb);
1011 EXPORT_SYMBOL(drm_plane_force_disable);
1014 * drm_mode_create - create a new display mode
1017 * Create a new drm_display_mode, give it an ID, and return it.
1020 * Pointer to new mode on success, NULL on error.
1022 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1024 struct drm_display_mode *nmode;
1026 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1030 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1037 EXPORT_SYMBOL(drm_mode_create);
1040 * drm_mode_destroy - remove a mode
1042 * @mode: mode to remove
1044 * Free @mode's unique identifier, then free it.
1046 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1051 drm_mode_object_put(dev, &mode->base);
1055 EXPORT_SYMBOL(drm_mode_destroy);
1057 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1059 struct drm_property *edid;
1060 struct drm_property *dpms;
1063 * Standard properties (apply to all connectors)
1065 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1066 DRM_MODE_PROP_IMMUTABLE,
1068 dev->mode_config.edid_property = edid;
1070 dpms = drm_property_create_enum(dev, 0,
1071 "DPMS", drm_dpms_enum_list,
1072 ARRAY_SIZE(drm_dpms_enum_list));
1073 dev->mode_config.dpms_property = dpms;
1079 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1082 * Called by a driver the first time a DVI-I connector is made.
1084 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1086 struct drm_property *dvi_i_selector;
1087 struct drm_property *dvi_i_subconnector;
1089 if (dev->mode_config.dvi_i_select_subconnector_property)
1093 drm_property_create_enum(dev, 0,
1094 "select subconnector",
1095 drm_dvi_i_select_enum_list,
1096 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1097 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1099 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1101 drm_dvi_i_subconnector_enum_list,
1102 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1103 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1107 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1110 * drm_create_tv_properties - create TV specific connector properties
1112 * @num_modes: number of different TV formats (modes) supported
1113 * @modes: array of pointers to strings containing name of each format
1115 * Called by a driver's TV initialization routine, this function creates
1116 * the TV specific connector properties for a given device. Caller is
1117 * responsible for allocating a list of format names and passing them to
1120 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1123 struct drm_property *tv_selector;
1124 struct drm_property *tv_subconnector;
1127 if (dev->mode_config.tv_select_subconnector_property)
1131 * Basic connector properties
1133 tv_selector = drm_property_create_enum(dev, 0,
1134 "select subconnector",
1135 drm_tv_select_enum_list,
1136 ARRAY_SIZE(drm_tv_select_enum_list));
1137 dev->mode_config.tv_select_subconnector_property = tv_selector;
1140 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1142 drm_tv_subconnector_enum_list,
1143 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1144 dev->mode_config.tv_subconnector_property = tv_subconnector;
1147 * Other, TV specific properties: margins & TV modes.
1149 dev->mode_config.tv_left_margin_property =
1150 drm_property_create_range(dev, 0, "left margin", 0, 100);
1152 dev->mode_config.tv_right_margin_property =
1153 drm_property_create_range(dev, 0, "right margin", 0, 100);
1155 dev->mode_config.tv_top_margin_property =
1156 drm_property_create_range(dev, 0, "top margin", 0, 100);
1158 dev->mode_config.tv_bottom_margin_property =
1159 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1161 dev->mode_config.tv_mode_property =
1162 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1164 for (i = 0; i < num_modes; i++)
1165 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1168 dev->mode_config.tv_brightness_property =
1169 drm_property_create_range(dev, 0, "brightness", 0, 100);
1171 dev->mode_config.tv_contrast_property =
1172 drm_property_create_range(dev, 0, "contrast", 0, 100);
1174 dev->mode_config.tv_flicker_reduction_property =
1175 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1177 dev->mode_config.tv_overscan_property =
1178 drm_property_create_range(dev, 0, "overscan", 0, 100);
1180 dev->mode_config.tv_saturation_property =
1181 drm_property_create_range(dev, 0, "saturation", 0, 100);
1183 dev->mode_config.tv_hue_property =
1184 drm_property_create_range(dev, 0, "hue", 0, 100);
1188 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1191 * drm_mode_create_scaling_mode_property - create scaling mode property
1194 * Called by a driver the first time it's needed, must be attached to desired
1197 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1199 struct drm_property *scaling_mode;
1201 if (dev->mode_config.scaling_mode_property)
1205 drm_property_create_enum(dev, 0, "scaling mode",
1206 drm_scaling_mode_enum_list,
1207 ARRAY_SIZE(drm_scaling_mode_enum_list));
1209 dev->mode_config.scaling_mode_property = scaling_mode;
1213 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1216 * drm_mode_create_dirty_property - create dirty property
1219 * Called by a driver the first time it's needed, must be attached to desired
1222 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1224 struct drm_property *dirty_info;
1226 if (dev->mode_config.dirty_info_property)
1230 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1232 drm_dirty_info_enum_list,
1233 ARRAY_SIZE(drm_dirty_info_enum_list));
1234 dev->mode_config.dirty_info_property = dirty_info;
1238 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1240 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1242 uint32_t total_objects = 0;
1244 total_objects += dev->mode_config.num_crtc;
1245 total_objects += dev->mode_config.num_connector;
1246 total_objects += dev->mode_config.num_encoder;
1247 total_objects += dev->mode_config.num_bridge;
1249 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1250 if (!group->id_list)
1253 group->num_crtcs = 0;
1254 group->num_connectors = 0;
1255 group->num_encoders = 0;
1256 group->num_bridges = 0;
1260 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1261 struct drm_mode_group *group)
1263 struct drm_crtc *crtc;
1264 struct drm_encoder *encoder;
1265 struct drm_connector *connector;
1266 struct drm_bridge *bridge;
1269 if ((ret = drm_mode_group_init(dev, group)))
1272 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1273 group->id_list[group->num_crtcs++] = crtc->base.id;
1275 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1276 group->id_list[group->num_crtcs + group->num_encoders++] =
1279 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1280 group->id_list[group->num_crtcs + group->num_encoders +
1281 group->num_connectors++] = connector->base.id;
1283 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1284 group->id_list[group->num_crtcs + group->num_encoders +
1285 group->num_connectors + group->num_bridges++] =
1290 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1293 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1294 * @out: drm_mode_modeinfo struct to return to the user
1295 * @in: drm_display_mode to use
1297 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1300 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1301 const struct drm_display_mode *in)
1303 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1304 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1305 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1306 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1307 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1308 "timing values too large for mode info\n");
1310 out->clock = in->clock;
1311 out->hdisplay = in->hdisplay;
1312 out->hsync_start = in->hsync_start;
1313 out->hsync_end = in->hsync_end;
1314 out->htotal = in->htotal;
1315 out->hskew = in->hskew;
1316 out->vdisplay = in->vdisplay;
1317 out->vsync_start = in->vsync_start;
1318 out->vsync_end = in->vsync_end;
1319 out->vtotal = in->vtotal;
1320 out->vscan = in->vscan;
1321 out->vrefresh = in->vrefresh;
1322 out->flags = in->flags;
1323 out->type = in->type;
1324 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1325 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1329 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1330 * @out: drm_display_mode to return to the user
1331 * @in: drm_mode_modeinfo to use
1333 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1337 * Zero on success, errno on failure.
1339 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1340 const struct drm_mode_modeinfo *in)
1342 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1345 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1348 out->clock = in->clock;
1349 out->hdisplay = in->hdisplay;
1350 out->hsync_start = in->hsync_start;
1351 out->hsync_end = in->hsync_end;
1352 out->htotal = in->htotal;
1353 out->hskew = in->hskew;
1354 out->vdisplay = in->vdisplay;
1355 out->vsync_start = in->vsync_start;
1356 out->vsync_end = in->vsync_end;
1357 out->vtotal = in->vtotal;
1358 out->vscan = in->vscan;
1359 out->vrefresh = in->vrefresh;
1360 out->flags = in->flags;
1361 out->type = in->type;
1362 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1363 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1369 * drm_mode_getresources - get graphics configuration
1370 * @dev: drm device for the ioctl
1371 * @data: data pointer for the ioctl
1372 * @file_priv: drm file for the ioctl call
1374 * Construct a set of configuration description structures and return
1375 * them to the user, including CRTC, connector and framebuffer configuration.
1377 * Called by the user via ioctl.
1380 * Zero on success, errno on failure.
1382 int drm_mode_getresources(struct drm_device *dev, void *data,
1383 struct drm_file *file_priv)
1385 struct drm_mode_card_res *card_res = data;
1386 struct list_head *lh;
1387 struct drm_framebuffer *fb;
1388 struct drm_connector *connector;
1389 struct drm_crtc *crtc;
1390 struct drm_encoder *encoder;
1392 int connector_count = 0;
1395 int encoder_count = 0;
1397 uint32_t __user *fb_id;
1398 uint32_t __user *crtc_id;
1399 uint32_t __user *connector_id;
1400 uint32_t __user *encoder_id;
1401 struct drm_mode_group *mode_group;
1403 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1407 mutex_lock(&file_priv->fbs_lock);
1409 * For the non-control nodes we need to limit the list of resources
1410 * by IDs in the group list for this node
1412 list_for_each(lh, &file_priv->fbs)
1415 /* handle this in 4 parts */
1417 if (card_res->count_fbs >= fb_count) {
1419 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1420 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1421 if (put_user(fb->base.id, fb_id + copied)) {
1422 mutex_unlock(&file_priv->fbs_lock);
1428 card_res->count_fbs = fb_count;
1429 mutex_unlock(&file_priv->fbs_lock);
1431 drm_modeset_lock_all(dev);
1432 if (file_priv->minor->type != DRM_MINOR_LEGACY) {
1435 list_for_each(lh, &dev->mode_config.crtc_list)
1438 list_for_each(lh, &dev->mode_config.connector_list)
1441 list_for_each(lh, &dev->mode_config.encoder_list)
1445 mode_group = &file_priv->master->minor->mode_group;
1446 crtc_count = mode_group->num_crtcs;
1447 connector_count = mode_group->num_connectors;
1448 encoder_count = mode_group->num_encoders;
1451 card_res->max_height = dev->mode_config.max_height;
1452 card_res->min_height = dev->mode_config.min_height;
1453 card_res->max_width = dev->mode_config.max_width;
1454 card_res->min_width = dev->mode_config.min_width;
1457 if (card_res->count_crtcs >= crtc_count) {
1459 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1461 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1463 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1464 if (put_user(crtc->base.id, crtc_id + copied)) {
1471 for (i = 0; i < mode_group->num_crtcs; i++) {
1472 if (put_user(mode_group->id_list[i],
1473 crtc_id + copied)) {
1481 card_res->count_crtcs = crtc_count;
1484 if (card_res->count_encoders >= encoder_count) {
1486 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1488 list_for_each_entry(encoder,
1489 &dev->mode_config.encoder_list,
1491 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1492 drm_get_encoder_name(encoder));
1493 if (put_user(encoder->base.id, encoder_id +
1501 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1502 if (put_user(mode_group->id_list[i],
1503 encoder_id + copied)) {
1512 card_res->count_encoders = encoder_count;
1515 if (card_res->count_connectors >= connector_count) {
1517 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1519 list_for_each_entry(connector,
1520 &dev->mode_config.connector_list,
1522 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1524 drm_get_connector_name(connector));
1525 if (put_user(connector->base.id,
1526 connector_id + copied)) {
1533 int start = mode_group->num_crtcs +
1534 mode_group->num_encoders;
1535 for (i = start; i < start + mode_group->num_connectors; i++) {
1536 if (put_user(mode_group->id_list[i],
1537 connector_id + copied)) {
1545 card_res->count_connectors = connector_count;
1547 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1548 card_res->count_connectors, card_res->count_encoders);
1551 drm_modeset_unlock_all(dev);
1556 * drm_mode_getcrtc - get CRTC configuration
1557 * @dev: drm device for the ioctl
1558 * @data: data pointer for the ioctl
1559 * @file_priv: drm file for the ioctl call
1561 * Construct a CRTC configuration structure to return to the user.
1563 * Called by the user via ioctl.
1566 * Zero on success, errno on failure.
1568 int drm_mode_getcrtc(struct drm_device *dev,
1569 void *data, struct drm_file *file_priv)
1571 struct drm_mode_crtc *crtc_resp = data;
1572 struct drm_crtc *crtc;
1573 struct drm_mode_object *obj;
1576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1579 drm_modeset_lock_all(dev);
1581 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1582 DRM_MODE_OBJECT_CRTC);
1587 crtc = obj_to_crtc(obj);
1589 crtc_resp->x = crtc->x;
1590 crtc_resp->y = crtc->y;
1591 crtc_resp->gamma_size = crtc->gamma_size;
1593 crtc_resp->fb_id = crtc->fb->base.id;
1595 crtc_resp->fb_id = 0;
1597 if (crtc->enabled) {
1599 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1600 crtc_resp->mode_valid = 1;
1603 crtc_resp->mode_valid = 0;
1607 drm_modeset_unlock_all(dev);
1611 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1612 const struct drm_file *file_priv)
1615 * If user-space hasn't configured the driver to expose the stereo 3D
1616 * modes, don't expose them.
1618 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1625 * drm_mode_getconnector - get connector configuration
1626 * @dev: drm device for the ioctl
1627 * @data: data pointer for the ioctl
1628 * @file_priv: drm file for the ioctl call
1630 * Construct a connector configuration structure to return to the user.
1632 * Called by the user via ioctl.
1635 * Zero on success, errno on failure.
1637 int drm_mode_getconnector(struct drm_device *dev, void *data,
1638 struct drm_file *file_priv)
1640 struct drm_mode_get_connector *out_resp = data;
1641 struct drm_mode_object *obj;
1642 struct drm_connector *connector;
1643 struct drm_display_mode *mode;
1645 int props_count = 0;
1646 int encoders_count = 0;
1650 struct drm_mode_modeinfo u_mode;
1651 struct drm_mode_modeinfo __user *mode_ptr;
1652 uint32_t __user *prop_ptr;
1653 uint64_t __user *prop_values;
1654 uint32_t __user *encoder_ptr;
1656 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1659 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1661 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1663 mutex_lock(&dev->mode_config.mutex);
1665 obj = drm_mode_object_find(dev, out_resp->connector_id,
1666 DRM_MODE_OBJECT_CONNECTOR);
1671 connector = obj_to_connector(obj);
1673 props_count = connector->properties.count;
1675 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1676 if (connector->encoder_ids[i] != 0) {
1681 if (out_resp->count_modes == 0) {
1682 connector->funcs->fill_modes(connector,
1683 dev->mode_config.max_width,
1684 dev->mode_config.max_height);
1687 /* delayed so we get modes regardless of pre-fill_modes state */
1688 list_for_each_entry(mode, &connector->modes, head)
1689 if (drm_mode_expose_to_userspace(mode, file_priv))
1692 out_resp->connector_id = connector->base.id;
1693 out_resp->connector_type = connector->connector_type;
1694 out_resp->connector_type_id = connector->connector_type_id;
1695 out_resp->mm_width = connector->display_info.width_mm;
1696 out_resp->mm_height = connector->display_info.height_mm;
1697 out_resp->subpixel = connector->display_info.subpixel_order;
1698 out_resp->connection = connector->status;
1699 if (connector->encoder)
1700 out_resp->encoder_id = connector->encoder->base.id;
1702 out_resp->encoder_id = 0;
1705 * This ioctl is called twice, once to determine how much space is
1706 * needed, and the 2nd time to fill it.
1708 if ((out_resp->count_modes >= mode_count) && mode_count) {
1710 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1711 list_for_each_entry(mode, &connector->modes, head) {
1712 if (!drm_mode_expose_to_userspace(mode, file_priv))
1715 drm_crtc_convert_to_umode(&u_mode, mode);
1716 if (copy_to_user(mode_ptr + copied,
1717 &u_mode, sizeof(u_mode))) {
1724 out_resp->count_modes = mode_count;
1726 if ((out_resp->count_props >= props_count) && props_count) {
1728 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1729 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1730 for (i = 0; i < connector->properties.count; i++) {
1731 if (put_user(connector->properties.ids[i],
1732 prop_ptr + copied)) {
1737 if (put_user(connector->properties.values[i],
1738 prop_values + copied)) {
1745 out_resp->count_props = props_count;
1747 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1749 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1750 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1751 if (connector->encoder_ids[i] != 0) {
1752 if (put_user(connector->encoder_ids[i],
1753 encoder_ptr + copied)) {
1761 out_resp->count_encoders = encoders_count;
1764 mutex_unlock(&dev->mode_config.mutex);
1769 int drm_mode_getencoder(struct drm_device *dev, void *data,
1770 struct drm_file *file_priv)
1772 struct drm_mode_get_encoder *enc_resp = data;
1773 struct drm_mode_object *obj;
1774 struct drm_encoder *encoder;
1777 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1780 drm_modeset_lock_all(dev);
1781 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1782 DRM_MODE_OBJECT_ENCODER);
1787 encoder = obj_to_encoder(obj);
1790 enc_resp->crtc_id = encoder->crtc->base.id;
1792 enc_resp->crtc_id = 0;
1793 enc_resp->encoder_type = encoder->encoder_type;
1794 enc_resp->encoder_id = encoder->base.id;
1795 enc_resp->possible_crtcs = encoder->possible_crtcs;
1796 enc_resp->possible_clones = encoder->possible_clones;
1799 drm_modeset_unlock_all(dev);
1804 * drm_mode_getplane_res - get plane info
1807 * @file_priv: DRM file info
1809 * Return an plane count and set of IDs.
1811 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1812 struct drm_file *file_priv)
1814 struct drm_mode_get_plane_res *plane_resp = data;
1815 struct drm_mode_config *config;
1816 struct drm_plane *plane;
1817 uint32_t __user *plane_ptr;
1818 int copied = 0, ret = 0;
1820 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1823 drm_modeset_lock_all(dev);
1824 config = &dev->mode_config;
1827 * This ioctl is called twice, once to determine how much space is
1828 * needed, and the 2nd time to fill it.
1830 if (config->num_plane &&
1831 (plane_resp->count_planes >= config->num_plane)) {
1832 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1834 list_for_each_entry(plane, &config->plane_list, head) {
1835 if (put_user(plane->base.id, plane_ptr + copied)) {
1842 plane_resp->count_planes = config->num_plane;
1845 drm_modeset_unlock_all(dev);
1850 * drm_mode_getplane - get plane info
1853 * @file_priv: DRM file info
1855 * Return plane info, including formats supported, gamma size, any
1858 int drm_mode_getplane(struct drm_device *dev, void *data,
1859 struct drm_file *file_priv)
1861 struct drm_mode_get_plane *plane_resp = data;
1862 struct drm_mode_object *obj;
1863 struct drm_plane *plane;
1864 uint32_t __user *format_ptr;
1867 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1870 drm_modeset_lock_all(dev);
1871 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1872 DRM_MODE_OBJECT_PLANE);
1877 plane = obj_to_plane(obj);
1880 plane_resp->crtc_id = plane->crtc->base.id;
1882 plane_resp->crtc_id = 0;
1885 plane_resp->fb_id = plane->fb->base.id;
1887 plane_resp->fb_id = 0;
1889 plane_resp->plane_id = plane->base.id;
1890 plane_resp->possible_crtcs = plane->possible_crtcs;
1891 plane_resp->gamma_size = 0;
1894 * This ioctl is called twice, once to determine how much space is
1895 * needed, and the 2nd time to fill it.
1897 if (plane->format_count &&
1898 (plane_resp->count_format_types >= plane->format_count)) {
1899 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1900 if (copy_to_user(format_ptr,
1901 plane->format_types,
1902 sizeof(uint32_t) * plane->format_count)) {
1907 plane_resp->count_format_types = plane->format_count;
1910 drm_modeset_unlock_all(dev);
1915 * drm_mode_setplane - set up or tear down an plane
1917 * @data: ioctl data*
1918 * @file_priv: DRM file info
1920 * Set plane info, including placement, fb, scaling, and other factors.
1921 * Or pass a NULL fb to disable.
1923 int drm_mode_setplane(struct drm_device *dev, void *data,
1924 struct drm_file *file_priv)
1926 struct drm_mode_set_plane *plane_req = data;
1927 struct drm_mode_object *obj;
1928 struct drm_plane *plane;
1929 struct drm_crtc *crtc;
1930 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1932 unsigned int fb_width, fb_height;
1935 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1939 * First, find the plane, crtc, and fb objects. If not available,
1940 * we don't bother to call the driver.
1942 obj = drm_mode_object_find(dev, plane_req->plane_id,
1943 DRM_MODE_OBJECT_PLANE);
1945 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1946 plane_req->plane_id);
1949 plane = obj_to_plane(obj);
1951 /* No fb means shut it down */
1952 if (!plane_req->fb_id) {
1953 drm_modeset_lock_all(dev);
1955 plane->funcs->disable_plane(plane);
1958 drm_modeset_unlock_all(dev);
1962 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1963 DRM_MODE_OBJECT_CRTC);
1965 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1966 plane_req->crtc_id);
1970 crtc = obj_to_crtc(obj);
1972 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1974 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1980 /* Check whether this plane supports the fb pixel format. */
1981 for (i = 0; i < plane->format_count; i++)
1982 if (fb->pixel_format == plane->format_types[i])
1984 if (i == plane->format_count) {
1985 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1986 drm_get_format_name(fb->pixel_format));
1991 fb_width = fb->width << 16;
1992 fb_height = fb->height << 16;
1994 /* Make sure source coordinates are inside the fb. */
1995 if (plane_req->src_w > fb_width ||
1996 plane_req->src_x > fb_width - plane_req->src_w ||
1997 plane_req->src_h > fb_height ||
1998 plane_req->src_y > fb_height - plane_req->src_h) {
1999 DRM_DEBUG_KMS("Invalid source coordinates "
2000 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2001 plane_req->src_w >> 16,
2002 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2003 plane_req->src_h >> 16,
2004 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2005 plane_req->src_x >> 16,
2006 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2007 plane_req->src_y >> 16,
2008 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2013 /* Give drivers some help against integer overflows */
2014 if (plane_req->crtc_w > INT_MAX ||
2015 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2016 plane_req->crtc_h > INT_MAX ||
2017 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2018 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2019 plane_req->crtc_w, plane_req->crtc_h,
2020 plane_req->crtc_x, plane_req->crtc_y);
2025 drm_modeset_lock_all(dev);
2026 ret = plane->funcs->update_plane(plane, crtc, fb,
2027 plane_req->crtc_x, plane_req->crtc_y,
2028 plane_req->crtc_w, plane_req->crtc_h,
2029 plane_req->src_x, plane_req->src_y,
2030 plane_req->src_w, plane_req->src_h);
2037 drm_modeset_unlock_all(dev);
2041 drm_framebuffer_unreference(fb);
2043 drm_framebuffer_unreference(old_fb);
2049 * drm_mode_set_config_internal - helper to call ->set_config
2050 * @set: modeset config to set
2052 * This is a little helper to wrap internal calls to the ->set_config driver
2053 * interface. The only thing it adds is correct refcounting dance.
2055 int drm_mode_set_config_internal(struct drm_mode_set *set)
2057 struct drm_crtc *crtc = set->crtc;
2058 struct drm_framebuffer *fb;
2059 struct drm_crtc *tmp;
2063 * NOTE: ->set_config can also disable other crtcs (if we steal all
2064 * connectors from it), hence we need to refcount the fbs across all
2065 * crtcs. Atomic modeset will have saner semantics ...
2067 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2068 tmp->old_fb = tmp->fb;
2072 ret = crtc->funcs->set_config(set);
2074 /* crtc->fb must be updated by ->set_config, enforces this. */
2075 WARN_ON(fb != crtc->fb);
2078 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2080 drm_framebuffer_reference(tmp->fb);
2082 drm_framebuffer_unreference(tmp->old_fb);
2087 EXPORT_SYMBOL(drm_mode_set_config_internal);
2090 * Checks that the framebuffer is big enough for the CRTC viewport
2091 * (x, y, hdisplay, vdisplay)
2093 static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2095 const struct drm_display_mode *mode,
2096 const struct drm_framebuffer *fb)
2099 int hdisplay, vdisplay;
2101 hdisplay = mode->hdisplay;
2102 vdisplay = mode->vdisplay;
2104 if (drm_mode_is_stereo(mode)) {
2105 struct drm_display_mode adjusted = *mode;
2107 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2108 hdisplay = adjusted.crtc_hdisplay;
2109 vdisplay = adjusted.crtc_vdisplay;
2112 if (crtc->invert_dimensions)
2113 swap(hdisplay, vdisplay);
2115 if (hdisplay > fb->width ||
2116 vdisplay > fb->height ||
2117 x > fb->width - hdisplay ||
2118 y > fb->height - vdisplay) {
2119 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2120 fb->width, fb->height, hdisplay, vdisplay, x, y,
2121 crtc->invert_dimensions ? " (inverted)" : "");
2129 * drm_mode_setcrtc - set CRTC configuration
2130 * @dev: drm device for the ioctl
2131 * @data: data pointer for the ioctl
2132 * @file_priv: drm file for the ioctl call
2134 * Build a new CRTC configuration based on user request.
2136 * Called by the user via ioctl.
2139 * Zero on success, errno on failure.
2141 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2142 struct drm_file *file_priv)
2144 struct drm_mode_config *config = &dev->mode_config;
2145 struct drm_mode_crtc *crtc_req = data;
2146 struct drm_mode_object *obj;
2147 struct drm_crtc *crtc;
2148 struct drm_connector **connector_set = NULL, *connector;
2149 struct drm_framebuffer *fb = NULL;
2150 struct drm_display_mode *mode = NULL;
2151 struct drm_mode_set set;
2152 uint32_t __user *set_connectors_ptr;
2156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2159 /* For some reason crtc x/y offsets are signed internally. */
2160 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2163 drm_modeset_lock_all(dev);
2164 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2165 DRM_MODE_OBJECT_CRTC);
2167 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2171 crtc = obj_to_crtc(obj);
2172 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2174 if (crtc_req->mode_valid) {
2175 /* If we have a mode we need a framebuffer. */
2176 /* If we pass -1, set the mode with the currently bound fb */
2177 if (crtc_req->fb_id == -1) {
2179 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2184 /* Make refcounting symmetric with the lookup path. */
2185 drm_framebuffer_reference(fb);
2187 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2189 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2196 mode = drm_mode_create(dev);
2202 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2204 DRM_DEBUG_KMS("Invalid mode\n");
2208 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2210 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2217 if (crtc_req->count_connectors == 0 && mode) {
2218 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2223 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2224 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2225 crtc_req->count_connectors);
2230 if (crtc_req->count_connectors > 0) {
2233 /* Avoid unbounded kernel memory allocation */
2234 if (crtc_req->count_connectors > config->num_connector) {
2239 connector_set = kmalloc(crtc_req->count_connectors *
2240 sizeof(struct drm_connector *),
2242 if (!connector_set) {
2247 for (i = 0; i < crtc_req->count_connectors; i++) {
2248 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2249 if (get_user(out_id, &set_connectors_ptr[i])) {
2254 obj = drm_mode_object_find(dev, out_id,
2255 DRM_MODE_OBJECT_CONNECTOR);
2257 DRM_DEBUG_KMS("Connector id %d unknown\n",
2262 connector = obj_to_connector(obj);
2263 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2265 drm_get_connector_name(connector));
2267 connector_set[i] = connector;
2272 set.x = crtc_req->x;
2273 set.y = crtc_req->y;
2275 set.connectors = connector_set;
2276 set.num_connectors = crtc_req->count_connectors;
2278 ret = drm_mode_set_config_internal(&set);
2282 drm_framebuffer_unreference(fb);
2284 kfree(connector_set);
2285 drm_mode_destroy(dev, mode);
2286 drm_modeset_unlock_all(dev);
2290 static int drm_mode_cursor_common(struct drm_device *dev,
2291 struct drm_mode_cursor2 *req,
2292 struct drm_file *file_priv)
2294 struct drm_mode_object *obj;
2295 struct drm_crtc *crtc;
2298 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2301 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2304 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2306 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2309 crtc = obj_to_crtc(obj);
2311 mutex_lock(&crtc->mutex);
2312 if (req->flags & DRM_MODE_CURSOR_BO) {
2313 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2317 /* Turns off the cursor if handle is 0 */
2318 if (crtc->funcs->cursor_set2)
2319 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2320 req->width, req->height, req->hot_x, req->hot_y);
2322 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2323 req->width, req->height);
2326 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2327 if (crtc->funcs->cursor_move) {
2328 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2335 mutex_unlock(&crtc->mutex);
2340 int drm_mode_cursor_ioctl(struct drm_device *dev,
2341 void *data, struct drm_file *file_priv)
2343 struct drm_mode_cursor *req = data;
2344 struct drm_mode_cursor2 new_req;
2346 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2347 new_req.hot_x = new_req.hot_y = 0;
2349 return drm_mode_cursor_common(dev, &new_req, file_priv);
2352 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2353 void *data, struct drm_file *file_priv)
2355 struct drm_mode_cursor2 *req = data;
2356 return drm_mode_cursor_common(dev, req, file_priv);
2359 /* Original addfb only supported RGB formats, so figure out which one */
2360 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2366 fmt = DRM_FORMAT_C8;
2370 fmt = DRM_FORMAT_XRGB1555;
2372 fmt = DRM_FORMAT_RGB565;
2375 fmt = DRM_FORMAT_RGB888;
2379 fmt = DRM_FORMAT_XRGB8888;
2380 else if (depth == 30)
2381 fmt = DRM_FORMAT_XRGB2101010;
2383 fmt = DRM_FORMAT_ARGB8888;
2386 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2387 fmt = DRM_FORMAT_XRGB8888;
2393 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2396 * drm_mode_addfb - add an FB to the graphics configuration
2397 * @dev: drm device for the ioctl
2398 * @data: data pointer for the ioctl
2399 * @file_priv: drm file for the ioctl call
2401 * Add a new FB to the specified CRTC, given a user request.
2403 * Called by the user via ioctl.
2406 * Zero on success, errno on failure.
2408 int drm_mode_addfb(struct drm_device *dev,
2409 void *data, struct drm_file *file_priv)
2411 struct drm_mode_fb_cmd *or = data;
2412 struct drm_mode_fb_cmd2 r = {};
2413 struct drm_mode_config *config = &dev->mode_config;
2414 struct drm_framebuffer *fb;
2417 /* Use new struct with format internally */
2418 r.fb_id = or->fb_id;
2419 r.width = or->width;
2420 r.height = or->height;
2421 r.pitches[0] = or->pitch;
2422 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2423 r.handles[0] = or->handle;
2425 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2428 if ((config->min_width > r.width) || (r.width > config->max_width))
2431 if ((config->min_height > r.height) || (r.height > config->max_height))
2434 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2436 DRM_DEBUG_KMS("could not create framebuffer\n");
2440 mutex_lock(&file_priv->fbs_lock);
2441 or->fb_id = fb->base.id;
2442 list_add(&fb->filp_head, &file_priv->fbs);
2443 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2444 mutex_unlock(&file_priv->fbs_lock);
2449 static int format_check(const struct drm_mode_fb_cmd2 *r)
2451 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2455 case DRM_FORMAT_RGB332:
2456 case DRM_FORMAT_BGR233:
2457 case DRM_FORMAT_XRGB4444:
2458 case DRM_FORMAT_XBGR4444:
2459 case DRM_FORMAT_RGBX4444:
2460 case DRM_FORMAT_BGRX4444:
2461 case DRM_FORMAT_ARGB4444:
2462 case DRM_FORMAT_ABGR4444:
2463 case DRM_FORMAT_RGBA4444:
2464 case DRM_FORMAT_BGRA4444:
2465 case DRM_FORMAT_XRGB1555:
2466 case DRM_FORMAT_XBGR1555:
2467 case DRM_FORMAT_RGBX5551:
2468 case DRM_FORMAT_BGRX5551:
2469 case DRM_FORMAT_ARGB1555:
2470 case DRM_FORMAT_ABGR1555:
2471 case DRM_FORMAT_RGBA5551:
2472 case DRM_FORMAT_BGRA5551:
2473 case DRM_FORMAT_RGB565:
2474 case DRM_FORMAT_BGR565:
2475 case DRM_FORMAT_RGB888:
2476 case DRM_FORMAT_BGR888:
2477 case DRM_FORMAT_XRGB8888:
2478 case DRM_FORMAT_XBGR8888:
2479 case DRM_FORMAT_RGBX8888:
2480 case DRM_FORMAT_BGRX8888:
2481 case DRM_FORMAT_ARGB8888:
2482 case DRM_FORMAT_ABGR8888:
2483 case DRM_FORMAT_RGBA8888:
2484 case DRM_FORMAT_BGRA8888:
2485 case DRM_FORMAT_XRGB2101010:
2486 case DRM_FORMAT_XBGR2101010:
2487 case DRM_FORMAT_RGBX1010102:
2488 case DRM_FORMAT_BGRX1010102:
2489 case DRM_FORMAT_ARGB2101010:
2490 case DRM_FORMAT_ABGR2101010:
2491 case DRM_FORMAT_RGBA1010102:
2492 case DRM_FORMAT_BGRA1010102:
2493 case DRM_FORMAT_YUYV:
2494 case DRM_FORMAT_YVYU:
2495 case DRM_FORMAT_UYVY:
2496 case DRM_FORMAT_VYUY:
2497 case DRM_FORMAT_AYUV:
2498 case DRM_FORMAT_NV12:
2499 case DRM_FORMAT_NV21:
2500 case DRM_FORMAT_NV16:
2501 case DRM_FORMAT_NV61:
2502 case DRM_FORMAT_NV24:
2503 case DRM_FORMAT_NV42:
2504 case DRM_FORMAT_YUV410:
2505 case DRM_FORMAT_YVU410:
2506 case DRM_FORMAT_YUV411:
2507 case DRM_FORMAT_YVU411:
2508 case DRM_FORMAT_YUV420:
2509 case DRM_FORMAT_YVU420:
2510 case DRM_FORMAT_YUV422:
2511 case DRM_FORMAT_YVU422:
2512 case DRM_FORMAT_YUV444:
2513 case DRM_FORMAT_YVU444:
2516 DRM_DEBUG_KMS("invalid pixel format %s\n",
2517 drm_get_format_name(r->pixel_format));
2522 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2524 int ret, hsub, vsub, num_planes, i;
2526 ret = format_check(r);
2528 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2529 drm_get_format_name(r->pixel_format));
2533 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2534 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2535 num_planes = drm_format_num_planes(r->pixel_format);
2537 if (r->width == 0 || r->width % hsub) {
2538 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2542 if (r->height == 0 || r->height % vsub) {
2543 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2547 for (i = 0; i < num_planes; i++) {
2548 unsigned int width = r->width / (i != 0 ? hsub : 1);
2549 unsigned int height = r->height / (i != 0 ? vsub : 1);
2550 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2552 if (!r->handles[i]) {
2553 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2557 if ((uint64_t) width * cpp > UINT_MAX)
2560 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2563 if (r->pitches[i] < width * cpp) {
2564 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2573 * drm_mode_addfb2 - add an FB to the graphics configuration
2574 * @dev: drm device for the ioctl
2575 * @data: data pointer for the ioctl
2576 * @file_priv: drm file for the ioctl call
2578 * Add a new FB to the specified CRTC, given a user request with format.
2580 * Called by the user via ioctl.
2583 * Zero on success, errno on failure.
2585 int drm_mode_addfb2(struct drm_device *dev,
2586 void *data, struct drm_file *file_priv)
2588 struct drm_mode_fb_cmd2 *r = data;
2589 struct drm_mode_config *config = &dev->mode_config;
2590 struct drm_framebuffer *fb;
2593 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2596 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2597 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2601 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2602 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2603 r->width, config->min_width, config->max_width);
2606 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2607 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2608 r->height, config->min_height, config->max_height);
2612 ret = framebuffer_check(r);
2616 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2618 DRM_DEBUG_KMS("could not create framebuffer\n");
2622 mutex_lock(&file_priv->fbs_lock);
2623 r->fb_id = fb->base.id;
2624 list_add(&fb->filp_head, &file_priv->fbs);
2625 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2626 mutex_unlock(&file_priv->fbs_lock);
2633 * drm_mode_rmfb - remove an FB from the configuration
2634 * @dev: drm device for the ioctl
2635 * @data: data pointer for the ioctl
2636 * @file_priv: drm file for the ioctl call
2638 * Remove the FB specified by the user.
2640 * Called by the user via ioctl.
2643 * Zero on success, errno on failure.
2645 int drm_mode_rmfb(struct drm_device *dev,
2646 void *data, struct drm_file *file_priv)
2648 struct drm_framebuffer *fb = NULL;
2649 struct drm_framebuffer *fbl = NULL;
2650 uint32_t *id = data;
2653 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2656 mutex_lock(&file_priv->fbs_lock);
2657 mutex_lock(&dev->mode_config.fb_lock);
2658 fb = __drm_framebuffer_lookup(dev, *id);
2662 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2668 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2669 __drm_framebuffer_unregister(dev, fb);
2671 list_del_init(&fb->filp_head);
2672 mutex_unlock(&dev->mode_config.fb_lock);
2673 mutex_unlock(&file_priv->fbs_lock);
2675 drm_framebuffer_remove(fb);
2680 mutex_unlock(&dev->mode_config.fb_lock);
2681 mutex_unlock(&file_priv->fbs_lock);
2687 * drm_mode_getfb - get FB info
2688 * @dev: drm device for the ioctl
2689 * @data: data pointer for the ioctl
2690 * @file_priv: drm file for the ioctl call
2692 * Lookup the FB given its ID and return info about it.
2694 * Called by the user via ioctl.
2697 * Zero on success, errno on failure.
2699 int drm_mode_getfb(struct drm_device *dev,
2700 void *data, struct drm_file *file_priv)
2702 struct drm_mode_fb_cmd *r = data;
2703 struct drm_framebuffer *fb;
2706 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2709 fb = drm_framebuffer_lookup(dev, r->fb_id);
2713 r->height = fb->height;
2714 r->width = fb->width;
2715 r->depth = fb->depth;
2716 r->bpp = fb->bits_per_pixel;
2717 r->pitch = fb->pitches[0];
2718 if (fb->funcs->create_handle) {
2719 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2720 file_priv->minor->type == DRM_MINOR_CONTROL) {
2721 ret = fb->funcs->create_handle(fb, file_priv,
2724 /* GET_FB() is an unprivileged ioctl so we must not
2725 * return a buffer-handle to non-master processes! For
2726 * backwards-compatibility reasons, we cannot make
2727 * GET_FB() privileged, so just return an invalid handle
2728 * for non-masters. */
2736 drm_framebuffer_unreference(fb);
2741 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2742 void *data, struct drm_file *file_priv)
2744 struct drm_clip_rect __user *clips_ptr;
2745 struct drm_clip_rect *clips = NULL;
2746 struct drm_mode_fb_dirty_cmd *r = data;
2747 struct drm_framebuffer *fb;
2752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2755 fb = drm_framebuffer_lookup(dev, r->fb_id);
2759 num_clips = r->num_clips;
2760 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2762 if (!num_clips != !clips_ptr) {
2767 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2769 /* If userspace annotates copy, clips must come in pairs */
2770 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2775 if (num_clips && clips_ptr) {
2776 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2780 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2786 ret = copy_from_user(clips, clips_ptr,
2787 num_clips * sizeof(*clips));
2794 if (fb->funcs->dirty) {
2795 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2804 drm_framebuffer_unreference(fb);
2811 * drm_fb_release - remove and free the FBs on this file
2812 * @priv: drm file for the ioctl
2814 * Destroy all the FBs associated with @filp.
2816 * Called by the user via ioctl.
2819 * Zero on success, errno on failure.
2821 void drm_fb_release(struct drm_file *priv)
2823 struct drm_device *dev = priv->minor->dev;
2824 struct drm_framebuffer *fb, *tfb;
2826 mutex_lock(&priv->fbs_lock);
2827 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2829 mutex_lock(&dev->mode_config.fb_lock);
2830 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2831 __drm_framebuffer_unregister(dev, fb);
2832 mutex_unlock(&dev->mode_config.fb_lock);
2834 list_del_init(&fb->filp_head);
2836 /* This will also drop the fpriv->fbs reference. */
2837 drm_framebuffer_remove(fb);
2839 mutex_unlock(&priv->fbs_lock);
2842 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2843 const char *name, int num_values)
2845 struct drm_property *property = NULL;
2848 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2853 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2854 if (!property->values)
2858 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2862 property->flags = flags;
2863 property->num_values = num_values;
2864 INIT_LIST_HEAD(&property->enum_blob_list);
2867 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2868 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2871 list_add_tail(&property->head, &dev->mode_config.property_list);
2874 kfree(property->values);
2878 EXPORT_SYMBOL(drm_property_create);
2880 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2882 const struct drm_prop_enum_list *props,
2885 struct drm_property *property;
2888 flags |= DRM_MODE_PROP_ENUM;
2890 property = drm_property_create(dev, flags, name, num_values);
2894 for (i = 0; i < num_values; i++) {
2895 ret = drm_property_add_enum(property, i,
2899 drm_property_destroy(dev, property);
2906 EXPORT_SYMBOL(drm_property_create_enum);
2908 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2909 int flags, const char *name,
2910 const struct drm_prop_enum_list *props,
2913 struct drm_property *property;
2916 flags |= DRM_MODE_PROP_BITMASK;
2918 property = drm_property_create(dev, flags, name, num_values);
2922 for (i = 0; i < num_values; i++) {
2923 ret = drm_property_add_enum(property, i,
2927 drm_property_destroy(dev, property);
2934 EXPORT_SYMBOL(drm_property_create_bitmask);
2936 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2938 uint64_t min, uint64_t max)
2940 struct drm_property *property;
2942 flags |= DRM_MODE_PROP_RANGE;
2944 property = drm_property_create(dev, flags, name, 2);
2948 property->values[0] = min;
2949 property->values[1] = max;
2953 EXPORT_SYMBOL(drm_property_create_range);
2955 int drm_property_add_enum(struct drm_property *property, int index,
2956 uint64_t value, const char *name)
2958 struct drm_property_enum *prop_enum;
2960 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2964 * Bitmask enum properties have the additional constraint of values
2967 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2970 if (!list_empty(&property->enum_blob_list)) {
2971 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2972 if (prop_enum->value == value) {
2973 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2974 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2980 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2984 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2985 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2986 prop_enum->value = value;
2988 property->values[index] = value;
2989 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2992 EXPORT_SYMBOL(drm_property_add_enum);
2994 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2996 struct drm_property_enum *prop_enum, *pt;
2998 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2999 list_del(&prop_enum->head);
3003 if (property->num_values)
3004 kfree(property->values);
3005 drm_mode_object_put(dev, &property->base);
3006 list_del(&property->head);
3009 EXPORT_SYMBOL(drm_property_destroy);
3011 void drm_object_attach_property(struct drm_mode_object *obj,
3012 struct drm_property *property,
3015 int count = obj->properties->count;
3017 if (count == DRM_OBJECT_MAX_PROPERTY) {
3018 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3019 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3020 "you see this message on the same object type.\n",
3025 obj->properties->ids[count] = property->base.id;
3026 obj->properties->values[count] = init_val;
3027 obj->properties->count++;
3029 EXPORT_SYMBOL(drm_object_attach_property);
3031 int drm_object_property_set_value(struct drm_mode_object *obj,
3032 struct drm_property *property, uint64_t val)
3036 for (i = 0; i < obj->properties->count; i++) {
3037 if (obj->properties->ids[i] == property->base.id) {
3038 obj->properties->values[i] = val;
3045 EXPORT_SYMBOL(drm_object_property_set_value);
3047 int drm_object_property_get_value(struct drm_mode_object *obj,
3048 struct drm_property *property, uint64_t *val)
3052 for (i = 0; i < obj->properties->count; i++) {
3053 if (obj->properties->ids[i] == property->base.id) {
3054 *val = obj->properties->values[i];
3061 EXPORT_SYMBOL(drm_object_property_get_value);
3063 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3064 void *data, struct drm_file *file_priv)
3066 struct drm_mode_object *obj;
3067 struct drm_mode_get_property *out_resp = data;
3068 struct drm_property *property;
3071 int value_count = 0;
3074 struct drm_property_enum *prop_enum;
3075 struct drm_mode_property_enum __user *enum_ptr;
3076 struct drm_property_blob *prop_blob;
3077 uint32_t __user *blob_id_ptr;
3078 uint64_t __user *values_ptr;
3079 uint32_t __user *blob_length_ptr;
3081 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3084 drm_modeset_lock_all(dev);
3085 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3090 property = obj_to_property(obj);
3092 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3093 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3095 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3096 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3100 value_count = property->num_values;
3102 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3103 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3104 out_resp->flags = property->flags;
3106 if ((out_resp->count_values >= value_count) && value_count) {
3107 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3108 for (i = 0; i < value_count; i++) {
3109 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3115 out_resp->count_values = value_count;
3117 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3118 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3120 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3121 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3123 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3128 if (copy_to_user(&enum_ptr[copied].name,
3129 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3136 out_resp->count_enum_blobs = enum_count;
3139 if (property->flags & DRM_MODE_PROP_BLOB) {
3140 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3142 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3143 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3145 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3146 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3151 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3159 out_resp->count_enum_blobs = blob_count;
3162 drm_modeset_unlock_all(dev);
3166 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3169 struct drm_property_blob *blob;
3172 if (!length || !data)
3175 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3179 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3185 blob->length = length;
3187 memcpy(blob->data, data, length);
3189 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3193 static void drm_property_destroy_blob(struct drm_device *dev,
3194 struct drm_property_blob *blob)
3196 drm_mode_object_put(dev, &blob->base);
3197 list_del(&blob->head);
3201 int drm_mode_getblob_ioctl(struct drm_device *dev,
3202 void *data, struct drm_file *file_priv)
3204 struct drm_mode_object *obj;
3205 struct drm_mode_get_blob *out_resp = data;
3206 struct drm_property_blob *blob;
3208 void __user *blob_ptr;
3210 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3213 drm_modeset_lock_all(dev);
3214 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3219 blob = obj_to_blob(obj);
3221 if (out_resp->length == blob->length) {
3222 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3223 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3228 out_resp->length = blob->length;
3231 drm_modeset_unlock_all(dev);
3235 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3238 struct drm_device *dev = connector->dev;
3241 if (connector->edid_blob_ptr)
3242 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3244 /* Delete edid, when there is none. */
3246 connector->edid_blob_ptr = NULL;
3247 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3251 size = EDID_LENGTH * (1 + edid->extensions);
3252 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3254 if (!connector->edid_blob_ptr)
3257 ret = drm_object_property_set_value(&connector->base,
3258 dev->mode_config.edid_property,
3259 connector->edid_blob_ptr->base.id);
3263 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3265 static bool drm_property_change_is_valid(struct drm_property *property,
3268 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3270 if (property->flags & DRM_MODE_PROP_RANGE) {
3271 if (value < property->values[0] || value > property->values[1])
3274 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3276 uint64_t valid_mask = 0;
3277 for (i = 0; i < property->num_values; i++)
3278 valid_mask |= (1ULL << property->values[i]);
3279 return !(value & ~valid_mask);
3280 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3281 /* Only the driver knows */
3285 for (i = 0; i < property->num_values; i++)
3286 if (property->values[i] == value)
3292 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3293 void *data, struct drm_file *file_priv)
3295 struct drm_mode_connector_set_property *conn_set_prop = data;
3296 struct drm_mode_obj_set_property obj_set_prop = {
3297 .value = conn_set_prop->value,
3298 .prop_id = conn_set_prop->prop_id,
3299 .obj_id = conn_set_prop->connector_id,
3300 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3303 /* It does all the locking and checking we need */
3304 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3307 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3308 struct drm_property *property,
3312 struct drm_connector *connector = obj_to_connector(obj);
3314 /* Do DPMS ourselves */
3315 if (property == connector->dev->mode_config.dpms_property) {
3316 if (connector->funcs->dpms)
3317 (*connector->funcs->dpms)(connector, (int)value);
3319 } else if (connector->funcs->set_property)
3320 ret = connector->funcs->set_property(connector, property, value);
3322 /* store the property value if successful */
3324 drm_object_property_set_value(&connector->base, property, value);
3328 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3329 struct drm_property *property,
3333 struct drm_crtc *crtc = obj_to_crtc(obj);
3335 if (crtc->funcs->set_property)
3336 ret = crtc->funcs->set_property(crtc, property, value);
3338 drm_object_property_set_value(obj, property, value);
3343 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3344 struct drm_property *property,
3348 struct drm_plane *plane = obj_to_plane(obj);
3350 if (plane->funcs->set_property)
3351 ret = plane->funcs->set_property(plane, property, value);
3353 drm_object_property_set_value(obj, property, value);
3358 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3359 struct drm_file *file_priv)
3361 struct drm_mode_obj_get_properties *arg = data;
3362 struct drm_mode_object *obj;
3366 int props_count = 0;
3367 uint32_t __user *props_ptr;
3368 uint64_t __user *prop_values_ptr;
3370 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3373 drm_modeset_lock_all(dev);
3375 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3380 if (!obj->properties) {
3385 props_count = obj->properties->count;
3387 /* This ioctl is called twice, once to determine how much space is
3388 * needed, and the 2nd time to fill it. */
3389 if ((arg->count_props >= props_count) && props_count) {
3391 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3392 prop_values_ptr = (uint64_t __user *)(unsigned long)
3393 (arg->prop_values_ptr);
3394 for (i = 0; i < props_count; i++) {
3395 if (put_user(obj->properties->ids[i],
3396 props_ptr + copied)) {
3400 if (put_user(obj->properties->values[i],
3401 prop_values_ptr + copied)) {
3408 arg->count_props = props_count;
3410 drm_modeset_unlock_all(dev);
3414 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3415 struct drm_file *file_priv)
3417 struct drm_mode_obj_set_property *arg = data;
3418 struct drm_mode_object *arg_obj;
3419 struct drm_mode_object *prop_obj;
3420 struct drm_property *property;
3424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3427 drm_modeset_lock_all(dev);
3429 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3434 if (!arg_obj->properties)
3437 for (i = 0; i < arg_obj->properties->count; i++)
3438 if (arg_obj->properties->ids[i] == arg->prop_id)
3441 if (i == arg_obj->properties->count)
3444 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3445 DRM_MODE_OBJECT_PROPERTY);
3450 property = obj_to_property(prop_obj);
3452 if (!drm_property_change_is_valid(property, arg->value))
3455 switch (arg_obj->type) {
3456 case DRM_MODE_OBJECT_CONNECTOR:
3457 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3460 case DRM_MODE_OBJECT_CRTC:
3461 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3463 case DRM_MODE_OBJECT_PLANE:
3464 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3469 drm_modeset_unlock_all(dev);
3473 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3474 struct drm_encoder *encoder)
3478 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3479 if (connector->encoder_ids[i] == 0) {
3480 connector->encoder_ids[i] = encoder->base.id;
3486 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3488 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3489 struct drm_encoder *encoder)
3492 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3493 if (connector->encoder_ids[i] == encoder->base.id) {
3494 connector->encoder_ids[i] = 0;
3495 if (connector->encoder == encoder)
3496 connector->encoder = NULL;
3501 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3503 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3506 crtc->gamma_size = gamma_size;
3508 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3509 if (!crtc->gamma_store) {
3510 crtc->gamma_size = 0;
3516 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3518 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3519 void *data, struct drm_file *file_priv)
3521 struct drm_mode_crtc_lut *crtc_lut = data;
3522 struct drm_mode_object *obj;
3523 struct drm_crtc *crtc;
3524 void *r_base, *g_base, *b_base;
3528 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3531 drm_modeset_lock_all(dev);
3532 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3537 crtc = obj_to_crtc(obj);
3539 if (crtc->funcs->gamma_set == NULL) {
3544 /* memcpy into gamma store */
3545 if (crtc_lut->gamma_size != crtc->gamma_size) {
3550 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3551 r_base = crtc->gamma_store;
3552 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3557 g_base = r_base + size;
3558 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3563 b_base = g_base + size;
3564 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3569 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3572 drm_modeset_unlock_all(dev);
3577 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3578 void *data, struct drm_file *file_priv)
3580 struct drm_mode_crtc_lut *crtc_lut = data;
3581 struct drm_mode_object *obj;
3582 struct drm_crtc *crtc;
3583 void *r_base, *g_base, *b_base;
3587 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3590 drm_modeset_lock_all(dev);
3591 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3596 crtc = obj_to_crtc(obj);
3598 /* memcpy into gamma store */
3599 if (crtc_lut->gamma_size != crtc->gamma_size) {
3604 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3605 r_base = crtc->gamma_store;
3606 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3611 g_base = r_base + size;
3612 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3617 b_base = g_base + size;
3618 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3623 drm_modeset_unlock_all(dev);
3627 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3628 void *data, struct drm_file *file_priv)
3630 struct drm_mode_crtc_page_flip *page_flip = data;
3631 struct drm_mode_object *obj;
3632 struct drm_crtc *crtc;
3633 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3634 struct drm_pending_vblank_event *e = NULL;
3635 unsigned long flags;
3638 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3639 page_flip->reserved != 0)
3642 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3645 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3648 crtc = obj_to_crtc(obj);
3650 mutex_lock(&crtc->mutex);
3651 if (crtc->fb == NULL) {
3652 /* The framebuffer is currently unbound, presumably
3653 * due to a hotplug event, that userspace has not
3660 if (crtc->funcs->page_flip == NULL)
3663 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3669 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3673 if (crtc->fb->pixel_format != fb->pixel_format) {
3674 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3679 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3681 spin_lock_irqsave(&dev->event_lock, flags);
3682 if (file_priv->event_space < sizeof e->event) {
3683 spin_unlock_irqrestore(&dev->event_lock, flags);
3686 file_priv->event_space -= sizeof e->event;
3687 spin_unlock_irqrestore(&dev->event_lock, flags);
3689 e = kzalloc(sizeof *e, GFP_KERNEL);
3691 spin_lock_irqsave(&dev->event_lock, flags);
3692 file_priv->event_space += sizeof e->event;
3693 spin_unlock_irqrestore(&dev->event_lock, flags);
3697 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3698 e->event.base.length = sizeof e->event;
3699 e->event.user_data = page_flip->user_data;
3700 e->base.event = &e->event.base;
3701 e->base.file_priv = file_priv;
3703 (void (*) (struct drm_pending_event *)) kfree;
3707 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
3709 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3710 spin_lock_irqsave(&dev->event_lock, flags);
3711 file_priv->event_space += sizeof e->event;
3712 spin_unlock_irqrestore(&dev->event_lock, flags);
3715 /* Keep the old fb, don't unref it. */
3719 * Warn if the driver hasn't properly updated the crtc->fb
3720 * field to reflect that the new framebuffer is now used.
3721 * Failing to do so will screw with the reference counting
3724 WARN_ON(crtc->fb != fb);
3725 /* Unref only the old framebuffer. */
3731 drm_framebuffer_unreference(fb);
3733 drm_framebuffer_unreference(old_fb);
3734 mutex_unlock(&crtc->mutex);
3739 void drm_mode_config_reset(struct drm_device *dev)
3741 struct drm_crtc *crtc;
3742 struct drm_encoder *encoder;
3743 struct drm_connector *connector;
3745 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3746 if (crtc->funcs->reset)
3747 crtc->funcs->reset(crtc);
3749 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3750 if (encoder->funcs->reset)
3751 encoder->funcs->reset(encoder);
3753 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3754 connector->status = connector_status_unknown;
3756 if (connector->funcs->reset)
3757 connector->funcs->reset(connector);
3760 EXPORT_SYMBOL(drm_mode_config_reset);
3762 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3763 void *data, struct drm_file *file_priv)
3765 struct drm_mode_create_dumb *args = data;
3767 if (!dev->driver->dumb_create)
3769 return dev->driver->dumb_create(file_priv, dev, args);
3772 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3773 void *data, struct drm_file *file_priv)
3775 struct drm_mode_map_dumb *args = data;
3777 /* call driver ioctl to get mmap offset */
3778 if (!dev->driver->dumb_map_offset)
3781 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3784 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3785 void *data, struct drm_file *file_priv)
3787 struct drm_mode_destroy_dumb *args = data;
3789 if (!dev->driver->dumb_destroy)
3792 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3796 * Just need to support RGB formats here for compat with code that doesn't
3797 * use pixel formats directly yet.
3799 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3804 case DRM_FORMAT_RGB332:
3805 case DRM_FORMAT_BGR233:
3809 case DRM_FORMAT_XRGB1555:
3810 case DRM_FORMAT_XBGR1555:
3811 case DRM_FORMAT_RGBX5551:
3812 case DRM_FORMAT_BGRX5551:
3813 case DRM_FORMAT_ARGB1555:
3814 case DRM_FORMAT_ABGR1555:
3815 case DRM_FORMAT_RGBA5551:
3816 case DRM_FORMAT_BGRA5551:
3820 case DRM_FORMAT_RGB565:
3821 case DRM_FORMAT_BGR565:
3825 case DRM_FORMAT_RGB888:
3826 case DRM_FORMAT_BGR888:
3830 case DRM_FORMAT_XRGB8888:
3831 case DRM_FORMAT_XBGR8888:
3832 case DRM_FORMAT_RGBX8888:
3833 case DRM_FORMAT_BGRX8888:
3837 case DRM_FORMAT_XRGB2101010:
3838 case DRM_FORMAT_XBGR2101010:
3839 case DRM_FORMAT_RGBX1010102:
3840 case DRM_FORMAT_BGRX1010102:
3841 case DRM_FORMAT_ARGB2101010:
3842 case DRM_FORMAT_ABGR2101010:
3843 case DRM_FORMAT_RGBA1010102:
3844 case DRM_FORMAT_BGRA1010102:
3848 case DRM_FORMAT_ARGB8888:
3849 case DRM_FORMAT_ABGR8888:
3850 case DRM_FORMAT_RGBA8888:
3851 case DRM_FORMAT_BGRA8888:
3856 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3857 drm_get_format_name(format));
3863 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3866 * drm_format_num_planes - get the number of planes for format
3867 * @format: pixel format (DRM_FORMAT_*)
3870 * The number of planes used by the specified pixel format.
3872 int drm_format_num_planes(uint32_t format)
3875 case DRM_FORMAT_YUV410:
3876 case DRM_FORMAT_YVU410:
3877 case DRM_FORMAT_YUV411:
3878 case DRM_FORMAT_YVU411:
3879 case DRM_FORMAT_YUV420:
3880 case DRM_FORMAT_YVU420:
3881 case DRM_FORMAT_YUV422:
3882 case DRM_FORMAT_YVU422:
3883 case DRM_FORMAT_YUV444:
3884 case DRM_FORMAT_YVU444:
3886 case DRM_FORMAT_NV12:
3887 case DRM_FORMAT_NV21:
3888 case DRM_FORMAT_NV16:
3889 case DRM_FORMAT_NV61:
3890 case DRM_FORMAT_NV24:
3891 case DRM_FORMAT_NV42:
3897 EXPORT_SYMBOL(drm_format_num_planes);
3900 * drm_format_plane_cpp - determine the bytes per pixel value
3901 * @format: pixel format (DRM_FORMAT_*)
3902 * @plane: plane index
3905 * The bytes per pixel value for the specified plane.
3907 int drm_format_plane_cpp(uint32_t format, int plane)
3912 if (plane >= drm_format_num_planes(format))
3916 case DRM_FORMAT_YUYV:
3917 case DRM_FORMAT_YVYU:
3918 case DRM_FORMAT_UYVY:
3919 case DRM_FORMAT_VYUY:
3921 case DRM_FORMAT_NV12:
3922 case DRM_FORMAT_NV21:
3923 case DRM_FORMAT_NV16:
3924 case DRM_FORMAT_NV61:
3925 case DRM_FORMAT_NV24:
3926 case DRM_FORMAT_NV42:
3927 return plane ? 2 : 1;
3928 case DRM_FORMAT_YUV410:
3929 case DRM_FORMAT_YVU410:
3930 case DRM_FORMAT_YUV411:
3931 case DRM_FORMAT_YVU411:
3932 case DRM_FORMAT_YUV420:
3933 case DRM_FORMAT_YVU420:
3934 case DRM_FORMAT_YUV422:
3935 case DRM_FORMAT_YVU422:
3936 case DRM_FORMAT_YUV444:
3937 case DRM_FORMAT_YVU444:
3940 drm_fb_get_bpp_depth(format, &depth, &bpp);
3944 EXPORT_SYMBOL(drm_format_plane_cpp);
3947 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3948 * @format: pixel format (DRM_FORMAT_*)
3951 * The horizontal chroma subsampling factor for the
3952 * specified pixel format.
3954 int drm_format_horz_chroma_subsampling(uint32_t format)
3957 case DRM_FORMAT_YUV411:
3958 case DRM_FORMAT_YVU411:
3959 case DRM_FORMAT_YUV410:
3960 case DRM_FORMAT_YVU410:
3962 case DRM_FORMAT_YUYV:
3963 case DRM_FORMAT_YVYU:
3964 case DRM_FORMAT_UYVY:
3965 case DRM_FORMAT_VYUY:
3966 case DRM_FORMAT_NV12:
3967 case DRM_FORMAT_NV21:
3968 case DRM_FORMAT_NV16:
3969 case DRM_FORMAT_NV61:
3970 case DRM_FORMAT_YUV422:
3971 case DRM_FORMAT_YVU422:
3972 case DRM_FORMAT_YUV420:
3973 case DRM_FORMAT_YVU420:
3979 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3982 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3983 * @format: pixel format (DRM_FORMAT_*)
3986 * The vertical chroma subsampling factor for the
3987 * specified pixel format.
3989 int drm_format_vert_chroma_subsampling(uint32_t format)
3992 case DRM_FORMAT_YUV410:
3993 case DRM_FORMAT_YVU410:
3995 case DRM_FORMAT_YUV420:
3996 case DRM_FORMAT_YVU420:
3997 case DRM_FORMAT_NV12:
3998 case DRM_FORMAT_NV21:
4004 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4007 * drm_mode_config_init - initialize DRM mode_configuration structure
4010 * Initialize @dev's mode_config structure, used for tracking the graphics
4011 * configuration of @dev.
4013 * Since this initializes the modeset locks, no locking is possible. Which is no
4014 * problem, since this should happen single threaded at init time. It is the
4015 * driver's problem to ensure this guarantee.
4018 void drm_mode_config_init(struct drm_device *dev)
4020 mutex_init(&dev->mode_config.mutex);
4021 mutex_init(&dev->mode_config.idr_mutex);
4022 mutex_init(&dev->mode_config.fb_lock);
4023 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4024 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4025 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4026 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4027 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4028 INIT_LIST_HEAD(&dev->mode_config.property_list);
4029 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4030 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4031 idr_init(&dev->mode_config.crtc_idr);
4033 drm_modeset_lock_all(dev);
4034 drm_mode_create_standard_connector_properties(dev);
4035 drm_modeset_unlock_all(dev);
4037 /* Just to be sure */
4038 dev->mode_config.num_fb = 0;
4039 dev->mode_config.num_connector = 0;
4040 dev->mode_config.num_crtc = 0;
4041 dev->mode_config.num_encoder = 0;
4043 EXPORT_SYMBOL(drm_mode_config_init);
4046 * drm_mode_config_cleanup - free up DRM mode_config info
4049 * Free up all the connectors and CRTCs associated with this DRM device, then
4050 * free up the framebuffers and associated buffer objects.
4052 * Note that since this /should/ happen single-threaded at driver/device
4053 * teardown time, no locking is required. It's the driver's job to ensure that
4054 * this guarantee actually holds true.
4056 * FIXME: cleanup any dangling user buffer objects too
4058 void drm_mode_config_cleanup(struct drm_device *dev)
4060 struct drm_connector *connector, *ot;
4061 struct drm_crtc *crtc, *ct;
4062 struct drm_encoder *encoder, *enct;
4063 struct drm_bridge *bridge, *brt;
4064 struct drm_framebuffer *fb, *fbt;
4065 struct drm_property *property, *pt;
4066 struct drm_property_blob *blob, *bt;
4067 struct drm_plane *plane, *plt;
4069 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4071 encoder->funcs->destroy(encoder);
4074 list_for_each_entry_safe(bridge, brt,
4075 &dev->mode_config.bridge_list, head) {
4076 bridge->funcs->destroy(bridge);
4079 list_for_each_entry_safe(connector, ot,
4080 &dev->mode_config.connector_list, head) {
4081 connector->funcs->destroy(connector);
4084 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4086 drm_property_destroy(dev, property);
4089 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4091 drm_property_destroy_blob(dev, blob);
4095 * Single-threaded teardown context, so it's not required to grab the
4096 * fb_lock to protect against concurrent fb_list access. Contrary, it
4097 * would actually deadlock with the drm_framebuffer_cleanup function.
4099 * Also, if there are any framebuffers left, that's a driver leak now,
4100 * so politely WARN about this.
4102 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4103 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4104 drm_framebuffer_remove(fb);
4107 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4109 plane->funcs->destroy(plane);
4112 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4113 crtc->funcs->destroy(crtc);
4116 idr_destroy(&dev->mode_config.crtc_idr);
4118 EXPORT_SYMBOL(drm_mode_config_cleanup);