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" },
232 static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
234 { SubPixelUnknown, "Unknown" },
235 { SubPixelHorizontalRGB, "Horizontal RGB" },
236 { SubPixelHorizontalBGR, "Horizontal BGR" },
237 { SubPixelVerticalRGB, "Vertical RGB" },
238 { SubPixelVerticalBGR, "Vertical BGR" },
239 { SubPixelNone, "None" },
242 void drm_connector_ida_init(void)
246 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
247 ida_init(&drm_connector_enum_list[i].ida);
250 void drm_connector_ida_destroy(void)
254 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
255 ida_destroy(&drm_connector_enum_list[i].ida);
259 * drm_get_encoder_name - return a string for encoder
260 * @encoder: encoder to compute name of
262 * Note that the buffer used by this function is globally shared and owned by
263 * the function itself.
265 * FIXME: This isn't really multithreading safe.
267 const char *drm_get_encoder_name(const struct drm_encoder *encoder)
271 snprintf(buf, 32, "%s-%d",
272 drm_encoder_enum_list[encoder->encoder_type].name,
276 EXPORT_SYMBOL(drm_get_encoder_name);
279 * drm_get_connector_name - return a string for connector
280 * @connector: connector to compute name of
282 * Note that the buffer used by this function is globally shared and owned by
283 * the function itself.
285 * FIXME: This isn't really multithreading safe.
287 const char *drm_get_connector_name(const struct drm_connector *connector)
291 snprintf(buf, 32, "%s-%d",
292 drm_connector_enum_list[connector->connector_type].name,
293 connector->connector_type_id);
296 EXPORT_SYMBOL(drm_get_connector_name);
299 * drm_get_connector_status_name - return a string for connector status
300 * @status: connector status to compute name of
302 * In contrast to the other drm_get_*_name functions this one here returns a
303 * const pointer and hence is threadsafe.
305 const char *drm_get_connector_status_name(enum drm_connector_status status)
307 if (status == connector_status_connected)
309 else if (status == connector_status_disconnected)
310 return "disconnected";
314 EXPORT_SYMBOL(drm_get_connector_status_name);
317 * drm_get_subpixel_order_name - return a string for a given subpixel enum
318 * @order: enum of subpixel_order
320 * Note you could abuse this and return something out of bounds, but that
321 * would be a caller error. No unscrubbed user data should make it here.
323 const char *drm_get_subpixel_order_name(enum subpixel_order order)
325 return drm_subpixel_enum_list[order].name;
327 EXPORT_SYMBOL(drm_get_subpixel_order_name);
329 static char printable_char(int c)
331 return isascii(c) && isprint(c) ? c : '?';
335 * drm_get_format_name - return a string for drm fourcc format
336 * @format: format to compute name of
338 * Note that the buffer used by this function is globally shared and owned by
339 * the function itself.
341 * FIXME: This isn't really multithreading safe.
343 const char *drm_get_format_name(uint32_t format)
347 snprintf(buf, sizeof(buf),
348 "%c%c%c%c %s-endian (0x%08x)",
349 printable_char(format & 0xff),
350 printable_char((format >> 8) & 0xff),
351 printable_char((format >> 16) & 0xff),
352 printable_char((format >> 24) & 0x7f),
353 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
358 EXPORT_SYMBOL(drm_get_format_name);
361 * drm_mode_object_get - allocate a new modeset identifier
363 * @obj: object pointer, used to generate unique ID
364 * @obj_type: object type
366 * Create a unique identifier based on @ptr in @dev's identifier space. Used
367 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
368 * modeset identifiers are _not_ reference counted. Hence don't use this for
369 * reference counted modeset objects like framebuffers.
372 * New unique (relative to other objects in @dev) integer identifier for the
375 int drm_mode_object_get(struct drm_device *dev,
376 struct drm_mode_object *obj, uint32_t obj_type)
380 mutex_lock(&dev->mode_config.idr_mutex);
381 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
384 * Set up the object linking under the protection of the idr
385 * lock so that other users can't see inconsistent state.
388 obj->type = obj_type;
390 mutex_unlock(&dev->mode_config.idr_mutex);
392 return ret < 0 ? ret : 0;
396 * drm_mode_object_put - free a modeset identifer
398 * @object: object to free
400 * Free @id from @dev's unique identifier pool. Note that despite the _get
401 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
402 * for reference counted modeset objects like framebuffers.
404 void drm_mode_object_put(struct drm_device *dev,
405 struct drm_mode_object *object)
407 mutex_lock(&dev->mode_config.idr_mutex);
408 idr_remove(&dev->mode_config.crtc_idr, object->id);
409 mutex_unlock(&dev->mode_config.idr_mutex);
413 * drm_mode_object_find - look up a drm object with static lifetime
415 * @id: id of the mode object
416 * @type: type of the mode object
418 * Note that framebuffers cannot be looked up with this functions - since those
419 * are reference counted, they need special treatment.
421 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
422 uint32_t id, uint32_t type)
424 struct drm_mode_object *obj = NULL;
426 /* Framebuffers are reference counted and need their own lookup
428 WARN_ON(type == DRM_MODE_OBJECT_FB);
430 mutex_lock(&dev->mode_config.idr_mutex);
431 obj = idr_find(&dev->mode_config.crtc_idr, id);
432 if (!obj || (obj->type != type) || (obj->id != id))
434 mutex_unlock(&dev->mode_config.idr_mutex);
438 EXPORT_SYMBOL(drm_mode_object_find);
441 * drm_framebuffer_init - initialize a framebuffer
443 * @fb: framebuffer to be initialized
444 * @funcs: ... with these functions
446 * Allocates an ID for the framebuffer's parent mode object, sets its mode
447 * functions & device file and adds it to the master fd list.
450 * This functions publishes the fb and makes it available for concurrent access
451 * by other users. Which means by this point the fb _must_ be fully set up -
452 * since all the fb attributes are invariant over its lifetime, no further
453 * locking but only correct reference counting is required.
456 * Zero on success, error code on failure.
458 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
459 const struct drm_framebuffer_funcs *funcs)
463 mutex_lock(&dev->mode_config.fb_lock);
464 kref_init(&fb->refcount);
465 INIT_LIST_HEAD(&fb->filp_head);
469 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
473 /* Grab the idr reference. */
474 drm_framebuffer_reference(fb);
476 dev->mode_config.num_fb++;
477 list_add(&fb->head, &dev->mode_config.fb_list);
479 mutex_unlock(&dev->mode_config.fb_lock);
483 EXPORT_SYMBOL(drm_framebuffer_init);
485 static void drm_framebuffer_free(struct kref *kref)
487 struct drm_framebuffer *fb =
488 container_of(kref, struct drm_framebuffer, refcount);
489 fb->funcs->destroy(fb);
492 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
495 struct drm_mode_object *obj = NULL;
496 struct drm_framebuffer *fb;
498 mutex_lock(&dev->mode_config.idr_mutex);
499 obj = idr_find(&dev->mode_config.crtc_idr, id);
500 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
504 mutex_unlock(&dev->mode_config.idr_mutex);
510 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
512 * @id: id of the fb object
514 * If successful, this grabs an additional reference to the framebuffer -
515 * callers need to make sure to eventually unreference the returned framebuffer
516 * again, using @drm_framebuffer_unreference.
518 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
521 struct drm_framebuffer *fb;
523 mutex_lock(&dev->mode_config.fb_lock);
524 fb = __drm_framebuffer_lookup(dev, id);
526 drm_framebuffer_reference(fb);
527 mutex_unlock(&dev->mode_config.fb_lock);
531 EXPORT_SYMBOL(drm_framebuffer_lookup);
534 * drm_framebuffer_unreference - unref a framebuffer
535 * @fb: framebuffer to unref
537 * This functions decrements the fb's refcount and frees it if it drops to zero.
539 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
541 DRM_DEBUG("FB ID: %d\n", fb->base.id);
542 kref_put(&fb->refcount, drm_framebuffer_free);
544 EXPORT_SYMBOL(drm_framebuffer_unreference);
547 * drm_framebuffer_reference - incr the fb refcnt
550 * This functions increments the fb's refcount.
552 void drm_framebuffer_reference(struct drm_framebuffer *fb)
554 DRM_DEBUG("FB ID: %d\n", fb->base.id);
555 kref_get(&fb->refcount);
557 EXPORT_SYMBOL(drm_framebuffer_reference);
559 static void drm_framebuffer_free_bug(struct kref *kref)
564 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
566 DRM_DEBUG("FB ID: %d\n", fb->base.id);
567 kref_put(&fb->refcount, drm_framebuffer_free_bug);
570 /* dev->mode_config.fb_lock must be held! */
571 static void __drm_framebuffer_unregister(struct drm_device *dev,
572 struct drm_framebuffer *fb)
574 mutex_lock(&dev->mode_config.idr_mutex);
575 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
576 mutex_unlock(&dev->mode_config.idr_mutex);
580 __drm_framebuffer_unreference(fb);
584 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
585 * @fb: fb to unregister
587 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
588 * those used for fbdev. Note that the caller must hold a reference of it's own,
589 * i.e. the object may not be destroyed through this call (since it'll lead to a
590 * locking inversion).
592 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
594 struct drm_device *dev = fb->dev;
596 mutex_lock(&dev->mode_config.fb_lock);
597 /* Mark fb as reaped and drop idr ref. */
598 __drm_framebuffer_unregister(dev, fb);
599 mutex_unlock(&dev->mode_config.fb_lock);
601 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
604 * drm_framebuffer_cleanup - remove a framebuffer object
605 * @fb: framebuffer to remove
607 * Cleanup framebuffer. This function is intended to be used from the drivers
608 * ->destroy callback. It can also be used to clean up driver private
609 * framebuffers embedded into a larger structure.
611 * Note that this function does not remove the fb from active usuage - if it is
612 * still used anywhere, hilarity can ensue since userspace could call getfb on
613 * the id and get back -EINVAL. Obviously no concern at driver unload time.
615 * Also, the framebuffer will not be removed from the lookup idr - for
616 * user-created framebuffers this will happen in in the rmfb ioctl. For
617 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
618 * drm_framebuffer_unregister_private.
620 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
622 struct drm_device *dev = fb->dev;
624 mutex_lock(&dev->mode_config.fb_lock);
626 dev->mode_config.num_fb--;
627 mutex_unlock(&dev->mode_config.fb_lock);
629 EXPORT_SYMBOL(drm_framebuffer_cleanup);
632 * drm_framebuffer_remove - remove and unreference a framebuffer object
633 * @fb: framebuffer to remove
635 * Scans all the CRTCs and planes in @dev's mode_config. If they're
636 * using @fb, removes it, setting it to NULL. Then drops the reference to the
637 * passed-in framebuffer. Might take the modeset locks.
639 * Note that this function optimizes the cleanup away if the caller holds the
640 * last reference to the framebuffer. It is also guaranteed to not take the
641 * modeset locks in this case.
643 void drm_framebuffer_remove(struct drm_framebuffer *fb)
645 struct drm_device *dev = fb->dev;
646 struct drm_crtc *crtc;
647 struct drm_plane *plane;
648 struct drm_mode_set set;
651 WARN_ON(!list_empty(&fb->filp_head));
654 * drm ABI mandates that we remove any deleted framebuffers from active
655 * useage. But since most sane clients only remove framebuffers they no
656 * longer need, try to optimize this away.
658 * Since we're holding a reference ourselves, observing a refcount of 1
659 * means that we're the last holder and can skip it. Also, the refcount
660 * can never increase from 1 again, so we don't need any barriers or
663 * Note that userspace could try to race with use and instate a new
664 * usage _after_ we've cleared all current ones. End result will be an
665 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
668 if (atomic_read(&fb->refcount.refcount) > 1) {
669 drm_modeset_lock_all(dev);
670 /* remove from any CRTC */
671 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
672 if (crtc->primary->fb == fb) {
673 /* should turn off the crtc */
674 memset(&set, 0, sizeof(struct drm_mode_set));
677 ret = drm_mode_set_config_internal(&set);
679 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
683 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
685 drm_plane_force_disable(plane);
687 drm_modeset_unlock_all(dev);
690 drm_framebuffer_unreference(fb);
692 EXPORT_SYMBOL(drm_framebuffer_remove);
695 * drm_crtc_init_with_planes - Initialise a new CRTC object with
696 * specified primary and cursor planes.
698 * @crtc: CRTC object to init
699 * @primary: Primary plane for CRTC
700 * @cursor: Cursor plane for CRTC
701 * @funcs: callbacks for the new CRTC
703 * Inits a new object created as base part of a driver crtc object.
706 * Zero on success, error code on failure.
708 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
709 struct drm_plane *primary,
711 const struct drm_crtc_funcs *funcs)
717 crtc->invert_dimensions = false;
719 drm_modeset_lock_all(dev);
720 mutex_init(&crtc->mutex);
721 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
723 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
727 crtc->base.properties = &crtc->properties;
729 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
730 dev->mode_config.num_crtc++;
732 crtc->primary = primary;
734 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
737 drm_modeset_unlock_all(dev);
741 EXPORT_SYMBOL(drm_crtc_init_with_planes);
744 * drm_crtc_cleanup - Clean up the core crtc usage
745 * @crtc: CRTC to cleanup
747 * This function cleans up @crtc and removes it from the DRM mode setting
748 * core. Note that the function does *not* free the crtc structure itself,
749 * this is the responsibility of the caller.
751 void drm_crtc_cleanup(struct drm_crtc *crtc)
753 struct drm_device *dev = crtc->dev;
755 kfree(crtc->gamma_store);
756 crtc->gamma_store = NULL;
758 drm_mode_object_put(dev, &crtc->base);
759 list_del(&crtc->head);
760 dev->mode_config.num_crtc--;
762 EXPORT_SYMBOL(drm_crtc_cleanup);
765 * drm_crtc_index - find the index of a registered CRTC
766 * @crtc: CRTC to find index for
768 * Given a registered CRTC, return the index of that CRTC within a DRM
769 * device's list of CRTCs.
771 unsigned int drm_crtc_index(struct drm_crtc *crtc)
773 unsigned int index = 0;
774 struct drm_crtc *tmp;
776 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
785 EXPORT_SYMBOL(drm_crtc_index);
788 * drm_mode_remove - remove and free a mode
789 * @connector: connector list to modify
790 * @mode: mode to remove
792 * Remove @mode from @connector's mode list, then free it.
794 static void drm_mode_remove(struct drm_connector *connector,
795 struct drm_display_mode *mode)
797 list_del(&mode->head);
798 drm_mode_destroy(connector->dev, mode);
802 * drm_connector_init - Init a preallocated connector
804 * @connector: the connector to init
805 * @funcs: callbacks for this connector
806 * @connector_type: user visible type of the connector
808 * Initialises a preallocated connector. Connectors should be
809 * subclassed as part of driver connector objects.
812 * Zero on success, error code on failure.
814 int drm_connector_init(struct drm_device *dev,
815 struct drm_connector *connector,
816 const struct drm_connector_funcs *funcs,
820 struct ida *connector_ida =
821 &drm_connector_enum_list[connector_type].ida;
823 drm_modeset_lock_all(dev);
825 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
829 connector->base.properties = &connector->properties;
830 connector->dev = dev;
831 connector->funcs = funcs;
832 connector->connector_type = connector_type;
833 connector->connector_type_id =
834 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
835 if (connector->connector_type_id < 0) {
836 ret = connector->connector_type_id;
837 drm_mode_object_put(dev, &connector->base);
840 INIT_LIST_HEAD(&connector->probed_modes);
841 INIT_LIST_HEAD(&connector->modes);
842 connector->edid_blob_ptr = NULL;
843 connector->status = connector_status_unknown;
845 list_add_tail(&connector->head, &dev->mode_config.connector_list);
846 dev->mode_config.num_connector++;
848 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
849 drm_object_attach_property(&connector->base,
850 dev->mode_config.edid_property,
853 drm_object_attach_property(&connector->base,
854 dev->mode_config.dpms_property, 0);
857 drm_modeset_unlock_all(dev);
861 EXPORT_SYMBOL(drm_connector_init);
864 * drm_connector_cleanup - cleans up an initialised connector
865 * @connector: connector to cleanup
867 * Cleans up the connector but doesn't free the object.
869 void drm_connector_cleanup(struct drm_connector *connector)
871 struct drm_device *dev = connector->dev;
872 struct drm_display_mode *mode, *t;
874 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
875 drm_mode_remove(connector, mode);
877 list_for_each_entry_safe(mode, t, &connector->modes, head)
878 drm_mode_remove(connector, mode);
880 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
881 connector->connector_type_id);
883 drm_mode_object_put(dev, &connector->base);
884 list_del(&connector->head);
885 dev->mode_config.num_connector--;
887 EXPORT_SYMBOL(drm_connector_cleanup);
890 * drm_connector_unplug_all - unregister connector userspace interfaces
893 * This function unregisters all connector userspace interfaces in sysfs. Should
894 * be call when the device is disconnected, e.g. from an usb driver's
895 * ->disconnect callback.
897 void drm_connector_unplug_all(struct drm_device *dev)
899 struct drm_connector *connector;
901 /* taking the mode config mutex ends up in a clash with sysfs */
902 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
903 drm_sysfs_connector_remove(connector);
906 EXPORT_SYMBOL(drm_connector_unplug_all);
909 * drm_bridge_init - initialize a drm transcoder/bridge
911 * @bridge: transcoder/bridge to set up
912 * @funcs: bridge function table
914 * Initialises a preallocated bridge. Bridges should be
915 * subclassed as part of driver connector objects.
918 * Zero on success, error code on failure.
920 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
921 const struct drm_bridge_funcs *funcs)
925 drm_modeset_lock_all(dev);
927 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
932 bridge->funcs = funcs;
934 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
935 dev->mode_config.num_bridge++;
938 drm_modeset_unlock_all(dev);
941 EXPORT_SYMBOL(drm_bridge_init);
944 * drm_bridge_cleanup - cleans up an initialised bridge
945 * @bridge: bridge to cleanup
947 * Cleans up the bridge but doesn't free the object.
949 void drm_bridge_cleanup(struct drm_bridge *bridge)
951 struct drm_device *dev = bridge->dev;
953 drm_modeset_lock_all(dev);
954 drm_mode_object_put(dev, &bridge->base);
955 list_del(&bridge->head);
956 dev->mode_config.num_bridge--;
957 drm_modeset_unlock_all(dev);
959 EXPORT_SYMBOL(drm_bridge_cleanup);
962 * drm_encoder_init - Init a preallocated encoder
964 * @encoder: the encoder to init
965 * @funcs: callbacks for this encoder
966 * @encoder_type: user visible type of the encoder
968 * Initialises a preallocated encoder. Encoder should be
969 * subclassed as part of driver encoder objects.
972 * Zero on success, error code on failure.
974 int drm_encoder_init(struct drm_device *dev,
975 struct drm_encoder *encoder,
976 const struct drm_encoder_funcs *funcs,
981 drm_modeset_lock_all(dev);
983 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
988 encoder->encoder_type = encoder_type;
989 encoder->funcs = funcs;
991 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
992 dev->mode_config.num_encoder++;
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 list_del(&encoder->head);
1013 dev->mode_config.num_encoder--;
1014 drm_modeset_unlock_all(dev);
1016 EXPORT_SYMBOL(drm_encoder_cleanup);
1019 * drm_universal_plane_init - Initialize a new universal plane object
1021 * @plane: plane object to init
1022 * @possible_crtcs: bitmask of possible CRTCs
1023 * @funcs: callbacks for the new plane
1024 * @formats: array of supported formats (%DRM_FORMAT_*)
1025 * @format_count: number of elements in @formats
1026 * @type: type of plane (overlay, primary, cursor)
1028 * Initializes a plane object of type @type.
1031 * Zero on success, error code on failure.
1033 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1034 unsigned long possible_crtcs,
1035 const struct drm_plane_funcs *funcs,
1036 const uint32_t *formats, uint32_t format_count,
1037 enum drm_plane_type type)
1041 drm_modeset_lock_all(dev);
1043 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1047 plane->base.properties = &plane->properties;
1049 plane->funcs = funcs;
1050 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
1052 if (!plane->format_types) {
1053 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1054 drm_mode_object_put(dev, &plane->base);
1059 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1060 plane->format_count = format_count;
1061 plane->possible_crtcs = possible_crtcs;
1064 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1065 dev->mode_config.num_total_plane++;
1066 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1067 dev->mode_config.num_overlay_plane++;
1069 drm_object_attach_property(&plane->base,
1070 dev->mode_config.plane_type_property,
1074 drm_modeset_unlock_all(dev);
1078 EXPORT_SYMBOL(drm_universal_plane_init);
1081 * drm_plane_init - Initialize a legacy plane
1083 * @plane: plane object to init
1084 * @possible_crtcs: bitmask of possible CRTCs
1085 * @funcs: callbacks for the new plane
1086 * @formats: array of supported formats (%DRM_FORMAT_*)
1087 * @format_count: number of elements in @formats
1088 * @is_primary: plane type (primary vs overlay)
1090 * Legacy API to initialize a DRM plane.
1092 * New drivers should call drm_universal_plane_init() instead.
1095 * Zero on success, error code on failure.
1097 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1098 unsigned long possible_crtcs,
1099 const struct drm_plane_funcs *funcs,
1100 const uint32_t *formats, uint32_t format_count,
1103 enum drm_plane_type type;
1105 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1106 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1107 formats, format_count, type);
1109 EXPORT_SYMBOL(drm_plane_init);
1112 * drm_plane_cleanup - Clean up the core plane usage
1113 * @plane: plane to cleanup
1115 * This function cleans up @plane and removes it from the DRM mode setting
1116 * core. Note that the function does *not* free the plane structure itself,
1117 * this is the responsibility of the caller.
1119 void drm_plane_cleanup(struct drm_plane *plane)
1121 struct drm_device *dev = plane->dev;
1123 drm_modeset_lock_all(dev);
1124 kfree(plane->format_types);
1125 drm_mode_object_put(dev, &plane->base);
1127 BUG_ON(list_empty(&plane->head));
1129 list_del(&plane->head);
1130 dev->mode_config.num_total_plane--;
1131 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1132 dev->mode_config.num_overlay_plane--;
1133 drm_modeset_unlock_all(dev);
1135 EXPORT_SYMBOL(drm_plane_cleanup);
1138 * drm_plane_force_disable - Forcibly disable a plane
1139 * @plane: plane to disable
1141 * Forces the plane to be disabled.
1143 * Used when the plane's current framebuffer is destroyed,
1144 * and when restoring fbdev mode.
1146 void drm_plane_force_disable(struct drm_plane *plane)
1148 struct drm_framebuffer *old_fb = plane->fb;
1154 ret = plane->funcs->disable_plane(plane);
1156 DRM_ERROR("failed to disable plane with busy fb\n");
1159 /* disconnect the plane from the fb and crtc: */
1160 __drm_framebuffer_unreference(old_fb);
1164 EXPORT_SYMBOL(drm_plane_force_disable);
1166 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1168 struct drm_property *edid;
1169 struct drm_property *dpms;
1172 * Standard properties (apply to all connectors)
1174 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1175 DRM_MODE_PROP_IMMUTABLE,
1177 dev->mode_config.edid_property = edid;
1179 dpms = drm_property_create_enum(dev, 0,
1180 "DPMS", drm_dpms_enum_list,
1181 ARRAY_SIZE(drm_dpms_enum_list));
1182 dev->mode_config.dpms_property = dpms;
1187 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1189 struct drm_property *type;
1192 * Standard properties (apply to all planes)
1194 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1195 "type", drm_plane_type_enum_list,
1196 ARRAY_SIZE(drm_plane_type_enum_list));
1197 dev->mode_config.plane_type_property = type;
1203 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1206 * Called by a driver the first time a DVI-I connector is made.
1208 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1210 struct drm_property *dvi_i_selector;
1211 struct drm_property *dvi_i_subconnector;
1213 if (dev->mode_config.dvi_i_select_subconnector_property)
1217 drm_property_create_enum(dev, 0,
1218 "select subconnector",
1219 drm_dvi_i_select_enum_list,
1220 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1221 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1223 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1225 drm_dvi_i_subconnector_enum_list,
1226 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1227 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1231 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1234 * drm_create_tv_properties - create TV specific connector properties
1236 * @num_modes: number of different TV formats (modes) supported
1237 * @modes: array of pointers to strings containing name of each format
1239 * Called by a driver's TV initialization routine, this function creates
1240 * the TV specific connector properties for a given device. Caller is
1241 * responsible for allocating a list of format names and passing them to
1244 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
1247 struct drm_property *tv_selector;
1248 struct drm_property *tv_subconnector;
1251 if (dev->mode_config.tv_select_subconnector_property)
1255 * Basic connector properties
1257 tv_selector = drm_property_create_enum(dev, 0,
1258 "select subconnector",
1259 drm_tv_select_enum_list,
1260 ARRAY_SIZE(drm_tv_select_enum_list));
1261 dev->mode_config.tv_select_subconnector_property = tv_selector;
1264 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1266 drm_tv_subconnector_enum_list,
1267 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1268 dev->mode_config.tv_subconnector_property = tv_subconnector;
1271 * Other, TV specific properties: margins & TV modes.
1273 dev->mode_config.tv_left_margin_property =
1274 drm_property_create_range(dev, 0, "left margin", 0, 100);
1276 dev->mode_config.tv_right_margin_property =
1277 drm_property_create_range(dev, 0, "right margin", 0, 100);
1279 dev->mode_config.tv_top_margin_property =
1280 drm_property_create_range(dev, 0, "top margin", 0, 100);
1282 dev->mode_config.tv_bottom_margin_property =
1283 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1285 dev->mode_config.tv_mode_property =
1286 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1288 for (i = 0; i < num_modes; i++)
1289 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1292 dev->mode_config.tv_brightness_property =
1293 drm_property_create_range(dev, 0, "brightness", 0, 100);
1295 dev->mode_config.tv_contrast_property =
1296 drm_property_create_range(dev, 0, "contrast", 0, 100);
1298 dev->mode_config.tv_flicker_reduction_property =
1299 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1301 dev->mode_config.tv_overscan_property =
1302 drm_property_create_range(dev, 0, "overscan", 0, 100);
1304 dev->mode_config.tv_saturation_property =
1305 drm_property_create_range(dev, 0, "saturation", 0, 100);
1307 dev->mode_config.tv_hue_property =
1308 drm_property_create_range(dev, 0, "hue", 0, 100);
1312 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1315 * drm_mode_create_scaling_mode_property - create scaling mode property
1318 * Called by a driver the first time it's needed, must be attached to desired
1321 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1323 struct drm_property *scaling_mode;
1325 if (dev->mode_config.scaling_mode_property)
1329 drm_property_create_enum(dev, 0, "scaling mode",
1330 drm_scaling_mode_enum_list,
1331 ARRAY_SIZE(drm_scaling_mode_enum_list));
1333 dev->mode_config.scaling_mode_property = scaling_mode;
1337 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1340 * drm_mode_create_dirty_property - create dirty property
1343 * Called by a driver the first time it's needed, must be attached to desired
1346 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1348 struct drm_property *dirty_info;
1350 if (dev->mode_config.dirty_info_property)
1354 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1356 drm_dirty_info_enum_list,
1357 ARRAY_SIZE(drm_dirty_info_enum_list));
1358 dev->mode_config.dirty_info_property = dirty_info;
1362 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1364 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1366 uint32_t total_objects = 0;
1368 total_objects += dev->mode_config.num_crtc;
1369 total_objects += dev->mode_config.num_connector;
1370 total_objects += dev->mode_config.num_encoder;
1371 total_objects += dev->mode_config.num_bridge;
1373 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1374 if (!group->id_list)
1377 group->num_crtcs = 0;
1378 group->num_connectors = 0;
1379 group->num_encoders = 0;
1380 group->num_bridges = 0;
1385 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1386 * the drm core's responsibility to set up mode control groups.
1388 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1389 struct drm_mode_group *group)
1391 struct drm_crtc *crtc;
1392 struct drm_encoder *encoder;
1393 struct drm_connector *connector;
1394 struct drm_bridge *bridge;
1397 if ((ret = drm_mode_group_init(dev, group)))
1400 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1401 group->id_list[group->num_crtcs++] = crtc->base.id;
1403 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1404 group->id_list[group->num_crtcs + group->num_encoders++] =
1407 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1408 group->id_list[group->num_crtcs + group->num_encoders +
1409 group->num_connectors++] = connector->base.id;
1411 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1412 group->id_list[group->num_crtcs + group->num_encoders +
1413 group->num_connectors + group->num_bridges++] =
1418 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1421 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1422 * @out: drm_mode_modeinfo struct to return to the user
1423 * @in: drm_display_mode to use
1425 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1428 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1429 const struct drm_display_mode *in)
1431 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1432 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1433 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1434 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1435 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1436 "timing values too large for mode info\n");
1438 out->clock = in->clock;
1439 out->hdisplay = in->hdisplay;
1440 out->hsync_start = in->hsync_start;
1441 out->hsync_end = in->hsync_end;
1442 out->htotal = in->htotal;
1443 out->hskew = in->hskew;
1444 out->vdisplay = in->vdisplay;
1445 out->vsync_start = in->vsync_start;
1446 out->vsync_end = in->vsync_end;
1447 out->vtotal = in->vtotal;
1448 out->vscan = in->vscan;
1449 out->vrefresh = in->vrefresh;
1450 out->flags = in->flags;
1451 out->type = in->type;
1452 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1453 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1457 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1458 * @out: drm_display_mode to return to the user
1459 * @in: drm_mode_modeinfo to use
1461 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1465 * Zero on success, errno on failure.
1467 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1468 const struct drm_mode_modeinfo *in)
1470 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1473 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1476 out->clock = in->clock;
1477 out->hdisplay = in->hdisplay;
1478 out->hsync_start = in->hsync_start;
1479 out->hsync_end = in->hsync_end;
1480 out->htotal = in->htotal;
1481 out->hskew = in->hskew;
1482 out->vdisplay = in->vdisplay;
1483 out->vsync_start = in->vsync_start;
1484 out->vsync_end = in->vsync_end;
1485 out->vtotal = in->vtotal;
1486 out->vscan = in->vscan;
1487 out->vrefresh = in->vrefresh;
1488 out->flags = in->flags;
1489 out->type = in->type;
1490 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1491 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1497 * drm_mode_getresources - get graphics configuration
1498 * @dev: drm device for the ioctl
1499 * @data: data pointer for the ioctl
1500 * @file_priv: drm file for the ioctl call
1502 * Construct a set of configuration description structures and return
1503 * them to the user, including CRTC, connector and framebuffer configuration.
1505 * Called by the user via ioctl.
1508 * Zero on success, errno on failure.
1510 int drm_mode_getresources(struct drm_device *dev, void *data,
1511 struct drm_file *file_priv)
1513 struct drm_mode_card_res *card_res = data;
1514 struct list_head *lh;
1515 struct drm_framebuffer *fb;
1516 struct drm_connector *connector;
1517 struct drm_crtc *crtc;
1518 struct drm_encoder *encoder;
1520 int connector_count = 0;
1523 int encoder_count = 0;
1525 uint32_t __user *fb_id;
1526 uint32_t __user *crtc_id;
1527 uint32_t __user *connector_id;
1528 uint32_t __user *encoder_id;
1529 struct drm_mode_group *mode_group;
1531 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1535 mutex_lock(&file_priv->fbs_lock);
1537 * For the non-control nodes we need to limit the list of resources
1538 * by IDs in the group list for this node
1540 list_for_each(lh, &file_priv->fbs)
1543 /* handle this in 4 parts */
1545 if (card_res->count_fbs >= fb_count) {
1547 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1548 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1549 if (put_user(fb->base.id, fb_id + copied)) {
1550 mutex_unlock(&file_priv->fbs_lock);
1556 card_res->count_fbs = fb_count;
1557 mutex_unlock(&file_priv->fbs_lock);
1559 drm_modeset_lock_all(dev);
1560 if (!drm_is_primary_client(file_priv)) {
1563 list_for_each(lh, &dev->mode_config.crtc_list)
1566 list_for_each(lh, &dev->mode_config.connector_list)
1569 list_for_each(lh, &dev->mode_config.encoder_list)
1573 mode_group = &file_priv->master->minor->mode_group;
1574 crtc_count = mode_group->num_crtcs;
1575 connector_count = mode_group->num_connectors;
1576 encoder_count = mode_group->num_encoders;
1579 card_res->max_height = dev->mode_config.max_height;
1580 card_res->min_height = dev->mode_config.min_height;
1581 card_res->max_width = dev->mode_config.max_width;
1582 card_res->min_width = dev->mode_config.min_width;
1585 if (card_res->count_crtcs >= crtc_count) {
1587 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1589 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1591 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1592 if (put_user(crtc->base.id, crtc_id + copied)) {
1599 for (i = 0; i < mode_group->num_crtcs; i++) {
1600 if (put_user(mode_group->id_list[i],
1601 crtc_id + copied)) {
1609 card_res->count_crtcs = crtc_count;
1612 if (card_res->count_encoders >= encoder_count) {
1614 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1616 list_for_each_entry(encoder,
1617 &dev->mode_config.encoder_list,
1619 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1620 drm_get_encoder_name(encoder));
1621 if (put_user(encoder->base.id, encoder_id +
1629 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1630 if (put_user(mode_group->id_list[i],
1631 encoder_id + copied)) {
1640 card_res->count_encoders = encoder_count;
1643 if (card_res->count_connectors >= connector_count) {
1645 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1647 list_for_each_entry(connector,
1648 &dev->mode_config.connector_list,
1650 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1652 drm_get_connector_name(connector));
1653 if (put_user(connector->base.id,
1654 connector_id + copied)) {
1661 int start = mode_group->num_crtcs +
1662 mode_group->num_encoders;
1663 for (i = start; i < start + mode_group->num_connectors; i++) {
1664 if (put_user(mode_group->id_list[i],
1665 connector_id + copied)) {
1673 card_res->count_connectors = connector_count;
1675 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1676 card_res->count_connectors, card_res->count_encoders);
1679 drm_modeset_unlock_all(dev);
1684 * drm_mode_getcrtc - get CRTC configuration
1685 * @dev: drm device for the ioctl
1686 * @data: data pointer for the ioctl
1687 * @file_priv: drm file for the ioctl call
1689 * Construct a CRTC configuration structure to return to the user.
1691 * Called by the user via ioctl.
1694 * Zero on success, errno on failure.
1696 int drm_mode_getcrtc(struct drm_device *dev,
1697 void *data, struct drm_file *file_priv)
1699 struct drm_mode_crtc *crtc_resp = data;
1700 struct drm_crtc *crtc;
1701 struct drm_mode_object *obj;
1704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1707 drm_modeset_lock_all(dev);
1709 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1710 DRM_MODE_OBJECT_CRTC);
1715 crtc = obj_to_crtc(obj);
1717 crtc_resp->x = crtc->x;
1718 crtc_resp->y = crtc->y;
1719 crtc_resp->gamma_size = crtc->gamma_size;
1720 if (crtc->primary->fb)
1721 crtc_resp->fb_id = crtc->primary->fb->base.id;
1723 crtc_resp->fb_id = 0;
1725 if (crtc->enabled) {
1727 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1728 crtc_resp->mode_valid = 1;
1731 crtc_resp->mode_valid = 0;
1735 drm_modeset_unlock_all(dev);
1739 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1740 const struct drm_file *file_priv)
1743 * If user-space hasn't configured the driver to expose the stereo 3D
1744 * modes, don't expose them.
1746 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1753 * drm_mode_getconnector - get connector configuration
1754 * @dev: drm device for the ioctl
1755 * @data: data pointer for the ioctl
1756 * @file_priv: drm file for the ioctl call
1758 * Construct a connector configuration structure to return to the user.
1760 * Called by the user via ioctl.
1763 * Zero on success, errno on failure.
1765 int drm_mode_getconnector(struct drm_device *dev, void *data,
1766 struct drm_file *file_priv)
1768 struct drm_mode_get_connector *out_resp = data;
1769 struct drm_mode_object *obj;
1770 struct drm_connector *connector;
1771 struct drm_display_mode *mode;
1773 int props_count = 0;
1774 int encoders_count = 0;
1778 struct drm_mode_modeinfo u_mode;
1779 struct drm_mode_modeinfo __user *mode_ptr;
1780 uint32_t __user *prop_ptr;
1781 uint64_t __user *prop_values;
1782 uint32_t __user *encoder_ptr;
1784 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1787 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1789 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1791 mutex_lock(&dev->mode_config.mutex);
1793 obj = drm_mode_object_find(dev, out_resp->connector_id,
1794 DRM_MODE_OBJECT_CONNECTOR);
1799 connector = obj_to_connector(obj);
1801 props_count = connector->properties.count;
1803 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1804 if (connector->encoder_ids[i] != 0) {
1809 if (out_resp->count_modes == 0) {
1810 connector->funcs->fill_modes(connector,
1811 dev->mode_config.max_width,
1812 dev->mode_config.max_height);
1815 /* delayed so we get modes regardless of pre-fill_modes state */
1816 list_for_each_entry(mode, &connector->modes, head)
1817 if (drm_mode_expose_to_userspace(mode, file_priv))
1820 out_resp->connector_id = connector->base.id;
1821 out_resp->connector_type = connector->connector_type;
1822 out_resp->connector_type_id = connector->connector_type_id;
1823 out_resp->mm_width = connector->display_info.width_mm;
1824 out_resp->mm_height = connector->display_info.height_mm;
1825 out_resp->subpixel = connector->display_info.subpixel_order;
1826 out_resp->connection = connector->status;
1827 if (connector->encoder)
1828 out_resp->encoder_id = connector->encoder->base.id;
1830 out_resp->encoder_id = 0;
1833 * This ioctl is called twice, once to determine how much space is
1834 * needed, and the 2nd time to fill it.
1836 if ((out_resp->count_modes >= mode_count) && mode_count) {
1838 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1839 list_for_each_entry(mode, &connector->modes, head) {
1840 if (!drm_mode_expose_to_userspace(mode, file_priv))
1843 drm_crtc_convert_to_umode(&u_mode, mode);
1844 if (copy_to_user(mode_ptr + copied,
1845 &u_mode, sizeof(u_mode))) {
1852 out_resp->count_modes = mode_count;
1854 if ((out_resp->count_props >= props_count) && props_count) {
1856 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1857 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1858 for (i = 0; i < connector->properties.count; i++) {
1859 if (put_user(connector->properties.ids[i],
1860 prop_ptr + copied)) {
1865 if (put_user(connector->properties.values[i],
1866 prop_values + copied)) {
1873 out_resp->count_props = props_count;
1875 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1877 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1878 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1879 if (connector->encoder_ids[i] != 0) {
1880 if (put_user(connector->encoder_ids[i],
1881 encoder_ptr + copied)) {
1889 out_resp->count_encoders = encoders_count;
1892 mutex_unlock(&dev->mode_config.mutex);
1898 * drm_mode_getencoder - get encoder configuration
1899 * @dev: drm device for the ioctl
1900 * @data: data pointer for the ioctl
1901 * @file_priv: drm file for the ioctl call
1903 * Construct a encoder configuration structure to return to the user.
1905 * Called by the user via ioctl.
1908 * Zero on success, errno on failure.
1910 int drm_mode_getencoder(struct drm_device *dev, void *data,
1911 struct drm_file *file_priv)
1913 struct drm_mode_get_encoder *enc_resp = data;
1914 struct drm_mode_object *obj;
1915 struct drm_encoder *encoder;
1918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1921 drm_modeset_lock_all(dev);
1922 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1923 DRM_MODE_OBJECT_ENCODER);
1928 encoder = obj_to_encoder(obj);
1931 enc_resp->crtc_id = encoder->crtc->base.id;
1933 enc_resp->crtc_id = 0;
1934 enc_resp->encoder_type = encoder->encoder_type;
1935 enc_resp->encoder_id = encoder->base.id;
1936 enc_resp->possible_crtcs = encoder->possible_crtcs;
1937 enc_resp->possible_clones = encoder->possible_clones;
1940 drm_modeset_unlock_all(dev);
1945 * drm_mode_getplane_res - enumerate all plane resources
1948 * @file_priv: DRM file info
1950 * Construct a list of plane ids to return to the user.
1952 * Called by the user via ioctl.
1955 * Zero on success, errno on failure.
1957 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1958 struct drm_file *file_priv)
1960 struct drm_mode_get_plane_res *plane_resp = data;
1961 struct drm_mode_config *config;
1962 struct drm_plane *plane;
1963 uint32_t __user *plane_ptr;
1964 int copied = 0, ret = 0;
1965 unsigned num_planes;
1967 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1970 drm_modeset_lock_all(dev);
1971 config = &dev->mode_config;
1973 if (file_priv->universal_planes)
1974 num_planes = config->num_total_plane;
1976 num_planes = config->num_overlay_plane;
1979 * This ioctl is called twice, once to determine how much space is
1980 * needed, and the 2nd time to fill it.
1983 (plane_resp->count_planes >= num_planes)) {
1984 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1986 list_for_each_entry(plane, &config->plane_list, head) {
1988 * Unless userspace set the 'universal planes'
1989 * capability bit, only advertise overlays.
1991 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
1992 !file_priv->universal_planes)
1995 if (put_user(plane->base.id, plane_ptr + copied)) {
2002 plane_resp->count_planes = num_planes;
2005 drm_modeset_unlock_all(dev);
2010 * drm_mode_getplane - get plane configuration
2013 * @file_priv: DRM file info
2015 * Construct a plane configuration structure to return to the user.
2017 * Called by the user via ioctl.
2020 * Zero on success, errno on failure.
2022 int drm_mode_getplane(struct drm_device *dev, void *data,
2023 struct drm_file *file_priv)
2025 struct drm_mode_get_plane *plane_resp = data;
2026 struct drm_mode_object *obj;
2027 struct drm_plane *plane;
2028 uint32_t __user *format_ptr;
2031 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2034 drm_modeset_lock_all(dev);
2035 obj = drm_mode_object_find(dev, plane_resp->plane_id,
2036 DRM_MODE_OBJECT_PLANE);
2041 plane = obj_to_plane(obj);
2044 plane_resp->crtc_id = plane->crtc->base.id;
2046 plane_resp->crtc_id = 0;
2049 plane_resp->fb_id = plane->fb->base.id;
2051 plane_resp->fb_id = 0;
2053 plane_resp->plane_id = plane->base.id;
2054 plane_resp->possible_crtcs = plane->possible_crtcs;
2055 plane_resp->gamma_size = 0;
2058 * This ioctl is called twice, once to determine how much space is
2059 * needed, and the 2nd time to fill it.
2061 if (plane->format_count &&
2062 (plane_resp->count_format_types >= plane->format_count)) {
2063 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2064 if (copy_to_user(format_ptr,
2065 plane->format_types,
2066 sizeof(uint32_t) * plane->format_count)) {
2071 plane_resp->count_format_types = plane->format_count;
2074 drm_modeset_unlock_all(dev);
2079 * drm_mode_setplane - configure a plane's configuration
2081 * @data: ioctl data*
2082 * @file_priv: DRM file info
2084 * Set plane configuration, including placement, fb, scaling, and other factors.
2085 * Or pass a NULL fb to disable.
2088 * Zero on success, errno on failure.
2090 int drm_mode_setplane(struct drm_device *dev, void *data,
2091 struct drm_file *file_priv)
2093 struct drm_mode_set_plane *plane_req = data;
2094 struct drm_mode_object *obj;
2095 struct drm_plane *plane;
2096 struct drm_crtc *crtc;
2097 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
2099 unsigned int fb_width, fb_height;
2102 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2106 * First, find the plane, crtc, and fb objects. If not available,
2107 * we don't bother to call the driver.
2109 obj = drm_mode_object_find(dev, plane_req->plane_id,
2110 DRM_MODE_OBJECT_PLANE);
2112 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2113 plane_req->plane_id);
2116 plane = obj_to_plane(obj);
2118 /* No fb means shut it down */
2119 if (!plane_req->fb_id) {
2120 drm_modeset_lock_all(dev);
2122 ret = plane->funcs->disable_plane(plane);
2129 drm_modeset_unlock_all(dev);
2133 obj = drm_mode_object_find(dev, plane_req->crtc_id,
2134 DRM_MODE_OBJECT_CRTC);
2136 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2137 plane_req->crtc_id);
2141 crtc = obj_to_crtc(obj);
2143 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2145 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2151 /* Check whether this plane supports the fb pixel format. */
2152 for (i = 0; i < plane->format_count; i++)
2153 if (fb->pixel_format == plane->format_types[i])
2155 if (i == plane->format_count) {
2156 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2157 drm_get_format_name(fb->pixel_format));
2162 fb_width = fb->width << 16;
2163 fb_height = fb->height << 16;
2165 /* Make sure source coordinates are inside the fb. */
2166 if (plane_req->src_w > fb_width ||
2167 plane_req->src_x > fb_width - plane_req->src_w ||
2168 plane_req->src_h > fb_height ||
2169 plane_req->src_y > fb_height - plane_req->src_h) {
2170 DRM_DEBUG_KMS("Invalid source coordinates "
2171 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2172 plane_req->src_w >> 16,
2173 ((plane_req->src_w & 0xffff) * 15625) >> 10,
2174 plane_req->src_h >> 16,
2175 ((plane_req->src_h & 0xffff) * 15625) >> 10,
2176 plane_req->src_x >> 16,
2177 ((plane_req->src_x & 0xffff) * 15625) >> 10,
2178 plane_req->src_y >> 16,
2179 ((plane_req->src_y & 0xffff) * 15625) >> 10);
2184 /* Give drivers some help against integer overflows */
2185 if (plane_req->crtc_w > INT_MAX ||
2186 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2187 plane_req->crtc_h > INT_MAX ||
2188 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2189 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2190 plane_req->crtc_w, plane_req->crtc_h,
2191 plane_req->crtc_x, plane_req->crtc_y);
2196 drm_modeset_lock_all(dev);
2198 ret = plane->funcs->update_plane(plane, crtc, fb,
2199 plane_req->crtc_x, plane_req->crtc_y,
2200 plane_req->crtc_w, plane_req->crtc_h,
2201 plane_req->src_x, plane_req->src_y,
2202 plane_req->src_w, plane_req->src_h);
2210 drm_modeset_unlock_all(dev);
2214 drm_framebuffer_unreference(fb);
2216 drm_framebuffer_unreference(old_fb);
2222 * drm_mode_set_config_internal - helper to call ->set_config
2223 * @set: modeset config to set
2225 * This is a little helper to wrap internal calls to the ->set_config driver
2226 * interface. The only thing it adds is correct refcounting dance.
2229 * Zero on success, errno on failure.
2231 int drm_mode_set_config_internal(struct drm_mode_set *set)
2233 struct drm_crtc *crtc = set->crtc;
2234 struct drm_framebuffer *fb;
2235 struct drm_crtc *tmp;
2239 * NOTE: ->set_config can also disable other crtcs (if we steal all
2240 * connectors from it), hence we need to refcount the fbs across all
2241 * crtcs. Atomic modeset will have saner semantics ...
2243 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2244 tmp->old_fb = tmp->primary->fb;
2248 ret = crtc->funcs->set_config(set);
2250 crtc->primary->crtc = crtc;
2251 crtc->primary->fb = fb;
2254 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2255 if (tmp->primary->fb)
2256 drm_framebuffer_reference(tmp->primary->fb);
2258 drm_framebuffer_unreference(tmp->old_fb);
2263 EXPORT_SYMBOL(drm_mode_set_config_internal);
2266 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2268 * @crtc: CRTC that framebuffer will be displayed on
2271 * @mode: mode that framebuffer will be displayed under
2272 * @fb: framebuffer to check size of
2274 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2276 const struct drm_display_mode *mode,
2277 const struct drm_framebuffer *fb)
2280 int hdisplay, vdisplay;
2282 hdisplay = mode->hdisplay;
2283 vdisplay = mode->vdisplay;
2285 if (drm_mode_is_stereo(mode)) {
2286 struct drm_display_mode adjusted = *mode;
2288 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2289 hdisplay = adjusted.crtc_hdisplay;
2290 vdisplay = adjusted.crtc_vdisplay;
2293 if (crtc->invert_dimensions)
2294 swap(hdisplay, vdisplay);
2296 if (hdisplay > fb->width ||
2297 vdisplay > fb->height ||
2298 x > fb->width - hdisplay ||
2299 y > fb->height - vdisplay) {
2300 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2301 fb->width, fb->height, hdisplay, vdisplay, x, y,
2302 crtc->invert_dimensions ? " (inverted)" : "");
2308 EXPORT_SYMBOL(drm_crtc_check_viewport);
2311 * drm_mode_setcrtc - set CRTC configuration
2312 * @dev: drm device for the ioctl
2313 * @data: data pointer for the ioctl
2314 * @file_priv: drm file for the ioctl call
2316 * Build a new CRTC configuration based on user request.
2318 * Called by the user via ioctl.
2321 * Zero on success, errno on failure.
2323 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2324 struct drm_file *file_priv)
2326 struct drm_mode_config *config = &dev->mode_config;
2327 struct drm_mode_crtc *crtc_req = data;
2328 struct drm_mode_object *obj;
2329 struct drm_crtc *crtc;
2330 struct drm_connector **connector_set = NULL, *connector;
2331 struct drm_framebuffer *fb = NULL;
2332 struct drm_display_mode *mode = NULL;
2333 struct drm_mode_set set;
2334 uint32_t __user *set_connectors_ptr;
2338 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2341 /* For some reason crtc x/y offsets are signed internally. */
2342 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2345 drm_modeset_lock_all(dev);
2346 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2347 DRM_MODE_OBJECT_CRTC);
2349 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2353 crtc = obj_to_crtc(obj);
2354 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2356 if (crtc_req->mode_valid) {
2357 /* If we have a mode we need a framebuffer. */
2358 /* If we pass -1, set the mode with the currently bound fb */
2359 if (crtc_req->fb_id == -1) {
2360 if (!crtc->primary->fb) {
2361 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2365 fb = crtc->primary->fb;
2366 /* Make refcounting symmetric with the lookup path. */
2367 drm_framebuffer_reference(fb);
2369 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2371 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2378 mode = drm_mode_create(dev);
2384 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2386 DRM_DEBUG_KMS("Invalid mode\n");
2390 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2392 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2399 if (crtc_req->count_connectors == 0 && mode) {
2400 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2405 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2406 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2407 crtc_req->count_connectors);
2412 if (crtc_req->count_connectors > 0) {
2415 /* Avoid unbounded kernel memory allocation */
2416 if (crtc_req->count_connectors > config->num_connector) {
2421 connector_set = kmalloc(crtc_req->count_connectors *
2422 sizeof(struct drm_connector *),
2424 if (!connector_set) {
2429 for (i = 0; i < crtc_req->count_connectors; i++) {
2430 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2431 if (get_user(out_id, &set_connectors_ptr[i])) {
2436 obj = drm_mode_object_find(dev, out_id,
2437 DRM_MODE_OBJECT_CONNECTOR);
2439 DRM_DEBUG_KMS("Connector id %d unknown\n",
2444 connector = obj_to_connector(obj);
2445 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2447 drm_get_connector_name(connector));
2449 connector_set[i] = connector;
2454 set.x = crtc_req->x;
2455 set.y = crtc_req->y;
2457 set.connectors = connector_set;
2458 set.num_connectors = crtc_req->count_connectors;
2460 ret = drm_mode_set_config_internal(&set);
2464 drm_framebuffer_unreference(fb);
2466 kfree(connector_set);
2467 drm_mode_destroy(dev, mode);
2468 drm_modeset_unlock_all(dev);
2472 static int drm_mode_cursor_common(struct drm_device *dev,
2473 struct drm_mode_cursor2 *req,
2474 struct drm_file *file_priv)
2476 struct drm_mode_object *obj;
2477 struct drm_crtc *crtc;
2480 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2483 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2486 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
2488 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2491 crtc = obj_to_crtc(obj);
2493 mutex_lock(&crtc->mutex);
2494 if (req->flags & DRM_MODE_CURSOR_BO) {
2495 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2499 /* Turns off the cursor if handle is 0 */
2500 if (crtc->funcs->cursor_set2)
2501 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2502 req->width, req->height, req->hot_x, req->hot_y);
2504 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2505 req->width, req->height);
2508 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2509 if (crtc->funcs->cursor_move) {
2510 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2517 mutex_unlock(&crtc->mutex);
2525 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2526 * @dev: drm device for the ioctl
2527 * @data: data pointer for the ioctl
2528 * @file_priv: drm file for the ioctl call
2530 * Set the cursor configuration based on user request.
2532 * Called by the user via ioctl.
2535 * Zero on success, errno on failure.
2537 int drm_mode_cursor_ioctl(struct drm_device *dev,
2538 void *data, struct drm_file *file_priv)
2540 struct drm_mode_cursor *req = data;
2541 struct drm_mode_cursor2 new_req;
2543 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2544 new_req.hot_x = new_req.hot_y = 0;
2546 return drm_mode_cursor_common(dev, &new_req, file_priv);
2550 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2551 * @dev: drm device for the ioctl
2552 * @data: data pointer for the ioctl
2553 * @file_priv: drm file for the ioctl call
2555 * Set the cursor configuration based on user request. This implements the 2nd
2556 * version of the cursor ioctl, which allows userspace to additionally specify
2557 * the hotspot of the pointer.
2559 * Called by the user via ioctl.
2562 * Zero on success, errno on failure.
2564 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2565 void *data, struct drm_file *file_priv)
2567 struct drm_mode_cursor2 *req = data;
2568 return drm_mode_cursor_common(dev, req, file_priv);
2572 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2573 * @bpp: bits per pixels
2574 * @depth: bit depth per pixel
2576 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2577 * Useful in fbdev emulation code, since that deals in those values.
2579 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2585 fmt = DRM_FORMAT_C8;
2589 fmt = DRM_FORMAT_XRGB1555;
2591 fmt = DRM_FORMAT_RGB565;
2594 fmt = DRM_FORMAT_RGB888;
2598 fmt = DRM_FORMAT_XRGB8888;
2599 else if (depth == 30)
2600 fmt = DRM_FORMAT_XRGB2101010;
2602 fmt = DRM_FORMAT_ARGB8888;
2605 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2606 fmt = DRM_FORMAT_XRGB8888;
2612 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2615 * drm_mode_addfb - add an FB to the graphics configuration
2616 * @dev: drm device for the ioctl
2617 * @data: data pointer for the ioctl
2618 * @file_priv: drm file for the ioctl call
2620 * Add a new FB to the specified CRTC, given a user request. This is the
2621 * original addfb ioclt which only supported RGB formats.
2623 * Called by the user via ioctl.
2626 * Zero on success, errno on failure.
2628 int drm_mode_addfb(struct drm_device *dev,
2629 void *data, struct drm_file *file_priv)
2631 struct drm_mode_fb_cmd *or = data;
2632 struct drm_mode_fb_cmd2 r = {};
2633 struct drm_mode_config *config = &dev->mode_config;
2634 struct drm_framebuffer *fb;
2637 /* Use new struct with format internally */
2638 r.fb_id = or->fb_id;
2639 r.width = or->width;
2640 r.height = or->height;
2641 r.pitches[0] = or->pitch;
2642 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2643 r.handles[0] = or->handle;
2645 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2648 if ((config->min_width > r.width) || (r.width > config->max_width))
2651 if ((config->min_height > r.height) || (r.height > config->max_height))
2654 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2656 DRM_DEBUG_KMS("could not create framebuffer\n");
2660 mutex_lock(&file_priv->fbs_lock);
2661 or->fb_id = fb->base.id;
2662 list_add(&fb->filp_head, &file_priv->fbs);
2663 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2664 mutex_unlock(&file_priv->fbs_lock);
2669 static int format_check(const struct drm_mode_fb_cmd2 *r)
2671 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2675 case DRM_FORMAT_RGB332:
2676 case DRM_FORMAT_BGR233:
2677 case DRM_FORMAT_XRGB4444:
2678 case DRM_FORMAT_XBGR4444:
2679 case DRM_FORMAT_RGBX4444:
2680 case DRM_FORMAT_BGRX4444:
2681 case DRM_FORMAT_ARGB4444:
2682 case DRM_FORMAT_ABGR4444:
2683 case DRM_FORMAT_RGBA4444:
2684 case DRM_FORMAT_BGRA4444:
2685 case DRM_FORMAT_XRGB1555:
2686 case DRM_FORMAT_XBGR1555:
2687 case DRM_FORMAT_RGBX5551:
2688 case DRM_FORMAT_BGRX5551:
2689 case DRM_FORMAT_ARGB1555:
2690 case DRM_FORMAT_ABGR1555:
2691 case DRM_FORMAT_RGBA5551:
2692 case DRM_FORMAT_BGRA5551:
2693 case DRM_FORMAT_RGB565:
2694 case DRM_FORMAT_BGR565:
2695 case DRM_FORMAT_RGB888:
2696 case DRM_FORMAT_BGR888:
2697 case DRM_FORMAT_XRGB8888:
2698 case DRM_FORMAT_XBGR8888:
2699 case DRM_FORMAT_RGBX8888:
2700 case DRM_FORMAT_BGRX8888:
2701 case DRM_FORMAT_ARGB8888:
2702 case DRM_FORMAT_ABGR8888:
2703 case DRM_FORMAT_RGBA8888:
2704 case DRM_FORMAT_BGRA8888:
2705 case DRM_FORMAT_XRGB2101010:
2706 case DRM_FORMAT_XBGR2101010:
2707 case DRM_FORMAT_RGBX1010102:
2708 case DRM_FORMAT_BGRX1010102:
2709 case DRM_FORMAT_ARGB2101010:
2710 case DRM_FORMAT_ABGR2101010:
2711 case DRM_FORMAT_RGBA1010102:
2712 case DRM_FORMAT_BGRA1010102:
2713 case DRM_FORMAT_YUYV:
2714 case DRM_FORMAT_YVYU:
2715 case DRM_FORMAT_UYVY:
2716 case DRM_FORMAT_VYUY:
2717 case DRM_FORMAT_AYUV:
2718 case DRM_FORMAT_NV12:
2719 case DRM_FORMAT_NV21:
2720 case DRM_FORMAT_NV16:
2721 case DRM_FORMAT_NV61:
2722 case DRM_FORMAT_NV24:
2723 case DRM_FORMAT_NV42:
2724 case DRM_FORMAT_YUV410:
2725 case DRM_FORMAT_YVU410:
2726 case DRM_FORMAT_YUV411:
2727 case DRM_FORMAT_YVU411:
2728 case DRM_FORMAT_YUV420:
2729 case DRM_FORMAT_YVU420:
2730 case DRM_FORMAT_YUV422:
2731 case DRM_FORMAT_YVU422:
2732 case DRM_FORMAT_YUV444:
2733 case DRM_FORMAT_YVU444:
2736 DRM_DEBUG_KMS("invalid pixel format %s\n",
2737 drm_get_format_name(r->pixel_format));
2742 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2744 int ret, hsub, vsub, num_planes, i;
2746 ret = format_check(r);
2748 DRM_DEBUG_KMS("bad framebuffer format %s\n",
2749 drm_get_format_name(r->pixel_format));
2753 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2754 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2755 num_planes = drm_format_num_planes(r->pixel_format);
2757 if (r->width == 0 || r->width % hsub) {
2758 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2762 if (r->height == 0 || r->height % vsub) {
2763 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2767 for (i = 0; i < num_planes; i++) {
2768 unsigned int width = r->width / (i != 0 ? hsub : 1);
2769 unsigned int height = r->height / (i != 0 ? vsub : 1);
2770 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
2772 if (!r->handles[i]) {
2773 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2777 if ((uint64_t) width * cpp > UINT_MAX)
2780 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2783 if (r->pitches[i] < width * cpp) {
2784 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2793 * drm_mode_addfb2 - add an FB to the graphics configuration
2794 * @dev: drm device for the ioctl
2795 * @data: data pointer for the ioctl
2796 * @file_priv: drm file for the ioctl call
2798 * Add a new FB to the specified CRTC, given a user request with format. This is
2799 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
2800 * and uses fourcc codes as pixel format specifiers.
2802 * Called by the user via ioctl.
2805 * Zero on success, errno on failure.
2807 int drm_mode_addfb2(struct drm_device *dev,
2808 void *data, struct drm_file *file_priv)
2810 struct drm_mode_fb_cmd2 *r = data;
2811 struct drm_mode_config *config = &dev->mode_config;
2812 struct drm_framebuffer *fb;
2815 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2818 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2819 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2823 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2824 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2825 r->width, config->min_width, config->max_width);
2828 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2829 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2830 r->height, config->min_height, config->max_height);
2834 ret = framebuffer_check(r);
2838 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2840 DRM_DEBUG_KMS("could not create framebuffer\n");
2844 mutex_lock(&file_priv->fbs_lock);
2845 r->fb_id = fb->base.id;
2846 list_add(&fb->filp_head, &file_priv->fbs);
2847 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2848 mutex_unlock(&file_priv->fbs_lock);
2855 * drm_mode_rmfb - remove an FB from the configuration
2856 * @dev: drm device for the ioctl
2857 * @data: data pointer for the ioctl
2858 * @file_priv: drm file for the ioctl call
2860 * Remove the FB specified by the user.
2862 * Called by the user via ioctl.
2865 * Zero on success, errno on failure.
2867 int drm_mode_rmfb(struct drm_device *dev,
2868 void *data, struct drm_file *file_priv)
2870 struct drm_framebuffer *fb = NULL;
2871 struct drm_framebuffer *fbl = NULL;
2872 uint32_t *id = data;
2875 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2878 mutex_lock(&file_priv->fbs_lock);
2879 mutex_lock(&dev->mode_config.fb_lock);
2880 fb = __drm_framebuffer_lookup(dev, *id);
2884 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2890 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2891 __drm_framebuffer_unregister(dev, fb);
2893 list_del_init(&fb->filp_head);
2894 mutex_unlock(&dev->mode_config.fb_lock);
2895 mutex_unlock(&file_priv->fbs_lock);
2897 drm_framebuffer_remove(fb);
2902 mutex_unlock(&dev->mode_config.fb_lock);
2903 mutex_unlock(&file_priv->fbs_lock);
2909 * drm_mode_getfb - get FB info
2910 * @dev: drm device for the ioctl
2911 * @data: data pointer for the ioctl
2912 * @file_priv: drm file for the ioctl call
2914 * Lookup the FB given its ID and return info about it.
2916 * Called by the user via ioctl.
2919 * Zero on success, errno on failure.
2921 int drm_mode_getfb(struct drm_device *dev,
2922 void *data, struct drm_file *file_priv)
2924 struct drm_mode_fb_cmd *r = data;
2925 struct drm_framebuffer *fb;
2928 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2931 fb = drm_framebuffer_lookup(dev, r->fb_id);
2935 r->height = fb->height;
2936 r->width = fb->width;
2937 r->depth = fb->depth;
2938 r->bpp = fb->bits_per_pixel;
2939 r->pitch = fb->pitches[0];
2940 if (fb->funcs->create_handle) {
2941 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
2942 drm_is_control_client(file_priv)) {
2943 ret = fb->funcs->create_handle(fb, file_priv,
2946 /* GET_FB() is an unprivileged ioctl so we must not
2947 * return a buffer-handle to non-master processes! For
2948 * backwards-compatibility reasons, we cannot make
2949 * GET_FB() privileged, so just return an invalid handle
2950 * for non-masters. */
2958 drm_framebuffer_unreference(fb);
2964 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
2965 * @dev: drm device for the ioctl
2966 * @data: data pointer for the ioctl
2967 * @file_priv: drm file for the ioctl call
2969 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
2970 * rectangle list. Generic userspace which does frontbuffer rendering must call
2971 * this ioctl to flush out the changes on manual-update display outputs, e.g.
2972 * usb display-link, mipi manual update panels or edp panel self refresh modes.
2974 * Modesetting drivers which always update the frontbuffer do not need to
2975 * implement the corresponding ->dirty framebuffer callback.
2977 * Called by the user via ioctl.
2980 * Zero on success, errno on failure.
2982 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2983 void *data, struct drm_file *file_priv)
2985 struct drm_clip_rect __user *clips_ptr;
2986 struct drm_clip_rect *clips = NULL;
2987 struct drm_mode_fb_dirty_cmd *r = data;
2988 struct drm_framebuffer *fb;
2993 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2996 fb = drm_framebuffer_lookup(dev, r->fb_id);
3000 num_clips = r->num_clips;
3001 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3003 if (!num_clips != !clips_ptr) {
3008 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3010 /* If userspace annotates copy, clips must come in pairs */
3011 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3016 if (num_clips && clips_ptr) {
3017 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3021 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
3027 ret = copy_from_user(clips, clips_ptr,
3028 num_clips * sizeof(*clips));
3035 if (fb->funcs->dirty) {
3036 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3045 drm_framebuffer_unreference(fb);
3052 * drm_fb_release - remove and free the FBs on this file
3053 * @priv: drm file for the ioctl
3055 * Destroy all the FBs associated with @filp.
3057 * Called by the user via ioctl.
3060 * Zero on success, errno on failure.
3062 void drm_fb_release(struct drm_file *priv)
3064 struct drm_device *dev = priv->minor->dev;
3065 struct drm_framebuffer *fb, *tfb;
3067 mutex_lock(&priv->fbs_lock);
3068 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3070 mutex_lock(&dev->mode_config.fb_lock);
3071 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3072 __drm_framebuffer_unregister(dev, fb);
3073 mutex_unlock(&dev->mode_config.fb_lock);
3075 list_del_init(&fb->filp_head);
3077 /* This will also drop the fpriv->fbs reference. */
3078 drm_framebuffer_remove(fb);
3080 mutex_unlock(&priv->fbs_lock);
3084 * drm_property_create - create a new property type
3086 * @flags: flags specifying the property type
3087 * @name: name of the property
3088 * @num_values: number of pre-defined values
3090 * This creates a new generic drm property which can then be attached to a drm
3091 * object with drm_object_attach_property. The returned property object must be
3092 * freed with drm_property_destroy.
3095 * A pointer to the newly created property on success, NULL on failure.
3097 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3098 const char *name, int num_values)
3100 struct drm_property *property = NULL;
3103 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3108 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
3109 if (!property->values)
3113 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3117 property->flags = flags;
3118 property->num_values = num_values;
3119 INIT_LIST_HEAD(&property->enum_blob_list);
3122 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3123 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3126 list_add_tail(&property->head, &dev->mode_config.property_list);
3129 kfree(property->values);
3133 EXPORT_SYMBOL(drm_property_create);
3136 * drm_property_create - create a new enumeration property type
3138 * @flags: flags specifying the property type
3139 * @name: name of the property
3140 * @props: enumeration lists with property values
3141 * @num_values: number of pre-defined values
3143 * This creates a new generic drm property which can then be attached to a drm
3144 * object with drm_object_attach_property. The returned property object must be
3145 * freed with drm_property_destroy.
3147 * Userspace is only allowed to set one of the predefined values for enumeration
3151 * A pointer to the newly created property on success, NULL on failure.
3153 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3155 const struct drm_prop_enum_list *props,
3158 struct drm_property *property;
3161 flags |= DRM_MODE_PROP_ENUM;
3163 property = drm_property_create(dev, flags, name, num_values);
3167 for (i = 0; i < num_values; i++) {
3168 ret = drm_property_add_enum(property, i,
3172 drm_property_destroy(dev, property);
3179 EXPORT_SYMBOL(drm_property_create_enum);
3182 * drm_property_create - create a new bitmask property type
3184 * @flags: flags specifying the property type
3185 * @name: name of the property
3186 * @props: enumeration lists with property bitflags
3187 * @num_values: number of pre-defined values
3189 * This creates a new generic drm property which can then be attached to a drm
3190 * object with drm_object_attach_property. The returned property object must be
3191 * freed with drm_property_destroy.
3193 * Compared to plain enumeration properties userspace is allowed to set any
3194 * or'ed together combination of the predefined property bitflag values
3197 * A pointer to the newly created property on success, NULL on failure.
3199 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3200 int flags, const char *name,
3201 const struct drm_prop_enum_list *props,
3204 struct drm_property *property;
3207 flags |= DRM_MODE_PROP_BITMASK;
3209 property = drm_property_create(dev, flags, name, num_values);
3213 for (i = 0; i < num_values; i++) {
3214 ret = drm_property_add_enum(property, i,
3218 drm_property_destroy(dev, property);
3225 EXPORT_SYMBOL(drm_property_create_bitmask);
3228 * drm_property_create - create a new ranged property type
3230 * @flags: flags specifying the property type
3231 * @name: name of the property
3232 * @min: minimum value of the property
3233 * @max: maximum value of the property
3235 * This creates a new generic drm property which can then be attached to a drm
3236 * object with drm_object_attach_property. The returned property object must be
3237 * freed with drm_property_destroy.
3239 * Userspace is allowed to set any interger value in the (min, max) range
3243 * A pointer to the newly created property on success, NULL on failure.
3245 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3247 uint64_t min, uint64_t max)
3249 struct drm_property *property;
3251 flags |= DRM_MODE_PROP_RANGE;
3253 property = drm_property_create(dev, flags, name, 2);
3257 property->values[0] = min;
3258 property->values[1] = max;
3262 EXPORT_SYMBOL(drm_property_create_range);
3265 * drm_property_add_enum - add a possible value to an enumeration property
3266 * @property: enumeration property to change
3267 * @index: index of the new enumeration
3268 * @value: value of the new enumeration
3269 * @name: symbolic name of the new enumeration
3271 * This functions adds enumerations to a property.
3273 * It's use is deprecated, drivers should use one of the more specific helpers
3274 * to directly create the property with all enumerations already attached.
3277 * Zero on success, error code on failure.
3279 int drm_property_add_enum(struct drm_property *property, int index,
3280 uint64_t value, const char *name)
3282 struct drm_property_enum *prop_enum;
3284 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3288 * Bitmask enum properties have the additional constraint of values
3291 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
3294 if (!list_empty(&property->enum_blob_list)) {
3295 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3296 if (prop_enum->value == value) {
3297 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3298 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3304 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3308 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3309 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3310 prop_enum->value = value;
3312 property->values[index] = value;
3313 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3316 EXPORT_SYMBOL(drm_property_add_enum);
3319 * drm_property_destroy - destroy a drm property
3321 * @property: property to destry
3323 * This function frees a property including any attached resources like
3324 * enumeration values.
3326 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3328 struct drm_property_enum *prop_enum, *pt;
3330 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3331 list_del(&prop_enum->head);
3335 if (property->num_values)
3336 kfree(property->values);
3337 drm_mode_object_put(dev, &property->base);
3338 list_del(&property->head);
3341 EXPORT_SYMBOL(drm_property_destroy);
3344 * drm_object_attach_property - attach a property to a modeset object
3345 * @obj: drm modeset object
3346 * @property: property to attach
3347 * @init_val: initial value of the property
3349 * This attaches the given property to the modeset object with the given initial
3350 * value. Currently this function cannot fail since the properties are stored in
3351 * a statically sized array.
3353 void drm_object_attach_property(struct drm_mode_object *obj,
3354 struct drm_property *property,
3357 int count = obj->properties->count;
3359 if (count == DRM_OBJECT_MAX_PROPERTY) {
3360 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3361 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3362 "you see this message on the same object type.\n",
3367 obj->properties->ids[count] = property->base.id;
3368 obj->properties->values[count] = init_val;
3369 obj->properties->count++;
3371 EXPORT_SYMBOL(drm_object_attach_property);
3374 * drm_object_property_set_value - set the value of a property
3375 * @obj: drm mode object to set property value for
3376 * @property: property to set
3377 * @val: value the property should be set to
3379 * This functions sets a given property on a given object. This function only
3380 * changes the software state of the property, it does not call into the
3381 * driver's ->set_property callback.
3384 * Zero on success, error code on failure.
3386 int drm_object_property_set_value(struct drm_mode_object *obj,
3387 struct drm_property *property, uint64_t val)
3391 for (i = 0; i < obj->properties->count; i++) {
3392 if (obj->properties->ids[i] == property->base.id) {
3393 obj->properties->values[i] = val;
3400 EXPORT_SYMBOL(drm_object_property_set_value);
3403 * drm_object_property_get_value - retrieve the value of a property
3404 * @obj: drm mode object to get property value from
3405 * @property: property to retrieve
3406 * @val: storage for the property value
3408 * This function retrieves the softare state of the given property for the given
3409 * property. Since there is no driver callback to retrieve the current property
3410 * value this might be out of sync with the hardware, depending upon the driver
3414 * Zero on success, error code on failure.
3416 int drm_object_property_get_value(struct drm_mode_object *obj,
3417 struct drm_property *property, uint64_t *val)
3421 for (i = 0; i < obj->properties->count; i++) {
3422 if (obj->properties->ids[i] == property->base.id) {
3423 *val = obj->properties->values[i];
3430 EXPORT_SYMBOL(drm_object_property_get_value);
3433 * drm_mode_getproperty_ioctl - get the current value of a connector's property
3436 * @file_priv: DRM file info
3438 * This function retrieves the current value for an connectors's property.
3440 * Called by the user via ioctl.
3443 * Zero on success, errno on failure.
3445 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3446 void *data, struct drm_file *file_priv)
3448 struct drm_mode_object *obj;
3449 struct drm_mode_get_property *out_resp = data;
3450 struct drm_property *property;
3453 int value_count = 0;
3456 struct drm_property_enum *prop_enum;
3457 struct drm_mode_property_enum __user *enum_ptr;
3458 struct drm_property_blob *prop_blob;
3459 uint32_t __user *blob_id_ptr;
3460 uint64_t __user *values_ptr;
3461 uint32_t __user *blob_length_ptr;
3463 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3466 drm_modeset_lock_all(dev);
3467 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3472 property = obj_to_property(obj);
3474 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3475 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3477 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3478 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3482 value_count = property->num_values;
3484 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3485 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3486 out_resp->flags = property->flags;
3488 if ((out_resp->count_values >= value_count) && value_count) {
3489 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3490 for (i = 0; i < value_count; i++) {
3491 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3497 out_resp->count_values = value_count;
3499 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
3500 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3502 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3503 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3505 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3510 if (copy_to_user(&enum_ptr[copied].name,
3511 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3518 out_resp->count_enum_blobs = enum_count;
3521 if (property->flags & DRM_MODE_PROP_BLOB) {
3522 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3524 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3525 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3527 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3528 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3533 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3541 out_resp->count_enum_blobs = blob_count;
3544 drm_modeset_unlock_all(dev);
3548 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3551 struct drm_property_blob *blob;
3554 if (!length || !data)
3557 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3561 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3567 blob->length = length;
3569 memcpy(blob->data, data, length);
3571 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3575 static void drm_property_destroy_blob(struct drm_device *dev,
3576 struct drm_property_blob *blob)
3578 drm_mode_object_put(dev, &blob->base);
3579 list_del(&blob->head);
3584 * drm_mode_getblob_ioctl - get the contents of a blob property value
3587 * @file_priv: DRM file info
3589 * This function retrieves the contents of a blob property. The value stored in
3590 * an object's blob property is just a normal modeset object id.
3592 * Called by the user via ioctl.
3595 * Zero on success, errno on failure.
3597 int drm_mode_getblob_ioctl(struct drm_device *dev,
3598 void *data, struct drm_file *file_priv)
3600 struct drm_mode_object *obj;
3601 struct drm_mode_get_blob *out_resp = data;
3602 struct drm_property_blob *blob;
3604 void __user *blob_ptr;
3606 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3609 drm_modeset_lock_all(dev);
3610 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3615 blob = obj_to_blob(obj);
3617 if (out_resp->length == blob->length) {
3618 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3619 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3624 out_resp->length = blob->length;
3627 drm_modeset_unlock_all(dev);
3632 * drm_mode_connector_update_edid_property - update the edid property of a connector
3633 * @connector: drm connector
3634 * @edid: new value of the edid property
3636 * This function creates a new blob modeset object and assigns its id to the
3637 * connector's edid property.
3640 * Zero on success, errno on failure.
3642 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3645 struct drm_device *dev = connector->dev;
3648 if (connector->edid_blob_ptr)
3649 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3651 /* Delete edid, when there is none. */
3653 connector->edid_blob_ptr = NULL;
3654 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
3658 size = EDID_LENGTH * (1 + edid->extensions);
3659 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3661 if (!connector->edid_blob_ptr)
3664 ret = drm_object_property_set_value(&connector->base,
3665 dev->mode_config.edid_property,
3666 connector->edid_blob_ptr->base.id);
3670 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3672 static bool drm_property_change_is_valid(struct drm_property *property,
3675 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3677 if (property->flags & DRM_MODE_PROP_RANGE) {
3678 if (value < property->values[0] || value > property->values[1])
3681 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3683 uint64_t valid_mask = 0;
3684 for (i = 0; i < property->num_values; i++)
3685 valid_mask |= (1ULL << property->values[i]);
3686 return !(value & ~valid_mask);
3687 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3688 /* Only the driver knows */
3692 for (i = 0; i < property->num_values; i++)
3693 if (property->values[i] == value)
3700 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
3703 * @file_priv: DRM file info
3705 * This function sets the current value for a connectors's property. It also
3706 * calls into a driver's ->set_property callback to update the hardware state
3708 * Called by the user via ioctl.
3711 * Zero on success, errno on failure.
3713 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3714 void *data, struct drm_file *file_priv)
3716 struct drm_mode_connector_set_property *conn_set_prop = data;
3717 struct drm_mode_obj_set_property obj_set_prop = {
3718 .value = conn_set_prop->value,
3719 .prop_id = conn_set_prop->prop_id,
3720 .obj_id = conn_set_prop->connector_id,
3721 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3724 /* It does all the locking and checking we need */
3725 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3728 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3729 struct drm_property *property,
3733 struct drm_connector *connector = obj_to_connector(obj);
3735 /* Do DPMS ourselves */
3736 if (property == connector->dev->mode_config.dpms_property) {
3737 if (connector->funcs->dpms)
3738 (*connector->funcs->dpms)(connector, (int)value);
3740 } else if (connector->funcs->set_property)
3741 ret = connector->funcs->set_property(connector, property, value);
3743 /* store the property value if successful */
3745 drm_object_property_set_value(&connector->base, property, value);
3749 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3750 struct drm_property *property,
3754 struct drm_crtc *crtc = obj_to_crtc(obj);
3756 if (crtc->funcs->set_property)
3757 ret = crtc->funcs->set_property(crtc, property, value);
3759 drm_object_property_set_value(obj, property, value);
3764 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3765 struct drm_property *property,
3769 struct drm_plane *plane = obj_to_plane(obj);
3771 if (plane->funcs->set_property)
3772 ret = plane->funcs->set_property(plane, property, value);
3774 drm_object_property_set_value(obj, property, value);
3780 * drm_mode_getproperty_ioctl - get the current value of a object's property
3783 * @file_priv: DRM file info
3785 * This function retrieves the current value for an object's property. Compared
3786 * to the connector specific ioctl this one is extended to also work on crtc and
3789 * Called by the user via ioctl.
3792 * Zero on success, errno on failure.
3794 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3795 struct drm_file *file_priv)
3797 struct drm_mode_obj_get_properties *arg = data;
3798 struct drm_mode_object *obj;
3802 int props_count = 0;
3803 uint32_t __user *props_ptr;
3804 uint64_t __user *prop_values_ptr;
3806 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3809 drm_modeset_lock_all(dev);
3811 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3816 if (!obj->properties) {
3821 props_count = obj->properties->count;
3823 /* This ioctl is called twice, once to determine how much space is
3824 * needed, and the 2nd time to fill it. */
3825 if ((arg->count_props >= props_count) && props_count) {
3827 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3828 prop_values_ptr = (uint64_t __user *)(unsigned long)
3829 (arg->prop_values_ptr);
3830 for (i = 0; i < props_count; i++) {
3831 if (put_user(obj->properties->ids[i],
3832 props_ptr + copied)) {
3836 if (put_user(obj->properties->values[i],
3837 prop_values_ptr + copied)) {
3844 arg->count_props = props_count;
3846 drm_modeset_unlock_all(dev);
3851 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
3854 * @file_priv: DRM file info
3856 * This function sets the current value for an object's property. It also calls
3857 * into a driver's ->set_property callback to update the hardware state.
3858 * Compared to the connector specific ioctl this one is extended to also work on
3859 * crtc and plane objects.
3861 * Called by the user via ioctl.
3864 * Zero on success, errno on failure.
3866 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3867 struct drm_file *file_priv)
3869 struct drm_mode_obj_set_property *arg = data;
3870 struct drm_mode_object *arg_obj;
3871 struct drm_mode_object *prop_obj;
3872 struct drm_property *property;
3876 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3879 drm_modeset_lock_all(dev);
3881 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3886 if (!arg_obj->properties)
3889 for (i = 0; i < arg_obj->properties->count; i++)
3890 if (arg_obj->properties->ids[i] == arg->prop_id)
3893 if (i == arg_obj->properties->count)
3896 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3897 DRM_MODE_OBJECT_PROPERTY);
3902 property = obj_to_property(prop_obj);
3904 if (!drm_property_change_is_valid(property, arg->value))
3907 switch (arg_obj->type) {
3908 case DRM_MODE_OBJECT_CONNECTOR:
3909 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3912 case DRM_MODE_OBJECT_CRTC:
3913 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3915 case DRM_MODE_OBJECT_PLANE:
3916 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3921 drm_modeset_unlock_all(dev);
3926 * drm_mode_connector_attach_encoder - attach a connector to an encoder
3927 * @connector: connector to attach
3928 * @encoder: encoder to attach @connector to
3930 * This function links up a connector to an encoder. Note that the routing
3931 * restrictions between encoders and crtcs are exposed to userspace through the
3932 * possible_clones and possible_crtcs bitmasks.
3935 * Zero on success, errno on failure.
3937 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3938 struct drm_encoder *encoder)
3942 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3943 if (connector->encoder_ids[i] == 0) {
3944 connector->encoder_ids[i] = encoder->base.id;
3950 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3953 * drm_mode_crtc_set_gamma_size - set the gamma table size
3954 * @crtc: CRTC to set the gamma table size for
3955 * @gamma_size: size of the gamma table
3957 * Drivers which support gamma tables should set this to the supported gamma
3958 * table size when initializing the CRTC. Currently the drm core only supports a
3959 * fixed gamma table size.
3962 * Zero on success, errno on failure.
3964 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3967 crtc->gamma_size = gamma_size;
3969 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3970 if (!crtc->gamma_store) {
3971 crtc->gamma_size = 0;
3977 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3980 * drm_mode_gamma_set_ioctl - set the gamma table
3983 * @file_priv: DRM file info
3985 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
3986 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
3988 * Called by the user via ioctl.
3991 * Zero on success, errno on failure.
3993 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3994 void *data, struct drm_file *file_priv)
3996 struct drm_mode_crtc_lut *crtc_lut = data;
3997 struct drm_mode_object *obj;
3998 struct drm_crtc *crtc;
3999 void *r_base, *g_base, *b_base;
4003 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4006 drm_modeset_lock_all(dev);
4007 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4012 crtc = obj_to_crtc(obj);
4014 if (crtc->funcs->gamma_set == NULL) {
4019 /* memcpy into gamma store */
4020 if (crtc_lut->gamma_size != crtc->gamma_size) {
4025 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4026 r_base = crtc->gamma_store;
4027 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4032 g_base = r_base + size;
4033 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4038 b_base = g_base + size;
4039 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4044 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4047 drm_modeset_unlock_all(dev);
4053 * drm_mode_gamma_get_ioctl - get the gamma table
4056 * @file_priv: DRM file info
4058 * Copy the current gamma table into the storage provided. This also provides
4059 * the gamma table size the driver expects, which can be used to size the
4060 * allocated storage.
4062 * Called by the user via ioctl.
4065 * Zero on success, errno on failure.
4067 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4068 void *data, struct drm_file *file_priv)
4070 struct drm_mode_crtc_lut *crtc_lut = data;
4071 struct drm_mode_object *obj;
4072 struct drm_crtc *crtc;
4073 void *r_base, *g_base, *b_base;
4077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4080 drm_modeset_lock_all(dev);
4081 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
4086 crtc = obj_to_crtc(obj);
4088 /* memcpy into gamma store */
4089 if (crtc_lut->gamma_size != crtc->gamma_size) {
4094 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4095 r_base = crtc->gamma_store;
4096 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4101 g_base = r_base + size;
4102 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4107 b_base = g_base + size;
4108 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4113 drm_modeset_unlock_all(dev);
4118 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4121 * @file_priv: DRM file info
4123 * This schedules an asynchronous update on a given CRTC, called page flip.
4124 * Optionally a drm event is generated to signal the completion of the event.
4125 * Generic drivers cannot assume that a pageflip with changed framebuffer
4126 * properties (including driver specific metadata like tiling layout) will work,
4127 * but some drivers support e.g. pixel format changes through the pageflip
4130 * Called by the user via ioctl.
4133 * Zero on success, errno on failure.
4135 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4136 void *data, struct drm_file *file_priv)
4138 struct drm_mode_crtc_page_flip *page_flip = data;
4139 struct drm_mode_object *obj;
4140 struct drm_crtc *crtc;
4141 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
4142 struct drm_pending_vblank_event *e = NULL;
4143 unsigned long flags;
4146 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4147 page_flip->reserved != 0)
4150 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4153 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
4156 crtc = obj_to_crtc(obj);
4158 mutex_lock(&crtc->mutex);
4159 if (crtc->primary->fb == NULL) {
4160 /* The framebuffer is currently unbound, presumably
4161 * due to a hotplug event, that userspace has not
4168 if (crtc->funcs->page_flip == NULL)
4171 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4177 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4181 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4182 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4187 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4189 spin_lock_irqsave(&dev->event_lock, flags);
4190 if (file_priv->event_space < sizeof e->event) {
4191 spin_unlock_irqrestore(&dev->event_lock, flags);
4194 file_priv->event_space -= sizeof e->event;
4195 spin_unlock_irqrestore(&dev->event_lock, flags);
4197 e = kzalloc(sizeof *e, GFP_KERNEL);
4199 spin_lock_irqsave(&dev->event_lock, flags);
4200 file_priv->event_space += sizeof e->event;
4201 spin_unlock_irqrestore(&dev->event_lock, flags);
4205 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4206 e->event.base.length = sizeof e->event;
4207 e->event.user_data = page_flip->user_data;
4208 e->base.event = &e->event.base;
4209 e->base.file_priv = file_priv;
4211 (void (*) (struct drm_pending_event *)) kfree;
4214 old_fb = crtc->primary->fb;
4215 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4217 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4218 spin_lock_irqsave(&dev->event_lock, flags);
4219 file_priv->event_space += sizeof e->event;
4220 spin_unlock_irqrestore(&dev->event_lock, flags);
4223 /* Keep the old fb, don't unref it. */
4227 * Warn if the driver hasn't properly updated the crtc->fb
4228 * field to reflect that the new framebuffer is now used.
4229 * Failing to do so will screw with the reference counting
4232 WARN_ON(crtc->primary->fb != fb);
4233 /* Unref only the old framebuffer. */
4239 drm_framebuffer_unreference(fb);
4241 drm_framebuffer_unreference(old_fb);
4242 mutex_unlock(&crtc->mutex);
4248 * drm_mode_config_reset - call ->reset callbacks
4251 * This functions calls all the crtc's, encoder's and connector's ->reset
4252 * callback. Drivers can use this in e.g. their driver load or resume code to
4253 * reset hardware and software state.
4255 void drm_mode_config_reset(struct drm_device *dev)
4257 struct drm_crtc *crtc;
4258 struct drm_encoder *encoder;
4259 struct drm_connector *connector;
4261 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4262 if (crtc->funcs->reset)
4263 crtc->funcs->reset(crtc);
4265 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4266 if (encoder->funcs->reset)
4267 encoder->funcs->reset(encoder);
4269 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4270 connector->status = connector_status_unknown;
4272 if (connector->funcs->reset)
4273 connector->funcs->reset(connector);
4276 EXPORT_SYMBOL(drm_mode_config_reset);
4279 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4282 * @file_priv: DRM file info
4284 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4285 * TTM or something else entirely) and returns the resulting buffer handle. This
4286 * handle can then be wrapped up into a framebuffer modeset object.
4288 * Note that userspace is not allowed to use such objects for render
4289 * acceleration - drivers must create their own private ioctls for such a use
4292 * Called by the user via ioctl.
4295 * Zero on success, errno on failure.
4297 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4298 void *data, struct drm_file *file_priv)
4300 struct drm_mode_create_dumb *args = data;
4301 u32 cpp, stride, size;
4303 if (!dev->driver->dumb_create)
4305 if (!args->width || !args->height || !args->bpp)
4308 /* overflow checks for 32bit size calculations */
4309 cpp = DIV_ROUND_UP(args->bpp, 8);
4310 if (cpp > 0xffffffffU / args->width)
4312 stride = cpp * args->width;
4313 if (args->height > 0xffffffffU / stride)
4316 /* test for wrap-around */
4317 size = args->height * stride;
4318 if (PAGE_ALIGN(size) == 0)
4321 return dev->driver->dumb_create(file_priv, dev, args);
4325 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4328 * @file_priv: DRM file info
4330 * Allocate an offset in the drm device node's address space to be able to
4331 * memory map a dumb buffer.
4333 * Called by the user via ioctl.
4336 * Zero on success, errno on failure.
4338 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4339 void *data, struct drm_file *file_priv)
4341 struct drm_mode_map_dumb *args = data;
4343 /* call driver ioctl to get mmap offset */
4344 if (!dev->driver->dumb_map_offset)
4347 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4351 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4354 * @file_priv: DRM file info
4356 * This destroys the userspace handle for the given dumb backing storage buffer.
4357 * Since buffer objects must be reference counted in the kernel a buffer object
4358 * won't be immediately freed if a framebuffer modeset object still uses it.
4360 * Called by the user via ioctl.
4363 * Zero on success, errno on failure.
4365 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4366 void *data, struct drm_file *file_priv)
4368 struct drm_mode_destroy_dumb *args = data;
4370 if (!dev->driver->dumb_destroy)
4373 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4377 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4378 * @format: pixel format (DRM_FORMAT_*)
4379 * @depth: storage for the depth value
4380 * @bpp: storage for the bpp value
4382 * This only supports RGB formats here for compat with code that doesn't use
4383 * pixel formats directly yet.
4385 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4390 case DRM_FORMAT_RGB332:
4391 case DRM_FORMAT_BGR233:
4395 case DRM_FORMAT_XRGB1555:
4396 case DRM_FORMAT_XBGR1555:
4397 case DRM_FORMAT_RGBX5551:
4398 case DRM_FORMAT_BGRX5551:
4399 case DRM_FORMAT_ARGB1555:
4400 case DRM_FORMAT_ABGR1555:
4401 case DRM_FORMAT_RGBA5551:
4402 case DRM_FORMAT_BGRA5551:
4406 case DRM_FORMAT_RGB565:
4407 case DRM_FORMAT_BGR565:
4411 case DRM_FORMAT_RGB888:
4412 case DRM_FORMAT_BGR888:
4416 case DRM_FORMAT_XRGB8888:
4417 case DRM_FORMAT_XBGR8888:
4418 case DRM_FORMAT_RGBX8888:
4419 case DRM_FORMAT_BGRX8888:
4423 case DRM_FORMAT_XRGB2101010:
4424 case DRM_FORMAT_XBGR2101010:
4425 case DRM_FORMAT_RGBX1010102:
4426 case DRM_FORMAT_BGRX1010102:
4427 case DRM_FORMAT_ARGB2101010:
4428 case DRM_FORMAT_ABGR2101010:
4429 case DRM_FORMAT_RGBA1010102:
4430 case DRM_FORMAT_BGRA1010102:
4434 case DRM_FORMAT_ARGB8888:
4435 case DRM_FORMAT_ABGR8888:
4436 case DRM_FORMAT_RGBA8888:
4437 case DRM_FORMAT_BGRA8888:
4442 DRM_DEBUG_KMS("unsupported pixel format %s\n",
4443 drm_get_format_name(format));
4449 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
4452 * drm_format_num_planes - get the number of planes for format
4453 * @format: pixel format (DRM_FORMAT_*)
4456 * The number of planes used by the specified pixel format.
4458 int drm_format_num_planes(uint32_t format)
4461 case DRM_FORMAT_YUV410:
4462 case DRM_FORMAT_YVU410:
4463 case DRM_FORMAT_YUV411:
4464 case DRM_FORMAT_YVU411:
4465 case DRM_FORMAT_YUV420:
4466 case DRM_FORMAT_YVU420:
4467 case DRM_FORMAT_YUV422:
4468 case DRM_FORMAT_YVU422:
4469 case DRM_FORMAT_YUV444:
4470 case DRM_FORMAT_YVU444:
4472 case DRM_FORMAT_NV12:
4473 case DRM_FORMAT_NV21:
4474 case DRM_FORMAT_NV16:
4475 case DRM_FORMAT_NV61:
4476 case DRM_FORMAT_NV24:
4477 case DRM_FORMAT_NV42:
4483 EXPORT_SYMBOL(drm_format_num_planes);
4486 * drm_format_plane_cpp - determine the bytes per pixel value
4487 * @format: pixel format (DRM_FORMAT_*)
4488 * @plane: plane index
4491 * The bytes per pixel value for the specified plane.
4493 int drm_format_plane_cpp(uint32_t format, int plane)
4498 if (plane >= drm_format_num_planes(format))
4502 case DRM_FORMAT_YUYV:
4503 case DRM_FORMAT_YVYU:
4504 case DRM_FORMAT_UYVY:
4505 case DRM_FORMAT_VYUY:
4507 case DRM_FORMAT_NV12:
4508 case DRM_FORMAT_NV21:
4509 case DRM_FORMAT_NV16:
4510 case DRM_FORMAT_NV61:
4511 case DRM_FORMAT_NV24:
4512 case DRM_FORMAT_NV42:
4513 return plane ? 2 : 1;
4514 case DRM_FORMAT_YUV410:
4515 case DRM_FORMAT_YVU410:
4516 case DRM_FORMAT_YUV411:
4517 case DRM_FORMAT_YVU411:
4518 case DRM_FORMAT_YUV420:
4519 case DRM_FORMAT_YVU420:
4520 case DRM_FORMAT_YUV422:
4521 case DRM_FORMAT_YVU422:
4522 case DRM_FORMAT_YUV444:
4523 case DRM_FORMAT_YVU444:
4526 drm_fb_get_bpp_depth(format, &depth, &bpp);
4530 EXPORT_SYMBOL(drm_format_plane_cpp);
4533 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4534 * @format: pixel format (DRM_FORMAT_*)
4537 * The horizontal chroma subsampling factor for the
4538 * specified pixel format.
4540 int drm_format_horz_chroma_subsampling(uint32_t format)
4543 case DRM_FORMAT_YUV411:
4544 case DRM_FORMAT_YVU411:
4545 case DRM_FORMAT_YUV410:
4546 case DRM_FORMAT_YVU410:
4548 case DRM_FORMAT_YUYV:
4549 case DRM_FORMAT_YVYU:
4550 case DRM_FORMAT_UYVY:
4551 case DRM_FORMAT_VYUY:
4552 case DRM_FORMAT_NV12:
4553 case DRM_FORMAT_NV21:
4554 case DRM_FORMAT_NV16:
4555 case DRM_FORMAT_NV61:
4556 case DRM_FORMAT_YUV422:
4557 case DRM_FORMAT_YVU422:
4558 case DRM_FORMAT_YUV420:
4559 case DRM_FORMAT_YVU420:
4565 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4568 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4569 * @format: pixel format (DRM_FORMAT_*)
4572 * The vertical chroma subsampling factor for the
4573 * specified pixel format.
4575 int drm_format_vert_chroma_subsampling(uint32_t format)
4578 case DRM_FORMAT_YUV410:
4579 case DRM_FORMAT_YVU410:
4581 case DRM_FORMAT_YUV420:
4582 case DRM_FORMAT_YVU420:
4583 case DRM_FORMAT_NV12:
4584 case DRM_FORMAT_NV21:
4590 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
4593 * drm_mode_config_init - initialize DRM mode_configuration structure
4596 * Initialize @dev's mode_config structure, used for tracking the graphics
4597 * configuration of @dev.
4599 * Since this initializes the modeset locks, no locking is possible. Which is no
4600 * problem, since this should happen single threaded at init time. It is the
4601 * driver's problem to ensure this guarantee.
4604 void drm_mode_config_init(struct drm_device *dev)
4606 mutex_init(&dev->mode_config.mutex);
4607 mutex_init(&dev->mode_config.idr_mutex);
4608 mutex_init(&dev->mode_config.fb_lock);
4609 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4610 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4611 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4612 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
4613 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4614 INIT_LIST_HEAD(&dev->mode_config.property_list);
4615 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4616 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4617 idr_init(&dev->mode_config.crtc_idr);
4619 drm_modeset_lock_all(dev);
4620 drm_mode_create_standard_connector_properties(dev);
4621 drm_mode_create_standard_plane_properties(dev);
4622 drm_modeset_unlock_all(dev);
4624 /* Just to be sure */
4625 dev->mode_config.num_fb = 0;
4626 dev->mode_config.num_connector = 0;
4627 dev->mode_config.num_crtc = 0;
4628 dev->mode_config.num_encoder = 0;
4629 dev->mode_config.num_overlay_plane = 0;
4630 dev->mode_config.num_total_plane = 0;
4632 EXPORT_SYMBOL(drm_mode_config_init);
4635 * drm_mode_config_cleanup - free up DRM mode_config info
4638 * Free up all the connectors and CRTCs associated with this DRM device, then
4639 * free up the framebuffers and associated buffer objects.
4641 * Note that since this /should/ happen single-threaded at driver/device
4642 * teardown time, no locking is required. It's the driver's job to ensure that
4643 * this guarantee actually holds true.
4645 * FIXME: cleanup any dangling user buffer objects too
4647 void drm_mode_config_cleanup(struct drm_device *dev)
4649 struct drm_connector *connector, *ot;
4650 struct drm_crtc *crtc, *ct;
4651 struct drm_encoder *encoder, *enct;
4652 struct drm_bridge *bridge, *brt;
4653 struct drm_framebuffer *fb, *fbt;
4654 struct drm_property *property, *pt;
4655 struct drm_property_blob *blob, *bt;
4656 struct drm_plane *plane, *plt;
4658 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4660 encoder->funcs->destroy(encoder);
4663 list_for_each_entry_safe(bridge, brt,
4664 &dev->mode_config.bridge_list, head) {
4665 bridge->funcs->destroy(bridge);
4668 list_for_each_entry_safe(connector, ot,
4669 &dev->mode_config.connector_list, head) {
4670 connector->funcs->destroy(connector);
4673 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4675 drm_property_destroy(dev, property);
4678 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
4680 drm_property_destroy_blob(dev, blob);
4684 * Single-threaded teardown context, so it's not required to grab the
4685 * fb_lock to protect against concurrent fb_list access. Contrary, it
4686 * would actually deadlock with the drm_framebuffer_cleanup function.
4688 * Also, if there are any framebuffers left, that's a driver leak now,
4689 * so politely WARN about this.
4691 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4692 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
4693 drm_framebuffer_remove(fb);
4696 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4698 plane->funcs->destroy(plane);
4701 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4702 crtc->funcs->destroy(crtc);
4705 idr_destroy(&dev->mode_config.crtc_idr);
4707 EXPORT_SYMBOL(drm_mode_config_cleanup);