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_mode_probed_add - add a mode to a connector's probed mode list
679 * @connector: connector the new mode
682 * Add @mode to @connector's mode list for later use.
684 void drm_mode_probed_add(struct drm_connector *connector,
685 struct drm_display_mode *mode)
687 list_add_tail(&mode->head, &connector->probed_modes);
689 EXPORT_SYMBOL(drm_mode_probed_add);
692 * drm_mode_remove - remove and free a mode
693 * @connector: connector list to modify
694 * @mode: mode to remove
696 * Remove @mode from @connector's mode list, then free it.
698 static void drm_mode_remove(struct drm_connector *connector,
699 struct drm_display_mode *mode)
701 list_del(&mode->head);
702 drm_mode_destroy(connector->dev, mode);
706 * drm_connector_init - Init a preallocated connector
708 * @connector: the connector to init
709 * @funcs: callbacks for this connector
710 * @connector_type: user visible type of the connector
712 * Initialises a preallocated connector. Connectors should be
713 * subclassed as part of driver connector objects.
716 * Zero on success, error code on failure.
718 int drm_connector_init(struct drm_device *dev,
719 struct drm_connector *connector,
720 const struct drm_connector_funcs *funcs,
724 struct ida *connector_ida =
725 &drm_connector_enum_list[connector_type].ida;
727 drm_modeset_lock_all(dev);
729 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
733 connector->base.properties = &connector->properties;
734 connector->dev = dev;
735 connector->funcs = funcs;
736 connector->connector_type = connector_type;
737 connector->connector_type_id =
738 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
739 if (connector->connector_type_id < 0) {
740 ret = connector->connector_type_id;
741 drm_mode_object_put(dev, &connector->base);
744 INIT_LIST_HEAD(&connector->probed_modes);
745 INIT_LIST_HEAD(&connector->modes);
746 connector->edid_blob_ptr = NULL;
747 connector->status = connector_status_unknown;
749 list_add_tail(&connector->head, &dev->mode_config.connector_list);
750 dev->mode_config.num_connector++;
752 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
753 drm_object_attach_property(&connector->base,
754 dev->mode_config.edid_property,
757 drm_object_attach_property(&connector->base,
758 dev->mode_config.dpms_property, 0);
761 drm_modeset_unlock_all(dev);
765 EXPORT_SYMBOL(drm_connector_init);
768 * drm_connector_cleanup - cleans up an initialised connector
769 * @connector: connector to cleanup
771 * Cleans up the connector but doesn't free the object.
773 void drm_connector_cleanup(struct drm_connector *connector)
775 struct drm_device *dev = connector->dev;
776 struct drm_display_mode *mode, *t;
778 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
779 drm_mode_remove(connector, mode);
781 list_for_each_entry_safe(mode, t, &connector->modes, head)
782 drm_mode_remove(connector, mode);
784 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
785 connector->connector_type_id);
787 drm_mode_object_put(dev, &connector->base);
788 list_del(&connector->head);
789 dev->mode_config.num_connector--;
791 EXPORT_SYMBOL(drm_connector_cleanup);
793 void drm_connector_unplug_all(struct drm_device *dev)
795 struct drm_connector *connector;
797 /* taking the mode config mutex ends up in a clash with sysfs */
798 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
799 drm_sysfs_connector_remove(connector);
802 EXPORT_SYMBOL(drm_connector_unplug_all);
804 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
805 const struct drm_bridge_funcs *funcs)
809 drm_modeset_lock_all(dev);
811 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
816 bridge->funcs = funcs;
818 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
819 dev->mode_config.num_bridge++;
822 drm_modeset_unlock_all(dev);
825 EXPORT_SYMBOL(drm_bridge_init);
827 void drm_bridge_cleanup(struct drm_bridge *bridge)
829 struct drm_device *dev = bridge->dev;
831 drm_modeset_lock_all(dev);
832 drm_mode_object_put(dev, &bridge->base);
833 list_del(&bridge->head);
834 dev->mode_config.num_bridge--;
835 drm_modeset_unlock_all(dev);
837 EXPORT_SYMBOL(drm_bridge_cleanup);
839 int drm_encoder_init(struct drm_device *dev,
840 struct drm_encoder *encoder,
841 const struct drm_encoder_funcs *funcs,
846 drm_modeset_lock_all(dev);
848 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
853 encoder->encoder_type = encoder_type;
854 encoder->funcs = funcs;
856 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
857 dev->mode_config.num_encoder++;
860 drm_modeset_unlock_all(dev);
864 EXPORT_SYMBOL(drm_encoder_init);
866 void drm_encoder_cleanup(struct drm_encoder *encoder)
868 struct drm_device *dev = encoder->dev;
869 drm_modeset_lock_all(dev);
870 drm_mode_object_put(dev, &encoder->base);
871 list_del(&encoder->head);
872 dev->mode_config.num_encoder--;
873 drm_modeset_unlock_all(dev);
875 EXPORT_SYMBOL(drm_encoder_cleanup);
878 * drm_plane_init - Initialise a new plane object
880 * @plane: plane object to init
881 * @possible_crtcs: bitmask of possible CRTCs
882 * @funcs: callbacks for the new plane
883 * @formats: array of supported formats (%DRM_FORMAT_*)
884 * @format_count: number of elements in @formats
885 * @priv: plane is private (hidden from userspace)?
887 * Inits a new object created as base part of a driver plane object.
890 * Zero on success, error code on failure.
892 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
893 unsigned long possible_crtcs,
894 const struct drm_plane_funcs *funcs,
895 const uint32_t *formats, uint32_t format_count,
900 drm_modeset_lock_all(dev);
902 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
906 plane->base.properties = &plane->properties;
908 plane->funcs = funcs;
909 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
911 if (!plane->format_types) {
912 DRM_DEBUG_KMS("out of memory when allocating plane\n");
913 drm_mode_object_put(dev, &plane->base);
918 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
919 plane->format_count = format_count;
920 plane->possible_crtcs = possible_crtcs;
922 /* private planes are not exposed to userspace, but depending on
923 * display hardware, might be convenient to allow sharing programming
924 * for the scanout engine with the crtc implementation.
927 list_add_tail(&plane->head, &dev->mode_config.plane_list);
928 dev->mode_config.num_plane++;
930 INIT_LIST_HEAD(&plane->head);
934 drm_modeset_unlock_all(dev);
938 EXPORT_SYMBOL(drm_plane_init);
941 * drm_plane_cleanup - Clean up the core plane usage
942 * @plane: plane to cleanup
944 * This function cleans up @plane and removes it from the DRM mode setting
945 * core. Note that the function does *not* free the plane structure itself,
946 * this is the responsibility of the caller.
948 void drm_plane_cleanup(struct drm_plane *plane)
950 struct drm_device *dev = plane->dev;
952 drm_modeset_lock_all(dev);
953 kfree(plane->format_types);
954 drm_mode_object_put(dev, &plane->base);
955 /* if not added to a list, it must be a private plane */
956 if (!list_empty(&plane->head)) {
957 list_del(&plane->head);
958 dev->mode_config.num_plane--;
960 drm_modeset_unlock_all(dev);
962 EXPORT_SYMBOL(drm_plane_cleanup);
965 * drm_plane_force_disable - Forcibly disable a plane
966 * @plane: plane to disable
968 * Forces the plane to be disabled.
970 * Used when the plane's current framebuffer is destroyed,
971 * and when restoring fbdev mode.
973 void drm_plane_force_disable(struct drm_plane *plane)
980 ret = plane->funcs->disable_plane(plane);
982 DRM_ERROR("failed to disable plane with busy fb\n");
983 /* disconnect the plane from the fb and crtc: */
984 __drm_framebuffer_unreference(plane->fb);
988 EXPORT_SYMBOL(drm_plane_force_disable);
991 * drm_mode_create - create a new display mode
994 * Create a new drm_display_mode, give it an ID, and return it.
997 * Pointer to new mode on success, NULL on error.
999 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
1001 struct drm_display_mode *nmode;
1003 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
1007 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
1014 EXPORT_SYMBOL(drm_mode_create);
1017 * drm_mode_destroy - remove a mode
1019 * @mode: mode to remove
1021 * Free @mode's unique identifier, then free it.
1023 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
1028 drm_mode_object_put(dev, &mode->base);
1032 EXPORT_SYMBOL(drm_mode_destroy);
1034 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1036 struct drm_property *edid;
1037 struct drm_property *dpms;
1040 * Standard properties (apply to all connectors)
1042 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1043 DRM_MODE_PROP_IMMUTABLE,
1045 dev->mode_config.edid_property = edid;
1047 dpms = drm_property_create_enum(dev, 0,
1048 "DPMS", drm_dpms_enum_list,
1049 ARRAY_SIZE(drm_dpms_enum_list));
1050 dev->mode_config.dpms_property = dpms;
1056 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1059 * Called by a driver the first time a DVI-I connector is made.
1061 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1063 struct drm_property *dvi_i_selector;
1064 struct drm_property *dvi_i_subconnector;
1066 if (dev->mode_config.dvi_i_select_subconnector_property)
1070 drm_property_create_enum(dev, 0,
1071 "select subconnector",
1072 drm_dvi_i_select_enum_list,
1073 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1074 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1076 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1078 drm_dvi_i_subconnector_enum_list,
1079 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1080 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1084 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1087 * drm_create_tv_properties - create TV specific connector properties
1089 * @num_modes: number of different TV formats (modes) supported
1090 * @modes: array of pointers to strings containing name of each format
1092 * Called by a driver's TV initialization routine, this function creates
1093 * the TV specific connector properties for a given device. Caller is
1094 * responsible for allocating a list of format names and passing them to
1097 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1100 struct drm_property *tv_selector;
1101 struct drm_property *tv_subconnector;
1104 if (dev->mode_config.tv_select_subconnector_property)
1108 * Basic connector properties
1110 tv_selector = drm_property_create_enum(dev, 0,
1111 "select subconnector",
1112 drm_tv_select_enum_list,
1113 ARRAY_SIZE(drm_tv_select_enum_list));
1114 dev->mode_config.tv_select_subconnector_property = tv_selector;
1117 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1119 drm_tv_subconnector_enum_list,
1120 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1121 dev->mode_config.tv_subconnector_property = tv_subconnector;
1124 * Other, TV specific properties: margins & TV modes.
1126 dev->mode_config.tv_left_margin_property =
1127 drm_property_create_range(dev, 0, "left margin", 0, 100);
1129 dev->mode_config.tv_right_margin_property =
1130 drm_property_create_range(dev, 0, "right margin", 0, 100);
1132 dev->mode_config.tv_top_margin_property =
1133 drm_property_create_range(dev, 0, "top margin", 0, 100);
1135 dev->mode_config.tv_bottom_margin_property =
1136 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1138 dev->mode_config.tv_mode_property =
1139 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1141 for (i = 0; i < num_modes; i++)
1142 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1145 dev->mode_config.tv_brightness_property =
1146 drm_property_create_range(dev, 0, "brightness", 0, 100);
1148 dev->mode_config.tv_contrast_property =
1149 drm_property_create_range(dev, 0, "contrast", 0, 100);
1151 dev->mode_config.tv_flicker_reduction_property =
1152 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1154 dev->mode_config.tv_overscan_property =
1155 drm_property_create_range(dev, 0, "overscan", 0, 100);
1157 dev->mode_config.tv_saturation_property =
1158 drm_property_create_range(dev, 0, "saturation", 0, 100);
1160 dev->mode_config.tv_hue_property =
1161 drm_property_create_range(dev, 0, "hue", 0, 100);
1165 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1168 * drm_mode_create_scaling_mode_property - create scaling mode property
1171 * Called by a driver the first time it's needed, must be attached to desired
1174 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1176 struct drm_property *scaling_mode;
1178 if (dev->mode_config.scaling_mode_property)
1182 drm_property_create_enum(dev, 0, "scaling mode",
1183 drm_scaling_mode_enum_list,
1184 ARRAY_SIZE(drm_scaling_mode_enum_list));
1186 dev->mode_config.scaling_mode_property = scaling_mode;
1190 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1193 * drm_mode_create_dirty_property - create dirty property
1196 * Called by a driver the first time it's needed, must be attached to desired
1199 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1201 struct drm_property *dirty_info;
1203 if (dev->mode_config.dirty_info_property)
1207 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1209 drm_dirty_info_enum_list,
1210 ARRAY_SIZE(drm_dirty_info_enum_list));
1211 dev->mode_config.dirty_info_property = dirty_info;
1215 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1217 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1219 uint32_t total_objects = 0;
1221 total_objects += dev->mode_config.num_crtc;
1222 total_objects += dev->mode_config.num_connector;
1223 total_objects += dev->mode_config.num_encoder;
1224 total_objects += dev->mode_config.num_bridge;
1226 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1227 if (!group->id_list)
1230 group->num_crtcs = 0;
1231 group->num_connectors = 0;
1232 group->num_encoders = 0;
1233 group->num_bridges = 0;
1237 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1238 struct drm_mode_group *group)
1240 struct drm_crtc *crtc;
1241 struct drm_encoder *encoder;
1242 struct drm_connector *connector;
1243 struct drm_bridge *bridge;
1246 if ((ret = drm_mode_group_init(dev, group)))
1249 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1250 group->id_list[group->num_crtcs++] = crtc->base.id;
1252 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1253 group->id_list[group->num_crtcs + group->num_encoders++] =
1256 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1257 group->id_list[group->num_crtcs + group->num_encoders +
1258 group->num_connectors++] = connector->base.id;
1260 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1261 group->id_list[group->num_crtcs + group->num_encoders +
1262 group->num_connectors + group->num_bridges++] =
1267 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1270 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1271 * @out: drm_mode_modeinfo struct to return to the user
1272 * @in: drm_display_mode to use
1274 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1277 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1278 const struct drm_display_mode *in)
1280 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1281 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1282 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1283 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1284 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1285 "timing values too large for mode info\n");
1287 out->clock = in->clock;
1288 out->hdisplay = in->hdisplay;
1289 out->hsync_start = in->hsync_start;
1290 out->hsync_end = in->hsync_end;
1291 out->htotal = in->htotal;
1292 out->hskew = in->hskew;
1293 out->vdisplay = in->vdisplay;
1294 out->vsync_start = in->vsync_start;
1295 out->vsync_end = in->vsync_end;
1296 out->vtotal = in->vtotal;
1297 out->vscan = in->vscan;
1298 out->vrefresh = in->vrefresh;
1299 out->flags = in->flags;
1300 out->type = in->type;
1301 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1302 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1306 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1307 * @out: drm_display_mode to return to the user
1308 * @in: drm_mode_modeinfo to use
1310 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1314 * Zero on success, errno on failure.
1316 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1317 const struct drm_mode_modeinfo *in)
1319 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1322 /* At most, 1 set bit describing the 3D layout of the mode */
1323 if (hweight32(in->flags & DRM_MODE_FLAG_3D_MASK) > 1)
1326 out->clock = in->clock;
1327 out->hdisplay = in->hdisplay;
1328 out->hsync_start = in->hsync_start;
1329 out->hsync_end = in->hsync_end;
1330 out->htotal = in->htotal;
1331 out->hskew = in->hskew;
1332 out->vdisplay = in->vdisplay;
1333 out->vsync_start = in->vsync_start;
1334 out->vsync_end = in->vsync_end;
1335 out->vtotal = in->vtotal;
1336 out->vscan = in->vscan;
1337 out->vrefresh = in->vrefresh;
1338 out->flags = in->flags;
1339 out->type = in->type;
1340 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1341 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1347 * drm_mode_getresources - get graphics configuration
1348 * @dev: drm device for the ioctl
1349 * @data: data pointer for the ioctl
1350 * @file_priv: drm file for the ioctl call
1352 * Construct a set of configuration description structures and return
1353 * them to the user, including CRTC, connector and framebuffer configuration.
1355 * Called by the user via ioctl.
1358 * Zero on success, errno on failure.
1360 int drm_mode_getresources(struct drm_device *dev, void *data,
1361 struct drm_file *file_priv)
1363 struct drm_mode_card_res *card_res = data;
1364 struct list_head *lh;
1365 struct drm_framebuffer *fb;
1366 struct drm_connector *connector;
1367 struct drm_crtc *crtc;
1368 struct drm_encoder *encoder;
1370 int connector_count = 0;
1373 int encoder_count = 0;
1375 uint32_t __user *fb_id;
1376 uint32_t __user *crtc_id;
1377 uint32_t __user *connector_id;
1378 uint32_t __user *encoder_id;
1379 struct drm_mode_group *mode_group;
1381 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1385 mutex_lock(&file_priv->fbs_lock);
1387 * For the non-control nodes we need to limit the list of resources
1388 * by IDs in the group list for this node
1390 list_for_each(lh, &file_priv->fbs)
1393 /* handle this in 4 parts */
1395 if (card_res->count_fbs >= fb_count) {
1397 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1398 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1399 if (put_user(fb->base.id, fb_id + copied)) {
1400 mutex_unlock(&file_priv->fbs_lock);
1406 card_res->count_fbs = fb_count;
1407 mutex_unlock(&file_priv->fbs_lock);
1409 drm_modeset_lock_all(dev);
1410 mode_group = &file_priv->master->minor->mode_group;
1411 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1413 list_for_each(lh, &dev->mode_config.crtc_list)
1416 list_for_each(lh, &dev->mode_config.connector_list)
1419 list_for_each(lh, &dev->mode_config.encoder_list)
1423 crtc_count = mode_group->num_crtcs;
1424 connector_count = mode_group->num_connectors;
1425 encoder_count = mode_group->num_encoders;
1428 card_res->max_height = dev->mode_config.max_height;
1429 card_res->min_height = dev->mode_config.min_height;
1430 card_res->max_width = dev->mode_config.max_width;
1431 card_res->min_width = dev->mode_config.min_width;
1434 if (card_res->count_crtcs >= crtc_count) {
1436 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1437 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1438 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1440 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1441 if (put_user(crtc->base.id, crtc_id + copied)) {
1448 for (i = 0; i < mode_group->num_crtcs; i++) {
1449 if (put_user(mode_group->id_list[i],
1450 crtc_id + copied)) {
1458 card_res->count_crtcs = crtc_count;
1461 if (card_res->count_encoders >= encoder_count) {
1463 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1464 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1465 list_for_each_entry(encoder,
1466 &dev->mode_config.encoder_list,
1468 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1469 drm_get_encoder_name(encoder));
1470 if (put_user(encoder->base.id, encoder_id +
1478 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1479 if (put_user(mode_group->id_list[i],
1480 encoder_id + copied)) {
1489 card_res->count_encoders = encoder_count;
1492 if (card_res->count_connectors >= connector_count) {
1494 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1495 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1496 list_for_each_entry(connector,
1497 &dev->mode_config.connector_list,
1499 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1501 drm_get_connector_name(connector));
1502 if (put_user(connector->base.id,
1503 connector_id + copied)) {
1510 int start = mode_group->num_crtcs +
1511 mode_group->num_encoders;
1512 for (i = start; i < start + mode_group->num_connectors; i++) {
1513 if (put_user(mode_group->id_list[i],
1514 connector_id + copied)) {
1522 card_res->count_connectors = connector_count;
1524 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1525 card_res->count_connectors, card_res->count_encoders);
1528 drm_modeset_unlock_all(dev);
1533 * drm_mode_getcrtc - get CRTC configuration
1534 * @dev: drm device for the ioctl
1535 * @data: data pointer for the ioctl
1536 * @file_priv: drm file for the ioctl call
1538 * Construct a CRTC configuration structure to return to the user.
1540 * Called by the user via ioctl.
1543 * Zero on success, errno on failure.
1545 int drm_mode_getcrtc(struct drm_device *dev,
1546 void *data, struct drm_file *file_priv)
1548 struct drm_mode_crtc *crtc_resp = data;
1549 struct drm_crtc *crtc;
1550 struct drm_mode_object *obj;
1553 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1556 drm_modeset_lock_all(dev);
1558 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1559 DRM_MODE_OBJECT_CRTC);
1564 crtc = obj_to_crtc(obj);
1566 crtc_resp->x = crtc->x;
1567 crtc_resp->y = crtc->y;
1568 crtc_resp->gamma_size = crtc->gamma_size;
1570 crtc_resp->fb_id = crtc->fb->base.id;
1572 crtc_resp->fb_id = 0;
1574 if (crtc->enabled) {
1576 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1577 crtc_resp->mode_valid = 1;
1580 crtc_resp->mode_valid = 0;
1584 drm_modeset_unlock_all(dev);
1588 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1589 const struct drm_file *file_priv)
1592 * If user-space hasn't configured the driver to expose the stereo 3D
1593 * modes, don't expose them.
1595 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1602 * drm_mode_getconnector - get connector configuration
1603 * @dev: drm device for the ioctl
1604 * @data: data pointer for the ioctl
1605 * @file_priv: drm file for the ioctl call
1607 * Construct a connector configuration structure to return to the user.
1609 * Called by the user via ioctl.
1612 * Zero on success, errno on failure.
1614 int drm_mode_getconnector(struct drm_device *dev, void *data,
1615 struct drm_file *file_priv)
1617 struct drm_mode_get_connector *out_resp = data;
1618 struct drm_mode_object *obj;
1619 struct drm_connector *connector;
1620 struct drm_display_mode *mode;
1622 int props_count = 0;
1623 int encoders_count = 0;
1627 struct drm_mode_modeinfo u_mode;
1628 struct drm_mode_modeinfo __user *mode_ptr;
1629 uint32_t __user *prop_ptr;
1630 uint64_t __user *prop_values;
1631 uint32_t __user *encoder_ptr;
1633 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1636 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1638 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1640 mutex_lock(&dev->mode_config.mutex);
1642 obj = drm_mode_object_find(dev, out_resp->connector_id,
1643 DRM_MODE_OBJECT_CONNECTOR);
1648 connector = obj_to_connector(obj);
1650 props_count = connector->properties.count;
1652 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1653 if (connector->encoder_ids[i] != 0) {
1658 if (out_resp->count_modes == 0) {
1659 connector->funcs->fill_modes(connector,
1660 dev->mode_config.max_width,
1661 dev->mode_config.max_height);
1664 /* delayed so we get modes regardless of pre-fill_modes state */
1665 list_for_each_entry(mode, &connector->modes, head)
1666 if (drm_mode_expose_to_userspace(mode, file_priv))
1669 out_resp->connector_id = connector->base.id;
1670 out_resp->connector_type = connector->connector_type;
1671 out_resp->connector_type_id = connector->connector_type_id;
1672 out_resp->mm_width = connector->display_info.width_mm;
1673 out_resp->mm_height = connector->display_info.height_mm;
1674 out_resp->subpixel = connector->display_info.subpixel_order;
1675 out_resp->connection = connector->status;
1676 if (connector->encoder)
1677 out_resp->encoder_id = connector->encoder->base.id;
1679 out_resp->encoder_id = 0;
1682 * This ioctl is called twice, once to determine how much space is
1683 * needed, and the 2nd time to fill it.
1685 if ((out_resp->count_modes >= mode_count) && mode_count) {
1687 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1688 list_for_each_entry(mode, &connector->modes, head) {
1689 if (!drm_mode_expose_to_userspace(mode, file_priv))
1692 drm_crtc_convert_to_umode(&u_mode, mode);
1693 if (copy_to_user(mode_ptr + copied,
1694 &u_mode, sizeof(u_mode))) {
1701 out_resp->count_modes = mode_count;
1703 if ((out_resp->count_props >= props_count) && props_count) {
1705 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1706 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1707 for (i = 0; i < connector->properties.count; i++) {
1708 if (put_user(connector->properties.ids[i],
1709 prop_ptr + copied)) {
1714 if (put_user(connector->properties.values[i],
1715 prop_values + copied)) {
1722 out_resp->count_props = props_count;
1724 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1726 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1727 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1728 if (connector->encoder_ids[i] != 0) {
1729 if (put_user(connector->encoder_ids[i],
1730 encoder_ptr + copied)) {
1738 out_resp->count_encoders = encoders_count;
1741 mutex_unlock(&dev->mode_config.mutex);
1746 int drm_mode_getencoder(struct drm_device *dev, void *data,
1747 struct drm_file *file_priv)
1749 struct drm_mode_get_encoder *enc_resp = data;
1750 struct drm_mode_object *obj;
1751 struct drm_encoder *encoder;
1754 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1757 drm_modeset_lock_all(dev);
1758 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1759 DRM_MODE_OBJECT_ENCODER);
1764 encoder = obj_to_encoder(obj);
1767 enc_resp->crtc_id = encoder->crtc->base.id;
1769 enc_resp->crtc_id = 0;
1770 enc_resp->encoder_type = encoder->encoder_type;
1771 enc_resp->encoder_id = encoder->base.id;
1772 enc_resp->possible_crtcs = encoder->possible_crtcs;
1773 enc_resp->possible_clones = encoder->possible_clones;
1776 drm_modeset_unlock_all(dev);
1781 * drm_mode_getplane_res - get plane info
1784 * @file_priv: DRM file info
1786 * Return an plane count and set of IDs.
1788 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1789 struct drm_file *file_priv)
1791 struct drm_mode_get_plane_res *plane_resp = data;
1792 struct drm_mode_config *config;
1793 struct drm_plane *plane;
1794 uint32_t __user *plane_ptr;
1795 int copied = 0, ret = 0;
1797 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1800 drm_modeset_lock_all(dev);
1801 config = &dev->mode_config;
1804 * This ioctl is called twice, once to determine how much space is
1805 * needed, and the 2nd time to fill it.
1807 if (config->num_plane &&
1808 (plane_resp->count_planes >= config->num_plane)) {
1809 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1811 list_for_each_entry(plane, &config->plane_list, head) {
1812 if (put_user(plane->base.id, plane_ptr + copied)) {
1819 plane_resp->count_planes = config->num_plane;
1822 drm_modeset_unlock_all(dev);
1827 * drm_mode_getplane - get plane info
1830 * @file_priv: DRM file info
1832 * Return plane info, including formats supported, gamma size, any
1835 int drm_mode_getplane(struct drm_device *dev, void *data,
1836 struct drm_file *file_priv)
1838 struct drm_mode_get_plane *plane_resp = data;
1839 struct drm_mode_object *obj;
1840 struct drm_plane *plane;
1841 uint32_t __user *format_ptr;
1844 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1847 drm_modeset_lock_all(dev);
1848 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1849 DRM_MODE_OBJECT_PLANE);
1854 plane = obj_to_plane(obj);
1857 plane_resp->crtc_id = plane->crtc->base.id;
1859 plane_resp->crtc_id = 0;
1862 plane_resp->fb_id = plane->fb->base.id;
1864 plane_resp->fb_id = 0;
1866 plane_resp->plane_id = plane->base.id;
1867 plane_resp->possible_crtcs = plane->possible_crtcs;
1868 plane_resp->gamma_size = 0;
1871 * This ioctl is called twice, once to determine how much space is
1872 * needed, and the 2nd time to fill it.
1874 if (plane->format_count &&
1875 (plane_resp->count_format_types >= plane->format_count)) {
1876 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1877 if (copy_to_user(format_ptr,
1878 plane->format_types,
1879 sizeof(uint32_t) * plane->format_count)) {
1884 plane_resp->count_format_types = plane->format_count;
1887 drm_modeset_unlock_all(dev);
1892 * drm_mode_setplane - set up or tear down an plane
1894 * @data: ioctl data*
1895 * @file_priv: DRM file info
1897 * Set plane info, including placement, fb, scaling, and other factors.
1898 * Or pass a NULL fb to disable.
1900 int drm_mode_setplane(struct drm_device *dev, void *data,
1901 struct drm_file *file_priv)
1903 struct drm_mode_set_plane *plane_req = data;
1904 struct drm_mode_object *obj;
1905 struct drm_plane *plane;
1906 struct drm_crtc *crtc;
1907 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1909 unsigned int fb_width, fb_height;
1912 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1916 * First, find the plane, crtc, and fb objects. If not available,
1917 * we don't bother to call the driver.
1919 obj = drm_mode_object_find(dev, plane_req->plane_id,
1920 DRM_MODE_OBJECT_PLANE);
1922 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1923 plane_req->plane_id);
1926 plane = obj_to_plane(obj);
1928 /* No fb means shut it down */
1929 if (!plane_req->fb_id) {
1930 drm_modeset_lock_all(dev);
1932 plane->funcs->disable_plane(plane);
1935 drm_modeset_unlock_all(dev);
1939 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1940 DRM_MODE_OBJECT_CRTC);
1942 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1943 plane_req->crtc_id);
1947 crtc = obj_to_crtc(obj);
1949 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1951 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1957 /* Check whether this plane supports the fb pixel format. */
1958 for (i = 0; i < plane->format_count; i++)
1959 if (fb->pixel_format == plane->format_types[i])
1961 if (i == plane->format_count) {
1962 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1963 drm_get_format_name(fb->pixel_format));
1968 fb_width = fb->width << 16;
1969 fb_height = fb->height << 16;
1971 /* Make sure source coordinates are inside the fb. */
1972 if (plane_req->src_w > fb_width ||
1973 plane_req->src_x > fb_width - plane_req->src_w ||
1974 plane_req->src_h > fb_height ||
1975 plane_req->src_y > fb_height - plane_req->src_h) {
1976 DRM_DEBUG_KMS("Invalid source coordinates "
1977 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1978 plane_req->src_w >> 16,
1979 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1980 plane_req->src_h >> 16,
1981 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1982 plane_req->src_x >> 16,
1983 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1984 plane_req->src_y >> 16,
1985 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1990 /* Give drivers some help against integer overflows */
1991 if (plane_req->crtc_w > INT_MAX ||
1992 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1993 plane_req->crtc_h > INT_MAX ||
1994 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1995 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1996 plane_req->crtc_w, plane_req->crtc_h,
1997 plane_req->crtc_x, plane_req->crtc_y);
2002 drm_modeset_lock_all(dev);
2003 ret = plane->funcs->update_plane(plane, crtc, fb,
2004 plane_req->crtc_x, plane_req->crtc_y,
2005 plane_req->crtc_w, plane_req->crtc_h,
2006 plane_req->src_x, plane_req->src_y,
2007 plane_req->src_w, plane_req->src_h);
2014 drm_modeset_unlock_all(dev);
2018 drm_framebuffer_unreference(fb);
2020 drm_framebuffer_unreference(old_fb);
2026 * drm_mode_set_config_internal - helper to call ->set_config
2027 * @set: modeset config to set
2029 * This is a little helper to wrap internal calls to the ->set_config driver
2030 * interface. The only thing it adds is correct refcounting dance.
2032 int drm_mode_set_config_internal(struct drm_mode_set *set)
2034 struct drm_crtc *crtc = set->crtc;
2035 struct drm_framebuffer *fb;
2036 struct drm_crtc *tmp;
2040 * NOTE: ->set_config can also disable other crtcs (if we steal all
2041 * connectors from it), hence we need to refcount the fbs across all
2042 * crtcs. Atomic modeset will have saner semantics ...
2044 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2045 tmp->old_fb = tmp->fb;
2049 ret = crtc->funcs->set_config(set);
2051 /* crtc->fb must be updated by ->set_config, enforces this. */
2052 WARN_ON(fb != crtc->fb);
2055 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2057 drm_framebuffer_reference(tmp->fb);
2059 drm_framebuffer_unreference(tmp->old_fb);
2064 EXPORT_SYMBOL(drm_mode_set_config_internal);
2067 * Checks that the framebuffer is big enough for the CRTC viewport
2068 * (x, y, hdisplay, vdisplay)
2070 static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2072 const struct drm_display_mode *mode,
2073 const struct drm_framebuffer *fb)
2076 int hdisplay, vdisplay;
2078 hdisplay = mode->hdisplay;
2079 vdisplay = mode->vdisplay;
2081 if (drm_mode_is_stereo(mode)) {
2082 struct drm_display_mode adjusted = *mode;
2084 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2085 hdisplay = adjusted.crtc_hdisplay;
2086 vdisplay = adjusted.crtc_vdisplay;
2089 if (crtc->invert_dimensions)
2090 swap(hdisplay, vdisplay);
2092 if (hdisplay > fb->width ||
2093 vdisplay > fb->height ||
2094 x > fb->width - hdisplay ||
2095 y > fb->height - vdisplay) {
2096 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2097 fb->width, fb->height, hdisplay, vdisplay, x, y,
2098 crtc->invert_dimensions ? " (inverted)" : "");
2106 * drm_mode_setcrtc - set CRTC configuration
2107 * @dev: drm device for the ioctl
2108 * @data: data pointer for the ioctl
2109 * @file_priv: drm file for the ioctl call
2111 * Build a new CRTC configuration based on user request.
2113 * Called by the user via ioctl.
2116 * Zero on success, errno on failure.
2118 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2119 struct drm_file *file_priv)
2121 struct drm_mode_config *config = &dev->mode_config;
2122 struct drm_mode_crtc *crtc_req = data;
2123 struct drm_mode_object *obj;
2124 struct drm_crtc *crtc;
2125 struct drm_connector **connector_set = NULL, *connector;
2126 struct drm_framebuffer *fb = NULL;
2127 struct drm_display_mode *mode = NULL;
2128 struct drm_mode_set set;
2129 uint32_t __user *set_connectors_ptr;
2133 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2136 /* For some reason crtc x/y offsets are signed internally. */
2137 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2140 drm_modeset_lock_all(dev);
2141 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2142 DRM_MODE_OBJECT_CRTC);
2144 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2148 crtc = obj_to_crtc(obj);
2149 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2151 if (crtc_req->mode_valid) {
2152 /* If we have a mode we need a framebuffer. */
2153 /* If we pass -1, set the mode with the currently bound fb */
2154 if (crtc_req->fb_id == -1) {
2156 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2161 /* Make refcounting symmetric with the lookup path. */
2162 drm_framebuffer_reference(fb);
2164 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2166 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2173 mode = drm_mode_create(dev);
2179 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2181 DRM_DEBUG_KMS("Invalid mode\n");
2185 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2187 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2194 if (crtc_req->count_connectors == 0 && mode) {
2195 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2200 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2201 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2202 crtc_req->count_connectors);
2207 if (crtc_req->count_connectors > 0) {
2210 /* Avoid unbounded kernel memory allocation */
2211 if (crtc_req->count_connectors > config->num_connector) {
2216 connector_set = kmalloc(crtc_req->count_connectors *
2217 sizeof(struct drm_connector *),
2219 if (!connector_set) {
2224 for (i = 0; i < crtc_req->count_connectors; i++) {
2225 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2226 if (get_user(out_id, &set_connectors_ptr[i])) {
2231 obj = drm_mode_object_find(dev, out_id,
2232 DRM_MODE_OBJECT_CONNECTOR);
2234 DRM_DEBUG_KMS("Connector id %d unknown\n",
2239 connector = obj_to_connector(obj);
2240 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2242 drm_get_connector_name(connector));
2244 connector_set[i] = connector;
2249 set.x = crtc_req->x;
2250 set.y = crtc_req->y;
2252 set.connectors = connector_set;
2253 set.num_connectors = crtc_req->count_connectors;
2255 ret = drm_mode_set_config_internal(&set);
2259 drm_framebuffer_unreference(fb);
2261 kfree(connector_set);
2262 drm_mode_destroy(dev, mode);
2263 drm_modeset_unlock_all(dev);
2267 static int drm_mode_cursor_common(struct drm_device *dev,
2268 struct drm_mode_cursor2 *req,
2269 struct drm_file *file_priv)
2271 struct drm_mode_object *obj;
2272 struct drm_crtc *crtc;
2275 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2278 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2281 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2283 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2286 crtc = obj_to_crtc(obj);
2288 mutex_lock(&crtc->mutex);
2289 if (req->flags & DRM_MODE_CURSOR_BO) {
2290 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2294 /* Turns off the cursor if handle is 0 */
2295 if (crtc->funcs->cursor_set2)
2296 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2297 req->width, req->height, req->hot_x, req->hot_y);
2299 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2300 req->width, req->height);
2303 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2304 if (crtc->funcs->cursor_move) {
2305 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2312 mutex_unlock(&crtc->mutex);
2317 int drm_mode_cursor_ioctl(struct drm_device *dev,
2318 void *data, struct drm_file *file_priv)
2320 struct drm_mode_cursor *req = data;
2321 struct drm_mode_cursor2 new_req;
2323 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2324 new_req.hot_x = new_req.hot_y = 0;
2326 return drm_mode_cursor_common(dev, &new_req, file_priv);
2329 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2330 void *data, struct drm_file *file_priv)
2332 struct drm_mode_cursor2 *req = data;
2333 return drm_mode_cursor_common(dev, req, file_priv);
2336 /* Original addfb only supported RGB formats, so figure out which one */
2337 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2343 fmt = DRM_FORMAT_C8;
2347 fmt = DRM_FORMAT_XRGB1555;
2349 fmt = DRM_FORMAT_RGB565;
2352 fmt = DRM_FORMAT_RGB888;
2356 fmt = DRM_FORMAT_XRGB8888;
2357 else if (depth == 30)
2358 fmt = DRM_FORMAT_XRGB2101010;
2360 fmt = DRM_FORMAT_ARGB8888;
2363 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2364 fmt = DRM_FORMAT_XRGB8888;
2370 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2373 * drm_mode_addfb - add an FB to the graphics configuration
2374 * @dev: drm device for the ioctl
2375 * @data: data pointer for the ioctl
2376 * @file_priv: drm file for the ioctl call
2378 * Add a new FB to the specified CRTC, given a user request.
2380 * Called by the user via ioctl.
2383 * Zero on success, errno on failure.
2385 int drm_mode_addfb(struct drm_device *dev,
2386 void *data, struct drm_file *file_priv)
2388 struct drm_mode_fb_cmd *or = data;
2389 struct drm_mode_fb_cmd2 r = {};
2390 struct drm_mode_config *config = &dev->mode_config;
2391 struct drm_framebuffer *fb;
2394 /* Use new struct with format internally */
2395 r.fb_id = or->fb_id;
2396 r.width = or->width;
2397 r.height = or->height;
2398 r.pitches[0] = or->pitch;
2399 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2400 r.handles[0] = or->handle;
2402 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2405 if ((config->min_width > r.width) || (r.width > config->max_width))
2408 if ((config->min_height > r.height) || (r.height > config->max_height))
2411 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2413 DRM_DEBUG_KMS("could not create framebuffer\n");
2417 mutex_lock(&file_priv->fbs_lock);
2418 or->fb_id = fb->base.id;
2419 list_add(&fb->filp_head, &file_priv->fbs);
2420 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2421 mutex_unlock(&file_priv->fbs_lock);
2426 static int format_check(const struct drm_mode_fb_cmd2 *r)
2428 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2432 case DRM_FORMAT_RGB332:
2433 case DRM_FORMAT_BGR233:
2434 case DRM_FORMAT_XRGB4444:
2435 case DRM_FORMAT_XBGR4444:
2436 case DRM_FORMAT_RGBX4444:
2437 case DRM_FORMAT_BGRX4444:
2438 case DRM_FORMAT_ARGB4444:
2439 case DRM_FORMAT_ABGR4444:
2440 case DRM_FORMAT_RGBA4444:
2441 case DRM_FORMAT_BGRA4444:
2442 case DRM_FORMAT_XRGB1555:
2443 case DRM_FORMAT_XBGR1555:
2444 case DRM_FORMAT_RGBX5551:
2445 case DRM_FORMAT_BGRX5551:
2446 case DRM_FORMAT_ARGB1555:
2447 case DRM_FORMAT_ABGR1555:
2448 case DRM_FORMAT_RGBA5551:
2449 case DRM_FORMAT_BGRA5551:
2450 case DRM_FORMAT_RGB565:
2451 case DRM_FORMAT_BGR565:
2452 case DRM_FORMAT_RGB888:
2453 case DRM_FORMAT_BGR888:
2454 case DRM_FORMAT_XRGB8888:
2455 case DRM_FORMAT_XBGR8888:
2456 case DRM_FORMAT_RGBX8888:
2457 case DRM_FORMAT_BGRX8888:
2458 case DRM_FORMAT_ARGB8888:
2459 case DRM_FORMAT_ABGR8888:
2460 case DRM_FORMAT_RGBA8888:
2461 case DRM_FORMAT_BGRA8888:
2462 case DRM_FORMAT_XRGB2101010:
2463 case DRM_FORMAT_XBGR2101010:
2464 case DRM_FORMAT_RGBX1010102:
2465 case DRM_FORMAT_BGRX1010102:
2466 case DRM_FORMAT_ARGB2101010:
2467 case DRM_FORMAT_ABGR2101010:
2468 case DRM_FORMAT_RGBA1010102:
2469 case DRM_FORMAT_BGRA1010102:
2470 case DRM_FORMAT_YUYV:
2471 case DRM_FORMAT_YVYU:
2472 case DRM_FORMAT_UYVY:
2473 case DRM_FORMAT_VYUY:
2474 case DRM_FORMAT_AYUV:
2475 case DRM_FORMAT_NV12:
2476 case DRM_FORMAT_NV21:
2477 case DRM_FORMAT_NV16:
2478 case DRM_FORMAT_NV61:
2479 case DRM_FORMAT_NV24:
2480 case DRM_FORMAT_NV42:
2481 case DRM_FORMAT_YUV410:
2482 case DRM_FORMAT_YVU410:
2483 case DRM_FORMAT_YUV411:
2484 case DRM_FORMAT_YVU411:
2485 case DRM_FORMAT_YUV420:
2486 case DRM_FORMAT_YVU420:
2487 case DRM_FORMAT_YUV422:
2488 case DRM_FORMAT_YVU422:
2489 case DRM_FORMAT_YUV444:
2490 case DRM_FORMAT_YVU444:
2497 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2499 int ret, hsub, vsub, num_planes, i;
2501 ret = format_check(r);
2503 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2504 drm_get_format_name(r->pixel_format));
2508 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2509 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2510 num_planes = drm_format_num_planes(r->pixel_format);
2512 if (r->width == 0 || r->width % hsub) {
2513 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2517 if (r->height == 0 || r->height % vsub) {
2518 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2522 for (i = 0; i < num_planes; i++) {
2523 unsigned int width = r->width / (i != 0 ? hsub : 1);
2524 unsigned int height = r->height / (i != 0 ? vsub : 1);
2525 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2527 if (!r->handles[i]) {
2528 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2532 if ((uint64_t) width * cpp > UINT_MAX)
2535 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2538 if (r->pitches[i] < width * cpp) {
2539 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2548 * drm_mode_addfb2 - add an FB to the graphics configuration
2549 * @dev: drm device for the ioctl
2550 * @data: data pointer for the ioctl
2551 * @file_priv: drm file for the ioctl call
2553 * Add a new FB to the specified CRTC, given a user request with format.
2555 * Called by the user via ioctl.
2558 * Zero on success, errno on failure.
2560 int drm_mode_addfb2(struct drm_device *dev,
2561 void *data, struct drm_file *file_priv)
2563 struct drm_mode_fb_cmd2 *r = data;
2564 struct drm_mode_config *config = &dev->mode_config;
2565 struct drm_framebuffer *fb;
2568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2571 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2572 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2576 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2577 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2578 r->width, config->min_width, config->max_width);
2581 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2582 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2583 r->height, config->min_height, config->max_height);
2587 ret = framebuffer_check(r);
2591 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2593 DRM_DEBUG_KMS("could not create framebuffer\n");
2597 mutex_lock(&file_priv->fbs_lock);
2598 r->fb_id = fb->base.id;
2599 list_add(&fb->filp_head, &file_priv->fbs);
2600 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2601 mutex_unlock(&file_priv->fbs_lock);
2608 * drm_mode_rmfb - remove an FB from the configuration
2609 * @dev: drm device for the ioctl
2610 * @data: data pointer for the ioctl
2611 * @file_priv: drm file for the ioctl call
2613 * Remove the FB specified by the user.
2615 * Called by the user via ioctl.
2618 * Zero on success, errno on failure.
2620 int drm_mode_rmfb(struct drm_device *dev,
2621 void *data, struct drm_file *file_priv)
2623 struct drm_framebuffer *fb = NULL;
2624 struct drm_framebuffer *fbl = NULL;
2625 uint32_t *id = data;
2628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2631 mutex_lock(&file_priv->fbs_lock);
2632 mutex_lock(&dev->mode_config.fb_lock);
2633 fb = __drm_framebuffer_lookup(dev, *id);
2637 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2643 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2644 __drm_framebuffer_unregister(dev, fb);
2646 list_del_init(&fb->filp_head);
2647 mutex_unlock(&dev->mode_config.fb_lock);
2648 mutex_unlock(&file_priv->fbs_lock);
2650 drm_framebuffer_remove(fb);
2655 mutex_unlock(&dev->mode_config.fb_lock);
2656 mutex_unlock(&file_priv->fbs_lock);
2662 * drm_mode_getfb - get FB info
2663 * @dev: drm device for the ioctl
2664 * @data: data pointer for the ioctl
2665 * @file_priv: drm file for the ioctl call
2667 * Lookup the FB given its ID and return info about it.
2669 * Called by the user via ioctl.
2672 * Zero on success, errno on failure.
2674 int drm_mode_getfb(struct drm_device *dev,
2675 void *data, struct drm_file *file_priv)
2677 struct drm_mode_fb_cmd *r = data;
2678 struct drm_framebuffer *fb;
2681 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2684 fb = drm_framebuffer_lookup(dev, r->fb_id);
2688 r->height = fb->height;
2689 r->width = fb->width;
2690 r->depth = fb->depth;
2691 r->bpp = fb->bits_per_pixel;
2692 r->pitch = fb->pitches[0];
2693 if (fb->funcs->create_handle) {
2694 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
2695 ret = fb->funcs->create_handle(fb, file_priv,
2698 /* GET_FB() is an unprivileged ioctl so we must not
2699 * return a buffer-handle to non-master processes! For
2700 * backwards-compatibility reasons, we cannot make
2701 * GET_FB() privileged, so just return an invalid handle
2702 * for non-masters. */
2710 drm_framebuffer_unreference(fb);
2715 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2716 void *data, struct drm_file *file_priv)
2718 struct drm_clip_rect __user *clips_ptr;
2719 struct drm_clip_rect *clips = NULL;
2720 struct drm_mode_fb_dirty_cmd *r = data;
2721 struct drm_framebuffer *fb;
2726 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2729 fb = drm_framebuffer_lookup(dev, r->fb_id);
2733 num_clips = r->num_clips;
2734 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2736 if (!num_clips != !clips_ptr) {
2741 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2743 /* If userspace annotates copy, clips must come in pairs */
2744 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2749 if (num_clips && clips_ptr) {
2750 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2754 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2760 ret = copy_from_user(clips, clips_ptr,
2761 num_clips * sizeof(*clips));
2768 if (fb->funcs->dirty) {
2769 drm_modeset_lock_all(dev);
2770 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2772 drm_modeset_unlock_all(dev);
2780 drm_framebuffer_unreference(fb);
2787 * drm_fb_release - remove and free the FBs on this file
2788 * @priv: drm file for the ioctl
2790 * Destroy all the FBs associated with @filp.
2792 * Called by the user via ioctl.
2795 * Zero on success, errno on failure.
2797 void drm_fb_release(struct drm_file *priv)
2799 struct drm_device *dev = priv->minor->dev;
2800 struct drm_framebuffer *fb, *tfb;
2802 mutex_lock(&priv->fbs_lock);
2803 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2805 mutex_lock(&dev->mode_config.fb_lock);
2806 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2807 __drm_framebuffer_unregister(dev, fb);
2808 mutex_unlock(&dev->mode_config.fb_lock);
2810 list_del_init(&fb->filp_head);
2812 /* This will also drop the fpriv->fbs reference. */
2813 drm_framebuffer_remove(fb);
2815 mutex_unlock(&priv->fbs_lock);
2818 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2819 const char *name, int num_values)
2821 struct drm_property *property = NULL;
2824 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2829 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2830 if (!property->values)
2834 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2838 property->flags = flags;
2839 property->num_values = num_values;
2840 INIT_LIST_HEAD(&property->enum_blob_list);
2843 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2844 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2847 list_add_tail(&property->head, &dev->mode_config.property_list);
2850 kfree(property->values);
2854 EXPORT_SYMBOL(drm_property_create);
2856 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2858 const struct drm_prop_enum_list *props,
2861 struct drm_property *property;
2864 flags |= DRM_MODE_PROP_ENUM;
2866 property = drm_property_create(dev, flags, name, num_values);
2870 for (i = 0; i < num_values; i++) {
2871 ret = drm_property_add_enum(property, i,
2875 drm_property_destroy(dev, property);
2882 EXPORT_SYMBOL(drm_property_create_enum);
2884 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2885 int flags, const char *name,
2886 const struct drm_prop_enum_list *props,
2889 struct drm_property *property;
2892 flags |= DRM_MODE_PROP_BITMASK;
2894 property = drm_property_create(dev, flags, name, num_values);
2898 for (i = 0; i < num_values; i++) {
2899 ret = drm_property_add_enum(property, i,
2903 drm_property_destroy(dev, property);
2910 EXPORT_SYMBOL(drm_property_create_bitmask);
2912 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2914 uint64_t min, uint64_t max)
2916 struct drm_property *property;
2918 flags |= DRM_MODE_PROP_RANGE;
2920 property = drm_property_create(dev, flags, name, 2);
2924 property->values[0] = min;
2925 property->values[1] = max;
2929 EXPORT_SYMBOL(drm_property_create_range);
2931 int drm_property_add_enum(struct drm_property *property, int index,
2932 uint64_t value, const char *name)
2934 struct drm_property_enum *prop_enum;
2936 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2940 * Bitmask enum properties have the additional constraint of values
2943 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2946 if (!list_empty(&property->enum_blob_list)) {
2947 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2948 if (prop_enum->value == value) {
2949 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2950 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2956 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2960 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2961 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2962 prop_enum->value = value;
2964 property->values[index] = value;
2965 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2968 EXPORT_SYMBOL(drm_property_add_enum);
2970 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2972 struct drm_property_enum *prop_enum, *pt;
2974 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2975 list_del(&prop_enum->head);
2979 if (property->num_values)
2980 kfree(property->values);
2981 drm_mode_object_put(dev, &property->base);
2982 list_del(&property->head);
2985 EXPORT_SYMBOL(drm_property_destroy);
2987 void drm_object_attach_property(struct drm_mode_object *obj,
2988 struct drm_property *property,
2991 int count = obj->properties->count;
2993 if (count == DRM_OBJECT_MAX_PROPERTY) {
2994 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2995 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2996 "you see this message on the same object type.\n",
3001 obj->properties->ids[count] = property->base.id;
3002 obj->properties->values[count] = init_val;
3003 obj->properties->count++;
3005 EXPORT_SYMBOL(drm_object_attach_property);
3007 int drm_object_property_set_value(struct drm_mode_object *obj,
3008 struct drm_property *property, uint64_t val)
3012 for (i = 0; i < obj->properties->count; i++) {
3013 if (obj->properties->ids[i] == property->base.id) {
3014 obj->properties->values[i] = val;
3021 EXPORT_SYMBOL(drm_object_property_set_value);
3023 int drm_object_property_get_value(struct drm_mode_object *obj,
3024 struct drm_property *property, uint64_t *val)
3028 for (i = 0; i < obj->properties->count; i++) {
3029 if (obj->properties->ids[i] == property->base.id) {
3030 *val = obj->properties->values[i];
3037 EXPORT_SYMBOL(drm_object_property_get_value);
3039 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3040 void *data, struct drm_file *file_priv)
3042 struct drm_mode_object *obj;
3043 struct drm_mode_get_property *out_resp = data;
3044 struct drm_property *property;
3047 int value_count = 0;
3050 struct drm_property_enum *prop_enum;
3051 struct drm_mode_property_enum __user *enum_ptr;
3052 struct drm_property_blob *prop_blob;
3053 uint32_t __user *blob_id_ptr;
3054 uint64_t __user *values_ptr;
3055 uint32_t __user *blob_length_ptr;
3057 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3060 drm_modeset_lock_all(dev);
3061 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3066 property = obj_to_property(obj);
3068 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3069 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3071 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3072 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3076 value_count = property->num_values;
3078 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3079 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3080 out_resp->flags = property->flags;
3082 if ((out_resp->count_values >= value_count) && value_count) {
3083 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3084 for (i = 0; i < value_count; i++) {
3085 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3091 out_resp->count_values = value_count;
3093 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3094 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3096 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3097 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3099 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3104 if (copy_to_user(&enum_ptr[copied].name,
3105 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3112 out_resp->count_enum_blobs = enum_count;
3115 if (property->flags & DRM_MODE_PROP_BLOB) {
3116 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3118 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3119 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3121 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3122 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3127 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3135 out_resp->count_enum_blobs = blob_count;
3138 drm_modeset_unlock_all(dev);
3142 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3145 struct drm_property_blob *blob;
3148 if (!length || !data)
3151 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3155 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3161 blob->length = length;
3163 memcpy(blob->data, data, length);
3165 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3169 static void drm_property_destroy_blob(struct drm_device *dev,
3170 struct drm_property_blob *blob)
3172 drm_mode_object_put(dev, &blob->base);
3173 list_del(&blob->head);
3177 int drm_mode_getblob_ioctl(struct drm_device *dev,
3178 void *data, struct drm_file *file_priv)
3180 struct drm_mode_object *obj;
3181 struct drm_mode_get_blob *out_resp = data;
3182 struct drm_property_blob *blob;
3184 void __user *blob_ptr;
3186 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3189 drm_modeset_lock_all(dev);
3190 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3195 blob = obj_to_blob(obj);
3197 if (out_resp->length == blob->length) {
3198 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3199 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3204 out_resp->length = blob->length;
3207 drm_modeset_unlock_all(dev);
3211 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3214 struct drm_device *dev = connector->dev;
3217 if (connector->edid_blob_ptr)
3218 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3220 /* Delete edid, when there is none. */
3222 connector->edid_blob_ptr = NULL;
3223 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3227 size = EDID_LENGTH * (1 + edid->extensions);
3228 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3230 if (!connector->edid_blob_ptr)
3233 ret = drm_object_property_set_value(&connector->base,
3234 dev->mode_config.edid_property,
3235 connector->edid_blob_ptr->base.id);
3239 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3241 static bool drm_property_change_is_valid(struct drm_property *property,
3244 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3246 if (property->flags & DRM_MODE_PROP_RANGE) {
3247 if (value < property->values[0] || value > property->values[1])
3250 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3252 uint64_t valid_mask = 0;
3253 for (i = 0; i < property->num_values; i++)
3254 valid_mask |= (1ULL << property->values[i]);
3255 return !(value & ~valid_mask);
3256 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3257 /* Only the driver knows */
3261 for (i = 0; i < property->num_values; i++)
3262 if (property->values[i] == value)
3268 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3269 void *data, struct drm_file *file_priv)
3271 struct drm_mode_connector_set_property *conn_set_prop = data;
3272 struct drm_mode_obj_set_property obj_set_prop = {
3273 .value = conn_set_prop->value,
3274 .prop_id = conn_set_prop->prop_id,
3275 .obj_id = conn_set_prop->connector_id,
3276 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3279 /* It does all the locking and checking we need */
3280 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3283 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3284 struct drm_property *property,
3288 struct drm_connector *connector = obj_to_connector(obj);
3290 /* Do DPMS ourselves */
3291 if (property == connector->dev->mode_config.dpms_property) {
3292 if (connector->funcs->dpms)
3293 (*connector->funcs->dpms)(connector, (int)value);
3295 } else if (connector->funcs->set_property)
3296 ret = connector->funcs->set_property(connector, property, value);
3298 /* store the property value if successful */
3300 drm_object_property_set_value(&connector->base, property, value);
3304 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3305 struct drm_property *property,
3309 struct drm_crtc *crtc = obj_to_crtc(obj);
3311 if (crtc->funcs->set_property)
3312 ret = crtc->funcs->set_property(crtc, property, value);
3314 drm_object_property_set_value(obj, property, value);
3319 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3320 struct drm_property *property,
3324 struct drm_plane *plane = obj_to_plane(obj);
3326 if (plane->funcs->set_property)
3327 ret = plane->funcs->set_property(plane, property, value);
3329 drm_object_property_set_value(obj, property, value);
3334 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3335 struct drm_file *file_priv)
3337 struct drm_mode_obj_get_properties *arg = data;
3338 struct drm_mode_object *obj;
3342 int props_count = 0;
3343 uint32_t __user *props_ptr;
3344 uint64_t __user *prop_values_ptr;
3346 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3349 drm_modeset_lock_all(dev);
3351 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3356 if (!obj->properties) {
3361 props_count = obj->properties->count;
3363 /* This ioctl is called twice, once to determine how much space is
3364 * needed, and the 2nd time to fill it. */
3365 if ((arg->count_props >= props_count) && props_count) {
3367 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3368 prop_values_ptr = (uint64_t __user *)(unsigned long)
3369 (arg->prop_values_ptr);
3370 for (i = 0; i < props_count; i++) {
3371 if (put_user(obj->properties->ids[i],
3372 props_ptr + copied)) {
3376 if (put_user(obj->properties->values[i],
3377 prop_values_ptr + copied)) {
3384 arg->count_props = props_count;
3386 drm_modeset_unlock_all(dev);
3390 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3391 struct drm_file *file_priv)
3393 struct drm_mode_obj_set_property *arg = data;
3394 struct drm_mode_object *arg_obj;
3395 struct drm_mode_object *prop_obj;
3396 struct drm_property *property;
3400 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3403 drm_modeset_lock_all(dev);
3405 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3408 if (!arg_obj->properties)
3411 for (i = 0; i < arg_obj->properties->count; i++)
3412 if (arg_obj->properties->ids[i] == arg->prop_id)
3415 if (i == arg_obj->properties->count)
3418 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3419 DRM_MODE_OBJECT_PROPERTY);
3422 property = obj_to_property(prop_obj);
3424 if (!drm_property_change_is_valid(property, arg->value))
3427 switch (arg_obj->type) {
3428 case DRM_MODE_OBJECT_CONNECTOR:
3429 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3432 case DRM_MODE_OBJECT_CRTC:
3433 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3435 case DRM_MODE_OBJECT_PLANE:
3436 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3441 drm_modeset_unlock_all(dev);
3445 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3446 struct drm_encoder *encoder)
3450 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3451 if (connector->encoder_ids[i] == 0) {
3452 connector->encoder_ids[i] = encoder->base.id;
3458 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3460 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3461 struct drm_encoder *encoder)
3464 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3465 if (connector->encoder_ids[i] == encoder->base.id) {
3466 connector->encoder_ids[i] = 0;
3467 if (connector->encoder == encoder)
3468 connector->encoder = NULL;
3473 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3475 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3478 crtc->gamma_size = gamma_size;
3480 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3481 if (!crtc->gamma_store) {
3482 crtc->gamma_size = 0;
3488 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3490 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3491 void *data, struct drm_file *file_priv)
3493 struct drm_mode_crtc_lut *crtc_lut = data;
3494 struct drm_mode_object *obj;
3495 struct drm_crtc *crtc;
3496 void *r_base, *g_base, *b_base;
3500 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3503 drm_modeset_lock_all(dev);
3504 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3509 crtc = obj_to_crtc(obj);
3511 if (crtc->funcs->gamma_set == NULL) {
3516 /* memcpy into gamma store */
3517 if (crtc_lut->gamma_size != crtc->gamma_size) {
3522 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3523 r_base = crtc->gamma_store;
3524 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3529 g_base = r_base + size;
3530 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3535 b_base = g_base + size;
3536 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3541 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3544 drm_modeset_unlock_all(dev);
3549 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3550 void *data, struct drm_file *file_priv)
3552 struct drm_mode_crtc_lut *crtc_lut = data;
3553 struct drm_mode_object *obj;
3554 struct drm_crtc *crtc;
3555 void *r_base, *g_base, *b_base;
3559 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3562 drm_modeset_lock_all(dev);
3563 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3568 crtc = obj_to_crtc(obj);
3570 /* memcpy into gamma store */
3571 if (crtc_lut->gamma_size != crtc->gamma_size) {
3576 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3577 r_base = crtc->gamma_store;
3578 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3583 g_base = r_base + size;
3584 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3589 b_base = g_base + size;
3590 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3595 drm_modeset_unlock_all(dev);
3599 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3600 void *data, struct drm_file *file_priv)
3602 struct drm_mode_crtc_page_flip *page_flip = data;
3603 struct drm_mode_object *obj;
3604 struct drm_crtc *crtc;
3605 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3606 struct drm_pending_vblank_event *e = NULL;
3607 unsigned long flags;
3610 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3611 page_flip->reserved != 0)
3614 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3617 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3620 crtc = obj_to_crtc(obj);
3622 mutex_lock(&crtc->mutex);
3623 if (crtc->fb == NULL) {
3624 /* The framebuffer is currently unbound, presumably
3625 * due to a hotplug event, that userspace has not
3632 if (crtc->funcs->page_flip == NULL)
3635 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3639 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3643 if (crtc->fb->pixel_format != fb->pixel_format) {
3644 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3649 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3651 spin_lock_irqsave(&dev->event_lock, flags);
3652 if (file_priv->event_space < sizeof e->event) {
3653 spin_unlock_irqrestore(&dev->event_lock, flags);
3656 file_priv->event_space -= sizeof e->event;
3657 spin_unlock_irqrestore(&dev->event_lock, flags);
3659 e = kzalloc(sizeof *e, GFP_KERNEL);
3661 spin_lock_irqsave(&dev->event_lock, flags);
3662 file_priv->event_space += sizeof e->event;
3663 spin_unlock_irqrestore(&dev->event_lock, flags);
3667 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3668 e->event.base.length = sizeof e->event;
3669 e->event.user_data = page_flip->user_data;
3670 e->base.event = &e->event.base;
3671 e->base.file_priv = file_priv;
3673 (void (*) (struct drm_pending_event *)) kfree;
3677 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
3679 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3680 spin_lock_irqsave(&dev->event_lock, flags);
3681 file_priv->event_space += sizeof e->event;
3682 spin_unlock_irqrestore(&dev->event_lock, flags);
3685 /* Keep the old fb, don't unref it. */
3689 * Warn if the driver hasn't properly updated the crtc->fb
3690 * field to reflect that the new framebuffer is now used.
3691 * Failing to do so will screw with the reference counting
3694 WARN_ON(crtc->fb != fb);
3695 /* Unref only the old framebuffer. */
3701 drm_framebuffer_unreference(fb);
3703 drm_framebuffer_unreference(old_fb);
3704 mutex_unlock(&crtc->mutex);
3709 void drm_mode_config_reset(struct drm_device *dev)
3711 struct drm_crtc *crtc;
3712 struct drm_encoder *encoder;
3713 struct drm_connector *connector;
3715 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3716 if (crtc->funcs->reset)
3717 crtc->funcs->reset(crtc);
3719 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3720 if (encoder->funcs->reset)
3721 encoder->funcs->reset(encoder);
3723 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3724 connector->status = connector_status_unknown;
3726 if (connector->funcs->reset)
3727 connector->funcs->reset(connector);
3730 EXPORT_SYMBOL(drm_mode_config_reset);
3732 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3733 void *data, struct drm_file *file_priv)
3735 struct drm_mode_create_dumb *args = data;
3737 if (!dev->driver->dumb_create)
3739 return dev->driver->dumb_create(file_priv, dev, args);
3742 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3743 void *data, struct drm_file *file_priv)
3745 struct drm_mode_map_dumb *args = data;
3747 /* call driver ioctl to get mmap offset */
3748 if (!dev->driver->dumb_map_offset)
3751 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3754 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3755 void *data, struct drm_file *file_priv)
3757 struct drm_mode_destroy_dumb *args = data;
3759 if (!dev->driver->dumb_destroy)
3762 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3766 * Just need to support RGB formats here for compat with code that doesn't
3767 * use pixel formats directly yet.
3769 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3774 case DRM_FORMAT_RGB332:
3775 case DRM_FORMAT_BGR233:
3779 case DRM_FORMAT_XRGB1555:
3780 case DRM_FORMAT_XBGR1555:
3781 case DRM_FORMAT_RGBX5551:
3782 case DRM_FORMAT_BGRX5551:
3783 case DRM_FORMAT_ARGB1555:
3784 case DRM_FORMAT_ABGR1555:
3785 case DRM_FORMAT_RGBA5551:
3786 case DRM_FORMAT_BGRA5551:
3790 case DRM_FORMAT_RGB565:
3791 case DRM_FORMAT_BGR565:
3795 case DRM_FORMAT_RGB888:
3796 case DRM_FORMAT_BGR888:
3800 case DRM_FORMAT_XRGB8888:
3801 case DRM_FORMAT_XBGR8888:
3802 case DRM_FORMAT_RGBX8888:
3803 case DRM_FORMAT_BGRX8888:
3807 case DRM_FORMAT_XRGB2101010:
3808 case DRM_FORMAT_XBGR2101010:
3809 case DRM_FORMAT_RGBX1010102:
3810 case DRM_FORMAT_BGRX1010102:
3811 case DRM_FORMAT_ARGB2101010:
3812 case DRM_FORMAT_ABGR2101010:
3813 case DRM_FORMAT_RGBA1010102:
3814 case DRM_FORMAT_BGRA1010102:
3818 case DRM_FORMAT_ARGB8888:
3819 case DRM_FORMAT_ABGR8888:
3820 case DRM_FORMAT_RGBA8888:
3821 case DRM_FORMAT_BGRA8888:
3826 DRM_DEBUG_KMS("unsupported pixel format\n");
3832 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3835 * drm_format_num_planes - get the number of planes for format
3836 * @format: pixel format (DRM_FORMAT_*)
3839 * The number of planes used by the specified pixel format.
3841 int drm_format_num_planes(uint32_t format)
3844 case DRM_FORMAT_YUV410:
3845 case DRM_FORMAT_YVU410:
3846 case DRM_FORMAT_YUV411:
3847 case DRM_FORMAT_YVU411:
3848 case DRM_FORMAT_YUV420:
3849 case DRM_FORMAT_YVU420:
3850 case DRM_FORMAT_YUV422:
3851 case DRM_FORMAT_YVU422:
3852 case DRM_FORMAT_YUV444:
3853 case DRM_FORMAT_YVU444:
3855 case DRM_FORMAT_NV12:
3856 case DRM_FORMAT_NV21:
3857 case DRM_FORMAT_NV16:
3858 case DRM_FORMAT_NV61:
3859 case DRM_FORMAT_NV24:
3860 case DRM_FORMAT_NV42:
3866 EXPORT_SYMBOL(drm_format_num_planes);
3869 * drm_format_plane_cpp - determine the bytes per pixel value
3870 * @format: pixel format (DRM_FORMAT_*)
3871 * @plane: plane index
3874 * The bytes per pixel value for the specified plane.
3876 int drm_format_plane_cpp(uint32_t format, int plane)
3881 if (plane >= drm_format_num_planes(format))
3885 case DRM_FORMAT_YUYV:
3886 case DRM_FORMAT_YVYU:
3887 case DRM_FORMAT_UYVY:
3888 case DRM_FORMAT_VYUY:
3890 case DRM_FORMAT_NV12:
3891 case DRM_FORMAT_NV21:
3892 case DRM_FORMAT_NV16:
3893 case DRM_FORMAT_NV61:
3894 case DRM_FORMAT_NV24:
3895 case DRM_FORMAT_NV42:
3896 return plane ? 2 : 1;
3897 case DRM_FORMAT_YUV410:
3898 case DRM_FORMAT_YVU410:
3899 case DRM_FORMAT_YUV411:
3900 case DRM_FORMAT_YVU411:
3901 case DRM_FORMAT_YUV420:
3902 case DRM_FORMAT_YVU420:
3903 case DRM_FORMAT_YUV422:
3904 case DRM_FORMAT_YVU422:
3905 case DRM_FORMAT_YUV444:
3906 case DRM_FORMAT_YVU444:
3909 drm_fb_get_bpp_depth(format, &depth, &bpp);
3913 EXPORT_SYMBOL(drm_format_plane_cpp);
3916 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3917 * @format: pixel format (DRM_FORMAT_*)
3920 * The horizontal chroma subsampling factor for the
3921 * specified pixel format.
3923 int drm_format_horz_chroma_subsampling(uint32_t format)
3926 case DRM_FORMAT_YUV411:
3927 case DRM_FORMAT_YVU411:
3928 case DRM_FORMAT_YUV410:
3929 case DRM_FORMAT_YVU410:
3931 case DRM_FORMAT_YUYV:
3932 case DRM_FORMAT_YVYU:
3933 case DRM_FORMAT_UYVY:
3934 case DRM_FORMAT_VYUY:
3935 case DRM_FORMAT_NV12:
3936 case DRM_FORMAT_NV21:
3937 case DRM_FORMAT_NV16:
3938 case DRM_FORMAT_NV61:
3939 case DRM_FORMAT_YUV422:
3940 case DRM_FORMAT_YVU422:
3941 case DRM_FORMAT_YUV420:
3942 case DRM_FORMAT_YVU420:
3948 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3951 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3952 * @format: pixel format (DRM_FORMAT_*)
3955 * The vertical chroma subsampling factor for the
3956 * specified pixel format.
3958 int drm_format_vert_chroma_subsampling(uint32_t format)
3961 case DRM_FORMAT_YUV410:
3962 case DRM_FORMAT_YVU410:
3964 case DRM_FORMAT_YUV420:
3965 case DRM_FORMAT_YVU420:
3966 case DRM_FORMAT_NV12:
3967 case DRM_FORMAT_NV21:
3973 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3976 * drm_mode_config_init - initialize DRM mode_configuration structure
3979 * Initialize @dev's mode_config structure, used for tracking the graphics
3980 * configuration of @dev.
3982 * Since this initializes the modeset locks, no locking is possible. Which is no
3983 * problem, since this should happen single threaded at init time. It is the
3984 * driver's problem to ensure this guarantee.
3987 void drm_mode_config_init(struct drm_device *dev)
3989 mutex_init(&dev->mode_config.mutex);
3990 mutex_init(&dev->mode_config.idr_mutex);
3991 mutex_init(&dev->mode_config.fb_lock);
3992 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3993 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3994 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3995 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
3996 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3997 INIT_LIST_HEAD(&dev->mode_config.property_list);
3998 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3999 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4000 idr_init(&dev->mode_config.crtc_idr);
4002 drm_modeset_lock_all(dev);
4003 drm_mode_create_standard_connector_properties(dev);
4004 drm_modeset_unlock_all(dev);
4006 /* Just to be sure */
4007 dev->mode_config.num_fb = 0;
4008 dev->mode_config.num_connector = 0;
4009 dev->mode_config.num_crtc = 0;
4010 dev->mode_config.num_encoder = 0;
4012 EXPORT_SYMBOL(drm_mode_config_init);
4015 * drm_mode_config_cleanup - free up DRM mode_config info
4018 * Free up all the connectors and CRTCs associated with this DRM device, then
4019 * free up the framebuffers and associated buffer objects.
4021 * Note that since this /should/ happen single-threaded at driver/device
4022 * teardown time, no locking is required. It's the driver's job to ensure that
4023 * this guarantee actually holds true.
4025 * FIXME: cleanup any dangling user buffer objects too
4027 void drm_mode_config_cleanup(struct drm_device *dev)
4029 struct drm_connector *connector, *ot;
4030 struct drm_crtc *crtc, *ct;
4031 struct drm_encoder *encoder, *enct;
4032 struct drm_bridge *bridge, *brt;
4033 struct drm_framebuffer *fb, *fbt;
4034 struct drm_property *property, *pt;
4035 struct drm_property_blob *blob, *bt;
4036 struct drm_plane *plane, *plt;
4038 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4040 encoder->funcs->destroy(encoder);
4043 list_for_each_entry_safe(bridge, brt,
4044 &dev->mode_config.bridge_list, head) {
4045 bridge->funcs->destroy(bridge);
4048 list_for_each_entry_safe(connector, ot,
4049 &dev->mode_config.connector_list, head) {
4050 connector->funcs->destroy(connector);
4053 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4055 drm_property_destroy(dev, property);
4058 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4060 drm_property_destroy_blob(dev, blob);
4064 * Single-threaded teardown context, so it's not required to grab the
4065 * fb_lock to protect against concurrent fb_list access. Contrary, it
4066 * would actually deadlock with the drm_framebuffer_cleanup function.
4068 * Also, if there are any framebuffers left, that's a driver leak now,
4069 * so politely WARN about this.
4071 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4072 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4073 drm_framebuffer_remove(fb);
4076 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4078 plane->funcs->destroy(plane);
4081 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4082 crtc->funcs->destroy(crtc);
4085 idr_destroy(&dev->mode_config.crtc_idr);
4087 EXPORT_SYMBOL(drm_mode_config_cleanup);