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