]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/drm_atomic.c
Merge origin/master into drm-misc-fixes
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / drm_atomic.c
CommitLineData
cc4ceb48
DV
1/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28
29#include <drm/drmP.h>
30#include <drm/drm_atomic.h>
5488dc16 31#include <drm/drm_mode.h>
cc4ceb48 32#include <drm/drm_plane_helper.h>
fceffb32 33#include <drm/drm_print.h>
96260142 34#include <linux/sync_file.h>
cc4ceb48 35
be35f94f
TR
36#include "drm_crtc_internal.h"
37
b3ba3f6f 38void __drm_crtc_commit_free(struct kref *kref)
3b24f7d6
DV
39{
40 struct drm_crtc_commit *commit =
41 container_of(kref, struct drm_crtc_commit, ref);
42
43 kfree(commit);
44}
b3ba3f6f 45EXPORT_SYMBOL(__drm_crtc_commit_free);
3b24f7d6 46
036ef573
ML
47/**
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
51 *
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
54 */
55void drm_atomic_state_default_release(struct drm_atomic_state *state)
cc4ceb48
DV
56{
57 kfree(state->connectors);
cc4ceb48 58 kfree(state->crtcs);
cc4ceb48 59 kfree(state->planes);
b430c27a 60 kfree(state->private_objs);
cc4ceb48 61}
036ef573 62EXPORT_SYMBOL(drm_atomic_state_default_release);
cc4ceb48
DV
63
64/**
036ef573 65 * drm_atomic_state_init - init new atomic state
cc4ceb48 66 * @dev: DRM device
036ef573 67 * @state: atomic state
cc4ceb48 68 *
036ef573
ML
69 * Default implementation for filling in a new atomic state.
70 * This is useful for drivers that subclass the atomic state.
cc4ceb48 71 */
036ef573
ML
72int
73drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
cc4ceb48 74{
0853695c
CW
75 kref_init(&state->ref);
76
d34f20d6
RC
77 /* TODO legacy paths should maybe do a better job about
78 * setting this appropriately?
79 */
80 state->allow_modeset = true;
81
cc4ceb48
DV
82 state->crtcs = kcalloc(dev->mode_config.num_crtc,
83 sizeof(*state->crtcs), GFP_KERNEL);
84 if (!state->crtcs)
85 goto fail;
cc4ceb48
DV
86 state->planes = kcalloc(dev->mode_config.num_total_plane,
87 sizeof(*state->planes), GFP_KERNEL);
88 if (!state->planes)
89 goto fail;
cc4ceb48
DV
90
91 state->dev = dev;
92
036ef573 93 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
cc4ceb48 94
036ef573 95 return 0;
cc4ceb48 96fail:
036ef573
ML
97 drm_atomic_state_default_release(state);
98 return -ENOMEM;
99}
100EXPORT_SYMBOL(drm_atomic_state_init);
101
102/**
103 * drm_atomic_state_alloc - allocate atomic state
104 * @dev: DRM device
105 *
106 * This allocates an empty atomic state to track updates.
107 */
108struct drm_atomic_state *
109drm_atomic_state_alloc(struct drm_device *dev)
110{
111 struct drm_mode_config *config = &dev->mode_config;
036ef573
ML
112
113 if (!config->funcs->atomic_state_alloc) {
ac7c7483
DK
114 struct drm_atomic_state *state;
115
036ef573
ML
116 state = kzalloc(sizeof(*state), GFP_KERNEL);
117 if (!state)
118 return NULL;
119 if (drm_atomic_state_init(dev, state) < 0) {
120 kfree(state);
121 return NULL;
122 }
123 return state;
124 }
cc4ceb48 125
036ef573 126 return config->funcs->atomic_state_alloc(dev);
cc4ceb48
DV
127}
128EXPORT_SYMBOL(drm_atomic_state_alloc);
129
130/**
036ef573 131 * drm_atomic_state_default_clear - clear base atomic state
cc4ceb48
DV
132 * @state: atomic state
133 *
036ef573
ML
134 * Default implementation for clearing atomic state.
135 * This is useful for drivers that subclass the atomic state.
cc4ceb48 136 */
036ef573 137void drm_atomic_state_default_clear(struct drm_atomic_state *state)
cc4ceb48
DV
138{
139 struct drm_device *dev = state->dev;
6f75cea6 140 struct drm_mode_config *config = &dev->mode_config;
cc4ceb48
DV
141 int i;
142
17a38d9c 143 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
cc4ceb48 144
f52b69f1 145 for (i = 0; i < state->num_connector; i++) {
63e83c1d 146 struct drm_connector *connector = state->connectors[i].ptr;
cc4ceb48
DV
147
148 if (!connector)
149 continue;
150
d2307dea 151 connector->funcs->atomic_destroy_state(connector,
63e83c1d
DV
152 state->connectors[i].state);
153 state->connectors[i].ptr = NULL;
154 state->connectors[i].state = NULL;
ad093607 155 drm_connector_put(connector);
cc4ceb48
DV
156 }
157
6f75cea6 158 for (i = 0; i < config->num_crtc; i++) {
5d943aa6 159 struct drm_crtc *crtc = state->crtcs[i].ptr;
cc4ceb48
DV
160
161 if (!crtc)
162 continue;
163
164 crtc->funcs->atomic_destroy_state(crtc,
5d943aa6 165 state->crtcs[i].state);
3b24f7d6
DV
166
167 if (state->crtcs[i].commit) {
168 kfree(state->crtcs[i].commit->event);
169 state->crtcs[i].commit->event = NULL;
170 drm_crtc_commit_put(state->crtcs[i].commit);
171 }
172
173 state->crtcs[i].commit = NULL;
5d943aa6
DV
174 state->crtcs[i].ptr = NULL;
175 state->crtcs[i].state = NULL;
cc4ceb48
DV
176 }
177
6f75cea6 178 for (i = 0; i < config->num_total_plane; i++) {
b8b5342b 179 struct drm_plane *plane = state->planes[i].ptr;
cc4ceb48
DV
180
181 if (!plane)
182 continue;
183
184 plane->funcs->atomic_destroy_state(plane,
b8b5342b
DV
185 state->planes[i].state);
186 state->planes[i].ptr = NULL;
187 state->planes[i].state = NULL;
cc4ceb48 188 }
b430c27a
PD
189
190 for (i = 0; i < state->num_private_objs; i++) {
191 void *obj_state = state->private_objs[i].obj_state;
192
193 state->private_objs[i].funcs->destroy_state(obj_state);
194 state->private_objs[i].obj = NULL;
195 state->private_objs[i].obj_state = NULL;
196 state->private_objs[i].funcs = NULL;
197 }
198 state->num_private_objs = 0;
199
cc4ceb48 200}
036ef573
ML
201EXPORT_SYMBOL(drm_atomic_state_default_clear);
202
203/**
204 * drm_atomic_state_clear - clear state object
205 * @state: atomic state
206 *
207 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
208 * all locks. So someone else could sneak in and change the current modeset
209 * configuration. Which means that all the state assembled in @state is no
210 * longer an atomic update to the current state, but to some arbitrary earlier
d574528a
DV
211 * state. Which could break assumptions the driver's
212 * &drm_mode_config_funcs.atomic_check likely relies on.
036ef573
ML
213 *
214 * Hence we must clear all cached state and completely start over, using this
215 * function.
216 */
217void drm_atomic_state_clear(struct drm_atomic_state *state)
218{
219 struct drm_device *dev = state->dev;
220 struct drm_mode_config *config = &dev->mode_config;
221
222 if (config->funcs->atomic_state_clear)
223 config->funcs->atomic_state_clear(state);
224 else
225 drm_atomic_state_default_clear(state);
226}
cc4ceb48
DV
227EXPORT_SYMBOL(drm_atomic_state_clear);
228
229/**
0853695c
CW
230 * __drm_atomic_state_free - free all memory for an atomic state
231 * @ref: This atomic state to deallocate
cc4ceb48
DV
232 *
233 * This frees all memory associated with an atomic state, including all the
234 * per-object state for planes, crtcs and connectors.
235 */
0853695c 236void __drm_atomic_state_free(struct kref *ref)
cc4ceb48 237{
0853695c
CW
238 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
239 struct drm_mode_config *config = &state->dev->mode_config;
036ef573 240
cc4ceb48
DV
241 drm_atomic_state_clear(state);
242
17a38d9c 243 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
cc4ceb48 244
036ef573
ML
245 if (config->funcs->atomic_state_free) {
246 config->funcs->atomic_state_free(state);
247 } else {
248 drm_atomic_state_default_release(state);
249 kfree(state);
250 }
cc4ceb48 251}
0853695c 252EXPORT_SYMBOL(__drm_atomic_state_free);
cc4ceb48
DV
253
254/**
255 * drm_atomic_get_crtc_state - get crtc state
256 * @state: global atomic state object
257 * @crtc: crtc to get state object for
258 *
259 * This function returns the crtc state for the given crtc, allocating it if
260 * needed. It will also grab the relevant crtc lock to make sure that the state
261 * is consistent.
262 *
263 * Returns:
264 *
265 * Either the allocated state or the error code encoded into the pointer. When
266 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
267 * entire atomic sequence must be restarted. All other errors are fatal.
268 */
269struct drm_crtc_state *
270drm_atomic_get_crtc_state(struct drm_atomic_state *state,
271 struct drm_crtc *crtc)
272{
1b26a5e1 273 int ret, index = drm_crtc_index(crtc);
cc4ceb48
DV
274 struct drm_crtc_state *crtc_state;
275
7f4eaa89
ML
276 WARN_ON(!state->acquire_ctx);
277
1b26a5e1
ML
278 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
279 if (crtc_state)
280 return crtc_state;
cc4ceb48
DV
281
282 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
283 if (ret)
284 return ERR_PTR(ret);
285
286 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
287 if (!crtc_state)
288 return ERR_PTR(-ENOMEM);
289
5d943aa6 290 state->crtcs[index].state = crtc_state;
581e49fe
ML
291 state->crtcs[index].old_state = crtc->state;
292 state->crtcs[index].new_state = crtc_state;
5d943aa6 293 state->crtcs[index].ptr = crtc;
cc4ceb48
DV
294 crtc_state->state = state;
295
fa3ab4c2
VS
296 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
297 crtc->base.id, crtc->name, crtc_state, state);
cc4ceb48
DV
298
299 return crtc_state;
300}
301EXPORT_SYMBOL(drm_atomic_get_crtc_state);
302
beaf5af4 303static void set_out_fence_for_crtc(struct drm_atomic_state *state,
7e9081c5 304 struct drm_crtc *crtc, s32 __user *fence_ptr)
beaf5af4
GP
305{
306 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
307}
308
7e9081c5 309static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
beaf5af4
GP
310 struct drm_crtc *crtc)
311{
7e9081c5 312 s32 __user *fence_ptr;
beaf5af4
GP
313
314 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
315 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
316
317 return fence_ptr;
318}
319
819364da
DS
320/**
321 * drm_atomic_set_mode_for_crtc - set mode for CRTC
322 * @state: the CRTC whose incoming state to update
323 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
324 *
cbef9099
DP
325 * Set a mode (originating from the kernel) on the desired CRTC state and update
326 * the enable property.
819364da
DS
327 *
328 * RETURNS:
329 * Zero on success, error code on failure. Cannot return -EDEADLK.
330 */
331int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
91110a4b 332 const struct drm_display_mode *mode)
819364da 333{
99cf4a29
DS
334 struct drm_mode_modeinfo umode;
335
819364da
DS
336 /* Early return for no change. */
337 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
338 return 0;
339
6472e509 340 drm_property_blob_put(state->mode_blob);
99cf4a29
DS
341 state->mode_blob = NULL;
342
819364da 343 if (mode) {
99cf4a29
DS
344 drm_mode_convert_to_umode(&umode, mode);
345 state->mode_blob =
346 drm_property_create_blob(state->crtc->dev,
347 sizeof(umode),
348 &umode);
349 if (IS_ERR(state->mode_blob))
350 return PTR_ERR(state->mode_blob);
351
819364da
DS
352 drm_mode_copy(&state->mode, mode);
353 state->enable = true;
354 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
355 mode->name, state);
356 } else {
357 memset(&state->mode, 0, sizeof(state->mode));
358 state->enable = false;
359 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
360 state);
361 }
362
363 return 0;
364}
365EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
366
955f3c33
DS
367/**
368 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
369 * @state: the CRTC whose incoming state to update
370 * @blob: pointer to blob property to use for mode
371 *
372 * Set a mode (originating from a blob property) on the desired CRTC state.
373 * This function will take a reference on the blob property for the CRTC state,
374 * and release the reference held on the state's existing mode property, if any
375 * was set.
376 *
377 * RETURNS:
378 * Zero on success, error code on failure. Cannot return -EDEADLK.
379 */
380int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
381 struct drm_property_blob *blob)
382{
383 if (blob == state->mode_blob)
384 return 0;
385
6472e509 386 drm_property_blob_put(state->mode_blob);
955f3c33
DS
387 state->mode_blob = NULL;
388
6709887c
TV
389 memset(&state->mode, 0, sizeof(state->mode));
390
955f3c33
DS
391 if (blob) {
392 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
393 drm_mode_convert_umode(&state->mode,
394 (const struct drm_mode_modeinfo *)
395 blob->data))
396 return -EINVAL;
397
6472e509 398 state->mode_blob = drm_property_blob_get(blob);
955f3c33
DS
399 state->enable = true;
400 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
401 state->mode.name, state);
402 } else {
955f3c33
DS
403 state->enable = false;
404 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
405 state);
406 }
407
408 return 0;
409}
410EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
411
5488dc16
LL
412/**
413 * drm_atomic_replace_property_blob - replace a blob property
414 * @blob: a pointer to the member blob to be replaced
415 * @new_blob: the new blob to replace with
5488dc16
LL
416 * @replaced: whether the blob has been replaced
417 *
418 * RETURNS:
419 * Zero on success, error code on failure
420 */
421static void
422drm_atomic_replace_property_blob(struct drm_property_blob **blob,
423 struct drm_property_blob *new_blob,
424 bool *replaced)
425{
426 struct drm_property_blob *old_blob = *blob;
427
428 if (old_blob == new_blob)
429 return;
430
6472e509 431 drm_property_blob_put(old_blob);
5488dc16 432 if (new_blob)
6472e509 433 drm_property_blob_get(new_blob);
5488dc16
LL
434 *blob = new_blob;
435 *replaced = true;
436
437 return;
438}
439
440static int
dafee60d 441drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
5488dc16
LL
442 struct drm_property_blob **blob,
443 uint64_t blob_id,
444 ssize_t expected_size,
445 bool *replaced)
446{
5488dc16
LL
447 struct drm_property_blob *new_blob = NULL;
448
449 if (blob_id != 0) {
dafee60d 450 new_blob = drm_property_lookup_blob(dev, blob_id);
5488dc16
LL
451 if (new_blob == NULL)
452 return -EINVAL;
cac5fced
FM
453
454 if (expected_size > 0 && expected_size != new_blob->length) {
6472e509 455 drm_property_blob_put(new_blob);
5488dc16 456 return -EINVAL;
cac5fced 457 }
5488dc16
LL
458 }
459
460 drm_atomic_replace_property_blob(blob, new_blob, replaced);
6472e509 461 drm_property_blob_put(new_blob);
5488dc16
LL
462
463 return 0;
464}
465
40ecc694
RC
466/**
467 * drm_atomic_crtc_set_property - set property on CRTC
468 * @crtc: the drm CRTC to set a property on
469 * @state: the state object to update with the new property value
470 * @property: the property to set
471 * @val: the new property value
472 *
d574528a
DV
473 * This function handles generic/core properties and calls out to driver's
474 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
475 * consistent behavior you must call this function rather than the driver hook
476 * directly.
40ecc694
RC
477 *
478 * RETURNS:
479 * Zero on success, error code on failure
480 */
481int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
482 struct drm_crtc_state *state, struct drm_property *property,
483 uint64_t val)
484{
eab3bbef
DV
485 struct drm_device *dev = crtc->dev;
486 struct drm_mode_config *config = &dev->mode_config;
5488dc16 487 bool replaced = false;
955f3c33 488 int ret;
eab3bbef 489
27798365 490 if (property == config->prop_active)
eab3bbef 491 state->active = val;
955f3c33
DS
492 else if (property == config->prop_mode_id) {
493 struct drm_property_blob *mode =
494 drm_property_lookup_blob(dev, val);
495 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
6472e509 496 drm_property_blob_put(mode);
955f3c33 497 return ret;
5488dc16 498 } else if (property == config->degamma_lut_property) {
dafee60d 499 ret = drm_atomic_replace_property_blob_from_id(dev,
5488dc16
LL
500 &state->degamma_lut,
501 val,
502 -1,
503 &replaced);
add1fa75 504 state->color_mgmt_changed |= replaced;
5488dc16
LL
505 return ret;
506 } else if (property == config->ctm_property) {
dafee60d 507 ret = drm_atomic_replace_property_blob_from_id(dev,
5488dc16
LL
508 &state->ctm,
509 val,
510 sizeof(struct drm_color_ctm),
511 &replaced);
add1fa75 512 state->color_mgmt_changed |= replaced;
5488dc16
LL
513 return ret;
514 } else if (property == config->gamma_lut_property) {
dafee60d 515 ret = drm_atomic_replace_property_blob_from_id(dev,
5488dc16
LL
516 &state->gamma_lut,
517 val,
518 -1,
519 &replaced);
add1fa75 520 state->color_mgmt_changed |= replaced;
5488dc16 521 return ret;
beaf5af4 522 } else if (property == config->prop_out_fence_ptr) {
7e9081c5 523 s32 __user *fence_ptr = u64_to_user_ptr(val);
beaf5af4
GP
524
525 if (!fence_ptr)
526 return 0;
527
528 if (put_user(-1, fence_ptr))
529 return -EFAULT;
530
531 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
5488dc16 532 } else if (crtc->funcs->atomic_set_property)
40ecc694 533 return crtc->funcs->atomic_set_property(crtc, state, property, val);
27798365
DS
534 else
535 return -EINVAL;
536
537 return 0;
40ecc694
RC
538}
539EXPORT_SYMBOL(drm_atomic_crtc_set_property);
540
c0714fc9
DV
541/**
542 * drm_atomic_crtc_get_property - get property value from CRTC state
543 * @crtc: the drm CRTC to set a property on
544 * @state: the state object to get the property value from
545 * @property: the property to set
546 * @val: return location for the property value
547 *
d574528a
DV
548 * This function handles generic/core properties and calls out to driver's
549 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
550 * consistent behavior you must call this function rather than the driver hook
551 * directly.
c0714fc9
DV
552 *
553 * RETURNS:
554 * Zero on success, error code on failure
ac9c9256 555 */
bf22f3be
GT
556static int
557drm_atomic_crtc_get_property(struct drm_crtc *crtc,
ac9c9256
RC
558 const struct drm_crtc_state *state,
559 struct drm_property *property, uint64_t *val)
560{
8f164ce4
DS
561 struct drm_device *dev = crtc->dev;
562 struct drm_mode_config *config = &dev->mode_config;
563
564 if (property == config->prop_active)
565 *val = state->active;
955f3c33
DS
566 else if (property == config->prop_mode_id)
567 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
5488dc16
LL
568 else if (property == config->degamma_lut_property)
569 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
570 else if (property == config->ctm_property)
571 *val = (state->ctm) ? state->ctm->base.id : 0;
572 else if (property == config->gamma_lut_property)
573 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
beaf5af4
GP
574 else if (property == config->prop_out_fence_ptr)
575 *val = 0;
8f164ce4 576 else if (crtc->funcs->atomic_get_property)
ac9c9256 577 return crtc->funcs->atomic_get_property(crtc, state, property, val);
8f164ce4
DS
578 else
579 return -EINVAL;
580
581 return 0;
ac9c9256 582}
ac9c9256 583
5e743737
RC
584/**
585 * drm_atomic_crtc_check - check crtc state
586 * @crtc: crtc to check
587 * @state: crtc state to check
588 *
589 * Provides core sanity checks for crtc state.
590 *
591 * RETURNS:
592 * Zero on success, error code on failure
593 */
594static int drm_atomic_crtc_check(struct drm_crtc *crtc,
595 struct drm_crtc_state *state)
596{
597 /* NOTE: we explicitly don't enforce constraints such as primary
598 * layer covering entire screen, since that is something we want
599 * to allow (on hw that supports it). For hw that does not, it
600 * should be checked in driver's crtc->atomic_check() vfunc.
601 *
602 * TODO: Add generic modeset state checks once we support those.
603 */
eab3bbef
DV
604
605 if (state->active && !state->enable) {
fa3ab4c2
VS
606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
607 crtc->base.id, crtc->name);
eab3bbef
DV
608 return -EINVAL;
609 }
610
99cf4a29
DS
611 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
612 * as this is a kernel-internal detail that userspace should never
613 * be able to trigger. */
614 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
615 WARN_ON(state->enable && !state->mode_blob)) {
fa3ab4c2
VS
616 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
617 crtc->base.id, crtc->name);
99cf4a29
DS
618 return -EINVAL;
619 }
620
621 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
622 WARN_ON(!state->enable && state->mode_blob)) {
fa3ab4c2
VS
623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
624 crtc->base.id, crtc->name);
99cf4a29
DS
625 return -EINVAL;
626 }
627
4cba6850
DV
628 /*
629 * Reject event generation for when a CRTC is off and stays off.
630 * It wouldn't be hard to implement this, but userspace has a track
631 * record of happily burning through 100% cpu (or worse, crash) when the
632 * display pipe is suspended. To avoid all that fun just reject updates
633 * that ask for events since likely that indicates a bug in the
634 * compositor's drawing loop. This is consistent with the vblank IOCTL
635 * and legacy page_flip IOCTL which also reject service on a disabled
636 * pipe.
637 */
638 if (state->event && !state->active && !crtc->state->active) {
6ac7c548
RK
639 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
640 crtc->base.id, crtc->name);
4cba6850
DV
641 return -EINVAL;
642 }
643
5e743737
RC
644 return 0;
645}
646
fceffb32
RC
647static void drm_atomic_crtc_print_state(struct drm_printer *p,
648 const struct drm_crtc_state *state)
649{
650 struct drm_crtc *crtc = state->crtc;
651
652 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
653 drm_printf(p, "\tenable=%d\n", state->enable);
654 drm_printf(p, "\tactive=%d\n", state->active);
655 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
656 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
657 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
658 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
659 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
660 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
661 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
662 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
663 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
664
665 if (crtc->funcs->atomic_print_state)
666 crtc->funcs->atomic_print_state(p, state);
667}
668
cc4ceb48
DV
669/**
670 * drm_atomic_get_plane_state - get plane state
671 * @state: global atomic state object
672 * @plane: plane to get state object for
673 *
674 * This function returns the plane state for the given plane, allocating it if
675 * needed. It will also grab the relevant plane lock to make sure that the state
676 * is consistent.
677 *
678 * Returns:
679 *
680 * Either the allocated state or the error code encoded into the pointer. When
681 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
682 * entire atomic sequence must be restarted. All other errors are fatal.
683 */
684struct drm_plane_state *
685drm_atomic_get_plane_state(struct drm_atomic_state *state,
686 struct drm_plane *plane)
687{
1b26a5e1 688 int ret, index = drm_plane_index(plane);
cc4ceb48
DV
689 struct drm_plane_state *plane_state;
690
7f4eaa89
ML
691 WARN_ON(!state->acquire_ctx);
692
1b26a5e1
ML
693 plane_state = drm_atomic_get_existing_plane_state(state, plane);
694 if (plane_state)
695 return plane_state;
cc4ceb48 696
4d02e2de 697 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
cc4ceb48
DV
698 if (ret)
699 return ERR_PTR(ret);
700
701 plane_state = plane->funcs->atomic_duplicate_state(plane);
702 if (!plane_state)
703 return ERR_PTR(-ENOMEM);
704
b8b5342b
DV
705 state->planes[index].state = plane_state;
706 state->planes[index].ptr = plane;
581e49fe
ML
707 state->planes[index].old_state = plane->state;
708 state->planes[index].new_state = plane_state;
cc4ceb48
DV
709 plane_state->state = state;
710
9f4c97a2
VS
711 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
712 plane->base.id, plane->name, plane_state, state);
cc4ceb48
DV
713
714 if (plane_state->crtc) {
715 struct drm_crtc_state *crtc_state;
716
717 crtc_state = drm_atomic_get_crtc_state(state,
718 plane_state->crtc);
719 if (IS_ERR(crtc_state))
720 return ERR_CAST(crtc_state);
721 }
722
723 return plane_state;
724}
725EXPORT_SYMBOL(drm_atomic_get_plane_state);
726
40ecc694
RC
727/**
728 * drm_atomic_plane_set_property - set property on plane
729 * @plane: the drm plane to set a property on
730 * @state: the state object to update with the new property value
731 * @property: the property to set
732 * @val: the new property value
733 *
d574528a
DV
734 * This function handles generic/core properties and calls out to driver's
735 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
736 * consistent behavior you must call this function rather than the driver hook
737 * directly.
40ecc694
RC
738 *
739 * RETURNS:
740 * Zero on success, error code on failure
741 */
742int drm_atomic_plane_set_property(struct drm_plane *plane,
743 struct drm_plane_state *state, struct drm_property *property,
744 uint64_t val)
745{
6b4959f4
RC
746 struct drm_device *dev = plane->dev;
747 struct drm_mode_config *config = &dev->mode_config;
748
749 if (property == config->prop_fb_id) {
750 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
751 drm_atomic_set_fb_for_plane(state, fb);
752 if (fb)
a4a69da0 753 drm_framebuffer_put(fb);
96260142
GP
754 } else if (property == config->prop_in_fence_fd) {
755 if (state->fence)
756 return -EINVAL;
757
758 if (U642I64(val) == -1)
759 return 0;
760
761 state->fence = sync_file_get_fence(val);
762 if (!state->fence)
763 return -EINVAL;
764
6b4959f4
RC
765 } else if (property == config->prop_crtc_id) {
766 struct drm_crtc *crtc = drm_crtc_find(dev, val);
767 return drm_atomic_set_crtc_for_plane(state, crtc);
768 } else if (property == config->prop_crtc_x) {
769 state->crtc_x = U642I64(val);
770 } else if (property == config->prop_crtc_y) {
771 state->crtc_y = U642I64(val);
772 } else if (property == config->prop_crtc_w) {
773 state->crtc_w = val;
774 } else if (property == config->prop_crtc_h) {
775 state->crtc_h = val;
776 } else if (property == config->prop_src_x) {
777 state->src_x = val;
778 } else if (property == config->prop_src_y) {
779 state->src_y = val;
780 } else if (property == config->prop_src_w) {
781 state->src_w = val;
782 } else if (property == config->prop_src_h) {
783 state->src_h = val;
6686df8c 784 } else if (property == plane->rotation_property) {
c2c446ad 785 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
6e0c7c33 786 return -EINVAL;
1da30627 787 state->rotation = val;
44d1240d
MS
788 } else if (property == plane->zpos_property) {
789 state->zpos = val;
6b4959f4
RC
790 } else if (plane->funcs->atomic_set_property) {
791 return plane->funcs->atomic_set_property(plane, state,
792 property, val);
793 } else {
794 return -EINVAL;
795 }
796
797 return 0;
40ecc694
RC
798}
799EXPORT_SYMBOL(drm_atomic_plane_set_property);
800
c0714fc9
DV
801/**
802 * drm_atomic_plane_get_property - get property value from plane state
803 * @plane: the drm plane to set a property on
804 * @state: the state object to get the property value from
805 * @property: the property to set
806 * @val: return location for the property value
807 *
d574528a
DV
808 * This function handles generic/core properties and calls out to driver's
809 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
810 * consistent behavior you must call this function rather than the driver hook
811 * directly.
c0714fc9
DV
812 *
813 * RETURNS:
814 * Zero on success, error code on failure
ac9c9256 815 */
a97df1cc
DV
816static int
817drm_atomic_plane_get_property(struct drm_plane *plane,
ac9c9256
RC
818 const struct drm_plane_state *state,
819 struct drm_property *property, uint64_t *val)
820{
6b4959f4
RC
821 struct drm_device *dev = plane->dev;
822 struct drm_mode_config *config = &dev->mode_config;
823
824 if (property == config->prop_fb_id) {
825 *val = (state->fb) ? state->fb->base.id : 0;
96260142
GP
826 } else if (property == config->prop_in_fence_fd) {
827 *val = -1;
6b4959f4
RC
828 } else if (property == config->prop_crtc_id) {
829 *val = (state->crtc) ? state->crtc->base.id : 0;
830 } else if (property == config->prop_crtc_x) {
831 *val = I642U64(state->crtc_x);
832 } else if (property == config->prop_crtc_y) {
833 *val = I642U64(state->crtc_y);
834 } else if (property == config->prop_crtc_w) {
835 *val = state->crtc_w;
836 } else if (property == config->prop_crtc_h) {
837 *val = state->crtc_h;
838 } else if (property == config->prop_src_x) {
839 *val = state->src_x;
840 } else if (property == config->prop_src_y) {
841 *val = state->src_y;
842 } else if (property == config->prop_src_w) {
843 *val = state->src_w;
844 } else if (property == config->prop_src_h) {
845 *val = state->src_h;
6686df8c 846 } else if (property == plane->rotation_property) {
4cda09ca 847 *val = state->rotation;
44d1240d
MS
848 } else if (property == plane->zpos_property) {
849 *val = state->zpos;
6b4959f4 850 } else if (plane->funcs->atomic_get_property) {
ac9c9256 851 return plane->funcs->atomic_get_property(plane, state, property, val);
6b4959f4
RC
852 } else {
853 return -EINVAL;
854 }
855
856 return 0;
ac9c9256 857}
ac9c9256 858
f8aeb41c
DV
859static bool
860plane_switching_crtc(struct drm_atomic_state *state,
861 struct drm_plane *plane,
862 struct drm_plane_state *plane_state)
863{
864 if (!plane->state->crtc || !plane_state->crtc)
865 return false;
866
867 if (plane->state->crtc == plane_state->crtc)
868 return false;
869
870 /* This could be refined, but currently there's no helper or driver code
871 * to implement direct switching of active planes nor userspace to take
872 * advantage of more direct plane switching without the intermediate
873 * full OFF state.
874 */
875 return true;
876}
877
5e743737
RC
878/**
879 * drm_atomic_plane_check - check plane state
880 * @plane: plane to check
881 * @state: plane state to check
882 *
883 * Provides core sanity checks for plane state.
884 *
885 * RETURNS:
886 * Zero on success, error code on failure
887 */
888static int drm_atomic_plane_check(struct drm_plane *plane,
889 struct drm_plane_state *state)
890{
891 unsigned int fb_width, fb_height;
ead8610d 892 int ret;
5e743737
RC
893
894 /* either *both* CRTC and FB must be set, or neither */
895 if (WARN_ON(state->crtc && !state->fb)) {
17a38d9c 896 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
5e743737
RC
897 return -EINVAL;
898 } else if (WARN_ON(state->fb && !state->crtc)) {
17a38d9c 899 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
5e743737
RC
900 return -EINVAL;
901 }
902
903 /* if disabled, we don't care about the rest of the state: */
904 if (!state->crtc)
905 return 0;
906
907 /* Check whether this plane is usable on this CRTC */
908 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
17a38d9c 909 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
5e743737
RC
910 return -EINVAL;
911 }
912
913 /* Check whether this plane supports the fb pixel format. */
438b74a5 914 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
ead8610d 915 if (ret) {
b3c11ac2
EE
916 struct drm_format_name_buf format_name;
917 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
438b74a5 918 drm_get_format_name(state->fb->format->format,
b3c11ac2 919 &format_name));
ead8610d 920 return ret;
5e743737
RC
921 }
922
923 /* Give drivers some help against integer overflows */
924 if (state->crtc_w > INT_MAX ||
925 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
926 state->crtc_h > INT_MAX ||
927 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
17a38d9c
DV
928 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
929 state->crtc_w, state->crtc_h,
930 state->crtc_x, state->crtc_y);
5e743737
RC
931 return -ERANGE;
932 }
933
934 fb_width = state->fb->width << 16;
935 fb_height = state->fb->height << 16;
936
937 /* Make sure source coordinates are inside the fb. */
938 if (state->src_w > fb_width ||
939 state->src_x > fb_width - state->src_w ||
940 state->src_h > fb_height ||
941 state->src_y > fb_height - state->src_h) {
17a38d9c
DV
942 DRM_DEBUG_ATOMIC("Invalid source coordinates "
943 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
944 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
945 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
946 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
947 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
5e743737
RC
948 return -ENOSPC;
949 }
950
f8aeb41c 951 if (plane_switching_crtc(state->state, plane, state)) {
9f4c97a2
VS
952 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
953 plane->base.id, plane->name);
f8aeb41c
DV
954 return -EINVAL;
955 }
956
5e743737
RC
957 return 0;
958}
959
fceffb32
RC
960static void drm_atomic_plane_print_state(struct drm_printer *p,
961 const struct drm_plane_state *state)
962{
963 struct drm_plane *plane = state->plane;
964 struct drm_rect src = drm_plane_state_src(state);
965 struct drm_rect dest = drm_plane_state_dest(state);
966
967 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
968 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
969 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
970 if (state->fb) {
971 struct drm_framebuffer *fb = state->fb;
bcb0b461 972 int i, n = fb->format->num_planes;
b3c11ac2 973 struct drm_format_name_buf format_name;
fceffb32
RC
974
975 drm_printf(p, "\t\tformat=%s\n",
438b74a5 976 drm_get_format_name(fb->format->format, &format_name));
bae781b2 977 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
fceffb32
RC
978 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
979 drm_printf(p, "\t\tlayers:\n");
980 for (i = 0; i < n; i++) {
981 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
982 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
fceffb32
RC
983 }
984 }
985 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
986 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
987 drm_printf(p, "\trotation=%x\n", state->rotation);
988
989 if (plane->funcs->atomic_print_state)
990 plane->funcs->atomic_print_state(p, state);
991}
992
b430c27a
PD
993/**
994 * drm_atomic_get_private_obj_state - get private object state
995 * @state: global atomic state
996 * @obj: private object to get the state for
997 * @funcs: pointer to the struct of function pointers that identify the object
998 * type
999 *
1000 * This function returns the private object state for the given private object,
1001 * allocating the state if needed. It does not grab any locks as the caller is
1002 * expected to care of any required locking.
1003 *
1004 * RETURNS:
1005 *
1006 * Either the allocated state or the error code encoded into a pointer.
1007 */
1008void *
1009drm_atomic_get_private_obj_state(struct drm_atomic_state *state, void *obj,
1010 const struct drm_private_state_funcs *funcs)
1011{
1012 int index, num_objs, i;
1013 size_t size;
1014 struct __drm_private_objs_state *arr;
1015
1016 for (i = 0; i < state->num_private_objs; i++)
1017 if (obj == state->private_objs[i].obj &&
1018 state->private_objs[i].obj_state)
1019 return state->private_objs[i].obj_state;
1020
1021 num_objs = state->num_private_objs + 1;
1022 size = sizeof(*state->private_objs) * num_objs;
1023 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1024 if (!arr)
1025 return ERR_PTR(-ENOMEM);
1026
1027 state->private_objs = arr;
1028 index = state->num_private_objs;
1029 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1030
1031 state->private_objs[index].obj_state = funcs->duplicate_state(state, obj);
1032 if (!state->private_objs[index].obj_state)
1033 return ERR_PTR(-ENOMEM);
1034
1035 state->private_objs[index].obj = obj;
1036 state->private_objs[index].funcs = funcs;
1037 state->num_private_objs = num_objs;
1038
1039 DRM_DEBUG_ATOMIC("Added new private object state %p to %p\n",
1040 state->private_objs[index].obj_state, state);
1041
1042 return state->private_objs[index].obj_state;
1043}
1044EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1045
cc4ceb48
DV
1046/**
1047 * drm_atomic_get_connector_state - get connector state
1048 * @state: global atomic state object
1049 * @connector: connector to get state object for
1050 *
1051 * This function returns the connector state for the given connector,
1052 * allocating it if needed. It will also grab the relevant connector lock to
1053 * make sure that the state is consistent.
1054 *
1055 * Returns:
1056 *
1057 * Either the allocated state or the error code encoded into the pointer. When
1058 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1059 * entire atomic sequence must be restarted. All other errors are fatal.
1060 */
1061struct drm_connector_state *
1062drm_atomic_get_connector_state(struct drm_atomic_state *state,
1063 struct drm_connector *connector)
1064{
1065 int ret, index;
1066 struct drm_mode_config *config = &connector->dev->mode_config;
1067 struct drm_connector_state *connector_state;
1068
7f4eaa89
ML
1069 WARN_ON(!state->acquire_ctx);
1070
c7eb76f4
DV
1071 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1072 if (ret)
1073 return ERR_PTR(ret);
1074
cc4ceb48
DV
1075 index = drm_connector_index(connector);
1076
f52b69f1 1077 if (index >= state->num_connector) {
63e83c1d 1078 struct __drm_connnectors_state *c;
5fff80bb
ML
1079 int alloc = max(index + 1, config->num_connector);
1080
1081 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1082 if (!c)
1083 return ERR_PTR(-ENOMEM);
1084
1085 state->connectors = c;
1086 memset(&state->connectors[state->num_connector], 0,
1087 sizeof(*state->connectors) * (alloc - state->num_connector));
1088
5fff80bb 1089 state->num_connector = alloc;
f52b69f1
DV
1090 }
1091
63e83c1d
DV
1092 if (state->connectors[index].state)
1093 return state->connectors[index].state;
cc4ceb48 1094
cc4ceb48
DV
1095 connector_state = connector->funcs->atomic_duplicate_state(connector);
1096 if (!connector_state)
1097 return ERR_PTR(-ENOMEM);
1098
ad093607 1099 drm_connector_get(connector);
63e83c1d 1100 state->connectors[index].state = connector_state;
581e49fe
ML
1101 state->connectors[index].old_state = connector->state;
1102 state->connectors[index].new_state = connector_state;
63e83c1d 1103 state->connectors[index].ptr = connector;
cc4ceb48
DV
1104 connector_state->state = state;
1105
6ac7c548
RK
1106 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1107 connector->base.id, connector->name,
1108 connector_state, state);
cc4ceb48
DV
1109
1110 if (connector_state->crtc) {
1111 struct drm_crtc_state *crtc_state;
1112
1113 crtc_state = drm_atomic_get_crtc_state(state,
1114 connector_state->crtc);
1115 if (IS_ERR(crtc_state))
1116 return ERR_CAST(crtc_state);
1117 }
1118
1119 return connector_state;
1120}
1121EXPORT_SYMBOL(drm_atomic_get_connector_state);
1122
40ecc694
RC
1123/**
1124 * drm_atomic_connector_set_property - set property on connector.
1125 * @connector: the drm connector to set a property on
1126 * @state: the state object to update with the new property value
1127 * @property: the property to set
1128 * @val: the new property value
1129 *
d574528a
DV
1130 * This function handles generic/core properties and calls out to driver's
1131 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1132 * consistent behavior you must call this function rather than the driver hook
1133 * directly.
40ecc694
RC
1134 *
1135 * RETURNS:
1136 * Zero on success, error code on failure
1137 */
1138int drm_atomic_connector_set_property(struct drm_connector *connector,
1139 struct drm_connector_state *state, struct drm_property *property,
1140 uint64_t val)
1141{
1142 struct drm_device *dev = connector->dev;
1143 struct drm_mode_config *config = &dev->mode_config;
1144
ae16c597
RC
1145 if (property == config->prop_crtc_id) {
1146 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1147 return drm_atomic_set_crtc_for_connector(state, crtc);
1148 } else if (property == config->dpms_property) {
40ecc694
RC
1149 /* setting DPMS property requires special handling, which
1150 * is done in legacy setprop path for us. Disallow (for
1151 * now?) atomic writes to DPMS property:
1152 */
1153 return -EINVAL;
299a16b1
BB
1154 } else if (property == config->tv_select_subconnector_property) {
1155 state->tv.subconnector = val;
1156 } else if (property == config->tv_left_margin_property) {
1157 state->tv.margins.left = val;
1158 } else if (property == config->tv_right_margin_property) {
1159 state->tv.margins.right = val;
1160 } else if (property == config->tv_top_margin_property) {
1161 state->tv.margins.top = val;
1162 } else if (property == config->tv_bottom_margin_property) {
1163 state->tv.margins.bottom = val;
1164 } else if (property == config->tv_mode_property) {
1165 state->tv.mode = val;
1166 } else if (property == config->tv_brightness_property) {
1167 state->tv.brightness = val;
1168 } else if (property == config->tv_contrast_property) {
1169 state->tv.contrast = val;
1170 } else if (property == config->tv_flicker_reduction_property) {
1171 state->tv.flicker_reduction = val;
1172 } else if (property == config->tv_overscan_property) {
1173 state->tv.overscan = val;
1174 } else if (property == config->tv_saturation_property) {
1175 state->tv.saturation = val;
1176 } else if (property == config->tv_hue_property) {
1177 state->tv.hue = val;
40ee6fbe
MN
1178 } else if (property == config->link_status_property) {
1179 /* Never downgrade from GOOD to BAD on userspace's request here,
1180 * only hw issues can do that.
1181 *
1182 * For an atomic property the userspace doesn't need to be able
1183 * to understand all the properties, but needs to be able to
1184 * restore the state it wants on VT switch. So if the userspace
1185 * tries to change the link_status from GOOD to BAD, driver
1186 * silently rejects it and returns a 0. This prevents userspace
1187 * from accidently breaking the display when it restores the
1188 * state.
1189 */
1190 if (state->link_status != DRM_LINK_STATUS_GOOD)
1191 state->link_status = val;
0e9f25d0
ML
1192 } else if (property == config->aspect_ratio_property) {
1193 state->picture_aspect_ratio = val;
8f6e1e22
ML
1194 } else if (property == connector->scaling_mode_property) {
1195 state->scaling_mode = val;
40ecc694
RC
1196 } else if (connector->funcs->atomic_set_property) {
1197 return connector->funcs->atomic_set_property(connector,
1198 state, property, val);
1199 } else {
1200 return -EINVAL;
1201 }
299a16b1
BB
1202
1203 return 0;
40ecc694
RC
1204}
1205EXPORT_SYMBOL(drm_atomic_connector_set_property);
1206
fceffb32
RC
1207static void drm_atomic_connector_print_state(struct drm_printer *p,
1208 const struct drm_connector_state *state)
1209{
1210 struct drm_connector *connector = state->connector;
1211
1212 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1213 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1214
1215 if (connector->funcs->atomic_print_state)
1216 connector->funcs->atomic_print_state(p, state);
1217}
1218
c0714fc9
DV
1219/**
1220 * drm_atomic_connector_get_property - get property value from connector state
1221 * @connector: the drm connector to set a property on
1222 * @state: the state object to get the property value from
1223 * @property: the property to set
1224 * @val: return location for the property value
1225 *
d574528a
DV
1226 * This function handles generic/core properties and calls out to driver's
1227 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1228 * consistent behavior you must call this function rather than the driver hook
1229 * directly.
c0714fc9
DV
1230 *
1231 * RETURNS:
1232 * Zero on success, error code on failure
ac9c9256 1233 */
a97df1cc
DV
1234static int
1235drm_atomic_connector_get_property(struct drm_connector *connector,
ac9c9256
RC
1236 const struct drm_connector_state *state,
1237 struct drm_property *property, uint64_t *val)
1238{
1239 struct drm_device *dev = connector->dev;
1240 struct drm_mode_config *config = &dev->mode_config;
1241
ae16c597
RC
1242 if (property == config->prop_crtc_id) {
1243 *val = (state->crtc) ? state->crtc->base.id : 0;
1244 } else if (property == config->dpms_property) {
ac9c9256 1245 *val = connector->dpms;
299a16b1
BB
1246 } else if (property == config->tv_select_subconnector_property) {
1247 *val = state->tv.subconnector;
1248 } else if (property == config->tv_left_margin_property) {
1249 *val = state->tv.margins.left;
1250 } else if (property == config->tv_right_margin_property) {
1251 *val = state->tv.margins.right;
1252 } else if (property == config->tv_top_margin_property) {
1253 *val = state->tv.margins.top;
1254 } else if (property == config->tv_bottom_margin_property) {
1255 *val = state->tv.margins.bottom;
1256 } else if (property == config->tv_mode_property) {
1257 *val = state->tv.mode;
1258 } else if (property == config->tv_brightness_property) {
1259 *val = state->tv.brightness;
1260 } else if (property == config->tv_contrast_property) {
1261 *val = state->tv.contrast;
1262 } else if (property == config->tv_flicker_reduction_property) {
1263 *val = state->tv.flicker_reduction;
1264 } else if (property == config->tv_overscan_property) {
1265 *val = state->tv.overscan;
1266 } else if (property == config->tv_saturation_property) {
1267 *val = state->tv.saturation;
1268 } else if (property == config->tv_hue_property) {
1269 *val = state->tv.hue;
40ee6fbe
MN
1270 } else if (property == config->link_status_property) {
1271 *val = state->link_status;
0e9f25d0
ML
1272 } else if (property == config->aspect_ratio_property) {
1273 *val = state->picture_aspect_ratio;
8f6e1e22
ML
1274 } else if (property == connector->scaling_mode_property) {
1275 *val = state->scaling_mode;
ac9c9256
RC
1276 } else if (connector->funcs->atomic_get_property) {
1277 return connector->funcs->atomic_get_property(connector,
1278 state, property, val);
1279 } else {
1280 return -EINVAL;
1281 }
1282
1283 return 0;
1284}
ac9c9256 1285
88a48e29
RC
1286int drm_atomic_get_property(struct drm_mode_object *obj,
1287 struct drm_property *property, uint64_t *val)
1288{
1289 struct drm_device *dev = property->dev;
1290 int ret;
1291
1292 switch (obj->type) {
1293 case DRM_MODE_OBJECT_CONNECTOR: {
1294 struct drm_connector *connector = obj_to_connector(obj);
1295 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1296 ret = drm_atomic_connector_get_property(connector,
1297 connector->state, property, val);
1298 break;
1299 }
1300 case DRM_MODE_OBJECT_CRTC: {
1301 struct drm_crtc *crtc = obj_to_crtc(obj);
1302 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1303 ret = drm_atomic_crtc_get_property(crtc,
1304 crtc->state, property, val);
1305 break;
1306 }
1307 case DRM_MODE_OBJECT_PLANE: {
1308 struct drm_plane *plane = obj_to_plane(obj);
1309 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1310 ret = drm_atomic_plane_get_property(plane,
1311 plane->state, property, val);
1312 break;
1313 }
1314 default:
1315 ret = -EINVAL;
1316 break;
1317 }
1318
1319 return ret;
1320}
1321
cc4ceb48
DV
1322/**
1323 * drm_atomic_set_crtc_for_plane - set crtc for plane
07cc0ef6 1324 * @plane_state: the plane whose incoming state to update
cc4ceb48
DV
1325 * @crtc: crtc to use for the plane
1326 *
1327 * Changing the assigned crtc for a plane requires us to grab the lock and state
1328 * for the new crtc, as needed. This function takes care of all these details
1329 * besides updating the pointer in the state object itself.
1330 *
1331 * Returns:
1332 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1333 * then the w/w mutex code has detected a deadlock and the entire atomic
1334 * sequence must be restarted. All other errors are fatal.
1335 */
1336int
07cc0ef6
DV
1337drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1338 struct drm_crtc *crtc)
cc4ceb48 1339{
07cc0ef6 1340 struct drm_plane *plane = plane_state->plane;
cc4ceb48
DV
1341 struct drm_crtc_state *crtc_state;
1342
6ddd388a
RC
1343 if (plane_state->crtc) {
1344 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1345 plane_state->crtc);
1346 if (WARN_ON(IS_ERR(crtc_state)))
1347 return PTR_ERR(crtc_state);
1348
1349 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1350 }
1351
1352 plane_state->crtc = crtc;
1353
cc4ceb48
DV
1354 if (crtc) {
1355 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1356 crtc);
1357 if (IS_ERR(crtc_state))
1358 return PTR_ERR(crtc_state);
6ddd388a 1359 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
cc4ceb48
DV
1360 }
1361
cc4ceb48 1362 if (crtc)
fa3ab4c2
VS
1363 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1364 plane_state, crtc->base.id, crtc->name);
cc4ceb48 1365 else
17a38d9c
DV
1366 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1367 plane_state);
cc4ceb48
DV
1368
1369 return 0;
1370}
1371EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1372
321ebf04 1373/**
16d78bc2 1374 * drm_atomic_set_fb_for_plane - set framebuffer for plane
321ebf04
DV
1375 * @plane_state: atomic state object for the plane
1376 * @fb: fb to use for the plane
1377 *
1378 * Changing the assigned framebuffer for a plane requires us to grab a reference
1379 * to the new fb and drop the reference to the old fb, if there is one. This
1380 * function takes care of all these details besides updating the pointer in the
1381 * state object itself.
1382 */
1383void
1384drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1385 struct drm_framebuffer *fb)
1386{
321ebf04 1387 if (fb)
17a38d9c
DV
1388 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1389 fb->base.id, plane_state);
321ebf04 1390 else
17a38d9c
DV
1391 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1392 plane_state);
389f78b3
CW
1393
1394 drm_framebuffer_assign(&plane_state->fb, fb);
321ebf04
DV
1395}
1396EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1397
13b55664
GP
1398/**
1399 * drm_atomic_set_fence_for_plane - set fence for plane
1400 * @plane_state: atomic state object for the plane
1401 * @fence: dma_fence to use for the plane
1402 *
1403 * Helper to setup the plane_state fence in case it is not set yet.
1404 * By using this drivers doesn't need to worry if the user choose
1405 * implicit or explicit fencing.
1406 *
1407 * This function will not set the fence to the state if it was set
d574528a
DV
1408 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1409 * drop the reference to the fence as we are not storing it anywhere.
1410 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1411 * with the received implicit fence. In both cases this function consumes a
1412 * reference for @fence.
13b55664
GP
1413 */
1414void
1415drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1416 struct dma_fence *fence)
1417{
1418 if (plane_state->fence) {
1419 dma_fence_put(fence);
1420 return;
1421 }
1422
1423 plane_state->fence = fence;
1424}
1425EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1426
cc4ceb48
DV
1427/**
1428 * drm_atomic_set_crtc_for_connector - set crtc for connector
1429 * @conn_state: atomic state object for the connector
1430 * @crtc: crtc to use for the connector
1431 *
1432 * Changing the assigned crtc for a connector requires us to grab the lock and
1433 * state for the new crtc, as needed. This function takes care of all these
1434 * details besides updating the pointer in the state object itself.
1435 *
1436 * Returns:
1437 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1438 * then the w/w mutex code has detected a deadlock and the entire atomic
1439 * sequence must be restarted. All other errors are fatal.
1440 */
1441int
1442drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1443 struct drm_crtc *crtc)
1444{
1445 struct drm_crtc_state *crtc_state;
1446
e2d800a3
CW
1447 if (conn_state->crtc == crtc)
1448 return 0;
1449
1450 if (conn_state->crtc) {
b4d93679
ML
1451 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1452 conn_state->crtc);
4cd9fa52
ML
1453
1454 crtc_state->connector_mask &=
1455 ~(1 << drm_connector_index(conn_state->connector));
e2d800a3 1456
ad093607 1457 drm_connector_put(conn_state->connector);
e2d800a3 1458 conn_state->crtc = NULL;
4cd9fa52
ML
1459 }
1460
cc4ceb48
DV
1461 if (crtc) {
1462 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1463 if (IS_ERR(crtc_state))
1464 return PTR_ERR(crtc_state);
4cd9fa52
ML
1465
1466 crtc_state->connector_mask |=
1467 1 << drm_connector_index(conn_state->connector);
cc4ceb48 1468
ad093607 1469 drm_connector_get(conn_state->connector);
e2d800a3 1470 conn_state->crtc = crtc;
cc4ceb48 1471
fa3ab4c2
VS
1472 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1473 conn_state, crtc->base.id, crtc->name);
e2d800a3 1474 } else {
17a38d9c
DV
1475 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1476 conn_state);
e2d800a3 1477 }
cc4ceb48
DV
1478
1479 return 0;
1480}
1481EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1482
1483/**
1484 * drm_atomic_add_affected_connectors - add connectors for crtc
1485 * @state: atomic state
1486 * @crtc: DRM crtc
1487 *
1488 * This function walks the current configuration and adds all connectors
1489 * currently using @crtc to the atomic configuration @state. Note that this
1490 * function must acquire the connection mutex. This can potentially cause
1491 * unneeded seralization if the update is just for the planes on one crtc. Hence
1492 * drivers and helpers should only call this when really needed (e.g. when a
1493 * full modeset needs to happen due to some change).
1494 *
1495 * Returns:
1496 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1497 * then the w/w mutex code has detected a deadlock and the entire atomic
1498 * sequence must be restarted. All other errors are fatal.
1499 */
1500int
1501drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1502 struct drm_crtc *crtc)
1503{
1504 struct drm_mode_config *config = &state->dev->mode_config;
1505 struct drm_connector *connector;
1506 struct drm_connector_state *conn_state;
613051da 1507 struct drm_connector_list_iter conn_iter;
5351bbdd 1508 struct drm_crtc_state *crtc_state;
cc4ceb48
DV
1509 int ret;
1510
5351bbdd
ML
1511 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1512 if (IS_ERR(crtc_state))
1513 return PTR_ERR(crtc_state);
1514
cc4ceb48
DV
1515 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1516 if (ret)
1517 return ret;
1518
fa3ab4c2
VS
1519 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1520 crtc->base.id, crtc->name, state);
cc4ceb48
DV
1521
1522 /*
5351bbdd
ML
1523 * Changed connectors are already in @state, so only need to look
1524 * at the connector_mask in crtc_state.
cc4ceb48 1525 */
b982dab1 1526 drm_connector_list_iter_begin(state->dev, &conn_iter);
613051da 1527 drm_for_each_connector_iter(connector, &conn_iter) {
5351bbdd 1528 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
cc4ceb48
DV
1529 continue;
1530
1531 conn_state = drm_atomic_get_connector_state(state, connector);
613051da 1532 if (IS_ERR(conn_state)) {
b982dab1 1533 drm_connector_list_iter_end(&conn_iter);
cc4ceb48 1534 return PTR_ERR(conn_state);
613051da 1535 }
cc4ceb48 1536 }
b982dab1 1537 drm_connector_list_iter_end(&conn_iter);
cc4ceb48
DV
1538
1539 return 0;
1540}
1541EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1542
e01e9f75
ML
1543/**
1544 * drm_atomic_add_affected_planes - add planes for crtc
1545 * @state: atomic state
1546 * @crtc: DRM crtc
1547 *
1548 * This function walks the current configuration and adds all planes
1549 * currently used by @crtc to the atomic configuration @state. This is useful
1550 * when an atomic commit also needs to check all currently enabled plane on
1551 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1552 * to avoid special code to force-enable all planes.
1553 *
1554 * Since acquiring a plane state will always also acquire the w/w mutex of the
1555 * current CRTC for that plane (if there is any) adding all the plane states for
1556 * a CRTC will not reduce parallism of atomic updates.
1557 *
1558 * Returns:
1559 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1560 * then the w/w mutex code has detected a deadlock and the entire atomic
1561 * sequence must be restarted. All other errors are fatal.
1562 */
1563int
1564drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1565 struct drm_crtc *crtc)
1566{
1567 struct drm_plane *plane;
1568
b4d93679 1569 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
e01e9f75
ML
1570
1571 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1572 struct drm_plane_state *plane_state =
1573 drm_atomic_get_plane_state(state, plane);
1574
1575 if (IS_ERR(plane_state))
1576 return PTR_ERR(plane_state);
1577 }
1578 return 0;
1579}
1580EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1581
cc4ceb48
DV
1582/**
1583 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1584 * @state: atomic state
1585 *
1586 * This function should be used by legacy entry points which don't understand
1587 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
16d78bc2 1588 * the slowpath completed.
cc4ceb48
DV
1589 */
1590void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1591{
81e257e9 1592 struct drm_device *dev = state->dev;
cc4ceb48 1593 int ret;
81e257e9
ML
1594 bool global = false;
1595
81e257e9
ML
1596 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1597 global = true;
1598
1599 dev->mode_config.acquire_ctx = NULL;
1600 }
cc4ceb48
DV
1601
1602retry:
1603 drm_modeset_backoff(state->acquire_ctx);
1604
81e257e9 1605 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
cc4ceb48
DV
1606 if (ret)
1607 goto retry;
81e257e9 1608
81e257e9
ML
1609 if (global)
1610 dev->mode_config.acquire_ctx = state->acquire_ctx;
cc4ceb48
DV
1611}
1612EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1613
1614/**
1615 * drm_atomic_check_only - check whether a given config would work
1616 * @state: atomic configuration to check
1617 *
1618 * Note that this function can return -EDEADLK if the driver needed to acquire
1619 * more locks but encountered a deadlock. The caller must then do the usual w/w
1620 * backoff dance and restart. All other errors are fatal.
1621 *
1622 * Returns:
1623 * 0 on success, negative error code on failure.
1624 */
1625int drm_atomic_check_only(struct drm_atomic_state *state)
1626{
5e743737
RC
1627 struct drm_device *dev = state->dev;
1628 struct drm_mode_config *config = &dev->mode_config;
df63b999
ACO
1629 struct drm_plane *plane;
1630 struct drm_plane_state *plane_state;
1631 struct drm_crtc *crtc;
1632 struct drm_crtc_state *crtc_state;
5e743737 1633 int i, ret = 0;
cc4ceb48 1634
17a38d9c 1635 DRM_DEBUG_ATOMIC("checking %p\n", state);
cc4ceb48 1636
5721a380 1637 for_each_new_plane_in_state(state, plane, plane_state, i) {
df63b999 1638 ret = drm_atomic_plane_check(plane, plane_state);
5e743737 1639 if (ret) {
9f4c97a2
VS
1640 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1641 plane->base.id, plane->name);
5e743737
RC
1642 return ret;
1643 }
1644 }
1645
5721a380 1646 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
df63b999 1647 ret = drm_atomic_crtc_check(crtc, crtc_state);
5e743737 1648 if (ret) {
fa3ab4c2
VS
1649 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1650 crtc->base.id, crtc->name);
5e743737
RC
1651 return ret;
1652 }
1653 }
1654
cc4ceb48 1655 if (config->funcs->atomic_check)
5e743737
RC
1656 ret = config->funcs->atomic_check(state->dev, state);
1657
d34f20d6 1658 if (!state->allow_modeset) {
5721a380 1659 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2465ff62 1660 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
fa3ab4c2
VS
1661 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1662 crtc->base.id, crtc->name);
d34f20d6
RC
1663 return -EINVAL;
1664 }
1665 }
1666 }
1667
5e743737 1668 return ret;
cc4ceb48
DV
1669}
1670EXPORT_SYMBOL(drm_atomic_check_only);
1671
1672/**
1673 * drm_atomic_commit - commit configuration atomically
1674 * @state: atomic configuration to check
1675 *
1676 * Note that this function can return -EDEADLK if the driver needed to acquire
1677 * more locks but encountered a deadlock. The caller must then do the usual w/w
1678 * backoff dance and restart. All other errors are fatal.
1679 *
76fede2f
ML
1680 * This function will take its own reference on @state.
1681 * Callers should always release their reference with drm_atomic_state_put().
cc4ceb48
DV
1682 *
1683 * Returns:
1684 * 0 on success, negative error code on failure.
1685 */
1686int drm_atomic_commit(struct drm_atomic_state *state)
1687{
1688 struct drm_mode_config *config = &state->dev->mode_config;
1689 int ret;
1690
1691 ret = drm_atomic_check_only(state);
1692 if (ret)
1693 return ret;
1694
a0752d4a 1695 DRM_DEBUG_ATOMIC("committing %p\n", state);
cc4ceb48
DV
1696
1697 return config->funcs->atomic_commit(state->dev, state, false);
1698}
1699EXPORT_SYMBOL(drm_atomic_commit);
1700
1701/**
d574528a 1702 * drm_atomic_nonblocking_commit - atomic nonblocking commit
cc4ceb48
DV
1703 * @state: atomic configuration to check
1704 *
1705 * Note that this function can return -EDEADLK if the driver needed to acquire
1706 * more locks but encountered a deadlock. The caller must then do the usual w/w
1707 * backoff dance and restart. All other errors are fatal.
1708 *
76fede2f
ML
1709 * This function will take its own reference on @state.
1710 * Callers should always release their reference with drm_atomic_state_put().
cc4ceb48
DV
1711 *
1712 * Returns:
1713 * 0 on success, negative error code on failure.
1714 */
b837ba0a 1715int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
cc4ceb48
DV
1716{
1717 struct drm_mode_config *config = &state->dev->mode_config;
1718 int ret;
1719
1720 ret = drm_atomic_check_only(state);
1721 if (ret)
1722 return ret;
1723
a0752d4a 1724 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
cc4ceb48
DV
1725
1726 return config->funcs->atomic_commit(state->dev, state, true);
1727}
b837ba0a 1728EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
d34f20d6 1729
fceffb32
RC
1730static void drm_atomic_print_state(const struct drm_atomic_state *state)
1731{
1732 struct drm_printer p = drm_info_printer(state->dev->dev);
1733 struct drm_plane *plane;
1734 struct drm_plane_state *plane_state;
1735 struct drm_crtc *crtc;
1736 struct drm_crtc_state *crtc_state;
1737 struct drm_connector *connector;
1738 struct drm_connector_state *connector_state;
1739 int i;
1740
1741 DRM_DEBUG_ATOMIC("checking %p\n", state);
1742
5721a380 1743 for_each_new_plane_in_state(state, plane, plane_state, i)
fceffb32
RC
1744 drm_atomic_plane_print_state(&p, plane_state);
1745
5721a380 1746 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
fceffb32
RC
1747 drm_atomic_crtc_print_state(&p, crtc_state);
1748
5721a380 1749 for_each_new_connector_in_state(state, connector, connector_state, i)
fceffb32
RC
1750 drm_atomic_connector_print_state(&p, connector_state);
1751}
1752
c2d85564
DV
1753static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1754 bool take_locks)
6559c901
RC
1755{
1756 struct drm_mode_config *config = &dev->mode_config;
1757 struct drm_plane *plane;
1758 struct drm_crtc *crtc;
1759 struct drm_connector *connector;
613051da 1760 struct drm_connector_list_iter conn_iter;
6559c901
RC
1761
1762 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1763 return;
1764
c2d85564
DV
1765 list_for_each_entry(plane, &config->plane_list, head) {
1766 if (take_locks)
1767 drm_modeset_lock(&plane->mutex, NULL);
6559c901 1768 drm_atomic_plane_print_state(p, plane->state);
c2d85564
DV
1769 if (take_locks)
1770 drm_modeset_unlock(&plane->mutex);
1771 }
6559c901 1772
c2d85564
DV
1773 list_for_each_entry(crtc, &config->crtc_list, head) {
1774 if (take_locks)
1775 drm_modeset_lock(&crtc->mutex, NULL);
6559c901 1776 drm_atomic_crtc_print_state(p, crtc->state);
c2d85564
DV
1777 if (take_locks)
1778 drm_modeset_unlock(&crtc->mutex);
1779 }
6559c901 1780
b982dab1 1781 drm_connector_list_iter_begin(dev, &conn_iter);
c2d85564
DV
1782 if (take_locks)
1783 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
613051da 1784 drm_for_each_connector_iter(connector, &conn_iter)
6559c901 1785 drm_atomic_connector_print_state(p, connector->state);
c2d85564
DV
1786 if (take_locks)
1787 drm_modeset_unlock(&dev->mode_config.connection_mutex);
b982dab1 1788 drm_connector_list_iter_end(&conn_iter);
6559c901 1789}
c2d85564
DV
1790
1791/**
1792 * drm_state_dump - dump entire device atomic state
1793 * @dev: the drm device
1794 * @p: where to print the state to
1795 *
1796 * Just for debugging. Drivers might want an option to dump state
1797 * to dmesg in case of error irq's. (Hint, you probably want to
1798 * ratelimit this!)
1799 *
1800 * The caller must drm_modeset_lock_all(), or if this is called
1801 * from error irq handler, it should not be enabled by default.
1802 * (Ie. if you are debugging errors you might not care that this
1803 * is racey. But calling this without all modeset locks held is
1804 * not inherently safe.)
1805 */
1806void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1807{
1808 __drm_state_dump(dev, p, false);
1809}
6559c901
RC
1810EXPORT_SYMBOL(drm_state_dump);
1811
1812#ifdef CONFIG_DEBUG_FS
1813static int drm_state_info(struct seq_file *m, void *data)
1814{
1815 struct drm_info_node *node = (struct drm_info_node *) m->private;
1816 struct drm_device *dev = node->minor->dev;
1817 struct drm_printer p = drm_seq_file_printer(m);
1818
c2d85564 1819 __drm_state_dump(dev, &p, true);
6559c901
RC
1820
1821 return 0;
1822}
1823
1824/* any use in debugfs files to dump individual planes/crtc/etc? */
1825static const struct drm_info_list drm_atomic_debugfs_list[] = {
1826 {"state", drm_state_info, 0},
1827};
1828
1829int drm_atomic_debugfs_init(struct drm_minor *minor)
1830{
1831 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1832 ARRAY_SIZE(drm_atomic_debugfs_list),
1833 minor->debugfs_root, minor);
1834}
1835#endif
1836
d34f20d6
RC
1837/*
1838 * The big monstor ioctl
1839 */
1840
1841static struct drm_pending_vblank_event *create_vblank_event(
beaf5af4 1842 struct drm_device *dev, uint64_t user_data)
d34f20d6
RC
1843{
1844 struct drm_pending_vblank_event *e = NULL;
d34f20d6
RC
1845
1846 e = kzalloc(sizeof *e, GFP_KERNEL);
2dd500f1
DV
1847 if (!e)
1848 return NULL;
d34f20d6
RC
1849
1850 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2dd500f1 1851 e->event.base.length = sizeof(e->event);
d34f20d6 1852 e->event.user_data = user_data;
d34f20d6 1853
2dd500f1 1854 return e;
d34f20d6
RC
1855}
1856
1857static int atomic_set_prop(struct drm_atomic_state *state,
1858 struct drm_mode_object *obj, struct drm_property *prop,
1859 uint64_t prop_value)
1860{
1861 struct drm_mode_object *ref;
1862 int ret;
1863
1864 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1865 return -EINVAL;
1866
1867 switch (obj->type) {
1868 case DRM_MODE_OBJECT_CONNECTOR: {
1869 struct drm_connector *connector = obj_to_connector(obj);
1870 struct drm_connector_state *connector_state;
1871
1872 connector_state = drm_atomic_get_connector_state(state, connector);
1873 if (IS_ERR(connector_state)) {
1874 ret = PTR_ERR(connector_state);
1875 break;
1876 }
1877
1878 ret = drm_atomic_connector_set_property(connector,
1879 connector_state, prop, prop_value);
1880 break;
1881 }
1882 case DRM_MODE_OBJECT_CRTC: {
1883 struct drm_crtc *crtc = obj_to_crtc(obj);
1884 struct drm_crtc_state *crtc_state;
1885
1886 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1887 if (IS_ERR(crtc_state)) {
1888 ret = PTR_ERR(crtc_state);
1889 break;
1890 }
1891
1892 ret = drm_atomic_crtc_set_property(crtc,
1893 crtc_state, prop, prop_value);
1894 break;
1895 }
1896 case DRM_MODE_OBJECT_PLANE: {
1897 struct drm_plane *plane = obj_to_plane(obj);
1898 struct drm_plane_state *plane_state;
1899
1900 plane_state = drm_atomic_get_plane_state(state, plane);
1901 if (IS_ERR(plane_state)) {
1902 ret = PTR_ERR(plane_state);
1903 break;
1904 }
1905
1906 ret = drm_atomic_plane_set_property(plane,
1907 plane_state, prop, prop_value);
1908 break;
1909 }
1910 default:
1911 ret = -EINVAL;
1912 break;
1913 }
1914
1915 drm_property_change_valid_put(prop, ref);
1916 return ret;
1917}
1918
0f45c26f 1919/**
9744bf41 1920 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
0f45c26f
ML
1921 *
1922 * @dev: drm device to check.
1923 * @plane_mask: plane mask for planes that were updated.
1924 * @ret: return value, can be -EDEADLK for a retry.
1925 *
d574528a
DV
1926 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1927 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1928 * is a common operation for each atomic update, so this call is split off as a
1929 * helper.
0f45c26f
ML
1930 */
1931void drm_atomic_clean_old_fb(struct drm_device *dev,
1932 unsigned plane_mask,
1933 int ret)
1934{
1935 struct drm_plane *plane;
1936
1937 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1938 * locks (ie. while it is still safe to deref plane->state). We
1939 * need to do this here because the driver entry points cannot
1940 * distinguish between legacy and atomic ioctls.
1941 */
1942 drm_for_each_plane_mask(plane, dev, plane_mask) {
1943 if (ret == 0) {
1944 struct drm_framebuffer *new_fb = plane->state->fb;
1945 if (new_fb)
a4a69da0 1946 drm_framebuffer_get(new_fb);
0f45c26f
ML
1947 plane->fb = new_fb;
1948 plane->crtc = plane->state->crtc;
1949
1950 if (plane->old_fb)
a4a69da0 1951 drm_framebuffer_put(plane->old_fb);
0f45c26f
ML
1952 }
1953 plane->old_fb = NULL;
1954 }
1955}
1956EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1957
9a83a71a
GP
1958/**
1959 * DOC: explicit fencing properties
1960 *
1961 * Explicit fencing allows userspace to control the buffer synchronization
1962 * between devices. A Fence or a group of fences are transfered to/from
1963 * userspace using Sync File fds and there are two DRM properties for that.
1964 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1965 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1966 *
1967 * As a contrast, with implicit fencing the kernel keeps track of any
1968 * ongoing rendering, and automatically ensures that the atomic update waits
1969 * for any pending rendering to complete. For shared buffers represented with
d574528a 1970 * a &struct dma_buf this is tracked in &struct reservation_object.
9a83a71a
GP
1971 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1972 * whereas explicit fencing is what Android wants.
1973 *
1974 * "IN_FENCE_FD”:
1975 * Use this property to pass a fence that DRM should wait on before
1976 * proceeding with the Atomic Commit request and show the framebuffer for
1977 * the plane on the screen. The fence can be either a normal fence or a
1978 * merged one, the sync_file framework will handle both cases and use a
1979 * fence_array if a merged fence is received. Passing -1 here means no
1980 * fences to wait on.
1981 *
1982 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1983 * it will only check if the Sync File is a valid one.
1984 *
1985 * On the driver side the fence is stored on the @fence parameter of
ea0dd85a 1986 * &struct drm_plane_state. Drivers which also support implicit fencing
9a83a71a
GP
1987 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1988 * to make sure there's consistent behaviour between drivers in precedence
1989 * of implicit vs. explicit fencing.
1990 *
1991 * "OUT_FENCE_PTR”:
1992 * Use this property to pass a file descriptor pointer to DRM. Once the
1993 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1994 * the file descriptor number of a Sync File. This Sync File contains the
1995 * CRTC fence that will be signaled when all framebuffers present on the
1996 * Atomic Commit * request for that given CRTC are scanned out on the
1997 * screen.
1998 *
1999 * The Atomic Commit request fails if a invalid pointer is passed. If the
2000 * Atomic Commit request fails for any other reason the out fence fd
2001 * returned will be -1. On a Atomic Commit with the
2002 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2003 *
2004 * Note that out-fences don't have a special interface to drivers and are
ea0dd85a 2005 * internally represented by a &struct drm_pending_vblank_event in struct
9a83a71a
GP
2006 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2007 * helpers and for the DRM event handling for existing userspace.
2008 */
2009
beaf5af4 2010struct drm_out_fence_state {
7e9081c5 2011 s32 __user *out_fence_ptr;
beaf5af4
GP
2012 struct sync_file *sync_file;
2013 int fd;
2014};
2015
2016static int setup_out_fence(struct drm_out_fence_state *fence_state,
2017 struct dma_fence *fence)
2018{
2019 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2020 if (fence_state->fd < 0)
2021 return fence_state->fd;
2022
2023 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2024 return -EFAULT;
2025
2026 fence_state->sync_file = sync_file_create(fence);
2027 if (!fence_state->sync_file)
2028 return -ENOMEM;
2029
2030 return 0;
2031}
2032
2033static int prepare_crtc_signaling(struct drm_device *dev,
2034 struct drm_atomic_state *state,
2035 struct drm_mode_atomic *arg,
2036 struct drm_file *file_priv,
2037 struct drm_out_fence_state **fence_state,
2038 unsigned int *num_fences)
2039{
2040 struct drm_crtc *crtc;
2041 struct drm_crtc_state *crtc_state;
2042 int i, ret;
2043
2044 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2045 return 0;
2046
5721a380 2047 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
7e9081c5 2048 s32 __user *fence_ptr;
beaf5af4
GP
2049
2050 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2051
2052 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2053 struct drm_pending_vblank_event *e;
2054
2055 e = create_vblank_event(dev, arg->user_data);
2056 if (!e)
2057 return -ENOMEM;
2058
2059 crtc_state->event = e;
2060 }
2061
2062 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2063 struct drm_pending_vblank_event *e = crtc_state->event;
2064
2065 if (!file_priv)
2066 continue;
2067
2068 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2069 &e->event.base);
2070 if (ret) {
2071 kfree(e);
2072 crtc_state->event = NULL;
2073 return ret;
2074 }
2075 }
2076
2077 if (fence_ptr) {
2078 struct dma_fence *fence;
2079 struct drm_out_fence_state *f;
2080
2081 f = krealloc(*fence_state, sizeof(**fence_state) *
2082 (*num_fences + 1), GFP_KERNEL);
2083 if (!f)
2084 return -ENOMEM;
2085
2086 memset(&f[*num_fences], 0, sizeof(*f));
2087
2088 f[*num_fences].out_fence_ptr = fence_ptr;
2089 *fence_state = f;
2090
35f8cc3b 2091 fence = drm_crtc_create_fence(crtc);
beaf5af4
GP
2092 if (!fence)
2093 return -ENOMEM;
2094
2095 ret = setup_out_fence(&f[(*num_fences)++], fence);
2096 if (ret) {
2097 dma_fence_put(fence);
2098 return ret;
2099 }
2100
2101 crtc_state->event->base.fence = fence;
2102 }
2103 }
2104
2105 return 0;
2106}
2107
2108static void complete_crtc_signaling(struct drm_device *dev,
2109 struct drm_atomic_state *state,
2110 struct drm_out_fence_state *fence_state,
2111 unsigned int num_fences,
2112 bool install_fds)
2113{
2114 struct drm_crtc *crtc;
2115 struct drm_crtc_state *crtc_state;
2116 int i;
2117
2118 if (install_fds) {
2119 for (i = 0; i < num_fences; i++)
2120 fd_install(fence_state[i].fd,
2121 fence_state[i].sync_file->file);
2122
2123 kfree(fence_state);
2124 return;
2125 }
2126
5721a380 2127 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
92c715fc 2128 struct drm_pending_vblank_event *event = crtc_state->event;
beaf5af4 2129 /*
92c715fc
ML
2130 * Free the allocated event. drm_atomic_helper_setup_commit
2131 * can allocate an event too, so only free it if it's ours
2132 * to prevent a double free in drm_atomic_state_clear.
beaf5af4 2133 */
92c715fc
ML
2134 if (event && (event->base.fence || event->base.file_priv)) {
2135 drm_event_cancel_free(dev, &event->base);
2136 crtc_state->event = NULL;
2137 }
beaf5af4
GP
2138 }
2139
2140 if (!fence_state)
2141 return;
2142
2143 for (i = 0; i < num_fences; i++) {
2144 if (fence_state[i].sync_file)
2145 fput(fence_state[i].sync_file->file);
2146 if (fence_state[i].fd >= 0)
2147 put_unused_fd(fence_state[i].fd);
2148
2149 /* If this fails log error to the user */
2150 if (fence_state[i].out_fence_ptr &&
2151 put_user(-1, fence_state[i].out_fence_ptr))
2152 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2153 }
2154
2155 kfree(fence_state);
2156}
2157
d34f20d6
RC
2158int drm_mode_atomic_ioctl(struct drm_device *dev,
2159 void *data, struct drm_file *file_priv)
2160{
2161 struct drm_mode_atomic *arg = data;
2162 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2163 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2164 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2165 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2166 unsigned int copied_objs, copied_props;
2167 struct drm_atomic_state *state;
2168 struct drm_modeset_acquire_ctx ctx;
2169 struct drm_plane *plane;
7f5d6dac 2170 struct drm_out_fence_state *fence_state;
45723728 2171 unsigned plane_mask;
d34f20d6 2172 int ret = 0;
7f5d6dac 2173 unsigned int i, j, num_fences;
d34f20d6
RC
2174
2175 /* disallow for drivers not supporting atomic: */
2176 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2177 return -EINVAL;
2178
2179 /* disallow for userspace that has not enabled atomic cap (even
2180 * though this may be a bit overkill, since legacy userspace
2181 * wouldn't know how to call this ioctl)
2182 */
2183 if (!file_priv->atomic)
2184 return -EINVAL;
2185
2186 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2187 return -EINVAL;
2188
2189 if (arg->reserved)
2190 return -EINVAL;
2191
2192 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2193 !dev->mode_config.async_page_flip)
2194 return -EINVAL;
2195
2196 /* can't test and expect an event at the same time. */
2197 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2198 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2199 return -EINVAL;
2200
2201 drm_modeset_acquire_init(&ctx, 0);
2202
2203 state = drm_atomic_state_alloc(dev);
2204 if (!state)
2205 return -ENOMEM;
2206
2207 state->acquire_ctx = &ctx;
2208 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2209
2210retry:
45723728 2211 plane_mask = 0;
d34f20d6
RC
2212 copied_objs = 0;
2213 copied_props = 0;
7f5d6dac
ML
2214 fence_state = NULL;
2215 num_fences = 0;
d34f20d6
RC
2216
2217 for (i = 0; i < arg->count_objs; i++) {
2218 uint32_t obj_id, count_props;
2219 struct drm_mode_object *obj;
2220
2221 if (get_user(obj_id, objs_ptr + copied_objs)) {
2222 ret = -EFAULT;
ec9f932e 2223 goto out;
d34f20d6
RC
2224 }
2225
2226 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
b164d31f
DA
2227 if (!obj) {
2228 ret = -ENOENT;
2229 goto out;
2230 }
2231
2232 if (!obj->properties) {
020a218f 2233 drm_mode_object_put(obj);
d34f20d6 2234 ret = -ENOENT;
ec9f932e 2235 goto out;
d34f20d6
RC
2236 }
2237
d34f20d6 2238 if (get_user(count_props, count_props_ptr + copied_objs)) {
020a218f 2239 drm_mode_object_put(obj);
d34f20d6 2240 ret = -EFAULT;
ec9f932e 2241 goto out;
d34f20d6
RC
2242 }
2243
2244 copied_objs++;
2245
2246 for (j = 0; j < count_props; j++) {
2247 uint32_t prop_id;
2248 uint64_t prop_value;
2249 struct drm_property *prop;
2250
2251 if (get_user(prop_id, props_ptr + copied_props)) {
020a218f 2252 drm_mode_object_put(obj);
d34f20d6 2253 ret = -EFAULT;
ec9f932e 2254 goto out;
d34f20d6
RC
2255 }
2256
f92f053b 2257 prop = drm_mode_obj_find_prop_id(obj, prop_id);
d34f20d6 2258 if (!prop) {
020a218f 2259 drm_mode_object_put(obj);
d34f20d6 2260 ret = -ENOENT;
ec9f932e 2261 goto out;
d34f20d6
RC
2262 }
2263
42c5814c
GR
2264 if (copy_from_user(&prop_value,
2265 prop_values_ptr + copied_props,
2266 sizeof(prop_value))) {
020a218f 2267 drm_mode_object_put(obj);
d34f20d6 2268 ret = -EFAULT;
ec9f932e 2269 goto out;
d34f20d6
RC
2270 }
2271
2272 ret = atomic_set_prop(state, obj, prop, prop_value);
b164d31f 2273 if (ret) {
020a218f 2274 drm_mode_object_put(obj);
ec9f932e 2275 goto out;
b164d31f 2276 }
d34f20d6
RC
2277
2278 copied_props++;
2279 }
a9cc54ee 2280
c4749c9a
ML
2281 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2282 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
a9cc54ee
ML
2283 plane = obj_to_plane(obj);
2284 plane_mask |= (1 << drm_plane_index(plane));
2285 plane->old_fb = plane->fb;
2286 }
020a218f 2287 drm_mode_object_put(obj);
d34f20d6
RC
2288 }
2289
beaf5af4
GP
2290 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2291 &num_fences);
2292 if (ret)
2293 goto out;
d34f20d6
RC
2294
2295 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2296 ret = drm_atomic_check_only(state);
d34f20d6 2297 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
b837ba0a 2298 ret = drm_atomic_nonblocking_commit(state);
d34f20d6 2299 } else {
fceffb32
RC
2300 if (unlikely(drm_debug & DRM_UT_STATE))
2301 drm_atomic_print_state(state);
2302
d34f20d6
RC
2303 ret = drm_atomic_commit(state);
2304 }
2305
ec9f932e 2306out:
0f45c26f 2307 drm_atomic_clean_old_fb(dev, plane_mask, ret);
d34f20d6 2308
beaf5af4 2309 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
c4749c9a 2310
ec9f932e
ML
2311 if (ret == -EDEADLK) {
2312 drm_atomic_state_clear(state);
2313 drm_modeset_backoff(&ctx);
2314 goto retry;
2315 }
d34f20d6 2316
0853695c 2317 drm_atomic_state_put(state);
d34f20d6
RC
2318
2319 drm_modeset_drop_locks(&ctx);
2320 drm_modeset_acquire_fini(&ctx);
2321
2322 return ret;
d34f20d6 2323}