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 <uapi_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 lockmgr(&crtc->mutex, LK_EXCLUSIVE);
57 EXPORT_SYMBOL(drm_modeset_lock_all);
60 * drm_modeset_unlock_all - drop all modeset locks
63 void drm_modeset_unlock_all(struct drm_device *dev)
65 struct drm_crtc *crtc;
67 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
68 mutex_unlock(&crtc->mutex);
70 mutex_unlock(&dev->mode_config.mutex);
72 EXPORT_SYMBOL(drm_modeset_unlock_all);
75 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
78 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
80 struct drm_crtc *crtc;
83 /* Locking is currently fubar in the panic handler. */
88 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
89 WARN_ON(!mutex_is_locked(&crtc->mutex));
91 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
93 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
95 /* Avoid boilerplate. I'm tired of typing. */
96 #define DRM_ENUM_NAME_FN(fnname, list) \
97 const char *fnname(int val) \
100 for (i = 0; i < ARRAY_SIZE(list); i++) { \
101 if (list[i].type == val) \
102 return list[i].name; \
104 return "(unknown)"; \
110 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
111 { { DRM_MODE_DPMS_ON, "On" },
112 { DRM_MODE_DPMS_STANDBY, "Standby" },
113 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
114 { DRM_MODE_DPMS_OFF, "Off" }
117 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
120 * Optional properties
122 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
124 { DRM_MODE_SCALE_NONE, "None" },
125 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
126 { DRM_MODE_SCALE_CENTER, "Center" },
127 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
130 static const struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
132 { DRM_MODE_DITHERING_OFF, "Off" },
133 { DRM_MODE_DITHERING_ON, "On" },
134 { DRM_MODE_DITHERING_AUTO, "Automatic" },
138 * Non-global properties, but "required" for certain connectors.
140 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
142 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
143 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
144 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
147 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
149 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
151 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
152 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
153 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
156 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
157 drm_dvi_i_subconnector_enum_list)
159 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
161 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
162 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
163 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
164 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
165 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
168 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
170 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
172 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
173 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
174 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
175 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
176 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
179 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
180 drm_tv_subconnector_enum_list)
182 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
183 { DRM_MODE_DIRTY_OFF, "Off" },
184 { DRM_MODE_DIRTY_ON, "On" },
185 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
188 struct drm_conn_prop_enum_list {
195 * Connector and encoder types.
197 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
198 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
199 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
200 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
201 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
202 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
203 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
204 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
205 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
206 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
207 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
208 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
209 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
210 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
211 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
212 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
213 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
216 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
217 { { DRM_MODE_ENCODER_NONE, "None" },
218 { DRM_MODE_ENCODER_DAC, "DAC" },
219 { DRM_MODE_ENCODER_TMDS, "TMDS" },
220 { DRM_MODE_ENCODER_LVDS, "LVDS" },
221 { DRM_MODE_ENCODER_TVDAC, "TV" },
222 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
225 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
229 ksnprintf(buf, 32, "%s-%d",
230 drm_encoder_enum_list[encoder->encoder_type].name,
234 EXPORT_SYMBOL(drm_get_encoder_name);
236 const char *drm_get_connector_name(const struct drm_connector *connector)
240 ksnprintf(buf, 32, "%s-%d",
241 drm_connector_enum_list[connector->connector_type].name,
242 connector->connector_type_id);
245 EXPORT_SYMBOL(drm_get_connector_name);
247 const char *drm_get_connector_status_name(enum drm_connector_status status)
249 if (status == connector_status_connected)
251 else if (status == connector_status_disconnected)
252 return "disconnected";
256 EXPORT_SYMBOL(drm_get_connector_status_name);
258 static char printable_char(int c)
260 return isascii(c) && isprint(c) ? c : '?';
263 const char *drm_get_format_name(uint32_t format)
267 ksnprintf(buf, sizeof(buf),
268 "%c%c%c%c %s-endian (0x%08x)",
269 printable_char(format & 0xff),
270 printable_char((format >> 8) & 0xff),
271 printable_char((format >> 16) & 0xff),
272 printable_char((format >> 24) & 0x7f),
273 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
278 EXPORT_SYMBOL(drm_get_format_name);
281 * drm_mode_object_get - allocate a new modeset identifier
283 * @obj: object pointer, used to generate unique ID
284 * @obj_type: object type
286 * Create a unique identifier based on @ptr in @dev's identifier space. Used
287 * for tracking modes, CRTCs and connectors.
290 * New unique (relative to other objects in @dev) integer identifier for the
293 static int drm_mode_object_get(struct drm_device *dev,
294 struct drm_mode_object *obj, uint32_t obj_type)
298 mutex_lock(&dev->mode_config.idr_mutex);
299 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
302 * Set up the object linking under the protection of the idr
303 * lock so that other users can't see inconsistent state.
306 obj->type = obj_type;
308 mutex_unlock(&dev->mode_config.idr_mutex);
310 return ret < 0 ? ret : 0;
314 * drm_mode_object_put - free a modeset identifer
316 * @object: object to free
318 * Free @id from @dev's unique identifier pool.
320 static void drm_mode_object_put(struct drm_device *dev,
321 struct drm_mode_object *object)
323 mutex_lock(&dev->mode_config.idr_mutex);
324 idr_remove(&dev->mode_config.crtc_idr, object->id);
325 mutex_unlock(&dev->mode_config.idr_mutex);
329 * drm_mode_object_find - look up a drm object with static lifetime
331 * @id: id of the mode object
332 * @type: type of the mode object
334 * Note that framebuffers cannot be looked up with this functions - since those
335 * are reference counted, they need special treatment.
337 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
338 uint32_t id, uint32_t type)
340 struct drm_mode_object *obj = NULL;
342 /* Framebuffers are reference counted and need their own lookup
344 WARN_ON(type == DRM_MODE_OBJECT_FB);
346 mutex_lock(&dev->mode_config.idr_mutex);
347 obj = idr_find(&dev->mode_config.crtc_idr, id);
348 if (!obj || (obj->type != type) || (obj->id != id))
350 mutex_unlock(&dev->mode_config.idr_mutex);
354 EXPORT_SYMBOL(drm_mode_object_find);
357 * drm_framebuffer_init - initialize a framebuffer
359 * @fb: framebuffer to be initialized
360 * @funcs: ... with these functions
362 * Allocates an ID for the framebuffer's parent mode object, sets its mode
363 * functions & device file and adds it to the master fd list.
366 * This functions publishes the fb and makes it available for concurrent access
367 * by other users. Which means by this point the fb _must_ be fully set up -
368 * since all the fb attributes are invariant over its lifetime, no further
369 * locking but only correct reference counting is required.
372 * Zero on success, error code on failure.
374 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
375 const struct drm_framebuffer_funcs *funcs)
379 mutex_lock(&dev->mode_config.fb_lock);
380 kref_init(&fb->refcount);
381 INIT_LIST_HEAD(&fb->filp_head);
385 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
389 /* Grab the idr reference. */
390 drm_framebuffer_reference(fb);
392 dev->mode_config.num_fb++;
393 list_add(&fb->head, &dev->mode_config.fb_list);
395 mutex_unlock(&dev->mode_config.fb_lock);
399 EXPORT_SYMBOL(drm_framebuffer_init);
401 static void drm_framebuffer_free(struct kref *kref)
403 struct drm_framebuffer *fb =
404 container_of(kref, struct drm_framebuffer, refcount);
405 fb->funcs->destroy(fb);
408 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
411 struct drm_mode_object *obj = NULL;
412 struct drm_framebuffer *fb;
414 mutex_lock(&dev->mode_config.idr_mutex);
415 obj = idr_find(&dev->mode_config.crtc_idr, id);
416 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
420 mutex_unlock(&dev->mode_config.idr_mutex);
426 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
428 * @id: id of the fb object
430 * If successful, this grabs an additional reference to the framebuffer -
431 * callers need to make sure to eventually unreference the returned framebuffer
434 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
437 struct drm_framebuffer *fb;
439 mutex_lock(&dev->mode_config.fb_lock);
440 fb = __drm_framebuffer_lookup(dev, id);
442 drm_framebuffer_reference(fb);
443 mutex_unlock(&dev->mode_config.fb_lock);
447 EXPORT_SYMBOL(drm_framebuffer_lookup);
450 * drm_framebuffer_unreference - unref a framebuffer
451 * @fb: framebuffer to unref
453 * This functions decrements the fb's refcount and frees it if it drops to zero.
455 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
457 DRM_DEBUG("FB ID: %d\n", fb->base.id);
458 kref_put(&fb->refcount, drm_framebuffer_free);
460 EXPORT_SYMBOL(drm_framebuffer_unreference);
463 * drm_framebuffer_reference - incr the fb refcnt
466 void drm_framebuffer_reference(struct drm_framebuffer *fb)
468 DRM_DEBUG("FB ID: %d\n", fb->base.id);
469 kref_get(&fb->refcount);
471 EXPORT_SYMBOL(drm_framebuffer_reference);
473 static void drm_framebuffer_free_bug(struct kref *kref)
478 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
480 DRM_DEBUG("FB ID: %d\n", fb->base.id);
481 kref_put(&fb->refcount, drm_framebuffer_free_bug);
484 /* dev->mode_config.fb_lock must be held! */
485 static void __drm_framebuffer_unregister(struct drm_device *dev,
486 struct drm_framebuffer *fb)
488 mutex_lock(&dev->mode_config.idr_mutex);
489 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
490 mutex_unlock(&dev->mode_config.idr_mutex);
494 __drm_framebuffer_unreference(fb);
498 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
499 * @fb: fb to unregister
501 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
502 * those used for fbdev. Note that the caller must hold a reference of it's own,
503 * i.e. the object may not be destroyed through this call (since it'll lead to a
504 * locking inversion).
506 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
508 struct drm_device *dev = fb->dev;
510 mutex_lock(&dev->mode_config.fb_lock);
511 /* Mark fb as reaped and drop idr ref. */
512 __drm_framebuffer_unregister(dev, fb);
513 mutex_unlock(&dev->mode_config.fb_lock);
515 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
518 * drm_framebuffer_cleanup - remove a framebuffer object
519 * @fb: framebuffer to remove
521 * Cleanup references to a user-created framebuffer. This function is intended
522 * to be used from the drivers ->destroy callback.
524 * Note that this function does not remove the fb from active usuage - if it is
525 * still used anywhere, hilarity can ensue since userspace could call getfb on
526 * the id and get back -EINVAL. Obviously no concern at driver unload time.
528 * Also, the framebuffer will not be removed from the lookup idr - for
529 * user-created framebuffers this will happen in in the rmfb ioctl. For
530 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
531 * drm_framebuffer_unregister_private.
533 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
535 struct drm_device *dev = fb->dev;
537 mutex_lock(&dev->mode_config.fb_lock);
539 dev->mode_config.num_fb--;
540 mutex_unlock(&dev->mode_config.fb_lock);
542 EXPORT_SYMBOL(drm_framebuffer_cleanup);
545 * drm_framebuffer_remove - remove and unreference a framebuffer object
546 * @fb: framebuffer to remove
548 * Scans all the CRTCs and planes in @dev's mode_config. If they're
549 * using @fb, removes it, setting it to NULL. Then drops the reference to the
550 * passed-in framebuffer. Might take the modeset locks.
552 * Note that this function optimizes the cleanup away if the caller holds the
553 * last reference to the framebuffer. It is also guaranteed to not take the
554 * modeset locks in this case.
556 void drm_framebuffer_remove(struct drm_framebuffer *fb)
558 struct drm_device *dev = fb->dev;
559 struct drm_crtc *crtc;
560 struct drm_plane *plane;
561 struct drm_mode_set set;
564 WARN_ON(!list_empty(&fb->filp_head));
567 * drm ABI mandates that we remove any deleted framebuffers from active
568 * useage. But since most sane clients only remove framebuffers they no
569 * longer need, try to optimize this away.
571 * Since we're holding a reference ourselves, observing a refcount of 1
572 * means that we're the last holder and can skip it. Also, the refcount
573 * can never increase from 1 again, so we don't need any barriers or
576 * Note that userspace could try to race with use and instate a new
577 * usage _after_ we've cleared all current ones. End result will be an
578 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
581 if (atomic_read(&fb->refcount.refcount) > 1) {
582 drm_modeset_lock_all(dev);
583 /* remove from any CRTC */
584 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
585 if (crtc->fb == fb) {
586 /* should turn off the crtc */
587 memset(&set, 0, sizeof(struct drm_mode_set));
590 ret = drm_mode_set_config_internal(&set);
592 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
596 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
598 drm_plane_force_disable(plane);
600 drm_modeset_unlock_all(dev);
603 drm_framebuffer_unreference(fb);
605 EXPORT_SYMBOL(drm_framebuffer_remove);
608 * drm_crtc_init - Initialise a new CRTC object
610 * @crtc: CRTC object to init
611 * @funcs: callbacks for the new CRTC
613 * Inits a new object created as base part of a driver crtc object.
616 * Zero on success, error code on failure.
618 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
619 const struct drm_crtc_funcs *funcs)
625 crtc->invert_dimensions = false;
627 drm_modeset_lock_all(dev);
628 lockinit(&crtc->mutex, "drmcm", 0, LK_CANRECURSE);
629 lockmgr(&crtc->mutex, LK_EXCLUSIVE);
631 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
635 crtc->base.properties = &crtc->properties;
637 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
638 dev->mode_config.num_crtc++;
641 drm_modeset_unlock_all(dev);
645 EXPORT_SYMBOL(drm_crtc_init);
648 * drm_crtc_cleanup - Clean up the core crtc usage
649 * @crtc: CRTC to cleanup
651 * This function cleans up @crtc and removes it from the DRM mode setting
652 * core. Note that the function does *not* free the crtc structure itself,
653 * this is the responsibility of the caller.
655 void drm_crtc_cleanup(struct drm_crtc *crtc)
657 struct drm_device *dev = crtc->dev;
659 kfree(crtc->gamma_store);
660 crtc->gamma_store = NULL;
662 drm_mode_object_put(dev, &crtc->base);
663 list_del(&crtc->head);
664 dev->mode_config.num_crtc--;
666 EXPORT_SYMBOL(drm_crtc_cleanup);
669 * drm_mode_probed_add - add a mode to a connector's probed mode list
670 * @connector: connector the new mode
673 * Add @mode to @connector's mode list for later use.
675 void drm_mode_probed_add(struct drm_connector *connector,
676 struct drm_display_mode *mode)
678 list_add_tail(&mode->head, &connector->probed_modes);
680 EXPORT_SYMBOL(drm_mode_probed_add);
683 * drm_mode_remove - remove and free a mode
684 * @connector: connector list to modify
685 * @mode: mode to remove
687 * Remove @mode from @connector's mode list, then free it.
689 void drm_mode_remove(struct drm_connector *connector,
690 struct drm_display_mode *mode)
692 list_del(&mode->head);
693 drm_mode_destroy(connector->dev, mode);
695 EXPORT_SYMBOL(drm_mode_remove);
698 * drm_connector_init - Init a preallocated connector
700 * @connector: the connector to init
701 * @funcs: callbacks for this connector
702 * @connector_type: user visible type of the connector
704 * Initialises a preallocated connector. Connectors should be
705 * subclassed as part of driver connector objects.
708 * Zero on success, error code on failure.
710 int drm_connector_init(struct drm_device *dev,
711 struct drm_connector *connector,
712 const struct drm_connector_funcs *funcs,
717 drm_modeset_lock_all(dev);
719 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
723 connector->base.properties = &connector->properties;
724 connector->dev = dev;
725 connector->funcs = funcs;
726 connector->connector_type = connector_type;
727 connector->connector_type_id =
728 ++drm_connector_enum_list[connector_type].count; /* TODO */
729 INIT_LIST_HEAD(&connector->probed_modes);
730 INIT_LIST_HEAD(&connector->modes);
731 connector->edid_blob_ptr = NULL;
732 connector->status = connector_status_unknown;
734 list_add_tail(&connector->head, &dev->mode_config.connector_list);
735 dev->mode_config.num_connector++;
737 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
738 drm_object_attach_property(&connector->base,
739 dev->mode_config.edid_property,
742 drm_object_attach_property(&connector->base,
743 dev->mode_config.dpms_property, 0);
746 drm_modeset_unlock_all(dev);
750 EXPORT_SYMBOL(drm_connector_init);
753 * drm_connector_cleanup - cleans up an initialised connector
754 * @connector: connector to cleanup
756 * Cleans up the connector but doesn't free the object.
758 void drm_connector_cleanup(struct drm_connector *connector)
760 struct drm_device *dev = connector->dev;
761 struct drm_display_mode *mode, *t;
763 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
764 drm_mode_remove(connector, mode);
766 list_for_each_entry_safe(mode, t, &connector->modes, head)
767 drm_mode_remove(connector, mode);
769 drm_mode_object_put(dev, &connector->base);
770 list_del(&connector->head);
771 dev->mode_config.num_connector--;
773 EXPORT_SYMBOL(drm_connector_cleanup);
775 void drm_connector_unplug_all(struct drm_device *dev)
778 struct drm_connector *connector;
780 /* taking the mode config mutex ends up in a clash with sysfs */
781 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
782 drm_sysfs_connector_remove(connector);
786 EXPORT_SYMBOL(drm_connector_unplug_all);
788 int drm_encoder_init(struct drm_device *dev,
789 struct drm_encoder *encoder,
790 const struct drm_encoder_funcs *funcs,
795 drm_modeset_lock_all(dev);
797 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
802 encoder->encoder_type = encoder_type;
803 encoder->funcs = funcs;
805 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
806 dev->mode_config.num_encoder++;
809 drm_modeset_unlock_all(dev);
813 EXPORT_SYMBOL(drm_encoder_init);
815 void drm_encoder_cleanup(struct drm_encoder *encoder)
817 struct drm_device *dev = encoder->dev;
818 drm_modeset_lock_all(dev);
819 drm_mode_object_put(dev, &encoder->base);
820 list_del(&encoder->head);
821 dev->mode_config.num_encoder--;
822 drm_modeset_unlock_all(dev);
824 EXPORT_SYMBOL(drm_encoder_cleanup);
827 * drm_plane_init - Initialise a new plane object
829 * @plane: plane object to init
830 * @possible_crtcs: bitmask of possible CRTCs
831 * @funcs: callbacks for the new plane
832 * @formats: array of supported formats (%DRM_FORMAT_*)
833 * @format_count: number of elements in @formats
834 * @priv: plane is private (hidden from userspace)?
836 * Inits a new object created as base part of a driver plane object.
839 * Zero on success, error code on failure.
841 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
842 unsigned long possible_crtcs,
843 const struct drm_plane_funcs *funcs,
844 const uint32_t *formats, uint32_t format_count,
849 drm_modeset_lock_all(dev);
851 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
855 plane->base.properties = &plane->properties;
857 plane->funcs = funcs;
858 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
860 if (!plane->format_types) {
861 DRM_DEBUG_KMS("out of memory when allocating plane\n");
862 drm_mode_object_put(dev, &plane->base);
867 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
868 plane->format_count = format_count;
869 plane->possible_crtcs = possible_crtcs;
871 /* private planes are not exposed to userspace, but depending on
872 * display hardware, might be convenient to allow sharing programming
873 * for the scanout engine with the crtc implementation.
876 list_add_tail(&plane->head, &dev->mode_config.plane_list);
877 dev->mode_config.num_plane++;
879 INIT_LIST_HEAD(&plane->head);
883 drm_modeset_unlock_all(dev);
887 EXPORT_SYMBOL(drm_plane_init);
890 * drm_plane_cleanup - Clean up the core plane usage
891 * @plane: plane to cleanup
893 * This function cleans up @plane and removes it from the DRM mode setting
894 * core. Note that the function does *not* free the plane structure itself,
895 * this is the responsibility of the caller.
897 void drm_plane_cleanup(struct drm_plane *plane)
899 struct drm_device *dev = plane->dev;
901 drm_modeset_lock_all(dev);
902 kfree(plane->format_types);
903 drm_mode_object_put(dev, &plane->base);
904 /* if not added to a list, it must be a private plane */
905 if (!list_empty(&plane->head)) {
906 list_del(&plane->head);
907 dev->mode_config.num_plane--;
909 drm_modeset_unlock_all(dev);
911 EXPORT_SYMBOL(drm_plane_cleanup);
914 * drm_plane_force_disable - Forcibly disable a plane
915 * @plane: plane to disable
917 * Forces the plane to be disabled.
919 * Used when the plane's current framebuffer is destroyed,
920 * and when restoring fbdev mode.
922 void drm_plane_force_disable(struct drm_plane *plane)
929 ret = plane->funcs->disable_plane(plane);
931 DRM_ERROR("failed to disable plane with busy fb\n");
932 /* disconnect the plane from the fb and crtc: */
933 __drm_framebuffer_unreference(plane->fb);
937 EXPORT_SYMBOL(drm_plane_force_disable);
940 * drm_mode_create - create a new display mode
943 * Create a new drm_display_mode, give it an ID, and return it.
946 * Pointer to new mode on success, NULL on error.
948 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
950 struct drm_display_mode *nmode;
952 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
956 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
963 EXPORT_SYMBOL(drm_mode_create);
966 * drm_mode_destroy - remove a mode
968 * @mode: mode to remove
970 * Free @mode's unique identifier, then free it.
972 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
977 drm_mode_object_put(dev, &mode->base);
981 EXPORT_SYMBOL(drm_mode_destroy);
983 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
985 struct drm_property *edid;
986 struct drm_property *dpms;
989 * Standard properties (apply to all connectors)
991 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
992 DRM_MODE_PROP_IMMUTABLE,
994 dev->mode_config.edid_property = edid;
996 dpms = drm_property_create_enum(dev, 0,
997 "DPMS", drm_dpms_enum_list,
998 ARRAY_SIZE(drm_dpms_enum_list));
999 dev->mode_config.dpms_property = dpms;
1005 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1008 * Called by a driver the first time a DVI-I connector is made.
1010 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1012 struct drm_property *dvi_i_selector;
1013 struct drm_property *dvi_i_subconnector;
1015 if (dev->mode_config.dvi_i_select_subconnector_property)
1019 drm_property_create_enum(dev, 0,
1020 "select subconnector",
1021 drm_dvi_i_select_enum_list,
1022 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1023 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1025 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1027 drm_dvi_i_subconnector_enum_list,
1028 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1029 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1033 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1036 * drm_create_tv_properties - create TV specific connector properties
1038 * @num_modes: number of different TV formats (modes) supported
1039 * @modes: array of pointers to strings containing name of each format
1041 * Called by a driver's TV initialization routine, this function creates
1042 * the TV specific connector properties for a given device. Caller is
1043 * responsible for allocating a list of format names and passing them to
1046 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1049 struct drm_property *tv_selector;
1050 struct drm_property *tv_subconnector;
1053 if (dev->mode_config.tv_select_subconnector_property)
1057 * Basic connector properties
1059 tv_selector = drm_property_create_enum(dev, 0,
1060 "select subconnector",
1061 drm_tv_select_enum_list,
1062 ARRAY_SIZE(drm_tv_select_enum_list));
1063 dev->mode_config.tv_select_subconnector_property = tv_selector;
1066 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1068 drm_tv_subconnector_enum_list,
1069 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1070 dev->mode_config.tv_subconnector_property = tv_subconnector;
1073 * Other, TV specific properties: margins & TV modes.
1075 dev->mode_config.tv_left_margin_property =
1076 drm_property_create_range(dev, 0, "left margin", 0, 100);
1078 dev->mode_config.tv_right_margin_property =
1079 drm_property_create_range(dev, 0, "right margin", 0, 100);
1081 dev->mode_config.tv_top_margin_property =
1082 drm_property_create_range(dev, 0, "top margin", 0, 100);
1084 dev->mode_config.tv_bottom_margin_property =
1085 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1087 dev->mode_config.tv_mode_property =
1088 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1090 for (i = 0; i < num_modes; i++)
1091 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1094 dev->mode_config.tv_brightness_property =
1095 drm_property_create_range(dev, 0, "brightness", 0, 100);
1097 dev->mode_config.tv_contrast_property =
1098 drm_property_create_range(dev, 0, "contrast", 0, 100);
1100 dev->mode_config.tv_flicker_reduction_property =
1101 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1103 dev->mode_config.tv_overscan_property =
1104 drm_property_create_range(dev, 0, "overscan", 0, 100);
1106 dev->mode_config.tv_saturation_property =
1107 drm_property_create_range(dev, 0, "saturation", 0, 100);
1109 dev->mode_config.tv_hue_property =
1110 drm_property_create_range(dev, 0, "hue", 0, 100);
1114 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1117 * drm_mode_create_scaling_mode_property - create scaling mode property
1120 * Called by a driver the first time it's needed, must be attached to desired
1123 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1125 struct drm_property *scaling_mode;
1127 if (dev->mode_config.scaling_mode_property)
1131 drm_property_create_enum(dev, 0, "scaling mode",
1132 drm_scaling_mode_enum_list,
1133 ARRAY_SIZE(drm_scaling_mode_enum_list));
1135 dev->mode_config.scaling_mode_property = scaling_mode;
1139 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1142 * drm_mode_create_dithering_property - create dithering property
1145 * Called by a driver the first time it's needed, must be attached to desired
1148 int drm_mode_create_dithering_property(struct drm_device *dev)
1150 struct drm_property *dithering_mode;
1152 if (dev->mode_config.dithering_mode_property)
1156 drm_property_create_enum(dev, 0, "dithering",
1157 drm_dithering_mode_enum_list,
1158 ARRAY_SIZE(drm_dithering_mode_enum_list));
1159 dev->mode_config.dithering_mode_property = dithering_mode;
1163 EXPORT_SYMBOL(drm_mode_create_dithering_property);
1166 * drm_mode_create_dirty_property - create dirty property
1169 * Called by a driver the first time it's needed, must be attached to desired
1172 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1174 struct drm_property *dirty_info;
1176 if (dev->mode_config.dirty_info_property)
1180 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1182 drm_dirty_info_enum_list,
1183 ARRAY_SIZE(drm_dirty_info_enum_list));
1184 dev->mode_config.dirty_info_property = dirty_info;
1188 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1190 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1192 uint32_t total_objects = 0;
1194 total_objects += dev->mode_config.num_crtc;
1195 total_objects += dev->mode_config.num_connector;
1196 total_objects += dev->mode_config.num_encoder;
1198 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1199 if (!group->id_list)
1202 group->num_crtcs = 0;
1203 group->num_connectors = 0;
1204 group->num_encoders = 0;
1208 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1209 struct drm_mode_group *group)
1211 struct drm_crtc *crtc;
1212 struct drm_encoder *encoder;
1213 struct drm_connector *connector;
1216 if ((ret = drm_mode_group_init(dev, group)))
1219 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1220 group->id_list[group->num_crtcs++] = crtc->base.id;
1222 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1223 group->id_list[group->num_crtcs + group->num_encoders++] =
1226 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1227 group->id_list[group->num_crtcs + group->num_encoders +
1228 group->num_connectors++] = connector->base.id;
1232 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1235 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1236 * @out: drm_mode_modeinfo struct to return to the user
1237 * @in: drm_display_mode to use
1239 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1242 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1243 const struct drm_display_mode *in)
1245 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1246 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1247 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1248 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1249 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1250 "timing values too large for mode info\n");
1252 out->clock = in->clock;
1253 out->hdisplay = in->hdisplay;
1254 out->hsync_start = in->hsync_start;
1255 out->hsync_end = in->hsync_end;
1256 out->htotal = in->htotal;
1257 out->hskew = in->hskew;
1258 out->vdisplay = in->vdisplay;
1259 out->vsync_start = in->vsync_start;
1260 out->vsync_end = in->vsync_end;
1261 out->vtotal = in->vtotal;
1262 out->vscan = in->vscan;
1263 out->vrefresh = in->vrefresh;
1264 out->flags = in->flags;
1265 out->type = in->type;
1266 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1267 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1271 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1272 * @out: drm_display_mode to return to the user
1273 * @in: drm_mode_modeinfo to use
1275 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1279 * Zero on success, errno on failure.
1281 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1282 const struct drm_mode_modeinfo *in)
1284 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
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;
1308 * drm_mode_getresources - get graphics configuration
1309 * @dev: drm device for the ioctl
1310 * @data: data pointer for the ioctl
1311 * @file_priv: drm file for the ioctl call
1313 * Construct a set of configuration description structures and return
1314 * them to the user, including CRTC, connector and framebuffer configuration.
1316 * Called by the user via ioctl.
1319 * Zero on success, errno on failure.
1321 int drm_mode_getresources(struct drm_device *dev, void *data,
1322 struct drm_file *file_priv)
1324 struct drm_mode_card_res *card_res = data;
1325 struct list_head *lh;
1326 struct drm_framebuffer *fb;
1327 struct drm_connector *connector;
1328 struct drm_crtc *crtc;
1329 struct drm_encoder *encoder;
1331 int connector_count = 0;
1334 int encoder_count = 0;
1336 uint32_t __user *fb_id;
1337 uint32_t __user *crtc_id;
1338 uint32_t __user *connector_id;
1339 uint32_t __user *encoder_id;
1340 struct drm_mode_group *mode_group;
1342 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1346 mutex_lock(&file_priv->fbs_lock);
1348 * For the non-control nodes we need to limit the list of resources
1349 * by IDs in the group list for this node
1351 list_for_each(lh, &file_priv->fbs)
1354 /* handle this in 4 parts */
1356 if (card_res->count_fbs >= fb_count) {
1358 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1359 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1360 if (put_user(fb->base.id, fb_id + copied)) {
1361 mutex_unlock(&file_priv->fbs_lock);
1367 card_res->count_fbs = fb_count;
1368 mutex_unlock(&file_priv->fbs_lock);
1370 drm_modeset_lock_all(dev);
1372 mode_group = &file_priv->master->minor->mode_group;
1373 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1375 mode_group = NULL; /* XXXKIB */
1376 if (1 || file_priv->master) {
1379 list_for_each(lh, &dev->mode_config.crtc_list)
1382 list_for_each(lh, &dev->mode_config.connector_list)
1385 list_for_each(lh, &dev->mode_config.encoder_list)
1389 crtc_count = mode_group->num_crtcs;
1390 connector_count = mode_group->num_connectors;
1391 encoder_count = mode_group->num_encoders;
1394 card_res->max_height = dev->mode_config.max_height;
1395 card_res->min_height = dev->mode_config.min_height;
1396 card_res->max_width = dev->mode_config.max_width;
1397 card_res->min_width = dev->mode_config.min_width;
1400 if (card_res->count_crtcs >= crtc_count) {
1402 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1404 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1406 if (1 || file_priv->master) {
1408 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1410 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1411 if (put_user(crtc->base.id, crtc_id + copied)) {
1418 for (i = 0; i < mode_group->num_crtcs; i++) {
1419 if (put_user(mode_group->id_list[i],
1420 crtc_id + copied)) {
1428 card_res->count_crtcs = crtc_count;
1431 if (card_res->count_encoders >= encoder_count) {
1433 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1435 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1437 if (file_priv->master) {
1439 list_for_each_entry(encoder,
1440 &dev->mode_config.encoder_list,
1442 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1443 drm_get_encoder_name(encoder));
1444 if (put_user(encoder->base.id, encoder_id +
1452 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1453 if (put_user(mode_group->id_list[i],
1454 encoder_id + copied)) {
1463 card_res->count_encoders = encoder_count;
1466 if (card_res->count_connectors >= connector_count) {
1468 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1470 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1472 if (file_priv->master) {
1474 list_for_each_entry(connector,
1475 &dev->mode_config.connector_list,
1477 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1479 drm_get_connector_name(connector));
1480 if (put_user(connector->base.id,
1481 connector_id + copied)) {
1488 int start = mode_group->num_crtcs +
1489 mode_group->num_encoders;
1490 for (i = start; i < start + mode_group->num_connectors; i++) {
1491 if (put_user(mode_group->id_list[i],
1492 connector_id + copied)) {
1500 card_res->count_connectors = connector_count;
1502 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1503 card_res->count_connectors, card_res->count_encoders);
1506 drm_modeset_unlock_all(dev);
1511 * drm_mode_getcrtc - get CRTC configuration
1512 * @dev: drm device for the ioctl
1513 * @data: data pointer for the ioctl
1514 * @file_priv: drm file for the ioctl call
1516 * Construct a CRTC configuration structure to return to the user.
1518 * Called by the user via ioctl.
1521 * Zero on success, errno on failure.
1523 int drm_mode_getcrtc(struct drm_device *dev,
1524 void *data, struct drm_file *file_priv)
1526 struct drm_mode_crtc *crtc_resp = data;
1527 struct drm_crtc *crtc;
1528 struct drm_mode_object *obj;
1531 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1534 drm_modeset_lock_all(dev);
1536 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1537 DRM_MODE_OBJECT_CRTC);
1542 crtc = obj_to_crtc(obj);
1544 crtc_resp->x = crtc->x;
1545 crtc_resp->y = crtc->y;
1546 crtc_resp->gamma_size = crtc->gamma_size;
1548 crtc_resp->fb_id = crtc->fb->base.id;
1550 crtc_resp->fb_id = 0;
1552 if (crtc->enabled) {
1554 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1555 crtc_resp->mode_valid = 1;
1558 crtc_resp->mode_valid = 0;
1562 drm_modeset_unlock_all(dev);
1567 * drm_mode_getconnector - get connector configuration
1568 * @dev: drm device for the ioctl
1569 * @data: data pointer for the ioctl
1570 * @file_priv: drm file for the ioctl call
1572 * Construct a connector configuration structure to return to the user.
1574 * Called by the user via ioctl.
1577 * Zero on success, errno on failure.
1579 int drm_mode_getconnector(struct drm_device *dev, void *data,
1580 struct drm_file *file_priv)
1582 struct drm_mode_get_connector *out_resp = data;
1583 struct drm_mode_object *obj;
1584 struct drm_connector *connector;
1585 struct drm_display_mode *mode;
1587 int props_count = 0;
1588 int encoders_count = 0;
1592 struct drm_mode_modeinfo u_mode;
1593 struct drm_mode_modeinfo __user *mode_ptr;
1594 uint32_t __user *prop_ptr;
1595 uint64_t __user *prop_values;
1596 uint32_t __user *encoder_ptr;
1598 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1601 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1603 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1605 mutex_lock(&dev->mode_config.mutex);
1607 obj = drm_mode_object_find(dev, out_resp->connector_id,
1608 DRM_MODE_OBJECT_CONNECTOR);
1613 connector = obj_to_connector(obj);
1615 props_count = connector->properties.count;
1617 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1618 if (connector->encoder_ids[i] != 0) {
1623 if (out_resp->count_modes == 0) {
1624 connector->funcs->fill_modes(connector,
1625 dev->mode_config.max_width,
1626 dev->mode_config.max_height);
1629 /* delayed so we get modes regardless of pre-fill_modes state */
1630 list_for_each_entry(mode, &connector->modes, head)
1633 out_resp->connector_id = connector->base.id;
1634 out_resp->connector_type = connector->connector_type;
1635 out_resp->connector_type_id = connector->connector_type_id;
1636 out_resp->mm_width = connector->display_info.width_mm;
1637 out_resp->mm_height = connector->display_info.height_mm;
1638 out_resp->subpixel = connector->display_info.subpixel_order;
1639 out_resp->connection = connector->status;
1640 if (connector->encoder)
1641 out_resp->encoder_id = connector->encoder->base.id;
1643 out_resp->encoder_id = 0;
1646 * This ioctl is called twice, once to determine how much space is
1647 * needed, and the 2nd time to fill it.
1649 if ((out_resp->count_modes >= mode_count) && mode_count) {
1651 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1652 list_for_each_entry(mode, &connector->modes, head) {
1653 drm_crtc_convert_to_umode(&u_mode, mode);
1654 if (copy_to_user(mode_ptr + copied,
1655 &u_mode, sizeof(u_mode))) {
1662 out_resp->count_modes = mode_count;
1664 if ((out_resp->count_props >= props_count) && props_count) {
1666 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1667 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1668 for (i = 0; i < connector->properties.count; i++) {
1669 if (put_user(connector->properties.ids[i],
1670 prop_ptr + copied)) {
1675 if (put_user(connector->properties.values[i],
1676 prop_values + copied)) {
1683 out_resp->count_props = props_count;
1685 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1687 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1688 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1689 if (connector->encoder_ids[i] != 0) {
1690 if (put_user(connector->encoder_ids[i],
1691 encoder_ptr + copied)) {
1699 out_resp->count_encoders = encoders_count;
1702 mutex_unlock(&dev->mode_config.mutex);
1707 int drm_mode_getencoder(struct drm_device *dev, void *data,
1708 struct drm_file *file_priv)
1710 struct drm_mode_get_encoder *enc_resp = data;
1711 struct drm_mode_object *obj;
1712 struct drm_encoder *encoder;
1715 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1718 drm_modeset_lock_all(dev);
1719 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1720 DRM_MODE_OBJECT_ENCODER);
1725 encoder = obj_to_encoder(obj);
1728 enc_resp->crtc_id = encoder->crtc->base.id;
1730 enc_resp->crtc_id = 0;
1731 enc_resp->encoder_type = encoder->encoder_type;
1732 enc_resp->encoder_id = encoder->base.id;
1733 enc_resp->possible_crtcs = encoder->possible_crtcs;
1734 enc_resp->possible_clones = encoder->possible_clones;
1737 drm_modeset_unlock_all(dev);
1742 * drm_mode_getplane_res - get plane info
1745 * @file_priv: DRM file info
1747 * Return an plane count and set of IDs.
1749 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1750 struct drm_file *file_priv)
1752 struct drm_mode_get_plane_res *plane_resp = data;
1753 struct drm_mode_config *config;
1754 struct drm_plane *plane;
1755 uint32_t __user *plane_ptr;
1756 int copied = 0, ret = 0;
1758 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1761 drm_modeset_lock_all(dev);
1762 config = &dev->mode_config;
1765 * This ioctl is called twice, once to determine how much space is
1766 * needed, and the 2nd time to fill it.
1768 if (config->num_plane &&
1769 (plane_resp->count_planes >= config->num_plane)) {
1770 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1772 list_for_each_entry(plane, &config->plane_list, head) {
1773 if (put_user(plane->base.id, plane_ptr + copied)) {
1780 plane_resp->count_planes = config->num_plane;
1783 drm_modeset_unlock_all(dev);
1788 * drm_mode_getplane - get plane info
1791 * @file_priv: DRM file info
1793 * Return plane info, including formats supported, gamma size, any
1796 int drm_mode_getplane(struct drm_device *dev, void *data,
1797 struct drm_file *file_priv)
1799 struct drm_mode_get_plane *plane_resp = data;
1800 struct drm_mode_object *obj;
1801 struct drm_plane *plane;
1802 uint32_t __user *format_ptr;
1805 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1808 drm_modeset_lock_all(dev);
1809 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1810 DRM_MODE_OBJECT_PLANE);
1815 plane = obj_to_plane(obj);
1818 plane_resp->crtc_id = plane->crtc->base.id;
1820 plane_resp->crtc_id = 0;
1823 plane_resp->fb_id = plane->fb->base.id;
1825 plane_resp->fb_id = 0;
1827 plane_resp->plane_id = plane->base.id;
1828 plane_resp->possible_crtcs = plane->possible_crtcs;
1829 plane_resp->gamma_size = 0;
1832 * This ioctl is called twice, once to determine how much space is
1833 * needed, and the 2nd time to fill it.
1835 if (plane->format_count &&
1836 (plane_resp->count_format_types >= plane->format_count)) {
1837 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1838 if (copy_to_user(format_ptr,
1839 plane->format_types,
1840 sizeof(uint32_t) * plane->format_count)) {
1845 plane_resp->count_format_types = plane->format_count;
1848 drm_modeset_unlock_all(dev);
1853 * drm_mode_setplane - set up or tear down an plane
1855 * @data: ioctl data*
1856 * @file_priv: DRM file info
1858 * Set plane info, including placement, fb, scaling, and other factors.
1859 * Or pass a NULL fb to disable.
1861 int drm_mode_setplane(struct drm_device *dev, void *data,
1862 struct drm_file *file_priv)
1864 struct drm_mode_set_plane *plane_req = data;
1865 struct drm_mode_object *obj;
1866 struct drm_plane *plane;
1867 struct drm_crtc *crtc;
1868 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1870 unsigned int fb_width, fb_height;
1873 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1877 * First, find the plane, crtc, and fb objects. If not available,
1878 * we don't bother to call the driver.
1880 obj = drm_mode_object_find(dev, plane_req->plane_id,
1881 DRM_MODE_OBJECT_PLANE);
1883 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1884 plane_req->plane_id);
1887 plane = obj_to_plane(obj);
1889 /* No fb means shut it down */
1890 if (!plane_req->fb_id) {
1891 drm_modeset_lock_all(dev);
1893 plane->funcs->disable_plane(plane);
1896 drm_modeset_unlock_all(dev);
1900 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1901 DRM_MODE_OBJECT_CRTC);
1903 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1904 plane_req->crtc_id);
1908 crtc = obj_to_crtc(obj);
1910 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1912 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1918 /* Check whether this plane supports the fb pixel format. */
1919 for (i = 0; i < plane->format_count; i++)
1920 if (fb->pixel_format == plane->format_types[i])
1922 if (i == plane->format_count) {
1923 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1924 drm_get_format_name(fb->pixel_format));
1929 fb_width = fb->width << 16;
1930 fb_height = fb->height << 16;
1932 /* Make sure source coordinates are inside the fb. */
1933 if (plane_req->src_w > fb_width ||
1934 plane_req->src_x > fb_width - plane_req->src_w ||
1935 plane_req->src_h > fb_height ||
1936 plane_req->src_y > fb_height - plane_req->src_h) {
1937 DRM_DEBUG_KMS("Invalid source coordinates "
1938 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1939 plane_req->src_w >> 16,
1940 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1941 plane_req->src_h >> 16,
1942 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1943 plane_req->src_x >> 16,
1944 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1945 plane_req->src_y >> 16,
1946 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1951 /* Give drivers some help against integer overflows */
1952 if (plane_req->crtc_w > INT_MAX ||
1953 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1954 plane_req->crtc_h > INT_MAX ||
1955 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1956 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1957 plane_req->crtc_w, plane_req->crtc_h,
1958 plane_req->crtc_x, plane_req->crtc_y);
1963 drm_modeset_lock_all(dev);
1964 ret = plane->funcs->update_plane(plane, crtc, fb,
1965 plane_req->crtc_x, plane_req->crtc_y,
1966 plane_req->crtc_w, plane_req->crtc_h,
1967 plane_req->src_x, plane_req->src_y,
1968 plane_req->src_w, plane_req->src_h);
1975 drm_modeset_unlock_all(dev);
1979 drm_framebuffer_unreference(fb);
1981 drm_framebuffer_unreference(old_fb);
1987 * drm_mode_set_config_internal - helper to call ->set_config
1988 * @set: modeset config to set
1990 * This is a little helper to wrap internal calls to the ->set_config driver
1991 * interface. The only thing it adds is correct refcounting dance.
1993 int drm_mode_set_config_internal(struct drm_mode_set *set)
1995 struct drm_crtc *crtc = set->crtc;
1996 struct drm_framebuffer *fb;
1997 struct drm_crtc *tmp;
2001 * NOTE: ->set_config can also disable other crtcs (if we steal all
2002 * connectors from it), hence we need to refcount the fbs across all
2003 * crtcs. Atomic modeset will have saner semantics ...
2005 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2006 tmp->old_fb = tmp->fb;
2010 ret = crtc->funcs->set_config(set);
2012 /* crtc->fb must be updated by ->set_config, enforces this. */
2013 WARN_ON(fb != crtc->fb);
2016 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2018 drm_framebuffer_reference(tmp->fb);
2020 drm_framebuffer_unreference(tmp->old_fb);
2025 EXPORT_SYMBOL(drm_mode_set_config_internal);
2028 * drm_mode_setcrtc - set CRTC configuration
2029 * @dev: drm device for the ioctl
2030 * @data: data pointer for the ioctl
2031 * @file_priv: drm file for the ioctl call
2033 * Build a new CRTC configuration based on user request.
2035 * Called by the user via ioctl.
2038 * Zero on success, errno on failure.
2040 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2041 struct drm_file *file_priv)
2043 struct drm_mode_config *config = &dev->mode_config;
2044 struct drm_mode_crtc *crtc_req = data;
2045 struct drm_mode_object *obj;
2046 struct drm_crtc *crtc;
2047 struct drm_connector **connector_set = NULL, *connector;
2048 struct drm_framebuffer *fb = NULL;
2049 struct drm_display_mode *mode = NULL;
2050 struct drm_mode_set set;
2051 uint32_t __user *set_connectors_ptr;
2055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2058 /* For some reason crtc x/y offsets are signed internally. */
2059 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2062 drm_modeset_lock_all(dev);
2063 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2064 DRM_MODE_OBJECT_CRTC);
2066 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2070 crtc = obj_to_crtc(obj);
2071 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2073 if (crtc_req->mode_valid) {
2074 int hdisplay, vdisplay;
2075 /* If we have a mode we need a framebuffer. */
2076 /* If we pass -1, set the mode with the currently bound fb */
2077 if (crtc_req->fb_id == -1) {
2079 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2084 /* Make refcounting symmetric with the lookup path. */
2085 drm_framebuffer_reference(fb);
2087 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2089 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2096 mode = drm_mode_create(dev);
2102 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2104 DRM_DEBUG_KMS("Invalid mode\n");
2108 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2110 hdisplay = mode->hdisplay;
2111 vdisplay = mode->vdisplay;
2113 if (crtc->invert_dimensions)
2114 swap(hdisplay, vdisplay);
2116 if (hdisplay > fb->width ||
2117 vdisplay > fb->height ||
2118 crtc_req->x > fb->width - hdisplay ||
2119 crtc_req->y > fb->height - vdisplay) {
2120 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2121 fb->width, fb->height,
2122 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2123 crtc->invert_dimensions ? " (inverted)" : "");
2129 if (crtc_req->count_connectors == 0 && mode) {
2130 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2135 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2136 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2137 crtc_req->count_connectors);
2142 if (crtc_req->count_connectors > 0) {
2145 /* Avoid unbounded kernel memory allocation */
2146 if (crtc_req->count_connectors > config->num_connector) {
2151 connector_set = kmalloc(crtc_req->count_connectors *
2152 sizeof(struct drm_connector *),
2154 if (!connector_set) {
2159 for (i = 0; i < crtc_req->count_connectors; i++) {
2160 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2161 if (get_user(out_id, &set_connectors_ptr[i])) {
2166 obj = drm_mode_object_find(dev, out_id,
2167 DRM_MODE_OBJECT_CONNECTOR);
2169 DRM_DEBUG_KMS("Connector id %d unknown\n",
2174 connector = obj_to_connector(obj);
2175 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2177 drm_get_connector_name(connector));
2179 connector_set[i] = connector;
2184 set.x = crtc_req->x;
2185 set.y = crtc_req->y;
2187 set.connectors = connector_set;
2188 set.num_connectors = crtc_req->count_connectors;
2190 ret = drm_mode_set_config_internal(&set);
2194 drm_framebuffer_unreference(fb);
2196 kfree(connector_set);
2197 drm_mode_destroy(dev, mode);
2198 drm_modeset_unlock_all(dev);
2202 static int drm_mode_cursor_common(struct drm_device *dev,
2203 struct drm_mode_cursor2 *req,
2204 struct drm_file *file_priv)
2206 struct drm_mode_object *obj;
2207 struct drm_crtc *crtc;
2210 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2213 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2216 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2218 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2221 crtc = obj_to_crtc(obj);
2223 mutex_lock(&crtc->mutex);
2224 if (req->flags & DRM_MODE_CURSOR_BO) {
2225 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2229 /* Turns off the cursor if handle is 0 */
2230 if (crtc->funcs->cursor_set2)
2231 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2232 req->width, req->height, req->hot_x, req->hot_y);
2234 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2235 req->width, req->height);
2238 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2239 if (crtc->funcs->cursor_move) {
2240 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2247 mutex_unlock(&crtc->mutex);
2252 int drm_mode_cursor_ioctl(struct drm_device *dev,
2253 void *data, struct drm_file *file_priv)
2255 struct drm_mode_cursor *req = data;
2256 struct drm_mode_cursor2 new_req;
2258 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2259 new_req.hot_x = new_req.hot_y = 0;
2261 return drm_mode_cursor_common(dev, &new_req, file_priv);
2264 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2265 void *data, struct drm_file *file_priv)
2267 struct drm_mode_cursor2 *req = data;
2268 return drm_mode_cursor_common(dev, req, file_priv);
2271 /* Original addfb only supported RGB formats, so figure out which one */
2272 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2278 fmt = DRM_FORMAT_C8;
2282 fmt = DRM_FORMAT_XRGB1555;
2284 fmt = DRM_FORMAT_RGB565;
2287 fmt = DRM_FORMAT_RGB888;
2291 fmt = DRM_FORMAT_XRGB8888;
2292 else if (depth == 30)
2293 fmt = DRM_FORMAT_XRGB2101010;
2295 fmt = DRM_FORMAT_ARGB8888;
2298 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2299 fmt = DRM_FORMAT_XRGB8888;
2305 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2308 * drm_mode_addfb - add an FB to the graphics configuration
2309 * @dev: drm device for the ioctl
2310 * @data: data pointer for the ioctl
2311 * @file_priv: drm file for the ioctl call
2313 * Add a new FB to the specified CRTC, given a user request.
2315 * Called by the user via ioctl.
2318 * Zero on success, errno on failure.
2320 int drm_mode_addfb(struct drm_device *dev,
2321 void *data, struct drm_file *file_priv)
2323 struct drm_mode_fb_cmd *or = data;
2324 struct drm_mode_fb_cmd2 r = {};
2325 struct drm_mode_config *config = &dev->mode_config;
2326 struct drm_framebuffer *fb;
2329 /* Use new struct with format internally */
2330 r.fb_id = or->fb_id;
2331 r.width = or->width;
2332 r.height = or->height;
2333 r.pitches[0] = or->pitch;
2334 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2335 r.handles[0] = or->handle;
2337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2340 if ((config->min_width > r.width) || (r.width > config->max_width))
2343 if ((config->min_height > r.height) || (r.height > config->max_height))
2346 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2348 DRM_DEBUG_KMS("could not create framebuffer\n");
2352 mutex_lock(&file_priv->fbs_lock);
2353 or->fb_id = fb->base.id;
2354 list_add(&fb->filp_head, &file_priv->fbs);
2355 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2356 mutex_unlock(&file_priv->fbs_lock);
2361 static int format_check(const struct drm_mode_fb_cmd2 *r)
2363 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2367 case DRM_FORMAT_RGB332:
2368 case DRM_FORMAT_BGR233:
2369 case DRM_FORMAT_XRGB4444:
2370 case DRM_FORMAT_XBGR4444:
2371 case DRM_FORMAT_RGBX4444:
2372 case DRM_FORMAT_BGRX4444:
2373 case DRM_FORMAT_ARGB4444:
2374 case DRM_FORMAT_ABGR4444:
2375 case DRM_FORMAT_RGBA4444:
2376 case DRM_FORMAT_BGRA4444:
2377 case DRM_FORMAT_XRGB1555:
2378 case DRM_FORMAT_XBGR1555:
2379 case DRM_FORMAT_RGBX5551:
2380 case DRM_FORMAT_BGRX5551:
2381 case DRM_FORMAT_ARGB1555:
2382 case DRM_FORMAT_ABGR1555:
2383 case DRM_FORMAT_RGBA5551:
2384 case DRM_FORMAT_BGRA5551:
2385 case DRM_FORMAT_RGB565:
2386 case DRM_FORMAT_BGR565:
2387 case DRM_FORMAT_RGB888:
2388 case DRM_FORMAT_BGR888:
2389 case DRM_FORMAT_XRGB8888:
2390 case DRM_FORMAT_XBGR8888:
2391 case DRM_FORMAT_RGBX8888:
2392 case DRM_FORMAT_BGRX8888:
2393 case DRM_FORMAT_ARGB8888:
2394 case DRM_FORMAT_ABGR8888:
2395 case DRM_FORMAT_RGBA8888:
2396 case DRM_FORMAT_BGRA8888:
2397 case DRM_FORMAT_XRGB2101010:
2398 case DRM_FORMAT_XBGR2101010:
2399 case DRM_FORMAT_RGBX1010102:
2400 case DRM_FORMAT_BGRX1010102:
2401 case DRM_FORMAT_ARGB2101010:
2402 case DRM_FORMAT_ABGR2101010:
2403 case DRM_FORMAT_RGBA1010102:
2404 case DRM_FORMAT_BGRA1010102:
2405 case DRM_FORMAT_YUYV:
2406 case DRM_FORMAT_YVYU:
2407 case DRM_FORMAT_UYVY:
2408 case DRM_FORMAT_VYUY:
2409 case DRM_FORMAT_AYUV:
2410 case DRM_FORMAT_NV12:
2411 case DRM_FORMAT_NV21:
2412 case DRM_FORMAT_NV16:
2413 case DRM_FORMAT_NV61:
2414 case DRM_FORMAT_NV24:
2415 case DRM_FORMAT_NV42:
2416 case DRM_FORMAT_YUV410:
2417 case DRM_FORMAT_YVU410:
2418 case DRM_FORMAT_YUV411:
2419 case DRM_FORMAT_YVU411:
2420 case DRM_FORMAT_YUV420:
2421 case DRM_FORMAT_YVU420:
2422 case DRM_FORMAT_YUV422:
2423 case DRM_FORMAT_YVU422:
2424 case DRM_FORMAT_YUV444:
2425 case DRM_FORMAT_YVU444:
2432 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2434 int ret, hsub, vsub, num_planes, i;
2436 ret = format_check(r);
2438 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2439 drm_get_format_name(r->pixel_format));
2443 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2444 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2445 num_planes = drm_format_num_planes(r->pixel_format);
2447 if (r->width == 0 || r->width % hsub) {
2448 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2452 if (r->height == 0 || r->height % vsub) {
2453 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2457 for (i = 0; i < num_planes; i++) {
2458 unsigned int width = r->width / (i != 0 ? hsub : 1);
2459 unsigned int height = r->height / (i != 0 ? vsub : 1);
2460 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2462 if (!r->handles[i]) {
2463 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2467 if ((uint64_t) width * cpp > UINT_MAX)
2470 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2473 if (r->pitches[i] < width * cpp) {
2474 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2483 * drm_mode_addfb2 - add an FB to the graphics configuration
2484 * @dev: drm device for the ioctl
2485 * @data: data pointer for the ioctl
2486 * @file_priv: drm file for the ioctl call
2488 * Add a new FB to the specified CRTC, given a user request with format.
2490 * Called by the user via ioctl.
2493 * Zero on success, errno on failure.
2495 int drm_mode_addfb2(struct drm_device *dev,
2496 void *data, struct drm_file *file_priv)
2498 struct drm_mode_fb_cmd2 *r = data;
2499 struct drm_mode_config *config = &dev->mode_config;
2500 struct drm_framebuffer *fb;
2503 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2506 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2507 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2511 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2512 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2513 r->width, config->min_width, config->max_width);
2516 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2517 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2518 r->height, config->min_height, config->max_height);
2522 ret = framebuffer_check(r);
2526 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2528 DRM_DEBUG_KMS("could not create framebuffer\n");
2532 mutex_lock(&file_priv->fbs_lock);
2533 r->fb_id = fb->base.id;
2534 list_add(&fb->filp_head, &file_priv->fbs);
2535 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2536 mutex_unlock(&file_priv->fbs_lock);
2543 * drm_mode_rmfb - remove an FB from the configuration
2544 * @dev: drm device for the ioctl
2545 * @data: data pointer for the ioctl
2546 * @file_priv: drm file for the ioctl call
2548 * Remove the FB specified by the user.
2550 * Called by the user via ioctl.
2553 * Zero on success, errno on failure.
2555 int drm_mode_rmfb(struct drm_device *dev,
2556 void *data, struct drm_file *file_priv)
2558 struct drm_framebuffer *fb = NULL;
2559 struct drm_framebuffer *fbl = NULL;
2560 uint32_t *id = data;
2563 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2566 mutex_lock(&file_priv->fbs_lock);
2567 mutex_lock(&dev->mode_config.fb_lock);
2568 fb = __drm_framebuffer_lookup(dev, *id);
2572 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2578 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2579 __drm_framebuffer_unregister(dev, fb);
2581 list_del_init(&fb->filp_head);
2582 mutex_unlock(&dev->mode_config.fb_lock);
2583 mutex_unlock(&file_priv->fbs_lock);
2585 drm_framebuffer_remove(fb);
2590 mutex_unlock(&dev->mode_config.fb_lock);
2591 mutex_unlock(&file_priv->fbs_lock);
2597 * drm_mode_getfb - get FB info
2598 * @dev: drm device for the ioctl
2599 * @data: data pointer for the ioctl
2600 * @file_priv: drm file for the ioctl call
2602 * Lookup the FB given its ID and return info about it.
2604 * Called by the user via ioctl.
2607 * Zero on success, errno on failure.
2609 int drm_mode_getfb(struct drm_device *dev,
2610 void *data, struct drm_file *file_priv)
2612 struct drm_mode_fb_cmd *r = data;
2613 struct drm_framebuffer *fb;
2616 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2619 fb = drm_framebuffer_lookup(dev, r->fb_id);
2623 r->height = fb->height;
2624 r->width = fb->width;
2625 r->depth = fb->depth;
2626 r->bpp = fb->bits_per_pixel;
2627 r->pitch = fb->pitches[0];
2628 if (fb->funcs->create_handle)
2629 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2633 drm_framebuffer_unreference(fb);
2638 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2639 void *data, struct drm_file *file_priv)
2641 struct drm_clip_rect __user *clips_ptr;
2642 struct drm_clip_rect *clips = NULL;
2643 struct drm_mode_fb_dirty_cmd *r = data;
2644 struct drm_framebuffer *fb;
2649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2652 fb = drm_framebuffer_lookup(dev, r->fb_id);
2656 num_clips = r->num_clips;
2657 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2659 if (!num_clips != !clips_ptr) {
2664 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2666 /* If userspace annotates copy, clips must come in pairs */
2667 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2672 if (num_clips && clips_ptr) {
2673 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2677 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2683 ret = copy_from_user(clips, clips_ptr,
2684 num_clips * sizeof(*clips));
2691 if (fb->funcs->dirty) {
2692 drm_modeset_lock_all(dev);
2693 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2695 drm_modeset_unlock_all(dev);
2703 drm_framebuffer_unreference(fb);
2710 * drm_fb_release - remove and free the FBs on this file
2711 * @priv: drm file for the ioctl
2713 * Destroy all the FBs associated with @filp.
2715 * Called by the user via ioctl.
2718 * Zero on success, errno on failure.
2720 void drm_fb_release(struct drm_file *priv)
2723 struct drm_device *dev = priv->minor->dev;
2725 struct drm_device *dev = priv->dev;
2727 struct drm_framebuffer *fb, *tfb;
2729 mutex_lock(&priv->fbs_lock);
2730 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2732 mutex_lock(&dev->mode_config.fb_lock);
2733 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2734 __drm_framebuffer_unregister(dev, fb);
2735 mutex_unlock(&dev->mode_config.fb_lock);
2737 list_del_init(&fb->filp_head);
2739 /* This will also drop the fpriv->fbs reference. */
2740 drm_framebuffer_remove(fb);
2742 mutex_unlock(&priv->fbs_lock);
2745 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2746 const char *name, int num_values)
2748 struct drm_property *property = NULL;
2751 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2756 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2757 if (!property->values)
2761 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2765 property->flags = flags;
2766 property->num_values = num_values;
2767 INIT_LIST_HEAD(&property->enum_blob_list);
2770 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2771 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2774 list_add_tail(&property->head, &dev->mode_config.property_list);
2777 kfree(property->values);
2781 EXPORT_SYMBOL(drm_property_create);
2783 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2785 const struct drm_prop_enum_list *props,
2788 struct drm_property *property;
2791 flags |= DRM_MODE_PROP_ENUM;
2793 property = drm_property_create(dev, flags, name, num_values);
2797 for (i = 0; i < num_values; i++) {
2798 ret = drm_property_add_enum(property, i,
2802 drm_property_destroy(dev, property);
2809 EXPORT_SYMBOL(drm_property_create_enum);
2811 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2812 int flags, const char *name,
2813 const struct drm_prop_enum_list *props,
2816 struct drm_property *property;
2819 flags |= DRM_MODE_PROP_BITMASK;
2821 property = drm_property_create(dev, flags, name, num_values);
2825 for (i = 0; i < num_values; i++) {
2826 ret = drm_property_add_enum(property, i,
2830 drm_property_destroy(dev, property);
2837 EXPORT_SYMBOL(drm_property_create_bitmask);
2839 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2841 uint64_t min, uint64_t max)
2843 struct drm_property *property;
2845 flags |= DRM_MODE_PROP_RANGE;
2847 property = drm_property_create(dev, flags, name, 2);
2851 property->values[0] = min;
2852 property->values[1] = max;
2856 EXPORT_SYMBOL(drm_property_create_range);
2858 int drm_property_add_enum(struct drm_property *property, int index,
2859 uint64_t value, const char *name)
2861 struct drm_property_enum *prop_enum;
2863 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2867 * Bitmask enum properties have the additional constraint of values
2870 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2873 if (!list_empty(&property->enum_blob_list)) {
2874 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2875 if (prop_enum->value == value) {
2876 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2877 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2883 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2887 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2888 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2889 prop_enum->value = value;
2891 property->values[index] = value;
2892 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2895 EXPORT_SYMBOL(drm_property_add_enum);
2897 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2899 struct drm_property_enum *prop_enum, *pt;
2901 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2902 list_del(&prop_enum->head);
2906 if (property->num_values)
2907 kfree(property->values);
2908 drm_mode_object_put(dev, &property->base);
2909 list_del(&property->head);
2912 EXPORT_SYMBOL(drm_property_destroy);
2914 void drm_object_attach_property(struct drm_mode_object *obj,
2915 struct drm_property *property,
2918 int count = obj->properties->count;
2920 if (count == DRM_OBJECT_MAX_PROPERTY) {
2921 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2922 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2923 "you see this message on the same object type.\n",
2928 obj->properties->ids[count] = property->base.id;
2929 obj->properties->values[count] = init_val;
2930 obj->properties->count++;
2932 EXPORT_SYMBOL(drm_object_attach_property);
2934 int drm_object_property_set_value(struct drm_mode_object *obj,
2935 struct drm_property *property, uint64_t val)
2939 for (i = 0; i < obj->properties->count; i++) {
2940 if (obj->properties->ids[i] == property->base.id) {
2941 obj->properties->values[i] = val;
2948 EXPORT_SYMBOL(drm_object_property_set_value);
2950 int drm_object_property_get_value(struct drm_mode_object *obj,
2951 struct drm_property *property, uint64_t *val)
2955 for (i = 0; i < obj->properties->count; i++) {
2956 if (obj->properties->ids[i] == property->base.id) {
2957 *val = obj->properties->values[i];
2964 EXPORT_SYMBOL(drm_object_property_get_value);
2966 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2967 void *data, struct drm_file *file_priv)
2969 struct drm_mode_object *obj;
2970 struct drm_mode_get_property *out_resp = data;
2971 struct drm_property *property;
2974 int value_count = 0;
2977 struct drm_property_enum *prop_enum;
2978 struct drm_mode_property_enum __user *enum_ptr;
2979 struct drm_property_blob *prop_blob;
2980 uint32_t __user *blob_id_ptr;
2981 uint64_t __user *values_ptr;
2982 uint32_t __user *blob_length_ptr;
2984 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2987 drm_modeset_lock_all(dev);
2988 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2993 property = obj_to_property(obj);
2995 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2996 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2998 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2999 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3003 value_count = property->num_values;
3005 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3006 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3007 out_resp->flags = property->flags;
3009 if ((out_resp->count_values >= value_count) && value_count) {
3010 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3011 for (i = 0; i < value_count; i++) {
3012 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3018 out_resp->count_values = value_count;
3020 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3021 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3023 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3024 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3026 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3031 if (copy_to_user(&enum_ptr[copied].name,
3032 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3039 out_resp->count_enum_blobs = enum_count;
3042 if (property->flags & DRM_MODE_PROP_BLOB) {
3043 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3045 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3046 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3048 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3049 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3054 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3062 out_resp->count_enum_blobs = blob_count;
3065 drm_modeset_unlock_all(dev);
3069 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3072 struct drm_property_blob *blob;
3075 if (!length || !data)
3078 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3082 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3088 blob->length = length;
3090 memcpy(blob->data, data, length);
3092 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3096 static void drm_property_destroy_blob(struct drm_device *dev,
3097 struct drm_property_blob *blob)
3099 drm_mode_object_put(dev, &blob->base);
3100 list_del(&blob->head);
3104 int drm_mode_getblob_ioctl(struct drm_device *dev,
3105 void *data, struct drm_file *file_priv)
3107 struct drm_mode_object *obj;
3108 struct drm_mode_get_blob *out_resp = data;
3109 struct drm_property_blob *blob;
3111 void __user *blob_ptr;
3113 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3116 drm_modeset_lock_all(dev);
3117 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3122 blob = obj_to_blob(obj);
3124 if (out_resp->length == blob->length) {
3125 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3126 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3131 out_resp->length = blob->length;
3134 drm_modeset_unlock_all(dev);
3138 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3141 struct drm_device *dev = connector->dev;
3144 if (connector->edid_blob_ptr)
3145 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3147 /* Delete edid, when there is none. */
3149 connector->edid_blob_ptr = NULL;
3150 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3154 size = EDID_LENGTH * (1 + edid->extensions);
3155 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3157 if (!connector->edid_blob_ptr)
3160 ret = drm_object_property_set_value(&connector->base,
3161 dev->mode_config.edid_property,
3162 connector->edid_blob_ptr->base.id);
3166 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3168 static bool drm_property_change_is_valid(struct drm_property *property,
3171 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3173 if (property->flags & DRM_MODE_PROP_RANGE) {
3174 if (value < property->values[0] || value > property->values[1])
3177 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3179 uint64_t valid_mask = 0;
3180 for (i = 0; i < property->num_values; i++)
3181 valid_mask |= (1ULL << property->values[i]);
3182 return !(value & ~valid_mask);
3183 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3184 /* Only the driver knows */
3188 for (i = 0; i < property->num_values; i++)
3189 if (property->values[i] == value)
3195 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3196 void *data, struct drm_file *file_priv)
3198 struct drm_mode_connector_set_property *conn_set_prop = data;
3199 struct drm_mode_obj_set_property obj_set_prop = {
3200 .value = conn_set_prop->value,
3201 .prop_id = conn_set_prop->prop_id,
3202 .obj_id = conn_set_prop->connector_id,
3203 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3206 /* It does all the locking and checking we need */
3207 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3210 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3211 struct drm_property *property,
3215 struct drm_connector *connector = obj_to_connector(obj);
3217 /* Do DPMS ourselves */
3218 if (property == connector->dev->mode_config.dpms_property) {
3219 if (connector->funcs->dpms)
3220 (*connector->funcs->dpms)(connector, (int)value);
3222 } else if (connector->funcs->set_property)
3223 ret = connector->funcs->set_property(connector, property, value);
3225 /* store the property value if successful */
3227 drm_object_property_set_value(&connector->base, property, value);
3231 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3232 struct drm_property *property,
3236 struct drm_crtc *crtc = obj_to_crtc(obj);
3238 if (crtc->funcs->set_property)
3239 ret = crtc->funcs->set_property(crtc, property, value);
3241 drm_object_property_set_value(obj, property, value);
3246 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3247 struct drm_property *property,
3251 struct drm_plane *plane = obj_to_plane(obj);
3253 if (plane->funcs->set_property)
3254 ret = plane->funcs->set_property(plane, property, value);
3256 drm_object_property_set_value(obj, property, value);
3261 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3262 struct drm_file *file_priv)
3264 struct drm_mode_obj_get_properties *arg = data;
3265 struct drm_mode_object *obj;
3269 int props_count = 0;
3270 uint32_t __user *props_ptr;
3271 uint64_t __user *prop_values_ptr;
3273 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3276 drm_modeset_lock_all(dev);
3278 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3283 if (!obj->properties) {
3288 props_count = obj->properties->count;
3290 /* This ioctl is called twice, once to determine how much space is
3291 * needed, and the 2nd time to fill it. */
3292 if ((arg->count_props >= props_count) && props_count) {
3294 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3295 prop_values_ptr = (uint64_t __user *)(unsigned long)
3296 (arg->prop_values_ptr);
3297 for (i = 0; i < props_count; i++) {
3298 if (put_user(obj->properties->ids[i],
3299 props_ptr + copied)) {
3303 if (put_user(obj->properties->values[i],
3304 prop_values_ptr + copied)) {
3311 arg->count_props = props_count;
3313 drm_modeset_unlock_all(dev);
3317 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3318 struct drm_file *file_priv)
3320 struct drm_mode_obj_set_property *arg = data;
3321 struct drm_mode_object *arg_obj;
3322 struct drm_mode_object *prop_obj;
3323 struct drm_property *property;
3327 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3330 drm_modeset_lock_all(dev);
3332 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3335 if (!arg_obj->properties)
3338 for (i = 0; i < arg_obj->properties->count; i++)
3339 if (arg_obj->properties->ids[i] == arg->prop_id)
3342 if (i == arg_obj->properties->count)
3345 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3346 DRM_MODE_OBJECT_PROPERTY);
3349 property = obj_to_property(prop_obj);
3351 if (!drm_property_change_is_valid(property, arg->value))
3354 switch (arg_obj->type) {
3355 case DRM_MODE_OBJECT_CONNECTOR:
3356 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3359 case DRM_MODE_OBJECT_CRTC:
3360 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3362 case DRM_MODE_OBJECT_PLANE:
3363 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3368 drm_modeset_unlock_all(dev);
3372 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3373 struct drm_encoder *encoder)
3377 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3378 if (connector->encoder_ids[i] == 0) {
3379 connector->encoder_ids[i] = encoder->base.id;
3385 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3387 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3388 struct drm_encoder *encoder)
3391 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3392 if (connector->encoder_ids[i] == encoder->base.id) {
3393 connector->encoder_ids[i] = 0;
3394 if (connector->encoder == encoder)
3395 connector->encoder = NULL;
3400 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3402 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3405 crtc->gamma_size = gamma_size;
3407 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3408 if (!crtc->gamma_store) {
3409 crtc->gamma_size = 0;
3415 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3417 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3418 void *data, struct drm_file *file_priv)
3420 struct drm_mode_crtc_lut *crtc_lut = data;
3421 struct drm_mode_object *obj;
3422 struct drm_crtc *crtc;
3423 void *r_base, *g_base, *b_base;
3427 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3430 drm_modeset_lock_all(dev);
3431 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3436 crtc = obj_to_crtc(obj);
3438 if (crtc->funcs->gamma_set == NULL) {
3443 /* memcpy into gamma store */
3444 if (crtc_lut->gamma_size != crtc->gamma_size) {
3449 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3450 r_base = crtc->gamma_store;
3451 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3456 g_base = (char *)r_base + size;
3457 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3462 b_base = (char *)g_base + size;
3463 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3468 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3471 drm_modeset_unlock_all(dev);
3476 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3477 void *data, struct drm_file *file_priv)
3479 struct drm_mode_crtc_lut *crtc_lut = data;
3480 struct drm_mode_object *obj;
3481 struct drm_crtc *crtc;
3482 void *r_base, *g_base, *b_base;
3486 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3489 drm_modeset_lock_all(dev);
3490 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3495 crtc = obj_to_crtc(obj);
3497 /* memcpy into gamma store */
3498 if (crtc_lut->gamma_size != crtc->gamma_size) {
3503 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3504 r_base = crtc->gamma_store;
3505 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3510 g_base = (char *)r_base + size;
3511 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3516 b_base = (char *)g_base + size;
3517 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3522 drm_modeset_unlock_all(dev);
3527 * The Linux version of kfree() is a macro and can't be called
3528 * directly via a function pointer
3530 static void drm_kms_free(void *arg)
3535 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3536 void *data, struct drm_file *file_priv)
3538 struct drm_mode_crtc_page_flip *page_flip = data;
3539 struct drm_mode_object *obj;
3540 struct drm_crtc *crtc;
3541 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3542 struct drm_pending_vblank_event *e = NULL;
3543 int hdisplay, vdisplay;
3546 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3547 page_flip->reserved != 0)
3550 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3553 crtc = obj_to_crtc(obj);
3555 mutex_lock(&crtc->mutex);
3556 if (crtc->fb == NULL) {
3557 /* The framebuffer is currently unbound, presumably
3558 * due to a hotplug event, that userspace has not
3565 if (crtc->funcs->page_flip == NULL)
3568 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3572 hdisplay = crtc->mode.hdisplay;
3573 vdisplay = crtc->mode.vdisplay;
3575 if (crtc->invert_dimensions)
3576 swap(hdisplay, vdisplay);
3578 if (hdisplay > fb->width ||
3579 vdisplay > fb->height ||
3580 crtc->x > fb->width - hdisplay ||
3581 crtc->y > fb->height - vdisplay) {
3582 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3583 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3584 crtc->invert_dimensions ? " (inverted)" : "");
3589 if (crtc->fb->pixel_format != fb->pixel_format) {
3590 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3595 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3597 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3598 if (file_priv->event_space < sizeof e->event) {
3599 lockmgr(&dev->event_lock, LK_RELEASE);
3602 file_priv->event_space -= sizeof e->event;
3603 lockmgr(&dev->event_lock, LK_RELEASE);
3605 e = kzalloc(sizeof *e, GFP_KERNEL);
3607 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3608 file_priv->event_space += sizeof e->event;
3609 lockmgr(&dev->event_lock, LK_RELEASE);
3613 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3614 e->event.base.length = sizeof e->event;
3615 e->event.user_data = page_flip->user_data;
3616 e->base.event = &e->event.base;
3617 e->base.file_priv = file_priv;
3619 (void (*) (struct drm_pending_event *))drm_kms_free;
3623 ret = crtc->funcs->page_flip(crtc, fb, e);
3625 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3626 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3627 file_priv->event_space += sizeof e->event;
3628 lockmgr(&dev->event_lock, LK_RELEASE);
3631 /* Keep the old fb, don't unref it. */
3635 * Warn if the driver hasn't properly updated the crtc->fb
3636 * field to reflect that the new framebuffer is now used.
3637 * Failing to do so will screw with the reference counting
3640 WARN_ON(crtc->fb != fb);
3641 /* Unref only the old framebuffer. */
3647 drm_framebuffer_unreference(fb);
3649 drm_framebuffer_unreference(old_fb);
3650 mutex_unlock(&crtc->mutex);
3655 void drm_mode_config_reset(struct drm_device *dev)
3657 struct drm_crtc *crtc;
3658 struct drm_encoder *encoder;
3659 struct drm_connector *connector;
3661 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3662 if (crtc->funcs->reset)
3663 crtc->funcs->reset(crtc);
3665 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3666 if (encoder->funcs->reset)
3667 encoder->funcs->reset(encoder);
3669 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3670 connector->status = connector_status_unknown;
3672 if (connector->funcs->reset)
3673 connector->funcs->reset(connector);
3676 EXPORT_SYMBOL(drm_mode_config_reset);
3678 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3679 void *data, struct drm_file *file_priv)
3681 struct drm_mode_create_dumb *args = data;
3683 if (!dev->driver->dumb_create)
3685 return dev->driver->dumb_create(file_priv, dev, args);
3688 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3689 void *data, struct drm_file *file_priv)
3691 struct drm_mode_map_dumb *args = data;
3693 /* call driver ioctl to get mmap offset */
3694 if (!dev->driver->dumb_map_offset)
3697 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3700 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3701 void *data, struct drm_file *file_priv)
3703 struct drm_mode_destroy_dumb *args = data;
3705 if (!dev->driver->dumb_destroy)
3708 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3712 * Just need to support RGB formats here for compat with code that doesn't
3713 * use pixel formats directly yet.
3715 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3720 case DRM_FORMAT_RGB332:
3721 case DRM_FORMAT_BGR233:
3725 case DRM_FORMAT_XRGB1555:
3726 case DRM_FORMAT_XBGR1555:
3727 case DRM_FORMAT_RGBX5551:
3728 case DRM_FORMAT_BGRX5551:
3729 case DRM_FORMAT_ARGB1555:
3730 case DRM_FORMAT_ABGR1555:
3731 case DRM_FORMAT_RGBA5551:
3732 case DRM_FORMAT_BGRA5551:
3736 case DRM_FORMAT_RGB565:
3737 case DRM_FORMAT_BGR565:
3741 case DRM_FORMAT_RGB888:
3742 case DRM_FORMAT_BGR888:
3746 case DRM_FORMAT_XRGB8888:
3747 case DRM_FORMAT_XBGR8888:
3748 case DRM_FORMAT_RGBX8888:
3749 case DRM_FORMAT_BGRX8888:
3753 case DRM_FORMAT_XRGB2101010:
3754 case DRM_FORMAT_XBGR2101010:
3755 case DRM_FORMAT_RGBX1010102:
3756 case DRM_FORMAT_BGRX1010102:
3757 case DRM_FORMAT_ARGB2101010:
3758 case DRM_FORMAT_ABGR2101010:
3759 case DRM_FORMAT_RGBA1010102:
3760 case DRM_FORMAT_BGRA1010102:
3764 case DRM_FORMAT_ARGB8888:
3765 case DRM_FORMAT_ABGR8888:
3766 case DRM_FORMAT_RGBA8888:
3767 case DRM_FORMAT_BGRA8888:
3772 DRM_DEBUG_KMS("unsupported pixel format\n");
3778 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3781 * drm_format_num_planes - get the number of planes for format
3782 * @format: pixel format (DRM_FORMAT_*)
3785 * The number of planes used by the specified pixel format.
3787 int drm_format_num_planes(uint32_t format)
3790 case DRM_FORMAT_YUV410:
3791 case DRM_FORMAT_YVU410:
3792 case DRM_FORMAT_YUV411:
3793 case DRM_FORMAT_YVU411:
3794 case DRM_FORMAT_YUV420:
3795 case DRM_FORMAT_YVU420:
3796 case DRM_FORMAT_YUV422:
3797 case DRM_FORMAT_YVU422:
3798 case DRM_FORMAT_YUV444:
3799 case DRM_FORMAT_YVU444:
3801 case DRM_FORMAT_NV12:
3802 case DRM_FORMAT_NV21:
3803 case DRM_FORMAT_NV16:
3804 case DRM_FORMAT_NV61:
3805 case DRM_FORMAT_NV24:
3806 case DRM_FORMAT_NV42:
3812 EXPORT_SYMBOL(drm_format_num_planes);
3815 * drm_format_plane_cpp - determine the bytes per pixel value
3816 * @format: pixel format (DRM_FORMAT_*)
3817 * @plane: plane index
3820 * The bytes per pixel value for the specified plane.
3822 int drm_format_plane_cpp(uint32_t format, int plane)
3827 if (plane >= drm_format_num_planes(format))
3831 case DRM_FORMAT_YUYV:
3832 case DRM_FORMAT_YVYU:
3833 case DRM_FORMAT_UYVY:
3834 case DRM_FORMAT_VYUY:
3836 case DRM_FORMAT_NV12:
3837 case DRM_FORMAT_NV21:
3838 case DRM_FORMAT_NV16:
3839 case DRM_FORMAT_NV61:
3840 case DRM_FORMAT_NV24:
3841 case DRM_FORMAT_NV42:
3842 return plane ? 2 : 1;
3843 case DRM_FORMAT_YUV410:
3844 case DRM_FORMAT_YVU410:
3845 case DRM_FORMAT_YUV411:
3846 case DRM_FORMAT_YVU411:
3847 case DRM_FORMAT_YUV420:
3848 case DRM_FORMAT_YVU420:
3849 case DRM_FORMAT_YUV422:
3850 case DRM_FORMAT_YVU422:
3851 case DRM_FORMAT_YUV444:
3852 case DRM_FORMAT_YVU444:
3855 drm_fb_get_bpp_depth(format, &depth, &bpp);
3859 EXPORT_SYMBOL(drm_format_plane_cpp);
3862 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3863 * @format: pixel format (DRM_FORMAT_*)
3866 * The horizontal chroma subsampling factor for the
3867 * specified pixel format.
3869 int drm_format_horz_chroma_subsampling(uint32_t format)
3872 case DRM_FORMAT_YUV411:
3873 case DRM_FORMAT_YVU411:
3874 case DRM_FORMAT_YUV410:
3875 case DRM_FORMAT_YVU410:
3877 case DRM_FORMAT_YUYV:
3878 case DRM_FORMAT_YVYU:
3879 case DRM_FORMAT_UYVY:
3880 case DRM_FORMAT_VYUY:
3881 case DRM_FORMAT_NV12:
3882 case DRM_FORMAT_NV21:
3883 case DRM_FORMAT_NV16:
3884 case DRM_FORMAT_NV61:
3885 case DRM_FORMAT_YUV422:
3886 case DRM_FORMAT_YVU422:
3887 case DRM_FORMAT_YUV420:
3888 case DRM_FORMAT_YVU420:
3894 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3897 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3898 * @format: pixel format (DRM_FORMAT_*)
3901 * The vertical chroma subsampling factor for the
3902 * specified pixel format.
3904 int drm_format_vert_chroma_subsampling(uint32_t format)
3907 case DRM_FORMAT_YUV410:
3908 case DRM_FORMAT_YVU410:
3910 case DRM_FORMAT_YUV420:
3911 case DRM_FORMAT_YVU420:
3912 case DRM_FORMAT_NV12:
3913 case DRM_FORMAT_NV21:
3919 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3922 * drm_mode_config_init - initialize DRM mode_configuration structure
3925 * Initialize @dev's mode_config structure, used for tracking the graphics
3926 * configuration of @dev.
3928 * Since this initializes the modeset locks, no locking is possible. Which is no
3929 * problem, since this should happen single threaded at init time. It is the
3930 * driver's problem to ensure this guarantee.
3933 void drm_mode_config_init(struct drm_device *dev)
3935 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
3936 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
3937 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
3938 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3939 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3940 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3941 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3942 INIT_LIST_HEAD(&dev->mode_config.property_list);
3943 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3944 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3945 idr_init(&dev->mode_config.crtc_idr);
3947 drm_modeset_lock_all(dev);
3948 drm_mode_create_standard_connector_properties(dev);
3949 drm_modeset_unlock_all(dev);
3951 /* Just to be sure */
3952 dev->mode_config.num_fb = 0;
3953 dev->mode_config.num_connector = 0;
3954 dev->mode_config.num_crtc = 0;
3955 dev->mode_config.num_encoder = 0;
3957 EXPORT_SYMBOL(drm_mode_config_init);
3960 * drm_mode_config_cleanup - free up DRM mode_config info
3963 * Free up all the connectors and CRTCs associated with this DRM device, then
3964 * free up the framebuffers and associated buffer objects.
3966 * Note that since this /should/ happen single-threaded at driver/device
3967 * teardown time, no locking is required. It's the driver's job to ensure that
3968 * this guarantee actually holds true.
3970 * FIXME: cleanup any dangling user buffer objects too
3972 void drm_mode_config_cleanup(struct drm_device *dev)
3974 struct drm_connector *connector, *ot;
3975 struct drm_crtc *crtc, *ct;
3976 struct drm_encoder *encoder, *enct;
3977 struct drm_framebuffer *fb, *fbt;
3978 struct drm_property *property, *pt;
3979 struct drm_property_blob *blob, *bt;
3980 struct drm_plane *plane, *plt;
3982 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3984 encoder->funcs->destroy(encoder);
3987 list_for_each_entry_safe(connector, ot,
3988 &dev->mode_config.connector_list, head) {
3989 connector->funcs->destroy(connector);
3992 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3994 drm_property_destroy(dev, property);
3997 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3999 drm_property_destroy_blob(dev, blob);
4003 * Single-threaded teardown context, so it's not required to grab the
4004 * fb_lock to protect against concurrent fb_list access. Contrary, it
4005 * would actually deadlock with the drm_framebuffer_cleanup function.
4007 * Also, if there are any framebuffers left, that's a driver leak now,
4008 * so politely WARN about this.
4010 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4011 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4012 drm_framebuffer_remove(fb);
4015 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4017 plane->funcs->destroy(plane);
4020 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4021 crtc->funcs->destroy(crtc);
4024 idr_destroy(&dev->mode_config.crtc_idr);
4026 EXPORT_SYMBOL(drm_mode_config_cleanup);