]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/drm_atomic.c
drm/atomic: add new drm_debug bit to dump atomic state
[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>
cc4ceb48 34
be35f94f
TR
35#include "drm_crtc_internal.h"
36
3b24f7d6
DV
37static void crtc_commit_free(struct kref *kref)
38{
39 struct drm_crtc_commit *commit =
40 container_of(kref, struct drm_crtc_commit, ref);
41
42 kfree(commit);
43}
44
45void drm_crtc_commit_put(struct drm_crtc_commit *commit)
46{
47 kref_put(&commit->ref, crtc_commit_free);
48}
49EXPORT_SYMBOL(drm_crtc_commit_put);
50
036ef573
ML
51/**
52 * drm_atomic_state_default_release -
53 * release memory initialized by drm_atomic_state_init
54 * @state: atomic state
55 *
56 * Free all the memory allocated by drm_atomic_state_init.
57 * This is useful for drivers that subclass the atomic state.
58 */
59void drm_atomic_state_default_release(struct drm_atomic_state *state)
cc4ceb48
DV
60{
61 kfree(state->connectors);
cc4ceb48 62 kfree(state->crtcs);
cc4ceb48 63 kfree(state->planes);
cc4ceb48 64}
036ef573 65EXPORT_SYMBOL(drm_atomic_state_default_release);
cc4ceb48
DV
66
67/**
036ef573 68 * drm_atomic_state_init - init new atomic state
cc4ceb48 69 * @dev: DRM device
036ef573 70 * @state: atomic state
cc4ceb48 71 *
036ef573
ML
72 * Default implementation for filling in a new atomic state.
73 * This is useful for drivers that subclass the atomic state.
cc4ceb48 74 */
036ef573
ML
75int
76drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
cc4ceb48 77{
0853695c
CW
78 kref_init(&state->ref);
79
d34f20d6
RC
80 /* TODO legacy paths should maybe do a better job about
81 * setting this appropriately?
82 */
83 state->allow_modeset = true;
84
cc4ceb48
DV
85 state->crtcs = kcalloc(dev->mode_config.num_crtc,
86 sizeof(*state->crtcs), GFP_KERNEL);
87 if (!state->crtcs)
88 goto fail;
cc4ceb48
DV
89 state->planes = kcalloc(dev->mode_config.num_total_plane,
90 sizeof(*state->planes), GFP_KERNEL);
91 if (!state->planes)
92 goto fail;
cc4ceb48
DV
93
94 state->dev = dev;
95
036ef573 96 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
cc4ceb48 97
036ef573 98 return 0;
cc4ceb48 99fail:
036ef573
ML
100 drm_atomic_state_default_release(state);
101 return -ENOMEM;
102}
103EXPORT_SYMBOL(drm_atomic_state_init);
104
105/**
106 * drm_atomic_state_alloc - allocate atomic state
107 * @dev: DRM device
108 *
109 * This allocates an empty atomic state to track updates.
110 */
111struct drm_atomic_state *
112drm_atomic_state_alloc(struct drm_device *dev)
113{
114 struct drm_mode_config *config = &dev->mode_config;
115 struct drm_atomic_state *state;
116
117 if (!config->funcs->atomic_state_alloc) {
118 state = kzalloc(sizeof(*state), GFP_KERNEL);
119 if (!state)
120 return NULL;
121 if (drm_atomic_state_init(dev, state) < 0) {
122 kfree(state);
123 return NULL;
124 }
125 return state;
126 }
cc4ceb48 127
036ef573 128 return config->funcs->atomic_state_alloc(dev);
cc4ceb48
DV
129}
130EXPORT_SYMBOL(drm_atomic_state_alloc);
131
132/**
036ef573 133 * drm_atomic_state_default_clear - clear base atomic state
cc4ceb48
DV
134 * @state: atomic state
135 *
036ef573
ML
136 * Default implementation for clearing atomic state.
137 * This is useful for drivers that subclass the atomic state.
cc4ceb48 138 */
036ef573 139void drm_atomic_state_default_clear(struct drm_atomic_state *state)
cc4ceb48
DV
140{
141 struct drm_device *dev = state->dev;
6f75cea6 142 struct drm_mode_config *config = &dev->mode_config;
cc4ceb48
DV
143 int i;
144
17a38d9c 145 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
cc4ceb48 146
f52b69f1 147 for (i = 0; i < state->num_connector; i++) {
63e83c1d 148 struct drm_connector *connector = state->connectors[i].ptr;
cc4ceb48
DV
149
150 if (!connector)
151 continue;
152
d2307dea 153 connector->funcs->atomic_destroy_state(connector,
63e83c1d
DV
154 state->connectors[i].state);
155 state->connectors[i].ptr = NULL;
156 state->connectors[i].state = NULL;
b164d31f 157 drm_connector_unreference(connector);
cc4ceb48
DV
158 }
159
6f75cea6 160 for (i = 0; i < config->num_crtc; i++) {
5d943aa6 161 struct drm_crtc *crtc = state->crtcs[i].ptr;
cc4ceb48
DV
162
163 if (!crtc)
164 continue;
165
166 crtc->funcs->atomic_destroy_state(crtc,
5d943aa6 167 state->crtcs[i].state);
3b24f7d6
DV
168
169 if (state->crtcs[i].commit) {
170 kfree(state->crtcs[i].commit->event);
171 state->crtcs[i].commit->event = NULL;
172 drm_crtc_commit_put(state->crtcs[i].commit);
173 }
174
175 state->crtcs[i].commit = NULL;
5d943aa6
DV
176 state->crtcs[i].ptr = NULL;
177 state->crtcs[i].state = NULL;
cc4ceb48
DV
178 }
179
6f75cea6 180 for (i = 0; i < config->num_total_plane; i++) {
b8b5342b 181 struct drm_plane *plane = state->planes[i].ptr;
cc4ceb48
DV
182
183 if (!plane)
184 continue;
185
186 plane->funcs->atomic_destroy_state(plane,
b8b5342b
DV
187 state->planes[i].state);
188 state->planes[i].ptr = NULL;
189 state->planes[i].state = NULL;
cc4ceb48
DV
190 }
191}
036ef573
ML
192EXPORT_SYMBOL(drm_atomic_state_default_clear);
193
194/**
195 * drm_atomic_state_clear - clear state object
196 * @state: atomic state
197 *
198 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
199 * all locks. So someone else could sneak in and change the current modeset
200 * configuration. Which means that all the state assembled in @state is no
201 * longer an atomic update to the current state, but to some arbitrary earlier
202 * state. Which could break assumptions the driver's ->atomic_check likely
203 * relies on.
204 *
205 * Hence we must clear all cached state and completely start over, using this
206 * function.
207 */
208void drm_atomic_state_clear(struct drm_atomic_state *state)
209{
210 struct drm_device *dev = state->dev;
211 struct drm_mode_config *config = &dev->mode_config;
212
213 if (config->funcs->atomic_state_clear)
214 config->funcs->atomic_state_clear(state);
215 else
216 drm_atomic_state_default_clear(state);
217}
cc4ceb48
DV
218EXPORT_SYMBOL(drm_atomic_state_clear);
219
220/**
0853695c
CW
221 * __drm_atomic_state_free - free all memory for an atomic state
222 * @ref: This atomic state to deallocate
cc4ceb48
DV
223 *
224 * This frees all memory associated with an atomic state, including all the
225 * per-object state for planes, crtcs and connectors.
226 */
0853695c 227void __drm_atomic_state_free(struct kref *ref)
cc4ceb48 228{
0853695c
CW
229 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
230 struct drm_mode_config *config = &state->dev->mode_config;
036ef573 231
cc4ceb48
DV
232 drm_atomic_state_clear(state);
233
17a38d9c 234 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
cc4ceb48 235
036ef573
ML
236 if (config->funcs->atomic_state_free) {
237 config->funcs->atomic_state_free(state);
238 } else {
239 drm_atomic_state_default_release(state);
240 kfree(state);
241 }
cc4ceb48 242}
0853695c 243EXPORT_SYMBOL(__drm_atomic_state_free);
cc4ceb48
DV
244
245/**
246 * drm_atomic_get_crtc_state - get crtc state
247 * @state: global atomic state object
248 * @crtc: crtc to get state object for
249 *
250 * This function returns the crtc state for the given crtc, allocating it if
251 * needed. It will also grab the relevant crtc lock to make sure that the state
252 * is consistent.
253 *
254 * Returns:
255 *
256 * Either the allocated state or the error code encoded into the pointer. When
257 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
258 * entire atomic sequence must be restarted. All other errors are fatal.
259 */
260struct drm_crtc_state *
261drm_atomic_get_crtc_state(struct drm_atomic_state *state,
262 struct drm_crtc *crtc)
263{
1b26a5e1 264 int ret, index = drm_crtc_index(crtc);
cc4ceb48
DV
265 struct drm_crtc_state *crtc_state;
266
7f4eaa89
ML
267 WARN_ON(!state->acquire_ctx);
268
1b26a5e1
ML
269 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
270 if (crtc_state)
271 return crtc_state;
cc4ceb48
DV
272
273 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
274 if (ret)
275 return ERR_PTR(ret);
276
277 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
278 if (!crtc_state)
279 return ERR_PTR(-ENOMEM);
280
5d943aa6
DV
281 state->crtcs[index].state = crtc_state;
282 state->crtcs[index].ptr = crtc;
cc4ceb48
DV
283 crtc_state->state = state;
284
fa3ab4c2
VS
285 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
286 crtc->base.id, crtc->name, crtc_state, state);
cc4ceb48
DV
287
288 return crtc_state;
289}
290EXPORT_SYMBOL(drm_atomic_get_crtc_state);
291
819364da
DS
292/**
293 * drm_atomic_set_mode_for_crtc - set mode for CRTC
294 * @state: the CRTC whose incoming state to update
295 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
296 *
297 * Set a mode (originating from the kernel) on the desired CRTC state. Does
298 * not change any other state properties, including enable, active, or
299 * mode_changed.
300 *
301 * RETURNS:
302 * Zero on success, error code on failure. Cannot return -EDEADLK.
303 */
304int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
305 struct drm_display_mode *mode)
306{
99cf4a29
DS
307 struct drm_mode_modeinfo umode;
308
819364da
DS
309 /* Early return for no change. */
310 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
311 return 0;
312
5f911905 313 drm_property_unreference_blob(state->mode_blob);
99cf4a29
DS
314 state->mode_blob = NULL;
315
819364da 316 if (mode) {
99cf4a29
DS
317 drm_mode_convert_to_umode(&umode, mode);
318 state->mode_blob =
319 drm_property_create_blob(state->crtc->dev,
320 sizeof(umode),
321 &umode);
322 if (IS_ERR(state->mode_blob))
323 return PTR_ERR(state->mode_blob);
324
819364da
DS
325 drm_mode_copy(&state->mode, mode);
326 state->enable = true;
327 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
328 mode->name, state);
329 } else {
330 memset(&state->mode, 0, sizeof(state->mode));
331 state->enable = false;
332 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
333 state);
334 }
335
336 return 0;
337}
338EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
339
955f3c33
DS
340/**
341 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
342 * @state: the CRTC whose incoming state to update
343 * @blob: pointer to blob property to use for mode
344 *
345 * Set a mode (originating from a blob property) on the desired CRTC state.
346 * This function will take a reference on the blob property for the CRTC state,
347 * and release the reference held on the state's existing mode property, if any
348 * was set.
349 *
350 * RETURNS:
351 * Zero on success, error code on failure. Cannot return -EDEADLK.
352 */
353int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
354 struct drm_property_blob *blob)
355{
356 if (blob == state->mode_blob)
357 return 0;
358
5f911905 359 drm_property_unreference_blob(state->mode_blob);
955f3c33
DS
360 state->mode_blob = NULL;
361
6709887c
TV
362 memset(&state->mode, 0, sizeof(state->mode));
363
955f3c33
DS
364 if (blob) {
365 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
366 drm_mode_convert_umode(&state->mode,
367 (const struct drm_mode_modeinfo *)
368 blob->data))
369 return -EINVAL;
370
371 state->mode_blob = drm_property_reference_blob(blob);
372 state->enable = true;
373 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
374 state->mode.name, state);
375 } else {
955f3c33
DS
376 state->enable = false;
377 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
378 state);
379 }
380
381 return 0;
382}
383EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
384
5488dc16
LL
385/**
386 * drm_atomic_replace_property_blob - replace a blob property
387 * @blob: a pointer to the member blob to be replaced
388 * @new_blob: the new blob to replace with
5488dc16
LL
389 * @replaced: whether the blob has been replaced
390 *
391 * RETURNS:
392 * Zero on success, error code on failure
393 */
394static void
395drm_atomic_replace_property_blob(struct drm_property_blob **blob,
396 struct drm_property_blob *new_blob,
397 bool *replaced)
398{
399 struct drm_property_blob *old_blob = *blob;
400
401 if (old_blob == new_blob)
402 return;
403
f35cbe6a 404 drm_property_unreference_blob(old_blob);
5488dc16
LL
405 if (new_blob)
406 drm_property_reference_blob(new_blob);
407 *blob = new_blob;
408 *replaced = true;
409
410 return;
411}
412
413static int
414drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
415 struct drm_property_blob **blob,
416 uint64_t blob_id,
417 ssize_t expected_size,
418 bool *replaced)
419{
420 struct drm_device *dev = crtc->dev;
421 struct drm_property_blob *new_blob = NULL;
422
423 if (blob_id != 0) {
424 new_blob = drm_property_lookup_blob(dev, blob_id);
425 if (new_blob == NULL)
426 return -EINVAL;
427 if (expected_size > 0 && expected_size != new_blob->length)
428 return -EINVAL;
429 }
430
431 drm_atomic_replace_property_blob(blob, new_blob, replaced);
432
433 return 0;
434}
435
40ecc694
RC
436/**
437 * drm_atomic_crtc_set_property - set property on CRTC
438 * @crtc: the drm CRTC to set a property on
439 * @state: the state object to update with the new property value
440 * @property: the property to set
441 * @val: the new property value
442 *
443 * Use this instead of calling crtc->atomic_set_property directly.
444 * This function handles generic/core properties and calls out to
445 * driver's ->atomic_set_property() for driver properties. To ensure
446 * consistent behavior you must call this function rather than the
447 * driver hook directly.
448 *
449 * RETURNS:
450 * Zero on success, error code on failure
451 */
452int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
453 struct drm_crtc_state *state, struct drm_property *property,
454 uint64_t val)
455{
eab3bbef
DV
456 struct drm_device *dev = crtc->dev;
457 struct drm_mode_config *config = &dev->mode_config;
5488dc16 458 bool replaced = false;
955f3c33 459 int ret;
eab3bbef 460
27798365 461 if (property == config->prop_active)
eab3bbef 462 state->active = val;
955f3c33
DS
463 else if (property == config->prop_mode_id) {
464 struct drm_property_blob *mode =
465 drm_property_lookup_blob(dev, val);
466 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
5f911905 467 drm_property_unreference_blob(mode);
955f3c33 468 return ret;
5488dc16
LL
469 } else if (property == config->degamma_lut_property) {
470 ret = drm_atomic_replace_property_blob_from_id(crtc,
471 &state->degamma_lut,
472 val,
473 -1,
474 &replaced);
add1fa75 475 state->color_mgmt_changed |= replaced;
5488dc16
LL
476 return ret;
477 } else if (property == config->ctm_property) {
478 ret = drm_atomic_replace_property_blob_from_id(crtc,
479 &state->ctm,
480 val,
481 sizeof(struct drm_color_ctm),
482 &replaced);
add1fa75 483 state->color_mgmt_changed |= replaced;
5488dc16
LL
484 return ret;
485 } else if (property == config->gamma_lut_property) {
486 ret = drm_atomic_replace_property_blob_from_id(crtc,
487 &state->gamma_lut,
488 val,
489 -1,
490 &replaced);
add1fa75 491 state->color_mgmt_changed |= replaced;
5488dc16
LL
492 return ret;
493 } else if (crtc->funcs->atomic_set_property)
40ecc694 494 return crtc->funcs->atomic_set_property(crtc, state, property, val);
27798365
DS
495 else
496 return -EINVAL;
497
498 return 0;
40ecc694
RC
499}
500EXPORT_SYMBOL(drm_atomic_crtc_set_property);
501
c0714fc9
DV
502/**
503 * drm_atomic_crtc_get_property - get property value from CRTC state
504 * @crtc: the drm CRTC to set a property on
505 * @state: the state object to get the property value from
506 * @property: the property to set
507 * @val: return location for the property value
508 *
ac9c9256
RC
509 * This function handles generic/core properties and calls out to
510 * driver's ->atomic_get_property() for driver properties. To ensure
511 * consistent behavior you must call this function rather than the
512 * driver hook directly.
c0714fc9
DV
513 *
514 * RETURNS:
515 * Zero on success, error code on failure
ac9c9256 516 */
bf22f3be
GT
517static int
518drm_atomic_crtc_get_property(struct drm_crtc *crtc,
ac9c9256
RC
519 const struct drm_crtc_state *state,
520 struct drm_property *property, uint64_t *val)
521{
8f164ce4
DS
522 struct drm_device *dev = crtc->dev;
523 struct drm_mode_config *config = &dev->mode_config;
524
525 if (property == config->prop_active)
526 *val = state->active;
955f3c33
DS
527 else if (property == config->prop_mode_id)
528 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
5488dc16
LL
529 else if (property == config->degamma_lut_property)
530 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
531 else if (property == config->ctm_property)
532 *val = (state->ctm) ? state->ctm->base.id : 0;
533 else if (property == config->gamma_lut_property)
534 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
8f164ce4 535 else if (crtc->funcs->atomic_get_property)
ac9c9256 536 return crtc->funcs->atomic_get_property(crtc, state, property, val);
8f164ce4
DS
537 else
538 return -EINVAL;
539
540 return 0;
ac9c9256 541}
ac9c9256 542
5e743737
RC
543/**
544 * drm_atomic_crtc_check - check crtc state
545 * @crtc: crtc to check
546 * @state: crtc state to check
547 *
548 * Provides core sanity checks for crtc state.
549 *
550 * RETURNS:
551 * Zero on success, error code on failure
552 */
553static int drm_atomic_crtc_check(struct drm_crtc *crtc,
554 struct drm_crtc_state *state)
555{
556 /* NOTE: we explicitly don't enforce constraints such as primary
557 * layer covering entire screen, since that is something we want
558 * to allow (on hw that supports it). For hw that does not, it
559 * should be checked in driver's crtc->atomic_check() vfunc.
560 *
561 * TODO: Add generic modeset state checks once we support those.
562 */
eab3bbef
DV
563
564 if (state->active && !state->enable) {
fa3ab4c2
VS
565 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
566 crtc->base.id, crtc->name);
eab3bbef
DV
567 return -EINVAL;
568 }
569
99cf4a29
DS
570 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
571 * as this is a kernel-internal detail that userspace should never
572 * be able to trigger. */
573 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
574 WARN_ON(state->enable && !state->mode_blob)) {
fa3ab4c2
VS
575 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
576 crtc->base.id, crtc->name);
99cf4a29
DS
577 return -EINVAL;
578 }
579
580 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
581 WARN_ON(!state->enable && state->mode_blob)) {
fa3ab4c2
VS
582 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
583 crtc->base.id, crtc->name);
99cf4a29
DS
584 return -EINVAL;
585 }
586
4cba6850
DV
587 /*
588 * Reject event generation for when a CRTC is off and stays off.
589 * It wouldn't be hard to implement this, but userspace has a track
590 * record of happily burning through 100% cpu (or worse, crash) when the
591 * display pipe is suspended. To avoid all that fun just reject updates
592 * that ask for events since likely that indicates a bug in the
593 * compositor's drawing loop. This is consistent with the vblank IOCTL
594 * and legacy page_flip IOCTL which also reject service on a disabled
595 * pipe.
596 */
597 if (state->event && !state->active && !crtc->state->active) {
598 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
599 crtc->base.id);
600 return -EINVAL;
601 }
602
5e743737
RC
603 return 0;
604}
605
fceffb32
RC
606static void drm_atomic_crtc_print_state(struct drm_printer *p,
607 const struct drm_crtc_state *state)
608{
609 struct drm_crtc *crtc = state->crtc;
610
611 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
612 drm_printf(p, "\tenable=%d\n", state->enable);
613 drm_printf(p, "\tactive=%d\n", state->active);
614 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
615 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
616 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
617 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
618 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
619 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
620 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
621 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
622 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
623
624 if (crtc->funcs->atomic_print_state)
625 crtc->funcs->atomic_print_state(p, state);
626}
627
cc4ceb48
DV
628/**
629 * drm_atomic_get_plane_state - get plane state
630 * @state: global atomic state object
631 * @plane: plane to get state object for
632 *
633 * This function returns the plane state for the given plane, allocating it if
634 * needed. It will also grab the relevant plane lock to make sure that the state
635 * is consistent.
636 *
637 * Returns:
638 *
639 * Either the allocated state or the error code encoded into the pointer. When
640 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
641 * entire atomic sequence must be restarted. All other errors are fatal.
642 */
643struct drm_plane_state *
644drm_atomic_get_plane_state(struct drm_atomic_state *state,
645 struct drm_plane *plane)
646{
1b26a5e1 647 int ret, index = drm_plane_index(plane);
cc4ceb48
DV
648 struct drm_plane_state *plane_state;
649
7f4eaa89
ML
650 WARN_ON(!state->acquire_ctx);
651
1b26a5e1
ML
652 plane_state = drm_atomic_get_existing_plane_state(state, plane);
653 if (plane_state)
654 return plane_state;
cc4ceb48 655
4d02e2de 656 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
cc4ceb48
DV
657 if (ret)
658 return ERR_PTR(ret);
659
660 plane_state = plane->funcs->atomic_duplicate_state(plane);
661 if (!plane_state)
662 return ERR_PTR(-ENOMEM);
663
b8b5342b
DV
664 state->planes[index].state = plane_state;
665 state->planes[index].ptr = plane;
cc4ceb48
DV
666 plane_state->state = state;
667
9f4c97a2
VS
668 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
669 plane->base.id, plane->name, plane_state, state);
cc4ceb48
DV
670
671 if (plane_state->crtc) {
672 struct drm_crtc_state *crtc_state;
673
674 crtc_state = drm_atomic_get_crtc_state(state,
675 plane_state->crtc);
676 if (IS_ERR(crtc_state))
677 return ERR_CAST(crtc_state);
678 }
679
680 return plane_state;
681}
682EXPORT_SYMBOL(drm_atomic_get_plane_state);
683
40ecc694
RC
684/**
685 * drm_atomic_plane_set_property - set property on plane
686 * @plane: the drm plane to set a property on
687 * @state: the state object to update with the new property value
688 * @property: the property to set
689 * @val: the new property value
690 *
691 * Use this instead of calling plane->atomic_set_property directly.
692 * This function handles generic/core properties and calls out to
693 * driver's ->atomic_set_property() for driver properties. To ensure
694 * consistent behavior you must call this function rather than the
695 * driver hook directly.
696 *
697 * RETURNS:
698 * Zero on success, error code on failure
699 */
700int drm_atomic_plane_set_property(struct drm_plane *plane,
701 struct drm_plane_state *state, struct drm_property *property,
702 uint64_t val)
703{
6b4959f4
RC
704 struct drm_device *dev = plane->dev;
705 struct drm_mode_config *config = &dev->mode_config;
706
707 if (property == config->prop_fb_id) {
708 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
709 drm_atomic_set_fb_for_plane(state, fb);
710 if (fb)
711 drm_framebuffer_unreference(fb);
712 } else if (property == config->prop_crtc_id) {
713 struct drm_crtc *crtc = drm_crtc_find(dev, val);
714 return drm_atomic_set_crtc_for_plane(state, crtc);
715 } else if (property == config->prop_crtc_x) {
716 state->crtc_x = U642I64(val);
717 } else if (property == config->prop_crtc_y) {
718 state->crtc_y = U642I64(val);
719 } else if (property == config->prop_crtc_w) {
720 state->crtc_w = val;
721 } else if (property == config->prop_crtc_h) {
722 state->crtc_h = val;
723 } else if (property == config->prop_src_x) {
724 state->src_x = val;
725 } else if (property == config->prop_src_y) {
726 state->src_y = val;
727 } else if (property == config->prop_src_w) {
728 state->src_w = val;
729 } else if (property == config->prop_src_h) {
730 state->src_h = val;
6686df8c 731 } else if (property == plane->rotation_property) {
6e0c7c33
VS
732 if (!is_power_of_2(val & DRM_ROTATE_MASK))
733 return -EINVAL;
1da30627 734 state->rotation = val;
44d1240d
MS
735 } else if (property == plane->zpos_property) {
736 state->zpos = val;
6b4959f4
RC
737 } else if (plane->funcs->atomic_set_property) {
738 return plane->funcs->atomic_set_property(plane, state,
739 property, val);
740 } else {
741 return -EINVAL;
742 }
743
744 return 0;
40ecc694
RC
745}
746EXPORT_SYMBOL(drm_atomic_plane_set_property);
747
c0714fc9
DV
748/**
749 * drm_atomic_plane_get_property - get property value from plane state
750 * @plane: the drm plane to set a property on
751 * @state: the state object to get the property value from
752 * @property: the property to set
753 * @val: return location for the property value
754 *
ac9c9256
RC
755 * This function handles generic/core properties and calls out to
756 * driver's ->atomic_get_property() for driver properties. To ensure
757 * consistent behavior you must call this function rather than the
758 * driver hook directly.
c0714fc9
DV
759 *
760 * RETURNS:
761 * Zero on success, error code on failure
ac9c9256 762 */
a97df1cc
DV
763static int
764drm_atomic_plane_get_property(struct drm_plane *plane,
ac9c9256
RC
765 const struct drm_plane_state *state,
766 struct drm_property *property, uint64_t *val)
767{
6b4959f4
RC
768 struct drm_device *dev = plane->dev;
769 struct drm_mode_config *config = &dev->mode_config;
770
771 if (property == config->prop_fb_id) {
772 *val = (state->fb) ? state->fb->base.id : 0;
773 } else if (property == config->prop_crtc_id) {
774 *val = (state->crtc) ? state->crtc->base.id : 0;
775 } else if (property == config->prop_crtc_x) {
776 *val = I642U64(state->crtc_x);
777 } else if (property == config->prop_crtc_y) {
778 *val = I642U64(state->crtc_y);
779 } else if (property == config->prop_crtc_w) {
780 *val = state->crtc_w;
781 } else if (property == config->prop_crtc_h) {
782 *val = state->crtc_h;
783 } else if (property == config->prop_src_x) {
784 *val = state->src_x;
785 } else if (property == config->prop_src_y) {
786 *val = state->src_y;
787 } else if (property == config->prop_src_w) {
788 *val = state->src_w;
789 } else if (property == config->prop_src_h) {
790 *val = state->src_h;
6686df8c 791 } else if (property == plane->rotation_property) {
4cda09ca 792 *val = state->rotation;
44d1240d
MS
793 } else if (property == plane->zpos_property) {
794 *val = state->zpos;
6b4959f4 795 } else if (plane->funcs->atomic_get_property) {
ac9c9256 796 return plane->funcs->atomic_get_property(plane, state, property, val);
6b4959f4
RC
797 } else {
798 return -EINVAL;
799 }
800
801 return 0;
ac9c9256 802}
ac9c9256 803
f8aeb41c
DV
804static bool
805plane_switching_crtc(struct drm_atomic_state *state,
806 struct drm_plane *plane,
807 struct drm_plane_state *plane_state)
808{
809 if (!plane->state->crtc || !plane_state->crtc)
810 return false;
811
812 if (plane->state->crtc == plane_state->crtc)
813 return false;
814
815 /* This could be refined, but currently there's no helper or driver code
816 * to implement direct switching of active planes nor userspace to take
817 * advantage of more direct plane switching without the intermediate
818 * full OFF state.
819 */
820 return true;
821}
822
5e743737
RC
823/**
824 * drm_atomic_plane_check - check plane state
825 * @plane: plane to check
826 * @state: plane state to check
827 *
828 * Provides core sanity checks for plane state.
829 *
830 * RETURNS:
831 * Zero on success, error code on failure
832 */
833static int drm_atomic_plane_check(struct drm_plane *plane,
834 struct drm_plane_state *state)
835{
836 unsigned int fb_width, fb_height;
ead8610d 837 int ret;
5e743737
RC
838
839 /* either *both* CRTC and FB must be set, or neither */
840 if (WARN_ON(state->crtc && !state->fb)) {
17a38d9c 841 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
5e743737
RC
842 return -EINVAL;
843 } else if (WARN_ON(state->fb && !state->crtc)) {
17a38d9c 844 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
5e743737
RC
845 return -EINVAL;
846 }
847
848 /* if disabled, we don't care about the rest of the state: */
849 if (!state->crtc)
850 return 0;
851
852 /* Check whether this plane is usable on this CRTC */
853 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
17a38d9c 854 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
5e743737
RC
855 return -EINVAL;
856 }
857
858 /* Check whether this plane supports the fb pixel format. */
ead8610d
LP
859 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
860 if (ret) {
d3828147 861 char *format_name = drm_get_format_name(state->fb->pixel_format);
90844f00
EE
862 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", format_name);
863 kfree(format_name);
ead8610d 864 return ret;
5e743737
RC
865 }
866
867 /* Give drivers some help against integer overflows */
868 if (state->crtc_w > INT_MAX ||
869 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
870 state->crtc_h > INT_MAX ||
871 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
17a38d9c
DV
872 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
873 state->crtc_w, state->crtc_h,
874 state->crtc_x, state->crtc_y);
5e743737
RC
875 return -ERANGE;
876 }
877
878 fb_width = state->fb->width << 16;
879 fb_height = state->fb->height << 16;
880
881 /* Make sure source coordinates are inside the fb. */
882 if (state->src_w > fb_width ||
883 state->src_x > fb_width - state->src_w ||
884 state->src_h > fb_height ||
885 state->src_y > fb_height - state->src_h) {
17a38d9c
DV
886 DRM_DEBUG_ATOMIC("Invalid source coordinates "
887 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
888 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
889 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
890 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
891 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
5e743737
RC
892 return -ENOSPC;
893 }
894
f8aeb41c 895 if (plane_switching_crtc(state->state, plane, state)) {
9f4c97a2
VS
896 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
897 plane->base.id, plane->name);
f8aeb41c
DV
898 return -EINVAL;
899 }
900
5e743737
RC
901 return 0;
902}
903
fceffb32
RC
904static void drm_atomic_plane_print_state(struct drm_printer *p,
905 const struct drm_plane_state *state)
906{
907 struct drm_plane *plane = state->plane;
908 struct drm_rect src = drm_plane_state_src(state);
909 struct drm_rect dest = drm_plane_state_dest(state);
910
911 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
912 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
913 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
914 if (state->fb) {
915 struct drm_framebuffer *fb = state->fb;
916 int i, n = drm_format_num_planes(fb->pixel_format);
917
918 drm_printf(p, "\t\tformat=%s\n",
919 drm_get_format_name(fb->pixel_format));
920 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
921 drm_printf(p, "\t\tlayers:\n");
922 for (i = 0; i < n; i++) {
923 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
924 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
925 drm_printf(p, "\t\t\tmodifier[%d]=0x%llx\n", i, fb->modifier[i]);
926 }
927 }
928 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
929 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
930 drm_printf(p, "\trotation=%x\n", state->rotation);
931
932 if (plane->funcs->atomic_print_state)
933 plane->funcs->atomic_print_state(p, state);
934}
935
cc4ceb48
DV
936/**
937 * drm_atomic_get_connector_state - get connector state
938 * @state: global atomic state object
939 * @connector: connector to get state object for
940 *
941 * This function returns the connector state for the given connector,
942 * allocating it if needed. It will also grab the relevant connector lock to
943 * make sure that the state is consistent.
944 *
945 * Returns:
946 *
947 * Either the allocated state or the error code encoded into the pointer. When
948 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
949 * entire atomic sequence must be restarted. All other errors are fatal.
950 */
951struct drm_connector_state *
952drm_atomic_get_connector_state(struct drm_atomic_state *state,
953 struct drm_connector *connector)
954{
955 int ret, index;
956 struct drm_mode_config *config = &connector->dev->mode_config;
957 struct drm_connector_state *connector_state;
958
7f4eaa89
ML
959 WARN_ON(!state->acquire_ctx);
960
c7eb76f4
DV
961 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
962 if (ret)
963 return ERR_PTR(ret);
964
cc4ceb48
DV
965 index = drm_connector_index(connector);
966
f52b69f1 967 if (index >= state->num_connector) {
63e83c1d 968 struct __drm_connnectors_state *c;
5fff80bb
ML
969 int alloc = max(index + 1, config->num_connector);
970
971 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
972 if (!c)
973 return ERR_PTR(-ENOMEM);
974
975 state->connectors = c;
976 memset(&state->connectors[state->num_connector], 0,
977 sizeof(*state->connectors) * (alloc - state->num_connector));
978
5fff80bb 979 state->num_connector = alloc;
f52b69f1
DV
980 }
981
63e83c1d
DV
982 if (state->connectors[index].state)
983 return state->connectors[index].state;
cc4ceb48 984
cc4ceb48
DV
985 connector_state = connector->funcs->atomic_duplicate_state(connector);
986 if (!connector_state)
987 return ERR_PTR(-ENOMEM);
988
b164d31f 989 drm_connector_reference(connector);
63e83c1d
DV
990 state->connectors[index].state = connector_state;
991 state->connectors[index].ptr = connector;
cc4ceb48
DV
992 connector_state->state = state;
993
17a38d9c
DV
994 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
995 connector->base.id, connector_state, state);
cc4ceb48
DV
996
997 if (connector_state->crtc) {
998 struct drm_crtc_state *crtc_state;
999
1000 crtc_state = drm_atomic_get_crtc_state(state,
1001 connector_state->crtc);
1002 if (IS_ERR(crtc_state))
1003 return ERR_CAST(crtc_state);
1004 }
1005
1006 return connector_state;
1007}
1008EXPORT_SYMBOL(drm_atomic_get_connector_state);
1009
40ecc694
RC
1010/**
1011 * drm_atomic_connector_set_property - set property on connector.
1012 * @connector: the drm connector to set a property on
1013 * @state: the state object to update with the new property value
1014 * @property: the property to set
1015 * @val: the new property value
1016 *
1017 * Use this instead of calling connector->atomic_set_property directly.
1018 * This function handles generic/core properties and calls out to
1019 * driver's ->atomic_set_property() for driver properties. To ensure
1020 * consistent behavior you must call this function rather than the
1021 * driver hook directly.
1022 *
1023 * RETURNS:
1024 * Zero on success, error code on failure
1025 */
1026int drm_atomic_connector_set_property(struct drm_connector *connector,
1027 struct drm_connector_state *state, struct drm_property *property,
1028 uint64_t val)
1029{
1030 struct drm_device *dev = connector->dev;
1031 struct drm_mode_config *config = &dev->mode_config;
1032
ae16c597
RC
1033 if (property == config->prop_crtc_id) {
1034 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1035 return drm_atomic_set_crtc_for_connector(state, crtc);
1036 } else if (property == config->dpms_property) {
40ecc694
RC
1037 /* setting DPMS property requires special handling, which
1038 * is done in legacy setprop path for us. Disallow (for
1039 * now?) atomic writes to DPMS property:
1040 */
1041 return -EINVAL;
1042 } else if (connector->funcs->atomic_set_property) {
1043 return connector->funcs->atomic_set_property(connector,
1044 state, property, val);
1045 } else {
1046 return -EINVAL;
1047 }
1048}
1049EXPORT_SYMBOL(drm_atomic_connector_set_property);
1050
fceffb32
RC
1051static void drm_atomic_connector_print_state(struct drm_printer *p,
1052 const struct drm_connector_state *state)
1053{
1054 struct drm_connector *connector = state->connector;
1055
1056 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1057 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1058
1059 if (connector->funcs->atomic_print_state)
1060 connector->funcs->atomic_print_state(p, state);
1061}
1062
c0714fc9
DV
1063/**
1064 * drm_atomic_connector_get_property - get property value from connector state
1065 * @connector: the drm connector to set a property on
1066 * @state: the state object to get the property value from
1067 * @property: the property to set
1068 * @val: return location for the property value
1069 *
ac9c9256
RC
1070 * This function handles generic/core properties and calls out to
1071 * driver's ->atomic_get_property() for driver properties. To ensure
1072 * consistent behavior you must call this function rather than the
1073 * driver hook directly.
c0714fc9
DV
1074 *
1075 * RETURNS:
1076 * Zero on success, error code on failure
ac9c9256 1077 */
a97df1cc
DV
1078static int
1079drm_atomic_connector_get_property(struct drm_connector *connector,
ac9c9256
RC
1080 const struct drm_connector_state *state,
1081 struct drm_property *property, uint64_t *val)
1082{
1083 struct drm_device *dev = connector->dev;
1084 struct drm_mode_config *config = &dev->mode_config;
1085
ae16c597
RC
1086 if (property == config->prop_crtc_id) {
1087 *val = (state->crtc) ? state->crtc->base.id : 0;
1088 } else if (property == config->dpms_property) {
ac9c9256
RC
1089 *val = connector->dpms;
1090 } else if (connector->funcs->atomic_get_property) {
1091 return connector->funcs->atomic_get_property(connector,
1092 state, property, val);
1093 } else {
1094 return -EINVAL;
1095 }
1096
1097 return 0;
1098}
ac9c9256 1099
88a48e29
RC
1100int drm_atomic_get_property(struct drm_mode_object *obj,
1101 struct drm_property *property, uint64_t *val)
1102{
1103 struct drm_device *dev = property->dev;
1104 int ret;
1105
1106 switch (obj->type) {
1107 case DRM_MODE_OBJECT_CONNECTOR: {
1108 struct drm_connector *connector = obj_to_connector(obj);
1109 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1110 ret = drm_atomic_connector_get_property(connector,
1111 connector->state, property, val);
1112 break;
1113 }
1114 case DRM_MODE_OBJECT_CRTC: {
1115 struct drm_crtc *crtc = obj_to_crtc(obj);
1116 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1117 ret = drm_atomic_crtc_get_property(crtc,
1118 crtc->state, property, val);
1119 break;
1120 }
1121 case DRM_MODE_OBJECT_PLANE: {
1122 struct drm_plane *plane = obj_to_plane(obj);
1123 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1124 ret = drm_atomic_plane_get_property(plane,
1125 plane->state, property, val);
1126 break;
1127 }
1128 default:
1129 ret = -EINVAL;
1130 break;
1131 }
1132
1133 return ret;
1134}
1135
cc4ceb48
DV
1136/**
1137 * drm_atomic_set_crtc_for_plane - set crtc for plane
07cc0ef6 1138 * @plane_state: the plane whose incoming state to update
cc4ceb48
DV
1139 * @crtc: crtc to use for the plane
1140 *
1141 * Changing the assigned crtc for a plane requires us to grab the lock and state
1142 * for the new crtc, as needed. This function takes care of all these details
1143 * besides updating the pointer in the state object itself.
1144 *
1145 * Returns:
1146 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1147 * then the w/w mutex code has detected a deadlock and the entire atomic
1148 * sequence must be restarted. All other errors are fatal.
1149 */
1150int
07cc0ef6
DV
1151drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1152 struct drm_crtc *crtc)
cc4ceb48 1153{
07cc0ef6 1154 struct drm_plane *plane = plane_state->plane;
cc4ceb48
DV
1155 struct drm_crtc_state *crtc_state;
1156
6ddd388a
RC
1157 if (plane_state->crtc) {
1158 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1159 plane_state->crtc);
1160 if (WARN_ON(IS_ERR(crtc_state)))
1161 return PTR_ERR(crtc_state);
1162
1163 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1164 }
1165
1166 plane_state->crtc = crtc;
1167
cc4ceb48
DV
1168 if (crtc) {
1169 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1170 crtc);
1171 if (IS_ERR(crtc_state))
1172 return PTR_ERR(crtc_state);
6ddd388a 1173 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
cc4ceb48
DV
1174 }
1175
cc4ceb48 1176 if (crtc)
fa3ab4c2
VS
1177 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1178 plane_state, crtc->base.id, crtc->name);
cc4ceb48 1179 else
17a38d9c
DV
1180 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1181 plane_state);
cc4ceb48
DV
1182
1183 return 0;
1184}
1185EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1186
321ebf04 1187/**
16d78bc2 1188 * drm_atomic_set_fb_for_plane - set framebuffer for plane
321ebf04
DV
1189 * @plane_state: atomic state object for the plane
1190 * @fb: fb to use for the plane
1191 *
1192 * Changing the assigned framebuffer for a plane requires us to grab a reference
1193 * to the new fb and drop the reference to the old fb, if there is one. This
1194 * function takes care of all these details besides updating the pointer in the
1195 * state object itself.
1196 */
1197void
1198drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1199 struct drm_framebuffer *fb)
1200{
1201 if (plane_state->fb)
1202 drm_framebuffer_unreference(plane_state->fb);
1203 if (fb)
1204 drm_framebuffer_reference(fb);
1205 plane_state->fb = fb;
1206
1207 if (fb)
17a38d9c
DV
1208 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1209 fb->base.id, plane_state);
321ebf04 1210 else
17a38d9c
DV
1211 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1212 plane_state);
321ebf04
DV
1213}
1214EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1215
13b55664
GP
1216/**
1217 * drm_atomic_set_fence_for_plane - set fence for plane
1218 * @plane_state: atomic state object for the plane
1219 * @fence: dma_fence to use for the plane
1220 *
1221 * Helper to setup the plane_state fence in case it is not set yet.
1222 * By using this drivers doesn't need to worry if the user choose
1223 * implicit or explicit fencing.
1224 *
1225 * This function will not set the fence to the state if it was set
1226 * via explicit fencing interfaces on the atomic ioctl. It will
1227 * all drope the reference to the fence as we not storing it
1228 * anywhere.
1229 *
1230 * Otherwise, if plane_state->fence is not set this function we
1231 * just set it with the received implict fence.
1232 */
1233void
1234drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1235 struct dma_fence *fence)
1236{
1237 if (plane_state->fence) {
1238 dma_fence_put(fence);
1239 return;
1240 }
1241
1242 plane_state->fence = fence;
1243}
1244EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1245
cc4ceb48
DV
1246/**
1247 * drm_atomic_set_crtc_for_connector - set crtc for connector
1248 * @conn_state: atomic state object for the connector
1249 * @crtc: crtc to use for the connector
1250 *
1251 * Changing the assigned crtc for a connector requires us to grab the lock and
1252 * state for the new crtc, as needed. This function takes care of all these
1253 * details besides updating the pointer in the state object itself.
1254 *
1255 * Returns:
1256 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1257 * then the w/w mutex code has detected a deadlock and the entire atomic
1258 * sequence must be restarted. All other errors are fatal.
1259 */
1260int
1261drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1262 struct drm_crtc *crtc)
1263{
1264 struct drm_crtc_state *crtc_state;
1265
e2d800a3
CW
1266 if (conn_state->crtc == crtc)
1267 return 0;
1268
1269 if (conn_state->crtc) {
4cd9fa52
ML
1270 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1271 conn_state->crtc);
1272
1273 crtc_state->connector_mask &=
1274 ~(1 << drm_connector_index(conn_state->connector));
e2d800a3
CW
1275
1276 drm_connector_unreference(conn_state->connector);
1277 conn_state->crtc = NULL;
4cd9fa52
ML
1278 }
1279
cc4ceb48
DV
1280 if (crtc) {
1281 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1282 if (IS_ERR(crtc_state))
1283 return PTR_ERR(crtc_state);
4cd9fa52
ML
1284
1285 crtc_state->connector_mask |=
1286 1 << drm_connector_index(conn_state->connector);
cc4ceb48 1287
e2d800a3
CW
1288 drm_connector_reference(conn_state->connector);
1289 conn_state->crtc = crtc;
cc4ceb48 1290
fa3ab4c2
VS
1291 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1292 conn_state, crtc->base.id, crtc->name);
e2d800a3 1293 } else {
17a38d9c
DV
1294 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1295 conn_state);
e2d800a3 1296 }
cc4ceb48
DV
1297
1298 return 0;
1299}
1300EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1301
1302/**
1303 * drm_atomic_add_affected_connectors - add connectors for crtc
1304 * @state: atomic state
1305 * @crtc: DRM crtc
1306 *
1307 * This function walks the current configuration and adds all connectors
1308 * currently using @crtc to the atomic configuration @state. Note that this
1309 * function must acquire the connection mutex. This can potentially cause
1310 * unneeded seralization if the update is just for the planes on one crtc. Hence
1311 * drivers and helpers should only call this when really needed (e.g. when a
1312 * full modeset needs to happen due to some change).
1313 *
1314 * Returns:
1315 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1316 * then the w/w mutex code has detected a deadlock and the entire atomic
1317 * sequence must be restarted. All other errors are fatal.
1318 */
1319int
1320drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1321 struct drm_crtc *crtc)
1322{
1323 struct drm_mode_config *config = &state->dev->mode_config;
1324 struct drm_connector *connector;
1325 struct drm_connector_state *conn_state;
1326 int ret;
1327
1328 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1329 if (ret)
1330 return ret;
1331
fa3ab4c2
VS
1332 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1333 crtc->base.id, crtc->name, state);
cc4ceb48
DV
1334
1335 /*
1336 * Changed connectors are already in @state, so only need to look at the
1337 * current configuration.
1338 */
9a9f5ce8 1339 drm_for_each_connector(connector, state->dev) {
cc4ceb48
DV
1340 if (connector->state->crtc != crtc)
1341 continue;
1342
1343 conn_state = drm_atomic_get_connector_state(state, connector);
1344 if (IS_ERR(conn_state))
1345 return PTR_ERR(conn_state);
1346 }
1347
1348 return 0;
1349}
1350EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1351
e01e9f75
ML
1352/**
1353 * drm_atomic_add_affected_planes - add planes for crtc
1354 * @state: atomic state
1355 * @crtc: DRM crtc
1356 *
1357 * This function walks the current configuration and adds all planes
1358 * currently used by @crtc to the atomic configuration @state. This is useful
1359 * when an atomic commit also needs to check all currently enabled plane on
1360 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1361 * to avoid special code to force-enable all planes.
1362 *
1363 * Since acquiring a plane state will always also acquire the w/w mutex of the
1364 * current CRTC for that plane (if there is any) adding all the plane states for
1365 * a CRTC will not reduce parallism of atomic updates.
1366 *
1367 * Returns:
1368 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1369 * then the w/w mutex code has detected a deadlock and the entire atomic
1370 * sequence must be restarted. All other errors are fatal.
1371 */
1372int
1373drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1374 struct drm_crtc *crtc)
1375{
1376 struct drm_plane *plane;
1377
1378 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1379
1380 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1381 struct drm_plane_state *plane_state =
1382 drm_atomic_get_plane_state(state, plane);
1383
1384 if (IS_ERR(plane_state))
1385 return PTR_ERR(plane_state);
1386 }
1387 return 0;
1388}
1389EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1390
cc4ceb48
DV
1391/**
1392 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1393 * @state: atomic state
1394 *
1395 * This function should be used by legacy entry points which don't understand
1396 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
16d78bc2 1397 * the slowpath completed.
cc4ceb48
DV
1398 */
1399void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1400{
81e257e9
ML
1401 struct drm_device *dev = state->dev;
1402 unsigned crtc_mask = 0;
1403 struct drm_crtc *crtc;
cc4ceb48 1404 int ret;
81e257e9
ML
1405 bool global = false;
1406
1407 drm_for_each_crtc(crtc, dev) {
1408 if (crtc->acquire_ctx != state->acquire_ctx)
1409 continue;
1410
1411 crtc_mask |= drm_crtc_mask(crtc);
1412 crtc->acquire_ctx = NULL;
1413 }
1414
1415 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1416 global = true;
1417
1418 dev->mode_config.acquire_ctx = NULL;
1419 }
cc4ceb48
DV
1420
1421retry:
1422 drm_modeset_backoff(state->acquire_ctx);
1423
81e257e9 1424 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
cc4ceb48
DV
1425 if (ret)
1426 goto retry;
81e257e9
ML
1427
1428 drm_for_each_crtc(crtc, dev)
1429 if (drm_crtc_mask(crtc) & crtc_mask)
1430 crtc->acquire_ctx = state->acquire_ctx;
1431
1432 if (global)
1433 dev->mode_config.acquire_ctx = state->acquire_ctx;
cc4ceb48
DV
1434}
1435EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1436
1437/**
1438 * drm_atomic_check_only - check whether a given config would work
1439 * @state: atomic configuration to check
1440 *
1441 * Note that this function can return -EDEADLK if the driver needed to acquire
1442 * more locks but encountered a deadlock. The caller must then do the usual w/w
1443 * backoff dance and restart. All other errors are fatal.
1444 *
1445 * Returns:
1446 * 0 on success, negative error code on failure.
1447 */
1448int drm_atomic_check_only(struct drm_atomic_state *state)
1449{
5e743737
RC
1450 struct drm_device *dev = state->dev;
1451 struct drm_mode_config *config = &dev->mode_config;
df63b999
ACO
1452 struct drm_plane *plane;
1453 struct drm_plane_state *plane_state;
1454 struct drm_crtc *crtc;
1455 struct drm_crtc_state *crtc_state;
5e743737 1456 int i, ret = 0;
cc4ceb48 1457
17a38d9c 1458 DRM_DEBUG_ATOMIC("checking %p\n", state);
cc4ceb48 1459
df63b999
ACO
1460 for_each_plane_in_state(state, plane, plane_state, i) {
1461 ret = drm_atomic_plane_check(plane, plane_state);
5e743737 1462 if (ret) {
9f4c97a2
VS
1463 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1464 plane->base.id, plane->name);
5e743737
RC
1465 return ret;
1466 }
1467 }
1468
df63b999
ACO
1469 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1470 ret = drm_atomic_crtc_check(crtc, crtc_state);
5e743737 1471 if (ret) {
fa3ab4c2
VS
1472 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1473 crtc->base.id, crtc->name);
5e743737
RC
1474 return ret;
1475 }
1476 }
1477
cc4ceb48 1478 if (config->funcs->atomic_check)
5e743737
RC
1479 ret = config->funcs->atomic_check(state->dev, state);
1480
d34f20d6 1481 if (!state->allow_modeset) {
df63b999 1482 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2465ff62 1483 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
fa3ab4c2
VS
1484 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1485 crtc->base.id, crtc->name);
d34f20d6
RC
1486 return -EINVAL;
1487 }
1488 }
1489 }
1490
5e743737 1491 return ret;
cc4ceb48
DV
1492}
1493EXPORT_SYMBOL(drm_atomic_check_only);
1494
1495/**
1496 * drm_atomic_commit - commit configuration atomically
1497 * @state: atomic configuration to check
1498 *
1499 * Note that this function can return -EDEADLK if the driver needed to acquire
1500 * more locks but encountered a deadlock. The caller must then do the usual w/w
1501 * backoff dance and restart. All other errors are fatal.
1502 *
1503 * Also note that on successful execution ownership of @state is transferred
1504 * from the caller of this function to the function itself. The caller must not
1505 * free or in any other way access @state. If the function fails then the caller
1506 * must clean up @state itself.
1507 *
1508 * Returns:
1509 * 0 on success, negative error code on failure.
1510 */
1511int drm_atomic_commit(struct drm_atomic_state *state)
1512{
1513 struct drm_mode_config *config = &state->dev->mode_config;
1514 int ret;
1515
1516 ret = drm_atomic_check_only(state);
1517 if (ret)
1518 return ret;
1519
17a38d9c 1520 DRM_DEBUG_ATOMIC("commiting %p\n", state);
cc4ceb48
DV
1521
1522 return config->funcs->atomic_commit(state->dev, state, false);
1523}
1524EXPORT_SYMBOL(drm_atomic_commit);
1525
1526/**
b837ba0a 1527 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
cc4ceb48
DV
1528 * @state: atomic configuration to check
1529 *
1530 * Note that this function can return -EDEADLK if the driver needed to acquire
1531 * more locks but encountered a deadlock. The caller must then do the usual w/w
1532 * backoff dance and restart. All other errors are fatal.
1533 *
1534 * Also note that on successful execution ownership of @state is transferred
1535 * from the caller of this function to the function itself. The caller must not
1536 * free or in any other way access @state. If the function fails then the caller
1537 * must clean up @state itself.
1538 *
1539 * Returns:
1540 * 0 on success, negative error code on failure.
1541 */
b837ba0a 1542int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
cc4ceb48
DV
1543{
1544 struct drm_mode_config *config = &state->dev->mode_config;
1545 int ret;
1546
1547 ret = drm_atomic_check_only(state);
1548 if (ret)
1549 return ret;
1550
b837ba0a 1551 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
cc4ceb48
DV
1552
1553 return config->funcs->atomic_commit(state->dev, state, true);
1554}
b837ba0a 1555EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
d34f20d6 1556
fceffb32
RC
1557static void drm_atomic_print_state(const struct drm_atomic_state *state)
1558{
1559 struct drm_printer p = drm_info_printer(state->dev->dev);
1560 struct drm_plane *plane;
1561 struct drm_plane_state *plane_state;
1562 struct drm_crtc *crtc;
1563 struct drm_crtc_state *crtc_state;
1564 struct drm_connector *connector;
1565 struct drm_connector_state *connector_state;
1566 int i;
1567
1568 DRM_DEBUG_ATOMIC("checking %p\n", state);
1569
1570 for_each_plane_in_state(state, plane, plane_state, i)
1571 drm_atomic_plane_print_state(&p, plane_state);
1572
1573 for_each_crtc_in_state(state, crtc, crtc_state, i)
1574 drm_atomic_crtc_print_state(&p, crtc_state);
1575
1576 for_each_connector_in_state(state, connector, connector_state, i)
1577 drm_atomic_connector_print_state(&p, connector_state);
1578}
1579
d34f20d6
RC
1580/*
1581 * The big monstor ioctl
1582 */
1583
1584static struct drm_pending_vblank_event *create_vblank_event(
1b47aaf9 1585 struct drm_device *dev, struct drm_file *file_priv,
f54d1867 1586 struct dma_fence *fence, uint64_t user_data)
d34f20d6
RC
1587{
1588 struct drm_pending_vblank_event *e = NULL;
2dd500f1 1589 int ret;
d34f20d6
RC
1590
1591 e = kzalloc(sizeof *e, GFP_KERNEL);
2dd500f1
DV
1592 if (!e)
1593 return NULL;
d34f20d6
RC
1594
1595 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2dd500f1 1596 e->event.base.length = sizeof(e->event);
d34f20d6 1597 e->event.user_data = user_data;
d34f20d6 1598
1b47aaf9
GP
1599 if (file_priv) {
1600 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1601 &e->event.base);
1602 if (ret) {
1603 kfree(e);
1604 return NULL;
1605 }
2dd500f1 1606 }
d34f20d6 1607
1b47aaf9
GP
1608 e->base.fence = fence;
1609
2dd500f1 1610 return e;
d34f20d6
RC
1611}
1612
1613static int atomic_set_prop(struct drm_atomic_state *state,
1614 struct drm_mode_object *obj, struct drm_property *prop,
1615 uint64_t prop_value)
1616{
1617 struct drm_mode_object *ref;
1618 int ret;
1619
1620 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1621 return -EINVAL;
1622
1623 switch (obj->type) {
1624 case DRM_MODE_OBJECT_CONNECTOR: {
1625 struct drm_connector *connector = obj_to_connector(obj);
1626 struct drm_connector_state *connector_state;
1627
1628 connector_state = drm_atomic_get_connector_state(state, connector);
1629 if (IS_ERR(connector_state)) {
1630 ret = PTR_ERR(connector_state);
1631 break;
1632 }
1633
1634 ret = drm_atomic_connector_set_property(connector,
1635 connector_state, prop, prop_value);
1636 break;
1637 }
1638 case DRM_MODE_OBJECT_CRTC: {
1639 struct drm_crtc *crtc = obj_to_crtc(obj);
1640 struct drm_crtc_state *crtc_state;
1641
1642 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1643 if (IS_ERR(crtc_state)) {
1644 ret = PTR_ERR(crtc_state);
1645 break;
1646 }
1647
1648 ret = drm_atomic_crtc_set_property(crtc,
1649 crtc_state, prop, prop_value);
1650 break;
1651 }
1652 case DRM_MODE_OBJECT_PLANE: {
1653 struct drm_plane *plane = obj_to_plane(obj);
1654 struct drm_plane_state *plane_state;
1655
1656 plane_state = drm_atomic_get_plane_state(state, plane);
1657 if (IS_ERR(plane_state)) {
1658 ret = PTR_ERR(plane_state);
1659 break;
1660 }
1661
1662 ret = drm_atomic_plane_set_property(plane,
1663 plane_state, prop, prop_value);
1664 break;
1665 }
1666 default:
1667 ret = -EINVAL;
1668 break;
1669 }
1670
1671 drm_property_change_valid_put(prop, ref);
1672 return ret;
1673}
1674
0f45c26f 1675/**
9744bf41 1676 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
0f45c26f
ML
1677 *
1678 * @dev: drm device to check.
1679 * @plane_mask: plane mask for planes that were updated.
1680 * @ret: return value, can be -EDEADLK for a retry.
1681 *
1682 * Before doing an update plane->old_fb is set to plane->fb,
1683 * but before dropping the locks old_fb needs to be set to NULL
1684 * and plane->fb updated. This is a common operation for each
1685 * atomic update, so this call is split off as a helper.
1686 */
1687void drm_atomic_clean_old_fb(struct drm_device *dev,
1688 unsigned plane_mask,
1689 int ret)
1690{
1691 struct drm_plane *plane;
1692
1693 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1694 * locks (ie. while it is still safe to deref plane->state). We
1695 * need to do this here because the driver entry points cannot
1696 * distinguish between legacy and atomic ioctls.
1697 */
1698 drm_for_each_plane_mask(plane, dev, plane_mask) {
1699 if (ret == 0) {
1700 struct drm_framebuffer *new_fb = plane->state->fb;
1701 if (new_fb)
1702 drm_framebuffer_reference(new_fb);
1703 plane->fb = new_fb;
1704 plane->crtc = plane->state->crtc;
1705
1706 if (plane->old_fb)
1707 drm_framebuffer_unreference(plane->old_fb);
1708 }
1709 plane->old_fb = NULL;
1710 }
1711}
1712EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1713
d34f20d6
RC
1714int drm_mode_atomic_ioctl(struct drm_device *dev,
1715 void *data, struct drm_file *file_priv)
1716{
1717 struct drm_mode_atomic *arg = data;
1718 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1719 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1720 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1721 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1722 unsigned int copied_objs, copied_props;
1723 struct drm_atomic_state *state;
1724 struct drm_modeset_acquire_ctx ctx;
1725 struct drm_plane *plane;
df63b999
ACO
1726 struct drm_crtc *crtc;
1727 struct drm_crtc_state *crtc_state;
45723728 1728 unsigned plane_mask;
d34f20d6 1729 int ret = 0;
f92f053b 1730 unsigned int i, j;
d34f20d6
RC
1731
1732 /* disallow for drivers not supporting atomic: */
1733 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1734 return -EINVAL;
1735
1736 /* disallow for userspace that has not enabled atomic cap (even
1737 * though this may be a bit overkill, since legacy userspace
1738 * wouldn't know how to call this ioctl)
1739 */
1740 if (!file_priv->atomic)
1741 return -EINVAL;
1742
1743 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1744 return -EINVAL;
1745
1746 if (arg->reserved)
1747 return -EINVAL;
1748
1749 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1750 !dev->mode_config.async_page_flip)
1751 return -EINVAL;
1752
1753 /* can't test and expect an event at the same time. */
1754 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1755 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1756 return -EINVAL;
1757
1758 drm_modeset_acquire_init(&ctx, 0);
1759
1760 state = drm_atomic_state_alloc(dev);
1761 if (!state)
1762 return -ENOMEM;
1763
1764 state->acquire_ctx = &ctx;
1765 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1766
1767retry:
45723728 1768 plane_mask = 0;
d34f20d6
RC
1769 copied_objs = 0;
1770 copied_props = 0;
1771
1772 for (i = 0; i < arg->count_objs; i++) {
1773 uint32_t obj_id, count_props;
1774 struct drm_mode_object *obj;
1775
1776 if (get_user(obj_id, objs_ptr + copied_objs)) {
1777 ret = -EFAULT;
ec9f932e 1778 goto out;
d34f20d6
RC
1779 }
1780
1781 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
b164d31f
DA
1782 if (!obj) {
1783 ret = -ENOENT;
1784 goto out;
1785 }
1786
1787 if (!obj->properties) {
1788 drm_mode_object_unreference(obj);
d34f20d6 1789 ret = -ENOENT;
ec9f932e 1790 goto out;
d34f20d6
RC
1791 }
1792
d34f20d6 1793 if (get_user(count_props, count_props_ptr + copied_objs)) {
b164d31f 1794 drm_mode_object_unreference(obj);
d34f20d6 1795 ret = -EFAULT;
ec9f932e 1796 goto out;
d34f20d6
RC
1797 }
1798
1799 copied_objs++;
1800
1801 for (j = 0; j < count_props; j++) {
1802 uint32_t prop_id;
1803 uint64_t prop_value;
1804 struct drm_property *prop;
1805
1806 if (get_user(prop_id, props_ptr + copied_props)) {
b164d31f 1807 drm_mode_object_unreference(obj);
d34f20d6 1808 ret = -EFAULT;
ec9f932e 1809 goto out;
d34f20d6
RC
1810 }
1811
f92f053b 1812 prop = drm_mode_obj_find_prop_id(obj, prop_id);
d34f20d6 1813 if (!prop) {
b164d31f 1814 drm_mode_object_unreference(obj);
d34f20d6 1815 ret = -ENOENT;
ec9f932e 1816 goto out;
d34f20d6
RC
1817 }
1818
42c5814c
GR
1819 if (copy_from_user(&prop_value,
1820 prop_values_ptr + copied_props,
1821 sizeof(prop_value))) {
b164d31f 1822 drm_mode_object_unreference(obj);
d34f20d6 1823 ret = -EFAULT;
ec9f932e 1824 goto out;
d34f20d6
RC
1825 }
1826
1827 ret = atomic_set_prop(state, obj, prop, prop_value);
b164d31f
DA
1828 if (ret) {
1829 drm_mode_object_unreference(obj);
ec9f932e 1830 goto out;
b164d31f 1831 }
d34f20d6
RC
1832
1833 copied_props++;
1834 }
a9cc54ee 1835
c4749c9a
ML
1836 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1837 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
a9cc54ee
ML
1838 plane = obj_to_plane(obj);
1839 plane_mask |= (1 << drm_plane_index(plane));
1840 plane->old_fb = plane->fb;
1841 }
b164d31f 1842 drm_mode_object_unreference(obj);
d34f20d6
RC
1843 }
1844
1845 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
df63b999 1846 for_each_crtc_in_state(state, crtc, crtc_state, i) {
d34f20d6
RC
1847 struct drm_pending_vblank_event *e;
1848
1b47aaf9
GP
1849 e = create_vblank_event(dev, file_priv, NULL,
1850 arg->user_data);
d34f20d6
RC
1851 if (!e) {
1852 ret = -ENOMEM;
ec9f932e 1853 goto out;
d34f20d6
RC
1854 }
1855
1856 crtc_state->event = e;
1857 }
1858 }
1859
1860 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
c4749c9a
ML
1861 /*
1862 * Unlike commit, check_only does not clean up state.
0853695c 1863 * Below we call drm_atomic_state_put for it.
c4749c9a 1864 */
d34f20d6 1865 ret = drm_atomic_check_only(state);
d34f20d6 1866 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
b837ba0a 1867 ret = drm_atomic_nonblocking_commit(state);
d34f20d6 1868 } else {
fceffb32
RC
1869 if (unlikely(drm_debug & DRM_UT_STATE))
1870 drm_atomic_print_state(state);
1871
d34f20d6
RC
1872 ret = drm_atomic_commit(state);
1873 }
1874
ec9f932e 1875out:
0f45c26f 1876 drm_atomic_clean_old_fb(dev, plane_mask, ret);
d34f20d6 1877
c4749c9a
ML
1878 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1879 /*
1880 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1881 * if they weren't, this code should be called on success
1882 * for TEST_ONLY too.
1883 */
1884
1885 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1886 if (!crtc_state->event)
1887 continue;
1888
2dd500f1 1889 drm_event_cancel_free(dev, &crtc_state->event->base);
c4749c9a
ML
1890 }
1891 }
1892
ec9f932e
ML
1893 if (ret == -EDEADLK) {
1894 drm_atomic_state_clear(state);
1895 drm_modeset_backoff(&ctx);
1896 goto retry;
1897 }
d34f20d6 1898
0853695c 1899 drm_atomic_state_put(state);
d34f20d6
RC
1900
1901 drm_modeset_drop_locks(&ctx);
1902 drm_modeset_acquire_fini(&ctx);
1903
1904 return ret;
d34f20d6 1905}