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/export.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <uapi_drm/drm_fourcc.h>
39 #include <linux/slab.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
45 * drm_modeset_lock_all - take all modeset locks
48 * This function takes all modeset locks, suitable where a more fine-grained
49 * scheme isn't (yet) implemented. Locks must be dropped with
50 * drm_modeset_unlock_all.
52 void drm_modeset_lock_all(struct drm_device *dev)
54 struct drm_mode_config *config = &dev->mode_config;
55 struct drm_modeset_acquire_ctx *ctx;
58 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
62 mutex_lock(&config->mutex);
64 drm_modeset_acquire_init(ctx, 0);
67 ret = drm_modeset_lock(&config->connection_mutex, ctx);
70 ret = drm_modeset_lock_all_crtcs(dev, ctx);
74 WARN_ON(config->acquire_ctx);
76 /* now we hold the locks, so now that it is safe, stash the
77 * ctx for drm_modeset_unlock_all():
79 config->acquire_ctx = ctx;
81 drm_warn_on_modeset_not_all_locked(dev);
86 if (ret == -EDEADLK) {
87 drm_modeset_backoff(ctx);
91 EXPORT_SYMBOL(drm_modeset_lock_all);
94 * drm_modeset_unlock_all - drop all modeset locks
97 * This function drop all modeset locks taken by drm_modeset_lock_all.
99 void drm_modeset_unlock_all(struct drm_device *dev)
101 struct drm_mode_config *config = &dev->mode_config;
102 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
107 config->acquire_ctx = NULL;
108 drm_modeset_drop_locks(ctx);
109 drm_modeset_acquire_fini(ctx);
113 mutex_unlock(&dev->mode_config.mutex);
115 EXPORT_SYMBOL(drm_modeset_unlock_all);
118 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
121 * Useful as a debug assert.
123 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
125 struct drm_crtc *crtc;
127 /* Locking is currently fubar in the panic handler. */
129 if (oops_in_progress)
133 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
134 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
136 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
137 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
139 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
141 /* Avoid boilerplate. I'm tired of typing. */
142 #define DRM_ENUM_NAME_FN(fnname, list) \
143 const char *fnname(int val) \
146 for (i = 0; i < ARRAY_SIZE(list); i++) { \
147 if (list[i].type == val) \
148 return list[i].name; \
150 return "(unknown)"; \
156 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
157 { { DRM_MODE_DPMS_ON, "On" },
158 { DRM_MODE_DPMS_STANDBY, "Standby" },
159 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
160 { DRM_MODE_DPMS_OFF, "Off" }
163 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
165 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
167 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
168 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
169 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
173 * Optional properties
175 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
177 { DRM_MODE_SCALE_NONE, "None" },
178 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
179 { DRM_MODE_SCALE_CENTER, "Center" },
180 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
184 * Non-global properties, but "required" for certain connectors.
186 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
188 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
189 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
190 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
193 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
195 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
197 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
198 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
199 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
202 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
203 drm_dvi_i_subconnector_enum_list)
205 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
207 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
208 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
209 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
210 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
211 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
214 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
216 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
218 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
219 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
220 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
221 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
222 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
225 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
226 drm_tv_subconnector_enum_list)
228 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
229 { DRM_MODE_DIRTY_OFF, "Off" },
230 { DRM_MODE_DIRTY_ON, "On" },
231 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
234 struct drm_conn_prop_enum_list {
241 * Connector and encoder types.
243 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
244 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
245 { DRM_MODE_CONNECTOR_VGA, "VGA" },
246 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
247 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
248 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
249 { DRM_MODE_CONNECTOR_Composite, "Composite" },
250 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
251 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
252 { DRM_MODE_CONNECTOR_Component, "Component" },
253 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
254 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
255 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
256 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
257 { DRM_MODE_CONNECTOR_TV, "TV" },
258 { DRM_MODE_CONNECTOR_eDP, "eDP" },
259 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
260 { DRM_MODE_CONNECTOR_DSI, "DSI" },
263 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
264 { { DRM_MODE_ENCODER_NONE, "None" },
265 { DRM_MODE_ENCODER_DAC, "DAC" },
266 { DRM_MODE_ENCODER_TMDS, "TMDS" },
267 { DRM_MODE_ENCODER_LVDS, "LVDS" },
268 { DRM_MODE_ENCODER_TVDAC, "TV" },
269 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
270 { DRM_MODE_ENCODER_DSI, "DSI" },
271 { DRM_MODE_ENCODER_DPMST, "DP MST" },
274 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
276 { SubPixelUnknown, "Unknown" },
277 { SubPixelHorizontalRGB, "Horizontal RGB" },
278 { SubPixelHorizontalBGR, "Horizontal BGR" },
279 { SubPixelVerticalRGB, "Vertical RGB" },
280 { SubPixelVerticalBGR, "Vertical BGR" },
281 { SubPixelNone, "None" },
284 void drm_connector_ida_init(void)
289 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
290 ida_init(&drm_connector_enum_list[i].ida);
294 void drm_connector_ida_destroy(void)
299 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
300 ida_destroy(&drm_connector_enum_list[i].ida);
305 * drm_get_connector_status_name - return a string for connector status
306 * @status: connector status to compute name of
308 * In contrast to the other drm_get_*_name functions this one here returns a
309 * const pointer and hence is threadsafe.
311 const char *drm_get_connector_status_name(enum drm_connector_status status)
313 if (status == connector_status_connected)
315 else if (status == connector_status_disconnected)
316 return "disconnected";
320 EXPORT_SYMBOL(drm_get_connector_status_name);
323 * drm_get_subpixel_order_name - return a string for a given subpixel enum
324 * @order: enum of subpixel_order
326 * Note you could abuse this and return something out of bounds, but that
327 * would be a caller error. No unscrubbed user data should make it here.
329 const char *drm_get_subpixel_order_name(enum subpixel_order order)
331 return drm_subpixel_enum_list[order].name;
333 EXPORT_SYMBOL(drm_get_subpixel_order_name);
335 static char printable_char(int c)
337 return isascii(c) && isprint(c) ? c : '?';
341 * drm_get_format_name - return a string for drm fourcc format
342 * @format: format to compute name of
344 * Note that the buffer used by this function is globally shared and owned by
345 * the function itself.
347 * FIXME: This isn't really multithreading safe.
349 const char *drm_get_format_name(uint32_t format)
353 ksnprintf(buf, sizeof(buf),
354 "%c%c%c%c %s-endian (0x%08x)",
355 printable_char(format & 0xff),
356 printable_char((format >> 8) & 0xff),
357 printable_char((format >> 16) & 0xff),
358 printable_char((format >> 24) & 0x7f),
359 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
364 EXPORT_SYMBOL(drm_get_format_name);
367 * drm_mode_object_get - allocate a new modeset identifier
369 * @obj: object pointer, used to generate unique ID
370 * @obj_type: object type
372 * Create a unique identifier based on @ptr in @dev's identifier space. Used
373 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
374 * modeset identifiers are _not_ reference counted. Hence don't use this for
375 * reference counted modeset objects like framebuffers.
378 * New unique (relative to other objects in @dev) integer identifier for the
381 int drm_mode_object_get(struct drm_device *dev,
382 struct drm_mode_object *obj, uint32_t obj_type)
386 mutex_lock(&dev->mode_config.idr_mutex);
387 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
390 * Set up the object linking under the protection of the idr
391 * lock so that other users can't see inconsistent state.
394 obj->type = obj_type;
396 mutex_unlock(&dev->mode_config.idr_mutex);
398 return ret < 0 ? ret : 0;
402 * drm_mode_object_put - free a modeset identifer
404 * @object: object to free
406 * Free @id from @dev's unique identifier pool. Note that despite the _get
407 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
408 * for reference counted modeset objects like framebuffers.
410 void drm_mode_object_put(struct drm_device *dev,
411 struct drm_mode_object *object)
413 mutex_lock(&dev->mode_config.idr_mutex);
414 idr_remove(&dev->mode_config.crtc_idr, object->id);
415 mutex_unlock(&dev->mode_config.idr_mutex);
418 static struct drm_mode_object *_object_find(struct drm_device *dev,
419 uint32_t id, uint32_t type)
421 struct drm_mode_object *obj = NULL;
423 mutex_lock(&dev->mode_config.idr_mutex);
424 obj = idr_find(&dev->mode_config.crtc_idr, id);
425 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
428 mutex_unlock(&dev->mode_config.idr_mutex);
434 * drm_mode_object_find - look up a drm object with static lifetime
436 * @id: id of the mode object
437 * @type: type of the mode object
439 * Note that framebuffers cannot be looked up with this functions - since those
440 * are reference counted, they need special treatment. Even with
441 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
442 * rather than WARN_ON()).
444 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
445 uint32_t id, uint32_t type)
447 struct drm_mode_object *obj = NULL;
449 /* Framebuffers are reference counted and need their own lookup
451 WARN_ON(type == DRM_MODE_OBJECT_FB);
452 obj = _object_find(dev, id, type);
453 /* don't leak out unref'd fb's */
454 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
458 EXPORT_SYMBOL(drm_mode_object_find);
461 * drm_framebuffer_init - initialize a framebuffer
463 * @fb: framebuffer to be initialized
464 * @funcs: ... with these functions
466 * Allocates an ID for the framebuffer's parent mode object, sets its mode
467 * functions & device file and adds it to the master fd list.
470 * This functions publishes the fb and makes it available for concurrent access
471 * by other users. Which means by this point the fb _must_ be fully set up -
472 * since all the fb attributes are invariant over its lifetime, no further
473 * locking but only correct reference counting is required.
476 * Zero on success, error code on failure.
478 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
479 const struct drm_framebuffer_funcs *funcs)
483 mutex_lock(&dev->mode_config.fb_lock);
484 kref_init(&fb->refcount);
485 INIT_LIST_HEAD(&fb->filp_head);
489 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
493 /* Grab the idr reference. */
494 drm_framebuffer_reference(fb);
496 dev->mode_config.num_fb++;
497 list_add(&fb->head, &dev->mode_config.fb_list);
499 mutex_unlock(&dev->mode_config.fb_lock);
503 EXPORT_SYMBOL(drm_framebuffer_init);
505 static void drm_framebuffer_free(struct kref *kref)
507 struct drm_framebuffer *fb =
508 container_of(kref, struct drm_framebuffer, refcount);
509 fb->funcs->destroy(fb);
512 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
515 struct drm_mode_object *obj = NULL;
516 struct drm_framebuffer *fb;
518 mutex_lock(&dev->mode_config.idr_mutex);
519 obj = idr_find(&dev->mode_config.crtc_idr, id);
520 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
524 mutex_unlock(&dev->mode_config.idr_mutex);
530 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
532 * @id: id of the fb object
534 * If successful, this grabs an additional reference to the framebuffer -
535 * callers need to make sure to eventually unreference the returned framebuffer
536 * again, using @drm_framebuffer_unreference.
538 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
541 struct drm_framebuffer *fb;
543 mutex_lock(&dev->mode_config.fb_lock);
544 fb = __drm_framebuffer_lookup(dev, id);
546 drm_framebuffer_reference(fb);
547 mutex_unlock(&dev->mode_config.fb_lock);
551 EXPORT_SYMBOL(drm_framebuffer_lookup);
554 * drm_framebuffer_unreference - unref a framebuffer
555 * @fb: framebuffer to unref
557 * This functions decrements the fb's refcount and frees it if it drops to zero.
559 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
561 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
562 kref_put(&fb->refcount, drm_framebuffer_free);
564 EXPORT_SYMBOL(drm_framebuffer_unreference);
567 * drm_framebuffer_reference - incr the fb refcnt
570 * This functions increments the fb's refcount.
572 void drm_framebuffer_reference(struct drm_framebuffer *fb)
574 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
575 kref_get(&fb->refcount);
577 EXPORT_SYMBOL(drm_framebuffer_reference);
579 static void drm_framebuffer_free_bug(struct kref *kref)
584 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
586 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
587 kref_put(&fb->refcount, drm_framebuffer_free_bug);
590 /* dev->mode_config.fb_lock must be held! */
591 static void __drm_framebuffer_unregister(struct drm_device *dev,
592 struct drm_framebuffer *fb)
594 mutex_lock(&dev->mode_config.idr_mutex);
595 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
596 mutex_unlock(&dev->mode_config.idr_mutex);
600 __drm_framebuffer_unreference(fb);
604 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
605 * @fb: fb to unregister
607 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
608 * those used for fbdev. Note that the caller must hold a reference of it's own,
609 * i.e. the object may not be destroyed through this call (since it'll lead to a
610 * locking inversion).
612 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
614 struct drm_device *dev = fb->dev;
616 mutex_lock(&dev->mode_config.fb_lock);
617 /* Mark fb as reaped and drop idr ref. */
618 __drm_framebuffer_unregister(dev, fb);
619 mutex_unlock(&dev->mode_config.fb_lock);
621 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
624 * drm_framebuffer_cleanup - remove a framebuffer object
625 * @fb: framebuffer to remove
627 * Cleanup framebuffer. This function is intended to be used from the drivers
628 * ->destroy callback. It can also be used to clean up driver private
629 * framebuffers embedded into a larger structure.
631 * Note that this function does not remove the fb from active usuage - if it is
632 * still used anywhere, hilarity can ensue since userspace could call getfb on
633 * the id and get back -EINVAL. Obviously no concern at driver unload time.
635 * Also, the framebuffer will not be removed from the lookup idr - for
636 * user-created framebuffers this will happen in in the rmfb ioctl. For
637 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
638 * drm_framebuffer_unregister_private.
640 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
642 struct drm_device *dev = fb->dev;
644 mutex_lock(&dev->mode_config.fb_lock);
646 dev->mode_config.num_fb--;
647 mutex_unlock(&dev->mode_config.fb_lock);
649 EXPORT_SYMBOL(drm_framebuffer_cleanup);
652 * drm_framebuffer_remove - remove and unreference a framebuffer object
653 * @fb: framebuffer to remove
655 * Scans all the CRTCs and planes in @dev's mode_config. If they're
656 * using @fb, removes it, setting it to NULL. Then drops the reference to the
657 * passed-in framebuffer. Might take the modeset locks.
659 * Note that this function optimizes the cleanup away if the caller holds the
660 * last reference to the framebuffer. It is also guaranteed to not take the
661 * modeset locks in this case.
663 void drm_framebuffer_remove(struct drm_framebuffer *fb)
665 struct drm_device *dev = fb->dev;
666 struct drm_crtc *crtc;
667 struct drm_plane *plane;
668 struct drm_mode_set set;
671 WARN_ON(!list_empty(&fb->filp_head));
674 * drm ABI mandates that we remove any deleted framebuffers from active
675 * useage. But since most sane clients only remove framebuffers they no
676 * longer need, try to optimize this away.
678 * Since we're holding a reference ourselves, observing a refcount of 1
679 * means that we're the last holder and can skip it. Also, the refcount
680 * can never increase from 1 again, so we don't need any barriers or
683 * Note that userspace could try to race with use and instate a new
684 * usage _after_ we've cleared all current ones. End result will be an
685 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
688 if (atomic_read(&fb->refcount.refcount) > 1) {
689 drm_modeset_lock_all(dev);
690 /* remove from any CRTC */
691 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
692 if (crtc->primary->fb == fb) {
693 /* should turn off the crtc */
694 memset(&set, 0, sizeof(struct drm_mode_set));
697 ret = drm_mode_set_config_internal(&set);
699 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
703 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
705 drm_plane_force_disable(plane);
707 drm_modeset_unlock_all(dev);
710 drm_framebuffer_unreference(fb);
712 EXPORT_SYMBOL(drm_framebuffer_remove);
714 DEFINE_WW_CLASS(crtc_ww_class);
717 * drm_crtc_init_with_planes - Initialise a new CRTC object with
718 * specified primary and cursor planes.
720 * @crtc: CRTC object to init
721 * @primary: Primary plane for CRTC
722 * @cursor: Cursor plane for CRTC
723 * @funcs: callbacks for the new CRTC
725 * Inits a new object created as base part of a driver crtc object.
728 * Zero on success, error code on failure.
730 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
731 struct drm_plane *primary,
733 const struct drm_crtc_funcs *funcs)
735 struct drm_mode_config *config = &dev->mode_config;
740 crtc->invert_dimensions = false;
742 drm_modeset_lock_all(dev);
743 drm_modeset_lock_init(&crtc->mutex);
744 /* dropped by _unlock_all(): */
745 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
747 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
751 crtc->base.properties = &crtc->properties;
753 list_add_tail(&crtc->head, &config->crtc_list);
756 crtc->primary = primary;
758 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
761 drm_modeset_unlock_all(dev);
765 EXPORT_SYMBOL(drm_crtc_init_with_planes);
768 * drm_crtc_cleanup - Clean up the core crtc usage
769 * @crtc: CRTC to cleanup
771 * This function cleans up @crtc and removes it from the DRM mode setting
772 * core. Note that the function does *not* free the crtc structure itself,
773 * this is the responsibility of the caller.
775 void drm_crtc_cleanup(struct drm_crtc *crtc)
777 struct drm_device *dev = crtc->dev;
779 kfree(crtc->gamma_store);
780 crtc->gamma_store = NULL;
782 drm_modeset_lock_fini(&crtc->mutex);
784 drm_mode_object_put(dev, &crtc->base);
785 list_del(&crtc->head);
786 dev->mode_config.num_crtc--;
788 EXPORT_SYMBOL(drm_crtc_cleanup);
791 * drm_crtc_index - find the index of a registered CRTC
792 * @crtc: CRTC to find index for
794 * Given a registered CRTC, return the index of that CRTC within a DRM
795 * device's list of CRTCs.
797 unsigned int drm_crtc_index(struct drm_crtc *crtc)
799 unsigned int index = 0;
800 struct drm_crtc *tmp;
802 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
811 EXPORT_SYMBOL(drm_crtc_index);
814 * drm_mode_remove - remove and free a mode
815 * @connector: connector list to modify
816 * @mode: mode to remove
818 * Remove @mode from @connector's mode list, then free it.
820 static void drm_mode_remove(struct drm_connector *connector,
821 struct drm_display_mode *mode)
823 list_del(&mode->head);
824 drm_mode_destroy(connector->dev, mode);
828 * drm_connector_init - Init a preallocated connector
830 * @connector: the connector to init
831 * @funcs: callbacks for this connector
832 * @connector_type: user visible type of the connector
834 * Initialises a preallocated connector. Connectors should be
835 * subclassed as part of driver connector objects.
838 * Zero on success, error code on failure.
840 int drm_connector_init(struct drm_device *dev,
841 struct drm_connector *connector,
842 const struct drm_connector_funcs *funcs,
847 drm_modeset_lock_all(dev);
849 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
853 connector->base.properties = &connector->properties;
854 connector->dev = dev;
855 connector->funcs = funcs;
856 connector->connector_type = connector_type;
857 connector->connector_type_id =
858 ++drm_connector_enum_list[connector_type].count; /* TODO */
859 if (connector->connector_type_id < 0) {
860 ret = connector->connector_type_id;
864 drm_asprintf(GFP_KERNEL, "%s-%d",
865 drm_connector_enum_list[connector_type].name,
866 connector->connector_type_id);
867 if (!connector->name) {
872 INIT_LIST_HEAD(&connector->probed_modes);
873 INIT_LIST_HEAD(&connector->modes);
874 connector->edid_blob_ptr = NULL;
875 connector->status = connector_status_unknown;
877 list_add_tail(&connector->head, &dev->mode_config.connector_list);
878 dev->mode_config.num_connector++;
880 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
881 drm_object_attach_property(&connector->base,
882 dev->mode_config.edid_property,
885 drm_object_attach_property(&connector->base,
886 dev->mode_config.dpms_property, 0);
890 drm_mode_object_put(dev, &connector->base);
893 drm_modeset_unlock_all(dev);
897 EXPORT_SYMBOL(drm_connector_init);
900 * drm_connector_cleanup - cleans up an initialised connector
901 * @connector: connector to cleanup
903 * Cleans up the connector but doesn't free the object.
905 void drm_connector_cleanup(struct drm_connector *connector)
907 struct drm_device *dev = connector->dev;
908 struct drm_display_mode *mode, *t;
910 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
911 drm_mode_remove(connector, mode);
913 list_for_each_entry_safe(mode, t, &connector->modes, head)
914 drm_mode_remove(connector, mode);
916 drm_mode_object_put(dev, &connector->base);
917 kfree(connector->name);
918 connector->name = NULL;
919 list_del(&connector->head);
920 dev->mode_config.num_connector--;
922 EXPORT_SYMBOL(drm_connector_cleanup);
925 * drm_connector_register - register a connector
926 * @connector: the connector to register
928 * Register userspace interfaces for a connector
931 * Zero on success, error code on failure.
933 int drm_connector_register(struct drm_connector *connector)
935 return drm_sysfs_connector_add(connector);
937 EXPORT_SYMBOL(drm_connector_register);
940 * drm_connector_unregister - unregister a connector
941 * @connector: the connector to unregister
943 * Unregister userspace interfaces for a connector
945 void drm_connector_unregister(struct drm_connector *connector)
947 drm_sysfs_connector_remove(connector);
949 EXPORT_SYMBOL(drm_connector_unregister);
953 * drm_connector_unplug_all - unregister connector userspace interfaces
956 * This function unregisters all connector userspace interfaces in sysfs. Should
957 * be call when the device is disconnected, e.g. from an usb driver's
958 * ->disconnect callback.
960 void drm_connector_unplug_all(struct drm_device *dev)
962 struct drm_connector *connector;
964 /* taking the mode config mutex ends up in a clash with sysfs */
965 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
966 drm_connector_unregister(connector);
969 EXPORT_SYMBOL(drm_connector_unplug_all);
972 * drm_bridge_init - initialize a drm transcoder/bridge
974 * @bridge: transcoder/bridge to set up
975 * @funcs: bridge function table
977 * Initialises a preallocated bridge. Bridges should be
978 * subclassed as part of driver connector objects.
981 * Zero on success, error code on failure.
983 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
984 const struct drm_bridge_funcs *funcs)
988 drm_modeset_lock_all(dev);
990 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
995 bridge->funcs = funcs;
997 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
998 dev->mode_config.num_bridge++;
1001 drm_modeset_unlock_all(dev);
1004 EXPORT_SYMBOL(drm_bridge_init);
1007 * drm_bridge_cleanup - cleans up an initialised bridge
1008 * @bridge: bridge to cleanup
1010 * Cleans up the bridge but doesn't free the object.
1012 void drm_bridge_cleanup(struct drm_bridge *bridge)
1014 struct drm_device *dev = bridge->dev;
1016 drm_modeset_lock_all(dev);
1017 drm_mode_object_put(dev, &bridge->base);
1018 list_del(&bridge->head);
1019 dev->mode_config.num_bridge--;
1020 drm_modeset_unlock_all(dev);
1022 EXPORT_SYMBOL(drm_bridge_cleanup);
1025 * drm_encoder_init - Init a preallocated encoder
1027 * @encoder: the encoder to init
1028 * @funcs: callbacks for this encoder
1029 * @encoder_type: user visible type of the encoder
1031 * Initialises a preallocated encoder. Encoder should be
1032 * subclassed as part of driver encoder objects.
1035 * Zero on success, error code on failure.
1037 int drm_encoder_init(struct drm_device *dev,
1038 struct drm_encoder *encoder,
1039 const struct drm_encoder_funcs *funcs,
1044 drm_modeset_lock_all(dev);
1046 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1051 encoder->encoder_type = encoder_type;
1052 encoder->funcs = funcs;
1053 encoder->name = drm_asprintf(GFP_KERNEL, "%s-%d",
1054 drm_encoder_enum_list[encoder_type].name,
1056 if (!encoder->name) {
1061 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1062 dev->mode_config.num_encoder++;
1066 drm_mode_object_put(dev, &encoder->base);
1069 drm_modeset_unlock_all(dev);
1073 EXPORT_SYMBOL(drm_encoder_init);
1076 * drm_encoder_cleanup - cleans up an initialised encoder
1077 * @encoder: encoder to cleanup
1079 * Cleans up the encoder but doesn't free the object.
1081 void drm_encoder_cleanup(struct drm_encoder *encoder)
1083 struct drm_device *dev = encoder->dev;
1084 drm_modeset_lock_all(dev);
1085 drm_mode_object_put(dev, &encoder->base);
1086 kfree(encoder->name);
1087 encoder->name = NULL;
1088 list_del(&encoder->head);
1089 dev->mode_config.num_encoder--;
1090 drm_modeset_unlock_all(dev);
1092 EXPORT_SYMBOL(drm_encoder_cleanup);
1095 * drm_universal_plane_init - Initialize a new universal plane object
1097 * @plane: plane object to init
1098 * @possible_crtcs: bitmask of possible CRTCs
1099 * @funcs: callbacks for the new plane
1100 * @formats: array of supported formats (%DRM_FORMAT_*)
1101 * @format_count: number of elements in @formats
1102 * @type: type of plane (overlay, primary, cursor)
1104 * Initializes a plane object of type @type.
1107 * Zero on success, error code on failure.
1109 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1110 unsigned long possible_crtcs,
1111 const struct drm_plane_funcs *funcs,
1112 const uint32_t *formats, uint32_t format_count,
1113 enum drm_plane_type type)
1117 drm_modeset_lock_all(dev);
1119 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1123 plane->base.properties = &plane->properties;
1125 plane->funcs = funcs;
1126 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1128 if (!plane->format_types) {
1129 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1130 drm_mode_object_put(dev, &plane->base);
1135 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1136 plane->format_count = format_count;
1137 plane->possible_crtcs = possible_crtcs;
1140 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1141 dev->mode_config.num_total_plane++;
1142 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1143 dev->mode_config.num_overlay_plane++;
1145 drm_object_attach_property(&plane->base,
1146 dev->mode_config.plane_type_property,
1150 drm_modeset_unlock_all(dev);
1154 EXPORT_SYMBOL(drm_universal_plane_init);
1157 * drm_plane_init - Initialize a legacy plane
1159 * @plane: plane object to init
1160 * @possible_crtcs: bitmask of possible CRTCs
1161 * @funcs: callbacks for the new plane
1162 * @formats: array of supported formats (%DRM_FORMAT_*)
1163 * @format_count: number of elements in @formats
1164 * @is_primary: plane type (primary vs overlay)
1166 * Legacy API to initialize a DRM plane.
1168 * New drivers should call drm_universal_plane_init() instead.
1171 * Zero on success, error code on failure.
1173 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1174 unsigned long possible_crtcs,
1175 const struct drm_plane_funcs *funcs,
1176 const uint32_t *formats, uint32_t format_count,
1179 enum drm_plane_type type;
1181 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1182 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1183 formats, format_count, type);
1185 EXPORT_SYMBOL(drm_plane_init);
1188 * drm_plane_cleanup - Clean up the core plane usage
1189 * @plane: plane to cleanup
1191 * This function cleans up @plane and removes it from the DRM mode setting
1192 * core. Note that the function does *not* free the plane structure itself,
1193 * this is the responsibility of the caller.
1195 void drm_plane_cleanup(struct drm_plane *plane)
1197 struct drm_device *dev = plane->dev;
1199 drm_modeset_lock_all(dev);
1200 kfree(plane->format_types);
1201 drm_mode_object_put(dev, &plane->base);
1203 BUG_ON(list_empty(&plane->head));
1205 list_del(&plane->head);
1206 dev->mode_config.num_total_plane--;
1207 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1208 dev->mode_config.num_overlay_plane--;
1209 drm_modeset_unlock_all(dev);
1211 EXPORT_SYMBOL(drm_plane_cleanup);
1214 * drm_plane_force_disable - Forcibly disable a plane
1215 * @plane: plane to disable
1217 * Forces the plane to be disabled.
1219 * Used when the plane's current framebuffer is destroyed,
1220 * and when restoring fbdev mode.
1222 void drm_plane_force_disable(struct drm_plane *plane)
1224 struct drm_framebuffer *old_fb = plane->fb;
1230 ret = plane->funcs->disable_plane(plane);
1232 DRM_ERROR("failed to disable plane with busy fb\n");
1235 /* disconnect the plane from the fb and crtc: */
1236 __drm_framebuffer_unreference(old_fb);
1240 EXPORT_SYMBOL(drm_plane_force_disable);
1242 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1244 struct drm_property *edid;
1245 struct drm_property *dpms;
1248 * Standard properties (apply to all connectors)
1250 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1251 DRM_MODE_PROP_IMMUTABLE,
1253 dev->mode_config.edid_property = edid;
1255 dpms = drm_property_create_enum(dev, 0,
1256 "DPMS", drm_dpms_enum_list,
1257 ARRAY_SIZE(drm_dpms_enum_list));
1258 dev->mode_config.dpms_property = dpms;
1263 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1265 struct drm_property *type;
1268 * Standard properties (apply to all planes)
1270 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1271 "type", drm_plane_type_enum_list,
1272 ARRAY_SIZE(drm_plane_type_enum_list));
1273 dev->mode_config.plane_type_property = type;
1279 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1282 * Called by a driver the first time a DVI-I connector is made.
1284 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1286 struct drm_property *dvi_i_selector;
1287 struct drm_property *dvi_i_subconnector;
1289 if (dev->mode_config.dvi_i_select_subconnector_property)
1293 drm_property_create_enum(dev, 0,
1294 "select subconnector",
1295 drm_dvi_i_select_enum_list,
1296 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1297 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1299 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1301 drm_dvi_i_subconnector_enum_list,
1302 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1303 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1307 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1310 * drm_create_tv_properties - create TV specific connector properties
1312 * @num_modes: number of different TV formats (modes) supported
1313 * @modes: array of pointers to strings containing name of each format
1315 * Called by a driver's TV initialization routine, this function creates
1316 * the TV specific connector properties for a given device. Caller is
1317 * responsible for allocating a list of format names and passing them to
1320 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1323 struct drm_property *tv_selector;
1324 struct drm_property *tv_subconnector;
1327 if (dev->mode_config.tv_select_subconnector_property)
1331 * Basic connector properties
1333 tv_selector = drm_property_create_enum(dev, 0,
1334 "select subconnector",
1335 drm_tv_select_enum_list,
1336 ARRAY_SIZE(drm_tv_select_enum_list));
1337 dev->mode_config.tv_select_subconnector_property = tv_selector;
1340 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1342 drm_tv_subconnector_enum_list,
1343 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1344 dev->mode_config.tv_subconnector_property = tv_subconnector;
1347 * Other, TV specific properties: margins & TV modes.
1349 dev->mode_config.tv_left_margin_property =
1350 drm_property_create_range(dev, 0, "left margin", 0, 100);
1352 dev->mode_config.tv_right_margin_property =
1353 drm_property_create_range(dev, 0, "right margin", 0, 100);
1355 dev->mode_config.tv_top_margin_property =
1356 drm_property_create_range(dev, 0, "top margin", 0, 100);
1358 dev->mode_config.tv_bottom_margin_property =
1359 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1361 dev->mode_config.tv_mode_property =
1362 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1364 for (i = 0; i < num_modes; i++)
1365 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1368 dev->mode_config.tv_brightness_property =
1369 drm_property_create_range(dev, 0, "brightness", 0, 100);
1371 dev->mode_config.tv_contrast_property =
1372 drm_property_create_range(dev, 0, "contrast", 0, 100);
1374 dev->mode_config.tv_flicker_reduction_property =
1375 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1377 dev->mode_config.tv_overscan_property =
1378 drm_property_create_range(dev, 0, "overscan", 0, 100);
1380 dev->mode_config.tv_saturation_property =
1381 drm_property_create_range(dev, 0, "saturation", 0, 100);
1383 dev->mode_config.tv_hue_property =
1384 drm_property_create_range(dev, 0, "hue", 0, 100);
1388 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1391 * drm_mode_create_scaling_mode_property - create scaling mode property
1394 * Called by a driver the first time it's needed, must be attached to desired
1397 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1399 struct drm_property *scaling_mode;
1401 if (dev->mode_config.scaling_mode_property)
1405 drm_property_create_enum(dev, 0, "scaling mode",
1406 drm_scaling_mode_enum_list,
1407 ARRAY_SIZE(drm_scaling_mode_enum_list));
1409 dev->mode_config.scaling_mode_property = scaling_mode;
1413 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1416 * drm_mode_create_dirty_property - create dirty property
1419 * Called by a driver the first time it's needed, must be attached to desired
1422 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1424 struct drm_property *dirty_info;
1426 if (dev->mode_config.dirty_info_property)
1430 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1432 drm_dirty_info_enum_list,
1433 ARRAY_SIZE(drm_dirty_info_enum_list));
1434 dev->mode_config.dirty_info_property = dirty_info;
1438 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1440 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1442 uint32_t total_objects = 0;
1444 total_objects += dev->mode_config.num_crtc;
1445 total_objects += dev->mode_config.num_connector;
1446 total_objects += dev->mode_config.num_encoder;
1447 total_objects += dev->mode_config.num_bridge;
1449 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1450 if (!group->id_list)
1453 group->num_crtcs = 0;
1454 group->num_connectors = 0;
1455 group->num_encoders = 0;
1456 group->num_bridges = 0;
1460 void drm_mode_group_destroy(struct drm_mode_group *group)
1462 kfree(group->id_list);
1463 group->id_list = NULL;
1467 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1468 * the drm core's responsibility to set up mode control groups.
1470 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1471 struct drm_mode_group *group)
1473 struct drm_crtc *crtc;
1474 struct drm_encoder *encoder;
1475 struct drm_connector *connector;
1476 struct drm_bridge *bridge;
1479 if ((ret = drm_mode_group_init(dev, group)))
1482 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1483 group->id_list[group->num_crtcs++] = crtc->base.id;
1485 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1486 group->id_list[group->num_crtcs + group->num_encoders++] =
1489 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1490 group->id_list[group->num_crtcs + group->num_encoders +
1491 group->num_connectors++] = connector->base.id;
1493 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1494 group->id_list[group->num_crtcs + group->num_encoders +
1495 group->num_connectors + group->num_bridges++] =
1500 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1503 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1504 * @out: drm_mode_modeinfo struct to return to the user
1505 * @in: drm_display_mode to use
1507 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1510 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1511 const struct drm_display_mode *in)
1513 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1514 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1515 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1516 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1517 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1518 "timing values too large for mode info\n");
1520 out->clock = in->clock;
1521 out->hdisplay = in->hdisplay;
1522 out->hsync_start = in->hsync_start;
1523 out->hsync_end = in->hsync_end;
1524 out->htotal = in->htotal;
1525 out->hskew = in->hskew;
1526 out->vdisplay = in->vdisplay;
1527 out->vsync_start = in->vsync_start;
1528 out->vsync_end = in->vsync_end;
1529 out->vtotal = in->vtotal;
1530 out->vscan = in->vscan;
1531 out->vrefresh = in->vrefresh;
1532 out->flags = in->flags;
1533 out->type = in->type;
1534 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1535 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1539 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1540 * @out: drm_display_mode to return to the user
1541 * @in: drm_mode_modeinfo to use
1543 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1547 * Zero on success, errno on failure.
1549 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1550 const struct drm_mode_modeinfo *in)
1552 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1555 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1558 out->clock = in->clock;
1559 out->hdisplay = in->hdisplay;
1560 out->hsync_start = in->hsync_start;
1561 out->hsync_end = in->hsync_end;
1562 out->htotal = in->htotal;
1563 out->hskew = in->hskew;
1564 out->vdisplay = in->vdisplay;
1565 out->vsync_start = in->vsync_start;
1566 out->vsync_end = in->vsync_end;
1567 out->vtotal = in->vtotal;
1568 out->vscan = in->vscan;
1569 out->vrefresh = in->vrefresh;
1570 out->flags = in->flags;
1571 out->type = in->type;
1572 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1573 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1579 * drm_mode_getresources - get graphics configuration
1580 * @dev: drm device for the ioctl
1581 * @data: data pointer for the ioctl
1582 * @file_priv: drm file for the ioctl call
1584 * Construct a set of configuration description structures and return
1585 * them to the user, including CRTC, connector and framebuffer configuration.
1587 * Called by the user via ioctl.
1590 * Zero on success, errno on failure.
1592 int drm_mode_getresources(struct drm_device *dev, void *data,
1593 struct drm_file *file_priv)
1595 struct drm_mode_card_res *card_res = data;
1596 struct list_head *lh;
1597 struct drm_framebuffer *fb;
1598 struct drm_connector *connector;
1599 struct drm_crtc *crtc;
1600 struct drm_encoder *encoder;
1602 int connector_count = 0;
1605 int encoder_count = 0;
1607 uint32_t __user *fb_id;
1608 uint32_t __user *crtc_id;
1609 uint32_t __user *connector_id;
1610 uint32_t __user *encoder_id;
1611 struct drm_mode_group *mode_group;
1613 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1617 mutex_lock(&file_priv->fbs_lock);
1619 * For the non-control nodes we need to limit the list of resources
1620 * by IDs in the group list for this node
1622 list_for_each(lh, &file_priv->fbs)
1625 /* handle this in 4 parts */
1627 if (card_res->count_fbs >= fb_count) {
1629 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1630 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1631 if (put_user(fb->base.id, fb_id + copied)) {
1632 mutex_unlock(&file_priv->fbs_lock);
1638 card_res->count_fbs = fb_count;
1639 mutex_unlock(&file_priv->fbs_lock);
1641 drm_modeset_lock_all(dev);
1642 if (!drm_is_primary_client(file_priv)) {
1645 list_for_each(lh, &dev->mode_config.crtc_list)
1648 list_for_each(lh, &dev->mode_config.connector_list)
1651 list_for_each(lh, &dev->mode_config.encoder_list)
1655 crtc_count = mode_group->num_crtcs;
1656 connector_count = mode_group->num_connectors;
1657 encoder_count = mode_group->num_encoders;
1660 card_res->max_height = dev->mode_config.max_height;
1661 card_res->min_height = dev->mode_config.min_height;
1662 card_res->max_width = dev->mode_config.max_width;
1663 card_res->min_width = dev->mode_config.min_width;
1666 if (card_res->count_crtcs >= crtc_count) {
1668 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1670 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1672 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1673 if (put_user(crtc->base.id, crtc_id + copied)) {
1680 for (i = 0; i < mode_group->num_crtcs; i++) {
1681 if (put_user(mode_group->id_list[i],
1682 crtc_id + copied)) {
1690 card_res->count_crtcs = crtc_count;
1693 if (card_res->count_encoders >= encoder_count) {
1695 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1697 list_for_each_entry(encoder,
1698 &dev->mode_config.encoder_list,
1700 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1702 if (put_user(encoder->base.id, encoder_id +
1710 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1711 if (put_user(mode_group->id_list[i],
1712 encoder_id + copied)) {
1721 card_res->count_encoders = encoder_count;
1724 if (card_res->count_connectors >= connector_count) {
1726 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1728 list_for_each_entry(connector,
1729 &dev->mode_config.connector_list,
1731 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1734 if (put_user(connector->base.id,
1735 connector_id + copied)) {
1742 int start = mode_group->num_crtcs +
1743 mode_group->num_encoders;
1744 for (i = start; i < start + mode_group->num_connectors; i++) {
1745 if (put_user(mode_group->id_list[i],
1746 connector_id + copied)) {
1754 card_res->count_connectors = connector_count;
1756 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1757 card_res->count_connectors, card_res->count_encoders);
1760 drm_modeset_unlock_all(dev);
1765 * drm_mode_getcrtc - get CRTC configuration
1766 * @dev: drm device for the ioctl
1767 * @data: data pointer for the ioctl
1768 * @file_priv: drm file for the ioctl call
1770 * Construct a CRTC configuration structure to return to the user.
1772 * Called by the user via ioctl.
1775 * Zero on success, errno on failure.
1777 int drm_mode_getcrtc(struct drm_device *dev,
1778 void *data, struct drm_file *file_priv)
1780 struct drm_mode_crtc *crtc_resp = data;
1781 struct drm_crtc *crtc;
1784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1787 drm_modeset_lock_all(dev);
1789 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1795 crtc_resp->x = crtc->x;
1796 crtc_resp->y = crtc->y;
1797 crtc_resp->gamma_size = crtc->gamma_size;
1798 if (crtc->primary->fb)
1799 crtc_resp->fb_id = crtc->primary->fb->base.id;
1801 crtc_resp->fb_id = 0;
1803 if (crtc->enabled) {
1805 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1806 crtc_resp->mode_valid = 1;
1809 crtc_resp->mode_valid = 0;
1813 drm_modeset_unlock_all(dev);
1817 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1818 const struct drm_file *file_priv)
1821 * If user-space hasn't configured the driver to expose the stereo 3D
1822 * modes, don't expose them.
1824 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1831 * drm_mode_getconnector - get connector configuration
1832 * @dev: drm device for the ioctl
1833 * @data: data pointer for the ioctl
1834 * @file_priv: drm file for the ioctl call
1836 * Construct a connector configuration structure to return to the user.
1838 * Called by the user via ioctl.
1841 * Zero on success, errno on failure.
1843 int drm_mode_getconnector(struct drm_device *dev, void *data,
1844 struct drm_file *file_priv)
1846 struct drm_mode_get_connector *out_resp = data;
1847 struct drm_connector *connector;
1848 struct drm_display_mode *mode;
1850 int props_count = 0;
1851 int encoders_count = 0;
1855 struct drm_mode_modeinfo u_mode;
1856 struct drm_mode_modeinfo __user *mode_ptr;
1857 uint32_t __user *prop_ptr;
1858 uint64_t __user *prop_values;
1859 uint32_t __user *encoder_ptr;
1861 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1864 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1866 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1868 mutex_lock(&dev->mode_config.mutex);
1869 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1871 connector = drm_connector_find(dev, out_resp->connector_id);
1877 props_count = connector->properties.count;
1879 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1880 if (connector->encoder_ids[i] != 0) {
1885 if (out_resp->count_modes == 0) {
1886 connector->funcs->fill_modes(connector,
1887 dev->mode_config.max_width,
1888 dev->mode_config.max_height);
1891 /* delayed so we get modes regardless of pre-fill_modes state */
1892 list_for_each_entry(mode, &connector->modes, head)
1893 if (drm_mode_expose_to_userspace(mode, file_priv))
1896 out_resp->connector_id = connector->base.id;
1897 out_resp->connector_type = connector->connector_type;
1898 out_resp->connector_type_id = connector->connector_type_id;
1899 out_resp->mm_width = connector->display_info.width_mm;
1900 out_resp->mm_height = connector->display_info.height_mm;
1901 out_resp->subpixel = connector->display_info.subpixel_order;
1902 out_resp->connection = connector->status;
1903 if (connector->encoder)
1904 out_resp->encoder_id = connector->encoder->base.id;
1906 out_resp->encoder_id = 0;
1909 * This ioctl is called twice, once to determine how much space is
1910 * needed, and the 2nd time to fill it.
1912 if ((out_resp->count_modes >= mode_count) && mode_count) {
1914 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1915 list_for_each_entry(mode, &connector->modes, head) {
1916 if (!drm_mode_expose_to_userspace(mode, file_priv))
1919 drm_crtc_convert_to_umode(&u_mode, mode);
1920 if (copy_to_user(mode_ptr + copied,
1921 &u_mode, sizeof(u_mode))) {
1928 out_resp->count_modes = mode_count;
1930 if ((out_resp->count_props >= props_count) && props_count) {
1932 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1933 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1934 for (i = 0; i < connector->properties.count; i++) {
1935 if (put_user(connector->properties.ids[i],
1936 prop_ptr + copied)) {
1941 if (put_user(connector->properties.values[i],
1942 prop_values + copied)) {
1949 out_resp->count_props = props_count;
1951 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1953 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1954 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1955 if (connector->encoder_ids[i] != 0) {
1956 if (put_user(connector->encoder_ids[i],
1957 encoder_ptr + copied)) {
1965 out_resp->count_encoders = encoders_count;
1968 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1969 mutex_unlock(&dev->mode_config.mutex);
1975 * drm_mode_getencoder - get encoder configuration
1976 * @dev: drm device for the ioctl
1977 * @data: data pointer for the ioctl
1978 * @file_priv: drm file for the ioctl call
1980 * Construct a encoder configuration structure to return to the user.
1982 * Called by the user via ioctl.
1985 * Zero on success, errno on failure.
1987 int drm_mode_getencoder(struct drm_device *dev, void *data,
1988 struct drm_file *file_priv)
1990 struct drm_mode_get_encoder *enc_resp = data;
1991 struct drm_encoder *encoder;
1994 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1997 drm_modeset_lock_all(dev);
1998 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2005 enc_resp->crtc_id = encoder->crtc->base.id;
2007 enc_resp->crtc_id = 0;
2008 enc_resp->encoder_type = encoder->encoder_type;
2009 enc_resp->encoder_id = encoder->base.id;
2010 enc_resp->possible_crtcs = encoder->possible_crtcs;
2011 enc_resp->possible_clones = encoder->possible_clones;
2014 drm_modeset_unlock_all(dev);
2019 * drm_mode_getplane_res - enumerate all plane resources
2022 * @file_priv: DRM file info
2024 * Construct a list of plane ids to return to the user.
2026 * Called by the user via ioctl.
2029 * Zero on success, errno on failure.
2031 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2032 struct drm_file *file_priv)
2034 struct drm_mode_get_plane_res *plane_resp = data;
2035 struct drm_mode_config *config;
2036 struct drm_plane *plane;
2037 uint32_t __user *plane_ptr;
2038 int copied = 0, ret = 0;
2039 unsigned num_planes;
2041 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2044 drm_modeset_lock_all(dev);
2045 config = &dev->mode_config;
2047 if (file_priv->universal_planes)
2048 num_planes = config->num_total_plane;
2050 num_planes = config->num_overlay_plane;
2053 * This ioctl is called twice, once to determine how much space is
2054 * needed, and the 2nd time to fill it.
2057 (plane_resp->count_planes >= num_planes)) {
2058 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2060 list_for_each_entry(plane, &config->plane_list, head) {
2062 * Unless userspace set the 'universal planes'
2063 * capability bit, only advertise overlays.
2065 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2066 !file_priv->universal_planes)
2069 if (put_user(plane->base.id, plane_ptr + copied)) {
2076 plane_resp->count_planes = num_planes;
2079 drm_modeset_unlock_all(dev);
2084 * drm_mode_getplane - get plane configuration
2087 * @file_priv: DRM file info
2089 * Construct a plane configuration structure to return to the user.
2091 * Called by the user via ioctl.
2094 * Zero on success, errno on failure.
2096 int drm_mode_getplane(struct drm_device *dev, void *data,
2097 struct drm_file *file_priv)
2099 struct drm_mode_get_plane *plane_resp = data;
2100 struct drm_plane *plane;
2101 uint32_t __user *format_ptr;
2104 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2107 drm_modeset_lock_all(dev);
2108 plane = drm_plane_find(dev, plane_resp->plane_id);
2115 plane_resp->crtc_id = plane->crtc->base.id;
2117 plane_resp->crtc_id = 0;
2120 plane_resp->fb_id = plane->fb->base.id;
2122 plane_resp->fb_id = 0;
2124 plane_resp->plane_id = plane->base.id;
2125 plane_resp->possible_crtcs = plane->possible_crtcs;
2126 plane_resp->gamma_size = 0;
2129 * This ioctl is called twice, once to determine how much space is
2130 * needed, and the 2nd time to fill it.
2132 if (plane->format_count &&
2133 (plane_resp->count_format_types >= plane->format_count)) {
2134 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2135 if (copy_to_user(format_ptr,
2136 plane->format_types,
2137 sizeof(uint32_t) * plane->format_count)) {
2142 plane_resp->count_format_types = plane->format_count;
2145 drm_modeset_unlock_all(dev);
2150 * drm_mode_setplane - configure a plane's configuration
2152 * @data: ioctl data*
2153 * @file_priv: DRM file info
2155 * Set plane configuration, including placement, fb, scaling, and other factors.
2156 * Or pass a NULL fb to disable.
2159 * Zero on success, errno on failure.
2161 int drm_mode_setplane(struct drm_device *dev, void *data,
2162 struct drm_file *file_priv)
2164 struct drm_mode_set_plane *plane_req = data;
2165 struct drm_plane *plane;
2166 struct drm_crtc *crtc;
2167 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2169 unsigned int fb_width, fb_height;
2172 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2176 * First, find the plane, crtc, and fb objects. If not available,
2177 * we don't bother to call the driver.
2179 plane = drm_plane_find(dev, plane_req->plane_id);
2181 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2182 plane_req->plane_id);
2186 /* No fb means shut it down */
2187 if (!plane_req->fb_id) {
2188 drm_modeset_lock_all(dev);
2190 ret = plane->funcs->disable_plane(plane);
2197 drm_modeset_unlock_all(dev);
2201 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2203 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2204 plane_req->crtc_id);
2209 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2211 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2217 /* Check whether this plane supports the fb pixel format. */
2218 for (i = 0; i < plane->format_count; i++)
2219 if (fb->pixel_format == plane->format_types[i])
2221 if (i == plane->format_count) {
2222 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2223 drm_get_format_name(fb->pixel_format));
2228 fb_width = fb->width << 16;
2229 fb_height = fb->height << 16;
2231 /* Make sure source coordinates are inside the fb. */
2232 if (plane_req->src_w > fb_width ||
2233 plane_req->src_x > fb_width - plane_req->src_w ||
2234 plane_req->src_h > fb_height ||
2235 plane_req->src_y > fb_height - plane_req->src_h) {
2236 DRM_DEBUG_KMS("Invalid source coordinates "
2237 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2238 plane_req->src_w >> 16,
2239 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2240 plane_req->src_h >> 16,
2241 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2242 plane_req->src_x >> 16,
2243 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2244 plane_req->src_y >> 16,
2245 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2250 /* Give drivers some help against integer overflows */
2251 if (plane_req->crtc_w > INT_MAX ||
2252 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2253 plane_req->crtc_h > INT_MAX ||
2254 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2255 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2256 plane_req->crtc_w, plane_req->crtc_h,
2257 plane_req->crtc_x, plane_req->crtc_y);
2262 drm_modeset_lock_all(dev);
2264 ret = plane->funcs->update_plane(plane, crtc, fb,
2265 plane_req->crtc_x, plane_req->crtc_y,
2266 plane_req->crtc_w, plane_req->crtc_h,
2267 plane_req->src_x, plane_req->src_y,
2268 plane_req->src_w, plane_req->src_h);
2276 drm_modeset_unlock_all(dev);
2280 drm_framebuffer_unreference(fb);
2282 drm_framebuffer_unreference(old_fb);
2288 * drm_mode_set_config_internal - helper to call ->set_config
2289 * @set: modeset config to set
2291 * This is a little helper to wrap internal calls to the ->set_config driver
2292 * interface. The only thing it adds is correct refcounting dance.
2295 * Zero on success, errno on failure.
2297 int drm_mode_set_config_internal(struct drm_mode_set *set)
2299 struct drm_crtc *crtc = set->crtc;
2300 struct drm_framebuffer *fb;
2301 struct drm_crtc *tmp;
2305 * NOTE: ->set_config can also disable other crtcs (if we steal all
2306 * connectors from it), hence we need to refcount the fbs across all
2307 * crtcs. Atomic modeset will have saner semantics ...
2309 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2310 tmp->old_fb = tmp->primary->fb;
2314 ret = crtc->funcs->set_config(set);
2316 crtc->primary->crtc = crtc;
2317 crtc->primary->fb = fb;
2320 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2321 if (tmp->primary->fb)
2322 drm_framebuffer_reference(tmp->primary->fb);
2324 drm_framebuffer_unreference(tmp->old_fb);
2329 EXPORT_SYMBOL(drm_mode_set_config_internal);
2332 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2334 * @crtc: CRTC that framebuffer will be displayed on
2337 * @mode: mode that framebuffer will be displayed under
2338 * @fb: framebuffer to check size of
2340 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2342 const struct drm_display_mode *mode,
2343 const struct drm_framebuffer *fb)
2346 int hdisplay, vdisplay;
2348 hdisplay = mode->hdisplay;
2349 vdisplay = mode->vdisplay;
2351 if (drm_mode_is_stereo(mode)) {
2352 struct drm_display_mode adjusted = *mode;
2354 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2355 hdisplay = adjusted.crtc_hdisplay;
2356 vdisplay = adjusted.crtc_vdisplay;
2359 if (crtc->invert_dimensions)
2360 swap(hdisplay, vdisplay);
2362 if (hdisplay > fb->width ||
2363 vdisplay > fb->height ||
2364 x > fb->width - hdisplay ||
2365 y > fb->height - vdisplay) {
2366 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2367 fb->width, fb->height, hdisplay, vdisplay, x, y,
2368 crtc->invert_dimensions ? " (inverted)" : "");
2374 EXPORT_SYMBOL(drm_crtc_check_viewport);
2377 * drm_mode_setcrtc - set CRTC configuration
2378 * @dev: drm device for the ioctl
2379 * @data: data pointer for the ioctl
2380 * @file_priv: drm file for the ioctl call
2382 * Build a new CRTC configuration based on user request.
2384 * Called by the user via ioctl.
2387 * Zero on success, errno on failure.
2389 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2390 struct drm_file *file_priv)
2392 struct drm_mode_config *config = &dev->mode_config;
2393 struct drm_mode_crtc *crtc_req = data;
2394 struct drm_crtc *crtc;
2395 struct drm_connector **connector_set = NULL, *connector;
2396 struct drm_framebuffer *fb = NULL;
2397 struct drm_display_mode *mode = NULL;
2398 struct drm_mode_set set;
2399 uint32_t __user *set_connectors_ptr;
2403 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2406 /* For some reason crtc x/y offsets are signed internally. */
2407 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2410 drm_modeset_lock_all(dev);
2411 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2413 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2417 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2419 if (crtc_req->mode_valid) {
2420 /* If we have a mode we need a framebuffer. */
2421 /* If we pass -1, set the mode with the currently bound fb */
2422 if (crtc_req->fb_id == -1) {
2423 if (!crtc->primary->fb) {
2424 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2428 fb = crtc->primary->fb;
2429 /* Make refcounting symmetric with the lookup path. */
2430 drm_framebuffer_reference(fb);
2432 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2434 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2441 mode = drm_mode_create(dev);
2447 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2449 DRM_DEBUG_KMS("Invalid mode\n");
2453 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2455 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2462 if (crtc_req->count_connectors == 0 && mode) {
2463 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2468 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2469 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2470 crtc_req->count_connectors);
2475 if (crtc_req->count_connectors > 0) {
2478 /* Avoid unbounded kernel memory allocation */
2479 if (crtc_req->count_connectors > config->num_connector) {
2484 connector_set = kmalloc(crtc_req->count_connectors *
2485 sizeof(struct drm_connector *),
2487 if (!connector_set) {
2492 for (i = 0; i < crtc_req->count_connectors; i++) {
2493 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2494 if (get_user(out_id, &set_connectors_ptr[i])) {
2499 connector = drm_connector_find(dev, out_id);
2501 DRM_DEBUG_KMS("Connector id %d unknown\n",
2506 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2510 connector_set[i] = connector;
2515 set.x = crtc_req->x;
2516 set.y = crtc_req->y;
2518 set.connectors = connector_set;
2519 set.num_connectors = crtc_req->count_connectors;
2521 ret = drm_mode_set_config_internal(&set);
2525 drm_framebuffer_unreference(fb);
2527 kfree(connector_set);
2528 drm_mode_destroy(dev, mode);
2529 drm_modeset_unlock_all(dev);
2533 static int drm_mode_cursor_common(struct drm_device *dev,
2534 struct drm_mode_cursor2 *req,
2535 struct drm_file *file_priv)
2537 struct drm_crtc *crtc;
2540 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2543 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2546 crtc = drm_crtc_find(dev, req->crtc_id);
2548 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2552 drm_modeset_lock(&crtc->mutex, NULL);
2553 if (req->flags & DRM_MODE_CURSOR_BO) {
2554 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2558 /* Turns off the cursor if handle is 0 */
2559 if (crtc->funcs->cursor_set2)
2560 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2561 req->width, req->height, req->hot_x, req->hot_y);
2563 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2564 req->width, req->height);
2567 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2568 if (crtc->funcs->cursor_move) {
2569 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2576 drm_modeset_unlock(&crtc->mutex);
2584 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2585 * @dev: drm device for the ioctl
2586 * @data: data pointer for the ioctl
2587 * @file_priv: drm file for the ioctl call
2589 * Set the cursor configuration based on user request.
2591 * Called by the user via ioctl.
2594 * Zero on success, errno on failure.
2596 int drm_mode_cursor_ioctl(struct drm_device *dev,
2597 void *data, struct drm_file *file_priv)
2599 struct drm_mode_cursor *req = data;
2600 struct drm_mode_cursor2 new_req;
2602 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2603 new_req.hot_x = new_req.hot_y = 0;
2605 return drm_mode_cursor_common(dev, &new_req, file_priv);
2609 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2610 * @dev: drm device for the ioctl
2611 * @data: data pointer for the ioctl
2612 * @file_priv: drm file for the ioctl call
2614 * Set the cursor configuration based on user request. This implements the 2nd
2615 * version of the cursor ioctl, which allows userspace to additionally specify
2616 * the hotspot of the pointer.
2618 * Called by the user via ioctl.
2621 * Zero on success, errno on failure.
2623 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2624 void *data, struct drm_file *file_priv)
2626 struct drm_mode_cursor2 *req = data;
2627 return drm_mode_cursor_common(dev, req, file_priv);
2631 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2632 * @bpp: bits per pixels
2633 * @depth: bit depth per pixel
2635 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2636 * Useful in fbdev emulation code, since that deals in those values.
2638 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2644 fmt = DRM_FORMAT_C8;
2648 fmt = DRM_FORMAT_XRGB1555;
2650 fmt = DRM_FORMAT_RGB565;
2653 fmt = DRM_FORMAT_RGB888;
2657 fmt = DRM_FORMAT_XRGB8888;
2658 else if (depth == 30)
2659 fmt = DRM_FORMAT_XRGB2101010;
2661 fmt = DRM_FORMAT_ARGB8888;
2664 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2665 fmt = DRM_FORMAT_XRGB8888;
2671 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2674 * drm_mode_addfb - add an FB to the graphics configuration
2675 * @dev: drm device for the ioctl
2676 * @data: data pointer for the ioctl
2677 * @file_priv: drm file for the ioctl call
2679 * Add a new FB to the specified CRTC, given a user request. This is the
2680 * original addfb ioclt which only supported RGB formats.
2682 * Called by the user via ioctl.
2685 * Zero on success, errno on failure.
2687 int drm_mode_addfb(struct drm_device *dev,
2688 void *data, struct drm_file *file_priv)
2690 struct drm_mode_fb_cmd *or = data;
2691 struct drm_mode_fb_cmd2 r = {};
2692 struct drm_mode_config *config = &dev->mode_config;
2693 struct drm_framebuffer *fb;
2696 /* Use new struct with format internally */
2697 r.fb_id = or->fb_id;
2698 r.width = or->width;
2699 r.height = or->height;
2700 r.pitches[0] = or->pitch;
2701 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2702 r.handles[0] = or->handle;
2704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2707 if ((config->min_width > r.width) || (r.width > config->max_width))
2710 if ((config->min_height > r.height) || (r.height > config->max_height))
2713 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2715 DRM_DEBUG_KMS("could not create framebuffer\n");
2719 mutex_lock(&file_priv->fbs_lock);
2720 or->fb_id = fb->base.id;
2721 list_add(&fb->filp_head, &file_priv->fbs);
2722 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2723 mutex_unlock(&file_priv->fbs_lock);
2728 static int format_check(const struct drm_mode_fb_cmd2 *r)
2730 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2734 case DRM_FORMAT_RGB332:
2735 case DRM_FORMAT_BGR233:
2736 case DRM_FORMAT_XRGB4444:
2737 case DRM_FORMAT_XBGR4444:
2738 case DRM_FORMAT_RGBX4444:
2739 case DRM_FORMAT_BGRX4444:
2740 case DRM_FORMAT_ARGB4444:
2741 case DRM_FORMAT_ABGR4444:
2742 case DRM_FORMAT_RGBA4444:
2743 case DRM_FORMAT_BGRA4444:
2744 case DRM_FORMAT_XRGB1555:
2745 case DRM_FORMAT_XBGR1555:
2746 case DRM_FORMAT_RGBX5551:
2747 case DRM_FORMAT_BGRX5551:
2748 case DRM_FORMAT_ARGB1555:
2749 case DRM_FORMAT_ABGR1555:
2750 case DRM_FORMAT_RGBA5551:
2751 case DRM_FORMAT_BGRA5551:
2752 case DRM_FORMAT_RGB565:
2753 case DRM_FORMAT_BGR565:
2754 case DRM_FORMAT_RGB888:
2755 case DRM_FORMAT_BGR888:
2756 case DRM_FORMAT_XRGB8888:
2757 case DRM_FORMAT_XBGR8888:
2758 case DRM_FORMAT_RGBX8888:
2759 case DRM_FORMAT_BGRX8888:
2760 case DRM_FORMAT_ARGB8888:
2761 case DRM_FORMAT_ABGR8888:
2762 case DRM_FORMAT_RGBA8888:
2763 case DRM_FORMAT_BGRA8888:
2764 case DRM_FORMAT_XRGB2101010:
2765 case DRM_FORMAT_XBGR2101010:
2766 case DRM_FORMAT_RGBX1010102:
2767 case DRM_FORMAT_BGRX1010102:
2768 case DRM_FORMAT_ARGB2101010:
2769 case DRM_FORMAT_ABGR2101010:
2770 case DRM_FORMAT_RGBA1010102:
2771 case DRM_FORMAT_BGRA1010102:
2772 case DRM_FORMAT_YUYV:
2773 case DRM_FORMAT_YVYU:
2774 case DRM_FORMAT_UYVY:
2775 case DRM_FORMAT_VYUY:
2776 case DRM_FORMAT_AYUV:
2777 case DRM_FORMAT_NV12:
2778 case DRM_FORMAT_NV21:
2779 case DRM_FORMAT_NV16:
2780 case DRM_FORMAT_NV61:
2781 case DRM_FORMAT_NV24:
2782 case DRM_FORMAT_NV42:
2783 case DRM_FORMAT_YUV410:
2784 case DRM_FORMAT_YVU410:
2785 case DRM_FORMAT_YUV411:
2786 case DRM_FORMAT_YVU411:
2787 case DRM_FORMAT_YUV420:
2788 case DRM_FORMAT_YVU420:
2789 case DRM_FORMAT_YUV422:
2790 case DRM_FORMAT_YVU422:
2791 case DRM_FORMAT_YUV444:
2792 case DRM_FORMAT_YVU444:
2795 DRM_DEBUG_KMS("invalid pixel format %s\n",
2796 drm_get_format_name(r->pixel_format));
2801 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2803 int ret, hsub, vsub, num_planes, i;
2805 ret = format_check(r);
2807 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2808 drm_get_format_name(r->pixel_format));
2812 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2813 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2814 num_planes = drm_format_num_planes(r->pixel_format);
2816 if (r->width == 0 || r->width % hsub) {
2817 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2821 if (r->height == 0 || r->height % vsub) {
2822 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2826 for (i = 0; i < num_planes; i++) {
2827 unsigned int width = r->width / (i != 0 ? hsub : 1);
2828 unsigned int height = r->height / (i != 0 ? vsub : 1);
2829 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2831 if (!r->handles[i]) {
2832 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2836 if ((uint64_t) width * cpp > UINT_MAX)
2839 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2842 if (r->pitches[i] < width * cpp) {
2843 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2852 * drm_mode_addfb2 - add an FB to the graphics configuration
2853 * @dev: drm device for the ioctl
2854 * @data: data pointer for the ioctl
2855 * @file_priv: drm file for the ioctl call
2857 * Add a new FB to the specified CRTC, given a user request with format. This is
2858 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2859 * and uses fourcc codes as pixel format specifiers.
2861 * Called by the user via ioctl.
2864 * Zero on success, errno on failure.
2866 int drm_mode_addfb2(struct drm_device *dev,
2867 void *data, struct drm_file *file_priv)
2869 struct drm_mode_fb_cmd2 *r = data;
2870 struct drm_mode_config *config = &dev->mode_config;
2871 struct drm_framebuffer *fb;
2874 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2877 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2878 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2882 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2883 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2884 r->width, config->min_width, config->max_width);
2887 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2888 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2889 r->height, config->min_height, config->max_height);
2893 ret = framebuffer_check(r);
2897 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2899 DRM_DEBUG_KMS("could not create framebuffer\n");
2903 mutex_lock(&file_priv->fbs_lock);
2904 r->fb_id = fb->base.id;
2905 list_add(&fb->filp_head, &file_priv->fbs);
2906 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2907 mutex_unlock(&file_priv->fbs_lock);
2914 * drm_mode_rmfb - remove an FB from the configuration
2915 * @dev: drm device for the ioctl
2916 * @data: data pointer for the ioctl
2917 * @file_priv: drm file for the ioctl call
2919 * Remove the FB specified by the user.
2921 * Called by the user via ioctl.
2924 * Zero on success, errno on failure.
2926 int drm_mode_rmfb(struct drm_device *dev,
2927 void *data, struct drm_file *file_priv)
2929 struct drm_framebuffer *fb = NULL;
2930 struct drm_framebuffer *fbl = NULL;
2931 uint32_t *id = data;
2934 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2937 mutex_lock(&file_priv->fbs_lock);
2938 mutex_lock(&dev->mode_config.fb_lock);
2939 fb = __drm_framebuffer_lookup(dev, *id);
2943 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2949 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2950 __drm_framebuffer_unregister(dev, fb);
2952 list_del_init(&fb->filp_head);
2953 mutex_unlock(&dev->mode_config.fb_lock);
2954 mutex_unlock(&file_priv->fbs_lock);
2956 drm_framebuffer_remove(fb);
2961 mutex_unlock(&dev->mode_config.fb_lock);
2962 mutex_unlock(&file_priv->fbs_lock);
2968 * drm_mode_getfb - get FB info
2969 * @dev: drm device for the ioctl
2970 * @data: data pointer for the ioctl
2971 * @file_priv: drm file for the ioctl call
2973 * Lookup the FB given its ID and return info about it.
2975 * Called by the user via ioctl.
2978 * Zero on success, errno on failure.
2980 int drm_mode_getfb(struct drm_device *dev,
2981 void *data, struct drm_file *file_priv)
2983 struct drm_mode_fb_cmd *r = data;
2984 struct drm_framebuffer *fb;
2987 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2990 fb = drm_framebuffer_lookup(dev, r->fb_id);
2994 r->height = fb->height;
2995 r->width = fb->width;
2996 r->depth = fb->depth;
2997 r->bpp = fb->bits_per_pixel;
2998 r->pitch = fb->pitches[0];
2999 if (fb->funcs->create_handle) {
3000 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
3005 drm_framebuffer_unreference(fb);
3011 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3012 * @dev: drm device for the ioctl
3013 * @data: data pointer for the ioctl
3014 * @file_priv: drm file for the ioctl call
3016 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3017 * rectangle list. Generic userspace which does frontbuffer rendering must call
3018 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3019 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3021 * Modesetting drivers which always update the frontbuffer do not need to
3022 * implement the corresponding ->dirty framebuffer callback.
3024 * Called by the user via ioctl.
3027 * Zero on success, errno on failure.
3029 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3030 void *data, struct drm_file *file_priv)
3032 struct drm_clip_rect __user *clips_ptr;
3033 struct drm_clip_rect *clips = NULL;
3034 struct drm_mode_fb_dirty_cmd *r = data;
3035 struct drm_framebuffer *fb;
3040 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3043 fb = drm_framebuffer_lookup(dev, r->fb_id);
3047 num_clips = r->num_clips;
3048 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3050 if (!num_clips != !clips_ptr) {
3055 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3057 /* If userspace annotates copy, clips must come in pairs */
3058 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3063 if (num_clips && clips_ptr) {
3064 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3068 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3074 ret = copy_from_user(clips, clips_ptr,
3075 num_clips * sizeof(*clips));
3082 if (fb->funcs->dirty) {
3083 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3092 drm_framebuffer_unreference(fb);
3099 * drm_fb_release - remove and free the FBs on this file
3100 * @priv: drm file for the ioctl
3102 * Destroy all the FBs associated with @filp.
3104 * Called by the user via ioctl.
3107 * Zero on success, errno on failure.
3109 void drm_fb_release(struct drm_file *priv)
3111 struct drm_device *dev = priv->dev;
3112 struct drm_framebuffer *fb, *tfb;
3114 mutex_lock(&priv->fbs_lock);
3115 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3117 mutex_lock(&dev->mode_config.fb_lock);
3118 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3119 __drm_framebuffer_unregister(dev, fb);
3120 mutex_unlock(&dev->mode_config.fb_lock);
3122 list_del_init(&fb->filp_head);
3124 /* This will also drop the fpriv->fbs reference. */
3125 drm_framebuffer_remove(fb);
3127 mutex_unlock(&priv->fbs_lock);
3131 * drm_property_create - create a new property type
3133 * @flags: flags specifying the property type
3134 * @name: name of the property
3135 * @num_values: number of pre-defined values
3137 * This creates a new generic drm property which can then be attached to a drm
3138 * object with drm_object_attach_property. The returned property object must be
3139 * freed with drm_property_destroy.
3142 * A pointer to the newly created property on success, NULL on failure.
3144 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3145 const char *name, int num_values)
3147 struct drm_property *property = NULL;
3150 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3154 property->dev = dev;
3157 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3158 if (!property->values)
3162 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3166 property->flags = flags;
3167 property->num_values = num_values;
3168 INIT_LIST_HEAD(&property->enum_blob_list);
3171 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3172 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3175 list_add_tail(&property->head, &dev->mode_config.property_list);
3177 WARN_ON(!drm_property_type_valid(property));
3181 kfree(property->values);
3185 EXPORT_SYMBOL(drm_property_create);
3188 * drm_property_create - create a new enumeration property type
3190 * @flags: flags specifying the property type
3191 * @name: name of the property
3192 * @props: enumeration lists with property values
3193 * @num_values: number of pre-defined values
3195 * This creates a new generic drm property which can then be attached to a drm
3196 * object with drm_object_attach_property. The returned property object must be
3197 * freed with drm_property_destroy.
3199 * Userspace is only allowed to set one of the predefined values for enumeration
3203 * A pointer to the newly created property on success, NULL on failure.
3205 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3207 const struct drm_prop_enum_list *props,
3210 struct drm_property *property;
3213 flags |= DRM_MODE_PROP_ENUM;
3215 property = drm_property_create(dev, flags, name, num_values);
3219 for (i = 0; i < num_values; i++) {
3220 ret = drm_property_add_enum(property, i,
3224 drm_property_destroy(dev, property);
3231 EXPORT_SYMBOL(drm_property_create_enum);
3234 * drm_property_create - create a new bitmask property type
3236 * @flags: flags specifying the property type
3237 * @name: name of the property
3238 * @props: enumeration lists with property bitflags
3239 * @num_values: number of pre-defined values
3241 * This creates a new generic drm property which can then be attached to a drm
3242 * object with drm_object_attach_property. The returned property object must be
3243 * freed with drm_property_destroy.
3245 * Compared to plain enumeration properties userspace is allowed to set any
3246 * or'ed together combination of the predefined property bitflag values
3249 * A pointer to the newly created property on success, NULL on failure.
3251 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3252 int flags, const char *name,
3253 const struct drm_prop_enum_list *props,
3256 struct drm_property *property;
3259 flags |= DRM_MODE_PROP_BITMASK;
3261 property = drm_property_create(dev, flags, name, num_values);
3265 for (i = 0; i < num_values; i++) {
3266 ret = drm_property_add_enum(property, i,
3270 drm_property_destroy(dev, property);
3277 EXPORT_SYMBOL(drm_property_create_bitmask);
3279 static struct drm_property *property_create_range(struct drm_device *dev,
3280 int flags, const char *name,
3281 uint64_t min, uint64_t max)
3283 struct drm_property *property;
3285 property = drm_property_create(dev, flags, name, 2);
3289 property->values[0] = min;
3290 property->values[1] = max;
3296 * drm_property_create - create a new ranged property type
3298 * @flags: flags specifying the property type
3299 * @name: name of the property
3300 * @min: minimum value of the property
3301 * @max: maximum value of the property
3303 * This creates a new generic drm property which can then be attached to a drm
3304 * object with drm_object_attach_property. The returned property object must be
3305 * freed with drm_property_destroy.
3307 * Userspace is allowed to set any interger value in the (min, max) range
3311 * A pointer to the newly created property on success, NULL on failure.
3313 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3315 uint64_t min, uint64_t max)
3317 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3320 EXPORT_SYMBOL(drm_property_create_range);
3322 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3323 int flags, const char *name,
3324 int64_t min, int64_t max)
3326 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3327 name, I642U64(min), I642U64(max));
3329 EXPORT_SYMBOL(drm_property_create_signed_range);
3331 struct drm_property *drm_property_create_object(struct drm_device *dev,
3332 int flags, const char *name, uint32_t type)
3334 struct drm_property *property;
3336 flags |= DRM_MODE_PROP_OBJECT;
3338 property = drm_property_create(dev, flags, name, 1);
3342 property->values[0] = type;
3346 EXPORT_SYMBOL(drm_property_create_object);
3349 * drm_property_add_enum - add a possible value to an enumeration property
3350 * @property: enumeration property to change
3351 * @index: index of the new enumeration
3352 * @value: value of the new enumeration
3353 * @name: symbolic name of the new enumeration
3355 * This functions adds enumerations to a property.
3357 * It's use is deprecated, drivers should use one of the more specific helpers
3358 * to directly create the property with all enumerations already attached.
3361 * Zero on success, error code on failure.
3363 int drm_property_add_enum(struct drm_property *property, int index,
3364 uint64_t value, const char *name)
3366 struct drm_property_enum *prop_enum;
3368 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3369 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3373 * Bitmask enum properties have the additional constraint of values
3376 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3380 if (!list_empty(&property->enum_blob_list)) {
3381 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3382 if (prop_enum->value == value) {
3383 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3384 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3390 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3394 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3395 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3396 prop_enum->value = value;
3398 property->values[index] = value;
3399 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3402 EXPORT_SYMBOL(drm_property_add_enum);
3405 * drm_property_destroy - destroy a drm property
3407 * @property: property to destry
3409 * This function frees a property including any attached resources like
3410 * enumeration values.
3412 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3414 struct drm_property_enum *prop_enum, *pt;
3416 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3417 list_del(&prop_enum->head);
3421 if (property->num_values)
3422 kfree(property->values);
3423 drm_mode_object_put(dev, &property->base);
3424 list_del(&property->head);
3427 EXPORT_SYMBOL(drm_property_destroy);
3430 * drm_object_attach_property - attach a property to a modeset object
3431 * @obj: drm modeset object
3432 * @property: property to attach
3433 * @init_val: initial value of the property
3435 * This attaches the given property to the modeset object with the given initial
3436 * value. Currently this function cannot fail since the properties are stored in
3437 * a statically sized array.
3439 void drm_object_attach_property(struct drm_mode_object *obj,
3440 struct drm_property *property,
3443 int count = obj->properties->count;
3445 if (count == DRM_OBJECT_MAX_PROPERTY) {
3446 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3447 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3448 "you see this message on the same object type.\n",
3453 obj->properties->ids[count] = property->base.id;
3454 obj->properties->values[count] = init_val;
3455 obj->properties->count++;
3457 EXPORT_SYMBOL(drm_object_attach_property);
3460 * drm_object_property_set_value - set the value of a property
3461 * @obj: drm mode object to set property value for
3462 * @property: property to set
3463 * @val: value the property should be set to
3465 * This functions sets a given property on a given object. This function only
3466 * changes the software state of the property, it does not call into the
3467 * driver's ->set_property callback.
3470 * Zero on success, error code on failure.
3472 int drm_object_property_set_value(struct drm_mode_object *obj,
3473 struct drm_property *property, uint64_t val)
3477 for (i = 0; i < obj->properties->count; i++) {
3478 if (obj->properties->ids[i] == property->base.id) {
3479 obj->properties->values[i] = val;
3486 EXPORT_SYMBOL(drm_object_property_set_value);
3489 * drm_object_property_get_value - retrieve the value of a property
3490 * @obj: drm mode object to get property value from
3491 * @property: property to retrieve
3492 * @val: storage for the property value
3494 * This function retrieves the softare state of the given property for the given
3495 * property. Since there is no driver callback to retrieve the current property
3496 * value this might be out of sync with the hardware, depending upon the driver
3500 * Zero on success, error code on failure.
3502 int drm_object_property_get_value(struct drm_mode_object *obj,
3503 struct drm_property *property, uint64_t *val)
3507 for (i = 0; i < obj->properties->count; i++) {
3508 if (obj->properties->ids[i] == property->base.id) {
3509 *val = obj->properties->values[i];
3516 EXPORT_SYMBOL(drm_object_property_get_value);
3519 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3522 * @file_priv: DRM file info
3524 * This function retrieves the current value for an connectors's property.
3526 * Called by the user via ioctl.
3529 * Zero on success, errno on failure.
3531 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3532 void *data, struct drm_file *file_priv)
3534 struct drm_mode_get_property *out_resp = data;
3535 struct drm_property *property;
3538 int value_count = 0;
3541 struct drm_property_enum *prop_enum;
3542 struct drm_mode_property_enum __user *enum_ptr;
3543 struct drm_property_blob *prop_blob;
3544 uint32_t __user *blob_id_ptr;
3545 uint64_t __user *values_ptr;
3546 uint32_t __user *blob_length_ptr;
3548 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3551 drm_modeset_lock_all(dev);
3552 property = drm_property_find(dev, out_resp->prop_id);
3558 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3559 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3560 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3562 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3563 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3567 value_count = property->num_values;
3569 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3570 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3571 out_resp->flags = property->flags;
3573 if ((out_resp->count_values >= value_count) && value_count) {
3574 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3575 for (i = 0; i < value_count; i++) {
3576 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3582 out_resp->count_values = value_count;
3584 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3585 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3586 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3588 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3589 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3591 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3596 if (copy_to_user(&enum_ptr[copied].name,
3597 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3604 out_resp->count_enum_blobs = enum_count;
3607 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3608 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3610 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3611 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3613 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3614 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3619 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3627 out_resp->count_enum_blobs = blob_count;
3630 drm_modeset_unlock_all(dev);
3634 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3637 struct drm_property_blob *blob;
3640 if (!length || !data)
3643 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3647 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3653 blob->length = length;
3655 memcpy(blob->data, data, length);
3657 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3661 static void drm_property_destroy_blob(struct drm_device *dev,
3662 struct drm_property_blob *blob)
3664 drm_mode_object_put(dev, &blob->base);
3665 list_del(&blob->head);
3670 * drm_mode_getblob_ioctl - get the contents of a blob property value
3673 * @file_priv: DRM file info
3675 * This function retrieves the contents of a blob property. The value stored in
3676 * an object's blob property is just a normal modeset object id.
3678 * Called by the user via ioctl.
3681 * Zero on success, errno on failure.
3683 int drm_mode_getblob_ioctl(struct drm_device *dev,
3684 void *data, struct drm_file *file_priv)
3686 struct drm_mode_get_blob *out_resp = data;
3687 struct drm_property_blob *blob;
3689 void __user *blob_ptr;
3691 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3694 drm_modeset_lock_all(dev);
3695 blob = drm_property_blob_find(dev, out_resp->blob_id);
3701 if (out_resp->length == blob->length) {
3702 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3703 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3708 out_resp->length = blob->length;
3711 drm_modeset_unlock_all(dev);
3716 * drm_mode_connector_update_edid_property - update the edid property of a connector
3717 * @connector: drm connector
3718 * @edid: new value of the edid property
3720 * This function creates a new blob modeset object and assigns its id to the
3721 * connector's edid property.
3724 * Zero on success, errno on failure.
3726 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3729 struct drm_device *dev = connector->dev;
3732 if (connector->edid_blob_ptr)
3733 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3735 /* Delete edid, when there is none. */
3737 connector->edid_blob_ptr = NULL;
3738 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3742 size = EDID_LENGTH * (1 + edid->extensions);
3743 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3745 if (!connector->edid_blob_ptr)
3748 ret = drm_object_property_set_value(&connector->base,
3749 dev->mode_config.edid_property,
3750 connector->edid_blob_ptr->base.id);
3754 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3756 static bool drm_property_change_is_valid(struct drm_property *property,
3759 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3762 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3763 if (value < property->values[0] || value > property->values[1])
3766 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3767 int64_t svalue = U642I64(value);
3768 if (svalue < U642I64(property->values[0]) ||
3769 svalue > U642I64(property->values[1]))
3772 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3774 uint64_t valid_mask = 0;
3775 for (i = 0; i < property->num_values; i++)
3776 valid_mask |= (1ULL << property->values[i]);
3777 return !(value & ~valid_mask);
3778 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3779 /* Only the driver knows */
3781 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3782 struct drm_mode_object *obj;
3783 /* a zero value for an object property translates to null: */
3787 * NOTE: use _object_find() directly to bypass restriction on
3788 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3789 * object this could race against object finalization, so it
3790 * simply tells us that the object *was* valid. Which is good
3793 obj = _object_find(property->dev, value, property->values[0]);
3797 for (i = 0; i < property->num_values; i++)
3798 if (property->values[i] == value)
3805 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3808 * @file_priv: DRM file info
3810 * This function sets the current value for a connectors's property. It also
3811 * calls into a driver's ->set_property callback to update the hardware state
3813 * Called by the user via ioctl.
3816 * Zero on success, errno on failure.
3818 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3819 void *data, struct drm_file *file_priv)
3821 struct drm_mode_connector_set_property *conn_set_prop = data;
3822 struct drm_mode_obj_set_property obj_set_prop = {
3823 .value = conn_set_prop->value,
3824 .prop_id = conn_set_prop->prop_id,
3825 .obj_id = conn_set_prop->connector_id,
3826 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3829 /* It does all the locking and checking we need */
3830 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3833 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3834 struct drm_property *property,
3838 struct drm_connector *connector = obj_to_connector(obj);
3840 /* Do DPMS ourselves */
3841 if (property == connector->dev->mode_config.dpms_property) {
3842 if (connector->funcs->dpms)
3843 (*connector->funcs->dpms)(connector, (int)value);
3845 } else if (connector->funcs->set_property)
3846 ret = connector->funcs->set_property(connector, property, value);
3848 /* store the property value if successful */
3850 drm_object_property_set_value(&connector->base, property, value);
3854 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3855 struct drm_property *property,
3859 struct drm_crtc *crtc = obj_to_crtc(obj);
3861 if (crtc->funcs->set_property)
3862 ret = crtc->funcs->set_property(crtc, property, value);
3864 drm_object_property_set_value(obj, property, value);
3869 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3870 struct drm_property *property,
3874 struct drm_plane *plane = obj_to_plane(obj);
3876 if (plane->funcs->set_property)
3877 ret = plane->funcs->set_property(plane, property, value);
3879 drm_object_property_set_value(obj, property, value);
3885 * drm_mode_getproperty_ioctl - get the current value of a object's property
3888 * @file_priv: DRM file info
3890 * This function retrieves the current value for an object's property. Compared
3891 * to the connector specific ioctl this one is extended to also work on crtc and
3894 * Called by the user via ioctl.
3897 * Zero on success, errno on failure.
3899 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3900 struct drm_file *file_priv)
3902 struct drm_mode_obj_get_properties *arg = data;
3903 struct drm_mode_object *obj;
3907 int props_count = 0;
3908 uint32_t __user *props_ptr;
3909 uint64_t __user *prop_values_ptr;
3911 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3914 drm_modeset_lock_all(dev);
3916 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3921 if (!obj->properties) {
3926 props_count = obj->properties->count;
3928 /* This ioctl is called twice, once to determine how much space is
3929 * needed, and the 2nd time to fill it. */
3930 if ((arg->count_props >= props_count) && props_count) {
3932 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3933 prop_values_ptr = (uint64_t __user *)(unsigned long)
3934 (arg->prop_values_ptr);
3935 for (i = 0; i < props_count; i++) {
3936 if (put_user(obj->properties->ids[i],
3937 props_ptr + copied)) {
3941 if (put_user(obj->properties->values[i],
3942 prop_values_ptr + copied)) {
3949 arg->count_props = props_count;
3951 drm_modeset_unlock_all(dev);
3956 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3959 * @file_priv: DRM file info
3961 * This function sets the current value for an object's property. It also calls
3962 * into a driver's ->set_property callback to update the hardware state.
3963 * Compared to the connector specific ioctl this one is extended to also work on
3964 * crtc and plane objects.
3966 * Called by the user via ioctl.
3969 * Zero on success, errno on failure.
3971 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3972 struct drm_file *file_priv)
3974 struct drm_mode_obj_set_property *arg = data;
3975 struct drm_mode_object *arg_obj;
3976 struct drm_mode_object *prop_obj;
3977 struct drm_property *property;
3981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3984 drm_modeset_lock_all(dev);
3986 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3991 if (!arg_obj->properties)
3994 for (i = 0; i < arg_obj->properties->count; i++)
3995 if (arg_obj->properties->ids[i] == arg->prop_id)
3998 if (i == arg_obj->properties->count)
4001 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4002 DRM_MODE_OBJECT_PROPERTY);
4007 property = obj_to_property(prop_obj);
4009 if (!drm_property_change_is_valid(property, arg->value))
4012 switch (arg_obj->type) {
4013 case DRM_MODE_OBJECT_CONNECTOR:
4014 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4017 case DRM_MODE_OBJECT_CRTC:
4018 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4020 case DRM_MODE_OBJECT_PLANE:
4021 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4026 drm_modeset_unlock_all(dev);
4031 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4032 * @connector: connector to attach
4033 * @encoder: encoder to attach @connector to
4035 * This function links up a connector to an encoder. Note that the routing
4036 * restrictions between encoders and crtcs are exposed to userspace through the
4037 * possible_clones and possible_crtcs bitmasks.
4040 * Zero on success, errno on failure.
4042 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4043 struct drm_encoder *encoder)
4047 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4048 if (connector->encoder_ids[i] == 0) {
4049 connector->encoder_ids[i] = encoder->base.id;
4055 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4058 * drm_mode_crtc_set_gamma_size - set the gamma table size
4059 * @crtc: CRTC to set the gamma table size for
4060 * @gamma_size: size of the gamma table
4062 * Drivers which support gamma tables should set this to the supported gamma
4063 * table size when initializing the CRTC. Currently the drm core only supports a
4064 * fixed gamma table size.
4067 * Zero on success, errno on failure.
4069 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4072 crtc->gamma_size = gamma_size;
4074 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4075 if (!crtc->gamma_store) {
4076 crtc->gamma_size = 0;
4082 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4085 * drm_mode_gamma_set_ioctl - set the gamma table
4088 * @file_priv: DRM file info
4090 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4091 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4093 * Called by the user via ioctl.
4096 * Zero on success, errno on failure.
4098 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4099 void *data, struct drm_file *file_priv)
4101 struct drm_mode_crtc_lut *crtc_lut = data;
4102 struct drm_crtc *crtc;
4103 void *r_base, *g_base, *b_base;
4107 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4110 drm_modeset_lock_all(dev);
4111 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4117 if (crtc->funcs->gamma_set == NULL) {
4122 /* memcpy into gamma store */
4123 if (crtc_lut->gamma_size != crtc->gamma_size) {
4128 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4129 r_base = crtc->gamma_store;
4130 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4135 g_base = (char *)r_base + size;
4136 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4141 b_base = (char *)g_base + size;
4142 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4147 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4150 drm_modeset_unlock_all(dev);
4156 * drm_mode_gamma_get_ioctl - get the gamma table
4159 * @file_priv: DRM file info
4161 * Copy the current gamma table into the storage provided. This also provides
4162 * the gamma table size the driver expects, which can be used to size the
4163 * allocated storage.
4165 * Called by the user via ioctl.
4168 * Zero on success, errno on failure.
4170 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4171 void *data, struct drm_file *file_priv)
4173 struct drm_mode_crtc_lut *crtc_lut = data;
4174 struct drm_crtc *crtc;
4175 void *r_base, *g_base, *b_base;
4179 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4182 drm_modeset_lock_all(dev);
4183 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4189 /* memcpy into gamma store */
4190 if (crtc_lut->gamma_size != crtc->gamma_size) {
4195 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4196 r_base = crtc->gamma_store;
4197 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4202 g_base = (char *)r_base + size;
4203 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4208 b_base = (char *)g_base + size;
4209 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4214 drm_modeset_unlock_all(dev);
4219 * The Linux version of kfree() is a macro and can't be called
4220 * directly via a function pointer
4222 static void drm_kms_free(void *arg)
4228 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4231 * @file_priv: DRM file info
4233 * This schedules an asynchronous update on a given CRTC, called page flip.
4234 * Optionally a drm event is generated to signal the completion of the event.
4235 * Generic drivers cannot assume that a pageflip with changed framebuffer
4236 * properties (including driver specific metadata like tiling layout) will work,
4237 * but some drivers support e.g. pixel format changes through the pageflip
4240 * Called by the user via ioctl.
4243 * Zero on success, errno on failure.
4245 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4246 void *data, struct drm_file *file_priv)
4248 struct drm_mode_crtc_page_flip *page_flip = data;
4249 struct drm_crtc *crtc;
4250 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4251 struct drm_pending_vblank_event *e = NULL;
4254 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4255 page_flip->reserved != 0)
4258 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4261 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4265 drm_modeset_lock(&crtc->mutex, NULL);
4266 if (crtc->primary->fb == NULL) {
4267 /* The framebuffer is currently unbound, presumably
4268 * due to a hotplug event, that userspace has not
4275 if (crtc->funcs->page_flip == NULL)
4278 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4284 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4288 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4289 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4294 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4296 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4297 if (file_priv->event_space < sizeof e->event) {
4298 lockmgr(&dev->event_lock, LK_RELEASE);
4301 file_priv->event_space -= sizeof e->event;
4302 lockmgr(&dev->event_lock, LK_RELEASE);
4304 e = kzalloc(sizeof *e, GFP_KERNEL);
4306 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4307 file_priv->event_space += sizeof e->event;
4308 lockmgr(&dev->event_lock, LK_RELEASE);
4312 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4313 e->event.base.length = sizeof e->event;
4314 e->event.user_data = page_flip->user_data;
4315 e->base.event = &e->event.base;
4316 e->base.file_priv = file_priv;
4317 /* XXX: DragonFly-specific */
4319 (void (*) (struct drm_pending_event *)) drm_kms_free;
4322 old_fb = crtc->primary->fb;
4323 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4325 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4326 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
4327 file_priv->event_space += sizeof e->event;
4328 lockmgr(&dev->event_lock, LK_RELEASE);
4331 /* Keep the old fb, don't unref it. */
4335 * Warn if the driver hasn't properly updated the crtc->fb
4336 * field to reflect that the new framebuffer is now used.
4337 * Failing to do so will screw with the reference counting
4340 WARN_ON(crtc->primary->fb != fb);
4341 /* Unref only the old framebuffer. */
4347 drm_framebuffer_unreference(fb);
4349 drm_framebuffer_unreference(old_fb);
4350 drm_modeset_unlock(&crtc->mutex);
4356 * drm_mode_config_reset - call ->reset callbacks
4359 * This functions calls all the crtc's, encoder's and connector's ->reset
4360 * callback. Drivers can use this in e.g. their driver load or resume code to
4361 * reset hardware and software state.
4363 void drm_mode_config_reset(struct drm_device *dev)
4365 struct drm_crtc *crtc;
4366 struct drm_encoder *encoder;
4367 struct drm_connector *connector;
4369 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4370 if (crtc->funcs->reset)
4371 crtc->funcs->reset(crtc);
4373 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4374 if (encoder->funcs->reset)
4375 encoder->funcs->reset(encoder);
4377 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4378 connector->status = connector_status_unknown;
4380 if (connector->funcs->reset)
4381 connector->funcs->reset(connector);
4384 EXPORT_SYMBOL(drm_mode_config_reset);
4387 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4390 * @file_priv: DRM file info
4392 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4393 * TTM or something else entirely) and returns the resulting buffer handle. This
4394 * handle can then be wrapped up into a framebuffer modeset object.
4396 * Note that userspace is not allowed to use such objects for render
4397 * acceleration - drivers must create their own private ioctls for such a use
4400 * Called by the user via ioctl.
4403 * Zero on success, errno on failure.
4405 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4406 void *data, struct drm_file *file_priv)
4408 struct drm_mode_create_dumb *args = data;
4409 u32 cpp, stride, size;
4411 if (!dev->driver->dumb_create)
4413 if (!args->width || !args->height || !args->bpp)
4416 /* overflow checks for 32bit size calculations */
4417 cpp = DIV_ROUND_UP(args->bpp, 8);
4418 if (cpp > 0xffffffffU / args->width)
4420 stride = cpp * args->width;
4421 if (args->height > 0xffffffffU / stride)
4424 /* test for wrap-around */
4425 size = args->height * stride;
4426 if (PAGE_ALIGN(size) == 0)
4429 return dev->driver->dumb_create(file_priv, dev, args);
4433 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4436 * @file_priv: DRM file info
4438 * Allocate an offset in the drm device node's address space to be able to
4439 * memory map a dumb buffer.
4441 * Called by the user via ioctl.
4444 * Zero on success, errno on failure.
4446 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4447 void *data, struct drm_file *file_priv)
4449 struct drm_mode_map_dumb *args = data;
4451 /* call driver ioctl to get mmap offset */
4452 if (!dev->driver->dumb_map_offset)
4455 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4459 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4462 * @file_priv: DRM file info
4464 * This destroys the userspace handle for the given dumb backing storage buffer.
4465 * Since buffer objects must be reference counted in the kernel a buffer object
4466 * won't be immediately freed if a framebuffer modeset object still uses it.
4468 * Called by the user via ioctl.
4471 * Zero on success, errno on failure.
4473 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4474 void *data, struct drm_file *file_priv)
4476 struct drm_mode_destroy_dumb *args = data;
4478 if (!dev->driver->dumb_destroy)
4481 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4485 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4486 * @format: pixel format (DRM_FORMAT_*)
4487 * @depth: storage for the depth value
4488 * @bpp: storage for the bpp value
4490 * This only supports RGB formats here for compat with code that doesn't use
4491 * pixel formats directly yet.
4493 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4498 case DRM_FORMAT_RGB332:
4499 case DRM_FORMAT_BGR233:
4503 case DRM_FORMAT_XRGB1555:
4504 case DRM_FORMAT_XBGR1555:
4505 case DRM_FORMAT_RGBX5551:
4506 case DRM_FORMAT_BGRX5551:
4507 case DRM_FORMAT_ARGB1555:
4508 case DRM_FORMAT_ABGR1555:
4509 case DRM_FORMAT_RGBA5551:
4510 case DRM_FORMAT_BGRA5551:
4514 case DRM_FORMAT_RGB565:
4515 case DRM_FORMAT_BGR565:
4519 case DRM_FORMAT_RGB888:
4520 case DRM_FORMAT_BGR888:
4524 case DRM_FORMAT_XRGB8888:
4525 case DRM_FORMAT_XBGR8888:
4526 case DRM_FORMAT_RGBX8888:
4527 case DRM_FORMAT_BGRX8888:
4531 case DRM_FORMAT_XRGB2101010:
4532 case DRM_FORMAT_XBGR2101010:
4533 case DRM_FORMAT_RGBX1010102:
4534 case DRM_FORMAT_BGRX1010102:
4535 case DRM_FORMAT_ARGB2101010:
4536 case DRM_FORMAT_ABGR2101010:
4537 case DRM_FORMAT_RGBA1010102:
4538 case DRM_FORMAT_BGRA1010102:
4542 case DRM_FORMAT_ARGB8888:
4543 case DRM_FORMAT_ABGR8888:
4544 case DRM_FORMAT_RGBA8888:
4545 case DRM_FORMAT_BGRA8888:
4550 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4551 drm_get_format_name(format));
4557 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4560 * drm_format_num_planes - get the number of planes for format
4561 * @format: pixel format (DRM_FORMAT_*)
4564 * The number of planes used by the specified pixel format.
4566 int drm_format_num_planes(uint32_t format)
4569 case DRM_FORMAT_YUV410:
4570 case DRM_FORMAT_YVU410:
4571 case DRM_FORMAT_YUV411:
4572 case DRM_FORMAT_YVU411:
4573 case DRM_FORMAT_YUV420:
4574 case DRM_FORMAT_YVU420:
4575 case DRM_FORMAT_YUV422:
4576 case DRM_FORMAT_YVU422:
4577 case DRM_FORMAT_YUV444:
4578 case DRM_FORMAT_YVU444:
4580 case DRM_FORMAT_NV12:
4581 case DRM_FORMAT_NV21:
4582 case DRM_FORMAT_NV16:
4583 case DRM_FORMAT_NV61:
4584 case DRM_FORMAT_NV24:
4585 case DRM_FORMAT_NV42:
4591 EXPORT_SYMBOL(drm_format_num_planes);
4594 * drm_format_plane_cpp - determine the bytes per pixel value
4595 * @format: pixel format (DRM_FORMAT_*)
4596 * @plane: plane index
4599 * The bytes per pixel value for the specified plane.
4601 int drm_format_plane_cpp(uint32_t format, int plane)
4606 if (plane >= drm_format_num_planes(format))
4610 case DRM_FORMAT_YUYV:
4611 case DRM_FORMAT_YVYU:
4612 case DRM_FORMAT_UYVY:
4613 case DRM_FORMAT_VYUY:
4615 case DRM_FORMAT_NV12:
4616 case DRM_FORMAT_NV21:
4617 case DRM_FORMAT_NV16:
4618 case DRM_FORMAT_NV61:
4619 case DRM_FORMAT_NV24:
4620 case DRM_FORMAT_NV42:
4621 return plane ? 2 : 1;
4622 case DRM_FORMAT_YUV410:
4623 case DRM_FORMAT_YVU410:
4624 case DRM_FORMAT_YUV411:
4625 case DRM_FORMAT_YVU411:
4626 case DRM_FORMAT_YUV420:
4627 case DRM_FORMAT_YVU420:
4628 case DRM_FORMAT_YUV422:
4629 case DRM_FORMAT_YVU422:
4630 case DRM_FORMAT_YUV444:
4631 case DRM_FORMAT_YVU444:
4634 drm_fb_get_bpp_depth(format, &depth, &bpp);
4638 EXPORT_SYMBOL(drm_format_plane_cpp);
4641 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4642 * @format: pixel format (DRM_FORMAT_*)
4645 * The horizontal chroma subsampling factor for the
4646 * specified pixel format.
4648 int drm_format_horz_chroma_subsampling(uint32_t format)
4651 case DRM_FORMAT_YUV411:
4652 case DRM_FORMAT_YVU411:
4653 case DRM_FORMAT_YUV410:
4654 case DRM_FORMAT_YVU410:
4656 case DRM_FORMAT_YUYV:
4657 case DRM_FORMAT_YVYU:
4658 case DRM_FORMAT_UYVY:
4659 case DRM_FORMAT_VYUY:
4660 case DRM_FORMAT_NV12:
4661 case DRM_FORMAT_NV21:
4662 case DRM_FORMAT_NV16:
4663 case DRM_FORMAT_NV61:
4664 case DRM_FORMAT_YUV422:
4665 case DRM_FORMAT_YVU422:
4666 case DRM_FORMAT_YUV420:
4667 case DRM_FORMAT_YVU420:
4673 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4676 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4677 * @format: pixel format (DRM_FORMAT_*)
4680 * The vertical chroma subsampling factor for the
4681 * specified pixel format.
4683 int drm_format_vert_chroma_subsampling(uint32_t format)
4686 case DRM_FORMAT_YUV410:
4687 case DRM_FORMAT_YVU410:
4689 case DRM_FORMAT_YUV420:
4690 case DRM_FORMAT_YVU420:
4691 case DRM_FORMAT_NV12:
4692 case DRM_FORMAT_NV21:
4698 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4701 * drm_mode_config_init - initialize DRM mode_configuration structure
4704 * Initialize @dev's mode_config structure, used for tracking the graphics
4705 * configuration of @dev.
4707 * Since this initializes the modeset locks, no locking is possible. Which is no
4708 * problem, since this should happen single threaded at init time. It is the
4709 * driver's problem to ensure this guarantee.
4712 void drm_mode_config_init(struct drm_device *dev)
4714 lockinit(&dev->mode_config.mutex, "drmmcm", 0, LK_CANRECURSE);
4715 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
4716 lockinit(&dev->mode_config.idr_mutex, "mcfgidr", 0, LK_CANRECURSE);
4717 lockinit(&dev->mode_config.fb_lock, "drmfbl", 0, LK_CANRECURSE);
4718 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4719 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4720 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4721 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4722 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4723 INIT_LIST_HEAD(&dev->mode_config.property_list);
4724 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4725 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4726 idr_init(&dev->mode_config.crtc_idr);
4728 drm_modeset_lock_all(dev);
4729 drm_mode_create_standard_connector_properties(dev);
4730 drm_mode_create_standard_plane_properties(dev);
4731 drm_modeset_unlock_all(dev);
4733 /* Just to be sure */
4734 dev->mode_config.num_fb = 0;
4735 dev->mode_config.num_connector = 0;
4736 dev->mode_config.num_crtc = 0;
4737 dev->mode_config.num_encoder = 0;
4738 dev->mode_config.num_overlay_plane = 0;
4739 dev->mode_config.num_total_plane = 0;
4741 EXPORT_SYMBOL(drm_mode_config_init);
4744 * drm_mode_config_cleanup - free up DRM mode_config info
4747 * Free up all the connectors and CRTCs associated with this DRM device, then
4748 * free up the framebuffers and associated buffer objects.
4750 * Note that since this /should/ happen single-threaded at driver/device
4751 * teardown time, no locking is required. It's the driver's job to ensure that
4752 * this guarantee actually holds true.
4754 * FIXME: cleanup any dangling user buffer objects too
4756 void drm_mode_config_cleanup(struct drm_device *dev)
4758 struct drm_connector *connector, *ot;
4759 struct drm_crtc *crtc, *ct;
4760 struct drm_encoder *encoder, *enct;
4761 struct drm_bridge *bridge, *brt;
4762 struct drm_framebuffer *fb, *fbt;
4763 struct drm_property *property, *pt;
4764 struct drm_property_blob *blob, *bt;
4765 struct drm_plane *plane, *plt;
4767 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4769 encoder->funcs->destroy(encoder);
4772 list_for_each_entry_safe(bridge, brt,
4773 &dev->mode_config.bridge_list, head) {
4774 bridge->funcs->destroy(bridge);
4777 list_for_each_entry_safe(connector, ot,
4778 &dev->mode_config.connector_list, head) {
4779 connector->funcs->destroy(connector);
4782 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4784 drm_property_destroy(dev, property);
4787 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4789 drm_property_destroy_blob(dev, blob);
4793 * Single-threaded teardown context, so it's not required to grab the
4794 * fb_lock to protect against concurrent fb_list access. Contrary, it
4795 * would actually deadlock with the drm_framebuffer_cleanup function.
4797 * Also, if there are any framebuffers left, that's a driver leak now,
4798 * so politely WARN about this.
4800 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4801 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4802 drm_framebuffer_remove(fb);
4805 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4807 plane->funcs->destroy(plane);
4810 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4811 crtc->funcs->destroy(crtc);
4814 idr_destroy(&dev->mode_config.crtc_idr);
4815 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
4817 EXPORT_SYMBOL(drm_mode_config_cleanup);