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