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>
41 #include <drm/drm_atomic.h>
43 #include "drm_crtc_internal.h"
44 #include "drm_internal.h"
46 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
47 struct drm_mode_fb_cmd2 *r,
48 struct drm_file *file_priv);
50 /* Avoid boilerplate. I'm tired of typing. */
51 #define DRM_ENUM_NAME_FN(fnname, list) \
52 const char *fnname(int val) \
55 for (i = 0; i < ARRAY_SIZE(list); i++) { \
56 if (list[i].type == val) \
57 return list[i].name; \
65 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
66 { DRM_MODE_DPMS_ON, "On" },
67 { DRM_MODE_DPMS_STANDBY, "Standby" },
68 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
69 { DRM_MODE_DPMS_OFF, "Off" }
72 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
74 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
75 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
76 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
77 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
83 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
84 { DRM_MODE_SCALE_NONE, "None" },
85 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
86 { DRM_MODE_SCALE_CENTER, "Center" },
87 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
90 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
91 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
92 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
93 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
97 * Non-global properties, but "required" for certain connectors.
99 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
100 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
101 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
102 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
105 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
107 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
108 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
109 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
110 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
113 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
114 drm_dvi_i_subconnector_enum_list)
116 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
117 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
124 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
126 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
127 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
128 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
129 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
134 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
135 drm_tv_subconnector_enum_list)
137 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
138 { DRM_MODE_DIRTY_OFF, "Off" },
139 { DRM_MODE_DIRTY_ON, "On" },
140 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
143 struct drm_conn_prop_enum_list {
150 * Connector and encoder types.
152 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
153 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
154 { DRM_MODE_CONNECTOR_VGA, "VGA" },
155 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
156 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
157 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
158 { DRM_MODE_CONNECTOR_Composite, "Composite" },
159 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
160 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
161 { DRM_MODE_CONNECTOR_Component, "Component" },
162 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
163 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
164 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
165 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
166 { DRM_MODE_CONNECTOR_TV, "TV" },
167 { DRM_MODE_CONNECTOR_eDP, "eDP" },
168 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
169 { DRM_MODE_CONNECTOR_DSI, "DSI" },
172 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
173 { DRM_MODE_ENCODER_NONE, "None" },
174 { DRM_MODE_ENCODER_DAC, "DAC" },
175 { DRM_MODE_ENCODER_TMDS, "TMDS" },
176 { DRM_MODE_ENCODER_LVDS, "LVDS" },
177 { DRM_MODE_ENCODER_TVDAC, "TV" },
178 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
179 { DRM_MODE_ENCODER_DSI, "DSI" },
180 { DRM_MODE_ENCODER_DPMST, "DP MST" },
183 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
184 { SubPixelUnknown, "Unknown" },
185 { SubPixelHorizontalRGB, "Horizontal RGB" },
186 { SubPixelHorizontalBGR, "Horizontal BGR" },
187 { SubPixelVerticalRGB, "Vertical RGB" },
188 { SubPixelVerticalBGR, "Vertical BGR" },
189 { SubPixelNone, "None" },
192 void drm_connector_ida_init(void)
196 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
197 ida_init(&drm_connector_enum_list[i].ida);
200 void drm_connector_ida_destroy(void)
204 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
205 ida_destroy(&drm_connector_enum_list[i].ida);
209 * drm_get_connector_status_name - return a string for connector status
210 * @status: connector status to compute name of
212 * In contrast to the other drm_get_*_name functions this one here returns a
213 * const pointer and hence is threadsafe.
215 const char *drm_get_connector_status_name(enum drm_connector_status status)
217 if (status == connector_status_connected)
219 else if (status == connector_status_disconnected)
220 return "disconnected";
224 EXPORT_SYMBOL(drm_get_connector_status_name);
227 * drm_get_subpixel_order_name - return a string for a given subpixel enum
228 * @order: enum of subpixel_order
230 * Note you could abuse this and return something out of bounds, but that
231 * would be a caller error. No unscrubbed user data should make it here.
233 const char *drm_get_subpixel_order_name(enum subpixel_order order)
235 return drm_subpixel_enum_list[order].name;
237 EXPORT_SYMBOL(drm_get_subpixel_order_name);
239 static char printable_char(int c)
241 return isascii(c) && isprint(c) ? c : '?';
245 * drm_get_format_name - return a string for drm fourcc format
246 * @format: format to compute name of
248 * Note that the buffer used by this function is globally shared and owned by
249 * the function itself.
251 * FIXME: This isn't really multithreading safe.
253 const char *drm_get_format_name(uint32_t format)
257 snprintf(buf, sizeof(buf),
258 "%c%c%c%c %s-endian (0x%08x)",
259 printable_char(format & 0xff),
260 printable_char((format >> 8) & 0xff),
261 printable_char((format >> 16) & 0xff),
262 printable_char((format >> 24) & 0x7f),
263 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
268 EXPORT_SYMBOL(drm_get_format_name);
271 * Internal function to assign a slot in the object idr and optionally
272 * register the object into the idr.
274 static int drm_mode_object_get_reg(struct drm_device *dev,
275 struct drm_mode_object *obj,
281 mutex_lock(&dev->mode_config.idr_mutex);
282 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
285 * Set up the object linking under the protection of the idr
286 * lock so that other users can't see inconsistent state.
289 obj->type = obj_type;
291 mutex_unlock(&dev->mode_config.idr_mutex);
293 return ret < 0 ? ret : 0;
297 * drm_mode_object_get - allocate a new modeset identifier
299 * @obj: object pointer, used to generate unique ID
300 * @obj_type: object type
302 * Create a unique identifier based on @ptr in @dev's identifier space. Used
303 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
304 * modeset identifiers are _not_ reference counted. Hence don't use this for
305 * reference counted modeset objects like framebuffers.
308 * New unique (relative to other objects in @dev) integer identifier for the
311 int drm_mode_object_get(struct drm_device *dev,
312 struct drm_mode_object *obj, uint32_t obj_type)
314 return drm_mode_object_get_reg(dev, obj, obj_type, true);
317 static void drm_mode_object_register(struct drm_device *dev,
318 struct drm_mode_object *obj)
320 mutex_lock(&dev->mode_config.idr_mutex);
321 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
322 mutex_unlock(&dev->mode_config.idr_mutex);
326 * drm_mode_object_put - free a modeset identifer
328 * @object: object to free
330 * Free @id from @dev's unique identifier pool. Note that despite the _get
331 * postfix modeset identifiers are _not_ reference counted. Hence don't use this
332 * for reference counted modeset objects like framebuffers.
334 void drm_mode_object_put(struct drm_device *dev,
335 struct drm_mode_object *object)
337 mutex_lock(&dev->mode_config.idr_mutex);
338 idr_remove(&dev->mode_config.crtc_idr, object->id);
339 mutex_unlock(&dev->mode_config.idr_mutex);
342 static struct drm_mode_object *_object_find(struct drm_device *dev,
343 uint32_t id, uint32_t type)
345 struct drm_mode_object *obj = NULL;
347 mutex_lock(&dev->mode_config.idr_mutex);
348 obj = idr_find(&dev->mode_config.crtc_idr, id);
349 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
351 if (obj && obj->id != id)
353 /* don't leak out unref'd fb's */
354 if (obj && (obj->type == DRM_MODE_OBJECT_FB))
356 mutex_unlock(&dev->mode_config.idr_mutex);
362 * drm_mode_object_find - look up a drm object with static lifetime
364 * @id: id of the mode object
365 * @type: type of the mode object
367 * Note that framebuffers cannot be looked up with this functions - since those
368 * are reference counted, they need special treatment. Even with
369 * DRM_MODE_OBJECT_ANY (although that will simply return NULL
370 * rather than WARN_ON()).
372 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
373 uint32_t id, uint32_t type)
375 struct drm_mode_object *obj = NULL;
377 /* Framebuffers are reference counted and need their own lookup
379 WARN_ON(type == DRM_MODE_OBJECT_FB);
380 obj = _object_find(dev, id, type);
383 EXPORT_SYMBOL(drm_mode_object_find);
386 * drm_framebuffer_init - initialize a framebuffer
388 * @fb: framebuffer to be initialized
389 * @funcs: ... with these functions
391 * Allocates an ID for the framebuffer's parent mode object, sets its mode
392 * functions & device file and adds it to the master fd list.
395 * This functions publishes the fb and makes it available for concurrent access
396 * by other users. Which means by this point the fb _must_ be fully set up -
397 * since all the fb attributes are invariant over its lifetime, no further
398 * locking but only correct reference counting is required.
401 * Zero on success, error code on failure.
403 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
404 const struct drm_framebuffer_funcs *funcs)
408 mutex_lock(&dev->mode_config.fb_lock);
409 kref_init(&fb->refcount);
410 INIT_LIST_HEAD(&fb->filp_head);
414 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
418 dev->mode_config.num_fb++;
419 list_add(&fb->head, &dev->mode_config.fb_list);
421 mutex_unlock(&dev->mode_config.fb_lock);
425 EXPORT_SYMBOL(drm_framebuffer_init);
427 /* dev->mode_config.fb_lock must be held! */
428 static void __drm_framebuffer_unregister(struct drm_device *dev,
429 struct drm_framebuffer *fb)
431 mutex_lock(&dev->mode_config.idr_mutex);
432 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
433 mutex_unlock(&dev->mode_config.idr_mutex);
438 static void drm_framebuffer_free(struct kref *kref)
440 struct drm_framebuffer *fb =
441 container_of(kref, struct drm_framebuffer, refcount);
442 struct drm_device *dev = fb->dev;
445 * The lookup idr holds a weak reference, which has not necessarily been
446 * removed at this point. Check for that.
448 mutex_lock(&dev->mode_config.fb_lock);
450 /* Mark fb as reaped and drop idr ref. */
451 __drm_framebuffer_unregister(dev, fb);
453 mutex_unlock(&dev->mode_config.fb_lock);
455 fb->funcs->destroy(fb);
458 static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
461 struct drm_mode_object *obj = NULL;
462 struct drm_framebuffer *fb;
464 mutex_lock(&dev->mode_config.idr_mutex);
465 obj = idr_find(&dev->mode_config.crtc_idr, id);
466 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
470 mutex_unlock(&dev->mode_config.idr_mutex);
476 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
478 * @id: id of the fb object
480 * If successful, this grabs an additional reference to the framebuffer -
481 * callers need to make sure to eventually unreference the returned framebuffer
482 * again, using @drm_framebuffer_unreference.
484 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
487 struct drm_framebuffer *fb;
489 mutex_lock(&dev->mode_config.fb_lock);
490 fb = __drm_framebuffer_lookup(dev, id);
492 if (!kref_get_unless_zero(&fb->refcount))
495 mutex_unlock(&dev->mode_config.fb_lock);
499 EXPORT_SYMBOL(drm_framebuffer_lookup);
502 * drm_framebuffer_unreference - unref a framebuffer
503 * @fb: framebuffer to unref
505 * This functions decrements the fb's refcount and frees it if it drops to zero.
507 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
509 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
510 kref_put(&fb->refcount, drm_framebuffer_free);
512 EXPORT_SYMBOL(drm_framebuffer_unreference);
515 * drm_framebuffer_reference - incr the fb refcnt
518 * This functions increments the fb's refcount.
520 void drm_framebuffer_reference(struct drm_framebuffer *fb)
522 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
523 kref_get(&fb->refcount);
525 EXPORT_SYMBOL(drm_framebuffer_reference);
527 static void drm_framebuffer_free_bug(struct kref *kref)
532 static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
534 DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
535 kref_put(&fb->refcount, drm_framebuffer_free_bug);
539 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
540 * @fb: fb to unregister
542 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
543 * those used for fbdev. Note that the caller must hold a reference of it's own,
544 * i.e. the object may not be destroyed through this call (since it'll lead to a
545 * locking inversion).
547 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
549 struct drm_device *dev = fb->dev;
551 mutex_lock(&dev->mode_config.fb_lock);
552 /* Mark fb as reaped and drop idr ref. */
553 __drm_framebuffer_unregister(dev, fb);
554 mutex_unlock(&dev->mode_config.fb_lock);
556 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
559 * drm_framebuffer_cleanup - remove a framebuffer object
560 * @fb: framebuffer to remove
562 * Cleanup framebuffer. This function is intended to be used from the drivers
563 * ->destroy callback. It can also be used to clean up driver private
564 * framebuffers embedded into a larger structure.
566 * Note that this function does not remove the fb from active usuage - if it is
567 * still used anywhere, hilarity can ensue since userspace could call getfb on
568 * the id and get back -EINVAL. Obviously no concern at driver unload time.
570 * Also, the framebuffer will not be removed from the lookup idr - for
571 * user-created framebuffers this will happen in in the rmfb ioctl. For
572 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
573 * drm_framebuffer_unregister_private.
575 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
577 struct drm_device *dev = fb->dev;
579 mutex_lock(&dev->mode_config.fb_lock);
581 dev->mode_config.num_fb--;
582 mutex_unlock(&dev->mode_config.fb_lock);
584 EXPORT_SYMBOL(drm_framebuffer_cleanup);
587 * drm_framebuffer_remove - remove and unreference a framebuffer object
588 * @fb: framebuffer to remove
590 * Scans all the CRTCs and planes in @dev's mode_config. If they're
591 * using @fb, removes it, setting it to NULL. Then drops the reference to the
592 * passed-in framebuffer. Might take the modeset locks.
594 * Note that this function optimizes the cleanup away if the caller holds the
595 * last reference to the framebuffer. It is also guaranteed to not take the
596 * modeset locks in this case.
598 void drm_framebuffer_remove(struct drm_framebuffer *fb)
600 struct drm_device *dev = fb->dev;
601 struct drm_crtc *crtc;
602 struct drm_plane *plane;
603 struct drm_mode_set set;
606 WARN_ON(!list_empty(&fb->filp_head));
609 * drm ABI mandates that we remove any deleted framebuffers from active
610 * useage. But since most sane clients only remove framebuffers they no
611 * longer need, try to optimize this away.
613 * Since we're holding a reference ourselves, observing a refcount of 1
614 * means that we're the last holder and can skip it. Also, the refcount
615 * can never increase from 1 again, so we don't need any barriers or
618 * Note that userspace could try to race with use and instate a new
619 * usage _after_ we've cleared all current ones. End result will be an
620 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
623 if (atomic_read(&fb->refcount.refcount) > 1) {
624 drm_modeset_lock_all(dev);
625 /* remove from any CRTC */
626 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
627 if (crtc->primary->fb == fb) {
628 /* should turn off the crtc */
629 memset(&set, 0, sizeof(struct drm_mode_set));
632 ret = drm_mode_set_config_internal(&set);
634 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
638 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
640 drm_plane_force_disable(plane);
642 drm_modeset_unlock_all(dev);
645 drm_framebuffer_unreference(fb);
647 EXPORT_SYMBOL(drm_framebuffer_remove);
649 DEFINE_WW_CLASS(crtc_ww_class);
652 * drm_crtc_init_with_planes - Initialise a new CRTC object with
653 * specified primary and cursor planes.
655 * @crtc: CRTC object to init
656 * @primary: Primary plane for CRTC
657 * @cursor: Cursor plane for CRTC
658 * @funcs: callbacks for the new CRTC
660 * Inits a new object created as base part of a driver crtc object.
663 * Zero on success, error code on failure.
665 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
666 struct drm_plane *primary,
667 struct drm_plane *cursor,
668 const struct drm_crtc_funcs *funcs)
670 struct drm_mode_config *config = &dev->mode_config;
675 crtc->invert_dimensions = false;
677 drm_modeset_lock_init(&crtc->mutex);
678 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
682 crtc->base.properties = &crtc->properties;
684 list_add_tail(&crtc->head, &config->crtc_list);
687 crtc->primary = primary;
688 crtc->cursor = cursor;
690 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
692 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
696 EXPORT_SYMBOL(drm_crtc_init_with_planes);
699 * drm_crtc_cleanup - Clean up the core crtc usage
700 * @crtc: CRTC to cleanup
702 * This function cleans up @crtc and removes it from the DRM mode setting
703 * core. Note that the function does *not* free the crtc structure itself,
704 * this is the responsibility of the caller.
706 void drm_crtc_cleanup(struct drm_crtc *crtc)
708 struct drm_device *dev = crtc->dev;
710 kfree(crtc->gamma_store);
711 crtc->gamma_store = NULL;
713 drm_modeset_lock_fini(&crtc->mutex);
715 drm_mode_object_put(dev, &crtc->base);
716 list_del(&crtc->head);
717 dev->mode_config.num_crtc--;
719 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
720 if (crtc->state && crtc->funcs->atomic_destroy_state)
721 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
723 memset(crtc, 0, sizeof(*crtc));
725 EXPORT_SYMBOL(drm_crtc_cleanup);
728 * drm_crtc_index - find the index of a registered CRTC
729 * @crtc: CRTC to find index for
731 * Given a registered CRTC, return the index of that CRTC within a DRM
732 * device's list of CRTCs.
734 unsigned int drm_crtc_index(struct drm_crtc *crtc)
736 unsigned int index = 0;
737 struct drm_crtc *tmp;
739 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
748 EXPORT_SYMBOL(drm_crtc_index);
751 * drm_mode_remove - remove and free a mode
752 * @connector: connector list to modify
753 * @mode: mode to remove
755 * Remove @mode from @connector's mode list, then free it.
757 static void drm_mode_remove(struct drm_connector *connector,
758 struct drm_display_mode *mode)
760 list_del(&mode->head);
761 drm_mode_destroy(connector->dev, mode);
765 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
766 * @connector: connector to quwery
768 * The kernel supports per-connector configration of its consoles through
769 * use of the video= parameter. This function parses that option and
770 * extracts the user's specified mode (or enable/disable status) for a
771 * particular connector. This is typically only used during the early fbdev
774 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
776 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
779 if (fb_get_options(connector->name, &option))
782 if (!drm_mode_parse_command_line_for_connector(option,
790 switch (mode->force) {
794 case DRM_FORCE_ON_DIGITAL:
803 DRM_INFO("forcing %s connector %s\n", connector->name, s);
804 connector->force = mode->force;
807 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
809 mode->xres, mode->yres,
810 mode->refresh_specified ? mode->refresh : 60,
811 mode->rb ? " reduced blanking" : "",
812 mode->margins ? " with margins" : "",
813 mode->interlace ? " interlaced" : "");
817 * drm_connector_init - Init a preallocated connector
819 * @connector: the connector to init
820 * @funcs: callbacks for this connector
821 * @connector_type: user visible type of the connector
823 * Initialises a preallocated connector. Connectors should be
824 * subclassed as part of driver connector objects.
827 * Zero on success, error code on failure.
829 int drm_connector_init(struct drm_device *dev,
830 struct drm_connector *connector,
831 const struct drm_connector_funcs *funcs,
835 struct ida *connector_ida =
836 &drm_connector_enum_list[connector_type].ida;
838 drm_modeset_lock_all(dev);
840 ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
844 connector->base.properties = &connector->properties;
845 connector->dev = dev;
846 connector->funcs = funcs;
847 connector->connector_type = connector_type;
848 connector->connector_type_id =
849 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
850 if (connector->connector_type_id < 0) {
851 ret = connector->connector_type_id;
855 kasprintf(GFP_KERNEL, "%s-%d",
856 drm_connector_enum_list[connector_type].name,
857 connector->connector_type_id);
858 if (!connector->name) {
863 INIT_LIST_HEAD(&connector->probed_modes);
864 INIT_LIST_HEAD(&connector->modes);
865 connector->edid_blob_ptr = NULL;
866 connector->status = connector_status_unknown;
868 drm_connector_get_cmdline_mode(connector);
870 /* We should add connectors at the end to avoid upsetting the connector
872 list_add_tail(&connector->head, &dev->mode_config.connector_list);
873 dev->mode_config.num_connector++;
875 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
876 drm_object_attach_property(&connector->base,
877 dev->mode_config.edid_property,
880 drm_object_attach_property(&connector->base,
881 dev->mode_config.dpms_property, 0);
883 connector->debugfs_entry = NULL;
887 drm_mode_object_put(dev, &connector->base);
890 drm_modeset_unlock_all(dev);
894 EXPORT_SYMBOL(drm_connector_init);
897 * drm_connector_cleanup - cleans up an initialised connector
898 * @connector: connector to cleanup
900 * Cleans up the connector but doesn't free the object.
902 void drm_connector_cleanup(struct drm_connector *connector)
904 struct drm_device *dev = connector->dev;
905 struct drm_display_mode *mode, *t;
907 if (connector->tile_group) {
908 drm_mode_put_tile_group(dev, connector->tile_group);
909 connector->tile_group = NULL;
912 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
913 drm_mode_remove(connector, mode);
915 list_for_each_entry_safe(mode, t, &connector->modes, head)
916 drm_mode_remove(connector, mode);
918 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
919 connector->connector_type_id);
921 drm_mode_object_put(dev, &connector->base);
922 kfree(connector->name);
923 connector->name = NULL;
924 list_del(&connector->head);
925 dev->mode_config.num_connector--;
927 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
928 if (connector->state && connector->funcs->atomic_destroy_state)
929 connector->funcs->atomic_destroy_state(connector,
932 memset(connector, 0, sizeof(*connector));
934 EXPORT_SYMBOL(drm_connector_cleanup);
937 * drm_connector_index - find the index of a registered connector
938 * @connector: connector to find index for
940 * Given a registered connector, return the index of that connector within a DRM
941 * device's list of connectors.
943 unsigned int drm_connector_index(struct drm_connector *connector)
945 unsigned int index = 0;
946 struct drm_connector *tmp;
947 struct drm_mode_config *config = &connector->dev->mode_config;
949 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
951 list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
952 if (tmp == connector)
960 EXPORT_SYMBOL(drm_connector_index);
963 * drm_connector_register - register a connector
964 * @connector: the connector to register
966 * Register userspace interfaces for a connector
969 * Zero on success, error code on failure.
971 int drm_connector_register(struct drm_connector *connector)
975 drm_mode_object_register(connector->dev, &connector->base);
977 ret = drm_sysfs_connector_add(connector);
981 ret = drm_debugfs_connector_add(connector);
983 drm_sysfs_connector_remove(connector);
989 EXPORT_SYMBOL(drm_connector_register);
992 * drm_connector_unregister - unregister a connector
993 * @connector: the connector to unregister
995 * Unregister userspace interfaces for a connector
997 void drm_connector_unregister(struct drm_connector *connector)
999 drm_sysfs_connector_remove(connector);
1000 drm_debugfs_connector_remove(connector);
1002 EXPORT_SYMBOL(drm_connector_unregister);
1006 * drm_connector_unplug_all - unregister connector userspace interfaces
1009 * This function unregisters all connector userspace interfaces in sysfs. Should
1010 * be call when the device is disconnected, e.g. from an usb driver's
1011 * ->disconnect callback.
1013 void drm_connector_unplug_all(struct drm_device *dev)
1015 struct drm_connector *connector;
1017 /* taking the mode config mutex ends up in a clash with sysfs */
1018 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1019 drm_connector_unregister(connector);
1022 EXPORT_SYMBOL(drm_connector_unplug_all);
1025 * drm_bridge_init - initialize a drm transcoder/bridge
1027 * @bridge: transcoder/bridge to set up
1028 * @funcs: bridge function table
1030 * Initialises a preallocated bridge. Bridges should be
1031 * subclassed as part of driver connector objects.
1034 * Zero on success, error code on failure.
1036 int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
1037 const struct drm_bridge_funcs *funcs)
1041 drm_modeset_lock_all(dev);
1043 ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
1048 bridge->funcs = funcs;
1050 list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
1051 dev->mode_config.num_bridge++;
1054 drm_modeset_unlock_all(dev);
1057 EXPORT_SYMBOL(drm_bridge_init);
1060 * drm_bridge_cleanup - cleans up an initialised bridge
1061 * @bridge: bridge to cleanup
1063 * Cleans up the bridge but doesn't free the object.
1065 void drm_bridge_cleanup(struct drm_bridge *bridge)
1067 struct drm_device *dev = bridge->dev;
1069 drm_modeset_lock_all(dev);
1070 drm_mode_object_put(dev, &bridge->base);
1071 list_del(&bridge->head);
1072 dev->mode_config.num_bridge--;
1073 drm_modeset_unlock_all(dev);
1075 memset(bridge, 0, sizeof(*bridge));
1077 EXPORT_SYMBOL(drm_bridge_cleanup);
1080 * drm_encoder_init - Init a preallocated encoder
1082 * @encoder: the encoder to init
1083 * @funcs: callbacks for this encoder
1084 * @encoder_type: user visible type of the encoder
1086 * Initialises a preallocated encoder. Encoder should be
1087 * subclassed as part of driver encoder objects.
1090 * Zero on success, error code on failure.
1092 int drm_encoder_init(struct drm_device *dev,
1093 struct drm_encoder *encoder,
1094 const struct drm_encoder_funcs *funcs,
1099 drm_modeset_lock_all(dev);
1101 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1106 encoder->encoder_type = encoder_type;
1107 encoder->funcs = funcs;
1108 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1109 drm_encoder_enum_list[encoder_type].name,
1111 if (!encoder->name) {
1116 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1117 dev->mode_config.num_encoder++;
1121 drm_mode_object_put(dev, &encoder->base);
1124 drm_modeset_unlock_all(dev);
1128 EXPORT_SYMBOL(drm_encoder_init);
1131 * drm_encoder_cleanup - cleans up an initialised encoder
1132 * @encoder: encoder to cleanup
1134 * Cleans up the encoder but doesn't free the object.
1136 void drm_encoder_cleanup(struct drm_encoder *encoder)
1138 struct drm_device *dev = encoder->dev;
1140 drm_modeset_lock_all(dev);
1141 drm_mode_object_put(dev, &encoder->base);
1142 kfree(encoder->name);
1143 list_del(&encoder->head);
1144 dev->mode_config.num_encoder--;
1145 drm_modeset_unlock_all(dev);
1147 memset(encoder, 0, sizeof(*encoder));
1149 EXPORT_SYMBOL(drm_encoder_cleanup);
1152 * drm_universal_plane_init - Initialize a new universal plane object
1154 * @plane: plane object to init
1155 * @possible_crtcs: bitmask of possible CRTCs
1156 * @funcs: callbacks for the new plane
1157 * @formats: array of supported formats (%DRM_FORMAT_*)
1158 * @format_count: number of elements in @formats
1159 * @type: type of plane (overlay, primary, cursor)
1161 * Initializes a plane object of type @type.
1164 * Zero on success, error code on failure.
1166 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1167 unsigned long possible_crtcs,
1168 const struct drm_plane_funcs *funcs,
1169 const uint32_t *formats, uint32_t format_count,
1170 enum drm_plane_type type)
1174 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1178 drm_modeset_lock_init(&plane->mutex);
1180 plane->base.properties = &plane->properties;
1182 plane->funcs = funcs;
1183 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1185 if (!plane->format_types) {
1186 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1187 drm_mode_object_put(dev, &plane->base);
1191 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1192 plane->format_count = format_count;
1193 plane->possible_crtcs = possible_crtcs;
1196 list_add_tail(&plane->head, &dev->mode_config.plane_list);
1197 dev->mode_config.num_total_plane++;
1198 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1199 dev->mode_config.num_overlay_plane++;
1201 drm_object_attach_property(&plane->base,
1202 dev->mode_config.plane_type_property,
1207 EXPORT_SYMBOL(drm_universal_plane_init);
1210 * drm_plane_init - Initialize a legacy plane
1212 * @plane: plane object to init
1213 * @possible_crtcs: bitmask of possible CRTCs
1214 * @funcs: callbacks for the new plane
1215 * @formats: array of supported formats (%DRM_FORMAT_*)
1216 * @format_count: number of elements in @formats
1217 * @is_primary: plane type (primary vs overlay)
1219 * Legacy API to initialize a DRM plane.
1221 * New drivers should call drm_universal_plane_init() instead.
1224 * Zero on success, error code on failure.
1226 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1227 unsigned long possible_crtcs,
1228 const struct drm_plane_funcs *funcs,
1229 const uint32_t *formats, uint32_t format_count,
1232 enum drm_plane_type type;
1234 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1235 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1236 formats, format_count, type);
1238 EXPORT_SYMBOL(drm_plane_init);
1241 * drm_plane_cleanup - Clean up the core plane usage
1242 * @plane: plane to cleanup
1244 * This function cleans up @plane and removes it from the DRM mode setting
1245 * core. Note that the function does *not* free the plane structure itself,
1246 * this is the responsibility of the caller.
1248 void drm_plane_cleanup(struct drm_plane *plane)
1250 struct drm_device *dev = plane->dev;
1252 drm_modeset_lock_all(dev);
1253 kfree(plane->format_types);
1254 drm_mode_object_put(dev, &plane->base);
1256 BUG_ON(list_empty(&plane->head));
1258 list_del(&plane->head);
1259 dev->mode_config.num_total_plane--;
1260 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1261 dev->mode_config.num_overlay_plane--;
1262 drm_modeset_unlock_all(dev);
1264 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1265 if (plane->state && plane->funcs->atomic_destroy_state)
1266 plane->funcs->atomic_destroy_state(plane, plane->state);
1268 memset(plane, 0, sizeof(*plane));
1270 EXPORT_SYMBOL(drm_plane_cleanup);
1273 * drm_plane_index - find the index of a registered plane
1274 * @plane: plane to find index for
1276 * Given a registered plane, return the index of that CRTC within a DRM
1277 * device's list of planes.
1279 unsigned int drm_plane_index(struct drm_plane *plane)
1281 unsigned int index = 0;
1282 struct drm_plane *tmp;
1284 list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
1293 EXPORT_SYMBOL(drm_plane_index);
1296 * drm_plane_force_disable - Forcibly disable a plane
1297 * @plane: plane to disable
1299 * Forces the plane to be disabled.
1301 * Used when the plane's current framebuffer is destroyed,
1302 * and when restoring fbdev mode.
1304 void drm_plane_force_disable(struct drm_plane *plane)
1311 plane->old_fb = plane->fb;
1312 ret = plane->funcs->disable_plane(plane);
1314 DRM_ERROR("failed to disable plane with busy fb\n");
1315 plane->old_fb = NULL;
1318 /* disconnect the plane from the fb and crtc: */
1319 __drm_framebuffer_unreference(plane->old_fb);
1320 plane->old_fb = NULL;
1324 EXPORT_SYMBOL(drm_plane_force_disable);
1326 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
1328 struct drm_property *prop;
1331 * Standard properties (apply to all connectors)
1333 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1334 DRM_MODE_PROP_IMMUTABLE,
1338 dev->mode_config.edid_property = prop;
1340 prop = drm_property_create_enum(dev, 0,
1341 "DPMS", drm_dpms_enum_list,
1342 ARRAY_SIZE(drm_dpms_enum_list));
1345 dev->mode_config.dpms_property = prop;
1347 prop = drm_property_create(dev,
1348 DRM_MODE_PROP_BLOB |
1349 DRM_MODE_PROP_IMMUTABLE,
1353 dev->mode_config.path_property = prop;
1355 prop = drm_property_create(dev,
1356 DRM_MODE_PROP_BLOB |
1357 DRM_MODE_PROP_IMMUTABLE,
1361 dev->mode_config.tile_property = prop;
1366 static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
1368 struct drm_property *type;
1371 * Standard properties (apply to all planes)
1373 type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1374 "type", drm_plane_type_enum_list,
1375 ARRAY_SIZE(drm_plane_type_enum_list));
1376 dev->mode_config.plane_type_property = type;
1382 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1385 * Called by a driver the first time a DVI-I connector is made.
1387 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1389 struct drm_property *dvi_i_selector;
1390 struct drm_property *dvi_i_subconnector;
1392 if (dev->mode_config.dvi_i_select_subconnector_property)
1396 drm_property_create_enum(dev, 0,
1397 "select subconnector",
1398 drm_dvi_i_select_enum_list,
1399 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1400 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1402 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1404 drm_dvi_i_subconnector_enum_list,
1405 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1406 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1410 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1413 * drm_create_tv_properties - create TV specific connector properties
1415 * @num_modes: number of different TV formats (modes) supported
1416 * @modes: array of pointers to strings containing name of each format
1418 * Called by a driver's TV initialization routine, this function creates
1419 * the TV specific connector properties for a given device. Caller is
1420 * responsible for allocating a list of format names and passing them to
1423 int drm_mode_create_tv_properties(struct drm_device *dev,
1424 unsigned int num_modes,
1427 struct drm_property *tv_selector;
1428 struct drm_property *tv_subconnector;
1431 if (dev->mode_config.tv_select_subconnector_property)
1435 * Basic connector properties
1437 tv_selector = drm_property_create_enum(dev, 0,
1438 "select subconnector",
1439 drm_tv_select_enum_list,
1440 ARRAY_SIZE(drm_tv_select_enum_list));
1441 dev->mode_config.tv_select_subconnector_property = tv_selector;
1444 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1446 drm_tv_subconnector_enum_list,
1447 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1448 dev->mode_config.tv_subconnector_property = tv_subconnector;
1451 * Other, TV specific properties: margins & TV modes.
1453 dev->mode_config.tv_left_margin_property =
1454 drm_property_create_range(dev, 0, "left margin", 0, 100);
1456 dev->mode_config.tv_right_margin_property =
1457 drm_property_create_range(dev, 0, "right margin", 0, 100);
1459 dev->mode_config.tv_top_margin_property =
1460 drm_property_create_range(dev, 0, "top margin", 0, 100);
1462 dev->mode_config.tv_bottom_margin_property =
1463 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1465 dev->mode_config.tv_mode_property =
1466 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1468 for (i = 0; i < num_modes; i++)
1469 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1472 dev->mode_config.tv_brightness_property =
1473 drm_property_create_range(dev, 0, "brightness", 0, 100);
1475 dev->mode_config.tv_contrast_property =
1476 drm_property_create_range(dev, 0, "contrast", 0, 100);
1478 dev->mode_config.tv_flicker_reduction_property =
1479 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1481 dev->mode_config.tv_overscan_property =
1482 drm_property_create_range(dev, 0, "overscan", 0, 100);
1484 dev->mode_config.tv_saturation_property =
1485 drm_property_create_range(dev, 0, "saturation", 0, 100);
1487 dev->mode_config.tv_hue_property =
1488 drm_property_create_range(dev, 0, "hue", 0, 100);
1492 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1495 * drm_mode_create_scaling_mode_property - create scaling mode property
1498 * Called by a driver the first time it's needed, must be attached to desired
1501 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1503 struct drm_property *scaling_mode;
1505 if (dev->mode_config.scaling_mode_property)
1509 drm_property_create_enum(dev, 0, "scaling mode",
1510 drm_scaling_mode_enum_list,
1511 ARRAY_SIZE(drm_scaling_mode_enum_list));
1513 dev->mode_config.scaling_mode_property = scaling_mode;
1517 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1520 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1523 * Called by a driver the first time it's needed, must be attached to desired
1527 * Zero on success, negative errno on failure.
1529 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1531 if (dev->mode_config.aspect_ratio_property)
1534 dev->mode_config.aspect_ratio_property =
1535 drm_property_create_enum(dev, 0, "aspect ratio",
1536 drm_aspect_ratio_enum_list,
1537 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1539 if (dev->mode_config.aspect_ratio_property == NULL)
1544 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1547 * drm_mode_create_dirty_property - create dirty property
1550 * Called by a driver the first time it's needed, must be attached to desired
1553 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1555 struct drm_property *dirty_info;
1557 if (dev->mode_config.dirty_info_property)
1561 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1563 drm_dirty_info_enum_list,
1564 ARRAY_SIZE(drm_dirty_info_enum_list));
1565 dev->mode_config.dirty_info_property = dirty_info;
1569 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1572 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1575 * Create the the suggested x/y offset property for connectors.
1577 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1579 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1582 dev->mode_config.suggested_x_property =
1583 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1585 dev->mode_config.suggested_y_property =
1586 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1588 if (dev->mode_config.suggested_x_property == NULL ||
1589 dev->mode_config.suggested_y_property == NULL)
1593 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1595 static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1597 uint32_t total_objects = 0;
1599 total_objects += dev->mode_config.num_crtc;
1600 total_objects += dev->mode_config.num_connector;
1601 total_objects += dev->mode_config.num_encoder;
1602 total_objects += dev->mode_config.num_bridge;
1604 group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
1605 if (!group->id_list)
1608 group->num_crtcs = 0;
1609 group->num_connectors = 0;
1610 group->num_encoders = 0;
1611 group->num_bridges = 0;
1615 void drm_mode_group_destroy(struct drm_mode_group *group)
1617 kfree(group->id_list);
1618 group->id_list = NULL;
1622 * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
1623 * the drm core's responsibility to set up mode control groups.
1625 int drm_mode_group_init_legacy_group(struct drm_device *dev,
1626 struct drm_mode_group *group)
1628 struct drm_crtc *crtc;
1629 struct drm_encoder *encoder;
1630 struct drm_connector *connector;
1631 struct drm_bridge *bridge;
1634 ret = drm_mode_group_init(dev, group);
1638 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1639 group->id_list[group->num_crtcs++] = crtc->base.id;
1641 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1642 group->id_list[group->num_crtcs + group->num_encoders++] =
1645 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1646 group->id_list[group->num_crtcs + group->num_encoders +
1647 group->num_connectors++] = connector->base.id;
1649 list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
1650 group->id_list[group->num_crtcs + group->num_encoders +
1651 group->num_connectors + group->num_bridges++] =
1656 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
1658 void drm_reinit_primary_mode_group(struct drm_device *dev)
1660 drm_modeset_lock_all(dev);
1661 drm_mode_group_destroy(&dev->primary->mode_group);
1662 drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
1663 drm_modeset_unlock_all(dev);
1665 EXPORT_SYMBOL(drm_reinit_primary_mode_group);
1668 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1669 * @out: drm_mode_modeinfo struct to return to the user
1670 * @in: drm_display_mode to use
1672 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1675 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1676 const struct drm_display_mode *in)
1678 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1679 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1680 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1681 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1682 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1683 "timing values too large for mode info\n");
1685 out->clock = in->clock;
1686 out->hdisplay = in->hdisplay;
1687 out->hsync_start = in->hsync_start;
1688 out->hsync_end = in->hsync_end;
1689 out->htotal = in->htotal;
1690 out->hskew = in->hskew;
1691 out->vdisplay = in->vdisplay;
1692 out->vsync_start = in->vsync_start;
1693 out->vsync_end = in->vsync_end;
1694 out->vtotal = in->vtotal;
1695 out->vscan = in->vscan;
1696 out->vrefresh = in->vrefresh;
1697 out->flags = in->flags;
1698 out->type = in->type;
1699 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1700 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1704 * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
1705 * @out: drm_display_mode to return to the user
1706 * @in: drm_mode_modeinfo to use
1708 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1712 * Zero on success, negative errno on failure.
1714 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1715 const struct drm_mode_modeinfo *in)
1717 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1720 if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
1723 out->clock = in->clock;
1724 out->hdisplay = in->hdisplay;
1725 out->hsync_start = in->hsync_start;
1726 out->hsync_end = in->hsync_end;
1727 out->htotal = in->htotal;
1728 out->hskew = in->hskew;
1729 out->vdisplay = in->vdisplay;
1730 out->vsync_start = in->vsync_start;
1731 out->vsync_end = in->vsync_end;
1732 out->vtotal = in->vtotal;
1733 out->vscan = in->vscan;
1734 out->vrefresh = in->vrefresh;
1735 out->flags = in->flags;
1736 out->type = in->type;
1737 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1738 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1744 * drm_mode_getresources - get graphics configuration
1745 * @dev: drm device for the ioctl
1746 * @data: data pointer for the ioctl
1747 * @file_priv: drm file for the ioctl call
1749 * Construct a set of configuration description structures and return
1750 * them to the user, including CRTC, connector and framebuffer configuration.
1752 * Called by the user via ioctl.
1755 * Zero on success, negative errno on failure.
1757 int drm_mode_getresources(struct drm_device *dev, void *data,
1758 struct drm_file *file_priv)
1760 struct drm_mode_card_res *card_res = data;
1761 struct list_head *lh;
1762 struct drm_framebuffer *fb;
1763 struct drm_connector *connector;
1764 struct drm_crtc *crtc;
1765 struct drm_encoder *encoder;
1767 int connector_count = 0;
1770 int encoder_count = 0;
1772 uint32_t __user *fb_id;
1773 uint32_t __user *crtc_id;
1774 uint32_t __user *connector_id;
1775 uint32_t __user *encoder_id;
1776 struct drm_mode_group *mode_group;
1778 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1782 mutex_lock(&file_priv->fbs_lock);
1784 * For the non-control nodes we need to limit the list of resources
1785 * by IDs in the group list for this node
1787 list_for_each(lh, &file_priv->fbs)
1790 /* handle this in 4 parts */
1792 if (card_res->count_fbs >= fb_count) {
1794 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1795 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1796 if (put_user(fb->base.id, fb_id + copied)) {
1797 mutex_unlock(&file_priv->fbs_lock);
1803 card_res->count_fbs = fb_count;
1804 mutex_unlock(&file_priv->fbs_lock);
1806 /* mode_config.mutex protects the connector list against e.g. DP MST
1807 * connector hot-adding. CRTC/Plane lists are invariant. */
1808 mutex_lock(&dev->mode_config.mutex);
1809 if (!drm_is_primary_client(file_priv)) {
1812 list_for_each(lh, &dev->mode_config.crtc_list)
1815 list_for_each(lh, &dev->mode_config.connector_list)
1818 list_for_each(lh, &dev->mode_config.encoder_list)
1822 mode_group = &file_priv->master->minor->mode_group;
1823 crtc_count = mode_group->num_crtcs;
1824 connector_count = mode_group->num_connectors;
1825 encoder_count = mode_group->num_encoders;
1828 card_res->max_height = dev->mode_config.max_height;
1829 card_res->min_height = dev->mode_config.min_height;
1830 card_res->max_width = dev->mode_config.max_width;
1831 card_res->min_width = dev->mode_config.min_width;
1834 if (card_res->count_crtcs >= crtc_count) {
1836 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1838 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1840 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1841 if (put_user(crtc->base.id, crtc_id + copied)) {
1848 for (i = 0; i < mode_group->num_crtcs; i++) {
1849 if (put_user(mode_group->id_list[i],
1850 crtc_id + copied)) {
1858 card_res->count_crtcs = crtc_count;
1861 if (card_res->count_encoders >= encoder_count) {
1863 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1865 list_for_each_entry(encoder,
1866 &dev->mode_config.encoder_list,
1868 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1870 if (put_user(encoder->base.id, encoder_id +
1878 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1879 if (put_user(mode_group->id_list[i],
1880 encoder_id + copied)) {
1889 card_res->count_encoders = encoder_count;
1892 if (card_res->count_connectors >= connector_count) {
1894 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1896 list_for_each_entry(connector,
1897 &dev->mode_config.connector_list,
1899 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1902 if (put_user(connector->base.id,
1903 connector_id + copied)) {
1910 int start = mode_group->num_crtcs +
1911 mode_group->num_encoders;
1912 for (i = start; i < start + mode_group->num_connectors; i++) {
1913 if (put_user(mode_group->id_list[i],
1914 connector_id + copied)) {
1922 card_res->count_connectors = connector_count;
1924 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1925 card_res->count_connectors, card_res->count_encoders);
1928 mutex_unlock(&dev->mode_config.mutex);
1933 * drm_mode_getcrtc - get CRTC configuration
1934 * @dev: drm device for the ioctl
1935 * @data: data pointer for the ioctl
1936 * @file_priv: drm file for the ioctl call
1938 * Construct a CRTC configuration structure to return to the user.
1940 * Called by the user via ioctl.
1943 * Zero on success, negative errno on failure.
1945 int drm_mode_getcrtc(struct drm_device *dev,
1946 void *data, struct drm_file *file_priv)
1948 struct drm_mode_crtc *crtc_resp = data;
1949 struct drm_crtc *crtc;
1951 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1954 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1958 drm_modeset_lock_crtc(crtc, crtc->primary);
1959 crtc_resp->x = crtc->x;
1960 crtc_resp->y = crtc->y;
1961 crtc_resp->gamma_size = crtc->gamma_size;
1962 if (crtc->primary->fb)
1963 crtc_resp->fb_id = crtc->primary->fb->base.id;
1965 crtc_resp->fb_id = 0;
1967 if (crtc->enabled) {
1969 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1970 crtc_resp->mode_valid = 1;
1973 crtc_resp->mode_valid = 0;
1975 drm_modeset_unlock_crtc(crtc);
1980 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1981 const struct drm_file *file_priv)
1984 * If user-space hasn't configured the driver to expose the stereo 3D
1985 * modes, don't expose them.
1987 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1993 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1995 /* For atomic drivers only state objects are synchronously updated and
1996 * protected by modeset locks, so check those first. */
1997 if (connector->state)
1998 return connector->state->best_encoder;
1999 return connector->encoder;
2002 /* helper for getconnector and getproperties ioctls */
2003 static int get_properties(struct drm_mode_object *obj, bool atomic,
2004 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2005 uint32_t *arg_count_props)
2010 props_count = obj->properties->count;
2012 props_count -= obj->properties->atomic_count;
2014 if ((*arg_count_props >= props_count) && props_count) {
2015 for (i = 0, copied = 0; copied < props_count; i++) {
2016 struct drm_property *prop = obj->properties->properties[i];
2019 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2022 ret = drm_object_property_get_value(obj, prop, &val);
2026 if (put_user(prop->base.id, prop_ptr + copied))
2029 if (put_user(val, prop_values + copied))
2035 *arg_count_props = props_count;
2041 * drm_mode_getconnector - get connector configuration
2042 * @dev: drm device for the ioctl
2043 * @data: data pointer for the ioctl
2044 * @file_priv: drm file for the ioctl call
2046 * Construct a connector configuration structure to return to the user.
2048 * Called by the user via ioctl.
2051 * Zero on success, negative errno on failure.
2053 int drm_mode_getconnector(struct drm_device *dev, void *data,
2054 struct drm_file *file_priv)
2056 struct drm_mode_get_connector *out_resp = data;
2057 struct drm_connector *connector;
2058 struct drm_encoder *encoder;
2059 struct drm_display_mode *mode;
2061 int encoders_count = 0;
2065 struct drm_mode_modeinfo u_mode;
2066 struct drm_mode_modeinfo __user *mode_ptr;
2067 uint32_t __user *encoder_ptr;
2069 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2072 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2074 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
2076 mutex_lock(&dev->mode_config.mutex);
2077 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2079 connector = drm_connector_find(dev, out_resp->connector_id);
2085 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2086 if (connector->encoder_ids[i] != 0)
2089 if (out_resp->count_modes == 0) {
2090 connector->funcs->fill_modes(connector,
2091 dev->mode_config.max_width,
2092 dev->mode_config.max_height);
2095 /* delayed so we get modes regardless of pre-fill_modes state */
2096 list_for_each_entry(mode, &connector->modes, head)
2097 if (drm_mode_expose_to_userspace(mode, file_priv))
2100 out_resp->connector_id = connector->base.id;
2101 out_resp->connector_type = connector->connector_type;
2102 out_resp->connector_type_id = connector->connector_type_id;
2103 out_resp->mm_width = connector->display_info.width_mm;
2104 out_resp->mm_height = connector->display_info.height_mm;
2105 out_resp->subpixel = connector->display_info.subpixel_order;
2106 out_resp->connection = connector->status;
2107 encoder = drm_connector_get_encoder(connector);
2109 out_resp->encoder_id = encoder->base.id;
2111 out_resp->encoder_id = 0;
2114 * This ioctl is called twice, once to determine how much space is
2115 * needed, and the 2nd time to fill it.
2117 if ((out_resp->count_modes >= mode_count) && mode_count) {
2119 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2120 list_for_each_entry(mode, &connector->modes, head) {
2121 if (!drm_mode_expose_to_userspace(mode, file_priv))
2124 drm_crtc_convert_to_umode(&u_mode, mode);
2125 if (copy_to_user(mode_ptr + copied,
2126 &u_mode, sizeof(u_mode))) {
2133 out_resp->count_modes = mode_count;
2135 ret = get_properties(&connector->base, file_priv->atomic,
2136 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2137 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2138 &out_resp->count_props);
2142 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2144 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2145 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2146 if (connector->encoder_ids[i] != 0) {
2147 if (put_user(connector->encoder_ids[i],
2148 encoder_ptr + copied)) {
2156 out_resp->count_encoders = encoders_count;
2159 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2160 mutex_unlock(&dev->mode_config.mutex);
2165 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2167 struct drm_connector *connector;
2168 struct drm_device *dev = encoder->dev;
2169 bool uses_atomic = false;
2171 /* For atomic drivers only state objects are synchronously updated and
2172 * protected by modeset locks, so check those first. */
2173 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2174 if (!connector->state)
2179 if (connector->state->best_encoder != encoder)
2182 return connector->state->crtc;
2185 /* Don't return stale data (e.g. pending async disable). */
2189 return encoder->crtc;
2193 * drm_mode_getencoder - get encoder configuration
2194 * @dev: drm device for the ioctl
2195 * @data: data pointer for the ioctl
2196 * @file_priv: drm file for the ioctl call
2198 * Construct a encoder configuration structure to return to the user.
2200 * Called by the user via ioctl.
2203 * Zero on success, negative errno on failure.
2205 int drm_mode_getencoder(struct drm_device *dev, void *data,
2206 struct drm_file *file_priv)
2208 struct drm_mode_get_encoder *enc_resp = data;
2209 struct drm_encoder *encoder;
2210 struct drm_crtc *crtc;
2212 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2215 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2219 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2220 crtc = drm_encoder_get_crtc(encoder);
2222 enc_resp->crtc_id = crtc->base.id;
2223 else if (encoder->crtc)
2224 enc_resp->crtc_id = encoder->crtc->base.id;
2226 enc_resp->crtc_id = 0;
2227 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2229 enc_resp->encoder_type = encoder->encoder_type;
2230 enc_resp->encoder_id = encoder->base.id;
2231 enc_resp->possible_crtcs = encoder->possible_crtcs;
2232 enc_resp->possible_clones = encoder->possible_clones;
2238 * drm_mode_getplane_res - enumerate all plane resources
2241 * @file_priv: DRM file info
2243 * Construct a list of plane ids to return to the user.
2245 * Called by the user via ioctl.
2248 * Zero on success, negative errno on failure.
2250 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2251 struct drm_file *file_priv)
2253 struct drm_mode_get_plane_res *plane_resp = data;
2254 struct drm_mode_config *config;
2255 struct drm_plane *plane;
2256 uint32_t __user *plane_ptr;
2258 unsigned num_planes;
2260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2263 config = &dev->mode_config;
2265 if (file_priv->universal_planes)
2266 num_planes = config->num_total_plane;
2268 num_planes = config->num_overlay_plane;
2271 * This ioctl is called twice, once to determine how much space is
2272 * needed, and the 2nd time to fill it.
2275 (plane_resp->count_planes >= num_planes)) {
2276 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2278 /* Plane lists are invariant, no locking needed. */
2279 list_for_each_entry(plane, &config->plane_list, head) {
2281 * Unless userspace set the 'universal planes'
2282 * capability bit, only advertise overlays.
2284 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2285 !file_priv->universal_planes)
2288 if (put_user(plane->base.id, plane_ptr + copied))
2293 plane_resp->count_planes = num_planes;
2299 * drm_mode_getplane - get plane configuration
2302 * @file_priv: DRM file info
2304 * Construct a plane configuration structure to return to the user.
2306 * Called by the user via ioctl.
2309 * Zero on success, negative errno on failure.
2311 int drm_mode_getplane(struct drm_device *dev, void *data,
2312 struct drm_file *file_priv)
2314 struct drm_mode_get_plane *plane_resp = data;
2315 struct drm_plane *plane;
2316 uint32_t __user *format_ptr;
2318 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2321 plane = drm_plane_find(dev, plane_resp->plane_id);
2325 drm_modeset_lock(&plane->mutex, NULL);
2327 plane_resp->crtc_id = plane->crtc->base.id;
2329 plane_resp->crtc_id = 0;
2332 plane_resp->fb_id = plane->fb->base.id;
2334 plane_resp->fb_id = 0;
2335 drm_modeset_unlock(&plane->mutex);
2337 plane_resp->plane_id = plane->base.id;
2338 plane_resp->possible_crtcs = plane->possible_crtcs;
2339 plane_resp->gamma_size = 0;
2342 * This ioctl is called twice, once to determine how much space is
2343 * needed, and the 2nd time to fill it.
2345 if (plane->format_count &&
2346 (plane_resp->count_format_types >= plane->format_count)) {
2347 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2348 if (copy_to_user(format_ptr,
2349 plane->format_types,
2350 sizeof(uint32_t) * plane->format_count)) {
2354 plane_resp->count_format_types = plane->format_count;
2360 * setplane_internal - setplane handler for internal callers
2362 * Note that we assume an extra reference has already been taken on fb. If the
2363 * update fails, this reference will be dropped before return; if it succeeds,
2364 * the previous framebuffer (if any) will be unreferenced instead.
2366 * src_{x,y,w,h} are provided in 16.16 fixed point format
2368 static int __setplane_internal(struct drm_plane *plane,
2369 struct drm_crtc *crtc,
2370 struct drm_framebuffer *fb,
2371 int32_t crtc_x, int32_t crtc_y,
2372 uint32_t crtc_w, uint32_t crtc_h,
2373 /* src_{x,y,w,h} values are 16.16 fixed point */
2374 uint32_t src_x, uint32_t src_y,
2375 uint32_t src_w, uint32_t src_h)
2378 unsigned int fb_width, fb_height;
2381 /* No fb means shut it down */
2383 plane->old_fb = plane->fb;
2384 ret = plane->funcs->disable_plane(plane);
2389 plane->old_fb = NULL;
2394 /* Check whether this plane is usable on this CRTC */
2395 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2396 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2401 /* Check whether this plane supports the fb pixel format. */
2402 for (i = 0; i < plane->format_count; i++)
2403 if (fb->pixel_format == plane->format_types[i])
2405 if (i == plane->format_count) {
2406 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2407 drm_get_format_name(fb->pixel_format));
2412 fb_width = fb->width << 16;
2413 fb_height = fb->height << 16;
2415 /* Make sure source coordinates are inside the fb. */
2416 if (src_w > fb_width ||
2417 src_x > fb_width - src_w ||
2418 src_h > fb_height ||
2419 src_y > fb_height - src_h) {
2420 DRM_DEBUG_KMS("Invalid source coordinates "
2421 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2422 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2423 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2424 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2425 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2430 plane->old_fb = plane->fb;
2431 ret = plane->funcs->update_plane(plane, crtc, fb,
2432 crtc_x, crtc_y, crtc_w, crtc_h,
2433 src_x, src_y, src_w, src_h);
2439 plane->old_fb = NULL;
2444 drm_framebuffer_unreference(fb);
2446 drm_framebuffer_unreference(plane->old_fb);
2447 plane->old_fb = NULL;
2452 static int setplane_internal(struct drm_plane *plane,
2453 struct drm_crtc *crtc,
2454 struct drm_framebuffer *fb,
2455 int32_t crtc_x, int32_t crtc_y,
2456 uint32_t crtc_w, uint32_t crtc_h,
2457 /* src_{x,y,w,h} values are 16.16 fixed point */
2458 uint32_t src_x, uint32_t src_y,
2459 uint32_t src_w, uint32_t src_h)
2463 drm_modeset_lock_all(plane->dev);
2464 ret = __setplane_internal(plane, crtc, fb,
2465 crtc_x, crtc_y, crtc_w, crtc_h,
2466 src_x, src_y, src_w, src_h);
2467 drm_modeset_unlock_all(plane->dev);
2473 * drm_mode_setplane - configure a plane's configuration
2475 * @data: ioctl data*
2476 * @file_priv: DRM file info
2478 * Set plane configuration, including placement, fb, scaling, and other factors.
2479 * Or pass a NULL fb to disable (planes may be disabled without providing a
2483 * Zero on success, negative errno on failure.
2485 int drm_mode_setplane(struct drm_device *dev, void *data,
2486 struct drm_file *file_priv)
2488 struct drm_mode_set_plane *plane_req = data;
2489 struct drm_plane *plane;
2490 struct drm_crtc *crtc = NULL;
2491 struct drm_framebuffer *fb = NULL;
2493 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2496 /* Give drivers some help against integer overflows */
2497 if (plane_req->crtc_w > INT_MAX ||
2498 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
2499 plane_req->crtc_h > INT_MAX ||
2500 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
2501 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2502 plane_req->crtc_w, plane_req->crtc_h,
2503 plane_req->crtc_x, plane_req->crtc_y);
2508 * First, find the plane, crtc, and fb objects. If not available,
2509 * we don't bother to call the driver.
2511 plane = drm_plane_find(dev, plane_req->plane_id);
2513 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2514 plane_req->plane_id);
2518 if (plane_req->fb_id) {
2519 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2521 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2526 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2528 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2529 plane_req->crtc_id);
2535 * setplane_internal will take care of deref'ing either the old or new
2536 * framebuffer depending on success.
2538 return setplane_internal(plane, crtc, fb,
2539 plane_req->crtc_x, plane_req->crtc_y,
2540 plane_req->crtc_w, plane_req->crtc_h,
2541 plane_req->src_x, plane_req->src_y,
2542 plane_req->src_w, plane_req->src_h);
2546 * drm_mode_set_config_internal - helper to call ->set_config
2547 * @set: modeset config to set
2549 * This is a little helper to wrap internal calls to the ->set_config driver
2550 * interface. The only thing it adds is correct refcounting dance.
2553 * Zero on success, negative errno on failure.
2555 int drm_mode_set_config_internal(struct drm_mode_set *set)
2557 struct drm_crtc *crtc = set->crtc;
2558 struct drm_framebuffer *fb;
2559 struct drm_crtc *tmp;
2563 * NOTE: ->set_config can also disable other crtcs (if we steal all
2564 * connectors from it), hence we need to refcount the fbs across all
2565 * crtcs. Atomic modeset will have saner semantics ...
2567 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
2568 tmp->primary->old_fb = tmp->primary->fb;
2572 ret = crtc->funcs->set_config(set);
2574 crtc->primary->crtc = crtc;
2575 crtc->primary->fb = fb;
2578 list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
2579 if (tmp->primary->fb)
2580 drm_framebuffer_reference(tmp->primary->fb);
2581 if (tmp->primary->old_fb)
2582 drm_framebuffer_unreference(tmp->primary->old_fb);
2583 tmp->primary->old_fb = NULL;
2588 EXPORT_SYMBOL(drm_mode_set_config_internal);
2591 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2593 * @crtc: CRTC that framebuffer will be displayed on
2596 * @mode: mode that framebuffer will be displayed under
2597 * @fb: framebuffer to check size of
2599 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2601 const struct drm_display_mode *mode,
2602 const struct drm_framebuffer *fb)
2605 int hdisplay, vdisplay;
2607 hdisplay = mode->hdisplay;
2608 vdisplay = mode->vdisplay;
2610 if (drm_mode_is_stereo(mode)) {
2611 struct drm_display_mode adjusted = *mode;
2613 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
2614 hdisplay = adjusted.crtc_hdisplay;
2615 vdisplay = adjusted.crtc_vdisplay;
2618 if (crtc->invert_dimensions)
2619 swap(hdisplay, vdisplay);
2621 if (hdisplay > fb->width ||
2622 vdisplay > fb->height ||
2623 x > fb->width - hdisplay ||
2624 y > fb->height - vdisplay) {
2625 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2626 fb->width, fb->height, hdisplay, vdisplay, x, y,
2627 crtc->invert_dimensions ? " (inverted)" : "");
2633 EXPORT_SYMBOL(drm_crtc_check_viewport);
2636 * drm_mode_setcrtc - set CRTC configuration
2637 * @dev: drm device for the ioctl
2638 * @data: data pointer for the ioctl
2639 * @file_priv: drm file for the ioctl call
2641 * Build a new CRTC configuration based on user request.
2643 * Called by the user via ioctl.
2646 * Zero on success, negative errno on failure.
2648 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2649 struct drm_file *file_priv)
2651 struct drm_mode_config *config = &dev->mode_config;
2652 struct drm_mode_crtc *crtc_req = data;
2653 struct drm_crtc *crtc;
2654 struct drm_connector **connector_set = NULL, *connector;
2655 struct drm_framebuffer *fb = NULL;
2656 struct drm_display_mode *mode = NULL;
2657 struct drm_mode_set set;
2658 uint32_t __user *set_connectors_ptr;
2662 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2665 /* For some reason crtc x/y offsets are signed internally. */
2666 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2669 drm_modeset_lock_all(dev);
2670 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2672 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2676 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
2678 if (crtc_req->mode_valid) {
2679 /* If we have a mode we need a framebuffer. */
2680 /* If we pass -1, set the mode with the currently bound fb */
2681 if (crtc_req->fb_id == -1) {
2682 if (!crtc->primary->fb) {
2683 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2687 fb = crtc->primary->fb;
2688 /* Make refcounting symmetric with the lookup path. */
2689 drm_framebuffer_reference(fb);
2691 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2693 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2700 mode = drm_mode_create(dev);
2706 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2708 DRM_DEBUG_KMS("Invalid mode\n");
2712 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
2714 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2721 if (crtc_req->count_connectors == 0 && mode) {
2722 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2727 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2728 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2729 crtc_req->count_connectors);
2734 if (crtc_req->count_connectors > 0) {
2737 /* Avoid unbounded kernel memory allocation */
2738 if (crtc_req->count_connectors > config->num_connector) {
2743 connector_set = kmalloc_array(crtc_req->count_connectors,
2744 sizeof(struct drm_connector *),
2746 if (!connector_set) {
2751 for (i = 0; i < crtc_req->count_connectors; i++) {
2752 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2753 if (get_user(out_id, &set_connectors_ptr[i])) {
2758 connector = drm_connector_find(dev, out_id);
2760 DRM_DEBUG_KMS("Connector id %d unknown\n",
2765 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2769 connector_set[i] = connector;
2774 set.x = crtc_req->x;
2775 set.y = crtc_req->y;
2777 set.connectors = connector_set;
2778 set.num_connectors = crtc_req->count_connectors;
2780 ret = drm_mode_set_config_internal(&set);
2784 drm_framebuffer_unreference(fb);
2786 kfree(connector_set);
2787 drm_mode_destroy(dev, mode);
2788 drm_modeset_unlock_all(dev);
2793 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2794 * universal plane handler call
2795 * @crtc: crtc to update cursor for
2796 * @req: data pointer for the ioctl
2797 * @file_priv: drm file for the ioctl call
2799 * Legacy cursor ioctl's work directly with driver buffer handles. To
2800 * translate legacy ioctl calls into universal plane handler calls, we need to
2801 * wrap the native buffer handle in a drm_framebuffer.
2803 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2804 * buffer with a pitch of 4*width; the universal plane interface should be used
2805 * directly in cases where the hardware can support other buffer settings and
2806 * userspace wants to make use of these capabilities.
2809 * Zero on success, negative errno on failure.
2811 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2812 struct drm_mode_cursor2 *req,
2813 struct drm_file *file_priv)
2815 struct drm_device *dev = crtc->dev;
2816 struct drm_framebuffer *fb = NULL;
2817 struct drm_mode_fb_cmd2 fbreq = {
2818 .width = req->width,
2819 .height = req->height,
2820 .pixel_format = DRM_FORMAT_ARGB8888,
2821 .pitches = { req->width * 4 },
2822 .handles = { req->handle },
2824 int32_t crtc_x, crtc_y;
2825 uint32_t crtc_w = 0, crtc_h = 0;
2826 uint32_t src_w = 0, src_h = 0;
2829 BUG_ON(!crtc->cursor);
2830 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2833 * Obtain fb we'll be using (either new or existing) and take an extra
2834 * reference to it if fb != null. setplane will take care of dropping
2835 * the reference if the plane update fails.
2837 if (req->flags & DRM_MODE_CURSOR_BO) {
2839 fb = add_framebuffer_internal(dev, &fbreq, file_priv);
2841 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2845 drm_framebuffer_reference(fb);
2850 fb = crtc->cursor->fb;
2852 drm_framebuffer_reference(fb);
2855 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2859 crtc_x = crtc->cursor_x;
2860 crtc_y = crtc->cursor_y;
2865 crtc_h = fb->height;
2866 src_w = fb->width << 16;
2867 src_h = fb->height << 16;
2871 * setplane_internal will take care of deref'ing either the old or new
2872 * framebuffer depending on success.
2874 ret = __setplane_internal(crtc->cursor, crtc, fb,
2875 crtc_x, crtc_y, crtc_w, crtc_h,
2876 0, 0, src_w, src_h);
2878 /* Update successful; save new cursor position, if necessary */
2879 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2880 crtc->cursor_x = req->x;
2881 crtc->cursor_y = req->y;
2887 static int drm_mode_cursor_common(struct drm_device *dev,
2888 struct drm_mode_cursor2 *req,
2889 struct drm_file *file_priv)
2891 struct drm_crtc *crtc;
2894 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2897 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2900 crtc = drm_crtc_find(dev, req->crtc_id);
2902 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2907 * If this crtc has a universal cursor plane, call that plane's update
2908 * handler rather than using legacy cursor handlers.
2910 drm_modeset_lock_crtc(crtc, crtc->cursor);
2912 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2916 if (req->flags & DRM_MODE_CURSOR_BO) {
2917 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2921 /* Turns off the cursor if handle is 0 */
2922 if (crtc->funcs->cursor_set2)
2923 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2924 req->width, req->height, req->hot_x, req->hot_y);
2926 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2927 req->width, req->height);
2930 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2931 if (crtc->funcs->cursor_move) {
2932 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2939 drm_modeset_unlock_crtc(crtc);
2947 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2948 * @dev: drm device for the ioctl
2949 * @data: data pointer for the ioctl
2950 * @file_priv: drm file for the ioctl call
2952 * Set the cursor configuration based on user request.
2954 * Called by the user via ioctl.
2957 * Zero on success, negative errno on failure.
2959 int drm_mode_cursor_ioctl(struct drm_device *dev,
2960 void *data, struct drm_file *file_priv)
2962 struct drm_mode_cursor *req = data;
2963 struct drm_mode_cursor2 new_req;
2965 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2966 new_req.hot_x = new_req.hot_y = 0;
2968 return drm_mode_cursor_common(dev, &new_req, file_priv);
2972 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2973 * @dev: drm device for the ioctl
2974 * @data: data pointer for the ioctl
2975 * @file_priv: drm file for the ioctl call
2977 * Set the cursor configuration based on user request. This implements the 2nd
2978 * version of the cursor ioctl, which allows userspace to additionally specify
2979 * the hotspot of the pointer.
2981 * Called by the user via ioctl.
2984 * Zero on success, negative errno on failure.
2986 int drm_mode_cursor2_ioctl(struct drm_device *dev,
2987 void *data, struct drm_file *file_priv)
2989 struct drm_mode_cursor2 *req = data;
2991 return drm_mode_cursor_common(dev, req, file_priv);
2995 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2996 * @bpp: bits per pixels
2997 * @depth: bit depth per pixel
2999 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3000 * Useful in fbdev emulation code, since that deals in those values.
3002 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3008 fmt = DRM_FORMAT_C8;
3012 fmt = DRM_FORMAT_XRGB1555;
3014 fmt = DRM_FORMAT_RGB565;
3017 fmt = DRM_FORMAT_RGB888;
3021 fmt = DRM_FORMAT_XRGB8888;
3022 else if (depth == 30)
3023 fmt = DRM_FORMAT_XRGB2101010;
3025 fmt = DRM_FORMAT_ARGB8888;
3028 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3029 fmt = DRM_FORMAT_XRGB8888;
3035 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3038 * drm_mode_addfb - add an FB to the graphics configuration
3039 * @dev: drm device for the ioctl
3040 * @data: data pointer for the ioctl
3041 * @file_priv: drm file for the ioctl call
3043 * Add a new FB to the specified CRTC, given a user request. This is the
3044 * original addfb ioctl which only supported RGB formats.
3046 * Called by the user via ioctl.
3049 * Zero on success, negative errno on failure.
3051 int drm_mode_addfb(struct drm_device *dev,
3052 void *data, struct drm_file *file_priv)
3054 struct drm_mode_fb_cmd *or = data;
3055 struct drm_mode_fb_cmd2 r = {};
3058 /* convert to new format and call new ioctl */
3059 r.fb_id = or->fb_id;
3060 r.width = or->width;
3061 r.height = or->height;
3062 r.pitches[0] = or->pitch;
3063 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3064 r.handles[0] = or->handle;
3066 ret = drm_mode_addfb2(dev, &r, file_priv);
3070 or->fb_id = r.fb_id;
3075 static int format_check(const struct drm_mode_fb_cmd2 *r)
3077 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3081 case DRM_FORMAT_RGB332:
3082 case DRM_FORMAT_BGR233:
3083 case DRM_FORMAT_XRGB4444:
3084 case DRM_FORMAT_XBGR4444:
3085 case DRM_FORMAT_RGBX4444:
3086 case DRM_FORMAT_BGRX4444:
3087 case DRM_FORMAT_ARGB4444:
3088 case DRM_FORMAT_ABGR4444:
3089 case DRM_FORMAT_RGBA4444:
3090 case DRM_FORMAT_BGRA4444:
3091 case DRM_FORMAT_XRGB1555:
3092 case DRM_FORMAT_XBGR1555:
3093 case DRM_FORMAT_RGBX5551:
3094 case DRM_FORMAT_BGRX5551:
3095 case DRM_FORMAT_ARGB1555:
3096 case DRM_FORMAT_ABGR1555:
3097 case DRM_FORMAT_RGBA5551:
3098 case DRM_FORMAT_BGRA5551:
3099 case DRM_FORMAT_RGB565:
3100 case DRM_FORMAT_BGR565:
3101 case DRM_FORMAT_RGB888:
3102 case DRM_FORMAT_BGR888:
3103 case DRM_FORMAT_XRGB8888:
3104 case DRM_FORMAT_XBGR8888:
3105 case DRM_FORMAT_RGBX8888:
3106 case DRM_FORMAT_BGRX8888:
3107 case DRM_FORMAT_ARGB8888:
3108 case DRM_FORMAT_ABGR8888:
3109 case DRM_FORMAT_RGBA8888:
3110 case DRM_FORMAT_BGRA8888:
3111 case DRM_FORMAT_XRGB2101010:
3112 case DRM_FORMAT_XBGR2101010:
3113 case DRM_FORMAT_RGBX1010102:
3114 case DRM_FORMAT_BGRX1010102:
3115 case DRM_FORMAT_ARGB2101010:
3116 case DRM_FORMAT_ABGR2101010:
3117 case DRM_FORMAT_RGBA1010102:
3118 case DRM_FORMAT_BGRA1010102:
3119 case DRM_FORMAT_YUYV:
3120 case DRM_FORMAT_YVYU:
3121 case DRM_FORMAT_UYVY:
3122 case DRM_FORMAT_VYUY:
3123 case DRM_FORMAT_AYUV:
3124 case DRM_FORMAT_NV12:
3125 case DRM_FORMAT_NV21:
3126 case DRM_FORMAT_NV16:
3127 case DRM_FORMAT_NV61:
3128 case DRM_FORMAT_NV24:
3129 case DRM_FORMAT_NV42:
3130 case DRM_FORMAT_YUV410:
3131 case DRM_FORMAT_YVU410:
3132 case DRM_FORMAT_YUV411:
3133 case DRM_FORMAT_YVU411:
3134 case DRM_FORMAT_YUV420:
3135 case DRM_FORMAT_YVU420:
3136 case DRM_FORMAT_YUV422:
3137 case DRM_FORMAT_YVU422:
3138 case DRM_FORMAT_YUV444:
3139 case DRM_FORMAT_YVU444:
3142 DRM_DEBUG_KMS("invalid pixel format %s\n",
3143 drm_get_format_name(r->pixel_format));
3148 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3150 int ret, hsub, vsub, num_planes, i;
3152 ret = format_check(r);
3154 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3155 drm_get_format_name(r->pixel_format));
3159 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3160 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3161 num_planes = drm_format_num_planes(r->pixel_format);
3163 if (r->width == 0 || r->width % hsub) {
3164 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3168 if (r->height == 0 || r->height % vsub) {
3169 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3173 for (i = 0; i < num_planes; i++) {
3174 unsigned int width = r->width / (i != 0 ? hsub : 1);
3175 unsigned int height = r->height / (i != 0 ? vsub : 1);
3176 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3178 if (!r->handles[i]) {
3179 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3183 if ((uint64_t) width * cpp > UINT_MAX)
3186 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3189 if (r->pitches[i] < width * cpp) {
3190 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3198 static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
3199 struct drm_mode_fb_cmd2 *r,
3200 struct drm_file *file_priv)
3202 struct drm_mode_config *config = &dev->mode_config;
3203 struct drm_framebuffer *fb;
3206 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
3207 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3208 return ERR_PTR(-EINVAL);
3211 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3212 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3213 r->width, config->min_width, config->max_width);
3214 return ERR_PTR(-EINVAL);
3216 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3217 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3218 r->height, config->min_height, config->max_height);
3219 return ERR_PTR(-EINVAL);
3222 ret = framebuffer_check(r);
3224 return ERR_PTR(ret);
3226 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3228 DRM_DEBUG_KMS("could not create framebuffer\n");
3232 mutex_lock(&file_priv->fbs_lock);
3233 r->fb_id = fb->base.id;
3234 list_add(&fb->filp_head, &file_priv->fbs);
3235 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3236 mutex_unlock(&file_priv->fbs_lock);
3242 * drm_mode_addfb2 - add an FB to the graphics configuration
3243 * @dev: drm device for the ioctl
3244 * @data: data pointer for the ioctl
3245 * @file_priv: drm file for the ioctl call
3247 * Add a new FB to the specified CRTC, given a user request with format. This is
3248 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3249 * and uses fourcc codes as pixel format specifiers.
3251 * Called by the user via ioctl.
3254 * Zero on success, negative errno on failure.
3256 int drm_mode_addfb2(struct drm_device *dev,
3257 void *data, struct drm_file *file_priv)
3259 struct drm_framebuffer *fb;
3261 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3264 fb = add_framebuffer_internal(dev, data, file_priv);
3272 * drm_mode_rmfb - remove an FB from the configuration
3273 * @dev: drm device for the ioctl
3274 * @data: data pointer for the ioctl
3275 * @file_priv: drm file for the ioctl call
3277 * Remove the FB specified by the user.
3279 * Called by the user via ioctl.
3282 * Zero on success, negative errno on failure.
3284 int drm_mode_rmfb(struct drm_device *dev,
3285 void *data, struct drm_file *file_priv)
3287 struct drm_framebuffer *fb = NULL;
3288 struct drm_framebuffer *fbl = NULL;
3289 uint32_t *id = data;
3292 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3295 mutex_lock(&file_priv->fbs_lock);
3296 mutex_lock(&dev->mode_config.fb_lock);
3297 fb = __drm_framebuffer_lookup(dev, *id);
3301 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3307 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3308 __drm_framebuffer_unregister(dev, fb);
3310 list_del_init(&fb->filp_head);
3311 mutex_unlock(&dev->mode_config.fb_lock);
3312 mutex_unlock(&file_priv->fbs_lock);
3314 drm_framebuffer_remove(fb);
3319 mutex_unlock(&dev->mode_config.fb_lock);
3320 mutex_unlock(&file_priv->fbs_lock);
3326 * drm_mode_getfb - get FB info
3327 * @dev: drm device for the ioctl
3328 * @data: data pointer for the ioctl
3329 * @file_priv: drm file for the ioctl call
3331 * Lookup the FB given its ID and return info about it.
3333 * Called by the user via ioctl.
3336 * Zero on success, negative errno on failure.
3338 int drm_mode_getfb(struct drm_device *dev,
3339 void *data, struct drm_file *file_priv)
3341 struct drm_mode_fb_cmd *r = data;
3342 struct drm_framebuffer *fb;
3345 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3348 fb = drm_framebuffer_lookup(dev, r->fb_id);
3352 r->height = fb->height;
3353 r->width = fb->width;
3354 r->depth = fb->depth;
3355 r->bpp = fb->bits_per_pixel;
3356 r->pitch = fb->pitches[0];
3357 if (fb->funcs->create_handle) {
3358 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3359 drm_is_control_client(file_priv)) {
3360 ret = fb->funcs->create_handle(fb, file_priv,
3363 /* GET_FB() is an unprivileged ioctl so we must not
3364 * return a buffer-handle to non-master processes! For
3365 * backwards-compatibility reasons, we cannot make
3366 * GET_FB() privileged, so just return an invalid handle
3367 * for non-masters. */
3375 drm_framebuffer_unreference(fb);
3381 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3382 * @dev: drm device for the ioctl
3383 * @data: data pointer for the ioctl
3384 * @file_priv: drm file for the ioctl call
3386 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3387 * rectangle list. Generic userspace which does frontbuffer rendering must call
3388 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3389 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3391 * Modesetting drivers which always update the frontbuffer do not need to
3392 * implement the corresponding ->dirty framebuffer callback.
3394 * Called by the user via ioctl.
3397 * Zero on success, negative errno on failure.
3399 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3400 void *data, struct drm_file *file_priv)
3402 struct drm_clip_rect __user *clips_ptr;
3403 struct drm_clip_rect *clips = NULL;
3404 struct drm_mode_fb_dirty_cmd *r = data;
3405 struct drm_framebuffer *fb;
3410 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3413 fb = drm_framebuffer_lookup(dev, r->fb_id);
3417 num_clips = r->num_clips;
3418 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3420 if (!num_clips != !clips_ptr) {
3425 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3427 /* If userspace annotates copy, clips must come in pairs */
3428 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3433 if (num_clips && clips_ptr) {
3434 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3438 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3444 ret = copy_from_user(clips, clips_ptr,
3445 num_clips * sizeof(*clips));
3452 if (fb->funcs->dirty) {
3453 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3462 drm_framebuffer_unreference(fb);
3469 * drm_fb_release - remove and free the FBs on this file
3470 * @priv: drm file for the ioctl
3472 * Destroy all the FBs associated with @filp.
3474 * Called by the user via ioctl.
3477 * Zero on success, negative errno on failure.
3479 void drm_fb_release(struct drm_file *priv)
3481 struct drm_device *dev = priv->minor->dev;
3482 struct drm_framebuffer *fb, *tfb;
3485 * When the file gets released that means no one else can access the fb
3486 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3487 * avoid upsetting lockdep since the universal cursor code adds a
3488 * framebuffer while holding mutex locks.
3490 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3491 * locks is impossible here since no one else but this function can get
3494 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3496 mutex_lock(&dev->mode_config.fb_lock);
3497 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
3498 __drm_framebuffer_unregister(dev, fb);
3499 mutex_unlock(&dev->mode_config.fb_lock);
3501 list_del_init(&fb->filp_head);
3503 /* This will also drop the fpriv->fbs reference. */
3504 drm_framebuffer_remove(fb);
3509 * drm_property_create - create a new property type
3511 * @flags: flags specifying the property type
3512 * @name: name of the property
3513 * @num_values: number of pre-defined values
3515 * This creates a new generic drm property which can then be attached to a drm
3516 * object with drm_object_attach_property. The returned property object must be
3517 * freed with drm_property_destroy.
3519 * Note that the DRM core keeps a per-device list of properties and that, if
3520 * drm_mode_config_cleanup() is called, it will destroy all properties created
3524 * A pointer to the newly created property on success, NULL on failure.
3526 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3527 const char *name, int num_values)
3529 struct drm_property *property = NULL;
3532 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3536 property->dev = dev;
3539 property->values = kcalloc(num_values, sizeof(uint64_t),
3541 if (!property->values)
3545 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3549 property->flags = flags;
3550 property->num_values = num_values;
3551 INIT_LIST_HEAD(&property->enum_list);
3554 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3555 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3558 list_add_tail(&property->head, &dev->mode_config.property_list);
3560 WARN_ON(!drm_property_type_valid(property));
3564 kfree(property->values);
3568 EXPORT_SYMBOL(drm_property_create);
3571 * drm_property_create_enum - create a new enumeration property type
3573 * @flags: flags specifying the property type
3574 * @name: name of the property
3575 * @props: enumeration lists with property values
3576 * @num_values: number of pre-defined values
3578 * This creates a new generic drm property which can then be attached to a drm
3579 * object with drm_object_attach_property. The returned property object must be
3580 * freed with drm_property_destroy.
3582 * Userspace is only allowed to set one of the predefined values for enumeration
3586 * A pointer to the newly created property on success, NULL on failure.
3588 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3590 const struct drm_prop_enum_list *props,
3593 struct drm_property *property;
3596 flags |= DRM_MODE_PROP_ENUM;
3598 property = drm_property_create(dev, flags, name, num_values);
3602 for (i = 0; i < num_values; i++) {
3603 ret = drm_property_add_enum(property, i,
3607 drm_property_destroy(dev, property);
3614 EXPORT_SYMBOL(drm_property_create_enum);
3617 * drm_property_create_bitmask - create a new bitmask property type
3619 * @flags: flags specifying the property type
3620 * @name: name of the property
3621 * @props: enumeration lists with property bitflags
3622 * @num_props: size of the @props array
3623 * @supported_bits: bitmask of all supported enumeration values
3625 * This creates a new bitmask drm property which can then be attached to a drm
3626 * object with drm_object_attach_property. The returned property object must be
3627 * freed with drm_property_destroy.
3629 * Compared to plain enumeration properties userspace is allowed to set any
3630 * or'ed together combination of the predefined property bitflag values
3633 * A pointer to the newly created property on success, NULL on failure.
3635 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3636 int flags, const char *name,
3637 const struct drm_prop_enum_list *props,
3639 uint64_t supported_bits)
3641 struct drm_property *property;
3642 int i, ret, index = 0;
3643 int num_values = hweight64(supported_bits);
3645 flags |= DRM_MODE_PROP_BITMASK;
3647 property = drm_property_create(dev, flags, name, num_values);
3650 for (i = 0; i < num_props; i++) {
3651 if (!(supported_bits & (1ULL << props[i].type)))
3654 if (WARN_ON(index >= num_values)) {
3655 drm_property_destroy(dev, property);
3659 ret = drm_property_add_enum(property, index++,
3663 drm_property_destroy(dev, property);
3670 EXPORT_SYMBOL(drm_property_create_bitmask);
3672 static struct drm_property *property_create_range(struct drm_device *dev,
3673 int flags, const char *name,
3674 uint64_t min, uint64_t max)
3676 struct drm_property *property;
3678 property = drm_property_create(dev, flags, name, 2);
3682 property->values[0] = min;
3683 property->values[1] = max;
3689 * drm_property_create_range - create a new ranged property type
3691 * @flags: flags specifying the property type
3692 * @name: name of the property
3693 * @min: minimum value of the property
3694 * @max: maximum value of the property
3696 * This creates a new generic drm property which can then be attached to a drm
3697 * object with drm_object_attach_property. The returned property object must be
3698 * freed with drm_property_destroy.
3700 * Userspace is allowed to set any integer value in the (min, max) range
3704 * A pointer to the newly created property on success, NULL on failure.
3706 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3708 uint64_t min, uint64_t max)
3710 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3713 EXPORT_SYMBOL(drm_property_create_range);
3715 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3716 int flags, const char *name,
3717 int64_t min, int64_t max)
3719 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3720 name, I642U64(min), I642U64(max));
3722 EXPORT_SYMBOL(drm_property_create_signed_range);
3724 struct drm_property *drm_property_create_object(struct drm_device *dev,
3725 int flags, const char *name, uint32_t type)
3727 struct drm_property *property;
3729 flags |= DRM_MODE_PROP_OBJECT;
3731 property = drm_property_create(dev, flags, name, 1);
3735 property->values[0] = type;
3739 EXPORT_SYMBOL(drm_property_create_object);
3742 * drm_property_add_enum - add a possible value to an enumeration property
3743 * @property: enumeration property to change
3744 * @index: index of the new enumeration
3745 * @value: value of the new enumeration
3746 * @name: symbolic name of the new enumeration
3748 * This functions adds enumerations to a property.
3750 * It's use is deprecated, drivers should use one of the more specific helpers
3751 * to directly create the property with all enumerations already attached.
3754 * Zero on success, error code on failure.
3756 int drm_property_add_enum(struct drm_property *property, int index,
3757 uint64_t value, const char *name)
3759 struct drm_property_enum *prop_enum;
3761 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3762 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3766 * Bitmask enum properties have the additional constraint of values
3769 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3773 if (!list_empty(&property->enum_list)) {
3774 list_for_each_entry(prop_enum, &property->enum_list, head) {
3775 if (prop_enum->value == value) {
3776 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3777 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3783 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3787 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3788 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3789 prop_enum->value = value;
3791 property->values[index] = value;
3792 list_add_tail(&prop_enum->head, &property->enum_list);
3795 EXPORT_SYMBOL(drm_property_add_enum);
3798 * drm_property_destroy - destroy a drm property
3800 * @property: property to destry
3802 * This function frees a property including any attached resources like
3803 * enumeration values.
3805 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3807 struct drm_property_enum *prop_enum, *pt;
3809 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
3810 list_del(&prop_enum->head);
3814 if (property->num_values)
3815 kfree(property->values);
3816 drm_mode_object_put(dev, &property->base);
3817 list_del(&property->head);
3820 EXPORT_SYMBOL(drm_property_destroy);
3823 * drm_object_attach_property - attach a property to a modeset object
3824 * @obj: drm modeset object
3825 * @property: property to attach
3826 * @init_val: initial value of the property
3828 * This attaches the given property to the modeset object with the given initial
3829 * value. Currently this function cannot fail since the properties are stored in
3830 * a statically sized array.
3832 void drm_object_attach_property(struct drm_mode_object *obj,
3833 struct drm_property *property,
3836 int count = obj->properties->count;
3838 if (count == DRM_OBJECT_MAX_PROPERTY) {
3839 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3840 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3841 "you see this message on the same object type.\n",
3846 obj->properties->properties[count] = property;
3847 obj->properties->values[count] = init_val;
3848 obj->properties->count++;
3849 if (property->flags & DRM_MODE_PROP_ATOMIC)
3850 obj->properties->atomic_count++;
3852 EXPORT_SYMBOL(drm_object_attach_property);
3855 * drm_object_property_set_value - set the value of a property
3856 * @obj: drm mode object to set property value for
3857 * @property: property to set
3858 * @val: value the property should be set to
3860 * This functions sets a given property on a given object. This function only
3861 * changes the software state of the property, it does not call into the
3862 * driver's ->set_property callback.
3865 * Zero on success, error code on failure.
3867 int drm_object_property_set_value(struct drm_mode_object *obj,
3868 struct drm_property *property, uint64_t val)
3872 for (i = 0; i < obj->properties->count; i++) {
3873 if (obj->properties->properties[i] == property) {
3874 obj->properties->values[i] = val;
3881 EXPORT_SYMBOL(drm_object_property_set_value);
3884 * drm_object_property_get_value - retrieve the value of a property
3885 * @obj: drm mode object to get property value from
3886 * @property: property to retrieve
3887 * @val: storage for the property value
3889 * This function retrieves the softare state of the given property for the given
3890 * property. Since there is no driver callback to retrieve the current property
3891 * value this might be out of sync with the hardware, depending upon the driver
3895 * Zero on success, error code on failure.
3897 int drm_object_property_get_value(struct drm_mode_object *obj,
3898 struct drm_property *property, uint64_t *val)
3902 /* read-only properties bypass atomic mechanism and still store
3903 * their value in obj->properties->values[].. mostly to avoid
3904 * having to deal w/ EDID and similar props in atomic paths:
3906 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
3907 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
3908 return drm_atomic_get_property(obj, property, val);
3910 for (i = 0; i < obj->properties->count; i++) {
3911 if (obj->properties->properties[i] == property) {
3912 *val = obj->properties->values[i];
3919 EXPORT_SYMBOL(drm_object_property_get_value);
3922 * drm_mode_getproperty_ioctl - get the property metadata
3925 * @file_priv: DRM file info
3927 * This function retrieves the metadata for a given property, like the different
3928 * possible values for an enum property or the limits for a range property.
3930 * Blob properties are special
3932 * Called by the user via ioctl.
3935 * Zero on success, negative errno on failure.
3937 int drm_mode_getproperty_ioctl(struct drm_device *dev,
3938 void *data, struct drm_file *file_priv)
3940 struct drm_mode_get_property *out_resp = data;
3941 struct drm_property *property;
3943 int value_count = 0;
3946 struct drm_property_enum *prop_enum;
3947 struct drm_mode_property_enum __user *enum_ptr;
3948 uint64_t __user *values_ptr;
3950 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3953 drm_modeset_lock_all(dev);
3954 property = drm_property_find(dev, out_resp->prop_id);
3960 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3961 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3962 list_for_each_entry(prop_enum, &property->enum_list, head)
3966 value_count = property->num_values;
3968 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3969 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3970 out_resp->flags = property->flags;
3972 if ((out_resp->count_values >= value_count) && value_count) {
3973 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
3974 for (i = 0; i < value_count; i++) {
3975 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3981 out_resp->count_values = value_count;
3983 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3984 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
3985 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3987 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
3988 list_for_each_entry(prop_enum, &property->enum_list, head) {
3990 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3995 if (copy_to_user(&enum_ptr[copied].name,
3996 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4003 out_resp->count_enum_blobs = enum_count;
4007 * NOTE: The idea seems to have been to use this to read all the blob
4008 * property values. But nothing ever added them to the corresponding
4009 * list, userspace always used the special-purpose get_blob ioctl to
4010 * read the value for a blob property. It also doesn't make a lot of
4011 * sense to return values here when everything else is just metadata for
4012 * the property itself.
4014 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4015 out_resp->count_enum_blobs = 0;
4017 drm_modeset_unlock_all(dev);
4021 static struct drm_property_blob *
4022 drm_property_create_blob(struct drm_device *dev, size_t length,
4025 struct drm_property_blob *blob;
4028 if (!length || !data)
4031 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4035 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
4041 blob->length = length;
4043 memcpy(blob->data, data, length);
4045 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
4049 static void drm_property_destroy_blob(struct drm_device *dev,
4050 struct drm_property_blob *blob)
4052 drm_mode_object_put(dev, &blob->base);
4053 list_del(&blob->head);
4058 * drm_mode_getblob_ioctl - get the contents of a blob property value
4061 * @file_priv: DRM file info
4063 * This function retrieves the contents of a blob property. The value stored in
4064 * an object's blob property is just a normal modeset object id.
4066 * Called by the user via ioctl.
4069 * Zero on success, negative errno on failure.
4071 int drm_mode_getblob_ioctl(struct drm_device *dev,
4072 void *data, struct drm_file *file_priv)
4074 struct drm_mode_get_blob *out_resp = data;
4075 struct drm_property_blob *blob;
4077 void __user *blob_ptr;
4079 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4082 drm_modeset_lock_all(dev);
4083 blob = drm_property_blob_find(dev, out_resp->blob_id);
4089 if (out_resp->length == blob->length) {
4090 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4091 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4096 out_resp->length = blob->length;
4099 drm_modeset_unlock_all(dev);
4104 * drm_mode_connector_set_path_property - set tile property on connector
4105 * @connector: connector to set property on.
4106 * @path: path to use for property.
4108 * This creates a property to expose to userspace to specify a
4109 * connector path. This is mainly used for DisplayPort MST where
4110 * connectors have a topology and we want to allow userspace to give
4111 * them more meaningful names.
4114 * Zero on success, negative errno on failure.
4116 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4119 struct drm_device *dev = connector->dev;
4120 size_t size = strlen(path) + 1;
4123 connector->path_blob_ptr = drm_property_create_blob(connector->dev,
4125 if (!connector->path_blob_ptr)
4128 ret = drm_object_property_set_value(&connector->base,
4129 dev->mode_config.path_property,
4130 connector->path_blob_ptr->base.id);
4133 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4136 * drm_mode_connector_set_tile_property - set tile property on connector
4137 * @connector: connector to set property on.
4139 * This looks up the tile information for a connector, and creates a
4140 * property for userspace to parse if it exists. The property is of
4141 * the form of 8 integers using ':' as a separator.
4144 * Zero on success, errno on failure.
4146 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4148 struct drm_device *dev = connector->dev;
4152 if (connector->tile_blob_ptr)
4153 drm_property_destroy_blob(dev, connector->tile_blob_ptr);
4155 if (!connector->has_tile) {
4156 connector->tile_blob_ptr = NULL;
4157 ret = drm_object_property_set_value(&connector->base,
4158 dev->mode_config.tile_property, 0);
4162 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4163 connector->tile_group->id, connector->tile_is_single_monitor,
4164 connector->num_h_tile, connector->num_v_tile,
4165 connector->tile_h_loc, connector->tile_v_loc,
4166 connector->tile_h_size, connector->tile_v_size);
4167 size = strlen(tile) + 1;
4169 connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
4171 if (!connector->tile_blob_ptr)
4174 ret = drm_object_property_set_value(&connector->base,
4175 dev->mode_config.tile_property,
4176 connector->tile_blob_ptr->base.id);
4179 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4182 * drm_mode_connector_update_edid_property - update the edid property of a connector
4183 * @connector: drm connector
4184 * @edid: new value of the edid property
4186 * This function creates a new blob modeset object and assigns its id to the
4187 * connector's edid property.
4190 * Zero on success, negative errno on failure.
4192 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4193 const struct edid *edid)
4195 struct drm_device *dev = connector->dev;
4199 /* ignore requests to set edid when overridden */
4200 if (connector->override_edid)
4203 if (connector->edid_blob_ptr)
4204 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
4206 /* Delete edid, when there is none. */
4208 connector->edid_blob_ptr = NULL;
4209 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
4213 size = EDID_LENGTH * (1 + edid->extensions);
4214 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
4216 if (!connector->edid_blob_ptr)
4219 ret = drm_object_property_set_value(&connector->base,
4220 dev->mode_config.edid_property,
4221 connector->edid_blob_ptr->base.id);
4225 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4227 /* Some properties could refer to dynamic refcnt'd objects, or things that
4228 * need special locking to handle lifetime issues (ie. to ensure the prop
4229 * value doesn't become invalid part way through the property update due to
4230 * race). The value returned by reference via 'obj' should be passed back
4231 * to drm_property_change_valid_put() after the property is set (and the
4232 * object to which the property is attached has a chance to take it's own
4235 static bool drm_property_change_valid_get(struct drm_property *property,
4236 uint64_t value, struct drm_mode_object **ref)
4240 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4245 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4246 if (value < property->values[0] || value > property->values[1])
4249 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4250 int64_t svalue = U642I64(value);
4252 if (svalue < U642I64(property->values[0]) ||
4253 svalue > U642I64(property->values[1]))
4256 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4257 uint64_t valid_mask = 0;
4259 for (i = 0; i < property->num_values; i++)
4260 valid_mask |= (1ULL << property->values[i]);
4261 return !(value & ~valid_mask);
4262 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4263 /* Only the driver knows */
4265 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4266 /* a zero value for an object property translates to null: */
4270 /* handle refcnt'd objects specially: */
4271 if (property->values[0] == DRM_MODE_OBJECT_FB) {
4272 struct drm_framebuffer *fb;
4273 fb = drm_framebuffer_lookup(property->dev, value);
4281 return _object_find(property->dev, value, property->values[0]) != NULL;
4285 for (i = 0; i < property->num_values; i++)
4286 if (property->values[i] == value)
4291 for (i = 0; i < property->num_values; i++)
4292 if (property->values[i] == value)
4297 static void drm_property_change_valid_put(struct drm_property *property,
4298 struct drm_mode_object *ref)
4303 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4304 if (property->values[0] == DRM_MODE_OBJECT_FB)
4305 drm_framebuffer_unreference(obj_to_fb(ref));
4310 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4313 * @file_priv: DRM file info
4315 * This function sets the current value for a connectors's property. It also
4316 * calls into a driver's ->set_property callback to update the hardware state
4318 * Called by the user via ioctl.
4321 * Zero on success, negative errno on failure.
4323 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4324 void *data, struct drm_file *file_priv)
4326 struct drm_mode_connector_set_property *conn_set_prop = data;
4327 struct drm_mode_obj_set_property obj_set_prop = {
4328 .value = conn_set_prop->value,
4329 .prop_id = conn_set_prop->prop_id,
4330 .obj_id = conn_set_prop->connector_id,
4331 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4334 /* It does all the locking and checking we need */
4335 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4338 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4339 struct drm_property *property,
4343 struct drm_connector *connector = obj_to_connector(obj);
4345 /* Do DPMS ourselves */
4346 if (property == connector->dev->mode_config.dpms_property) {
4347 if (connector->funcs->dpms)
4348 (*connector->funcs->dpms)(connector, (int)value);
4350 } else if (connector->funcs->set_property)
4351 ret = connector->funcs->set_property(connector, property, value);
4353 /* store the property value if successful */
4355 drm_object_property_set_value(&connector->base, property, value);
4359 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4360 struct drm_property *property,
4364 struct drm_crtc *crtc = obj_to_crtc(obj);
4366 if (crtc->funcs->set_property)
4367 ret = crtc->funcs->set_property(crtc, property, value);
4369 drm_object_property_set_value(obj, property, value);
4375 * drm_mode_plane_set_obj_prop - set the value of a property
4376 * @plane: drm plane object to set property value for
4377 * @property: property to set
4378 * @value: value the property should be set to
4380 * This functions sets a given property on a given plane object. This function
4381 * calls the driver's ->set_property callback and changes the software state of
4382 * the property if the callback succeeds.
4385 * Zero on success, error code on failure.
4387 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4388 struct drm_property *property,
4392 struct drm_mode_object *obj = &plane->base;
4394 if (plane->funcs->set_property)
4395 ret = plane->funcs->set_property(plane, property, value);
4397 drm_object_property_set_value(obj, property, value);
4401 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4404 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4407 * @file_priv: DRM file info
4409 * This function retrieves the current value for an object's property. Compared
4410 * to the connector specific ioctl this one is extended to also work on crtc and
4413 * Called by the user via ioctl.
4416 * Zero on success, negative errno on failure.
4418 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4419 struct drm_file *file_priv)
4421 struct drm_mode_obj_get_properties *arg = data;
4422 struct drm_mode_object *obj;
4425 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4428 drm_modeset_lock_all(dev);
4430 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4435 if (!obj->properties) {
4440 ret = get_properties(obj, file_priv->atomic,
4441 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4442 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4446 drm_modeset_unlock_all(dev);
4451 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4454 * @file_priv: DRM file info
4456 * This function sets the current value for an object's property. It also calls
4457 * into a driver's ->set_property callback to update the hardware state.
4458 * Compared to the connector specific ioctl this one is extended to also work on
4459 * crtc and plane objects.
4461 * Called by the user via ioctl.
4464 * Zero on success, negative errno on failure.
4466 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4467 struct drm_file *file_priv)
4469 struct drm_mode_obj_set_property *arg = data;
4470 struct drm_mode_object *arg_obj;
4471 struct drm_mode_object *prop_obj;
4472 struct drm_property *property;
4473 int i, ret = -EINVAL;
4474 struct drm_mode_object *ref;
4476 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4479 drm_modeset_lock_all(dev);
4481 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4486 if (!arg_obj->properties)
4489 for (i = 0; i < arg_obj->properties->count; i++)
4490 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
4493 if (i == arg_obj->properties->count)
4496 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4497 DRM_MODE_OBJECT_PROPERTY);
4502 property = obj_to_property(prop_obj);
4504 if (!drm_property_change_valid_get(property, arg->value, &ref))
4507 switch (arg_obj->type) {
4508 case DRM_MODE_OBJECT_CONNECTOR:
4509 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4512 case DRM_MODE_OBJECT_CRTC:
4513 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4515 case DRM_MODE_OBJECT_PLANE:
4516 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4517 property, arg->value);
4521 drm_property_change_valid_put(property, ref);
4524 drm_modeset_unlock_all(dev);
4529 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4530 * @connector: connector to attach
4531 * @encoder: encoder to attach @connector to
4533 * This function links up a connector to an encoder. Note that the routing
4534 * restrictions between encoders and crtcs are exposed to userspace through the
4535 * possible_clones and possible_crtcs bitmasks.
4538 * Zero on success, negative errno on failure.
4540 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4541 struct drm_encoder *encoder)
4545 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4546 if (connector->encoder_ids[i] == 0) {
4547 connector->encoder_ids[i] = encoder->base.id;
4553 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4556 * drm_mode_crtc_set_gamma_size - set the gamma table size
4557 * @crtc: CRTC to set the gamma table size for
4558 * @gamma_size: size of the gamma table
4560 * Drivers which support gamma tables should set this to the supported gamma
4561 * table size when initializing the CRTC. Currently the drm core only supports a
4562 * fixed gamma table size.
4565 * Zero on success, negative errno on failure.
4567 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
4570 crtc->gamma_size = gamma_size;
4572 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4574 if (!crtc->gamma_store) {
4575 crtc->gamma_size = 0;
4581 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4584 * drm_mode_gamma_set_ioctl - set the gamma table
4587 * @file_priv: DRM file info
4589 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4590 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4592 * Called by the user via ioctl.
4595 * Zero on success, negative errno on failure.
4597 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4598 void *data, struct drm_file *file_priv)
4600 struct drm_mode_crtc_lut *crtc_lut = data;
4601 struct drm_crtc *crtc;
4602 void *r_base, *g_base, *b_base;
4606 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4609 drm_modeset_lock_all(dev);
4610 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4616 if (crtc->funcs->gamma_set == NULL) {
4621 /* memcpy into gamma store */
4622 if (crtc_lut->gamma_size != crtc->gamma_size) {
4627 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4628 r_base = crtc->gamma_store;
4629 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4634 g_base = r_base + size;
4635 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4640 b_base = g_base + size;
4641 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4646 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
4649 drm_modeset_unlock_all(dev);
4655 * drm_mode_gamma_get_ioctl - get the gamma table
4658 * @file_priv: DRM file info
4660 * Copy the current gamma table into the storage provided. This also provides
4661 * the gamma table size the driver expects, which can be used to size the
4662 * allocated storage.
4664 * Called by the user via ioctl.
4667 * Zero on success, negative errno on failure.
4669 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4670 void *data, struct drm_file *file_priv)
4672 struct drm_mode_crtc_lut *crtc_lut = data;
4673 struct drm_crtc *crtc;
4674 void *r_base, *g_base, *b_base;
4678 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4681 drm_modeset_lock_all(dev);
4682 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4688 /* memcpy into gamma store */
4689 if (crtc_lut->gamma_size != crtc->gamma_size) {
4694 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4695 r_base = crtc->gamma_store;
4696 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4701 g_base = r_base + size;
4702 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4707 b_base = g_base + size;
4708 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4713 drm_modeset_unlock_all(dev);
4718 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4721 * @file_priv: DRM file info
4723 * This schedules an asynchronous update on a given CRTC, called page flip.
4724 * Optionally a drm event is generated to signal the completion of the event.
4725 * Generic drivers cannot assume that a pageflip with changed framebuffer
4726 * properties (including driver specific metadata like tiling layout) will work,
4727 * but some drivers support e.g. pixel format changes through the pageflip
4730 * Called by the user via ioctl.
4733 * Zero on success, negative errno on failure.
4735 int drm_mode_page_flip_ioctl(struct drm_device *dev,
4736 void *data, struct drm_file *file_priv)
4738 struct drm_mode_crtc_page_flip *page_flip = data;
4739 struct drm_crtc *crtc;
4740 struct drm_framebuffer *fb = NULL;
4741 struct drm_pending_vblank_event *e = NULL;
4742 unsigned long flags;
4745 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4746 page_flip->reserved != 0)
4749 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4752 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4756 drm_modeset_lock_crtc(crtc, crtc->primary);
4757 if (crtc->primary->fb == NULL) {
4758 /* The framebuffer is currently unbound, presumably
4759 * due to a hotplug event, that userspace has not
4766 if (crtc->funcs->page_flip == NULL)
4769 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
4775 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4779 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
4780 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4785 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4787 spin_lock_irqsave(&dev->event_lock, flags);
4788 if (file_priv->event_space < sizeof(e->event)) {
4789 spin_unlock_irqrestore(&dev->event_lock, flags);
4792 file_priv->event_space -= sizeof(e->event);
4793 spin_unlock_irqrestore(&dev->event_lock, flags);
4795 e = kzalloc(sizeof(*e), GFP_KERNEL);
4797 spin_lock_irqsave(&dev->event_lock, flags);
4798 file_priv->event_space += sizeof(e->event);
4799 spin_unlock_irqrestore(&dev->event_lock, flags);
4803 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
4804 e->event.base.length = sizeof(e->event);
4805 e->event.user_data = page_flip->user_data;
4806 e->base.event = &e->event.base;
4807 e->base.file_priv = file_priv;
4809 (void (*) (struct drm_pending_event *)) kfree;
4812 crtc->primary->old_fb = crtc->primary->fb;
4813 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
4815 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
4816 spin_lock_irqsave(&dev->event_lock, flags);
4817 file_priv->event_space += sizeof(e->event);
4818 spin_unlock_irqrestore(&dev->event_lock, flags);
4821 /* Keep the old fb, don't unref it. */
4822 crtc->primary->old_fb = NULL;
4825 * Warn if the driver hasn't properly updated the crtc->fb
4826 * field to reflect that the new framebuffer is now used.
4827 * Failing to do so will screw with the reference counting
4830 WARN_ON(crtc->primary->fb != fb);
4831 /* Unref only the old framebuffer. */
4837 drm_framebuffer_unreference(fb);
4838 if (crtc->primary->old_fb)
4839 drm_framebuffer_unreference(crtc->primary->old_fb);
4840 crtc->primary->old_fb = NULL;
4841 drm_modeset_unlock_crtc(crtc);
4847 * drm_mode_config_reset - call ->reset callbacks
4850 * This functions calls all the crtc's, encoder's and connector's ->reset
4851 * callback. Drivers can use this in e.g. their driver load or resume code to
4852 * reset hardware and software state.
4854 void drm_mode_config_reset(struct drm_device *dev)
4856 struct drm_crtc *crtc;
4857 struct drm_plane *plane;
4858 struct drm_encoder *encoder;
4859 struct drm_connector *connector;
4861 list_for_each_entry(plane, &dev->mode_config.plane_list, head)
4862 if (plane->funcs->reset)
4863 plane->funcs->reset(plane);
4865 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4866 if (crtc->funcs->reset)
4867 crtc->funcs->reset(crtc);
4869 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
4870 if (encoder->funcs->reset)
4871 encoder->funcs->reset(encoder);
4873 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4874 connector->status = connector_status_unknown;
4876 if (connector->funcs->reset)
4877 connector->funcs->reset(connector);
4880 EXPORT_SYMBOL(drm_mode_config_reset);
4883 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4886 * @file_priv: DRM file info
4888 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4889 * TTM or something else entirely) and returns the resulting buffer handle. This
4890 * handle can then be wrapped up into a framebuffer modeset object.
4892 * Note that userspace is not allowed to use such objects for render
4893 * acceleration - drivers must create their own private ioctls for such a use
4896 * Called by the user via ioctl.
4899 * Zero on success, negative errno on failure.
4901 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4902 void *data, struct drm_file *file_priv)
4904 struct drm_mode_create_dumb *args = data;
4905 u32 cpp, stride, size;
4907 if (!dev->driver->dumb_create)
4909 if (!args->width || !args->height || !args->bpp)
4912 /* overflow checks for 32bit size calculations */
4913 /* NOTE: DIV_ROUND_UP() can overflow */
4914 cpp = DIV_ROUND_UP(args->bpp, 8);
4915 if (!cpp || cpp > 0xffffffffU / args->width)
4917 stride = cpp * args->width;
4918 if (args->height > 0xffffffffU / stride)
4921 /* test for wrap-around */
4922 size = args->height * stride;
4923 if (PAGE_ALIGN(size) == 0)
4927 * handle, pitch and size are output parameters. Zero them out to
4928 * prevent drivers from accidentally using uninitialized data. Since
4929 * not all existing userspace is clearing these fields properly we
4930 * cannot reject IOCTL with garbage in them.
4936 return dev->driver->dumb_create(file_priv, dev, args);
4940 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4943 * @file_priv: DRM file info
4945 * Allocate an offset in the drm device node's address space to be able to
4946 * memory map a dumb buffer.
4948 * Called by the user via ioctl.
4951 * Zero on success, negative errno on failure.
4953 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4954 void *data, struct drm_file *file_priv)
4956 struct drm_mode_map_dumb *args = data;
4958 /* call driver ioctl to get mmap offset */
4959 if (!dev->driver->dumb_map_offset)
4962 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4966 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4969 * @file_priv: DRM file info
4971 * This destroys the userspace handle for the given dumb backing storage buffer.
4972 * Since buffer objects must be reference counted in the kernel a buffer object
4973 * won't be immediately freed if a framebuffer modeset object still uses it.
4975 * Called by the user via ioctl.
4978 * Zero on success, negative errno on failure.
4980 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4981 void *data, struct drm_file *file_priv)
4983 struct drm_mode_destroy_dumb *args = data;
4985 if (!dev->driver->dumb_destroy)
4988 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4992 * drm_fb_get_bpp_depth - get the bpp/depth values for format
4993 * @format: pixel format (DRM_FORMAT_*)
4994 * @depth: storage for the depth value
4995 * @bpp: storage for the bpp value
4997 * This only supports RGB formats here for compat with code that doesn't use
4998 * pixel formats directly yet.
5000 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
5005 case DRM_FORMAT_RGB332:
5006 case DRM_FORMAT_BGR233:
5010 case DRM_FORMAT_XRGB1555:
5011 case DRM_FORMAT_XBGR1555:
5012 case DRM_FORMAT_RGBX5551:
5013 case DRM_FORMAT_BGRX5551:
5014 case DRM_FORMAT_ARGB1555:
5015 case DRM_FORMAT_ABGR1555:
5016 case DRM_FORMAT_RGBA5551:
5017 case DRM_FORMAT_BGRA5551:
5021 case DRM_FORMAT_RGB565:
5022 case DRM_FORMAT_BGR565:
5026 case DRM_FORMAT_RGB888:
5027 case DRM_FORMAT_BGR888:
5031 case DRM_FORMAT_XRGB8888:
5032 case DRM_FORMAT_XBGR8888:
5033 case DRM_FORMAT_RGBX8888:
5034 case DRM_FORMAT_BGRX8888:
5038 case DRM_FORMAT_XRGB2101010:
5039 case DRM_FORMAT_XBGR2101010:
5040 case DRM_FORMAT_RGBX1010102:
5041 case DRM_FORMAT_BGRX1010102:
5042 case DRM_FORMAT_ARGB2101010:
5043 case DRM_FORMAT_ABGR2101010:
5044 case DRM_FORMAT_RGBA1010102:
5045 case DRM_FORMAT_BGRA1010102:
5049 case DRM_FORMAT_ARGB8888:
5050 case DRM_FORMAT_ABGR8888:
5051 case DRM_FORMAT_RGBA8888:
5052 case DRM_FORMAT_BGRA8888:
5057 DRM_DEBUG_KMS("unsupported pixel format %s\n",
5058 drm_get_format_name(format));
5064 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
5067 * drm_format_num_planes - get the number of planes for format
5068 * @format: pixel format (DRM_FORMAT_*)
5071 * The number of planes used by the specified pixel format.
5073 int drm_format_num_planes(uint32_t format)
5076 case DRM_FORMAT_YUV410:
5077 case DRM_FORMAT_YVU410:
5078 case DRM_FORMAT_YUV411:
5079 case DRM_FORMAT_YVU411:
5080 case DRM_FORMAT_YUV420:
5081 case DRM_FORMAT_YVU420:
5082 case DRM_FORMAT_YUV422:
5083 case DRM_FORMAT_YVU422:
5084 case DRM_FORMAT_YUV444:
5085 case DRM_FORMAT_YVU444:
5087 case DRM_FORMAT_NV12:
5088 case DRM_FORMAT_NV21:
5089 case DRM_FORMAT_NV16:
5090 case DRM_FORMAT_NV61:
5091 case DRM_FORMAT_NV24:
5092 case DRM_FORMAT_NV42:
5098 EXPORT_SYMBOL(drm_format_num_planes);
5101 * drm_format_plane_cpp - determine the bytes per pixel value
5102 * @format: pixel format (DRM_FORMAT_*)
5103 * @plane: plane index
5106 * The bytes per pixel value for the specified plane.
5108 int drm_format_plane_cpp(uint32_t format, int plane)
5113 if (plane >= drm_format_num_planes(format))
5117 case DRM_FORMAT_YUYV:
5118 case DRM_FORMAT_YVYU:
5119 case DRM_FORMAT_UYVY:
5120 case DRM_FORMAT_VYUY:
5122 case DRM_FORMAT_NV12:
5123 case DRM_FORMAT_NV21:
5124 case DRM_FORMAT_NV16:
5125 case DRM_FORMAT_NV61:
5126 case DRM_FORMAT_NV24:
5127 case DRM_FORMAT_NV42:
5128 return plane ? 2 : 1;
5129 case DRM_FORMAT_YUV410:
5130 case DRM_FORMAT_YVU410:
5131 case DRM_FORMAT_YUV411:
5132 case DRM_FORMAT_YVU411:
5133 case DRM_FORMAT_YUV420:
5134 case DRM_FORMAT_YVU420:
5135 case DRM_FORMAT_YUV422:
5136 case DRM_FORMAT_YVU422:
5137 case DRM_FORMAT_YUV444:
5138 case DRM_FORMAT_YVU444:
5141 drm_fb_get_bpp_depth(format, &depth, &bpp);
5145 EXPORT_SYMBOL(drm_format_plane_cpp);
5148 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
5149 * @format: pixel format (DRM_FORMAT_*)
5152 * The horizontal chroma subsampling factor for the
5153 * specified pixel format.
5155 int drm_format_horz_chroma_subsampling(uint32_t format)
5158 case DRM_FORMAT_YUV411:
5159 case DRM_FORMAT_YVU411:
5160 case DRM_FORMAT_YUV410:
5161 case DRM_FORMAT_YVU410:
5163 case DRM_FORMAT_YUYV:
5164 case DRM_FORMAT_YVYU:
5165 case DRM_FORMAT_UYVY:
5166 case DRM_FORMAT_VYUY:
5167 case DRM_FORMAT_NV12:
5168 case DRM_FORMAT_NV21:
5169 case DRM_FORMAT_NV16:
5170 case DRM_FORMAT_NV61:
5171 case DRM_FORMAT_YUV422:
5172 case DRM_FORMAT_YVU422:
5173 case DRM_FORMAT_YUV420:
5174 case DRM_FORMAT_YVU420:
5180 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
5183 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
5184 * @format: pixel format (DRM_FORMAT_*)
5187 * The vertical chroma subsampling factor for the
5188 * specified pixel format.
5190 int drm_format_vert_chroma_subsampling(uint32_t format)
5193 case DRM_FORMAT_YUV410:
5194 case DRM_FORMAT_YVU410:
5196 case DRM_FORMAT_YUV420:
5197 case DRM_FORMAT_YVU420:
5198 case DRM_FORMAT_NV12:
5199 case DRM_FORMAT_NV21:
5205 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
5208 * drm_rotation_simplify() - Try to simplify the rotation
5209 * @rotation: Rotation to be simplified
5210 * @supported_rotations: Supported rotations
5212 * Attempt to simplify the rotation to a form that is supported.
5213 * Eg. if the hardware supports everything except DRM_REFLECT_X
5214 * one could call this function like this:
5216 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5217 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5218 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5220 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5221 * transforms the hardware supports, this function may not
5222 * be able to produce a supported transform, so the caller should
5223 * check the result afterwards.
5225 unsigned int drm_rotation_simplify(unsigned int rotation,
5226 unsigned int supported_rotations)
5228 if (rotation & ~supported_rotations) {
5229 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5230 rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
5235 EXPORT_SYMBOL(drm_rotation_simplify);
5238 * drm_mode_config_init - initialize DRM mode_configuration structure
5241 * Initialize @dev's mode_config structure, used for tracking the graphics
5242 * configuration of @dev.
5244 * Since this initializes the modeset locks, no locking is possible. Which is no
5245 * problem, since this should happen single threaded at init time. It is the
5246 * driver's problem to ensure this guarantee.
5249 void drm_mode_config_init(struct drm_device *dev)
5251 mutex_init(&dev->mode_config.mutex);
5252 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5253 mutex_init(&dev->mode_config.idr_mutex);
5254 mutex_init(&dev->mode_config.fb_lock);
5255 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5256 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5257 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5258 INIT_LIST_HEAD(&dev->mode_config.bridge_list);
5259 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5260 INIT_LIST_HEAD(&dev->mode_config.property_list);
5261 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5262 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5263 idr_init(&dev->mode_config.crtc_idr);
5264 idr_init(&dev->mode_config.tile_idr);
5266 drm_modeset_lock_all(dev);
5267 drm_mode_create_standard_connector_properties(dev);
5268 drm_mode_create_standard_plane_properties(dev);
5269 drm_modeset_unlock_all(dev);
5271 /* Just to be sure */
5272 dev->mode_config.num_fb = 0;
5273 dev->mode_config.num_connector = 0;
5274 dev->mode_config.num_crtc = 0;
5275 dev->mode_config.num_encoder = 0;
5276 dev->mode_config.num_overlay_plane = 0;
5277 dev->mode_config.num_total_plane = 0;
5279 EXPORT_SYMBOL(drm_mode_config_init);
5282 * drm_mode_config_cleanup - free up DRM mode_config info
5285 * Free up all the connectors and CRTCs associated with this DRM device, then
5286 * free up the framebuffers and associated buffer objects.
5288 * Note that since this /should/ happen single-threaded at driver/device
5289 * teardown time, no locking is required. It's the driver's job to ensure that
5290 * this guarantee actually holds true.
5292 * FIXME: cleanup any dangling user buffer objects too
5294 void drm_mode_config_cleanup(struct drm_device *dev)
5296 struct drm_connector *connector, *ot;
5297 struct drm_crtc *crtc, *ct;
5298 struct drm_encoder *encoder, *enct;
5299 struct drm_bridge *bridge, *brt;
5300 struct drm_framebuffer *fb, *fbt;
5301 struct drm_property *property, *pt;
5302 struct drm_property_blob *blob, *bt;
5303 struct drm_plane *plane, *plt;
5305 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5307 encoder->funcs->destroy(encoder);
5310 list_for_each_entry_safe(bridge, brt,
5311 &dev->mode_config.bridge_list, head) {
5312 bridge->funcs->destroy(bridge);
5315 list_for_each_entry_safe(connector, ot,
5316 &dev->mode_config.connector_list, head) {
5317 connector->funcs->destroy(connector);
5320 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5322 drm_property_destroy(dev, property);
5325 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5327 drm_property_destroy_blob(dev, blob);
5331 * Single-threaded teardown context, so it's not required to grab the
5332 * fb_lock to protect against concurrent fb_list access. Contrary, it
5333 * would actually deadlock with the drm_framebuffer_cleanup function.
5335 * Also, if there are any framebuffers left, that's a driver leak now,
5336 * so politely WARN about this.
5338 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5339 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5340 drm_framebuffer_remove(fb);
5343 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5345 plane->funcs->destroy(plane);
5348 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5349 crtc->funcs->destroy(crtc);
5352 idr_destroy(&dev->mode_config.tile_idr);
5353 idr_destroy(&dev->mode_config.crtc_idr);
5354 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5356 EXPORT_SYMBOL(drm_mode_config_cleanup);
5358 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5359 unsigned int supported_rotations)
5361 static const struct drm_prop_enum_list props[] = {
5362 { DRM_ROTATE_0, "rotate-0" },
5363 { DRM_ROTATE_90, "rotate-90" },
5364 { DRM_ROTATE_180, "rotate-180" },
5365 { DRM_ROTATE_270, "rotate-270" },
5366 { DRM_REFLECT_X, "reflect-x" },
5367 { DRM_REFLECT_Y, "reflect-y" },
5370 return drm_property_create_bitmask(dev, 0, "rotation",
5371 props, ARRAY_SIZE(props),
5372 supported_rotations);
5374 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5379 * Tile groups are used to represent tiled monitors with a unique
5380 * integer identifier. Tiled monitors using DisplayID v1.3 have
5381 * a unique 8-byte handle, we store this in a tile group, so we
5382 * have a common identifier for all tiles in a monitor group.
5384 static void drm_tile_group_free(struct kref *kref)
5386 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5387 struct drm_device *dev = tg->dev;
5388 mutex_lock(&dev->mode_config.idr_mutex);
5389 idr_remove(&dev->mode_config.tile_idr, tg->id);
5390 mutex_unlock(&dev->mode_config.idr_mutex);
5395 * drm_mode_put_tile_group - drop a reference to a tile group.
5397 * @tg: tile group to drop reference to.
5399 * drop reference to tile group and free if 0.
5401 void drm_mode_put_tile_group(struct drm_device *dev,
5402 struct drm_tile_group *tg)
5404 kref_put(&tg->refcount, drm_tile_group_free);
5408 * drm_mode_get_tile_group - get a reference to an existing tile group
5410 * @topology: 8-bytes unique per monitor.
5412 * Use the unique bytes to get a reference to an existing tile group.
5415 * tile group or NULL if not found.
5417 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5420 struct drm_tile_group *tg;
5422 mutex_lock(&dev->mode_config.idr_mutex);
5423 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5424 if (!memcmp(tg->group_data, topology, 8)) {
5425 if (!kref_get_unless_zero(&tg->refcount))
5427 mutex_unlock(&dev->mode_config.idr_mutex);
5431 mutex_unlock(&dev->mode_config.idr_mutex);
5436 * drm_mode_create_tile_group - create a tile group from a displayid description
5438 * @topology: 8-bytes unique per monitor.
5440 * Create a tile group for the unique monitor, and get a unique
5441 * identifier for the tile group.
5444 * new tile group or error.
5446 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5449 struct drm_tile_group *tg;
5452 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5454 return ERR_PTR(-ENOMEM);
5456 kref_init(&tg->refcount);
5457 memcpy(tg->group_data, topology, 8);
5460 mutex_lock(&dev->mode_config.idr_mutex);
5461 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5469 mutex_unlock(&dev->mode_config.idr_mutex);