2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
41 #include "drm_crtc_internal.h"
44 * drm_modeset_lock_all - take all modeset locks
47 * This function takes all modeset locks, suitable where a more fine-grained
48 * scheme isn't (yet) implemented. Locks must be dropped with
49 * drm_modeset_unlock_all.
51 void drm_modeset_lock_all(struct drm_device *dev)
53 struct drm_crtc *crtc;
55 mutex_lock(&dev->mode_config.mutex);
57 mutex_lock(&dev->mode_config.connection_mutex);
59 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
60 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
62 EXPORT_SYMBOL(drm_modeset_lock_all);
65 * drm_modeset_unlock_all - drop all modeset locks
68 * This function drop all modeset locks taken by drm_modeset_lock_all.
70 void drm_modeset_unlock_all(struct drm_device *dev)
72 struct drm_crtc *crtc;
74 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
75 mutex_unlock(&crtc->mutex);
77 mutex_unlock(&dev->mode_config.connection_mutex);
79 mutex_unlock(&dev->mode_config.mutex);
81 EXPORT_SYMBOL(drm_modeset_unlock_all);
84 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
87 * Useful as a debug assert.
89 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
91 struct drm_crtc *crtc;
93 /* Locking is currently fubar in the panic handler. */
97 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
98 WARN_ON(!mutex_is_locked(&crtc->mutex));
100 WARN_ON(!mutex_is_locked(&dev->mode_config.connection_mutex));
101 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
103 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
105 /* Avoid boilerplate. I'm tired of typing. */
106 #define DRM_ENUM_NAME_FN(fnname, list) \
107 const char *fnname(int val) \
110 for (i = 0; i < ARRAY_SIZE(list); i++) { \
111 if (list[i].type == val) \
112 return list[i].name; \
114 return "(unknown)"; \
120 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
121 { { DRM_MODE_DPMS_ON, "On" },
122 { DRM_MODE_DPMS_STANDBY, "Standby" },
123 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
124 { DRM_MODE_DPMS_OFF, "Off" }
127 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
129 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
131 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
132 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
133 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
137 * Optional properties
139 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
141 { DRM_MODE_SCALE_NONE, "None" },
142 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
143 { DRM_MODE_SCALE_CENTER, "Center" },
144 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
148 * Non-global properties, but "required" for certain connectors.
150 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
152 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
153 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
154 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
157 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
159 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
161 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
162 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
163 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
166 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
167 drm_dvi_i_subconnector_enum_list)
169 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
171 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
172 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
173 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
174 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
175 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
178 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
180 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
182 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
183 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
184 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
185 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
186 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
189 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
190 drm_tv_subconnector_enum_list)
192 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
193 { DRM_MODE_DIRTY_OFF, "Off" },
194 { DRM_MODE_DIRTY_ON, "On" },
195 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
198 struct drm_conn_prop_enum_list {
205 * Connector and encoder types.
207 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
208 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
209 { DRM_MODE_CONNECTOR_VGA, "VGA" },
210 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
211 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
212 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
213 { DRM_MODE_CONNECTOR_Composite, "Composite" },
214 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
215 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
216 { DRM_MODE_CONNECTOR_Component, "Component" },
217 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
218 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
219 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
220 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
221 { DRM_MODE_CONNECTOR_TV, "TV" },
222 { DRM_MODE_CONNECTOR_eDP, "eDP" },
223 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
224 { DRM_MODE_CONNECTOR_DSI, "DSI" },
227 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
228 { { DRM_MODE_ENCODER_NONE, "None" },
229 { DRM_MODE_ENCODER_DAC, "DAC" },
230 { DRM_MODE_ENCODER_TMDS, "TMDS" },
231 { DRM_MODE_ENCODER_LVDS, "LVDS" },
232 { DRM_MODE_ENCODER_TVDAC, "TV" },
233 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
234 { DRM_MODE_ENCODER_DSI, "DSI" },
235 { DRM_MODE_ENCODER_DPMST, "DP MST" },
238 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
240 { SubPixelUnknown, "Unknown" },
241 { SubPixelHorizontalRGB, "Horizontal RGB" },
242 { SubPixelHorizontalBGR, "Horizontal BGR" },
243 { SubPixelVerticalRGB, "Vertical RGB" },
244 { SubPixelVerticalBGR, "Vertical BGR" },
245 { SubPixelNone, "None" },
248 void drm_connector_ida_init(void)
252 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
253 ida_init(&drm_connector_enum_list[i].ida);
256 void drm_connector_ida_destroy(void)
260 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
261 ida_destroy(&drm_connector_enum_list[i].ida);
265 * drm_get_connector_status_name - return a string for connector status
266 * @status: connector status to compute name of
268 * In contrast to the other drm_get_*_name functions this one here returns a
269 * const pointer and hence is threadsafe.
271 const char *drm_get_connector_status_name(enum drm_connector_status status)
273 if (status == connector_status_connected)
275 else if (status == connector_status_disconnected)
276 return "disconnected";
280 EXPORT_SYMBOL(drm_get_connector_status_name);
283 * drm_get_subpixel_order_name - return a string for a given subpixel enum
284 * @order: enum of subpixel_order
286 * Note you could abuse this and return something out of bounds, but that
287 * would be a caller error. No unscrubbed user data should make it here.
289 const char *drm_get_subpixel_order_name(enum subpixel_order order)
291 return drm_subpixel_enum_list[order].name;
293 EXPORT_SYMBOL(drm_get_subpixel_order_name);
295 static char printable_char(int c)
297 return isascii(c) && isprint(c) ? c : '?';
301 * drm_get_format_name - return a string for drm fourcc format
302 * @format: format to compute name of
304 * Note that the buffer used by this function is globally shared and owned by
305 * the function itself.
307 * FIXME: This isn't really multithreading safe.
309 const char *drm_get_format_name(uint32_t format)
313 snprintf(buf, sizeof(buf),
314 "%c%c%c%c %s-endian (0x%08x)",
315 printable_char(format & 0xff),
316 printable_char((format >> 8) & 0xff),
317 printable_char((format >> 16) & 0xff),
318 printable_char((format >> 24) & 0x7f),
319 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
324 EXPORT_SYMBOL(drm_get_format_name);
327 * drm_mode_object_get - allocate a new modeset identifier
329 * @obj: object pointer, used to generate unique ID
330 * @obj_type: object type
332 * Create a unique identifier based on @ptr in @dev's identifier space. Used
333 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
334 * modeset identifiers are _not_ reference counted. Hence don't use this for
335 * reference counted modeset objects like framebuffers.
338 * New unique (relative to other objects in @dev) integer identifier for the
341 int drm_mode_object_get(struct drm_device *dev,
342 struct drm_mode_object *obj, uint32_t obj_type)
346 mutex_lock(&dev->mode_config.idr_mutex);
347 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
350 * Set up the object linking under the protection of the idr
351 * lock so that other users can't see inconsistent state.
354 obj->type = obj_type;
356 mutex_unlock(&dev->mode_config.idr_mutex);
358 return ret < 0 ? ret : 0;
362 * drm_mode_object_put - free a modeset identifer
364 * @object: object to free
366 * Free @id from @dev's unique identifier pool. Note that despite the _get
367 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
368 * for reference counted modeset objects like framebuffers.
370 void drm_mode_object_put(struct drm_device *dev,
371 struct drm_mode_object *object)
373 mutex_lock(&dev->mode_config.idr_mutex);
374 idr_remove(&dev->mode_config.crtc_idr, object->id);
375 mutex_unlock(&dev->mode_config.idr_mutex);
378 static struct drm_mode_object *_object_find(struct drm_device *dev,
379 uint32_t id, uint32_t type)
381 struct drm_mode_object *obj = NULL;
383 mutex_lock(&dev->mode_config.idr_mutex);
384 obj = idr_find(&dev->mode_config.crtc_idr, id);
385 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
388 mutex_unlock(&dev->mode_config.idr_mutex);
394 * drm_mode_object_find - look up a drm object with static lifetime
396 * @id: id of the mode object
397 * @type: type of the mode object
399 * Note that framebuffers cannot be looked up with this functions - since those
400 * are reference counted, they need special treatment. Even with
401 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
402 * rather than WARN_ON()).
404 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
405 uint32_t id, uint32_t type)
407 struct drm_mode_object *obj = NULL;
409 /* Framebuffers are reference counted and need their own lookup
411 WARN_ON(type == DRM_MODE_OBJECT_FB);
412 obj = _object_find(dev, id, type);
413 /* don't leak out unref'd fb's */
414 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
418 EXPORT_SYMBOL(drm_mode_object_find);
421 * drm_framebuffer_init - initialize a framebuffer
423 * @fb: framebuffer to be initialized
424 * @funcs: ... with these functions
426 * Allocates an ID for the framebuffer's parent mode object, sets its mode
427 * functions & device file and adds it to the master fd list.
430 * This functions publishes the fb and makes it available for concurrent access
431 * by other users. Which means by this point the fb _must_ be fully set up -
432 * since all the fb attributes are invariant over its lifetime, no further
433 * locking but only correct reference counting is required.
436 * Zero on success, error code on failure.
438 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
439 const struct drm_framebuffer_funcs *funcs)
443 mutex_lock(&dev->mode_config.fb_lock);
444 kref_init(&fb->refcount);
445 INIT_LIST_HEAD(&fb->filp_head);
449 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
453 /* Grab the idr reference. */
454 drm_framebuffer_reference(fb);
456 dev->mode_config.num_fb++;
457 list_add(&fb->head, &dev->mode_config.fb_list);
459 mutex_unlock(&dev->mode_config.fb_lock);
463 EXPORT_SYMBOL(drm_framebuffer_init);
465 static void drm_framebuffer_free(struct kref *kref)
467 struct drm_framebuffer *fb =
468 container_of(kref, struct drm_framebuffer, refcount);
469 fb->funcs->destroy(fb);
472 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
475 struct drm_mode_object *obj = NULL;
476 struct drm_framebuffer *fb;
478 mutex_lock(&dev->mode_config.idr_mutex);
479 obj = idr_find(&dev->mode_config.crtc_idr, id);
480 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
484 mutex_unlock(&dev->mode_config.idr_mutex);
490 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
492 * @id: id of the fb object
494 * If successful, this grabs an additional reference to the framebuffer -
495 * callers need to make sure to eventually unreference the returned framebuffer
496 * again, using @drm_framebuffer_unreference.
498 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
501 struct drm_framebuffer *fb;
503 mutex_lock(&dev->mode_config.fb_lock);
504 fb = __drm_framebuffer_lookup(dev, id);
506 drm_framebuffer_reference(fb);
507 mutex_unlock(&dev->mode_config.fb_lock);
511 EXPORT_SYMBOL(drm_framebuffer_lookup);
514 * drm_framebuffer_unreference - unref a framebuffer
515 * @fb: framebuffer to unref
517 * This functions decrements the fb's refcount and frees it if it drops to zero.
519 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
521 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
522 kref_put(&fb->refcount, drm_framebuffer_free);
524 EXPORT_SYMBOL(drm_framebuffer_unreference);
527 * drm_framebuffer_reference - incr the fb refcnt
530 * This functions increments the fb's refcount.
532 void drm_framebuffer_reference(struct drm_framebuffer *fb)
534 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
535 kref_get(&fb->refcount);
537 EXPORT_SYMBOL(drm_framebuffer_reference);
539 static void drm_framebuffer_free_bug(struct kref *kref)
544 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
546 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
547 kref_put(&fb->refcount, drm_framebuffer_free_bug);
550 /* dev->mode_config.fb_lock must be held! */
551 static void __drm_framebuffer_unregister(struct drm_device *dev,
552 struct drm_framebuffer *fb)
554 mutex_lock(&dev->mode_config.idr_mutex);
555 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
556 mutex_unlock(&dev->mode_config.idr_mutex);
560 __drm_framebuffer_unreference(fb);
564 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
565 * @fb: fb to unregister
567 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
568 * those used for fbdev. Note that the caller must hold a reference of it's own,
569 * i.e. the object may not be destroyed through this call (since it'll lead to a
570 * locking inversion).
572 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
574 struct drm_device *dev = fb->dev;
576 mutex_lock(&dev->mode_config.fb_lock);
577 /* Mark fb as reaped and drop idr ref. */
578 __drm_framebuffer_unregister(dev, fb);
579 mutex_unlock(&dev->mode_config.fb_lock);
581 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
584 * drm_framebuffer_cleanup - remove a framebuffer object
585 * @fb: framebuffer to remove
587 * Cleanup framebuffer. This function is intended to be used from the drivers
588 * ->destroy callback. It can also be used to clean up driver private
589 * framebuffers embedded into a larger structure.
591 * Note that this function does not remove the fb from active usuage - if it is
592 * still used anywhere, hilarity can ensue since userspace could call getfb on
593 * the id and get back -EINVAL. Obviously no concern at driver unload time.
595 * Also, the framebuffer will not be removed from the lookup idr - for
596 * user-created framebuffers this will happen in in the rmfb ioctl. For
597 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
598 * drm_framebuffer_unregister_private.
600 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
602 struct drm_device *dev = fb->dev;
604 mutex_lock(&dev->mode_config.fb_lock);
606 dev->mode_config.num_fb--;
607 mutex_unlock(&dev->mode_config.fb_lock);
609 EXPORT_SYMBOL(drm_framebuffer_cleanup);
612 * drm_framebuffer_remove - remove and unreference a framebuffer object
613 * @fb: framebuffer to remove
615 * Scans all the CRTCs and planes in @dev's mode_config. If they're
616 * using @fb, removes it, setting it to NULL. Then drops the reference to the
617 * passed-in framebuffer. Might take the modeset locks.
619 * Note that this function optimizes the cleanup away if the caller holds the
620 * last reference to the framebuffer. It is also guaranteed to not take the
621 * modeset locks in this case.
623 void drm_framebuffer_remove(struct drm_framebuffer *fb)
625 struct drm_device *dev = fb->dev;
626 struct drm_crtc *crtc;
627 struct drm_plane *plane;
628 struct drm_mode_set set;
631 WARN_ON(!list_empty(&fb->filp_head));
634 * drm ABI mandates that we remove any deleted framebuffers from active
635 * useage. But since most sane clients only remove framebuffers they no
636 * longer need, try to optimize this away.
638 * Since we're holding a reference ourselves, observing a refcount of 1
639 * means that we're the last holder and can skip it. Also, the refcount
640 * can never increase from 1 again, so we don't need any barriers or
643 * Note that userspace could try to race with use and instate a new
644 * usage _after_ we've cleared all current ones. End result will be an
645 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
648 if (atomic_read(&fb->refcount.refcount) > 1) {
649 drm_modeset_lock_all(dev);
650 /* remove from any CRTC */
651 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
652 if (crtc->primary->fb == fb) {
653 /* should turn off the crtc */
654 memset(&set, 0, sizeof(struct drm_mode_set));
657 ret = drm_mode_set_config_internal(&set);
659 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
663 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
665 drm_plane_force_disable(plane);
667 drm_modeset_unlock_all(dev);
670 drm_framebuffer_unreference(fb);
672 EXPORT_SYMBOL(drm_framebuffer_remove);
675 * drm_crtc_init_with_planes - Initialise a new CRTC object with
676 * specified primary and cursor planes.
678 * @crtc: CRTC object to init
679 * @primary: Primary plane for CRTC
680 * @cursor: Cursor plane for CRTC
681 * @funcs: callbacks for the new CRTC
683 * Inits a new object created as base part of a driver crtc object.
686 * Zero on success, error code on failure.
688 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
689 struct drm_plane *primary,
691 const struct drm_crtc_funcs *funcs)
697 crtc->invert_dimensions = false;
699 drm_modeset_lock_all(dev);
700 mutex_init(&crtc->mutex);
701 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
703 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
707 crtc->base.properties = &crtc->properties;
709 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
710 dev->mode_config.num_crtc++;
712 crtc->primary = primary;
714 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
717 drm_modeset_unlock_all(dev);
721 EXPORT_SYMBOL(drm_crtc_init_with_planes);
724 * drm_crtc_cleanup - Clean up the core crtc usage
725 * @crtc: CRTC to cleanup
727 * This function cleans up @crtc and removes it from the DRM mode setting
728 * core. Note that the function does *not* free the crtc structure itself,
729 * this is the responsibility of the caller.
731 void drm_crtc_cleanup(struct drm_crtc *crtc)
733 struct drm_device *dev = crtc->dev;
735 kfree(crtc->gamma_store);
736 crtc->gamma_store = NULL;
738 drm_mode_object_put(dev, &crtc->base);
739 list_del(&crtc->head);
740 dev->mode_config.num_crtc--;
742 EXPORT_SYMBOL(drm_crtc_cleanup);
745 * drm_crtc_index - find the index of a registered CRTC
746 * @crtc: CRTC to find index for
748 * Given a registered CRTC, return the index of that CRTC within a DRM
749 * device's list of CRTCs.
751 unsigned int drm_crtc_index(struct drm_crtc *crtc)
753 unsigned int index = 0;
754 struct drm_crtc *tmp;
756 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
765 EXPORT_SYMBOL(drm_crtc_index);
768 * drm_mode_remove - remove and free a mode
769 * @connector: connector list to modify
770 * @mode: mode to remove
772 * Remove @mode from @connector's mode list, then free it.
774 static void drm_mode_remove(struct drm_connector *connector,
775 struct drm_display_mode *mode)
777 list_del(&mode->head);
778 drm_mode_destroy(connector->dev, mode);
782 * drm_connector_init - Init a preallocated connector
784 * @connector: the connector to init
785 * @funcs: callbacks for this connector
786 * @connector_type: user visible type of the connector
788 * Initialises a preallocated connector. Connectors should be
789 * subclassed as part of driver connector objects.
792 * Zero on success, error code on failure.
794 int drm_connector_init(struct drm_device *dev,
795 struct drm_connector *connector,
796 const struct drm_connector_funcs *funcs,
800 struct ida *connector_ida =
801 &drm_connector_enum_list[connector_type].ida;
803 drm_modeset_lock_all(dev);
805 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
809 connector->base.properties = &connector->properties;
810 connector->dev = dev;
811 connector->funcs = funcs;
812 connector->connector_type = connector_type;
813 connector->connector_type_id =
814 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
815 if (connector->connector_type_id < 0) {
816 ret = connector->connector_type_id;
820 kasprintf(GFP_KERNEL, "%s-%d",
821 drm_connector_enum_list[connector_type].name,
822 connector->connector_type_id);
823 if (!connector->name) {
828 INIT_LIST_HEAD(&connector->probed_modes);
829 INIT_LIST_HEAD(&connector->modes);
830 connector->edid_blob_ptr = NULL;
831 connector->status = connector_status_unknown;
833 list_add_tail(&connector->head, &dev->mode_config.connector_list);
834 dev->mode_config.num_connector++;
836 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
837 drm_object_attach_property(&connector->base,
838 dev->mode_config.edid_property,
841 drm_object_attach_property(&connector->base,
842 dev->mode_config.dpms_property, 0);
846 drm_mode_object_put(dev, &connector->base);
849 drm_modeset_unlock_all(dev);
853 EXPORT_SYMBOL(drm_connector_init);
856 * drm_connector_cleanup - cleans up an initialised connector
857 * @connector: connector to cleanup
859 * Cleans up the connector but doesn't free the object.
861 void drm_connector_cleanup(struct drm_connector *connector)
863 struct drm_device *dev = connector->dev;
864 struct drm_display_mode *mode, *t;
866 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
867 drm_mode_remove(connector, mode);
869 list_for_each_entry_safe(mode, t, &connector->modes, head)
870 drm_mode_remove(connector, mode);
872 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
873 connector->connector_type_id);
875 drm_mode_object_put(dev, &connector->base);
876 kfree(connector->name);
877 connector->name = NULL;
878 list_del(&connector->head);
879 dev->mode_config.num_connector--;
881 EXPORT_SYMBOL(drm_connector_cleanup);
884 * drm_connector_unplug_all - unregister connector userspace interfaces
887 * This function unregisters all connector userspace interfaces in sysfs. Should
888 * be call when the device is disconnected, e.g. from an usb driver's
889 * ->disconnect callback.
891 void drm_connector_unplug_all(struct drm_device *dev)
893 struct drm_connector *connector;
895 /* taking the mode config mutex ends up in a clash with sysfs */
896 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
897 drm_sysfs_connector_remove(connector);
900 EXPORT_SYMBOL(drm_connector_unplug_all);
903 * drm_bridge_init - initialize a drm transcoder/bridge
905 * @bridge: transcoder/bridge to set up
906 * @funcs: bridge function table
908 * Initialises a preallocated bridge. Bridges should be
909 * subclassed as part of driver connector objects.
912 * Zero on success, error code on failure.
914 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
915 const struct drm_bridge_funcs *funcs)
919 drm_modeset_lock_all(dev);
921 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
926 bridge->funcs = funcs;
928 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
929 dev->mode_config.num_bridge++;
932 drm_modeset_unlock_all(dev);
935 EXPORT_SYMBOL(drm_bridge_init);
938 * drm_bridge_cleanup - cleans up an initialised bridge
939 * @bridge: bridge to cleanup
941 * Cleans up the bridge but doesn't free the object.
943 void drm_bridge_cleanup(struct drm_bridge *bridge)
945 struct drm_device *dev = bridge->dev;
947 drm_modeset_lock_all(dev);
948 drm_mode_object_put(dev, &bridge->base);
949 list_del(&bridge->head);
950 dev->mode_config.num_bridge--;
951 drm_modeset_unlock_all(dev);
953 EXPORT_SYMBOL(drm_bridge_cleanup);
956 * drm_encoder_init - Init a preallocated encoder
958 * @encoder: the encoder to init
959 * @funcs: callbacks for this encoder
960 * @encoder_type: user visible type of the encoder
962 * Initialises a preallocated encoder. Encoder should be
963 * subclassed as part of driver encoder objects.
966 * Zero on success, error code on failure.
968 int drm_encoder_init(struct drm_device *dev,
969 struct drm_encoder *encoder,
970 const struct drm_encoder_funcs *funcs,
975 drm_modeset_lock_all(dev);
977 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
982 encoder->encoder_type = encoder_type;
983 encoder->funcs = funcs;
984 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
985 drm_encoder_enum_list[encoder_type].name,
987 if (!encoder->name) {
992 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
993 dev->mode_config.num_encoder++;
997 drm_mode_object_put(dev, &encoder->base);
1000 drm_modeset_unlock_all(dev);
1004 EXPORT_SYMBOL(drm_encoder_init);
1007 * drm_encoder_cleanup - cleans up an initialised encoder
1008 * @encoder: encoder to cleanup
1010 * Cleans up the encoder but doesn't free the object.
1012 void drm_encoder_cleanup(struct drm_encoder *encoder)
1014 struct drm_device *dev = encoder->dev;
1015 drm_modeset_lock_all(dev);
1016 drm_mode_object_put(dev, &encoder->base);
1017 kfree(encoder->name);
1018 encoder->name = NULL;
1019 list_del(&encoder->head);
1020 dev->mode_config.num_encoder--;
1021 drm_modeset_unlock_all(dev);
1023 EXPORT_SYMBOL(drm_encoder_cleanup);
1026 * drm_universal_plane_init - Initialize a new universal plane object
1028 * @plane: plane object to init
1029 * @possible_crtcs: bitmask of possible CRTCs
1030 * @funcs: callbacks for the new plane
1031 * @formats: array of supported formats (%DRM_FORMAT_*)
1032 * @format_count: number of elements in @formats
1033 * @type: type of plane (overlay, primary, cursor)
1035 * Initializes a plane object of type @type.
1038 * Zero on success, error code on failure.
1040 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1041 unsigned long possible_crtcs,
1042 const struct drm_plane_funcs *funcs,
1043 const uint32_t *formats, uint32_t format_count,
1044 enum drm_plane_type type)
1048 drm_modeset_lock_all(dev);
1050 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1054 plane->base.properties = &plane->properties;
1056 plane->funcs = funcs;
1057 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1059 if (!plane->format_types) {
1060 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1061 drm_mode_object_put(dev, &plane->base);
1066 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1067 plane->format_count = format_count;
1068 plane->possible_crtcs = possible_crtcs;
1071 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1072 dev->mode_config.num_total_plane++;
1073 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1074 dev->mode_config.num_overlay_plane++;
1076 drm_object_attach_property(&plane->base,
1077 dev->mode_config.plane_type_property,
1081 drm_modeset_unlock_all(dev);
1085 EXPORT_SYMBOL(drm_universal_plane_init);
1088 * drm_plane_init - Initialize a legacy plane
1090 * @plane: plane object to init
1091 * @possible_crtcs: bitmask of possible CRTCs
1092 * @funcs: callbacks for the new plane
1093 * @formats: array of supported formats (%DRM_FORMAT_*)
1094 * @format_count: number of elements in @formats
1095 * @is_primary: plane type (primary vs overlay)
1097 * Legacy API to initialize a DRM plane.
1099 * New drivers should call drm_universal_plane_init() instead.
1102 * Zero on success, error code on failure.
1104 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1105 unsigned long possible_crtcs,
1106 const struct drm_plane_funcs *funcs,
1107 const uint32_t *formats, uint32_t format_count,
1110 enum drm_plane_type type;
1112 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1113 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1114 formats, format_count, type);
1116 EXPORT_SYMBOL(drm_plane_init);
1119 * drm_plane_cleanup - Clean up the core plane usage
1120 * @plane: plane to cleanup
1122 * This function cleans up @plane and removes it from the DRM mode setting
1123 * core. Note that the function does *not* free the plane structure itself,
1124 * this is the responsibility of the caller.
1126 void drm_plane_cleanup(struct drm_plane *plane)
1128 struct drm_device *dev = plane->dev;
1130 drm_modeset_lock_all(dev);
1131 kfree(plane->format_types);
1132 drm_mode_object_put(dev, &plane->base);
1134 BUG_ON(list_empty(&plane->head));
1136 list_del(&plane->head);
1137 dev->mode_config.num_total_plane--;
1138 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1139 dev->mode_config.num_overlay_plane--;
1140 drm_modeset_unlock_all(dev);
1142 EXPORT_SYMBOL(drm_plane_cleanup);
1145 * drm_plane_force_disable - Forcibly disable a plane
1146 * @plane: plane to disable
1148 * Forces the plane to be disabled.
1150 * Used when the plane's current framebuffer is destroyed,
1151 * and when restoring fbdev mode.
1153 void drm_plane_force_disable(struct drm_plane *plane)
1155 struct drm_framebuffer *old_fb = plane->fb;
1161 ret = plane->funcs->disable_plane(plane);
1163 DRM_ERROR("failed to disable plane with busy fb\n");
1166 /* disconnect the plane from the fb and crtc: */
1167 __drm_framebuffer_unreference(old_fb);
1171 EXPORT_SYMBOL(drm_plane_force_disable);
1173 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1175 struct drm_property *edid;
1176 struct drm_property *dpms;
1179 * Standard properties (apply to all connectors)
1181 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1182 DRM_MODE_PROP_IMMUTABLE,
1184 dev->mode_config.edid_property = edid;
1186 dpms = drm_property_create_enum(dev, 0,
1187 "DPMS", drm_dpms_enum_list,
1188 ARRAY_SIZE(drm_dpms_enum_list));
1189 dev->mode_config.dpms_property = dpms;
1194 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1196 struct drm_property *type;
1199 * Standard properties (apply to all planes)
1201 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1202 "type", drm_plane_type_enum_list,
1203 ARRAY_SIZE(drm_plane_type_enum_list));
1204 dev->mode_config.plane_type_property = type;
1210 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1213 * Called by a driver the first time a DVI-I connector is made.
1215 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1217 struct drm_property *dvi_i_selector;
1218 struct drm_property *dvi_i_subconnector;
1220 if (dev->mode_config.dvi_i_select_subconnector_property)
1224 drm_property_create_enum(dev, 0,
1225 "select subconnector",
1226 drm_dvi_i_select_enum_list,
1227 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1228 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1230 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1232 drm_dvi_i_subconnector_enum_list,
1233 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1234 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1238 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1241 * drm_create_tv_properties - create TV specific connector properties
1243 * @num_modes: number of different TV formats (modes) supported
1244 * @modes: array of pointers to strings containing name of each format
1246 * Called by a driver's TV initialization routine, this function creates
1247 * the TV specific connector properties for a given device. Caller is
1248 * responsible for allocating a list of format names and passing them to
1251 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1254 struct drm_property *tv_selector;
1255 struct drm_property *tv_subconnector;
1258 if (dev->mode_config.tv_select_subconnector_property)
1262 * Basic connector properties
1264 tv_selector = drm_property_create_enum(dev, 0,
1265 "select subconnector",
1266 drm_tv_select_enum_list,
1267 ARRAY_SIZE(drm_tv_select_enum_list));
1268 dev->mode_config.tv_select_subconnector_property = tv_selector;
1271 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1273 drm_tv_subconnector_enum_list,
1274 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1275 dev->mode_config.tv_subconnector_property = tv_subconnector;
1278 * Other, TV specific properties: margins & TV modes.
1280 dev->mode_config.tv_left_margin_property =
1281 drm_property_create_range(dev, 0, "left margin", 0, 100);
1283 dev->mode_config.tv_right_margin_property =
1284 drm_property_create_range(dev, 0, "right margin", 0, 100);
1286 dev->mode_config.tv_top_margin_property =
1287 drm_property_create_range(dev, 0, "top margin", 0, 100);
1289 dev->mode_config.tv_bottom_margin_property =
1290 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1292 dev->mode_config.tv_mode_property =
1293 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1295 for (i = 0; i < num_modes; i++)
1296 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1299 dev->mode_config.tv_brightness_property =
1300 drm_property_create_range(dev, 0, "brightness", 0, 100);
1302 dev->mode_config.tv_contrast_property =
1303 drm_property_create_range(dev, 0, "contrast", 0, 100);
1305 dev->mode_config.tv_flicker_reduction_property =
1306 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1308 dev->mode_config.tv_overscan_property =
1309 drm_property_create_range(dev, 0, "overscan", 0, 100);
1311 dev->mode_config.tv_saturation_property =
1312 drm_property_create_range(dev, 0, "saturation", 0, 100);
1314 dev->mode_config.tv_hue_property =
1315 drm_property_create_range(dev, 0, "hue", 0, 100);
1319 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1322 * drm_mode_create_scaling_mode_property - create scaling mode property
1325 * Called by a driver the first time it's needed, must be attached to desired
1328 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1330 struct drm_property *scaling_mode;
1332 if (dev->mode_config.scaling_mode_property)
1336 drm_property_create_enum(dev, 0, "scaling mode",
1337 drm_scaling_mode_enum_list,
1338 ARRAY_SIZE(drm_scaling_mode_enum_list));
1340 dev->mode_config.scaling_mode_property = scaling_mode;
1344 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1347 * drm_mode_create_dirty_property - create dirty property
1350 * Called by a driver the first time it's needed, must be attached to desired
1353 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1355 struct drm_property *dirty_info;
1357 if (dev->mode_config.dirty_info_property)
1361 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1363 drm_dirty_info_enum_list,
1364 ARRAY_SIZE(drm_dirty_info_enum_list));
1365 dev->mode_config.dirty_info_property = dirty_info;
1369 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1371 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1373 uint32_t total_objects = 0;
1375 total_objects += dev->mode_config.num_crtc;
1376 total_objects += dev->mode_config.num_connector;
1377 total_objects += dev->mode_config.num_encoder;
1378 total_objects += dev->mode_config.num_bridge;
1380 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1381 if (!group->id_list)
1384 group->num_crtcs = 0;
1385 group->num_connectors = 0;
1386 group->num_encoders = 0;
1387 group->num_bridges = 0;
1391 void drm_mode_group_destroy(struct drm_mode_group *group)
1393 kfree(group->id_list);
1394 group->id_list = NULL;
1398 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1399 * the drm core's responsibility to set up mode control groups.
1401 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1402 struct drm_mode_group *group)
1404 struct drm_crtc *crtc;
1405 struct drm_encoder *encoder;
1406 struct drm_connector *connector;
1407 struct drm_bridge *bridge;
1410 if ((ret = drm_mode_group_init(dev, group)))
1413 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1414 group->id_list[group->num_crtcs++] = crtc->base.id;
1416 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1417 group->id_list[group->num_crtcs + group->num_encoders++] =
1420 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1421 group->id_list[group->num_crtcs + group->num_encoders +
1422 group->num_connectors++] = connector->base.id;
1424 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1425 group->id_list[group->num_crtcs + group->num_encoders +
1426 group->num_connectors + group->num_bridges++] =
1431 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1434 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1435 * @out: drm_mode_modeinfo struct to return to the user
1436 * @in: drm_display_mode to use
1438 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1441 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1442 const struct drm_display_mode *in)
1444 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1445 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1446 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1447 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1448 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1449 "timing values too large for mode info\n");
1451 out->clock = in->clock;
1452 out->hdisplay = in->hdisplay;
1453 out->hsync_start = in->hsync_start;
1454 out->hsync_end = in->hsync_end;
1455 out->htotal = in->htotal;
1456 out->hskew = in->hskew;
1457 out->vdisplay = in->vdisplay;
1458 out->vsync_start = in->vsync_start;
1459 out->vsync_end = in->vsync_end;
1460 out->vtotal = in->vtotal;
1461 out->vscan = in->vscan;
1462 out->vrefresh = in->vrefresh;
1463 out->flags = in->flags;
1464 out->type = in->type;
1465 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1466 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1470 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1471 * @out: drm_display_mode to return to the user
1472 * @in: drm_mode_modeinfo to use
1474 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1478 * Zero on success, errno on failure.
1480 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1481 const struct drm_mode_modeinfo *in)
1483 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1486 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1489 out->clock = in->clock;
1490 out->hdisplay = in->hdisplay;
1491 out->hsync_start = in->hsync_start;
1492 out->hsync_end = in->hsync_end;
1493 out->htotal = in->htotal;
1494 out->hskew = in->hskew;
1495 out->vdisplay = in->vdisplay;
1496 out->vsync_start = in->vsync_start;
1497 out->vsync_end = in->vsync_end;
1498 out->vtotal = in->vtotal;
1499 out->vscan = in->vscan;
1500 out->vrefresh = in->vrefresh;
1501 out->flags = in->flags;
1502 out->type = in->type;
1503 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1504 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1510 * drm_mode_getresources - get graphics configuration
1511 * @dev: drm device for the ioctl
1512 * @data: data pointer for the ioctl
1513 * @file_priv: drm file for the ioctl call
1515 * Construct a set of configuration description structures and return
1516 * them to the user, including CRTC, connector and framebuffer configuration.
1518 * Called by the user via ioctl.
1521 * Zero on success, errno on failure.
1523 int drm_mode_getresources(struct drm_device *dev, void *data,
1524 struct drm_file *file_priv)
1526 struct drm_mode_card_res *card_res = data;
1527 struct list_head *lh;
1528 struct drm_framebuffer *fb;
1529 struct drm_connector *connector;
1530 struct drm_crtc *crtc;
1531 struct drm_encoder *encoder;
1533 int connector_count = 0;
1536 int encoder_count = 0;
1538 uint32_t __user *fb_id;
1539 uint32_t __user *crtc_id;
1540 uint32_t __user *connector_id;
1541 uint32_t __user *encoder_id;
1542 struct drm_mode_group *mode_group;
1544 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1548 mutex_lock(&file_priv->fbs_lock);
1550 * For the non-control nodes we need to limit the list of resources
1551 * by IDs in the group list for this node
1553 list_for_each(lh, &file_priv->fbs)
1556 /* handle this in 4 parts */
1558 if (card_res->count_fbs >= fb_count) {
1560 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1561 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1562 if (put_user(fb->base.id, fb_id + copied)) {
1563 mutex_unlock(&file_priv->fbs_lock);
1569 card_res->count_fbs = fb_count;
1570 mutex_unlock(&file_priv->fbs_lock);
1572 drm_modeset_lock_all(dev);
1573 if (!drm_is_primary_client(file_priv)) {
1576 list_for_each(lh, &dev->mode_config.crtc_list)
1579 list_for_each(lh, &dev->mode_config.connector_list)
1582 list_for_each(lh, &dev->mode_config.encoder_list)
1586 mode_group = &file_priv->master->minor->mode_group;
1587 crtc_count = mode_group->num_crtcs;
1588 connector_count = mode_group->num_connectors;
1589 encoder_count = mode_group->num_encoders;
1592 card_res->max_height = dev->mode_config.max_height;
1593 card_res->min_height = dev->mode_config.min_height;
1594 card_res->max_width = dev->mode_config.max_width;
1595 card_res->min_width = dev->mode_config.min_width;
1598 if (card_res->count_crtcs >= crtc_count) {
1600 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1602 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1604 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1605 if (put_user(crtc->base.id, crtc_id + copied)) {
1612 for (i = 0; i < mode_group->num_crtcs; i++) {
1613 if (put_user(mode_group->id_list[i],
1614 crtc_id + copied)) {
1622 card_res->count_crtcs = crtc_count;
1625 if (card_res->count_encoders >= encoder_count) {
1627 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1629 list_for_each_entry(encoder,
1630 &dev->mode_config.encoder_list,
1632 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1634 if (put_user(encoder->base.id, encoder_id +
1642 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1643 if (put_user(mode_group->id_list[i],
1644 encoder_id + copied)) {
1653 card_res->count_encoders = encoder_count;
1656 if (card_res->count_connectors >= connector_count) {
1658 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1660 list_for_each_entry(connector,
1661 &dev->mode_config.connector_list,
1663 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1666 if (put_user(connector->base.id,
1667 connector_id + copied)) {
1674 int start = mode_group->num_crtcs +
1675 mode_group->num_encoders;
1676 for (i = start; i < start + mode_group->num_connectors; i++) {
1677 if (put_user(mode_group->id_list[i],
1678 connector_id + copied)) {
1686 card_res->count_connectors = connector_count;
1688 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1689 card_res->count_connectors, card_res->count_encoders);
1692 drm_modeset_unlock_all(dev);
1697 * drm_mode_getcrtc - get CRTC configuration
1698 * @dev: drm device for the ioctl
1699 * @data: data pointer for the ioctl
1700 * @file_priv: drm file for the ioctl call
1702 * Construct a CRTC configuration structure to return to the user.
1704 * Called by the user via ioctl.
1707 * Zero on success, errno on failure.
1709 int drm_mode_getcrtc(struct drm_device *dev,
1710 void *data, struct drm_file *file_priv)
1712 struct drm_mode_crtc *crtc_resp = data;
1713 struct drm_crtc *crtc;
1716 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1719 drm_modeset_lock_all(dev);
1721 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1727 crtc_resp->x = crtc->x;
1728 crtc_resp->y = crtc->y;
1729 crtc_resp->gamma_size = crtc->gamma_size;
1730 if (crtc->primary->fb)
1731 crtc_resp->fb_id = crtc->primary->fb->base.id;
1733 crtc_resp->fb_id = 0;
1735 if (crtc->enabled) {
1737 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1738 crtc_resp->mode_valid = 1;
1741 crtc_resp->mode_valid = 0;
1745 drm_modeset_unlock_all(dev);
1749 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1750 const struct drm_file *file_priv)
1753 * If user-space hasn't configured the driver to expose the stereo 3D
1754 * modes, don't expose them.
1756 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1763 * drm_mode_getconnector - get connector configuration
1764 * @dev: drm device for the ioctl
1765 * @data: data pointer for the ioctl
1766 * @file_priv: drm file for the ioctl call
1768 * Construct a connector configuration structure to return to the user.
1770 * Called by the user via ioctl.
1773 * Zero on success, errno on failure.
1775 int drm_mode_getconnector(struct drm_device *dev, void *data,
1776 struct drm_file *file_priv)
1778 struct drm_mode_get_connector *out_resp = data;
1779 struct drm_connector *connector;
1780 struct drm_display_mode *mode;
1782 int props_count = 0;
1783 int encoders_count = 0;
1787 struct drm_mode_modeinfo u_mode;
1788 struct drm_mode_modeinfo __user *mode_ptr;
1789 uint32_t __user *prop_ptr;
1790 uint64_t __user *prop_values;
1791 uint32_t __user *encoder_ptr;
1793 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1796 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1798 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1800 mutex_lock(&dev->mode_config.mutex);
1801 mutex_lock(&dev->mode_config.connection_mutex);
1803 connector = drm_connector_find(dev, out_resp->connector_id);
1809 props_count = connector->properties.count;
1811 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1812 if (connector->encoder_ids[i] != 0) {
1817 if (out_resp->count_modes == 0) {
1818 connector->funcs->fill_modes(connector,
1819 dev->mode_config.max_width,
1820 dev->mode_config.max_height);
1823 /* delayed so we get modes regardless of pre-fill_modes state */
1824 list_for_each_entry(mode, &connector->modes, head)
1825 if (drm_mode_expose_to_userspace(mode, file_priv))
1828 out_resp->connector_id = connector->base.id;
1829 out_resp->connector_type = connector->connector_type;
1830 out_resp->connector_type_id = connector->connector_type_id;
1831 out_resp->mm_width = connector->display_info.width_mm;
1832 out_resp->mm_height = connector->display_info.height_mm;
1833 out_resp->subpixel = connector->display_info.subpixel_order;
1834 out_resp->connection = connector->status;
1835 if (connector->encoder)
1836 out_resp->encoder_id = connector->encoder->base.id;
1838 out_resp->encoder_id = 0;
1841 * This ioctl is called twice, once to determine how much space is
1842 * needed, and the 2nd time to fill it.
1844 if ((out_resp->count_modes >= mode_count) && mode_count) {
1846 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1847 list_for_each_entry(mode, &connector->modes, head) {
1848 if (!drm_mode_expose_to_userspace(mode, file_priv))
1851 drm_crtc_convert_to_umode(&u_mode, mode);
1852 if (copy_to_user(mode_ptr + copied,
1853 &u_mode, sizeof(u_mode))) {
1860 out_resp->count_modes = mode_count;
1862 if ((out_resp->count_props >= props_count) && props_count) {
1864 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1865 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1866 for (i = 0; i < connector->properties.count; i++) {
1867 if (put_user(connector->properties.ids[i],
1868 prop_ptr + copied)) {
1873 if (put_user(connector->properties.values[i],
1874 prop_values + copied)) {
1881 out_resp->count_props = props_count;
1883 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1885 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1886 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1887 if (connector->encoder_ids[i] != 0) {
1888 if (put_user(connector->encoder_ids[i],
1889 encoder_ptr + copied)) {
1897 out_resp->count_encoders = encoders_count;
1900 mutex_unlock(&dev->mode_config.connection_mutex);
1901 mutex_unlock(&dev->mode_config.mutex);
1907 * drm_mode_getencoder - get encoder configuration
1908 * @dev: drm device for the ioctl
1909 * @data: data pointer for the ioctl
1910 * @file_priv: drm file for the ioctl call
1912 * Construct a encoder configuration structure to return to the user.
1914 * Called by the user via ioctl.
1917 * Zero on success, errno on failure.
1919 int drm_mode_getencoder(struct drm_device *dev, void *data,
1920 struct drm_file *file_priv)
1922 struct drm_mode_get_encoder *enc_resp = data;
1923 struct drm_encoder *encoder;
1926 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1929 drm_modeset_lock_all(dev);
1930 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1937 enc_resp->crtc_id = encoder->crtc->base.id;
1939 enc_resp->crtc_id = 0;
1940 enc_resp->encoder_type = encoder->encoder_type;
1941 enc_resp->encoder_id = encoder->base.id;
1942 enc_resp->possible_crtcs = encoder->possible_crtcs;
1943 enc_resp->possible_clones = encoder->possible_clones;
1946 drm_modeset_unlock_all(dev);
1951 * drm_mode_getplane_res - enumerate all plane resources
1954 * @file_priv: DRM file info
1956 * Construct a list of plane ids to return to the user.
1958 * Called by the user via ioctl.
1961 * Zero on success, errno on failure.
1963 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1964 struct drm_file *file_priv)
1966 struct drm_mode_get_plane_res *plane_resp = data;
1967 struct drm_mode_config *config;
1968 struct drm_plane *plane;
1969 uint32_t __user *plane_ptr;
1970 int copied = 0, ret = 0;
1971 unsigned num_planes;
1973 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1976 drm_modeset_lock_all(dev);
1977 config = &dev->mode_config;
1979 if (file_priv->universal_planes)
1980 num_planes = config->num_total_plane;
1982 num_planes = config->num_overlay_plane;
1985 * This ioctl is called twice, once to determine how much space is
1986 * needed, and the 2nd time to fill it.
1989 (plane_resp->count_planes >= num_planes)) {
1990 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1992 list_for_each_entry(plane, &config->plane_list, head) {
1994 * Unless userspace set the 'universal planes'
1995 * capability bit, only advertise overlays.
1997 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1998 !file_priv->universal_planes)
2001 if (put_user(plane->base.id, plane_ptr + copied)) {
2008 plane_resp->count_planes = num_planes;
2011 drm_modeset_unlock_all(dev);
2016 * drm_mode_getplane - get plane configuration
2019 * @file_priv: DRM file info
2021 * Construct a plane configuration structure to return to the user.
2023 * Called by the user via ioctl.
2026 * Zero on success, errno on failure.
2028 int drm_mode_getplane(struct drm_device *dev, void *data,
2029 struct drm_file *file_priv)
2031 struct drm_mode_get_plane *plane_resp = data;
2032 struct drm_plane *plane;
2033 uint32_t __user *format_ptr;
2036 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2039 drm_modeset_lock_all(dev);
2040 plane = drm_plane_find(dev, plane_resp->plane_id);
2047 plane_resp->crtc_id = plane->crtc->base.id;
2049 plane_resp->crtc_id = 0;
2052 plane_resp->fb_id = plane->fb->base.id;
2054 plane_resp->fb_id = 0;
2056 plane_resp->plane_id = plane->base.id;
2057 plane_resp->possible_crtcs = plane->possible_crtcs;
2058 plane_resp->gamma_size = 0;
2061 * This ioctl is called twice, once to determine how much space is
2062 * needed, and the 2nd time to fill it.
2064 if (plane->format_count &&
2065 (plane_resp->count_format_types >= plane->format_count)) {
2066 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2067 if (copy_to_user(format_ptr,
2068 plane->format_types,
2069 sizeof(uint32_t) * plane->format_count)) {
2074 plane_resp->count_format_types = plane->format_count;
2077 drm_modeset_unlock_all(dev);
2082 * drm_mode_setplane - configure a plane's configuration
2084 * @data: ioctl data*
2085 * @file_priv: DRM file info
2087 * Set plane configuration, including placement, fb, scaling, and other factors.
2088 * Or pass a NULL fb to disable.
2091 * Zero on success, errno on failure.
2093 int drm_mode_setplane(struct drm_device *dev, void *data,
2094 struct drm_file *file_priv)
2096 struct drm_mode_set_plane *plane_req = data;
2097 struct drm_plane *plane;
2098 struct drm_crtc *crtc;
2099 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2101 unsigned int fb_width, fb_height;
2104 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2108 * First, find the plane, crtc, and fb objects. If not available,
2109 * we don't bother to call the driver.
2111 plane = drm_plane_find(dev, plane_req->plane_id);
2113 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2114 plane_req->plane_id);
2118 /* No fb means shut it down */
2119 if (!plane_req->fb_id) {
2120 drm_modeset_lock_all(dev);
2122 ret = plane->funcs->disable_plane(plane);
2129 drm_modeset_unlock_all(dev);
2133 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2135 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2136 plane_req->crtc_id);
2141 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2143 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2149 /* Check whether this plane supports the fb pixel format. */
2150 for (i = 0; i < plane->format_count; i++)
2151 if (fb->pixel_format == plane->format_types[i])
2153 if (i == plane->format_count) {
2154 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2155 drm_get_format_name(fb->pixel_format));
2160 fb_width = fb->width << 16;
2161 fb_height = fb->height << 16;
2163 /* Make sure source coordinates are inside the fb. */
2164 if (plane_req->src_w > fb_width ||
2165 plane_req->src_x > fb_width - plane_req->src_w ||
2166 plane_req->src_h > fb_height ||
2167 plane_req->src_y > fb_height - plane_req->src_h) {
2168 DRM_DEBUG_KMS("Invalid source coordinates "
2169 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2170 plane_req->src_w >> 16,
2171 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2172 plane_req->src_h >> 16,
2173 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2174 plane_req->src_x >> 16,
2175 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2176 plane_req->src_y >> 16,
2177 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2182 /* Give drivers some help against integer overflows */
2183 if (plane_req->crtc_w > INT_MAX ||
2184 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2185 plane_req->crtc_h > INT_MAX ||
2186 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2187 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2188 plane_req->crtc_w, plane_req->crtc_h,
2189 plane_req->crtc_x, plane_req->crtc_y);
2194 drm_modeset_lock_all(dev);
2196 ret = plane->funcs->update_plane(plane, crtc, fb,
2197 plane_req->crtc_x, plane_req->crtc_y,
2198 plane_req->crtc_w, plane_req->crtc_h,
2199 plane_req->src_x, plane_req->src_y,
2200 plane_req->src_w, plane_req->src_h);
2208 drm_modeset_unlock_all(dev);
2212 drm_framebuffer_unreference(fb);
2214 drm_framebuffer_unreference(old_fb);
2220 * drm_mode_set_config_internal - helper to call ->set_config
2221 * @set: modeset config to set
2223 * This is a little helper to wrap internal calls to the ->set_config driver
2224 * interface. The only thing it adds is correct refcounting dance.
2227 * Zero on success, errno on failure.
2229 int drm_mode_set_config_internal(struct drm_mode_set *set)
2231 struct drm_crtc *crtc = set->crtc;
2232 struct drm_framebuffer *fb;
2233 struct drm_crtc *tmp;
2237 * NOTE: ->set_config can also disable other crtcs (if we steal all
2238 * connectors from it), hence we need to refcount the fbs across all
2239 * crtcs. Atomic modeset will have saner semantics ...
2241 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2242 tmp->old_fb = tmp->primary->fb;
2246 ret = crtc->funcs->set_config(set);
2248 crtc->primary->crtc = crtc;
2249 crtc->primary->fb = fb;
2252 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2253 if (tmp->primary->fb)
2254 drm_framebuffer_reference(tmp->primary->fb);
2256 drm_framebuffer_unreference(tmp->old_fb);
2261 EXPORT_SYMBOL(drm_mode_set_config_internal);
2264 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2266 * @crtc: CRTC that framebuffer will be displayed on
2269 * @mode: mode that framebuffer will be displayed under
2270 * @fb: framebuffer to check size of
2272 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2274 const struct drm_display_mode *mode,
2275 const struct drm_framebuffer *fb)
2278 int hdisplay, vdisplay;
2280 hdisplay = mode->hdisplay;
2281 vdisplay = mode->vdisplay;
2283 if (drm_mode_is_stereo(mode)) {
2284 struct drm_display_mode adjusted = *mode;
2286 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2287 hdisplay = adjusted.crtc_hdisplay;
2288 vdisplay = adjusted.crtc_vdisplay;
2291 if (crtc->invert_dimensions)
2292 swap(hdisplay, vdisplay);
2294 if (hdisplay > fb->width ||
2295 vdisplay > fb->height ||
2296 x > fb->width - hdisplay ||
2297 y > fb->height - vdisplay) {
2298 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2299 fb->width, fb->height, hdisplay, vdisplay, x, y,
2300 crtc->invert_dimensions ? " (inverted)" : "");
2306 EXPORT_SYMBOL(drm_crtc_check_viewport);
2309 * drm_mode_setcrtc - set CRTC configuration
2310 * @dev: drm device for the ioctl
2311 * @data: data pointer for the ioctl
2312 * @file_priv: drm file for the ioctl call
2314 * Build a new CRTC configuration based on user request.
2316 * Called by the user via ioctl.
2319 * Zero on success, errno on failure.
2321 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2322 struct drm_file *file_priv)
2324 struct drm_mode_config *config = &dev->mode_config;
2325 struct drm_mode_crtc *crtc_req = data;
2326 struct drm_crtc *crtc;
2327 struct drm_connector **connector_set = NULL, *connector;
2328 struct drm_framebuffer *fb = NULL;
2329 struct drm_display_mode *mode = NULL;
2330 struct drm_mode_set set;
2331 uint32_t __user *set_connectors_ptr;
2335 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2338 /* For some reason crtc x/y offsets are signed internally. */
2339 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2342 drm_modeset_lock_all(dev);
2343 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2345 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2349 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2351 if (crtc_req->mode_valid) {
2352 /* If we have a mode we need a framebuffer. */
2353 /* If we pass -1, set the mode with the currently bound fb */
2354 if (crtc_req->fb_id == -1) {
2355 if (!crtc->primary->fb) {
2356 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2360 fb = crtc->primary->fb;
2361 /* Make refcounting symmetric with the lookup path. */
2362 drm_framebuffer_reference(fb);
2364 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2366 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2373 mode = drm_mode_create(dev);
2379 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2381 DRM_DEBUG_KMS("Invalid mode\n");
2385 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2387 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2394 if (crtc_req->count_connectors == 0 && mode) {
2395 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2400 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2401 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2402 crtc_req->count_connectors);
2407 if (crtc_req->count_connectors > 0) {
2410 /* Avoid unbounded kernel memory allocation */
2411 if (crtc_req->count_connectors > config->num_connector) {
2416 connector_set = kmalloc(crtc_req->count_connectors *
2417 sizeof(struct drm_connector *),
2419 if (!connector_set) {
2424 for (i = 0; i < crtc_req->count_connectors; i++) {
2425 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2426 if (get_user(out_id, &set_connectors_ptr[i])) {
2431 connector = drm_connector_find(dev, out_id);
2433 DRM_DEBUG_KMS("Connector id %d unknown\n",
2438 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2442 connector_set[i] = connector;
2447 set.x = crtc_req->x;
2448 set.y = crtc_req->y;
2450 set.connectors = connector_set;
2451 set.num_connectors = crtc_req->count_connectors;
2453 ret = drm_mode_set_config_internal(&set);
2457 drm_framebuffer_unreference(fb);
2459 kfree(connector_set);
2460 drm_mode_destroy(dev, mode);
2461 drm_modeset_unlock_all(dev);
2465 static int drm_mode_cursor_common(struct drm_device *dev,
2466 struct drm_mode_cursor2 *req,
2467 struct drm_file *file_priv)
2469 struct drm_crtc *crtc;
2472 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2475 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2478 crtc = drm_crtc_find(dev, req->crtc_id);
2480 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2484 mutex_lock(&crtc->mutex);
2485 if (req->flags & DRM_MODE_CURSOR_BO) {
2486 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2490 /* Turns off the cursor if handle is 0 */
2491 if (crtc->funcs->cursor_set2)
2492 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2493 req->width, req->height, req->hot_x, req->hot_y);
2495 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2496 req->width, req->height);
2499 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2500 if (crtc->funcs->cursor_move) {
2501 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2508 mutex_unlock(&crtc->mutex);
2516 * drm_mode_cursor_ioctl - set CRTC's cursor 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 * Set the cursor configuration based on user request.
2523 * Called by the user via ioctl.
2526 * Zero on success, errno on failure.
2528 int drm_mode_cursor_ioctl(struct drm_device *dev,
2529 void *data, struct drm_file *file_priv)
2531 struct drm_mode_cursor *req = data;
2532 struct drm_mode_cursor2 new_req;
2534 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2535 new_req.hot_x = new_req.hot_y = 0;
2537 return drm_mode_cursor_common(dev, &new_req, file_priv);
2541 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2542 * @dev: drm device for the ioctl
2543 * @data: data pointer for the ioctl
2544 * @file_priv: drm file for the ioctl call
2546 * Set the cursor configuration based on user request. This implements the 2nd
2547 * version of the cursor ioctl, which allows userspace to additionally specify
2548 * the hotspot of the pointer.
2550 * Called by the user via ioctl.
2553 * Zero on success, errno on failure.
2555 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2556 void *data, struct drm_file *file_priv)
2558 struct drm_mode_cursor2 *req = data;
2559 return drm_mode_cursor_common(dev, req, file_priv);
2563 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2564 * @bpp: bits per pixels
2565 * @depth: bit depth per pixel
2567 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2568 * Useful in fbdev emulation code, since that deals in those values.
2570 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2576 fmt = DRM_FORMAT_C8;
2580 fmt = DRM_FORMAT_XRGB1555;
2582 fmt = DRM_FORMAT_RGB565;
2585 fmt = DRM_FORMAT_RGB888;
2589 fmt = DRM_FORMAT_XRGB8888;
2590 else if (depth == 30)
2591 fmt = DRM_FORMAT_XRGB2101010;
2593 fmt = DRM_FORMAT_ARGB8888;
2596 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2597 fmt = DRM_FORMAT_XRGB8888;
2603 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2606 * drm_mode_addfb - add an FB to the graphics configuration
2607 * @dev: drm device for the ioctl
2608 * @data: data pointer for the ioctl
2609 * @file_priv: drm file for the ioctl call
2611 * Add a new FB to the specified CRTC, given a user request. This is the
2612 * original addfb ioclt which only supported RGB formats.
2614 * Called by the user via ioctl.
2617 * Zero on success, errno on failure.
2619 int drm_mode_addfb(struct drm_device *dev,
2620 void *data, struct drm_file *file_priv)
2622 struct drm_mode_fb_cmd *or = data;
2623 struct drm_mode_fb_cmd2 r = {};
2624 struct drm_mode_config *config = &dev->mode_config;
2625 struct drm_framebuffer *fb;
2628 /* Use new struct with format internally */
2629 r.fb_id = or->fb_id;
2630 r.width = or->width;
2631 r.height = or->height;
2632 r.pitches[0] = or->pitch;
2633 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2634 r.handles[0] = or->handle;
2636 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2639 if ((config->min_width > r.width) || (r.width > config->max_width))
2642 if ((config->min_height > r.height) || (r.height > config->max_height))
2645 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2647 DRM_DEBUG_KMS("could not create framebuffer\n");
2651 mutex_lock(&file_priv->fbs_lock);
2652 or->fb_id = fb->base.id;
2653 list_add(&fb->filp_head, &file_priv->fbs);
2654 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2655 mutex_unlock(&file_priv->fbs_lock);
2660 static int format_check(const struct drm_mode_fb_cmd2 *r)
2662 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2666 case DRM_FORMAT_RGB332:
2667 case DRM_FORMAT_BGR233:
2668 case DRM_FORMAT_XRGB4444:
2669 case DRM_FORMAT_XBGR4444:
2670 case DRM_FORMAT_RGBX4444:
2671 case DRM_FORMAT_BGRX4444:
2672 case DRM_FORMAT_ARGB4444:
2673 case DRM_FORMAT_ABGR4444:
2674 case DRM_FORMAT_RGBA4444:
2675 case DRM_FORMAT_BGRA4444:
2676 case DRM_FORMAT_XRGB1555:
2677 case DRM_FORMAT_XBGR1555:
2678 case DRM_FORMAT_RGBX5551:
2679 case DRM_FORMAT_BGRX5551:
2680 case DRM_FORMAT_ARGB1555:
2681 case DRM_FORMAT_ABGR1555:
2682 case DRM_FORMAT_RGBA5551:
2683 case DRM_FORMAT_BGRA5551:
2684 case DRM_FORMAT_RGB565:
2685 case DRM_FORMAT_BGR565:
2686 case DRM_FORMAT_RGB888:
2687 case DRM_FORMAT_BGR888:
2688 case DRM_FORMAT_XRGB8888:
2689 case DRM_FORMAT_XBGR8888:
2690 case DRM_FORMAT_RGBX8888:
2691 case DRM_FORMAT_BGRX8888:
2692 case DRM_FORMAT_ARGB8888:
2693 case DRM_FORMAT_ABGR8888:
2694 case DRM_FORMAT_RGBA8888:
2695 case DRM_FORMAT_BGRA8888:
2696 case DRM_FORMAT_XRGB2101010:
2697 case DRM_FORMAT_XBGR2101010:
2698 case DRM_FORMAT_RGBX1010102:
2699 case DRM_FORMAT_BGRX1010102:
2700 case DRM_FORMAT_ARGB2101010:
2701 case DRM_FORMAT_ABGR2101010:
2702 case DRM_FORMAT_RGBA1010102:
2703 case DRM_FORMAT_BGRA1010102:
2704 case DRM_FORMAT_YUYV:
2705 case DRM_FORMAT_YVYU:
2706 case DRM_FORMAT_UYVY:
2707 case DRM_FORMAT_VYUY:
2708 case DRM_FORMAT_AYUV:
2709 case DRM_FORMAT_NV12:
2710 case DRM_FORMAT_NV21:
2711 case DRM_FORMAT_NV16:
2712 case DRM_FORMAT_NV61:
2713 case DRM_FORMAT_NV24:
2714 case DRM_FORMAT_NV42:
2715 case DRM_FORMAT_YUV410:
2716 case DRM_FORMAT_YVU410:
2717 case DRM_FORMAT_YUV411:
2718 case DRM_FORMAT_YVU411:
2719 case DRM_FORMAT_YUV420:
2720 case DRM_FORMAT_YVU420:
2721 case DRM_FORMAT_YUV422:
2722 case DRM_FORMAT_YVU422:
2723 case DRM_FORMAT_YUV444:
2724 case DRM_FORMAT_YVU444:
2727 DRM_DEBUG_KMS("invalid pixel format %s\n",
2728 drm_get_format_name(r->pixel_format));
2733 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2735 int ret, hsub, vsub, num_planes, i;
2737 ret = format_check(r);
2739 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2740 drm_get_format_name(r->pixel_format));
2744 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2745 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2746 num_planes = drm_format_num_planes(r->pixel_format);
2748 if (r->width == 0 || r->width % hsub) {
2749 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2753 if (r->height == 0 || r->height % vsub) {
2754 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2758 for (i = 0; i < num_planes; i++) {
2759 unsigned int width = r->width / (i != 0 ? hsub : 1);
2760 unsigned int height = r->height / (i != 0 ? vsub : 1);
2761 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2763 if (!r->handles[i]) {
2764 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2768 if ((uint64_t) width * cpp > UINT_MAX)
2771 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2774 if (r->pitches[i] < width * cpp) {
2775 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2784 * drm_mode_addfb2 - add an FB to the graphics configuration
2785 * @dev: drm device for the ioctl
2786 * @data: data pointer for the ioctl
2787 * @file_priv: drm file for the ioctl call
2789 * Add a new FB to the specified CRTC, given a user request with format. This is
2790 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2791 * and uses fourcc codes as pixel format specifiers.
2793 * Called by the user via ioctl.
2796 * Zero on success, errno on failure.
2798 int drm_mode_addfb2(struct drm_device *dev,
2799 void *data, struct drm_file *file_priv)
2801 struct drm_mode_fb_cmd2 *r = data;
2802 struct drm_mode_config *config = &dev->mode_config;
2803 struct drm_framebuffer *fb;
2806 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2809 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2810 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2814 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2815 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2816 r->width, config->min_width, config->max_width);
2819 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2820 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2821 r->height, config->min_height, config->max_height);
2825 ret = framebuffer_check(r);
2829 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2831 DRM_DEBUG_KMS("could not create framebuffer\n");
2835 mutex_lock(&file_priv->fbs_lock);
2836 r->fb_id = fb->base.id;
2837 list_add(&fb->filp_head, &file_priv->fbs);
2838 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2839 mutex_unlock(&file_priv->fbs_lock);
2846 * drm_mode_rmfb - remove an FB from the configuration
2847 * @dev: drm device for the ioctl
2848 * @data: data pointer for the ioctl
2849 * @file_priv: drm file for the ioctl call
2851 * Remove the FB specified by the user.
2853 * Called by the user via ioctl.
2856 * Zero on success, errno on failure.
2858 int drm_mode_rmfb(struct drm_device *dev,
2859 void *data, struct drm_file *file_priv)
2861 struct drm_framebuffer *fb = NULL;
2862 struct drm_framebuffer *fbl = NULL;
2863 uint32_t *id = data;
2866 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2869 mutex_lock(&file_priv->fbs_lock);
2870 mutex_lock(&dev->mode_config.fb_lock);
2871 fb = __drm_framebuffer_lookup(dev, *id);
2875 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2881 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2882 __drm_framebuffer_unregister(dev, fb);
2884 list_del_init(&fb->filp_head);
2885 mutex_unlock(&dev->mode_config.fb_lock);
2886 mutex_unlock(&file_priv->fbs_lock);
2888 drm_framebuffer_remove(fb);
2893 mutex_unlock(&dev->mode_config.fb_lock);
2894 mutex_unlock(&file_priv->fbs_lock);
2900 * drm_mode_getfb - get FB info
2901 * @dev: drm device for the ioctl
2902 * @data: data pointer for the ioctl
2903 * @file_priv: drm file for the ioctl call
2905 * Lookup the FB given its ID and return info about it.
2907 * Called by the user via ioctl.
2910 * Zero on success, errno on failure.
2912 int drm_mode_getfb(struct drm_device *dev,
2913 void *data, struct drm_file *file_priv)
2915 struct drm_mode_fb_cmd *r = data;
2916 struct drm_framebuffer *fb;
2919 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2922 fb = drm_framebuffer_lookup(dev, r->fb_id);
2926 r->height = fb->height;
2927 r->width = fb->width;
2928 r->depth = fb->depth;
2929 r->bpp = fb->bits_per_pixel;
2930 r->pitch = fb->pitches[0];
2931 if (fb->funcs->create_handle) {
2932 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2933 drm_is_control_client(file_priv)) {
2934 ret = fb->funcs->create_handle(fb, file_priv,
2937 /* GET_FB() is an unprivileged ioctl so we must not
2938 * return a buffer-handle to non-master processes! For
2939 * backwards-compatibility reasons, we cannot make
2940 * GET_FB() privileged, so just return an invalid handle
2941 * for non-masters. */
2949 drm_framebuffer_unreference(fb);
2955 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2956 * @dev: drm device for the ioctl
2957 * @data: data pointer for the ioctl
2958 * @file_priv: drm file for the ioctl call
2960 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2961 * rectangle list. Generic userspace which does frontbuffer rendering must call
2962 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2963 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2965 * Modesetting drivers which always update the frontbuffer do not need to
2966 * implement the corresponding ->dirty framebuffer callback.
2968 * Called by the user via ioctl.
2971 * Zero on success, errno on failure.
2973 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2974 void *data, struct drm_file *file_priv)
2976 struct drm_clip_rect __user *clips_ptr;
2977 struct drm_clip_rect *clips = NULL;
2978 struct drm_mode_fb_dirty_cmd *r = data;
2979 struct drm_framebuffer *fb;
2984 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2987 fb = drm_framebuffer_lookup(dev, r->fb_id);
2991 num_clips = r->num_clips;
2992 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2994 if (!num_clips != !clips_ptr) {
2999 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3001 /* If userspace annotates copy, clips must come in pairs */
3002 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3007 if (num_clips && clips_ptr) {
3008 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3012 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3018 ret = copy_from_user(clips, clips_ptr,
3019 num_clips * sizeof(*clips));
3026 if (fb->funcs->dirty) {
3027 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3036 drm_framebuffer_unreference(fb);
3043 * drm_fb_release - remove and free the FBs on this file
3044 * @priv: drm file for the ioctl
3046 * Destroy all the FBs associated with @filp.
3048 * Called by the user via ioctl.
3051 * Zero on success, errno on failure.
3053 void drm_fb_release(struct drm_file *priv)
3055 struct drm_device *dev = priv->minor->dev;
3056 struct drm_framebuffer *fb, *tfb;
3058 mutex_lock(&priv->fbs_lock);
3059 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3061 mutex_lock(&dev->mode_config.fb_lock);
3062 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3063 __drm_framebuffer_unregister(dev, fb);
3064 mutex_unlock(&dev->mode_config.fb_lock);
3066 list_del_init(&fb->filp_head);
3068 /* This will also drop the fpriv->fbs reference. */
3069 drm_framebuffer_remove(fb);
3071 mutex_unlock(&priv->fbs_lock);
3075 * drm_property_create - create a new property type
3077 * @flags: flags specifying the property type
3078 * @name: name of the property
3079 * @num_values: number of pre-defined values
3081 * This creates a new generic drm property which can then be attached to a drm
3082 * object with drm_object_attach_property. The returned property object must be
3083 * freed with drm_property_destroy.
3086 * A pointer to the newly created property on success, NULL on failure.
3088 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3089 const char *name, int num_values)
3091 struct drm_property *property = NULL;
3094 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3098 property->dev = dev;
3101 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3102 if (!property->values)
3106 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3110 property->flags = flags;
3111 property->num_values = num_values;
3112 INIT_LIST_HEAD(&property->enum_blob_list);
3115 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3116 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3119 list_add_tail(&property->head, &dev->mode_config.property_list);
3121 WARN_ON(!drm_property_type_valid(property));
3125 kfree(property->values);
3129 EXPORT_SYMBOL(drm_property_create);
3132 * drm_property_create - create a new enumeration property type
3134 * @flags: flags specifying the property type
3135 * @name: name of the property
3136 * @props: enumeration lists with property values
3137 * @num_values: number of pre-defined values
3139 * This creates a new generic drm property which can then be attached to a drm
3140 * object with drm_object_attach_property. The returned property object must be
3141 * freed with drm_property_destroy.
3143 * Userspace is only allowed to set one of the predefined values for enumeration
3147 * A pointer to the newly created property on success, NULL on failure.
3149 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3151 const struct drm_prop_enum_list *props,
3154 struct drm_property *property;
3157 flags |= DRM_MODE_PROP_ENUM;
3159 property = drm_property_create(dev, flags, name, num_values);
3163 for (i = 0; i < num_values; i++) {
3164 ret = drm_property_add_enum(property, i,
3168 drm_property_destroy(dev, property);
3175 EXPORT_SYMBOL(drm_property_create_enum);
3178 * drm_property_create - create a new bitmask property type
3180 * @flags: flags specifying the property type
3181 * @name: name of the property
3182 * @props: enumeration lists with property bitflags
3183 * @num_values: number of pre-defined values
3185 * This creates a new generic drm property which can then be attached to a drm
3186 * object with drm_object_attach_property. The returned property object must be
3187 * freed with drm_property_destroy.
3189 * Compared to plain enumeration properties userspace is allowed to set any
3190 * or'ed together combination of the predefined property bitflag values
3193 * A pointer to the newly created property on success, NULL on failure.
3195 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3196 int flags, const char *name,
3197 const struct drm_prop_enum_list *props,
3200 struct drm_property *property;
3203 flags |= DRM_MODE_PROP_BITMASK;
3205 property = drm_property_create(dev, flags, name, num_values);
3209 for (i = 0; i < num_values; i++) {
3210 ret = drm_property_add_enum(property, i,
3214 drm_property_destroy(dev, property);
3221 EXPORT_SYMBOL(drm_property_create_bitmask);
3223 static struct drm_property *property_create_range(struct drm_device *dev,
3224 int flags, const char *name,
3225 uint64_t min, uint64_t max)
3227 struct drm_property *property;
3229 property = drm_property_create(dev, flags, name, 2);
3233 property->values[0] = min;
3234 property->values[1] = max;
3240 * drm_property_create - create a new ranged property type
3242 * @flags: flags specifying the property type
3243 * @name: name of the property
3244 * @min: minimum value of the property
3245 * @max: maximum value of the property
3247 * This creates a new generic drm property which can then be attached to a drm
3248 * object with drm_object_attach_property. The returned property object must be
3249 * freed with drm_property_destroy.
3251 * Userspace is allowed to set any interger value in the (min, max) range
3255 * A pointer to the newly created property on success, NULL on failure.
3257 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3259 uint64_t min, uint64_t max)
3261 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3264 EXPORT_SYMBOL(drm_property_create_range);
3266 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3267 int flags, const char *name,
3268 int64_t min, int64_t max)
3270 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3271 name, I642U64(min), I642U64(max));
3273 EXPORT_SYMBOL(drm_property_create_signed_range);
3275 struct drm_property *drm_property_create_object(struct drm_device *dev,
3276 int flags, const char *name, uint32_t type)
3278 struct drm_property *property;
3280 flags |= DRM_MODE_PROP_OBJECT;
3282 property = drm_property_create(dev, flags, name, 1);
3286 property->values[0] = type;
3290 EXPORT_SYMBOL(drm_property_create_object);
3293 * drm_property_add_enum - add a possible value to an enumeration property
3294 * @property: enumeration property to change
3295 * @index: index of the new enumeration
3296 * @value: value of the new enumeration
3297 * @name: symbolic name of the new enumeration
3299 * This functions adds enumerations to a property.
3301 * It's use is deprecated, drivers should use one of the more specific helpers
3302 * to directly create the property with all enumerations already attached.
3305 * Zero on success, error code on failure.
3307 int drm_property_add_enum(struct drm_property *property, int index,
3308 uint64_t value, const char *name)
3310 struct drm_property_enum *prop_enum;
3312 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3313 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3317 * Bitmask enum properties have the additional constraint of values
3320 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3324 if (!list_empty(&property->enum_blob_list)) {
3325 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3326 if (prop_enum->value == value) {
3327 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3328 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3334 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3338 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3339 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3340 prop_enum->value = value;
3342 property->values[index] = value;
3343 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3346 EXPORT_SYMBOL(drm_property_add_enum);
3349 * drm_property_destroy - destroy a drm property
3351 * @property: property to destry
3353 * This function frees a property including any attached resources like
3354 * enumeration values.
3356 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3358 struct drm_property_enum *prop_enum, *pt;
3360 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3361 list_del(&prop_enum->head);
3365 if (property->num_values)
3366 kfree(property->values);
3367 drm_mode_object_put(dev, &property->base);
3368 list_del(&property->head);
3371 EXPORT_SYMBOL(drm_property_destroy);
3374 * drm_object_attach_property - attach a property to a modeset object
3375 * @obj: drm modeset object
3376 * @property: property to attach
3377 * @init_val: initial value of the property
3379 * This attaches the given property to the modeset object with the given initial
3380 * value. Currently this function cannot fail since the properties are stored in
3381 * a statically sized array.
3383 void drm_object_attach_property(struct drm_mode_object *obj,
3384 struct drm_property *property,
3387 int count = obj->properties->count;
3389 if (count == DRM_OBJECT_MAX_PROPERTY) {
3390 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3391 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3392 "you see this message on the same object type.\n",
3397 obj->properties->ids[count] = property->base.id;
3398 obj->properties->values[count] = init_val;
3399 obj->properties->count++;
3401 EXPORT_SYMBOL(drm_object_attach_property);
3404 * drm_object_property_set_value - set the value of a property
3405 * @obj: drm mode object to set property value for
3406 * @property: property to set
3407 * @val: value the property should be set to
3409 * This functions sets a given property on a given object. This function only
3410 * changes the software state of the property, it does not call into the
3411 * driver's ->set_property callback.
3414 * Zero on success, error code on failure.
3416 int drm_object_property_set_value(struct drm_mode_object *obj,
3417 struct drm_property *property, uint64_t val)
3421 for (i = 0; i < obj->properties->count; i++) {
3422 if (obj->properties->ids[i] == property->base.id) {
3423 obj->properties->values[i] = val;
3430 EXPORT_SYMBOL(drm_object_property_set_value);
3433 * drm_object_property_get_value - retrieve the value of a property
3434 * @obj: drm mode object to get property value from
3435 * @property: property to retrieve
3436 * @val: storage for the property value
3438 * This function retrieves the softare state of the given property for the given
3439 * property. Since there is no driver callback to retrieve the current property
3440 * value this might be out of sync with the hardware, depending upon the driver
3444 * Zero on success, error code on failure.
3446 int drm_object_property_get_value(struct drm_mode_object *obj,
3447 struct drm_property *property, uint64_t *val)
3451 for (i = 0; i < obj->properties->count; i++) {
3452 if (obj->properties->ids[i] == property->base.id) {
3453 *val = obj->properties->values[i];
3460 EXPORT_SYMBOL(drm_object_property_get_value);
3463 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3466 * @file_priv: DRM file info
3468 * This function retrieves the current value for an connectors's property.
3470 * Called by the user via ioctl.
3473 * Zero on success, errno on failure.
3475 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3476 void *data, struct drm_file *file_priv)
3478 struct drm_mode_get_property *out_resp = data;
3479 struct drm_property *property;
3482 int value_count = 0;
3485 struct drm_property_enum *prop_enum;
3486 struct drm_mode_property_enum __user *enum_ptr;
3487 struct drm_property_blob *prop_blob;
3488 uint32_t __user *blob_id_ptr;
3489 uint64_t __user *values_ptr;
3490 uint32_t __user *blob_length_ptr;
3492 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3495 drm_modeset_lock_all(dev);
3496 property = drm_property_find(dev, out_resp->prop_id);
3502 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3503 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3504 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3506 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3507 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3511 value_count = property->num_values;
3513 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3514 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3515 out_resp->flags = property->flags;
3517 if ((out_resp->count_values >= value_count) && value_count) {
3518 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3519 for (i = 0; i < value_count; i++) {
3520 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3526 out_resp->count_values = value_count;
3528 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3529 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3530 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3532 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3533 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3535 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3540 if (copy_to_user(&enum_ptr[copied].name,
3541 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3548 out_resp->count_enum_blobs = enum_count;
3551 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3552 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3554 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3555 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3557 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3558 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3563 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3571 out_resp->count_enum_blobs = blob_count;
3574 drm_modeset_unlock_all(dev);
3578 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3581 struct drm_property_blob *blob;
3584 if (!length || !data)
3587 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3591 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3597 blob->length = length;
3599 memcpy(blob->data, data, length);
3601 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3605 static void drm_property_destroy_blob(struct drm_device *dev,
3606 struct drm_property_blob *blob)
3608 drm_mode_object_put(dev, &blob->base);
3609 list_del(&blob->head);
3614 * drm_mode_getblob_ioctl - get the contents of a blob property value
3617 * @file_priv: DRM file info
3619 * This function retrieves the contents of a blob property. The value stored in
3620 * an object's blob property is just a normal modeset object id.
3622 * Called by the user via ioctl.
3625 * Zero on success, errno on failure.
3627 int drm_mode_getblob_ioctl(struct drm_device *dev,
3628 void *data, struct drm_file *file_priv)
3630 struct drm_mode_get_blob *out_resp = data;
3631 struct drm_property_blob *blob;
3633 void __user *blob_ptr;
3635 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3638 drm_modeset_lock_all(dev);
3639 blob = drm_property_blob_find(dev, out_resp->blob_id);
3645 if (out_resp->length == blob->length) {
3646 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3647 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3652 out_resp->length = blob->length;
3655 drm_modeset_unlock_all(dev);
3660 * drm_mode_connector_update_edid_property - update the edid property of a connector
3661 * @connector: drm connector
3662 * @edid: new value of the edid property
3664 * This function creates a new blob modeset object and assigns its id to the
3665 * connector's edid property.
3668 * Zero on success, errno on failure.
3670 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3673 struct drm_device *dev = connector->dev;
3676 if (connector->edid_blob_ptr)
3677 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3679 /* Delete edid, when there is none. */
3681 connector->edid_blob_ptr = NULL;
3682 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3686 size = EDID_LENGTH * (1 + edid->extensions);
3687 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3689 if (!connector->edid_blob_ptr)
3692 ret = drm_object_property_set_value(&connector->base,
3693 dev->mode_config.edid_property,
3694 connector->edid_blob_ptr->base.id);
3698 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3700 static bool drm_property_change_is_valid(struct drm_property *property,
3703 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3706 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3707 if (value < property->values[0] || value > property->values[1])
3710 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3711 int64_t svalue = U642I64(value);
3712 if (svalue < U642I64(property->values[0]) ||
3713 svalue > U642I64(property->values[1]))
3716 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3718 uint64_t valid_mask = 0;
3719 for (i = 0; i < property->num_values; i++)
3720 valid_mask |= (1ULL << property->values[i]);
3721 return !(value & ~valid_mask);
3722 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3723 /* Only the driver knows */
3725 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3726 struct drm_mode_object *obj;
3727 /* a zero value for an object property translates to null: */
3731 * NOTE: use _object_find() directly to bypass restriction on
3732 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3733 * object this could race against object finalization, so it
3734 * simply tells us that the object *was* valid. Which is good
3737 obj = _object_find(property->dev, value, property->values[0]);
3741 for (i = 0; i < property->num_values; i++)
3742 if (property->values[i] == value)
3749 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3752 * @file_priv: DRM file info
3754 * This function sets the current value for a connectors's property. It also
3755 * calls into a driver's ->set_property callback to update the hardware state
3757 * Called by the user via ioctl.
3760 * Zero on success, errno on failure.
3762 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3763 void *data, struct drm_file *file_priv)
3765 struct drm_mode_connector_set_property *conn_set_prop = data;
3766 struct drm_mode_obj_set_property obj_set_prop = {
3767 .value = conn_set_prop->value,
3768 .prop_id = conn_set_prop->prop_id,
3769 .obj_id = conn_set_prop->connector_id,
3770 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3773 /* It does all the locking and checking we need */
3774 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3777 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3778 struct drm_property *property,
3782 struct drm_connector *connector = obj_to_connector(obj);
3784 /* Do DPMS ourselves */
3785 if (property == connector->dev->mode_config.dpms_property) {
3786 if (connector->funcs->dpms)
3787 (*connector->funcs->dpms)(connector, (int)value);
3789 } else if (connector->funcs->set_property)
3790 ret = connector->funcs->set_property(connector, property, value);
3792 /* store the property value if successful */
3794 drm_object_property_set_value(&connector->base, property, value);
3798 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3799 struct drm_property *property,
3803 struct drm_crtc *crtc = obj_to_crtc(obj);
3805 if (crtc->funcs->set_property)
3806 ret = crtc->funcs->set_property(crtc, property, value);
3808 drm_object_property_set_value(obj, property, value);
3813 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3814 struct drm_property *property,
3818 struct drm_plane *plane = obj_to_plane(obj);
3820 if (plane->funcs->set_property)
3821 ret = plane->funcs->set_property(plane, property, value);
3823 drm_object_property_set_value(obj, property, value);
3829 * drm_mode_getproperty_ioctl - get the current value of a object's property
3832 * @file_priv: DRM file info
3834 * This function retrieves the current value for an object's property. Compared
3835 * to the connector specific ioctl this one is extended to also work on crtc and
3838 * Called by the user via ioctl.
3841 * Zero on success, errno on failure.
3843 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3844 struct drm_file *file_priv)
3846 struct drm_mode_obj_get_properties *arg = data;
3847 struct drm_mode_object *obj;
3851 int props_count = 0;
3852 uint32_t __user *props_ptr;
3853 uint64_t __user *prop_values_ptr;
3855 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3858 drm_modeset_lock_all(dev);
3860 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3865 if (!obj->properties) {
3870 props_count = obj->properties->count;
3872 /* This ioctl is called twice, once to determine how much space is
3873 * needed, and the 2nd time to fill it. */
3874 if ((arg->count_props >= props_count) && props_count) {
3876 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3877 prop_values_ptr = (uint64_t __user *)(unsigned long)
3878 (arg->prop_values_ptr);
3879 for (i = 0; i < props_count; i++) {
3880 if (put_user(obj->properties->ids[i],
3881 props_ptr + copied)) {
3885 if (put_user(obj->properties->values[i],
3886 prop_values_ptr + copied)) {
3893 arg->count_props = props_count;
3895 drm_modeset_unlock_all(dev);
3900 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3903 * @file_priv: DRM file info
3905 * This function sets the current value for an object's property. It also calls
3906 * into a driver's ->set_property callback to update the hardware state.
3907 * Compared to the connector specific ioctl this one is extended to also work on
3908 * crtc and plane objects.
3910 * Called by the user via ioctl.
3913 * Zero on success, errno on failure.
3915 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3916 struct drm_file *file_priv)
3918 struct drm_mode_obj_set_property *arg = data;
3919 struct drm_mode_object *arg_obj;
3920 struct drm_mode_object *prop_obj;
3921 struct drm_property *property;
3925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3928 drm_modeset_lock_all(dev);
3930 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3935 if (!arg_obj->properties)
3938 for (i = 0; i < arg_obj->properties->count; i++)
3939 if (arg_obj->properties->ids[i] == arg->prop_id)
3942 if (i == arg_obj->properties->count)
3945 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3946 DRM_MODE_OBJECT_PROPERTY);
3951 property = obj_to_property(prop_obj);
3953 if (!drm_property_change_is_valid(property, arg->value))
3956 switch (arg_obj->type) {
3957 case DRM_MODE_OBJECT_CONNECTOR:
3958 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3961 case DRM_MODE_OBJECT_CRTC:
3962 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3964 case DRM_MODE_OBJECT_PLANE:
3965 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3970 drm_modeset_unlock_all(dev);
3975 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3976 * @connector: connector to attach
3977 * @encoder: encoder to attach @connector to
3979 * This function links up a connector to an encoder. Note that the routing
3980 * restrictions between encoders and crtcs are exposed to userspace through the
3981 * possible_clones and possible_crtcs bitmasks.
3984 * Zero on success, errno on failure.
3986 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3987 struct drm_encoder *encoder)
3991 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3992 if (connector->encoder_ids[i] == 0) {
3993 connector->encoder_ids[i] = encoder->base.id;
3999 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4002 * drm_mode_crtc_set_gamma_size - set the gamma table size
4003 * @crtc: CRTC to set the gamma table size for
4004 * @gamma_size: size of the gamma table
4006 * Drivers which support gamma tables should set this to the supported gamma
4007 * table size when initializing the CRTC. Currently the drm core only supports a
4008 * fixed gamma table size.
4011 * Zero on success, errno on failure.
4013 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4016 crtc->gamma_size = gamma_size;
4018 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4019 if (!crtc->gamma_store) {
4020 crtc->gamma_size = 0;
4026 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4029 * drm_mode_gamma_set_ioctl - set the gamma table
4032 * @file_priv: DRM file info
4034 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4035 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4037 * Called by the user via ioctl.
4040 * Zero on success, errno on failure.
4042 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4043 void *data, struct drm_file *file_priv)
4045 struct drm_mode_crtc_lut *crtc_lut = data;
4046 struct drm_crtc *crtc;
4047 void *r_base, *g_base, *b_base;
4051 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4054 drm_modeset_lock_all(dev);
4055 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4061 if (crtc->funcs->gamma_set == NULL) {
4066 /* memcpy into gamma store */
4067 if (crtc_lut->gamma_size != crtc->gamma_size) {
4072 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4073 r_base = crtc->gamma_store;
4074 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4079 g_base = r_base + size;
4080 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4085 b_base = g_base + size;
4086 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4091 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4094 drm_modeset_unlock_all(dev);
4100 * drm_mode_gamma_get_ioctl - get the gamma table
4103 * @file_priv: DRM file info
4105 * Copy the current gamma table into the storage provided. This also provides
4106 * the gamma table size the driver expects, which can be used to size the
4107 * allocated storage.
4109 * Called by the user via ioctl.
4112 * Zero on success, errno on failure.
4114 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4115 void *data, struct drm_file *file_priv)
4117 struct drm_mode_crtc_lut *crtc_lut = data;
4118 struct drm_crtc *crtc;
4119 void *r_base, *g_base, *b_base;
4123 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4126 drm_modeset_lock_all(dev);
4127 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4133 /* memcpy into gamma store */
4134 if (crtc_lut->gamma_size != crtc->gamma_size) {
4139 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4140 r_base = crtc->gamma_store;
4141 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4146 g_base = r_base + size;
4147 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4152 b_base = g_base + size;
4153 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4158 drm_modeset_unlock_all(dev);
4163 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4166 * @file_priv: DRM file info
4168 * This schedules an asynchronous update on a given CRTC, called page flip.
4169 * Optionally a drm event is generated to signal the completion of the event.
4170 * Generic drivers cannot assume that a pageflip with changed framebuffer
4171 * properties (including driver specific metadata like tiling layout) will work,
4172 * but some drivers support e.g. pixel format changes through the pageflip
4175 * Called by the user via ioctl.
4178 * Zero on success, errno on failure.
4180 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4181 void *data, struct drm_file *file_priv)
4183 struct drm_mode_crtc_page_flip *page_flip = data;
4184 struct drm_crtc *crtc;
4185 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4186 struct drm_pending_vblank_event *e = NULL;
4187 unsigned long flags;
4190 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4191 page_flip->reserved != 0)
4194 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4197 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4201 mutex_lock(&crtc->mutex);
4202 if (crtc->primary->fb == NULL) {
4203 /* The framebuffer is currently unbound, presumably
4204 * due to a hotplug event, that userspace has not
4211 if (crtc->funcs->page_flip == NULL)
4214 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4220 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4224 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4225 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4230 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4232 spin_lock_irqsave(&dev->event_lock, flags);
4233 if (file_priv->event_space < sizeof e->event) {
4234 spin_unlock_irqrestore(&dev->event_lock, flags);
4237 file_priv->event_space -= sizeof e->event;
4238 spin_unlock_irqrestore(&dev->event_lock, flags);
4240 e = kzalloc(sizeof *e, GFP_KERNEL);
4242 spin_lock_irqsave(&dev->event_lock, flags);
4243 file_priv->event_space += sizeof e->event;
4244 spin_unlock_irqrestore(&dev->event_lock, flags);
4248 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4249 e->event.base.length = sizeof e->event;
4250 e->event.user_data = page_flip->user_data;
4251 e->base.event = &e->event.base;
4252 e->base.file_priv = file_priv;
4254 (void (*) (struct drm_pending_event *)) kfree;
4257 old_fb = crtc->primary->fb;
4258 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4260 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4261 spin_lock_irqsave(&dev->event_lock, flags);
4262 file_priv->event_space += sizeof e->event;
4263 spin_unlock_irqrestore(&dev->event_lock, flags);
4266 /* Keep the old fb, don't unref it. */
4270 * Warn if the driver hasn't properly updated the crtc->fb
4271 * field to reflect that the new framebuffer is now used.
4272 * Failing to do so will screw with the reference counting
4275 WARN_ON(crtc->primary->fb != fb);
4276 /* Unref only the old framebuffer. */
4282 drm_framebuffer_unreference(fb);
4284 drm_framebuffer_unreference(old_fb);
4285 mutex_unlock(&crtc->mutex);
4291 * drm_mode_config_reset - call ->reset callbacks
4294 * This functions calls all the crtc's, encoder's and connector's ->reset
4295 * callback. Drivers can use this in e.g. their driver load or resume code to
4296 * reset hardware and software state.
4298 void drm_mode_config_reset(struct drm_device *dev)
4300 struct drm_crtc *crtc;
4301 struct drm_encoder *encoder;
4302 struct drm_connector *connector;
4304 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4305 if (crtc->funcs->reset)
4306 crtc->funcs->reset(crtc);
4308 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4309 if (encoder->funcs->reset)
4310 encoder->funcs->reset(encoder);
4312 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4313 connector->status = connector_status_unknown;
4315 if (connector->funcs->reset)
4316 connector->funcs->reset(connector);
4319 EXPORT_SYMBOL(drm_mode_config_reset);
4322 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4325 * @file_priv: DRM file info
4327 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4328 * TTM or something else entirely) and returns the resulting buffer handle. This
4329 * handle can then be wrapped up into a framebuffer modeset object.
4331 * Note that userspace is not allowed to use such objects for render
4332 * acceleration - drivers must create their own private ioctls for such a use
4335 * Called by the user via ioctl.
4338 * Zero on success, errno on failure.
4340 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4341 void *data, struct drm_file *file_priv)
4343 struct drm_mode_create_dumb *args = data;
4344 u32 cpp, stride, size;
4346 if (!dev->driver->dumb_create)
4348 if (!args->width || !args->height || !args->bpp)
4351 /* overflow checks for 32bit size calculations */
4352 cpp = DIV_ROUND_UP(args->bpp, 8);
4353 if (cpp > 0xffffffffU / args->width)
4355 stride = cpp * args->width;
4356 if (args->height > 0xffffffffU / stride)
4359 /* test for wrap-around */
4360 size = args->height * stride;
4361 if (PAGE_ALIGN(size) == 0)
4364 return dev->driver->dumb_create(file_priv, dev, args);
4368 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4371 * @file_priv: DRM file info
4373 * Allocate an offset in the drm device node's address space to be able to
4374 * memory map a dumb buffer.
4376 * Called by the user via ioctl.
4379 * Zero on success, errno on failure.
4381 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4382 void *data, struct drm_file *file_priv)
4384 struct drm_mode_map_dumb *args = data;
4386 /* call driver ioctl to get mmap offset */
4387 if (!dev->driver->dumb_map_offset)
4390 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4394 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4397 * @file_priv: DRM file info
4399 * This destroys the userspace handle for the given dumb backing storage buffer.
4400 * Since buffer objects must be reference counted in the kernel a buffer object
4401 * won't be immediately freed if a framebuffer modeset object still uses it.
4403 * Called by the user via ioctl.
4406 * Zero on success, errno on failure.
4408 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4409 void *data, struct drm_file *file_priv)
4411 struct drm_mode_destroy_dumb *args = data;
4413 if (!dev->driver->dumb_destroy)
4416 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4420 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4421 * @format: pixel format (DRM_FORMAT_*)
4422 * @depth: storage for the depth value
4423 * @bpp: storage for the bpp value
4425 * This only supports RGB formats here for compat with code that doesn't use
4426 * pixel formats directly yet.
4428 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4433 case DRM_FORMAT_RGB332:
4434 case DRM_FORMAT_BGR233:
4438 case DRM_FORMAT_XRGB1555:
4439 case DRM_FORMAT_XBGR1555:
4440 case DRM_FORMAT_RGBX5551:
4441 case DRM_FORMAT_BGRX5551:
4442 case DRM_FORMAT_ARGB1555:
4443 case DRM_FORMAT_ABGR1555:
4444 case DRM_FORMAT_RGBA5551:
4445 case DRM_FORMAT_BGRA5551:
4449 case DRM_FORMAT_RGB565:
4450 case DRM_FORMAT_BGR565:
4454 case DRM_FORMAT_RGB888:
4455 case DRM_FORMAT_BGR888:
4459 case DRM_FORMAT_XRGB8888:
4460 case DRM_FORMAT_XBGR8888:
4461 case DRM_FORMAT_RGBX8888:
4462 case DRM_FORMAT_BGRX8888:
4466 case DRM_FORMAT_XRGB2101010:
4467 case DRM_FORMAT_XBGR2101010:
4468 case DRM_FORMAT_RGBX1010102:
4469 case DRM_FORMAT_BGRX1010102:
4470 case DRM_FORMAT_ARGB2101010:
4471 case DRM_FORMAT_ABGR2101010:
4472 case DRM_FORMAT_RGBA1010102:
4473 case DRM_FORMAT_BGRA1010102:
4477 case DRM_FORMAT_ARGB8888:
4478 case DRM_FORMAT_ABGR8888:
4479 case DRM_FORMAT_RGBA8888:
4480 case DRM_FORMAT_BGRA8888:
4485 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4486 drm_get_format_name(format));
4492 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4495 * drm_format_num_planes - get the number of planes for format
4496 * @format: pixel format (DRM_FORMAT_*)
4499 * The number of planes used by the specified pixel format.
4501 int drm_format_num_planes(uint32_t format)
4504 case DRM_FORMAT_YUV410:
4505 case DRM_FORMAT_YVU410:
4506 case DRM_FORMAT_YUV411:
4507 case DRM_FORMAT_YVU411:
4508 case DRM_FORMAT_YUV420:
4509 case DRM_FORMAT_YVU420:
4510 case DRM_FORMAT_YUV422:
4511 case DRM_FORMAT_YVU422:
4512 case DRM_FORMAT_YUV444:
4513 case DRM_FORMAT_YVU444:
4515 case DRM_FORMAT_NV12:
4516 case DRM_FORMAT_NV21:
4517 case DRM_FORMAT_NV16:
4518 case DRM_FORMAT_NV61:
4519 case DRM_FORMAT_NV24:
4520 case DRM_FORMAT_NV42:
4526 EXPORT_SYMBOL(drm_format_num_planes);
4529 * drm_format_plane_cpp - determine the bytes per pixel value
4530 * @format: pixel format (DRM_FORMAT_*)
4531 * @plane: plane index
4534 * The bytes per pixel value for the specified plane.
4536 int drm_format_plane_cpp(uint32_t format, int plane)
4541 if (plane >= drm_format_num_planes(format))
4545 case DRM_FORMAT_YUYV:
4546 case DRM_FORMAT_YVYU:
4547 case DRM_FORMAT_UYVY:
4548 case DRM_FORMAT_VYUY:
4550 case DRM_FORMAT_NV12:
4551 case DRM_FORMAT_NV21:
4552 case DRM_FORMAT_NV16:
4553 case DRM_FORMAT_NV61:
4554 case DRM_FORMAT_NV24:
4555 case DRM_FORMAT_NV42:
4556 return plane ? 2 : 1;
4557 case DRM_FORMAT_YUV410:
4558 case DRM_FORMAT_YVU410:
4559 case DRM_FORMAT_YUV411:
4560 case DRM_FORMAT_YVU411:
4561 case DRM_FORMAT_YUV420:
4562 case DRM_FORMAT_YVU420:
4563 case DRM_FORMAT_YUV422:
4564 case DRM_FORMAT_YVU422:
4565 case DRM_FORMAT_YUV444:
4566 case DRM_FORMAT_YVU444:
4569 drm_fb_get_bpp_depth(format, &depth, &bpp);
4573 EXPORT_SYMBOL(drm_format_plane_cpp);
4576 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4577 * @format: pixel format (DRM_FORMAT_*)
4580 * The horizontal chroma subsampling factor for the
4581 * specified pixel format.
4583 int drm_format_horz_chroma_subsampling(uint32_t format)
4586 case DRM_FORMAT_YUV411:
4587 case DRM_FORMAT_YVU411:
4588 case DRM_FORMAT_YUV410:
4589 case DRM_FORMAT_YVU410:
4591 case DRM_FORMAT_YUYV:
4592 case DRM_FORMAT_YVYU:
4593 case DRM_FORMAT_UYVY:
4594 case DRM_FORMAT_VYUY:
4595 case DRM_FORMAT_NV12:
4596 case DRM_FORMAT_NV21:
4597 case DRM_FORMAT_NV16:
4598 case DRM_FORMAT_NV61:
4599 case DRM_FORMAT_YUV422:
4600 case DRM_FORMAT_YVU422:
4601 case DRM_FORMAT_YUV420:
4602 case DRM_FORMAT_YVU420:
4608 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4611 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4612 * @format: pixel format (DRM_FORMAT_*)
4615 * The vertical chroma subsampling factor for the
4616 * specified pixel format.
4618 int drm_format_vert_chroma_subsampling(uint32_t format)
4621 case DRM_FORMAT_YUV410:
4622 case DRM_FORMAT_YVU410:
4624 case DRM_FORMAT_YUV420:
4625 case DRM_FORMAT_YVU420:
4626 case DRM_FORMAT_NV12:
4627 case DRM_FORMAT_NV21:
4633 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4636 * drm_mode_config_init - initialize DRM mode_configuration structure
4639 * Initialize @dev's mode_config structure, used for tracking the graphics
4640 * configuration of @dev.
4642 * Since this initializes the modeset locks, no locking is possible. Which is no
4643 * problem, since this should happen single threaded at init time. It is the
4644 * driver's problem to ensure this guarantee.
4647 void drm_mode_config_init(struct drm_device *dev)
4649 mutex_init(&dev->mode_config.mutex);
4650 mutex_init(&dev->mode_config.connection_mutex);
4651 mutex_init(&dev->mode_config.idr_mutex);
4652 mutex_init(&dev->mode_config.fb_lock);
4653 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4654 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4655 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4656 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4657 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4658 INIT_LIST_HEAD(&dev->mode_config.property_list);
4659 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4660 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4661 idr_init(&dev->mode_config.crtc_idr);
4663 drm_modeset_lock_all(dev);
4664 drm_mode_create_standard_connector_properties(dev);
4665 drm_mode_create_standard_plane_properties(dev);
4666 drm_modeset_unlock_all(dev);
4668 /* Just to be sure */
4669 dev->mode_config.num_fb = 0;
4670 dev->mode_config.num_connector = 0;
4671 dev->mode_config.num_crtc = 0;
4672 dev->mode_config.num_encoder = 0;
4673 dev->mode_config.num_overlay_plane = 0;
4674 dev->mode_config.num_total_plane = 0;
4676 EXPORT_SYMBOL(drm_mode_config_init);
4679 * drm_mode_config_cleanup - free up DRM mode_config info
4682 * Free up all the connectors and CRTCs associated with this DRM device, then
4683 * free up the framebuffers and associated buffer objects.
4685 * Note that since this /should/ happen single-threaded at driver/device
4686 * teardown time, no locking is required. It's the driver's job to ensure that
4687 * this guarantee actually holds true.
4689 * FIXME: cleanup any dangling user buffer objects too
4691 void drm_mode_config_cleanup(struct drm_device *dev)
4693 struct drm_connector *connector, *ot;
4694 struct drm_crtc *crtc, *ct;
4695 struct drm_encoder *encoder, *enct;
4696 struct drm_bridge *bridge, *brt;
4697 struct drm_framebuffer *fb, *fbt;
4698 struct drm_property *property, *pt;
4699 struct drm_property_blob *blob, *bt;
4700 struct drm_plane *plane, *plt;
4702 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4704 encoder->funcs->destroy(encoder);
4707 list_for_each_entry_safe(bridge, brt,
4708 &dev->mode_config.bridge_list, head) {
4709 bridge->funcs->destroy(bridge);
4712 list_for_each_entry_safe(connector, ot,
4713 &dev->mode_config.connector_list, head) {
4714 connector->funcs->destroy(connector);
4717 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4719 drm_property_destroy(dev, property);
4722 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4724 drm_property_destroy_blob(dev, blob);
4728 * Single-threaded teardown context, so it's not required to grab the
4729 * fb_lock to protect against concurrent fb_list access. Contrary, it
4730 * would actually deadlock with the drm_framebuffer_cleanup function.
4732 * Also, if there are any framebuffers left, that's a driver leak now,
4733 * so politely WARN about this.
4735 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4736 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4737 drm_framebuffer_remove(fb);
4740 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4742 plane->funcs->destroy(plane);
4745 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4746 crtc->funcs->destroy(crtc);
4749 idr_destroy(&dev->mode_config.crtc_idr);
4751 EXPORT_SYMBOL(drm_mode_config_cleanup);