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