blob: dfc8aaa1457e43e638c6e253df73e07fbe757329 [file] [log] [blame]
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001/*
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
Maarten Lankhorst036ef572015-05-18 10:06:40 +020033/**
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)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020042{
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);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020049}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020050EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020051
52/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020053 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020054 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020055 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020056 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020057 * Default implementation for filling in a new atomic state.
58 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020060int
61drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020062{
Rob Clarkd34f20d2014-12-18 16:01:56 -050063 /* TODO legacy paths should maybe do a better job about
64 * setting this appropriately?
65 */
66 state->allow_modeset = true;
67
Daniel Vetterf52b69f12014-11-19 18:38:08 +010068 state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
69
Daniel Vettercc4ceb42014-07-25 21:30:38 +020070 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;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010086 state->connectors = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020087 sizeof(*state->connectors),
88 GFP_KERNEL);
89 if (!state->connectors)
90 goto fail;
Daniel Vetterf52b69f12014-11-19 18:38:08 +010091 state->connector_states = kcalloc(state->num_connector,
Daniel Vettercc4ceb42014-07-25 21:30:38 +020092 sizeof(*state->connector_states),
93 GFP_KERNEL);
94 if (!state->connector_states)
95 goto fail;
96
97 state->dev = dev;
98
Maarten Lankhorst036ef572015-05-18 10:06:40 +020099 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200100
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200101 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200102fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200103 drm_atomic_state_default_release(state);
104 return -ENOMEM;
105}
106EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200107
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200108/**
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 }
130
131 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200132}
133EXPORT_SYMBOL(drm_atomic_state_alloc);
134
135/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200136 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200137 * @state: atomic state
138 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200139 * Default implementation for clearing atomic state.
140 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200141 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200142void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200143{
144 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100145 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200146 int i;
147
Daniel Vetter17a38d92015-02-22 12:24:16 +0100148 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200149
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100150 for (i = 0; i < state->num_connector; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200151 struct drm_connector *connector = state->connectors[i];
152
153 if (!connector)
154 continue;
155
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100156 WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
157
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200158 connector->funcs->atomic_destroy_state(connector,
159 state->connector_states[i]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300160 state->connectors[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200161 state->connector_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200162 }
163
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100164 for (i = 0; i < config->num_crtc; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200165 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]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300172 state->crtcs[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200173 state->crtc_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200174 }
175
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100176 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200177 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]);
Ander Conselvan de Oliveira8a5c0bd2015-03-30 10:41:19 +0300184 state->planes[i] = NULL;
Ander Conselvan de Oliveira94692442015-01-23 09:27:59 +0200185 state->plane_states[i] = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200186 }
187}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200188EXPORT_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}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200214EXPORT_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{
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200225 struct drm_device *dev;
226 struct drm_mode_config *config;
227
Ander Conselvan de Oliveiraa0211bb2015-03-30 14:05:43 +0300228 if (!state)
229 return;
230
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200231 dev = state->dev;
232 config = &dev->mode_config;
233
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200234 drm_atomic_state_clear(state);
235
Daniel Vetter17a38d92015-02-22 12:24:16 +0100236 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200237
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200238 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 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200244}
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{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200266 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200267 struct drm_crtc_state *crtc_state;
268
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200269 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
270 if (crtc_state)
271 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200272
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
Daniel Vetter17a38d92015-02-22 12:24:16 +0100285 DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
286 crtc->base.id, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200287
288 return crtc_state;
289}
290EXPORT_SYMBOL(drm_atomic_get_crtc_state);
291
292/**
Daniel Stone819364d2015-05-26 14:36:48 +0100293 * 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{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100307 struct drm_mode_modeinfo umode;
308
Daniel Stone819364d2015-05-26 14:36:48 +0100309 /* Early return for no change. */
310 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
311 return 0;
312
Daniel Stone99cf4a22015-05-25 19:11:51 +0100313 if (state->mode_blob)
314 drm_property_unreference_blob(state->mode_blob);
315 state->mode_blob = NULL;
316
Daniel Stone819364d2015-05-26 14:36:48 +0100317 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100318 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
Daniel Stone819364d2015-05-26 14:36:48 +0100326 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
Daniel Stone819364d2015-05-26 14:36:48 +0100341/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100342 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
343 * @state: the CRTC whose incoming state to update
344 * @blob: pointer to blob property to use for mode
345 *
346 * Set a mode (originating from a blob property) on the desired CRTC state.
347 * This function will take a reference on the blob property for the CRTC state,
348 * and release the reference held on the state's existing mode property, if any
349 * was set.
350 *
351 * RETURNS:
352 * Zero on success, error code on failure. Cannot return -EDEADLK.
353 */
354int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
355 struct drm_property_blob *blob)
356{
357 if (blob == state->mode_blob)
358 return 0;
359
360 if (state->mode_blob)
361 drm_property_unreference_blob(state->mode_blob);
362 state->mode_blob = NULL;
363
364 if (blob) {
365 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
366 drm_mode_convert_umode(&state->mode,
367 (const struct drm_mode_modeinfo *)
368 blob->data))
369 return -EINVAL;
370
371 state->mode_blob = drm_property_reference_blob(blob);
372 state->enable = true;
373 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
374 state->mode.name, state);
375 } else {
376 memset(&state->mode, 0, sizeof(state->mode));
377 state->enable = false;
378 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
379 state);
380 }
381
382 return 0;
383}
384EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
385
386/**
Rob Clark40ecc692014-12-18 16:01:46 -0500387 * drm_atomic_crtc_set_property - set property on CRTC
388 * @crtc: the drm CRTC to set a property on
389 * @state: the state object to update with the new property value
390 * @property: the property to set
391 * @val: the new property value
392 *
393 * Use this instead of calling crtc->atomic_set_property directly.
394 * This function handles generic/core properties and calls out to
395 * driver's ->atomic_set_property() for driver properties. To ensure
396 * consistent behavior you must call this function rather than the
397 * driver hook directly.
398 *
399 * RETURNS:
400 * Zero on success, error code on failure
401 */
402int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
403 struct drm_crtc_state *state, struct drm_property *property,
404 uint64_t val)
405{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100406 struct drm_device *dev = crtc->dev;
407 struct drm_mode_config *config = &dev->mode_config;
Daniel Stone955f3c32015-05-25 19:11:52 +0100408 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100409
Daniel Stone27798362015-03-19 04:33:26 +0000410 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100411 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100412 else if (property == config->prop_mode_id) {
413 struct drm_property_blob *mode =
414 drm_property_lookup_blob(dev, val);
415 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
416 if (mode)
417 drm_property_unreference_blob(mode);
418 return ret;
419 }
Daniel Stone27798362015-03-19 04:33:26 +0000420 else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500421 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000422 else
423 return -EINVAL;
424
425 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500426}
427EXPORT_SYMBOL(drm_atomic_crtc_set_property);
428
Daniel Vettera97df1c2014-12-18 22:49:02 +0100429/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500430 * This function handles generic/core properties and calls out to
431 * driver's ->atomic_get_property() for driver properties. To ensure
432 * consistent behavior you must call this function rather than the
433 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500434 */
435int drm_atomic_crtc_get_property(struct drm_crtc *crtc,
436 const struct drm_crtc_state *state,
437 struct drm_property *property, uint64_t *val)
438{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000439 struct drm_device *dev = crtc->dev;
440 struct drm_mode_config *config = &dev->mode_config;
441
442 if (property == config->prop_active)
443 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100444 else if (property == config->prop_mode_id)
445 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000446 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500447 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000448 else
449 return -EINVAL;
450
451 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500452}
Rob Clarkac9c9252014-12-18 16:01:47 -0500453
454/**
Rob Clark5e743732014-12-18 16:01:51 -0500455 * drm_atomic_crtc_check - check crtc state
456 * @crtc: crtc to check
457 * @state: crtc state to check
458 *
459 * Provides core sanity checks for crtc state.
460 *
461 * RETURNS:
462 * Zero on success, error code on failure
463 */
464static int drm_atomic_crtc_check(struct drm_crtc *crtc,
465 struct drm_crtc_state *state)
466{
467 /* NOTE: we explicitly don't enforce constraints such as primary
468 * layer covering entire screen, since that is something we want
469 * to allow (on hw that supports it). For hw that does not, it
470 * should be checked in driver's crtc->atomic_check() vfunc.
471 *
472 * TODO: Add generic modeset state checks once we support those.
473 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100474
475 if (state->active && !state->enable) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100476 DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
477 crtc->base.id);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100478 return -EINVAL;
479 }
480
Daniel Stone99cf4a22015-05-25 19:11:51 +0100481 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
482 * as this is a kernel-internal detail that userspace should never
483 * be able to trigger. */
484 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
485 WARN_ON(state->enable && !state->mode_blob)) {
486 DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
487 crtc->base.id);
488 return -EINVAL;
489 }
490
491 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
492 WARN_ON(!state->enable && state->mode_blob)) {
493 DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
494 crtc->base.id);
495 return -EINVAL;
496 }
497
Rob Clark5e743732014-12-18 16:01:51 -0500498 return 0;
499}
500
501/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200502 * drm_atomic_get_plane_state - get plane state
503 * @state: global atomic state object
504 * @plane: plane to get state object for
505 *
506 * This function returns the plane state for the given plane, allocating it if
507 * needed. It will also grab the relevant plane lock to make sure that the state
508 * is consistent.
509 *
510 * Returns:
511 *
512 * Either the allocated state or the error code encoded into the pointer. When
513 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
514 * entire atomic sequence must be restarted. All other errors are fatal.
515 */
516struct drm_plane_state *
517drm_atomic_get_plane_state(struct drm_atomic_state *state,
518 struct drm_plane *plane)
519{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200520 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200521 struct drm_plane_state *plane_state;
522
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200523 plane_state = drm_atomic_get_existing_plane_state(state, plane);
524 if (plane_state)
525 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200526
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100527 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200528 if (ret)
529 return ERR_PTR(ret);
530
531 plane_state = plane->funcs->atomic_duplicate_state(plane);
532 if (!plane_state)
533 return ERR_PTR(-ENOMEM);
534
535 state->plane_states[index] = plane_state;
536 state->planes[index] = plane;
537 plane_state->state = state;
538
Daniel Vetter17a38d92015-02-22 12:24:16 +0100539 DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
540 plane->base.id, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200541
542 if (plane_state->crtc) {
543 struct drm_crtc_state *crtc_state;
544
545 crtc_state = drm_atomic_get_crtc_state(state,
546 plane_state->crtc);
547 if (IS_ERR(crtc_state))
548 return ERR_CAST(crtc_state);
549 }
550
551 return plane_state;
552}
553EXPORT_SYMBOL(drm_atomic_get_plane_state);
554
555/**
Rob Clark40ecc692014-12-18 16:01:46 -0500556 * drm_atomic_plane_set_property - set property on plane
557 * @plane: the drm plane to set a property on
558 * @state: the state object to update with the new property value
559 * @property: the property to set
560 * @val: the new property value
561 *
562 * Use this instead of calling plane->atomic_set_property directly.
563 * This function handles generic/core properties and calls out to
564 * driver's ->atomic_set_property() for driver properties. To ensure
565 * consistent behavior you must call this function rather than the
566 * driver hook directly.
567 *
568 * RETURNS:
569 * Zero on success, error code on failure
570 */
571int drm_atomic_plane_set_property(struct drm_plane *plane,
572 struct drm_plane_state *state, struct drm_property *property,
573 uint64_t val)
574{
Rob Clark6b4959f2014-12-18 16:01:53 -0500575 struct drm_device *dev = plane->dev;
576 struct drm_mode_config *config = &dev->mode_config;
577
578 if (property == config->prop_fb_id) {
579 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
580 drm_atomic_set_fb_for_plane(state, fb);
581 if (fb)
582 drm_framebuffer_unreference(fb);
583 } else if (property == config->prop_crtc_id) {
584 struct drm_crtc *crtc = drm_crtc_find(dev, val);
585 return drm_atomic_set_crtc_for_plane(state, crtc);
586 } else if (property == config->prop_crtc_x) {
587 state->crtc_x = U642I64(val);
588 } else if (property == config->prop_crtc_y) {
589 state->crtc_y = U642I64(val);
590 } else if (property == config->prop_crtc_w) {
591 state->crtc_w = val;
592 } else if (property == config->prop_crtc_h) {
593 state->crtc_h = val;
594 } else if (property == config->prop_src_x) {
595 state->src_x = val;
596 } else if (property == config->prop_src_y) {
597 state->src_y = val;
598 } else if (property == config->prop_src_w) {
599 state->src_w = val;
600 } else if (property == config->prop_src_h) {
601 state->src_h = val;
Matt Roper1da30622015-01-21 16:35:40 -0800602 } else if (property == config->rotation_property) {
603 state->rotation = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500604 } else if (plane->funcs->atomic_set_property) {
605 return plane->funcs->atomic_set_property(plane, state,
606 property, val);
607 } else {
608 return -EINVAL;
609 }
610
611 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500612}
613EXPORT_SYMBOL(drm_atomic_plane_set_property);
614
Daniel Vettera97df1c2014-12-18 22:49:02 +0100615/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500616 * This function handles generic/core properties and calls out to
617 * driver's ->atomic_get_property() for driver properties. To ensure
618 * consistent behavior you must call this function rather than the
619 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500620 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100621static int
622drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500623 const struct drm_plane_state *state,
624 struct drm_property *property, uint64_t *val)
625{
Rob Clark6b4959f2014-12-18 16:01:53 -0500626 struct drm_device *dev = plane->dev;
627 struct drm_mode_config *config = &dev->mode_config;
628
629 if (property == config->prop_fb_id) {
630 *val = (state->fb) ? state->fb->base.id : 0;
631 } else if (property == config->prop_crtc_id) {
632 *val = (state->crtc) ? state->crtc->base.id : 0;
633 } else if (property == config->prop_crtc_x) {
634 *val = I642U64(state->crtc_x);
635 } else if (property == config->prop_crtc_y) {
636 *val = I642U64(state->crtc_y);
637 } else if (property == config->prop_crtc_w) {
638 *val = state->crtc_w;
639 } else if (property == config->prop_crtc_h) {
640 *val = state->crtc_h;
641 } else if (property == config->prop_src_x) {
642 *val = state->src_x;
643 } else if (property == config->prop_src_y) {
644 *val = state->src_y;
645 } else if (property == config->prop_src_w) {
646 *val = state->src_w;
647 } else if (property == config->prop_src_h) {
648 *val = state->src_h;
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000649 } else if (property == config->rotation_property) {
650 *val = state->rotation;
Rob Clark6b4959f2014-12-18 16:01:53 -0500651 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500652 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500653 } else {
654 return -EINVAL;
655 }
656
657 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500658}
Rob Clarkac9c9252014-12-18 16:01:47 -0500659
660/**
Rob Clark5e743732014-12-18 16:01:51 -0500661 * drm_atomic_plane_check - check plane state
662 * @plane: plane to check
663 * @state: plane state to check
664 *
665 * Provides core sanity checks for plane state.
666 *
667 * RETURNS:
668 * Zero on success, error code on failure
669 */
670static int drm_atomic_plane_check(struct drm_plane *plane,
671 struct drm_plane_state *state)
672{
673 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200674 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500675
676 /* either *both* CRTC and FB must be set, or neither */
677 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100678 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500679 return -EINVAL;
680 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100681 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500682 return -EINVAL;
683 }
684
685 /* if disabled, we don't care about the rest of the state: */
686 if (!state->crtc)
687 return 0;
688
689 /* Check whether this plane is usable on this CRTC */
690 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100691 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500692 return -EINVAL;
693 }
694
695 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200696 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
697 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100698 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
699 drm_get_format_name(state->fb->pixel_format));
Laurent Pinchartead86102015-03-05 02:25:43 +0200700 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500701 }
702
703 /* Give drivers some help against integer overflows */
704 if (state->crtc_w > INT_MAX ||
705 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
706 state->crtc_h > INT_MAX ||
707 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100708 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
709 state->crtc_w, state->crtc_h,
710 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500711 return -ERANGE;
712 }
713
714 fb_width = state->fb->width << 16;
715 fb_height = state->fb->height << 16;
716
717 /* Make sure source coordinates are inside the fb. */
718 if (state->src_w > fb_width ||
719 state->src_x > fb_width - state->src_w ||
720 state->src_h > fb_height ||
721 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100722 DRM_DEBUG_ATOMIC("Invalid source coordinates "
723 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
724 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
725 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
726 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
727 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500728 return -ENOSPC;
729 }
730
731 return 0;
732}
733
734/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200735 * drm_atomic_get_connector_state - get connector state
736 * @state: global atomic state object
737 * @connector: connector to get state object for
738 *
739 * This function returns the connector state for the given connector,
740 * allocating it if needed. It will also grab the relevant connector lock to
741 * make sure that the state is consistent.
742 *
743 * Returns:
744 *
745 * Either the allocated state or the error code encoded into the pointer. When
746 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
747 * entire atomic sequence must be restarted. All other errors are fatal.
748 */
749struct drm_connector_state *
750drm_atomic_get_connector_state(struct drm_atomic_state *state,
751 struct drm_connector *connector)
752{
753 int ret, index;
754 struct drm_mode_config *config = &connector->dev->mode_config;
755 struct drm_connector_state *connector_state;
756
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100757 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
758 if (ret)
759 return ERR_PTR(ret);
760
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200761 index = drm_connector_index(connector);
762
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100763 /*
764 * Construction of atomic state updates can race with a connector
765 * hot-add which might overflow. In this case flip the table and just
766 * restart the entire ioctl - no one is fast enough to livelock a cpu
767 * with physical hotplug events anyway.
768 *
769 * Note that we only grab the indexes once we have the right lock to
770 * prevent hotplug/unplugging of connectors. So removal is no problem,
771 * at most the array is a bit too large.
772 */
773 if (index >= state->num_connector) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100774 DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
Daniel Vetterfc2d2bc2014-11-20 09:53:35 +0100775 return ERR_PTR(-EAGAIN);
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100776 }
777
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200778 if (state->connector_states[index])
779 return state->connector_states[index];
780
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200781 connector_state = connector->funcs->atomic_duplicate_state(connector);
782 if (!connector_state)
783 return ERR_PTR(-ENOMEM);
784
785 state->connector_states[index] = connector_state;
786 state->connectors[index] = connector;
787 connector_state->state = state;
788
Daniel Vetter17a38d92015-02-22 12:24:16 +0100789 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
790 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200791
792 if (connector_state->crtc) {
793 struct drm_crtc_state *crtc_state;
794
795 crtc_state = drm_atomic_get_crtc_state(state,
796 connector_state->crtc);
797 if (IS_ERR(crtc_state))
798 return ERR_CAST(crtc_state);
799 }
800
801 return connector_state;
802}
803EXPORT_SYMBOL(drm_atomic_get_connector_state);
804
805/**
Rob Clark40ecc692014-12-18 16:01:46 -0500806 * drm_atomic_connector_set_property - set property on connector.
807 * @connector: the drm connector to set a property on
808 * @state: the state object to update with the new property value
809 * @property: the property to set
810 * @val: the new property value
811 *
812 * Use this instead of calling connector->atomic_set_property directly.
813 * This function handles generic/core properties and calls out to
814 * driver's ->atomic_set_property() for driver properties. To ensure
815 * consistent behavior you must call this function rather than the
816 * driver hook directly.
817 *
818 * RETURNS:
819 * Zero on success, error code on failure
820 */
821int drm_atomic_connector_set_property(struct drm_connector *connector,
822 struct drm_connector_state *state, struct drm_property *property,
823 uint64_t val)
824{
825 struct drm_device *dev = connector->dev;
826 struct drm_mode_config *config = &dev->mode_config;
827
Rob Clarkae16c592014-12-18 16:01:54 -0500828 if (property == config->prop_crtc_id) {
829 struct drm_crtc *crtc = drm_crtc_find(dev, val);
830 return drm_atomic_set_crtc_for_connector(state, crtc);
831 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -0500832 /* setting DPMS property requires special handling, which
833 * is done in legacy setprop path for us. Disallow (for
834 * now?) atomic writes to DPMS property:
835 */
836 return -EINVAL;
837 } else if (connector->funcs->atomic_set_property) {
838 return connector->funcs->atomic_set_property(connector,
839 state, property, val);
840 } else {
841 return -EINVAL;
842 }
843}
844EXPORT_SYMBOL(drm_atomic_connector_set_property);
845
Daniel Vettera97df1c2014-12-18 22:49:02 +0100846/*
Rob Clarkac9c9252014-12-18 16:01:47 -0500847 * This function handles generic/core properties and calls out to
848 * driver's ->atomic_get_property() for driver properties. To ensure
849 * consistent behavior you must call this function rather than the
850 * driver hook directly.
Rob Clarkac9c9252014-12-18 16:01:47 -0500851 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100852static int
853drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -0500854 const struct drm_connector_state *state,
855 struct drm_property *property, uint64_t *val)
856{
857 struct drm_device *dev = connector->dev;
858 struct drm_mode_config *config = &dev->mode_config;
859
Rob Clarkae16c592014-12-18 16:01:54 -0500860 if (property == config->prop_crtc_id) {
861 *val = (state->crtc) ? state->crtc->base.id : 0;
862 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500863 *val = connector->dpms;
864 } else if (connector->funcs->atomic_get_property) {
865 return connector->funcs->atomic_get_property(connector,
866 state, property, val);
867 } else {
868 return -EINVAL;
869 }
870
871 return 0;
872}
Rob Clarkac9c9252014-12-18 16:01:47 -0500873
Rob Clark88a48e22014-12-18 16:01:50 -0500874int drm_atomic_get_property(struct drm_mode_object *obj,
875 struct drm_property *property, uint64_t *val)
876{
877 struct drm_device *dev = property->dev;
878 int ret;
879
880 switch (obj->type) {
881 case DRM_MODE_OBJECT_CONNECTOR: {
882 struct drm_connector *connector = obj_to_connector(obj);
883 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
884 ret = drm_atomic_connector_get_property(connector,
885 connector->state, property, val);
886 break;
887 }
888 case DRM_MODE_OBJECT_CRTC: {
889 struct drm_crtc *crtc = obj_to_crtc(obj);
890 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
891 ret = drm_atomic_crtc_get_property(crtc,
892 crtc->state, property, val);
893 break;
894 }
895 case DRM_MODE_OBJECT_PLANE: {
896 struct drm_plane *plane = obj_to_plane(obj);
897 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
898 ret = drm_atomic_plane_get_property(plane,
899 plane->state, property, val);
900 break;
901 }
902 default:
903 ret = -EINVAL;
904 break;
905 }
906
907 return ret;
908}
909
910/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200911 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100912 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200913 * @crtc: crtc to use for the plane
914 *
915 * Changing the assigned crtc for a plane requires us to grab the lock and state
916 * for the new crtc, as needed. This function takes care of all these details
917 * besides updating the pointer in the state object itself.
918 *
919 * Returns:
920 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
921 * then the w/w mutex code has detected a deadlock and the entire atomic
922 * sequence must be restarted. All other errors are fatal.
923 */
924int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100925drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
926 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200927{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +0100928 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200929 struct drm_crtc_state *crtc_state;
930
Rob Clark6ddd3882014-11-21 15:28:31 -0500931 if (plane_state->crtc) {
932 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
933 plane_state->crtc);
934 if (WARN_ON(IS_ERR(crtc_state)))
935 return PTR_ERR(crtc_state);
936
937 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
938 }
939
940 plane_state->crtc = crtc;
941
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200942 if (crtc) {
943 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
944 crtc);
945 if (IS_ERR(crtc_state))
946 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -0500947 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200948 }
949
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200950 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100951 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
952 plane_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200953 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100954 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
955 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200956
957 return 0;
958}
959EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
960
961/**
John Hunter16d78bc2e2015-04-07 19:38:50 +0800962 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +0100963 * @plane_state: atomic state object for the plane
964 * @fb: fb to use for the plane
965 *
966 * Changing the assigned framebuffer for a plane requires us to grab a reference
967 * to the new fb and drop the reference to the old fb, if there is one. This
968 * function takes care of all these details besides updating the pointer in the
969 * state object itself.
970 */
971void
972drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
973 struct drm_framebuffer *fb)
974{
975 if (plane_state->fb)
976 drm_framebuffer_unreference(plane_state->fb);
977 if (fb)
978 drm_framebuffer_reference(fb);
979 plane_state->fb = fb;
980
981 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +0100982 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
983 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100984 else
Daniel Vetter17a38d92015-02-22 12:24:16 +0100985 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
986 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +0100987}
988EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
989
990/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200991 * drm_atomic_set_crtc_for_connector - set crtc for connector
992 * @conn_state: atomic state object for the connector
993 * @crtc: crtc to use for the connector
994 *
995 * Changing the assigned crtc for a connector requires us to grab the lock and
996 * state for the new crtc, as needed. This function takes care of all these
997 * details besides updating the pointer in the state object itself.
998 *
999 * Returns:
1000 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1001 * then the w/w mutex code has detected a deadlock and the entire atomic
1002 * sequence must be restarted. All other errors are fatal.
1003 */
1004int
1005drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1006 struct drm_crtc *crtc)
1007{
1008 struct drm_crtc_state *crtc_state;
1009
1010 if (crtc) {
1011 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1012 if (IS_ERR(crtc_state))
1013 return PTR_ERR(crtc_state);
1014 }
1015
1016 conn_state->crtc = crtc;
1017
1018 if (crtc)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001019 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
1020 conn_state, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001021 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001022 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1023 conn_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001024
1025 return 0;
1026}
1027EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1028
1029/**
1030 * drm_atomic_add_affected_connectors - add connectors for crtc
1031 * @state: atomic state
1032 * @crtc: DRM crtc
1033 *
1034 * This function walks the current configuration and adds all connectors
1035 * currently using @crtc to the atomic configuration @state. Note that this
1036 * function must acquire the connection mutex. This can potentially cause
1037 * unneeded seralization if the update is just for the planes on one crtc. Hence
1038 * drivers and helpers should only call this when really needed (e.g. when a
1039 * full modeset needs to happen due to some change).
1040 *
1041 * Returns:
1042 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1043 * then the w/w mutex code has detected a deadlock and the entire atomic
1044 * sequence must be restarted. All other errors are fatal.
1045 */
1046int
1047drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1048 struct drm_crtc *crtc)
1049{
1050 struct drm_mode_config *config = &state->dev->mode_config;
1051 struct drm_connector *connector;
1052 struct drm_connector_state *conn_state;
1053 int ret;
1054
1055 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1056 if (ret)
1057 return ret;
1058
Daniel Vetter17a38d92015-02-22 12:24:16 +01001059 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1060 crtc->base.id, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001061
1062 /*
1063 * Changed connectors are already in @state, so only need to look at the
1064 * current configuration.
1065 */
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001066 drm_for_each_connector(connector, state->dev) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001067 if (connector->state->crtc != crtc)
1068 continue;
1069
1070 conn_state = drm_atomic_get_connector_state(state, connector);
1071 if (IS_ERR(conn_state))
1072 return PTR_ERR(conn_state);
1073 }
1074
1075 return 0;
1076}
1077EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1078
1079/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001080 * drm_atomic_add_affected_planes - add planes for crtc
1081 * @state: atomic state
1082 * @crtc: DRM crtc
1083 *
1084 * This function walks the current configuration and adds all planes
1085 * currently used by @crtc to the atomic configuration @state. This is useful
1086 * when an atomic commit also needs to check all currently enabled plane on
1087 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1088 * to avoid special code to force-enable all planes.
1089 *
1090 * Since acquiring a plane state will always also acquire the w/w mutex of the
1091 * current CRTC for that plane (if there is any) adding all the plane states for
1092 * a CRTC will not reduce parallism of atomic updates.
1093 *
1094 * Returns:
1095 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1096 * then the w/w mutex code has detected a deadlock and the entire atomic
1097 * sequence must be restarted. All other errors are fatal.
1098 */
1099int
1100drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1101 struct drm_crtc *crtc)
1102{
1103 struct drm_plane *plane;
1104
1105 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1106
1107 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1108 struct drm_plane_state *plane_state =
1109 drm_atomic_get_plane_state(state, plane);
1110
1111 if (IS_ERR(plane_state))
1112 return PTR_ERR(plane_state);
1113 }
1114 return 0;
1115}
1116EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1117
1118/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001119 * drm_atomic_connectors_for_crtc - count number of connected outputs
1120 * @state: atomic state
1121 * @crtc: DRM crtc
1122 *
1123 * This function counts all connectors which will be connected to @crtc
1124 * according to @state. Useful to recompute the enable state for @crtc.
1125 */
1126int
1127drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1128 struct drm_crtc *crtc)
1129{
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001130 struct drm_connector *connector;
1131 struct drm_connector_state *conn_state;
1132
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001133 int i, num_connected_connectors = 0;
1134
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001135 for_each_connector_in_state(state, connector, conn_state, i) {
1136 if (conn_state->crtc == crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001137 num_connected_connectors++;
1138 }
1139
Daniel Vetter17a38d92015-02-22 12:24:16 +01001140 DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1141 state, num_connected_connectors, crtc->base.id);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001142
1143 return num_connected_connectors;
1144}
1145EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1146
1147/**
1148 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1149 * @state: atomic state
1150 *
1151 * This function should be used by legacy entry points which don't understand
1152 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001153 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001154 */
1155void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1156{
1157 int ret;
1158
1159retry:
1160 drm_modeset_backoff(state->acquire_ctx);
1161
1162 ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1163 state->acquire_ctx);
1164 if (ret)
1165 goto retry;
1166 ret = drm_modeset_lock_all_crtcs(state->dev,
1167 state->acquire_ctx);
1168 if (ret)
1169 goto retry;
1170}
1171EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1172
1173/**
1174 * drm_atomic_check_only - check whether a given config would work
1175 * @state: atomic configuration to check
1176 *
1177 * Note that this function can return -EDEADLK if the driver needed to acquire
1178 * more locks but encountered a deadlock. The caller must then do the usual w/w
1179 * backoff dance and restart. All other errors are fatal.
1180 *
1181 * Returns:
1182 * 0 on success, negative error code on failure.
1183 */
1184int drm_atomic_check_only(struct drm_atomic_state *state)
1185{
Rob Clark5e743732014-12-18 16:01:51 -05001186 struct drm_device *dev = state->dev;
1187 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001188 struct drm_plane *plane;
1189 struct drm_plane_state *plane_state;
1190 struct drm_crtc *crtc;
1191 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001192 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001193
Daniel Vetter17a38d92015-02-22 12:24:16 +01001194 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001195
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001196 for_each_plane_in_state(state, plane, plane_state, i) {
1197 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001198 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001199 DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1200 plane->base.id);
Rob Clark5e743732014-12-18 16:01:51 -05001201 return ret;
1202 }
1203 }
1204
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001205 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1206 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001207 if (ret) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001208 DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1209 crtc->base.id);
Rob Clark5e743732014-12-18 16:01:51 -05001210 return ret;
1211 }
1212 }
1213
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001214 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001215 ret = config->funcs->atomic_check(state->dev, state);
1216
Rob Clarkd34f20d2014-12-18 16:01:56 -05001217 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001218 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001219 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001220 DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1221 crtc->base.id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001222 return -EINVAL;
1223 }
1224 }
1225 }
1226
Rob Clark5e743732014-12-18 16:01:51 -05001227 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001228}
1229EXPORT_SYMBOL(drm_atomic_check_only);
1230
1231/**
1232 * drm_atomic_commit - commit configuration atomically
1233 * @state: atomic configuration to check
1234 *
1235 * Note that this function can return -EDEADLK if the driver needed to acquire
1236 * more locks but encountered a deadlock. The caller must then do the usual w/w
1237 * backoff dance and restart. All other errors are fatal.
1238 *
1239 * Also note that on successful execution ownership of @state is transferred
1240 * from the caller of this function to the function itself. The caller must not
1241 * free or in any other way access @state. If the function fails then the caller
1242 * must clean up @state itself.
1243 *
1244 * Returns:
1245 * 0 on success, negative error code on failure.
1246 */
1247int drm_atomic_commit(struct drm_atomic_state *state)
1248{
1249 struct drm_mode_config *config = &state->dev->mode_config;
1250 int ret;
1251
1252 ret = drm_atomic_check_only(state);
1253 if (ret)
1254 return ret;
1255
Daniel Vetter17a38d92015-02-22 12:24:16 +01001256 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001257
1258 return config->funcs->atomic_commit(state->dev, state, false);
1259}
1260EXPORT_SYMBOL(drm_atomic_commit);
1261
1262/**
1263 * drm_atomic_async_commit - atomic&async configuration commit
1264 * @state: atomic configuration to check
1265 *
1266 * Note that this function can return -EDEADLK if the driver needed to acquire
1267 * more locks but encountered a deadlock. The caller must then do the usual w/w
1268 * backoff dance and restart. All other errors are fatal.
1269 *
1270 * Also note that on successful execution ownership of @state is transferred
1271 * from the caller of this function to the function itself. The caller must not
1272 * free or in any other way access @state. If the function fails then the caller
1273 * must clean up @state itself.
1274 *
1275 * Returns:
1276 * 0 on success, negative error code on failure.
1277 */
1278int drm_atomic_async_commit(struct drm_atomic_state *state)
1279{
1280 struct drm_mode_config *config = &state->dev->mode_config;
1281 int ret;
1282
1283 ret = drm_atomic_check_only(state);
1284 if (ret)
1285 return ret;
1286
Daniel Vetter17a38d92015-02-22 12:24:16 +01001287 DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001288
1289 return config->funcs->atomic_commit(state->dev, state, true);
1290}
1291EXPORT_SYMBOL(drm_atomic_async_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001292
1293/*
1294 * The big monstor ioctl
1295 */
1296
1297static struct drm_pending_vblank_event *create_vblank_event(
1298 struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1299{
1300 struct drm_pending_vblank_event *e = NULL;
1301 unsigned long flags;
1302
1303 spin_lock_irqsave(&dev->event_lock, flags);
1304 if (file_priv->event_space < sizeof e->event) {
1305 spin_unlock_irqrestore(&dev->event_lock, flags);
1306 goto out;
1307 }
1308 file_priv->event_space -= sizeof e->event;
1309 spin_unlock_irqrestore(&dev->event_lock, flags);
1310
1311 e = kzalloc(sizeof *e, GFP_KERNEL);
1312 if (e == NULL) {
1313 spin_lock_irqsave(&dev->event_lock, flags);
1314 file_priv->event_space += sizeof e->event;
1315 spin_unlock_irqrestore(&dev->event_lock, flags);
1316 goto out;
1317 }
1318
1319 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1320 e->event.base.length = sizeof e->event;
1321 e->event.user_data = user_data;
1322 e->base.event = &e->event.base;
1323 e->base.file_priv = file_priv;
1324 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1325
1326out:
1327 return e;
1328}
1329
1330static void destroy_vblank_event(struct drm_device *dev,
1331 struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1332{
1333 unsigned long flags;
1334
1335 spin_lock_irqsave(&dev->event_lock, flags);
1336 file_priv->event_space += sizeof e->event;
1337 spin_unlock_irqrestore(&dev->event_lock, flags);
1338 kfree(e);
1339}
1340
1341static int atomic_set_prop(struct drm_atomic_state *state,
1342 struct drm_mode_object *obj, struct drm_property *prop,
1343 uint64_t prop_value)
1344{
1345 struct drm_mode_object *ref;
1346 int ret;
1347
1348 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1349 return -EINVAL;
1350
1351 switch (obj->type) {
1352 case DRM_MODE_OBJECT_CONNECTOR: {
1353 struct drm_connector *connector = obj_to_connector(obj);
1354 struct drm_connector_state *connector_state;
1355
1356 connector_state = drm_atomic_get_connector_state(state, connector);
1357 if (IS_ERR(connector_state)) {
1358 ret = PTR_ERR(connector_state);
1359 break;
1360 }
1361
1362 ret = drm_atomic_connector_set_property(connector,
1363 connector_state, prop, prop_value);
1364 break;
1365 }
1366 case DRM_MODE_OBJECT_CRTC: {
1367 struct drm_crtc *crtc = obj_to_crtc(obj);
1368 struct drm_crtc_state *crtc_state;
1369
1370 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1371 if (IS_ERR(crtc_state)) {
1372 ret = PTR_ERR(crtc_state);
1373 break;
1374 }
1375
1376 ret = drm_atomic_crtc_set_property(crtc,
1377 crtc_state, prop, prop_value);
1378 break;
1379 }
1380 case DRM_MODE_OBJECT_PLANE: {
1381 struct drm_plane *plane = obj_to_plane(obj);
1382 struct drm_plane_state *plane_state;
1383
1384 plane_state = drm_atomic_get_plane_state(state, plane);
1385 if (IS_ERR(plane_state)) {
1386 ret = PTR_ERR(plane_state);
1387 break;
1388 }
1389
1390 ret = drm_atomic_plane_set_property(plane,
1391 plane_state, prop, prop_value);
1392 break;
1393 }
1394 default:
1395 ret = -EINVAL;
1396 break;
1397 }
1398
1399 drm_property_change_valid_put(prop, ref);
1400 return ret;
1401}
1402
1403int drm_mode_atomic_ioctl(struct drm_device *dev,
1404 void *data, struct drm_file *file_priv)
1405{
1406 struct drm_mode_atomic *arg = data;
1407 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1408 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1409 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1410 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1411 unsigned int copied_objs, copied_props;
1412 struct drm_atomic_state *state;
1413 struct drm_modeset_acquire_ctx ctx;
1414 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001415 struct drm_crtc *crtc;
1416 struct drm_crtc_state *crtc_state;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001417 unsigned plane_mask = 0;
1418 int ret = 0;
1419 unsigned int i, j;
1420
1421 /* disallow for drivers not supporting atomic: */
1422 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1423 return -EINVAL;
1424
1425 /* disallow for userspace that has not enabled atomic cap (even
1426 * though this may be a bit overkill, since legacy userspace
1427 * wouldn't know how to call this ioctl)
1428 */
1429 if (!file_priv->atomic)
1430 return -EINVAL;
1431
1432 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1433 return -EINVAL;
1434
1435 if (arg->reserved)
1436 return -EINVAL;
1437
1438 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1439 !dev->mode_config.async_page_flip)
1440 return -EINVAL;
1441
1442 /* can't test and expect an event at the same time. */
1443 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1444 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1445 return -EINVAL;
1446
1447 drm_modeset_acquire_init(&ctx, 0);
1448
1449 state = drm_atomic_state_alloc(dev);
1450 if (!state)
1451 return -ENOMEM;
1452
1453 state->acquire_ctx = &ctx;
1454 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1455
1456retry:
1457 copied_objs = 0;
1458 copied_props = 0;
1459
1460 for (i = 0; i < arg->count_objs; i++) {
1461 uint32_t obj_id, count_props;
1462 struct drm_mode_object *obj;
1463
1464 if (get_user(obj_id, objs_ptr + copied_objs)) {
1465 ret = -EFAULT;
1466 goto fail;
1467 }
1468
1469 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
1470 if (!obj || !obj->properties) {
1471 ret = -ENOENT;
1472 goto fail;
1473 }
1474
1475 if (obj->type == DRM_MODE_OBJECT_PLANE) {
1476 plane = obj_to_plane(obj);
1477 plane_mask |= (1 << drm_plane_index(plane));
1478 plane->old_fb = plane->fb;
1479 }
1480
1481 if (get_user(count_props, count_props_ptr + copied_objs)) {
1482 ret = -EFAULT;
1483 goto fail;
1484 }
1485
1486 copied_objs++;
1487
1488 for (j = 0; j < count_props; j++) {
1489 uint32_t prop_id;
1490 uint64_t prop_value;
1491 struct drm_property *prop;
1492
1493 if (get_user(prop_id, props_ptr + copied_props)) {
1494 ret = -EFAULT;
1495 goto fail;
1496 }
1497
1498 prop = drm_property_find(dev, prop_id);
1499 if (!prop) {
1500 ret = -ENOENT;
1501 goto fail;
1502 }
1503
Guenter Roeck42c58142015-01-12 21:12:17 -08001504 if (copy_from_user(&prop_value,
1505 prop_values_ptr + copied_props,
1506 sizeof(prop_value))) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001507 ret = -EFAULT;
1508 goto fail;
1509 }
1510
1511 ret = atomic_set_prop(state, obj, prop, prop_value);
1512 if (ret)
1513 goto fail;
1514
1515 copied_props++;
1516 }
1517 }
1518
1519 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001520 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001521 struct drm_pending_vblank_event *e;
1522
Rob Clarkd34f20d2014-12-18 16:01:56 -05001523 e = create_vblank_event(dev, file_priv, arg->user_data);
1524 if (!e) {
1525 ret = -ENOMEM;
1526 goto fail;
1527 }
1528
1529 crtc_state->event = e;
1530 }
1531 }
1532
1533 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1534 ret = drm_atomic_check_only(state);
1535 /* _check_only() does not free state, unlike _commit() */
1536 drm_atomic_state_free(state);
1537 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1538 ret = drm_atomic_async_commit(state);
1539 } else {
1540 ret = drm_atomic_commit(state);
1541 }
1542
1543 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1544 * locks (ie. while it is still safe to deref plane->state). We
1545 * need to do this here because the driver entry points cannot
1546 * distinguish between legacy and atomic ioctls.
1547 */
1548 drm_for_each_plane_mask(plane, dev, plane_mask) {
1549 if (ret == 0) {
1550 struct drm_framebuffer *new_fb = plane->state->fb;
1551 if (new_fb)
1552 drm_framebuffer_reference(new_fb);
1553 plane->fb = new_fb;
1554 plane->crtc = plane->state->crtc;
1555 } else {
1556 plane->old_fb = NULL;
1557 }
1558 if (plane->old_fb) {
1559 drm_framebuffer_unreference(plane->old_fb);
1560 plane->old_fb = NULL;
1561 }
1562 }
1563
1564 drm_modeset_drop_locks(&ctx);
1565 drm_modeset_acquire_fini(&ctx);
1566
1567 return ret;
1568
1569fail:
1570 if (ret == -EDEADLK)
1571 goto backoff;
1572
1573 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001574 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001575 destroy_vblank_event(dev, file_priv, crtc_state->event);
1576 crtc_state->event = NULL;
1577 }
1578 }
1579
1580 drm_atomic_state_free(state);
1581
1582 drm_modeset_drop_locks(&ctx);
1583 drm_modeset_acquire_fini(&ctx);
1584
1585 return ret;
1586
1587backoff:
1588 drm_atomic_state_clear(state);
1589 drm_modeset_backoff(&ctx);
1590
1591 goto retry;
1592}