2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
42 #include "drm_crtc_internal.h"
43 #include "drm_internal.h"
45 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
46 struct drm_mode_fb_cmd2 *r,
47 struct drm_file *file_priv);
49 /* Avoid boilerplate. I'm tired of typing. */
50 #define DRM_ENUM_NAME_FN(fnname, list) \
51 const char *fnname(int val) \
54 for (i = 0; i < ARRAY_SIZE(list); i++) { \
55 if (list[i].type == val) \
56 return list[i].name; \
64 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
65 { DRM_MODE_DPMS_ON, "On" },
66 { DRM_MODE_DPMS_STANDBY, "Standby" },
67 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
68 { DRM_MODE_DPMS_OFF, "Off" }
71 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
73 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
74 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
75 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
76 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
82 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
83 { DRM_MODE_SCALE_NONE, "None" },
84 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
85 { DRM_MODE_SCALE_CENTER, "Center" },
86 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
89 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
90 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
91 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
92 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
96 * Non-global properties, but "required" for certain connectors.
98 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
99 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
100 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
101 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
104 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
106 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
107 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
109 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
112 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
113 drm_dvi_i_subconnector_enum_list)
115 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
116 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
117 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
118 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
123 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
125 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
126 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
127 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
128 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
129 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
133 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
134 drm_tv_subconnector_enum_list)
136 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
137 { DRM_MODE_DIRTY_OFF, "Off" },
138 { DRM_MODE_DIRTY_ON, "On" },
139 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
142 struct drm_conn_prop_enum_list {
149 * Connector and encoder types.
151 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
152 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
153 { DRM_MODE_CONNECTOR_VGA, "VGA" },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
157 { DRM_MODE_CONNECTOR_Composite, "Composite" },
158 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
160 { DRM_MODE_CONNECTOR_Component, "Component" },
161 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
165 { DRM_MODE_CONNECTOR_TV, "TV" },
166 { DRM_MODE_CONNECTOR_eDP, "eDP" },
167 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
168 { DRM_MODE_CONNECTOR_DSI, "DSI" },
171 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
172 { DRM_MODE_ENCODER_NONE, "None" },
173 { DRM_MODE_ENCODER_DAC, "DAC" },
174 { DRM_MODE_ENCODER_TMDS, "TMDS" },
175 { DRM_MODE_ENCODER_LVDS, "LVDS" },
176 { DRM_MODE_ENCODER_TVDAC, "TV" },
177 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
178 { DRM_MODE_ENCODER_DSI, "DSI" },
179 { DRM_MODE_ENCODER_DPMST, "DP MST" },
182 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
183 { SubPixelUnknown, "Unknown" },
184 { SubPixelHorizontalRGB, "Horizontal RGB" },
185 { SubPixelHorizontalBGR, "Horizontal BGR" },
186 { SubPixelVerticalRGB, "Vertical RGB" },
187 { SubPixelVerticalBGR, "Vertical BGR" },
188 { SubPixelNone, "None" },
191 void drm_connector_ida_init(void)
195 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
196 ida_init(&drm_connector_enum_list[i].ida);
199 void drm_connector_ida_destroy(void)
203 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
204 ida_destroy(&drm_connector_enum_list[i].ida);
208 * drm_get_connector_status_name - return a string for connector status
209 * @status: connector status to compute name of
211 * In contrast to the other drm_get_*_name functions this one here returns a
212 * const pointer and hence is threadsafe.
214 const char *drm_get_connector_status_name(enum drm_connector_status status)
216 if (status == connector_status_connected)
218 else if (status == connector_status_disconnected)
219 return "disconnected";
223 EXPORT_SYMBOL(drm_get_connector_status_name);
226 * drm_get_subpixel_order_name - return a string for a given subpixel enum
227 * @order: enum of subpixel_order
229 * Note you could abuse this and return something out of bounds, but that
230 * would be a caller error. No unscrubbed user data should make it here.
232 const char *drm_get_subpixel_order_name(enum subpixel_order order)
234 return drm_subpixel_enum_list[order].name;
236 EXPORT_SYMBOL(drm_get_subpixel_order_name);
238 static char printable_char(int c)
240 return isascii(c) && isprint(c) ? c : '?';
244 * drm_get_format_name - return a string for drm fourcc format
245 * @format: format to compute name of
247 * Note that the buffer used by this function is globally shared and owned by
248 * the function itself.
250 * FIXME: This isn't really multithreading safe.
252 const char *drm_get_format_name(uint32_t format)
256 snprintf(buf, sizeof(buf),
257 "%c%c%c%c %s-endian (0x%08x)",
258 printable_char(format & 0xff),
259 printable_char((format >> 8) & 0xff),
260 printable_char((format >> 16) & 0xff),
261 printable_char((format >> 24) & 0x7f),
262 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
267 EXPORT_SYMBOL(drm_get_format_name);
270 * Internal function to assign a slot in the object idr and optionally
271 * register the object into the idr.
273 static int drm_mode_object_get_reg(struct drm_device *dev,
274 struct drm_mode_object *obj,
280 mutex_lock(&dev->mode_config.idr_mutex);
281 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
284 * Set up the object linking under the protection of the idr
285 * lock so that other users can't see inconsistent state.
288 obj->type = obj_type;
290 mutex_unlock(&dev->mode_config.idr_mutex);
292 return ret < 0 ? ret : 0;
296 * drm_mode_object_get - allocate a new modeset identifier
298 * @obj: object pointer, used to generate unique ID
299 * @obj_type: object type
301 * Create a unique identifier based on @ptr in @dev's identifier space. Used
302 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
303 * modeset identifiers are _not_ reference counted. Hence don't use this for
304 * reference counted modeset objects like framebuffers.
307 * New unique (relative to other objects in @dev) integer identifier for the
310 int drm_mode_object_get(struct drm_device *dev,
311 struct drm_mode_object *obj, uint32_t obj_type)
313 return drm_mode_object_get_reg(dev, obj, obj_type, true);
316 static void drm_mode_object_register(struct drm_device *dev,
317 struct drm_mode_object *obj)
319 mutex_lock(&dev->mode_config.idr_mutex);
320 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
321 mutex_unlock(&dev->mode_config.idr_mutex);
325 * drm_mode_object_put - free a modeset identifer
327 * @object: object to free
329 * Free @id from @dev's unique identifier pool. Note that despite the _get
330 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
331 * for reference counted modeset objects like framebuffers.
333 void drm_mode_object_put(struct drm_device *dev,
334 struct drm_mode_object *object)
336 mutex_lock(&dev->mode_config.idr_mutex);
337 idr_remove(&dev->mode_config.crtc_idr, object->id);
338 mutex_unlock(&dev->mode_config.idr_mutex);
341 static struct drm_mode_object *_object_find(struct drm_device *dev,
342 uint32_t id, uint32_t type)
344 struct drm_mode_object *obj = NULL;
346 mutex_lock(&dev->mode_config.idr_mutex);
347 obj = idr_find(&dev->mode_config.crtc_idr, id);
348 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
350 if (obj && obj->id != id)
352 /* don't leak out unref'd fb's */
353 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
355 mutex_unlock(&dev->mode_config.idr_mutex);
361 * drm_mode_object_find - look up a drm object with static lifetime
363 * @id: id of the mode object
364 * @type: type of the mode object
366 * Note that framebuffers cannot be looked up with this functions - since those
367 * are reference counted, they need special treatment. Even with
368 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
369 * rather than WARN_ON()).
371 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
372 uint32_t id, uint32_t type)
374 struct drm_mode_object *obj = NULL;
376 /* Framebuffers are reference counted and need their own lookup
378 WARN_ON(type == DRM_MODE_OBJECT_FB);
379 obj = _object_find(dev, id, type);
382 EXPORT_SYMBOL(drm_mode_object_find);
385 * drm_framebuffer_init - initialize a framebuffer
387 * @fb: framebuffer to be initialized
388 * @funcs: ... with these functions
390 * Allocates an ID for the framebuffer's parent mode object, sets its mode
391 * functions & device file and adds it to the master fd list.
394 * This functions publishes the fb and makes it available for concurrent access
395 * by other users. Which means by this point the fb _must_ be fully set up -
396 * since all the fb attributes are invariant over its lifetime, no further
397 * locking but only correct reference counting is required.
400 * Zero on success, error code on failure.
402 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
403 const struct drm_framebuffer_funcs *funcs)
407 mutex_lock(&dev->mode_config.fb_lock);
408 kref_init(&fb->refcount);
409 INIT_LIST_HEAD(&fb->filp_head);
413 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
417 dev->mode_config.num_fb++;
418 list_add(&fb->head, &dev->mode_config.fb_list);
420 mutex_unlock(&dev->mode_config.fb_lock);
424 EXPORT_SYMBOL(drm_framebuffer_init);
426 /* dev->mode_config.fb_lock must be held! */
427 static void __drm_framebuffer_unregister(struct drm_device *dev,
428 struct drm_framebuffer *fb)
430 mutex_lock(&dev->mode_config.idr_mutex);
431 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
432 mutex_unlock(&dev->mode_config.idr_mutex);
437 static void drm_framebuffer_free(struct kref *kref)
439 struct drm_framebuffer *fb =
440 container_of(kref, struct drm_framebuffer, refcount);
441 struct drm_device *dev = fb->dev;
444 * The lookup idr holds a weak reference, which has not necessarily been
445 * removed at this point. Check for that.
447 mutex_lock(&dev->mode_config.fb_lock);
449 /* Mark fb as reaped and drop idr ref. */
450 __drm_framebuffer_unregister(dev, fb);
452 mutex_unlock(&dev->mode_config.fb_lock);
454 fb->funcs->destroy(fb);
457 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
460 struct drm_mode_object *obj = NULL;
461 struct drm_framebuffer *fb;
463 mutex_lock(&dev->mode_config.idr_mutex);
464 obj = idr_find(&dev->mode_config.crtc_idr, id);
465 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
469 mutex_unlock(&dev->mode_config.idr_mutex);
475 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
477 * @id: id of the fb object
479 * If successful, this grabs an additional reference to the framebuffer -
480 * callers need to make sure to eventually unreference the returned framebuffer
481 * again, using @drm_framebuffer_unreference.
483 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
486 struct drm_framebuffer *fb;
488 mutex_lock(&dev->mode_config.fb_lock);
489 fb = __drm_framebuffer_lookup(dev, id);
491 if (!kref_get_unless_zero(&fb->refcount))
494 mutex_unlock(&dev->mode_config.fb_lock);
498 EXPORT_SYMBOL(drm_framebuffer_lookup);
501 * drm_framebuffer_unreference - unref a framebuffer
502 * @fb: framebuffer to unref
504 * This functions decrements the fb's refcount and frees it if it drops to zero.
506 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
508 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
509 kref_put(&fb->refcount, drm_framebuffer_free);
511 EXPORT_SYMBOL(drm_framebuffer_unreference);
514 * drm_framebuffer_reference - incr the fb refcnt
517 * This functions increments the fb's refcount.
519 void drm_framebuffer_reference(struct drm_framebuffer *fb)
521 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
522 kref_get(&fb->refcount);
524 EXPORT_SYMBOL(drm_framebuffer_reference);
526 static void drm_framebuffer_free_bug(struct kref *kref)
531 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
533 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
534 kref_put(&fb->refcount, drm_framebuffer_free_bug);
538 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
539 * @fb: fb to unregister
541 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
542 * those used for fbdev. Note that the caller must hold a reference of it's own,
543 * i.e. the object may not be destroyed through this call (since it'll lead to a
544 * locking inversion).
546 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
548 struct drm_device *dev = fb->dev;
550 mutex_lock(&dev->mode_config.fb_lock);
551 /* Mark fb as reaped and drop idr ref. */
552 __drm_framebuffer_unregister(dev, fb);
553 mutex_unlock(&dev->mode_config.fb_lock);
555 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
558 * drm_framebuffer_cleanup - remove a framebuffer object
559 * @fb: framebuffer to remove
561 * Cleanup framebuffer. This function is intended to be used from the drivers
562 * ->destroy callback. It can also be used to clean up driver private
563 * framebuffers embedded into a larger structure.
565 * Note that this function does not remove the fb from active usuage - if it is
566 * still used anywhere, hilarity can ensue since userspace could call getfb on
567 * the id and get back -EINVAL. Obviously no concern at driver unload time.
569 * Also, the framebuffer will not be removed from the lookup idr - for
570 * user-created framebuffers this will happen in in the rmfb ioctl. For
571 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
572 * drm_framebuffer_unregister_private.
574 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
576 struct drm_device *dev = fb->dev;
578 mutex_lock(&dev->mode_config.fb_lock);
580 dev->mode_config.num_fb--;
581 mutex_unlock(&dev->mode_config.fb_lock);
583 EXPORT_SYMBOL(drm_framebuffer_cleanup);
586 * drm_framebuffer_remove - remove and unreference a framebuffer object
587 * @fb: framebuffer to remove
589 * Scans all the CRTCs and planes in @dev's mode_config. If they're
590 * using @fb, removes it, setting it to NULL. Then drops the reference to the
591 * passed-in framebuffer. Might take the modeset locks.
593 * Note that this function optimizes the cleanup away if the caller holds the
594 * last reference to the framebuffer. It is also guaranteed to not take the
595 * modeset locks in this case.
597 void drm_framebuffer_remove(struct drm_framebuffer *fb)
599 struct drm_device *dev = fb->dev;
600 struct drm_crtc *crtc;
601 struct drm_plane *plane;
602 struct drm_mode_set set;
605 WARN_ON(!list_empty(&fb->filp_head));
608 * drm ABI mandates that we remove any deleted framebuffers from active
609 * useage. But since most sane clients only remove framebuffers they no
610 * longer need, try to optimize this away.
612 * Since we're holding a reference ourselves, observing a refcount of 1
613 * means that we're the last holder and can skip it. Also, the refcount
614 * can never increase from 1 again, so we don't need any barriers or
617 * Note that userspace could try to race with use and instate a new
618 * usage _after_ we've cleared all current ones. End result will be an
619 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
622 if (atomic_read(&fb->refcount.refcount) > 1) {
623 drm_modeset_lock_all(dev);
624 /* remove from any CRTC */
625 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
626 if (crtc->primary->fb == fb) {
627 /* should turn off the crtc */
628 memset(&set, 0, sizeof(struct drm_mode_set));
631 ret = drm_mode_set_config_internal(&set);
633 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
637 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
639 drm_plane_force_disable(plane);
641 drm_modeset_unlock_all(dev);
644 drm_framebuffer_unreference(fb);
646 EXPORT_SYMBOL(drm_framebuffer_remove);
648 DEFINE_WW_CLASS(crtc_ww_class);
651 * drm_crtc_init_with_planes - Initialise a new CRTC object with
652 * specified primary and cursor planes.
654 * @crtc: CRTC object to init
655 * @primary: Primary plane for CRTC
656 * @cursor: Cursor plane for CRTC
657 * @funcs: callbacks for the new CRTC
659 * Inits a new object created as base part of a driver crtc object.
662 * Zero on success, error code on failure.
664 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
665 struct drm_plane *primary,
666 struct drm_plane *cursor,
667 const struct drm_crtc_funcs *funcs)
669 struct drm_mode_config *config = &dev->mode_config;
674 crtc->invert_dimensions = false;
676 drm_modeset_lock_init(&crtc->mutex);
677 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
681 crtc->base.properties = &crtc->properties;
683 list_add_tail(&crtc->head, &config->crtc_list);
686 crtc->primary = primary;
687 crtc->cursor = cursor;
689 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
691 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
695 EXPORT_SYMBOL(drm_crtc_init_with_planes);
698 * drm_crtc_cleanup - Clean up the core crtc usage
699 * @crtc: CRTC to cleanup
701 * This function cleans up @crtc and removes it from the DRM mode setting
702 * core. Note that the function does *not* free the crtc structure itself,
703 * this is the responsibility of the caller.
705 void drm_crtc_cleanup(struct drm_crtc *crtc)
707 struct drm_device *dev = crtc->dev;
709 kfree(crtc->gamma_store);
710 crtc->gamma_store = NULL;
712 drm_modeset_lock_fini(&crtc->mutex);
714 drm_mode_object_put(dev, &crtc->base);
715 list_del(&crtc->head);
716 dev->mode_config.num_crtc--;
718 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
719 if (crtc->state && crtc->funcs->atomic_destroy_state)
720 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
722 memset(crtc, 0, sizeof(*crtc));
724 EXPORT_SYMBOL(drm_crtc_cleanup);
727 * drm_crtc_index - find the index of a registered CRTC
728 * @crtc: CRTC to find index for
730 * Given a registered CRTC, return the index of that CRTC within a DRM
731 * device's list of CRTCs.
733 unsigned int drm_crtc_index(struct drm_crtc *crtc)
735 unsigned int index = 0;
736 struct drm_crtc *tmp;
738 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
747 EXPORT_SYMBOL(drm_crtc_index);
750 * drm_mode_remove - remove and free a mode
751 * @connector: connector list to modify
752 * @mode: mode to remove
754 * Remove @mode from @connector's mode list, then free it.
756 static void drm_mode_remove(struct drm_connector *connector,
757 struct drm_display_mode *mode)
759 list_del(&mode->head);
760 drm_mode_destroy(connector->dev, mode);
764 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
765 * @connector: connector to quwery
767 * The kernel supports per-connector configration of its consoles through
768 * use of the video= parameter. This function parses that option and
769 * extracts the user's specified mode (or enable/disable status) for a
770 * particular connector. This is typically only used during the early fbdev
773 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
775 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
778 if (fb_get_options(connector->name, &option))
781 if (!drm_mode_parse_command_line_for_connector(option,
789 switch (mode->force) {
793 case DRM_FORCE_ON_DIGITAL:
802 DRM_INFO("forcing %s connector %s\n", connector->name, s);
803 connector->force = mode->force;
806 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
808 mode->xres, mode->yres,
809 mode->refresh_specified ? mode->refresh : 60,
810 mode->rb ? " reduced blanking" : "",
811 mode->margins ? " with margins" : "",
812 mode->interlace ? " interlaced" : "");
816 * drm_connector_init - Init a preallocated connector
818 * @connector: the connector to init
819 * @funcs: callbacks for this connector
820 * @connector_type: user visible type of the connector
822 * Initialises a preallocated connector. Connectors should be
823 * subclassed as part of driver connector objects.
826 * Zero on success, error code on failure.
828 int drm_connector_init(struct drm_device *dev,
829 struct drm_connector *connector,
830 const struct drm_connector_funcs *funcs,
834 struct ida *connector_ida =
835 &drm_connector_enum_list[connector_type].ida;
837 drm_modeset_lock_all(dev);
839 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
843 connector->base.properties = &connector->properties;
844 connector->dev = dev;
845 connector->funcs = funcs;
846 connector->connector_type = connector_type;
847 connector->connector_type_id =
848 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
849 if (connector->connector_type_id < 0) {
850 ret = connector->connector_type_id;
854 kasprintf(GFP_KERNEL, "%s-%d",
855 drm_connector_enum_list[connector_type].name,
856 connector->connector_type_id);
857 if (!connector->name) {
862 INIT_LIST_HEAD(&connector->probed_modes);
863 INIT_LIST_HEAD(&connector->modes);
864 connector->edid_blob_ptr = NULL;
865 connector->status = connector_status_unknown;
867 drm_connector_get_cmdline_mode(connector);
869 /* We should add connectors at the end to avoid upsetting the connector
871 list_add_tail(&connector->head, &dev->mode_config.connector_list);
872 dev->mode_config.num_connector++;
874 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
875 drm_object_attach_property(&connector->base,
876 dev->mode_config.edid_property,
879 drm_object_attach_property(&connector->base,
880 dev->mode_config.dpms_property, 0);
882 connector->debugfs_entry = NULL;
886 drm_mode_object_put(dev, &connector->base);
889 drm_modeset_unlock_all(dev);
893 EXPORT_SYMBOL(drm_connector_init);
896 * drm_connector_cleanup - cleans up an initialised connector
897 * @connector: connector to cleanup
899 * Cleans up the connector but doesn't free the object.
901 void drm_connector_cleanup(struct drm_connector *connector)
903 struct drm_device *dev = connector->dev;
904 struct drm_display_mode *mode, *t;
906 if (connector->tile_group) {
907 drm_mode_put_tile_group(dev, connector->tile_group);
908 connector->tile_group = NULL;
911 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
912 drm_mode_remove(connector, mode);
914 list_for_each_entry_safe(mode, t, &connector->modes, head)
915 drm_mode_remove(connector, mode);
917 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
918 connector->connector_type_id);
920 drm_mode_object_put(dev, &connector->base);
921 kfree(connector->name);
922 connector->name = NULL;
923 list_del(&connector->head);
924 dev->mode_config.num_connector--;
926 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
927 if (connector->state && connector->funcs->atomic_destroy_state)
928 connector->funcs->atomic_destroy_state(connector,
931 memset(connector, 0, sizeof(*connector));
933 EXPORT_SYMBOL(drm_connector_cleanup);
936 * drm_connector_index - find the index of a registered connector
937 * @connector: connector to find index for
939 * Given a registered connector, return the index of that connector within a DRM
940 * device's list of connectors.
942 unsigned int drm_connector_index(struct drm_connector *connector)
944 unsigned int index = 0;
945 struct drm_connector *tmp;
946 struct drm_mode_config *config = &connector->dev->mode_config;
948 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
950 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
951 if (tmp == connector)
959 EXPORT_SYMBOL(drm_connector_index);
962 * drm_connector_register - register a connector
963 * @connector: the connector to register
965 * Register userspace interfaces for a connector
968 * Zero on success, error code on failure.
970 int drm_connector_register(struct drm_connector *connector)
974 drm_mode_object_register(connector->dev, &connector->base);
976 ret = drm_sysfs_connector_add(connector);
980 ret = drm_debugfs_connector_add(connector);
982 drm_sysfs_connector_remove(connector);
988 EXPORT_SYMBOL(drm_connector_register);
991 * drm_connector_unregister - unregister a connector
992 * @connector: the connector to unregister
994 * Unregister userspace interfaces for a connector
996 void drm_connector_unregister(struct drm_connector *connector)
998 drm_sysfs_connector_remove(connector);
999 drm_debugfs_connector_remove(connector);
1001 EXPORT_SYMBOL(drm_connector_unregister);
1005 * drm_connector_unplug_all - unregister connector userspace interfaces
1008 * This function unregisters all connector userspace interfaces in sysfs. Should
1009 * be call when the device is disconnected, e.g. from an usb driver's
1010 * ->disconnect callback.
1012 void drm_connector_unplug_all(struct drm_device *dev)
1014 struct drm_connector *connector;
1016 /* taking the mode config mutex ends up in a clash with sysfs */
1017 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1018 drm_connector_unregister(connector);
1021 EXPORT_SYMBOL(drm_connector_unplug_all);
1024 * drm_bridge_init - initialize a drm transcoder/bridge
1026 * @bridge: transcoder/bridge to set up
1027 * @funcs: bridge function table
1029 * Initialises a preallocated bridge. Bridges should be
1030 * subclassed as part of driver connector objects.
1033 * Zero on success, error code on failure.
1035 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1036 const struct drm_bridge_funcs *funcs)
1040 drm_modeset_lock_all(dev);
1042 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1047 bridge->funcs = funcs;
1049 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1050 dev->mode_config.num_bridge++;
1053 drm_modeset_unlock_all(dev);
1056 EXPORT_SYMBOL(drm_bridge_init);
1059 * drm_bridge_cleanup - cleans up an initialised bridge
1060 * @bridge: bridge to cleanup
1062 * Cleans up the bridge but doesn't free the object.
1064 void drm_bridge_cleanup(struct drm_bridge *bridge)
1066 struct drm_device *dev = bridge->dev;
1068 drm_modeset_lock_all(dev);
1069 drm_mode_object_put(dev, &bridge->base);
1070 list_del(&bridge->head);
1071 dev->mode_config.num_bridge--;
1072 drm_modeset_unlock_all(dev);
1074 memset(bridge, 0, sizeof(*bridge));
1076 EXPORT_SYMBOL(drm_bridge_cleanup);
1079 * drm_encoder_init - Init a preallocated encoder
1081 * @encoder: the encoder to init
1082 * @funcs: callbacks for this encoder
1083 * @encoder_type: user visible type of the encoder
1085 * Initialises a preallocated encoder. Encoder should be
1086 * subclassed as part of driver encoder objects.
1089 * Zero on success, error code on failure.
1091 int drm_encoder_init(struct drm_device *dev,
1092 struct drm_encoder *encoder,
1093 const struct drm_encoder_funcs *funcs,
1098 drm_modeset_lock_all(dev);
1100 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1105 encoder->encoder_type = encoder_type;
1106 encoder->funcs = funcs;
1107 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1108 drm_encoder_enum_list[encoder_type].name,
1110 if (!encoder->name) {
1115 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1116 dev->mode_config.num_encoder++;
1120 drm_mode_object_put(dev, &encoder->base);
1123 drm_modeset_unlock_all(dev);
1127 EXPORT_SYMBOL(drm_encoder_init);
1130 * drm_encoder_cleanup - cleans up an initialised encoder
1131 * @encoder: encoder to cleanup
1133 * Cleans up the encoder but doesn't free the object.
1135 void drm_encoder_cleanup(struct drm_encoder *encoder)
1137 struct drm_device *dev = encoder->dev;
1139 drm_modeset_lock_all(dev);
1140 drm_mode_object_put(dev, &encoder->base);
1141 kfree(encoder->name);
1142 list_del(&encoder->head);
1143 dev->mode_config.num_encoder--;
1144 drm_modeset_unlock_all(dev);
1146 memset(encoder, 0, sizeof(*encoder));
1148 EXPORT_SYMBOL(drm_encoder_cleanup);
1151 * drm_universal_plane_init - Initialize a new universal plane object
1153 * @plane: plane object to init
1154 * @possible_crtcs: bitmask of possible CRTCs
1155 * @funcs: callbacks for the new plane
1156 * @formats: array of supported formats (%DRM_FORMAT_*)
1157 * @format_count: number of elements in @formats
1158 * @type: type of plane (overlay, primary, cursor)
1160 * Initializes a plane object of type @type.
1163 * Zero on success, error code on failure.
1165 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1166 unsigned long possible_crtcs,
1167 const struct drm_plane_funcs *funcs,
1168 const uint32_t *formats, uint32_t format_count,
1169 enum drm_plane_type type)
1173 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1177 drm_modeset_lock_init(&plane->mutex);
1179 plane->base.properties = &plane->properties;
1181 plane->funcs = funcs;
1182 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1184 if (!plane->format_types) {
1185 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1186 drm_mode_object_put(dev, &plane->base);
1190 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1191 plane->format_count = format_count;
1192 plane->possible_crtcs = possible_crtcs;
1195 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1196 dev->mode_config.num_total_plane++;
1197 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1198 dev->mode_config.num_overlay_plane++;
1200 drm_object_attach_property(&plane->base,
1201 dev->mode_config.plane_type_property,
1206 EXPORT_SYMBOL(drm_universal_plane_init);
1209 * drm_plane_init - Initialize a legacy plane
1211 * @plane: plane object to init
1212 * @possible_crtcs: bitmask of possible CRTCs
1213 * @funcs: callbacks for the new plane
1214 * @formats: array of supported formats (%DRM_FORMAT_*)
1215 * @format_count: number of elements in @formats
1216 * @is_primary: plane type (primary vs overlay)
1218 * Legacy API to initialize a DRM plane.
1220 * New drivers should call drm_universal_plane_init() instead.
1223 * Zero on success, error code on failure.
1225 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1226 unsigned long possible_crtcs,
1227 const struct drm_plane_funcs *funcs,
1228 const uint32_t *formats, uint32_t format_count,
1231 enum drm_plane_type type;
1233 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1234 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1235 formats, format_count, type);
1237 EXPORT_SYMBOL(drm_plane_init);
1240 * drm_plane_cleanup - Clean up the core plane usage
1241 * @plane: plane to cleanup
1243 * This function cleans up @plane and removes it from the DRM mode setting
1244 * core. Note that the function does *not* free the plane structure itself,
1245 * this is the responsibility of the caller.
1247 void drm_plane_cleanup(struct drm_plane *plane)
1249 struct drm_device *dev = plane->dev;
1251 drm_modeset_lock_all(dev);
1252 kfree(plane->format_types);
1253 drm_mode_object_put(dev, &plane->base);
1255 BUG_ON(list_empty(&plane->head));
1257 list_del(&plane->head);
1258 dev->mode_config.num_total_plane--;
1259 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1260 dev->mode_config.num_overlay_plane--;
1261 drm_modeset_unlock_all(dev);
1263 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1264 if (plane->state && plane->funcs->atomic_destroy_state)
1265 plane->funcs->atomic_destroy_state(plane, plane->state);
1267 memset(plane, 0, sizeof(*plane));
1269 EXPORT_SYMBOL(drm_plane_cleanup);
1272 * drm_plane_index - find the index of a registered plane
1273 * @plane: plane to find index for
1275 * Given a registered plane, return the index of that CRTC within a DRM
1276 * device's list of planes.
1278 unsigned int drm_plane_index(struct drm_plane *plane)
1280 unsigned int index = 0;
1281 struct drm_plane *tmp;
1283 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1292 EXPORT_SYMBOL(drm_plane_index);
1295 * drm_plane_force_disable - Forcibly disable a plane
1296 * @plane: plane to disable
1298 * Forces the plane to be disabled.
1300 * Used when the plane's current framebuffer is destroyed,
1301 * and when restoring fbdev mode.
1303 void drm_plane_force_disable(struct drm_plane *plane)
1310 plane->old_fb = plane->fb;
1311 ret = plane->funcs->disable_plane(plane);
1313 DRM_ERROR("failed to disable plane with busy fb\n");
1314 plane->old_fb = NULL;
1317 /* disconnect the plane from the fb and crtc: */
1318 __drm_framebuffer_unreference(plane->old_fb);
1319 plane->old_fb = NULL;
1323 EXPORT_SYMBOL(drm_plane_force_disable);
1325 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1327 struct drm_property *edid;
1328 struct drm_property *dpms;
1329 struct drm_property *dev_path;
1332 * Standard properties (apply to all connectors)
1334 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1335 DRM_MODE_PROP_IMMUTABLE,
1337 dev->mode_config.edid_property = edid;
1339 dpms = drm_property_create_enum(dev, 0,
1340 "DPMS", drm_dpms_enum_list,
1341 ARRAY_SIZE(drm_dpms_enum_list));
1342 dev->mode_config.dpms_property = dpms;
1344 dev_path = drm_property_create(dev,
1345 DRM_MODE_PROP_BLOB |
1346 DRM_MODE_PROP_IMMUTABLE,
1348 dev->mode_config.path_property = dev_path;
1350 dev->mode_config.tile_property = drm_property_create(dev,
1351 DRM_MODE_PROP_BLOB |
1352 DRM_MODE_PROP_IMMUTABLE,
1358 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1360 struct drm_property *type;
1363 * Standard properties (apply to all planes)
1365 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1366 "type", drm_plane_type_enum_list,
1367 ARRAY_SIZE(drm_plane_type_enum_list));
1368 dev->mode_config.plane_type_property = type;
1374 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1377 * Called by a driver the first time a DVI-I connector is made.
1379 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1381 struct drm_property *dvi_i_selector;
1382 struct drm_property *dvi_i_subconnector;
1384 if (dev->mode_config.dvi_i_select_subconnector_property)
1388 drm_property_create_enum(dev, 0,
1389 "select subconnector",
1390 drm_dvi_i_select_enum_list,
1391 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1392 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1394 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1396 drm_dvi_i_subconnector_enum_list,
1397 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1398 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1402 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1405 * drm_create_tv_properties - create TV specific connector properties
1407 * @num_modes: number of different TV formats (modes) supported
1408 * @modes: array of pointers to strings containing name of each format
1410 * Called by a driver's TV initialization routine, this function creates
1411 * the TV specific connector properties for a given device. Caller is
1412 * responsible for allocating a list of format names and passing them to
1415 int drm_mode_create_tv_properties(struct drm_device *dev,
1416 unsigned int num_modes,
1419 struct drm_property *tv_selector;
1420 struct drm_property *tv_subconnector;
1423 if (dev->mode_config.tv_select_subconnector_property)
1427 * Basic connector properties
1429 tv_selector = drm_property_create_enum(dev, 0,
1430 "select subconnector",
1431 drm_tv_select_enum_list,
1432 ARRAY_SIZE(drm_tv_select_enum_list));
1433 dev->mode_config.tv_select_subconnector_property = tv_selector;
1436 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1438 drm_tv_subconnector_enum_list,
1439 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1440 dev->mode_config.tv_subconnector_property = tv_subconnector;
1443 * Other, TV specific properties: margins & TV modes.
1445 dev->mode_config.tv_left_margin_property =
1446 drm_property_create_range(dev, 0, "left margin", 0, 100);
1448 dev->mode_config.tv_right_margin_property =
1449 drm_property_create_range(dev, 0, "right margin", 0, 100);
1451 dev->mode_config.tv_top_margin_property =
1452 drm_property_create_range(dev, 0, "top margin", 0, 100);
1454 dev->mode_config.tv_bottom_margin_property =
1455 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1457 dev->mode_config.tv_mode_property =
1458 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1460 for (i = 0; i < num_modes; i++)
1461 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1464 dev->mode_config.tv_brightness_property =
1465 drm_property_create_range(dev, 0, "brightness", 0, 100);
1467 dev->mode_config.tv_contrast_property =
1468 drm_property_create_range(dev, 0, "contrast", 0, 100);
1470 dev->mode_config.tv_flicker_reduction_property =
1471 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1473 dev->mode_config.tv_overscan_property =
1474 drm_property_create_range(dev, 0, "overscan", 0, 100);
1476 dev->mode_config.tv_saturation_property =
1477 drm_property_create_range(dev, 0, "saturation", 0, 100);
1479 dev->mode_config.tv_hue_property =
1480 drm_property_create_range(dev, 0, "hue", 0, 100);
1484 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1487 * drm_mode_create_scaling_mode_property - create scaling mode property
1490 * Called by a driver the first time it's needed, must be attached to desired
1493 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1495 struct drm_property *scaling_mode;
1497 if (dev->mode_config.scaling_mode_property)
1501 drm_property_create_enum(dev, 0, "scaling mode",
1502 drm_scaling_mode_enum_list,
1503 ARRAY_SIZE(drm_scaling_mode_enum_list));
1505 dev->mode_config.scaling_mode_property = scaling_mode;
1509 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1512 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1515 * Called by a driver the first time it's needed, must be attached to desired
1519 * Zero on success, negative errno on failure.
1521 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1523 if (dev->mode_config.aspect_ratio_property)
1526 dev->mode_config.aspect_ratio_property =
1527 drm_property_create_enum(dev, 0, "aspect ratio",
1528 drm_aspect_ratio_enum_list,
1529 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1531 if (dev->mode_config.aspect_ratio_property == NULL)
1536 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1539 * drm_mode_create_dirty_property - create dirty property
1542 * Called by a driver the first time it's needed, must be attached to desired
1545 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1547 struct drm_property *dirty_info;
1549 if (dev->mode_config.dirty_info_property)
1553 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1555 drm_dirty_info_enum_list,
1556 ARRAY_SIZE(drm_dirty_info_enum_list));
1557 dev->mode_config.dirty_info_property = dirty_info;
1561 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1564 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1567 * Create the the suggested x/y offset property for connectors.
1569 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1571 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1574 dev->mode_config.suggested_x_property =
1575 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1577 dev->mode_config.suggested_y_property =
1578 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1580 if (dev->mode_config.suggested_x_property == NULL ||
1581 dev->mode_config.suggested_y_property == NULL)
1585 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1587 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1589 uint32_t total_objects = 0;
1591 total_objects += dev->mode_config.num_crtc;
1592 total_objects += dev->mode_config.num_connector;
1593 total_objects += dev->mode_config.num_encoder;
1594 total_objects += dev->mode_config.num_bridge;
1596 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
1597 if (!group->id_list)
1600 group->num_crtcs = 0;
1601 group->num_connectors = 0;
1602 group->num_encoders = 0;
1603 group->num_bridges = 0;
1607 void drm_mode_group_destroy(struct drm_mode_group *group)
1609 kfree(group->id_list);
1610 group->id_list = NULL;
1614 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1615 * the drm core's responsibility to set up mode control groups.
1617 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1618 struct drm_mode_group *group)
1620 struct drm_crtc *crtc;
1621 struct drm_encoder *encoder;
1622 struct drm_connector *connector;
1623 struct drm_bridge *bridge;
1626 ret = drm_mode_group_init(dev, group);
1630 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1631 group->id_list[group->num_crtcs++] = crtc->base.id;
1633 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1634 group->id_list[group->num_crtcs + group->num_encoders++] =
1637 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1638 group->id_list[group->num_crtcs + group->num_encoders +
1639 group->num_connectors++] = connector->base.id;
1641 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1642 group->id_list[group->num_crtcs + group->num_encoders +
1643 group->num_connectors + group->num_bridges++] =
1648 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1650 void drm_reinit_primary_mode_group(struct drm_device *dev)
1652 drm_modeset_lock_all(dev);
1653 drm_mode_group_destroy(&dev->primary->mode_group);
1654 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1655 drm_modeset_unlock_all(dev);
1657 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1660 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1661 * @out: drm_mode_modeinfo struct to return to the user
1662 * @in: drm_display_mode to use
1664 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1667 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1668 const struct drm_display_mode *in)
1670 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1671 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1672 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1673 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1674 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1675 "timing values too large for mode info\n");
1677 out->clock = in->clock;
1678 out->hdisplay = in->hdisplay;
1679 out->hsync_start = in->hsync_start;
1680 out->hsync_end = in->hsync_end;
1681 out->htotal = in->htotal;
1682 out->hskew = in->hskew;
1683 out->vdisplay = in->vdisplay;
1684 out->vsync_start = in->vsync_start;
1685 out->vsync_end = in->vsync_end;
1686 out->vtotal = in->vtotal;
1687 out->vscan = in->vscan;
1688 out->vrefresh = in->vrefresh;
1689 out->flags = in->flags;
1690 out->type = in->type;
1691 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1692 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1696 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1697 * @out: drm_display_mode to return to the user
1698 * @in: drm_mode_modeinfo to use
1700 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1704 * Zero on success, negative errno on failure.
1706 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1707 const struct drm_mode_modeinfo *in)
1709 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1712 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1715 out->clock = in->clock;
1716 out->hdisplay = in->hdisplay;
1717 out->hsync_start = in->hsync_start;
1718 out->hsync_end = in->hsync_end;
1719 out->htotal = in->htotal;
1720 out->hskew = in->hskew;
1721 out->vdisplay = in->vdisplay;
1722 out->vsync_start = in->vsync_start;
1723 out->vsync_end = in->vsync_end;
1724 out->vtotal = in->vtotal;
1725 out->vscan = in->vscan;
1726 out->vrefresh = in->vrefresh;
1727 out->flags = in->flags;
1728 out->type = in->type;
1729 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1730 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1736 * drm_mode_getresources - get graphics configuration
1737 * @dev: drm device for the ioctl
1738 * @data: data pointer for the ioctl
1739 * @file_priv: drm file for the ioctl call
1741 * Construct a set of configuration description structures and return
1742 * them to the user, including CRTC, connector and framebuffer configuration.
1744 * Called by the user via ioctl.
1747 * Zero on success, negative errno on failure.
1749 int drm_mode_getresources(struct drm_device *dev, void *data,
1750 struct drm_file *file_priv)
1752 struct drm_mode_card_res *card_res = data;
1753 struct list_head *lh;
1754 struct drm_framebuffer *fb;
1755 struct drm_connector *connector;
1756 struct drm_crtc *crtc;
1757 struct drm_encoder *encoder;
1759 int connector_count = 0;
1762 int encoder_count = 0;
1764 uint32_t __user *fb_id;
1765 uint32_t __user *crtc_id;
1766 uint32_t __user *connector_id;
1767 uint32_t __user *encoder_id;
1768 struct drm_mode_group *mode_group;
1770 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1774 mutex_lock(&file_priv->fbs_lock);
1776 * For the non-control nodes we need to limit the list of resources
1777 * by IDs in the group list for this node
1779 list_for_each(lh, &file_priv->fbs)
1782 /* handle this in 4 parts */
1784 if (card_res->count_fbs >= fb_count) {
1786 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1787 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1788 if (put_user(fb->base.id, fb_id + copied)) {
1789 mutex_unlock(&file_priv->fbs_lock);
1795 card_res->count_fbs = fb_count;
1796 mutex_unlock(&file_priv->fbs_lock);
1798 /* mode_config.mutex protects the connector list against e.g. DP MST
1799 * connector hot-adding. CRTC/Plane lists are invariant. */
1800 mutex_lock(&dev->mode_config.mutex);
1801 if (!drm_is_primary_client(file_priv)) {
1804 list_for_each(lh, &dev->mode_config.crtc_list)
1807 list_for_each(lh, &dev->mode_config.connector_list)
1810 list_for_each(lh, &dev->mode_config.encoder_list)
1814 mode_group = &file_priv->master->minor->mode_group;
1815 crtc_count = mode_group->num_crtcs;
1816 connector_count = mode_group->num_connectors;
1817 encoder_count = mode_group->num_encoders;
1820 card_res->max_height = dev->mode_config.max_height;
1821 card_res->min_height = dev->mode_config.min_height;
1822 card_res->max_width = dev->mode_config.max_width;
1823 card_res->min_width = dev->mode_config.min_width;
1826 if (card_res->count_crtcs >= crtc_count) {
1828 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1830 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1832 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1833 if (put_user(crtc->base.id, crtc_id + copied)) {
1840 for (i = 0; i < mode_group->num_crtcs; i++) {
1841 if (put_user(mode_group->id_list[i],
1842 crtc_id + copied)) {
1850 card_res->count_crtcs = crtc_count;
1853 if (card_res->count_encoders >= encoder_count) {
1855 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1857 list_for_each_entry(encoder,
1858 &dev->mode_config.encoder_list,
1860 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1862 if (put_user(encoder->base.id, encoder_id +
1870 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1871 if (put_user(mode_group->id_list[i],
1872 encoder_id + copied)) {
1881 card_res->count_encoders = encoder_count;
1884 if (card_res->count_connectors >= connector_count) {
1886 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1888 list_for_each_entry(connector,
1889 &dev->mode_config.connector_list,
1891 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1894 if (put_user(connector->base.id,
1895 connector_id + copied)) {
1902 int start = mode_group->num_crtcs +
1903 mode_group->num_encoders;
1904 for (i = start; i < start + mode_group->num_connectors; i++) {
1905 if (put_user(mode_group->id_list[i],
1906 connector_id + copied)) {
1914 card_res->count_connectors = connector_count;
1916 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1917 card_res->count_connectors, card_res->count_encoders);
1920 mutex_unlock(&dev->mode_config.mutex);
1925 * drm_mode_getcrtc - get CRTC configuration
1926 * @dev: drm device for the ioctl
1927 * @data: data pointer for the ioctl
1928 * @file_priv: drm file for the ioctl call
1930 * Construct a CRTC configuration structure to return to the user.
1932 * Called by the user via ioctl.
1935 * Zero on success, negative errno on failure.
1937 int drm_mode_getcrtc(struct drm_device *dev,
1938 void *data, struct drm_file *file_priv)
1940 struct drm_mode_crtc *crtc_resp = data;
1941 struct drm_crtc *crtc;
1943 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1946 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1950 drm_modeset_lock_crtc(crtc, crtc->primary);
1951 crtc_resp->x = crtc->x;
1952 crtc_resp->y = crtc->y;
1953 crtc_resp->gamma_size = crtc->gamma_size;
1954 if (crtc->primary->fb)
1955 crtc_resp->fb_id = crtc->primary->fb->base.id;
1957 crtc_resp->fb_id = 0;
1959 if (crtc->enabled) {
1961 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1962 crtc_resp->mode_valid = 1;
1965 crtc_resp->mode_valid = 0;
1967 drm_modeset_unlock_crtc(crtc);
1972 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1973 const struct drm_file *file_priv)
1976 * If user-space hasn't configured the driver to expose the stereo 3D
1977 * modes, don't expose them.
1979 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1985 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1987 /* For atomic drivers only state objects are synchronously updated and
1988 * protected by modeset locks, so check those first. */
1989 if (connector->state)
1990 return connector->state->best_encoder;
1991 return connector->encoder;
1995 * drm_mode_getconnector - get connector configuration
1996 * @dev: drm device for the ioctl
1997 * @data: data pointer for the ioctl
1998 * @file_priv: drm file for the ioctl call
2000 * Construct a connector configuration structure to return to the user.
2002 * Called by the user via ioctl.
2005 * Zero on success, negative errno on failure.
2007 int drm_mode_getconnector(struct drm_device *dev, void *data,
2008 struct drm_file *file_priv)
2010 struct drm_mode_get_connector *out_resp = data;
2011 struct drm_connector *connector;
2012 struct drm_encoder *encoder;
2013 struct drm_display_mode *mode;
2015 int props_count = 0;
2016 int encoders_count = 0;
2020 struct drm_mode_modeinfo u_mode;
2021 struct drm_mode_modeinfo __user *mode_ptr;
2022 uint32_t __user *prop_ptr;
2023 uint64_t __user *prop_values;
2024 uint32_t __user *encoder_ptr;
2026 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2029 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2031 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2033 mutex_lock(&dev->mode_config.mutex);
2035 connector = drm_connector_find(dev, out_resp->connector_id);
2041 props_count = connector->properties.count;
2043 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2044 if (connector->encoder_ids[i] != 0)
2047 if (out_resp->count_modes == 0) {
2048 connector->funcs->fill_modes(connector,
2049 dev->mode_config.max_width,
2050 dev->mode_config.max_height);
2053 /* delayed so we get modes regardless of pre-fill_modes state */
2054 list_for_each_entry(mode, &connector->modes, head)
2055 if (drm_mode_expose_to_userspace(mode, file_priv))
2058 out_resp->connector_id = connector->base.id;
2059 out_resp->connector_type = connector->connector_type;
2060 out_resp->connector_type_id = connector->connector_type_id;
2061 out_resp->mm_width = connector->display_info.width_mm;
2062 out_resp->mm_height = connector->display_info.height_mm;
2063 out_resp->subpixel = connector->display_info.subpixel_order;
2064 out_resp->connection = connector->status;
2065 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2067 encoder = drm_connector_get_encoder(connector);
2069 out_resp->encoder_id = encoder->base.id;
2071 out_resp->encoder_id = 0;
2072 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2075 * This ioctl is called twice, once to determine how much space is
2076 * needed, and the 2nd time to fill it.
2078 if ((out_resp->count_modes >= mode_count) && mode_count) {
2080 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2081 list_for_each_entry(mode, &connector->modes, head) {
2082 if (!drm_mode_expose_to_userspace(mode, file_priv))
2085 drm_crtc_convert_to_umode(&u_mode, mode);
2086 if (copy_to_user(mode_ptr + copied,
2087 &u_mode, sizeof(u_mode))) {
2094 out_resp->count_modes = mode_count;
2096 if ((out_resp->count_props >= props_count) && props_count) {
2098 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
2099 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
2100 for (i = 0; i < connector->properties.count; i++) {
2101 if (put_user(connector->properties.ids[i],
2102 prop_ptr + copied)) {
2107 if (put_user(connector->properties.values[i],
2108 prop_values + copied)) {
2115 out_resp->count_props = props_count;
2117 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2119 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2120 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2121 if (connector->encoder_ids[i] != 0) {
2122 if (put_user(connector->encoder_ids[i],
2123 encoder_ptr + copied)) {
2131 out_resp->count_encoders = encoders_count;
2134 mutex_unlock(&dev->mode_config.mutex);
2139 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2141 struct drm_connector *connector;
2142 struct drm_device *dev = encoder->dev;
2143 bool uses_atomic = false;
2145 /* For atomic drivers only state objects are synchronously updated and
2146 * protected by modeset locks, so check those first. */
2147 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2148 if (!connector->state)
2153 if (connector->state->best_encoder != encoder)
2156 return connector->state->crtc;
2159 /* Don't return stale data (e.g. pending async disable). */
2163 return encoder->crtc;
2167 * drm_mode_getencoder - get encoder configuration
2168 * @dev: drm device for the ioctl
2169 * @data: data pointer for the ioctl
2170 * @file_priv: drm file for the ioctl call
2172 * Construct a encoder configuration structure to return to the user.
2174 * Called by the user via ioctl.
2177 * Zero on success, negative errno on failure.
2179 int drm_mode_getencoder(struct drm_device *dev, void *data,
2180 struct drm_file *file_priv)
2182 struct drm_mode_get_encoder *enc_resp = data;
2183 struct drm_encoder *encoder;
2184 struct drm_crtc *crtc;
2186 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2189 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2193 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2194 crtc = drm_encoder_get_crtc(encoder);
2196 enc_resp->crtc_id = crtc->base.id;
2197 else if (encoder->crtc)
2198 enc_resp->crtc_id = encoder->crtc->base.id;
2200 enc_resp->crtc_id = 0;
2201 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2203 enc_resp->encoder_type = encoder->encoder_type;
2204 enc_resp->encoder_id = encoder->base.id;
2205 enc_resp->possible_crtcs = encoder->possible_crtcs;
2206 enc_resp->possible_clones = encoder->possible_clones;
2212 * drm_mode_getplane_res - enumerate all plane resources
2215 * @file_priv: DRM file info
2217 * Construct a list of plane ids to return to the user.
2219 * Called by the user via ioctl.
2222 * Zero on success, negative errno on failure.
2224 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2225 struct drm_file *file_priv)
2227 struct drm_mode_get_plane_res *plane_resp = data;
2228 struct drm_mode_config *config;
2229 struct drm_plane *plane;
2230 uint32_t __user *plane_ptr;
2232 unsigned num_planes;
2234 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2237 config = &dev->mode_config;
2239 if (file_priv->universal_planes)
2240 num_planes = config->num_total_plane;
2242 num_planes = config->num_overlay_plane;
2245 * This ioctl is called twice, once to determine how much space is
2246 * needed, and the 2nd time to fill it.
2249 (plane_resp->count_planes >= num_planes)) {
2250 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2252 /* Plane lists are invariant, no locking needed. */
2253 list_for_each_entry(plane, &config->plane_list, head) {
2255 * Unless userspace set the 'universal planes'
2256 * capability bit, only advertise overlays.
2258 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2259 !file_priv->universal_planes)
2262 if (put_user(plane->base.id, plane_ptr + copied))
2267 plane_resp->count_planes = num_planes;
2273 * drm_mode_getplane - get plane configuration
2276 * @file_priv: DRM file info
2278 * Construct a plane configuration structure to return to the user.
2280 * Called by the user via ioctl.
2283 * Zero on success, negative errno on failure.
2285 int drm_mode_getplane(struct drm_device *dev, void *data,
2286 struct drm_file *file_priv)
2288 struct drm_mode_get_plane *plane_resp = data;
2289 struct drm_plane *plane;
2290 uint32_t __user *format_ptr;
2292 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2295 plane = drm_plane_find(dev, plane_resp->plane_id);
2299 drm_modeset_lock(&plane->mutex, NULL);
2301 plane_resp->crtc_id = plane->crtc->base.id;
2303 plane_resp->crtc_id = 0;
2306 plane_resp->fb_id = plane->fb->base.id;
2308 plane_resp->fb_id = 0;
2309 drm_modeset_unlock(&plane->mutex);
2311 plane_resp->plane_id = plane->base.id;
2312 plane_resp->possible_crtcs = plane->possible_crtcs;
2313 plane_resp->gamma_size = 0;
2316 * This ioctl is called twice, once to determine how much space is
2317 * needed, and the 2nd time to fill it.
2319 if (plane->format_count &&
2320 (plane_resp->count_format_types >= plane->format_count)) {
2321 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2322 if (copy_to_user(format_ptr,
2323 plane->format_types,
2324 sizeof(uint32_t) * plane->format_count)) {
2328 plane_resp->count_format_types = plane->format_count;
2334 * setplane_internal - setplane handler for internal callers
2336 * Note that we assume an extra reference has already been taken on fb. If the
2337 * update fails, this reference will be dropped before return; if it succeeds,
2338 * the previous framebuffer (if any) will be unreferenced instead.
2340 * src_{x,y,w,h} are provided in 16.16 fixed point format
2342 static int __setplane_internal(struct drm_plane *plane,
2343 struct drm_crtc *crtc,
2344 struct drm_framebuffer *fb,
2345 int32_t crtc_x, int32_t crtc_y,
2346 uint32_t crtc_w, uint32_t crtc_h,
2347 /* src_{x,y,w,h} values are 16.16 fixed point */
2348 uint32_t src_x, uint32_t src_y,
2349 uint32_t src_w, uint32_t src_h)
2352 unsigned int fb_width, fb_height;
2355 /* No fb means shut it down */
2357 plane->old_fb = plane->fb;
2358 ret = plane->funcs->disable_plane(plane);
2363 plane->old_fb = NULL;
2368 /* Check whether this plane is usable on this CRTC */
2369 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2370 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2375 /* Check whether this plane supports the fb pixel format. */
2376 for (i = 0; i < plane->format_count; i++)
2377 if (fb->pixel_format == plane->format_types[i])
2379 if (i == plane->format_count) {
2380 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2381 drm_get_format_name(fb->pixel_format));
2386 fb_width = fb->width << 16;
2387 fb_height = fb->height << 16;
2389 /* Make sure source coordinates are inside the fb. */
2390 if (src_w > fb_width ||
2391 src_x > fb_width - src_w ||
2392 src_h > fb_height ||
2393 src_y > fb_height - src_h) {
2394 DRM_DEBUG_KMS("Invalid source coordinates "
2395 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2396 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2397 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2398 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2399 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2404 plane->old_fb = plane->fb;
2405 ret = plane->funcs->update_plane(plane, crtc, fb,
2406 crtc_x, crtc_y, crtc_w, crtc_h,
2407 src_x, src_y, src_w, src_h);
2413 plane->old_fb = NULL;
2418 drm_framebuffer_unreference(fb);
2420 drm_framebuffer_unreference(plane->old_fb);
2421 plane->old_fb = NULL;
2426 static int setplane_internal(struct drm_plane *plane,
2427 struct drm_crtc *crtc,
2428 struct drm_framebuffer *fb,
2429 int32_t crtc_x, int32_t crtc_y,
2430 uint32_t crtc_w, uint32_t crtc_h,
2431 /* src_{x,y,w,h} values are 16.16 fixed point */
2432 uint32_t src_x, uint32_t src_y,
2433 uint32_t src_w, uint32_t src_h)
2437 drm_modeset_lock_all(plane->dev);
2438 ret = __setplane_internal(plane, crtc, fb,
2439 crtc_x, crtc_y, crtc_w, crtc_h,
2440 src_x, src_y, src_w, src_h);
2441 drm_modeset_unlock_all(plane->dev);
2447 * drm_mode_setplane - configure a plane's configuration
2449 * @data: ioctl data*
2450 * @file_priv: DRM file info
2452 * Set plane configuration, including placement, fb, scaling, and other factors.
2453 * Or pass a NULL fb to disable (planes may be disabled without providing a
2457 * Zero on success, negative errno on failure.
2459 int drm_mode_setplane(struct drm_device *dev, void *data,
2460 struct drm_file *file_priv)
2462 struct drm_mode_set_plane *plane_req = data;
2463 struct drm_plane *plane;
2464 struct drm_crtc *crtc = NULL;
2465 struct drm_framebuffer *fb = NULL;
2467 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2470 /* Give drivers some help against integer overflows */
2471 if (plane_req->crtc_w > INT_MAX ||
2472 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2473 plane_req->crtc_h > INT_MAX ||
2474 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2475 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2476 plane_req->crtc_w, plane_req->crtc_h,
2477 plane_req->crtc_x, plane_req->crtc_y);
2482 * First, find the plane, crtc, and fb objects. If not available,
2483 * we don't bother to call the driver.
2485 plane = drm_plane_find(dev, plane_req->plane_id);
2487 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2488 plane_req->plane_id);
2492 if (plane_req->fb_id) {
2493 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2495 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2500 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2502 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2503 plane_req->crtc_id);
2509 * setplane_internal will take care of deref'ing either the old or new
2510 * framebuffer depending on success.
2512 return setplane_internal(plane, crtc, fb,
2513 plane_req->crtc_x, plane_req->crtc_y,
2514 plane_req->crtc_w, plane_req->crtc_h,
2515 plane_req->src_x, plane_req->src_y,
2516 plane_req->src_w, plane_req->src_h);
2520 * drm_mode_set_config_internal - helper to call ->set_config
2521 * @set: modeset config to set
2523 * This is a little helper to wrap internal calls to the ->set_config driver
2524 * interface. The only thing it adds is correct refcounting dance.
2527 * Zero on success, negative errno on failure.
2529 int drm_mode_set_config_internal(struct drm_mode_set *set)
2531 struct drm_crtc *crtc = set->crtc;
2532 struct drm_framebuffer *fb;
2533 struct drm_crtc *tmp;
2537 * NOTE: ->set_config can also disable other crtcs (if we steal all
2538 * connectors from it), hence we need to refcount the fbs across all
2539 * crtcs. Atomic modeset will have saner semantics ...
2541 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2542 tmp->primary->old_fb = tmp->primary->fb;
2546 ret = crtc->funcs->set_config(set);
2548 crtc->primary->crtc = crtc;
2549 crtc->primary->fb = fb;
2552 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2553 if (tmp->primary->fb)
2554 drm_framebuffer_reference(tmp->primary->fb);
2555 if (tmp->primary->old_fb)
2556 drm_framebuffer_unreference(tmp->primary->old_fb);
2557 tmp->primary->old_fb = NULL;
2562 EXPORT_SYMBOL(drm_mode_set_config_internal);
2565 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2567 * @crtc: CRTC that framebuffer will be displayed on
2570 * @mode: mode that framebuffer will be displayed under
2571 * @fb: framebuffer to check size of
2573 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2575 const struct drm_display_mode *mode,
2576 const struct drm_framebuffer *fb)
2579 int hdisplay, vdisplay;
2581 hdisplay = mode->hdisplay;
2582 vdisplay = mode->vdisplay;
2584 if (drm_mode_is_stereo(mode)) {
2585 struct drm_display_mode adjusted = *mode;
2587 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2588 hdisplay = adjusted.crtc_hdisplay;
2589 vdisplay = adjusted.crtc_vdisplay;
2592 if (crtc->invert_dimensions)
2593 swap(hdisplay, vdisplay);
2595 if (hdisplay > fb->width ||
2596 vdisplay > fb->height ||
2597 x > fb->width - hdisplay ||
2598 y > fb->height - vdisplay) {
2599 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2600 fb->width, fb->height, hdisplay, vdisplay, x, y,
2601 crtc->invert_dimensions ? " (inverted)" : "");
2607 EXPORT_SYMBOL(drm_crtc_check_viewport);
2610 * drm_mode_setcrtc - set CRTC configuration
2611 * @dev: drm device for the ioctl
2612 * @data: data pointer for the ioctl
2613 * @file_priv: drm file for the ioctl call
2615 * Build a new CRTC configuration based on user request.
2617 * Called by the user via ioctl.
2620 * Zero on success, negative errno on failure.
2622 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2623 struct drm_file *file_priv)
2625 struct drm_mode_config *config = &dev->mode_config;
2626 struct drm_mode_crtc *crtc_req = data;
2627 struct drm_crtc *crtc;
2628 struct drm_connector **connector_set = NULL, *connector;
2629 struct drm_framebuffer *fb = NULL;
2630 struct drm_display_mode *mode = NULL;
2631 struct drm_mode_set set;
2632 uint32_t __user *set_connectors_ptr;
2636 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2639 /* For some reason crtc x/y offsets are signed internally. */
2640 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2643 drm_modeset_lock_all(dev);
2644 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2646 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2650 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2652 if (crtc_req->mode_valid) {
2653 /* If we have a mode we need a framebuffer. */
2654 /* If we pass -1, set the mode with the currently bound fb */
2655 if (crtc_req->fb_id == -1) {
2656 if (!crtc->primary->fb) {
2657 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2661 fb = crtc->primary->fb;
2662 /* Make refcounting symmetric with the lookup path. */
2663 drm_framebuffer_reference(fb);
2665 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2667 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2674 mode = drm_mode_create(dev);
2680 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2682 DRM_DEBUG_KMS("Invalid mode\n");
2686 mode->status = drm_mode_validate_basic(mode);
2687 if (mode->status != MODE_OK) {
2692 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2694 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2701 if (crtc_req->count_connectors == 0 && mode) {
2702 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2707 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2708 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2709 crtc_req->count_connectors);
2714 if (crtc_req->count_connectors > 0) {
2717 /* Avoid unbounded kernel memory allocation */
2718 if (crtc_req->count_connectors > config->num_connector) {
2723 connector_set = kmalloc_array(crtc_req->count_connectors,
2724 sizeof(struct drm_connector *),
2726 if (!connector_set) {
2731 for (i = 0; i < crtc_req->count_connectors; i++) {
2732 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2733 if (get_user(out_id, &set_connectors_ptr[i])) {
2738 connector = drm_connector_find(dev, out_id);
2740 DRM_DEBUG_KMS("Connector id %d unknown\n",
2745 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2749 connector_set[i] = connector;
2754 set.x = crtc_req->x;
2755 set.y = crtc_req->y;
2757 set.connectors = connector_set;
2758 set.num_connectors = crtc_req->count_connectors;
2760 ret = drm_mode_set_config_internal(&set);
2764 drm_framebuffer_unreference(fb);
2766 kfree(connector_set);
2767 drm_mode_destroy(dev, mode);
2768 drm_modeset_unlock_all(dev);
2773 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2774 * universal plane handler call
2775 * @crtc: crtc to update cursor for
2776 * @req: data pointer for the ioctl
2777 * @file_priv: drm file for the ioctl call
2779 * Legacy cursor ioctl's work directly with driver buffer handles. To
2780 * translate legacy ioctl calls into universal plane handler calls, we need to
2781 * wrap the native buffer handle in a drm_framebuffer.
2783 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2784 * buffer with a pitch of 4*width; the universal plane interface should be used
2785 * directly in cases where the hardware can support other buffer settings and
2786 * userspace wants to make use of these capabilities.
2789 * Zero on success, negative errno on failure.
2791 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2792 struct drm_mode_cursor2 *req,
2793 struct drm_file *file_priv)
2795 struct drm_device *dev = crtc->dev;
2796 struct drm_framebuffer *fb = NULL;
2797 struct drm_mode_fb_cmd2 fbreq = {
2798 .width = req->width,
2799 .height = req->height,
2800 .pixel_format = DRM_FORMAT_ARGB8888,
2801 .pitches = { req->width * 4 },
2802 .handles = { req->handle },
2804 int32_t crtc_x, crtc_y;
2805 uint32_t crtc_w = 0, crtc_h = 0;
2806 uint32_t src_w = 0, src_h = 0;
2809 BUG_ON(!crtc->cursor);
2810 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2813 * Obtain fb we'll be using (either new or existing) and take an extra
2814 * reference to it if fb != null. setplane will take care of dropping
2815 * the reference if the plane update fails.
2817 if (req->flags & DRM_MODE_CURSOR_BO) {
2819 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2821 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2825 drm_framebuffer_reference(fb);
2830 fb = crtc->cursor->fb;
2832 drm_framebuffer_reference(fb);
2835 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2839 crtc_x = crtc->cursor_x;
2840 crtc_y = crtc->cursor_y;
2845 crtc_h = fb->height;
2846 src_w = fb->width << 16;
2847 src_h = fb->height << 16;
2851 * setplane_internal will take care of deref'ing either the old or new
2852 * framebuffer depending on success.
2854 ret = __setplane_internal(crtc->cursor, crtc, fb,
2855 crtc_x, crtc_y, crtc_w, crtc_h,
2856 0, 0, src_w, src_h);
2858 /* Update successful; save new cursor position, if necessary */
2859 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2860 crtc->cursor_x = req->x;
2861 crtc->cursor_y = req->y;
2867 static int drm_mode_cursor_common(struct drm_device *dev,
2868 struct drm_mode_cursor2 *req,
2869 struct drm_file *file_priv)
2871 struct drm_crtc *crtc;
2874 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2877 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2880 crtc = drm_crtc_find(dev, req->crtc_id);
2882 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2887 * If this crtc has a universal cursor plane, call that plane's update
2888 * handler rather than using legacy cursor handlers.
2890 drm_modeset_lock_crtc(crtc, crtc->cursor);
2892 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2896 if (req->flags & DRM_MODE_CURSOR_BO) {
2897 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2901 /* Turns off the cursor if handle is 0 */
2902 if (crtc->funcs->cursor_set2)
2903 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2904 req->width, req->height, req->hot_x, req->hot_y);
2906 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2907 req->width, req->height);
2910 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2911 if (crtc->funcs->cursor_move) {
2912 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2919 drm_modeset_unlock_crtc(crtc);
2927 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2928 * @dev: drm device for the ioctl
2929 * @data: data pointer for the ioctl
2930 * @file_priv: drm file for the ioctl call
2932 * Set the cursor configuration based on user request.
2934 * Called by the user via ioctl.
2937 * Zero on success, negative errno on failure.
2939 int drm_mode_cursor_ioctl(struct drm_device *dev,
2940 void *data, struct drm_file *file_priv)
2942 struct drm_mode_cursor *req = data;
2943 struct drm_mode_cursor2 new_req;
2945 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2946 new_req.hot_x = new_req.hot_y = 0;
2948 return drm_mode_cursor_common(dev, &new_req, file_priv);
2952 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2953 * @dev: drm device for the ioctl
2954 * @data: data pointer for the ioctl
2955 * @file_priv: drm file for the ioctl call
2957 * Set the cursor configuration based on user request. This implements the 2nd
2958 * version of the cursor ioctl, which allows userspace to additionally specify
2959 * the hotspot of the pointer.
2961 * Called by the user via ioctl.
2964 * Zero on success, negative errno on failure.
2966 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2967 void *data, struct drm_file *file_priv)
2969 struct drm_mode_cursor2 *req = data;
2971 return drm_mode_cursor_common(dev, req, file_priv);
2975 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2976 * @bpp: bits per pixels
2977 * @depth: bit depth per pixel
2979 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2980 * Useful in fbdev emulation code, since that deals in those values.
2982 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2988 fmt = DRM_FORMAT_C8;
2992 fmt = DRM_FORMAT_XRGB1555;
2994 fmt = DRM_FORMAT_RGB565;
2997 fmt = DRM_FORMAT_RGB888;
3001 fmt = DRM_FORMAT_XRGB8888;
3002 else if (depth == 30)
3003 fmt = DRM_FORMAT_XRGB2101010;
3005 fmt = DRM_FORMAT_ARGB8888;
3008 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3009 fmt = DRM_FORMAT_XRGB8888;
3015 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3018 * drm_mode_addfb - add an FB to the graphics configuration
3019 * @dev: drm device for the ioctl
3020 * @data: data pointer for the ioctl
3021 * @file_priv: drm file for the ioctl call
3023 * Add a new FB to the specified CRTC, given a user request. This is the
3024 * original addfb ioctl which only supported RGB formats.
3026 * Called by the user via ioctl.
3029 * Zero on success, negative errno on failure.
3031 int drm_mode_addfb(struct drm_device *dev,
3032 void *data, struct drm_file *file_priv)
3034 struct drm_mode_fb_cmd *or = data;
3035 struct drm_mode_fb_cmd2 r = {};
3038 /* convert to new format and call new ioctl */
3039 r.fb_id = or->fb_id;
3040 r.width = or->width;
3041 r.height = or->height;
3042 r.pitches[0] = or->pitch;
3043 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3044 r.handles[0] = or->handle;
3046 ret = drm_mode_addfb2(dev, &r, file_priv);
3050 or->fb_id = r.fb_id;
3055 static int format_check(const struct drm_mode_fb_cmd2 *r)
3057 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3061 case DRM_FORMAT_RGB332:
3062 case DRM_FORMAT_BGR233:
3063 case DRM_FORMAT_XRGB4444:
3064 case DRM_FORMAT_XBGR4444:
3065 case DRM_FORMAT_RGBX4444:
3066 case DRM_FORMAT_BGRX4444:
3067 case DRM_FORMAT_ARGB4444:
3068 case DRM_FORMAT_ABGR4444:
3069 case DRM_FORMAT_RGBA4444:
3070 case DRM_FORMAT_BGRA4444:
3071 case DRM_FORMAT_XRGB1555:
3072 case DRM_FORMAT_XBGR1555:
3073 case DRM_FORMAT_RGBX5551:
3074 case DRM_FORMAT_BGRX5551:
3075 case DRM_FORMAT_ARGB1555:
3076 case DRM_FORMAT_ABGR1555:
3077 case DRM_FORMAT_RGBA5551:
3078 case DRM_FORMAT_BGRA5551:
3079 case DRM_FORMAT_RGB565:
3080 case DRM_FORMAT_BGR565:
3081 case DRM_FORMAT_RGB888:
3082 case DRM_FORMAT_BGR888:
3083 case DRM_FORMAT_XRGB8888:
3084 case DRM_FORMAT_XBGR8888:
3085 case DRM_FORMAT_RGBX8888:
3086 case DRM_FORMAT_BGRX8888:
3087 case DRM_FORMAT_ARGB8888:
3088 case DRM_FORMAT_ABGR8888:
3089 case DRM_FORMAT_RGBA8888:
3090 case DRM_FORMAT_BGRA8888:
3091 case DRM_FORMAT_XRGB2101010:
3092 case DRM_FORMAT_XBGR2101010:
3093 case DRM_FORMAT_RGBX1010102:
3094 case DRM_FORMAT_BGRX1010102:
3095 case DRM_FORMAT_ARGB2101010:
3096 case DRM_FORMAT_ABGR2101010:
3097 case DRM_FORMAT_RGBA1010102:
3098 case DRM_FORMAT_BGRA1010102:
3099 case DRM_FORMAT_YUYV:
3100 case DRM_FORMAT_YVYU:
3101 case DRM_FORMAT_UYVY:
3102 case DRM_FORMAT_VYUY:
3103 case DRM_FORMAT_AYUV:
3104 case DRM_FORMAT_NV12:
3105 case DRM_FORMAT_NV21:
3106 case DRM_FORMAT_NV16:
3107 case DRM_FORMAT_NV61:
3108 case DRM_FORMAT_NV24:
3109 case DRM_FORMAT_NV42:
3110 case DRM_FORMAT_YUV410:
3111 case DRM_FORMAT_YVU410:
3112 case DRM_FORMAT_YUV411:
3113 case DRM_FORMAT_YVU411:
3114 case DRM_FORMAT_YUV420:
3115 case DRM_FORMAT_YVU420:
3116 case DRM_FORMAT_YUV422:
3117 case DRM_FORMAT_YVU422:
3118 case DRM_FORMAT_YUV444:
3119 case DRM_FORMAT_YVU444:
3122 DRM_DEBUG_KMS("invalid pixel format %s\n",
3123 drm_get_format_name(r->pixel_format));
3128 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3130 int ret, hsub, vsub, num_planes, i;
3132 ret = format_check(r);
3134 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3135 drm_get_format_name(r->pixel_format));
3139 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3140 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3141 num_planes = drm_format_num_planes(r->pixel_format);
3143 if (r->width == 0 || r->width % hsub) {
3144 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3148 if (r->height == 0 || r->height % vsub) {
3149 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3153 for (i = 0; i < num_planes; i++) {
3154 unsigned int width = r->width / (i != 0 ? hsub : 1);
3155 unsigned int height = r->height / (i != 0 ? vsub : 1);
3156 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3158 if (!r->handles[i]) {
3159 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3163 if ((uint64_t) width * cpp > UINT_MAX)
3166 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3169 if (r->pitches[i] < width * cpp) {
3170 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3178 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3179 struct drm_mode_fb_cmd2 *r,
3180 struct drm_file *file_priv)
3182 struct drm_mode_config *config = &dev->mode_config;
3183 struct drm_framebuffer *fb;
3186 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3187 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3188 return ERR_PTR(-EINVAL);
3191 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3192 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3193 r->width, config->min_width, config->max_width);
3194 return ERR_PTR(-EINVAL);
3196 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3197 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3198 r->height, config->min_height, config->max_height);
3199 return ERR_PTR(-EINVAL);
3202 ret = framebuffer_check(r);
3204 return ERR_PTR(ret);
3206 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3208 DRM_DEBUG_KMS("could not create framebuffer\n");
3212 mutex_lock(&file_priv->fbs_lock);
3213 r->fb_id = fb->base.id;
3214 list_add(&fb->filp_head, &file_priv->fbs);
3215 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3216 mutex_unlock(&file_priv->fbs_lock);
3222 * drm_mode_addfb2 - add an FB to the graphics configuration
3223 * @dev: drm device for the ioctl
3224 * @data: data pointer for the ioctl
3225 * @file_priv: drm file for the ioctl call
3227 * Add a new FB to the specified CRTC, given a user request with format. This is
3228 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3229 * and uses fourcc codes as pixel format specifiers.
3231 * Called by the user via ioctl.
3234 * Zero on success, negative errno on failure.
3236 int drm_mode_addfb2(struct drm_device *dev,
3237 void *data, struct drm_file *file_priv)
3239 struct drm_framebuffer *fb;
3241 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3244 fb = add_framebuffer_internal(dev, data, file_priv);
3252 * drm_mode_rmfb - remove an FB from the configuration
3253 * @dev: drm device for the ioctl
3254 * @data: data pointer for the ioctl
3255 * @file_priv: drm file for the ioctl call
3257 * Remove the FB specified by the user.
3259 * Called by the user via ioctl.
3262 * Zero on success, negative errno on failure.
3264 int drm_mode_rmfb(struct drm_device *dev,
3265 void *data, struct drm_file *file_priv)
3267 struct drm_framebuffer *fb = NULL;
3268 struct drm_framebuffer *fbl = NULL;
3269 uint32_t *id = data;
3272 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3275 mutex_lock(&file_priv->fbs_lock);
3276 mutex_lock(&dev->mode_config.fb_lock);
3277 fb = __drm_framebuffer_lookup(dev, *id);
3281 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3287 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3288 __drm_framebuffer_unregister(dev, fb);
3290 list_del_init(&fb->filp_head);
3291 mutex_unlock(&dev->mode_config.fb_lock);
3292 mutex_unlock(&file_priv->fbs_lock);
3294 drm_framebuffer_remove(fb);
3299 mutex_unlock(&dev->mode_config.fb_lock);
3300 mutex_unlock(&file_priv->fbs_lock);
3306 * drm_mode_getfb - get FB info
3307 * @dev: drm device for the ioctl
3308 * @data: data pointer for the ioctl
3309 * @file_priv: drm file for the ioctl call
3311 * Lookup the FB given its ID and return info about it.
3313 * Called by the user via ioctl.
3316 * Zero on success, negative errno on failure.
3318 int drm_mode_getfb(struct drm_device *dev,
3319 void *data, struct drm_file *file_priv)
3321 struct drm_mode_fb_cmd *r = data;
3322 struct drm_framebuffer *fb;
3325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3328 fb = drm_framebuffer_lookup(dev, r->fb_id);
3332 r->height = fb->height;
3333 r->width = fb->width;
3334 r->depth = fb->depth;
3335 r->bpp = fb->bits_per_pixel;
3336 r->pitch = fb->pitches[0];
3337 if (fb->funcs->create_handle) {
3338 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3339 drm_is_control_client(file_priv)) {
3340 ret = fb->funcs->create_handle(fb, file_priv,
3343 /* GET_FB() is an unprivileged ioctl so we must not
3344 * return a buffer-handle to non-master processes! For
3345 * backwards-compatibility reasons, we cannot make
3346 * GET_FB() privileged, so just return an invalid handle
3347 * for non-masters. */
3355 drm_framebuffer_unreference(fb);
3361 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3362 * @dev: drm device for the ioctl
3363 * @data: data pointer for the ioctl
3364 * @file_priv: drm file for the ioctl call
3366 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3367 * rectangle list. Generic userspace which does frontbuffer rendering must call
3368 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3369 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3371 * Modesetting drivers which always update the frontbuffer do not need to
3372 * implement the corresponding ->dirty framebuffer callback.
3374 * Called by the user via ioctl.
3377 * Zero on success, negative errno on failure.
3379 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3380 void *data, struct drm_file *file_priv)
3382 struct drm_clip_rect __user *clips_ptr;
3383 struct drm_clip_rect *clips = NULL;
3384 struct drm_mode_fb_dirty_cmd *r = data;
3385 struct drm_framebuffer *fb;
3390 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3393 fb = drm_framebuffer_lookup(dev, r->fb_id);
3397 num_clips = r->num_clips;
3398 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3400 if (!num_clips != !clips_ptr) {
3405 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3407 /* If userspace annotates copy, clips must come in pairs */
3408 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3413 if (num_clips && clips_ptr) {
3414 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3418 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3424 ret = copy_from_user(clips, clips_ptr,
3425 num_clips * sizeof(*clips));
3432 if (fb->funcs->dirty) {
3433 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3442 drm_framebuffer_unreference(fb);
3449 * drm_fb_release - remove and free the FBs on this file
3450 * @priv: drm file for the ioctl
3452 * Destroy all the FBs associated with @filp.
3454 * Called by the user via ioctl.
3457 * Zero on success, negative errno on failure.
3459 void drm_fb_release(struct drm_file *priv)
3461 struct drm_device *dev = priv->minor->dev;
3462 struct drm_framebuffer *fb, *tfb;
3465 * When the file gets released that means no one else can access the fb
3466 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3467 * avoid upsetting lockdep since the universal cursor code adds a
3468 * framebuffer while holding mutex locks.
3470 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3471 * locks is impossible here since no one else but this function can get
3474 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3476 mutex_lock(&dev->mode_config.fb_lock);
3477 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3478 __drm_framebuffer_unregister(dev, fb);
3479 mutex_unlock(&dev->mode_config.fb_lock);
3481 list_del_init(&fb->filp_head);
3483 /* This will also drop the fpriv->fbs reference. */
3484 drm_framebuffer_remove(fb);
3489 * drm_property_create - create a new property type
3491 * @flags: flags specifying the property type
3492 * @name: name of the property
3493 * @num_values: number of pre-defined values
3495 * This creates a new generic drm property which can then be attached to a drm
3496 * object with drm_object_attach_property. The returned property object must be
3497 * freed with drm_property_destroy.
3499 * Note that the DRM core keeps a per-device list of properties and that, if
3500 * drm_mode_config_cleanup() is called, it will destroy all properties created
3504 * A pointer to the newly created property on success, NULL on failure.
3506 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3507 const char *name, int num_values)
3509 struct drm_property *property = NULL;
3512 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3516 property->dev = dev;
3519 property->values = kcalloc(num_values, sizeof(uint64_t),
3521 if (!property->values)
3525 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3529 property->flags = flags;
3530 property->num_values = num_values;
3531 INIT_LIST_HEAD(&property->enum_list);
3534 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3535 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3538 list_add_tail(&property->head, &dev->mode_config.property_list);
3540 WARN_ON(!drm_property_type_valid(property));
3544 kfree(property->values);
3548 EXPORT_SYMBOL(drm_property_create);
3551 * drm_property_create_enum - create a new enumeration property type
3553 * @flags: flags specifying the property type
3554 * @name: name of the property
3555 * @props: enumeration lists with property values
3556 * @num_values: number of pre-defined values
3558 * This creates a new generic drm property which can then be attached to a drm
3559 * object with drm_object_attach_property. The returned property object must be
3560 * freed with drm_property_destroy.
3562 * Userspace is only allowed to set one of the predefined values for enumeration
3566 * A pointer to the newly created property on success, NULL on failure.
3568 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3570 const struct drm_prop_enum_list *props,
3573 struct drm_property *property;
3576 flags |= DRM_MODE_PROP_ENUM;
3578 property = drm_property_create(dev, flags, name, num_values);
3582 for (i = 0; i < num_values; i++) {
3583 ret = drm_property_add_enum(property, i,
3587 drm_property_destroy(dev, property);
3594 EXPORT_SYMBOL(drm_property_create_enum);
3597 * drm_property_create_bitmask - create a new bitmask property type
3599 * @flags: flags specifying the property type
3600 * @name: name of the property
3601 * @props: enumeration lists with property bitflags
3602 * @num_props: size of the @props array
3603 * @supported_bits: bitmask of all supported enumeration values
3605 * This creates a new bitmask drm property which can then be attached to a drm
3606 * object with drm_object_attach_property. The returned property object must be
3607 * freed with drm_property_destroy.
3609 * Compared to plain enumeration properties userspace is allowed to set any
3610 * or'ed together combination of the predefined property bitflag values
3613 * A pointer to the newly created property on success, NULL on failure.
3615 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3616 int flags, const char *name,
3617 const struct drm_prop_enum_list *props,
3619 uint64_t supported_bits)
3621 struct drm_property *property;
3622 int i, ret, index = 0;
3623 int num_values = hweight64(supported_bits);
3625 flags |= DRM_MODE_PROP_BITMASK;
3627 property = drm_property_create(dev, flags, name, num_values);
3630 for (i = 0; i < num_props; i++) {
3631 if (!(supported_bits & (1ULL << props[i].type)))
3634 if (WARN_ON(index >= num_values)) {
3635 drm_property_destroy(dev, property);
3639 ret = drm_property_add_enum(property, index++,
3643 drm_property_destroy(dev, property);
3650 EXPORT_SYMBOL(drm_property_create_bitmask);
3652 static struct drm_property *property_create_range(struct drm_device *dev,
3653 int flags, const char *name,
3654 uint64_t min, uint64_t max)
3656 struct drm_property *property;
3658 property = drm_property_create(dev, flags, name, 2);
3662 property->values[0] = min;
3663 property->values[1] = max;
3669 * drm_property_create_range - create a new ranged property type
3671 * @flags: flags specifying the property type
3672 * @name: name of the property
3673 * @min: minimum value of the property
3674 * @max: maximum value of the property
3676 * This creates a new generic drm property which can then be attached to a drm
3677 * object with drm_object_attach_property. The returned property object must be
3678 * freed with drm_property_destroy.
3680 * Userspace is allowed to set any integer value in the (min, max) range
3684 * A pointer to the newly created property on success, NULL on failure.
3686 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3688 uint64_t min, uint64_t max)
3690 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3693 EXPORT_SYMBOL(drm_property_create_range);
3695 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3696 int flags, const char *name,
3697 int64_t min, int64_t max)
3699 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3700 name, I642U64(min), I642U64(max));
3702 EXPORT_SYMBOL(drm_property_create_signed_range);
3704 struct drm_property *drm_property_create_object(struct drm_device *dev,
3705 int flags, const char *name, uint32_t type)
3707 struct drm_property *property;
3709 flags |= DRM_MODE_PROP_OBJECT;
3711 property = drm_property_create(dev, flags, name, 1);
3715 property->values[0] = type;
3719 EXPORT_SYMBOL(drm_property_create_object);
3722 * drm_property_add_enum - add a possible value to an enumeration property
3723 * @property: enumeration property to change
3724 * @index: index of the new enumeration
3725 * @value: value of the new enumeration
3726 * @name: symbolic name of the new enumeration
3728 * This functions adds enumerations to a property.
3730 * It's use is deprecated, drivers should use one of the more specific helpers
3731 * to directly create the property with all enumerations already attached.
3734 * Zero on success, error code on failure.
3736 int drm_property_add_enum(struct drm_property *property, int index,
3737 uint64_t value, const char *name)
3739 struct drm_property_enum *prop_enum;
3741 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3742 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3746 * Bitmask enum properties have the additional constraint of values
3749 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3753 if (!list_empty(&property->enum_list)) {
3754 list_for_each_entry(prop_enum, &property->enum_list, head) {
3755 if (prop_enum->value == value) {
3756 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3757 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3763 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3767 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3768 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3769 prop_enum->value = value;
3771 property->values[index] = value;
3772 list_add_tail(&prop_enum->head, &property->enum_list);
3775 EXPORT_SYMBOL(drm_property_add_enum);
3778 * drm_property_destroy - destroy a drm property
3780 * @property: property to destry
3782 * This function frees a property including any attached resources like
3783 * enumeration values.
3785 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3787 struct drm_property_enum *prop_enum, *pt;
3789 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3790 list_del(&prop_enum->head);
3794 if (property->num_values)
3795 kfree(property->values);
3796 drm_mode_object_put(dev, &property->base);
3797 list_del(&property->head);
3800 EXPORT_SYMBOL(drm_property_destroy);
3803 * drm_object_attach_property - attach a property to a modeset object
3804 * @obj: drm modeset object
3805 * @property: property to attach
3806 * @init_val: initial value of the property
3808 * This attaches the given property to the modeset object with the given initial
3809 * value. Currently this function cannot fail since the properties are stored in
3810 * a statically sized array.
3812 void drm_object_attach_property(struct drm_mode_object *obj,
3813 struct drm_property *property,
3816 int count = obj->properties->count;
3818 if (count == DRM_OBJECT_MAX_PROPERTY) {
3819 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3820 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3821 "you see this message on the same object type.\n",
3826 obj->properties->ids[count] = property->base.id;
3827 obj->properties->values[count] = init_val;
3828 obj->properties->count++;
3830 EXPORT_SYMBOL(drm_object_attach_property);
3833 * drm_object_property_set_value - set the value of a property
3834 * @obj: drm mode object to set property value for
3835 * @property: property to set
3836 * @val: value the property should be set to
3838 * This functions sets a given property on a given object. This function only
3839 * changes the software state of the property, it does not call into the
3840 * driver's ->set_property callback.
3843 * Zero on success, error code on failure.
3845 int drm_object_property_set_value(struct drm_mode_object *obj,
3846 struct drm_property *property, uint64_t val)
3850 for (i = 0; i < obj->properties->count; i++) {
3851 if (obj->properties->ids[i] == property->base.id) {
3852 obj->properties->values[i] = val;
3859 EXPORT_SYMBOL(drm_object_property_set_value);
3862 * drm_object_property_get_value - retrieve the value of a property
3863 * @obj: drm mode object to get property value from
3864 * @property: property to retrieve
3865 * @val: storage for the property value
3867 * This function retrieves the softare state of the given property for the given
3868 * property. Since there is no driver callback to retrieve the current property
3869 * value this might be out of sync with the hardware, depending upon the driver
3873 * Zero on success, error code on failure.
3875 int drm_object_property_get_value(struct drm_mode_object *obj,
3876 struct drm_property *property, uint64_t *val)
3880 for (i = 0; i < obj->properties->count; i++) {
3881 if (obj->properties->ids[i] == property->base.id) {
3882 *val = obj->properties->values[i];
3889 EXPORT_SYMBOL(drm_object_property_get_value);
3892 * drm_mode_getproperty_ioctl - get the property metadata
3895 * @file_priv: DRM file info
3897 * This function retrieves the metadata for a given property, like the different
3898 * possible values for an enum property or the limits for a range property.
3900 * Blob properties are special
3902 * Called by the user via ioctl.
3905 * Zero on success, negative errno on failure.
3907 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3908 void *data, struct drm_file *file_priv)
3910 struct drm_mode_get_property *out_resp = data;
3911 struct drm_property *property;
3913 int value_count = 0;
3916 struct drm_property_enum *prop_enum;
3917 struct drm_mode_property_enum __user *enum_ptr;
3918 uint64_t __user *values_ptr;
3920 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3923 drm_modeset_lock_all(dev);
3924 property = drm_property_find(dev, out_resp->prop_id);
3930 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3931 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3932 list_for_each_entry(prop_enum, &property->enum_list, head)
3936 value_count = property->num_values;
3938 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3939 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3940 out_resp->flags = property->flags;
3942 if ((out_resp->count_values >= value_count) && value_count) {
3943 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3944 for (i = 0; i < value_count; i++) {
3945 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3951 out_resp->count_values = value_count;
3953 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3954 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3955 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3957 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3958 list_for_each_entry(prop_enum, &property->enum_list, head) {
3960 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3965 if (copy_to_user(&enum_ptr[copied].name,
3966 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3973 out_resp->count_enum_blobs = enum_count;
3977 * NOTE: The idea seems to have been to use this to read all the blob
3978 * property values. But nothing ever added them to the corresponding
3979 * list, userspace always used the special-purpose get_blob ioctl to
3980 * read the value for a blob property. It also doesn't make a lot of
3981 * sense to return values here when everything else is just metadata for
3982 * the property itself.
3984 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3985 out_resp->count_enum_blobs = 0;
3987 drm_modeset_unlock_all(dev);
3991 static struct drm_property_blob *
3992 drm_property_create_blob(struct drm_device *dev, size_t length,
3995 struct drm_property_blob *blob;
3998 if (!length || !data)
4001 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4005 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4011 blob->length = length;
4013 memcpy(blob->data, data, length);
4015 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4019 static void drm_property_destroy_blob(struct drm_device *dev,
4020 struct drm_property_blob *blob)
4022 drm_mode_object_put(dev, &blob->base);
4023 list_del(&blob->head);
4028 * drm_mode_getblob_ioctl - get the contents of a blob property value
4031 * @file_priv: DRM file info
4033 * This function retrieves the contents of a blob property. The value stored in
4034 * an object's blob property is just a normal modeset object id.
4036 * Called by the user via ioctl.
4039 * Zero on success, negative errno on failure.
4041 int drm_mode_getblob_ioctl(struct drm_device *dev,
4042 void *data, struct drm_file *file_priv)
4044 struct drm_mode_get_blob *out_resp = data;
4045 struct drm_property_blob *blob;
4047 void __user *blob_ptr;
4049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4052 drm_modeset_lock_all(dev);
4053 blob = drm_property_blob_find(dev, out_resp->blob_id);
4059 if (out_resp->length == blob->length) {
4060 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4061 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4066 out_resp->length = blob->length;
4069 drm_modeset_unlock_all(dev);
4074 * drm_mode_connector_set_path_property - set tile property on connector
4075 * @connector: connector to set property on.
4076 * @path: path to use for property.
4078 * This creates a property to expose to userspace to specify a
4079 * connector path. This is mainly used for DisplayPort MST where
4080 * connectors have a topology and we want to allow userspace to give
4081 * them more meaningful names.
4084 * Zero on success, negative errno on failure.
4086 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4089 struct drm_device *dev = connector->dev;
4090 size_t size = strlen(path) + 1;
4093 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4095 if (!connector->path_blob_ptr)
4098 ret = drm_object_property_set_value(&connector->base,
4099 dev->mode_config.path_property,
4100 connector->path_blob_ptr->base.id);
4103 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4106 * drm_mode_connector_set_tile_property - set tile property on connector
4107 * @connector: connector to set property on.
4109 * This looks up the tile information for a connector, and creates a
4110 * property for userspace to parse if it exists. The property is of
4111 * the form of 8 integers using ':' as a separator.
4114 * Zero on success, errno on failure.
4116 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4118 struct drm_device *dev = connector->dev;
4122 if (connector->tile_blob_ptr)
4123 drm_property_destroy_blob(dev, connector->tile_blob_ptr);
4125 if (!connector->has_tile) {
4126 connector->tile_blob_ptr = NULL;
4127 ret = drm_object_property_set_value(&connector->base,
4128 dev->mode_config.tile_property, 0);
4132 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4133 connector->tile_group->id, connector->tile_is_single_monitor,
4134 connector->num_h_tile, connector->num_v_tile,
4135 connector->tile_h_loc, connector->tile_v_loc,
4136 connector->tile_h_size, connector->tile_v_size);
4137 size = strlen(tile) + 1;
4139 connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
4141 if (!connector->tile_blob_ptr)
4144 ret = drm_object_property_set_value(&connector->base,
4145 dev->mode_config.tile_property,
4146 connector->tile_blob_ptr->base.id);
4149 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4152 * drm_mode_connector_update_edid_property - update the edid property of a connector
4153 * @connector: drm connector
4154 * @edid: new value of the edid property
4156 * This function creates a new blob modeset object and assigns its id to the
4157 * connector's edid property.
4160 * Zero on success, negative errno on failure.
4162 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4163 const struct edid *edid)
4165 struct drm_device *dev = connector->dev;
4169 /* ignore requests to set edid when overridden */
4170 if (connector->override_edid)
4173 if (connector->edid_blob_ptr)
4174 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4176 /* Delete edid, when there is none. */
4178 connector->edid_blob_ptr = NULL;
4179 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
4183 size = EDID_LENGTH * (1 + edid->extensions);
4184 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4186 if (!connector->edid_blob_ptr)
4189 ret = drm_object_property_set_value(&connector->base,
4190 dev->mode_config.edid_property,
4191 connector->edid_blob_ptr->base.id);
4195 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4197 static bool drm_property_change_is_valid(struct drm_property *property,
4202 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4205 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4206 if (value < property->values[0] || value > property->values[1])
4209 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4210 int64_t svalue = U642I64(value);
4212 if (svalue < U642I64(property->values[0]) ||
4213 svalue > U642I64(property->values[1]))
4216 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4217 uint64_t valid_mask = 0;
4219 for (i = 0; i < property->num_values; i++)
4220 valid_mask |= (1ULL << property->values[i]);
4221 return !(value & ~valid_mask);
4222 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4223 /* Only the driver knows */
4225 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4226 struct drm_mode_object *obj;
4228 /* a zero value for an object property translates to null: */
4232 * NOTE: use _object_find() directly to bypass restriction on
4233 * looking up refcnt'd objects (ie. fb's). For a refcnt'd
4234 * object this could race against object finalization, so it
4235 * simply tells us that the object *was* valid. Which is good
4238 obj = _object_find(property->dev, value, property->values[0]);
4242 for (i = 0; i < property->num_values; i++)
4243 if (property->values[i] == value)
4249 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4252 * @file_priv: DRM file info
4254 * This function sets the current value for a connectors's property. It also
4255 * calls into a driver's ->set_property callback to update the hardware state
4257 * Called by the user via ioctl.
4260 * Zero on success, negative errno on failure.
4262 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4263 void *data, struct drm_file *file_priv)
4265 struct drm_mode_connector_set_property *conn_set_prop = data;
4266 struct drm_mode_obj_set_property obj_set_prop = {
4267 .value = conn_set_prop->value,
4268 .prop_id = conn_set_prop->prop_id,
4269 .obj_id = conn_set_prop->connector_id,
4270 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4273 /* It does all the locking and checking we need */
4274 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4277 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4278 struct drm_property *property,
4282 struct drm_connector *connector = obj_to_connector(obj);
4284 /* Do DPMS ourselves */
4285 if (property == connector->dev->mode_config.dpms_property) {
4286 if (connector->funcs->dpms)
4287 (*connector->funcs->dpms)(connector, (int)value);
4289 } else if (connector->funcs->set_property)
4290 ret = connector->funcs->set_property(connector, property, value);
4292 /* store the property value if successful */
4294 drm_object_property_set_value(&connector->base, property, value);
4298 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4299 struct drm_property *property,
4303 struct drm_crtc *crtc = obj_to_crtc(obj);
4305 if (crtc->funcs->set_property)
4306 ret = crtc->funcs->set_property(crtc, property, value);
4308 drm_object_property_set_value(obj, property, value);
4314 * drm_mode_plane_set_obj_prop - set the value of a property
4315 * @plane: drm plane object to set property value for
4316 * @property: property to set
4317 * @value: value the property should be set to
4319 * This functions sets a given property on a given plane object. This function
4320 * calls the driver's ->set_property callback and changes the software state of
4321 * the property if the callback succeeds.
4324 * Zero on success, error code on failure.
4326 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4327 struct drm_property *property,
4331 struct drm_mode_object *obj = &plane->base;
4333 if (plane->funcs->set_property)
4334 ret = plane->funcs->set_property(plane, property, value);
4336 drm_object_property_set_value(obj, property, value);
4340 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4343 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4346 * @file_priv: DRM file info
4348 * This function retrieves the current value for an object's property. Compared
4349 * to the connector specific ioctl this one is extended to also work on crtc and
4352 * Called by the user via ioctl.
4355 * Zero on success, negative errno on failure.
4357 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4358 struct drm_file *file_priv)
4360 struct drm_mode_obj_get_properties *arg = data;
4361 struct drm_mode_object *obj;
4365 int props_count = 0;
4366 uint32_t __user *props_ptr;
4367 uint64_t __user *prop_values_ptr;
4369 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4372 drm_modeset_lock_all(dev);
4374 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4379 if (!obj->properties) {
4384 props_count = obj->properties->count;
4386 /* This ioctl is called twice, once to determine how much space is
4387 * needed, and the 2nd time to fill it. */
4388 if ((arg->count_props >= props_count) && props_count) {
4390 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
4391 prop_values_ptr = (uint64_t __user *)(unsigned long)
4392 (arg->prop_values_ptr);
4393 for (i = 0; i < props_count; i++) {
4394 if (put_user(obj->properties->ids[i],
4395 props_ptr + copied)) {
4399 if (put_user(obj->properties->values[i],
4400 prop_values_ptr + copied)) {
4407 arg->count_props = props_count;
4409 drm_modeset_unlock_all(dev);
4414 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4417 * @file_priv: DRM file info
4419 * This function sets the current value for an object's property. It also calls
4420 * into a driver's ->set_property callback to update the hardware state.
4421 * Compared to the connector specific ioctl this one is extended to also work on
4422 * crtc and plane objects.
4424 * Called by the user via ioctl.
4427 * Zero on success, negative errno on failure.
4429 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4430 struct drm_file *file_priv)
4432 struct drm_mode_obj_set_property *arg = data;
4433 struct drm_mode_object *arg_obj;
4434 struct drm_mode_object *prop_obj;
4435 struct drm_property *property;
4439 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4442 drm_modeset_lock_all(dev);
4444 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4449 if (!arg_obj->properties)
4452 for (i = 0; i < arg_obj->properties->count; i++)
4453 if (arg_obj->properties->ids[i] == arg->prop_id)
4456 if (i == arg_obj->properties->count)
4459 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4460 DRM_MODE_OBJECT_PROPERTY);
4465 property = obj_to_property(prop_obj);
4467 if (!drm_property_change_is_valid(property, arg->value))
4470 switch (arg_obj->type) {
4471 case DRM_MODE_OBJECT_CONNECTOR:
4472 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4475 case DRM_MODE_OBJECT_CRTC:
4476 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4478 case DRM_MODE_OBJECT_PLANE:
4479 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4480 property, arg->value);
4485 drm_modeset_unlock_all(dev);
4490 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4491 * @connector: connector to attach
4492 * @encoder: encoder to attach @connector to
4494 * This function links up a connector to an encoder. Note that the routing
4495 * restrictions between encoders and crtcs are exposed to userspace through the
4496 * possible_clones and possible_crtcs bitmasks.
4499 * Zero on success, negative errno on failure.
4501 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4502 struct drm_encoder *encoder)
4506 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4507 if (connector->encoder_ids[i] == 0) {
4508 connector->encoder_ids[i] = encoder->base.id;
4514 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4517 * drm_mode_crtc_set_gamma_size - set the gamma table size
4518 * @crtc: CRTC to set the gamma table size for
4519 * @gamma_size: size of the gamma table
4521 * Drivers which support gamma tables should set this to the supported gamma
4522 * table size when initializing the CRTC. Currently the drm core only supports a
4523 * fixed gamma table size.
4526 * Zero on success, negative errno on failure.
4528 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4531 crtc->gamma_size = gamma_size;
4533 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4535 if (!crtc->gamma_store) {
4536 crtc->gamma_size = 0;
4542 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4545 * drm_mode_gamma_set_ioctl - set the gamma table
4548 * @file_priv: DRM file info
4550 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4551 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4553 * Called by the user via ioctl.
4556 * Zero on success, negative errno on failure.
4558 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4559 void *data, struct drm_file *file_priv)
4561 struct drm_mode_crtc_lut *crtc_lut = data;
4562 struct drm_crtc *crtc;
4563 void *r_base, *g_base, *b_base;
4567 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4570 drm_modeset_lock_all(dev);
4571 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4577 if (crtc->funcs->gamma_set == NULL) {
4582 /* memcpy into gamma store */
4583 if (crtc_lut->gamma_size != crtc->gamma_size) {
4588 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4589 r_base = crtc->gamma_store;
4590 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4595 g_base = r_base + size;
4596 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4601 b_base = g_base + size;
4602 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4607 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4610 drm_modeset_unlock_all(dev);
4616 * drm_mode_gamma_get_ioctl - get the gamma table
4619 * @file_priv: DRM file info
4621 * Copy the current gamma table into the storage provided. This also provides
4622 * the gamma table size the driver expects, which can be used to size the
4623 * allocated storage.
4625 * Called by the user via ioctl.
4628 * Zero on success, negative errno on failure.
4630 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4631 void *data, struct drm_file *file_priv)
4633 struct drm_mode_crtc_lut *crtc_lut = data;
4634 struct drm_crtc *crtc;
4635 void *r_base, *g_base, *b_base;
4639 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4642 drm_modeset_lock_all(dev);
4643 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4649 /* memcpy into gamma store */
4650 if (crtc_lut->gamma_size != crtc->gamma_size) {
4655 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4656 r_base = crtc->gamma_store;
4657 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4662 g_base = r_base + size;
4663 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4668 b_base = g_base + size;
4669 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4674 drm_modeset_unlock_all(dev);
4679 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4682 * @file_priv: DRM file info
4684 * This schedules an asynchronous update on a given CRTC, called page flip.
4685 * Optionally a drm event is generated to signal the completion of the event.
4686 * Generic drivers cannot assume that a pageflip with changed framebuffer
4687 * properties (including driver specific metadata like tiling layout) will work,
4688 * but some drivers support e.g. pixel format changes through the pageflip
4691 * Called by the user via ioctl.
4694 * Zero on success, negative errno on failure.
4696 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4697 void *data, struct drm_file *file_priv)
4699 struct drm_mode_crtc_page_flip *page_flip = data;
4700 struct drm_crtc *crtc;
4701 struct drm_framebuffer *fb = NULL;
4702 struct drm_pending_vblank_event *e = NULL;
4703 unsigned long flags;
4706 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4707 page_flip->reserved != 0)
4710 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4713 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4717 drm_modeset_lock_crtc(crtc, crtc->primary);
4718 if (crtc->primary->fb == NULL) {
4719 /* The framebuffer is currently unbound, presumably
4720 * due to a hotplug event, that userspace has not
4727 if (crtc->funcs->page_flip == NULL)
4730 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4736 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4740 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4741 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4746 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4748 spin_lock_irqsave(&dev->event_lock, flags);
4749 if (file_priv->event_space < sizeof(e->event)) {
4750 spin_unlock_irqrestore(&dev->event_lock, flags);
4753 file_priv->event_space -= sizeof(e->event);
4754 spin_unlock_irqrestore(&dev->event_lock, flags);
4756 e = kzalloc(sizeof(*e), GFP_KERNEL);
4758 spin_lock_irqsave(&dev->event_lock, flags);
4759 file_priv->event_space += sizeof(e->event);
4760 spin_unlock_irqrestore(&dev->event_lock, flags);
4764 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4765 e->event.base.length = sizeof(e->event);
4766 e->event.user_data = page_flip->user_data;
4767 e->base.event = &e->event.base;
4768 e->base.file_priv = file_priv;
4770 (void (*) (struct drm_pending_event *)) kfree;
4773 crtc->primary->old_fb = crtc->primary->fb;
4774 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4776 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4777 spin_lock_irqsave(&dev->event_lock, flags);
4778 file_priv->event_space += sizeof(e->event);
4779 spin_unlock_irqrestore(&dev->event_lock, flags);
4782 /* Keep the old fb, don't unref it. */
4783 crtc->primary->old_fb = NULL;
4786 * Warn if the driver hasn't properly updated the crtc->fb
4787 * field to reflect that the new framebuffer is now used.
4788 * Failing to do so will screw with the reference counting
4791 WARN_ON(crtc->primary->fb != fb);
4792 /* Unref only the old framebuffer. */
4798 drm_framebuffer_unreference(fb);
4799 if (crtc->primary->old_fb)
4800 drm_framebuffer_unreference(crtc->primary->old_fb);
4801 crtc->primary->old_fb = NULL;
4802 drm_modeset_unlock_crtc(crtc);
4808 * drm_mode_config_reset - call ->reset callbacks
4811 * This functions calls all the crtc's, encoder's and connector's ->reset
4812 * callback. Drivers can use this in e.g. their driver load or resume code to
4813 * reset hardware and software state.
4815 void drm_mode_config_reset(struct drm_device *dev)
4817 struct drm_crtc *crtc;
4818 struct drm_plane *plane;
4819 struct drm_encoder *encoder;
4820 struct drm_connector *connector;
4822 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4823 if (plane->funcs->reset)
4824 plane->funcs->reset(plane);
4826 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4827 if (crtc->funcs->reset)
4828 crtc->funcs->reset(crtc);
4830 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4831 if (encoder->funcs->reset)
4832 encoder->funcs->reset(encoder);
4834 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4835 connector->status = connector_status_unknown;
4837 if (connector->funcs->reset)
4838 connector->funcs->reset(connector);
4841 EXPORT_SYMBOL(drm_mode_config_reset);
4844 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4847 * @file_priv: DRM file info
4849 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4850 * TTM or something else entirely) and returns the resulting buffer handle. This
4851 * handle can then be wrapped up into a framebuffer modeset object.
4853 * Note that userspace is not allowed to use such objects for render
4854 * acceleration - drivers must create their own private ioctls for such a use
4857 * Called by the user via ioctl.
4860 * Zero on success, negative errno on failure.
4862 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4863 void *data, struct drm_file *file_priv)
4865 struct drm_mode_create_dumb *args = data;
4866 u32 cpp, stride, size;
4868 if (!dev->driver->dumb_create)
4870 if (!args->width || !args->height || !args->bpp)
4873 /* overflow checks for 32bit size calculations */
4874 /* NOTE: DIV_ROUND_UP() can overflow */
4875 cpp = DIV_ROUND_UP(args->bpp, 8);
4876 if (!cpp || cpp > 0xffffffffU / args->width)
4878 stride = cpp * args->width;
4879 if (args->height > 0xffffffffU / stride)
4882 /* test for wrap-around */
4883 size = args->height * stride;
4884 if (PAGE_ALIGN(size) == 0)
4888 * handle, pitch and size are output parameters. Zero them out to
4889 * prevent drivers from accidentally using uninitialized data. Since
4890 * not all existing userspace is clearing these fields properly we
4891 * cannot reject IOCTL with garbage in them.
4897 return dev->driver->dumb_create(file_priv, dev, args);
4901 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4904 * @file_priv: DRM file info
4906 * Allocate an offset in the drm device node's address space to be able to
4907 * memory map a dumb buffer.
4909 * Called by the user via ioctl.
4912 * Zero on success, negative errno on failure.
4914 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4915 void *data, struct drm_file *file_priv)
4917 struct drm_mode_map_dumb *args = data;
4919 /* call driver ioctl to get mmap offset */
4920 if (!dev->driver->dumb_map_offset)
4923 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4927 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4930 * @file_priv: DRM file info
4932 * This destroys the userspace handle for the given dumb backing storage buffer.
4933 * Since buffer objects must be reference counted in the kernel a buffer object
4934 * won't be immediately freed if a framebuffer modeset object still uses it.
4936 * Called by the user via ioctl.
4939 * Zero on success, negative errno on failure.
4941 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4942 void *data, struct drm_file *file_priv)
4944 struct drm_mode_destroy_dumb *args = data;
4946 if (!dev->driver->dumb_destroy)
4949 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4953 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4954 * @format: pixel format (DRM_FORMAT_*)
4955 * @depth: storage for the depth value
4956 * @bpp: storage for the bpp value
4958 * This only supports RGB formats here for compat with code that doesn't use
4959 * pixel formats directly yet.
4961 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
4966 case DRM_FORMAT_RGB332:
4967 case DRM_FORMAT_BGR233:
4971 case DRM_FORMAT_XRGB1555:
4972 case DRM_FORMAT_XBGR1555:
4973 case DRM_FORMAT_RGBX5551:
4974 case DRM_FORMAT_BGRX5551:
4975 case DRM_FORMAT_ARGB1555:
4976 case DRM_FORMAT_ABGR1555:
4977 case DRM_FORMAT_RGBA5551:
4978 case DRM_FORMAT_BGRA5551:
4982 case DRM_FORMAT_RGB565:
4983 case DRM_FORMAT_BGR565:
4987 case DRM_FORMAT_RGB888:
4988 case DRM_FORMAT_BGR888:
4992 case DRM_FORMAT_XRGB8888:
4993 case DRM_FORMAT_XBGR8888:
4994 case DRM_FORMAT_RGBX8888:
4995 case DRM_FORMAT_BGRX8888:
4999 case DRM_FORMAT_XRGB2101010:
5000 case DRM_FORMAT_XBGR2101010:
5001 case DRM_FORMAT_RGBX1010102:
5002 case DRM_FORMAT_BGRX1010102:
5003 case DRM_FORMAT_ARGB2101010:
5004 case DRM_FORMAT_ABGR2101010:
5005 case DRM_FORMAT_RGBA1010102:
5006 case DRM_FORMAT_BGRA1010102:
5010 case DRM_FORMAT_ARGB8888:
5011 case DRM_FORMAT_ABGR8888:
5012 case DRM_FORMAT_RGBA8888:
5013 case DRM_FORMAT_BGRA8888:
5018 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5019 drm_get_format_name(format));
5025 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5028 * drm_format_num_planes - get the number of planes for format
5029 * @format: pixel format (DRM_FORMAT_*)
5032 * The number of planes used by the specified pixel format.
5034 int drm_format_num_planes(uint32_t format)
5037 case DRM_FORMAT_YUV410:
5038 case DRM_FORMAT_YVU410:
5039 case DRM_FORMAT_YUV411:
5040 case DRM_FORMAT_YVU411:
5041 case DRM_FORMAT_YUV420:
5042 case DRM_FORMAT_YVU420:
5043 case DRM_FORMAT_YUV422:
5044 case DRM_FORMAT_YVU422:
5045 case DRM_FORMAT_YUV444:
5046 case DRM_FORMAT_YVU444:
5048 case DRM_FORMAT_NV12:
5049 case DRM_FORMAT_NV21:
5050 case DRM_FORMAT_NV16:
5051 case DRM_FORMAT_NV61:
5052 case DRM_FORMAT_NV24:
5053 case DRM_FORMAT_NV42:
5059 EXPORT_SYMBOL(drm_format_num_planes);
5062 * drm_format_plane_cpp - determine the bytes per pixel value
5063 * @format: pixel format (DRM_FORMAT_*)
5064 * @plane: plane index
5067 * The bytes per pixel value for the specified plane.
5069 int drm_format_plane_cpp(uint32_t format, int plane)
5074 if (plane >= drm_format_num_planes(format))
5078 case DRM_FORMAT_YUYV:
5079 case DRM_FORMAT_YVYU:
5080 case DRM_FORMAT_UYVY:
5081 case DRM_FORMAT_VYUY:
5083 case DRM_FORMAT_NV12:
5084 case DRM_FORMAT_NV21:
5085 case DRM_FORMAT_NV16:
5086 case DRM_FORMAT_NV61:
5087 case DRM_FORMAT_NV24:
5088 case DRM_FORMAT_NV42:
5089 return plane ? 2 : 1;
5090 case DRM_FORMAT_YUV410:
5091 case DRM_FORMAT_YVU410:
5092 case DRM_FORMAT_YUV411:
5093 case DRM_FORMAT_YVU411:
5094 case DRM_FORMAT_YUV420:
5095 case DRM_FORMAT_YVU420:
5096 case DRM_FORMAT_YUV422:
5097 case DRM_FORMAT_YVU422:
5098 case DRM_FORMAT_YUV444:
5099 case DRM_FORMAT_YVU444:
5102 drm_fb_get_bpp_depth(format, &depth, &bpp);
5106 EXPORT_SYMBOL(drm_format_plane_cpp);
5109 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5110 * @format: pixel format (DRM_FORMAT_*)
5113 * The horizontal chroma subsampling factor for the
5114 * specified pixel format.
5116 int drm_format_horz_chroma_subsampling(uint32_t format)
5119 case DRM_FORMAT_YUV411:
5120 case DRM_FORMAT_YVU411:
5121 case DRM_FORMAT_YUV410:
5122 case DRM_FORMAT_YVU410:
5124 case DRM_FORMAT_YUYV:
5125 case DRM_FORMAT_YVYU:
5126 case DRM_FORMAT_UYVY:
5127 case DRM_FORMAT_VYUY:
5128 case DRM_FORMAT_NV12:
5129 case DRM_FORMAT_NV21:
5130 case DRM_FORMAT_NV16:
5131 case DRM_FORMAT_NV61:
5132 case DRM_FORMAT_YUV422:
5133 case DRM_FORMAT_YVU422:
5134 case DRM_FORMAT_YUV420:
5135 case DRM_FORMAT_YVU420:
5141 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5144 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5145 * @format: pixel format (DRM_FORMAT_*)
5148 * The vertical chroma subsampling factor for the
5149 * specified pixel format.
5151 int drm_format_vert_chroma_subsampling(uint32_t format)
5154 case DRM_FORMAT_YUV410:
5155 case DRM_FORMAT_YVU410:
5157 case DRM_FORMAT_YUV420:
5158 case DRM_FORMAT_YVU420:
5159 case DRM_FORMAT_NV12:
5160 case DRM_FORMAT_NV21:
5166 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5169 * drm_rotation_simplify() - Try to simplify the rotation
5170 * @rotation: Rotation to be simplified
5171 * @supported_rotations: Supported rotations
5173 * Attempt to simplify the rotation to a form that is supported.
5174 * Eg. if the hardware supports everything except DRM_REFLECT_X
5175 * one could call this function like this:
5177 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5178 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5179 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5181 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5182 * transforms the hardware supports, this function may not
5183 * be able to produce a supported transform, so the caller should
5184 * check the result afterwards.
5186 unsigned int drm_rotation_simplify(unsigned int rotation,
5187 unsigned int supported_rotations)
5189 if (rotation & ~supported_rotations) {
5190 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5191 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5196 EXPORT_SYMBOL(drm_rotation_simplify);
5199 * drm_mode_config_init - initialize DRM mode_configuration structure
5202 * Initialize @dev's mode_config structure, used for tracking the graphics
5203 * configuration of @dev.
5205 * Since this initializes the modeset locks, no locking is possible. Which is no
5206 * problem, since this should happen single threaded at init time. It is the
5207 * driver's problem to ensure this guarantee.
5210 void drm_mode_config_init(struct drm_device *dev)
5212 mutex_init(&dev->mode_config.mutex);
5213 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5214 mutex_init(&dev->mode_config.idr_mutex);
5215 mutex_init(&dev->mode_config.fb_lock);
5216 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5217 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5218 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5219 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
5220 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5221 INIT_LIST_HEAD(&dev->mode_config.property_list);
5222 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5223 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5224 idr_init(&dev->mode_config.crtc_idr);
5225 idr_init(&dev->mode_config.tile_idr);
5227 drm_modeset_lock_all(dev);
5228 drm_mode_create_standard_connector_properties(dev);
5229 drm_mode_create_standard_plane_properties(dev);
5230 drm_modeset_unlock_all(dev);
5232 /* Just to be sure */
5233 dev->mode_config.num_fb = 0;
5234 dev->mode_config.num_connector = 0;
5235 dev->mode_config.num_crtc = 0;
5236 dev->mode_config.num_encoder = 0;
5237 dev->mode_config.num_overlay_plane = 0;
5238 dev->mode_config.num_total_plane = 0;
5240 EXPORT_SYMBOL(drm_mode_config_init);
5243 * drm_mode_config_cleanup - free up DRM mode_config info
5246 * Free up all the connectors and CRTCs associated with this DRM device, then
5247 * free up the framebuffers and associated buffer objects.
5249 * Note that since this /should/ happen single-threaded at driver/device
5250 * teardown time, no locking is required. It's the driver's job to ensure that
5251 * this guarantee actually holds true.
5253 * FIXME: cleanup any dangling user buffer objects too
5255 void drm_mode_config_cleanup(struct drm_device *dev)
5257 struct drm_connector *connector, *ot;
5258 struct drm_crtc *crtc, *ct;
5259 struct drm_encoder *encoder, *enct;
5260 struct drm_bridge *bridge, *brt;
5261 struct drm_framebuffer *fb, *fbt;
5262 struct drm_property *property, *pt;
5263 struct drm_property_blob *blob, *bt;
5264 struct drm_plane *plane, *plt;
5266 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5268 encoder->funcs->destroy(encoder);
5271 list_for_each_entry_safe(bridge, brt,
5272 &dev->mode_config.bridge_list, head) {
5273 bridge->funcs->destroy(bridge);
5276 list_for_each_entry_safe(connector, ot,
5277 &dev->mode_config.connector_list, head) {
5278 connector->funcs->destroy(connector);
5281 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5283 drm_property_destroy(dev, property);
5286 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5288 drm_property_destroy_blob(dev, blob);
5292 * Single-threaded teardown context, so it's not required to grab the
5293 * fb_lock to protect against concurrent fb_list access. Contrary, it
5294 * would actually deadlock with the drm_framebuffer_cleanup function.
5296 * Also, if there are any framebuffers left, that's a driver leak now,
5297 * so politely WARN about this.
5299 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5300 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5301 drm_framebuffer_remove(fb);
5304 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5306 plane->funcs->destroy(plane);
5309 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5310 crtc->funcs->destroy(crtc);
5313 idr_destroy(&dev->mode_config.tile_idr);
5314 idr_destroy(&dev->mode_config.crtc_idr);
5315 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5317 EXPORT_SYMBOL(drm_mode_config_cleanup);
5319 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5320 unsigned int supported_rotations)
5322 static const struct drm_prop_enum_list props[] = {
5323 { DRM_ROTATE_0, "rotate-0" },
5324 { DRM_ROTATE_90, "rotate-90" },
5325 { DRM_ROTATE_180, "rotate-180" },
5326 { DRM_ROTATE_270, "rotate-270" },
5327 { DRM_REFLECT_X, "reflect-x" },
5328 { DRM_REFLECT_Y, "reflect-y" },
5331 return drm_property_create_bitmask(dev, 0, "rotation",
5332 props, ARRAY_SIZE(props),
5333 supported_rotations);
5335 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5340 * Tile groups are used to represent tiled monitors with a unique
5341 * integer identifier. Tiled monitors using DisplayID v1.3 have
5342 * a unique 8-byte handle, we store this in a tile group, so we
5343 * have a common identifier for all tiles in a monitor group.
5345 static void drm_tile_group_free(struct kref *kref)
5347 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5348 struct drm_device *dev = tg->dev;
5349 mutex_lock(&dev->mode_config.idr_mutex);
5350 idr_remove(&dev->mode_config.tile_idr, tg->id);
5351 mutex_unlock(&dev->mode_config.idr_mutex);
5356 * drm_mode_put_tile_group - drop a reference to a tile group.
5358 * @tg: tile group to drop reference to.
5360 * drop reference to tile group and free if 0.
5362 void drm_mode_put_tile_group(struct drm_device *dev,
5363 struct drm_tile_group *tg)
5365 kref_put(&tg->refcount, drm_tile_group_free);
5369 * drm_mode_get_tile_group - get a reference to an existing tile group
5371 * @topology: 8-bytes unique per monitor.
5373 * Use the unique bytes to get a reference to an existing tile group.
5376 * tile group or NULL if not found.
5378 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5381 struct drm_tile_group *tg;
5383 mutex_lock(&dev->mode_config.idr_mutex);
5384 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5385 if (!memcmp(tg->group_data, topology, 8)) {
5386 if (!kref_get_unless_zero(&tg->refcount))
5388 mutex_unlock(&dev->mode_config.idr_mutex);
5392 mutex_unlock(&dev->mode_config.idr_mutex);
5397 * drm_mode_create_tile_group - create a tile group from a displayid description
5399 * @topology: 8-bytes unique per monitor.
5401 * Create a tile group for the unique monitor, and get a unique
5402 * identifier for the tile group.
5405 * new tile group or error.
5407 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5410 struct drm_tile_group *tg;
5413 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5415 return ERR_PTR(-ENOMEM);
5417 kref_init(&tg->refcount);
5418 memcpy(tg->group_data, topology, 8);
5421 mutex_lock(&dev->mode_config.idr_mutex);
5422 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5430 mutex_unlock(&dev->mode_config.idr_mutex);