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;
82 /* 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" },
131 * Non-global properties, but "required" for certain connectors.
133 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
140 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
142 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
149 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
150 drm_dvi_i_subconnector_enum_list)
152 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
161 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
163 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
172 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
173 drm_tv_subconnector_enum_list)
175 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
176 { DRM_MODE_DIRTY_OFF, "Off" },
177 { DRM_MODE_DIRTY_ON, "On" },
178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
181 struct drm_conn_prop_enum_list {
188 * Connector and encoder types.
190 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
191 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
192 { DRM_MODE_CONNECTOR_VGA, "VGA" },
193 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
194 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
195 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
196 { DRM_MODE_CONNECTOR_Composite, "Composite" },
197 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
198 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
199 { DRM_MODE_CONNECTOR_Component, "Component" },
200 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
201 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
202 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
203 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
204 { DRM_MODE_CONNECTOR_TV, "TV" },
205 { DRM_MODE_CONNECTOR_eDP, "eDP" },
206 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
207 { DRM_MODE_CONNECTOR_DSI, "DSI" },
210 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
211 { { DRM_MODE_ENCODER_NONE, "None" },
212 { DRM_MODE_ENCODER_DAC, "DAC" },
213 { DRM_MODE_ENCODER_TMDS, "TMDS" },
214 { DRM_MODE_ENCODER_LVDS, "LVDS" },
215 { DRM_MODE_ENCODER_TVDAC, "TV" },
216 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
217 { DRM_MODE_ENCODER_DSI, "DSI" },
220 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
224 ksnprintf(buf, 32, "%s-%d",
225 drm_encoder_enum_list[encoder->encoder_type].name,
229 EXPORT_SYMBOL(drm_get_encoder_name);
231 const char *drm_get_connector_name(const struct drm_connector *connector)
235 ksnprintf(buf, 32, "%s-%d",
236 drm_connector_enum_list[connector->connector_type].name,
237 connector->connector_type_id);
240 EXPORT_SYMBOL(drm_get_connector_name);
242 const char *drm_get_connector_status_name(enum drm_connector_status status)
244 if (status == connector_status_connected)
246 else if (status == connector_status_disconnected)
247 return "disconnected";
251 EXPORT_SYMBOL(drm_get_connector_status_name);
253 static char printable_char(int c)
255 return isascii(c) && isprint(c) ? c : '?';
258 const char *drm_get_format_name(uint32_t format)
262 ksnprintf(buf, sizeof(buf),
263 "%c%c%c%c %s-endian (0x%08x)",
264 printable_char(format & 0xff),
265 printable_char((format >> 8) & 0xff),
266 printable_char((format >> 16) & 0xff),
267 printable_char((format >> 24) & 0x7f),
268 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
273 EXPORT_SYMBOL(drm_get_format_name);
276 * drm_mode_object_get - allocate a new modeset identifier
278 * @obj: object pointer, used to generate unique ID
279 * @obj_type: object type
281 * Create a unique identifier based on @ptr in @dev's identifier space. Used
282 * for tracking modes, CRTCs and connectors.
285 * New unique (relative to other objects in @dev) integer identifier for the
288 static int drm_mode_object_get(struct drm_device *dev,
289 struct drm_mode_object *obj, uint32_t obj_type)
293 mutex_lock(&dev->mode_config.idr_mutex);
294 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
297 * Set up the object linking under the protection of the idr
298 * lock so that other users can't see inconsistent state.
301 obj->type = obj_type;
303 mutex_unlock(&dev->mode_config.idr_mutex);
305 return ret < 0 ? ret : 0;
309 * drm_mode_object_put - free a modeset identifer
311 * @object: object to free
313 * Free @id from @dev's unique identifier pool.
315 static void drm_mode_object_put(struct drm_device *dev,
316 struct drm_mode_object *object)
318 mutex_lock(&dev->mode_config.idr_mutex);
319 idr_remove(&dev->mode_config.crtc_idr, object->id);
320 mutex_unlock(&dev->mode_config.idr_mutex);
324 * drm_mode_object_find - look up a drm object with static lifetime
326 * @id: id of the mode object
327 * @type: type of the mode object
329 * Note that framebuffers cannot be looked up with this functions - since those
330 * are reference counted, they need special treatment.
332 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
333 uint32_t id, uint32_t type)
335 struct drm_mode_object *obj = NULL;
337 /* Framebuffers are reference counted and need their own lookup
339 WARN_ON(type == DRM_MODE_OBJECT_FB);
341 mutex_lock(&dev->mode_config.idr_mutex);
342 obj = idr_find(&dev->mode_config.crtc_idr, id);
343 if (!obj || (obj->type != type) || (obj->id != id))
345 mutex_unlock(&dev->mode_config.idr_mutex);
349 EXPORT_SYMBOL(drm_mode_object_find);
352 * drm_framebuffer_init - initialize a framebuffer
354 * @fb: framebuffer to be initialized
355 * @funcs: ... with these functions
357 * Allocates an ID for the framebuffer's parent mode object, sets its mode
358 * functions & device file and adds it to the master fd list.
361 * This functions publishes the fb and makes it available for concurrent access
362 * by other users. Which means by this point the fb _must_ be fully set up -
363 * since all the fb attributes are invariant over its lifetime, no further
364 * locking but only correct reference counting is required.
367 * Zero on success, error code on failure.
369 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
370 const struct drm_framebuffer_funcs *funcs)
374 mutex_lock(&dev->mode_config.fb_lock);
375 kref_init(&fb->refcount);
376 INIT_LIST_HEAD(&fb->filp_head);
380 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
384 /* Grab the idr reference. */
385 drm_framebuffer_reference(fb);
387 dev->mode_config.num_fb++;
388 list_add(&fb->head, &dev->mode_config.fb_list);
390 mutex_unlock(&dev->mode_config.fb_lock);
394 EXPORT_SYMBOL(drm_framebuffer_init);
396 static void drm_framebuffer_free(struct kref *kref)
398 struct drm_framebuffer *fb =
399 container_of(kref, struct drm_framebuffer, refcount);
400 fb->funcs->destroy(fb);
403 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
406 struct drm_mode_object *obj = NULL;
407 struct drm_framebuffer *fb;
409 mutex_lock(&dev->mode_config.idr_mutex);
410 obj = idr_find(&dev->mode_config.crtc_idr, id);
411 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
415 mutex_unlock(&dev->mode_config.idr_mutex);
421 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
423 * @id: id of the fb object
425 * If successful, this grabs an additional reference to the framebuffer -
426 * callers need to make sure to eventually unreference the returned framebuffer
429 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
432 struct drm_framebuffer *fb;
434 mutex_lock(&dev->mode_config.fb_lock);
435 fb = __drm_framebuffer_lookup(dev, id);
437 drm_framebuffer_reference(fb);
438 mutex_unlock(&dev->mode_config.fb_lock);
442 EXPORT_SYMBOL(drm_framebuffer_lookup);
445 * drm_framebuffer_unreference - unref a framebuffer
446 * @fb: framebuffer to unref
448 * This functions decrements the fb's refcount and frees it if it drops to zero.
450 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
452 DRM_DEBUG("FB ID: %d\n", fb->base.id);
453 kref_put(&fb->refcount, drm_framebuffer_free);
455 EXPORT_SYMBOL(drm_framebuffer_unreference);
458 * drm_framebuffer_reference - incr the fb refcnt
461 void drm_framebuffer_reference(struct drm_framebuffer *fb)
463 DRM_DEBUG("FB ID: %d\n", fb->base.id);
464 kref_get(&fb->refcount);
466 EXPORT_SYMBOL(drm_framebuffer_reference);
468 static void drm_framebuffer_free_bug(struct kref *kref)
473 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
475 DRM_DEBUG("FB ID: %d\n", fb->base.id);
476 kref_put(&fb->refcount, drm_framebuffer_free_bug);
479 /* dev->mode_config.fb_lock must be held! */
480 static void __drm_framebuffer_unregister(struct drm_device *dev,
481 struct drm_framebuffer *fb)
483 mutex_lock(&dev->mode_config.idr_mutex);
484 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
485 mutex_unlock(&dev->mode_config.idr_mutex);
489 __drm_framebuffer_unreference(fb);
493 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
494 * @fb: fb to unregister
496 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
497 * those used for fbdev. Note that the caller must hold a reference of it's own,
498 * i.e. the object may not be destroyed through this call (since it'll lead to a
499 * locking inversion).
501 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
503 struct drm_device *dev = fb->dev;
505 mutex_lock(&dev->mode_config.fb_lock);
506 /* Mark fb as reaped and drop idr ref. */
507 __drm_framebuffer_unregister(dev, fb);
508 mutex_unlock(&dev->mode_config.fb_lock);
510 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
513 * drm_framebuffer_cleanup - remove a framebuffer object
514 * @fb: framebuffer to remove
516 * Cleanup references to a user-created framebuffer. This function is intended
517 * to be used from the drivers ->destroy callback.
519 * Note that this function does not remove the fb from active usuage - if it is
520 * still used anywhere, hilarity can ensue since userspace could call getfb on
521 * the id and get back -EINVAL. Obviously no concern at driver unload time.
523 * Also, the framebuffer will not be removed from the lookup idr - for
524 * user-created framebuffers this will happen in in the rmfb ioctl. For
525 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
526 * drm_framebuffer_unregister_private.
528 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
530 struct drm_device *dev = fb->dev;
532 mutex_lock(&dev->mode_config.fb_lock);
534 dev->mode_config.num_fb--;
535 mutex_unlock(&dev->mode_config.fb_lock);
537 EXPORT_SYMBOL(drm_framebuffer_cleanup);
540 * drm_framebuffer_remove - remove and unreference a framebuffer object
541 * @fb: framebuffer to remove
543 * Scans all the CRTCs and planes in @dev's mode_config. If they're
544 * using @fb, removes it, setting it to NULL. Then drops the reference to the
545 * passed-in framebuffer. Might take the modeset locks.
547 * Note that this function optimizes the cleanup away if the caller holds the
548 * last reference to the framebuffer. It is also guaranteed to not take the
549 * modeset locks in this case.
551 void drm_framebuffer_remove(struct drm_framebuffer *fb)
553 struct drm_device *dev = fb->dev;
554 struct drm_crtc *crtc;
555 struct drm_plane *plane;
556 struct drm_mode_set set;
559 WARN_ON(!list_empty(&fb->filp_head));
562 * drm ABI mandates that we remove any deleted framebuffers from active
563 * useage. But since most sane clients only remove framebuffers they no
564 * longer need, try to optimize this away.
566 * Since we're holding a reference ourselves, observing a refcount of 1
567 * means that we're the last holder and can skip it. Also, the refcount
568 * can never increase from 1 again, so we don't need any barriers or
571 * Note that userspace could try to race with use and instate a new
572 * usage _after_ we've cleared all current ones. End result will be an
573 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
576 if (atomic_read(&fb->refcount.refcount) > 1) {
577 drm_modeset_lock_all(dev);
578 /* remove from any CRTC */
579 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
580 if (crtc->fb == fb) {
581 /* should turn off the crtc */
582 memset(&set, 0, sizeof(struct drm_mode_set));
585 ret = drm_mode_set_config_internal(&set);
587 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
591 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
593 drm_plane_force_disable(plane);
595 drm_modeset_unlock_all(dev);
598 drm_framebuffer_unreference(fb);
600 EXPORT_SYMBOL(drm_framebuffer_remove);
603 * drm_crtc_init - Initialise a new CRTC object
605 * @crtc: CRTC object to init
606 * @funcs: callbacks for the new CRTC
608 * Inits a new object created as base part of a driver crtc object.
611 * Zero on success, error code on failure.
613 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
614 const struct drm_crtc_funcs *funcs)
620 crtc->invert_dimensions = false;
622 drm_modeset_lock_all(dev);
623 lockinit(&crtc->mutex, "drmcm", 0, LK_CANRECURSE);
624 lockmgr(&crtc->mutex, LK_EXCLUSIVE);
626 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
630 crtc->base.properties = &crtc->properties;
632 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
633 dev->mode_config.num_crtc++;
636 drm_modeset_unlock_all(dev);
640 EXPORT_SYMBOL(drm_crtc_init);
643 * drm_crtc_cleanup - Clean up the core crtc usage
644 * @crtc: CRTC to cleanup
646 * This function cleans up @crtc and removes it from the DRM mode setting
647 * core. Note that the function does *not* free the crtc structure itself,
648 * this is the responsibility of the caller.
650 void drm_crtc_cleanup(struct drm_crtc *crtc)
652 struct drm_device *dev = crtc->dev;
654 kfree(crtc->gamma_store);
655 crtc->gamma_store = NULL;
657 drm_mode_object_put(dev, &crtc->base);
658 list_del(&crtc->head);
659 dev->mode_config.num_crtc--;
661 EXPORT_SYMBOL(drm_crtc_cleanup);
664 * drm_crtc_index - find the index of a registered CRTC
665 * @crtc: CRTC to find index for
667 * Given a registered CRTC, return the index of that CRTC within a DRM
668 * device's list of CRTCs.
670 unsigned int drm_crtc_index(struct drm_crtc *crtc)
672 unsigned int index = 0;
673 struct drm_crtc *tmp;
675 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
684 EXPORT_SYMBOL(drm_crtc_index);
687 * drm_mode_probed_add - add a mode to a connector's probed mode list
688 * @connector: connector the new mode
691 * Add @mode to @connector's mode list for later use.
693 void drm_mode_probed_add(struct drm_connector *connector,
694 struct drm_display_mode *mode)
696 list_add_tail(&mode->head, &connector->probed_modes);
698 EXPORT_SYMBOL(drm_mode_probed_add);
701 * drm_mode_remove - remove and free a mode
702 * @connector: connector list to modify
703 * @mode: mode to remove
705 * Remove @mode from @connector's mode list, then free it.
707 static void drm_mode_remove(struct drm_connector *connector,
708 struct drm_display_mode *mode)
710 list_del(&mode->head);
711 drm_mode_destroy(connector->dev, mode);
715 * drm_connector_init - Init a preallocated connector
717 * @connector: the connector to init
718 * @funcs: callbacks for this connector
719 * @connector_type: user visible type of the connector
721 * Initialises a preallocated connector. Connectors should be
722 * subclassed as part of driver connector objects.
725 * Zero on success, error code on failure.
727 int drm_connector_init(struct drm_device *dev,
728 struct drm_connector *connector,
729 const struct drm_connector_funcs *funcs,
734 drm_modeset_lock_all(dev);
736 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
740 connector->base.properties = &connector->properties;
741 connector->dev = dev;
742 connector->funcs = funcs;
743 connector->connector_type = connector_type;
744 connector->connector_type_id =
745 ++drm_connector_enum_list[connector_type].count; /* TODO */
746 if (connector->connector_type_id < 0) {
747 ret = connector->connector_type_id;
748 drm_mode_object_put(dev, &connector->base);
751 INIT_LIST_HEAD(&connector->probed_modes);
752 INIT_LIST_HEAD(&connector->modes);
753 connector->edid_blob_ptr = NULL;
754 connector->status = connector_status_unknown;
756 list_add_tail(&connector->head, &dev->mode_config.connector_list);
757 dev->mode_config.num_connector++;
759 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
760 drm_object_attach_property(&connector->base,
761 dev->mode_config.edid_property,
764 drm_object_attach_property(&connector->base,
765 dev->mode_config.dpms_property, 0);
768 drm_modeset_unlock_all(dev);
772 EXPORT_SYMBOL(drm_connector_init);
775 * drm_connector_cleanup - cleans up an initialised connector
776 * @connector: connector to cleanup
778 * Cleans up the connector but doesn't free the object.
780 void drm_connector_cleanup(struct drm_connector *connector)
782 struct drm_device *dev = connector->dev;
783 struct drm_display_mode *mode, *t;
785 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
786 drm_mode_remove(connector, mode);
788 list_for_each_entry_safe(mode, t, &connector->modes, head)
789 drm_mode_remove(connector, mode);
791 drm_mode_object_put(dev, &connector->base);
792 list_del(&connector->head);
793 dev->mode_config.num_connector--;
795 EXPORT_SYMBOL(drm_connector_cleanup);
797 void drm_connector_unplug_all(struct drm_device *dev)
800 struct drm_connector *connector;
802 /* taking the mode config mutex ends up in a clash with sysfs */
803 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
804 drm_sysfs_connector_remove(connector);
808 EXPORT_SYMBOL(drm_connector_unplug_all);
810 int drm_encoder_init(struct drm_device *dev,
811 struct drm_encoder *encoder,
812 const struct drm_encoder_funcs *funcs,
817 drm_modeset_lock_all(dev);
819 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
824 encoder->encoder_type = encoder_type;
825 encoder->funcs = funcs;
827 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
828 dev->mode_config.num_encoder++;
831 drm_modeset_unlock_all(dev);
835 EXPORT_SYMBOL(drm_encoder_init);
837 void drm_encoder_cleanup(struct drm_encoder *encoder)
839 struct drm_device *dev = encoder->dev;
840 drm_modeset_lock_all(dev);
841 drm_mode_object_put(dev, &encoder->base);
842 list_del(&encoder->head);
843 dev->mode_config.num_encoder--;
844 drm_modeset_unlock_all(dev);
846 EXPORT_SYMBOL(drm_encoder_cleanup);
849 * drm_plane_init - Initialise a new plane object
851 * @plane: plane object to init
852 * @possible_crtcs: bitmask of possible CRTCs
853 * @funcs: callbacks for the new plane
854 * @formats: array of supported formats (%DRM_FORMAT_*)
855 * @format_count: number of elements in @formats
856 * @priv: plane is private (hidden from userspace)?
858 * Inits a new object created as base part of a driver plane object.
861 * Zero on success, error code on failure.
863 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
864 unsigned long possible_crtcs,
865 const struct drm_plane_funcs *funcs,
866 const uint32_t *formats, uint32_t format_count,
871 drm_modeset_lock_all(dev);
873 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
877 plane->base.properties = &plane->properties;
879 plane->funcs = funcs;
880 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
882 if (!plane->format_types) {
883 DRM_DEBUG_KMS("out of memory when allocating plane\n");
884 drm_mode_object_put(dev, &plane->base);
889 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
890 plane->format_count = format_count;
891 plane->possible_crtcs = possible_crtcs;
893 /* private planes are not exposed to userspace, but depending on
894 * display hardware, might be convenient to allow sharing programming
895 * for the scanout engine with the crtc implementation.
898 list_add_tail(&plane->head, &dev->mode_config.plane_list);
899 dev->mode_config.num_plane++;
901 INIT_LIST_HEAD(&plane->head);
905 drm_modeset_unlock_all(dev);
909 EXPORT_SYMBOL(drm_plane_init);
912 * drm_plane_cleanup - Clean up the core plane usage
913 * @plane: plane to cleanup
915 * This function cleans up @plane and removes it from the DRM mode setting
916 * core. Note that the function does *not* free the plane structure itself,
917 * this is the responsibility of the caller.
919 void drm_plane_cleanup(struct drm_plane *plane)
921 struct drm_device *dev = plane->dev;
923 drm_modeset_lock_all(dev);
924 kfree(plane->format_types);
925 drm_mode_object_put(dev, &plane->base);
926 /* if not added to a list, it must be a private plane */
927 if (!list_empty(&plane->head)) {
928 list_del(&plane->head);
929 dev->mode_config.num_plane--;
931 drm_modeset_unlock_all(dev);
933 EXPORT_SYMBOL(drm_plane_cleanup);
936 * drm_plane_force_disable - Forcibly disable a plane
937 * @plane: plane to disable
939 * Forces the plane to be disabled.
941 * Used when the plane's current framebuffer is destroyed,
942 * and when restoring fbdev mode.
944 void drm_plane_force_disable(struct drm_plane *plane)
951 ret = plane->funcs->disable_plane(plane);
953 DRM_ERROR("failed to disable plane with busy fb\n");
954 /* disconnect the plane from the fb and crtc: */
955 __drm_framebuffer_unreference(plane->fb);
959 EXPORT_SYMBOL(drm_plane_force_disable);
962 * drm_mode_create - create a new display mode
965 * Create a new drm_display_mode, give it an ID, and return it.
968 * Pointer to new mode on success, NULL on error.
970 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
972 struct drm_display_mode *nmode;
974 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
978 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
985 EXPORT_SYMBOL(drm_mode_create);
988 * drm_mode_destroy - remove a mode
990 * @mode: mode to remove
992 * Free @mode's unique identifier, then free it.
994 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
999 drm_mode_object_put(dev, &mode->base);
1003 EXPORT_SYMBOL(drm_mode_destroy);
1005 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1007 struct drm_property *edid;
1008 struct drm_property *dpms;
1011 * Standard properties (apply to all connectors)
1013 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1014 DRM_MODE_PROP_IMMUTABLE,
1016 dev->mode_config.edid_property = edid;
1018 dpms = drm_property_create_enum(dev, 0,
1019 "DPMS", drm_dpms_enum_list,
1020 ARRAY_SIZE(drm_dpms_enum_list));
1021 dev->mode_config.dpms_property = dpms;
1027 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1030 * Called by a driver the first time a DVI-I connector is made.
1032 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1034 struct drm_property *dvi_i_selector;
1035 struct drm_property *dvi_i_subconnector;
1037 if (dev->mode_config.dvi_i_select_subconnector_property)
1041 drm_property_create_enum(dev, 0,
1042 "select subconnector",
1043 drm_dvi_i_select_enum_list,
1044 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1045 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1047 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1049 drm_dvi_i_subconnector_enum_list,
1050 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1051 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1055 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1058 * drm_create_tv_properties - create TV specific connector properties
1060 * @num_modes: number of different TV formats (modes) supported
1061 * @modes: array of pointers to strings containing name of each format
1063 * Called by a driver's TV initialization routine, this function creates
1064 * the TV specific connector properties for a given device. Caller is
1065 * responsible for allocating a list of format names and passing them to
1068 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1071 struct drm_property *tv_selector;
1072 struct drm_property *tv_subconnector;
1075 if (dev->mode_config.tv_select_subconnector_property)
1079 * Basic connector properties
1081 tv_selector = drm_property_create_enum(dev, 0,
1082 "select subconnector",
1083 drm_tv_select_enum_list,
1084 ARRAY_SIZE(drm_tv_select_enum_list));
1085 dev->mode_config.tv_select_subconnector_property = tv_selector;
1088 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1090 drm_tv_subconnector_enum_list,
1091 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1092 dev->mode_config.tv_subconnector_property = tv_subconnector;
1095 * Other, TV specific properties: margins & TV modes.
1097 dev->mode_config.tv_left_margin_property =
1098 drm_property_create_range(dev, 0, "left margin", 0, 100);
1100 dev->mode_config.tv_right_margin_property =
1101 drm_property_create_range(dev, 0, "right margin", 0, 100);
1103 dev->mode_config.tv_top_margin_property =
1104 drm_property_create_range(dev, 0, "top margin", 0, 100);
1106 dev->mode_config.tv_bottom_margin_property =
1107 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1109 dev->mode_config.tv_mode_property =
1110 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1112 for (i = 0; i < num_modes; i++)
1113 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1116 dev->mode_config.tv_brightness_property =
1117 drm_property_create_range(dev, 0, "brightness", 0, 100);
1119 dev->mode_config.tv_contrast_property =
1120 drm_property_create_range(dev, 0, "contrast", 0, 100);
1122 dev->mode_config.tv_flicker_reduction_property =
1123 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1125 dev->mode_config.tv_overscan_property =
1126 drm_property_create_range(dev, 0, "overscan", 0, 100);
1128 dev->mode_config.tv_saturation_property =
1129 drm_property_create_range(dev, 0, "saturation", 0, 100);
1131 dev->mode_config.tv_hue_property =
1132 drm_property_create_range(dev, 0, "hue", 0, 100);
1136 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1139 * drm_mode_create_scaling_mode_property - create scaling mode property
1142 * Called by a driver the first time it's needed, must be attached to desired
1145 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1147 struct drm_property *scaling_mode;
1149 if (dev->mode_config.scaling_mode_property)
1153 drm_property_create_enum(dev, 0, "scaling mode",
1154 drm_scaling_mode_enum_list,
1155 ARRAY_SIZE(drm_scaling_mode_enum_list));
1157 dev->mode_config.scaling_mode_property = scaling_mode;
1161 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1164 * drm_mode_create_dirty_property - create dirty property
1167 * Called by a driver the first time it's needed, must be attached to desired
1170 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1172 struct drm_property *dirty_info;
1174 if (dev->mode_config.dirty_info_property)
1178 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1180 drm_dirty_info_enum_list,
1181 ARRAY_SIZE(drm_dirty_info_enum_list));
1182 dev->mode_config.dirty_info_property = dirty_info;
1186 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1188 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1190 uint32_t total_objects = 0;
1192 total_objects += dev->mode_config.num_crtc;
1193 total_objects += dev->mode_config.num_connector;
1194 total_objects += dev->mode_config.num_encoder;
1196 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1197 if (!group->id_list)
1200 group->num_crtcs = 0;
1201 group->num_connectors = 0;
1202 group->num_encoders = 0;
1206 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1207 struct drm_mode_group *group)
1209 struct drm_crtc *crtc;
1210 struct drm_encoder *encoder;
1211 struct drm_connector *connector;
1214 if ((ret = drm_mode_group_init(dev, group)))
1217 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1218 group->id_list[group->num_crtcs++] = crtc->base.id;
1220 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1221 group->id_list[group->num_crtcs + group->num_encoders++] =
1224 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1225 group->id_list[group->num_crtcs + group->num_encoders +
1226 group->num_connectors++] = connector->base.id;
1230 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1233 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1234 * @out: drm_mode_modeinfo struct to return to the user
1235 * @in: drm_display_mode to use
1237 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1240 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1241 const struct drm_display_mode *in)
1243 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1244 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1245 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1246 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1247 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1248 "timing values too large for mode info\n");
1250 out->clock = in->clock;
1251 out->hdisplay = in->hdisplay;
1252 out->hsync_start = in->hsync_start;
1253 out->hsync_end = in->hsync_end;
1254 out->htotal = in->htotal;
1255 out->hskew = in->hskew;
1256 out->vdisplay = in->vdisplay;
1257 out->vsync_start = in->vsync_start;
1258 out->vsync_end = in->vsync_end;
1259 out->vtotal = in->vtotal;
1260 out->vscan = in->vscan;
1261 out->vrefresh = in->vrefresh;
1262 out->flags = in->flags;
1263 out->type = in->type;
1264 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1265 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1269 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1270 * @out: drm_display_mode to return to the user
1271 * @in: drm_mode_modeinfo to use
1273 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1277 * Zero on success, errno on failure.
1279 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1280 const struct drm_mode_modeinfo *in)
1282 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1285 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1288 out->clock = in->clock;
1289 out->hdisplay = in->hdisplay;
1290 out->hsync_start = in->hsync_start;
1291 out->hsync_end = in->hsync_end;
1292 out->htotal = in->htotal;
1293 out->hskew = in->hskew;
1294 out->vdisplay = in->vdisplay;
1295 out->vsync_start = in->vsync_start;
1296 out->vsync_end = in->vsync_end;
1297 out->vtotal = in->vtotal;
1298 out->vscan = in->vscan;
1299 out->vrefresh = in->vrefresh;
1300 out->flags = in->flags;
1301 out->type = in->type;
1302 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1303 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1309 * drm_mode_getresources - get graphics configuration
1310 * @dev: drm device for the ioctl
1311 * @data: data pointer for the ioctl
1312 * @file_priv: drm file for the ioctl call
1314 * Construct a set of configuration description structures and return
1315 * them to the user, including CRTC, connector and framebuffer configuration.
1317 * Called by the user via ioctl.
1320 * Zero on success, errno on failure.
1322 int drm_mode_getresources(struct drm_device *dev, void *data,
1323 struct drm_file *file_priv)
1325 struct drm_mode_card_res *card_res = data;
1326 struct list_head *lh;
1327 struct drm_framebuffer *fb;
1328 struct drm_connector *connector;
1329 struct drm_crtc *crtc;
1330 struct drm_encoder *encoder;
1332 int connector_count = 0;
1335 int encoder_count = 0;
1337 uint32_t __user *fb_id;
1338 uint32_t __user *crtc_id;
1339 uint32_t __user *connector_id;
1340 uint32_t __user *encoder_id;
1341 struct drm_mode_group *mode_group;
1343 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1347 mutex_lock(&file_priv->fbs_lock);
1349 * For the non-control nodes we need to limit the list of resources
1350 * by IDs in the group list for this node
1352 list_for_each(lh, &file_priv->fbs)
1355 /* handle this in 4 parts */
1357 if (card_res->count_fbs >= fb_count) {
1359 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1360 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1361 if (put_user(fb->base.id, fb_id + copied)) {
1362 mutex_unlock(&file_priv->fbs_lock);
1368 card_res->count_fbs = fb_count;
1369 mutex_unlock(&file_priv->fbs_lock);
1371 drm_modeset_lock_all(dev);
1373 mode_group = &file_priv->master->minor->mode_group;
1374 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1376 mode_group = NULL; /* XXXKIB */
1377 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;
1403 if (1 || file_priv->master) {
1404 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1406 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1407 if (put_user(crtc->base.id, crtc_id + copied)) {
1414 for (i = 0; i < mode_group->num_crtcs; i++) {
1415 if (put_user(mode_group->id_list[i],
1416 crtc_id + copied)) {
1424 card_res->count_crtcs = crtc_count;
1427 if (card_res->count_encoders >= encoder_count) {
1429 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1430 if (file_priv->master) {
1431 list_for_each_entry(encoder,
1432 &dev->mode_config.encoder_list,
1434 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1435 drm_get_encoder_name(encoder));
1436 if (put_user(encoder->base.id, encoder_id +
1444 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1445 if (put_user(mode_group->id_list[i],
1446 encoder_id + copied)) {
1455 card_res->count_encoders = encoder_count;
1458 if (card_res->count_connectors >= connector_count) {
1460 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1461 if (file_priv->master) {
1462 list_for_each_entry(connector,
1463 &dev->mode_config.connector_list,
1465 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1467 drm_get_connector_name(connector));
1468 if (put_user(connector->base.id,
1469 connector_id + copied)) {
1476 int start = mode_group->num_crtcs +
1477 mode_group->num_encoders;
1478 for (i = start; i < start + mode_group->num_connectors; i++) {
1479 if (put_user(mode_group->id_list[i],
1480 connector_id + copied)) {
1488 card_res->count_connectors = connector_count;
1490 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1491 card_res->count_connectors, card_res->count_encoders);
1494 drm_modeset_unlock_all(dev);
1499 * drm_mode_getcrtc - get CRTC configuration
1500 * @dev: drm device for the ioctl
1501 * @data: data pointer for the ioctl
1502 * @file_priv: drm file for the ioctl call
1504 * Construct a CRTC configuration structure to return to the user.
1506 * Called by the user via ioctl.
1509 * Zero on success, errno on failure.
1511 int drm_mode_getcrtc(struct drm_device *dev,
1512 void *data, struct drm_file *file_priv)
1514 struct drm_mode_crtc *crtc_resp = data;
1515 struct drm_crtc *crtc;
1516 struct drm_mode_object *obj;
1519 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1522 drm_modeset_lock_all(dev);
1524 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1525 DRM_MODE_OBJECT_CRTC);
1530 crtc = obj_to_crtc(obj);
1532 crtc_resp->x = crtc->x;
1533 crtc_resp->y = crtc->y;
1534 crtc_resp->gamma_size = crtc->gamma_size;
1536 crtc_resp->fb_id = crtc->fb->base.id;
1538 crtc_resp->fb_id = 0;
1540 if (crtc->enabled) {
1542 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1543 crtc_resp->mode_valid = 1;
1546 crtc_resp->mode_valid = 0;
1550 drm_modeset_unlock_all(dev);
1554 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1555 const struct drm_file *file_priv)
1558 * If user-space hasn't configured the driver to expose the stereo 3D
1559 * modes, don't expose them.
1561 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1568 * drm_mode_getconnector - get connector configuration
1569 * @dev: drm device for the ioctl
1570 * @data: data pointer for the ioctl
1571 * @file_priv: drm file for the ioctl call
1573 * Construct a connector configuration structure to return to the user.
1575 * Called by the user via ioctl.
1578 * Zero on success, errno on failure.
1580 int drm_mode_getconnector(struct drm_device *dev, void *data,
1581 struct drm_file *file_priv)
1583 struct drm_mode_get_connector *out_resp = data;
1584 struct drm_mode_object *obj;
1585 struct drm_connector *connector;
1586 struct drm_display_mode *mode;
1588 int props_count = 0;
1589 int encoders_count = 0;
1593 struct drm_mode_modeinfo u_mode;
1594 struct drm_mode_modeinfo __user *mode_ptr;
1595 uint32_t __user *prop_ptr;
1596 uint64_t __user *prop_values;
1597 uint32_t __user *encoder_ptr;
1599 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1602 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1604 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1606 mutex_lock(&dev->mode_config.mutex);
1608 obj = drm_mode_object_find(dev, out_resp->connector_id,
1609 DRM_MODE_OBJECT_CONNECTOR);
1614 connector = obj_to_connector(obj);
1616 props_count = connector->properties.count;
1618 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1619 if (connector->encoder_ids[i] != 0) {
1624 if (out_resp->count_modes == 0) {
1625 connector->funcs->fill_modes(connector,
1626 dev->mode_config.max_width,
1627 dev->mode_config.max_height);
1630 /* delayed so we get modes regardless of pre-fill_modes state */
1631 list_for_each_entry(mode, &connector->modes, head)
1632 if (drm_mode_expose_to_userspace(mode, file_priv))
1635 out_resp->connector_id = connector->base.id;
1636 out_resp->connector_type = connector->connector_type;
1637 out_resp->connector_type_id = connector->connector_type_id;
1638 out_resp->mm_width = connector->display_info.width_mm;
1639 out_resp->mm_height = connector->display_info.height_mm;
1640 out_resp->subpixel = connector->display_info.subpixel_order;
1641 out_resp->connection = connector->status;
1642 if (connector->encoder)
1643 out_resp->encoder_id = connector->encoder->base.id;
1645 out_resp->encoder_id = 0;
1648 * This ioctl is called twice, once to determine how much space is
1649 * needed, and the 2nd time to fill it.
1651 if ((out_resp->count_modes >= mode_count) && mode_count) {
1653 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1654 list_for_each_entry(mode, &connector->modes, head) {
1655 if (!drm_mode_expose_to_userspace(mode, file_priv))
1658 drm_crtc_convert_to_umode(&u_mode, mode);
1659 if (copy_to_user(mode_ptr + copied,
1660 &u_mode, sizeof(u_mode))) {
1667 out_resp->count_modes = mode_count;
1669 if ((out_resp->count_props >= props_count) && props_count) {
1671 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1672 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1673 for (i = 0; i < connector->properties.count; i++) {
1674 if (put_user(connector->properties.ids[i],
1675 prop_ptr + copied)) {
1680 if (put_user(connector->properties.values[i],
1681 prop_values + copied)) {
1688 out_resp->count_props = props_count;
1690 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1692 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1693 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1694 if (connector->encoder_ids[i] != 0) {
1695 if (put_user(connector->encoder_ids[i],
1696 encoder_ptr + copied)) {
1704 out_resp->count_encoders = encoders_count;
1707 mutex_unlock(&dev->mode_config.mutex);
1712 int drm_mode_getencoder(struct drm_device *dev, void *data,
1713 struct drm_file *file_priv)
1715 struct drm_mode_get_encoder *enc_resp = data;
1716 struct drm_mode_object *obj;
1717 struct drm_encoder *encoder;
1720 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1723 drm_modeset_lock_all(dev);
1724 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1725 DRM_MODE_OBJECT_ENCODER);
1730 encoder = obj_to_encoder(obj);
1733 enc_resp->crtc_id = encoder->crtc->base.id;
1735 enc_resp->crtc_id = 0;
1736 enc_resp->encoder_type = encoder->encoder_type;
1737 enc_resp->encoder_id = encoder->base.id;
1738 enc_resp->possible_crtcs = encoder->possible_crtcs;
1739 enc_resp->possible_clones = encoder->possible_clones;
1742 drm_modeset_unlock_all(dev);
1747 * drm_mode_getplane_res - get plane info
1750 * @file_priv: DRM file info
1752 * Return an plane count and set of IDs.
1754 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1755 struct drm_file *file_priv)
1757 struct drm_mode_get_plane_res *plane_resp = data;
1758 struct drm_mode_config *config;
1759 struct drm_plane *plane;
1760 uint32_t __user *plane_ptr;
1761 int copied = 0, ret = 0;
1763 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1766 drm_modeset_lock_all(dev);
1767 config = &dev->mode_config;
1770 * This ioctl is called twice, once to determine how much space is
1771 * needed, and the 2nd time to fill it.
1773 if (config->num_plane &&
1774 (plane_resp->count_planes >= config->num_plane)) {
1775 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1777 list_for_each_entry(plane, &config->plane_list, head) {
1778 if (put_user(plane->base.id, plane_ptr + copied)) {
1785 plane_resp->count_planes = config->num_plane;
1788 drm_modeset_unlock_all(dev);
1793 * drm_mode_getplane - get plane info
1796 * @file_priv: DRM file info
1798 * Return plane info, including formats supported, gamma size, any
1801 int drm_mode_getplane(struct drm_device *dev, void *data,
1802 struct drm_file *file_priv)
1804 struct drm_mode_get_plane *plane_resp = data;
1805 struct drm_mode_object *obj;
1806 struct drm_plane *plane;
1807 uint32_t __user *format_ptr;
1810 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1813 drm_modeset_lock_all(dev);
1814 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1815 DRM_MODE_OBJECT_PLANE);
1820 plane = obj_to_plane(obj);
1823 plane_resp->crtc_id = plane->crtc->base.id;
1825 plane_resp->crtc_id = 0;
1828 plane_resp->fb_id = plane->fb->base.id;
1830 plane_resp->fb_id = 0;
1832 plane_resp->plane_id = plane->base.id;
1833 plane_resp->possible_crtcs = plane->possible_crtcs;
1834 plane_resp->gamma_size = 0;
1837 * This ioctl is called twice, once to determine how much space is
1838 * needed, and the 2nd time to fill it.
1840 if (plane->format_count &&
1841 (plane_resp->count_format_types >= plane->format_count)) {
1842 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1843 if (copy_to_user(format_ptr,
1844 plane->format_types,
1845 sizeof(uint32_t) * plane->format_count)) {
1850 plane_resp->count_format_types = plane->format_count;
1853 drm_modeset_unlock_all(dev);
1858 * drm_mode_setplane - set up or tear down an plane
1860 * @data: ioctl data*
1861 * @file_priv: DRM file info
1863 * Set plane info, including placement, fb, scaling, and other factors.
1864 * Or pass a NULL fb to disable.
1866 int drm_mode_setplane(struct drm_device *dev, void *data,
1867 struct drm_file *file_priv)
1869 struct drm_mode_set_plane *plane_req = data;
1870 struct drm_mode_object *obj;
1871 struct drm_plane *plane;
1872 struct drm_crtc *crtc;
1873 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
1875 unsigned int fb_width, fb_height;
1878 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1882 * First, find the plane, crtc, and fb objects. If not available,
1883 * we don't bother to call the driver.
1885 obj = drm_mode_object_find(dev, plane_req->plane_id,
1886 DRM_MODE_OBJECT_PLANE);
1888 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1889 plane_req->plane_id);
1892 plane = obj_to_plane(obj);
1894 /* No fb means shut it down */
1895 if (!plane_req->fb_id) {
1896 drm_modeset_lock_all(dev);
1898 plane->funcs->disable_plane(plane);
1901 drm_modeset_unlock_all(dev);
1905 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1906 DRM_MODE_OBJECT_CRTC);
1908 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1909 plane_req->crtc_id);
1913 crtc = obj_to_crtc(obj);
1915 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1917 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1923 /* Check whether this plane supports the fb pixel format. */
1924 for (i = 0; i < plane->format_count; i++)
1925 if (fb->pixel_format == plane->format_types[i])
1927 if (i == plane->format_count) {
1928 DRM_DEBUG_KMS("Invalid pixel format %s\n",
1929 drm_get_format_name(fb->pixel_format));
1934 fb_width = fb->width << 16;
1935 fb_height = fb->height << 16;
1937 /* Make sure source coordinates are inside the fb. */
1938 if (plane_req->src_w > fb_width ||
1939 plane_req->src_x > fb_width - plane_req->src_w ||
1940 plane_req->src_h > fb_height ||
1941 plane_req->src_y > fb_height - plane_req->src_h) {
1942 DRM_DEBUG_KMS("Invalid source coordinates "
1943 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1944 plane_req->src_w >> 16,
1945 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1946 plane_req->src_h >> 16,
1947 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1948 plane_req->src_x >> 16,
1949 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1950 plane_req->src_y >> 16,
1951 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1956 /* Give drivers some help against integer overflows */
1957 if (plane_req->crtc_w > INT_MAX ||
1958 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1959 plane_req->crtc_h > INT_MAX ||
1960 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1961 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1962 plane_req->crtc_w, plane_req->crtc_h,
1963 plane_req->crtc_x, plane_req->crtc_y);
1968 drm_modeset_lock_all(dev);
1969 ret = plane->funcs->update_plane(plane, crtc, fb,
1970 plane_req->crtc_x, plane_req->crtc_y,
1971 plane_req->crtc_w, plane_req->crtc_h,
1972 plane_req->src_x, plane_req->src_y,
1973 plane_req->src_w, plane_req->src_h);
1980 drm_modeset_unlock_all(dev);
1984 drm_framebuffer_unreference(fb);
1986 drm_framebuffer_unreference(old_fb);
1992 * drm_mode_set_config_internal - helper to call ->set_config
1993 * @set: modeset config to set
1995 * This is a little helper to wrap internal calls to the ->set_config driver
1996 * interface. The only thing it adds is correct refcounting dance.
1998 int drm_mode_set_config_internal(struct drm_mode_set *set)
2000 struct drm_crtc *crtc = set->crtc;
2001 struct drm_framebuffer *fb;
2002 struct drm_crtc *tmp;
2006 * NOTE: ->set_config can also disable other crtcs (if we steal all
2007 * connectors from it), hence we need to refcount the fbs across all
2008 * crtcs. Atomic modeset will have saner semantics ...
2010 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2011 tmp->old_fb = tmp->fb;
2015 ret = crtc->funcs->set_config(set);
2017 /* crtc->fb must be updated by ->set_config, enforces this. */
2018 WARN_ON(fb != crtc->fb);
2021 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2023 drm_framebuffer_reference(tmp->fb);
2025 drm_framebuffer_unreference(tmp->old_fb);
2030 EXPORT_SYMBOL(drm_mode_set_config_internal);
2033 * Checks that the framebuffer is big enough for the CRTC viewport
2034 * (x, y, hdisplay, vdisplay)
2036 static int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2038 const struct drm_display_mode *mode,
2039 const struct drm_framebuffer *fb)
2042 int hdisplay, vdisplay;
2044 hdisplay = mode->hdisplay;
2045 vdisplay = mode->vdisplay;
2047 if (drm_mode_is_stereo(mode)) {
2048 struct drm_display_mode adjusted = *mode;
2050 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2051 hdisplay = adjusted.crtc_hdisplay;
2052 vdisplay = adjusted.crtc_vdisplay;
2055 if (crtc->invert_dimensions)
2056 swap(hdisplay, vdisplay);
2058 if (hdisplay > fb->width ||
2059 vdisplay > fb->height ||
2060 x > fb->width - hdisplay ||
2061 y > fb->height - vdisplay) {
2062 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2063 fb->width, fb->height, hdisplay, vdisplay, x, y,
2064 crtc->invert_dimensions ? " (inverted)" : "");
2072 * drm_mode_setcrtc - set CRTC configuration
2073 * @dev: drm device for the ioctl
2074 * @data: data pointer for the ioctl
2075 * @file_priv: drm file for the ioctl call
2077 * Build a new CRTC configuration based on user request.
2079 * Called by the user via ioctl.
2082 * Zero on success, errno on failure.
2084 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2085 struct drm_file *file_priv)
2087 struct drm_mode_config *config = &dev->mode_config;
2088 struct drm_mode_crtc *crtc_req = data;
2089 struct drm_mode_object *obj;
2090 struct drm_crtc *crtc;
2091 struct drm_connector **connector_set = NULL, *connector;
2092 struct drm_framebuffer *fb = NULL;
2093 struct drm_display_mode *mode = NULL;
2094 struct drm_mode_set set;
2095 uint32_t __user *set_connectors_ptr;
2099 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2102 /* For some reason crtc x/y offsets are signed internally. */
2103 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2106 drm_modeset_lock_all(dev);
2107 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2108 DRM_MODE_OBJECT_CRTC);
2110 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2114 crtc = obj_to_crtc(obj);
2115 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2117 if (crtc_req->mode_valid) {
2118 /* If we have a mode we need a framebuffer. */
2119 /* If we pass -1, set the mode with the currently bound fb */
2120 if (crtc_req->fb_id == -1) {
2122 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2127 /* Make refcounting symmetric with the lookup path. */
2128 drm_framebuffer_reference(fb);
2130 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2132 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2139 mode = drm_mode_create(dev);
2145 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2147 DRM_DEBUG_KMS("Invalid mode\n");
2151 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2153 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2160 if (crtc_req->count_connectors == 0 && mode) {
2161 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2166 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2167 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2168 crtc_req->count_connectors);
2173 if (crtc_req->count_connectors > 0) {
2176 /* Avoid unbounded kernel memory allocation */
2177 if (crtc_req->count_connectors > config->num_connector) {
2182 connector_set = kmalloc(crtc_req->count_connectors *
2183 sizeof(struct drm_connector *),
2185 if (!connector_set) {
2190 for (i = 0; i < crtc_req->count_connectors; i++) {
2191 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2192 if (get_user(out_id, &set_connectors_ptr[i])) {
2197 obj = drm_mode_object_find(dev, out_id,
2198 DRM_MODE_OBJECT_CONNECTOR);
2200 DRM_DEBUG_KMS("Connector id %d unknown\n",
2205 connector = obj_to_connector(obj);
2206 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2208 drm_get_connector_name(connector));
2210 connector_set[i] = connector;
2215 set.x = crtc_req->x;
2216 set.y = crtc_req->y;
2218 set.connectors = connector_set;
2219 set.num_connectors = crtc_req->count_connectors;
2221 ret = drm_mode_set_config_internal(&set);
2225 drm_framebuffer_unreference(fb);
2227 kfree(connector_set);
2228 drm_mode_destroy(dev, mode);
2229 drm_modeset_unlock_all(dev);
2233 static int drm_mode_cursor_common(struct drm_device *dev,
2234 struct drm_mode_cursor2 *req,
2235 struct drm_file *file_priv)
2237 struct drm_mode_object *obj;
2238 struct drm_crtc *crtc;
2241 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2244 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2247 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2249 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2252 crtc = obj_to_crtc(obj);
2254 mutex_lock(&crtc->mutex);
2255 if (req->flags & DRM_MODE_CURSOR_BO) {
2256 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2260 /* Turns off the cursor if handle is 0 */
2261 if (crtc->funcs->cursor_set2)
2262 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2263 req->width, req->height, req->hot_x, req->hot_y);
2265 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2266 req->width, req->height);
2269 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2270 if (crtc->funcs->cursor_move) {
2271 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2278 mutex_unlock(&crtc->mutex);
2283 int drm_mode_cursor_ioctl(struct drm_device *dev,
2284 void *data, struct drm_file *file_priv)
2286 struct drm_mode_cursor *req = data;
2287 struct drm_mode_cursor2 new_req;
2289 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2290 new_req.hot_x = new_req.hot_y = 0;
2292 return drm_mode_cursor_common(dev, &new_req, file_priv);
2295 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2296 void *data, struct drm_file *file_priv)
2298 struct drm_mode_cursor2 *req = data;
2299 return drm_mode_cursor_common(dev, req, file_priv);
2302 /* Original addfb only supported RGB formats, so figure out which one */
2303 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2309 fmt = DRM_FORMAT_C8;
2313 fmt = DRM_FORMAT_XRGB1555;
2315 fmt = DRM_FORMAT_RGB565;
2318 fmt = DRM_FORMAT_RGB888;
2322 fmt = DRM_FORMAT_XRGB8888;
2323 else if (depth == 30)
2324 fmt = DRM_FORMAT_XRGB2101010;
2326 fmt = DRM_FORMAT_ARGB8888;
2329 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2330 fmt = DRM_FORMAT_XRGB8888;
2336 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2339 * drm_mode_addfb - add an FB to the graphics configuration
2340 * @dev: drm device for the ioctl
2341 * @data: data pointer for the ioctl
2342 * @file_priv: drm file for the ioctl call
2344 * Add a new FB to the specified CRTC, given a user request.
2346 * Called by the user via ioctl.
2349 * Zero on success, errno on failure.
2351 int drm_mode_addfb(struct drm_device *dev,
2352 void *data, struct drm_file *file_priv)
2354 struct drm_mode_fb_cmd *or = data;
2355 struct drm_mode_fb_cmd2 r = {};
2356 struct drm_mode_config *config = &dev->mode_config;
2357 struct drm_framebuffer *fb;
2360 /* Use new struct with format internally */
2361 r.fb_id = or->fb_id;
2362 r.width = or->width;
2363 r.height = or->height;
2364 r.pitches[0] = or->pitch;
2365 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2366 r.handles[0] = or->handle;
2368 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2371 if ((config->min_width > r.width) || (r.width > config->max_width))
2374 if ((config->min_height > r.height) || (r.height > config->max_height))
2377 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2379 DRM_DEBUG_KMS("could not create framebuffer\n");
2383 mutex_lock(&file_priv->fbs_lock);
2384 or->fb_id = fb->base.id;
2385 list_add(&fb->filp_head, &file_priv->fbs);
2386 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2387 mutex_unlock(&file_priv->fbs_lock);
2392 static int format_check(const struct drm_mode_fb_cmd2 *r)
2394 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2398 case DRM_FORMAT_RGB332:
2399 case DRM_FORMAT_BGR233:
2400 case DRM_FORMAT_XRGB4444:
2401 case DRM_FORMAT_XBGR4444:
2402 case DRM_FORMAT_RGBX4444:
2403 case DRM_FORMAT_BGRX4444:
2404 case DRM_FORMAT_ARGB4444:
2405 case DRM_FORMAT_ABGR4444:
2406 case DRM_FORMAT_RGBA4444:
2407 case DRM_FORMAT_BGRA4444:
2408 case DRM_FORMAT_XRGB1555:
2409 case DRM_FORMAT_XBGR1555:
2410 case DRM_FORMAT_RGBX5551:
2411 case DRM_FORMAT_BGRX5551:
2412 case DRM_FORMAT_ARGB1555:
2413 case DRM_FORMAT_ABGR1555:
2414 case DRM_FORMAT_RGBA5551:
2415 case DRM_FORMAT_BGRA5551:
2416 case DRM_FORMAT_RGB565:
2417 case DRM_FORMAT_BGR565:
2418 case DRM_FORMAT_RGB888:
2419 case DRM_FORMAT_BGR888:
2420 case DRM_FORMAT_XRGB8888:
2421 case DRM_FORMAT_XBGR8888:
2422 case DRM_FORMAT_RGBX8888:
2423 case DRM_FORMAT_BGRX8888:
2424 case DRM_FORMAT_ARGB8888:
2425 case DRM_FORMAT_ABGR8888:
2426 case DRM_FORMAT_RGBA8888:
2427 case DRM_FORMAT_BGRA8888:
2428 case DRM_FORMAT_XRGB2101010:
2429 case DRM_FORMAT_XBGR2101010:
2430 case DRM_FORMAT_RGBX1010102:
2431 case DRM_FORMAT_BGRX1010102:
2432 case DRM_FORMAT_ARGB2101010:
2433 case DRM_FORMAT_ABGR2101010:
2434 case DRM_FORMAT_RGBA1010102:
2435 case DRM_FORMAT_BGRA1010102:
2436 case DRM_FORMAT_YUYV:
2437 case DRM_FORMAT_YVYU:
2438 case DRM_FORMAT_UYVY:
2439 case DRM_FORMAT_VYUY:
2440 case DRM_FORMAT_AYUV:
2441 case DRM_FORMAT_NV12:
2442 case DRM_FORMAT_NV21:
2443 case DRM_FORMAT_NV16:
2444 case DRM_FORMAT_NV61:
2445 case DRM_FORMAT_NV24:
2446 case DRM_FORMAT_NV42:
2447 case DRM_FORMAT_YUV410:
2448 case DRM_FORMAT_YVU410:
2449 case DRM_FORMAT_YUV411:
2450 case DRM_FORMAT_YVU411:
2451 case DRM_FORMAT_YUV420:
2452 case DRM_FORMAT_YVU420:
2453 case DRM_FORMAT_YUV422:
2454 case DRM_FORMAT_YVU422:
2455 case DRM_FORMAT_YUV444:
2456 case DRM_FORMAT_YVU444:
2459 DRM_DEBUG_KMS("invalid pixel format %s\n",
2460 drm_get_format_name(r->pixel_format));
2465 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2467 int ret, hsub, vsub, num_planes, i;
2469 ret = format_check(r);
2471 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2472 drm_get_format_name(r->pixel_format));
2476 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2477 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2478 num_planes = drm_format_num_planes(r->pixel_format);
2480 if (r->width == 0 || r->width % hsub) {
2481 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2485 if (r->height == 0 || r->height % vsub) {
2486 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2490 for (i = 0; i < num_planes; i++) {
2491 unsigned int width = r->width / (i != 0 ? hsub : 1);
2492 unsigned int height = r->height / (i != 0 ? vsub : 1);
2493 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2495 if (!r->handles[i]) {
2496 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2500 if ((uint64_t) width * cpp > UINT_MAX)
2503 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2506 if (r->pitches[i] < width * cpp) {
2507 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2516 * drm_mode_addfb2 - add an FB to the graphics configuration
2517 * @dev: drm device for the ioctl
2518 * @data: data pointer for the ioctl
2519 * @file_priv: drm file for the ioctl call
2521 * Add a new FB to the specified CRTC, given a user request with format.
2523 * Called by the user via ioctl.
2526 * Zero on success, errno on failure.
2528 int drm_mode_addfb2(struct drm_device *dev,
2529 void *data, struct drm_file *file_priv)
2531 struct drm_mode_fb_cmd2 *r = data;
2532 struct drm_mode_config *config = &dev->mode_config;
2533 struct drm_framebuffer *fb;
2536 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2539 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2540 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2544 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2545 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2546 r->width, config->min_width, config->max_width);
2549 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2550 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2551 r->height, config->min_height, config->max_height);
2555 ret = framebuffer_check(r);
2559 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2561 DRM_DEBUG_KMS("could not create framebuffer\n");
2565 mutex_lock(&file_priv->fbs_lock);
2566 r->fb_id = fb->base.id;
2567 list_add(&fb->filp_head, &file_priv->fbs);
2568 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2569 mutex_unlock(&file_priv->fbs_lock);
2576 * drm_mode_rmfb - remove an FB from the configuration
2577 * @dev: drm device for the ioctl
2578 * @data: data pointer for the ioctl
2579 * @file_priv: drm file for the ioctl call
2581 * Remove the FB specified by the user.
2583 * Called by the user via ioctl.
2586 * Zero on success, errno on failure.
2588 int drm_mode_rmfb(struct drm_device *dev,
2589 void *data, struct drm_file *file_priv)
2591 struct drm_framebuffer *fb = NULL;
2592 struct drm_framebuffer *fbl = NULL;
2593 uint32_t *id = data;
2596 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2599 mutex_lock(&file_priv->fbs_lock);
2600 mutex_lock(&dev->mode_config.fb_lock);
2601 fb = __drm_framebuffer_lookup(dev, *id);
2605 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2611 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2612 __drm_framebuffer_unregister(dev, fb);
2614 list_del_init(&fb->filp_head);
2615 mutex_unlock(&dev->mode_config.fb_lock);
2616 mutex_unlock(&file_priv->fbs_lock);
2618 drm_framebuffer_remove(fb);
2623 mutex_unlock(&dev->mode_config.fb_lock);
2624 mutex_unlock(&file_priv->fbs_lock);
2630 * drm_mode_getfb - get FB info
2631 * @dev: drm device for the ioctl
2632 * @data: data pointer for the ioctl
2633 * @file_priv: drm file for the ioctl call
2635 * Lookup the FB given its ID and return info about it.
2637 * Called by the user via ioctl.
2640 * Zero on success, errno on failure.
2642 int drm_mode_getfb(struct drm_device *dev,
2643 void *data, struct drm_file *file_priv)
2645 struct drm_mode_fb_cmd *r = data;
2646 struct drm_framebuffer *fb;
2649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2652 fb = drm_framebuffer_lookup(dev, r->fb_id);
2656 r->height = fb->height;
2657 r->width = fb->width;
2658 r->depth = fb->depth;
2659 r->bpp = fb->bits_per_pixel;
2660 r->pitch = fb->pitches[0];
2661 if (fb->funcs->create_handle)
2662 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2666 drm_framebuffer_unreference(fb);
2671 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2672 void *data, struct drm_file *file_priv)
2674 struct drm_clip_rect __user *clips_ptr;
2675 struct drm_clip_rect *clips = NULL;
2676 struct drm_mode_fb_dirty_cmd *r = data;
2677 struct drm_framebuffer *fb;
2682 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2685 fb = drm_framebuffer_lookup(dev, r->fb_id);
2689 num_clips = r->num_clips;
2690 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2692 if (!num_clips != !clips_ptr) {
2697 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2699 /* If userspace annotates copy, clips must come in pairs */
2700 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2705 if (num_clips && clips_ptr) {
2706 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2710 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2716 ret = copy_from_user(clips, clips_ptr,
2717 num_clips * sizeof(*clips));
2724 if (fb->funcs->dirty) {
2725 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2734 drm_framebuffer_unreference(fb);
2741 * drm_fb_release - remove and free the FBs on this file
2742 * @priv: drm file for the ioctl
2744 * Destroy all the FBs associated with @filp.
2746 * Called by the user via ioctl.
2749 * Zero on success, errno on failure.
2751 void drm_fb_release(struct drm_file *priv)
2754 struct drm_device *dev = priv->minor->dev;
2756 struct drm_device *dev = priv->dev;
2758 struct drm_framebuffer *fb, *tfb;
2760 mutex_lock(&priv->fbs_lock);
2761 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2763 mutex_lock(&dev->mode_config.fb_lock);
2764 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2765 __drm_framebuffer_unregister(dev, fb);
2766 mutex_unlock(&dev->mode_config.fb_lock);
2768 list_del_init(&fb->filp_head);
2770 /* This will also drop the fpriv->fbs reference. */
2771 drm_framebuffer_remove(fb);
2773 mutex_unlock(&priv->fbs_lock);
2776 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2777 const char *name, int num_values)
2779 struct drm_property *property = NULL;
2782 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2787 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2788 if (!property->values)
2792 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2796 property->flags = flags;
2797 property->num_values = num_values;
2798 INIT_LIST_HEAD(&property->enum_blob_list);
2801 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2802 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2805 list_add_tail(&property->head, &dev->mode_config.property_list);
2808 kfree(property->values);
2812 EXPORT_SYMBOL(drm_property_create);
2814 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2816 const struct drm_prop_enum_list *props,
2819 struct drm_property *property;
2822 flags |= DRM_MODE_PROP_ENUM;
2824 property = drm_property_create(dev, flags, name, num_values);
2828 for (i = 0; i < num_values; i++) {
2829 ret = drm_property_add_enum(property, i,
2833 drm_property_destroy(dev, property);
2840 EXPORT_SYMBOL(drm_property_create_enum);
2842 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2843 int flags, const char *name,
2844 const struct drm_prop_enum_list *props,
2847 struct drm_property *property;
2850 flags |= DRM_MODE_PROP_BITMASK;
2852 property = drm_property_create(dev, flags, name, num_values);
2856 for (i = 0; i < num_values; i++) {
2857 ret = drm_property_add_enum(property, i,
2861 drm_property_destroy(dev, property);
2868 EXPORT_SYMBOL(drm_property_create_bitmask);
2870 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2872 uint64_t min, uint64_t max)
2874 struct drm_property *property;
2876 flags |= DRM_MODE_PROP_RANGE;
2878 property = drm_property_create(dev, flags, name, 2);
2882 property->values[0] = min;
2883 property->values[1] = max;
2887 EXPORT_SYMBOL(drm_property_create_range);
2889 int drm_property_add_enum(struct drm_property *property, int index,
2890 uint64_t value, const char *name)
2892 struct drm_property_enum *prop_enum;
2894 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2898 * Bitmask enum properties have the additional constraint of values
2901 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2904 if (!list_empty(&property->enum_blob_list)) {
2905 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2906 if (prop_enum->value == value) {
2907 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2908 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2914 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2918 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2919 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2920 prop_enum->value = value;
2922 property->values[index] = value;
2923 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2926 EXPORT_SYMBOL(drm_property_add_enum);
2928 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2930 struct drm_property_enum *prop_enum, *pt;
2932 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2933 list_del(&prop_enum->head);
2937 if (property->num_values)
2938 kfree(property->values);
2939 drm_mode_object_put(dev, &property->base);
2940 list_del(&property->head);
2943 EXPORT_SYMBOL(drm_property_destroy);
2945 void drm_object_attach_property(struct drm_mode_object *obj,
2946 struct drm_property *property,
2949 int count = obj->properties->count;
2951 if (count == DRM_OBJECT_MAX_PROPERTY) {
2952 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2953 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2954 "you see this message on the same object type.\n",
2959 obj->properties->ids[count] = property->base.id;
2960 obj->properties->values[count] = init_val;
2961 obj->properties->count++;
2963 EXPORT_SYMBOL(drm_object_attach_property);
2965 int drm_object_property_set_value(struct drm_mode_object *obj,
2966 struct drm_property *property, uint64_t val)
2970 for (i = 0; i < obj->properties->count; i++) {
2971 if (obj->properties->ids[i] == property->base.id) {
2972 obj->properties->values[i] = val;
2979 EXPORT_SYMBOL(drm_object_property_set_value);
2981 int drm_object_property_get_value(struct drm_mode_object *obj,
2982 struct drm_property *property, uint64_t *val)
2986 for (i = 0; i < obj->properties->count; i++) {
2987 if (obj->properties->ids[i] == property->base.id) {
2988 *val = obj->properties->values[i];
2995 EXPORT_SYMBOL(drm_object_property_get_value);
2997 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2998 void *data, struct drm_file *file_priv)
3000 struct drm_mode_object *obj;
3001 struct drm_mode_get_property *out_resp = data;
3002 struct drm_property *property;
3005 int value_count = 0;
3008 struct drm_property_enum *prop_enum;
3009 struct drm_mode_property_enum __user *enum_ptr;
3010 struct drm_property_blob *prop_blob;
3011 uint32_t __user *blob_id_ptr;
3012 uint64_t __user *values_ptr;
3013 uint32_t __user *blob_length_ptr;
3015 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3018 drm_modeset_lock_all(dev);
3019 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3024 property = obj_to_property(obj);
3026 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3027 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3029 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3030 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3034 value_count = property->num_values;
3036 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3037 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3038 out_resp->flags = property->flags;
3040 if ((out_resp->count_values >= value_count) && value_count) {
3041 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3042 for (i = 0; i < value_count; i++) {
3043 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3049 out_resp->count_values = value_count;
3051 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3052 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3054 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3055 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3057 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3062 if (copy_to_user(&enum_ptr[copied].name,
3063 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3070 out_resp->count_enum_blobs = enum_count;
3073 if (property->flags & DRM_MODE_PROP_BLOB) {
3074 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3076 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3077 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3079 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3080 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3085 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3093 out_resp->count_enum_blobs = blob_count;
3096 drm_modeset_unlock_all(dev);
3100 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3103 struct drm_property_blob *blob;
3106 if (!length || !data)
3109 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3113 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3119 blob->length = length;
3121 memcpy(blob->data, data, length);
3123 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3127 static void drm_property_destroy_blob(struct drm_device *dev,
3128 struct drm_property_blob *blob)
3130 drm_mode_object_put(dev, &blob->base);
3131 list_del(&blob->head);
3135 int drm_mode_getblob_ioctl(struct drm_device *dev,
3136 void *data, struct drm_file *file_priv)
3138 struct drm_mode_object *obj;
3139 struct drm_mode_get_blob *out_resp = data;
3140 struct drm_property_blob *blob;
3142 void __user *blob_ptr;
3144 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3147 drm_modeset_lock_all(dev);
3148 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3153 blob = obj_to_blob(obj);
3155 if (out_resp->length == blob->length) {
3156 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3157 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3162 out_resp->length = blob->length;
3165 drm_modeset_unlock_all(dev);
3169 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3172 struct drm_device *dev = connector->dev;
3175 if (connector->edid_blob_ptr)
3176 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3178 /* Delete edid, when there is none. */
3180 connector->edid_blob_ptr = NULL;
3181 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3185 size = EDID_LENGTH * (1 + edid->extensions);
3186 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3188 if (!connector->edid_blob_ptr)
3191 ret = drm_object_property_set_value(&connector->base,
3192 dev->mode_config.edid_property,
3193 connector->edid_blob_ptr->base.id);
3197 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3199 static bool drm_property_change_is_valid(struct drm_property *property,
3202 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3204 if (property->flags & DRM_MODE_PROP_RANGE) {
3205 if (value < property->values[0] || value > property->values[1])
3208 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3210 uint64_t valid_mask = 0;
3211 for (i = 0; i < property->num_values; i++)
3212 valid_mask |= (1ULL << property->values[i]);
3213 return !(value & ~valid_mask);
3214 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3215 /* Only the driver knows */
3219 for (i = 0; i < property->num_values; i++)
3220 if (property->values[i] == value)
3226 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3227 void *data, struct drm_file *file_priv)
3229 struct drm_mode_connector_set_property *conn_set_prop = data;
3230 struct drm_mode_obj_set_property obj_set_prop = {
3231 .value = conn_set_prop->value,
3232 .prop_id = conn_set_prop->prop_id,
3233 .obj_id = conn_set_prop->connector_id,
3234 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3237 /* It does all the locking and checking we need */
3238 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3241 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3242 struct drm_property *property,
3246 struct drm_connector *connector = obj_to_connector(obj);
3248 /* Do DPMS ourselves */
3249 if (property == connector->dev->mode_config.dpms_property) {
3250 if (connector->funcs->dpms)
3251 (*connector->funcs->dpms)(connector, (int)value);
3253 } else if (connector->funcs->set_property)
3254 ret = connector->funcs->set_property(connector, property, value);
3256 /* store the property value if successful */
3258 drm_object_property_set_value(&connector->base, property, value);
3262 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3263 struct drm_property *property,
3267 struct drm_crtc *crtc = obj_to_crtc(obj);
3269 if (crtc->funcs->set_property)
3270 ret = crtc->funcs->set_property(crtc, property, value);
3272 drm_object_property_set_value(obj, property, value);
3277 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3278 struct drm_property *property,
3282 struct drm_plane *plane = obj_to_plane(obj);
3284 if (plane->funcs->set_property)
3285 ret = plane->funcs->set_property(plane, property, value);
3287 drm_object_property_set_value(obj, property, value);
3292 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3293 struct drm_file *file_priv)
3295 struct drm_mode_obj_get_properties *arg = data;
3296 struct drm_mode_object *obj;
3300 int props_count = 0;
3301 uint32_t __user *props_ptr;
3302 uint64_t __user *prop_values_ptr;
3304 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3307 drm_modeset_lock_all(dev);
3309 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3314 if (!obj->properties) {
3319 props_count = obj->properties->count;
3321 /* This ioctl is called twice, once to determine how much space is
3322 * needed, and the 2nd time to fill it. */
3323 if ((arg->count_props >= props_count) && props_count) {
3325 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3326 prop_values_ptr = (uint64_t __user *)(unsigned long)
3327 (arg->prop_values_ptr);
3328 for (i = 0; i < props_count; i++) {
3329 if (put_user(obj->properties->ids[i],
3330 props_ptr + copied)) {
3334 if (put_user(obj->properties->values[i],
3335 prop_values_ptr + copied)) {
3342 arg->count_props = props_count;
3344 drm_modeset_unlock_all(dev);
3348 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3349 struct drm_file *file_priv)
3351 struct drm_mode_obj_set_property *arg = data;
3352 struct drm_mode_object *arg_obj;
3353 struct drm_mode_object *prop_obj;
3354 struct drm_property *property;
3358 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3361 drm_modeset_lock_all(dev);
3363 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3368 if (!arg_obj->properties)
3371 for (i = 0; i < arg_obj->properties->count; i++)
3372 if (arg_obj->properties->ids[i] == arg->prop_id)
3375 if (i == arg_obj->properties->count)
3378 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3379 DRM_MODE_OBJECT_PROPERTY);
3384 property = obj_to_property(prop_obj);
3386 if (!drm_property_change_is_valid(property, arg->value))
3389 switch (arg_obj->type) {
3390 case DRM_MODE_OBJECT_CONNECTOR:
3391 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3394 case DRM_MODE_OBJECT_CRTC:
3395 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3397 case DRM_MODE_OBJECT_PLANE:
3398 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3403 drm_modeset_unlock_all(dev);
3407 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3408 struct drm_encoder *encoder)
3412 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3413 if (connector->encoder_ids[i] == 0) {
3414 connector->encoder_ids[i] = encoder->base.id;
3420 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3422 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3423 struct drm_encoder *encoder)
3426 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3427 if (connector->encoder_ids[i] == encoder->base.id) {
3428 connector->encoder_ids[i] = 0;
3429 if (connector->encoder == encoder)
3430 connector->encoder = NULL;
3435 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3437 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3440 crtc->gamma_size = gamma_size;
3442 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3443 if (!crtc->gamma_store) {
3444 crtc->gamma_size = 0;
3450 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3452 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3453 void *data, struct drm_file *file_priv)
3455 struct drm_mode_crtc_lut *crtc_lut = data;
3456 struct drm_mode_object *obj;
3457 struct drm_crtc *crtc;
3458 void *r_base, *g_base, *b_base;
3462 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3465 drm_modeset_lock_all(dev);
3466 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3471 crtc = obj_to_crtc(obj);
3473 if (crtc->funcs->gamma_set == NULL) {
3478 /* memcpy into gamma store */
3479 if (crtc_lut->gamma_size != crtc->gamma_size) {
3484 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3485 r_base = crtc->gamma_store;
3486 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3491 g_base = (char *)r_base + size;
3492 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3497 b_base = (char *)g_base + size;
3498 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3503 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3506 drm_modeset_unlock_all(dev);
3511 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3514 struct drm_mode_crtc_lut *crtc_lut = data;
3515 struct drm_mode_object *obj;
3516 struct drm_crtc *crtc;
3517 void *r_base, *g_base, *b_base;
3521 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3524 drm_modeset_lock_all(dev);
3525 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3530 crtc = obj_to_crtc(obj);
3532 /* memcpy into gamma store */
3533 if (crtc_lut->gamma_size != crtc->gamma_size) {
3538 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3539 r_base = crtc->gamma_store;
3540 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3545 g_base = (char *)r_base + size;
3546 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3551 b_base = (char *)g_base + size;
3552 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3557 drm_modeset_unlock_all(dev);
3562 * The Linux version of kfree() is a macro and can't be called
3563 * directly via a function pointer
3565 static void drm_kms_free(void *arg)
3570 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3571 void *data, struct drm_file *file_priv)
3573 struct drm_mode_crtc_page_flip *page_flip = data;
3574 struct drm_mode_object *obj;
3575 struct drm_crtc *crtc;
3576 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
3577 struct drm_pending_vblank_event *e = NULL;
3580 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3581 page_flip->reserved != 0)
3584 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
3587 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3590 crtc = obj_to_crtc(obj);
3592 mutex_lock(&crtc->mutex);
3593 if (crtc->fb == NULL) {
3594 /* The framebuffer is currently unbound, presumably
3595 * due to a hotplug event, that userspace has not
3602 if (crtc->funcs->page_flip == NULL)
3605 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3611 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
3615 if (crtc->fb->pixel_format != fb->pixel_format) {
3616 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3621 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3623 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3624 if (file_priv->event_space < sizeof e->event) {
3625 lockmgr(&dev->event_lock, LK_RELEASE);
3628 file_priv->event_space -= sizeof e->event;
3629 lockmgr(&dev->event_lock, LK_RELEASE);
3631 e = kzalloc(sizeof *e, GFP_KERNEL);
3633 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3634 file_priv->event_space += sizeof e->event;
3635 lockmgr(&dev->event_lock, LK_RELEASE);
3639 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3640 e->event.base.length = sizeof e->event;
3641 e->event.user_data = page_flip->user_data;
3642 e->base.event = &e->event.base;
3643 e->base.file_priv = file_priv;
3645 (void (*) (struct drm_pending_event *)) drm_kms_free;
3649 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
3651 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3652 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
3653 file_priv->event_space += sizeof e->event;
3654 lockmgr(&dev->event_lock, LK_RELEASE);
3657 /* Keep the old fb, don't unref it. */
3661 * Warn if the driver hasn't properly updated the crtc->fb
3662 * field to reflect that the new framebuffer is now used.
3663 * Failing to do so will screw with the reference counting
3666 WARN_ON(crtc->fb != fb);
3667 /* Unref only the old framebuffer. */
3673 drm_framebuffer_unreference(fb);
3675 drm_framebuffer_unreference(old_fb);
3676 mutex_unlock(&crtc->mutex);
3681 void drm_mode_config_reset(struct drm_device *dev)
3683 struct drm_crtc *crtc;
3684 struct drm_encoder *encoder;
3685 struct drm_connector *connector;
3687 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3688 if (crtc->funcs->reset)
3689 crtc->funcs->reset(crtc);
3691 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3692 if (encoder->funcs->reset)
3693 encoder->funcs->reset(encoder);
3695 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3696 connector->status = connector_status_unknown;
3698 if (connector->funcs->reset)
3699 connector->funcs->reset(connector);
3702 EXPORT_SYMBOL(drm_mode_config_reset);
3704 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3705 void *data, struct drm_file *file_priv)
3707 struct drm_mode_create_dumb *args = data;
3709 if (!dev->driver->dumb_create)
3711 return dev->driver->dumb_create(file_priv, dev, args);
3714 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3715 void *data, struct drm_file *file_priv)
3717 struct drm_mode_map_dumb *args = data;
3719 /* call driver ioctl to get mmap offset */
3720 if (!dev->driver->dumb_map_offset)
3723 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3726 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3727 void *data, struct drm_file *file_priv)
3729 struct drm_mode_destroy_dumb *args = data;
3731 if (!dev->driver->dumb_destroy)
3734 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3738 * Just need to support RGB formats here for compat with code that doesn't
3739 * use pixel formats directly yet.
3741 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3746 case DRM_FORMAT_RGB332:
3747 case DRM_FORMAT_BGR233:
3751 case DRM_FORMAT_XRGB1555:
3752 case DRM_FORMAT_XBGR1555:
3753 case DRM_FORMAT_RGBX5551:
3754 case DRM_FORMAT_BGRX5551:
3755 case DRM_FORMAT_ARGB1555:
3756 case DRM_FORMAT_ABGR1555:
3757 case DRM_FORMAT_RGBA5551:
3758 case DRM_FORMAT_BGRA5551:
3762 case DRM_FORMAT_RGB565:
3763 case DRM_FORMAT_BGR565:
3767 case DRM_FORMAT_RGB888:
3768 case DRM_FORMAT_BGR888:
3772 case DRM_FORMAT_XRGB8888:
3773 case DRM_FORMAT_XBGR8888:
3774 case DRM_FORMAT_RGBX8888:
3775 case DRM_FORMAT_BGRX8888:
3779 case DRM_FORMAT_XRGB2101010:
3780 case DRM_FORMAT_XBGR2101010:
3781 case DRM_FORMAT_RGBX1010102:
3782 case DRM_FORMAT_BGRX1010102:
3783 case DRM_FORMAT_ARGB2101010:
3784 case DRM_FORMAT_ABGR2101010:
3785 case DRM_FORMAT_RGBA1010102:
3786 case DRM_FORMAT_BGRA1010102:
3790 case DRM_FORMAT_ARGB8888:
3791 case DRM_FORMAT_ABGR8888:
3792 case DRM_FORMAT_RGBA8888:
3793 case DRM_FORMAT_BGRA8888:
3798 DRM_DEBUG_KMS("unsupported pixel format %s\n",
3799 drm_get_format_name(format));
3805 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3808 * drm_format_num_planes - get the number of planes for format
3809 * @format: pixel format (DRM_FORMAT_*)
3812 * The number of planes used by the specified pixel format.
3814 int drm_format_num_planes(uint32_t format)
3817 case DRM_FORMAT_YUV410:
3818 case DRM_FORMAT_YVU410:
3819 case DRM_FORMAT_YUV411:
3820 case DRM_FORMAT_YVU411:
3821 case DRM_FORMAT_YUV420:
3822 case DRM_FORMAT_YVU420:
3823 case DRM_FORMAT_YUV422:
3824 case DRM_FORMAT_YVU422:
3825 case DRM_FORMAT_YUV444:
3826 case DRM_FORMAT_YVU444:
3828 case DRM_FORMAT_NV12:
3829 case DRM_FORMAT_NV21:
3830 case DRM_FORMAT_NV16:
3831 case DRM_FORMAT_NV61:
3832 case DRM_FORMAT_NV24:
3833 case DRM_FORMAT_NV42:
3839 EXPORT_SYMBOL(drm_format_num_planes);
3842 * drm_format_plane_cpp - determine the bytes per pixel value
3843 * @format: pixel format (DRM_FORMAT_*)
3844 * @plane: plane index
3847 * The bytes per pixel value for the specified plane.
3849 int drm_format_plane_cpp(uint32_t format, int plane)
3854 if (plane >= drm_format_num_planes(format))
3858 case DRM_FORMAT_YUYV:
3859 case DRM_FORMAT_YVYU:
3860 case DRM_FORMAT_UYVY:
3861 case DRM_FORMAT_VYUY:
3863 case DRM_FORMAT_NV12:
3864 case DRM_FORMAT_NV21:
3865 case DRM_FORMAT_NV16:
3866 case DRM_FORMAT_NV61:
3867 case DRM_FORMAT_NV24:
3868 case DRM_FORMAT_NV42:
3869 return plane ? 2 : 1;
3870 case DRM_FORMAT_YUV410:
3871 case DRM_FORMAT_YVU410:
3872 case DRM_FORMAT_YUV411:
3873 case DRM_FORMAT_YVU411:
3874 case DRM_FORMAT_YUV420:
3875 case DRM_FORMAT_YVU420:
3876 case DRM_FORMAT_YUV422:
3877 case DRM_FORMAT_YVU422:
3878 case DRM_FORMAT_YUV444:
3879 case DRM_FORMAT_YVU444:
3882 drm_fb_get_bpp_depth(format, &depth, &bpp);
3886 EXPORT_SYMBOL(drm_format_plane_cpp);
3889 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3890 * @format: pixel format (DRM_FORMAT_*)
3893 * The horizontal chroma subsampling factor for the
3894 * specified pixel format.
3896 int drm_format_horz_chroma_subsampling(uint32_t format)
3899 case DRM_FORMAT_YUV411:
3900 case DRM_FORMAT_YVU411:
3901 case DRM_FORMAT_YUV410:
3902 case DRM_FORMAT_YVU410:
3904 case DRM_FORMAT_YUYV:
3905 case DRM_FORMAT_YVYU:
3906 case DRM_FORMAT_UYVY:
3907 case DRM_FORMAT_VYUY:
3908 case DRM_FORMAT_NV12:
3909 case DRM_FORMAT_NV21:
3910 case DRM_FORMAT_NV16:
3911 case DRM_FORMAT_NV61:
3912 case DRM_FORMAT_YUV422:
3913 case DRM_FORMAT_YVU422:
3914 case DRM_FORMAT_YUV420:
3915 case DRM_FORMAT_YVU420:
3921 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3924 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3925 * @format: pixel format (DRM_FORMAT_*)
3928 * The vertical chroma subsampling factor for the
3929 * specified pixel format.
3931 int drm_format_vert_chroma_subsampling(uint32_t format)
3934 case DRM_FORMAT_YUV410:
3935 case DRM_FORMAT_YVU410:
3937 case DRM_FORMAT_YUV420:
3938 case DRM_FORMAT_YVU420:
3939 case DRM_FORMAT_NV12:
3940 case DRM_FORMAT_NV21:
3946 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
3949 * drm_mode_config_init - initialize DRM mode_configuration structure
3952 * Initialize @dev's mode_config structure, used for tracking the graphics
3953 * configuration of @dev.
3955 * Since this initializes the modeset locks, no locking is possible. Which is no
3956 * problem, since this should happen single threaded at init time. It is the
3957 * driver's problem to ensure this guarantee.
3960 void drm_mode_config_init(struct drm_device *dev)
3962 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
3963 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
3964 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
3965 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3966 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3967 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3968 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3969 INIT_LIST_HEAD(&dev->mode_config.property_list);
3970 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3971 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3972 idr_init(&dev->mode_config.crtc_idr);
3974 drm_modeset_lock_all(dev);
3975 drm_mode_create_standard_connector_properties(dev);
3976 drm_modeset_unlock_all(dev);
3978 /* Just to be sure */
3979 dev->mode_config.num_fb = 0;
3980 dev->mode_config.num_connector = 0;
3981 dev->mode_config.num_crtc = 0;
3982 dev->mode_config.num_encoder = 0;
3984 EXPORT_SYMBOL(drm_mode_config_init);
3987 * drm_mode_config_cleanup - free up DRM mode_config info
3990 * Free up all the connectors and CRTCs associated with this DRM device, then
3991 * free up the framebuffers and associated buffer objects.
3993 * Note that since this /should/ happen single-threaded at driver/device
3994 * teardown time, no locking is required. It's the driver's job to ensure that
3995 * this guarantee actually holds true.
3997 * FIXME: cleanup any dangling user buffer objects too
3999 void drm_mode_config_cleanup(struct drm_device *dev)
4001 struct drm_connector *connector, *ot;
4002 struct drm_crtc *crtc, *ct;
4003 struct drm_encoder *encoder, *enct;
4004 struct drm_framebuffer *fb, *fbt;
4005 struct drm_property *property, *pt;
4006 struct drm_property_blob *blob, *bt;
4007 struct drm_plane *plane, *plt;
4009 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4011 encoder->funcs->destroy(encoder);
4014 list_for_each_entry_safe(connector, ot,
4015 &dev->mode_config.connector_list, head) {
4016 connector->funcs->destroy(connector);
4019 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4021 drm_property_destroy(dev, property);
4024 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4026 drm_property_destroy_blob(dev, blob);
4030 * Single-threaded teardown context, so it's not required to grab the
4031 * fb_lock to protect against concurrent fb_list access. Contrary, it
4032 * would actually deadlock with the drm_framebuffer_cleanup function.
4034 * Also, if there are any framebuffers left, that's a driver leak now,
4035 * so politely WARN about this.
4037 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4038 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4039 drm_framebuffer_remove(fb);
4042 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4044 plane->funcs->destroy(plane);
4047 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4048 crtc->funcs->destroy(crtc);
4051 idr_destroy(&dev->mode_config.crtc_idr);
4053 EXPORT_SYMBOL(drm_mode_config_cleanup);