blob: aed25c4183bb0e5df17a751e25a33c907b91d9b1 [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>
Gustavo Padovan96260142016-11-15 22:06:39 +090034#include <linux/sync_file.h>
Daniel Vettercc4ceb42014-07-25 21:30:38 +020035
Thierry Redingbe35f942016-04-28 15:19:56 +020036#include "drm_crtc_internal.h"
37
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010038void __drm_crtc_commit_free(struct kref *kref)
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020039{
40 struct drm_crtc_commit *commit =
41 container_of(kref, struct drm_crtc_commit, ref);
42
43 kfree(commit);
44}
Daniel Vetterb3ba3f62016-12-21 14:03:35 +010045EXPORT_SYMBOL(__drm_crtc_commit_free);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +020046
Maarten Lankhorst036ef572015-05-18 10:06:40 +020047/**
48 * drm_atomic_state_default_release -
49 * release memory initialized by drm_atomic_state_init
50 * @state: atomic state
51 *
52 * Free all the memory allocated by drm_atomic_state_init.
53 * This is useful for drivers that subclass the atomic state.
54 */
55void drm_atomic_state_default_release(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020056{
57 kfree(state->connectors);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020058 kfree(state->crtcs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020059 kfree(state->planes);
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -070060 kfree(state->private_objs);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020061}
Maarten Lankhorst036ef572015-05-18 10:06:40 +020062EXPORT_SYMBOL(drm_atomic_state_default_release);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020063
64/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +020065 * drm_atomic_state_init - init new atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020066 * @dev: DRM device
Maarten Lankhorst036ef572015-05-18 10:06:40 +020067 * @state: atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +020068 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +020069 * Default implementation for filling in a new atomic state.
70 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +020071 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +020072int
73drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +020074{
Chris Wilson08536952016-10-14 13:18:18 +010075 kref_init(&state->ref);
76
Rob Clarkd34f20d2014-12-18 16:01:56 -050077 /* TODO legacy paths should maybe do a better job about
78 * setting this appropriately?
79 */
80 state->allow_modeset = true;
81
Daniel Vettercc4ceb42014-07-25 21:30:38 +020082 state->crtcs = kcalloc(dev->mode_config.num_crtc,
83 sizeof(*state->crtcs), GFP_KERNEL);
84 if (!state->crtcs)
85 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020086 state->planes = kcalloc(dev->mode_config.num_total_plane,
87 sizeof(*state->planes), GFP_KERNEL);
88 if (!state->planes)
89 goto fail;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020090
91 state->dev = dev;
92
Maarten Lankhorst036ef572015-05-18 10:06:40 +020093 DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +020094
Maarten Lankhorst036ef572015-05-18 10:06:40 +020095 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +020096fail:
Maarten Lankhorst036ef572015-05-18 10:06:40 +020097 drm_atomic_state_default_release(state);
98 return -ENOMEM;
99}
100EXPORT_SYMBOL(drm_atomic_state_init);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200101
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200102/**
103 * drm_atomic_state_alloc - allocate atomic state
104 * @dev: DRM device
105 *
106 * This allocates an empty atomic state to track updates.
107 */
108struct drm_atomic_state *
109drm_atomic_state_alloc(struct drm_device *dev)
110{
111 struct drm_mode_config *config = &dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200112
113 if (!config->funcs->atomic_state_alloc) {
Dawid Kurekac7c7482017-06-15 19:45:56 +0200114 struct drm_atomic_state *state;
115
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200116 state = kzalloc(sizeof(*state), GFP_KERNEL);
117 if (!state)
118 return NULL;
119 if (drm_atomic_state_init(dev, state) < 0) {
120 kfree(state);
121 return NULL;
122 }
123 return state;
124 }
125
126 return config->funcs->atomic_state_alloc(dev);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200127}
128EXPORT_SYMBOL(drm_atomic_state_alloc);
129
130/**
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200131 * drm_atomic_state_default_clear - clear base atomic state
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200132 * @state: atomic state
133 *
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200134 * Default implementation for clearing atomic state.
135 * This is useful for drivers that subclass the atomic state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200136 */
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200137void drm_atomic_state_default_clear(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200138{
139 struct drm_device *dev = state->dev;
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100140 struct drm_mode_config *config = &dev->mode_config;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200141 int i;
142
Daniel Vetter17a38d92015-02-22 12:24:16 +0100143 DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200144
Daniel Vetterf52b69f12014-11-19 18:38:08 +0100145 for (i = 0; i < state->num_connector; i++) {
Daniel Vetter63e83c12016-06-02 00:06:32 +0200146 struct drm_connector *connector = state->connectors[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200147
148 if (!connector)
149 continue;
150
Dave Airlied2307de2016-04-27 11:27:39 +1000151 connector->funcs->atomic_destroy_state(connector,
Daniel Vetter63e83c12016-06-02 00:06:32 +0200152 state->connectors[i].state);
153 state->connectors[i].ptr = NULL;
154 state->connectors[i].state = NULL;
Thierry Redingad093602017-02-28 15:46:39 +0100155 drm_connector_put(connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200156 }
157
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100158 for (i = 0; i < config->num_crtc; i++) {
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200159 struct drm_crtc *crtc = state->crtcs[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200160
161 if (!crtc)
162 continue;
163
164 crtc->funcs->atomic_destroy_state(crtc,
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200165 state->crtcs[i].state);
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200166
167 if (state->crtcs[i].commit) {
168 kfree(state->crtcs[i].commit->event);
169 state->crtcs[i].commit->event = NULL;
170 drm_crtc_commit_put(state->crtcs[i].commit);
171 }
172
173 state->crtcs[i].commit = NULL;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200174 state->crtcs[i].ptr = NULL;
175 state->crtcs[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200176 }
177
Daniel Vetter6f75cea2014-11-19 18:38:07 +0100178 for (i = 0; i < config->num_total_plane; i++) {
Daniel Vetterb8b53422016-06-02 00:06:33 +0200179 struct drm_plane *plane = state->planes[i].ptr;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200180
181 if (!plane)
182 continue;
183
184 plane->funcs->atomic_destroy_state(plane,
Daniel Vetterb8b53422016-06-02 00:06:33 +0200185 state->planes[i].state);
186 state->planes[i].ptr = NULL;
187 state->planes[i].state = NULL;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200188 }
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700189
190 for (i = 0; i < state->num_private_objs; i++) {
191 void *obj_state = state->private_objs[i].obj_state;
192
193 state->private_objs[i].funcs->destroy_state(obj_state);
194 state->private_objs[i].obj = NULL;
195 state->private_objs[i].obj_state = NULL;
196 state->private_objs[i].funcs = NULL;
197 }
198 state->num_private_objs = 0;
199
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200200}
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200201EXPORT_SYMBOL(drm_atomic_state_default_clear);
202
203/**
204 * drm_atomic_state_clear - clear state object
205 * @state: atomic state
206 *
207 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
208 * all locks. So someone else could sneak in and change the current modeset
209 * configuration. Which means that all the state assembled in @state is no
210 * longer an atomic update to the current state, but to some arbitrary earlier
Daniel Vetterd5745282017-01-25 07:26:45 +0100211 * state. Which could break assumptions the driver's
212 * &drm_mode_config_funcs.atomic_check likely relies on.
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200213 *
214 * Hence we must clear all cached state and completely start over, using this
215 * function.
216 */
217void drm_atomic_state_clear(struct drm_atomic_state *state)
218{
219 struct drm_device *dev = state->dev;
220 struct drm_mode_config *config = &dev->mode_config;
221
222 if (config->funcs->atomic_state_clear)
223 config->funcs->atomic_state_clear(state);
224 else
225 drm_atomic_state_default_clear(state);
226}
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200227EXPORT_SYMBOL(drm_atomic_state_clear);
228
229/**
Chris Wilson08536952016-10-14 13:18:18 +0100230 * __drm_atomic_state_free - free all memory for an atomic state
231 * @ref: This atomic state to deallocate
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200232 *
233 * This frees all memory associated with an atomic state, including all the
234 * per-object state for planes, crtcs and connectors.
235 */
Chris Wilson08536952016-10-14 13:18:18 +0100236void __drm_atomic_state_free(struct kref *ref)
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200237{
Chris Wilson08536952016-10-14 13:18:18 +0100238 struct drm_atomic_state *state = container_of(ref, typeof(*state), ref);
239 struct drm_mode_config *config = &state->dev->mode_config;
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200240
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200241 drm_atomic_state_clear(state);
242
Daniel Vetter17a38d92015-02-22 12:24:16 +0100243 DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200244
Maarten Lankhorst036ef572015-05-18 10:06:40 +0200245 if (config->funcs->atomic_state_free) {
246 config->funcs->atomic_state_free(state);
247 } else {
248 drm_atomic_state_default_release(state);
249 kfree(state);
250 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200251}
Chris Wilson08536952016-10-14 13:18:18 +0100252EXPORT_SYMBOL(__drm_atomic_state_free);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200253
254/**
255 * drm_atomic_get_crtc_state - get crtc state
256 * @state: global atomic state object
257 * @crtc: crtc to get state object for
258 *
259 * This function returns the crtc state for the given crtc, allocating it if
260 * needed. It will also grab the relevant crtc lock to make sure that the state
261 * is consistent.
262 *
263 * Returns:
264 *
265 * Either the allocated state or the error code encoded into the pointer. When
266 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
267 * entire atomic sequence must be restarted. All other errors are fatal.
268 */
269struct drm_crtc_state *
270drm_atomic_get_crtc_state(struct drm_atomic_state *state,
271 struct drm_crtc *crtc)
272{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200273 int ret, index = drm_crtc_index(crtc);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200274 struct drm_crtc_state *crtc_state;
275
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200276 WARN_ON(!state->acquire_ctx);
277
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200278 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
279 if (crtc_state)
280 return crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200281
282 ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
283 if (ret)
284 return ERR_PTR(ret);
285
286 crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
287 if (!crtc_state)
288 return ERR_PTR(-ENOMEM);
289
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200290 state->crtcs[index].state = crtc_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100291 state->crtcs[index].old_state = crtc->state;
292 state->crtcs[index].new_state = crtc_state;
Daniel Vetter5d943aa62016-06-02 00:06:34 +0200293 state->crtcs[index].ptr = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200294 crtc_state->state = state;
295
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200296 DRM_DEBUG_ATOMIC("Added [CRTC:%d:%s] %p state to %p\n",
297 crtc->base.id, crtc->name, crtc_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200298
299 return crtc_state;
300}
301EXPORT_SYMBOL(drm_atomic_get_crtc_state);
302
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900303static void set_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200304 struct drm_crtc *crtc, s32 __user *fence_ptr)
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900305{
306 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
307}
308
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200309static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900310 struct drm_crtc *crtc)
311{
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200312 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900313
314 fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
315 state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
316
317 return fence_ptr;
318}
319
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200320/**
Daniel Stone819364d2015-05-26 14:36:48 +0100321 * drm_atomic_set_mode_for_crtc - set mode for CRTC
322 * @state: the CRTC whose incoming state to update
323 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
324 *
Dhinakaran Pandiyancbef9092017-01-30 22:18:38 -0800325 * Set a mode (originating from the kernel) on the desired CRTC state and update
326 * the enable property.
Daniel Stone819364d2015-05-26 14:36:48 +0100327 *
328 * RETURNS:
329 * Zero on success, error code on failure. Cannot return -EDEADLK.
330 */
331int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
Ville Syrjälä91110a42017-05-18 22:38:36 +0300332 const struct drm_display_mode *mode)
Daniel Stone819364d2015-05-26 14:36:48 +0100333{
Daniel Stone99cf4a22015-05-25 19:11:51 +0100334 struct drm_mode_modeinfo umode;
335
Daniel Stone819364d2015-05-26 14:36:48 +0100336 /* Early return for no change. */
337 if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
338 return 0;
339
Thierry Reding6472e502017-02-28 15:46:42 +0100340 drm_property_blob_put(state->mode_blob);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100341 state->mode_blob = NULL;
342
Daniel Stone819364d2015-05-26 14:36:48 +0100343 if (mode) {
Daniel Stone99cf4a22015-05-25 19:11:51 +0100344 drm_mode_convert_to_umode(&umode, mode);
345 state->mode_blob =
346 drm_property_create_blob(state->crtc->dev,
347 sizeof(umode),
348 &umode);
349 if (IS_ERR(state->mode_blob))
350 return PTR_ERR(state->mode_blob);
351
Daniel Stone819364d2015-05-26 14:36:48 +0100352 drm_mode_copy(&state->mode, mode);
353 state->enable = true;
354 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
355 mode->name, state);
356 } else {
357 memset(&state->mode, 0, sizeof(state->mode));
358 state->enable = false;
359 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
360 state);
361 }
362
363 return 0;
364}
365EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
366
Daniel Stone819364d2015-05-26 14:36:48 +0100367/**
Daniel Stone955f3c32015-05-25 19:11:52 +0100368 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
369 * @state: the CRTC whose incoming state to update
370 * @blob: pointer to blob property to use for mode
371 *
372 * Set a mode (originating from a blob property) on the desired CRTC state.
373 * This function will take a reference on the blob property for the CRTC state,
374 * and release the reference held on the state's existing mode property, if any
375 * was set.
376 *
377 * RETURNS:
378 * Zero on success, error code on failure. Cannot return -EDEADLK.
379 */
380int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
381 struct drm_property_blob *blob)
382{
383 if (blob == state->mode_blob)
384 return 0;
385
Thierry Reding6472e502017-02-28 15:46:42 +0100386 drm_property_blob_put(state->mode_blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100387 state->mode_blob = NULL;
388
Tomi Valkeinen67098872016-05-31 15:03:17 +0300389 memset(&state->mode, 0, sizeof(state->mode));
390
Daniel Stone955f3c32015-05-25 19:11:52 +0100391 if (blob) {
392 if (blob->length != sizeof(struct drm_mode_modeinfo) ||
393 drm_mode_convert_umode(&state->mode,
394 (const struct drm_mode_modeinfo *)
395 blob->data))
396 return -EINVAL;
397
Thierry Reding6472e502017-02-28 15:46:42 +0100398 state->mode_blob = drm_property_blob_get(blob);
Daniel Stone955f3c32015-05-25 19:11:52 +0100399 state->enable = true;
400 DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
401 state->mode.name, state);
402 } else {
Daniel Stone955f3c32015-05-25 19:11:52 +0100403 state->enable = false;
404 DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
405 state);
406 }
407
408 return 0;
409}
410EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
411
412/**
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000413 * drm_atomic_replace_property_blob - replace a blob property
414 * @blob: a pointer to the member blob to be replaced
415 * @new_blob: the new blob to replace with
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000416 * @replaced: whether the blob has been replaced
417 *
418 * RETURNS:
419 * Zero on success, error code on failure
420 */
421static void
422drm_atomic_replace_property_blob(struct drm_property_blob **blob,
423 struct drm_property_blob *new_blob,
424 bool *replaced)
425{
426 struct drm_property_blob *old_blob = *blob;
427
428 if (old_blob == new_blob)
429 return;
430
Thierry Reding6472e502017-02-28 15:46:42 +0100431 drm_property_blob_put(old_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000432 if (new_blob)
Thierry Reding6472e502017-02-28 15:46:42 +0100433 drm_property_blob_get(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000434 *blob = new_blob;
435 *replaced = true;
436
437 return;
438}
439
440static int
Jyri Sarhadafee602017-04-21 12:51:13 +0300441drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000442 struct drm_property_blob **blob,
443 uint64_t blob_id,
444 ssize_t expected_size,
445 bool *replaced)
446{
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000447 struct drm_property_blob *new_blob = NULL;
448
449 if (blob_id != 0) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300450 new_blob = drm_property_lookup_blob(dev, blob_id);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000451 if (new_blob == NULL)
452 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100453
454 if (expected_size > 0 && expected_size != new_blob->length) {
Thierry Reding6472e502017-02-28 15:46:42 +0100455 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000456 return -EINVAL;
Felix Monningercac5fced2016-10-25 22:28:08 +0100457 }
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000458 }
459
460 drm_atomic_replace_property_blob(blob, new_blob, replaced);
Thierry Reding6472e502017-02-28 15:46:42 +0100461 drm_property_blob_put(new_blob);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000462
463 return 0;
464}
465
466/**
Rob Clark40ecc692014-12-18 16:01:46 -0500467 * drm_atomic_crtc_set_property - set property on CRTC
468 * @crtc: the drm CRTC to set a property on
469 * @state: the state object to update with the new property value
470 * @property: the property to set
471 * @val: the new property value
472 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100473 * This function handles generic/core properties and calls out to driver's
474 * &drm_crtc_funcs.atomic_set_property for driver properties. To ensure
475 * consistent behavior you must call this function rather than the driver hook
476 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500477 *
478 * RETURNS:
479 * Zero on success, error code on failure
480 */
481int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
482 struct drm_crtc_state *state, struct drm_property *property,
483 uint64_t val)
484{
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100485 struct drm_device *dev = crtc->dev;
486 struct drm_mode_config *config = &dev->mode_config;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000487 bool replaced = false;
Daniel Stone955f3c32015-05-25 19:11:52 +0100488 int ret;
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100489
Daniel Stone27798362015-03-19 04:33:26 +0000490 if (property == config->prop_active)
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100491 state->active = val;
Daniel Stone955f3c32015-05-25 19:11:52 +0100492 else if (property == config->prop_mode_id) {
493 struct drm_property_blob *mode =
494 drm_property_lookup_blob(dev, val);
495 ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
Thierry Reding6472e502017-02-28 15:46:42 +0100496 drm_property_blob_put(mode);
Daniel Stone955f3c32015-05-25 19:11:52 +0100497 return ret;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000498 } else if (property == config->degamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300499 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000500 &state->degamma_lut,
501 val,
502 -1,
503 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200504 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000505 return ret;
506 } else if (property == config->ctm_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300507 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000508 &state->ctm,
509 val,
510 sizeof(struct drm_color_ctm),
511 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200512 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000513 return ret;
514 } else if (property == config->gamma_lut_property) {
Jyri Sarhadafee602017-04-21 12:51:13 +0300515 ret = drm_atomic_replace_property_blob_from_id(dev,
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000516 &state->gamma_lut,
517 val,
518 -1,
519 &replaced);
Mario Kleineradd1fa72016-08-27 01:02:28 +0200520 state->color_mgmt_changed |= replaced;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000521 return ret;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900522 } else if (property == config->prop_out_fence_ptr) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -0200523 s32 __user *fence_ptr = u64_to_user_ptr(val);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900524
525 if (!fence_ptr)
526 return 0;
527
528 if (put_user(-1, fence_ptr))
529 return -EFAULT;
530
531 set_out_fence_for_crtc(state->state, crtc, fence_ptr);
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000532 } else if (crtc->funcs->atomic_set_property)
Rob Clark40ecc692014-12-18 16:01:46 -0500533 return crtc->funcs->atomic_set_property(crtc, state, property, val);
Daniel Stone27798362015-03-19 04:33:26 +0000534 else
535 return -EINVAL;
536
537 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500538}
539EXPORT_SYMBOL(drm_atomic_crtc_set_property);
540
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100541/**
542 * drm_atomic_crtc_get_property - get property value from CRTC state
543 * @crtc: the drm CRTC to set a property on
544 * @state: the state object to get the property value from
545 * @property: the property to set
546 * @val: return location for the property value
547 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100548 * This function handles generic/core properties and calls out to driver's
549 * &drm_crtc_funcs.atomic_get_property for driver properties. To ensure
550 * consistent behavior you must call this function rather than the driver hook
551 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100552 *
553 * RETURNS:
554 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500555 */
Geliang Tangbf22f3b2015-09-24 03:01:03 -0700556static int
557drm_atomic_crtc_get_property(struct drm_crtc *crtc,
Rob Clarkac9c9252014-12-18 16:01:47 -0500558 const struct drm_crtc_state *state,
559 struct drm_property *property, uint64_t *val)
560{
Daniel Stone8f164ce2015-03-19 04:33:25 +0000561 struct drm_device *dev = crtc->dev;
562 struct drm_mode_config *config = &dev->mode_config;
563
564 if (property == config->prop_active)
565 *val = state->active;
Daniel Stone955f3c32015-05-25 19:11:52 +0100566 else if (property == config->prop_mode_id)
567 *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
Lionel Landwerlin5488dc12016-02-26 17:05:00 +0000568 else if (property == config->degamma_lut_property)
569 *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
570 else if (property == config->ctm_property)
571 *val = (state->ctm) ? state->ctm->base.id : 0;
572 else if (property == config->gamma_lut_property)
573 *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +0900574 else if (property == config->prop_out_fence_ptr)
575 *val = 0;
Daniel Stone8f164ce2015-03-19 04:33:25 +0000576 else if (crtc->funcs->atomic_get_property)
Rob Clarkac9c9252014-12-18 16:01:47 -0500577 return crtc->funcs->atomic_get_property(crtc, state, property, val);
Daniel Stone8f164ce2015-03-19 04:33:25 +0000578 else
579 return -EINVAL;
580
581 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500582}
Rob Clarkac9c9252014-12-18 16:01:47 -0500583
584/**
Rob Clark5e743732014-12-18 16:01:51 -0500585 * drm_atomic_crtc_check - check crtc state
586 * @crtc: crtc to check
587 * @state: crtc state to check
588 *
589 * Provides core sanity checks for crtc state.
590 *
591 * RETURNS:
592 * Zero on success, error code on failure
593 */
594static int drm_atomic_crtc_check(struct drm_crtc *crtc,
595 struct drm_crtc_state *state)
596{
597 /* NOTE: we explicitly don't enforce constraints such as primary
598 * layer covering entire screen, since that is something we want
599 * to allow (on hw that supports it). For hw that does not, it
600 * should be checked in driver's crtc->atomic_check() vfunc.
601 *
602 * TODO: Add generic modeset state checks once we support those.
603 */
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100604
605 if (state->active && !state->enable) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200606 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] active without enabled\n",
607 crtc->base.id, crtc->name);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100608 return -EINVAL;
609 }
610
Daniel Stone99cf4a22015-05-25 19:11:51 +0100611 /* The state->enable vs. state->mode_blob checks can be WARN_ON,
612 * as this is a kernel-internal detail that userspace should never
613 * be able to trigger. */
614 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
615 WARN_ON(state->enable && !state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200616 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] enabled without mode blob\n",
617 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100618 return -EINVAL;
619 }
620
621 if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
622 WARN_ON(!state->enable && state->mode_blob)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200623 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] disabled with mode blob\n",
624 crtc->base.id, crtc->name);
Daniel Stone99cf4a22015-05-25 19:11:51 +0100625 return -EINVAL;
626 }
627
Daniel Vetter4cba6852015-12-08 09:49:20 +0100628 /*
629 * Reject event generation for when a CRTC is off and stays off.
630 * It wouldn't be hard to implement this, but userspace has a track
631 * record of happily burning through 100% cpu (or worse, crash) when the
632 * display pipe is suspended. To avoid all that fun just reject updates
633 * that ask for events since likely that indicates a bug in the
634 * compositor's drawing loop. This is consistent with the vblank IOCTL
635 * and legacy page_flip IOCTL which also reject service on a disabled
636 * pipe.
637 */
638 if (state->event && !state->active && !crtc->state->active) {
Russell King6ac7c542017-02-13 12:27:03 +0000639 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requesting event but off\n",
640 crtc->base.id, crtc->name);
Daniel Vetter4cba6852015-12-08 09:49:20 +0100641 return -EINVAL;
642 }
643
Rob Clark5e743732014-12-18 16:01:51 -0500644 return 0;
645}
646
Rob Clarkfceffb322016-11-05 11:08:09 -0400647static void drm_atomic_crtc_print_state(struct drm_printer *p,
648 const struct drm_crtc_state *state)
649{
650 struct drm_crtc *crtc = state->crtc;
651
652 drm_printf(p, "crtc[%u]: %s\n", crtc->base.id, crtc->name);
653 drm_printf(p, "\tenable=%d\n", state->enable);
654 drm_printf(p, "\tactive=%d\n", state->active);
655 drm_printf(p, "\tplanes_changed=%d\n", state->planes_changed);
656 drm_printf(p, "\tmode_changed=%d\n", state->mode_changed);
657 drm_printf(p, "\tactive_changed=%d\n", state->active_changed);
658 drm_printf(p, "\tconnectors_changed=%d\n", state->connectors_changed);
659 drm_printf(p, "\tcolor_mgmt_changed=%d\n", state->color_mgmt_changed);
660 drm_printf(p, "\tplane_mask=%x\n", state->plane_mask);
661 drm_printf(p, "\tconnector_mask=%x\n", state->connector_mask);
662 drm_printf(p, "\tencoder_mask=%x\n", state->encoder_mask);
663 drm_printf(p, "\tmode: " DRM_MODE_FMT "\n", DRM_MODE_ARG(&state->mode));
664
665 if (crtc->funcs->atomic_print_state)
666 crtc->funcs->atomic_print_state(p, state);
667}
668
Rob Clark5e743732014-12-18 16:01:51 -0500669/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200670 * drm_atomic_get_plane_state - get plane state
671 * @state: global atomic state object
672 * @plane: plane to get state object for
673 *
674 * This function returns the plane state for the given plane, allocating it if
675 * needed. It will also grab the relevant plane lock to make sure that the state
676 * is consistent.
677 *
678 * Returns:
679 *
680 * Either the allocated state or the error code encoded into the pointer. When
681 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
682 * entire atomic sequence must be restarted. All other errors are fatal.
683 */
684struct drm_plane_state *
685drm_atomic_get_plane_state(struct drm_atomic_state *state,
686 struct drm_plane *plane)
687{
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200688 int ret, index = drm_plane_index(plane);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200689 struct drm_plane_state *plane_state;
690
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +0200691 WARN_ON(!state->acquire_ctx);
692
Maarten Lankhorst1b26a5e2015-05-13 10:37:25 +0200693 plane_state = drm_atomic_get_existing_plane_state(state, plane);
694 if (plane_state)
695 return plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200696
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100697 ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200698 if (ret)
699 return ERR_PTR(ret);
700
701 plane_state = plane->funcs->atomic_duplicate_state(plane);
702 if (!plane_state)
703 return ERR_PTR(-ENOMEM);
704
Daniel Vetterb8b53422016-06-02 00:06:33 +0200705 state->planes[index].state = plane_state;
706 state->planes[index].ptr = plane;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +0100707 state->planes[index].old_state = plane->state;
708 state->planes[index].new_state = plane_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200709 plane_state->state = state;
710
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200711 DRM_DEBUG_ATOMIC("Added [PLANE:%d:%s] %p state to %p\n",
712 plane->base.id, plane->name, plane_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +0200713
714 if (plane_state->crtc) {
715 struct drm_crtc_state *crtc_state;
716
717 crtc_state = drm_atomic_get_crtc_state(state,
718 plane_state->crtc);
719 if (IS_ERR(crtc_state))
720 return ERR_CAST(crtc_state);
721 }
722
723 return plane_state;
724}
725EXPORT_SYMBOL(drm_atomic_get_plane_state);
726
727/**
Rob Clark40ecc692014-12-18 16:01:46 -0500728 * drm_atomic_plane_set_property - set property on plane
729 * @plane: the drm plane to set a property on
730 * @state: the state object to update with the new property value
731 * @property: the property to set
732 * @val: the new property value
733 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100734 * This function handles generic/core properties and calls out to driver's
735 * &drm_plane_funcs.atomic_set_property for driver properties. To ensure
736 * consistent behavior you must call this function rather than the driver hook
737 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -0500738 *
739 * RETURNS:
740 * Zero on success, error code on failure
741 */
742int drm_atomic_plane_set_property(struct drm_plane *plane,
743 struct drm_plane_state *state, struct drm_property *property,
744 uint64_t val)
745{
Rob Clark6b4959f2014-12-18 16:01:53 -0500746 struct drm_device *dev = plane->dev;
747 struct drm_mode_config *config = &dev->mode_config;
748
749 if (property == config->prop_fb_id) {
750 struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
751 drm_atomic_set_fb_for_plane(state, fb);
752 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100753 drm_framebuffer_put(fb);
Gustavo Padovan96260142016-11-15 22:06:39 +0900754 } else if (property == config->prop_in_fence_fd) {
755 if (state->fence)
756 return -EINVAL;
757
758 if (U642I64(val) == -1)
759 return 0;
760
761 state->fence = sync_file_get_fence(val);
762 if (!state->fence)
763 return -EINVAL;
764
Rob Clark6b4959f2014-12-18 16:01:53 -0500765 } else if (property == config->prop_crtc_id) {
766 struct drm_crtc *crtc = drm_crtc_find(dev, val);
767 return drm_atomic_set_crtc_for_plane(state, crtc);
768 } else if (property == config->prop_crtc_x) {
769 state->crtc_x = U642I64(val);
770 } else if (property == config->prop_crtc_y) {
771 state->crtc_y = U642I64(val);
772 } else if (property == config->prop_crtc_w) {
773 state->crtc_w = val;
774 } else if (property == config->prop_crtc_h) {
775 state->crtc_h = val;
776 } else if (property == config->prop_src_x) {
777 state->src_x = val;
778 } else if (property == config->prop_src_y) {
779 state->src_y = val;
780 } else if (property == config->prop_src_w) {
781 state->src_w = val;
782 } else if (property == config->prop_src_h) {
783 state->src_h = val;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300784 } else if (property == plane->rotation_property) {
Robert Fossc2c446a2017-05-19 16:50:17 -0400785 if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK))
Ville Syrjälä6e0c7c32016-09-26 19:30:47 +0300786 return -EINVAL;
Matt Roper1da30622015-01-21 16:35:40 -0800787 state->rotation = val;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200788 } else if (property == plane->zpos_property) {
789 state->zpos = val;
Rob Clark6b4959f2014-12-18 16:01:53 -0500790 } else if (plane->funcs->atomic_set_property) {
791 return plane->funcs->atomic_set_property(plane, state,
792 property, val);
793 } else {
794 return -EINVAL;
795 }
796
797 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -0500798}
799EXPORT_SYMBOL(drm_atomic_plane_set_property);
800
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100801/**
802 * drm_atomic_plane_get_property - get property value from plane state
803 * @plane: the drm plane to set a property on
804 * @state: the state object to get the property value from
805 * @property: the property to set
806 * @val: return location for the property value
807 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100808 * This function handles generic/core properties and calls out to driver's
809 * &drm_plane_funcs.atomic_get_property for driver properties. To ensure
810 * consistent behavior you must call this function rather than the driver hook
811 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +0100812 *
813 * RETURNS:
814 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -0500815 */
Daniel Vettera97df1c2014-12-18 22:49:02 +0100816static int
817drm_atomic_plane_get_property(struct drm_plane *plane,
Rob Clarkac9c9252014-12-18 16:01:47 -0500818 const struct drm_plane_state *state,
819 struct drm_property *property, uint64_t *val)
820{
Rob Clark6b4959f2014-12-18 16:01:53 -0500821 struct drm_device *dev = plane->dev;
822 struct drm_mode_config *config = &dev->mode_config;
823
824 if (property == config->prop_fb_id) {
825 *val = (state->fb) ? state->fb->base.id : 0;
Gustavo Padovan96260142016-11-15 22:06:39 +0900826 } else if (property == config->prop_in_fence_fd) {
827 *val = -1;
Rob Clark6b4959f2014-12-18 16:01:53 -0500828 } else if (property == config->prop_crtc_id) {
829 *val = (state->crtc) ? state->crtc->base.id : 0;
830 } else if (property == config->prop_crtc_x) {
831 *val = I642U64(state->crtc_x);
832 } else if (property == config->prop_crtc_y) {
833 *val = I642U64(state->crtc_y);
834 } else if (property == config->prop_crtc_w) {
835 *val = state->crtc_w;
836 } else if (property == config->prop_crtc_h) {
837 *val = state->crtc_h;
838 } else if (property == config->prop_src_x) {
839 *val = state->src_x;
840 } else if (property == config->prop_src_y) {
841 *val = state->src_y;
842 } else if (property == config->prop_src_w) {
843 *val = state->src_w;
844 } else if (property == config->prop_src_h) {
845 *val = state->src_h;
Ville Syrjälä6686df82016-10-21 22:22:45 +0300846 } else if (property == plane->rotation_property) {
Tvrtko Ursulin4cda09c2015-02-26 13:49:17 +0000847 *val = state->rotation;
Marek Szyprowski44d1240d2016-06-13 11:11:26 +0200848 } else if (property == plane->zpos_property) {
849 *val = state->zpos;
Rob Clark6b4959f2014-12-18 16:01:53 -0500850 } else if (plane->funcs->atomic_get_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -0500851 return plane->funcs->atomic_get_property(plane, state, property, val);
Rob Clark6b4959f2014-12-18 16:01:53 -0500852 } else {
853 return -EINVAL;
854 }
855
856 return 0;
Rob Clarkac9c9252014-12-18 16:01:47 -0500857}
Rob Clarkac9c9252014-12-18 16:01:47 -0500858
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200859static bool
860plane_switching_crtc(struct drm_atomic_state *state,
861 struct drm_plane *plane,
862 struct drm_plane_state *plane_state)
863{
864 if (!plane->state->crtc || !plane_state->crtc)
865 return false;
866
867 if (plane->state->crtc == plane_state->crtc)
868 return false;
869
870 /* This could be refined, but currently there's no helper or driver code
871 * to implement direct switching of active planes nor userspace to take
872 * advantage of more direct plane switching without the intermediate
873 * full OFF state.
874 */
875 return true;
876}
877
Rob Clarkac9c9252014-12-18 16:01:47 -0500878/**
Rob Clark5e743732014-12-18 16:01:51 -0500879 * drm_atomic_plane_check - check plane state
880 * @plane: plane to check
881 * @state: plane state to check
882 *
883 * Provides core sanity checks for plane state.
884 *
885 * RETURNS:
886 * Zero on success, error code on failure
887 */
888static int drm_atomic_plane_check(struct drm_plane *plane,
889 struct drm_plane_state *state)
890{
891 unsigned int fb_width, fb_height;
Laurent Pinchartead86102015-03-05 02:25:43 +0200892 int ret;
Rob Clark5e743732014-12-18 16:01:51 -0500893
894 /* either *both* CRTC and FB must be set, or neither */
895 if (WARN_ON(state->crtc && !state->fb)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100896 DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
Rob Clark5e743732014-12-18 16:01:51 -0500897 return -EINVAL;
898 } else if (WARN_ON(state->fb && !state->crtc)) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100899 DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
Rob Clark5e743732014-12-18 16:01:51 -0500900 return -EINVAL;
901 }
902
903 /* if disabled, we don't care about the rest of the state: */
904 if (!state->crtc)
905 return 0;
906
907 /* Check whether this plane is usable on this CRTC */
908 if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100909 DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
Rob Clark5e743732014-12-18 16:01:51 -0500910 return -EINVAL;
911 }
912
913 /* Check whether this plane supports the fb pixel format. */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200914 ret = drm_plane_check_pixel_format(plane, state->fb->format->format);
Laurent Pinchartead86102015-03-05 02:25:43 +0200915 if (ret) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000916 struct drm_format_name_buf format_name;
917 DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200918 drm_get_format_name(state->fb->format->format,
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000919 &format_name));
Laurent Pinchartead86102015-03-05 02:25:43 +0200920 return ret;
Rob Clark5e743732014-12-18 16:01:51 -0500921 }
922
923 /* Give drivers some help against integer overflows */
924 if (state->crtc_w > INT_MAX ||
925 state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
926 state->crtc_h > INT_MAX ||
927 state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100928 DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
929 state->crtc_w, state->crtc_h,
930 state->crtc_x, state->crtc_y);
Rob Clark5e743732014-12-18 16:01:51 -0500931 return -ERANGE;
932 }
933
934 fb_width = state->fb->width << 16;
935 fb_height = state->fb->height << 16;
936
937 /* Make sure source coordinates are inside the fb. */
938 if (state->src_w > fb_width ||
939 state->src_x > fb_width - state->src_w ||
940 state->src_h > fb_height ||
941 state->src_y > fb_height - state->src_h) {
Daniel Vetter17a38d92015-02-22 12:24:16 +0100942 DRM_DEBUG_ATOMIC("Invalid source coordinates "
943 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
944 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
945 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
946 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
947 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
Rob Clark5e743732014-12-18 16:01:51 -0500948 return -ENOSPC;
949 }
950
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200951 if (plane_switching_crtc(state->state, plane, state)) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +0200952 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] switching CRTC directly\n",
953 plane->base.id, plane->name);
Daniel Vetterf8aeb412015-08-26 21:49:42 +0200954 return -EINVAL;
955 }
956
Rob Clark5e743732014-12-18 16:01:51 -0500957 return 0;
958}
959
Rob Clarkfceffb322016-11-05 11:08:09 -0400960static void drm_atomic_plane_print_state(struct drm_printer *p,
961 const struct drm_plane_state *state)
962{
963 struct drm_plane *plane = state->plane;
964 struct drm_rect src = drm_plane_state_src(state);
965 struct drm_rect dest = drm_plane_state_dest(state);
966
967 drm_printf(p, "plane[%u]: %s\n", plane->base.id, plane->name);
968 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
969 drm_printf(p, "\tfb=%u\n", state->fb ? state->fb->base.id : 0);
970 if (state->fb) {
971 struct drm_framebuffer *fb = state->fb;
Ville Syrjäläbcb0b462016-12-14 23:30:22 +0200972 int i, n = fb->format->num_planes;
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000973 struct drm_format_name_buf format_name;
Rob Clarkfceffb322016-11-05 11:08:09 -0400974
975 drm_printf(p, "\t\tformat=%s\n",
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200976 drm_get_format_name(fb->format->format, &format_name));
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200977 drm_printf(p, "\t\t\tmodifier=0x%llx\n", fb->modifier);
Rob Clarkfceffb322016-11-05 11:08:09 -0400978 drm_printf(p, "\t\tsize=%dx%d\n", fb->width, fb->height);
979 drm_printf(p, "\t\tlayers:\n");
980 for (i = 0; i < n; i++) {
981 drm_printf(p, "\t\t\tpitch[%d]=%u\n", i, fb->pitches[i]);
982 drm_printf(p, "\t\t\toffset[%d]=%u\n", i, fb->offsets[i]);
Rob Clarkfceffb322016-11-05 11:08:09 -0400983 }
984 }
985 drm_printf(p, "\tcrtc-pos=" DRM_RECT_FMT "\n", DRM_RECT_ARG(&dest));
986 drm_printf(p, "\tsrc-pos=" DRM_RECT_FP_FMT "\n", DRM_RECT_FP_ARG(&src));
987 drm_printf(p, "\trotation=%x\n", state->rotation);
988
989 if (plane->funcs->atomic_print_state)
990 plane->funcs->atomic_print_state(p, state);
991}
992
Rob Clark5e743732014-12-18 16:01:51 -0500993/**
Pandiyan, Dhinakaranb430c272017-04-20 22:51:30 -0700994 * drm_atomic_get_private_obj_state - get private object state
995 * @state: global atomic state
996 * @obj: private object to get the state for
997 * @funcs: pointer to the struct of function pointers that identify the object
998 * type
999 *
1000 * This function returns the private object state for the given private object,
1001 * allocating the state if needed. It does not grab any locks as the caller is
1002 * expected to care of any required locking.
1003 *
1004 * RETURNS:
1005 *
1006 * Either the allocated state or the error code encoded into a pointer.
1007 */
1008void *
1009drm_atomic_get_private_obj_state(struct drm_atomic_state *state, void *obj,
1010 const struct drm_private_state_funcs *funcs)
1011{
1012 int index, num_objs, i;
1013 size_t size;
1014 struct __drm_private_objs_state *arr;
1015
1016 for (i = 0; i < state->num_private_objs; i++)
1017 if (obj == state->private_objs[i].obj &&
1018 state->private_objs[i].obj_state)
1019 return state->private_objs[i].obj_state;
1020
1021 num_objs = state->num_private_objs + 1;
1022 size = sizeof(*state->private_objs) * num_objs;
1023 arr = krealloc(state->private_objs, size, GFP_KERNEL);
1024 if (!arr)
1025 return ERR_PTR(-ENOMEM);
1026
1027 state->private_objs = arr;
1028 index = state->num_private_objs;
1029 memset(&state->private_objs[index], 0, sizeof(*state->private_objs));
1030
1031 state->private_objs[index].obj_state = funcs->duplicate_state(state, obj);
1032 if (!state->private_objs[index].obj_state)
1033 return ERR_PTR(-ENOMEM);
1034
1035 state->private_objs[index].obj = obj;
1036 state->private_objs[index].funcs = funcs;
1037 state->num_private_objs = num_objs;
1038
1039 DRM_DEBUG_ATOMIC("Added new private object state %p to %p\n",
1040 state->private_objs[index].obj_state, state);
1041
1042 return state->private_objs[index].obj_state;
1043}
1044EXPORT_SYMBOL(drm_atomic_get_private_obj_state);
1045
1046/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001047 * drm_atomic_get_connector_state - get connector state
1048 * @state: global atomic state object
1049 * @connector: connector to get state object for
1050 *
1051 * This function returns the connector state for the given connector,
1052 * allocating it if needed. It will also grab the relevant connector lock to
1053 * make sure that the state is consistent.
1054 *
1055 * Returns:
1056 *
1057 * Either the allocated state or the error code encoded into the pointer. When
1058 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
1059 * entire atomic sequence must be restarted. All other errors are fatal.
1060 */
1061struct drm_connector_state *
1062drm_atomic_get_connector_state(struct drm_atomic_state *state,
1063 struct drm_connector *connector)
1064{
1065 int ret, index;
1066 struct drm_mode_config *config = &connector->dev->mode_config;
1067 struct drm_connector_state *connector_state;
1068
Maarten Lankhorst7f4eaa892016-05-03 11:12:31 +02001069 WARN_ON(!state->acquire_ctx);
1070
Daniel Vetterc7eb76f2014-11-19 18:38:06 +01001071 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1072 if (ret)
1073 return ERR_PTR(ret);
1074
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001075 index = drm_connector_index(connector);
1076
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001077 if (index >= state->num_connector) {
Daniel Vetter63e83c12016-06-02 00:06:32 +02001078 struct __drm_connnectors_state *c;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001079 int alloc = max(index + 1, config->num_connector);
1080
1081 c = krealloc(state->connectors, alloc * sizeof(*state->connectors), GFP_KERNEL);
1082 if (!c)
1083 return ERR_PTR(-ENOMEM);
1084
1085 state->connectors = c;
1086 memset(&state->connectors[state->num_connector], 0,
1087 sizeof(*state->connectors) * (alloc - state->num_connector));
1088
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01001089 state->num_connector = alloc;
Daniel Vetterf52b69f12014-11-19 18:38:08 +01001090 }
1091
Daniel Vetter63e83c12016-06-02 00:06:32 +02001092 if (state->connectors[index].state)
1093 return state->connectors[index].state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001094
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001095 connector_state = connector->funcs->atomic_duplicate_state(connector);
1096 if (!connector_state)
1097 return ERR_PTR(-ENOMEM);
1098
Thierry Redingad093602017-02-28 15:46:39 +01001099 drm_connector_get(connector);
Daniel Vetter63e83c12016-06-02 00:06:32 +02001100 state->connectors[index].state = connector_state;
Maarten Lankhorst581e49f2017-01-16 10:37:38 +01001101 state->connectors[index].old_state = connector->state;
1102 state->connectors[index].new_state = connector_state;
Daniel Vetter63e83c12016-06-02 00:06:32 +02001103 state->connectors[index].ptr = connector;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001104 connector_state->state = state;
1105
Russell King6ac7c542017-02-13 12:27:03 +00001106 DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d:%s] %p state to %p\n",
1107 connector->base.id, connector->name,
1108 connector_state, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001109
1110 if (connector_state->crtc) {
1111 struct drm_crtc_state *crtc_state;
1112
1113 crtc_state = drm_atomic_get_crtc_state(state,
1114 connector_state->crtc);
1115 if (IS_ERR(crtc_state))
1116 return ERR_CAST(crtc_state);
1117 }
1118
1119 return connector_state;
1120}
1121EXPORT_SYMBOL(drm_atomic_get_connector_state);
1122
1123/**
Rob Clark40ecc692014-12-18 16:01:46 -05001124 * drm_atomic_connector_set_property - set property on connector.
1125 * @connector: the drm connector to set a property on
1126 * @state: the state object to update with the new property value
1127 * @property: the property to set
1128 * @val: the new property value
1129 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001130 * This function handles generic/core properties and calls out to driver's
1131 * &drm_connector_funcs.atomic_set_property for driver properties. To ensure
1132 * consistent behavior you must call this function rather than the driver hook
1133 * directly.
Rob Clark40ecc692014-12-18 16:01:46 -05001134 *
1135 * RETURNS:
1136 * Zero on success, error code on failure
1137 */
1138int drm_atomic_connector_set_property(struct drm_connector *connector,
1139 struct drm_connector_state *state, struct drm_property *property,
1140 uint64_t val)
1141{
1142 struct drm_device *dev = connector->dev;
1143 struct drm_mode_config *config = &dev->mode_config;
1144
Rob Clarkae16c592014-12-18 16:01:54 -05001145 if (property == config->prop_crtc_id) {
1146 struct drm_crtc *crtc = drm_crtc_find(dev, val);
1147 return drm_atomic_set_crtc_for_connector(state, crtc);
1148 } else if (property == config->dpms_property) {
Rob Clark40ecc692014-12-18 16:01:46 -05001149 /* setting DPMS property requires special handling, which
1150 * is done in legacy setprop path for us. Disallow (for
1151 * now?) atomic writes to DPMS property:
1152 */
1153 return -EINVAL;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001154 } else if (property == config->tv_select_subconnector_property) {
1155 state->tv.subconnector = val;
1156 } else if (property == config->tv_left_margin_property) {
1157 state->tv.margins.left = val;
1158 } else if (property == config->tv_right_margin_property) {
1159 state->tv.margins.right = val;
1160 } else if (property == config->tv_top_margin_property) {
1161 state->tv.margins.top = val;
1162 } else if (property == config->tv_bottom_margin_property) {
1163 state->tv.margins.bottom = val;
1164 } else if (property == config->tv_mode_property) {
1165 state->tv.mode = val;
1166 } else if (property == config->tv_brightness_property) {
1167 state->tv.brightness = val;
1168 } else if (property == config->tv_contrast_property) {
1169 state->tv.contrast = val;
1170 } else if (property == config->tv_flicker_reduction_property) {
1171 state->tv.flicker_reduction = val;
1172 } else if (property == config->tv_overscan_property) {
1173 state->tv.overscan = val;
1174 } else if (property == config->tv_saturation_property) {
1175 state->tv.saturation = val;
1176 } else if (property == config->tv_hue_property) {
1177 state->tv.hue = val;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001178 } else if (property == config->link_status_property) {
1179 /* Never downgrade from GOOD to BAD on userspace's request here,
1180 * only hw issues can do that.
1181 *
1182 * For an atomic property the userspace doesn't need to be able
1183 * to understand all the properties, but needs to be able to
1184 * restore the state it wants on VT switch. So if the userspace
1185 * tries to change the link_status from GOOD to BAD, driver
1186 * silently rejects it and returns a 0. This prevents userspace
1187 * from accidently breaking the display when it restores the
1188 * state.
1189 */
1190 if (state->link_status != DRM_LINK_STATUS_GOOD)
1191 state->link_status = val;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001192 } else if (property == config->aspect_ratio_property) {
1193 state->picture_aspect_ratio = val;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001194 } else if (property == connector->scaling_mode_property) {
1195 state->scaling_mode = val;
Rob Clark40ecc692014-12-18 16:01:46 -05001196 } else if (connector->funcs->atomic_set_property) {
1197 return connector->funcs->atomic_set_property(connector,
1198 state, property, val);
1199 } else {
1200 return -EINVAL;
1201 }
Boris Brezillon299a16b2016-12-02 14:48:09 +01001202
1203 return 0;
Rob Clark40ecc692014-12-18 16:01:46 -05001204}
1205EXPORT_SYMBOL(drm_atomic_connector_set_property);
1206
Rob Clarkfceffb322016-11-05 11:08:09 -04001207static void drm_atomic_connector_print_state(struct drm_printer *p,
1208 const struct drm_connector_state *state)
1209{
1210 struct drm_connector *connector = state->connector;
1211
1212 drm_printf(p, "connector[%u]: %s\n", connector->base.id, connector->name);
1213 drm_printf(p, "\tcrtc=%s\n", state->crtc ? state->crtc->name : "(null)");
1214
1215 if (connector->funcs->atomic_print_state)
1216 connector->funcs->atomic_print_state(p, state);
1217}
1218
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001219/**
1220 * drm_atomic_connector_get_property - get property value from connector state
1221 * @connector: the drm connector to set a property on
1222 * @state: the state object to get the property value from
1223 * @property: the property to set
1224 * @val: return location for the property value
1225 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001226 * This function handles generic/core properties and calls out to driver's
1227 * &drm_connector_funcs.atomic_get_property for driver properties. To ensure
1228 * consistent behavior you must call this function rather than the driver hook
1229 * directly.
Daniel Vetterc0714fc2015-12-04 09:45:57 +01001230 *
1231 * RETURNS:
1232 * Zero on success, error code on failure
Rob Clarkac9c9252014-12-18 16:01:47 -05001233 */
Daniel Vettera97df1c2014-12-18 22:49:02 +01001234static int
1235drm_atomic_connector_get_property(struct drm_connector *connector,
Rob Clarkac9c9252014-12-18 16:01:47 -05001236 const struct drm_connector_state *state,
1237 struct drm_property *property, uint64_t *val)
1238{
1239 struct drm_device *dev = connector->dev;
1240 struct drm_mode_config *config = &dev->mode_config;
1241
Rob Clarkae16c592014-12-18 16:01:54 -05001242 if (property == config->prop_crtc_id) {
1243 *val = (state->crtc) ? state->crtc->base.id : 0;
1244 } else if (property == config->dpms_property) {
Rob Clarkac9c9252014-12-18 16:01:47 -05001245 *val = connector->dpms;
Boris Brezillon299a16b2016-12-02 14:48:09 +01001246 } else if (property == config->tv_select_subconnector_property) {
1247 *val = state->tv.subconnector;
1248 } else if (property == config->tv_left_margin_property) {
1249 *val = state->tv.margins.left;
1250 } else if (property == config->tv_right_margin_property) {
1251 *val = state->tv.margins.right;
1252 } else if (property == config->tv_top_margin_property) {
1253 *val = state->tv.margins.top;
1254 } else if (property == config->tv_bottom_margin_property) {
1255 *val = state->tv.margins.bottom;
1256 } else if (property == config->tv_mode_property) {
1257 *val = state->tv.mode;
1258 } else if (property == config->tv_brightness_property) {
1259 *val = state->tv.brightness;
1260 } else if (property == config->tv_contrast_property) {
1261 *val = state->tv.contrast;
1262 } else if (property == config->tv_flicker_reduction_property) {
1263 *val = state->tv.flicker_reduction;
1264 } else if (property == config->tv_overscan_property) {
1265 *val = state->tv.overscan;
1266 } else if (property == config->tv_saturation_property) {
1267 *val = state->tv.saturation;
1268 } else if (property == config->tv_hue_property) {
1269 *val = state->tv.hue;
Manasi Navare40ee6fb2016-12-16 12:29:06 +02001270 } else if (property == config->link_status_property) {
1271 *val = state->link_status;
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001272 } else if (property == config->aspect_ratio_property) {
1273 *val = state->picture_aspect_ratio;
Maarten Lankhorst8f6e1e22017-05-01 15:37:54 +02001274 } else if (property == connector->scaling_mode_property) {
1275 *val = state->scaling_mode;
Rob Clarkac9c9252014-12-18 16:01:47 -05001276 } else if (connector->funcs->atomic_get_property) {
1277 return connector->funcs->atomic_get_property(connector,
1278 state, property, val);
1279 } else {
1280 return -EINVAL;
1281 }
1282
1283 return 0;
1284}
Rob Clarkac9c9252014-12-18 16:01:47 -05001285
Rob Clark88a48e22014-12-18 16:01:50 -05001286int drm_atomic_get_property(struct drm_mode_object *obj,
1287 struct drm_property *property, uint64_t *val)
1288{
1289 struct drm_device *dev = property->dev;
1290 int ret;
1291
1292 switch (obj->type) {
1293 case DRM_MODE_OBJECT_CONNECTOR: {
1294 struct drm_connector *connector = obj_to_connector(obj);
1295 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
1296 ret = drm_atomic_connector_get_property(connector,
1297 connector->state, property, val);
1298 break;
1299 }
1300 case DRM_MODE_OBJECT_CRTC: {
1301 struct drm_crtc *crtc = obj_to_crtc(obj);
1302 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
1303 ret = drm_atomic_crtc_get_property(crtc,
1304 crtc->state, property, val);
1305 break;
1306 }
1307 case DRM_MODE_OBJECT_PLANE: {
1308 struct drm_plane *plane = obj_to_plane(obj);
1309 WARN_ON(!drm_modeset_is_locked(&plane->mutex));
1310 ret = drm_atomic_plane_get_property(plane,
1311 plane->state, property, val);
1312 break;
1313 }
1314 default:
1315 ret = -EINVAL;
1316 break;
1317 }
1318
1319 return ret;
1320}
1321
1322/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001323 * drm_atomic_set_crtc_for_plane - set crtc for plane
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001324 * @plane_state: the plane whose incoming state to update
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001325 * @crtc: crtc to use for the plane
1326 *
1327 * Changing the assigned crtc for a plane requires us to grab the lock and state
1328 * for the new crtc, as needed. This function takes care of all these details
1329 * besides updating the pointer in the state object itself.
1330 *
1331 * Returns:
1332 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1333 * then the w/w mutex code has detected a deadlock and the entire atomic
1334 * sequence must be restarted. All other errors are fatal.
1335 */
1336int
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001337drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
1338 struct drm_crtc *crtc)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001339{
Daniel Vetter07cc0ef2014-11-27 15:49:39 +01001340 struct drm_plane *plane = plane_state->plane;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001341 struct drm_crtc_state *crtc_state;
1342
Rob Clark6ddd3882014-11-21 15:28:31 -05001343 if (plane_state->crtc) {
1344 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1345 plane_state->crtc);
1346 if (WARN_ON(IS_ERR(crtc_state)))
1347 return PTR_ERR(crtc_state);
1348
1349 crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
1350 }
1351
1352 plane_state->crtc = crtc;
1353
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001354 if (crtc) {
1355 crtc_state = drm_atomic_get_crtc_state(plane_state->state,
1356 crtc);
1357 if (IS_ERR(crtc_state))
1358 return PTR_ERR(crtc_state);
Rob Clark6ddd3882014-11-21 15:28:31 -05001359 crtc_state->plane_mask |= (1 << drm_plane_index(plane));
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001360 }
1361
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001362 if (crtc)
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001363 DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d:%s]\n",
1364 plane_state, crtc->base.id, crtc->name);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001365 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001366 DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
1367 plane_state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001368
1369 return 0;
1370}
1371EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
1372
1373/**
John Hunter16d78bc2e2015-04-07 19:38:50 +08001374 * drm_atomic_set_fb_for_plane - set framebuffer for plane
Daniel Vetter321ebf02014-11-04 22:57:27 +01001375 * @plane_state: atomic state object for the plane
1376 * @fb: fb to use for the plane
1377 *
1378 * Changing the assigned framebuffer for a plane requires us to grab a reference
1379 * to the new fb and drop the reference to the old fb, if there is one. This
1380 * function takes care of all these details besides updating the pointer in the
1381 * state object itself.
1382 */
1383void
1384drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1385 struct drm_framebuffer *fb)
1386{
Daniel Vetter321ebf02014-11-04 22:57:27 +01001387 if (fb)
Daniel Vetter17a38d92015-02-22 12:24:16 +01001388 DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1389 fb->base.id, plane_state);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001390 else
Daniel Vetter17a38d92015-02-22 12:24:16 +01001391 DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1392 plane_state);
Chris Wilson389f78b2016-11-25 15:32:30 +00001393
1394 drm_framebuffer_assign(&plane_state->fb, fb);
Daniel Vetter321ebf02014-11-04 22:57:27 +01001395}
1396EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1397
1398/**
Gustavo Padovan13b55662016-11-07 19:03:30 +09001399 * drm_atomic_set_fence_for_plane - set fence for plane
1400 * @plane_state: atomic state object for the plane
1401 * @fence: dma_fence to use for the plane
1402 *
1403 * Helper to setup the plane_state fence in case it is not set yet.
1404 * By using this drivers doesn't need to worry if the user choose
1405 * implicit or explicit fencing.
1406 *
1407 * This function will not set the fence to the state if it was set
Daniel Vetterd5745282017-01-25 07:26:45 +01001408 * via explicit fencing interfaces on the atomic ioctl. In that case it will
1409 * drop the reference to the fence as we are not storing it anywhere.
1410 * Otherwise, if &drm_plane_state.fence is not set this function we just set it
1411 * with the received implicit fence. In both cases this function consumes a
1412 * reference for @fence.
Gustavo Padovan13b55662016-11-07 19:03:30 +09001413 */
1414void
1415drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
1416 struct dma_fence *fence)
1417{
1418 if (plane_state->fence) {
1419 dma_fence_put(fence);
1420 return;
1421 }
1422
1423 plane_state->fence = fence;
1424}
1425EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
1426
1427/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001428 * drm_atomic_set_crtc_for_connector - set crtc for connector
1429 * @conn_state: atomic state object for the connector
1430 * @crtc: crtc to use for the connector
1431 *
1432 * Changing the assigned crtc for a connector requires us to grab the lock and
1433 * state for the new crtc, as needed. This function takes care of all these
1434 * details besides updating the pointer in the state object itself.
1435 *
1436 * Returns:
1437 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1438 * then the w/w mutex code has detected a deadlock and the entire atomic
1439 * sequence must be restarted. All other errors are fatal.
1440 */
1441int
1442drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1443 struct drm_crtc *crtc)
1444{
1445 struct drm_crtc_state *crtc_state;
1446
Chris Wilsone2d800a2016-05-06 12:47:45 +01001447 if (conn_state->crtc == crtc)
1448 return 0;
1449
1450 if (conn_state->crtc) {
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001451 crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
1452 conn_state->crtc);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001453
1454 crtc_state->connector_mask &=
1455 ~(1 << drm_connector_index(conn_state->connector));
Chris Wilsone2d800a2016-05-06 12:47:45 +01001456
Thierry Redingad093602017-02-28 15:46:39 +01001457 drm_connector_put(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001458 conn_state->crtc = NULL;
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001459 }
1460
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001461 if (crtc) {
1462 crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1463 if (IS_ERR(crtc_state))
1464 return PTR_ERR(crtc_state);
Maarten Lankhorst4cd9fa52016-01-04 12:53:18 +01001465
1466 crtc_state->connector_mask |=
1467 1 << drm_connector_index(conn_state->connector);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001468
Thierry Redingad093602017-02-28 15:46:39 +01001469 drm_connector_get(conn_state->connector);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001470 conn_state->crtc = crtc;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001471
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001472 DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d:%s]\n",
1473 conn_state, crtc->base.id, crtc->name);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001474 } else {
Daniel Vetter17a38d92015-02-22 12:24:16 +01001475 DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1476 conn_state);
Chris Wilsone2d800a2016-05-06 12:47:45 +01001477 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001478
1479 return 0;
1480}
1481EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1482
1483/**
1484 * drm_atomic_add_affected_connectors - add connectors for crtc
1485 * @state: atomic state
1486 * @crtc: DRM crtc
1487 *
1488 * This function walks the current configuration and adds all connectors
1489 * currently using @crtc to the atomic configuration @state. Note that this
1490 * function must acquire the connection mutex. This can potentially cause
1491 * unneeded seralization if the update is just for the planes on one crtc. Hence
1492 * drivers and helpers should only call this when really needed (e.g. when a
1493 * full modeset needs to happen due to some change).
1494 *
1495 * Returns:
1496 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1497 * then the w/w mutex code has detected a deadlock and the entire atomic
1498 * sequence must be restarted. All other errors are fatal.
1499 */
1500int
1501drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1502 struct drm_crtc *crtc)
1503{
1504 struct drm_mode_config *config = &state->dev->mode_config;
1505 struct drm_connector *connector;
1506 struct drm_connector_state *conn_state;
Daniel Vetter613051d2016-12-14 00:08:06 +01001507 struct drm_connector_list_iter conn_iter;
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001508 struct drm_crtc_state *crtc_state;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001509 int ret;
1510
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001511 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1512 if (IS_ERR(crtc_state))
1513 return PTR_ERR(crtc_state);
1514
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001515 ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1516 if (ret)
1517 return ret;
1518
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001519 DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d:%s] to %p\n",
1520 crtc->base.id, crtc->name, state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001521
1522 /*
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001523 * Changed connectors are already in @state, so only need to look
1524 * at the connector_mask in crtc_state.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001525 */
Thierry Redingb982dab2017-02-28 15:46:43 +01001526 drm_connector_list_iter_begin(state->dev, &conn_iter);
Daniel Vetter613051d2016-12-14 00:08:06 +01001527 drm_for_each_connector_iter(connector, &conn_iter) {
Maarten Lankhorst5351bbd2017-01-16 10:37:39 +01001528 if (!(crtc_state->connector_mask & (1 << drm_connector_index(connector))))
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001529 continue;
1530
1531 conn_state = drm_atomic_get_connector_state(state, connector);
Daniel Vetter613051d2016-12-14 00:08:06 +01001532 if (IS_ERR(conn_state)) {
Thierry Redingb982dab2017-02-28 15:46:43 +01001533 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001534 return PTR_ERR(conn_state);
Daniel Vetter613051d2016-12-14 00:08:06 +01001535 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001536 }
Thierry Redingb982dab2017-02-28 15:46:43 +01001537 drm_connector_list_iter_end(&conn_iter);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001538
1539 return 0;
1540}
1541EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1542
1543/**
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001544 * drm_atomic_add_affected_planes - add planes for crtc
1545 * @state: atomic state
1546 * @crtc: DRM crtc
1547 *
1548 * This function walks the current configuration and adds all planes
1549 * currently used by @crtc to the atomic configuration @state. This is useful
1550 * when an atomic commit also needs to check all currently enabled plane on
1551 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1552 * to avoid special code to force-enable all planes.
1553 *
1554 * Since acquiring a plane state will always also acquire the w/w mutex of the
1555 * current CRTC for that plane (if there is any) adding all the plane states for
1556 * a CRTC will not reduce parallism of atomic updates.
1557 *
1558 * Returns:
1559 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1560 * then the w/w mutex code has detected a deadlock and the entire atomic
1561 * sequence must be restarted. All other errors are fatal.
1562 */
1563int
1564drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1565 struct drm_crtc *crtc)
1566{
1567 struct drm_plane *plane;
1568
Maarten Lankhorstb4d93672017-03-01 10:22:10 +01001569 WARN_ON(!drm_atomic_get_new_crtc_state(state, crtc));
Maarten Lankhorste01e9f72015-05-19 16:41:02 +02001570
1571 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1572 struct drm_plane_state *plane_state =
1573 drm_atomic_get_plane_state(state, plane);
1574
1575 if (IS_ERR(plane_state))
1576 return PTR_ERR(plane_state);
1577 }
1578 return 0;
1579}
1580EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1581
1582/**
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001583 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1584 * @state: atomic state
1585 *
1586 * This function should be used by legacy entry points which don't understand
1587 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
John Hunter16d78bc2e2015-04-07 19:38:50 +08001588 * the slowpath completed.
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001589 */
1590void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1591{
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001592 struct drm_device *dev = state->dev;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001593 int ret;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001594 bool global = false;
1595
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001596 if (WARN_ON(dev->mode_config.acquire_ctx == state->acquire_ctx)) {
1597 global = true;
1598
1599 dev->mode_config.acquire_ctx = NULL;
1600 }
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001601
1602retry:
1603 drm_modeset_backoff(state->acquire_ctx);
1604
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001605 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001606 if (ret)
1607 goto retry;
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001608
Maarten Lankhorst81e257e2016-06-23 13:45:06 +02001609 if (global)
1610 dev->mode_config.acquire_ctx = state->acquire_ctx;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001611}
1612EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1613
1614/**
1615 * drm_atomic_check_only - check whether a given config would work
1616 * @state: atomic configuration to check
1617 *
1618 * Note that this function can return -EDEADLK if the driver needed to acquire
1619 * more locks but encountered a deadlock. The caller must then do the usual w/w
1620 * backoff dance and restart. All other errors are fatal.
1621 *
1622 * Returns:
1623 * 0 on success, negative error code on failure.
1624 */
1625int drm_atomic_check_only(struct drm_atomic_state *state)
1626{
Rob Clark5e743732014-12-18 16:01:51 -05001627 struct drm_device *dev = state->dev;
1628 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001629 struct drm_plane *plane;
1630 struct drm_plane_state *plane_state;
1631 struct drm_crtc *crtc;
1632 struct drm_crtc_state *crtc_state;
Rob Clark5e743732014-12-18 16:01:51 -05001633 int i, ret = 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001634
Daniel Vetter17a38d92015-02-22 12:24:16 +01001635 DRM_DEBUG_ATOMIC("checking %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001636
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001637 for_each_new_plane_in_state(state, plane, plane_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001638 ret = drm_atomic_plane_check(plane, plane_state);
Rob Clark5e743732014-12-18 16:01:51 -05001639 if (ret) {
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001640 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] atomic core check failed\n",
1641 plane->base.id, plane->name);
Rob Clark5e743732014-12-18 16:01:51 -05001642 return ret;
1643 }
1644 }
1645
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001646 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiradf63b992015-04-10 14:58:39 +03001647 ret = drm_atomic_crtc_check(crtc, crtc_state);
Rob Clark5e743732014-12-18 16:01:51 -05001648 if (ret) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001649 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] atomic core check failed\n",
1650 crtc->base.id, crtc->name);
Rob Clark5e743732014-12-18 16:01:51 -05001651 return ret;
1652 }
1653 }
1654
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001655 if (config->funcs->atomic_check)
Rob Clark5e743732014-12-18 16:01:51 -05001656 ret = config->funcs->atomic_check(state->dev, state);
1657
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001658 if (ret)
1659 return ret;
1660
Rob Clarkd34f20d2014-12-18 16:01:56 -05001661 if (!state->allow_modeset) {
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001662 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Daniel Vetter2465ff62015-06-18 09:58:55 +02001663 if (drm_atomic_crtc_needs_modeset(crtc_state)) {
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02001664 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] requires full modeset\n",
1665 crtc->base.id, crtc->name);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001666 return -EINVAL;
1667 }
1668 }
1669 }
1670
Maarten Lankhorsta0ffc512017-08-15 11:57:06 +02001671 return 0;
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001672}
1673EXPORT_SYMBOL(drm_atomic_check_only);
1674
1675/**
1676 * drm_atomic_commit - commit configuration atomically
1677 * @state: atomic configuration to check
1678 *
1679 * Note that this function can return -EDEADLK if the driver needed to acquire
1680 * more locks but encountered a deadlock. The caller must then do the usual w/w
1681 * backoff dance and restart. All other errors are fatal.
1682 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001683 * This function will take its own reference on @state.
1684 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001685 *
1686 * Returns:
1687 * 0 on success, negative error code on failure.
1688 */
1689int drm_atomic_commit(struct drm_atomic_state *state)
1690{
1691 struct drm_mode_config *config = &state->dev->mode_config;
1692 int ret;
1693
1694 ret = drm_atomic_check_only(state);
1695 if (ret)
1696 return ret;
1697
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001698 DRM_DEBUG_ATOMIC("committing %p\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001699
1700 return config->funcs->atomic_commit(state->dev, state, false);
1701}
1702EXPORT_SYMBOL(drm_atomic_commit);
1703
1704/**
Daniel Vetterd5745282017-01-25 07:26:45 +01001705 * drm_atomic_nonblocking_commit - atomic nonblocking commit
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001706 * @state: atomic configuration to check
1707 *
1708 * Note that this function can return -EDEADLK if the driver needed to acquire
1709 * more locks but encountered a deadlock. The caller must then do the usual w/w
1710 * backoff dance and restart. All other errors are fatal.
1711 *
Maarten Lankhorst76fede22017-01-04 12:34:00 +01001712 * This function will take its own reference on @state.
1713 * Callers should always release their reference with drm_atomic_state_put().
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001714 *
1715 * Returns:
1716 * 0 on success, negative error code on failure.
1717 */
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001718int drm_atomic_nonblocking_commit(struct drm_atomic_state *state)
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001719{
1720 struct drm_mode_config *config = &state->dev->mode_config;
1721 int ret;
1722
1723 ret = drm_atomic_check_only(state);
1724 if (ret)
1725 return ret;
1726
Colin Ian Kinga0752d42017-04-12 17:27:22 +01001727 DRM_DEBUG_ATOMIC("committing %p nonblocking\n", state);
Daniel Vettercc4ceb42014-07-25 21:30:38 +02001728
1729 return config->funcs->atomic_commit(state->dev, state, true);
1730}
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02001731EXPORT_SYMBOL(drm_atomic_nonblocking_commit);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001732
Rob Clarkfceffb322016-11-05 11:08:09 -04001733static void drm_atomic_print_state(const struct drm_atomic_state *state)
1734{
1735 struct drm_printer p = drm_info_printer(state->dev->dev);
1736 struct drm_plane *plane;
1737 struct drm_plane_state *plane_state;
1738 struct drm_crtc *crtc;
1739 struct drm_crtc_state *crtc_state;
1740 struct drm_connector *connector;
1741 struct drm_connector_state *connector_state;
1742 int i;
1743
1744 DRM_DEBUG_ATOMIC("checking %p\n", state);
1745
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001746 for_each_new_plane_in_state(state, plane, plane_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001747 drm_atomic_plane_print_state(&p, plane_state);
1748
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001749 for_each_new_crtc_in_state(state, crtc, crtc_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001750 drm_atomic_crtc_print_state(&p, crtc_state);
1751
Maarten Lankhorst5721a382017-01-16 10:37:40 +01001752 for_each_new_connector_in_state(state, connector, connector_state, i)
Rob Clarkfceffb322016-11-05 11:08:09 -04001753 drm_atomic_connector_print_state(&p, connector_state);
1754}
1755
Daniel Vetterc2d85562017-04-03 10:32:54 +02001756static void __drm_state_dump(struct drm_device *dev, struct drm_printer *p,
1757 bool take_locks)
1758{
1759 struct drm_mode_config *config = &dev->mode_config;
1760 struct drm_plane *plane;
1761 struct drm_crtc *crtc;
1762 struct drm_connector *connector;
1763 struct drm_connector_list_iter conn_iter;
1764
1765 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1766 return;
1767
1768 list_for_each_entry(plane, &config->plane_list, head) {
1769 if (take_locks)
1770 drm_modeset_lock(&plane->mutex, NULL);
1771 drm_atomic_plane_print_state(p, plane->state);
1772 if (take_locks)
1773 drm_modeset_unlock(&plane->mutex);
1774 }
1775
1776 list_for_each_entry(crtc, &config->crtc_list, head) {
1777 if (take_locks)
1778 drm_modeset_lock(&crtc->mutex, NULL);
1779 drm_atomic_crtc_print_state(p, crtc->state);
1780 if (take_locks)
1781 drm_modeset_unlock(&crtc->mutex);
1782 }
1783
1784 drm_connector_list_iter_begin(dev, &conn_iter);
1785 if (take_locks)
1786 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
1787 drm_for_each_connector_iter(connector, &conn_iter)
1788 drm_atomic_connector_print_state(p, connector->state);
1789 if (take_locks)
1790 drm_modeset_unlock(&dev->mode_config.connection_mutex);
1791 drm_connector_list_iter_end(&conn_iter);
1792}
1793
Rob Clark6559c902016-11-05 11:08:10 -04001794/**
1795 * drm_state_dump - dump entire device atomic state
1796 * @dev: the drm device
1797 * @p: where to print the state to
1798 *
1799 * Just for debugging. Drivers might want an option to dump state
1800 * to dmesg in case of error irq's. (Hint, you probably want to
1801 * ratelimit this!)
1802 *
1803 * The caller must drm_modeset_lock_all(), or if this is called
1804 * from error irq handler, it should not be enabled by default.
1805 * (Ie. if you are debugging errors you might not care that this
1806 * is racey. But calling this without all modeset locks held is
1807 * not inherently safe.)
1808 */
1809void drm_state_dump(struct drm_device *dev, struct drm_printer *p)
1810{
Daniel Vetterc2d85562017-04-03 10:32:54 +02001811 __drm_state_dump(dev, p, false);
Rob Clark6559c902016-11-05 11:08:10 -04001812}
1813EXPORT_SYMBOL(drm_state_dump);
1814
1815#ifdef CONFIG_DEBUG_FS
1816static int drm_state_info(struct seq_file *m, void *data)
1817{
1818 struct drm_info_node *node = (struct drm_info_node *) m->private;
1819 struct drm_device *dev = node->minor->dev;
1820 struct drm_printer p = drm_seq_file_printer(m);
1821
Daniel Vetterc2d85562017-04-03 10:32:54 +02001822 __drm_state_dump(dev, &p, true);
Rob Clark6559c902016-11-05 11:08:10 -04001823
1824 return 0;
1825}
1826
1827/* any use in debugfs files to dump individual planes/crtc/etc? */
1828static const struct drm_info_list drm_atomic_debugfs_list[] = {
1829 {"state", drm_state_info, 0},
1830};
1831
1832int drm_atomic_debugfs_init(struct drm_minor *minor)
1833{
1834 return drm_debugfs_create_files(drm_atomic_debugfs_list,
1835 ARRAY_SIZE(drm_atomic_debugfs_list),
1836 minor->debugfs_root, minor);
1837}
1838#endif
1839
Rob Clarkd34f20d2014-12-18 16:01:56 -05001840/*
1841 * The big monstor ioctl
1842 */
1843
1844static struct drm_pending_vblank_event *create_vblank_event(
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09001845 struct drm_device *dev, uint64_t user_data)
Rob Clarkd34f20d2014-12-18 16:01:56 -05001846{
1847 struct drm_pending_vblank_event *e = NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001848
1849 e = kzalloc(sizeof *e, GFP_KERNEL);
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001850 if (!e)
1851 return NULL;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001852
1853 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01001854 e->event.base.length = sizeof(e->event);
Rob Clarkd34f20d2014-12-18 16:01:56 -05001855 e->event.user_data = user_data;
Rob Clarkd34f20d2014-12-18 16:01:56 -05001856
Rob Clarkd34f20d2014-12-18 16:01:56 -05001857 return e;
1858}
1859
Rob Clarkd34f20d2014-12-18 16:01:56 -05001860static int atomic_set_prop(struct drm_atomic_state *state,
1861 struct drm_mode_object *obj, struct drm_property *prop,
1862 uint64_t prop_value)
1863{
1864 struct drm_mode_object *ref;
1865 int ret;
1866
1867 if (!drm_property_change_valid_get(prop, prop_value, &ref))
1868 return -EINVAL;
1869
1870 switch (obj->type) {
1871 case DRM_MODE_OBJECT_CONNECTOR: {
1872 struct drm_connector *connector = obj_to_connector(obj);
1873 struct drm_connector_state *connector_state;
1874
1875 connector_state = drm_atomic_get_connector_state(state, connector);
1876 if (IS_ERR(connector_state)) {
1877 ret = PTR_ERR(connector_state);
1878 break;
1879 }
1880
1881 ret = drm_atomic_connector_set_property(connector,
1882 connector_state, prop, prop_value);
1883 break;
1884 }
1885 case DRM_MODE_OBJECT_CRTC: {
1886 struct drm_crtc *crtc = obj_to_crtc(obj);
1887 struct drm_crtc_state *crtc_state;
1888
1889 crtc_state = drm_atomic_get_crtc_state(state, crtc);
1890 if (IS_ERR(crtc_state)) {
1891 ret = PTR_ERR(crtc_state);
1892 break;
1893 }
1894
1895 ret = drm_atomic_crtc_set_property(crtc,
1896 crtc_state, prop, prop_value);
1897 break;
1898 }
1899 case DRM_MODE_OBJECT_PLANE: {
1900 struct drm_plane *plane = obj_to_plane(obj);
1901 struct drm_plane_state *plane_state;
1902
1903 plane_state = drm_atomic_get_plane_state(state, plane);
1904 if (IS_ERR(plane_state)) {
1905 ret = PTR_ERR(plane_state);
1906 break;
1907 }
1908
1909 ret = drm_atomic_plane_set_property(plane,
1910 plane_state, prop, prop_value);
1911 break;
1912 }
1913 default:
1914 ret = -EINVAL;
1915 break;
1916 }
1917
1918 drm_property_change_valid_put(prop, ref);
1919 return ret;
1920}
1921
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001922/**
Maarten Lankhorst9744bf42015-11-24 10:34:34 +01001923 * drm_atomic_clean_old_fb -- Unset old_fb pointers and set plane->fb pointers.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001924 *
1925 * @dev: drm device to check.
1926 * @plane_mask: plane mask for planes that were updated.
1927 * @ret: return value, can be -EDEADLK for a retry.
1928 *
Daniel Vetterd5745282017-01-25 07:26:45 +01001929 * Before doing an update &drm_plane.old_fb is set to &drm_plane.fb, but before
1930 * dropping the locks old_fb needs to be set to NULL and plane->fb updated. This
1931 * is a common operation for each atomic update, so this call is split off as a
1932 * helper.
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001933 */
1934void drm_atomic_clean_old_fb(struct drm_device *dev,
1935 unsigned plane_mask,
1936 int ret)
1937{
1938 struct drm_plane *plane;
1939
1940 /* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1941 * locks (ie. while it is still safe to deref plane->state). We
1942 * need to do this here because the driver entry points cannot
1943 * distinguish between legacy and atomic ioctls.
1944 */
1945 drm_for_each_plane_mask(plane, dev, plane_mask) {
1946 if (ret == 0) {
1947 struct drm_framebuffer *new_fb = plane->state->fb;
1948 if (new_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001949 drm_framebuffer_get(new_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001950 plane->fb = new_fb;
1951 plane->crtc = plane->state->crtc;
1952
1953 if (plane->old_fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +01001954 drm_framebuffer_put(plane->old_fb);
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01001955 }
1956 plane->old_fb = NULL;
1957 }
1958}
1959EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1960
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001961/**
1962 * DOC: explicit fencing properties
1963 *
1964 * Explicit fencing allows userspace to control the buffer synchronization
1965 * between devices. A Fence or a group of fences are transfered to/from
1966 * userspace using Sync File fds and there are two DRM properties for that.
1967 * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1968 * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1969 *
1970 * As a contrast, with implicit fencing the kernel keeps track of any
1971 * ongoing rendering, and automatically ensures that the atomic update waits
1972 * for any pending rendering to complete. For shared buffers represented with
Daniel Vetterd5745282017-01-25 07:26:45 +01001973 * a &struct dma_buf this is tracked in &struct reservation_object.
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001974 * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1975 * whereas explicit fencing is what Android wants.
1976 *
1977 * "IN_FENCE_FD”:
1978 * Use this property to pass a fence that DRM should wait on before
1979 * proceeding with the Atomic Commit request and show the framebuffer for
1980 * the plane on the screen. The fence can be either a normal fence or a
1981 * merged one, the sync_file framework will handle both cases and use a
1982 * fence_array if a merged fence is received. Passing -1 here means no
1983 * fences to wait on.
1984 *
1985 * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1986 * it will only check if the Sync File is a valid one.
1987 *
1988 * On the driver side the fence is stored on the @fence parameter of
Daniel Vetterea0dd852016-12-29 21:48:26 +01001989 * &struct drm_plane_state. Drivers which also support implicit fencing
Gustavo Padovan9a83a712016-11-22 09:11:28 +09001990 * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1991 * to make sure there's consistent behaviour between drivers in precedence
1992 * of implicit vs. explicit fencing.
1993 *
1994 * "OUT_FENCE_PTR”:
1995 * Use this property to pass a file descriptor pointer to DRM. Once the
1996 * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1997 * the file descriptor number of a Sync File. This Sync File contains the
1998 * CRTC fence that will be signaled when all framebuffers present on the
1999 * Atomic Commit * request for that given CRTC are scanned out on the
2000 * screen.
2001 *
2002 * The Atomic Commit request fails if a invalid pointer is passed. If the
2003 * Atomic Commit request fails for any other reason the out fence fd
2004 * returned will be -1. On a Atomic Commit with the
2005 * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
2006 *
2007 * Note that out-fences don't have a special interface to drivers and are
Daniel Vetterea0dd852016-12-29 21:48:26 +01002008 * internally represented by a &struct drm_pending_vblank_event in struct
Gustavo Padovan9a83a712016-11-22 09:11:28 +09002009 * &drm_crtc_state, which is also used by the nonblocking atomic commit
2010 * helpers and for the DRM event handling for existing userspace.
2011 */
2012
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002013struct drm_out_fence_state {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002014 s32 __user *out_fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002015 struct sync_file *sync_file;
2016 int fd;
2017};
2018
2019static int setup_out_fence(struct drm_out_fence_state *fence_state,
2020 struct dma_fence *fence)
2021{
2022 fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
2023 if (fence_state->fd < 0)
2024 return fence_state->fd;
2025
2026 if (put_user(fence_state->fd, fence_state->out_fence_ptr))
2027 return -EFAULT;
2028
2029 fence_state->sync_file = sync_file_create(fence);
2030 if (!fence_state->sync_file)
2031 return -ENOMEM;
2032
2033 return 0;
2034}
2035
2036static int prepare_crtc_signaling(struct drm_device *dev,
2037 struct drm_atomic_state *state,
2038 struct drm_mode_atomic *arg,
2039 struct drm_file *file_priv,
2040 struct drm_out_fence_state **fence_state,
2041 unsigned int *num_fences)
2042{
2043 struct drm_crtc *crtc;
2044 struct drm_crtc_state *crtc_state;
2045 int i, ret;
2046
2047 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
2048 return 0;
2049
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002050 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Gustavo Padovan7e9081c2017-01-13 12:22:09 -02002051 s32 __user *fence_ptr;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002052
2053 fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
2054
2055 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
2056 struct drm_pending_vblank_event *e;
2057
2058 e = create_vblank_event(dev, arg->user_data);
2059 if (!e)
2060 return -ENOMEM;
2061
2062 crtc_state->event = e;
2063 }
2064
2065 if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2066 struct drm_pending_vblank_event *e = crtc_state->event;
2067
2068 if (!file_priv)
2069 continue;
2070
2071 ret = drm_event_reserve_init(dev, file_priv, &e->base,
2072 &e->event.base);
2073 if (ret) {
2074 kfree(e);
2075 crtc_state->event = NULL;
2076 return ret;
2077 }
2078 }
2079
2080 if (fence_ptr) {
2081 struct dma_fence *fence;
2082 struct drm_out_fence_state *f;
2083
2084 f = krealloc(*fence_state, sizeof(**fence_state) *
2085 (*num_fences + 1), GFP_KERNEL);
2086 if (!f)
2087 return -ENOMEM;
2088
2089 memset(&f[*num_fences], 0, sizeof(*f));
2090
2091 f[*num_fences].out_fence_ptr = fence_ptr;
2092 *fence_state = f;
2093
Gustavo Padovan35f8cc32016-12-06 15:47:17 -02002094 fence = drm_crtc_create_fence(crtc);
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002095 if (!fence)
2096 return -ENOMEM;
2097
2098 ret = setup_out_fence(&f[(*num_fences)++], fence);
2099 if (ret) {
2100 dma_fence_put(fence);
2101 return ret;
2102 }
2103
2104 crtc_state->event->base.fence = fence;
2105 }
2106 }
2107
2108 return 0;
2109}
2110
2111static void complete_crtc_signaling(struct drm_device *dev,
2112 struct drm_atomic_state *state,
2113 struct drm_out_fence_state *fence_state,
2114 unsigned int num_fences,
2115 bool install_fds)
2116{
2117 struct drm_crtc *crtc;
2118 struct drm_crtc_state *crtc_state;
2119 int i;
2120
2121 if (install_fds) {
2122 for (i = 0; i < num_fences; i++)
2123 fd_install(fence_state[i].fd,
2124 fence_state[i].sync_file->file);
2125
2126 kfree(fence_state);
2127 return;
2128 }
2129
Maarten Lankhorst5721a382017-01-16 10:37:40 +01002130 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002131 struct drm_pending_vblank_event *event = crtc_state->event;
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002132 /*
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002133 * Free the allocated event. drm_atomic_helper_setup_commit
2134 * can allocate an event too, so only free it if it's ours
2135 * to prevent a double free in drm_atomic_state_clear.
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002136 */
Maarten Lankhorst92c715f2017-01-31 10:25:25 +01002137 if (event && (event->base.fence || event->base.file_priv)) {
2138 drm_event_cancel_free(dev, &event->base);
2139 crtc_state->event = NULL;
2140 }
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002141 }
2142
2143 if (!fence_state)
2144 return;
2145
2146 for (i = 0; i < num_fences; i++) {
2147 if (fence_state[i].sync_file)
2148 fput(fence_state[i].sync_file->file);
2149 if (fence_state[i].fd >= 0)
2150 put_unused_fd(fence_state[i].fd);
2151
2152 /* If this fails log error to the user */
2153 if (fence_state[i].out_fence_ptr &&
2154 put_user(-1, fence_state[i].out_fence_ptr))
2155 DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
2156 }
2157
2158 kfree(fence_state);
2159}
2160
Rob Clarkd34f20d2014-12-18 16:01:56 -05002161int drm_mode_atomic_ioctl(struct drm_device *dev,
2162 void *data, struct drm_file *file_priv)
2163{
2164 struct drm_mode_atomic *arg = data;
2165 uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
2166 uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
2167 uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
2168 uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
2169 unsigned int copied_objs, copied_props;
2170 struct drm_atomic_state *state;
2171 struct drm_modeset_acquire_ctx ctx;
2172 struct drm_plane *plane;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002173 struct drm_out_fence_state *fence_state;
Maarten Lankhorst45723722015-11-11 11:29:08 +01002174 unsigned plane_mask;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002175 int ret = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002176 unsigned int i, j, num_fences;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002177
2178 /* disallow for drivers not supporting atomic: */
2179 if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
2180 return -EINVAL;
2181
2182 /* disallow for userspace that has not enabled atomic cap (even
2183 * though this may be a bit overkill, since legacy userspace
2184 * wouldn't know how to call this ioctl)
2185 */
2186 if (!file_priv->atomic)
2187 return -EINVAL;
2188
2189 if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
2190 return -EINVAL;
2191
2192 if (arg->reserved)
2193 return -EINVAL;
2194
2195 if ((arg->flags & DRM_MODE_PAGE_FLIP_ASYNC) &&
2196 !dev->mode_config.async_page_flip)
2197 return -EINVAL;
2198
2199 /* can't test and expect an event at the same time. */
2200 if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
2201 (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
2202 return -EINVAL;
2203
2204 drm_modeset_acquire_init(&ctx, 0);
2205
2206 state = drm_atomic_state_alloc(dev);
2207 if (!state)
2208 return -ENOMEM;
2209
2210 state->acquire_ctx = &ctx;
2211 state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
2212
2213retry:
Maarten Lankhorst45723722015-11-11 11:29:08 +01002214 plane_mask = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002215 copied_objs = 0;
2216 copied_props = 0;
Maarten Lankhorst7f5d6da2017-08-14 12:07:21 +02002217 fence_state = NULL;
2218 num_fences = 0;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002219
2220 for (i = 0; i < arg->count_objs; i++) {
2221 uint32_t obj_id, count_props;
2222 struct drm_mode_object *obj;
2223
2224 if (get_user(obj_id, objs_ptr + copied_objs)) {
2225 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002226 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002227 }
2228
2229 obj = drm_mode_object_find(dev, obj_id, DRM_MODE_OBJECT_ANY);
Dave Airlieb164d312016-04-27 11:10:09 +10002230 if (!obj) {
2231 ret = -ENOENT;
2232 goto out;
2233 }
2234
2235 if (!obj->properties) {
Thierry Reding020a2182017-02-28 15:46:38 +01002236 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002237 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002238 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002239 }
2240
Rob Clarkd34f20d2014-12-18 16:01:56 -05002241 if (get_user(count_props, count_props_ptr + copied_objs)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002242 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002243 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002244 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002245 }
2246
2247 copied_objs++;
2248
2249 for (j = 0; j < count_props; j++) {
2250 uint32_t prop_id;
2251 uint64_t prop_value;
2252 struct drm_property *prop;
2253
2254 if (get_user(prop_id, props_ptr + copied_props)) {
Thierry Reding020a2182017-02-28 15:46:38 +01002255 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002256 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002257 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002258 }
2259
Maarten Lankhorstf92f0532016-09-08 12:30:01 +02002260 prop = drm_mode_obj_find_prop_id(obj, prop_id);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002261 if (!prop) {
Thierry Reding020a2182017-02-28 15:46:38 +01002262 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002263 ret = -ENOENT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002264 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002265 }
2266
Guenter Roeck42c58142015-01-12 21:12:17 -08002267 if (copy_from_user(&prop_value,
2268 prop_values_ptr + copied_props,
2269 sizeof(prop_value))) {
Thierry Reding020a2182017-02-28 15:46:38 +01002270 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002271 ret = -EFAULT;
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002272 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002273 }
2274
2275 ret = atomic_set_prop(state, obj, prop, prop_value);
Dave Airlieb164d312016-04-27 11:10:09 +10002276 if (ret) {
Thierry Reding020a2182017-02-28 15:46:38 +01002277 drm_mode_object_put(obj);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002278 goto out;
Dave Airlieb164d312016-04-27 11:10:09 +10002279 }
Rob Clarkd34f20d2014-12-18 16:01:56 -05002280
2281 copied_props++;
2282 }
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002283
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002284 if (obj->type == DRM_MODE_OBJECT_PLANE && count_props &&
2285 !(arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)) {
Maarten Lankhorsta9cc54e2015-06-24 08:59:24 +02002286 plane = obj_to_plane(obj);
2287 plane_mask |= (1 << drm_plane_index(plane));
2288 plane->old_fb = plane->fb;
2289 }
Thierry Reding020a2182017-02-28 15:46:38 +01002290 drm_mode_object_put(obj);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002291 }
2292
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002293 ret = prepare_crtc_signaling(dev, state, arg, file_priv, &fence_state,
2294 &num_fences);
2295 if (ret)
2296 goto out;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002297
2298 if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
2299 ret = drm_atomic_check_only(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002300 } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
Maarten Lankhorstb837ba02016-04-26 16:11:35 +02002301 ret = drm_atomic_nonblocking_commit(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002302 } else {
Rob Clarkfceffb322016-11-05 11:08:09 -04002303 if (unlikely(drm_debug & DRM_UT_STATE))
2304 drm_atomic_print_state(state);
2305
Rob Clarkd34f20d2014-12-18 16:01:56 -05002306 ret = drm_atomic_commit(state);
2307 }
2308
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002309out:
Maarten Lankhorst0f45c262015-11-11 11:29:09 +01002310 drm_atomic_clean_old_fb(dev, plane_mask, ret);
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002311
Gustavo Padovanbeaf5af2016-11-16 22:00:21 +09002312 complete_crtc_signaling(dev, state, fence_state, num_fences, !ret);
Maarten Lankhorstc4749c92015-08-31 12:25:04 +02002313
Maarten Lankhorstec9f9322015-06-24 08:59:25 +02002314 if (ret == -EDEADLK) {
2315 drm_atomic_state_clear(state);
2316 drm_modeset_backoff(&ctx);
2317 goto retry;
2318 }
2319
Chris Wilson08536952016-10-14 13:18:18 +01002320 drm_atomic_state_put(state);
Rob Clarkd34f20d2014-12-18 16:01:56 -05002321
2322 drm_modeset_drop_locks(&ctx);
2323 drm_modeset_acquire_fini(&ctx);
2324
2325 return ret;
Rob Clarkd34f20d2014-12-18 16:01:56 -05002326}