]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/drm_atomic.c
drm/core: Use recommened kerneldoc for struct member refs
[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;
b164d31f 153 drm_connector_unreference(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
198 * state. Which could break assumptions the driver's ->atomic_check likely
199 * relies on.
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
DV
277 state->crtcs[index].state = crtc_state;
278 state->crtcs[index].ptr = crtc;
cc4ceb48
DV
279 crtc_state->state = state;
280
fa3ab4c2
VS
281 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
282 crtc->base.id, crtc->name, crtc_state, state);
cc4ceb48
DV
283
284 return crtc_state;
285}
286EXPORT_SYMBOL(drm_atomic_get_crtc_state);
287
beaf5af4
GP
288static void set_out_fence_for_crtc(struct drm_atomic_state *state,
289 struct drm_crtc *crtc, s64 __user *fence_ptr)
290{
291 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
292}
293
294static s64 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
295 struct drm_crtc *crtc)
296{
297 s64 __user *fence_ptr;
298
299 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
300 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
301
302 return fence_ptr;
303}
304
819364da
DS
305/**
306 * drm_atomic_set_mode_for_crtc - set mode for CRTC
307 * @state: the CRTC whose incoming state to update
308 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
309 *
310 * Set a mode (originating from the kernel) on the desired CRTC state. Does
311 * not change any other state properties, including enable, active, or
312 * mode_changed.
313 *
314 * RETURNS:
315 * Zero on success, error code on failure. Cannot return -EDEADLK.
316 */
317int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
318 struct drm_display_mode *mode)
319{
99cf4a29
DS
320 struct drm_mode_modeinfo umode;
321
819364da
DS
322 /* Early return for no change. */
323 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
324 return 0;
325
5f911905 326 drm_property_unreference_blob(state->mode_blob);
99cf4a29
DS
327 state->mode_blob = NULL;
328
819364da 329 if (mode) {
99cf4a29
DS
330 drm_mode_convert_to_umode(&umode, mode);
331 state->mode_blob =
332 drm_property_create_blob(state->crtc->dev,
333 sizeof(umode),
334 &umode);
335 if (IS_ERR(state->mode_blob))
336 return PTR_ERR(state->mode_blob);
337
819364da
DS
338 drm_mode_copy(&state->mode, mode);
339 state->enable = true;
340 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
341 mode->name, state);
342 } else {
343 memset(&state->mode, 0, sizeof(state->mode));
344 state->enable = false;
345 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
346 state);
347 }
348
349 return 0;
350}
351EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
352
955f3c33
DS
353/**
354 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
355 * @state: the CRTC whose incoming state to update
356 * @blob: pointer to blob property to use for mode
357 *
358 * Set a mode (originating from a blob property) on the desired CRTC state.
359 * This function will take a reference on the blob property for the CRTC state,
360 * and release the reference held on the state's existing mode property, if any
361 * was set.
362 *
363 * RETURNS:
364 * Zero on success, error code on failure. Cannot return -EDEADLK.
365 */
366int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
367 struct drm_property_blob *blob)
368{
369 if (blob == state->mode_blob)
370 return 0;
371
5f911905 372 drm_property_unreference_blob(state->mode_blob);
955f3c33
DS
373 state->mode_blob = NULL;
374
6709887c
TV
375 memset(&state->mode, 0, sizeof(state->mode));
376
955f3c33
DS
377 if (blob) {
378 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
379 drm_mode_convert_umode(&state->mode,
380 (const struct drm_mode_modeinfo *)
381 blob->data))
382 return -EINVAL;
383
384 state->mode_blob = drm_property_reference_blob(blob);
385 state->enable = true;
386 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
387 state->mode.name, state);
388 } else {
955f3c33
DS
389 state->enable = false;
390 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
391 state);
392 }
393
394 return 0;
395}
396EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
397
5488dc16
LL
398/**
399 * drm_atomic_replace_property_blob - replace a blob property
400 * @blob: a pointer to the member blob to be replaced
401 * @new_blob: the new blob to replace with
5488dc16
LL
402 * @replaced: whether the blob has been replaced
403 *
404 * RETURNS:
405 * Zero on success, error code on failure
406 */
407static void
408drm_atomic_replace_property_blob(struct drm_property_blob **blob,
409 struct drm_property_blob *new_blob,
410 bool *replaced)
411{
412 struct drm_property_blob *old_blob = *blob;
413
414 if (old_blob == new_blob)
415 return;
416
f35cbe6a 417 drm_property_unreference_blob(old_blob);
5488dc16
LL
418 if (new_blob)
419 drm_property_reference_blob(new_blob);
420 *blob = new_blob;
421 *replaced = true;
422
423 return;
424}
425
426static int
427drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
428 struct drm_property_blob **blob,
429 uint64_t blob_id,
430 ssize_t expected_size,
431 bool *replaced)
432{
5488dc16
LL
433 struct drm_property_blob *new_blob = NULL;
434
435 if (blob_id != 0) {
cac5fced 436 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
5488dc16
LL
437 if (new_blob == NULL)
438 return -EINVAL;
cac5fced
FM
439
440 if (expected_size > 0 && expected_size != new_blob->length) {
441 drm_property_unreference_blob(new_blob);
5488dc16 442 return -EINVAL;
cac5fced 443 }
5488dc16
LL
444 }
445
446 drm_atomic_replace_property_blob(blob, new_blob, replaced);
cac5fced 447 drm_property_unreference_blob(new_blob);
5488dc16
LL
448
449 return 0;
450}
451
40ecc694
RC
452/**
453 * drm_atomic_crtc_set_property - set property on CRTC
454 * @crtc: the drm CRTC to set a property on
455 * @state: the state object to update with the new property value
456 * @property: the property to set
457 * @val: the new property value
458 *
459 * Use this instead of calling crtc->atomic_set_property directly.
460 * This function handles generic/core properties and calls out to
461 * driver's ->atomic_set_property() for driver properties. To ensure
462 * consistent behavior you must call this function rather than the
463 * driver hook directly.
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);
5f911905 483 drm_property_unreference_blob(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
GP
509 } else if (property == config->prop_out_fence_ptr) {
510 s64 __user *fence_ptr = u64_to_user_ptr(val);
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 *
ac9c9256
RC
535 * This function handles generic/core properties and calls out to
536 * driver's ->atomic_get_property() for driver properties. To ensure
537 * consistent behavior you must call this function rather than the
538 * driver hook 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) {
626 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
627 crtc->base.id);
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;
cc4ceb48
DV
694 plane_state->state = state;
695
9f4c97a2
VS
696 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
697 plane->base.id, plane->name, plane_state, state);
cc4ceb48
DV
698
699 if (plane_state->crtc) {
700 struct drm_crtc_state *crtc_state;
701
702 crtc_state = drm_atomic_get_crtc_state(state,
703 plane_state->crtc);
704 if (IS_ERR(crtc_state))
705 return ERR_CAST(crtc_state);
706 }
707
708 return plane_state;
709}
710EXPORT_SYMBOL(drm_atomic_get_plane_state);
711
40ecc694
RC
712/**
713 * drm_atomic_plane_set_property - set property on plane
714 * @plane: the drm plane to set a property on
715 * @state: the state object to update with the new property value
716 * @property: the property to set
717 * @val: the new property value
718 *
719 * Use this instead of calling plane->atomic_set_property directly.
720 * This function handles generic/core properties and calls out to
721 * driver's ->atomic_set_property() for driver properties. To ensure
722 * consistent behavior you must call this function rather than the
723 * driver hook directly.
724 *
725 * RETURNS:
726 * Zero on success, error code on failure
727 */
728int drm_atomic_plane_set_property(struct drm_plane *plane,
729 struct drm_plane_state *state, struct drm_property *property,
730 uint64_t val)
731{
6b4959f4
RC
732 struct drm_device *dev = plane->dev;
733 struct drm_mode_config *config = &dev->mode_config;
734
735 if (property == config->prop_fb_id) {
736 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
737 drm_atomic_set_fb_for_plane(state, fb);
738 if (fb)
739 drm_framebuffer_unreference(fb);
96260142
GP
740 } else if (property == config->prop_in_fence_fd) {
741 if (state->fence)
742 return -EINVAL;
743
744 if (U642I64(val) == -1)
745 return 0;
746
747 state->fence = sync_file_get_fence(val);
748 if (!state->fence)
749 return -EINVAL;
750
6b4959f4
RC
751 } else if (property == config->prop_crtc_id) {
752 struct drm_crtc *crtc = drm_crtc_find(dev, val);
753 return drm_atomic_set_crtc_for_plane(state, crtc);
754 } else if (property == config->prop_crtc_x) {
755 state->crtc_x = U642I64(val);
756 } else if (property == config->prop_crtc_y) {
757 state->crtc_y = U642I64(val);
758 } else if (property == config->prop_crtc_w) {
759 state->crtc_w = val;
760 } else if (property == config->prop_crtc_h) {
761 state->crtc_h = val;
762 } else if (property == config->prop_src_x) {
763 state->src_x = val;
764 } else if (property == config->prop_src_y) {
765 state->src_y = val;
766 } else if (property == config->prop_src_w) {
767 state->src_w = val;
768 } else if (property == config->prop_src_h) {
769 state->src_h = val;
6686df8c 770 } else if (property == plane->rotation_property) {
6e0c7c33
VS
771 if (!is_power_of_2(val & DRM_ROTATE_MASK))
772 return -EINVAL;
1da30627 773 state->rotation = val;
44d1240d
MS
774 } else if (property == plane->zpos_property) {
775 state->zpos = val;
6b4959f4
RC
776 } else if (plane->funcs->atomic_set_property) {
777 return plane->funcs->atomic_set_property(plane, state,
778 property, val);
779 } else {
780 return -EINVAL;
781 }
782
783 return 0;
40ecc694
RC
784}
785EXPORT_SYMBOL(drm_atomic_plane_set_property);
786
c0714fc9
DV
787/**
788 * drm_atomic_plane_get_property - get property value from plane state
789 * @plane: the drm plane to set a property on
790 * @state: the state object to get the property value from
791 * @property: the property to set
792 * @val: return location for the property value
793 *
ac9c9256
RC
794 * This function handles generic/core properties and calls out to
795 * driver's ->atomic_get_property() for driver properties. To ensure
796 * consistent behavior you must call this function rather than the
797 * driver hook directly.
c0714fc9
DV
798 *
799 * RETURNS:
800 * Zero on success, error code on failure
ac9c9256 801 */
a97df1cc
DV
802static int
803drm_atomic_plane_get_property(struct drm_plane *plane,
ac9c9256
RC
804 const struct drm_plane_state *state,
805 struct drm_property *property, uint64_t *val)
806{
6b4959f4
RC
807 struct drm_device *dev = plane->dev;
808 struct drm_mode_config *config = &dev->mode_config;
809
810 if (property == config->prop_fb_id) {
811 *val = (state->fb) ? state->fb->base.id : 0;
96260142
GP
812 } else if (property == config->prop_in_fence_fd) {
813 *val = -1;
6b4959f4
RC
814 } else if (property == config->prop_crtc_id) {
815 *val = (state->crtc) ? state->crtc->base.id : 0;
816 } else if (property == config->prop_crtc_x) {
817 *val = I642U64(state->crtc_x);
818 } else if (property == config->prop_crtc_y) {
819 *val = I642U64(state->crtc_y);
820 } else if (property == config->prop_crtc_w) {
821 *val = state->crtc_w;
822 } else if (property == config->prop_crtc_h) {
823 *val = state->crtc_h;
824 } else if (property == config->prop_src_x) {
825 *val = state->src_x;
826 } else if (property == config->prop_src_y) {
827 *val = state->src_y;
828 } else if (property == config->prop_src_w) {
829 *val = state->src_w;
830 } else if (property == config->prop_src_h) {
831 *val = state->src_h;
6686df8c 832 } else if (property == plane->rotation_property) {
4cda09ca 833 *val = state->rotation;
44d1240d
MS
834 } else if (property == plane->zpos_property) {
835 *val = state->zpos;
6b4959f4 836 } else if (plane->funcs->atomic_get_property) {
ac9c9256 837 return plane->funcs->atomic_get_property(plane, state, property, val);
6b4959f4
RC
838 } else {
839 return -EINVAL;
840 }
841
842 return 0;
ac9c9256 843}
ac9c9256 844
f8aeb41c
DV
845static bool
846plane_switching_crtc(struct drm_atomic_state *state,
847 struct drm_plane *plane,
848 struct drm_plane_state *plane_state)
849{
850 if (!plane->state->crtc || !plane_state->crtc)
851 return false;
852
853 if (plane->state->crtc == plane_state->crtc)
854 return false;
855
856 /* This could be refined, but currently there's no helper or driver code
857 * to implement direct switching of active planes nor userspace to take
858 * advantage of more direct plane switching without the intermediate
859 * full OFF state.
860 */
861 return true;
862}
863
5e743737
RC
864/**
865 * drm_atomic_plane_check - check plane state
866 * @plane: plane to check
867 * @state: plane state to check
868 *
869 * Provides core sanity checks for plane state.
870 *
871 * RETURNS:
872 * Zero on success, error code on failure
873 */
874static int drm_atomic_plane_check(struct drm_plane *plane,
875 struct drm_plane_state *state)
876{
877 unsigned int fb_width, fb_height;
ead8610d 878 int ret;
5e743737
RC
879
880 /* either *both* CRTC and FB must be set, or neither */
881 if (WARN_ON(state->crtc && !state->fb)) {
17a38d9c 882 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
5e743737
RC
883 return -EINVAL;
884 } else if (WARN_ON(state->fb && !state->crtc)) {
17a38d9c 885 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
5e743737
RC
886 return -EINVAL;
887 }
888
889 /* if disabled, we don't care about the rest of the state: */
890 if (!state->crtc)
891 return 0;
892
893 /* Check whether this plane is usable on this CRTC */
894 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
17a38d9c 895 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
5e743737
RC
896 return -EINVAL;
897 }
898
899 /* Check whether this plane supports the fb pixel format. */
438b74a5 900 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
ead8610d 901 if (ret) {
b3c11ac2
EE
902 struct drm_format_name_buf format_name;
903 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
438b74a5 904 drm_get_format_name(state->fb->format->format,
b3c11ac2 905 &format_name));
ead8610d 906 return ret;
5e743737
RC
907 }
908
909 /* Give drivers some help against integer overflows */
910 if (state->crtc_w > INT_MAX ||
911 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
912 state->crtc_h > INT_MAX ||
913 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
17a38d9c
DV
914 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
915 state->crtc_w, state->crtc_h,
916 state->crtc_x, state->crtc_y);
5e743737
RC
917 return -ERANGE;
918 }
919
920 fb_width = state->fb->width << 16;
921 fb_height = state->fb->height << 16;
922
923 /* Make sure source coordinates are inside the fb. */
924 if (state->src_w > fb_width ||
925 state->src_x > fb_width - state->src_w ||
926 state->src_h > fb_height ||
927 state->src_y > fb_height - state->src_h) {
17a38d9c
DV
928 DRM_DEBUG_ATOMIC("Invalid source coordinates "
929 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
930 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
931 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
932 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
933 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
5e743737
RC
934 return -ENOSPC;
935 }
936
f8aeb41c 937 if (plane_switching_crtc(state->state, plane, state)) {
9f4c97a2
VS
938 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
939 plane->base.id, plane->name);
f8aeb41c
DV
940 return -EINVAL;
941 }
942
5e743737
RC
943 return 0;
944}
945
fceffb32
RC
946static void drm_atomic_plane_print_state(struct drm_printer *p,
947 const struct drm_plane_state *state)
948{
949 struct drm_plane *plane = state->plane;
950 struct drm_rect src = drm_plane_state_src(state);
951 struct drm_rect dest = drm_plane_state_dest(state);
952
953 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
954 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
955 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
956 if (state->fb) {
957 struct drm_framebuffer *fb = state->fb;
bcb0b461 958 int i, n = fb->format->num_planes;
b3c11ac2 959 struct drm_format_name_buf format_name;
fceffb32
RC
960
961 drm_printf(p, "\t\tformat=%s\n",
438b74a5 962 drm_get_format_name(fb->format->format, &format_name));
bae781b2 963 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
fceffb32
RC
964 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
965 drm_printf(p, "\t\tlayers:\n");
966 for (i = 0; i < n; i++) {
967 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
968 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
fceffb32
RC
969 }
970 }
971 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
972 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
973 drm_printf(p, "\trotation=%x\n", state->rotation);
974
975 if (plane->funcs->atomic_print_state)
976 plane->funcs->atomic_print_state(p, state);
977}
978
cc4ceb48
DV
979/**
980 * drm_atomic_get_connector_state - get connector state
981 * @state: global atomic state object
982 * @connector: connector to get state object for
983 *
984 * This function returns the connector state for the given connector,
985 * allocating it if needed. It will also grab the relevant connector lock to
986 * make sure that the state is consistent.
987 *
988 * Returns:
989 *
990 * Either the allocated state or the error code encoded into the pointer. When
991 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
992 * entire atomic sequence must be restarted. All other errors are fatal.
993 */
994struct drm_connector_state *
995drm_atomic_get_connector_state(struct drm_atomic_state *state,
996 struct drm_connector *connector)
997{
998 int ret, index;
999 struct drm_mode_config *config = &connector->dev->mode_config;
1000 struct drm_connector_state *connector_state;
1001
7f4eaa89
ML
1002 WARN_ON(!state->acquire_ctx);
1003
c7eb76f4
DV
1004 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1005 if (ret)
1006 return ERR_PTR(ret);
1007
cc4ceb48
DV
1008 index = drm_connector_index(connector);
1009
f52b69f1 1010 if (index >= state->num_connector) {
63e83c1d 1011 struct __drm_connnectors_state *c;
5fff80bb
ML
1012 int alloc = max(index + 1, config->num_connector);
1013
1014 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1015 if (!c)
1016 return ERR_PTR(-ENOMEM);
1017
1018 state->connectors = c;
1019 memset(&state->connectors[state->num_connector], 0,
1020 sizeof(*state->connectors) * (alloc - state->num_connector));
1021
5fff80bb 1022 state->num_connector = alloc;
f52b69f1
DV
1023 }
1024
63e83c1d
DV
1025 if (state->connectors[index].state)
1026 return state->connectors[index].state;
cc4ceb48 1027
cc4ceb48
DV
1028 connector_state = connector->funcs->atomic_duplicate_state(connector);
1029 if (!connector_state)
1030 return ERR_PTR(-ENOMEM);
1031
b164d31f 1032 drm_connector_reference(connector);
63e83c1d
DV
1033 state->connectors[index].state = connector_state;
1034 state->connectors[index].ptr = connector;
cc4ceb48
DV
1035 connector_state->state = state;
1036
17a38d9c
DV
1037 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
1038 connector->base.id, connector_state, state);
cc4ceb48
DV
1039
1040 if (connector_state->crtc) {
1041 struct drm_crtc_state *crtc_state;
1042
1043 crtc_state = drm_atomic_get_crtc_state(state,
1044 connector_state->crtc);
1045 if (IS_ERR(crtc_state))
1046 return ERR_CAST(crtc_state);
1047 }
1048
1049 return connector_state;
1050}
1051EXPORT_SYMBOL(drm_atomic_get_connector_state);
1052
40ecc694
RC
1053/**
1054 * drm_atomic_connector_set_property - set property on connector.
1055 * @connector: the drm connector to set a property on
1056 * @state: the state object to update with the new property value
1057 * @property: the property to set
1058 * @val: the new property value
1059 *
1060 * Use this instead of calling connector->atomic_set_property directly.
1061 * This function handles generic/core properties and calls out to
1062 * driver's ->atomic_set_property() for driver properties. To ensure
1063 * consistent behavior you must call this function rather than the
1064 * driver hook directly.
1065 *
1066 * RETURNS:
1067 * Zero on success, error code on failure
1068 */
1069int drm_atomic_connector_set_property(struct drm_connector *connector,
1070 struct drm_connector_state *state, struct drm_property *property,
1071 uint64_t val)
1072{
1073 struct drm_device *dev = connector->dev;
1074 struct drm_mode_config *config = &dev->mode_config;
1075
ae16c597
RC
1076 if (property == config->prop_crtc_id) {
1077 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1078 return drm_atomic_set_crtc_for_connector(state, crtc);
1079 } else if (property == config->dpms_property) {
40ecc694
RC
1080 /* setting DPMS property requires special handling, which
1081 * is done in legacy setprop path for us. Disallow (for
1082 * now?) atomic writes to DPMS property:
1083 */
1084 return -EINVAL;
299a16b1
BB
1085 } else if (property == config->tv_select_subconnector_property) {
1086 state->tv.subconnector = val;
1087 } else if (property == config->tv_left_margin_property) {
1088 state->tv.margins.left = val;
1089 } else if (property == config->tv_right_margin_property) {
1090 state->tv.margins.right = val;
1091 } else if (property == config->tv_top_margin_property) {
1092 state->tv.margins.top = val;
1093 } else if (property == config->tv_bottom_margin_property) {
1094 state->tv.margins.bottom = val;
1095 } else if (property == config->tv_mode_property) {
1096 state->tv.mode = val;
1097 } else if (property == config->tv_brightness_property) {
1098 state->tv.brightness = val;
1099 } else if (property == config->tv_contrast_property) {
1100 state->tv.contrast = val;
1101 } else if (property == config->tv_flicker_reduction_property) {
1102 state->tv.flicker_reduction = val;
1103 } else if (property == config->tv_overscan_property) {
1104 state->tv.overscan = val;
1105 } else if (property == config->tv_saturation_property) {
1106 state->tv.saturation = val;
1107 } else if (property == config->tv_hue_property) {
1108 state->tv.hue = val;
40ecc694
RC
1109 } else if (connector->funcs->atomic_set_property) {
1110 return connector->funcs->atomic_set_property(connector,
1111 state, property, val);
1112 } else {
1113 return -EINVAL;
1114 }
299a16b1
BB
1115
1116 return 0;
40ecc694
RC
1117}
1118EXPORT_SYMBOL(drm_atomic_connector_set_property);
1119
fceffb32
RC
1120static void drm_atomic_connector_print_state(struct drm_printer *p,
1121 const struct drm_connector_state *state)
1122{
1123 struct drm_connector *connector = state->connector;
1124
1125 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1126 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1127
1128 if (connector->funcs->atomic_print_state)
1129 connector->funcs->atomic_print_state(p, state);
1130}
1131
c0714fc9
DV
1132/**
1133 * drm_atomic_connector_get_property - get property value from connector state
1134 * @connector: the drm connector to set a property on
1135 * @state: the state object to get the property value from
1136 * @property: the property to set
1137 * @val: return location for the property value
1138 *
ac9c9256
RC
1139 * This function handles generic/core properties and calls out to
1140 * driver's ->atomic_get_property() for driver properties. To ensure
1141 * consistent behavior you must call this function rather than the
1142 * driver hook directly.
c0714fc9
DV
1143 *
1144 * RETURNS:
1145 * Zero on success, error code on failure
ac9c9256 1146 */
a97df1cc
DV
1147static int
1148drm_atomic_connector_get_property(struct drm_connector *connector,
ac9c9256
RC
1149 const struct drm_connector_state *state,
1150 struct drm_property *property, uint64_t *val)
1151{
1152 struct drm_device *dev = connector->dev;
1153 struct drm_mode_config *config = &dev->mode_config;
1154
ae16c597
RC
1155 if (property == config->prop_crtc_id) {
1156 *val = (state->crtc) ? state->crtc->base.id : 0;
1157 } else if (property == config->dpms_property) {
ac9c9256 1158 *val = connector->dpms;
299a16b1
BB
1159 } else if (property == config->tv_select_subconnector_property) {
1160 *val = state->tv.subconnector;
1161 } else if (property == config->tv_left_margin_property) {
1162 *val = state->tv.margins.left;
1163 } else if (property == config->tv_right_margin_property) {
1164 *val = state->tv.margins.right;
1165 } else if (property == config->tv_top_margin_property) {
1166 *val = state->tv.margins.top;
1167 } else if (property == config->tv_bottom_margin_property) {
1168 *val = state->tv.margins.bottom;
1169 } else if (property == config->tv_mode_property) {
1170 *val = state->tv.mode;
1171 } else if (property == config->tv_brightness_property) {
1172 *val = state->tv.brightness;
1173 } else if (property == config->tv_contrast_property) {
1174 *val = state->tv.contrast;
1175 } else if (property == config->tv_flicker_reduction_property) {
1176 *val = state->tv.flicker_reduction;
1177 } else if (property == config->tv_overscan_property) {
1178 *val = state->tv.overscan;
1179 } else if (property == config->tv_saturation_property) {
1180 *val = state->tv.saturation;
1181 } else if (property == config->tv_hue_property) {
1182 *val = state->tv.hue;
ac9c9256
RC
1183 } else if (connector->funcs->atomic_get_property) {
1184 return connector->funcs->atomic_get_property(connector,
1185 state, property, val);
1186 } else {
1187 return -EINVAL;
1188 }
1189
1190 return 0;
1191}
ac9c9256 1192
88a48e29
RC
1193int drm_atomic_get_property(struct drm_mode_object *obj,
1194 struct drm_property *property, uint64_t *val)
1195{
1196 struct drm_device *dev = property->dev;
1197 int ret;
1198
1199 switch (obj->type) {
1200 case DRM_MODE_OBJECT_CONNECTOR: {
1201 struct drm_connector *connector = obj_to_connector(obj);
1202 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1203 ret = drm_atomic_connector_get_property(connector,
1204 connector->state, property, val);
1205 break;
1206 }
1207 case DRM_MODE_OBJECT_CRTC: {
1208 struct drm_crtc *crtc = obj_to_crtc(obj);
1209 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1210 ret = drm_atomic_crtc_get_property(crtc,
1211 crtc->state, property, val);
1212 break;
1213 }
1214 case DRM_MODE_OBJECT_PLANE: {
1215 struct drm_plane *plane = obj_to_plane(obj);
1216 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1217 ret = drm_atomic_plane_get_property(plane,
1218 plane->state, property, val);
1219 break;
1220 }
1221 default:
1222 ret = -EINVAL;
1223 break;
1224 }
1225
1226 return ret;
1227}
1228
cc4ceb48
DV
1229/**
1230 * drm_atomic_set_crtc_for_plane - set crtc for plane
07cc0ef6 1231 * @plane_state: the plane whose incoming state to update
cc4ceb48
DV
1232 * @crtc: crtc to use for the plane
1233 *
1234 * Changing the assigned crtc for a plane requires us to grab the lock and state
1235 * for the new crtc, as needed. This function takes care of all these details
1236 * besides updating the pointer in the state object itself.
1237 *
1238 * Returns:
1239 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1240 * then the w/w mutex code has detected a deadlock and the entire atomic
1241 * sequence must be restarted. All other errors are fatal.
1242 */
1243int
07cc0ef6
DV
1244drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1245 struct drm_crtc *crtc)
cc4ceb48 1246{
07cc0ef6 1247 struct drm_plane *plane = plane_state->plane;
cc4ceb48
DV
1248 struct drm_crtc_state *crtc_state;
1249
6ddd388a
RC
1250 if (plane_state->crtc) {
1251 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1252 plane_state->crtc);
1253 if (WARN_ON(IS_ERR(crtc_state)))
1254 return PTR_ERR(crtc_state);
1255
1256 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1257 }
1258
1259 plane_state->crtc = crtc;
1260
cc4ceb48
DV
1261 if (crtc) {
1262 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1263 crtc);
1264 if (IS_ERR(crtc_state))
1265 return PTR_ERR(crtc_state);
6ddd388a 1266 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
cc4ceb48
DV
1267 }
1268
cc4ceb48 1269 if (crtc)
fa3ab4c2
VS
1270 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1271 plane_state, crtc->base.id, crtc->name);
cc4ceb48 1272 else
17a38d9c
DV
1273 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1274 plane_state);
cc4ceb48
DV
1275
1276 return 0;
1277}
1278EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1279
321ebf04 1280/**
16d78bc2 1281 * drm_atomic_set_fb_for_plane - set framebuffer for plane
321ebf04
DV
1282 * @plane_state: atomic state object for the plane
1283 * @fb: fb to use for the plane
1284 *
1285 * Changing the assigned framebuffer for a plane requires us to grab a reference
1286 * to the new fb and drop the reference to the old fb, if there is one. This
1287 * function takes care of all these details besides updating the pointer in the
1288 * state object itself.
1289 */
1290void
1291drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1292 struct drm_framebuffer *fb)
1293{
321ebf04 1294 if (fb)
17a38d9c
DV
1295 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1296 fb->base.id, plane_state);
321ebf04 1297 else
17a38d9c
DV
1298 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1299 plane_state);
389f78b3
CW
1300
1301 drm_framebuffer_assign(&plane_state->fb, fb);
321ebf04
DV
1302}
1303EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1304
13b55664
GP
1305/**
1306 * drm_atomic_set_fence_for_plane - set fence for plane
1307 * @plane_state: atomic state object for the plane
1308 * @fence: dma_fence to use for the plane
1309 *
1310 * Helper to setup the plane_state fence in case it is not set yet.
1311 * By using this drivers doesn't need to worry if the user choose
1312 * implicit or explicit fencing.
1313 *
1314 * This function will not set the fence to the state if it was set
1315 * via explicit fencing interfaces on the atomic ioctl. It will
1316 * all drope the reference to the fence as we not storing it
1317 * anywhere.
1318 *
1319 * Otherwise, if plane_state->fence is not set this function we
1320 * just set it with the received implict fence.
1321 */
1322void
1323drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1324 struct dma_fence *fence)
1325{
1326 if (plane_state->fence) {
1327 dma_fence_put(fence);
1328 return;
1329 }
1330
1331 plane_state->fence = fence;
1332}
1333EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1334
cc4ceb48
DV
1335/**
1336 * drm_atomic_set_crtc_for_connector - set crtc for connector
1337 * @conn_state: atomic state object for the connector
1338 * @crtc: crtc to use for the connector
1339 *
1340 * Changing the assigned crtc for a connector requires us to grab the lock and
1341 * state for the new crtc, as needed. This function takes care of all these
1342 * details besides updating the pointer in the state object itself.
1343 *
1344 * Returns:
1345 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1346 * then the w/w mutex code has detected a deadlock and the entire atomic
1347 * sequence must be restarted. All other errors are fatal.
1348 */
1349int
1350drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1351 struct drm_crtc *crtc)
1352{
1353 struct drm_crtc_state *crtc_state;
1354
e2d800a3
CW
1355 if (conn_state->crtc == crtc)
1356 return 0;
1357
1358 if (conn_state->crtc) {
4cd9fa52
ML
1359 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1360 conn_state->crtc);
1361
1362 crtc_state->connector_mask &=
1363 ~(1 << drm_connector_index(conn_state->connector));
e2d800a3
CW
1364
1365 drm_connector_unreference(conn_state->connector);
1366 conn_state->crtc = NULL;
4cd9fa52
ML
1367 }
1368
cc4ceb48
DV
1369 if (crtc) {
1370 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1371 if (IS_ERR(crtc_state))
1372 return PTR_ERR(crtc_state);
4cd9fa52
ML
1373
1374 crtc_state->connector_mask |=
1375 1 << drm_connector_index(conn_state->connector);
cc4ceb48 1376
e2d800a3
CW
1377 drm_connector_reference(conn_state->connector);
1378 conn_state->crtc = crtc;
cc4ceb48 1379
fa3ab4c2
VS
1380 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1381 conn_state, crtc->base.id, crtc->name);
e2d800a3 1382 } else {
17a38d9c
DV
1383 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1384 conn_state);
e2d800a3 1385 }
cc4ceb48
DV
1386
1387 return 0;
1388}
1389EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1390
1391/**
1392 * drm_atomic_add_affected_connectors - add connectors for crtc
1393 * @state: atomic state
1394 * @crtc: DRM crtc
1395 *
1396 * This function walks the current configuration and adds all connectors
1397 * currently using @crtc to the atomic configuration @state. Note that this
1398 * function must acquire the connection mutex. This can potentially cause
1399 * unneeded seralization if the update is just for the planes on one crtc. Hence
1400 * drivers and helpers should only call this when really needed (e.g. when a
1401 * full modeset needs to happen due to some change).
1402 *
1403 * Returns:
1404 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1405 * then the w/w mutex code has detected a deadlock and the entire atomic
1406 * sequence must be restarted. All other errors are fatal.
1407 */
1408int
1409drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1410 struct drm_crtc *crtc)
1411{
1412 struct drm_mode_config *config = &state->dev->mode_config;
1413 struct drm_connector *connector;
1414 struct drm_connector_state *conn_state;
613051da 1415 struct drm_connector_list_iter conn_iter;
cc4ceb48
DV
1416 int ret;
1417
1418 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1419 if (ret)
1420 return ret;
1421
fa3ab4c2
VS
1422 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1423 crtc->base.id, crtc->name, state);
cc4ceb48
DV
1424
1425 /*
1426 * Changed connectors are already in @state, so only need to look at the
1427 * current configuration.
1428 */
613051da
DV
1429 drm_connector_list_iter_get(state->dev, &conn_iter);
1430 drm_for_each_connector_iter(connector, &conn_iter) {
cc4ceb48
DV
1431 if (connector->state->crtc != crtc)
1432 continue;
1433
1434 conn_state = drm_atomic_get_connector_state(state, connector);
613051da
DV
1435 if (IS_ERR(conn_state)) {
1436 drm_connector_list_iter_put(&conn_iter);
cc4ceb48 1437 return PTR_ERR(conn_state);
613051da 1438 }
cc4ceb48 1439 }
613051da 1440 drm_connector_list_iter_put(&conn_iter);
cc4ceb48
DV
1441
1442 return 0;
1443}
1444EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1445
e01e9f75
ML
1446/**
1447 * drm_atomic_add_affected_planes - add planes for crtc
1448 * @state: atomic state
1449 * @crtc: DRM crtc
1450 *
1451 * This function walks the current configuration and adds all planes
1452 * currently used by @crtc to the atomic configuration @state. This is useful
1453 * when an atomic commit also needs to check all currently enabled plane on
1454 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1455 * to avoid special code to force-enable all planes.
1456 *
1457 * Since acquiring a plane state will always also acquire the w/w mutex of the
1458 * current CRTC for that plane (if there is any) adding all the plane states for
1459 * a CRTC will not reduce parallism of atomic updates.
1460 *
1461 * Returns:
1462 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1463 * then the w/w mutex code has detected a deadlock and the entire atomic
1464 * sequence must be restarted. All other errors are fatal.
1465 */
1466int
1467drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1468 struct drm_crtc *crtc)
1469{
1470 struct drm_plane *plane;
1471
1472 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1473
1474 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1475 struct drm_plane_state *plane_state =
1476 drm_atomic_get_plane_state(state, plane);
1477
1478 if (IS_ERR(plane_state))
1479 return PTR_ERR(plane_state);
1480 }
1481 return 0;
1482}
1483EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1484
cc4ceb48
DV
1485/**
1486 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1487 * @state: atomic state
1488 *
1489 * This function should be used by legacy entry points which don't understand
1490 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
16d78bc2 1491 * the slowpath completed.
cc4ceb48
DV
1492 */
1493void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1494{
81e257e9
ML
1495 struct drm_device *dev = state->dev;
1496 unsigned crtc_mask = 0;
1497 struct drm_crtc *crtc;
cc4ceb48 1498 int ret;
81e257e9
ML
1499 bool global = false;
1500
1501 drm_for_each_crtc(crtc, dev) {
1502 if (crtc->acquire_ctx != state->acquire_ctx)
1503 continue;
1504
1505 crtc_mask |= drm_crtc_mask(crtc);
1506 crtc->acquire_ctx = NULL;
1507 }
1508
1509 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1510 global = true;
1511
1512 dev->mode_config.acquire_ctx = NULL;
1513 }
cc4ceb48
DV
1514
1515retry:
1516 drm_modeset_backoff(state->acquire_ctx);
1517
81e257e9 1518 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
cc4ceb48
DV
1519 if (ret)
1520 goto retry;
81e257e9
ML
1521
1522 drm_for_each_crtc(crtc, dev)
1523 if (drm_crtc_mask(crtc) & crtc_mask)
1524 crtc->acquire_ctx = state->acquire_ctx;
1525
1526 if (global)
1527 dev->mode_config.acquire_ctx = state->acquire_ctx;
cc4ceb48
DV
1528}
1529EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1530
1531/**
1532 * drm_atomic_check_only - check whether a given config would work
1533 * @state: atomic configuration to check
1534 *
1535 * Note that this function can return -EDEADLK if the driver needed to acquire
1536 * more locks but encountered a deadlock. The caller must then do the usual w/w
1537 * backoff dance and restart. All other errors are fatal.
1538 *
1539 * Returns:
1540 * 0 on success, negative error code on failure.
1541 */
1542int drm_atomic_check_only(struct drm_atomic_state *state)
1543{
5e743737
RC
1544 struct drm_device *dev = state->dev;
1545 struct drm_mode_config *config = &dev->mode_config;
df63b999
ACO
1546 struct drm_plane *plane;
1547 struct drm_plane_state *plane_state;
1548 struct drm_crtc *crtc;
1549 struct drm_crtc_state *crtc_state;
5e743737 1550 int i, ret = 0;
cc4ceb48 1551
17a38d9c 1552 DRM_DEBUG_ATOMIC("checking %p\n", state);
cc4ceb48 1553
df63b999
ACO
1554 for_each_plane_in_state(state, plane, plane_state, i) {
1555 ret = drm_atomic_plane_check(plane, plane_state);
5e743737 1556 if (ret) {
9f4c97a2
VS
1557 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1558 plane->base.id, plane->name);
5e743737
RC
1559 return ret;
1560 }
1561 }
1562
df63b999
ACO
1563 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1564 ret = drm_atomic_crtc_check(crtc, crtc_state);
5e743737 1565 if (ret) {
fa3ab4c2
VS
1566 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1567 crtc->base.id, crtc->name);
5e743737
RC
1568 return ret;
1569 }
1570 }
1571
cc4ceb48 1572 if (config->funcs->atomic_check)
5e743737
RC
1573 ret = config->funcs->atomic_check(state->dev, state);
1574
d34f20d6 1575 if (!state->allow_modeset) {
df63b999 1576 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2465ff62 1577 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
fa3ab4c2
VS
1578 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1579 crtc->base.id, crtc->name);
d34f20d6
RC
1580 return -EINVAL;
1581 }
1582 }
1583 }
1584
5e743737 1585 return ret;
cc4ceb48
DV
1586}
1587EXPORT_SYMBOL(drm_atomic_check_only);
1588
1589/**
1590 * drm_atomic_commit - commit configuration atomically
1591 * @state: atomic configuration to check
1592 *
1593 * Note that this function can return -EDEADLK if the driver needed to acquire
1594 * more locks but encountered a deadlock. The caller must then do the usual w/w
1595 * backoff dance and restart. All other errors are fatal.
1596 *
76fede2f
ML
1597 * This function will take its own reference on @state.
1598 * Callers should always release their reference with drm_atomic_state_put().
cc4ceb48
DV
1599 *
1600 * Returns:
1601 * 0 on success, negative error code on failure.
1602 */
1603int drm_atomic_commit(struct drm_atomic_state *state)
1604{
1605 struct drm_mode_config *config = &state->dev->mode_config;
1606 int ret;
1607
1608 ret = drm_atomic_check_only(state);
1609 if (ret)
1610 return ret;
1611
17a38d9c 1612 DRM_DEBUG_ATOMIC("commiting %p\n", state);
cc4ceb48
DV
1613
1614 return config->funcs->atomic_commit(state->dev, state, false);
1615}
1616EXPORT_SYMBOL(drm_atomic_commit);
1617
1618/**
b837ba0a 1619 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
cc4ceb48
DV
1620 * @state: atomic configuration to check
1621 *
1622 * Note that this function can return -EDEADLK if the driver needed to acquire
1623 * more locks but encountered a deadlock. The caller must then do the usual w/w
1624 * backoff dance and restart. All other errors are fatal.
1625 *
76fede2f
ML
1626 * This function will take its own reference on @state.
1627 * Callers should always release their reference with drm_atomic_state_put().
cc4ceb48
DV
1628 *
1629 * Returns:
1630 * 0 on success, negative error code on failure.
1631 */
b837ba0a 1632int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
cc4ceb48
DV
1633{
1634 struct drm_mode_config *config = &state->dev->mode_config;
1635 int ret;
1636
1637 ret = drm_atomic_check_only(state);
1638 if (ret)
1639 return ret;
1640
b837ba0a 1641 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
cc4ceb48
DV
1642
1643 return config->funcs->atomic_commit(state->dev, state, true);
1644}
b837ba0a 1645EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
d34f20d6 1646
fceffb32
RC
1647static void drm_atomic_print_state(const struct drm_atomic_state *state)
1648{
1649 struct drm_printer p = drm_info_printer(state->dev->dev);
1650 struct drm_plane *plane;
1651 struct drm_plane_state *plane_state;
1652 struct drm_crtc *crtc;
1653 struct drm_crtc_state *crtc_state;
1654 struct drm_connector *connector;
1655 struct drm_connector_state *connector_state;
1656 int i;
1657
1658 DRM_DEBUG_ATOMIC("checking %p\n", state);
1659
1660 for_each_plane_in_state(state, plane, plane_state, i)
1661 drm_atomic_plane_print_state(&p, plane_state);
1662
1663 for_each_crtc_in_state(state, crtc, crtc_state, i)
1664 drm_atomic_crtc_print_state(&p, crtc_state);
1665
1666 for_each_connector_in_state(state, connector, connector_state, i)
1667 drm_atomic_connector_print_state(&p, connector_state);
1668}
1669
6559c901
RC
1670/**
1671 * drm_state_dump - dump entire device atomic state
1672 * @dev: the drm device
1673 * @p: where to print the state to
1674 *
1675 * Just for debugging. Drivers might want an option to dump state
1676 * to dmesg in case of error irq's. (Hint, you probably want to
1677 * ratelimit this!)
1678 *
1679 * The caller must drm_modeset_lock_all(), or if this is called
1680 * from error irq handler, it should not be enabled by default.
1681 * (Ie. if you are debugging errors you might not care that this
1682 * is racey. But calling this without all modeset locks held is
1683 * not inherently safe.)
1684 */
1685void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1686{
1687 struct drm_mode_config *config = &dev->mode_config;
1688 struct drm_plane *plane;
1689 struct drm_crtc *crtc;
1690 struct drm_connector *connector;
613051da 1691 struct drm_connector_list_iter conn_iter;
6559c901
RC
1692
1693 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1694 return;
1695
1696 list_for_each_entry(plane, &config->plane_list, head)
1697 drm_atomic_plane_print_state(p, plane->state);
1698
1699 list_for_each_entry(crtc, &config->crtc_list, head)
1700 drm_atomic_crtc_print_state(p, crtc->state);
1701
613051da
DV
1702 drm_connector_list_iter_get(dev, &conn_iter);
1703 drm_for_each_connector_iter(connector, &conn_iter)
6559c901 1704 drm_atomic_connector_print_state(p, connector->state);
613051da 1705 drm_connector_list_iter_put(&conn_iter);
6559c901
RC
1706}
1707EXPORT_SYMBOL(drm_state_dump);
1708
1709#ifdef CONFIG_DEBUG_FS
1710static int drm_state_info(struct seq_file *m, void *data)
1711{
1712 struct drm_info_node *node = (struct drm_info_node *) m->private;
1713 struct drm_device *dev = node->minor->dev;
1714 struct drm_printer p = drm_seq_file_printer(m);
1715
1716 drm_modeset_lock_all(dev);
1717 drm_state_dump(dev, &p);
1718 drm_modeset_unlock_all(dev);
1719
1720 return 0;
1721}
1722
1723/* any use in debugfs files to dump individual planes/crtc/etc? */
1724static const struct drm_info_list drm_atomic_debugfs_list[] = {
1725 {"state", drm_state_info, 0},
1726};
1727
1728int drm_atomic_debugfs_init(struct drm_minor *minor)
1729{
1730 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1731 ARRAY_SIZE(drm_atomic_debugfs_list),
1732 minor->debugfs_root, minor);
1733}
8c0b55e2
LD
1734
1735int drm_atomic_debugfs_cleanup(struct drm_minor *minor)
1736{
1737 return drm_debugfs_remove_files(drm_atomic_debugfs_list,
1738 ARRAY_SIZE(drm_atomic_debugfs_list),
1739 minor);
1740}
6559c901
RC
1741#endif
1742
d34f20d6
RC
1743/*
1744 * The big monstor ioctl
1745 */
1746
1747static struct drm_pending_vblank_event *create_vblank_event(
beaf5af4 1748 struct drm_device *dev, uint64_t user_data)
d34f20d6
RC
1749{
1750 struct drm_pending_vblank_event *e = NULL;
d34f20d6
RC
1751
1752 e = kzalloc(sizeof *e, GFP_KERNEL);
2dd500f1
DV
1753 if (!e)
1754 return NULL;
d34f20d6
RC
1755
1756 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2dd500f1 1757 e->event.base.length = sizeof(e->event);
d34f20d6 1758 e->event.user_data = user_data;
d34f20d6 1759
2dd500f1 1760 return e;
d34f20d6
RC
1761}
1762
1763static int atomic_set_prop(struct drm_atomic_state *state,
1764 struct drm_mode_object *obj, struct drm_property *prop,
1765 uint64_t prop_value)
1766{
1767 struct drm_mode_object *ref;
1768 int ret;
1769
1770 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1771 return -EINVAL;
1772
1773 switch (obj->type) {
1774 case DRM_MODE_OBJECT_CONNECTOR: {
1775 struct drm_connector *connector = obj_to_connector(obj);
1776 struct drm_connector_state *connector_state;
1777
1778 connector_state = drm_atomic_get_connector_state(state, connector);
1779 if (IS_ERR(connector_state)) {
1780 ret = PTR_ERR(connector_state);
1781 break;
1782 }
1783
1784 ret = drm_atomic_connector_set_property(connector,
1785 connector_state, prop, prop_value);
1786 break;
1787 }
1788 case DRM_MODE_OBJECT_CRTC: {
1789 struct drm_crtc *crtc = obj_to_crtc(obj);
1790 struct drm_crtc_state *crtc_state;
1791
1792 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1793 if (IS_ERR(crtc_state)) {
1794 ret = PTR_ERR(crtc_state);
1795 break;
1796 }
1797
1798 ret = drm_atomic_crtc_set_property(crtc,
1799 crtc_state, prop, prop_value);
1800 break;
1801 }
1802 case DRM_MODE_OBJECT_PLANE: {
1803 struct drm_plane *plane = obj_to_plane(obj);
1804 struct drm_plane_state *plane_state;
1805
1806 plane_state = drm_atomic_get_plane_state(state, plane);
1807 if (IS_ERR(plane_state)) {
1808 ret = PTR_ERR(plane_state);
1809 break;
1810 }
1811
1812 ret = drm_atomic_plane_set_property(plane,
1813 plane_state, prop, prop_value);
1814 break;
1815 }
1816 default:
1817 ret = -EINVAL;
1818 break;
1819 }
1820
1821 drm_property_change_valid_put(prop, ref);
1822 return ret;
1823}
1824
0f45c26f 1825/**
9744bf41 1826 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
0f45c26f
ML
1827 *
1828 * @dev: drm device to check.
1829 * @plane_mask: plane mask for planes that were updated.
1830 * @ret: return value, can be -EDEADLK for a retry.
1831 *
1832 * Before doing an update plane->old_fb is set to plane->fb,
1833 * but before dropping the locks old_fb needs to be set to NULL
1834 * and plane->fb updated. This is a common operation for each
1835 * atomic update, so this call is split off as a helper.
1836 */
1837void drm_atomic_clean_old_fb(struct drm_device *dev,
1838 unsigned plane_mask,
1839 int ret)
1840{
1841 struct drm_plane *plane;
1842
1843 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1844 * locks (ie. while it is still safe to deref plane->state). We
1845 * need to do this here because the driver entry points cannot
1846 * distinguish between legacy and atomic ioctls.
1847 */
1848 drm_for_each_plane_mask(plane, dev, plane_mask) {
1849 if (ret == 0) {
1850 struct drm_framebuffer *new_fb = plane->state->fb;
1851 if (new_fb)
1852 drm_framebuffer_reference(new_fb);
1853 plane->fb = new_fb;
1854 plane->crtc = plane->state->crtc;
1855
1856 if (plane->old_fb)
1857 drm_framebuffer_unreference(plane->old_fb);
1858 }
1859 plane->old_fb = NULL;
1860 }
1861}
1862EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1863
9a83a71a
GP
1864/**
1865 * DOC: explicit fencing properties
1866 *
1867 * Explicit fencing allows userspace to control the buffer synchronization
1868 * between devices. A Fence or a group of fences are transfered to/from
1869 * userspace using Sync File fds and there are two DRM properties for that.
1870 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1871 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1872 *
1873 * As a contrast, with implicit fencing the kernel keeps track of any
1874 * ongoing rendering, and automatically ensures that the atomic update waits
1875 * for any pending rendering to complete. For shared buffers represented with
ea0dd85a 1876 * a &struct dma_buf this is tracked in &reservation_object structures.
9a83a71a
GP
1877 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1878 * whereas explicit fencing is what Android wants.
1879 *
1880 * "IN_FENCE_FD”:
1881 * Use this property to pass a fence that DRM should wait on before
1882 * proceeding with the Atomic Commit request and show the framebuffer for
1883 * the plane on the screen. The fence can be either a normal fence or a
1884 * merged one, the sync_file framework will handle both cases and use a
1885 * fence_array if a merged fence is received. Passing -1 here means no
1886 * fences to wait on.
1887 *
1888 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1889 * it will only check if the Sync File is a valid one.
1890 *
1891 * On the driver side the fence is stored on the @fence parameter of
ea0dd85a 1892 * &struct drm_plane_state. Drivers which also support implicit fencing
9a83a71a
GP
1893 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1894 * to make sure there's consistent behaviour between drivers in precedence
1895 * of implicit vs. explicit fencing.
1896 *
1897 * "OUT_FENCE_PTR”:
1898 * Use this property to pass a file descriptor pointer to DRM. Once the
1899 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1900 * the file descriptor number of a Sync File. This Sync File contains the
1901 * CRTC fence that will be signaled when all framebuffers present on the
1902 * Atomic Commit * request for that given CRTC are scanned out on the
1903 * screen.
1904 *
1905 * The Atomic Commit request fails if a invalid pointer is passed. If the
1906 * Atomic Commit request fails for any other reason the out fence fd
1907 * returned will be -1. On a Atomic Commit with the
1908 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1909 *
1910 * Note that out-fences don't have a special interface to drivers and are
ea0dd85a 1911 * internally represented by a &struct drm_pending_vblank_event in struct
9a83a71a
GP
1912 * &drm_crtc_state, which is also used by the nonblocking atomic commit
1913 * helpers and for the DRM event handling for existing userspace.
1914 */
1915
beaf5af4
GP
1916struct drm_out_fence_state {
1917 s64 __user *out_fence_ptr;
1918 struct sync_file *sync_file;
1919 int fd;
1920};
1921
1922static int setup_out_fence(struct drm_out_fence_state *fence_state,
1923 struct dma_fence *fence)
1924{
1925 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1926 if (fence_state->fd < 0)
1927 return fence_state->fd;
1928
1929 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1930 return -EFAULT;
1931
1932 fence_state->sync_file = sync_file_create(fence);
1933 if (!fence_state->sync_file)
1934 return -ENOMEM;
1935
1936 return 0;
1937}
1938
1939static int prepare_crtc_signaling(struct drm_device *dev,
1940 struct drm_atomic_state *state,
1941 struct drm_mode_atomic *arg,
1942 struct drm_file *file_priv,
1943 struct drm_out_fence_state **fence_state,
1944 unsigned int *num_fences)
1945{
1946 struct drm_crtc *crtc;
1947 struct drm_crtc_state *crtc_state;
1948 int i, ret;
1949
1950 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1951 return 0;
1952
1953 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1954 u64 __user *fence_ptr;
1955
1956 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1957
1958 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1959 struct drm_pending_vblank_event *e;
1960
1961 e = create_vblank_event(dev, arg->user_data);
1962 if (!e)
1963 return -ENOMEM;
1964
1965 crtc_state->event = e;
1966 }
1967
1968 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1969 struct drm_pending_vblank_event *e = crtc_state->event;
1970
1971 if (!file_priv)
1972 continue;
1973
1974 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1975 &e->event.base);
1976 if (ret) {
1977 kfree(e);
1978 crtc_state->event = NULL;
1979 return ret;
1980 }
1981 }
1982
1983 if (fence_ptr) {
1984 struct dma_fence *fence;
1985 struct drm_out_fence_state *f;
1986
1987 f = krealloc(*fence_state, sizeof(**fence_state) *
1988 (*num_fences + 1), GFP_KERNEL);
1989 if (!f)
1990 return -ENOMEM;
1991
1992 memset(&f[*num_fences], 0, sizeof(*f));
1993
1994 f[*num_fences].out_fence_ptr = fence_ptr;
1995 *fence_state = f;
1996
35f8cc3b 1997 fence = drm_crtc_create_fence(crtc);
beaf5af4
GP
1998 if (!fence)
1999 return -ENOMEM;
2000
2001 ret = setup_out_fence(&f[(*num_fences)++], fence);
2002 if (ret) {
2003 dma_fence_put(fence);
2004 return ret;
2005 }
2006
2007 crtc_state->event->base.fence = fence;
2008 }
2009 }
2010
2011 return 0;
2012}
2013
2014static void complete_crtc_signaling(struct drm_device *dev,
2015 struct drm_atomic_state *state,
2016 struct drm_out_fence_state *fence_state,
2017 unsigned int num_fences,
2018 bool install_fds)
2019{
2020 struct drm_crtc *crtc;
2021 struct drm_crtc_state *crtc_state;
2022 int i;
2023
2024 if (install_fds) {
2025 for (i = 0; i < num_fences; i++)
2026 fd_install(fence_state[i].fd,
2027 fence_state[i].sync_file->file);
2028
2029 kfree(fence_state);
2030 return;
2031 }
2032
2033 for_each_crtc_in_state(state, crtc, crtc_state, i) {
2034 /*
2035 * TEST_ONLY and PAGE_FLIP_EVENT are mutually
2036 * exclusive, if they weren't, this code should be
2037 * called on success for TEST_ONLY too.
2038 */
2039 if (crtc_state->event)
2040 drm_event_cancel_free(dev, &crtc_state->event->base);
2041 }
2042
2043 if (!fence_state)
2044 return;
2045
2046 for (i = 0; i < num_fences; i++) {
2047 if (fence_state[i].sync_file)
2048 fput(fence_state[i].sync_file->file);
2049 if (fence_state[i].fd >= 0)
2050 put_unused_fd(fence_state[i].fd);
2051
2052 /* If this fails log error to the user */
2053 if (fence_state[i].out_fence_ptr &&
2054 put_user(-1, fence_state[i].out_fence_ptr))
2055 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2056 }
2057
2058 kfree(fence_state);
2059}
2060
d34f20d6
RC
2061int drm_mode_atomic_ioctl(struct drm_device *dev,
2062 void *data, struct drm_file *file_priv)
2063{
2064 struct drm_mode_atomic *arg = data;
2065 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2066 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2067 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2068 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2069 unsigned int copied_objs, copied_props;
2070 struct drm_atomic_state *state;
2071 struct drm_modeset_acquire_ctx ctx;
2072 struct drm_plane *plane;
beaf5af4 2073 struct drm_out_fence_state *fence_state = NULL;
45723728 2074 unsigned plane_mask;
d34f20d6 2075 int ret = 0;
beaf5af4 2076 unsigned int i, j, num_fences = 0;
d34f20d6
RC
2077
2078 /* disallow for drivers not supporting atomic: */
2079 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2080 return -EINVAL;
2081
2082 /* disallow for userspace that has not enabled atomic cap (even
2083 * though this may be a bit overkill, since legacy userspace
2084 * wouldn't know how to call this ioctl)
2085 */
2086 if (!file_priv->atomic)
2087 return -EINVAL;
2088
2089 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2090 return -EINVAL;
2091
2092 if (arg->reserved)
2093 return -EINVAL;
2094
2095 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2096 !dev->mode_config.async_page_flip)
2097 return -EINVAL;
2098
2099 /* can't test and expect an event at the same time. */
2100 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2101 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2102 return -EINVAL;
2103
2104 drm_modeset_acquire_init(&ctx, 0);
2105
2106 state = drm_atomic_state_alloc(dev);
2107 if (!state)
2108 return -ENOMEM;
2109
2110 state->acquire_ctx = &ctx;
2111 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2112
2113retry:
45723728 2114 plane_mask = 0;
d34f20d6
RC
2115 copied_objs = 0;
2116 copied_props = 0;
2117
2118 for (i = 0; i < arg->count_objs; i++) {
2119 uint32_t obj_id, count_props;
2120 struct drm_mode_object *obj;
2121
2122 if (get_user(obj_id, objs_ptr + copied_objs)) {
2123 ret = -EFAULT;
ec9f932e 2124 goto out;
d34f20d6
RC
2125 }
2126
2127 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
b164d31f
DA
2128 if (!obj) {
2129 ret = -ENOENT;
2130 goto out;
2131 }
2132
2133 if (!obj->properties) {
2134 drm_mode_object_unreference(obj);
d34f20d6 2135 ret = -ENOENT;
ec9f932e 2136 goto out;
d34f20d6
RC
2137 }
2138
d34f20d6 2139 if (get_user(count_props, count_props_ptr + copied_objs)) {
b164d31f 2140 drm_mode_object_unreference(obj);
d34f20d6 2141 ret = -EFAULT;
ec9f932e 2142 goto out;
d34f20d6
RC
2143 }
2144
2145 copied_objs++;
2146
2147 for (j = 0; j < count_props; j++) {
2148 uint32_t prop_id;
2149 uint64_t prop_value;
2150 struct drm_property *prop;
2151
2152 if (get_user(prop_id, props_ptr + copied_props)) {
b164d31f 2153 drm_mode_object_unreference(obj);
d34f20d6 2154 ret = -EFAULT;
ec9f932e 2155 goto out;
d34f20d6
RC
2156 }
2157
f92f053b 2158 prop = drm_mode_obj_find_prop_id(obj, prop_id);
d34f20d6 2159 if (!prop) {
b164d31f 2160 drm_mode_object_unreference(obj);
d34f20d6 2161 ret = -ENOENT;
ec9f932e 2162 goto out;
d34f20d6
RC
2163 }
2164
42c5814c
GR
2165 if (copy_from_user(&prop_value,
2166 prop_values_ptr + copied_props,
2167 sizeof(prop_value))) {
b164d31f 2168 drm_mode_object_unreference(obj);
d34f20d6 2169 ret = -EFAULT;
ec9f932e 2170 goto out;
d34f20d6
RC
2171 }
2172
2173 ret = atomic_set_prop(state, obj, prop, prop_value);
b164d31f
DA
2174 if (ret) {
2175 drm_mode_object_unreference(obj);
ec9f932e 2176 goto out;
b164d31f 2177 }
d34f20d6
RC
2178
2179 copied_props++;
2180 }
a9cc54ee 2181
c4749c9a
ML
2182 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2183 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
a9cc54ee
ML
2184 plane = obj_to_plane(obj);
2185 plane_mask |= (1 << drm_plane_index(plane));
2186 plane->old_fb = plane->fb;
2187 }
b164d31f 2188 drm_mode_object_unreference(obj);
d34f20d6
RC
2189 }
2190
beaf5af4
GP
2191 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2192 &num_fences);
2193 if (ret)
2194 goto out;
d34f20d6
RC
2195
2196 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2197 ret = drm_atomic_check_only(state);
d34f20d6 2198 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
b837ba0a 2199 ret = drm_atomic_nonblocking_commit(state);
d34f20d6 2200 } else {
fceffb32
RC
2201 if (unlikely(drm_debug & DRM_UT_STATE))
2202 drm_atomic_print_state(state);
2203
d34f20d6
RC
2204 ret = drm_atomic_commit(state);
2205 }
2206
ec9f932e 2207out:
0f45c26f 2208 drm_atomic_clean_old_fb(dev, plane_mask, ret);
d34f20d6 2209
beaf5af4 2210 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
c4749c9a 2211
ec9f932e
ML
2212 if (ret == -EDEADLK) {
2213 drm_atomic_state_clear(state);
2214 drm_modeset_backoff(&ctx);
2215 goto retry;
2216 }
d34f20d6 2217
0853695c 2218 drm_atomic_state_put(state);
d34f20d6
RC
2219
2220 drm_modeset_drop_locks(&ctx);
2221 drm_modeset_acquire_fini(&ctx);
2222
2223 return ret;
d34f20d6 2224}