2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
41 #include "drm_crtc_internal.h"
44 * drm_modeset_lock_all - take all modeset locks
47 * This function takes all modeset locks, suitable where a more fine-grained
48 * scheme isn't (yet) implemented. Locks must be dropped with
49 * drm_modeset_unlock_all.
51 void drm_modeset_lock_all(struct drm_device *dev)
53 struct drm_crtc *crtc;
55 mutex_lock(&dev->mode_config.mutex);
57 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
58 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
60 EXPORT_SYMBOL(drm_modeset_lock_all);
63 * drm_modeset_unlock_all - drop all modeset locks
66 * This function drop all modeset locks taken by drm_modeset_lock_all.
68 void drm_modeset_unlock_all(struct drm_device *dev)
70 struct drm_crtc *crtc;
72 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
73 mutex_unlock(&crtc->mutex);
75 mutex_unlock(&dev->mode_config.mutex);
77 EXPORT_SYMBOL(drm_modeset_unlock_all);
80 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
83 * Useful as a debug assert.
85 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
87 struct drm_crtc *crtc;
89 /* Locking is currently fubar in the panic handler. */
93 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
94 WARN_ON(!mutex_is_locked(&crtc->mutex));
96 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
98 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
100 /* Avoid boilerplate. I'm tired of typing. */
101 #define DRM_ENUM_NAME_FN(fnname, list) \
102 const char *fnname(int val) \
105 for (i = 0; i < ARRAY_SIZE(list); i++) { \
106 if (list[i].type == val) \
107 return list[i].name; \
109 return "(unknown)"; \
115 static const struct drm_prop_enum_list drm_dpms_enum_list[] =
116 { { DRM_MODE_DPMS_ON, "On" },
117 { DRM_MODE_DPMS_STANDBY, "Standby" },
118 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
119 { DRM_MODE_DPMS_OFF, "Off" }
122 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
124 static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
126 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
127 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
128 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
132 * Optional properties
134 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
136 { DRM_MODE_SCALE_NONE, "None" },
137 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
138 { DRM_MODE_SCALE_CENTER, "Center" },
139 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
143 * Non-global properties, but "required" for certain connectors.
145 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
147 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
148 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
149 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
152 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
154 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
156 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
157 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
158 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
161 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
162 drm_dvi_i_subconnector_enum_list)
164 static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
166 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
167 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
169 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
170 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
173 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
175 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
177 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
178 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
179 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
180 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
181 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
184 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
185 drm_tv_subconnector_enum_list)
187 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
188 { DRM_MODE_DIRTY_OFF, "Off" },
189 { DRM_MODE_DIRTY_ON, "On" },
190 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
193 struct drm_conn_prop_enum_list {
200 * Connector and encoder types.
202 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
203 { { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
204 { DRM_MODE_CONNECTOR_VGA, "VGA" },
205 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
206 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
207 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
208 { DRM_MODE_CONNECTOR_Composite, "Composite" },
209 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
210 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
211 { DRM_MODE_CONNECTOR_Component, "Component" },
212 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
213 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
214 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
215 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
216 { DRM_MODE_CONNECTOR_TV, "TV" },
217 { DRM_MODE_CONNECTOR_eDP, "eDP" },
218 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
219 { DRM_MODE_CONNECTOR_DSI, "DSI" },
222 static const struct drm_prop_enum_list drm_encoder_enum_list[] =
223 { { DRM_MODE_ENCODER_NONE, "None" },
224 { DRM_MODE_ENCODER_DAC, "DAC" },
225 { DRM_MODE_ENCODER_TMDS, "TMDS" },
226 { DRM_MODE_ENCODER_LVDS, "LVDS" },
227 { DRM_MODE_ENCODER_TVDAC, "TV" },
228 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
229 { DRM_MODE_ENCODER_DSI, "DSI" },
230 { DRM_MODE_ENCODER_DPMST, "DP MST" },
233 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
235 { SubPixelUnknown, "Unknown" },
236 { SubPixelHorizontalRGB, "Horizontal RGB" },
237 { SubPixelHorizontalBGR, "Horizontal BGR" },
238 { SubPixelVerticalRGB, "Vertical RGB" },
239 { SubPixelVerticalBGR, "Vertical BGR" },
240 { SubPixelNone, "None" },
243 void drm_connector_ida_init(void)
247 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
248 ida_init(&drm_connector_enum_list[i].ida);
251 void drm_connector_ida_destroy(void)
255 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
256 ida_destroy(&drm_connector_enum_list[i].ida);
260 * drm_get_connector_status_name - return a string for connector status
261 * @status: connector status to compute name of
263 * In contrast to the other drm_get_*_name functions this one here returns a
264 * const pointer and hence is threadsafe.
266 const char *drm_get_connector_status_name(enum drm_connector_status status)
268 if (status == connector_status_connected)
270 else if (status == connector_status_disconnected)
271 return "disconnected";
275 EXPORT_SYMBOL(drm_get_connector_status_name);
278 * drm_get_subpixel_order_name - return a string for a given subpixel enum
279 * @order: enum of subpixel_order
281 * Note you could abuse this and return something out of bounds, but that
282 * would be a caller error. No unscrubbed user data should make it here.
284 const char *drm_get_subpixel_order_name(enum subpixel_order order)
286 return drm_subpixel_enum_list[order].name;
288 EXPORT_SYMBOL(drm_get_subpixel_order_name);
290 static char printable_char(int c)
292 return isascii(c) && isprint(c) ? c : '?';
296 * drm_get_format_name - return a string for drm fourcc format
297 * @format: format to compute name of
299 * Note that the buffer used by this function is globally shared and owned by
300 * the function itself.
302 * FIXME: This isn't really multithreading safe.
304 const char *drm_get_format_name(uint32_t format)
308 snprintf(buf, sizeof(buf),
309 "%c%c%c%c %s-endian (0x%08x)",
310 printable_char(format & 0xff),
311 printable_char((format >> 8) & 0xff),
312 printable_char((format >> 16) & 0xff),
313 printable_char((format >> 24) & 0x7f),
314 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
319 EXPORT_SYMBOL(drm_get_format_name);
322 * drm_mode_object_get - allocate a new modeset identifier
324 * @obj: object pointer, used to generate unique ID
325 * @obj_type: object type
327 * Create a unique identifier based on @ptr in @dev's identifier space. Used
328 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
329 * modeset identifiers are _not_ reference counted. Hence don't use this for
330 * reference counted modeset objects like framebuffers.
333 * New unique (relative to other objects in @dev) integer identifier for the
336 int drm_mode_object_get(struct drm_device *dev,
337 struct drm_mode_object *obj, uint32_t obj_type)
341 mutex_lock(&dev->mode_config.idr_mutex);
342 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
345 * Set up the object linking under the protection of the idr
346 * lock so that other users can't see inconsistent state.
349 obj->type = obj_type;
351 mutex_unlock(&dev->mode_config.idr_mutex);
353 return ret < 0 ? ret : 0;
357 * drm_mode_object_put - free a modeset identifer
359 * @object: object to free
361 * Free @id from @dev's unique identifier pool. Note that despite the _get
362 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
363 * for reference counted modeset objects like framebuffers.
365 void drm_mode_object_put(struct drm_device *dev,
366 struct drm_mode_object *object)
368 mutex_lock(&dev->mode_config.idr_mutex);
369 idr_remove(&dev->mode_config.crtc_idr, object->id);
370 mutex_unlock(&dev->mode_config.idr_mutex);
373 static struct drm_mode_object *_object_find(struct drm_device *dev,
374 uint32_t id, uint32_t type)
376 struct drm_mode_object *obj = NULL;
378 mutex_lock(&dev->mode_config.idr_mutex);
379 obj = idr_find(&dev->mode_config.crtc_idr, id);
380 if (!obj || (type != DRM_MODE_OBJECT_ANY && obj->type != type) ||
383 mutex_unlock(&dev->mode_config.idr_mutex);
389 * drm_mode_object_find - look up a drm object with static lifetime
391 * @id: id of the mode object
392 * @type: type of the mode object
394 * Note that framebuffers cannot be looked up with this functions - since those
395 * are reference counted, they need special treatment. Even with
396 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
397 * rather than WARN_ON()).
399 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
400 uint32_t id, uint32_t type)
402 struct drm_mode_object *obj = NULL;
404 /* Framebuffers are reference counted and need their own lookup
406 WARN_ON(type == DRM_MODE_OBJECT_FB);
407 obj = _object_find(dev, id, type);
408 /* don't leak out unref'd fb's */
409 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
413 EXPORT_SYMBOL(drm_mode_object_find);
416 * drm_framebuffer_init - initialize a framebuffer
418 * @fb: framebuffer to be initialized
419 * @funcs: ... with these functions
421 * Allocates an ID for the framebuffer's parent mode object, sets its mode
422 * functions & device file and adds it to the master fd list.
425 * This functions publishes the fb and makes it available for concurrent access
426 * by other users. Which means by this point the fb _must_ be fully set up -
427 * since all the fb attributes are invariant over its lifetime, no further
428 * locking but only correct reference counting is required.
431 * Zero on success, error code on failure.
433 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
434 const struct drm_framebuffer_funcs *funcs)
438 mutex_lock(&dev->mode_config.fb_lock);
439 kref_init(&fb->refcount);
440 INIT_LIST_HEAD(&fb->filp_head);
444 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
448 /* Grab the idr reference. */
449 drm_framebuffer_reference(fb);
451 dev->mode_config.num_fb++;
452 list_add(&fb->head, &dev->mode_config.fb_list);
454 mutex_unlock(&dev->mode_config.fb_lock);
458 EXPORT_SYMBOL(drm_framebuffer_init);
460 static void drm_framebuffer_free(struct kref *kref)
462 struct drm_framebuffer *fb =
463 container_of(kref, struct drm_framebuffer, refcount);
464 fb->funcs->destroy(fb);
467 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
470 struct drm_mode_object *obj = NULL;
471 struct drm_framebuffer *fb;
473 mutex_lock(&dev->mode_config.idr_mutex);
474 obj = idr_find(&dev->mode_config.crtc_idr, id);
475 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
479 mutex_unlock(&dev->mode_config.idr_mutex);
485 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
487 * @id: id of the fb object
489 * If successful, this grabs an additional reference to the framebuffer -
490 * callers need to make sure to eventually unreference the returned framebuffer
491 * again, using @drm_framebuffer_unreference.
493 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
496 struct drm_framebuffer *fb;
498 mutex_lock(&dev->mode_config.fb_lock);
499 fb = __drm_framebuffer_lookup(dev, id);
501 drm_framebuffer_reference(fb);
502 mutex_unlock(&dev->mode_config.fb_lock);
506 EXPORT_SYMBOL(drm_framebuffer_lookup);
509 * drm_framebuffer_unreference - unref a framebuffer
510 * @fb: framebuffer to unref
512 * This functions decrements the fb's refcount and frees it if it drops to zero.
514 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
516 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
517 kref_put(&fb->refcount, drm_framebuffer_free);
519 EXPORT_SYMBOL(drm_framebuffer_unreference);
522 * drm_framebuffer_reference - incr the fb refcnt
525 * This functions increments the fb's refcount.
527 void drm_framebuffer_reference(struct drm_framebuffer *fb)
529 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
530 kref_get(&fb->refcount);
532 EXPORT_SYMBOL(drm_framebuffer_reference);
534 static void drm_framebuffer_free_bug(struct kref *kref)
539 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
541 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
542 kref_put(&fb->refcount, drm_framebuffer_free_bug);
545 /* dev->mode_config.fb_lock must be held! */
546 static void __drm_framebuffer_unregister(struct drm_device *dev,
547 struct drm_framebuffer *fb)
549 mutex_lock(&dev->mode_config.idr_mutex);
550 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
551 mutex_unlock(&dev->mode_config.idr_mutex);
555 __drm_framebuffer_unreference(fb);
559 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
560 * @fb: fb to unregister
562 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
563 * those used for fbdev. Note that the caller must hold a reference of it's own,
564 * i.e. the object may not be destroyed through this call (since it'll lead to a
565 * locking inversion).
567 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
569 struct drm_device *dev = fb->dev;
571 mutex_lock(&dev->mode_config.fb_lock);
572 /* Mark fb as reaped and drop idr ref. */
573 __drm_framebuffer_unregister(dev, fb);
574 mutex_unlock(&dev->mode_config.fb_lock);
576 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
579 * drm_framebuffer_cleanup - remove a framebuffer object
580 * @fb: framebuffer to remove
582 * Cleanup framebuffer. This function is intended to be used from the drivers
583 * ->destroy callback. It can also be used to clean up driver private
584 * framebuffers embedded into a larger structure.
586 * Note that this function does not remove the fb from active usuage - if it is
587 * still used anywhere, hilarity can ensue since userspace could call getfb on
588 * the id and get back -EINVAL. Obviously no concern at driver unload time.
590 * Also, the framebuffer will not be removed from the lookup idr - for
591 * user-created framebuffers this will happen in in the rmfb ioctl. For
592 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
593 * drm_framebuffer_unregister_private.
595 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
597 struct drm_device *dev = fb->dev;
599 mutex_lock(&dev->mode_config.fb_lock);
601 dev->mode_config.num_fb--;
602 mutex_unlock(&dev->mode_config.fb_lock);
604 EXPORT_SYMBOL(drm_framebuffer_cleanup);
607 * drm_framebuffer_remove - remove and unreference a framebuffer object
608 * @fb: framebuffer to remove
610 * Scans all the CRTCs and planes in @dev's mode_config. If they're
611 * using @fb, removes it, setting it to NULL. Then drops the reference to the
612 * passed-in framebuffer. Might take the modeset locks.
614 * Note that this function optimizes the cleanup away if the caller holds the
615 * last reference to the framebuffer. It is also guaranteed to not take the
616 * modeset locks in this case.
618 void drm_framebuffer_remove(struct drm_framebuffer *fb)
620 struct drm_device *dev = fb->dev;
621 struct drm_crtc *crtc;
622 struct drm_plane *plane;
623 struct drm_mode_set set;
626 WARN_ON(!list_empty(&fb->filp_head));
629 * drm ABI mandates that we remove any deleted framebuffers from active
630 * useage. But since most sane clients only remove framebuffers they no
631 * longer need, try to optimize this away.
633 * Since we're holding a reference ourselves, observing a refcount of 1
634 * means that we're the last holder and can skip it. Also, the refcount
635 * can never increase from 1 again, so we don't need any barriers or
638 * Note that userspace could try to race with use and instate a new
639 * usage _after_ we've cleared all current ones. End result will be an
640 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
643 if (atomic_read(&fb->refcount.refcount) > 1) {
644 drm_modeset_lock_all(dev);
645 /* remove from any CRTC */
646 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
647 if (crtc->primary->fb == fb) {
648 /* should turn off the crtc */
649 memset(&set, 0, sizeof(struct drm_mode_set));
652 ret = drm_mode_set_config_internal(&set);
654 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
658 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
660 drm_plane_force_disable(plane);
662 drm_modeset_unlock_all(dev);
665 drm_framebuffer_unreference(fb);
667 EXPORT_SYMBOL(drm_framebuffer_remove);
670 * drm_crtc_init_with_planes - Initialise a new CRTC object with
671 * specified primary and cursor planes.
673 * @crtc: CRTC object to init
674 * @primary: Primary plane for CRTC
675 * @cursor: Cursor plane for CRTC
676 * @funcs: callbacks for the new CRTC
678 * Inits a new object created as base part of a driver crtc object.
681 * Zero on success, error code on failure.
683 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
684 struct drm_plane *primary,
686 const struct drm_crtc_funcs *funcs)
692 crtc->invert_dimensions = false;
694 drm_modeset_lock_all(dev);
695 mutex_init(&crtc->mutex);
696 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
698 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
702 crtc->base.properties = &crtc->properties;
704 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
705 dev->mode_config.num_crtc++;
707 crtc->primary = primary;
709 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
712 drm_modeset_unlock_all(dev);
716 EXPORT_SYMBOL(drm_crtc_init_with_planes);
719 * drm_crtc_cleanup - Clean up the core crtc usage
720 * @crtc: CRTC to cleanup
722 * This function cleans up @crtc and removes it from the DRM mode setting
723 * core. Note that the function does *not* free the crtc structure itself,
724 * this is the responsibility of the caller.
726 void drm_crtc_cleanup(struct drm_crtc *crtc)
728 struct drm_device *dev = crtc->dev;
730 kfree(crtc->gamma_store);
731 crtc->gamma_store = NULL;
733 drm_mode_object_put(dev, &crtc->base);
734 list_del(&crtc->head);
735 dev->mode_config.num_crtc--;
737 EXPORT_SYMBOL(drm_crtc_cleanup);
740 * drm_crtc_index - find the index of a registered CRTC
741 * @crtc: CRTC to find index for
743 * Given a registered CRTC, return the index of that CRTC within a DRM
744 * device's list of CRTCs.
746 unsigned int drm_crtc_index(struct drm_crtc *crtc)
748 unsigned int index = 0;
749 struct drm_crtc *tmp;
751 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
760 EXPORT_SYMBOL(drm_crtc_index);
763 * drm_mode_remove - remove and free a mode
764 * @connector: connector list to modify
765 * @mode: mode to remove
767 * Remove @mode from @connector's mode list, then free it.
769 static void drm_mode_remove(struct drm_connector *connector,
770 struct drm_display_mode *mode)
772 list_del(&mode->head);
773 drm_mode_destroy(connector->dev, mode);
777 * drm_connector_init - Init a preallocated connector
779 * @connector: the connector to init
780 * @funcs: callbacks for this connector
781 * @connector_type: user visible type of the connector
783 * Initialises a preallocated connector. Connectors should be
784 * subclassed as part of driver connector objects.
787 * Zero on success, error code on failure.
789 int drm_connector_init(struct drm_device *dev,
790 struct drm_connector *connector,
791 const struct drm_connector_funcs *funcs,
795 struct ida *connector_ida =
796 &drm_connector_enum_list[connector_type].ida;
798 drm_modeset_lock_all(dev);
800 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
804 connector->base.properties = &connector->properties;
805 connector->dev = dev;
806 connector->funcs = funcs;
807 connector->connector_type = connector_type;
808 connector->connector_type_id =
809 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
810 if (connector->connector_type_id < 0) {
811 ret = connector->connector_type_id;
815 kasprintf(GFP_KERNEL, "%s-%d",
816 drm_connector_enum_list[connector_type].name,
817 connector->connector_type_id);
818 if (!connector->name) {
823 INIT_LIST_HEAD(&connector->probed_modes);
824 INIT_LIST_HEAD(&connector->modes);
825 connector->edid_blob_ptr = NULL;
826 connector->status = connector_status_unknown;
828 list_add_tail(&connector->head, &dev->mode_config.connector_list);
829 dev->mode_config.num_connector++;
831 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
832 drm_object_attach_property(&connector->base,
833 dev->mode_config.edid_property,
836 drm_object_attach_property(&connector->base,
837 dev->mode_config.dpms_property, 0);
841 drm_mode_object_put(dev, &connector->base);
844 drm_modeset_unlock_all(dev);
848 EXPORT_SYMBOL(drm_connector_init);
851 * drm_connector_cleanup - cleans up an initialised connector
852 * @connector: connector to cleanup
854 * Cleans up the connector but doesn't free the object.
856 void drm_connector_cleanup(struct drm_connector *connector)
858 struct drm_device *dev = connector->dev;
859 struct drm_display_mode *mode, *t;
861 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
862 drm_mode_remove(connector, mode);
864 list_for_each_entry_safe(mode, t, &connector->modes, head)
865 drm_mode_remove(connector, mode);
867 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
868 connector->connector_type_id);
870 drm_mode_object_put(dev, &connector->base);
871 kfree(connector->name);
872 connector->name = NULL;
873 list_del(&connector->head);
874 dev->mode_config.num_connector--;
876 EXPORT_SYMBOL(drm_connector_cleanup);
879 * drm_connector_unplug_all - unregister connector userspace interfaces
882 * This function unregisters all connector userspace interfaces in sysfs. Should
883 * be call when the device is disconnected, e.g. from an usb driver's
884 * ->disconnect callback.
886 void drm_connector_unplug_all(struct drm_device *dev)
888 struct drm_connector *connector;
890 /* taking the mode config mutex ends up in a clash with sysfs */
891 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
892 drm_sysfs_connector_remove(connector);
895 EXPORT_SYMBOL(drm_connector_unplug_all);
898 * drm_bridge_init - initialize a drm transcoder/bridge
900 * @bridge: transcoder/bridge to set up
901 * @funcs: bridge function table
903 * Initialises a preallocated bridge. Bridges should be
904 * subclassed as part of driver connector objects.
907 * Zero on success, error code on failure.
909 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
910 const struct drm_bridge_funcs *funcs)
914 drm_modeset_lock_all(dev);
916 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
921 bridge->funcs = funcs;
923 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
924 dev->mode_config.num_bridge++;
927 drm_modeset_unlock_all(dev);
930 EXPORT_SYMBOL(drm_bridge_init);
933 * drm_bridge_cleanup - cleans up an initialised bridge
934 * @bridge: bridge to cleanup
936 * Cleans up the bridge but doesn't free the object.
938 void drm_bridge_cleanup(struct drm_bridge *bridge)
940 struct drm_device *dev = bridge->dev;
942 drm_modeset_lock_all(dev);
943 drm_mode_object_put(dev, &bridge->base);
944 list_del(&bridge->head);
945 dev->mode_config.num_bridge--;
946 drm_modeset_unlock_all(dev);
948 EXPORT_SYMBOL(drm_bridge_cleanup);
951 * drm_encoder_init - Init a preallocated encoder
953 * @encoder: the encoder to init
954 * @funcs: callbacks for this encoder
955 * @encoder_type: user visible type of the encoder
957 * Initialises a preallocated encoder. Encoder should be
958 * subclassed as part of driver encoder objects.
961 * Zero on success, error code on failure.
963 int drm_encoder_init(struct drm_device *dev,
964 struct drm_encoder *encoder,
965 const struct drm_encoder_funcs *funcs,
970 drm_modeset_lock_all(dev);
972 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
977 encoder->encoder_type = encoder_type;
978 encoder->funcs = funcs;
979 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
980 drm_encoder_enum_list[encoder_type].name,
982 if (!encoder->name) {
987 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
988 dev->mode_config.num_encoder++;
992 drm_mode_object_put(dev, &encoder->base);
995 drm_modeset_unlock_all(dev);
999 EXPORT_SYMBOL(drm_encoder_init);
1002 * drm_encoder_cleanup - cleans up an initialised encoder
1003 * @encoder: encoder to cleanup
1005 * Cleans up the encoder but doesn't free the object.
1007 void drm_encoder_cleanup(struct drm_encoder *encoder)
1009 struct drm_device *dev = encoder->dev;
1010 drm_modeset_lock_all(dev);
1011 drm_mode_object_put(dev, &encoder->base);
1012 kfree(encoder->name);
1013 encoder->name = NULL;
1014 list_del(&encoder->head);
1015 dev->mode_config.num_encoder--;
1016 drm_modeset_unlock_all(dev);
1018 EXPORT_SYMBOL(drm_encoder_cleanup);
1021 * drm_universal_plane_init - Initialize a new universal plane object
1023 * @plane: plane object to init
1024 * @possible_crtcs: bitmask of possible CRTCs
1025 * @funcs: callbacks for the new plane
1026 * @formats: array of supported formats (%DRM_FORMAT_*)
1027 * @format_count: number of elements in @formats
1028 * @type: type of plane (overlay, primary, cursor)
1030 * Initializes a plane object of type @type.
1033 * Zero on success, error code on failure.
1035 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1036 unsigned long possible_crtcs,
1037 const struct drm_plane_funcs *funcs,
1038 const uint32_t *formats, uint32_t format_count,
1039 enum drm_plane_type type)
1043 drm_modeset_lock_all(dev);
1045 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1049 plane->base.properties = &plane->properties;
1051 plane->funcs = funcs;
1052 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1054 if (!plane->format_types) {
1055 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1056 drm_mode_object_put(dev, &plane->base);
1061 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1062 plane->format_count = format_count;
1063 plane->possible_crtcs = possible_crtcs;
1066 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1067 dev->mode_config.num_total_plane++;
1068 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1069 dev->mode_config.num_overlay_plane++;
1071 drm_object_attach_property(&plane->base,
1072 dev->mode_config.plane_type_property,
1076 drm_modeset_unlock_all(dev);
1080 EXPORT_SYMBOL(drm_universal_plane_init);
1083 * drm_plane_init - Initialize a legacy plane
1085 * @plane: plane object to init
1086 * @possible_crtcs: bitmask of possible CRTCs
1087 * @funcs: callbacks for the new plane
1088 * @formats: array of supported formats (%DRM_FORMAT_*)
1089 * @format_count: number of elements in @formats
1090 * @is_primary: plane type (primary vs overlay)
1092 * Legacy API to initialize a DRM plane.
1094 * New drivers should call drm_universal_plane_init() instead.
1097 * Zero on success, error code on failure.
1099 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1100 unsigned long possible_crtcs,
1101 const struct drm_plane_funcs *funcs,
1102 const uint32_t *formats, uint32_t format_count,
1105 enum drm_plane_type type;
1107 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1108 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1109 formats, format_count, type);
1111 EXPORT_SYMBOL(drm_plane_init);
1114 * drm_plane_cleanup - Clean up the core plane usage
1115 * @plane: plane to cleanup
1117 * This function cleans up @plane and removes it from the DRM mode setting
1118 * core. Note that the function does *not* free the plane structure itself,
1119 * this is the responsibility of the caller.
1121 void drm_plane_cleanup(struct drm_plane *plane)
1123 struct drm_device *dev = plane->dev;
1125 drm_modeset_lock_all(dev);
1126 kfree(plane->format_types);
1127 drm_mode_object_put(dev, &plane->base);
1129 BUG_ON(list_empty(&plane->head));
1131 list_del(&plane->head);
1132 dev->mode_config.num_total_plane--;
1133 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1134 dev->mode_config.num_overlay_plane--;
1135 drm_modeset_unlock_all(dev);
1137 EXPORT_SYMBOL(drm_plane_cleanup);
1140 * drm_plane_force_disable - Forcibly disable a plane
1141 * @plane: plane to disable
1143 * Forces the plane to be disabled.
1145 * Used when the plane's current framebuffer is destroyed,
1146 * and when restoring fbdev mode.
1148 void drm_plane_force_disable(struct drm_plane *plane)
1150 struct drm_framebuffer *old_fb = plane->fb;
1156 ret = plane->funcs->disable_plane(plane);
1158 DRM_ERROR("failed to disable plane with busy fb\n");
1161 /* disconnect the plane from the fb and crtc: */
1162 __drm_framebuffer_unreference(old_fb);
1166 EXPORT_SYMBOL(drm_plane_force_disable);
1168 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1170 struct drm_property *edid;
1171 struct drm_property *dpms;
1174 * Standard properties (apply to all connectors)
1176 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1177 DRM_MODE_PROP_IMMUTABLE,
1179 dev->mode_config.edid_property = edid;
1181 dpms = drm_property_create_enum(dev, 0,
1182 "DPMS", drm_dpms_enum_list,
1183 ARRAY_SIZE(drm_dpms_enum_list));
1184 dev->mode_config.dpms_property = dpms;
1189 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1191 struct drm_property *type;
1194 * Standard properties (apply to all planes)
1196 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1197 "type", drm_plane_type_enum_list,
1198 ARRAY_SIZE(drm_plane_type_enum_list));
1199 dev->mode_config.plane_type_property = type;
1205 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1208 * Called by a driver the first time a DVI-I connector is made.
1210 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1212 struct drm_property *dvi_i_selector;
1213 struct drm_property *dvi_i_subconnector;
1215 if (dev->mode_config.dvi_i_select_subconnector_property)
1219 drm_property_create_enum(dev, 0,
1220 "select subconnector",
1221 drm_dvi_i_select_enum_list,
1222 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1223 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1225 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1227 drm_dvi_i_subconnector_enum_list,
1228 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1229 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1233 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1236 * drm_create_tv_properties - create TV specific connector properties
1238 * @num_modes: number of different TV formats (modes) supported
1239 * @modes: array of pointers to strings containing name of each format
1241 * Called by a driver's TV initialization routine, this function creates
1242 * the TV specific connector properties for a given device. Caller is
1243 * responsible for allocating a list of format names and passing them to
1246 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1249 struct drm_property *tv_selector;
1250 struct drm_property *tv_subconnector;
1253 if (dev->mode_config.tv_select_subconnector_property)
1257 * Basic connector properties
1259 tv_selector = drm_property_create_enum(dev, 0,
1260 "select subconnector",
1261 drm_tv_select_enum_list,
1262 ARRAY_SIZE(drm_tv_select_enum_list));
1263 dev->mode_config.tv_select_subconnector_property = tv_selector;
1266 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1268 drm_tv_subconnector_enum_list,
1269 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1270 dev->mode_config.tv_subconnector_property = tv_subconnector;
1273 * Other, TV specific properties: margins & TV modes.
1275 dev->mode_config.tv_left_margin_property =
1276 drm_property_create_range(dev, 0, "left margin", 0, 100);
1278 dev->mode_config.tv_right_margin_property =
1279 drm_property_create_range(dev, 0, "right margin", 0, 100);
1281 dev->mode_config.tv_top_margin_property =
1282 drm_property_create_range(dev, 0, "top margin", 0, 100);
1284 dev->mode_config.tv_bottom_margin_property =
1285 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1287 dev->mode_config.tv_mode_property =
1288 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1290 for (i = 0; i < num_modes; i++)
1291 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1294 dev->mode_config.tv_brightness_property =
1295 drm_property_create_range(dev, 0, "brightness", 0, 100);
1297 dev->mode_config.tv_contrast_property =
1298 drm_property_create_range(dev, 0, "contrast", 0, 100);
1300 dev->mode_config.tv_flicker_reduction_property =
1301 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1303 dev->mode_config.tv_overscan_property =
1304 drm_property_create_range(dev, 0, "overscan", 0, 100);
1306 dev->mode_config.tv_saturation_property =
1307 drm_property_create_range(dev, 0, "saturation", 0, 100);
1309 dev->mode_config.tv_hue_property =
1310 drm_property_create_range(dev, 0, "hue", 0, 100);
1314 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1317 * drm_mode_create_scaling_mode_property - create scaling mode property
1320 * Called by a driver the first time it's needed, must be attached to desired
1323 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1325 struct drm_property *scaling_mode;
1327 if (dev->mode_config.scaling_mode_property)
1331 drm_property_create_enum(dev, 0, "scaling mode",
1332 drm_scaling_mode_enum_list,
1333 ARRAY_SIZE(drm_scaling_mode_enum_list));
1335 dev->mode_config.scaling_mode_property = scaling_mode;
1339 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1342 * drm_mode_create_dirty_property - create dirty property
1345 * Called by a driver the first time it's needed, must be attached to desired
1348 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1350 struct drm_property *dirty_info;
1352 if (dev->mode_config.dirty_info_property)
1356 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1358 drm_dirty_info_enum_list,
1359 ARRAY_SIZE(drm_dirty_info_enum_list));
1360 dev->mode_config.dirty_info_property = dirty_info;
1364 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1366 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1368 uint32_t total_objects = 0;
1370 total_objects += dev->mode_config.num_crtc;
1371 total_objects += dev->mode_config.num_connector;
1372 total_objects += dev->mode_config.num_encoder;
1373 total_objects += dev->mode_config.num_bridge;
1375 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1376 if (!group->id_list)
1379 group->num_crtcs = 0;
1380 group->num_connectors = 0;
1381 group->num_encoders = 0;
1382 group->num_bridges = 0;
1386 void drm_mode_group_destroy(struct drm_mode_group *group)
1388 kfree(group->id_list);
1389 group->id_list = NULL;
1393 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1394 * the drm core's responsibility to set up mode control groups.
1396 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1397 struct drm_mode_group *group)
1399 struct drm_crtc *crtc;
1400 struct drm_encoder *encoder;
1401 struct drm_connector *connector;
1402 struct drm_bridge *bridge;
1405 if ((ret = drm_mode_group_init(dev, group)))
1408 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1409 group->id_list[group->num_crtcs++] = crtc->base.id;
1411 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1412 group->id_list[group->num_crtcs + group->num_encoders++] =
1415 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1416 group->id_list[group->num_crtcs + group->num_encoders +
1417 group->num_connectors++] = connector->base.id;
1419 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1420 group->id_list[group->num_crtcs + group->num_encoders +
1421 group->num_connectors + group->num_bridges++] =
1426 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1429 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1430 * @out: drm_mode_modeinfo struct to return to the user
1431 * @in: drm_display_mode to use
1433 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1436 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1437 const struct drm_display_mode *in)
1439 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1440 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1441 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1442 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1443 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1444 "timing values too large for mode info\n");
1446 out->clock = in->clock;
1447 out->hdisplay = in->hdisplay;
1448 out->hsync_start = in->hsync_start;
1449 out->hsync_end = in->hsync_end;
1450 out->htotal = in->htotal;
1451 out->hskew = in->hskew;
1452 out->vdisplay = in->vdisplay;
1453 out->vsync_start = in->vsync_start;
1454 out->vsync_end = in->vsync_end;
1455 out->vtotal = in->vtotal;
1456 out->vscan = in->vscan;
1457 out->vrefresh = in->vrefresh;
1458 out->flags = in->flags;
1459 out->type = in->type;
1460 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1461 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1465 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1466 * @out: drm_display_mode to return to the user
1467 * @in: drm_mode_modeinfo to use
1469 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1473 * Zero on success, errno on failure.
1475 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1476 const struct drm_mode_modeinfo *in)
1478 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1481 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1484 out->clock = in->clock;
1485 out->hdisplay = in->hdisplay;
1486 out->hsync_start = in->hsync_start;
1487 out->hsync_end = in->hsync_end;
1488 out->htotal = in->htotal;
1489 out->hskew = in->hskew;
1490 out->vdisplay = in->vdisplay;
1491 out->vsync_start = in->vsync_start;
1492 out->vsync_end = in->vsync_end;
1493 out->vtotal = in->vtotal;
1494 out->vscan = in->vscan;
1495 out->vrefresh = in->vrefresh;
1496 out->flags = in->flags;
1497 out->type = in->type;
1498 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1499 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1505 * drm_mode_getresources - get graphics configuration
1506 * @dev: drm device for the ioctl
1507 * @data: data pointer for the ioctl
1508 * @file_priv: drm file for the ioctl call
1510 * Construct a set of configuration description structures and return
1511 * them to the user, including CRTC, connector and framebuffer configuration.
1513 * Called by the user via ioctl.
1516 * Zero on success, errno on failure.
1518 int drm_mode_getresources(struct drm_device *dev, void *data,
1519 struct drm_file *file_priv)
1521 struct drm_mode_card_res *card_res = data;
1522 struct list_head *lh;
1523 struct drm_framebuffer *fb;
1524 struct drm_connector *connector;
1525 struct drm_crtc *crtc;
1526 struct drm_encoder *encoder;
1528 int connector_count = 0;
1531 int encoder_count = 0;
1533 uint32_t __user *fb_id;
1534 uint32_t __user *crtc_id;
1535 uint32_t __user *connector_id;
1536 uint32_t __user *encoder_id;
1537 struct drm_mode_group *mode_group;
1539 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1543 mutex_lock(&file_priv->fbs_lock);
1545 * For the non-control nodes we need to limit the list of resources
1546 * by IDs in the group list for this node
1548 list_for_each(lh, &file_priv->fbs)
1551 /* handle this in 4 parts */
1553 if (card_res->count_fbs >= fb_count) {
1555 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1556 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1557 if (put_user(fb->base.id, fb_id + copied)) {
1558 mutex_unlock(&file_priv->fbs_lock);
1564 card_res->count_fbs = fb_count;
1565 mutex_unlock(&file_priv->fbs_lock);
1567 drm_modeset_lock_all(dev);
1568 if (!drm_is_primary_client(file_priv)) {
1571 list_for_each(lh, &dev->mode_config.crtc_list)
1574 list_for_each(lh, &dev->mode_config.connector_list)
1577 list_for_each(lh, &dev->mode_config.encoder_list)
1581 mode_group = &file_priv->master->minor->mode_group;
1582 crtc_count = mode_group->num_crtcs;
1583 connector_count = mode_group->num_connectors;
1584 encoder_count = mode_group->num_encoders;
1587 card_res->max_height = dev->mode_config.max_height;
1588 card_res->min_height = dev->mode_config.min_height;
1589 card_res->max_width = dev->mode_config.max_width;
1590 card_res->min_width = dev->mode_config.min_width;
1593 if (card_res->count_crtcs >= crtc_count) {
1595 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1597 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1599 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1600 if (put_user(crtc->base.id, crtc_id + copied)) {
1607 for (i = 0; i < mode_group->num_crtcs; i++) {
1608 if (put_user(mode_group->id_list[i],
1609 crtc_id + copied)) {
1617 card_res->count_crtcs = crtc_count;
1620 if (card_res->count_encoders >= encoder_count) {
1622 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1624 list_for_each_entry(encoder,
1625 &dev->mode_config.encoder_list,
1627 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1629 if (put_user(encoder->base.id, encoder_id +
1637 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1638 if (put_user(mode_group->id_list[i],
1639 encoder_id + copied)) {
1648 card_res->count_encoders = encoder_count;
1651 if (card_res->count_connectors >= connector_count) {
1653 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1655 list_for_each_entry(connector,
1656 &dev->mode_config.connector_list,
1658 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1661 if (put_user(connector->base.id,
1662 connector_id + copied)) {
1669 int start = mode_group->num_crtcs +
1670 mode_group->num_encoders;
1671 for (i = start; i < start + mode_group->num_connectors; i++) {
1672 if (put_user(mode_group->id_list[i],
1673 connector_id + copied)) {
1681 card_res->count_connectors = connector_count;
1683 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1684 card_res->count_connectors, card_res->count_encoders);
1687 drm_modeset_unlock_all(dev);
1692 * drm_mode_getcrtc - get CRTC configuration
1693 * @dev: drm device for the ioctl
1694 * @data: data pointer for the ioctl
1695 * @file_priv: drm file for the ioctl call
1697 * Construct a CRTC configuration structure to return to the user.
1699 * Called by the user via ioctl.
1702 * Zero on success, errno on failure.
1704 int drm_mode_getcrtc(struct drm_device *dev,
1705 void *data, struct drm_file *file_priv)
1707 struct drm_mode_crtc *crtc_resp = data;
1708 struct drm_crtc *crtc;
1711 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1714 drm_modeset_lock_all(dev);
1716 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1722 crtc_resp->x = crtc->x;
1723 crtc_resp->y = crtc->y;
1724 crtc_resp->gamma_size = crtc->gamma_size;
1725 if (crtc->primary->fb)
1726 crtc_resp->fb_id = crtc->primary->fb->base.id;
1728 crtc_resp->fb_id = 0;
1730 if (crtc->enabled) {
1732 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1733 crtc_resp->mode_valid = 1;
1736 crtc_resp->mode_valid = 0;
1740 drm_modeset_unlock_all(dev);
1744 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1745 const struct drm_file *file_priv)
1748 * If user-space hasn't configured the driver to expose the stereo 3D
1749 * modes, don't expose them.
1751 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1758 * drm_mode_getconnector - get connector configuration
1759 * @dev: drm device for the ioctl
1760 * @data: data pointer for the ioctl
1761 * @file_priv: drm file for the ioctl call
1763 * Construct a connector configuration structure to return to the user.
1765 * Called by the user via ioctl.
1768 * Zero on success, errno on failure.
1770 int drm_mode_getconnector(struct drm_device *dev, void *data,
1771 struct drm_file *file_priv)
1773 struct drm_mode_get_connector *out_resp = data;
1774 struct drm_connector *connector;
1775 struct drm_display_mode *mode;
1777 int props_count = 0;
1778 int encoders_count = 0;
1782 struct drm_mode_modeinfo u_mode;
1783 struct drm_mode_modeinfo __user *mode_ptr;
1784 uint32_t __user *prop_ptr;
1785 uint64_t __user *prop_values;
1786 uint32_t __user *encoder_ptr;
1788 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1791 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1793 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1795 mutex_lock(&dev->mode_config.mutex);
1797 connector = drm_connector_find(dev, out_resp->connector_id);
1803 props_count = connector->properties.count;
1805 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1806 if (connector->encoder_ids[i] != 0) {
1811 if (out_resp->count_modes == 0) {
1812 connector->funcs->fill_modes(connector,
1813 dev->mode_config.max_width,
1814 dev->mode_config.max_height);
1817 /* delayed so we get modes regardless of pre-fill_modes state */
1818 list_for_each_entry(mode, &connector->modes, head)
1819 if (drm_mode_expose_to_userspace(mode, file_priv))
1822 out_resp->connector_id = connector->base.id;
1823 out_resp->connector_type = connector->connector_type;
1824 out_resp->connector_type_id = connector->connector_type_id;
1825 out_resp->mm_width = connector->display_info.width_mm;
1826 out_resp->mm_height = connector->display_info.height_mm;
1827 out_resp->subpixel = connector->display_info.subpixel_order;
1828 out_resp->connection = connector->status;
1829 if (connector->encoder)
1830 out_resp->encoder_id = connector->encoder->base.id;
1832 out_resp->encoder_id = 0;
1835 * This ioctl is called twice, once to determine how much space is
1836 * needed, and the 2nd time to fill it.
1838 if ((out_resp->count_modes >= mode_count) && mode_count) {
1840 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1841 list_for_each_entry(mode, &connector->modes, head) {
1842 if (!drm_mode_expose_to_userspace(mode, file_priv))
1845 drm_crtc_convert_to_umode(&u_mode, mode);
1846 if (copy_to_user(mode_ptr + copied,
1847 &u_mode, sizeof(u_mode))) {
1854 out_resp->count_modes = mode_count;
1856 if ((out_resp->count_props >= props_count) && props_count) {
1858 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1859 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1860 for (i = 0; i < connector->properties.count; i++) {
1861 if (put_user(connector->properties.ids[i],
1862 prop_ptr + copied)) {
1867 if (put_user(connector->properties.values[i],
1868 prop_values + copied)) {
1875 out_resp->count_props = props_count;
1877 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1879 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1880 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1881 if (connector->encoder_ids[i] != 0) {
1882 if (put_user(connector->encoder_ids[i],
1883 encoder_ptr + copied)) {
1891 out_resp->count_encoders = encoders_count;
1894 mutex_unlock(&dev->mode_config.mutex);
1900 * drm_mode_getencoder - get encoder configuration
1901 * @dev: drm device for the ioctl
1902 * @data: data pointer for the ioctl
1903 * @file_priv: drm file for the ioctl call
1905 * Construct a encoder configuration structure to return to the user.
1907 * Called by the user via ioctl.
1910 * Zero on success, errno on failure.
1912 int drm_mode_getencoder(struct drm_device *dev, void *data,
1913 struct drm_file *file_priv)
1915 struct drm_mode_get_encoder *enc_resp = data;
1916 struct drm_encoder *encoder;
1919 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1922 drm_modeset_lock_all(dev);
1923 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
1930 enc_resp->crtc_id = encoder->crtc->base.id;
1932 enc_resp->crtc_id = 0;
1933 enc_resp->encoder_type = encoder->encoder_type;
1934 enc_resp->encoder_id = encoder->base.id;
1935 enc_resp->possible_crtcs = encoder->possible_crtcs;
1936 enc_resp->possible_clones = encoder->possible_clones;
1939 drm_modeset_unlock_all(dev);
1944 * drm_mode_getplane_res - enumerate all plane resources
1947 * @file_priv: DRM file info
1949 * Construct a list of plane ids to return to the user.
1951 * Called by the user via ioctl.
1954 * Zero on success, errno on failure.
1956 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1957 struct drm_file *file_priv)
1959 struct drm_mode_get_plane_res *plane_resp = data;
1960 struct drm_mode_config *config;
1961 struct drm_plane *plane;
1962 uint32_t __user *plane_ptr;
1963 int copied = 0, ret = 0;
1964 unsigned num_planes;
1966 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1969 drm_modeset_lock_all(dev);
1970 config = &dev->mode_config;
1972 if (file_priv->universal_planes)
1973 num_planes = config->num_total_plane;
1975 num_planes = config->num_overlay_plane;
1978 * This ioctl is called twice, once to determine how much space is
1979 * needed, and the 2nd time to fill it.
1982 (plane_resp->count_planes >= num_planes)) {
1983 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1985 list_for_each_entry(plane, &config->plane_list, head) {
1987 * Unless userspace set the 'universal planes'
1988 * capability bit, only advertise overlays.
1990 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1991 !file_priv->universal_planes)
1994 if (put_user(plane->base.id, plane_ptr + copied)) {
2001 plane_resp->count_planes = num_planes;
2004 drm_modeset_unlock_all(dev);
2009 * drm_mode_getplane - get plane configuration
2012 * @file_priv: DRM file info
2014 * Construct a plane configuration structure to return to the user.
2016 * Called by the user via ioctl.
2019 * Zero on success, errno on failure.
2021 int drm_mode_getplane(struct drm_device *dev, void *data,
2022 struct drm_file *file_priv)
2024 struct drm_mode_get_plane *plane_resp = data;
2025 struct drm_plane *plane;
2026 uint32_t __user *format_ptr;
2029 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2032 drm_modeset_lock_all(dev);
2033 plane = drm_plane_find(dev, plane_resp->plane_id);
2040 plane_resp->crtc_id = plane->crtc->base.id;
2042 plane_resp->crtc_id = 0;
2045 plane_resp->fb_id = plane->fb->base.id;
2047 plane_resp->fb_id = 0;
2049 plane_resp->plane_id = plane->base.id;
2050 plane_resp->possible_crtcs = plane->possible_crtcs;
2051 plane_resp->gamma_size = 0;
2054 * This ioctl is called twice, once to determine how much space is
2055 * needed, and the 2nd time to fill it.
2057 if (plane->format_count &&
2058 (plane_resp->count_format_types >= plane->format_count)) {
2059 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2060 if (copy_to_user(format_ptr,
2061 plane->format_types,
2062 sizeof(uint32_t) * plane->format_count)) {
2067 plane_resp->count_format_types = plane->format_count;
2070 drm_modeset_unlock_all(dev);
2075 * drm_mode_setplane - configure a plane's configuration
2077 * @data: ioctl data*
2078 * @file_priv: DRM file info
2080 * Set plane configuration, including placement, fb, scaling, and other factors.
2081 * Or pass a NULL fb to disable.
2084 * Zero on success, errno on failure.
2086 int drm_mode_setplane(struct drm_device *dev, void *data,
2087 struct drm_file *file_priv)
2089 struct drm_mode_set_plane *plane_req = data;
2090 struct drm_plane *plane;
2091 struct drm_crtc *crtc;
2092 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2094 unsigned int fb_width, fb_height;
2097 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2101 * First, find the plane, crtc, and fb objects. If not available,
2102 * we don't bother to call the driver.
2104 plane = drm_plane_find(dev, plane_req->plane_id);
2106 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2107 plane_req->plane_id);
2111 /* No fb means shut it down */
2112 if (!plane_req->fb_id) {
2113 drm_modeset_lock_all(dev);
2115 ret = plane->funcs->disable_plane(plane);
2122 drm_modeset_unlock_all(dev);
2126 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2128 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2129 plane_req->crtc_id);
2134 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2136 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2142 /* Check whether this plane supports the fb pixel format. */
2143 for (i = 0; i < plane->format_count; i++)
2144 if (fb->pixel_format == plane->format_types[i])
2146 if (i == plane->format_count) {
2147 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2148 drm_get_format_name(fb->pixel_format));
2153 fb_width = fb->width << 16;
2154 fb_height = fb->height << 16;
2156 /* Make sure source coordinates are inside the fb. */
2157 if (plane_req->src_w > fb_width ||
2158 plane_req->src_x > fb_width - plane_req->src_w ||
2159 plane_req->src_h > fb_height ||
2160 plane_req->src_y > fb_height - plane_req->src_h) {
2161 DRM_DEBUG_KMS("Invalid source coordinates "
2162 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2163 plane_req->src_w >> 16,
2164 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2165 plane_req->src_h >> 16,
2166 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2167 plane_req->src_x >> 16,
2168 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2169 plane_req->src_y >> 16,
2170 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2175 /* Give drivers some help against integer overflows */
2176 if (plane_req->crtc_w > INT_MAX ||
2177 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2178 plane_req->crtc_h > INT_MAX ||
2179 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2180 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2181 plane_req->crtc_w, plane_req->crtc_h,
2182 plane_req->crtc_x, plane_req->crtc_y);
2187 drm_modeset_lock_all(dev);
2189 ret = plane->funcs->update_plane(plane, crtc, fb,
2190 plane_req->crtc_x, plane_req->crtc_y,
2191 plane_req->crtc_w, plane_req->crtc_h,
2192 plane_req->src_x, plane_req->src_y,
2193 plane_req->src_w, plane_req->src_h);
2201 drm_modeset_unlock_all(dev);
2205 drm_framebuffer_unreference(fb);
2207 drm_framebuffer_unreference(old_fb);
2213 * drm_mode_set_config_internal - helper to call ->set_config
2214 * @set: modeset config to set
2216 * This is a little helper to wrap internal calls to the ->set_config driver
2217 * interface. The only thing it adds is correct refcounting dance.
2220 * Zero on success, errno on failure.
2222 int drm_mode_set_config_internal(struct drm_mode_set *set)
2224 struct drm_crtc *crtc = set->crtc;
2225 struct drm_framebuffer *fb;
2226 struct drm_crtc *tmp;
2230 * NOTE: ->set_config can also disable other crtcs (if we steal all
2231 * connectors from it), hence we need to refcount the fbs across all
2232 * crtcs. Atomic modeset will have saner semantics ...
2234 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2235 tmp->old_fb = tmp->primary->fb;
2239 ret = crtc->funcs->set_config(set);
2241 crtc->primary->crtc = crtc;
2242 crtc->primary->fb = fb;
2245 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2246 if (tmp->primary->fb)
2247 drm_framebuffer_reference(tmp->primary->fb);
2249 drm_framebuffer_unreference(tmp->old_fb);
2254 EXPORT_SYMBOL(drm_mode_set_config_internal);
2257 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2259 * @crtc: CRTC that framebuffer will be displayed on
2262 * @mode: mode that framebuffer will be displayed under
2263 * @fb: framebuffer to check size of
2265 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2267 const struct drm_display_mode *mode,
2268 const struct drm_framebuffer *fb)
2271 int hdisplay, vdisplay;
2273 hdisplay = mode->hdisplay;
2274 vdisplay = mode->vdisplay;
2276 if (drm_mode_is_stereo(mode)) {
2277 struct drm_display_mode adjusted = *mode;
2279 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2280 hdisplay = adjusted.crtc_hdisplay;
2281 vdisplay = adjusted.crtc_vdisplay;
2284 if (crtc->invert_dimensions)
2285 swap(hdisplay, vdisplay);
2287 if (hdisplay > fb->width ||
2288 vdisplay > fb->height ||
2289 x > fb->width - hdisplay ||
2290 y > fb->height - vdisplay) {
2291 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2292 fb->width, fb->height, hdisplay, vdisplay, x, y,
2293 crtc->invert_dimensions ? " (inverted)" : "");
2299 EXPORT_SYMBOL(drm_crtc_check_viewport);
2302 * drm_mode_setcrtc - set CRTC configuration
2303 * @dev: drm device for the ioctl
2304 * @data: data pointer for the ioctl
2305 * @file_priv: drm file for the ioctl call
2307 * Build a new CRTC configuration based on user request.
2309 * Called by the user via ioctl.
2312 * Zero on success, errno on failure.
2314 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2315 struct drm_file *file_priv)
2317 struct drm_mode_config *config = &dev->mode_config;
2318 struct drm_mode_crtc *crtc_req = data;
2319 struct drm_crtc *crtc;
2320 struct drm_connector **connector_set = NULL, *connector;
2321 struct drm_framebuffer *fb = NULL;
2322 struct drm_display_mode *mode = NULL;
2323 struct drm_mode_set set;
2324 uint32_t __user *set_connectors_ptr;
2328 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2331 /* For some reason crtc x/y offsets are signed internally. */
2332 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2335 drm_modeset_lock_all(dev);
2336 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2338 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2342 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2344 if (crtc_req->mode_valid) {
2345 /* If we have a mode we need a framebuffer. */
2346 /* If we pass -1, set the mode with the currently bound fb */
2347 if (crtc_req->fb_id == -1) {
2348 if (!crtc->primary->fb) {
2349 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2353 fb = crtc->primary->fb;
2354 /* Make refcounting symmetric with the lookup path. */
2355 drm_framebuffer_reference(fb);
2357 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2359 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2366 mode = drm_mode_create(dev);
2372 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2374 DRM_DEBUG_KMS("Invalid mode\n");
2378 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2380 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2387 if (crtc_req->count_connectors == 0 && mode) {
2388 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2393 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2394 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2395 crtc_req->count_connectors);
2400 if (crtc_req->count_connectors > 0) {
2403 /* Avoid unbounded kernel memory allocation */
2404 if (crtc_req->count_connectors > config->num_connector) {
2409 connector_set = kmalloc(crtc_req->count_connectors *
2410 sizeof(struct drm_connector *),
2412 if (!connector_set) {
2417 for (i = 0; i < crtc_req->count_connectors; i++) {
2418 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2419 if (get_user(out_id, &set_connectors_ptr[i])) {
2424 connector = drm_connector_find(dev, out_id);
2426 DRM_DEBUG_KMS("Connector id %d unknown\n",
2431 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2435 connector_set[i] = connector;
2440 set.x = crtc_req->x;
2441 set.y = crtc_req->y;
2443 set.connectors = connector_set;
2444 set.num_connectors = crtc_req->count_connectors;
2446 ret = drm_mode_set_config_internal(&set);
2450 drm_framebuffer_unreference(fb);
2452 kfree(connector_set);
2453 drm_mode_destroy(dev, mode);
2454 drm_modeset_unlock_all(dev);
2458 static int drm_mode_cursor_common(struct drm_device *dev,
2459 struct drm_mode_cursor2 *req,
2460 struct drm_file *file_priv)
2462 struct drm_crtc *crtc;
2465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2468 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2471 crtc = drm_crtc_find(dev, req->crtc_id);
2473 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2477 mutex_lock(&crtc->mutex);
2478 if (req->flags & DRM_MODE_CURSOR_BO) {
2479 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2483 /* Turns off the cursor if handle is 0 */
2484 if (crtc->funcs->cursor_set2)
2485 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2486 req->width, req->height, req->hot_x, req->hot_y);
2488 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2489 req->width, req->height);
2492 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2493 if (crtc->funcs->cursor_move) {
2494 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2501 mutex_unlock(&crtc->mutex);
2509 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2510 * @dev: drm device for the ioctl
2511 * @data: data pointer for the ioctl
2512 * @file_priv: drm file for the ioctl call
2514 * Set the cursor configuration based on user request.
2516 * Called by the user via ioctl.
2519 * Zero on success, errno on failure.
2521 int drm_mode_cursor_ioctl(struct drm_device *dev,
2522 void *data, struct drm_file *file_priv)
2524 struct drm_mode_cursor *req = data;
2525 struct drm_mode_cursor2 new_req;
2527 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2528 new_req.hot_x = new_req.hot_y = 0;
2530 return drm_mode_cursor_common(dev, &new_req, file_priv);
2534 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2535 * @dev: drm device for the ioctl
2536 * @data: data pointer for the ioctl
2537 * @file_priv: drm file for the ioctl call
2539 * Set the cursor configuration based on user request. This implements the 2nd
2540 * version of the cursor ioctl, which allows userspace to additionally specify
2541 * the hotspot of the pointer.
2543 * Called by the user via ioctl.
2546 * Zero on success, errno on failure.
2548 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2549 void *data, struct drm_file *file_priv)
2551 struct drm_mode_cursor2 *req = data;
2552 return drm_mode_cursor_common(dev, req, file_priv);
2556 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2557 * @bpp: bits per pixels
2558 * @depth: bit depth per pixel
2560 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2561 * Useful in fbdev emulation code, since that deals in those values.
2563 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2569 fmt = DRM_FORMAT_C8;
2573 fmt = DRM_FORMAT_XRGB1555;
2575 fmt = DRM_FORMAT_RGB565;
2578 fmt = DRM_FORMAT_RGB888;
2582 fmt = DRM_FORMAT_XRGB8888;
2583 else if (depth == 30)
2584 fmt = DRM_FORMAT_XRGB2101010;
2586 fmt = DRM_FORMAT_ARGB8888;
2589 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2590 fmt = DRM_FORMAT_XRGB8888;
2596 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2599 * drm_mode_addfb - add an FB to the graphics configuration
2600 * @dev: drm device for the ioctl
2601 * @data: data pointer for the ioctl
2602 * @file_priv: drm file for the ioctl call
2604 * Add a new FB to the specified CRTC, given a user request. This is the
2605 * original addfb ioclt which only supported RGB formats.
2607 * Called by the user via ioctl.
2610 * Zero on success, errno on failure.
2612 int drm_mode_addfb(struct drm_device *dev,
2613 void *data, struct drm_file *file_priv)
2615 struct drm_mode_fb_cmd *or = data;
2616 struct drm_mode_fb_cmd2 r = {};
2617 struct drm_mode_config *config = &dev->mode_config;
2618 struct drm_framebuffer *fb;
2621 /* Use new struct with format internally */
2622 r.fb_id = or->fb_id;
2623 r.width = or->width;
2624 r.height = or->height;
2625 r.pitches[0] = or->pitch;
2626 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2627 r.handles[0] = or->handle;
2629 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2632 if ((config->min_width > r.width) || (r.width > config->max_width))
2635 if ((config->min_height > r.height) || (r.height > config->max_height))
2638 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2640 DRM_DEBUG_KMS("could not create framebuffer\n");
2644 mutex_lock(&file_priv->fbs_lock);
2645 or->fb_id = fb->base.id;
2646 list_add(&fb->filp_head, &file_priv->fbs);
2647 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2648 mutex_unlock(&file_priv->fbs_lock);
2653 static int format_check(const struct drm_mode_fb_cmd2 *r)
2655 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2659 case DRM_FORMAT_RGB332:
2660 case DRM_FORMAT_BGR233:
2661 case DRM_FORMAT_XRGB4444:
2662 case DRM_FORMAT_XBGR4444:
2663 case DRM_FORMAT_RGBX4444:
2664 case DRM_FORMAT_BGRX4444:
2665 case DRM_FORMAT_ARGB4444:
2666 case DRM_FORMAT_ABGR4444:
2667 case DRM_FORMAT_RGBA4444:
2668 case DRM_FORMAT_BGRA4444:
2669 case DRM_FORMAT_XRGB1555:
2670 case DRM_FORMAT_XBGR1555:
2671 case DRM_FORMAT_RGBX5551:
2672 case DRM_FORMAT_BGRX5551:
2673 case DRM_FORMAT_ARGB1555:
2674 case DRM_FORMAT_ABGR1555:
2675 case DRM_FORMAT_RGBA5551:
2676 case DRM_FORMAT_BGRA5551:
2677 case DRM_FORMAT_RGB565:
2678 case DRM_FORMAT_BGR565:
2679 case DRM_FORMAT_RGB888:
2680 case DRM_FORMAT_BGR888:
2681 case DRM_FORMAT_XRGB8888:
2682 case DRM_FORMAT_XBGR8888:
2683 case DRM_FORMAT_RGBX8888:
2684 case DRM_FORMAT_BGRX8888:
2685 case DRM_FORMAT_ARGB8888:
2686 case DRM_FORMAT_ABGR8888:
2687 case DRM_FORMAT_RGBA8888:
2688 case DRM_FORMAT_BGRA8888:
2689 case DRM_FORMAT_XRGB2101010:
2690 case DRM_FORMAT_XBGR2101010:
2691 case DRM_FORMAT_RGBX1010102:
2692 case DRM_FORMAT_BGRX1010102:
2693 case DRM_FORMAT_ARGB2101010:
2694 case DRM_FORMAT_ABGR2101010:
2695 case DRM_FORMAT_RGBA1010102:
2696 case DRM_FORMAT_BGRA1010102:
2697 case DRM_FORMAT_YUYV:
2698 case DRM_FORMAT_YVYU:
2699 case DRM_FORMAT_UYVY:
2700 case DRM_FORMAT_VYUY:
2701 case DRM_FORMAT_AYUV:
2702 case DRM_FORMAT_NV12:
2703 case DRM_FORMAT_NV21:
2704 case DRM_FORMAT_NV16:
2705 case DRM_FORMAT_NV61:
2706 case DRM_FORMAT_NV24:
2707 case DRM_FORMAT_NV42:
2708 case DRM_FORMAT_YUV410:
2709 case DRM_FORMAT_YVU410:
2710 case DRM_FORMAT_YUV411:
2711 case DRM_FORMAT_YVU411:
2712 case DRM_FORMAT_YUV420:
2713 case DRM_FORMAT_YVU420:
2714 case DRM_FORMAT_YUV422:
2715 case DRM_FORMAT_YVU422:
2716 case DRM_FORMAT_YUV444:
2717 case DRM_FORMAT_YVU444:
2720 DRM_DEBUG_KMS("invalid pixel format %s\n",
2721 drm_get_format_name(r->pixel_format));
2726 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2728 int ret, hsub, vsub, num_planes, i;
2730 ret = format_check(r);
2732 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2733 drm_get_format_name(r->pixel_format));
2737 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2738 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2739 num_planes = drm_format_num_planes(r->pixel_format);
2741 if (r->width == 0 || r->width % hsub) {
2742 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2746 if (r->height == 0 || r->height % vsub) {
2747 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2751 for (i = 0; i < num_planes; i++) {
2752 unsigned int width = r->width / (i != 0 ? hsub : 1);
2753 unsigned int height = r->height / (i != 0 ? vsub : 1);
2754 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2756 if (!r->handles[i]) {
2757 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2761 if ((uint64_t) width * cpp > UINT_MAX)
2764 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2767 if (r->pitches[i] < width * cpp) {
2768 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2777 * drm_mode_addfb2 - add an FB to the graphics configuration
2778 * @dev: drm device for the ioctl
2779 * @data: data pointer for the ioctl
2780 * @file_priv: drm file for the ioctl call
2782 * Add a new FB to the specified CRTC, given a user request with format. This is
2783 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2784 * and uses fourcc codes as pixel format specifiers.
2786 * Called by the user via ioctl.
2789 * Zero on success, errno on failure.
2791 int drm_mode_addfb2(struct drm_device *dev,
2792 void *data, struct drm_file *file_priv)
2794 struct drm_mode_fb_cmd2 *r = data;
2795 struct drm_mode_config *config = &dev->mode_config;
2796 struct drm_framebuffer *fb;
2799 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2802 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2803 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2807 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2808 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2809 r->width, config->min_width, config->max_width);
2812 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2813 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2814 r->height, config->min_height, config->max_height);
2818 ret = framebuffer_check(r);
2822 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2824 DRM_DEBUG_KMS("could not create framebuffer\n");
2828 mutex_lock(&file_priv->fbs_lock);
2829 r->fb_id = fb->base.id;
2830 list_add(&fb->filp_head, &file_priv->fbs);
2831 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2832 mutex_unlock(&file_priv->fbs_lock);
2839 * drm_mode_rmfb - remove an FB from the configuration
2840 * @dev: drm device for the ioctl
2841 * @data: data pointer for the ioctl
2842 * @file_priv: drm file for the ioctl call
2844 * Remove the FB specified by the user.
2846 * Called by the user via ioctl.
2849 * Zero on success, errno on failure.
2851 int drm_mode_rmfb(struct drm_device *dev,
2852 void *data, struct drm_file *file_priv)
2854 struct drm_framebuffer *fb = NULL;
2855 struct drm_framebuffer *fbl = NULL;
2856 uint32_t *id = data;
2859 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2862 mutex_lock(&file_priv->fbs_lock);
2863 mutex_lock(&dev->mode_config.fb_lock);
2864 fb = __drm_framebuffer_lookup(dev, *id);
2868 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2874 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2875 __drm_framebuffer_unregister(dev, fb);
2877 list_del_init(&fb->filp_head);
2878 mutex_unlock(&dev->mode_config.fb_lock);
2879 mutex_unlock(&file_priv->fbs_lock);
2881 drm_framebuffer_remove(fb);
2886 mutex_unlock(&dev->mode_config.fb_lock);
2887 mutex_unlock(&file_priv->fbs_lock);
2893 * drm_mode_getfb - get FB info
2894 * @dev: drm device for the ioctl
2895 * @data: data pointer for the ioctl
2896 * @file_priv: drm file for the ioctl call
2898 * Lookup the FB given its ID and return info about it.
2900 * Called by the user via ioctl.
2903 * Zero on success, errno on failure.
2905 int drm_mode_getfb(struct drm_device *dev,
2906 void *data, struct drm_file *file_priv)
2908 struct drm_mode_fb_cmd *r = data;
2909 struct drm_framebuffer *fb;
2912 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2915 fb = drm_framebuffer_lookup(dev, r->fb_id);
2919 r->height = fb->height;
2920 r->width = fb->width;
2921 r->depth = fb->depth;
2922 r->bpp = fb->bits_per_pixel;
2923 r->pitch = fb->pitches[0];
2924 if (fb->funcs->create_handle) {
2925 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2926 drm_is_control_client(file_priv)) {
2927 ret = fb->funcs->create_handle(fb, file_priv,
2930 /* GET_FB() is an unprivileged ioctl so we must not
2931 * return a buffer-handle to non-master processes! For
2932 * backwards-compatibility reasons, we cannot make
2933 * GET_FB() privileged, so just return an invalid handle
2934 * for non-masters. */
2942 drm_framebuffer_unreference(fb);
2948 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2949 * @dev: drm device for the ioctl
2950 * @data: data pointer for the ioctl
2951 * @file_priv: drm file for the ioctl call
2953 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2954 * rectangle list. Generic userspace which does frontbuffer rendering must call
2955 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2956 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2958 * Modesetting drivers which always update the frontbuffer do not need to
2959 * implement the corresponding ->dirty framebuffer callback.
2961 * Called by the user via ioctl.
2964 * Zero on success, errno on failure.
2966 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2967 void *data, struct drm_file *file_priv)
2969 struct drm_clip_rect __user *clips_ptr;
2970 struct drm_clip_rect *clips = NULL;
2971 struct drm_mode_fb_dirty_cmd *r = data;
2972 struct drm_framebuffer *fb;
2977 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2980 fb = drm_framebuffer_lookup(dev, r->fb_id);
2984 num_clips = r->num_clips;
2985 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2987 if (!num_clips != !clips_ptr) {
2992 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2994 /* If userspace annotates copy, clips must come in pairs */
2995 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3000 if (num_clips && clips_ptr) {
3001 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3005 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3011 ret = copy_from_user(clips, clips_ptr,
3012 num_clips * sizeof(*clips));
3019 if (fb->funcs->dirty) {
3020 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3029 drm_framebuffer_unreference(fb);
3036 * drm_fb_release - remove and free the FBs on this file
3037 * @priv: drm file for the ioctl
3039 * Destroy all the FBs associated with @filp.
3041 * Called by the user via ioctl.
3044 * Zero on success, errno on failure.
3046 void drm_fb_release(struct drm_file *priv)
3048 struct drm_device *dev = priv->minor->dev;
3049 struct drm_framebuffer *fb, *tfb;
3051 mutex_lock(&priv->fbs_lock);
3052 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3054 mutex_lock(&dev->mode_config.fb_lock);
3055 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3056 __drm_framebuffer_unregister(dev, fb);
3057 mutex_unlock(&dev->mode_config.fb_lock);
3059 list_del_init(&fb->filp_head);
3061 /* This will also drop the fpriv->fbs reference. */
3062 drm_framebuffer_remove(fb);
3064 mutex_unlock(&priv->fbs_lock);
3068 * drm_property_create - create a new property type
3070 * @flags: flags specifying the property type
3071 * @name: name of the property
3072 * @num_values: number of pre-defined values
3074 * This creates a new generic drm property which can then be attached to a drm
3075 * object with drm_object_attach_property. The returned property object must be
3076 * freed with drm_property_destroy.
3079 * A pointer to the newly created property on success, NULL on failure.
3081 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3082 const char *name, int num_values)
3084 struct drm_property *property = NULL;
3087 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3091 property->dev = dev;
3094 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3095 if (!property->values)
3099 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3103 property->flags = flags;
3104 property->num_values = num_values;
3105 INIT_LIST_HEAD(&property->enum_blob_list);
3108 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3109 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3112 list_add_tail(&property->head, &dev->mode_config.property_list);
3114 WARN_ON(!drm_property_type_valid(property));
3118 kfree(property->values);
3122 EXPORT_SYMBOL(drm_property_create);
3125 * drm_property_create - create a new enumeration property type
3127 * @flags: flags specifying the property type
3128 * @name: name of the property
3129 * @props: enumeration lists with property values
3130 * @num_values: number of pre-defined values
3132 * This creates a new generic drm property which can then be attached to a drm
3133 * object with drm_object_attach_property. The returned property object must be
3134 * freed with drm_property_destroy.
3136 * Userspace is only allowed to set one of the predefined values for enumeration
3140 * A pointer to the newly created property on success, NULL on failure.
3142 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3144 const struct drm_prop_enum_list *props,
3147 struct drm_property *property;
3150 flags |= DRM_MODE_PROP_ENUM;
3152 property = drm_property_create(dev, flags, name, num_values);
3156 for (i = 0; i < num_values; i++) {
3157 ret = drm_property_add_enum(property, i,
3161 drm_property_destroy(dev, property);
3168 EXPORT_SYMBOL(drm_property_create_enum);
3171 * drm_property_create - create a new bitmask property type
3173 * @flags: flags specifying the property type
3174 * @name: name of the property
3175 * @props: enumeration lists with property bitflags
3176 * @num_values: number of pre-defined values
3178 * This creates a new generic drm property which can then be attached to a drm
3179 * object with drm_object_attach_property. The returned property object must be
3180 * freed with drm_property_destroy.
3182 * Compared to plain enumeration properties userspace is allowed to set any
3183 * or'ed together combination of the predefined property bitflag values
3186 * A pointer to the newly created property on success, NULL on failure.
3188 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3189 int flags, const char *name,
3190 const struct drm_prop_enum_list *props,
3193 struct drm_property *property;
3196 flags |= DRM_MODE_PROP_BITMASK;
3198 property = drm_property_create(dev, flags, name, num_values);
3202 for (i = 0; i < num_values; i++) {
3203 ret = drm_property_add_enum(property, i,
3207 drm_property_destroy(dev, property);
3214 EXPORT_SYMBOL(drm_property_create_bitmask);
3216 static struct drm_property *property_create_range(struct drm_device *dev,
3217 int flags, const char *name,
3218 uint64_t min, uint64_t max)
3220 struct drm_property *property;
3222 property = drm_property_create(dev, flags, name, 2);
3226 property->values[0] = min;
3227 property->values[1] = max;
3233 * drm_property_create - create a new ranged property type
3235 * @flags: flags specifying the property type
3236 * @name: name of the property
3237 * @min: minimum value of the property
3238 * @max: maximum value of the property
3240 * This creates a new generic drm property which can then be attached to a drm
3241 * object with drm_object_attach_property. The returned property object must be
3242 * freed with drm_property_destroy.
3244 * Userspace is allowed to set any interger value in the (min, max) range
3248 * A pointer to the newly created property on success, NULL on failure.
3250 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3252 uint64_t min, uint64_t max)
3254 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3257 EXPORT_SYMBOL(drm_property_create_range);
3259 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3260 int flags, const char *name,
3261 int64_t min, int64_t max)
3263 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3264 name, I642U64(min), I642U64(max));
3266 EXPORT_SYMBOL(drm_property_create_signed_range);
3268 struct drm_property *drm_property_create_object(struct drm_device *dev,
3269 int flags, const char *name, uint32_t type)
3271 struct drm_property *property;
3273 flags |= DRM_MODE_PROP_OBJECT;
3275 property = drm_property_create(dev, flags, name, 1);
3279 property->values[0] = type;
3283 EXPORT_SYMBOL(drm_property_create_object);
3286 * drm_property_add_enum - add a possible value to an enumeration property
3287 * @property: enumeration property to change
3288 * @index: index of the new enumeration
3289 * @value: value of the new enumeration
3290 * @name: symbolic name of the new enumeration
3292 * This functions adds enumerations to a property.
3294 * It's use is deprecated, drivers should use one of the more specific helpers
3295 * to directly create the property with all enumerations already attached.
3298 * Zero on success, error code on failure.
3300 int drm_property_add_enum(struct drm_property *property, int index,
3301 uint64_t value, const char *name)
3303 struct drm_property_enum *prop_enum;
3305 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3306 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3310 * Bitmask enum properties have the additional constraint of values
3313 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3317 if (!list_empty(&property->enum_blob_list)) {
3318 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3319 if (prop_enum->value == value) {
3320 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3321 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3327 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3331 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3332 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3333 prop_enum->value = value;
3335 property->values[index] = value;
3336 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3339 EXPORT_SYMBOL(drm_property_add_enum);
3342 * drm_property_destroy - destroy a drm property
3344 * @property: property to destry
3346 * This function frees a property including any attached resources like
3347 * enumeration values.
3349 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3351 struct drm_property_enum *prop_enum, *pt;
3353 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3354 list_del(&prop_enum->head);
3358 if (property->num_values)
3359 kfree(property->values);
3360 drm_mode_object_put(dev, &property->base);
3361 list_del(&property->head);
3364 EXPORT_SYMBOL(drm_property_destroy);
3367 * drm_object_attach_property - attach a property to a modeset object
3368 * @obj: drm modeset object
3369 * @property: property to attach
3370 * @init_val: initial value of the property
3372 * This attaches the given property to the modeset object with the given initial
3373 * value. Currently this function cannot fail since the properties are stored in
3374 * a statically sized array.
3376 void drm_object_attach_property(struct drm_mode_object *obj,
3377 struct drm_property *property,
3380 int count = obj->properties->count;
3382 if (count == DRM_OBJECT_MAX_PROPERTY) {
3383 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3384 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3385 "you see this message on the same object type.\n",
3390 obj->properties->ids[count] = property->base.id;
3391 obj->properties->values[count] = init_val;
3392 obj->properties->count++;
3394 EXPORT_SYMBOL(drm_object_attach_property);
3397 * drm_object_property_set_value - set the value of a property
3398 * @obj: drm mode object to set property value for
3399 * @property: property to set
3400 * @val: value the property should be set to
3402 * This functions sets a given property on a given object. This function only
3403 * changes the software state of the property, it does not call into the
3404 * driver's ->set_property callback.
3407 * Zero on success, error code on failure.
3409 int drm_object_property_set_value(struct drm_mode_object *obj,
3410 struct drm_property *property, uint64_t val)
3414 for (i = 0; i < obj->properties->count; i++) {
3415 if (obj->properties->ids[i] == property->base.id) {
3416 obj->properties->values[i] = val;
3423 EXPORT_SYMBOL(drm_object_property_set_value);
3426 * drm_object_property_get_value - retrieve the value of a property
3427 * @obj: drm mode object to get property value from
3428 * @property: property to retrieve
3429 * @val: storage for the property value
3431 * This function retrieves the softare state of the given property for the given
3432 * property. Since there is no driver callback to retrieve the current property
3433 * value this might be out of sync with the hardware, depending upon the driver
3437 * Zero on success, error code on failure.
3439 int drm_object_property_get_value(struct drm_mode_object *obj,
3440 struct drm_property *property, uint64_t *val)
3444 for (i = 0; i < obj->properties->count; i++) {
3445 if (obj->properties->ids[i] == property->base.id) {
3446 *val = obj->properties->values[i];
3453 EXPORT_SYMBOL(drm_object_property_get_value);
3456 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3459 * @file_priv: DRM file info
3461 * This function retrieves the current value for an connectors's property.
3463 * Called by the user via ioctl.
3466 * Zero on success, errno on failure.
3468 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3469 void *data, struct drm_file *file_priv)
3471 struct drm_mode_get_property *out_resp = data;
3472 struct drm_property *property;
3475 int value_count = 0;
3478 struct drm_property_enum *prop_enum;
3479 struct drm_mode_property_enum __user *enum_ptr;
3480 struct drm_property_blob *prop_blob;
3481 uint32_t __user *blob_id_ptr;
3482 uint64_t __user *values_ptr;
3483 uint32_t __user *blob_length_ptr;
3485 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3488 drm_modeset_lock_all(dev);
3489 property = drm_property_find(dev, out_resp->prop_id);
3495 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3496 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3497 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3499 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3500 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3504 value_count = property->num_values;
3506 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3507 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3508 out_resp->flags = property->flags;
3510 if ((out_resp->count_values >= value_count) && value_count) {
3511 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3512 for (i = 0; i < value_count; i++) {
3513 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3519 out_resp->count_values = value_count;
3521 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3522 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3523 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3525 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3526 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3528 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3533 if (copy_to_user(&enum_ptr[copied].name,
3534 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3541 out_resp->count_enum_blobs = enum_count;
3544 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3545 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3547 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3548 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3550 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3551 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3556 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3564 out_resp->count_enum_blobs = blob_count;
3567 drm_modeset_unlock_all(dev);
3571 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3574 struct drm_property_blob *blob;
3577 if (!length || !data)
3580 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3584 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3590 blob->length = length;
3592 memcpy(blob->data, data, length);
3594 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3598 static void drm_property_destroy_blob(struct drm_device *dev,
3599 struct drm_property_blob *blob)
3601 drm_mode_object_put(dev, &blob->base);
3602 list_del(&blob->head);
3607 * drm_mode_getblob_ioctl - get the contents of a blob property value
3610 * @file_priv: DRM file info
3612 * This function retrieves the contents of a blob property. The value stored in
3613 * an object's blob property is just a normal modeset object id.
3615 * Called by the user via ioctl.
3618 * Zero on success, errno on failure.
3620 int drm_mode_getblob_ioctl(struct drm_device *dev,
3621 void *data, struct drm_file *file_priv)
3623 struct drm_mode_get_blob *out_resp = data;
3624 struct drm_property_blob *blob;
3626 void __user *blob_ptr;
3628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3631 drm_modeset_lock_all(dev);
3632 blob = drm_property_blob_find(dev, out_resp->blob_id);
3638 if (out_resp->length == blob->length) {
3639 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3640 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3645 out_resp->length = blob->length;
3648 drm_modeset_unlock_all(dev);
3653 * drm_mode_connector_update_edid_property - update the edid property of a connector
3654 * @connector: drm connector
3655 * @edid: new value of the edid property
3657 * This function creates a new blob modeset object and assigns its id to the
3658 * connector's edid property.
3661 * Zero on success, errno on failure.
3663 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3666 struct drm_device *dev = connector->dev;
3669 if (connector->edid_blob_ptr)
3670 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3672 /* Delete edid, when there is none. */
3674 connector->edid_blob_ptr = NULL;
3675 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3679 size = EDID_LENGTH * (1 + edid->extensions);
3680 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3682 if (!connector->edid_blob_ptr)
3685 ret = drm_object_property_set_value(&connector->base,
3686 dev->mode_config.edid_property,
3687 connector->edid_blob_ptr->base.id);
3691 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3693 static bool drm_property_change_is_valid(struct drm_property *property,
3696 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3699 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
3700 if (value < property->values[0] || value > property->values[1])
3703 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
3704 int64_t svalue = U642I64(value);
3705 if (svalue < U642I64(property->values[0]) ||
3706 svalue > U642I64(property->values[1]))
3709 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3711 uint64_t valid_mask = 0;
3712 for (i = 0; i < property->num_values; i++)
3713 valid_mask |= (1ULL << property->values[i]);
3714 return !(value & ~valid_mask);
3715 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
3716 /* Only the driver knows */
3718 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
3719 struct drm_mode_object *obj;
3720 /* a zero value for an object property translates to null: */
3724 * NOTE: use _object_find() directly to bypass restriction on
3725 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
3726 * object this could race against object finalization, so it
3727 * simply tells us that the object *was* valid. Which is good
3730 obj = _object_find(property->dev, value, property->values[0]);
3734 for (i = 0; i < property->num_values; i++)
3735 if (property->values[i] == value)
3742 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3745 * @file_priv: DRM file info
3747 * This function sets the current value for a connectors's property. It also
3748 * calls into a driver's ->set_property callback to update the hardware state
3750 * Called by the user via ioctl.
3753 * Zero on success, errno on failure.
3755 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3756 void *data, struct drm_file *file_priv)
3758 struct drm_mode_connector_set_property *conn_set_prop = data;
3759 struct drm_mode_obj_set_property obj_set_prop = {
3760 .value = conn_set_prop->value,
3761 .prop_id = conn_set_prop->prop_id,
3762 .obj_id = conn_set_prop->connector_id,
3763 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3766 /* It does all the locking and checking we need */
3767 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3770 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3771 struct drm_property *property,
3775 struct drm_connector *connector = obj_to_connector(obj);
3777 /* Do DPMS ourselves */
3778 if (property == connector->dev->mode_config.dpms_property) {
3779 if (connector->funcs->dpms)
3780 (*connector->funcs->dpms)(connector, (int)value);
3782 } else if (connector->funcs->set_property)
3783 ret = connector->funcs->set_property(connector, property, value);
3785 /* store the property value if successful */
3787 drm_object_property_set_value(&connector->base, property, value);
3791 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3792 struct drm_property *property,
3796 struct drm_crtc *crtc = obj_to_crtc(obj);
3798 if (crtc->funcs->set_property)
3799 ret = crtc->funcs->set_property(crtc, property, value);
3801 drm_object_property_set_value(obj, property, value);
3806 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3807 struct drm_property *property,
3811 struct drm_plane *plane = obj_to_plane(obj);
3813 if (plane->funcs->set_property)
3814 ret = plane->funcs->set_property(plane, property, value);
3816 drm_object_property_set_value(obj, property, value);
3822 * drm_mode_getproperty_ioctl - get the current value of a object's property
3825 * @file_priv: DRM file info
3827 * This function retrieves the current value for an object's property. Compared
3828 * to the connector specific ioctl this one is extended to also work on crtc and
3831 * Called by the user via ioctl.
3834 * Zero on success, errno on failure.
3836 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3837 struct drm_file *file_priv)
3839 struct drm_mode_obj_get_properties *arg = data;
3840 struct drm_mode_object *obj;
3844 int props_count = 0;
3845 uint32_t __user *props_ptr;
3846 uint64_t __user *prop_values_ptr;
3848 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3851 drm_modeset_lock_all(dev);
3853 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3858 if (!obj->properties) {
3863 props_count = obj->properties->count;
3865 /* This ioctl is called twice, once to determine how much space is
3866 * needed, and the 2nd time to fill it. */
3867 if ((arg->count_props >= props_count) && props_count) {
3869 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3870 prop_values_ptr = (uint64_t __user *)(unsigned long)
3871 (arg->prop_values_ptr);
3872 for (i = 0; i < props_count; i++) {
3873 if (put_user(obj->properties->ids[i],
3874 props_ptr + copied)) {
3878 if (put_user(obj->properties->values[i],
3879 prop_values_ptr + copied)) {
3886 arg->count_props = props_count;
3888 drm_modeset_unlock_all(dev);
3893 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3896 * @file_priv: DRM file info
3898 * This function sets the current value for an object's property. It also calls
3899 * into a driver's ->set_property callback to update the hardware state.
3900 * Compared to the connector specific ioctl this one is extended to also work on
3901 * crtc and plane objects.
3903 * Called by the user via ioctl.
3906 * Zero on success, errno on failure.
3908 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3909 struct drm_file *file_priv)
3911 struct drm_mode_obj_set_property *arg = data;
3912 struct drm_mode_object *arg_obj;
3913 struct drm_mode_object *prop_obj;
3914 struct drm_property *property;
3918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3921 drm_modeset_lock_all(dev);
3923 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3928 if (!arg_obj->properties)
3931 for (i = 0; i < arg_obj->properties->count; i++)
3932 if (arg_obj->properties->ids[i] == arg->prop_id)
3935 if (i == arg_obj->properties->count)
3938 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3939 DRM_MODE_OBJECT_PROPERTY);
3944 property = obj_to_property(prop_obj);
3946 if (!drm_property_change_is_valid(property, arg->value))
3949 switch (arg_obj->type) {
3950 case DRM_MODE_OBJECT_CONNECTOR:
3951 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3954 case DRM_MODE_OBJECT_CRTC:
3955 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3957 case DRM_MODE_OBJECT_PLANE:
3958 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3963 drm_modeset_unlock_all(dev);
3968 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3969 * @connector: connector to attach
3970 * @encoder: encoder to attach @connector to
3972 * This function links up a connector to an encoder. Note that the routing
3973 * restrictions between encoders and crtcs are exposed to userspace through the
3974 * possible_clones and possible_crtcs bitmasks.
3977 * Zero on success, errno on failure.
3979 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3980 struct drm_encoder *encoder)
3984 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3985 if (connector->encoder_ids[i] == 0) {
3986 connector->encoder_ids[i] = encoder->base.id;
3992 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3995 * drm_mode_crtc_set_gamma_size - set the gamma table size
3996 * @crtc: CRTC to set the gamma table size for
3997 * @gamma_size: size of the gamma table
3999 * Drivers which support gamma tables should set this to the supported gamma
4000 * table size when initializing the CRTC. Currently the drm core only supports a
4001 * fixed gamma table size.
4004 * Zero on success, errno on failure.
4006 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4009 crtc->gamma_size = gamma_size;
4011 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
4012 if (!crtc->gamma_store) {
4013 crtc->gamma_size = 0;
4019 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4022 * drm_mode_gamma_set_ioctl - set the gamma table
4025 * @file_priv: DRM file info
4027 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4028 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4030 * Called by the user via ioctl.
4033 * Zero on success, errno on failure.
4035 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4036 void *data, struct drm_file *file_priv)
4038 struct drm_mode_crtc_lut *crtc_lut = data;
4039 struct drm_crtc *crtc;
4040 void *r_base, *g_base, *b_base;
4044 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4047 drm_modeset_lock_all(dev);
4048 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4054 if (crtc->funcs->gamma_set == NULL) {
4059 /* memcpy into gamma store */
4060 if (crtc_lut->gamma_size != crtc->gamma_size) {
4065 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4066 r_base = crtc->gamma_store;
4067 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4072 g_base = r_base + size;
4073 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4078 b_base = g_base + size;
4079 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4084 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4087 drm_modeset_unlock_all(dev);
4093 * drm_mode_gamma_get_ioctl - get the gamma table
4096 * @file_priv: DRM file info
4098 * Copy the current gamma table into the storage provided. This also provides
4099 * the gamma table size the driver expects, which can be used to size the
4100 * allocated storage.
4102 * Called by the user via ioctl.
4105 * Zero on success, errno on failure.
4107 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4108 void *data, struct drm_file *file_priv)
4110 struct drm_mode_crtc_lut *crtc_lut = data;
4111 struct drm_crtc *crtc;
4112 void *r_base, *g_base, *b_base;
4116 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4119 drm_modeset_lock_all(dev);
4120 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4126 /* memcpy into gamma store */
4127 if (crtc_lut->gamma_size != crtc->gamma_size) {
4132 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4133 r_base = crtc->gamma_store;
4134 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4139 g_base = r_base + size;
4140 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4145 b_base = g_base + size;
4146 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4151 drm_modeset_unlock_all(dev);
4156 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4159 * @file_priv: DRM file info
4161 * This schedules an asynchronous update on a given CRTC, called page flip.
4162 * Optionally a drm event is generated to signal the completion of the event.
4163 * Generic drivers cannot assume that a pageflip with changed framebuffer
4164 * properties (including driver specific metadata like tiling layout) will work,
4165 * but some drivers support e.g. pixel format changes through the pageflip
4168 * Called by the user via ioctl.
4171 * Zero on success, errno on failure.
4173 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4174 void *data, struct drm_file *file_priv)
4176 struct drm_mode_crtc_page_flip *page_flip = data;
4177 struct drm_crtc *crtc;
4178 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4179 struct drm_pending_vblank_event *e = NULL;
4180 unsigned long flags;
4183 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4184 page_flip->reserved != 0)
4187 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4190 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4194 mutex_lock(&crtc->mutex);
4195 if (crtc->primary->fb == NULL) {
4196 /* The framebuffer is currently unbound, presumably
4197 * due to a hotplug event, that userspace has not
4204 if (crtc->funcs->page_flip == NULL)
4207 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4213 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4217 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4218 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4223 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4225 spin_lock_irqsave(&dev->event_lock, flags);
4226 if (file_priv->event_space < sizeof e->event) {
4227 spin_unlock_irqrestore(&dev->event_lock, flags);
4230 file_priv->event_space -= sizeof e->event;
4231 spin_unlock_irqrestore(&dev->event_lock, flags);
4233 e = kzalloc(sizeof *e, GFP_KERNEL);
4235 spin_lock_irqsave(&dev->event_lock, flags);
4236 file_priv->event_space += sizeof e->event;
4237 spin_unlock_irqrestore(&dev->event_lock, flags);
4241 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4242 e->event.base.length = sizeof e->event;
4243 e->event.user_data = page_flip->user_data;
4244 e->base.event = &e->event.base;
4245 e->base.file_priv = file_priv;
4247 (void (*) (struct drm_pending_event *)) kfree;
4250 old_fb = crtc->primary->fb;
4251 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4253 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4254 spin_lock_irqsave(&dev->event_lock, flags);
4255 file_priv->event_space += sizeof e->event;
4256 spin_unlock_irqrestore(&dev->event_lock, flags);
4259 /* Keep the old fb, don't unref it. */
4263 * Warn if the driver hasn't properly updated the crtc->fb
4264 * field to reflect that the new framebuffer is now used.
4265 * Failing to do so will screw with the reference counting
4268 WARN_ON(crtc->primary->fb != fb);
4269 /* Unref only the old framebuffer. */
4275 drm_framebuffer_unreference(fb);
4277 drm_framebuffer_unreference(old_fb);
4278 mutex_unlock(&crtc->mutex);
4284 * drm_mode_config_reset - call ->reset callbacks
4287 * This functions calls all the crtc's, encoder's and connector's ->reset
4288 * callback. Drivers can use this in e.g. their driver load or resume code to
4289 * reset hardware and software state.
4291 void drm_mode_config_reset(struct drm_device *dev)
4293 struct drm_crtc *crtc;
4294 struct drm_encoder *encoder;
4295 struct drm_connector *connector;
4297 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4298 if (crtc->funcs->reset)
4299 crtc->funcs->reset(crtc);
4301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4302 if (encoder->funcs->reset)
4303 encoder->funcs->reset(encoder);
4305 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4306 connector->status = connector_status_unknown;
4308 if (connector->funcs->reset)
4309 connector->funcs->reset(connector);
4312 EXPORT_SYMBOL(drm_mode_config_reset);
4315 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4318 * @file_priv: DRM file info
4320 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4321 * TTM or something else entirely) and returns the resulting buffer handle. This
4322 * handle can then be wrapped up into a framebuffer modeset object.
4324 * Note that userspace is not allowed to use such objects for render
4325 * acceleration - drivers must create their own private ioctls for such a use
4328 * Called by the user via ioctl.
4331 * Zero on success, errno on failure.
4333 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4334 void *data, struct drm_file *file_priv)
4336 struct drm_mode_create_dumb *args = data;
4337 u32 cpp, stride, size;
4339 if (!dev->driver->dumb_create)
4341 if (!args->width || !args->height || !args->bpp)
4344 /* overflow checks for 32bit size calculations */
4345 cpp = DIV_ROUND_UP(args->bpp, 8);
4346 if (cpp > 0xffffffffU / args->width)
4348 stride = cpp * args->width;
4349 if (args->height > 0xffffffffU / stride)
4352 /* test for wrap-around */
4353 size = args->height * stride;
4354 if (PAGE_ALIGN(size) == 0)
4357 return dev->driver->dumb_create(file_priv, dev, args);
4361 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4364 * @file_priv: DRM file info
4366 * Allocate an offset in the drm device node's address space to be able to
4367 * memory map a dumb buffer.
4369 * Called by the user via ioctl.
4372 * Zero on success, errno on failure.
4374 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4375 void *data, struct drm_file *file_priv)
4377 struct drm_mode_map_dumb *args = data;
4379 /* call driver ioctl to get mmap offset */
4380 if (!dev->driver->dumb_map_offset)
4383 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4387 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4390 * @file_priv: DRM file info
4392 * This destroys the userspace handle for the given dumb backing storage buffer.
4393 * Since buffer objects must be reference counted in the kernel a buffer object
4394 * won't be immediately freed if a framebuffer modeset object still uses it.
4396 * Called by the user via ioctl.
4399 * Zero on success, errno on failure.
4401 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4402 void *data, struct drm_file *file_priv)
4404 struct drm_mode_destroy_dumb *args = data;
4406 if (!dev->driver->dumb_destroy)
4409 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4413 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4414 * @format: pixel format (DRM_FORMAT_*)
4415 * @depth: storage for the depth value
4416 * @bpp: storage for the bpp value
4418 * This only supports RGB formats here for compat with code that doesn't use
4419 * pixel formats directly yet.
4421 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4426 case DRM_FORMAT_RGB332:
4427 case DRM_FORMAT_BGR233:
4431 case DRM_FORMAT_XRGB1555:
4432 case DRM_FORMAT_XBGR1555:
4433 case DRM_FORMAT_RGBX5551:
4434 case DRM_FORMAT_BGRX5551:
4435 case DRM_FORMAT_ARGB1555:
4436 case DRM_FORMAT_ABGR1555:
4437 case DRM_FORMAT_RGBA5551:
4438 case DRM_FORMAT_BGRA5551:
4442 case DRM_FORMAT_RGB565:
4443 case DRM_FORMAT_BGR565:
4447 case DRM_FORMAT_RGB888:
4448 case DRM_FORMAT_BGR888:
4452 case DRM_FORMAT_XRGB8888:
4453 case DRM_FORMAT_XBGR8888:
4454 case DRM_FORMAT_RGBX8888:
4455 case DRM_FORMAT_BGRX8888:
4459 case DRM_FORMAT_XRGB2101010:
4460 case DRM_FORMAT_XBGR2101010:
4461 case DRM_FORMAT_RGBX1010102:
4462 case DRM_FORMAT_BGRX1010102:
4463 case DRM_FORMAT_ARGB2101010:
4464 case DRM_FORMAT_ABGR2101010:
4465 case DRM_FORMAT_RGBA1010102:
4466 case DRM_FORMAT_BGRA1010102:
4470 case DRM_FORMAT_ARGB8888:
4471 case DRM_FORMAT_ABGR8888:
4472 case DRM_FORMAT_RGBA8888:
4473 case DRM_FORMAT_BGRA8888:
4478 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4479 drm_get_format_name(format));
4485 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4488 * drm_format_num_planes - get the number of planes for format
4489 * @format: pixel format (DRM_FORMAT_*)
4492 * The number of planes used by the specified pixel format.
4494 int drm_format_num_planes(uint32_t format)
4497 case DRM_FORMAT_YUV410:
4498 case DRM_FORMAT_YVU410:
4499 case DRM_FORMAT_YUV411:
4500 case DRM_FORMAT_YVU411:
4501 case DRM_FORMAT_YUV420:
4502 case DRM_FORMAT_YVU420:
4503 case DRM_FORMAT_YUV422:
4504 case DRM_FORMAT_YVU422:
4505 case DRM_FORMAT_YUV444:
4506 case DRM_FORMAT_YVU444:
4508 case DRM_FORMAT_NV12:
4509 case DRM_FORMAT_NV21:
4510 case DRM_FORMAT_NV16:
4511 case DRM_FORMAT_NV61:
4512 case DRM_FORMAT_NV24:
4513 case DRM_FORMAT_NV42:
4519 EXPORT_SYMBOL(drm_format_num_planes);
4522 * drm_format_plane_cpp - determine the bytes per pixel value
4523 * @format: pixel format (DRM_FORMAT_*)
4524 * @plane: plane index
4527 * The bytes per pixel value for the specified plane.
4529 int drm_format_plane_cpp(uint32_t format, int plane)
4534 if (plane >= drm_format_num_planes(format))
4538 case DRM_FORMAT_YUYV:
4539 case DRM_FORMAT_YVYU:
4540 case DRM_FORMAT_UYVY:
4541 case DRM_FORMAT_VYUY:
4543 case DRM_FORMAT_NV12:
4544 case DRM_FORMAT_NV21:
4545 case DRM_FORMAT_NV16:
4546 case DRM_FORMAT_NV61:
4547 case DRM_FORMAT_NV24:
4548 case DRM_FORMAT_NV42:
4549 return plane ? 2 : 1;
4550 case DRM_FORMAT_YUV410:
4551 case DRM_FORMAT_YVU410:
4552 case DRM_FORMAT_YUV411:
4553 case DRM_FORMAT_YVU411:
4554 case DRM_FORMAT_YUV420:
4555 case DRM_FORMAT_YVU420:
4556 case DRM_FORMAT_YUV422:
4557 case DRM_FORMAT_YVU422:
4558 case DRM_FORMAT_YUV444:
4559 case DRM_FORMAT_YVU444:
4562 drm_fb_get_bpp_depth(format, &depth, &bpp);
4566 EXPORT_SYMBOL(drm_format_plane_cpp);
4569 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4570 * @format: pixel format (DRM_FORMAT_*)
4573 * The horizontal chroma subsampling factor for the
4574 * specified pixel format.
4576 int drm_format_horz_chroma_subsampling(uint32_t format)
4579 case DRM_FORMAT_YUV411:
4580 case DRM_FORMAT_YVU411:
4581 case DRM_FORMAT_YUV410:
4582 case DRM_FORMAT_YVU410:
4584 case DRM_FORMAT_YUYV:
4585 case DRM_FORMAT_YVYU:
4586 case DRM_FORMAT_UYVY:
4587 case DRM_FORMAT_VYUY:
4588 case DRM_FORMAT_NV12:
4589 case DRM_FORMAT_NV21:
4590 case DRM_FORMAT_NV16:
4591 case DRM_FORMAT_NV61:
4592 case DRM_FORMAT_YUV422:
4593 case DRM_FORMAT_YVU422:
4594 case DRM_FORMAT_YUV420:
4595 case DRM_FORMAT_YVU420:
4601 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4604 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4605 * @format: pixel format (DRM_FORMAT_*)
4608 * The vertical chroma subsampling factor for the
4609 * specified pixel format.
4611 int drm_format_vert_chroma_subsampling(uint32_t format)
4614 case DRM_FORMAT_YUV410:
4615 case DRM_FORMAT_YVU410:
4617 case DRM_FORMAT_YUV420:
4618 case DRM_FORMAT_YVU420:
4619 case DRM_FORMAT_NV12:
4620 case DRM_FORMAT_NV21:
4626 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4629 * drm_mode_config_init - initialize DRM mode_configuration structure
4632 * Initialize @dev's mode_config structure, used for tracking the graphics
4633 * configuration of @dev.
4635 * Since this initializes the modeset locks, no locking is possible. Which is no
4636 * problem, since this should happen single threaded at init time. It is the
4637 * driver's problem to ensure this guarantee.
4640 void drm_mode_config_init(struct drm_device *dev)
4642 mutex_init(&dev->mode_config.mutex);
4643 mutex_init(&dev->mode_config.idr_mutex);
4644 mutex_init(&dev->mode_config.fb_lock);
4645 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4646 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4647 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4648 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4649 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4650 INIT_LIST_HEAD(&dev->mode_config.property_list);
4651 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4652 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4653 idr_init(&dev->mode_config.crtc_idr);
4655 drm_modeset_lock_all(dev);
4656 drm_mode_create_standard_connector_properties(dev);
4657 drm_mode_create_standard_plane_properties(dev);
4658 drm_modeset_unlock_all(dev);
4660 /* Just to be sure */
4661 dev->mode_config.num_fb = 0;
4662 dev->mode_config.num_connector = 0;
4663 dev->mode_config.num_crtc = 0;
4664 dev->mode_config.num_encoder = 0;
4665 dev->mode_config.num_overlay_plane = 0;
4666 dev->mode_config.num_total_plane = 0;
4668 EXPORT_SYMBOL(drm_mode_config_init);
4671 * drm_mode_config_cleanup - free up DRM mode_config info
4674 * Free up all the connectors and CRTCs associated with this DRM device, then
4675 * free up the framebuffers and associated buffer objects.
4677 * Note that since this /should/ happen single-threaded at driver/device
4678 * teardown time, no locking is required. It's the driver's job to ensure that
4679 * this guarantee actually holds true.
4681 * FIXME: cleanup any dangling user buffer objects too
4683 void drm_mode_config_cleanup(struct drm_device *dev)
4685 struct drm_connector *connector, *ot;
4686 struct drm_crtc *crtc, *ct;
4687 struct drm_encoder *encoder, *enct;
4688 struct drm_bridge *bridge, *brt;
4689 struct drm_framebuffer *fb, *fbt;
4690 struct drm_property *property, *pt;
4691 struct drm_property_blob *blob, *bt;
4692 struct drm_plane *plane, *plt;
4694 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4696 encoder->funcs->destroy(encoder);
4699 list_for_each_entry_safe(bridge, brt,
4700 &dev->mode_config.bridge_list, head) {
4701 bridge->funcs->destroy(bridge);
4704 list_for_each_entry_safe(connector, ot,
4705 &dev->mode_config.connector_list, head) {
4706 connector->funcs->destroy(connector);
4709 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4711 drm_property_destroy(dev, property);
4714 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4716 drm_property_destroy_blob(dev, blob);
4720 * Single-threaded teardown context, so it's not required to grab the
4721 * fb_lock to protect against concurrent fb_list access. Contrary, it
4722 * would actually deadlock with the drm_framebuffer_cleanup function.
4724 * Also, if there are any framebuffers left, that's a driver leak now,
4725 * so politely WARN about this.
4727 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4728 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4729 drm_framebuffer_remove(fb);
4732 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4734 plane->funcs->destroy(plane);
4737 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4738 crtc->funcs->destroy(crtc);
4741 idr_destroy(&dev->mode_config.crtc_idr);
4743 EXPORT_SYMBOL(drm_mode_config_cleanup);