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 *
47 internal_framebuffer_create(struct drm_device *dev,
48 const struct drm_mode_fb_cmd2 *r,
49 struct drm_file *file_priv);
51 /* Avoid boilerplate. I'm tired of typing. */
52 #define DRM_ENUM_NAME_FN(fnname, list) \
53 const char *fnname(int val) \
56 for (i = 0; i < ARRAY_SIZE(list); i++) { \
57 if (list[i].type == val) \
58 return list[i].name; \
66 static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
67 { DRM_MODE_DPMS_ON, "On" },
68 { DRM_MODE_DPMS_STANDBY, "Standby" },
69 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
70 { DRM_MODE_DPMS_OFF, "Off" }
73 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
75 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
76 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
77 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
78 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
84 static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
85 { DRM_MODE_SCALE_NONE, "None" },
86 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
87 { DRM_MODE_SCALE_CENTER, "Center" },
88 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
91 static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
92 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
93 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
94 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
98 * Non-global properties, but "required" for certain connectors.
100 static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
101 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
108 static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
109 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
110 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
111 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
114 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
115 drm_dvi_i_subconnector_enum_list)
117 static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
118 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
125 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
127 static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
128 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
129 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
130 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
135 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
136 drm_tv_subconnector_enum_list)
138 static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
139 { DRM_MODE_DIRTY_OFF, "Off" },
140 { DRM_MODE_DIRTY_ON, "On" },
141 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
144 struct drm_conn_prop_enum_list {
151 * Connector and encoder types.
153 static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
154 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
155 { DRM_MODE_CONNECTOR_VGA, "VGA" },
156 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
157 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
158 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
159 { DRM_MODE_CONNECTOR_Composite, "Composite" },
160 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
161 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
162 { DRM_MODE_CONNECTOR_Component, "Component" },
163 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
164 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
165 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
166 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
167 { DRM_MODE_CONNECTOR_TV, "TV" },
168 { DRM_MODE_CONNECTOR_eDP, "eDP" },
169 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
170 { DRM_MODE_CONNECTOR_DSI, "DSI" },
171 { DRM_MODE_CONNECTOR_DPI, "DPI" },
174 static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
175 { DRM_MODE_ENCODER_NONE, "None" },
176 { DRM_MODE_ENCODER_DAC, "DAC" },
177 { DRM_MODE_ENCODER_TMDS, "TMDS" },
178 { DRM_MODE_ENCODER_LVDS, "LVDS" },
179 { DRM_MODE_ENCODER_TVDAC, "TV" },
180 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
181 { DRM_MODE_ENCODER_DSI, "DSI" },
182 { DRM_MODE_ENCODER_DPMST, "DP MST" },
183 { DRM_MODE_ENCODER_DPI, "DPI" },
186 static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
187 { SubPixelUnknown, "Unknown" },
188 { SubPixelHorizontalRGB, "Horizontal RGB" },
189 { SubPixelHorizontalBGR, "Horizontal BGR" },
190 { SubPixelVerticalRGB, "Vertical RGB" },
191 { SubPixelVerticalBGR, "Vertical BGR" },
192 { SubPixelNone, "None" },
195 void drm_connector_ida_init(void)
199 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
200 ida_init(&drm_connector_enum_list[i].ida);
203 void drm_connector_ida_destroy(void)
207 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
208 ida_destroy(&drm_connector_enum_list[i].ida);
212 * drm_get_connector_status_name - return a string for connector status
213 * @status: connector status to compute name of
215 * In contrast to the other drm_get_*_name functions this one here returns a
216 * const pointer and hence is threadsafe.
218 const char *drm_get_connector_status_name(enum drm_connector_status status)
220 if (status == connector_status_connected)
222 else if (status == connector_status_disconnected)
223 return "disconnected";
227 EXPORT_SYMBOL(drm_get_connector_status_name);
230 * drm_get_subpixel_order_name - return a string for a given subpixel enum
231 * @order: enum of subpixel_order
233 * Note you could abuse this and return something out of bounds, but that
234 * would be a caller error. No unscrubbed user data should make it here.
236 const char *drm_get_subpixel_order_name(enum subpixel_order order)
238 return drm_subpixel_enum_list[order].name;
240 EXPORT_SYMBOL(drm_get_subpixel_order_name);
243 * Internal function to assign a slot in the object idr and optionally
244 * register the object into the idr.
246 static int drm_mode_object_get_reg(struct drm_device *dev,
247 struct drm_mode_object *obj,
250 void (*obj_free_cb)(struct kref *kref))
254 mutex_lock(&dev->mode_config.idr_mutex);
255 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
258 * Set up the object linking under the protection of the idr
259 * lock so that other users can't see inconsistent state.
262 obj->type = obj_type;
264 obj->free_cb = obj_free_cb;
265 kref_init(&obj->refcount);
268 mutex_unlock(&dev->mode_config.idr_mutex);
270 return ret < 0 ? ret : 0;
274 * drm_mode_object_get - allocate a new modeset identifier
276 * @obj: object pointer, used to generate unique ID
277 * @obj_type: object type
279 * Create a unique identifier based on @ptr in @dev's identifier space. Used
280 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
281 * modeset identifiers are _not_ reference counted. Hence don't use this for
282 * reference counted modeset objects like framebuffers.
285 * Zero on success, error code on failure.
287 int drm_mode_object_get(struct drm_device *dev,
288 struct drm_mode_object *obj, uint32_t obj_type)
290 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
293 static void drm_mode_object_register(struct drm_device *dev,
294 struct drm_mode_object *obj)
296 mutex_lock(&dev->mode_config.idr_mutex);
297 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
298 mutex_unlock(&dev->mode_config.idr_mutex);
302 * drm_mode_object_unregister - free a modeset identifer
304 * @object: object to free
306 * Free @id from @dev's unique identifier pool.
307 * This function can be called multiple times, and guards against
309 * These modeset identifiers are _not_ reference counted. Hence don't use this
310 * for reference counted modeset objects like framebuffers.
312 void drm_mode_object_unregister(struct drm_device *dev,
313 struct drm_mode_object *object)
315 mutex_lock(&dev->mode_config.idr_mutex);
317 idr_remove(&dev->mode_config.crtc_idr, object->id);
320 mutex_unlock(&dev->mode_config.idr_mutex);
323 static struct drm_mode_object *_object_find(struct drm_device *dev,
324 uint32_t id, uint32_t type)
326 struct drm_mode_object *obj = NULL;
328 mutex_lock(&dev->mode_config.idr_mutex);
329 obj = idr_find(&dev->mode_config.crtc_idr, id);
330 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
332 if (obj && obj->id != id)
335 if (obj && obj->free_cb) {
336 if (!kref_get_unless_zero(&obj->refcount))
339 mutex_unlock(&dev->mode_config.idr_mutex);
345 * drm_mode_object_find - look up a drm object with static lifetime
347 * @id: id of the mode object
348 * @type: type of the mode object
350 * This function is used to look up a modeset object. It will acquire a
351 * reference for reference counted objects. This reference must be dropped again
352 * by callind drm_mode_object_unreference().
354 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
355 uint32_t id, uint32_t type)
357 struct drm_mode_object *obj = NULL;
359 obj = _object_find(dev, id, type);
362 EXPORT_SYMBOL(drm_mode_object_find);
365 * drm_mode_object_unreference - decr the object refcnt
368 * This functions decrements the object's refcount if it is a refcounted modeset
369 * object. It is a no-op on any other object. This is used to drop references
370 * acquired with drm_mode_object_reference().
372 void drm_mode_object_unreference(struct drm_mode_object *obj)
375 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
376 kref_put(&obj->refcount, obj->free_cb);
379 EXPORT_SYMBOL(drm_mode_object_unreference);
382 * drm_mode_object_reference - incr the object refcnt
385 * This functions increments the object's refcount if it is a refcounted modeset
386 * object. It is a no-op on any other object. References should be dropped again
387 * by calling drm_mode_object_unreference().
389 void drm_mode_object_reference(struct drm_mode_object *obj)
392 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
393 kref_get(&obj->refcount);
396 EXPORT_SYMBOL(drm_mode_object_reference);
398 static void drm_framebuffer_free(struct kref *kref)
400 struct drm_framebuffer *fb =
401 container_of(kref, struct drm_framebuffer, base.refcount);
402 struct drm_device *dev = fb->dev;
405 * The lookup idr holds a weak reference, which has not necessarily been
406 * removed at this point. Check for that.
408 drm_mode_object_unregister(dev, &fb->base);
410 fb->funcs->destroy(fb);
414 * drm_framebuffer_init - initialize a framebuffer
416 * @fb: framebuffer to be initialized
417 * @funcs: ... with these functions
419 * Allocates an ID for the framebuffer's parent mode object, sets its mode
420 * functions & device file and adds it to the master fd list.
423 * This functions publishes the fb and makes it available for concurrent access
424 * by other users. Which means by this point the fb _must_ be fully set up -
425 * since all the fb attributes are invariant over its lifetime, no further
426 * locking but only correct reference counting is required.
429 * Zero on success, error code on failure.
431 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
432 const struct drm_framebuffer_funcs *funcs)
436 INIT_LIST_HEAD(&fb->filp_head);
440 ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
441 false, drm_framebuffer_free);
445 mutex_lock(&dev->mode_config.fb_lock);
446 dev->mode_config.num_fb++;
447 list_add(&fb->head, &dev->mode_config.fb_list);
448 mutex_unlock(&dev->mode_config.fb_lock);
450 drm_mode_object_register(dev, &fb->base);
454 EXPORT_SYMBOL(drm_framebuffer_init);
457 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
459 * @id: id of the fb object
461 * If successful, this grabs an additional reference to the framebuffer -
462 * callers need to make sure to eventually unreference the returned framebuffer
463 * again, using @drm_framebuffer_unreference.
465 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
468 struct drm_mode_object *obj;
469 struct drm_framebuffer *fb = NULL;
471 obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
476 EXPORT_SYMBOL(drm_framebuffer_lookup);
479 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
480 * @fb: fb to unregister
482 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
483 * those used for fbdev. Note that the caller must hold a reference of it's own,
484 * i.e. the object may not be destroyed through this call (since it'll lead to a
485 * locking inversion).
487 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
489 struct drm_device *dev;
496 /* Mark fb as reaped and drop idr ref. */
497 drm_mode_object_unregister(dev, &fb->base);
499 EXPORT_SYMBOL(drm_framebuffer_unregister_private);
502 * drm_framebuffer_cleanup - remove a framebuffer object
503 * @fb: framebuffer to remove
505 * Cleanup framebuffer. This function is intended to be used from the drivers
506 * ->destroy callback. It can also be used to clean up driver private
507 * framebuffers embedded into a larger structure.
509 * Note that this function does not remove the fb from active usuage - if it is
510 * still used anywhere, hilarity can ensue since userspace could call getfb on
511 * the id and get back -EINVAL. Obviously no concern at driver unload time.
513 * Also, the framebuffer will not be removed from the lookup idr - for
514 * user-created framebuffers this will happen in in the rmfb ioctl. For
515 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
516 * drm_framebuffer_unregister_private.
518 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
520 struct drm_device *dev = fb->dev;
522 mutex_lock(&dev->mode_config.fb_lock);
524 dev->mode_config.num_fb--;
525 mutex_unlock(&dev->mode_config.fb_lock);
527 EXPORT_SYMBOL(drm_framebuffer_cleanup);
530 * drm_framebuffer_remove - remove and unreference a framebuffer object
531 * @fb: framebuffer to remove
533 * Scans all the CRTCs and planes in @dev's mode_config. If they're
534 * using @fb, removes it, setting it to NULL. Then drops the reference to the
535 * passed-in framebuffer. Might take the modeset locks.
537 * Note that this function optimizes the cleanup away if the caller holds the
538 * last reference to the framebuffer. It is also guaranteed to not take the
539 * modeset locks in this case.
541 void drm_framebuffer_remove(struct drm_framebuffer *fb)
543 struct drm_device *dev;
544 struct drm_crtc *crtc;
545 struct drm_plane *plane;
546 struct drm_mode_set set;
554 WARN_ON(!list_empty(&fb->filp_head));
557 * drm ABI mandates that we remove any deleted framebuffers from active
558 * useage. But since most sane clients only remove framebuffers they no
559 * longer need, try to optimize this away.
561 * Since we're holding a reference ourselves, observing a refcount of 1
562 * means that we're the last holder and can skip it. Also, the refcount
563 * can never increase from 1 again, so we don't need any barriers or
566 * Note that userspace could try to race with use and instate a new
567 * usage _after_ we've cleared all current ones. End result will be an
568 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
571 if (drm_framebuffer_read_refcount(fb) > 1) {
572 drm_modeset_lock_all(dev);
573 /* remove from any CRTC */
574 drm_for_each_crtc(crtc, dev) {
575 if (crtc->primary->fb == fb) {
576 /* should turn off the crtc */
577 memset(&set, 0, sizeof(struct drm_mode_set));
580 ret = drm_mode_set_config_internal(&set);
582 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
586 drm_for_each_plane(plane, dev) {
588 drm_plane_force_disable(plane);
590 drm_modeset_unlock_all(dev);
593 drm_framebuffer_unreference(fb);
595 EXPORT_SYMBOL(drm_framebuffer_remove);
597 DEFINE_WW_CLASS(crtc_ww_class);
599 static unsigned int drm_num_crtcs(struct drm_device *dev)
601 unsigned int num = 0;
602 struct drm_crtc *tmp;
604 drm_for_each_crtc(tmp, dev) {
612 * drm_crtc_init_with_planes - Initialise a new CRTC object with
613 * specified primary and cursor planes.
615 * @crtc: CRTC object to init
616 * @primary: Primary plane for CRTC
617 * @cursor: Cursor plane for CRTC
618 * @funcs: callbacks for the new CRTC
619 * @name: printf style format string for the CRTC name, or NULL for default name
621 * Inits a new object created as base part of a driver crtc object.
624 * Zero on success, error code on failure.
626 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
627 struct drm_plane *primary,
628 struct drm_plane *cursor,
629 const struct drm_crtc_funcs *funcs,
630 const char *name, ...)
632 struct drm_mode_config *config = &dev->mode_config;
635 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
636 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
641 INIT_LIST_HEAD(&crtc->commit_list);
642 spin_lock_init(&crtc->commit_lock);
644 drm_modeset_lock_init(&crtc->mutex);
645 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
653 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
656 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
660 drm_mode_object_unregister(dev, &crtc->base);
664 crtc->base.properties = &crtc->properties;
666 list_add_tail(&crtc->head, &config->crtc_list);
667 crtc->index = config->num_crtc++;
669 crtc->primary = primary;
670 crtc->cursor = cursor;
672 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
674 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
676 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
677 drm_object_attach_property(&crtc->base, config->prop_active, 0);
678 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
683 EXPORT_SYMBOL(drm_crtc_init_with_planes);
686 * drm_crtc_cleanup - Clean up the core crtc usage
687 * @crtc: CRTC to cleanup
689 * This function cleans up @crtc and removes it from the DRM mode setting
690 * core. Note that the function does *not* free the crtc structure itself,
691 * this is the responsibility of the caller.
693 void drm_crtc_cleanup(struct drm_crtc *crtc)
695 struct drm_device *dev = crtc->dev;
697 /* Note that the crtc_list is considered to be static; should we
698 * remove the drm_crtc at runtime we would have to decrement all
699 * the indices on the drm_crtc after us in the crtc_list.
702 kfree(crtc->gamma_store);
703 crtc->gamma_store = NULL;
705 drm_modeset_lock_fini(&crtc->mutex);
707 drm_mode_object_unregister(dev, &crtc->base);
708 list_del(&crtc->head);
709 dev->mode_config.num_crtc--;
711 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
712 if (crtc->state && crtc->funcs->atomic_destroy_state)
713 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
717 memset(crtc, 0, sizeof(*crtc));
719 EXPORT_SYMBOL(drm_crtc_cleanup);
722 * drm_mode_remove - remove and free a mode
723 * @connector: connector list to modify
724 * @mode: mode to remove
726 * Remove @mode from @connector's mode list, then free it.
728 static void drm_mode_remove(struct drm_connector *connector,
729 struct drm_display_mode *mode)
731 list_del(&mode->head);
732 drm_mode_destroy(connector->dev, mode);
736 * drm_display_info_set_bus_formats - set the supported bus formats
737 * @info: display info to store bus formats in
738 * @formats: array containing the supported bus formats
739 * @num_formats: the number of entries in the fmts array
741 * Store the supported bus formats in display info structure.
742 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
743 * a full list of available formats.
745 int drm_display_info_set_bus_formats(struct drm_display_info *info,
747 unsigned int num_formats)
751 if (!formats && num_formats)
754 if (formats && num_formats) {
755 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
761 kfree(info->bus_formats);
762 info->bus_formats = fmts;
763 info->num_bus_formats = num_formats;
767 EXPORT_SYMBOL(drm_display_info_set_bus_formats);
770 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
771 * @connector: connector to quwery
773 * The kernel supports per-connector configration of its consoles through
774 * use of the video= parameter. This function parses that option and
775 * extracts the user's specified mode (or enable/disable status) for a
776 * particular connector. This is typically only used during the early fbdev
779 static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
781 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
784 if (fb_get_options(connector->name, &option))
787 if (!drm_mode_parse_command_line_for_connector(option,
795 switch (mode->force) {
799 case DRM_FORCE_ON_DIGITAL:
808 DRM_INFO("forcing %s connector %s\n", connector->name, s);
809 connector->force = mode->force;
812 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
814 mode->xres, mode->yres,
815 mode->refresh_specified ? mode->refresh : 60,
816 mode->rb ? " reduced blanking" : "",
817 mode->margins ? " with margins" : "",
818 mode->interlace ? " interlaced" : "");
821 static void drm_connector_free(struct kref *kref)
823 struct drm_connector *connector =
824 container_of(kref, struct drm_connector, base.refcount);
825 struct drm_device *dev = connector->dev;
827 drm_mode_object_unregister(dev, &connector->base);
828 connector->funcs->destroy(connector);
832 * drm_connector_init - Init a preallocated connector
834 * @connector: the connector to init
835 * @funcs: callbacks for this connector
836 * @connector_type: user visible type of the connector
838 * Initialises a preallocated connector. Connectors should be
839 * subclassed as part of driver connector objects.
842 * Zero on success, error code on failure.
844 int drm_connector_init(struct drm_device *dev,
845 struct drm_connector *connector,
846 const struct drm_connector_funcs *funcs,
849 struct drm_mode_config *config = &dev->mode_config;
851 struct ida *connector_ida =
852 &drm_connector_enum_list[connector_type].ida;
854 drm_modeset_lock_all(dev);
856 ret = drm_mode_object_get_reg(dev, &connector->base,
857 DRM_MODE_OBJECT_CONNECTOR,
858 false, drm_connector_free);
862 connector->base.properties = &connector->properties;
863 connector->dev = dev;
864 connector->funcs = funcs;
866 connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
867 if (connector->connector_id < 0) {
868 ret = connector->connector_id;
872 connector->connector_type = connector_type;
873 connector->connector_type_id =
874 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
875 if (connector->connector_type_id < 0) {
876 ret = connector->connector_type_id;
880 kasprintf(GFP_KERNEL, "%s-%d",
881 drm_connector_enum_list[connector_type].name,
882 connector->connector_type_id);
883 if (!connector->name) {
885 goto out_put_type_id;
888 INIT_LIST_HEAD(&connector->probed_modes);
889 INIT_LIST_HEAD(&connector->modes);
890 connector->edid_blob_ptr = NULL;
891 connector->status = connector_status_unknown;
893 drm_connector_get_cmdline_mode(connector);
895 /* We should add connectors at the end to avoid upsetting the connector
897 list_add_tail(&connector->head, &config->connector_list);
898 config->num_connector++;
900 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
901 drm_object_attach_property(&connector->base,
902 config->edid_property,
905 drm_object_attach_property(&connector->base,
906 config->dpms_property, 0);
908 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
909 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
912 connector->debugfs_entry = NULL;
915 ida_remove(connector_ida, connector->connector_type_id);
918 ida_remove(&config->connector_ida, connector->connector_id);
921 drm_mode_object_unregister(dev, &connector->base);
924 drm_modeset_unlock_all(dev);
928 EXPORT_SYMBOL(drm_connector_init);
931 * drm_connector_cleanup - cleans up an initialised connector
932 * @connector: connector to cleanup
934 * Cleans up the connector but doesn't free the object.
936 void drm_connector_cleanup(struct drm_connector *connector)
938 struct drm_device *dev = connector->dev;
939 struct drm_display_mode *mode, *t;
941 if (connector->tile_group) {
942 drm_mode_put_tile_group(dev, connector->tile_group);
943 connector->tile_group = NULL;
946 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
947 drm_mode_remove(connector, mode);
949 list_for_each_entry_safe(mode, t, &connector->modes, head)
950 drm_mode_remove(connector, mode);
952 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
953 connector->connector_type_id);
955 ida_remove(&dev->mode_config.connector_ida,
956 connector->connector_id);
958 kfree(connector->display_info.bus_formats);
959 drm_mode_object_unregister(dev, &connector->base);
960 kfree(connector->name);
961 connector->name = NULL;
962 list_del(&connector->head);
963 dev->mode_config.num_connector--;
965 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
966 if (connector->state && connector->funcs->atomic_destroy_state)
967 connector->funcs->atomic_destroy_state(connector,
970 memset(connector, 0, sizeof(*connector));
972 EXPORT_SYMBOL(drm_connector_cleanup);
975 * drm_connector_register - register a connector
976 * @connector: the connector to register
978 * Register userspace interfaces for a connector
981 * Zero on success, error code on failure.
983 int drm_connector_register(struct drm_connector *connector)
987 ret = drm_sysfs_connector_add(connector);
991 ret = drm_debugfs_connector_add(connector);
996 if (connector->funcs->late_register) {
997 ret = connector->funcs->late_register(connector);
1002 drm_mode_object_register(connector->dev, &connector->base);
1007 drm_debugfs_connector_remove(connector);
1009 drm_sysfs_connector_remove(connector);
1012 EXPORT_SYMBOL(drm_connector_register);
1015 * drm_connector_unregister - unregister a connector
1016 * @connector: the connector to unregister
1018 * Unregister userspace interfaces for a connector
1020 void drm_connector_unregister(struct drm_connector *connector)
1022 if (connector->funcs->early_unregister)
1023 connector->funcs->early_unregister(connector);
1025 drm_sysfs_connector_remove(connector);
1026 drm_debugfs_connector_remove(connector);
1028 EXPORT_SYMBOL(drm_connector_unregister);
1031 * drm_connector_register_all - register all connectors
1034 * This function registers all connectors in sysfs and other places so that
1035 * userspace can start to access them. Drivers can call it after calling
1036 * drm_dev_register() to complete the device registration, if they don't call
1037 * drm_connector_register() on each connector individually.
1039 * When a device is unplugged and should be removed from userspace access,
1040 * call drm_connector_unregister_all(), which is the inverse of this
1044 * Zero on success, error code on failure.
1046 int drm_connector_register_all(struct drm_device *dev)
1048 struct drm_connector *connector;
1051 mutex_lock(&dev->mode_config.mutex);
1053 drm_for_each_connector(connector, dev) {
1054 ret = drm_connector_register(connector);
1059 mutex_unlock(&dev->mode_config.mutex);
1064 mutex_unlock(&dev->mode_config.mutex);
1065 drm_connector_unregister_all(dev);
1068 EXPORT_SYMBOL(drm_connector_register_all);
1071 * drm_connector_unregister_all - unregister connector userspace interfaces
1074 * This functions unregisters all connectors from sysfs and other places so
1075 * that userspace can no longer access them. Drivers should call this as the
1076 * first step tearing down the device instace, or when the underlying
1077 * physical device disappeared (e.g. USB unplug), right before calling
1078 * drm_dev_unregister().
1080 void drm_connector_unregister_all(struct drm_device *dev)
1082 struct drm_connector *connector;
1084 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
1085 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1086 drm_connector_unregister(connector);
1088 EXPORT_SYMBOL(drm_connector_unregister_all);
1091 * drm_encoder_init - Init a preallocated encoder
1093 * @encoder: the encoder to init
1094 * @funcs: callbacks for this encoder
1095 * @encoder_type: user visible type of the encoder
1096 * @name: printf style format string for the encoder name, or NULL for default name
1098 * Initialises a preallocated encoder. Encoder should be
1099 * subclassed as part of driver encoder objects.
1102 * Zero on success, error code on failure.
1104 int drm_encoder_init(struct drm_device *dev,
1105 struct drm_encoder *encoder,
1106 const struct drm_encoder_funcs *funcs,
1107 int encoder_type, const char *name, ...)
1111 drm_modeset_lock_all(dev);
1113 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
1118 encoder->encoder_type = encoder_type;
1119 encoder->funcs = funcs;
1124 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
1127 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1128 drm_encoder_enum_list[encoder_type].name,
1131 if (!encoder->name) {
1136 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
1137 encoder->index = dev->mode_config.num_encoder++;
1141 drm_mode_object_unregister(dev, &encoder->base);
1144 drm_modeset_unlock_all(dev);
1148 EXPORT_SYMBOL(drm_encoder_init);
1151 * drm_encoder_cleanup - cleans up an initialised encoder
1152 * @encoder: encoder to cleanup
1154 * Cleans up the encoder but doesn't free the object.
1156 void drm_encoder_cleanup(struct drm_encoder *encoder)
1158 struct drm_device *dev = encoder->dev;
1160 /* Note that the encoder_list is considered to be static; should we
1161 * remove the drm_encoder at runtime we would have to decrement all
1162 * the indices on the drm_encoder after us in the encoder_list.
1165 drm_modeset_lock_all(dev);
1166 drm_mode_object_unregister(dev, &encoder->base);
1167 kfree(encoder->name);
1168 list_del(&encoder->head);
1169 dev->mode_config.num_encoder--;
1170 drm_modeset_unlock_all(dev);
1172 memset(encoder, 0, sizeof(*encoder));
1174 EXPORT_SYMBOL(drm_encoder_cleanup);
1176 static unsigned int drm_num_planes(struct drm_device *dev)
1178 unsigned int num = 0;
1179 struct drm_plane *tmp;
1181 drm_for_each_plane(tmp, dev) {
1189 * drm_universal_plane_init - Initialize a new universal plane object
1191 * @plane: plane object to init
1192 * @possible_crtcs: bitmask of possible CRTCs
1193 * @funcs: callbacks for the new plane
1194 * @formats: array of supported formats (%DRM_FORMAT_*)
1195 * @format_count: number of elements in @formats
1196 * @type: type of plane (overlay, primary, cursor)
1197 * @name: printf style format string for the plane name, or NULL for default name
1199 * Initializes a plane object of type @type.
1202 * Zero on success, error code on failure.
1204 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1205 unsigned long possible_crtcs,
1206 const struct drm_plane_funcs *funcs,
1207 const uint32_t *formats, unsigned int format_count,
1208 enum drm_plane_type type,
1209 const char *name, ...)
1211 struct drm_mode_config *config = &dev->mode_config;
1214 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1218 drm_modeset_lock_init(&plane->mutex);
1220 plane->base.properties = &plane->properties;
1222 plane->funcs = funcs;
1223 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1225 if (!plane->format_types) {
1226 DRM_DEBUG_KMS("out of memory when allocating plane\n");
1227 drm_mode_object_unregister(dev, &plane->base);
1235 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1238 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1239 drm_num_planes(dev));
1242 kfree(plane->format_types);
1243 drm_mode_object_unregister(dev, &plane->base);
1247 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
1248 plane->format_count = format_count;
1249 plane->possible_crtcs = possible_crtcs;
1252 list_add_tail(&plane->head, &config->plane_list);
1253 plane->index = config->num_total_plane++;
1254 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1255 config->num_overlay_plane++;
1257 drm_object_attach_property(&plane->base,
1258 config->plane_type_property,
1261 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1262 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1263 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1264 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1265 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1266 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1267 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1268 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1269 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1270 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1271 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1276 EXPORT_SYMBOL(drm_universal_plane_init);
1279 * drm_plane_init - Initialize a legacy plane
1281 * @plane: plane object to init
1282 * @possible_crtcs: bitmask of possible CRTCs
1283 * @funcs: callbacks for the new plane
1284 * @formats: array of supported formats (%DRM_FORMAT_*)
1285 * @format_count: number of elements in @formats
1286 * @is_primary: plane type (primary vs overlay)
1288 * Legacy API to initialize a DRM plane.
1290 * New drivers should call drm_universal_plane_init() instead.
1293 * Zero on success, error code on failure.
1295 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1296 unsigned long possible_crtcs,
1297 const struct drm_plane_funcs *funcs,
1298 const uint32_t *formats, unsigned int format_count,
1301 enum drm_plane_type type;
1303 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1304 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
1305 formats, format_count, type, NULL);
1307 EXPORT_SYMBOL(drm_plane_init);
1310 * drm_plane_cleanup - Clean up the core plane usage
1311 * @plane: plane to cleanup
1313 * This function cleans up @plane and removes it from the DRM mode setting
1314 * core. Note that the function does *not* free the plane structure itself,
1315 * this is the responsibility of the caller.
1317 void drm_plane_cleanup(struct drm_plane *plane)
1319 struct drm_device *dev = plane->dev;
1321 drm_modeset_lock_all(dev);
1322 kfree(plane->format_types);
1323 drm_mode_object_unregister(dev, &plane->base);
1325 BUG_ON(list_empty(&plane->head));
1327 /* Note that the plane_list is considered to be static; should we
1328 * remove the drm_plane at runtime we would have to decrement all
1329 * the indices on the drm_plane after us in the plane_list.
1332 list_del(&plane->head);
1333 dev->mode_config.num_total_plane--;
1334 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1335 dev->mode_config.num_overlay_plane--;
1336 drm_modeset_unlock_all(dev);
1338 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1339 if (plane->state && plane->funcs->atomic_destroy_state)
1340 plane->funcs->atomic_destroy_state(plane, plane->state);
1344 memset(plane, 0, sizeof(*plane));
1346 EXPORT_SYMBOL(drm_plane_cleanup);
1349 * drm_plane_from_index - find the registered plane at an index
1351 * @idx: index of registered plane to find for
1353 * Given a plane index, return the registered plane from DRM device's
1354 * list of planes with matching index.
1357 drm_plane_from_index(struct drm_device *dev, int idx)
1359 struct drm_plane *plane;
1361 drm_for_each_plane(plane, dev)
1362 if (idx == plane->index)
1367 EXPORT_SYMBOL(drm_plane_from_index);
1370 * drm_plane_force_disable - Forcibly disable a plane
1371 * @plane: plane to disable
1373 * Forces the plane to be disabled.
1375 * Used when the plane's current framebuffer is destroyed,
1376 * and when restoring fbdev mode.
1378 void drm_plane_force_disable(struct drm_plane *plane)
1385 plane->old_fb = plane->fb;
1386 ret = plane->funcs->disable_plane(plane);
1388 DRM_ERROR("failed to disable plane with busy fb\n");
1389 plane->old_fb = NULL;
1392 /* disconnect the plane from the fb and crtc: */
1393 drm_framebuffer_unreference(plane->old_fb);
1394 plane->old_fb = NULL;
1398 EXPORT_SYMBOL(drm_plane_force_disable);
1400 static int drm_mode_create_standard_properties(struct drm_device *dev)
1402 struct drm_property *prop;
1405 * Standard properties (apply to all connectors)
1407 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
1408 DRM_MODE_PROP_IMMUTABLE,
1412 dev->mode_config.edid_property = prop;
1414 prop = drm_property_create_enum(dev, 0,
1415 "DPMS", drm_dpms_enum_list,
1416 ARRAY_SIZE(drm_dpms_enum_list));
1419 dev->mode_config.dpms_property = prop;
1421 prop = drm_property_create(dev,
1422 DRM_MODE_PROP_BLOB |
1423 DRM_MODE_PROP_IMMUTABLE,
1427 dev->mode_config.path_property = prop;
1429 prop = drm_property_create(dev,
1430 DRM_MODE_PROP_BLOB |
1431 DRM_MODE_PROP_IMMUTABLE,
1435 dev->mode_config.tile_property = prop;
1437 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1438 "type", drm_plane_type_enum_list,
1439 ARRAY_SIZE(drm_plane_type_enum_list));
1442 dev->mode_config.plane_type_property = prop;
1444 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1445 "SRC_X", 0, UINT_MAX);
1448 dev->mode_config.prop_src_x = prop;
1450 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1451 "SRC_Y", 0, UINT_MAX);
1454 dev->mode_config.prop_src_y = prop;
1456 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1457 "SRC_W", 0, UINT_MAX);
1460 dev->mode_config.prop_src_w = prop;
1462 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1463 "SRC_H", 0, UINT_MAX);
1466 dev->mode_config.prop_src_h = prop;
1468 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1469 "CRTC_X", INT_MIN, INT_MAX);
1472 dev->mode_config.prop_crtc_x = prop;
1474 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1475 "CRTC_Y", INT_MIN, INT_MAX);
1478 dev->mode_config.prop_crtc_y = prop;
1480 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1481 "CRTC_W", 0, INT_MAX);
1484 dev->mode_config.prop_crtc_w = prop;
1486 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1487 "CRTC_H", 0, INT_MAX);
1490 dev->mode_config.prop_crtc_h = prop;
1492 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1493 "FB_ID", DRM_MODE_OBJECT_FB);
1496 dev->mode_config.prop_fb_id = prop;
1498 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1499 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1502 dev->mode_config.prop_crtc_id = prop;
1504 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1508 dev->mode_config.prop_active = prop;
1510 prop = drm_property_create(dev,
1511 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1515 dev->mode_config.prop_mode_id = prop;
1517 prop = drm_property_create(dev,
1522 dev->mode_config.degamma_lut_property = prop;
1524 prop = drm_property_create_range(dev,
1525 DRM_MODE_PROP_IMMUTABLE,
1526 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1529 dev->mode_config.degamma_lut_size_property = prop;
1531 prop = drm_property_create(dev,
1536 dev->mode_config.ctm_property = prop;
1538 prop = drm_property_create(dev,
1543 dev->mode_config.gamma_lut_property = prop;
1545 prop = drm_property_create_range(dev,
1546 DRM_MODE_PROP_IMMUTABLE,
1547 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1550 dev->mode_config.gamma_lut_size_property = prop;
1556 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1559 * Called by a driver the first time a DVI-I connector is made.
1561 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1563 struct drm_property *dvi_i_selector;
1564 struct drm_property *dvi_i_subconnector;
1566 if (dev->mode_config.dvi_i_select_subconnector_property)
1570 drm_property_create_enum(dev, 0,
1571 "select subconnector",
1572 drm_dvi_i_select_enum_list,
1573 ARRAY_SIZE(drm_dvi_i_select_enum_list));
1574 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1576 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1578 drm_dvi_i_subconnector_enum_list,
1579 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
1580 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1584 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1587 * drm_create_tv_properties - create TV specific connector properties
1589 * @num_modes: number of different TV formats (modes) supported
1590 * @modes: array of pointers to strings containing name of each format
1592 * Called by a driver's TV initialization routine, this function creates
1593 * the TV specific connector properties for a given device. Caller is
1594 * responsible for allocating a list of format names and passing them to
1597 int drm_mode_create_tv_properties(struct drm_device *dev,
1598 unsigned int num_modes,
1599 const char * const modes[])
1601 struct drm_property *tv_selector;
1602 struct drm_property *tv_subconnector;
1605 if (dev->mode_config.tv_select_subconnector_property)
1609 * Basic connector properties
1611 tv_selector = drm_property_create_enum(dev, 0,
1612 "select subconnector",
1613 drm_tv_select_enum_list,
1614 ARRAY_SIZE(drm_tv_select_enum_list));
1618 dev->mode_config.tv_select_subconnector_property = tv_selector;
1621 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1623 drm_tv_subconnector_enum_list,
1624 ARRAY_SIZE(drm_tv_subconnector_enum_list));
1625 if (!tv_subconnector)
1627 dev->mode_config.tv_subconnector_property = tv_subconnector;
1630 * Other, TV specific properties: margins & TV modes.
1632 dev->mode_config.tv_left_margin_property =
1633 drm_property_create_range(dev, 0, "left margin", 0, 100);
1634 if (!dev->mode_config.tv_left_margin_property)
1637 dev->mode_config.tv_right_margin_property =
1638 drm_property_create_range(dev, 0, "right margin", 0, 100);
1639 if (!dev->mode_config.tv_right_margin_property)
1642 dev->mode_config.tv_top_margin_property =
1643 drm_property_create_range(dev, 0, "top margin", 0, 100);
1644 if (!dev->mode_config.tv_top_margin_property)
1647 dev->mode_config.tv_bottom_margin_property =
1648 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
1649 if (!dev->mode_config.tv_bottom_margin_property)
1652 dev->mode_config.tv_mode_property =
1653 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1655 if (!dev->mode_config.tv_mode_property)
1658 for (i = 0; i < num_modes; i++)
1659 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1662 dev->mode_config.tv_brightness_property =
1663 drm_property_create_range(dev, 0, "brightness", 0, 100);
1664 if (!dev->mode_config.tv_brightness_property)
1667 dev->mode_config.tv_contrast_property =
1668 drm_property_create_range(dev, 0, "contrast", 0, 100);
1669 if (!dev->mode_config.tv_contrast_property)
1672 dev->mode_config.tv_flicker_reduction_property =
1673 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
1674 if (!dev->mode_config.tv_flicker_reduction_property)
1677 dev->mode_config.tv_overscan_property =
1678 drm_property_create_range(dev, 0, "overscan", 0, 100);
1679 if (!dev->mode_config.tv_overscan_property)
1682 dev->mode_config.tv_saturation_property =
1683 drm_property_create_range(dev, 0, "saturation", 0, 100);
1684 if (!dev->mode_config.tv_saturation_property)
1687 dev->mode_config.tv_hue_property =
1688 drm_property_create_range(dev, 0, "hue", 0, 100);
1689 if (!dev->mode_config.tv_hue_property)
1696 EXPORT_SYMBOL(drm_mode_create_tv_properties);
1699 * drm_mode_create_scaling_mode_property - create scaling mode property
1702 * Called by a driver the first time it's needed, must be attached to desired
1705 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1707 struct drm_property *scaling_mode;
1709 if (dev->mode_config.scaling_mode_property)
1713 drm_property_create_enum(dev, 0, "scaling mode",
1714 drm_scaling_mode_enum_list,
1715 ARRAY_SIZE(drm_scaling_mode_enum_list));
1717 dev->mode_config.scaling_mode_property = scaling_mode;
1721 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1724 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1727 * Called by a driver the first time it's needed, must be attached to desired
1731 * Zero on success, negative errno on failure.
1733 int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1735 if (dev->mode_config.aspect_ratio_property)
1738 dev->mode_config.aspect_ratio_property =
1739 drm_property_create_enum(dev, 0, "aspect ratio",
1740 drm_aspect_ratio_enum_list,
1741 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1743 if (dev->mode_config.aspect_ratio_property == NULL)
1748 EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1751 * drm_mode_create_dirty_property - create dirty property
1754 * Called by a driver the first time it's needed, must be attached to desired
1757 int drm_mode_create_dirty_info_property(struct drm_device *dev)
1759 struct drm_property *dirty_info;
1761 if (dev->mode_config.dirty_info_property)
1765 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1767 drm_dirty_info_enum_list,
1768 ARRAY_SIZE(drm_dirty_info_enum_list));
1769 dev->mode_config.dirty_info_property = dirty_info;
1773 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1776 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1779 * Create the the suggested x/y offset property for connectors.
1781 int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1783 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1786 dev->mode_config.suggested_x_property =
1787 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1789 dev->mode_config.suggested_y_property =
1790 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1792 if (dev->mode_config.suggested_x_property == NULL ||
1793 dev->mode_config.suggested_y_property == NULL)
1797 EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1800 * drm_mode_getresources - get graphics configuration
1801 * @dev: drm device for the ioctl
1802 * @data: data pointer for the ioctl
1803 * @file_priv: drm file for the ioctl call
1805 * Construct a set of configuration description structures and return
1806 * them to the user, including CRTC, connector and framebuffer configuration.
1808 * Called by the user via ioctl.
1811 * Zero on success, negative errno on failure.
1813 int drm_mode_getresources(struct drm_device *dev, void *data,
1814 struct drm_file *file_priv)
1816 struct drm_mode_card_res *card_res = data;
1817 struct list_head *lh;
1818 struct drm_framebuffer *fb;
1819 struct drm_connector *connector;
1820 struct drm_crtc *crtc;
1821 struct drm_encoder *encoder;
1823 int connector_count = 0;
1826 int encoder_count = 0;
1828 uint32_t __user *fb_id;
1829 uint32_t __user *crtc_id;
1830 uint32_t __user *connector_id;
1831 uint32_t __user *encoder_id;
1833 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1837 mutex_lock(&file_priv->fbs_lock);
1839 * For the non-control nodes we need to limit the list of resources
1840 * by IDs in the group list for this node
1842 list_for_each(lh, &file_priv->fbs)
1845 /* handle this in 4 parts */
1847 if (card_res->count_fbs >= fb_count) {
1849 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1850 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1851 if (put_user(fb->base.id, fb_id + copied)) {
1852 mutex_unlock(&file_priv->fbs_lock);
1858 card_res->count_fbs = fb_count;
1859 mutex_unlock(&file_priv->fbs_lock);
1861 /* mode_config.mutex protects the connector list against e.g. DP MST
1862 * connector hot-adding. CRTC/Plane lists are invariant. */
1863 mutex_lock(&dev->mode_config.mutex);
1864 drm_for_each_crtc(crtc, dev)
1867 drm_for_each_connector(connector, dev)
1870 drm_for_each_encoder(encoder, dev)
1873 card_res->max_height = dev->mode_config.max_height;
1874 card_res->min_height = dev->mode_config.min_height;
1875 card_res->max_width = dev->mode_config.max_width;
1876 card_res->min_width = dev->mode_config.min_width;
1879 if (card_res->count_crtcs >= crtc_count) {
1881 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1882 drm_for_each_crtc(crtc, dev) {
1883 if (put_user(crtc->base.id, crtc_id + copied)) {
1890 card_res->count_crtcs = crtc_count;
1893 if (card_res->count_encoders >= encoder_count) {
1895 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1896 drm_for_each_encoder(encoder, dev) {
1897 if (put_user(encoder->base.id, encoder_id +
1905 card_res->count_encoders = encoder_count;
1908 if (card_res->count_connectors >= connector_count) {
1910 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1911 drm_for_each_connector(connector, dev) {
1912 if (put_user(connector->base.id,
1913 connector_id + copied)) {
1920 card_res->count_connectors = connector_count;
1923 mutex_unlock(&dev->mode_config.mutex);
1928 * drm_mode_getcrtc - get CRTC configuration
1929 * @dev: drm device for the ioctl
1930 * @data: data pointer for the ioctl
1931 * @file_priv: drm file for the ioctl call
1933 * Construct a CRTC configuration structure to return to the user.
1935 * Called by the user via ioctl.
1938 * Zero on success, negative errno on failure.
1940 int drm_mode_getcrtc(struct drm_device *dev,
1941 void *data, struct drm_file *file_priv)
1943 struct drm_mode_crtc *crtc_resp = data;
1944 struct drm_crtc *crtc;
1946 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1949 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
1953 drm_modeset_lock_crtc(crtc, crtc->primary);
1954 crtc_resp->gamma_size = crtc->gamma_size;
1955 if (crtc->primary->fb)
1956 crtc_resp->fb_id = crtc->primary->fb->base.id;
1958 crtc_resp->fb_id = 0;
1961 crtc_resp->x = crtc->primary->state->src_x >> 16;
1962 crtc_resp->y = crtc->primary->state->src_y >> 16;
1963 if (crtc->state->enable) {
1964 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
1965 crtc_resp->mode_valid = 1;
1968 crtc_resp->mode_valid = 0;
1971 crtc_resp->x = crtc->x;
1972 crtc_resp->y = crtc->y;
1973 if (crtc->enabled) {
1974 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1975 crtc_resp->mode_valid = 1;
1978 crtc_resp->mode_valid = 0;
1981 drm_modeset_unlock_crtc(crtc);
1986 static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1987 const struct drm_file *file_priv)
1990 * If user-space hasn't configured the driver to expose the stereo 3D
1991 * modes, don't expose them.
1993 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1999 static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
2001 /* For atomic drivers only state objects are synchronously updated and
2002 * protected by modeset locks, so check those first. */
2003 if (connector->state)
2004 return connector->state->best_encoder;
2005 return connector->encoder;
2008 /* helper for getconnector and getproperties ioctls */
2009 static int get_properties(struct drm_mode_object *obj, bool atomic,
2010 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
2011 uint32_t *arg_count_props)
2016 props_count = obj->properties->count;
2018 props_count -= obj->properties->atomic_count;
2020 if ((*arg_count_props >= props_count) && props_count) {
2021 for (i = 0, copied = 0; copied < props_count; i++) {
2022 struct drm_property *prop = obj->properties->properties[i];
2025 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
2028 ret = drm_object_property_get_value(obj, prop, &val);
2032 if (put_user(prop->base.id, prop_ptr + copied))
2035 if (put_user(val, prop_values + copied))
2041 *arg_count_props = props_count;
2047 * drm_mode_getconnector - get connector configuration
2048 * @dev: drm device for the ioctl
2049 * @data: data pointer for the ioctl
2050 * @file_priv: drm file for the ioctl call
2052 * Construct a connector configuration structure to return to the user.
2054 * Called by the user via ioctl.
2057 * Zero on success, negative errno on failure.
2059 int drm_mode_getconnector(struct drm_device *dev, void *data,
2060 struct drm_file *file_priv)
2062 struct drm_mode_get_connector *out_resp = data;
2063 struct drm_connector *connector;
2064 struct drm_encoder *encoder;
2065 struct drm_display_mode *mode;
2067 int encoders_count = 0;
2071 struct drm_mode_modeinfo u_mode;
2072 struct drm_mode_modeinfo __user *mode_ptr;
2073 uint32_t __user *encoder_ptr;
2075 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2078 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
2080 mutex_lock(&dev->mode_config.mutex);
2082 connector = drm_connector_lookup(dev, out_resp->connector_id);
2088 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2089 if (connector->encoder_ids[i] != 0)
2092 if (out_resp->count_modes == 0) {
2093 connector->funcs->fill_modes(connector,
2094 dev->mode_config.max_width,
2095 dev->mode_config.max_height);
2098 /* delayed so we get modes regardless of pre-fill_modes state */
2099 list_for_each_entry(mode, &connector->modes, head)
2100 if (drm_mode_expose_to_userspace(mode, file_priv))
2103 out_resp->connector_id = connector->base.id;
2104 out_resp->connector_type = connector->connector_type;
2105 out_resp->connector_type_id = connector->connector_type_id;
2106 out_resp->mm_width = connector->display_info.width_mm;
2107 out_resp->mm_height = connector->display_info.height_mm;
2108 out_resp->subpixel = connector->display_info.subpixel_order;
2109 out_resp->connection = connector->status;
2111 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2112 encoder = drm_connector_get_encoder(connector);
2114 out_resp->encoder_id = encoder->base.id;
2116 out_resp->encoder_id = 0;
2119 * This ioctl is called twice, once to determine how much space is
2120 * needed, and the 2nd time to fill it.
2122 if ((out_resp->count_modes >= mode_count) && mode_count) {
2124 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
2125 list_for_each_entry(mode, &connector->modes, head) {
2126 if (!drm_mode_expose_to_userspace(mode, file_priv))
2129 drm_mode_convert_to_umode(&u_mode, mode);
2130 if (copy_to_user(mode_ptr + copied,
2131 &u_mode, sizeof(u_mode))) {
2138 out_resp->count_modes = mode_count;
2140 ret = get_properties(&connector->base, file_priv->atomic,
2141 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2142 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2143 &out_resp->count_props);
2147 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2149 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
2150 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2151 if (connector->encoder_ids[i] != 0) {
2152 if (put_user(connector->encoder_ids[i],
2153 encoder_ptr + copied)) {
2161 out_resp->count_encoders = encoders_count;
2164 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2166 drm_connector_unreference(connector);
2168 mutex_unlock(&dev->mode_config.mutex);
2173 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2175 struct drm_connector *connector;
2176 struct drm_device *dev = encoder->dev;
2177 bool uses_atomic = false;
2179 /* For atomic drivers only state objects are synchronously updated and
2180 * protected by modeset locks, so check those first. */
2181 drm_for_each_connector(connector, dev) {
2182 if (!connector->state)
2187 if (connector->state->best_encoder != encoder)
2190 return connector->state->crtc;
2193 /* Don't return stale data (e.g. pending async disable). */
2197 return encoder->crtc;
2201 * drm_mode_getencoder - get encoder configuration
2202 * @dev: drm device for the ioctl
2203 * @data: data pointer for the ioctl
2204 * @file_priv: drm file for the ioctl call
2206 * Construct a encoder configuration structure to return to the user.
2208 * Called by the user via ioctl.
2211 * Zero on success, negative errno on failure.
2213 int drm_mode_getencoder(struct drm_device *dev, void *data,
2214 struct drm_file *file_priv)
2216 struct drm_mode_get_encoder *enc_resp = data;
2217 struct drm_encoder *encoder;
2218 struct drm_crtc *crtc;
2220 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2223 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
2227 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
2228 crtc = drm_encoder_get_crtc(encoder);
2230 enc_resp->crtc_id = crtc->base.id;
2232 enc_resp->crtc_id = 0;
2233 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2235 enc_resp->encoder_type = encoder->encoder_type;
2236 enc_resp->encoder_id = encoder->base.id;
2237 enc_resp->possible_crtcs = encoder->possible_crtcs;
2238 enc_resp->possible_clones = encoder->possible_clones;
2244 * drm_mode_getplane_res - enumerate all plane resources
2247 * @file_priv: DRM file info
2249 * Construct a list of plane ids to return to the user.
2251 * Called by the user via ioctl.
2254 * Zero on success, negative errno on failure.
2256 int drm_mode_getplane_res(struct drm_device *dev, void *data,
2257 struct drm_file *file_priv)
2259 struct drm_mode_get_plane_res *plane_resp = data;
2260 struct drm_mode_config *config;
2261 struct drm_plane *plane;
2262 uint32_t __user *plane_ptr;
2264 unsigned num_planes;
2266 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2269 config = &dev->mode_config;
2271 if (file_priv->universal_planes)
2272 num_planes = config->num_total_plane;
2274 num_planes = config->num_overlay_plane;
2277 * This ioctl is called twice, once to determine how much space is
2278 * needed, and the 2nd time to fill it.
2281 (plane_resp->count_planes >= num_planes)) {
2282 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
2284 /* Plane lists are invariant, no locking needed. */
2285 drm_for_each_plane(plane, dev) {
2287 * Unless userspace set the 'universal planes'
2288 * capability bit, only advertise overlays.
2290 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2291 !file_priv->universal_planes)
2294 if (put_user(plane->base.id, plane_ptr + copied))
2299 plane_resp->count_planes = num_planes;
2305 * drm_mode_getplane - get plane configuration
2308 * @file_priv: DRM file info
2310 * Construct a plane configuration structure to return to the user.
2312 * Called by the user via ioctl.
2315 * Zero on success, negative errno on failure.
2317 int drm_mode_getplane(struct drm_device *dev, void *data,
2318 struct drm_file *file_priv)
2320 struct drm_mode_get_plane *plane_resp = data;
2321 struct drm_plane *plane;
2322 uint32_t __user *format_ptr;
2324 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2327 plane = drm_plane_find(dev, plane_resp->plane_id);
2331 drm_modeset_lock(&plane->mutex, NULL);
2333 plane_resp->crtc_id = plane->crtc->base.id;
2335 plane_resp->crtc_id = 0;
2338 plane_resp->fb_id = plane->fb->base.id;
2340 plane_resp->fb_id = 0;
2341 drm_modeset_unlock(&plane->mutex);
2343 plane_resp->plane_id = plane->base.id;
2344 plane_resp->possible_crtcs = plane->possible_crtcs;
2345 plane_resp->gamma_size = 0;
2348 * This ioctl is called twice, once to determine how much space is
2349 * needed, and the 2nd time to fill it.
2351 if (plane->format_count &&
2352 (plane_resp->count_format_types >= plane->format_count)) {
2353 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
2354 if (copy_to_user(format_ptr,
2355 plane->format_types,
2356 sizeof(uint32_t) * plane->format_count)) {
2360 plane_resp->count_format_types = plane->format_count;
2366 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2367 * @plane: plane to check for format support
2368 * @format: the pixel format
2371 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2374 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2378 for (i = 0; i < plane->format_count; i++) {
2379 if (format == plane->format_types[i])
2386 static int check_src_coords(uint32_t src_x, uint32_t src_y,
2387 uint32_t src_w, uint32_t src_h,
2388 const struct drm_framebuffer *fb)
2390 unsigned int fb_width, fb_height;
2392 fb_width = fb->width << 16;
2393 fb_height = fb->height << 16;
2395 /* Make sure source coordinates are inside the fb. */
2396 if (src_w > fb_width ||
2397 src_x > fb_width - src_w ||
2398 src_h > fb_height ||
2399 src_y > fb_height - src_h) {
2400 DRM_DEBUG_KMS("Invalid source coordinates "
2401 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2402 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2403 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2404 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2405 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2413 * setplane_internal - setplane handler for internal callers
2415 * Note that we assume an extra reference has already been taken on fb. If the
2416 * update fails, this reference will be dropped before return; if it succeeds,
2417 * the previous framebuffer (if any) will be unreferenced instead.
2419 * src_{x,y,w,h} are provided in 16.16 fixed point format
2421 static int __setplane_internal(struct drm_plane *plane,
2422 struct drm_crtc *crtc,
2423 struct drm_framebuffer *fb,
2424 int32_t crtc_x, int32_t crtc_y,
2425 uint32_t crtc_w, uint32_t crtc_h,
2426 /* src_{x,y,w,h} values are 16.16 fixed point */
2427 uint32_t src_x, uint32_t src_y,
2428 uint32_t src_w, uint32_t src_h)
2432 /* No fb means shut it down */
2434 plane->old_fb = plane->fb;
2435 ret = plane->funcs->disable_plane(plane);
2440 plane->old_fb = NULL;
2445 /* Check whether this plane is usable on this CRTC */
2446 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2447 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2452 /* Check whether this plane supports the fb pixel format. */
2453 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2455 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2456 drm_get_format_name(fb->pixel_format));
2460 /* Give drivers some help against integer overflows */
2461 if (crtc_w > INT_MAX ||
2462 crtc_x > INT_MAX - (int32_t) crtc_w ||
2464 crtc_y > INT_MAX - (int32_t) crtc_h) {
2465 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2466 crtc_w, crtc_h, crtc_x, crtc_y);
2471 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2475 plane->old_fb = plane->fb;
2476 ret = plane->funcs->update_plane(plane, crtc, fb,
2477 crtc_x, crtc_y, crtc_w, crtc_h,
2478 src_x, src_y, src_w, src_h);
2484 plane->old_fb = NULL;
2489 drm_framebuffer_unreference(fb);
2491 drm_framebuffer_unreference(plane->old_fb);
2492 plane->old_fb = NULL;
2497 static int setplane_internal(struct drm_plane *plane,
2498 struct drm_crtc *crtc,
2499 struct drm_framebuffer *fb,
2500 int32_t crtc_x, int32_t crtc_y,
2501 uint32_t crtc_w, uint32_t crtc_h,
2502 /* src_{x,y,w,h} values are 16.16 fixed point */
2503 uint32_t src_x, uint32_t src_y,
2504 uint32_t src_w, uint32_t src_h)
2508 drm_modeset_lock_all(plane->dev);
2509 ret = __setplane_internal(plane, crtc, fb,
2510 crtc_x, crtc_y, crtc_w, crtc_h,
2511 src_x, src_y, src_w, src_h);
2512 drm_modeset_unlock_all(plane->dev);
2518 * drm_mode_setplane - configure a plane's configuration
2520 * @data: ioctl data*
2521 * @file_priv: DRM file info
2523 * Set plane configuration, including placement, fb, scaling, and other factors.
2524 * Or pass a NULL fb to disable (planes may be disabled without providing a
2528 * Zero on success, negative errno on failure.
2530 int drm_mode_setplane(struct drm_device *dev, void *data,
2531 struct drm_file *file_priv)
2533 struct drm_mode_set_plane *plane_req = data;
2534 struct drm_plane *plane;
2535 struct drm_crtc *crtc = NULL;
2536 struct drm_framebuffer *fb = NULL;
2538 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2542 * First, find the plane, crtc, and fb objects. If not available,
2543 * we don't bother to call the driver.
2545 plane = drm_plane_find(dev, plane_req->plane_id);
2547 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2548 plane_req->plane_id);
2552 if (plane_req->fb_id) {
2553 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2555 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2560 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2562 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2563 plane_req->crtc_id);
2569 * setplane_internal will take care of deref'ing either the old or new
2570 * framebuffer depending on success.
2572 return setplane_internal(plane, crtc, fb,
2573 plane_req->crtc_x, plane_req->crtc_y,
2574 plane_req->crtc_w, plane_req->crtc_h,
2575 plane_req->src_x, plane_req->src_y,
2576 plane_req->src_w, plane_req->src_h);
2580 * drm_mode_set_config_internal - helper to call ->set_config
2581 * @set: modeset config to set
2583 * This is a little helper to wrap internal calls to the ->set_config driver
2584 * interface. The only thing it adds is correct refcounting dance.
2587 * Zero on success, negative errno on failure.
2589 int drm_mode_set_config_internal(struct drm_mode_set *set)
2591 struct drm_crtc *crtc = set->crtc;
2592 struct drm_framebuffer *fb;
2593 struct drm_crtc *tmp;
2597 * NOTE: ->set_config can also disable other crtcs (if we steal all
2598 * connectors from it), hence we need to refcount the fbs across all
2599 * crtcs. Atomic modeset will have saner semantics ...
2601 drm_for_each_crtc(tmp, crtc->dev)
2602 tmp->primary->old_fb = tmp->primary->fb;
2606 ret = crtc->funcs->set_config(set);
2608 crtc->primary->crtc = crtc;
2609 crtc->primary->fb = fb;
2612 drm_for_each_crtc(tmp, crtc->dev) {
2613 if (tmp->primary->fb)
2614 drm_framebuffer_reference(tmp->primary->fb);
2615 if (tmp->primary->old_fb)
2616 drm_framebuffer_unreference(tmp->primary->old_fb);
2617 tmp->primary->old_fb = NULL;
2622 EXPORT_SYMBOL(drm_mode_set_config_internal);
2625 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2626 * @mode: mode to query
2627 * @hdisplay: hdisplay value to fill in
2628 * @vdisplay: vdisplay value to fill in
2630 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2631 * the appropriate layout.
2633 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2634 int *hdisplay, int *vdisplay)
2636 struct drm_display_mode adjusted;
2638 drm_mode_copy(&adjusted, mode);
2639 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2640 *hdisplay = adjusted.crtc_hdisplay;
2641 *vdisplay = adjusted.crtc_vdisplay;
2643 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2646 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2648 * @crtc: CRTC that framebuffer will be displayed on
2651 * @mode: mode that framebuffer will be displayed under
2652 * @fb: framebuffer to check size of
2654 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2656 const struct drm_display_mode *mode,
2657 const struct drm_framebuffer *fb)
2660 int hdisplay, vdisplay;
2662 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
2665 crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
2666 BIT(DRM_ROTATE_270)))
2667 swap(hdisplay, vdisplay);
2669 return check_src_coords(x << 16, y << 16,
2670 hdisplay << 16, vdisplay << 16, fb);
2672 EXPORT_SYMBOL(drm_crtc_check_viewport);
2675 * drm_mode_setcrtc - set CRTC configuration
2676 * @dev: drm device for the ioctl
2677 * @data: data pointer for the ioctl
2678 * @file_priv: drm file for the ioctl call
2680 * Build a new CRTC configuration based on user request.
2682 * Called by the user via ioctl.
2685 * Zero on success, negative errno on failure.
2687 int drm_mode_setcrtc(struct drm_device *dev, void *data,
2688 struct drm_file *file_priv)
2690 struct drm_mode_config *config = &dev->mode_config;
2691 struct drm_mode_crtc *crtc_req = data;
2692 struct drm_crtc *crtc;
2693 struct drm_connector **connector_set = NULL, *connector;
2694 struct drm_framebuffer *fb = NULL;
2695 struct drm_display_mode *mode = NULL;
2696 struct drm_mode_set set;
2697 uint32_t __user *set_connectors_ptr;
2701 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2705 * Universal plane src offsets are only 16.16, prevent havoc for
2706 * drivers using universal plane code internally.
2708 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
2711 drm_modeset_lock_all(dev);
2712 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2714 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
2718 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
2720 if (crtc_req->mode_valid) {
2721 /* If we have a mode we need a framebuffer. */
2722 /* If we pass -1, set the mode with the currently bound fb */
2723 if (crtc_req->fb_id == -1) {
2724 if (!crtc->primary->fb) {
2725 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2729 fb = crtc->primary->fb;
2730 /* Make refcounting symmetric with the lookup path. */
2731 drm_framebuffer_reference(fb);
2733 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2735 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2742 mode = drm_mode_create(dev);
2748 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
2750 DRM_DEBUG_KMS("Invalid mode\n");
2755 * Check whether the primary plane supports the fb pixel format.
2756 * Drivers not implementing the universal planes API use a
2757 * default formats list provided by the DRM core which doesn't
2758 * match real hardware capabilities. Skip the check in that
2761 if (!crtc->primary->format_default) {
2762 ret = drm_plane_check_pixel_format(crtc->primary,
2765 DRM_DEBUG_KMS("Invalid pixel format %s\n",
2766 drm_get_format_name(fb->pixel_format));
2771 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2778 if (crtc_req->count_connectors == 0 && mode) {
2779 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
2784 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
2785 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
2786 crtc_req->count_connectors);
2791 if (crtc_req->count_connectors > 0) {
2794 /* Avoid unbounded kernel memory allocation */
2795 if (crtc_req->count_connectors > config->num_connector) {
2800 connector_set = kmalloc_array(crtc_req->count_connectors,
2801 sizeof(struct drm_connector *),
2803 if (!connector_set) {
2808 for (i = 0; i < crtc_req->count_connectors; i++) {
2809 connector_set[i] = NULL;
2810 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
2811 if (get_user(out_id, &set_connectors_ptr[i])) {
2816 connector = drm_connector_lookup(dev, out_id);
2818 DRM_DEBUG_KMS("Connector id %d unknown\n",
2823 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2827 connector_set[i] = connector;
2832 set.x = crtc_req->x;
2833 set.y = crtc_req->y;
2835 set.connectors = connector_set;
2836 set.num_connectors = crtc_req->count_connectors;
2838 ret = drm_mode_set_config_internal(&set);
2842 drm_framebuffer_unreference(fb);
2844 if (connector_set) {
2845 for (i = 0; i < crtc_req->count_connectors; i++) {
2846 if (connector_set[i])
2847 drm_connector_unreference(connector_set[i]);
2850 kfree(connector_set);
2851 drm_mode_destroy(dev, mode);
2852 drm_modeset_unlock_all(dev);
2857 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2858 * universal plane handler call
2859 * @crtc: crtc to update cursor for
2860 * @req: data pointer for the ioctl
2861 * @file_priv: drm file for the ioctl call
2863 * Legacy cursor ioctl's work directly with driver buffer handles. To
2864 * translate legacy ioctl calls into universal plane handler calls, we need to
2865 * wrap the native buffer handle in a drm_framebuffer.
2867 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2868 * buffer with a pitch of 4*width; the universal plane interface should be used
2869 * directly in cases where the hardware can support other buffer settings and
2870 * userspace wants to make use of these capabilities.
2873 * Zero on success, negative errno on failure.
2875 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2876 struct drm_mode_cursor2 *req,
2877 struct drm_file *file_priv)
2879 struct drm_device *dev = crtc->dev;
2880 struct drm_framebuffer *fb = NULL;
2881 struct drm_mode_fb_cmd2 fbreq = {
2882 .width = req->width,
2883 .height = req->height,
2884 .pixel_format = DRM_FORMAT_ARGB8888,
2885 .pitches = { req->width * 4 },
2886 .handles = { req->handle },
2888 int32_t crtc_x, crtc_y;
2889 uint32_t crtc_w = 0, crtc_h = 0;
2890 uint32_t src_w = 0, src_h = 0;
2893 BUG_ON(!crtc->cursor);
2894 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
2897 * Obtain fb we'll be using (either new or existing) and take an extra
2898 * reference to it if fb != null. setplane will take care of dropping
2899 * the reference if the plane update fails.
2901 if (req->flags & DRM_MODE_CURSOR_BO) {
2903 fb = internal_framebuffer_create(dev, &fbreq, file_priv);
2905 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2908 fb->hot_x = req->hot_x;
2909 fb->hot_y = req->hot_y;
2914 fb = crtc->cursor->fb;
2916 drm_framebuffer_reference(fb);
2919 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2923 crtc_x = crtc->cursor_x;
2924 crtc_y = crtc->cursor_y;
2929 crtc_h = fb->height;
2930 src_w = fb->width << 16;
2931 src_h = fb->height << 16;
2935 * setplane_internal will take care of deref'ing either the old or new
2936 * framebuffer depending on success.
2938 ret = __setplane_internal(crtc->cursor, crtc, fb,
2939 crtc_x, crtc_y, crtc_w, crtc_h,
2940 0, 0, src_w, src_h);
2942 /* Update successful; save new cursor position, if necessary */
2943 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2944 crtc->cursor_x = req->x;
2945 crtc->cursor_y = req->y;
2951 static int drm_mode_cursor_common(struct drm_device *dev,
2952 struct drm_mode_cursor2 *req,
2953 struct drm_file *file_priv)
2955 struct drm_crtc *crtc;
2958 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2961 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
2964 crtc = drm_crtc_find(dev, req->crtc_id);
2966 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
2971 * If this crtc has a universal cursor plane, call that plane's update
2972 * handler rather than using legacy cursor handlers.
2974 drm_modeset_lock_crtc(crtc, crtc->cursor);
2976 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2980 if (req->flags & DRM_MODE_CURSOR_BO) {
2981 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
2985 /* Turns off the cursor if handle is 0 */
2986 if (crtc->funcs->cursor_set2)
2987 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2988 req->width, req->height, req->hot_x, req->hot_y);
2990 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2991 req->width, req->height);
2994 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2995 if (crtc->funcs->cursor_move) {
2996 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
3003 drm_modeset_unlock_crtc(crtc);
3011 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
3012 * @dev: drm device for the ioctl
3013 * @data: data pointer for the ioctl
3014 * @file_priv: drm file for the ioctl call
3016 * Set the cursor configuration based on user request.
3018 * Called by the user via ioctl.
3021 * Zero on success, negative errno on failure.
3023 int drm_mode_cursor_ioctl(struct drm_device *dev,
3024 void *data, struct drm_file *file_priv)
3026 struct drm_mode_cursor *req = data;
3027 struct drm_mode_cursor2 new_req;
3029 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
3030 new_req.hot_x = new_req.hot_y = 0;
3032 return drm_mode_cursor_common(dev, &new_req, file_priv);
3036 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
3037 * @dev: drm device for the ioctl
3038 * @data: data pointer for the ioctl
3039 * @file_priv: drm file for the ioctl call
3041 * Set the cursor configuration based on user request. This implements the 2nd
3042 * version of the cursor ioctl, which allows userspace to additionally specify
3043 * the hotspot of the pointer.
3045 * Called by the user via ioctl.
3048 * Zero on success, negative errno on failure.
3050 int drm_mode_cursor2_ioctl(struct drm_device *dev,
3051 void *data, struct drm_file *file_priv)
3053 struct drm_mode_cursor2 *req = data;
3055 return drm_mode_cursor_common(dev, req, file_priv);
3059 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
3060 * @bpp: bits per pixels
3061 * @depth: bit depth per pixel
3063 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
3064 * Useful in fbdev emulation code, since that deals in those values.
3066 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
3072 fmt = DRM_FORMAT_C8;
3076 fmt = DRM_FORMAT_XRGB1555;
3078 fmt = DRM_FORMAT_RGB565;
3081 fmt = DRM_FORMAT_RGB888;
3085 fmt = DRM_FORMAT_XRGB8888;
3086 else if (depth == 30)
3087 fmt = DRM_FORMAT_XRGB2101010;
3089 fmt = DRM_FORMAT_ARGB8888;
3092 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3093 fmt = DRM_FORMAT_XRGB8888;
3099 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3102 * drm_mode_addfb - add an FB to the graphics configuration
3103 * @dev: drm device for the ioctl
3104 * @data: data pointer for the ioctl
3105 * @file_priv: drm file for the ioctl call
3107 * Add a new FB to the specified CRTC, given a user request. This is the
3108 * original addfb ioctl which only supported RGB formats.
3110 * Called by the user via ioctl.
3113 * Zero on success, negative errno on failure.
3115 int drm_mode_addfb(struct drm_device *dev,
3116 void *data, struct drm_file *file_priv)
3118 struct drm_mode_fb_cmd *or = data;
3119 struct drm_mode_fb_cmd2 r = {};
3122 /* convert to new format and call new ioctl */
3123 r.fb_id = or->fb_id;
3124 r.width = or->width;
3125 r.height = or->height;
3126 r.pitches[0] = or->pitch;
3127 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
3128 r.handles[0] = or->handle;
3130 ret = drm_mode_addfb2(dev, &r, file_priv);
3134 or->fb_id = r.fb_id;
3139 static int format_check(const struct drm_mode_fb_cmd2 *r)
3141 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
3145 case DRM_FORMAT_RGB332:
3146 case DRM_FORMAT_BGR233:
3147 case DRM_FORMAT_XRGB4444:
3148 case DRM_FORMAT_XBGR4444:
3149 case DRM_FORMAT_RGBX4444:
3150 case DRM_FORMAT_BGRX4444:
3151 case DRM_FORMAT_ARGB4444:
3152 case DRM_FORMAT_ABGR4444:
3153 case DRM_FORMAT_RGBA4444:
3154 case DRM_FORMAT_BGRA4444:
3155 case DRM_FORMAT_XRGB1555:
3156 case DRM_FORMAT_XBGR1555:
3157 case DRM_FORMAT_RGBX5551:
3158 case DRM_FORMAT_BGRX5551:
3159 case DRM_FORMAT_ARGB1555:
3160 case DRM_FORMAT_ABGR1555:
3161 case DRM_FORMAT_RGBA5551:
3162 case DRM_FORMAT_BGRA5551:
3163 case DRM_FORMAT_RGB565:
3164 case DRM_FORMAT_BGR565:
3165 case DRM_FORMAT_RGB888:
3166 case DRM_FORMAT_BGR888:
3167 case DRM_FORMAT_XRGB8888:
3168 case DRM_FORMAT_XBGR8888:
3169 case DRM_FORMAT_RGBX8888:
3170 case DRM_FORMAT_BGRX8888:
3171 case DRM_FORMAT_ARGB8888:
3172 case DRM_FORMAT_ABGR8888:
3173 case DRM_FORMAT_RGBA8888:
3174 case DRM_FORMAT_BGRA8888:
3175 case DRM_FORMAT_XRGB2101010:
3176 case DRM_FORMAT_XBGR2101010:
3177 case DRM_FORMAT_RGBX1010102:
3178 case DRM_FORMAT_BGRX1010102:
3179 case DRM_FORMAT_ARGB2101010:
3180 case DRM_FORMAT_ABGR2101010:
3181 case DRM_FORMAT_RGBA1010102:
3182 case DRM_FORMAT_BGRA1010102:
3183 case DRM_FORMAT_YUYV:
3184 case DRM_FORMAT_YVYU:
3185 case DRM_FORMAT_UYVY:
3186 case DRM_FORMAT_VYUY:
3187 case DRM_FORMAT_AYUV:
3188 case DRM_FORMAT_NV12:
3189 case DRM_FORMAT_NV21:
3190 case DRM_FORMAT_NV16:
3191 case DRM_FORMAT_NV61:
3192 case DRM_FORMAT_NV24:
3193 case DRM_FORMAT_NV42:
3194 case DRM_FORMAT_YUV410:
3195 case DRM_FORMAT_YVU410:
3196 case DRM_FORMAT_YUV411:
3197 case DRM_FORMAT_YVU411:
3198 case DRM_FORMAT_YUV420:
3199 case DRM_FORMAT_YVU420:
3200 case DRM_FORMAT_YUV422:
3201 case DRM_FORMAT_YVU422:
3202 case DRM_FORMAT_YUV444:
3203 case DRM_FORMAT_YVU444:
3206 DRM_DEBUG_KMS("invalid pixel format %s\n",
3207 drm_get_format_name(r->pixel_format));
3212 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
3214 int ret, hsub, vsub, num_planes, i;
3216 ret = format_check(r);
3218 DRM_DEBUG_KMS("bad framebuffer format %s\n",
3219 drm_get_format_name(r->pixel_format));
3223 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
3224 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
3225 num_planes = drm_format_num_planes(r->pixel_format);
3227 if (r->width == 0 || r->width % hsub) {
3228 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
3232 if (r->height == 0 || r->height % vsub) {
3233 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
3237 for (i = 0; i < num_planes; i++) {
3238 unsigned int width = r->width / (i != 0 ? hsub : 1);
3239 unsigned int height = r->height / (i != 0 ? vsub : 1);
3240 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
3242 if (!r->handles[i]) {
3243 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
3247 if ((uint64_t) width * cpp > UINT_MAX)
3250 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
3253 if (r->pitches[i] < width * cpp) {
3254 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
3258 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
3259 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
3264 /* modifier specific checks: */
3265 switch (r->modifier[i]) {
3266 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
3267 /* NOTE: the pitch restriction may be lifted later if it turns
3268 * out that no hw has this restriction:
3270 if (r->pixel_format != DRM_FORMAT_NV12 ||
3271 width % 128 || height % 32 ||
3272 r->pitches[i] % 128) {
3273 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
3283 for (i = num_planes; i < 4; i++) {
3284 if (r->modifier[i]) {
3285 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
3289 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
3290 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
3293 if (r->handles[i]) {
3294 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
3298 if (r->pitches[i]) {
3299 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
3303 if (r->offsets[i]) {
3304 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
3312 static struct drm_framebuffer *
3313 internal_framebuffer_create(struct drm_device *dev,
3314 const struct drm_mode_fb_cmd2 *r,
3315 struct drm_file *file_priv)
3317 struct drm_mode_config *config = &dev->mode_config;
3318 struct drm_framebuffer *fb;
3321 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
3322 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
3323 return ERR_PTR(-EINVAL);
3326 if ((config->min_width > r->width) || (r->width > config->max_width)) {
3327 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
3328 r->width, config->min_width, config->max_width);
3329 return ERR_PTR(-EINVAL);
3331 if ((config->min_height > r->height) || (r->height > config->max_height)) {
3332 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
3333 r->height, config->min_height, config->max_height);
3334 return ERR_PTR(-EINVAL);
3337 if (r->flags & DRM_MODE_FB_MODIFIERS &&
3338 !dev->mode_config.allow_fb_modifiers) {
3339 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
3340 return ERR_PTR(-EINVAL);
3343 ret = framebuffer_check(r);
3345 return ERR_PTR(ret);
3347 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
3349 DRM_DEBUG_KMS("could not create framebuffer\n");
3357 * drm_mode_addfb2 - add an FB to the graphics configuration
3358 * @dev: drm device for the ioctl
3359 * @data: data pointer for the ioctl
3360 * @file_priv: drm file for the ioctl call
3362 * Add a new FB to the specified CRTC, given a user request with format. This is
3363 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
3364 * and uses fourcc codes as pixel format specifiers.
3366 * Called by the user via ioctl.
3369 * Zero on success, negative errno on failure.
3371 int drm_mode_addfb2(struct drm_device *dev,
3372 void *data, struct drm_file *file_priv)
3374 struct drm_mode_fb_cmd2 *r = data;
3375 struct drm_framebuffer *fb;
3377 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3380 fb = internal_framebuffer_create(dev, r, file_priv);
3384 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
3385 r->fb_id = fb->base.id;
3387 /* Transfer ownership to the filp for reaping on close */
3388 mutex_lock(&file_priv->fbs_lock);
3389 list_add(&fb->filp_head, &file_priv->fbs);
3390 mutex_unlock(&file_priv->fbs_lock);
3395 struct drm_mode_rmfb_work {
3396 struct work_struct work;
3397 struct list_head fbs;
3400 static void drm_mode_rmfb_work_fn(struct work_struct *w)
3402 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
3404 while (!list_empty(&arg->fbs)) {
3405 struct drm_framebuffer *fb =
3406 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
3408 list_del_init(&fb->filp_head);
3409 drm_framebuffer_remove(fb);
3414 * drm_mode_rmfb - remove an FB from the configuration
3415 * @dev: drm device for the ioctl
3416 * @data: data pointer for the ioctl
3417 * @file_priv: drm file for the ioctl call
3419 * Remove the FB specified by the user.
3421 * Called by the user via ioctl.
3424 * Zero on success, negative errno on failure.
3426 int drm_mode_rmfb(struct drm_device *dev,
3427 void *data, struct drm_file *file_priv)
3429 struct drm_framebuffer *fb = NULL;
3430 struct drm_framebuffer *fbl = NULL;
3431 uint32_t *id = data;
3434 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3437 fb = drm_framebuffer_lookup(dev, *id);
3441 mutex_lock(&file_priv->fbs_lock);
3442 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
3446 mutex_unlock(&file_priv->fbs_lock);
3450 list_del_init(&fb->filp_head);
3451 mutex_unlock(&file_priv->fbs_lock);
3453 /* drop the reference we picked up in framebuffer lookup */
3454 drm_framebuffer_unreference(fb);
3457 * we now own the reference that was stored in the fbs list
3459 * drm_framebuffer_remove may fail with -EINTR on pending signals,
3460 * so run this in a separate stack as there's no way to correctly
3461 * handle this after the fb is already removed from the lookup table.
3463 if (drm_framebuffer_read_refcount(fb) > 1) {
3464 struct drm_mode_rmfb_work arg;
3466 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3467 INIT_LIST_HEAD(&arg.fbs);
3468 list_add_tail(&fb->filp_head, &arg.fbs);
3470 schedule_work(&arg.work);
3471 flush_work(&arg.work);
3472 destroy_work_on_stack(&arg.work);
3474 drm_framebuffer_unreference(fb);
3479 drm_framebuffer_unreference(fb);
3484 * drm_mode_getfb - get FB info
3485 * @dev: drm device for the ioctl
3486 * @data: data pointer for the ioctl
3487 * @file_priv: drm file for the ioctl call
3489 * Lookup the FB given its ID and return info about it.
3491 * Called by the user via ioctl.
3494 * Zero on success, negative errno on failure.
3496 int drm_mode_getfb(struct drm_device *dev,
3497 void *data, struct drm_file *file_priv)
3499 struct drm_mode_fb_cmd *r = data;
3500 struct drm_framebuffer *fb;
3503 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3506 fb = drm_framebuffer_lookup(dev, r->fb_id);
3510 r->height = fb->height;
3511 r->width = fb->width;
3512 r->depth = fb->depth;
3513 r->bpp = fb->bits_per_pixel;
3514 r->pitch = fb->pitches[0];
3515 if (fb->funcs->create_handle) {
3516 if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
3517 drm_is_control_client(file_priv)) {
3518 ret = fb->funcs->create_handle(fb, file_priv,
3521 /* GET_FB() is an unprivileged ioctl so we must not
3522 * return a buffer-handle to non-master processes! For
3523 * backwards-compatibility reasons, we cannot make
3524 * GET_FB() privileged, so just return an invalid handle
3525 * for non-masters. */
3533 drm_framebuffer_unreference(fb);
3539 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
3540 * @dev: drm device for the ioctl
3541 * @data: data pointer for the ioctl
3542 * @file_priv: drm file for the ioctl call
3544 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
3545 * rectangle list. Generic userspace which does frontbuffer rendering must call
3546 * this ioctl to flush out the changes on manual-update display outputs, e.g.
3547 * usb display-link, mipi manual update panels or edp panel self refresh modes.
3549 * Modesetting drivers which always update the frontbuffer do not need to
3550 * implement the corresponding ->dirty framebuffer callback.
3552 * Called by the user via ioctl.
3555 * Zero on success, negative errno on failure.
3557 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
3558 void *data, struct drm_file *file_priv)
3560 struct drm_clip_rect __user *clips_ptr;
3561 struct drm_clip_rect *clips = NULL;
3562 struct drm_mode_fb_dirty_cmd *r = data;
3563 struct drm_framebuffer *fb;
3568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3571 fb = drm_framebuffer_lookup(dev, r->fb_id);
3575 num_clips = r->num_clips;
3576 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
3578 if (!num_clips != !clips_ptr) {
3583 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
3585 /* If userspace annotates copy, clips must come in pairs */
3586 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
3591 if (num_clips && clips_ptr) {
3592 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
3596 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
3602 ret = copy_from_user(clips, clips_ptr,
3603 num_clips * sizeof(*clips));
3610 if (fb->funcs->dirty) {
3611 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
3620 drm_framebuffer_unreference(fb);
3626 * drm_fb_release - remove and free the FBs on this file
3627 * @priv: drm file for the ioctl
3629 * Destroy all the FBs associated with @filp.
3631 * Called by the user via ioctl.
3634 * Zero on success, negative errno on failure.
3636 void drm_fb_release(struct drm_file *priv)
3638 struct drm_framebuffer *fb, *tfb;
3639 struct drm_mode_rmfb_work arg;
3641 INIT_LIST_HEAD(&arg.fbs);
3644 * When the file gets released that means no one else can access the fb
3645 * list any more, so no need to grab fpriv->fbs_lock. And we need to
3646 * avoid upsetting lockdep since the universal cursor code adds a
3647 * framebuffer while holding mutex locks.
3649 * Note that a real deadlock between fpriv->fbs_lock and the modeset
3650 * locks is impossible here since no one else but this function can get
3653 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
3654 if (drm_framebuffer_read_refcount(fb) > 1) {
3655 list_move_tail(&fb->filp_head, &arg.fbs);
3657 list_del_init(&fb->filp_head);
3659 /* This drops the fpriv->fbs reference. */
3660 drm_framebuffer_unreference(fb);
3664 if (!list_empty(&arg.fbs)) {
3665 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
3667 schedule_work(&arg.work);
3668 flush_work(&arg.work);
3669 destroy_work_on_stack(&arg.work);
3674 * drm_property_create - create a new property type
3676 * @flags: flags specifying the property type
3677 * @name: name of the property
3678 * @num_values: number of pre-defined values
3680 * This creates a new generic drm property which can then be attached to a drm
3681 * object with drm_object_attach_property. The returned property object must be
3682 * freed with drm_property_destroy.
3684 * Note that the DRM core keeps a per-device list of properties and that, if
3685 * drm_mode_config_cleanup() is called, it will destroy all properties created
3689 * A pointer to the newly created property on success, NULL on failure.
3691 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3692 const char *name, int num_values)
3694 struct drm_property *property = NULL;
3697 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3701 property->dev = dev;
3704 property->values = kcalloc(num_values, sizeof(uint64_t),
3706 if (!property->values)
3710 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3714 property->flags = flags;
3715 property->num_values = num_values;
3716 INIT_LIST_HEAD(&property->enum_list);
3719 strncpy(property->name, name, DRM_PROP_NAME_LEN);
3720 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3723 list_add_tail(&property->head, &dev->mode_config.property_list);
3725 WARN_ON(!drm_property_type_valid(property));
3729 kfree(property->values);
3733 EXPORT_SYMBOL(drm_property_create);
3736 * drm_property_create_enum - create a new enumeration property type
3738 * @flags: flags specifying the property type
3739 * @name: name of the property
3740 * @props: enumeration lists with property values
3741 * @num_values: number of pre-defined values
3743 * This creates a new generic drm property which can then be attached to a drm
3744 * object with drm_object_attach_property. The returned property object must be
3745 * freed with drm_property_destroy.
3747 * Userspace is only allowed to set one of the predefined values for enumeration
3751 * A pointer to the newly created property on success, NULL on failure.
3753 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3755 const struct drm_prop_enum_list *props,
3758 struct drm_property *property;
3761 flags |= DRM_MODE_PROP_ENUM;
3763 property = drm_property_create(dev, flags, name, num_values);
3767 for (i = 0; i < num_values; i++) {
3768 ret = drm_property_add_enum(property, i,
3772 drm_property_destroy(dev, property);
3779 EXPORT_SYMBOL(drm_property_create_enum);
3782 * drm_property_create_bitmask - create a new bitmask property type
3784 * @flags: flags specifying the property type
3785 * @name: name of the property
3786 * @props: enumeration lists with property bitflags
3787 * @num_props: size of the @props array
3788 * @supported_bits: bitmask of all supported enumeration values
3790 * This creates a new bitmask drm property which can then be attached to a drm
3791 * object with drm_object_attach_property. The returned property object must be
3792 * freed with drm_property_destroy.
3794 * Compared to plain enumeration properties userspace is allowed to set any
3795 * or'ed together combination of the predefined property bitflag values
3798 * A pointer to the newly created property on success, NULL on failure.
3800 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3801 int flags, const char *name,
3802 const struct drm_prop_enum_list *props,
3804 uint64_t supported_bits)
3806 struct drm_property *property;
3807 int i, ret, index = 0;
3808 int num_values = hweight64(supported_bits);
3810 flags |= DRM_MODE_PROP_BITMASK;
3812 property = drm_property_create(dev, flags, name, num_values);
3815 for (i = 0; i < num_props; i++) {
3816 if (!(supported_bits & (1ULL << props[i].type)))
3819 if (WARN_ON(index >= num_values)) {
3820 drm_property_destroy(dev, property);
3824 ret = drm_property_add_enum(property, index++,
3828 drm_property_destroy(dev, property);
3835 EXPORT_SYMBOL(drm_property_create_bitmask);
3837 static struct drm_property *property_create_range(struct drm_device *dev,
3838 int flags, const char *name,
3839 uint64_t min, uint64_t max)
3841 struct drm_property *property;
3843 property = drm_property_create(dev, flags, name, 2);
3847 property->values[0] = min;
3848 property->values[1] = max;
3854 * drm_property_create_range - create a new unsigned ranged property type
3856 * @flags: flags specifying the property type
3857 * @name: name of the property
3858 * @min: minimum value of the property
3859 * @max: maximum value of the property
3861 * This creates a new generic drm property which can then be attached to a drm
3862 * object with drm_object_attach_property. The returned property object must be
3863 * freed with drm_property_destroy.
3865 * Userspace is allowed to set any unsigned integer value in the (min, max)
3869 * A pointer to the newly created property on success, NULL on failure.
3871 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3873 uint64_t min, uint64_t max)
3875 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3878 EXPORT_SYMBOL(drm_property_create_range);
3881 * drm_property_create_signed_range - create a new signed ranged property type
3883 * @flags: flags specifying the property type
3884 * @name: name of the property
3885 * @min: minimum value of the property
3886 * @max: maximum value of the property
3888 * This creates a new generic drm property which can then be attached to a drm
3889 * object with drm_object_attach_property. The returned property object must be
3890 * freed with drm_property_destroy.
3892 * Userspace is allowed to set any signed integer value in the (min, max)
3896 * A pointer to the newly created property on success, NULL on failure.
3898 struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3899 int flags, const char *name,
3900 int64_t min, int64_t max)
3902 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3903 name, I642U64(min), I642U64(max));
3905 EXPORT_SYMBOL(drm_property_create_signed_range);
3908 * drm_property_create_object - create a new object property type
3910 * @flags: flags specifying the property type
3911 * @name: name of the property
3912 * @type: object type from DRM_MODE_OBJECT_* defines
3914 * This creates a new generic drm property which can then be attached to a drm
3915 * object with drm_object_attach_property. The returned property object must be
3916 * freed with drm_property_destroy.
3918 * Userspace is only allowed to set this to any property value of the given
3919 * @type. Only useful for atomic properties, which is enforced.
3922 * A pointer to the newly created property on success, NULL on failure.
3924 struct drm_property *drm_property_create_object(struct drm_device *dev,
3925 int flags, const char *name, uint32_t type)
3927 struct drm_property *property;
3929 flags |= DRM_MODE_PROP_OBJECT;
3931 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3934 property = drm_property_create(dev, flags, name, 1);
3938 property->values[0] = type;
3942 EXPORT_SYMBOL(drm_property_create_object);
3945 * drm_property_create_bool - create a new boolean property type
3947 * @flags: flags specifying the property type
3948 * @name: name of the property
3950 * This creates a new generic drm property which can then be attached to a drm
3951 * object with drm_object_attach_property. The returned property object must be
3952 * freed with drm_property_destroy.
3954 * This is implemented as a ranged property with only {0, 1} as valid values.
3957 * A pointer to the newly created property on success, NULL on failure.
3959 struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3962 return drm_property_create_range(dev, flags, name, 0, 1);
3964 EXPORT_SYMBOL(drm_property_create_bool);
3967 * drm_property_add_enum - add a possible value to an enumeration property
3968 * @property: enumeration property to change
3969 * @index: index of the new enumeration
3970 * @value: value of the new enumeration
3971 * @name: symbolic name of the new enumeration
3973 * This functions adds enumerations to a property.
3975 * It's use is deprecated, drivers should use one of the more specific helpers
3976 * to directly create the property with all enumerations already attached.
3979 * Zero on success, error code on failure.
3981 int drm_property_add_enum(struct drm_property *property, int index,
3982 uint64_t value, const char *name)
3984 struct drm_property_enum *prop_enum;
3986 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3987 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
3991 * Bitmask enum properties have the additional constraint of values
3994 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3998 if (!list_empty(&property->enum_list)) {
3999 list_for_each_entry(prop_enum, &property->enum_list, head) {
4000 if (prop_enum->value == value) {
4001 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4002 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4008 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
4012 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
4013 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
4014 prop_enum->value = value;
4016 property->values[index] = value;
4017 list_add_tail(&prop_enum->head, &property->enum_list);
4020 EXPORT_SYMBOL(drm_property_add_enum);
4023 * drm_property_destroy - destroy a drm property
4025 * @property: property to destry
4027 * This function frees a property including any attached resources like
4028 * enumeration values.
4030 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
4032 struct drm_property_enum *prop_enum, *pt;
4034 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
4035 list_del(&prop_enum->head);
4039 if (property->num_values)
4040 kfree(property->values);
4041 drm_mode_object_unregister(dev, &property->base);
4042 list_del(&property->head);
4045 EXPORT_SYMBOL(drm_property_destroy);
4048 * drm_object_attach_property - attach a property to a modeset object
4049 * @obj: drm modeset object
4050 * @property: property to attach
4051 * @init_val: initial value of the property
4053 * This attaches the given property to the modeset object with the given initial
4054 * value. Currently this function cannot fail since the properties are stored in
4055 * a statically sized array.
4057 void drm_object_attach_property(struct drm_mode_object *obj,
4058 struct drm_property *property,
4061 int count = obj->properties->count;
4063 if (count == DRM_OBJECT_MAX_PROPERTY) {
4064 WARN(1, "Failed to attach object property (type: 0x%x). Please "
4065 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
4066 "you see this message on the same object type.\n",
4071 obj->properties->properties[count] = property;
4072 obj->properties->values[count] = init_val;
4073 obj->properties->count++;
4074 if (property->flags & DRM_MODE_PROP_ATOMIC)
4075 obj->properties->atomic_count++;
4077 EXPORT_SYMBOL(drm_object_attach_property);
4080 * drm_object_property_set_value - set the value of a property
4081 * @obj: drm mode object to set property value for
4082 * @property: property to set
4083 * @val: value the property should be set to
4085 * This functions sets a given property on a given object. This function only
4086 * changes the software state of the property, it does not call into the
4087 * driver's ->set_property callback.
4090 * Zero on success, error code on failure.
4092 int drm_object_property_set_value(struct drm_mode_object *obj,
4093 struct drm_property *property, uint64_t val)
4097 for (i = 0; i < obj->properties->count; i++) {
4098 if (obj->properties->properties[i] == property) {
4099 obj->properties->values[i] = val;
4106 EXPORT_SYMBOL(drm_object_property_set_value);
4109 * drm_object_property_get_value - retrieve the value of a property
4110 * @obj: drm mode object to get property value from
4111 * @property: property to retrieve
4112 * @val: storage for the property value
4114 * This function retrieves the softare state of the given property for the given
4115 * property. Since there is no driver callback to retrieve the current property
4116 * value this might be out of sync with the hardware, depending upon the driver
4120 * Zero on success, error code on failure.
4122 int drm_object_property_get_value(struct drm_mode_object *obj,
4123 struct drm_property *property, uint64_t *val)
4127 /* read-only properties bypass atomic mechanism and still store
4128 * their value in obj->properties->values[].. mostly to avoid
4129 * having to deal w/ EDID and similar props in atomic paths:
4131 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
4132 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
4133 return drm_atomic_get_property(obj, property, val);
4135 for (i = 0; i < obj->properties->count; i++) {
4136 if (obj->properties->properties[i] == property) {
4137 *val = obj->properties->values[i];
4144 EXPORT_SYMBOL(drm_object_property_get_value);
4147 * drm_mode_getproperty_ioctl - get the property metadata
4150 * @file_priv: DRM file info
4152 * This function retrieves the metadata for a given property, like the different
4153 * possible values for an enum property or the limits for a range property.
4155 * Blob properties are special
4157 * Called by the user via ioctl.
4160 * Zero on success, negative errno on failure.
4162 int drm_mode_getproperty_ioctl(struct drm_device *dev,
4163 void *data, struct drm_file *file_priv)
4165 struct drm_mode_get_property *out_resp = data;
4166 struct drm_property *property;
4168 int value_count = 0;
4171 struct drm_property_enum *prop_enum;
4172 struct drm_mode_property_enum __user *enum_ptr;
4173 uint64_t __user *values_ptr;
4175 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4178 drm_modeset_lock_all(dev);
4179 property = drm_property_find(dev, out_resp->prop_id);
4185 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4186 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4187 list_for_each_entry(prop_enum, &property->enum_list, head)
4191 value_count = property->num_values;
4193 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
4194 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
4195 out_resp->flags = property->flags;
4197 if ((out_resp->count_values >= value_count) && value_count) {
4198 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
4199 for (i = 0; i < value_count; i++) {
4200 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
4206 out_resp->count_values = value_count;
4208 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
4209 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4210 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
4212 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
4213 list_for_each_entry(prop_enum, &property->enum_list, head) {
4215 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
4220 if (copy_to_user(&enum_ptr[copied].name,
4221 &prop_enum->name, DRM_PROP_NAME_LEN)) {
4228 out_resp->count_enum_blobs = enum_count;
4232 * NOTE: The idea seems to have been to use this to read all the blob
4233 * property values. But nothing ever added them to the corresponding
4234 * list, userspace always used the special-purpose get_blob ioctl to
4235 * read the value for a blob property. It also doesn't make a lot of
4236 * sense to return values here when everything else is just metadata for
4237 * the property itself.
4239 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4240 out_resp->count_enum_blobs = 0;
4242 drm_modeset_unlock_all(dev);
4246 static void drm_property_free_blob(struct kref *kref)
4248 struct drm_property_blob *blob =
4249 container_of(kref, struct drm_property_blob, base.refcount);
4251 mutex_lock(&blob->dev->mode_config.blob_lock);
4252 list_del(&blob->head_global);
4253 mutex_unlock(&blob->dev->mode_config.blob_lock);
4255 drm_mode_object_unregister(blob->dev, &blob->base);
4261 * drm_property_create_blob - Create new blob property
4263 * Creates a new blob property for a specified DRM device, optionally
4266 * @dev: DRM device to create property for
4267 * @length: Length to allocate for blob data
4268 * @data: If specified, copies data into blob
4271 * New blob property with a single reference on success, or an ERR_PTR
4274 struct drm_property_blob *
4275 drm_property_create_blob(struct drm_device *dev, size_t length,
4278 struct drm_property_blob *blob;
4281 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
4282 return ERR_PTR(-EINVAL);
4284 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
4286 return ERR_PTR(-ENOMEM);
4288 /* This must be explicitly initialised, so we can safely call list_del
4289 * on it in the removal handler, even if it isn't in a file list. */
4290 INIT_LIST_HEAD(&blob->head_file);
4291 blob->length = length;
4295 memcpy(blob->data, data, length);
4297 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
4298 true, drm_property_free_blob);
4301 return ERR_PTR(-EINVAL);
4304 mutex_lock(&dev->mode_config.blob_lock);
4305 list_add_tail(&blob->head_global,
4306 &dev->mode_config.property_blob_list);
4307 mutex_unlock(&dev->mode_config.blob_lock);
4311 EXPORT_SYMBOL(drm_property_create_blob);
4314 * drm_property_unreference_blob - Unreference a blob property
4316 * Drop a reference on a blob property. May free the object.
4318 * @blob: Pointer to blob property
4320 void drm_property_unreference_blob(struct drm_property_blob *blob)
4325 drm_mode_object_unreference(&blob->base);
4327 EXPORT_SYMBOL(drm_property_unreference_blob);
4330 * drm_property_destroy_user_blobs - destroy all blobs created by this client
4332 * @file_priv: destroy all blobs owned by this file handle
4334 void drm_property_destroy_user_blobs(struct drm_device *dev,
4335 struct drm_file *file_priv)
4337 struct drm_property_blob *blob, *bt;
4340 * When the file gets released that means no one else can access the
4341 * blob list any more, so no need to grab dev->blob_lock.
4343 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
4344 list_del_init(&blob->head_file);
4345 drm_property_unreference_blob(blob);
4350 * drm_property_reference_blob - Take a reference on an existing property
4352 * Take a new reference on an existing blob property.
4354 * @blob: Pointer to blob property
4356 struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
4358 drm_mode_object_reference(&blob->base);
4361 EXPORT_SYMBOL(drm_property_reference_blob);
4364 * drm_property_lookup_blob - look up a blob property and take a reference
4366 * @id: id of the blob property
4368 * If successful, this takes an additional reference to the blob property.
4369 * callers need to make sure to eventually unreference the returned property
4370 * again, using @drm_property_unreference_blob.
4372 struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
4375 struct drm_mode_object *obj;
4376 struct drm_property_blob *blob = NULL;
4378 obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
4380 blob = obj_to_blob(obj);
4383 EXPORT_SYMBOL(drm_property_lookup_blob);
4386 * drm_property_replace_global_blob - atomically replace existing blob property
4388 * @replace: location of blob property pointer to be replaced
4389 * @length: length of data for new blob, or 0 for no data
4390 * @data: content for new blob, or NULL for no data
4391 * @obj_holds_id: optional object for property holding blob ID
4392 * @prop_holds_id: optional property holding blob ID
4393 * @return 0 on success or error on failure
4395 * This function will atomically replace a global property in the blob list,
4396 * optionally updating a property which holds the ID of that property. It is
4397 * guaranteed to be atomic: no caller will be allowed to see intermediate
4398 * results, and either the entire operation will succeed and clean up the
4399 * previous property, or it will fail and the state will be unchanged.
4401 * If length is 0 or data is NULL, no new blob will be created, and the holding
4402 * property, if specified, will be set to 0.
4404 * Access to the replace pointer is assumed to be protected by the caller, e.g.
4405 * by holding the relevant modesetting object lock for its parent.
4407 * For example, a drm_connector has a 'PATH' property, which contains the ID
4408 * of a blob property with the value of the MST path information. Calling this
4409 * function with replace pointing to the connector's path_blob_ptr, length and
4410 * data set for the new path information, obj_holds_id set to the connector's
4411 * base object, and prop_holds_id set to the path property name, will perform
4412 * a completely atomic update. The access to path_blob_ptr is protected by the
4413 * caller holding a lock on the connector.
4415 static int drm_property_replace_global_blob(struct drm_device *dev,
4416 struct drm_property_blob **replace,
4419 struct drm_mode_object *obj_holds_id,
4420 struct drm_property *prop_holds_id)
4422 struct drm_property_blob *new_blob = NULL;
4423 struct drm_property_blob *old_blob = NULL;
4426 WARN_ON(replace == NULL);
4428 old_blob = *replace;
4430 if (length && data) {
4431 new_blob = drm_property_create_blob(dev, length, data);
4432 if (IS_ERR(new_blob))
4433 return PTR_ERR(new_blob);
4436 /* This does not need to be synchronised with blob_lock, as the
4437 * get_properties ioctl locks all modesetting objects, and
4438 * obj_holds_id must be locked before calling here, so we cannot
4439 * have its value out of sync with the list membership modified
4440 * below under blob_lock. */
4442 ret = drm_object_property_set_value(obj_holds_id,
4445 new_blob->base.id : 0);
4450 drm_property_unreference_blob(old_blob);
4451 *replace = new_blob;
4456 drm_property_unreference_blob(new_blob);
4461 * drm_mode_getblob_ioctl - get the contents of a blob property value
4464 * @file_priv: DRM file info
4466 * This function retrieves the contents of a blob property. The value stored in
4467 * an object's blob property is just a normal modeset object id.
4469 * Called by the user via ioctl.
4472 * Zero on success, negative errno on failure.
4474 int drm_mode_getblob_ioctl(struct drm_device *dev,
4475 void *data, struct drm_file *file_priv)
4477 struct drm_mode_get_blob *out_resp = data;
4478 struct drm_property_blob *blob;
4480 void __user *blob_ptr;
4482 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4485 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4489 if (out_resp->length == blob->length) {
4490 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4491 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
4496 out_resp->length = blob->length;
4498 drm_property_unreference_blob(blob);
4504 * drm_mode_createblob_ioctl - create a new blob property
4507 * @file_priv: DRM file info
4509 * This function creates a new blob property with user-defined values. In order
4510 * to give us sensible validation and checking when creating, rather than at
4511 * every potential use, we also require a type to be provided upfront.
4513 * Called by the user via ioctl.
4516 * Zero on success, negative errno on failure.
4518 int drm_mode_createblob_ioctl(struct drm_device *dev,
4519 void *data, struct drm_file *file_priv)
4521 struct drm_mode_create_blob *out_resp = data;
4522 struct drm_property_blob *blob;
4523 void __user *blob_ptr;
4526 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4529 blob = drm_property_create_blob(dev, out_resp->length, NULL);
4531 return PTR_ERR(blob);
4533 blob_ptr = (void __user *)(unsigned long)out_resp->data;
4534 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
4539 /* Dropping the lock between create_blob and our access here is safe
4540 * as only the same file_priv can remove the blob; at this point, it is
4541 * not associated with any file_priv. */
4542 mutex_lock(&dev->mode_config.blob_lock);
4543 out_resp->blob_id = blob->base.id;
4544 list_add_tail(&blob->head_file, &file_priv->blobs);
4545 mutex_unlock(&dev->mode_config.blob_lock);
4550 drm_property_unreference_blob(blob);
4555 * drm_mode_destroyblob_ioctl - destroy a user blob property
4558 * @file_priv: DRM file info
4560 * Destroy an existing user-defined blob property.
4562 * Called by the user via ioctl.
4565 * Zero on success, negative errno on failure.
4567 int drm_mode_destroyblob_ioctl(struct drm_device *dev,
4568 void *data, struct drm_file *file_priv)
4570 struct drm_mode_destroy_blob *out_resp = data;
4571 struct drm_property_blob *blob = NULL, *bt;
4575 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4578 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
4582 mutex_lock(&dev->mode_config.blob_lock);
4583 /* Ensure the property was actually created by this user. */
4584 list_for_each_entry(bt, &file_priv->blobs, head_file) {
4596 /* We must drop head_file here, because we may not be the last
4597 * reference on the blob. */
4598 list_del_init(&blob->head_file);
4599 mutex_unlock(&dev->mode_config.blob_lock);
4601 /* One reference from lookup, and one from the filp. */
4602 drm_property_unreference_blob(blob);
4603 drm_property_unreference_blob(blob);
4608 mutex_unlock(&dev->mode_config.blob_lock);
4609 drm_property_unreference_blob(blob);
4615 * drm_mode_connector_set_path_property - set tile property on connector
4616 * @connector: connector to set property on.
4617 * @path: path to use for property; must not be NULL.
4619 * This creates a property to expose to userspace to specify a
4620 * connector path. This is mainly used for DisplayPort MST where
4621 * connectors have a topology and we want to allow userspace to give
4622 * them more meaningful names.
4625 * Zero on success, negative errno on failure.
4627 int drm_mode_connector_set_path_property(struct drm_connector *connector,
4630 struct drm_device *dev = connector->dev;
4633 ret = drm_property_replace_global_blob(dev,
4634 &connector->path_blob_ptr,
4638 dev->mode_config.path_property);
4641 EXPORT_SYMBOL(drm_mode_connector_set_path_property);
4644 * drm_mode_connector_set_tile_property - set tile property on connector
4645 * @connector: connector to set property on.
4647 * This looks up the tile information for a connector, and creates a
4648 * property for userspace to parse if it exists. The property is of
4649 * the form of 8 integers using ':' as a separator.
4652 * Zero on success, errno on failure.
4654 int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4656 struct drm_device *dev = connector->dev;
4660 if (!connector->has_tile) {
4661 ret = drm_property_replace_global_blob(dev,
4662 &connector->tile_blob_ptr,
4666 dev->mode_config.tile_property);
4670 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4671 connector->tile_group->id, connector->tile_is_single_monitor,
4672 connector->num_h_tile, connector->num_v_tile,
4673 connector->tile_h_loc, connector->tile_v_loc,
4674 connector->tile_h_size, connector->tile_v_size);
4676 ret = drm_property_replace_global_blob(dev,
4677 &connector->tile_blob_ptr,
4681 dev->mode_config.tile_property);
4684 EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4687 * drm_mode_connector_update_edid_property - update the edid property of a connector
4688 * @connector: drm connector
4689 * @edid: new value of the edid property
4691 * This function creates a new blob modeset object and assigns its id to the
4692 * connector's edid property.
4695 * Zero on success, negative errno on failure.
4697 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
4698 const struct edid *edid)
4700 struct drm_device *dev = connector->dev;
4704 /* ignore requests to set edid when overridden */
4705 if (connector->override_edid)
4709 size = EDID_LENGTH * (1 + edid->extensions);
4711 ret = drm_property_replace_global_blob(dev,
4712 &connector->edid_blob_ptr,
4716 dev->mode_config.edid_property);
4719 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4721 /* Some properties could refer to dynamic refcnt'd objects, or things that
4722 * need special locking to handle lifetime issues (ie. to ensure the prop
4723 * value doesn't become invalid part way through the property update due to
4724 * race). The value returned by reference via 'obj' should be passed back
4725 * to drm_property_change_valid_put() after the property is set (and the
4726 * object to which the property is attached has a chance to take it's own
4729 bool drm_property_change_valid_get(struct drm_property *property,
4730 uint64_t value, struct drm_mode_object **ref)
4734 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4739 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
4740 if (value < property->values[0] || value > property->values[1])
4743 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4744 int64_t svalue = U642I64(value);
4746 if (svalue < U642I64(property->values[0]) ||
4747 svalue > U642I64(property->values[1]))
4750 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
4751 uint64_t valid_mask = 0;
4753 for (i = 0; i < property->num_values; i++)
4754 valid_mask |= (1ULL << property->values[i]);
4755 return !(value & ~valid_mask);
4756 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
4757 struct drm_property_blob *blob;
4762 blob = drm_property_lookup_blob(property->dev, value);
4769 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4770 /* a zero value for an object property translates to null: */
4774 *ref = _object_find(property->dev, value, property->values[0]);
4775 return *ref != NULL;
4778 for (i = 0; i < property->num_values; i++)
4779 if (property->values[i] == value)
4784 void drm_property_change_valid_put(struct drm_property *property,
4785 struct drm_mode_object *ref)
4790 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
4791 drm_mode_object_unreference(ref);
4792 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4793 drm_property_unreference_blob(obj_to_blob(ref));
4797 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4800 * @file_priv: DRM file info
4802 * This function sets the current value for a connectors's property. It also
4803 * calls into a driver's ->set_property callback to update the hardware state
4805 * Called by the user via ioctl.
4808 * Zero on success, negative errno on failure.
4810 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4811 void *data, struct drm_file *file_priv)
4813 struct drm_mode_connector_set_property *conn_set_prop = data;
4814 struct drm_mode_obj_set_property obj_set_prop = {
4815 .value = conn_set_prop->value,
4816 .prop_id = conn_set_prop->prop_id,
4817 .obj_id = conn_set_prop->connector_id,
4818 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4821 /* It does all the locking and checking we need */
4822 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
4825 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4826 struct drm_property *property,
4830 struct drm_connector *connector = obj_to_connector(obj);
4832 /* Do DPMS ourselves */
4833 if (property == connector->dev->mode_config.dpms_property) {
4834 ret = (*connector->funcs->dpms)(connector, (int)value);
4835 } else if (connector->funcs->set_property)
4836 ret = connector->funcs->set_property(connector, property, value);
4838 /* store the property value if successful */
4840 drm_object_property_set_value(&connector->base, property, value);
4844 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4845 struct drm_property *property,
4849 struct drm_crtc *crtc = obj_to_crtc(obj);
4851 if (crtc->funcs->set_property)
4852 ret = crtc->funcs->set_property(crtc, property, value);
4854 drm_object_property_set_value(obj, property, value);
4860 * drm_mode_plane_set_obj_prop - set the value of a property
4861 * @plane: drm plane object to set property value for
4862 * @property: property to set
4863 * @value: value the property should be set to
4865 * This functions sets a given property on a given plane object. This function
4866 * calls the driver's ->set_property callback and changes the software state of
4867 * the property if the callback succeeds.
4870 * Zero on success, error code on failure.
4872 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4873 struct drm_property *property,
4877 struct drm_mode_object *obj = &plane->base;
4879 if (plane->funcs->set_property)
4880 ret = plane->funcs->set_property(plane, property, value);
4882 drm_object_property_set_value(obj, property, value);
4886 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4889 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
4892 * @file_priv: DRM file info
4894 * This function retrieves the current value for an object's property. Compared
4895 * to the connector specific ioctl this one is extended to also work on crtc and
4898 * Called by the user via ioctl.
4901 * Zero on success, negative errno on failure.
4903 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4904 struct drm_file *file_priv)
4906 struct drm_mode_obj_get_properties *arg = data;
4907 struct drm_mode_object *obj;
4910 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4913 drm_modeset_lock_all(dev);
4915 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4920 if (!obj->properties) {
4925 ret = get_properties(obj, file_priv->atomic,
4926 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4927 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4931 drm_mode_object_unreference(obj);
4933 drm_modeset_unlock_all(dev);
4938 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4941 * @file_priv: DRM file info
4943 * This function sets the current value for an object's property. It also calls
4944 * into a driver's ->set_property callback to update the hardware state.
4945 * Compared to the connector specific ioctl this one is extended to also work on
4946 * crtc and plane objects.
4948 * Called by the user via ioctl.
4951 * Zero on success, negative errno on failure.
4953 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4954 struct drm_file *file_priv)
4956 struct drm_mode_obj_set_property *arg = data;
4957 struct drm_mode_object *arg_obj;
4958 struct drm_mode_object *prop_obj;
4959 struct drm_property *property;
4960 int i, ret = -EINVAL;
4961 struct drm_mode_object *ref;
4963 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4966 drm_modeset_lock_all(dev);
4968 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4973 if (!arg_obj->properties)
4976 for (i = 0; i < arg_obj->properties->count; i++)
4977 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
4980 if (i == arg_obj->properties->count)
4983 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4984 DRM_MODE_OBJECT_PROPERTY);
4989 property = obj_to_property(prop_obj);
4991 if (!drm_property_change_valid_get(property, arg->value, &ref))
4994 switch (arg_obj->type) {
4995 case DRM_MODE_OBJECT_CONNECTOR:
4996 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4999 case DRM_MODE_OBJECT_CRTC:
5000 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
5002 case DRM_MODE_OBJECT_PLANE:
5003 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
5004 property, arg->value);
5008 drm_property_change_valid_put(property, ref);
5011 drm_mode_object_unreference(arg_obj);
5013 drm_modeset_unlock_all(dev);
5018 * drm_mode_connector_attach_encoder - attach a connector to an encoder
5019 * @connector: connector to attach
5020 * @encoder: encoder to attach @connector to
5022 * This function links up a connector to an encoder. Note that the routing
5023 * restrictions between encoders and crtcs are exposed to userspace through the
5024 * possible_clones and possible_crtcs bitmasks.
5027 * Zero on success, negative errno on failure.
5029 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
5030 struct drm_encoder *encoder)
5035 * In the past, drivers have attempted to model the static association
5036 * of connector to encoder in simple connector/encoder devices using a
5037 * direct assignment of connector->encoder = encoder. This connection
5038 * is a logical one and the responsibility of the core, so drivers are
5039 * expected not to mess with this.
5041 * Note that the error return should've been enough here, but a large
5042 * majority of drivers ignores the return value, so add in a big WARN
5043 * to get people's attention.
5045 if (WARN_ON(connector->encoder))
5048 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
5049 if (connector->encoder_ids[i] == 0) {
5050 connector->encoder_ids[i] = encoder->base.id;
5056 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
5059 * drm_mode_crtc_set_gamma_size - set the gamma table size
5060 * @crtc: CRTC to set the gamma table size for
5061 * @gamma_size: size of the gamma table
5063 * Drivers which support gamma tables should set this to the supported gamma
5064 * table size when initializing the CRTC. Currently the drm core only supports a
5065 * fixed gamma table size.
5068 * Zero on success, negative errno on failure.
5070 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
5073 uint16_t *r_base, *g_base, *b_base;
5076 crtc->gamma_size = gamma_size;
5078 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
5080 if (!crtc->gamma_store) {
5081 crtc->gamma_size = 0;
5085 r_base = crtc->gamma_store;
5086 g_base = r_base + gamma_size;
5087 b_base = g_base + gamma_size;
5088 for (i = 0; i < gamma_size; i++) {
5097 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
5100 * drm_mode_gamma_set_ioctl - set the gamma table
5103 * @file_priv: DRM file info
5105 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
5106 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
5108 * Called by the user via ioctl.
5111 * Zero on success, negative errno on failure.
5113 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
5114 void *data, struct drm_file *file_priv)
5116 struct drm_mode_crtc_lut *crtc_lut = data;
5117 struct drm_crtc *crtc;
5118 void *r_base, *g_base, *b_base;
5122 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5125 drm_modeset_lock_all(dev);
5126 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5132 if (crtc->funcs->gamma_set == NULL) {
5137 /* memcpy into gamma store */
5138 if (crtc_lut->gamma_size != crtc->gamma_size) {
5143 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5144 r_base = crtc->gamma_store;
5145 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
5150 g_base = r_base + size;
5151 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
5156 b_base = g_base + size;
5157 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
5162 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
5165 drm_modeset_unlock_all(dev);
5171 * drm_mode_gamma_get_ioctl - get the gamma table
5174 * @file_priv: DRM file info
5176 * Copy the current gamma table into the storage provided. This also provides
5177 * the gamma table size the driver expects, which can be used to size the
5178 * allocated storage.
5180 * Called by the user via ioctl.
5183 * Zero on success, negative errno on failure.
5185 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
5186 void *data, struct drm_file *file_priv)
5188 struct drm_mode_crtc_lut *crtc_lut = data;
5189 struct drm_crtc *crtc;
5190 void *r_base, *g_base, *b_base;
5194 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5197 drm_modeset_lock_all(dev);
5198 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
5204 /* memcpy into gamma store */
5205 if (crtc_lut->gamma_size != crtc->gamma_size) {
5210 size = crtc_lut->gamma_size * (sizeof(uint16_t));
5211 r_base = crtc->gamma_store;
5212 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
5217 g_base = r_base + size;
5218 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
5223 b_base = g_base + size;
5224 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
5229 drm_modeset_unlock_all(dev);
5234 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
5237 * @file_priv: DRM file info
5239 * This schedules an asynchronous update on a given CRTC, called page flip.
5240 * Optionally a drm event is generated to signal the completion of the event.
5241 * Generic drivers cannot assume that a pageflip with changed framebuffer
5242 * properties (including driver specific metadata like tiling layout) will work,
5243 * but some drivers support e.g. pixel format changes through the pageflip
5246 * Called by the user via ioctl.
5249 * Zero on success, negative errno on failure.
5251 int drm_mode_page_flip_ioctl(struct drm_device *dev,
5252 void *data, struct drm_file *file_priv)
5254 struct drm_mode_crtc_page_flip *page_flip = data;
5255 struct drm_crtc *crtc;
5256 struct drm_framebuffer *fb = NULL;
5257 struct drm_pending_vblank_event *e = NULL;
5260 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
5261 page_flip->reserved != 0)
5264 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
5267 crtc = drm_crtc_find(dev, page_flip->crtc_id);
5271 drm_modeset_lock_crtc(crtc, crtc->primary);
5272 if (crtc->primary->fb == NULL) {
5273 /* The framebuffer is currently unbound, presumably
5274 * due to a hotplug event, that userspace has not
5281 if (crtc->funcs->page_flip == NULL)
5284 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
5291 const struct drm_plane_state *state = crtc->primary->state;
5293 ret = check_src_coords(state->src_x, state->src_y,
5294 state->src_w, state->src_h, fb);
5296 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
5301 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
5302 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
5307 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
5308 e = kzalloc(sizeof *e, GFP_KERNEL);
5313 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
5314 e->event.base.length = sizeof(e->event);
5315 e->event.user_data = page_flip->user_data;
5316 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
5323 crtc->primary->old_fb = crtc->primary->fb;
5324 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
5326 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
5327 drm_event_cancel_free(dev, &e->base);
5328 /* Keep the old fb, don't unref it. */
5329 crtc->primary->old_fb = NULL;
5331 crtc->primary->fb = fb;
5332 /* Unref only the old framebuffer. */
5338 drm_framebuffer_unreference(fb);
5339 if (crtc->primary->old_fb)
5340 drm_framebuffer_unreference(crtc->primary->old_fb);
5341 crtc->primary->old_fb = NULL;
5342 drm_modeset_unlock_crtc(crtc);
5348 * drm_mode_config_reset - call ->reset callbacks
5351 * This functions calls all the crtc's, encoder's and connector's ->reset
5352 * callback. Drivers can use this in e.g. their driver load or resume code to
5353 * reset hardware and software state.
5355 void drm_mode_config_reset(struct drm_device *dev)
5357 struct drm_crtc *crtc;
5358 struct drm_plane *plane;
5359 struct drm_encoder *encoder;
5360 struct drm_connector *connector;
5362 drm_for_each_plane(plane, dev)
5363 if (plane->funcs->reset)
5364 plane->funcs->reset(plane);
5366 drm_for_each_crtc(crtc, dev)
5367 if (crtc->funcs->reset)
5368 crtc->funcs->reset(crtc);
5370 drm_for_each_encoder(encoder, dev)
5371 if (encoder->funcs->reset)
5372 encoder->funcs->reset(encoder);
5374 mutex_lock(&dev->mode_config.mutex);
5375 drm_for_each_connector(connector, dev)
5376 if (connector->funcs->reset)
5377 connector->funcs->reset(connector);
5378 mutex_unlock(&dev->mode_config.mutex);
5380 EXPORT_SYMBOL(drm_mode_config_reset);
5383 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
5386 * @file_priv: DRM file info
5388 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
5389 * TTM or something else entirely) and returns the resulting buffer handle. This
5390 * handle can then be wrapped up into a framebuffer modeset object.
5392 * Note that userspace is not allowed to use such objects for render
5393 * acceleration - drivers must create their own private ioctls for such a use
5396 * Called by the user via ioctl.
5399 * Zero on success, negative errno on failure.
5401 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
5402 void *data, struct drm_file *file_priv)
5404 struct drm_mode_create_dumb *args = data;
5405 u32 cpp, stride, size;
5407 if (!dev->driver->dumb_create)
5409 if (!args->width || !args->height || !args->bpp)
5412 /* overflow checks for 32bit size calculations */
5413 /* NOTE: DIV_ROUND_UP() can overflow */
5414 cpp = DIV_ROUND_UP(args->bpp, 8);
5415 if (!cpp || cpp > 0xffffffffU / args->width)
5417 stride = cpp * args->width;
5418 if (args->height > 0xffffffffU / stride)
5421 /* test for wrap-around */
5422 size = args->height * stride;
5423 if (PAGE_ALIGN(size) == 0)
5427 * handle, pitch and size are output parameters. Zero them out to
5428 * prevent drivers from accidentally using uninitialized data. Since
5429 * not all existing userspace is clearing these fields properly we
5430 * cannot reject IOCTL with garbage in them.
5436 return dev->driver->dumb_create(file_priv, dev, args);
5440 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
5443 * @file_priv: DRM file info
5445 * Allocate an offset in the drm device node's address space to be able to
5446 * memory map a dumb buffer.
5448 * Called by the user via ioctl.
5451 * Zero on success, negative errno on failure.
5453 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
5454 void *data, struct drm_file *file_priv)
5456 struct drm_mode_map_dumb *args = data;
5458 /* call driver ioctl to get mmap offset */
5459 if (!dev->driver->dumb_map_offset)
5462 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
5466 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
5469 * @file_priv: DRM file info
5471 * This destroys the userspace handle for the given dumb backing storage buffer.
5472 * Since buffer objects must be reference counted in the kernel a buffer object
5473 * won't be immediately freed if a framebuffer modeset object still uses it.
5475 * Called by the user via ioctl.
5478 * Zero on success, negative errno on failure.
5480 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
5481 void *data, struct drm_file *file_priv)
5483 struct drm_mode_destroy_dumb *args = data;
5485 if (!dev->driver->dumb_destroy)
5488 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
5492 * drm_rotation_simplify() - Try to simplify the rotation
5493 * @rotation: Rotation to be simplified
5494 * @supported_rotations: Supported rotations
5496 * Attempt to simplify the rotation to a form that is supported.
5497 * Eg. if the hardware supports everything except DRM_REFLECT_X
5498 * one could call this function like this:
5500 * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
5501 * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
5502 * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
5504 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
5505 * transforms the hardware supports, this function may not
5506 * be able to produce a supported transform, so the caller should
5507 * check the result afterwards.
5509 unsigned int drm_rotation_simplify(unsigned int rotation,
5510 unsigned int supported_rotations)
5512 if (rotation & ~supported_rotations) {
5513 rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
5514 rotation = (rotation & DRM_REFLECT_MASK) |
5515 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
5520 EXPORT_SYMBOL(drm_rotation_simplify);
5523 * drm_mode_config_init - initialize DRM mode_configuration structure
5526 * Initialize @dev's mode_config structure, used for tracking the graphics
5527 * configuration of @dev.
5529 * Since this initializes the modeset locks, no locking is possible. Which is no
5530 * problem, since this should happen single threaded at init time. It is the
5531 * driver's problem to ensure this guarantee.
5534 void drm_mode_config_init(struct drm_device *dev)
5536 mutex_init(&dev->mode_config.mutex);
5537 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
5538 mutex_init(&dev->mode_config.idr_mutex);
5539 mutex_init(&dev->mode_config.fb_lock);
5540 mutex_init(&dev->mode_config.blob_lock);
5541 INIT_LIST_HEAD(&dev->mode_config.fb_list);
5542 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
5543 INIT_LIST_HEAD(&dev->mode_config.connector_list);
5544 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
5545 INIT_LIST_HEAD(&dev->mode_config.property_list);
5546 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
5547 INIT_LIST_HEAD(&dev->mode_config.plane_list);
5548 idr_init(&dev->mode_config.crtc_idr);
5549 idr_init(&dev->mode_config.tile_idr);
5550 ida_init(&dev->mode_config.connector_ida);
5552 drm_modeset_lock_all(dev);
5553 drm_mode_create_standard_properties(dev);
5554 drm_modeset_unlock_all(dev);
5556 /* Just to be sure */
5557 dev->mode_config.num_fb = 0;
5558 dev->mode_config.num_connector = 0;
5559 dev->mode_config.num_crtc = 0;
5560 dev->mode_config.num_encoder = 0;
5561 dev->mode_config.num_overlay_plane = 0;
5562 dev->mode_config.num_total_plane = 0;
5564 EXPORT_SYMBOL(drm_mode_config_init);
5567 * drm_mode_config_cleanup - free up DRM mode_config info
5570 * Free up all the connectors and CRTCs associated with this DRM device, then
5571 * free up the framebuffers and associated buffer objects.
5573 * Note that since this /should/ happen single-threaded at driver/device
5574 * teardown time, no locking is required. It's the driver's job to ensure that
5575 * this guarantee actually holds true.
5577 * FIXME: cleanup any dangling user buffer objects too
5579 void drm_mode_config_cleanup(struct drm_device *dev)
5581 struct drm_connector *connector, *ot;
5582 struct drm_crtc *crtc, *ct;
5583 struct drm_encoder *encoder, *enct;
5584 struct drm_framebuffer *fb, *fbt;
5585 struct drm_property *property, *pt;
5586 struct drm_property_blob *blob, *bt;
5587 struct drm_plane *plane, *plt;
5589 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
5591 encoder->funcs->destroy(encoder);
5594 list_for_each_entry_safe(connector, ot,
5595 &dev->mode_config.connector_list, head) {
5596 connector->funcs->destroy(connector);
5599 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
5601 drm_property_destroy(dev, property);
5604 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
5606 plane->funcs->destroy(plane);
5609 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
5610 crtc->funcs->destroy(crtc);
5613 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
5615 drm_property_unreference_blob(blob);
5619 * Single-threaded teardown context, so it's not required to grab the
5620 * fb_lock to protect against concurrent fb_list access. Contrary, it
5621 * would actually deadlock with the drm_framebuffer_cleanup function.
5623 * Also, if there are any framebuffers left, that's a driver leak now,
5624 * so politely WARN about this.
5626 WARN_ON(!list_empty(&dev->mode_config.fb_list));
5627 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
5628 drm_framebuffer_free(&fb->base.refcount);
5631 ida_destroy(&dev->mode_config.connector_ida);
5632 idr_destroy(&dev->mode_config.tile_idr);
5633 idr_destroy(&dev->mode_config.crtc_idr);
5634 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
5636 EXPORT_SYMBOL(drm_mode_config_cleanup);
5638 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
5639 unsigned int supported_rotations)
5641 static const struct drm_prop_enum_list props[] = {
5642 { DRM_ROTATE_0, "rotate-0" },
5643 { DRM_ROTATE_90, "rotate-90" },
5644 { DRM_ROTATE_180, "rotate-180" },
5645 { DRM_ROTATE_270, "rotate-270" },
5646 { DRM_REFLECT_X, "reflect-x" },
5647 { DRM_REFLECT_Y, "reflect-y" },
5650 return drm_property_create_bitmask(dev, 0, "rotation",
5651 props, ARRAY_SIZE(props),
5652 supported_rotations);
5654 EXPORT_SYMBOL(drm_mode_create_rotation_property);
5659 * Tile groups are used to represent tiled monitors with a unique
5660 * integer identifier. Tiled monitors using DisplayID v1.3 have
5661 * a unique 8-byte handle, we store this in a tile group, so we
5662 * have a common identifier for all tiles in a monitor group.
5664 static void drm_tile_group_free(struct kref *kref)
5666 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5667 struct drm_device *dev = tg->dev;
5668 mutex_lock(&dev->mode_config.idr_mutex);
5669 idr_remove(&dev->mode_config.tile_idr, tg->id);
5670 mutex_unlock(&dev->mode_config.idr_mutex);
5675 * drm_mode_put_tile_group - drop a reference to a tile group.
5677 * @tg: tile group to drop reference to.
5679 * drop reference to tile group and free if 0.
5681 void drm_mode_put_tile_group(struct drm_device *dev,
5682 struct drm_tile_group *tg)
5684 kref_put(&tg->refcount, drm_tile_group_free);
5688 * drm_mode_get_tile_group - get a reference to an existing tile group
5690 * @topology: 8-bytes unique per monitor.
5692 * Use the unique bytes to get a reference to an existing tile group.
5695 * tile group or NULL if not found.
5697 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5700 struct drm_tile_group *tg;
5702 mutex_lock(&dev->mode_config.idr_mutex);
5703 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5704 if (!memcmp(tg->group_data, topology, 8)) {
5705 if (!kref_get_unless_zero(&tg->refcount))
5707 mutex_unlock(&dev->mode_config.idr_mutex);
5711 mutex_unlock(&dev->mode_config.idr_mutex);
5714 EXPORT_SYMBOL(drm_mode_get_tile_group);
5717 * drm_mode_create_tile_group - create a tile group from a displayid description
5719 * @topology: 8-bytes unique per monitor.
5721 * Create a tile group for the unique monitor, and get a unique
5722 * identifier for the tile group.
5725 * new tile group or error.
5727 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5730 struct drm_tile_group *tg;
5733 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5735 return ERR_PTR(-ENOMEM);
5737 kref_init(&tg->refcount);
5738 memcpy(tg->group_data, topology, 8);
5741 mutex_lock(&dev->mode_config.idr_mutex);
5742 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5750 mutex_unlock(&dev->mode_config.idr_mutex);
5753 EXPORT_SYMBOL(drm_mode_create_tile_group);
5756 * drm_crtc_enable_color_mgmt - enable color management properties
5758 * @degamma_lut_size: the size of the degamma lut (before CSC)
5759 * @has_ctm: whether to attach ctm_property for CSC matrix
5760 * @gamma_lut_size: the size of the gamma lut (after CSC)
5762 * This function lets the driver enable the color correction
5763 * properties on a CRTC. This includes 3 degamma, csc and gamma
5764 * properties that userspace can set and 2 size properties to inform
5765 * the userspace of the lut sizes. Each of the properties are
5766 * optional. The gamma and degamma properties are only attached if
5767 * their size is not 0 and ctm_property is only attached if has_ctm is
5770 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5771 uint degamma_lut_size,
5773 uint gamma_lut_size)
5775 struct drm_device *dev = crtc->dev;
5776 struct drm_mode_config *config = &dev->mode_config;
5778 if (degamma_lut_size) {
5779 drm_object_attach_property(&crtc->base,
5780 config->degamma_lut_property, 0);
5781 drm_object_attach_property(&crtc->base,
5782 config->degamma_lut_size_property,
5787 drm_object_attach_property(&crtc->base,
5788 config->ctm_property, 0);
5790 if (gamma_lut_size) {
5791 drm_object_attach_property(&crtc->base,
5792 config->gamma_lut_property, 0);
5793 drm_object_attach_property(&crtc->base,
5794 config->gamma_lut_size_property,
5798 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);