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.
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 drm_mode_convert_to_umode(&umode, mode);
80 drm_property_create_blob(state->crtc->dev,
83 if (IS_ERR(state->mode_blob))
84 return PTR_ERR(state->mode_blob);
86 drm_mode_copy(&state->mode, mode);
88 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
89 mode->name, crtc->base.id, crtc->name, state);
91 memset(&state->mode, 0, sizeof(state->mode));
92 state->enable = false;
93 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
94 crtc->base.id, crtc->name, state);
99 EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
102 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
103 * @state: the CRTC whose incoming state to update
104 * @blob: pointer to blob property to use for mode
106 * Set a mode (originating from a blob property) on the desired CRTC state.
107 * This function will take a reference on the blob property for the CRTC state,
108 * and release the reference held on the state's existing mode property, if any
112 * Zero on success, error code on failure. Cannot return -EDEADLK.
114 int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
115 struct drm_property_blob *blob)
117 struct drm_crtc *crtc = state->crtc;
119 if (blob == state->mode_blob)
122 drm_property_blob_put(state->mode_blob);
123 state->mode_blob = NULL;
125 memset(&state->mode, 0, sizeof(state->mode));
130 if (blob->length != sizeof(struct drm_mode_modeinfo)) {
131 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
132 crtc->base.id, crtc->name,
137 ret = drm_mode_convert_umode(crtc->dev,
138 &state->mode, blob->data);
140 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
141 crtc->base.id, crtc->name,
142 ret, drm_get_mode_status_name(state->mode.status));
143 drm_mode_debug_printmodeline(&state->mode);
147 state->mode_blob = drm_property_blob_get(blob);
148 state->enable = true;
149 DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
150 state->mode.name, crtc->base.id, crtc->name,
153 state->enable = false;
154 DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
155 crtc->base.id, crtc->name, state);
160 EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
163 * drm_atomic_set_crtc_for_plane - set CRTC for plane
164 * @plane_state: the plane whose incoming state to update
165 * @crtc: CRTC to use for the plane
167 * Changing the assigned CRTC for a plane requires us to grab the lock and state
168 * for the new CRTC, as needed. This function takes care of all these details
169 * besides updating the pointer in the state object itself.
172 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
173 * then the w/w mutex code has detected a deadlock and the entire atomic
174 * sequence must be restarted. All other errors are fatal.
177 drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
178 struct drm_crtc *crtc)
180 struct drm_plane *plane = plane_state->plane;
181 struct drm_crtc_state *crtc_state;
182 /* Nothing to do for same crtc*/
183 if (plane_state->crtc == crtc)
185 if (plane_state->crtc) {
186 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
188 if (WARN_ON(IS_ERR(crtc_state)))
189 return PTR_ERR(crtc_state);
191 crtc_state->plane_mask &= ~drm_plane_mask(plane);
194 plane_state->crtc = crtc;
197 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
199 if (IS_ERR(crtc_state))
200 return PTR_ERR(crtc_state);
201 crtc_state->plane_mask |= drm_plane_mask(plane);
205 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
206 plane->base.id, plane->name, plane_state,
207 crtc->base.id, crtc->name);
209 DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
210 plane->base.id, plane->name, plane_state);
214 EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
217 * drm_atomic_set_fb_for_plane - set framebuffer for plane
218 * @plane_state: atomic state object for the plane
219 * @fb: fb to use for the plane
221 * Changing the assigned framebuffer for a plane requires us to grab a reference
222 * to the new fb and drop the reference to the old fb, if there is one. This
223 * function takes care of all these details besides updating the pointer in the
224 * state object itself.
227 drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
228 struct drm_framebuffer *fb)
230 struct drm_plane *plane = plane_state->plane;
233 DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
234 fb->base.id, plane->base.id, plane->name,
237 DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
238 plane->base.id, plane->name, plane_state);
240 drm_framebuffer_assign(&plane_state->fb, fb);
242 EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
245 * drm_atomic_set_fence_for_plane - set fence for plane
246 * @plane_state: atomic state object for the plane
247 * @fence: dma_fence to use for the plane
249 * Helper to setup the plane_state fence in case it is not set yet.
250 * By using this drivers doesn't need to worry if the user choose
251 * implicit or explicit fencing.
253 * This function will not set the fence to the state if it was set
254 * via explicit fencing interfaces on the atomic ioctl. In that case it will
255 * drop the reference to the fence as we are not storing it anywhere.
256 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
257 * with the received implicit fence. In both cases this function consumes a
258 * reference for @fence.
260 * This way explicit fencing can be used to overrule implicit fencing, which is
261 * important to make explicit fencing use-cases work: One example is using one
262 * buffer for 2 screens with different refresh rates. Implicit fencing will
263 * clamp rendering to the refresh rate of the slower screen, whereas explicit
264 * fence allows 2 independent render and display loops on a single buffer. If a
265 * driver allows obeys both implicit and explicit fences for plane updates, then
266 * it will break all the benefits of explicit fencing.
269 drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
270 struct dma_fence *fence)
272 if (plane_state->fence) {
273 dma_fence_put(fence);
277 plane_state->fence = fence;
279 EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
282 * drm_atomic_set_crtc_for_connector - set CRTC for connector
283 * @conn_state: atomic state object for the connector
284 * @crtc: CRTC to use for the connector
286 * Changing the assigned CRTC for a connector requires us to grab the lock and
287 * state for the new CRTC, as needed. This function takes care of all these
288 * details besides updating the pointer in the state object itself.
291 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
292 * then the w/w mutex code has detected a deadlock and the entire atomic
293 * sequence must be restarted. All other errors are fatal.
296 drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
297 struct drm_crtc *crtc)
299 struct drm_connector *connector = conn_state->connector;
300 struct drm_crtc_state *crtc_state;
302 if (conn_state->crtc == crtc)
305 if (conn_state->crtc) {
306 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
309 crtc_state->connector_mask &=
310 ~drm_connector_mask(conn_state->connector);
312 drm_connector_put(conn_state->connector);
313 conn_state->crtc = NULL;
317 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
318 if (IS_ERR(crtc_state))
319 return PTR_ERR(crtc_state);
321 crtc_state->connector_mask |=
322 drm_connector_mask(conn_state->connector);
324 drm_connector_get(conn_state->connector);
325 conn_state->crtc = crtc;
327 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
328 connector->base.id, connector->name,
329 conn_state, crtc->base.id, crtc->name);
331 DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
332 connector->base.id, connector->name,
338 EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
340 static void set_out_fence_for_crtc(struct drm_atomic_state *state,
341 struct drm_crtc *crtc, s32 __user *fence_ptr)
343 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
346 static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
347 struct drm_crtc *crtc)
349 s32 __user *fence_ptr;
351 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
352 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
357 static int set_out_fence_for_connector(struct drm_atomic_state *state,
358 struct drm_connector *connector,
359 s32 __user *fence_ptr)
361 unsigned int index = drm_connector_index(connector);
366 if (put_user(-1, fence_ptr))
369 state->connectors[index].out_fence_ptr = fence_ptr;
374 static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
375 struct drm_connector *connector)
377 unsigned int index = drm_connector_index(connector);
378 s32 __user *fence_ptr;
380 fence_ptr = state->connectors[index].out_fence_ptr;
381 state->connectors[index].out_fence_ptr = NULL;
387 drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
388 struct drm_property_blob **blob,
390 ssize_t expected_size,
391 ssize_t expected_elem_size,
394 struct drm_property_blob *new_blob = NULL;
397 new_blob = drm_property_lookup_blob(dev, blob_id);
398 if (new_blob == NULL)
401 if (expected_size > 0 &&
402 new_blob->length != expected_size) {
403 drm_property_blob_put(new_blob);
406 if (expected_elem_size > 0 &&
407 new_blob->length % expected_elem_size != 0) {
408 drm_property_blob_put(new_blob);
413 *replaced |= drm_property_replace_blob(blob, new_blob);
414 drm_property_blob_put(new_blob);
419 static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
420 struct drm_crtc_state *state, struct drm_property *property,
423 struct drm_device *dev = crtc->dev;
424 struct drm_mode_config *config = &dev->mode_config;
425 bool replaced = false;
428 if (property == config->prop_active)
430 else if (property == config->prop_mode_id) {
431 struct drm_property_blob *mode =
432 drm_property_lookup_blob(dev, val);
433 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
434 drm_property_blob_put(mode);
436 } else if (property == config->prop_vrr_enabled) {
437 state->vrr_enabled = val;
438 } else if (property == config->degamma_lut_property) {
439 ret = drm_atomic_replace_property_blob_from_id(dev,
442 -1, sizeof(struct drm_color_lut),
444 state->color_mgmt_changed |= replaced;
446 } else if (property == config->ctm_property) {
447 ret = drm_atomic_replace_property_blob_from_id(dev,
450 sizeof(struct drm_color_ctm), -1,
452 state->color_mgmt_changed |= replaced;
454 } else if (property == config->gamma_lut_property) {
455 ret = drm_atomic_replace_property_blob_from_id(dev,
458 -1, sizeof(struct drm_color_lut),
460 state->color_mgmt_changed |= replaced;
462 } else if (property == config->prop_out_fence_ptr) {
463 s32 __user *fence_ptr = u64_to_user_ptr(val);
468 if (put_user(-1, fence_ptr))
471 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
472 } else if (property == crtc->scaling_filter_property) {
473 state->scaling_filter = val;
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 (property == crtc->scaling_filter_property)
509 *val = state->scaling_filter;
510 else if (crtc->funcs->atomic_get_property)
511 return crtc->funcs->atomic_get_property(crtc, state, property, val);
518 static int drm_atomic_plane_set_property(struct drm_plane *plane,
519 struct drm_plane_state *state, struct drm_file *file_priv,
520 struct drm_property *property, uint64_t val)
522 struct drm_device *dev = plane->dev;
523 struct drm_mode_config *config = &dev->mode_config;
524 bool replaced = false;
527 if (property == config->prop_fb_id) {
528 struct drm_framebuffer *fb;
530 fb = drm_framebuffer_lookup(dev, file_priv, val);
531 drm_atomic_set_fb_for_plane(state, fb);
533 drm_framebuffer_put(fb);
534 } else if (property == config->prop_in_fence_fd) {
538 if (U642I64(val) == -1)
541 state->fence = sync_file_get_fence(val);
545 } else if (property == config->prop_crtc_id) {
546 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
550 return drm_atomic_set_crtc_for_plane(state, crtc);
551 } else if (property == config->prop_crtc_x) {
552 state->crtc_x = U642I64(val);
553 } else if (property == config->prop_crtc_y) {
554 state->crtc_y = U642I64(val);
555 } else if (property == config->prop_crtc_w) {
557 } else if (property == config->prop_crtc_h) {
559 } else if (property == config->prop_src_x) {
561 } else if (property == config->prop_src_y) {
563 } else if (property == config->prop_src_w) {
565 } else if (property == config->prop_src_h) {
567 } else if (property == plane->alpha_property) {
569 } else if (property == plane->blend_mode_property) {
570 state->pixel_blend_mode = val;
571 } else if (property == plane->rotation_property) {
572 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
573 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
574 plane->base.id, plane->name, val);
577 state->rotation = val;
578 } else if (property == plane->zpos_property) {
580 } else if (property == plane->color_encoding_property) {
581 state->color_encoding = val;
582 } else if (property == plane->color_range_property) {
583 state->color_range = val;
584 } else if (property == config->prop_fb_damage_clips) {
585 ret = drm_atomic_replace_property_blob_from_id(dev,
586 &state->fb_damage_clips,
589 sizeof(struct drm_rect),
592 } else if (property == plane->scaling_filter_property) {
593 state->scaling_filter = val;
594 } else if (plane->funcs->atomic_set_property) {
595 return plane->funcs->atomic_set_property(plane, state,
598 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
599 plane->base.id, plane->name,
600 property->base.id, property->name);
608 drm_atomic_plane_get_property(struct drm_plane *plane,
609 const struct drm_plane_state *state,
610 struct drm_property *property, uint64_t *val)
612 struct drm_device *dev = plane->dev;
613 struct drm_mode_config *config = &dev->mode_config;
615 if (property == config->prop_fb_id) {
616 *val = (state->fb) ? state->fb->base.id : 0;
617 } else if (property == config->prop_in_fence_fd) {
619 } else if (property == config->prop_crtc_id) {
620 *val = (state->crtc) ? state->crtc->base.id : 0;
621 } else if (property == config->prop_crtc_x) {
622 *val = I642U64(state->crtc_x);
623 } else if (property == config->prop_crtc_y) {
624 *val = I642U64(state->crtc_y);
625 } else if (property == config->prop_crtc_w) {
626 *val = state->crtc_w;
627 } else if (property == config->prop_crtc_h) {
628 *val = state->crtc_h;
629 } else if (property == config->prop_src_x) {
631 } else if (property == config->prop_src_y) {
633 } else if (property == config->prop_src_w) {
635 } else if (property == config->prop_src_h) {
637 } else if (property == plane->alpha_property) {
639 } else if (property == plane->blend_mode_property) {
640 *val = state->pixel_blend_mode;
641 } else if (property == plane->rotation_property) {
642 *val = state->rotation;
643 } else if (property == plane->zpos_property) {
645 } else if (property == plane->color_encoding_property) {
646 *val = state->color_encoding;
647 } else if (property == plane->color_range_property) {
648 *val = state->color_range;
649 } else if (property == config->prop_fb_damage_clips) {
650 *val = (state->fb_damage_clips) ?
651 state->fb_damage_clips->base.id : 0;
652 } else if (property == plane->scaling_filter_property) {
653 *val = state->scaling_filter;
654 } else if (plane->funcs->atomic_get_property) {
655 return plane->funcs->atomic_get_property(plane, state, property, val);
663 static int drm_atomic_set_writeback_fb_for_connector(
664 struct drm_connector_state *conn_state,
665 struct drm_framebuffer *fb)
669 ret = drm_writeback_set_fb(conn_state, fb);
674 DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
675 fb->base.id, conn_state);
677 DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
683 static int drm_atomic_connector_set_property(struct drm_connector *connector,
684 struct drm_connector_state *state, struct drm_file *file_priv,
685 struct drm_property *property, uint64_t val)
687 struct drm_device *dev = connector->dev;
688 struct drm_mode_config *config = &dev->mode_config;
689 bool replaced = false;
692 if (property == config->prop_crtc_id) {
693 struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
697 return drm_atomic_set_crtc_for_connector(state, crtc);
698 } else if (property == config->dpms_property) {
699 /* setting DPMS property requires special handling, which
700 * is done in legacy setprop path for us. Disallow (for
701 * now?) atomic writes to DPMS property:
704 } else if (property == config->tv_select_subconnector_property) {
705 state->tv.subconnector = val;
706 } else if (property == config->tv_left_margin_property) {
707 state->tv.margins.left = val;
708 } else if (property == config->tv_right_margin_property) {
709 state->tv.margins.right = val;
710 } else if (property == config->tv_top_margin_property) {
711 state->tv.margins.top = val;
712 } else if (property == config->tv_bottom_margin_property) {
713 state->tv.margins.bottom = val;
714 } else if (property == config->tv_mode_property) {
715 state->tv.mode = val;
716 } else if (property == config->tv_brightness_property) {
717 state->tv.brightness = val;
718 } else if (property == config->tv_contrast_property) {
719 state->tv.contrast = val;
720 } else if (property == config->tv_flicker_reduction_property) {
721 state->tv.flicker_reduction = val;
722 } else if (property == config->tv_overscan_property) {
723 state->tv.overscan = val;
724 } else if (property == config->tv_saturation_property) {
725 state->tv.saturation = val;
726 } else if (property == config->tv_hue_property) {
728 } else if (property == config->link_status_property) {
729 /* Never downgrade from GOOD to BAD on userspace's request here,
730 * only hw issues can do that.
732 * For an atomic property the userspace doesn't need to be able
733 * to understand all the properties, but needs to be able to
734 * restore the state it wants on VT switch. So if the userspace
735 * tries to change the link_status from GOOD to BAD, driver
736 * silently rejects it and returns a 0. This prevents userspace
737 * from accidently breaking the display when it restores the
740 if (state->link_status != DRM_LINK_STATUS_GOOD)
741 state->link_status = val;
742 } else if (property == config->hdr_output_metadata_property) {
743 ret = drm_atomic_replace_property_blob_from_id(dev,
744 &state->hdr_output_metadata,
746 sizeof(struct hdr_output_metadata), -1,
749 } else if (property == config->aspect_ratio_property) {
750 state->picture_aspect_ratio = val;
751 } else if (property == config->content_type_property) {
752 state->content_type = val;
753 } else if (property == connector->scaling_mode_property) {
754 state->scaling_mode = val;
755 } else if (property == config->content_protection_property) {
756 if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
757 DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
760 state->content_protection = val;
761 } else if (property == config->hdcp_content_type_property) {
762 state->hdcp_content_type = val;
763 } else if (property == connector->colorspace_property) {
764 state->colorspace = val;
765 } else if (property == config->writeback_fb_id_property) {
766 struct drm_framebuffer *fb;
769 fb = drm_framebuffer_lookup(dev, file_priv, val);
770 ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
772 drm_framebuffer_put(fb);
774 } else if (property == config->writeback_out_fence_ptr_property) {
775 s32 __user *fence_ptr = u64_to_user_ptr(val);
777 return set_out_fence_for_connector(state->state, connector,
779 } else if (property == connector->max_bpc_property) {
780 state->max_requested_bpc = val;
781 } else if (connector->funcs->atomic_set_property) {
782 return connector->funcs->atomic_set_property(connector,
783 state, property, val);
785 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
786 connector->base.id, connector->name,
787 property->base.id, property->name);
795 drm_atomic_connector_get_property(struct drm_connector *connector,
796 const struct drm_connector_state *state,
797 struct drm_property *property, uint64_t *val)
799 struct drm_device *dev = connector->dev;
800 struct drm_mode_config *config = &dev->mode_config;
802 if (property == config->prop_crtc_id) {
803 *val = (state->crtc) ? state->crtc->base.id : 0;
804 } else if (property == config->dpms_property) {
805 if (state->crtc && state->crtc->state->self_refresh_active)
806 *val = DRM_MODE_DPMS_ON;
808 *val = connector->dpms;
809 } else if (property == config->tv_select_subconnector_property) {
810 *val = state->tv.subconnector;
811 } else if (property == config->tv_left_margin_property) {
812 *val = state->tv.margins.left;
813 } else if (property == config->tv_right_margin_property) {
814 *val = state->tv.margins.right;
815 } else if (property == config->tv_top_margin_property) {
816 *val = state->tv.margins.top;
817 } else if (property == config->tv_bottom_margin_property) {
818 *val = state->tv.margins.bottom;
819 } else if (property == config->tv_mode_property) {
820 *val = state->tv.mode;
821 } else if (property == config->tv_brightness_property) {
822 *val = state->tv.brightness;
823 } else if (property == config->tv_contrast_property) {
824 *val = state->tv.contrast;
825 } else if (property == config->tv_flicker_reduction_property) {
826 *val = state->tv.flicker_reduction;
827 } else if (property == config->tv_overscan_property) {
828 *val = state->tv.overscan;
829 } else if (property == config->tv_saturation_property) {
830 *val = state->tv.saturation;
831 } else if (property == config->tv_hue_property) {
832 *val = state->tv.hue;
833 } else if (property == config->link_status_property) {
834 *val = state->link_status;
835 } else if (property == config->aspect_ratio_property) {
836 *val = state->picture_aspect_ratio;
837 } else if (property == config->content_type_property) {
838 *val = state->content_type;
839 } else if (property == connector->colorspace_property) {
840 *val = state->colorspace;
841 } else if (property == connector->scaling_mode_property) {
842 *val = state->scaling_mode;
843 } else if (property == config->hdr_output_metadata_property) {
844 *val = state->hdr_output_metadata ?
845 state->hdr_output_metadata->base.id : 0;
846 } else if (property == config->content_protection_property) {
847 *val = state->content_protection;
848 } else if (property == config->hdcp_content_type_property) {
849 *val = state->hdcp_content_type;
850 } else if (property == config->writeback_fb_id_property) {
851 /* Writeback framebuffer is one-shot, write and forget */
853 } else if (property == config->writeback_out_fence_ptr_property) {
855 } else if (property == connector->max_bpc_property) {
856 *val = state->max_requested_bpc;
857 } else if (connector->funcs->atomic_get_property) {
858 return connector->funcs->atomic_get_property(connector,
859 state, property, val);
867 int drm_atomic_get_property(struct drm_mode_object *obj,
868 struct drm_property *property, uint64_t *val)
870 struct drm_device *dev = property->dev;
874 case DRM_MODE_OBJECT_CONNECTOR: {
875 struct drm_connector *connector = obj_to_connector(obj);
877 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
878 ret = drm_atomic_connector_get_property(connector,
879 connector->state, property, val);
882 case DRM_MODE_OBJECT_CRTC: {
883 struct drm_crtc *crtc = obj_to_crtc(obj);
885 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
886 ret = drm_atomic_crtc_get_property(crtc,
887 crtc->state, property, val);
890 case DRM_MODE_OBJECT_PLANE: {
891 struct drm_plane *plane = obj_to_plane(obj);
893 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
894 ret = drm_atomic_plane_get_property(plane,
895 plane->state, property, val);
907 * The big monster ioctl
910 static struct drm_pending_vblank_event *create_vblank_event(
911 struct drm_crtc *crtc, uint64_t user_data)
913 struct drm_pending_vblank_event *e = NULL;
915 e = kzalloc(sizeof *e, GFP_KERNEL);
919 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
920 e->event.base.length = sizeof(e->event);
921 e->event.vbl.crtc_id = crtc->base.id;
922 e->event.vbl.user_data = user_data;
927 int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
928 struct drm_connector *connector,
931 struct drm_connector *tmp_connector;
932 struct drm_connector_state *new_conn_state;
933 struct drm_crtc *crtc;
934 struct drm_crtc_state *crtc_state;
935 int i, ret, old_mode = connector->dpms;
938 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
943 if (mode != DRM_MODE_DPMS_ON)
944 mode = DRM_MODE_DPMS_OFF;
945 connector->dpms = mode;
947 crtc = connector->state->crtc;
950 ret = drm_atomic_add_affected_connectors(state, crtc);
954 crtc_state = drm_atomic_get_crtc_state(state, crtc);
955 if (IS_ERR(crtc_state)) {
956 ret = PTR_ERR(crtc_state);
960 for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
961 if (new_conn_state->crtc != crtc)
963 if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
969 crtc_state->active = active;
970 ret = drm_atomic_commit(state);
973 connector->dpms = old_mode;
977 int drm_atomic_set_property(struct drm_atomic_state *state,
978 struct drm_file *file_priv,
979 struct drm_mode_object *obj,
980 struct drm_property *prop,
983 struct drm_mode_object *ref;
986 if (!drm_property_change_valid_get(prop, prop_value, &ref))
990 case DRM_MODE_OBJECT_CONNECTOR: {
991 struct drm_connector *connector = obj_to_connector(obj);
992 struct drm_connector_state *connector_state;
994 connector_state = drm_atomic_get_connector_state(state, connector);
995 if (IS_ERR(connector_state)) {
996 ret = PTR_ERR(connector_state);
1000 ret = drm_atomic_connector_set_property(connector,
1001 connector_state, file_priv,
1005 case DRM_MODE_OBJECT_CRTC: {
1006 struct drm_crtc *crtc = obj_to_crtc(obj);
1007 struct drm_crtc_state *crtc_state;
1009 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1010 if (IS_ERR(crtc_state)) {
1011 ret = PTR_ERR(crtc_state);
1015 ret = drm_atomic_crtc_set_property(crtc,
1016 crtc_state, prop, prop_value);
1019 case DRM_MODE_OBJECT_PLANE: {
1020 struct drm_plane *plane = obj_to_plane(obj);
1021 struct drm_plane_state *plane_state;
1023 plane_state = drm_atomic_get_plane_state(state, plane);
1024 if (IS_ERR(plane_state)) {
1025 ret = PTR_ERR(plane_state);
1029 ret = drm_atomic_plane_set_property(plane,
1030 plane_state, file_priv,
1039 drm_property_change_valid_put(prop, ref);
1044 * DOC: explicit fencing properties
1046 * Explicit fencing allows userspace to control the buffer synchronization
1047 * between devices. A Fence or a group of fences are transfered to/from
1048 * userspace using Sync File fds and there are two DRM properties for that.
1049 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1050 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1052 * As a contrast, with implicit fencing the kernel keeps track of any
1053 * ongoing rendering, and automatically ensures that the atomic update waits
1054 * for any pending rendering to complete. For shared buffers represented with
1055 * a &struct dma_buf this is tracked in &struct dma_resv.
1056 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1057 * whereas explicit fencing is what Android wants.
1060 * Use this property to pass a fence that DRM should wait on before
1061 * proceeding with the Atomic Commit request and show the framebuffer for
1062 * the plane on the screen. The fence can be either a normal fence or a
1063 * merged one, the sync_file framework will handle both cases and use a
1064 * fence_array if a merged fence is received. Passing -1 here means no
1065 * fences to wait on.
1067 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1068 * it will only check if the Sync File is a valid one.
1070 * On the driver side the fence is stored on the @fence parameter of
1071 * &struct drm_plane_state. Drivers which also support implicit fencing
1072 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1073 * to make sure there's consistent behaviour between drivers in precedence
1074 * of implicit vs. explicit fencing.
1077 * Use this property to pass a file descriptor pointer to DRM. Once the
1078 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1079 * the file descriptor number of a Sync File. This Sync File contains the
1080 * CRTC fence that will be signaled when all framebuffers present on the
1081 * Atomic Commit * request for that given CRTC are scanned out on the
1084 * The Atomic Commit request fails if a invalid pointer is passed. If the
1085 * Atomic Commit request fails for any other reason the out fence fd
1086 * returned will be -1. On a Atomic Commit with the
1087 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1089 * Note that out-fences don't have a special interface to drivers and are
1090 * internally represented by a &struct drm_pending_vblank_event in struct
1091 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1092 * helpers and for the DRM event handling for existing userspace.
1095 struct drm_out_fence_state {
1096 s32 __user *out_fence_ptr;
1097 struct sync_file *sync_file;
1101 static int setup_out_fence(struct drm_out_fence_state *fence_state,
1102 struct dma_fence *fence)
1104 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1105 if (fence_state->fd < 0)
1106 return fence_state->fd;
1108 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1111 fence_state->sync_file = sync_file_create(fence);
1112 if (!fence_state->sync_file)
1118 static int prepare_signaling(struct drm_device *dev,
1119 struct drm_atomic_state *state,
1120 struct drm_mode_atomic *arg,
1121 struct drm_file *file_priv,
1122 struct drm_out_fence_state **fence_state,
1123 unsigned int *num_fences)
1125 struct drm_crtc *crtc;
1126 struct drm_crtc_state *crtc_state;
1127 struct drm_connector *conn;
1128 struct drm_connector_state *conn_state;
1131 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1134 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1135 s32 __user *fence_ptr;
1137 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1139 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1140 struct drm_pending_vblank_event *e;
1142 e = create_vblank_event(crtc, arg->user_data);
1146 crtc_state->event = e;
1149 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1150 struct drm_pending_vblank_event *e = crtc_state->event;
1155 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1159 crtc_state->event = NULL;
1165 struct dma_fence *fence;
1166 struct drm_out_fence_state *f;
1168 f = krealloc(*fence_state, sizeof(**fence_state) *
1169 (*num_fences + 1), GFP_KERNEL);
1173 memset(&f[*num_fences], 0, sizeof(*f));
1175 f[*num_fences].out_fence_ptr = fence_ptr;
1178 fence = drm_crtc_create_fence(crtc);
1182 ret = setup_out_fence(&f[(*num_fences)++], fence);
1184 dma_fence_put(fence);
1188 crtc_state->event->base.fence = fence;
1194 for_each_new_connector_in_state(state, conn, conn_state, i) {
1195 struct drm_writeback_connector *wb_conn;
1196 struct drm_out_fence_state *f;
1197 struct dma_fence *fence;
1198 s32 __user *fence_ptr;
1200 if (!conn_state->writeback_job)
1203 fence_ptr = get_out_fence_for_connector(state, conn);
1207 f = krealloc(*fence_state, sizeof(**fence_state) *
1208 (*num_fences + 1), GFP_KERNEL);
1212 memset(&f[*num_fences], 0, sizeof(*f));
1214 f[*num_fences].out_fence_ptr = fence_ptr;
1217 wb_conn = drm_connector_to_writeback(conn);
1218 fence = drm_writeback_get_out_fence(wb_conn);
1222 ret = setup_out_fence(&f[(*num_fences)++], fence);
1224 dma_fence_put(fence);
1228 conn_state->writeback_job->out_fence = fence;
1232 * Having this flag means user mode pends on event which will never
1233 * reach due to lack of at least one CRTC for signaling
1235 if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1241 static void complete_signaling(struct drm_device *dev,
1242 struct drm_atomic_state *state,
1243 struct drm_out_fence_state *fence_state,
1244 unsigned int num_fences,
1247 struct drm_crtc *crtc;
1248 struct drm_crtc_state *crtc_state;
1252 for (i = 0; i < num_fences; i++)
1253 fd_install(fence_state[i].fd,
1254 fence_state[i].sync_file->file);
1260 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1261 struct drm_pending_vblank_event *event = crtc_state->event;
1263 * Free the allocated event. drm_atomic_helper_setup_commit
1264 * can allocate an event too, so only free it if it's ours
1265 * to prevent a double free in drm_atomic_state_clear.
1267 if (event && (event->base.fence || event->base.file_priv)) {
1268 drm_event_cancel_free(dev, &event->base);
1269 crtc_state->event = NULL;
1276 for (i = 0; i < num_fences; i++) {
1277 if (fence_state[i].sync_file)
1278 fput(fence_state[i].sync_file->file);
1279 if (fence_state[i].fd >= 0)
1280 put_unused_fd(fence_state[i].fd);
1282 /* If this fails log error to the user */
1283 if (fence_state[i].out_fence_ptr &&
1284 put_user(-1, fence_state[i].out_fence_ptr))
1285 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
1291 int drm_mode_atomic_ioctl(struct drm_device *dev,
1292 void *data, struct drm_file *file_priv)
1294 struct drm_mode_atomic *arg = data;
1295 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1296 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1297 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1298 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1299 unsigned int copied_objs, copied_props;
1300 struct drm_atomic_state *state;
1301 struct drm_modeset_acquire_ctx ctx;
1302 struct drm_out_fence_state *fence_state;
1304 unsigned int i, j, num_fences;
1306 /* disallow for drivers not supporting atomic: */
1307 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1310 /* disallow for userspace that has not enabled atomic cap (even
1311 * though this may be a bit overkill, since legacy userspace
1312 * wouldn't know how to call this ioctl)
1314 if (!file_priv->atomic)
1317 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1323 if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1326 /* can't test and expect an event at the same time. */
1327 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1328 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1331 state = drm_atomic_state_alloc(dev);
1335 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1336 state->acquire_ctx = &ctx;
1337 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1345 for (i = 0; i < arg->count_objs; i++) {
1346 uint32_t obj_id, count_props;
1347 struct drm_mode_object *obj;
1349 if (get_user(obj_id, objs_ptr + copied_objs)) {
1354 obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1360 if (!obj->properties) {
1361 drm_mode_object_put(obj);
1366 if (get_user(count_props, count_props_ptr + copied_objs)) {
1367 drm_mode_object_put(obj);
1374 for (j = 0; j < count_props; j++) {
1376 uint64_t prop_value;
1377 struct drm_property *prop;
1379 if (get_user(prop_id, props_ptr + copied_props)) {
1380 drm_mode_object_put(obj);
1385 prop = drm_mode_obj_find_prop_id(obj, prop_id);
1387 drm_mode_object_put(obj);
1392 if (copy_from_user(&prop_value,
1393 prop_values_ptr + copied_props,
1394 sizeof(prop_value))) {
1395 drm_mode_object_put(obj);
1400 ret = drm_atomic_set_property(state, file_priv,
1401 obj, prop, prop_value);
1403 drm_mode_object_put(obj);
1410 drm_mode_object_put(obj);
1413 ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1418 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1419 ret = drm_atomic_check_only(state);
1420 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1421 ret = drm_atomic_nonblocking_commit(state);
1423 if (drm_debug_enabled(DRM_UT_STATE))
1424 drm_atomic_print_state(state);
1426 ret = drm_atomic_commit(state);
1430 complete_signaling(dev, state, fence_state, num_fences, !ret);
1432 if (ret == -EDEADLK) {
1433 drm_atomic_state_clear(state);
1434 ret = drm_modeset_backoff(&ctx);
1439 drm_atomic_state_put(state);
1441 drm_modeset_drop_locks(&ctx);
1442 drm_modeset_acquire_fini(&ctx);