2 * Copyright (c) 2006-2008 Intel Corporation
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
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>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_framebuffer.h>
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
51 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
52 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
61 * drm_crtc_force_disable - Forcibly turn off a CRTC
62 * @crtc: CRTC to turn off
65 * Zero on success, error code on failure.
67 int drm_crtc_force_disable(struct drm_crtc *crtc)
69 struct drm_mode_set set = {
73 return drm_mode_set_config_internal(&set);
75 EXPORT_SYMBOL(drm_crtc_force_disable);
78 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79 * @dev: DRM device whose CRTCs to turn off
81 * Drivers may want to call this on unload to ensure that all displays are
82 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
85 * Zero on success, error code on failure.
87 int drm_crtc_force_disable_all(struct drm_device *dev)
89 struct drm_crtc *crtc;
92 drm_modeset_lock_all(dev);
93 drm_for_each_crtc(crtc, dev)
95 ret = drm_crtc_force_disable(crtc);
100 drm_modeset_unlock_all(dev);
103 EXPORT_SYMBOL(drm_crtc_force_disable_all);
105 DEFINE_WW_CLASS(crtc_ww_class);
107 static unsigned int drm_num_crtcs(struct drm_device *dev)
109 unsigned int num = 0;
110 struct drm_crtc *tmp;
112 drm_for_each_crtc(tmp, dev) {
119 static int drm_crtc_register_all(struct drm_device *dev)
121 struct drm_crtc *crtc;
124 drm_for_each_crtc(crtc, dev) {
125 if (crtc->funcs->late_register)
126 ret = crtc->funcs->late_register(crtc);
134 static void drm_crtc_unregister_all(struct drm_device *dev)
136 struct drm_crtc *crtc;
138 drm_for_each_crtc(crtc, dev) {
139 if (crtc->funcs->early_unregister)
140 crtc->funcs->early_unregister(crtc);
145 * drm_crtc_init_with_planes - Initialise a new CRTC object with
146 * specified primary and cursor planes.
148 * @crtc: CRTC object to init
149 * @primary: Primary plane for CRTC
150 * @cursor: Cursor plane for CRTC
151 * @funcs: callbacks for the new CRTC
152 * @name: printf style format string for the CRTC name, or NULL for default name
154 * Inits a new object created as base part of a driver crtc object.
157 * Zero on success, error code on failure.
159 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
160 struct drm_plane *primary,
161 struct drm_plane *cursor,
162 const struct drm_crtc_funcs *funcs,
163 const char *name, ...)
165 struct drm_mode_config *config = &dev->mode_config;
168 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
169 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
174 INIT_LIST_HEAD(&crtc->commit_list);
175 spin_lock_init(&crtc->commit_lock);
177 drm_modeset_lock_init(&crtc->mutex);
178 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
186 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
189 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
193 drm_mode_object_unregister(dev, &crtc->base);
197 crtc->base.properties = &crtc->properties;
199 list_add_tail(&crtc->head, &config->crtc_list);
200 crtc->index = config->num_crtc++;
202 crtc->primary = primary;
203 crtc->cursor = cursor;
205 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
207 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
209 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
210 drm_object_attach_property(&crtc->base, config->prop_active, 0);
211 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
216 EXPORT_SYMBOL(drm_crtc_init_with_planes);
219 * drm_crtc_cleanup - Clean up the core crtc usage
220 * @crtc: CRTC to cleanup
222 * This function cleans up @crtc and removes it from the DRM mode setting
223 * core. Note that the function does *not* free the crtc structure itself,
224 * this is the responsibility of the caller.
226 void drm_crtc_cleanup(struct drm_crtc *crtc)
228 struct drm_device *dev = crtc->dev;
230 /* Note that the crtc_list is considered to be static; should we
231 * remove the drm_crtc at runtime we would have to decrement all
232 * the indices on the drm_crtc after us in the crtc_list.
235 kfree(crtc->gamma_store);
236 crtc->gamma_store = NULL;
238 drm_modeset_lock_fini(&crtc->mutex);
240 drm_mode_object_unregister(dev, &crtc->base);
241 list_del(&crtc->head);
242 dev->mode_config.num_crtc--;
244 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
245 if (crtc->state && crtc->funcs->atomic_destroy_state)
246 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
250 memset(crtc, 0, sizeof(*crtc));
252 EXPORT_SYMBOL(drm_crtc_cleanup);
254 static unsigned int drm_num_planes(struct drm_device *dev)
256 unsigned int num = 0;
257 struct drm_plane *tmp;
259 drm_for_each_plane(tmp, dev) {
267 * drm_universal_plane_init - Initialize a new universal plane object
269 * @plane: plane object to init
270 * @possible_crtcs: bitmask of possible CRTCs
271 * @funcs: callbacks for the new plane
272 * @formats: array of supported formats (DRM_FORMAT\_\*)
273 * @format_count: number of elements in @formats
274 * @type: type of plane (overlay, primary, cursor)
275 * @name: printf style format string for the plane name, or NULL for default name
277 * Initializes a plane object of type @type.
280 * Zero on success, error code on failure.
282 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
283 unsigned long possible_crtcs,
284 const struct drm_plane_funcs *funcs,
285 const uint32_t *formats, unsigned int format_count,
286 enum drm_plane_type type,
287 const char *name, ...)
289 struct drm_mode_config *config = &dev->mode_config;
292 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
296 drm_modeset_lock_init(&plane->mutex);
298 plane->base.properties = &plane->properties;
300 plane->funcs = funcs;
301 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
303 if (!plane->format_types) {
304 DRM_DEBUG_KMS("out of memory when allocating plane\n");
305 drm_mode_object_unregister(dev, &plane->base);
313 plane->name = kvasprintf(GFP_KERNEL, name, ap);
316 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
317 drm_num_planes(dev));
320 kfree(plane->format_types);
321 drm_mode_object_unregister(dev, &plane->base);
325 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
326 plane->format_count = format_count;
327 plane->possible_crtcs = possible_crtcs;
330 list_add_tail(&plane->head, &config->plane_list);
331 plane->index = config->num_total_plane++;
332 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
333 config->num_overlay_plane++;
335 drm_object_attach_property(&plane->base,
336 config->plane_type_property,
339 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
340 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
341 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
342 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
343 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
344 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
345 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
346 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
347 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
348 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
349 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
354 EXPORT_SYMBOL(drm_universal_plane_init);
356 static int drm_plane_register_all(struct drm_device *dev)
358 struct drm_plane *plane;
361 drm_for_each_plane(plane, dev) {
362 if (plane->funcs->late_register)
363 ret = plane->funcs->late_register(plane);
371 static void drm_plane_unregister_all(struct drm_device *dev)
373 struct drm_plane *plane;
375 drm_for_each_plane(plane, dev) {
376 if (plane->funcs->early_unregister)
377 plane->funcs->early_unregister(plane);
382 * drm_plane_init - Initialize a legacy plane
384 * @plane: plane object to init
385 * @possible_crtcs: bitmask of possible CRTCs
386 * @funcs: callbacks for the new plane
387 * @formats: array of supported formats (DRM_FORMAT\_\*)
388 * @format_count: number of elements in @formats
389 * @is_primary: plane type (primary vs overlay)
391 * Legacy API to initialize a DRM plane.
393 * New drivers should call drm_universal_plane_init() instead.
396 * Zero on success, error code on failure.
398 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
399 unsigned long possible_crtcs,
400 const struct drm_plane_funcs *funcs,
401 const uint32_t *formats, unsigned int format_count,
404 enum drm_plane_type type;
406 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
407 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
408 formats, format_count, type, NULL);
410 EXPORT_SYMBOL(drm_plane_init);
413 * drm_plane_cleanup - Clean up the core plane usage
414 * @plane: plane to cleanup
416 * This function cleans up @plane and removes it from the DRM mode setting
417 * core. Note that the function does *not* free the plane structure itself,
418 * this is the responsibility of the caller.
420 void drm_plane_cleanup(struct drm_plane *plane)
422 struct drm_device *dev = plane->dev;
424 drm_modeset_lock_all(dev);
425 kfree(plane->format_types);
426 drm_mode_object_unregister(dev, &plane->base);
428 BUG_ON(list_empty(&plane->head));
430 /* Note that the plane_list is considered to be static; should we
431 * remove the drm_plane at runtime we would have to decrement all
432 * the indices on the drm_plane after us in the plane_list.
435 list_del(&plane->head);
436 dev->mode_config.num_total_plane--;
437 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
438 dev->mode_config.num_overlay_plane--;
439 drm_modeset_unlock_all(dev);
441 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
442 if (plane->state && plane->funcs->atomic_destroy_state)
443 plane->funcs->atomic_destroy_state(plane, plane->state);
447 memset(plane, 0, sizeof(*plane));
449 EXPORT_SYMBOL(drm_plane_cleanup);
452 * drm_plane_from_index - find the registered plane at an index
454 * @idx: index of registered plane to find for
456 * Given a plane index, return the registered plane from DRM device's
457 * list of planes with matching index.
460 drm_plane_from_index(struct drm_device *dev, int idx)
462 struct drm_plane *plane;
464 drm_for_each_plane(plane, dev)
465 if (idx == plane->index)
470 EXPORT_SYMBOL(drm_plane_from_index);
473 * drm_plane_force_disable - Forcibly disable a plane
474 * @plane: plane to disable
476 * Forces the plane to be disabled.
478 * Used when the plane's current framebuffer is destroyed,
479 * and when restoring fbdev mode.
481 void drm_plane_force_disable(struct drm_plane *plane)
488 plane->old_fb = plane->fb;
489 ret = plane->funcs->disable_plane(plane);
491 DRM_ERROR("failed to disable plane with busy fb\n");
492 plane->old_fb = NULL;
495 /* disconnect the plane from the fb and crtc: */
496 drm_framebuffer_unreference(plane->old_fb);
497 plane->old_fb = NULL;
501 EXPORT_SYMBOL(drm_plane_force_disable);
503 int drm_modeset_register_all(struct drm_device *dev)
507 ret = drm_plane_register_all(dev);
511 ret = drm_crtc_register_all(dev);
515 ret = drm_encoder_register_all(dev);
519 ret = drm_connector_register_all(dev);
526 drm_encoder_unregister_all(dev);
528 drm_crtc_unregister_all(dev);
530 drm_plane_unregister_all(dev);
535 void drm_modeset_unregister_all(struct drm_device *dev)
537 drm_connector_unregister_all(dev);
538 drm_encoder_unregister_all(dev);
539 drm_crtc_unregister_all(dev);
540 drm_plane_unregister_all(dev);
543 static int drm_mode_create_standard_properties(struct drm_device *dev)
545 struct drm_property *prop;
548 ret = drm_connector_create_standard_properties(dev);
552 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
553 "type", drm_plane_type_enum_list,
554 ARRAY_SIZE(drm_plane_type_enum_list));
557 dev->mode_config.plane_type_property = prop;
559 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
560 "SRC_X", 0, UINT_MAX);
563 dev->mode_config.prop_src_x = prop;
565 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
566 "SRC_Y", 0, UINT_MAX);
569 dev->mode_config.prop_src_y = prop;
571 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
572 "SRC_W", 0, UINT_MAX);
575 dev->mode_config.prop_src_w = prop;
577 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
578 "SRC_H", 0, UINT_MAX);
581 dev->mode_config.prop_src_h = prop;
583 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
584 "CRTC_X", INT_MIN, INT_MAX);
587 dev->mode_config.prop_crtc_x = prop;
589 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
590 "CRTC_Y", INT_MIN, INT_MAX);
593 dev->mode_config.prop_crtc_y = prop;
595 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
596 "CRTC_W", 0, INT_MAX);
599 dev->mode_config.prop_crtc_w = prop;
601 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
602 "CRTC_H", 0, INT_MAX);
605 dev->mode_config.prop_crtc_h = prop;
607 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
608 "FB_ID", DRM_MODE_OBJECT_FB);
611 dev->mode_config.prop_fb_id = prop;
613 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
614 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
617 dev->mode_config.prop_crtc_id = prop;
619 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
623 dev->mode_config.prop_active = prop;
625 prop = drm_property_create(dev,
626 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
630 dev->mode_config.prop_mode_id = prop;
632 prop = drm_property_create(dev,
637 dev->mode_config.degamma_lut_property = prop;
639 prop = drm_property_create_range(dev,
640 DRM_MODE_PROP_IMMUTABLE,
641 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
644 dev->mode_config.degamma_lut_size_property = prop;
646 prop = drm_property_create(dev,
651 dev->mode_config.ctm_property = prop;
653 prop = drm_property_create(dev,
658 dev->mode_config.gamma_lut_property = prop;
660 prop = drm_property_create_range(dev,
661 DRM_MODE_PROP_IMMUTABLE,
662 "GAMMA_LUT_SIZE", 0, UINT_MAX);
665 dev->mode_config.gamma_lut_size_property = prop;
671 * drm_mode_getresources - get graphics configuration
672 * @dev: drm device for the ioctl
673 * @data: data pointer for the ioctl
674 * @file_priv: drm file for the ioctl call
676 * Construct a set of configuration description structures and return
677 * them to the user, including CRTC, connector and framebuffer configuration.
679 * Called by the user via ioctl.
682 * Zero on success, negative errno on failure.
684 int drm_mode_getresources(struct drm_device *dev, void *data,
685 struct drm_file *file_priv)
687 struct drm_mode_card_res *card_res = data;
688 struct list_head *lh;
689 struct drm_framebuffer *fb;
690 struct drm_connector *connector;
691 struct drm_crtc *crtc;
692 struct drm_encoder *encoder;
694 int connector_count = 0;
697 int encoder_count = 0;
699 uint32_t __user *fb_id;
700 uint32_t __user *crtc_id;
701 uint32_t __user *connector_id;
702 uint32_t __user *encoder_id;
704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
708 mutex_lock(&file_priv->fbs_lock);
710 * For the non-control nodes we need to limit the list of resources
711 * by IDs in the group list for this node
713 list_for_each(lh, &file_priv->fbs)
716 /* handle this in 4 parts */
718 if (card_res->count_fbs >= fb_count) {
720 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
721 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
722 if (put_user(fb->base.id, fb_id + copied)) {
723 mutex_unlock(&file_priv->fbs_lock);
729 card_res->count_fbs = fb_count;
730 mutex_unlock(&file_priv->fbs_lock);
732 /* mode_config.mutex protects the connector list against e.g. DP MST
733 * connector hot-adding. CRTC/Plane lists are invariant. */
734 mutex_lock(&dev->mode_config.mutex);
735 drm_for_each_crtc(crtc, dev)
738 drm_for_each_connector(connector, dev)
741 drm_for_each_encoder(encoder, dev)
744 card_res->max_height = dev->mode_config.max_height;
745 card_res->min_height = dev->mode_config.min_height;
746 card_res->max_width = dev->mode_config.max_width;
747 card_res->min_width = dev->mode_config.min_width;
750 if (card_res->count_crtcs >= crtc_count) {
752 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
753 drm_for_each_crtc(crtc, dev) {
754 if (put_user(crtc->base.id, crtc_id + copied)) {
761 card_res->count_crtcs = crtc_count;
764 if (card_res->count_encoders >= encoder_count) {
766 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
767 drm_for_each_encoder(encoder, dev) {
768 if (put_user(encoder->base.id, encoder_id +
776 card_res->count_encoders = encoder_count;
779 if (card_res->count_connectors >= connector_count) {
781 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
782 drm_for_each_connector(connector, dev) {
783 if (put_user(connector->base.id,
784 connector_id + copied)) {
791 card_res->count_connectors = connector_count;
794 mutex_unlock(&dev->mode_config.mutex);
799 * drm_mode_getcrtc - get CRTC configuration
800 * @dev: drm device for the ioctl
801 * @data: data pointer for the ioctl
802 * @file_priv: drm file for the ioctl call
804 * Construct a CRTC configuration structure to return to the user.
806 * Called by the user via ioctl.
809 * Zero on success, negative errno on failure.
811 int drm_mode_getcrtc(struct drm_device *dev,
812 void *data, struct drm_file *file_priv)
814 struct drm_mode_crtc *crtc_resp = data;
815 struct drm_crtc *crtc;
817 if (!drm_core_check_feature(dev, DRIVER_MODESET))
820 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
824 drm_modeset_lock_crtc(crtc, crtc->primary);
825 crtc_resp->gamma_size = crtc->gamma_size;
826 if (crtc->primary->fb)
827 crtc_resp->fb_id = crtc->primary->fb->base.id;
829 crtc_resp->fb_id = 0;
832 crtc_resp->x = crtc->primary->state->src_x >> 16;
833 crtc_resp->y = crtc->primary->state->src_y >> 16;
834 if (crtc->state->enable) {
835 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
836 crtc_resp->mode_valid = 1;
839 crtc_resp->mode_valid = 0;
842 crtc_resp->x = crtc->x;
843 crtc_resp->y = crtc->y;
845 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
846 crtc_resp->mode_valid = 1;
849 crtc_resp->mode_valid = 0;
852 drm_modeset_unlock_crtc(crtc);
858 * drm_mode_getplane_res - enumerate all plane resources
861 * @file_priv: DRM file info
863 * Construct a list of plane ids to return to the user.
865 * Called by the user via ioctl.
868 * Zero on success, negative errno on failure.
870 int drm_mode_getplane_res(struct drm_device *dev, void *data,
871 struct drm_file *file_priv)
873 struct drm_mode_get_plane_res *plane_resp = data;
874 struct drm_mode_config *config;
875 struct drm_plane *plane;
876 uint32_t __user *plane_ptr;
880 if (!drm_core_check_feature(dev, DRIVER_MODESET))
883 config = &dev->mode_config;
885 if (file_priv->universal_planes)
886 num_planes = config->num_total_plane;
888 num_planes = config->num_overlay_plane;
891 * This ioctl is called twice, once to determine how much space is
892 * needed, and the 2nd time to fill it.
895 (plane_resp->count_planes >= num_planes)) {
896 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
898 /* Plane lists are invariant, no locking needed. */
899 drm_for_each_plane(plane, dev) {
901 * Unless userspace set the 'universal planes'
902 * capability bit, only advertise overlays.
904 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
905 !file_priv->universal_planes)
908 if (put_user(plane->base.id, plane_ptr + copied))
913 plane_resp->count_planes = num_planes;
919 * drm_mode_getplane - get plane configuration
922 * @file_priv: DRM file info
924 * Construct a plane configuration structure to return to the user.
926 * Called by the user via ioctl.
929 * Zero on success, negative errno on failure.
931 int drm_mode_getplane(struct drm_device *dev, void *data,
932 struct drm_file *file_priv)
934 struct drm_mode_get_plane *plane_resp = data;
935 struct drm_plane *plane;
936 uint32_t __user *format_ptr;
938 if (!drm_core_check_feature(dev, DRIVER_MODESET))
941 plane = drm_plane_find(dev, plane_resp->plane_id);
945 drm_modeset_lock(&plane->mutex, NULL);
947 plane_resp->crtc_id = plane->crtc->base.id;
949 plane_resp->crtc_id = 0;
952 plane_resp->fb_id = plane->fb->base.id;
954 plane_resp->fb_id = 0;
955 drm_modeset_unlock(&plane->mutex);
957 plane_resp->plane_id = plane->base.id;
958 plane_resp->possible_crtcs = plane->possible_crtcs;
959 plane_resp->gamma_size = 0;
962 * This ioctl is called twice, once to determine how much space is
963 * needed, and the 2nd time to fill it.
965 if (plane->format_count &&
966 (plane_resp->count_format_types >= plane->format_count)) {
967 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
968 if (copy_to_user(format_ptr,
970 sizeof(uint32_t) * plane->format_count)) {
974 plane_resp->count_format_types = plane->format_count;
980 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
981 * @plane: plane to check for format support
982 * @format: the pixel format
985 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
988 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
992 for (i = 0; i < plane->format_count; i++) {
993 if (format == plane->format_types[i])
1000 static int check_src_coords(uint32_t src_x, uint32_t src_y,
1001 uint32_t src_w, uint32_t src_h,
1002 const struct drm_framebuffer *fb)
1004 unsigned int fb_width, fb_height;
1006 fb_width = fb->width << 16;
1007 fb_height = fb->height << 16;
1009 /* Make sure source coordinates are inside the fb. */
1010 if (src_w > fb_width ||
1011 src_x > fb_width - src_w ||
1012 src_h > fb_height ||
1013 src_y > fb_height - src_h) {
1014 DRM_DEBUG_KMS("Invalid source coordinates "
1015 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1016 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
1017 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
1018 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
1019 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
1027 * setplane_internal - setplane handler for internal callers
1029 * Note that we assume an extra reference has already been taken on fb. If the
1030 * update fails, this reference will be dropped before return; if it succeeds,
1031 * the previous framebuffer (if any) will be unreferenced instead.
1033 * src_{x,y,w,h} are provided in 16.16 fixed point format
1035 static int __setplane_internal(struct drm_plane *plane,
1036 struct drm_crtc *crtc,
1037 struct drm_framebuffer *fb,
1038 int32_t crtc_x, int32_t crtc_y,
1039 uint32_t crtc_w, uint32_t crtc_h,
1040 /* src_{x,y,w,h} values are 16.16 fixed point */
1041 uint32_t src_x, uint32_t src_y,
1042 uint32_t src_w, uint32_t src_h)
1046 /* No fb means shut it down */
1048 plane->old_fb = plane->fb;
1049 ret = plane->funcs->disable_plane(plane);
1054 plane->old_fb = NULL;
1059 /* Check whether this plane is usable on this CRTC */
1060 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
1061 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1066 /* Check whether this plane supports the fb pixel format. */
1067 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1069 char *format_name = drm_get_format_name(fb->pixel_format);
1070 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1075 /* Give drivers some help against integer overflows */
1076 if (crtc_w > INT_MAX ||
1077 crtc_x > INT_MAX - (int32_t) crtc_w ||
1079 crtc_y > INT_MAX - (int32_t) crtc_h) {
1080 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1081 crtc_w, crtc_h, crtc_x, crtc_y);
1086 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1090 plane->old_fb = plane->fb;
1091 ret = plane->funcs->update_plane(plane, crtc, fb,
1092 crtc_x, crtc_y, crtc_w, crtc_h,
1093 src_x, src_y, src_w, src_h);
1099 plane->old_fb = NULL;
1104 drm_framebuffer_unreference(fb);
1106 drm_framebuffer_unreference(plane->old_fb);
1107 plane->old_fb = NULL;
1112 static int setplane_internal(struct drm_plane *plane,
1113 struct drm_crtc *crtc,
1114 struct drm_framebuffer *fb,
1115 int32_t crtc_x, int32_t crtc_y,
1116 uint32_t crtc_w, uint32_t crtc_h,
1117 /* src_{x,y,w,h} values are 16.16 fixed point */
1118 uint32_t src_x, uint32_t src_y,
1119 uint32_t src_w, uint32_t src_h)
1123 drm_modeset_lock_all(plane->dev);
1124 ret = __setplane_internal(plane, crtc, fb,
1125 crtc_x, crtc_y, crtc_w, crtc_h,
1126 src_x, src_y, src_w, src_h);
1127 drm_modeset_unlock_all(plane->dev);
1133 * drm_mode_setplane - configure a plane's configuration
1135 * @data: ioctl data*
1136 * @file_priv: DRM file info
1138 * Set plane configuration, including placement, fb, scaling, and other factors.
1139 * Or pass a NULL fb to disable (planes may be disabled without providing a
1143 * Zero on success, negative errno on failure.
1145 int drm_mode_setplane(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
1148 struct drm_mode_set_plane *plane_req = data;
1149 struct drm_plane *plane;
1150 struct drm_crtc *crtc = NULL;
1151 struct drm_framebuffer *fb = NULL;
1153 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1157 * First, find the plane, crtc, and fb objects. If not available,
1158 * we don't bother to call the driver.
1160 plane = drm_plane_find(dev, plane_req->plane_id);
1162 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1163 plane_req->plane_id);
1167 if (plane_req->fb_id) {
1168 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1170 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1175 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1177 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1178 plane_req->crtc_id);
1184 * setplane_internal will take care of deref'ing either the old or new
1185 * framebuffer depending on success.
1187 return setplane_internal(plane, crtc, fb,
1188 plane_req->crtc_x, plane_req->crtc_y,
1189 plane_req->crtc_w, plane_req->crtc_h,
1190 plane_req->src_x, plane_req->src_y,
1191 plane_req->src_w, plane_req->src_h);
1195 * drm_mode_set_config_internal - helper to call ->set_config
1196 * @set: modeset config to set
1198 * This is a little helper to wrap internal calls to the ->set_config driver
1199 * interface. The only thing it adds is correct refcounting dance.
1202 * Zero on success, negative errno on failure.
1204 int drm_mode_set_config_internal(struct drm_mode_set *set)
1206 struct drm_crtc *crtc = set->crtc;
1207 struct drm_framebuffer *fb;
1208 struct drm_crtc *tmp;
1212 * NOTE: ->set_config can also disable other crtcs (if we steal all
1213 * connectors from it), hence we need to refcount the fbs across all
1214 * crtcs. Atomic modeset will have saner semantics ...
1216 drm_for_each_crtc(tmp, crtc->dev)
1217 tmp->primary->old_fb = tmp->primary->fb;
1221 ret = crtc->funcs->set_config(set);
1223 crtc->primary->crtc = crtc;
1224 crtc->primary->fb = fb;
1227 drm_for_each_crtc(tmp, crtc->dev) {
1228 if (tmp->primary->fb)
1229 drm_framebuffer_reference(tmp->primary->fb);
1230 if (tmp->primary->old_fb)
1231 drm_framebuffer_unreference(tmp->primary->old_fb);
1232 tmp->primary->old_fb = NULL;
1237 EXPORT_SYMBOL(drm_mode_set_config_internal);
1240 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1241 * @mode: mode to query
1242 * @hdisplay: hdisplay value to fill in
1243 * @vdisplay: vdisplay value to fill in
1245 * The vdisplay value will be doubled if the specified mode is a stereo mode of
1246 * the appropriate layout.
1248 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1249 int *hdisplay, int *vdisplay)
1251 struct drm_display_mode adjusted;
1253 drm_mode_copy(&adjusted, mode);
1254 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1255 *hdisplay = adjusted.crtc_hdisplay;
1256 *vdisplay = adjusted.crtc_vdisplay;
1258 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1261 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1263 * @crtc: CRTC that framebuffer will be displayed on
1266 * @mode: mode that framebuffer will be displayed under
1267 * @fb: framebuffer to check size of
1269 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1271 const struct drm_display_mode *mode,
1272 const struct drm_framebuffer *fb)
1275 int hdisplay, vdisplay;
1277 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
1280 crtc->primary->state->rotation & (DRM_ROTATE_90 |
1282 swap(hdisplay, vdisplay);
1284 return check_src_coords(x << 16, y << 16,
1285 hdisplay << 16, vdisplay << 16, fb);
1287 EXPORT_SYMBOL(drm_crtc_check_viewport);
1290 * drm_mode_setcrtc - set CRTC configuration
1291 * @dev: drm device for the ioctl
1292 * @data: data pointer for the ioctl
1293 * @file_priv: drm file for the ioctl call
1295 * Build a new CRTC configuration based on user request.
1297 * Called by the user via ioctl.
1300 * Zero on success, negative errno on failure.
1302 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1303 struct drm_file *file_priv)
1305 struct drm_mode_config *config = &dev->mode_config;
1306 struct drm_mode_crtc *crtc_req = data;
1307 struct drm_crtc *crtc;
1308 struct drm_connector **connector_set = NULL, *connector;
1309 struct drm_framebuffer *fb = NULL;
1310 struct drm_display_mode *mode = NULL;
1311 struct drm_mode_set set;
1312 uint32_t __user *set_connectors_ptr;
1316 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1320 * Universal plane src offsets are only 16.16, prevent havoc for
1321 * drivers using universal plane code internally.
1323 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1326 drm_modeset_lock_all(dev);
1327 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1329 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1333 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
1335 if (crtc_req->mode_valid) {
1336 /* If we have a mode we need a framebuffer. */
1337 /* If we pass -1, set the mode with the currently bound fb */
1338 if (crtc_req->fb_id == -1) {
1339 if (!crtc->primary->fb) {
1340 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1344 fb = crtc->primary->fb;
1345 /* Make refcounting symmetric with the lookup path. */
1346 drm_framebuffer_reference(fb);
1348 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1350 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1357 mode = drm_mode_create(dev);
1363 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
1365 DRM_DEBUG_KMS("Invalid mode\n");
1370 * Check whether the primary plane supports the fb pixel format.
1371 * Drivers not implementing the universal planes API use a
1372 * default formats list provided by the DRM core which doesn't
1373 * match real hardware capabilities. Skip the check in that
1376 if (!crtc->primary->format_default) {
1377 ret = drm_plane_check_pixel_format(crtc->primary,
1380 char *format_name = drm_get_format_name(fb->pixel_format);
1381 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1387 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1394 if (crtc_req->count_connectors == 0 && mode) {
1395 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1400 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1401 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1402 crtc_req->count_connectors);
1407 if (crtc_req->count_connectors > 0) {
1410 /* Avoid unbounded kernel memory allocation */
1411 if (crtc_req->count_connectors > config->num_connector) {
1416 connector_set = kmalloc_array(crtc_req->count_connectors,
1417 sizeof(struct drm_connector *),
1419 if (!connector_set) {
1424 for (i = 0; i < crtc_req->count_connectors; i++) {
1425 connector_set[i] = NULL;
1426 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1427 if (get_user(out_id, &set_connectors_ptr[i])) {
1432 connector = drm_connector_lookup(dev, out_id);
1434 DRM_DEBUG_KMS("Connector id %d unknown\n",
1439 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1443 connector_set[i] = connector;
1448 set.x = crtc_req->x;
1449 set.y = crtc_req->y;
1451 set.connectors = connector_set;
1452 set.num_connectors = crtc_req->count_connectors;
1454 ret = drm_mode_set_config_internal(&set);
1458 drm_framebuffer_unreference(fb);
1460 if (connector_set) {
1461 for (i = 0; i < crtc_req->count_connectors; i++) {
1462 if (connector_set[i])
1463 drm_connector_unreference(connector_set[i]);
1466 kfree(connector_set);
1467 drm_mode_destroy(dev, mode);
1468 drm_modeset_unlock_all(dev);
1473 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1474 * universal plane handler call
1475 * @crtc: crtc to update cursor for
1476 * @req: data pointer for the ioctl
1477 * @file_priv: drm file for the ioctl call
1479 * Legacy cursor ioctl's work directly with driver buffer handles. To
1480 * translate legacy ioctl calls into universal plane handler calls, we need to
1481 * wrap the native buffer handle in a drm_framebuffer.
1483 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1484 * buffer with a pitch of 4*width; the universal plane interface should be used
1485 * directly in cases where the hardware can support other buffer settings and
1486 * userspace wants to make use of these capabilities.
1489 * Zero on success, negative errno on failure.
1491 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
1492 struct drm_mode_cursor2 *req,
1493 struct drm_file *file_priv)
1495 struct drm_device *dev = crtc->dev;
1496 struct drm_framebuffer *fb = NULL;
1497 struct drm_mode_fb_cmd2 fbreq = {
1498 .width = req->width,
1499 .height = req->height,
1500 .pixel_format = DRM_FORMAT_ARGB8888,
1501 .pitches = { req->width * 4 },
1502 .handles = { req->handle },
1504 int32_t crtc_x, crtc_y;
1505 uint32_t crtc_w = 0, crtc_h = 0;
1506 uint32_t src_w = 0, src_h = 0;
1509 BUG_ON(!crtc->cursor);
1510 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
1513 * Obtain fb we'll be using (either new or existing) and take an extra
1514 * reference to it if fb != null. setplane will take care of dropping
1515 * the reference if the plane update fails.
1517 if (req->flags & DRM_MODE_CURSOR_BO) {
1519 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
1521 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1524 fb->hot_x = req->hot_x;
1525 fb->hot_y = req->hot_y;
1530 fb = crtc->cursor->fb;
1532 drm_framebuffer_reference(fb);
1535 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1539 crtc_x = crtc->cursor_x;
1540 crtc_y = crtc->cursor_y;
1545 crtc_h = fb->height;
1546 src_w = fb->width << 16;
1547 src_h = fb->height << 16;
1551 * setplane_internal will take care of deref'ing either the old or new
1552 * framebuffer depending on success.
1554 ret = __setplane_internal(crtc->cursor, crtc, fb,
1555 crtc_x, crtc_y, crtc_w, crtc_h,
1556 0, 0, src_w, src_h);
1558 /* Update successful; save new cursor position, if necessary */
1559 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1560 crtc->cursor_x = req->x;
1561 crtc->cursor_y = req->y;
1567 static int drm_mode_cursor_common(struct drm_device *dev,
1568 struct drm_mode_cursor2 *req,
1569 struct drm_file *file_priv)
1571 struct drm_crtc *crtc;
1574 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1577 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1580 crtc = drm_crtc_find(dev, req->crtc_id);
1582 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1587 * If this crtc has a universal cursor plane, call that plane's update
1588 * handler rather than using legacy cursor handlers.
1590 drm_modeset_lock_crtc(crtc, crtc->cursor);
1592 ret = drm_mode_cursor_universal(crtc, req, file_priv);
1596 if (req->flags & DRM_MODE_CURSOR_BO) {
1597 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1601 /* Turns off the cursor if handle is 0 */
1602 if (crtc->funcs->cursor_set2)
1603 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1604 req->width, req->height, req->hot_x, req->hot_y);
1606 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1607 req->width, req->height);
1610 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1611 if (crtc->funcs->cursor_move) {
1612 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1619 drm_modeset_unlock_crtc(crtc);
1627 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
1628 * @dev: drm device for the ioctl
1629 * @data: data pointer for the ioctl
1630 * @file_priv: drm file for the ioctl call
1632 * Set the cursor configuration based on user request.
1634 * Called by the user via ioctl.
1637 * Zero on success, negative errno on failure.
1639 int drm_mode_cursor_ioctl(struct drm_device *dev,
1640 void *data, struct drm_file *file_priv)
1642 struct drm_mode_cursor *req = data;
1643 struct drm_mode_cursor2 new_req;
1645 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1646 new_req.hot_x = new_req.hot_y = 0;
1648 return drm_mode_cursor_common(dev, &new_req, file_priv);
1652 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
1653 * @dev: drm device for the ioctl
1654 * @data: data pointer for the ioctl
1655 * @file_priv: drm file for the ioctl call
1657 * Set the cursor configuration based on user request. This implements the 2nd
1658 * version of the cursor ioctl, which allows userspace to additionally specify
1659 * the hotspot of the pointer.
1661 * Called by the user via ioctl.
1664 * Zero on success, negative errno on failure.
1666 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1667 void *data, struct drm_file *file_priv)
1669 struct drm_mode_cursor2 *req = data;
1671 return drm_mode_cursor_common(dev, req, file_priv);
1674 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
1675 struct drm_property *property,
1679 struct drm_crtc *crtc = obj_to_crtc(obj);
1681 if (crtc->funcs->set_property)
1682 ret = crtc->funcs->set_property(crtc, property, value);
1684 drm_object_property_set_value(obj, property, value);
1690 * drm_mode_plane_set_obj_prop - set the value of a property
1691 * @plane: drm plane object to set property value for
1692 * @property: property to set
1693 * @value: value the property should be set to
1695 * This functions sets a given property on a given plane object. This function
1696 * calls the driver's ->set_property callback and changes the software state of
1697 * the property if the callback succeeds.
1700 * Zero on success, error code on failure.
1702 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
1703 struct drm_property *property,
1707 struct drm_mode_object *obj = &plane->base;
1709 if (plane->funcs->set_property)
1710 ret = plane->funcs->set_property(plane, property, value);
1712 drm_object_property_set_value(obj, property, value);
1716 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
1719 * drm_mode_crtc_set_gamma_size - set the gamma table size
1720 * @crtc: CRTC to set the gamma table size for
1721 * @gamma_size: size of the gamma table
1723 * Drivers which support gamma tables should set this to the supported gamma
1724 * table size when initializing the CRTC. Currently the drm core only supports a
1725 * fixed gamma table size.
1728 * Zero on success, negative errno on failure.
1730 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1733 uint16_t *r_base, *g_base, *b_base;
1736 crtc->gamma_size = gamma_size;
1738 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
1740 if (!crtc->gamma_store) {
1741 crtc->gamma_size = 0;
1745 r_base = crtc->gamma_store;
1746 g_base = r_base + gamma_size;
1747 b_base = g_base + gamma_size;
1748 for (i = 0; i < gamma_size; i++) {
1757 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
1760 * drm_mode_gamma_set_ioctl - set the gamma table
1763 * @file_priv: DRM file info
1765 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
1766 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
1768 * Called by the user via ioctl.
1771 * Zero on success, negative errno on failure.
1773 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
1774 void *data, struct drm_file *file_priv)
1776 struct drm_mode_crtc_lut *crtc_lut = data;
1777 struct drm_crtc *crtc;
1778 void *r_base, *g_base, *b_base;
1782 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1785 drm_modeset_lock_all(dev);
1786 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1792 if (crtc->funcs->gamma_set == NULL) {
1797 /* memcpy into gamma store */
1798 if (crtc_lut->gamma_size != crtc->gamma_size) {
1803 size = crtc_lut->gamma_size * (sizeof(uint16_t));
1804 r_base = crtc->gamma_store;
1805 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
1810 g_base = r_base + size;
1811 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
1816 b_base = g_base + size;
1817 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
1822 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
1825 drm_modeset_unlock_all(dev);
1831 * drm_mode_gamma_get_ioctl - get the gamma table
1834 * @file_priv: DRM file info
1836 * Copy the current gamma table into the storage provided. This also provides
1837 * the gamma table size the driver expects, which can be used to size the
1838 * allocated storage.
1840 * Called by the user via ioctl.
1843 * Zero on success, negative errno on failure.
1845 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1846 void *data, struct drm_file *file_priv)
1848 struct drm_mode_crtc_lut *crtc_lut = data;
1849 struct drm_crtc *crtc;
1850 void *r_base, *g_base, *b_base;
1854 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1857 drm_modeset_lock_all(dev);
1858 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1864 /* memcpy into gamma store */
1865 if (crtc_lut->gamma_size != crtc->gamma_size) {
1870 size = crtc_lut->gamma_size * (sizeof(uint16_t));
1871 r_base = crtc->gamma_store;
1872 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
1877 g_base = r_base + size;
1878 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
1883 b_base = g_base + size;
1884 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
1889 drm_modeset_unlock_all(dev);
1894 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
1897 * @file_priv: DRM file info
1899 * This schedules an asynchronous update on a given CRTC, called page flip.
1900 * Optionally a drm event is generated to signal the completion of the event.
1901 * Generic drivers cannot assume that a pageflip with changed framebuffer
1902 * properties (including driver specific metadata like tiling layout) will work,
1903 * but some drivers support e.g. pixel format changes through the pageflip
1906 * Called by the user via ioctl.
1909 * Zero on success, negative errno on failure.
1911 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1912 void *data, struct drm_file *file_priv)
1914 struct drm_mode_crtc_page_flip_target *page_flip = data;
1915 struct drm_crtc *crtc;
1916 struct drm_framebuffer *fb = NULL;
1917 struct drm_pending_vblank_event *e = NULL;
1918 u32 target_vblank = page_flip->sequence;
1921 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1924 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1927 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1930 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1933 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1936 crtc = drm_crtc_find(dev, page_flip->crtc_id);
1940 if (crtc->funcs->page_flip_target) {
1944 r = drm_crtc_vblank_get(crtc);
1948 current_vblank = drm_crtc_vblank_count(crtc);
1950 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1951 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1952 if ((int)(target_vblank - current_vblank) > 1) {
1953 DRM_DEBUG("Invalid absolute flip target %u, "
1954 "must be <= %u\n", target_vblank,
1955 current_vblank + 1);
1956 drm_crtc_vblank_put(crtc);
1960 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1961 if (target_vblank != 0 && target_vblank != 1) {
1962 DRM_DEBUG("Invalid relative flip target %u, "
1963 "must be 0 or 1\n", target_vblank);
1964 drm_crtc_vblank_put(crtc);
1967 target_vblank += current_vblank;
1970 target_vblank = current_vblank +
1971 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1974 } else if (crtc->funcs->page_flip == NULL ||
1975 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1979 drm_modeset_lock_crtc(crtc, crtc->primary);
1980 if (crtc->primary->fb == NULL) {
1981 /* The framebuffer is currently unbound, presumably
1982 * due to a hotplug event, that userspace has not
1989 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
1996 const struct drm_plane_state *state = crtc->primary->state;
1998 ret = check_src_coords(state->src_x, state->src_y,
1999 state->src_w, state->src_h, fb);
2001 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
2006 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
2007 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
2012 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2013 e = kzalloc(sizeof *e, GFP_KERNEL);
2018 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2019 e->event.base.length = sizeof(e->event);
2020 e->event.user_data = page_flip->user_data;
2021 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
2028 crtc->primary->old_fb = crtc->primary->fb;
2029 if (crtc->funcs->page_flip_target)
2030 ret = crtc->funcs->page_flip_target(crtc, fb, e,
2034 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
2036 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
2037 drm_event_cancel_free(dev, &e->base);
2038 /* Keep the old fb, don't unref it. */
2039 crtc->primary->old_fb = NULL;
2041 crtc->primary->fb = fb;
2042 /* Unref only the old framebuffer. */
2047 if (ret && crtc->funcs->page_flip_target)
2048 drm_crtc_vblank_put(crtc);
2050 drm_framebuffer_unreference(fb);
2051 if (crtc->primary->old_fb)
2052 drm_framebuffer_unreference(crtc->primary->old_fb);
2053 crtc->primary->old_fb = NULL;
2054 drm_modeset_unlock_crtc(crtc);
2060 * drm_mode_config_reset - call ->reset callbacks
2063 * This functions calls all the crtc's, encoder's and connector's ->reset
2064 * callback. Drivers can use this in e.g. their driver load or resume code to
2065 * reset hardware and software state.
2067 void drm_mode_config_reset(struct drm_device *dev)
2069 struct drm_crtc *crtc;
2070 struct drm_plane *plane;
2071 struct drm_encoder *encoder;
2072 struct drm_connector *connector;
2074 drm_for_each_plane(plane, dev)
2075 if (plane->funcs->reset)
2076 plane->funcs->reset(plane);
2078 drm_for_each_crtc(crtc, dev)
2079 if (crtc->funcs->reset)
2080 crtc->funcs->reset(crtc);
2082 drm_for_each_encoder(encoder, dev)
2083 if (encoder->funcs->reset)
2084 encoder->funcs->reset(encoder);
2086 mutex_lock(&dev->mode_config.mutex);
2087 drm_for_each_connector(connector, dev)
2088 if (connector->funcs->reset)
2089 connector->funcs->reset(connector);
2090 mutex_unlock(&dev->mode_config.mutex);
2092 EXPORT_SYMBOL(drm_mode_config_reset);
2095 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
2098 * @file_priv: DRM file info
2100 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
2101 * TTM or something else entirely) and returns the resulting buffer handle. This
2102 * handle can then be wrapped up into a framebuffer modeset object.
2104 * Note that userspace is not allowed to use such objects for render
2105 * acceleration - drivers must create their own private ioctls for such a use
2108 * Called by the user via ioctl.
2111 * Zero on success, negative errno on failure.
2113 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2114 void *data, struct drm_file *file_priv)
2116 struct drm_mode_create_dumb *args = data;
2117 u32 cpp, stride, size;
2119 if (!dev->driver->dumb_create)
2121 if (!args->width || !args->height || !args->bpp)
2124 /* overflow checks for 32bit size calculations */
2125 /* NOTE: DIV_ROUND_UP() can overflow */
2126 cpp = DIV_ROUND_UP(args->bpp, 8);
2127 if (!cpp || cpp > 0xffffffffU / args->width)
2129 stride = cpp * args->width;
2130 if (args->height > 0xffffffffU / stride)
2133 /* test for wrap-around */
2134 size = args->height * stride;
2135 if (PAGE_ALIGN(size) == 0)
2139 * handle, pitch and size are output parameters. Zero them out to
2140 * prevent drivers from accidentally using uninitialized data. Since
2141 * not all existing userspace is clearing these fields properly we
2142 * cannot reject IOCTL with garbage in them.
2148 return dev->driver->dumb_create(file_priv, dev, args);
2152 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
2155 * @file_priv: DRM file info
2157 * Allocate an offset in the drm device node's address space to be able to
2158 * memory map a dumb buffer.
2160 * Called by the user via ioctl.
2163 * Zero on success, negative errno on failure.
2165 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2166 void *data, struct drm_file *file_priv)
2168 struct drm_mode_map_dumb *args = data;
2170 /* call driver ioctl to get mmap offset */
2171 if (!dev->driver->dumb_map_offset)
2174 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2178 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
2181 * @file_priv: DRM file info
2183 * This destroys the userspace handle for the given dumb backing storage buffer.
2184 * Since buffer objects must be reference counted in the kernel a buffer object
2185 * won't be immediately freed if a framebuffer modeset object still uses it.
2187 * Called by the user via ioctl.
2190 * Zero on success, negative errno on failure.
2192 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2193 void *data, struct drm_file *file_priv)
2195 struct drm_mode_destroy_dumb *args = data;
2197 if (!dev->driver->dumb_destroy)
2200 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2204 * drm_rotation_simplify() - Try to simplify the rotation
2205 * @rotation: Rotation to be simplified
2206 * @supported_rotations: Supported rotations
2208 * Attempt to simplify the rotation to a form that is supported.
2209 * Eg. if the hardware supports everything except DRM_REFLECT_X
2210 * one could call this function like this:
2212 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
2213 * DRM_ROTATE_90 | DRM_ROTATE_180 |
2214 * DRM_ROTATE_270 | DRM_REFLECT_Y);
2216 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
2217 * transforms the hardware supports, this function may not
2218 * be able to produce a supported transform, so the caller should
2219 * check the result afterwards.
2221 unsigned int drm_rotation_simplify(unsigned int rotation,
2222 unsigned int supported_rotations)
2224 if (rotation & ~supported_rotations) {
2225 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
2226 rotation = (rotation & DRM_REFLECT_MASK) |
2227 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
2232 EXPORT_SYMBOL(drm_rotation_simplify);
2235 * drm_mode_config_init - initialize DRM mode_configuration structure
2238 * Initialize @dev's mode_config structure, used for tracking the graphics
2239 * configuration of @dev.
2241 * Since this initializes the modeset locks, no locking is possible. Which is no
2242 * problem, since this should happen single threaded at init time. It is the
2243 * driver's problem to ensure this guarantee.
2246 void drm_mode_config_init(struct drm_device *dev)
2248 mutex_init(&dev->mode_config.mutex);
2249 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
2250 mutex_init(&dev->mode_config.idr_mutex);
2251 mutex_init(&dev->mode_config.fb_lock);
2252 mutex_init(&dev->mode_config.blob_lock);
2253 INIT_LIST_HEAD(&dev->mode_config.fb_list);
2254 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
2255 INIT_LIST_HEAD(&dev->mode_config.connector_list);
2256 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
2257 INIT_LIST_HEAD(&dev->mode_config.property_list);
2258 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
2259 INIT_LIST_HEAD(&dev->mode_config.plane_list);
2260 idr_init(&dev->mode_config.crtc_idr);
2261 idr_init(&dev->mode_config.tile_idr);
2262 ida_init(&dev->mode_config.connector_ida);
2264 drm_modeset_lock_all(dev);
2265 drm_mode_create_standard_properties(dev);
2266 drm_modeset_unlock_all(dev);
2268 /* Just to be sure */
2269 dev->mode_config.num_fb = 0;
2270 dev->mode_config.num_connector = 0;
2271 dev->mode_config.num_crtc = 0;
2272 dev->mode_config.num_encoder = 0;
2273 dev->mode_config.num_overlay_plane = 0;
2274 dev->mode_config.num_total_plane = 0;
2276 EXPORT_SYMBOL(drm_mode_config_init);
2279 * drm_mode_config_cleanup - free up DRM mode_config info
2282 * Free up all the connectors and CRTCs associated with this DRM device, then
2283 * free up the framebuffers and associated buffer objects.
2285 * Note that since this /should/ happen single-threaded at driver/device
2286 * teardown time, no locking is required. It's the driver's job to ensure that
2287 * this guarantee actually holds true.
2289 * FIXME: cleanup any dangling user buffer objects too
2291 void drm_mode_config_cleanup(struct drm_device *dev)
2293 struct drm_connector *connector, *ot;
2294 struct drm_crtc *crtc, *ct;
2295 struct drm_encoder *encoder, *enct;
2296 struct drm_framebuffer *fb, *fbt;
2297 struct drm_property *property, *pt;
2298 struct drm_property_blob *blob, *bt;
2299 struct drm_plane *plane, *plt;
2301 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
2303 encoder->funcs->destroy(encoder);
2306 list_for_each_entry_safe(connector, ot,
2307 &dev->mode_config.connector_list, head) {
2308 connector->funcs->destroy(connector);
2311 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
2313 drm_property_destroy(dev, property);
2316 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
2318 plane->funcs->destroy(plane);
2321 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
2322 crtc->funcs->destroy(crtc);
2325 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
2327 drm_property_unreference_blob(blob);
2331 * Single-threaded teardown context, so it's not required to grab the
2332 * fb_lock to protect against concurrent fb_list access. Contrary, it
2333 * would actually deadlock with the drm_framebuffer_cleanup function.
2335 * Also, if there are any framebuffers left, that's a driver leak now,
2336 * so politely WARN about this.
2338 WARN_ON(!list_empty(&dev->mode_config.fb_list));
2339 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
2340 drm_framebuffer_free(&fb->base.refcount);
2343 ida_destroy(&dev->mode_config.connector_ida);
2344 idr_destroy(&dev->mode_config.tile_idr);
2345 idr_destroy(&dev->mode_config.crtc_idr);
2346 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
2348 EXPORT_SYMBOL(drm_mode_config_cleanup);
2350 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2351 unsigned int supported_rotations)
2353 static const struct drm_prop_enum_list props[] = {
2354 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
2355 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
2356 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
2357 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
2358 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
2359 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
2362 return drm_property_create_bitmask(dev, 0, "rotation",
2363 props, ARRAY_SIZE(props),
2364 supported_rotations);
2366 EXPORT_SYMBOL(drm_mode_create_rotation_property);
2371 * Tile groups are used to represent tiled monitors with a unique
2372 * integer identifier. Tiled monitors using DisplayID v1.3 have
2373 * a unique 8-byte handle, we store this in a tile group, so we
2374 * have a common identifier for all tiles in a monitor group.
2376 static void drm_tile_group_free(struct kref *kref)
2378 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2379 struct drm_device *dev = tg->dev;
2380 mutex_lock(&dev->mode_config.idr_mutex);
2381 idr_remove(&dev->mode_config.tile_idr, tg->id);
2382 mutex_unlock(&dev->mode_config.idr_mutex);
2387 * drm_mode_put_tile_group - drop a reference to a tile group.
2389 * @tg: tile group to drop reference to.
2391 * drop reference to tile group and free if 0.
2393 void drm_mode_put_tile_group(struct drm_device *dev,
2394 struct drm_tile_group *tg)
2396 kref_put(&tg->refcount, drm_tile_group_free);
2400 * drm_mode_get_tile_group - get a reference to an existing tile group
2402 * @topology: 8-bytes unique per monitor.
2404 * Use the unique bytes to get a reference to an existing tile group.
2407 * tile group or NULL if not found.
2409 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2412 struct drm_tile_group *tg;
2414 mutex_lock(&dev->mode_config.idr_mutex);
2415 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2416 if (!memcmp(tg->group_data, topology, 8)) {
2417 if (!kref_get_unless_zero(&tg->refcount))
2419 mutex_unlock(&dev->mode_config.idr_mutex);
2423 mutex_unlock(&dev->mode_config.idr_mutex);
2426 EXPORT_SYMBOL(drm_mode_get_tile_group);
2429 * drm_mode_create_tile_group - create a tile group from a displayid description
2431 * @topology: 8-bytes unique per monitor.
2433 * Create a tile group for the unique monitor, and get a unique
2434 * identifier for the tile group.
2437 * new tile group or error.
2439 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2442 struct drm_tile_group *tg;
2445 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2447 return ERR_PTR(-ENOMEM);
2449 kref_init(&tg->refcount);
2450 memcpy(tg->group_data, topology, 8);
2453 mutex_lock(&dev->mode_config.idr_mutex);
2454 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2462 mutex_unlock(&dev->mode_config.idr_mutex);
2465 EXPORT_SYMBOL(drm_mode_create_tile_group);
2468 * drm_crtc_enable_color_mgmt - enable color management properties
2470 * @degamma_lut_size: the size of the degamma lut (before CSC)
2471 * @has_ctm: whether to attach ctm_property for CSC matrix
2472 * @gamma_lut_size: the size of the gamma lut (after CSC)
2474 * This function lets the driver enable the color correction
2475 * properties on a CRTC. This includes 3 degamma, csc and gamma
2476 * properties that userspace can set and 2 size properties to inform
2477 * the userspace of the lut sizes. Each of the properties are
2478 * optional. The gamma and degamma properties are only attached if
2479 * their size is not 0 and ctm_property is only attached if has_ctm is
2482 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
2483 uint degamma_lut_size,
2485 uint gamma_lut_size)
2487 struct drm_device *dev = crtc->dev;
2488 struct drm_mode_config *config = &dev->mode_config;
2490 if (degamma_lut_size) {
2491 drm_object_attach_property(&crtc->base,
2492 config->degamma_lut_property, 0);
2493 drm_object_attach_property(&crtc->base,
2494 config->degamma_lut_size_property,
2499 drm_object_attach_property(&crtc->base,
2500 config->ctm_property, 0);
2502 if (gamma_lut_size) {
2503 drm_object_attach_property(&crtc->base,
2504 config->gamma_lut_property, 0);
2505 drm_object_attach_property(&crtc->base,
2506 config->gamma_lut_size_property,
2510 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);