blob: f5ea7db4a48a28ceca45deeb3cbf63d7798bc2d6 [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>
Lionel Landwerlin5488dc12016-02-26 17:05:00 +000031#include <drm/drm_mode.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020032#include <drm/drm_plane_helper.h>
Rob Clarkfceffb322016-11-05 11:08:09 -040033#include <drm/drm_print.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020034
Thierry Redingbe35f942016-04-28 15:19:56 +020035#include "drm_crtc_internal.h"
36
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020037static void crtc_commit_free(struct kref *kref)
38{
39 struct drm_crtc_commit *commit =
40 container_of(kref, struct drm_crtc_commit, ref);
41
42 kfree(commit);
43}
44
45void drm_crtc_commit_put(struct drm_crtc_commit *commit)
46{
47 kref_put(&commit->ref, crtc_commit_free);
48}
49EXPORT_SYMBOL(drm_crtc_commit_put);
50
Maarten Lankhorst036ef572015-05-18 10:06:40 +020051/**
52 * drm_atomic_state_default_release -
53 * release memory initialized by drm_atomic_state_init
54 * @state: atomic state
55 *
56 * Free all the memory allocated by drm_atomic_state_init.
57 * This is useful for drivers that subclass the atomic state.
58 */
59void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020060{
61 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020062 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020063 kfree(state->planes);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020064}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020065EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020066
67/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020068 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020069 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020070 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020071 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020072 * Default implementation for filling in a new atomic state.
73 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020074 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020075int
76drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020077{
Chris Wilson08536952016-10-14 13:18:18 +010078 kref_init(&state->ref);
79
Rob Clarkd34f20d2014-12-18 16:01:56 -050080 /* TODO legacy paths should maybe do a better job about
81 * setting this appropriately?
82 */
83 state->allow_modeset = true;
84
Daniel Vettercc4ceb42014-07-25 21:30:38 +020085 state->crtcs = kcalloc(dev->mode_config.num_crtc,
86 sizeof(*state->crtcs), GFP_KERNEL);
87 if (!state->crtcs)
88 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020089 state->planes = kcalloc(dev->mode_config.num_total_plane,
90 sizeof(*state->planes), GFP_KERNEL);
91 if (!state->planes)
92 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020093
94 state->dev = dev;
95
Maarten Lankhorst036ef572015-05-18 10:06:40 +020096 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020097
Maarten Lankhorst036ef572015-05-18 10:06:40 +020098 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020099fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200100 drm_atomic_state_default_release(state);
101 return -ENOMEM;
102}
103EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200104
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200105/**
106 * drm_atomic_state_alloc - allocate atomic state
107 * @dev: DRM device
108 *
109 * This allocates an empty atomic state to track updates.
110 */
111struct drm_atomic_state *
112drm_atomic_state_alloc(struct drm_device *dev)
113{
114 struct drm_mode_config *config = &dev->mode_config;
115 struct drm_atomic_state *state;
116
117 if (!config->funcs->atomic_state_alloc) {
118 state = kzalloc(sizeof(*state), GFP_KERNEL);
119 if (!state)
120 return NULL;
121 if (drm_atomic_state_init(dev, state) < 0) {
122 kfree(state);
123 return NULL;
124 }
125 return state;
126 }
127
128 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200129}
130EXPORT_SYMBOL(drm_atomic_state_alloc);
131
132/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200133 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200134 * @state: atomic state
135 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200136 * Default implementation for clearing atomic state.
137 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200138 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200139void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200140{
141 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100142 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200143 int i;
144
Daniel Vetter17a38d92015-02-22 12:24:16 +0100145 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200146
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100147 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200148 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200149
150 if (!connector)
151 continue;
152
Dave Airlied2307de2016-04-27 11:27:39 +1000153 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200154 state->connectors[i].state);
155 state->connectors[i].ptr = NULL;
156 state->connectors[i].state = NULL;
Dave Airlieb164d312016-04-27 11:10:09 +1000157 drm_connector_unreference(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200158 }
159
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100160 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200161 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200162
163 if (!crtc)
164 continue;
165
166 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200167 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200168
169 if (state->crtcs[i].commit) {
170 kfree(state->crtcs[i].commit->event);
171 state->crtcs[i].commit->event = NULL;
172 drm_crtc_commit_put(state->crtcs[i].commit);
173 }
174
175 state->crtcs[i].commit = NULL;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200176 state->crtcs[i].ptr = NULL;
177 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200178 }
179
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100180 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200181 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200182
183 if (!plane)
184 continue;
185
186 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200187 state->planes[i].state);
188 state->planes[i].ptr = NULL;
189 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200190 }
191}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200192EXPORT_SYMBOL(drm_atomic_state_default_clear);
193
194/**
195 * drm_atomic_state_clear - clear state object
196 * @state: atomic state
197 *
198 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
199 * all locks. So someone else could sneak in and change the current modeset
200 * configuration. Which means that all the state assembled in @state is no
201 * longer an atomic update to the current state, but to some arbitrary earlier
202 * state. Which could break assumptions the driver's ->atomic_check likely
203 * relies on.
204 *
205 * Hence we must clear all cached state and completely start over, using this
206 * function.
207 */
208void drm_atomic_state_clear(struct drm_atomic_state *state)
209{
210 struct drm_device *dev = state->dev;
211 struct drm_mode_config *config = &dev->mode_config;
212
213 if (config->funcs->atomic_state_clear)
214 config->funcs->atomic_state_clear(state);
215 else
216 drm_atomic_state_default_clear(state);
217}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200218EXPORT_SYMBOL(drm_atomic_state_clear);
219
220/**
Chris Wilson08536952016-10-14 13:18:18 +0100221 * __drm_atomic_state_free - free all memory for an atomic state
222 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200223 *
224 * This frees all memory associated with an atomic state, including all the
225 * per-object state for planes, crtcs and connectors.
226 */
Chris Wilson08536952016-10-14 13:18:18 +0100227void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200228{
Chris Wilson08536952016-10-14 13:18:18 +0100229 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
230 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200231
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200232 drm_atomic_state_clear(state);
233
Daniel Vetter17a38d92015-02-22 12:24:16 +0100234 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200235
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200236 if (config->funcs->atomic_state_free) {
237 config->funcs->atomic_state_free(state);
238 } else {
239 drm_atomic_state_default_release(state);
240 kfree(state);
241 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200242}
Chris Wilson08536952016-10-14 13:18:18 +0100243EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200244
245/**
246 * drm_atomic_get_crtc_state - get crtc state
247 * @state: global atomic state object
248 * @crtc: crtc to get state object for
249 *
250 * This function returns the crtc state for the given crtc, allocating it if
251 * needed. It will also grab the relevant crtc lock to make sure that the state
252 * is consistent.
253 *
254 * Returns:
255 *
256 * Either the allocated state or the error code encoded into the pointer. When
257 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
258 * entire atomic sequence must be restarted. All other errors are fatal.
259 */
260struct drm_crtc_state *
261drm_atomic_get_crtc_state(struct drm_atomic_state *state,
262 struct drm_crtc *crtc)
263{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200264 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200265 struct drm_crtc_state *crtc_state;
266
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200267 WARN_ON(!state->acquire_ctx);
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
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200281 state->crtcs[index].state = crtc_state;
282 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200283 crtc_state->state = state;
284
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200285 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
286 crtc->base.id, crtc->name, 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
Markus Elfring5f911902015-11-06 12:03:46 +0100313 drm_property_unreference_blob(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100314 state->mode_blob = NULL;
315
Daniel Stone819364d2015-05-26 14:36:48 +0100316 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100317 drm_mode_convert_to_umode(&umode, mode);
318 state->mode_blob =
319 drm_property_create_blob(state->crtc->dev,
320 sizeof(umode),
321 &umode);
322 if (IS_ERR(state->mode_blob))
323 return PTR_ERR(state->mode_blob);
324
Daniel Stone819364d2015-05-26 14:36:48 +0100325 drm_mode_copy(&state->mode, mode);
326 state->enable = true;
327 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
328 mode->name, state);
329 } else {
330 memset(&state->mode, 0, sizeof(state->mode));
331 state->enable = false;
332 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
333 state);
334 }
335
336 return 0;
337}
338EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
339
Daniel Stone819364d2015-05-26 14:36:48 +0100340/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100341 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
342 * @state: the CRTC whose incoming state to update
343 * @blob: pointer to blob property to use for mode
344 *
345 * Set a mode (originating from a blob property) on the desired CRTC state.
346 * This function will take a reference on the blob property for the CRTC state,
347 * and release the reference held on the state's existing mode property, if any
348 * was set.
349 *
350 * RETURNS:
351 * Zero on success, error code on failure. Cannot return -EDEADLK.
352 */
353int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
354 struct drm_property_blob *blob)
355{
356 if (blob == state->mode_blob)
357 return 0;
358
Markus Elfring5f911902015-11-06 12:03:46 +0100359 drm_property_unreference_blob(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100360 state->mode_blob = NULL;
361
Tomi Valkeinen67098872016-05-31 15:03:17 +0300362 memset(&state->mode, 0, sizeof(state->mode));
363
Daniel Stone955f3c32015-05-25 19:11:52 +0100364 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 {
Daniel Stone955f3c32015-05-25 19:11:52 +0100376 state->enable = false;
377 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
378 state);
379 }
380
381 return 0;
382}
383EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
384
385/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000386 * drm_atomic_replace_property_blob - replace a blob property
387 * @blob: a pointer to the member blob to be replaced
388 * @new_blob: the new blob to replace with
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000389 * @replaced: whether the blob has been replaced
390 *
391 * RETURNS:
392 * Zero on success, error code on failure
393 */
394static void
395drm_atomic_replace_property_blob(struct drm_property_blob **blob,
396 struct drm_property_blob *new_blob,
397 bool *replaced)
398{
399 struct drm_property_blob *old_blob = *blob;
400
401 if (old_blob == new_blob)
402 return;
403
Markus Elfringf35cbe62016-07-20 17:54:32 +0200404 drm_property_unreference_blob(old_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000405 if (new_blob)
406 drm_property_reference_blob(new_blob);
407 *blob = new_blob;
408 *replaced = true;
409
410 return;
411}
412
413static int
414drm_atomic_replace_property_blob_from_id(struct drm_crtc *crtc,
415 struct drm_property_blob **blob,
416 uint64_t blob_id,
417 ssize_t expected_size,
418 bool *replaced)
419{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000420 struct drm_property_blob *new_blob = NULL;
421
422 if (blob_id != 0) {
Felix Monningercac5fced2016-10-25 22:28:08 +0100423 new_blob = drm_property_lookup_blob(crtc->dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000424 if (new_blob == NULL)
425 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100426
427 if (expected_size > 0 && expected_size != new_blob->length) {
428 drm_property_unreference_blob(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000429 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100430 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000431 }
432
433 drm_atomic_replace_property_blob(blob, new_blob, replaced);
Felix Monningercac5fced2016-10-25 22:28:08 +0100434 drm_property_unreference_blob(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000435
436 return 0;
437}
438
439/**
Rob Clark40ecc692014-12-18 16:01:46 -0500440 * drm_atomic_crtc_set_property - set property on CRTC
441 * @crtc: the drm CRTC to set a property on
442 * @state: the state object to update with the new property value
443 * @property: the property to set
444 * @val: the new property value
445 *
446 * Use this instead of calling crtc->atomic_set_property directly.
447 * This function handles generic/core properties and calls out to
448 * driver's ->atomic_set_property() for driver properties. To ensure
449 * consistent behavior you must call this function rather than the
450 * driver hook directly.
451 *
452 * RETURNS:
453 * Zero on success, error code on failure
454 */
455int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
456 struct drm_crtc_state *state, struct drm_property *property,
457 uint64_t val)
458{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100459 struct drm_device *dev = crtc->dev;
460 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000461 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100462 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100463
Daniel Stone27798362015-03-19 04:33:26 +0000464 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100465 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100466 else if (property == config->prop_mode_id) {
467 struct drm_property_blob *mode =
468 drm_property_lookup_blob(dev, val);
469 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Markus Elfring5f911902015-11-06 12:03:46 +0100470 drm_property_unreference_blob(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100471 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000472 } else if (property == config->degamma_lut_property) {
473 ret = drm_atomic_replace_property_blob_from_id(crtc,
474 &state->degamma_lut,
475 val,
476 -1,
477 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200478 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000479 return ret;
480 } else if (property == config->ctm_property) {
481 ret = drm_atomic_replace_property_blob_from_id(crtc,
482 &state->ctm,
483 val,
484 sizeof(struct drm_color_ctm),
485 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200486 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000487 return ret;
488 } else if (property == config->gamma_lut_property) {
489 ret = drm_atomic_replace_property_blob_from_id(crtc,
490 &state->gamma_lut,
491 val,
492 -1,
493 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200494 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000495 return ret;
496 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500497 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000498 else
499 return -EINVAL;
500
501 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500502}
503EXPORT_SYMBOL(drm_atomic_crtc_set_property);
504
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100505/**
506 * drm_atomic_crtc_get_property - get property value from CRTC state
507 * @crtc: the drm CRTC to set a property on
508 * @state: the state object to get the property value from
509 * @property: the property to set
510 * @val: return location for the property value
511 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500512 * This function handles generic/core properties and calls out to
513 * driver's ->atomic_get_property() for driver properties. To ensure
514 * consistent behavior you must call this function rather than the
515 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100516 *
517 * RETURNS:
518 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500519 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700520static int
521drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500522 const struct drm_crtc_state *state,
523 struct drm_property *property, uint64_t *val)
524{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000525 struct drm_device *dev = crtc->dev;
526 struct drm_mode_config *config = &dev->mode_config;
527
528 if (property == config->prop_active)
529 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100530 else if (property == config->prop_mode_id)
531 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000532 else if (property == config->degamma_lut_property)
533 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
534 else if (property == config->ctm_property)
535 *val = (state->ctm) ? state->ctm->base.id : 0;
536 else if (property == config->gamma_lut_property)
537 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000538 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500539 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000540 else
541 return -EINVAL;
542
543 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500544}
Rob Clarkac9c9252014-12-18 16:01:47 -0500545
546/**
Rob Clark5e743732014-12-18 16:01:51 -0500547 * drm_atomic_crtc_check - check crtc state
548 * @crtc: crtc to check
549 * @state: crtc state to check
550 *
551 * Provides core sanity checks for crtc state.
552 *
553 * RETURNS:
554 * Zero on success, error code on failure
555 */
556static int drm_atomic_crtc_check(struct drm_crtc *crtc,
557 struct drm_crtc_state *state)
558{
559 /* NOTE: we explicitly don't enforce constraints such as primary
560 * layer covering entire screen, since that is something we want
561 * to allow (on hw that supports it). For hw that does not, it
562 * should be checked in driver's crtc->atomic_check() vfunc.
563 *
564 * TODO: Add generic modeset state checks once we support those.
565 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100566
567 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200568 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
569 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100570 return -EINVAL;
571 }
572
Daniel Stone99cf4a22015-05-25 19:11:51 +0100573 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
574 * as this is a kernel-internal detail that userspace should never
575 * be able to trigger. */
576 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
577 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200578 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
579 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100580 return -EINVAL;
581 }
582
583 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
584 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200585 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
586 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100587 return -EINVAL;
588 }
589
Daniel Vetter4cba6852015-12-08 09:49:20 +0100590 /*
591 * Reject event generation for when a CRTC is off and stays off.
592 * It wouldn't be hard to implement this, but userspace has a track
593 * record of happily burning through 100% cpu (or worse, crash) when the
594 * display pipe is suspended. To avoid all that fun just reject updates
595 * that ask for events since likely that indicates a bug in the
596 * compositor's drawing loop. This is consistent with the vblank IOCTL
597 * and legacy page_flip IOCTL which also reject service on a disabled
598 * pipe.
599 */
600 if (state->event && !state->active && !crtc->state->active) {
601 DRM_DEBUG_ATOMIC("[CRTC:%d] requesting event but off\n",
602 crtc->base.id);
603 return -EINVAL;
604 }
605
Rob Clark5e743732014-12-18 16:01:51 -0500606 return 0;
607}
608
Rob Clarkfceffb322016-11-05 11:08:09 -0400609static void drm_atomic_crtc_print_state(struct drm_printer *p,
610 const struct drm_crtc_state *state)
611{
612 struct drm_crtc *crtc = state->crtc;
613
614 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
615 drm_printf(p, "\tenable=%d\n", state->enable);
616 drm_printf(p, "\tactive=%d\n", state->active);
617 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
618 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
619 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
620 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
621 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
622 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
623 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
624 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
625 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
626
627 if (crtc->funcs->atomic_print_state)
628 crtc->funcs->atomic_print_state(p, state);
629}
630
Rob Clark5e743732014-12-18 16:01:51 -0500631/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200632 * drm_atomic_get_plane_state - get plane state
633 * @state: global atomic state object
634 * @plane: plane to get state object for
635 *
636 * This function returns the plane state for the given plane, allocating it if
637 * needed. It will also grab the relevant plane lock to make sure that the state
638 * is consistent.
639 *
640 * Returns:
641 *
642 * Either the allocated state or the error code encoded into the pointer. When
643 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
644 * entire atomic sequence must be restarted. All other errors are fatal.
645 */
646struct drm_plane_state *
647drm_atomic_get_plane_state(struct drm_atomic_state *state,
648 struct drm_plane *plane)
649{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200650 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200651 struct drm_plane_state *plane_state;
652
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200653 WARN_ON(!state->acquire_ctx);
654
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200655 plane_state = drm_atomic_get_existing_plane_state(state, plane);
656 if (plane_state)
657 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200658
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100659 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200660 if (ret)
661 return ERR_PTR(ret);
662
663 plane_state = plane->funcs->atomic_duplicate_state(plane);
664 if (!plane_state)
665 return ERR_PTR(-ENOMEM);
666
Daniel Vetterb8b53422016-06-02 00:06:33 +0200667 state->planes[index].state = plane_state;
668 state->planes[index].ptr = plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200669 plane_state->state = state;
670
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200671 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
672 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200673
674 if (plane_state->crtc) {
675 struct drm_crtc_state *crtc_state;
676
677 crtc_state = drm_atomic_get_crtc_state(state,
678 plane_state->crtc);
679 if (IS_ERR(crtc_state))
680 return ERR_CAST(crtc_state);
681 }
682
683 return plane_state;
684}
685EXPORT_SYMBOL(drm_atomic_get_plane_state);
686
687/**
Rob Clark40ecc692014-12-18 16:01:46 -0500688 * drm_atomic_plane_set_property - set property on plane
689 * @plane: the drm plane to set a property on
690 * @state: the state object to update with the new property value
691 * @property: the property to set
692 * @val: the new property value
693 *
694 * Use this instead of calling plane->atomic_set_property directly.
695 * This function handles generic/core properties and calls out to
696 * driver's ->atomic_set_property() for driver properties. To ensure
697 * consistent behavior you must call this function rather than the
698 * driver hook directly.
699 *
700 * RETURNS:
701 * Zero on success, error code on failure
702 */
703int drm_atomic_plane_set_property(struct drm_plane *plane,
704 struct drm_plane_state *state, struct drm_property *property,
705 uint64_t val)
706{
Rob Clark6b4959f2014-12-18 16:01:53 -0500707 struct drm_device *dev = plane->dev;
708 struct drm_mode_config *config = &dev->mode_config;
709
710 if (property == config->prop_fb_id) {
711 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
712 drm_atomic_set_fb_for_plane(state, fb);
713 if (fb)
714 drm_framebuffer_unreference(fb);
715 } else if (property == config->prop_crtc_id) {
716 struct drm_crtc *crtc = drm_crtc_find(dev, val);
717 return drm_atomic_set_crtc_for_plane(state, crtc);
718 } else if (property == config->prop_crtc_x) {
719 state->crtc_x = U642I64(val);
720 } else if (property == config->prop_crtc_y) {
721 state->crtc_y = U642I64(val);
722 } else if (property == config->prop_crtc_w) {
723 state->crtc_w = val;
724 } else if (property == config->prop_crtc_h) {
725 state->crtc_h = val;
726 } else if (property == config->prop_src_x) {
727 state->src_x = val;
728 } else if (property == config->prop_src_y) {
729 state->src_y = val;
730 } else if (property == config->prop_src_w) {
731 state->src_w = val;
732 } else if (property == config->prop_src_h) {
733 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300734 } else if (property == plane->rotation_property) {
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300735 if (!is_power_of_2(val & DRM_ROTATE_MASK))
736 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800737 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200738 } else if (property == plane->zpos_property) {
739 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500740 } else if (plane->funcs->atomic_set_property) {
741 return plane->funcs->atomic_set_property(plane, state,
742 property, val);
743 } else {
744 return -EINVAL;
745 }
746
747 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500748}
749EXPORT_SYMBOL(drm_atomic_plane_set_property);
750
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100751/**
752 * drm_atomic_plane_get_property - get property value from plane state
753 * @plane: the drm plane to set a property on
754 * @state: the state object to get the property value from
755 * @property: the property to set
756 * @val: return location for the property value
757 *
Rob Clarkac9c9252014-12-18 16:01:47 -0500758 * This function handles generic/core properties and calls out to
759 * driver's ->atomic_get_property() for driver properties. To ensure
760 * consistent behavior you must call this function rather than the
761 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100762 *
763 * RETURNS:
764 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500765 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100766static int
767drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500768 const struct drm_plane_state *state,
769 struct drm_property *property, uint64_t *val)
770{
Rob Clark6b4959f2014-12-18 16:01:53 -0500771 struct drm_device *dev = plane->dev;
772 struct drm_mode_config *config = &dev->mode_config;
773
774 if (property == config->prop_fb_id) {
775 *val = (state->fb) ? state->fb->base.id : 0;
776 } else if (property == config->prop_crtc_id) {
777 *val = (state->crtc) ? state->crtc->base.id : 0;
778 } else if (property == config->prop_crtc_x) {
779 *val = I642U64(state->crtc_x);
780 } else if (property == config->prop_crtc_y) {
781 *val = I642U64(state->crtc_y);
782 } else if (property == config->prop_crtc_w) {
783 *val = state->crtc_w;
784 } else if (property == config->prop_crtc_h) {
785 *val = state->crtc_h;
786 } else if (property == config->prop_src_x) {
787 *val = state->src_x;
788 } else if (property == config->prop_src_y) {
789 *val = state->src_y;
790 } else if (property == config->prop_src_w) {
791 *val = state->src_w;
792 } else if (property == config->prop_src_h) {
793 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300794 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000795 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200796 } else if (property == plane->zpos_property) {
797 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500798 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500799 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500800 } else {
801 return -EINVAL;
802 }
803
804 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500805}
Rob Clarkac9c9252014-12-18 16:01:47 -0500806
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200807static bool
808plane_switching_crtc(struct drm_atomic_state *state,
809 struct drm_plane *plane,
810 struct drm_plane_state *plane_state)
811{
812 if (!plane->state->crtc || !plane_state->crtc)
813 return false;
814
815 if (plane->state->crtc == plane_state->crtc)
816 return false;
817
818 /* This could be refined, but currently there's no helper or driver code
819 * to implement direct switching of active planes nor userspace to take
820 * advantage of more direct plane switching without the intermediate
821 * full OFF state.
822 */
823 return true;
824}
825
Rob Clarkac9c9252014-12-18 16:01:47 -0500826/**
Rob Clark5e743732014-12-18 16:01:51 -0500827 * drm_atomic_plane_check - check plane state
828 * @plane: plane to check
829 * @state: plane state to check
830 *
831 * Provides core sanity checks for plane state.
832 *
833 * RETURNS:
834 * Zero on success, error code on failure
835 */
836static int drm_atomic_plane_check(struct drm_plane *plane,
837 struct drm_plane_state *state)
838{
839 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200840 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500841
842 /* either *both* CRTC and FB must be set, or neither */
843 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100844 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500845 return -EINVAL;
846 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100847 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500848 return -EINVAL;
849 }
850
851 /* if disabled, we don't care about the rest of the state: */
852 if (!state->crtc)
853 return 0;
854
855 /* Check whether this plane is usable on this CRTC */
856 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100857 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500858 return -EINVAL;
859 }
860
861 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +0200862 ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
863 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +0100864 char *format_name = drm_get_format_name(state->fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +0100865 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n", format_name);
866 kfree(format_name);
Laurent Pinchartead86102015-03-05 02:25:43 +0200867 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500868 }
869
870 /* Give drivers some help against integer overflows */
871 if (state->crtc_w > INT_MAX ||
872 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
873 state->crtc_h > INT_MAX ||
874 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100875 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
876 state->crtc_w, state->crtc_h,
877 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500878 return -ERANGE;
879 }
880
881 fb_width = state->fb->width << 16;
882 fb_height = state->fb->height << 16;
883
884 /* Make sure source coordinates are inside the fb. */
885 if (state->src_w > fb_width ||
886 state->src_x > fb_width - state->src_w ||
887 state->src_h > fb_height ||
888 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100889 DRM_DEBUG_ATOMIC("Invalid source coordinates "
890 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
891 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
892 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
893 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
894 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500895 return -ENOSPC;
896 }
897
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200898 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200899 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
900 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200901 return -EINVAL;
902 }
903
Rob Clark5e743732014-12-18 16:01:51 -0500904 return 0;
905}
906
Rob Clarkfceffb322016-11-05 11:08:09 -0400907static void drm_atomic_plane_print_state(struct drm_printer *p,
908 const struct drm_plane_state *state)
909{
910 struct drm_plane *plane = state->plane;
911 struct drm_rect src = drm_plane_state_src(state);
912 struct drm_rect dest = drm_plane_state_dest(state);
913
914 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
915 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
916 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
917 if (state->fb) {
918 struct drm_framebuffer *fb = state->fb;
919 int i, n = drm_format_num_planes(fb->pixel_format);
920
921 drm_printf(p, "\t\tformat=%s\n",
922 drm_get_format_name(fb->pixel_format));
923 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
924 drm_printf(p, "\t\tlayers:\n");
925 for (i = 0; i < n; i++) {
926 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
927 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
928 drm_printf(p, "\t\t\tmodifier[%d]=0x%llx\n", i, fb->modifier[i]);
929 }
930 }
931 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
932 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
933 drm_printf(p, "\trotation=%x\n", state->rotation);
934
935 if (plane->funcs->atomic_print_state)
936 plane->funcs->atomic_print_state(p, state);
937}
938
Rob Clark5e743732014-12-18 16:01:51 -0500939/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200940 * drm_atomic_get_connector_state - get connector state
941 * @state: global atomic state object
942 * @connector: connector to get state object for
943 *
944 * This function returns the connector state for the given connector,
945 * allocating it if needed. It will also grab the relevant connector lock to
946 * make sure that the state is consistent.
947 *
948 * Returns:
949 *
950 * Either the allocated state or the error code encoded into the pointer. When
951 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
952 * entire atomic sequence must be restarted. All other errors are fatal.
953 */
954struct drm_connector_state *
955drm_atomic_get_connector_state(struct drm_atomic_state *state,
956 struct drm_connector *connector)
957{
958 int ret, index;
959 struct drm_mode_config *config = &connector->dev->mode_config;
960 struct drm_connector_state *connector_state;
961
Maarten Lankhorst7f4eaa82016-05-03 11:12:31 +0200962 WARN_ON(!state->acquire_ctx);
963
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100964 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
965 if (ret)
966 return ERR_PTR(ret);
967
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200968 index = drm_connector_index(connector);
969
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100970 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200971 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100972 int alloc = max(index + 1, config->num_connector);
973
974 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
975 if (!c)
976 return ERR_PTR(-ENOMEM);
977
978 state->connectors = c;
979 memset(&state->connectors[state->num_connector], 0,
980 sizeof(*state->connectors) * (alloc - state->num_connector));
981
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100982 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100983 }
984
Daniel Vetter63e83c12016-06-02 00:06:32 +0200985 if (state->connectors[index].state)
986 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200987
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200988 connector_state = connector->funcs->atomic_duplicate_state(connector);
989 if (!connector_state)
990 return ERR_PTR(-ENOMEM);
991
Dave Airlieb164d312016-04-27 11:10:09 +1000992 drm_connector_reference(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +0200993 state->connectors[index].state = connector_state;
994 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200995 connector_state->state = state;
996
Daniel Vetter17a38d92015-02-22 12:24:16 +0100997 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
998 connector->base.id, connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200999
1000 if (connector_state->crtc) {
1001 struct drm_crtc_state *crtc_state;
1002
1003 crtc_state = drm_atomic_get_crtc_state(state,
1004 connector_state->crtc);
1005 if (IS_ERR(crtc_state))
1006 return ERR_CAST(crtc_state);
1007 }
1008
1009 return connector_state;
1010}
1011EXPORT_SYMBOL(drm_atomic_get_connector_state);
1012
1013/**
Rob Clark40ecc692014-12-18 16:01:46 -05001014 * drm_atomic_connector_set_property - set property on connector.
1015 * @connector: the drm connector to set a property on
1016 * @state: the state object to update with the new property value
1017 * @property: the property to set
1018 * @val: the new property value
1019 *
1020 * Use this instead of calling connector->atomic_set_property directly.
1021 * This function handles generic/core properties and calls out to
1022 * driver's ->atomic_set_property() for driver properties. To ensure
1023 * consistent behavior you must call this function rather than the
1024 * driver hook directly.
1025 *
1026 * RETURNS:
1027 * Zero on success, error code on failure
1028 */
1029int drm_atomic_connector_set_property(struct drm_connector *connector,
1030 struct drm_connector_state *state, struct drm_property *property,
1031 uint64_t val)
1032{
1033 struct drm_device *dev = connector->dev;
1034 struct drm_mode_config *config = &dev->mode_config;
1035
Rob Clarkae16c592014-12-18 16:01:54 -05001036 if (property == config->prop_crtc_id) {
1037 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1038 return drm_atomic_set_crtc_for_connector(state, crtc);
1039 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001040 /* setting DPMS property requires special handling, which
1041 * is done in legacy setprop path for us. Disallow (for
1042 * now?) atomic writes to DPMS property:
1043 */
1044 return -EINVAL;
1045 } else if (connector->funcs->atomic_set_property) {
1046 return connector->funcs->atomic_set_property(connector,
1047 state, property, val);
1048 } else {
1049 return -EINVAL;
1050 }
1051}
1052EXPORT_SYMBOL(drm_atomic_connector_set_property);
1053
Rob Clarkfceffb322016-11-05 11:08:09 -04001054static void drm_atomic_connector_print_state(struct drm_printer *p,
1055 const struct drm_connector_state *state)
1056{
1057 struct drm_connector *connector = state->connector;
1058
1059 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1060 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1061
1062 if (connector->funcs->atomic_print_state)
1063 connector->funcs->atomic_print_state(p, state);
1064}
1065
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001066/**
1067 * drm_atomic_connector_get_property - get property value from connector state
1068 * @connector: the drm connector to set a property on
1069 * @state: the state object to get the property value from
1070 * @property: the property to set
1071 * @val: return location for the property value
1072 *
Rob Clarkac9c9252014-12-18 16:01:47 -05001073 * This function handles generic/core properties and calls out to
1074 * driver's ->atomic_get_property() for driver properties. To ensure
1075 * consistent behavior you must call this function rather than the
1076 * driver hook directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001077 *
1078 * RETURNS:
1079 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001080 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001081static int
1082drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001083 const struct drm_connector_state *state,
1084 struct drm_property *property, uint64_t *val)
1085{
1086 struct drm_device *dev = connector->dev;
1087 struct drm_mode_config *config = &dev->mode_config;
1088
Rob Clarkae16c592014-12-18 16:01:54 -05001089 if (property == config->prop_crtc_id) {
1090 *val = (state->crtc) ? state->crtc->base.id : 0;
1091 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001092 *val = connector->dpms;
1093 } else if (connector->funcs->atomic_get_property) {
1094 return connector->funcs->atomic_get_property(connector,
1095 state, property, val);
1096 } else {
1097 return -EINVAL;
1098 }
1099
1100 return 0;
1101}
Rob Clarkac9c9252014-12-18 16:01:47 -05001102
Rob Clark88a48e22014-12-18 16:01:50 -05001103int drm_atomic_get_property(struct drm_mode_object *obj,
1104 struct drm_property *property, uint64_t *val)
1105{
1106 struct drm_device *dev = property->dev;
1107 int ret;
1108
1109 switch (obj->type) {
1110 case DRM_MODE_OBJECT_CONNECTOR: {
1111 struct drm_connector *connector = obj_to_connector(obj);
1112 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1113 ret = drm_atomic_connector_get_property(connector,
1114 connector->state, property, val);
1115 break;
1116 }
1117 case DRM_MODE_OBJECT_CRTC: {
1118 struct drm_crtc *crtc = obj_to_crtc(obj);
1119 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1120 ret = drm_atomic_crtc_get_property(crtc,
1121 crtc->state, property, val);
1122 break;
1123 }
1124 case DRM_MODE_OBJECT_PLANE: {
1125 struct drm_plane *plane = obj_to_plane(obj);
1126 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1127 ret = drm_atomic_plane_get_property(plane,
1128 plane->state, property, val);
1129 break;
1130 }
1131 default:
1132 ret = -EINVAL;
1133 break;
1134 }
1135
1136 return ret;
1137}
1138
1139/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001140 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001141 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001142 * @crtc: crtc to use for the plane
1143 *
1144 * Changing the assigned crtc for a plane requires us to grab the lock and state
1145 * for the new crtc, as needed. This function takes care of all these details
1146 * besides updating the pointer in the state object itself.
1147 *
1148 * Returns:
1149 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1150 * then the w/w mutex code has detected a deadlock and the entire atomic
1151 * sequence must be restarted. All other errors are fatal.
1152 */
1153int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001154drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1155 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001156{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001157 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001158 struct drm_crtc_state *crtc_state;
1159
Rob Clark6ddd3882014-11-21 15:28:31 -05001160 if (plane_state->crtc) {
1161 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1162 plane_state->crtc);
1163 if (WARN_ON(IS_ERR(crtc_state)))
1164 return PTR_ERR(crtc_state);
1165
1166 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1167 }
1168
1169 plane_state->crtc = crtc;
1170
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001171 if (crtc) {
1172 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1173 crtc);
1174 if (IS_ERR(crtc_state))
1175 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001176 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001177 }
1178
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001179 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001180 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1181 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001182 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001183 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1184 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001185
1186 return 0;
1187}
1188EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1189
1190/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001191 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001192 * @plane_state: atomic state object for the plane
1193 * @fb: fb to use for the plane
1194 *
1195 * Changing the assigned framebuffer for a plane requires us to grab a reference
1196 * to the new fb and drop the reference to the old fb, if there is one. This
1197 * function takes care of all these details besides updating the pointer in the
1198 * state object itself.
1199 */
1200void
1201drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1202 struct drm_framebuffer *fb)
1203{
1204 if (plane_state->fb)
1205 drm_framebuffer_unreference(plane_state->fb);
1206 if (fb)
1207 drm_framebuffer_reference(fb);
1208 plane_state->fb = fb;
1209
1210 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001211 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1212 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001213 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001214 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1215 plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001216}
1217EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1218
1219/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001220 * drm_atomic_set_fence_for_plane - set fence for plane
1221 * @plane_state: atomic state object for the plane
1222 * @fence: dma_fence to use for the plane
1223 *
1224 * Helper to setup the plane_state fence in case it is not set yet.
1225 * By using this drivers doesn't need to worry if the user choose
1226 * implicit or explicit fencing.
1227 *
1228 * This function will not set the fence to the state if it was set
1229 * via explicit fencing interfaces on the atomic ioctl. It will
1230 * all drope the reference to the fence as we not storing it
1231 * anywhere.
1232 *
1233 * Otherwise, if plane_state->fence is not set this function we
1234 * just set it with the received implict fence.
1235 */
1236void
1237drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1238 struct dma_fence *fence)
1239{
1240 if (plane_state->fence) {
1241 dma_fence_put(fence);
1242 return;
1243 }
1244
1245 plane_state->fence = fence;
1246}
1247EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1248
1249/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001250 * drm_atomic_set_crtc_for_connector - set crtc for connector
1251 * @conn_state: atomic state object for the connector
1252 * @crtc: crtc to use for the connector
1253 *
1254 * Changing the assigned crtc for a connector requires us to grab the lock and
1255 * state for the new crtc, as needed. This function takes care of all these
1256 * details besides updating the pointer in the state object itself.
1257 *
1258 * Returns:
1259 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1260 * then the w/w mutex code has detected a deadlock and the entire atomic
1261 * sequence must be restarted. All other errors are fatal.
1262 */
1263int
1264drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1265 struct drm_crtc *crtc)
1266{
1267 struct drm_crtc_state *crtc_state;
1268
Chris Wilsone2d800a2016-05-06 12:47:45 +01001269 if (conn_state->crtc == crtc)
1270 return 0;
1271
1272 if (conn_state->crtc) {
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001273 crtc_state = drm_atomic_get_existing_crtc_state(conn_state->state,
1274 conn_state->crtc);
1275
1276 crtc_state->connector_mask &=
1277 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001278
1279 drm_connector_unreference(conn_state->connector);
1280 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001281 }
1282
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001283 if (crtc) {
1284 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1285 if (IS_ERR(crtc_state))
1286 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001287
1288 crtc_state->connector_mask |=
1289 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001290
Chris Wilsone2d800a2016-05-06 12:47:45 +01001291 drm_connector_reference(conn_state->connector);
1292 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001293
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001294 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1295 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001296 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001297 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1298 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001299 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001300
1301 return 0;
1302}
1303EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1304
1305/**
1306 * drm_atomic_add_affected_connectors - add connectors for crtc
1307 * @state: atomic state
1308 * @crtc: DRM crtc
1309 *
1310 * This function walks the current configuration and adds all connectors
1311 * currently using @crtc to the atomic configuration @state. Note that this
1312 * function must acquire the connection mutex. This can potentially cause
1313 * unneeded seralization if the update is just for the planes on one crtc. Hence
1314 * drivers and helpers should only call this when really needed (e.g. when a
1315 * full modeset needs to happen due to some change).
1316 *
1317 * Returns:
1318 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1319 * then the w/w mutex code has detected a deadlock and the entire atomic
1320 * sequence must be restarted. All other errors are fatal.
1321 */
1322int
1323drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1324 struct drm_crtc *crtc)
1325{
1326 struct drm_mode_config *config = &state->dev->mode_config;
1327 struct drm_connector *connector;
1328 struct drm_connector_state *conn_state;
1329 int ret;
1330
1331 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1332 if (ret)
1333 return ret;
1334
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001335 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1336 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001337
1338 /*
1339 * Changed connectors are already in @state, so only need to look at the
1340 * current configuration.
1341 */
Daniel Vetter9a9f5ce2015-07-09 23:44:34 +02001342 drm_for_each_connector(connector, state->dev) {
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001343 if (connector->state->crtc != crtc)
1344 continue;
1345
1346 conn_state = drm_atomic_get_connector_state(state, connector);
1347 if (IS_ERR(conn_state))
1348 return PTR_ERR(conn_state);
1349 }
1350
1351 return 0;
1352}
1353EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1354
1355/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001356 * drm_atomic_add_affected_planes - add planes for crtc
1357 * @state: atomic state
1358 * @crtc: DRM crtc
1359 *
1360 * This function walks the current configuration and adds all planes
1361 * currently used by @crtc to the atomic configuration @state. This is useful
1362 * when an atomic commit also needs to check all currently enabled plane on
1363 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1364 * to avoid special code to force-enable all planes.
1365 *
1366 * Since acquiring a plane state will always also acquire the w/w mutex of the
1367 * current CRTC for that plane (if there is any) adding all the plane states for
1368 * a CRTC will not reduce parallism of atomic updates.
1369 *
1370 * Returns:
1371 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1372 * then the w/w mutex code has detected a deadlock and the entire atomic
1373 * sequence must be restarted. All other errors are fatal.
1374 */
1375int
1376drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1377 struct drm_crtc *crtc)
1378{
1379 struct drm_plane *plane;
1380
1381 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1382
1383 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1384 struct drm_plane_state *plane_state =
1385 drm_atomic_get_plane_state(state, plane);
1386
1387 if (IS_ERR(plane_state))
1388 return PTR_ERR(plane_state);
1389 }
1390 return 0;
1391}
1392EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1393
1394/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001395 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1396 * @state: atomic state
1397 *
1398 * This function should be used by legacy entry points which don't understand
1399 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001400 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001401 */
1402void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1403{
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001404 struct drm_device *dev = state->dev;
1405 unsigned crtc_mask = 0;
1406 struct drm_crtc *crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001407 int ret;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001408 bool global = false;
1409
1410 drm_for_each_crtc(crtc, dev) {
1411 if (crtc->acquire_ctx != state->acquire_ctx)
1412 continue;
1413
1414 crtc_mask |= drm_crtc_mask(crtc);
1415 crtc->acquire_ctx = NULL;
1416 }
1417
1418 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1419 global = true;
1420
1421 dev->mode_config.acquire_ctx = NULL;
1422 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001423
1424retry:
1425 drm_modeset_backoff(state->acquire_ctx);
1426
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001427 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001428 if (ret)
1429 goto retry;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001430
1431 drm_for_each_crtc(crtc, dev)
1432 if (drm_crtc_mask(crtc) & crtc_mask)
1433 crtc->acquire_ctx = state->acquire_ctx;
1434
1435 if (global)
1436 dev->mode_config.acquire_ctx = state->acquire_ctx;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001437}
1438EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1439
1440/**
1441 * drm_atomic_check_only - check whether a given config would work
1442 * @state: atomic configuration to check
1443 *
1444 * Note that this function can return -EDEADLK if the driver needed to acquire
1445 * more locks but encountered a deadlock. The caller must then do the usual w/w
1446 * backoff dance and restart. All other errors are fatal.
1447 *
1448 * Returns:
1449 * 0 on success, negative error code on failure.
1450 */
1451int drm_atomic_check_only(struct drm_atomic_state *state)
1452{
Rob Clark5e743732014-12-18 16:01:51 -05001453 struct drm_device *dev = state->dev;
1454 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001455 struct drm_plane *plane;
1456 struct drm_plane_state *plane_state;
1457 struct drm_crtc *crtc;
1458 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001459 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001460
Daniel Vetter17a38d92015-02-22 12:24:16 +01001461 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001462
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001463 for_each_plane_in_state(state, plane, plane_state, i) {
1464 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001465 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001466 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1467 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001468 return ret;
1469 }
1470 }
1471
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001472 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1473 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001474 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001475 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1476 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001477 return ret;
1478 }
1479 }
1480
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001481 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001482 ret = config->funcs->atomic_check(state->dev, state);
1483
Rob Clarkd34f20d2014-12-18 16:01:56 -05001484 if (!state->allow_modeset) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001485 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001486 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001487 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1488 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001489 return -EINVAL;
1490 }
1491 }
1492 }
1493
Rob Clark5e743732014-12-18 16:01:51 -05001494 return ret;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001495}
1496EXPORT_SYMBOL(drm_atomic_check_only);
1497
1498/**
1499 * drm_atomic_commit - commit configuration atomically
1500 * @state: atomic configuration to check
1501 *
1502 * Note that this function can return -EDEADLK if the driver needed to acquire
1503 * more locks but encountered a deadlock. The caller must then do the usual w/w
1504 * backoff dance and restart. All other errors are fatal.
1505 *
1506 * Also note that on successful execution ownership of @state is transferred
1507 * from the caller of this function to the function itself. The caller must not
1508 * free or in any other way access @state. If the function fails then the caller
1509 * must clean up @state itself.
1510 *
1511 * Returns:
1512 * 0 on success, negative error code on failure.
1513 */
1514int drm_atomic_commit(struct drm_atomic_state *state)
1515{
1516 struct drm_mode_config *config = &state->dev->mode_config;
1517 int ret;
1518
1519 ret = drm_atomic_check_only(state);
1520 if (ret)
1521 return ret;
1522
Daniel Vetter17a38d92015-02-22 12:24:16 +01001523 DRM_DEBUG_ATOMIC("commiting %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001524
1525 return config->funcs->atomic_commit(state->dev, state, false);
1526}
1527EXPORT_SYMBOL(drm_atomic_commit);
1528
1529/**
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001530 * drm_atomic_nonblocking_commit - atomic&nonblocking configuration commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001531 * @state: atomic configuration to check
1532 *
1533 * Note that this function can return -EDEADLK if the driver needed to acquire
1534 * more locks but encountered a deadlock. The caller must then do the usual w/w
1535 * backoff dance and restart. All other errors are fatal.
1536 *
1537 * Also note that on successful execution ownership of @state is transferred
1538 * from the caller of this function to the function itself. The caller must not
1539 * free or in any other way access @state. If the function fails then the caller
1540 * must clean up @state itself.
1541 *
1542 * Returns:
1543 * 0 on success, negative error code on failure.
1544 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001545int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001546{
1547 struct drm_mode_config *config = &state->dev->mode_config;
1548 int ret;
1549
1550 ret = drm_atomic_check_only(state);
1551 if (ret)
1552 return ret;
1553
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001554 DRM_DEBUG_ATOMIC("commiting %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001555
1556 return config->funcs->atomic_commit(state->dev, state, true);
1557}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001558EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001559
Rob Clarkfceffb322016-11-05 11:08:09 -04001560static void drm_atomic_print_state(const struct drm_atomic_state *state)
1561{
1562 struct drm_printer p = drm_info_printer(state->dev->dev);
1563 struct drm_plane *plane;
1564 struct drm_plane_state *plane_state;
1565 struct drm_crtc *crtc;
1566 struct drm_crtc_state *crtc_state;
1567 struct drm_connector *connector;
1568 struct drm_connector_state *connector_state;
1569 int i;
1570
1571 DRM_DEBUG_ATOMIC("checking %p\n", state);
1572
1573 for_each_plane_in_state(state, plane, plane_state, i)
1574 drm_atomic_plane_print_state(&p, plane_state);
1575
1576 for_each_crtc_in_state(state, crtc, crtc_state, i)
1577 drm_atomic_crtc_print_state(&p, crtc_state);
1578
1579 for_each_connector_in_state(state, connector, connector_state, i)
1580 drm_atomic_connector_print_state(&p, connector_state);
1581}
1582
Rob Clark6559c902016-11-05 11:08:10 -04001583/**
1584 * drm_state_dump - dump entire device atomic state
1585 * @dev: the drm device
1586 * @p: where to print the state to
1587 *
1588 * Just for debugging. Drivers might want an option to dump state
1589 * to dmesg in case of error irq's. (Hint, you probably want to
1590 * ratelimit this!)
1591 *
1592 * The caller must drm_modeset_lock_all(), or if this is called
1593 * from error irq handler, it should not be enabled by default.
1594 * (Ie. if you are debugging errors you might not care that this
1595 * is racey. But calling this without all modeset locks held is
1596 * not inherently safe.)
1597 */
1598void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1599{
1600 struct drm_mode_config *config = &dev->mode_config;
1601 struct drm_plane *plane;
1602 struct drm_crtc *crtc;
1603 struct drm_connector *connector;
1604
1605 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1606 return;
1607
1608 list_for_each_entry(plane, &config->plane_list, head)
1609 drm_atomic_plane_print_state(p, plane->state);
1610
1611 list_for_each_entry(crtc, &config->crtc_list, head)
1612 drm_atomic_crtc_print_state(p, crtc->state);
1613
1614 list_for_each_entry(connector, &config->connector_list, head)
1615 drm_atomic_connector_print_state(p, connector->state);
1616}
1617EXPORT_SYMBOL(drm_state_dump);
1618
1619#ifdef CONFIG_DEBUG_FS
1620static int drm_state_info(struct seq_file *m, void *data)
1621{
1622 struct drm_info_node *node = (struct drm_info_node *) m->private;
1623 struct drm_device *dev = node->minor->dev;
1624 struct drm_printer p = drm_seq_file_printer(m);
1625
1626 drm_modeset_lock_all(dev);
1627 drm_state_dump(dev, &p);
1628 drm_modeset_unlock_all(dev);
1629
1630 return 0;
1631}
1632
1633/* any use in debugfs files to dump individual planes/crtc/etc? */
1634static const struct drm_info_list drm_atomic_debugfs_list[] = {
1635 {"state", drm_state_info, 0},
1636};
1637
1638int drm_atomic_debugfs_init(struct drm_minor *minor)
1639{
1640 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1641 ARRAY_SIZE(drm_atomic_debugfs_list),
1642 minor->debugfs_root, minor);
1643}
1644#endif
1645
Rob Clarkd34f20d2014-12-18 16:01:56 -05001646/*
1647 * The big monstor ioctl
1648 */
1649
1650static struct drm_pending_vblank_event *create_vblank_event(
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +02001651 struct drm_device *dev, struct drm_file *file_priv,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001652 struct dma_fence *fence, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001653{
1654 struct drm_pending_vblank_event *e = NULL;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001655 int ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001656
1657 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001658 if (!e)
1659 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001660
1661 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001662 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001663 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001664
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +02001665 if (file_priv) {
1666 ret = drm_event_reserve_init(dev, file_priv, &e->base,
1667 &e->event.base);
1668 if (ret) {
1669 kfree(e);
1670 return NULL;
1671 }
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001672 }
1673
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +02001674 e->base.fence = fence;
1675
Rob Clarkd34f20d2014-12-18 16:01:56 -05001676 return e;
1677}
1678
Rob Clarkd34f20d2014-12-18 16:01:56 -05001679static int atomic_set_prop(struct drm_atomic_state *state,
1680 struct drm_mode_object *obj, struct drm_property *prop,
1681 uint64_t prop_value)
1682{
1683 struct drm_mode_object *ref;
1684 int ret;
1685
1686 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1687 return -EINVAL;
1688
1689 switch (obj->type) {
1690 case DRM_MODE_OBJECT_CONNECTOR: {
1691 struct drm_connector *connector = obj_to_connector(obj);
1692 struct drm_connector_state *connector_state;
1693
1694 connector_state = drm_atomic_get_connector_state(state, connector);
1695 if (IS_ERR(connector_state)) {
1696 ret = PTR_ERR(connector_state);
1697 break;
1698 }
1699
1700 ret = drm_atomic_connector_set_property(connector,
1701 connector_state, prop, prop_value);
1702 break;
1703 }
1704 case DRM_MODE_OBJECT_CRTC: {
1705 struct drm_crtc *crtc = obj_to_crtc(obj);
1706 struct drm_crtc_state *crtc_state;
1707
1708 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1709 if (IS_ERR(crtc_state)) {
1710 ret = PTR_ERR(crtc_state);
1711 break;
1712 }
1713
1714 ret = drm_atomic_crtc_set_property(crtc,
1715 crtc_state, prop, prop_value);
1716 break;
1717 }
1718 case DRM_MODE_OBJECT_PLANE: {
1719 struct drm_plane *plane = obj_to_plane(obj);
1720 struct drm_plane_state *plane_state;
1721
1722 plane_state = drm_atomic_get_plane_state(state, plane);
1723 if (IS_ERR(plane_state)) {
1724 ret = PTR_ERR(plane_state);
1725 break;
1726 }
1727
1728 ret = drm_atomic_plane_set_property(plane,
1729 plane_state, prop, prop_value);
1730 break;
1731 }
1732 default:
1733 ret = -EINVAL;
1734 break;
1735 }
1736
1737 drm_property_change_valid_put(prop, ref);
1738 return ret;
1739}
1740
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001741/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001742 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001743 *
1744 * @dev: drm device to check.
1745 * @plane_mask: plane mask for planes that were updated.
1746 * @ret: return value, can be -EDEADLK for a retry.
1747 *
1748 * Before doing an update plane->old_fb is set to plane->fb,
1749 * but before dropping the locks old_fb needs to be set to NULL
1750 * and plane->fb updated. This is a common operation for each
1751 * atomic update, so this call is split off as a helper.
1752 */
1753void drm_atomic_clean_old_fb(struct drm_device *dev,
1754 unsigned plane_mask,
1755 int ret)
1756{
1757 struct drm_plane *plane;
1758
1759 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1760 * locks (ie. while it is still safe to deref plane->state). We
1761 * need to do this here because the driver entry points cannot
1762 * distinguish between legacy and atomic ioctls.
1763 */
1764 drm_for_each_plane_mask(plane, dev, plane_mask) {
1765 if (ret == 0) {
1766 struct drm_framebuffer *new_fb = plane->state->fb;
1767 if (new_fb)
1768 drm_framebuffer_reference(new_fb);
1769 plane->fb = new_fb;
1770 plane->crtc = plane->state->crtc;
1771
1772 if (plane->old_fb)
1773 drm_framebuffer_unreference(plane->old_fb);
1774 }
1775 plane->old_fb = NULL;
1776 }
1777}
1778EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1779
Rob Clarkd34f20d2014-12-18 16:01:56 -05001780int drm_mode_atomic_ioctl(struct drm_device *dev,
1781 void *data, struct drm_file *file_priv)
1782{
1783 struct drm_mode_atomic *arg = data;
1784 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1785 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1786 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1787 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1788 unsigned int copied_objs, copied_props;
1789 struct drm_atomic_state *state;
1790 struct drm_modeset_acquire_ctx ctx;
1791 struct drm_plane *plane;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001792 struct drm_crtc *crtc;
1793 struct drm_crtc_state *crtc_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01001794 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001795 int ret = 0;
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02001796 unsigned int i, j;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001797
1798 /* disallow for drivers not supporting atomic: */
1799 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1800 return -EINVAL;
1801
1802 /* disallow for userspace that has not enabled atomic cap (even
1803 * though this may be a bit overkill, since legacy userspace
1804 * wouldn't know how to call this ioctl)
1805 */
1806 if (!file_priv->atomic)
1807 return -EINVAL;
1808
1809 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1810 return -EINVAL;
1811
1812 if (arg->reserved)
1813 return -EINVAL;
1814
1815 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
1816 !dev->mode_config.async_page_flip)
1817 return -EINVAL;
1818
1819 /* can't test and expect an event at the same time. */
1820 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1821 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1822 return -EINVAL;
1823
1824 drm_modeset_acquire_init(&ctx, 0);
1825
1826 state = drm_atomic_state_alloc(dev);
1827 if (!state)
1828 return -ENOMEM;
1829
1830 state->acquire_ctx = &ctx;
1831 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1832
1833retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01001834 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001835 copied_objs = 0;
1836 copied_props = 0;
1837
1838 for (i = 0; i < arg->count_objs; i++) {
1839 uint32_t obj_id, count_props;
1840 struct drm_mode_object *obj;
1841
1842 if (get_user(obj_id, objs_ptr + copied_objs)) {
1843 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001844 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001845 }
1846
1847 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10001848 if (!obj) {
1849 ret = -ENOENT;
1850 goto out;
1851 }
1852
1853 if (!obj->properties) {
1854 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001855 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001856 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001857 }
1858
Rob Clarkd34f20d2014-12-18 16:01:56 -05001859 if (get_user(count_props, count_props_ptr + copied_objs)) {
Dave Airlieb164d312016-04-27 11:10:09 +10001860 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001861 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001862 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001863 }
1864
1865 copied_objs++;
1866
1867 for (j = 0; j < count_props; j++) {
1868 uint32_t prop_id;
1869 uint64_t prop_value;
1870 struct drm_property *prop;
1871
1872 if (get_user(prop_id, props_ptr + copied_props)) {
Dave Airlieb164d312016-04-27 11:10:09 +10001873 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001874 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001875 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001876 }
1877
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02001878 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001879 if (!prop) {
Dave Airlieb164d312016-04-27 11:10:09 +10001880 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001881 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001882 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001883 }
1884
Guenter Roeck42c58142015-01-12 21:12:17 -08001885 if (copy_from_user(&prop_value,
1886 prop_values_ptr + copied_props,
1887 sizeof(prop_value))) {
Dave Airlieb164d312016-04-27 11:10:09 +10001888 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001889 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001890 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001891 }
1892
1893 ret = atomic_set_prop(state, obj, prop, prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10001894 if (ret) {
1895 drm_mode_object_unreference(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001896 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10001897 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05001898
1899 copied_props++;
1900 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001901
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001902 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
1903 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02001904 plane = obj_to_plane(obj);
1905 plane_mask |= (1 << drm_plane_index(plane));
1906 plane->old_fb = plane->fb;
1907 }
Dave Airlieb164d312016-04-27 11:10:09 +10001908 drm_mode_object_unreference(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001909 }
1910
1911 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001912 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Rob Clarkd34f20d2014-12-18 16:01:56 -05001913 struct drm_pending_vblank_event *e;
1914
Gustavo Padovan1b47aaf2016-06-02 00:06:35 +02001915 e = create_vblank_event(dev, file_priv, NULL,
1916 arg->user_data);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001917 if (!e) {
1918 ret = -ENOMEM;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001919 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001920 }
1921
1922 crtc_state->event = e;
1923 }
1924 }
1925
1926 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001927 /*
1928 * Unlike commit, check_only does not clean up state.
Chris Wilson08536952016-10-14 13:18:18 +01001929 * Below we call drm_atomic_state_put for it.
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001930 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05001931 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001932 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001933 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001934 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04001935 if (unlikely(drm_debug & DRM_UT_STATE))
1936 drm_atomic_print_state(state);
1937
Rob Clarkd34f20d2014-12-18 16:01:56 -05001938 ret = drm_atomic_commit(state);
1939 }
1940
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001941out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001942 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001943
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001944 if (ret && arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1945 /*
1946 * TEST_ONLY and PAGE_FLIP_EVENT are mutually exclusive,
1947 * if they weren't, this code should be called on success
1948 * for TEST_ONLY too.
1949 */
1950
1951 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1952 if (!crtc_state->event)
1953 continue;
1954
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001955 drm_event_cancel_free(dev, &crtc_state->event->base);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02001956 }
1957 }
1958
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02001959 if (ret == -EDEADLK) {
1960 drm_atomic_state_clear(state);
1961 drm_modeset_backoff(&ctx);
1962 goto retry;
1963 }
1964
Chris Wilson08536952016-10-14 13:18:18 +01001965 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001966
1967 drm_modeset_drop_locks(&ctx);
1968 drm_modeset_acquire_fini(&ctx);
1969
1970 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001971}