2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 * Copyright (C) 2018 Intel Corp.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Rob Clark <robdclark@gmail.com>
26 * Daniel Vetter <daniel.vetter@ffwll.ch>
29 #include <drm/drm_atomic_uapi.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_drv.h>
33 #include <drm/drm_writeback.h>
34 #include <drm/drm_vblank.h>
36 #include <linux/dma-fence.h>
37 #include <linux/uaccess.h>
38 #include <linux/sync_file.h>
39 #include <linux/file.h>
41 #include "drm_crtc_internal.h"
46 * This file contains the marshalling and demarshalling glue for the atomic UAPI
47 * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
48 * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
49 * drivers which have special needs to construct their own atomic updates, e.g.
50 * for load detect or similiar.
54 * drm_atomic_set_mode_for_crtc - set mode for CRTC
55 * @state: the CRTC whose incoming state to update
56 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
58 * Set a mode (originating from the kernel) on the desired CRTC state and update
59 * the enable property.
62 * Zero on success, error code on failure. Cannot return -EDEADLK.
64 int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
65 const struct drm_display_mode *mode)
67 struct drm_crtc *crtc = state->crtc;
68 struct drm_mode_modeinfo umode;
70 /* Early return for no change. */
71 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
74 drm_property_blob_put(state->mode_blob);
75 state->mode_blob = NULL;
78 struct drm_property_blob *blob;
80 drm_mode_convert_to_umode(&umode, mode);
81 blob = drm_property_create_blob(crtc->dev,
82 sizeof(umode), &umode);
86 drm_mode_copy(&state->mode, mode);
88 state->mode_blob = blob;
90 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
91 mode->name, crtc->base.id, crtc->name, state);
93 memset(&state->mode, 0, sizeof(state->mode));
94 state->enable = false;
95 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
96 crtc->base.id, crtc->name, state);
101 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
104 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
105 * @state: the CRTC whose incoming state to update
106 * @blob: pointer to blob property to use for mode
108 * Set a mode (originating from a blob property) on the desired CRTC state.
109 * This function will take a reference on the blob property for the CRTC state,
110 * and release the reference held on the state's existing mode property, if any
114 * Zero on success, error code on failure. Cannot return -EDEADLK.
116 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
117 struct drm_property_blob *blob)
119 struct drm_crtc *crtc = state->crtc;
121 if (blob == state->mode_blob)
124 drm_property_blob_put(state->mode_blob);
125 state->mode_blob = NULL;
127 memset(&state->mode, 0, sizeof(state->mode));
132 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
133 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
134 crtc->base.id, crtc->name,
139 ret = drm_mode_convert_umode(crtc->dev,
140 &state->mode, blob->data);
142 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
143 crtc->base.id, crtc->name,
144 ret, drm_get_mode_status_name(state->mode.status));
145 drm_mode_debug_printmodeline(&state->mode);
149 state->mode_blob = drm_property_blob_get(blob);
150 state->enable = true;
151 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
152 state->mode.name, crtc->base.id, crtc->name,
155 state->enable = false;
156 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
157 crtc->base.id, crtc->name, state);
162 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
165 * drm_atomic_set_crtc_for_plane - set CRTC for plane
166 * @plane_state: the plane whose incoming state to update
167 * @crtc: CRTC to use for the plane
169 * Changing the assigned CRTC for a plane requires us to grab the lock and state
170 * for the new CRTC, as needed. This function takes care of all these details
171 * besides updating the pointer in the state object itself.
174 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
175 * then the w/w mutex code has detected a deadlock and the entire atomic
176 * sequence must be restarted. All other errors are fatal.
179 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
180 struct drm_crtc *crtc)
182 struct drm_plane *plane = plane_state->plane;
183 struct drm_crtc_state *crtc_state;
184 /* Nothing to do for same crtc*/
185 if (plane_state->crtc == crtc)
187 if (plane_state->crtc) {
188 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
190 if (WARN_ON(IS_ERR(crtc_state)))
191 return PTR_ERR(crtc_state);
193 crtc_state->plane_mask &= ~drm_plane_mask(plane);
196 plane_state->crtc = crtc;
199 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
201 if (IS_ERR(crtc_state))
202 return PTR_ERR(crtc_state);
203 crtc_state->plane_mask |= drm_plane_mask(plane);
207 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
208 plane->base.id, plane->name, plane_state,
209 crtc->base.id, crtc->name);
211 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
212 plane->base.id, plane->name, plane_state);
216 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
219 * drm_atomic_set_fb_for_plane - set framebuffer for plane
220 * @plane_state: atomic state object for the plane
221 * @fb: fb to use for the plane
223 * Changing the assigned framebuffer for a plane requires us to grab a reference
224 * to the new fb and drop the reference to the old fb, if there is one. This
225 * function takes care of all these details besides updating the pointer in the
226 * state object itself.
229 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
230 struct drm_framebuffer *fb)
232 struct drm_plane *plane = plane_state->plane;
235 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
236 fb->base.id, plane->base.id, plane->name,
239 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
240 plane->base.id, plane->name, plane_state);
242 drm_framebuffer_assign(&plane_state->fb, fb);
244 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
247 * drm_atomic_set_fence_for_plane - set fence for plane
248 * @plane_state: atomic state object for the plane
249 * @fence: dma_fence to use for the plane
251 * Helper to setup the plane_state fence in case it is not set yet.
252 * By using this drivers doesn't need to worry if the user choose
253 * implicit or explicit fencing.
255 * This function will not set the fence to the state if it was set
256 * via explicit fencing interfaces on the atomic ioctl. In that case it will
257 * drop the reference to the fence as we are not storing it anywhere.
258 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
259 * with the received implicit fence. In both cases this function consumes a
260 * reference for @fence.
262 * This way explicit fencing can be used to overrule implicit fencing, which is
263 * important to make explicit fencing use-cases work: One example is using one
264 * buffer for 2 screens with different refresh rates. Implicit fencing will
265 * clamp rendering to the refresh rate of the slower screen, whereas explicit
266 * fence allows 2 independent render and display loops on a single buffer. If a
267 * driver allows obeys both implicit and explicit fences for plane updates, then
268 * it will break all the benefits of explicit fencing.
271 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
272 struct dma_fence *fence)
274 if (plane_state->fence) {
275 dma_fence_put(fence);
279 plane_state->fence = fence;
281 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
284 * drm_atomic_set_crtc_for_connector - set CRTC for connector
285 * @conn_state: atomic state object for the connector
286 * @crtc: CRTC to use for the connector
288 * Changing the assigned CRTC for a connector requires us to grab the lock and
289 * state for the new CRTC, as needed. This function takes care of all these
290 * details besides updating the pointer in the state object itself.
293 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
294 * then the w/w mutex code has detected a deadlock and the entire atomic
295 * sequence must be restarted. All other errors are fatal.
298 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
299 struct drm_crtc *crtc)
301 struct drm_connector *connector = conn_state->connector;
302 struct drm_crtc_state *crtc_state;
304 if (conn_state->crtc == crtc)
307 if (conn_state->crtc) {
308 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
311 crtc_state->connector_mask &=
312 ~drm_connector_mask(conn_state->connector);
314 drm_connector_put(conn_state->connector);
315 conn_state->crtc = NULL;
319 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
320 if (IS_ERR(crtc_state))
321 return PTR_ERR(crtc_state);
323 crtc_state->connector_mask |=
324 drm_connector_mask(conn_state->connector);
326 drm_connector_get(conn_state->connector);
327 conn_state->crtc = crtc;
329 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
330 connector->base.id, connector->name,
331 conn_state, crtc->base.id, crtc->name);
333 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
334 connector->base.id, connector->name,
340 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
342 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
343 struct drm_crtc *crtc, s32 __user *fence_ptr)
345 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
348 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
349 struct drm_crtc *crtc)
351 s32 __user *fence_ptr;
353 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
354 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
359 static int set_out_fence_for_connector(struct drm_atomic_state *state,
360 struct drm_connector *connector,
361 s32 __user *fence_ptr)
363 unsigned int index = drm_connector_index(connector);
368 if (put_user(-1, fence_ptr))
371 state->connectors[index].out_fence_ptr = fence_ptr;
376 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
377 struct drm_connector *connector)
379 unsigned int index = drm_connector_index(connector);
380 s32 __user *fence_ptr;
382 fence_ptr = state->connectors[index].out_fence_ptr;
383 state->connectors[index].out_fence_ptr = NULL;
389 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
390 struct drm_property_blob **blob,
392 ssize_t expected_size,
393 ssize_t expected_elem_size,
396 struct drm_property_blob *new_blob = NULL;
399 new_blob = drm_property_lookup_blob(dev, blob_id);
400 if (new_blob == NULL)
403 if (expected_size > 0 &&
404 new_blob->length != expected_size) {
405 drm_property_blob_put(new_blob);
408 if (expected_elem_size > 0 &&
409 new_blob->length % expected_elem_size != 0) {
410 drm_property_blob_put(new_blob);
415 *replaced |= drm_property_replace_blob(blob, new_blob);
416 drm_property_blob_put(new_blob);
421 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
422 struct drm_crtc_state *state, struct drm_property *property,
425 struct drm_device *dev = crtc->dev;
426 struct drm_mode_config *config = &dev->mode_config;
427 bool replaced = false;
430 if (property == config->prop_active)
432 else if (property == config->prop_mode_id) {
433 struct drm_property_blob *mode =
434 drm_property_lookup_blob(dev, val);
435 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
436 drm_property_blob_put(mode);
438 } else if (property == config->prop_vrr_enabled) {
439 state->vrr_enabled = val;
440 } else if (property == config->degamma_lut_property) {
441 ret = drm_atomic_replace_property_blob_from_id(dev,
444 -1, sizeof(struct drm_color_lut),
446 state->color_mgmt_changed |= replaced;
448 } else if (property == config->ctm_property) {
449 ret = drm_atomic_replace_property_blob_from_id(dev,
452 sizeof(struct drm_color_ctm), -1,
454 state->color_mgmt_changed |= replaced;
456 } else if (property == config->gamma_lut_property) {
457 ret = drm_atomic_replace_property_blob_from_id(dev,
460 -1, sizeof(struct drm_color_lut),
462 state->color_mgmt_changed |= replaced;
464 } else if (property == config->prop_out_fence_ptr) {
465 s32 __user *fence_ptr = u64_to_user_ptr(val);
470 if (put_user(-1, fence_ptr))
473 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
474 } else if (crtc->funcs->atomic_set_property) {
475 return crtc->funcs->atomic_set_property(crtc, state, property, val);
477 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
478 crtc->base.id, crtc->name,
479 property->base.id, property->name);
487 drm_atomic_crtc_get_property(struct drm_crtc *crtc,
488 const struct drm_crtc_state *state,
489 struct drm_property *property, uint64_t *val)
491 struct drm_device *dev = crtc->dev;
492 struct drm_mode_config *config = &dev->mode_config;
494 if (property == config->prop_active)
495 *val = drm_atomic_crtc_effectively_active(state);
496 else if (property == config->prop_mode_id)
497 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
498 else if (property == config->prop_vrr_enabled)
499 *val = state->vrr_enabled;
500 else if (property == config->degamma_lut_property)
501 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
502 else if (property == config->ctm_property)
503 *val = (state->ctm) ? state->ctm->base.id : 0;
504 else if (property == config->gamma_lut_property)
505 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
506 else if (property == config->prop_out_fence_ptr)
508 else if (crtc->funcs->atomic_get_property)
509 return crtc->funcs->atomic_get_property(crtc, state, property, val);
516 static int drm_atomic_plane_set_property(struct drm_plane *plane,
517 struct drm_plane_state *state, struct drm_file *file_priv,
518 struct drm_property *property, uint64_t val)
520 struct drm_device *dev = plane->dev;
521 struct drm_mode_config *config = &dev->mode_config;
522 bool replaced = false;
525 if (property == config->prop_fb_id) {
526 struct drm_framebuffer *fb;
528 fb = drm_framebuffer_lookup(dev, file_priv, val);
529 drm_atomic_set_fb_for_plane(state, fb);
531 drm_framebuffer_put(fb);
532 } else if (property == config->prop_in_fence_fd) {
536 if (U642I64(val) == -1)
539 state->fence = sync_file_get_fence(val);
543 } else if (property == config->prop_crtc_id) {
544 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
548 return drm_atomic_set_crtc_for_plane(state, crtc);
549 } else if (property == config->prop_crtc_x) {
550 state->crtc_x = U642I64(val);
551 } else if (property == config->prop_crtc_y) {
552 state->crtc_y = U642I64(val);
553 } else if (property == config->prop_crtc_w) {
555 } else if (property == config->prop_crtc_h) {
557 } else if (property == config->prop_src_x) {
559 } else if (property == config->prop_src_y) {
561 } else if (property == config->prop_src_w) {
563 } else if (property == config->prop_src_h) {
565 } else if (property == plane->alpha_property) {
567 } else if (property == plane->blend_mode_property) {
568 state->pixel_blend_mode = val;
569 } else if (property == plane->rotation_property) {
570 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
571 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
572 plane->base.id, plane->name, val);
575 state->rotation = val;
576 } else if (property == plane->zpos_property) {
578 } else if (property == plane->color_encoding_property) {
579 state->color_encoding = val;
580 } else if (property == plane->color_range_property) {
581 state->color_range = val;
582 } else if (property == config->prop_fb_damage_clips) {
583 ret = drm_atomic_replace_property_blob_from_id(dev,
584 &state->fb_damage_clips,
587 sizeof(struct drm_rect),
590 } else if (plane->funcs->atomic_set_property) {
591 return plane->funcs->atomic_set_property(plane, state,
594 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
595 plane->base.id, plane->name,
596 property->base.id, property->name);
604 drm_atomic_plane_get_property(struct drm_plane *plane,
605 const struct drm_plane_state *state,
606 struct drm_property *property, uint64_t *val)
608 struct drm_device *dev = plane->dev;
609 struct drm_mode_config *config = &dev->mode_config;
611 if (property == config->prop_fb_id) {
612 *val = (state->fb) ? state->fb->base.id : 0;
613 } else if (property == config->prop_in_fence_fd) {
615 } else if (property == config->prop_crtc_id) {
616 *val = (state->crtc) ? state->crtc->base.id : 0;
617 } else if (property == config->prop_crtc_x) {
618 *val = I642U64(state->crtc_x);
619 } else if (property == config->prop_crtc_y) {
620 *val = I642U64(state->crtc_y);
621 } else if (property == config->prop_crtc_w) {
622 *val = state->crtc_w;
623 } else if (property == config->prop_crtc_h) {
624 *val = state->crtc_h;
625 } else if (property == config->prop_src_x) {
627 } else if (property == config->prop_src_y) {
629 } else if (property == config->prop_src_w) {
631 } else if (property == config->prop_src_h) {
633 } else if (property == plane->alpha_property) {
635 } else if (property == plane->blend_mode_property) {
636 *val = state->pixel_blend_mode;
637 } else if (property == plane->rotation_property) {
638 *val = state->rotation;
639 } else if (property == plane->zpos_property) {
641 } else if (property == plane->color_encoding_property) {
642 *val = state->color_encoding;
643 } else if (property == plane->color_range_property) {
644 *val = state->color_range;
645 } else if (property == config->prop_fb_damage_clips) {
646 *val = (state->fb_damage_clips) ?
647 state->fb_damage_clips->base.id : 0;
648 } else if (plane->funcs->atomic_get_property) {
649 return plane->funcs->atomic_get_property(plane, state, property, val);
657 static int drm_atomic_set_writeback_fb_for_connector(
658 struct drm_connector_state *conn_state,
659 struct drm_framebuffer *fb)
663 ret = drm_writeback_set_fb(conn_state, fb);
668 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
669 fb->base.id, conn_state);
671 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
677 static int drm_atomic_connector_set_property(struct drm_connector *connector,
678 struct drm_connector_state *state, struct drm_file *file_priv,
679 struct drm_property *property, uint64_t val)
681 struct drm_device *dev = connector->dev;
682 struct drm_mode_config *config = &dev->mode_config;
683 bool replaced = false;
686 if (property == config->prop_crtc_id) {
687 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
691 return drm_atomic_set_crtc_for_connector(state, crtc);
692 } else if (property == config->dpms_property) {
693 /* setting DPMS property requires special handling, which
694 * is done in legacy setprop path for us. Disallow (for
695 * now?) atomic writes to DPMS property:
698 } else if (property == config->tv_select_subconnector_property) {
699 state->tv.subconnector = val;
700 } else if (property == config->tv_left_margin_property) {
701 state->tv.margins.left = val;
702 } else if (property == config->tv_right_margin_property) {
703 state->tv.margins.right = val;
704 } else if (property == config->tv_top_margin_property) {
705 state->tv.margins.top = val;
706 } else if (property == config->tv_bottom_margin_property) {
707 state->tv.margins.bottom = val;
708 } else if (property == config->tv_mode_property) {
709 state->tv.mode = val;
710 } else if (property == config->tv_brightness_property) {
711 state->tv.brightness = val;
712 } else if (property == config->tv_contrast_property) {
713 state->tv.contrast = val;
714 } else if (property == config->tv_flicker_reduction_property) {
715 state->tv.flicker_reduction = val;
716 } else if (property == config->tv_overscan_property) {
717 state->tv.overscan = val;
718 } else if (property == config->tv_saturation_property) {
719 state->tv.saturation = val;
720 } else if (property == config->tv_hue_property) {
722 } else if (property == config->link_status_property) {
723 /* Never downgrade from GOOD to BAD on userspace's request here,
724 * only hw issues can do that.
726 * For an atomic property the userspace doesn't need to be able
727 * to understand all the properties, but needs to be able to
728 * restore the state it wants on VT switch. So if the userspace
729 * tries to change the link_status from GOOD to BAD, driver
730 * silently rejects it and returns a 0. This prevents userspace
731 * from accidently breaking the display when it restores the
734 if (state->link_status != DRM_LINK_STATUS_GOOD)
735 state->link_status = val;
736 } else if (property == config->hdr_output_metadata_property) {
737 ret = drm_atomic_replace_property_blob_from_id(dev,
738 &state->hdr_output_metadata,
740 sizeof(struct hdr_output_metadata), -1,
743 } else if (property == config->aspect_ratio_property) {
744 state->picture_aspect_ratio = val;
745 } else if (property == config->content_type_property) {
746 state->content_type = val;
747 } else if (property == connector->scaling_mode_property) {
748 state->scaling_mode = val;
749 } else if (property == config->content_protection_property) {
750 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
751 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
754 state->content_protection = val;
755 } else if (property == config->hdcp_content_type_property) {
756 state->hdcp_content_type = val;
757 } else if (property == connector->colorspace_property) {
758 state->colorspace = val;
759 } else if (property == config->writeback_fb_id_property) {
760 struct drm_framebuffer *fb;
763 fb = drm_framebuffer_lookup(dev, file_priv, val);
764 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
766 drm_framebuffer_put(fb);
768 } else if (property == config->writeback_out_fence_ptr_property) {
769 s32 __user *fence_ptr = u64_to_user_ptr(val);
771 return set_out_fence_for_connector(state->state, connector,
773 } else if (property == connector->max_bpc_property) {
774 state->max_requested_bpc = val;
775 } else if (connector->funcs->atomic_set_property) {
776 return connector->funcs->atomic_set_property(connector,
777 state, property, val);
779 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
780 connector->base.id, connector->name,
781 property->base.id, property->name);
789 drm_atomic_connector_get_property(struct drm_connector *connector,
790 const struct drm_connector_state *state,
791 struct drm_property *property, uint64_t *val)
793 struct drm_device *dev = connector->dev;
794 struct drm_mode_config *config = &dev->mode_config;
796 if (property == config->prop_crtc_id) {
797 *val = (state->crtc) ? state->crtc->base.id : 0;
798 } else if (property == config->dpms_property) {
799 if (state->crtc && state->crtc->state->self_refresh_active)
800 *val = DRM_MODE_DPMS_ON;
802 *val = connector->dpms;
803 } else if (property == config->tv_select_subconnector_property) {
804 *val = state->tv.subconnector;
805 } else if (property == config->tv_left_margin_property) {
806 *val = state->tv.margins.left;
807 } else if (property == config->tv_right_margin_property) {
808 *val = state->tv.margins.right;
809 } else if (property == config->tv_top_margin_property) {
810 *val = state->tv.margins.top;
811 } else if (property == config->tv_bottom_margin_property) {
812 *val = state->tv.margins.bottom;
813 } else if (property == config->tv_mode_property) {
814 *val = state->tv.mode;
815 } else if (property == config->tv_brightness_property) {
816 *val = state->tv.brightness;
817 } else if (property == config->tv_contrast_property) {
818 *val = state->tv.contrast;
819 } else if (property == config->tv_flicker_reduction_property) {
820 *val = state->tv.flicker_reduction;
821 } else if (property == config->tv_overscan_property) {
822 *val = state->tv.overscan;
823 } else if (property == config->tv_saturation_property) {
824 *val = state->tv.saturation;
825 } else if (property == config->tv_hue_property) {
826 *val = state->tv.hue;
827 } else if (property == config->link_status_property) {
828 *val = state->link_status;
829 } else if (property == config->aspect_ratio_property) {
830 *val = state->picture_aspect_ratio;
831 } else if (property == config->content_type_property) {
832 *val = state->content_type;
833 } else if (property == connector->colorspace_property) {
834 *val = state->colorspace;
835 } else if (property == connector->scaling_mode_property) {
836 *val = state->scaling_mode;
837 } else if (property == config->hdr_output_metadata_property) {
838 *val = state->hdr_output_metadata ?
839 state->hdr_output_metadata->base.id : 0;
840 } else if (property == config->content_protection_property) {
841 *val = state->content_protection;
842 } else if (property == config->hdcp_content_type_property) {
843 *val = state->hdcp_content_type;
844 } else if (property == config->writeback_fb_id_property) {
845 /* Writeback framebuffer is one-shot, write and forget */
847 } else if (property == config->writeback_out_fence_ptr_property) {
849 } else if (property == connector->max_bpc_property) {
850 *val = state->max_requested_bpc;
851 } else if (connector->funcs->atomic_get_property) {
852 return connector->funcs->atomic_get_property(connector,
853 state, property, val);
861 int drm_atomic_get_property(struct drm_mode_object *obj,
862 struct drm_property *property, uint64_t *val)
864 struct drm_device *dev = property->dev;
868 case DRM_MODE_OBJECT_CONNECTOR: {
869 struct drm_connector *connector = obj_to_connector(obj);
871 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
872 ret = drm_atomic_connector_get_property(connector,
873 connector->state, property, val);
876 case DRM_MODE_OBJECT_CRTC: {
877 struct drm_crtc *crtc = obj_to_crtc(obj);
879 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
880 ret = drm_atomic_crtc_get_property(crtc,
881 crtc->state, property, val);
884 case DRM_MODE_OBJECT_PLANE: {
885 struct drm_plane *plane = obj_to_plane(obj);
887 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
888 ret = drm_atomic_plane_get_property(plane,
889 plane->state, property, val);
901 * The big monster ioctl
904 static struct drm_pending_vblank_event *create_vblank_event(
905 struct drm_crtc *crtc, uint64_t user_data)
907 struct drm_pending_vblank_event *e = NULL;
909 e = kzalloc(sizeof *e, GFP_KERNEL);
913 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
914 e->event.base.length = sizeof(e->event);
915 e->event.vbl.crtc_id = crtc->base.id;
916 e->event.vbl.user_data = user_data;
921 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
922 struct drm_connector *connector,
925 struct drm_connector *tmp_connector;
926 struct drm_connector_state *new_conn_state;
927 struct drm_crtc *crtc;
928 struct drm_crtc_state *crtc_state;
929 int i, ret, old_mode = connector->dpms;
932 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
937 if (mode != DRM_MODE_DPMS_ON)
938 mode = DRM_MODE_DPMS_OFF;
939 connector->dpms = mode;
941 crtc = connector->state->crtc;
944 ret = drm_atomic_add_affected_connectors(state, crtc);
948 crtc_state = drm_atomic_get_crtc_state(state, crtc);
949 if (IS_ERR(crtc_state)) {
950 ret = PTR_ERR(crtc_state);
954 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
955 if (new_conn_state->crtc != crtc)
957 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
963 crtc_state->active = active;
964 ret = drm_atomic_commit(state);
967 connector->dpms = old_mode;
971 int drm_atomic_set_property(struct drm_atomic_state *state,
972 struct drm_file *file_priv,
973 struct drm_mode_object *obj,
974 struct drm_property *prop,
977 struct drm_mode_object *ref;
980 if (!drm_property_change_valid_get(prop, prop_value, &ref))
984 case DRM_MODE_OBJECT_CONNECTOR: {
985 struct drm_connector *connector = obj_to_connector(obj);
986 struct drm_connector_state *connector_state;
988 connector_state = drm_atomic_get_connector_state(state, connector);
989 if (IS_ERR(connector_state)) {
990 ret = PTR_ERR(connector_state);
994 ret = drm_atomic_connector_set_property(connector,
995 connector_state, file_priv,
999 case DRM_MODE_OBJECT_CRTC: {
1000 struct drm_crtc *crtc = obj_to_crtc(obj);
1001 struct drm_crtc_state *crtc_state;
1003 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1004 if (IS_ERR(crtc_state)) {
1005 ret = PTR_ERR(crtc_state);
1009 ret = drm_atomic_crtc_set_property(crtc,
1010 crtc_state, prop, prop_value);
1013 case DRM_MODE_OBJECT_PLANE: {
1014 struct drm_plane *plane = obj_to_plane(obj);
1015 struct drm_plane_state *plane_state;
1017 plane_state = drm_atomic_get_plane_state(state, plane);
1018 if (IS_ERR(plane_state)) {
1019 ret = PTR_ERR(plane_state);
1023 ret = drm_atomic_plane_set_property(plane,
1024 plane_state, file_priv,
1033 drm_property_change_valid_put(prop, ref);
1038 * DOC: explicit fencing properties
1040 * Explicit fencing allows userspace to control the buffer synchronization
1041 * between devices. A Fence or a group of fences are transfered to/from
1042 * userspace using Sync File fds and there are two DRM properties for that.
1043 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1044 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1046 * As a contrast, with implicit fencing the kernel keeps track of any
1047 * ongoing rendering, and automatically ensures that the atomic update waits
1048 * for any pending rendering to complete. For shared buffers represented with
1049 * a &struct dma_buf this is tracked in &struct dma_resv.
1050 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1051 * whereas explicit fencing is what Android wants.
1054 * Use this property to pass a fence that DRM should wait on before
1055 * proceeding with the Atomic Commit request and show the framebuffer for
1056 * the plane on the screen. The fence can be either a normal fence or a
1057 * merged one, the sync_file framework will handle both cases and use a
1058 * fence_array if a merged fence is received. Passing -1 here means no
1059 * fences to wait on.
1061 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1062 * it will only check if the Sync File is a valid one.
1064 * On the driver side the fence is stored on the @fence parameter of
1065 * &struct drm_plane_state. Drivers which also support implicit fencing
1066 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1067 * to make sure there's consistent behaviour between drivers in precedence
1068 * of implicit vs. explicit fencing.
1071 * Use this property to pass a file descriptor pointer to DRM. Once the
1072 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1073 * the file descriptor number of a Sync File. This Sync File contains the
1074 * CRTC fence that will be signaled when all framebuffers present on the
1075 * Atomic Commit * request for that given CRTC are scanned out on the
1078 * The Atomic Commit request fails if a invalid pointer is passed. If the
1079 * Atomic Commit request fails for any other reason the out fence fd
1080 * returned will be -1. On a Atomic Commit with the
1081 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1083 * Note that out-fences don't have a special interface to drivers and are
1084 * internally represented by a &struct drm_pending_vblank_event in struct
1085 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1086 * helpers and for the DRM event handling for existing userspace.
1089 struct drm_out_fence_state {
1090 s32 __user *out_fence_ptr;
1091 struct sync_file *sync_file;
1095 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1096 struct dma_fence *fence)
1098 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1099 if (fence_state->fd < 0)
1100 return fence_state->fd;
1102 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1105 fence_state->sync_file = sync_file_create(fence);
1106 if (!fence_state->sync_file)
1112 static int prepare_signaling(struct drm_device *dev,
1113 struct drm_atomic_state *state,
1114 struct drm_mode_atomic *arg,
1115 struct drm_file *file_priv,
1116 struct drm_out_fence_state **fence_state,
1117 unsigned int *num_fences)
1119 struct drm_crtc *crtc;
1120 struct drm_crtc_state *crtc_state;
1121 struct drm_connector *conn;
1122 struct drm_connector_state *conn_state;
1125 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1128 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1129 s32 __user *fence_ptr;
1131 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1133 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1134 struct drm_pending_vblank_event *e;
1136 e = create_vblank_event(crtc, arg->user_data);
1140 crtc_state->event = e;
1143 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1144 struct drm_pending_vblank_event *e = crtc_state->event;
1149 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1153 crtc_state->event = NULL;
1159 struct dma_fence *fence;
1160 struct drm_out_fence_state *f;
1162 f = krealloc(*fence_state, sizeof(**fence_state) *
1163 (*num_fences + 1), GFP_KERNEL);
1167 memset(&f[*num_fences], 0, sizeof(*f));
1169 f[*num_fences].out_fence_ptr = fence_ptr;
1172 fence = drm_crtc_create_fence(crtc);
1176 ret = setup_out_fence(&f[(*num_fences)++], fence);
1178 dma_fence_put(fence);
1182 crtc_state->event->base.fence = fence;
1188 for_each_new_connector_in_state(state, conn, conn_state, i) {
1189 struct drm_writeback_connector *wb_conn;
1190 struct drm_out_fence_state *f;
1191 struct dma_fence *fence;
1192 s32 __user *fence_ptr;
1194 if (!conn_state->writeback_job)
1197 fence_ptr = get_out_fence_for_connector(state, conn);
1201 f = krealloc(*fence_state, sizeof(**fence_state) *
1202 (*num_fences + 1), GFP_KERNEL);
1206 memset(&f[*num_fences], 0, sizeof(*f));
1208 f[*num_fences].out_fence_ptr = fence_ptr;
1211 wb_conn = drm_connector_to_writeback(conn);
1212 fence = drm_writeback_get_out_fence(wb_conn);
1216 ret = setup_out_fence(&f[(*num_fences)++], fence);
1218 dma_fence_put(fence);
1222 conn_state->writeback_job->out_fence = fence;
1226 * Having this flag means user mode pends on event which will never
1227 * reach due to lack of at least one CRTC for signaling
1229 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1235 static void complete_signaling(struct drm_device *dev,
1236 struct drm_atomic_state *state,
1237 struct drm_out_fence_state *fence_state,
1238 unsigned int num_fences,
1241 struct drm_crtc *crtc;
1242 struct drm_crtc_state *crtc_state;
1246 for (i = 0; i < num_fences; i++)
1247 fd_install(fence_state[i].fd,
1248 fence_state[i].sync_file->file);
1254 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1255 struct drm_pending_vblank_event *event = crtc_state->event;
1257 * Free the allocated event. drm_atomic_helper_setup_commit
1258 * can allocate an event too, so only free it if it's ours
1259 * to prevent a double free in drm_atomic_state_clear.
1261 if (event && (event->base.fence || event->base.file_priv)) {
1262 drm_event_cancel_free(dev, &event->base);
1263 crtc_state->event = NULL;
1270 for (i = 0; i < num_fences; i++) {
1271 if (fence_state[i].sync_file)
1272 fput(fence_state[i].sync_file->file);
1273 if (fence_state[i].fd >= 0)
1274 put_unused_fd(fence_state[i].fd);
1276 /* If this fails log error to the user */
1277 if (fence_state[i].out_fence_ptr &&
1278 put_user(-1, fence_state[i].out_fence_ptr))
1279 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
1285 int drm_mode_atomic_ioctl(struct drm_device *dev,
1286 void *data, struct drm_file *file_priv)
1288 struct drm_mode_atomic *arg = data;
1289 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1290 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1291 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1292 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1293 unsigned int copied_objs, copied_props;
1294 struct drm_atomic_state *state;
1295 struct drm_modeset_acquire_ctx ctx;
1296 struct drm_out_fence_state *fence_state;
1298 unsigned int i, j, num_fences;
1300 /* disallow for drivers not supporting atomic: */
1301 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1304 /* disallow for userspace that has not enabled atomic cap (even
1305 * though this may be a bit overkill, since legacy userspace
1306 * wouldn't know how to call this ioctl)
1308 if (!file_priv->atomic)
1311 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1317 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1320 /* can't test and expect an event at the same time. */
1321 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1322 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1325 state = drm_atomic_state_alloc(dev);
1329 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1330 state->acquire_ctx = &ctx;
1331 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1339 for (i = 0; i < arg->count_objs; i++) {
1340 uint32_t obj_id, count_props;
1341 struct drm_mode_object *obj;
1343 if (get_user(obj_id, objs_ptr + copied_objs)) {
1348 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1354 if (!obj->properties) {
1355 drm_mode_object_put(obj);
1360 if (get_user(count_props, count_props_ptr + copied_objs)) {
1361 drm_mode_object_put(obj);
1368 for (j = 0; j < count_props; j++) {
1370 uint64_t prop_value;
1371 struct drm_property *prop;
1373 if (get_user(prop_id, props_ptr + copied_props)) {
1374 drm_mode_object_put(obj);
1379 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1381 drm_mode_object_put(obj);
1386 if (copy_from_user(&prop_value,
1387 prop_values_ptr + copied_props,
1388 sizeof(prop_value))) {
1389 drm_mode_object_put(obj);
1394 ret = drm_atomic_set_property(state, file_priv,
1395 obj, prop, prop_value);
1397 drm_mode_object_put(obj);
1404 drm_mode_object_put(obj);
1407 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1412 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1413 ret = drm_atomic_check_only(state);
1414 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1415 ret = drm_atomic_nonblocking_commit(state);
1417 if (drm_debug_enabled(DRM_UT_STATE))
1418 drm_atomic_print_state(state);
1420 ret = drm_atomic_commit(state);
1424 complete_signaling(dev, state, fence_state, num_fences, !ret);
1426 if (ret == -EDEADLK) {
1427 drm_atomic_state_clear(state);
1428 ret = drm_modeset_backoff(&ctx);
1433 drm_atomic_state_put(state);
1435 drm_modeset_drop_locks(&ctx);
1436 drm_modeset_acquire_fini(&ctx);