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