2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
44 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
45 struct drm_mode_fb_cmd2 *r,
46 struct drm_file *file_priv);
49 * drm_modeset_lock_all - take all modeset locks
52 * This function takes all modeset locks, suitable where a more fine-grained
53 * scheme isn't (yet) implemented. Locks must be dropped with
54 * drm_modeset_unlock_all.
56 void drm_modeset_lock_all(struct drm_device *dev)
58 struct drm_mode_config *config = &dev->mode_config;
59 struct drm_modeset_acquire_ctx *ctx;
62 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
66 mutex_lock(&config->mutex);
68 drm_modeset_acquire_init(ctx, 0);
71 ret = drm_modeset_lock(&config->connection_mutex, ctx);
74 ret = drm_modeset_lock_all_crtcs(dev, ctx);
78 WARN_ON(config->acquire_ctx);
80 /* now we hold the locks, so now that it is safe, stash the
81 * ctx for drm_modeset_unlock_all():
83 config->acquire_ctx = ctx;
85 drm_warn_on_modeset_not_all_locked(dev);
90 if (ret == -EDEADLK) {
91 drm_modeset_backoff(ctx);
95 EXPORT_SYMBOL(drm_modeset_lock_all);
98 * drm_modeset_unlock_all - drop all modeset locks
101 * This function drop all modeset locks taken by drm_modeset_lock_all.
103 void drm_modeset_unlock_all(struct drm_device *dev)
105 struct drm_mode_config *config = &dev->mode_config;
106 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
111 config->acquire_ctx = NULL;
112 drm_modeset_drop_locks(ctx);
113 drm_modeset_acquire_fini(ctx);
117 mutex_unlock(&dev->mode_config.mutex);
119 EXPORT_SYMBOL(drm_modeset_unlock_all);
122 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
125 * Useful as a debug assert.
127 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
129 struct drm_crtc *crtc;
131 /* Locking is currently fubar in the panic handler. */
132 if (oops_in_progress)
135 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
136 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
138 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
139 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
141 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
143 /* Avoid boilerplate. I'm tired of typing. */
144 #define DRM_ENUM_NAME_FN(fnname, list) \
145 const char *fnname(int val) \
148 for (i = 0; i < ARRAY_SIZE(list); i++) { \
149 if (list[i].type == val) \
150 return list[i].name; \
152 return "(unknown)"; \
158 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
159 { { DRM_MODE_DPMS_ON, "On" },
160 { DRM_MODE_DPMS_STANDBY, "Standby" },
161 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
162 { DRM_MODE_DPMS_OFF, "Off" }
165 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
167 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
169 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
170 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
171 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
175 * Optional properties
177 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
179 { DRM_MODE_SCALE_NONE, "None" },
180 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
181 { DRM_MODE_SCALE_CENTER, "Center" },
182 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
186 * Non-global properties, but "required" for certain connectors.
188 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
190 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
191 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
192 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
195 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
197 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
199 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
200 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
201 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
204 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
205 drm_dvi_i_subconnector_enum_list)
207 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
209 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
210 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
211 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
212 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
213 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
216 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
218 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
220 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
221 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
222 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
223 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
224 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
227 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
228 drm_tv_subconnector_enum_list)
230 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
231 { DRM_MODE_DIRTY_OFF, "Off" },
232 { DRM_MODE_DIRTY_ON, "On" },
233 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
236 struct drm_conn_prop_enum_list {
243 * Connector and encoder types.
245 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
246 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
247 { DRM_MODE_CONNECTOR_VGA, "VGA" },
248 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
249 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
250 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
251 { DRM_MODE_CONNECTOR_Composite, "Composite" },
252 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
253 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
254 { DRM_MODE_CONNECTOR_Component, "Component" },
255 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
256 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
257 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
258 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
259 { DRM_MODE_CONNECTOR_TV, "TV" },
260 { DRM_MODE_CONNECTOR_eDP, "eDP" },
261 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
262 { DRM_MODE_CONNECTOR_DSI, "DSI" },
265 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
266 { { DRM_MODE_ENCODER_NONE, "None" },
267 { DRM_MODE_ENCODER_DAC, "DAC" },
268 { DRM_MODE_ENCODER_TMDS, "TMDS" },
269 { DRM_MODE_ENCODER_LVDS, "LVDS" },
270 { DRM_MODE_ENCODER_TVDAC, "TV" },
271 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
272 { DRM_MODE_ENCODER_DSI, "DSI" },
273 { DRM_MODE_ENCODER_DPMST, "DP MST" },
276 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
278 { SubPixelUnknown, "Unknown" },
279 { SubPixelHorizontalRGB, "Horizontal RGB" },
280 { SubPixelHorizontalBGR, "Horizontal BGR" },
281 { SubPixelVerticalRGB, "Vertical RGB" },
282 { SubPixelVerticalBGR, "Vertical BGR" },
283 { SubPixelNone, "None" },
286 void drm_connector_ida_init(void)
290 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
291 ida_init(&drm_connector_enum_list[i].ida);
294 void drm_connector_ida_destroy(void)
298 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
299 ida_destroy(&drm_connector_enum_list[i].ida);
303 * drm_get_connector_status_name - return a string for connector status
304 * @status: connector status to compute name of
306 * In contrast to the other drm_get_*_name functions this one here returns a
307 * const pointer and hence is threadsafe.
309 const char *drm_get_connector_status_name(enum drm_connector_status status)
311 if (status == connector_status_connected)
313 else if (status == connector_status_disconnected)
314 return "disconnected";
318 EXPORT_SYMBOL(drm_get_connector_status_name);
321 * drm_get_subpixel_order_name - return a string for a given subpixel enum
322 * @order: enum of subpixel_order
324 * Note you could abuse this and return something out of bounds, but that
325 * would be a caller error. No unscrubbed user data should make it here.
327 const char *drm_get_subpixel_order_name(enum subpixel_order order)
329 return drm_subpixel_enum_list[order].name;
331 EXPORT_SYMBOL(drm_get_subpixel_order_name);
333 static char printable_char(int c)
335 return isascii(c) && isprint(c) ? c : '?';
339 * drm_get_format_name - return a string for drm fourcc format
340 * @format: format to compute name of
342 * Note that the buffer used by this function is globally shared and owned by
343 * the function itself.
345 * FIXME: This isn't really multithreading safe.
347 const char *drm_get_format_name(uint32_t format)
351 snprintf(buf, sizeof(buf),
352 "%c%c%c%c %s-endian (0x%08x)",
353 printable_char(format & 0xff),
354 printable_char((format >> 8) & 0xff),
355 printable_char((format >> 16) & 0xff),
356 printable_char((format >> 24) & 0x7f),
357 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
362 EXPORT_SYMBOL(drm_get_format_name);
365 * Internal function to assign a slot in the object idr and optionally
366 * register the object into the idr.
368 static int drm_mode_object_get_reg(struct drm_device *dev,
369 struct drm_mode_object *obj,
375 mutex_lock(&dev->mode_config.idr_mutex);
376 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
379 * Set up the object linking under the protection of the idr
380 * lock so that other users can't see inconsistent state.
383 obj->type = obj_type;
385 mutex_unlock(&dev->mode_config.idr_mutex);
387 return ret < 0 ? ret : 0;
391 * drm_mode_object_get - allocate a new modeset identifier
393 * @obj: object pointer, used to generate unique ID
394 * @obj_type: object type
396 * Create a unique identifier based on @ptr in @dev's identifier space. Used
397 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
398 * modeset identifiers are _not_ reference counted. Hence don't use this for
399 * reference counted modeset objects like framebuffers.
402 * New unique (relative to other objects in @dev) integer identifier for the
405 int drm_mode_object_get(struct drm_device *dev,
406 struct drm_mode_object *obj, uint32_t obj_type)
408 return drm_mode_object_get_reg(dev, obj, obj_type, true);
411 static void drm_mode_object_register(struct drm_device *dev,
412 struct drm_mode_object *obj)
414 mutex_lock(&dev->mode_config.idr_mutex);
415 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
416 mutex_unlock(&dev->mode_config.idr_mutex);
420 * drm_mode_object_put - free a modeset identifer
422 * @object: object to free
424 * Free @id from @dev's unique identifier pool. Note that despite the _get
425 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
426 * for reference counted modeset objects like framebuffers.
428 void drm_mode_object_put(struct drm_device *dev,
429 struct drm_mode_object *object)
431 mutex_lock(&dev->mode_config.idr_mutex);
432 idr_remove(&dev->mode_config.crtc_idr, object->id);
433 mutex_unlock(&dev->mode_config.idr_mutex);
436 static struct drm_mode_object *_object_find(struct drm_device *dev,
437 uint32_t id, uint32_t type)
439 struct drm_mode_object *obj = NULL;
441 mutex_lock(&dev->mode_config.idr_mutex);
442 obj = idr_find(&dev->mode_config.crtc_idr, id);
443 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
446 mutex_unlock(&dev->mode_config.idr_mutex);
452 * drm_mode_object_find - look up a drm object with static lifetime
454 * @id: id of the mode object
455 * @type: type of the mode object
457 * Note that framebuffers cannot be looked up with this functions - since those
458 * are reference counted, they need special treatment. Even with
459 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
460 * rather than WARN_ON()).
462 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
463 uint32_t id, uint32_t type)
465 struct drm_mode_object *obj = NULL;
467 /* Framebuffers are reference counted and need their own lookup
469 WARN_ON(type == DRM_MODE_OBJECT_FB);
470 obj = _object_find(dev, id, type);
471 /* don't leak out unref'd fb's */
472 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
476 EXPORT_SYMBOL(drm_mode_object_find);
479 * drm_framebuffer_init - initialize a framebuffer
481 * @fb: framebuffer to be initialized
482 * @funcs: ... with these functions
484 * Allocates an ID for the framebuffer's parent mode object, sets its mode
485 * functions & device file and adds it to the master fd list.
488 * This functions publishes the fb and makes it available for concurrent access
489 * by other users. Which means by this point the fb _must_ be fully set up -
490 * since all the fb attributes are invariant over its lifetime, no further
491 * locking but only correct reference counting is required.
494 * Zero on success, error code on failure.
496 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
497 const struct drm_framebuffer_funcs *funcs)
501 mutex_lock(&dev->mode_config.fb_lock);
502 kref_init(&fb->refcount);
503 INIT_LIST_HEAD(&fb->filp_head);
507 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
511 /* Grab the idr reference. */
512 drm_framebuffer_reference(fb);
514 dev->mode_config.num_fb++;
515 list_add(&fb->head, &dev->mode_config.fb_list);
517 mutex_unlock(&dev->mode_config.fb_lock);
521 EXPORT_SYMBOL(drm_framebuffer_init);
523 static void drm_framebuffer_free(struct kref *kref)
525 struct drm_framebuffer *fb =
526 container_of(kref, struct drm_framebuffer, refcount);
527 fb->funcs->destroy(fb);
530 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
533 struct drm_mode_object *obj = NULL;
534 struct drm_framebuffer *fb;
536 mutex_lock(&dev->mode_config.idr_mutex);
537 obj = idr_find(&dev->mode_config.crtc_idr, id);
538 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
542 mutex_unlock(&dev->mode_config.idr_mutex);
548 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
550 * @id: id of the fb object
552 * If successful, this grabs an additional reference to the framebuffer -
553 * callers need to make sure to eventually unreference the returned framebuffer
554 * again, using @drm_framebuffer_unreference.
556 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
559 struct drm_framebuffer *fb;
561 mutex_lock(&dev->mode_config.fb_lock);
562 fb = __drm_framebuffer_lookup(dev, id);
564 drm_framebuffer_reference(fb);
565 mutex_unlock(&dev->mode_config.fb_lock);
569 EXPORT_SYMBOL(drm_framebuffer_lookup);
572 * drm_framebuffer_unreference - unref a framebuffer
573 * @fb: framebuffer to unref
575 * This functions decrements the fb's refcount and frees it if it drops to zero.
577 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
579 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
580 kref_put(&fb->refcount, drm_framebuffer_free);
582 EXPORT_SYMBOL(drm_framebuffer_unreference);
585 * drm_framebuffer_reference - incr the fb refcnt
588 * This functions increments the fb's refcount.
590 void drm_framebuffer_reference(struct drm_framebuffer *fb)
592 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
593 kref_get(&fb->refcount);
595 EXPORT_SYMBOL(drm_framebuffer_reference);
597 static void drm_framebuffer_free_bug(struct kref *kref)
602 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
604 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
605 kref_put(&fb->refcount, drm_framebuffer_free_bug);
608 /* dev->mode_config.fb_lock must be held! */
609 static void __drm_framebuffer_unregister(struct drm_device *dev,
610 struct drm_framebuffer *fb)
612 mutex_lock(&dev->mode_config.idr_mutex);
613 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
614 mutex_unlock(&dev->mode_config.idr_mutex);
618 __drm_framebuffer_unreference(fb);
622 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
623 * @fb: fb to unregister
625 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
626 * those used for fbdev. Note that the caller must hold a reference of it's own,
627 * i.e. the object may not be destroyed through this call (since it'll lead to a
628 * locking inversion).
630 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
632 struct drm_device *dev = fb->dev;
634 mutex_lock(&dev->mode_config.fb_lock);
635 /* Mark fb as reaped and drop idr ref. */
636 __drm_framebuffer_unregister(dev, fb);
637 mutex_unlock(&dev->mode_config.fb_lock);
639 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
642 * drm_framebuffer_cleanup - remove a framebuffer object
643 * @fb: framebuffer to remove
645 * Cleanup framebuffer. This function is intended to be used from the drivers
646 * ->destroy callback. It can also be used to clean up driver private
647 * framebuffers embedded into a larger structure.
649 * Note that this function does not remove the fb from active usuage - if it is
650 * still used anywhere, hilarity can ensue since userspace could call getfb on
651 * the id and get back -EINVAL. Obviously no concern at driver unload time.
653 * Also, the framebuffer will not be removed from the lookup idr - for
654 * user-created framebuffers this will happen in in the rmfb ioctl. For
655 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
656 * drm_framebuffer_unregister_private.
658 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
660 struct drm_device *dev = fb->dev;
662 mutex_lock(&dev->mode_config.fb_lock);
664 dev->mode_config.num_fb--;
665 mutex_unlock(&dev->mode_config.fb_lock);
667 EXPORT_SYMBOL(drm_framebuffer_cleanup);
670 * drm_framebuffer_remove - remove and unreference a framebuffer object
671 * @fb: framebuffer to remove
673 * Scans all the CRTCs and planes in @dev's mode_config. If they're
674 * using @fb, removes it, setting it to NULL. Then drops the reference to the
675 * passed-in framebuffer. Might take the modeset locks.
677 * Note that this function optimizes the cleanup away if the caller holds the
678 * last reference to the framebuffer. It is also guaranteed to not take the
679 * modeset locks in this case.
681 void drm_framebuffer_remove(struct drm_framebuffer *fb)
683 struct drm_device *dev = fb->dev;
684 struct drm_crtc *crtc;
685 struct drm_plane *plane;
686 struct drm_mode_set set;
689 WARN_ON(!list_empty(&fb->filp_head));
692 * drm ABI mandates that we remove any deleted framebuffers from active
693 * useage. But since most sane clients only remove framebuffers they no
694 * longer need, try to optimize this away.
696 * Since we're holding a reference ourselves, observing a refcount of 1
697 * means that we're the last holder and can skip it. Also, the refcount
698 * can never increase from 1 again, so we don't need any barriers or
701 * Note that userspace could try to race with use and instate a new
702 * usage _after_ we've cleared all current ones. End result will be an
703 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
706 if (atomic_read(&fb->refcount.refcount) > 1) {
707 drm_modeset_lock_all(dev);
708 /* remove from any CRTC */
709 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
710 if (crtc->primary->fb == fb) {
711 /* should turn off the crtc */
712 memset(&set, 0, sizeof(struct drm_mode_set));
715 ret = drm_mode_set_config_internal(&set);
717 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
721 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
723 drm_plane_force_disable(plane);
725 drm_modeset_unlock_all(dev);
728 drm_framebuffer_unreference(fb);
730 EXPORT_SYMBOL(drm_framebuffer_remove);
732 DEFINE_WW_CLASS(crtc_ww_class);
735 * drm_crtc_init_with_planes - Initialise a new CRTC object with
736 * specified primary and cursor planes.
738 * @crtc: CRTC object to init
739 * @primary: Primary plane for CRTC
740 * @cursor: Cursor plane for CRTC
741 * @funcs: callbacks for the new CRTC
743 * Inits a new object created as base part of a driver crtc object.
746 * Zero on success, error code on failure.
748 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
749 struct drm_plane *primary,
750 struct drm_plane *cursor,
751 const struct drm_crtc_funcs *funcs)
753 struct drm_mode_config *config = &dev->mode_config;
758 crtc->invert_dimensions = false;
760 drm_modeset_lock_all(dev);
761 drm_modeset_lock_init(&crtc->mutex);
762 /* dropped by _unlock_all(): */
763 drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
765 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
769 crtc->base.properties = &crtc->properties;
771 list_add_tail(&crtc->head, &config->crtc_list);
774 crtc->primary = primary;
775 crtc->cursor = cursor;
777 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
779 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
782 drm_modeset_unlock_all(dev);
786 EXPORT_SYMBOL(drm_crtc_init_with_planes);
789 * drm_crtc_cleanup - Clean up the core crtc usage
790 * @crtc: CRTC to cleanup
792 * This function cleans up @crtc and removes it from the DRM mode setting
793 * core. Note that the function does *not* free the crtc structure itself,
794 * this is the responsibility of the caller.
796 void drm_crtc_cleanup(struct drm_crtc *crtc)
798 struct drm_device *dev = crtc->dev;
800 kfree(crtc->gamma_store);
801 crtc->gamma_store = NULL;
803 drm_modeset_lock_fini(&crtc->mutex);
805 drm_mode_object_put(dev, &crtc->base);
806 list_del(&crtc->head);
807 dev->mode_config.num_crtc--;
809 EXPORT_SYMBOL(drm_crtc_cleanup);
812 * drm_crtc_index - find the index of a registered CRTC
813 * @crtc: CRTC to find index for
815 * Given a registered CRTC, return the index of that CRTC within a DRM
816 * device's list of CRTCs.
818 unsigned int drm_crtc_index(struct drm_crtc *crtc)
820 unsigned int index = 0;
821 struct drm_crtc *tmp;
823 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
832 EXPORT_SYMBOL(drm_crtc_index);
835 * drm_mode_remove - remove and free a mode
836 * @connector: connector list to modify
837 * @mode: mode to remove
839 * Remove @mode from @connector's mode list, then free it.
841 static void drm_mode_remove(struct drm_connector *connector,
842 struct drm_display_mode *mode)
844 list_del(&mode->head);
845 drm_mode_destroy(connector->dev, mode);
849 * drm_connector_init - Init a preallocated connector
851 * @connector: the connector to init
852 * @funcs: callbacks for this connector
853 * @connector_type: user visible type of the connector
855 * Initialises a preallocated connector. Connectors should be
856 * subclassed as part of driver connector objects.
859 * Zero on success, error code on failure.
861 int drm_connector_init(struct drm_device *dev,
862 struct drm_connector *connector,
863 const struct drm_connector_funcs *funcs,
867 struct ida *connector_ida =
868 &drm_connector_enum_list[connector_type].ida;
870 drm_modeset_lock_all(dev);
872 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
876 connector->base.properties = &connector->properties;
877 connector->dev = dev;
878 connector->funcs = funcs;
879 connector->connector_type = connector_type;
880 connector->connector_type_id =
881 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
882 if (connector->connector_type_id < 0) {
883 ret = connector->connector_type_id;
887 kasprintf(GFP_KERNEL, "%s-%d",
888 drm_connector_enum_list[connector_type].name,
889 connector->connector_type_id);
890 if (!connector->name) {
895 INIT_LIST_HEAD(&connector->probed_modes);
896 INIT_LIST_HEAD(&connector->modes);
897 connector->edid_blob_ptr = NULL;
898 connector->status = connector_status_unknown;
900 list_add_tail(&connector->head, &dev->mode_config.connector_list);
901 dev->mode_config.num_connector++;
903 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
904 drm_object_attach_property(&connector->base,
905 dev->mode_config.edid_property,
908 drm_object_attach_property(&connector->base,
909 dev->mode_config.dpms_property, 0);
911 connector->debugfs_entry = NULL;
915 drm_mode_object_put(dev, &connector->base);
918 drm_modeset_unlock_all(dev);
922 EXPORT_SYMBOL(drm_connector_init);
925 * drm_connector_cleanup - cleans up an initialised connector
926 * @connector: connector to cleanup
928 * Cleans up the connector but doesn't free the object.
930 void drm_connector_cleanup(struct drm_connector *connector)
932 struct drm_device *dev = connector->dev;
933 struct drm_display_mode *mode, *t;
935 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
936 drm_mode_remove(connector, mode);
938 list_for_each_entry_safe(mode, t, &connector->modes, head)
939 drm_mode_remove(connector, mode);
941 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
942 connector->connector_type_id);
944 drm_mode_object_put(dev, &connector->base);
945 kfree(connector->name);
946 connector->name = NULL;
947 list_del(&connector->head);
948 dev->mode_config.num_connector--;
950 EXPORT_SYMBOL(drm_connector_cleanup);
953 * drm_connector_register - register a connector
954 * @connector: the connector to register
956 * Register userspace interfaces for a connector
959 * Zero on success, error code on failure.
961 int drm_connector_register(struct drm_connector *connector)
965 drm_mode_object_register(connector->dev, &connector->base);
967 ret = drm_sysfs_connector_add(connector);
971 ret = drm_debugfs_connector_add(connector);
973 drm_sysfs_connector_remove(connector);
979 EXPORT_SYMBOL(drm_connector_register);
982 * drm_connector_unregister - unregister a connector
983 * @connector: the connector to unregister
985 * Unregister userspace interfaces for a connector
987 void drm_connector_unregister(struct drm_connector *connector)
989 drm_sysfs_connector_remove(connector);
990 drm_debugfs_connector_remove(connector);
992 EXPORT_SYMBOL(drm_connector_unregister);
996 * drm_connector_unplug_all - unregister connector userspace interfaces
999 * This function unregisters all connector userspace interfaces in sysfs. Should
1000 * be call when the device is disconnected, e.g. from an usb driver's
1001 * ->disconnect callback.
1003 void drm_connector_unplug_all(struct drm_device *dev)
1005 struct drm_connector *connector;
1007 /* taking the mode config mutex ends up in a clash with sysfs */
1008 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1009 drm_connector_unregister(connector);
1012 EXPORT_SYMBOL(drm_connector_unplug_all);
1015 * drm_bridge_init - initialize a drm transcoder/bridge
1017 * @bridge: transcoder/bridge to set up
1018 * @funcs: bridge function table
1020 * Initialises a preallocated bridge. Bridges should be
1021 * subclassed as part of driver connector objects.
1024 * Zero on success, error code on failure.
1026 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1027 const struct drm_bridge_funcs *funcs)
1031 drm_modeset_lock_all(dev);
1033 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1038 bridge->funcs = funcs;
1040 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1041 dev->mode_config.num_bridge++;
1044 drm_modeset_unlock_all(dev);
1047 EXPORT_SYMBOL(drm_bridge_init);
1050 * drm_bridge_cleanup - cleans up an initialised bridge
1051 * @bridge: bridge to cleanup
1053 * Cleans up the bridge but doesn't free the object.
1055 void drm_bridge_cleanup(struct drm_bridge *bridge)
1057 struct drm_device *dev = bridge->dev;
1059 drm_modeset_lock_all(dev);
1060 drm_mode_object_put(dev, &bridge->base);
1061 list_del(&bridge->head);
1062 dev->mode_config.num_bridge--;
1063 drm_modeset_unlock_all(dev);
1065 EXPORT_SYMBOL(drm_bridge_cleanup);
1068 * drm_encoder_init - Init a preallocated encoder
1070 * @encoder: the encoder to init
1071 * @funcs: callbacks for this encoder
1072 * @encoder_type: user visible type of the encoder
1074 * Initialises a preallocated encoder. Encoder should be
1075 * subclassed as part of driver encoder objects.
1078 * Zero on success, error code on failure.
1080 int drm_encoder_init(struct drm_device *dev,
1081 struct drm_encoder *encoder,
1082 const struct drm_encoder_funcs *funcs,
1087 drm_modeset_lock_all(dev);
1089 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1094 encoder->encoder_type = encoder_type;
1095 encoder->funcs = funcs;
1096 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1097 drm_encoder_enum_list[encoder_type].name,
1099 if (!encoder->name) {
1104 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1105 dev->mode_config.num_encoder++;
1109 drm_mode_object_put(dev, &encoder->base);
1112 drm_modeset_unlock_all(dev);
1116 EXPORT_SYMBOL(drm_encoder_init);
1119 * drm_encoder_cleanup - cleans up an initialised encoder
1120 * @encoder: encoder to cleanup
1122 * Cleans up the encoder but doesn't free the object.
1124 void drm_encoder_cleanup(struct drm_encoder *encoder)
1126 struct drm_device *dev = encoder->dev;
1127 drm_modeset_lock_all(dev);
1128 drm_mode_object_put(dev, &encoder->base);
1129 kfree(encoder->name);
1130 encoder->name = NULL;
1131 list_del(&encoder->head);
1132 dev->mode_config.num_encoder--;
1133 drm_modeset_unlock_all(dev);
1135 EXPORT_SYMBOL(drm_encoder_cleanup);
1138 * drm_universal_plane_init - Initialize a new universal plane object
1140 * @plane: plane object to init
1141 * @possible_crtcs: bitmask of possible CRTCs
1142 * @funcs: callbacks for the new plane
1143 * @formats: array of supported formats (%DRM_FORMAT_*)
1144 * @format_count: number of elements in @formats
1145 * @type: type of plane (overlay, primary, cursor)
1147 * Initializes a plane object of type @type.
1150 * Zero on success, error code on failure.
1152 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1153 unsigned long possible_crtcs,
1154 const struct drm_plane_funcs *funcs,
1155 const uint32_t *formats, uint32_t format_count,
1156 enum drm_plane_type type)
1160 drm_modeset_lock_all(dev);
1162 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1166 plane->base.properties = &plane->properties;
1168 plane->funcs = funcs;
1169 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1171 if (!plane->format_types) {
1172 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1173 drm_mode_object_put(dev, &plane->base);
1178 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1179 plane->format_count = format_count;
1180 plane->possible_crtcs = possible_crtcs;
1183 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1184 dev->mode_config.num_total_plane++;
1185 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1186 dev->mode_config.num_overlay_plane++;
1188 drm_object_attach_property(&plane->base,
1189 dev->mode_config.plane_type_property,
1193 drm_modeset_unlock_all(dev);
1197 EXPORT_SYMBOL(drm_universal_plane_init);
1200 * drm_plane_init - Initialize a legacy plane
1202 * @plane: plane object to init
1203 * @possible_crtcs: bitmask of possible CRTCs
1204 * @funcs: callbacks for the new plane
1205 * @formats: array of supported formats (%DRM_FORMAT_*)
1206 * @format_count: number of elements in @formats
1207 * @is_primary: plane type (primary vs overlay)
1209 * Legacy API to initialize a DRM plane.
1211 * New drivers should call drm_universal_plane_init() instead.
1214 * Zero on success, error code on failure.
1216 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1217 unsigned long possible_crtcs,
1218 const struct drm_plane_funcs *funcs,
1219 const uint32_t *formats, uint32_t format_count,
1222 enum drm_plane_type type;
1224 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1225 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1226 formats, format_count, type);
1228 EXPORT_SYMBOL(drm_plane_init);
1231 * drm_plane_cleanup - Clean up the core plane usage
1232 * @plane: plane to cleanup
1234 * This function cleans up @plane and removes it from the DRM mode setting
1235 * core. Note that the function does *not* free the plane structure itself,
1236 * this is the responsibility of the caller.
1238 void drm_plane_cleanup(struct drm_plane *plane)
1240 struct drm_device *dev = plane->dev;
1242 drm_modeset_lock_all(dev);
1243 kfree(plane->format_types);
1244 drm_mode_object_put(dev, &plane->base);
1246 BUG_ON(list_empty(&plane->head));
1248 list_del(&plane->head);
1249 dev->mode_config.num_total_plane--;
1250 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1251 dev->mode_config.num_overlay_plane--;
1252 drm_modeset_unlock_all(dev);
1254 EXPORT_SYMBOL(drm_plane_cleanup);
1257 * drm_plane_force_disable - Forcibly disable a plane
1258 * @plane: plane to disable
1260 * Forces the plane to be disabled.
1262 * Used when the plane's current framebuffer is destroyed,
1263 * and when restoring fbdev mode.
1265 void drm_plane_force_disable(struct drm_plane *plane)
1267 struct drm_framebuffer *old_fb = plane->fb;
1273 ret = plane->funcs->disable_plane(plane);
1275 DRM_ERROR("failed to disable plane with busy fb\n");
1278 /* disconnect the plane from the fb and crtc: */
1279 __drm_framebuffer_unreference(old_fb);
1283 EXPORT_SYMBOL(drm_plane_force_disable);
1285 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1287 struct drm_property *edid;
1288 struct drm_property *dpms;
1289 struct drm_property *dev_path;
1292 * Standard properties (apply to all connectors)
1294 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1295 DRM_MODE_PROP_IMMUTABLE,
1297 dev->mode_config.edid_property = edid;
1299 dpms = drm_property_create_enum(dev, 0,
1300 "DPMS", drm_dpms_enum_list,
1301 ARRAY_SIZE(drm_dpms_enum_list));
1302 dev->mode_config.dpms_property = dpms;
1304 dev_path = drm_property_create(dev,
1305 DRM_MODE_PROP_BLOB |
1306 DRM_MODE_PROP_IMMUTABLE,
1308 dev->mode_config.path_property = dev_path;
1313 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1315 struct drm_property *type;
1318 * Standard properties (apply to all planes)
1320 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1321 "type", drm_plane_type_enum_list,
1322 ARRAY_SIZE(drm_plane_type_enum_list));
1323 dev->mode_config.plane_type_property = type;
1329 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1332 * Called by a driver the first time a DVI-I connector is made.
1334 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1336 struct drm_property *dvi_i_selector;
1337 struct drm_property *dvi_i_subconnector;
1339 if (dev->mode_config.dvi_i_select_subconnector_property)
1343 drm_property_create_enum(dev, 0,
1344 "select subconnector",
1345 drm_dvi_i_select_enum_list,
1346 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1347 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1349 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1351 drm_dvi_i_subconnector_enum_list,
1352 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1353 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1357 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1360 * drm_create_tv_properties - create TV specific connector properties
1362 * @num_modes: number of different TV formats (modes) supported
1363 * @modes: array of pointers to strings containing name of each format
1365 * Called by a driver's TV initialization routine, this function creates
1366 * the TV specific connector properties for a given device. Caller is
1367 * responsible for allocating a list of format names and passing them to
1370 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1373 struct drm_property *tv_selector;
1374 struct drm_property *tv_subconnector;
1377 if (dev->mode_config.tv_select_subconnector_property)
1381 * Basic connector properties
1383 tv_selector = drm_property_create_enum(dev, 0,
1384 "select subconnector",
1385 drm_tv_select_enum_list,
1386 ARRAY_SIZE(drm_tv_select_enum_list));
1387 dev->mode_config.tv_select_subconnector_property = tv_selector;
1390 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1392 drm_tv_subconnector_enum_list,
1393 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1394 dev->mode_config.tv_subconnector_property = tv_subconnector;
1397 * Other, TV specific properties: margins & TV modes.
1399 dev->mode_config.tv_left_margin_property =
1400 drm_property_create_range(dev, 0, "left margin", 0, 100);
1402 dev->mode_config.tv_right_margin_property =
1403 drm_property_create_range(dev, 0, "right margin", 0, 100);
1405 dev->mode_config.tv_top_margin_property =
1406 drm_property_create_range(dev, 0, "top margin", 0, 100);
1408 dev->mode_config.tv_bottom_margin_property =
1409 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1411 dev->mode_config.tv_mode_property =
1412 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1414 for (i = 0; i < num_modes; i++)
1415 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1418 dev->mode_config.tv_brightness_property =
1419 drm_property_create_range(dev, 0, "brightness", 0, 100);
1421 dev->mode_config.tv_contrast_property =
1422 drm_property_create_range(dev, 0, "contrast", 0, 100);
1424 dev->mode_config.tv_flicker_reduction_property =
1425 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1427 dev->mode_config.tv_overscan_property =
1428 drm_property_create_range(dev, 0, "overscan", 0, 100);
1430 dev->mode_config.tv_saturation_property =
1431 drm_property_create_range(dev, 0, "saturation", 0, 100);
1433 dev->mode_config.tv_hue_property =
1434 drm_property_create_range(dev, 0, "hue", 0, 100);
1438 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1441 * drm_mode_create_scaling_mode_property - create scaling mode property
1444 * Called by a driver the first time it's needed, must be attached to desired
1447 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1449 struct drm_property *scaling_mode;
1451 if (dev->mode_config.scaling_mode_property)
1455 drm_property_create_enum(dev, 0, "scaling mode",
1456 drm_scaling_mode_enum_list,
1457 ARRAY_SIZE(drm_scaling_mode_enum_list));
1459 dev->mode_config.scaling_mode_property = scaling_mode;
1463 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1466 * drm_mode_create_dirty_property - create dirty property
1469 * Called by a driver the first time it's needed, must be attached to desired
1472 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1474 struct drm_property *dirty_info;
1476 if (dev->mode_config.dirty_info_property)
1480 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1482 drm_dirty_info_enum_list,
1483 ARRAY_SIZE(drm_dirty_info_enum_list));
1484 dev->mode_config.dirty_info_property = dirty_info;
1488 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1490 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1492 uint32_t total_objects = 0;
1494 total_objects += dev->mode_config.num_crtc;
1495 total_objects += dev->mode_config.num_connector;
1496 total_objects += dev->mode_config.num_encoder;
1497 total_objects += dev->mode_config.num_bridge;
1499 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1500 if (!group->id_list)
1503 group->num_crtcs = 0;
1504 group->num_connectors = 0;
1505 group->num_encoders = 0;
1506 group->num_bridges = 0;
1510 void drm_mode_group_destroy(struct drm_mode_group *group)
1512 kfree(group->id_list);
1513 group->id_list = NULL;
1517 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1518 * the drm core's responsibility to set up mode control groups.
1520 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1521 struct drm_mode_group *group)
1523 struct drm_crtc *crtc;
1524 struct drm_encoder *encoder;
1525 struct drm_connector *connector;
1526 struct drm_bridge *bridge;
1529 if ((ret = drm_mode_group_init(dev, group)))
1532 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1533 group->id_list[group->num_crtcs++] = crtc->base.id;
1535 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1536 group->id_list[group->num_crtcs + group->num_encoders++] =
1539 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1540 group->id_list[group->num_crtcs + group->num_encoders +
1541 group->num_connectors++] = connector->base.id;
1543 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1544 group->id_list[group->num_crtcs + group->num_encoders +
1545 group->num_connectors + group->num_bridges++] =
1550 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1552 void drm_reinit_primary_mode_group(struct drm_device *dev)
1554 drm_modeset_lock_all(dev);
1555 drm_mode_group_destroy(&dev->primary->mode_group);
1556 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1557 drm_modeset_unlock_all(dev);
1559 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1562 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1563 * @out: drm_mode_modeinfo struct to return to the user
1564 * @in: drm_display_mode to use
1566 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1569 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1570 const struct drm_display_mode *in)
1572 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1573 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1574 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1575 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1576 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1577 "timing values too large for mode info\n");
1579 out->clock = in->clock;
1580 out->hdisplay = in->hdisplay;
1581 out->hsync_start = in->hsync_start;
1582 out->hsync_end = in->hsync_end;
1583 out->htotal = in->htotal;
1584 out->hskew = in->hskew;
1585 out->vdisplay = in->vdisplay;
1586 out->vsync_start = in->vsync_start;
1587 out->vsync_end = in->vsync_end;
1588 out->vtotal = in->vtotal;
1589 out->vscan = in->vscan;
1590 out->vrefresh = in->vrefresh;
1591 out->flags = in->flags;
1592 out->type = in->type;
1593 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1594 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1598 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1599 * @out: drm_display_mode to return to the user
1600 * @in: drm_mode_modeinfo to use
1602 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1606 * Zero on success, errno on failure.
1608 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1609 const struct drm_mode_modeinfo *in)
1611 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1614 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1617 out->clock = in->clock;
1618 out->hdisplay = in->hdisplay;
1619 out->hsync_start = in->hsync_start;
1620 out->hsync_end = in->hsync_end;
1621 out->htotal = in->htotal;
1622 out->hskew = in->hskew;
1623 out->vdisplay = in->vdisplay;
1624 out->vsync_start = in->vsync_start;
1625 out->vsync_end = in->vsync_end;
1626 out->vtotal = in->vtotal;
1627 out->vscan = in->vscan;
1628 out->vrefresh = in->vrefresh;
1629 out->flags = in->flags;
1630 out->type = in->type;
1631 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1632 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1638 * drm_mode_getresources - get graphics configuration
1639 * @dev: drm device for the ioctl
1640 * @data: data pointer for the ioctl
1641 * @file_priv: drm file for the ioctl call
1643 * Construct a set of configuration description structures and return
1644 * them to the user, including CRTC, connector and framebuffer configuration.
1646 * Called by the user via ioctl.
1649 * Zero on success, errno on failure.
1651 int drm_mode_getresources(struct drm_device *dev, void *data,
1652 struct drm_file *file_priv)
1654 struct drm_mode_card_res *card_res = data;
1655 struct list_head *lh;
1656 struct drm_framebuffer *fb;
1657 struct drm_connector *connector;
1658 struct drm_crtc *crtc;
1659 struct drm_encoder *encoder;
1661 int connector_count = 0;
1664 int encoder_count = 0;
1666 uint32_t __user *fb_id;
1667 uint32_t __user *crtc_id;
1668 uint32_t __user *connector_id;
1669 uint32_t __user *encoder_id;
1670 struct drm_mode_group *mode_group;
1672 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1676 mutex_lock(&file_priv->fbs_lock);
1678 * For the non-control nodes we need to limit the list of resources
1679 * by IDs in the group list for this node
1681 list_for_each(lh, &file_priv->fbs)
1684 /* handle this in 4 parts */
1686 if (card_res->count_fbs >= fb_count) {
1688 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1689 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1690 if (put_user(fb->base.id, fb_id + copied)) {
1691 mutex_unlock(&file_priv->fbs_lock);
1697 card_res->count_fbs = fb_count;
1698 mutex_unlock(&file_priv->fbs_lock);
1700 drm_modeset_lock_all(dev);
1701 if (!drm_is_primary_client(file_priv)) {
1704 list_for_each(lh, &dev->mode_config.crtc_list)
1707 list_for_each(lh, &dev->mode_config.connector_list)
1710 list_for_each(lh, &dev->mode_config.encoder_list)
1714 mode_group = &file_priv->master->minor->mode_group;
1715 crtc_count = mode_group->num_crtcs;
1716 connector_count = mode_group->num_connectors;
1717 encoder_count = mode_group->num_encoders;
1720 card_res->max_height = dev->mode_config.max_height;
1721 card_res->min_height = dev->mode_config.min_height;
1722 card_res->max_width = dev->mode_config.max_width;
1723 card_res->min_width = dev->mode_config.min_width;
1726 if (card_res->count_crtcs >= crtc_count) {
1728 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1730 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1732 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1733 if (put_user(crtc->base.id, crtc_id + copied)) {
1740 for (i = 0; i < mode_group->num_crtcs; i++) {
1741 if (put_user(mode_group->id_list[i],
1742 crtc_id + copied)) {
1750 card_res->count_crtcs = crtc_count;
1753 if (card_res->count_encoders >= encoder_count) {
1755 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1757 list_for_each_entry(encoder,
1758 &dev->mode_config.encoder_list,
1760 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1762 if (put_user(encoder->base.id, encoder_id +
1770 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1771 if (put_user(mode_group->id_list[i],
1772 encoder_id + copied)) {
1781 card_res->count_encoders = encoder_count;
1784 if (card_res->count_connectors >= connector_count) {
1786 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1788 list_for_each_entry(connector,
1789 &dev->mode_config.connector_list,
1791 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1794 if (put_user(connector->base.id,
1795 connector_id + copied)) {
1802 int start = mode_group->num_crtcs +
1803 mode_group->num_encoders;
1804 for (i = start; i < start + mode_group->num_connectors; i++) {
1805 if (put_user(mode_group->id_list[i],
1806 connector_id + copied)) {
1814 card_res->count_connectors = connector_count;
1816 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1817 card_res->count_connectors, card_res->count_encoders);
1820 drm_modeset_unlock_all(dev);
1825 * drm_mode_getcrtc - get CRTC configuration
1826 * @dev: drm device for the ioctl
1827 * @data: data pointer for the ioctl
1828 * @file_priv: drm file for the ioctl call
1830 * Construct a CRTC configuration structure to return to the user.
1832 * Called by the user via ioctl.
1835 * Zero on success, errno on failure.
1837 int drm_mode_getcrtc(struct drm_device *dev,
1838 void *data, struct drm_file *file_priv)
1840 struct drm_mode_crtc *crtc_resp = data;
1841 struct drm_crtc *crtc;
1844 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1847 drm_modeset_lock_all(dev);
1849 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1855 crtc_resp->x = crtc->x;
1856 crtc_resp->y = crtc->y;
1857 crtc_resp->gamma_size = crtc->gamma_size;
1858 if (crtc->primary->fb)
1859 crtc_resp->fb_id = crtc->primary->fb->base.id;
1861 crtc_resp->fb_id = 0;
1863 if (crtc->enabled) {
1865 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1866 crtc_resp->mode_valid = 1;
1869 crtc_resp->mode_valid = 0;
1873 drm_modeset_unlock_all(dev);
1877 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1878 const struct drm_file *file_priv)
1881 * If user-space hasn't configured the driver to expose the stereo 3D
1882 * modes, don't expose them.
1884 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1891 * drm_mode_getconnector - get connector configuration
1892 * @dev: drm device for the ioctl
1893 * @data: data pointer for the ioctl
1894 * @file_priv: drm file for the ioctl call
1896 * Construct a connector configuration structure to return to the user.
1898 * Called by the user via ioctl.
1901 * Zero on success, errno on failure.
1903 int drm_mode_getconnector(struct drm_device *dev, void *data,
1904 struct drm_file *file_priv)
1906 struct drm_mode_get_connector *out_resp = data;
1907 struct drm_connector *connector;
1908 struct drm_display_mode *mode;
1910 int props_count = 0;
1911 int encoders_count = 0;
1915 struct drm_mode_modeinfo u_mode;
1916 struct drm_mode_modeinfo __user *mode_ptr;
1917 uint32_t __user *prop_ptr;
1918 uint64_t __user *prop_values;
1919 uint32_t __user *encoder_ptr;
1921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1924 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1926 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1928 mutex_lock(&dev->mode_config.mutex);
1930 connector = drm_connector_find(dev, out_resp->connector_id);
1936 props_count = connector->properties.count;
1938 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1939 if (connector->encoder_ids[i] != 0) {
1944 if (out_resp->count_modes == 0) {
1945 connector->funcs->fill_modes(connector,
1946 dev->mode_config.max_width,
1947 dev->mode_config.max_height);
1950 /* delayed so we get modes regardless of pre-fill_modes state */
1951 list_for_each_entry(mode, &connector->modes, head)
1952 if (drm_mode_expose_to_userspace(mode, file_priv))
1955 out_resp->connector_id = connector->base.id;
1956 out_resp->connector_type = connector->connector_type;
1957 out_resp->connector_type_id = connector->connector_type_id;
1958 out_resp->mm_width = connector->display_info.width_mm;
1959 out_resp->mm_height = connector->display_info.height_mm;
1960 out_resp->subpixel = connector->display_info.subpixel_order;
1961 out_resp->connection = connector->status;
1962 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1963 if (connector->encoder)
1964 out_resp->encoder_id = connector->encoder->base.id;
1966 out_resp->encoder_id = 0;
1967 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1970 * This ioctl is called twice, once to determine how much space is
1971 * needed, and the 2nd time to fill it.
1973 if ((out_resp->count_modes >= mode_count) && mode_count) {
1975 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1976 list_for_each_entry(mode, &connector->modes, head) {
1977 if (!drm_mode_expose_to_userspace(mode, file_priv))
1980 drm_crtc_convert_to_umode(&u_mode, mode);
1981 if (copy_to_user(mode_ptr + copied,
1982 &u_mode, sizeof(u_mode))) {
1989 out_resp->count_modes = mode_count;
1991 if ((out_resp->count_props >= props_count) && props_count) {
1993 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1994 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1995 for (i = 0; i < connector->properties.count; i++) {
1996 if (put_user(connector->properties.ids[i],
1997 prop_ptr + copied)) {
2002 if (put_user(connector->properties.values[i],
2003 prop_values + copied)) {
2010 out_resp->count_props = props_count;
2012 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2014 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2015 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2016 if (connector->encoder_ids[i] != 0) {
2017 if (put_user(connector->encoder_ids[i],
2018 encoder_ptr + copied)) {
2026 out_resp->count_encoders = encoders_count;
2029 mutex_unlock(&dev->mode_config.mutex);
2035 * drm_mode_getencoder - get encoder configuration
2036 * @dev: drm device for the ioctl
2037 * @data: data pointer for the ioctl
2038 * @file_priv: drm file for the ioctl call
2040 * Construct a encoder configuration structure to return to the user.
2042 * Called by the user via ioctl.
2045 * Zero on success, errno on failure.
2047 int drm_mode_getencoder(struct drm_device *dev, void *data,
2048 struct drm_file *file_priv)
2050 struct drm_mode_get_encoder *enc_resp = data;
2051 struct drm_encoder *encoder;
2054 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2057 drm_modeset_lock_all(dev);
2058 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2065 enc_resp->crtc_id = encoder->crtc->base.id;
2067 enc_resp->crtc_id = 0;
2068 enc_resp->encoder_type = encoder->encoder_type;
2069 enc_resp->encoder_id = encoder->base.id;
2070 enc_resp->possible_crtcs = encoder->possible_crtcs;
2071 enc_resp->possible_clones = encoder->possible_clones;
2074 drm_modeset_unlock_all(dev);
2079 * drm_mode_getplane_res - enumerate all plane resources
2082 * @file_priv: DRM file info
2084 * Construct a list of plane ids to return to the user.
2086 * Called by the user via ioctl.
2089 * Zero on success, errno on failure.
2091 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2092 struct drm_file *file_priv)
2094 struct drm_mode_get_plane_res *plane_resp = data;
2095 struct drm_mode_config *config;
2096 struct drm_plane *plane;
2097 uint32_t __user *plane_ptr;
2098 int copied = 0, ret = 0;
2099 unsigned num_planes;
2101 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2104 drm_modeset_lock_all(dev);
2105 config = &dev->mode_config;
2107 if (file_priv->universal_planes)
2108 num_planes = config->num_total_plane;
2110 num_planes = config->num_overlay_plane;
2113 * This ioctl is called twice, once to determine how much space is
2114 * needed, and the 2nd time to fill it.
2117 (plane_resp->count_planes >= num_planes)) {
2118 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2120 list_for_each_entry(plane, &config->plane_list, head) {
2122 * Unless userspace set the 'universal planes'
2123 * capability bit, only advertise overlays.
2125 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2126 !file_priv->universal_planes)
2129 if (put_user(plane->base.id, plane_ptr + copied)) {
2136 plane_resp->count_planes = num_planes;
2139 drm_modeset_unlock_all(dev);
2144 * drm_mode_getplane - get plane configuration
2147 * @file_priv: DRM file info
2149 * Construct a plane configuration structure to return to the user.
2151 * Called by the user via ioctl.
2154 * Zero on success, errno on failure.
2156 int drm_mode_getplane(struct drm_device *dev, void *data,
2157 struct drm_file *file_priv)
2159 struct drm_mode_get_plane *plane_resp = data;
2160 struct drm_plane *plane;
2161 uint32_t __user *format_ptr;
2164 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2167 drm_modeset_lock_all(dev);
2168 plane = drm_plane_find(dev, plane_resp->plane_id);
2175 plane_resp->crtc_id = plane->crtc->base.id;
2177 plane_resp->crtc_id = 0;
2180 plane_resp->fb_id = plane->fb->base.id;
2182 plane_resp->fb_id = 0;
2184 plane_resp->plane_id = plane->base.id;
2185 plane_resp->possible_crtcs = plane->possible_crtcs;
2186 plane_resp->gamma_size = 0;
2189 * This ioctl is called twice, once to determine how much space is
2190 * needed, and the 2nd time to fill it.
2192 if (plane->format_count &&
2193 (plane_resp->count_format_types >= plane->format_count)) {
2194 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2195 if (copy_to_user(format_ptr,
2196 plane->format_types,
2197 sizeof(uint32_t) * plane->format_count)) {
2202 plane_resp->count_format_types = plane->format_count;
2205 drm_modeset_unlock_all(dev);
2210 * setplane_internal - setplane handler for internal callers
2212 * Note that we assume an extra reference has already been taken on fb. If the
2213 * update fails, this reference will be dropped before return; if it succeeds,
2214 * the previous framebuffer (if any) will be unreferenced instead.
2216 * src_{x,y,w,h} are provided in 16.16 fixed point format
2218 static int setplane_internal(struct drm_plane *plane,
2219 struct drm_crtc *crtc,
2220 struct drm_framebuffer *fb,
2221 int32_t crtc_x, int32_t crtc_y,
2222 uint32_t crtc_w, uint32_t crtc_h,
2223 /* src_{x,y,w,h} values are 16.16 fixed point */
2224 uint32_t src_x, uint32_t src_y,
2225 uint32_t src_w, uint32_t src_h)
2227 struct drm_device *dev = plane->dev;
2228 struct drm_framebuffer *old_fb = NULL;
2230 unsigned int fb_width, fb_height;
2233 /* No fb means shut it down */
2235 drm_modeset_lock_all(dev);
2237 ret = plane->funcs->disable_plane(plane);
2244 drm_modeset_unlock_all(dev);
2248 /* Check whether this plane is usable on this CRTC */
2249 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2250 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2255 /* Check whether this plane supports the fb pixel format. */
2256 for (i = 0; i < plane->format_count; i++)
2257 if (fb->pixel_format == plane->format_types[i])
2259 if (i == plane->format_count) {
2260 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2261 drm_get_format_name(fb->pixel_format));
2266 fb_width = fb->width << 16;
2267 fb_height = fb->height << 16;
2269 /* Make sure source coordinates are inside the fb. */
2270 if (src_w > fb_width ||
2271 src_x > fb_width - src_w ||
2272 src_h > fb_height ||
2273 src_y > fb_height - src_h) {
2274 DRM_DEBUG_KMS("Invalid source coordinates "
2275 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2276 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2277 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2278 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2279 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2284 drm_modeset_lock_all(dev);
2286 ret = plane->funcs->update_plane(plane, crtc, fb,
2287 crtc_x, crtc_y, crtc_w, crtc_h,
2288 src_x, src_y, src_w, src_h);
2296 drm_modeset_unlock_all(dev);
2300 drm_framebuffer_unreference(fb);
2302 drm_framebuffer_unreference(old_fb);
2309 * drm_mode_setplane - configure a plane's configuration
2311 * @data: ioctl data*
2312 * @file_priv: DRM file info
2314 * Set plane configuration, including placement, fb, scaling, and other factors.
2315 * Or pass a NULL fb to disable (planes may be disabled without providing a
2319 * Zero on success, errno on failure.
2321 int drm_mode_setplane(struct drm_device *dev, void *data,
2322 struct drm_file *file_priv)
2324 struct drm_mode_set_plane *plane_req = data;
2325 struct drm_mode_object *obj;
2326 struct drm_plane *plane;
2327 struct drm_crtc *crtc = NULL;
2328 struct drm_framebuffer *fb = NULL;
2330 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2333 /* Give drivers some help against integer overflows */
2334 if (plane_req->crtc_w > INT_MAX ||
2335 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2336 plane_req->crtc_h > INT_MAX ||
2337 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2338 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2339 plane_req->crtc_w, plane_req->crtc_h,
2340 plane_req->crtc_x, plane_req->crtc_y);
2345 * First, find the plane, crtc, and fb objects. If not available,
2346 * we don't bother to call the driver.
2348 obj = drm_mode_object_find(dev, plane_req->plane_id,
2349 DRM_MODE_OBJECT_PLANE);
2351 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2352 plane_req->plane_id);
2355 plane = obj_to_plane(obj);
2357 if (plane_req->fb_id) {
2358 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2360 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2365 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2366 DRM_MODE_OBJECT_CRTC);
2368 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2369 plane_req->crtc_id);
2372 crtc = obj_to_crtc(obj);
2376 * setplane_internal will take care of deref'ing either the old or new
2377 * framebuffer depending on success.
2379 return setplane_internal(plane, crtc, fb,
2380 plane_req->crtc_x, plane_req->crtc_y,
2381 plane_req->crtc_w, plane_req->crtc_h,
2382 plane_req->src_x, plane_req->src_y,
2383 plane_req->src_w, plane_req->src_h);
2387 * drm_mode_set_config_internal - helper to call ->set_config
2388 * @set: modeset config to set
2390 * This is a little helper to wrap internal calls to the ->set_config driver
2391 * interface. The only thing it adds is correct refcounting dance.
2394 * Zero on success, errno on failure.
2396 int drm_mode_set_config_internal(struct drm_mode_set *set)
2398 struct drm_crtc *crtc = set->crtc;
2399 struct drm_framebuffer *fb;
2400 struct drm_crtc *tmp;
2404 * NOTE: ->set_config can also disable other crtcs (if we steal all
2405 * connectors from it), hence we need to refcount the fbs across all
2406 * crtcs. Atomic modeset will have saner semantics ...
2408 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2409 tmp->old_fb = tmp->primary->fb;
2413 ret = crtc->funcs->set_config(set);
2415 crtc->primary->crtc = crtc;
2416 crtc->primary->fb = fb;
2419 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2420 if (tmp->primary->fb)
2421 drm_framebuffer_reference(tmp->primary->fb);
2423 drm_framebuffer_unreference(tmp->old_fb);
2428 EXPORT_SYMBOL(drm_mode_set_config_internal);
2431 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2433 * @crtc: CRTC that framebuffer will be displayed on
2436 * @mode: mode that framebuffer will be displayed under
2437 * @fb: framebuffer to check size of
2439 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2441 const struct drm_display_mode *mode,
2442 const struct drm_framebuffer *fb)
2445 int hdisplay, vdisplay;
2447 hdisplay = mode->hdisplay;
2448 vdisplay = mode->vdisplay;
2450 if (drm_mode_is_stereo(mode)) {
2451 struct drm_display_mode adjusted = *mode;
2453 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2454 hdisplay = adjusted.crtc_hdisplay;
2455 vdisplay = adjusted.crtc_vdisplay;
2458 if (crtc->invert_dimensions)
2459 swap(hdisplay, vdisplay);
2461 if (hdisplay > fb->width ||
2462 vdisplay > fb->height ||
2463 x > fb->width - hdisplay ||
2464 y > fb->height - vdisplay) {
2465 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2466 fb->width, fb->height, hdisplay, vdisplay, x, y,
2467 crtc->invert_dimensions ? " (inverted)" : "");
2473 EXPORT_SYMBOL(drm_crtc_check_viewport);
2476 * drm_mode_setcrtc - set CRTC configuration
2477 * @dev: drm device for the ioctl
2478 * @data: data pointer for the ioctl
2479 * @file_priv: drm file for the ioctl call
2481 * Build a new CRTC configuration based on user request.
2483 * Called by the user via ioctl.
2486 * Zero on success, errno on failure.
2488 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2489 struct drm_file *file_priv)
2491 struct drm_mode_config *config = &dev->mode_config;
2492 struct drm_mode_crtc *crtc_req = data;
2493 struct drm_crtc *crtc;
2494 struct drm_connector **connector_set = NULL, *connector;
2495 struct drm_framebuffer *fb = NULL;
2496 struct drm_display_mode *mode = NULL;
2497 struct drm_mode_set set;
2498 uint32_t __user *set_connectors_ptr;
2502 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2505 /* For some reason crtc x/y offsets are signed internally. */
2506 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2509 drm_modeset_lock_all(dev);
2510 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2512 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2516 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2518 if (crtc_req->mode_valid) {
2519 /* If we have a mode we need a framebuffer. */
2520 /* If we pass -1, set the mode with the currently bound fb */
2521 if (crtc_req->fb_id == -1) {
2522 if (!crtc->primary->fb) {
2523 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2527 fb = crtc->primary->fb;
2528 /* Make refcounting symmetric with the lookup path. */
2529 drm_framebuffer_reference(fb);
2531 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2533 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2540 mode = drm_mode_create(dev);
2546 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2548 DRM_DEBUG_KMS("Invalid mode\n");
2552 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2554 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2561 if (crtc_req->count_connectors == 0 && mode) {
2562 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2567 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2568 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2569 crtc_req->count_connectors);
2574 if (crtc_req->count_connectors > 0) {
2577 /* Avoid unbounded kernel memory allocation */
2578 if (crtc_req->count_connectors > config->num_connector) {
2583 connector_set = kmalloc(crtc_req->count_connectors *
2584 sizeof(struct drm_connector *),
2586 if (!connector_set) {
2591 for (i = 0; i < crtc_req->count_connectors; i++) {
2592 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2593 if (get_user(out_id, &set_connectors_ptr[i])) {
2598 connector = drm_connector_find(dev, out_id);
2600 DRM_DEBUG_KMS("Connector id %d unknown\n",
2605 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2609 connector_set[i] = connector;
2614 set.x = crtc_req->x;
2615 set.y = crtc_req->y;
2617 set.connectors = connector_set;
2618 set.num_connectors = crtc_req->count_connectors;
2620 ret = drm_mode_set_config_internal(&set);
2624 drm_framebuffer_unreference(fb);
2626 kfree(connector_set);
2627 drm_mode_destroy(dev, mode);
2628 drm_modeset_unlock_all(dev);
2633 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2634 * universal plane handler call
2635 * @crtc: crtc to update cursor for
2636 * @req: data pointer for the ioctl
2637 * @file_priv: drm file for the ioctl call
2639 * Legacy cursor ioctl's work directly with driver buffer handles. To
2640 * translate legacy ioctl calls into universal plane handler calls, we need to
2641 * wrap the native buffer handle in a drm_framebuffer.
2643 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2644 * buffer with a pitch of 4*width; the universal plane interface should be used
2645 * directly in cases where the hardware can support other buffer settings and
2646 * userspace wants to make use of these capabilities.
2649 * Zero on success, errno on failure.
2651 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2652 struct drm_mode_cursor2 *req,
2653 struct drm_file *file_priv)
2655 struct drm_device *dev = crtc->dev;
2656 struct drm_framebuffer *fb = NULL;
2657 struct drm_mode_fb_cmd2 fbreq = {
2658 .width = req->width,
2659 .height = req->height,
2660 .pixel_format = DRM_FORMAT_ARGB8888,
2661 .pitches = { req->width * 4 },
2662 .handles = { req->handle },
2664 int32_t crtc_x, crtc_y;
2665 uint32_t crtc_w = 0, crtc_h = 0;
2666 uint32_t src_w = 0, src_h = 0;
2669 BUG_ON(!crtc->cursor);
2672 * Obtain fb we'll be using (either new or existing) and take an extra
2673 * reference to it if fb != null. setplane will take care of dropping
2674 * the reference if the plane update fails.
2676 if (req->flags & DRM_MODE_CURSOR_BO) {
2678 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2680 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2684 drm_framebuffer_reference(fb);
2689 mutex_lock(&dev->mode_config.mutex);
2690 fb = crtc->cursor->fb;
2692 drm_framebuffer_reference(fb);
2693 mutex_unlock(&dev->mode_config.mutex);
2696 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2700 crtc_x = crtc->cursor_x;
2701 crtc_y = crtc->cursor_y;
2706 crtc_h = fb->height;
2707 src_w = fb->width << 16;
2708 src_h = fb->height << 16;
2712 * setplane_internal will take care of deref'ing either the old or new
2713 * framebuffer depending on success.
2715 ret = setplane_internal(crtc->cursor, crtc, fb,
2716 crtc_x, crtc_y, crtc_w, crtc_h,
2717 0, 0, src_w, src_h);
2719 /* Update successful; save new cursor position, if necessary */
2720 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2721 crtc->cursor_x = req->x;
2722 crtc->cursor_y = req->y;
2728 static int drm_mode_cursor_common(struct drm_device *dev,
2729 struct drm_mode_cursor2 *req,
2730 struct drm_file *file_priv)
2732 struct drm_crtc *crtc;
2735 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2738 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2741 crtc = drm_crtc_find(dev, req->crtc_id);
2743 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2748 * If this crtc has a universal cursor plane, call that plane's update
2749 * handler rather than using legacy cursor handlers.
2752 return drm_mode_cursor_universal(crtc, req, file_priv);
2754 drm_modeset_lock(&crtc->mutex, NULL);
2755 if (req->flags & DRM_MODE_CURSOR_BO) {
2756 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2760 /* Turns off the cursor if handle is 0 */
2761 if (crtc->funcs->cursor_set2)
2762 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2763 req->width, req->height, req->hot_x, req->hot_y);
2765 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2766 req->width, req->height);
2769 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2770 if (crtc->funcs->cursor_move) {
2771 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2778 drm_modeset_unlock(&crtc->mutex);
2786 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2787 * @dev: drm device for the ioctl
2788 * @data: data pointer for the ioctl
2789 * @file_priv: drm file for the ioctl call
2791 * Set the cursor configuration based on user request.
2793 * Called by the user via ioctl.
2796 * Zero on success, errno on failure.
2798 int drm_mode_cursor_ioctl(struct drm_device *dev,
2799 void *data, struct drm_file *file_priv)
2801 struct drm_mode_cursor *req = data;
2802 struct drm_mode_cursor2 new_req;
2804 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2805 new_req.hot_x = new_req.hot_y = 0;
2807 return drm_mode_cursor_common(dev, &new_req, file_priv);
2811 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2812 * @dev: drm device for the ioctl
2813 * @data: data pointer for the ioctl
2814 * @file_priv: drm file for the ioctl call
2816 * Set the cursor configuration based on user request. This implements the 2nd
2817 * version of the cursor ioctl, which allows userspace to additionally specify
2818 * the hotspot of the pointer.
2820 * Called by the user via ioctl.
2823 * Zero on success, errno on failure.
2825 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2826 void *data, struct drm_file *file_priv)
2828 struct drm_mode_cursor2 *req = data;
2829 return drm_mode_cursor_common(dev, req, file_priv);
2833 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2834 * @bpp: bits per pixels
2835 * @depth: bit depth per pixel
2837 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2838 * Useful in fbdev emulation code, since that deals in those values.
2840 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2846 fmt = DRM_FORMAT_C8;
2850 fmt = DRM_FORMAT_XRGB1555;
2852 fmt = DRM_FORMAT_RGB565;
2855 fmt = DRM_FORMAT_RGB888;
2859 fmt = DRM_FORMAT_XRGB8888;
2860 else if (depth == 30)
2861 fmt = DRM_FORMAT_XRGB2101010;
2863 fmt = DRM_FORMAT_ARGB8888;
2866 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2867 fmt = DRM_FORMAT_XRGB8888;
2873 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2876 * drm_mode_addfb - add an FB to the graphics configuration
2877 * @dev: drm device for the ioctl
2878 * @data: data pointer for the ioctl
2879 * @file_priv: drm file for the ioctl call
2881 * Add a new FB to the specified CRTC, given a user request. This is the
2882 * original addfb ioclt which only supported RGB formats.
2884 * Called by the user via ioctl.
2887 * Zero on success, errno on failure.
2889 int drm_mode_addfb(struct drm_device *dev,
2890 void *data, struct drm_file *file_priv)
2892 struct drm_mode_fb_cmd *or = data;
2893 struct drm_mode_fb_cmd2 r = {};
2894 struct drm_mode_config *config = &dev->mode_config;
2895 struct drm_framebuffer *fb;
2898 /* Use new struct with format internally */
2899 r.fb_id = or->fb_id;
2900 r.width = or->width;
2901 r.height = or->height;
2902 r.pitches[0] = or->pitch;
2903 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2904 r.handles[0] = or->handle;
2906 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2909 if ((config->min_width > r.width) || (r.width > config->max_width))
2912 if ((config->min_height > r.height) || (r.height > config->max_height))
2915 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2917 DRM_DEBUG_KMS("could not create framebuffer\n");
2921 mutex_lock(&file_priv->fbs_lock);
2922 or->fb_id = fb->base.id;
2923 list_add(&fb->filp_head, &file_priv->fbs);
2924 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2925 mutex_unlock(&file_priv->fbs_lock);
2930 static int format_check(const struct drm_mode_fb_cmd2 *r)
2932 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2936 case DRM_FORMAT_RGB332:
2937 case DRM_FORMAT_BGR233:
2938 case DRM_FORMAT_XRGB4444:
2939 case DRM_FORMAT_XBGR4444:
2940 case DRM_FORMAT_RGBX4444:
2941 case DRM_FORMAT_BGRX4444:
2942 case DRM_FORMAT_ARGB4444:
2943 case DRM_FORMAT_ABGR4444:
2944 case DRM_FORMAT_RGBA4444:
2945 case DRM_FORMAT_BGRA4444:
2946 case DRM_FORMAT_XRGB1555:
2947 case DRM_FORMAT_XBGR1555:
2948 case DRM_FORMAT_RGBX5551:
2949 case DRM_FORMAT_BGRX5551:
2950 case DRM_FORMAT_ARGB1555:
2951 case DRM_FORMAT_ABGR1555:
2952 case DRM_FORMAT_RGBA5551:
2953 case DRM_FORMAT_BGRA5551:
2954 case DRM_FORMAT_RGB565:
2955 case DRM_FORMAT_BGR565:
2956 case DRM_FORMAT_RGB888:
2957 case DRM_FORMAT_BGR888:
2958 case DRM_FORMAT_XRGB8888:
2959 case DRM_FORMAT_XBGR8888:
2960 case DRM_FORMAT_RGBX8888:
2961 case DRM_FORMAT_BGRX8888:
2962 case DRM_FORMAT_ARGB8888:
2963 case DRM_FORMAT_ABGR8888:
2964 case DRM_FORMAT_RGBA8888:
2965 case DRM_FORMAT_BGRA8888:
2966 case DRM_FORMAT_XRGB2101010:
2967 case DRM_FORMAT_XBGR2101010:
2968 case DRM_FORMAT_RGBX1010102:
2969 case DRM_FORMAT_BGRX1010102:
2970 case DRM_FORMAT_ARGB2101010:
2971 case DRM_FORMAT_ABGR2101010:
2972 case DRM_FORMAT_RGBA1010102:
2973 case DRM_FORMAT_BGRA1010102:
2974 case DRM_FORMAT_YUYV:
2975 case DRM_FORMAT_YVYU:
2976 case DRM_FORMAT_UYVY:
2977 case DRM_FORMAT_VYUY:
2978 case DRM_FORMAT_AYUV:
2979 case DRM_FORMAT_NV12:
2980 case DRM_FORMAT_NV21:
2981 case DRM_FORMAT_NV16:
2982 case DRM_FORMAT_NV61:
2983 case DRM_FORMAT_NV24:
2984 case DRM_FORMAT_NV42:
2985 case DRM_FORMAT_YUV410:
2986 case DRM_FORMAT_YVU410:
2987 case DRM_FORMAT_YUV411:
2988 case DRM_FORMAT_YVU411:
2989 case DRM_FORMAT_YUV420:
2990 case DRM_FORMAT_YVU420:
2991 case DRM_FORMAT_YUV422:
2992 case DRM_FORMAT_YVU422:
2993 case DRM_FORMAT_YUV444:
2994 case DRM_FORMAT_YVU444:
2997 DRM_DEBUG_KMS("invalid pixel format %s\n",
2998 drm_get_format_name(r->pixel_format));
3003 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3005 int ret, hsub, vsub, num_planes, i;
3007 ret = format_check(r);
3009 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3010 drm_get_format_name(r->pixel_format));
3014 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3015 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3016 num_planes = drm_format_num_planes(r->pixel_format);
3018 if (r->width == 0 || r->width % hsub) {
3019 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
3023 if (r->height == 0 || r->height % vsub) {
3024 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3028 for (i = 0; i < num_planes; i++) {
3029 unsigned int width = r->width / (i != 0 ? hsub : 1);
3030 unsigned int height = r->height / (i != 0 ? vsub : 1);
3031 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3033 if (!r->handles[i]) {
3034 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3038 if ((uint64_t) width * cpp > UINT_MAX)
3041 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3044 if (r->pitches[i] < width * cpp) {
3045 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3053 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3054 struct drm_mode_fb_cmd2 *r,
3055 struct drm_file *file_priv)
3057 struct drm_mode_config *config = &dev->mode_config;
3058 struct drm_framebuffer *fb;
3061 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3062 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3063 return ERR_PTR(-EINVAL);
3066 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3067 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3068 r->width, config->min_width, config->max_width);
3069 return ERR_PTR(-EINVAL);
3071 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3072 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3073 r->height, config->min_height, config->max_height);
3074 return ERR_PTR(-EINVAL);
3077 ret = framebuffer_check(r);
3079 return ERR_PTR(ret);
3081 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3083 DRM_DEBUG_KMS("could not create framebuffer\n");
3087 mutex_lock(&file_priv->fbs_lock);
3088 r->fb_id = fb->base.id;
3089 list_add(&fb->filp_head, &file_priv->fbs);
3090 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3091 mutex_unlock(&file_priv->fbs_lock);
3097 * drm_mode_addfb2 - add an FB to the graphics configuration
3098 * @dev: drm device for the ioctl
3099 * @data: data pointer for the ioctl
3100 * @file_priv: drm file for the ioctl call
3102 * Add a new FB to the specified CRTC, given a user request with format. This is
3103 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3104 * and uses fourcc codes as pixel format specifiers.
3106 * Called by the user via ioctl.
3109 * Zero on success, errno on failure.
3111 int drm_mode_addfb2(struct drm_device *dev,
3112 void *data, struct drm_file *file_priv)
3114 struct drm_framebuffer *fb;
3116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3119 fb = add_framebuffer_internal(dev, data, file_priv);
3127 * drm_mode_rmfb - remove an FB from the configuration
3128 * @dev: drm device for the ioctl
3129 * @data: data pointer for the ioctl
3130 * @file_priv: drm file for the ioctl call
3132 * Remove the FB specified by the user.
3134 * Called by the user via ioctl.
3137 * Zero on success, errno on failure.
3139 int drm_mode_rmfb(struct drm_device *dev,
3140 void *data, struct drm_file *file_priv)
3142 struct drm_framebuffer *fb = NULL;
3143 struct drm_framebuffer *fbl = NULL;
3144 uint32_t *id = data;
3147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3150 mutex_lock(&file_priv->fbs_lock);
3151 mutex_lock(&dev->mode_config.fb_lock);
3152 fb = __drm_framebuffer_lookup(dev, *id);
3156 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3162 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3163 __drm_framebuffer_unregister(dev, fb);
3165 list_del_init(&fb->filp_head);
3166 mutex_unlock(&dev->mode_config.fb_lock);
3167 mutex_unlock(&file_priv->fbs_lock);
3169 drm_framebuffer_remove(fb);
3174 mutex_unlock(&dev->mode_config.fb_lock);
3175 mutex_unlock(&file_priv->fbs_lock);
3181 * drm_mode_getfb - get FB info
3182 * @dev: drm device for the ioctl
3183 * @data: data pointer for the ioctl
3184 * @file_priv: drm file for the ioctl call
3186 * Lookup the FB given its ID and return info about it.
3188 * Called by the user via ioctl.
3191 * Zero on success, errno on failure.
3193 int drm_mode_getfb(struct drm_device *dev,
3194 void *data, struct drm_file *file_priv)
3196 struct drm_mode_fb_cmd *r = data;
3197 struct drm_framebuffer *fb;
3200 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3203 fb = drm_framebuffer_lookup(dev, r->fb_id);
3207 r->height = fb->height;
3208 r->width = fb->width;
3209 r->depth = fb->depth;
3210 r->bpp = fb->bits_per_pixel;
3211 r->pitch = fb->pitches[0];
3212 if (fb->funcs->create_handle) {
3213 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3214 drm_is_control_client(file_priv)) {
3215 ret = fb->funcs->create_handle(fb, file_priv,
3218 /* GET_FB() is an unprivileged ioctl so we must not
3219 * return a buffer-handle to non-master processes! For
3220 * backwards-compatibility reasons, we cannot make
3221 * GET_FB() privileged, so just return an invalid handle
3222 * for non-masters. */
3230 drm_framebuffer_unreference(fb);
3236 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3237 * @dev: drm device for the ioctl
3238 * @data: data pointer for the ioctl
3239 * @file_priv: drm file for the ioctl call
3241 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3242 * rectangle list. Generic userspace which does frontbuffer rendering must call
3243 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3244 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3246 * Modesetting drivers which always update the frontbuffer do not need to
3247 * implement the corresponding ->dirty framebuffer callback.
3249 * Called by the user via ioctl.
3252 * Zero on success, errno on failure.
3254 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3255 void *data, struct drm_file *file_priv)
3257 struct drm_clip_rect __user *clips_ptr;
3258 struct drm_clip_rect *clips = NULL;
3259 struct drm_mode_fb_dirty_cmd *r = data;
3260 struct drm_framebuffer *fb;
3265 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3268 fb = drm_framebuffer_lookup(dev, r->fb_id);
3272 num_clips = r->num_clips;
3273 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3275 if (!num_clips != !clips_ptr) {
3280 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3282 /* If userspace annotates copy, clips must come in pairs */
3283 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3288 if (num_clips && clips_ptr) {
3289 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3293 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3299 ret = copy_from_user(clips, clips_ptr,
3300 num_clips * sizeof(*clips));
3307 if (fb->funcs->dirty) {
3308 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3317 drm_framebuffer_unreference(fb);
3324 * drm_fb_release - remove and free the FBs on this file
3325 * @priv: drm file for the ioctl
3327 * Destroy all the FBs associated with @filp.
3329 * Called by the user via ioctl.
3332 * Zero on success, errno on failure.
3334 void drm_fb_release(struct drm_file *priv)
3336 struct drm_device *dev = priv->minor->dev;
3337 struct drm_framebuffer *fb, *tfb;
3339 mutex_lock(&priv->fbs_lock);
3340 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3342 mutex_lock(&dev->mode_config.fb_lock);
3343 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3344 __drm_framebuffer_unregister(dev, fb);
3345 mutex_unlock(&dev->mode_config.fb_lock);
3347 list_del_init(&fb->filp_head);
3349 /* This will also drop the fpriv->fbs reference. */
3350 drm_framebuffer_remove(fb);
3352 mutex_unlock(&priv->fbs_lock);
3356 * drm_property_create - create a new property type
3358 * @flags: flags specifying the property type
3359 * @name: name of the property
3360 * @num_values: number of pre-defined values
3362 * This creates a new generic drm property which can then be attached to a drm
3363 * object with drm_object_attach_property. The returned property object must be
3364 * freed with drm_property_destroy.
3367 * A pointer to the newly created property on success, NULL on failure.
3369 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3370 const char *name, int num_values)
3372 struct drm_property *property = NULL;
3375 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3379 property->dev = dev;
3382 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3383 if (!property->values)
3387 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3391 property->flags = flags;
3392 property->num_values = num_values;
3393 INIT_LIST_HEAD(&property->enum_blob_list);
3396 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3397 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3400 list_add_tail(&property->head, &dev->mode_config.property_list);
3402 WARN_ON(!drm_property_type_valid(property));
3406 kfree(property->values);
3410 EXPORT_SYMBOL(drm_property_create);
3413 * drm_property_create_enum - create a new enumeration property type
3415 * @flags: flags specifying the property type
3416 * @name: name of the property
3417 * @props: enumeration lists with property values
3418 * @num_values: number of pre-defined values
3420 * This creates a new generic drm property which can then be attached to a drm
3421 * object with drm_object_attach_property. The returned property object must be
3422 * freed with drm_property_destroy.
3424 * Userspace is only allowed to set one of the predefined values for enumeration
3428 * A pointer to the newly created property on success, NULL on failure.
3430 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3432 const struct drm_prop_enum_list *props,
3435 struct drm_property *property;
3438 flags |= DRM_MODE_PROP_ENUM;
3440 property = drm_property_create(dev, flags, name, num_values);
3444 for (i = 0; i < num_values; i++) {
3445 ret = drm_property_add_enum(property, i,
3449 drm_property_destroy(dev, property);
3456 EXPORT_SYMBOL(drm_property_create_enum);
3459 * drm_property_create_bitmask - create a new bitmask property type
3461 * @flags: flags specifying the property type
3462 * @name: name of the property
3463 * @props: enumeration lists with property bitflags
3464 * @num_values: number of pre-defined values
3466 * This creates a new generic drm property which can then be attached to a drm
3467 * object with drm_object_attach_property. The returned property object must be
3468 * freed with drm_property_destroy.
3470 * Compared to plain enumeration properties userspace is allowed to set any
3471 * or'ed together combination of the predefined property bitflag values
3474 * A pointer to the newly created property on success, NULL on failure.
3476 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3477 int flags, const char *name,
3478 const struct drm_prop_enum_list *props,
3481 struct drm_property *property;
3484 flags |= DRM_MODE_PROP_BITMASK;
3486 property = drm_property_create(dev, flags, name, num_values);
3490 for (i = 0; i < num_values; i++) {
3491 ret = drm_property_add_enum(property, i,
3495 drm_property_destroy(dev, property);
3502 EXPORT_SYMBOL(drm_property_create_bitmask);
3504 static struct drm_property *property_create_range(struct drm_device *dev,
3505 int flags, const char *name,
3506 uint64_t min, uint64_t max)
3508 struct drm_property *property;
3510 property = drm_property_create(dev, flags, name, 2);
3514 property->values[0] = min;
3515 property->values[1] = max;
3521 * drm_property_create_range - create a new ranged property type
3523 * @flags: flags specifying the property type
3524 * @name: name of the property
3525 * @min: minimum value of the property
3526 * @max: maximum value of the property
3528 * This creates a new generic drm property which can then be attached to a drm
3529 * object with drm_object_attach_property. The returned property object must be
3530 * freed with drm_property_destroy.
3532 * Userspace is allowed to set any interger value in the (min, max) range
3536 * A pointer to the newly created property on success, NULL on failure.
3538 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3540 uint64_t min, uint64_t max)
3542 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3545 EXPORT_SYMBOL(drm_property_create_range);
3547 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3548 int flags, const char *name,
3549 int64_t min, int64_t max)
3551 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3552 name, I642U64(min), I642U64(max));
3554 EXPORT_SYMBOL(drm_property_create_signed_range);
3556 struct drm_property *drm_property_create_object(struct drm_device *dev,
3557 int flags, const char *name, uint32_t type)
3559 struct drm_property *property;
3561 flags |= DRM_MODE_PROP_OBJECT;
3563 property = drm_property_create(dev, flags, name, 1);
3567 property->values[0] = type;
3571 EXPORT_SYMBOL(drm_property_create_object);
3574 * drm_property_add_enum - add a possible value to an enumeration property
3575 * @property: enumeration property to change
3576 * @index: index of the new enumeration
3577 * @value: value of the new enumeration
3578 * @name: symbolic name of the new enumeration
3580 * This functions adds enumerations to a property.
3582 * It's use is deprecated, drivers should use one of the more specific helpers
3583 * to directly create the property with all enumerations already attached.
3586 * Zero on success, error code on failure.
3588 int drm_property_add_enum(struct drm_property *property, int index,
3589 uint64_t value, const char *name)
3591 struct drm_property_enum *prop_enum;
3593 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3594 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3598 * Bitmask enum properties have the additional constraint of values
3601 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3605 if (!list_empty(&property->enum_blob_list)) {
3606 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3607 if (prop_enum->value == value) {
3608 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3609 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3615 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3619 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3620 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3621 prop_enum->value = value;
3623 property->values[index] = value;
3624 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3627 EXPORT_SYMBOL(drm_property_add_enum);
3630 * drm_property_destroy - destroy a drm property
3632 * @property: property to destry
3634 * This function frees a property including any attached resources like
3635 * enumeration values.
3637 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3639 struct drm_property_enum *prop_enum, *pt;
3641 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3642 list_del(&prop_enum->head);
3646 if (property->num_values)
3647 kfree(property->values);
3648 drm_mode_object_put(dev, &property->base);
3649 list_del(&property->head);
3652 EXPORT_SYMBOL(drm_property_destroy);
3655 * drm_object_attach_property - attach a property to a modeset object
3656 * @obj: drm modeset object
3657 * @property: property to attach
3658 * @init_val: initial value of the property
3660 * This attaches the given property to the modeset object with the given initial
3661 * value. Currently this function cannot fail since the properties are stored in
3662 * a statically sized array.
3664 void drm_object_attach_property(struct drm_mode_object *obj,
3665 struct drm_property *property,
3668 int count = obj->properties->count;
3670 if (count == DRM_OBJECT_MAX_PROPERTY) {
3671 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3672 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3673 "you see this message on the same object type.\n",
3678 obj->properties->ids[count] = property->base.id;
3679 obj->properties->values[count] = init_val;
3680 obj->properties->count++;
3682 EXPORT_SYMBOL(drm_object_attach_property);
3685 * drm_object_property_set_value - set the value of a property
3686 * @obj: drm mode object to set property value for
3687 * @property: property to set
3688 * @val: value the property should be set to
3690 * This functions sets a given property on a given object. This function only
3691 * changes the software state of the property, it does not call into the
3692 * driver's ->set_property callback.
3695 * Zero on success, error code on failure.
3697 int drm_object_property_set_value(struct drm_mode_object *obj,
3698 struct drm_property *property, uint64_t val)
3702 for (i = 0; i < obj->properties->count; i++) {
3703 if (obj->properties->ids[i] == property->base.id) {
3704 obj->properties->values[i] = val;
3711 EXPORT_SYMBOL(drm_object_property_set_value);
3714 * drm_object_property_get_value - retrieve the value of a property
3715 * @obj: drm mode object to get property value from
3716 * @property: property to retrieve
3717 * @val: storage for the property value
3719 * This function retrieves the softare state of the given property for the given
3720 * property. Since there is no driver callback to retrieve the current property
3721 * value this might be out of sync with the hardware, depending upon the driver
3725 * Zero on success, error code on failure.
3727 int drm_object_property_get_value(struct drm_mode_object *obj,
3728 struct drm_property *property, uint64_t *val)
3732 for (i = 0; i < obj->properties->count; i++) {
3733 if (obj->properties->ids[i] == property->base.id) {
3734 *val = obj->properties->values[i];
3741 EXPORT_SYMBOL(drm_object_property_get_value);
3744 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3747 * @file_priv: DRM file info
3749 * This function retrieves the current value for an connectors's property.
3751 * Called by the user via ioctl.
3754 * Zero on success, errno on failure.
3756 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3757 void *data, struct drm_file *file_priv)
3759 struct drm_mode_get_property *out_resp = data;
3760 struct drm_property *property;
3763 int value_count = 0;
3766 struct drm_property_enum *prop_enum;
3767 struct drm_mode_property_enum __user *enum_ptr;
3768 struct drm_property_blob *prop_blob;
3769 uint32_t __user *blob_id_ptr;
3770 uint64_t __user *values_ptr;
3771 uint32_t __user *blob_length_ptr;
3773 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3776 drm_modeset_lock_all(dev);
3777 property = drm_property_find(dev, out_resp->prop_id);
3783 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3784 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3785 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3787 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3788 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3792 value_count = property->num_values;
3794 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3795 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3796 out_resp->flags = property->flags;
3798 if ((out_resp->count_values >= value_count) && value_count) {
3799 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3800 for (i = 0; i < value_count; i++) {
3801 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3807 out_resp->count_values = value_count;
3809 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3810 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3811 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3813 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3814 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3816 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3821 if (copy_to_user(&enum_ptr[copied].name,
3822 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3829 out_resp->count_enum_blobs = enum_count;
3832 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3833 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3835 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3836 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3838 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3839 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3844 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3852 out_resp->count_enum_blobs = blob_count;
3855 drm_modeset_unlock_all(dev);
3859 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3862 struct drm_property_blob *blob;
3865 if (!length || !data)
3868 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3872 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3878 blob->length = length;
3880 memcpy(blob->data, data, length);
3882 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3886 static void drm_property_destroy_blob(struct drm_device *dev,
3887 struct drm_property_blob *blob)
3889 drm_mode_object_put(dev, &blob->base);
3890 list_del(&blob->head);
3895 * drm_mode_getblob_ioctl - get the contents of a blob property value
3898 * @file_priv: DRM file info
3900 * This function retrieves the contents of a blob property. The value stored in
3901 * an object's blob property is just a normal modeset object id.
3903 * Called by the user via ioctl.
3906 * Zero on success, errno on failure.
3908 int drm_mode_getblob_ioctl(struct drm_device *dev,
3909 void *data, struct drm_file *file_priv)
3911 struct drm_mode_get_blob *out_resp = data;
3912 struct drm_property_blob *blob;
3914 void __user *blob_ptr;
3916 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3919 drm_modeset_lock_all(dev);
3920 blob = drm_property_blob_find(dev, out_resp->blob_id);
3926 if (out_resp->length == blob->length) {
3927 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3928 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3933 out_resp->length = blob->length;
3936 drm_modeset_unlock_all(dev);
3940 int drm_mode_connector_set_path_property(struct drm_connector *connector,
3943 struct drm_device *dev = connector->dev;
3945 size = strlen(path) + 1;
3947 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
3949 if (!connector->path_blob_ptr)
3952 ret = drm_object_property_set_value(&connector->base,
3953 dev->mode_config.path_property,
3954 connector->path_blob_ptr->base.id);
3957 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
3960 * drm_mode_connector_update_edid_property - update the edid property of a connector
3961 * @connector: drm connector
3962 * @edid: new value of the edid property
3964 * This function creates a new blob modeset object and assigns its id to the
3965 * connector's edid property.
3968 * Zero on success, errno on failure.
3970 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3973 struct drm_device *dev = connector->dev;
3976 /* ignore requests to set edid when overridden */
3977 if (connector->override_edid)
3980 if (connector->edid_blob_ptr)
3981 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3983 /* Delete edid, when there is none. */
3985 connector->edid_blob_ptr = NULL;
3986 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3990 size = EDID_LENGTH * (1 + edid->extensions);
3991 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3993 if (!connector->edid_blob_ptr)
3996 ret = drm_object_property_set_value(&connector->base,
3997 dev->mode_config.edid_property,
3998 connector->edid_blob_ptr->base.id);
4002 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4004 static bool drm_property_change_is_valid(struct drm_property *property,
4007 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4010 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4011 if (value < property->values[0] || value > property->values[1])
4014 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4015 int64_t svalue = U642I64(value);
4016 if (svalue < U642I64(property->values[0]) ||
4017 svalue > U642I64(property->values[1]))
4020 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4022 uint64_t valid_mask = 0;
4023 for (i = 0; i < property->num_values; i++)
4024 valid_mask |= (1ULL << property->values[i]);
4025 return !(value & ~valid_mask);
4026 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4027 /* Only the driver knows */
4029 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4030 struct drm_mode_object *obj;
4031 /* a zero value for an object property translates to null: */
4035 * NOTE: use _object_find() directly to bypass restriction on
4036 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4037 * object this could race against object finalization, so it
4038 * simply tells us that the object *was* valid. Which is good
4041 obj = _object_find(property->dev, value, property->values[0]);
4045 for (i = 0; i < property->num_values; i++)
4046 if (property->values[i] == value)
4053 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4056 * @file_priv: DRM file info
4058 * This function sets the current value for a connectors's property. It also
4059 * calls into a driver's ->set_property callback to update the hardware state
4061 * Called by the user via ioctl.
4064 * Zero on success, errno on failure.
4066 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4067 void *data, struct drm_file *file_priv)
4069 struct drm_mode_connector_set_property *conn_set_prop = data;
4070 struct drm_mode_obj_set_property obj_set_prop = {
4071 .value = conn_set_prop->value,
4072 .prop_id = conn_set_prop->prop_id,
4073 .obj_id = conn_set_prop->connector_id,
4074 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4077 /* It does all the locking and checking we need */
4078 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4081 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4082 struct drm_property *property,
4086 struct drm_connector *connector = obj_to_connector(obj);
4088 /* Do DPMS ourselves */
4089 if (property == connector->dev->mode_config.dpms_property) {
4090 if (connector->funcs->dpms)
4091 (*connector->funcs->dpms)(connector, (int)value);
4093 } else if (connector->funcs->set_property)
4094 ret = connector->funcs->set_property(connector, property, value);
4096 /* store the property value if successful */
4098 drm_object_property_set_value(&connector->base, property, value);
4102 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4103 struct drm_property *property,
4107 struct drm_crtc *crtc = obj_to_crtc(obj);
4109 if (crtc->funcs->set_property)
4110 ret = crtc->funcs->set_property(crtc, property, value);
4112 drm_object_property_set_value(obj, property, value);
4117 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
4118 struct drm_property *property,
4122 struct drm_plane *plane = obj_to_plane(obj);
4124 if (plane->funcs->set_property)
4125 ret = plane->funcs->set_property(plane, property, value);
4127 drm_object_property_set_value(obj, property, value);
4133 * drm_mode_getproperty_ioctl - get the current value of a object's property
4136 * @file_priv: DRM file info
4138 * This function retrieves the current value for an object's property. Compared
4139 * to the connector specific ioctl this one is extended to also work on crtc and
4142 * Called by the user via ioctl.
4145 * Zero on success, errno on failure.
4147 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4148 struct drm_file *file_priv)
4150 struct drm_mode_obj_get_properties *arg = data;
4151 struct drm_mode_object *obj;
4155 int props_count = 0;
4156 uint32_t __user *props_ptr;
4157 uint64_t __user *prop_values_ptr;
4159 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4162 drm_modeset_lock_all(dev);
4164 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4169 if (!obj->properties) {
4174 props_count = obj->properties->count;
4176 /* This ioctl is called twice, once to determine how much space is
4177 * needed, and the 2nd time to fill it. */
4178 if ((arg->count_props >= props_count) && props_count) {
4180 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4181 prop_values_ptr = (uint64_t __user *)(unsigned long)
4182 (arg->prop_values_ptr);
4183 for (i = 0; i < props_count; i++) {
4184 if (put_user(obj->properties->ids[i],
4185 props_ptr + copied)) {
4189 if (put_user(obj->properties->values[i],
4190 prop_values_ptr + copied)) {
4197 arg->count_props = props_count;
4199 drm_modeset_unlock_all(dev);
4204 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4207 * @file_priv: DRM file info
4209 * This function sets the current value for an object's property. It also calls
4210 * into a driver's ->set_property callback to update the hardware state.
4211 * Compared to the connector specific ioctl this one is extended to also work on
4212 * crtc and plane objects.
4214 * Called by the user via ioctl.
4217 * Zero on success, errno on failure.
4219 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4220 struct drm_file *file_priv)
4222 struct drm_mode_obj_set_property *arg = data;
4223 struct drm_mode_object *arg_obj;
4224 struct drm_mode_object *prop_obj;
4225 struct drm_property *property;
4229 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4232 drm_modeset_lock_all(dev);
4234 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4239 if (!arg_obj->properties)
4242 for (i = 0; i < arg_obj->properties->count; i++)
4243 if (arg_obj->properties->ids[i] == arg->prop_id)
4246 if (i == arg_obj->properties->count)
4249 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4250 DRM_MODE_OBJECT_PROPERTY);
4255 property = obj_to_property(prop_obj);
4257 if (!drm_property_change_is_valid(property, arg->value))
4260 switch (arg_obj->type) {
4261 case DRM_MODE_OBJECT_CONNECTOR:
4262 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4265 case DRM_MODE_OBJECT_CRTC:
4266 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4268 case DRM_MODE_OBJECT_PLANE:
4269 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
4274 drm_modeset_unlock_all(dev);
4279 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4280 * @connector: connector to attach
4281 * @encoder: encoder to attach @connector to
4283 * This function links up a connector to an encoder. Note that the routing
4284 * restrictions between encoders and crtcs are exposed to userspace through the
4285 * possible_clones and possible_crtcs bitmasks.
4288 * Zero on success, errno on failure.
4290 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4291 struct drm_encoder *encoder)
4295 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4296 if (connector->encoder_ids[i] == 0) {
4297 connector->encoder_ids[i] = encoder->base.id;
4303 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4306 * drm_mode_crtc_set_gamma_size - set the gamma table size
4307 * @crtc: CRTC to set the gamma table size for
4308 * @gamma_size: size of the gamma table
4310 * Drivers which support gamma tables should set this to the supported gamma
4311 * table size when initializing the CRTC. Currently the drm core only supports a
4312 * fixed gamma table size.
4315 * Zero on success, errno on failure.
4317 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4320 crtc->gamma_size = gamma_size;
4322 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4323 if (!crtc->gamma_store) {
4324 crtc->gamma_size = 0;
4330 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4333 * drm_mode_gamma_set_ioctl - set the gamma table
4336 * @file_priv: DRM file info
4338 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4339 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4341 * Called by the user via ioctl.
4344 * Zero on success, errno on failure.
4346 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4347 void *data, struct drm_file *file_priv)
4349 struct drm_mode_crtc_lut *crtc_lut = data;
4350 struct drm_crtc *crtc;
4351 void *r_base, *g_base, *b_base;
4355 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4358 drm_modeset_lock_all(dev);
4359 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4365 if (crtc->funcs->gamma_set == NULL) {
4370 /* memcpy into gamma store */
4371 if (crtc_lut->gamma_size != crtc->gamma_size) {
4376 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4377 r_base = crtc->gamma_store;
4378 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4383 g_base = r_base + size;
4384 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4389 b_base = g_base + size;
4390 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4395 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4398 drm_modeset_unlock_all(dev);
4404 * drm_mode_gamma_get_ioctl - get the gamma table
4407 * @file_priv: DRM file info
4409 * Copy the current gamma table into the storage provided. This also provides
4410 * the gamma table size the driver expects, which can be used to size the
4411 * allocated storage.
4413 * Called by the user via ioctl.
4416 * Zero on success, errno on failure.
4418 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4419 void *data, struct drm_file *file_priv)
4421 struct drm_mode_crtc_lut *crtc_lut = data;
4422 struct drm_crtc *crtc;
4423 void *r_base, *g_base, *b_base;
4427 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4430 drm_modeset_lock_all(dev);
4431 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4437 /* memcpy into gamma store */
4438 if (crtc_lut->gamma_size != crtc->gamma_size) {
4443 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4444 r_base = crtc->gamma_store;
4445 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4450 g_base = r_base + size;
4451 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4456 b_base = g_base + size;
4457 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4462 drm_modeset_unlock_all(dev);
4467 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4470 * @file_priv: DRM file info
4472 * This schedules an asynchronous update on a given CRTC, called page flip.
4473 * Optionally a drm event is generated to signal the completion of the event.
4474 * Generic drivers cannot assume that a pageflip with changed framebuffer
4475 * properties (including driver specific metadata like tiling layout) will work,
4476 * but some drivers support e.g. pixel format changes through the pageflip
4479 * Called by the user via ioctl.
4482 * Zero on success, errno on failure.
4484 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4485 void *data, struct drm_file *file_priv)
4487 struct drm_mode_crtc_page_flip *page_flip = data;
4488 struct drm_crtc *crtc;
4489 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4490 struct drm_pending_vblank_event *e = NULL;
4491 unsigned long flags;
4494 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4495 page_flip->reserved != 0)
4498 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4501 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4505 drm_modeset_lock(&crtc->mutex, NULL);
4506 if (crtc->primary->fb == NULL) {
4507 /* The framebuffer is currently unbound, presumably
4508 * due to a hotplug event, that userspace has not
4515 if (crtc->funcs->page_flip == NULL)
4518 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4524 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4528 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4529 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4534 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4536 spin_lock_irqsave(&dev->event_lock, flags);
4537 if (file_priv->event_space < sizeof e->event) {
4538 spin_unlock_irqrestore(&dev->event_lock, flags);
4541 file_priv->event_space -= sizeof e->event;
4542 spin_unlock_irqrestore(&dev->event_lock, flags);
4544 e = kzalloc(sizeof *e, GFP_KERNEL);
4546 spin_lock_irqsave(&dev->event_lock, flags);
4547 file_priv->event_space += sizeof e->event;
4548 spin_unlock_irqrestore(&dev->event_lock, flags);
4552 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4553 e->event.base.length = sizeof e->event;
4554 e->event.user_data = page_flip->user_data;
4555 e->base.event = &e->event.base;
4556 e->base.file_priv = file_priv;
4558 (void (*) (struct drm_pending_event *)) kfree;
4561 old_fb = crtc->primary->fb;
4562 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4564 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4565 spin_lock_irqsave(&dev->event_lock, flags);
4566 file_priv->event_space += sizeof e->event;
4567 spin_unlock_irqrestore(&dev->event_lock, flags);
4570 /* Keep the old fb, don't unref it. */
4574 * Warn if the driver hasn't properly updated the crtc->fb
4575 * field to reflect that the new framebuffer is now used.
4576 * Failing to do so will screw with the reference counting
4579 WARN_ON(crtc->primary->fb != fb);
4580 /* Unref only the old framebuffer. */
4586 drm_framebuffer_unreference(fb);
4588 drm_framebuffer_unreference(old_fb);
4589 drm_modeset_unlock(&crtc->mutex);
4595 * drm_mode_config_reset - call ->reset callbacks
4598 * This functions calls all the crtc's, encoder's and connector's ->reset
4599 * callback. Drivers can use this in e.g. their driver load or resume code to
4600 * reset hardware and software state.
4602 void drm_mode_config_reset(struct drm_device *dev)
4604 struct drm_crtc *crtc;
4605 struct drm_encoder *encoder;
4606 struct drm_connector *connector;
4608 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4609 if (crtc->funcs->reset)
4610 crtc->funcs->reset(crtc);
4612 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4613 if (encoder->funcs->reset)
4614 encoder->funcs->reset(encoder);
4616 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4617 connector->status = connector_status_unknown;
4619 if (connector->funcs->reset)
4620 connector->funcs->reset(connector);
4623 EXPORT_SYMBOL(drm_mode_config_reset);
4626 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4629 * @file_priv: DRM file info
4631 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4632 * TTM or something else entirely) and returns the resulting buffer handle. This
4633 * handle can then be wrapped up into a framebuffer modeset object.
4635 * Note that userspace is not allowed to use such objects for render
4636 * acceleration - drivers must create their own private ioctls for such a use
4639 * Called by the user via ioctl.
4642 * Zero on success, errno on failure.
4644 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4645 void *data, struct drm_file *file_priv)
4647 struct drm_mode_create_dumb *args = data;
4648 u32 cpp, stride, size;
4650 if (!dev->driver->dumb_create)
4652 if (!args->width || !args->height || !args->bpp)
4655 /* overflow checks for 32bit size calculations */
4656 cpp = DIV_ROUND_UP(args->bpp, 8);
4657 if (cpp > 0xffffffffU / args->width)
4659 stride = cpp * args->width;
4660 if (args->height > 0xffffffffU / stride)
4663 /* test for wrap-around */
4664 size = args->height * stride;
4665 if (PAGE_ALIGN(size) == 0)
4668 return dev->driver->dumb_create(file_priv, dev, args);
4672 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4675 * @file_priv: DRM file info
4677 * Allocate an offset in the drm device node's address space to be able to
4678 * memory map a dumb buffer.
4680 * Called by the user via ioctl.
4683 * Zero on success, errno on failure.
4685 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4686 void *data, struct drm_file *file_priv)
4688 struct drm_mode_map_dumb *args = data;
4690 /* call driver ioctl to get mmap offset */
4691 if (!dev->driver->dumb_map_offset)
4694 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4698 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4701 * @file_priv: DRM file info
4703 * This destroys the userspace handle for the given dumb backing storage buffer.
4704 * Since buffer objects must be reference counted in the kernel a buffer object
4705 * won't be immediately freed if a framebuffer modeset object still uses it.
4707 * Called by the user via ioctl.
4710 * Zero on success, errno on failure.
4712 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4713 void *data, struct drm_file *file_priv)
4715 struct drm_mode_destroy_dumb *args = data;
4717 if (!dev->driver->dumb_destroy)
4720 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4724 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4725 * @format: pixel format (DRM_FORMAT_*)
4726 * @depth: storage for the depth value
4727 * @bpp: storage for the bpp value
4729 * This only supports RGB formats here for compat with code that doesn't use
4730 * pixel formats directly yet.
4732 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4737 case DRM_FORMAT_RGB332:
4738 case DRM_FORMAT_BGR233:
4742 case DRM_FORMAT_XRGB1555:
4743 case DRM_FORMAT_XBGR1555:
4744 case DRM_FORMAT_RGBX5551:
4745 case DRM_FORMAT_BGRX5551:
4746 case DRM_FORMAT_ARGB1555:
4747 case DRM_FORMAT_ABGR1555:
4748 case DRM_FORMAT_RGBA5551:
4749 case DRM_FORMAT_BGRA5551:
4753 case DRM_FORMAT_RGB565:
4754 case DRM_FORMAT_BGR565:
4758 case DRM_FORMAT_RGB888:
4759 case DRM_FORMAT_BGR888:
4763 case DRM_FORMAT_XRGB8888:
4764 case DRM_FORMAT_XBGR8888:
4765 case DRM_FORMAT_RGBX8888:
4766 case DRM_FORMAT_BGRX8888:
4770 case DRM_FORMAT_XRGB2101010:
4771 case DRM_FORMAT_XBGR2101010:
4772 case DRM_FORMAT_RGBX1010102:
4773 case DRM_FORMAT_BGRX1010102:
4774 case DRM_FORMAT_ARGB2101010:
4775 case DRM_FORMAT_ABGR2101010:
4776 case DRM_FORMAT_RGBA1010102:
4777 case DRM_FORMAT_BGRA1010102:
4781 case DRM_FORMAT_ARGB8888:
4782 case DRM_FORMAT_ABGR8888:
4783 case DRM_FORMAT_RGBA8888:
4784 case DRM_FORMAT_BGRA8888:
4789 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4790 drm_get_format_name(format));
4796 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4799 * drm_format_num_planes - get the number of planes for format
4800 * @format: pixel format (DRM_FORMAT_*)
4803 * The number of planes used by the specified pixel format.
4805 int drm_format_num_planes(uint32_t format)
4808 case DRM_FORMAT_YUV410:
4809 case DRM_FORMAT_YVU410:
4810 case DRM_FORMAT_YUV411:
4811 case DRM_FORMAT_YVU411:
4812 case DRM_FORMAT_YUV420:
4813 case DRM_FORMAT_YVU420:
4814 case DRM_FORMAT_YUV422:
4815 case DRM_FORMAT_YVU422:
4816 case DRM_FORMAT_YUV444:
4817 case DRM_FORMAT_YVU444:
4819 case DRM_FORMAT_NV12:
4820 case DRM_FORMAT_NV21:
4821 case DRM_FORMAT_NV16:
4822 case DRM_FORMAT_NV61:
4823 case DRM_FORMAT_NV24:
4824 case DRM_FORMAT_NV42:
4830 EXPORT_SYMBOL(drm_format_num_planes);
4833 * drm_format_plane_cpp - determine the bytes per pixel value
4834 * @format: pixel format (DRM_FORMAT_*)
4835 * @plane: plane index
4838 * The bytes per pixel value for the specified plane.
4840 int drm_format_plane_cpp(uint32_t format, int plane)
4845 if (plane >= drm_format_num_planes(format))
4849 case DRM_FORMAT_YUYV:
4850 case DRM_FORMAT_YVYU:
4851 case DRM_FORMAT_UYVY:
4852 case DRM_FORMAT_VYUY:
4854 case DRM_FORMAT_NV12:
4855 case DRM_FORMAT_NV21:
4856 case DRM_FORMAT_NV16:
4857 case DRM_FORMAT_NV61:
4858 case DRM_FORMAT_NV24:
4859 case DRM_FORMAT_NV42:
4860 return plane ? 2 : 1;
4861 case DRM_FORMAT_YUV410:
4862 case DRM_FORMAT_YVU410:
4863 case DRM_FORMAT_YUV411:
4864 case DRM_FORMAT_YVU411:
4865 case DRM_FORMAT_YUV420:
4866 case DRM_FORMAT_YVU420:
4867 case DRM_FORMAT_YUV422:
4868 case DRM_FORMAT_YVU422:
4869 case DRM_FORMAT_YUV444:
4870 case DRM_FORMAT_YVU444:
4873 drm_fb_get_bpp_depth(format, &depth, &bpp);
4877 EXPORT_SYMBOL(drm_format_plane_cpp);
4880 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4881 * @format: pixel format (DRM_FORMAT_*)
4884 * The horizontal chroma subsampling factor for the
4885 * specified pixel format.
4887 int drm_format_horz_chroma_subsampling(uint32_t format)
4890 case DRM_FORMAT_YUV411:
4891 case DRM_FORMAT_YVU411:
4892 case DRM_FORMAT_YUV410:
4893 case DRM_FORMAT_YVU410:
4895 case DRM_FORMAT_YUYV:
4896 case DRM_FORMAT_YVYU:
4897 case DRM_FORMAT_UYVY:
4898 case DRM_FORMAT_VYUY:
4899 case DRM_FORMAT_NV12:
4900 case DRM_FORMAT_NV21:
4901 case DRM_FORMAT_NV16:
4902 case DRM_FORMAT_NV61:
4903 case DRM_FORMAT_YUV422:
4904 case DRM_FORMAT_YVU422:
4905 case DRM_FORMAT_YUV420:
4906 case DRM_FORMAT_YVU420:
4912 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4915 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4916 * @format: pixel format (DRM_FORMAT_*)
4919 * The vertical chroma subsampling factor for the
4920 * specified pixel format.
4922 int drm_format_vert_chroma_subsampling(uint32_t format)
4925 case DRM_FORMAT_YUV410:
4926 case DRM_FORMAT_YVU410:
4928 case DRM_FORMAT_YUV420:
4929 case DRM_FORMAT_YVU420:
4930 case DRM_FORMAT_NV12:
4931 case DRM_FORMAT_NV21:
4937 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4940 * drm_mode_config_init - initialize DRM mode_configuration structure
4943 * Initialize @dev's mode_config structure, used for tracking the graphics
4944 * configuration of @dev.
4946 * Since this initializes the modeset locks, no locking is possible. Which is no
4947 * problem, since this should happen single threaded at init time. It is the
4948 * driver's problem to ensure this guarantee.
4951 void drm_mode_config_init(struct drm_device *dev)
4953 mutex_init(&dev->mode_config.mutex);
4954 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
4955 mutex_init(&dev->mode_config.idr_mutex);
4956 mutex_init(&dev->mode_config.fb_lock);
4957 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4958 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4959 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4960 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4961 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4962 INIT_LIST_HEAD(&dev->mode_config.property_list);
4963 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4964 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4965 idr_init(&dev->mode_config.crtc_idr);
4967 drm_modeset_lock_all(dev);
4968 drm_mode_create_standard_connector_properties(dev);
4969 drm_mode_create_standard_plane_properties(dev);
4970 drm_modeset_unlock_all(dev);
4972 /* Just to be sure */
4973 dev->mode_config.num_fb = 0;
4974 dev->mode_config.num_connector = 0;
4975 dev->mode_config.num_crtc = 0;
4976 dev->mode_config.num_encoder = 0;
4977 dev->mode_config.num_overlay_plane = 0;
4978 dev->mode_config.num_total_plane = 0;
4980 EXPORT_SYMBOL(drm_mode_config_init);
4983 * drm_mode_config_cleanup - free up DRM mode_config info
4986 * Free up all the connectors and CRTCs associated with this DRM device, then
4987 * free up the framebuffers and associated buffer objects.
4989 * Note that since this /should/ happen single-threaded at driver/device
4990 * teardown time, no locking is required. It's the driver's job to ensure that
4991 * this guarantee actually holds true.
4993 * FIXME: cleanup any dangling user buffer objects too
4995 void drm_mode_config_cleanup(struct drm_device *dev)
4997 struct drm_connector *connector, *ot;
4998 struct drm_crtc *crtc, *ct;
4999 struct drm_encoder *encoder, *enct;
5000 struct drm_bridge *bridge, *brt;
5001 struct drm_framebuffer *fb, *fbt;
5002 struct drm_property *property, *pt;
5003 struct drm_property_blob *blob, *bt;
5004 struct drm_plane *plane, *plt;
5006 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5008 encoder->funcs->destroy(encoder);
5011 list_for_each_entry_safe(bridge, brt,
5012 &dev->mode_config.bridge_list, head) {
5013 bridge->funcs->destroy(bridge);
5016 list_for_each_entry_safe(connector, ot,
5017 &dev->mode_config.connector_list, head) {
5018 connector->funcs->destroy(connector);
5021 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5023 drm_property_destroy(dev, property);
5026 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5028 drm_property_destroy_blob(dev, blob);
5032 * Single-threaded teardown context, so it's not required to grab the
5033 * fb_lock to protect against concurrent fb_list access. Contrary, it
5034 * would actually deadlock with the drm_framebuffer_cleanup function.
5036 * Also, if there are any framebuffers left, that's a driver leak now,
5037 * so politely WARN about this.
5039 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5040 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5041 drm_framebuffer_remove(fb);
5044 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5046 plane->funcs->destroy(plane);
5049 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5050 crtc->funcs->destroy(crtc);
5053 idr_destroy(&dev->mode_config.crtc_idr);
5054 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5056 EXPORT_SYMBOL(drm_mode_config_cleanup);