]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/drm_atomic.c
drm/atomic: Add current-mode blob to CRTC state
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / drm_atomic.c
CommitLineData
cc4ceb48
DV
1/*
2 * Copyright (C) 2014 Red Hat
3 * Copyright (C) 2014 Intel Corp.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robdclark@gmail.com>
25 * Daniel Vetter <daniel.vetter@ffwll.ch>
26 */
27
28
29#include <drm/drmP.h>
30#include <drm/drm_atomic.h>
31#include <drm/drm_plane_helper.h>
32
036ef573
ML
33/**
34 * drm_atomic_state_default_release -
35 * release memory initialized by drm_atomic_state_init
36 * @state: atomic state
37 *
38 * Free all the memory allocated by drm_atomic_state_init.
39 * This is useful for drivers that subclass the atomic state.
40 */
41void drm_atomic_state_default_release(struct drm_atomic_state *state)
cc4ceb48
DV
42{
43 kfree(state->connectors);
44 kfree(state->connector_states);
45 kfree(state->crtcs);
46 kfree(state->crtc_states);
47 kfree(state->planes);
48 kfree(state->plane_states);
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
f52b69f1
DV
68 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
69
cc4ceb48
DV
70 state->crtcs = kcalloc(dev->mode_config.num_crtc,
71 sizeof(*state->crtcs), GFP_KERNEL);
72 if (!state->crtcs)
73 goto fail;
74 state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75 sizeof(*state->crtc_states), GFP_KERNEL);
76 if (!state->crtc_states)
77 goto fail;
78 state->planes = kcalloc(dev->mode_config.num_total_plane,
79 sizeof(*state->planes), GFP_KERNEL);
80 if (!state->planes)
81 goto fail;
82 state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83 sizeof(*state->plane_states), GFP_KERNEL);
84 if (!state->plane_states)
85 goto fail;
f52b69f1 86 state->connectors = kcalloc(state->num_connector,
cc4ceb48
DV
87 sizeof(*state->connectors),
88 GFP_KERNEL);
89 if (!state->connectors)
90 goto fail;
f52b69f1 91 state->connector_states = kcalloc(state->num_connector,
cc4ceb48
DV
92 sizeof(*state->connector_states),
93 GFP_KERNEL);
94 if (!state->connector_states)
95 goto fail;
96
97 state->dev = dev;
98
036ef573 99 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
cc4ceb48 100
036ef573 101 return 0;
cc4ceb48 102fail:
036ef573
ML
103 drm_atomic_state_default_release(state);
104 return -ENOMEM;
105}
106EXPORT_SYMBOL(drm_atomic_state_init);
107
108/**
109 * drm_atomic_state_alloc - allocate atomic state
110 * @dev: DRM device
111 *
112 * This allocates an empty atomic state to track updates.
113 */
114struct drm_atomic_state *
115drm_atomic_state_alloc(struct drm_device *dev)
116{
117 struct drm_mode_config *config = &dev->mode_config;
118 struct drm_atomic_state *state;
119
120 if (!config->funcs->atomic_state_alloc) {
121 state = kzalloc(sizeof(*state), GFP_KERNEL);
122 if (!state)
123 return NULL;
124 if (drm_atomic_state_init(dev, state) < 0) {
125 kfree(state);
126 return NULL;
127 }
128 return state;
129 }
cc4ceb48 130
036ef573 131 return config->funcs->atomic_state_alloc(dev);
cc4ceb48
DV
132}
133EXPORT_SYMBOL(drm_atomic_state_alloc);
134
135/**
036ef573 136 * drm_atomic_state_default_clear - clear base atomic state
cc4ceb48
DV
137 * @state: atomic state
138 *
036ef573
ML
139 * Default implementation for clearing atomic state.
140 * This is useful for drivers that subclass the atomic state.
cc4ceb48 141 */
036ef573 142void drm_atomic_state_default_clear(struct drm_atomic_state *state)
cc4ceb48
DV
143{
144 struct drm_device *dev = state->dev;
6f75cea6 145 struct drm_mode_config *config = &dev->mode_config;
cc4ceb48
DV
146 int i;
147
17a38d9c 148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
cc4ceb48 149
f52b69f1 150 for (i = 0; i < state->num_connector; i++) {
cc4ceb48
DV
151 struct drm_connector *connector = state->connectors[i];
152
153 if (!connector)
154 continue;
155
6f75cea6
DV
156 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
157
cc4ceb48
DV
158 connector->funcs->atomic_destroy_state(connector,
159 state->connector_states[i]);
8a5c0bde 160 state->connectors[i] = NULL;
9469244d 161 state->connector_states[i] = NULL;
cc4ceb48
DV
162 }
163
6f75cea6 164 for (i = 0; i < config->num_crtc; i++) {
cc4ceb48
DV
165 struct drm_crtc *crtc = state->crtcs[i];
166
167 if (!crtc)
168 continue;
169
170 crtc->funcs->atomic_destroy_state(crtc,
171 state->crtc_states[i]);
8a5c0bde 172 state->crtcs[i] = NULL;
9469244d 173 state->crtc_states[i] = NULL;
cc4ceb48
DV
174 }
175
6f75cea6 176 for (i = 0; i < config->num_total_plane; i++) {
cc4ceb48
DV
177 struct drm_plane *plane = state->planes[i];
178
179 if (!plane)
180 continue;
181
182 plane->funcs->atomic_destroy_state(plane,
183 state->plane_states[i]);
8a5c0bde 184 state->planes[i] = NULL;
9469244d 185 state->plane_states[i] = 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/**
217 * drm_atomic_state_free - free all memory for an atomic state
218 * @state: atomic state to deallocate
219 *
220 * This frees all memory associated with an atomic state, including all the
221 * per-object state for planes, crtcs and connectors.
222 */
223void drm_atomic_state_free(struct drm_atomic_state *state)
224{
036ef573
ML
225 struct drm_device *dev;
226 struct drm_mode_config *config;
227
a0211bb4
ACO
228 if (!state)
229 return;
230
036ef573
ML
231 dev = state->dev;
232 config = &dev->mode_config;
233
cc4ceb48
DV
234 drm_atomic_state_clear(state);
235
17a38d9c 236 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
cc4ceb48 237
036ef573
ML
238 if (config->funcs->atomic_state_free) {
239 config->funcs->atomic_state_free(state);
240 } else {
241 drm_atomic_state_default_release(state);
242 kfree(state);
243 }
cc4ceb48
DV
244}
245EXPORT_SYMBOL(drm_atomic_state_free);
246
247/**
248 * drm_atomic_get_crtc_state - get crtc state
249 * @state: global atomic state object
250 * @crtc: crtc to get state object for
251 *
252 * This function returns the crtc state for the given crtc, allocating it if
253 * needed. It will also grab the relevant crtc lock to make sure that the state
254 * is consistent.
255 *
256 * Returns:
257 *
258 * Either the allocated state or the error code encoded into the pointer. When
259 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
260 * entire atomic sequence must be restarted. All other errors are fatal.
261 */
262struct drm_crtc_state *
263drm_atomic_get_crtc_state(struct drm_atomic_state *state,
264 struct drm_crtc *crtc)
265{
1b26a5e1 266 int ret, index = drm_crtc_index(crtc);
cc4ceb48
DV
267 struct drm_crtc_state *crtc_state;
268
1b26a5e1
ML
269 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
270 if (crtc_state)
271 return crtc_state;
cc4ceb48
DV
272
273 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
274 if (ret)
275 return ERR_PTR(ret);
276
277 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
278 if (!crtc_state)
279 return ERR_PTR(-ENOMEM);
280
281 state->crtc_states[index] = crtc_state;
282 state->crtcs[index] = crtc;
283 crtc_state->state = state;
284
17a38d9c
DV
285 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
286 crtc->base.id, crtc_state, state);
cc4ceb48
DV
287
288 return crtc_state;
289}
290EXPORT_SYMBOL(drm_atomic_get_crtc_state);
291
819364da
DS
292/**
293 * drm_atomic_set_mode_for_crtc - set mode for CRTC
294 * @state: the CRTC whose incoming state to update
295 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
296 *
297 * Set a mode (originating from the kernel) on the desired CRTC state. Does
298 * not change any other state properties, including enable, active, or
299 * mode_changed.
300 *
301 * RETURNS:
302 * Zero on success, error code on failure. Cannot return -EDEADLK.
303 */
304int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
305 struct drm_display_mode *mode)
306{
99cf4a29
DS
307 struct drm_mode_modeinfo umode;
308
819364da
DS
309 /* Early return for no change. */
310 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
311 return 0;
312
99cf4a29
DS
313 if (state->mode_blob)
314 drm_property_unreference_blob(state->mode_blob);
315 state->mode_blob = NULL;
316
819364da 317 if (mode) {
99cf4a29
DS
318 drm_mode_convert_to_umode(&umode, mode);
319 state->mode_blob =
320 drm_property_create_blob(state->crtc->dev,
321 sizeof(umode),
322 &umode);
323 if (IS_ERR(state->mode_blob))
324 return PTR_ERR(state->mode_blob);
325
819364da
DS
326 drm_mode_copy(&state->mode, mode);
327 state->enable = true;
328 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
329 mode->name, state);
330 } else {
331 memset(&state->mode, 0, sizeof(state->mode));
332 state->enable = false;
333 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
334 state);
335 }
336
337 return 0;
338}
339EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
340
40ecc694
RC
341/**
342 * drm_atomic_crtc_set_property - set property on CRTC
343 * @crtc: the drm CRTC to set a property on
344 * @state: the state object to update with the new property value
345 * @property: the property to set
346 * @val: the new property value
347 *
348 * Use this instead of calling crtc->atomic_set_property directly.
349 * This function handles generic/core properties and calls out to
350 * driver's ->atomic_set_property() for driver properties. To ensure
351 * consistent behavior you must call this function rather than the
352 * driver hook directly.
353 *
354 * RETURNS:
355 * Zero on success, error code on failure
356 */
357int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
358 struct drm_crtc_state *state, struct drm_property *property,
359 uint64_t val)
360{
eab3bbef
DV
361 struct drm_device *dev = crtc->dev;
362 struct drm_mode_config *config = &dev->mode_config;
363
364 /* FIXME: Mode prop is missing, which also controls ->enable. */
27798365 365 if (property == config->prop_active)
eab3bbef 366 state->active = val;
27798365 367 else if (crtc->funcs->atomic_set_property)
40ecc694 368 return crtc->funcs->atomic_set_property(crtc, state, property, val);
27798365
DS
369 else
370 return -EINVAL;
371
372 return 0;
40ecc694
RC
373}
374EXPORT_SYMBOL(drm_atomic_crtc_set_property);
375
a97df1cc 376/*
ac9c9256
RC
377 * This function handles generic/core properties and calls out to
378 * driver's ->atomic_get_property() for driver properties. To ensure
379 * consistent behavior you must call this function rather than the
380 * driver hook directly.
ac9c9256
RC
381 */
382int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
383 const struct drm_crtc_state *state,
384 struct drm_property *property, uint64_t *val)
385{
8f164ce4
DS
386 struct drm_device *dev = crtc->dev;
387 struct drm_mode_config *config = &dev->mode_config;
388
389 if (property == config->prop_active)
390 *val = state->active;
391 else if (crtc->funcs->atomic_get_property)
ac9c9256 392 return crtc->funcs->atomic_get_property(crtc, state, property, val);
8f164ce4
DS
393 else
394 return -EINVAL;
395
396 return 0;
ac9c9256 397}
ac9c9256 398
5e743737
RC
399/**
400 * drm_atomic_crtc_check - check crtc state
401 * @crtc: crtc to check
402 * @state: crtc state to check
403 *
404 * Provides core sanity checks for crtc state.
405 *
406 * RETURNS:
407 * Zero on success, error code on failure
408 */
409static int drm_atomic_crtc_check(struct drm_crtc *crtc,
410 struct drm_crtc_state *state)
411{
412 /* NOTE: we explicitly don't enforce constraints such as primary
413 * layer covering entire screen, since that is something we want
414 * to allow (on hw that supports it). For hw that does not, it
415 * should be checked in driver's crtc->atomic_check() vfunc.
416 *
417 * TODO: Add generic modeset state checks once we support those.
418 */
eab3bbef
DV
419
420 if (state->active && !state->enable) {
17a38d9c
DV
421 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
422 crtc->base.id);
eab3bbef
DV
423 return -EINVAL;
424 }
425
99cf4a29
DS
426 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
427 * as this is a kernel-internal detail that userspace should never
428 * be able to trigger. */
429 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
430 WARN_ON(state->enable && !state->mode_blob)) {
431 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
432 crtc->base.id);
433 return -EINVAL;
434 }
435
436 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
437 WARN_ON(!state->enable && state->mode_blob)) {
438 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
439 crtc->base.id);
440 return -EINVAL;
441 }
442
5e743737
RC
443 return 0;
444}
445
cc4ceb48
DV
446/**
447 * drm_atomic_get_plane_state - get plane state
448 * @state: global atomic state object
449 * @plane: plane to get state object for
450 *
451 * This function returns the plane state for the given plane, allocating it if
452 * needed. It will also grab the relevant plane lock to make sure that the state
453 * is consistent.
454 *
455 * Returns:
456 *
457 * Either the allocated state or the error code encoded into the pointer. When
458 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
459 * entire atomic sequence must be restarted. All other errors are fatal.
460 */
461struct drm_plane_state *
462drm_atomic_get_plane_state(struct drm_atomic_state *state,
463 struct drm_plane *plane)
464{
1b26a5e1 465 int ret, index = drm_plane_index(plane);
cc4ceb48
DV
466 struct drm_plane_state *plane_state;
467
1b26a5e1
ML
468 plane_state = drm_atomic_get_existing_plane_state(state, plane);
469 if (plane_state)
470 return plane_state;
cc4ceb48 471
4d02e2de 472 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
cc4ceb48
DV
473 if (ret)
474 return ERR_PTR(ret);
475
476 plane_state = plane->funcs->atomic_duplicate_state(plane);
477 if (!plane_state)
478 return ERR_PTR(-ENOMEM);
479
480 state->plane_states[index] = plane_state;
481 state->planes[index] = plane;
482 plane_state->state = state;
483
17a38d9c
DV
484 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
485 plane->base.id, plane_state, state);
cc4ceb48
DV
486
487 if (plane_state->crtc) {
488 struct drm_crtc_state *crtc_state;
489
490 crtc_state = drm_atomic_get_crtc_state(state,
491 plane_state->crtc);
492 if (IS_ERR(crtc_state))
493 return ERR_CAST(crtc_state);
494 }
495
496 return plane_state;
497}
498EXPORT_SYMBOL(drm_atomic_get_plane_state);
499
40ecc694
RC
500/**
501 * drm_atomic_plane_set_property - set property on plane
502 * @plane: the drm plane to set a property on
503 * @state: the state object to update with the new property value
504 * @property: the property to set
505 * @val: the new property value
506 *
507 * Use this instead of calling plane->atomic_set_property directly.
508 * This function handles generic/core properties and calls out to
509 * driver's ->atomic_set_property() for driver properties. To ensure
510 * consistent behavior you must call this function rather than the
511 * driver hook directly.
512 *
513 * RETURNS:
514 * Zero on success, error code on failure
515 */
516int drm_atomic_plane_set_property(struct drm_plane *plane,
517 struct drm_plane_state *state, struct drm_property *property,
518 uint64_t val)
519{
6b4959f4
RC
520 struct drm_device *dev = plane->dev;
521 struct drm_mode_config *config = &dev->mode_config;
522
523 if (property == config->prop_fb_id) {
524 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
525 drm_atomic_set_fb_for_plane(state, fb);
526 if (fb)
527 drm_framebuffer_unreference(fb);
528 } else if (property == config->prop_crtc_id) {
529 struct drm_crtc *crtc = drm_crtc_find(dev, val);
530 return drm_atomic_set_crtc_for_plane(state, crtc);
531 } else if (property == config->prop_crtc_x) {
532 state->crtc_x = U642I64(val);
533 } else if (property == config->prop_crtc_y) {
534 state->crtc_y = U642I64(val);
535 } else if (property == config->prop_crtc_w) {
536 state->crtc_w = val;
537 } else if (property == config->prop_crtc_h) {
538 state->crtc_h = val;
539 } else if (property == config->prop_src_x) {
540 state->src_x = val;
541 } else if (property == config->prop_src_y) {
542 state->src_y = val;
543 } else if (property == config->prop_src_w) {
544 state->src_w = val;
545 } else if (property == config->prop_src_h) {
546 state->src_h = val;
1da30627
MR
547 } else if (property == config->rotation_property) {
548 state->rotation = val;
6b4959f4
RC
549 } else if (plane->funcs->atomic_set_property) {
550 return plane->funcs->atomic_set_property(plane, state,
551 property, val);
552 } else {
553 return -EINVAL;
554 }
555
556 return 0;
40ecc694
RC
557}
558EXPORT_SYMBOL(drm_atomic_plane_set_property);
559
a97df1cc 560/*
ac9c9256
RC
561 * This function handles generic/core properties and calls out to
562 * driver's ->atomic_get_property() for driver properties. To ensure
563 * consistent behavior you must call this function rather than the
564 * driver hook directly.
ac9c9256 565 */
a97df1cc
DV
566static int
567drm_atomic_plane_get_property(struct drm_plane *plane,
ac9c9256
RC
568 const struct drm_plane_state *state,
569 struct drm_property *property, uint64_t *val)
570{
6b4959f4
RC
571 struct drm_device *dev = plane->dev;
572 struct drm_mode_config *config = &dev->mode_config;
573
574 if (property == config->prop_fb_id) {
575 *val = (state->fb) ? state->fb->base.id : 0;
576 } else if (property == config->prop_crtc_id) {
577 *val = (state->crtc) ? state->crtc->base.id : 0;
578 } else if (property == config->prop_crtc_x) {
579 *val = I642U64(state->crtc_x);
580 } else if (property == config->prop_crtc_y) {
581 *val = I642U64(state->crtc_y);
582 } else if (property == config->prop_crtc_w) {
583 *val = state->crtc_w;
584 } else if (property == config->prop_crtc_h) {
585 *val = state->crtc_h;
586 } else if (property == config->prop_src_x) {
587 *val = state->src_x;
588 } else if (property == config->prop_src_y) {
589 *val = state->src_y;
590 } else if (property == config->prop_src_w) {
591 *val = state->src_w;
592 } else if (property == config->prop_src_h) {
593 *val = state->src_h;
4cda09ca
TU
594 } else if (property == config->rotation_property) {
595 *val = state->rotation;
6b4959f4 596 } else if (plane->funcs->atomic_get_property) {
ac9c9256 597 return plane->funcs->atomic_get_property(plane, state, property, val);
6b4959f4
RC
598 } else {
599 return -EINVAL;
600 }
601
602 return 0;
ac9c9256 603}
ac9c9256 604
5e743737
RC
605/**
606 * drm_atomic_plane_check - check plane state
607 * @plane: plane to check
608 * @state: plane state to check
609 *
610 * Provides core sanity checks for plane state.
611 *
612 * RETURNS:
613 * Zero on success, error code on failure
614 */
615static int drm_atomic_plane_check(struct drm_plane *plane,
616 struct drm_plane_state *state)
617{
618 unsigned int fb_width, fb_height;
ead8610d 619 int ret;
5e743737
RC
620
621 /* either *both* CRTC and FB must be set, or neither */
622 if (WARN_ON(state->crtc && !state->fb)) {
17a38d9c 623 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
5e743737
RC
624 return -EINVAL;
625 } else if (WARN_ON(state->fb && !state->crtc)) {
17a38d9c 626 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
5e743737
RC
627 return -EINVAL;
628 }
629
630 /* if disabled, we don't care about the rest of the state: */
631 if (!state->crtc)
632 return 0;
633
634 /* Check whether this plane is usable on this CRTC */
635 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
17a38d9c 636 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
5e743737
RC
637 return -EINVAL;
638 }
639
640 /* Check whether this plane supports the fb pixel format. */
ead8610d
LP
641 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
642 if (ret) {
17a38d9c
DV
643 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
644 drm_get_format_name(state->fb->pixel_format));
ead8610d 645 return ret;
5e743737
RC
646 }
647
648 /* Give drivers some help against integer overflows */
649 if (state->crtc_w > INT_MAX ||
650 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
651 state->crtc_h > INT_MAX ||
652 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
17a38d9c
DV
653 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
654 state->crtc_w, state->crtc_h,
655 state->crtc_x, state->crtc_y);
5e743737
RC
656 return -ERANGE;
657 }
658
659 fb_width = state->fb->width << 16;
660 fb_height = state->fb->height << 16;
661
662 /* Make sure source coordinates are inside the fb. */
663 if (state->src_w > fb_width ||
664 state->src_x > fb_width - state->src_w ||
665 state->src_h > fb_height ||
666 state->src_y > fb_height - state->src_h) {
17a38d9c
DV
667 DRM_DEBUG_ATOMIC("Invalid source coordinates "
668 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
669 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
670 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
671 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
672 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
5e743737
RC
673 return -ENOSPC;
674 }
675
676 return 0;
677}
678
cc4ceb48
DV
679/**
680 * drm_atomic_get_connector_state - get connector state
681 * @state: global atomic state object
682 * @connector: connector to get state object for
683 *
684 * This function returns the connector state for the given connector,
685 * allocating it if needed. It will also grab the relevant connector lock to
686 * make sure that the state is consistent.
687 *
688 * Returns:
689 *
690 * Either the allocated state or the error code encoded into the pointer. When
691 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
692 * entire atomic sequence must be restarted. All other errors are fatal.
693 */
694struct drm_connector_state *
695drm_atomic_get_connector_state(struct drm_atomic_state *state,
696 struct drm_connector *connector)
697{
698 int ret, index;
699 struct drm_mode_config *config = &connector->dev->mode_config;
700 struct drm_connector_state *connector_state;
701
c7eb76f4
DV
702 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
703 if (ret)
704 return ERR_PTR(ret);
705
cc4ceb48
DV
706 index = drm_connector_index(connector);
707
f52b69f1
DV
708 /*
709 * Construction of atomic state updates can race with a connector
710 * hot-add which might overflow. In this case flip the table and just
711 * restart the entire ioctl - no one is fast enough to livelock a cpu
712 * with physical hotplug events anyway.
713 *
714 * Note that we only grab the indexes once we have the right lock to
715 * prevent hotplug/unplugging of connectors. So removal is no problem,
716 * at most the array is a bit too large.
717 */
718 if (index >= state->num_connector) {
17a38d9c 719 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
fc2d2bc1 720 return ERR_PTR(-EAGAIN);
f52b69f1
DV
721 }
722
cc4ceb48
DV
723 if (state->connector_states[index])
724 return state->connector_states[index];
725
cc4ceb48
DV
726 connector_state = connector->funcs->atomic_duplicate_state(connector);
727 if (!connector_state)
728 return ERR_PTR(-ENOMEM);
729
730 state->connector_states[index] = connector_state;
731 state->connectors[index] = connector;
732 connector_state->state = state;
733
17a38d9c
DV
734 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
735 connector->base.id, connector_state, state);
cc4ceb48
DV
736
737 if (connector_state->crtc) {
738 struct drm_crtc_state *crtc_state;
739
740 crtc_state = drm_atomic_get_crtc_state(state,
741 connector_state->crtc);
742 if (IS_ERR(crtc_state))
743 return ERR_CAST(crtc_state);
744 }
745
746 return connector_state;
747}
748EXPORT_SYMBOL(drm_atomic_get_connector_state);
749
40ecc694
RC
750/**
751 * drm_atomic_connector_set_property - set property on connector.
752 * @connector: the drm connector to set a property on
753 * @state: the state object to update with the new property value
754 * @property: the property to set
755 * @val: the new property value
756 *
757 * Use this instead of calling connector->atomic_set_property directly.
758 * This function handles generic/core properties and calls out to
759 * driver's ->atomic_set_property() for driver properties. To ensure
760 * consistent behavior you must call this function rather than the
761 * driver hook directly.
762 *
763 * RETURNS:
764 * Zero on success, error code on failure
765 */
766int drm_atomic_connector_set_property(struct drm_connector *connector,
767 struct drm_connector_state *state, struct drm_property *property,
768 uint64_t val)
769{
770 struct drm_device *dev = connector->dev;
771 struct drm_mode_config *config = &dev->mode_config;
772
ae16c597
RC
773 if (property == config->prop_crtc_id) {
774 struct drm_crtc *crtc = drm_crtc_find(dev, val);
775 return drm_atomic_set_crtc_for_connector(state, crtc);
776 } else if (property == config->dpms_property) {
40ecc694
RC
777 /* setting DPMS property requires special handling, which
778 * is done in legacy setprop path for us. Disallow (for
779 * now?) atomic writes to DPMS property:
780 */
781 return -EINVAL;
782 } else if (connector->funcs->atomic_set_property) {
783 return connector->funcs->atomic_set_property(connector,
784 state, property, val);
785 } else {
786 return -EINVAL;
787 }
788}
789EXPORT_SYMBOL(drm_atomic_connector_set_property);
790
a97df1cc 791/*
ac9c9256
RC
792 * This function handles generic/core properties and calls out to
793 * driver's ->atomic_get_property() for driver properties. To ensure
794 * consistent behavior you must call this function rather than the
795 * driver hook directly.
ac9c9256 796 */
a97df1cc
DV
797static int
798drm_atomic_connector_get_property(struct drm_connector *connector,
ac9c9256
RC
799 const struct drm_connector_state *state,
800 struct drm_property *property, uint64_t *val)
801{
802 struct drm_device *dev = connector->dev;
803 struct drm_mode_config *config = &dev->mode_config;
804
ae16c597
RC
805 if (property == config->prop_crtc_id) {
806 *val = (state->crtc) ? state->crtc->base.id : 0;
807 } else if (property == config->dpms_property) {
ac9c9256
RC
808 *val = connector->dpms;
809 } else if (connector->funcs->atomic_get_property) {
810 return connector->funcs->atomic_get_property(connector,
811 state, property, val);
812 } else {
813 return -EINVAL;
814 }
815
816 return 0;
817}
ac9c9256 818
88a48e29
RC
819int drm_atomic_get_property(struct drm_mode_object *obj,
820 struct drm_property *property, uint64_t *val)
821{
822 struct drm_device *dev = property->dev;
823 int ret;
824
825 switch (obj->type) {
826 case DRM_MODE_OBJECT_CONNECTOR: {
827 struct drm_connector *connector = obj_to_connector(obj);
828 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
829 ret = drm_atomic_connector_get_property(connector,
830 connector->state, property, val);
831 break;
832 }
833 case DRM_MODE_OBJECT_CRTC: {
834 struct drm_crtc *crtc = obj_to_crtc(obj);
835 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
836 ret = drm_atomic_crtc_get_property(crtc,
837 crtc->state, property, val);
838 break;
839 }
840 case DRM_MODE_OBJECT_PLANE: {
841 struct drm_plane *plane = obj_to_plane(obj);
842 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
843 ret = drm_atomic_plane_get_property(plane,
844 plane->state, property, val);
845 break;
846 }
847 default:
848 ret = -EINVAL;
849 break;
850 }
851
852 return ret;
853}
854
cc4ceb48
DV
855/**
856 * drm_atomic_set_crtc_for_plane - set crtc for plane
07cc0ef6 857 * @plane_state: the plane whose incoming state to update
cc4ceb48
DV
858 * @crtc: crtc to use for the plane
859 *
860 * Changing the assigned crtc for a plane requires us to grab the lock and state
861 * for the new crtc, as needed. This function takes care of all these details
862 * besides updating the pointer in the state object itself.
863 *
864 * Returns:
865 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
866 * then the w/w mutex code has detected a deadlock and the entire atomic
867 * sequence must be restarted. All other errors are fatal.
868 */
869int
07cc0ef6
DV
870drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
871 struct drm_crtc *crtc)
cc4ceb48 872{
07cc0ef6 873 struct drm_plane *plane = plane_state->plane;
cc4ceb48
DV
874 struct drm_crtc_state *crtc_state;
875
6ddd388a
RC
876 if (plane_state->crtc) {
877 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
878 plane_state->crtc);
879 if (WARN_ON(IS_ERR(crtc_state)))
880 return PTR_ERR(crtc_state);
881
882 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
883 }
884
885 plane_state->crtc = crtc;
886
cc4ceb48
DV
887 if (crtc) {
888 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
889 crtc);
890 if (IS_ERR(crtc_state))
891 return PTR_ERR(crtc_state);
6ddd388a 892 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
cc4ceb48
DV
893 }
894
cc4ceb48 895 if (crtc)
17a38d9c
DV
896 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
897 plane_state, crtc->base.id);
cc4ceb48 898 else
17a38d9c
DV
899 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
900 plane_state);
cc4ceb48
DV
901
902 return 0;
903}
904EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
905
321ebf04 906/**
16d78bc2 907 * drm_atomic_set_fb_for_plane - set framebuffer for plane
321ebf04
DV
908 * @plane_state: atomic state object for the plane
909 * @fb: fb to use for the plane
910 *
911 * Changing the assigned framebuffer for a plane requires us to grab a reference
912 * to the new fb and drop the reference to the old fb, if there is one. This
913 * function takes care of all these details besides updating the pointer in the
914 * state object itself.
915 */
916void
917drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
918 struct drm_framebuffer *fb)
919{
920 if (plane_state->fb)
921 drm_framebuffer_unreference(plane_state->fb);
922 if (fb)
923 drm_framebuffer_reference(fb);
924 plane_state->fb = fb;
925
926 if (fb)
17a38d9c
DV
927 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
928 fb->base.id, plane_state);
321ebf04 929 else
17a38d9c
DV
930 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
931 plane_state);
321ebf04
DV
932}
933EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
934
cc4ceb48
DV
935/**
936 * drm_atomic_set_crtc_for_connector - set crtc for connector
937 * @conn_state: atomic state object for the connector
938 * @crtc: crtc to use for the connector
939 *
940 * Changing the assigned crtc for a connector requires us to grab the lock and
941 * state for the new crtc, as needed. This function takes care of all these
942 * details besides updating the pointer in the state object itself.
943 *
944 * Returns:
945 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
946 * then the w/w mutex code has detected a deadlock and the entire atomic
947 * sequence must be restarted. All other errors are fatal.
948 */
949int
950drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
951 struct drm_crtc *crtc)
952{
953 struct drm_crtc_state *crtc_state;
954
955 if (crtc) {
956 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
957 if (IS_ERR(crtc_state))
958 return PTR_ERR(crtc_state);
959 }
960
961 conn_state->crtc = crtc;
962
963 if (crtc)
17a38d9c
DV
964 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
965 conn_state, crtc->base.id);
cc4ceb48 966 else
17a38d9c
DV
967 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
968 conn_state);
cc4ceb48
DV
969
970 return 0;
971}
972EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
973
974/**
975 * drm_atomic_add_affected_connectors - add connectors for crtc
976 * @state: atomic state
977 * @crtc: DRM crtc
978 *
979 * This function walks the current configuration and adds all connectors
980 * currently using @crtc to the atomic configuration @state. Note that this
981 * function must acquire the connection mutex. This can potentially cause
982 * unneeded seralization if the update is just for the planes on one crtc. Hence
983 * drivers and helpers should only call this when really needed (e.g. when a
984 * full modeset needs to happen due to some change).
985 *
986 * Returns:
987 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
988 * then the w/w mutex code has detected a deadlock and the entire atomic
989 * sequence must be restarted. All other errors are fatal.
990 */
991int
992drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
993 struct drm_crtc *crtc)
994{
995 struct drm_mode_config *config = &state->dev->mode_config;
996 struct drm_connector *connector;
997 struct drm_connector_state *conn_state;
998 int ret;
999
1000 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1001 if (ret)
1002 return ret;
1003
17a38d9c
DV
1004 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1005 crtc->base.id, state);
cc4ceb48
DV
1006
1007 /*
1008 * Changed connectors are already in @state, so only need to look at the
1009 * current configuration.
1010 */
1011 list_for_each_entry(connector, &config->connector_list, head) {
1012 if (connector->state->crtc != crtc)
1013 continue;
1014
1015 conn_state = drm_atomic_get_connector_state(state, connector);
1016 if (IS_ERR(conn_state))
1017 return PTR_ERR(conn_state);
1018 }
1019
1020 return 0;
1021}
1022EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1023
e01e9f75
ML
1024/**
1025 * drm_atomic_add_affected_planes - add planes for crtc
1026 * @state: atomic state
1027 * @crtc: DRM crtc
1028 *
1029 * This function walks the current configuration and adds all planes
1030 * currently used by @crtc to the atomic configuration @state. This is useful
1031 * when an atomic commit also needs to check all currently enabled plane on
1032 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1033 * to avoid special code to force-enable all planes.
1034 *
1035 * Since acquiring a plane state will always also acquire the w/w mutex of the
1036 * current CRTC for that plane (if there is any) adding all the plane states for
1037 * a CRTC will not reduce parallism of atomic updates.
1038 *
1039 * Returns:
1040 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1041 * then the w/w mutex code has detected a deadlock and the entire atomic
1042 * sequence must be restarted. All other errors are fatal.
1043 */
1044int
1045drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1046 struct drm_crtc *crtc)
1047{
1048 struct drm_plane *plane;
1049
1050 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1051
1052 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1053 struct drm_plane_state *plane_state =
1054 drm_atomic_get_plane_state(state, plane);
1055
1056 if (IS_ERR(plane_state))
1057 return PTR_ERR(plane_state);
1058 }
1059 return 0;
1060}
1061EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1062
cc4ceb48
DV
1063/**
1064 * drm_atomic_connectors_for_crtc - count number of connected outputs
1065 * @state: atomic state
1066 * @crtc: DRM crtc
1067 *
1068 * This function counts all connectors which will be connected to @crtc
1069 * according to @state. Useful to recompute the enable state for @crtc.
1070 */
1071int
1072drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1073 struct drm_crtc *crtc)
1074{
df63b999
ACO
1075 struct drm_connector *connector;
1076 struct drm_connector_state *conn_state;
cc4ceb48 1077
df63b999 1078 int i, num_connected_connectors = 0;
cc4ceb48 1079
df63b999
ACO
1080 for_each_connector_in_state(state, connector, conn_state, i) {
1081 if (conn_state->crtc == crtc)
cc4ceb48
DV
1082 num_connected_connectors++;
1083 }
1084
17a38d9c
DV
1085 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1086 state, num_connected_connectors, crtc->base.id);
cc4ceb48
DV
1087
1088 return num_connected_connectors;
1089}
1090EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1091
1092/**
1093 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1094 * @state: atomic state
1095 *
1096 * This function should be used by legacy entry points which don't understand
1097 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
16d78bc2 1098 * the slowpath completed.
cc4ceb48
DV
1099 */
1100void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1101{
1102 int ret;
1103
1104retry:
1105 drm_modeset_backoff(state->acquire_ctx);
1106
1107 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1108 state->acquire_ctx);
1109 if (ret)
1110 goto retry;
1111 ret = drm_modeset_lock_all_crtcs(state->dev,
1112 state->acquire_ctx);
1113 if (ret)
1114 goto retry;
1115}
1116EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1117
1118/**
1119 * drm_atomic_check_only - check whether a given config would work
1120 * @state: atomic configuration to check
1121 *
1122 * Note that this function can return -EDEADLK if the driver needed to acquire
1123 * more locks but encountered a deadlock. The caller must then do the usual w/w
1124 * backoff dance and restart. All other errors are fatal.
1125 *
1126 * Returns:
1127 * 0 on success, negative error code on failure.
1128 */
1129int drm_atomic_check_only(struct drm_atomic_state *state)
1130{
5e743737
RC
1131 struct drm_device *dev = state->dev;
1132 struct drm_mode_config *config = &dev->mode_config;
df63b999
ACO
1133 struct drm_plane *plane;
1134 struct drm_plane_state *plane_state;
1135 struct drm_crtc *crtc;
1136 struct drm_crtc_state *crtc_state;
5e743737 1137 int i, ret = 0;
cc4ceb48 1138
17a38d9c 1139 DRM_DEBUG_ATOMIC("checking %p\n", state);
cc4ceb48 1140
df63b999
ACO
1141 for_each_plane_in_state(state, plane, plane_state, i) {
1142 ret = drm_atomic_plane_check(plane, plane_state);
5e743737 1143 if (ret) {
17a38d9c
DV
1144 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1145 plane->base.id);
5e743737
RC
1146 return ret;
1147 }
1148 }
1149
df63b999
ACO
1150 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1151 ret = drm_atomic_crtc_check(crtc, crtc_state);
5e743737 1152 if (ret) {
17a38d9c
DV
1153 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1154 crtc->base.id);
5e743737
RC
1155 return ret;
1156 }
1157 }
1158
cc4ceb48 1159 if (config->funcs->atomic_check)
5e743737
RC
1160 ret = config->funcs->atomic_check(state->dev, state);
1161
d34f20d6 1162 if (!state->allow_modeset) {
df63b999 1163 for_each_crtc_in_state(state, crtc, crtc_state, i) {
eab3bbef
DV
1164 if (crtc_state->mode_changed ||
1165 crtc_state->active_changed) {
17a38d9c
DV
1166 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1167 crtc->base.id);
d34f20d6
RC
1168 return -EINVAL;
1169 }
1170 }
1171 }
1172
5e743737 1173 return ret;
cc4ceb48
DV
1174}
1175EXPORT_SYMBOL(drm_atomic_check_only);
1176
1177/**
1178 * drm_atomic_commit - commit configuration atomically
1179 * @state: atomic configuration to check
1180 *
1181 * Note that this function can return -EDEADLK if the driver needed to acquire
1182 * more locks but encountered a deadlock. The caller must then do the usual w/w
1183 * backoff dance and restart. All other errors are fatal.
1184 *
1185 * Also note that on successful execution ownership of @state is transferred
1186 * from the caller of this function to the function itself. The caller must not
1187 * free or in any other way access @state. If the function fails then the caller
1188 * must clean up @state itself.
1189 *
1190 * Returns:
1191 * 0 on success, negative error code on failure.
1192 */
1193int drm_atomic_commit(struct drm_atomic_state *state)
1194{
1195 struct drm_mode_config *config = &state->dev->mode_config;
1196 int ret;
1197
1198 ret = drm_atomic_check_only(state);
1199 if (ret)
1200 return ret;
1201
17a38d9c 1202 DRM_DEBUG_ATOMIC("commiting %p\n", state);
cc4ceb48
DV
1203
1204 return config->funcs->atomic_commit(state->dev, state, false);
1205}
1206EXPORT_SYMBOL(drm_atomic_commit);
1207
1208/**
1209 * drm_atomic_async_commit - atomic&async configuration commit
1210 * @state: atomic configuration to check
1211 *
1212 * Note that this function can return -EDEADLK if the driver needed to acquire
1213 * more locks but encountered a deadlock. The caller must then do the usual w/w
1214 * backoff dance and restart. All other errors are fatal.
1215 *
1216 * Also note that on successful execution ownership of @state is transferred
1217 * from the caller of this function to the function itself. The caller must not
1218 * free or in any other way access @state. If the function fails then the caller
1219 * must clean up @state itself.
1220 *
1221 * Returns:
1222 * 0 on success, negative error code on failure.
1223 */
1224int drm_atomic_async_commit(struct drm_atomic_state *state)
1225{
1226 struct drm_mode_config *config = &state->dev->mode_config;
1227 int ret;
1228
1229 ret = drm_atomic_check_only(state);
1230 if (ret)
1231 return ret;
1232
17a38d9c 1233 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
cc4ceb48
DV
1234
1235 return config->funcs->atomic_commit(state->dev, state, true);
1236}
1237EXPORT_SYMBOL(drm_atomic_async_commit);
d34f20d6
RC
1238
1239/*
1240 * The big monstor ioctl
1241 */
1242
1243static struct drm_pending_vblank_event *create_vblank_event(
1244 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1245{
1246 struct drm_pending_vblank_event *e = NULL;
1247 unsigned long flags;
1248
1249 spin_lock_irqsave(&dev->event_lock, flags);
1250 if (file_priv->event_space < sizeof e->event) {
1251 spin_unlock_irqrestore(&dev->event_lock, flags);
1252 goto out;
1253 }
1254 file_priv->event_space -= sizeof e->event;
1255 spin_unlock_irqrestore(&dev->event_lock, flags);
1256
1257 e = kzalloc(sizeof *e, GFP_KERNEL);
1258 if (e == NULL) {
1259 spin_lock_irqsave(&dev->event_lock, flags);
1260 file_priv->event_space += sizeof e->event;
1261 spin_unlock_irqrestore(&dev->event_lock, flags);
1262 goto out;
1263 }
1264
1265 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1266 e->event.base.length = sizeof e->event;
1267 e->event.user_data = user_data;
1268 e->base.event = &e->event.base;
1269 e->base.file_priv = file_priv;
1270 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1271
1272out:
1273 return e;
1274}
1275
1276static void destroy_vblank_event(struct drm_device *dev,
1277 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1278{
1279 unsigned long flags;
1280
1281 spin_lock_irqsave(&dev->event_lock, flags);
1282 file_priv->event_space += sizeof e->event;
1283 spin_unlock_irqrestore(&dev->event_lock, flags);
1284 kfree(e);
1285}
1286
1287static int atomic_set_prop(struct drm_atomic_state *state,
1288 struct drm_mode_object *obj, struct drm_property *prop,
1289 uint64_t prop_value)
1290{
1291 struct drm_mode_object *ref;
1292 int ret;
1293
1294 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1295 return -EINVAL;
1296
1297 switch (obj->type) {
1298 case DRM_MODE_OBJECT_CONNECTOR: {
1299 struct drm_connector *connector = obj_to_connector(obj);
1300 struct drm_connector_state *connector_state;
1301
1302 connector_state = drm_atomic_get_connector_state(state, connector);
1303 if (IS_ERR(connector_state)) {
1304 ret = PTR_ERR(connector_state);
1305 break;
1306 }
1307
1308 ret = drm_atomic_connector_set_property(connector,
1309 connector_state, prop, prop_value);
1310 break;
1311 }
1312 case DRM_MODE_OBJECT_CRTC: {
1313 struct drm_crtc *crtc = obj_to_crtc(obj);
1314 struct drm_crtc_state *crtc_state;
1315
1316 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1317 if (IS_ERR(crtc_state)) {
1318 ret = PTR_ERR(crtc_state);
1319 break;
1320 }
1321
1322 ret = drm_atomic_crtc_set_property(crtc,
1323 crtc_state, prop, prop_value);
1324 break;
1325 }
1326 case DRM_MODE_OBJECT_PLANE: {
1327 struct drm_plane *plane = obj_to_plane(obj);
1328 struct drm_plane_state *plane_state;
1329
1330 plane_state = drm_atomic_get_plane_state(state, plane);
1331 if (IS_ERR(plane_state)) {
1332 ret = PTR_ERR(plane_state);
1333 break;
1334 }
1335
1336 ret = drm_atomic_plane_set_property(plane,
1337 plane_state, prop, prop_value);
1338 break;
1339 }
1340 default:
1341 ret = -EINVAL;
1342 break;
1343 }
1344
1345 drm_property_change_valid_put(prop, ref);
1346 return ret;
1347}
1348
1349int drm_mode_atomic_ioctl(struct drm_device *dev,
1350 void *data, struct drm_file *file_priv)
1351{
1352 struct drm_mode_atomic *arg = data;
1353 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1354 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1355 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1356 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1357 unsigned int copied_objs, copied_props;
1358 struct drm_atomic_state *state;
1359 struct drm_modeset_acquire_ctx ctx;
1360 struct drm_plane *plane;
df63b999
ACO
1361 struct drm_crtc *crtc;
1362 struct drm_crtc_state *crtc_state;
d34f20d6
RC
1363 unsigned plane_mask = 0;
1364 int ret = 0;
1365 unsigned int i, j;
1366
1367 /* disallow for drivers not supporting atomic: */
1368 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1369 return -EINVAL;
1370
1371 /* disallow for userspace that has not enabled atomic cap (even
1372 * though this may be a bit overkill, since legacy userspace
1373 * wouldn't know how to call this ioctl)
1374 */
1375 if (!file_priv->atomic)
1376 return -EINVAL;
1377
1378 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1379 return -EINVAL;
1380
1381 if (arg->reserved)
1382 return -EINVAL;
1383
1384 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1385 !dev->mode_config.async_page_flip)
1386 return -EINVAL;
1387
1388 /* can't test and expect an event at the same time. */
1389 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1390 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1391 return -EINVAL;
1392
1393 drm_modeset_acquire_init(&ctx, 0);
1394
1395 state = drm_atomic_state_alloc(dev);
1396 if (!state)
1397 return -ENOMEM;
1398
1399 state->acquire_ctx = &ctx;
1400 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1401
1402retry:
1403 copied_objs = 0;
1404 copied_props = 0;
1405
1406 for (i = 0; i < arg->count_objs; i++) {
1407 uint32_t obj_id, count_props;
1408 struct drm_mode_object *obj;
1409
1410 if (get_user(obj_id, objs_ptr + copied_objs)) {
1411 ret = -EFAULT;
1412 goto fail;
1413 }
1414
1415 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1416 if (!obj || !obj->properties) {
1417 ret = -ENOENT;
1418 goto fail;
1419 }
1420
1421 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1422 plane = obj_to_plane(obj);
1423 plane_mask |= (1 << drm_plane_index(plane));
1424 plane->old_fb = plane->fb;
1425 }
1426
1427 if (get_user(count_props, count_props_ptr + copied_objs)) {
1428 ret = -EFAULT;
1429 goto fail;
1430 }
1431
1432 copied_objs++;
1433
1434 for (j = 0; j < count_props; j++) {
1435 uint32_t prop_id;
1436 uint64_t prop_value;
1437 struct drm_property *prop;
1438
1439 if (get_user(prop_id, props_ptr + copied_props)) {
1440 ret = -EFAULT;
1441 goto fail;
1442 }
1443
1444 prop = drm_property_find(dev, prop_id);
1445 if (!prop) {
1446 ret = -ENOENT;
1447 goto fail;
1448 }
1449
42c5814c
GR
1450 if (copy_from_user(&prop_value,
1451 prop_values_ptr + copied_props,
1452 sizeof(prop_value))) {
d34f20d6
RC
1453 ret = -EFAULT;
1454 goto fail;
1455 }
1456
1457 ret = atomic_set_prop(state, obj, prop, prop_value);
1458 if (ret)
1459 goto fail;
1460
1461 copied_props++;
1462 }
1463 }
1464
1465 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
df63b999 1466 for_each_crtc_in_state(state, crtc, crtc_state, i) {
d34f20d6
RC
1467 struct drm_pending_vblank_event *e;
1468
d34f20d6
RC
1469 e = create_vblank_event(dev, file_priv, arg->user_data);
1470 if (!e) {
1471 ret = -ENOMEM;
1472 goto fail;
1473 }
1474
1475 crtc_state->event = e;
1476 }
1477 }
1478
1479 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1480 ret = drm_atomic_check_only(state);
1481 /* _check_only() does not free state, unlike _commit() */
1482 drm_atomic_state_free(state);
1483 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1484 ret = drm_atomic_async_commit(state);
1485 } else {
1486 ret = drm_atomic_commit(state);
1487 }
1488
1489 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1490 * locks (ie. while it is still safe to deref plane->state). We
1491 * need to do this here because the driver entry points cannot
1492 * distinguish between legacy and atomic ioctls.
1493 */
1494 drm_for_each_plane_mask(plane, dev, plane_mask) {
1495 if (ret == 0) {
1496 struct drm_framebuffer *new_fb = plane->state->fb;
1497 if (new_fb)
1498 drm_framebuffer_reference(new_fb);
1499 plane->fb = new_fb;
1500 plane->crtc = plane->state->crtc;
1501 } else {
1502 plane->old_fb = NULL;
1503 }
1504 if (plane->old_fb) {
1505 drm_framebuffer_unreference(plane->old_fb);
1506 plane->old_fb = NULL;
1507 }
1508 }
1509
1510 drm_modeset_drop_locks(&ctx);
1511 drm_modeset_acquire_fini(&ctx);
1512
1513 return ret;
1514
1515fail:
1516 if (ret == -EDEADLK)
1517 goto backoff;
1518
1519 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
df63b999 1520 for_each_crtc_in_state(state, crtc, crtc_state, i) {
d34f20d6
RC
1521 destroy_vblank_event(dev, file_priv, crtc_state->event);
1522 crtc_state->event = NULL;
1523 }
1524 }
1525
1526 drm_atomic_state_free(state);
1527
1528 drm_modeset_drop_locks(&ctx);
1529 drm_modeset_acquire_fini(&ctx);
1530
1531 return ret;
1532
1533backoff:
1534 drm_atomic_state_clear(state);
1535 drm_modeset_backoff(&ctx);
1536
1537 goto retry;
1538}